Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add foundational CUDA support and basic kernel #459

Draft
wants to merge 23 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
08421f2
check in partially completed, need kernel to continue
corybarr Aug 22, 2023
a0bcc4e
fix merge conflict
corybarr Aug 22, 2023
1289ce1
add stub of CudaManager.h
corybarr Aug 23, 2023
2cc510b
Revert "Remove cuda from build. No longer needed for Kit 105"
lilleyse Aug 24, 2023
7dbbc2b
Fix all compilation errors
corybarr Aug 24, 2023
6477098
Merge branch 'main' of github.com:CesiumGS/cesium-omniverse into add-…
corybarr Aug 24, 2023
b06bbb1
update extern/CMakeLists.txt
corybarr Aug 24, 2023
d5546b2
add nvrtc and cuda to CMakeLists.txt
corybarr Aug 24, 2023
1c3c770
manually revert commit 34e6e3ff6917ef59d2786540fe4a5ac6f5e9f7f6
corybarr Aug 25, 2023
c844568
add in tileId
corybarr Aug 25, 2023
ab04c1f
enable basic print-to-screen kernel
corybarr Aug 25, 2023
18422c3
run placeholder kernel for every tile
corybarr Aug 25, 2023
0324bd4
merge with add-cuda-cmake
corybarr Aug 28, 2023
3e37801
remove cudart from CMakeLists.txt
corybarr Aug 28, 2023
643f76d
delete CudaRunners when tile is freed
corybarr Aug 28, 2023
f0ec135
clang formatting fixes
corybarr Aug 28, 2023
701b60b
cosmetic
corybarr Aug 28, 2023
f8a62f5
clean up CudaManager.h
corybarr Aug 28, 2023
8313f49
clean up CudaManager
corybarr Aug 28, 2023
39a702d
fix variable shadowing
corybarr Aug 28, 2023
49b897e
remove redundant lines in CMakeLists.txt
Aug 30, 2023
866ee66
Merge branch 'add-cuda-cmake' into add-cuda
corybarr Aug 31, 2023
f08be11
Revert "Temporarily remove nvrtc and nvrtc-builtins from install"
lilleyse Oct 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,22 @@ install(
COMPONENT library
EXCLUDE_FROM_ALL)

# Nothing links against nvrtc-builtins but if we don't include it we get a runtime crash.
# Unfortunately this means we have to bypass CMake's dependency system and install it manually here.
if(WIN32)
install(
FILES "${PROJECT_SOURCE_DIR}/extern/nvidia/_build/target-deps/cuda/cuda/bin/nvrtc-builtins64_118.dll"
DESTINATION "${KIT_EXTENSION_BIN_PATH}"
COMPONENT install)
else()
install(
FILES "${PROJECT_SOURCE_DIR}/extern/nvidia/_build/target-deps/cuda/cuda/lib64/libnvrtc-builtins.so"
"${PROJECT_SOURCE_DIR}/extern/nvidia/_build/target-deps/cuda/cuda/lib64/libnvrtc-builtins.so.11.8"
"${PROJECT_SOURCE_DIR}/extern/nvidia/_build/target-deps/cuda/cuda/lib64/libnvrtc-builtins.so.11.8.89"
DESTINATION "${KIT_EXTENSION_BIN_PATH}"
COMPONENT install)
endif()

install(
TARGETS CesiumOmniversePythonBindings
ARCHIVE DESTINATION ${KIT_EXTENSION_BINDINGS_PATH} COMPONENT install
Expand Down
6 changes: 6 additions & 0 deletions exts/cesium.omniverse/config/extension.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ archiveDirs = ["vendor"]
[[native.plugin]]
path = "bin/cesium.omniverse.plugin"

[[native.library]]
"filter:platform"."windows-x86_64"."path" = "bin/${lib_prefix}nvrtc-builtins64_118${lib_ext}"

[[native.library]]
"filter:platform"."linux-x86_64"."path" = "bin/${lib_prefix}nvrtc-builtins${lib_ext}"

[settings]
exts."cesium.omniverse".defaultAccessToken = ""
persistent.exts."cesium.omniverse".userAccessToken = ""
Expand Down
2 changes: 2 additions & 0 deletions src/core/include/cesium/omniverse/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class Context {

int64_t getContextId() const;
int64_t getNextTilesetId() const;
int64_t getNextTileId() const;

const CesiumGeospatial::Cartographic getGeoreferenceOrigin() const;
void setGeoreferenceOrigin(const CesiumGeospatial::Cartographic& origin);
Expand Down Expand Up @@ -151,6 +152,7 @@ class Context {
int64_t _contextId;

mutable std::atomic<int64_t> _tilesetId{};
mutable std::atomic<int64_t> _tileId{};

std::filesystem::path _cesiumExtensionLocation;
std::filesystem::path _certificatePath;
Expand Down
38 changes: 38 additions & 0 deletions src/core/include/cesium/omniverse/CudaKernels.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once

namespace cesium::omniverse::cudaKernels {

inline const char* printPointsKernel = R"(

extern "C" __global__ void printPoints(float3** points, int numPoints) {
const size_t i = blockIdx.x * blockDim.x + threadIdx.x;
if (i >= numPoints) return;

int pointIndex = static_cast<int>(i);

printf("point %d: %f, %f, %f\n", pointIndex, points[0][pointIndex].x, points[0][pointIndex].y, points[0][pointIndex].z);
}
)";

inline const char* helloWorldKernel = R"(
extern "C" __global__
void helloWorld(double* values, size_t count)
{
size_t i = blockIdx.x * blockDim.x + threadIdx.x;
if (count <= i) return;

printf("Hello world, from index %llu\n", i);
}
)";

inline const char* createVoxelsKernel = R"(
extern "C" __global__
void createVoxels(float3** points, size_t count)
{
size_t i = blockIdx.x * blockDim.x + threadIdx.x;
if (count <= i) return;

// printf("Placeholder: create voxel at index %llu\n", i);
}
)";
} // namespace cesium::omniverse::cudaKernels
97 changes: 97 additions & 0 deletions src/core/include/cesium/omniverse/CudaManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#pragma once

#include "cesium/omniverse/CudaKernels.h"
#include "cesium/omniverse/CudaManager.h"

#include <cuda/include/cuda.h>
#include <cuda/include/cuda_runtime.h>
#include <cuda/include/nvrtc.h>
#include <omni/fabric/FabricUSD.h>
#include <omni/fabric/IFabric.h>

#include <any>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

namespace cesium::omniverse {
class CudaManager;
class CudaRunner;
struct CudaKernel;
struct CudaKernelArgs;

enum CudaKernelType { HELLO_WORLD, CREATE_VOXELS, PRINT_POINTS };
enum CudaUpdateType { ONCE, ON_UPDATE_FRAME };

struct CudaKernelArgs {
std::unordered_map<std::string, std::any> args;
};

struct CudaKernel {
nvrtcProgram program;
char* ptx;
CUmodule module;
CUfunction function;
};

class CudaRunner {
public:
CudaKernelType kernelType;

CudaRunner() = delete;
CudaRunner(
CudaKernelType cudaKernelType,
CudaUpdateType updateType,
int64_t tileId,
CudaKernelArgs args,
int elementCountArg)
: kernelType(cudaKernelType)
, kernelArgs(std::move(args))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This clang convention of starting with a comma seems odd. Is this correct?

, elementCount(elementCountArg)
, _tileId(tileId)
, _updateType(updateType){};
[[nodiscard]] int64_t getTileId() const {
return _tileId;
}
CudaKernelArgs kernelArgs;
[[nodiscard]] const CudaUpdateType& getUpdateType() const {
return _updateType;
}
int elementCount;

private:
// omni::fabric::PrimBucketList _bucketList;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Placeholder for incorporating the kernel args in the next PR.

int64_t _tileId;
CudaUpdateType _updateType;
};

class CudaManager {
public:
static CudaManager& getInstance() {
static CudaManager instance;
return instance;
}

void onUpdateFrame();
void addRunner(CudaRunner& cudaRunner);
void removeRunner(int64_t tileId);
[[nodiscard]] const char* getKernelCode(CudaKernelType kernelType) const;
[[nodiscard]] const char* getFunctionName(CudaKernelType kernelType) const;

private:
CUdevice _device;
CUcontext _context;
bool _initialized = false;
std::unordered_map<CudaUpdateType, std::unordered_map<int64_t, CudaRunner>> _runnersByUpdateType;
std::unordered_map<CudaKernelType, CudaKernel> _kernels;
int _blockSize, _numBlocks;

void compileKernel(CudaKernelType kernelType);
void runAllRunners();
void initialize();
void runRunner(CudaRunner& runner);
void** packArgs(CudaKernelArgs cudaKernelArgs, CudaKernelType cudaKernelType);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Placeholder for next PR. Signature might change.

};
} // namespace cesium::omniverse
5 changes: 4 additions & 1 deletion src/core/include/cesium/omniverse/FabricGeometry.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "cesium/omniverse/CudaManager.h"
#include "cesium/omniverse/FabricGeometryDefinition.h"

#include <glm/glm.hpp>
Expand All @@ -24,13 +25,15 @@ class FabricGeometry {

void setGeometry(
int64_t tilesetId,
int64_t tileId,
const glm::dmat4& ecefToUsdTransform,
const glm::dmat4& gltfToEcefTransform,
const glm::dmat4& nodeTransform,
const CesiumGltf::Model& model,
const CesiumGltf::MeshPrimitive& primitive,
bool smoothNormals,
bool hasImagery);
bool hasImagery,
float geometricError);

void setActive(bool active);
void setVisibility(bool visible);
Expand Down
4 changes: 2 additions & 2 deletions src/core/include/cesium/omniverse/FabricMaterial.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class FabricMaterial {
long stageId);
~FabricMaterial();

void setMaterial(int64_t tilesetId, const MaterialInfo& materialInfo);
void setMaterial(int64_t tilesetId, int64_t tileId, const MaterialInfo& materialInfo);
void setBaseColorTexture(const pxr::TfToken& textureAssetPathToken, const TextureInfo& textureInfo);

void clearMaterial();
Expand All @@ -50,7 +50,7 @@ class FabricMaterial {
const omni::fabric::Path& texturePath,
const pxr::TfToken& textureAssetPathToken,
const TextureInfo& textureInfo);
void setTilesetId(int64_t tilesetId);
void setTilesetIdAndTileId(int64_t tilesetId, int64_t tileId);
bool stageDestroyed();

omni::fabric::Path _materialPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ struct FabricMesh {
struct TileRenderResources {
glm::dmat4 tileTransform;
std::vector<FabricMesh> fabricMeshes;
int64_t tileId;
};

class FabricPrepareRenderResources final : public Cesium3DTilesSelection::IPrepareRendererResources {
Expand Down
3 changes: 2 additions & 1 deletion src/core/include/cesium/omniverse/FabricUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ struct FabricStatistics {

// -1 means the prim is not yet associated with a tileset
const auto NO_TILESET_ID = int64_t(-1);
const auto NO_TILE_ID = int64_t(-1);

} // namespace cesium::omniverse

Expand All @@ -31,7 +32,7 @@ std::string printFabricStage();
FabricStatistics getStatistics();
void destroyPrim(const omni::fabric::Path& path);
void setTilesetTransform(int64_t tilesetId, const glm::dmat4& ecefToUsdTransform);
void setTilesetId(const omni::fabric::Path& path, int64_t tilesetId);
void setTilesetIdAndTileId(const omni::fabric::Path& pathFabric, int64_t tilesetId, int64_t tileId);
omni::fabric::Path toFabricPath(const pxr::SdfPath& path);
omni::fabric::Path joinPaths(const omni::fabric::Path& absolutePath, const omni::fabric::Token& relativePath);

Expand Down
2 changes: 2 additions & 0 deletions src/core/include/cesium/omniverse/Tokens.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ __pragma(warning(push)) __pragma(warning(disable : 4003))
(vertexColor) \
(_cesium_localToEcefTransform) \
(_cesium_tilesetId) \
(_cesium_tileId) \
(_deletedPrims) \
(_paramColorSpace) \
(_sdrMetadata) \
Expand Down Expand Up @@ -144,6 +145,7 @@ const omni::fabric::Type primvars_vertexColor(omni::fabric::BaseDataType::eFloat
const omni::fabric::Type Shader(omni::fabric::BaseDataType::eTag, 1, 0, omni::fabric::AttributeRole::ePrimTypeName);
const omni::fabric::Type subdivisionScheme(omni::fabric::BaseDataType::eToken, 1, 0, omni::fabric::AttributeRole::eNone);
const omni::fabric::Type _cesium_localToEcefTransform(omni::fabric::BaseDataType::eDouble, 16, 0, omni::fabric::AttributeRole::eMatrix);
const omni::fabric::Type _cesium_tileId(omni::fabric::BaseDataType::eInt64, 1, 0, omni::fabric::AttributeRole::eNone);
const omni::fabric::Type _cesium_tilesetId(omni::fabric::BaseDataType::eInt64, 1, 0, omni::fabric::AttributeRole::eNone);
const omni::fabric::Type _paramColorSpace(omni::fabric::BaseDataType::eToken, 1, 1, omni::fabric::AttributeRole::eNone);
const omni::fabric::Type _sdrMetadata(omni::fabric::BaseDataType::eToken, 1, 1, omni::fabric::AttributeRole::eNone);
Expand Down
7 changes: 7 additions & 0 deletions src/core/src/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "cesium/omniverse/AssetRegistry.h"
#include "cesium/omniverse/Broadcast.h"
#include "cesium/omniverse/CesiumIonSession.h"
#include "cesium/omniverse/CudaManager.h"
#include "cesium/omniverse/FabricResourceManager.h"
#include "cesium/omniverse/FabricUtil.h"
#include "cesium/omniverse/GeospatialUtil.h"
Expand Down Expand Up @@ -205,6 +206,8 @@ void Context::onUpdateFrame(const std::vector<Viewport>& viewports) {
for (const auto& tileset : tilesets) {
tileset->onUpdateFrame(viewports);
}

CudaManager::getInstance().onUpdateFrame();
}

void Context::processPropertyChanged(const ChangedPrim& changedPrim) {
Expand Down Expand Up @@ -420,6 +423,10 @@ int64_t Context::getNextTilesetId() const {
return _tilesetId++;
}

int64_t Context::getNextTileId() const {
return _tileId++;
}

const CesiumGeospatial::Cartographic Context::getGeoreferenceOrigin() const {
const auto georeference = UsdUtil::getOrCreateCesiumGeoreference();

Expand Down
Loading
Loading