Skip to content

Commit

Permalink
Merge branch 'main' into update-changes
Browse files Browse the repository at this point in the history
  • Loading branch information
lilleyse authored Feb 23, 2024
2 parents 269a9ec + a7ec5d2 commit e065226
Show file tree
Hide file tree
Showing 16 changed files with 236 additions and 5 deletions.
1 change: 1 addition & 0 deletions extern/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ add_external_project(
webpdecoder
turbojpeg
meshoptimizer
sqlite3
OPTIONS
CESIUM_TESTS_ENABLED=OFF
CESIUM_COVERAGE_ENABLED=OFF
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class Connection:

class ICesiumOmniverseInterface:
def __init__(self, *args, **kwargs) -> None: ...
def clear_accessor_cache(self) -> None: ...
def connect_to_ion(self) -> None: ...
def create_token(self, arg0: str) -> None: ...
def credits_available(self) -> bool: ...
Expand Down
31 changes: 31 additions & 0 deletions exts/cesium.omniverse/cesium/omniverse/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from .ui.asset_window import CesiumOmniverseAssetWindow
from .ui.debug_window import CesiumOmniverseDebugWindow
from .ui.main_window import CesiumOmniverseMainWindow
from .ui.settings_window import CesiumOmniverseSettingsWindow
from .ui.credits_viewport_frame import CesiumCreditsViewportFrame
from .ui.fabric_modal import CesiumFabricModal
from .models import AssetToAdd, RasterOverlayToAdd
Expand Down Expand Up @@ -53,6 +54,7 @@ def __init__(self) -> None:
self._main_window: Optional[CesiumOmniverseMainWindow] = None
self._asset_window: Optional[CesiumOmniverseAssetWindow] = None
self._debug_window: Optional[CesiumOmniverseDebugWindow] = None
self._settings_window: Optional[CesiumOmniverseSettingsWindow] = None
self._credits_viewport_frames: List[CesiumCreditsViewportFrame] = []
self._on_stage_subscription: Optional[carb.events.ISubscription] = None
self._on_update_subscription: Optional[carb.events.ISubscription] = None
Expand Down Expand Up @@ -82,13 +84,17 @@ def on_startup(self):
CesiumOmniverseAssetWindow.WINDOW_NAME, partial(self.show_assets_window, None)
)
ui.Workspace.set_show_window_fn(CesiumOmniverseDebugWindow.WINDOW_NAME, partial(self.show_debug_window, None))
ui.Workspace.set_show_window_fn(
CesiumOmniverseSettingsWindow.WINDOW_NAME, partial(self.show_settings_window, None)
)

settings = omni_settings.get_settings()
show_on_startup = settings.get_as_bool("/exts/cesium.omniverse/showOnStartup")

self._add_to_menu(CesiumOmniverseMainWindow.MENU_PATH, self.show_main_window, show_on_startup)
self._add_to_menu(CesiumOmniverseAssetWindow.MENU_PATH, self.show_assets_window, False)
self._add_to_menu(CesiumOmniverseDebugWindow.MENU_PATH, self.show_debug_window, False)
self._add_to_menu(CesiumOmniverseSettingsWindow.MENU_PATH, self.show_settings_window, False)

self._logger.info("CesiumOmniverse startup")

Expand Down Expand Up @@ -190,6 +196,10 @@ def on_shutdown(self):
self._debug_window.destroy()
self._debug_window = None

if self._settings_window is not None:
self._settings_window.destroy()
self._settings_window = None

if self._credits_viewport_controller is not None:
self._credits_viewport_controller.destroy()
self._credits_viewport_controller = None
Expand All @@ -198,6 +208,7 @@ def on_shutdown(self):
ui.Workspace.set_show_window_fn(CesiumOmniverseMainWindow.WINDOW_NAME, None)
ui.Workspace.set_show_window_fn(CesiumOmniverseAssetWindow.WINDOW_NAME, None)
ui.Workspace.set_show_window_fn(CesiumOmniverseDebugWindow.WINDOW_NAME, None)
ui.Workspace.set_show_window_fn(CesiumOmniverseSettingsWindow.WINDOW_NAME, None)

if self._on_stage_subscription is not None:
self._on_stage_subscription.unsubscribe()
Expand Down Expand Up @@ -411,6 +422,10 @@ async def _destroy_window_async(self, path):
if self._debug_window is not None:
self._debug_window.destroy()
self._debug_window = None
elif path is CesiumOmniverseSettingsWindow.MENU_PATH:
if self._settings_window is not None:
self._settings_window.destroy()
self._settings_window = None

def _visibility_changed_fn(self, path, visible):
# Called when the user pressed "X"
Expand Down Expand Up @@ -470,6 +485,22 @@ def show_debug_window(self, _menu, value):
elif self._debug_window is not None:
self._debug_window.visible = False

def show_settings_window(self, _menu, value):
if _cesium_omniverse_interface is None:
logging.error("Cesium Omniverse Interface is not set.")
return

if value:
self._settings_window = CesiumOmniverseSettingsWindow(
_cesium_omniverse_interface, CesiumOmniverseSettingsWindow.WINDOW_NAME, width=300, height=365
)
self._settings_window.set_visibility_changed_fn(
partial(self._visibility_changed_fn, CesiumOmniverseSettingsWindow.MENU_PATH)
)
asyncio.ensure_future(dock_window_async(self._settings_window))
elif self._settings_window is not None:
self._settings_window.visible = False

def _setup_credits_viewport_frames(self):
self._destroy_credits_viewport_frames()
self._credits_viewport_frames = [
Expand Down
52 changes: 52 additions & 0 deletions exts/cesium.omniverse/cesium/omniverse/ui/settings_window.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import logging
import carb.settings
from typing import Optional
import omni.ui as ui
from ..bindings import ICesiumOmniverseInterface
from cesium.omniverse.utils.custom_fields import int_field_with_label


class CesiumOmniverseSettingsWindow(ui.Window):
WINDOW_NAME = "Cesium Settings"
MENU_PATH = f"Window/Cesium/{WINDOW_NAME}"

_logger: logging.Logger
_cesium_omniverse_interface: Optional[ICesiumOmniverseInterface] = None

def __init__(self, cesium_omniverse_interface: ICesiumOmniverseInterface, title: str, **kwargs):
super().__init__(title, **kwargs)

self._logger = logging.getLogger(__name__)
self._cesium_omniverse_interface = cesium_omniverse_interface
self._cache_items_setting = "/persistent/exts/cesium.omniverse/maxCacheItems"

# Set the function that is called to build widgets when the window is visible
self.frame.set_build_fn(self._build_fn)

def destroy(self):
# It will destroy all the children
super().destroy()

def __del__(self):
self.destroy()

@staticmethod
def show_window():
ui.Workspace.show_window(CesiumOmniverseSettingsWindow.WINDOW_NAME)

def _build_fn(self):
"""Builds out the UI buttons and their handlers."""

def set_cache_parameters():
newval = self._cache_items_model.get_value_as_int()
carb.settings.get_settings().set(self._cache_items_setting, newval)

def clear_cache():
self._cesium_omniverse_interface.clear_accessor_cache()

with ui.VStack(spacing=4):
cache_items = carb.settings.get_settings().get(self._cache_items_setting)
self._cache_items_model = ui.SimpleIntModel(cache_items)
int_field_with_label("Maximum cache items", model=self._cache_items_model)
ui.Button("Set cache parameters (requires restart)", height=20, clicked_fn=set_cache_parameters)
ui.Button("Clear cache", height=20, clicked_fn=clear_cache)
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import omni.usd
from .proj import epsg_to_ecef, epsg_to_wgs84, get_crs_name_from_epsg
import math
from .custom_fields import string_field_with_label, int_field_with_label, float_field_with_label
from cesium.omniverse.utils.custom_fields import string_field_with_label, int_field_with_label, float_field_with_label
from pxr import Sdf


Expand Down
4 changes: 4 additions & 0 deletions include/cesium/omniverse/CesiumOmniverse.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ class ICesiumOmniverseInterface {
virtual void creditsStartNextFrame() noexcept = 0;

virtual bool isTracingEnabled() noexcept = 0;
/**
* @brief Clear the asset accessor cache.
*/
virtual void clearAccessorCache() = 0;
};

} // namespace cesium::omniverse
3 changes: 2 additions & 1 deletion src/bindings/PythonBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ PYBIND11_MODULE(CesiumOmniversePythonBindings, m) {
.def("credits_available", &ICesiumOmniverseInterface::creditsAvailable)
.def("get_credits", &ICesiumOmniverseInterface::getCredits)
.def("credits_start_next_frame", &ICesiumOmniverseInterface::creditsStartNextFrame)
.def("is_tracing_enabled", &ICesiumOmniverseInterface::isTracingEnabled);
.def("is_tracing_enabled", &ICesiumOmniverseInterface::isTracingEnabled)
.def("clear_accessor_cache", &ICesiumOmniverseInterface::clearAccessorCache);
// clang-format on

py::class_<CesiumIonSession, std::shared_ptr<CesiumIonSession>>(m, "CesiumIonSession")
Expand Down
1 change: 1 addition & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ setup_lib(
webpdecoder
turbojpeg
meshoptimizer
sqlite3
cpr::cpr
stb::stb
ZLIB::ZLIB
Expand Down
5 changes: 4 additions & 1 deletion src/core/include/cesium/omniverse/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class CreditSystem;
namespace CesiumAsync {
class AsyncSystem;
class IAssetAccessor;
class ICacheDatabase;
} // namespace CesiumAsync

namespace cesium::omniverse {
Expand Down Expand Up @@ -58,6 +59,7 @@ class Context {

void clearStage();
void reloadStage();
void clearAccessorCache();

void onUpdateFrame(const gsl::span<const Viewport>& viewports, bool waitForLoadingTiles);
void onUsdStageChanged(int64_t stageId);
Expand All @@ -78,9 +80,10 @@ class Context {

std::shared_ptr<TaskProcessor> _pTaskProcessor;
std::unique_ptr<CesiumAsync::AsyncSystem> _pAsyncSystem;
std::shared_ptr<Logger> _pLogger;
std::shared_ptr<CesiumAsync::IAssetAccessor> _pAssetAccessor;
std::shared_ptr<CesiumAsync::ICacheDatabase> _pCacheDatabase;
std::shared_ptr<CesiumUtility::CreditSystem> _pCreditSystem;
std::shared_ptr<Logger> _pLogger;
std::unique_ptr<AssetRegistry> _pAssetRegistry;
std::unique_ptr<FabricResourceManager> _pFabricResourceManager;
std::unique_ptr<CesiumIonServerManager> _pCesiumIonServerManager;
Expand Down
8 changes: 8 additions & 0 deletions src/core/include/cesium/omniverse/FilesystemUtil.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <filesystem>

namespace cesium::omniverse::FilesystemUtil {
std::filesystem::path getCesiumCacheDirectory();
std::filesystem::path getUserHomeDirectory();
} // namespace cesium::omniverse::FilesystemUtil
3 changes: 3 additions & 0 deletions src/core/include/cesium/omniverse/SettingsWrapper.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <cstdint>
#include <string>
#include <vector>

Expand All @@ -15,4 +16,6 @@ void setAccessToken(const AccessToken& accessToken);
void removeAccessToken(const std::string& ionApiUrl);
void clearTokens();

uint64_t getMaxCacheItems();

} // namespace cesium::omniverse::Settings
45 changes: 43 additions & 2 deletions src/core/src/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
#include "cesium/omniverse/FabricResourceManager.h"
#include "cesium/omniverse/FabricStatistics.h"
#include "cesium/omniverse/FabricUtil.h"
#include "cesium/omniverse/FilesystemUtil.h"
#include "cesium/omniverse/Logger.h"
#include "cesium/omniverse/OmniData.h"
#include "cesium/omniverse/OmniIonRasterOverlay.h"
#include "cesium/omniverse/OmniTileset.h"
#include "cesium/omniverse/RenderStatistics.h"
#include "cesium/omniverse/SettingsWrapper.h"
#include "cesium/omniverse/TaskProcessor.h"
#include "cesium/omniverse/TilesetStatistics.h"
#include "cesium/omniverse/UrlAssetAccessor.h"
Expand All @@ -22,6 +24,8 @@
#endif

#include <Cesium3DTilesContent/registerAllTileContentTypes.h>
#include <CesiumAsync/CachingAssetAccessor.h>
#include <CesiumAsync/SqliteCache.h>
#include <CesiumUtility/CreditSystem.h>
#include <omni/fabric/SimStageWithHistory.h>
#include <pxr/usd/sdf/path.h>
Expand All @@ -31,6 +35,31 @@

namespace cesium::omniverse {

namespace {

std::string getCacheDatabaseName() {
auto cacheDirPath = FilesystemUtil::getCesiumCacheDirectory();
if (!cacheDirPath.empty()) {
auto cacheFilePath = cacheDirPath / "cesium-request-cache.sqlite";
return cacheFilePath.generic_string();
}
return {};
}

std::shared_ptr<CesiumAsync::ICacheDatabase> makeCacheDatabase(const std::shared_ptr<Logger>& logger) {
uint64_t maxCacheItems = Settings::getMaxCacheItems();
if (maxCacheItems == 0) {
logger->oneTimeWarning("maxCacheItems set to 0, so disabling accessor cache");
return {};
} else if (auto dbName = getCacheDatabaseName(); !dbName.empty()) {
logger->oneTimeWarning(fmt::format("Cesium cache file: {}", dbName));
return std::make_shared<CesiumAsync::SqliteCache>(logger, dbName, maxCacheItems);
}
logger->oneTimeWarning("could not get name for cache database");
return {};
}
} // namespace

namespace {
uint64_t getSecondsSinceEpoch() {
const auto timePoint = std::chrono::system_clock::now();
Expand All @@ -43,15 +72,21 @@ Context::Context(const std::filesystem::path& cesiumExtensionLocation)
, _cesiumMdlPathToken(pxr::TfToken((_cesiumExtensionLocation / "mdl" / "cesium.mdl").generic_string()))
, _pTaskProcessor(std::make_shared<TaskProcessor>())
, _pAsyncSystem(std::make_unique<CesiumAsync::AsyncSystem>(_pTaskProcessor))
, _pAssetAccessor(std::make_shared<UrlAssetAccessor>())
, _pCreditSystem(std::make_shared<CesiumUtility::CreditSystem>())
, _pLogger(std::make_shared<Logger>())
, _pCacheDatabase(makeCacheDatabase(_pLogger))
, _pCreditSystem(std::make_shared<CesiumUtility::CreditSystem>())
, _pAssetRegistry(std::make_unique<AssetRegistry>(this))
, _pFabricResourceManager(std::make_unique<FabricResourceManager>(this))
, _pCesiumIonServerManager(std::make_unique<CesiumIonServerManager>(this))
, _pUsdNotificationHandler(std::make_unique<UsdNotificationHandler>(this))
, _contextId(static_cast<int64_t>(getSecondsSinceEpoch())) {
if (_pCacheDatabase) {
_pAssetAccessor = std::make_shared<CesiumAsync::CachingAssetAccessor>(
_pLogger, std::make_shared<UrlAssetAccessor>(), _pCacheDatabase);

} else {
_pAssetAccessor = std::make_shared<UrlAssetAccessor>();
}
Cesium3DTilesContent::registerAllTileContentTypes();

#if CESIUM_TRACING_ENABLED
Expand Down Expand Up @@ -149,6 +184,12 @@ void Context::reloadStage() {
}
}

void Context::clearAccessorCache() {
if (_pCacheDatabase) {
_pCacheDatabase->clearAll();
}
}

void Context::onUpdateFrame(const gsl::span<const Viewport>& viewports, bool waitForLoadingTiles) {
_pUsdNotificationHandler->onUpdateFrame();
_pAssetRegistry->onUpdateFrame(viewports, waitForLoadingTiles);
Expand Down
Loading

0 comments on commit e065226

Please sign in to comment.