Skip to content

Commit

Permalink
Added ability to disable extension libraries (NVAPI and AMD AGS)
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhdanNV committed May 27, 2024
1 parent ec705f3 commit 749f869
Show file tree
Hide file tree
Showing 17 changed files with 274 additions and 101 deletions.
59 changes: 41 additions & 18 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ set (NRI_AGILITY_SDK_PATH "C:/AgilitySDK" CACHE STRING "Path to a directory cont
set (NRI_AGILITY_SDK_VERSION "614" CACHE STRING "Agility SDK version")
set (NRI_AGILITY_SDK_DIR "AgilitySDK" CACHE STRING "Directory where Agility SDK binaries will be copied to relative to 'CMAKE_RUNTIME_OUTPUT_DIRECTORY'")

# Options: D3D11/D3D12 specific
option (NRI_ENABLE_EXTERNAL_LIBRARIES "Enable vendor specific extension libraries (NVAPI and AMD AGS)" ON)

# Is submodule?
if (${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
set (IS_SUBMODULE OFF)
Expand Down Expand Up @@ -117,50 +120,70 @@ else ()
endif ()

set (COMPILE_DEFINITIONS WIN32_LEAN_AND_MEAN NOMINMAX _CRT_SECURE_NO_WARNINGS)
if (NRI_ENABLE_EXTERNAL_LIBRARIES)
set (COMPILE_DEFINITIONS ${COMPILE_DEFINITIONS} NRI_USE_EXT_LIBS=1)
endif ()

# External libs
if (WIN32 AND NRI_ENABLE_EXTERNAL_LIBRARIES AND (NRI_ENABLE_D3D11_SUPPORT OR NRI_ENABLE_D3D12_SUPPORT))
# Statically linked
find_library (NVAPI_LIB NAMES nvapi64 nvapi PATHS "External/nvapi/${NVAPI_BIN_ARCHITECTURE}" REQUIRED)
# Dynamically loaded
find_library (AGS_LIB NAMES amd_ags_${BIN_ARCHITECTURE} PATHS "External/amdags/ags_lib/lib" REQUIRED)

file (GLOB NVAPI_HEADERS "External/nvapi/*.h")
source_group ("External/nvapi" FILES ${NVAPI_HEADERS})
file (GLOB AMDAGS_HEADERS "External/amdags/ags_lib/inc/*.h")
source_group ("External/amdags" FILES ${AMDAGS_HEADERS})
endif ()

# D3D11
if (WIN32 AND NRI_ENABLE_D3D11_SUPPORT)
message ("NRI adding backend: D3D11")

set (COMPILE_DEFINITIONS ${COMPILE_DEFINITIONS} NRI_USE_D3D11=1)
set (INPUT_LIBS_D3D11 ${INPUT_LIB_D3D11} ${INPUT_LIB_DXGI})

find_library (INPUT_LIB_NVAPI NAMES nvapi64 nvapi PATHS "External/nvapi/${NVAPI_BIN_ARCHITECTURE}" REQUIRED)
find_library (INPUT_LIB_AGS NAMES amd_ags_${BIN_ARCHITECTURE} PATHS "External/amdags/ags_lib/lib" REQUIRED)

file (GLOB D3D11_SOURCE "Source/D3D11/*.cpp" "Source/D3D11/*.h" "Source/D3D11/*.hpp")
source_group ("" FILES ${D3D11_SOURCE})
file (GLOB D3D11_NVAPI "External/nvapi/*.h")
source_group ("External/nvapi" FILES ${D3D11_NVAPI})
file (GLOB D3D11_AMDAGS "External/amdags/ags_lib/inc/*.h")
source_group ("External/amdags" FILES ${D3D11_AMDAGS})
add_library (NRI_D3D11 STATIC ${D3D11_SOURCE} ${D3D11_NVAPI} ${D3D11_AMDAGS})

if (NRI_ENABLE_EXTERNAL_LIBRARIES)
add_library (NRI_D3D11 STATIC ${D3D11_SOURCE} ${NVAPI_HEADERS} ${AMDAGS_HEADERS})
target_link_libraries (NRI_D3D11 PRIVATE ${NVAPI_LIB})
else ()
add_library (NRI_D3D11 STATIC ${D3D11_SOURCE})
endif ()

target_include_directories (NRI_D3D11 PRIVATE "Include" "Source/Shared" "External")
target_compile_definitions (NRI_D3D11 PRIVATE ${COMPILE_DEFINITIONS})
target_compile_options (NRI_D3D11 PRIVATE ${COMPILE_OPTIONS})
target_link_libraries (NRI_D3D11 PRIVATE NRI_Shared ${INPUT_LIBS_D3D11} ${INPUT_LIB_NVAPI} ${INPUT_LIB_DXGUID})
target_link_libraries (NRI_D3D11 PRIVATE NRI_Shared ${INPUT_LIBS_D3D11} ${INPUT_LIB_DXGUID})

set_property (TARGET NRI_D3D11 PROPERTY FOLDER ${PROJECT_NAME})
endif ()

# D3D12
if (WIN32 AND NRI_ENABLE_D3D12_SUPPORT)
message ("NRI adding backend: D3D12")

set (COMPILE_DEFINITIONS ${COMPILE_DEFINITIONS} NRI_USE_D3D12=1)
set (INPUT_LIBS_D3D12 ${INPUT_LIB_D3D12} ${INPUT_LIB_DXGI})

find_library (INPUT_LIB_NVAPI NAMES nvapi64 nvapi PATHS "External/nvapi/${NVAPI_BIN_ARCHITECTURE}" REQUIRED)
find_library (INPUT_LIB_AGS NAMES amd_ags_${BIN_ARCHITECTURE} PATHS "External/amdags/ags_lib/lib" REQUIRED)

file (GLOB D3D12_SOURCE "Source/D3D12/*.cpp" "Source/D3D12/*.h" "Source/D3D12/*.hpp")
source_group ("" FILES ${D3D12_SOURCE})
file (GLOB D3D12_NVAPI "External/nvapi/*.h")
source_group ("External/nvapi" FILES ${D3D12_NVAPI})
file (GLOB D3D12_AMDAGS "External/amdags/ags_lib/inc/*.h")
source_group ("External/amdags" FILES ${D3D12_AMDAGS})
add_library (NRI_D3D12 STATIC ${D3D12_SOURCE} ${D3D12_NVAPI} ${D3D12_AMDAGS})

if (NRI_ENABLE_EXTERNAL_LIBRARIES)
add_library (NRI_D3D12 STATIC ${D3D12_SOURCE} ${NVAPI_HEADERS} ${AMDAGS_HEADERS})
target_link_libraries (NRI_D3D12 PRIVATE ${NVAPI_LIB})
else ()
add_library (NRI_D3D12 STATIC ${D3D12_SOURCE})
endif ()

target_include_directories (NRI_D3D12 PRIVATE "Include" "Source/Shared" "External")
target_compile_definitions (NRI_D3D12 PRIVATE ${COMPILE_DEFINITIONS})
target_compile_options (NRI_D3D12 PRIVATE ${COMPILE_OPTIONS})
target_link_libraries (NRI_D3D12 PRIVATE NRI_Shared ${INPUT_LIBS_D3D12} ${INPUT_LIB_NVAPI})
target_link_libraries (NRI_D3D12 PRIVATE NRI_Shared ${INPUT_LIBS_D3D12})

set_property (TARGET NRI_D3D12 PROPERTY FOLDER ${PROJECT_NAME})

if (NRI_ENABLE_AGILITY_SDK_SUPPORT)
Expand Down
4 changes: 2 additions & 2 deletions Include/NRI.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ Non-goals:
#include <stddef.h>

#define NRI_VERSION_MAJOR 1
#define NRI_VERSION_MINOR 134
#define NRI_VERSION_DATE "21 May 2024"
#define NRI_VERSION_MINOR 135
#define NRI_VERSION_DATE "27 May 2024"

#ifdef _WIN32
#define NRI_CALL __fastcall
Expand Down
2 changes: 1 addition & 1 deletion Resources/Version.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#define STR(x) STR_HELPER(x)

#define VERSION_MAJOR 1
#define VERSION_MINOR 134
#define VERSION_MINOR 135
#define VERSION_BUILD 0
#define VERSION_REVISION 0

Expand Down
16 changes: 8 additions & 8 deletions Source/Creation/Creation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,17 @@ NRI_API Result NRI_CALL nriCreateDevice(const DeviceCreationDesc& deviceCreation
CheckAndSetDefaultCallbacks(modifiedDeviceCreationDesc.callbackInterface);
CheckAndSetDefaultAllocator(modifiedDeviceCreationDesc.memoryAllocatorInterface);

#if (NRI_USE_D3D11 == 1)
#if NRI_USE_D3D11
if (modifiedDeviceCreationDesc.graphicsAPI == GraphicsAPI::D3D11)
result = CreateDeviceD3D11(modifiedDeviceCreationDesc, deviceImpl);
#endif

#if (NRI_USE_D3D12 == 1)
#if NRI_USE_D3D12
if (modifiedDeviceCreationDesc.graphicsAPI == GraphicsAPI::D3D12)
result = CreateDeviceD3D12(modifiedDeviceCreationDesc, deviceImpl);
#endif

#if (NRI_USE_VULKAN == 1)
#if NRI_USE_VULKAN
if (modifiedDeviceCreationDesc.graphicsAPI == GraphicsAPI::VULKAN)
result = CreateDeviceVK(modifiedDeviceCreationDesc, deviceImpl);
#endif
Expand Down Expand Up @@ -146,7 +146,7 @@ NRI_API Result NRI_CALL nriCreateDeviceFromD3D11Device(const DeviceCreationD3D11
Result result = Result::UNSUPPORTED;
DeviceBase* deviceImpl = nullptr;

#if (NRI_USE_D3D11 == 1)
#if NRI_USE_D3D11
result = CreateDeviceD3D11(tempDeviceCreationD3D11Desc, deviceImpl);
#endif

Expand Down Expand Up @@ -174,7 +174,7 @@ NRI_API Result NRI_CALL nriCreateDeviceFromD3D12Device(const DeviceCreationD3D12
Result result = Result::UNSUPPORTED;
DeviceBase* deviceImpl = nullptr;

#if (NRI_USE_D3D12 == 1)
#if NRI_USE_D3D12
result = CreateDeviceD3D12(tempDeviceCreationD3D12Desc, deviceImpl);
#endif

Expand Down Expand Up @@ -203,7 +203,7 @@ NRI_API Result NRI_CALL nriCreateDeviceFromVkDevice(const DeviceCreationVKDesc&
Result result = Result::UNSUPPORTED;
DeviceBase* deviceImpl = nullptr;

#if (NRI_USE_VULKAN == 1)
#if NRI_USE_VULKAN
result = CreateDeviceVK(tempDeviceCreationVKDesc, deviceImpl);
#endif

Expand All @@ -228,7 +228,7 @@ NRI_API Format NRI_CALL nriConvertDXGIFormatToNRI(uint32_t dxgiFormat) {
NRI_API uint32_t NRI_CALL nriConvertNRIFormatToVK(Format format) {
MaybeUnused(format);

#if (NRI_USE_VULKAN == 1)
#if NRI_USE_VULKAN
return NRIFormatToVKFormat(format);
#else
return 0;
Expand All @@ -238,7 +238,7 @@ NRI_API uint32_t NRI_CALL nriConvertNRIFormatToVK(Format format) {
NRI_API uint32_t NRI_CALL nriConvertNRIFormatToDXGI(Format format) {
MaybeUnused(format);

#if (NRI_USE_D3D11 == 1 || NRI_USE_D3D12 == 1)
#if (NRI_USE_D3D11 || NRI_USE_D3D12)
return NRIFormatToDXGIFormat(format);
#else
return 0;
Expand Down
5 changes: 4 additions & 1 deletion Source/D3D11/CommandBufferD3D11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ void CommandBufferD3D11::SetStencilReference(uint8_t frontRef, uint8_t backRef)
}

void CommandBufferD3D11::SetSamplePositions(const SamplePosition* positions, Sample_t positionNum, Sample_t sampleNum) {
MaybeUnused(sampleNum); // already have this in "m_RasterizerStateExDesc"
MaybeUnused(sampleNum); // already have this in "m_RasterizerDesc"

m_SamplePositionsState.Set(positions, positionNum);

Expand Down Expand Up @@ -446,6 +446,8 @@ void CommandBufferD3D11::DispatchIndirect(const Buffer& buffer, uint64_t offset)
}

void CommandBufferD3D11::Barrier(const BarrierGroupDesc& barrierGroupDesc) {
MaybeUnused(barrierGroupDesc);
#if NRI_USE_EXT_LIBS
constexpr AccessBits STORAGE_MASK = AccessBits::SHADER_RESOURCE_STORAGE;

if (barrierGroupDesc.textureNum == 0 && barrierGroupDesc.bufferNum == 0)
Expand Down Expand Up @@ -494,6 +496,7 @@ void CommandBufferD3D11::Barrier(const BarrierGroupDesc& barrierGroupDesc) {

if (flags)
m_Device.GetExt()->WaitForDrain(m_DeferredContext, flags);
#endif
}

void CommandBufferD3D11::BeginQuery(const QueryPool& queryPool, uint32_t offset) {
Expand Down
57 changes: 30 additions & 27 deletions Source/D3D11/DeviceD3D11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ DeviceD3D11::~DeviceD3D11() {

DeleteCriticalSection(&m_CriticalSection);

#if NRI_USE_EXT_LIBS
if (m_Ext.HasAGS() && !m_IsWrapped)
m_Ext.m_AGS.DestroyDeviceD3D11(m_Ext.m_AGSContext, m_Device, nullptr, m_ImmediateContext, nullptr);
#endif
}

Result DeviceD3D11::Create(const DeviceCreationDesc& deviceCreationDesc, ID3D11Device* device, AGSContext* agsContext, bool isNVAPILoadedInApp) {
Expand Down Expand Up @@ -141,21 +143,17 @@ Result DeviceD3D11::Create(const DeviceCreationDesc& deviceCreationDesc, ID3D11D
m_Ext.InitializeAMDExt(this, agsContext, device != nullptr);

// Device
AGSDX11ReturnedParams agsParams = {};
if (m_Desc.adapterDesc.vendor == Vendor::NVIDIA) { // Tricky part: "params" is used to properly propagate exts support
agsParams.extensionsSupported.depthBoundsTest = true;
agsParams.extensionsSupported.depthBoundsDeferredContexts = true;
agsParams.extensionsSupported.uavOverlap = true;
agsParams.extensionsSupported.UAVOverlapDeferredContexts = true;
}

ComPtr<ID3D11DeviceBest> deviceTemp = (ID3D11DeviceBest*)device;
if (!deviceTemp) {
uint32_t shaderExtRegister = deviceCreationDesc.shaderExtRegister ? deviceCreationDesc.shaderExtRegister : 63;
const UINT flags = deviceCreationDesc.enableAPIValidation ? D3D11_CREATE_DEVICE_DEBUG : 0;
D3D_FEATURE_LEVEL levels[2] = {D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0};
const uint32_t levelNum = GetCountOf(levels);
bool isDepthBoundsTestSupported = false;
bool isDrawIndirectCountSupported = false;
bool isShaderAtomicsI64Supported = false;

#if NRI_USE_EXT_LIBS
uint32_t shaderExtRegister = deviceCreationDesc.shaderExtRegister ? deviceCreationDesc.shaderExtRegister : 63;
if (m_Ext.HasAGS()) {
AGSDX11DeviceCreationParams deviceCreationParams = {};
deviceCreationParams.pAdapter = m_Adapter;
Expand All @@ -168,6 +166,7 @@ Result DeviceD3D11::Create(const DeviceCreationDesc& deviceCreationDesc, ID3D11D
AGSDX11ExtensionParams extensionsParams = {};
extensionsParams.uavSlot = shaderExtRegister;

AGSDX11ReturnedParams agsParams = {};
AGSReturnCode result = m_Ext.m_AGS.CreateDeviceD3D11(m_Ext.m_AGSContext, &deviceCreationParams, &extensionsParams, &agsParams);
if (flags != 0 && result != AGS_SUCCESS) {
// If Debug Layer is not available, try without D3D11_CREATE_DEVICE_DEBUG
Expand All @@ -178,7 +177,11 @@ Result DeviceD3D11::Create(const DeviceCreationDesc& deviceCreationDesc, ID3D11D
RETURN_ON_FAILURE(this, result == AGS_SUCCESS, Result::FAILURE, "agsDriverExtensionsDX11_CreateDevice() failed: %d", (int32_t)result);

deviceTemp = (ID3D11DeviceBest*)agsParams.pDevice;
isDepthBoundsTestSupported = agsParams.extensionsSupported.depthBoundsDeferredContexts;
isDrawIndirectCountSupported = agsParams.extensionsSupported.multiDrawIndirectCountIndirect;
isShaderAtomicsI64Supported = agsParams.extensionsSupported.intrinsics19;
} else {
#endif
hr = D3D11CreateDevice(m_Adapter, D3D_DRIVER_TYPE_UNKNOWN, nullptr, flags, levels, levelNum, D3D11_SDK_VERSION, (ID3D11Device**)&deviceTemp, nullptr, nullptr);
if (flags && (uint32_t)hr == 0x887a002d) {
// If Debug Layer is not available, try without D3D11_CREATE_DEVICE_DEBUG
Expand All @@ -187,12 +190,20 @@ Result DeviceD3D11::Create(const DeviceCreationDesc& deviceCreationDesc, ID3D11D

RETURN_ON_BAD_HRESULT(this, hr, "D3D11CreateDevice()");

// Register device
#if NRI_USE_EXT_LIBS
if (m_Ext.HasNVAPI()) {
NvAPI_D3D_RegisterDevice(m_Device);
NvAPI_D3D11_SetNvShaderExtnSlot(m_Device, shaderExtRegister);
NvAPI_D3D11_IsNvShaderExtnOpCodeSupported(m_Device, NV_EXTN_OP_UINT64_ATOMIC, &isShaderAtomicsI64Supported);
isDepthBoundsTestSupported = true;
}
}
#endif

// Start filling here to avoid passing additional arguments into "FillDesc"
m_Desc.isDepthBoundsTestSupported = isDepthBoundsTestSupported;
m_Desc.isDrawIndirectCountSupported = isDrawIndirectCountSupported;
m_Desc.isShaderAtomicsI64Supported = isShaderAtomicsI64Supported;
}
else
m_IsWrapped = true;
Expand Down Expand Up @@ -220,9 +231,6 @@ Result DeviceD3D11::Create(const DeviceCreationDesc& deviceCreationDesc, ID3D11D
if (!threadingCaps.DriverCommandLists) {
REPORT_WARNING(this, "Deferred Contexts are not supported by the driver and will be emulated!");
m_IsDeferredContextEmulated = true;

agsParams.extensionsSupported.UAVOverlapDeferredContexts = agsParams.extensionsSupported.uavOverlap;
agsParams.extensionsSupported.depthBoundsDeferredContexts = agsParams.extensionsSupported.depthBoundsTest;
}

hr = m_ImmediateContext->QueryInterface(IID_PPV_ARGS(&m_Multithread));
Expand All @@ -233,15 +241,15 @@ Result DeviceD3D11::Create(const DeviceCreationDesc& deviceCreationDesc, ID3D11D
m_Multithread->SetMultithreadProtected(true);

// Other
FillDesc(agsParams);
FillDesc();

for (uint32_t i = 0; i < (uint32_t)CommandQueueType::MAX_NUM; i++)
m_CommandQueues.emplace_back(*this);

return FillFunctionTable(m_CoreInterface);
}

void DeviceD3D11::FillDesc(const AGSDX11ReturnedParams& agsParams) {
void DeviceD3D11::FillDesc() {
D3D11_FEATURE_DATA_D3D11_OPTIONS options = {};
HRESULT hr = m_Device->CheckFeatureSupport(D3D11_FEATURE_D3D11_OPTIONS, &options, sizeof(options));
if (FAILED(hr))
Expand Down Expand Up @@ -397,33 +405,28 @@ void DeviceD3D11::FillDesc(const AGSDX11ReturnedParams& agsParams) {
m_Desc.combinedClipAndCullDistanceMaxNum = D3D11_CLIP_OR_CULL_DISTANCE_COUNT;
m_Desc.conservativeRasterTier = (uint8_t)options2.ConservativeRasterizationTier;

bool isShaderAtomicsF16Supported = false;
bool isShaderAtomicsF32Supported = false;
#if NRI_USE_EXT_LIBS
NvAPI_D3D11_IsNvShaderExtnOpCodeSupported(m_Device, NV_EXTN_OP_FP16_ATOMIC, &isShaderAtomicsF16Supported);
NvAPI_D3D11_IsNvShaderExtnOpCodeSupported(m_Device, NV_EXTN_OP_FP32_ATOMIC, &isShaderAtomicsF32Supported);

NV_D3D11_FEATURE_DATA_RASTERIZER_SUPPORT rasterizerFeatures = {};
NvAPI_D3D11_CheckFeatureSupport(m_Device, NV_D3D11_FEATURE_RASTERIZER, &rasterizerFeatures, sizeof(rasterizerFeatures));
m_Desc.programmableSampleLocationsTier = rasterizerFeatures.ProgrammableSamplePositions ? 2 : 0;
#endif

m_Desc.isTextureFilterMinMaxSupported = options1.MinMaxFiltering != 0;
m_Desc.isLogicOpSupported = options.OutputMergerLogicOp != 0;
m_Desc.isDepthBoundsTestSupported = agsParams.extensionsSupported.depthBoundsDeferredContexts;
m_Desc.isDrawIndirectCountSupported = agsParams.extensionsSupported.multiDrawIndirectCountIndirect;
m_Desc.isLineSmoothingSupported = true;

m_Desc.isShaderNativeI32Supported = true;
m_Desc.isShaderNativeF32Supported = true;
m_Desc.isShaderNativeF64Supported = options.ExtendedDoublesShaderInstructions;

bool isShaderAtomicsF16Supported = false;
NvAPI_D3D11_IsNvShaderExtnOpCodeSupported(m_Device, NV_EXTN_OP_FP16_ATOMIC, &isShaderAtomicsF16Supported);

bool isShaderAtomicsF32Supported = false;
NvAPI_D3D11_IsNvShaderExtnOpCodeSupported(m_Device, NV_EXTN_OP_FP32_ATOMIC, &isShaderAtomicsF32Supported);

bool isShaderAtomicsI64Supported = false;
NvAPI_D3D11_IsNvShaderExtnOpCodeSupported(m_Device, NV_EXTN_OP_UINT64_ATOMIC, &isShaderAtomicsI64Supported);

m_Desc.isShaderAtomicsF16Supported = isShaderAtomicsF16Supported;
m_Desc.isShaderAtomicsI32Supported = true;
m_Desc.isShaderAtomicsF32Supported = isShaderAtomicsF32Supported;
m_Desc.isShaderAtomicsI64Supported = (isShaderAtomicsI64Supported || agsParams.extensionsSupported.intrinsics19) ? true : false;

m_Desc.isSwapChainSupported = HasOutput();
m_Desc.isLowLatencySupported = m_Ext.HasNVAPI();
Expand Down
2 changes: 1 addition & 1 deletion Source/D3D11/DeviceD3D11.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ struct DeviceD3D11 final : public DeviceBase {
Result FillFunctionTable(StreamerInterface& streamerInterface) const;

private:
void FillDesc(const AGSDX11ReturnedParams& agsParams);
void FillDesc();

template <typename Implementation, typename Interface, typename... Args>
Result CreateImplementation(Interface*& entity, const Args&... args);
Expand Down
Loading

0 comments on commit 749f869

Please sign in to comment.