Skip to content

Commit

Permalink
nvapi support (#76)
Browse files Browse the repository at this point in the history
* add nvapi support

* rename SLANG_RHI_HAS_OPTIX -> SLANG_RHI_ENABLE_OPTIX
  • Loading branch information
skallweitNV authored Oct 11, 2024
1 parent 4f5ef3c commit b58c00d
Show file tree
Hide file tree
Showing 18 changed files with 62 additions and 50 deletions.
50 changes: 31 additions & 19 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,6 @@ if(SLANG_RHI_MASTER_PROJECT)
FetchPackage(slang URL ${SLANG_URL})
set(SLANG_RHI_SLANG_INCLUDE_DIR ${slang_SOURCE_DIR}/include)
set(SLANG_RHI_SLANG_BINARY_DIR ${slang_SOURCE_DIR})

if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
# Fetch nvapi
# set(NVAPI_VERSION "4ba3384657149d63aa193f5a34e20efe1e42bf31")
# FetchPackage(nvapi URL "https://github.com/NVIDIA/nvapi/archive/${NVAPI_VERSION}.zip")
# set(NVAPI_ROOT_DIR ${nvapi_SOURCE_DIR})

# Fetch dxc
# set(DXC_VERSION "1.8.2407")
# set(DXC_TAG "2024_07_31")
# FetchPackage(dxc URL "https://github.com/microsoft/DirectXShaderCompiler/releases/download/v${DXC_VERSION}/dxc_${DXC_TAG}.zip")

# Fetch Agility SDK
# set(AGILITY_SDK_VERSION "1.611.2")
# FetchPackage(agility_sdk URL "https://www.nuget.org/api/v2/package/Microsoft.Direct3D.D3D12/${AGILITY_SDK_VERSION}")
endif()
endif()

set(SLANG_RHI_SLANG_INCLUDE_DIR ${SLANG_RHI_SLANG_INCLUDE_DIR} CACHE STRING "Slang include directory")
Expand All @@ -119,6 +103,31 @@ set(SLANG_RHI_SLANG_BINARY_DIR ${SLANG_RHI_SLANG_BINARY_DIR} CACHE STRING "Slang
unset(SLANG_RHI_SLANG_INCLUDE_DIR)
unset(SLANG_RHI_SLANG_BINARY_DIR)

# Fetch NVAPI & Agility SDK
if((SLANG_RHI_ENABLE_D3D11 OR SLANG_RHI_ENABLE_D3D12) AND (CMAKE_SYSTEM_NAME STREQUAL "Windows") AND (CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64"))
# NVAPI
set(NVAPI_VERSION "4ba3384657149d63aa193f5a34e20efe1e42bf31")
FetchPackage(nvapi URL "https://github.com/NVIDIA/nvapi/archive/${NVAPI_VERSION}.zip")
set(NVAPI_ROOT_DIR ${nvapi_SOURCE_DIR})

add_library(slang-rhi-nvapi INTERFACE)
target_include_directories(slang-rhi-nvapi INTERFACE ${NVAPI_ROOT_DIR})
target_link_libraries(slang-rhi-nvapi INTERFACE ${NVAPI_ROOT_DIR}/amd64/nvapi64.lib)
target_link_libraries(slang-rhi PUBLIC slang-rhi-nvapi)
set(SLANG_RHI_ENABLE_NVAPI ON)

# Fetch dxc
# set(DXC_VERSION "1.8.2407")
# set(DXC_TAG "2024_07_31")
# FetchPackage(dxc URL "https://github.com/microsoft/DirectXShaderCompiler/releases/download/v${DXC_VERSION}/dxc_${DXC_TAG}.zip")

# Agility SDK
set(AGILITY_SDK_VERSION "1.614.1")
FetchPackage(agility_sdk URL "https://www.nuget.org/api/v2/package/Microsoft.Direct3D.D3D12/${AGILITY_SDK_VERSION}")
else()
set(SLANG_RHI_ENABLE_NVAPI OFF)
endif()

# Fetch and setup Google Dawn library (WebGPU implementation)
if(SLANG_RHI_ENABLE_WGPU)
set(DAWN_VERSION "131.0.6738.0")
Expand Down Expand Up @@ -186,13 +195,14 @@ if(SLANG_RHI_ENABLE_CUDA)
# Find Optix
set(Optix_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/external/optix CACHE STRING "Optix root directory")
find_package(OptiX)
set(SLANG_RHI_HAS_OPTIX ${OptiX_FOUND})
if(${SLANG_RHI_HAS_OPTIX})
set(SLANG_RHI_ENABLE_OPTIX ${OptiX_FOUND})
if(${SLANG_RHI_ENABLE_OPTIX})
add_library(slang-rhi-optix INTERFACE)
target_include_directories(slang-rhi-optix INTERFACE ${OptiX_INCLUDE_DIRS})
target_link_libraries(slang-rhi PUBLIC slang-rhi-optix)
endif()
target_compile_definitions(slang-rhi PRIVATE SLANG_RHI_HAS_OPTIX=$<BOOL:${SLANG_RHI_HAS_OPTIX}>)
else()
set(SLANG_RHI_ENABLE_OPTIX OFF)
endif()

# Fetch glfw
Expand Down Expand Up @@ -466,6 +476,8 @@ target_compile_definitions(slang-rhi
SLANG_RHI_ENABLE_METAL=$<BOOL:${SLANG_RHI_ENABLE_METAL}>
SLANG_RHI_ENABLE_CUDA=$<BOOL:${SLANG_RHI_ENABLE_CUDA}>
SLANG_RHI_ENABLE_WGPU=$<BOOL:${SLANG_RHI_ENABLE_WGPU}>
SLANG_RHI_ENABLE_NVAPI=$<BOOL:${SLANG_RHI_ENABLE_NVAPI}>
SLANG_RHI_ENABLE_OPTIX=$<BOOL:${SLANG_RHI_ENABLE_OPTIX}>
$<$<PLATFORM_ID:Windows>:NOMINMAX> # do not define min/max macros
$<$<PLATFORM_ID:Windows>:UNICODE> # force character map to unicode
)
Expand Down
4 changes: 2 additions & 2 deletions src/cuda/cuda-acceleration-structure.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "cuda-acceleration-structure.h"
#include "cuda-device.h"

#if SLANG_RHI_HAS_OPTIX
#if SLANG_RHI_ENABLE_OPTIX

namespace rhi::cuda {

Expand Down Expand Up @@ -213,4 +213,4 @@ OptixVertexFormat AccelerationStructureBuildInputBuilder::translateVertexFormat(

} // namespace rhi::cuda

#endif // SLANG_RHI_HAS_OPTIX
#endif // SLANG_RHI_ENABLE_OPTIX
4 changes: 2 additions & 2 deletions src/cuda/cuda-acceleration-structure.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "core/stable_vector.h"

#if SLANG_RHI_HAS_OPTIX
#if SLANG_RHI_ENABLE_OPTIX

namespace rhi::cuda {

Expand Down Expand Up @@ -49,4 +49,4 @@ struct AccelerationStructureBuildInputBuilder

} // namespace rhi::cuda

#endif // SLANG_RHI_HAS_OPTIX
#endif // SLANG_RHI_ENABLE_OPTIX
2 changes: 1 addition & 1 deletion src/cuda/cuda-api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,6 @@ void rhiCudaApiShutdown() {}

#endif // SLANG_RHI_USE_DYNAMIC_CUDA

#if SLANG_RHI_HAS_OPTIX
#if SLANG_RHI_ENABLE_OPTIX
#include <optix_function_table_definition.h>
#endif
2 changes: 1 addition & 1 deletion src/cuda/cuda-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ enum CUmemAttach_flags

#endif // SLANG_RHI_USE_DYNAMIC_CUDA

#if SLANG_RHI_HAS_OPTIX
#if SLANG_RHI_ENABLE_OPTIX
#define OPTIX_DONT_INCLUDE_CUDA
#include <optix.h>
#include <optix_stubs.h>
Expand Down
2 changes: 1 addition & 1 deletion src/cuda/cuda-command-buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Result CommandBufferImpl::beginComputePass(IComputePassEncoder** outEncoder)

Result CommandBufferImpl::beginRayTracingPass(IRayTracingPassEncoder** outEncoder)
{
#if SLANG_RHI_HAS_OPTIX
#if SLANG_RHI_ENABLE_OPTIX
m_rayTracingPassEncoder.init(this);
*outEncoder = &m_rayTracingPassEncoder;
return SLANG_OK;
Expand Down
2 changes: 1 addition & 1 deletion src/cuda/cuda-command-buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class CommandBufferImpl : public ICommandBuffer, public CommandWriter, public Co
TransientResourceHeap* m_transientHeap;
ResourcePassEncoderImpl m_resourcePassEncoder;
ComputePassEncoderImpl m_computePassEncoder;
#if SLANG_RHI_HAS_OPTIX
#if SLANG_RHI_ENABLE_OPTIX
RayTracingPassEncoderImpl m_rayTracingPassEncoder;
#endif

Expand Down
4 changes: 2 additions & 2 deletions src/cuda/cuda-command-encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ Result ComputePassEncoderImpl::dispatchComputeIndirect(IBuffer* argBuffer, Offse

// RayTracingPassEncoderImpl

#if SLANG_RHI_HAS_OPTIX
#if SLANG_RHI_ENABLE_OPTIX

void RayTracingPassEncoderImpl::init(CommandBufferImpl* cmdBuffer)
{
Expand Down Expand Up @@ -329,6 +329,6 @@ Result RayTracingPassEncoderImpl::dispatchRays(
return SLANG_E_NOT_IMPLEMENTED;
}

#endif // SLANG_RHI_HAS_OPTIX
#endif // SLANG_RHI_ENABLE_OPTIX

} // namespace rhi::cuda
4 changes: 2 additions & 2 deletions src/cuda/cuda-command-encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class ComputePassEncoderImpl : public IComputePassEncoder, public PassEncoderImp
virtual SLANG_NO_THROW Result SLANG_MCALL dispatchComputeIndirect(IBuffer* argBuffer, Offset offset) override;
};

#if SLANG_RHI_HAS_OPTIX
#if SLANG_RHI_ENABLE_OPTIX

class RayTracingPassEncoderImpl : public IRayTracingPassEncoder, public PassEncoderImpl
{
Expand Down Expand Up @@ -203,6 +203,6 @@ class RayTracingPassEncoderImpl : public IRayTracingPassEncoder, public PassEnco
override;
};

#endif // SLANG_RHI_HAS_OPTIX
#endif // SLANG_RHI_ENABLE_OPTIX

} // namespace rhi::cuda
10 changes: 5 additions & 5 deletions src/cuda/cuda-device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ DeviceImpl::~DeviceImpl()
{
m_queue.setNull();

#if SLANG_RHI_HAS_OPTIX
#if SLANG_RHI_ENABLE_OPTIX
if (m_ctx.optixContext)
{
optixDeviceContextDestroy(m_ctx.optixContext);
Expand All @@ -149,7 +149,7 @@ Result DeviceImpl::getNativeDeviceHandles(DeviceNativeHandles* outHandles)
{
outHandles->handles[0].type = NativeHandleType::CUdevice;
outHandles->handles[0].value = m_ctx.device;
#if SLANG_RHI_HAS_OPTIX
#if SLANG_RHI_ENABLE_OPTIX
outHandles->handles[1].type = NativeHandleType::OptixDeviceContext;
outHandles->handles[1].value = (uint64_t)m_ctx.optixContext;
#else
Expand Down Expand Up @@ -208,7 +208,7 @@ Result DeviceImpl::initialize(const DeviceDesc& desc)
m_features.push_back("has-ptr");
}

#if SLANG_RHI_HAS_OPTIX
#if SLANG_RHI_ENABLE_OPTIX
{
SLANG_OPTIX_RETURN_ON_FAIL(optixInit());

Expand Down Expand Up @@ -1168,7 +1168,7 @@ Result DeviceImpl::getAccelerationStructureSizes(
AccelerationStructureSizes* outSizes
)
{
#if SLANG_RHI_HAS_OPTIX
#if SLANG_RHI_ENABLE_OPTIX
AccelerationStructureBuildInputBuilder builder;
builder.build(desc, m_debugCallback);
OptixAccelBufferSizes sizes;
Expand All @@ -1194,7 +1194,7 @@ Result DeviceImpl::createAccelerationStructure(
IAccelerationStructure** outAccelerationStructure
)
{
#if SLANG_RHI_HAS_OPTIX
#if SLANG_RHI_ENABLE_OPTIX
RefPtr<AccelerationStructureImpl> result = new AccelerationStructureImpl(this, desc);
SLANG_CUDA_RETURN_ON_FAIL(cuMemAlloc(&result->m_buffer, desc.size));
SLANG_CUDA_RETURN_ON_FAIL(cuMemAlloc(&result->m_propertyBuffer, 8));
Expand Down
2 changes: 1 addition & 1 deletion src/cuda/cuda-device.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct Context
{
CUdevice device = -1;
CUcontext context = nullptr;
#if SLANG_RHI_HAS_OPTIX
#if SLANG_RHI_ENABLE_OPTIX
OptixDeviceContext optixContext = nullptr;
#endif
};
Expand Down
4 changes: 2 additions & 2 deletions src/cuda/cuda-helper-functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Result _handleCUDAError(CUresult cuResult, const char* file, int line)
return info.handle();
}

#if SLANG_RHI_HAS_OPTIX
#if SLANG_RHI_ENABLE_OPTIX

bool _isError(OptixResult result)
{
Expand All @@ -36,7 +36,7 @@ void _optixLogCallback(unsigned int level, const char* tag, const char* message,
fprintf(stderr, "optix: %s (%s)\n", message, tag);
}
#endif
#endif // SLANG_RHI_HAS_OPTIX
#endif // SLANG_RHI_ENABLE_OPTIX

AdapterLUID getAdapterLUID(int deviceIndex)
{
Expand Down
4 changes: 2 additions & 2 deletions src/cuda/cuda-helper-functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Result _handleCUDAError(CUresult cuResult, const char* file, int line);
}; \
}

#if SLANG_RHI_HAS_OPTIX
#if SLANG_RHI_ENABLE_OPTIX

bool _isError(OptixResult result);

Expand All @@ -87,7 +87,7 @@ Result _handleOptixError(OptixResult result, char const* file, int line);

void _optixLogCallback(unsigned int level, const char* tag, const char* message, void* userData);

#endif // SLANG_RHI_HAS_OPTIX
#endif // SLANG_RHI_ENABLE_OPTIX

AdapterLUID getAdapterLUID(int deviceIndex);

Expand Down
6 changes: 3 additions & 3 deletions src/d3d11/d3d11-device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,16 @@ Result DeviceImpl::initialize(const DeviceDesc& desc)

if (isSupportedNVAPIOp(m_device, NV_EXTN_OP_UINT64_ATOMIC))
{
m_features.add("atomic-int64");
m_features.push_back("atomic-int64");
}
if (isSupportedNVAPIOp(m_device, NV_EXTN_OP_FP32_ATOMIC))
{
m_features.add("atomic-float");
m_features.push_back("atomic-float");
}

// If we have NVAPI well assume we have realtime clock
{
m_features.add("realtime-clock");
m_features.push_back("realtime-clock");
}

m_nvapi = true;
Expand Down
4 changes: 2 additions & 2 deletions src/d3d12/d3d12-helper-functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "d3d12-query.h"
#include "d3d12-transient-heap.h"

#ifdef SLANG_RHI_NVAPI
#ifdef SLANG_RHI_ENABLE_NVAPI
#include "../nvapi/nvapi-include.h"
#endif

Expand All @@ -21,7 +21,7 @@ namespace rhi::d3d12 {

bool isSupportedNVAPIOp(ID3D12Device* dev, uint32_t op)
{
#ifdef SLANG_RHI_NVAPI
#ifdef SLANG_RHI_ENABLE_NVAPI
{
bool isSupported;
NvAPI_Status status = NvAPI_D3D12_IsNvShaderExtnOpCodeSupported(dev, NvU32(op), &isSupported);
Expand Down
4 changes: 2 additions & 2 deletions src/d3d12/d3d12-pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "d3d12-shader-program.h"
#include "d3d12-input-layout.h"

#ifdef SLANG_RHI_NVAPI
#ifdef SLANG_RHI_ENABLE_NVAPI
#include "../nvapi/nvapi-include.h"
#endif

Expand Down Expand Up @@ -255,7 +255,7 @@ Result DeviceImpl::createComputePipeline2(const ComputePipelineDesc2& desc, ICom
: program->m_rootObjectLayout->m_rootSignature;
computeDesc.CS = {program->m_shaders[0].code.data(), SIZE_T(program->m_shaders[0].code.size())};

#ifdef SLANG_RHI_NVAPI
#ifdef SLANG_RHI_ENABLE_NVAPI
if (m_nvapi)
{
// Also fill the extension structure.
Expand Down
2 changes: 1 addition & 1 deletion src/nvapi/nvapi-include.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// A helper that makes the NVAPI available across targets

#ifdef SLANG_RHI_NVAPI
#ifdef SLANG_RHI_ENABLE_NVAPI
// On windows if we include NVAPI, we must include windows.h first

#ifdef _WIN32
Expand Down
2 changes: 1 addition & 1 deletion src/nvapi/nvapi-util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ static Result g_initStatus = SLANG_E_UNINITIALIZED;

Result NVAPIUtil::initialize()
{
#ifdef SLANG_RHI_NVAPI
#ifdef SLANG_RHI_ENABLE_NVAPI
if (g_initStatus == SLANG_E_UNINITIALIZED)
{
NvAPI_Status ret = NVAPI_OK;
Expand Down

0 comments on commit b58c00d

Please sign in to comment.