Skip to content

Commit

Permalink
Adding C-compatible interface
Browse files Browse the repository at this point in the history
* Adding c-compatible interface. C header is now the SDK native API which is wrapped by a C++ version.
* Some (trivial) rename of functions and member structs. 
* Code for the C (omm.h) and C++ (omm.hpp) are generated from the omm.json via python. (generation off by default, only useful for active SDK development. Can be enabled via OMM_ENABLE_INTERFACE_GEN)
* Other: Fixing bug where version.h wasn't properly read in CMake
  • Loading branch information
nv-jdeligiannis authored Jan 25, 2023
1 parent 2402bf7 commit 9c6adf7
Show file tree
Hide file tree
Showing 47 changed files with 6,012 additions and 1,910 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ build.ninja
compile_commands.json
*.log

# gitlab
.gitlab-ci.yml

# Output files
bin/
install/
10 changes: 5 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.12)

option(OMM_ENABLE_BENCHMARK "Enable benchmark" ON)
option(OMM_ENABLE_TESTS "Enable unit test" ON)

file(READ "${CMAKE_CURRENT_SOURCE_DIR}/omm-sdk/include/omm.h" ver_h)
string(REGEX MATCH "OMM_VERSION_MAJOR ([0-9]*)" _ ${ver_h})
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/omm-sdk/src/version.h" ver_h)
string(REGEX MATCH "VERSION_MAJOR ([0-9]*)" _ ${ver_h})
set(ver_major ${CMAKE_MATCH_1})
string(REGEX MATCH "OMM_VERSION_MINOR ([0-9]*)" _ ${ver_h})
string(REGEX MATCH "VERSION_MINOR ([0-9]*)" _ ${ver_h})
set(ver_minor ${CMAKE_MATCH_1})
string(REGEX MATCH "OMM_VERSION_BUILD ([0-9]*)" _ ${ver_h})
string(REGEX MATCH "VERSION_BUILD ([0-9]*)" _ ${ver_h})
set(ver_patch ${CMAKE_MATCH_1})

project(omm-sdk VERSION ${ver_major}.${ver_minor}.${ver_patch} LANGUAGES CXX)
Expand Down
2 changes: 1 addition & 1 deletion benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.12)

add_executable(benchmarks bm_ommbake.cpp)

Expand Down
12 changes: 6 additions & 6 deletions benchmark/bm_ommbake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ license agreement from NVIDIA CORPORATION is strictly prohibited.
#include <random>

#include <benchmark/benchmark.h>
#include <omm.h>
#include <omm.hpp>
#include <shared/bird.h>

class OMMBake : public benchmark::Fixture {
protected:
void SetUp(const ::benchmark::State& state) override {
omm::CreateOpacityMicromapBaker({ .type = omm::BakerType::CPU }, &_baker);
omm::CreateBaker({ .type = omm::BakerType::CPU }, &_baker);

omm::Cpu::TextureFlags flags = (omm::Cpu::TextureFlags)state.range(0);
_extraBakeFlags = (omm::Cpu::BakeFlags)state.range(1);
Expand Down Expand Up @@ -71,7 +71,7 @@ class OMMBake : public benchmark::Fixture {

void TearDown(const ::benchmark::State& state) override {
omm::Cpu::DestroyTexture(_baker, _texture);
omm::DestroyOpacityMicromapBaker(_baker);
omm::DestroyBaker(_baker);
}

void RunVmBake(benchmark::State& st, bool parallel, omm::TextureFilterMode filter) {
Expand Down Expand Up @@ -101,12 +101,12 @@ class OMMBake : public benchmark::Fixture {
st.ResumeTiming();

omm::Cpu::BakeResult res = 0;
omm::Cpu::BakeOpacityMicromap(_baker, desc, &res);
omm::Cpu::Bake(_baker, desc, &res);

st.PauseTiming();
const omm::Cpu::BakeResultDesc* resDesc = nullptr;
omm::Cpu::GetBakeResultDesc(res, resDesc);
volatile size_t totalSize = resDesc->ommArrayDataSize;
omm::Cpu::GetBakeResultDesc(res, &resDesc);
volatile size_t totalSize = resDesc->arrayDataSize;
volatile size_t totalSize2 = totalSize;

omm::Debug::Stats stats = omm::Debug::Stats{};
Expand Down
67 changes: 29 additions & 38 deletions integration/omm-sdk-nvrhi/omm-sdk-nvrhi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ void GpuBakeNvrhi::InitStaticBuffers(nvrhi::CommandListHandle commandList)
{
{
size_t size = 0;
omm::Result res = omm::Gpu::GetStaticResourceData(omm::Gpu::ResourceType::STATIC_VERTEX_BUFFER, nullptr, size);
omm::Result res = omm::Gpu::GetStaticResourceData(omm::Gpu::ResourceType::STATIC_VERTEX_BUFFER, nullptr, &size);
assert(res == omm::Result::SUCCESS);

std::vector<uint8_t> vertexData(size);
res = omm::Gpu::GetStaticResourceData(omm::Gpu::ResourceType::STATIC_VERTEX_BUFFER, vertexData.data(), size);
res = omm::Gpu::GetStaticResourceData(omm::Gpu::ResourceType::STATIC_VERTEX_BUFFER, vertexData.data(), &size);
assert(res == omm::Result::SUCCESS);

nvrhi::BufferDesc bufferDesc;
Expand All @@ -186,11 +186,11 @@ void GpuBakeNvrhi::InitStaticBuffers(nvrhi::CommandListHandle commandList)

{
size_t size = 0;
omm::Result res = omm::Gpu::GetStaticResourceData(omm::Gpu::ResourceType::STATIC_INDEX_BUFFER, nullptr, size);
omm::Result res = omm::Gpu::GetStaticResourceData(omm::Gpu::ResourceType::STATIC_INDEX_BUFFER, nullptr, &size);
assert(res == omm::Result::SUCCESS);

std::vector<uint8_t> indexData(size);
res = omm::Gpu::GetStaticResourceData(omm::Gpu::ResourceType::STATIC_INDEX_BUFFER, indexData.data(), size);
res = omm::Gpu::GetStaticResourceData(omm::Gpu::ResourceType::STATIC_INDEX_BUFFER, indexData.data(), &size);
assert(res == omm::Result::SUCCESS);

nvrhi::BufferDesc bufferDesc;
Expand Down Expand Up @@ -251,7 +251,7 @@ void GpuBakeNvrhi::InitBaker(ShaderProviderCb* shaderProviderCb)
desc.type = omm::BakerType::GPU;
desc.enableValidation = true;

omm::Result res = omm::CreateOpacityMicromapBaker(desc, &m_baker);
omm::Result res = omm::CreateBaker(desc, &m_baker);
assert(res == omm::Result::SUCCESS);
}

Expand All @@ -260,7 +260,7 @@ void GpuBakeNvrhi::InitBaker(ShaderProviderCb* shaderProviderCb)
desc.type = omm::BakerType::CPU;
desc.enableValidation = true;

omm::Result res = omm::CreateOpacityMicromapBaker(desc, &m_cpuBaker);
omm::Result res = omm::CreateBaker(desc, &m_cpuBaker);
assert(res == omm::Result::SUCCESS);
}

Expand All @@ -272,7 +272,7 @@ void GpuBakeNvrhi::InitBaker(ShaderProviderCb* shaderProviderCb)
assert(res == omm::Result::SUCCESS);

const omm::Gpu::BakePipelineInfoDesc* desc;
res = omm::Gpu::GetPipelineDesc(m_pipeline, desc);
res = omm::Gpu::GetPipelineDesc(m_pipeline, &desc);
assert(res == omm::Result::SUCCESS);

SetupPipelines(desc, shaderProviderCb);
Expand All @@ -288,10 +288,10 @@ void GpuBakeNvrhi::DestroyBaker()
omm::Result res = omm::Gpu::DestroyPipeline(m_baker, m_pipeline);
assert(res == omm::Result::SUCCESS);

res = omm::DestroyOpacityMicromapBaker(m_baker);
res = omm::DestroyBaker(m_baker);
assert(res == omm::Result::SUCCESS);

res = omm::DestroyOpacityMicromapBaker(m_cpuBaker);
res = omm::DestroyBaker(m_cpuBaker);
assert(res == omm::Result::SUCCESS);
}

Expand Down Expand Up @@ -407,7 +407,7 @@ void GpuBakeNvrhi::SetupPipelines(
case omm::Gpu::PipelineType::Graphics:
{
const omm::Gpu::GraphicsPipelineDesc& gfx = pipeline.graphics;
static_assert(omm::Gpu::GraphicsPipelineDesc::VERSION == 1, "New GFX pipeline version detected, update integration code.");
static_assert((uint32_t)omm::Gpu::GraphicsPipelineDescVersion::VERSION == 2, "New GFX pipeline version detected, update integration code.");

nvrhi::ShaderHandle vertex;
if (shaderProviderCb)
Expand Down Expand Up @@ -458,27 +458,20 @@ void GpuBakeNvrhi::SetupPipelines(
nvrhi::InputLayoutHandle inputLayout;
{
nvrhi::VertexAttributeDesc desc;
desc.name = omm::Gpu::GraphicsPipelineDesc::InputElementDesc::semanticName;
desc.name = omm::Gpu::GraphicsPipelineInputElementDesc::semanticName;
desc.format = nvrhi::Format::R32_UINT;
desc.elementStride = sizeof(uint32_t);
static_assert(omm::Gpu::GraphicsPipelineDesc::InputElementDesc::format == omm::Gpu::BufferFormat::R32_UINT);
static_assert(omm::Gpu::GraphicsPipelineInputElementDesc::format == omm::Gpu::BufferFormat::R32_UINT);
desc.arraySize = 1;
static_assert(omm::Gpu::GraphicsPipelineDesc::inputElementDescCount == 1);
desc.bufferIndex = 0;
static_assert(omm::Gpu::GraphicsPipelineDesc::InputElementDesc::inputSlot == 0);
static_assert(omm::Gpu::GraphicsPipelineInputElementDesc::inputSlot == 0);
desc.offset = 0;
static_assert(omm::Gpu::GraphicsPipelineDesc::InputElementDesc::semanticIndex == 0);
static_assert(omm::Gpu::GraphicsPipelineInputElementDesc::semanticIndex == 0);
inputLayout = m_device->createInputLayout(&desc, 1 /*attributeCount*/, vertex);
}

nvrhi::GraphicsPipelineHandle pipeline;
{
static_assert(omm::Gpu::GraphicsPipelineDesc::RasterState::cullMode == omm::Gpu::RasterCullMode::None);
static_assert(omm::Gpu::GraphicsPipelineDesc::topology == omm::Gpu::PrimitiveTopology::TriangleList);
static_assert(omm::Gpu::GraphicsPipelineDesc::DepthState::depthTestEnable == false);
static_assert(omm::Gpu::GraphicsPipelineDesc::DepthState::depthWriteEnable == false);
static_assert(omm::Gpu::GraphicsPipelineDesc::DepthState::stencilEnable == false);

nvrhi::GraphicsPipelineDesc gfxDesc;
gfxDesc.primType = nvrhi::PrimitiveType::TriangleList;
gfxDesc.renderState.depthStencilState.disableDepthTest();
Expand All @@ -489,7 +482,7 @@ void GpuBakeNvrhi::SetupPipelines(
gfxDesc.PS = pixel;
gfxDesc.bindingLayouts = { layout };
gfxDesc.inputLayout = inputLayout;
gfxDesc.renderState.rasterState.conservativeRasterEnable = gfx.rasterState.conservativeRasterization;
gfxDesc.renderState.rasterState.conservativeRasterEnable = gfx.conservativeRasterization;
gfxDesc.renderState.rasterState.cullMode = nvrhi::RasterCullMode::None;
gfxDesc.renderState.rasterState.frontCounterClockwise = true;
gfxDesc.renderState.rasterState.enableScissor(); // <- This is to prevent the framebuffer from implicitly setting the scissor rect...
Expand Down Expand Up @@ -540,10 +533,8 @@ omm::Gpu::BakeDispatchConfigDesc GpuBakeNvrhi::GetConfig(const Input& params)
config.texCoordStrideInBytes = params.texCoordStrideInBytes;
config.indexFormat = IndexFormat::I32_UINT;
config.indexCount = (uint32_t)params.numIndices;
config.globalOMMFormat = params.use2State ? OMMFormat::OC1_2_State : OMMFormat::OC1_4_State;
config.supportedOMMFormats[0] = params.use2State ? OMMFormat::OC1_2_State : OMMFormat::OC1_4_State;
config.numSupportedOMMFormats = 1;
config.maxScratchMemorySize = params.minimalMemoryMode ? Gpu::ScratchMemoryBudget::MB_4 : Gpu::ScratchMemoryBudget::HighMemory;
config.globalFormat = params.use2State ? Format::OC1_2_State : Format::OC1_4_State;
config.maxScratchMemorySize = params.minimalMemoryMode ? Gpu::ScratchMemoryBudget::MB_4 : Gpu::ScratchMemoryBudget::MB_256;
config.maxSubdivisionLevel = params.globalSubdivisionLevel;
config.globalSubdivisionLevel = params.globalSubdivisionLevel;
config.dynamicSubdivisionScale = params.dynamicSubdivisionScale;
Expand Down Expand Up @@ -611,7 +602,7 @@ void GpuBakeNvrhi::RunBake(
ReserveScratchBuffers(preBuildInfo);

const omm::Gpu::BakeDispatchChain* dispatchDesc = nullptr;
res = Gpu::Bake(m_pipeline, config, dispatchDesc);
res = Gpu::Bake(m_pipeline, config, &dispatchDesc);
assert(res == omm::Result::SUCCESS);

ExecuteBakeOperation(commandList, params, result, dispatchDesc);
Expand Down Expand Up @@ -799,7 +790,7 @@ void GpuBakeNvrhi::ExecuteBakeOperation(
};

const omm::Gpu::BakePipelineInfoDesc* pipelineDesc;
omm::Result res = omm::Gpu::GetPipelineDesc(m_pipeline, pipelineDesc);
omm::Result res = omm::Gpu::GetPipelineDesc(m_pipeline, &pipelineDesc);
assert(res == omm::Result::SUCCESS);

assert(m_globalCBuffer && m_globalCBuffer->getDesc().byteSize >= pipelineDesc->globalConstantBufferDesc.maxDataSize);
Expand Down Expand Up @@ -988,16 +979,16 @@ void GpuBakeNvrhi::DumpDebug(
const omm::IndexFormat ommIndexBufferFormat = indexBufferFormat == nvrhi::Format::R32_UINT ? omm::IndexFormat::I32_UINT : omm::IndexFormat::I16_UINT;

omm::Cpu::BakeResultDesc result;
result.ommArrayData = ommArrayBuffer.data();
result.ommArrayDataSize = (uint32_t)ommArrayBuffer.size();
result.ommDescArray = (const omm::Cpu::OpacityMicromapDesc*)ommDescBuffer.data();
result.ommDescArrayCount = (uint32_t)(ommDescBuffer.size() / sizeof(omm::Cpu::OpacityMicromapDesc));
result.ommIndexBuffer = ommIndexBuffer.data();
result.ommIndexFormat = ommIndexBufferFormat;
result.ommDescArrayHistogramCount = (uint32_t)(ommDescArrayHistogramBuffer.size() / sizeof(omm::Cpu::OpacityMicromapUsageCount));
result.ommDescArrayHistogram = (const omm::Cpu::OpacityMicromapUsageCount*)ommDescArrayHistogramBuffer.data();
result.ommIndexHistogramCount = (uint32_t)(ommIndexHistogramBuffer.size() / sizeof(omm::Cpu::OpacityMicromapUsageCount));
result.ommIndexHistogram = (const omm::Cpu::OpacityMicromapUsageCount*)ommIndexHistogramBuffer.data();
result.arrayData = ommArrayBuffer.data();
result.arrayDataSize = (uint32_t)ommArrayBuffer.size();
result.descArray = (const omm::Cpu::OpacityMicromapDesc*)ommDescBuffer.data();
result.descArrayCount = (uint32_t)(ommDescBuffer.size() / sizeof(omm::Cpu::OpacityMicromapDesc));
result.indexBuffer = ommIndexBuffer.data();
result.indexFormat = ommIndexBufferFormat;
result.descArrayHistogramCount = (uint32_t)(ommDescArrayHistogramBuffer.size() / sizeof(omm::Cpu::OpacityMicromapUsageCount));
result.descArrayHistogram = (const omm::Cpu::OpacityMicromapUsageCount*)ommDescArrayHistogramBuffer.data();
result.indexHistogramCount = (uint32_t)(ommIndexHistogramBuffer.size() / sizeof(omm::Cpu::OpacityMicromapUsageCount));
result.indexHistogram = (const omm::Cpu::OpacityMicromapUsageCount*)ommIndexHistogramBuffer.data();

omm::Cpu::TextureMipDesc mip;
mip.width = width;
Expand Down
2 changes: 1 addition & 1 deletion integration/omm-sdk-nvrhi/omm-sdk-nvrhi.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ license agreement from NVIDIA CORPORATION is strictly prohibited.

#pragma once

#include <omm.h>
#include <omm.hpp>
#include <nvrhi/nvrhi.h>
#include <vector>
#include <stdint.h>
Expand Down
32 changes: 31 additions & 1 deletion omm-sdk/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.12)

option(OMM_DISABLE_INTERPROCEDURAL_OPTIMIZATION "disable interprocedural optimization" OFF)
option(OMM_ENABLE_OPENMP "enable openmp" ON)
Expand All @@ -9,6 +9,7 @@ option(OMM_CROSSCOMPILE_AARCH64 "cross compilation for aarch64" OFF)
option(OMM_CROSSCOMPILE_X86_64 "cross compilation for x86_64" OFF)
option(OMM_SHADER_DEBUG_INFO "enable embedded shader debug info" OFF)
option(OMM_INSTALL "Generate install rules for OMM" ON)
option(OMM_ENABLE_INTERFACE_GEN "Enable interface generation script. Only required for SDK development." OFF)

if (OMM_ENABLE_OPENMP)
find_package(OpenMP)
Expand Down Expand Up @@ -114,6 +115,35 @@ if (OMM_ENABLE_PRECOMPILED_SHADERS)
add_dependencies(${TARGET_NAME} Shaders)
endif()

if (OMM_ENABLE_INTERFACE_GEN)
find_package(Python COMPONENTS Interpreter)

if (Python_Interpreter_FOUND)

add_custom_target(
GenInterfaceH ALL
COMMAND ${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/scripts/_interface_gen_c.py ${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/scripts/omm.json
COMMENT "Generating Interface H"
)

add_custom_target(
GenInterfaceHPP ALL
COMMAND ${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/scripts/_interface_gen_cpp.py ${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/scripts/omm.json
COMMENT "Generating Interface HPP"
)

set_property(TARGET GenInterfaceH PROPERTY FOLDER "${OMM_PROJECT_FOLDER}/CustomScripts")
set_property(TARGET GenInterfaceHPP PROPERTY FOLDER "${OMM_PROJECT_FOLDER}/CustomScripts")

add_dependencies(${TARGET_NAME} GenInterfaceH GenInterfaceHPP)

else()
message(WARNING "Python not found. Interface generator will be disabled.")
endif()
endif()

if (OMM_ENABLE_PRECOMPILED_SHADERS)
target_include_directories(${TARGET_NAME} PRIVATE "${OMM_SHADER_OUTPUT_PATH}")
endif()
Expand Down
Loading

0 comments on commit 9c6adf7

Please sign in to comment.