From 028bf26b8ea3c96b1968086c29dd0657318a2f0b Mon Sep 17 00:00:00 2001 From: dzhdan Date: Mon, 20 Mar 2023 20:11:26 +0400 Subject: [PATCH] v1.93: - NRI: replacing "QueueSemaphore" and "DeviceSemaphore" with "Fence" object - NRI: decoupling synchronization from submission of commands - NRI: added optional Streamline support - NRI: "CmdDispatchMeshTasks" got "Dispatch"-like parameters - NRI: "PhysicalDeviceGroup::description" changed back to char - NRI: fixed compilation of C-style interface on Linux - NRI: fixed potential out-of-bounds access in "GetPhysicalDevices" - VK: VK SDK updated to v1.3.239 - VK: NV-only extensions replaced with cross-vendor EXT extensions - VK: switched to VK v1.3, simplified initialization - VK: removed "error to warning" conversion for old errors - D3D12: fixed wrong PS_RESOURCE state usage in non-graphics queues - D3D12: expensive allocation in "CommandQueueD3D12::Submit" replaced with stack alloc - D3D11: fixed leaks caused by mixing "ptr" and "ComPtr" logic - D3D11: maximum frame latency is set based on number of buffers in SwapChain - fixes for Linux - Cmake improvements - updated dependencies - updated scripts - updated docs - refactoring --- .gitmodules | 2 +- 1-Deploy.sh | 2 +- 2-Build.bat | 4 +- 2-Build.sh | 4 +- CMakeLists.txt | 465 +-- External/Packman/bootstrap/configure.bat | 25 +- .../bootstrap/download_file_from_url.ps1 | 19 +- .../fetch_file_from_packman_bootstrap.cmd | 3 +- External/Packman/bootstrap/install_package.py | 143 +- External/Packman/packman.cmd | 22 +- External/Packman/{packman => packman.sh} | 90 +- External/vulkan | 2 +- Include/Extensions/NRIDeviceCreation.h | 2 +- Include/Extensions/NRIMeshShader.h | 2 +- Include/Extensions/NRISwapChain.h | 6 +- Include/Extensions/NRIWrapperVK.h | 12 +- .../Optional/NRIStreamlineWrapper.h | 37 + Include/NRI.h | 32 +- Include/NRIDescs.h | 11 +- Include/NRIMacro.h | 155 +- ProgrammingGuide.md | 643 ++-- README.md | 2 +- Resources/Version.h | 2 +- Source/Creation/Creation.cpp | 21 +- Source/D3D11/BufferD3D11.cpp | 56 +- Source/D3D11/BufferD3D11.h | 129 +- Source/D3D11/BufferD3D11.hpp | 13 +- Source/D3D11/CommandAllocatorD3D11.cpp | 43 +- Source/D3D11/CommandAllocatorD3D11.h | 51 +- Source/D3D11/CommandAllocatorD3D11.hpp | 11 +- Source/D3D11/CommandBufferD3D11.cpp | 176 +- Source/D3D11/CommandBufferD3D11.h | 164 +- Source/D3D11/CommandBufferD3D11.hpp | 54 +- Source/D3D11/CommandBufferEmuD3D11.cpp | 40 +- Source/D3D11/CommandBufferEmuD3D11.h | 127 +- Source/D3D11/CommandBufferEmuD3D11.hpp | 18 +- Source/D3D11/CommandQueueD3D11.cpp | 50 +- Source/D3D11/CommandQueueD3D11.h | 58 +- Source/D3D11/CommandQueueD3D11.hpp | 36 +- Source/D3D11/D3D11Extensions.cpp | 59 +- Source/D3D11/D3D11Extensions.h | 30 +- Source/D3D11/DescriptorD3D11.cpp | 76 +- Source/D3D11/DescriptorD3D11.h | 141 +- Source/D3D11/DescriptorD3D11.hpp | 11 +- Source/D3D11/DescriptorPoolD3D11.cpp | 23 +- Source/D3D11/DescriptorPoolD3D11.h | 67 +- Source/D3D11/DescriptorPoolD3D11.hpp | 11 +- Source/D3D11/DescriptorSetD3D11.cpp | 15 +- Source/D3D11/DescriptorSetD3D11.h | 76 +- Source/D3D11/DescriptorSetD3D11.hpp | 12 +- Source/D3D11/DeviceD3D11.cpp | 267 +- Source/D3D11/DeviceD3D11.h | 199 +- Source/D3D11/DeviceD3D11.hpp | 163 +- Source/D3D11/DeviceSemaphoreD3D11.cpp | 114 - Source/D3D11/DeviceSemaphoreD3D11.h | 43 - Source/D3D11/DeviceSemaphoreD3D11.hpp | 23 - Source/D3D11/FenceD3D11.cpp | 99 + Source/D3D11/FenceD3D11.h | 55 + Source/D3D11/FenceD3D11.hpp | 40 + Source/D3D11/FrameBufferD3D11.cpp | 17 +- Source/D3D11/FrameBufferD3D11.h | 54 +- Source/D3D11/FrameBufferD3D11.hpp | 23 - Source/D3D11/MemoryD3D11.cpp | 14 - Source/D3D11/MemoryD3D11.h | 78 +- Source/D3D11/MemoryD3D11.hpp | 23 - Source/D3D11/PipelineD3D11.cpp | 114 +- Source/D3D11/PipelineD3D11.h | 144 +- Source/D3D11/PipelineD3D11.hpp | 23 - Source/D3D11/PipelineLayoutD3D11.cpp | 55 +- Source/D3D11/PipelineLayoutD3D11.h | 169 +- Source/D3D11/PipelineLayoutD3D11.hpp | 23 - Source/D3D11/QueryPoolD3D11.cpp | 34 +- Source/D3D11/QueryPoolD3D11.h | 60 +- Source/D3D11/QueryPoolD3D11.hpp | 10 +- Source/D3D11/QueueSemaphoreD3D11.cpp | 31 - Source/D3D11/QueueSemaphoreD3D11.h | 39 - Source/D3D11/QueueSemaphoreD3D11.hpp | 23 - Source/D3D11/SharedD3D11.cpp | 3 +- Source/D3D11/SharedD3D11.h | 65 +- Source/D3D11/SwapChainD3D11.cpp | 94 +- Source/D3D11/SwapChainD3D11.h | 86 +- Source/D3D11/SwapChainD3D11.hpp | 21 +- Source/D3D11/TextureD3D11.cpp | 42 +- Source/D3D11/TextureD3D11.h | 94 +- Source/D3D11/TextureD3D11.hpp | 11 +- Source/D3D12/AccelerationStructureD3D12.cpp | 19 +- Source/D3D12/AccelerationStructureD3D12.h | 80 +- Source/D3D12/AccelerationStructureD3D12.hpp | 15 +- Source/D3D12/BufferD3D12.cpp | 7 +- Source/D3D12/BufferD3D12.h | 113 +- Source/D3D12/BufferD3D12.hpp | 13 +- Source/D3D12/CommandAllocatorD3D12.cpp | 8 +- Source/D3D12/CommandAllocatorD3D12.h | 57 +- Source/D3D12/CommandAllocatorD3D12.hpp | 11 +- Source/D3D12/CommandBufferD3D12.cpp | 99 +- Source/D3D12/CommandBufferD3D12.h | 200 +- Source/D3D12/CommandBufferD3D12.hpp | 88 +- Source/D3D12/CommandQueueD3D12.cpp | 41 +- Source/D3D12/CommandQueueD3D12.h | 69 +- Source/D3D12/CommandQueueD3D12.hpp | 36 +- Source/D3D12/ConversionD3D12.cpp | 12 +- Source/D3D12/DescriptorD3D12.cpp | 7 +- Source/D3D12/DescriptorD3D12.h | 132 +- .../DescriptorD3D12.hpp} | 16 +- Source/D3D12/DescriptorPoolD3D12.cpp | 22 +- Source/D3D12/DescriptorPoolD3D12.h | 81 +- Source/D3D12/DescriptorPoolD3D12.hpp | 11 +- Source/D3D12/DescriptorSetD3D12.cpp | 9 +- Source/D3D12/DescriptorSetD3D12.h | 100 +- Source/D3D12/DescriptorSetD3D12.hpp | 12 +- Source/D3D12/DeviceD3D12.cpp | 1059 +++--- Source/D3D12/DeviceD3D12.h | 324 +- Source/D3D12/DeviceD3D12.hpp | 177 +- Source/D3D12/DeviceSemaphoreD3D12.cpp | 47 - Source/D3D12/DeviceSemaphoreD3D12.h | 62 - Source/D3D12/FenceD3D12.cpp | 53 + Source/D3D12/FenceD3D12.h | 50 + Source/D3D12/FenceD3D12.hpp | 40 + Source/D3D12/FrameBufferD3D12.cpp | 12 - Source/D3D12/FrameBufferD3D12.h | 53 +- Source/D3D12/MemoryD3D12.cpp | 6 - Source/D3D12/MemoryD3D12.h | 69 +- Source/D3D12/PipelineD3D12.cpp | 46 +- Source/D3D12/PipelineD3D12.h | 139 +- Source/D3D12/PipelineD3D12.hpp | 2 +- Source/D3D12/PipelineLayoutD3D12.cpp | 44 +- Source/D3D12/PipelineLayoutD3D12.h | 168 +- Source/D3D12/PipelineLayoutD3D12.hpp | 23 - Source/D3D12/QueryPoolD3D12.cpp | 8 +- Source/D3D12/QueryPoolD3D12.h | 88 +- Source/D3D12/QueryPoolD3D12.hpp | 10 +- Source/D3D12/QueueSemaphoreD3D12.cpp | 45 - Source/D3D12/QueueSemaphoreD3D12.h | 62 - Source/D3D12/SharedD3D12.h | 2 +- Source/D3D12/SwapChainD3D12.cpp | 215 +- Source/D3D12/SwapChainD3D12.h | 93 +- Source/D3D12/SwapChainD3D12.hpp | 21 +- Source/D3D12/TextureD3D12.cpp | 25 +- Source/D3D12/TextureD3D12.h | 109 +- Source/D3D12/TextureD3D12.hpp | 11 +- Source/Shared/DeviceBase.h | 209 +- Source/Shared/HelperDataUpload.cpp | 21 +- Source/Shared/HelperDataUpload.h | 3 +- Source/Shared/HelperResourceStateChange.cpp | 18 +- Source/Shared/HelperWaitIdle.cpp | 16 +- Source/Shared/HelperWaitIdle.h | 2 +- Source/Shared/SharedExternal.h | 12 +- Source/Shared/StdAllocator.h | 19 +- Source/Streamline/StreamlineWrapper.cpp | 63 + Source/VK/AccelerationStructureVK.cpp | 19 +- Source/VK/AccelerationStructureVK.h | 119 +- Source/VK/AcceleratrionStructureVK.hpp | 17 +- Source/VK/BufferVK.cpp | 7 +- Source/VK/BufferVK.h | 120 +- Source/VK/BufferVK.hpp | 15 +- Source/VK/CommandAllocatorVK.cpp | 10 +- Source/VK/CommandAllocatorVK.h | 70 +- Source/VK/CommandAllocatorVK.hpp | 13 +- Source/VK/CommandBufferVK.cpp | 152 +- Source/VK/CommandBufferVK.h | 241 +- Source/VK/CommandBufferVK.hpp | 96 +- Source/VK/CommandQueueVK.cpp | 89 +- Source/VK/CommandQueueVK.h | 87 +- Source/VK/CommandQueueVK.hpp | 38 +- Source/VK/ConversionVK.h | 1547 ++++----- Source/VK/DescriptorPoolVK.cpp | 15 +- Source/VK/DescriptorPoolVK.h | 77 +- Source/VK/DescriptorPoolVK.hpp | 13 +- Source/VK/DescriptorSetVK.cpp | 112 +- Source/VK/DescriptorSetVK.h | 75 +- Source/VK/DescriptorSetVK.hpp | 14 +- Source/VK/DescriptorVK.cpp | 16 +- Source/VK/DescriptorVK.h | 240 +- Source/VK/DescriptorVK.hpp | 13 +- Source/VK/DeviceSemaphoreVK.cpp | 56 - Source/VK/DeviceSemaphoreVK.h | 49 - Source/VK/DeviceSemaphoreVK.hpp | 25 - Source/VK/DeviceVK.cpp | 2971 ++++++++--------- Source/VK/DeviceVK.h | 425 +-- Source/VK/DeviceVK.hpp | 198 +- Source/VK/DispatchTable.h | 63 +- Source/VK/FenceVK.cpp | 83 + Source/VK/FenceVK.h | 47 + Source/VK/FenceVK.hpp | 40 + Source/VK/FrameBufferVK.cpp | 9 +- Source/VK/FrameBufferVK.h | 130 +- Source/VK/FrameBufferVK.hpp | 25 - Source/VK/MemoryVK.cpp | 9 +- Source/VK/MemoryVK.h | 78 +- Source/VK/MemoryVK.hpp | 25 - Source/VK/PipelineLayoutVK.cpp | 24 +- Source/VK/PipelineLayoutVK.h | 165 +- Source/VK/PipelineLayoutVK.hpp | 25 - Source/VK/PipelineVK.cpp | 46 +- Source/VK/PipelineVK.h | 95 +- Source/VK/PipelineVK.hpp | 18 +- Source/VK/QueryPoolVK.cpp | 7 +- Source/VK/QueryPoolVK.h | 72 +- Source/VK/QueryPoolVK.hpp | 12 +- Source/VK/QueueSemaphoreVK.cpp | 56 - Source/VK/QueueSemaphoreVK.h | 49 - Source/VK/SharedVK.h | 16 +- Source/VK/SwapChainVK.cpp | 58 +- Source/VK/SwapChainVK.h | 73 +- Source/VK/SwapChainVK.hpp | 23 +- Source/VK/TextureVK.cpp | 25 +- Source/VK/TextureVK.h | 138 +- Source/VK/TextureVK.hpp | 13 +- Source/Validation/AccelerationStructureVal.h | 76 +- .../Validation/AccelerationStructureVal.hpp | 15 +- Source/Validation/BufferVal.h | 82 +- Source/Validation/BufferVal.hpp | 23 +- Source/Validation/CommandAllocatorVal.h | 22 +- Source/Validation/CommandAllocatorVal.hpp | 17 +- Source/Validation/CommandBufferVal.cpp | 12 +- Source/Validation/CommandBufferVal.h | 208 +- Source/Validation/CommandBufferVal.hpp | 173 +- Source/Validation/CommandQueueVal.cpp | 49 +- Source/Validation/CommandQueueVal.h | 53 +- Source/Validation/CommandQueueVal.hpp | 38 +- Source/Validation/DescriptorPoolVal.h | 74 +- Source/Validation/DescriptorPoolVal.hpp | 17 +- Source/Validation/DescriptorSetVal.h | 38 +- Source/Validation/DescriptorSetVal.hpp | 21 +- Source/Validation/DescriptorVal.h | 136 +- Source/Validation/DescriptorVal.hpp | 11 +- Source/Validation/DeviceSemaphoreVal.cpp | 49 - Source/Validation/DeviceSemaphoreVal.h | 39 - Source/Validation/DeviceSemaphoreVal.hpp | 23 - Source/Validation/DeviceVal.cpp | 79 +- Source/Validation/DeviceVal.h | 320 +- Source/Validation/DeviceVal.hpp | 177 +- Source/Validation/FenceVal.cpp | 48 + Source/Validation/FenceVal.h | 37 + Source/Validation/FenceVal.hpp | 41 + Source/Validation/FrameBufferVal.cpp | 2 - Source/Validation/FrameBufferVal.h | 18 +- Source/Validation/FrameBufferVal.hpp | 23 - Source/Validation/MemoryVal.cpp | 2 - Source/Validation/MemoryVal.h | 80 +- Source/Validation/MemoryVal.hpp | 23 - Source/Validation/PipelineLayoutVal.cpp | 2 - Source/Validation/PipelineLayoutVal.h | 40 +- Source/Validation/PipelineLayoutVal.hpp | 23 - Source/Validation/PipelineVal.h | 42 +- Source/Validation/PipelineVal.hpp | 12 +- Source/Validation/QueryPoolVal.h | 44 +- Source/Validation/QueryPoolVal.hpp | 14 +- Source/Validation/QueueSemaphoreVal.cpp | 44 - Source/Validation/QueueSemaphoreVal.h | 30 - Source/Validation/QueueSemaphoreVal.hpp | 24 - Source/Validation/SharedVal.h | 320 +- Source/Validation/SwapChainVal.cpp | 16 +- Source/Validation/SwapChainVal.h | 44 +- Source/Validation/SwapChainVal.hpp | 27 +- Source/Validation/TextureVal.h | 76 +- Source/Validation/TextureVal.hpp | 11 +- 257 files changed, 9767 insertions(+), 11681 deletions(-) rename External/Packman/{packman => packman.sh} (72%) create mode 100644 Include/Extensions/Optional/NRIStreamlineWrapper.h delete mode 100644 Source/D3D11/DeviceSemaphoreD3D11.cpp delete mode 100644 Source/D3D11/DeviceSemaphoreD3D11.h delete mode 100644 Source/D3D11/DeviceSemaphoreD3D11.hpp create mode 100644 Source/D3D11/FenceD3D11.cpp create mode 100644 Source/D3D11/FenceD3D11.h create mode 100644 Source/D3D11/FenceD3D11.hpp delete mode 100644 Source/D3D11/FrameBufferD3D11.hpp delete mode 100644 Source/D3D11/MemoryD3D11.hpp delete mode 100644 Source/D3D11/PipelineD3D11.hpp delete mode 100644 Source/D3D11/PipelineLayoutD3D11.hpp delete mode 100644 Source/D3D11/QueueSemaphoreD3D11.cpp delete mode 100644 Source/D3D11/QueueSemaphoreD3D11.h delete mode 100644 Source/D3D11/QueueSemaphoreD3D11.hpp rename Source/{VK/QueueSemaphoreVK.hpp => D3D12/DescriptorD3D12.hpp} (50%) delete mode 100644 Source/D3D12/DeviceSemaphoreD3D12.cpp delete mode 100644 Source/D3D12/DeviceSemaphoreD3D12.h create mode 100644 Source/D3D12/FenceD3D12.cpp create mode 100644 Source/D3D12/FenceD3D12.h create mode 100644 Source/D3D12/FenceD3D12.hpp delete mode 100644 Source/D3D12/PipelineLayoutD3D12.hpp delete mode 100644 Source/D3D12/QueueSemaphoreD3D12.cpp delete mode 100644 Source/D3D12/QueueSemaphoreD3D12.h create mode 100644 Source/Streamline/StreamlineWrapper.cpp delete mode 100644 Source/VK/DeviceSemaphoreVK.cpp delete mode 100644 Source/VK/DeviceSemaphoreVK.h delete mode 100644 Source/VK/DeviceSemaphoreVK.hpp create mode 100644 Source/VK/FenceVK.cpp create mode 100644 Source/VK/FenceVK.h create mode 100644 Source/VK/FenceVK.hpp delete mode 100644 Source/VK/FrameBufferVK.hpp delete mode 100644 Source/VK/MemoryVK.hpp delete mode 100644 Source/VK/PipelineLayoutVK.hpp delete mode 100644 Source/VK/QueueSemaphoreVK.cpp delete mode 100644 Source/VK/QueueSemaphoreVK.h delete mode 100644 Source/Validation/DeviceSemaphoreVal.cpp delete mode 100644 Source/Validation/DeviceSemaphoreVal.h delete mode 100644 Source/Validation/DeviceSemaphoreVal.hpp create mode 100644 Source/Validation/FenceVal.cpp create mode 100644 Source/Validation/FenceVal.h create mode 100644 Source/Validation/FenceVal.hpp delete mode 100644 Source/Validation/FrameBufferVal.hpp delete mode 100644 Source/Validation/MemoryVal.hpp delete mode 100644 Source/Validation/PipelineLayoutVal.hpp delete mode 100644 Source/Validation/QueueSemaphoreVal.cpp delete mode 100644 Source/Validation/QueueSemaphoreVal.h delete mode 100644 Source/Validation/QueueSemaphoreVal.hpp diff --git a/.gitmodules b/.gitmodules index 63a1d3e1..8ff22427 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,5 +1,5 @@ [submodule "External/vulkan"] path = External/vulkan url = https://github.com/KhronosGroup/Vulkan-Headers.git - branch = sdk-1.3.236.0 + branch = sdk-1.3.239.0 update = merge diff --git a/1-Deploy.sh b/1-Deploy.sh index 4d4e5a8a..22886a98 100644 --- a/1-Deploy.sh +++ b/1-Deploy.sh @@ -10,5 +10,5 @@ chmod +x "4-Clean.sh" mkdir -p "_Compiler" cd "_Compiler" -cmake .. -A x64 +cmake .. cd .. diff --git a/2-Build.bat b/2-Build.bat index 9fad85c8..f028aad7 100644 --- a/2-Build.bat +++ b/2-Build.bat @@ -1,6 +1,6 @@ @echo off cd "_Compiler" -cmake --build . --config Release -cmake --build . --config Debug +cmake --build . --config Release -j 4 +cmake --build . --config Debug -j 4 cd .. diff --git a/2-Build.sh b/2-Build.sh index 079e5297..ae972c48 100644 --- a/2-Build.sh +++ b/2-Build.sh @@ -4,6 +4,6 @@ mkdir -p "_Compiler" cd "_Compiler" cmake .. -cmake --build . --config Release -cmake --build . --config Debug +cmake --build . --config Release -j 4 +cmake --build . --config Debug -j 4 cd .. diff --git a/CMakeLists.txt b/CMakeLists.txt index 6b362863..304331bc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,247 +1,302 @@ -cmake_minimum_required(VERSION 3.15) +cmake_minimum_required (VERSION 3.15) # Options -option(NRI_DISABLE_XLIB_SUPPORT "Disable 'xlib' support" OFF) -option(NRI_DISABLE_WAYLAND_SUPPORT "Disable 'wayland' support" OFF) -option(NRI_STATIC_LIBRARY "Build static library" OFF) +option (NRI_DISABLE_XLIB_SUPPORT "Disable 'xlib' support" OFF) +option (NRI_DISABLE_WAYLAND_SUPPORT "Disable 'wayland' support" OFF) +option (NRI_STATIC_LIBRARY "Build static library" OFF) + +if (WIN32) + option (NRI_ENABLE_STREAMLINE_SUPPORT "Enables streamline wrapper for streamline integration. Requires NRI_STREAMLINE_SDK_PATH to point to a valid streamline package" OFF) + set (NRI_STREAMLINE_SDK_PATH "" CACHE STRING "Path to a directory containing streamline sdk package. Must not include the name of the library 'sl.interposer.lib'. Must not be empty when NRI_ENABLE_STREAMLINE_SUPPORT is on.") +endif () + +# Is submodule? +if (${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}) + set (IS_SUBMODULE OFF) +else() + set (IS_SUBMODULE ON) +endif() # Cached -set(GLOBAL_BIN_OUTPUT_PATH "${CMAKE_SOURCE_DIR}/_Build" CACHE STRING "") -set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "") +if (NOT IS_SUBMODULE) + set (CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "") +endif() + +set (GLOBAL_BIN_OUTPUT_PATH "${CMAKE_SOURCE_DIR}/_Build" CACHE STRING "") # Create project -file(READ "${CMAKE_CURRENT_SOURCE_DIR}/Include/NRI.h" ver_h) -string(REGEX MATCH "NRI_VERSION_MAJOR ([0-9]*)" _ ${ver_h}) -set(VERSION_MAJOR ${CMAKE_MATCH_1}) -string(REGEX MATCH "NRI_VERSION_MINOR ([0-9]*)" _ ${ver_h}) -set(VERSION_MINOR ${CMAKE_MATCH_1}) +file (READ "${CMAKE_CURRENT_SOURCE_DIR}/Include/NRI.h" ver_h) +string (REGEX MATCH "NRI_VERSION_MAJOR ([0-9]*)" _ ${ver_h}) +set (VERSION_MAJOR ${CMAKE_MATCH_1}) +string (REGEX MATCH "NRI_VERSION_MINOR ([0-9]*)" _ ${ver_h}) +set (VERSION_MINOR ${CMAKE_MATCH_1}) -message("NRI v${VERSION_MAJOR}.${VERSION_MINOR}") -project(NRI LANGUAGES C CXX) +message ("NRI v${VERSION_MAJOR}.${VERSION_MINOR}") +project (NRI LANGUAGES C CXX) # Download dependencies using Packman -if(WIN32) - set(PACKMAN_EXT ".cmd") -endif() +if (WIN32) + set (PACKMAN_EXT ".cmd") +else () + set (PACKMAN_EXT ".sh") +endif () -if((CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64") OR (CMAKE_SYSTEM_PROCESSOR MATCHES "AMD64")) - if(WIN32) - set(PACKMAN_PLATFORM "windows-x86_64") - else() - set(PACKMAN_PLATFORM "linux-x86_64") - endif() -elseif((CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64") OR (CMAKE_SYSTEM_PROCESSOR MATCHES "arm64")) - set(PACKMAN_PLATFORM "linux-aarch64") -else() - message(FATAL_ERROR "Unsupported architecture: '${CMAKE_SYSTEM_PROCESSOR}'") -endif() -message("Packman platform: ${PACKMAN_PLATFORM}") +if ((CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64") OR(CMAKE_SYSTEM_PROCESSOR MATCHES "AMD64")) + if (WIN32) + set (PACKMAN_PLATFORM "windows-x86_64") + else () + set (PACKMAN_PLATFORM "linux-x86_64") + endif () +elseif ((CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64") OR(CMAKE_SYSTEM_PROCESSOR MATCHES "arm64")) + set (PACKMAN_PLATFORM "linux-aarch64") +else () + message (FATAL_ERROR "Unsupported architecture: '${CMAKE_SYSTEM_PROCESSOR}'") +endif () + +message ("Packman platform: ${PACKMAN_PLATFORM}") -execute_process(COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/External/Packman/Packman${PACKMAN_EXT}" pull "${CMAKE_CURRENT_SOURCE_DIR}/Dependencies.xml" -p ${PACKMAN_PLATFORM} +execute_process (COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/External/Packman/packman${PACKMAN_EXT}" pull "${CMAKE_CURRENT_SOURCE_DIR}/Dependencies.xml" -p ${PACKMAN_PLATFORM} WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" RESULT_VARIABLE PACKMAN_RESULT) -if(NOT "${PACKMAN_RESULT}" STREQUAL "0") - message(FATAL_ERROR "Packman failed(code = ${PACKMAN_RESULT})") -endif() +if (NOT "${PACKMAN_RESULT}" STREQUAL "0") + message (FATAL_ERROR "Packman failed(code = ${PACKMAN_RESULT})") +endif () # Setup dependencies -if(WIN32) - if(CMAKE_SIZEOF_VOID_P EQUAL 8) - set(BIN_ARCHITECTURE "x64") - set(NVAPI_BIN_ARCHITECTURE "amd64") - else() - set(BIN_ARCHITECTURE "x86") - set(NVAPI_BIN_ARCHITECTURE "x86") - endif() +if (WIN32) + if (CMAKE_SIZEOF_VOID_P EQUAL 8) + set (BIN_ARCHITECTURE "x64") + set (NVAPI_BIN_ARCHITECTURE "amd64") + else () + set (BIN_ARCHITECTURE "x86") + set (NVAPI_BIN_ARCHITECTURE "x86") + endif () # External - 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/AGS/lib" REQUIRED) + 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/AGS/lib" REQUIRED) # System - if("$ENV{WIN10_SDK_PATH}$ENV{WIN10_SDK_VERSION}" STREQUAL "") - get_filename_component(WINDOWS_SDK_PATH "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows Kits\\Installed Roots;KitsRoot10]" ABSOLUTE CACHE) - set(WINDOWS_SDK_VERSION ${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}) - if("${WINDOWS_SDK_VERSION}" STREQUAL "") - set(WINDOWS_SDK_VERSION ${CMAKE_SYSTEM_VERSION}) - message("Using CMAKE_SYSTEM_VERSION as WindowsSDK version.(${CMAKE_SYSTEM_VERSION})") - endif() - else() - set(WINDOWS_SDK_PATH $ENV{WIN10_SDK_PATH}) - set(WINDOWS_SDK_VERSION $ENV{WIN10_SDK_VERSION}) - endif() - - if("${WINDOWS_SDK_PATH}" STREQUAL "") - message(FATAL_ERROR "Failed to detect WindowsSDK path. Please add enviroment variables WIN10_SDK_PATH and WIN10_SDK_VERSION or use MSVC") - endif() - if("${WINDOWS_SDK_VERSION}" STREQUAL "") - message(FATAL_ERROR "Failed to detect WindowsSDK version. Please add enviroment variables WIN10_SDK_PATH and WIN10_SDK_VERSION or use MSVC") - endif() - if(IS_DIRECTORY "${WINDOWS_SDK_PATH}/Include/${WINDOWS_SDK_VERSION}.0") - set(WINDOWS_SDK_VERSION "${WINDOWS_SDK_VERSION}.0") - endif() - set(WINDOWS_SDK_LIB_PATH "${WINDOWS_SDK_PATH}/Lib/${WINDOWS_SDK_VERSION}/um/${BIN_ARCHITECTURE}") - - find_library(INPUT_LIB_D3D11 NAMES d3d11 HINTS ${WINDOWS_SDK_LIB_PATH} REQUIRED) - find_library(INPUT_LIB_D3D12 NAMES d3d12 HINTS ${WINDOWS_SDK_LIB_PATH} REQUIRED) - find_library(INPUT_LIB_DXGI NAMES dxgi HINTS ${WINDOWS_SDK_LIB_PATH} REQUIRED) - find_library(INPUT_LIB_DXGUID NAMES dxguid HINTS ${WINDOWS_SDK_LIB_PATH} REQUIRED) -endif() + if ("$ENV{WIN10_SDK_PATH}$ENV{WIN10_SDK_VERSION}" STREQUAL "") + get_filename_component (WINDOWS_SDK_PATH "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows Kits\\Installed Roots;KitsRoot10]" ABSOLUTE CACHE) + set (WINDOWS_SDK_VERSION ${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}) + + if ("${WINDOWS_SDK_VERSION}" STREQUAL "") + set (WINDOWS_SDK_VERSION ${CMAKE_SYSTEM_VERSION}) + message ("Using CMAKE_SYSTEM_VERSION as WindowsSDK version.(${CMAKE_SYSTEM_VERSION})") + endif () + else () + set (WINDOWS_SDK_PATH $ENV{WIN10_SDK_PATH}) + set (WINDOWS_SDK_VERSION $ENV{WIN10_SDK_VERSION}) + endif () + + if ("${WINDOWS_SDK_PATH}" STREQUAL "") + message (FATAL_ERROR "Failed to detect WindowsSDK path. Please add enviroment variables WIN10_SDK_PATH and WIN10_SDK_VERSION or use MSVC") + endif () + + if ("${WINDOWS_SDK_VERSION}" STREQUAL "") + message (FATAL_ERROR "Failed to detect WindowsSDK version. Please add enviroment variables WIN10_SDK_PATH and WIN10_SDK_VERSION or use MSVC") + endif () + + if (IS_DIRECTORY "${WINDOWS_SDK_PATH}/Include/${WINDOWS_SDK_VERSION}.0") + set (WINDOWS_SDK_VERSION "${WINDOWS_SDK_VERSION}.0") + endif () + + set (WINDOWS_SDK_LIB_PATH "${WINDOWS_SDK_PATH}/Lib/${WINDOWS_SDK_VERSION}/um/${BIN_ARCHITECTURE}") + + find_library (INPUT_LIB_D3D11 NAMES d3d11 HINTS ${WINDOWS_SDK_LIB_PATH} REQUIRED) + find_library (INPUT_LIB_D3D12 NAMES d3d12 HINTS ${WINDOWS_SDK_LIB_PATH} REQUIRED) + find_library (INPUT_LIB_DXGI NAMES dxgi HINTS ${WINDOWS_SDK_LIB_PATH} REQUIRED) + find_library (INPUT_LIB_DXGUID NAMES dxguid HINTS ${WINDOWS_SDK_LIB_PATH} REQUIRED) +endif () # Globals? -set_property(GLOBAL PROPERTY USE_FOLDERS ON) - -set(CMAKE_CXX_STANDARD 17) -set(CMAKE_CXX_STANDARD_REQUIRED ON) -set(CMAKE_CXX_VISIBILITY_PRESET hidden) -set(CMAKE_CXX_EXTENSIONS OFF) -set(CMAKE_POSITION_INDEPENDENT_CODE ON) -set(CMAKE_POSITION_INDEPENDENT_BINARIES ON) -set(CMAKE_VISIBILITY_INLINES_HIDDEN ON) - -if(MSVC) - set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") -else() - set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} -static-libstdc++ -static-libgcc") - set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -static-libstdc++ -static-libgcc") - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++ -static-libgcc") -endif() +set_property (GLOBAL PROPERTY USE_FOLDERS ON) + +set (CMAKE_CXX_STANDARD 17) +set (CMAKE_CXX_STANDARD_REQUIRED ON) +set (CMAKE_CXX_VISIBILITY_PRESET hidden) +set (CMAKE_CXX_EXTENSIONS OFF) +set (CMAKE_POSITION_INDEPENDENT_CODE ON) +set (CMAKE_POSITION_INDEPENDENT_BINARIES ON) +set (CMAKE_VISIBILITY_INLINES_HIDDEN ON) + +if (MSVC) + set (CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") +endif () # Compile options -if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") - set(COMPILE_OPTIONS -Wextra) -elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU") - set(COMPILE_OPTIONS -Wextra) -elseif(MSVC) - set(COMPILE_OPTIONS /W4 /WX /wd4324) -else() - message(WARNING "Unknown compiler!") -endif() +if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") + set (COMPILE_OPTIONS -msse4.1 -Wextra) +elseif (CMAKE_CXX_COMPILER_ID MATCHES "GNU") + set (COMPILE_OPTIONS -msse4.1 -Wextra) +elseif (MSVC) + set (COMPILE_OPTIONS /W4 /WX /wd4324) +else () + message (WARNING "Unknown compiler!") +endif () # Compile definitions -set(COMPILE_DEFINITIONS NRI_USE_VULKAN=1) -if(WIN32) - set(COMPILE_DEFINITIONS ${COMPILE_DEFINITIONS} WIN32_LEAN_AND_MEAN NOMINMAX _CRT_SECURE_NO_WARNINGS _UNICODE UNICODE _ENFORCE_MATCHING_ALLOCATORS=0) - set(COMPILE_DEFINITIONS ${COMPILE_DEFINITIONS} NRI_USE_D3D11=1 NRI_USE_D3D12=1) -endif() +set (COMPILE_DEFINITIONS NRI_USE_VULKAN=1) + +if (WIN32) + set (COMPILE_DEFINITIONS ${COMPILE_DEFINITIONS} WIN32_LEAN_AND_MEAN NOMINMAX _CRT_SECURE_NO_WARNINGS _UNICODE UNICODE _ENFORCE_MATCHING_ALLOCATORS=0) + set (COMPILE_DEFINITIONS ${COMPILE_DEFINITIONS} NRI_USE_D3D11=1 NRI_USE_D3D12=1) +endif () # Shared -file(GLOB SHARED_SOURCE "Source/Shared/*.cpp" "Source/Shared/*.h" "Source/Shared/*.hpp") -source_group("" FILES ${SHARED_SOURCE}) -add_library(NRI_Shared STATIC ${SHARED_SOURCE}) -target_include_directories(NRI_Shared PRIVATE "Include" "Source/Shared") -target_compile_definitions(NRI_Shared PRIVATE ${COMPILE_DEFINITIONS}) -target_compile_options(NRI_Shared PRIVATE ${COMPILE_OPTIONS}) -set_property(TARGET NRI_Shared PROPERTY FOLDER ${PROJECT_NAME}) +file (GLOB SHARED_SOURCE "Source/Shared/*.cpp" "Source/Shared/*.h" "Source/Shared/*.hpp") +source_group ("" FILES ${SHARED_SOURCE}) +add_library (NRI_Shared STATIC ${SHARED_SOURCE}) +target_include_directories (NRI_Shared PRIVATE "Include" "Source/Shared") +target_compile_definitions (NRI_Shared PRIVATE ${COMPILE_DEFINITIONS}) +target_compile_options (NRI_Shared PRIVATE ${COMPILE_OPTIONS}) +set_property (TARGET NRI_Shared PROPERTY FOLDER ${PROJECT_NAME}) + +if (NOT NRI_ENABLE_STREAMLINE_SUPPORT) + set (INPUT_LIBS_D3D11 ${INPUT_LIB_D3D11} ${INPUT_LIB_DXGI}) + set (INPUT_LIBS_D3D12 ${INPUT_LIB_D3D12} ${INPUT_LIB_DXGI}) +endif () # D3D11 -if(WIN32) - 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/AGS/inc/*.h") - source_group("External/AMDAGS" FILES ${D3D11_AMDAGS}) - add_library(NRI_D3D11 STATIC ${D3D11_SOURCE} ${D3D11_NVAPI} ${D3D11_AMDAGS}) - 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_LIB_D3D11} ${INPUT_LIB_DXGI} ${INPUT_LIB_DXGUID} ${INPUT_LIB_NVAPI}) - set_property(TARGET NRI_D3D11 PROPERTY FOLDER ${PROJECT_NAME}) -endif() +if (WIN32) + 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/AGS/inc/*.h") + source_group ("External/AMDAGS" FILES ${D3D11_AMDAGS}) + add_library (NRI_D3D11 STATIC ${D3D11_SOURCE} ${D3D11_NVAPI} ${D3D11_AMDAGS}) + 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_DXGUID} ${INPUT_LIB_NVAPI}) + set_property (TARGET NRI_D3D11 PROPERTY FOLDER ${PROJECT_NAME}) +endif () # D3D12 -if(WIN32) - file(GLOB D3D12_SOURCE "Source/D3D12/*.cpp" "Source/D3D12/*.h" "Source/D3D12/*.hpp") - source_group("" FILES ${D3D12_SOURCE}) - add_library(NRI_D3D12 STATIC ${D3D12_SOURCE}) - target_include_directories(NRI_D3D12 PRIVATE "Include" "Source/Shared") - 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_LIB_D3D12} ${INPUT_LIB_DXGI}) - set_property(TARGET NRI_D3D12 PROPERTY FOLDER ${PROJECT_NAME}) -endif() +if (WIN32) + file (GLOB D3D12_SOURCE "Source/D3D12/*.cpp" "Source/D3D12/*.h" "Source/D3D12/*.hpp") + source_group ("" FILES ${D3D12_SOURCE}) + add_library (NRI_D3D12 STATIC ${D3D12_SOURCE}) + target_include_directories (NRI_D3D12 PRIVATE "Include" "Source/Shared") + 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}) + set_property (TARGET NRI_D3D12 PROPERTY FOLDER ${PROJECT_NAME}) +endif () # VK -file(GLOB VK_SOURCE "Source/VK/*.cpp" "Source/VK/*.h" "Source/VK/*.hpp") -source_group("" FILES ${VK_SOURCE}) -add_library(NRI_VK STATIC ${VK_SOURCE}) -target_include_directories(NRI_VK PRIVATE "Include" "Source/Shared" "External/vulkan/include") -target_compile_definitions(NRI_VK PRIVATE ${COMPILE_DEFINITIONS}) -target_compile_options(NRI_VK PRIVATE ${COMPILE_OPTIONS}) -target_link_libraries(NRI_VK PRIVATE NRI_Shared) -set_property(TARGET NRI_VK PROPERTY FOLDER ${PROJECT_NAME}) - -if(WIN32) - target_compile_definitions(NRI_VK PRIVATE VK_USE_PLATFORM_WIN32_KHR) -elseif(APPLE) - target_compile_definitions(NRI_VK PRIVATE VK_USE_PLATFORM_METAL_EXT) -else() - if(NOT NRI_DISABLE_XLIB_SUPPORT) - find_path(XLIB_HEADERS NAMES "X11/Xlib.h") - if(NOT XLIB_HEADERS) - message(FATAL_ERROR "Can't find 'X11/xlib.h'.(libx11-dev is not installed?)") - endif() - target_compile_definitions(NRI_VK PRIVATE VK_USE_PLATFORM_XLIB_KHR) - endif() - if(NOT NRI_DISABLE_WAYLAND_SUPPORT) - find_path(WAYLAND_HEADERS NAMES "wayland-client.h") - if(NOT WAYLAND_HEADERS) - message(FATAL_ERROR "Can't find 'wayland-client.h'.(libwayland-dev is not installed?)") - endif() - target_compile_definitions(NRI_VK PRIVATE VK_USE_PLATFORM_WAYLAND_KHR) - endif() -endif() +file (GLOB VK_SOURCE "Source/VK/*.cpp" "Source/VK/*.h" "Source/VK/*.hpp") +source_group ("" FILES ${VK_SOURCE}) +add_library (NRI_VK STATIC ${VK_SOURCE}) +target_include_directories (NRI_VK PRIVATE "Include" "Source/Shared" "External/vulkan/include") +target_compile_definitions (NRI_VK PRIVATE ${COMPILE_DEFINITIONS}) +target_compile_options (NRI_VK PRIVATE ${COMPILE_OPTIONS}) +target_link_libraries (NRI_VK PRIVATE NRI_Shared) +set_property (TARGET NRI_VK PROPERTY FOLDER ${PROJECT_NAME}) + +if (WIN32) + target_compile_definitions (NRI_VK PRIVATE VK_USE_PLATFORM_WIN32_KHR) +elseif (APPLE) + target_compile_definitions (NRI_VK PRIVATE VK_USE_PLATFORM_METAL_EXT) +else () + if (NOT NRI_DISABLE_XLIB_SUPPORT) + find_path (XLIB_HEADERS NAMES "X11/Xlib.h") + + if (NOT XLIB_HEADERS) + message (FATAL_ERROR "Can't find 'X11/xlib.h'.(libx11-dev is not installed?)") + endif () + + target_compile_definitions (NRI_VK PRIVATE VK_USE_PLATFORM_XLIB_KHR) + endif () + + if (NOT NRI_DISABLE_WAYLAND_SUPPORT) + find_path (WAYLAND_HEADERS NAMES "wayland-client.h") + + if (NOT WAYLAND_HEADERS) + message (FATAL_ERROR "Can't find 'wayland-client.h'.(libwayland-dev is not installed?)") + endif () + + target_compile_definitions (NRI_VK PRIVATE VK_USE_PLATFORM_WAYLAND_KHR) + endif () +endif () # Validation -file(GLOB NRI_VALIDATION_SOURCE "Source/Validation/*.cpp" "Source/Validation/*.h" "Source/Validation/*.hpp") -source_group("" FILES ${NRI_VALIDATION_SOURCE}) -add_library(NRI_Validation STATIC ${NRI_VALIDATION_SOURCE}) -target_include_directories(NRI_Validation PRIVATE "Include" "Source/Shared") -target_compile_definitions(NRI_Validation PRIVATE ${COMPILE_DEFINITIONS}) -target_compile_options(NRI_Validation PRIVATE ${COMPILE_OPTIONS}) -target_link_libraries(NRI_Validation PRIVATE NRI_Shared) -set_property(TARGET NRI_Validation PROPERTY FOLDER ${PROJECT_NAME}) +file (GLOB NRI_VALIDATION_SOURCE "Source/Validation/*.cpp" "Source/Validation/*.h" "Source/Validation/*.hpp") +source_group ("" FILES ${NRI_VALIDATION_SOURCE}) +add_library (NRI_Validation STATIC ${NRI_VALIDATION_SOURCE}) +target_include_directories (NRI_Validation PRIVATE "Include" "Source/Shared") +target_compile_definitions (NRI_Validation PRIVATE ${COMPILE_DEFINITIONS}) +target_compile_options (NRI_Validation PRIVATE ${COMPILE_OPTIONS}) +target_link_libraries (NRI_Validation PRIVATE NRI_Shared) +set_property (TARGET NRI_Validation PROPERTY FOLDER ${PROJECT_NAME}) # NRI -file(GLOB NRI_HEADERS "Include/*.h" "Include/*.hpp") -source_group("Include" FILES ${NRI_HEADERS}) -file(GLOB NRI_EXTENSIONS "Include/Extensions/*.h" "Include/Extensions/*.hpp") -source_group("Include/Extensions" FILES ${NRI_EXTENSIONS}) -file(GLOB NRI_SOURCE "Source/Creation/*.cpp" "Source/Creation/*.h") -source_group("Sources" FILES ${NRI_SOURCE}) -file(GLOB NRI_RESOURCES "Resources/*") -source_group("Resources" FILES ${NRI_RESOURCES}) - -if(NRI_STATIC_LIBRARY) - add_library(${PROJECT_NAME} STATIC ${NRI_SOURCE} ${NRI_HEADERS} ${NRI_RESOURCES} ${NRI_EXTENSIONS}) -else() - add_library(${PROJECT_NAME} SHARED ${NRI_SOURCE} ${NRI_HEADERS} ${NRI_RESOURCES} ${NRI_EXTENSIONS}) - - if(WIN32) - target_compile_definitions(${PROJECT_NAME} PRIVATE "NRI_API=extern \"C\" __declspec(dllexport)") - target_compile_definitions(${PROJECT_NAME} PRIVATE "NRIC_API=extern \"C\" __declspec(dllexport)") - else() - target_compile_definitions(${PROJECT_NAME} PRIVATE "NRI_API=extern \"C\" __attribute__((visibility(\"default\")))") - target_compile_definitions(${PROJECT_NAME} PRIVATE "NRIC_API=extern \"C\" __attribute__((visibility(\"default\")))") - endif() -endif() +file (GLOB NRI_HEADERS "Include/*.h" "Include/*.hpp") +source_group ("Include" FILES ${NRI_HEADERS}) +file (GLOB NRI_EXTENSIONS "Include/Extensions/*.h" "Include/Extensions/*.hpp") +source_group ("Include/Extensions" FILES ${NRI_EXTENSIONS}) +file (GLOB NRI_SOURCE "Source/Creation/*.cpp" "Source/Creation/*.h") -target_include_directories(${PROJECT_NAME} PRIVATE "Include" "Source/Shared" "External/vulkan/include") -target_compile_definitions(${PROJECT_NAME} PRIVATE ${COMPILE_DEFINITIONS}) -target_compile_options(${PROJECT_NAME} PRIVATE ${COMPILE_OPTIONS}) +if (NRI_ENABLE_STREAMLINE_SUPPORT) + file (GLOB NRI_SOURCE ${NRI_SOURCE} "Source/Streamline/*.cpp") + file (GLOB NRI_EXTENSIONS ${NRI_EXTENSIONS} "Include/Extensions/Optional/NRIStreamlineWrapper.h") +endif () -target_link_libraries(${PROJECT_NAME} PRIVATE NRI_Shared NRI_VK NRI_Validation) -if(WIN32) - target_link_libraries(${PROJECT_NAME} PRIVATE NRI_D3D11 NRI_D3D12) -elseif(UNIX) - target_link_libraries(${PROJECT_NAME} PRIVATE ${CMAKE_DL_LIBS}) -endif() +source_group ("Sources" FILES ${NRI_SOURCE}) +file (GLOB NRI_RESOURCES "Resources/*") +source_group ("Resources" FILES ${NRI_RESOURCES}) + +if (NRI_STATIC_LIBRARY) + add_library (${PROJECT_NAME} STATIC ${NRI_SOURCE} ${NRI_HEADERS} ${NRI_RESOURCES} ${NRI_EXTENSIONS}) +else () + add_library (${PROJECT_NAME} SHARED ${NRI_SOURCE} ${NRI_HEADERS} ${NRI_RESOURCES} ${NRI_EXTENSIONS}) + + if (WIN32) + target_compile_definitions (${PROJECT_NAME} PRIVATE "NRI_API=extern \"C\" __declspec(dllexport)") + target_compile_definitions (${PROJECT_NAME} PRIVATE "NRIC_API=extern \"C\" __declspec(dllexport)") + else () + target_compile_definitions (${PROJECT_NAME} PRIVATE "NRI_API=extern \"C\" __attribute__((visibility(\"default\")))") + target_compile_definitions (${PROJECT_NAME} PRIVATE "NRIC_API=extern \"C\" __attribute__((visibility(\"default\")))") + endif () +endif () + +if (NRI_ENABLE_STREAMLINE_SUPPORT) + set (SL_INTERPOSER_LINK_PATH "${NRI_STREAMLINE_SDK_PATH}/lib/x64/sl.interposer.lib") + set (SL_INCLUDE_DIRECTORY "${NRI_STREAMLINE_SDK_PATH}/include/") + set (SL_PLUGINS_DIRECTORY ${NRI_STREAMLINE_SDK_PATH}/bin/x64) + + target_include_directories (${PROJECT_NAME} PRIVATE ${SL_INCLUDE_DIRECTORY}) + target_link_libraries (${PROJECT_NAME} PRIVATE ${SL_INTERPOSER_LINK_PATH}) + target_compile_definitions (${PROJECT_NAME} PRIVATE SL_PLUGINS_DIRECTORY=${SL_PLUGINS_DIRECTORY}) + + file (TO_NATIVE_PATH ${GLOBAL_BIN_OUTPUT_PATH}/$ BIN_OUTPUT_PATH_NATIVE) + file (TO_NATIVE_PATH ${SL_PLUGINS_DIRECTORY} SL_PLUGINS_PATH_NATIVE) + + add_custom_command (TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy ${BIN_OUTPUT_PATH_NATIVE} ${SL_PLUGINS_PATH_NATIVE} + COMMENT "Copying '${BIN_OUTPUT_PATH_NATIVE}' to '${SL_PLUGINS_PATH_NATIVE}'" + VERBATIM + ) +endif () + +target_include_directories (${PROJECT_NAME} PRIVATE "Include" "Source/Shared" "External/vulkan/include") +target_compile_definitions (${PROJECT_NAME} PRIVATE ${COMPILE_DEFINITIONS}) +target_compile_options (${PROJECT_NAME} PRIVATE ${COMPILE_OPTIONS}) + +target_link_libraries (${PROJECT_NAME} PRIVATE NRI_Shared NRI_VK NRI_Validation) + +if (WIN32) + target_link_libraries (${PROJECT_NAME} PRIVATE NRI_D3D11 NRI_D3D12) +elseif (UNIX) + target_link_libraries (${PROJECT_NAME} PRIVATE ${CMAKE_DL_LIBS}) +endif () -set_property(TARGET ${PROJECT_NAME} PROPERTY FOLDER ${PROJECT_NAME}) +set_property (TARGET ${PROJECT_NAME} PROPERTY FOLDER ${PROJECT_NAME}) -set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${GLOBAL_BIN_OUTPUT_PATH}/$") -set_target_properties(${PROJECT_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${GLOBAL_BIN_OUTPUT_PATH}/$") -set_target_properties(${PROJECT_NAME} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY "${GLOBAL_BIN_OUTPUT_PATH}/$") -message("NRI output path: '${GLOBAL_BIN_OUTPUT_PATH}'") +set_target_properties (${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${GLOBAL_BIN_OUTPUT_PATH}/$") +set_target_properties (${PROJECT_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${GLOBAL_BIN_OUTPUT_PATH}/$") +set_target_properties (${PROJECT_NAME} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY "${GLOBAL_BIN_OUTPUT_PATH}/$") +message ("NRI output path: '${GLOBAL_BIN_OUTPUT_PATH}'") diff --git a/External/Packman/bootstrap/configure.bat b/External/Packman/bootstrap/configure.bat index 13af294f..2f4e6b1d 100644 --- a/External/Packman/bootstrap/configure.bat +++ b/External/Packman/bootstrap/configure.bat @@ -1,4 +1,4 @@ -:: Copyright 2019 NVIDIA CORPORATION +:: Copyright 2019-2023 NVIDIA CORPORATION :: :: Licensed under the Apache License, Version 2.0 (the "License"); :: you may not use this file except in compliance with the License. @@ -12,7 +12,7 @@ :: See the License for the specific language governing permissions and :: limitations under the License. -set PM_PACKMAN_VERSION=6.35 +set PM_PACKMAN_VERSION=7.2.1 :: Specify where packman command is rooted set PM_INSTALL_PATH=%~dp0.. @@ -48,7 +48,7 @@ echo. :: that may be needed in the path :ENSURE_DIR if not exist "%PM_PACKAGES_ROOT%" ( - echo Creating directory %PM_PACKAGES_ROOT% + echo Creating packman packages cache at %PM_PACKAGES_ROOT% mkdir "%PM_PACKAGES_ROOT%" ) if %errorlevel% neq 0 ( goto ERROR_MKDIR_PACKAGES_ROOT ) @@ -59,7 +59,7 @@ if defined PM_PYTHON_EXT ( goto PACKMAN ) -set PM_PYTHON_VERSION=3.7.9-windows-x86_64 +set PM_PYTHON_VERSION=3.10.5-1-windows-x86_64 set PM_PYTHON_BASE_DIR=%PM_PACKAGES_ROOT%\python set PM_PYTHON_DIR=%PM_PYTHON_BASE_DIR%\%PM_PYTHON_VERSION% set PM_PYTHON=%PM_PYTHON_DIR%\python.exe @@ -112,9 +112,9 @@ if defined PM_MODULE_DIR_EXT ( set PM_MODULE_DIR=%PM_PACKAGES_ROOT%\packman-common\%PM_PACKMAN_VERSION% ) -set PM_MODULE=%PM_MODULE_DIR%\packman.py +set PM_MODULE=%PM_MODULE_DIR%\run.py -if exist "%PM_MODULE%" goto ENSURE_7ZA +if exist "%PM_MODULE%" goto END :: Clean out broken PM_MODULE_DIR if it exists if exist "%PM_MODULE_DIR%" ( rd /s /q "%PM_MODULE_DIR%" > nul ) @@ -137,19 +137,6 @@ if %errorlevel% neq 0 ( del "%TARGET%" -:ENSURE_7ZA -set PM_7Za_VERSION=16.02.4 -set PM_7Za_PATH=%PM_PACKAGES_ROOT%\7za\%PM_7ZA_VERSION% -if exist "%PM_7Za_PATH%" goto END -set PM_7Za_PATH=%PM_PACKAGES_ROOT%\chk\7za\%PM_7ZA_VERSION% -if exist "%PM_7Za_PATH%" goto END - -"%PM_PYTHON%" -S -s -u -E "%PM_MODULE%" pull "%PM_MODULE_DIR%\deps.packman.xml" -if %errorlevel% neq 0 ( - echo !!! Error fetching packman dependencies !!! - goto ERROR -) - goto END :ERROR_MKDIR_PACKAGES_ROOT diff --git a/External/Packman/bootstrap/download_file_from_url.ps1 b/External/Packman/bootstrap/download_file_from_url.ps1 index 5a3e2ede..df557456 100644 --- a/External/Packman/bootstrap/download_file_from_url.ps1 +++ b/External/Packman/bootstrap/download_file_from_url.ps1 @@ -20,8 +20,8 @@ param( ) $filename = $output -$triesLeft = 3 - +$triesLeft = 4 +$delay = 2 do { $triesLeft -= 1 @@ -31,12 +31,23 @@ do Write-Host "Downloading from bootstrap.packman.nvidia.com ..." $wc = New-Object net.webclient $wc.Downloadfile($source, $fileName) - $triesLeft = 0 + exit 0 } catch { Write-Host "Error downloading $source!" Write-Host $_.Exception|format-list -force + if ($triesLeft) + { + Write-Host "Retrying in $delay seconds ..." + Start-Sleep -seconds $delay + } + $delay = $delay * $delay } } while ($triesLeft -gt 0) - +# We only get here if the retries have been exhausted, remove any left-overs: +if (Test-Path $fileName) +{ + Remove-Item $fileName +} +exit 1 \ No newline at end of file diff --git a/External/Packman/bootstrap/fetch_file_from_packman_bootstrap.cmd b/External/Packman/bootstrap/fetch_file_from_packman_bootstrap.cmd index 4339bdc3..bf3a88d5 100644 --- a/External/Packman/bootstrap/fetch_file_from_packman_bootstrap.cmd +++ b/External/Packman/bootstrap/fetch_file_from_packman_bootstrap.cmd @@ -22,9 +22,8 @@ @powershell -ExecutionPolicy ByPass -NoLogo -NoProfile -File "%~dp0download_file_from_url.ps1" ^ -source "http://bootstrap.packman.nvidia.com/%PACKAGE_NAME%" -output %TARGET_PATH% :: A bug in powershell prevents the errorlevel code from being set when using the -File execution option -:: We must therefore do our own failure analysis, basically make sure the file exists and is larger than 0 bytes: +:: We must therefore do our own failure analysis, basically make sure the file exists: @if not exist %TARGET_PATH% goto ERROR_DOWNLOAD_FAILED -@if %~z2==0 goto ERROR_DOWNLOAD_FAILED @endlocal @exit /b 0 diff --git a/External/Packman/bootstrap/install_package.py b/External/Packman/bootstrap/install_package.py index 5b56a705..b8ae7f64 100644 --- a/External/Packman/bootstrap/install_package.py +++ b/External/Packman/bootstrap/install_package.py @@ -16,42 +16,139 @@ import zipfile import tempfile import sys -import shutil +import os +import stat +import time +from typing import Any, Callable + + +RENAME_RETRY_COUNT = 100 +RENAME_RETRY_DELAY = 0.1 -__author__ = "hfannar" logging.basicConfig(level=logging.WARNING, format="%(message)s") logger = logging.getLogger("install_package") -class TemporaryDirectory: - def __init__(self): - self.path = None +def remove_directory_item(path): + if os.path.islink(path) or os.path.isfile(path): + try: + os.remove(path) + except PermissionError: + # make sure we have access and try again: + os.chmod(path, stat.S_IRWXU) + os.remove(path) + else: + # try first to delete the dir because this will work for folder junctions, otherwise we would follow the junctions and cause destruction! + clean_out_folder = False + try: + # make sure we have access preemptively - this is necessary because recursing into a directory without permissions + # will only lead to heart ache + os.chmod(path, stat.S_IRWXU) + os.rmdir(path) + except OSError: + clean_out_folder = True + + if clean_out_folder: + # we should make sure the directory is empty + names = os.listdir(path) + for name in names: + fullname = os.path.join(path, name) + remove_directory_item(fullname) + # now try to again get rid of the folder - and not catch if it raises: + os.rmdir(path) + + +class StagingDirectory: + def __init__(self, staging_path): + self.staging_path = staging_path + self.temp_folder_path = None + os.makedirs(staging_path, exist_ok=True) def __enter__(self): - self.path = tempfile.mkdtemp() - return self.path + self.temp_folder_path = tempfile.mkdtemp(prefix="ver-", dir=self.staging_path) + return self + + def get_temp_folder_path(self): + return self.temp_folder_path + + # this function renames the temp staging folder to folder_name, it is required that the parent path exists! + def promote_and_rename(self, folder_name): + abs_dst_folder_name = os.path.join(self.staging_path, folder_name) + os.rename(self.temp_folder_path, abs_dst_folder_name) def __exit__(self, type, value, traceback): - # Remove temporary data created - shutil.rmtree(self.path) + # Remove temp staging folder if it's still there (something went wrong): + path = self.temp_folder_path + if os.path.isdir(path): + remove_directory_item(path) -def install_package(package_src_path, package_dst_path): - with zipfile.ZipFile( - package_src_path, allowZip64=True - ) as zip_file, TemporaryDirectory() as temp_dir: - zip_file.extractall(temp_dir) - # Recursively copy (temp_dir will be automatically cleaned up on exit) - try: - # Recursive copy is needed because both package name and version folder could be missing in - # target directory: - shutil.copytree(temp_dir, package_dst_path) - except OSError as exc: +def rename_folder(staging_dir: StagingDirectory, folder_name: str): + try: + staging_dir.promote_and_rename(folder_name) + except OSError as exc: + # if we failed to rename because the folder now exists we can assume that another packman process + # has managed to update the package before us - in all other cases we re-raise the exception + abs_dst_folder_name = os.path.join(staging_dir.staging_path, folder_name) + if os.path.exists(abs_dst_folder_name): logger.warning( - "Directory %s already present, packaged installation aborted" % package_dst_path + f"Directory {abs_dst_folder_name} already present, package installation already completed" ) else: - logger.info("Package successfully installed to %s" % package_dst_path) + raise + + +def call_with_retry( + op_name: str, func: Callable, retry_count: int = 3, retry_delay: float = 20 +) -> Any: + retries_left = retry_count + while True: + try: + return func() + except (OSError, IOError) as exc: + logger.warning(f"Failure while executing {op_name} [{str(exc)}]") + if retries_left: + retry_str = "retry" if retries_left == 1 else "retries" + logger.warning( + f"Retrying after {retry_delay} seconds" + f" ({retries_left} {retry_str} left) ..." + ) + time.sleep(retry_delay) + else: + logger.error("Maximum retries exceeded, giving up") + raise + retries_left -= 1 + + +def rename_folder_with_retry(staging_dir: StagingDirectory, folder_name): + dst_path = os.path.join(staging_dir.staging_path, folder_name) + call_with_retry( + f"rename {staging_dir.get_temp_folder_path()} -> {dst_path}", + lambda: rename_folder(staging_dir, folder_name), + RENAME_RETRY_COUNT, + RENAME_RETRY_DELAY, + ) + + +def install_package(package_path, install_path): + staging_path, version = os.path.split(install_path) + with StagingDirectory(staging_path) as staging_dir: + output_folder = staging_dir.get_temp_folder_path() + with zipfile.ZipFile(package_path, allowZip64=True) as zip_file: + zip_file.extractall(output_folder) + + # attempt the rename operation + rename_folder_with_retry(staging_dir, version) + + print(f"Package successfully installed to {install_path}") -install_package(sys.argv[1], sys.argv[2]) +if __name__ == "__main__": + executable_paths = os.getenv("PATH") + paths_list = executable_paths.split(os.path.pathsep) if executable_paths else [] + target_path_np = os.path.normpath(sys.argv[2]) + target_path_np_nc = os.path.normcase(target_path_np) + for exec_path in paths_list: + if os.path.normcase(os.path.normpath(exec_path)) == target_path_np_nc: + raise RuntimeError(f"packman will not install to executable path '{exec_path}'") + install_package(sys.argv[1], target_path_np) diff --git a/External/Packman/packman.cmd b/External/Packman/packman.cmd index c7bf3dcf..b1be5d8b 100644 --- a/External/Packman/packman.cmd +++ b/External/Packman/packman.cmd @@ -1,4 +1,6 @@ -:: Reset errorlevel status (don't inherit from caller) [xxxxxxxxxxx] +:: RUN_PM_MODULE must always be at the same spot for packman update to work (batch reloads file during update!) +:: [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] +:: Reset errorlevel status (don't inherit from caller) @call :ECHO_AND_RESET_ERROR :: You can remove the call below if you do your own manual configuration of the dev machines call "%~dp0\bootstrap\configure.bat" @@ -8,16 +10,12 @@ if %errorlevel% neq 0 ( exit /b %errorlevel% ) if not defined PM_PYTHON goto :PYTHON_ENV_ERROR if not defined PM_MODULE goto :MODULE_ENV_ERROR -:: Generate temporary path for variable file -for /f "delims=" %%a in ('powershell -ExecutionPolicy ByPass -NoLogo -NoProfile ^ --File "%~dp0bootstrap\generate_temp_file_name.ps1"') do set PM_VAR_PATH=%%a +set PM_VAR_PATH_ARG= -if %1.==. ( - set PM_VAR_PATH_ARG= -) else ( - set PM_VAR_PATH_ARG=--var-path="%PM_VAR_PATH%" -) +if "%1"=="pull" goto :SET_VAR_PATH +if "%1"=="install" goto :SET_VAR_PATH +:RUN_PM_MODULE "%PM_PYTHON%" -S -s -u -E "%PM_MODULE%" %* %PM_VAR_PATH_ARG% if %errorlevel% neq 0 ( exit /b %errorlevel% ) @@ -54,3 +52,9 @@ if /I "%PM_VERBOSITY%"=="debug" ( @echo on ) exit /b 0 + +:SET_VAR_PATH +:: Generate temporary path for variable file +for /f "delims=" %%a in ('%PM_PYTHON% -S -s -u -E -c "import tempfile;file = tempfile.NamedTemporaryFile(mode='w+t', delete=False);print(file.name)"') do (set PM_VAR_PATH=%%a) +set PM_VAR_PATH_ARG=--var-path="%PM_VAR_PATH%" +goto :RUN_PM_MODULE diff --git a/External/Packman/packman b/External/Packman/packman.sh similarity index 72% rename from External/Packman/packman rename to External/Packman/packman.sh index ad2c6d9d..6a64b6bb 100644 --- a/External/Packman/packman +++ b/External/Packman/packman.sh @@ -1,6 +1,6 @@ #!/bin/bash -# Copyright 2019-2020 NVIDIA CORPORATION +# Copyright 2019-2023 NVIDIA CORPORATION # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -22,7 +22,7 @@ else PM_CURL_SILENT="-s -S" PM_WGET_QUIET="--quiet" fi -PM_PACKMAN_VERSION=6.35 +PM_PACKMAN_VERSION=7.2.1 # This is necessary for newer macOS if [ `uname` == 'Darwin' ]; then @@ -30,33 +30,32 @@ if [ `uname` == 'Darwin' ]; then export LANG=en_US.UTF-8 fi -# Specify where packman command exists -export PM_INSTALL_PATH="$(realpath "$(dirname "${BASH_SOURCE}")")" - -add_packages_root_to_file() -{ - FILE_PATH=$1 - if [ -f "$FILE_PATH" ]; then - if ! grep -Fq "PM_PACKAGES_ROOT" $FILE_PATH ; then - echo "Adjusting $FILE_PATH" - echo -e "export PM_PACKAGES_ROOT=\$HOME/packman-repo\n" >> $FILE_PATH - fi - fi +# We cannot rely on realpath, it isn't installed on macOS and some Linux distros +get_abs_filename() { + echo "$(cd "$(dirname "$1")" && pwd)/$(basename "$1")" } +# Specify where packman command exists +export PM_INSTALL_PATH="$(get_abs_filename "$(dirname "${BASH_SOURCE}")")" + # The packages root may already be configured by the user if [ -z "${PM_PACKAGES_ROOT:-}" ]; then - # Set variable permanently using .profile for this user (if exists) - add_packages_root_to_file ~/.profile - add_packages_root_to_file ~/.bashrc # Set variable temporarily in this process so that the following execution will work - export PM_PACKAGES_ROOT="${HOME}/packman-repo" + if [ `uname` == 'Darwin' ]; then + export PM_PACKAGES_ROOT="${HOME}/Library/Application Support/packman-cache" + else + if [ -z "${XDG_CACHE_HOME:-}" ]; then + export PM_PACKAGES_ROOT="${HOME}/.cache/packman" + else + export PM_PACKAGES_ROOT="${XDG_CACHE_HOME}/packman" + fi + fi fi # Ensure the packages root path exists: if [ ! -d "$PM_PACKAGES_ROOT" ]; then - echo "Creating packman packages repository at $PM_PACKAGES_ROOT" - mkdir -p "$PM_PACKAGES_ROOT" + echo "Creating packman packages cache at $PM_PACKAGES_ROOT" + mkdir -p -m a+rwx "$PM_PACKAGES_ROOT" fi fetch_file_from_s3() @@ -72,18 +71,28 @@ fetch_file_from_s3() fi } +generate_temp_file_name() +{ + if [ `uname` == "Darwin" ]; then + local tmpfile=`mktemp -t packman` + else + local tmpfile=`mktemp -t packman.XXXXXXXX` + fi + echo "$tmpfile" +} + install_python() { PLATFORM=`uname` PROCESSOR=`uname -m` - PYTHON_VERSION=3.7.9 + PYTHON_VERSION=3.10.5-1 if [ $PLATFORM == 'Darwin' ]; then PYTHON_PACKAGE=$PYTHON_VERSION-macos-x86_64 elif [ $PLATFORM == 'Linux' ] && [ $PROCESSOR == 'x86_64' ]; then PYTHON_PACKAGE=$PYTHON_VERSION-linux-x86_64 elif [ $PLATFORM == 'Linux' ] && [ $PROCESSOR == 'aarch64' ]; then - PYTHON_PACKAGE=$PYTHON_VERSION-177-linux-aarch64 + PYTHON_PACKAGE=$PYTHON_VERSION-linux-aarch64 else echo "Operating system not supported" exit 1 @@ -97,10 +106,12 @@ install_python() export PM_PYTHON="$PYTHON_INSTALL_FOLDER/python" if [ ! -f "$PM_PYTHON" ]; then - fetch_file_from_s3 "python@$PYTHON_PACKAGE.tar.gz" "/tmp/python@$PYTHON_PACKAGE.tar.gz" + PYTHON_PACKAGE_TMP=$(generate_temp_file_name) + fetch_file_from_s3 "python@$PYTHON_PACKAGE.tar.gz" "$PYTHON_PACKAGE_TMP" if [ "$?" -eq "0" ]; then echo "Unpacking python" - tar -xf "/tmp/python@$PYTHON_PACKAGE.tar.gz" -C "$PYTHON_INSTALL_FOLDER" + tar -xf "$PYTHON_PACKAGE_TMP" -C "$PYTHON_INSTALL_FOLDER" + rm "$PYTHON_PACKAGE_TMP" else echo "Failed downloading the Python interpreter" exit $? @@ -121,7 +132,7 @@ if [ -z "${PM_MODULE_DIR_EXT:-}" ]; then else PM_MODULE_DIR="$PM_MODULE_DIR_EXT" fi -export PM_MODULE="$PM_MODULE_DIR/packman.py" +export PM_MODULE="$PM_MODULE_DIR/run.py" # Ensure the packman package exists: if [ ! -f "$PM_MODULE" ]; then @@ -130,33 +141,19 @@ if [ ! -f "$PM_MODULE" ]; then rm -rf "$PM_MODULE_DIR" fi PM_MODULE_PACKAGE="packman-common@$PM_PACKMAN_VERSION.zip" - TARGET="/tmp/$PM_MODULE_PACKAGE" + TARGET=$(generate_temp_file_name) # We always fetch packman from S3: - fetch_file_from_s3 $PM_MODULE_PACKAGE $TARGET + fetch_file_from_s3 "$PM_MODULE_PACKAGE" "$TARGET" if [ "$?" -eq "0" ]; then echo "Unpacking ..." "$PM_PYTHON" -S -s -u -E "$PM_INSTALL_PATH/bootstrap/install_package.py" "$TARGET" "$PM_MODULE_DIR" - rm $TARGET + rm "$TARGET" else echo "Failure while fetching packman module from S3!" exit 1 fi fi -# Ensure 7za package exists: -PM_7za_VERSION=16.02.4 -export PM_7za_PATH="$PM_PACKAGES_ROOT/7za/$PM_7za_VERSION" -if [ ! -d "$PM_7za_PATH" ]; then - export PM_7za_PATH="$PM_PACKAGES_ROOT/chk/7za/$PM_7za_VERSION" - if [ ! -d "$PM_7za_PATH" ]; then - "$PM_PYTHON" -S -s -u -E "$PM_MODULE" pull "$PM_MODULE_DIR/deps.packman.xml" - if [ "$?" -ne 0 ]; then - echo "Failure while installing required 7za package" - exit 1 - fi - fi -fi - # Generate temporary file name for environment variables: PM_VAR_PATH=`mktemp -u -t tmp.$$.pmvars.XXXXXX` @@ -178,11 +175,6 @@ if [ -f "$PM_VAR_PATH" ]; then rm -f "$PM_VAR_PATH" fi -# Return the exit code from python -if [ "$exit_code" != 0 ]; then - exit "$exit_code" -fi - # avoid leaking -e and -u into the host script if they weren't originally set if [[ ! ( "$SAVED_SETTINGS" =~ e ) ]]; then set +e @@ -192,3 +184,7 @@ if [[ ! ( "$SAVED_SETTINGS" =~ u ) ]]; then set +u fi +# Return the exit code from python +if [ "$exit_code" != 0 ]; then + exit "$exit_code" +fi diff --git a/External/vulkan b/External/vulkan index b75e5a02..2bb0a231 160000 --- a/External/vulkan +++ b/External/vulkan @@ -1 +1 @@ -Subproject commit b75e5a02b6933caf86c5822d019067b335492c85 +Subproject commit 2bb0a23104ceffd9a28d5b7401f2cee7dae35bb8 diff --git a/Include/Extensions/NRIDeviceCreation.h b/Include/Extensions/NRIDeviceCreation.h index a0ef30f2..ba5c48e5 100644 --- a/Include/Extensions/NRIDeviceCreation.h +++ b/Include/Extensions/NRIDeviceCreation.h @@ -40,7 +40,7 @@ NRI_STRUCT(CallbackInterface) NRI_STRUCT(PhysicalDeviceGroup) { - wchar_t description[128]; + char description[128]; uint64_t luid; uint64_t dedicatedVideoMemory; uint32_t deviceID; diff --git a/Include/Extensions/NRIMeshShader.h b/Include/Extensions/NRIMeshShader.h index 0b191dd3..8d14a90b 100644 --- a/Include/Extensions/NRIMeshShader.h +++ b/Include/Extensions/NRIMeshShader.h @@ -14,7 +14,7 @@ NRI_NAMESPACE_BEGIN NRI_STRUCT(MeshShaderInterface) { - void (NRI_CALL *CmdDispatchMeshTasks)(NRI_REF_NAME(CommandBuffer) commandBuffer, uint32_t taskNum); + void (NRI_CALL *CmdDispatchMeshTasks)(NRI_REF_NAME(CommandBuffer) commandBuffer, uint32_t x, uint32_t y, uint32_t z); }; NRI_NAMESPACE_END diff --git a/Include/Extensions/NRISwapChain.h b/Include/Extensions/NRISwapChain.h index 60bfeb50..e0c8e2b8 100644 --- a/Include/Extensions/NRISwapChain.h +++ b/Include/Extensions/NRISwapChain.h @@ -81,7 +81,7 @@ NRI_STRUCT(SwapChainDesc) { NRI_NAME(WindowSystemType) windowSystemType; NRI_NAME(Window) window; - const NRI_NAME(CommandQueue) * commandQueue; + const NRI_NAME(CommandQueue)* commandQueue; uint16_t width; uint16_t height; uint16_t textureNum; @@ -109,8 +109,8 @@ NRI_STRUCT(SwapChainInterface) void (NRI_CALL *DestroySwapChain)(NRI_REF_NAME(SwapChain) swapChain); void (NRI_CALL *SetSwapChainDebugName)(NRI_REF_NAME(SwapChain) swapChain, const char* name); NRI_NAME(Texture)* const* (NRI_CALL *GetSwapChainTextures)(const NRI_REF_NAME(SwapChain) swapChain, NRI_REF(uint32_t) textureNum, NRI_REF_NAME(Format) format); - uint32_t (NRI_CALL *AcquireNextSwapChainTexture)(NRI_REF_NAME(SwapChain) swapChain, NRI_REF_NAME(QueueSemaphore) textureReadyForRender); - NRI_NAME(Result) (NRI_CALL *SwapChainPresent)(NRI_REF_NAME(SwapChain) swapChain, NRI_REF_NAME(QueueSemaphore) textureReadyForPresent); + uint32_t (NRI_CALL *AcquireNextSwapChainTexture)(NRI_REF_NAME(SwapChain) swapChain); + NRI_NAME(Result) (NRI_CALL *SwapChainPresent)(NRI_REF_NAME(SwapChain) swapChain); NRI_NAME(Result) (NRI_CALL *SetSwapChainHdrMetadata)(NRI_REF_NAME(SwapChain) swapChain, const NRI_REF_NAME(HdrMetadata) hdrMetadata); NRI_NAME(Result) (NRI_CALL *GetDisplays)(NRI_REF_NAME(Device) device, NRI_NAME(Display)** displays, NRI_REF(uint32_t) displayNum); NRI_NAME(Result) (NRI_CALL *GetDisplaySize)(NRI_REF_NAME(Device) device, NRI_REF_NAME(Display) display, NRI_REF(uint16_t) width, NRI_REF(uint16_t) height); diff --git a/Include/Extensions/NRIWrapperVK.h b/Include/Extensions/NRIWrapperVK.h index 31001799..ae22d960 100644 --- a/Include/Extensions/NRIWrapperVK.h +++ b/Include/Extensions/NRIWrapperVK.h @@ -29,8 +29,6 @@ typedef uint64_t NRIVkDeviceMemory; typedef uint64_t NRIVkQueryPool; typedef uint64_t NRIVkPipeline; typedef uint64_t NRIVkDescriptorPool; -typedef uint64_t NRIVkSemaphore; -typedef uint64_t NRIVkFence; typedef uint64_t NRIVkImageView; typedef uint64_t NRIVkBufferView; typedef uint64_t NRIVkAccelerationStructureKHR; @@ -52,14 +50,10 @@ NRI_STRUCT(DeviceCreationVulkanDesc) uint32_t queueFamilyIndexNum; NRI_NAME(CallbackInterface) callbackInterface; NRI_NAME(MemoryAllocatorInterface) memoryAllocatorInterface; + NRI_NAME(SPIRVBindingOffsets) spirvBindingOffsets; + const char* vulkanLoaderPath; bool enableNRIValidation; bool enableAPIValidation; - const char* const* instanceExtensions; - uint32_t instanceExtensionNum; - const char* const* deviceExtensions; - uint32_t deviceExtensionNum; - const char* vulkanLoaderPath; - NRI_NAME(SPIRVBindingOffsets) spirvBindingOffsets; }; NRI_STRUCT(CommandQueueVulkanDesc) @@ -139,8 +133,6 @@ NRI_STRUCT(WrapperVKInterface) NRI_NAME(Result) (NRI_CALL *CreateGraphicsPipelineVK)(NRI_REF_NAME(Device) device, NRIVkPipeline vkPipeline, NRI_REF_NAME(Pipeline*) pipeline); NRI_NAME(Result) (NRI_CALL *CreateComputePipelineVK)(NRI_REF_NAME(Device) device, NRIVkPipeline vkPipeline, NRI_REF_NAME(Pipeline*) pipeline); NRI_NAME(Result) (NRI_CALL *CreateQueryPoolVK)(NRI_REF_NAME(Device) device, const NRI_REF_NAME(QueryPoolVulkanDesc) queryPoolVulkanDesc, NRI_REF_NAME(QueryPool*) queryPool); - NRI_NAME(Result) (NRI_CALL *CreateQueueSemaphoreVK)(NRI_REF_NAME(Device) device, NRIVkSemaphore vkSemaphore, NRI_REF_NAME(QueueSemaphore*) queueSemaphore); - NRI_NAME(Result) (NRI_CALL *CreateDeviceSemaphoreVK)(NRI_REF_NAME(Device) device, NRIVkFence vkFence, NRI_REF_NAME(DeviceSemaphore*) deviceSemaphore); NRI_NAME(Result) (NRI_CALL *CreateAccelerationStructureVK)(NRI_REF_NAME(Device) device, const NRI_REF_NAME(AccelerationStructureVulkanDesc) accelerationStructureDesc, NRI_REF_NAME(AccelerationStructure*) accelerationStructure); NRIVkPhysicalDevice (NRI_CALL *GetVkPhysicalDevice)(const NRI_REF_NAME(Device) device); diff --git a/Include/Extensions/Optional/NRIStreamlineWrapper.h b/Include/Extensions/Optional/NRIStreamlineWrapper.h new file mode 100644 index 00000000..9924b9de --- /dev/null +++ b/Include/Extensions/Optional/NRIStreamlineWrapper.h @@ -0,0 +1,37 @@ +/* +Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. + +NVIDIA CORPORATION and its licensors retain all intellectual property +and proprietary rights in and to this software, related documentation +and any modifications thereto. Any use, reproduction, disclosure or +distribution of this software and related documentation without an express +license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#pragma once + +#include +#include + +#ifndef UINT_MAX +#define UNDEF_UINT_MAX +#define UINT_MAX 0xFFFFFFFF +#endif + +#include + +#ifdef UNDEF_UINT_MAX +#undef UINT_MAX +#endif + +namespace slwrap +{ + NRI_API bool NRI_CALL setConstants(const ::sl::Constants& values, uint32_t frameIndex, uint32_t id); + NRI_API bool NRI_CALL shutdown(); + NRI_API bool NRI_CALL init(const sl::Preferences& pref, int applicationId = 0); + NRI_API bool NRI_CALL setTag(const sl::Resource* resource, sl::BufferType tag, uint32_t id, const sl::Extent* extent); + NRI_API bool NRI_CALL isFeatureSupported(sl::Feature feature, uint32_t* adapterBitMask = nullptr); + NRI_API bool NRI_CALL setFeatureConstants(sl::Feature feature, const void* consts, uint32_t frameIndex, uint32_t id); + NRI_API bool NRI_CALL evaluateFeature(sl::CommandBuffer* cmdBuffer, sl::Feature feature, uint32_t frameIndex, uint32_t id); + NRI_API bool NRI_CALL getFeatureSettings(sl::Feature feature, const void* consts, void* settings); +} diff --git a/Include/NRI.h b/Include/NRI.h index aa1d5323..5b73a542 100644 --- a/Include/NRI.h +++ b/Include/NRI.h @@ -14,8 +14,8 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include #define NRI_VERSION_MAJOR 1 -#define NRI_VERSION_MINOR 91 -#define NRI_VERSION_DATE "19 January 2023" +#define NRI_VERSION_MINOR 93 +#define NRI_VERSION_DATE "14 March 2023" #define NRI_INTERFACE( name ) #name, sizeof(name) #ifdef _WIN32 @@ -66,6 +66,7 @@ NRI_STRUCT(CoreInterface) // Create NRI_NAME(Result) (NRI_CALL *CreateCommandAllocator)(const NRI_REF_NAME(CommandQueue) commandQueue, uint32_t physicalDeviceMask, NRI_REF_NAME(CommandAllocator*) commandAllocator); + NRI_NAME(Result) (NRI_CALL *CreateCommandBuffer)(NRI_REF_NAME(CommandAllocator) commandAllocator, NRI_REF_NAME(CommandBuffer*) commandBuffer); NRI_NAME(Result) (NRI_CALL *CreateDescriptorPool)(NRI_REF_NAME(Device) device, const NRI_REF_NAME(DescriptorPoolDesc) descriptorPoolDesc, NRI_REF_NAME(DescriptorPool*) descriptorPool); NRI_NAME(Result) (NRI_CALL *CreateBuffer)(NRI_REF_NAME(Device) device, const NRI_REF_NAME(BufferDesc) bufferDesc, NRI_REF_NAME(Buffer*) buffer); NRI_NAME(Result) (NRI_CALL *CreateTexture)(NRI_REF_NAME(Device) device, const NRI_REF_NAME(TextureDesc) textureDesc, NRI_REF_NAME(Texture*) texture); @@ -79,12 +80,11 @@ NRI_STRUCT(CoreInterface) NRI_NAME(Result) (NRI_CALL *CreateComputePipeline)(NRI_REF_NAME(Device) device, const NRI_REF_NAME(ComputePipelineDesc) computePipelineDesc, NRI_REF_NAME(Pipeline*) pipeline); NRI_NAME(Result) (NRI_CALL *CreateFrameBuffer)(NRI_REF_NAME(Device) device, const NRI_REF_NAME(FrameBufferDesc) frameBufferDesc, NRI_REF_NAME(FrameBuffer*) frameBuffer); NRI_NAME(Result) (NRI_CALL *CreateQueryPool)(NRI_REF_NAME(Device) device, const NRI_REF_NAME(QueryPoolDesc) queryPoolDesc, NRI_REF_NAME(QueryPool*) queryPool); - NRI_NAME(Result) (NRI_CALL *CreateQueueSemaphore)(NRI_REF_NAME(Device) device, NRI_REF_NAME(QueueSemaphore*) queueSemaphore); - NRI_NAME(Result) (NRI_CALL *CreateDeviceSemaphore)(NRI_REF_NAME(Device) device, bool signaled, NRI_REF_NAME(DeviceSemaphore*) deviceSemaphore); - NRI_NAME(Result) (NRI_CALL *CreateCommandBuffer)(NRI_REF_NAME(CommandAllocator) commandAllocator, NRI_REF_NAME(CommandBuffer*) commandBuffer); + NRI_NAME(Result) (NRI_CALL *CreateFence)(NRI_REF_NAME(Device) device, uint64_t initialValue, NRI_REF_NAME(Fence*) fence); // Destroy void (NRI_CALL *DestroyCommandAllocator)(NRI_REF_NAME(CommandAllocator) commandAllocator); + void (NRI_CALL *DestroyCommandBuffer)(NRI_REF_NAME(CommandBuffer) commandBuffer); void (NRI_CALL *DestroyDescriptorPool)(NRI_REF_NAME(DescriptorPool) descriptorPool); void (NRI_CALL *DestroyBuffer)(NRI_REF_NAME(Buffer) buffer); void (NRI_CALL *DestroyTexture)(NRI_REF_NAME(Texture) texture); @@ -93,9 +93,7 @@ NRI_STRUCT(CoreInterface) void (NRI_CALL *DestroyPipeline)(NRI_REF_NAME(Pipeline) pipeline); void (NRI_CALL *DestroyFrameBuffer)(NRI_REF_NAME(FrameBuffer) frameBuffer); void (NRI_CALL *DestroyQueryPool)(NRI_REF_NAME(QueryPool) queryPool); - void (NRI_CALL *DestroyQueueSemaphore)(NRI_REF_NAME(QueueSemaphore) queueSemaphore); - void (NRI_CALL *DestroyDeviceSemaphore)(NRI_REF_NAME(DeviceSemaphore) deviceSemaphore); - void (NRI_CALL *DestroyCommandBuffer)(NRI_REF_NAME(CommandBuffer) commandBuffer); + void (NRI_CALL *DestroyFence)(NRI_REF_NAME(Fence) fence); // Memory NRI_NAME(Result) (NRI_CALL *AllocateMemory)(NRI_REF_NAME(Device) device, uint32_t physicalDeviceMask, NRI_NAME(MemoryType) memoryType, uint64_t size, NRI_REF_NAME(Memory*) memory); @@ -142,12 +140,17 @@ NRI_STRUCT(CoreInterface) void (NRI_CALL *CmdCopyTexture)(NRI_REF_NAME(CommandBuffer) commandBuffer, NRI_REF_NAME(Texture) dstTexture, uint32_t dstPhysicalDeviceIndex, const NRI_NAME(TextureRegionDesc)* dstRegionDesc, const NRI_REF_NAME(Texture) srcTexture, uint32_t srcPhysicalDeviceIndex, const NRI_NAME(TextureRegionDesc)* srcRegionDesc); void (NRI_CALL *CmdUploadBufferToTexture)(NRI_REF_NAME(CommandBuffer) commandBuffer, NRI_REF_NAME(Texture) dstTexture, const NRI_REF_NAME(TextureRegionDesc) dstRegionDesc, const NRI_REF_NAME(Buffer) srcBuffer, const NRI_REF_NAME(TextureDataLayoutDesc) srcDataLayoutDesc); void (NRI_CALL *CmdReadbackTextureToBuffer)(NRI_REF_NAME(CommandBuffer) commandBuffer, NRI_REF_NAME(Buffer) dstBuffer, NRI_REF_NAME(TextureDataLayoutDesc) dstDataLayoutDesc, const NRI_REF_NAME(Texture) srcTexture, const NRI_REF_NAME(TextureRegionDesc) srcRegionDesc); - void (NRI_CALL* CmdCopyQueries)(NRI_REF_NAME(CommandBuffer) commandBuffer, const NRI_REF_NAME(QueryPool) queryPool, uint32_t offset, uint32_t num, NRI_REF_NAME(Buffer) dstBuffer, uint64_t dstOffset); - void (NRI_CALL* CmdResetQueries)(NRI_REF_NAME(CommandBuffer) commandBuffer, const NRI_REF_NAME(QueryPool) queryPool, uint32_t offset, uint32_t num); + void (NRI_CALL *CmdCopyQueries)(NRI_REF_NAME(CommandBuffer) commandBuffer, const NRI_REF_NAME(QueryPool) queryPool, uint32_t offset, uint32_t num, NRI_REF_NAME(Buffer) dstBuffer, uint64_t dstOffset); + void (NRI_CALL *CmdResetQueries)(NRI_REF_NAME(CommandBuffer) commandBuffer, const NRI_REF_NAME(QueryPool) queryPool, uint32_t offset, uint32_t num); + + // Synchronization + uint64_t (NRI_CALL *GetFenceValue)(NRI_REF_NAME(Fence) fence); + void (NRI_CALL *QueueSignal)(NRI_REF_NAME(CommandQueue) commandQueue, NRI_REF_NAME(Fence) fence, uint64_t value); + void (NRI_CALL *QueueWait)(NRI_REF_NAME(CommandQueue) commandQueue, NRI_REF_NAME(Fence) fence, uint64_t value); + void (NRI_CALL *Wait)(NRI_REF_NAME(Fence) fence, uint64_t value); - // Work submission & idle - void (NRI_CALL *SubmitQueueWork)(NRI_REF_NAME(CommandQueue) commandQueue, const NRI_REF_NAME(WorkSubmissionDesc) workSubmissionDesc, NRI_NAME(DeviceSemaphore)* deviceSemaphore); - void (NRI_CALL *WaitForSemaphore)(NRI_REF_NAME(CommandQueue) commandQueue, NRI_REF_NAME(DeviceSemaphore) deviceSemaphore); + // Work submission + void (NRI_CALL *QueueSubmit)(NRI_REF_NAME(CommandQueue) commandQueue, const NRI_REF_NAME(QueueSubmitDesc) queueSubmitDesc); // Descriptor set void (NRI_CALL *UpdateDescriptorRanges)(NRI_REF_NAME(DescriptorSet) descriptorSet, uint32_t physicalDeviceMask, uint32_t baseRange, uint32_t rangeNum, const NRI_NAME(DescriptorRangeUpdateDesc)* rangeUpdateDescs); @@ -168,8 +171,7 @@ NRI_STRUCT(CoreInterface) // Debug name void (NRI_CALL *SetDeviceDebugName)(NRI_REF_NAME(Device) device, const char* name); void (NRI_CALL *SetCommandQueueDebugName)(NRI_REF_NAME(CommandQueue) commandQueue, const char* name); - void (NRI_CALL *SetDeviceSemaphoreDebugName)(NRI_REF_NAME(DeviceSemaphore) deviceSemaphore, const char* name); - void (NRI_CALL *SetQueueSemaphoreDebugName)(NRI_REF_NAME(QueueSemaphore) queueSemaphore, const char* name); + void (NRI_CALL *SetFenceDebugName)(NRI_REF_NAME(Fence) fence, const char* name); void (NRI_CALL *SetCommandAllocatorDebugName)(NRI_REF_NAME(CommandAllocator) commandAllocator, const char* name); void (NRI_CALL *SetDescriptorPoolDebugName)(NRI_REF_NAME(DescriptorPool) descriptorPool, const char* name); void (NRI_CALL *SetBufferDebugName)(NRI_REF_NAME(Buffer) buffer, const char* name); diff --git a/Include/NRIDescs.h b/Include/NRIDescs.h index f801ab90..00e662ba 100644 --- a/Include/NRIDescs.h +++ b/Include/NRIDescs.h @@ -24,12 +24,11 @@ NRI_FORWARD_STRUCT(Device); NRI_FORWARD_STRUCT(Descriptor); NRI_FORWARD_STRUCT(DescriptorPool); NRI_FORWARD_STRUCT(DescriptorSet); -NRI_FORWARD_STRUCT(DeviceSemaphore); +NRI_FORWARD_STRUCT(Fence); NRI_FORWARD_STRUCT(FrameBuffer); NRI_FORWARD_STRUCT(Memory); NRI_FORWARD_STRUCT(PipelineLayout); NRI_FORWARD_STRUCT(Pipeline); -NRI_FORWARD_STRUCT(QueueSemaphore); NRI_FORWARD_STRUCT(QueryPool); NRI_FORWARD_STRUCT(Texture); @@ -805,14 +804,10 @@ NRI_STRUCT(TextureDataLayoutDesc) uint32_t slicePitch; }; -NRI_STRUCT(WorkSubmissionDesc) +NRI_STRUCT(QueueSubmitDesc) { const NRI_NAME(CommandBuffer)* const* commandBuffers; - const NRI_NAME(QueueSemaphore)* const* wait; - const NRI_NAME(QueueSemaphore)* const* signal; uint32_t commandBufferNum; - uint32_t waitNum; - uint32_t signalNum; uint32_t physicalDeviceIndex; }; @@ -1351,14 +1346,12 @@ NRI_STRUCT(DeviceDesc) uint32_t rayTracingGeometryObjectMaxNum; // Mesh shader - uint32_t meshTaskMaxNum; uint32_t meshTaskWorkGroupInvocationMaxNum; uint32_t meshTaskWorkGroupMaxDim[3]; uint32_t meshTaskTotalMemoryMaxSize; uint32_t meshTaskOutputMaxNum; uint32_t meshWorkGroupInvocationMaxNum; uint32_t meshWorkGroupMaxDim[3]; - uint32_t meshTotalMemoryMaxSize; uint32_t meshOutputVertexMaxNum; uint32_t meshOutputPrimitiveMaxNum; uint32_t meshMultiviewViewMaxNum; diff --git a/Include/NRIMacro.h b/Include/NRIMacro.h index 349620f2..83f32d4a 100644 --- a/Include/NRIMacro.h +++ b/Include/NRIMacro.h @@ -9,7 +9,6 @@ #endif #define NRI_EXPAND(args) args -#define NRI_SHIELD(...) __VA_ARGS__ #if defined(_MSC_VER) #define _NRI_NARGS(_1, _2, _3, _4, _5, _6_, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, _61, _62, _63, _64, _65, _66, _67, _68, _69, _70, _71, _72, _73, VAL, ...) VAL @@ -27,80 +26,80 @@ #define _NRI_MERGE_TOKEN3(_0, _1, _2) _0##_1##_2 #define NRI_MERGE_TOKEN3(_0, _1, _2) _NRI_MERGE_TOKEN3(_0, _1, _2) -#define _NRI_SEQ0(code, separator, ...) -#define _NRI_SEQ1(code, separator, ...) NRI_EXPAND(code(0, __VA_ARGS__)) -#define _NRI_SEQ2(code, separator, ...) _NRI_SEQ1(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(1, __VA_ARGS__)) -#define _NRI_SEQ3(code, separator, ...) _NRI_SEQ2(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(2, __VA_ARGS__)) -#define _NRI_SEQ4(code, separator, ...) _NRI_SEQ3(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(3, __VA_ARGS__)) -#define _NRI_SEQ5(code, separator, ...) _NRI_SEQ4(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(4, __VA_ARGS__)) -#define _NRI_SEQ6(code, separator, ...) _NRI_SEQ5(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(5, __VA_ARGS__)) -#define _NRI_SEQ7(code, separator, ...) _NRI_SEQ6(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(6, __VA_ARGS__)) -#define _NRI_SEQ8(code, separator, ...) _NRI_SEQ7(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(7, __VA_ARGS__)) -#define _NRI_SEQ9(code, separator, ...) _NRI_SEQ8(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(8, __VA_ARGS__)) -#define _NRI_SEQ10(code, separator, ...) _NRI_SEQ9(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(9, __VA_ARGS__)) -#define _NRI_SEQ11(code, separator, ...) _NRI_SEQ10(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(10, __VA_ARGS__)) -#define _NRI_SEQ12(code, separator, ...) _NRI_SEQ11(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(11, __VA_ARGS__)) -#define _NRI_SEQ13(code, separator, ...) _NRI_SEQ12(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(12, __VA_ARGS__)) -#define _NRI_SEQ14(code, separator, ...) _NRI_SEQ13(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(13, __VA_ARGS__)) -#define _NRI_SEQ15(code, separator, ...) _NRI_SEQ14(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(14, __VA_ARGS__)) -#define _NRI_SEQ16(code, separator, ...) _NRI_SEQ15(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(15, __VA_ARGS__)) -#define _NRI_SEQ17(code, separator, ...) _NRI_SEQ16(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(16, __VA_ARGS__)) -#define _NRI_SEQ18(code, separator, ...) _NRI_SEQ17(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(17, __VA_ARGS__)) -#define _NRI_SEQ19(code, separator, ...) _NRI_SEQ18(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(18, __VA_ARGS__)) -#define _NRI_SEQ20(code, separator, ...) _NRI_SEQ19(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(19, __VA_ARGS__)) -#define _NRI_SEQ21(code, separator, ...) _NRI_SEQ20(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(20, __VA_ARGS__)) -#define _NRI_SEQ22(code, separator, ...) _NRI_SEQ21(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(21, __VA_ARGS__)) -#define _NRI_SEQ23(code, separator, ...) _NRI_SEQ22(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(22, __VA_ARGS__)) -#define _NRI_SEQ24(code, separator, ...) _NRI_SEQ23(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(23, __VA_ARGS__)) -#define _NRI_SEQ25(code, separator, ...) _NRI_SEQ24(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(24, __VA_ARGS__)) -#define _NRI_SEQ26(code, separator, ...) _NRI_SEQ25(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(25, __VA_ARGS__)) -#define _NRI_SEQ27(code, separator, ...) _NRI_SEQ26(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(26, __VA_ARGS__)) -#define _NRI_SEQ28(code, separator, ...) _NRI_SEQ27(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(27, __VA_ARGS__)) -#define _NRI_SEQ29(code, separator, ...) _NRI_SEQ28(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(28, __VA_ARGS__)) -#define _NRI_SEQ30(code, separator, ...) _NRI_SEQ29(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(29, __VA_ARGS__)) -#define _NRI_SEQ31(code, separator, ...) _NRI_SEQ30(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(30, __VA_ARGS__)) -#define _NRI_SEQ32(code, separator, ...) _NRI_SEQ31(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(31, __VA_ARGS__)) -#define _NRI_SEQ33(code, separator, ...) _NRI_SEQ32(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(32, __VA_ARGS__)) -#define _NRI_SEQ34(code, separator, ...) _NRI_SEQ33(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(33, __VA_ARGS__)) -#define _NRI_SEQ35(code, separator, ...) _NRI_SEQ34(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(34, __VA_ARGS__)) -#define _NRI_SEQ36(code, separator, ...) _NRI_SEQ35(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(35, __VA_ARGS__)) -#define _NRI_SEQ37(code, separator, ...) _NRI_SEQ36(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(36, __VA_ARGS__)) -#define _NRI_SEQ38(code, separator, ...) _NRI_SEQ37(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(37, __VA_ARGS__)) -#define _NRI_SEQ39(code, separator, ...) _NRI_SEQ38(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(38, __VA_ARGS__)) -#define _NRI_SEQ40(code, separator, ...) _NRI_SEQ39(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(39, __VA_ARGS__)) -#define _NRI_SEQ41(code, separator, ...) _NRI_SEQ40(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(40, __VA_ARGS__)) -#define _NRI_SEQ42(code, separator, ...) _NRI_SEQ41(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(41, __VA_ARGS__)) -#define _NRI_SEQ43(code, separator, ...) _NRI_SEQ42(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(42, __VA_ARGS__)) -#define _NRI_SEQ44(code, separator, ...) _NRI_SEQ43(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(43, __VA_ARGS__)) -#define _NRI_SEQ45(code, separator, ...) _NRI_SEQ44(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(44, __VA_ARGS__)) -#define _NRI_SEQ46(code, separator, ...) _NRI_SEQ45(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(45, __VA_ARGS__)) -#define _NRI_SEQ47(code, separator, ...) _NRI_SEQ46(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(46, __VA_ARGS__)) -#define _NRI_SEQ48(code, separator, ...) _NRI_SEQ47(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(47, __VA_ARGS__)) -#define _NRI_SEQ49(code, separator, ...) _NRI_SEQ48(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(48, __VA_ARGS__)) -#define _NRI_SEQ50(code, separator, ...) _NRI_SEQ49(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(49, __VA_ARGS__)) -#define _NRI_SEQ51(code, separator, ...) _NRI_SEQ50(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(50, __VA_ARGS__)) -#define _NRI_SEQ52(code, separator, ...) _NRI_SEQ51(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(51, __VA_ARGS__)) -#define _NRI_SEQ53(code, separator, ...) _NRI_SEQ52(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(52, __VA_ARGS__)) -#define _NRI_SEQ54(code, separator, ...) _NRI_SEQ53(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(53, __VA_ARGS__)) -#define _NRI_SEQ55(code, separator, ...) _NRI_SEQ54(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(54, __VA_ARGS__)) -#define _NRI_SEQ56(code, separator, ...) _NRI_SEQ55(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(55, __VA_ARGS__)) -#define _NRI_SEQ57(code, separator, ...) _NRI_SEQ56(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(56, __VA_ARGS__)) -#define _NRI_SEQ58(code, separator, ...) _NRI_SEQ57(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(57, __VA_ARGS__)) -#define _NRI_SEQ59(code, separator, ...) _NRI_SEQ58(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(58, __VA_ARGS__)) -#define _NRI_SEQ60(code, separator, ...) _NRI_SEQ59(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(59, __VA_ARGS__)) -#define _NRI_SEQ61(code, separator, ...) _NRI_SEQ60(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(60, __VA_ARGS__)) -#define _NRI_SEQ62(code, separator, ...) _NRI_SEQ61(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(61, __VA_ARGS__)) -#define _NRI_SEQ63(code, separator, ...) _NRI_SEQ62(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(62, __VA_ARGS__)) -#define _NRI_SEQ64(code, separator, ...) _NRI_SEQ63(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(63, __VA_ARGS__)) -#define _NRI_SEQ65(code, separator, ...) _NRI_SEQ64(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(64, __VA_ARGS__)) -#define _NRI_SEQ66(code, separator, ...) _NRI_SEQ65(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(65, __VA_ARGS__)) -#define _NRI_SEQ67(code, separator, ...) _NRI_SEQ66(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(66, __VA_ARGS__)) -#define _NRI_SEQ68(code, separator, ...) _NRI_SEQ67(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(67, __VA_ARGS__)) -#define _NRI_SEQ69(code, separator, ...) _NRI_SEQ68(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(68, __VA_ARGS__)) -#define _NRI_SEQ70(code, separator, ...) _NRI_SEQ69(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(69, __VA_ARGS__)) -#define _NRI_SEQ71(code, separator, ...) _NRI_SEQ70(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(70, __VA_ARGS__)) -#define _NRI_SEQ72(code, separator, ...) _NRI_SEQ71(code, separator, __VA_ARGS__) separator NRI_EXPAND(code(71, __VA_ARGS__)) -#define NRI_SEQN(code, separator, count, ...) NRI_MERGE_TOKEN2(_NRI_SEQ, count)(code, separator, __VA_ARGS__) +#define _NRI_SEQ0(code, ...) +#define _NRI_SEQ1(code, ...) NRI_EXPAND(code(0, __VA_ARGS__)) +#define _NRI_SEQ2(code, ...) _NRI_SEQ1(code, __VA_ARGS__), NRI_EXPAND(code(1, __VA_ARGS__)) +#define _NRI_SEQ3(code, ...) _NRI_SEQ2(code, __VA_ARGS__), NRI_EXPAND(code(2, __VA_ARGS__)) +#define _NRI_SEQ4(code, ...) _NRI_SEQ3(code, __VA_ARGS__), NRI_EXPAND(code(3, __VA_ARGS__)) +#define _NRI_SEQ5(code, ...) _NRI_SEQ4(code, __VA_ARGS__), NRI_EXPAND(code(4, __VA_ARGS__)) +#define _NRI_SEQ6(code, ...) _NRI_SEQ5(code, __VA_ARGS__), NRI_EXPAND(code(5, __VA_ARGS__)) +#define _NRI_SEQ7(code, ...) _NRI_SEQ6(code, __VA_ARGS__), NRI_EXPAND(code(6, __VA_ARGS__)) +#define _NRI_SEQ8(code, ...) _NRI_SEQ7(code, __VA_ARGS__), NRI_EXPAND(code(7, __VA_ARGS__)) +#define _NRI_SEQ9(code, ...) _NRI_SEQ8(code, __VA_ARGS__), NRI_EXPAND(code(8, __VA_ARGS__)) +#define _NRI_SEQ10(code, ...) _NRI_SEQ9(code, __VA_ARGS__), NRI_EXPAND(code(9, __VA_ARGS__)) +#define _NRI_SEQ11(code, ...) _NRI_SEQ10(code, __VA_ARGS__), NRI_EXPAND(code(10, __VA_ARGS__)) +#define _NRI_SEQ12(code, ...) _NRI_SEQ11(code, __VA_ARGS__), NRI_EXPAND(code(11, __VA_ARGS__)) +#define _NRI_SEQ13(code, ...) _NRI_SEQ12(code, __VA_ARGS__), NRI_EXPAND(code(12, __VA_ARGS__)) +#define _NRI_SEQ14(code, ...) _NRI_SEQ13(code, __VA_ARGS__), NRI_EXPAND(code(13, __VA_ARGS__)) +#define _NRI_SEQ15(code, ...) _NRI_SEQ14(code, __VA_ARGS__), NRI_EXPAND(code(14, __VA_ARGS__)) +#define _NRI_SEQ16(code, ...) _NRI_SEQ15(code, __VA_ARGS__), NRI_EXPAND(code(15, __VA_ARGS__)) +#define _NRI_SEQ17(code, ...) _NRI_SEQ16(code, __VA_ARGS__), NRI_EXPAND(code(16, __VA_ARGS__)) +#define _NRI_SEQ18(code, ...) _NRI_SEQ17(code, __VA_ARGS__), NRI_EXPAND(code(17, __VA_ARGS__)) +#define _NRI_SEQ19(code, ...) _NRI_SEQ18(code, __VA_ARGS__), NRI_EXPAND(code(18, __VA_ARGS__)) +#define _NRI_SEQ20(code, ...) _NRI_SEQ19(code, __VA_ARGS__), NRI_EXPAND(code(19, __VA_ARGS__)) +#define _NRI_SEQ21(code, ...) _NRI_SEQ20(code, __VA_ARGS__), NRI_EXPAND(code(20, __VA_ARGS__)) +#define _NRI_SEQ22(code, ...) _NRI_SEQ21(code, __VA_ARGS__), NRI_EXPAND(code(21, __VA_ARGS__)) +#define _NRI_SEQ23(code, ...) _NRI_SEQ22(code, __VA_ARGS__), NRI_EXPAND(code(22, __VA_ARGS__)) +#define _NRI_SEQ24(code, ...) _NRI_SEQ23(code, __VA_ARGS__), NRI_EXPAND(code(23, __VA_ARGS__)) +#define _NRI_SEQ25(code, ...) _NRI_SEQ24(code, __VA_ARGS__), NRI_EXPAND(code(24, __VA_ARGS__)) +#define _NRI_SEQ26(code, ...) _NRI_SEQ25(code, __VA_ARGS__), NRI_EXPAND(code(25, __VA_ARGS__)) +#define _NRI_SEQ27(code, ...) _NRI_SEQ26(code, __VA_ARGS__), NRI_EXPAND(code(26, __VA_ARGS__)) +#define _NRI_SEQ28(code, ...) _NRI_SEQ27(code, __VA_ARGS__), NRI_EXPAND(code(27, __VA_ARGS__)) +#define _NRI_SEQ29(code, ...) _NRI_SEQ28(code, __VA_ARGS__), NRI_EXPAND(code(28, __VA_ARGS__)) +#define _NRI_SEQ30(code, ...) _NRI_SEQ29(code, __VA_ARGS__), NRI_EXPAND(code(29, __VA_ARGS__)) +#define _NRI_SEQ31(code, ...) _NRI_SEQ30(code, __VA_ARGS__), NRI_EXPAND(code(30, __VA_ARGS__)) +#define _NRI_SEQ32(code, ...) _NRI_SEQ31(code, __VA_ARGS__), NRI_EXPAND(code(31, __VA_ARGS__)) +#define _NRI_SEQ33(code, ...) _NRI_SEQ32(code, __VA_ARGS__), NRI_EXPAND(code(32, __VA_ARGS__)) +#define _NRI_SEQ34(code, ...) _NRI_SEQ33(code, __VA_ARGS__), NRI_EXPAND(code(33, __VA_ARGS__)) +#define _NRI_SEQ35(code, ...) _NRI_SEQ34(code, __VA_ARGS__), NRI_EXPAND(code(34, __VA_ARGS__)) +#define _NRI_SEQ36(code, ...) _NRI_SEQ35(code, __VA_ARGS__), NRI_EXPAND(code(35, __VA_ARGS__)) +#define _NRI_SEQ37(code, ...) _NRI_SEQ36(code, __VA_ARGS__), NRI_EXPAND(code(36, __VA_ARGS__)) +#define _NRI_SEQ38(code, ...) _NRI_SEQ37(code, __VA_ARGS__), NRI_EXPAND(code(37, __VA_ARGS__)) +#define _NRI_SEQ39(code, ...) _NRI_SEQ38(code, __VA_ARGS__), NRI_EXPAND(code(38, __VA_ARGS__)) +#define _NRI_SEQ40(code, ...) _NRI_SEQ39(code, __VA_ARGS__), NRI_EXPAND(code(39, __VA_ARGS__)) +#define _NRI_SEQ41(code, ...) _NRI_SEQ40(code, __VA_ARGS__), NRI_EXPAND(code(40, __VA_ARGS__)) +#define _NRI_SEQ42(code, ...) _NRI_SEQ41(code, __VA_ARGS__), NRI_EXPAND(code(41, __VA_ARGS__)) +#define _NRI_SEQ43(code, ...) _NRI_SEQ42(code, __VA_ARGS__), NRI_EXPAND(code(42, __VA_ARGS__)) +#define _NRI_SEQ44(code, ...) _NRI_SEQ43(code, __VA_ARGS__), NRI_EXPAND(code(43, __VA_ARGS__)) +#define _NRI_SEQ45(code, ...) _NRI_SEQ44(code, __VA_ARGS__), NRI_EXPAND(code(44, __VA_ARGS__)) +#define _NRI_SEQ46(code, ...) _NRI_SEQ45(code, __VA_ARGS__), NRI_EXPAND(code(45, __VA_ARGS__)) +#define _NRI_SEQ47(code, ...) _NRI_SEQ46(code, __VA_ARGS__), NRI_EXPAND(code(46, __VA_ARGS__)) +#define _NRI_SEQ48(code, ...) _NRI_SEQ47(code, __VA_ARGS__), NRI_EXPAND(code(47, __VA_ARGS__)) +#define _NRI_SEQ49(code, ...) _NRI_SEQ48(code, __VA_ARGS__), NRI_EXPAND(code(48, __VA_ARGS__)) +#define _NRI_SEQ50(code, ...) _NRI_SEQ49(code, __VA_ARGS__), NRI_EXPAND(code(49, __VA_ARGS__)) +#define _NRI_SEQ51(code, ...) _NRI_SEQ50(code, __VA_ARGS__), NRI_EXPAND(code(50, __VA_ARGS__)) +#define _NRI_SEQ52(code, ...) _NRI_SEQ51(code, __VA_ARGS__), NRI_EXPAND(code(51, __VA_ARGS__)) +#define _NRI_SEQ53(code, ...) _NRI_SEQ52(code, __VA_ARGS__), NRI_EXPAND(code(52, __VA_ARGS__)) +#define _NRI_SEQ54(code, ...) _NRI_SEQ53(code, __VA_ARGS__), NRI_EXPAND(code(53, __VA_ARGS__)) +#define _NRI_SEQ55(code, ...) _NRI_SEQ54(code, __VA_ARGS__), NRI_EXPAND(code(54, __VA_ARGS__)) +#define _NRI_SEQ56(code, ...) _NRI_SEQ55(code, __VA_ARGS__), NRI_EXPAND(code(55, __VA_ARGS__)) +#define _NRI_SEQ57(code, ...) _NRI_SEQ56(code, __VA_ARGS__), NRI_EXPAND(code(56, __VA_ARGS__)) +#define _NRI_SEQ58(code, ...) _NRI_SEQ57(code, __VA_ARGS__), NRI_EXPAND(code(57, __VA_ARGS__)) +#define _NRI_SEQ59(code, ...) _NRI_SEQ58(code, __VA_ARGS__), NRI_EXPAND(code(58, __VA_ARGS__)) +#define _NRI_SEQ60(code, ...) _NRI_SEQ59(code, __VA_ARGS__), NRI_EXPAND(code(59, __VA_ARGS__)) +#define _NRI_SEQ61(code, ...) _NRI_SEQ60(code, __VA_ARGS__), NRI_EXPAND(code(60, __VA_ARGS__)) +#define _NRI_SEQ62(code, ...) _NRI_SEQ61(code, __VA_ARGS__), NRI_EXPAND(code(61, __VA_ARGS__)) +#define _NRI_SEQ63(code, ...) _NRI_SEQ62(code, __VA_ARGS__), NRI_EXPAND(code(62, __VA_ARGS__)) +#define _NRI_SEQ64(code, ...) _NRI_SEQ63(code, __VA_ARGS__), NRI_EXPAND(code(63, __VA_ARGS__)) +#define _NRI_SEQ65(code, ...) _NRI_SEQ64(code, __VA_ARGS__), NRI_EXPAND(code(64, __VA_ARGS__)) +#define _NRI_SEQ66(code, ...) _NRI_SEQ65(code, __VA_ARGS__), NRI_EXPAND(code(65, __VA_ARGS__)) +#define _NRI_SEQ67(code, ...) _NRI_SEQ66(code, __VA_ARGS__), NRI_EXPAND(code(66, __VA_ARGS__)) +#define _NRI_SEQ68(code, ...) _NRI_SEQ67(code, __VA_ARGS__), NRI_EXPAND(code(67, __VA_ARGS__)) +#define _NRI_SEQ69(code, ...) _NRI_SEQ68(code, __VA_ARGS__), NRI_EXPAND(code(68, __VA_ARGS__)) +#define _NRI_SEQ70(code, ...) _NRI_SEQ69(code, __VA_ARGS__), NRI_EXPAND(code(69, __VA_ARGS__)) +#define _NRI_SEQ71(code, ...) _NRI_SEQ70(code, __VA_ARGS__), NRI_EXPAND(code(70, __VA_ARGS__)) +#define _NRI_SEQ72(code, ...) _NRI_SEQ71(code, __VA_ARGS__), NRI_EXPAND(code(71, __VA_ARGS__)) +#define NRI_SEQN(code, count, ...) NRI_MERGE_TOKEN2(_NRI_SEQ, count)(code, __VA_ARGS__) #define _NRI_VA_ARGS_AT0(_0, ...) _0 #define _NRI_VA_ARGS_AT1(_0, _1, ...) _1 @@ -179,7 +178,7 @@ #if defined(NRI_CPP) #define _NRI_ENUM_ENTRY(index, ...) NRI_VA_ARGS_AT(index, __VA_ARGS__) - #define _NRI_EXPAND_ENUM(...) NRI_SEQN(_NRI_ENUM_ENTRY, NRI_SHIELD(,), NRI_NARGS(__VA_ARGS__), __VA_ARGS__) + #define _NRI_EXPAND_ENUM(...) NRI_SEQN(_NRI_ENUM_ENTRY, NRI_NARGS(__VA_ARGS__), __VA_ARGS__) #define NRI_NAMESPACE_BEGIN namespace nri { #define NRI_NAMESPACE_END } @@ -189,7 +188,7 @@ #define NRI_FORWARD_STRUCT(name) struct name #define NRI_REF(arg) arg& #define NRI_REF_ACCESS(arg) (&arg) - #define NRI_ENUM_MEMBER(name, nameCaps, member) name::##member + #define NRI_ENUM_MEMBER(name, nameCaps, member) name::member #define NRI_DEFAULT_VALUE(arg) = arg #define NRI_ZERO_INIT {} @@ -210,7 +209,7 @@ #define _NRI_ENUM_PREFIX(name) NRI_MERGE_TOKEN3(NRI_, name, _) #define _NRI_ENUM_NAME(name) nri_##name##_ #define _NRI_ENUM_ENTRY(index, prefix, ...) NRI_MERGE_TOKEN2(prefix, NRI_VA_ARGS_AT(index, __VA_ARGS__)) - #define _NRI_EXPAND_ENUM(prefix, ...) NRI_SEQN(_NRI_ENUM_ENTRY, NRI_SHIELD(,), NRI_NARGS(__VA_ARGS__), prefix, __VA_ARGS__) + #define _NRI_EXPAND_ENUM(prefix, ...) NRI_SEQN(_NRI_ENUM_ENTRY, NRI_NARGS(__VA_ARGS__), prefix, __VA_ARGS__) #define NRI_NAMESPACE_BEGIN #define NRI_NAMESPACE_END diff --git a/ProgrammingGuide.md b/ProgrammingGuide.md index 819b3943..ae2fbaaf 100644 --- a/ProgrammingGuide.md +++ b/ProgrammingGuide.md @@ -19,7 +19,7 @@ NRI can be used as a *shared* or *static* library. ## C/C++ interface differences -| C | C++ | +| C++ | C | |-----------------------|-----------------------| | `nri::` | `nri_` | | Reference `&` | Pointer `*` | @@ -34,8 +34,7 @@ Analogues from the graphics APIs: | Device | Device | Device | | Command Buffer | Command List | Command Buffer | | Command Queue | Command Queue | Queue | -| Device Semaphore | Fence | Fence | -| Queue Semaphore | Fence | Semaphore | +| Fence | Fence | Timeline semaphore | | Command Allocator | Command Allocator | Command Pool | | Buffer | Resource | Buffer | | Texture | Resource | Image | @@ -53,9 +52,9 @@ Analogues from the graphics APIs: `Command Queue` is a queue of `Command Buffers` which are ready for execution on a physical device. A physical device may support multiple queue types, queues of different types are processed in parallel by the physical device. -`Device Semaphore` is a synchronization primitive which allows the application to wait for a physical device to complete execution of command buffers. - -`Queue Semaphore` is a synchronization primitive which allows the application to synchronize execution of `Command Buffers` between `Command Queues`. +`Fence` is a synchronization primitive which allows the application: +- to synchronize execution of `Command Buffers` between `Command Queues` +- to wait for a physical device to complete execution of command buffers `Command Allocator` is a memory allocator for command recording. When the application records commands into `Command Buffer`, memory for the commands is allocated from `Command Allocator` associated with the `Command Buffer`. @@ -95,7 +94,6 @@ Result CreateDevice(const DeviceCreationVulkanDesc& deviceDesc, Device*& device) ``` The application must specify the following members of `DeviceCreationVulkanDesc`: - * `deviceDesc.vkInstance` * VkInstance handle * `deviceDesc.vkDevice` @@ -123,30 +121,24 @@ Result GetPhysicalDevices(PhysicalDeviceGroup* physicalDeviceGroups, uint32_t& p The function allows the application to enumerate available physical devices. -*Valid usage:* - +Valid usage: * To get the number of physical devices, the application can call the function with physicalDeviceGroupNum set to 0 -*Multithreading:* - +Multithreading: * No synchronization required ```cpp Result CreateDevice(const DeviceCreationDesc& deviceCreationDesc, Device*& device) ``` - * `deviceCreationDesc.physicalDeviceGroup` * implementation will choose the first physical device if the value is nullptr - * `deviceCreationDesc.graphicsAPI` * controls which implementation will be used (D3D11, D3D12, VK) -*Valid usage:* - +Valid usage: * The application can create any number of NRI devices using different implementations, there is no limitation on this -*Multithreading:* - +Multithreading: * No synchronization required ## Destroying device @@ -157,12 +149,10 @@ An NRI device must be explicitly destroyed to free memory and graphics API objec void DestroyDevice(Device& device) ``` -*Valid usage:* - +Valid usage: * All NRI objects which were created using the device must be already destroyed -*Multithreading:* - +Multithreading: * Access to device must be externally synchronized # Querying interfaces @@ -172,7 +162,6 @@ To get NRI interface, the application can use the following function: ```cpp Result GetInterface(const Device& device, const char* interfaceName, size_t interfaceSize, void* interfacePtr) ``` - * `interfaceName` * The name of the interface C++ struct with "nri::" prefix @@ -190,32 +179,27 @@ const DeviceDesc& GetDeviceDesc(const Device& device) Returns the device description which contains capabilities of the device and some info on the NRI implementation. -*Multithreading:* - +Multithreading: * No synchronization required ```cpp Result GetCommandQueue(Device& device, CommandQueueType commandQueueType, CommandQueue*& commandQueue) ``` -*Valid usage:* - +Valid usage: * The application can call this function to check which command queue types are supported -*Multithreading:* - +Multithreading: * No synchronization required ```cpp Result CreateCommandAllocator(const CommandQueue& commandQueue, uint32_t physicalDeviceMask, CommandAllocator*& commandAllocator) ``` -*Valid usage:* - +Valid usage: * physicalDeviceMask can be set to 0 (WHOLE_DEVICE_GROUP) to create a command allocator which can be used with all physical devices in the device group -*Multithreading:* - +Multithreading: * No synchronization required ```cpp @@ -226,12 +210,10 @@ Creates a descriptor pool which can be used to allocate descriptor sets. If descriptorPoolDesc.physicalDeviceMask is set to 0 or has more than one bit set and the device group consists of more than one physical device, the maximum number of descriptors in the descriptor pool is multiplied by the number of physical devices. -*Valid usage:* - +Valid usage: * The function may fail if the maximum number of descriptors exceeds the maximum size of D3D12 descriptor heap -*Multithreading:* - +Multithreading: * No synchronization required ```cpp @@ -240,8 +222,7 @@ Result CreateBuffer(Device& device, const BufferDesc& bufferDesc, Buffer*& buffe Creates a buffer which must be bound to memory before it can be used on GPU. -*Multithreading:* - +Multithreading: * No synchronization required ```cpp @@ -250,64 +231,54 @@ Result CreateTexture(Device& device, const TextureDesc& textureDesc, Texture*& t Creates a texture which must be bound to memory before it can be used on GPU. -*Multithreading:* - +Multithreading: * No synchronization required ```cpp Result CreateBufferView(const BufferViewDesc& bufferViewDesc, Descriptor*& bufferView) ``` -*Valid usage:* - +Valid usage: * The buffer must be bound to memory -*Multithreading:* - +Multithreading: * No synchronization required ```cpp Result CreateTexture1DView(const Texture1DViewDesc& textureViewDesc, Descriptor*& textureView) ``` -*Valid usage:* - +Valid usage: * The texture must be bound to memory -*Multithreading:* - +Multithreading: * No synchronization required ```cpp Result CreateTexture2DView(const Texture2DViewDesc& textureViewDesc, Descriptor*& textureView) ``` -*Valid usage:* - +Valid usage: * The texture must be bound to memory. -*Multithreading:* - +Multithreading: * No synchronization required ```cpp Result CreateTexture3DView(const Texture3DViewDesc& textureViewDesc, Descriptor*& textureView) ``` -*Valid usage:* - +Valid usage: * The texture must be bound to memory -*Multithreading:* - +Multithreading: * No synchronization required ```cpp Result CreateSampler(Device& device, const SamplerDesc& samplerDesc, Descriptor*& sampler) ``` -*Multithreading:* - +Multithreading: * No synchronization required ```cpp @@ -316,99 +287,75 @@ Result CreatePipelineLayout(Device& device, const PipelineLayoutDesc& pipelineLa Creates a pipeline layout which describes resource binding for descriptors sets and pipelines. -*Valid usage:* - -* pipelineLayoutDesc.stageMask can be a combination of bits which correspond to only one type of pipeline (graphics, compute, or ray tracing). For example, a pipeline layout can not be created for graphics and compute stages together, the application must create separate pipeline layouts -pipelineLayoutDesc.stageMask must be a minimal set of pipeline stages which need access to shader resources. +Valid usage: +* pipelineLayoutDesc.stageMask can be a combination of bits which correspond to only one type of pipeline (graphics, compute, or ray tracing). For example, a pipeline layout can not be created for graphics and compute stages together, the application must create separate pipeline layouts pipelineLayoutDesc.stageMask must be a minimal set of pipeline stages which need access to shader resources. * If the pipeline layout is used with a pipeline which has a vertex stage that needs access to vertex attributes, pipelineLayoutDesc.stageMask must include the vertex pipeline stage -*Multithreading:* - +Multithreading: * No synchronization required ```cpp Result CreateGraphicsPipeline(Device& device, const GraphicsPipelineDesc& graphicsPipelineDesc, Pipeline*& pipeline) ``` -*Valid usage:* - +Valid usage: * graphicsPipelineDesc.pipelineLayout must be a valid pipeline layout * graphicsPipelineDesc.inputAssembly must be a valid pointer to InputAssemblyDesc * graphicsPipelineDesc.rasterization can be nullptr * graphicsPipelineDesc.outputMerger can be nullptr * graphicsPipelineDesc.shaderStageNum must be greater than 0 -*Multithreading:* - +Multithreading: * No synchronization required ```cpp Result CreateComputePipeline(Device& device, const ComputePipelineDesc& computePipelineDesc, Pipeline*& pipeline) ``` -*Valid usage:* - +Valid usage: * computePipelineDesc.pipelineLayout must be a valid pipeline layout * computePipelineDesc.computeShader must be a valid shader description -*Multithreading:* - +Multithreading: * No synchronization required ```cpp Result CreateFrameBuffer(Device& device, const FrameBufferDesc& frameBufferDesc, FrameBuffer*& frameBuffer) ``` -*Multithreading:* - +Multithreading: * No synchronization required ```cpp Result CreateQueryPool(Device& device, const QueryPoolDesc& queryPoolDesc, QueryPool*& queryPool) ``` -*Multithreading:* - +Multithreading: * No synchronization required ```cpp -Result CreateQueueSemaphore(Device& device, QueueSemaphore*& queueSemaphore) +Result CreateQueueFence(Device& device, uint64_t initialValue, Fence*& fence) ``` -Creates a queue semaphore. The initial state of the semaphore is unsignaled, therefore it must be signaled by a command queue before it can be used to wait on a command queue. - -*Multithreading:* - -* No synchronization required - -```cpp -Result CreateDeviceSemaphore(Device& device, bool signaled, DeviceSemaphore*& deviceSemaphore) -``` - -*Multithreading:* - +Multithreading: * No synchronization required ```cpp Result CreateCommandBuffer(CommandAllocator& commandAllocator, CommandBuffer*& commandBuffer) ``` -*Multithreading:* - +Multithreading: * No synchronization required ```cpp void DestroyCommandAllocator(CommandAllocator& commandAllocator) ``` -*Valid usage:* - -* All command buffers that were allocated using the command allocator must be destroyed -* The application must ensure that all command buffers which were allocated using the command allocator have completed execution - -*Multithreading:* +Valid usage: +* All command buffers that were allocated using the command allocator must be destroyed* The application must ensure that all command buffers which were allocated using the command allocator have completed execution +Multithreading: * Access to commandAllocator must be externally synchronized ```cpp @@ -417,156 +364,121 @@ void DestroyDescriptorPool(DescriptorPool& descriptorPool) Destroys the descriptor pool and its descriptor sets. -*Valid usage:* - +Valid usage: * The application must ensure that all command buffers which use the descriptor pool and the descriptor sets have completed execution -*Multithreading:* - +Multithreading: * Access to descriptorPool must be externally synchronized ```cpp void DestroyBuffer(Buffer& buffer) ``` -*Valid usage:* - +Valid usage: * The application must ensure that GPU finished execution of command buffers which use the buffer * All descriptors (buffer views) which reference the buffer must be destroyed -*Multithreading:* - +Multithreading: * Access to buffer must be externally synchronized ```cpp void DestroyTexture(Texture& texture) ``` -*Valid usage:* - +Valid usage: * The application must ensure that all command buffers which use the texture have completed execution * All descriptors (texture views) which reference the texture must be destroyed -*Multithreading:* - +Multithreading: * Access to texture must be externally synchronized ```cpp void DestroyDescriptor(Descriptor& descriptor) ``` -*Valid usage:* - +Valid usage: * The application must ensure that all command buffers which use descriptor sets which contain the descriptor have completed execution * Descriptor sets must not reference the descriptor -*Multithreading:* - +Multithreading: * Access to descriptor must be externally synchronized ```cpp void DestroyPipelineLayout(PipelineLayout& pipelineLayout) ``` -*Valid usage:* - +Valid usage: * Pipelines which were created using the pipeline layout must be destroyed * Descriptor sets which were allocated using the pipeline layout must be destroyed -*Multithreading:* - +Multithreading: * Access to pipelineLayout must be externally synchronized ```cpp void DestroyPipeline(Pipeline& pipeline) ``` -*Valid usage:* - +Valid usage: * The application must ensure that all command buffers which use the pipeline have completed execution -*Multithreading:* - +Multithreading: * Access to pipeline must be externally synchronized ```cpp void DestroyFrameBuffer(FrameBuffer& frameBuffer) ``` -*Valid usage:* - +Valid usage: * The application must ensure that all command buffers which use the framebuffer have completed execution -*Multithreading:* - +Multithreading: * Access to frameBuffer must be externally synchronized ```cpp void DestroyQueryPool(QueryPool& queryPool) ``` -*Valid usage:* - +Valid usage: * The application must ensure that all command buffers which use the query pool have completed execution -*Multithreading:* - +Multithreading: * Access to queryPool must be externally synchronized ```cpp -void DestroyQueueSemaphore(QueueSemaphore& queueSemaphore) -``` - -*Valid usage:* - -* The application must ensure that all GPU operations related to the semaphore have finished - -*Multithreading:* - -* Access to queueSemaphore must be externally synchronized - -```cpp -void DestroyDeviceSemaphore(DeviceSemaphore& deviceSemaphore) +void DestroyFence(Fence& fence) ``` -*Valid usage:* - -* The application must ensure that all GPU operations related to the semaphore have finished - -*Multithreading:* +Valid usage: +* The application must ensure that all GPU operations related to the fence have finished -* Access to deviceSemaphore must be externally synchronized +Multithreading: +* Access to fence must be externally synchronized ```cpp void DestroyCommandBuffer(CommandBuffer& commandBuffer) ``` -*Valid usage:* - +Valid usage: * The application must ensure that the command buffer has completed execution -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp Result AllocateMemory(Device& device, uint32_t physicalDeviceMask, MemoryType memoryType, uint64_t size, Memory*& memory) ``` -*Valid usage:* - +Valid usage: * memoryType must be a valid memory type received using GetBufferMemoryInfo or GetTextureMemoryInfo -*Multithreading:* - +Multithreading: * No synchronization required ```cpp Result BindBufferMemory(Device& device, const BufferMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum) ``` -*Multithreading:* - +Multithreading: * Access to buffers must be externally synchronized * Access to memory instances must be externally synchronized @@ -574,8 +486,7 @@ Result BindBufferMemory(Device& device, const BufferMemoryBindingDesc* memoryBin Result BindTextureMemory(Device& device, const TextureMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum) ``` -*Multithreading:* - +Multithreading: * Access to textures must be externally synchronized * Access to memory instances must be externally synchronized @@ -583,69 +494,59 @@ Result BindTextureMemory(Device& device, const TextureMemoryBindingDesc* memoryB void FreeMemory(Memory& memory) ``` -*Valid usage:* - +Valid usage: * All buffers or textures which are bound to the memory must be destroyed. -*Multithreading:* - +Multithreading: * Access to memory must be externally synchronized ```cpp Result BeginCommandBuffer(CommandBuffer& commandBuffer, const DescriptorPool* descriptorPool, uint32_t physicalDeviceIndex) ``` -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp Result EndCommandBuffer(CommandBuffer& commandBuffer) ``` -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdSetPipeline(CommandBuffer& commandBuffer, const Pipeline& pipeline) ``` -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdSetPipelineLayout(CommandBuffer& commandBuffer, const PipelineLayout& pipelineLayout) ``` -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdSetDescriptorSet(CommandBuffer& commandBuffer, uint32_t setIndexInPipelineLayout, const DescriptorSet& descriptorSet, const uint32_t* dynamicConstantBufferOffsets) ``` -*Valid usage:* - +Valid usage: * A compatible pipeline layout must be set * The number of offsets in dynamicConstantBufferOffsets must be equal to the total number of dynamic constant buffers in descriptorSet -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdSetConstants(CommandBuffer& commandBuffer, uint32_t pushConstantIndex, const void* data, uint32_t size) ``` -*Valid usage:* - +Valid usage: * A compatible pipeline layout must be set -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp @@ -653,364 +554,311 @@ void CmdSetDescriptorPool(CommandBuffer& commandBuffer, const DescriptorPool& de ``` **`Performance notes:`** - * Sets descriptor heap in D3D12, which in some cases might cause pipeline flush -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdPipelineBarrier(CommandBuffer& commandBuffer, const TransitionBarrierDesc* transitionBarriers, const AliasingBarrierDesc* aliasingBarriers, BarrierDependency dependency) ``` -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdBeginRenderPass(CommandBuffer& commandBuffer, const FrameBuffer& frameBuffer, FramebufferBindFlag renderPassBeginFlag) ``` -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdEndRenderPass(CommandBuffer& commandBuffer) ``` -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdSetViewports(CommandBuffer& commandBuffer, const Viewport* viewports, uint32_t viewportNum) ``` -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdSetScissors(CommandBuffer& commandBuffer, const Rect* rects, uint32_t rectNum) ``` -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdSetDepthBounds(CommandBuffer& commandBuffer, float boundsMin, float boundsMax) ``` -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdSetStencilReference(CommandBuffer& commandBuffer, uint8_t reference) ``` -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdSetSamplePositions(CommandBuffer& commandBuffer, const SamplePosition* positions, uint32_t positionNum) ``` -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdClearAttachments(CommandBuffer& commandBuffer, const ClearDesc* clearDescs, uint32_t clearDescNum, const Rect* rects, uint32_t rectNum) ``` -*Valid usage:* - +Valid usage: * Can be called only inside render pass -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdSetIndexBuffer(CommandBuffer& commandBuffer, const Buffer& buffer, uint64_t offset, IndexType indexType) ``` -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdSetVertexBuffers(CommandBuffer& commandBuffer, uint32_t baseSlot, uint32_t bufferNum, const Buffer* const* buffers, const uint64_t* offsets) ``` -*Valid usage:* - +Valid usage: * CmdSetVertexBuffers uses vertex stream strides from the current pipeline -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdDraw(CommandBuffer& commandBuffer, uint32_t vertexNum, uint32_t instanceNum, uint32_t baseVertex, uint32_t baseInstance) ``` -*Valid usage:* - +Valid usage: * Can be called only inside render pass -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdDrawIndexed(CommandBuffer& commandBuffer, uint32_t indexNum, uint32_t instanceNum, uint32_t baseIndex, uint32_t baseVertex, uint32_t baseInstance) ``` -*Valid usage:* - +Valid usage: * Can be called only inside render pass -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdDrawIndirect(CommandBuffer& commandBuffer, const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride) ``` -*Valid usage:* - +Valid usage: * Can be called only inside render pass -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdDrawIndexedIndirect(CommandBuffer& commandBuffer, const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride) ``` -*Valid usage:* - +Valid usage: * Can be called only inside render pass -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdDispatch(CommandBuffer& commandBuffer, uint32_t x, uint32_t y, uint32_t z) ``` -*Valid usage:* - +Valid usage: * Can be called only outside of a render pass -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdDispatchIndirect(CommandBuffer& commandBuffer, const Buffer& buffer, uint64_t offset) ``` -*Valid usage:* - +Valid usage: * Can be called only outside of a render pass -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdBeginQuery(CommandBuffer& commandBuffer, const QueryPool& queryPool, uint32_t offset) ``` -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdEndQuery(CommandBuffer& commandBuffer, const QueryPool& queryPool, uint32_t offset) ``` -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdBeginAnnotation(CommandBuffer& commandBuffer, const char* name) ``` -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdEndAnnotation(CommandBuffer& commandBuffer) ``` -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdClearStorageBuffer(CommandBuffer& commandBuffer, const ClearStorageBufferDesc& clearDesc) ``` -*Valid usage:* - +Valid usage: * Can be called only outside of a render pass -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdClearStorageTexture(CommandBuffer& commandBuffer, const ClearStorageTextureDesc& clearDesc) ``` -*Valid usage:* - +Valid usage: * Can be called only outside of a render pass -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdCopyBuffer(CommandBuffer& commandBuffer, Buffer& dstBuffer, uint32_t dstPhysicalDeviceIndex, uint64_t dstOffset, const Buffer& srcBuffer, uint32_t srcPhysicalDeviceIndex, uint64_t srcOffset, uint64_t size) ``` -*Valid usage:* - +Valid usage: * Can be called only outside of a render pass -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdCopyTexture(CommandBuffer& commandBuffer, Texture& dstTexture, uint32_t dstPhysicalDeviceIndex, const TextureRegionDesc* dstRegionDesc, const Texture& srcTexture, uint32_t srcPhysicalDeviceIndex, const TextureRegionDesc* srcRegionDesc) ``` -*Valid usage:* - +Valid usage: * Can be called only outside of a render pass -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdUploadBufferToTexture(CommandBuffer& commandBuffer, Texture& dstTexture, const TextureRegionDesc& dstRegionDesc, const Buffer& srcBuffer, const TextureDataLayoutDesc& srcDataLayoutDesc) ``` -*Valid usage:* - +Valid usage: * Can be called only outside of a render pass -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdReadbackTextureToBuffer(CommandBuffer& commandBuffer, Buffer& dstBuffer, TextureDataLayoutDesc& dstDataLayoutDesc, const Texture& srcTexture, const TextureRegionDesc& srcRegionDesc) ``` -*Valid usage:* - +Valid usage: * Can be called only outside of a render pass -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdCopyQueries(CommandBuffer& commandBuffer, const QueryPool& queryPool, uint32_t offset, uint32_t num, Buffer& dstBuffer, uint64_t dstOffset) ``` -*Valid usage:* - +Valid usage: * Can be called only outside of a render pass -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp -void SubmitQueueWork(CommandQueue& commandQueue, const WorkSubmissionDesc& workSubmissionDesc, DeviceSemaphore* deviceSemaphore) +void QueueSubmit(CommandQueue& commandQueue, const QueueSubmitDesc& queueSubmitDesc) ``` -*Valid usage:* - -* If deviceSemaphore is not nullptr, it must be in unsignaled state -* If queue submission waits a queue semaphore, there must be queue submission which signals the queue semaphore and it must be submitted for execution before the one which waits - -**Correct:** - -1. SubmitQueueWork(...) // signals semaphore A -2. SubmitQueueWork(...) // waits semaphore A +Multithreading: +* Access to commandQueue must be externally synchronized -**Incorrect:** +```cpp +void QueueWait(CommandQueue& commandQueue, Fence& fence, uint64_t value) +``` -1. SubmitQueueWork(...) // waits semaphore A -2. SubmitQueueWork(...) // signals semaphore A +Multithreading: +* Access to commandQueue must be externally synchronized -*Multithreading:* +```cpp +void QueueSignal(CommandQueue& commandQueue, Fence& fence, uint64_t value) +``` +Multithreading: * Access to commandQueue must be externally synchronized -* Access to deviceSemaphore must be externally synchronized ```cpp -void WaitForSemaphore(CommandQueue& commandQueue, DeviceSemaphore& deviceSemaphore) +uint64_t GetFenceValue(Fence& fence) ``` -The function blocks execution until the device semaphore is signaled by the command queue. -The function resets deviceSemaphore after waiting. - -A device semaphore can be signaled using SubmitQueueWork(). +Multithreading: +* No synchronization required -*Multithreading:* +```cpp +void Wait(Fence& fence, uint64_t value) +``` -* Access to commandQueue must be externally synchronized -* Access to deviceSemaphore must be externally synchronized +Multithreading: +* No synchronization required ```cpp void UpdateDescriptorRanges(DescriptorSet& descriptorSet, uint32_t physicalDeviceMask, uint32_t baseRange, uint32_t rangeNum, const DescriptorRangeUpdateDesc* rangeUpdateDescs) ``` -*Multithreading:* - +Multithreading: * Access to descriptorSet must be externally synchronized ```cpp void UpdateDynamicConstantBuffers(DescriptorSet& descriptorSet, uint32_t physicalDeviceMask, uint32_t baseBuffer, uint32_t bufferNum, const Descriptor* const* descriptors) ``` -*Multithreading:* - +Multithreading: * Access to descriptorSet must be externally synchronized ```cpp void CopyDescriptorSet(DescriptorSet& descriptorSet, const DescriptorSetCopyDesc& descriptorSetCopyDesc) ``` -*Multithreading:* - -* Access to descriptorSet must be externally synchronized -* Access to descriptorSetCopyDesc.srcDescriptorSet must be externally synchronized +Multithreading: +* Access to descriptorSet must be externally synchronized* Access to descriptorSetCopyDesc.srcDescriptorSet must be externally synchronized ```cpp Result AllocateDescriptorSets(DescriptorPool& descriptorPool, const PipelineLayout& pipelineLayout, uint32_t setIndexInPipelineLayout, DescriptorSet** const descriptorSets, uint32_t instanceNum, uint32_t physicalDeviceMask, uint32_t variableDescriptorNum) ``` - * `pipelineLayout` * The pipeline layout which describes shader resource binding * `setIndexInPipelineLayout` @@ -1020,8 +868,7 @@ Result AllocateDescriptorSets(DescriptorPool& descriptorPool, const PipelineLayo * `variableDescriptorNum` * If the descriptor set in the pipeline layout uses variable descriptor number, the argument specifies the actual size of the descriptor range -*Multithreading:* - +Multithreading: * Access to descriptorPool must be externally synchronized ```cpp @@ -1030,12 +877,10 @@ void ResetDescriptorPool(DescriptorPool& descriptorPool) Destroys descriptor sets allocated from the descriptor pool -*Valid usage:* - +Valid usage: * The application must ensure that all command buffers which use the descriptor pool and the descriptor sets completed execution -*Multithreading:* - +Multithreading: * Access to descriptorPool must be externally synchronized ```cpp @@ -1044,12 +889,10 @@ void ResetCommandAllocator(CommandAllocator& commandAllocator) Deallocates memory which was allocated for command recording, but does not destroy command buffers. -*Valid usage:* - +Valid usage: * The application must ensure that all command buffers which were allocated using the command allocator completed execution -*Multithreading:* - +Multithreading: * Access to commandAllocator must be externally synchronized ```cpp @@ -1058,8 +901,7 @@ uint32_t GetQuerySize(const QueryPool& queryPool) Returns the size of the query in bytes -*Multithreading:* - +Multithreading: * No synchronized required ```cpp @@ -1067,45 +909,38 @@ void GetBufferMemoryInfo(const Buffer& buffer, MemoryLocation memoryLocation, Me ``` Returns memory requirements such as size, alignment and memory type. - * `memoryLocation` * Memory location for the buffer, it can be DEVICE, HOST_UPLOAD, or HOST_READBACK. **`Performance notes:`** - * HOST_UPLOAD and HOST_READBACK both correspond to coherent and cached memory in Vulkan -*Multithreading:* - +Multithreading: * No synchronized required ```cpp void* MapBuffer(Buffer& buffer, uint64_t offset, uint64_t size) ``` -*Valid usage:* - +Valid usage: * The buffer must be bound to HOST_UPLOAD or HOST_READBACK memory * The buffer must not already be mapped -*Multithreading:* - +Multithreading: * Access to buffer must be externally synchronized ```cpp void UnmapBuffer(Buffer& buffer) ``` -*Multithreading:* - +Multithreading: * Access to buffer must be externally synchronized ```cpp void GetTextureMemoryInfo(const Texture& texture, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) ``` -*Multithreading:* - +Multithreading: * Access to texture must be externally synchronized ```cpp @@ -1114,136 +949,112 @@ FormatSupportBits GetFormatSupport(const Device& device, Format format) The function allows the application to check format support for different use cases. -*Multithreading:* - +Multithreading: * No synchronized required ```cpp void SetDeviceDebugName(Device& device, const char* name) ``` -*Multithreading:* - +Multithreading: * Access to device must be externally synchronized ```cpp void SetCommandQueueDebugName(CommandQueue& commandQueue, const char* name) ``` -*Multithreading:* - +Multithreading: * Access to commandQueue must be externally synchronized ```cpp -void SetDeviceSemaphoreDebugName(DeviceSemaphore& deviceSemaphore, const char* name) -``` - -*Multithreading:* - -* Access to deviceSemaphore must be externally synchronized - -```cpp -void SetQueueSemaphoreDebugName(QueueSemaphore& queueSemaphore, const char* name) +void SetFenceDebugName(Fence& fence, const char* name) ``` -*Multithreading:* - -* Access to queueSemaphore must be externally synchronized +Multithreading: +* Access to fence must be externally synchronized ```cpp void SetCommandAllocatorDebugName(CommandAllocator& commandAllocator, const char* name) ``` -*Multithreading:* - +Multithreading: * Access to commandAllocator must be externally synchronized ```cpp void SetDescriptorPoolDebugName(DescriptorPool& descriptorPool, const char* name) ``` -*Multithreading:* - +Multithreading: * Access to descriptorPool must be externally synchronized ```cpp void SetBufferDebugName(Buffer& buffer, const char* name) ``` -*Multithreading:* - +Multithreading: * Access to buffer must be externally synchronized ```cpp void SetTextureDebugName(Texture& texture, const char* name) ``` -*Multithreading:* - +Multithreading: * Access to texture must be externally synchronized ```cpp void SetDescriptorDebugName(Descriptor& descriptor, const char* name) ``` -*Multithreading:* - +Multithreading: * Access to descriptor must be externally synchronized ```cpp void SetPipelineLayoutDebugName(PipelineLayout& pipelineLayout, const char* name) ``` -*Multithreading:* - +Multithreading: * Access to pipelineLayout must be externally synchronized ```cpp void SetPipelineDebugName(Pipeline& pipeline, const char* name) ``` -*Multithreading:* - +Multithreading: * Access to pipeline must be externally synchronized ```cpp void SetFrameBufferDebugName(FrameBuffer& frameBuffer, const char* name) ``` -*Multithreading:* - +Multithreading: * Access to frameBuffer must be externally synchronized ```cpp void SetQueryPoolDebugName(QueryPool& queryPool, const char* name) ``` -*Multithreading:* - +Multithreading: * Access to queryPool must be externally synchronized ```cpp void SetDescriptorSetDebugName(DescriptorSet& descriptorSet, const char* name) ``` -*Multithreading:* - +Multithreading: * Access to descriptorSet must be externally synchronized ```cpp void SetCommandBufferDebugName(CommandBuffer& commandBuffer, const char* name) ``` -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void SetMemoryDebugName(Memory& memory, const char* name) ``` -*Multithreading:* - +Multithreading: * Access to memory must be externally synchronized # Swapchain Interface @@ -1251,75 +1062,56 @@ void SetMemoryDebugName(Memory& memory, const char* name) ```cpp Result CreateSwapChain(Device& device, const SwapChainDesc& swapChainDesc, SwapChain*& swapChain) ``` - * `swapChainDesc.commandQueue` * The command queue which will be presenting -*Multithreading:* - +Multithreading: * No synchronization required ```cpp void DestroySwapChain(SwapChain& swapChain) ``` -*Multithreading:* - +Multithreading: * Access to swapChain must be externally synchronized ```cpp void SetSwapChainDebugName(SwapChain& swapChain, const char* name) ``` -*Multithreading:* - +Multithreading: * Access to swapChain must be externally synchronized ```cpp Texture* const* GetSwapChainTextures(const SwapChain& swapChain, uint32_t& textureNum, Format& format) ``` -*Valid usage:* - -* To use the textures, the application must acquire access using AcquireNextSwapChainTexture() -* textureNum may not match swapChainDesc.textureNum - -*Multithreading:* +Valid usage: +* To use the textures, the application must acquire access using AcquireNextSwapChainTexture()* textureNum may not match swapChainDesc.textureNum +Multithreading: * No synchronization required ```cpp -uint32_t AcquireNextSwapChainTexture(SwapChain& swapChain, QueueSemaphore& textureReadyForRender) +uint32_t AcquireNextSwapChainTexture(SwapChain& swapChain) ``` - Returns index of the acquired swapchain texture. -*Valid usage:* - -* The application must add textureReadyForRender to WorkSubmissionDesc::wait to synchronize access to the texture - -*Multithreading:* - +Multithreading: * Access to swapChain must be externally synchronized ```cpp -Result SwapChainPresent(SwapChain& swapChain, QueueSemaphore& textureReadyForPresent) +Result SwapChainPresent(SwapChain& swapChain) ``` -*Valid usage:* - -* textureReadyForPresent must be signaled by the application - -*Multithreading:* - +Multithreading: * Access to swapChain must be externally synchronized ```cpp Result SetSwapChainHdrMetadata(SwapChain& swapChain, const HdrMetadata& hdrMetadata) ``` -*Multithreading:* - +Multithreading: * Access to swapChain must be externally synchronized # Ray Tracing Interface @@ -1328,143 +1120,124 @@ Result SetSwapChainHdrMetadata(SwapChain& swapChain, const HdrMetadata& hdrMetad Result CreateRayTracingPipeline(Device& device, const RayTracingPipelineDesc& rayTracingPipelineDesc, Pipeline*& pipeline) ``` -*Multithreading:* - +Multithreading: * No synchronization required ```cpp Result CreateAccelerationStructure(Device& device, const AccelerationStructureDesc& accelerationStructureDesc, AccelerationStructure*& accelerationStructure) ``` -*Multithreading:* - +Multithreading: * No synchronization required ```cpp Result BindAccelerationStructureMemory(Device& device, const AccelerationStructureMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum) ``` -*Multithreading:* - -* Access to the acceleration structure instances must be externally synchronized -* Access to the memory instances must be externally synchronized +Multithreading: +* Access to the acceleration structure instances must be externally synchronized* Access to the memory instances must be externally synchronized ```cpp Result CreateAccelerationStructureDescriptor(const AccelerationStructure& accelerationStructure, uint32_t physicalDeviceMask, Descriptor*& descriptor) ``` -*Multithreading:* - +Multithreading: * Access to accelerationStructure must be externally synchronized ```cpp void SetAccelerationStructureDebugName(AccelerationStructure& accelerationStructure, const char* name) ``` -*Multithreading:* - +Multithreading: * Access to accelerationStructure must be externally synchronized ```cpp void DestroyAccelerationStructure(AccelerationStructure& accelerationStructure) ``` -*Multithreading:* - +Multithreading: * Access to accelerationStructure must be externally synchronized ```cpp void GetAccelerationStructureMemoryInfo(const AccelerationStructure& accelerationStructure, MemoryDesc& memoryDesc) ``` -*Multithreading:* - +Multithreading: * No synchronization required ```cpp uint64_t GetAccelerationStructureUpdateScratchBufferSize(const AccelerationStructure& accelerationStructure) ``` -*Multithreading:* - +Multithreading: * No synchronization required ```cpp uint64_t GetAccelerationStructureBuildScratchBufferSize(const AccelerationStructure& accelerationStructure) ``` -*Multithreading:* - +Multithreading: * No synchronization required ```cpp uint64_t GetAccelerationStructureHandle(const AccelerationStructure& accelerationStructure, uint32_t physicalDeviceIndex) ``` -*Multithreading:* - +Multithreading: * No synchronization required ```cpp Result WriteShaderGroupIdentifiers(const Pipeline& pipeline, uint32_t baseShaderGroupIndex, uint32_t shaderGroupNum, void* buffer) ``` -*Multithreading:* - +Multithreading: * No synchronization required ```cpp void CmdBuildTopLevelAccelerationStructure(CommandBuffer& commandBuffer, uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, AccelerationStructureBuildBits flags, AccelerationStructure& dst, Buffer& scratch, uint64_t scratchOffset) ``` -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdBuildBottomLevelAccelerationStructure(CommandBuffer& commandBuffer, uint32_t geometryObjectNum, const GeometryObject* geometryObjects, AccelerationStructureBuildBits flags, AccelerationStructure& dst, Buffer& scratch, uint64_t scratchOffset) ``` -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdUpdateTopLevelAccelerationStructure(CommandBuffer& commandBuffer, uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, AccelerationStructureBuildBits flags, AccelerationStructure& dst, AccelerationStructure& src, Buffer& scratch, uint64_t scratchOffset) ``` -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdUpdateBottomLevelAccelerationStructure(CommandBuffer& commandBuffer, uint32_t geometryObjectNum, const GeometryObject* geometryObjects, AccelerationStructureBuildBits flags, AccelerationStructure& dst, AccelerationStructure& src, Buffer& scratch, uint64_t scratchOffset) ``` -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdCopyAccelerationStructure(CommandBuffer& commandBuffer, AccelerationStructure& dst, AccelerationStructure& src, CopyMode copyMode) ``` -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdWriteAccelerationStructureSize(CommandBuffer& commandBuffer, const AccelerationStructure* const* accelerationStructures, uint32_t accelerationStructureNum, QueryPool& queryPool, uint32_t queryPoolOffset) ``` -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized ```cpp void CmdDispatchRays(CommandBuffer& commandBuffer, const DispatchRaysDesc& dispatchRaysDesc) ``` -*Multithreading:* - +Multithreading: * Access to commandBuffer must be externally synchronized diff --git a/README.md b/README.md index 244e8e6c..1fb11cbf 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# NVIDIA Render Interface v1.91 (NRI) +# NVIDIA Render Interface v1.93 (NRI) *NRI* is a low-level abstract render interface which currently supports three backends: D3D11, D3D12 and Vulkan. *NRI* has been designed to support all (at least major) low level features of D3D12 and Vulkan APIs, but at the same time to simplify usage and reduce the amount of code needed (especially compared with Vulkan). *NRI* is written in *C++*, but supports both *C++* and *C* interfaces. diff --git a/Resources/Version.h b/Resources/Version.h index 1063c7e1..fff180ec 100644 --- a/Resources/Version.h +++ b/Resources/Version.h @@ -21,7 +21,7 @@ Versioning rules: */ #define VERSION_MAJOR 1 -#define VERSION_MINOR 91 +#define VERSION_MINOR 93 #define VERSION_BUILD 0 #define VERSION_REVISION 0 diff --git a/Source/Creation/Creation.cpp b/Source/Creation/Creation.cpp index bd2f6903..1d41fb82 100644 --- a/Source/Creation/Creation.cpp +++ b/Source/Creation/Creation.cpp @@ -414,14 +414,17 @@ NRI_API Result NRI_CALL nri::GetPhysicalDevices(PhysicalDeviceGroup* physicalDev { qsort(adapters, adaptersNum, sizeof(adapters[0]), SortAdaptersByDedicatedVideoMemorySize); + if (adaptersNum < physicalDeviceGroupNum) + physicalDeviceGroupNum = adaptersNum; + for (uint32_t i = 0; i < physicalDeviceGroupNum; i++) { DXGI_ADAPTER_DESC1 desc; adapters[i]->GetDesc1(&desc); PhysicalDeviceGroup& group = physicalDeviceGroups[i]; - memset(&group, 0 ,sizeof(group)); - wcscpy(group.description, desc.Description); + memset(&group, 0, sizeof(group)); + wcstombs(group.description, desc.Description, GetCountOf(group.description) - 1); group.luid = *(uint64_t*)&desc.AdapterLuid; group.dedicatedVideoMemory = desc.DedicatedVideoMemory; group.deviceID = desc.DeviceId; @@ -473,7 +476,7 @@ NRI_API Result NRI_CALL nri::GetPhysicalDevices(PhysicalDeviceGroup* physicalDev // Create instance VkApplicationInfo applicationInfo = {}; - applicationInfo.apiVersion = VK_API_VERSION_1_1; + applicationInfo.apiVersion = VK_API_VERSION_1_3; VkInstanceCreateInfo instanceCreateInfo = {}; instanceCreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; @@ -518,14 +521,7 @@ NRI_API Result NRI_CALL nri::GetPhysicalDevices(PhysicalDeviceGroup* physicalDev PhysicalDeviceGroup& group = physicalDeviceGroupsSorted[i]; memset(&group, 0, sizeof(group)); - - char* src = properties2.properties.deviceName; - wchar_t* dst = group.description; - uint32_t n = 0; - while (*src && ++n < GetCountOf(group.description)) - *dst++ = *src++; - *dst = 0; - + strncpy(group.description, properties2.properties.deviceName, sizeof(group.description)); group.luid = *(uint64_t*)&deviceIDProperties.deviceLUID[0]; group.deviceID = properties2.properties.deviceID; group.vendor = GetVendorFromID(properties2.properties.vendorID); @@ -555,6 +551,9 @@ NRI_API Result NRI_CALL nri::GetPhysicalDevices(PhysicalDeviceGroup* physicalDev qsort(physicalDeviceGroupsSorted, deviceGroupNum, sizeof(physicalDeviceGroupsSorted[0]), SortAdaptersByDedicatedVideoMemorySize); // Copy to output + if (deviceGroupNum < physicalDeviceGroupNum) + physicalDeviceGroupNum = deviceGroupNum; + for (uint32_t i = 0; i < physicalDeviceGroupNum; i++) *physicalDeviceGroups++ = *physicalDeviceGroupsSorted++; } diff --git a/Source/D3D11/BufferD3D11.cpp b/Source/D3D11/BufferD3D11.cpp index 6cf04ee2..40000da1 100644 --- a/Source/D3D11/BufferD3D11.cpp +++ b/Source/D3D11/BufferD3D11.cpp @@ -8,36 +8,21 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#include "SharedExternal.h" #include "SharedD3D11.h" #include "BufferD3D11.h" - #include "MemoryD3D11.h" #include "QueryPoolD3D11.h" #include "TextureD3D11.h" using namespace nri; -BufferD3D11::BufferD3D11(DeviceD3D11& device, const VersionedContext& immediateContext, const BufferDesc& bufferDesc) : - m_ImmediateContext(immediateContext), - m_Desc(bufferDesc), - m_Device(device) -{ -} - -BufferD3D11::BufferD3D11(DeviceD3D11& device, const VersionedContext& immediateContext) : - m_ImmediateContext(immediateContext), - m_Device(device) -{ -} - BufferD3D11::~BufferD3D11() { if (m_ReadbackTexture) Deallocate(m_Device.GetStdAllocator(), m_ReadbackTexture); } -Result BufferD3D11::Create(const VersionedDevice& device, const MemoryD3D11& memory) +Result BufferD3D11::Create(const MemoryD3D11& memory) { MemoryLocation memoryLocation = memory.GetType(); @@ -94,7 +79,7 @@ Result BufferD3D11::Create(const VersionedDevice& device, const MemoryD3D11& mem if (m_Desc.usageMask & BufferUsageBits::SHADER_RESOURCE_STORAGE) desc.BindFlags |= D3D11_BIND_UNORDERED_ACCESS; - HRESULT hr = device->CreateBuffer(&desc, nullptr, &m_Buffer); + HRESULT hr = m_Device.GetDevice()->CreateBuffer(&desc, nullptr, &m_Buffer); RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "ID3D11Device::CreateBuffer() - FAILED!"); uint32_t priority = memory.GetResidencyPriority(m_Desc.size); @@ -155,7 +140,7 @@ Result BufferD3D11::Create(const BufferD3D11Desc& bufferDesc) void* BufferD3D11::Map(MapType mapType, uint64_t offset) { - CriticalSection criticalSection(m_ImmediateContext); + CriticalSection criticalSection(m_Device.GetImmediateContext()); FinalizeQueries(); FinalizeReadback(); @@ -167,7 +152,7 @@ void* BufferD3D11::Map(MapType mapType, uint64_t offset) map = D3D11_MAP_WRITE; D3D11_MAPPED_SUBRESOURCE mappedData = {}; - HRESULT hr = m_ImmediateContext->Map(m_Buffer, 0, map, 0, &mappedData); + HRESULT hr = m_Device.GetImmediateContext()->Map(m_Buffer, 0, map, 0, &mappedData); if (FAILED(hr)) { REPORT_ERROR(m_Device.GetLog(), "ID3D11DeviceContext::Map() - FAILED!"); @@ -185,15 +170,15 @@ void BufferD3D11::FinalizeQueries() return; D3D11_MAPPED_SUBRESOURCE mappedData = {}; - HRESULT hr = m_ImmediateContext->Map(m_Buffer, 0, D3D11_MAP_WRITE, 0, &mappedData); + HRESULT hr = m_Device.GetImmediateContext()->Map(m_Buffer, 0, D3D11_MAP_WRITE, 0, &mappedData); if (SUCCEEDED(hr)) { uint8_t* ptr = (uint8_t*)mappedData.pData; ptr += m_QueryRange.bufferOffset; - m_QueryRange.pool->GetData(ptr, m_ImmediateContext, m_QueryRange.offset, m_QueryRange.num); + m_QueryRange.pool->GetData(ptr, m_QueryRange.offset, m_QueryRange.num); - m_ImmediateContext->Unmap(m_Buffer, 0); + m_Device.GetImmediateContext()->Unmap(m_Buffer, 0); } else REPORT_ERROR(m_Device.GetLog(), "ID3D11DeviceContext::Map() - FAILED!"); @@ -209,7 +194,7 @@ void BufferD3D11::FinalizeReadback() m_IsReadbackDataChanged = false; D3D11_MAPPED_SUBRESOURCE srcData = {}; - HRESULT hr = m_ImmediateContext->Map(*m_ReadbackTexture, 0, D3D11_MAP_READ, 0, &srcData); + HRESULT hr = m_Device.GetImmediateContext()->Map(*m_ReadbackTexture, 0, D3D11_MAP_READ, 0, &srcData); if (FAILED(hr)) { REPORT_ERROR(m_Device.GetLog(), "ID3D11DeviceContext::Map() - FAILED!"); @@ -217,10 +202,10 @@ void BufferD3D11::FinalizeReadback() } D3D11_MAPPED_SUBRESOURCE dstData = {}; - hr = m_ImmediateContext->Map(m_Buffer, 0, D3D11_MAP_WRITE, 0, &dstData); + hr = m_Device.GetImmediateContext()->Map(m_Buffer, 0, D3D11_MAP_WRITE, 0, &dstData); if (FAILED(hr)) { - m_ImmediateContext->Unmap(*m_ReadbackTexture, 0); + m_Device.GetImmediateContext()->Unmap(*m_ReadbackTexture, 0); REPORT_ERROR(m_Device.GetLog(), "ID3D11DeviceContext::Map() - FAILED!"); return; } @@ -242,11 +227,11 @@ void BufferD3D11::FinalizeReadback() } - m_ImmediateContext->Unmap(m_Buffer, 0); - m_ImmediateContext->Unmap(*m_ReadbackTexture, 0); + m_Device.GetImmediateContext()->Unmap(m_Buffer, 0); + m_Device.GetImmediateContext()->Unmap(*m_ReadbackTexture, 0); } -TextureD3D11& BufferD3D11::RecreateReadbackTexture(const VersionedDevice& device, const TextureD3D11& srcTexture, const TextureRegionDesc& srcRegionDesc, const TextureDataLayoutDesc& readbackDataLayoutDesc) +TextureD3D11& BufferD3D11::RecreateReadbackTexture(const TextureD3D11& srcTexture, const TextureRegionDesc& srcRegionDesc, const TextureDataLayoutDesc& readbackDataLayoutDesc) { bool isChanged = true; if (m_ReadbackTexture) @@ -279,7 +264,7 @@ TextureD3D11& BufferD3D11::RecreateReadbackTexture(const VersionedDevice& device Deallocate(m_Device.GetStdAllocator(), m_ReadbackTexture); m_ReadbackTexture = Allocate(m_Device.GetStdAllocator(), m_Device, textureDesc); - m_ReadbackTexture->Create(device, nullptr); + m_ReadbackTexture->Create(nullptr); } m_IsReadbackDataChanged = true; @@ -288,10 +273,9 @@ TextureD3D11& BufferD3D11::RecreateReadbackTexture(const VersionedDevice& device return *m_ReadbackTexture; } -inline void BufferD3D11::SetDebugName(const char* name) -{ - SetName(m_Buffer, name); -} +//================================================================================================================ +// NRI +//================================================================================================================ inline void BufferD3D11::GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const { @@ -311,7 +295,7 @@ inline void BufferD3D11::GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc memoryDesc.mustBeDedicated = false; } -void* BufferD3D11::Map(uint64_t offset, uint64_t size) +inline void* BufferD3D11::Map(uint64_t offset, uint64_t size) { MaybeUnused(size); @@ -320,9 +304,9 @@ void* BufferD3D11::Map(uint64_t offset, uint64_t size) void BufferD3D11::Unmap() { - CriticalSection criticalSection(m_ImmediateContext); + CriticalSection criticalSection(m_Device.GetImmediateContext()); - m_ImmediateContext->Unmap(m_Buffer, 0); + m_Device.GetImmediateContext()->Unmap(m_Buffer, 0); } #include "BufferD3D11.hpp" diff --git a/Source/D3D11/BufferD3D11.h b/Source/D3D11/BufferD3D11.h index 88fcd943..72bbb96a 100644 --- a/Source/D3D11/BufferD3D11.h +++ b/Source/D3D11/BufferD3D11.h @@ -12,66 +12,77 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct DeviceD3D11; - struct MemoryD3D11; - struct QueryPoolD3D11; - struct TextureD3D11; - struct QueryRange - { - const QueryPoolD3D11* pool; - uint32_t offset; - uint32_t num; - uint64_t bufferOffset; - }; +struct DeviceD3D11; +struct MemoryD3D11; +struct QueryPoolD3D11; +struct TextureD3D11; + +struct QueryRange +{ + const QueryPoolD3D11* pool; + uint32_t offset; + uint32_t num; + uint64_t bufferOffset; +}; + +struct BufferD3D11 +{ + inline BufferD3D11(DeviceD3D11& device, const BufferDesc& bufferDesc) : + m_Device(device) + , m_Desc(bufferDesc) + {} + + inline BufferD3D11(DeviceD3D11& device) : + m_Device(device) + {} + + inline operator ID3D11Buffer*() const + { return m_Buffer; } + + inline const BufferDesc& GetDesc() const + { return m_Desc; } - struct BufferD3D11 + inline DeviceD3D11& GetDevice() const + { return m_Device; } + + inline void AssignQueryPoolRange(const QueryPoolD3D11* queryPool, uint32_t offset, uint32_t num, uint64_t bufferOffset) { - BufferD3D11(DeviceD3D11& device, const VersionedContext& immediateContext, const BufferDesc& bufferDesc); - BufferD3D11(DeviceD3D11& device, const VersionedContext& immediateContext); - ~BufferD3D11(); - - inline operator ID3D11Buffer*() const - { return m_Buffer; } - - inline const BufferDesc& GetDesc() const - { return m_Desc; } - - inline DeviceD3D11& GetDevice() const - { return m_Device; } - - inline void AssignQueryPoolRange(const QueryPoolD3D11* queryPool, uint32_t offset, uint32_t num, uint64_t bufferOffset) - { - m_QueryRange.pool = queryPool; - m_QueryRange.offset = offset; - m_QueryRange.num = num; - m_QueryRange.bufferOffset = bufferOffset; - } - - Result Create(const VersionedDevice& device, const MemoryD3D11& memory); - Result Create(const BufferD3D11Desc& bufferDesc); - void* Map(MapType mapType, uint64_t offset); - void FinalizeQueries(); - void FinalizeReadback(); - TextureD3D11& RecreateReadbackTexture(const VersionedDevice& device, const TextureD3D11& srcTexture, const TextureRegionDesc& srcRegionDesc, const TextureDataLayoutDesc& readbackDataLayoutDesc); - - //====================================================================================================================== - // NRI - //====================================================================================================================== - void SetDebugName(const char* name); - void GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const; - void* Map(uint64_t offset, uint64_t size); - void Unmap(); - - private: - const VersionedContext& m_ImmediateContext; - ComPtr m_Buffer; - TextureD3D11* m_ReadbackTexture = nullptr; - BufferDesc m_Desc = {}; - BufferType m_Type = BufferType::DEVICE; - QueryRange m_QueryRange = {}; - bool m_IsReadbackDataChanged = false; - TextureDataLayoutDesc m_ReadbackDataLayoutDesc = {}; - DeviceD3D11& m_Device; - }; + m_QueryRange.pool = queryPool; + m_QueryRange.offset = offset; + m_QueryRange.num = num; + m_QueryRange.bufferOffset = bufferOffset; + } + + ~BufferD3D11(); + + Result Create(const MemoryD3D11& memory); + Result Create(const BufferD3D11Desc& bufferDesc); + void* Map(MapType mapType, uint64_t offset); + void FinalizeQueries(); + void FinalizeReadback(); + TextureD3D11& RecreateReadbackTexture(const TextureD3D11& srcTexture, const TextureRegionDesc& srcRegionDesc, const TextureDataLayoutDesc& readbackDataLayoutDesc); + + //================================================================================================================ + // NRI + //================================================================================================================ + + inline void SetDebugName(const char* name) + { SET_D3D_DEBUG_OBJECT_NAME(m_Buffer, name); } + + void GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const; + void* Map(uint64_t offset, uint64_t size); + void Unmap(); + +private: + DeviceD3D11& m_Device; + ComPtr m_Buffer; + TextureD3D11* m_ReadbackTexture = nullptr; + BufferDesc m_Desc = {}; + BufferType m_Type = BufferType::DEVICE; + QueryRange m_QueryRange = {}; + bool m_IsReadbackDataChanged = false; + TextureDataLayoutDesc m_ReadbackDataLayoutDesc = {}; +}; + } diff --git a/Source/D3D11/BufferD3D11.hpp b/Source/D3D11/BufferD3D11.hpp index df2208ed..dfbf6d84 100644 --- a/Source/D3D11/BufferD3D11.hpp +++ b/Source/D3D11/BufferD3D11.hpp @@ -8,7 +8,7 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ CoreInterface ] +#pragma region [ Core ] static void NRI_CALL SetBufferDebugName(Buffer& buffer, const char* name) { @@ -37,13 +37,6 @@ static void NRI_CALL UnmapBuffer(Buffer& buffer) ((BufferD3D11&)buffer).Unmap(); } -void FillFunctionTableBufferD3D11(CoreInterface& coreInterface) -{ - coreInterface.SetBufferDebugName = ::SetBufferDebugName; - coreInterface.GetBufferNativeObject = ::GetBufferNativeObject; - coreInterface.GetBufferMemoryInfo = ::GetBufferMemoryInfo; - coreInterface.MapBuffer = ::MapBuffer; - coreInterface.UnmapBuffer = ::UnmapBuffer; -} - #pragma endregion + +Define_Core_Buffer_PartiallyFillFunctionTable(D3D11) diff --git a/Source/D3D11/CommandAllocatorD3D11.cpp b/Source/D3D11/CommandAllocatorD3D11.cpp index cc635f70..7a86b22f 100644 --- a/Source/D3D11/CommandAllocatorD3D11.cpp +++ b/Source/D3D11/CommandAllocatorD3D11.cpp @@ -8,22 +8,26 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#include "SharedExternal.h" #include "SharedD3D11.h" #include "CommandAllocatorD3D11.h" - #include "CommandBufferD3D11.h" #include "CommandBufferEmuD3D11.h" using namespace nri; -Result CreateCommandBuffer(DeviceD3D11& deviceImpl, ID3D11DeviceContext* precreatedContext, CommandBuffer*& commandBuffer) +Result CreateCommandBuffer(DeviceD3D11& device, ID3D11DeviceContext* precreatedContext, CommandBuffer*& commandBuffer) { + bool isImmediate = false; + if (precreatedContext) + isImmediate = precreatedContext->GetType() == D3D11_DEVICE_CONTEXT_IMMEDIATE; + else + isImmediate = device.GetDevice().isDeferredContextEmulated; + void* impl; - if (deviceImpl.GetDevice().isDeferredContextsEmulated) - impl = Allocate(deviceImpl.GetStdAllocator(), deviceImpl); + if (isImmediate) + impl = Allocate(device.GetStdAllocator(), device); else - impl = Allocate(deviceImpl.GetStdAllocator(), deviceImpl); + impl = Allocate(device.GetStdAllocator(), device); const nri::Result result = ((CommandBufferHelper*)impl)->Create(precreatedContext); @@ -33,36 +37,21 @@ Result CreateCommandBuffer(DeviceD3D11& deviceImpl, ID3D11DeviceContext* precrea return nri::Result::SUCCESS; } - if (deviceImpl.GetDevice().isDeferredContextsEmulated) - Deallocate(deviceImpl.GetStdAllocator(), (CommandBufferEmuD3D11*)impl); + if (isImmediate) + Deallocate(device.GetStdAllocator(), (CommandBufferEmuD3D11*)impl); else - Deallocate(deviceImpl.GetStdAllocator(), (CommandBufferD3D11*)impl); + Deallocate(device.GetStdAllocator(), (CommandBufferD3D11*)impl); return result; } -CommandAllocatorD3D11::CommandAllocatorD3D11(DeviceD3D11& device, const VersionedDevice& versionedDevice) : - m_Device(device) -{ - MaybeUnused(versionedDevice); -} - -CommandAllocatorD3D11::~CommandAllocatorD3D11() -{ -} - -inline void CommandAllocatorD3D11::SetDebugName(const char* name) -{ - MaybeUnused(name); -} - inline Result CommandAllocatorD3D11::CreateCommandBuffer(CommandBuffer*& commandBuffer) { return ::CreateCommandBuffer(m_Device, nullptr, commandBuffer); } -inline void CommandAllocatorD3D11::Reset() -{ -} +//================================================================================================================ +// NRI +//================================================================================================================ #include "CommandAllocatorD3D11.hpp" diff --git a/Source/D3D11/CommandAllocatorD3D11.h b/Source/D3D11/CommandAllocatorD3D11.h index b8e05dab..f74aceb2 100644 --- a/Source/D3D11/CommandAllocatorD3D11.h +++ b/Source/D3D11/CommandAllocatorD3D11.h @@ -12,26 +12,37 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct DeviceD3D11; - - struct CommandAllocatorD3D11 - { - CommandAllocatorD3D11(DeviceD3D11& device, const VersionedDevice& versionedDevice); - ~CommandAllocatorD3D11(); - - inline DeviceD3D11& GetDevice() const - { return m_Device; } - - //====================================================================================================================== - // NRI - //====================================================================================================================== - void SetDebugName(const char* name); - Result CreateCommandBuffer(CommandBuffer*& commandBuffer); - void Reset(); - - private: - DeviceD3D11& m_Device; - }; + +struct DeviceD3D11; + +struct CommandAllocatorD3D11 +{ + inline CommandAllocatorD3D11(DeviceD3D11& device) : + m_Device(device) + {} + + inline ~CommandAllocatorD3D11() + {} + + inline DeviceD3D11& GetDevice() const + { return m_Device; } + + //================================================================================================================ + // NRI + //================================================================================================================ + + inline void SetDebugName(const char* name) + { MaybeUnused(name); } + + inline void Reset() + {} + + Result CreateCommandBuffer(CommandBuffer*& commandBuffer); + +private: + DeviceD3D11& m_Device; +}; + } nri::Result CreateCommandBuffer(nri::DeviceD3D11& deviceImpl, ID3D11DeviceContext* precreatedContext, nri::CommandBuffer*& commandBuffer); diff --git a/Source/D3D11/CommandAllocatorD3D11.hpp b/Source/D3D11/CommandAllocatorD3D11.hpp index e77479b9..e261a504 100644 --- a/Source/D3D11/CommandAllocatorD3D11.hpp +++ b/Source/D3D11/CommandAllocatorD3D11.hpp @@ -8,7 +8,7 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ CoreInterface ] +#pragma region [ Core ] static void NRI_CALL SetCommandAllocatorDebugName(CommandAllocator& commandAllocator, const char* name) { @@ -25,11 +25,6 @@ static void NRI_CALL ResetCommandAllocator(CommandAllocator& commandAllocator) ((CommandAllocatorD3D11&)commandAllocator).Reset(); } -void FillFunctionTableCommandAllocatorD3D11(CoreInterface& coreInterface) -{ - coreInterface.SetCommandAllocatorDebugName = ::SetCommandAllocatorDebugName; - coreInterface.CreateCommandBuffer = ::CreateCommandBuffer; - coreInterface.ResetCommandAllocator = ::ResetCommandAllocator; -} - #pragma endregion + +Define_Core_CommandAllocator_PartiallyFillFunctionTable(D3D11) \ No newline at end of file diff --git a/Source/D3D11/CommandBufferD3D11.cpp b/Source/D3D11/CommandBufferD3D11.cpp index ba27c0a2..989a7e6d 100644 --- a/Source/D3D11/CommandBufferD3D11.cpp +++ b/Source/D3D11/CommandBufferD3D11.cpp @@ -8,10 +8,8 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#include "SharedExternal.h" #include "SharedD3D11.h" #include "CommandBufferD3D11.h" - #include "BufferD3D11.h" #include "DescriptorD3D11.h" #include "DescriptorSetD3D11.h" @@ -25,26 +23,23 @@ using namespace nri; static constexpr uint64_t s_nullOffsets[D3D11_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT] = {0}; -CommandBufferD3D11::CommandBufferD3D11(DeviceD3D11& deviceImpl) : - m_Device(deviceImpl.GetDevice()), - m_DeviceImpl(deviceImpl) -{ -} - -CommandBufferD3D11::CommandBufferD3D11(DeviceD3D11& deviceImpl, const VersionedContext& immediateContext) : - m_Device(deviceImpl.GetDevice()), - m_Context(immediateContext), - m_DeviceImpl(deviceImpl) +CommandBufferD3D11::CommandBufferD3D11(DeviceD3D11& device) : + m_Device(device), + m_DeferredContext(device.GetImmediateContext()) { - m_Context->QueryInterface(IID_PPV_ARGS(&m_Annotation)); - m_Context.ext->BeginUAVOverlap(m_Context); + m_DeferredContext->QueryInterface(IID_PPV_ARGS(&m_Annotation)); + m_DeferredContext.ext->BeginUAVOverlap(m_DeferredContext); } CommandBufferD3D11::~CommandBufferD3D11() { - m_Context.ext->EndUAVOverlap(m_Context); + m_DeferredContext.ext->EndUAVOverlap(m_DeferredContext); } +//================================================================================================================ +// CommandBufferHelper +//================================================================================================================ + Result CommandBufferD3D11::Create(ID3D11DeviceContext* precreatedContext) { HRESULT hr; @@ -52,56 +47,69 @@ Result CommandBufferD3D11::Create(ID3D11DeviceContext* precreatedContext) if (!precreatedContext) { - hr = m_Device->CreateDeferredContext(0, &context); - RETURN_ON_BAD_HRESULT(m_DeviceImpl.GetLog(), hr, "ID3D11Device::CreateDeferredContext() - FAILED!"); + hr = m_Device.GetDevice()->CreateDeferredContext(0, &context); + RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "ID3D11Device::CreateDeferredContext() - FAILED!"); } - hr = context->QueryInterface(__uuidof(ID3D11DeviceContext4), (void**)&m_Context.ptr); - m_Context.version = 4; - m_Context.ext = m_Device.ext; + // Release inherited interfaces from the immediate context + m_DeferredContext.ptr = nullptr; + m_Annotation = nullptr; + + hr = context->QueryInterface(__uuidof(ID3D11DeviceContext4), (void**)&m_DeferredContext.ptr); + m_DeferredContext.version = 4; + m_DeferredContext.ext = m_Device.GetDevice().ext; if (FAILED(hr)) { - REPORT_WARNING(m_DeviceImpl.GetLog(), "QueryInterface(ID3D11DeviceContext4) - FAILED!"); - hr = context->QueryInterface(__uuidof(ID3D11DeviceContext3), (void**)&m_Context.ptr); - m_Context.version = 3; + REPORT_WARNING(m_Device.GetLog(), "QueryInterface(ID3D11DeviceContext4) - FAILED!"); + hr = context->QueryInterface(__uuidof(ID3D11DeviceContext3), (void**)&m_DeferredContext.ptr); + m_DeferredContext.version = 3; if (FAILED(hr)) { - REPORT_WARNING(m_DeviceImpl.GetLog(), "QueryInterface(ID3D11DeviceContext3) - FAILED!"); - hr = context->QueryInterface(__uuidof(ID3D11DeviceContext2), (void**)&m_Context.ptr); - m_Context.version = 2; + REPORT_WARNING(m_Device.GetLog(), "QueryInterface(ID3D11DeviceContext3) - FAILED!"); + hr = context->QueryInterface(__uuidof(ID3D11DeviceContext2), (void**)&m_DeferredContext.ptr); + m_DeferredContext.version = 2; if (FAILED(hr)) { - REPORT_WARNING(m_DeviceImpl.GetLog(), "QueryInterface(ID3D11DeviceContext2) - FAILED!"); - hr = context->QueryInterface(__uuidof(ID3D11DeviceContext1), (void**)&m_Context.ptr); - m_Context.version = 1; + REPORT_WARNING(m_Device.GetLog(), "QueryInterface(ID3D11DeviceContext2) - FAILED!"); + hr = context->QueryInterface(__uuidof(ID3D11DeviceContext1), (void**)&m_DeferredContext.ptr); + m_DeferredContext.version = 1; if (FAILED(hr)) { - REPORT_WARNING(m_DeviceImpl.GetLog(), "QueryInterface(ID3D11DeviceContext1) - FAILED!"); - m_Context.ptr = (ID3D11DeviceContext4*)context.GetInterface(); - m_Context.version = 0; + REPORT_WARNING(m_Device.GetLog(), "QueryInterface(ID3D11DeviceContext1) - FAILED!"); + m_DeferredContext.ptr = (ID3D11DeviceContext4*)context.GetInterface(); + m_DeferredContext.version = 0; } } } } - hr = m_Context->QueryInterface(IID_PPV_ARGS(&m_Annotation)); - RETURN_ON_BAD_HRESULT(m_DeviceImpl.GetLog(), hr, "QueryInterface(ID3DUserDefinedAnnotation) - FAILED!"); + hr = m_DeferredContext->QueryInterface(IID_PPV_ARGS(&m_Annotation)); + RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "QueryInterface(ID3DUserDefinedAnnotation) - FAILED!"); - m_Context.ext->BeginUAVOverlap(m_Context); + m_DeferredContext.ext->BeginUAVOverlap(m_DeferredContext); return Result::SUCCESS; } -void CommandBufferD3D11::Submit(const VersionedContext& immediateContext) +void CommandBufferD3D11::Submit() +{ + m_Device.GetImmediateContext()->ExecuteCommandList(m_CommandList, FALSE); +} + +ID3D11DeviceContext* CommandBufferD3D11::GetNativeObject() const { - immediateContext->ExecuteCommandList(m_CommandList, FALSE); + return m_DeferredContext.ptr; } -void CommandBufferD3D11::SetDebugName(const char* name) +StdAllocator& CommandBufferD3D11::GetStdAllocator() const { - SetName(m_Context.ptr, name); + return m_Device.GetStdAllocator(); } +//================================================================================================================ +// NRI +//================================================================================================================ + Result CommandBufferD3D11::Begin(const DescriptorPool* descriptorPool) { m_SamplePositionsState.Reset(); @@ -122,17 +130,17 @@ Result CommandBufferD3D11::Begin(const DescriptorPool* descriptorPool) Result CommandBufferD3D11::End() { - HRESULT hr = m_Context->FinishCommandList(FALSE, &m_CommandList); - RETURN_ON_BAD_HRESULT(m_DeviceImpl.GetLog(), hr, "ID3D11DeviceContext::FinishCommandList() - FAILED!"); + HRESULT hr = m_DeferredContext->FinishCommandList(FALSE, &m_CommandList); + RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "ID3D11DeviceContext::FinishCommandList() - FAILED!"); - m_BindingState.UnbindAndReset(m_Context); + m_BindingState.UnbindAndReset(m_DeferredContext); return Result::SUCCESS; } void CommandBufferD3D11::SetViewports(const Viewport* viewports, uint32_t viewportNum) { - m_Context->RSSetViewports(viewportNum, (const D3D11_VIEWPORT*)viewports); + m_DeferredContext->RSSetViewports(viewportNum, (const D3D11_VIEWPORT*)viewports); } void CommandBufferD3D11::SetScissors(const Rect* rects, uint32_t rectNum) @@ -146,14 +154,14 @@ void CommandBufferD3D11::SetScissors(const Rect* rects, uint32_t rectNum) } if (!m_CurrentPipeline || !m_CurrentPipeline->IsRasterizerDiscarded()) - m_Context->RSSetScissorRects(rectNum, &winRect[0]); + m_DeferredContext->RSSetScissorRects(rectNum, &winRect[0]); } void CommandBufferD3D11::SetDepthBounds(float boundsMin, float boundsMax) { if (m_DepthBounds[0] != boundsMin || m_DepthBounds[1] != boundsMax) { - m_Context.ext->SetDepthBounds(m_Context, boundsMin, boundsMax); + m_DeferredContext.ext->SetDepthBounds(m_DeferredContext, boundsMin, boundsMax); m_DepthBounds[0] = boundsMin; m_DepthBounds[1] = boundsMax; @@ -164,21 +172,21 @@ void CommandBufferD3D11::SetStencilReference(uint8_t reference) { m_StencilRef = reference; - if (m_CurrentPipeline && m_CurrentPipeline->IsValid()) - m_CurrentPipeline->ChangeStencilReference(m_Context, m_StencilRef, DynamicState::BIND_AND_SET); + if (m_CurrentPipeline) + m_CurrentPipeline->ChangeStencilReference(m_DeferredContext, m_StencilRef, DynamicState::BIND_AND_SET); } void CommandBufferD3D11::SetSamplePositions(const SamplePosition* positions, uint32_t positionNum) { m_SamplePositionsState.Set(positions, positionNum); - if (m_CurrentPipeline && m_CurrentPipeline->IsValid()) - m_CurrentPipeline->ChangeSamplePositions(m_Context, m_SamplePositionsState, DynamicState::BIND_AND_SET); + if (m_CurrentPipeline) + m_CurrentPipeline->ChangeSamplePositions(m_DeferredContext, m_SamplePositionsState, DynamicState::BIND_AND_SET); } void CommandBufferD3D11::ClearAttachments(const ClearDesc* clearDescs, uint32_t clearDescNum, const Rect* rects, uint32_t rectNum) { - m_CurrentFrameBuffer->ClearAttachments(m_Context, clearDescs, clearDescNum, rects, rectNum); + m_CurrentFrameBuffer->ClearAttachments(m_DeferredContext, clearDescs, clearDescNum, rects, rectNum); } void CommandBufferD3D11::ClearStorageBuffer(const ClearStorageBufferDesc& clearDesc) @@ -186,7 +194,7 @@ void CommandBufferD3D11::ClearStorageBuffer(const ClearStorageBufferDesc& clearD const DescriptorD3D11& descriptor = *(const DescriptorD3D11*)clearDesc.storageBuffer; Color32ui clearValue = {clearDesc.value, clearDesc.value, clearDesc.value, clearDesc.value}; - m_Context->ClearUnorderedAccessViewUint(descriptor, &clearValue.x); + m_DeferredContext->ClearUnorderedAccessViewUint(descriptor, &clearValue.x); } void CommandBufferD3D11::ClearStorageTexture(const ClearStorageTextureDesc& clearDesc) @@ -194,20 +202,20 @@ void CommandBufferD3D11::ClearStorageTexture(const ClearStorageTextureDesc& clea const DescriptorD3D11& descriptor = *(const DescriptorD3D11*)clearDesc.storageTexture; if (descriptor.IsIntegerFormat()) - m_Context->ClearUnorderedAccessViewUint(descriptor, &clearDesc.value.color32ui.x); + m_DeferredContext->ClearUnorderedAccessViewUint(descriptor, &clearDesc.value.color32ui.x); else - m_Context->ClearUnorderedAccessViewFloat(descriptor, &clearDesc.value.color32f.x); + m_DeferredContext->ClearUnorderedAccessViewFloat(descriptor, &clearDesc.value.color32f.x); } void CommandBufferD3D11::BeginRenderPass(const FrameBuffer& frameBuffer, RenderPassBeginFlag renderPassBeginFlag) { m_CurrentFrameBuffer = (FrameBufferD3D11*)&frameBuffer; - m_CurrentFrameBuffer->Bind(m_Context, renderPassBeginFlag); + m_CurrentFrameBuffer->Bind(m_DeferredContext, renderPassBeginFlag); } void CommandBufferD3D11::EndRenderPass() { - m_Context->OMSetRenderTargets(0, nullptr, nullptr); + m_DeferredContext->OMSetRenderTargets(0, nullptr, nullptr); m_CurrentFrameBuffer = nullptr; } @@ -238,7 +246,7 @@ void CommandBufferD3D11::SetVertexBuffers(uint32_t baseSlot, uint32_t bufferNum, offsetsUint[i] = (uint32_t)offsets[i]; } - m_Context->IASetVertexBuffers(baseSlot, bufferNum, buf, strides, offsetsUint); + m_DeferredContext->IASetVertexBuffers(baseSlot, bufferNum, buf, strides, offsetsUint); m_CurrentVertexBuffer = buffers[0]; m_CurrentVertexBufferOffset = offsets[0]; @@ -253,7 +261,7 @@ void CommandBufferD3D11::SetIndexBuffer(const Buffer& buffer, uint64_t offset, I const BufferD3D11& bufferD3D11 = (BufferD3D11&)buffer; const DXGI_FORMAT format = indexType == IndexType::UINT16 ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT; - m_Context->IASetIndexBuffer(bufferD3D11, format, (uint32_t)offset); + m_DeferredContext->IASetIndexBuffer(bufferD3D11, format, (uint32_t)offset); m_CurrentIndexBuffer = &buffer; m_CurrentIndexBufferOffset = offset; @@ -264,7 +272,7 @@ void CommandBufferD3D11::SetIndexBuffer(const Buffer& buffer, uint64_t offset, I void CommandBufferD3D11::SetPipelineLayout(const PipelineLayout& pipelineLayout) { PipelineLayoutD3D11* pipelineLayoutD3D11 = (PipelineLayoutD3D11*)&pipelineLayout; - pipelineLayoutD3D11->Bind(m_Context); + pipelineLayoutD3D11->Bind(m_DeferredContext); m_CurrentPipelineLayout = pipelineLayoutD3D11; } @@ -272,9 +280,9 @@ void CommandBufferD3D11::SetPipelineLayout(const PipelineLayout& pipelineLayout) void CommandBufferD3D11::SetPipeline(const Pipeline& pipeline) { PipelineD3D11* pipelineD3D11 = (PipelineD3D11*)&pipeline; - pipelineD3D11->ChangeSamplePositions(m_Context, m_SamplePositionsState, DynamicState::SET_ONLY); - pipelineD3D11->ChangeStencilReference(m_Context, m_StencilRef, DynamicState::SET_ONLY); - pipelineD3D11->Bind(m_Context, m_CurrentPipeline); + pipelineD3D11->ChangeSamplePositions(m_DeferredContext, m_SamplePositionsState, DynamicState::SET_ONLY); + pipelineD3D11->ChangeStencilReference(m_DeferredContext, m_StencilRef, DynamicState::SET_ONLY); + pipelineD3D11->Bind(m_DeferredContext, m_CurrentPipeline); m_CurrentPipeline = pipelineD3D11; } @@ -287,32 +295,32 @@ void CommandBufferD3D11::SetDescriptorPool(const DescriptorPool& descriptorPool) void CommandBufferD3D11::SetDescriptorSet(uint32_t setIndexInPipelineLayout, const DescriptorSet& descriptorSet, const uint32_t* dynamicConstantBufferOffsets) { const DescriptorSetD3D11& descriptorSetImpl = (DescriptorSetD3D11&)descriptorSet; - m_CurrentPipelineLayout->BindDescriptorSet(m_BindingState, m_Context, setIndexInPipelineLayout, descriptorSetImpl, dynamicConstantBufferOffsets); + m_CurrentPipelineLayout->BindDescriptorSet(m_BindingState, m_DeferredContext, setIndexInPipelineLayout, descriptorSetImpl, dynamicConstantBufferOffsets); } void CommandBufferD3D11::SetConstants(uint32_t pushConstantIndex, const void* data, uint32_t size) { - m_CurrentPipelineLayout->SetConstants(m_Context, pushConstantIndex, (const Vec4*)data, size); + m_CurrentPipelineLayout->SetConstants(m_DeferredContext, pushConstantIndex, (const Vec4*)data, size); } void CommandBufferD3D11::Draw(uint32_t vertexNum, uint32_t instanceNum, uint32_t baseVertex, uint32_t baseInstance) { - m_Context->DrawInstanced(vertexNum, instanceNum, baseVertex, baseInstance); + m_DeferredContext->DrawInstanced(vertexNum, instanceNum, baseVertex, baseInstance); } void CommandBufferD3D11::DrawIndexed(uint32_t indexNum, uint32_t instanceNum, uint32_t baseIndex, uint32_t baseVertex, uint32_t baseInstance) { - m_Context->DrawIndexedInstanced(indexNum, instanceNum, baseIndex, baseVertex, baseInstance); + m_DeferredContext->DrawIndexedInstanced(indexNum, instanceNum, baseIndex, baseVertex, baseInstance); } void CommandBufferD3D11::DrawIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride) { - m_Context.ext->MultiDrawIndirect(m_Context, (BufferD3D11&)buffer, offset, drawNum, stride); + m_DeferredContext.ext->MultiDrawIndirect(m_DeferredContext, (BufferD3D11&)buffer, offset, drawNum, stride); } void CommandBufferD3D11::DrawIndexedIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride) { - m_Context.ext->MultiDrawIndexedIndirect(m_Context, (BufferD3D11&)buffer, offset, drawNum, stride); + m_DeferredContext.ext->MultiDrawIndexedIndirect(m_DeferredContext, (BufferD3D11&)buffer, offset, drawNum, stride); } void CommandBufferD3D11::CopyBuffer(Buffer& dstBuffer, uint64_t dstOffset, const Buffer& srcBuffer, uint64_t srcOffset, uint64_t size) @@ -328,7 +336,7 @@ void CommandBufferD3D11::CopyBuffer(Buffer& dstBuffer, uint64_t dstOffset, const isEntireResource &= dst.GetDesc().size == size; if (isEntireResource) - m_Context->CopyResource(dst, src); + m_DeferredContext->CopyResource(dst, src); else { D3D11_BOX box = {}; @@ -337,7 +345,7 @@ void CommandBufferD3D11::CopyBuffer(Buffer& dstBuffer, uint64_t dstOffset, const box.bottom = 1; box.back = 1; - m_Context->CopySubresourceRegion(dst, 0, (uint32_t)dstOffset, 0, 0, src, 0, &box); + m_DeferredContext->CopySubresourceRegion(dst, 0, (uint32_t)dstOffset, 0, 0, src, 0, &box); } } @@ -346,13 +354,10 @@ void CommandBufferD3D11::CopyTexture(Texture& dstTexture, const TextureRegionDes TextureD3D11& dst = (TextureD3D11&)dstTexture; TextureD3D11& src = (TextureD3D11&)srcTexture; - if ( (!dstRegionDesc && !srcRegionDesc) || (dstRegionDesc->mipOffset == NULL_TEXTURE_REGION_DESC && srcRegionDesc->mipOffset == NULL_TEXTURE_REGION_DESC) ) - m_Context->CopyResource(dst, src); + if ( (!dstRegionDesc || !srcRegionDesc) || (dstRegionDesc->mipOffset == NULL_TEXTURE_REGION_DESC && srcRegionDesc->mipOffset == NULL_TEXTURE_REGION_DESC) ) + m_DeferredContext->CopyResource(dst, src); else { - assert(dstRegionDesc != nullptr); - assert(srcRegionDesc != nullptr); - D3D11_BOX srcBox = {}; srcBox.left = srcRegionDesc->offset[0]; srcBox.top = srcRegionDesc->offset[1]; @@ -367,7 +372,7 @@ void CommandBufferD3D11::CopyTexture(Texture& dstTexture, const TextureRegionDes uint32_t dstSubresource = dst.GetSubresourceIndex(*dstRegionDesc); uint32_t srcSubresource = src.GetSubresourceIndex(*srcRegionDesc); - m_Context->CopySubresourceRegion(dst, dstSubresource, dstRegionDesc->offset[0], dstRegionDesc->offset[1], + m_DeferredContext->CopySubresourceRegion(dst, dstSubresource, dstRegionDesc->offset[0], dstRegionDesc->offset[1], dstRegionDesc->offset[2], src, srcSubresource, &srcBox); } } @@ -391,18 +396,18 @@ void CommandBufferD3D11::UploadBufferToTexture(Texture& dstTexture, const Textur uint32_t dstSubresource = dst.GetSubresourceIndex(dstRegionDesc); uint8_t* data = (uint8_t*)src.Map(MapType::READ, srcDataLayoutDesc.offset); - m_Context->UpdateSubresource(dst, dstSubresource, &dstBox, data, srcDataLayoutDesc.rowPitch, srcDataLayoutDesc.slicePitch); + m_DeferredContext->UpdateSubresource(dst, dstSubresource, &dstBox, data, srcDataLayoutDesc.rowPitch, srcDataLayoutDesc.slicePitch); src.Unmap(); } void CommandBufferD3D11::ReadbackTextureToBuffer(Buffer& dstBuffer, TextureDataLayoutDesc& dstDataLayoutDesc, const Texture& srcTexture, const TextureRegionDesc& srcRegionDesc) { - CHECK(m_DeviceImpl.GetLog(), dstDataLayoutDesc.offset == 0, "D3D11 implementation currently supports copying a texture region to a buffer only with offset = 0!"); + CHECK(m_Device.GetLog(), dstDataLayoutDesc.offset == 0, "D3D11 implementation currently supports copying a texture region to a buffer only with offset = 0!"); BufferD3D11& dst = (BufferD3D11&)dstBuffer; TextureD3D11& src = (TextureD3D11&)srcTexture; - TextureD3D11& dstTemp = dst.RecreateReadbackTexture(m_Device, src, srcRegionDesc, dstDataLayoutDesc); + TextureD3D11& dstTemp = dst.RecreateReadbackTexture(src, srcRegionDesc, dstDataLayoutDesc); TextureRegionDesc dstRegionDesc = {}; dstRegionDesc.mipOffset = srcRegionDesc.mipOffset; @@ -416,12 +421,12 @@ void CommandBufferD3D11::ReadbackTextureToBuffer(Buffer& dstBuffer, TextureDataL void CommandBufferD3D11::Dispatch(uint32_t x, uint32_t y, uint32_t z) { - m_Context->Dispatch(x, y, z); + m_DeferredContext->Dispatch(x, y, z); } void CommandBufferD3D11::DispatchIndirect(const Buffer& buffer, uint64_t offset) { - m_Context->DispatchIndirect((BufferD3D11&)buffer, (uint32_t)offset); + m_DeferredContext->DispatchIndirect((BufferD3D11&)buffer, (uint32_t)offset); } void CommandBufferD3D11::PipelineBarrier(const TransitionBarrierDesc* transitionBarriers, const AliasingBarrierDesc* aliasingBarriers, BarrierDependency dependency) @@ -453,17 +458,17 @@ void CommandBufferD3D11::PipelineBarrier(const TransitionBarrierDesc* transition } if (result != NO_WFI) - m_Context.ext->WaitForDrain(m_Context, result); + m_DeferredContext.ext->WaitForDrain(m_DeferredContext, result); } void CommandBufferD3D11::BeginQuery(const QueryPool& queryPool, uint32_t offset) { - ((QueryPoolD3D11&)queryPool).BeginQuery(m_Context, offset); + ((QueryPoolD3D11&)queryPool).BeginQuery(m_DeferredContext, offset); } void CommandBufferD3D11::EndQuery(const QueryPool& queryPool, uint32_t offset) { - ((QueryPoolD3D11&)queryPool).EndQuery(m_Context, offset); + ((QueryPoolD3D11&)queryPool).EndQuery(m_DeferredContext, offset); } void CommandBufferD3D11::CopyQueries(const QueryPool& queryPool, uint32_t offset, uint32_t num, Buffer& dstBuffer, uint64_t dstOffset) @@ -485,9 +490,4 @@ void CommandBufferD3D11::EndAnnotation() m_Annotation->EndEvent(); } -StdAllocator& CommandBufferD3D11::GetStdAllocator() const -{ - return m_DeviceImpl.GetStdAllocator(); -} - #include "CommandBufferD3D11.hpp" diff --git a/Source/D3D11/CommandBufferD3D11.h b/Source/D3D11/CommandBufferD3D11.h index c29ee413..102679ab 100644 --- a/Source/D3D11/CommandBufferD3D11.h +++ b/Source/D3D11/CommandBufferD3D11.h @@ -12,88 +12,96 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct FrameBufferD3D11; - struct PipelineLayoutD3D11; - struct PipelineD3D11; - struct BufferD3D11; - struct CommandBufferD3D11 final : public CommandBufferHelper - { - CommandBufferD3D11(DeviceD3D11& deviceImpl); - CommandBufferD3D11(DeviceD3D11& deviceImpl, const VersionedContext& immediateContext); - ~CommandBufferD3D11(); +struct FrameBufferD3D11; +struct PipelineLayoutD3D11; +struct PipelineD3D11; +struct BufferD3D11; + +struct CommandBufferD3D11 final : public CommandBufferHelper +{ + CommandBufferD3D11(DeviceD3D11& device); + ~CommandBufferD3D11(); + + inline DeviceD3D11& GetDevice() const + { return m_Device; } - inline const VersionedContext& GetContext() const - { return m_Context; } + inline operator ID3D11CommandList*() const + { return m_CommandList.GetInterface(); } - inline operator ID3D11CommandList*() const - { return m_CommandList.GetInterface(); } + //================================================================================================================ + // CommandBufferHelper + //================================================================================================================ + + Result Create(ID3D11DeviceContext* precreatedContext); + void Submit(); + ID3D11DeviceContext* GetNativeObject() const; + StdAllocator& GetStdAllocator() const; + + //================================================================================================================ + // NRI + //================================================================================================================ + + inline void SetDebugName(const char* name) + { + SET_D3D_DEBUG_OBJECT_NAME(m_DeferredContext.ptr, name); + SET_D3D_DEBUG_OBJECT_NAME(m_CommandList, name); + } - //====================================================================================================================== - // CommandBufferHelper - //====================================================================================================================== - Result Create(ID3D11DeviceContext* precreatedContext); - void Submit(const VersionedContext& context); - StdAllocator& GetStdAllocator() const; + Result Begin(const DescriptorPool* descriptorPool); + Result End(); + void SetViewports(const Viewport* viewports, uint32_t viewportNum); + void SetScissors(const Rect* rects, uint32_t rectNum); + void SetDepthBounds(float boundsMin, float boundsMax); + void SetStencilReference(uint8_t reference); + void SetSamplePositions(const SamplePosition* positions, uint32_t positionNum); + void ClearAttachments(const ClearDesc* clearDescs, uint32_t clearDescNum, const Rect* rects, uint32_t rectNum); + void ClearStorageBuffer(const ClearStorageBufferDesc& clearDesc); + void ClearStorageTexture(const ClearStorageTextureDesc& clearDesc); + void BeginRenderPass(const FrameBuffer& frameBuffer, RenderPassBeginFlag renderPassBeginFlag); + void EndRenderPass(); + void SetVertexBuffers(uint32_t baseSlot, uint32_t bufferNum, const Buffer* const* buffers, const uint64_t* offsets); + void SetIndexBuffer(const Buffer& buffer, uint64_t offset, IndexType indexType); + void SetPipelineLayout(const PipelineLayout& pipelineLayout); + void SetPipeline(const Pipeline& pipeline); + void SetDescriptorPool(const DescriptorPool& descriptorPool); + void SetDescriptorSet(uint32_t setIndexInPipelineLayout, const DescriptorSet& descriptorSet, const uint32_t* dynamicConstantBufferOffsets); + void SetConstants(uint32_t pushConstantIndex, const void* data, uint32_t size); + void Draw(uint32_t vertexNum, uint32_t instanceNum, uint32_t baseVertex, uint32_t baseInstance); + void DrawIndexed(uint32_t indexNum, uint32_t instanceNum, uint32_t baseIndex, uint32_t baseVertex, uint32_t baseInstance); + void DrawIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride); + void DrawIndexedIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride); + void CopyBuffer(Buffer& dstBuffer, uint64_t dstOffset, const Buffer& srcBuffer, uint64_t srcOffset, uint64_t size); + void CopyTexture(Texture& dstTexture, const TextureRegionDesc* dstRegionDesc, const Texture& srcTexture, const TextureRegionDesc* srcRegionDesc); + void UploadBufferToTexture(Texture& dstTexture, const TextureRegionDesc& dstRegionDesc, const Buffer& srcBuffer, const TextureDataLayoutDesc& srcDataLayoutDesc); + void ReadbackTextureToBuffer(Buffer& dstBuffer, TextureDataLayoutDesc& dstDataLayoutDesc, const Texture& srcTexture, const TextureRegionDesc& srcRegionDesc); + void Dispatch(uint32_t x, uint32_t y, uint32_t z); + void DispatchIndirect(const Buffer& buffer, uint64_t offset); + void PipelineBarrier(const TransitionBarrierDesc* transitionBarriers, const AliasingBarrierDesc* aliasingBarriers, BarrierDependency dependency); + void BeginQuery(const QueryPool& queryPool, uint32_t offset); + void EndQuery(const QueryPool& queryPool, uint32_t offset); + void CopyQueries(const QueryPool& queryPool, uint32_t offset, uint32_t num, Buffer& dstBuffer, uint64_t dstOffset); + void BeginAnnotation(const char* name); + void EndAnnotation(); - //====================================================================================================================== - // NRI - //====================================================================================================================== - void SetDebugName(const char* name); - Result Begin(const DescriptorPool* descriptorPool); - Result End(); - void SetViewports(const Viewport* viewports, uint32_t viewportNum); - void SetScissors(const Rect* rects, uint32_t rectNum); - void SetDepthBounds(float boundsMin, float boundsMax); - void SetStencilReference(uint8_t reference); - void SetSamplePositions(const SamplePosition* positions, uint32_t positionNum); - void ClearAttachments(const ClearDesc* clearDescs, uint32_t clearDescNum, const Rect* rects, uint32_t rectNum); - void ClearStorageBuffer(const ClearStorageBufferDesc& clearDesc); - void ClearStorageTexture(const ClearStorageTextureDesc& clearDesc); - void BeginRenderPass(const FrameBuffer& frameBuffer, RenderPassBeginFlag renderPassBeginFlag); - void EndRenderPass(); - void SetVertexBuffers(uint32_t baseSlot, uint32_t bufferNum, const Buffer* const* buffers, const uint64_t* offsets); - void SetIndexBuffer(const Buffer& buffer, uint64_t offset, IndexType indexType); - void SetPipelineLayout(const PipelineLayout& pipelineLayout); - void SetPipeline(const Pipeline& pipeline); - void SetDescriptorPool(const DescriptorPool& descriptorPool); - void SetDescriptorSet(uint32_t setIndexInPipelineLayout, const DescriptorSet& descriptorSet, const uint32_t* dynamicConstantBufferOffsets); - void SetConstants(uint32_t pushConstantIndex, const void* data, uint32_t size); - void Draw(uint32_t vertexNum, uint32_t instanceNum, uint32_t baseVertex, uint32_t baseInstance); - void DrawIndexed(uint32_t indexNum, uint32_t instanceNum, uint32_t baseIndex, uint32_t baseVertex, uint32_t baseInstance); - void DrawIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride); - void DrawIndexedIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride); - void CopyBuffer(Buffer& dstBuffer, uint64_t dstOffset, const Buffer& srcBuffer, uint64_t srcOffset, uint64_t size); - void CopyTexture(Texture& dstTexture, const TextureRegionDesc* dstRegionDesc, const Texture& srcTexture, const TextureRegionDesc* srcRegionDesc); - void UploadBufferToTexture(Texture& dstTexture, const TextureRegionDesc& dstRegionDesc, const Buffer& srcBuffer, const TextureDataLayoutDesc& srcDataLayoutDesc); - void ReadbackTextureToBuffer(Buffer& dstBuffer, TextureDataLayoutDesc& dstDataLayoutDesc, const Texture& srcTexture, const TextureRegionDesc& srcRegionDesc); - void Dispatch(uint32_t x, uint32_t y, uint32_t z); - void DispatchIndirect(const Buffer& buffer, uint64_t offset); - void PipelineBarrier(const TransitionBarrierDesc* transitionBarriers, const AliasingBarrierDesc* aliasingBarriers, BarrierDependency dependency); - void BeginQuery(const QueryPool& queryPool, uint32_t offset); - void EndQuery(const QueryPool& queryPool, uint32_t offset); - void CopyQueries(const QueryPool& queryPool, uint32_t offset, uint32_t num, Buffer& dstBuffer, uint64_t dstOffset); - void BeginAnnotation(const char* name); - void EndAnnotation(); +private: + DeviceD3D11& m_Device; + VersionedContext m_DeferredContext = {}; // can be immediate to redirect data from emulation + BindingState m_BindingState = {}; + SamplePositionsState m_SamplePositionsState = {}; + ComPtr m_CommandList; + ComPtr m_Annotation; + const FrameBufferD3D11* m_CurrentFrameBuffer = nullptr; + PipelineLayoutD3D11* m_CurrentPipelineLayout = nullptr; + PipelineD3D11* m_CurrentPipeline = nullptr; + const Buffer* m_CurrentIndexBuffer = nullptr; + const Buffer* m_CurrentVertexBuffer = nullptr; + uint64_t m_CurrentIndexBufferOffset = 0; + uint64_t m_CurrentVertexBufferOffset = 0; + uint32_t m_CurrentVertexBufferBaseSlot = 0; + IndexType m_CurrentIndexType = IndexType::UINT32; + float m_DepthBounds[2] = {0.0f, 1.0f}; + uint8_t m_StencilRef = 0; +}; - private: - const VersionedDevice& m_Device; - VersionedContext m_Context = {}; - BindingState m_BindingState = {}; - SamplePositionsState m_SamplePositionsState = {}; - ComPtr m_CommandList; - ComPtr m_Annotation; - const FrameBufferD3D11* m_CurrentFrameBuffer = nullptr; - PipelineLayoutD3D11* m_CurrentPipelineLayout = nullptr; - PipelineD3D11* m_CurrentPipeline = nullptr; - const Buffer* m_CurrentIndexBuffer = nullptr; - const Buffer* m_CurrentVertexBuffer = nullptr; - uint64_t m_CurrentIndexBufferOffset = 0; - uint64_t m_CurrentVertexBufferOffset = 0; - uint32_t m_CurrentVertexBufferBaseSlot = 0; - IndexType m_CurrentIndexType = IndexType::UINT32; - float m_DepthBounds[2] = {0.0f, 1.0f}; - uint8_t m_StencilRef = 0; - DeviceD3D11& m_DeviceImpl; - }; } diff --git a/Source/D3D11/CommandBufferD3D11.hpp b/Source/D3D11/CommandBufferD3D11.hpp index 9779c2b6..3bd24ed4 100644 --- a/Source/D3D11/CommandBufferD3D11.hpp +++ b/Source/D3D11/CommandBufferD3D11.hpp @@ -8,7 +8,7 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ CoreInterface ] +#pragma region [ Core ] static void NRI_CALL SetCommandBufferDebugName(CommandBuffer& commandBuffer, const char* name) { @@ -213,54 +213,6 @@ static void* NRI_CALL GetCommandBufferNativeObject(const CommandBuffer& commandB return (CommandBufferD3D11&)commandBuffer; } -void FillFunctionTableCommandBufferD3D11(CoreInterface& coreInterface) -{ - coreInterface.DestroyCommandBuffer = ::DestroyCommandBuffer; - - coreInterface.BeginCommandBuffer = ::BeginCommandBuffer; - coreInterface.EndCommandBuffer = ::EndCommandBuffer; - - coreInterface.CmdSetPipelineLayout = ::CmdSetPipelineLayout; - coreInterface.CmdSetPipeline = ::CmdSetPipeline; - coreInterface.CmdPipelineBarrier = ::CmdPipelineBarrier; - coreInterface.CmdSetDescriptorPool = ::CmdSetDescriptorPool; - coreInterface.CmdSetDescriptorSet = ::CmdSetDescriptorSet; - coreInterface.CmdSetConstants = ::CmdSetConstants; - - coreInterface.CmdBeginRenderPass = ::CmdBeginRenderPass; - coreInterface.CmdEndRenderPass = ::CmdEndRenderPass; - coreInterface.CmdSetViewports = ::CmdSetViewports; - coreInterface.CmdSetScissors = ::CmdSetScissors; - coreInterface.CmdSetDepthBounds = ::CmdSetDepthBounds; - coreInterface.CmdSetStencilReference = ::CmdSetStencilReference; - coreInterface.CmdSetSamplePositions = ::CmdSetSamplePositions; - coreInterface.CmdClearAttachments = ::CmdClearAttachments; - coreInterface.CmdSetIndexBuffer = ::CmdSetIndexBuffer; - coreInterface.CmdSetVertexBuffers = ::CmdSetVertexBuffers; - - coreInterface.CmdDraw = ::CmdDraw; - coreInterface.CmdDrawIndexed = ::CmdDrawIndexed; - coreInterface.CmdDrawIndirect = ::CmdDrawIndirect; - coreInterface.CmdDrawIndexedIndirect = ::CmdDrawIndexedIndirect; - coreInterface.CmdDispatch = ::CmdDispatch; - coreInterface.CmdDispatchIndirect = ::CmdDispatchIndirect; - coreInterface.CmdBeginQuery = ::CmdBeginQuery; - coreInterface.CmdEndQuery = ::CmdEndQuery; - coreInterface.CmdBeginAnnotation = ::CmdBeginAnnotation; - coreInterface.CmdEndAnnotation = ::CmdEndAnnotation; - - coreInterface.CmdClearStorageBuffer = ::CmdClearStorageBuffer; - coreInterface.CmdClearStorageTexture = ::CmdClearStorageTexture; - coreInterface.CmdCopyBuffer = ::CmdCopyBuffer; - coreInterface.CmdCopyTexture = ::CmdCopyTexture; - coreInterface.CmdUploadBufferToTexture = ::CmdUploadBufferToTexture; - coreInterface.CmdReadbackTextureToBuffer = ::CmdReadbackTextureToBuffer; - coreInterface.CmdCopyQueries = ::CmdCopyQueries; - coreInterface.CmdResetQueries = ::CmdResetQueries; - - coreInterface.SetCommandBufferDebugName = ::SetCommandBufferDebugName; - - coreInterface.GetCommandBufferNativeObject = ::GetCommandBufferNativeObject; -} - #pragma endregion + +Define_Core_CommandBuffer_PartiallyFillFunctionTable(D3D11) diff --git a/Source/D3D11/CommandBufferEmuD3D11.cpp b/Source/D3D11/CommandBufferEmuD3D11.cpp index 6bf4ad1b..8ca293e5 100644 --- a/Source/D3D11/CommandBufferEmuD3D11.cpp +++ b/Source/D3D11/CommandBufferEmuD3D11.cpp @@ -8,10 +8,8 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#include "SharedExternal.h" #include "SharedD3D11.h" #include "CommandBufferEmuD3D11.h" - #include "CommandBufferD3D11.h" #include "DescriptorSetD3D11.h" #include "PipelineD3D11.h" @@ -102,18 +100,9 @@ template inline void Read(PushBuffer& pushBuffer, size_t& i, T*& dat i += GetElementNum(sizeof(T) * num); } -//============================================================================================================================== - -CommandBufferEmuD3D11::CommandBufferEmuD3D11(DeviceD3D11& deviceImpl) : - m_Device(deviceImpl.GetDevice()), - m_PushBuffer(deviceImpl.GetStdAllocator()), - m_DeviceImpl(deviceImpl) -{ -} - -CommandBufferEmuD3D11::~CommandBufferEmuD3D11() -{ -} +//================================================================================================================ +// CommandBufferHelper +//================================================================================================================ Result CommandBufferEmuD3D11::Create(ID3D11DeviceContext* precreatedContext) { @@ -124,9 +113,10 @@ Result CommandBufferEmuD3D11::Create(ID3D11DeviceContext* precreatedContext) return Result::SUCCESS; } -void CommandBufferEmuD3D11::Submit(const VersionedContext& immediateContext) +void CommandBufferEmuD3D11::Submit() { - CommandBufferD3D11 commandBuffer(m_DeviceImpl, immediateContext); + CommandBufferD3D11 commandBuffer(m_Device); + OpCode opCode = UNKNOWN; size_t i = 0; @@ -551,11 +541,20 @@ void CommandBufferEmuD3D11::Submit(const VersionedContext& immediateContext) } } -inline void CommandBufferEmuD3D11::SetDebugName(const char* name) +ID3D11DeviceContext* CommandBufferEmuD3D11::GetNativeObject() const { - MaybeUnused(name); + return m_Device.GetImmediateContext(); } +StdAllocator& CommandBufferEmuD3D11::GetStdAllocator() const +{ + return m_Device.GetStdAllocator(); +} + +//================================================================================================================ +// NRI +//================================================================================================================ + inline Result CommandBufferEmuD3D11::Begin(const DescriptorPool* descriptorPool) { m_PushBuffer.clear(); @@ -837,9 +836,4 @@ inline void CommandBufferEmuD3D11::EndAnnotation() Push(m_PushBuffer, END_ANNOTATION); } -StdAllocator& CommandBufferEmuD3D11::GetStdAllocator() const -{ - return m_DeviceImpl.GetStdAllocator(); -} - #include "CommandBufferEmuD3D11.hpp" diff --git a/Source/D3D11/CommandBufferEmuD3D11.h b/Source/D3D11/CommandBufferEmuD3D11.h index b7f64027..61d6891f 100644 --- a/Source/D3D11/CommandBufferEmuD3D11.h +++ b/Source/D3D11/CommandBufferEmuD3D11.h @@ -12,67 +12,78 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct PipelineD3D11; - typedef Vector PushBuffer; - struct CommandBufferEmuD3D11 final : public CommandBufferHelper - { - CommandBufferEmuD3D11(DeviceD3D11& deviceImpl); - ~CommandBufferEmuD3D11(); +struct PipelineD3D11; +typedef Vector PushBuffer; - inline const VersionedContext& GetImmediateContext() const - { return m_DeviceImpl.GetImmediateContext(); } +struct CommandBufferEmuD3D11 final : public CommandBufferHelper +{ + inline CommandBufferEmuD3D11(DeviceD3D11& device) : + m_Device(device) + , m_PushBuffer(device.GetStdAllocator()) + {} + + inline ~CommandBufferEmuD3D11() + {} + + inline DeviceD3D11& GetDevice() const + { return m_Device; } + + //================================================================================================================ + // CommandBufferHelper + //================================================================================================================ + + Result Create(ID3D11DeviceContext* precreatedContext); + void Submit(); + ID3D11DeviceContext* GetNativeObject() const; + StdAllocator& GetStdAllocator() const; + + //================================================================================================================ + // NRI + //================================================================================================================ + + inline void SetDebugName(const char* name) + { MaybeUnused(name); } - //====================================================================================================================== - // CommandBufferHelper - //====================================================================================================================== - Result Create(ID3D11DeviceContext* precreatedContext); - void Submit(const VersionedContext& context); - StdAllocator& GetStdAllocator() const; + Result Begin(const DescriptorPool* descriptorPool); + Result End(); + void SetViewports(const Viewport* viewports, uint32_t viewportNum); + void SetScissors(const Rect* rects, uint32_t rectNum); + void SetDepthBounds(float boundsMin, float boundsMax); + void SetStencilReference(uint8_t reference); + void SetSamplePositions(const SamplePosition* positions, uint32_t positionNum); + void ClearAttachments(const ClearDesc* clearDescs, uint32_t clearDescNum, const Rect* rects, uint32_t rectNum); + void ClearStorageBuffer(const ClearStorageBufferDesc& clearDesc); + void ClearStorageTexture(const ClearStorageTextureDesc& clearDesc); + void BeginRenderPass(const FrameBuffer& frameBuffer, RenderPassBeginFlag renderPassBeginFlag); + void EndRenderPass(); + void SetVertexBuffers(uint32_t baseSlot, uint32_t bufferNum, const Buffer* const* buffers, const uint64_t* offsets); + void SetIndexBuffer(const Buffer& buffer, uint64_t offset, IndexType indexType); + void SetPipelineLayout(const PipelineLayout& pipelineLayout); + void SetPipeline(const Pipeline& pipeline); + void SetDescriptorPool(const DescriptorPool& descriptorPool); + void SetDescriptorSet(uint32_t setIndexInPipelineLayout, const DescriptorSet& descriptorSet, const uint32_t* dynamicConstantBufferOffsets); + void SetConstants(uint32_t pushConstantIndex, const void* data, uint32_t size); + void Draw(uint32_t vertexNum, uint32_t instanceNum, uint32_t baseVertex, uint32_t baseInstance); + void DrawIndexed(uint32_t indexNum, uint32_t instanceNum, uint32_t baseIndex, uint32_t baseVertex, uint32_t baseInstance); + void DrawIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride); + void DrawIndexedIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride); + void CopyBuffer(Buffer& dstBuffer, uint64_t dstOffset, const Buffer& srcBuffer, uint64_t srcOffset, uint64_t size); + void CopyTexture(Texture& dstTexture, const TextureRegionDesc* dstRegionDesc, const Texture& srcTexture, const TextureRegionDesc* srcRegionDesc); + void UploadBufferToTexture(Texture& dstTexture, const TextureRegionDesc& dstRegionDesc, const Buffer& srcBuffer, const TextureDataLayoutDesc& srcDataLayoutDesc); + void ReadbackTextureToBuffer(Buffer& dstBuffer, TextureDataLayoutDesc& dstDataLayoutDesc, const Texture& srcTexture, const TextureRegionDesc& srcRegionDesc); + void Dispatch(uint32_t x, uint32_t y, uint32_t z); + void DispatchIndirect(const Buffer& buffer, uint64_t offset); + void PipelineBarrier(const TransitionBarrierDesc* transitionBarriers, const AliasingBarrierDesc* aliasingBarriers, BarrierDependency dependency); + void BeginQuery(const QueryPool& queryPool, uint32_t offset); + void EndQuery(const QueryPool& queryPool, uint32_t offset); + void CopyQueries(const QueryPool& queryPool, uint32_t offset, uint32_t num, Buffer& dstBuffer, uint64_t dstOffset); + void BeginAnnotation(const char* name); + void EndAnnotation(); - //====================================================================================================================== - // NRI - //====================================================================================================================== - void SetDebugName(const char* name); - Result Begin(const DescriptorPool* descriptorPool); - Result End(); - void SetViewports(const Viewport* viewports, uint32_t viewportNum); - void SetScissors(const Rect* rects, uint32_t rectNum); - void SetDepthBounds(float boundsMin, float boundsMax); - void SetStencilReference(uint8_t reference); - void SetSamplePositions(const SamplePosition* positions, uint32_t positionNum); - void ClearAttachments(const ClearDesc* clearDescs, uint32_t clearDescNum, const Rect* rects, uint32_t rectNum); - void ClearStorageBuffer(const ClearStorageBufferDesc& clearDesc); - void ClearStorageTexture(const ClearStorageTextureDesc& clearDesc); - void BeginRenderPass(const FrameBuffer& frameBuffer, RenderPassBeginFlag renderPassBeginFlag); - void EndRenderPass(); - void SetVertexBuffers(uint32_t baseSlot, uint32_t bufferNum, const Buffer* const* buffers, const uint64_t* offsets); - void SetIndexBuffer(const Buffer& buffer, uint64_t offset, IndexType indexType); - void SetPipelineLayout(const PipelineLayout& pipelineLayout); - void SetPipeline(const Pipeline& pipeline); - void SetDescriptorPool(const DescriptorPool& descriptorPool); - void SetDescriptorSet(uint32_t setIndexInPipelineLayout, const DescriptorSet& descriptorSet, const uint32_t* dynamicConstantBufferOffsets); - void SetConstants(uint32_t pushConstantIndex, const void* data, uint32_t size); - void Draw(uint32_t vertexNum, uint32_t instanceNum, uint32_t baseVertex, uint32_t baseInstance); - void DrawIndexed(uint32_t indexNum, uint32_t instanceNum, uint32_t baseIndex, uint32_t baseVertex, uint32_t baseInstance); - void DrawIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride); - void DrawIndexedIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride); - void CopyBuffer(Buffer& dstBuffer, uint64_t dstOffset, const Buffer& srcBuffer, uint64_t srcOffset, uint64_t size); - void CopyTexture(Texture& dstTexture, const TextureRegionDesc* dstRegionDesc, const Texture& srcTexture, const TextureRegionDesc* srcRegionDesc); - void UploadBufferToTexture(Texture& dstTexture, const TextureRegionDesc& dstRegionDesc, const Buffer& srcBuffer, const TextureDataLayoutDesc& srcDataLayoutDesc); - void ReadbackTextureToBuffer(Buffer& dstBuffer, TextureDataLayoutDesc& dstDataLayoutDesc, const Texture& srcTexture, const TextureRegionDesc& srcRegionDesc); - void Dispatch(uint32_t x, uint32_t y, uint32_t z); - void DispatchIndirect(const Buffer& buffer, uint64_t offset); - void PipelineBarrier(const TransitionBarrierDesc* transitionBarriers, const AliasingBarrierDesc* aliasingBarriers, BarrierDependency dependency); - void BeginQuery(const QueryPool& queryPool, uint32_t offset); - void EndQuery(const QueryPool& queryPool, uint32_t offset); - void CopyQueries(const QueryPool& queryPool, uint32_t offset, uint32_t num, Buffer& dstBuffer, uint64_t dstOffset); - void BeginAnnotation(const char* name); - void EndAnnotation(); +private: + DeviceD3D11& m_Device; + PushBuffer m_PushBuffer; +}; - private: - const VersionedDevice& m_Device; - PushBuffer m_PushBuffer; - DeviceD3D11& m_DeviceImpl; - }; } diff --git a/Source/D3D11/CommandBufferEmuD3D11.hpp b/Source/D3D11/CommandBufferEmuD3D11.hpp index a5114239..26ebe7f1 100644 --- a/Source/D3D11/CommandBufferEmuD3D11.hpp +++ b/Source/D3D11/CommandBufferEmuD3D11.hpp @@ -8,7 +8,7 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ CoreInterface ] +#pragma region [ Core ] static void NRI_CALL SetCommandBufferDebugName(CommandBuffer& commandBuffer, const char* name) { @@ -212,23 +212,23 @@ static void NRI_CALL DestroyCommandBuffer(CommandBuffer& commandBuffer) static void* NRI_CALL GetCommandBufferNativeObject(const CommandBuffer& commandBuffer) { - return ((CommandBufferEmuD3D11&)commandBuffer).GetImmediateContext(); + CommandBufferHelper& commandBufferHelper = (CommandBufferHelper&)commandBuffer; + return commandBufferHelper.GetNativeObject(); } -void FillFunctionTableCommandBufferEmuD3D11(CoreInterface& coreInterface) +#pragma endregion + +void Core_CommandBufferEmu_PartiallyFillFunctionTable(CoreInterface& coreInterface) { coreInterface.DestroyCommandBuffer = ::DestroyCommandBuffer; - coreInterface.BeginCommandBuffer = ::BeginCommandBuffer; coreInterface.EndCommandBuffer = ::EndCommandBuffer; - coreInterface.CmdSetPipelineLayout = ::CmdSetPipelineLayout; coreInterface.CmdSetPipeline = ::CmdSetPipeline; coreInterface.CmdPipelineBarrier = ::CmdPipelineBarrier; coreInterface.CmdSetDescriptorPool = ::CmdSetDescriptorPool; coreInterface.CmdSetDescriptorSet = ::CmdSetDescriptorSet; coreInterface.CmdSetConstants = ::CmdSetConstants; - coreInterface.CmdBeginRenderPass = ::CmdBeginRenderPass; coreInterface.CmdEndRenderPass = ::CmdEndRenderPass; coreInterface.CmdSetViewports = ::CmdSetViewports; @@ -239,7 +239,6 @@ void FillFunctionTableCommandBufferEmuD3D11(CoreInterface& coreInterface) coreInterface.CmdClearAttachments = ::CmdClearAttachments; coreInterface.CmdSetIndexBuffer = ::CmdSetIndexBuffer; coreInterface.CmdSetVertexBuffers = ::CmdSetVertexBuffers; - coreInterface.CmdDraw = ::CmdDraw; coreInterface.CmdDrawIndexed = ::CmdDrawIndexed; coreInterface.CmdDrawIndirect = ::CmdDrawIndirect; @@ -250,7 +249,6 @@ void FillFunctionTableCommandBufferEmuD3D11(CoreInterface& coreInterface) coreInterface.CmdEndQuery = ::CmdEndQuery; coreInterface.CmdBeginAnnotation = ::CmdBeginAnnotation; coreInterface.CmdEndAnnotation = ::CmdEndAnnotation; - coreInterface.CmdClearStorageBuffer = ::CmdClearStorageBuffer; coreInterface.CmdClearStorageTexture = ::CmdClearStorageTexture; coreInterface.CmdCopyBuffer = ::CmdCopyBuffer; @@ -259,10 +257,6 @@ void FillFunctionTableCommandBufferEmuD3D11(CoreInterface& coreInterface) coreInterface.CmdReadbackTextureToBuffer = ::CmdReadbackTextureToBuffer; coreInterface.CmdCopyQueries = ::CmdCopyQueries; coreInterface.CmdResetQueries = ::CmdResetQueries; - coreInterface.SetCommandBufferDebugName = ::SetCommandBufferDebugName; - coreInterface.GetCommandBufferNativeObject = ::GetCommandBufferNativeObject; } - -#pragma endregion diff --git a/Source/D3D11/CommandQueueD3D11.cpp b/Source/D3D11/CommandQueueD3D11.cpp index 102caec8..f19899ac 100644 --- a/Source/D3D11/CommandQueueD3D11.cpp +++ b/Source/D3D11/CommandQueueD3D11.cpp @@ -8,58 +8,22 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#include "SharedExternal.h" #include "SharedD3D11.h" #include "CommandQueueD3D11.h" -#include "CommandBufferD3D11.h" -#include "DeviceSemaphoreD3D11.h" -#include "QueueSemaphoreD3D11.h" - using namespace nri; -CommandQueueD3D11::CommandQueueD3D11(DeviceD3D11& device, const VersionedContext& immediateContext) : - m_ImmediateContext(immediateContext), - m_Device(device) -{ -} - -CommandQueueD3D11::~CommandQueueD3D11() -{ -} - -inline void CommandQueueD3D11::SetDebugName(const char* name) -{ - SetName(m_ImmediateContext.ptr, name); -} +//================================================================================================================ +// NRI +//================================================================================================================ -inline void CommandQueueD3D11::Submit(const WorkSubmissionDesc& workSubmissionDesc, DeviceSemaphore* deviceSemaphore) +inline void CommandQueueD3D11::Submit(const QueueSubmitDesc& queueSubmitDesc) { - for (uint32_t i = 0; i < workSubmissionDesc.waitNum; i++) + for (uint32_t i = 0; i < queueSubmitDesc.commandBufferNum; i++) { - QueueSemaphoreD3D11* semaphore = (QueueSemaphoreD3D11*)workSubmissionDesc.wait[i]; - semaphore->Wait(); + CommandBufferHelper* commandBuffer = (CommandBufferHelper*)queueSubmitDesc.commandBuffers[i]; + commandBuffer->Submit(); } - - for (uint32_t i = 0; i < workSubmissionDesc.commandBufferNum; i++) - { - CommandBufferHelper* commandBuffer = (CommandBufferHelper*)workSubmissionDesc.commandBuffers[i]; - commandBuffer->Submit(m_ImmediateContext); - } - - for (uint32_t i = 0; i < workSubmissionDesc.signalNum; i++) - { - QueueSemaphoreD3D11* semaphore = (QueueSemaphoreD3D11*)workSubmissionDesc.signal[i]; - semaphore->Signal(); - } - - if (deviceSemaphore) - ((DeviceSemaphoreD3D11*)deviceSemaphore)->Signal(m_ImmediateContext); -} - -inline void CommandQueueD3D11::Wait(DeviceSemaphore& deviceSemaphore) -{ - ((DeviceSemaphoreD3D11&)deviceSemaphore).Wait(m_ImmediateContext); } inline Result CommandQueueD3D11::ChangeResourceStates(const TransitionBarrierDesc& transitionBarriers) diff --git a/Source/D3D11/CommandQueueD3D11.h b/Source/D3D11/CommandQueueD3D11.h index dedd5152..eb3066db 100644 --- a/Source/D3D11/CommandQueueD3D11.h +++ b/Source/D3D11/CommandQueueD3D11.h @@ -12,30 +12,36 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct DeviceD3D11; - - struct CommandQueueD3D11 - { - CommandQueueD3D11(DeviceD3D11& device, const VersionedContext& immediateContext); - ~CommandQueueD3D11(); - - inline DeviceD3D11& GetDevice() const - { return m_Device; } - - //====================================================================================================================== - // NRI - //====================================================================================================================== - void SetDebugName(const char* name); - void Submit(const WorkSubmissionDesc& workSubmissions, DeviceSemaphore* deviceSemaphore); - void Wait(DeviceSemaphore& deviceSemaphore); - - Result ChangeResourceStates(const TransitionBarrierDesc& transitionBarriers); - Result UploadData(const TextureUploadDesc* textureUploadDescs, uint32_t textureUploadDescNum, - const BufferUploadDesc* bufferUploadDescs, uint32_t bufferUploadDescNum); - Result WaitForIdle(); - - private: - const VersionedContext& m_ImmediateContext; - DeviceD3D11& m_Device; - }; + +struct DeviceD3D11; + +struct CommandQueueD3D11 +{ + inline CommandQueueD3D11(DeviceD3D11& device) : + m_Device(device) + {} + + inline ~CommandQueueD3D11() + {} + + inline DeviceD3D11& GetDevice() const + { return m_Device; } + + //================================================================================================================ + // NRI + //================================================================================================================ + + inline void SetDebugName(const char* name) + { MaybeUnused(name); } + + void Submit(const QueueSubmitDesc& queueSubmitDesc); + Result ChangeResourceStates(const TransitionBarrierDesc& transitionBarriers); + Result UploadData(const TextureUploadDesc* textureUploadDescs, uint32_t textureUploadDescNum, + const BufferUploadDesc* bufferUploadDescs, uint32_t bufferUploadDescNum); + Result WaitForIdle(); + +private: + DeviceD3D11& m_Device; +}; + } diff --git a/Source/D3D11/CommandQueueD3D11.hpp b/Source/D3D11/CommandQueueD3D11.hpp index d03fae86..4dadd3a1 100644 --- a/Source/D3D11/CommandQueueD3D11.hpp +++ b/Source/D3D11/CommandQueueD3D11.hpp @@ -8,55 +8,39 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ CoreInterface ] +#pragma region [ Core ] static void NRI_CALL SetCommandQueueDebugName(CommandQueue& commandQueue, const char* name) { ((CommandQueueD3D11&)commandQueue).SetDebugName(name); } -static void NRI_CALL SubmitQueueWork(CommandQueue& commandQueue, const WorkSubmissionDesc& workSubmissionDesc, DeviceSemaphore* deviceSemaphore) +static void NRI_CALL QueueSubmit(CommandQueue& commandQueue, const QueueSubmitDesc& queueSubmitDesc) { - ((CommandQueueD3D11&)commandQueue).Submit(workSubmissionDesc, deviceSemaphore); -} - -static void NRI_CALL WaitForSemaphore(CommandQueue& commandQueue, DeviceSemaphore& deviceSemaphore) -{ - ((CommandQueueD3D11&)commandQueue).Wait(deviceSemaphore); -} - -void FillFunctionTableCommandQueueD3D11(CoreInterface& coreInterface) -{ - coreInterface.SetCommandQueueDebugName = ::SetCommandQueueDebugName; - coreInterface.SubmitQueueWork = ::SubmitQueueWork; - coreInterface.WaitForSemaphore = ::WaitForSemaphore; + ((CommandQueueD3D11&)commandQueue).Submit(queueSubmitDesc); } #pragma endregion -#pragma region [ HelperInterface ] +#pragma region [ Helper ] -static Result NRI_CALL ChangeResourceStatesD3D11(CommandQueue& commandQueue, const TransitionBarrierDesc& transitionBarriers) +static Result NRI_CALL ChangeResourceStates(CommandQueue& commandQueue, const TransitionBarrierDesc& transitionBarriers) { return ((CommandQueueD3D11&)commandQueue).ChangeResourceStates(transitionBarriers); } -static nri::Result NRI_CALL UploadDataD3D11(CommandQueue& commandQueue, const TextureUploadDesc* textureUploadDescs, uint32_t textureUploadDescNum, +static nri::Result NRI_CALL UploadData(CommandQueue& commandQueue, const TextureUploadDesc* textureUploadDescs, uint32_t textureUploadDescNum, const BufferUploadDesc* bufferUploadDescs, uint32_t bufferUploadDescNum) { return ((CommandQueueD3D11&)commandQueue).UploadData(textureUploadDescs, textureUploadDescNum, bufferUploadDescs, bufferUploadDescNum); } -static nri::Result NRI_CALL WaitForIdleD3D11(CommandQueue& commandQueue) +static nri::Result NRI_CALL WaitForIdle(CommandQueue& commandQueue) { return ((CommandQueueD3D11&)commandQueue).WaitForIdle(); } -void FillFunctionTableCommandQueueD3D11(HelperInterface& helperInterface) -{ - helperInterface.ChangeResourceStates = ::ChangeResourceStatesD3D11; - helperInterface.UploadData = ::UploadDataD3D11; - helperInterface.WaitForIdle = ::WaitForIdleD3D11; -} - #pragma endregion + +Define_Core_CommandQueue_PartiallyFillFunctionTable(D3D11) +Define_Helper_CommandQueue_PartiallyFillFunctionTable(D3D11) diff --git a/Source/D3D11/D3D11Extensions.cpp b/Source/D3D11/D3D11Extensions.cpp index 22ba5072..9cc75d7c 100644 --- a/Source/D3D11/D3D11Extensions.cpp +++ b/Source/D3D11/D3D11Extensions.cpp @@ -1,4 +1,13 @@ -#include "SharedExternal.h" +/* +Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. + +NVIDIA CORPORATION and its licensors retain all intellectual property +and proprietary rights in and to this software, related documentation +and any modifications thereto. Any use, reproduction, disclosure or +distribution of this software and related documentation without an express +license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + #include "SharedD3D11.h" #include "D3D11Extensions.h" @@ -18,7 +27,7 @@ D3D11Extensions::~D3D11Extensions() } } -void D3D11Extensions::Create(const Log& log, nri::Vendor vendor, AGSContext* context, bool isImported) +void D3D11Extensions::Create(const Log& log, nri::Vendor vendor, AGSContext* agsContext, bool isImported) { m_Log = &log; @@ -34,12 +43,12 @@ void D3D11Extensions::Create(const Log& log, nri::Vendor vendor, AGSContext* con break; case nri::Vendor::AMD: { - if (isImported && context == nullptr) + if (isImported && !agsContext) break; if (LoadAGS()) { - m_AGSContext = context; + m_AGSContext = agsContext; m_IsImported = isImported; if (m_AGSContext == nullptr) @@ -95,35 +104,35 @@ bool D3D11Extensions::LoadAGS() return true; } -void D3D11Extensions::BeginUAVOverlap(const VersionedContext& context) const +void D3D11Extensions::BeginUAVOverlap(const VersionedContext& deferredContext) const { if (m_IsNvAPIAvailable) { - const NvAPI_Status res = NvAPI_D3D11_BeginUAVOverlap(context.ptr); + const NvAPI_Status res = NvAPI_D3D11_BeginUAVOverlap(deferredContext.ptr); CHECK(*m_Log, res == NVAPI_OK, "NvAPI_D3D11_BeginUAVOverlap() - FAILED!"); } else if (m_IsAGSAvailable) { - const AGSReturnCode res = m_AGS.BeginUAVOverlap(m_AGSContext, context.ptr); + const AGSReturnCode res = m_AGS.BeginUAVOverlap(m_AGSContext, deferredContext.ptr); CHECK(*m_Log, res == AGS_SUCCESS, "agsDriverExtensionsDX11_BeginUAVOverlap() - FAILED!"); } } -void D3D11Extensions::EndUAVOverlap(const VersionedContext& context) const +void D3D11Extensions::EndUAVOverlap(const VersionedContext& deferredContext) const { if (m_IsNvAPIAvailable) { - const NvAPI_Status status = NvAPI_D3D11_EndUAVOverlap(context.ptr); + const NvAPI_Status status = NvAPI_D3D11_EndUAVOverlap(deferredContext.ptr); CHECK(*m_Log, status == NVAPI_OK, "NvAPI_D3D11_EndUAVOverlap() - FAILED!"); } else if (m_IsAGSAvailable) { - const AGSReturnCode res = m_AGS.EndUAVOverlap(m_AGSContext, context.ptr); + const AGSReturnCode res = m_AGS.EndUAVOverlap(m_AGSContext, deferredContext.ptr); CHECK(*m_Log, res == AGS_SUCCESS, "agsDriverExtensionsDX11_EndUAVOverlap() - FAILED!"); } } -void D3D11Extensions::WaitForDrain(const VersionedContext& context, nri::BarrierDependency dependency) const +void D3D11Extensions::WaitForDrain(const VersionedContext& deferredContext, nri::BarrierDependency dependency) const { if (m_IsNvAPIAvailable) { @@ -136,75 +145,75 @@ void D3D11Extensions::WaitForDrain(const VersionedContext& context, nri::Barrier else flags = NVAPI_D3D_BEGIN_UAV_OVERLAP_GFX_WFI | NVAPI_D3D_BEGIN_UAV_OVERLAP_COMP_WFI; - const NvAPI_Status res = NvAPI_D3D11_BeginUAVOverlapEx(context.ptr, flags); + const NvAPI_Status res = NvAPI_D3D11_BeginUAVOverlapEx(deferredContext.ptr, flags); CHECK(*m_Log, res == NVAPI_OK, "NvAPI_D3D11_BeginUAVOverlap() - FAILED!"); } else if (m_IsAGSAvailable) { REPORT_WARNING(*m_Log, "Verify that this code actually works on AMD!"); - const AGSReturnCode res1 = m_AGS.EndUAVOverlap(m_AGSContext, context.ptr); + const AGSReturnCode res1 = m_AGS.EndUAVOverlap(m_AGSContext, deferredContext.ptr); CHECK(*m_Log, res1 == AGS_SUCCESS, "agsDriverExtensionsDX11_EndUAVOverlap() - FAILED!"); - const AGSReturnCode res2 = m_AGS.BeginUAVOverlap(m_AGSContext, context.ptr); + const AGSReturnCode res2 = m_AGS.BeginUAVOverlap(m_AGSContext, deferredContext.ptr); CHECK(*m_Log, res2 == AGS_SUCCESS, "agsDriverExtensionsDX11_BeginUAVOverlap() - FAILED!"); } } -void D3D11Extensions::SetDepthBounds(const VersionedContext& context, float minBound, float maxBound) const +void D3D11Extensions::SetDepthBounds(const VersionedContext& deferredContext, float minBound, float maxBound) const { bool isEnabled = minBound != 0.0f || maxBound != 1.0f; if (m_IsNvAPIAvailable) { - const NvAPI_Status status = NvAPI_D3D11_SetDepthBoundsTest(context.ptr, isEnabled, minBound, maxBound); + const NvAPI_Status status = NvAPI_D3D11_SetDepthBoundsTest(deferredContext.ptr, isEnabled, minBound, maxBound); CHECK(*m_Log, status == NVAPI_OK, "NvAPI_D3D11_SetDepthBoundsTest() - FAILED!"); } else if (m_IsAGSAvailable) { - const AGSReturnCode res = m_AGS.SetDepthBounds(m_AGSContext, context.ptr, isEnabled, minBound, maxBound); + const AGSReturnCode res = m_AGS.SetDepthBounds(m_AGSContext, deferredContext.ptr, isEnabled, minBound, maxBound); CHECK(*m_Log, res == AGS_SUCCESS, "agsDriverExtensionsDX11_SetDepthBounds() - FAILED!"); } } -void D3D11Extensions::MultiDrawIndirect(const VersionedContext& context, ID3D11Buffer* buffer, uint64_t offset, uint32_t drawNum, uint32_t stride) const +void D3D11Extensions::MultiDrawIndirect(const VersionedContext& deferredContext, ID3D11Buffer* buffer, uint64_t offset, uint32_t drawNum, uint32_t stride) const { if (m_IsNvAPIAvailable) { - const NvAPI_Status status = NvAPI_D3D11_MultiDrawInstancedIndirect(context.ptr, drawNum, buffer, (uint32_t)offset, stride); + const NvAPI_Status status = NvAPI_D3D11_MultiDrawInstancedIndirect(deferredContext.ptr, drawNum, buffer, (uint32_t)offset, stride); CHECK(*m_Log, status == NVAPI_OK, "NvAPI_D3D11_MultiDrawInstancedIndirect() - FAILED!"); } else if (m_IsAGSAvailable) { - const AGSReturnCode res = m_AGS.MultiDrawInstancedIndirect(m_AGSContext, context.ptr, drawNum, buffer, (uint32_t)offset, stride); + const AGSReturnCode res = m_AGS.MultiDrawInstancedIndirect(m_AGSContext, deferredContext.ptr, drawNum, buffer, (uint32_t)offset, stride); CHECK(*m_Log, res == AGS_SUCCESS, "agsDriverExtensionsDX11_MultiDrawIndexedInstancedIndirect() - FAILED!"); } else { for (uint32_t i = 0; i < drawNum; i++) { - context->DrawInstancedIndirect(buffer, (uint32_t)offset); + deferredContext->DrawInstancedIndirect(buffer, (uint32_t)offset); offset += stride; } } } -void D3D11Extensions::MultiDrawIndexedIndirect(const VersionedContext& context, ID3D11Buffer* buffer, uint64_t offset, uint32_t drawNum, uint32_t stride) const +void D3D11Extensions::MultiDrawIndexedIndirect(const VersionedContext& deferredContext, ID3D11Buffer* buffer, uint64_t offset, uint32_t drawNum, uint32_t stride) const { if (m_IsNvAPIAvailable) { - const NvAPI_Status status = NvAPI_D3D11_MultiDrawIndexedInstancedIndirect(context.ptr, drawNum, buffer, (uint32_t)offset, stride); + const NvAPI_Status status = NvAPI_D3D11_MultiDrawIndexedInstancedIndirect(deferredContext.ptr, drawNum, buffer, (uint32_t)offset, stride); CHECK(*m_Log, status == NVAPI_OK, "NvAPI_D3D11_MultiDrawInstancedIndirect() - FAILED!"); } else if (m_IsAGSAvailable) { - const AGSReturnCode res = m_AGS.MultiDrawIndexedInstancedIndirect(m_AGSContext, context.ptr, drawNum, buffer, (uint32_t)offset, stride); + const AGSReturnCode res = m_AGS.MultiDrawIndexedInstancedIndirect(m_AGSContext, deferredContext.ptr, drawNum, buffer, (uint32_t)offset, stride); CHECK(*m_Log, res == AGS_SUCCESS, "agsDriverExtensionsDX11_MultiDrawIndexedInstancedIndirect() - FAILED!"); } else { for (uint32_t i = 0; i < drawNum; i++) { - context->DrawIndexedInstancedIndirect(buffer, (uint32_t)offset); + deferredContext->DrawIndexedInstancedIndirect(buffer, (uint32_t)offset); offset += stride; } } diff --git a/Source/D3D11/D3D11Extensions.h b/Source/D3D11/D3D11Extensions.h index 6deb3ec5..04b773df 100644 --- a/Source/D3D11/D3D11Extensions.h +++ b/Source/D3D11/D3D11Extensions.h @@ -3,14 +3,14 @@ #include "AGS/inc/amd_ags.h" #include "NVAPI/nvapi.h" -typedef AGSReturnCode (*PFN_agsInit)(AGSContext** context, const AGSConfiguration* config, AGSGPUInfo* gpuInfo); -typedef AGSReturnCode (*PFN_agsDeInit)(AGSContext* context); -typedef AGSReturnCode (*PFN_agsDriverExtensionsDX11_CreateDevice)(AGSContext* context, const AGSDX11DeviceCreationParams* creationParams, const AGSDX11ExtensionParams* extensionParams, AGSDX11ReturnedParams* returnedParams); -typedef AGSReturnCode (*PFN_agsDriverExtensionsDX11_BeginUAVOverlap)(AGSContext* context, ID3D11DeviceContext* dxContext); -typedef AGSReturnCode (*PFN_agsDriverExtensionsDX11_EndUAVOverlap)(AGSContext* context, ID3D11DeviceContext* dxContext); -typedef AGSReturnCode (*PFN_agsDriverExtensionsDX11_MultiDrawIndexedInstancedIndirect)(AGSContext* context, ID3D11DeviceContext* dxContext, unsigned int drawCount, ID3D11Buffer* pBufferForArgs, unsigned int alignedByteOffsetForArgs, unsigned int byteStrideForArgs); -typedef AGSReturnCode (*PFN_agsDriverExtensionsDX11_SetDepthBounds)(AGSContext* context, ID3D11DeviceContext* dxContext, bool enabled, float minDepth, float maxDepth); -typedef AGSReturnCode (*PFN_agsDriverExtensionsDX11_MultiDrawInstancedIndirect)(AGSContext* context, ID3D11DeviceContext* dxContext, unsigned int drawCount, ID3D11Buffer* pBufferForArgs, unsigned int alignedByteOffsetForArgs, unsigned int byteStrideForArgs); +typedef AGSReturnCode (*PFN_agsInit)(AGSContext** agsContext, const AGSConfiguration* config, AGSGPUInfo* gpuInfo); +typedef AGSReturnCode (*PFN_agsDeInit)(AGSContext* agsContext); +typedef AGSReturnCode (*PFN_agsDriverExtensionsDX11_CreateDevice)(AGSContext* agsContext, const AGSDX11DeviceCreationParams* creationParams, const AGSDX11ExtensionParams* extensionParams, AGSDX11ReturnedParams* returnedParams); +typedef AGSReturnCode (*PFN_agsDriverExtensionsDX11_BeginUAVOverlap)(AGSContext* agsContext, ID3D11DeviceContext* dxContext); +typedef AGSReturnCode (*PFN_agsDriverExtensionsDX11_EndUAVOverlap)(AGSContext* agsContext, ID3D11DeviceContext* dxContext); +typedef AGSReturnCode (*PFN_agsDriverExtensionsDX11_MultiDrawIndexedInstancedIndirect)(AGSContext* agsContext, ID3D11DeviceContext* dxContext, unsigned int drawCount, ID3D11Buffer* pBufferForArgs, unsigned int alignedByteOffsetForArgs, unsigned int byteStrideForArgs); +typedef AGSReturnCode (*PFN_agsDriverExtensionsDX11_SetDepthBounds)(AGSContext* agsContext, ID3D11DeviceContext* dxContext, bool enabled, float minDepth, float maxDepth); +typedef AGSReturnCode (*PFN_agsDriverExtensionsDX11_MultiDrawInstancedIndirect)(AGSContext* agsContext, ID3D11DeviceContext* dxContext, unsigned int drawCount, ID3D11Buffer* pBufferForArgs, unsigned int alignedByteOffsetForArgs, unsigned int byteStrideForArgs); struct AGSFunctionTable { @@ -32,13 +32,13 @@ struct D3D11Extensions constexpr bool IsNvAPIAvailable() const; constexpr bool IsAGSAvailable() const; - void Create(const Log& log, nri::Vendor vendor, AGSContext* context, bool isImported); - void BeginUAVOverlap(const VersionedContext& context) const; - void EndUAVOverlap(const VersionedContext& context) const; - void WaitForDrain(const VersionedContext& context, nri::BarrierDependency dependency) const; - void SetDepthBounds(const VersionedContext& context, float minBound, float maxBound) const; - void MultiDrawIndirect(const VersionedContext& context, ID3D11Buffer* buffer, uint64_t offset, uint32_t drawNum, uint32_t stride) const; - void MultiDrawIndexedIndirect(const VersionedContext& context, ID3D11Buffer* buffer, uint64_t offset, uint32_t drawNum, uint32_t stride) const; + void Create(const Log& log, nri::Vendor vendor, AGSContext* agsContext, bool isImported); + void BeginUAVOverlap(const VersionedContext& deferredContext) const; + void EndUAVOverlap(const VersionedContext& deferredContext) const; + void WaitForDrain(const VersionedContext& deferredContext, nri::BarrierDependency dependency) const; + void SetDepthBounds(const VersionedContext& deferredContext, float minBound, float maxBound) const; + void MultiDrawIndirect(const VersionedContext& deferredContext, ID3D11Buffer* buffer, uint64_t offset, uint32_t drawNum, uint32_t stride) const; + void MultiDrawIndexedIndirect(const VersionedContext& deferredContext, ID3D11Buffer* buffer, uint64_t offset, uint32_t drawNum, uint32_t stride) const; ID3D11Device* CreateDeviceUsingAGS(IDXGIAdapter* adapter, const D3D_FEATURE_LEVEL* featureLevels, const size_t featureLevelNum, UINT flags); diff --git a/Source/D3D11/DescriptorD3D11.cpp b/Source/D3D11/DescriptorD3D11.cpp index b09b4448..fd2bbbac 100644 --- a/Source/D3D11/DescriptorD3D11.cpp +++ b/Source/D3D11/DescriptorD3D11.cpp @@ -8,10 +8,8 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#include "SharedExternal.h" #include "SharedD3D11.h" #include "DescriptorD3D11.h" - #include "TextureD3D11.h" #include "BufferD3D11.h" @@ -65,16 +63,7 @@ D3D11_FILTER GetFilterAnisotropic(FilterExt filterExt, bool useComparison) return useComparison ? D3D11_FILTER_COMPARISON_ANISOTROPIC : D3D11_FILTER_ANISOTROPIC; } -DescriptorD3D11::DescriptorD3D11(DeviceD3D11& device) : - m_Device(device) -{ -} - -DescriptorD3D11::~DescriptorD3D11() -{ -} - -Result DescriptorD3D11::Create(const VersionedDevice& device, const Texture1DViewDesc& textureViewDesc) +Result DescriptorD3D11::Create(const Texture1DViewDesc& textureViewDesc) { const TextureD3D11& texture = *(TextureD3D11*)textureViewDesc.texture; const FormatInfo& formatInfo = GetFormatInfo(textureViewDesc.format); @@ -95,7 +84,7 @@ Result DescriptorD3D11::Create(const VersionedDevice& device, const Texture1DVie srv.Texture1D.MipLevels = remainingMipLevels; srv.Format = formatInfo.typed; - hr = device->CreateShaderResourceView(texture, &srv, (ID3D11ShaderResourceView**)&m_Descriptor); + hr = m_Device.GetDevice()->CreateShaderResourceView(texture, &srv, (ID3D11ShaderResourceView**)&m_Descriptor); m_Type = DescriptorTypeDX11::RESOURCE; } @@ -111,7 +100,7 @@ Result DescriptorD3D11::Create(const VersionedDevice& device, const Texture1DVie srv.Texture1DArray.ArraySize = remainingArrayLayers; srv.Format = formatInfo.typed; - hr = device->CreateShaderResourceView(texture, &srv, (ID3D11ShaderResourceView**)&m_Descriptor); + hr = m_Device.GetDevice()->CreateShaderResourceView(texture, &srv, (ID3D11ShaderResourceView**)&m_Descriptor); m_Type = DescriptorTypeDX11::RESOURCE; } @@ -124,7 +113,7 @@ Result DescriptorD3D11::Create(const VersionedDevice& device, const Texture1DVie uav.Texture1D.MipSlice = textureViewDesc.mipOffset; uav.Format = formatInfo.typed; - hr = device->CreateUnorderedAccessView(texture, &uav, (ID3D11UnorderedAccessView**)&m_Descriptor); + hr = m_Device.GetDevice()->CreateUnorderedAccessView(texture, &uav, (ID3D11UnorderedAccessView**)&m_Descriptor); m_Type = DescriptorTypeDX11::STORAGE; } @@ -139,7 +128,7 @@ Result DescriptorD3D11::Create(const VersionedDevice& device, const Texture1DVie uav.Texture1DArray.ArraySize = remainingArrayLayers; uav.Format = formatInfo.typed; - hr = device->CreateUnorderedAccessView(texture, &uav, (ID3D11UnorderedAccessView**)&m_Descriptor); + hr = m_Device.GetDevice()->CreateUnorderedAccessView(texture, &uav, (ID3D11UnorderedAccessView**)&m_Descriptor); m_Type = DescriptorTypeDX11::STORAGE; } @@ -154,7 +143,7 @@ Result DescriptorD3D11::Create(const VersionedDevice& device, const Texture1DVie rtv.Texture1DArray.ArraySize = remainingArrayLayers; rtv.Format = formatInfo.typed; - hr = device->CreateRenderTargetView(texture, &rtv, (ID3D11RenderTargetView**)&m_Descriptor); + hr = m_Device.GetDevice()->CreateRenderTargetView(texture, &rtv, (ID3D11RenderTargetView**)&m_Descriptor); m_Type = DescriptorTypeDX11::NO_SHADER_VISIBLE; } @@ -175,7 +164,7 @@ Result DescriptorD3D11::Create(const VersionedDevice& device, const Texture1DVie if (textureViewDesc.flags & ResourceViewBits::READONLY_STENCIL) dsv.Flags |= D3D11_DSV_READ_ONLY_STENCIL; - hr = device->CreateDepthStencilView(texture, &dsv, (ID3D11DepthStencilView**)&m_Descriptor); + hr = m_Device.GetDevice()->CreateDepthStencilView(texture, &dsv, (ID3D11DepthStencilView**)&m_Descriptor); m_Type = DescriptorTypeDX11::NO_SHADER_VISIBLE; } @@ -190,7 +179,7 @@ Result DescriptorD3D11::Create(const VersionedDevice& device, const Texture1DVie return Result::SUCCESS; } -Result DescriptorD3D11::Create(const VersionedDevice& device, const Texture2DViewDesc& textureViewDesc) +Result DescriptorD3D11::Create(const Texture2DViewDesc& textureViewDesc) { const TextureD3D11& texture = *(TextureD3D11*)textureViewDesc.texture; const TextureDesc& desc = texture.GetDesc(); @@ -217,7 +206,7 @@ Result DescriptorD3D11::Create(const VersionedDevice& device, const Texture2DVie } srv.Format = GetShaderFormatForDepth(formatInfo.typed); - hr = device->CreateShaderResourceView(texture, &srv, (ID3D11ShaderResourceView**)&m_Descriptor); + hr = m_Device.GetDevice()->CreateShaderResourceView(texture, &srv, (ID3D11ShaderResourceView**)&m_Descriptor); m_Type = DescriptorTypeDX11::RESOURCE; } @@ -242,7 +231,7 @@ Result DescriptorD3D11::Create(const VersionedDevice& device, const Texture2DVie } srv.Format = GetShaderFormatForDepth(formatInfo.typed); - hr = device->CreateShaderResourceView(texture, &srv, (ID3D11ShaderResourceView**)&m_Descriptor); + hr = m_Device.GetDevice()->CreateShaderResourceView(texture, &srv, (ID3D11ShaderResourceView**)&m_Descriptor); m_Type = DescriptorTypeDX11::RESOURCE; } @@ -256,7 +245,7 @@ Result DescriptorD3D11::Create(const VersionedDevice& device, const Texture2DVie srv.TextureCube.MipLevels = remainingMipLevels; srv.Format = formatInfo.typed; - hr = device->CreateShaderResourceView(texture, &srv, (ID3D11ShaderResourceView**)&m_Descriptor); + hr = m_Device.GetDevice()->CreateShaderResourceView(texture, &srv, (ID3D11ShaderResourceView**)&m_Descriptor); m_Type = DescriptorTypeDX11::RESOURCE; } @@ -272,7 +261,7 @@ Result DescriptorD3D11::Create(const VersionedDevice& device, const Texture2DVie srv.TextureCubeArray.NumCubes = textureViewDesc.arraySize / 6; srv.Format = GetShaderFormatForDepth(formatInfo.typed); - hr = device->CreateShaderResourceView(texture, &srv, (ID3D11ShaderResourceView**)&m_Descriptor); + hr = m_Device.GetDevice()->CreateShaderResourceView(texture, &srv, (ID3D11ShaderResourceView**)&m_Descriptor); m_Type = DescriptorTypeDX11::RESOURCE; } @@ -285,7 +274,7 @@ Result DescriptorD3D11::Create(const VersionedDevice& device, const Texture2DVie uav.Texture2D.MipSlice = textureViewDesc.mipOffset; uav.Format = GetShaderFormatForDepth(formatInfo.typed); - hr = device->CreateUnorderedAccessView(texture, &uav, (ID3D11UnorderedAccessView**)&m_Descriptor); + hr = m_Device.GetDevice()->CreateUnorderedAccessView(texture, &uav, (ID3D11UnorderedAccessView**)&m_Descriptor); m_Type = DescriptorTypeDX11::STORAGE; } @@ -300,7 +289,7 @@ Result DescriptorD3D11::Create(const VersionedDevice& device, const Texture2DVie uav.Texture2DArray.ArraySize = remainingArrayLayers; uav.Format = GetShaderFormatForDepth(formatInfo.typed); - hr = device->CreateUnorderedAccessView(texture, &uav, (ID3D11UnorderedAccessView**)&m_Descriptor); + hr = m_Device.GetDevice()->CreateUnorderedAccessView(texture, &uav, (ID3D11UnorderedAccessView**)&m_Descriptor); m_Type = DescriptorTypeDX11::STORAGE; } @@ -324,7 +313,7 @@ Result DescriptorD3D11::Create(const VersionedDevice& device, const Texture2DVie } rtv.Format = formatInfo.typed; - hr = device->CreateRenderTargetView(texture, &rtv, (ID3D11RenderTargetView**)&m_Descriptor); + hr = m_Device.GetDevice()->CreateRenderTargetView(texture, &rtv, (ID3D11RenderTargetView**)&m_Descriptor); m_Type = DescriptorTypeDX11::NO_SHADER_VISIBLE; } @@ -354,7 +343,7 @@ Result DescriptorD3D11::Create(const VersionedDevice& device, const Texture2DVie if (textureViewDesc.flags & ResourceViewBits::READONLY_STENCIL) dsv.Flags |= D3D11_DSV_READ_ONLY_STENCIL; - hr = device->CreateDepthStencilView(texture, &dsv, (ID3D11DepthStencilView**)&m_Descriptor); + hr = m_Device.GetDevice()->CreateDepthStencilView(texture, &dsv, (ID3D11DepthStencilView**)&m_Descriptor); m_Type = DescriptorTypeDX11::NO_SHADER_VISIBLE; } @@ -369,7 +358,7 @@ Result DescriptorD3D11::Create(const VersionedDevice& device, const Texture2DVie return Result::SUCCESS; } -Result DescriptorD3D11::Create(const VersionedDevice& device, const Texture3DViewDesc& textureViewDesc) +Result DescriptorD3D11::Create(const Texture3DViewDesc& textureViewDesc) { const TextureD3D11& texture = *(TextureD3D11*)textureViewDesc.texture; const FormatInfo& formatInfo = GetFormatInfo(textureViewDesc.format); @@ -389,7 +378,7 @@ Result DescriptorD3D11::Create(const VersionedDevice& device, const Texture3DVie srv.Texture3D.MipLevels = remainingMipLevels; srv.Format = formatInfo.typed; - hr = device->CreateShaderResourceView(texture, &srv, (ID3D11ShaderResourceView**)&m_Descriptor); + hr = m_Device.GetDevice()->CreateShaderResourceView(texture, &srv, (ID3D11ShaderResourceView**)&m_Descriptor); m_Type = DescriptorTypeDX11::RESOURCE; } @@ -404,7 +393,7 @@ Result DescriptorD3D11::Create(const VersionedDevice& device, const Texture3DVie uav.Texture3D.WSize = textureViewDesc.sliceNum; uav.Format = formatInfo.typed; - hr = device->CreateUnorderedAccessView(texture, &uav, (ID3D11UnorderedAccessView**)&m_Descriptor); + hr = m_Device.GetDevice()->CreateUnorderedAccessView(texture, &uav, (ID3D11UnorderedAccessView**)&m_Descriptor); m_Type = DescriptorTypeDX11::STORAGE; } @@ -419,7 +408,7 @@ Result DescriptorD3D11::Create(const VersionedDevice& device, const Texture3DVie rtv.Texture3D.WSize = textureViewDesc.sliceNum; rtv.Format = formatInfo.typed; - hr = device->CreateRenderTargetView(texture, &rtv, (ID3D11RenderTargetView**)&m_Descriptor); + hr = m_Device.GetDevice()->CreateRenderTargetView(texture, &rtv, (ID3D11RenderTargetView**)&m_Descriptor); m_Type = DescriptorTypeDX11::NO_SHADER_VISIBLE; } @@ -434,7 +423,7 @@ Result DescriptorD3D11::Create(const VersionedDevice& device, const Texture3DVie return Result::SUCCESS; } -Result DescriptorD3D11::Create(const VersionedDevice& device, const BufferViewDesc& bufferViewDesc) +Result DescriptorD3D11::Create(const BufferViewDesc& bufferViewDesc) { const BufferD3D11& buffer = *(BufferD3D11*)bufferViewDesc.buffer; const BufferDesc& desc = buffer.GetDesc(); @@ -447,7 +436,7 @@ Result DescriptorD3D11::Create(const VersionedDevice& device, const BufferViewDe { format = Format::RGBA32_SFLOAT; - if (bufferViewDesc.offset != 0 && device.version == 0) + if (bufferViewDesc.offset != 0 && m_Device.GetDevice().version == 0) REPORT_ERROR(m_Device.GetLog(), "Constant buffers with non-zero offsets require 11.1+ feature level!"); } else if (stride) @@ -478,7 +467,7 @@ Result DescriptorD3D11::Create(const VersionedDevice& device, const BufferViewDe srv.Buffer.FirstElement = m_ElementOffset; srv.Buffer.NumElements = m_ElementNum; - hr = device->CreateShaderResourceView(buffer, &srv, (ID3D11ShaderResourceView**)&m_Descriptor); + hr = m_Device.GetDevice()->CreateShaderResourceView(buffer, &srv, (ID3D11ShaderResourceView**)&m_Descriptor); m_Type = DescriptorTypeDX11::RESOURCE; } @@ -492,7 +481,7 @@ Result DescriptorD3D11::Create(const VersionedDevice& device, const BufferViewDe uav.Buffer.FirstElement = m_ElementOffset; uav.Buffer.NumElements = m_ElementNum; - hr = device->CreateUnorderedAccessView(buffer, &uav, (ID3D11UnorderedAccessView**)&m_Descriptor); + hr = m_Device.GetDevice()->CreateUnorderedAccessView(buffer, &uav, (ID3D11UnorderedAccessView**)&m_Descriptor); m_Type = DescriptorTypeDX11::STORAGE; } @@ -507,7 +496,7 @@ Result DescriptorD3D11::Create(const VersionedDevice& device, const BufferViewDe return Result::SUCCESS; } -void DescriptorD3D11::CreateSamplerState(const Log& log, const VersionedDevice& device, const SamplerDesc& samplerDesc, ID3D11SamplerState** samplerState) +Result DescriptorD3D11::Create(const SamplerDesc& samplerDesc) { bool isAnisotropy = samplerDesc.anisotropy > 1; bool isComparison = samplerDesc.compareFunc != CompareFunc::NONE; @@ -535,14 +524,10 @@ void DescriptorD3D11::CreateSamplerState(const Log& log, const VersionedDevice& desc.BorderColor[3] = 1.0f; } - HRESULT hr = device->CreateSamplerState(&desc, samplerState); + HRESULT hr = m_Device.GetDevice()->CreateSamplerState(&desc, (ID3D11SamplerState**)&m_Descriptor); if (FAILED(hr)) - REPORT_ERROR(log, "ID3D11Device::CreateSamplerState() - FAILED!"); -} + REPORT_ERROR(m_Device.GetLog(), "ID3D11Device::CreateSamplerState() - FAILED!"); -Result DescriptorD3D11::Create(const VersionedDevice& device, const SamplerDesc& samplerDesc) -{ - CreateSamplerState(m_Device.GetLog(), device, samplerDesc, (ID3D11SamplerState**)&m_Descriptor); m_Type = DescriptorTypeDX11::SAMPLER; return m_Descriptor ? Result::SUCCESS : Result::FAILURE; @@ -592,9 +577,8 @@ DescriptorD3D11::DescriptorD3D11(DeviceD3D11& device, ID3D11SamplerState* sample m_Type = DescriptorTypeDX11::SAMPLER; } -void DescriptorD3D11::SetDebugName(const char* name) -{ - SetName(m_Descriptor, name); -} +//================================================================================================================ +// NRI +//================================================================================================================ #include "DescriptorD3D11.hpp" diff --git a/Source/D3D11/DescriptorD3D11.h b/Source/D3D11/DescriptorD3D11.h index eaad4b3c..f6070473 100644 --- a/Source/D3D11/DescriptorD3D11.h +++ b/Source/D3D11/DescriptorD3D11.h @@ -12,73 +12,76 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct DeviceD3D11; - - struct DescriptorD3D11 - { - DescriptorD3D11(DeviceD3D11& device); - ~DescriptorD3D11(); - - inline DeviceD3D11& GetDevice() const - { return m_Device; } - - inline operator void*() const - { return (void*)m_Descriptor.GetInterface(); } - - inline operator ID3D11View*() const - { return (ID3D11View*)m_Descriptor.GetInterface(); } - - inline operator ID3D11RenderTargetView*() const - { return (ID3D11RenderTargetView*)m_Descriptor.GetInterface(); } - - inline operator ID3D11DepthStencilView*() const - { return (ID3D11DepthStencilView*)m_Descriptor.GetInterface(); } - - inline operator ID3D11UnorderedAccessView*() const - { return (ID3D11UnorderedAccessView*)m_Descriptor.GetInterface(); } - - inline uint32_t GetElementOffset() const - { return m_ElementOffset; } - - inline uint32_t GetElementNum() const - { return m_ElementNum; } - - inline uint32_t IsIntegerFormat() const - { return m_IsIntegerFormat; } - - inline const SubresourceInfo& GetSubresourceInfo() const - { return m_SubresourceInfo; } - - static void CreateSamplerState(const Log& log, const VersionedDevice& device, const SamplerDesc& samplerDesc, ID3D11SamplerState** samplerState); - - Result Create(const VersionedDevice& device, const Texture1DViewDesc& textureViewDesc); - Result Create(const VersionedDevice& device, const Texture2DViewDesc& textureViewDesc); - Result Create(const VersionedDevice& device, const Texture3DViewDesc& textureViewDesc); - Result Create(const VersionedDevice& device, const BufferViewDesc& bufferViewDesc); - Result Create(const VersionedDevice& device, const SamplerDesc& samplerDesc); - - //====================================================================================================================== - // Native wrapper - //====================================================================================================================== - DescriptorD3D11(DeviceD3D11& device, ID3D11ShaderResourceView* resource); - DescriptorD3D11(DeviceD3D11& device, ID3D11UnorderedAccessView* storage); - DescriptorD3D11(DeviceD3D11& device, ID3D11RenderTargetView* randerTarget); - DescriptorD3D11(DeviceD3D11& device, ID3D11DepthStencilView* depthStencil); - DescriptorD3D11(DeviceD3D11& device, ID3D11Buffer* constantBuffer, uint32_t elementOffset = 0, uint32_t elementNum = 0); - DescriptorD3D11(DeviceD3D11& device, ID3D11SamplerState* sampler); - - //====================================================================================================================== - // NRI - //====================================================================================================================== - void SetDebugName(const char* name); - - private: - ComPtr m_Descriptor; - SubresourceInfo m_SubresourceInfo = {}; - uint32_t m_ElementOffset = 0; - uint32_t m_ElementNum = 0; - DescriptorTypeDX11 m_Type = DescriptorTypeDX11::NO_SHADER_VISIBLE; - bool m_IsIntegerFormat = false; - DeviceD3D11& m_Device; - }; + +struct DeviceD3D11; + +struct DescriptorD3D11 +{ + inline DescriptorD3D11(DeviceD3D11& device) : + m_Device(device) + {} + + inline ~DescriptorD3D11() + {} + + inline DeviceD3D11& GetDevice() const + { return m_Device; } + + inline operator void*() const + { return (void*)m_Descriptor.GetInterface(); } + + inline operator ID3D11View*() const + { return (ID3D11View*)m_Descriptor.GetInterface(); } + + inline operator ID3D11RenderTargetView*() const + { return (ID3D11RenderTargetView*)m_Descriptor.GetInterface(); } + + inline operator ID3D11DepthStencilView*() const + { return (ID3D11DepthStencilView*)m_Descriptor.GetInterface(); } + + inline operator ID3D11UnorderedAccessView*() const + { return (ID3D11UnorderedAccessView*)m_Descriptor.GetInterface(); } + + inline uint32_t GetElementOffset() const + { return m_ElementOffset; } + + inline uint32_t GetElementNum() const + { return m_ElementNum; } + + inline uint32_t IsIntegerFormat() const + { return m_IsIntegerFormat; } + + inline const SubresourceInfo& GetSubresourceInfo() const + { return m_SubresourceInfo; } + + Result Create(const Texture1DViewDesc& textureViewDesc); + Result Create(const Texture2DViewDesc& textureViewDesc); + Result Create(const Texture3DViewDesc& textureViewDesc); + Result Create(const BufferViewDesc& bufferViewDesc); + Result Create(const SamplerDesc& samplerDesc); + + DescriptorD3D11(DeviceD3D11& device, ID3D11ShaderResourceView* resource); + DescriptorD3D11(DeviceD3D11& device, ID3D11UnorderedAccessView* storage); + DescriptorD3D11(DeviceD3D11& device, ID3D11RenderTargetView* randerTarget); + DescriptorD3D11(DeviceD3D11& device, ID3D11DepthStencilView* depthStencil); + DescriptorD3D11(DeviceD3D11& device, ID3D11Buffer* constantBuffer, uint32_t elementOffset = 0, uint32_t elementNum = 0); + DescriptorD3D11(DeviceD3D11& device, ID3D11SamplerState* sampler); + + //================================================================================================================ + // NRI + //================================================================================================================ + + inline void SetDebugName(const char* name) + { SET_D3D_DEBUG_OBJECT_NAME(m_Descriptor, name); } + +private: + DeviceD3D11& m_Device; + ComPtr m_Descriptor; + SubresourceInfo m_SubresourceInfo = {}; + uint32_t m_ElementOffset = 0; + uint32_t m_ElementNum = 0; + DescriptorTypeDX11 m_Type = DescriptorTypeDX11::NO_SHADER_VISIBLE; + bool m_IsIntegerFormat = false; +}; + } diff --git a/Source/D3D11/DescriptorD3D11.hpp b/Source/D3D11/DescriptorD3D11.hpp index 667c26af..8d4d92f2 100644 --- a/Source/D3D11/DescriptorD3D11.hpp +++ b/Source/D3D11/DescriptorD3D11.hpp @@ -8,7 +8,7 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ CoreInterface ] +#pragma region [ Core ] static void NRI_CALL SetDescriptorDebugName(Descriptor& descriptor, const char* name) { @@ -22,11 +22,6 @@ static uint64_t NRI_CALL GetDescriptorNativeObject(const Descriptor& descriptor, return uint64_t( (ID3D11View*)((DescriptorD3D11&)descriptor) ); } -void FillFunctionTableDescriptorD3D11(CoreInterface& coreInterface) -{ - coreInterface.SetDescriptorDebugName = ::SetDescriptorDebugName; - - coreInterface.GetDescriptorNativeObject = ::GetDescriptorNativeObject; -} - #pragma endregion + +Define_Core_Descriptor_PartiallyFillFunctionTable(D3D11) \ No newline at end of file diff --git a/Source/D3D11/DescriptorPoolD3D11.cpp b/Source/D3D11/DescriptorPoolD3D11.cpp index e6c728d8..32df43d0 100644 --- a/Source/D3D11/DescriptorPoolD3D11.cpp +++ b/Source/D3D11/DescriptorPoolD3D11.cpp @@ -8,22 +8,12 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#include "SharedExternal.h" #include "SharedD3D11.h" #include "DescriptorSetD3D11.h" #include "DescriptorPoolD3D11.h" -#include "PipelineLayoutD3D11.h" - using namespace nri; -DescriptorPoolD3D11::DescriptorPoolD3D11(DeviceD3D11& device) : - m_Sets(device.GetStdAllocator()), - m_Pool(device.GetStdAllocator()), - m_Device(device) -{ -} - Result DescriptorPoolD3D11::Create(const DescriptorPoolDesc& descriptorPoolDesc) { uint32_t descriptorNum = descriptorPoolDesc.samplerMaxNum; @@ -43,10 +33,9 @@ Result DescriptorPoolD3D11::Create(const DescriptorPoolDesc& descriptorPoolDesc) return Result::SUCCESS; } -inline void DescriptorPoolD3D11::SetDebugName(const char* name) -{ - MaybeUnused(name); -} +//================================================================================================================ +// NRI +//================================================================================================================ inline Result DescriptorPoolD3D11::AllocateDescriptorSets(const PipelineLayout& pipelineLayout, uint32_t setIndexInPipelineLayout, DescriptorSet** descriptorSets, uint32_t instanceNum, uint32_t physicalDeviceMask, uint32_t variableDescriptorNum) @@ -70,10 +59,4 @@ inline Result DescriptorPoolD3D11::AllocateDescriptorSets(const PipelineLayout& return Result::SUCCESS; } -inline void DescriptorPoolD3D11::Reset() -{ - m_DescriptorPoolOffset = 0; - m_DescriptorSetIndex = 0; -} - #include "DescriptorPoolD3D11.hpp" diff --git a/Source/D3D11/DescriptorPoolD3D11.h b/Source/D3D11/DescriptorPoolD3D11.h index 516adadc..6c5f14ea 100644 --- a/Source/D3D11/DescriptorPoolD3D11.h +++ b/Source/D3D11/DescriptorPoolD3D11.h @@ -12,31 +12,48 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct DeviceD3D11; - struct DescriptorD3D11; - struct DescriptorPoolD3D11 +struct DeviceD3D11; +struct DescriptorD3D11; + +struct DescriptorPoolD3D11 +{ + inline DescriptorPoolD3D11(DeviceD3D11& device) : + m_Sets(device.GetStdAllocator()), + m_Pool(device.GetStdAllocator()), + m_Device(device) + {} + + inline ~DescriptorPoolD3D11() + {} + + inline DeviceD3D11& GetDevice() const + { return m_Device; } + + Result Create(const DescriptorPoolDesc& descriptorPoolDesc); + + //================================================================================================================ + // NRI + //================================================================================================================ + + inline void SetDebugName(const char* name) + { MaybeUnused(name); } + + inline void Reset() { - DescriptorPoolD3D11(DeviceD3D11& device); - - inline DeviceD3D11& GetDevice() const - { return m_Device; } - - Result Create(const DescriptorPoolDesc& descriptorPoolDesc); - - //====================================================================================================================== - // NRI - //====================================================================================================================== - void SetDebugName(const char* name); - Result AllocateDescriptorSets(const PipelineLayout& pipelineLayout, uint32_t setIndexInPipelineLayout, DescriptorSet** descriptorSets, - uint32_t instanceNum, uint32_t physicalDeviceMask, uint32_t variableDescriptorNum); - void Reset(); - - private: - Vector m_Sets; - Vector m_Pool; - uint32_t m_DescriptorPoolOffset = 0; - uint32_t m_DescriptorSetIndex = 0; - DeviceD3D11& m_Device; - }; + m_DescriptorPoolOffset = 0; + m_DescriptorSetIndex = 0; + } + + Result AllocateDescriptorSets(const PipelineLayout& pipelineLayout, uint32_t setIndexInPipelineLayout, DescriptorSet** descriptorSets, + uint32_t instanceNum, uint32_t physicalDeviceMask, uint32_t variableDescriptorNum); + +private: + DeviceD3D11& m_Device; + Vector m_Sets; + Vector m_Pool; + uint32_t m_DescriptorPoolOffset = 0; + uint32_t m_DescriptorSetIndex = 0; +}; + } diff --git a/Source/D3D11/DescriptorPoolD3D11.hpp b/Source/D3D11/DescriptorPoolD3D11.hpp index 749572c0..96f7be33 100644 --- a/Source/D3D11/DescriptorPoolD3D11.hpp +++ b/Source/D3D11/DescriptorPoolD3D11.hpp @@ -8,7 +8,7 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ CoreInterface ] +#pragma region [ Core ] static void NRI_CALL SetDescriptorPoolDebugName(DescriptorPool& descriptorPool, const char* name) { @@ -27,11 +27,6 @@ static void NRI_CALL ResetDescriptorPool(DescriptorPool& descriptorPool) ((DescriptorPoolD3D11&)descriptorPool).Reset(); } -void FillFunctionTableDescriptorPoolD3D11(CoreInterface& coreInterface) -{ - coreInterface.SetDescriptorPoolDebugName = ::SetDescriptorPoolDebugName; - coreInterface.AllocateDescriptorSets = ::AllocateDescriptorSets; - coreInterface.ResetDescriptorPool = ::ResetDescriptorPool; -} - #pragma endregion + +Define_Core_DescriptorPool_PartiallyFillFunctionTable(D3D11) diff --git a/Source/D3D11/DescriptorSetD3D11.cpp b/Source/D3D11/DescriptorSetD3D11.cpp index 103e1b9c..879d953a 100644 --- a/Source/D3D11/DescriptorSetD3D11.cpp +++ b/Source/D3D11/DescriptorSetD3D11.cpp @@ -8,20 +8,12 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#include "SharedExternal.h" #include "SharedD3D11.h" #include "DescriptorSetD3D11.h" - #include "PipelineLayoutD3D11.h" using namespace nri; -DescriptorSetD3D11::DescriptorSetD3D11(DeviceD3D11& device) : - m_Ranges(device.GetStdAllocator()), - m_DynamicConstantBuffers(device.GetStdAllocator()) -{ -} - uint32_t DescriptorSetD3D11::Initialize(const PipelineLayoutD3D11& pipelineLayout, uint32_t setIndexInPipelineLayout, const DescriptorD3D11** descriptors) { const BindingSet& bindingSet = pipelineLayout.GetBindingSet(setIndexInPipelineLayout); @@ -45,10 +37,9 @@ uint32_t DescriptorSetD3D11::Initialize(const PipelineLayoutD3D11& pipelineLayou return bindingSet.descriptorNum; } -inline void DescriptorSetD3D11::SetDebugName(const char* name) -{ - MaybeUnused(name); -} +//================================================================================================================ +// NRI +//================================================================================================================ inline void DescriptorSetD3D11::UpdateDescriptorRanges(uint32_t rangeOffset, uint32_t rangeNum, const DescriptorRangeUpdateDesc* rangeUpdateDescs) { diff --git a/Source/D3D11/DescriptorSetD3D11.h b/Source/D3D11/DescriptorSetD3D11.h index bf27b2a3..ecc31951 100644 --- a/Source/D3D11/DescriptorSetD3D11.h +++ b/Source/D3D11/DescriptorSetD3D11.h @@ -12,38 +12,46 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct DescriptorD3D11; - struct PipelineLayoutD3D11; - - struct OffsetNum - { - uint32_t descriptorOffset; - uint32_t descriptorNum; - }; - - struct DescriptorSetD3D11 - { - DescriptorSetD3D11(DeviceD3D11& device); - - inline const DescriptorD3D11* GetDescriptor(uint32_t i) const - { return m_Descriptors[i]; } - - inline uint32_t GetDynamicConstantBufferNum() const - { return (uint32_t)m_DynamicConstantBuffers.size(); } - - uint32_t Initialize(const PipelineLayoutD3D11& pipelineLayout, uint32_t setIndexInPipelineLayout, const DescriptorD3D11** descriptors); - - //====================================================================================================================== - // NRI - //====================================================================================================================== - void SetDebugName(const char* name); - void UpdateDescriptorRanges(uint32_t rangeOffset, uint32_t rangeNum, const DescriptorRangeUpdateDesc* rangeUpdateDescs); - void UpdateDynamicConstantBuffers(uint32_t baseBuffer, uint32_t bufferNum, const Descriptor* const* descriptors); - void Copy(const DescriptorSetCopyDesc& descriptorSetCopyDesc); - - private: - const DescriptorD3D11** m_Descriptors = nullptr; - Vector m_Ranges; - Vector m_DynamicConstantBuffers; - }; + +struct DescriptorD3D11; +struct PipelineLayoutD3D11; + +struct OffsetNum +{ + uint32_t descriptorOffset; + uint32_t descriptorNum; +}; + +struct DescriptorSetD3D11 +{ + inline DescriptorSetD3D11(DeviceD3D11& device) : + m_Ranges(device.GetStdAllocator()) + , m_DynamicConstantBuffers(device.GetStdAllocator()) + {} + + inline const DescriptorD3D11* GetDescriptor(uint32_t i) const + { return m_Descriptors[i]; } + + inline uint32_t GetDynamicConstantBufferNum() const + { return (uint32_t)m_DynamicConstantBuffers.size(); } + + uint32_t Initialize(const PipelineLayoutD3D11& pipelineLayout, uint32_t setIndexInPipelineLayout, const DescriptorD3D11** descriptors); + + //================================================================================================================ + // NRI + //================================================================================================================ + + inline void SetDebugName(const char* name) + { MaybeUnused(name); } + + void UpdateDescriptorRanges(uint32_t rangeOffset, uint32_t rangeNum, const DescriptorRangeUpdateDesc* rangeUpdateDescs); + void UpdateDynamicConstantBuffers(uint32_t baseBuffer, uint32_t bufferNum, const Descriptor* const* descriptors); + void Copy(const DescriptorSetCopyDesc& descriptorSetCopyDesc); + +private: + const DescriptorD3D11** m_Descriptors = nullptr; + Vector m_Ranges; + Vector m_DynamicConstantBuffers; +}; + } diff --git a/Source/D3D11/DescriptorSetD3D11.hpp b/Source/D3D11/DescriptorSetD3D11.hpp index 997a17c7..1e852107 100644 --- a/Source/D3D11/DescriptorSetD3D11.hpp +++ b/Source/D3D11/DescriptorSetD3D11.hpp @@ -8,7 +8,7 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ CoreInterface ] +#pragma region [ Core ] static void NRI_CALL SetDescriptorSetDebugName(DescriptorSet& descriptorSet, const char* name) { @@ -34,12 +34,6 @@ static void NRI_CALL CopyDescriptorSet(DescriptorSet& descriptorSet, const Descr ((DescriptorSetD3D11&)descriptorSet).Copy(descriptorSetCopyDesc); } -void FillFunctionTableDescriptorSetD3D11(CoreInterface& coreInterface) -{ - coreInterface.SetDescriptorSetDebugName = ::SetDescriptorSetDebugName; - coreInterface.UpdateDescriptorRanges = ::UpdateDescriptorRanges; - coreInterface.UpdateDynamicConstantBuffers = ::UpdateDynamicConstantBuffers; - coreInterface.CopyDescriptorSet = ::CopyDescriptorSet; -} - #pragma endregion + +Define_Core_DescriptorSet_PartiallyFillFunctionTable(D3D11) diff --git a/Source/D3D11/DeviceD3D11.cpp b/Source/D3D11/DeviceD3D11.cpp index 51f9cbd0..4ae685ae 100644 --- a/Source/D3D11/DeviceD3D11.cpp +++ b/Source/D3D11/DeviceD3D11.cpp @@ -8,23 +8,20 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#include "SharedExternal.h" #include "SharedD3D11.h" #include "DeviceD3D11.h" - #include "BufferD3D11.h" #include "CommandQueueD3D11.h" #include "CommandAllocatorD3D11.h" #include "DescriptorD3D11.h" #include "DescriptorSetD3D11.h" #include "DescriptorPoolD3D11.h" -#include "DeviceSemaphoreD3D11.h" +#include "FenceD3D11.h" #include "FrameBufferD3D11.h" #include "MemoryD3D11.h" #include "PipelineLayoutD3D11.h" #include "PipelineD3D11.h" #include "QueryPoolD3D11.h" -#include "QueueSemaphoreD3D11.h" #include "SwapChainD3D11.h" #include "TextureD3D11.h" @@ -33,11 +30,46 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. using namespace nri; +Result CreateDeviceD3D11(const DeviceCreationDesc& deviceCreationDesc, DeviceBase*& device) +{ + Log log(GraphicsAPI::D3D11, deviceCreationDesc.callbackInterface); + StdAllocator allocator(deviceCreationDesc.memoryAllocatorInterface); + + ComPtr factory; + HRESULT hr = CreateDXGIFactory2(0, IID_PPV_ARGS(&factory)); + RETURN_ON_BAD_HRESULT(log, hr, "Can't create D3D11 device. CreateDXGIFactory2() failed. (result: %d)", (int32_t)hr); + + ComPtr adapter; + if (deviceCreationDesc.physicalDeviceGroup != nullptr) + { + LUID luid = *(LUID*)&deviceCreationDesc.physicalDeviceGroup->luid; + hr = factory->EnumAdapterByLuid(luid, IID_PPV_ARGS(&adapter)); + RETURN_ON_BAD_HRESULT(log, hr, "Can't create D3D11 device. IDXGIFactory4::EnumAdapterByLuid() failed. (result: %d)", (int32_t)hr); + } + else + { + hr = factory->EnumAdapters(0, &adapter); + RETURN_ON_BAD_HRESULT(log, hr, "Can't create D3D11 device. IDXGIFactory4::EnumAdapters() failed. (result: %d)", (int32_t)hr); + } + + DeviceD3D11* implementation = Allocate(allocator, log, allocator); + const nri::Result result = implementation->Create(deviceCreationDesc, adapter, nullptr, nullptr); + + if (result == nri::Result::SUCCESS) + { + device = (DeviceBase*)implementation; + return nri::Result::SUCCESS; + } + + Deallocate(allocator, implementation); + + return result; +} + DeviceD3D11::DeviceD3D11(const Log& log, StdAllocator& stdAllocator) : DeviceBase(log, stdAllocator) , m_CommandQueues(GetStdAllocator()) -{ -} +{} DeviceD3D11::~DeviceD3D11() { @@ -67,6 +99,7 @@ Result DeviceD3D11::Create(const DeviceCreationDesc& deviceCreationDesc, IDXGIAd m_Ext.Create(GetLog(), vendor, agsContext, device != nullptr); + m_Device.ptr = (ID3D11Device5*)device; if (!device) { const UINT flags = deviceCreationDesc.enableAPIValidation ? D3D11_CREATE_DEVICE_DEBUG : 0; @@ -84,10 +117,10 @@ Result DeviceD3D11::Create(const DeviceCreationDesc& deviceCreationDesc, IDXGIAd } else { - HRESULT hr = D3D11CreateDevice(adapter, D3D_DRIVER_TYPE_UNKNOWN, nullptr, flags, levels.data(), (uint32_t)levels.size(), D3D11_SDK_VERSION, &device, nullptr, nullptr); + HRESULT hr = D3D11CreateDevice(adapter, D3D_DRIVER_TYPE_UNKNOWN, nullptr, flags, levels.data(), (uint32_t)levels.size(), D3D11_SDK_VERSION, (ID3D11Device**)&m_Device.ptr, nullptr, nullptr); if (flags && (uint32_t)hr == 0x887a002d) - hr = D3D11CreateDevice(adapter, D3D_DRIVER_TYPE_UNKNOWN, nullptr, 0, &levels[0], (uint32_t)levels.size(), D3D11_SDK_VERSION, &device, nullptr, nullptr); + hr = D3D11CreateDevice(adapter, D3D_DRIVER_TYPE_UNKNOWN, nullptr, 0, &levels[0], (uint32_t)levels.size(), D3D11_SDK_VERSION, (ID3D11Device**)&m_Device.ptr, nullptr, nullptr); RETURN_ON_BAD_HRESULT(GetLog(), hr, "D3D11CreateDevice() - FAILED!"); } @@ -95,12 +128,12 @@ Result DeviceD3D11::Create(const DeviceCreationDesc& deviceCreationDesc, IDXGIAd else device->AddRef(); - InitVersionedDevice(device, deviceCreationDesc.D3D11CommandBufferEmulation); + InitVersionedDevice(deviceCreationDesc.D3D11CommandBufferEmulation); InitVersionedContext(); FillLimits(deviceCreationDesc.enableAPIValidation, vendor); for (uint32_t i = 0; i < COMMAND_QUEUE_TYPE_NUM; i++) - m_CommandQueues.emplace_back(*this, m_ImmediateContext); + m_CommandQueues.emplace_back(*this); if (FillFunctionTable(m_CoreInterface) != Result::SUCCESS) REPORT_ERROR(GetLog(), "Failed to get 'CoreInterface' interface in DeviceD3D11()."); @@ -108,94 +141,100 @@ Result DeviceD3D11::Create(const DeviceCreationDesc& deviceCreationDesc, IDXGIAd return Result::SUCCESS; } -void DeviceD3D11::InitVersionedDevice(ID3D11Device* device, bool isDeferredContextsEmulationRequested) +void DeviceD3D11::InitVersionedDevice(bool isDeferredContextsEmulationRequested) { - HRESULT hr = device->QueryInterface(__uuidof(ID3D11Device5), (void**)&m_Device.ptr); - m_Device.version = 5; m_Device.ext = &m_Ext; + + ComPtr versionedDevice; + HRESULT hr = m_Device->QueryInterface(__uuidof(ID3D11Device5), (void**)&versionedDevice); + m_Device.version = 5; if (FAILED(hr)) { REPORT_WARNING(GetLog(), "QueryInterface(ID3D11Device5) - FAILED!"); - hr = device->QueryInterface(__uuidof(ID3D11Device4), (void**)&m_Device.ptr); + hr = m_Device->QueryInterface(__uuidof(ID3D11Device4), (void**)&versionedDevice); m_Device.version = 4; if (FAILED(hr)) { REPORT_WARNING(GetLog(), "QueryInterface(ID3D11Device4) - FAILED!"); - hr = device->QueryInterface(__uuidof(ID3D11Device3), (void**)&m_Device.ptr); + hr = m_Device->QueryInterface(__uuidof(ID3D11Device3), (void**)&versionedDevice); m_Device.version = 3; if (FAILED(hr)) { REPORT_WARNING(GetLog(), "QueryInterface(ID3D11Device3) - FAILED!"); - hr = device->QueryInterface(__uuidof(ID3D11Device2), (void**)&m_Device.ptr); + hr = m_Device->QueryInterface(__uuidof(ID3D11Device2), (void**)&versionedDevice); m_Device.version = 2; if (FAILED(hr)) { REPORT_WARNING(GetLog(), "QueryInterface(ID3D11Device2) - FAILED!"); - hr = device->QueryInterface(__uuidof(ID3D11Device1), (void**)&m_Device.ptr); + hr = m_Device->QueryInterface(__uuidof(ID3D11Device1), (void**)&versionedDevice); m_Device.version = 1; if (FAILED(hr)) { REPORT_WARNING(GetLog(), "QueryInterface(ID3D11Device1) - FAILED!"); - m_Device.ptr = (ID3D11Device5*)device; m_Device.version = 0; + versionedDevice = m_Device.ptr; } } } } } + m_Device.ptr = versionedDevice; + D3D11_FEATURE_DATA_THREADING threadingCaps = {}; - hr = device->CheckFeatureSupport(D3D11_FEATURE_THREADING, &threadingCaps, sizeof(threadingCaps)); + hr = m_Device->CheckFeatureSupport(D3D11_FEATURE_THREADING, &threadingCaps, sizeof(threadingCaps)); if (FAILED(hr) || !threadingCaps.DriverConcurrentCreates) REPORT_WARNING(GetLog(), "Concurrent resource creation is not supported by the driver!"); - m_Device.isDeferredContextsEmulated = !m_Ext.IsNvAPIAvailable() || isDeferredContextsEmulationRequested; + m_Device.isDeferredContextEmulated = !m_Ext.IsNvAPIAvailable() || isDeferredContextsEmulationRequested; if (!threadingCaps.DriverCommandLists) { REPORT_WARNING(GetLog(), "Deferred Contexts are not supported by the driver and will be emulated!"); - m_Device.isDeferredContextsEmulated = true; + m_Device.isDeferredContextEmulated = true; } } void DeviceD3D11::InitVersionedContext() { - ID3D11DeviceContext* immediateContext = nullptr; - m_Device.ptr->GetImmediateContext(&immediateContext); - immediateContext->Release(); + m_ImmediateContext.ext = &m_Ext; - HRESULT hr = immediateContext->QueryInterface(__uuidof(ID3D11DeviceContext4), (void**)&m_ImmediateContext.ptr); + m_Device.ptr->GetImmediateContext((ID3D11DeviceContext**)&m_ImmediateContext.ptr); + + ComPtr versionedImmediateContext; + HRESULT hr = m_ImmediateContext->QueryInterface(__uuidof(ID3D11DeviceContext4), (void**)&versionedImmediateContext); m_ImmediateContext.version = 4; - m_ImmediateContext.ext = &m_Ext; if (FAILED(hr)) { REPORT_WARNING(GetLog(), "QueryInterface(ID3D11DeviceContext4) - FAILED!"); - hr = immediateContext->QueryInterface(__uuidof(ID3D11DeviceContext3), (void**)&m_ImmediateContext.ptr); + hr = m_ImmediateContext->QueryInterface(__uuidof(ID3D11DeviceContext3), (void**)&versionedImmediateContext); m_ImmediateContext.version = 3; if (FAILED(hr)) { REPORT_WARNING(GetLog(), "QueryInterface(ID3D11DeviceContext3) - FAILED!"); - hr = immediateContext->QueryInterface(__uuidof(ID3D11DeviceContext2), (void**)&m_ImmediateContext.ptr); + hr = m_ImmediateContext->QueryInterface(__uuidof(ID3D11DeviceContext2), (void**)&versionedImmediateContext); m_ImmediateContext.version = 2; if (FAILED(hr)) { REPORT_WARNING(GetLog(), "QueryInterface(ID3D11DeviceContext2) - FAILED!"); - hr = immediateContext->QueryInterface(__uuidof(ID3D11DeviceContext1), (void**)&m_ImmediateContext.ptr); + hr = m_ImmediateContext->QueryInterface(__uuidof(ID3D11DeviceContext1), (void**)&versionedImmediateContext); m_ImmediateContext.version = 1; if (FAILED(hr)) { REPORT_WARNING(GetLog(), "QueryInterface(ID3D11DeviceContext1) - FAILED!"); - m_ImmediateContext.ptr = (ID3D11DeviceContext4*)immediateContext; m_ImmediateContext.version = 0; + versionedImmediateContext = m_ImmediateContext.ptr; } } } } + m_ImmediateContext.ptr = versionedImmediateContext; + InitializeCriticalSection(&m_CriticalSection); - hr = immediateContext->QueryInterface(IID_PPV_ARGS(&m_ImmediateContext.multiThread)); + hr = m_ImmediateContext->QueryInterface(IID_PPV_ARGS(&m_ImmediateContext.multiThread)); if (FAILED(hr)) { REPORT_WARNING(GetLog(), "QueryInterface(ID3D11Multithread) - FAILED! Critical section will be used instead!"); @@ -370,9 +409,48 @@ void DeviceD3D11::FillLimits(bool isValidationEnabled, Vendor vendor) m_Desc.isSubsetAllocationSupported = true; } +template +nri::Result DeviceD3D11::CreateImplementationWithNonEmptyConstructor(Interface*& entity, ConstructorArg&& constructorArg, const Args&... args) +{ + Implementation* implementation = Allocate(GetStdAllocator(), constructorArg); + const nri::Result res = implementation->Create(args...); + + if (res == nri::Result::SUCCESS) + { + entity = (Interface*)implementation; + return nri::Result::SUCCESS; + } + + Deallocate(GetStdAllocator(), implementation); + + return res; +} + +//================================================================================================================ +// DeviceBase +//================================================================================================================ + +void DeviceD3D11::Destroy() +{ + bool skipLiveObjectsReporting = m_SkipLiveObjectsReporting; + Deallocate(GetStdAllocator(), this); + + if (!skipLiveObjectsReporting) + { + ComPtr pDebug; + HRESULT hr = DXGIGetDebugInterface1(0, IID_PPV_ARGS(&pDebug)); + if (SUCCEEDED(hr)) + pDebug->ReportLiveObjects(DXGI_DEBUG_ALL, (DXGI_DEBUG_RLO_FLAGS)((uint32_t)DXGI_DEBUG_RLO_DETAIL | (uint32_t)DXGI_DEBUG_RLO_IGNORE_INTERNAL)); + } +} + +//================================================================================================================ +// NRI +//================================================================================================================ + inline Result DeviceD3D11::CreateSwapChain(const SwapChainDesc& swapChainDesc, SwapChain*& swapChain) { - return CreateImplementationWithNonEmptyConstructor(swapChain, *this, m_Device, swapChainDesc); + return CreateImplementationWithNonEmptyConstructor(swapChain, *this, swapChainDesc); } inline void DeviceD3D11::DestroySwapChain(SwapChain& swapChain) @@ -447,11 +525,6 @@ inline Result DeviceD3D11::GetDisplaySize(Display& display, uint16_t& width, uin return Result::SUCCESS; } -inline void DeviceD3D11::SetDebugName(const char* name) -{ - SetName(m_Device.ptr, name); -} - inline Result DeviceD3D11::GetCommandQueue(CommandQueueType commandQueueType, CommandQueue*& commandQueue) { commandQueue = (CommandQueue*)&m_CommandQueues[(uint32_t)commandQueueType]; @@ -463,7 +536,7 @@ inline Result DeviceD3D11::CreateCommandAllocator(const CommandQueue& commandQue { MaybeUnused(commandQueue); - commandAllocator = (CommandAllocator*)Allocate(GetStdAllocator(), *this, m_Device); + commandAllocator = (CommandAllocator*)Allocate(GetStdAllocator(), *this); return Result::SUCCESS; } @@ -475,7 +548,7 @@ inline Result DeviceD3D11::CreateDescriptorPool(const DescriptorPoolDesc& descri inline Result DeviceD3D11::CreateBuffer(const BufferDesc& bufferDesc, Buffer*& buffer) { - buffer = (Buffer*)Allocate(GetStdAllocator(), *this, m_ImmediateContext, bufferDesc); + buffer = (Buffer*)Allocate(GetStdAllocator(), *this, bufferDesc); return Result::SUCCESS; } @@ -489,32 +562,32 @@ inline Result DeviceD3D11::CreateTexture(const TextureDesc& textureDesc, Texture inline Result DeviceD3D11::CreateDescriptor(const BufferViewDesc& bufferViewDesc, Descriptor*& bufferView) { - return CreateImplementationWithNonEmptyConstructor(bufferView, *this, m_Device, bufferViewDesc); + return CreateImplementationWithNonEmptyConstructor(bufferView, *this, bufferViewDesc); } inline Result DeviceD3D11::CreateDescriptor(const Texture1DViewDesc& textureViewDesc, Descriptor*& textureView) { - return CreateImplementationWithNonEmptyConstructor(textureView, *this, m_Device, textureViewDesc); + return CreateImplementationWithNonEmptyConstructor(textureView, *this, textureViewDesc); } inline Result DeviceD3D11::CreateDescriptor(const Texture2DViewDesc& textureViewDesc, Descriptor*& textureView) { - return CreateImplementationWithNonEmptyConstructor(textureView, *this, m_Device, textureViewDesc); + return CreateImplementationWithNonEmptyConstructor(textureView, *this, textureViewDesc); } inline Result DeviceD3D11::CreateDescriptor(const Texture3DViewDesc& textureViewDesc, Descriptor*& textureView) { - return CreateImplementationWithNonEmptyConstructor(textureView, *this, m_Device, textureViewDesc); + return CreateImplementationWithNonEmptyConstructor(textureView, *this, textureViewDesc); } inline Result DeviceD3D11::CreateDescriptor(const SamplerDesc& samplerDesc, Descriptor*& sampler) { - return CreateImplementationWithNonEmptyConstructor(sampler, *this, m_Device, samplerDesc); + return CreateImplementationWithNonEmptyConstructor(sampler, *this, samplerDesc); } inline Result DeviceD3D11::CreatePipelineLayout(const PipelineLayoutDesc& pipelineLayoutDesc, PipelineLayout*& pipelineLayout) { - PipelineLayoutD3D11* implementation = Allocate(GetStdAllocator(), *this, m_Device); + PipelineLayoutD3D11* implementation = Allocate(GetStdAllocator(), *this); const nri::Result res = implementation->Create(pipelineLayoutDesc); if (res == nri::Result::SUCCESS) @@ -524,12 +597,13 @@ inline Result DeviceD3D11::CreatePipelineLayout(const PipelineLayoutDesc& pipeli } Deallocate(GetStdAllocator(), implementation); + return res; } inline Result DeviceD3D11::CreatePipeline(const GraphicsPipelineDesc& graphicsPipelineDesc, Pipeline*& pipeline) { - PipelineD3D11* implementation = Allocate(GetStdAllocator(), *this, &m_Device); + PipelineD3D11* implementation = Allocate(GetStdAllocator(), *this); const nri::Result res = implementation->Create(graphicsPipelineDesc); if (res == nri::Result::SUCCESS) @@ -539,12 +613,13 @@ inline Result DeviceD3D11::CreatePipeline(const GraphicsPipelineDesc& graphicsPi } Deallocate(GetStdAllocator(), implementation); + return res; } inline Result DeviceD3D11::CreatePipeline(const ComputePipelineDesc& computePipelineDesc, Pipeline*& pipeline) { - PipelineD3D11* implementation = Allocate(GetStdAllocator(), *this, &m_Device); + PipelineD3D11* implementation = Allocate(GetStdAllocator(), *this); const nri::Result res = implementation->Create(computePipelineDesc); if (res == nri::Result::SUCCESS) @@ -554,6 +629,7 @@ inline Result DeviceD3D11::CreatePipeline(const ComputePipelineDesc& computePipe } Deallocate(GetStdAllocator(), implementation); + return res; } @@ -566,28 +642,22 @@ inline Result DeviceD3D11::CreateFrameBuffer(const FrameBufferDesc& frameBufferD inline Result DeviceD3D11::CreateQueryPool(const QueryPoolDesc& queryPoolDesc, QueryPool*& queryPool) { - return CreateImplementationWithNonEmptyConstructor(queryPool, *this, m_Device, queryPoolDesc); -} - -inline Result DeviceD3D11::CreateQueueSemaphore(QueueSemaphore*& queueSemaphore) -{ - queueSemaphore = (QueueSemaphore*)Allocate(GetStdAllocator(), *this); - - return Result::SUCCESS; + return CreateImplementationWithNonEmptyConstructor(queryPool, *this, queryPoolDesc); } -inline Result DeviceD3D11::CreateDeviceSemaphore(bool signaled, DeviceSemaphore*& deviceSemaphore) +inline Result DeviceD3D11::CreateFence(uint64_t initialValue, Fence*& fence) { - DeviceSemaphoreD3D11* implementation = Allocate(GetStdAllocator(), *this, m_Device); - const nri::Result res = implementation->Create(signaled); + FenceD3D11* implementation = Allocate(GetStdAllocator(), *this); + const nri::Result res = implementation->Create(initialValue); if (res == nri::Result::SUCCESS) { - deviceSemaphore = (DeviceSemaphore*)implementation; + fence = (Fence*)implementation; return nri::Result::SUCCESS; } Deallocate(GetStdAllocator(), implementation); + return res; } @@ -636,14 +706,9 @@ inline void DeviceD3D11::DestroyQueryPool(QueryPool& queryPool) Deallocate(GetStdAllocator(), (QueryPoolD3D11*)&queryPool); } -inline void DeviceD3D11::DestroyQueueSemaphore(QueueSemaphore& queueSemaphore) +inline void DeviceD3D11::DestroyFence(Fence& fence) { - Deallocate(GetStdAllocator(), (QueueSemaphoreD3D11*)&queueSemaphore); -} - -inline void DeviceD3D11::DestroyDeviceSemaphore(DeviceSemaphore& deviceSemaphore) -{ - Deallocate(GetStdAllocator(), (DeviceSemaphoreD3D11*)&deviceSemaphore); + Deallocate(GetStdAllocator(), (FenceD3D11*)&fence); } inline Result DeviceD3D11::AllocateMemory(MemoryType memoryType, uint64_t size, Memory*& memory) @@ -660,7 +725,7 @@ inline Result DeviceD3D11::BindBufferMemory(const BufferMemoryBindingDesc* memor for (uint32_t i = 0; i < memoryBindingDescNum; i++) { const BufferMemoryBindingDesc& desc = memoryBindingDescs[i]; - Result res = ((BufferD3D11*)desc.buffer)->Create(m_Device, *(MemoryD3D11*)desc.memory); + Result res = ((BufferD3D11*)desc.buffer)->Create(*(MemoryD3D11*)desc.memory); if (res != Result::SUCCESS) return res; } @@ -673,7 +738,7 @@ inline Result DeviceD3D11::BindTextureMemory(const TextureMemoryBindingDesc* mem for (uint32_t i = 0; i < memoryBindingDescNum; i++) { const TextureMemoryBindingDesc& desc = memoryBindingDescs[i]; - Result res = ((TextureD3D11*)desc.texture)->Create(m_Device, (MemoryD3D11*)desc.memory); + Result res = ((TextureD3D11*)desc.texture)->Create((MemoryD3D11*)desc.memory); if (res != Result::SUCCESS) return res; } @@ -707,70 +772,4 @@ inline Result DeviceD3D11::AllocateAndBindMemory(const ResourceGroupDesc& resour return allocator.AllocateAndBindMemory(resourceGroupDesc, allocations); } -void DeviceD3D11::Destroy() -{ - bool skipLiveObjectsReporting = m_SkipLiveObjectsReporting; - Deallocate(GetStdAllocator(), this); - - if (!skipLiveObjectsReporting) - { - ComPtr pDebug; - HRESULT hr = DXGIGetDebugInterface1(0, IID_PPV_ARGS(&pDebug)); - if (SUCCEEDED(hr)) - pDebug->ReportLiveObjects(DXGI_DEBUG_ALL, (DXGI_DEBUG_RLO_FLAGS)((uint32_t)DXGI_DEBUG_RLO_DETAIL | (uint32_t)DXGI_DEBUG_RLO_IGNORE_INTERNAL)); - } -} - -template -nri::Result DeviceD3D11::CreateImplementationWithNonEmptyConstructor(Interface*& entity, ConstructorArg&& constructorArg, const Args&... args) -{ - Implementation* implementation = Allocate(GetStdAllocator(), constructorArg); - const nri::Result res = implementation->Create(args...); - - if (res == nri::Result::SUCCESS) - { - entity = (Interface*)implementation; - return nri::Result::SUCCESS; - } - - Deallocate(GetStdAllocator(), implementation); - return res; -} - -Result CreateDeviceD3D11(const DeviceCreationDesc& deviceCreationDesc, DeviceBase*& device) -{ - Log log(GraphicsAPI::D3D11, deviceCreationDesc.callbackInterface); - StdAllocator allocator(deviceCreationDesc.memoryAllocatorInterface); - - ComPtr factory; - HRESULT hr = CreateDXGIFactory2(0, IID_PPV_ARGS(&factory)); - RETURN_ON_BAD_HRESULT(log, hr, "Can't create D3D11 device. CreateDXGIFactory2() failed. (result: %d)", (int32_t)hr); - - ComPtr adapter; - - if (deviceCreationDesc.physicalDeviceGroup != nullptr) - { - LUID luid = *(LUID*)&deviceCreationDesc.physicalDeviceGroup->luid; - hr = factory->EnumAdapterByLuid(luid, IID_PPV_ARGS(&adapter)); - RETURN_ON_BAD_HRESULT(log, hr, "Can't create D3D11 device. IDXGIFactory4::EnumAdapterByLuid() failed. (result: %d)", (int32_t)hr); - } - else - { - hr = factory->EnumAdapters(0, &adapter); - RETURN_ON_BAD_HRESULT(log, hr, "Can't create D3D11 device. IDXGIFactory4::EnumAdapters() failed. (result: %d)", (int32_t)hr); - } - - DeviceD3D11* implementation = Allocate(allocator, log, allocator); - const nri::Result result = implementation->Create(deviceCreationDesc, adapter, nullptr, nullptr); - - if (result == nri::Result::SUCCESS) - { - device = (DeviceBase*)implementation; - return nri::Result::SUCCESS; - } - - Deallocate(allocator, implementation); - return result; -} - #include "DeviceD3D11.hpp" diff --git a/Source/D3D11/DeviceD3D11.h b/Source/D3D11/DeviceD3D11.h index 94368e60..be215778 100644 --- a/Source/D3D11/DeviceD3D11.h +++ b/Source/D3D11/DeviceD3D11.h @@ -12,102 +12,109 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct CommandQueueD3D11; - struct DeviceD3D11 final : public DeviceBase +struct CommandQueueD3D11; + +struct DeviceD3D11 final : public DeviceBase +{ + DeviceD3D11(const Log& log, StdAllocator& stdAllocator); + ~DeviceD3D11(); + + inline const VersionedDevice& GetDevice() const + { return m_Device; } + + inline const VersionedContext& GetImmediateContext() const + { return m_ImmediateContext; } + + inline const CoreInterface& GetCoreInterface() const + { return m_CoreInterface; } + + bool GetOutput(Display* display, ComPtr& output) const; + Result Create(const DeviceCreationDesc& deviceCreationDesc, IDXGIAdapter* adapter, ID3D11Device* precreatedDevice, AGSContext* agsContext); + + //================================================================================================================ + // DeviceBase + //================================================================================================================ + + void Destroy(); + Result FillFunctionTable(CoreInterface& table) const; + Result FillFunctionTable(SwapChainInterface& table) const; + Result FillFunctionTable(WrapperD3D11Interface& table) const; + Result FillFunctionTable(HelperInterface& helperInterface) const; + + //================================================================================================================ + // NRI + //================================================================================================================ + + inline const DeviceDesc& GetDesc() const + { return m_Desc; } + + inline void SetDebugName(const char* name) { - DeviceD3D11(const Log& log, StdAllocator& stdAllocator); - ~DeviceD3D11(); - - inline const VersionedContext& GetImmediateContext() const - { return m_ImmediateContext; } - - inline const VersionedDevice& GetDevice() const - { return m_Device; } - - inline const CoreInterface& GetCoreInterface() const - { return m_CoreInterface; } - - bool GetOutput(Display* display, ComPtr& output) const; - Result Create(const DeviceCreationDesc& deviceCreationDesc, IDXGIAdapter* adapter, ID3D11Device* precreatedDevice, AGSContext* agsContext); - - //====================================================================================================================== - // NRI - //====================================================================================================================== - Result CreateSwapChain(const SwapChainDesc& swapChainDesc, SwapChain*& swapChain); - void DestroySwapChain(SwapChain& swapChain); - Result GetDisplays(Display** displays, uint32_t& displayNum); - Result GetDisplaySize(Display& display, uint16_t& width, uint16_t& height); - void SetDebugName(const char* name); - - inline const DeviceDesc& GetDesc() const - { return m_Desc; } - - Result GetCommandQueue(CommandQueueType commandQueueType, CommandQueue*& commandQueue); - Result CreateCommandAllocator(const CommandQueue& commandQueue, CommandAllocator*& commandAllocator); - Result CreateDescriptorPool(const DescriptorPoolDesc& descriptorPoolDesc, DescriptorPool*& descriptorPool); - Result CreateBuffer(const BufferDesc& bufferDesc, Buffer*& buffer); - Result CreateTexture(const TextureDesc& textureDesc, Texture*& texture); - Result CreateDescriptor(const BufferViewDesc& bufferViewDesc, Descriptor*& bufferView); - Result CreateDescriptor(const Texture1DViewDesc& textureViewDesc, Descriptor*& textureView); - Result CreateDescriptor(const Texture2DViewDesc& textureViewDesc, Descriptor*& textureView); - Result CreateDescriptor(const Texture3DViewDesc& textureViewDesc, Descriptor*& textureView); - Result CreateDescriptor(const SamplerDesc& samplerDesc, Descriptor*& sampler); - Result CreatePipelineLayout(const PipelineLayoutDesc& pipelineLayoutDesc, PipelineLayout*& pipelineLayout); - Result CreatePipeline(const GraphicsPipelineDesc& graphicsPipelineDesc, Pipeline*& pipeline); - Result CreatePipeline(const ComputePipelineDesc& computePipelineDesc, Pipeline*& pipeline); - Result CreateFrameBuffer(const FrameBufferDesc& frameBufferDesc, FrameBuffer*& frameBuffer); - Result CreateQueryPool(const QueryPoolDesc& queryPoolDesc, QueryPool*& queryPool); - Result CreateQueueSemaphore(QueueSemaphore*& queueSemaphore); - Result CreateDeviceSemaphore(bool signaled, DeviceSemaphore*& deviceSemaphore); - void DestroyCommandAllocator(CommandAllocator& commandAllocator); - void DestroyDescriptorPool(DescriptorPool& descriptorPool); - void DestroyBuffer(Buffer& buffer); - void DestroyTexture(Texture& texture); - void DestroyDescriptor(Descriptor& descriptor); - void DestroyPipelineLayout(PipelineLayout& pipelineLayout); - void DestroyPipeline(Pipeline& pipeline); - void DestroyFrameBuffer(FrameBuffer& frameBuffer); - void DestroyQueryPool(QueryPool& queryPool); - void DestroyQueueSemaphore(QueueSemaphore& queueSemaphore); - void DestroyDeviceSemaphore(DeviceSemaphore& deviceSemaphore); - Result AllocateMemory(MemoryType memoryType, uint64_t size, Memory*& memory); - Result BindBufferMemory(const BufferMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum); - Result BindTextureMemory(const TextureMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum); - void FreeMemory(Memory& memory); - FormatSupportBits GetFormatSupport(Format format) const; - uint32_t CalculateAllocationNumber(const ResourceGroupDesc& resourceGroupDesc) const; - Result AllocateAndBindMemory(const ResourceGroupDesc& resourceGroupDesc, Memory** allocations); - - //================================================================================================================ - // DeviceBase - //================================================================================================================ - void Destroy(); - Result FillFunctionTable(CoreInterface& table) const; - Result FillFunctionTable(SwapChainInterface& table) const; - Result FillFunctionTable(WrapperD3D11Interface& table) const; - Result FillFunctionTable(HelperInterface& helperInterface) const; - - private: - void InitVersionedDevice(ID3D11Device* device, bool isDeferredContextsEmulationRequested); - void InitVersionedContext(); - - template - nri::Result CreateImplementationWithNonEmptyConstructor(Interface*& entity, ConstructorArg&& constructorArg, const Args&... args); - - private: - // don't sort - ~D3D11Extensions must be called last! - D3D11Extensions m_Ext = {}; - VersionedDevice m_Device = {}; - VersionedContext m_ImmediateContext = {}; - Vector m_CommandQueues; - DeviceDesc m_Desc = {}; - CRITICAL_SECTION m_CriticalSection = {}; - CoreInterface m_CoreInterface = {}; - ComPtr m_Adapter; - bool m_SkipLiveObjectsReporting = false; - - private: - void FillLimits(bool isValidationEnabled, Vendor vendor); - }; + SET_D3D_DEBUG_OBJECT_NAME(m_Device.ptr, name); + SET_D3D_DEBUG_OBJECT_NAME(m_ImmediateContext.ptr, name); + } + + Result CreateSwapChain(const SwapChainDesc& swapChainDesc, SwapChain*& swapChain); + void DestroySwapChain(SwapChain& swapChain); + Result GetDisplays(Display** displays, uint32_t& displayNum); + Result GetDisplaySize(Display& display, uint16_t& width, uint16_t& height); + + Result GetCommandQueue(CommandQueueType commandQueueType, CommandQueue*& commandQueue); + Result CreateCommandAllocator(const CommandQueue& commandQueue, CommandAllocator*& commandAllocator); + Result CreateDescriptorPool(const DescriptorPoolDesc& descriptorPoolDesc, DescriptorPool*& descriptorPool); + Result CreateBuffer(const BufferDesc& bufferDesc, Buffer*& buffer); + Result CreateTexture(const TextureDesc& textureDesc, Texture*& texture); + Result CreateDescriptor(const BufferViewDesc& bufferViewDesc, Descriptor*& bufferView); + Result CreateDescriptor(const Texture1DViewDesc& textureViewDesc, Descriptor*& textureView); + Result CreateDescriptor(const Texture2DViewDesc& textureViewDesc, Descriptor*& textureView); + Result CreateDescriptor(const Texture3DViewDesc& textureViewDesc, Descriptor*& textureView); + Result CreateDescriptor(const SamplerDesc& samplerDesc, Descriptor*& sampler); + Result CreatePipelineLayout(const PipelineLayoutDesc& pipelineLayoutDesc, PipelineLayout*& pipelineLayout); + Result CreatePipeline(const GraphicsPipelineDesc& graphicsPipelineDesc, Pipeline*& pipeline); + Result CreatePipeline(const ComputePipelineDesc& computePipelineDesc, Pipeline*& pipeline); + Result CreateFrameBuffer(const FrameBufferDesc& frameBufferDesc, FrameBuffer*& frameBuffer); + Result CreateQueryPool(const QueryPoolDesc& queryPoolDesc, QueryPool*& queryPool); + Result CreateFence(uint64_t initialValue, Fence*& queueSemaphore); + void DestroyCommandAllocator(CommandAllocator& commandAllocator); + void DestroyDescriptorPool(DescriptorPool& descriptorPool); + void DestroyBuffer(Buffer& buffer); + void DestroyTexture(Texture& texture); + void DestroyDescriptor(Descriptor& descriptor); + void DestroyPipelineLayout(PipelineLayout& pipelineLayout); + void DestroyPipeline(Pipeline& pipeline); + void DestroyFrameBuffer(FrameBuffer& frameBuffer); + void DestroyQueryPool(QueryPool& queryPool); + void DestroyFence(Fence& fence); + Result AllocateMemory(MemoryType memoryType, uint64_t size, Memory*& memory); + Result BindBufferMemory(const BufferMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum); + Result BindTextureMemory(const TextureMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum); + void FreeMemory(Memory& memory); + FormatSupportBits GetFormatSupport(Format format) const; + uint32_t CalculateAllocationNumber(const ResourceGroupDesc& resourceGroupDesc) const; + Result AllocateAndBindMemory(const ResourceGroupDesc& resourceGroupDesc, Memory** allocations); + +private: + void InitVersionedDevice(bool isDeferredContextsEmulationRequested); + void InitVersionedContext(); + + template + nri::Result CreateImplementationWithNonEmptyConstructor(Interface*& entity, ConstructorArg&& constructorArg, const Args&... args); + +private: + // don't sort - ~D3D11Extensions must be called last! + D3D11Extensions m_Ext = {}; + VersionedDevice m_Device = {}; + VersionedContext m_ImmediateContext = {}; + Vector m_CommandQueues; + DeviceDesc m_Desc = {}; + CRITICAL_SECTION m_CriticalSection = {}; + CoreInterface m_CoreInterface = {}; + ComPtr m_Adapter; + bool m_SkipLiveObjectsReporting = false; + +private: + void FillLimits(bool isValidationEnabled, Vendor vendor); +}; + } diff --git a/Source/D3D11/DeviceD3D11.hpp b/Source/D3D11/DeviceD3D11.hpp index c83ae7d5..75afeb24 100644 --- a/Source/D3D11/DeviceD3D11.hpp +++ b/Source/D3D11/DeviceD3D11.hpp @@ -8,7 +8,9 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ CoreInterface ] +Declare_PartiallyFillFunctionTable_Functions(D3D11) + +#pragma region [ Core ] static const DeviceDesc& NRI_CALL GetDeviceDesc(const Device& device) { @@ -97,14 +99,9 @@ static Result NRI_CALL CreateQueryPool(Device& device, const QueryPoolDesc& quer return ((DeviceD3D11&)device).CreateQueryPool(queryPoolDesc, queryPool); } -static Result NRI_CALL CreateQueueSemaphore(Device& device, QueueSemaphore*& queueSemaphore) +static Result NRI_CALL CreateFence(Device& device, uint64_t initialValue, Fence*& queueSemaphore) { - return ((DeviceD3D11&)device).CreateQueueSemaphore(queueSemaphore); -} - -static Result NRI_CALL CreateDeviceSemaphore(Device& device, bool signaled, DeviceSemaphore*& deviceSemaphore) -{ - return ((DeviceD3D11&)device).CreateDeviceSemaphore(signaled, deviceSemaphore); + return ((DeviceD3D11&)device).CreateFence(initialValue, queueSemaphore); } static void NRI_CALL DestroyCommandAllocator(CommandAllocator& commandAllocator) @@ -161,16 +158,10 @@ static void NRI_CALL DestroyQueryPool(QueryPool& queryPool) device.DestroyQueryPool(queryPool); } -static void NRI_CALL DestroyQueueSemaphore(QueueSemaphore& queueSemaphore) -{ - DeviceD3D11& device = ((QueueSemaphoreD3D11&)queueSemaphore).GetDevice(); - device.DestroyQueueSemaphore(queueSemaphore); -} - -static void NRI_CALL DestroyDeviceSemaphore(DeviceSemaphore& deviceSemaphore) +static void NRI_CALL DestroyFence(Fence& fence) { - DeviceD3D11& device = ((DeviceSemaphoreD3D11&)deviceSemaphore).GetDevice(); - device.DestroyDeviceSemaphore(deviceSemaphore); + DeviceD3D11& device = ((FenceD3D11&)fence).GetDevice(); + device.DestroyFence(fence); } static Result NRI_CALL AllocateMemory(Device& device, uint32_t physicalDeviceMask, MemoryType memoryType, uint64_t size, Memory*& memory) @@ -206,56 +197,39 @@ static void NRI_CALL SetDeviceDebugName(Device& device, const char* name) ((DeviceD3D11&)device).SetDebugName(name); } +static void NRI_CALL SetPipelineDebugName(Pipeline& pipeline, const char* name) +{ + ((PipelineD3D11&)pipeline).SetDebugName(name); +} + +static void NRI_CALL SetPipelineLayoutDebugName(PipelineLayout& pipelineLayout, const char* name) +{ + ((PipelineLayoutD3D11&)pipelineLayout).SetDebugName(name); +} + +static void NRI_CALL SetFrameBufferDebugName(FrameBuffer& frameBuffer, const char* name) +{ + ((FrameBufferD3D11&)frameBuffer).SetDebugName(name); +} + +static void NRI_CALL SetMemoryDebugName(Memory& memory, const char* name) +{ + ((MemoryD3D11&)memory).SetDebugName(name); +} + static void* NRI_CALL GetDeviceNativeObject(const Device& device) { return ((DeviceD3D11&)device).GetDevice(); } -void FillFunctionTableBufferD3D11(CoreInterface& coreInterface); -void FillFunctionTableCommandAllocatorD3D11(CoreInterface& coreInterface); -void FillFunctionTableCommandBufferD3D11(CoreInterface& coreInterface); -void FillFunctionTableCommandBufferEmuD3D11(CoreInterface& coreInterface); -void FillFunctionTableCommandQueueD3D11(CoreInterface& coreInterface); -void FillFunctionTableDescriptorD3D11(CoreInterface& coreInterface); -void FillFunctionTableDescriptorPoolD3D11(CoreInterface& coreInterface); -void FillFunctionTableDescriptorSetD3D11(CoreInterface& coreInterface); -void FillFunctionTableDeviceSemaphoreD3D11(CoreInterface& coreInterface); -void FillFunctionTableFrameBufferD3D11(CoreInterface& coreInterface); -void FillFunctionTableMemoryD3D11(CoreInterface& coreInterface); -void FillFunctionTablePipelineLayoutD3D11(CoreInterface& coreInterface); -void FillFunctionTablePipelineD3D11(CoreInterface& coreInterface); -void FillFunctionTableQueryPoolD3D11(CoreInterface& coreInterface); -void FillFunctionTableQueueSemaphoreD3D11(CoreInterface& coreInterface); -void FillFunctionTableTextureD3D11(CoreInterface& coreInterface); +void Core_CommandBufferEmu_PartiallyFillFunctionTable(CoreInterface& coreInterface); Result DeviceD3D11::FillFunctionTable(CoreInterface& coreInterface) const { coreInterface = {}; - - FillFunctionTableBufferD3D11(coreInterface); - FillFunctionTableCommandAllocatorD3D11(coreInterface); - FillFunctionTableCommandQueueD3D11(coreInterface); - FillFunctionTableDescriptorD3D11(coreInterface); - FillFunctionTableDescriptorPoolD3D11(coreInterface); - FillFunctionTableDescriptorSetD3D11(coreInterface); - FillFunctionTableDeviceSemaphoreD3D11(coreInterface); - FillFunctionTableFrameBufferD3D11(coreInterface); - FillFunctionTableMemoryD3D11(coreInterface); - FillFunctionTablePipelineLayoutD3D11(coreInterface); - FillFunctionTablePipelineD3D11(coreInterface); - FillFunctionTableQueryPoolD3D11(coreInterface); - FillFunctionTableQueueSemaphoreD3D11(coreInterface); - FillFunctionTableTextureD3D11(coreInterface); - - if (m_Device.isDeferredContextsEmulated) - FillFunctionTableCommandBufferEmuD3D11(coreInterface); - else - FillFunctionTableCommandBufferD3D11(coreInterface); - coreInterface.GetDeviceDesc = ::GetDeviceDesc; coreInterface.GetFormatSupport = ::GetFormatSupport; coreInterface.GetCommandQueue = ::GetCommandQueue; - coreInterface.CreateCommandAllocator = ::CreateCommandAllocator; coreInterface.CreateDescriptorPool = ::CreateDescriptorPool; coreInterface.CreateBuffer = ::CreateBuffer; @@ -270,9 +244,7 @@ Result DeviceD3D11::FillFunctionTable(CoreInterface& coreInterface) const coreInterface.CreateComputePipeline = ::CreateComputePipeline; coreInterface.CreateFrameBuffer = ::CreateFrameBuffer; coreInterface.CreateQueryPool = ::CreateQueryPool; - coreInterface.CreateQueueSemaphore = ::CreateQueueSemaphore; - coreInterface.CreateDeviceSemaphore = ::CreateDeviceSemaphore; - + coreInterface.CreateFence = ::CreateFence; coreInterface.DestroyCommandAllocator = ::DestroyCommandAllocator; coreInterface.DestroyDescriptorPool = ::DestroyDescriptorPool; coreInterface.DestroyBuffer = ::DestroyBuffer; @@ -282,24 +254,39 @@ Result DeviceD3D11::FillFunctionTable(CoreInterface& coreInterface) const coreInterface.DestroyPipeline = ::DestroyPipeline; coreInterface.DestroyFrameBuffer = ::DestroyFrameBuffer; coreInterface.DestroyQueryPool = ::DestroyQueryPool; - coreInterface.DestroyQueueSemaphore = ::DestroyQueueSemaphore; - coreInterface.DestroyDeviceSemaphore = ::DestroyDeviceSemaphore; - + coreInterface.DestroyFence = ::DestroyFence; coreInterface.AllocateMemory = ::AllocateMemory; coreInterface.BindBufferMemory = ::BindBufferMemory; coreInterface.BindTextureMemory = ::BindTextureMemory; coreInterface.FreeMemory = ::FreeMemory; - coreInterface.SetDeviceDebugName = ::SetDeviceDebugName; - + coreInterface.SetPipelineDebugName = ::SetPipelineDebugName; + coreInterface.SetPipelineLayoutDebugName = ::SetPipelineLayoutDebugName; + coreInterface.SetFrameBufferDebugName = ::SetFrameBufferDebugName; + coreInterface.SetMemoryDebugName = ::SetMemoryDebugName; coreInterface.GetDeviceNativeObject = ::GetDeviceNativeObject; + Core_Buffer_PartiallyFillFunctionTableD3D11(coreInterface); + Core_CommandAllocator_PartiallyFillFunctionTableD3D11(coreInterface); + Core_CommandQueue_PartiallyFillFunctionTableD3D11(coreInterface); + Core_Descriptor_PartiallyFillFunctionTableD3D11(coreInterface); + Core_DescriptorPool_PartiallyFillFunctionTableD3D11(coreInterface); + Core_DescriptorSet_PartiallyFillFunctionTableD3D11(coreInterface); + Core_Fence_PartiallyFillFunctionTableD3D11(coreInterface); + Core_QueryPool_PartiallyFillFunctionTableD3D11(coreInterface); + Core_Texture_PartiallyFillFunctionTableD3D11(coreInterface); + + if (m_Device.isDeferredContextEmulated) + Core_CommandBufferEmu_PartiallyFillFunctionTable(coreInterface); + else + Core_CommandBuffer_PartiallyFillFunctionTableD3D11(coreInterface); + return ValidateFunctionTable(GetLog(), coreInterface); } #pragma endregion -#pragma region [ SwapChainInterface ] +#pragma region [ SwapChain ] static Result NRI_CALL CreateSwapChain(Device& device, const SwapChainDesc& swapChainDesc, SwapChain*& swapChain) { @@ -322,25 +309,22 @@ static Result NRI_CALL GetDisplaySize(Device& device, Display& display, uint16_t return ((DeviceD3D11&)device).GetDisplaySize(display, width, height); } -void FillFunctionTableSwapChainD3D11(SwapChainInterface& swapChainInterface); - Result DeviceD3D11::FillFunctionTable(SwapChainInterface& swapChainInterface) const { swapChainInterface = {}; - - FillFunctionTableSwapChainD3D11(swapChainInterface); - swapChainInterface.CreateSwapChain = ::CreateSwapChain; swapChainInterface.DestroySwapChain = ::DestroySwapChain; swapChainInterface.GetDisplays = ::GetDisplays; swapChainInterface.GetDisplaySize = ::GetDisplaySize; + SwapChain_PartiallyFillFunctionTableD3D11(swapChainInterface); + return ValidateFunctionTable(GetLog(), swapChainInterface); } #pragma endregion -#pragma region [ WrapperD3D11Interface ] +#pragma region [ WrapperD3D11 ] Result CreateDeviceD3D11(const DeviceCreationD3D11Desc& deviceCreationD3D11Desc, DeviceBase*& device) { @@ -352,13 +336,13 @@ Result CreateDeviceD3D11(const DeviceCreationD3D11Desc& deviceCreationD3D11Desc, Log log(GraphicsAPI::D3D11, deviceCreationDesc.callbackInterface); StdAllocator allocator(deviceCreationDesc.memoryAllocatorInterface); - ID3D11Device* d3d11Device = (ID3D11Device*)deviceCreationD3D11Desc.d3d11Device; + ComPtr d3d11Device = (ID3D11Device*)deviceCreationD3D11Desc.d3d11Device; - ComPtr adapter; ComPtr dxgiDevice; HRESULT hr = d3d11Device->QueryInterface(IID_PPV_ARGS(&dxgiDevice)); RETURN_ON_BAD_HRESULT(log, hr, "Can't create device. Failed to query IDXGIDevice from ID3D11Device. (result: %d)", (int32_t)hr); + ComPtr adapter; hr = dxgiDevice->GetAdapter(&adapter); RETURN_ON_BAD_HRESULT(log, hr, "Can't create device. IDXGIDevice::GetAdapter() failed. (result: %d)", (int32_t)hr); @@ -372,21 +356,22 @@ Result CreateDeviceD3D11(const DeviceCreationD3D11Desc& deviceCreationD3D11Desc, } Deallocate(allocator, implementation); + return result; } -static Result NRI_CALL CreateCommandBufferD3D11(Device& device, const CommandBufferD3D11Desc& commandBufferDesc, CommandBuffer*& commandBuffer) +static Result NRI_CALL CreateCommandBuffer(Device& device, const CommandBufferD3D11Desc& commandBufferDesc, CommandBuffer*& commandBuffer) { DeviceD3D11& deviceD3D11 = (DeviceD3D11&)device; return ::CreateCommandBuffer(deviceD3D11, (ID3D11DeviceContext*)commandBufferDesc.d3d11DeviceContext, commandBuffer); } -static Result NRI_CALL CreateBufferD3D11(Device& device, const BufferD3D11Desc& bufferDesc, Buffer*& buffer) +static Result NRI_CALL CreateBuffer(Device& device, const BufferD3D11Desc& bufferDesc, Buffer*& buffer) { DeviceD3D11& deviceD3D11 = (DeviceD3D11&)device; - BufferD3D11* implementation = Allocate(deviceD3D11.GetStdAllocator(), deviceD3D11, deviceD3D11.GetImmediateContext()); + BufferD3D11* implementation = Allocate(deviceD3D11.GetStdAllocator(), deviceD3D11); const nri::Result res = implementation->Create(bufferDesc); if (res == nri::Result::SUCCESS) @@ -396,15 +381,16 @@ static Result NRI_CALL CreateBufferD3D11(Device& device, const BufferD3D11Desc& } Deallocate(deviceD3D11.GetStdAllocator(), implementation); + return res; } -static Result NRI_CALL CreateTextureD3D11(Device& device, const TextureD3D11Desc& textureDesc, Texture*& texture) +static Result NRI_CALL CreateTexture(Device& device, const TextureD3D11Desc& textureDesc, Texture*& texture) { DeviceD3D11& deviceD3D11 = (DeviceD3D11&)device; - TextureD3D11* implementation = Allocate(deviceD3D11.GetStdAllocator()); - const nri::Result res = implementation->Create(deviceD3D11, textureDesc); + TextureD3D11* implementation = Allocate(deviceD3D11.GetStdAllocator(), deviceD3D11); + const nri::Result res = implementation->Create(textureDesc); if (res == nri::Result::SUCCESS) { @@ -413,42 +399,41 @@ static Result NRI_CALL CreateTextureD3D11(Device& device, const TextureD3D11Desc } Deallocate(deviceD3D11.GetStdAllocator(), implementation); + return res; } Result DeviceD3D11::FillFunctionTable(WrapperD3D11Interface& wrapperD3D11Interface) const { wrapperD3D11Interface = {}; - wrapperD3D11Interface.CreateCommandBufferD3D11 = ::CreateCommandBufferD3D11; - wrapperD3D11Interface.CreateTextureD3D11 = ::CreateTextureD3D11; - wrapperD3D11Interface.CreateBufferD3D11 = ::CreateBufferD3D11; + wrapperD3D11Interface.CreateCommandBufferD3D11 = ::CreateCommandBuffer; + wrapperD3D11Interface.CreateTextureD3D11 = ::CreateTexture; + wrapperD3D11Interface.CreateBufferD3D11 = ::CreateBuffer; return ValidateFunctionTable(GetLog(), wrapperD3D11Interface); } #pragma endregion -#pragma region [ HelperInterface ] +#pragma region [ Helper ] -static uint32_t NRI_CALL CountAllocationNumD3D11(Device& device, const ResourceGroupDesc& resourceGroupDesc) +static uint32_t NRI_CALL CountAllocationNum(Device& device, const ResourceGroupDesc& resourceGroupDesc) { return ((DeviceD3D11&)device).CalculateAllocationNumber(resourceGroupDesc); } -static Result NRI_CALL AllocateAndBindMemoryD3D11(Device& device, const ResourceGroupDesc& resourceGroupDesc, Memory** allocations) +static Result NRI_CALL AllocateAndBindMemory(Device& device, const ResourceGroupDesc& resourceGroupDesc, Memory** allocations) { return ((DeviceD3D11&)device).AllocateAndBindMemory(resourceGroupDesc, allocations); } -void FillFunctionTableCommandQueueD3D11(HelperInterface& helperInterface); - Result DeviceD3D11::FillFunctionTable(HelperInterface& helperInterface) const { helperInterface = {}; + helperInterface.CalculateAllocationNumber = ::CountAllocationNum; + helperInterface.AllocateAndBindMemory = ::AllocateAndBindMemory; - helperInterface.CalculateAllocationNumber = ::CountAllocationNumD3D11; - helperInterface.AllocateAndBindMemory = ::AllocateAndBindMemoryD3D11; - FillFunctionTableCommandQueueD3D11(helperInterface); + Helper_CommandQueue_PartiallyFillFunctionTableD3D11(helperInterface); return ValidateFunctionTable(GetLog(), helperInterface); } diff --git a/Source/D3D11/DeviceSemaphoreD3D11.cpp b/Source/D3D11/DeviceSemaphoreD3D11.cpp deleted file mode 100644 index cede3fbf..00000000 --- a/Source/D3D11/DeviceSemaphoreD3D11.cpp +++ /dev/null @@ -1,114 +0,0 @@ -/* -Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. - -NVIDIA CORPORATION and its licensors retain all intellectual property -and proprietary rights in and to this software, related documentation -and any modifications thereto. Any use, reproduction, disclosure or -distribution of this software and related documentation without an express -license agreement from NVIDIA CORPORATION is strictly prohibited. -*/ - -#include "SharedExternal.h" -#include "SharedD3D11.h" -#include "DeviceSemaphoreD3D11.h" - -using namespace nri; - -DeviceSemaphoreD3D11::DeviceSemaphoreD3D11(DeviceD3D11& device, const VersionedDevice& versionedDevice) : - m_VersionedDevice(versionedDevice), - m_Device(device) -{ -} - -DeviceSemaphoreD3D11::~DeviceSemaphoreD3D11() -{ - if (m_Handle) - CloseHandle(m_Handle); -} - -Result DeviceSemaphoreD3D11::Create(bool signaled) -{ - HRESULT hr = E_INVALIDARG; - const char* message = ""; - - m_IgnoreWait = signaled; - - if (m_VersionedDevice.version >= 5) - { - m_Handle = CreateEvent(nullptr, FALSE, 0, nullptr); - RETURN_ON_FAILURE(m_Device.GetLog(), m_Handle, Result::FAILURE, "CreateEvent() - FAILED!"); - - hr = m_VersionedDevice->CreateFence(0, D3D11_FENCE_FLAG_NONE, IID_PPV_ARGS(&m_Fence)); - message = "ID3D11Device5::CreateFence() - FAILED!"; - } - else if (m_VersionedDevice.version >= 3) - { - D3D11_QUERY_DESC1 queryDesc = {}; - queryDesc.Query = D3D11_QUERY_EVENT; - queryDesc.ContextType = D3D11_CONTEXT_TYPE_ALL; - - hr = m_VersionedDevice->CreateQuery1(&queryDesc, &m_Query); - message = "ID3D11Device3::CreateQuery1() - FAILED!"; - } - else - { - D3D11_QUERY_DESC queryDesc = {}; - queryDesc.Query = D3D11_QUERY_EVENT; - - hr = m_VersionedDevice->CreateQuery(&queryDesc, (ID3D11Query**)&m_Query); - message = "ID3D11Device::CreateQuery() - FAILED!"; - } - - RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, message); - - return Result::SUCCESS; -} - -void DeviceSemaphoreD3D11::Signal(const VersionedContext& immediateContext) -{ - m_IgnoreWait = false; - m_Value++; - - if (m_Fence) - immediateContext->Signal(m_Fence, m_Value); - else - immediateContext->End(m_Query); -} - -void DeviceSemaphoreD3D11::Wait(const VersionedContext& immediateContext) -{ - if (m_IgnoreWait) - return; - - HRESULT hr; - - if (m_Fence) - { - hr = m_Fence->SetEventOnCompletion(m_Value, m_Handle); - CHECK(m_Device.GetLog(), hr == S_OK, "D3D11Fence::SetEventOnCompletion() - FAILED!"); - - WaitForSingleObject(m_Handle, INFINITE); - } - else - { - while (true) - { - hr = immediateContext->GetData(m_Query, nullptr, 0, 0); - if (hr != S_FALSE) - break; - Sleep(0); - } - - CHECK(m_Device.GetLog(), hr == S_OK, "D3D11DeviceContext::GetData() - FAILED!"); - } - - m_Value++; -} - -void DeviceSemaphoreD3D11::SetDebugName(const char* name) -{ - SetName(m_Fence, name); - SetName(m_Query, name); -} - -#include "DeviceSemaphoreD3D11.hpp" diff --git a/Source/D3D11/DeviceSemaphoreD3D11.h b/Source/D3D11/DeviceSemaphoreD3D11.h deleted file mode 100644 index 84cea260..00000000 --- a/Source/D3D11/DeviceSemaphoreD3D11.h +++ /dev/null @@ -1,43 +0,0 @@ -/* -Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. - -NVIDIA CORPORATION and its licensors retain all intellectual property -and proprietary rights in and to this software, related documentation -and any modifications thereto. Any use, reproduction, disclosure or -distribution of this software and related documentation without an express -license agreement from NVIDIA CORPORATION is strictly prohibited. -*/ - -#pragma once - -namespace nri -{ - struct DeviceD3D11; - - struct DeviceSemaphoreD3D11 - { - DeviceSemaphoreD3D11(DeviceD3D11& device, const VersionedDevice& versionedDevice); - ~DeviceSemaphoreD3D11(); - - inline DeviceD3D11& GetDevice() const - { return m_Device; } - - Result Create(bool signaled); - void Signal(const VersionedContext& immediateContext); - void Wait(const VersionedContext& immediateContext); - - //====================================================================================================================== - // NRI - //====================================================================================================================== - void SetDebugName(const char* name); - - private: - ComPtr m_Query; - ComPtr m_Fence; - void* m_Handle = nullptr; - const VersionedDevice& m_VersionedDevice; - uint64_t m_Value = 0; - bool m_IgnoreWait = false; - DeviceD3D11& m_Device; - }; -} diff --git a/Source/D3D11/DeviceSemaphoreD3D11.hpp b/Source/D3D11/DeviceSemaphoreD3D11.hpp deleted file mode 100644 index d3ed8760..00000000 --- a/Source/D3D11/DeviceSemaphoreD3D11.hpp +++ /dev/null @@ -1,23 +0,0 @@ -/* -Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. - -NVIDIA CORPORATION and its licensors retain all intellectual property -and proprietary rights in and to this software, related documentation -and any modifications thereto. Any use, reproduction, disclosure or -distribution of this software and related documentation without an express -license agreement from NVIDIA CORPORATION is strictly prohibited. -*/ - -#pragma region [ CoreInterface ] - -static void NRI_CALL SetDeviceSemaphoreDebugName(DeviceSemaphore& deviceSemaphore, const char* name) -{ - ((DeviceSemaphoreD3D11&)deviceSemaphore).SetDebugName(name); -} - -void FillFunctionTableDeviceSemaphoreD3D11(CoreInterface& coreInterface) -{ - coreInterface.SetDeviceSemaphoreDebugName = ::SetDeviceSemaphoreDebugName; -} - -#pragma endregion diff --git a/Source/D3D11/FenceD3D11.cpp b/Source/D3D11/FenceD3D11.cpp new file mode 100644 index 00000000..739a1517 --- /dev/null +++ b/Source/D3D11/FenceD3D11.cpp @@ -0,0 +1,99 @@ +/* +Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. + +NVIDIA CORPORATION and its licensors retain all intellectual property +and proprietary rights in and to this software, related documentation +and any modifications thereto. Any use, reproduction, disclosure or +distribution of this software and related documentation without an express +license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "SharedD3D11.h" +#include "FenceD3D11.h" + +using namespace nri; + +Result FenceD3D11::Create(uint64_t initialValue) +{ + HRESULT hr = E_INVALIDARG; + const char* message = ""; + + if (m_Device.GetDevice().version >= 5) + { + hr = m_Device.GetDevice()->CreateFence(initialValue, D3D11_FENCE_FLAG_NONE, IID_PPV_ARGS(&m_Fence)); + message = "ID3D11Device5::CreateFence() - FAILED!"; + } + else + { + D3D11_QUERY_DESC queryDesc = {}; + queryDesc.Query = D3D11_QUERY_EVENT; + + hr = m_Device.GetDevice()->CreateQuery(&queryDesc, &m_Query); + message = "ID3D11Device::CreateQuery() - FAILED!"; + } + + RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, message); + + m_Value = initialValue; + + return Result::SUCCESS; +} + +//================================================================================================================ +// NRI +//================================================================================================================ + +inline uint64_t FenceD3D11::GetFenceValue() const +{ + if (m_Fence) + return m_Fence->GetCompletedValue(); + + return m_Value; +} + +inline void FenceD3D11::QueueSignal(CommandQueueD3D11& commandQueue, uint64_t value) +{ + MaybeUnused(commandQueue); + + if (m_Fence) + { + HRESULT hr = m_Device.GetImmediateContext()->Signal(m_Fence, value); + CHECK(m_Device.GetLog(), hr == S_OK, "D3D11DeviceContext4::Signal() - FAILED!"); + } + else + { + m_Device.GetImmediateContext()->End(m_Query); + m_Value = value; + } +} + +inline void FenceD3D11::QueueWait(CommandQueueD3D11& commandQueue, uint64_t value) +{ + MaybeUnused(commandQueue); + + if (m_Fence) + { + HRESULT hr = m_Device.GetImmediateContext()->Wait(m_Fence, value); + CHECK(m_Device.GetLog(), hr == S_OK, "D3D11DeviceContext4::Wait() - FAILED!"); + } +} + +inline void FenceD3D11::Wait(uint64_t value) +{ + if (m_Fence) + { + // TODO: "busy wait" vs "wait for event"? + while (m_Fence->GetCompletedValue() < value) + ; + } + else + { + HRESULT hr = S_FALSE; + while (hr == S_FALSE) + hr = m_Device.GetImmediateContext()->GetData(m_Query, nullptr, 0, 0); + + CHECK(m_Device.GetLog(), hr == S_OK, "D3D11DeviceContext::GetData() - FAILED!"); + } +} + +#include "FenceD3D11.hpp" diff --git a/Source/D3D11/FenceD3D11.h b/Source/D3D11/FenceD3D11.h new file mode 100644 index 00000000..aa1dfd26 --- /dev/null +++ b/Source/D3D11/FenceD3D11.h @@ -0,0 +1,55 @@ +/* +Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. + +NVIDIA CORPORATION and its licensors retain all intellectual property +and proprietary rights in and to this software, related documentation +and any modifications thereto. Any use, reproduction, disclosure or +distribution of this software and related documentation without an express +license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#pragma once + +namespace nri +{ + +struct DeviceD3D11; +struct CommandQueueD3D11; + +struct FenceD3D11 +{ + inline FenceD3D11(DeviceD3D11& device) : + m_Device(device) + {} + + inline ~FenceD3D11() + {} + + inline DeviceD3D11& GetDevice() const + { return m_Device; } + + Result Create(uint64_t initialValue); + + //================================================================================================================ + // NRI + //================================================================================================================ + + inline void FenceD3D11::SetDebugName(const char* name) + { + SET_D3D_DEBUG_OBJECT_NAME(m_Fence, name); + SET_D3D_DEBUG_OBJECT_NAME(m_Query, name); + } + + uint64_t GetFenceValue() const; + void QueueSignal(CommandQueueD3D11& commandQueue, uint64_t value); + void QueueWait(CommandQueueD3D11& commandQueue, uint64_t value); + void Wait(uint64_t value); + +private: + DeviceD3D11& m_Device; + ComPtr m_Query; + ComPtr m_Fence; + uint64_t m_Value = 0; +}; + +} diff --git a/Source/D3D11/FenceD3D11.hpp b/Source/D3D11/FenceD3D11.hpp new file mode 100644 index 00000000..379a82d7 --- /dev/null +++ b/Source/D3D11/FenceD3D11.hpp @@ -0,0 +1,40 @@ +/* +Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. + +NVIDIA CORPORATION and its licensors retain all intellectual property +and proprietary rights in and to this software, related documentation +and any modifications thereto. Any use, reproduction, disclosure or +distribution of this software and related documentation without an express +license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#pragma region [ Core ] + +static uint64_t NRI_CALL GetFenceValue(Fence& fence) +{ + return ((FenceD3D11&)fence).GetFenceValue(); +} + +static void NRI_CALL QueueSignal(CommandQueue& commandQueue, Fence& fence, uint64_t value) +{ + return ((FenceD3D11&)fence).QueueSignal((CommandQueueD3D11&)commandQueue, value); +} + +static void NRI_CALL QueueWait(CommandQueue& commandQueue, Fence& fence, uint64_t value) +{ + return ((FenceD3D11&)fence).QueueWait((CommandQueueD3D11&)commandQueue, value); +} + +static void NRI_CALL Wait(Fence& fence, uint64_t value) +{ + ((FenceD3D11&)fence).Wait(value); +} + +static void NRI_CALL SetFenceDebugName(Fence& fence, const char* name) +{ + ((FenceD3D11&)fence).SetDebugName(name); +} + +#pragma endregion + +Define_Core_Fence_PartiallyFillFunctionTable(D3D11) diff --git a/Source/D3D11/FrameBufferD3D11.cpp b/Source/D3D11/FrameBufferD3D11.cpp index 9f00204e..21533a71 100644 --- a/Source/D3D11/FrameBufferD3D11.cpp +++ b/Source/D3D11/FrameBufferD3D11.cpp @@ -8,18 +8,16 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#include "SharedExternal.h" #include "SharedD3D11.h" #include "FrameBufferD3D11.h" - #include "DescriptorD3D11.h" using namespace nri; FrameBufferD3D11::FrameBufferD3D11(DeviceD3D11& device, const FrameBufferDesc& desc) : - m_ClearDescs(device.GetStdAllocator()), - m_RenderTargets(device.GetStdAllocator()), m_Device(device) + , m_ClearDescs(device.GetStdAllocator()) + , m_RenderTargets(device.GetStdAllocator()) { uint32_t clearDescNum = 0; if (desc.colorAttachments && desc.colorClearValues) @@ -52,10 +50,6 @@ FrameBufferD3D11::FrameBufferD3D11(DeviceD3D11& device, const FrameBufferDesc& d } } -FrameBufferD3D11::~FrameBufferD3D11() -{ -} - void FrameBufferD3D11::Bind(VersionedContext& context, RenderPassBeginFlag renderPassBeginFlag) const { if (renderPassBeginFlag != RenderPassBeginFlag::SKIP_FRAME_BUFFER_CLEAR && !m_ClearDescs.empty()) @@ -123,10 +117,3 @@ void FrameBufferD3D11::ClearAttachments(VersionedContext& context, const ClearDe CHECK(m_Device.GetLog(), false, "add 'ClearView' emulation!"); } } - -void FrameBufferD3D11::SetDebugName(const char* name) -{ - MaybeUnused(name); -} - -#include "FrameBufferD3D11.hpp" diff --git a/Source/D3D11/FrameBufferD3D11.h b/Source/D3D11/FrameBufferD3D11.h index 312e55c7..6bcaff0e 100644 --- a/Source/D3D11/FrameBufferD3D11.h +++ b/Source/D3D11/FrameBufferD3D11.h @@ -12,28 +12,34 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct DeviceD3D11; - - struct FrameBufferD3D11 - { - FrameBufferD3D11(DeviceD3D11& device, const FrameBufferDesc& desc); - ~FrameBufferD3D11(); - - inline DeviceD3D11& GetDevice() const - { return m_Device; } - - void Bind(VersionedContext& context, RenderPassBeginFlag renderPassBeginFlag) const; - void ClearAttachments(VersionedContext& context, const ClearDesc* clearDescs, uint32_t clearDescNum, const Rect* rects, uint32_t rectNum) const; - - //====================================================================================================================== - // NRI - //====================================================================================================================== - void SetDebugName(const char* name); - - private: - Vector m_ClearDescs; - Vector m_RenderTargets; - ID3D11DepthStencilView* m_DepthStencil = nullptr; - DeviceD3D11& m_Device; - }; + +struct DeviceD3D11; + +struct FrameBufferD3D11 +{ + inline ~FrameBufferD3D11() + {} + + inline DeviceD3D11& GetDevice() const + { return m_Device; } + + FrameBufferD3D11(DeviceD3D11& device, const FrameBufferDesc& desc); + + void Bind(VersionedContext& context, RenderPassBeginFlag renderPassBeginFlag) const; + void ClearAttachments(VersionedContext& context, const ClearDesc* clearDescs, uint32_t clearDescNum, const Rect* rects, uint32_t rectNum) const; + + //================================================================================================================ + // NRI + //================================================================================================================ + + inline void SetDebugName(const char* name) + { MaybeUnused(name); } + +private: + DeviceD3D11& m_Device; + Vector m_ClearDescs; + Vector m_RenderTargets; + ID3D11DepthStencilView* m_DepthStencil = nullptr; +}; + } diff --git a/Source/D3D11/FrameBufferD3D11.hpp b/Source/D3D11/FrameBufferD3D11.hpp deleted file mode 100644 index 6a239d49..00000000 --- a/Source/D3D11/FrameBufferD3D11.hpp +++ /dev/null @@ -1,23 +0,0 @@ -/* -Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. - -NVIDIA CORPORATION and its licensors retain all intellectual property -and proprietary rights in and to this software, related documentation -and any modifications thereto. Any use, reproduction, disclosure or -distribution of this software and related documentation without an express -license agreement from NVIDIA CORPORATION is strictly prohibited. -*/ - -#pragma region [ CoreInterface ] - -static void NRI_CALL SetFrameBufferDebugName(FrameBuffer& frameBuffer, const char* name) -{ - ((FrameBufferD3D11&)frameBuffer).SetDebugName(name); -} - -void FillFunctionTableFrameBufferD3D11(CoreInterface& coreInterface) -{ - coreInterface.SetFrameBufferDebugName = ::SetFrameBufferDebugName; -} - -#pragma endregion diff --git a/Source/D3D11/MemoryD3D11.cpp b/Source/D3D11/MemoryD3D11.cpp index 5a9367b8..b56d03eb 100644 --- a/Source/D3D11/MemoryD3D11.cpp +++ b/Source/D3D11/MemoryD3D11.cpp @@ -8,18 +8,11 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#include "SharedExternal.h" #include "SharedD3D11.h" #include "MemoryD3D11.h" using namespace nri; -MemoryD3D11::MemoryD3D11(DeviceD3D11& device, MemoryType memoryType) : - m_Location((MemoryLocation)memoryType), - m_Device(device) -{ -} - uint32_t MemoryD3D11::GetResidencyPriority(uint64_t size) const { uint32_t high = 0; @@ -43,10 +36,3 @@ uint32_t MemoryD3D11::GetResidencyPriority(uint64_t size) const return high; } - -void MemoryD3D11::SetDebugName(const char* name) -{ - MaybeUnused(name); -} - -#include "MemoryD3D11.hpp" diff --git a/Source/D3D11/MemoryD3D11.h b/Source/D3D11/MemoryD3D11.h index 5b78c45a..65ee6094 100644 --- a/Source/D3D11/MemoryD3D11.h +++ b/Source/D3D11/MemoryD3D11.h @@ -12,38 +12,48 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct DeviceD3D11; - - enum class MemoryResidencyPriority - { - DEFAULT, - MINIMUM, - LOW, - NORMAL, - HIGH, - MAXIMUM - }; - - struct MemoryD3D11 - { - MemoryD3D11(DeviceD3D11& device, MemoryType memoryType); - - inline DeviceD3D11& GetDevice() const - { return m_Device; } - - inline MemoryLocation GetType() const - { return m_Location; } - - uint32_t GetResidencyPriority(uint64_t size) const; - - //====================================================================================================================== - // NRI - //====================================================================================================================== - void SetDebugName(const char* name); - - private: - MemoryLocation m_Location; - MemoryResidencyPriority m_ResidencyPriority = MemoryResidencyPriority::DEFAULT; - DeviceD3D11& m_Device; - }; + +struct DeviceD3D11; + +enum class MemoryResidencyPriority +{ + DEFAULT, + MINIMUM, + LOW, + NORMAL, + HIGH, + MAXIMUM +}; + +struct MemoryD3D11 +{ + inline MemoryD3D11(DeviceD3D11& device, MemoryType memoryType) : + m_Device(device) + , m_Location((MemoryLocation)memoryType) + {} + + inline ~MemoryD3D11() + {} + + inline DeviceD3D11& GetDevice() const + { return m_Device; } + + inline MemoryLocation GetType() const + { return m_Location; } + + uint32_t GetResidencyPriority(uint64_t size) const; + + //================================================================================================================ + // NRI + //================================================================================================================ + + inline void SetDebugName(const char* name) + { MaybeUnused(name); } + +private: + DeviceD3D11& m_Device; + MemoryLocation m_Location; + MemoryResidencyPriority m_ResidencyPriority = MemoryResidencyPriority::DEFAULT; +}; + } diff --git a/Source/D3D11/MemoryD3D11.hpp b/Source/D3D11/MemoryD3D11.hpp deleted file mode 100644 index 02a29333..00000000 --- a/Source/D3D11/MemoryD3D11.hpp +++ /dev/null @@ -1,23 +0,0 @@ -/* -Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. - -NVIDIA CORPORATION and its licensors retain all intellectual property -and proprietary rights in and to this software, related documentation -and any modifications thereto. Any use, reproduction, disclosure or -distribution of this software and related documentation without an express -license agreement from NVIDIA CORPORATION is strictly prohibited. -*/ - -#pragma region [ CoreInterface ] - -static void NRI_CALL SetMemoryDebugName(Memory& memory, const char* name) -{ - ((MemoryD3D11&)memory).SetDebugName(name); -} - -void FillFunctionTableMemoryD3D11(CoreInterface& coreInterface) -{ - coreInterface.SetMemoryDebugName = ::SetMemoryDebugName; -} - -#pragma endregion diff --git a/Source/D3D11/PipelineD3D11.cpp b/Source/D3D11/PipelineD3D11.cpp index 051b8a01..dd9862c7 100644 --- a/Source/D3D11/PipelineD3D11.cpp +++ b/Source/D3D11/PipelineD3D11.cpp @@ -8,23 +8,11 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#include "SharedExternal.h" #include "SharedD3D11.h" #include "PipelineD3D11.h" -#include "DescriptorD3D11.h" -#include "DescriptorSetD3D11.h" - using namespace nri; -PipelineD3D11::PipelineD3D11(DeviceD3D11& device, const VersionedDevice* versionedDevice) : - m_VersionedDevice(versionedDevice), - m_InputAssemplyStrides(device.GetStdAllocator()), - m_RasterizerStates(device.GetStdAllocator()), - m_Device(device) -{ -} - PipelineD3D11::~PipelineD3D11() { Deallocate(m_Device.GetStdAllocator(), m_RasterizerStateExDesc); @@ -32,8 +20,6 @@ PipelineD3D11::~PipelineD3D11() Result PipelineD3D11::Create(const GraphicsPipelineDesc& pipelineDesc) { - const VersionedDevice& versionedDevice = *m_VersionedDevice; - const InputAssemblyDesc& ia = *pipelineDesc.inputAssembly; const RasterizationDesc& rs = *pipelineDesc.rasterization; const OutputMergerDesc& om = *pipelineDesc.outputMerger; @@ -51,27 +37,27 @@ Result PipelineD3D11::Create(const GraphicsPipelineDesc& pipelineDesc) if (shaderDesc->stage == ShaderStage::VERTEX) { vertexShader = shaderDesc; - hr = versionedDevice->CreateVertexShader(shaderDesc->bytecode, (size_t)shaderDesc->size, nullptr, &m_VertexShader); + hr = m_Device.GetDevice()->CreateVertexShader(shaderDesc->bytecode, (size_t)shaderDesc->size, nullptr, &m_VertexShader); RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "ID3D11Device::CreateVertexShader() - FAILED!"); } else if (shaderDesc->stage == ShaderStage::TESS_CONTROL) { - hr = versionedDevice->CreateHullShader(shaderDesc->bytecode, (size_t)shaderDesc->size, nullptr, &m_TessControlShader); + hr = m_Device.GetDevice()->CreateHullShader(shaderDesc->bytecode, (size_t)shaderDesc->size, nullptr, &m_TessControlShader); RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "ID3D11Device::CreateHullShader() - FAILED!"); } else if (shaderDesc->stage == ShaderStage::TESS_EVALUATION) { - hr = versionedDevice->CreateDomainShader(shaderDesc->bytecode, (size_t)shaderDesc->size, nullptr, &m_TessEvaluationShader); + hr = m_Device.GetDevice()->CreateDomainShader(shaderDesc->bytecode, (size_t)shaderDesc->size, nullptr, &m_TessEvaluationShader); RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "ID3D11Device::CreateDomainShader() - FAILED!"); } else if (shaderDesc->stage == ShaderStage::GEOMETRY) { - hr = versionedDevice->CreateGeometryShader(shaderDesc->bytecode, (size_t)shaderDesc->size, nullptr, &m_GeometryShader); + hr = m_Device.GetDevice()->CreateGeometryShader(shaderDesc->bytecode, (size_t)shaderDesc->size, nullptr, &m_GeometryShader); RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "ID3D11Device::CreateGeometryShader() - FAILED!"); } else if (shaderDesc->stage == ShaderStage::FRAGMENT) { - hr = versionedDevice->CreatePixelShader(shaderDesc->bytecode, (size_t)shaderDesc->size, nullptr, &m_FragmentShader); + hr = m_Device.GetDevice()->CreatePixelShader(shaderDesc->bytecode, (size_t)shaderDesc->size, nullptr, &m_FragmentShader); RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "ID3D11Device::CreatePixelShader() - FAILED!"); } else @@ -119,7 +105,7 @@ Result PipelineD3D11::Create(const GraphicsPipelineDesc& pipelineDesc) }; assert(vertexShader != nullptr); - hr = versionedDevice->CreateInputLayout(&inputElements[0], ia.attributeNum, vertexShader->bytecode, (size_t)vertexShader->size, &m_InputLayout); + hr = m_Device.GetDevice()->CreateInputLayout(&inputElements[0], ia.attributeNum, vertexShader->bytecode, (size_t)vertexShader->size, &m_InputLayout); RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "ID3D11Device::CreateInputLayout() - FAILED!"); } @@ -140,14 +126,14 @@ Result PipelineD3D11::Create(const GraphicsPipelineDesc& pipelineDesc) D3D11_CONSERVATIVE_RASTERIZATION_MODE_ON : D3D11_CONSERVATIVE_RASTERIZATION_MODE_OFF; RasterizerState rasterizerState; - if (versionedDevice.version >= 3) + if (m_Device.GetDevice().version >= 3) { - hr = versionedDevice->CreateRasterizerState2(&rasterizerDesc, &rasterizerState.ptr); + hr = m_Device.GetDevice()->CreateRasterizerState2(&rasterizerDesc, &rasterizerState.ptr); RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "ID3D11Device3::CreateRasterizerState2() - FAILED!"); } else { - hr = versionedDevice->CreateRasterizerState((D3D11_RASTERIZER_DESC*)&rasterizerDesc, (ID3D11RasterizerState**)&rasterizerState.ptr); + hr = m_Device.GetDevice()->CreateRasterizerState((D3D11_RASTERIZER_DESC*)&rasterizerDesc, (ID3D11RasterizerState**)&rasterizerState.ptr); RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "ID3D11Device::CreateRasterizerState() - FAILED!"); } @@ -183,7 +169,7 @@ Result PipelineD3D11::Create(const GraphicsPipelineDesc& pipelineDesc) depthStencilState.BackFace.StencilPassOp = GetD3D11StencilOpFromStencilFunc(ss.front.pass); depthStencilState.BackFace.StencilFunc = GetD3D11ComparisonFuncFromCompareFunc(ss.back.compareFunc); - hr = versionedDevice->CreateDepthStencilState(&depthStencilState, &m_DepthStencilState); + hr = m_Device.GetDevice()->CreateDepthStencilState(&depthStencilState, &m_DepthStencilState); RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "ID3D11Device::CreateDepthStencilState() - FAILED!"); // output merger @@ -208,8 +194,8 @@ Result PipelineD3D11::Create(const GraphicsPipelineDesc& pipelineDesc) blendState1.RenderTarget[i].LogicOp = GetD3D11LogicOpFromLogicFunc(om.colorLogicFunc); } - if (versionedDevice.version >= 1) - hr = versionedDevice->CreateBlendState1(&blendState1, &m_BlendState); + if (m_Device.GetDevice().version >= 1) + hr = m_Device.GetDevice()->CreateBlendState1(&blendState1, &m_BlendState); else { D3D11_BLEND_DESC blendState = {}; @@ -227,7 +213,7 @@ Result PipelineD3D11::Create(const GraphicsPipelineDesc& pipelineDesc) blendState.RenderTarget[i].RenderTargetWriteMask = blendState1.RenderTarget[i].RenderTargetWriteMask; } - hr = versionedDevice->CreateBlendState(&blendState, (ID3D11BlendState**)&m_BlendState); + hr = m_Device.GetDevice()->CreateBlendState(&blendState, (ID3D11BlendState**)&m_BlendState); } RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "ID3D11Device1::CreateBlendState1() - FAILED!"); @@ -241,27 +227,19 @@ Result PipelineD3D11::Create(const GraphicsPipelineDesc& pipelineDesc) Result PipelineD3D11::Create(const ComputePipelineDesc& pipelineDesc) { - const VersionedDevice& versionedDevice = *m_VersionedDevice; - - HRESULT hr; - - // shaders - if (pipelineDesc.computeShader.bytecode) { - hr = versionedDevice->CreateComputeShader(pipelineDesc.computeShader.bytecode, (size_t)pipelineDesc.computeShader.size, nullptr, &m_ComputeShader); + HRESULT hr = m_Device.GetDevice()->CreateComputeShader(pipelineDesc.computeShader.bytecode, (size_t)pipelineDesc.computeShader.size, nullptr, &m_ComputeShader); RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "ID3D11Device::CreateComputeShader() - FAILED!"); } - // resources - m_PipelineLayout = (const PipelineLayoutD3D11*)pipelineDesc.pipelineLayout; return Result::SUCCESS; } -void PipelineD3D11::ChangeSamplePositions(const VersionedContext& context, const SamplePositionsState& samplePositionState, DynamicState mode) +void PipelineD3D11::ChangeSamplePositions(const VersionedContext& deferredContext, const SamplePositionsState& samplePositionState, DynamicState mode) { if (IsCompute()) return; @@ -285,9 +263,9 @@ void PipelineD3D11::ChangeSamplePositions(const VersionedContext& context, const m_RasterizerStateExDesc->SamplePositionsY[j] = samplePositionState.positions[j].y + 8; } - if (context.ext->IsNvAPIAvailable()) + if (deferredContext.ext->IsNvAPIAvailable()) { - NvAPI_Status result = NvAPI_D3D11_CreateRasterizerState(m_VersionedDevice->ptr, m_RasterizerStateExDesc, (ID3D11RasterizerState**)&newState.ptr); + NvAPI_Status result = NvAPI_D3D11_CreateRasterizerState(m_Device.GetDevice().ptr, m_RasterizerStateExDesc, (ID3D11RasterizerState**)&newState.ptr); if (result != NVAPI_OK) REPORT_ERROR(m_Device.GetLog(), "NvAPI_D3D11_CreateRasterizerState() - FAILED!"); } @@ -302,20 +280,20 @@ void PipelineD3D11::ChangeSamplePositions(const VersionedContext& context, const ID3D11RasterizerState2* newState = m_RasterizerStates[i].ptr; if (mode == DynamicState::BIND_AND_SET && m_RasterizerState != newState) - context->RSSetState(newState); + deferredContext->RSSetState(newState); m_RasterizerState = newState; } -void PipelineD3D11::ChangeStencilReference(const VersionedContext& context, uint8_t stencilRef, DynamicState mode) +void PipelineD3D11::ChangeStencilReference(const VersionedContext& deferredContext, uint8_t stencilRef, DynamicState mode) { if (mode == DynamicState::BIND_AND_SET && m_StencilRef != stencilRef) - context->OMSetDepthStencilState(m_DepthStencilState, stencilRef); + deferredContext->OMSetDepthStencilState(m_DepthStencilState, stencilRef); m_StencilRef = stencilRef; } -void PipelineD3D11::Bind(const VersionedContext& context, const PipelineD3D11* currentPipeline) const +void PipelineD3D11::Bind(const VersionedContext& deferredContext, const PipelineD3D11* currentPipeline) const { if (this == currentPipeline) return; @@ -323,63 +301,65 @@ void PipelineD3D11::Bind(const VersionedContext& context, const PipelineD3D11* c if (IsCompute()) { if (!currentPipeline || m_ComputeShader != currentPipeline->m_ComputeShader) - context->CSSetShader(m_ComputeShader, nullptr, 0); + deferredContext->CSSetShader(m_ComputeShader, nullptr, 0); } else { if (!currentPipeline || m_Topology != currentPipeline->m_Topology) - context->IASetPrimitiveTopology(m_Topology); + deferredContext->IASetPrimitiveTopology(m_Topology); if (!currentPipeline || m_InputLayout != currentPipeline->m_InputLayout) - context->IASetInputLayout(m_InputLayout); + deferredContext->IASetInputLayout(m_InputLayout); if (!currentPipeline || m_RasterizerState != currentPipeline->m_RasterizerState) - context->RSSetState(m_RasterizerState); + deferredContext->RSSetState(m_RasterizerState); if (!currentPipeline || m_DepthStencilState != currentPipeline->m_DepthStencilState || m_StencilRef != currentPipeline->m_StencilRef) - context->OMSetDepthStencilState(m_DepthStencilState, m_StencilRef); + deferredContext->OMSetDepthStencilState(m_DepthStencilState, m_StencilRef); if (!currentPipeline || m_BlendState != currentPipeline->m_BlendState || m_SampleMask != currentPipeline->m_SampleMask || memcmp(&m_BlendFactor.x, ¤tPipeline->m_BlendFactor.x, sizeof(m_BlendFactor))) - context->OMSetBlendState(m_BlendState, &m_BlendFactor.x, m_SampleMask); + deferredContext->OMSetBlendState(m_BlendState, &m_BlendFactor.x, m_SampleMask); if (!currentPipeline || m_VertexShader != currentPipeline->m_VertexShader) - context->VSSetShader(m_VertexShader, nullptr, 0); + deferredContext->VSSetShader(m_VertexShader, nullptr, 0); if (!currentPipeline || m_TessControlShader != currentPipeline->m_TessControlShader) - context->HSSetShader(m_TessControlShader, nullptr, 0); + deferredContext->HSSetShader(m_TessControlShader, nullptr, 0); if (!currentPipeline || m_TessEvaluationShader != currentPipeline->m_TessEvaluationShader) - context->DSSetShader(m_TessEvaluationShader, nullptr, 0); + deferredContext->DSSetShader(m_TessEvaluationShader, nullptr, 0); if (!currentPipeline || m_GeometryShader != currentPipeline->m_GeometryShader) - context->GSSetShader(m_GeometryShader, nullptr, 0); + deferredContext->GSSetShader(m_GeometryShader, nullptr, 0); if (!currentPipeline || m_FragmentShader != currentPipeline->m_FragmentShader) - context->PSSetShader(m_FragmentShader, nullptr, 0); + deferredContext->PSSetShader(m_FragmentShader, nullptr, 0); if (m_IsRasterizerDiscarded) { // no RASTERIZER_DISCARD support in DX11, below is the simplest emulation D3D11_RECT rect = { -1, -1, -1, -1 }; - context->RSSetScissorRects(1, &rect); + deferredContext->RSSetScissorRects(1, &rect); } } } +//================================================================================================================ +// NRI +//================================================================================================================ + void PipelineD3D11::SetDebugName(const char* name) { - SetName(m_VertexShader, name); - SetName(m_TessControlShader, name); - SetName(m_TessEvaluationShader, name); - SetName(m_GeometryShader, name); - SetName(m_FragmentShader, name); - SetName(m_ComputeShader, name); - SetName(m_InputLayout, name); - SetName(m_DepthStencilState, name); - SetName(m_BlendState, name); + SET_D3D_DEBUG_OBJECT_NAME(m_VertexShader, name); + SET_D3D_DEBUG_OBJECT_NAME(m_TessControlShader, name); + SET_D3D_DEBUG_OBJECT_NAME(m_TessEvaluationShader, name); + SET_D3D_DEBUG_OBJECT_NAME(m_GeometryShader, name); + SET_D3D_DEBUG_OBJECT_NAME(m_FragmentShader, name); + SET_D3D_DEBUG_OBJECT_NAME(m_ComputeShader, name); + SET_D3D_DEBUG_OBJECT_NAME(m_InputLayout, name); + SET_D3D_DEBUG_OBJECT_NAME(m_DepthStencilState, name); + SET_D3D_DEBUG_OBJECT_NAME(m_BlendState, name); for (size_t i = 0; i < m_RasterizerStates.size(); i++) - SetName(m_RasterizerStates[i].ptr, name); + SET_D3D_DEBUG_OBJECT_NAME(m_RasterizerStates[i].ptr, name); } - -#include "PipelineD3D11.hpp" diff --git a/Source/D3D11/PipelineD3D11.h b/Source/D3D11/PipelineD3D11.h index dad73169..fd775013 100644 --- a/Source/D3D11/PipelineD3D11.h +++ b/Source/D3D11/PipelineD3D11.h @@ -14,74 +14,78 @@ struct NvAPI_D3D11_RASTERIZER_DESC_EX; namespace nri { - struct DeviceD3D11; - struct DescriptorSetD3D11; - struct PipelineLayoutD3D11; - - struct RasterizerState - { - ComPtr ptr; - uint64_t samplePositionHash = 0; - }; - - struct PipelineD3D11 - { - PipelineD3D11(DeviceD3D11& device, const VersionedDevice* versionedDevice); - ~PipelineD3D11(); - - inline DeviceD3D11& GetDevice() const - { return m_Device; } - - inline uint32_t GetInputAssemblyStride(uint32_t bindingSlot) const - { return m_InputAssemplyStrides[bindingSlot]; } - - inline bool IsValid() const - { return m_VersionedDevice != nullptr; } - - inline bool IsCompute() const - { return m_ComputeShader != nullptr; } - - inline bool IsRasterizerDiscarded() const - { return m_IsRasterizerDiscarded; } - - Result Create(const GraphicsPipelineDesc& pipelineDesc); - Result Create(const ComputePipelineDesc& pipelineDesc); - void Bind(const VersionedContext& context, const PipelineD3D11* currentPipeline) const; - - // dynamic state - void ChangeSamplePositions(const VersionedContext& context, const SamplePositionsState& samplePositionState, DynamicState mode); - void ChangeStencilReference(const VersionedContext& context, uint8_t stencilRef, DynamicState mode); - - //====================================================================================================================== - // NRI - //====================================================================================================================== - void SetDebugName(const char* name); - - private: - const VersionedDevice* m_VersionedDevice = nullptr; - - Vector m_InputAssemplyStrides; - Vector m_RasterizerStates; - - ComPtr m_VertexShader; - ComPtr m_TessControlShader; - ComPtr m_TessEvaluationShader; - ComPtr m_GeometryShader; - ComPtr m_FragmentShader; - ComPtr m_ComputeShader; - ComPtr m_InputLayout; - ComPtr m_DepthStencilState; - ComPtr m_BlendState; - - ID3D11RasterizerState2* m_RasterizerState = nullptr; - NvAPI_D3D11_RASTERIZER_DESC_EX* m_RasterizerStateExDesc = nullptr; - - D3D11_PRIMITIVE_TOPOLOGY m_Topology = D3D11_PRIMITIVE_TOPOLOGY_UNDEFINED; - Color32f m_BlendFactor = {}; - uint16_t m_SampleMask = 0; - uint16_t m_StencilRef = uint16_t(-1); - bool m_IsRasterizerDiscarded = false; - const PipelineLayoutD3D11* m_PipelineLayout = nullptr; - DeviceD3D11& m_Device; - }; + +struct DeviceD3D11; +struct DescriptorSetD3D11; +struct PipelineLayoutD3D11; + +struct RasterizerState +{ + ComPtr ptr; + uint64_t samplePositionHash = 0; +}; + +struct PipelineD3D11 +{ + inline PipelineD3D11(DeviceD3D11& device) : + m_Device(device) + , m_InputAssemplyStrides(device.GetStdAllocator()) + , m_RasterizerStates(device.GetStdAllocator()) + {} + + inline DeviceD3D11& GetDevice() const + { return m_Device; } + + inline uint32_t GetInputAssemblyStride(uint32_t bindingSlot) const + { return m_InputAssemplyStrides[bindingSlot]; } + + inline bool IsCompute() const + { return m_ComputeShader != nullptr; } + + inline bool IsRasterizerDiscarded() const + { return m_IsRasterizerDiscarded; } + + ~PipelineD3D11(); + + Result Create(const GraphicsPipelineDesc& pipelineDesc); + Result Create(const ComputePipelineDesc& pipelineDesc); + void Bind(const VersionedContext& deferredContext, const PipelineD3D11* currentPipeline) const; + + // dynamic state + void ChangeSamplePositions(const VersionedContext& deferredContext, const SamplePositionsState& samplePositionState, DynamicState mode); + void ChangeStencilReference(const VersionedContext& deferredContext, uint8_t stencilRef, DynamicState mode); + + //================================================================================================================ + // NRI + //================================================================================================================ + + void SetDebugName(const char* name); + +private: + DeviceD3D11& m_Device; + + Vector m_InputAssemplyStrides; + Vector m_RasterizerStates; + + ComPtr m_VertexShader; + ComPtr m_TessControlShader; + ComPtr m_TessEvaluationShader; + ComPtr m_GeometryShader; + ComPtr m_FragmentShader; + ComPtr m_ComputeShader; + ComPtr m_InputLayout; + ComPtr m_DepthStencilState; + ComPtr m_BlendState; + + ID3D11RasterizerState2* m_RasterizerState = nullptr; + NvAPI_D3D11_RASTERIZER_DESC_EX* m_RasterizerStateExDesc = nullptr; + + D3D11_PRIMITIVE_TOPOLOGY m_Topology = D3D11_PRIMITIVE_TOPOLOGY_UNDEFINED; + Color32f m_BlendFactor = {}; + uint16_t m_SampleMask = 0; + uint16_t m_StencilRef = uint16_t(-1); + bool m_IsRasterizerDiscarded = false; + const PipelineLayoutD3D11* m_PipelineLayout = nullptr; +}; + } diff --git a/Source/D3D11/PipelineD3D11.hpp b/Source/D3D11/PipelineD3D11.hpp deleted file mode 100644 index 9aff8832..00000000 --- a/Source/D3D11/PipelineD3D11.hpp +++ /dev/null @@ -1,23 +0,0 @@ -/* -Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. - -NVIDIA CORPORATION and its licensors retain all intellectual property -and proprietary rights in and to this software, related documentation -and any modifications thereto. Any use, reproduction, disclosure or -distribution of this software and related documentation without an express -license agreement from NVIDIA CORPORATION is strictly prohibited. -*/ - -#pragma region [ CoreInterface ] - -static void NRI_CALL SetPipelineDebugName(Pipeline& pipeline, const char* name) -{ - ((PipelineD3D11&)pipeline).SetDebugName(name); -} - -void FillFunctionTablePipelineD3D11(CoreInterface& coreInterface) -{ - coreInterface.SetPipelineDebugName = ::SetPipelineDebugName; -} - -#pragma endregion diff --git a/Source/D3D11/PipelineLayoutD3D11.cpp b/Source/D3D11/PipelineLayoutD3D11.cpp index 277d6927..e180694c 100644 --- a/Source/D3D11/PipelineLayoutD3D11.cpp +++ b/Source/D3D11/PipelineLayoutD3D11.cpp @@ -8,10 +8,8 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#include "SharedExternal.h" #include "SharedD3D11.h" #include "PipelineLayoutD3D11.h" - #include "DescriptorD3D11.h" #include "DescriptorSetD3D11.h" @@ -19,27 +17,27 @@ using namespace nri; #define SET_CONSTANT_BUFFERS1(xy, stage) \ if ( IsShaderVisible(bindingRange.shaderVisibility, stage) ) \ - context->xy##SetConstantBuffers1(bindingRange.baseSlot, bindingRange.descriptorNum, (ID3D11Buffer**)descriptors, constantFirst, constantNum) + deferredContext->xy##SetConstantBuffers1(bindingRange.baseSlot, bindingRange.descriptorNum, (ID3D11Buffer**)descriptors, constantFirst, constantNum) #define SET_CONSTANT_BUFFERS(xy, stage) \ if ( IsShaderVisible(bindingRange.shaderVisibility, stage) ) \ - context->xy##SetConstantBuffers(bindingRange.baseSlot, bindingRange.descriptorNum, (ID3D11Buffer**)descriptors) + deferredContext->xy##SetConstantBuffers(bindingRange.baseSlot, bindingRange.descriptorNum, (ID3D11Buffer**)descriptors) #define SET_SHADER_RESOURCES(xy, stage) \ if ( IsShaderVisible(bindingRange.shaderVisibility, stage) ) \ - context->xy##SetShaderResources(bindingRange.baseSlot, bindingRange.descriptorNum, (ID3D11ShaderResourceView**)descriptors) + deferredContext->xy##SetShaderResources(bindingRange.baseSlot, bindingRange.descriptorNum, (ID3D11ShaderResourceView**)descriptors) #define SET_SAMPLERS(xy, stage) \ if ( IsShaderVisible(bindingRange.shaderVisibility, stage) ) \ - context->xy##SetSamplers(bindingRange.baseSlot, bindingRange.descriptorNum, (ID3D11SamplerState**)descriptors) + deferredContext->xy##SetSamplers(bindingRange.baseSlot, bindingRange.descriptorNum, (ID3D11SamplerState**)descriptors) #define SET_CONSTANT_BUFFER(xy, stage) \ if ( IsShaderVisible(cb.shaderVisibility, stage) ) \ - context->xy##SetConstantBuffers(cb.slot, 1, (ID3D11Buffer**)&cb.buffer) + deferredContext->xy##SetConstantBuffers(cb.slot, 1, (ID3D11Buffer**)&cb.buffer) #define SET_SAMPLER(xy, stage) \ if ( IsShaderVisible(ss.shaderVisibility, stage) ) \ - context->xy##SetSamplers(ss.slot, 1, (ID3D11SamplerState**)&ss.sampler) + deferredContext->xy##SetSamplers(ss.slot, 1, (ID3D11SamplerState**)&ss.sampler) // see StageSlots constexpr std::array g_RemapDescriptorTypeToIndex = @@ -74,15 +72,6 @@ constexpr uint32_t GetShaderVisibility(ShaderStage visibility, PipelineLayoutSha return (visibility == ShaderStage::ALL) ? (uint32_t)stageMask : (1 << (uint32_t)visibility) & (uint32_t)stageMask; } -PipelineLayoutD3D11::PipelineLayoutD3D11(DeviceD3D11& device, const VersionedDevice& versionedDevice) : - m_BindingSets(device.GetStdAllocator()), - m_BindingRanges(device.GetStdAllocator()), - m_ConstantBuffers(device.GetStdAllocator()), - m_VersionedDevice(versionedDevice), - m_Device(device) -{ -} - Result PipelineLayoutD3D11::Create(const PipelineLayoutDesc& pipelineLayoutDesc) { m_IsGraphicsPipelineLayout = pipelineLayoutDesc.stageMask & PipelineLayoutShaderStageBits::ALL_GRAPHICS; @@ -112,7 +101,7 @@ Result PipelineLayoutD3D11::Create(const PipelineLayoutDesc& pipelineLayoutDesc) } // Dynamic constant buffers - if (set.dynamicConstantBufferNum && m_VersionedDevice.version == 0) + if (set.dynamicConstantBufferNum && m_Device.GetDevice().version == 0) REPORT_ERROR(m_Device.GetLog(), "Dynamic constant buffers with non-zero offsets require DX11.1+"); for (uint32_t j = 0; j < set.dynamicConstantBufferNum; j++) @@ -152,7 +141,7 @@ Result PipelineLayoutD3D11::Create(const PipelineLayoutDesc& pipelineLayoutDesc) cb.slot = pushConstant.registerIndex; desc.ByteWidth = Align(pushConstant.size, 16); - HRESULT hr = m_VersionedDevice->CreateBuffer(&desc, nullptr, &cb.buffer); + HRESULT hr = m_Device.GetDevice()->CreateBuffer(&desc, nullptr, &cb.buffer); if (FAILED(hr)) REPORT_ERROR(m_Device.GetLog(), "Can't create a constant buffer for push constants! ID3D11Device::CreateBuffer() - FAILED!"); @@ -162,7 +151,7 @@ Result PipelineLayoutD3D11::Create(const PipelineLayoutDesc& pipelineLayoutDesc) return Result::SUCCESS; } -void PipelineLayoutD3D11::Bind(const VersionedContext& context) +void PipelineLayoutD3D11::Bind(const VersionedContext& deferredContext) { for (size_t i = 0; i < m_ConstantBuffers.size(); i++) { @@ -177,29 +166,25 @@ void PipelineLayoutD3D11::Bind(const VersionedContext& context) } } -void PipelineLayoutD3D11::SetConstants(const VersionedContext& context, uint32_t pushConstantIndex, const Vec4* data, uint32_t size) const +void PipelineLayoutD3D11::SetConstants(const VersionedContext& deferredContext, uint32_t pushConstantIndex, const Vec4* data, uint32_t size) const { MaybeUnused(size); const ConstantBuffer& cb = m_ConstantBuffers[pushConstantIndex]; - context->UpdateSubresource(cb.buffer, 0, nullptr, data, 0, 0); + deferredContext->UpdateSubresource(cb.buffer, 0, nullptr, data, 0, 0); } -void PipelineLayoutD3D11::BindDescriptorSet(BindingState& currentBindingState, const VersionedContext& context, +void PipelineLayoutD3D11::BindDescriptorSet(BindingState& currentBindingState, const VersionedContext& deferredContext, uint32_t setIndexInPipelineLayout, const DescriptorSetD3D11& descriptorSet, const uint32_t* dynamicConstantBufferOffsets) const { if (m_IsGraphicsPipelineLayout) - BindDescriptorSetImpl(currentBindingState, context, setIndexInPipelineLayout, descriptorSet, dynamicConstantBufferOffsets); + BindDescriptorSetImpl(currentBindingState, deferredContext, setIndexInPipelineLayout, descriptorSet, dynamicConstantBufferOffsets); else - BindDescriptorSetImpl(currentBindingState, context, setIndexInPipelineLayout, descriptorSet, dynamicConstantBufferOffsets); -} - -void PipelineLayoutD3D11::SetDebugName(const char*) -{ + BindDescriptorSetImpl(currentBindingState, deferredContext, setIndexInPipelineLayout, descriptorSet, dynamicConstantBufferOffsets); } template -void PipelineLayoutD3D11::BindDescriptorSetImpl(BindingState& currentBindingState, const VersionedContext& context, uint32_t setIndexInPipelineLayout, +void PipelineLayoutD3D11::BindDescriptorSetImpl(BindingState& currentBindingState, const VersionedContext& deferredContext, uint32_t setIndexInPipelineLayout, const DescriptorSetD3D11& descriptorSet, const uint32_t* dynamicConstantBufferOffsets) const { const BindingSet& bindingSet = m_BindingSets[setIndexInPipelineLayout]; @@ -241,9 +226,9 @@ void PipelineLayoutD3D11::BindDescriptorSetImpl(BindingState& currentBindingStat constantNum[i] = descriptor->GetElementNum(); } else if (bindingRange.descriptorType == DescriptorTypeDX11::STORAGE) - currentBindingState.TrackSubresource_UnbindIfNeeded_PostponeGraphicsStorageBinding(context, descriptor->GetSubresourceInfo(), *descriptor, bindingRange.baseSlot + i, isGraphics, true); + currentBindingState.TrackSubresource_UnbindIfNeeded_PostponeGraphicsStorageBinding(deferredContext, descriptor->GetSubresourceInfo(), *descriptor, bindingRange.baseSlot + i, isGraphics, true); else if (bindingRange.descriptorType == DescriptorTypeDX11::RESOURCE) - currentBindingState.TrackSubresource_UnbindIfNeeded_PostponeGraphicsStorageBinding(context, descriptor->GetSubresourceInfo(), *descriptor, bindingRange.baseSlot + i, isGraphics, false); + currentBindingState.TrackSubresource_UnbindIfNeeded_PostponeGraphicsStorageBinding(deferredContext, descriptor->GetSubresourceInfo(), *descriptor, bindingRange.baseSlot + i, isGraphics, false); } else { @@ -323,7 +308,7 @@ void PipelineLayoutD3D11::BindDescriptorSetImpl(BindingState& currentBindingStat else { if (IsShaderVisible(bindingRange.shaderVisibility, ShaderStage::COMPUTE)) - context->CSSetUnorderedAccessViews(bindingRange.baseSlot, bindingRange.descriptorNum, (ID3D11UnorderedAccessView**)descriptors, nullptr); + deferredContext->CSSetUnorderedAccessViews(bindingRange.baseSlot, bindingRange.descriptorNum, (ID3D11UnorderedAccessView**)descriptors, nullptr); } } } @@ -333,8 +318,6 @@ void PipelineLayoutD3D11::BindDescriptorSetImpl(BindingState& currentBindingStat uint32_t num = (uint32_t)currentBindingState.graphicsStorageDescriptors.size(); ID3D11UnorderedAccessView** storages = currentBindingState.graphicsStorageDescriptors.data(); - context->OMSetRenderTargetsAndUnorderedAccessViews(D3D11_KEEP_RENDER_TARGETS_AND_DEPTH_STENCIL, nullptr, nullptr, 0, num, storages, nullptr); + deferredContext->OMSetRenderTargetsAndUnorderedAccessViews(D3D11_KEEP_RENDER_TARGETS_AND_DEPTH_STENCIL, nullptr, nullptr, 0, num, storages, nullptr); } } - -#include "PipelineLayoutD3D11.hpp" \ No newline at end of file diff --git a/Source/D3D11/PipelineLayoutD3D11.h b/Source/D3D11/PipelineLayoutD3D11.h index b4007b56..85b310e8 100644 --- a/Source/D3D11/PipelineLayoutD3D11.h +++ b/Source/D3D11/PipelineLayoutD3D11.h @@ -14,90 +14,87 @@ struct NvAPI_D3D11_RASTERIZER_DESC_EX; namespace nri { - struct DeviceD3D11; - struct DescriptorSetD3D11; - - struct BindingSet - { - uint32_t descriptorNum; - uint32_t rangeStart; - uint32_t rangeEnd; - }; - - struct BindingRange - { - uint32_t baseSlot; - uint32_t descriptorNum; - uint32_t descriptorOffset; - uint32_t shaderVisibility; - DescriptorTypeDX11 descriptorType; - }; - - struct ConstantBuffer - { - ComPtr buffer; - uint32_t slot; - uint32_t shaderVisibility; - }; - - union Vec4 - { - uint32_t ui[4]; - float f[4]; - }; - - struct BindingData - { - void** descriptors; - uint32_t* constantFirst; - uint32_t* constantNum; - }; - - struct PipelineLayoutD3D11 - { - PipelineLayoutD3D11(DeviceD3D11& device, const VersionedDevice& versionedDevice); - - Result Create(const PipelineLayoutDesc& pipelineDesc); - - DeviceD3D11& GetDevice() const; - const BindingSet& GetBindingSet(uint32_t set) const; - const BindingRange& GetBindingRange(uint32_t range) const; - void SetConstants(const VersionedContext& context, uint32_t pushConstantIndex, const Vec4* data, uint32_t size) const; - - void Bind(const VersionedContext& context); - void BindDescriptorSet(BindingState& currentBindingState, const VersionedContext& context, - uint32_t setIndexInPipelineLayout, const DescriptorSetD3D11& descriptorSet, const uint32_t* dynamicConstantBufferOffsets) const; - - //====================================================================================================================== - // NRI - //====================================================================================================================== - void SetDebugName(const char* name); - - private: - template - void BindDescriptorSetImpl(BindingState& currentBindingState, const VersionedContext& context, uint32_t setIndexInPipelineLayout, - const DescriptorSetD3D11& descriptorSet, const uint32_t* dynamicConstantBufferOffsets) const; - - Vector m_BindingSets; - Vector m_BindingRanges; - Vector m_ConstantBuffers; - bool m_IsGraphicsPipelineLayout = false; - const VersionedDevice& m_VersionedDevice; - DeviceD3D11& m_Device; - }; - - inline DeviceD3D11& PipelineLayoutD3D11::GetDevice() const - { - return m_Device; - } - - inline const BindingSet& PipelineLayoutD3D11::GetBindingSet(uint32_t set) const - { - return m_BindingSets[set]; - } - - inline const BindingRange& PipelineLayoutD3D11::GetBindingRange(uint32_t range) const - { - return m_BindingRanges[range]; - } + +struct DeviceD3D11; +struct DescriptorSetD3D11; + +struct BindingSet +{ + uint32_t descriptorNum; + uint32_t rangeStart; + uint32_t rangeEnd; +}; + +struct BindingRange +{ + uint32_t baseSlot; + uint32_t descriptorNum; + uint32_t descriptorOffset; + uint32_t shaderVisibility; + DescriptorTypeDX11 descriptorType; +}; + +struct ConstantBuffer +{ + ComPtr buffer; + uint32_t slot; + uint32_t shaderVisibility; +}; + +union Vec4 +{ + uint32_t ui[4]; + float f[4]; +}; + +struct BindingData +{ + void** descriptors; + uint32_t* constantFirst; + uint32_t* constantNum; +}; + +struct PipelineLayoutD3D11 +{ + inline PipelineLayoutD3D11(DeviceD3D11& device) : + m_Device(device) + , m_BindingSets(device.GetStdAllocator()) + , m_BindingRanges(device.GetStdAllocator()) + , m_ConstantBuffers(device.GetStdAllocator()) + {} + + inline DeviceD3D11& GetDevice() const + { return m_Device; } + + inline const BindingSet& GetBindingSet(uint32_t set) const + { return m_BindingSets[set]; } + + inline const BindingRange& GetBindingRange(uint32_t range) const + { return m_BindingRanges[range]; } + + Result Create(const PipelineLayoutDesc& pipelineDesc); + void SetConstants(const VersionedContext& deferredContext, uint32_t pushConstantIndex, const Vec4* data, uint32_t size) const; + void Bind(const VersionedContext& deferredContext); + void BindDescriptorSet(BindingState& currentBindingState, const VersionedContext& deferredContext, + uint32_t setIndexInPipelineLayout, const DescriptorSetD3D11& descriptorSet, const uint32_t* dynamicConstantBufferOffsets) const; + + //================================================================================================================ + // NRI + //================================================================================================================ + + inline void SetDebugName(const char* name) + { MaybeUnused(name); } + +private: + template + void BindDescriptorSetImpl(BindingState& currentBindingState, const VersionedContext& deferredContext, uint32_t setIndexInPipelineLayout, + const DescriptorSetD3D11& descriptorSet, const uint32_t* dynamicConstantBufferOffsets) const; + + DeviceD3D11& m_Device; + Vector m_BindingSets; + Vector m_BindingRanges; + Vector m_ConstantBuffers; + bool m_IsGraphicsPipelineLayout = false; +}; + } diff --git a/Source/D3D11/PipelineLayoutD3D11.hpp b/Source/D3D11/PipelineLayoutD3D11.hpp deleted file mode 100644 index edf016ed..00000000 --- a/Source/D3D11/PipelineLayoutD3D11.hpp +++ /dev/null @@ -1,23 +0,0 @@ -/* -Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. - -NVIDIA CORPORATION and its licensors retain all intellectual property -and proprietary rights in and to this software, related documentation -and any modifications thereto. Any use, reproduction, disclosure or -distribution of this software and related documentation without an express -license agreement from NVIDIA CORPORATION is strictly prohibited. -*/ - -#pragma region [ CoreInterface ] - -static void NRI_CALL SetPipelineLayoutDebugName(PipelineLayout& pipelineLayout, const char* name) -{ - ((PipelineLayoutD3D11&)pipelineLayout).SetDebugName(name); -} - -void FillFunctionTablePipelineLayoutD3D11(CoreInterface& coreInterface) -{ - coreInterface.SetPipelineLayoutDebugName = ::SetPipelineLayoutDebugName; -} - -#pragma endregion diff --git a/Source/D3D11/QueryPoolD3D11.cpp b/Source/D3D11/QueryPoolD3D11.cpp index 283dd8c4..cc3bf32f 100644 --- a/Source/D3D11/QueryPoolD3D11.cpp +++ b/Source/D3D11/QueryPoolD3D11.cpp @@ -8,23 +8,12 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#include "SharedExternal.h" #include "SharedD3D11.h" #include "QueryPoolD3D11.h" using namespace nri; -QueryPoolD3D11::QueryPoolD3D11(DeviceD3D11& device) : - m_Pool(device.GetStdAllocator()), - m_Device(device) -{ -} - -QueryPoolD3D11::~QueryPoolD3D11() -{ -} - -Result QueryPoolD3D11::Create(const VersionedDevice& device, const QueryPoolDesc& queryPoolDesc) +Result QueryPoolD3D11::Create(const QueryPoolDesc& queryPoolDesc) { m_Pool.reserve(queryPoolDesc.capacity); m_Type = queryPoolDesc.queryType; @@ -43,7 +32,7 @@ Result QueryPoolD3D11::Create(const VersionedDevice& device, const QueryPoolDesc for (uint32_t i = 0; i < queryPoolDesc.capacity; i++) { ID3D11Query* query = nullptr; - HRESULT hr = device->CreateQuery(&queryDesc, &query); + HRESULT hr = m_Device.GetDevice()->CreateQuery(&queryDesc, &query); RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "ID3D11Device::CreateQuery() - FAILED!"); m_Pool.push_back(query); @@ -52,21 +41,21 @@ Result QueryPoolD3D11::Create(const VersionedDevice& device, const QueryPoolDesc return Result::SUCCESS; } -void QueryPoolD3D11::BeginQuery(const VersionedContext& context, uint32_t offset) +void QueryPoolD3D11::BeginQuery(const VersionedContext& deferredContext, uint32_t offset) { ID3D11Query* query = m_Pool[offset]; - context->Begin(query); + deferredContext->Begin(query); } -void QueryPoolD3D11::EndQuery(const VersionedContext& context, uint32_t offset) +void QueryPoolD3D11::EndQuery(const VersionedContext& deferredContext, uint32_t offset) { ID3D11Query* query = m_Pool[offset]; - context->End(query); + deferredContext->End(query); } -void QueryPoolD3D11::GetData(uint8_t* dstMemory, const VersionedContext& immediateContext, uint32_t offset, uint32_t num) const +void QueryPoolD3D11::GetData(uint8_t* dstMemory, uint32_t offset, uint32_t num) const { uint32_t dataSize = GetQuerySize(); @@ -75,15 +64,20 @@ void QueryPoolD3D11::GetData(uint8_t* dstMemory, const VersionedContext& immedia for (uint32_t i = offset; i < num; i++) { ID3D11Query* query = m_Pool[i]; - immediateContext->GetData(query, dstMemory, dataSize, 0); + m_Device.GetImmediateContext()->GetData(query, dstMemory, dataSize, 0); dstMemory += dataSize; } } +//================================================================================================================ +// NRI +//================================================================================================================ + inline void QueryPoolD3D11::SetDebugName(const char* name) { - MaybeUnused(name); + for(ComPtr& query : m_Pool) + SET_D3D_DEBUG_OBJECT_NAME(query, name); } inline uint32_t QueryPoolD3D11::GetQuerySize() const diff --git a/Source/D3D11/QueryPoolD3D11.h b/Source/D3D11/QueryPoolD3D11.h index bf9aa52d..f92723d8 100644 --- a/Source/D3D11/QueryPoolD3D11.h +++ b/Source/D3D11/QueryPoolD3D11.h @@ -12,30 +12,38 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct DeviceD3D11; - - struct QueryPoolD3D11 - { - QueryPoolD3D11(DeviceD3D11& device); - ~QueryPoolD3D11(); - - inline DeviceD3D11& GetDevice() const - { return m_Device; } - - Result Create(const VersionedDevice& device, const QueryPoolDesc& queryPoolDesc); - void BeginQuery(const VersionedContext& context, uint32_t offset); - void EndQuery(const VersionedContext& context, uint32_t offset); - void GetData(uint8_t* dstMemory, const VersionedContext& immediateContext, uint32_t offset, uint32_t num) const; - - //====================================================================================================================== - // NRI - //====================================================================================================================== - void SetDebugName(const char* name); - uint32_t GetQuerySize() const; - - private: - Vector> m_Pool; - DeviceD3D11& m_Device; - QueryType m_Type = QueryType::MAX_NUM; - }; + +struct DeviceD3D11; + +struct QueryPoolD3D11 +{ + inline QueryPoolD3D11(DeviceD3D11& device) : + m_Device(device) + , m_Pool(device.GetStdAllocator()) + {} + + inline ~QueryPoolD3D11() + {} + + inline DeviceD3D11& GetDevice() const + { return m_Device; } + + Result Create(const QueryPoolDesc& queryPoolDesc); + void BeginQuery(const VersionedContext& deferredContext, uint32_t offset); + void EndQuery(const VersionedContext& deferredContext, uint32_t offset); + void GetData(uint8_t* dstMemory, uint32_t offset, uint32_t num) const; + + //================================================================================================================ + // NRI + //================================================================================================================ + + void SetDebugName(const char* name); + uint32_t GetQuerySize() const; + +private: + DeviceD3D11& m_Device; + Vector> m_Pool; + QueryType m_Type = QueryType::MAX_NUM; +}; + } diff --git a/Source/D3D11/QueryPoolD3D11.hpp b/Source/D3D11/QueryPoolD3D11.hpp index d12fa93b..146767bf 100644 --- a/Source/D3D11/QueryPoolD3D11.hpp +++ b/Source/D3D11/QueryPoolD3D11.hpp @@ -8,7 +8,7 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ CoreInterface ] +#pragma region [ Core ] static void NRI_CALL SetQueryPoolDebugName(QueryPool& queryPool, const char* name) { @@ -20,10 +20,6 @@ static uint32_t NRI_CALL GetQuerySize(const QueryPool& queryPool) return ((QueryPoolD3D11&)queryPool).GetQuerySize(); } -void FillFunctionTableQueryPoolD3D11(CoreInterface& coreInterface) -{ - coreInterface.SetQueryPoolDebugName = ::SetQueryPoolDebugName; - coreInterface.GetQuerySize = ::GetQuerySize; -} - #pragma endregion + +Define_Core_QueryPool_PartiallyFillFunctionTable(D3D11) diff --git a/Source/D3D11/QueueSemaphoreD3D11.cpp b/Source/D3D11/QueueSemaphoreD3D11.cpp deleted file mode 100644 index d0d26932..00000000 --- a/Source/D3D11/QueueSemaphoreD3D11.cpp +++ /dev/null @@ -1,31 +0,0 @@ -/* -Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. - -NVIDIA CORPORATION and its licensors retain all intellectual property -and proprietary rights in and to this software, related documentation -and any modifications thereto. Any use, reproduction, disclosure or -distribution of this software and related documentation without an express -license agreement from NVIDIA CORPORATION is strictly prohibited. -*/ - -#include "SharedExternal.h" -#include "SharedD3D11.h" -#include "QueueSemaphoreD3D11.h" - -using namespace nri; - -QueueSemaphoreD3D11::QueueSemaphoreD3D11(DeviceD3D11& device) : - m_Device(device) -{ -} - -QueueSemaphoreD3D11::~QueueSemaphoreD3D11() -{ -} - -void QueueSemaphoreD3D11::SetDebugName(const char* name) -{ - MaybeUnused(name); -} - -#include "QueueSemaphoreD3D11.hpp" diff --git a/Source/D3D11/QueueSemaphoreD3D11.h b/Source/D3D11/QueueSemaphoreD3D11.h deleted file mode 100644 index a30efb49..00000000 --- a/Source/D3D11/QueueSemaphoreD3D11.h +++ /dev/null @@ -1,39 +0,0 @@ -/* -Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. - -NVIDIA CORPORATION and its licensors retain all intellectual property -and proprietary rights in and to this software, related documentation -and any modifications thereto. Any use, reproduction, disclosure or -distribution of this software and related documentation without an express -license agreement from NVIDIA CORPORATION is strictly prohibited. -*/ - -#pragma once - -namespace nri -{ - struct DeviceD3D11; - - struct QueueSemaphoreD3D11 - { - QueueSemaphoreD3D11(DeviceD3D11& device); - ~QueueSemaphoreD3D11(); - - inline DeviceD3D11& GetDevice() const - { return m_Device; } - - inline void Wait() - {} - - inline void Signal() - {} - - //====================================================================================================================== - // NRI - //====================================================================================================================== - void SetDebugName(const char* name); - - private: - DeviceD3D11& m_Device; - }; -} diff --git a/Source/D3D11/QueueSemaphoreD3D11.hpp b/Source/D3D11/QueueSemaphoreD3D11.hpp deleted file mode 100644 index d3669003..00000000 --- a/Source/D3D11/QueueSemaphoreD3D11.hpp +++ /dev/null @@ -1,23 +0,0 @@ -/* -Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. - -NVIDIA CORPORATION and its licensors retain all intellectual property -and proprietary rights in and to this software, related documentation -and any modifications thereto. Any use, reproduction, disclosure or -distribution of this software and related documentation without an express -license agreement from NVIDIA CORPORATION is strictly prohibited. -*/ - -#pragma region [ CoreInterface ] - -static void NRI_CALL SetQueueSemaphoreDebugName(QueueSemaphore& queueSemaphore, const char* name) -{ - ((QueueSemaphoreD3D11&)queueSemaphore).SetDebugName(name); -} - -void FillFunctionTableQueueSemaphoreD3D11(CoreInterface& coreInterface) -{ - coreInterface.SetQueueSemaphoreDebugName = ::SetQueueSemaphoreDebugName; -} - -#pragma endregion diff --git a/Source/D3D11/SharedD3D11.cpp b/Source/D3D11/SharedD3D11.cpp index dbbb991a..b49f7fc3 100644 --- a/Source/D3D11/SharedD3D11.cpp +++ b/Source/D3D11/SharedD3D11.cpp @@ -8,7 +8,6 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#include "SharedExternal.h" #include "SharedD3D11.h" constexpr std::array FORMAT_INFO_TABLE = @@ -246,7 +245,7 @@ D3D11_PRIMITIVE_TOPOLOGY GetD3D11TopologyFromTopology(nri::Topology topology, ui return (D3D11_PRIMITIVE_TOPOLOGY)res; } -//============================================================================================================================== +//================================================================================================================ inline bool DoesLibraryExist(const Log& log, const char* moduleName) { diff --git a/Source/D3D11/SharedD3D11.h b/Source/D3D11/SharedD3D11.h index bb24af42..5177dd6c 100644 --- a/Source/D3D11/SharedD3D11.h +++ b/Source/D3D11/SharedD3D11.h @@ -12,6 +12,7 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include +#include "SharedExternal.h" #include "DeviceBase.h" #define NULL_TEXTURE_REGION_DESC 0xFFFF @@ -67,7 +68,6 @@ D3D11_LOGIC_OP GetD3D11LogicOpFromLogicFunc(nri::LogicFunc logicalFunc); struct AGSContext; struct D3D11Extensions; -struct IDXGISwapChain4; struct VersionedDevice { @@ -82,7 +82,7 @@ struct VersionedDevice ComPtr ptr; const D3D11Extensions* ext = nullptr; - bool isDeferredContextsEmulated = false; + bool isDeferredContextEmulated = false; uint8_t version = 0; }; @@ -120,25 +120,13 @@ struct VersionedContext uint8_t version = 0; }; -struct VersionedSwapchain -{ - ~VersionedSwapchain() - {} - - inline IDXGISwapChain4* operator->() const - { return ptr; } - - ComPtr ptr; - uint8_t version = 0; -}; - struct CriticalSection { - CriticalSection(const VersionedContext& context) : - m_Context(context) + inline CriticalSection(const VersionedContext& deferredContext) : + m_Context(deferredContext) { m_Context.EnterCriticalSection(); } - ~CriticalSection() + inline ~CriticalSection() { m_Context.LeaveCriticalSection(); } const VersionedContext& m_Context; @@ -177,7 +165,7 @@ struct BindingState std::vector storages; // max expected size - D3D11_1_UAV_SLOT_COUNT std::array graphicsStorageDescriptors = {}; - inline void TrackSubresource_UnbindIfNeeded_PostponeGraphicsStorageBinding(const VersionedContext& context, const SubresourceInfo& subresource, void* descriptor, uint32_t slot, bool isGraphics, bool isStorage) + inline void TrackSubresource_UnbindIfNeeded_PostponeGraphicsStorageBinding(const VersionedContext& deferredContext, const SubresourceInfo& subresource, void* descriptor, uint32_t slot, bool isGraphics, bool isStorage) { constexpr void* null = nullptr; @@ -189,12 +177,12 @@ struct BindingState if (subresourceAndSlot.subresource == subresource) { // TODO: store visibility to unbind only in a necessary stage - context->VSSetShaderResources(subresourceAndSlot.slot, 1, (ID3D11ShaderResourceView**)&null); - context->HSSetShaderResources(subresourceAndSlot.slot, 1, (ID3D11ShaderResourceView**)&null); - context->DSSetShaderResources(subresourceAndSlot.slot, 1, (ID3D11ShaderResourceView**)&null); - context->GSSetShaderResources(subresourceAndSlot.slot, 1, (ID3D11ShaderResourceView**)&null); - context->PSSetShaderResources(subresourceAndSlot.slot, 1, (ID3D11ShaderResourceView**)&null); - context->CSSetShaderResources(subresourceAndSlot.slot, 1, (ID3D11ShaderResourceView**)&null); + deferredContext->VSSetShaderResources(subresourceAndSlot.slot, 1, (ID3D11ShaderResourceView**)&null); + deferredContext->HSSetShaderResources(subresourceAndSlot.slot, 1, (ID3D11ShaderResourceView**)&null); + deferredContext->DSSetShaderResources(subresourceAndSlot.slot, 1, (ID3D11ShaderResourceView**)&null); + deferredContext->GSSetShaderResources(subresourceAndSlot.slot, 1, (ID3D11ShaderResourceView**)&null); + deferredContext->PSSetShaderResources(subresourceAndSlot.slot, 1, (ID3D11ShaderResourceView**)&null); + deferredContext->CSSetShaderResources(subresourceAndSlot.slot, 1, (ID3D11ShaderResourceView**)&null); resources[i] = resources.back(); resources.pop_back(); @@ -214,7 +202,7 @@ struct BindingState const SubresourceAndSlot& subresourceAndSlot = storages[i]; if (subresourceAndSlot.subresource == subresource) { - context->CSSetUnorderedAccessViews(subresourceAndSlot.slot, 1, (ID3D11UnorderedAccessView**)&null, nullptr); + deferredContext->CSSetUnorderedAccessViews(subresourceAndSlot.slot, 1, (ID3D11UnorderedAccessView**)&null, nullptr); graphicsStorageDescriptors[subresourceAndSlot.slot] = nullptr; @@ -228,26 +216,26 @@ struct BindingState } } - inline void UnbindAndReset(const VersionedContext& context) + inline void UnbindAndReset(const VersionedContext& deferredContext) { constexpr void* null = nullptr; for (const SubresourceAndSlot& subresourceAndSlot : resources) { // TODO: store visibility to unbind only in a necessary stage - context->VSSetShaderResources(subresourceAndSlot.slot, 1, (ID3D11ShaderResourceView**)&null); - context->HSSetShaderResources(subresourceAndSlot.slot, 1, (ID3D11ShaderResourceView**)&null); - context->DSSetShaderResources(subresourceAndSlot.slot, 1, (ID3D11ShaderResourceView**)&null); - context->GSSetShaderResources(subresourceAndSlot.slot, 1, (ID3D11ShaderResourceView**)&null); - context->PSSetShaderResources(subresourceAndSlot.slot, 1, (ID3D11ShaderResourceView**)&null); - context->CSSetShaderResources(subresourceAndSlot.slot, 1, (ID3D11ShaderResourceView**)&null); + deferredContext->VSSetShaderResources(subresourceAndSlot.slot, 1, (ID3D11ShaderResourceView**)&null); + deferredContext->HSSetShaderResources(subresourceAndSlot.slot, 1, (ID3D11ShaderResourceView**)&null); + deferredContext->DSSetShaderResources(subresourceAndSlot.slot, 1, (ID3D11ShaderResourceView**)&null); + deferredContext->GSSetShaderResources(subresourceAndSlot.slot, 1, (ID3D11ShaderResourceView**)&null); + deferredContext->PSSetShaderResources(subresourceAndSlot.slot, 1, (ID3D11ShaderResourceView**)&null); + deferredContext->CSSetShaderResources(subresourceAndSlot.slot, 1, (ID3D11ShaderResourceView**)&null); } resources.clear(); if (!storages.empty()) - context->OMSetRenderTargetsAndUnorderedAccessViews(D3D11_KEEP_RENDER_TARGETS_AND_DEPTH_STENCIL, nullptr, nullptr, 0, 0, nullptr, nullptr); + deferredContext->OMSetRenderTargetsAndUnorderedAccessViews(D3D11_KEEP_RENDER_TARGETS_AND_DEPTH_STENCIL, nullptr, nullptr, 0, 0, nullptr, nullptr); for (const SubresourceAndSlot& subresourceAndSlot : storages) - context->CSSetUnorderedAccessViews(subresourceAndSlot.slot, 1, (ID3D11UnorderedAccessView**)&null, nullptr); + deferredContext->CSSetUnorderedAccessViews(subresourceAndSlot.slot, 1, (ID3D11UnorderedAccessView**)&null, nullptr); storages.clear(); memset(&graphicsStorageDescriptors, 0, sizeof(graphicsStorageDescriptors)); @@ -260,17 +248,12 @@ namespace nri { virtual ~CommandBufferHelper() {} virtual Result Create(ID3D11DeviceContext* precreatedContext) = 0; - virtual void Submit(const VersionedContext& context) = 0; + virtual void Submit() = 0; + virtual ID3D11DeviceContext* GetNativeObject() const = 0; virtual StdAllocator& GetStdAllocator() const = 0; }; } -template void SetName(const ComPtr& obj, const char* name) -{ - if (obj) - obj->SetPrivateData(WKPDID_D3DDebugObjectName, (uint32_t)std::strlen(name), name); -} - static inline uint64_t ComputeHash(const void* key, uint32_t len) { const uint8_t* p = (uint8_t*)key; diff --git a/Source/D3D11/SwapChainD3D11.cpp b/Source/D3D11/SwapChainD3D11.cpp index c7f5afcb..106bbf12 100644 --- a/Source/D3D11/SwapChainD3D11.cpp +++ b/Source/D3D11/SwapChainD3D11.cpp @@ -8,14 +8,11 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#include "SharedExternal.h" +#include + #include "SharedD3D11.h" #include "SwapChainD3D11.h" - #include "TextureD3D11.h" -#include "QueueSemaphoreD3D11.h" - -#include using namespace nri; @@ -46,13 +43,6 @@ static std::array g_swapChainTextureFormat = Format::R10_G10_B10_A2_UNORM, // BT2020_G2084_10BIT }; -SwapChainD3D11::SwapChainD3D11(DeviceD3D11& device) : - m_RenderTargets(device.GetStdAllocator()), - m_RenderTargetPointers(device.GetStdAllocator()), - m_Device(device) -{ -} - SwapChainD3D11::~SwapChainD3D11() { if (m_IsFullscreenEnabled) @@ -62,12 +52,19 @@ SwapChainD3D11::~SwapChainD3D11() if (fullscreen) m_SwapChain->SetFullscreenState(FALSE, nullptr); } + + for (TextureD3D11* texture : m_Textures) + Deallocate(m_Device.GetStdAllocator(), texture); } -Result SwapChainD3D11::Create(const VersionedDevice& device, const SwapChainDesc& swapChainDesc) +Result SwapChainD3D11::Create(const SwapChainDesc& swapChainDesc) { + HWND hwnd = (HWND)swapChainDesc.window.windows.hwnd; + if (!hwnd) + return Result::INVALID_ARGUMENT; + ComPtr dxgiDevice; - HRESULT hr = device->QueryInterface(IID_PPV_ARGS(&dxgiDevice)); + HRESULT hr = m_Device.GetDevice()->QueryInterface(IID_PPV_ARGS(&dxgiDevice)); RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "IUnknown::QueryInterface() - FAILED!"); ComPtr dxgiAdapter; @@ -91,8 +88,6 @@ Result SwapChainD3D11::Create(const VersionedDevice& device, const SwapChainDesc DXGI_FORMAT format = g_swapChainFormat[(uint32_t)swapChainDesc.format]; DXGI_COLOR_SPACE_TYPE colorSpace = g_colorSpace[(uint32_t)swapChainDesc.format]; - const HWND hwnd = (HWND)swapChainDesc.window.windows.hwnd; - DXGI_SWAP_CHAIN_DESC1 desc = {}; desc.BufferCount = swapChainDesc.textureNum; desc.Width = swapChainDesc.width; @@ -106,12 +101,12 @@ Result SwapChainD3D11::Create(const VersionedDevice& device, const SwapChainDesc desc.AlphaMode = DXGI_ALPHA_MODE_IGNORE; ComPtr swapChain; - hr = dxgiFactory2->CreateSwapChainForHwnd(device.ptr, hwnd, &desc, nullptr, nullptr, &swapChain); + hr = dxgiFactory2->CreateSwapChainForHwnd(m_Device.GetDevice().ptr, hwnd, &desc, nullptr, nullptr, &swapChain); if (FAILED(hr)) { // are we on Win7? desc.Scaling = DXGI_SCALING_STRETCH; - hr = dxgiFactory2->CreateSwapChainForHwnd(device.ptr, hwnd, &desc, nullptr, nullptr, &swapChain); + hr = dxgiFactory2->CreateSwapChainForHwnd(m_Device.GetDevice().ptr, hwnd, &desc, nullptr, nullptr, &swapChain); } RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "IDXGIFactory2::CreateSwapChainForHwnd() - FAILED!"); @@ -123,17 +118,17 @@ Result SwapChainD3D11::Create(const VersionedDevice& device, const SwapChainDesc if (FAILED(hr)) { REPORT_WARNING(m_Device.GetLog(), "QueryInterface(IDXGISwapChain4) - FAILED!"); - hr = device->QueryInterface(__uuidof(IDXGISwapChain3), (void**)&m_SwapChain.ptr); + hr = m_Device.GetDevice()->QueryInterface(__uuidof(IDXGISwapChain3), (void**)&m_SwapChain.ptr); m_SwapChain.version = 3; if (FAILED(hr)) { REPORT_WARNING(m_Device.GetLog(), "QueryInterface(IDXGISwapChain3) - FAILED!"); - hr = device->QueryInterface(__uuidof(IDXGISwapChain2), (void**)&m_SwapChain.ptr); + hr = m_Device.GetDevice()->QueryInterface(__uuidof(IDXGISwapChain2), (void**)&m_SwapChain.ptr); m_SwapChain.version = 2; if (FAILED(hr)) { REPORT_WARNING(m_Device.GetLog(), "QueryInterface(IDXGISwapChain2) - FAILED!"); - hr = device->QueryInterface(__uuidof(IDXGISwapChain1), (void**)&m_SwapChain.ptr); + hr = m_Device.GetDevice()->QueryInterface(__uuidof(IDXGISwapChain1), (void**)&m_SwapChain.ptr); m_SwapChain.version = 1; if (FAILED(hr)) { @@ -180,59 +175,62 @@ Result SwapChainD3D11::Create(const VersionedDevice& device, const SwapChainDesc } hr = m_SwapChain->SetFullscreenState(TRUE, output); - RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "IDXGISwapChain1::SetFullscreenState() failed, error code: 0x%X.", hr); + RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "IDXGISwapChain::SetFullscreenState() failed, error code: 0x%X.", hr); hr = m_SwapChain->ResizeBuffers(desc.BufferCount, desc.Width, desc.Height, desc.Format, desc.Flags); - RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "IDXGISwapChain1::ResizeBuffers() failed, error code: 0x%X.", hr); + RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "IDXGISwapChain::ResizeBuffers() failed, error code: 0x%X.", hr); m_IsTearingAllowed = false; m_IsFullscreenEnabled = true; } - // in DX11 only 'bufferIndex = 0' can be used to create render targets, so set BufferCount to '1' and ignore 'desc.BufferCount' - const uint32_t bufferCount = 1; + // Use "swapChainDesc.textureNum" to explicitly limit maximum frames in flight + ComPtr dxgiDevice1; + hr = dxgiDevice->QueryInterface(IID_PPV_ARGS(&dxgiDevice1)); + if (SUCCEEDED(hr)) + dxgiDevice1->SetMaximumFrameLatency(swapChainDesc.textureNum); + + m_Format = g_swapChainTextureFormat[(uint32_t)swapChainDesc.format]; + m_SwapChainDesc = swapChainDesc; - m_RenderTargets.resize(bufferCount); - m_RenderTargetPointers.clear(); + // In DX11 only 'bufferIndex = 0' can be used to create render targets, so set BufferCount to '1' and ignore 'desc.BufferCount' + m_SwapChainDesc.textureNum = 1; - for (uint32_t i = 0; i < bufferCount; i++) + m_Textures.reserve(m_SwapChainDesc.textureNum); + for (uint32_t i = 0; i < m_SwapChainDesc.textureNum; i++) { - ComPtr backBuffer; - hr = m_SwapChain->GetBuffer(i, IID_PPV_ARGS(&backBuffer)); + ComPtr textureNative; + hr = m_SwapChain->GetBuffer(i, IID_PPV_ARGS(&textureNative)); RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "IDXGISwapChain::GetBuffer() - FAILED!"); TextureD3D11Desc textureDesc = {}; - textureDesc.d3d11Resource = backBuffer; - m_RenderTargets[i].Create(m_Device, textureDesc); + textureDesc.d3d11Resource = textureNative; - m_RenderTargetPointers.push_back((Texture*)&m_RenderTargets[i]); - } - - m_Format = g_swapChainTextureFormat[(uint32_t)swapChainDesc.format]; + TextureD3D11* texture = Allocate(m_Device.GetStdAllocator(), m_Device); + const nri::Result res = texture->Create(textureDesc); + if (res != nri::Result::SUCCESS) + return res; - m_SwapChainDesc = swapChainDesc; - m_SwapChainDesc.textureNum = bufferCount; + m_Textures.push_back(texture); + } return Result::SUCCESS; } -inline void SwapChainD3D11::SetDebugName(const char* name) -{ - SetName(m_SwapChain.ptr, name); -} +//================================================================================================================ +// NRI +//================================================================================================================ inline Texture* const* SwapChainD3D11::GetTextures(uint32_t& textureNum, Format& format) const { textureNum = m_SwapChainDesc.textureNum; format = m_Format; - return &m_RenderTargetPointers[0]; + return (Texture**)m_Textures.data(); } -inline uint32_t SwapChainD3D11::AcquireNextTexture(QueueSemaphore& textureReadyForRender) +inline uint32_t SwapChainD3D11::AcquireNextTexture() { - ((QueueSemaphoreD3D11&)textureReadyForRender).Signal(); - uint32_t nextTextureIndex = 0; if (m_SwapChain.version >= 3) nextTextureIndex = m_SwapChain->GetCurrentBackBufferIndex(); @@ -240,10 +238,8 @@ inline uint32_t SwapChainD3D11::AcquireNextTexture(QueueSemaphore& textureReadyF return nextTextureIndex; } -inline Result SwapChainD3D11::Present(QueueSemaphore& textureReadyForPresent) +inline Result SwapChainD3D11::Present() { - ((QueueSemaphoreD3D11&)textureReadyForPresent).Wait(); - BOOL fullscreen = FALSE; m_SwapChain->GetFullscreenState(&fullscreen, nullptr); if (fullscreen != BOOL(m_IsFullscreenEnabled)) diff --git a/Source/D3D11/SwapChainD3D11.h b/Source/D3D11/SwapChainD3D11.h index cd93d4d9..a252ff47 100644 --- a/Source/D3D11/SwapChainD3D11.h +++ b/Source/D3D11/SwapChainD3D11.h @@ -10,38 +10,60 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #pragma once +struct IDXGISwapChain4; + namespace nri { - struct DeviceD3D11; - struct TextureD3D11; - - struct SwapChainD3D11 - { - SwapChainD3D11(DeviceD3D11& device); - ~SwapChainD3D11(); - - inline DeviceD3D11& GetDevice() const - { return m_Device; } - - Result Create(const VersionedDevice& device, const SwapChainDesc& swapChainDesc); - - //====================================================================================================================== - // NRI - //====================================================================================================================== - void SetDebugName(const char* name); - Texture* const* GetTextures(uint32_t& textureNum, Format& format) const; - uint32_t AcquireNextTexture(QueueSemaphore& textureReadyForRender); - Result Present(QueueSemaphore& textureReadyForPresent); - Result SetHdrMetadata(const HdrMetadata& hdrMetadata); - - private: - VersionedSwapchain m_SwapChain; - Vector m_RenderTargets; - Vector m_RenderTargetPointers; - SwapChainDesc m_SwapChainDesc = {}; - Format m_Format = Format::UNKNOWN; - bool m_IsTearingAllowed = false; - bool m_IsFullscreenEnabled = false; - DeviceD3D11& m_Device; - }; + +struct DeviceD3D11; +struct TextureD3D11; + +struct VersionedSwapChain +{ + inline ~VersionedSwapChain() + {} + + inline IDXGISwapChain4* operator->() const + { return ptr; } + + ComPtr ptr; + uint8_t version = 0; +}; + +struct SwapChainD3D11 +{ + inline SwapChainD3D11(DeviceD3D11& device) : + m_Device(device) + , m_Textures(device.GetStdAllocator()) + {} + + inline DeviceD3D11& GetDevice() const + { return m_Device; } + + ~SwapChainD3D11(); + + Result Create(const SwapChainDesc& swapChainDesc); + + //================================================================================================================ + // NRI + //================================================================================================================ + + inline void SetDebugName(const char* name) + { SET_D3D_DEBUG_OBJECT_NAME(m_SwapChain.ptr, name); } + + Texture* const* GetTextures(uint32_t& textureNum, Format& format) const; + uint32_t AcquireNextTexture(); + Result Present(); + Result SetHdrMetadata(const HdrMetadata& hdrMetadata); + +private: + DeviceD3D11& m_Device; + VersionedSwapChain m_SwapChain; + Vector m_Textures; + SwapChainDesc m_SwapChainDesc = {}; + Format m_Format = Format::UNKNOWN; + bool m_IsTearingAllowed = false; + bool m_IsFullscreenEnabled = false; +}; + } diff --git a/Source/D3D11/SwapChainD3D11.hpp b/Source/D3D11/SwapChainD3D11.hpp index 9421261f..4c17996f 100644 --- a/Source/D3D11/SwapChainD3D11.hpp +++ b/Source/D3D11/SwapChainD3D11.hpp @@ -8,8 +8,6 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ SwapChainInterface ] - static void NRI_CALL SetSwapChainDebugName(SwapChain& swapChain, const char* name) { ((SwapChainD3D11&)swapChain).SetDebugName(name); @@ -20,14 +18,14 @@ static Texture* const* NRI_CALL GetSwapChainTextures(const SwapChain& swapChain, return ((SwapChainD3D11&)swapChain).GetTextures(textureNum, format); } -static uint32_t NRI_CALL AcquireNextSwapChainTexture(SwapChain& swapChain, QueueSemaphore& textureReadyForRender) +static uint32_t NRI_CALL AcquireNextSwapChainTexture(SwapChain& swapChain) { - return ((SwapChainD3D11&)swapChain).AcquireNextTexture(textureReadyForRender); + return ((SwapChainD3D11&)swapChain).AcquireNextTexture(); } -static Result NRI_CALL SwapChainPresent(SwapChain& swapChain, QueueSemaphore& textureReadyForPresent) +static Result NRI_CALL SwapChainPresent(SwapChain& swapChain) { - return ((SwapChainD3D11&)swapChain).Present(textureReadyForPresent); + return ((SwapChainD3D11&)swapChain).Present(); } static Result NRI_CALL SetSwapChainHdrMetadata(SwapChain& swapChain, const HdrMetadata& hdrMetadata) @@ -35,13 +33,4 @@ static Result NRI_CALL SetSwapChainHdrMetadata(SwapChain& swapChain, const HdrMe return ((SwapChainD3D11&)swapChain).SetHdrMetadata(hdrMetadata); } -void FillFunctionTableSwapChainD3D11(SwapChainInterface& table) -{ - table.SetSwapChainDebugName = ::SetSwapChainDebugName; - table.GetSwapChainTextures = ::GetSwapChainTextures; - table.AcquireNextSwapChainTexture = ::AcquireNextSwapChainTexture; - table.SwapChainPresent = ::SwapChainPresent; - table.SetSwapChainHdrMetadata = ::SetSwapChainHdrMetadata; -} - -#pragma endregion +Define_SwapChain_PartiallyFillFunctionTable(D3D11) diff --git a/Source/D3D11/TextureD3D11.cpp b/Source/D3D11/TextureD3D11.cpp index 627da590..3304867b 100644 --- a/Source/D3D11/TextureD3D11.cpp +++ b/Source/D3D11/TextureD3D11.cpp @@ -8,15 +8,13 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#include "SharedExternal.h" #include "SharedD3D11.h" #include "TextureD3D11.h" - #include "MemoryD3D11.h" using namespace nri; -static uint16_t GetMaxMipNum(uint16_t w, uint16_t h, uint16_t d) +static inline uint16_t GetMaxMipNum(uint16_t w, uint16_t h, uint16_t d) { uint16_t mipNum = 1; @@ -37,24 +35,15 @@ static uint16_t GetMaxMipNum(uint16_t w, uint16_t h, uint16_t d) return mipNum; } -TextureD3D11::TextureD3D11() : - m_Device(nullptr) -{ -} - TextureD3D11::TextureD3D11(DeviceD3D11& device, const TextureDesc& textureDesc) : - m_Desc(textureDesc), - m_Device(&device) + m_Device(device) + , m_Desc(textureDesc) { uint16_t mipNum = GetMaxMipNum(m_Desc.size[0], m_Desc.size[1], m_Desc.size[2]); m_Desc.mipNum = std::min(m_Desc.mipNum, mipNum); } -TextureD3D11::~TextureD3D11() -{ -} - -Result TextureD3D11::Create(const VersionedDevice& device, const MemoryD3D11* memory) +Result TextureD3D11::Create(const MemoryD3D11* memory) { HRESULT hr = E_INVALIDARG; const FormatInfo& formatInfo = GetFormatInfo(m_Desc.format); @@ -92,7 +81,7 @@ Result TextureD3D11::Create(const VersionedDevice& device, const MemoryD3D11* me desc.BindFlags = bindFlags; desc.CPUAccessFlags = cpuAccessFlags; - hr = device->CreateTexture1D(&desc, nullptr, (ID3D11Texture1D**)&m_Texture); + hr = m_Device.GetDevice()->CreateTexture1D(&desc, nullptr, (ID3D11Texture1D**)&m_Texture); } else if (m_Desc.type == TextureType::TEXTURE_3D) { @@ -107,7 +96,7 @@ Result TextureD3D11::Create(const VersionedDevice& device, const MemoryD3D11* me desc.BindFlags = bindFlags; desc.CPUAccessFlags = cpuAccessFlags; - hr = device->CreateTexture3D(&desc, nullptr, (ID3D11Texture3D**)&m_Texture); + hr = m_Device.GetDevice()->CreateTexture3D(&desc, nullptr, (ID3D11Texture3D**)&m_Texture); } else { @@ -126,10 +115,10 @@ Result TextureD3D11::Create(const VersionedDevice& device, const MemoryD3D11* me if (m_Desc.sampleNum == 1 && desc.Width == desc.Height && (m_Desc.arraySize % 6 == 0)) desc.MiscFlags = D3D11_RESOURCE_MISC_TEXTURECUBE; - hr = device->CreateTexture2D(&desc, nullptr, (ID3D11Texture2D**)&m_Texture); + hr = m_Device.GetDevice()->CreateTexture2D(&desc, nullptr, (ID3D11Texture2D**)&m_Texture); } - RETURN_ON_BAD_HRESULT(m_Device->GetLog(), hr, "Can't create texture!"); + RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "Can't create texture!"); uint64_t size = GetMipmappedSize(); uint32_t priority = memory ? memory->GetResidencyPriority(size) : 0; @@ -139,10 +128,8 @@ Result TextureD3D11::Create(const VersionedDevice& device, const MemoryD3D11* me return Result::SUCCESS; } -Result TextureD3D11::Create(DeviceD3D11& device, const TextureD3D11Desc& textureDesc) +Result TextureD3D11::Create(const TextureD3D11Desc& textureDesc) { - m_Device = &device; - ID3D11Resource* resource = (ID3D11Resource*)textureDesc.d3d11Resource; if (!resource) return Result::INVALID_ARGUMENT; @@ -274,10 +261,9 @@ uint32_t TextureD3D11::GetMipmappedSize(uint32_t w, uint32_t h, uint32_t d, uint return size; } -inline void TextureD3D11::SetDebugName(const char* name) -{ - SetName(m_Texture, name); -} +//================================================================================================================ +// NRI +//================================================================================================================ inline void TextureD3D11::GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const { @@ -299,7 +285,3 @@ inline void TextureD3D11::GetMemoryInfo(MemoryLocation memoryLocation, MemoryDes } #include "TextureD3D11.hpp" - -static_assert((uint32_t)nri::TextureType::TEXTURE_1D == 0u, "TextureD3D11::GetSize() depends on nri::TextureType"); -static_assert((uint32_t)nri::TextureType::TEXTURE_2D == 1u, "TextureD3D11::GetSize() depends on nri::TextureType"); -static_assert((uint32_t)nri::TextureType::TEXTURE_3D == 2u, "TextureD3D11::GetSize() depends on nri::TextureType"); \ No newline at end of file diff --git a/Source/D3D11/TextureD3D11.h b/Source/D3D11/TextureD3D11.h index 457ad4d4..38f1c46f 100644 --- a/Source/D3D11/TextureD3D11.h +++ b/Source/D3D11/TextureD3D11.h @@ -12,61 +12,71 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct DeviceD3D11; - struct MemoryD3D11; - struct TextureD3D11 - { - TextureD3D11(); - TextureD3D11(DeviceD3D11& device, const TextureDesc& textureDesc); - ~TextureD3D11(); +struct DeviceD3D11; +struct MemoryD3D11; + +struct TextureD3D11 +{ + inline TextureD3D11(DeviceD3D11& device) : + m_Device(device) + {} + + inline ~TextureD3D11() + {} + + inline DeviceD3D11& GetDevice() const + { return m_Device; } - inline DeviceD3D11& GetDevice() const - { return *m_Device; } + inline operator ID3D11Resource*() const + { return m_Texture; } - inline operator ID3D11Resource*() const - { return m_Texture; } + inline operator ID3D11Texture1D*() const + { return (ID3D11Texture1D*)m_Texture.GetInterface(); } - inline operator ID3D11Texture1D*() const - { return (ID3D11Texture1D*)m_Texture.GetInterface(); } + inline operator ID3D11Texture2D*() const + { return (ID3D11Texture2D*)m_Texture.GetInterface(); } - inline operator ID3D11Texture2D*() const - { return (ID3D11Texture2D*)m_Texture.GetInterface(); } + inline operator ID3D11Texture3D*() const + { return (ID3D11Texture3D*)m_Texture.GetInterface(); } + + inline const TextureDesc& GetDesc() const + { return m_Desc; } + + inline uint32_t GetSubresourceIndex(const TextureRegionDesc& regionDesc) const + { return regionDesc.mipOffset + regionDesc.arrayOffset * m_Desc.mipNum; } + + inline uint16_t GetSize(uint32_t dimension, uint32_t mipOffset = 0) const + { + assert(dimension < 3); - inline operator ID3D11Texture3D*() const - { return (ID3D11Texture3D*)m_Texture.GetInterface(); } + uint16_t size = m_Desc.size[dimension]; + size = (uint16_t)std::max(size >> mipOffset, 1); + size = Align(size, dimension < 2 ? (uint16_t)GetTexelBlockWidth(m_Desc.format) : 1); - inline const TextureDesc& GetDesc() const - { return m_Desc; } + return size; + } - inline uint32_t GetSubresourceIndex(const TextureRegionDesc& regionDesc) const - { return regionDesc.mipOffset + regionDesc.arrayOffset * m_Desc.mipNum; } + TextureD3D11(DeviceD3D11& device, const TextureDesc& textureDesc); - inline uint16_t GetSize(uint32_t dimension, uint32_t mipOffset = 0) const - { - assert(dimension < 3); + Result Create(const MemoryD3D11* memory); + Result Create(const TextureD3D11Desc& textureDesc); - uint16_t size = m_Desc.size[dimension]; - size = (uint16_t)std::max(size >> mipOffset, 1); - size = Align(size, dimension < 2 ? (uint16_t)GetTexelBlockWidth(m_Desc.format) : 1); + uint32_t GetMipmappedSize(uint32_t w = 0, uint32_t h = 0, uint32_t d = 0, uint32_t mipOffset = 0, uint32_t mipNum = 0) const; - return size; - } + //================================================================================================================ + // NRI + //================================================================================================================ - Result Create(const VersionedDevice& device, const MemoryD3D11* memory); - Result Create(DeviceD3D11& device, const TextureD3D11Desc& textureDesc); + inline void SetDebugName(const char* name) + { SET_D3D_DEBUG_OBJECT_NAME(m_Texture, name); } - uint32_t GetMipmappedSize(uint32_t w = 0, uint32_t h = 0, uint32_t d = 0, uint32_t mipOffset = 0, uint32_t mipNum = 0) const; + void GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const; - //====================================================================================================================== - // NRI - //====================================================================================================================== - void SetDebugName(const char* name); - void GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const; +private: + DeviceD3D11& m_Device; + ComPtr m_Texture; + TextureDesc m_Desc = {}; +}; - private: - ComPtr m_Texture; - TextureDesc m_Desc = {}; - DeviceD3D11* m_Device = nullptr; - }; } diff --git a/Source/D3D11/TextureD3D11.hpp b/Source/D3D11/TextureD3D11.hpp index b7c1cff9..62a7908a 100644 --- a/Source/D3D11/TextureD3D11.hpp +++ b/Source/D3D11/TextureD3D11.hpp @@ -8,7 +8,7 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ CoreInterface ] +#pragma region [ Core ] static void NRI_CALL SetTextureDebugName(Texture& texture, const char* name) { @@ -27,11 +27,6 @@ static void NRI_CALL GetTextureMemoryInfo(const Texture& texture, MemoryLocation ((TextureD3D11&)texture).GetMemoryInfo(memoryLocation, memoryDesc); } -void FillFunctionTableTextureD3D11(CoreInterface& coreInterface) -{ - coreInterface.SetTextureDebugName = ::SetTextureDebugName; - coreInterface.GetTextureNativeObject = ::GetTextureNativeObject; - coreInterface.GetTextureMemoryInfo = ::GetTextureMemoryInfo; -} - #pragma endregion + +Define_Core_Texture_PartiallyFillFunctionTable(D3D11) diff --git a/Source/D3D12/AccelerationStructureD3D12.cpp b/Source/D3D12/AccelerationStructureD3D12.cpp index 34134c3b..0bf6f65b 100644 --- a/Source/D3D12/AccelerationStructureD3D12.cpp +++ b/Source/D3D12/AccelerationStructureD3D12.cpp @@ -9,13 +9,8 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. */ #include "SharedD3D12.h" - #include "AccelerationStructureD3D12.h" -#include "DeviceD3D12.h" #include "BufferD3D12.h" -#include "MemoryD3D12.h" - -#ifdef __ID3D12GraphicsCommandList4_INTERFACE_DEFINED__ using namespace nri; @@ -106,16 +101,18 @@ uint64_t AccelerationStructureD3D12::GetHandle() const return m_Buffer->GetPointerGPU(); } -inline void AccelerationStructureD3D12::SetDebugName(const char* name) +AccelerationStructureD3D12::operator ID3D12Resource* () const { - m_Buffer->SetDebugName(name); + return (ID3D12Resource*)*m_Buffer; } -inline AccelerationStructureD3D12::operator ID3D12Resource* () const +//================================================================================================================ +// NRI +//================================================================================================================ + +inline void AccelerationStructureD3D12::SetDebugName(const char* name) { - return (ID3D12Resource*)*m_Buffer; + m_Buffer->SetDebugName(name); } #include "AccelerationStructureD3D12.hpp" - -#endif diff --git a/Source/D3D12/AccelerationStructureD3D12.h b/Source/D3D12/AccelerationStructureD3D12.h index a0cbefb8..72e89eeb 100644 --- a/Source/D3D12/AccelerationStructureD3D12.h +++ b/Source/D3D12/AccelerationStructureD3D12.h @@ -10,53 +10,47 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #pragma once -#ifdef __ID3D12GraphicsCommandList4_INTERFACE_DEFINED__ namespace nri { - struct DeviceD3D12; - struct BufferD3D12; - - struct AccelerationStructureD3D12 - { - AccelerationStructureD3D12(DeviceD3D12& device); - ~AccelerationStructureD3D12(); - - DeviceD3D12& GetDevice() const; - operator ID3D12Resource*() const; - operator BufferD3D12* () const; - - Result Create(const AccelerationStructureDesc& accelerationStructureDesc); - Result Create(const AccelerationStructureD3D12Desc& accelerationStructureDesc); - void GetMemoryInfo(MemoryDesc& memoryDesc) const; - uint64_t GetUpdateScratchBufferSize() const; - uint64_t GetBuildScratchBufferSize() const; - Result BindMemory(Memory* memory, uint64_t offset); - Result CreateDescriptor(Descriptor*& descriptor) const; - uint64_t GetHandle() const; - - //================================================================================================================ - // NRI - //================================================================================================================ - void SetDebugName(const char* name); - - private: - DeviceD3D12& m_Device; - D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO m_PrebuildInfo = {}; - BufferD3D12* m_Buffer = nullptr; - }; - - inline AccelerationStructureD3D12::AccelerationStructureD3D12(DeviceD3D12& device) + +struct DeviceD3D12; +struct BufferD3D12; + +struct AccelerationStructureD3D12 +{ + inline AccelerationStructureD3D12(DeviceD3D12& device) : m_Device(device) {} - inline DeviceD3D12& AccelerationStructureD3D12::GetDevice() const - { - return m_Device; - } + inline DeviceD3D12& GetDevice() const + { return m_Device; } + + inline operator BufferD3D12* () const + { return m_Buffer; } + + ~AccelerationStructureD3D12(); + + Result Create(const AccelerationStructureDesc& accelerationStructureDesc); + Result Create(const AccelerationStructureD3D12Desc& accelerationStructureDesc); + void GetMemoryInfo(MemoryDesc& memoryDesc) const; + uint64_t GetUpdateScratchBufferSize() const; + uint64_t GetBuildScratchBufferSize() const; + Result BindMemory(Memory* memory, uint64_t offset); + Result CreateDescriptor(Descriptor*& descriptor) const; + + uint64_t GetHandle() const; + operator ID3D12Resource* () const; + + //================================================================================================================ + // NRI + //================================================================================================================ + + void SetDebugName(const char* name); + +private: + DeviceD3D12& m_Device; + D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO m_PrebuildInfo = {}; + BufferD3D12* m_Buffer = nullptr; +}; - inline AccelerationStructureD3D12::operator BufferD3D12* () const - { - return m_Buffer; - } } -#endif diff --git a/Source/D3D12/AccelerationStructureD3D12.hpp b/Source/D3D12/AccelerationStructureD3D12.hpp index 1aa5f3a5..a81ce588 100644 --- a/Source/D3D12/AccelerationStructureD3D12.hpp +++ b/Source/D3D12/AccelerationStructureD3D12.hpp @@ -8,7 +8,7 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ RayTracingInterface ] +#pragma region [ RayTracing ] static Result NRI_CALL CreateAccelerationStructureDescriptor(const AccelerationStructure& accelerationStructure, uint32_t physicalDeviceIndex, Descriptor*& descriptor) { @@ -51,15 +51,6 @@ static uint64_t NRI_CALL GetAccelerationStructureNativeObject(const Acceleration return uint64_t((ID3D12Resource*)((AccelerationStructureD3D12&)accelerationStructure)); } -void FillFunctionTableAccelerationStructureD3D12(RayTracingInterface& rayTracingInterface) -{ - rayTracingInterface.CreateAccelerationStructureDescriptor = ::CreateAccelerationStructureDescriptor; - rayTracingInterface.GetAccelerationStructureMemoryInfo = ::GetAccelerationStructureMemoryInfo; - rayTracingInterface.GetAccelerationStructureUpdateScratchBufferSize = ::GetAccelerationStructureUpdateScratchBufferSize; - rayTracingInterface.GetAccelerationStructureBuildScratchBufferSize = ::GetAccelerationStructureBuildScratchBufferSize; - rayTracingInterface.GetAccelerationStructureHandle = ::GetAccelerationStructureHandle; - rayTracingInterface.SetAccelerationStructureDebugName = ::SetAccelerationStructureDebugName; - rayTracingInterface.GetAccelerationStructureNativeObject = ::GetAccelerationStructureNativeObject; -} - #pragma endregion + +Define_RayTracing_AccelerationStructure_PartiallyFillFunctionTable(D3D12) diff --git a/Source/D3D12/BufferD3D12.cpp b/Source/D3D12/BufferD3D12.cpp index c6353996..50bf2591 100644 --- a/Source/D3D12/BufferD3D12.cpp +++ b/Source/D3D12/BufferD3D12.cpp @@ -10,7 +10,6 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include "SharedD3D12.h" #include "BufferD3D12.h" -#include "DeviceD3D12.h" #include "MemoryD3D12.h" using namespace nri; @@ -57,10 +56,8 @@ Result BufferD3D12::BindMemory(const MemoryD3D12* memory, uint64_t offset, bool else if (heapDesc.Properties.Type == D3D12_HEAP_TYPE_READBACK) initialState |= D3D12_RESOURCE_STATE_COPY_DEST; -#ifdef __ID3D12GraphicsCommandList4_INTERFACE_DEFINED__ if (isAccelerationStructureBuffer) initialState |= D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE; -#endif if (memory->RequiresDedicatedAllocation()) { @@ -100,6 +97,10 @@ Result BufferD3D12::BindMemory(const MemoryD3D12* memory, uint64_t offset, bool return Result::SUCCESS; } +//================================================================================================================ +// NRI +//================================================================================================================ + inline void BufferD3D12::GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const { m_Device.GetMemoryInfo(memoryLocation, m_BufferDesc, memoryDesc); diff --git a/Source/D3D12/BufferD3D12.h b/Source/D3D12/BufferD3D12.h index 8052335c..27de308a 100644 --- a/Source/D3D12/BufferD3D12.h +++ b/Source/D3D12/BufferD3D12.h @@ -14,76 +14,55 @@ struct ID3D12Resource; namespace nri { - struct DeviceD3D12; - struct MemoryD3D12; - - struct BufferD3D12 - { - BufferD3D12(DeviceD3D12& device); - ~BufferD3D12(); - - operator ID3D12Resource*() const; - - DeviceD3D12& GetDevice() const; - - Result Create(const BufferDesc& bufferDesc); - Result Create(const BufferD3D12Desc& bufferDesc); - void Initialize(ID3D12Resource* resource); - Result BindMemory(const MemoryD3D12* memory, uint64_t offset, bool isAccelerationStructureBuffer = false); - - uint64_t GetByteSize() const; - uint32_t GetStructureStride() const; - D3D12_GPU_VIRTUAL_ADDRESS GetPointerGPU() const; - - //================================================================================================================ - // NRI - //================================================================================================================ - void SetDebugName(const char* name); - void GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const; - void* Map(uint64_t offset, uint64_t size); - void Unmap(); - - private: - DeviceD3D12& m_Device; - D3D12_RESOURCE_DESC m_BufferDesc = {}; - uint32_t m_StructureStride = 0; - ComPtr m_Buffer; - }; - - inline BufferD3D12::BufferD3D12(DeviceD3D12& device) + +struct DeviceD3D12; +struct MemoryD3D12; + +struct BufferD3D12 +{ + inline BufferD3D12(DeviceD3D12& device) : m_Device(device) {} - inline BufferD3D12::~BufferD3D12() + inline ~BufferD3D12() {} - inline BufferD3D12::operator ID3D12Resource*() const - { - return m_Buffer.GetInterface(); - } - - inline uint64_t BufferD3D12::GetByteSize() const - { - return m_BufferDesc.Width; - } - - inline uint32_t BufferD3D12::GetStructureStride() const - { - return m_StructureStride; - } - - inline D3D12_GPU_VIRTUAL_ADDRESS BufferD3D12::GetPointerGPU() const - { - return m_Buffer->GetGPUVirtualAddress(); - } - - inline DeviceD3D12& BufferD3D12::GetDevice() const - { - return m_Device; - } - - inline void BufferD3D12::SetDebugName(const char* name) - { - SET_D3D_DEBUG_OBJECT_NAME(m_Buffer, name); - } + inline operator ID3D12Resource*() const + { return m_Buffer.GetInterface(); } + + inline uint64_t GetByteSize() const + { return m_BufferDesc.Width; } + + inline uint32_t GetStructureStride() const + { return m_StructureStride; } + + inline D3D12_GPU_VIRTUAL_ADDRESS GetPointerGPU() const + { return m_Buffer->GetGPUVirtualAddress(); } + + inline DeviceD3D12& GetDevice() const + { return m_Device; } + + Result Create(const BufferDesc& bufferDesc); + Result Create(const BufferD3D12Desc& bufferDesc); + void Initialize(ID3D12Resource* resource); + Result BindMemory(const MemoryD3D12* memory, uint64_t offset, bool isAccelerationStructureBuffer = false); + + //================================================================================================================ + // NRI + //================================================================================================================ + + inline void SetDebugName(const char* name) + { SET_D3D_DEBUG_OBJECT_NAME(m_Buffer, name); } + + void GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const; + void* Map(uint64_t offset, uint64_t size); + void Unmap(); + +private: + DeviceD3D12& m_Device; + D3D12_RESOURCE_DESC m_BufferDesc = {}; + uint32_t m_StructureStride = 0; + ComPtr m_Buffer; +}; + } diff --git a/Source/D3D12/BufferD3D12.hpp b/Source/D3D12/BufferD3D12.hpp index 5df9acd0..df8d5300 100644 --- a/Source/D3D12/BufferD3D12.hpp +++ b/Source/D3D12/BufferD3D12.hpp @@ -8,7 +8,7 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ CoreInterface ] +#pragma region [ Core ] static void NRI_CALL SetBufferDebugName(Buffer& buffer, const char* name) { @@ -37,13 +37,6 @@ static void NRI_CALL UnmapBuffer(Buffer& buffer) ((BufferD3D12&)buffer).Unmap(); } -void FillFunctionTableBufferD3D12(CoreInterface& coreInterface) -{ - coreInterface.SetBufferDebugName = ::SetBufferDebugName; - coreInterface.GetBufferNativeObject = ::GetBufferNativeObject; - coreInterface.GetBufferMemoryInfo = ::GetBufferMemoryInfo; - coreInterface.MapBuffer = ::MapBuffer; - coreInterface.UnmapBuffer = ::UnmapBuffer; -} - #pragma endregion + +Define_Core_Buffer_PartiallyFillFunctionTable(D3D12) diff --git a/Source/D3D12/CommandAllocatorD3D12.cpp b/Source/D3D12/CommandAllocatorD3D12.cpp index bea6ebc5..b7fb3019 100644 --- a/Source/D3D12/CommandAllocatorD3D12.cpp +++ b/Source/D3D12/CommandAllocatorD3D12.cpp @@ -10,7 +10,6 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include "SharedD3D12.h" #include "CommandAllocatorD3D12.h" -#include "DeviceD3D12.h" #include "CommandQueueD3D12.h" #include "CommandBufferD3D12.h" @@ -30,10 +29,9 @@ Result CommandAllocatorD3D12::Create(const CommandQueue& commandQueue) return Result::SUCCESS; } -inline void CommandAllocatorD3D12::SetDebugName(const char* name) -{ - SET_D3D_DEBUG_OBJECT_NAME(m_CommandAllocator, name); -} +//================================================================================================================ +// NRI +//================================================================================================================ inline Result CommandAllocatorD3D12::CreateCommandBuffer(CommandBuffer*& commandBuffer) { diff --git a/Source/D3D12/CommandAllocatorD3D12.h b/Source/D3D12/CommandAllocatorD3D12.h index 16dd9fef..5fd4d072 100644 --- a/Source/D3D12/CommandAllocatorD3D12.h +++ b/Source/D3D12/CommandAllocatorD3D12.h @@ -15,47 +15,40 @@ enum D3D12_COMMAND_LIST_TYPE; namespace nri { - struct DeviceD3D12; - struct CommandAllocatorD3D12 - { - CommandAllocatorD3D12(DeviceD3D12& device); - ~CommandAllocatorD3D12(); +struct DeviceD3D12; - operator ID3D12CommandAllocator*() const; +struct CommandAllocatorD3D12 +{ + inline CommandAllocatorD3D12(DeviceD3D12& device) + : m_Device(device) + {} - DeviceD3D12& GetDevice() const; + inline ~CommandAllocatorD3D12() + {} - Result Create(const CommandQueue& commandQueue); + inline operator ID3D12CommandAllocator*() const + { return m_CommandAllocator.GetInterface(); } - //================================================================================================================ - // NRI - //================================================================================================================ - void SetDebugName(const char* name); + inline DeviceD3D12& GetDevice() const + { return m_Device; } - Result CreateCommandBuffer(CommandBuffer*& commandBuffer); - void Reset(); + Result Create(const CommandQueue& commandQueue); - private: - DeviceD3D12& m_Device; - ComPtr m_CommandAllocator; - D3D12_COMMAND_LIST_TYPE m_CommandListType = D3D12_COMMAND_LIST_TYPE(-1); - }; + //================================================================================================================ + // NRI + //================================================================================================================ - inline CommandAllocatorD3D12::CommandAllocatorD3D12(DeviceD3D12& device) - : m_Device(device) - {} + inline void SetDebugName(const char* name) + { SET_D3D_DEBUG_OBJECT_NAME(m_CommandAllocator, name); } - inline CommandAllocatorD3D12::~CommandAllocatorD3D12() - {} + Result CreateCommandBuffer(CommandBuffer*& commandBuffer); + void Reset(); - inline CommandAllocatorD3D12::operator ID3D12CommandAllocator*() const - { - return m_CommandAllocator.GetInterface(); - } +private: + DeviceD3D12& m_Device; + ComPtr m_CommandAllocator; + D3D12_COMMAND_LIST_TYPE m_CommandListType = D3D12_COMMAND_LIST_TYPE(-1); +}; - inline DeviceD3D12& CommandAllocatorD3D12::GetDevice() const - { - return m_Device; - } } diff --git a/Source/D3D12/CommandAllocatorD3D12.hpp b/Source/D3D12/CommandAllocatorD3D12.hpp index 26e1c1ec..cb0298d5 100644 --- a/Source/D3D12/CommandAllocatorD3D12.hpp +++ b/Source/D3D12/CommandAllocatorD3D12.hpp @@ -8,7 +8,7 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ CoreInterface ] +#pragma region [ Core ] static void NRI_CALL SetCommandAllocatorDebugName(CommandAllocator& commandAllocator, const char* name) { @@ -25,11 +25,6 @@ static void NRI_CALL ResetCommandAllocator(CommandAllocator& commandAllocator) ((CommandAllocatorD3D12&)commandAllocator).Reset(); } -void FillFunctionTableCommandAllocatorD3D12(CoreInterface& coreInterface) -{ - coreInterface.SetCommandAllocatorDebugName = ::SetCommandAllocatorDebugName; - coreInterface.CreateCommandBuffer = ::CreateCommandBuffer; - coreInterface.ResetCommandAllocator = ::ResetCommandAllocator; -} - #pragma endregion + +Define_Core_CommandAllocator_PartiallyFillFunctionTable(D3D12) diff --git a/Source/D3D12/CommandBufferD3D12.cpp b/Source/D3D12/CommandBufferD3D12.cpp index d1a5d0e6..4223c348 100644 --- a/Source/D3D12/CommandBufferD3D12.cpp +++ b/Source/D3D12/CommandBufferD3D12.cpp @@ -10,9 +10,6 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include "SharedD3D12.h" #include "CommandBufferD3D12.h" -#include "DeviceD3D12.h" -#include "CommandQueueD3D12.h" -#include "CommandAllocatorD3D12.h" #include "BufferD3D12.h" #include "TextureD3D12.h" #include "DescriptorD3D12.h" @@ -22,18 +19,16 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include "PipelineLayoutD3D12.h" #include "QueryPoolD3D12.h" #include "AccelerationStructureD3D12.h" + #include using namespace nri; -extern D3D12_RESOURCE_STATES GetResourceStates(nri::AccessBits accessMask); +extern D3D12_RESOURCE_STATES GetResourceStates(AccessBits accessMask, D3D12_COMMAND_LIST_TYPE commandListType); extern void ConvertRects(D3D12_RECT* rectsD3D12, const Rect* rects, uint32_t rectNum); - -#ifdef __ID3D12GraphicsCommandList4_INTERFACE_DEFINED__ extern D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAGS GetAccelerationStructureBuildFlags(AccelerationStructureBuildBits accelerationStructureBuildFlags); extern void ConvertGeometryDescs(D3D12_RAYTRACING_GEOMETRY_DESC* geometryDescs, const GeometryObject* geometryObjects, uint32_t geometryObjectNum); extern D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE GetCopyMode(CopyMode copyMode); -#endif Result CommandBufferD3D12::Create(D3D12_COMMAND_LIST_TYPE commandListType, ID3D12CommandAllocator* commandAllocator) { @@ -48,14 +43,8 @@ Result CommandBufferD3D12::Create(D3D12_COMMAND_LIST_TYPE commandListType, ID3D1 m_CommandAllocator = commandAllocator; m_GraphicsCommandList = graphicsCommandList; m_GraphicsCommandList->QueryInterface(IID_PPV_ARGS(&m_GraphicsCommandList1)); - -#ifdef __ID3D12GraphicsCommandList4_INTERFACE_DEFINED__ m_GraphicsCommandList->QueryInterface(IID_PPV_ARGS(&m_GraphicsCommandList4)); -#endif - -#ifdef __ID3D12GraphicsCommandList6_INTERFACE_DEFINED__ m_GraphicsCommandList->QueryInterface(IID_PPV_ARGS(&m_GraphicsCommandList6)); -#endif hr = m_GraphicsCommandList->Close(); if (FAILED(hr)) @@ -72,23 +61,37 @@ Result CommandBufferD3D12::Create(const CommandBufferD3D12Desc& commandBufferDes m_CommandAllocator = (ID3D12CommandAllocator*)commandBufferDesc.d3d12CommandAllocator; m_GraphicsCommandList = (ID3D12GraphicsCommandList*)commandBufferDesc.d3d12CommandList; m_GraphicsCommandList->QueryInterface(IID_PPV_ARGS(&m_GraphicsCommandList1)); - -#ifdef __ID3D12GraphicsCommandList4_INTERFACE_DEFINED__ m_GraphicsCommandList->QueryInterface(IID_PPV_ARGS(&m_GraphicsCommandList4)); -#endif - -#ifdef __ID3D12GraphicsCommandList6_INTERFACE_DEFINED__ m_GraphicsCommandList->QueryInterface(IID_PPV_ARGS(&m_GraphicsCommandList6)); -#endif return Result::SUCCESS; } -inline void CommandBufferD3D12::SetDebugName(const char* name) +inline void CommandBufferD3D12::AddResourceBarrier(ID3D12Resource* resource, AccessBits before, AccessBits after, D3D12_RESOURCE_BARRIER& resourceBarrier, uint32_t subresource) { - SET_D3D_DEBUG_OBJECT_NAME(m_GraphicsCommandList, name); + D3D12_COMMAND_LIST_TYPE commandListType = m_GraphicsCommandList->GetType(); + D3D12_RESOURCE_STATES resourceStateBefore = GetResourceStates(before, commandListType); + D3D12_RESOURCE_STATES resourceStateAfter = GetResourceStates(after, commandListType); + + if (resourceStateBefore == resourceStateAfter && resourceStateBefore == D3D12_RESOURCE_STATE_UNORDERED_ACCESS) + { + resourceBarrier.Type = D3D12_RESOURCE_BARRIER_TYPE_UAV; + resourceBarrier.UAV.pResource = resource; + } + else + { + resourceBarrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; + resourceBarrier.Transition.pResource = resource; + resourceBarrier.Transition.StateBefore = resourceStateBefore; + resourceBarrier.Transition.StateAfter = resourceStateAfter; + resourceBarrier.Transition.Subresource = subresource; + } } +//================================================================================================================ +// NRI +//================================================================================================================ + inline Result CommandBufferD3D12::Begin(const DescriptorPool* descriptorPool) { HRESULT hr = m_GraphicsCommandList->Reset(m_CommandAllocator, nullptr); @@ -566,10 +569,9 @@ inline void CommandBufferD3D12::EndAnnotation() PIXEndEvent(m_GraphicsCommandList); } -void CommandBufferD3D12::BuildTopLevelAccelerationStructure(uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, +inline void CommandBufferD3D12::BuildTopLevelAccelerationStructure(uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, AccelerationStructureBuildBits flags, AccelerationStructure& dst, Buffer& scratch, uint64_t scratchOffset) { -#ifdef __ID3D12GraphicsCommandList4_INTERFACE_DEFINED__ D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC desc = {}; desc.DestAccelerationStructureData = ((AccelerationStructureD3D12&)dst).GetHandle(); desc.ScratchAccelerationStructureData = ((BufferD3D12&)scratch).GetPointerGPU() + scratchOffset; @@ -581,13 +583,11 @@ void CommandBufferD3D12::BuildTopLevelAccelerationStructure(uint32_t instanceNum desc.Inputs.InstanceDescs = ((BufferD3D12&)buffer).GetPointerGPU() + bufferOffset; m_GraphicsCommandList4->BuildRaytracingAccelerationStructure(&desc, 0, nullptr); -#endif } -void CommandBufferD3D12::BuildBottomLevelAccelerationStructure(uint32_t geometryObjectNum, const GeometryObject* geometryObjects, +inline void CommandBufferD3D12::BuildBottomLevelAccelerationStructure(uint32_t geometryObjectNum, const GeometryObject* geometryObjects, AccelerationStructureBuildBits flags, AccelerationStructure& dst, Buffer& scratch, uint64_t scratchOffset) { -#ifdef __ID3D12GraphicsCommandList4_INTERFACE_DEFINED__ D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC desc = {}; desc.DestAccelerationStructureData = ((AccelerationStructureD3D12&)dst).GetHandle(); desc.ScratchAccelerationStructureData = ((BufferD3D12&)scratch).GetPointerGPU() + scratchOffset; @@ -601,13 +601,11 @@ void CommandBufferD3D12::BuildBottomLevelAccelerationStructure(uint32_t geometry desc.Inputs.pGeometryDescs = &geometryDescs[0]; m_GraphicsCommandList4->BuildRaytracingAccelerationStructure(&desc, 0, nullptr); -#endif } -void CommandBufferD3D12::UpdateTopLevelAccelerationStructure(uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, +inline void CommandBufferD3D12::UpdateTopLevelAccelerationStructure(uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, AccelerationStructureBuildBits flags, AccelerationStructure& dst, AccelerationStructure& src, Buffer& scratch, uint64_t scratchOffset) { -#ifdef __ID3D12GraphicsCommandList4_INTERFACE_DEFINED__ D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC desc = {}; desc.DestAccelerationStructureData = ((AccelerationStructureD3D12&)dst).GetHandle(); desc.SourceAccelerationStructureData = ((AccelerationStructureD3D12&)src).GetHandle(); @@ -618,13 +616,11 @@ void CommandBufferD3D12::UpdateTopLevelAccelerationStructure(uint32_t instanceNu desc.Inputs.InstanceDescs = ((BufferD3D12&)buffer).GetPointerGPU() + bufferOffset; m_GraphicsCommandList4->BuildRaytracingAccelerationStructure(&desc, 0, nullptr); -#endif } -void CommandBufferD3D12::UpdateBottomLevelAccelerationStructure(uint32_t geometryObjectNum, const GeometryObject* geometryObjects, +inline void CommandBufferD3D12::UpdateBottomLevelAccelerationStructure(uint32_t geometryObjectNum, const GeometryObject* geometryObjects, AccelerationStructureBuildBits flags, AccelerationStructure& dst, AccelerationStructure& src, Buffer& scratch, uint64_t scratchOffset) { -#ifdef __ID3D12GraphicsCommandList4_INTERFACE_DEFINED__ D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC desc = {}; desc.DestAccelerationStructureData = ((AccelerationStructureD3D12&)dst).GetHandle(); desc.SourceAccelerationStructureData = ((AccelerationStructureD3D12&)src).GetHandle(); @@ -639,22 +635,18 @@ void CommandBufferD3D12::UpdateBottomLevelAccelerationStructure(uint32_t geometr desc.Inputs.pGeometryDescs = &geometryDescs[0]; m_GraphicsCommandList4->BuildRaytracingAccelerationStructure(&desc, 0, nullptr); -#endif } -void CommandBufferD3D12::CopyAccelerationStructure(AccelerationStructure& dst, AccelerationStructure& src, CopyMode copyMode) +inline void CommandBufferD3D12::CopyAccelerationStructure(AccelerationStructure& dst, AccelerationStructure& src, CopyMode copyMode) { -#ifdef __ID3D12GraphicsCommandList4_INTERFACE_DEFINED__ m_GraphicsCommandList4->CopyRaytracingAccelerationStructure(((AccelerationStructureD3D12&)dst).GetHandle(), ((AccelerationStructureD3D12&)src).GetHandle(), GetCopyMode(copyMode)); -#endif } -void CommandBufferD3D12::WriteAccelerationStructureSize(const AccelerationStructure* const* accelerationStructures, uint32_t accelerationStructureNum, QueryPool& queryPool, uint32_t queryOffset) +inline void CommandBufferD3D12::WriteAccelerationStructureSize(const AccelerationStructure* const* accelerationStructures, uint32_t accelerationStructureNum, QueryPool& queryPool, uint32_t queryOffset) { MaybeUnused(accelerationStructures); MaybeUnused(queryOffset); -#ifdef __ID3D12GraphicsCommandList4_INTERFACE_DEFINED__ D3D12_GPU_VIRTUAL_ADDRESS* virtualAddresses = ALLOCATE_SCRATCH(m_Device, D3D12_GPU_VIRTUAL_ADDRESS, accelerationStructureNum); QueryPoolD3D12& queryPoolD3D12 = (QueryPoolD3D12&)queryPool; @@ -666,12 +658,10 @@ void CommandBufferD3D12::WriteAccelerationStructureSize(const AccelerationStruct m_GraphicsCommandList4->EmitRaytracingAccelerationStructurePostbuildInfo(&postbuildInfo, accelerationStructureNum, virtualAddresses); FREE_SCRATCH(m_Device, virtualAddresses, accelerationStructureNum); -#endif } -void CommandBufferD3D12::DispatchRays(const DispatchRaysDesc& dispatchRaysDesc) +inline void CommandBufferD3D12::DispatchRays(const DispatchRaysDesc& dispatchRaysDesc) { -#ifdef __ID3D12GraphicsCommandList4_INTERFACE_DEFINED__ D3D12_DISPATCH_RAYS_DESC desc = {}; desc.RayGenerationShaderRecord.StartAddress = (*(BufferD3D12*)dispatchRaysDesc.raygenShader.buffer).GetPointerGPU() + dispatchRaysDesc.raygenShader.offset; @@ -703,34 +693,11 @@ void CommandBufferD3D12::DispatchRays(const DispatchRaysDesc& dispatchRaysDesc) desc.Depth = dispatchRaysDesc.depth; m_GraphicsCommandList4->DispatchRays(&desc); -#endif -} - -inline void CommandBufferD3D12::DispatchMeshTasks(uint32_t taskNum) -{ -#ifdef __ID3D12GraphicsCommandList6_INTERFACE_DEFINED__ - m_GraphicsCommandList6->DispatchMesh(taskNum, 1, 1); -#endif } -inline void CommandBufferD3D12::AddResourceBarrier(ID3D12Resource* resource, AccessBits before, AccessBits after, D3D12_RESOURCE_BARRIER& resourceBarrier, uint32_t subresource) +inline void CommandBufferD3D12::DispatchMeshTasks(uint32_t x, uint32_t y, uint32_t z) { - D3D12_RESOURCE_STATES resourceStateBefore = GetResourceStates(before); - D3D12_RESOURCE_STATES resourceStateAfter = GetResourceStates(after); - - if (resourceStateBefore == resourceStateAfter && resourceStateBefore == D3D12_RESOURCE_STATE_UNORDERED_ACCESS) - { - resourceBarrier.Type = D3D12_RESOURCE_BARRIER_TYPE_UAV; - resourceBarrier.UAV.pResource = resource; - } - else - { - resourceBarrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; - resourceBarrier.Transition.pResource = resource; - resourceBarrier.Transition.StateBefore = resourceStateBefore; - resourceBarrier.Transition.StateAfter = resourceStateAfter; - resourceBarrier.Transition.Subresource = subresource; - } + m_GraphicsCommandList6->DispatchMesh(x, y, z); } #include "CommandBufferD3D12.hpp" diff --git a/Source/D3D12/CommandBufferD3D12.h b/Source/D3D12/CommandBufferD3D12.h index 583312dd..103e715a 100644 --- a/Source/D3D12/CommandBufferD3D12.h +++ b/Source/D3D12/CommandBufferD3D12.h @@ -17,115 +17,105 @@ struct ID3D12Resource; namespace nri { - struct DeviceD3D12; - struct PipelineD3D12; - struct PipelineLayoutD3D12; - struct DescriptorPoolD3D12; - struct FrameBufferD3D12; - struct DescriptorSetD3D12; - - struct CommandBufferD3D12 - { - CommandBufferD3D12(DeviceD3D12& device); - ~CommandBufferD3D12(); - - operator ID3D12GraphicsCommandList*() const; - - DeviceD3D12& GetDevice() const; - - Result Create(D3D12_COMMAND_LIST_TYPE commandListType, ID3D12CommandAllocator* commandAllocator); - Result Create(const CommandBufferD3D12Desc& commandBufferDesc); - - //================================================================================================================ - // NRI - //================================================================================================================ - void SetDebugName(const char* name); - - Result Begin(const DescriptorPool* descriptorPool); - Result End(); - void SetViewports(const Viewport* viewports, uint32_t viewportNum); - void SetScissors(const Rect* rects, uint32_t rectNum); - void SetDepthBounds(float boundsMin, float boundsMax); - void SetStencilReference(uint8_t reference); - void SetSamplePositions(const SamplePosition* positions, uint32_t positionNum); - void ClearAttachments(const ClearDesc* clearDescs, uint32_t clearDescNum, const Rect* rects, uint32_t rectNum); - void ClearStorageBuffer(const ClearStorageBufferDesc& clearDesc); - void ClearStorageTexture(const ClearStorageTextureDesc& clearDesc); - void BeginRenderPass(const FrameBuffer& frameBuffer, RenderPassBeginFlag renderPassBeginFlag); - void EndRenderPass(); - void SetVertexBuffers(uint32_t baseSlot, uint32_t bufferNum, const Buffer* const* buffers, const uint64_t* offsets); - void SetIndexBuffer(const Buffer& buffer, uint64_t offset, IndexType indexType); - void SetPipelineLayout(const PipelineLayout& pipelineLayout); - void SetPipeline(const Pipeline& pipeline); - void SetDescriptorPool(const DescriptorPool& descriptorPool); - void SetDescriptorSet(uint32_t setIndexInPipelineLayout, const DescriptorSet& descriptorSet, const uint32_t* dynamicConstantBufferOffsets); - void SetConstants(uint32_t pushConstantRangeIndex, const void* data, uint32_t size); - void Draw(uint32_t vertexNum, uint32_t instanceNum, uint32_t baseVertex, uint32_t baseInstance); - void DrawIndexed(uint32_t indexNum, uint32_t instanceNum, uint32_t baseIndex, uint32_t baseVertex, uint32_t baseInstance); - void DrawIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride); - void DrawIndexedIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride); - void CopyBuffer(Buffer& dstBuffer, uint64_t dstOffset, const Buffer& srcBuffer, uint64_t srcOffset, uint64_t size); - void CopyTexture(Texture& dstTexture, const TextureRegionDesc* dstRegionDesc, const Texture& srcTexture, const TextureRegionDesc* srcRegionDesc); - void UploadBufferToTexture(Texture& dstTexture, const TextureRegionDesc& dstRegionDesc, const Buffer& srcBuffer, const TextureDataLayoutDesc& srcDataLayoutDesc); - void ReadbackTextureToBuffer(Buffer& dstBuffer, TextureDataLayoutDesc& dstDataLayoutDesc, const Texture& srcTexture, const TextureRegionDesc& srcRegionDesc); - void Dispatch(uint32_t x, uint32_t y, uint32_t z); - void DispatchIndirect(const Buffer& buffer, uint64_t offset); - void PipelineBarrier(const TransitionBarrierDesc* transitionBarriers, const AliasingBarrierDesc* aliasingBarriers, BarrierDependency dependency); - void BeginQuery(const QueryPool& queryPool, uint32_t offset); - void EndQuery(const QueryPool& queryPool, uint32_t offset); - void CopyQueries(const QueryPool& queryPool, uint32_t offset, uint32_t num, Buffer& buffer, uint64_t alignedBufferOffset); - void BeginAnnotation(const char* name); - void EndAnnotation(); - - void BuildTopLevelAccelerationStructure(uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, - AccelerationStructureBuildBits flags, AccelerationStructure& dst, Buffer& scratch, uint64_t scratchOffset); - void BuildBottomLevelAccelerationStructure(uint32_t geometryObjectNum, const GeometryObject* geometryObjects, - AccelerationStructureBuildBits flags, AccelerationStructure& dst, Buffer& scratch, uint64_t scratchOffset); - void UpdateTopLevelAccelerationStructure(uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, - AccelerationStructureBuildBits flags, AccelerationStructure& dst, AccelerationStructure& src, Buffer& scratch, uint64_t scratchOffset); - void UpdateBottomLevelAccelerationStructure(uint32_t geometryObjectNum, const GeometryObject* geometryObjects, - AccelerationStructureBuildBits flags, AccelerationStructure& dst, AccelerationStructure& src, Buffer& scratch, uint64_t scratchOffset); - void CopyAccelerationStructure(AccelerationStructure& dst, AccelerationStructure& src, CopyMode copyMode); - void WriteAccelerationStructureSize(const AccelerationStructure* const* accelerationStructures, uint32_t accelerationStructureNum, QueryPool& queryPool, uint32_t queryOffset); - void DispatchRays(const DispatchRaysDesc& dispatchRaysDesc); - - void DispatchMeshTasks(uint32_t taskNum); - - private: - static void AddResourceBarrier(ID3D12Resource* resource, AccessBits before, AccessBits after, D3D12_RESOURCE_BARRIER& resourceBarrier, uint32_t subresource); - - DeviceD3D12& m_Device; - ComPtr m_CommandAllocator; - ComPtr m_GraphicsCommandList; - ComPtr m_GraphicsCommandList1; -#ifdef __ID3D12GraphicsCommandList4_INTERFACE_DEFINED__ - ComPtr m_GraphicsCommandList4; -#endif -#ifdef __ID3D12GraphicsCommandList6_INTERFACE_DEFINED__ - ComPtr m_GraphicsCommandList6; -#endif - const PipelineLayoutD3D12* m_PipelineLayout = nullptr; - bool m_IsGraphicsPipelineLayout = false; - PipelineD3D12* m_Pipeline = nullptr; - FrameBufferD3D12* m_FrameBuffer = nullptr; - D3D12_PRIMITIVE_TOPOLOGY m_PrimitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED; - std::array m_DescriptorSets = {}; - }; - - inline CommandBufferD3D12::CommandBufferD3D12(DeviceD3D12& device) + +struct DeviceD3D12; +struct PipelineD3D12; +struct PipelineLayoutD3D12; +struct DescriptorPoolD3D12; +struct FrameBufferD3D12; +struct DescriptorSetD3D12; + +struct CommandBufferD3D12 +{ + inline CommandBufferD3D12(DeviceD3D12& device) : m_Device(device) {} - inline CommandBufferD3D12::~CommandBufferD3D12() + inline ~CommandBufferD3D12() {} - inline CommandBufferD3D12::operator ID3D12GraphicsCommandList*() const - { - return m_GraphicsCommandList.GetInterface(); - } + inline operator ID3D12GraphicsCommandList*() const + { return m_GraphicsCommandList.GetInterface(); } + + inline DeviceD3D12& GetDevice() const + { return m_Device; } + + Result Create(D3D12_COMMAND_LIST_TYPE commandListType, ID3D12CommandAllocator* commandAllocator); + Result Create(const CommandBufferD3D12Desc& commandBufferDesc); + + //================================================================================================================ + // NRI + //================================================================================================================ + + inline void SetDebugName(const char* name) + { SET_D3D_DEBUG_OBJECT_NAME(m_GraphicsCommandList, name); } + + Result Begin(const DescriptorPool* descriptorPool); + Result End(); + void SetViewports(const Viewport* viewports, uint32_t viewportNum); + void SetScissors(const Rect* rects, uint32_t rectNum); + void SetDepthBounds(float boundsMin, float boundsMax); + void SetStencilReference(uint8_t reference); + void SetSamplePositions(const SamplePosition* positions, uint32_t positionNum); + void ClearAttachments(const ClearDesc* clearDescs, uint32_t clearDescNum, const Rect* rects, uint32_t rectNum); + void ClearStorageBuffer(const ClearStorageBufferDesc& clearDesc); + void ClearStorageTexture(const ClearStorageTextureDesc& clearDesc); + void BeginRenderPass(const FrameBuffer& frameBuffer, RenderPassBeginFlag renderPassBeginFlag); + void EndRenderPass(); + void SetVertexBuffers(uint32_t baseSlot, uint32_t bufferNum, const Buffer* const* buffers, const uint64_t* offsets); + void SetIndexBuffer(const Buffer& buffer, uint64_t offset, IndexType indexType); + void SetPipelineLayout(const PipelineLayout& pipelineLayout); + void SetPipeline(const Pipeline& pipeline); + void SetDescriptorPool(const DescriptorPool& descriptorPool); + void SetDescriptorSet(uint32_t setIndexInPipelineLayout, const DescriptorSet& descriptorSet, const uint32_t* dynamicConstantBufferOffsets); + void SetConstants(uint32_t pushConstantRangeIndex, const void* data, uint32_t size); + void Draw(uint32_t vertexNum, uint32_t instanceNum, uint32_t baseVertex, uint32_t baseInstance); + void DrawIndexed(uint32_t indexNum, uint32_t instanceNum, uint32_t baseIndex, uint32_t baseVertex, uint32_t baseInstance); + void DrawIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride); + void DrawIndexedIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride); + void CopyBuffer(Buffer& dstBuffer, uint64_t dstOffset, const Buffer& srcBuffer, uint64_t srcOffset, uint64_t size); + void CopyTexture(Texture& dstTexture, const TextureRegionDesc* dstRegionDesc, const Texture& srcTexture, const TextureRegionDesc* srcRegionDesc); + void UploadBufferToTexture(Texture& dstTexture, const TextureRegionDesc& dstRegionDesc, const Buffer& srcBuffer, const TextureDataLayoutDesc& srcDataLayoutDesc); + void ReadbackTextureToBuffer(Buffer& dstBuffer, TextureDataLayoutDesc& dstDataLayoutDesc, const Texture& srcTexture, const TextureRegionDesc& srcRegionDesc); + void Dispatch(uint32_t x, uint32_t y, uint32_t z); + void DispatchIndirect(const Buffer& buffer, uint64_t offset); + void PipelineBarrier(const TransitionBarrierDesc* transitionBarriers, const AliasingBarrierDesc* aliasingBarriers, BarrierDependency dependency); + void BeginQuery(const QueryPool& queryPool, uint32_t offset); + void EndQuery(const QueryPool& queryPool, uint32_t offset); + void CopyQueries(const QueryPool& queryPool, uint32_t offset, uint32_t num, Buffer& buffer, uint64_t alignedBufferOffset); + void BeginAnnotation(const char* name); + void EndAnnotation(); + + void BuildTopLevelAccelerationStructure(uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, + AccelerationStructureBuildBits flags, AccelerationStructure& dst, Buffer& scratch, uint64_t scratchOffset); + void BuildBottomLevelAccelerationStructure(uint32_t geometryObjectNum, const GeometryObject* geometryObjects, + AccelerationStructureBuildBits flags, AccelerationStructure& dst, Buffer& scratch, uint64_t scratchOffset); + void UpdateTopLevelAccelerationStructure(uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, + AccelerationStructureBuildBits flags, AccelerationStructure& dst, AccelerationStructure& src, Buffer& scratch, uint64_t scratchOffset); + void UpdateBottomLevelAccelerationStructure(uint32_t geometryObjectNum, const GeometryObject* geometryObjects, + AccelerationStructureBuildBits flags, AccelerationStructure& dst, AccelerationStructure& src, Buffer& scratch, uint64_t scratchOffset); + void CopyAccelerationStructure(AccelerationStructure& dst, AccelerationStructure& src, CopyMode copyMode); + void WriteAccelerationStructureSize(const AccelerationStructure* const* accelerationStructures, uint32_t accelerationStructureNum, QueryPool& queryPool, uint32_t queryOffset); + void DispatchRays(const DispatchRaysDesc& dispatchRaysDesc); + + void DispatchMeshTasks(uint32_t x, uint32_t y, uint32_t z); + +private: + void AddResourceBarrier(ID3D12Resource* resource, AccessBits before, AccessBits after, D3D12_RESOURCE_BARRIER& resourceBarrier, uint32_t subresource); + +private: + DeviceD3D12& m_Device; + ComPtr m_CommandAllocator; + ComPtr m_GraphicsCommandList; + ComPtr m_GraphicsCommandList1; + ComPtr m_GraphicsCommandList4; + ComPtr m_GraphicsCommandList6; + const PipelineLayoutD3D12* m_PipelineLayout = nullptr; + bool m_IsGraphicsPipelineLayout = false; + PipelineD3D12* m_Pipeline = nullptr; + FrameBufferD3D12* m_FrameBuffer = nullptr; + D3D12_PRIMITIVE_TOPOLOGY m_PrimitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED; + std::array m_DescriptorSets = {}; +}; - inline DeviceD3D12& CommandBufferD3D12::GetDevice() const - { - return m_Device; - } } diff --git a/Source/D3D12/CommandBufferD3D12.hpp b/Source/D3D12/CommandBufferD3D12.hpp index 56f61268..f5671358 100644 --- a/Source/D3D12/CommandBufferD3D12.hpp +++ b/Source/D3D12/CommandBufferD3D12.hpp @@ -8,7 +8,7 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ CoreInterface ] +#pragma region [ Core ] static void NRI_CALL SetCommandBufferDebugName(CommandBuffer& commandBuffer, const char* name) { @@ -215,61 +215,9 @@ static void* NRI_CALL GetCommandBufferNativeObject(const CommandBuffer& commandB return (CommandBufferD3D12&)commandBuffer; } -void FillFunctionTableCommandBufferD3D12(CoreInterface& coreInterface) -{ - coreInterface.DestroyCommandBuffer = ::DestroyCommandBuffer; - - coreInterface.BeginCommandBuffer = ::BeginCommandBuffer; - coreInterface.EndCommandBuffer = ::EndCommandBuffer; - - coreInterface.CmdSetPipelineLayout = ::CmdSetPipelineLayout; - coreInterface.CmdSetPipeline = ::CmdSetPipeline; - coreInterface.CmdPipelineBarrier = ::CmdPipelineBarrier; - coreInterface.CmdSetDescriptorPool = ::CmdSetDescriptorPool; - coreInterface.CmdSetDescriptorSet = ::CmdSetDescriptorSet; - coreInterface.CmdSetConstants = ::CmdSetConstants; - - coreInterface.CmdBeginRenderPass = ::CmdBeginRenderPass; - coreInterface.CmdEndRenderPass = ::CmdEndRenderPass; - coreInterface.CmdSetViewports = ::CmdSetViewports; - coreInterface.CmdSetScissors = ::CmdSetScissors; - coreInterface.CmdSetDepthBounds = ::CmdSetDepthBounds; - coreInterface.CmdSetStencilReference = ::CmdSetStencilReference; - coreInterface.CmdSetSamplePositions = ::CmdSetSamplePositions; - coreInterface.CmdClearAttachments = ::CmdClearAttachments; - coreInterface.CmdSetIndexBuffer = ::CmdSetIndexBuffer; - coreInterface.CmdSetVertexBuffers = ::CmdSetVertexBuffers; - - coreInterface.CmdDraw = ::CmdDraw; - coreInterface.CmdDrawIndexed = ::CmdDrawIndexed; - coreInterface.CmdDrawIndirect = ::CmdDrawIndirect; - coreInterface.CmdDrawIndexedIndirect = ::CmdDrawIndexedIndirect; - coreInterface.CmdDispatch = ::CmdDispatch; - coreInterface.CmdDispatchIndirect = ::CmdDispatchIndirect; - coreInterface.CmdBeginQuery = ::CmdBeginQuery; - coreInterface.CmdEndQuery = ::CmdEndQuery; - coreInterface.CmdBeginAnnotation = ::CmdBeginAnnotation; - coreInterface.CmdEndAnnotation = ::CmdEndAnnotation; - - coreInterface.CmdClearStorageBuffer = ::CmdClearStorageBuffer; - coreInterface.CmdClearStorageTexture = ::CmdClearStorageTexture; - coreInterface.CmdCopyBuffer = ::CmdCopyBuffer; - coreInterface.CmdCopyTexture = ::CmdCopyTexture; - coreInterface.CmdUploadBufferToTexture = ::CmdUploadBufferToTexture; - coreInterface.CmdReadbackTextureToBuffer = ::CmdReadbackTextureToBuffer; - coreInterface.CmdCopyQueries = ::CmdCopyQueries; - coreInterface.CmdResetQueries = ::CmdResetQueries; - - coreInterface.SetCommandBufferDebugName = ::SetCommandBufferDebugName; - - coreInterface.GetCommandBufferNativeObject = ::GetCommandBufferNativeObject; -} - #pragma endregion -#pragma region [ RayTracingInterface ] - -#ifdef __ID3D12GraphicsCommandList4_INTERFACE_DEFINED__ +#pragma region [ RayTracing ] static void NRI_CALL CmdBuildTopLevelAccelerationStructure(CommandBuffer& commandBuffer, uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, AccelerationStructureBuildBits flags, AccelerationStructure& dst, Buffer& scratch, uint64_t scratchOffset) @@ -310,35 +258,17 @@ static void NRI_CALL CmdDispatchRays(CommandBuffer& commandBuffer, const Dispatc ((CommandBufferD3D12&)commandBuffer).DispatchRays(dispatchRaysDesc); } -void FillFunctionTableCommandBufferD3D12(RayTracingInterface& rayTracingInterface) -{ - rayTracingInterface.CmdBuildTopLevelAccelerationStructure = ::CmdBuildTopLevelAccelerationStructure; - rayTracingInterface.CmdBuildBottomLevelAccelerationStructure = ::CmdBuildBottomLevelAccelerationStructure; - rayTracingInterface.CmdUpdateTopLevelAccelerationStructure = ::CmdUpdateTopLevelAccelerationStructure; - rayTracingInterface.CmdUpdateBottomLevelAccelerationStructure = ::CmdUpdateBottomLevelAccelerationStructure; - rayTracingInterface.CmdCopyAccelerationStructure = ::CmdCopyAccelerationStructure; - rayTracingInterface.CmdWriteAccelerationStructureSize = ::CmdWriteAccelerationStructureSize; - rayTracingInterface.CmdDispatchRays = ::CmdDispatchRays; -} - -#endif - #pragma endregion -#pragma region [ MeshShaderInterface ] +#pragma region [ MeshShader ] -#ifdef __ID3D12GraphicsCommandList6_INTERFACE_DEFINED__ - -static void NRI_CALL CmdDispatchMeshTasks(CommandBuffer& commandBuffer, uint32_t taskNum) -{ - ((CommandBufferD3D12&)commandBuffer).DispatchMeshTasks(taskNum); -} - -void FillFunctionTableCommandBufferD3D12(MeshShaderInterface& meshShaderInterface) +static void NRI_CALL CmdDispatchMeshTasks(CommandBuffer& commandBuffer, uint32_t x, uint32_t y, uint32_t z) { - meshShaderInterface.CmdDispatchMeshTasks = CmdDispatchMeshTasks; + ((CommandBufferD3D12&)commandBuffer).DispatchMeshTasks(x, y, z); } -#endif - #pragma endregion + +Define_Core_CommandBuffer_PartiallyFillFunctionTable(D3D12) +Define_RayTracing_CommandBuffer_PartiallyFillFunctionTable(D3D12) +Define_MeshShader_CommandBuffer_PartiallyFillFunctionTable(D3D12) diff --git a/Source/D3D12/CommandQueueD3D12.cpp b/Source/D3D12/CommandQueueD3D12.cpp index 6b6f1175..6c3dbb79 100644 --- a/Source/D3D12/CommandQueueD3D12.cpp +++ b/Source/D3D12/CommandQueueD3D12.cpp @@ -10,9 +10,6 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include "SharedD3D12.h" #include "CommandQueueD3D12.h" -#include "DeviceD3D12.h" -#include "QueueSemaphoreD3D12.h" -#include "DeviceSemaphoreD3D12.h" #include "CommandBufferD3D12.h" using namespace nri; @@ -49,40 +46,20 @@ Result CommandQueueD3D12::Create(ID3D12CommandQueue* commandQueue) return Result::SUCCESS; } -D3D12_COMMAND_LIST_TYPE CommandQueueD3D12::GetType() const -{ - return m_CommandListType; -} - -inline void CommandQueueD3D12::SetDebugName(const char* name) -{ - SET_D3D_DEBUG_OBJECT_NAME(m_CommandQueue, name); -} +//================================================================================================================ +// NRI +//================================================================================================================ -inline void CommandQueueD3D12::Submit(const WorkSubmissionDesc& workSubmissionDesc, DeviceSemaphore* deviceSemaphore) +inline void CommandQueueD3D12::Submit(const QueueSubmitDesc& queueSubmitDesc) { - for (uint32_t i = 0; i < workSubmissionDesc.waitNum; i++) - ((QueueSemaphoreD3D12*)workSubmissionDesc.wait[i])->Wait(m_CommandQueue); - - if (workSubmissionDesc.commandBufferNum) + if (queueSubmitDesc.commandBufferNum) { - Vector commandLists(m_Device.GetStdAllocator()); - for (uint32_t j = 0; j < workSubmissionDesc.commandBufferNum; j++) - commandLists.push_back(*((CommandBufferD3D12*)workSubmissionDesc.commandBuffers[j])); + ID3D12CommandList** commandLists = STACK_ALLOC(ID3D12CommandList*, queueSubmitDesc.commandBufferNum); + for (uint32_t j = 0; j < queueSubmitDesc.commandBufferNum; j++) + commandLists[j] = *(CommandBufferD3D12*)queueSubmitDesc.commandBuffers[j]; - m_CommandQueue->ExecuteCommandLists((UINT)commandLists.size(), (ID3D12CommandList**)&commandLists[0]); + m_CommandQueue->ExecuteCommandLists(queueSubmitDesc.commandBufferNum, commandLists); } - - for (uint32_t i = 0; i < workSubmissionDesc.signalNum; i++) - ((QueueSemaphoreD3D12*)workSubmissionDesc.signal[i])->Signal(m_CommandQueue); - - if (deviceSemaphore) - ((DeviceSemaphoreD3D12*)deviceSemaphore)->Signal(m_CommandQueue); -} - -inline void CommandQueueD3D12::Wait(DeviceSemaphore& deviceSemaphore) -{ - ((DeviceSemaphoreD3D12&)deviceSemaphore).Wait(); } inline Result CommandQueueD3D12::ChangeResourceStates(const TransitionBarrierDesc& transitionBarriers) diff --git a/Source/D3D12/CommandQueueD3D12.h b/Source/D3D12/CommandQueueD3D12.h index d0012806..74a0fb69 100644 --- a/Source/D3D12/CommandQueueD3D12.h +++ b/Source/D3D12/CommandQueueD3D12.h @@ -16,55 +16,46 @@ enum D3D12_COMMAND_LIST_TYPE; namespace nri { - struct DeviceD3D12; - struct CommandQueueD3D12 - { - CommandQueueD3D12(DeviceD3D12& device); - ~CommandQueueD3D12(); +struct DeviceD3D12; - operator ID3D12CommandQueue*() const; - - DeviceD3D12& GetDevice() const; +struct CommandQueueD3D12 +{ + inline CommandQueueD3D12(DeviceD3D12& device) : m_Device(device) + {} - Result Create(CommandQueueType queueType); - Result Create(ID3D12CommandQueue* commandQueue); + inline ~CommandQueueD3D12() + {} - D3D12_COMMAND_LIST_TYPE GetType() const; + inline operator ID3D12CommandQueue*() const + { return m_CommandQueue.GetInterface(); } - //================================================================================================================ - // NRI - //================================================================================================================ - void SetDebugName(const char* name); + inline DeviceD3D12& GetDevice() const + { return m_Device; } - void Submit(const WorkSubmissionDesc& workSubmissionDesc, DeviceSemaphore* deviceSemaphore); - void Wait(DeviceSemaphore& deviceSemaphore); + inline D3D12_COMMAND_LIST_TYPE GetType() const + { return m_CommandListType; } - Result ChangeResourceStates(const TransitionBarrierDesc& transitionBarriers); - Result UploadData(const TextureUploadDesc* textureUploadDescs, uint32_t textureUploadDescNum, - const BufferUploadDesc* bufferUploadDescs, uint32_t bufferUploadDescNum); - Result WaitForIdle(); + Result Create(CommandQueueType queueType); + Result Create(ID3D12CommandQueue* commandQueue); - private: - DeviceD3D12& m_Device; - ComPtr m_CommandQueue; - D3D12_COMMAND_LIST_TYPE m_CommandListType = D3D12_COMMAND_LIST_TYPE(-1); - }; + //================================================================================================================ + // NRI + //================================================================================================================ - inline CommandQueueD3D12::CommandQueueD3D12(DeviceD3D12& device) - : m_Device(device) - {} + inline void SetDebugName(const char* name) + { SET_D3D_DEBUG_OBJECT_NAME(m_CommandQueue, name); } - inline CommandQueueD3D12::~CommandQueueD3D12() - {} + void Submit(const QueueSubmitDesc& queueSubmitDesc); + Result ChangeResourceStates(const TransitionBarrierDesc& transitionBarriers); + Result UploadData(const TextureUploadDesc* textureUploadDescs, uint32_t textureUploadDescNum, + const BufferUploadDesc* bufferUploadDescs, uint32_t bufferUploadDescNum); + Result WaitForIdle(); - inline CommandQueueD3D12::operator ID3D12CommandQueue*() const - { - return m_CommandQueue.GetInterface(); - } +private: + DeviceD3D12& m_Device; + ComPtr m_CommandQueue; + D3D12_COMMAND_LIST_TYPE m_CommandListType = D3D12_COMMAND_LIST_TYPE(-1); +}; - inline DeviceD3D12& CommandQueueD3D12::GetDevice() const - { - return m_Device; - } } diff --git a/Source/D3D12/CommandQueueD3D12.hpp b/Source/D3D12/CommandQueueD3D12.hpp index d7a08c23..3d063ca2 100644 --- a/Source/D3D12/CommandQueueD3D12.hpp +++ b/Source/D3D12/CommandQueueD3D12.hpp @@ -8,55 +8,39 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ CoreInterface ] +#pragma region [ Core ] static void NRI_CALL SetCommandQueueDebugName(CommandQueue& commandQueue, const char* name) { ((CommandQueueD3D12&)commandQueue).SetDebugName(name); } -static void NRI_CALL SubmitQueueWork(CommandQueue& commandQueue, const WorkSubmissionDesc& workSubmissionDesc, DeviceSemaphore* deviceSemaphore) +static void NRI_CALL QueueSubmit(CommandQueue& commandQueue, const QueueSubmitDesc& queueSubmitDesc) { - ((CommandQueueD3D12&)commandQueue).Submit(workSubmissionDesc, deviceSemaphore); -} - -static void NRI_CALL WaitForSemaphore(CommandQueue& commandQueue, DeviceSemaphore& deviceSemaphore) -{ - ((CommandQueueD3D12&)commandQueue).Wait(deviceSemaphore); -} - -void FillFunctionTableCommandQueueD3D12(CoreInterface& coreInterface) -{ - coreInterface.SetCommandQueueDebugName = ::SetCommandQueueDebugName; - coreInterface.SubmitQueueWork = ::SubmitQueueWork; - coreInterface.WaitForSemaphore = ::WaitForSemaphore; + ((CommandQueueD3D12&)commandQueue).Submit(queueSubmitDesc); } #pragma endregion -#pragma region [ HelperInterface ] +#pragma region [ Helper ] -static Result NRI_CALL ChangeResourceStatesD3D12(CommandQueue& commandQueue, const TransitionBarrierDesc& transitionBarriers) +static Result NRI_CALL ChangeResourceStates(CommandQueue& commandQueue, const TransitionBarrierDesc& transitionBarriers) { return ((CommandQueueD3D12&)commandQueue).ChangeResourceStates(transitionBarriers); } -static nri::Result NRI_CALL UploadDataD3D12(CommandQueue& commandQueue, const TextureUploadDesc* textureUploadDescs, uint32_t textureUploadDescNum, +static nri::Result NRI_CALL UploadData(CommandQueue& commandQueue, const TextureUploadDesc* textureUploadDescs, uint32_t textureUploadDescNum, const BufferUploadDesc* bufferUploadDescs, uint32_t bufferUploadDescNum) { return ((CommandQueueD3D12&)commandQueue).UploadData(textureUploadDescs, textureUploadDescNum, bufferUploadDescs, bufferUploadDescNum); } -static nri::Result NRI_CALL WaitForIdleD3D12(CommandQueue& commandQueue) +static nri::Result NRI_CALL WaitForIdle(CommandQueue& commandQueue) { return ((CommandQueueD3D12&)commandQueue).WaitForIdle(); } -void FillFunctionTableCommandQueueD3D12(HelperInterface& helperInterface) -{ - helperInterface.ChangeResourceStates = ::ChangeResourceStatesD3D12; - helperInterface.UploadData = ::UploadDataD3D12; - helperInterface.WaitForIdle = ::WaitForIdleD3D12; -} - #pragma endregion + +Define_Core_CommandQueue_PartiallyFillFunctionTable(D3D12) +Define_Helper_CommandQueue_PartiallyFillFunctionTable(D3D12) diff --git a/Source/D3D12/ConversionD3D12.cpp b/Source/D3D12/ConversionD3D12.cpp index 077f5976..5de721ff 100644 --- a/Source/D3D12/ConversionD3D12.cpp +++ b/Source/D3D12/ConversionD3D12.cpp @@ -72,7 +72,7 @@ bool RequiresDedicatedAllocation(MemoryType memoryType) return false; } -D3D12_RESOURCE_STATES GetResourceStates(AccessBits accessMask) +D3D12_RESOURCE_STATES GetResourceStates(AccessBits accessMask, D3D12_COMMAND_LIST_TYPE commandListType) { D3D12_RESOURCE_STATES resourceStates = D3D12_RESOURCE_STATES::D3D12_RESOURCE_STATE_COMMON; // TODO: PS resource and/or non-PS resource? @@ -95,7 +95,11 @@ D3D12_RESOURCE_STATES GetResourceStates(AccessBits accessMask) if (accessMask & AccessBits::COPY_DESTINATION) resourceStates |= D3D12_RESOURCE_STATES::D3D12_RESOURCE_STATE_COPY_DEST; if (accessMask & AccessBits::SHADER_RESOURCE) - resourceStates |= D3D12_RESOURCE_STATES::D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE | D3D12_RESOURCE_STATES::D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE; + { + resourceStates |= D3D12_RESOURCE_STATES::D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE; + if (commandListType == D3D12_COMMAND_LIST_TYPE_DIRECT) + resourceStates |= D3D12_RESOURCE_STATES::D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE; + } if (accessMask & AccessBits::ACCELERATION_STRUCTURE_READ) resourceStates |= D3D12_RESOURCE_STATES::D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE; if (accessMask & AccessBits::ACCELERATION_STRUCTURE_WRITE) @@ -751,8 +755,6 @@ namespace nri } } -#ifdef __ID3D12GraphicsCommandList4_INTERFACE_DEFINED__ - D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE GetAccelerationStructureType(AccelerationStructureType accelerationStructureType) { static_assert(D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL == (uint32_t)AccelerationStructureType::TOP_LEVEL, "Unsupported AccelerationStructureType."); @@ -824,5 +826,3 @@ D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE GetCopyMode(CopyMode copyMode) return (D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE)copyMode; } - -#endif diff --git a/Source/D3D12/DescriptorD3D12.cpp b/Source/D3D12/DescriptorD3D12.cpp index d4f05d51..3599b185 100644 --- a/Source/D3D12/DescriptorD3D12.cpp +++ b/Source/D3D12/DescriptorD3D12.cpp @@ -361,7 +361,6 @@ Result DescriptorD3D12::Create(const Texture3DViewDesc& textureViewDesc) return Result::FAILURE; } -#ifdef __ID3D12GraphicsCommandList4_INTERFACE_DEFINED__ Result DescriptorD3D12::Create(const AccelerationStructure& accelerationStructure) { D3D12_SHADER_RESOURCE_VIEW_DESC desc = {}; @@ -371,7 +370,6 @@ Result DescriptorD3D12::Create(const AccelerationStructure& accelerationStructur return CreateShaderResourceView(nullptr, desc); } -#endif Result DescriptorD3D12::Create(const SamplerDesc& samplerDesc) { @@ -491,7 +489,4 @@ Result DescriptorD3D12::CreateDepthStencilView(ID3D12Resource* resource, const D return result; } -void DescriptorD3D12::SetDebugName(const char* name) -{ - MaybeUnused(name); -} +#include "DescriptorD3D12.hpp" diff --git a/Source/D3D12/DescriptorD3D12.h b/Source/D3D12/DescriptorD3D12.h index ceb7b53a..a5e3f333 100644 --- a/Source/D3D12/DescriptorD3D12.h +++ b/Source/D3D12/DescriptorD3D12.h @@ -10,88 +10,66 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #pragma once -#include "DeviceD3D12.h" - struct ID3D12Resource; namespace nri { - struct DeviceD3D12; - - struct DescriptorD3D12 - { - DescriptorD3D12(DeviceD3D12& device); - ~DescriptorD3D12(); - - operator ID3D12Resource*() const; - - DeviceD3D12& GetDevice() const; - - Result Create(const BufferViewDesc& bufferViewDesc); - Result Create(const Texture1DViewDesc& textureViewDesc); - Result Create(const Texture2DViewDesc& textureViewDesc); - Result Create(const Texture3DViewDesc& textureViewDesc); -#ifdef __ID3D12GraphicsCommandList4_INTERFACE_DEFINED__ - Result Create(const AccelerationStructure& accelerationStructure); -#endif - Result Create(const SamplerDesc& samplerDesc); - DescriptorPointerCPU GetPointerCPU() const; - D3D12_GPU_VIRTUAL_ADDRESS GetBufferLocation() const; - bool IsFloatingPointUAV() const; - - //================================================================================================================ - // NRI - //================================================================================================================ - void SetDebugName(const char* name); - - private: - Result CreateConstantBufferView(const D3D12_CONSTANT_BUFFER_VIEW_DESC& desc); - Result CreateShaderResourceView(ID3D12Resource* resource, const D3D12_SHADER_RESOURCE_VIEW_DESC& desc); - Result CreateUnorderedAccessView(ID3D12Resource* resource, const D3D12_UNORDERED_ACCESS_VIEW_DESC& desc, Format format); - Result CreateRenderTargetView(ID3D12Resource* resource, const D3D12_RENDER_TARGET_VIEW_DESC& desc); - Result CreateDepthStencilView(ID3D12Resource* resource, const D3D12_DEPTH_STENCIL_VIEW_DESC& desc); - - private: - DeviceD3D12& m_Device; - ID3D12Resource* m_Resource = nullptr; - D3D12_GPU_VIRTUAL_ADDRESS m_BufferLocation = 0; - DescriptorHandle m_Handle = {}; - DescriptorPointerCPU m_DescriptorPointerCPU = {}; - D3D12_DESCRIPTOR_HEAP_TYPE m_HeapType = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV; - bool m_IsFloatingPointFormatUAV = false; - }; - - inline DescriptorD3D12::DescriptorD3D12(DeviceD3D12& device) + +struct DeviceD3D12; + +struct DescriptorD3D12 +{ + inline DescriptorD3D12(DeviceD3D12& device) : m_Device(device) {} - inline DescriptorD3D12::~DescriptorD3D12() - { - m_Device.FreeDescriptorHandle(m_HeapType, m_Handle); - } - - inline DescriptorD3D12::operator ID3D12Resource*() const - { - return m_Resource; - } - - inline DescriptorPointerCPU DescriptorD3D12::GetPointerCPU() const - { - return m_DescriptorPointerCPU; - } - - inline D3D12_GPU_VIRTUAL_ADDRESS DescriptorD3D12::GetBufferLocation() const - { - return m_BufferLocation; - } - - inline bool DescriptorD3D12::IsFloatingPointUAV() const - { - return m_IsFloatingPointFormatUAV; - } - - inline DeviceD3D12& DescriptorD3D12::GetDevice() const - { - return m_Device; - } + inline ~DescriptorD3D12() + { m_Device.FreeDescriptorHandle(m_HeapType, m_Handle); } + + inline operator ID3D12Resource*() const + { return m_Resource; } + + inline DescriptorPointerCPU GetPointerCPU() const + { return m_DescriptorPointerCPU; } + + inline D3D12_GPU_VIRTUAL_ADDRESS GetBufferLocation() const + { return m_BufferLocation; } + + inline bool IsFloatingPointUAV() const + { return m_IsFloatingPointFormatUAV; } + + inline DeviceD3D12& GetDevice() const + { return m_Device; } + + Result Create(const BufferViewDesc& bufferViewDesc); + Result Create(const Texture1DViewDesc& textureViewDesc); + Result Create(const Texture2DViewDesc& textureViewDesc); + Result Create(const Texture3DViewDesc& textureViewDesc); + Result Create(const AccelerationStructure& accelerationStructure); + Result Create(const SamplerDesc& samplerDesc); + + //================================================================================================================ + // NRI + //================================================================================================================ + + inline void SetDebugName(const char* name) + { MaybeUnused(name); } + +private: + Result CreateConstantBufferView(const D3D12_CONSTANT_BUFFER_VIEW_DESC& desc); + Result CreateShaderResourceView(ID3D12Resource* resource, const D3D12_SHADER_RESOURCE_VIEW_DESC& desc); + Result CreateUnorderedAccessView(ID3D12Resource* resource, const D3D12_UNORDERED_ACCESS_VIEW_DESC& desc, Format format); + Result CreateRenderTargetView(ID3D12Resource* resource, const D3D12_RENDER_TARGET_VIEW_DESC& desc); + Result CreateDepthStencilView(ID3D12Resource* resource, const D3D12_DEPTH_STENCIL_VIEW_DESC& desc); + +private: + DeviceD3D12& m_Device; + ID3D12Resource* m_Resource = nullptr; + D3D12_GPU_VIRTUAL_ADDRESS m_BufferLocation = 0; + DescriptorHandle m_Handle = {}; + DescriptorPointerCPU m_DescriptorPointerCPU = {}; + D3D12_DESCRIPTOR_HEAP_TYPE m_HeapType = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV; + bool m_IsFloatingPointFormatUAV = false; +}; + } diff --git a/Source/VK/QueueSemaphoreVK.hpp b/Source/D3D12/DescriptorD3D12.hpp similarity index 50% rename from Source/VK/QueueSemaphoreVK.hpp rename to Source/D3D12/DescriptorD3D12.hpp index c5fa199d..8444662c 100644 --- a/Source/VK/QueueSemaphoreVK.hpp +++ b/Source/D3D12/DescriptorD3D12.hpp @@ -8,18 +8,20 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma once +#pragma region [ Core ] -#pragma region [ CoreInterface ] - -static void NRI_CALL SetQueueSemaphoreDebugName(QueueSemaphore& queueSemaphore, const char* name) +static void NRI_CALL SetDescriptorDebugName(Descriptor& descriptor, const char* name) { - ((QueueSemaphoreVK&)queueSemaphore).SetDebugName(name); + ((DescriptorD3D12&)descriptor).SetDebugName(name); } -void FillFunctionTableQueueSemaphoreVK(CoreInterface& coreInterface) +static uint64_t NRI_CALL GetDescriptorNativeObject(const Descriptor& descriptor, uint32_t physicalDeviceIndex) { - coreInterface.SetQueueSemaphoreDebugName = ::SetQueueSemaphoreDebugName; + MaybeUnused(physicalDeviceIndex); + + return uint64_t( ((DescriptorD3D12&)descriptor).GetPointerCPU() ); } #pragma endregion + +Define_Core_Descriptor_PartiallyFillFunctionTable(D3D12) diff --git a/Source/D3D12/DescriptorPoolD3D12.cpp b/Source/D3D12/DescriptorPoolD3D12.cpp index 844da097..d2c5fcb1 100644 --- a/Source/D3D12/DescriptorPoolD3D12.cpp +++ b/Source/D3D12/DescriptorPoolD3D12.cpp @@ -10,25 +10,12 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include "SharedD3D12.h" #include "DescriptorPoolD3D12.h" -#include "DeviceD3D12.h" #include "PipelineLayoutD3D12.h" using namespace nri; extern D3D12_DESCRIPTOR_HEAP_TYPE GetDescriptorHeapType(DescriptorType descriptorType); -DescriptorPoolD3D12::DescriptorPoolD3D12(DeviceD3D12& device) - : m_Device(device) - , m_DescriptorSets(device.GetStdAllocator()) -{} - -DescriptorPoolD3D12::~DescriptorPoolD3D12() -{ - Reset(); - - m_DescriptorSets.clear(); -} - Result DescriptorPoolD3D12::Create(const DescriptorPoolDesc& descriptorPoolDesc) { uint32_t descriptorHeapSize[DescriptorHeapType::MAX_NUM] = {}; @@ -99,9 +86,14 @@ DescriptorPointerGPU DescriptorPoolD3D12::GetDescriptorPointerGPU(DescriptorHeap return descriptorPointer; } +//================================================================================================================ +// NRI +//================================================================================================================ + inline void DescriptorPoolD3D12::SetDebugName(const char* name) { - MaybeUnused(name); + for (ID3D12DescriptorHeap* descriptorHeap : m_DescriptorHeaps) + SET_D3D_DEBUG_OBJECT_NAME(descriptorHeap, name); } inline Result DescriptorPoolD3D12::AllocateDescriptorSets(const PipelineLayout& pipelineLayout, uint32_t setIndexInPipelineLayout, DescriptorSet** descriptorSets, uint32_t instanceNum, uint32_t variableDescriptorNum) @@ -127,7 +119,7 @@ inline Result DescriptorPoolD3D12::AllocateDescriptorSets(const PipelineLayout& return Result::SUCCESS; } -inline void DescriptorPoolD3D12::Reset() +void DescriptorPoolD3D12::Reset() { for (uint32_t i = 0; i < DescriptorHeapType::MAX_NUM; i++) m_DescriptorNum[i] = 0; diff --git a/Source/D3D12/DescriptorPoolD3D12.h b/Source/D3D12/DescriptorPoolD3D12.h index 6d7f892a..558f4081 100644 --- a/Source/D3D12/DescriptorPoolD3D12.h +++ b/Source/D3D12/DescriptorPoolD3D12.h @@ -10,54 +10,57 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #pragma once -#include "DeviceD3D12.h" - namespace nri { - struct DeviceD3D12; - struct DescriptorSetD3D12; - enum DescriptorHeapType : uint32_t - { - RESOURCE = 0, - SAMPLER, - MAX_NUM - }; +struct DeviceD3D12; +struct DescriptorSetD3D12; - struct DescriptorPoolD3D12 - { - DescriptorPoolD3D12(DeviceD3D12& device); - ~DescriptorPoolD3D12(); +enum DescriptorHeapType : uint32_t +{ + RESOURCE = 0, + SAMPLER, + MAX_NUM +}; + +struct DescriptorPoolD3D12 +{ + inline DescriptorPoolD3D12(DeviceD3D12& device) + : m_Device(device) + , m_DescriptorSets(device.GetStdAllocator()) + {} - DeviceD3D12& GetDevice() const; + inline ~DescriptorPoolD3D12() + { + Reset(); + m_DescriptorSets.clear(); + } - Result Create(const DescriptorPoolDesc& descriptorPoolDesc); - void Bind(ID3D12GraphicsCommandList* graphicsCommandList) const; - uint32_t AllocateDescriptors(DescriptorHeapType descriptorHeapType, uint32_t descriptorNum); - DescriptorPointerCPU GetDescriptorPointerCPU(DescriptorHeapType descriptorHeapType, uint32_t offset) const; - DescriptorPointerGPU GetDescriptorPointerGPU(DescriptorHeapType descriptorHeapType, uint32_t offset) const; + inline DeviceD3D12& GetDevice() const + { return m_Device; } - //====================================================================================================================== - // NRI - //====================================================================================================================== - void SetDebugName(const char* name); + Result Create(const DescriptorPoolDesc& descriptorPoolDesc); + void Bind(ID3D12GraphicsCommandList* graphicsCommandList) const; + uint32_t AllocateDescriptors(DescriptorHeapType descriptorHeapType, uint32_t descriptorNum); + DescriptorPointerCPU GetDescriptorPointerCPU(DescriptorHeapType descriptorHeapType, uint32_t offset) const; + DescriptorPointerGPU GetDescriptorPointerGPU(DescriptorHeapType descriptorHeapType, uint32_t offset) const; - Result AllocateDescriptorSets(const PipelineLayout& pipelineLayout, uint32_t setIndexInPipelineLayout, DescriptorSet** descriptorSets, uint32_t instanceNum, uint32_t variableDescriptorNum); + //================================================================================================================ + // NRI + //================================================================================================================ - void Reset(); + void SetDebugName(const char* name); + Result AllocateDescriptorSets(const PipelineLayout& pipelineLayout, uint32_t setIndexInPipelineLayout, DescriptorSet** descriptorSets, uint32_t instanceNum, uint32_t variableDescriptorNum); + void Reset(); - private: - DeviceD3D12& m_Device; - std::array m_DescriptorHeapDescs; - std::array m_DescriptorNum = {}; - std::array m_DescriptorHeaps = {}; - Vector m_DescriptorSets; - uint32_t m_DescriptorHeapNum = 0; - uint32_t m_DescriptorSetNum = 0; - }; +private: + DeviceD3D12& m_Device; + std::array m_DescriptorHeapDescs; + std::array m_DescriptorNum = {}; + std::array m_DescriptorHeaps = {}; + Vector m_DescriptorSets; + uint32_t m_DescriptorHeapNum = 0; + uint32_t m_DescriptorSetNum = 0; +}; - inline DeviceD3D12& DescriptorPoolD3D12::GetDevice() const - { - return m_Device; - } } diff --git a/Source/D3D12/DescriptorPoolD3D12.hpp b/Source/D3D12/DescriptorPoolD3D12.hpp index d48e86d8..540f66a1 100644 --- a/Source/D3D12/DescriptorPoolD3D12.hpp +++ b/Source/D3D12/DescriptorPoolD3D12.hpp @@ -8,7 +8,7 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ CoreInterface ] +#pragma region [ Core ] static void NRI_CALL SetDescriptorPoolDebugName(DescriptorPool& descriptorPool, const char* name) { @@ -28,11 +28,6 @@ static void NRI_CALL ResetDescriptorPool(DescriptorPool& descriptorPool) ((DescriptorPoolD3D12&)descriptorPool).Reset(); } -void FillFunctionTableDescriptorPoolD3D12(CoreInterface& coreInterface) -{ - coreInterface.SetDescriptorPoolDebugName = ::SetDescriptorPoolDebugName; - coreInterface.AllocateDescriptorSets = ::AllocateDescriptorSets; - coreInterface.ResetDescriptorPool = ::ResetDescriptorPool; -} - #pragma endregion + +Define_Core_DescriptorPool_PartiallyFillFunctionTable(D3D12) diff --git a/Source/D3D12/DescriptorSetD3D12.cpp b/Source/D3D12/DescriptorSetD3D12.cpp index 56761458..ac7c17f2 100644 --- a/Source/D3D12/DescriptorSetD3D12.cpp +++ b/Source/D3D12/DescriptorSetD3D12.cpp @@ -10,8 +10,6 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include "SharedD3D12.h" #include "DescriptorSetD3D12.h" -#include "DeviceD3D12.h" -#include "DescriptorPoolD3D12.h" #include "DescriptorD3D12.h" using namespace nri; @@ -78,10 +76,9 @@ DescriptorPointerGPU DescriptorSetD3D12::GetDynamicPointerGPU(uint32_t dynamicCo return m_DynamicConstantBuffers[dynamicConstantBufferIndex]; } -inline void DescriptorSetD3D12::SetDebugName(const char* name) -{ - MaybeUnused(name); -} +//================================================================================================================ +// NRI +//================================================================================================================ inline void DescriptorSetD3D12::UpdateDescriptorRanges(uint32_t rangeOffset, uint32_t rangeNum, const DescriptorRangeUpdateDesc* rangeUpdateDescs) { diff --git a/Source/D3D12/DescriptorSetD3D12.h b/Source/D3D12/DescriptorSetD3D12.h index c0757cdf..cc30e54c 100644 --- a/Source/D3D12/DescriptorSetD3D12.h +++ b/Source/D3D12/DescriptorSetD3D12.h @@ -14,59 +14,57 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct DeviceD3D12; - - struct DescriptorRangeMapping - { - DescriptorHeapType descriptorHeapType; - uint32_t heapOffset; - uint32_t descriptorNum; - }; - - struct DescriptorSetMapping - { - DescriptorSetMapping(StdAllocator& allocator); - - std::array descriptorNum = {}; - Vector descriptorRangeMappings; - }; - - struct DescriptorSetD3D12 - { - DescriptorSetD3D12(DeviceD3D12& device, DescriptorPoolD3D12& desriptorPoolD3D12, const DescriptorSetMapping& descriptorSetMapping, uint16_t dynamicConstantBufferNum); - ~DescriptorSetD3D12(); - - DeviceD3D12& GetDevice() const; - - static void BuildDescriptorSetMapping(const DescriptorSetDesc& descriptorSetDesc, DescriptorSetMapping& descriptorSetMapping); - DescriptorPointerCPU GetPointerCPU(uint32_t rangeIndex, uint32_t rangeOffset) const; - DescriptorPointerGPU GetPointerGPU(uint32_t rangeIndex, uint32_t rangeOffset) const; - DescriptorPointerGPU GetDynamicPointerGPU(uint32_t dynamicConstantBufferIndex) const; - - //====================================================================================================================== - // NRI - //====================================================================================================================== - void SetDebugName(const char* name); - void UpdateDescriptorRanges(uint32_t rangeOffset, uint32_t rangeNum, const DescriptorRangeUpdateDesc* rangeUpdateDescs); - void UpdateDynamicConstantBuffers(uint32_t baseBuffer, uint32_t bufferNum, const Descriptor* const* descriptors); - void Copy(const DescriptorSetCopyDesc& descriptorSetCopyDesc); - - private: - DeviceD3D12& m_Device; - DescriptorPoolD3D12& m_DescriptorPoolD3D12; - DescriptorSetMapping m_DescriptorSetMapping; - Vector m_DynamicConstantBuffers; - }; - - inline DescriptorSetD3D12::~DescriptorSetD3D12() - {} - inline DeviceD3D12& DescriptorSetD3D12::GetDevice() const - { - return m_Device; - } +struct DeviceD3D12; + +struct DescriptorRangeMapping +{ + DescriptorHeapType descriptorHeapType; + uint32_t heapOffset; + uint32_t descriptorNum; +}; - inline DescriptorSetMapping::DescriptorSetMapping(StdAllocator& allocator) +struct DescriptorSetMapping +{ + inline DescriptorSetMapping(StdAllocator& allocator) : descriptorRangeMappings(allocator) {} + + std::array descriptorNum = {}; + Vector descriptorRangeMappings; +}; + +struct DescriptorSetD3D12 +{ + inline ~DescriptorSetD3D12() + {} + + inline DeviceD3D12& GetDevice() const + { return m_Device; } + + DescriptorSetD3D12(DeviceD3D12& device, DescriptorPoolD3D12& desriptorPoolD3D12, const DescriptorSetMapping& descriptorSetMapping, uint16_t dynamicConstantBufferNum); + + static void BuildDescriptorSetMapping(const DescriptorSetDesc& descriptorSetDesc, DescriptorSetMapping& descriptorSetMapping); + DescriptorPointerCPU GetPointerCPU(uint32_t rangeIndex, uint32_t rangeOffset) const; + DescriptorPointerGPU GetPointerGPU(uint32_t rangeIndex, uint32_t rangeOffset) const; + DescriptorPointerGPU GetDynamicPointerGPU(uint32_t dynamicConstantBufferIndex) const; + + //================================================================================================================ + // NRI + //================================================================================================================ + + inline void SetDebugName(const char* name) + { MaybeUnused(name); } + + void UpdateDescriptorRanges(uint32_t rangeOffset, uint32_t rangeNum, const DescriptorRangeUpdateDesc* rangeUpdateDescs); + void UpdateDynamicConstantBuffers(uint32_t baseBuffer, uint32_t bufferNum, const Descriptor* const* descriptors); + void Copy(const DescriptorSetCopyDesc& descriptorSetCopyDesc); + +private: + DeviceD3D12& m_Device; + DescriptorPoolD3D12& m_DescriptorPoolD3D12; + DescriptorSetMapping m_DescriptorSetMapping; + Vector m_DynamicConstantBuffers; +}; + } diff --git a/Source/D3D12/DescriptorSetD3D12.hpp b/Source/D3D12/DescriptorSetD3D12.hpp index 82c80e4d..046330a9 100644 --- a/Source/D3D12/DescriptorSetD3D12.hpp +++ b/Source/D3D12/DescriptorSetD3D12.hpp @@ -8,7 +8,7 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ CoreInterface ] +#pragma region [ Core ] static void NRI_CALL SetDescriptorSetDebugName(DescriptorSet& descriptorSet, const char* name) { @@ -36,12 +36,6 @@ static void NRI_CALL CopyDescriptorSet(DescriptorSet& descriptorSet, const Descr ((DescriptorSetD3D12&)descriptorSet).Copy(descriptorSetCopyDesc); } -void FillFunctionTableDescriptorSetD3D12(CoreInterface& coreInterface) -{ - coreInterface.SetDescriptorSetDebugName = ::SetDescriptorSetDebugName; - coreInterface.UpdateDescriptorRanges = ::UpdateDescriptorRanges; - coreInterface.UpdateDynamicConstantBuffers = ::UpdateDynamicConstantBuffers; - coreInterface.CopyDescriptorSet = ::CopyDescriptorSet; -} - #pragma endregion + +Define_Core_DescriptorSet_PartiallyFillFunctionTable(D3D12) diff --git a/Source/D3D12/DeviceD3D12.cpp b/Source/D3D12/DeviceD3D12.cpp index 160a786c..8b033f18 100644 --- a/Source/D3D12/DeviceD3D12.cpp +++ b/Source/D3D12/DeviceD3D12.cpp @@ -8,12 +8,13 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ +#include + #include "SharedD3D12.h" -#include "DeviceD3D12.h" #include "CommandQueueD3D12.h" #include "CommandAllocatorD3D12.h" #include "DescriptorPoolD3D12.h" -#include "DeviceSemaphoreD3D12.h" +#include "FenceD3D12.h" #include "FrameBufferD3D12.h" #include "MemoryD3D12.h" #include "BufferD3D12.h" @@ -22,12 +23,10 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include "PipelineLayoutD3D12.h" #include "PipelineD3D12.h" #include "QueryPoolD3D12.h" -#include "QueueSemaphoreD3D12.h" #include "SwapChainD3D12.h" #include "AccelerationStructureD3D12.h" #include "CommandBufferD3D12.h" -#include #include using namespace nri; @@ -35,6 +34,78 @@ using namespace nri; extern MemoryType GetMemoryType(MemoryLocation memoryLocation, const D3D12_RESOURCE_DESC& resourceDesc); extern bool RequiresDedicatedAllocation(MemoryType memoryType); +inline Vendor GetVendor(ID3D12Device* device) +{ + ComPtr DXGIFactory; + CreateDXGIFactory(IID_PPV_ARGS(&DXGIFactory)); + + DXGI_ADAPTER_DESC desc = {}; + if (DXGIFactory) + { + LUID luid = device->GetAdapterLuid(); + + ComPtr adapter; + DXGIFactory->EnumAdapterByLuid(luid, IID_PPV_ARGS(&adapter)); + if (adapter) + adapter->GetDesc(&desc); + } + + return GetVendorFromID(desc.VendorId); +} + +Result CreateDeviceD3D12(const DeviceCreationD3D12Desc& deviceCreationDesc, DeviceBase*& device) +{ + Log log(GraphicsAPI::D3D12, deviceCreationDesc.callbackInterface); + StdAllocator allocator(deviceCreationDesc.memoryAllocatorInterface); + + DeviceD3D12* implementation = Allocate(allocator, log, allocator); + const Result res = implementation->Create(deviceCreationDesc); + + if (res == Result::SUCCESS) + { + device = implementation; + return Result::SUCCESS; + } + + Deallocate(allocator, implementation); + return res; +} + +Result CreateDeviceD3D12(const DeviceCreationDesc& deviceCreationDesc, DeviceBase*& device) +{ + Log log(GraphicsAPI::D3D12, deviceCreationDesc.callbackInterface); + StdAllocator allocator(deviceCreationDesc.memoryAllocatorInterface); + + ComPtr factory; + HRESULT hr = CreateDXGIFactory2(0, IID_PPV_ARGS(&factory)); + RETURN_ON_BAD_HRESULT(log, hr, "CreateDXGIFactory2() failed, error code: 0x%X.", hr); + + ComPtr adapter; + if (deviceCreationDesc.physicalDeviceGroup != nullptr) + { + LUID luid = *(LUID*)&deviceCreationDesc.physicalDeviceGroup->luid; + hr = factory->EnumAdapterByLuid(luid, IID_PPV_ARGS(&adapter)); + RETURN_ON_BAD_HRESULT(log, hr, "IDXGIFactory4::EnumAdapterByLuid() failed, error code: 0x%X.", hr); + } + else + { + hr = factory->EnumAdapters(0, &adapter); + RETURN_ON_BAD_HRESULT(log, hr, "IDXGIFactory4::EnumAdapters() failed, error code: 0x%X.", hr); + } + + DeviceD3D12* implementation = Allocate(allocator, log, allocator); + const nri::Result result = implementation->Create(adapter, deviceCreationDesc); + if (result != nri::Result::SUCCESS) + { + Deallocate(allocator, implementation); + return result; + } + + device = (DeviceBase*)implementation; + + return nri::Result::SUCCESS; +} + DeviceD3D12::DeviceD3D12(const Log& log, StdAllocator& stdAllocator) : DeviceBase(log, stdAllocator) , m_DescriptorHeaps(GetStdAllocator()) @@ -57,6 +128,23 @@ DeviceD3D12::~DeviceD3D12() } } +template +Result DeviceD3D12::CreateImplementation(Interface*& entity, const Args&... args) +{ + Implementation* implementation = Allocate(GetStdAllocator(), *this); + const Result result = implementation->Create(args...); + + if (result == Result::SUCCESS) + { + entity = (Interface*)implementation; + return Result::SUCCESS; + } + + Deallocate(GetStdAllocator(), implementation); + + return result; +} + bool DeviceD3D12::GetOutput(Display* display, ComPtr& output) const { if (display == nullptr) @@ -86,9 +174,7 @@ Result DeviceD3D12::Create(const DeviceCreationD3D12Desc& deviceCreationDesc) RETURN_ON_BAD_HRESULT(GetLog(), result, "Failed to find IDXGIAdapter by LUID"); } -#ifdef __ID3D12Device5_INTERFACE_DEFINED__ m_Device->QueryInterface(IID_PPV_ARGS(&m_Device5)); -#endif if (deviceCreationDesc.d3d12GraphicsQueue) CreateCommandQueue((ID3D12CommandQueue*)deviceCreationDesc.d3d12GraphicsQueue, m_CommandQueues[(uint32_t)CommandQueueType::GRAPHICS]); @@ -157,9 +243,7 @@ Result DeviceD3D12::Create(IDXGIAdapter* dxgiAdapter, const DeviceCreationDesc& } } -#ifdef __ID3D12Device5_INTERFACE_DEFINED__ m_Device->QueryInterface(IID_PPV_ARGS(&m_Device5)); -#endif CommandQueue* commandQueue; Result result = GetCommandQueue(CommandQueueType::GRAPHICS, commandQueue); @@ -187,103 +271,9 @@ Result DeviceD3D12::Create(IDXGIAdapter* dxgiAdapter, const DeviceCreationDesc& return Result::SUCCESS; } -Result DeviceD3D12::CreateSwapChain(const SwapChainDesc& swapChainDesc, SwapChain*& swapChain) -{ - return CreateImplementation(swapChain, swapChainDesc); -} - -inline Result DeviceD3D12::GetDisplays(Display** displays, uint32_t& displayNum) -{ - HRESULT result = S_OK; - - if (displays == nullptr || displayNum == 0) - { - UINT i = 0; - for(; result != DXGI_ERROR_NOT_FOUND; i++) - { - ComPtr output; - result = m_Adapter->EnumOutputs(i, &output); - } - - displayNum = i; - return Result::SUCCESS; - } - - UINT i = 0; - for(; result != DXGI_ERROR_NOT_FOUND && i < displayNum; i++) - { - ComPtr output; - result = m_Adapter->EnumOutputs(i, &output); - if (result != DXGI_ERROR_NOT_FOUND) - displays[i] = (Display*)(size_t)(i + 1); - } - - for(; i < displayNum; i++) - displays[i] = nullptr; - - return Result::SUCCESS; -} - -inline Result DeviceD3D12::GetDisplaySize(Display& display, uint16_t& width, uint16_t& height) -{ - Display* address = &display; - - if (address == nullptr) - return Result::UNSUPPORTED; - - const uint32_t index = (*(uint32_t*)&address) - 1; - - ComPtr output; - HRESULT result = m_Adapter->EnumOutputs(index, &output); - - if (FAILED(result)) - return Result::UNSUPPORTED; - - DXGI_OUTPUT_DESC outputDesc = {}; - result = output->GetDesc(&outputDesc); - - if (FAILED(result)) - return Result::UNSUPPORTED; - - MONITORINFO monitorInfo = {}; - monitorInfo.cbSize = sizeof(monitorInfo); - - if (!GetMonitorInfoA(outputDesc.Monitor, &monitorInfo)) - return Result::UNSUPPORTED; - - const RECT rect = monitorInfo.rcMonitor; - - width = uint16_t(rect.right - rect.left); - height = uint16_t(rect.bottom - rect.top); - - return Result::SUCCESS; -} - -void DeviceD3D12::DestroySwapChain(SwapChain& swapChain) -{ - Deallocate(GetStdAllocator(), (SwapChainD3D12*)&swapChain); -} - -#ifdef __ID3D12GraphicsCommandList4_INTERFACE_DEFINED__ -Result DeviceD3D12::CreateAccelerationStructure(const AccelerationStructureDesc& accelerationStructureDesc, AccelerationStructure*& accelerationStructure) -{ - return CreateImplementation(accelerationStructure, accelerationStructureDesc); -} - -Result DeviceD3D12::CreateAccelerationStructure(const AccelerationStructureD3D12Desc& accelerationStructureDesc, AccelerationStructure*& accelerationStructure) -{ - return CreateImplementation(accelerationStructure, accelerationStructureDesc); -} - -inline void DeviceD3D12::DestroyAccelerationStructure(AccelerationStructure& accelerationStructure) -{ - Deallocate(GetStdAllocator(), (AccelerationStructureD3D12*)&accelerationStructure); -} -#endif - -// m_FreeDescriptorLocks[type] must be acquired before calling this function Result DeviceD3D12::CreateCpuOnlyVisibleDescriptorHeap(D3D12_DESCRIPTOR_HEAP_TYPE type) { + // IMPORTANT: m_FreeDescriptorLocks[type] must be acquired before calling this function ExclusiveScope lock(m_DescriptorHeapLock); size_t heapIndex = m_DescriptorHeaps.size(); @@ -404,572 +394,559 @@ MemoryType DeviceD3D12::GetMemoryType(MemoryLocation memoryLocation, const D3D12 return ::GetMemoryType(memoryLocation, resourceDesc); } -//================================================================================================================ -// nri::Device -//================================================================================================================ -inline void DeviceD3D12::SetDebugName(const char* name) -{ - SET_D3D_DEBUG_OBJECT_NAME(m_Device, name); -} - -inline Result DeviceD3D12::GetCommandQueue(CommandQueueType commandQueueType, CommandQueue*& commandQueue) +void DeviceD3D12::UpdateDeviceDesc(bool enableValidation) { - ExclusiveScope lock(m_QueueLock); + D3D12_FEATURE_DATA_D3D12_OPTIONS options = {}; + m_Device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS, &options, sizeof(options)); - uint32_t queueIndex = (uint32_t)commandQueueType; + D3D12_FEATURE_DATA_D3D12_OPTIONS1 options1 = {}; + m_Device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS1, &options1, sizeof(options1)); - if (m_CommandQueues[queueIndex]) - { - commandQueue = (CommandQueue*)m_CommandQueues[queueIndex]; - return Result::SUCCESS; - } + D3D12_FEATURE_DATA_D3D12_OPTIONS2 options2 = {}; + m_Device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS2, &options2, sizeof(options2)); - Result result = CreateCommandQueue(commandQueueType, commandQueue); - if (result != Result::SUCCESS) - { - REPORT_ERROR(GetLog(), "Device::GetCommandQueue() failed."); - return result; - } + D3D12_FEATURE_DATA_D3D12_OPTIONS3 options3 = {}; + m_Device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS3, &options3, sizeof(options3)); - m_CommandQueues[queueIndex] = (CommandQueueD3D12*)commandQueue; + D3D12_FEATURE_DATA_D3D12_OPTIONS4 options4 = {}; + m_Device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS4, &options4, sizeof(options4)); - return Result::SUCCESS; -} + D3D12_FEATURE_DATA_D3D12_OPTIONS5 options5 = {}; + m_Device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS5, &options5, sizeof(options5)); -inline Result DeviceD3D12::CreateCommandQueue(CommandQueueType commandQueueType, CommandQueue*& commandQueue) -{ - return CreateImplementation(commandQueue, commandQueueType); -} + D3D12_FEATURE_DATA_D3D12_OPTIONS7 options7 = {}; + m_Device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS7, &options7, sizeof(options7)); -inline Result DeviceD3D12::CreateCommandQueue(void* d3d12commandQueue, CommandQueueD3D12*& commandQueue) -{ - return CreateImplementation(commandQueue, (ID3D12CommandQueue*)d3d12commandQueue); -} + m_IsRaytracingSupported = options5.RaytracingTier >= D3D12_RAYTRACING_TIER_1_0; + m_IsMeshShaderSupported = options7.MeshShaderTier >= D3D12_MESH_SHADER_TIER_1; -inline Result DeviceD3D12::CreateCommandAllocator(const CommandQueue& commandQueue, CommandAllocator*& commandAllocator) -{ - return CreateImplementation(commandAllocator, commandQueue); -} + D3D12_FEATURE_DATA_FEATURE_LEVELS levels = {}; + const std::array levelsList = { + D3D_FEATURE_LEVEL_11_0, + D3D_FEATURE_LEVEL_11_1, + D3D_FEATURE_LEVEL_12_0, + D3D_FEATURE_LEVEL_12_1, + }; + levels.NumFeatureLevels = (uint32_t)levelsList.size(); + levels.pFeatureLevelsRequested = levelsList.data(); + m_Device->CheckFeatureSupport(D3D12_FEATURE_FEATURE_LEVELS, &levels, sizeof(levels)); -inline Result DeviceD3D12::CreateDescriptorPool(const DescriptorPoolDesc& descriptorPoolDesc, DescriptorPool*& descriptorPool) -{ - return CreateImplementation(descriptorPool, descriptorPoolDesc); -} + uint64_t timestampFrequency = 0; -inline Result DeviceD3D12::CreateBuffer(const BufferDesc& bufferDesc, Buffer*& buffer) -{ - return CreateImplementation(buffer, bufferDesc); -} + CommandQueue* commandQueue = nullptr; + const Result result = GetCommandQueue(CommandQueueType::GRAPHICS, commandQueue); -inline Result DeviceD3D12::CreateTexture(const TextureDesc& textureDesc, Texture*& texture) -{ - return CreateImplementation(texture, textureDesc); -} + if (result == Result::SUCCESS) + { + ID3D12CommandQueue* commandQueueD3D12 = *(CommandQueueD3D12*)commandQueue; + commandQueueD3D12->GetTimestampFrequency(×tampFrequency); + } + else + { + REPORT_ERROR(GetLog(), "Failed to get command queue to update device desc, result: %d.", (int32_t)result); + } -inline Result DeviceD3D12::CreateDescriptor(const BufferViewDesc& bufferViewDesc, Descriptor*& bufferView) -{ - return CreateImplementation(bufferView, bufferViewDesc); -} + m_DeviceDesc.graphicsAPI = GraphicsAPI::D3D12; + m_DeviceDesc.vendor = GetVendor(m_Device); + m_DeviceDesc.nriVersionMajor = NRI_VERSION_MAJOR; + m_DeviceDesc.nriVersionMinor = NRI_VERSION_MINOR; -inline Result DeviceD3D12::CreateDescriptor(const Texture1DViewDesc& textureViewDesc, Descriptor*& textureView) -{ - return CreateImplementation(textureView, textureViewDesc); -} + m_DeviceDesc.viewportMaxNum = D3D12_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE; + m_DeviceDesc.viewportSubPixelBits = D3D12_SUBPIXEL_FRACTIONAL_BIT_COUNT; + m_DeviceDesc.viewportBoundsRange[0] = D3D12_VIEWPORT_BOUNDS_MIN; + m_DeviceDesc.viewportBoundsRange[1] = D3D12_VIEWPORT_BOUNDS_MAX; -inline Result DeviceD3D12::CreateDescriptor(const Texture2DViewDesc& textureViewDesc, Descriptor*& textureView) -{ - return CreateImplementation(textureView, textureViewDesc); -} + m_DeviceDesc.frameBufferMaxDim = D3D12_REQ_RENDER_TO_BUFFER_WINDOW_WIDTH; + m_DeviceDesc.frameBufferLayerMaxNum = D3D12_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; + m_DeviceDesc.framebufferColorAttachmentMaxNum = D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT; -inline Result DeviceD3D12::CreateDescriptor(const Texture3DViewDesc& textureViewDesc, Descriptor*& textureView) -{ - return CreateImplementation(textureView, textureViewDesc); -} + m_DeviceDesc.frameBufferColorSampleMaxNum = D3D12_MAX_MULTISAMPLE_SAMPLE_COUNT; + m_DeviceDesc.frameBufferDepthSampleMaxNum = D3D12_MAX_MULTISAMPLE_SAMPLE_COUNT; + m_DeviceDesc.frameBufferStencilSampleMaxNum = D3D12_MAX_MULTISAMPLE_SAMPLE_COUNT; + m_DeviceDesc.frameBufferNoAttachmentsSampleMaxNum = D3D12_MAX_MULTISAMPLE_SAMPLE_COUNT; + m_DeviceDesc.textureColorSampleMaxNum = D3D12_MAX_MULTISAMPLE_SAMPLE_COUNT; + m_DeviceDesc.textureIntegerSampleMaxNum = 1; + m_DeviceDesc.textureDepthSampleMaxNum = D3D12_MAX_MULTISAMPLE_SAMPLE_COUNT; + m_DeviceDesc.textureStencilSampleMaxNum = D3D12_MAX_MULTISAMPLE_SAMPLE_COUNT; + m_DeviceDesc.storageTextureSampleMaxNum = 1; -#ifdef __ID3D12GraphicsCommandList4_INTERFACE_DEFINED__ -Result DeviceD3D12::CreateDescriptor(const AccelerationStructure& accelerationStructure, Descriptor*& accelerationStructureView) // TODO: not inline -{ - return CreateImplementation(accelerationStructureView, accelerationStructure); -} -#endif + m_DeviceDesc.texture1DMaxDim = D3D12_REQ_TEXTURE1D_U_DIMENSION; + m_DeviceDesc.texture2DMaxDim = D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION; + m_DeviceDesc.texture3DMaxDim = D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; + m_DeviceDesc.textureArrayMaxDim = D3D12_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; + m_DeviceDesc.texelBufferMaxDim = (1 << D3D12_REQ_BUFFER_RESOURCE_TEXEL_COUNT_2_TO_EXP) - 1; -inline Result DeviceD3D12::CreateDescriptor(const SamplerDesc& samplerDesc, Descriptor*& sampler) -{ - return CreateImplementation(sampler, samplerDesc); -} + m_DeviceDesc.memoryAllocationMaxNum = 0xFFFFFFFF; + m_DeviceDesc.samplerAllocationMaxNum = D3D12_REQ_SAMPLER_OBJECT_COUNT_PER_DEVICE; + m_DeviceDesc.uploadBufferTextureRowAlignment = D3D12_TEXTURE_DATA_PITCH_ALIGNMENT; + m_DeviceDesc.uploadBufferTextureSliceAlignment = D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT; + m_DeviceDesc.typedBufferOffsetAlignment = D3D12_RAW_UAV_SRV_BYTE_ALIGNMENT; + m_DeviceDesc.constantBufferOffsetAlignment = D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT; + m_DeviceDesc.constantBufferMaxRange = D3D12_REQ_IMMEDIATE_CONSTANT_BUFFER_ELEMENT_COUNT * 16; + m_DeviceDesc.storageBufferOffsetAlignment = D3D12_RAW_UAV_SRV_BYTE_ALIGNMENT; + m_DeviceDesc.storageBufferMaxRange = (1 << D3D12_REQ_BUFFER_RESOURCE_TEXEL_COUNT_2_TO_EXP) - 1; + m_DeviceDesc.bufferTextureGranularity = 1; // TODO: 64KB? + m_DeviceDesc.bufferMaxSize = D3D12_REQ_RESOURCE_SIZE_IN_MEGABYTES_EXPRESSION_C_TERM * 1024ull * 1024ull; + m_DeviceDesc.pushConstantsMaxSize = D3D12_REQ_IMMEDIATE_CONSTANT_BUFFER_ELEMENT_COUNT * 16; -inline Result DeviceD3D12::CreatePipelineLayout(const PipelineLayoutDesc& pipelineLayoutDesc, PipelineLayout*& pipelineLayout) -{ - return CreateImplementation(pipelineLayout, pipelineLayoutDesc); -} + m_DeviceDesc.boundDescriptorSetMaxNum = D3D12_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT; + m_DeviceDesc.perStageDescriptorSamplerMaxNum = D3D12_COMMONSHADER_SAMPLER_SLOT_COUNT; + m_DeviceDesc.perStageDescriptorConstantBufferMaxNum = D3D12_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT; + m_DeviceDesc.perStageDescriptorStorageBufferMaxNum = levels.MaxSupportedFeatureLevel >= D3D_FEATURE_LEVEL_11_1 ? D3D12_UAV_SLOT_COUNT : D3D12_PS_CS_UAV_REGISTER_COUNT; + m_DeviceDesc.perStageDescriptorTextureMaxNum = D3D12_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT; + m_DeviceDesc.perStageDescriptorStorageTextureMaxNum = levels.MaxSupportedFeatureLevel >= D3D_FEATURE_LEVEL_11_1 ? D3D12_UAV_SLOT_COUNT : D3D12_PS_CS_UAV_REGISTER_COUNT; + m_DeviceDesc.perStageResourceMaxNum = D3D12_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT; -inline Result DeviceD3D12::CreatePipeline(const GraphicsPipelineDesc& graphicsPipelineDesc, Pipeline*& pipeline) -{ - return CreateImplementation(pipeline, graphicsPipelineDesc); -} + m_DeviceDesc.descriptorSetSamplerMaxNum = m_DeviceDesc.perStageDescriptorSamplerMaxNum; + m_DeviceDesc.descriptorSetConstantBufferMaxNum = m_DeviceDesc.perStageDescriptorConstantBufferMaxNum; + m_DeviceDesc.descriptorSetStorageBufferMaxNum = m_DeviceDesc.perStageDescriptorStorageBufferMaxNum; + m_DeviceDesc.descriptorSetTextureMaxNum = m_DeviceDesc.perStageDescriptorTextureMaxNum; + m_DeviceDesc.descriptorSetStorageTextureMaxNum = m_DeviceDesc.perStageDescriptorStorageTextureMaxNum; -inline Result DeviceD3D12::CreatePipeline(const ComputePipelineDesc& computePipelineDesc, Pipeline*& pipeline) -{ - return CreateImplementation(pipeline, computePipelineDesc); -} + m_DeviceDesc.vertexShaderAttributeMaxNum = D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT; + m_DeviceDesc.vertexShaderStreamMaxNum = D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT; + m_DeviceDesc.vertexShaderOutputComponentMaxNum = D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT * 4; -inline Result DeviceD3D12::CreatePipeline(const RayTracingPipelineDesc& rayTracingPipelineDesc, Pipeline*& pipeline) -{ - return CreateImplementation(pipeline, rayTracingPipelineDesc); -} + m_DeviceDesc.tessControlShaderGenerationMaxLevel = D3D12_HS_MAXTESSFACTOR_UPPER_BOUND; + m_DeviceDesc.tessControlShaderPatchPointMaxNum = D3D12_IA_PATCH_MAX_CONTROL_POINT_COUNT; + m_DeviceDesc.tessControlShaderPerVertexInputComponentMaxNum = D3D12_HS_CONTROL_POINT_PHASE_INPUT_REGISTER_COUNT * D3D12_HS_CONTROL_POINT_REGISTER_COMPONENTS; + m_DeviceDesc.tessControlShaderPerVertexOutputComponentMaxNum = D3D12_HS_CONTROL_POINT_PHASE_OUTPUT_REGISTER_COUNT * D3D12_HS_CONTROL_POINT_REGISTER_COMPONENTS; + m_DeviceDesc.tessControlShaderPerPatchOutputComponentMaxNum = D3D12_HS_OUTPUT_PATCH_CONSTANT_REGISTER_SCALAR_COMPONENTS; + m_DeviceDesc.tessControlShaderTotalOutputComponentMaxNum = m_DeviceDesc.tessControlShaderPatchPointMaxNum * m_DeviceDesc.tessControlShaderPerVertexOutputComponentMaxNum + m_DeviceDesc.tessControlShaderPerPatchOutputComponentMaxNum; -inline Result DeviceD3D12::CreateFrameBuffer(const FrameBufferDesc& frameBufferDesc, FrameBuffer*& frameBuffer) -{ - return CreateImplementation(frameBuffer, frameBufferDesc); -} + m_DeviceDesc.tessEvaluationShaderInputComponentMaxNum = D3D12_DS_INPUT_CONTROL_POINT_REGISTER_COUNT * D3D12_DS_INPUT_CONTROL_POINT_REGISTER_COMPONENTS; + m_DeviceDesc.tessEvaluationShaderOutputComponentMaxNum = D3D12_DS_INPUT_CONTROL_POINT_REGISTER_COUNT * D3D12_DS_INPUT_CONTROL_POINT_REGISTER_COMPONENTS; -inline Result DeviceD3D12::CreateQueryPool(const QueryPoolDesc& queryPoolDesc, QueryPool*& queryPool) -{ - return CreateImplementation(queryPool, queryPoolDesc); -} + m_DeviceDesc.geometryShaderInvocationMaxNum = D3D12_GS_MAX_INSTANCE_COUNT; + m_DeviceDesc.geometryShaderInputComponentMaxNum = D3D12_GS_INPUT_REGISTER_COUNT * D3D12_GS_INPUT_REGISTER_COMPONENTS; + m_DeviceDesc.geometryShaderOutputComponentMaxNum = D3D12_GS_OUTPUT_REGISTER_COUNT * D3D12_GS_INPUT_REGISTER_COMPONENTS; + m_DeviceDesc.geometryShaderOutputVertexMaxNum = D3D12_GS_MAX_OUTPUT_VERTEX_COUNT_ACROSS_INSTANCES; + m_DeviceDesc.geometryShaderTotalOutputComponentMaxNum = D3D12_REQ_GS_INVOCATION_32BIT_OUTPUT_COMPONENT_LIMIT; -inline Result DeviceD3D12::CreateQueueSemaphore(QueueSemaphore*& queueSemaphore) -{ - return CreateImplementation(queueSemaphore); -} + m_DeviceDesc.fragmentShaderInputComponentMaxNum = D3D12_PS_INPUT_REGISTER_COUNT * D3D12_PS_INPUT_REGISTER_COMPONENTS; + m_DeviceDesc.fragmentShaderOutputAttachmentMaxNum = D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT; + m_DeviceDesc.fragmentShaderDualSourceAttachmentMaxNum = 1; + m_DeviceDesc.fragmentShaderCombinedOutputResourceMaxNum = D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT + D3D12_PS_CS_UAV_REGISTER_COUNT; -inline Result DeviceD3D12::CreateDeviceSemaphore(bool signaled, DeviceSemaphore*& deviceSemaphore) -{ - return CreateImplementation(deviceSemaphore, signaled); -} + m_DeviceDesc.computeShaderSharedMemoryMaxSize = D3D12_CS_THREAD_LOCAL_TEMP_REGISTER_POOL; + m_DeviceDesc.computeShaderWorkGroupMaxNum[0] = D3D12_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION; + m_DeviceDesc.computeShaderWorkGroupMaxNum[1] = D3D12_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION; + m_DeviceDesc.computeShaderWorkGroupMaxNum[2] = D3D12_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION; + m_DeviceDesc.computeShaderWorkGroupInvocationMaxNum = D3D12_CS_THREAD_GROUP_MAX_THREADS_PER_GROUP; + m_DeviceDesc.computeShaderWorkGroupMaxDim[0] = D3D12_CS_THREAD_GROUP_MAX_X; + m_DeviceDesc.computeShaderWorkGroupMaxDim[1] = D3D12_CS_THREAD_GROUP_MAX_Y; + m_DeviceDesc.computeShaderWorkGroupMaxDim[2] = D3D12_CS_THREAD_GROUP_MAX_Z; -inline Result DeviceD3D12::CreateCommandBuffer(const CommandBufferD3D12Desc& commandBufferDesc, CommandBuffer*& commandBuffer) -{ - return CreateImplementation(commandBuffer, commandBufferDesc); -} + if (m_IsRaytracingSupported) + { + m_DeviceDesc.rayTracingShaderGroupIdentifierSize = D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT; + m_DeviceDesc.rayTracingShaderTableAligment = D3D12_RAYTRACING_SHADER_TABLE_BYTE_ALIGNMENT; + m_DeviceDesc.rayTracingShaderTableMaxStride = std::numeric_limits::max(); + m_DeviceDesc.rayTracingShaderRecursionMaxDepth = D3D12_RAYTRACING_MAX_DECLARABLE_TRACE_RECURSION_DEPTH; + m_DeviceDesc.rayTracingGeometryObjectMaxNum = (1 << 24) - 1; + } -Result DeviceD3D12::CreateBuffer(const BufferD3D12Desc& bufferDesc, Buffer*& buffer) // TODO: not inline -{ - return CreateImplementation(buffer, bufferDesc); -} + m_DeviceDesc.timestampFrequencyHz = timestampFrequency; + m_DeviceDesc.subPixelPrecisionBits = D3D12_SUBPIXEL_FRACTIONAL_BIT_COUNT; + m_DeviceDesc.subTexelPrecisionBits = D3D12_SUBTEXEL_FRACTIONAL_BIT_COUNT; + m_DeviceDesc.mipmapPrecisionBits = D3D12_MIP_LOD_FRACTIONAL_BIT_COUNT; + m_DeviceDesc.drawIndexedIndex16ValueMax = D3D12_16BIT_INDEX_STRIP_CUT_VALUE; + m_DeviceDesc.drawIndexedIndex32ValueMax = D3D12_32BIT_INDEX_STRIP_CUT_VALUE; + m_DeviceDesc.drawIndirectMaxNum = (1ull << D3D12_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP) - 1; + m_DeviceDesc.samplerLodBiasMin = D3D12_MIP_LOD_BIAS_MIN; + m_DeviceDesc.samplerLodBiasMax = D3D12_MIP_LOD_BIAS_MAX; + m_DeviceDesc.samplerAnisotropyMax = D3D12_DEFAULT_MAX_ANISOTROPY; + m_DeviceDesc.texelOffsetMin = D3D12_COMMONSHADER_TEXEL_OFFSET_MAX_NEGATIVE; + m_DeviceDesc.texelOffsetMax = D3D12_COMMONSHADER_TEXEL_OFFSET_MAX_POSITIVE; + m_DeviceDesc.texelGatherOffsetMin = D3D12_COMMONSHADER_TEXEL_OFFSET_MAX_NEGATIVE; + m_DeviceDesc.texelGatherOffsetMax = D3D12_COMMONSHADER_TEXEL_OFFSET_MAX_POSITIVE; + m_DeviceDesc.clipDistanceMaxNum = D3D12_CLIP_OR_CULL_DISTANCE_COUNT; + m_DeviceDesc.cullDistanceMaxNum = D3D12_CLIP_OR_CULL_DISTANCE_COUNT; + m_DeviceDesc.combinedClipAndCullDistanceMaxNum = D3D12_CLIP_OR_CULL_DISTANCE_COUNT; + m_DeviceDesc.conservativeRasterTier = (uint8_t)options.ConservativeRasterizationTier; + m_DeviceDesc.physicalDeviceNum = (uint8_t)m_Device->GetNodeCount(); -inline Result DeviceD3D12::CreateTexture(const TextureD3D12Desc& textureDesc, Texture*& texture) -{ - return CreateImplementation(texture, textureDesc); + m_DeviceDesc.isAPIValidationEnabled = enableValidation; + m_DeviceDesc.isTextureFilterMinMaxSupported = levels.MaxSupportedFeatureLevel >= D3D_FEATURE_LEVEL_11_1 ? true : false; + m_DeviceDesc.isLogicOpSupported = options.OutputMergerLogicOp != 0; + m_DeviceDesc.isDepthBoundsTestSupported = options2.DepthBoundsTestSupported != 0; + m_DeviceDesc.isProgrammableSampleLocationsSupported = options2.ProgrammableSamplePositionsTier != D3D12_PROGRAMMABLE_SAMPLE_POSITIONS_TIER_NOT_SUPPORTED; + m_DeviceDesc.isComputeQueueSupported = true; + m_DeviceDesc.isCopyQueueSupported = true; + m_DeviceDesc.isCopyQueueTimestampSupported = options3.CopyQueueTimestampQueriesSupported != 0; + m_DeviceDesc.isRegisterAliasingSupported = true; + m_DeviceDesc.isSubsetAllocationSupported = true; + m_DeviceDesc.isFloat16Supported = options4.Native16BitShaderOpsSupported; } -inline Result DeviceD3D12::CreateMemory(const MemoryD3D12Desc& memoryDesc, Memory*& memory) -{ - return CreateImplementation(memory, memoryDesc); -} +//================================================================================================================ +// DeviceBase +//================================================================================================================ -inline void DeviceD3D12::DestroyCommandAllocator(CommandAllocator& commandAllocator) +void DeviceD3D12::Destroy() { - Deallocate(GetStdAllocator(), (CommandAllocatorD3D12*)&commandAllocator); -} + bool skipLiveObjectsReporting = m_SkipLiveObjectsReporting; + Deallocate(GetStdAllocator(), this); -inline void DeviceD3D12::DestroyDescriptorPool(DescriptorPool& descriptorPool) -{ - Deallocate(GetStdAllocator(), (DescriptorPoolD3D12*)&descriptorPool); + if (!skipLiveObjectsReporting) + { + ComPtr pDebug; + HRESULT hr = DXGIGetDebugInterface1(0, IID_PPV_ARGS(&pDebug)); + if (SUCCEEDED(hr)) + pDebug->ReportLiveObjects(DXGI_DEBUG_ALL, (DXGI_DEBUG_RLO_FLAGS)((uint32_t)DXGI_DEBUG_RLO_DETAIL | (uint32_t)DXGI_DEBUG_RLO_IGNORE_INTERNAL)); + } } -inline void DeviceD3D12::DestroyBuffer(Buffer& buffer) -{ - Deallocate(GetStdAllocator(), (BufferD3D12*)&buffer); -} +//================================================================================================================ +// NRI +//================================================================================================================ -inline void DeviceD3D12::DestroyTexture(Texture& texture) +inline Result DeviceD3D12::CreateSwapChain(const SwapChainDesc& swapChainDesc, SwapChain*& swapChain) { - Deallocate(GetStdAllocator(), (TextureD3D12*)&texture); + return CreateImplementation(swapChain, swapChainDesc); } -inline void DeviceD3D12::DestroyDescriptor(Descriptor& descriptor) +inline void DeviceD3D12::DestroySwapChain(SwapChain& swapChain) { - Deallocate(GetStdAllocator(), (DescriptorD3D12*)&descriptor); + Deallocate(GetStdAllocator(), (SwapChainD3D12*)&swapChain); } -inline void DeviceD3D12::DestroyPipelineLayout(PipelineLayout& pipelineLayout) +inline Result DeviceD3D12::GetDisplays(Display** displays, uint32_t& displayNum) { - Deallocate(GetStdAllocator(), (PipelineLayoutD3D12*)&pipelineLayout); -} + HRESULT result = S_OK; -inline void DeviceD3D12::DestroyPipeline(Pipeline& pipeline) -{ - Deallocate(GetStdAllocator(), (PipelineD3D12*)&pipeline); -} + if (displays == nullptr || displayNum == 0) + { + UINT i = 0; + for(; result != DXGI_ERROR_NOT_FOUND; i++) + { + ComPtr output; + result = m_Adapter->EnumOutputs(i, &output); + } -inline void DeviceD3D12::DestroyFrameBuffer(FrameBuffer& frameBuffer) -{ - Deallocate(GetStdAllocator(), (FrameBufferD3D12*)&frameBuffer); -} + displayNum = i; + return Result::SUCCESS; + } -inline void DeviceD3D12::DestroyQueryPool(QueryPool& queryPool) -{ - Deallocate(GetStdAllocator(), (QueryPoolD3D12*)&queryPool); -} + UINT i = 0; + for(; result != DXGI_ERROR_NOT_FOUND && i < displayNum; i++) + { + ComPtr output; + result = m_Adapter->EnumOutputs(i, &output); + if (result != DXGI_ERROR_NOT_FOUND) + displays[i] = (Display*)(size_t)(i + 1); + } -inline void DeviceD3D12::DestroyQueueSemaphore(QueueSemaphore& queueSemaphore) -{ - Deallocate(GetStdAllocator(), (QueueSemaphoreD3D12*)&queueSemaphore); -} + for(; i < displayNum; i++) + displays[i] = nullptr; -inline void DeviceD3D12::DestroyDeviceSemaphore(DeviceSemaphore& deviceSemaphore) -{ - Deallocate(GetStdAllocator(), (DeviceSemaphoreD3D12*)&deviceSemaphore); + return Result::SUCCESS; } -inline Result DeviceD3D12::AllocateMemory(const MemoryType memoryType, uint64_t size, Memory*& memory) +inline Result DeviceD3D12::GetDisplaySize(Display& display, uint16_t& width, uint16_t& height) { - return CreateImplementation(memory, memoryType, size); -} + Display* address = &display; -inline Result DeviceD3D12::BindBufferMemory(const BufferMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum) -{ - for (uint32_t i = 0; i < memoryBindingDescNum; i++) - { - Result result = ((BufferD3D12*)memoryBindingDescs[i].buffer)->BindMemory((MemoryD3D12*)memoryBindingDescs[i].memory, memoryBindingDescs[i].offset); - if (result != Result::SUCCESS) - return result; - } + if (address == nullptr) + return Result::UNSUPPORTED; + + const uint32_t index = (*(uint32_t*)&address) - 1; + + ComPtr output; + HRESULT result = m_Adapter->EnumOutputs(index, &output); + + if (FAILED(result)) + return Result::UNSUPPORTED; + + DXGI_OUTPUT_DESC outputDesc = {}; + result = output->GetDesc(&outputDesc); + + if (FAILED(result)) + return Result::UNSUPPORTED; + + MONITORINFO monitorInfo = {}; + monitorInfo.cbSize = sizeof(monitorInfo); + + if (!GetMonitorInfoA(outputDesc.Monitor, &monitorInfo)) + return Result::UNSUPPORTED; + + const RECT rect = monitorInfo.rcMonitor; + + width = uint16_t(rect.right - rect.left); + height = uint16_t(rect.bottom - rect.top); return Result::SUCCESS; } -inline Result DeviceD3D12::BindTextureMemory(const TextureMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum) +inline Result DeviceD3D12::GetCommandQueue(CommandQueueType commandQueueType, CommandQueue*& commandQueue) { - for (uint32_t i = 0; i < memoryBindingDescNum; i++) + ExclusiveScope lock(m_QueueLock); + + uint32_t queueIndex = (uint32_t)commandQueueType; + + if (m_CommandQueues[queueIndex]) { - Result result = ((TextureD3D12*)memoryBindingDescs[i].texture)->BindMemory((MemoryD3D12*)memoryBindingDescs[i].memory, memoryBindingDescs[i].offset); - if (result != Result::SUCCESS) - return result; + commandQueue = (CommandQueue*)m_CommandQueues[queueIndex]; + return Result::SUCCESS; } - return Result::SUCCESS; -} - -#ifdef __ID3D12GraphicsCommandList4_INTERFACE_DEFINED__ -inline Result DeviceD3D12::BindAccelerationStructureMemory(const AccelerationStructureMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum) -{ - for (uint32_t i = 0; i < memoryBindingDescNum; i++) + Result result = CreateCommandQueue(commandQueueType, commandQueue); + if (result != Result::SUCCESS) { - Result result = ((AccelerationStructureD3D12*)memoryBindingDescs[i].accelerationStructure)->BindMemory(memoryBindingDescs[i].memory, memoryBindingDescs[i].offset); - if (result != Result::SUCCESS) - return result; + REPORT_ERROR(GetLog(), "Device::GetCommandQueue() failed."); + return result; } + m_CommandQueues[queueIndex] = (CommandQueueD3D12*)commandQueue; + return Result::SUCCESS; } -#endif -inline void DeviceD3D12::FreeMemory(Memory& memory) +inline Result DeviceD3D12::CreateCommandQueue(CommandQueueType commandQueueType, CommandQueue*& commandQueue) { - Deallocate(GetStdAllocator(), (MemoryD3D12*)&memory); + return CreateImplementation(commandQueue, commandQueueType); } -inline FormatSupportBits DeviceD3D12::GetFormatSupport(Format format) const +inline Result DeviceD3D12::CreateCommandQueue(void* d3d12commandQueue, CommandQueueD3D12*& commandQueue) { - const uint32_t offset = std::min((uint32_t)format, (uint32_t)GetCountOf(D3D_FORMAT_SUPPORT_TABLE) - 1); - - return D3D_FORMAT_SUPPORT_TABLE[offset]; + return CreateImplementation(commandQueue, (ID3D12CommandQueue*)d3d12commandQueue); } -inline uint32_t DeviceD3D12::CalculateAllocationNumber(const ResourceGroupDesc& resourceGroupDesc) const +inline Result DeviceD3D12::CreateCommandAllocator(const CommandQueue& commandQueue, CommandAllocator*& commandAllocator) { - HelperDeviceMemoryAllocator allocator(m_CoreInterface, (Device&)*this, m_StdAllocator); - - return allocator.CalculateAllocationNumber(resourceGroupDesc); + return CreateImplementation(commandAllocator, commandQueue); } -inline Result DeviceD3D12::AllocateAndBindMemory(const ResourceGroupDesc& resourceGroupDesc, nri::Memory** allocations) +inline Result DeviceD3D12::CreateDescriptorPool(const DescriptorPoolDesc& descriptorPoolDesc, DescriptorPool*& descriptorPool) { - HelperDeviceMemoryAllocator allocator(m_CoreInterface, (Device&)*this, m_StdAllocator); - - return allocator.AllocateAndBindMemory(resourceGroupDesc, allocations); + return CreateImplementation(descriptorPool, descriptorPoolDesc); } -template -Result DeviceD3D12::CreateImplementation(Interface*& entity, const Args&... args) +inline Result DeviceD3D12::CreateBuffer(const BufferDesc& bufferDesc, Buffer*& buffer) { - Implementation* implementation = Allocate(GetStdAllocator(), *this); - const Result result = implementation->Create(args...); - - if (result == Result::SUCCESS) - { - entity = (Interface*)implementation; - return Result::SUCCESS; - } - - Deallocate(GetStdAllocator(), implementation); - - return result; + return CreateImplementation(buffer, bufferDesc); } -inline Vendor GetVendor(ID3D12Device* device) +inline Result DeviceD3D12::CreateTexture(const TextureDesc& textureDesc, Texture*& texture) { - ComPtr DXGIFactory; - CreateDXGIFactory(IID_PPV_ARGS(&DXGIFactory)); - - DXGI_ADAPTER_DESC desc = {}; - if (DXGIFactory) - { - LUID luid = device->GetAdapterLuid(); - - ComPtr adapter; - DXGIFactory->EnumAdapterByLuid(luid, IID_PPV_ARGS(&adapter)); - if (adapter) - adapter->GetDesc(&desc); - } - - return GetVendorFromID(desc.VendorId); + return CreateImplementation(texture, textureDesc); } -void DeviceD3D12::UpdateDeviceDesc(bool enableValidation) +inline Result DeviceD3D12::CreateDescriptor(const BufferViewDesc& bufferViewDesc, Descriptor*& bufferView) { - D3D12_FEATURE_DATA_D3D12_OPTIONS options = {}; - m_Device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS, &options, sizeof(options)); - - D3D12_FEATURE_DATA_D3D12_OPTIONS1 options1 = {}; - m_Device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS1, &options1, sizeof(options1)); - - D3D12_FEATURE_DATA_D3D12_OPTIONS2 options2 = {}; - m_Device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS2, &options2, sizeof(options2)); - - D3D12_FEATURE_DATA_D3D12_OPTIONS3 options3 = {}; - m_Device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS3, &options3, sizeof(options3)); - - D3D12_FEATURE_DATA_D3D12_OPTIONS4 options4 = {}; - m_Device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS4, &options4, sizeof(options4)); - - D3D12_FEATURE_DATA_D3D12_OPTIONS5 options5 = {}; - m_Device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS5, &options5, sizeof(options5)); - - D3D12_FEATURE_DATA_D3D12_OPTIONS7 options7 = {}; - m_Device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS7, &options7, sizeof(options7)); - - m_IsRaytracingSupported = options5.RaytracingTier >= D3D12_RAYTRACING_TIER_1_0; - m_IsMeshShaderSupported = options7.MeshShaderTier >= D3D12_MESH_SHADER_TIER_1; - - D3D12_FEATURE_DATA_FEATURE_LEVELS levels = {}; - const std::array levelsList = { - D3D_FEATURE_LEVEL_11_0, - D3D_FEATURE_LEVEL_11_1, - D3D_FEATURE_LEVEL_12_0, - D3D_FEATURE_LEVEL_12_1, - }; - levels.NumFeatureLevels = (uint32_t)levelsList.size(); - levels.pFeatureLevelsRequested = levelsList.data(); - m_Device->CheckFeatureSupport(D3D12_FEATURE_FEATURE_LEVELS, &levels, sizeof(levels)); - - uint64_t timestampFrequency = 0; - - CommandQueue* commandQueue = nullptr; - const Result result = GetCommandQueue(CommandQueueType::GRAPHICS, commandQueue); + return CreateImplementation(bufferView, bufferViewDesc); +} - if (result == Result::SUCCESS) - { - ID3D12CommandQueue* commandQueueD3D12 = *(CommandQueueD3D12*)commandQueue; - commandQueueD3D12->GetTimestampFrequency(×tampFrequency); - } - else - { - REPORT_ERROR(GetLog(), "Failed to get command queue to update device desc, result: %d.", (int32_t)result); - } +inline Result DeviceD3D12::CreateDescriptor(const Texture1DViewDesc& textureViewDesc, Descriptor*& textureView) +{ + return CreateImplementation(textureView, textureViewDesc); +} - m_DeviceDesc.graphicsAPI = GraphicsAPI::D3D12; - m_DeviceDesc.vendor = GetVendor(m_Device); - m_DeviceDesc.nriVersionMajor = NRI_VERSION_MAJOR; - m_DeviceDesc.nriVersionMinor = NRI_VERSION_MINOR; +inline Result DeviceD3D12::CreateDescriptor(const Texture2DViewDesc& textureViewDesc, Descriptor*& textureView) +{ + return CreateImplementation(textureView, textureViewDesc); +} - m_DeviceDesc.viewportMaxNum = D3D12_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE; - m_DeviceDesc.viewportSubPixelBits = D3D12_SUBPIXEL_FRACTIONAL_BIT_COUNT; - m_DeviceDesc.viewportBoundsRange[0] = D3D12_VIEWPORT_BOUNDS_MIN; - m_DeviceDesc.viewportBoundsRange[1] = D3D12_VIEWPORT_BOUNDS_MAX; +inline Result DeviceD3D12::CreateDescriptor(const Texture3DViewDesc& textureViewDesc, Descriptor*& textureView) +{ + return CreateImplementation(textureView, textureViewDesc); +} - m_DeviceDesc.frameBufferMaxDim = D3D12_REQ_RENDER_TO_BUFFER_WINDOW_WIDTH; - m_DeviceDesc.frameBufferLayerMaxNum = D3D12_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; - m_DeviceDesc.framebufferColorAttachmentMaxNum = D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT; +Result DeviceD3D12::CreateDescriptor(const AccelerationStructure& accelerationStructure, Descriptor*& accelerationStructureView) // TODO: not inline +{ + return CreateImplementation(accelerationStructureView, accelerationStructure); +} - m_DeviceDesc.frameBufferColorSampleMaxNum = D3D12_MAX_MULTISAMPLE_SAMPLE_COUNT; - m_DeviceDesc.frameBufferDepthSampleMaxNum = D3D12_MAX_MULTISAMPLE_SAMPLE_COUNT; - m_DeviceDesc.frameBufferStencilSampleMaxNum = D3D12_MAX_MULTISAMPLE_SAMPLE_COUNT; - m_DeviceDesc.frameBufferNoAttachmentsSampleMaxNum = D3D12_MAX_MULTISAMPLE_SAMPLE_COUNT; - m_DeviceDesc.textureColorSampleMaxNum = D3D12_MAX_MULTISAMPLE_SAMPLE_COUNT; - m_DeviceDesc.textureIntegerSampleMaxNum = 1; - m_DeviceDesc.textureDepthSampleMaxNum = D3D12_MAX_MULTISAMPLE_SAMPLE_COUNT; - m_DeviceDesc.textureStencilSampleMaxNum = D3D12_MAX_MULTISAMPLE_SAMPLE_COUNT; - m_DeviceDesc.storageTextureSampleMaxNum = 1; +inline Result DeviceD3D12::CreateDescriptor(const SamplerDesc& samplerDesc, Descriptor*& sampler) +{ + return CreateImplementation(sampler, samplerDesc); +} - m_DeviceDesc.texture1DMaxDim = D3D12_REQ_TEXTURE1D_U_DIMENSION; - m_DeviceDesc.texture2DMaxDim = D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION; - m_DeviceDesc.texture3DMaxDim = D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; - m_DeviceDesc.textureArrayMaxDim = D3D12_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; - m_DeviceDesc.texelBufferMaxDim = (1 << D3D12_REQ_BUFFER_RESOURCE_TEXEL_COUNT_2_TO_EXP) - 1; +inline Result DeviceD3D12::CreatePipelineLayout(const PipelineLayoutDesc& pipelineLayoutDesc, PipelineLayout*& pipelineLayout) +{ + return CreateImplementation(pipelineLayout, pipelineLayoutDesc); +} - m_DeviceDesc.memoryAllocationMaxNum = 0xFFFFFFFF; - m_DeviceDesc.samplerAllocationMaxNum = D3D12_REQ_SAMPLER_OBJECT_COUNT_PER_DEVICE; - m_DeviceDesc.uploadBufferTextureRowAlignment = D3D12_TEXTURE_DATA_PITCH_ALIGNMENT; - m_DeviceDesc.uploadBufferTextureSliceAlignment = D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT; - m_DeviceDesc.typedBufferOffsetAlignment = D3D12_RAW_UAV_SRV_BYTE_ALIGNMENT; - m_DeviceDesc.constantBufferOffsetAlignment = D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT; - m_DeviceDesc.constantBufferMaxRange = D3D12_REQ_IMMEDIATE_CONSTANT_BUFFER_ELEMENT_COUNT * 16; - m_DeviceDesc.storageBufferOffsetAlignment = D3D12_RAW_UAV_SRV_BYTE_ALIGNMENT; - m_DeviceDesc.storageBufferMaxRange = (1 << D3D12_REQ_BUFFER_RESOURCE_TEXEL_COUNT_2_TO_EXP) - 1; - m_DeviceDesc.bufferTextureGranularity = 1; // TODO: 64KB? - m_DeviceDesc.bufferMaxSize = D3D12_REQ_RESOURCE_SIZE_IN_MEGABYTES_EXPRESSION_C_TERM * 1024ull * 1024ull; - m_DeviceDesc.pushConstantsMaxSize = D3D12_REQ_IMMEDIATE_CONSTANT_BUFFER_ELEMENT_COUNT * 16; +inline Result DeviceD3D12::CreatePipeline(const GraphicsPipelineDesc& graphicsPipelineDesc, Pipeline*& pipeline) +{ + return CreateImplementation(pipeline, graphicsPipelineDesc); +} - m_DeviceDesc.boundDescriptorSetMaxNum = D3D12_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT; - m_DeviceDesc.perStageDescriptorSamplerMaxNum = D3D12_COMMONSHADER_SAMPLER_SLOT_COUNT; - m_DeviceDesc.perStageDescriptorConstantBufferMaxNum = D3D12_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT; - m_DeviceDesc.perStageDescriptorStorageBufferMaxNum = levels.MaxSupportedFeatureLevel >= D3D_FEATURE_LEVEL_11_1 ? D3D12_UAV_SLOT_COUNT : D3D12_PS_CS_UAV_REGISTER_COUNT; - m_DeviceDesc.perStageDescriptorTextureMaxNum = D3D12_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT; - m_DeviceDesc.perStageDescriptorStorageTextureMaxNum = levels.MaxSupportedFeatureLevel >= D3D_FEATURE_LEVEL_11_1 ? D3D12_UAV_SLOT_COUNT : D3D12_PS_CS_UAV_REGISTER_COUNT; - m_DeviceDesc.perStageResourceMaxNum = D3D12_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT; +inline Result DeviceD3D12::CreatePipeline(const ComputePipelineDesc& computePipelineDesc, Pipeline*& pipeline) +{ + return CreateImplementation(pipeline, computePipelineDesc); +} - m_DeviceDesc.descriptorSetSamplerMaxNum = m_DeviceDesc.perStageDescriptorSamplerMaxNum; - m_DeviceDesc.descriptorSetConstantBufferMaxNum = m_DeviceDesc.perStageDescriptorConstantBufferMaxNum; - m_DeviceDesc.descriptorSetStorageBufferMaxNum = m_DeviceDesc.perStageDescriptorStorageBufferMaxNum; - m_DeviceDesc.descriptorSetTextureMaxNum = m_DeviceDesc.perStageDescriptorTextureMaxNum; - m_DeviceDesc.descriptorSetStorageTextureMaxNum = m_DeviceDesc.perStageDescriptorStorageTextureMaxNum; +inline Result DeviceD3D12::CreatePipeline(const RayTracingPipelineDesc& rayTracingPipelineDesc, Pipeline*& pipeline) +{ + return CreateImplementation(pipeline, rayTracingPipelineDesc); +} - m_DeviceDesc.vertexShaderAttributeMaxNum = D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT; - m_DeviceDesc.vertexShaderStreamMaxNum = D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT; - m_DeviceDesc.vertexShaderOutputComponentMaxNum = D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT * 4; +inline Result DeviceD3D12::CreateFence(uint64_t initialValue, Fence*& fence) +{ + return CreateImplementation(fence, initialValue); +} - m_DeviceDesc.tessControlShaderGenerationMaxLevel = D3D12_HS_MAXTESSFACTOR_UPPER_BOUND; - m_DeviceDesc.tessControlShaderPatchPointMaxNum = D3D12_IA_PATCH_MAX_CONTROL_POINT_COUNT; - m_DeviceDesc.tessControlShaderPerVertexInputComponentMaxNum = D3D12_HS_CONTROL_POINT_PHASE_INPUT_REGISTER_COUNT * D3D12_HS_CONTROL_POINT_REGISTER_COMPONENTS; - m_DeviceDesc.tessControlShaderPerVertexOutputComponentMaxNum = D3D12_HS_CONTROL_POINT_PHASE_OUTPUT_REGISTER_COUNT * D3D12_HS_CONTROL_POINT_REGISTER_COMPONENTS; - m_DeviceDesc.tessControlShaderPerPatchOutputComponentMaxNum = D3D12_HS_OUTPUT_PATCH_CONSTANT_REGISTER_SCALAR_COMPONENTS; - m_DeviceDesc.tessControlShaderTotalOutputComponentMaxNum = m_DeviceDesc.tessControlShaderPatchPointMaxNum * m_DeviceDesc.tessControlShaderPerVertexOutputComponentMaxNum + m_DeviceDesc.tessControlShaderPerPatchOutputComponentMaxNum; +inline Result DeviceD3D12::CreateFrameBuffer(const FrameBufferDesc& frameBufferDesc, FrameBuffer*& frameBuffer) +{ + return CreateImplementation(frameBuffer, frameBufferDesc); +} - m_DeviceDesc.tessEvaluationShaderInputComponentMaxNum = D3D12_DS_INPUT_CONTROL_POINT_REGISTER_COUNT * D3D12_DS_INPUT_CONTROL_POINT_REGISTER_COMPONENTS; - m_DeviceDesc.tessEvaluationShaderOutputComponentMaxNum = D3D12_DS_INPUT_CONTROL_POINT_REGISTER_COUNT * D3D12_DS_INPUT_CONTROL_POINT_REGISTER_COMPONENTS; +inline Result DeviceD3D12::CreateQueryPool(const QueryPoolDesc& queryPoolDesc, QueryPool*& queryPool) +{ + return CreateImplementation(queryPool, queryPoolDesc); +} - m_DeviceDesc.geometryShaderInvocationMaxNum = D3D12_GS_MAX_INSTANCE_COUNT; - m_DeviceDesc.geometryShaderInputComponentMaxNum = D3D12_GS_INPUT_REGISTER_COUNT * D3D12_GS_INPUT_REGISTER_COMPONENTS; - m_DeviceDesc.geometryShaderOutputComponentMaxNum = D3D12_GS_OUTPUT_REGISTER_COUNT * D3D12_GS_INPUT_REGISTER_COMPONENTS; - m_DeviceDesc.geometryShaderOutputVertexMaxNum = D3D12_GS_MAX_OUTPUT_VERTEX_COUNT_ACROSS_INSTANCES; - m_DeviceDesc.geometryShaderTotalOutputComponentMaxNum = D3D12_REQ_GS_INVOCATION_32BIT_OUTPUT_COMPONENT_LIMIT; +inline Result DeviceD3D12::CreateCommandBuffer(const CommandBufferD3D12Desc& commandBufferDesc, CommandBuffer*& commandBuffer) +{ + return CreateImplementation(commandBuffer, commandBufferDesc); +} - m_DeviceDesc.fragmentShaderInputComponentMaxNum = D3D12_PS_INPUT_REGISTER_COUNT * D3D12_PS_INPUT_REGISTER_COMPONENTS; - m_DeviceDesc.fragmentShaderOutputAttachmentMaxNum = D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT; - m_DeviceDesc.fragmentShaderDualSourceAttachmentMaxNum = 1; - m_DeviceDesc.fragmentShaderCombinedOutputResourceMaxNum = D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT + D3D12_PS_CS_UAV_REGISTER_COUNT; +Result DeviceD3D12::CreateBuffer(const BufferD3D12Desc& bufferDesc, Buffer*& buffer) // TODO: not inline +{ + return CreateImplementation(buffer, bufferDesc); +} - m_DeviceDesc.computeShaderSharedMemoryMaxSize = D3D12_CS_THREAD_LOCAL_TEMP_REGISTER_POOL; - m_DeviceDesc.computeShaderWorkGroupMaxNum[0] = D3D12_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION; - m_DeviceDesc.computeShaderWorkGroupMaxNum[1] = D3D12_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION; - m_DeviceDesc.computeShaderWorkGroupMaxNum[2] = D3D12_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION; - m_DeviceDesc.computeShaderWorkGroupInvocationMaxNum = D3D12_CS_THREAD_GROUP_MAX_THREADS_PER_GROUP; - m_DeviceDesc.computeShaderWorkGroupMaxDim[0] = D3D12_CS_THREAD_GROUP_MAX_X; - m_DeviceDesc.computeShaderWorkGroupMaxDim[1] = D3D12_CS_THREAD_GROUP_MAX_Y; - m_DeviceDesc.computeShaderWorkGroupMaxDim[2] = D3D12_CS_THREAD_GROUP_MAX_Z; +inline Result DeviceD3D12::CreateTexture(const TextureD3D12Desc& textureDesc, Texture*& texture) +{ + return CreateImplementation(texture, textureDesc); +} -#ifdef __ID3D12Device5_INTERFACE_DEFINED__ - if (m_IsRaytracingSupported) - { - m_DeviceDesc.rayTracingShaderGroupIdentifierSize = D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT; - m_DeviceDesc.rayTracingShaderTableAligment = D3D12_RAYTRACING_SHADER_TABLE_BYTE_ALIGNMENT; - m_DeviceDesc.rayTracingShaderTableMaxStride = std::numeric_limits::max(); - m_DeviceDesc.rayTracingShaderRecursionMaxDepth = D3D12_RAYTRACING_MAX_DECLARABLE_TRACE_RECURSION_DEPTH; - m_DeviceDesc.rayTracingGeometryObjectMaxNum = (1 << 24) - 1; - } -#endif +inline Result DeviceD3D12::CreateMemory(const MemoryD3D12Desc& memoryDesc, Memory*& memory) +{ + return CreateImplementation(memory, memoryDesc); +} - m_DeviceDesc.timestampFrequencyHz = timestampFrequency; - m_DeviceDesc.subPixelPrecisionBits = D3D12_SUBPIXEL_FRACTIONAL_BIT_COUNT; - m_DeviceDesc.subTexelPrecisionBits = D3D12_SUBTEXEL_FRACTIONAL_BIT_COUNT; - m_DeviceDesc.mipmapPrecisionBits = D3D12_MIP_LOD_FRACTIONAL_BIT_COUNT; - m_DeviceDesc.drawIndexedIndex16ValueMax = D3D12_16BIT_INDEX_STRIP_CUT_VALUE; - m_DeviceDesc.drawIndexedIndex32ValueMax = D3D12_32BIT_INDEX_STRIP_CUT_VALUE; - m_DeviceDesc.drawIndirectMaxNum = (1ull << D3D12_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP) - 1; - m_DeviceDesc.samplerLodBiasMin = D3D12_MIP_LOD_BIAS_MIN; - m_DeviceDesc.samplerLodBiasMax = D3D12_MIP_LOD_BIAS_MAX; - m_DeviceDesc.samplerAnisotropyMax = D3D12_DEFAULT_MAX_ANISOTROPY; - m_DeviceDesc.texelOffsetMin = D3D12_COMMONSHADER_TEXEL_OFFSET_MAX_NEGATIVE; - m_DeviceDesc.texelOffsetMax = D3D12_COMMONSHADER_TEXEL_OFFSET_MAX_POSITIVE; - m_DeviceDesc.texelGatherOffsetMin = D3D12_COMMONSHADER_TEXEL_OFFSET_MAX_NEGATIVE; - m_DeviceDesc.texelGatherOffsetMax = D3D12_COMMONSHADER_TEXEL_OFFSET_MAX_POSITIVE; - m_DeviceDesc.clipDistanceMaxNum = D3D12_CLIP_OR_CULL_DISTANCE_COUNT; - m_DeviceDesc.cullDistanceMaxNum = D3D12_CLIP_OR_CULL_DISTANCE_COUNT; - m_DeviceDesc.combinedClipAndCullDistanceMaxNum = D3D12_CLIP_OR_CULL_DISTANCE_COUNT; - m_DeviceDesc.conservativeRasterTier = (uint8_t)options.ConservativeRasterizationTier; - m_DeviceDesc.physicalDeviceNum = (uint8_t)m_Device->GetNodeCount(); +inline void DeviceD3D12::DestroyCommandAllocator(CommandAllocator& commandAllocator) +{ + Deallocate(GetStdAllocator(), (CommandAllocatorD3D12*)&commandAllocator); +} - m_DeviceDesc.isAPIValidationEnabled = enableValidation; - m_DeviceDesc.isTextureFilterMinMaxSupported = levels.MaxSupportedFeatureLevel >= D3D_FEATURE_LEVEL_11_1 ? true : false; - m_DeviceDesc.isLogicOpSupported = options.OutputMergerLogicOp != 0; - m_DeviceDesc.isDepthBoundsTestSupported = options2.DepthBoundsTestSupported != 0; - m_DeviceDesc.isProgrammableSampleLocationsSupported = options2.ProgrammableSamplePositionsTier != D3D12_PROGRAMMABLE_SAMPLE_POSITIONS_TIER_NOT_SUPPORTED; - m_DeviceDesc.isComputeQueueSupported = true; - m_DeviceDesc.isCopyQueueSupported = true; - m_DeviceDesc.isCopyQueueTimestampSupported = options3.CopyQueueTimestampQueriesSupported != 0; - m_DeviceDesc.isRegisterAliasingSupported = true; - m_DeviceDesc.isSubsetAllocationSupported = true; - m_DeviceDesc.isFloat16Supported = options4.Native16BitShaderOpsSupported; +inline void DeviceD3D12::DestroyDescriptorPool(DescriptorPool& descriptorPool) +{ + Deallocate(GetStdAllocator(), (DescriptorPoolD3D12*)&descriptorPool); } -void DeviceD3D12::Destroy() +inline void DeviceD3D12::DestroyBuffer(Buffer& buffer) { - bool skipLiveObjectsReporting = m_SkipLiveObjectsReporting; - Deallocate(GetStdAllocator(), this); + Deallocate(GetStdAllocator(), (BufferD3D12*)&buffer); +} - if (!skipLiveObjectsReporting) - { - ComPtr pDebug; - HRESULT hr = DXGIGetDebugInterface1(0, IID_PPV_ARGS(&pDebug)); - if (SUCCEEDED(hr)) - pDebug->ReportLiveObjects(DXGI_DEBUG_ALL, (DXGI_DEBUG_RLO_FLAGS)((uint32_t)DXGI_DEBUG_RLO_DETAIL | (uint32_t)DXGI_DEBUG_RLO_IGNORE_INTERNAL)); - } +inline void DeviceD3D12::DestroyTexture(Texture& texture) +{ + Deallocate(GetStdAllocator(), (TextureD3D12*)&texture); } -Result CreateDeviceD3D12(const DeviceCreationD3D12Desc& deviceCreationDesc, DeviceBase*& device) +inline void DeviceD3D12::DestroyDescriptor(Descriptor& descriptor) { - Log log(GraphicsAPI::D3D12, deviceCreationDesc.callbackInterface); - StdAllocator allocator(deviceCreationDesc.memoryAllocatorInterface); + Deallocate(GetStdAllocator(), (DescriptorD3D12*)&descriptor); +} - DeviceD3D12* implementation = Allocate(allocator, log, allocator); - const Result res = implementation->Create(deviceCreationDesc); +inline void DeviceD3D12::DestroyPipelineLayout(PipelineLayout& pipelineLayout) +{ + Deallocate(GetStdAllocator(), (PipelineLayoutD3D12*)&pipelineLayout); +} - if (res == Result::SUCCESS) - { - device = implementation; - return Result::SUCCESS; - } +inline void DeviceD3D12::DestroyPipeline(Pipeline& pipeline) +{ + Deallocate(GetStdAllocator(), (PipelineD3D12*)&pipeline); +} - Deallocate(allocator, implementation); - return res; +inline void DeviceD3D12::DestroyFence(Fence& fence) +{ + Deallocate(GetStdAllocator(), (FenceD3D12*)&fence); } -Result CreateDeviceD3D12(const DeviceCreationDesc& deviceCreationDesc, DeviceBase*& device) +inline void DeviceD3D12::DestroyFrameBuffer(FrameBuffer& frameBuffer) { - Log log(GraphicsAPI::D3D12, deviceCreationDesc.callbackInterface); - StdAllocator allocator(deviceCreationDesc.memoryAllocatorInterface); + Deallocate(GetStdAllocator(), (FrameBufferD3D12*)&frameBuffer); +} - ComPtr factory; - HRESULT hr = CreateDXGIFactory2(0, IID_PPV_ARGS(&factory)); - RETURN_ON_BAD_HRESULT(log, hr, "CreateDXGIFactory2() failed, error code: 0x%X.", hr); +inline void DeviceD3D12::DestroyQueryPool(QueryPool& queryPool) +{ + Deallocate(GetStdAllocator(), (QueryPoolD3D12*)&queryPool); +} - ComPtr adapter; - if (deviceCreationDesc.physicalDeviceGroup != nullptr) +inline Result DeviceD3D12::AllocateMemory(const MemoryType memoryType, uint64_t size, Memory*& memory) +{ + return CreateImplementation(memory, memoryType, size); +} + +inline Result DeviceD3D12::BindBufferMemory(const BufferMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum) +{ + for (uint32_t i = 0; i < memoryBindingDescNum; i++) { - LUID luid = *(LUID*)&deviceCreationDesc.physicalDeviceGroup->luid; - hr = factory->EnumAdapterByLuid(luid, IID_PPV_ARGS(&adapter)); - RETURN_ON_BAD_HRESULT(log, hr, "IDXGIFactory4::EnumAdapterByLuid() failed, error code: 0x%X.", hr); + Result result = ((BufferD3D12*)memoryBindingDescs[i].buffer)->BindMemory((MemoryD3D12*)memoryBindingDescs[i].memory, memoryBindingDescs[i].offset); + if (result != Result::SUCCESS) + return result; } - else + + return Result::SUCCESS; +} + +inline Result DeviceD3D12::BindTextureMemory(const TextureMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum) +{ + for (uint32_t i = 0; i < memoryBindingDescNum; i++) { - hr = factory->EnumAdapters(0, &adapter); - RETURN_ON_BAD_HRESULT(log, hr, "IDXGIFactory4::EnumAdapters() failed, error code: 0x%X.", hr); + Result result = ((TextureD3D12*)memoryBindingDescs[i].texture)->BindMemory((MemoryD3D12*)memoryBindingDescs[i].memory, memoryBindingDescs[i].offset); + if (result != Result::SUCCESS) + return result; } - DeviceD3D12* implementation = Allocate(allocator, log, allocator); - const nri::Result result = implementation->Create(adapter, deviceCreationDesc); - if (result != nri::Result::SUCCESS) + return Result::SUCCESS; +} + +inline Result DeviceD3D12::BindAccelerationStructureMemory(const AccelerationStructureMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum) +{ + for (uint32_t i = 0; i < memoryBindingDescNum; i++) { - Deallocate(allocator, implementation); - return result; + Result result = ((AccelerationStructureD3D12*)memoryBindingDescs[i].accelerationStructure)->BindMemory(memoryBindingDescs[i].memory, memoryBindingDescs[i].offset); + if (result != Result::SUCCESS) + return result; } - device = (DeviceBase*)implementation; + return Result::SUCCESS; +} - return nri::Result::SUCCESS; +inline void DeviceD3D12::FreeMemory(Memory& memory) +{ + Deallocate(GetStdAllocator(), (MemoryD3D12*)&memory); +} + +inline FormatSupportBits DeviceD3D12::GetFormatSupport(Format format) const +{ + const uint32_t offset = std::min((uint32_t)format, (uint32_t)GetCountOf(D3D_FORMAT_SUPPORT_TABLE) - 1); + + return D3D_FORMAT_SUPPORT_TABLE[offset]; +} + +inline uint32_t DeviceD3D12::CalculateAllocationNumber(const ResourceGroupDesc& resourceGroupDesc) const +{ + HelperDeviceMemoryAllocator allocator(m_CoreInterface, (Device&)*this, m_StdAllocator); + + return allocator.CalculateAllocationNumber(resourceGroupDesc); +} + +inline Result DeviceD3D12::AllocateAndBindMemory(const ResourceGroupDesc& resourceGroupDesc, nri::Memory** allocations) +{ + HelperDeviceMemoryAllocator allocator(m_CoreInterface, (Device&)*this, m_StdAllocator); + + return allocator.AllocateAndBindMemory(resourceGroupDesc, allocations); +} + +inline Result DeviceD3D12::CreateAccelerationStructure(const AccelerationStructureDesc& accelerationStructureDesc, AccelerationStructure*& accelerationStructure) +{ + return CreateImplementation(accelerationStructure, accelerationStructureDesc); +} + +inline Result DeviceD3D12::CreateAccelerationStructure(const AccelerationStructureD3D12Desc& accelerationStructureDesc, AccelerationStructure*& accelerationStructure) +{ + return CreateImplementation(accelerationStructure, accelerationStructureDesc); +} + +inline void DeviceD3D12::DestroyAccelerationStructure(AccelerationStructure& accelerationStructure) +{ + Deallocate(GetStdAllocator(), (AccelerationStructureD3D12*)&accelerationStructure); } #include "DeviceD3D12.hpp" diff --git a/Source/D3D12/DeviceD3D12.h b/Source/D3D12/DeviceD3D12.h index 7a242af4..91b3a49d 100644 --- a/Source/D3D12/DeviceD3D12.h +++ b/Source/D3D12/DeviceD3D12.h @@ -26,194 +26,164 @@ typedef uint16_t HeapOffsetType; namespace nri { - struct CommandQueueD3D12; - struct DescriptorHandle - { - HeapIndexType heapIndex; - HeapOffsetType heapOffset; - }; +struct CommandQueueD3D12; - struct DescriptorHeapDesc - { - ComPtr descriptorHeap; - DescriptorPointerCPU descriptorPointerCPU; - DescriptorPointerGPU descriptorPointerGPU; - uint32_t descriptorSize; - }; +struct DescriptorHandle +{ + HeapIndexType heapIndex; + HeapOffsetType heapOffset; +}; - constexpr size_t DESCRIPTOR_HEAP_TYPE_NUM = D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES; - constexpr uint32_t DESCRIPTORS_BATCH_SIZE = 1024; +struct DescriptorHeapDesc +{ + ComPtr descriptorHeap; + DescriptorPointerCPU descriptorPointerCPU; + DescriptorPointerGPU descriptorPointerGPU; + uint32_t descriptorSize; +}; - struct DeviceD3D12 final : public DeviceBase - { - DeviceD3D12(const Log& log, StdAllocator& stdAllocator); - ~DeviceD3D12(); - - operator ID3D12Device*() const; - operator ID3D12Device5*() const; - - template - Result CreateImplementation(Interface*& entity, const Args&... args); - - bool GetOutput(Display* display, ComPtr& output) const; - - Result Create(IDXGIAdapter* dxgiAdapter, const DeviceCreationDesc& deviceCreationDesc); - Result Create(const DeviceCreationD3D12Desc& deviceCreationDesc); - - Result CreateCpuOnlyVisibleDescriptorHeap(D3D12_DESCRIPTOR_HEAP_TYPE type); - Result GetDescriptorHandle(D3D12_DESCRIPTOR_HEAP_TYPE type, DescriptorHandle& descriptorHandle); - void FreeDescriptorHandle(D3D12_DESCRIPTOR_HEAP_TYPE type, const DescriptorHandle& descriptorHandle); - DescriptorPointerCPU GetDescriptorPointerCPU(const DescriptorHandle& descriptorHandle); - void GetMemoryInfo(MemoryLocation memoryLocation, const D3D12_RESOURCE_DESC& resourceDesc, MemoryDesc& memoryDesc) const; - - ID3D12CommandSignature* CreateCommandSignature(D3D12_INDIRECT_ARGUMENT_TYPE indirectArgumentType, uint32_t stride); - ID3D12CommandSignature* GetDrawCommandSignature(uint32_t stride); - ID3D12CommandSignature* GetDrawIndexedCommandSignature(uint32_t stride); - ID3D12CommandSignature* GetDispatchCommandSignature() const; - - bool IsMeshShaderSupported() const; - const CoreInterface& GetCoreInterface() const; - - //================================================================================================================ - // NRI - //================================================================================================================ - void SetDebugName(const char* name); - - inline const DeviceDesc& GetDesc() const - { return m_DeviceDesc; } - - Result GetCommandQueue(CommandQueueType commandQueueType, CommandQueue*& commandQueue); - - Result CreateCommandQueue(CommandQueueType commandQueueType, CommandQueue*& commandQueue); - Result CreateCommandQueue(void* d3d12commandQueue, CommandQueueD3D12*& commandQueue); - Result CreateCommandAllocator(const CommandQueue& commandQueue, CommandAllocator*& commandAllocator); - Result CreateDescriptorPool(const DescriptorPoolDesc& descriptorPoolDesc, DescriptorPool*& descriptorPool); - Result CreateBuffer(const BufferDesc& bufferDesc, Buffer*& buffer); - Result CreateTexture(const TextureDesc& textureDesc, Texture*& texture); - Result CreateDescriptor(const BufferViewDesc& bufferViewDesc, Descriptor*& bufferView); - Result CreateDescriptor(const Texture1DViewDesc& textureViewDesc, Descriptor*& textureView); - Result CreateDescriptor(const Texture2DViewDesc& textureViewDesc, Descriptor*& textureView); - Result CreateDescriptor(const Texture3DViewDesc& textureViewDesc, Descriptor*& textureView); -#ifdef __ID3D12GraphicsCommandList4_INTERFACE_DEFINED__ - Result CreateDescriptor(const AccelerationStructure& accelerationStructure, Descriptor*& accelerationStructureView); -#endif - Result CreateDescriptor(const SamplerDesc& samplerDesc, Descriptor*& sampler); - Result CreatePipelineLayout(const PipelineLayoutDesc& pipelineLayoutDesc, PipelineLayout*& pipelineLayout); - Result CreatePipeline(const GraphicsPipelineDesc& graphicsPipelineDesc, Pipeline*& pipeline); - Result CreatePipeline(const ComputePipelineDesc& computePipelineDesc, Pipeline*& pipeline); - Result CreatePipeline(const RayTracingPipelineDesc& rayTracingPipelineDesc, Pipeline*& pipeline); - Result CreateFrameBuffer(const FrameBufferDesc& frameBufferDesc, FrameBuffer*& frameBuffer); - Result CreateQueryPool(const QueryPoolDesc& queryPoolDesc, QueryPool*& queryPool); - Result CreateQueueSemaphore(QueueSemaphore*& queueSemaphore); - Result CreateDeviceSemaphore(bool signaled, DeviceSemaphore*& deviceSemaphore); - - Result CreateCommandBuffer(const CommandBufferD3D12Desc& commandBufferDesc, CommandBuffer*& commandBuffer); - Result CreateBuffer(const BufferD3D12Desc& bufferDesc, Buffer*& buffer); - Result CreateTexture(const TextureD3D12Desc& textureDesc, Texture*& texture); - Result CreateMemory(const MemoryD3D12Desc& memoryDesc, Memory*& memory); - Result CreateAccelerationStructure(const AccelerationStructureD3D12Desc& accelerationStructureDesc, AccelerationStructure*& accelerationStructure); - - void DestroyCommandAllocator(CommandAllocator& commandAllocator); - void DestroyDescriptorPool(DescriptorPool& descriptorPool); - void DestroyBuffer(Buffer& buffer); - void DestroyTexture(Texture& texture); - void DestroyDescriptor(Descriptor& descriptor); - void DestroyPipelineLayout(PipelineLayout& pipelineLayout); - void DestroyPipeline(Pipeline& pipeline); - void DestroyFrameBuffer(FrameBuffer& frameBuffer); - void DestroyQueryPool(QueryPool& queryPool); - void DestroyQueueSemaphore(QueueSemaphore& queueSemaphore); - void DestroyDeviceSemaphore(DeviceSemaphore& deviceSemaphore); - void DestroySwapChain(SwapChain& swapChain); - - Result AllocateMemory(const MemoryType memoryType, uint64_t size, Memory*& memory); - Result BindBufferMemory(const BufferMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum); - Result BindTextureMemory(const TextureMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum); -#ifdef __ID3D12GraphicsCommandList4_INTERFACE_DEFINED__ - Result CreateAccelerationStructure(const AccelerationStructureDesc& accelerationStructureDesc, AccelerationStructure*& accelerationStructure); - Result BindAccelerationStructureMemory(const AccelerationStructureMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum); - void DestroyAccelerationStructure(AccelerationStructure& accelerationStructure); -#endif - void FreeMemory(Memory& memory); - - FormatSupportBits GetFormatSupport(Format format) const; - - uint32_t CalculateAllocationNumber(const ResourceGroupDesc& resourceGroupDesc) const; - Result AllocateAndBindMemory(const ResourceGroupDesc& resourceGroupDesc, Memory** allocations); - - Result CreateSwapChain(const SwapChainDesc& swapChainDesc, SwapChain*& swapChain); - Result GetDisplays(Display** displays, uint32_t& displayNum); - Result GetDisplaySize(Display& display, uint16_t& width, uint16_t& height); - - //================================================================================================================ - // DeviceBase - //================================================================================================================ - void Destroy(); - Result FillFunctionTable(CoreInterface& table) const; - Result FillFunctionTable(SwapChainInterface& table) const; - Result FillFunctionTable(WrapperD3D12Interface& wrapperD3D12Interface) const; -#ifdef __ID3D12GraphicsCommandList4_INTERFACE_DEFINED__ - Result FillFunctionTable(RayTracingInterface& rayTracingInterface) const; -#endif -#ifdef __ID3D12GraphicsCommandList6_INTERFACE_DEFINED__ - Result FillFunctionTable(MeshShaderInterface& meshShaderInterface) const; -#endif - Result FillFunctionTable(HelperInterface& helperInterface) const; - - private: - void UpdateDeviceDesc(bool enableValidation); - MemoryType GetMemoryType(MemoryLocation memoryLocation, const D3D12_RESOURCE_DESC& resourceDesc) const; - - private: - ComPtr m_Device; -#ifdef __ID3D12Device5_INTERFACE_DEFINED__ - ComPtr m_Device5; -#endif - std::array m_CommandQueues = {}; - Vector m_DescriptorHeaps; - Vector> m_FreeDescriptors; - DeviceDesc m_DeviceDesc = {}; - UnorderedMap> m_DrawCommandSignatures; - UnorderedMap> m_DrawIndexedCommandSignatures; - ComPtr m_DispatchCommandSignature; - CoreInterface m_CoreInterface = {}; - ComPtr m_Adapter; - bool m_IsRaytracingSupported = false; - bool m_IsMeshShaderSupported = false; - bool m_SkipLiveObjectsReporting = false; - - std::array m_FreeDescriptorLocks; - Lock m_DescriptorHeapLock; - Lock m_QueueLock; - }; - - inline DeviceD3D12::operator ID3D12Device*() const - { - return m_Device.GetInterface(); - } +constexpr size_t DESCRIPTOR_HEAP_TYPE_NUM = D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES; +constexpr uint32_t DESCRIPTORS_BATCH_SIZE = 1024; -#ifdef __ID3D12Device5_INTERFACE_DEFINED__ - inline DeviceD3D12::operator ID3D12Device5*() const - { - return m_Device5.GetInterface(); - } -#endif +struct DeviceD3D12 final : public DeviceBase +{ + DeviceD3D12(const Log& log, StdAllocator& stdAllocator); + ~DeviceD3D12(); - inline bool DeviceD3D12::IsMeshShaderSupported() const - { - return m_IsMeshShaderSupported; - } + inline operator ID3D12Device*() const + { return m_Device.GetInterface(); } - inline const CoreInterface& DeviceD3D12::GetCoreInterface() const - { - return m_CoreInterface; - } + inline operator ID3D12Device5*() const + { return m_Device5.GetInterface(); } - inline void DeviceD3D12::FreeDescriptorHandle(D3D12_DESCRIPTOR_HEAP_TYPE type, const DescriptorHandle& descriptorHandle) + inline bool IsMeshShaderSupported() const + { return m_IsMeshShaderSupported; } + + inline const CoreInterface& GetCoreInterface() const + { return m_CoreInterface; } + + inline void FreeDescriptorHandle(D3D12_DESCRIPTOR_HEAP_TYPE type, const DescriptorHandle& descriptorHandle) { ExclusiveScope lock(m_FreeDescriptorLocks[type]); auto& freeDescriptors = m_FreeDescriptors[type]; freeDescriptors.push_back(descriptorHandle); } + + template + Result CreateImplementation(Interface*& entity, const Args&... args); + + bool GetOutput(Display* display, ComPtr& output) const; + + Result Create(IDXGIAdapter* dxgiAdapter, const DeviceCreationDesc& deviceCreationDesc); + Result Create(const DeviceCreationD3D12Desc& deviceCreationDesc); + + Result CreateCpuOnlyVisibleDescriptorHeap(D3D12_DESCRIPTOR_HEAP_TYPE type); + Result GetDescriptorHandle(D3D12_DESCRIPTOR_HEAP_TYPE type, DescriptorHandle& descriptorHandle); + DescriptorPointerCPU GetDescriptorPointerCPU(const DescriptorHandle& descriptorHandle); + void GetMemoryInfo(MemoryLocation memoryLocation, const D3D12_RESOURCE_DESC& resourceDesc, MemoryDesc& memoryDesc) const; + + ID3D12CommandSignature* CreateCommandSignature(D3D12_INDIRECT_ARGUMENT_TYPE indirectArgumentType, uint32_t stride); + ID3D12CommandSignature* GetDrawCommandSignature(uint32_t stride); + ID3D12CommandSignature* GetDrawIndexedCommandSignature(uint32_t stride); + ID3D12CommandSignature* GetDispatchCommandSignature() const; + + //================================================================================================================ + // NRI + //================================================================================================================ + + inline void SetDebugName(const char* name) + { SET_D3D_DEBUG_OBJECT_NAME(m_Device, name); } + + inline const DeviceDesc& GetDesc() const + { return m_DeviceDesc; } + + Result CreateSwapChain(const SwapChainDesc& swapChainDesc, SwapChain*& swapChain); + void DestroySwapChain(SwapChain& swapChain); + Result GetDisplays(Display** displays, uint32_t& displayNum); + Result GetDisplaySize(Display& display, uint16_t& width, uint16_t& height); + + Result GetCommandQueue(CommandQueueType commandQueueType, CommandQueue*& commandQueue); + Result CreateCommandQueue(CommandQueueType commandQueueType, CommandQueue*& commandQueue); + Result CreateCommandQueue(void* d3d12commandQueue, CommandQueueD3D12*& commandQueue); + Result CreateCommandAllocator(const CommandQueue& commandQueue, CommandAllocator*& commandAllocator); + Result CreateDescriptorPool(const DescriptorPoolDesc& descriptorPoolDesc, DescriptorPool*& descriptorPool); + Result CreateBuffer(const BufferDesc& bufferDesc, Buffer*& buffer); + Result CreateTexture(const TextureDesc& textureDesc, Texture*& texture); + Result CreateDescriptor(const BufferViewDesc& bufferViewDesc, Descriptor*& bufferView); + Result CreateDescriptor(const Texture1DViewDesc& textureViewDesc, Descriptor*& textureView); + Result CreateDescriptor(const Texture2DViewDesc& textureViewDesc, Descriptor*& textureView); + Result CreateDescriptor(const Texture3DViewDesc& textureViewDesc, Descriptor*& textureView); + Result CreateDescriptor(const AccelerationStructure& accelerationStructure, Descriptor*& accelerationStructureView); + Result CreateDescriptor(const SamplerDesc& samplerDesc, Descriptor*& sampler); + Result CreatePipelineLayout(const PipelineLayoutDesc& pipelineLayoutDesc, PipelineLayout*& pipelineLayout); + Result CreatePipeline(const GraphicsPipelineDesc& graphicsPipelineDesc, Pipeline*& pipeline); + Result CreatePipeline(const ComputePipelineDesc& computePipelineDesc, Pipeline*& pipeline); + Result CreatePipeline(const RayTracingPipelineDesc& rayTracingPipelineDesc, Pipeline*& pipeline); + Result CreateFence(uint64_t initialValue, Fence*& fence); + Result CreateFrameBuffer(const FrameBufferDesc& frameBufferDesc, FrameBuffer*& frameBuffer); + Result CreateQueryPool(const QueryPoolDesc& queryPoolDesc, QueryPool*& queryPool); + Result CreateCommandBuffer(const CommandBufferD3D12Desc& commandBufferDesc, CommandBuffer*& commandBuffer); + Result CreateBuffer(const BufferD3D12Desc& bufferDesc, Buffer*& buffer); + Result CreateTexture(const TextureD3D12Desc& textureDesc, Texture*& texture); + Result CreateMemory(const MemoryD3D12Desc& memoryDesc, Memory*& memory); + Result CreateAccelerationStructure(const AccelerationStructureD3D12Desc& accelerationStructureDesc, AccelerationStructure*& accelerationStructure); + void DestroyCommandAllocator(CommandAllocator& commandAllocator); + void DestroyDescriptorPool(DescriptorPool& descriptorPool); + void DestroyBuffer(Buffer& buffer); + void DestroyTexture(Texture& texture); + void DestroyDescriptor(Descriptor& descriptor); + void DestroyPipelineLayout(PipelineLayout& pipelineLayout); + void DestroyPipeline(Pipeline& pipeline); + void DestroyFence(Fence& queueSemaphore); + void DestroyFrameBuffer(FrameBuffer& frameBuffer); + void DestroyQueryPool(QueryPool& queryPool); + Result AllocateMemory(const MemoryType memoryType, uint64_t size, Memory*& memory); + Result BindBufferMemory(const BufferMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum); + Result BindTextureMemory(const TextureMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum); + void FreeMemory(Memory& memory); + FormatSupportBits GetFormatSupport(Format format) const; + uint32_t CalculateAllocationNumber(const ResourceGroupDesc& resourceGroupDesc) const; + Result AllocateAndBindMemory(const ResourceGroupDesc& resourceGroupDesc, Memory** allocations); + + Result CreateAccelerationStructure(const AccelerationStructureDesc& accelerationStructureDesc, AccelerationStructure*& accelerationStructure); + Result BindAccelerationStructureMemory(const AccelerationStructureMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum); + void DestroyAccelerationStructure(AccelerationStructure& accelerationStructure); + + //================================================================================================================ + // DeviceBase + //================================================================================================================ + + void Destroy(); + Result FillFunctionTable(CoreInterface& table) const; + Result FillFunctionTable(SwapChainInterface& table) const; + Result FillFunctionTable(WrapperD3D12Interface& wrapperD3D12Interface) const; + Result FillFunctionTable(RayTracingInterface& rayTracingInterface) const; + Result FillFunctionTable(MeshShaderInterface& meshShaderInterface) const; + Result FillFunctionTable(HelperInterface& helperInterface) const; + +private: + void UpdateDeviceDesc(bool enableValidation); + MemoryType GetMemoryType(MemoryLocation memoryLocation, const D3D12_RESOURCE_DESC& resourceDesc) const; + +private: + ComPtr m_Device; + ComPtr m_Device5; + std::array m_CommandQueues = {}; + Vector m_DescriptorHeaps; + Vector> m_FreeDescriptors; + DeviceDesc m_DeviceDesc = {}; + UnorderedMap> m_DrawCommandSignatures; + UnorderedMap> m_DrawIndexedCommandSignatures; + ComPtr m_DispatchCommandSignature; + CoreInterface m_CoreInterface = {}; + ComPtr m_Adapter; + std::array m_FreeDescriptorLocks; + Lock m_DescriptorHeapLock; + Lock m_QueueLock; + bool m_IsRaytracingSupported = false; + bool m_IsMeshShaderSupported = false; + bool m_SkipLiveObjectsReporting = false; +}; + } diff --git a/Source/D3D12/DeviceD3D12.hpp b/Source/D3D12/DeviceD3D12.hpp index 4147317c..caac2679 100644 --- a/Source/D3D12/DeviceD3D12.hpp +++ b/Source/D3D12/DeviceD3D12.hpp @@ -8,7 +8,9 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ CoreInterface ] +Declare_PartiallyFillFunctionTable_Functions(D3D12) + +#pragma region [ Core ] static const DeviceDesc& NRI_CALL GetDeviceDesc(const Device& device) { @@ -87,6 +89,11 @@ static Result NRI_CALL CreateComputePipeline(Device& device, const ComputePipeli return ((DeviceD3D12&)device).CreatePipeline(computePipelineDesc, pipeline); } +static Result NRI_CALL CreateFence(Device& device, uint64_t initialValue, Fence*& fence) +{ + return ((DeviceD3D12&)device).CreateFence(initialValue, fence); +} + static Result NRI_CALL CreateFrameBuffer(Device& device, const FrameBufferDesc& frameBufferDesc, FrameBuffer*& frameBuffer) { return ((DeviceD3D12&)device).CreateFrameBuffer(frameBufferDesc, frameBuffer); @@ -97,16 +104,6 @@ static Result NRI_CALL CreateQueryPool(Device& device, const QueryPoolDesc& quer return ((DeviceD3D12&)device).CreateQueryPool(queryPoolDesc, queryPool); } -static Result NRI_CALL CreateQueueSemaphore(Device& device, QueueSemaphore*& queueSemaphore) -{ - return ((DeviceD3D12&)device).CreateQueueSemaphore(queueSemaphore); -} - -static Result NRI_CALL CreateDeviceSemaphore(Device& device, bool signaled, DeviceSemaphore*& deviceSemaphore) -{ - return ((DeviceD3D12&)device).CreateDeviceSemaphore(signaled, deviceSemaphore); -} - static void NRI_CALL DestroyCommandAllocator(CommandAllocator& commandAllocator) { DeviceD3D12& device = ((CommandAllocatorD3D12&)commandAllocator).GetDevice(); @@ -149,6 +146,12 @@ static void NRI_CALL DestroyPipeline(Pipeline& pipeline) device.DestroyPipeline(pipeline); } +static void NRI_CALL DestroyFence(Fence& fence) +{ + DeviceD3D12& device = ((FenceD3D12&)fence).GetDevice(); + device.DestroyFence(fence); +} + static void NRI_CALL DestroyFrameBuffer(FrameBuffer& frameBuffer) { DeviceD3D12& device = ((FrameBufferD3D12&)frameBuffer).GetDevice(); @@ -161,18 +164,6 @@ static void NRI_CALL DestroyQueryPool(QueryPool& queryPool) device.DestroyQueryPool(queryPool); } -static void NRI_CALL DestroyQueueSemaphore(QueueSemaphore& queueSemaphore) -{ - DeviceD3D12& device = ((QueueSemaphoreD3D12&)queueSemaphore).GetDevice(); - device.DestroyQueueSemaphore(queueSemaphore); -} - -static void NRI_CALL DestroyDeviceSemaphore(DeviceSemaphore& deviceSemaphore) -{ - DeviceD3D12& device = ((DeviceSemaphoreD3D12&)deviceSemaphore).GetDevice(); - device.DestroyDeviceSemaphore(deviceSemaphore); -} - static Result NRI_CALL AllocateMemory(Device& device, uint32_t physicalDeviceMask, MemoryType memoryType, uint64_t size, Memory*& memory) { MaybeUnused(physicalDeviceMask); // TODO: use it @@ -206,24 +197,14 @@ static void NRI_CALL SetDeviceDebugName(Device& device, const char* name) ((DeviceD3D12&)device).SetDebugName(name); } -static void NRI_CALL SetDeviceSemaphoreDebugName(DeviceSemaphore& deviceSemaphore, const char* name) -{ - ((DeviceSemaphoreD3D12&)deviceSemaphore).SetDebugName(name); -} - -static void NRI_CALL SetQueueSemaphoreDebugName(QueueSemaphore& queueSemaphore, const char* name) -{ - ((QueueSemaphoreD3D12&)queueSemaphore).SetDebugName(name); -} - -static void NRI_CALL SetDescriptorDebugName(Descriptor& descriptor, const char* name) +static void NRI_CALL SetPipelineDebugName(Pipeline& pipeline, const char* name) { - ((DescriptorD3D12&)descriptor).SetDebugName(name); + ((PipelineD3D12&)pipeline).SetDebugName(name); } -static void NRI_CALL SetPipelineDebugName(Pipeline& pipeline, const char* name) +static void NRI_CALL SetPipelineLayoutDebugName(PipelineLayout& pipelineLayout, const char* name) { - ((PipelineD3D12&)pipeline).SetDebugName(name); + ((PipelineLayoutD3D12&)pipelineLayout).SetDebugName(name); } static void NRI_CALL SetFrameBufferDebugName(FrameBuffer& frameBuffer, const char* name) @@ -241,41 +222,12 @@ static void* NRI_CALL GetDeviceNativeObject(const Device& device) return (DeviceD3D12&)device; } -static uint64_t NRI_CALL GetDescriptorNativeObject(const Descriptor& descriptor, uint32_t physicalDeviceIndex) -{ - MaybeUnused(physicalDeviceIndex); - - return uint64_t( ((DescriptorD3D12&)descriptor).GetPointerCPU() ); -} - -void FillFunctionTableBufferD3D12(CoreInterface& coreInterface); -void FillFunctionTableCommandAllocatorD3D12(CoreInterface& coreInterface); -void FillFunctionTableCommandBufferD3D12(CoreInterface& coreInterface); -void FillFunctionTableCommandQueueD3D12(CoreInterface& coreInterface); -void FillFunctionTableDescriptorPoolD3D12(CoreInterface& coreInterface); -void FillFunctionTableDescriptorSetD3D12(CoreInterface& coreInterface); -void FillFunctionTableQueryPoolD3D12(CoreInterface& coreInterface); -void FillFunctionTableTextureD3D12(CoreInterface& coreInterface); -void FillFunctionTablePipelineLayoutD3D12(CoreInterface& coreInterface); - Result DeviceD3D12::FillFunctionTable(CoreInterface& coreInterface) const { coreInterface = {}; - - FillFunctionTableBufferD3D12(coreInterface); - FillFunctionTableCommandAllocatorD3D12(coreInterface); - FillFunctionTableCommandBufferD3D12(coreInterface); - FillFunctionTableCommandQueueD3D12(coreInterface); - FillFunctionTableDescriptorPoolD3D12(coreInterface); - FillFunctionTableDescriptorSetD3D12(coreInterface); - FillFunctionTableQueryPoolD3D12(coreInterface); - FillFunctionTableTextureD3D12(coreInterface); - FillFunctionTablePipelineLayoutD3D12(coreInterface); - coreInterface.GetDeviceDesc = ::GetDeviceDesc; coreInterface.GetFormatSupport = ::GetFormatSupport; coreInterface.GetCommandQueue = ::GetCommandQueue; - coreInterface.CreateCommandAllocator = ::CreateCommandAllocator; coreInterface.CreateDescriptorPool = ::CreateDescriptorPool; coreInterface.CreateBuffer = ::CreateBuffer; @@ -290,9 +242,7 @@ Result DeviceD3D12::FillFunctionTable(CoreInterface& coreInterface) const coreInterface.CreateComputePipeline = ::CreateComputePipeline; coreInterface.CreateFrameBuffer = ::CreateFrameBuffer; coreInterface.CreateQueryPool = ::CreateQueryPool; - coreInterface.CreateQueueSemaphore = ::CreateQueueSemaphore; - coreInterface.CreateDeviceSemaphore = ::CreateDeviceSemaphore; - + coreInterface.CreateFence = ::CreateFence; coreInterface.DestroyCommandAllocator = ::DestroyCommandAllocator; coreInterface.DestroyDescriptorPool = ::DestroyDescriptorPool; coreInterface.DestroyBuffer = ::DestroyBuffer; @@ -300,33 +250,37 @@ Result DeviceD3D12::FillFunctionTable(CoreInterface& coreInterface) const coreInterface.DestroyDescriptor = ::DestroyDescriptor; coreInterface.DestroyPipelineLayout = ::DestroyPipelineLayout; coreInterface.DestroyPipeline = ::DestroyPipeline; + coreInterface.DestroyFence = ::DestroyFence; coreInterface.DestroyFrameBuffer = ::DestroyFrameBuffer; coreInterface.DestroyQueryPool = ::DestroyQueryPool; - coreInterface.DestroyQueueSemaphore = ::DestroyQueueSemaphore; - coreInterface.DestroyDeviceSemaphore = ::DestroyDeviceSemaphore; - coreInterface.AllocateMemory = ::AllocateMemory; coreInterface.BindBufferMemory = ::BindBufferMemory; coreInterface.BindTextureMemory = ::BindTextureMemory; coreInterface.FreeMemory = ::FreeMemory; - coreInterface.SetDeviceDebugName = ::SetDeviceDebugName; - coreInterface.SetDeviceSemaphoreDebugName = ::SetDeviceSemaphoreDebugName; - coreInterface.SetQueueSemaphoreDebugName = ::SetQueueSemaphoreDebugName; - coreInterface.SetDescriptorDebugName = ::SetDescriptorDebugName; coreInterface.SetPipelineDebugName = ::SetPipelineDebugName; + coreInterface.SetPipelineLayoutDebugName = ::SetPipelineLayoutDebugName; coreInterface.SetFrameBufferDebugName = ::SetFrameBufferDebugName; coreInterface.SetMemoryDebugName = ::SetMemoryDebugName; - coreInterface.GetDeviceNativeObject = ::GetDeviceNativeObject; - coreInterface.GetDescriptorNativeObject = ::GetDescriptorNativeObject; + + Core_Buffer_PartiallyFillFunctionTableD3D12(coreInterface); + Core_CommandAllocator_PartiallyFillFunctionTableD3D12(coreInterface); + Core_CommandBuffer_PartiallyFillFunctionTableD3D12(coreInterface); + Core_CommandQueue_PartiallyFillFunctionTableD3D12(coreInterface); + Core_Descriptor_PartiallyFillFunctionTableD3D12(coreInterface); + Core_DescriptorPool_PartiallyFillFunctionTableD3D12(coreInterface); + Core_DescriptorSet_PartiallyFillFunctionTableD3D12(coreInterface); + Core_Fence_PartiallyFillFunctionTableD3D12(coreInterface); + Core_QueryPool_PartiallyFillFunctionTableD3D12(coreInterface); + Core_Texture_PartiallyFillFunctionTableD3D12(coreInterface); return ValidateFunctionTable(GetLog(), coreInterface); } #pragma endregion -#pragma region [ SwapChainInterface ] +#pragma region [ SwapChain ] static Result NRI_CALL CreateSwapChain(Device& device, const SwapChainDesc& swapChainDesc, SwapChain*& swapChain) { @@ -349,47 +303,44 @@ static Result NRI_CALL GetDisplaySize(Device& device, Display& display, uint16_t return ((DeviceD3D12&)device).GetDisplaySize(display, width, height); } -void FillFunctionTableSwapChainD3D12(SwapChainInterface& swapChainInterface); - Result DeviceD3D12::FillFunctionTable(SwapChainInterface& swapChainInterface) const { swapChainInterface = {}; - - FillFunctionTableSwapChainD3D12(swapChainInterface); - swapChainInterface.CreateSwapChain = ::CreateSwapChain; swapChainInterface.DestroySwapChain = ::DestroySwapChain; swapChainInterface.GetDisplays = ::GetDisplays; swapChainInterface.GetDisplaySize = ::GetDisplaySize; + SwapChain_PartiallyFillFunctionTableD3D12(swapChainInterface); + return ValidateFunctionTable(GetLog(), swapChainInterface); } #pragma endregion -#pragma region [ WrapperD3D12Interface ] +#pragma region [ WrapperD3D12 ] -static Result NRI_CALL CreateCommandBufferD3D12(Device& device, const CommandBufferD3D12Desc& commandBufferDesc, CommandBuffer*& commandBuffer) +static Result NRI_CALL CreateCommandBuffer(Device& device, const CommandBufferD3D12Desc& commandBufferDesc, CommandBuffer*& commandBuffer) { return ((DeviceD3D12&)device).CreateCommandBuffer(commandBufferDesc, commandBuffer); } -static Result NRI_CALL CreateBufferD3D12(Device& device, const BufferD3D12Desc& bufferDesc, Buffer*& buffer) +static Result NRI_CALL CreateBuffer(Device& device, const BufferD3D12Desc& bufferDesc, Buffer*& buffer) { return ((DeviceD3D12&)device).CreateBuffer(bufferDesc, buffer); } -static Result NRI_CALL CreateTextureD3D12(Device& device, const TextureD3D12Desc& textureDesc, Texture*& texture) +static Result NRI_CALL CreateTexture(Device& device, const TextureD3D12Desc& textureDesc, Texture*& texture) { return ((DeviceD3D12&)device).CreateTexture(textureDesc, texture); } -static Result NRI_CALL CreateMemoryD3D12(Device& device, const MemoryD3D12Desc& memoryDesc, Memory*& memory) +static Result NRI_CALL CreateMemory(Device& device, const MemoryD3D12Desc& memoryDesc, Memory*& memory) { return ((DeviceD3D12&)device).CreateMemory(memoryDesc, memory); } -static Result NRI_CALL CreateAccelerationStructureD3D12(Device& device, const AccelerationStructureD3D12Desc& accelerationStructureDesc, AccelerationStructure*& memory) +static Result NRI_CALL CreateAccelerationStructure(Device& device, const AccelerationStructureD3D12Desc& accelerationStructureDesc, AccelerationStructure*& memory) { return ((DeviceD3D12&)device).CreateAccelerationStructure(accelerationStructureDesc, memory); } @@ -397,20 +348,18 @@ static Result NRI_CALL CreateAccelerationStructureD3D12(Device& device, const Ac Result DeviceD3D12::FillFunctionTable(WrapperD3D12Interface& wrapperD3D12Interface) const { wrapperD3D12Interface = {}; - wrapperD3D12Interface.CreateCommandBufferD3D12 = ::CreateCommandBufferD3D12; - wrapperD3D12Interface.CreateBufferD3D12 = ::CreateBufferD3D12; - wrapperD3D12Interface.CreateTextureD3D12 = ::CreateTextureD3D12; - wrapperD3D12Interface.CreateMemoryD3D12 = ::CreateMemoryD3D12; - wrapperD3D12Interface.CreateAccelerationStructureD3D12 = ::CreateAccelerationStructureD3D12; + wrapperD3D12Interface.CreateCommandBufferD3D12 = ::CreateCommandBuffer; + wrapperD3D12Interface.CreateBufferD3D12 = ::CreateBuffer; + wrapperD3D12Interface.CreateTextureD3D12 = ::CreateTexture; + wrapperD3D12Interface.CreateMemoryD3D12 = ::CreateMemory; + wrapperD3D12Interface.CreateAccelerationStructureD3D12 = ::CreateAccelerationStructure; return ValidateFunctionTable(GetLog(), wrapperD3D12Interface); } #pragma endregion -#pragma region [ RayTracingInterface ] - -#ifdef __ID3D12GraphicsCommandList4_INTERFACE_DEFINED__ +#pragma region [ RayTracing ] static Result NRI_CALL CreateRayTracingPipeline(Device& device, const RayTracingPipelineDesc& rayTracingPipelineDesc, Pipeline*& pipeline) { @@ -434,8 +383,6 @@ static Result NRI_CALL BindAccelerationStructureMemory(Device& device, const Acc } void FillFunctionTablePipelineD3D12(RayTracingInterface& rayTracingInterface); -void FillFunctionTableCommandBufferD3D12(RayTracingInterface& rayTracingInterface); -void FillFunctionTableAccelerationStructureD3D12(RayTracingInterface& rayTracingInterface); Result DeviceD3D12::FillFunctionTable(RayTracingInterface& rayTracingInterface) const { @@ -445,8 +392,8 @@ Result DeviceD3D12::FillFunctionTable(RayTracingInterface& rayTracingInterface) return Result::UNSUPPORTED; FillFunctionTablePipelineD3D12(rayTracingInterface); - FillFunctionTableAccelerationStructureD3D12(rayTracingInterface); - FillFunctionTableCommandBufferD3D12(rayTracingInterface); + RayTracing_AccelerationStructure_PartiallyFillFunctionTableD3D12(rayTracingInterface); + RayTracing_CommandBuffer_PartiallyFillFunctionTableD3D12(rayTracingInterface); rayTracingInterface.CreateRayTracingPipeline = ::CreateRayTracingPipeline; rayTracingInterface.CreateAccelerationStructure = ::CreateAccelerationStructure; @@ -456,15 +403,9 @@ Result DeviceD3D12::FillFunctionTable(RayTracingInterface& rayTracingInterface) return ValidateFunctionTable(GetLog(), rayTracingInterface); } -#endif - #pragma endregion -#pragma region [ MeshShaderInterface ] - -#ifdef __ID3D12GraphicsCommandList6_INTERFACE_DEFINED__ - -void FillFunctionTableCommandBufferD3D12(MeshShaderInterface& meshShaderInterface); +#pragma region [ MeshShader ] Result DeviceD3D12::FillFunctionTable(MeshShaderInterface& meshShaderInterface) const { @@ -473,36 +414,32 @@ Result DeviceD3D12::FillFunctionTable(MeshShaderInterface& meshShaderInterface) meshShaderInterface = {}; - FillFunctionTableCommandBufferD3D12(meshShaderInterface); + MeshShader_CommandBuffer_PartiallyFillFunctionTableD3D12(meshShaderInterface); return ValidateFunctionTable(GetLog(), meshShaderInterface); } -#endif - #pragma endregion -#pragma region [ HelperInterface ] +#pragma region [ Helper ] -static uint32_t NRI_CALL CountAllocationNumD3D12(Device& device, const ResourceGroupDesc& resourceGroupDesc) +static uint32_t NRI_CALL CountAllocationNum(Device& device, const ResourceGroupDesc& resourceGroupDesc) { return ((DeviceD3D12&)device).CalculateAllocationNumber(resourceGroupDesc); } -static Result NRI_CALL AllocateAndBindMemoryD3D12(Device& device, const ResourceGroupDesc& resourceGroupDesc, Memory** allocations) +static Result NRI_CALL AllocateAndBindMemory(Device& device, const ResourceGroupDesc& resourceGroupDesc, Memory** allocations) { return ((DeviceD3D12&)device).AllocateAndBindMemory(resourceGroupDesc, allocations); } -void FillFunctionTableCommandQueueD3D12(HelperInterface& helperInterface); - Result DeviceD3D12::FillFunctionTable(HelperInterface& helperInterface) const { helperInterface = {}; + helperInterface.CalculateAllocationNumber = ::CountAllocationNum; + helperInterface.AllocateAndBindMemory = ::AllocateAndBindMemory; - helperInterface.CalculateAllocationNumber = ::CountAllocationNumD3D12; - helperInterface.AllocateAndBindMemory = ::AllocateAndBindMemoryD3D12; - FillFunctionTableCommandQueueD3D12(helperInterface); + Helper_CommandQueue_PartiallyFillFunctionTableD3D12(helperInterface); return ValidateFunctionTable(GetLog(), helperInterface); } diff --git a/Source/D3D12/DeviceSemaphoreD3D12.cpp b/Source/D3D12/DeviceSemaphoreD3D12.cpp deleted file mode 100644 index ec8e7bc2..00000000 --- a/Source/D3D12/DeviceSemaphoreD3D12.cpp +++ /dev/null @@ -1,47 +0,0 @@ -/* -Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. - -NVIDIA CORPORATION and its licensors retain all intellectual property -and proprietary rights in and to this software, related documentation -and any modifications thereto. Any use, reproduction, disclosure or -distribution of this software and related documentation without an express -license agreement from NVIDIA CORPORATION is strictly prohibited. -*/ - -#include "SharedD3D12.h" -#include "DeviceSemaphoreD3D12.h" -#include "DeviceD3D12.h" - -using namespace nri; - -Result DeviceSemaphoreD3D12::Create(bool signaled) -{ - HRESULT hr = ((ID3D12Device*)m_Device)->CreateFence(m_SignalValue, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&m_Fence)); - if (FAILED(hr)) - { - REPORT_ERROR(m_Device.GetLog(), "ID3D12Device::CreateFence() failed, error code: 0x%X.", hr); - return Result::FAILURE; - } - - if (!signaled) - m_SignalValue++; - - return Result::SUCCESS; -} - -void DeviceSemaphoreD3D12::Signal(ID3D12CommandQueue* commandQueue) const -{ - commandQueue->Signal(m_Fence, m_SignalValue); -} - -void DeviceSemaphoreD3D12::Wait() -{ - while (m_Fence->GetCompletedValue() != m_SignalValue); - - m_SignalValue++; -} - -void DeviceSemaphoreD3D12::SetDebugName(const char* name) -{ - SET_D3D_DEBUG_OBJECT_NAME(m_Fence, name); -} diff --git a/Source/D3D12/DeviceSemaphoreD3D12.h b/Source/D3D12/DeviceSemaphoreD3D12.h deleted file mode 100644 index 1b479a18..00000000 --- a/Source/D3D12/DeviceSemaphoreD3D12.h +++ /dev/null @@ -1,62 +0,0 @@ -/* -Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. - -NVIDIA CORPORATION and its licensors retain all intellectual property -and proprietary rights in and to this software, related documentation -and any modifications thereto. Any use, reproduction, disclosure or -distribution of this software and related documentation without an express -license agreement from NVIDIA CORPORATION is strictly prohibited. -*/ - -#pragma once - -struct ID3D12Device; -struct ID3D12CommandQueue; -struct ID3D12Fence; - -namespace nri -{ - struct DeviceD3D12; - - struct DeviceSemaphoreD3D12 - { - DeviceSemaphoreD3D12(DeviceD3D12& device); - ~DeviceSemaphoreD3D12(); - - operator ID3D12Fence*() const; - - DeviceD3D12& GetDevice() const; - - Result Create(bool signaled); - - void Signal(ID3D12CommandQueue* commandQueue) const; - void Wait(); - - //================================================================================================================ - // NRI - //================================================================================================================ - void SetDebugName(const char* name); - - private: - DeviceD3D12& m_Device; - ComPtr m_Fence; - uint64_t m_SignalValue = 0; - }; - - inline DeviceSemaphoreD3D12::DeviceSemaphoreD3D12(DeviceD3D12& device) - : m_Device(device) - {} - - inline DeviceSemaphoreD3D12::~DeviceSemaphoreD3D12() - {} - - inline DeviceSemaphoreD3D12::operator ID3D12Fence*() const - { - return m_Fence.GetInterface(); - } - - inline DeviceD3D12& DeviceSemaphoreD3D12::GetDevice() const - { - return m_Device; - } -} diff --git a/Source/D3D12/FenceD3D12.cpp b/Source/D3D12/FenceD3D12.cpp new file mode 100644 index 00000000..efbf9c09 --- /dev/null +++ b/Source/D3D12/FenceD3D12.cpp @@ -0,0 +1,53 @@ +/* +Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. + +NVIDIA CORPORATION and its licensors retain all intellectual property +and proprietary rights in and to this software, related documentation +and any modifications thereto. Any use, reproduction, disclosure or +distribution of this software and related documentation without an express +license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "SharedD3D12.h" +#include "FenceD3D12.h" +#include "CommandQueueD3D12.h" + +using namespace nri; + +Result FenceD3D12::Create(uint64_t initialValue) +{ + HRESULT hr = ((ID3D12Device*)m_Device)->CreateFence(initialValue, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&m_Fence)); + RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "ID3D12Device::CreateFence() - FAILED!"); + + return Result::SUCCESS; +} + +//================================================================================================================ +// NRI +//================================================================================================================ + +inline uint64_t FenceD3D12::GetFenceValue() const +{ + return m_Fence->GetCompletedValue(); +} + +inline void FenceD3D12::QueueSignal(CommandQueueD3D12& commandQueue, uint64_t value) +{ + HRESULT hr = ((ID3D12CommandQueue*)commandQueue)->Signal(m_Fence, value); + CHECK(m_Device.GetLog(), hr == S_OK, "ID3D12CommandQueue::Signal() - FAILED!"); +} + +inline void FenceD3D12::QueueWait(CommandQueueD3D12& commandQueue, uint64_t value) +{ + HRESULT hr = ((ID3D12CommandQueue*)commandQueue)->Wait(m_Fence, value); + CHECK(m_Device.GetLog(), hr == S_OK, "ID3D12CommandQueue::Wait() - FAILED!"); +} + +inline void FenceD3D12::Wait(uint64_t value) +{ + // TODO: "busy wait" vs "wait for event"? + while (m_Fence->GetCompletedValue() < value) + ; +} + +#include "FenceD3D12.hpp" diff --git a/Source/D3D12/FenceD3D12.h b/Source/D3D12/FenceD3D12.h new file mode 100644 index 00000000..29696a61 --- /dev/null +++ b/Source/D3D12/FenceD3D12.h @@ -0,0 +1,50 @@ +/* +Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. + +NVIDIA CORPORATION and its licensors retain all intellectual property +and proprietary rights in and to this software, related documentation +and any modifications thereto. Any use, reproduction, disclosure or +distribution of this software and related documentation without an express +license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#pragma once + +namespace nri +{ + +struct DeviceD3D12; +struct CommandQueueD3D12; + +struct FenceD3D12 +{ + inline FenceD3D12(DeviceD3D12& device) : + m_Device(device) + {} + + inline ~FenceD3D12() + {} + + inline DeviceD3D12& GetDevice() const + { return m_Device; } + + Result Create(uint64_t initialValue); + + //================================================================================================================ + // NRI + //================================================================================================================ + + inline void SetDebugName(const char* name) + { SET_D3D_DEBUG_OBJECT_NAME(m_Fence, name); } + + uint64_t GetFenceValue() const; + void QueueSignal(CommandQueueD3D12& commandQueue, uint64_t value); + void QueueWait(CommandQueueD3D12& commandQueue, uint64_t value); + void Wait(uint64_t value); + +private: + DeviceD3D12& m_Device; + ComPtr m_Fence; +}; + +} diff --git a/Source/D3D12/FenceD3D12.hpp b/Source/D3D12/FenceD3D12.hpp new file mode 100644 index 00000000..4749400e --- /dev/null +++ b/Source/D3D12/FenceD3D12.hpp @@ -0,0 +1,40 @@ +/* +Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. + +NVIDIA CORPORATION and its licensors retain all intellectual property +and proprietary rights in and to this software, related documentation +and any modifications thereto. Any use, reproduction, disclosure or +distribution of this software and related documentation without an express +license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#pragma region [ Core ] + +static uint64_t NRI_CALL GetFenceValue(Fence& fence) +{ + return ((FenceD3D12&)fence).GetFenceValue(); +} + +static void NRI_CALL QueueSignal(CommandQueue& commandQueue, Fence& fence, uint64_t value) +{ + return ((FenceD3D12&)fence).QueueSignal((CommandQueueD3D12&)commandQueue, value); +} + +static void NRI_CALL QueueWait(CommandQueue& commandQueue, Fence& fence, uint64_t value) +{ + return ((FenceD3D12&)fence).QueueWait((CommandQueueD3D12&)commandQueue, value); +} + +static void NRI_CALL Wait(Fence& fence, uint64_t value) +{ + ((FenceD3D12&)fence).Wait(value); +} + +static void NRI_CALL SetFenceDebugName(Fence& fence, const char* name) +{ + ((FenceD3D12&)fence).SetDebugName(name); +} + +#pragma endregion + +Define_Core_Fence_PartiallyFillFunctionTable(D3D12) diff --git a/Source/D3D12/FrameBufferD3D12.cpp b/Source/D3D12/FrameBufferD3D12.cpp index 59f51a44..11ad2a6e 100644 --- a/Source/D3D12/FrameBufferD3D12.cpp +++ b/Source/D3D12/FrameBufferD3D12.cpp @@ -10,19 +10,12 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include "SharedD3D12.h" #include "FrameBufferD3D12.h" -#include "DeviceD3D12.h" #include "DescriptorD3D12.h" using namespace nri; extern void ConvertRects(D3D12_RECT* rectsD3D12, const Rect* rects, uint32_t rectNum); -FrameBufferD3D12::FrameBufferD3D12(DeviceD3D12& device) - : m_Device(device) - , m_RenderTargets(device.GetStdAllocator()) - , m_ClearDescs(device.GetStdAllocator()) -{} - Result FrameBufferD3D12::Create(const FrameBufferDesc& frameBufferDesc) { if (frameBufferDesc.colorAttachmentNum) @@ -103,8 +96,3 @@ void FrameBufferD3D12::Clear(ID3D12GraphicsCommandList* graphicsCommandList, con } } } - -void FrameBufferD3D12::SetDebugName(const char* name) -{ - MaybeUnused(name); -} diff --git a/Source/D3D12/FrameBufferD3D12.h b/Source/D3D12/FrameBufferD3D12.h index 2641ba39..d30f944e 100644 --- a/Source/D3D12/FrameBufferD3D12.h +++ b/Source/D3D12/FrameBufferD3D12.h @@ -12,36 +12,39 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct DeviceD3D12; - struct FrameBufferD3D12 - { - FrameBufferD3D12(DeviceD3D12& device); - ~FrameBufferD3D12(); +struct DeviceD3D12; - DeviceD3D12& GetDevice() const; +struct FrameBufferD3D12 +{ + inline FrameBufferD3D12(DeviceD3D12& device) + : m_Device(device) + , m_RenderTargets(device.GetStdAllocator()) + , m_ClearDescs(device.GetStdAllocator()) + {} + + inline ~FrameBufferD3D12() + {} - Result Create(const FrameBufferDesc& frameBufferDesc); - void Bind(ID3D12GraphicsCommandList* graphicsCommandList, RenderPassBeginFlag renderPassBeginFlag); - void Clear(ID3D12GraphicsCommandList* graphicsCommandList, const ClearDesc* clearDescs, uint32_t clearDescNum, const Rect* rects, uint32_t rectNum); + inline DeviceD3D12& GetDevice() const + { return m_Device; } - //================================================================================================================ - // NRI - //================================================================================================================ - void SetDebugName(const char* name); + Result Create(const FrameBufferDesc& frameBufferDesc); + void Bind(ID3D12GraphicsCommandList* graphicsCommandList, RenderPassBeginFlag renderPassBeginFlag); + void Clear(ID3D12GraphicsCommandList* graphicsCommandList, const ClearDesc* clearDescs, uint32_t clearDescNum, const Rect* rects, uint32_t rectNum); - private: - DeviceD3D12& m_Device; - Vector m_RenderTargets; - D3D12_CPU_DESCRIPTOR_HANDLE m_DepthStencilTarget = {}; - Vector m_ClearDescs; - }; + //================================================================================================================ + // NRI + //================================================================================================================ - inline FrameBufferD3D12::~FrameBufferD3D12() - {} + inline void SetDebugName(const char* name) + { MaybeUnused(name); } + +private: + DeviceD3D12& m_Device; + Vector m_RenderTargets; + D3D12_CPU_DESCRIPTOR_HANDLE m_DepthStencilTarget = {}; + Vector m_ClearDescs; +}; - inline DeviceD3D12& FrameBufferD3D12::GetDevice() const - { - return m_Device; - } } diff --git a/Source/D3D12/MemoryD3D12.cpp b/Source/D3D12/MemoryD3D12.cpp index 986ac54c..7aa954f7 100644 --- a/Source/D3D12/MemoryD3D12.cpp +++ b/Source/D3D12/MemoryD3D12.cpp @@ -10,7 +10,6 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include "SharedD3D12.h" #include "MemoryD3D12.h" -#include "DeviceD3D12.h" using namespace nri; @@ -52,8 +51,3 @@ Result MemoryD3D12::Create(const MemoryD3D12Desc& memoryDesc) return Result::SUCCESS; } - -void MemoryD3D12::SetDebugName(const char* name) -{ - SET_D3D_DEBUG_OBJECT_NAME(m_Heap, name); -} diff --git a/Source/D3D12/MemoryD3D12.h b/Source/D3D12/MemoryD3D12.h index a9598394..8e1decfe 100644 --- a/Source/D3D12/MemoryD3D12.h +++ b/Source/D3D12/MemoryD3D12.h @@ -12,57 +12,44 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct DeviceD3D12; - struct MemoryD3D12 - { - MemoryD3D12(DeviceD3D12& device); - ~MemoryD3D12(); +struct DeviceD3D12; - operator ID3D12Heap*() const; +struct MemoryD3D12 +{ + inline MemoryD3D12(DeviceD3D12& device) + : m_Device(device) + {} - DeviceD3D12& GetDevice() const; + inline ~MemoryD3D12() + {} - Result Create(const MemoryType memoryType, uint64_t size); - Result Create(const MemoryD3D12Desc& memoryDesc); - bool RequiresDedicatedAllocation() const; - const D3D12_HEAP_DESC& GetHeapDesc() const; + inline operator ID3D12Heap*() const + { return m_Heap.GetInterface(); } - //====================================================================================================================== - // NRI - //====================================================================================================================== - void SetDebugName(const char* name); + inline const D3D12_HEAP_DESC& GetHeapDesc() const + { return m_HeapDesc; } - private: - DeviceD3D12& m_Device; - ComPtr m_Heap; - D3D12_HEAP_DESC m_HeapDesc = {}; - }; + inline bool RequiresDedicatedAllocation() const + { return m_Heap.GetInterface() ? false : true; } - inline MemoryD3D12::MemoryD3D12(DeviceD3D12& device) - : m_Device(device) - {} + inline DeviceD3D12& GetDevice() const + { return m_Device; } - inline MemoryD3D12::~MemoryD3D12() - {} + Result Create(const MemoryType memoryType, uint64_t size); + Result Create(const MemoryD3D12Desc& memoryDesc); - inline MemoryD3D12::operator ID3D12Heap*() const - { - return m_Heap.GetInterface(); - } + //================================================================================================================ + // NRI + //================================================================================================================ - inline const D3D12_HEAP_DESC& MemoryD3D12::GetHeapDesc() const - { - return m_HeapDesc; - } + inline void SetDebugName(const char* name) + { SET_D3D_DEBUG_OBJECT_NAME(m_Heap, name); } - inline bool MemoryD3D12::RequiresDedicatedAllocation() const - { - return m_Heap.GetInterface() ? false : true; - } +private: + DeviceD3D12& m_Device; + ComPtr m_Heap; + D3D12_HEAP_DESC m_HeapDesc = {}; +}; - inline DeviceD3D12& MemoryD3D12::GetDevice() const - { - return m_Device; - } } diff --git a/Source/D3D12/PipelineD3D12.cpp b/Source/D3D12/PipelineD3D12.cpp index ff0d0ea6..2915386c 100644 --- a/Source/D3D12/PipelineD3D12.cpp +++ b/Source/D3D12/PipelineD3D12.cpp @@ -10,8 +10,6 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include "SharedD3D12.h" #include "PipelineD3D12.h" -#include "DeviceD3D12.h" -#include "DescriptorSetD3D12.h" #include "PipelineLayoutD3D12.h" using namespace nri; @@ -74,7 +72,6 @@ typedef PipelineDescComponentQueryInterface(&m_StateObjectProperties); return Result::SUCCESS; -#else - return Result::FAILURE; -#endif -} - -Result PipelineD3D12::WriteShaderGroupIdentifiers(uint32_t baseShaderGroupIndex, uint32_t shaderGroupNum, void* buffer) const -{ -#ifdef __ID3D12Device5_INTERFACE_DEFINED__ - uint8_t* byteBuffer = (uint8_t*)buffer; - for (uint32_t i = 0; i < shaderGroupNum; i++) - { - memcpy(byteBuffer + i * D3D12_RAYTRACING_SHADER_TABLE_BYTE_ALIGNMENT, m_StateObjectProperties->GetShaderIdentifier(m_ShaderGroupNames[baseShaderGroupIndex + i].c_str()), - (size_t)m_Device.GetDesc().rayTracingShaderGroupIdentifierSize); - } - - return Result::SUCCESS; -#else - return Result::FAILURE; -#endif } void PipelineD3D12::Bind(ID3D12GraphicsCommandList* graphicsCommandList, D3D12_PRIMITIVE_TOPOLOGY& primitiveTopology) const { -#ifdef __ID3D12Device5_INTERFACE_DEFINED__ if (m_StateObject) ((ID3D12GraphicsCommandList4*)graphicsCommandList)->SetPipelineState1(m_StateObject); else -#endif - graphicsCommandList->SetPipelineState(m_PipelineState); + graphicsCommandList->SetPipelineState(m_PipelineState); if (m_IsGraphicsPipeline) { @@ -573,9 +546,20 @@ void PipelineD3D12::FillSampleDesc(DXGI_SAMPLE_DESC& sampleDesc, UINT& sampleMas m_SampleNum = rasterizationDesc.sampleNum; } -void PipelineD3D12::SetDebugName(const char* name) +//================================================================================================================ +// NRI +//================================================================================================================ + +inline Result PipelineD3D12::WriteShaderGroupIdentifiers(uint32_t baseShaderGroupIndex, uint32_t shaderGroupNum, void* buffer) const { - SET_D3D_DEBUG_OBJECT_NAME(m_PipelineState, name); + uint8_t* byteBuffer = (uint8_t*)buffer; + for (uint32_t i = 0; i < shaderGroupNum; i++) + { + memcpy(byteBuffer + i * D3D12_RAYTRACING_SHADER_TABLE_BYTE_ALIGNMENT, m_StateObjectProperties->GetShaderIdentifier(m_ShaderGroupNames[baseShaderGroupIndex + i].c_str()), + (size_t)m_Device.GetDesc().rayTracingShaderGroupIdentifierSize); + } + + return Result::SUCCESS; } #include "PipelineD3D12.hpp" diff --git a/Source/D3D12/PipelineD3D12.h b/Source/D3D12/PipelineD3D12.h index a62ba8e4..33103e82 100644 --- a/Source/D3D12/PipelineD3D12.h +++ b/Source/D3D12/PipelineD3D12.h @@ -14,87 +14,72 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct DeviceD3D12; - struct PipelineLayoutD3D12; - struct CommandBufferD3D12; - - struct PipelineD3D12 - { - PipelineD3D12(DeviceD3D12& device); - ~PipelineD3D12(); - - DeviceD3D12& GetDevice() const; - - operator ID3D12PipelineState*() const; - - Result Create(const GraphicsPipelineDesc& graphicsPipelineDesc); - Result Create(const ComputePipelineDesc& computePipelineDesc); - Result Create(const RayTracingPipelineDesc& rayTracingPipelineDesc); - Result WriteShaderGroupIdentifiers(uint32_t baseShaderGroupIndex, uint32_t shaderGroupNum, void* buffer) const; - - void Bind(ID3D12GraphicsCommandList* graphicsCommandList, D3D12_PRIMITIVE_TOPOLOGY& primitiveTopology) const; - bool IsGraphicsPipeline() const; - uint32_t GetIAStreamStride(uint32_t streamSlot) const; - uint8_t GetSampleNum() const; - const PipelineLayoutD3D12& GetPipelineLayout() const; - - //====================================================================================================================== - // NRI - //====================================================================================================================== - void SetDebugName(const char* name); - - private: - Result CreateFromStream(const GraphicsPipelineDesc& graphicsPipelineDesc); - void FillInputLayout(D3D12_INPUT_LAYOUT_DESC& inputLayoutDesc, const GraphicsPipelineDesc& graphicsPipelineDesc); - void FillShaderBytecode(D3D12_SHADER_BYTECODE& shaderBytecode, const ShaderDesc& shaderDesc) const; - void FillRasterizerState(D3D12_RASTERIZER_DESC& rasterizerDesc, const GraphicsPipelineDesc& graphicsPipelineDesc); - void FillDepthStencilState(D3D12_DEPTH_STENCIL_DESC& depthStencilDesc, const OutputMergerDesc& outputMergerDesc) const; - void FillBlendState(D3D12_BLEND_DESC& blendDesc, const GraphicsPipelineDesc& graphicsPipelineDesc); - void FillSampleDesc(DXGI_SAMPLE_DESC& sampleDesc, UINT& sampleMask, const GraphicsPipelineDesc& graphicsPipelineDesc); - - DeviceD3D12& m_Device; - std::array m_IAStreamStride = {}; // TODO: optimize? - ComPtr m_PipelineState; -#ifdef __ID3D12Device5_INTERFACE_DEFINED__ - ComPtr m_StateObject; - ComPtr m_StateObjectProperties; - Vector m_ShaderGroupNames; -#endif - const PipelineLayoutD3D12* m_PipelineLayout = nullptr; - D3D_PRIMITIVE_TOPOLOGY m_PrimitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED; - Color32f m_BlendFactor = {}; - uint8_t m_SampleNum = 1; - bool m_BlendEnabled = false; - bool m_IsGraphicsPipeline = false; - }; - - inline PipelineD3D12::PipelineD3D12(DeviceD3D12& device) + +struct DeviceD3D12; +struct PipelineLayoutD3D12; +struct CommandBufferD3D12; + +struct PipelineD3D12 +{ + inline PipelineD3D12(DeviceD3D12& device) : m_Device(device) -#ifdef __ID3D12Device5_INTERFACE_DEFINED__ , m_ShaderGroupNames(device.GetStdAllocator()) -#endif {} - inline PipelineD3D12::~PipelineD3D12() + inline ~PipelineD3D12() {} - inline PipelineD3D12::operator ID3D12PipelineState*() const - { - return m_PipelineState.GetInterface(); - } - - inline bool PipelineD3D12::IsGraphicsPipeline() const - { - return m_IsGraphicsPipeline; - } - - inline DeviceD3D12& PipelineD3D12::GetDevice() const - { - return m_Device; - } - - inline const PipelineLayoutD3D12& PipelineD3D12::GetPipelineLayout() const - { - return *m_PipelineLayout; - } + inline operator ID3D12PipelineState*() const + { return m_PipelineState.GetInterface(); } + + inline bool IsGraphicsPipeline() const + { return m_IsGraphicsPipeline; } + + inline DeviceD3D12& GetDevice() const + { return m_Device; } + + inline const PipelineLayoutD3D12& GetPipelineLayout() const + { return *m_PipelineLayout; } + + Result Create(const GraphicsPipelineDesc& graphicsPipelineDesc); + Result Create(const ComputePipelineDesc& computePipelineDesc); + Result Create(const RayTracingPipelineDesc& rayTracingPipelineDesc); + + void Bind(ID3D12GraphicsCommandList* graphicsCommandList, D3D12_PRIMITIVE_TOPOLOGY& primitiveTopology) const; + uint32_t GetIAStreamStride(uint32_t streamSlot) const; + uint8_t GetSampleNum() const; + + //================================================================================================================ + // NRI + //================================================================================================================ + + inline void SetDebugName(const char* name) + { SET_D3D_DEBUG_OBJECT_NAME(m_PipelineState, name); } + + Result WriteShaderGroupIdentifiers(uint32_t baseShaderGroupIndex, uint32_t shaderGroupNum, void* buffer) const; + +private: + Result CreateFromStream(const GraphicsPipelineDesc& graphicsPipelineDesc); + void FillInputLayout(D3D12_INPUT_LAYOUT_DESC& inputLayoutDesc, const GraphicsPipelineDesc& graphicsPipelineDesc); + void FillShaderBytecode(D3D12_SHADER_BYTECODE& shaderBytecode, const ShaderDesc& shaderDesc) const; + void FillRasterizerState(D3D12_RASTERIZER_DESC& rasterizerDesc, const GraphicsPipelineDesc& graphicsPipelineDesc); + void FillDepthStencilState(D3D12_DEPTH_STENCIL_DESC& depthStencilDesc, const OutputMergerDesc& outputMergerDesc) const; + void FillBlendState(D3D12_BLEND_DESC& blendDesc, const GraphicsPipelineDesc& graphicsPipelineDesc); + void FillSampleDesc(DXGI_SAMPLE_DESC& sampleDesc, UINT& sampleMask, const GraphicsPipelineDesc& graphicsPipelineDesc); + +private: + DeviceD3D12& m_Device; + std::array m_IAStreamStride = {}; // TODO: optimize? + ComPtr m_PipelineState; + ComPtr m_StateObject; + ComPtr m_StateObjectProperties; + Vector m_ShaderGroupNames; + const PipelineLayoutD3D12* m_PipelineLayout = nullptr; + D3D_PRIMITIVE_TOPOLOGY m_PrimitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED; + Color32f m_BlendFactor = {}; + uint8_t m_SampleNum = 1; + bool m_BlendEnabled = false; + bool m_IsGraphicsPipeline = false; +}; + } diff --git a/Source/D3D12/PipelineD3D12.hpp b/Source/D3D12/PipelineD3D12.hpp index 0056ae3e..19b40057 100644 --- a/Source/D3D12/PipelineD3D12.hpp +++ b/Source/D3D12/PipelineD3D12.hpp @@ -8,7 +8,7 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ RayTracingInterface ] +#pragma region [ RayTracing ] static Result NRI_CALL WriteShaderGroupIdentifiers(const Pipeline& pipeline, uint32_t baseShaderGroupIndex, uint32_t shaderGroupNum, void* buffer) { diff --git a/Source/D3D12/PipelineLayoutD3D12.cpp b/Source/D3D12/PipelineLayoutD3D12.cpp index 40b7861a..e7f11335 100644 --- a/Source/D3D12/PipelineLayoutD3D12.cpp +++ b/Source/D3D12/PipelineLayoutD3D12.cpp @@ -10,7 +10,6 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include "SharedD3D12.h" #include "PipelineLayoutD3D12.h" -#include "DeviceD3D12.h" using namespace nri; @@ -203,9 +202,46 @@ Result PipelineLayoutD3D12::Create(const PipelineLayoutDesc& pipelineLayoutDesc) return Result::SUCCESS; } -void PipelineLayoutD3D12::SetDebugName(const char* name) +template +void PipelineLayoutD3D12::SetDescriptorSetImpl(ID3D12GraphicsCommandList& graphicsCommandList, uint32_t setIndexInPipelineLayout, + const DescriptorSet& descriptorSet, const uint32_t* dynamicConstantBufferOffsets) const { - SET_D3D_DEBUG_OBJECT_NAME(m_RootSignature, name); + const DescriptorSetD3D12& descriptorSetImpl = (const DescriptorSetD3D12&)descriptorSet; + + const auto& rootOffsets = m_DescriptorSetRootMappings[setIndexInPipelineLayout].rootOffsets; + uint32_t descriptorRangeNum = (uint32_t)rootOffsets.size(); + for (uint32_t j = 0; j < descriptorRangeNum; j++) + { + uint16_t rootParameterIndex = rootOffsets[j]; + if (rootParameterIndex == ROOT_PARAMETER_UNUSED) + continue; + + DescriptorPointerGPU descriptorPointerGPU = descriptorSetImpl.GetPointerGPU(j, 0); + + if (isGraphics) + graphicsCommandList.SetGraphicsRootDescriptorTable(rootParameterIndex, { descriptorPointerGPU }); + else + graphicsCommandList.SetComputeRootDescriptorTable(rootParameterIndex, { descriptorPointerGPU }); + } + + const auto& dynamicConstantBufferMapping = m_DynamicConstantBufferMappings[setIndexInPipelineLayout]; + for (uint16_t j = 0; j < dynamicConstantBufferMapping.constantNum; j++) + { + uint16_t rootParameterIndex = dynamicConstantBufferMapping.rootOffset + j; + DescriptorPointerGPU descriptorPointerGPU = descriptorSetImpl.GetDynamicPointerGPU(j) + dynamicConstantBufferOffsets[j]; + + if (isGraphics) + graphicsCommandList.SetGraphicsRootConstantBufferView(rootParameterIndex, descriptorPointerGPU); + else + graphicsCommandList.SetComputeRootConstantBufferView(rootParameterIndex, descriptorPointerGPU); + } } -#include "PipelineLayoutD3D12.hpp" +void PipelineLayoutD3D12::SetDescriptorSet(ID3D12GraphicsCommandList& graphicsCommandList, bool isGraphics, + uint32_t setIndexInPipelineLayout, const DescriptorSet& descriptorSet, const uint32_t* dynamicConstantBufferOffsets) const +{ + if (isGraphics) + SetDescriptorSetImpl(graphicsCommandList, setIndexInPipelineLayout, descriptorSet, dynamicConstantBufferOffsets); + else + SetDescriptorSetImpl(graphicsCommandList, setIndexInPipelineLayout, descriptorSet, dynamicConstantBufferOffsets); +} diff --git a/Source/D3D12/PipelineLayoutD3D12.h b/Source/D3D12/PipelineLayoutD3D12.h index 3aa0f966..fcee2462 100644 --- a/Source/D3D12/PipelineLayoutD3D12.h +++ b/Source/D3D12/PipelineLayoutD3D12.h @@ -14,138 +14,76 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct DeviceD3D12; - constexpr uint16_t ROOT_PARAMETER_UNUSED = uint16_t(-1); +struct DeviceD3D12; - struct DescriptorSetRootMapping - { - DescriptorSetRootMapping(StdAllocator& allocator); +constexpr uint16_t ROOT_PARAMETER_UNUSED = uint16_t(-1); - Vector rootOffsets; - }; - - struct DynamicConstantBufferMapping - { - uint16_t constantNum; - uint16_t rootOffset; - }; - - struct PipelineLayoutD3D12 - { - PipelineLayoutD3D12(DeviceD3D12& device); - - operator ID3D12RootSignature*() const; +struct DescriptorSetRootMapping +{ + inline DescriptorSetRootMapping(StdAllocator& allocator) + : rootOffsets(allocator) + {} - DeviceD3D12& GetDevice() const; - bool IsGraphicsPipelineLayout() const; + Vector rootOffsets; +}; - const DescriptorSetMapping& GetDescriptorSetMapping(uint32_t setIndexInPipelineLayout) const; - const DescriptorSetRootMapping& GetDescriptorSetRootMapping(uint32_t setIndexInPipelineLayout) const; - const DynamicConstantBufferMapping& GetDynamicConstantBufferMapping(uint32_t setIndexInPipelineLayout) const; - uint32_t GetPushConstantsRootOffset(uint32_t rangeIndex) const; +struct DynamicConstantBufferMapping +{ + uint16_t constantNum; + uint16_t rootOffset; +}; - void SetDescriptorSet(ID3D12GraphicsCommandList& graphicsCommandList, bool isGraphics, - uint32_t setIndexInPipelineLayout, const DescriptorSet& descriptorSet, const uint32_t* dynamicConstantBufferOffsets) const; +struct PipelineLayoutD3D12 +{ + PipelineLayoutD3D12(DeviceD3D12& device); - Result Create(const PipelineLayoutDesc& pipelineLayoutDesc); + inline operator ID3D12RootSignature*() const + { return m_RootSignature.GetInterface(); } - void SetDebugName(const char* name); + inline DeviceD3D12& GetDevice() const + { return m_Device; } - private: - template - void SetDescriptorSetImpl(ID3D12GraphicsCommandList& graphicsCommandList, uint32_t setIndexInPipelineLayout, - const DescriptorSet& descriptorSet, const uint32_t* dynamicConstantBufferOffsets) const; + inline bool IsGraphicsPipelineLayout() const + { return m_IsGraphicsPipelineLayout; } - ComPtr m_RootSignature; - bool m_IsGraphicsPipelineLayout = false; - uint32_t m_PushConstantsBaseIndex = 0; - Vector m_DescriptorSetMappings; - Vector m_DescriptorSetRootMappings; - Vector m_DynamicConstantBufferMappings; - DeviceD3D12& m_Device; - }; + inline const DescriptorSetMapping& GetDescriptorSetMapping(uint32_t setIndexInPipelineLayout) const + { return m_DescriptorSetMappings[setIndexInPipelineLayout]; } - inline PipelineLayoutD3D12::operator ID3D12RootSignature*() const - { - return m_RootSignature.GetInterface(); - } + inline const DescriptorSetRootMapping& GetDescriptorSetRootMapping(uint32_t setIndexInPipelineLayout) const + { return m_DescriptorSetRootMappings[setIndexInPipelineLayout]; } - inline DeviceD3D12& PipelineLayoutD3D12::GetDevice() const - { - return m_Device; - } + inline const DynamicConstantBufferMapping& GetDynamicConstantBufferMapping(uint32_t setIndexInPipelineLayout) const + { return m_DynamicConstantBufferMappings[setIndexInPipelineLayout]; } - inline bool PipelineLayoutD3D12::IsGraphicsPipelineLayout() const - { - return m_IsGraphicsPipelineLayout; - } + inline uint32_t GetPushConstantsRootOffset(uint32_t rangeIndex) const + { return m_PushConstantsBaseIndex + rangeIndex; } - inline const DescriptorSetMapping& PipelineLayoutD3D12::GetDescriptorSetMapping(uint32_t setIndexInPipelineLayout) const - { - return m_DescriptorSetMappings[setIndexInPipelineLayout]; - } + Result Create(const PipelineLayoutDesc& pipelineLayoutDesc); - inline const DescriptorSetRootMapping& PipelineLayoutD3D12::GetDescriptorSetRootMapping(uint32_t setIndexInPipelineLayout) const - { - return m_DescriptorSetRootMappings[setIndexInPipelineLayout]; - } + void SetDescriptorSet(ID3D12GraphicsCommandList& graphicsCommandList, bool isGraphics, + uint32_t setIndexInPipelineLayout, const DescriptorSet& descriptorSet, const uint32_t* dynamicConstantBufferOffsets) const; - inline const DynamicConstantBufferMapping& PipelineLayoutD3D12::GetDynamicConstantBufferMapping(uint32_t setIndexInPipelineLayout) const - { - return m_DynamicConstantBufferMappings[setIndexInPipelineLayout]; - } + //================================================================================================================ + // NRI + //================================================================================================================ - inline uint32_t PipelineLayoutD3D12::GetPushConstantsRootOffset(uint32_t rangeIndex) const - { - return m_PushConstantsBaseIndex + rangeIndex; - } + inline void SetDebugName(const char* name) + { SET_D3D_DEBUG_OBJECT_NAME(m_RootSignature, name); } +private: template - inline void PipelineLayoutD3D12::SetDescriptorSetImpl(ID3D12GraphicsCommandList& graphicsCommandList, uint32_t setIndexInPipelineLayout, - const DescriptorSet& descriptorSet, const uint32_t* dynamicConstantBufferOffsets) const - { - const DescriptorSetD3D12& descriptorSetImpl = (const DescriptorSetD3D12&)descriptorSet; - - const auto& rootOffsets = m_DescriptorSetRootMappings[setIndexInPipelineLayout].rootOffsets; - uint32_t descriptorRangeNum = (uint32_t)rootOffsets.size(); - for (uint32_t j = 0; j < descriptorRangeNum; j++) - { - uint16_t rootParameterIndex = rootOffsets[j]; - if (rootParameterIndex == ROOT_PARAMETER_UNUSED) - continue; - - DescriptorPointerGPU descriptorPointerGPU = descriptorSetImpl.GetPointerGPU(j, 0); - - if (isGraphics) - graphicsCommandList.SetGraphicsRootDescriptorTable(rootParameterIndex, { descriptorPointerGPU }); - else - graphicsCommandList.SetComputeRootDescriptorTable(rootParameterIndex, { descriptorPointerGPU }); - } - - const auto& dynamicConstantBufferMapping = m_DynamicConstantBufferMappings[setIndexInPipelineLayout]; - for (uint16_t j = 0; j < dynamicConstantBufferMapping.constantNum; j++) - { - uint16_t rootParameterIndex = dynamicConstantBufferMapping.rootOffset + j; - DescriptorPointerGPU descriptorPointerGPU = descriptorSetImpl.GetDynamicPointerGPU(j) + dynamicConstantBufferOffsets[j]; - - if (isGraphics) - graphicsCommandList.SetGraphicsRootConstantBufferView(rootParameterIndex, descriptorPointerGPU); - else - graphicsCommandList.SetComputeRootConstantBufferView(rootParameterIndex, descriptorPointerGPU); - } - } - - inline void PipelineLayoutD3D12::SetDescriptorSet(ID3D12GraphicsCommandList& graphicsCommandList, bool isGraphics, - uint32_t setIndexInPipelineLayout, const DescriptorSet& descriptorSet, const uint32_t* dynamicConstantBufferOffsets) const - { - if (isGraphics) - SetDescriptorSetImpl(graphicsCommandList, setIndexInPipelineLayout, descriptorSet, dynamicConstantBufferOffsets); - else - SetDescriptorSetImpl(graphicsCommandList, setIndexInPipelineLayout, descriptorSet, dynamicConstantBufferOffsets); - } - - inline DescriptorSetRootMapping::DescriptorSetRootMapping(StdAllocator& allocator) - : rootOffsets(allocator) - {} + void SetDescriptorSetImpl(ID3D12GraphicsCommandList& graphicsCommandList, uint32_t setIndexInPipelineLayout, + const DescriptorSet& descriptorSet, const uint32_t* dynamicConstantBufferOffsets) const; + +private: + ComPtr m_RootSignature; + bool m_IsGraphicsPipelineLayout = false; + uint32_t m_PushConstantsBaseIndex = 0; + Vector m_DescriptorSetMappings; + Vector m_DescriptorSetRootMappings; + Vector m_DynamicConstantBufferMappings; + DeviceD3D12& m_Device; +}; + } \ No newline at end of file diff --git a/Source/D3D12/PipelineLayoutD3D12.hpp b/Source/D3D12/PipelineLayoutD3D12.hpp deleted file mode 100644 index 630924a8..00000000 --- a/Source/D3D12/PipelineLayoutD3D12.hpp +++ /dev/null @@ -1,23 +0,0 @@ -/* -Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. - -NVIDIA CORPORATION and its licensors retain all intellectual property -and proprietary rights in and to this software, related documentation -and any modifications thereto. Any use, reproduction, disclosure or -distribution of this software and related documentation without an express -license agreement from NVIDIA CORPORATION is strictly prohibited. -*/ - -#pragma region [ CoreInterface ] - -static void NRI_CALL SetPipelineLayoutDebugName(PipelineLayout& pipelineLayout, const char* name) -{ - ((PipelineLayoutD3D12&)pipelineLayout).SetDebugName(name); -} - -void FillFunctionTablePipelineLayoutD3D12(CoreInterface& coreInterface) -{ - coreInterface.SetPipelineLayoutDebugName = ::SetPipelineLayoutDebugName; -} - -#pragma endregion diff --git a/Source/D3D12/QueryPoolD3D12.cpp b/Source/D3D12/QueryPoolD3D12.cpp index 87b2a176..b8167d09 100644 --- a/Source/D3D12/QueryPoolD3D12.cpp +++ b/Source/D3D12/QueryPoolD3D12.cpp @@ -10,7 +10,6 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include "SharedD3D12.h" #include "QueryPoolD3D12.h" -#include "DeviceD3D12.h" using namespace nri; @@ -71,9 +70,8 @@ Result QueryPoolD3D12::CreateReadbackBuffer(const QueryPoolDesc& queryPoolDesc) return Result::SUCCESS; } -inline void QueryPoolD3D12::SetDebugName(const char* name) -{ - SET_D3D_DEBUG_OBJECT_NAME(m_QueryHeap, name); -} +//================================================================================================================ +// NRI +//================================================================================================================ #include "QueryPoolD3D12.hpp" diff --git a/Source/D3D12/QueryPoolD3D12.h b/Source/D3D12/QueryPoolD3D12.h index 487350b2..a97eca6b 100644 --- a/Source/D3D12/QueryPoolD3D12.h +++ b/Source/D3D12/QueryPoolD3D12.h @@ -12,66 +12,50 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct DeviceD3D12; - struct QueryPoolD3D12 - { - QueryPoolD3D12(DeviceD3D12& device); - ~QueryPoolD3D12(); +struct DeviceD3D12; - operator ID3D12QueryHeap*() const; +struct QueryPoolD3D12 +{ + inline QueryPoolD3D12(DeviceD3D12& device) + : m_Device(device) + {} - DeviceD3D12& GetDevice() const; + inline ~QueryPoolD3D12() + {} - Result Create(const QueryPoolDesc& queryPoolDesc); - D3D12_QUERY_TYPE GetType() const; - ID3D12Resource* GetReadbackBuffer() const; + inline operator ID3D12QueryHeap*() const + { return m_QueryHeap.GetInterface(); } - //====================================================================================================================== - // NRI - //====================================================================================================================== - void SetDebugName(const char* name); - uint32_t GetQuerySize() const; + inline D3D12_QUERY_TYPE GetType() const + { return m_QueryType; } - private: - Result CreateReadbackBuffer(const QueryPoolDesc& queryPoolDesc); + inline ID3D12Resource* GetReadbackBuffer() const + { return m_ReadbackBuffer.GetInterface(); } - DeviceD3D12& m_Device; - D3D12_QUERY_TYPE m_QueryType = D3D12_QUERY_TYPE(-1); - uint32_t m_QuerySize = 0; - ComPtr m_QueryHeap; - ComPtr m_ReadbackBuffer; - }; + inline DeviceD3D12& GetDevice() const + { return m_Device; } - inline QueryPoolD3D12::QueryPoolD3D12(DeviceD3D12& device) - : m_Device(device) - {} + Result Create(const QueryPoolDesc& queryPoolDesc); - inline QueryPoolD3D12::~QueryPoolD3D12() - {} + //================================================================================================================ + // NRI + //================================================================================================================ + + inline void SetDebugName(const char* name) + { SET_D3D_DEBUG_OBJECT_NAME(m_QueryHeap, name); } + + inline uint32_t GetQuerySize() const + { return m_QuerySize; } + +private: + Result CreateReadbackBuffer(const QueryPoolDesc& queryPoolDesc); + + DeviceD3D12& m_Device; + D3D12_QUERY_TYPE m_QueryType = D3D12_QUERY_TYPE(-1); + uint32_t m_QuerySize = 0; + ComPtr m_QueryHeap; + ComPtr m_ReadbackBuffer; +}; - inline QueryPoolD3D12::operator ID3D12QueryHeap*() const - { - return m_QueryHeap.GetInterface(); - } - - inline D3D12_QUERY_TYPE QueryPoolD3D12::GetType() const - { - return m_QueryType; - } - - inline ID3D12Resource* QueryPoolD3D12::GetReadbackBuffer() const - { - return m_ReadbackBuffer.GetInterface(); - } - - inline DeviceD3D12& QueryPoolD3D12::GetDevice() const - { - return m_Device; - } - - inline uint32_t QueryPoolD3D12::GetQuerySize() const - { - return m_QuerySize; - } } diff --git a/Source/D3D12/QueryPoolD3D12.hpp b/Source/D3D12/QueryPoolD3D12.hpp index 64095494..68d0bb00 100644 --- a/Source/D3D12/QueryPoolD3D12.hpp +++ b/Source/D3D12/QueryPoolD3D12.hpp @@ -8,7 +8,7 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ CoreInterface ] +#pragma region [ Core ] static void NRI_CALL SetQueryPoolDebugName(QueryPool& queryPool, const char* name) { @@ -20,10 +20,6 @@ static uint32_t NRI_CALL GetQuerySize(const QueryPool& queryPool) return ((QueryPoolD3D12&)queryPool).GetQuerySize(); } -void FillFunctionTableQueryPoolD3D12(CoreInterface& coreInterface) -{ - coreInterface.SetQueryPoolDebugName = ::SetQueryPoolDebugName; - coreInterface.GetQuerySize = ::GetQuerySize; -} - #pragma endregion + +Define_Core_QueryPool_PartiallyFillFunctionTable(D3D12) diff --git a/Source/D3D12/QueueSemaphoreD3D12.cpp b/Source/D3D12/QueueSemaphoreD3D12.cpp deleted file mode 100644 index 367f7979..00000000 --- a/Source/D3D12/QueueSemaphoreD3D12.cpp +++ /dev/null @@ -1,45 +0,0 @@ -/* -Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. - -NVIDIA CORPORATION and its licensors retain all intellectual property -and proprietary rights in and to this software, related documentation -and any modifications thereto. Any use, reproduction, disclosure or -distribution of this software and related documentation without an express -license agreement from NVIDIA CORPORATION is strictly prohibited. -*/ - -#include "SharedD3D12.h" -#include "QueueSemaphoreD3D12.h" -#include "DeviceD3D12.h" - -using namespace nri; - -Result QueueSemaphoreD3D12::Create() -{ - HRESULT hr = ((ID3D12Device*)m_Device)->CreateFence(m_SignalValue, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&m_Fence)); - if (FAILED(hr)) - { - REPORT_ERROR(m_Device.GetLog(), "ID3D12Device::CreateFence() failed, error code: 0x%X.", hr); - return Result::FAILURE; - } - - m_SignalValue++; - - return Result::SUCCESS; -} - -void QueueSemaphoreD3D12::Signal(ID3D12CommandQueue* commandQueue) -{ - commandQueue->Signal(m_Fence, m_SignalValue); -} - -void QueueSemaphoreD3D12::Wait(ID3D12CommandQueue* commandQueue) -{ - commandQueue->Wait(m_Fence, m_SignalValue); - m_SignalValue++; -} - -void QueueSemaphoreD3D12::SetDebugName(const char* name) -{ - SET_D3D_DEBUG_OBJECT_NAME(m_Fence, name); -} diff --git a/Source/D3D12/QueueSemaphoreD3D12.h b/Source/D3D12/QueueSemaphoreD3D12.h deleted file mode 100644 index ec4e8c8b..00000000 --- a/Source/D3D12/QueueSemaphoreD3D12.h +++ /dev/null @@ -1,62 +0,0 @@ -/* -Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. - -NVIDIA CORPORATION and its licensors retain all intellectual property -and proprietary rights in and to this software, related documentation -and any modifications thereto. Any use, reproduction, disclosure or -distribution of this software and related documentation without an express -license agreement from NVIDIA CORPORATION is strictly prohibited. -*/ - -#pragma once - -struct ID3D12Device; -struct ID3D12CommandQueue; -struct ID3D12Fence; - -namespace nri -{ - struct DeviceD3D12; - - struct QueueSemaphoreD3D12 - { - QueueSemaphoreD3D12(DeviceD3D12& device); - ~QueueSemaphoreD3D12(); - - Result Create(); - - operator ID3D12Fence*() const; - - DeviceD3D12& GetDevice() const; - - void Signal(ID3D12CommandQueue* commandQueue); - void Wait(ID3D12CommandQueue* commandQueue); - - //================================================================================================================ - // NRI - //================================================================================================================ - void SetDebugName(const char* name); - - private: - DeviceD3D12& m_Device; - ComPtr m_Fence; - uint64_t m_SignalValue = 0; - }; - - inline QueueSemaphoreD3D12::QueueSemaphoreD3D12(DeviceD3D12& device) - : m_Device(device) - {} - - inline QueueSemaphoreD3D12::~QueueSemaphoreD3D12() - {} - - inline QueueSemaphoreD3D12::operator ID3D12Fence*() const - { - return m_Fence.GetInterface(); - } - - inline DeviceD3D12& QueueSemaphoreD3D12::GetDevice() const - { - return m_Device; - } -} diff --git a/Source/D3D12/SharedD3D12.h b/Source/D3D12/SharedD3D12.h index fd4aa5ef..a8681ed9 100644 --- a/Source/D3D12/SharedD3D12.h +++ b/Source/D3D12/SharedD3D12.h @@ -14,8 +14,8 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include "SharedExternal.h" #include "DeviceBase.h" +#include "DeviceD3D12.h" DXGI_FORMAT GetDXGIFormat(nri::Format format); #define NRI_TEMP_NODE_MASK 0x1 -#define SET_D3D_DEBUG_OBJECT_NAME(obj, name) if (obj) obj->SetPrivateData(WKPDID_D3DDebugObjectName, (UINT)std::strlen(name), name) diff --git a/Source/D3D12/SwapChainD3D12.cpp b/Source/D3D12/SwapChainD3D12.cpp index 29c6dc26..974b3b6e 100644 --- a/Source/D3D12/SwapChainD3D12.cpp +++ b/Source/D3D12/SwapChainD3D12.cpp @@ -8,18 +8,16 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ +#include + #include "SharedD3D12.h" #include "SwapChainD3D12.h" -#include "DeviceD3D12.h" -#include "CommandQueueD3D12.h" #include "TextureD3D12.h" -#include "QueueSemaphoreD3D12.h" - -#include +#include "CommandQueueD3D12.h" using namespace nri; -static std::array g_SwapChainFormat = +static std::array g_swapChainFormat = { DXGI_FORMAT_R8G8B8A8_UNORM, // BT709_G10_8BIT, DXGI_FORMAT_R16G16B16A16_FLOAT, // BT709_G10_16BIT, @@ -28,7 +26,7 @@ static std::array g_SwapChainFormat = DXGI_FORMAT_R10G10B10A2_UNORM, // BT2020_G2084_10BIT }; -static std::array g_ColorSpace = +static std::array g_colorSpace = { DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709, // BT709_G10_8BIT, DXGI_COLOR_SPACE_RGB_FULL_G10_NONE_P709, // BT709_G10_16BIT, @@ -37,7 +35,7 @@ static std::array g_ColorSpace = DXGI_COLOR_SPACE_RGB_FULL_G2084_NONE_P2020, // BT2020_G2084_10BIT }; -static std::array g_SwapChainTextureFormat = +static std::array g_swapChainTextureFormat = { Format::RGBA8_SRGB, // BT709_G10_8BIT, Format::RGBA16_SFLOAT, // BT709_G10_16BIT, @@ -46,12 +44,6 @@ static std::array g_SwapChainTextureFormat = Format::R10_G10_B10_A2_UNORM, // BT2020_G2084_10BIT }; -SwapChainD3D12::SwapChainD3D12(DeviceD3D12& device) - : m_Device(device) - , m_Textures(device.GetStdAllocator()) - , m_TexturePointer(device.GetStdAllocator()) -{} - SwapChainD3D12::~SwapChainD3D12() { if (m_IsFullscreenEnabled) @@ -61,23 +53,31 @@ SwapChainD3D12::~SwapChainD3D12() if (fullscreen) m_SwapChain->SetFullscreenState(FALSE, nullptr); } + + for (TextureD3D12* texture : m_Textures) + Deallocate(m_Device.GetStdAllocator(), texture); } Result SwapChainD3D12::Create(const SwapChainDesc& swapChainDesc) { + HWND hwnd = (HWND)swapChainDesc.window.windows.hwnd; + if (!hwnd) + return Result::INVALID_ARGUMENT; + ID3D12Device* device = m_Device; + CommandQueueD3D12& commandQueue = *(CommandQueueD3D12*)swapChainDesc.commandQueue; - ComPtr factory; - HRESULT hr = CreateDXGIFactory2(0, IID_PPV_ARGS(&factory)); - RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "CreateDXGIFactory2(), error code: 0x%X.", hr); + ComPtr dxgifactory4; + HRESULT hr = CreateDXGIFactory2(0, IID_PPV_ARGS(&dxgifactory4)); + RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "CreateDXGIFactory2() - FAILED!"); ComPtr adapter; - hr = factory->EnumAdapterByLuid(device->GetAdapterLuid(), IID_PPV_ARGS(&adapter)); - RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "IDXGIFactory4::EnumAdapterByLuid(), error code: 0x%X.", hr); + hr = dxgifactory4->EnumAdapterByLuid(device->GetAdapterLuid(), IID_PPV_ARGS(&adapter)); + RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "IDXGIFactory4::EnumAdapterByLuid() - FAILED!"); m_IsTearingAllowed = false; ComPtr dxgiFactory5; - hr = factory->QueryInterface(IID_PPV_ARGS(&dxgiFactory5)); + hr = dxgifactory4->QueryInterface(IID_PPV_ARGS(&dxgiFactory5)); if (SUCCEEDED(hr)) { uint32_t tearingSupport = 0; @@ -85,52 +85,85 @@ Result SwapChainD3D12::Create(const SwapChainDesc& swapChainDesc) m_IsTearingAllowed = (SUCCEEDED(hr) && tearingSupport) ? true : false; } - CommandQueue* commandQueue; - if (m_Device.GetCommandQueue(CommandQueueType::GRAPHICS, commandQueue) != Result::SUCCESS) - return Result::FAILURE; - - CommandQueueD3D12& commandQueueD3D12 = (CommandQueueD3D12&)*commandQueue; - - DXGI_FORMAT format = g_SwapChainFormat[(uint32_t)swapChainDesc.format]; - DXGI_COLOR_SPACE_TYPE colorSpace = g_ColorSpace[(uint32_t)swapChainDesc.format]; - - const HWND window = (HWND)swapChainDesc.window.windows.hwnd; - - if (!window) - return Result::INVALID_ARGUMENT; - - DXGI_SWAP_CHAIN_DESC1 swapChainDesc1 = {}; - swapChainDesc1.BufferCount = swapChainDesc.textureNum; - swapChainDesc1.Width = swapChainDesc.width; - swapChainDesc1.Height = swapChainDesc.height; - swapChainDesc1.Format = format; - swapChainDesc1.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; - swapChainDesc1.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD; - swapChainDesc1.SampleDesc.Count = 1; - swapChainDesc1.Flags = m_IsTearingAllowed ? DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING : 0; - swapChainDesc1.Scaling = DXGI_SCALING_NONE; + DXGI_FORMAT format = g_swapChainFormat[(uint32_t)swapChainDesc.format]; + DXGI_COLOR_SPACE_TYPE colorSpace = g_colorSpace[(uint32_t)swapChainDesc.format]; + + DXGI_SWAP_CHAIN_DESC1 desc = {}; + desc.BufferCount = swapChainDesc.textureNum; + desc.Width = swapChainDesc.width; + desc.Height = swapChainDesc.height; + desc.Format = format; + desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; + desc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD; + desc.SampleDesc.Count = 1; + desc.Flags = m_IsTearingAllowed ? DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING : 0; + desc.Scaling = DXGI_SCALING_NONE; + desc.AlphaMode = DXGI_ALPHA_MODE_IGNORE; ComPtr swapChain; - hr = factory->CreateSwapChainForHwnd((ID3D12CommandQueue*)commandQueueD3D12, window, &swapChainDesc1, nullptr, nullptr, &swapChain); - RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "IDXGIFactory2::CreateSwapChainForHwnd() failed, error code: 0x%X.", hr); + hr = dxgifactory4->CreateSwapChainForHwnd((ID3D12CommandQueue*)commandQueue, hwnd, &desc, nullptr, nullptr, &swapChain); + if (FAILED(hr)) + { + // are we on Win7? + desc.Scaling = DXGI_SCALING_STRETCH; + hr = dxgifactory4->CreateSwapChainForHwnd((ID3D12CommandQueue*)commandQueue, hwnd, &desc, nullptr, nullptr, &swapChain); + } + RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "IDXGIFactory2::CreateSwapChainForHwnd() - FAILED!"); - hr = factory->MakeWindowAssociation(window, DXGI_MWA_NO_ALT_ENTER); - RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "CreateSwapChainForHwnd::MakeWindowAssociation() failed, error code: 0x%X.", hr); + hr = dxgifactory4->MakeWindowAssociation(hwnd, DXGI_MWA_NO_ALT_ENTER); + RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "IDXGIFactory::MakeWindowAssociation() - FAILED!"); - hr = swapChain->QueryInterface(IID_PPV_ARGS(&m_SwapChain)); - RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "IDXGISwapChain1::QueryInterface() failed, error code: 0x%X.", hr); + hr = swapChain->QueryInterface(__uuidof(IDXGISwapChain4), (void**)&m_SwapChain.ptr); + m_SwapChain.version = 4; + if (FAILED(hr)) + { + REPORT_WARNING(m_Device.GetLog(), "QueryInterface(IDXGISwapChain4) - FAILED!"); + hr = device->QueryInterface(__uuidof(IDXGISwapChain3), (void**)&m_SwapChain.ptr); + m_SwapChain.version = 3; + if (FAILED(hr)) + { + REPORT_WARNING(m_Device.GetLog(), "QueryInterface(IDXGISwapChain3) - FAILED!"); + hr = device->QueryInterface(__uuidof(IDXGISwapChain2), (void**)&m_SwapChain.ptr); + m_SwapChain.version = 2; + if (FAILED(hr)) + { + REPORT_WARNING(m_Device.GetLog(), "QueryInterface(IDXGISwapChain2) - FAILED!"); + hr = device->QueryInterface(__uuidof(IDXGISwapChain1), (void**)&m_SwapChain.ptr); + m_SwapChain.version = 1; + if (FAILED(hr)) + { + REPORT_WARNING(m_Device.GetLog(), "QueryInterface(IDXGISwapChain1) - FAILED!"); + m_SwapChain.ptr = (IDXGISwapChain4*)swapChain.GetInterface(); + m_SwapChain.version = 0; + } + } + } + } - UINT colorSpaceSupport = 0; - hr = m_SwapChain->CheckColorSpaceSupport(colorSpace, &colorSpaceSupport); + if (m_SwapChain.version >= 3) + { + UINT colorSpaceSupport = 0; + hr = m_SwapChain->CheckColorSpaceSupport(colorSpace, &colorSpaceSupport); - if (!(colorSpaceSupport & DXGI_SWAP_CHAIN_COLOR_SPACE_SUPPORT_FLAG_PRESENT)) - hr = E_FAIL; + if ( !(colorSpaceSupport & DXGI_SWAP_CHAIN_COLOR_SPACE_SUPPORT_FLAG_PRESENT) ) + hr = E_FAIL; - if (SUCCEEDED(hr)) - hr = m_SwapChain->SetColorSpace1(colorSpace); + if (SUCCEEDED(hr)) + hr = m_SwapChain->SetColorSpace1(colorSpace); - if (FAILED(hr)) - REPORT_ERROR(m_Device.GetLog(), "IDXGISwapChain3::SetColorSpace1() failed, error code: 0x%X.", hr); + if (FAILED(hr)) + REPORT_WARNING(m_Device.GetLog(), "IDXGISwapChain3::SetColorSpace1() - FAILED!"); + } + else + REPORT_ERROR(m_Device.GetLog(), "IDXGISwapChain3::SetColorSpace1() is not supported by the OS!"); + + if (m_SwapChain.version >= 1) + { + DXGI_RGBA color = {}; + hr = m_SwapChain->SetBackgroundColor(&color); + if (FAILED(hr)) + REPORT_WARNING(m_Device.GetLog(), "IDXGISwapChain1::SetBackgroundColor() - FAILED!"); + } if (swapChainDesc.display != nullptr) { @@ -141,66 +174,59 @@ Result SwapChainD3D12::Create(const SwapChainDesc& swapChainDesc) return Result::UNSUPPORTED; } - hr = m_SwapChain->SetFullscreenState(TRUE, nullptr); - RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "IDXGISwapChain1::SetFullscreenState() failed, error code: 0x%X.", hr); + hr = m_SwapChain->SetFullscreenState(TRUE, output); + RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "IDXGISwapChain::SetFullscreenState() failed, error code: 0x%X.", hr); - hr = m_SwapChain->ResizeBuffers(swapChainDesc1.BufferCount, swapChainDesc1.Width, swapChainDesc1.Height, swapChainDesc1.Format, swapChainDesc1.Flags); - RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "IDXGISwapChain1::ResizeBuffers() failed, error code: 0x%X.", hr); + hr = m_SwapChain->ResizeBuffers(desc.BufferCount, desc.Width, desc.Height, desc.Format, desc.Flags); + RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "IDXGISwapChain::ResizeBuffers() failed, error code: 0x%X.", hr); m_IsTearingAllowed = false; m_IsFullscreenEnabled = true; } + m_Format = g_swapChainTextureFormat[(uint32_t)swapChainDesc.format]; m_SwapChainDesc = swapChainDesc; - m_Format = g_SwapChainTextureFormat[(uint32_t)swapChainDesc.format]; - for (uint32_t i = 0; i < swapChainDesc.textureNum; i++) + m_Textures.reserve(m_SwapChainDesc.textureNum); + for (uint32_t i = 0; i < m_SwapChainDesc.textureNum; i++) { - ComPtr resource; - hr = m_SwapChain->GetBuffer(i, IID_PPV_ARGS(&resource)); - if (FAILED(hr)) - { - REPORT_ERROR(m_Device.GetLog(), "IDXGISwapChain4::GetBuffer() failed, error code: 0x%X.", hr); - return Result::FAILURE; - } + ComPtr textureNative; + hr = m_SwapChain->GetBuffer(i, IID_PPV_ARGS(&textureNative)); + RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "IDXGISwapChain::GetBuffer() - FAILED!"); - m_Textures.emplace_back(m_Device); - m_Textures[i].Initialize(resource); - } + TextureD3D12Desc textureDesc = {}; + textureDesc.d3d12Resource = textureNative; - m_TexturePointer.resize(swapChainDesc.textureNum); - for (uint32_t i = 0; i < swapChainDesc.textureNum; i++) - m_TexturePointer[i] = (Texture*)&m_Textures[i]; + TextureD3D12* texture = Allocate(m_Device.GetStdAllocator(), m_Device); + const nri::Result res = texture->Create(textureDesc); + if (res != nri::Result::SUCCESS) + return res; - m_CommandQueue = (ID3D12CommandQueue*)commandQueueD3D12; + m_Textures.push_back(texture); + } return Result::SUCCESS; } -inline void SwapChainD3D12::SetDebugName(const char* name) -{ - SET_D3D_DEBUG_OBJECT_NAME(m_SwapChain, name); -} +//================================================================================================================ +// NRI +//================================================================================================================ inline Texture* const* SwapChainD3D12::GetTextures(uint32_t& textureNum, Format& format) const { - textureNum = (uint32_t)m_TexturePointer.size(); + textureNum = m_SwapChainDesc.textureNum; format = m_Format; - return &m_TexturePointer[0]; + return (Texture**)m_Textures.data(); } -inline uint32_t SwapChainD3D12::AcquireNextTexture(QueueSemaphore& textureReadyForRender) +inline uint32_t SwapChainD3D12::AcquireNextTexture() { - ((QueueSemaphoreD3D12&)textureReadyForRender).Signal(m_CommandQueue); - return m_SwapChain->GetCurrentBackBufferIndex(); } -inline Result SwapChainD3D12::Present(QueueSemaphore& textureReadyForPresent) +inline Result SwapChainD3D12::Present() { - ((QueueSemaphoreD3D12&)textureReadyForPresent).Wait(m_CommandQueue); - BOOL fullscreen = FALSE; m_SwapChain->GetFullscreenState(&fullscreen, nullptr); if (fullscreen != BOOL(m_IsFullscreenEnabled)) @@ -216,6 +242,9 @@ inline Result SwapChainD3D12::Present(QueueSemaphore& textureReadyForPresent) inline Result SwapChainD3D12::SetHdrMetadata(const HdrMetadata& hdrMetadata) { + if (m_SwapChain.version < 4) + return Result::UNSUPPORTED; + DXGI_HDR_METADATA_HDR10 data = {}; data.RedPrimary[0] = uint16_t(hdrMetadata.displayPrimaryRed[0] * 50000.0f); data.RedPrimary[1] = uint16_t(hdrMetadata.displayPrimaryRed[1] * 50000.0f); @@ -231,11 +260,7 @@ inline Result SwapChainD3D12::SetHdrMetadata(const HdrMetadata& hdrMetadata) data.MaxFrameAverageLightLevel = uint16_t(hdrMetadata.frameAverageLightLevelMax); HRESULT hr = m_SwapChain->SetHDRMetaData(DXGI_HDR_METADATA_TYPE_HDR10, sizeof(DXGI_HDR_METADATA_HDR10), &data); - if (FAILED(hr)) - { - REPORT_ERROR(m_Device.GetLog(), "IDXGISwapChain4::SetHDRMetaData() failed, error code: 0x%X.", hr); - return Result::FAILURE; - } + RETURN_ON_BAD_HRESULT(m_Device.GetLog(), hr, "IDXGISwapChain4::SetHDRMetaData() - FAILED"); return Result::SUCCESS; } diff --git a/Source/D3D12/SwapChainD3D12.h b/Source/D3D12/SwapChainD3D12.h index c0fce5a3..7e2b2643 100644 --- a/Source/D3D12/SwapChainD3D12.h +++ b/Source/D3D12/SwapChainD3D12.h @@ -10,49 +10,60 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #pragma once -struct IDXGIFactory2; -struct ID3D12CommandQueue; struct IDXGISwapChain4; namespace nri { - struct DeviceD3D12; - struct TextureD3D12; - - struct SwapChainD3D12 - { - SwapChainD3D12(DeviceD3D12& device); - ~SwapChainD3D12(); - - DeviceD3D12& GetDevice() const; - - Result Create(const SwapChainDesc& swapChainDesc); - - //================================================================================================================ - // NRI - //================================================================================================================ - void SetDebugName(const char* name); - - Texture* const* GetTextures(uint32_t& textureNum, Format& format) const; - uint32_t AcquireNextTexture(QueueSemaphore& textureReadyForRender); - Result Present(QueueSemaphore& textureReadyForPresent); - Result SetHdrMetadata(const HdrMetadata& hdrMetadata); - - private: - DeviceD3D12& m_Device; - ComPtr m_SwapChain; - ComPtr m_CommandQueue; - Vector m_Textures; - Vector m_TexturePointer; - Format m_Format = Format::UNKNOWN; - bool m_IsTearingAllowed = false; - bool m_IsFullscreenEnabled = false; - - SwapChainDesc m_SwapChainDesc = {}; - }; - - inline DeviceD3D12& SwapChainD3D12::GetDevice() const - { - return m_Device; - } + +struct DeviceD3D12; +struct TextureD3D12; + +struct VersionedSwapChain +{ + inline ~VersionedSwapChain() + {} + + inline IDXGISwapChain4* operator->() const + { return ptr; } + + ComPtr ptr; + uint8_t version = 0; +}; + +struct SwapChainD3D12 +{ + inline SwapChainD3D12(DeviceD3D12& device) : + m_Device(device) + , m_Textures(device.GetStdAllocator()) + {} + + inline DeviceD3D12& GetDevice() const + { return m_Device; } + + ~SwapChainD3D12(); + + Result Create(const SwapChainDesc& swapChainDesc); + + //================================================================================================================ + // NRI + //================================================================================================================ + + inline void SetDebugName(const char* name) + { SET_D3D_DEBUG_OBJECT_NAME(m_SwapChain.ptr, name); } + + Texture* const* GetTextures(uint32_t& textureNum, Format& format) const; + uint32_t AcquireNextTexture(); + Result Present(); + Result SetHdrMetadata(const HdrMetadata& hdrMetadata); + +private: + DeviceD3D12& m_Device; + VersionedSwapChain m_SwapChain; + Vector m_Textures; + SwapChainDesc m_SwapChainDesc = {}; + Format m_Format = Format::UNKNOWN; + bool m_IsTearingAllowed = false; + bool m_IsFullscreenEnabled = false; +}; + } diff --git a/Source/D3D12/SwapChainD3D12.hpp b/Source/D3D12/SwapChainD3D12.hpp index faf429d8..a9b843eb 100644 --- a/Source/D3D12/SwapChainD3D12.hpp +++ b/Source/D3D12/SwapChainD3D12.hpp @@ -8,7 +8,7 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ SwapChainInterface ] +#pragma region [ SwapChain ] static void NRI_CALL SetSwapChainDebugName(SwapChain& swapChain, const char* name) { @@ -20,14 +20,14 @@ static Texture* const* NRI_CALL GetSwapChainTextures(const SwapChain& swapChain, return ((SwapChainD3D12&)swapChain).GetTextures(textureNum, format); } -static uint32_t NRI_CALL AcquireNextSwapChainTexture(SwapChain& swapChain, QueueSemaphore& textureReadyForRender) +static uint32_t NRI_CALL AcquireNextSwapChainTexture(SwapChain& swapChain) { - return ((SwapChainD3D12&)swapChain).AcquireNextTexture(textureReadyForRender); + return ((SwapChainD3D12&)swapChain).AcquireNextTexture(); } -static Result NRI_CALL SwapChainPresent(SwapChain& swapChain, QueueSemaphore& textureReadyForPresent) +static Result NRI_CALL SwapChainPresent(SwapChain& swapChain) { - return ((SwapChainD3D12&)swapChain).Present(textureReadyForPresent); + return ((SwapChainD3D12&)swapChain).Present(); } static Result NRI_CALL SetSwapChainHdrMetadata(SwapChain& swapChain, const HdrMetadata& hdrMetadata) @@ -35,13 +35,6 @@ static Result NRI_CALL SetSwapChainHdrMetadata(SwapChain& swapChain, const HdrMe return ((SwapChainD3D12&)swapChain).SetHdrMetadata(hdrMetadata); } -void FillFunctionTableSwapChainD3D12(SwapChainInterface& swapChainInterface) -{ - swapChainInterface.SetSwapChainDebugName = ::SetSwapChainDebugName; - swapChainInterface.GetSwapChainTextures = ::GetSwapChainTextures; - swapChainInterface.AcquireNextSwapChainTexture = ::AcquireNextSwapChainTexture; - swapChainInterface.SwapChainPresent = ::SwapChainPresent; - swapChainInterface.SetSwapChainHdrMetadata = ::SetSwapChainHdrMetadata; -} +#pragma endregion -#pragma endregion \ No newline at end of file +Define_SwapChain_PartiallyFillFunctionTable(D3D12) diff --git a/Source/D3D12/TextureD3D12.cpp b/Source/D3D12/TextureD3D12.cpp index 32038aab..515397d0 100644 --- a/Source/D3D12/TextureD3D12.cpp +++ b/Source/D3D12/TextureD3D12.cpp @@ -10,7 +10,6 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include "SharedD3D12.h" #include "TextureD3D12.h" -#include "DeviceD3D12.h" #include "MemoryD3D12.h" using namespace nri; @@ -97,16 +96,28 @@ Result TextureD3D12::BindMemory(const MemoryD3D12* memory, uint64_t offset) return Result::SUCCESS; } -uint32_t TextureD3D12::GetSubresourceIndex(uint32_t arrayOffset, uint32_t mipOffset) const +uint16_t TextureD3D12::GetSize(uint32_t dimension, uint32_t mipOffset) const { - return arrayOffset * m_TextureDesc.MipLevels + mipOffset; -} + assert(dimension < 3); -inline void TextureD3D12::SetDebugName(const char* name) -{ - SET_D3D_DEBUG_OBJECT_NAME(m_Texture, name); + uint16_t size; + if (dimension == 0) + size = (uint16_t)m_TextureDesc.Width; + else if (dimension == 1) + size = (uint16_t)m_TextureDesc.Height; + else + size = (uint16_t)m_TextureDesc.DepthOrArraySize; + + size = (uint16_t)std::max(size >> mipOffset, 1); + size = Align(size, dimension < 2 ? (uint16_t)GetTexelBlockWidth(m_Format) : 1); + + return size; } +//================================================================================================================ +// NRI +//================================================================================================================ + inline void TextureD3D12::GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const { m_Device.GetMemoryInfo(memoryLocation, m_TextureDesc, memoryDesc); diff --git a/Source/D3D12/TextureD3D12.h b/Source/D3D12/TextureD3D12.h index 70311c84..f69d1bb7 100644 --- a/Source/D3D12/TextureD3D12.h +++ b/Source/D3D12/TextureD3D12.h @@ -16,76 +16,51 @@ struct ID3D12Resource; namespace nri { - struct DeviceD3D12; - struct MemoryD3D12; - - struct TextureD3D12 - { - TextureD3D12(DeviceD3D12& device); - ~TextureD3D12(); - - operator ID3D12Resource*() const; - - DeviceD3D12& GetDevice() const; - - Result Create(const TextureDesc& textureDesc); - Result Create(const TextureD3D12Desc& textureDesc); - void Initialize(ID3D12Resource* resource); - Result BindMemory(const MemoryD3D12* memory, uint64_t offset); - const D3D12_RESOURCE_DESC& GetTextureDesc() const; - uint32_t GetSubresourceIndex(uint32_t arrayOffset, uint32_t mipOffset) const; - uint16_t GetSize(uint32_t dim, uint32_t mipOffset = 0) const; - - //================================================================================================================ - // NRI - //================================================================================================================ - void SetDebugName(const char* name); - void GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const; - - private: - DeviceD3D12& m_Device; - D3D12_RESOURCE_DESC m_TextureDesc = {}; - ComPtr m_Texture; - Format m_Format = Format::UNKNOWN; - }; - - inline TextureD3D12::TextureD3D12(DeviceD3D12& device) + +struct DeviceD3D12; +struct MemoryD3D12; + +struct TextureD3D12 +{ + inline TextureD3D12(DeviceD3D12& device) : m_Device(device) {} - inline TextureD3D12::~TextureD3D12() + inline ~TextureD3D12() {} - inline TextureD3D12::operator ID3D12Resource*() const - { - return m_Texture.GetInterface(); - } - - inline const D3D12_RESOURCE_DESC& TextureD3D12::GetTextureDesc() const - { - return m_TextureDesc; - } - - inline DeviceD3D12& TextureD3D12::GetDevice() const - { - return m_Device; - } - - inline uint16_t TextureD3D12::GetSize(uint32_t dimension, uint32_t mipOffset) const - { - assert(dimension < 3); - - uint16_t size; - if (dimension == 0) - size = (uint16_t)m_TextureDesc.Width; - else if (dimension == 1) - size = (uint16_t)m_TextureDesc.Height; - else - size = (uint16_t)m_TextureDesc.DepthOrArraySize; - - size = (uint16_t)std::max(size >> mipOffset, 1); - size = Align(size, dimension < 2 ? (uint16_t)GetTexelBlockWidth(m_Format) : 1); - - return size; - } + inline operator ID3D12Resource*() const + { return m_Texture.GetInterface(); } + + inline const D3D12_RESOURCE_DESC& GetTextureDesc() const + { return m_TextureDesc; } + + inline DeviceD3D12& GetDevice() const + { return m_Device; } + + inline uint32_t GetSubresourceIndex(uint32_t arrayOffset, uint32_t mipOffset) const + { return arrayOffset * m_TextureDesc.MipLevels + mipOffset; } + + Result Create(const TextureDesc& textureDesc); + Result Create(const TextureD3D12Desc& textureDesc); + void Initialize(ID3D12Resource* resource); + Result BindMemory(const MemoryD3D12* memory, uint64_t offset); + uint16_t GetSize(uint32_t dim, uint32_t mipOffset = 0) const; + + //================================================================================================================ + // NRI + //================================================================================================================ + + inline void SetDebugName(const char* name) + { SET_D3D_DEBUG_OBJECT_NAME(m_Texture, name); } + + void GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const; + +private: + DeviceD3D12& m_Device; + D3D12_RESOURCE_DESC m_TextureDesc = {}; + ComPtr m_Texture; + Format m_Format = Format::UNKNOWN; +}; + } diff --git a/Source/D3D12/TextureD3D12.hpp b/Source/D3D12/TextureD3D12.hpp index 33af94e2..1477a5f4 100644 --- a/Source/D3D12/TextureD3D12.hpp +++ b/Source/D3D12/TextureD3D12.hpp @@ -8,7 +8,7 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ CoreInterface ] +#pragma region [ Core ] static void NRI_CALL SetTextureDebugName(Texture& texture, const char* name) { @@ -27,11 +27,6 @@ static void NRI_CALL GetTextureMemoryInfo(const Texture& texture, MemoryLocation ((TextureD3D12&)texture).GetMemoryInfo(memoryLocation, memoryDesc); } -void FillFunctionTableTextureD3D12(CoreInterface& coreInterface) -{ - coreInterface.SetTextureDebugName = ::SetTextureDebugName; - coreInterface.GetTextureNativeObject = ::GetTextureNativeObject; - coreInterface.GetTextureMemoryInfo = ::GetTextureMemoryInfo; -} - #pragma endregion + +Define_Core_Texture_PartiallyFillFunctionTable(D3D12) diff --git a/Source/Shared/DeviceBase.h b/Source/Shared/DeviceBase.h index 6845b13e..00d3ca37 100644 --- a/Source/Shared/DeviceBase.h +++ b/Source/Shared/DeviceBase.h @@ -14,7 +14,16 @@ namespace nri { struct DeviceBase { - DeviceBase(const Log& log, const StdAllocator& stdAllocator); + inline DeviceBase(const Log& log, const StdAllocator& stdAllocator) : + m_Log(log), + m_StdAllocator(stdAllocator) + {} + + inline const Log& GetLog() const + { return m_Log; } + + inline StdAllocator& GetStdAllocator() + { return m_StdAllocator; } virtual ~DeviceBase() {} virtual void Destroy() = 0; @@ -27,27 +36,191 @@ namespace nri virtual Result FillFunctionTable(MeshShaderInterface& table) const { table = {}; return Result::UNSUPPORTED; } virtual Result FillFunctionTable(HelperInterface& table) const { table = {}; return Result::UNSUPPORTED; } - const Log& GetLog() const; - StdAllocator& GetStdAllocator(); - protected: Log m_Log; StdAllocator m_StdAllocator; }; +} - inline DeviceBase::DeviceBase(const Log& log, const StdAllocator& stdAllocator) : - m_Log(log), - m_StdAllocator(stdAllocator) - { - } +#define Declare_PartiallyFillFunctionTable_Functions(API) \ + void Core_Buffer_PartiallyFillFunctionTable##API(CoreInterface& table); \ + void Core_CommandAllocator_PartiallyFillFunctionTable##API(CoreInterface& table); \ + void Core_CommandBuffer_PartiallyFillFunctionTable##API(CoreInterface& table); \ + void Core_CommandQueue_PartiallyFillFunctionTable##API(CoreInterface& table); \ + void Core_Descriptor_PartiallyFillFunctionTable##API(CoreInterface& table); \ + void Core_DescriptorPool_PartiallyFillFunctionTable##API(CoreInterface& table); \ + void Core_DescriptorSet_PartiallyFillFunctionTable##API(CoreInterface& table); \ + void Core_Fence_PartiallyFillFunctionTable##API(CoreInterface& table); \ + void Core_QueryPool_PartiallyFillFunctionTable##API(CoreInterface& table); \ + void Core_Texture_PartiallyFillFunctionTable##API(CoreInterface& table); \ + void SwapChain_PartiallyFillFunctionTable##API(SwapChainInterface& table); \ + void RayTracing_CommandBuffer_PartiallyFillFunctionTable##API(RayTracingInterface& table); \ + void RayTracing_AccelerationStructure_PartiallyFillFunctionTable##API(RayTracingInterface& table); \ + void MeshShader_CommandBuffer_PartiallyFillFunctionTable##API(MeshShaderInterface& table); \ + void Helper_CommandQueue_PartiallyFillFunctionTable##API(HelperInterface& table); - inline const Log& DeviceBase::GetLog() const - { - return m_Log; - } +#define Define_Core_Buffer_PartiallyFillFunctionTable(API) \ +void Core_Buffer_PartiallyFillFunctionTable##API(CoreInterface& table) \ +{ \ + table.SetBufferDebugName = ::SetBufferDebugName; \ + table.GetBufferNativeObject = ::GetBufferNativeObject; \ + table.GetBufferMemoryInfo = ::GetBufferMemoryInfo; \ + table.MapBuffer = ::MapBuffer; \ + table.UnmapBuffer = ::UnmapBuffer; \ +} - inline StdAllocator& DeviceBase::GetStdAllocator() - { - return m_StdAllocator; - } -} \ No newline at end of file +#define Define_Core_CommandAllocator_PartiallyFillFunctionTable(API) \ +void Core_CommandAllocator_PartiallyFillFunctionTable##API(CoreInterface& table) \ +{ \ + table.SetCommandAllocatorDebugName = ::SetCommandAllocatorDebugName; \ + table.CreateCommandBuffer = ::CreateCommandBuffer; \ + table.ResetCommandAllocator = ::ResetCommandAllocator; \ +} + +#define Define_Core_CommandBuffer_PartiallyFillFunctionTable(API) \ +void Core_CommandBuffer_PartiallyFillFunctionTable##API(CoreInterface& table) \ +{ \ + table.DestroyCommandBuffer = ::DestroyCommandBuffer; \ + table.BeginCommandBuffer = ::BeginCommandBuffer; \ + table.EndCommandBuffer = ::EndCommandBuffer; \ + table.CmdSetPipelineLayout = ::CmdSetPipelineLayout; \ + table.CmdSetPipeline = ::CmdSetPipeline; \ + table.CmdPipelineBarrier = ::CmdPipelineBarrier; \ + table.CmdSetDescriptorPool = ::CmdSetDescriptorPool; \ + table.CmdSetDescriptorSet = ::CmdSetDescriptorSet; \ + table.CmdSetConstants = ::CmdSetConstants; \ + table.CmdBeginRenderPass = ::CmdBeginRenderPass; \ + table.CmdEndRenderPass = ::CmdEndRenderPass; \ + table.CmdSetViewports = ::CmdSetViewports; \ + table.CmdSetScissors = ::CmdSetScissors; \ + table.CmdSetDepthBounds = ::CmdSetDepthBounds; \ + table.CmdSetStencilReference = ::CmdSetStencilReference; \ + table.CmdSetSamplePositions = ::CmdSetSamplePositions; \ + table.CmdClearAttachments = ::CmdClearAttachments; \ + table.CmdSetIndexBuffer = ::CmdSetIndexBuffer; \ + table.CmdSetVertexBuffers = ::CmdSetVertexBuffers; \ + table.CmdDraw = ::CmdDraw; \ + table.CmdDrawIndexed = ::CmdDrawIndexed; \ + table.CmdDrawIndirect = ::CmdDrawIndirect; \ + table.CmdDrawIndexedIndirect = ::CmdDrawIndexedIndirect; \ + table.CmdDispatch = ::CmdDispatch; \ + table.CmdDispatchIndirect = ::CmdDispatchIndirect; \ + table.CmdBeginQuery = ::CmdBeginQuery; \ + table.CmdEndQuery = ::CmdEndQuery; \ + table.CmdBeginAnnotation = ::CmdBeginAnnotation; \ + table.CmdEndAnnotation = ::CmdEndAnnotation; \ + table.CmdClearStorageBuffer = ::CmdClearStorageBuffer; \ + table.CmdClearStorageTexture = ::CmdClearStorageTexture; \ + table.CmdCopyBuffer = ::CmdCopyBuffer; \ + table.CmdCopyTexture = ::CmdCopyTexture; \ + table.CmdUploadBufferToTexture = ::CmdUploadBufferToTexture; \ + table.CmdReadbackTextureToBuffer = ::CmdReadbackTextureToBuffer; \ + table.CmdCopyQueries = ::CmdCopyQueries; \ + table.CmdResetQueries = ::CmdResetQueries; \ + table.SetCommandBufferDebugName = ::SetCommandBufferDebugName; \ + table.GetCommandBufferNativeObject = ::GetCommandBufferNativeObject; \ +} + +#define Define_Core_CommandQueue_PartiallyFillFunctionTable(API) \ +void Core_CommandQueue_PartiallyFillFunctionTable##API(CoreInterface& table) \ +{ \ + table.SetCommandQueueDebugName = ::SetCommandQueueDebugName; \ + table.QueueSubmit = ::QueueSubmit; \ +} + +#define Define_Core_Descriptor_PartiallyFillFunctionTable(API) \ +void Core_Descriptor_PartiallyFillFunctionTable##API(CoreInterface& table) \ +{ \ + table.SetDescriptorDebugName = ::SetDescriptorDebugName; \ + table.GetDescriptorNativeObject = ::GetDescriptorNativeObject; \ +} + +#define Define_Core_DescriptorPool_PartiallyFillFunctionTable(API) \ +void Core_DescriptorPool_PartiallyFillFunctionTable##API(CoreInterface& table) \ +{ \ + table.SetDescriptorPoolDebugName = ::SetDescriptorPoolDebugName; \ + table.AllocateDescriptorSets = ::AllocateDescriptorSets; \ + table.ResetDescriptorPool = ::ResetDescriptorPool; \ +} + +#define Define_Core_DescriptorSet_PartiallyFillFunctionTable(API) \ +void Core_DescriptorSet_PartiallyFillFunctionTable##API(CoreInterface& table) \ +{ \ + table.SetDescriptorSetDebugName = ::SetDescriptorSetDebugName; \ + table.UpdateDescriptorRanges = ::UpdateDescriptorRanges; \ + table.UpdateDynamicConstantBuffers = ::UpdateDynamicConstantBuffers; \ + table.CopyDescriptorSet = ::CopyDescriptorSet; \ +} + +#define Define_Core_Fence_PartiallyFillFunctionTable(API) \ +void Core_Fence_PartiallyFillFunctionTable##API(CoreInterface& table) \ +{ \ + table.GetFenceValue = ::GetFenceValue; \ + table.QueueSignal = ::QueueSignal; \ + table.QueueWait = ::QueueWait; \ + table.Wait = ::Wait; \ + table.SetFenceDebugName = ::SetFenceDebugName; \ +} + +#define Define_Core_QueryPool_PartiallyFillFunctionTable(API) \ +void Core_QueryPool_PartiallyFillFunctionTable##API(CoreInterface& table) \ +{ \ + table.SetQueryPoolDebugName = ::SetQueryPoolDebugName; \ + table.GetQuerySize = ::GetQuerySize; \ +} + +#define Define_Core_Texture_PartiallyFillFunctionTable(API) \ +void Core_Texture_PartiallyFillFunctionTable##API(CoreInterface& table) \ +{ \ + table.SetTextureDebugName = ::SetTextureDebugName; \ + table.GetTextureNativeObject = ::GetTextureNativeObject; \ + table.GetTextureMemoryInfo = ::GetTextureMemoryInfo; \ +} + +#define Define_SwapChain_PartiallyFillFunctionTable(API) \ +void SwapChain_PartiallyFillFunctionTable##API(SwapChainInterface& table) \ +{ \ + table.SetSwapChainDebugName = ::SetSwapChainDebugName; \ + table.GetSwapChainTextures = ::GetSwapChainTextures; \ + table.AcquireNextSwapChainTexture = ::AcquireNextSwapChainTexture; \ + table.SwapChainPresent = ::SwapChainPresent; \ + table.SetSwapChainHdrMetadata = ::SetSwapChainHdrMetadata; \ +} + +#define Define_RayTracing_CommandBuffer_PartiallyFillFunctionTable(API) \ +void RayTracing_CommandBuffer_PartiallyFillFunctionTable##API(RayTracingInterface& table) \ +{ \ + table.CmdBuildTopLevelAccelerationStructure = ::CmdBuildTopLevelAccelerationStructure; \ + table.CmdBuildBottomLevelAccelerationStructure = ::CmdBuildBottomLevelAccelerationStructure; \ + table.CmdUpdateTopLevelAccelerationStructure = ::CmdUpdateTopLevelAccelerationStructure; \ + table.CmdUpdateBottomLevelAccelerationStructure = ::CmdUpdateBottomLevelAccelerationStructure; \ + table.CmdCopyAccelerationStructure = ::CmdCopyAccelerationStructure; \ + table.CmdWriteAccelerationStructureSize = ::CmdWriteAccelerationStructureSize; \ + table.CmdDispatchRays = ::CmdDispatchRays; \ +} + +#define Define_RayTracing_AccelerationStructure_PartiallyFillFunctionTable(API) \ +void RayTracing_AccelerationStructure_PartiallyFillFunctionTable##API(RayTracingInterface& table) \ +{ \ + table.CreateAccelerationStructureDescriptor = ::CreateAccelerationStructureDescriptor; \ + table.GetAccelerationStructureMemoryInfo = ::GetAccelerationStructureMemoryInfo; \ + table.GetAccelerationStructureUpdateScratchBufferSize = ::GetAccelerationStructureUpdateScratchBufferSize; \ + table.GetAccelerationStructureBuildScratchBufferSize = ::GetAccelerationStructureBuildScratchBufferSize; \ + table.GetAccelerationStructureHandle = ::GetAccelerationStructureHandle; \ + table.SetAccelerationStructureDebugName = ::SetAccelerationStructureDebugName; \ + table.GetAccelerationStructureNativeObject = ::GetAccelerationStructureNativeObject; \ +} + +#define Define_MeshShader_CommandBuffer_PartiallyFillFunctionTable(API) \ +void MeshShader_CommandBuffer_PartiallyFillFunctionTable##API(MeshShaderInterface& table) \ +{ \ + table.CmdDispatchMeshTasks = ::CmdDispatchMeshTasks; \ +} + +#define Define_Helper_CommandQueue_PartiallyFillFunctionTable(API) \ +void Helper_CommandQueue_PartiallyFillFunctionTable##API(HelperInterface& table) \ +{ \ + table.ChangeResourceStates = ::ChangeResourceStates; \ + table.UploadData = ::UploadData; \ + table.WaitForIdle = ::WaitForIdle; \ +} diff --git a/Source/Shared/HelperDataUpload.cpp b/Source/Shared/HelperDataUpload.cpp index 7b7e930b..9c027d58 100644 --- a/Source/Shared/HelperDataUpload.cpp +++ b/Source/Shared/HelperDataUpload.cpp @@ -71,7 +71,7 @@ Result HelperDataUpload::Create() if (result != Result::SUCCESS) return result; - result = NRI.CreateDeviceSemaphore(m_Device, false, m_DeviceSemaphore); + result = NRI.CreateFence(m_Device, 0, m_Fence); if (result != Result::SUCCESS) return result; @@ -99,8 +99,8 @@ void HelperDataUpload::Destroy() NRI.DestroyCommandBuffer(*m_CommandBuffers[i]); NRI.DestroyCommandAllocator(*m_CommandAllocators[i]); } - NRI.DestroyDeviceSemaphore(*m_DeviceSemaphore); + NRI.DestroyFence(*m_Fence); NRI.DestroyBuffer(*m_UploadBuffer); NRI.FreeMemory(*m_UploadBufferMemory); } @@ -188,15 +188,20 @@ Result HelperDataUpload::EndCommandBuffersAndSubmit() return result; } - WorkSubmissionDesc workSubmissionDesc = {}; - workSubmissionDesc.commandBufferNum = 1; + QueueSubmitDesc queueSubmitDesc = {}; + queueSubmitDesc.commandBufferNum = 1; for (uint32_t i = 0; i < m_CommandBuffers.size(); i++) { - workSubmissionDesc.commandBuffers = &m_CommandBuffers[i]; - workSubmissionDesc.physicalDeviceIndex = i; - NRI.SubmitQueueWork(m_CommandQueue, workSubmissionDesc, m_DeviceSemaphore); - NRI.WaitForSemaphore(m_CommandQueue, *m_DeviceSemaphore); + queueSubmitDesc.commandBuffers = &m_CommandBuffers[i]; + queueSubmitDesc.physicalDeviceIndex = i; + + NRI.QueueSubmit(m_CommandQueue, queueSubmitDesc); + + NRI.QueueSignal(m_CommandQueue, *m_Fence, m_FenceValue); + NRI.Wait(*m_Fence, m_FenceValue); + + m_FenceValue++; } for (uint32_t i = 0; i < m_CommandAllocators.size(); i++) diff --git a/Source/Shared/HelperDataUpload.h b/Source/Shared/HelperDataUpload.h index 5e576a44..954203ea 100644 --- a/Source/Shared/HelperDataUpload.h +++ b/Source/Shared/HelperDataUpload.h @@ -25,7 +25,7 @@ struct HelperDataUpload const nri::DeviceDesc& m_DeviceDesc; nri::Device& m_Device; nri::CommandQueue& m_CommandQueue; - nri::DeviceSemaphore* m_DeviceSemaphore = nullptr; + nri::Fence* m_Fence = nullptr; Vector m_CommandAllocators; Vector m_CommandBuffers; nri::Buffer* m_UploadBuffer = nullptr; @@ -33,6 +33,7 @@ struct HelperDataUpload uint8_t* m_MappedMemory = nullptr; uint64_t m_UploadBufferSize = 0; uint64_t m_UploadBufferOffset = 0; + uint64_t m_FenceValue = 1; static constexpr uint64_t COPY_ALIGMENT = 16; }; \ No newline at end of file diff --git a/Source/Shared/HelperResourceStateChange.cpp b/Source/Shared/HelperResourceStateChange.cpp index 90245fca..313fe560 100644 --- a/Source/Shared/HelperResourceStateChange.cpp +++ b/Source/Shared/HelperResourceStateChange.cpp @@ -14,11 +14,11 @@ HelperResourceStateChange::HelperResourceStateChange(const CoreInterface& NRI, D HelperResourceStateChange::~HelperResourceStateChange() { - if (m_CommandBuffer != nullptr) + if (m_CommandBuffer) NRI.DestroyCommandBuffer(*m_CommandBuffer); m_CommandBuffer = nullptr; - if (m_CommandAllocator != nullptr) + if (m_CommandAllocator) NRI.DestroyCommandAllocator(*m_CommandAllocator); m_CommandAllocator = nullptr; } @@ -33,15 +33,17 @@ Result HelperResourceStateChange::ChangeStates(const TransitionBarrierDesc& tran for (uint32_t i = 0; i < physicalDeviceNum; i++) { NRI.BeginCommandBuffer(*m_CommandBuffer, nullptr, i); - NRI.CmdPipelineBarrier(*m_CommandBuffer, &transitionBarriers, nullptr, BarrierDependency::ALL_STAGES); + { + NRI.CmdPipelineBarrier(*m_CommandBuffer, &transitionBarriers, nullptr, BarrierDependency::ALL_STAGES); + } NRI.EndCommandBuffer(*m_CommandBuffer); - WorkSubmissionDesc workSubmissionDesc = {}; - workSubmissionDesc.physicalDeviceIndex = i; - workSubmissionDesc.commandBufferNum = 1; - workSubmissionDesc.commandBuffers = &m_CommandBuffer; + QueueSubmitDesc queueSubmitDesc = {}; + queueSubmitDesc.physicalDeviceIndex = i; + queueSubmitDesc.commandBufferNum = 1; + queueSubmitDesc.commandBuffers = &m_CommandBuffer; - NRI.SubmitQueueWork(m_CommandQueue, workSubmissionDesc, nullptr); + NRI.QueueSubmit(m_CommandQueue, queueSubmitDesc); } return m_HelperWaitIdle.WaitIdle(); diff --git a/Source/Shared/HelperWaitIdle.cpp b/Source/Shared/HelperWaitIdle.cpp index 26b80e75..706128cb 100644 --- a/Source/Shared/HelperWaitIdle.cpp +++ b/Source/Shared/HelperWaitIdle.cpp @@ -7,29 +7,27 @@ HelperWaitIdle::HelperWaitIdle(const CoreInterface& NRI, Device& device, Command m_Device(device), m_CommandQueue(commandQueue) { - NRI.CreateDeviceSemaphore(device, false, m_DeviceSemaphore); + NRI.CreateFence(device, 0, m_Fence); } HelperWaitIdle::~HelperWaitIdle() { - if (m_DeviceSemaphore != nullptr) - NRI.DestroyDeviceSemaphore(*m_DeviceSemaphore); - m_DeviceSemaphore = nullptr; + if (m_Fence) + NRI.DestroyFence(*m_Fence); + m_Fence = nullptr; } Result HelperWaitIdle::WaitIdle() { - if (m_DeviceSemaphore == nullptr) + if (!m_Fence) return Result::FAILURE; const uint32_t physicalDeviceNum = NRI.GetDeviceDesc(m_Device).physicalDeviceNum; for (uint32_t i = 0; i < physicalDeviceNum; i++) { - WorkSubmissionDesc workSubmissionDesc = {}; - workSubmissionDesc.physicalDeviceIndex = i; - NRI.SubmitQueueWork(m_CommandQueue, workSubmissionDesc, m_DeviceSemaphore); - NRI.WaitForSemaphore(m_CommandQueue, *m_DeviceSemaphore); + NRI.QueueSignal(m_CommandQueue, *m_Fence, 1); + NRI.Wait(*m_Fence, 1); } return Result::SUCCESS; diff --git a/Source/Shared/HelperWaitIdle.h b/Source/Shared/HelperWaitIdle.h index b928e44b..20fc5356 100644 --- a/Source/Shared/HelperWaitIdle.h +++ b/Source/Shared/HelperWaitIdle.h @@ -11,5 +11,5 @@ struct HelperWaitIdle const nri::CoreInterface& NRI; nri::Device& m_Device; nri::CommandQueue& m_CommandQueue; - nri::DeviceSemaphore* m_DeviceSemaphore = nullptr; + nri::Fence* m_Fence = nullptr; }; \ No newline at end of file diff --git a/Source/Shared/SharedExternal.h b/Source/Shared/SharedExternal.h index 50bc61cd..e190c171 100644 --- a/Source/Shared/SharedExternal.h +++ b/Source/Shared/SharedExternal.h @@ -56,7 +56,11 @@ struct Log nri::CallbackInterface m_CallbackInterface; }; -//============================================================================================================================== +//================================================================================================================ + +#define SET_D3D_DEBUG_OBJECT_NAME(obj, name) \ + if (obj) \ + obj->SetPrivateData(WKPDID_D3DDebugObjectName, (UINT)std::strlen(name), name) #define RETURN_ON_BAD_HRESULT(log, hresult, format, ...) \ if ( FAILED(hresult) ) \ @@ -93,7 +97,7 @@ struct Log constexpr void ReturnVoid() {} template constexpr void MaybeUnused([[maybe_unused]] const Args&... args) {} -//============================================================================================================================== +//================================================================================================================ void ConvertCharToWchar(const char* in, wchar_t* out, size_t outLen); void ConvertWcharToChar(const wchar_t* in, char* out, size_t outLen); @@ -111,7 +115,7 @@ inline nri::Vendor GetVendorFromID(uint32_t vendorID) return nri::Vendor::UNKNOWN; } -//============================================================================================================================== +//================================================================================================================ // TODO: This code is Windows/D3D specific, so it's probably better to move it into a separate header #if _WIN32 @@ -352,7 +356,7 @@ nri::Result ValidateFunctionTable(const Log& log, const T& table) { if (*current == nullptr) { - REPORT_ERROR(log, "Invalid function table: function#%u is NULL!", uint32_t(current - begin)); + REPORT_ERROR(log, "Invalid function table: function #%u is NULL!", uint32_t(current - begin)); return nri::Result::FAILURE; } } diff --git a/Source/Shared/StdAllocator.h b/Source/Shared/StdAllocator.h index 3ea3ae20..e3561000 100644 --- a/Source/Shared/StdAllocator.h +++ b/Source/Shared/StdAllocator.h @@ -203,7 +203,9 @@ inline T* Allocate(StdAllocator& allocator, Args&&... args) const auto& lowLevelAllocator = allocator.GetInterface(); T* object = (T*)lowLevelAllocator.Allocate(lowLevelAllocator.userArg, sizeof(T), alignof(T)); - new (object) T(std::forward(args)...); + if (object) + new (object) T(std::forward(args)...); + return object; } @@ -213,8 +215,11 @@ inline T* AllocateArray(StdAllocator& allocator, size_t arraySize, Args const auto& lowLevelAllocator = allocator.GetInterface(); T* array = (T*)lowLevelAllocator.Allocate(lowLevelAllocator.userArg, arraySize * sizeof(T), alignof(T)); - for (size_t i = 0; i < arraySize; i++) - new (array + i) T(std::forward(args)...); + if (array) + { + for (size_t i = 0; i < arraySize; i++) + new (array + i) T(std::forward(args)...); + } return array; } @@ -222,7 +227,7 @@ inline T* AllocateArray(StdAllocator& allocator, size_t arraySize, Args template inline void Deallocate(StdAllocator& allocator, T* object) { - if (object == nullptr) + if (!object) return; object->~T(); @@ -234,7 +239,7 @@ inline void Deallocate(StdAllocator& allocator, T* object) template inline void DeallocateArray(StdAllocator& allocator, T* array, size_t arraySize) { - if (array == nullptr) + if (!array) return; for (size_t i = 0; i < arraySize; i++) @@ -244,7 +249,7 @@ inline void DeallocateArray(StdAllocator& allocator, T* array, size_t a lowLevelAllocator.Free(lowLevelAllocator.userArg, array); } -//============================================================================================================================== +//================================================================================================================ template using Vector = std::vector>; @@ -254,7 +259,7 @@ using UnorderedMap = std::unordered_map, std::equal_to, St using String = std::basic_string, StdAllocator>; -//============================================================================================================================== +//================================================================================================================ constexpr size_t STACK_ALLOC_MAX_SIZE = 65536; diff --git a/Source/Streamline/StreamlineWrapper.cpp b/Source/Streamline/StreamlineWrapper.cpp new file mode 100644 index 00000000..d9bbfec1 --- /dev/null +++ b/Source/Streamline/StreamlineWrapper.cpp @@ -0,0 +1,63 @@ +/* +Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. + +NVIDIA CORPORATION and its licensors retain all intellectual property +and proprietary rights in and to this software, related documentation +and any modifications thereto. Any use, reproduction, disclosure or +distribution of this software and related documentation without an express +license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include + +#define _STRINGIFY(str) L#str +#define STRINGIFY(str) _STRINGIFY(str) + +const wchar_t* pluginPath = STRINGIFY(SL_PLUGINS_DIRECTORY); + +bool slwrap::setConstants(const ::sl::Constants& values, uint32_t frameIndex, uint32_t id) +{ + return slSetConstants(values, frameIndex, id); +} + +bool slwrap::shutdown() +{ + return slShutdown(); +} + +bool slwrap::init(const sl::Preferences& pref, int applicationId) +{ + sl::Preferences preferences = pref; + if (preferences.numPathsToPlugins == 0) + { + preferences.pathsToPlugins = &pluginPath; + preferences.numPathsToPlugins = 1; + } + + return slInit(preferences, applicationId); +} + +bool slwrap::setTag(const sl::Resource* resource, sl::BufferType tag, uint32_t id, const sl::Extent* extent) +{ + return slSetTag(resource, tag, id, extent); +} + +bool slwrap::isFeatureSupported(sl::Feature feature, uint32_t* adapterBitMask) +{ + return slIsFeatureSupported(feature, adapterBitMask); +} + +bool slwrap::setFeatureConstants(sl::Feature feature, const void* consts, uint32_t frameIndex, uint32_t id) +{ + return slSetFeatureConstants(feature, consts, frameIndex, id); +} + +bool slwrap::evaluateFeature(sl::CommandBuffer* cmdBuffer, sl::Feature feature, uint32_t frameIndex, uint32_t id) +{ + return slEvaluateFeature(cmdBuffer, feature, frameIndex, id); +} + +bool slwrap::getFeatureSettings(sl::Feature feature, const void* consts, void* settings) +{ + return slGetFeatureSettings(feature, consts, settings); +} diff --git a/Source/VK/AccelerationStructureVK.cpp b/Source/VK/AccelerationStructureVK.cpp index 1f6e5dd4..0c5ea22a 100644 --- a/Source/VK/AccelerationStructureVK.cpp +++ b/Source/VK/AccelerationStructureVK.cpp @@ -13,7 +13,6 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include "AccelerationStructureVK.h" #include "DescriptorVK.h" #include "CommandQueueVK.h" -#include "DeviceVK.h" #include "ConversionVK.h" using namespace nri; @@ -75,7 +74,6 @@ Result AccelerationStructureVK::Create(const AccelerationStructureVulkanDesc& ac deviceAddressInfo.accelerationStructure = (VkAccelerationStructureKHR)accelerationStructureDesc.vkAccelerationStructure; const auto& vk = m_Device.GetDispatchTable(); - const uint64_t deviceAddress = vk.GetAccelerationStructureDeviceAddressKHR(m_Device, &deviceAddressInfo); if (deviceAddress == 0) @@ -112,12 +110,11 @@ void AccelerationStructureVK::PrecreateBottomLevel(const AccelerationStructureDe Vector geometries(m_Device.GetStdAllocator()); uint32_t* primitiveMaxNums = ALLOCATE_SCRATCH(m_Device, uint32_t, accelerationStructureDesc.instanceOrGeometryObjectNum); - const auto& vk = m_Device.GetDispatchTable(); - geometries.resize(accelerationStructureDesc.instanceOrGeometryObjectNum); ConvertGeometryObjectSizesVK(0, geometries.data(), primitiveMaxNums, accelerationStructureDesc.geometryObjects, (uint32_t)geometries.size()); buildInfo.pGeometries = geometries.data(); + const auto& vk = m_Device.GetDispatchTable(); vk.GetAccelerationStructureBuildSizesKHR(m_Device, buildType, &buildInfo, primitiveMaxNums, &sizeInfo); m_BuildScratchSize = sizeInfo.buildScratchSize; @@ -193,6 +190,10 @@ Result AccelerationStructureVK::FinishCreation() return Result::SUCCESS; } +//================================================================================================================ +// NRI +//================================================================================================================ + inline void AccelerationStructureVK::SetDebugName(const char* name) { std::array handles; @@ -208,16 +209,6 @@ inline void AccelerationStructureVK::GetMemoryInfo(MemoryDesc& memoryDesc) const m_Buffer->GetMemoryInfo(MemoryLocation::DEVICE, memoryDesc); } -inline uint64_t AccelerationStructureVK::GetUpdateScratchBufferSize() const -{ - return m_UpdateScratchSize; -} - -inline uint64_t AccelerationStructureVK::GetBuildScratchBufferSize() const -{ - return m_BuildScratchSize; -} - inline Result AccelerationStructureVK::CreateDescriptor(uint32_t physicalDeviceMask, Descriptor*& descriptor) const { DescriptorVK& descriptorImpl = *Allocate(m_Device.GetStdAllocator(), m_Device); diff --git a/Source/VK/AccelerationStructureVK.h b/Source/VK/AccelerationStructureVK.h index 79f42579..6534dd21 100644 --- a/Source/VK/AccelerationStructureVK.h +++ b/Source/VK/AccelerationStructureVK.h @@ -12,66 +12,63 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct DeviceVK; - - struct AccelerationStructureVK - { - AccelerationStructureVK(DeviceVK& device); - ~AccelerationStructureVK(); - - DeviceVK& GetDevice() const; - Result Create(const AccelerationStructureDesc& accelerationStructureDesc); - Result Create(const AccelerationStructureVulkanDesc& accelerationStructureDesc); - Result FinishCreation(); - VkAccelerationStructureKHR GetHandle(uint32_t physicalDeviceIndex) const; - BufferVK* GetBuffer() const; - - void SetDebugName(const char* name); - void GetMemoryInfo(MemoryDesc& memoryDesc) const; - uint64_t GetUpdateScratchBufferSize() const; - uint64_t GetBuildScratchBufferSize() const; - VkDeviceAddress GetNativeHandle(uint32_t physicalDeviceIndex) const; - Result CreateDescriptor(uint32_t physicalDeviceMask, Descriptor*& descriptor) const; - - private: - void PrecreateBottomLevel(const AccelerationStructureDesc& accelerationStructureDesc); - void PrecreateTopLevel(const AccelerationStructureDesc& accelerationStructureDesc); - - std::array m_Handles = {}; - std::array m_DeviceAddresses = {}; - BufferVK* m_Buffer = nullptr; - DeviceVK& m_Device; - uint64_t m_BuildScratchSize = 0; - uint64_t m_UpdateScratchSize = 0; - uint64_t m_AccelerationStructureSize = 0; - uint32_t m_PhysicalDeviceMask = 0; - VkAccelerationStructureTypeKHR m_Type = (VkAccelerationStructureTypeKHR)0; - VkBuildAccelerationStructureFlagsKHR m_BuildFlags = (VkBuildAccelerationStructureFlagsKHR)0; - bool m_OwnsNativeObjects = false; - }; - - inline AccelerationStructureVK::AccelerationStructureVK(DeviceVK& device) : + +struct DeviceVK; + +struct AccelerationStructureVK +{ + inline AccelerationStructureVK(DeviceVK& device) : m_Device(device) - { - } - - inline VkAccelerationStructureKHR AccelerationStructureVK::GetHandle(uint32_t physicalDeviceIndex) const - { - return m_Handles[physicalDeviceIndex]; - } - - inline DeviceVK& AccelerationStructureVK::GetDevice() const - { - return m_Device; - } - - inline VkDeviceAddress AccelerationStructureVK::GetNativeHandle(uint32_t physicalDeviceIndex) const - { - return m_DeviceAddresses[physicalDeviceIndex]; - } - - inline BufferVK* AccelerationStructureVK::GetBuffer() const - { - return m_Buffer; - } + {} + + inline VkAccelerationStructureKHR GetHandle(uint32_t physicalDeviceIndex) const + { return m_Handles[physicalDeviceIndex]; } + + inline DeviceVK& GetDevice() const + { return m_Device; } + + inline BufferVK* GetBuffer() const + { return m_Buffer; } + + ~AccelerationStructureVK(); + + Result Create(const AccelerationStructureDesc& accelerationStructureDesc); + Result Create(const AccelerationStructureVulkanDesc& accelerationStructureDesc); + Result FinishCreation(); + + //================================================================================================================ + // NRI + //================================================================================================================ + + inline uint64_t GetUpdateScratchBufferSize() const + { return m_UpdateScratchSize; } + + inline uint64_t GetBuildScratchBufferSize() const + { return m_BuildScratchSize; } + + inline VkDeviceAddress GetNativeHandle(uint32_t physicalDeviceIndex) const + { return m_DeviceAddresses[physicalDeviceIndex]; } + + void SetDebugName(const char* name); + void GetMemoryInfo(MemoryDesc& memoryDesc) const; + Result CreateDescriptor(uint32_t physicalDeviceMask, Descriptor*& descriptor) const; + +private: + void PrecreateBottomLevel(const AccelerationStructureDesc& accelerationStructureDesc); + void PrecreateTopLevel(const AccelerationStructureDesc& accelerationStructureDesc); + +private: + DeviceVK& m_Device; + std::array m_Handles = {}; + std::array m_DeviceAddresses = {}; + BufferVK* m_Buffer = nullptr; + uint64_t m_BuildScratchSize = 0; + uint64_t m_UpdateScratchSize = 0; + uint64_t m_AccelerationStructureSize = 0; + uint32_t m_PhysicalDeviceMask = 0; + VkAccelerationStructureTypeKHR m_Type = (VkAccelerationStructureTypeKHR)0; + VkBuildAccelerationStructureFlagsKHR m_BuildFlags = (VkBuildAccelerationStructureFlagsKHR)0; + bool m_OwnsNativeObjects = false; +}; + } \ No newline at end of file diff --git a/Source/VK/AcceleratrionStructureVK.hpp b/Source/VK/AcceleratrionStructureVK.hpp index 5cb897a4..8906566d 100644 --- a/Source/VK/AcceleratrionStructureVK.hpp +++ b/Source/VK/AcceleratrionStructureVK.hpp @@ -8,9 +8,7 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma once - -#pragma region [ RayTracingInterface ] +#pragma region [ RayTracing ] static void NRI_CALL GetAccelerationStructureMemoryInfo(const AccelerationStructure& accelerationStructure, MemoryDesc& memoryDesc) { @@ -48,15 +46,6 @@ static uint64_t NRI_CALL GetAccelerationStructureNativeObject(const Acceleration return uint64_t(((AccelerationStructureVK&)accelerationStructure).GetHandle(physicalDeviceIndex)); } -void FillFunctionTableAccelerationStructureVK(RayTracingInterface& rayTracingInterface) -{ - rayTracingInterface.GetAccelerationStructureMemoryInfo = ::GetAccelerationStructureMemoryInfo; - rayTracingInterface.GetAccelerationStructureUpdateScratchBufferSize = ::GetAccelerationStructureUpdateScratchBufferSize; - rayTracingInterface.GetAccelerationStructureBuildScratchBufferSize = ::GetAccelerationStructureBuildScratchBufferSize; - rayTracingInterface.GetAccelerationStructureHandle = ::GetAccelerationStructureHandle; - rayTracingInterface.SetAccelerationStructureDebugName = ::SetAccelerationStructureDebugName; - rayTracingInterface.CreateAccelerationStructureDescriptor = ::CreateAccelerationStructureDescriptor; - rayTracingInterface.GetAccelerationStructureNativeObject = ::GetAccelerationStructureNativeObject; -} +#pragma endregion -#pragma endregion \ No newline at end of file +Define_RayTracing_AccelerationStructure_PartiallyFillFunctionTable(VK) diff --git a/Source/VK/BufferVK.cpp b/Source/VK/BufferVK.cpp index c914567c..4452d3a0 100644 --- a/Source/VK/BufferVK.cpp +++ b/Source/VK/BufferVK.cpp @@ -12,7 +12,6 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include "BufferVK.h" #include "MemoryVK.h" #include "CommandQueueVK.h" -#include "DeviceVK.h" using namespace nri; @@ -118,7 +117,7 @@ void BufferVK::ReadDeviceAddress() const auto& vk = m_Device.GetDispatchTable(); - if (vk.GetBufferDeviceAddress == nullptr) + if (!vk.GetBufferDeviceAddress) return; for (uint32_t i = 0; i < m_Device.GetPhysicalDeviceGroupSize(); i++) @@ -131,6 +130,10 @@ void BufferVK::ReadDeviceAddress() } } +//================================================================================================================ +// NRI +//================================================================================================================ + void BufferVK::SetDebugName(const char* name) { std::array handles; diff --git a/Source/VK/BufferVK.h b/Source/VK/BufferVK.h index 574aef6f..973c014c 100644 --- a/Source/VK/BufferVK.h +++ b/Source/VK/BufferVK.h @@ -12,69 +12,61 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct DeviceVK; - struct MemoryVK; - - struct BufferVK - { - BufferVK(DeviceVK& device); - ~BufferVK(); - - DeviceVK& GetDevice() const; - VkBuffer GetHandle(uint32_t physicalDeviceIndex) const; - VkDeviceAddress GetDeviceAddress(uint32_t physicalDeviceIndex) const; - uint64_t GetSize() const; - Result Create(const BufferDesc& bufferDesc); - Result Create(const BufferVulkanDesc& bufferDesc); - void SetHostMemory(MemoryVK& memory, uint64_t memoryOffset); - void ReadDeviceAddress(); - - void SetDebugName(const char* name); - void GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const; - void* Map(uint64_t offset, uint64_t size); - void Unmap(); - - private: - std::array m_Handles = {}; - std::array m_DeviceAddresses = {}; - DeviceVK& m_Device; - uint64_t m_Size = 0; - MemoryVK* m_Memory = nullptr; - uint64_t m_MappedMemoryOffset = 0; - uint64_t m_MappedRangeOffset = 0; - uint64_t m_MappedRangeSize = 0; - bool m_OwnsNativeObjects = false; - }; - - inline BufferVK::BufferVK(DeviceVK& device) : + +struct DeviceVK; +struct MemoryVK; + +struct BufferVK +{ + inline BufferVK(DeviceVK& device) : m_Device(device) - { - } - - inline VkBuffer BufferVK::GetHandle(uint32_t physicalDeviceIndex) const - { - return m_Handles[physicalDeviceIndex]; - } - - inline VkDeviceAddress BufferVK::GetDeviceAddress(uint32_t physicalDeviceIndex) const - { - return m_DeviceAddresses[physicalDeviceIndex]; - } - - inline DeviceVK& BufferVK::GetDevice() const - { - return m_Device; - } - - inline uint64_t BufferVK::GetSize() const - { - return m_Size; - } - - inline VkDeviceAddress GetBufferDeviceAddress(const Buffer* buffer, uint32_t physicalDeviceIndex) - { - const BufferVK* bufferVK = (const BufferVK*)buffer; - - return bufferVK != nullptr ? bufferVK->GetDeviceAddress(physicalDeviceIndex) : 0; - } + {} + + inline VkBuffer GetHandle(uint32_t physicalDeviceIndex) const + { return m_Handles[physicalDeviceIndex]; } + + inline VkDeviceAddress GetDeviceAddress(uint32_t physicalDeviceIndex) const + { return m_DeviceAddresses[physicalDeviceIndex]; } + + inline DeviceVK& GetDevice() const + { return m_Device; } + + inline uint64_t GetSize() const + { return m_Size; } + + ~BufferVK(); + + Result Create(const BufferDesc& bufferDesc); + Result Create(const BufferVulkanDesc& bufferDesc); + void SetHostMemory(MemoryVK& memory, uint64_t memoryOffset); + void ReadDeviceAddress(); + + //================================================================================================================ + // NRI + //================================================================================================================ + + void SetDebugName(const char* name); + void GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const; + void* Map(uint64_t offset, uint64_t size); + void Unmap(); + +private: + DeviceVK& m_Device; + std::array m_Handles = {}; + std::array m_DeviceAddresses = {}; + uint64_t m_Size = 0; + MemoryVK* m_Memory = nullptr; + uint64_t m_MappedMemoryOffset = 0; + uint64_t m_MappedRangeOffset = 0; + uint64_t m_MappedRangeSize = 0; + bool m_OwnsNativeObjects = false; +}; + +inline VkDeviceAddress GetBufferDeviceAddress(const Buffer* buffer, uint32_t physicalDeviceIndex) +{ + const BufferVK* bufferVK = (const BufferVK*)buffer; + + return bufferVK != nullptr ? bufferVK->GetDeviceAddress(physicalDeviceIndex) : 0; +} + } \ No newline at end of file diff --git a/Source/VK/BufferVK.hpp b/Source/VK/BufferVK.hpp index 0ec1bcc4..558018f0 100644 --- a/Source/VK/BufferVK.hpp +++ b/Source/VK/BufferVK.hpp @@ -8,9 +8,7 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma once - -#pragma region [ CoreInterface ] +#pragma region [ Core ] static void NRI_CALL SetBufferDebugName(Buffer& buffer, const char* name) { @@ -37,13 +35,6 @@ static void NRI_CALL UnmapBuffer(Buffer& buffer) ((BufferVK&)buffer).Unmap(); } -void FillFunctionTableBufferVK(CoreInterface& coreInterface) -{ - coreInterface.SetBufferDebugName = ::SetBufferDebugName; - coreInterface.GetBufferNativeObject = ::GetBufferNativeObject; - coreInterface.GetBufferMemoryInfo = ::GetBufferMemoryInfo; - coreInterface.MapBuffer = ::MapBuffer; - coreInterface.UnmapBuffer = ::UnmapBuffer; -} - #pragma endregion + +Define_Core_Buffer_PartiallyFillFunctionTable(VK) diff --git a/Source/VK/CommandAllocatorVK.cpp b/Source/VK/CommandAllocatorVK.cpp index 767201c5..adccd8a8 100644 --- a/Source/VK/CommandAllocatorVK.cpp +++ b/Source/VK/CommandAllocatorVK.cpp @@ -12,7 +12,6 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include "CommandAllocatorVK.h" #include "CommandBufferVK.h" #include "CommandQueueVK.h" -#include "DeviceVK.h" using namespace nri; @@ -53,9 +52,14 @@ Result CommandAllocatorVK::Create(const CommandAllocatorVulkanDesc& commandAlloc m_OwnsNativeObjects = false; m_Handle = (VkCommandPool)commandAllocatorDesc.vkCommandPool; m_Type = commandAllocatorDesc.commandQueueType; + return Result::SUCCESS; } +//================================================================================================================ +// NRI +//================================================================================================================ + inline void CommandAllocatorVK::SetDebugName(const char* name) { m_Device.SetDebugNameToTrivialObject(VK_OBJECT_TYPE_COMMAND_POOL, (uint64_t)m_Handle, name); @@ -63,8 +67,6 @@ inline void CommandAllocatorVK::SetDebugName(const char* name) inline Result CommandAllocatorVK::CreateCommandBuffer(CommandBuffer*& commandBuffer) { - const auto& vk = m_Device.GetDispatchTable(); - const VkCommandBufferAllocateInfo info = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, nullptr, @@ -74,6 +76,8 @@ inline Result CommandAllocatorVK::CreateCommandBuffer(CommandBuffer*& commandBuf }; VkCommandBuffer commandBufferHandle = VK_NULL_HANDLE; + + const auto& vk = m_Device.GetDispatchTable(); const VkResult result = vk.AllocateCommandBuffers(m_Device, &info, &commandBufferHandle); RETURN_ON_FAILURE(m_Device.GetLog(), result == VK_SUCCESS, GetReturnCode(result), diff --git a/Source/VK/CommandAllocatorVK.h b/Source/VK/CommandAllocatorVK.h index 30f64a48..1bb6382c 100644 --- a/Source/VK/CommandAllocatorVK.h +++ b/Source/VK/CommandAllocatorVK.h @@ -12,41 +12,39 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct DeviceVK; - - struct CommandAllocatorVK - { - CommandAllocatorVK(DeviceVK& device); - ~CommandAllocatorVK(); - - operator VkCommandPool() const; - DeviceVK& GetDevice() const; - Result Create(const CommandQueue& commandQueue, uint32_t physicalDeviceMask); - Result Create(const CommandAllocatorVulkanDesc& commandAllocatorDesc); - - void SetDebugName(const char* name); - Result CreateCommandBuffer(CommandBuffer*& commandBuffer); - void Reset(); - - private: - VkCommandPool m_Handle = VK_NULL_HANDLE; - CommandQueueType m_Type = (CommandQueueType)0; - DeviceVK& m_Device; - bool m_OwnsNativeObjects = false; - }; - - inline CommandAllocatorVK::CommandAllocatorVK(DeviceVK& device) : + +struct DeviceVK; + +struct CommandAllocatorVK +{ + inline CommandAllocatorVK(DeviceVK& device) : m_Device(device) - { - } - - inline CommandAllocatorVK::operator VkCommandPool() const - { - return m_Handle; - } - - inline DeviceVK& CommandAllocatorVK::GetDevice() const - { - return m_Device; - } + {} + + inline operator VkCommandPool() const + { return m_Handle; } + + inline DeviceVK& GetDevice() const + { return m_Device; } + + ~CommandAllocatorVK(); + + Result Create(const CommandQueue& commandQueue, uint32_t physicalDeviceMask); + Result Create(const CommandAllocatorVulkanDesc& commandAllocatorDesc); + + //================================================================================================================ + // NRI + //================================================================================================================ + + void SetDebugName(const char* name); + Result CreateCommandBuffer(CommandBuffer*& commandBuffer); + void Reset(); + +private: + DeviceVK& m_Device; + VkCommandPool m_Handle = VK_NULL_HANDLE; + CommandQueueType m_Type = (CommandQueueType)0; + bool m_OwnsNativeObjects = false; +}; + } diff --git a/Source/VK/CommandAllocatorVK.hpp b/Source/VK/CommandAllocatorVK.hpp index 0e44aaf1..39d6caef 100644 --- a/Source/VK/CommandAllocatorVK.hpp +++ b/Source/VK/CommandAllocatorVK.hpp @@ -8,9 +8,7 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma once - -#pragma region [ CoreInterface ] +#pragma region [ Core ] static void NRI_CALL SetCommandAllocatorDebugName(CommandAllocator& commandAllocator, const char* name) { @@ -27,11 +25,6 @@ static void NRI_CALL ResetCommandAllocator(CommandAllocator& commandAllocator) ((CommandAllocatorVK&)commandAllocator).Reset(); } -void FillFunctionTableCommandAllocatorVK(CoreInterface& coreInterface) -{ - coreInterface.SetCommandAllocatorDebugName = ::SetCommandAllocatorDebugName; - coreInterface.CreateCommandBuffer = ::CreateCommandBuffer; - coreInterface.ResetCommandAllocator = ::ResetCommandAllocator; -} - #pragma endregion + +Define_Core_CommandAllocator_PartiallyFillFunctionTable(VK) diff --git a/Source/VK/CommandBufferVK.cpp b/Source/VK/CommandBufferVK.cpp index 292f5c04..733d6c25 100644 --- a/Source/VK/CommandBufferVK.cpp +++ b/Source/VK/CommandBufferVK.cpp @@ -20,7 +20,6 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include "CommandQueueVK.h" #include "QueryPoolVK.h" #include "AccelerationStructureVK.h" -#include "DeviceVK.h" using namespace nri; @@ -32,12 +31,6 @@ struct Barriers uint32_t imageNum; }; -CommandBufferVK::CommandBufferVK(DeviceVK& device) : - m_VK(device.GetDispatchTable()), - m_Device(device) -{ -} - CommandBufferVK::~CommandBufferVK() { if (m_CommandPool == VK_NULL_HANDLE) @@ -63,6 +56,10 @@ Result CommandBufferVK::Create(const CommandBufferVulkanDesc& commandBufferDesc) return Result::SUCCESS; } +//================================================================================================================ +// NRI +//================================================================================================================ + inline void CommandBufferVK::SetDebugName(const char* name) { m_Device.SetDebugNameToTrivialObject(VK_OBJECT_TYPE_COMMAND_BUFFER, (uint64_t)m_Handle, name); @@ -93,13 +90,14 @@ inline Result CommandBufferVK::Begin(const DescriptorPool* descriptorPool, uint3 info.pNext = &deviceGroupInfo; } - const VkResult result = m_VK.BeginCommandBuffer(m_Handle, &info); + const auto& vk = m_Device.GetDispatchTable(); + const VkResult result = vk.BeginCommandBuffer(m_Handle, &info); RETURN_ON_FAILURE(m_Device.GetLog(), result == VK_SUCCESS, GetReturnCode(result), "Can't begin a command buffer: vkBeginCommandBuffer returned %d.", (int32_t)result); if (m_Type == CommandQueueType::GRAPHICS) - m_VK.CmdSetDepthBounds(m_Handle, 0.0f, 1.0f); + vk.CmdSetDepthBounds(m_Handle, 0.0f, 1.0f); m_CurrentPipelineBindPoint = VK_PIPELINE_BIND_POINT_MAX_ENUM; m_CurrentPipelineLayoutHandle = VK_NULL_HANDLE; @@ -112,7 +110,8 @@ inline Result CommandBufferVK::Begin(const DescriptorPool* descriptorPool, uint3 inline Result CommandBufferVK::End() { - const VkResult result = m_VK.EndCommandBuffer(m_Handle); + const auto& vk = m_Device.GetDispatchTable(); + const VkResult result = vk.EndCommandBuffer(m_Handle); RETURN_ON_FAILURE(m_Device.GetLog(), result == VK_SUCCESS, GetReturnCode(result), "Can't end a command buffer: vkEndCommandBuffer returned %d.", (int32_t)result); @@ -133,22 +132,26 @@ inline void CommandBufferVK::SetViewports(const Viewport* viewports, uint32_t vi flippedViewport.height = -viewport.height; } - m_VK.CmdSetViewport(m_Handle, 0, viewportNum, flippedViewports); + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdSetViewport(m_Handle, 0, viewportNum, flippedViewports); } inline void CommandBufferVK::SetScissors(const Rect* rects, uint32_t rectNum) { - m_VK.CmdSetScissor(m_Handle, 0, rectNum, (const VkRect2D*)rects); + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdSetScissor(m_Handle, 0, rectNum, (const VkRect2D*)rects); } inline void CommandBufferVK::SetDepthBounds(float boundsMin, float boundsMax) { - m_VK.CmdSetDepthBounds(m_Handle, boundsMin, boundsMax); + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdSetDepthBounds(m_Handle, boundsMin, boundsMax); } inline void CommandBufferVK::SetStencilReference(uint8_t reference) { - m_VK.CmdSetStencilReference(m_Handle, VK_STENCIL_FRONT_AND_BACK, reference); + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdSetStencilReference(m_Handle, VK_STENCIL_FRONT_AND_BACK, reference); } inline void CommandBufferVK::SetSamplePositions(const SamplePosition* positions, uint32_t positionNum) @@ -224,13 +227,15 @@ inline void CommandBufferVK::ClearAttachments(const ClearDesc* clearDescs, uint3 } } - m_VK.CmdClearAttachments(m_Handle, clearDescNum, attachments, rectNum, clearRects); + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdClearAttachments(m_Handle, clearDescNum, attachments, rectNum, clearRects); } inline void CommandBufferVK::ClearStorageBuffer(const ClearStorageBufferDesc& clearDesc) { const DescriptorVK& descriptor = *(const DescriptorVK*)clearDesc.storageBuffer; - m_VK.CmdFillBuffer(m_Handle, descriptor.GetBuffer(m_PhysicalDeviceIndex), 0, VK_WHOLE_SIZE, clearDesc.value); + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdFillBuffer(m_Handle, descriptor.GetBuffer(m_PhysicalDeviceIndex), 0, VK_WHOLE_SIZE, clearDesc.value); } inline void CommandBufferVK::ClearStorageTexture(const ClearStorageTextureDesc& clearDesc) @@ -241,7 +246,8 @@ inline void CommandBufferVK::ClearStorageTexture(const ClearStorageTextureDesc& VkImageSubresourceRange range; descriptor.GetImageSubresourceRange(range); - m_VK.CmdClearColorImage(m_Handle, descriptor.GetImage(m_PhysicalDeviceIndex), VK_IMAGE_LAYOUT_GENERAL, value, 1, &range); + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdClearColorImage(m_Handle, descriptor.GetImage(m_PhysicalDeviceIndex), VK_IMAGE_LAYOUT_GENERAL, value, 1, &range); } inline void CommandBufferVK::BeginRenderPass(const FrameBuffer& frameBuffer, RenderPassBeginFlag renderPassBeginFlag) @@ -262,14 +268,16 @@ inline void CommandBufferVK::BeginRenderPass(const FrameBuffer& frameBuffer, Ren values }; - m_VK.CmdBeginRenderPass(m_Handle, &info, VK_SUBPASS_CONTENTS_INLINE); + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdBeginRenderPass(m_Handle, &info, VK_SUBPASS_CONTENTS_INLINE); m_CurrentFrameBuffer = &frameBufferImpl; } inline void CommandBufferVK::EndRenderPass() { - m_VK.CmdEndRenderPass(m_Handle); + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdEndRenderPass(m_Handle); } inline void CommandBufferVK::SetVertexBuffers(uint32_t baseSlot, uint32_t bufferNum, const Buffer* const* buffers, const uint64_t* offsets) @@ -279,13 +287,15 @@ inline void CommandBufferVK::SetVertexBuffers(uint32_t baseSlot, uint32_t buffer for (uint32_t i = 0; i < bufferNum; i++) bufferHandles[i] = GetVulkanHandle(buffers[i], m_PhysicalDeviceIndex); - m_VK.CmdBindVertexBuffers(m_Handle, baseSlot, bufferNum, bufferHandles, offsets); + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdBindVertexBuffers(m_Handle, baseSlot, bufferNum, bufferHandles, offsets); } inline void CommandBufferVK::SetIndexBuffer(const Buffer& buffer, uint64_t offset, IndexType indexType) { const VkBuffer bufferHandle = GetVulkanHandle(&buffer, m_PhysicalDeviceIndex); - m_VK.CmdBindIndexBuffer(m_Handle, bufferHandle, offset, GetIndexType(indexType)); + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdBindIndexBuffer(m_Handle, bufferHandle, offset, GetIndexType(indexType)); } inline void CommandBufferVK::SetPipelineLayout(const PipelineLayout& pipelineLayout) @@ -304,7 +314,8 @@ inline void CommandBufferVK::SetPipeline(const Pipeline& pipeline) const PipelineVK& pipelineImpl = (const PipelineVK&)pipeline; - m_VK.CmdBindPipeline(m_Handle, pipelineImpl.GetBindPoint(), pipelineImpl); + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdBindPipeline(m_Handle, pipelineImpl.GetBindPoint(), pipelineImpl); m_CurrentPipeline = &pipelineImpl; } @@ -321,7 +332,8 @@ inline void CommandBufferVK::SetDescriptorSet(uint32_t setIndexInPipelineLayout, uint32_t space = m_CurrentPipelineLayout->GetDescriptorSetSpace(setIndexInPipelineLayout); uint32_t dynamicOffsetNum = descriptorSetImpl.GetDynamicConstantBufferNum(); - m_VK.CmdBindDescriptorSets( + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdBindDescriptorSets( m_Handle, m_CurrentPipelineBindPoint, m_CurrentPipelineLayoutHandle, @@ -334,29 +346,34 @@ inline void CommandBufferVK::SetConstants(uint32_t pushConstantIndex, const void const auto& bindingInfo = m_CurrentPipelineLayout->GetRuntimeBindingInfo(); const PushConstantRangeBindingDesc& desc = bindingInfo.pushConstantBindings[pushConstantIndex]; - m_VK.CmdPushConstants(m_Handle, m_CurrentPipelineLayoutHandle, desc.flags, desc.offset, size, data); + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdPushConstants(m_Handle, m_CurrentPipelineLayoutHandle, desc.flags, desc.offset, size, data); } inline void CommandBufferVK::Draw(uint32_t vertexNum, uint32_t instanceNum, uint32_t baseVertex, uint32_t baseInstance) { - m_VK.CmdDraw(m_Handle, vertexNum, instanceNum, baseVertex, baseInstance); + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdDraw(m_Handle, vertexNum, instanceNum, baseVertex, baseInstance); } inline void CommandBufferVK::DrawIndexed(uint32_t indexNum, uint32_t instanceNum, uint32_t baseIndex, uint32_t baseVertex, uint32_t baseInstance) { - m_VK.CmdDrawIndexed(m_Handle, indexNum, instanceNum, baseIndex, baseVertex, baseInstance); + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdDrawIndexed(m_Handle, indexNum, instanceNum, baseIndex, baseVertex, baseInstance); } inline void CommandBufferVK::DrawIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride) { const VkBuffer bufferHandle = GetVulkanHandle(&buffer, m_PhysicalDeviceIndex); - m_VK.CmdDrawIndirect(m_Handle, bufferHandle, offset, drawNum, (uint32_t)stride); + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdDrawIndirect(m_Handle, bufferHandle, offset, drawNum, (uint32_t)stride); } inline void CommandBufferVK::DrawIndexedIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride) { const VkBuffer bufferHandle = GetVulkanHandle(&buffer, m_PhysicalDeviceIndex); - m_VK.CmdDrawIndexedIndirect(m_Handle, bufferHandle, offset, drawNum, (uint32_t)stride); + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdDrawIndexedIndirect(m_Handle, bufferHandle, offset, drawNum, (uint32_t)stride); } inline void CommandBufferVK::CopyBuffer(Buffer& dstBuffer, uint32_t dstPhysicalDeviceIndex, uint64_t dstOffset, const Buffer& srcBuffer, @@ -371,7 +388,8 @@ inline void CommandBufferVK::CopyBuffer(Buffer& dstBuffer, uint32_t dstPhysicalD size == WHOLE_SIZE ? srcBufferImpl.GetSize() : size }; - m_VK.CmdCopyBuffer(m_Handle, srcBufferImpl.GetHandle(srcPhysicalDeviceIndex), dstBufferImpl.GetHandle(dstPhysicalDeviceIndex), 1, ®ion); + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdCopyBuffer(m_Handle, srcBufferImpl.GetHandle(srcPhysicalDeviceIndex), dstBufferImpl.GetHandle(dstPhysicalDeviceIndex), 1, ®ion); } inline void CommandBufferVK::CopyTexture(Texture& dstTexture, uint32_t dstPhysicalDeviceIndex, const TextureRegionDesc* dstRegionDesc, @@ -449,7 +467,8 @@ inline void CommandBufferVK::CopyTexture(Texture& dstTexture, uint32_t dstPhysic region.dstOffset = {}; } - m_VK.CmdCopyImage(m_Handle, srcTextureImpl.GetHandle(dstPhysicalDeviceIndex), VK_IMAGE_LAYOUT_GENERAL, + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdCopyImage(m_Handle, srcTextureImpl.GetHandle(dstPhysicalDeviceIndex), VK_IMAGE_LAYOUT_GENERAL, dstTextureImpl.GetHandle(srcPhysicalDeviceIndex), VK_IMAGE_LAYOUT_GENERAL, 1, ®ion); } @@ -486,7 +505,8 @@ inline void CommandBufferVK::UploadBufferToTexture(Texture& dstTexture, const Te } }; - m_VK.CmdCopyBufferToImage(m_Handle, srcBufferImpl.GetHandle(0), dstTextureImpl.GetHandle(m_PhysicalDeviceIndex), VK_IMAGE_LAYOUT_GENERAL, 1, ®ion); + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdCopyBufferToImage(m_Handle, srcBufferImpl.GetHandle(0), dstTextureImpl.GetHandle(m_PhysicalDeviceIndex), VK_IMAGE_LAYOUT_GENERAL, 1, ®ion); } inline void CommandBufferVK::ReadbackTextureToBuffer(Buffer& dstBuffer, TextureDataLayoutDesc& dstDataLayoutDesc, const Texture& srcTexture, const TextureRegionDesc& srcRegionDesc) @@ -522,18 +542,21 @@ inline void CommandBufferVK::ReadbackTextureToBuffer(Buffer& dstBuffer, TextureD } }; - m_VK.CmdCopyImageToBuffer(m_Handle, srcTextureImpl.GetHandle(m_PhysicalDeviceIndex), VK_IMAGE_LAYOUT_GENERAL, dstBufferImpl.GetHandle(0), 1, ®ion); + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdCopyImageToBuffer(m_Handle, srcTextureImpl.GetHandle(m_PhysicalDeviceIndex), VK_IMAGE_LAYOUT_GENERAL, dstBufferImpl.GetHandle(0), 1, ®ion); } inline void CommandBufferVK::Dispatch(uint32_t x, uint32_t y, uint32_t z) { - m_VK.CmdDispatch(m_Handle, x, y, z); + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdDispatch(m_Handle, x, y, z); } inline void CommandBufferVK::DispatchIndirect(const Buffer& buffer, uint64_t offset) { const BufferVK& bufferImpl = (const BufferVK&)buffer; - m_VK.CmdDispatchIndirect(m_Handle, bufferImpl.GetHandle(m_PhysicalDeviceIndex), offset); + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdDispatchIndirect(m_Handle, bufferImpl.GetHandle(m_PhysicalDeviceIndex), offset); } inline void CommandBufferVK::PipelineBarrier(const TransitionBarrierDesc* transitionBarriers, const AliasingBarrierDesc* aliasingBarriers, BarrierDependency dependency) @@ -565,7 +588,8 @@ inline void CommandBufferVK::PipelineBarrier(const TransitionBarrierDesc* transi FillTransitionImageBarriers(*transitionBarriers, barriers); // TODO: more optimal srcStageMask and dstStageMask - m_VK.CmdPipelineBarrier( + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdPipelineBarrier( m_Handle, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, @@ -581,20 +605,19 @@ inline void CommandBufferVK::PipelineBarrier(const TransitionBarrierDesc* transi inline void CommandBufferVK::BeginQuery(const QueryPool& queryPool, uint32_t offset) { const QueryPoolVK& queryPoolImpl = (const QueryPoolVK&)queryPool; - m_VK.CmdBeginQuery(m_Handle, queryPoolImpl.GetHandle(m_PhysicalDeviceIndex), offset, (VkQueryControlFlagBits)0); + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdBeginQuery(m_Handle, queryPoolImpl.GetHandle(m_PhysicalDeviceIndex), offset, (VkQueryControlFlagBits)0); } inline void CommandBufferVK::EndQuery(const QueryPool& queryPool, uint32_t offset) { const QueryPoolVK& queryPoolImpl = (const QueryPoolVK&)queryPool; + const auto& vk = m_Device.GetDispatchTable(); if (queryPoolImpl.GetType() == VK_QUERY_TYPE_TIMESTAMP) - { - m_VK.CmdWriteTimestamp(m_Handle, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, queryPoolImpl.GetHandle(m_PhysicalDeviceIndex), offset); - return; - } - - m_VK.CmdEndQuery(m_Handle, queryPoolImpl.GetHandle(m_PhysicalDeviceIndex), offset); + vk.CmdWriteTimestamp(m_Handle, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, queryPoolImpl.GetHandle(m_PhysicalDeviceIndex), offset); + else + vk.CmdEndQuery(m_Handle, queryPoolImpl.GetHandle(m_PhysicalDeviceIndex), offset); } inline void CommandBufferVK::CopyQueries(const QueryPool& queryPool, uint32_t offset, uint32_t num, Buffer& dstBuffer, uint64_t dstOffset) @@ -606,7 +629,8 @@ inline void CommandBufferVK::CopyQueries(const QueryPool& queryPool, uint32_t of if (queryPoolImpl.GetType() == VK_QUERY_TYPE_TIMESTAMP) flags = VK_QUERY_RESULT_64_BIT; - m_VK.CmdCopyQueryPoolResults(m_Handle, queryPoolImpl.GetHandle(m_PhysicalDeviceIndex), offset, num, bufferImpl.GetHandle(m_PhysicalDeviceIndex), dstOffset, + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdCopyQueryPoolResults(m_Handle, queryPoolImpl.GetHandle(m_PhysicalDeviceIndex), offset, num, bufferImpl.GetHandle(m_PhysicalDeviceIndex), dstOffset, queryPoolImpl.GetStride(), flags); } @@ -614,26 +638,29 @@ inline void CommandBufferVK::ResetQueries(const QueryPool& queryPool, uint32_t o { const QueryPoolVK& queryPoolImpl = (const QueryPoolVK&)queryPool; - m_VK.CmdResetQueryPool(m_Handle, queryPoolImpl.GetHandle(m_PhysicalDeviceIndex), offset, num); + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdResetQueryPool(m_Handle, queryPoolImpl.GetHandle(m_PhysicalDeviceIndex), offset, num); } inline void CommandBufferVK::BeginAnnotation(const char* name) { - if (m_VK.CmdBeginDebugUtilsLabelEXT == nullptr) + const auto& vk = m_Device.GetDispatchTable(); + if (vk.CmdBeginDebugUtilsLabelEXT == nullptr) return; VkDebugUtilsLabelEXT info = { VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT }; info.pLabelName = name; - m_VK.CmdBeginDebugUtilsLabelEXT(m_Handle, &info); + vk.CmdBeginDebugUtilsLabelEXT(m_Handle, &info); } inline void CommandBufferVK::EndAnnotation() { - if (m_VK.CmdEndDebugUtilsLabelEXT == nullptr) + const auto& vk = m_Device.GetDispatchTable(); + if (vk.CmdEndDebugUtilsLabelEXT == nullptr) return; - m_VK.CmdEndDebugUtilsLabelEXT(m_Handle); + vk.CmdEndDebugUtilsLabelEXT(m_Handle); } inline void CommandBufferVK::FillAliasingBufferBarriers(const AliasingBarrierDesc& aliasing, Barriers& barriers) const @@ -763,7 +790,8 @@ inline void CommandBufferVK::CopyWholeTexture(const TextureVK& dstTexture, uint3 regions[i].extent = dstTexture.GetExtent(); } - m_VK.CmdCopyImage(m_Handle, + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdCopyImage(m_Handle, srcTexture.GetHandle(srcPhysicalDeviceIndex), VK_IMAGE_LAYOUT_GENERAL, dstTexture.GetHandle(dstPhysicalDeviceIndex), VK_IMAGE_LAYOUT_GENERAL, dstTexture.GetMipNum(), regions); @@ -799,7 +827,8 @@ inline void CommandBufferVK::BuildTopLevelAccelerationStructure(uint32_t instanc const VkAccelerationStructureBuildRangeInfoKHR* rangeArrays[1] = { &range }; - m_VK.CmdBuildAccelerationStructuresKHR(m_Handle, 1, &buildGeometryInfo, rangeArrays); + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdBuildAccelerationStructuresKHR(m_Handle, 1, &buildGeometryInfo, rangeArrays); } inline void CommandBufferVK::BuildBottomLevelAccelerationStructure(uint32_t geometryObjectNum, const GeometryObject* geometryObjects, @@ -825,7 +854,8 @@ inline void CommandBufferVK::BuildBottomLevelAccelerationStructure(uint32_t geom const VkAccelerationStructureBuildRangeInfoKHR* rangeArrays[1] = { ranges }; - m_VK.CmdBuildAccelerationStructuresKHR(m_Handle, 1, &buildGeometryInfo, rangeArrays); + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdBuildAccelerationStructuresKHR(m_Handle, 1, &buildGeometryInfo, rangeArrays); FREE_SCRATCH(m_Device, ranges, geometryObjectNum); FREE_SCRATCH(m_Device, geometries, geometryObjectNum); @@ -863,7 +893,8 @@ inline void CommandBufferVK::UpdateTopLevelAccelerationStructure(uint32_t instan const VkAccelerationStructureBuildRangeInfoKHR* rangeArrays[1] = { &range }; - m_VK.CmdBuildAccelerationStructuresKHR(m_Handle, 1, &buildGeometryInfo, rangeArrays); + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdBuildAccelerationStructuresKHR(m_Handle, 1, &buildGeometryInfo, rangeArrays); } inline void CommandBufferVK::UpdateBottomLevelAccelerationStructure(uint32_t geometryObjectNum, const GeometryObject* geometryObjects, @@ -891,7 +922,8 @@ inline void CommandBufferVK::UpdateBottomLevelAccelerationStructure(uint32_t geo const VkAccelerationStructureBuildRangeInfoKHR* rangeArrays[1] = { ranges }; - m_VK.CmdBuildAccelerationStructuresKHR(m_Handle, 1, &buildGeometryInfo, rangeArrays); + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdBuildAccelerationStructuresKHR(m_Handle, 1, &buildGeometryInfo, rangeArrays); FREE_SCRATCH(m_Device, ranges, geometryObjectNum); FREE_SCRATCH(m_Device, geometries, geometryObjectNum); @@ -908,7 +940,8 @@ inline void CommandBufferVK::CopyAccelerationStructure(AccelerationStructure& ds info.dst = dstASHandle; info.mode = GetCopyMode(copyMode); - m_VK.CmdCopyAccelerationStructureKHR(m_Handle, &info); + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdCopyAccelerationStructureKHR(m_Handle, &info); } inline void CommandBufferVK::WriteAccelerationStructureSize(const AccelerationStructure* const* accelerationStructures, uint32_t accelerationStructureNum, @@ -921,7 +954,8 @@ inline void CommandBufferVK::WriteAccelerationStructureSize(const AccelerationSt const VkQueryPool queryPoolHandle = ((const QueryPoolVK&)queryPool).GetHandle(m_PhysicalDeviceIndex); - m_VK.CmdWriteAccelerationStructuresPropertiesKHR(m_Handle, accelerationStructureNum, ASes, VK_QUERY_TYPE_ACCELERATION_STRUCTURE_COMPACTED_SIZE_KHR, + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdWriteAccelerationStructuresPropertiesKHR(m_Handle, accelerationStructureNum, ASes, VK_QUERY_TYPE_ACCELERATION_STRUCTURE_COMPACTED_SIZE_KHR, queryPoolHandle, queryPoolOffset); FREE_SCRATCH(m_Device, ASes, accelerationStructureNum); @@ -949,12 +983,14 @@ inline void CommandBufferVK::DispatchRays(const DispatchRaysDesc& dispatchRaysDe callable.size = dispatchRaysDesc.callableShaders.size; callable.stride = dispatchRaysDesc.callableShaders.stride; - m_VK.CmdTraceRaysKHR(m_Handle, &raygen, &miss, &hit, &callable, dispatchRaysDesc.width, dispatchRaysDesc.height, dispatchRaysDesc.depth); + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdTraceRaysKHR(m_Handle, &raygen, &miss, &hit, &callable, dispatchRaysDesc.width, dispatchRaysDesc.height, dispatchRaysDesc.depth); } -inline void CommandBufferVK::DispatchMeshTasks(uint32_t taskNum) +inline void CommandBufferVK::DispatchMeshTasks(uint32_t x, uint32_t y, uint32_t z) { - m_VK.CmdDrawMeshTasksNV(m_Handle, taskNum, 0); + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdDrawMeshTasksEXT(m_Handle, x, y, z); } #include "CommandBufferVK.hpp" \ No newline at end of file diff --git a/Source/VK/CommandBufferVK.h b/Source/VK/CommandBufferVK.h index 40bd28ee..c16b3fef 100644 --- a/Source/VK/CommandBufferVK.h +++ b/Source/VK/CommandBufferVK.h @@ -14,123 +14,126 @@ struct Barriers; namespace nri { - struct DeviceVK; - struct PipelineVK; - struct PipelineLayoutVK; - struct TextureVK; - struct FrameBufferVK; - - struct CommandBufferVK - { - CommandBufferVK(DeviceVK& device); - ~CommandBufferVK(); - - operator VkCommandBuffer() const; - DeviceVK& GetDevice() const; - void Create(VkCommandPool commandPool, VkCommandBuffer commandBuffer, CommandQueueType type); - Result Create(const CommandBufferVulkanDesc& commandBufferDesc); - - void SetDebugName(const char* name); - Result Begin(const DescriptorPool* descriptorPool, uint32_t physicalDeviceIndex); - Result End(); - - void SetPipeline(const Pipeline& pipeline); - void SetPipelineLayout(const PipelineLayout& pipelineLayout); - void SetDescriptorSet(uint32_t setIndexInPipelineLayout, const DescriptorSet& descriptorSet, const uint32_t* dynamicConstantBufferOffsets); - void SetConstants(uint32_t pushConstantIndex, const void* data, uint32_t size); - void SetDescriptorPool(const DescriptorPool& descriptorPool); - void PipelineBarrier(const TransitionBarrierDesc* transitionBarriers, const AliasingBarrierDesc* aliasingBarriers, BarrierDependency dependency); - - void BeginRenderPass(const FrameBuffer& frameBuffer, RenderPassBeginFlag renderPassBeginFlag); - void EndRenderPass(); - void SetViewports(const Viewport* viewports, uint32_t viewportNum); - void SetScissors(const Rect* rects, uint32_t rectNum); - void SetDepthBounds(float boundsMin, float boundsMax); - void SetStencilReference(uint8_t reference); - void SetSamplePositions(const SamplePosition* positions, uint32_t positionNum); - void ClearAttachments(const ClearDesc* clearDescs, uint32_t clearDescNum, const Rect* rects, uint32_t rectNum); - void SetIndexBuffer(const Buffer& buffer, uint64_t offset, IndexType indexType); - void SetVertexBuffers(uint32_t baseSlot, uint32_t bufferNum, const Buffer* const* buffers, const uint64_t* offsets); - - void Draw(uint32_t vertexNum, uint32_t instanceNum, uint32_t baseVertex, uint32_t baseInstance); - void DrawIndexed(uint32_t indexNum, uint32_t instanceNum, uint32_t baseIndex, uint32_t baseVertex, uint32_t baseInstance); - void DrawIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride); - void DrawIndexedIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride); - void Dispatch(uint32_t x, uint32_t y, uint32_t z); - void DispatchIndirect(const Buffer& buffer, uint64_t offset); - void BeginQuery(const QueryPool& queryPool, uint32_t offset); - void EndQuery(const QueryPool& queryPool, uint32_t offset); - void BeginAnnotation(const char* name); - void EndAnnotation(); - - void ClearStorageBuffer(const ClearStorageBufferDesc& clearDesc); - - void ClearStorageTexture(const ClearStorageTextureDesc& clearDesc); - - void CopyBuffer(Buffer& dstBuffer, uint32_t dstPhysicalDeviceIndex, uint64_t dstOffset, const Buffer& srcBuffer, - uint32_t srcPhysicalDeviceIndex, uint64_t srcOffset, uint64_t size); - - void CopyTexture(Texture& dstTexture, uint32_t dstPhysicalDeviceIndex, const TextureRegionDesc* dstRegionDesc, - const Texture& srcTexture, uint32_t srcPhysicalDeviceIndex, const TextureRegionDesc* srcRegionDesc); - - void UploadBufferToTexture(Texture& dstTexture, const TextureRegionDesc& dstRegionDesc, const Buffer& srcBuffer, - const TextureDataLayoutDesc& srcDataLayoutDesc); - - void ReadbackTextureToBuffer(Buffer& dstBuffer, TextureDataLayoutDesc& dstDataLayoutDesc, const Texture& srcTexture, - const TextureRegionDesc& srcRegionDesc); - - void CopyQueries(const QueryPool& queryPool, uint32_t offset, uint32_t num, Buffer& dstBuffer, uint64_t dstOffset); - void ResetQueries(const QueryPool& queryPool, uint32_t offset, uint32_t num); - - void BuildTopLevelAccelerationStructure(uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, - AccelerationStructureBuildBits flags, AccelerationStructure& dst, Buffer& scratch, uint64_t scratchOffset); - - void BuildBottomLevelAccelerationStructure(uint32_t geometryObjectNum, const GeometryObject* geometryObjects, - AccelerationStructureBuildBits flags, AccelerationStructure& dst, Buffer& scratch, uint64_t scratchOffset); - - void UpdateTopLevelAccelerationStructure(uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, - AccelerationStructureBuildBits flags, AccelerationStructure& dst, AccelerationStructure& src, Buffer& scratch, - uint64_t scratchOffset); - - void UpdateBottomLevelAccelerationStructure(uint32_t geometryObjectNum, const GeometryObject* geometryObjects, - AccelerationStructureBuildBits flags, AccelerationStructure& dst, AccelerationStructure& src, Buffer& scratch, - uint64_t scratchOffset); - - void CopyAccelerationStructure(AccelerationStructure& dst, AccelerationStructure& src, CopyMode copyMode); - - void WriteAccelerationStructureSize(const AccelerationStructure* const* accelerationStructures, - uint32_t accelerationStructureNum, QueryPool& queryPool, uint32_t queryPoolOffset); - - void DispatchRays(const DispatchRaysDesc& dispatchRaysDesc); - void DispatchMeshTasks(uint32_t taskNum); - - private: - void FillAliasingBufferBarriers(const AliasingBarrierDesc& aliasing, Barriers& barriers) const; - void FillAliasingImageBarriers(const AliasingBarrierDesc& aliasing, Barriers& barriers) const; - void FillTransitionBufferBarriers(const TransitionBarrierDesc& transitions, Barriers& barriers) const; - void FillTransitionImageBarriers(const TransitionBarrierDesc& transitions, Barriers& barriers) const; - void CopyWholeTexture(const TextureVK& dstTexture, uint32_t dstPhysicalDeviceIndex, const TextureVK& srcTexture, uint32_t srcPhysicalDeviceIndex); - - const DispatchTable& m_VK; - VkCommandBuffer m_Handle = VK_NULL_HANDLE; - uint32_t m_PhysicalDeviceIndex = 0; - DeviceVK& m_Device; - CommandQueueType m_Type = (CommandQueueType)0; - VkPipelineBindPoint m_CurrentPipelineBindPoint = VK_PIPELINE_BIND_POINT_MAX_ENUM; - VkPipelineLayout m_CurrentPipelineLayoutHandle = VK_NULL_HANDLE; - const PipelineVK* m_CurrentPipeline = nullptr; - const PipelineLayoutVK* m_CurrentPipelineLayout = nullptr; - const FrameBufferVK* m_CurrentFrameBuffer = nullptr; - VkCommandPool m_CommandPool = VK_NULL_HANDLE; - }; - - inline CommandBufferVK::operator VkCommandBuffer() const - { - return m_Handle; - } - - inline DeviceVK& CommandBufferVK::GetDevice() const - { - return m_Device; - } + +struct DeviceVK; +struct PipelineVK; +struct PipelineLayoutVK; +struct TextureVK; +struct FrameBufferVK; + +struct CommandBufferVK +{ + inline CommandBufferVK(DeviceVK& device) : + m_Device(device) + {} + + inline operator VkCommandBuffer() const + { return m_Handle; } + + inline DeviceVK& GetDevice() const + { return m_Device; } + + ~CommandBufferVK(); + + void Create(VkCommandPool commandPool, VkCommandBuffer commandBuffer, CommandQueueType type); + Result Create(const CommandBufferVulkanDesc& commandBufferDesc); + + //================================================================================================================ + // NRI + //================================================================================================================ + + void SetDebugName(const char* name); + Result Begin(const DescriptorPool* descriptorPool, uint32_t physicalDeviceIndex); + Result End(); + + void SetPipeline(const Pipeline& pipeline); + void SetPipelineLayout(const PipelineLayout& pipelineLayout); + void SetDescriptorSet(uint32_t setIndexInPipelineLayout, const DescriptorSet& descriptorSet, const uint32_t* dynamicConstantBufferOffsets); + void SetConstants(uint32_t pushConstantIndex, const void* data, uint32_t size); + void SetDescriptorPool(const DescriptorPool& descriptorPool); + void PipelineBarrier(const TransitionBarrierDesc* transitionBarriers, const AliasingBarrierDesc* aliasingBarriers, BarrierDependency dependency); + + void BeginRenderPass(const FrameBuffer& frameBuffer, RenderPassBeginFlag renderPassBeginFlag); + void EndRenderPass(); + void SetViewports(const Viewport* viewports, uint32_t viewportNum); + void SetScissors(const Rect* rects, uint32_t rectNum); + void SetDepthBounds(float boundsMin, float boundsMax); + void SetStencilReference(uint8_t reference); + void SetSamplePositions(const SamplePosition* positions, uint32_t positionNum); + void ClearAttachments(const ClearDesc* clearDescs, uint32_t clearDescNum, const Rect* rects, uint32_t rectNum); + void SetIndexBuffer(const Buffer& buffer, uint64_t offset, IndexType indexType); + void SetVertexBuffers(uint32_t baseSlot, uint32_t bufferNum, const Buffer* const* buffers, const uint64_t* offsets); + + void Draw(uint32_t vertexNum, uint32_t instanceNum, uint32_t baseVertex, uint32_t baseInstance); + void DrawIndexed(uint32_t indexNum, uint32_t instanceNum, uint32_t baseIndex, uint32_t baseVertex, uint32_t baseInstance); + void DrawIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride); + void DrawIndexedIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride); + void Dispatch(uint32_t x, uint32_t y, uint32_t z); + void DispatchIndirect(const Buffer& buffer, uint64_t offset); + void BeginQuery(const QueryPool& queryPool, uint32_t offset); + void EndQuery(const QueryPool& queryPool, uint32_t offset); + void BeginAnnotation(const char* name); + void EndAnnotation(); + + void ClearStorageBuffer(const ClearStorageBufferDesc& clearDesc); + + void ClearStorageTexture(const ClearStorageTextureDesc& clearDesc); + + void CopyBuffer(Buffer& dstBuffer, uint32_t dstPhysicalDeviceIndex, uint64_t dstOffset, const Buffer& srcBuffer, + uint32_t srcPhysicalDeviceIndex, uint64_t srcOffset, uint64_t size); + + void CopyTexture(Texture& dstTexture, uint32_t dstPhysicalDeviceIndex, const TextureRegionDesc* dstRegionDesc, + const Texture& srcTexture, uint32_t srcPhysicalDeviceIndex, const TextureRegionDesc* srcRegionDesc); + + void UploadBufferToTexture(Texture& dstTexture, const TextureRegionDesc& dstRegionDesc, const Buffer& srcBuffer, + const TextureDataLayoutDesc& srcDataLayoutDesc); + + void ReadbackTextureToBuffer(Buffer& dstBuffer, TextureDataLayoutDesc& dstDataLayoutDesc, const Texture& srcTexture, + const TextureRegionDesc& srcRegionDesc); + + void CopyQueries(const QueryPool& queryPool, uint32_t offset, uint32_t num, Buffer& dstBuffer, uint64_t dstOffset); + void ResetQueries(const QueryPool& queryPool, uint32_t offset, uint32_t num); + + void BuildTopLevelAccelerationStructure(uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, + AccelerationStructureBuildBits flags, AccelerationStructure& dst, Buffer& scratch, uint64_t scratchOffset); + + void BuildBottomLevelAccelerationStructure(uint32_t geometryObjectNum, const GeometryObject* geometryObjects, + AccelerationStructureBuildBits flags, AccelerationStructure& dst, Buffer& scratch, uint64_t scratchOffset); + + void UpdateTopLevelAccelerationStructure(uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, + AccelerationStructureBuildBits flags, AccelerationStructure& dst, AccelerationStructure& src, Buffer& scratch, + uint64_t scratchOffset); + + void UpdateBottomLevelAccelerationStructure(uint32_t geometryObjectNum, const GeometryObject* geometryObjects, + AccelerationStructureBuildBits flags, AccelerationStructure& dst, AccelerationStructure& src, Buffer& scratch, + uint64_t scratchOffset); + + void CopyAccelerationStructure(AccelerationStructure& dst, AccelerationStructure& src, CopyMode copyMode); + + void WriteAccelerationStructureSize(const AccelerationStructure* const* accelerationStructures, + uint32_t accelerationStructureNum, QueryPool& queryPool, uint32_t queryPoolOffset); + + void DispatchRays(const DispatchRaysDesc& dispatchRaysDesc); + void DispatchMeshTasks(uint32_t x, uint32_t y, uint32_t z); + +private: + void FillAliasingBufferBarriers(const AliasingBarrierDesc& aliasing, Barriers& barriers) const; + void FillAliasingImageBarriers(const AliasingBarrierDesc& aliasing, Barriers& barriers) const; + void FillTransitionBufferBarriers(const TransitionBarrierDesc& transitions, Barriers& barriers) const; + void FillTransitionImageBarriers(const TransitionBarrierDesc& transitions, Barriers& barriers) const; + void CopyWholeTexture(const TextureVK& dstTexture, uint32_t dstPhysicalDeviceIndex, const TextureVK& srcTexture, uint32_t srcPhysicalDeviceIndex); + +private: + VkCommandBuffer m_Handle = VK_NULL_HANDLE; + uint32_t m_PhysicalDeviceIndex = 0; + DeviceVK& m_Device; + CommandQueueType m_Type = (CommandQueueType)0; + VkPipelineBindPoint m_CurrentPipelineBindPoint = VK_PIPELINE_BIND_POINT_MAX_ENUM; + VkPipelineLayout m_CurrentPipelineLayoutHandle = VK_NULL_HANDLE; + const PipelineVK* m_CurrentPipeline = nullptr; + const PipelineLayoutVK* m_CurrentPipelineLayout = nullptr; + const FrameBufferVK* m_CurrentFrameBuffer = nullptr; + VkCommandPool m_CommandPool = VK_NULL_HANDLE; +}; + } diff --git a/Source/VK/CommandBufferVK.hpp b/Source/VK/CommandBufferVK.hpp index ebf1c5bd..a906e3c6 100644 --- a/Source/VK/CommandBufferVK.hpp +++ b/Source/VK/CommandBufferVK.hpp @@ -8,9 +8,7 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma once - -#pragma region [ CoreInterface ] +#pragma region [ Core ] static void NRI_CALL SetCommandBufferDebugName(CommandBuffer& commandBuffer, const char* name) { @@ -228,61 +226,11 @@ static void* NRI_CALL GetCommandBufferNativeObject(const CommandBuffer& commandB return (VkCommandBuffer)((CommandBufferVK&)commandBuffer); } -void FillFunctionTableCommandBufferVK(CoreInterface& coreInterface) -{ - coreInterface.DestroyCommandBuffer = ::DestroyCommandBuffer; - - coreInterface.BeginCommandBuffer = ::BeginCommandBuffer; - coreInterface.EndCommandBuffer = ::EndCommandBuffer; - - coreInterface.CmdSetPipelineLayout = ::CmdSetPipelineLayout; - coreInterface.CmdSetPipeline = ::CmdSetPipeline; - coreInterface.CmdPipelineBarrier = ::CmdPipelineBarrier; - coreInterface.CmdSetDescriptorPool = ::CmdSetDescriptorPool; - coreInterface.CmdSetDescriptorSet = ::CmdSetDescriptorSet; - coreInterface.CmdSetConstants = ::CmdSetConstants; - - coreInterface.CmdBeginRenderPass = ::CmdBeginRenderPass; - coreInterface.CmdEndRenderPass = ::CmdEndRenderPass; - coreInterface.CmdSetViewports = ::CmdSetViewports; - coreInterface.CmdSetScissors = ::CmdSetScissors; - coreInterface.CmdSetDepthBounds = ::CmdSetDepthBounds; - coreInterface.CmdSetStencilReference = ::CmdSetStencilReference; - coreInterface.CmdSetSamplePositions = ::CmdSetSamplePositions; - coreInterface.CmdClearAttachments = ::CmdClearAttachments; - coreInterface.CmdSetIndexBuffer = ::CmdSetIndexBuffer; - coreInterface.CmdSetVertexBuffers = ::CmdSetVertexBuffers; - - coreInterface.CmdDraw = ::CmdDraw; - coreInterface.CmdDrawIndexed = ::CmdDrawIndexed; - coreInterface.CmdDrawIndirect = ::CmdDrawIndirect; - coreInterface.CmdDrawIndexedIndirect = ::CmdDrawIndexedIndirect; - coreInterface.CmdDispatch = ::CmdDispatch; - coreInterface.CmdDispatchIndirect = ::CmdDispatchIndirect; - coreInterface.CmdBeginQuery = ::CmdBeginQuery; - coreInterface.CmdEndQuery = ::CmdEndQuery; - coreInterface.CmdBeginAnnotation = ::CmdBeginAnnotation; - coreInterface.CmdEndAnnotation = ::CmdEndAnnotation; - - coreInterface.CmdClearStorageBuffer = ::CmdClearStorageBuffer; - coreInterface.CmdClearStorageTexture = ::CmdClearStorageTexture; - coreInterface.CmdCopyBuffer = ::CmdCopyBuffer; - coreInterface.CmdCopyTexture = ::CmdCopyTexture; - coreInterface.CmdUploadBufferToTexture = ::CmdUploadBufferToTexture; - coreInterface.CmdReadbackTextureToBuffer = ::CmdReadbackTextureToBuffer; - coreInterface.CmdCopyQueries = ::CmdCopyQueries; - coreInterface.CmdResetQueries = ::CmdResetQueries; - - coreInterface.SetCommandBufferDebugName = ::SetCommandBufferDebugName; - - coreInterface.GetCommandBufferNativeObject = ::GetCommandBufferNativeObject; -} - #pragma endregion -#pragma region [ RayTracingInterface ] +#pragma region [ RayTracing ] -void NRI_CALL CmdBuildTopLevelAccelerationStructure(CommandBuffer& commandBuffer, uint32_t instanceNum, const Buffer& buffer, +static void NRI_CALL CmdBuildTopLevelAccelerationStructure(CommandBuffer& commandBuffer, uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, AccelerationStructureBuildBits flags, AccelerationStructure& dst, Buffer& scratch, uint64_t scratchOffset) { @@ -290,7 +238,7 @@ void NRI_CALL CmdBuildTopLevelAccelerationStructure(CommandBuffer& commandBuffer scratch, scratchOffset); } -void NRI_CALL CmdBuildBottomLevelAccelerationStructure(CommandBuffer& commandBuffer, uint32_t geometryObjectNum, +static void NRI_CALL CmdBuildBottomLevelAccelerationStructure(CommandBuffer& commandBuffer, uint32_t geometryObjectNum, const GeometryObject* geometryObjects, AccelerationStructureBuildBits flags, AccelerationStructure& dst, Buffer& scratch, uint64_t scratchOffset) { @@ -298,7 +246,7 @@ void NRI_CALL CmdBuildBottomLevelAccelerationStructure(CommandBuffer& commandBuf scratch, scratchOffset); } -void NRI_CALL CmdUpdateTopLevelAccelerationStructure(CommandBuffer& commandBuffer, uint32_t instanceNum, const Buffer& buffer, +static void NRI_CALL CmdUpdateTopLevelAccelerationStructure(CommandBuffer& commandBuffer, uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, AccelerationStructureBuildBits flags, AccelerationStructure& dst, AccelerationStructure& src, Buffer& scratch, uint64_t scratchOffset) { @@ -306,7 +254,7 @@ void NRI_CALL CmdUpdateTopLevelAccelerationStructure(CommandBuffer& commandBuffe scratch, scratchOffset); } -void NRI_CALL CmdUpdateBottomLevelAccelerationStructure(CommandBuffer& commandBuffer, uint32_t geometryObjectNum, +static void NRI_CALL CmdUpdateBottomLevelAccelerationStructure(CommandBuffer& commandBuffer, uint32_t geometryObjectNum, const GeometryObject* geometryObjects, AccelerationStructureBuildBits flags, AccelerationStructure& dst, AccelerationStructure& src, Buffer& scratch, uint64_t scratchOffset) { @@ -314,46 +262,34 @@ void NRI_CALL CmdUpdateBottomLevelAccelerationStructure(CommandBuffer& commandBu src, scratch, scratchOffset); } -void NRI_CALL CmdCopyAccelerationStructure(CommandBuffer& commandBuffer, AccelerationStructure& dst, +static void NRI_CALL CmdCopyAccelerationStructure(CommandBuffer& commandBuffer, AccelerationStructure& dst, AccelerationStructure& src, CopyMode mode) { ((CommandBufferVK&)commandBuffer).CopyAccelerationStructure(dst, src, mode); } -void NRI_CALL CmdWriteAccelerationStructureSize(CommandBuffer& commandBuffer, +static void NRI_CALL CmdWriteAccelerationStructureSize(CommandBuffer& commandBuffer, const AccelerationStructure* const* accelerationStructures, uint32_t accelerationStructureNum, QueryPool& queryPool, uint32_t queryOffset) { ((CommandBufferVK&)commandBuffer).WriteAccelerationStructureSize(accelerationStructures, accelerationStructureNum, queryPool, queryOffset); } -void NRI_CALL CmdDispatchRays(CommandBuffer& commandBuffer, const DispatchRaysDesc& dispatchRaysDesc) +static void NRI_CALL CmdDispatchRays(CommandBuffer& commandBuffer, const DispatchRaysDesc& dispatchRaysDesc) { ((CommandBufferVK&)commandBuffer).DispatchRays(dispatchRaysDesc); } -void FillFunctionTableCommandBufferVK(RayTracingInterface& rayTracingInterface) -{ - rayTracingInterface.CmdBuildTopLevelAccelerationStructure = ::CmdBuildTopLevelAccelerationStructure; - rayTracingInterface.CmdBuildBottomLevelAccelerationStructure = ::CmdBuildBottomLevelAccelerationStructure; - rayTracingInterface.CmdUpdateTopLevelAccelerationStructure = ::CmdUpdateTopLevelAccelerationStructure; - rayTracingInterface.CmdUpdateBottomLevelAccelerationStructure = ::CmdUpdateBottomLevelAccelerationStructure; - rayTracingInterface.CmdCopyAccelerationStructure = ::CmdCopyAccelerationStructure; - rayTracingInterface.CmdWriteAccelerationStructureSize = ::CmdWriteAccelerationStructureSize; - rayTracingInterface.CmdDispatchRays = ::CmdDispatchRays; -} - #pragma endregion -#pragma region [ MeshShaderInterface ] +#pragma region [ MeshShader ] -static void NRI_CALL CmdDispatchMeshTasks(CommandBuffer& commandBuffer, uint32_t taskNum) +static void NRI_CALL CmdDispatchMeshTasks(CommandBuffer& commandBuffer, uint32_t x, uint32_t y, uint32_t z) { - ((CommandBufferVK&)commandBuffer).DispatchMeshTasks(taskNum); -} - -void FillFunctionTableCommandBufferVK(MeshShaderInterface& meshShaderInterface) -{ - meshShaderInterface.CmdDispatchMeshTasks = ::CmdDispatchMeshTasks; + ((CommandBufferVK&)commandBuffer).DispatchMeshTasks(x, y, z); } #pragma endregion + +Define_Core_CommandBuffer_PartiallyFillFunctionTable(VK) +Define_RayTracing_CommandBuffer_PartiallyFillFunctionTable(VK) +Define_MeshShader_CommandBuffer_PartiallyFillFunctionTable(VK) diff --git a/Source/VK/CommandQueueVK.cpp b/Source/VK/CommandQueueVK.cpp index 29aac31a..2fdadcdc 100644 --- a/Source/VK/CommandQueueVK.cpp +++ b/Source/VK/CommandQueueVK.cpp @@ -9,11 +9,8 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. */ #include "SharedVK.h" -#include "DeviceSemaphoreVK.h" -#include "QueueSemaphoreVK.h" #include "CommandBufferVK.h" #include "CommandQueueVK.h" -#include "DeviceVK.h" using namespace nri; @@ -26,101 +23,63 @@ Result CommandQueueVK::Create(const CommandQueueVulkanDesc& commandQueueDesc) return Result::SUCCESS; } +//================================================================================================================ +// NRI +//================================================================================================================ + inline void CommandQueueVK::SetDebugName(const char* name) { m_Device.SetDebugNameToTrivialObject(VK_OBJECT_TYPE_QUEUE, (uint64_t)m_Handle, name); } -inline void CommandQueueVK::Submit(const WorkSubmissionDesc& workSubmissionDesc, DeviceSemaphore* deviceSemaphore) +inline void CommandQueueVK::Submit(const QueueSubmitDesc& queueSubmitDesc) { - VkCommandBuffer* commandBuffers = STACK_ALLOC(VkCommandBuffer, workSubmissionDesc.commandBufferNum); - VkSemaphore* waitSemaphores = STACK_ALLOC(VkSemaphore, workSubmissionDesc.waitNum); - VkPipelineStageFlags* waitStages = STACK_ALLOC(VkPipelineStageFlags, workSubmissionDesc.waitNum); - VkSemaphore* signalSemaphores = STACK_ALLOC(VkSemaphore, workSubmissionDesc.signalNum); + VkCommandBuffer* commandBuffers = STACK_ALLOC(VkCommandBuffer, queueSubmitDesc.commandBufferNum); VkSubmitInfo submission = { VK_STRUCTURE_TYPE_SUBMIT_INFO, nullptr, - workSubmissionDesc.waitNum, - waitSemaphores, - waitStages, - workSubmissionDesc.commandBufferNum, + 0, + nullptr, + nullptr, + queueSubmitDesc.commandBufferNum, commandBuffers, - workSubmissionDesc.signalNum, - signalSemaphores + 0, + nullptr }; - for (uint32_t i = 0; i < workSubmissionDesc.commandBufferNum; i++) - *(commandBuffers++) = *(CommandBufferVK*)workSubmissionDesc.commandBuffers[i]; - - for (uint32_t i = 0; i < workSubmissionDesc.waitNum; i++) - { - *(waitSemaphores++) = *(QueueSemaphoreVK*)workSubmissionDesc.wait[i]; - *(waitStages++) = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT; // TODO: more optimal stage - } + for (uint32_t i = 0; i < queueSubmitDesc.commandBufferNum; i++) + *(commandBuffers++) = *(CommandBufferVK*)queueSubmitDesc.commandBuffers[i]; - for (uint32_t i = 0; i < workSubmissionDesc.signalNum; i++) - *(signalSemaphores++) = *(QueueSemaphoreVK*)workSubmissionDesc.signal[i]; + VkDeviceGroupSubmitInfo deviceGroupInfo = {}; + uint32_t* commandBufferDeviceMasks = STACK_ALLOC(uint32_t, queueSubmitDesc.commandBufferNum); - VkFence fence = VK_NULL_HANDLE; - if (deviceSemaphore != nullptr) - fence = *(DeviceSemaphoreVK*)deviceSemaphore; - - VkDeviceGroupSubmitInfo deviceGroupInfo; if (m_Device.GetPhysicalDeviceGroupSize() > 1) { - uint32_t* waitSemaphoreDeviceIndices = STACK_ALLOC(uint32_t, workSubmissionDesc.waitNum); - uint32_t* commandBufferDeviceMasks = STACK_ALLOC(uint32_t, workSubmissionDesc.commandBufferNum); - uint32_t* signalSemaphoreDeviceIndices = STACK_ALLOC(uint32_t, workSubmissionDesc.signalNum); - - for (uint32_t i = 0; i < workSubmissionDesc.waitNum; i++) - waitSemaphoreDeviceIndices[i] = workSubmissionDesc.physicalDeviceIndex; - - for (uint32_t i = 0; i < workSubmissionDesc.commandBufferNum; i++) - commandBufferDeviceMasks[i] = 1u << workSubmissionDesc.physicalDeviceIndex; - - for (uint32_t i = 0; i < workSubmissionDesc.signalNum; i++) - signalSemaphoreDeviceIndices[i] = workSubmissionDesc.physicalDeviceIndex; + for (uint32_t i = 0; i < queueSubmitDesc.commandBufferNum; i++) + commandBufferDeviceMasks[i] = 1u << queueSubmitDesc.physicalDeviceIndex; deviceGroupInfo = { VK_STRUCTURE_TYPE_DEVICE_GROUP_SUBMIT_INFO, nullptr, - workSubmissionDesc.waitNum, - waitSemaphoreDeviceIndices, - workSubmissionDesc.commandBufferNum, + 0, + nullptr, + queueSubmitDesc.commandBufferNum, commandBufferDeviceMasks, - workSubmissionDesc.signalNum, - signalSemaphoreDeviceIndices + 0, + nullptr }; submission.pNext = &deviceGroupInfo; } const auto& vk = m_Device.GetDispatchTable(); - const VkResult result = vk.QueueSubmit(m_Handle, 1, &submission, fence); + const VkResult result = vk.QueueSubmit(m_Handle, 1, &submission, VK_NULL_HANDLE); RETURN_ON_FAILURE(m_Device.GetLog(), result == VK_SUCCESS, ReturnVoid(), "Can't submit work to a command queue: vkQueueSubmit returned %d.", (int32_t)result); } -inline void CommandQueueVK::Wait(DeviceSemaphore& deviceSemaphore) -{ - const VkFence fence = *(DeviceSemaphoreVK*)&deviceSemaphore; - - constexpr uint64_t tenSeconds = 10000000000; - - const auto& vk = m_Device.GetDispatchTable(); - VkResult result = vk.WaitForFences(m_Device, 1, &fence, VK_TRUE, tenSeconds); - - RETURN_ON_FAILURE(m_Device.GetLog(), result == VK_SUCCESS, ReturnVoid(), - "Can't wait on a device semaphore: vkWaitForFences returned %d.", (int32_t)result); - - result = vk.ResetFences(m_Device, 1, &fence); - - RETURN_ON_FAILURE(m_Device.GetLog(), result == VK_SUCCESS, ReturnVoid(), - "Can't reset a device semaphore: vkResetFences returned %d.", (int32_t)result); -} - inline Result CommandQueueVK::ChangeResourceStates(const TransitionBarrierDesc& transitionBarriers) { HelperResourceStateChange resourceStateChange(m_Device.GetCoreInterface(), (Device&)m_Device, (CommandQueue&)*this); diff --git a/Source/VK/CommandQueueVK.h b/Source/VK/CommandQueueVK.h index 2f78914f..194749be 100644 --- a/Source/VK/CommandQueueVK.h +++ b/Source/VK/CommandQueueVK.h @@ -12,65 +12,52 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct DeviceVK; - struct CommandQueueVK - { - CommandQueueVK(DeviceVK& device); - CommandQueueVK(DeviceVK& device, VkQueue queue, uint32_t familyIndex, CommandQueueType type); +struct DeviceVK; - operator VkQueue() const; - DeviceVK& GetDevice() const; - Result Create(const CommandQueueVulkanDesc& commandQueueDesc); - uint32_t GetFamilyIndex() const; - CommandQueueType GetType() const; +struct CommandQueueVK +{ + inline CommandQueueVK(DeviceVK& device) : + m_Device(device) + {} + + inline CommandQueueVK(DeviceVK& device, VkQueue queue, uint32_t familyIndex, CommandQueueType type) : + m_Device(device) + , m_FamilyIndex(familyIndex) + , m_Type(type) + , m_Handle(queue) + {} - void SetDebugName(const char* name); - void Submit(const WorkSubmissionDesc& workSubmissionDesc, DeviceSemaphore* deviceSemaphore); - void Wait(DeviceSemaphore& deviceSemaphore); + inline operator VkQueue() const + { return m_Handle; } - Result ChangeResourceStates(const TransitionBarrierDesc& transitionBarriers); - Result UploadData(const TextureUploadDesc* textureUploadDescs, uint32_t textureUploadDescNum, - const BufferUploadDesc* bufferUploadDescs, uint32_t bufferUploadDescNum); - Result WaitForIdle(); + inline DeviceVK& GetDevice() const + { return m_Device; } - private: - VkQueue m_Handle = VK_NULL_HANDLE; - uint32_t m_FamilyIndex = (uint32_t)-1; - CommandQueueType m_Type = (CommandQueueType)-1; - DeviceVK& m_Device; - }; + inline uint32_t GetFamilyIndex() const + { return m_FamilyIndex; } - inline CommandQueueVK::CommandQueueVK(DeviceVK& device) : - m_Device(device) - { - } + inline CommandQueueType GetType() const + { return m_Type; } - inline CommandQueueVK::CommandQueueVK(DeviceVK& device, VkQueue queue, uint32_t familyIndex, CommandQueueType type) : - m_Handle(queue), - m_FamilyIndex(familyIndex), - m_Type(type), - m_Device(device) - { - } + Result Create(const CommandQueueVulkanDesc& commandQueueDesc); - inline CommandQueueVK::operator VkQueue() const - { - return m_Handle; - } + //================================================================================================================ + // NRI + //================================================================================================================ - inline DeviceVK& CommandQueueVK::GetDevice() const - { - return m_Device; - } + void SetDebugName(const char* name); + void Submit(const QueueSubmitDesc& queueSubmitDesc); + Result ChangeResourceStates(const TransitionBarrierDesc& transitionBarriers); + Result UploadData(const TextureUploadDesc* textureUploadDescs, uint32_t textureUploadDescNum, + const BufferUploadDesc* bufferUploadDescs, uint32_t bufferUploadDescNum); + Result WaitForIdle(); - inline uint32_t CommandQueueVK::GetFamilyIndex() const - { - return m_FamilyIndex; - } +private: + DeviceVK& m_Device; + VkQueue m_Handle = VK_NULL_HANDLE; + uint32_t m_FamilyIndex = (uint32_t)-1; + CommandQueueType m_Type = (CommandQueueType)-1; +}; - inline CommandQueueType CommandQueueVK::GetType() const - { - return m_Type; - } } diff --git a/Source/VK/CommandQueueVK.hpp b/Source/VK/CommandQueueVK.hpp index 15345c20..3003b39e 100644 --- a/Source/VK/CommandQueueVK.hpp +++ b/Source/VK/CommandQueueVK.hpp @@ -8,57 +8,39 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma once - -#pragma region [ CoreInterface ] +#pragma region [ Core ] static void NRI_CALL SetCommandQueueDebugName(CommandQueue& commandQueue, const char* name) { ((CommandQueueVK&)commandQueue).SetDebugName(name); } -static void NRI_CALL SubmitQueueWork(CommandQueue& commandQueue, const WorkSubmissionDesc& workSubmissionDesc, DeviceSemaphore* deviceSemaphore) -{ - ((CommandQueueVK&)commandQueue).Submit(workSubmissionDesc, deviceSemaphore); -} - -static void NRI_CALL WaitForSemaphore(CommandQueue& commandQueue, DeviceSemaphore& deviceSemaphore) +static void NRI_CALL QueueSubmit(CommandQueue& commandQueue, const QueueSubmitDesc& workSubmissionDesc) { - ((CommandQueueVK&)commandQueue).Wait(deviceSemaphore); -} - -void FillFunctionTableCommandQueueVK(CoreInterface& coreInterface) -{ - coreInterface.SetCommandQueueDebugName = ::SetCommandQueueDebugName; - coreInterface.SubmitQueueWork = ::SubmitQueueWork; - coreInterface.WaitForSemaphore = ::WaitForSemaphore; + ((CommandQueueVK&)commandQueue).Submit(workSubmissionDesc); } #pragma endregion -#pragma region [ HelperInterface ] +#pragma region [ Helper ] -static Result NRI_CALL ChangeResourceStatesVK(CommandQueue& commandQueue, const TransitionBarrierDesc& transitionBarriers) +static Result NRI_CALL ChangeResourceStates(CommandQueue& commandQueue, const TransitionBarrierDesc& transitionBarriers) { return ((CommandQueueVK&)commandQueue).ChangeResourceStates(transitionBarriers); } -static nri::Result NRI_CALL UploadDataVK(CommandQueue& commandQueue, const TextureUploadDesc* textureUploadDescs, uint32_t textureUploadDescNum, +static nri::Result NRI_CALL UploadData(CommandQueue& commandQueue, const TextureUploadDesc* textureUploadDescs, uint32_t textureUploadDescNum, const BufferUploadDesc* bufferUploadDescs, uint32_t bufferUploadDescNum) { return ((CommandQueueVK&)commandQueue).UploadData(textureUploadDescs, textureUploadDescNum, bufferUploadDescs, bufferUploadDescNum); } -static nri::Result NRI_CALL WaitForIdleVK(CommandQueue& commandQueue) +static nri::Result NRI_CALL WaitForIdle(CommandQueue& commandQueue) { return ((CommandQueueVK&)commandQueue).WaitForIdle(); } -void FillFunctionTableCommandQueueVK(HelperInterface& helperInterface) -{ - helperInterface.ChangeResourceStates = ::ChangeResourceStatesVK; - helperInterface.UploadData = ::UploadDataVK; - helperInterface.WaitForIdle = ::WaitForIdleVK; -} - #pragma endregion + +Define_Core_CommandQueue_PartiallyFillFunctionTable(VK) +Define_Helper_CommandQueue_PartiallyFillFunctionTable(VK) diff --git a/Source/VK/ConversionVK.h b/Source/VK/ConversionVK.h index c524dbdd..28b14aa4 100644 --- a/Source/VK/ConversionVK.h +++ b/Source/VK/ConversionVK.h @@ -12,867 +12,868 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - constexpr std::array INDEX_TYPE_TABLE = { - VK_INDEX_TYPE_UINT16, - VK_INDEX_TYPE_UINT32 - }; - - constexpr std::array LAYOUT_TABLE = { - VK_IMAGE_LAYOUT_GENERAL, // GENERAL, - VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // COLOR_ATTACHMENT, - VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, // DEPTH_STENCIL, - VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL, // DEPTH_STENCIL_READONLY, - VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL_KHR, // DEPTH_READONLY, - VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL_KHR, // STENCIL_READONLY, - VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, // SHADER_RESOURCE, - VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, // PRESENT, - VK_IMAGE_LAYOUT_UNDEFINED // UNKNOWN - }; - - constexpr VkBufferUsageFlags GetBufferUsageFlags(BufferUsageBits usageMask, uint32_t structureStride) + +constexpr std::array INDEX_TYPE_TABLE = { + VK_INDEX_TYPE_UINT16, + VK_INDEX_TYPE_UINT32 +}; + +constexpr std::array LAYOUT_TABLE = { + VK_IMAGE_LAYOUT_GENERAL, // GENERAL, + VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // COLOR_ATTACHMENT, + VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, // DEPTH_STENCIL, + VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL, // DEPTH_STENCIL_READONLY, + VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL_KHR, // DEPTH_READONLY, + VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL_KHR, // STENCIL_READONLY, + VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, // SHADER_RESOURCE, + VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, // PRESENT, + VK_IMAGE_LAYOUT_UNDEFINED // UNKNOWN +}; + +constexpr VkBufferUsageFlags GetBufferUsageFlags(BufferUsageBits usageMask, uint32_t structureStride) +{ + VkBufferUsageFlags flags = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | + VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT; + + if (usageMask & BufferUsageBits::VERTEX_BUFFER) + flags |= VK_BUFFER_USAGE_VERTEX_BUFFER_BIT; + + if (usageMask & BufferUsageBits::INDEX_BUFFER) + flags |= VK_BUFFER_USAGE_INDEX_BUFFER_BIT; + + if (usageMask & BufferUsageBits::CONSTANT_BUFFER) + flags |= VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT; + + if (usageMask & BufferUsageBits::ARGUMENT_BUFFER) + flags |= VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT; + + // TODO: add more usage bits + if (usageMask & BufferUsageBits::RAY_TRACING_BUFFER) { - VkBufferUsageFlags flags = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | - VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT; - - if (usageMask & BufferUsageBits::VERTEX_BUFFER) - flags |= VK_BUFFER_USAGE_VERTEX_BUFFER_BIT; - - if (usageMask & BufferUsageBits::INDEX_BUFFER) - flags |= VK_BUFFER_USAGE_INDEX_BUFFER_BIT; - - if (usageMask & BufferUsageBits::CONSTANT_BUFFER) - flags |= VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT; - - if (usageMask & BufferUsageBits::ARGUMENT_BUFFER) - flags |= VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT; - - // TODO: add more usage bits - if (usageMask & BufferUsageBits::RAY_TRACING_BUFFER) - { - flags |= VK_BUFFER_USAGE_SHADER_BINDING_TABLE_BIT_KHR | VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_STORAGE_BIT_KHR | - VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; - } - - if (usageMask & BufferUsageBits::ACCELERATION_STRUCTURE_BUILD_READ) - flags |= VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR; - - if (usageMask & BufferUsageBits::SHADER_RESOURCE) - { - if (structureStride == 0) - flags |= VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT; - else - flags |= VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; - } - - if (usageMask & BufferUsageBits::SHADER_RESOURCE_STORAGE) - { - if (structureStride == 0 && (usageMask & BufferUsageBits::RAY_TRACING_BUFFER) == 0) - flags |= VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT; - else - flags |= VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; - } - - return flags; + flags |= VK_BUFFER_USAGE_SHADER_BINDING_TABLE_BIT_KHR | VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_STORAGE_BIT_KHR | + VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; } - constexpr VkIndexType GetIndexType(IndexType indexType) + if (usageMask & BufferUsageBits::ACCELERATION_STRUCTURE_BUILD_READ) + flags |= VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR; + + if (usageMask & BufferUsageBits::SHADER_RESOURCE) { - return INDEX_TYPE_TABLE[(uint32_t)indexType]; + if (structureStride == 0) + flags |= VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT; + else + flags |= VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; } - constexpr VkImageLayout GetImageLayout(TextureLayout layout) + if (usageMask & BufferUsageBits::SHADER_RESOURCE_STORAGE) { - return LAYOUT_TABLE[(uint32_t)layout]; + if (structureStride == 0 && (usageMask & BufferUsageBits::RAY_TRACING_BUFFER) == 0) + flags |= VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT; + else + flags |= VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; } - constexpr VkAccessFlags GetAccessFlags(AccessBits mask) - { - VkAccessFlags flags = 0; + return flags; +} - if (mask & AccessBits::VERTEX_BUFFER) - flags |= VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT; +constexpr VkIndexType GetIndexType(IndexType indexType) +{ + return INDEX_TYPE_TABLE[(uint32_t)indexType]; +} - if (mask & AccessBits::INDEX_BUFFER) - flags |= VK_ACCESS_INDEX_READ_BIT; +constexpr VkImageLayout GetImageLayout(TextureLayout layout) +{ + return LAYOUT_TABLE[(uint32_t)layout]; +} - if (mask & AccessBits::CONSTANT_BUFFER) - flags |= VK_ACCESS_UNIFORM_READ_BIT; +constexpr VkAccessFlags GetAccessFlags(AccessBits mask) +{ + VkAccessFlags flags = 0; - if (mask & AccessBits::ARGUMENT_BUFFER) - flags |= VK_ACCESS_INDIRECT_COMMAND_READ_BIT; + if (mask & AccessBits::VERTEX_BUFFER) + flags |= VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT; - if (mask & AccessBits::SHADER_RESOURCE) - flags |= VK_ACCESS_SHADER_READ_BIT; + if (mask & AccessBits::INDEX_BUFFER) + flags |= VK_ACCESS_INDEX_READ_BIT; - if (mask & AccessBits::SHADER_RESOURCE_STORAGE) - flags |= VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT; + if (mask & AccessBits::CONSTANT_BUFFER) + flags |= VK_ACCESS_UNIFORM_READ_BIT; - if (mask & AccessBits::COLOR_ATTACHMENT) - flags |= VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; // TODO: add READ bit too? + if (mask & AccessBits::ARGUMENT_BUFFER) + flags |= VK_ACCESS_INDIRECT_COMMAND_READ_BIT; - if (mask & AccessBits::DEPTH_STENCIL_WRITE) - flags |= VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; + if (mask & AccessBits::SHADER_RESOURCE) + flags |= VK_ACCESS_SHADER_READ_BIT; - if (mask & AccessBits::DEPTH_STENCIL_READ) - flags |= VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT; + if (mask & AccessBits::SHADER_RESOURCE_STORAGE) + flags |= VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT; - if (mask & AccessBits::COPY_SOURCE) - flags |= VK_ACCESS_TRANSFER_READ_BIT; + if (mask & AccessBits::COLOR_ATTACHMENT) + flags |= VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; // TODO: add READ bit too? - if (mask & AccessBits::COPY_DESTINATION) - flags |= VK_ACCESS_TRANSFER_WRITE_BIT; + if (mask & AccessBits::DEPTH_STENCIL_WRITE) + flags |= VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; - if (mask & AccessBits::ACCELERATION_STRUCTURE_READ) - flags |= VK_ACCESS_ACCELERATION_STRUCTURE_READ_BIT_KHR; + if (mask & AccessBits::DEPTH_STENCIL_READ) + flags |= VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT; - if (mask & AccessBits::ACCELERATION_STRUCTURE_WRITE) - flags |= VK_ACCESS_ACCELERATION_STRUCTURE_WRITE_BIT_KHR; + if (mask & AccessBits::COPY_SOURCE) + flags |= VK_ACCESS_TRANSFER_READ_BIT; - return flags; - } + if (mask & AccessBits::COPY_DESTINATION) + flags |= VK_ACCESS_TRANSFER_WRITE_BIT; - constexpr std::array DESCRIPTOR_TYPES = - { - VK_DESCRIPTOR_TYPE_SAMPLER, // SAMPLER - VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, // CONSTANT_BUFFER - VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, // TEXTURE - VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, // STORAGE_TEXTURE - VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, // BUFFER - VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, // STORAGE_BUFFER - VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, // STRUCTURED_BUFFER - VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, // STORAGE_STRUCTURED_BUFFER - VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR, // ACCELERATION_STRUCTURE - }; - - constexpr VkDescriptorType GetDescriptorType(DescriptorType type) - { - return DESCRIPTOR_TYPES[(uint32_t)type]; - } + if (mask & AccessBits::ACCELERATION_STRUCTURE_READ) + flags |= VK_ACCESS_ACCELERATION_STRUCTURE_READ_BIT_KHR; - constexpr std::array SHADER_STAGE_TABLE = { - VK_SHADER_STAGE_ALL, // ALL, - VK_SHADER_STAGE_VERTEX_BIT, // VERTEX, - VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT, // TESS_CONTROL, - VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT, // TESS_EVALUATION, - VK_SHADER_STAGE_GEOMETRY_BIT, // GEOMETRY, - VK_SHADER_STAGE_FRAGMENT_BIT, // FRAGMENT, - VK_SHADER_STAGE_COMPUTE_BIT, // COMPUTE, - VK_SHADER_STAGE_RAYGEN_BIT_KHR, // RAYGEN, - VK_SHADER_STAGE_MISS_BIT_KHR, // MISS, - VK_SHADER_STAGE_INTERSECTION_BIT_KHR, // INTERSECTION, - VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR, // CLOSEST_HIT, - VK_SHADER_STAGE_ANY_HIT_BIT_KHR, // ANY_HIT - VK_SHADER_STAGE_CALLABLE_BIT_KHR, // CALLABLE - VK_SHADER_STAGE_TASK_BIT_NV, // MESH_CONTROL - VK_SHADER_STAGE_MESH_BIT_NV // MESH_EVALUATION - }; - - constexpr VkShaderStageFlags GetShaderStageFlags(ShaderStage stage) - { - return SHADER_STAGE_TABLE[(uint32_t)stage]; - } + if (mask & AccessBits::ACCELERATION_STRUCTURE_WRITE) + flags |= VK_ACCESS_ACCELERATION_STRUCTURE_WRITE_BIT_KHR; - constexpr std::array VK_IMAGE_VIEW_FORMAT = { - VK_FORMAT_UNDEFINED, // UNKNOWN - - VK_FORMAT_R8_UNORM, // R8_UNORM - VK_FORMAT_R8_SNORM, // R8_SNORM - VK_FORMAT_R8_UINT, // R8_UINT - VK_FORMAT_R8_SINT, // R8_SINT - - VK_FORMAT_R8G8_UNORM, // RG8_UNORM - VK_FORMAT_R8G8_SNORM, // RG8_SNORM - VK_FORMAT_R8G8_UINT, // RG8_UINT - VK_FORMAT_R8G8_SINT, // RG8_SINT - - VK_FORMAT_B8G8R8A8_UNORM, // BGRA8_UNORM - VK_FORMAT_B8G8R8A8_SRGB, // BGRA8_SRGB - - VK_FORMAT_R8G8B8A8_UNORM, // RGBA8_UNORM - VK_FORMAT_R8G8B8A8_SNORM, // RGBA8_SNORM - VK_FORMAT_R8G8B8A8_UINT, // RGBA8_UINT - VK_FORMAT_R8G8B8A8_SINT, // RGBA8_SINT - VK_FORMAT_R8G8B8A8_SRGB, // RGBA8_SRGB - - VK_FORMAT_R16_UNORM, // R16_UNORM - VK_FORMAT_R16_SNORM, // R16_SNORM - VK_FORMAT_R16_UINT, // R16_UINT - VK_FORMAT_R16_SINT, // R16_SINT - VK_FORMAT_R16_SFLOAT, // R16_SFLOAT - - VK_FORMAT_R16G16_UNORM, // RG16_UNORM - VK_FORMAT_R16G16_SNORM, // RG16_SNORM - VK_FORMAT_R16G16_UINT, // RG16_UINT - VK_FORMAT_R16G16_SINT, // RG16_SINT - VK_FORMAT_R16G16_SFLOAT, // RG16_SFLOAT - - VK_FORMAT_R16G16B16A16_UNORM, // RGBA16_UNORM - VK_FORMAT_R16G16B16A16_SNORM, // RGBA16_SNORM - VK_FORMAT_R16G16B16A16_UINT, // RGBA16_UINT - VK_FORMAT_R16G16B16A16_SINT, // RGBA16_SINT - VK_FORMAT_R16G16B16A16_SFLOAT, // RGBA16_SFLOAT - - VK_FORMAT_R32_UINT, // R32_UINT - VK_FORMAT_R32_SINT, // R32_SINT - VK_FORMAT_R32_SFLOAT, // R32_SFLOAT - - VK_FORMAT_R32G32_UINT, // RG32_UINT - VK_FORMAT_R32G32_SINT, // RG32_SINT - VK_FORMAT_R32G32_SFLOAT, // RG32_SFLOAT - - VK_FORMAT_R32G32B32_UINT, // RGB32_UINT - VK_FORMAT_R32G32B32_SINT, // RGB32_SINT - VK_FORMAT_R32G32B32_SFLOAT, // RGB32_SFLOAT - - VK_FORMAT_R32G32B32A32_UINT, // RGB32_UINT - VK_FORMAT_R32G32B32A32_SINT, // RGB32_SINT - VK_FORMAT_R32G32B32A32_SFLOAT, // RGB32_SFLOAT - - VK_FORMAT_A2B10G10R10_UNORM_PACK32, // R10_G10_B10_A2_UNORM - VK_FORMAT_A2B10G10R10_UINT_PACK32, // R10_G10_B10_A2_UINT - VK_FORMAT_B10G11R11_UFLOAT_PACK32, // R11_G11_B10_UFLOAT - VK_FORMAT_E5B9G9R9_UFLOAT_PACK32, // R9_G9_B9_E5_UFLOAT - - VK_FORMAT_BC1_RGBA_UNORM_BLOCK, // BC1_RGBA_UNORM - VK_FORMAT_BC1_RGBA_SRGB_BLOCK, // BC1_RGBA_SRGB - VK_FORMAT_BC2_UNORM_BLOCK, // BC2_RGBA_UNORM - VK_FORMAT_BC2_SRGB_BLOCK, // BC2_RGBA_SRGB - VK_FORMAT_BC3_UNORM_BLOCK, // BC3_RGBA_UNORM - VK_FORMAT_BC3_SRGB_BLOCK, // BC3_RGBA_SRGB - VK_FORMAT_BC4_UNORM_BLOCK, // BC4_R_UNORM - VK_FORMAT_BC4_SNORM_BLOCK, // BC4_R_SNORM - VK_FORMAT_BC5_UNORM_BLOCK, // BC5_RG_UNORM - VK_FORMAT_BC5_SNORM_BLOCK, // BC5_RG_SNORM - VK_FORMAT_BC6H_UFLOAT_BLOCK, // BC6H_RGB_UFLOAT - VK_FORMAT_BC6H_SFLOAT_BLOCK, // BC6H_RGB_SFLOAT - VK_FORMAT_BC7_UNORM_BLOCK, // BC7_RGBA_UNORM - VK_FORMAT_BC7_SRGB_BLOCK, // BC7_RGBA_SRGB - - // Depth-specific - - VK_FORMAT_D16_UNORM, // D16_UNORM - VK_FORMAT_D24_UNORM_S8_UINT, // D24_UNORM_S8_UINT - VK_FORMAT_D32_SFLOAT, // D32_SFLOAT - VK_FORMAT_D32_SFLOAT_S8_UINT, // D32_SFLOAT_S8_UINT_X24_TYPELESS - - VK_FORMAT_X8_D24_UNORM_PACK32, // D24_UNORM_X8_TYPELESS - VK_FORMAT_D24_UNORM_S8_UINT, // X24_TYPLESS_S8_UINT /// TODO: there is no such format in VK - VK_FORMAT_D32_SFLOAT_S8_UINT, // X32_TYPLESS_S8_UINT_X24_TYPELESS - VK_FORMAT_D32_SFLOAT_S8_UINT // D32_SFLOAT_X8_TYPLESS_X24_TYPELESS - }; - - constexpr std::array IMAGE_CREATE_FLAGS = { - 0, // UNKNOWN, - - 0, // R8_UNORM, - 0, // R8_SNORM, - 0, // R8_UINT, - 0, // R8_SINT, - - 0, // RG8_UNORM, - 0, // RG8_SNORM, - 0, // RG8_UINT, - 0, // RG8_SINT, - - 0, // BGRA8_UNORM, - VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT, // BGRA8_SRGB, - - 0, // RGBA8_UNORM, - 0, // RGBA8_SNORM, - 0, // RGBA8_UINT, - 0, // RGBA8_SINT, - VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT, // RGBA8_SRGB, - - 0, // R16_UNORM, - 0, // R16_SNORM, - 0, // R16_UINT, - 0, // R16_SINT, - 0, // R16_SFLOAT, - - 0, // RG16_UNORM, - 0, // RG16_SNORM, - 0, // RG16_UINT, - 0, // RG16_SINT, - 0, // RG16_SFLOAT, - - 0, // RGBA16_UNORM, - 0, // RGBA16_SNORM, - 0, // RGBA16_UINT, - 0, // RGBA16_SINT, - 0, // RGBA16_SFLOAT, - - 0, // R32_UINT, - 0, // R32_SINT, - 0, // R32_SFLOAT, - - 0, // RG32_UINT, - 0, // RG32_SINT, - 0, // RG32_SFLOAT, - - 0, // RGB32_UINT, - 0, // RGB32_SINT, - 0, // RGB32_SFLOAT, - - 0, // RGBA32_UINT, - 0, // RGBA32_SINT, - 0, // RGBA32_SFLOAT, - - 0, // R10_G10_B10_A2_UNORM, - 0, // R10_G10_B10_A2_UINT, - 0, // R11_G11_B10_UFLOAT, - 0, // R9_G9_B9_E5_UFLOAT, - - 0, // BC1_RGBA_UNORM, - 0, // BC1_RGBA_SRGB, - 0, // BC2_RGBA_UNORM, - 0, // BC2_RGBA_SRGB, - 0, // BC3_RGBA_UNORM, - 0, // BC3_RGBA_SRGB, - 0, // BC4_R_UNORM, - 0, // BC4_R_SNORM, - 0, // BC5_RG_UNORM, - 0, // BC5_RG_SNORM, - 0, // BC6H_RGB_UFLOAT, - 0, // BC6H_RGB_SFLOAT, - 0, // BC7_RGBA_UNORM, - 0, // BC7_RGBA_SRGB, - - 0, // D16_UNORM, - 0, // D24_UNORM_S8_UINT, - 0, // D32_SFLOAT, - 0, // D32_SFLOAT_S8_UINT_X24, - - 0, // R24_UNORM_X8, - 0, // X24_R8_UINT, - 0, // X32_R8_UINT_X24, - 0 // R32_SFLOAT_X8_X24, - }; - - constexpr VkImageCreateFlags GetImageCreateFlags(Format format) - { - return IMAGE_CREATE_FLAGS[(size_t)format]; - } + return flags; +} - inline VkFormat GetVkFormat(Format format) - { - return (VkFormat)NRIFormatToVKFormat(format); - } +constexpr std::array DESCRIPTOR_TYPES = +{ + VK_DESCRIPTOR_TYPE_SAMPLER, // SAMPLER + VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, // CONSTANT_BUFFER + VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, // TEXTURE + VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, // STORAGE_TEXTURE + VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, // BUFFER + VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, // STORAGE_BUFFER + VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, // STRUCTURED_BUFFER + VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, // STORAGE_STRUCTURED_BUFFER + VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR, // ACCELERATION_STRUCTURE +}; + +constexpr VkDescriptorType GetDescriptorType(DescriptorType type) +{ + return DESCRIPTOR_TYPES[(uint32_t)type]; +} + +constexpr std::array SHADER_STAGE_TABLE = { + VK_SHADER_STAGE_ALL, // ALL, + VK_SHADER_STAGE_VERTEX_BIT, // VERTEX, + VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT, // TESS_CONTROL, + VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT, // TESS_EVALUATION, + VK_SHADER_STAGE_GEOMETRY_BIT, // GEOMETRY, + VK_SHADER_STAGE_FRAGMENT_BIT, // FRAGMENT, + VK_SHADER_STAGE_COMPUTE_BIT, // COMPUTE, + VK_SHADER_STAGE_RAYGEN_BIT_KHR, // RAYGEN, + VK_SHADER_STAGE_MISS_BIT_KHR, // MISS, + VK_SHADER_STAGE_INTERSECTION_BIT_KHR, // INTERSECTION, + VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR, // CLOSEST_HIT, + VK_SHADER_STAGE_ANY_HIT_BIT_KHR, // ANY_HIT + VK_SHADER_STAGE_CALLABLE_BIT_KHR, // CALLABLE + VK_SHADER_STAGE_TASK_BIT_EXT, // MESH_CONTROL + VK_SHADER_STAGE_MESH_BIT_EXT // MESH_EVALUATION +}; + +constexpr VkShaderStageFlags GetShaderStageFlags(ShaderStage stage) +{ + return SHADER_STAGE_TABLE[(uint32_t)stage]; +} + +constexpr std::array VK_IMAGE_VIEW_FORMAT = { + VK_FORMAT_UNDEFINED, // UNKNOWN + + VK_FORMAT_R8_UNORM, // R8_UNORM + VK_FORMAT_R8_SNORM, // R8_SNORM + VK_FORMAT_R8_UINT, // R8_UINT + VK_FORMAT_R8_SINT, // R8_SINT + + VK_FORMAT_R8G8_UNORM, // RG8_UNORM + VK_FORMAT_R8G8_SNORM, // RG8_SNORM + VK_FORMAT_R8G8_UINT, // RG8_UINT + VK_FORMAT_R8G8_SINT, // RG8_SINT + + VK_FORMAT_B8G8R8A8_UNORM, // BGRA8_UNORM + VK_FORMAT_B8G8R8A8_SRGB, // BGRA8_SRGB + + VK_FORMAT_R8G8B8A8_UNORM, // RGBA8_UNORM + VK_FORMAT_R8G8B8A8_SNORM, // RGBA8_SNORM + VK_FORMAT_R8G8B8A8_UINT, // RGBA8_UINT + VK_FORMAT_R8G8B8A8_SINT, // RGBA8_SINT + VK_FORMAT_R8G8B8A8_SRGB, // RGBA8_SRGB + + VK_FORMAT_R16_UNORM, // R16_UNORM + VK_FORMAT_R16_SNORM, // R16_SNORM + VK_FORMAT_R16_UINT, // R16_UINT + VK_FORMAT_R16_SINT, // R16_SINT + VK_FORMAT_R16_SFLOAT, // R16_SFLOAT + + VK_FORMAT_R16G16_UNORM, // RG16_UNORM + VK_FORMAT_R16G16_SNORM, // RG16_SNORM + VK_FORMAT_R16G16_UINT, // RG16_UINT + VK_FORMAT_R16G16_SINT, // RG16_SINT + VK_FORMAT_R16G16_SFLOAT, // RG16_SFLOAT + + VK_FORMAT_R16G16B16A16_UNORM, // RGBA16_UNORM + VK_FORMAT_R16G16B16A16_SNORM, // RGBA16_SNORM + VK_FORMAT_R16G16B16A16_UINT, // RGBA16_UINT + VK_FORMAT_R16G16B16A16_SINT, // RGBA16_SINT + VK_FORMAT_R16G16B16A16_SFLOAT, // RGBA16_SFLOAT + + VK_FORMAT_R32_UINT, // R32_UINT + VK_FORMAT_R32_SINT, // R32_SINT + VK_FORMAT_R32_SFLOAT, // R32_SFLOAT + + VK_FORMAT_R32G32_UINT, // RG32_UINT + VK_FORMAT_R32G32_SINT, // RG32_SINT + VK_FORMAT_R32G32_SFLOAT, // RG32_SFLOAT + + VK_FORMAT_R32G32B32_UINT, // RGB32_UINT + VK_FORMAT_R32G32B32_SINT, // RGB32_SINT + VK_FORMAT_R32G32B32_SFLOAT, // RGB32_SFLOAT + + VK_FORMAT_R32G32B32A32_UINT, // RGB32_UINT + VK_FORMAT_R32G32B32A32_SINT, // RGB32_SINT + VK_FORMAT_R32G32B32A32_SFLOAT, // RGB32_SFLOAT + + VK_FORMAT_A2B10G10R10_UNORM_PACK32, // R10_G10_B10_A2_UNORM + VK_FORMAT_A2B10G10R10_UINT_PACK32, // R10_G10_B10_A2_UINT + VK_FORMAT_B10G11R11_UFLOAT_PACK32, // R11_G11_B10_UFLOAT + VK_FORMAT_E5B9G9R9_UFLOAT_PACK32, // R9_G9_B9_E5_UFLOAT + + VK_FORMAT_BC1_RGBA_UNORM_BLOCK, // BC1_RGBA_UNORM + VK_FORMAT_BC1_RGBA_SRGB_BLOCK, // BC1_RGBA_SRGB + VK_FORMAT_BC2_UNORM_BLOCK, // BC2_RGBA_UNORM + VK_FORMAT_BC2_SRGB_BLOCK, // BC2_RGBA_SRGB + VK_FORMAT_BC3_UNORM_BLOCK, // BC3_RGBA_UNORM + VK_FORMAT_BC3_SRGB_BLOCK, // BC3_RGBA_SRGB + VK_FORMAT_BC4_UNORM_BLOCK, // BC4_R_UNORM + VK_FORMAT_BC4_SNORM_BLOCK, // BC4_R_SNORM + VK_FORMAT_BC5_UNORM_BLOCK, // BC5_RG_UNORM + VK_FORMAT_BC5_SNORM_BLOCK, // BC5_RG_SNORM + VK_FORMAT_BC6H_UFLOAT_BLOCK, // BC6H_RGB_UFLOAT + VK_FORMAT_BC6H_SFLOAT_BLOCK, // BC6H_RGB_SFLOAT + VK_FORMAT_BC7_UNORM_BLOCK, // BC7_RGBA_UNORM + VK_FORMAT_BC7_SRGB_BLOCK, // BC7_RGBA_SRGB + + // Depth-specific + + VK_FORMAT_D16_UNORM, // D16_UNORM + VK_FORMAT_D24_UNORM_S8_UINT, // D24_UNORM_S8_UINT + VK_FORMAT_D32_SFLOAT, // D32_SFLOAT + VK_FORMAT_D32_SFLOAT_S8_UINT, // D32_SFLOAT_S8_UINT_X24_TYPELESS + + VK_FORMAT_X8_D24_UNORM_PACK32, // D24_UNORM_X8_TYPELESS + VK_FORMAT_D24_UNORM_S8_UINT, // X24_TYPLESS_S8_UINT /// TODO: there is no such format in VK + VK_FORMAT_D32_SFLOAT_S8_UINT, // X32_TYPLESS_S8_UINT_X24_TYPELESS + VK_FORMAT_D32_SFLOAT_S8_UINT // D32_SFLOAT_X8_TYPLESS_X24_TYPELESS +}; + +constexpr std::array IMAGE_CREATE_FLAGS = { + 0, // UNKNOWN, + + 0, // R8_UNORM, + 0, // R8_SNORM, + 0, // R8_UINT, + 0, // R8_SINT, + + 0, // RG8_UNORM, + 0, // RG8_SNORM, + 0, // RG8_UINT, + 0, // RG8_SINT, + + 0, // BGRA8_UNORM, + VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT, // BGRA8_SRGB, + + 0, // RGBA8_UNORM, + 0, // RGBA8_SNORM, + 0, // RGBA8_UINT, + 0, // RGBA8_SINT, + VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT, // RGBA8_SRGB, + + 0, // R16_UNORM, + 0, // R16_SNORM, + 0, // R16_UINT, + 0, // R16_SINT, + 0, // R16_SFLOAT, + + 0, // RG16_UNORM, + 0, // RG16_SNORM, + 0, // RG16_UINT, + 0, // RG16_SINT, + 0, // RG16_SFLOAT, + + 0, // RGBA16_UNORM, + 0, // RGBA16_SNORM, + 0, // RGBA16_UINT, + 0, // RGBA16_SINT, + 0, // RGBA16_SFLOAT, + + 0, // R32_UINT, + 0, // R32_SINT, + 0, // R32_SFLOAT, + + 0, // RG32_UINT, + 0, // RG32_SINT, + 0, // RG32_SFLOAT, + + 0, // RGB32_UINT, + 0, // RGB32_SINT, + 0, // RGB32_SFLOAT, + + 0, // RGBA32_UINT, + 0, // RGBA32_SINT, + 0, // RGBA32_SFLOAT, + + 0, // R10_G10_B10_A2_UNORM, + 0, // R10_G10_B10_A2_UINT, + 0, // R11_G11_B10_UFLOAT, + 0, // R9_G9_B9_E5_UFLOAT, + + 0, // BC1_RGBA_UNORM, + 0, // BC1_RGBA_SRGB, + 0, // BC2_RGBA_UNORM, + 0, // BC2_RGBA_SRGB, + 0, // BC3_RGBA_UNORM, + 0, // BC3_RGBA_SRGB, + 0, // BC4_R_UNORM, + 0, // BC4_R_SNORM, + 0, // BC5_RG_UNORM, + 0, // BC5_RG_SNORM, + 0, // BC6H_RGB_UFLOAT, + 0, // BC6H_RGB_SFLOAT, + 0, // BC7_RGBA_UNORM, + 0, // BC7_RGBA_SRGB, + + 0, // D16_UNORM, + 0, // D24_UNORM_S8_UINT, + 0, // D32_SFLOAT, + 0, // D32_SFLOAT_S8_UINT_X24, + + 0, // R24_UNORM_X8, + 0, // X24_R8_UINT, + 0, // X32_R8_UINT_X24, + 0 // R32_SFLOAT_X8_X24, +}; + +constexpr VkImageCreateFlags GetImageCreateFlags(Format format) +{ + return IMAGE_CREATE_FLAGS[(size_t)format]; +} - constexpr VkFormat GetVkImageViewFormat(Format format) - { - return VK_IMAGE_VIEW_FORMAT[(uint32_t)format]; - } +inline VkFormat GetVkFormat(Format format) +{ + return (VkFormat)NRIFormatToVKFormat(format); +} - constexpr std::array TOPOLOGIES = { - VK_PRIMITIVE_TOPOLOGY_POINT_LIST, // POINT_LIST - VK_PRIMITIVE_TOPOLOGY_LINE_LIST, // LINE_LIST - VK_PRIMITIVE_TOPOLOGY_LINE_STRIP, // LINE_STRIP - VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, // TRIANGLE_LIST - VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP, // TRIANGLE_STRIP - VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY, // LINE_LIST_WITH_ADJACENCY - VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY, // LINE_STRIP_WITH_ADJACENCY - VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY, // TRIANGLE_LIST_WITH_ADJACENCY - VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY, // TRIANGLE_STRIP_WITH_ADJACENCY - VK_PRIMITIVE_TOPOLOGY_PATCH_LIST // PATCH_LIST - }; - - constexpr VkPrimitiveTopology GetTopology(Topology topology) - { - return TOPOLOGIES[(uint32_t)topology]; - } +constexpr VkFormat GetVkImageViewFormat(Format format) +{ + return VK_IMAGE_VIEW_FORMAT[(uint32_t)format]; +} + +constexpr std::array TOPOLOGIES = { + VK_PRIMITIVE_TOPOLOGY_POINT_LIST, // POINT_LIST + VK_PRIMITIVE_TOPOLOGY_LINE_LIST, // LINE_LIST + VK_PRIMITIVE_TOPOLOGY_LINE_STRIP, // LINE_STRIP + VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, // TRIANGLE_LIST + VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP, // TRIANGLE_STRIP + VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY, // LINE_LIST_WITH_ADJACENCY + VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY, // LINE_STRIP_WITH_ADJACENCY + VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY, // TRIANGLE_LIST_WITH_ADJACENCY + VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY, // TRIANGLE_STRIP_WITH_ADJACENCY + VK_PRIMITIVE_TOPOLOGY_PATCH_LIST // PATCH_LIST +}; + +constexpr VkPrimitiveTopology GetTopology(Topology topology) +{ + return TOPOLOGIES[(uint32_t)topology]; +} - constexpr std::array CULL_MODES = { - VK_CULL_MODE_NONE, // NONE - VK_CULL_MODE_FRONT_BIT, // FRONT - VK_CULL_MODE_BACK_BIT // BACK - }; +constexpr std::array CULL_MODES = { + VK_CULL_MODE_NONE, // NONE + VK_CULL_MODE_FRONT_BIT, // FRONT + VK_CULL_MODE_BACK_BIT // BACK +}; - constexpr VkCullModeFlags GetCullMode(CullMode cullMode) - { - return CULL_MODES[(uint32_t)cullMode]; - } +constexpr VkCullModeFlags GetCullMode(CullMode cullMode) +{ + return CULL_MODES[(uint32_t)cullMode]; +} - constexpr std::array POLYGON_MODES = { - VK_POLYGON_MODE_FILL, // SOLID - VK_POLYGON_MODE_LINE, // WIREFRAME - }; +constexpr std::array POLYGON_MODES = { + VK_POLYGON_MODE_FILL, // SOLID + VK_POLYGON_MODE_LINE, // WIREFRAME +}; - constexpr VkPolygonMode GetPolygonMode(FillMode fillMode) - { - return POLYGON_MODES[(uint32_t)fillMode]; - } +constexpr VkPolygonMode GetPolygonMode(FillMode fillMode) +{ + return POLYGON_MODES[(uint32_t)fillMode]; +} - constexpr float GetDepthOffset(int32_t) - { - return 0.0f; // TODO: implement correct conversion - } +constexpr float GetDepthOffset(int32_t) +{ + return 0.0f; // TODO: implement correct conversion +} - constexpr VkSampleCountFlagBits GetSampleCount(uint32_t sampleNum) - { - return (VkSampleCountFlagBits)sampleNum; - } +constexpr VkSampleCountFlagBits GetSampleCount(uint32_t sampleNum) +{ + return (VkSampleCountFlagBits)sampleNum; +} + +constexpr std::array COMPARE_OP = { + VK_COMPARE_OP_NEVER, // NONE + VK_COMPARE_OP_ALWAYS, // ALWAYS + VK_COMPARE_OP_NEVER, // NEVER + VK_COMPARE_OP_LESS, // LESS + VK_COMPARE_OP_LESS_OR_EQUAL, // LESS_EQUAL + VK_COMPARE_OP_EQUAL, // EQUAL + VK_COMPARE_OP_GREATER_OR_EQUAL, // GREATER_EQUAL + VK_COMPARE_OP_GREATER, // GREATER + VK_COMPARE_OP_NOT_EQUAL, // NOT_EQUAL +}; + +constexpr VkCompareOp GetCompareOp(CompareFunc compareFunc) +{ + return COMPARE_OP[(uint32_t)compareFunc]; +} + +constexpr std::array STENCIL_OP = { + VK_STENCIL_OP_KEEP, // KEEP, + VK_STENCIL_OP_ZERO, // ZERO, + VK_STENCIL_OP_REPLACE, // REPLACE, + VK_STENCIL_OP_INCREMENT_AND_CLAMP, // INCREMENT_AND_CLAMP, + VK_STENCIL_OP_DECREMENT_AND_CLAMP, // DECREMENT_AND_CLAMP, + VK_STENCIL_OP_INVERT, // INVERT, + VK_STENCIL_OP_INCREMENT_AND_WRAP, // INCREMENT_AND_WRAP, + VK_STENCIL_OP_DECREMENT_AND_WRAP // DECREMENT_AND_WRAP +}; + +constexpr VkStencilOp GetStencilOp(StencilFunc stencilFunc) +{ + return STENCIL_OP[(uint32_t)stencilFunc]; +} + +constexpr std::array LOGIC_OP = { + VK_LOGIC_OP_MAX_ENUM, // NONE + VK_LOGIC_OP_CLEAR, // CLEAR + VK_LOGIC_OP_AND, // AND + VK_LOGIC_OP_AND_REVERSE, // AND_REVERSE + VK_LOGIC_OP_COPY, // COPY + VK_LOGIC_OP_AND_INVERTED, // AND_INVERTED + VK_LOGIC_OP_XOR, // XOR + VK_LOGIC_OP_OR, // OR + VK_LOGIC_OP_NOR, // NOR + VK_LOGIC_OP_EQUIVALENT, // EQUIVALENT + VK_LOGIC_OP_INVERT, // INVERT + VK_LOGIC_OP_OR_REVERSE, // OR_REVERSE + VK_LOGIC_OP_COPY_INVERTED, // COPY_INVERTED + VK_LOGIC_OP_OR_INVERTED, // OR_INVERTED + VK_LOGIC_OP_NAND, // NAND + VK_LOGIC_OP_SET // SET +}; + +constexpr VkLogicOp GetLogicOp(LogicFunc logicFunc) +{ + return LOGIC_OP[(uint32_t)logicFunc]; +} + +constexpr std::array BLEND_FACTOR = { + VK_BLEND_FACTOR_ZERO, // ZERO + VK_BLEND_FACTOR_ONE, // ONE + VK_BLEND_FACTOR_SRC_COLOR, // SRC_COLOR + VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR, // ONE_MINUS_SRC_COLOR + VK_BLEND_FACTOR_DST_COLOR, // DST_COLOR + VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR, // ONE_MINUS_DST_COLOR + VK_BLEND_FACTOR_SRC_ALPHA, // SRC_ALPHA + VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, // ONE_MINUS_SRC_ALPHA + VK_BLEND_FACTOR_DST_ALPHA, // DST_ALPHA + VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA, // ONE_MINUS_DST_ALPHA + VK_BLEND_FACTOR_CONSTANT_COLOR, // CONSTANT_COLOR + VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR, // ONE_MINUS_CONSTANT_COLOR + VK_BLEND_FACTOR_CONSTANT_ALPHA, // CONSTANT_ALPHA + VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA, // ONE_MINUS_CONSTANT_ALPHA + VK_BLEND_FACTOR_SRC_ALPHA_SATURATE, // SRC_ALPHA_SATURATE + VK_BLEND_FACTOR_SRC1_COLOR, // SRC1_COLOR + VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR, // ONE_MINUS_SRC1_COLOR + VK_BLEND_FACTOR_SRC1_ALPHA, // SRC1_ALPHA + VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA, // ONE_MINUS_SRC1_ALPHA +}; + +constexpr VkBlendFactor GetBlendFactor(BlendFactor blendFactor) +{ + return BLEND_FACTOR[(uint32_t)blendFactor]; +} + +constexpr std::array BLEND_OP = { + VK_BLEND_OP_ADD, // ADD + VK_BLEND_OP_SUBTRACT, // SUBTRACT + VK_BLEND_OP_REVERSE_SUBTRACT, // REVERSE_SUBTRACT + VK_BLEND_OP_MIN, // MIN + VK_BLEND_OP_MAX // MAX +}; + +constexpr VkBlendOp GetBlendOp(BlendFunc blendFunc) +{ + return BLEND_OP[(uint32_t)blendFunc]; +} - constexpr std::array COMPARE_OP = { - VK_COMPARE_OP_NEVER, // NONE - VK_COMPARE_OP_ALWAYS, // ALWAYS - VK_COMPARE_OP_NEVER, // NEVER - VK_COMPARE_OP_LESS, // LESS - VK_COMPARE_OP_LESS_OR_EQUAL, // LESS_EQUAL - VK_COMPARE_OP_EQUAL, // EQUAL - VK_COMPARE_OP_GREATER_OR_EQUAL, // GREATER_EQUAL - VK_COMPARE_OP_GREATER, // GREATER - VK_COMPARE_OP_NOT_EQUAL, // NOT_EQUAL - }; - - constexpr VkCompareOp GetCompareOp(CompareFunc compareFunc) - { - return COMPARE_OP[(uint32_t)compareFunc]; - } +constexpr VkColorComponentFlags GetColorComponent(ColorWriteBits colorWriteMask) +{ + return VkColorComponentFlags(colorWriteMask & ColorWriteBits::RGBA); +} - constexpr std::array STENCIL_OP = { - VK_STENCIL_OP_KEEP, // KEEP, - VK_STENCIL_OP_ZERO, // ZERO, - VK_STENCIL_OP_REPLACE, // REPLACE, - VK_STENCIL_OP_INCREMENT_AND_CLAMP, // INCREMENT_AND_CLAMP, - VK_STENCIL_OP_DECREMENT_AND_CLAMP, // DECREMENT_AND_CLAMP, - VK_STENCIL_OP_INVERT, // INVERT, - VK_STENCIL_OP_INCREMENT_AND_WRAP, // INCREMENT_AND_WRAP, - VK_STENCIL_OP_DECREMENT_AND_WRAP // DECREMENT_AND_WRAP - }; - - constexpr VkStencilOp GetStencilOp(StencilFunc stencilFunc) - { - return STENCIL_OP[(uint32_t)stencilFunc]; - } +constexpr std::array IMAGE_TYPES = { + VK_IMAGE_TYPE_1D, + VK_IMAGE_TYPE_2D, + VK_IMAGE_TYPE_3D +}; - constexpr std::array LOGIC_OP = { - VK_LOGIC_OP_MAX_ENUM, // NONE - VK_LOGIC_OP_CLEAR, // CLEAR - VK_LOGIC_OP_AND, // AND - VK_LOGIC_OP_AND_REVERSE, // AND_REVERSE - VK_LOGIC_OP_COPY, // COPY - VK_LOGIC_OP_AND_INVERTED, // AND_INVERTED - VK_LOGIC_OP_XOR, // XOR - VK_LOGIC_OP_OR, // OR - VK_LOGIC_OP_NOR, // NOR - VK_LOGIC_OP_EQUIVALENT, // EQUIVALENT - VK_LOGIC_OP_INVERT, // INVERT - VK_LOGIC_OP_OR_REVERSE, // OR_REVERSE - VK_LOGIC_OP_COPY_INVERTED, // COPY_INVERTED - VK_LOGIC_OP_OR_INVERTED, // OR_INVERTED - VK_LOGIC_OP_NAND, // NAND - VK_LOGIC_OP_SET // SET - }; - - constexpr VkLogicOp GetLogicOp(LogicFunc logicFunc) - { - return LOGIC_OP[(uint32_t)logicFunc]; - } +constexpr VkImageType GetImageType(TextureType type) +{ + return IMAGE_TYPES[(uint32_t)type]; +} - constexpr std::array BLEND_FACTOR = { - VK_BLEND_FACTOR_ZERO, // ZERO - VK_BLEND_FACTOR_ONE, // ONE - VK_BLEND_FACTOR_SRC_COLOR, // SRC_COLOR - VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR, // ONE_MINUS_SRC_COLOR - VK_BLEND_FACTOR_DST_COLOR, // DST_COLOR - VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR, // ONE_MINUS_DST_COLOR - VK_BLEND_FACTOR_SRC_ALPHA, // SRC_ALPHA - VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, // ONE_MINUS_SRC_ALPHA - VK_BLEND_FACTOR_DST_ALPHA, // DST_ALPHA - VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA, // ONE_MINUS_DST_ALPHA - VK_BLEND_FACTOR_CONSTANT_COLOR, // CONSTANT_COLOR - VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR, // ONE_MINUS_CONSTANT_COLOR - VK_BLEND_FACTOR_CONSTANT_ALPHA, // CONSTANT_ALPHA - VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA, // ONE_MINUS_CONSTANT_ALPHA - VK_BLEND_FACTOR_SRC_ALPHA_SATURATE, // SRC_ALPHA_SATURATE - VK_BLEND_FACTOR_SRC1_COLOR, // SRC1_COLOR - VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR, // ONE_MINUS_SRC1_COLOR - VK_BLEND_FACTOR_SRC1_ALPHA, // SRC1_ALPHA - VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA, // ONE_MINUS_SRC1_ALPHA - }; - - constexpr VkBlendFactor GetBlendFactor(BlendFactor blendFactor) - { - return BLEND_FACTOR[(uint32_t)blendFactor]; - } +constexpr VkImageUsageFlags GetImageUsageFlags(TextureUsageBits usageMask) +{ + VkImageUsageFlags flags = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT; - constexpr std::array BLEND_OP = { - VK_BLEND_OP_ADD, // ADD - VK_BLEND_OP_SUBTRACT, // SUBTRACT - VK_BLEND_OP_REVERSE_SUBTRACT, // REVERSE_SUBTRACT - VK_BLEND_OP_MIN, // MIN - VK_BLEND_OP_MAX // MAX - }; + if (usageMask & TextureUsageBits::SHADER_RESOURCE) + flags |= VK_IMAGE_USAGE_SAMPLED_BIT; - constexpr VkBlendOp GetBlendOp(BlendFunc blendFunc) - { - return BLEND_OP[(uint32_t)blendFunc]; - } + if (usageMask & TextureUsageBits::SHADER_RESOURCE_STORAGE) + flags |= VK_IMAGE_USAGE_STORAGE_BIT; - constexpr VkColorComponentFlags GetColorComponent(ColorWriteBits colorWriteMask) - { - return VkColorComponentFlags(colorWriteMask & ColorWriteBits::RGBA); - } + if (usageMask & TextureUsageBits::COLOR_ATTACHMENT) + flags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; - constexpr std::array IMAGE_TYPES = { - VK_IMAGE_TYPE_1D, - VK_IMAGE_TYPE_2D, - VK_IMAGE_TYPE_3D - }; + if (usageMask & TextureUsageBits::DEPTH_STENCIL_ATTACHMENT) + flags |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT; - constexpr VkImageType GetImageType(TextureType type) - { - return IMAGE_TYPES[(uint32_t)type]; - } + return flags; +} - constexpr VkImageUsageFlags GetImageUsageFlags(TextureUsageBits usageMask) +constexpr VkImageAspectFlags GetImageAspectFlags(Format format) +{ + switch (format) { - VkImageUsageFlags flags = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT; + case Format::D16_UNORM: + case Format::D32_SFLOAT: + case Format::R24_UNORM_X8: + case Format::R32_SFLOAT_X8_X24: + return VK_IMAGE_ASPECT_DEPTH_BIT; - if (usageMask & TextureUsageBits::SHADER_RESOURCE) - flags |= VK_IMAGE_USAGE_SAMPLED_BIT; + case Format::D24_UNORM_S8_UINT: + case Format::D32_SFLOAT_S8_UINT_X24: + return VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT; - if (usageMask & TextureUsageBits::SHADER_RESOURCE_STORAGE) - flags |= VK_IMAGE_USAGE_STORAGE_BIT; + case Format::X32_R8_UINT_X24: + case Format::X24_R8_UINT: + return VK_IMAGE_ASPECT_STENCIL_BIT; - if (usageMask & TextureUsageBits::COLOR_ATTACHMENT) - flags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; - - if (usageMask & TextureUsageBits::DEPTH_STENCIL_ATTACHMENT) - flags |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT; - - return flags; + default: + return VK_IMAGE_ASPECT_COLOR_BIT; } +} - constexpr VkImageAspectFlags GetImageAspectFlags(Format format) - { - switch (format) - { - case Format::D16_UNORM: - case Format::D32_SFLOAT: - case Format::R24_UNORM_X8: - case Format::R32_SFLOAT_X8_X24: - return VK_IMAGE_ASPECT_DEPTH_BIT; - - case Format::D24_UNORM_S8_UINT: - case Format::D32_SFLOAT_S8_UINT_X24: - return VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT; - - case Format::X32_R8_UINT_X24: - case Format::X24_R8_UINT: - return VK_IMAGE_ASPECT_STENCIL_BIT; - - default: - return VK_IMAGE_ASPECT_COLOR_BIT; - } - } +constexpr std::array FILTER = { + VK_FILTER_NEAREST, // NEAREST + VK_FILTER_LINEAR, // LINEAR +}; - constexpr std::array FILTER = { - VK_FILTER_NEAREST, // NEAREST - VK_FILTER_LINEAR, // LINEAR - }; +constexpr VkFilter GetFilter(Filter filter) +{ + return FILTER[(uint32_t)filter]; +} - constexpr VkFilter GetFilter(Filter filter) - { - return FILTER[(uint32_t)filter]; - } +constexpr std::array SAMPLER_MIPMAP_MODE = { + VK_SAMPLER_MIPMAP_MODE_NEAREST, // NEAREST + VK_SAMPLER_MIPMAP_MODE_LINEAR, // LINEAR +}; - constexpr std::array SAMPLER_MIPMAP_MODE = { - VK_SAMPLER_MIPMAP_MODE_NEAREST, // NEAREST - VK_SAMPLER_MIPMAP_MODE_LINEAR, // LINEAR - }; +constexpr VkSamplerMipmapMode GetSamplerMipmapMode(Filter filter) +{ + return SAMPLER_MIPMAP_MODE[(uint32_t)filter]; +} - constexpr VkSamplerMipmapMode GetSamplerMipmapMode(Filter filter) - { - return SAMPLER_MIPMAP_MODE[(uint32_t)filter]; - } +constexpr std::array SAMPLER_ADDRESS_MODE = { + VK_SAMPLER_ADDRESS_MODE_REPEAT, // REPEAT + VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT, // MIRRORED_REPEAT + VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, // CLAMP_TO_EDGE + VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER // CLAMP_TO_BORDER +}; - constexpr std::array SAMPLER_ADDRESS_MODE = { - VK_SAMPLER_ADDRESS_MODE_REPEAT, // REPEAT - VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT, // MIRRORED_REPEAT - VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, // CLAMP_TO_EDGE - VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER // CLAMP_TO_BORDER - }; +constexpr VkSamplerAddressMode GetSamplerAddressMode(AddressMode addressMode) +{ + return SAMPLER_ADDRESS_MODE[(uint32_t)addressMode]; +} - constexpr VkSamplerAddressMode GetSamplerAddressMode(AddressMode addressMode) - { - return SAMPLER_ADDRESS_MODE[(uint32_t)addressMode]; - } +constexpr std::array QUERY_TYPE = { + VK_QUERY_TYPE_TIMESTAMP, // TIMESTAMP + VK_QUERY_TYPE_OCCLUSION, // OCCLUSION + VK_QUERY_TYPE_PIPELINE_STATISTICS, // PIPELINE_STATISTICS + VK_QUERY_TYPE_ACCELERATION_STRUCTURE_COMPACTED_SIZE_KHR // ACCELERATION_STRUCTURE_COMPACTED_SIZE +}; - constexpr std::array QUERY_TYPE = { - VK_QUERY_TYPE_TIMESTAMP, // TIMESTAMP - VK_QUERY_TYPE_OCCLUSION, // OCCLUSION - VK_QUERY_TYPE_PIPELINE_STATISTICS, // PIPELINE_STATISTICS - VK_QUERY_TYPE_ACCELERATION_STRUCTURE_COMPACTED_SIZE_KHR // ACCELERATION_STRUCTURE_COMPACTED_SIZE - }; +constexpr VkQueryType GetQueryType(QueryType queryType) +{ + return QUERY_TYPE[(uint32_t)queryType]; +} - constexpr VkQueryType GetQueryType(QueryType queryType) - { - return QUERY_TYPE[(uint32_t)queryType]; - } +constexpr VkQueryPipelineStatisticFlags GetQueryPipelineStatisticsFlags(PipelineStatsBits pipelineStatsMask) +{ + VkQueryPipelineStatisticFlags flags = 0; - constexpr VkQueryPipelineStatisticFlags GetQueryPipelineStatisticsFlags(PipelineStatsBits pipelineStatsMask) - { - VkQueryPipelineStatisticFlags flags = 0; + if (pipelineStatsMask & PipelineStatsBits::INPUT_ASSEMBLY_VERTICES) + flags |= VK_QUERY_PIPELINE_STATISTIC_INPUT_ASSEMBLY_VERTICES_BIT; - if (pipelineStatsMask & PipelineStatsBits::INPUT_ASSEMBLY_VERTICES) - flags |= VK_QUERY_PIPELINE_STATISTIC_INPUT_ASSEMBLY_VERTICES_BIT; + if (pipelineStatsMask & PipelineStatsBits::INPUT_ASSEMBLY_PRIMITIVES) + flags |= VK_QUERY_PIPELINE_STATISTIC_INPUT_ASSEMBLY_PRIMITIVES_BIT; - if (pipelineStatsMask & PipelineStatsBits::INPUT_ASSEMBLY_PRIMITIVES) - flags |= VK_QUERY_PIPELINE_STATISTIC_INPUT_ASSEMBLY_PRIMITIVES_BIT; + if (pipelineStatsMask & PipelineStatsBits::VERTEX_SHADER_INVOCATIONS) + flags |= VK_QUERY_PIPELINE_STATISTIC_VERTEX_SHADER_INVOCATIONS_BIT; - if (pipelineStatsMask & PipelineStatsBits::VERTEX_SHADER_INVOCATIONS) - flags |= VK_QUERY_PIPELINE_STATISTIC_VERTEX_SHADER_INVOCATIONS_BIT; + if (pipelineStatsMask & PipelineStatsBits::GEOMETRY_SHADER_INVOCATIONS) + flags |= VK_QUERY_PIPELINE_STATISTIC_GEOMETRY_SHADER_INVOCATIONS_BIT; - if (pipelineStatsMask & PipelineStatsBits::GEOMETRY_SHADER_INVOCATIONS) - flags |= VK_QUERY_PIPELINE_STATISTIC_GEOMETRY_SHADER_INVOCATIONS_BIT; + if (pipelineStatsMask & PipelineStatsBits::GEOMETRY_SHADER_PRIMITIVES) + flags |= VK_QUERY_PIPELINE_STATISTIC_GEOMETRY_SHADER_PRIMITIVES_BIT; - if (pipelineStatsMask & PipelineStatsBits::GEOMETRY_SHADER_PRIMITIVES) - flags |= VK_QUERY_PIPELINE_STATISTIC_GEOMETRY_SHADER_PRIMITIVES_BIT; + if (pipelineStatsMask & PipelineStatsBits::CLIPPING_INVOCATIONS) + flags |= VK_QUERY_PIPELINE_STATISTIC_CLIPPING_INVOCATIONS_BIT; - if (pipelineStatsMask & PipelineStatsBits::CLIPPING_INVOCATIONS) - flags |= VK_QUERY_PIPELINE_STATISTIC_CLIPPING_INVOCATIONS_BIT; + if (pipelineStatsMask & PipelineStatsBits::FRAGMENT_SHADER_INVOCATIONS) + flags |= VK_QUERY_PIPELINE_STATISTIC_FRAGMENT_SHADER_INVOCATIONS_BIT; - if (pipelineStatsMask & PipelineStatsBits::FRAGMENT_SHADER_INVOCATIONS) - flags |= VK_QUERY_PIPELINE_STATISTIC_FRAGMENT_SHADER_INVOCATIONS_BIT; + if (pipelineStatsMask & PipelineStatsBits::TESS_CONTROL_SHADER_PATCHES) + flags |= VK_QUERY_PIPELINE_STATISTIC_TESSELLATION_CONTROL_SHADER_PATCHES_BIT; - if (pipelineStatsMask & PipelineStatsBits::TESS_CONTROL_SHADER_PATCHES) - flags |= VK_QUERY_PIPELINE_STATISTIC_TESSELLATION_CONTROL_SHADER_PATCHES_BIT; + if (pipelineStatsMask & PipelineStatsBits::TESS_EVALUATION_SHADER_INVOCATIONS) + flags |= VK_QUERY_PIPELINE_STATISTIC_TESSELLATION_EVALUATION_SHADER_INVOCATIONS_BIT; - if (pipelineStatsMask & PipelineStatsBits::TESS_EVALUATION_SHADER_INVOCATIONS) - flags |= VK_QUERY_PIPELINE_STATISTIC_TESSELLATION_EVALUATION_SHADER_INVOCATIONS_BIT; + if (pipelineStatsMask & PipelineStatsBits::COMPUTE_SHADER_INVOCATIONS) + flags |= VK_QUERY_PIPELINE_STATISTIC_COMPUTE_SHADER_INVOCATIONS_BIT; - if (pipelineStatsMask & PipelineStatsBits::COMPUTE_SHADER_INVOCATIONS) - flags |= VK_QUERY_PIPELINE_STATISTIC_COMPUTE_SHADER_INVOCATIONS_BIT; + return flags; +} - return flags; - } +constexpr std::array IMAGE_VIEW_TYPE_1D = { + VK_IMAGE_VIEW_TYPE_1D, // SHADER_RESOURCE_1D, + VK_IMAGE_VIEW_TYPE_1D_ARRAY, // SHADER_RESOURCE_1D_ARRAY, + VK_IMAGE_VIEW_TYPE_1D, // SHADER_RESOURCE_STORAGE_1D, + VK_IMAGE_VIEW_TYPE_1D_ARRAY, // SHADER_RESOURCE_STORAGE_1D_ARRAY, + VK_IMAGE_VIEW_TYPE_1D, // COLOR_ATTACHMENT, + VK_IMAGE_VIEW_TYPE_1D, // DEPTH_STENCIL_ATTACHMENT +}; - constexpr std::array IMAGE_VIEW_TYPE_1D = { - VK_IMAGE_VIEW_TYPE_1D, // SHADER_RESOURCE_1D, - VK_IMAGE_VIEW_TYPE_1D_ARRAY, // SHADER_RESOURCE_1D_ARRAY, - VK_IMAGE_VIEW_TYPE_1D, // SHADER_RESOURCE_STORAGE_1D, - VK_IMAGE_VIEW_TYPE_1D_ARRAY, // SHADER_RESOURCE_STORAGE_1D_ARRAY, - VK_IMAGE_VIEW_TYPE_1D, // COLOR_ATTACHMENT, - VK_IMAGE_VIEW_TYPE_1D, // DEPTH_STENCIL_ATTACHMENT - }; - - constexpr std::array IMAGE_VIEW_TYPE_2D = { - VK_IMAGE_VIEW_TYPE_2D, // SHADER_RESOURCE_2D, - VK_IMAGE_VIEW_TYPE_2D_ARRAY, // SHADER_RESOURCE_2D_ARRAY, - VK_IMAGE_VIEW_TYPE_CUBE, // SHADER_RESOURCE_CUBE, - VK_IMAGE_VIEW_TYPE_CUBE_ARRAY, // SHADER_RESOURCE_CUBE_ARRAY, - VK_IMAGE_VIEW_TYPE_2D, // SHADER_RESOURCE_STORAGE_2D, - VK_IMAGE_VIEW_TYPE_2D_ARRAY, // SHADER_RESOURCE_STORAGE_2D_ARRAY, - VK_IMAGE_VIEW_TYPE_2D, // COLOR_ATTACHMENT, - VK_IMAGE_VIEW_TYPE_2D, // DEPTH_STENCIL_ATTACHMENT - }; - - constexpr std::array IMAGE_VIEW_TYPE_3D = { - VK_IMAGE_VIEW_TYPE_3D, // SHADER_RESOURCE_3D, - VK_IMAGE_VIEW_TYPE_3D, // SHADER_RESOURCE_STORAGE_3D, - VK_IMAGE_VIEW_TYPE_3D, // COLOR_ATTACHMENT - }; - - constexpr VkImageViewType GetImageViewType(Texture1DViewType type) - { - return IMAGE_VIEW_TYPE_1D[(uint32_t)type]; - } +constexpr std::array IMAGE_VIEW_TYPE_2D = { + VK_IMAGE_VIEW_TYPE_2D, // SHADER_RESOURCE_2D, + VK_IMAGE_VIEW_TYPE_2D_ARRAY, // SHADER_RESOURCE_2D_ARRAY, + VK_IMAGE_VIEW_TYPE_CUBE, // SHADER_RESOURCE_CUBE, + VK_IMAGE_VIEW_TYPE_CUBE_ARRAY, // SHADER_RESOURCE_CUBE_ARRAY, + VK_IMAGE_VIEW_TYPE_2D, // SHADER_RESOURCE_STORAGE_2D, + VK_IMAGE_VIEW_TYPE_2D_ARRAY, // SHADER_RESOURCE_STORAGE_2D_ARRAY, + VK_IMAGE_VIEW_TYPE_2D, // COLOR_ATTACHMENT, + VK_IMAGE_VIEW_TYPE_2D, // DEPTH_STENCIL_ATTACHMENT +}; - constexpr VkImageViewType GetImageViewType(Texture2DViewType type) - { - return IMAGE_VIEW_TYPE_2D[(uint32_t)type]; - } +constexpr std::array IMAGE_VIEW_TYPE_3D = { + VK_IMAGE_VIEW_TYPE_3D, // SHADER_RESOURCE_3D, + VK_IMAGE_VIEW_TYPE_3D, // SHADER_RESOURCE_STORAGE_3D, + VK_IMAGE_VIEW_TYPE_3D, // COLOR_ATTACHMENT +}; - constexpr VkImageViewType GetImageViewType(Texture3DViewType type) - { - return IMAGE_VIEW_TYPE_3D[(uint32_t)type]; - } +constexpr VkImageViewType GetImageViewType(Texture1DViewType type) +{ + return IMAGE_VIEW_TYPE_1D[(uint32_t)type]; +} - constexpr std::array IMAGE_VIEW_USAGE_1D = { - VK_IMAGE_USAGE_SAMPLED_BIT, // SHADER_RESOURCE_1D, - VK_IMAGE_USAGE_SAMPLED_BIT, // SHADER_RESOURCE_1D_ARRAY, - VK_IMAGE_USAGE_STORAGE_BIT, // SHADER_RESOURCE_STORAGE_1D, - VK_IMAGE_USAGE_STORAGE_BIT, // SHADER_RESOURCE_STORAGE_1D_ARRAY, - VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, // COLOR_ATTACHMENT, - VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, // DEPTH_STENCIL_ATTACHMENT - }; - - constexpr std::array IMAGE_VIEW_USAGE_2D = { - VK_IMAGE_USAGE_SAMPLED_BIT, // SHADER_RESOURCE_2D, - VK_IMAGE_USAGE_SAMPLED_BIT, // SHADER_RESOURCE_2D_ARRAY, - VK_IMAGE_USAGE_SAMPLED_BIT, // SHADER_RESOURCE_CUBE, - VK_IMAGE_USAGE_SAMPLED_BIT, // SHADER_RESOURCE_CUBE_ARRAY, - VK_IMAGE_USAGE_STORAGE_BIT, // SHADER_RESOURCE_STORAGE_2D, - VK_IMAGE_USAGE_STORAGE_BIT, // SHADER_RESOURCE_STORAGE_2D_ARRAY, - VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, // COLOR_ATTACHMENT, - VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, // DEPTH_STENCIL_ATTACHMENT - }; - - constexpr std::array IMAGE_VIEW_USAGE_3D = { - VK_IMAGE_USAGE_SAMPLED_BIT, // SHADER_RESOURCE_3D, - VK_IMAGE_USAGE_STORAGE_BIT, // SHADER_RESOURCE_STORAGE_3D, - VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, // COLOR_ATTACHMENT - }; - - constexpr VkImageUsageFlags GetImageViewUsage(Texture1DViewType type) - { - return IMAGE_VIEW_USAGE_1D[(uint32_t)type]; - } +constexpr VkImageViewType GetImageViewType(Texture2DViewType type) +{ + return IMAGE_VIEW_TYPE_2D[(uint32_t)type]; +} - constexpr VkImageUsageFlags GetImageViewUsage(Texture2DViewType type) - { - return IMAGE_VIEW_USAGE_2D[(uint32_t)type]; - } +constexpr VkImageViewType GetImageViewType(Texture3DViewType type) +{ + return IMAGE_VIEW_TYPE_3D[(uint32_t)type]; +} + +constexpr std::array IMAGE_VIEW_USAGE_1D = { + VK_IMAGE_USAGE_SAMPLED_BIT, // SHADER_RESOURCE_1D, + VK_IMAGE_USAGE_SAMPLED_BIT, // SHADER_RESOURCE_1D_ARRAY, + VK_IMAGE_USAGE_STORAGE_BIT, // SHADER_RESOURCE_STORAGE_1D, + VK_IMAGE_USAGE_STORAGE_BIT, // SHADER_RESOURCE_STORAGE_1D_ARRAY, + VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, // COLOR_ATTACHMENT, + VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, // DEPTH_STENCIL_ATTACHMENT +}; + +constexpr std::array IMAGE_VIEW_USAGE_2D = { + VK_IMAGE_USAGE_SAMPLED_BIT, // SHADER_RESOURCE_2D, + VK_IMAGE_USAGE_SAMPLED_BIT, // SHADER_RESOURCE_2D_ARRAY, + VK_IMAGE_USAGE_SAMPLED_BIT, // SHADER_RESOURCE_CUBE, + VK_IMAGE_USAGE_SAMPLED_BIT, // SHADER_RESOURCE_CUBE_ARRAY, + VK_IMAGE_USAGE_STORAGE_BIT, // SHADER_RESOURCE_STORAGE_2D, + VK_IMAGE_USAGE_STORAGE_BIT, // SHADER_RESOURCE_STORAGE_2D_ARRAY, + VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, // COLOR_ATTACHMENT, + VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, // DEPTH_STENCIL_ATTACHMENT +}; + +constexpr std::array IMAGE_VIEW_USAGE_3D = { + VK_IMAGE_USAGE_SAMPLED_BIT, // SHADER_RESOURCE_3D, + VK_IMAGE_USAGE_STORAGE_BIT, // SHADER_RESOURCE_STORAGE_3D, + VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, // COLOR_ATTACHMENT +}; + +constexpr VkImageUsageFlags GetImageViewUsage(Texture1DViewType type) +{ + return IMAGE_VIEW_USAGE_1D[(uint32_t)type]; +} - constexpr VkImageUsageFlags GetImageViewUsage(Texture3DViewType type) - { - return IMAGE_VIEW_USAGE_3D[(uint32_t)type]; - } +constexpr VkImageUsageFlags GetImageViewUsage(Texture2DViewType type) +{ + return IMAGE_VIEW_USAGE_2D[(uint32_t)type]; +} - constexpr std::array IMAGE_LAYOUT_1D = { - VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, // SHADER_RESOURCE_1D, - VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, // SHADER_RESOURCE_1D_ARRAY, - VK_IMAGE_LAYOUT_GENERAL, // SHADER_RESOURCE_STORAGE_1D, - VK_IMAGE_LAYOUT_GENERAL, // SHADER_RESOURCE_STORAGE_1D_ARRAY, - VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // COLOR_ATTACHMENT, - VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, // DEPTH_STENCIL_ATTACHMENT - }; - - constexpr std::array IMAGE_LAYOUT_2D = { - VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, // SHADER_RESOURCE_2D, - VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, // SHADER_RESOURCE_2D_ARRAY, - VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, // SHADER_RESOURCE_CUBE, - VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, // SHADER_RESOURCE_CUBE_ARRAY, - VK_IMAGE_LAYOUT_GENERAL, // SHADER_RESOURCE_STORAGE_2D, - VK_IMAGE_LAYOUT_GENERAL, // SHADER_RESOURCE_STORAGE_2D_ARRAY, - VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // COLOR_ATTACHMENT, - VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, // DEPTH_STENCIL_ATTACHMENT - }; - - constexpr std::array IMAGE_LAYOUT_3D = { - VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, // SHADER_RESOURCE_3D, - VK_IMAGE_LAYOUT_GENERAL, // SHADER_RESOURCE_STORAGE_3D, - VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // COLOR_ATTACHMENT - }; - - constexpr VkImageLayout GetImageLayoutForView(Texture1DViewType type) - { - return IMAGE_LAYOUT_1D[(uint32_t)type]; - } +constexpr VkImageUsageFlags GetImageViewUsage(Texture3DViewType type) +{ + return IMAGE_VIEW_USAGE_3D[(uint32_t)type]; +} + +constexpr std::array IMAGE_LAYOUT_1D = { + VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, // SHADER_RESOURCE_1D, + VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, // SHADER_RESOURCE_1D_ARRAY, + VK_IMAGE_LAYOUT_GENERAL, // SHADER_RESOURCE_STORAGE_1D, + VK_IMAGE_LAYOUT_GENERAL, // SHADER_RESOURCE_STORAGE_1D_ARRAY, + VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // COLOR_ATTACHMENT, + VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, // DEPTH_STENCIL_ATTACHMENT +}; + +constexpr std::array IMAGE_LAYOUT_2D = { + VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, // SHADER_RESOURCE_2D, + VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, // SHADER_RESOURCE_2D_ARRAY, + VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, // SHADER_RESOURCE_CUBE, + VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, // SHADER_RESOURCE_CUBE_ARRAY, + VK_IMAGE_LAYOUT_GENERAL, // SHADER_RESOURCE_STORAGE_2D, + VK_IMAGE_LAYOUT_GENERAL, // SHADER_RESOURCE_STORAGE_2D_ARRAY, + VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // COLOR_ATTACHMENT, + VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, // DEPTH_STENCIL_ATTACHMENT +}; + +constexpr std::array IMAGE_LAYOUT_3D = { + VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, // SHADER_RESOURCE_3D, + VK_IMAGE_LAYOUT_GENERAL, // SHADER_RESOURCE_STORAGE_3D, + VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // COLOR_ATTACHMENT +}; + +constexpr VkImageLayout GetImageLayoutForView(Texture1DViewType type) +{ + return IMAGE_LAYOUT_1D[(uint32_t)type]; +} - constexpr VkImageLayout GetImageLayoutForView(Texture2DViewType type) - { - return IMAGE_LAYOUT_2D[(uint32_t)type]; - } +constexpr VkImageLayout GetImageLayoutForView(Texture2DViewType type) +{ + return IMAGE_LAYOUT_2D[(uint32_t)type]; +} - constexpr VkImageLayout GetImageLayoutForView(Texture3DViewType type) - { - return IMAGE_LAYOUT_3D[(uint32_t)type]; - } +constexpr VkImageLayout GetImageLayoutForView(Texture3DViewType type) +{ + return IMAGE_LAYOUT_3D[(uint32_t)type]; +} - constexpr VkAccelerationStructureTypeKHR GetAccelerationStructureType(AccelerationStructureType type) - { - static_assert( VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR == (uint32_t)AccelerationStructureType::TOP_LEVEL, "Enum mismatch." ); - static_assert( VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR == (uint32_t)AccelerationStructureType::BOTTOM_LEVEL, "Enum mismatch." ); - return (VkAccelerationStructureTypeKHR)type; - } +constexpr VkAccelerationStructureTypeKHR GetAccelerationStructureType(AccelerationStructureType type) +{ + static_assert( VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR == (uint32_t)AccelerationStructureType::TOP_LEVEL, "Enum mismatch." ); + static_assert( VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR == (uint32_t)AccelerationStructureType::BOTTOM_LEVEL, "Enum mismatch." ); + return (VkAccelerationStructureTypeKHR)type; +} - constexpr VkBuildAccelerationStructureFlagsKHR GetAccelerationStructureBuildFlags(AccelerationStructureBuildBits flags) - { - static_assert( VK_BUILD_ACCELERATION_STRUCTURE_ALLOW_UPDATE_BIT_KHR == (uint32_t)AccelerationStructureBuildBits::ALLOW_UPDATE, "Enum mismatch." ); - static_assert( VK_BUILD_ACCELERATION_STRUCTURE_ALLOW_COMPACTION_BIT_KHR == (uint32_t)AccelerationStructureBuildBits::ALLOW_COMPACTION, "Enum mismatch." ); - static_assert( VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR == (uint32_t)AccelerationStructureBuildBits::PREFER_FAST_TRACE, "Enum mismatch." ); - static_assert( VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_BUILD_BIT_KHR == (uint32_t)AccelerationStructureBuildBits::PREFER_FAST_BUILD, "Enum mismatch." ); - static_assert( VK_BUILD_ACCELERATION_STRUCTURE_LOW_MEMORY_BIT_KHR == (uint32_t)AccelerationStructureBuildBits::MINIMIZE_MEMORY, "Enum mismatch." ); - return (VkBuildAccelerationStructureFlagsKHR)flags; - } +constexpr VkBuildAccelerationStructureFlagsKHR GetAccelerationStructureBuildFlags(AccelerationStructureBuildBits flags) +{ + static_assert( VK_BUILD_ACCELERATION_STRUCTURE_ALLOW_UPDATE_BIT_KHR == (uint32_t)AccelerationStructureBuildBits::ALLOW_UPDATE, "Enum mismatch." ); + static_assert( VK_BUILD_ACCELERATION_STRUCTURE_ALLOW_COMPACTION_BIT_KHR == (uint32_t)AccelerationStructureBuildBits::ALLOW_COMPACTION, "Enum mismatch." ); + static_assert( VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR == (uint32_t)AccelerationStructureBuildBits::PREFER_FAST_TRACE, "Enum mismatch." ); + static_assert( VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_BUILD_BIT_KHR == (uint32_t)AccelerationStructureBuildBits::PREFER_FAST_BUILD, "Enum mismatch." ); + static_assert( VK_BUILD_ACCELERATION_STRUCTURE_LOW_MEMORY_BIT_KHR == (uint32_t)AccelerationStructureBuildBits::MINIMIZE_MEMORY, "Enum mismatch." ); + return (VkBuildAccelerationStructureFlagsKHR)flags; +} + +constexpr VkGeometryFlagsKHR GetGeometryFlags(BottomLevelGeometryBits geometryFlags) +{ + static_assert( VK_GEOMETRY_OPAQUE_BIT_KHR == (uint32_t)BottomLevelGeometryBits::OPAQUE_GEOMETRY, "Enum mismatch." ); + static_assert( VK_GEOMETRY_NO_DUPLICATE_ANY_HIT_INVOCATION_BIT_KHR == (uint32_t)BottomLevelGeometryBits::NO_DUPLICATE_ANY_HIT_INVOCATION, "Enum mismatch." ); + return (VkGeometryFlagsKHR)geometryFlags; +} - constexpr VkGeometryFlagsKHR GetGeometryFlags(BottomLevelGeometryBits geometryFlags) - { - static_assert( VK_GEOMETRY_OPAQUE_BIT_KHR == (uint32_t)BottomLevelGeometryBits::OPAQUE_GEOMETRY, "Enum mismatch." ); - static_assert( VK_GEOMETRY_NO_DUPLICATE_ANY_HIT_INVOCATION_BIT_KHR == (uint32_t)BottomLevelGeometryBits::NO_DUPLICATE_ANY_HIT_INVOCATION, "Enum mismatch." ); - return (VkGeometryFlagsKHR)geometryFlags; - } +constexpr VkGeometryTypeKHR GetGeometryType(GeometryType geometryType) +{ + static_assert( VK_GEOMETRY_TYPE_TRIANGLES_KHR == (uint32_t)GeometryType::TRIANGLES, "Enum mismatch." ); + static_assert( VK_GEOMETRY_TYPE_AABBS_KHR == (uint32_t)GeometryType::AABBS, "Enum mismatch." ); + return (VkGeometryTypeKHR)geometryType; +} - constexpr VkGeometryTypeKHR GetGeometryType(GeometryType geometryType) - { - static_assert( VK_GEOMETRY_TYPE_TRIANGLES_KHR == (uint32_t)GeometryType::TRIANGLES, "Enum mismatch." ); - static_assert( VK_GEOMETRY_TYPE_AABBS_KHR == (uint32_t)GeometryType::AABBS, "Enum mismatch." ); - return (VkGeometryTypeKHR)geometryType; - } +constexpr VkCopyAccelerationStructureModeKHR GetCopyMode(CopyMode copyMode) +{ + static_assert( VK_COPY_ACCELERATION_STRUCTURE_MODE_CLONE_KHR == (uint32_t)CopyMode::CLONE, "Enum mismatch." ); + static_assert( VK_COPY_ACCELERATION_STRUCTURE_MODE_COMPACT_KHR == (uint32_t)CopyMode::COMPACT, "Enum mismatch." ); + return (VkCopyAccelerationStructureModeKHR)copyMode; +} - constexpr VkCopyAccelerationStructureModeKHR GetCopyMode(CopyMode copyMode) - { - static_assert( VK_COPY_ACCELERATION_STRUCTURE_MODE_CLONE_KHR == (uint32_t)CopyMode::CLONE, "Enum mismatch." ); - static_assert( VK_COPY_ACCELERATION_STRUCTURE_MODE_COMPACT_KHR == (uint32_t)CopyMode::COMPACT, "Enum mismatch." ); - return (VkCopyAccelerationStructureModeKHR)copyMode; - } +void ConvertGeometryObjectSizesVK(uint32_t physicalDeviceIndex, VkAccelerationStructureGeometryKHR* destObjects, uint32_t* primitiveNums, const GeometryObject* sourceObjects, uint32_t objectNum); +void ConvertGeometryObjectsVK(uint32_t physicalDeviceIndex, VkAccelerationStructureGeometryKHR* destObjects, VkAccelerationStructureBuildRangeInfoKHR* ranges, const GeometryObject* sourceObjects, uint32_t objectNum); - void ConvertGeometryObjectSizesVK(uint32_t physicalDeviceIndex, VkAccelerationStructureGeometryKHR* destObjects, uint32_t* primitiveNums, const GeometryObject* sourceObjects, uint32_t objectNum); - void ConvertGeometryObjectsVK(uint32_t physicalDeviceIndex, VkAccelerationStructureGeometryKHR* destObjects, VkAccelerationStructureBuildRangeInfoKHR* ranges, const GeometryObject* sourceObjects, uint32_t objectNum); +constexpr std::array TEXTURE_TYPE_TABLE = { + TextureType::TEXTURE_1D, // VK_IMAGE_TYPE_1D + TextureType::TEXTURE_2D, // VK_IMAGE_TYPE_2D + TextureType::TEXTURE_3D, // VK_IMAGE_TYPE_3D +}; - constexpr std::array TEXTURE_TYPE_TABLE = { - TextureType::TEXTURE_1D, // VK_IMAGE_TYPE_1D - TextureType::TEXTURE_2D, // VK_IMAGE_TYPE_2D - TextureType::TEXTURE_3D, // VK_IMAGE_TYPE_3D - }; +constexpr TextureType GetTextureType(VkImageType type) +{ + if ((size_t)type < TEXTURE_TYPE_TABLE.size()) + return TEXTURE_TYPE_TABLE[(uint32_t)type]; - constexpr TextureType GetTextureType(VkImageType type) - { - if ((size_t)type < TEXTURE_TYPE_TABLE.size()) - return TEXTURE_TYPE_TABLE[(uint32_t)type]; + return TextureType::MAX_NUM; +} - return TextureType::MAX_NUM; - } +constexpr Result GetReturnCode(VkResult result) +{ + switch (result) + { + case VK_SUCCESS: + case VK_EVENT_SET: + case VK_EVENT_RESET: + return Result::SUCCESS; + + case VK_ERROR_DEVICE_LOST: + return Result::DEVICE_LOST; + + case VK_ERROR_SURFACE_LOST_KHR: + case VK_ERROR_OUT_OF_DATE_KHR: + return Result::SWAPCHAIN_RESIZE; + + case VK_ERROR_NATIVE_WINDOW_IN_USE_KHR: + case VK_ERROR_INCOMPATIBLE_DISPLAY_KHR: + case VK_ERROR_FORMAT_NOT_SUPPORTED: + case VK_ERROR_INCOMPATIBLE_DRIVER: + case VK_ERROR_FEATURE_NOT_PRESENT: + case VK_ERROR_EXTENSION_NOT_PRESENT: + case VK_ERROR_LAYER_NOT_PRESENT: + return Result::UNSUPPORTED; + + case VK_ERROR_INVALID_EXTERNAL_HANDLE: + case VK_ERROR_VALIDATION_FAILED_EXT: + case VK_ERROR_NOT_PERMITTED_EXT: + case VK_SUBOPTIMAL_KHR: + case VK_ERROR_TOO_MANY_OBJECTS: + case VK_ERROR_MEMORY_MAP_FAILED: + case VK_ERROR_INITIALIZATION_FAILED: + case VK_INCOMPLETE: + case VK_TIMEOUT: + case VK_NOT_READY: + return Result::FAILURE; + + case VK_ERROR_OUT_OF_HOST_MEMORY: + case VK_ERROR_OUT_OF_DEVICE_MEMORY: + case VK_ERROR_OUT_OF_POOL_MEMORY: + case VK_ERROR_FRAGMENTATION_EXT: + case VK_ERROR_FRAGMENTED_POOL: + return Result::OUT_OF_MEMORY; + + default: + return Result::FAILURE; + } +} - constexpr Result GetReturnCode(VkResult result) - { - switch (result) - { - case VK_SUCCESS: - case VK_EVENT_SET: - case VK_EVENT_RESET: - return Result::SUCCESS; - - case VK_ERROR_DEVICE_LOST: - return Result::DEVICE_LOST; - - case VK_ERROR_SURFACE_LOST_KHR: - case VK_ERROR_OUT_OF_DATE_KHR: - return Result::SWAPCHAIN_RESIZE; - - case VK_ERROR_NATIVE_WINDOW_IN_USE_KHR: - case VK_ERROR_INCOMPATIBLE_DISPLAY_KHR: - case VK_ERROR_FORMAT_NOT_SUPPORTED: - case VK_ERROR_INCOMPATIBLE_DRIVER: - case VK_ERROR_FEATURE_NOT_PRESENT: - case VK_ERROR_EXTENSION_NOT_PRESENT: - case VK_ERROR_LAYER_NOT_PRESENT: - return Result::UNSUPPORTED; - - case VK_ERROR_INVALID_EXTERNAL_HANDLE: - case VK_ERROR_VALIDATION_FAILED_EXT: - case VK_ERROR_INVALID_SHADER_NV: - case VK_ERROR_NOT_PERMITTED_EXT: - case VK_SUBOPTIMAL_KHR: - case VK_ERROR_TOO_MANY_OBJECTS: - case VK_ERROR_MEMORY_MAP_FAILED: - case VK_ERROR_INITIALIZATION_FAILED: - case VK_INCOMPLETE: - case VK_TIMEOUT: - case VK_NOT_READY: - return Result::FAILURE; - - case VK_ERROR_OUT_OF_HOST_MEMORY: - case VK_ERROR_OUT_OF_DEVICE_MEMORY: - case VK_ERROR_OUT_OF_POOL_MEMORY: - case VK_ERROR_FRAGMENTATION_EXT: - case VK_ERROR_FRAGMENTED_POOL: - return Result::OUT_OF_MEMORY; - - default: - return Result::FAILURE; - } - } } \ No newline at end of file diff --git a/Source/VK/DescriptorPoolVK.cpp b/Source/VK/DescriptorPoolVK.cpp index c7fe4d70..b5dab180 100644 --- a/Source/VK/DescriptorPoolVK.cpp +++ b/Source/VK/DescriptorPoolVK.cpp @@ -12,18 +12,9 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include "DescriptorPoolVK.h" #include "DescriptorSetVK.h" #include "PipelineLayoutVK.h" -#include "DeviceVK.h" using namespace nri; -DescriptorPoolVK::DescriptorPoolVK(DeviceVK& device) : - m_AllocatedSets(device.GetStdAllocator()), - m_Device(device) -{ - const size_t initialCapacity = 64; - m_AllocatedSets.reserve(initialCapacity); -} - DescriptorPoolVK::~DescriptorPoolVK() { const auto& lowLevelAllocator = m_Device.GetStdAllocator().GetInterface(); @@ -75,7 +66,7 @@ Result DescriptorPoolVK::Create(const DescriptorPoolDesc& descriptorPoolDesc) AddDescriptorPoolSize(descriptorPoolSizeArray, poolSizeCount, VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, descriptorPoolDesc.bufferMaxNum); AddDescriptorPoolSize(descriptorPoolSizeArray, poolSizeCount, VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, descriptorPoolDesc.storageBufferMaxNum); AddDescriptorPoolSize(descriptorPoolSizeArray, poolSizeCount, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, descriptorPoolDesc.structuredBufferMaxNum + descriptorPoolDesc.storageStructuredBufferMaxNum); - AddDescriptorPoolSize(descriptorPoolSizeArray, poolSizeCount, VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV, descriptorPoolDesc.accelerationStructureMaxNum); + AddDescriptorPoolSize(descriptorPoolSizeArray, poolSizeCount, VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR, descriptorPoolDesc.accelerationStructureMaxNum); for (uint32_t i = 0; i < poolSizeCount; i++) descriptorPoolSizeArray[i].descriptorCount *= physicalDeviceNum; @@ -105,6 +96,10 @@ Result DescriptorPoolVK::Create(NRIVkDescriptorPool vkDescriptorPool) return Result::SUCCESS; } +//================================================================================================================ +// NRI +//================================================================================================================ + inline void DescriptorPoolVK::SetDebugName(const char* name) { m_Device.SetDebugNameToTrivialObject(VK_OBJECT_TYPE_DESCRIPTOR_POOL, (uint64_t)m_Handle, name); diff --git a/Source/VK/DescriptorPoolVK.h b/Source/VK/DescriptorPoolVK.h index a1f008c5..925f9f49 100644 --- a/Source/VK/DescriptorPoolVK.h +++ b/Source/VK/DescriptorPoolVK.h @@ -12,41 +12,44 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct DeviceVK; - struct DescriptorSetVK; - - struct DescriptorPoolVK - { - DescriptorPoolVK(DeviceVK& device); - ~DescriptorPoolVK(); - - operator VkDescriptorPool() const; - DeviceVK& GetDevice() const; - Result Create(const DescriptorPoolDesc& descriptorPoolDesc); - Result Create(NRIVkDescriptorPool vkDescriptorPool); - - void SetDebugName(const char* name); - - Result AllocateDescriptorSets(const PipelineLayout& pipelineLayout, uint32_t setIndexInPipelineLayout, DescriptorSet** descriptorSets, - uint32_t numberOfCopies, uint32_t physicalDeviceMask, uint32_t variableDescriptorNum); - - void Reset(); - - private: - VkDescriptorPool m_Handle = VK_NULL_HANDLE; - Vector m_AllocatedSets; - uint32_t m_UsedSets = 0; - DeviceVK& m_Device; - bool m_OwnsNativeObjects = false; - }; - - inline DescriptorPoolVK::operator VkDescriptorPool() const - { - return m_Handle; - } - - inline DeviceVK& DescriptorPoolVK::GetDevice() const - { - return m_Device; - } + +struct DeviceVK; +struct DescriptorSetVK; + +struct DescriptorPoolVK +{ + inline DescriptorPoolVK(DeviceVK& device) : + m_Device(device) + , m_AllocatedSets(device.GetStdAllocator()) + { m_AllocatedSets.reserve(64); } + + inline operator VkDescriptorPool() const + { return m_Handle; } + + inline DeviceVK& GetDevice() const + { return m_Device; } + + ~DescriptorPoolVK(); + + Result Create(const DescriptorPoolDesc& descriptorPoolDesc); + Result Create(NRIVkDescriptorPool vkDescriptorPool); + + //================================================================================================================ + // NRI + //================================================================================================================ + + void SetDebugName(const char* name); + void Reset(); + + Result AllocateDescriptorSets(const PipelineLayout& pipelineLayout, uint32_t setIndexInPipelineLayout, DescriptorSet** descriptorSets, + uint32_t numberOfCopies, uint32_t physicalDeviceMask, uint32_t variableDescriptorNum); + +private: + DeviceVK& m_Device; + Vector m_AllocatedSets; + VkDescriptorPool m_Handle = VK_NULL_HANDLE; + uint32_t m_UsedSets = 0; + bool m_OwnsNativeObjects = false; +}; + } \ No newline at end of file diff --git a/Source/VK/DescriptorPoolVK.hpp b/Source/VK/DescriptorPoolVK.hpp index ec231b4c..05270d34 100644 --- a/Source/VK/DescriptorPoolVK.hpp +++ b/Source/VK/DescriptorPoolVK.hpp @@ -8,9 +8,7 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma once - -#pragma region [ CoreInterface ] +#pragma region [ Core ] static void NRI_CALL SetDescriptorPoolDebugName(DescriptorPool& descriptorPool, const char* name) { @@ -28,11 +26,6 @@ static void NRI_CALL ResetDescriptorPool(DescriptorPool& descriptorPool) ((DescriptorPoolVK&)descriptorPool).Reset(); } -void FillFunctionTableDescriptorPoolVK(CoreInterface& coreInterface) -{ - coreInterface.SetDescriptorPoolDebugName = ::SetDescriptorPoolDebugName; - coreInterface.AllocateDescriptorSets = ::AllocateDescriptorSets; - coreInterface.ResetDescriptorPool = ::ResetDescriptorPool; -} - #pragma endregion + +Define_Core_DescriptorPool_PartiallyFillFunctionTable(VK) diff --git a/Source/VK/DescriptorSetVK.cpp b/Source/VK/DescriptorSetVK.cpp index 64c25a6b..90e52e18 100644 --- a/Source/VK/DescriptorSetVK.cpp +++ b/Source/VK/DescriptorSetVK.cpp @@ -11,17 +11,29 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include "SharedVK.h" #include "DescriptorSetVK.h" #include "DescriptorVK.h" -#include "DeviceVK.h" using namespace nri; struct SlabAllocator { - SlabAllocator(void* memory, size_t size); + inline SlabAllocator(void* memory, size_t size) : + m_CurrentOffset((uint8_t*)memory), + m_End((size_t)memory + size), + m_Memory((uint8_t*)memory) + {} + + template + inline T* Allocate(uint32_t& number) + { + T* items = (T*)Align(m_CurrentOffset, alignof(T)); + const size_t itemsLeft = (m_End - (size_t)items) / sizeof(T); + number = std::min((uint32_t)itemsLeft, number); + m_CurrentOffset = (uint8_t*)(items + number); + return items; + } - template< typename T > - T* Allocate(uint32_t& number); - void Reset(); + inline void Reset() + { m_CurrentOffset = m_Memory; } private: uint8_t * m_CurrentOffset; @@ -29,36 +41,7 @@ struct SlabAllocator uint8_t* m_Memory; }; -DescriptorSetVK::DescriptorSetVK(DeviceVK& device) : m_Device(device) -{ -} - -void DescriptorSetVK::Create(const VkDescriptorSet* handles, uint32_t physicalDeviceMask, const DescriptorSetDesc& setDesc) -{ - m_SetDesc = &setDesc; - m_DynamicConstantBufferNum = setDesc.dynamicConstantBufferNum; - - uint32_t handleIndex = 0; - for (uint32_t i = 0; i < m_Device.GetPhysicalDeviceGroupSize(); i++) - { - if ((1 << i) & physicalDeviceMask) - m_Handles[i] = handles[handleIndex++]; - } -} - -inline void DescriptorSetVK::SetDebugName(const char* name) -{ - std::array handles; - for (size_t i = 0; i < handles.size(); i++) - handles[i] = (uint64_t)m_Handles[i]; - - m_Device.SetDebugNameToDeviceGroupObject(VK_OBJECT_TYPE_DESCRIPTOR_SET, handles.data(), name); -} - -typedef bool(*WriteDescriptorsFunc)(uint32_t physicalDeviceIndex, const DescriptorRangeDesc& rangeDesc, const DescriptorRangeUpdateDesc& update, - uint32_t& descriptorOffset, VkWriteDescriptorSet& write, SlabAllocator& slab); - -bool WriteTextures(uint32_t physicalDeviceIndex, const DescriptorRangeDesc& rangeDesc, const DescriptorRangeUpdateDesc& update, +static bool WriteTextures(uint32_t physicalDeviceIndex, const DescriptorRangeDesc& rangeDesc, const DescriptorRangeUpdateDesc& update, uint32_t& descriptorOffset, VkWriteDescriptorSet& write, SlabAllocator& slab) { const uint32_t totalItemNum = update.descriptorNum - descriptorOffset; @@ -82,7 +65,7 @@ bool WriteTextures(uint32_t physicalDeviceIndex, const DescriptorRangeDesc& rang return itemNumForWriting == totalItemNum; } -inline bool WriteTypedBuffers(uint32_t physicalDeviceIndex, const DescriptorRangeDesc& rangeDesc, const DescriptorRangeUpdateDesc& update, +static bool WriteTypedBuffers(uint32_t physicalDeviceIndex, const DescriptorRangeDesc& rangeDesc, const DescriptorRangeUpdateDesc& update, uint32_t& descriptorOffset, VkWriteDescriptorSet& write, SlabAllocator& slab) { const uint32_t totalItemNum = update.descriptorNum - descriptorOffset; @@ -103,7 +86,7 @@ inline bool WriteTypedBuffers(uint32_t physicalDeviceIndex, const DescriptorRang return itemNumForWriting == totalItemNum; } -inline bool WriteBuffers(uint32_t physicalDeviceIndex, const DescriptorRangeDesc& rangeDesc, const DescriptorRangeUpdateDesc& update, +static bool WriteBuffers(uint32_t physicalDeviceIndex, const DescriptorRangeDesc& rangeDesc, const DescriptorRangeUpdateDesc& update, uint32_t& descriptorOffset, VkWriteDescriptorSet& write, SlabAllocator& slab) { const uint32_t totalItemNum = update.descriptorNum - descriptorOffset; @@ -124,7 +107,7 @@ inline bool WriteBuffers(uint32_t physicalDeviceIndex, const DescriptorRangeDesc return itemNumForWriting == totalItemNum; } -inline bool WriteSamplers(uint32_t physicalDeviceIndex, const DescriptorRangeDesc& rangeDesc, const DescriptorRangeUpdateDesc& update, +static bool WriteSamplers(uint32_t physicalDeviceIndex, const DescriptorRangeDesc& rangeDesc, const DescriptorRangeUpdateDesc& update, uint32_t& descriptorOffset, VkWriteDescriptorSet& write, SlabAllocator& slab) { MaybeUnused(rangeDesc); @@ -150,7 +133,7 @@ inline bool WriteSamplers(uint32_t physicalDeviceIndex, const DescriptorRangeDes return itemNumForWriting == totalItemNum; } -inline bool WriteAccelerationStructures(uint32_t physicalDeviceIndex, const DescriptorRangeDesc& rangeDesc, const DescriptorRangeUpdateDesc& update, +static bool WriteAccelerationStructures(uint32_t physicalDeviceIndex, const DescriptorRangeDesc& rangeDesc, const DescriptorRangeUpdateDesc& update, uint32_t& descriptorOffset, VkWriteDescriptorSet& write, SlabAllocator& slab) { MaybeUnused(rangeDesc); @@ -185,6 +168,9 @@ inline bool WriteAccelerationStructures(uint32_t physicalDeviceIndex, const Desc return itemNumForWriting == totalItemNum; } +typedef bool(*WriteDescriptorsFunc)(uint32_t physicalDeviceIndex, const DescriptorRangeDesc& rangeDesc, const DescriptorRangeUpdateDesc& update, + uint32_t& descriptorOffset, VkWriteDescriptorSet& write, SlabAllocator& slab); + constexpr std::array WRITE_FUNCS = { (WriteDescriptorsFunc)&WriteSamplers, // SAMPLER @@ -198,6 +184,32 @@ constexpr std::array WR (WriteDescriptorsFunc)&WriteAccelerationStructures, // ACCELERATION_STRUCTURE }; +void DescriptorSetVK::Create(const VkDescriptorSet* handles, uint32_t physicalDeviceMask, const DescriptorSetDesc& setDesc) +{ + m_SetDesc = &setDesc; + m_DynamicConstantBufferNum = setDesc.dynamicConstantBufferNum; + + uint32_t handleIndex = 0; + for (uint32_t i = 0; i < m_Device.GetPhysicalDeviceGroupSize(); i++) + { + if ((1 << i) & physicalDeviceMask) + m_Handles[i] = handles[handleIndex++]; + } +} + +//================================================================================================================ +// NRI +//================================================================================================================ + +inline void DescriptorSetVK::SetDebugName(const char* name) +{ + std::array handles; + for (size_t i = 0; i < handles.size(); i++) + handles[i] = (uint64_t)m_Handles[i]; + + m_Device.SetDebugNameToDeviceGroupObject(VK_OBJECT_TYPE_DESCRIPTOR_SET, handles.data(), name); +} + inline void DescriptorSetVK::UpdateDescriptorRanges(uint32_t physicalDeviceMask, uint32_t rangeOffset, uint32_t rangeNum, const DescriptorRangeUpdateDesc* rangeUpdateDescs) { constexpr uint32_t writesPerIteration = 1024; @@ -354,26 +366,4 @@ inline void DescriptorSetVK::Copy(const DescriptorSetCopyDesc& descriptorSetCopy vk.UpdateDescriptorSets(m_Device, 0, nullptr, copyNum, copies); } -inline SlabAllocator::SlabAllocator(void* memory, size_t size) : - m_CurrentOffset((uint8_t*)memory), - m_End((size_t)memory + size), - m_Memory((uint8_t*)memory) -{ -} - -template< typename T > -inline T* SlabAllocator::Allocate(uint32_t& number) -{ - T* items = (T*)Align(m_CurrentOffset, alignof(T)); - const size_t itemsLeft = (m_End - (size_t)items) / sizeof(T); - number = std::min((uint32_t)itemsLeft, number); - m_CurrentOffset = (uint8_t*)(items + number); - return items; -} - -inline void SlabAllocator::Reset() -{ - m_CurrentOffset = m_Memory; -} - #include "DescriptorSetVK.hpp" diff --git a/Source/VK/DescriptorSetVK.h b/Source/VK/DescriptorSetVK.h index d3d6faa4..fba6ce9b 100644 --- a/Source/VK/DescriptorSetVK.h +++ b/Source/VK/DescriptorSetVK.h @@ -12,42 +12,41 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct DeviceVK; - struct DescriptorSetDesc; - - struct DescriptorSetVK - { - DescriptorSetVK(DeviceVK& device); - - VkDescriptorSet GetHandle(uint32_t physicalDeviceIndex) const; - DeviceVK& GetDevice() const; - uint32_t GetDynamicConstantBufferNum() const; - void Create(const VkDescriptorSet* handles, uint32_t physicalDeviceMask, const DescriptorSetDesc& setDesc); - - void SetDebugName(const char* name); - void UpdateDescriptorRanges(uint32_t physicalDeviceMask, uint32_t rangeOffset, uint32_t rangeNum, const DescriptorRangeUpdateDesc* rangeUpdateDescs); - void UpdateDynamicConstantBuffers(uint32_t physicalDeviceMask, uint32_t bufferOffset, uint32_t descriptorNum, const Descriptor* const* descriptors); - void Copy(const DescriptorSetCopyDesc& descriptorSetCopyDesc); - - private: - std::array m_Handles = {}; - uint32_t m_DynamicConstantBufferNum = 0; - const DescriptorSetDesc* m_SetDesc = nullptr; - DeviceVK& m_Device; - }; - - inline VkDescriptorSet DescriptorSetVK::GetHandle(uint32_t physicalDeviceIndex) const - { - return m_Handles[physicalDeviceIndex]; - } - - inline DeviceVK& DescriptorSetVK::GetDevice() const - { - return m_Device; - } - - inline uint32_t DescriptorSetVK::GetDynamicConstantBufferNum() const - { - return m_DynamicConstantBufferNum; - } + +struct DeviceVK; +struct DescriptorSetDesc; + +struct DescriptorSetVK +{ + inline DescriptorSetVK(DeviceVK& device) : + m_Device(device) + {} + + inline VkDescriptorSet GetHandle(uint32_t physicalDeviceIndex) const + { return m_Handles[physicalDeviceIndex]; } + + inline DeviceVK& GetDevice() const + { return m_Device; } + + inline uint32_t GetDynamicConstantBufferNum() const + { return m_DynamicConstantBufferNum; } + + void Create(const VkDescriptorSet* handles, uint32_t physicalDeviceMask, const DescriptorSetDesc& setDesc); + + //================================================================================================================ + // NRI + //================================================================================================================ + + void SetDebugName(const char* name); + void UpdateDescriptorRanges(uint32_t physicalDeviceMask, uint32_t rangeOffset, uint32_t rangeNum, const DescriptorRangeUpdateDesc* rangeUpdateDescs); + void UpdateDynamicConstantBuffers(uint32_t physicalDeviceMask, uint32_t bufferOffset, uint32_t descriptorNum, const Descriptor* const* descriptors); + void Copy(const DescriptorSetCopyDesc& descriptorSetCopyDesc); + +private: + DeviceVK& m_Device; + std::array m_Handles = {}; + const DescriptorSetDesc* m_SetDesc = nullptr; + uint32_t m_DynamicConstantBufferNum = 0; +}; + } \ No newline at end of file diff --git a/Source/VK/DescriptorSetVK.hpp b/Source/VK/DescriptorSetVK.hpp index 9f14b2a5..3d56d638 100644 --- a/Source/VK/DescriptorSetVK.hpp +++ b/Source/VK/DescriptorSetVK.hpp @@ -8,9 +8,7 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma once - -#pragma region [ CoreInterface ] +#pragma region [ Core ] static void NRI_CALL SetDescriptorSetDebugName(DescriptorSet& descriptorSet, const char* name) { @@ -32,12 +30,6 @@ static void NRI_CALL CopyDescriptorSet(DescriptorSet& descriptorSet, const Descr ((DescriptorSetVK&)descriptorSet).Copy(descriptorSetCopyDesc); } -void FillFunctionTableDescriptorSetVK(CoreInterface& coreInterface) -{ - coreInterface.SetDescriptorSetDebugName = ::SetDescriptorSetDebugName; - coreInterface.UpdateDescriptorRanges = ::UpdateDescriptorRanges; - coreInterface.UpdateDynamicConstantBuffers = ::UpdateDynamicConstantBuffers; - coreInterface.CopyDescriptorSet = ::CopyDescriptorSet; -} - #pragma endregion + +Define_Core_DescriptorSet_PartiallyFillFunctionTable(VK) diff --git a/Source/VK/DescriptorVK.cpp b/Source/VK/DescriptorVK.cpp index fe720ec9..84bf9287 100644 --- a/Source/VK/DescriptorVK.cpp +++ b/Source/VK/DescriptorVK.cpp @@ -12,7 +12,6 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include "DescriptorVK.h" #include "TextureVK.h" #include "BufferVK.h" -#include "DeviceVK.h" using namespace nri; @@ -263,6 +262,10 @@ Result DescriptorVK::Create(const VkAccelerationStructureKHR* accelerationStruct return Result::SUCCESS; } +//================================================================================================================ +// NRI +//================================================================================================================ + inline void DescriptorVK::SetDebugName(const char* name) { std::array handles; @@ -297,15 +300,4 @@ inline void DescriptorVK::SetDebugName(const char* name) } } -inline VkBufferView DescriptorVK::GetBufferDescriptorVK(uint32_t physicalDeviceIndex) const -{ - return m_BufferViews[physicalDeviceIndex]; -} - -inline VkImageView DescriptorVK::GetTextureDescriptorVK(uint32_t physicalDeviceIndex, VkImageSubresourceRange& subresourceRange) const -{ - GetImageSubresourceRange(subresourceRange); - return m_ImageViews[physicalDeviceIndex]; -} - #include "DescriptorVK.hpp" \ No newline at end of file diff --git a/Source/VK/DescriptorVK.h b/Source/VK/DescriptorVK.h index 2e96907b..0e874a88 100644 --- a/Source/VK/DescriptorVK.h +++ b/Source/VK/DescriptorVK.h @@ -12,154 +12,91 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct DeviceVK; - struct TextureVK; - enum class DescriptorTypeVK - { - NONE = 0, - BUFFER_VIEW, - IMAGE_VIEW, - SAMPLER, - ACCELERATION_STRUCTURE - }; +struct DeviceVK; +struct TextureVK; - struct DescriptorBufferDesc - { - std::array handles; - uint64_t offset; - uint64_t size; - }; +enum class DescriptorTypeVK +{ + NONE = 0, + BUFFER_VIEW, + IMAGE_VIEW, + SAMPLER, + ACCELERATION_STRUCTURE +}; + +struct DescriptorBufferDesc +{ + std::array handles; + uint64_t offset; + uint64_t size; +}; - struct DescriptorTextureDesc - { - std::array handles; - const TextureVK* texture; - VkImageLayout imageLayout; - uint32_t imageMipOffset; - uint32_t imageMipNum; - uint32_t imageArrayOffset; - uint32_t imageArraySize; - VkImageAspectFlags imageAspectFlags; - }; +struct DescriptorTextureDesc +{ + std::array handles; + const TextureVK* texture; + VkImageLayout imageLayout; + uint32_t imageMipOffset; + uint32_t imageMipNum; + uint32_t imageArrayOffset; + uint32_t imageArraySize; + VkImageAspectFlags imageAspectFlags; +}; + +struct DescriptorVK +{ + inline DescriptorVK(DeviceVK& device) : + m_Device(device) + { m_BufferViews.fill(VK_NULL_HANDLE); } - struct DescriptorVK - { - DescriptorVK(DeviceVK& device); - ~DescriptorVK(); - - DeviceVK& GetDevice() const; - Result Create(const BufferViewDesc& bufferViewDesc); - Result Create(const Texture1DViewDesc& textureViewDesc); - Result Create(const Texture2DViewDesc& textureViewDesc); - Result Create(const Texture3DViewDesc& textureViewDesc); - Result Create(const SamplerDesc& samplerDesc); - Result Create(const VkAccelerationStructureKHR* accelerationStructures, uint32_t physicalDeviceMask); - VkBufferView GetBufferView(uint32_t physicalDeviceIndex) const; - VkImageView GetImageView(uint32_t physicalDeviceIndex) const; - const VkSampler& GetSampler() const; - VkAccelerationStructureKHR GetAccelerationStructure(uint32_t physicalDeviceIndex) const; - VkBuffer GetBuffer(uint32_t physicalDeviceIndex) const; - VkImage GetImage(uint32_t physicalDeviceIndex) const; - void GetBufferInfo(uint32_t physicalDeviceIndex, VkDescriptorBufferInfo& info) const; - const TextureVK& GetTexture() const; - DescriptorTypeVK GetType() const; - VkFormat GetFormat() const; - void GetImageSubresourceRange(VkImageSubresourceRange& range) const; - VkImageLayout GetImageLayout() const; - const DescriptorTextureDesc& GetTextureDesc() const; - const DescriptorBufferDesc& GetBufferDesc() const; - - template - Result CreateTextureView(const T& textureViewDesc); - - void SetDebugName(const char* name); - VkBufferView GetBufferDescriptorVK(uint32_t physicalDeviceIndex) const; - VkImageView GetTextureDescriptorVK(uint32_t physicalDeviceIndex, VkImageSubresourceRange& subresourceRange) const; - - private: - union - { - std::array m_BufferViews; - std::array m_ImageViews; - std::array m_AccelerationStructures; - VkSampler m_Sampler; - }; - union - { - DescriptorBufferDesc m_BufferDesc; - DescriptorTextureDesc m_TextureDesc; - }; - DescriptorTypeVK m_Type = DescriptorTypeVK::NONE; - VkFormat m_Format = VK_FORMAT_UNDEFINED; - DeviceVK& m_Device; - }; + inline DeviceVK& GetDevice() const + { return m_Device; } - inline DescriptorVK::DescriptorVK(DeviceVK& device) : - m_Device(device) - { - m_BufferViews.fill(VK_NULL_HANDLE); - m_TextureDesc = {}; - } + inline VkBufferView GetBufferView(uint32_t physicalDeviceIndex) const + { return m_BufferViews[physicalDeviceIndex]; } - inline VkBufferView DescriptorVK::GetBufferView(uint32_t physicalDeviceIndex) const - { - return m_BufferViews[physicalDeviceIndex]; - } + inline VkImageView GetImageView(uint32_t physicalDeviceIndex) const + { return m_ImageViews[physicalDeviceIndex]; } - inline VkImageView DescriptorVK::GetImageView(uint32_t physicalDeviceIndex) const - { - return m_ImageViews[physicalDeviceIndex]; - } + inline const VkSampler& GetSampler() const + { return m_Sampler; } - inline const VkSampler& DescriptorVK::GetSampler() const - { - return m_Sampler; - } + inline VkAccelerationStructureKHR GetAccelerationStructure(uint32_t physicalDeviceIndex) const + { return m_AccelerationStructures[physicalDeviceIndex]; } - inline VkAccelerationStructureKHR DescriptorVK::GetAccelerationStructure(uint32_t physicalDeviceIndex) const - { - return m_AccelerationStructures[physicalDeviceIndex]; - } + inline VkBuffer GetBuffer(uint32_t physicalDeviceIndex) const + { return m_BufferDesc.handles[physicalDeviceIndex]; } - inline VkBuffer DescriptorVK::GetBuffer(uint32_t physicalDeviceIndex) const - { - return m_BufferDesc.handles[physicalDeviceIndex]; - } + inline VkImage GetImage(uint32_t physicalDeviceIndex) const + { return m_TextureDesc.handles[physicalDeviceIndex]; } - inline VkImage DescriptorVK::GetImage(uint32_t physicalDeviceIndex) const - { - return m_TextureDesc.handles[physicalDeviceIndex]; - } + inline const TextureVK& GetTexture() const + { return *m_TextureDesc.texture; } - inline void DescriptorVK::GetBufferInfo(uint32_t physicalDeviceIndex, VkDescriptorBufferInfo& info) const - { - info.buffer = m_BufferDesc.handles[physicalDeviceIndex]; - info.offset = m_BufferDesc.offset; - info.range = m_BufferDesc.size; - } + inline DescriptorTypeVK GetType() const + { return m_Type; } - inline const TextureVK& DescriptorVK::GetTexture() const - { - return *m_TextureDesc.texture; - } + inline VkFormat GetFormat() const + { return m_Format; } - inline DeviceVK& DescriptorVK::GetDevice() const - { - return m_Device; - } + inline VkImageLayout GetImageLayout() const + { return m_TextureDesc.imageLayout; } - inline DescriptorTypeVK DescriptorVK::GetType() const - { - return m_Type; - } + inline const DescriptorTextureDesc& GetTextureDesc() const + { return m_TextureDesc; } - inline VkFormat DescriptorVK::GetFormat() const + inline const DescriptorBufferDesc& GetBufferDesc() const + { return m_BufferDesc; } + + inline void GetBufferInfo(uint32_t physicalDeviceIndex, VkDescriptorBufferInfo& info) const { - return m_Format; + info.buffer = m_BufferDesc.handles[physicalDeviceIndex]; + info.offset = m_BufferDesc.offset; + info.range = m_BufferDesc.size; } - inline void DescriptorVK::GetImageSubresourceRange(VkImageSubresourceRange& range) const + inline void GetImageSubresourceRange(VkImageSubresourceRange& range) const { range.aspectMask = m_TextureDesc.imageAspectFlags; range.baseMipLevel = m_TextureDesc.imageMipOffset; @@ -168,18 +105,41 @@ namespace nri range.layerCount = m_TextureDesc.imageArraySize; } - inline VkImageLayout DescriptorVK::GetImageLayout() const - { - return m_TextureDesc.imageLayout; - } + ~DescriptorVK(); - inline const DescriptorTextureDesc& DescriptorVK::GetTextureDesc() const - { - return m_TextureDesc; - } + Result Create(const BufferViewDesc& bufferViewDesc); + Result Create(const Texture1DViewDesc& textureViewDesc); + Result Create(const Texture2DViewDesc& textureViewDesc); + Result Create(const Texture3DViewDesc& textureViewDesc); + Result Create(const SamplerDesc& samplerDesc); + Result Create(const VkAccelerationStructureKHR* accelerationStructures, uint32_t physicalDeviceMask); + + //================================================================================================================ + // NRI + //================================================================================================================ + + void SetDebugName(const char* name); - inline const DescriptorBufferDesc& DescriptorVK::GetBufferDesc() const +private: + template + Result CreateTextureView(const T& textureViewDesc); + +private: + DeviceVK& m_Device; + union { - return m_BufferDesc; - } + std::array m_BufferViews; + std::array m_ImageViews; + std::array m_AccelerationStructures; + VkSampler m_Sampler; + }; + union + { + DescriptorBufferDesc m_BufferDesc; + DescriptorTextureDesc m_TextureDesc = {}; + }; + DescriptorTypeVK m_Type = DescriptorTypeVK::NONE; + VkFormat m_Format = VK_FORMAT_UNDEFINED; +}; + } \ No newline at end of file diff --git a/Source/VK/DescriptorVK.hpp b/Source/VK/DescriptorVK.hpp index 1b6edf20..23d12c77 100644 --- a/Source/VK/DescriptorVK.hpp +++ b/Source/VK/DescriptorVK.hpp @@ -8,9 +8,7 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma once - -#pragma region [ CoreInterface ] +#pragma region [ Core ] static void NRI_CALL SetDescriptorDebugName(Descriptor& descriptor, const char* name) { @@ -34,11 +32,6 @@ static uint64_t NRI_CALL GetDescriptorNativeObject(const Descriptor& descriptor, return handle; } -void FillFunctionTableDescriptorVK(CoreInterface& coreInterface) -{ - coreInterface.SetDescriptorDebugName = ::SetDescriptorDebugName; - - coreInterface.GetDescriptorNativeObject = ::GetDescriptorNativeObject; -} - #pragma endregion + +Define_Core_Descriptor_PartiallyFillFunctionTable(VK) diff --git a/Source/VK/DeviceSemaphoreVK.cpp b/Source/VK/DeviceSemaphoreVK.cpp deleted file mode 100644 index 8912839a..00000000 --- a/Source/VK/DeviceSemaphoreVK.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/* -Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. - -NVIDIA CORPORATION and its licensors retain all intellectual property -and proprietary rights in and to this software, related documentation -and any modifications thereto. Any use, reproduction, disclosure or -distribution of this software and related documentation without an express -license agreement from NVIDIA CORPORATION is strictly prohibited. -*/ - -#include "SharedVK.h" -#include "DeviceSemaphoreVK.h" -#include "DeviceVK.h" - -using namespace nri; - -DeviceSemaphoreVK::~DeviceSemaphoreVK() -{ - const auto& vk = m_Device.GetDispatchTable(); - if (m_Handle != VK_NULL_HANDLE && m_OwnsNativeObjects) - vk.DestroyFence(m_Device, m_Handle, m_Device.GetAllocationCallbacks()); -} - -Result DeviceSemaphoreVK::Create(bool signaled) -{ - m_OwnsNativeObjects = true; - - const VkFenceCreateInfo fenceInfo = { - VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, - nullptr, - signaled ? VK_FENCE_CREATE_SIGNALED_BIT : (VkFenceCreateFlags)0 - }; - - const auto& vk = m_Device.GetDispatchTable(); - const VkResult result = vk.CreateFence(m_Device, &fenceInfo, m_Device.GetAllocationCallbacks(), &m_Handle); - - RETURN_ON_FAILURE(m_Device.GetLog(), result == VK_SUCCESS, GetReturnCode(result), - "Can't create a semaphore: vkCreateFence returned %d.", (int32_t)result); - - return Result::SUCCESS; -} - -Result DeviceSemaphoreVK::Create(NRIVkFence vkFence) -{ - m_OwnsNativeObjects = false; - m_Handle = (VkFence)vkFence; - - return Result::SUCCESS; -} - -inline void DeviceSemaphoreVK::SetDebugName(const char* name) -{ - m_Device.SetDebugNameToTrivialObject(VK_OBJECT_TYPE_FENCE, (uint64_t)m_Handle, name); -} - -#include "DeviceSemaphoreVK.hpp" \ No newline at end of file diff --git a/Source/VK/DeviceSemaphoreVK.h b/Source/VK/DeviceSemaphoreVK.h deleted file mode 100644 index 55947278..00000000 --- a/Source/VK/DeviceSemaphoreVK.h +++ /dev/null @@ -1,49 +0,0 @@ -/* -Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. - -NVIDIA CORPORATION and its licensors retain all intellectual property -and proprietary rights in and to this software, related documentation -and any modifications thereto. Any use, reproduction, disclosure or -distribution of this software and related documentation without an express -license agreement from NVIDIA CORPORATION is strictly prohibited. -*/ - -#pragma once - -namespace nri -{ - struct DeviceVK; - - struct DeviceSemaphoreVK - { - DeviceSemaphoreVK(DeviceVK& device); - ~DeviceSemaphoreVK(); - - operator VkFence() const; - DeviceVK& GetDevice() const; - Result Create(bool signaled); - Result Create(NRIVkFence vkFence); - - void SetDebugName(const char* name); - - private: - VkFence m_Handle = VK_NULL_HANDLE; - DeviceVK& m_Device; - bool m_OwnsNativeObjects = false; - }; - - inline DeviceSemaphoreVK::DeviceSemaphoreVK(DeviceVK& device) : - m_Device(device) - { - } - - inline DeviceSemaphoreVK::operator VkFence() const - { - return m_Handle; - } - - inline DeviceVK& DeviceSemaphoreVK::GetDevice() const - { - return m_Device; - } -} \ No newline at end of file diff --git a/Source/VK/DeviceSemaphoreVK.hpp b/Source/VK/DeviceSemaphoreVK.hpp deleted file mode 100644 index 8f6073d0..00000000 --- a/Source/VK/DeviceSemaphoreVK.hpp +++ /dev/null @@ -1,25 +0,0 @@ -/* -Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. - -NVIDIA CORPORATION and its licensors retain all intellectual property -and proprietary rights in and to this software, related documentation -and any modifications thereto. Any use, reproduction, disclosure or -distribution of this software and related documentation without an express -license agreement from NVIDIA CORPORATION is strictly prohibited. -*/ - -#pragma once - -#pragma region [ CoreInterface ] - -static void NRI_CALL SetDeviceSemaphoreDebugName(DeviceSemaphore& deviceSemaphore, const char* name) -{ - ((DeviceSemaphoreVK&)deviceSemaphore).SetDebugName(name); -} - -void FillFunctionTableDeviceSemaphoreVK(CoreInterface& coreInterface) -{ - coreInterface.SetDeviceSemaphoreDebugName = ::SetDeviceSemaphoreDebugName; -} - -#pragma endregion \ No newline at end of file diff --git a/Source/VK/DeviceVK.cpp b/Source/VK/DeviceVK.cpp index 8999a3bc..615ce2e5 100644 --- a/Source/VK/DeviceVK.cpp +++ b/Source/VK/DeviceVK.cpp @@ -9,7 +9,6 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. */ #include "SharedVK.h" -#include "DeviceVK.h" #include "CommandQueueVK.h" #include "CommandAllocatorVK.h" #include "CommandBufferVK.h" @@ -17,8 +16,7 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include "BufferVK.h" #include "DescriptorVK.h" #include "FrameBufferVK.h" -#include "DeviceSemaphoreVK.h" -#include "QueueSemaphoreVK.h" +#include "FenceVK.h" #include "SwapChainVK.h" #include "QueryPoolVK.h" #include "DescriptorPoolVK.h" @@ -28,12 +26,25 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include "AccelerationStructureVK.h" #include "MemoryVK.h" +constexpr uint32_t INVALID_FAMILY_INDEX = uint32_t(-1); + #if _WIN32 #include #endif using namespace nri; +inline bool IsExtensionSupported(const char* ext, const Vector& list) +{ + for (auto& e : list) + { + if (!strcmp(ext, e.extensionName)) + return true; + } + + return false; +} + void* VKAPI_PTR vkAllocateHostMemory(void* pUserData, size_t size, size_t alignment, VkSystemAllocationScope allocationScope) { MaybeUnused(allocationScope); @@ -127,14 +138,14 @@ DeviceVK::~DeviceVK() m_VK.DestroyInstance(m_Instance, m_AllocationCallbackPtr); } - if (m_Loader != nullptr) + if (m_Loader) UnloadSharedLibrary(*m_Loader); - m_Loader = nullptr; } Result DeviceVK::Create(const DeviceCreationDesc& deviceCreationDesc) { m_OwnsNativeObjects = true; + m_SPIRVBindingOffsets = deviceCreationDesc.spirvBindingOffsets; m_AllocationCallbacks.pUserData = &GetStdAllocator(); m_AllocationCallbacks.pfnAllocation = vkAllocateHostMemory; @@ -147,12 +158,13 @@ Result DeviceVK::Create(const DeviceCreationDesc& deviceCreationDesc) m_AllocationCallbackPtr = &m_AllocationCallbacks; m_Loader = LoadSharedLibrary(VULKAN_LOADER_NAME); - if (m_Loader == nullptr) + if (!m_Loader) { REPORT_ERROR(GetLog(), "Failed to load Vulkan loader: '%s'.", VULKAN_LOADER_NAME); return Result::UNSUPPORTED; } + // Create instance Result res = ResolvePreInstanceDispatchTable(); if (res != Result::SUCCESS) return res; @@ -161,6 +173,7 @@ Result DeviceVK::Create(const DeviceCreationDesc& deviceCreationDesc) if (res != Result::SUCCESS) return res; + // Create device res = ResolveInstanceDispatchTable(); if (res != Result::SUCCESS) return res; @@ -176,14 +189,11 @@ Result DeviceVK::Create(const DeviceCreationDesc& deviceCreationDesc) if (res != Result::SUCCESS) return res; - RetrieveRayTracingInfo(); - RetrieveMeshShaderInfo(); - CreateCommandQueues(); - res = ResolveDispatchTable(); if (res != Result::SUCCESS) return res; + CreateCommandQueues(); SetDeviceLimits(deviceCreationDesc.enableAPIValidation); const uint32_t groupSize = m_DeviceDesc.physicalDeviceNum; @@ -195,11 +205,6 @@ Result DeviceVK::Create(const DeviceCreationDesc& deviceCreationDesc) if (deviceCreationDesc.enableAPIValidation) ReportDeviceGroupInfo(); - m_SPIRVBindingOffsets = deviceCreationDesc.spirvBindingOffsets; - - m_IsConcurrentSharingModeEnabledForBuffers = m_IsConcurrentSharingModeEnabledForBuffers && m_ConcurrentSharingModeQueueIndices.size() > 1; - m_IsConcurrentSharingModeEnabledForImages = m_IsConcurrentSharingModeEnabledForImages && m_ConcurrentSharingModeQueueIndices.size() > 1; - FindDXGIAdapter(); return res; @@ -208,14 +213,11 @@ Result DeviceVK::Create(const DeviceCreationDesc& deviceCreationDesc) Result DeviceVK::Create(const DeviceCreationVulkanDesc& deviceCreationVulkanDesc) { m_OwnsNativeObjects = false; - - m_Instance = (VkInstance)deviceCreationVulkanDesc.vkInstance; + m_SPIRVBindingOffsets = deviceCreationVulkanDesc.spirvBindingOffsets; const VkPhysicalDevice* physicalDevices = (VkPhysicalDevice*)deviceCreationVulkanDesc.vkPhysicalDevices; m_PhysicalDevices.insert(m_PhysicalDevices.begin(), physicalDevices, physicalDevices + deviceCreationVulkanDesc.deviceGroupSize); - m_Device = (VkDevice)deviceCreationVulkanDesc.vkDevice; - m_AllocationCallbacks.pUserData = &GetStdAllocator(); m_AllocationCallbacks.pfnAllocation = vkAllocateHostMemory; m_AllocationCallbacks.pfnReallocation = vkReallocateHostMemory; @@ -226,56 +228,81 @@ Result DeviceVK::Create(const DeviceCreationVulkanDesc& deviceCreationVulkanDesc if (deviceCreationVulkanDesc.enableAPIValidation) m_AllocationCallbackPtr = &m_AllocationCallbacks; - const char* loaderPath = deviceCreationVulkanDesc.vulkanLoaderPath; - if (loaderPath == nullptr) - loaderPath = VULKAN_LOADER_NAME; - + const char* loaderPath = deviceCreationVulkanDesc.vulkanLoaderPath ? deviceCreationVulkanDesc.vulkanLoaderPath : VULKAN_LOADER_NAME; m_Loader = LoadSharedLibrary(loaderPath); - if (m_Loader == nullptr) + if (!m_Loader) { REPORT_ERROR(GetLog(), "Failed to load Vulkan loader: '%s'.", loaderPath); return Result::UNSUPPORTED; } - Vector extensions(GetStdAllocator()); - extensions.insert(extensions.end(), deviceCreationVulkanDesc.instanceExtensions, - deviceCreationVulkanDesc.instanceExtensions + deviceCreationVulkanDesc.instanceExtensionNum); - - CheckSupportedInstanceExtensions(extensions); - - extensions.clear(); - extensions.insert(extensions.end(), deviceCreationVulkanDesc.deviceExtensions, - deviceCreationVulkanDesc.deviceExtensions + deviceCreationVulkanDesc.deviceExtensionNum); - - CheckSupportedDeviceExtensions(extensions); - + // Create instance Result res = ResolvePreInstanceDispatchTable(); if (res != Result::SUCCESS) return res; + m_Instance = (VkInstance)deviceCreationVulkanDesc.vkInstance; + + // Create device res = ResolveInstanceDispatchTable(); if (res != Result::SUCCESS) return res; - m_VK.GetPhysicalDeviceMemoryProperties(m_PhysicalDevices.front(), &m_MemoryProps); - - FillFamilyIndices(true, deviceCreationVulkanDesc.queueFamilyIndices, deviceCreationVulkanDesc.queueFamilyIndexNum); - CreateCommandQueues(); + m_Device = (VkDevice)deviceCreationVulkanDesc.vkDevice; res = ResolveDispatchTable(); if (res != Result::SUCCESS) return res; + m_VK.GetPhysicalDeviceMemoryProperties(m_PhysicalDevices.front(), &m_MemoryProps); + + FillFamilyIndices(true, deviceCreationVulkanDesc.queueFamilyIndices, deviceCreationVulkanDesc.queueFamilyIndexNum); + + // Instance extensions + { + uint32_t extensionNum = 0; + m_VK.EnumerateInstanceExtensionProperties(nullptr, &extensionNum, nullptr); + + Vector supportedExts(extensionNum, GetStdAllocator()); + m_VK.EnumerateInstanceExtensionProperties(nullptr, &extensionNum, supportedExts.data()); + + if (IsExtensionSupported(VK_EXT_DEBUG_UTILS_EXTENSION_NAME, supportedExts)) + m_IsDebugUtilsSupported = true; + } + + // Device extensions + { + uint32_t extensionNum = 0; + m_VK.EnumerateDeviceExtensionProperties(m_PhysicalDevices.front(), nullptr, &extensionNum, nullptr); + + Vector supportedExts(extensionNum, GetStdAllocator()); + m_VK.EnumerateDeviceExtensionProperties(m_PhysicalDevices.front(), nullptr, &extensionNum, supportedExts.data()); + + if (IsExtensionSupported(VK_KHR_ACCELERATION_STRUCTURE_EXTENSION_NAME, supportedExts)) + m_IsRayTracingExtSupported = true; + + if (IsExtensionSupported(VK_EXT_SAMPLE_LOCATIONS_EXTENSION_NAME, supportedExts)) + m_IsSampleLocationExtSupported = true; + + if (IsExtensionSupported(VK_EXT_CONSERVATIVE_RASTERIZATION_EXTENSION_NAME, supportedExts)) + m_IsConservativeRasterExtSupported = true; + + if (IsExtensionSupported(VK_EXT_HDR_METADATA_EXTENSION_NAME, supportedExts)) + m_IsHDRExtSupported = true; + + if (IsExtensionSupported(VK_EXT_OPACITY_MICROMAP_EXTENSION_NAME, supportedExts)) + m_IsMicroMapSupported = true; + + if (IsExtensionSupported(VK_EXT_MESH_SHADER_EXTENSION_NAME, supportedExts)) + m_IsMeshShaderExtSupported = true; + } + + CreateCommandQueues(); SetDeviceLimits(deviceCreationVulkanDesc.enableAPIValidation); if (deviceCreationVulkanDesc.enableAPIValidation) ReportDeviceGroupInfo(); - m_SPIRVBindingOffsets = deviceCreationVulkanDesc.spirvBindingOffsets; - - m_IsConcurrentSharingModeEnabledForBuffers = m_IsConcurrentSharingModeEnabledForBuffers && m_ConcurrentSharingModeQueueIndices.size() > 1; - m_IsConcurrentSharingModeEnabledForImages = m_IsConcurrentSharingModeEnabledForImages && m_ConcurrentSharingModeQueueIndices.size() > 1; - FindDXGIAdapter(); return res; @@ -318,9 +345,7 @@ bool DeviceVK::GetMemoryType(MemoryLocation memoryLocation, uint32_t memoryTypeM VkMemoryPropertyFlags unwantedFlags = IsHostVisibleMemory(memoryLocation) ? hostUnwantedFlags : deviceUnwantedFlags; memoryTypeInfo.isHostCoherent = IsHostVisibleMemory(memoryLocation); - memoryTypeInfo.location = (uint8_t)memoryLocation; - static_assert((uint32_t)MemoryLocation::MAX_NUM <= std::numeric_limits::max(), "Unexpected number of memory locations"); for (uint_fast16_t i = 0; i < m_MemoryProps.memoryTypeCount; i++) { @@ -368,1901 +393,1721 @@ bool DeviceVK::GetMemoryType(uint32_t index, MemoryTypeInfo& memoryTypeInfo) con return true; } -inline void DeviceVK::SetDebugName(const char* name) +const char* GetObjectTypeName(VkObjectType objectType) { - SetDebugNameToTrivialObject(VK_OBJECT_TYPE_DEVICE, (uint64_t)m_Device, name); + switch(objectType) + { + case VK_OBJECT_TYPE_INSTANCE: + return "VkInstance"; + case VK_OBJECT_TYPE_PHYSICAL_DEVICE: + return "VkPhysicalDevice"; + case VK_OBJECT_TYPE_DEVICE: + return "VkDevice"; + case VK_OBJECT_TYPE_QUEUE: + return "VkQueue"; + case VK_OBJECT_TYPE_SEMAPHORE: + return "VkSemaphore"; + case VK_OBJECT_TYPE_COMMAND_BUFFER: + return "VkCommandBuffer"; + case VK_OBJECT_TYPE_FENCE: + return "VkFence"; + case VK_OBJECT_TYPE_DEVICE_MEMORY: + return "VkDeviceMemory"; + case VK_OBJECT_TYPE_BUFFER: + return "VkBuffer"; + case VK_OBJECT_TYPE_IMAGE: + return "VkImage"; + case VK_OBJECT_TYPE_EVENT: + return "VkEvent"; + case VK_OBJECT_TYPE_QUERY_POOL: + return "VkQueryPool"; + case VK_OBJECT_TYPE_BUFFER_VIEW: + return "VkBufferView"; + case VK_OBJECT_TYPE_IMAGE_VIEW: + return "VkImageView"; + case VK_OBJECT_TYPE_SHADER_MODULE: + return "VkShaderModule"; + case VK_OBJECT_TYPE_PIPELINE_CACHE: + return "VkPipelineCache"; + case VK_OBJECT_TYPE_PIPELINE_LAYOUT: + return "VkPipelineLayout"; + case VK_OBJECT_TYPE_RENDER_PASS: + return "VkRenderPass"; + case VK_OBJECT_TYPE_PIPELINE: + return "VkPipeline"; + case VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT: + return "VkDescriptorSetLayout"; + case VK_OBJECT_TYPE_SAMPLER: + return "VkSampler"; + case VK_OBJECT_TYPE_DESCRIPTOR_POOL: + return "VkDescriptorPool"; + case VK_OBJECT_TYPE_DESCRIPTOR_SET: + return "VkDescriptorSet"; + case VK_OBJECT_TYPE_FRAMEBUFFER: + return "VkFramebuffer"; + case VK_OBJECT_TYPE_COMMAND_POOL: + return "VkCommandPool"; + case VK_OBJECT_TYPE_SAMPLER_YCBCR_CONVERSION: + return "VkSamplerYcbcrConversion"; + case VK_OBJECT_TYPE_DESCRIPTOR_UPDATE_TEMPLATE: + return "VkDescriptorUpdateTemplate"; + case VK_OBJECT_TYPE_SURFACE_KHR: + return "VkSurfaceKHR"; + case VK_OBJECT_TYPE_SWAPCHAIN_KHR: + return "VkSwapchainKHR"; + case VK_OBJECT_TYPE_DISPLAY_KHR: + return "VkDisplayKHR"; + case VK_OBJECT_TYPE_DISPLAY_MODE_KHR: + return "VkDisplayModeKHR"; + case VK_OBJECT_TYPE_DEBUG_REPORT_CALLBACK_EXT: + return "VkDebugReportCallbackEXT"; + case VK_OBJECT_TYPE_DEBUG_UTILS_MESSENGER_EXT: + return "VkDebugUtilsMessengerEXT"; + case VK_OBJECT_TYPE_ACCELERATION_STRUCTURE_KHR: + return "VkAccelerationStructureKHR"; + case VK_OBJECT_TYPE_VALIDATION_CACHE_EXT: + return "VkValidationCacheEXT"; + case VK_OBJECT_TYPE_DEFERRED_OPERATION_KHR: + return "VkDeferredOperationKHR"; + default: + return "unknown"; + } } -inline Result DeviceVK::GetCommandQueue(CommandQueueType commandQueueType, CommandQueue*& commandQueue) +VkBool32 VKAPI_PTR DebugUtilsMessenger( + VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, + VkDebugUtilsMessageTypeFlagsEXT messageType, + const VkDebugUtilsMessengerCallbackDataEXT* callbackData, + void* userData) { - SharedScope sharedScope(m_Lock); + MaybeUnused(messageType); - if (m_FamilyIndices[(uint32_t)commandQueueType] == std::numeric_limits::max()) - return Result::UNSUPPORTED; + bool isError = false; + bool isWarning = false; - commandQueue = (CommandQueue*)m_Queues[(uint32_t)commandQueueType]; - return Result::SUCCESS; -} + /* + // TODO: convert an error to a warning as + if (callbackData->messageIdNumber == ) + messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT; + */ -inline Result DeviceVK::CreateCommandAllocator(const CommandQueue& commandQueue, uint32_t physicalDeviceMask, CommandAllocator*& commandAllocator) -{ - return CreateImplementation(commandAllocator, commandQueue, physicalDeviceMask); -} + const char* type = "unknown"; + switch( messageSeverity ) + { + case VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT: + type = "verbose"; + break; + case VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT: + type = "info"; + break; + case VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT: + type = "warning"; + isWarning = true; + break; + case VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT: + type = "error"; + isError = true; + break; + } -inline Result DeviceVK::CreateDescriptorPool(const DescriptorPoolDesc& descriptorPoolDesc, DescriptorPool*& descriptorPool) -{ - return CreateImplementation(descriptorPool, descriptorPoolDesc); -} + if (!isWarning && !isError) + return VK_FALSE; -Result DeviceVK::CreateBuffer(const BufferDesc& bufferDesc, Buffer*& buffer) // TODO: not inline -{ - return CreateImplementation(buffer, bufferDesc); -} + DeviceVK& device = *(DeviceVK*)userData; -inline Result DeviceVK::CreateTexture(const TextureDesc& textureDesc, Texture*& texture) -{ - return CreateImplementation(texture, textureDesc); -} + String message(device.GetStdAllocator()); + message += std::to_string(callbackData->messageIdNumber); + message += " "; + message += callbackData->pMessageIdName; + message += " "; + message += callbackData->pMessage; -inline Result DeviceVK::CreateBufferView(const BufferViewDesc& bufferViewDesc, Descriptor*& bufferView) -{ - return CreateImplementation(bufferView, bufferViewDesc); -} + // vkCmdCopyBufferToImage: For optimal performance VkImage 0x984b920000000104 layout should be VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL instead of GENERAL. + if (callbackData->messageIdNumber == 1303270965) + return VK_FALSE; -inline Result DeviceVK::CreateTexture1DView(const Texture1DViewDesc& textureViewDesc, Descriptor*& textureView) -{ - return CreateImplementation(textureView, textureViewDesc); -} + if (messageSeverity == VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) + { + message += "\nObjectNum: " + std::to_string(callbackData->objectCount); -inline Result DeviceVK::CreateTexture2DView(const Texture2DViewDesc& textureViewDesc, Descriptor*& textureView) -{ - return CreateImplementation(textureView, textureViewDesc); -} + char buffer[64]; -inline Result DeviceVK::CreateTexture3DView(const Texture3DViewDesc& textureViewDesc, Descriptor*& textureView) -{ - return CreateImplementation(textureView, textureViewDesc); -} + for (uint32_t i = 0; i < callbackData->objectCount; i++) + { + const VkDebugUtilsObjectNameInfoEXT& object = callbackData->pObjects[i]; + snprintf(buffer, sizeof(buffer), "0x%llx", (unsigned long long)object.objectHandle); -inline Result DeviceVK::CreateSampler(const SamplerDesc& samplerDesc, Descriptor*& sampler) -{ - return CreateImplementation(sampler, samplerDesc); -} + message += "\n\tObject "; + message += object.pObjectName != nullptr ? object.pObjectName : ""; + message += " "; + message += GetObjectTypeName(object.objectType); + message += " ("; + message += buffer; + message += ")"; + } -inline Result DeviceVK::CreatePipelineLayout(const PipelineLayoutDesc& pipelineLayoutDesc, PipelineLayout*& pipelineLayout) -{ - return CreateImplementation(pipelineLayout, pipelineLayoutDesc); -} + REPORT_ERROR(device.GetLog(), "DebugUtilsMessenger: %s, %s", type, message.c_str()); + } + else if (messageSeverity == VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) + REPORT_WARNING(device.GetLog(), "DebugUtilsMessenger: %s, %s", type, message.c_str()); + else + REPORT_INFO(device.GetLog(), "DebugUtilsMessenger: %s, %s", type, message.c_str()); -inline Result DeviceVK::CreatePipeline(const GraphicsPipelineDesc& graphicsPipelineDesc, Pipeline*& pipeline) -{ - return CreateImplementation(pipeline, graphicsPipelineDesc); + return VK_FALSE; } -inline Result DeviceVK::CreatePipeline(const ComputePipelineDesc& computePipelineDesc, Pipeline*& pipeline) +void DeviceVK::FilterInstanceLayers(Vector& layers) { - return CreateImplementation(pipeline, computePipelineDesc); -} + uint32_t layerNum = 0; + m_VK.EnumerateInstanceLayerProperties(&layerNum, nullptr); -inline Result DeviceVK::CreateFrameBuffer(const FrameBufferDesc& frameBufferDesc, FrameBuffer*& frameBuffer) -{ - return CreateImplementation(frameBuffer, frameBufferDesc); -} + Vector supportedLayers(layerNum, GetStdAllocator()); + m_VK.EnumerateInstanceLayerProperties(&layerNum, supportedLayers.data()); -inline Result DeviceVK::CreateQueryPool(const QueryPoolDesc& queryPoolDesc, QueryPool*& queryPool) -{ - return CreateImplementation(queryPool, queryPoolDesc); -} + for (size_t i = 0; i < layers.size(); i++) + { + bool found = false; + for (uint32_t j = 0; j < layerNum && !found; j++) + { + if (strcmp(supportedLayers[j].layerName, layers[i]) == 0) + found = true; + } -inline Result DeviceVK::CreateQueueSemaphore(QueueSemaphore*& queueSemaphore) -{ - return CreateImplementation(queueSemaphore); + if (!found) + layers.erase(layers.begin() + i--); + } } -inline Result DeviceVK::CreateDeviceSemaphore(bool signaled, DeviceSemaphore*& deviceSemaphore) +Result DeviceVK::CreateInstance(const DeviceCreationDesc& deviceCreationDesc) { - return CreateImplementation(deviceSemaphore, signaled); -} + Vector layers(GetStdAllocator()); + if (deviceCreationDesc.enableAPIValidation) + layers.push_back("VK_LAYER_KHRONOS_validation"); -inline Result DeviceVK::CreateSwapChain(const SwapChainDesc& swapChainDesc, SwapChain*& swapChain) -{ - return CreateImplementation(swapChain, swapChainDesc); -} + FilterInstanceLayers(layers); -inline Result DeviceVK::CreatePipeline(const RayTracingPipelineDesc& rayTracingPipelineDesc, Pipeline*& pipeline) -{ - return CreateImplementation(pipeline, rayTracingPipelineDesc); -} + uint32_t extensionNum = 0; + m_VK.EnumerateInstanceExtensionProperties(nullptr, &extensionNum, nullptr); -inline Result DeviceVK::CreateAccelerationStructure(const AccelerationStructureDesc& accelerationStructureDesc, AccelerationStructure*& accelerationStructure) -{ - return CreateImplementation(accelerationStructure, accelerationStructureDesc); -} + Vector supportedExts(extensionNum, GetStdAllocator()); + m_VK.EnumerateInstanceExtensionProperties(nullptr, &extensionNum, supportedExts.data()); -inline Result DeviceVK::CreateCommandQueue(const CommandQueueVulkanDesc& commandQueueVulkanDesc, CommandQueue*& commandQueue) -{ - const uint32_t commandQueueTypeIndex = (uint32_t)commandQueueVulkanDesc.commandQueueType; + Vector extensions(GetStdAllocator()); + for (uint32_t i = 0; i < deviceCreationDesc.vulkanExtensions.instanceExtensionNum; i++) + extensions.push_back(deviceCreationDesc.vulkanExtensions.instanceExtensions[i]); - ExclusiveScope exclusiveScope(m_Lock); + extensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME); + #ifdef VK_USE_PLATFORM_WIN32_KHR + extensions.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME); + #endif + #ifdef VK_USE_PLATFORM_METAL_EXT + extensions.push_back(VK_EXT_METAL_SURFACE_EXTENSION_NAME); + #endif + #ifdef VK_USE_PLATFORM_XLIB_KHR + extensions.push_back(VK_KHR_XLIB_SURFACE_EXTENSION_NAME); + #endif + #ifdef VK_USE_PLATFORM_WAYLAND_KHR + extensions.push_back(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME); + #endif - const bool isFamilyIndexSame = m_FamilyIndices[commandQueueTypeIndex] == commandQueueVulkanDesc.familyIndex; - const bool isQueueSame = (VkQueue)m_Queues[commandQueueTypeIndex] == (VkQueue)commandQueueVulkanDesc.vkQueue; - if (isFamilyIndexSame && isQueueSame) + if (IsExtensionSupported(VK_EXT_DEBUG_UTILS_EXTENSION_NAME, supportedExts)) { - commandQueue = (CommandQueue*)m_Queues[commandQueueTypeIndex]; - return Result::SUCCESS; + extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME); + m_IsDebugUtilsSupported = true; } - CreateImplementation(commandQueue, commandQueueVulkanDesc); + const VkApplicationInfo appInfo = { + VK_STRUCTURE_TYPE_APPLICATION_INFO, + nullptr, + nullptr, + 0, + nullptr, + 0, + VK_API_VERSION_1_3 + }; - if (m_Queues[commandQueueTypeIndex] != nullptr) - Deallocate(GetStdAllocator(), m_Queues[commandQueueTypeIndex]); + const VkInstanceCreateInfo info = { + VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, + nullptr, + (VkInstanceCreateFlags)0, + &appInfo, + (uint32_t)layers.size(), + layers.data(), + (uint32_t)extensions.size(), + extensions.data(), + }; - m_FamilyIndices[commandQueueTypeIndex] = commandQueueVulkanDesc.familyIndex; - m_Queues[commandQueueTypeIndex] = (CommandQueueVK*)commandQueue; + VkResult result = m_VK.CreateInstance(&info, m_AllocationCallbackPtr, &m_Instance); - return Result::SUCCESS; -} + RETURN_ON_FAILURE(GetLog(), result == VK_SUCCESS, GetReturnCode(result), + "Can't create a VkInstance: vkCreateInstance returned %d.", (int32_t)result); -inline Result DeviceVK::CreateCommandAllocator(const CommandAllocatorVulkanDesc& commandAllocatorVulkanDesc, CommandAllocator*& commandAllocator) -{ - return CreateImplementation(commandAllocator, commandAllocatorVulkanDesc); -} + if (deviceCreationDesc.enableAPIValidation) + { + typedef PFN_vkCreateDebugUtilsMessengerEXT Func; + Func vkCreateDebugUtilsMessengerEXT = nullptr; + vkCreateDebugUtilsMessengerEXT = (Func)m_VK.GetInstanceProcAddr(m_Instance, "vkCreateDebugUtilsMessengerEXT"); -inline Result DeviceVK::CreateCommandBuffer(const CommandBufferVulkanDesc& commandBufferVulkanDesc, CommandBuffer*& commandBuffer) -{ - return CreateImplementation(commandBuffer, commandBufferVulkanDesc); -} + VkDebugUtilsMessengerCreateInfoEXT createInfo = { VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT }; -inline Result DeviceVK::CreateDescriptorPool(NRIVkDescriptorPool vkDescriptorPool, DescriptorPool*& descriptorPool) -{ - return CreateImplementation(descriptorPool, vkDescriptorPool); -} + createInfo.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT; + createInfo.messageSeverity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT; -inline Result DeviceVK::CreateBuffer(const BufferVulkanDesc& bufferDesc, Buffer*& buffer) -{ - return CreateImplementation(buffer, bufferDesc); -} + createInfo.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT; + createInfo.messageType |= VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT; -inline Result DeviceVK::CreateTexture(const TextureVulkanDesc& textureVulkanDesc, Texture*& texture) -{ - return CreateImplementation(texture, textureVulkanDesc); -} + createInfo.pUserData = this; + createInfo.pfnUserCallback = DebugUtilsMessenger; -inline Result DeviceVK::CreateMemory(const MemoryVulkanDesc& memoryVulkanDesc, Memory*& memory) -{ - return CreateImplementation(memory, memoryVulkanDesc); + result = vkCreateDebugUtilsMessengerEXT(m_Instance, &createInfo, m_AllocationCallbackPtr, &m_Messenger); + + RETURN_ON_FAILURE(GetLog(), result == VK_SUCCESS, GetReturnCode(result), + "Can't create a debug utils messenger callback: vkCreateDebugUtilsMessengerEXT returned %d.", (int32_t)result); + } + + return Result::SUCCESS; } -inline Result DeviceVK::CreateGraphicsPipeline(NRIVkPipeline vkPipeline, Pipeline*& pipeline) +Result DeviceVK::FindPhysicalDeviceGroup(const PhysicalDeviceGroup* physicalDeviceGroup, bool enableMGPU) { - PipelineVK* implementation = Allocate(GetStdAllocator(), *this); - const Result result = implementation->CreateGraphics(vkPipeline); + uint32_t deviceGroupNum = 0; + m_VK.EnumeratePhysicalDeviceGroups(m_Instance, &deviceGroupNum, nullptr); - if (result != Result::SUCCESS) + VkPhysicalDeviceGroupProperties* deviceGroups = STACK_ALLOC(VkPhysicalDeviceGroupProperties, deviceGroupNum); + VkResult result = m_VK.EnumeratePhysicalDeviceGroups(m_Instance, &deviceGroupNum, deviceGroups); + + RETURN_ON_FAILURE(GetLog(), result == VK_SUCCESS, GetReturnCode(result), + "Can't enumerate physical devices: vkEnumeratePhysicalDevices returned %d.", (int32_t)result); + + VkPhysicalDeviceIDProperties idProps = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES }; + VkPhysicalDeviceProperties2 props = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2 }; + props.pNext = &idProps; + + bool isVulkan11Supported = false; + + uint32_t i = 0; + for (; i < deviceGroupNum && m_PhysicalDevices.empty(); i++) { - pipeline = (Pipeline*)implementation; - return result; - } + const VkPhysicalDeviceGroupProperties& group = deviceGroups[i]; + m_VK.GetPhysicalDeviceProperties2(group.physicalDevices[0], &props); - Deallocate(GetStdAllocator(), implementation); + const uint32_t majorVersion = VK_VERSION_MAJOR(props.properties.apiVersion); + const uint32_t minorVersion = VK_VERSION_MINOR(props.properties.apiVersion); + isVulkan11Supported = majorVersion > 1 || (majorVersion == 1 && minorVersion >= 1); - return result; -} + if (physicalDeviceGroup) + { + const uint64_t luid = *(uint64_t*)idProps.deviceLUID; + if (luid == physicalDeviceGroup->luid) + { + RETURN_ON_FAILURE(GetLog(), isVulkan11Supported, Result::UNSUPPORTED, + "Can't create a device: the specified physical device does not support Vulkan 1.1."); + break; + } + } + else + { + if (isVulkan11Supported) + break; + } + } -inline Result DeviceVK::CreateComputePipeline(NRIVkPipeline vkPipeline, Pipeline*& pipeline) -{ - PipelineVK* implementation = Allocate(GetStdAllocator(), *this); - const Result result = implementation->CreateCompute(vkPipeline); + RETURN_ON_FAILURE(GetLog(), i != deviceGroupNum, Result::UNSUPPORTED, + "Can't create a device: physical device not found."); - if (result != Result::SUCCESS) + const VkPhysicalDeviceGroupProperties& group = deviceGroups[i]; + + m_IsSubsetAllocationSupported = true; + if (group.subsetAllocation == VK_FALSE && group.physicalDeviceCount > 1) { - pipeline = (Pipeline*)implementation; - return result; + m_IsSubsetAllocationSupported = false; + REPORT_WARNING(GetLog(), "The device group does not support memory allocation on a subset of the physical devices."); } - Deallocate(GetStdAllocator(), implementation); + m_PhysicalDevices.insert(m_PhysicalDevices.begin(), group.physicalDevices, group.physicalDevices + group.physicalDeviceCount); - return result; -} + if (!enableMGPU) + m_PhysicalDevices.resize(1); -inline Result DeviceVK::CreateQueryPool(const QueryPoolVulkanDesc& queryPoolVulkanDesc, QueryPool*& queryPool) -{ - return CreateImplementation(queryPool, queryPoolVulkanDesc); + return Result::SUCCESS; } -inline Result DeviceVK::CreateQueueSemaphore(NRIVkSemaphore vkSemaphore, QueueSemaphore*& queueSemaphore) +void DeviceVK::SetDeviceLimits(bool enableValidation) { - return CreateImplementation(queueSemaphore, vkSemaphore); -} + uint32_t familyNum = 0; + m_VK.GetPhysicalDeviceQueueFamilyProperties(m_PhysicalDevices.front(), &familyNum, nullptr); -inline Result DeviceVK::CreateDeviceSemaphore(NRIVkFence vkFence, DeviceSemaphore*& deviceSemaphore) -{ - return CreateImplementation(deviceSemaphore, vkFence); -} + Vector familyProperties(familyNum, m_StdAllocator); + m_VK.GetPhysicalDeviceQueueFamilyProperties(m_PhysicalDevices.front(), &familyNum, familyProperties.data()); -inline Result DeviceVK::CreateAccelerationStructure(const AccelerationStructureVulkanDesc& accelerationStructureDesc, AccelerationStructure*& accelerationStructure) -{ - return CreateImplementation(accelerationStructure, accelerationStructureDesc); -} + uint32_t copyQueueTimestampValidBits = 0; + const uint32_t copyQueueFamilyIndex = m_FamilyIndices[(uint32_t)CommandQueueType::COPY]; + if (copyQueueFamilyIndex != INVALID_FAMILY_INDEX) + copyQueueTimestampValidBits = familyProperties[copyQueueFamilyIndex].timestampValidBits; -inline void DeviceVK::DestroyCommandAllocator(CommandAllocator& commandAllocator) -{ - Deallocate(GetStdAllocator(), (CommandAllocatorVK*)&commandAllocator); -} + VkPhysicalDeviceIDProperties deviceIDProps = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES }; + VkPhysicalDeviceProperties2 props = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, &deviceIDProps }; -inline void DeviceVK::DestroyDescriptorPool(DescriptorPool& descriptorPool) -{ - Deallocate(GetStdAllocator(), (DescriptorPoolVK*)&descriptorPool); -} + m_VK.GetPhysicalDeviceProperties2(m_PhysicalDevices.front(), &props); + const VkPhysicalDeviceLimits& limits = props.properties.limits; -void DeviceVK::DestroyBuffer(Buffer& buffer) // TODO: not inline -{ - Deallocate(GetStdAllocator(), (BufferVK*)&buffer); -} + VkPhysicalDeviceFeatures features = {}; + m_VK.GetPhysicalDeviceFeatures(m_PhysicalDevices.front(), &features); -inline void DeviceVK::DestroyTexture(Texture& texture) -{ - Deallocate(GetStdAllocator(), (TextureVK*)&texture); -} + VkPhysicalDeviceVulkan12Features features12 = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES }; + VkPhysicalDeviceFeatures2 features2 = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, &features12 }; -inline void DeviceVK::DestroyDescriptor(Descriptor& descriptor) -{ - Deallocate(GetStdAllocator(), (DescriptorVK*)&descriptor); -} + m_VK.GetPhysicalDeviceFeatures2(m_PhysicalDevices.front(), &features2); -inline void DeviceVK::DestroyPipelineLayout(PipelineLayout& pipelineLayout) -{ - Deallocate(GetStdAllocator(), (PipelineLayoutVK*)&pipelineLayout); -} + m_IsDescriptorIndexingSupported = features12.descriptorIndexing ? true : false; + m_IsBufferDeviceAddressSupported = features12.bufferDeviceAddress ? true : false; -inline void DeviceVK::DestroyPipeline(Pipeline& pipeline) -{ - Deallocate(GetStdAllocator(), (PipelineVK*)&pipeline); -} + static_assert(VK_LUID_SIZE == sizeof(uint64_t), "invalid sizeof"); + m_LUID = *(uint64_t*)deviceIDProps.deviceLUID; -inline void DeviceVK::DestroyFrameBuffer(FrameBuffer& frameBuffer) -{ - Deallocate(GetStdAllocator(), (FrameBufferVK*)&frameBuffer); -} + m_DeviceDesc.graphicsAPI = GraphicsAPI::VULKAN; + m_DeviceDesc.vendor = GetVendorFromID(props.properties.vendorID); + m_DeviceDesc.nriVersionMajor = NRI_VERSION_MAJOR; + m_DeviceDesc.nriVersionMinor = NRI_VERSION_MINOR; -inline void DeviceVK::DestroyQueryPool(QueryPool& queryPool) -{ - Deallocate(GetStdAllocator(), (QueryPoolVK*)&queryPool); -} + m_DeviceDesc.viewportMaxNum = limits.maxViewports; + m_DeviceDesc.viewportSubPixelBits = limits.viewportSubPixelBits; + m_DeviceDesc.viewportBoundsRange[0] = int32_t(limits.viewportBoundsRange[0]); + m_DeviceDesc.viewportBoundsRange[1] = int32_t(limits.viewportBoundsRange[1]); -inline void DeviceVK::DestroyQueueSemaphore(QueueSemaphore& queueSemaphore) -{ - Deallocate(GetStdAllocator(), (QueueSemaphoreVK*)&queueSemaphore); -} + m_DeviceDesc.frameBufferMaxDim = std::min(limits.maxFramebufferWidth, limits.maxFramebufferHeight); + m_DeviceDesc.frameBufferLayerMaxNum = limits.maxFramebufferLayers; + m_DeviceDesc.framebufferColorAttachmentMaxNum = limits.maxColorAttachments; -inline void DeviceVK::DestroyDeviceSemaphore(DeviceSemaphore& deviceSemaphore) -{ - Deallocate(GetStdAllocator(), (DeviceSemaphoreVK*)&deviceSemaphore); -} + m_DeviceDesc.frameBufferColorSampleMaxNum = (uint8_t)(limits.framebufferColorSampleCounts); + m_DeviceDesc.frameBufferDepthSampleMaxNum = (uint8_t)(limits.framebufferDepthSampleCounts); + m_DeviceDesc.frameBufferStencilSampleMaxNum = (uint8_t)(limits.framebufferStencilSampleCounts); + m_DeviceDesc.frameBufferNoAttachmentsSampleMaxNum = (uint8_t)(limits.framebufferNoAttachmentsSampleCounts); + m_DeviceDesc.textureColorSampleMaxNum = (uint8_t)(limits.sampledImageColorSampleCounts); + m_DeviceDesc.textureIntegerSampleMaxNum = (uint8_t)(limits.sampledImageIntegerSampleCounts); + m_DeviceDesc.textureDepthSampleMaxNum = (uint8_t)(limits.sampledImageDepthSampleCounts); + m_DeviceDesc.textureStencilSampleMaxNum = (uint8_t)(limits.sampledImageStencilSampleCounts); + m_DeviceDesc.storageTextureSampleMaxNum = (uint8_t)(limits.storageImageSampleCounts); -inline void DeviceVK::DestroySwapChain(SwapChain& swapChain) -{ - Deallocate(GetStdAllocator(), (SwapChainVK*)&swapChain); -} + m_DeviceDesc.texture1DMaxDim = limits.maxImageDimension1D; + m_DeviceDesc.texture2DMaxDim = limits.maxImageDimension2D; + m_DeviceDesc.texture3DMaxDim = limits.maxImageDimension3D; + m_DeviceDesc.textureArrayMaxDim = limits.maxImageArrayLayers; + m_DeviceDesc.texelBufferMaxDim = limits.maxTexelBufferElements; -inline void DeviceVK::DestroyAccelerationStructure(AccelerationStructure& accelerationStructure) -{ - Deallocate(GetStdAllocator(), (AccelerationStructureVK*)&accelerationStructure); -} + m_DeviceDesc.memoryAllocationMaxNum = limits.maxMemoryAllocationCount; + m_DeviceDesc.samplerAllocationMaxNum = limits.maxSamplerAllocationCount; + m_DeviceDesc.uploadBufferTextureRowAlignment = 1; + m_DeviceDesc.uploadBufferTextureSliceAlignment = 1; + m_DeviceDesc.typedBufferOffsetAlignment = (uint32_t)limits.minTexelBufferOffsetAlignment; + m_DeviceDesc.constantBufferOffsetAlignment = (uint32_t)limits.minUniformBufferOffsetAlignment; + m_DeviceDesc.constantBufferMaxRange = limits.maxUniformBufferRange; + m_DeviceDesc.storageBufferOffsetAlignment = (uint32_t)limits.minStorageBufferOffsetAlignment; + m_DeviceDesc.storageBufferMaxRange = limits.maxStorageBufferRange; + m_DeviceDesc.pushConstantsMaxSize = limits.maxPushConstantsSize; + m_DeviceDesc.bufferMaxSize = std::numeric_limits::max(); + m_DeviceDesc.bufferTextureGranularity = (uint32_t)limits.bufferImageGranularity; -inline Result DeviceVK::GetDisplays(Display** displays, uint32_t& displayNum) -{ - MaybeUnused(displays, displayNum); + m_DeviceDesc.boundDescriptorSetMaxNum = limits.maxBoundDescriptorSets; + m_DeviceDesc.perStageDescriptorSamplerMaxNum = limits.maxPerStageDescriptorSamplers; + m_DeviceDesc.perStageDescriptorConstantBufferMaxNum = limits.maxPerStageDescriptorUniformBuffers; + m_DeviceDesc.perStageDescriptorStorageBufferMaxNum = limits.maxPerStageDescriptorStorageBuffers; + m_DeviceDesc.perStageDescriptorTextureMaxNum = limits.maxPerStageDescriptorSampledImages; + m_DeviceDesc.perStageDescriptorStorageTextureMaxNum = limits.maxPerStageDescriptorStorageImages; + m_DeviceDesc.perStageResourceMaxNum = limits.maxPerStageResources; -#if _WIN32 - if (m_Adapter == nullptr) - return Result::UNSUPPORTED; + m_DeviceDesc.descriptorSetSamplerMaxNum = limits.maxDescriptorSetSamplers; + m_DeviceDesc.descriptorSetConstantBufferMaxNum = limits.maxDescriptorSetUniformBuffers; + m_DeviceDesc.descriptorSetStorageBufferMaxNum = limits.maxDescriptorSetStorageBuffers; + m_DeviceDesc.descriptorSetTextureMaxNum = limits.maxDescriptorSetSampledImages; + m_DeviceDesc.descriptorSetStorageTextureMaxNum = limits.maxDescriptorSetStorageImages; - HRESULT result = S_OK; + m_DeviceDesc.vertexShaderAttributeMaxNum = limits.maxVertexInputAttributes; + m_DeviceDesc.vertexShaderStreamMaxNum = limits.maxVertexInputBindings; + m_DeviceDesc.vertexShaderOutputComponentMaxNum = limits.maxVertexOutputComponents; - if (displays == nullptr || displayNum == 0) - { - UINT i = 0; - for(; result != DXGI_ERROR_NOT_FOUND; i++) - { - ComPtr output; - result = m_Adapter->EnumOutputs(i, &output); - } + m_DeviceDesc.tessControlShaderGenerationMaxLevel = (float)limits.maxTessellationGenerationLevel; + m_DeviceDesc.tessControlShaderPatchPointMaxNum = limits.maxTessellationPatchSize; + m_DeviceDesc.tessControlShaderPerVertexInputComponentMaxNum = limits.maxTessellationControlPerVertexInputComponents; + m_DeviceDesc.tessControlShaderPerVertexOutputComponentMaxNum = limits.maxTessellationControlPerVertexOutputComponents; + m_DeviceDesc.tessControlShaderPerPatchOutputComponentMaxNum = limits.maxTessellationControlPerPatchOutputComponents; + m_DeviceDesc.tessControlShaderTotalOutputComponentMaxNum = limits.maxTessellationControlTotalOutputComponents; - displayNum = i; - return Result::SUCCESS; - } + m_DeviceDesc.tessEvaluationShaderInputComponentMaxNum = limits.maxTessellationEvaluationInputComponents; + m_DeviceDesc.tessEvaluationShaderOutputComponentMaxNum = limits.maxTessellationEvaluationOutputComponents; - UINT i = 0; - for(; result != DXGI_ERROR_NOT_FOUND && i < displayNum; i++) - { - ComPtr output; - result = m_Adapter->EnumOutputs(i, &output); - if (result != DXGI_ERROR_NOT_FOUND) - displays[i] = (Display*)(size_t)(i + 1); - } + m_DeviceDesc.geometryShaderInvocationMaxNum = limits.maxGeometryShaderInvocations; + m_DeviceDesc.geometryShaderInputComponentMaxNum = limits.maxGeometryInputComponents; + m_DeviceDesc.geometryShaderOutputComponentMaxNum = limits.maxGeometryOutputComponents; + m_DeviceDesc.geometryShaderOutputVertexMaxNum = limits.maxGeometryOutputVertices; + m_DeviceDesc.geometryShaderTotalOutputComponentMaxNum = limits.maxGeometryTotalOutputComponents; - for(; i < displayNum; i++) - displays[i] = nullptr; + m_DeviceDesc.fragmentShaderInputComponentMaxNum = limits.maxFragmentInputComponents; + m_DeviceDesc.fragmentShaderOutputAttachmentMaxNum = limits.maxFragmentOutputAttachments; + m_DeviceDesc.fragmentShaderDualSourceAttachmentMaxNum = limits.maxFragmentDualSrcAttachments; + m_DeviceDesc.fragmentShaderCombinedOutputResourceMaxNum = limits.maxFragmentCombinedOutputResources; - return Result::SUCCESS; -#else - return Result::UNSUPPORTED; -#endif -} + m_DeviceDesc.computeShaderSharedMemoryMaxSize = limits.maxComputeSharedMemorySize; + m_DeviceDesc.computeShaderWorkGroupMaxNum[0] = limits.maxComputeWorkGroupCount[0]; + m_DeviceDesc.computeShaderWorkGroupMaxNum[1] = limits.maxComputeWorkGroupCount[1]; + m_DeviceDesc.computeShaderWorkGroupMaxNum[2] = limits.maxComputeWorkGroupCount[2]; + m_DeviceDesc.computeShaderWorkGroupInvocationMaxNum = limits.maxComputeWorkGroupInvocations; + m_DeviceDesc.computeShaderWorkGroupMaxDim[0] = limits.maxComputeWorkGroupSize[0]; + m_DeviceDesc.computeShaderWorkGroupMaxDim[1] = limits.maxComputeWorkGroupSize[1]; + m_DeviceDesc.computeShaderWorkGroupMaxDim[2] = limits.maxComputeWorkGroupSize[2]; -inline Result DeviceVK::GetDisplaySize(Display& display, uint16_t& width, uint16_t& height) -{ - MaybeUnused(display, width, height); + m_DeviceDesc.timestampFrequencyHz = uint64_t( 1e9 / double(limits.timestampPeriod) + 0.5 ); + m_DeviceDesc.subPixelPrecisionBits = limits.subPixelPrecisionBits; + m_DeviceDesc.subTexelPrecisionBits = limits.subTexelPrecisionBits; + m_DeviceDesc.mipmapPrecisionBits = limits.mipmapPrecisionBits; + m_DeviceDesc.drawIndexedIndex16ValueMax = std::min(std::numeric_limits::max(), limits.maxDrawIndexedIndexValue); + m_DeviceDesc.drawIndexedIndex32ValueMax = limits.maxDrawIndexedIndexValue; + m_DeviceDesc.drawIndirectMaxNum = limits.maxDrawIndirectCount; + m_DeviceDesc.samplerLodBiasMin = -limits.maxSamplerLodBias; + m_DeviceDesc.samplerLodBiasMax = limits.maxSamplerLodBias; + m_DeviceDesc.samplerAnisotropyMax = limits.maxSamplerAnisotropy; + m_DeviceDesc.texelOffsetMin = limits.minTexelOffset; + m_DeviceDesc.texelOffsetMax = limits.maxTexelOffset; + m_DeviceDesc.texelGatherOffsetMin = limits.minTexelGatherOffset; + m_DeviceDesc.texelGatherOffsetMax = limits.maxTexelGatherOffset; + m_DeviceDesc.clipDistanceMaxNum = limits.maxClipDistances; + m_DeviceDesc.cullDistanceMaxNum = limits.maxCullDistances; + m_DeviceDesc.combinedClipAndCullDistanceMaxNum = limits.maxCombinedClipAndCullDistances; + m_DeviceDesc.physicalDeviceNum = (uint8_t)m_PhysicalDevices.size(); -#if _WIN32 - if (m_Adapter == nullptr) - return Result::UNSUPPORTED; + m_DeviceDesc.isAPIValidationEnabled = enableValidation; + m_DeviceDesc.isTextureFilterMinMaxSupported = features12.samplerFilterMinmax; + m_DeviceDesc.isLogicOpSupported = features.logicOp; + m_DeviceDesc.isDepthBoundsTestSupported = features.depthBounds; + m_DeviceDesc.isProgrammableSampleLocationsSupported = m_IsSampleLocationExtSupported; + m_DeviceDesc.isComputeQueueSupported = m_Queues[(uint32_t)CommandQueueType::COMPUTE] != nullptr; + m_DeviceDesc.isCopyQueueSupported = m_Queues[(uint32_t)CommandQueueType::COPY] != nullptr; + m_DeviceDesc.isCopyQueueTimestampSupported = copyQueueTimestampValidBits == 64; + m_DeviceDesc.isRegisterAliasingSupported = true; + m_DeviceDesc.isSubsetAllocationSupported = m_IsSubsetAllocationSupported; + m_DeviceDesc.isFloat16Supported = features12.shaderFloat16; - Display* address = &display; + // Conservative raster + if (m_IsConservativeRasterExtSupported) + { + VkPhysicalDeviceConservativeRasterizationPropertiesEXT conservativeRasterProps = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CONSERVATIVE_RASTERIZATION_PROPERTIES_EXT }; - if (address == nullptr) - return Result::UNSUPPORTED; + props.pNext = &conservativeRasterProps; - const uint32_t index = (*(uint32_t*)&address) - 1; + m_VK.GetPhysicalDeviceProperties2(m_PhysicalDevices.front(), &props); - ComPtr output; - HRESULT result = m_Adapter->EnumOutputs(index, &output); + if ( conservativeRasterProps.fullyCoveredFragmentShaderInputVariable && conservativeRasterProps.primitiveOverestimationSize <= (1.0 / 256.0f) ) + m_DeviceDesc.conservativeRasterTier = 3; + else if ( conservativeRasterProps.degenerateTrianglesRasterized && conservativeRasterProps.primitiveOverestimationSize < (1.0f / 2.0f) ) + m_DeviceDesc.conservativeRasterTier = 2; + else + m_DeviceDesc.conservativeRasterTier = 1; + } - if (FAILED(result)) - return Result::UNSUPPORTED; + // Ray tracing + if (m_IsRayTracingExtSupported) + { + VkPhysicalDeviceRayTracingPipelinePropertiesKHR rayTracingProps = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_TRACING_PIPELINE_PROPERTIES_KHR }; + VkPhysicalDeviceAccelerationStructurePropertiesKHR accelerationStructureProperties = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ACCELERATION_STRUCTURE_PROPERTIES_KHR }; - DXGI_OUTPUT_DESC outputDesc = {}; - result = output->GetDesc(&outputDesc); + props.pNext = &rayTracingProps; + rayTracingProps.pNext = &accelerationStructureProperties; - if (FAILED(result)) - return Result::UNSUPPORTED; + m_VK.GetPhysicalDeviceProperties2(m_PhysicalDevices.front(), &props); - MONITORINFO monitorInfo = {}; - monitorInfo.cbSize = sizeof(monitorInfo); + m_DeviceDesc.rayTracingShaderGroupIdentifierSize = rayTracingProps.shaderGroupHandleSize; + m_DeviceDesc.rayTracingShaderRecursionMaxDepth = rayTracingProps.maxRayRecursionDepth; + m_DeviceDesc.rayTracingGeometryObjectMaxNum = (uint32_t)accelerationStructureProperties.maxGeometryCount; + m_DeviceDesc.rayTracingShaderTableAligment = rayTracingProps.shaderGroupBaseAlignment; + m_DeviceDesc.rayTracingShaderTableMaxStride = rayTracingProps.maxShaderGroupStride; + } - if (!GetMonitorInfoA(outputDesc.Monitor, &monitorInfo)) - return Result::UNSUPPORTED; + // Mesh shader + if (!m_IsMeshShaderExtSupported) + { + VkPhysicalDeviceMeshShaderPropertiesEXT meshShaderProps = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MESH_SHADER_PROPERTIES_EXT }; - const RECT rect = monitorInfo.rcMonitor; + props.pNext = &meshShaderProps; - width = uint16_t(rect.right - rect.left); - height = uint16_t(rect.bottom - rect.top); + m_VK.GetPhysicalDeviceProperties2(m_PhysicalDevices.front(), &props); - return Result::SUCCESS; -#else - return Result::UNSUPPORTED; -#endif + m_DeviceDesc.meshTaskWorkGroupInvocationMaxNum = meshShaderProps.maxTaskWorkGroupInvocations; + m_DeviceDesc.meshTaskWorkGroupMaxDim[0] = meshShaderProps.maxTaskWorkGroupSize[0]; + m_DeviceDesc.meshTaskWorkGroupMaxDim[1] = meshShaderProps.maxTaskWorkGroupSize[1]; + m_DeviceDesc.meshTaskWorkGroupMaxDim[2] = meshShaderProps.maxTaskWorkGroupSize[2]; + m_DeviceDesc.meshTaskTotalMemoryMaxSize = meshShaderProps.maxTaskPayloadAndSharedMemorySize; + m_DeviceDesc.meshTaskOutputMaxNum = meshShaderProps.maxMeshWorkGroupTotalCount; + m_DeviceDesc.meshWorkGroupInvocationMaxNum = meshShaderProps.maxMeshWorkGroupInvocations; + m_DeviceDesc.meshWorkGroupMaxDim[0] = meshShaderProps.maxMeshWorkGroupSize[0]; + m_DeviceDesc.meshWorkGroupMaxDim[1] = meshShaderProps.maxMeshWorkGroupSize[1]; + m_DeviceDesc.meshWorkGroupMaxDim[2] = meshShaderProps.maxMeshWorkGroupSize[2]; + m_DeviceDesc.meshOutputVertexMaxNum = meshShaderProps.maxMeshOutputVertices; + m_DeviceDesc.meshOutputPrimitiveMaxNum = meshShaderProps.maxMeshOutputPrimitives; + m_DeviceDesc.meshMultiviewViewMaxNum = meshShaderProps.maxMeshMultiviewViewCount; + m_DeviceDesc.meshOutputPerVertexGranularity = meshShaderProps.meshOutputPerVertexGranularity; + m_DeviceDesc.meshOutputPerPrimitiveGranularity = meshShaderProps.meshOutputPerPrimitiveGranularity; + } } -inline Result DeviceVK::AllocateMemory(uint32_t physicalDeviceMask, MemoryType memoryType, uint64_t size, Memory*& memory) +void DeviceVK::FillFamilyIndices(bool useEnabledFamilyIndices, const uint32_t* enabledFamilyIndices, uint32_t familyIndexNum) { - return CreateImplementation(memory, physicalDeviceMask, memoryType, size); + uint32_t familyNum = 0; + m_VK.GetPhysicalDeviceQueueFamilyProperties(m_PhysicalDevices.front(), &familyNum, nullptr); + + Vector familyProps(familyNum, GetStdAllocator()); + m_VK.GetPhysicalDeviceQueueFamilyProperties(m_PhysicalDevices.front(), &familyNum, familyProps.data()); + + memset(m_FamilyIndices.data(), INVALID_FAMILY_INDEX, m_FamilyIndices.size() * sizeof(uint32_t)); + + for (uint32_t i = 0; i < familyProps.size(); i++) + { + const VkQueueFlags mask = familyProps[i].queueFlags; + const bool graphics = mask & VK_QUEUE_GRAPHICS_BIT; + const bool compute = mask & VK_QUEUE_COMPUTE_BIT; + const bool copy = mask & VK_QUEUE_TRANSFER_BIT; + + if (useEnabledFamilyIndices) + { + bool isFamilyEnabled = false; + for (uint32_t j = 0; j < familyIndexNum && !isFamilyEnabled; j++) + isFamilyEnabled = enabledFamilyIndices[j] == i; + + if (!isFamilyEnabled) + continue; + } + + if (graphics) + m_FamilyIndices[(uint32_t)CommandQueueType::GRAPHICS] = i; + else if (compute) + m_FamilyIndices[(uint32_t)CommandQueueType::COMPUTE] = i; + else if (copy) + m_FamilyIndices[(uint32_t)CommandQueueType::COPY] = i; + } } -inline Result DeviceVK::BindBufferMemory(const BufferMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum) +Result DeviceVK::CreateLogicalDevice(const DeviceCreationDesc& deviceCreationDesc) { - if (memoryBindingDescNum == 0) - return Result::SUCCESS; + uint32_t extensionNum = 0; + m_VK.EnumerateDeviceExtensionProperties(m_PhysicalDevices.front(), nullptr, &extensionNum, nullptr); - const uint32_t infoMaxNum = memoryBindingDescNum * m_DeviceDesc.physicalDeviceNum; + Vector supportedExts(extensionNum, GetStdAllocator()); + m_VK.EnumerateDeviceExtensionProperties(m_PhysicalDevices.front(), nullptr, &extensionNum, supportedExts.data()); - VkBindBufferMemoryInfo* infos = STACK_ALLOC(VkBindBufferMemoryInfo, infoMaxNum); - uint32_t infoNum = 0; + Vector desiredExts(GetStdAllocator()); + for (uint32_t i = 0; i < deviceCreationDesc.vulkanExtensions.deviceExtensionNum; i++) + desiredExts.push_back(deviceCreationDesc.vulkanExtensions.deviceExtensions[i]); - VkBindBufferMemoryDeviceGroupInfo* deviceGroupInfos = nullptr; - if (m_DeviceDesc.physicalDeviceNum > 1) - deviceGroupInfos = STACK_ALLOC(VkBindBufferMemoryDeviceGroupInfo, infoMaxNum); + if (IsExtensionSupported(VK_KHR_SWAPCHAIN_EXTENSION_NAME, supportedExts)) + desiredExts.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME); - for (uint32_t i = 0; i < memoryBindingDescNum; i++) - { - const BufferMemoryBindingDesc& bindingDesc = memoryBindingDescs[i]; + if (IsExtensionSupported(VK_KHR_DEFERRED_HOST_OPERATIONS_EXTENSION_NAME, supportedExts)) + desiredExts.push_back(VK_KHR_DEFERRED_HOST_OPERATIONS_EXTENSION_NAME); - MemoryVK& memoryImpl = *(MemoryVK*)bindingDesc.memory; - BufferVK& bufferImpl = *(BufferVK*)bindingDesc.buffer; + if (IsExtensionSupported(VK_KHR_SYNCHRONIZATION_2_EXTENSION_NAME, supportedExts)) + desiredExts.push_back(VK_KHR_SYNCHRONIZATION_2_EXTENSION_NAME); - const MemoryTypeUnpack unpack = { memoryImpl.GetType() }; - const MemoryTypeInfo& memoryTypeInfo = unpack.info; + if (IsExtensionSupported(VK_KHR_PIPELINE_LIBRARY_EXTENSION_NAME, supportedExts)) + desiredExts.push_back(VK_KHR_PIPELINE_LIBRARY_EXTENSION_NAME); - const MemoryLocation memoryLocation = (MemoryLocation)memoryTypeInfo.location; + if (IsExtensionSupported(VK_KHR_RAY_TRACING_PIPELINE_EXTENSION_NAME, supportedExts)) + desiredExts.push_back(VK_KHR_RAY_TRACING_PIPELINE_EXTENSION_NAME); - uint32_t physicalDeviceMask = GetPhysicalDeviceGroupMask(bindingDesc.physicalDeviceMask); - if (IsHostVisibleMemory(memoryLocation)) - physicalDeviceMask = 0x1; + if (IsExtensionSupported(VK_KHR_RAY_QUERY_EXTENSION_NAME, supportedExts)) + desiredExts.push_back(VK_KHR_RAY_QUERY_EXTENSION_NAME); - if (memoryTypeInfo.isDedicated == 1) - memoryImpl.CreateDedicated(bufferImpl, physicalDeviceMask); + if (IsExtensionSupported(VK_KHR_ACCELERATION_STRUCTURE_EXTENSION_NAME, supportedExts)) + { + desiredExts.push_back(VK_KHR_ACCELERATION_STRUCTURE_EXTENSION_NAME); + m_IsRayTracingExtSupported = true; + } - for (uint32_t j = 0; j < m_DeviceDesc.physicalDeviceNum; j++) - { - if ((1u << j) & physicalDeviceMask) - { - VkBindBufferMemoryInfo& info = infos[infoNum++]; + if (IsExtensionSupported(VK_EXT_SAMPLE_LOCATIONS_EXTENSION_NAME, supportedExts)) + { + desiredExts.push_back(VK_EXT_SAMPLE_LOCATIONS_EXTENSION_NAME); + m_IsSampleLocationExtSupported = true; + } - info = {}; - info.sType = VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO; - info.buffer = bufferImpl.GetHandle(j); - info.memory = memoryImpl.GetHandle(j); - info.memoryOffset = bindingDesc.offset; - - if (IsHostVisibleMemory(memoryLocation)) - bufferImpl.SetHostMemory(memoryImpl, info.memoryOffset); - - if (deviceGroupInfos != nullptr) - { - VkBindBufferMemoryDeviceGroupInfo& deviceGroupInfo = deviceGroupInfos[infoNum - 1]; - deviceGroupInfo = {}; - deviceGroupInfo.sType = VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_DEVICE_GROUP_INFO; - deviceGroupInfo.deviceIndexCount = m_DeviceDesc.physicalDeviceNum; - deviceGroupInfo.pDeviceIndices = &m_PhysicalDeviceIndices[j * m_DeviceDesc.physicalDeviceNum]; - info.pNext = &deviceGroupInfo; - } - } - } + if (IsExtensionSupported(VK_EXT_CONSERVATIVE_RASTERIZATION_EXTENSION_NAME, supportedExts)) + { + desiredExts.push_back(VK_EXT_CONSERVATIVE_RASTERIZATION_EXTENSION_NAME); + m_IsConservativeRasterExtSupported = true; } - VkResult result = VK_SUCCESS; - if (infoNum > 0) - result = m_VK.BindBufferMemory2(m_Device, infoNum, infos); - - RETURN_ON_FAILURE(GetLog(), result == VK_SUCCESS, GetReturnCode(result), - "Can't bind a memory to a buffer: vkBindBufferMemory2 returned %d.", (int32_t)result); + if (IsExtensionSupported(VK_EXT_HDR_METADATA_EXTENSION_NAME, supportedExts)) + { + desiredExts.push_back(VK_EXT_HDR_METADATA_EXTENSION_NAME); + m_IsHDRExtSupported = true; + } - for (uint32_t i = 0; i < memoryBindingDescNum; i++) + if (IsExtensionSupported(VK_EXT_OPACITY_MICROMAP_EXTENSION_NAME, supportedExts)) { - BufferVK& bufferImpl = *(BufferVK*)memoryBindingDescs[i].buffer; - bufferImpl.ReadDeviceAddress(); + desiredExts.push_back(VK_EXT_OPACITY_MICROMAP_EXTENSION_NAME); + m_IsMicroMapSupported = true; } - return Result::SUCCESS; -} + if (IsExtensionSupported(VK_EXT_MESH_SHADER_EXTENSION_NAME, supportedExts)) + { + desiredExts.push_back(VK_EXT_MESH_SHADER_EXTENSION_NAME); + m_IsMeshShaderExtSupported = true; + } -inline Result DeviceVK::BindTextureMemory(const TextureMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum) -{ - const uint32_t infoMaxNum = memoryBindingDescNum * m_DeviceDesc.physicalDeviceNum; + VkPhysicalDeviceRayTracingPipelineFeaturesKHR rayTracingFeatures = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_TRACING_PIPELINE_FEATURES_KHR }; + VkPhysicalDeviceAccelerationStructureFeaturesKHR accelerationStructureFeatures = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ACCELERATION_STRUCTURE_FEATURES_KHR }; + VkPhysicalDeviceRayQueryFeaturesKHR rayQueryFeatures = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_QUERY_FEATURES_KHR }; + VkPhysicalDeviceOpacityMicromapFeaturesEXT micromapFeatures = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_OPACITY_MICROMAP_FEATURES_EXT }; + VkPhysicalDeviceMeshShaderFeaturesEXT meshShaderFeatures = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MESH_SHADER_FEATURES_EXT }; + VkPhysicalDeviceVulkan11Features features11 = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES }; + VkPhysicalDeviceVulkan12Features features12 = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES }; + VkPhysicalDeviceVulkan13Features features13 = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES }; + VkPhysicalDeviceFeatures2 features2 = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2 }; - VkBindImageMemoryInfo* infos = STACK_ALLOC(VkBindImageMemoryInfo, infoMaxNum); - uint32_t infoNum = 0; + features2.pNext = &features11; + features11.pNext = &features12; + features12.pNext = &features13; - VkBindImageMemoryDeviceGroupInfo* deviceGroupInfos = nullptr; - if (m_DeviceDesc.physicalDeviceNum > 1) - deviceGroupInfos = STACK_ALLOC(VkBindImageMemoryDeviceGroupInfo, infoMaxNum); + if (m_IsRayTracingExtSupported) + { + rayTracingFeatures.pNext = features2.pNext; + features2.pNext = &rayTracingFeatures; + accelerationStructureFeatures.pNext = features2.pNext; + features2.pNext = &accelerationStructureFeatures; + rayQueryFeatures.pNext = features2.pNext; + features2.pNext = &rayQueryFeatures; + } - for (uint32_t i = 0; i < memoryBindingDescNum; i++) + if (m_IsMicroMapSupported) { - const TextureMemoryBindingDesc& bindingDesc = memoryBindingDescs[i]; + micromapFeatures.pNext = features2.pNext; + features2.pNext = µmapFeatures; + } - const uint32_t physicalDeviceMask = GetPhysicalDeviceGroupMask(bindingDesc.physicalDeviceMask); + if (m_IsMeshShaderExtSupported) + { + meshShaderFeatures.pNext = features2.pNext; + features2.pNext = &meshShaderFeatures; + } - MemoryVK& memoryImpl = *(MemoryVK*)bindingDesc.memory; - TextureVK& textureImpl = *(TextureVK*)bindingDesc.texture; + m_VK.GetPhysicalDeviceFeatures2(m_PhysicalDevices.front(), &features2); - const MemoryTypeUnpack unpack = { memoryImpl.GetType() }; - const MemoryTypeInfo& memoryTypeInfo = unpack.info; + Vector queues(GetStdAllocator()); + const float priorities = 1.0f; + for (size_t i = 0; i < m_FamilyIndices.size(); i++) + { + if (m_FamilyIndices[i] == INVALID_FAMILY_INDEX) + continue; - if (memoryTypeInfo.isDedicated == 1) - memoryImpl.CreateDedicated(textureImpl, physicalDeviceMask); + VkDeviceQueueCreateInfo info = { VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO }; + info.queueCount = 1; + info.queueFamilyIndex = m_FamilyIndices[i]; + info.pQueuePriorities = &priorities; + queues.push_back(info); + } - for (uint32_t j = 0; j < m_DeviceDesc.physicalDeviceNum; j++) - { - if ((1u << j) & physicalDeviceMask) - { - VkBindImageMemoryInfo& info = infos[infoNum++]; - info.sType = VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_INFO; - info.pNext = nullptr; - info.image = textureImpl.GetHandle(j); - info.memory = memoryImpl.GetHandle(j); - info.memoryOffset = bindingDesc.offset; + VkDeviceCreateInfo deviceCreateInfo = { VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO }; + deviceCreateInfo.pNext = &features2; + deviceCreateInfo.queueCreateInfoCount = (uint32_t)queues.size(); + deviceCreateInfo.pQueueCreateInfos = queues.data(); + deviceCreateInfo.enabledExtensionCount = (uint32_t)desiredExts.size(); + deviceCreateInfo.ppEnabledExtensionNames = desiredExts.data(); - if (deviceGroupInfos != nullptr) - { - VkBindImageMemoryDeviceGroupInfo& deviceGroupInfo = deviceGroupInfos[infoNum - 1]; - deviceGroupInfo = {}; - deviceGroupInfo.sType = VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_DEVICE_GROUP_INFO; - deviceGroupInfo.deviceIndexCount = m_DeviceDesc.physicalDeviceNum; - deviceGroupInfo.pDeviceIndices = &m_PhysicalDeviceIndices[j * m_DeviceDesc.physicalDeviceNum]; - info.pNext = &deviceGroupInfo; - } - } - } + VkDeviceGroupDeviceCreateInfo deviceGroupInfo = { VK_STRUCTURE_TYPE_DEVICE_GROUP_DEVICE_CREATE_INFO }; + if (m_PhysicalDevices.size() > 1) + { + deviceGroupInfo.pNext = deviceCreateInfo.pNext; + deviceGroupInfo.physicalDeviceCount = (uint32_t)m_PhysicalDevices.size(); + deviceGroupInfo.pPhysicalDevices = m_PhysicalDevices.data(); + deviceCreateInfo.pNext = &deviceGroupInfo; } - VkResult result = VK_SUCCESS; - if (infoNum > 0) - result = m_VK.BindImageMemory2(m_Device, infoNum, infos); + const VkResult result = m_VK.CreateDevice(m_PhysicalDevices.front(), &deviceCreateInfo, m_AllocationCallbackPtr, &m_Device); - RETURN_ON_FAILURE(GetLog(), result == VK_SUCCESS, GetReturnCode(result), - "Can't bind a memory to a texture: vkBindImageMemory2 returned %d.", (int32_t)result); + RETURN_ON_FAILURE(GetLog(), result == VK_SUCCESS, GetReturnCode(result), "Can't create a device: " + "vkCreateDevice returned %d.", (int32_t)result); return Result::SUCCESS; } -inline Result DeviceVK::BindAccelerationStructureMemory(const AccelerationStructureMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum) +void DeviceVK::CreateCommandQueues() { - if (memoryBindingDescNum == 0) - return Result::SUCCESS; - - BufferMemoryBindingDesc* infos = ALLOCATE_SCRATCH(*this, BufferMemoryBindingDesc, memoryBindingDescNum); - - for (uint32_t i = 0; i < memoryBindingDescNum; i++) + for (uint32_t i = 0; i < m_FamilyIndices.size(); i++) { - const AccelerationStructureMemoryBindingDesc& bindingDesc = memoryBindingDescs[i]; - AccelerationStructureVK& accelerationStructure = *(AccelerationStructureVK*)bindingDesc.accelerationStructure; + if (m_FamilyIndices[i] == INVALID_FAMILY_INDEX) + continue; - BufferMemoryBindingDesc& bufferMemoryBinding = infos[i]; - bufferMemoryBinding = {}; - bufferMemoryBinding.buffer = (Buffer*)accelerationStructure.GetBuffer(); - bufferMemoryBinding.memory = bindingDesc.memory; - bufferMemoryBinding.offset = bindingDesc.offset; - bufferMemoryBinding.physicalDeviceMask = bindingDesc.physicalDeviceMask; + VkQueue handle = VK_NULL_HANDLE; + m_VK.GetDeviceQueue(m_Device, m_FamilyIndices[i], 0, &handle); + + m_Queues[i] = Allocate(GetStdAllocator(), *this, handle, m_FamilyIndices[i], (CommandQueueType)i); + m_ConcurrentSharingModeQueueIndices.push_back(m_FamilyIndices[i]); } +} - Result result = BindBufferMemory(infos, memoryBindingDescNum); +void DeviceVK::SetDebugNameToTrivialObject(VkObjectType objectType, uint64_t handle, const char* name) +{ + if (!m_VK.SetDebugUtilsObjectNameEXT) + return; - for (uint32_t i = 0; i < memoryBindingDescNum && result == Result::SUCCESS; i++) - { - AccelerationStructureVK& accelerationStructure = *(AccelerationStructureVK*)memoryBindingDescs[i].accelerationStructure; - result = accelerationStructure.FinishCreation(); - } + VkDebugUtilsObjectNameInfoEXT info = { + VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT, + nullptr, + objectType, + (uint64_t)handle, + name + }; - FREE_SCRATCH(*this, infos, memoryBindingDescNum); + const VkResult result = m_VK.SetDebugUtilsObjectNameEXT(m_Device, &info); - return result; + RETURN_ON_FAILURE(GetLog(), result == VK_SUCCESS, ReturnVoid(), + "Can't set a debug name to an object: vkSetDebugUtilsObjectNameEXT returned %d.", (int32_t)result); } -inline void DeviceVK::FreeMemory(Memory& memory) +void DeviceVK::SetDebugNameToDeviceGroupObject(VkObjectType objectType, const uint64_t* handles, const char* name) { - Deallocate(GetStdAllocator(), (MemoryVK*)&memory); -} + if (!m_VK.SetDebugUtilsObjectNameEXT) + return; -inline FormatSupportBits DeviceVK::GetFormatSupport(Format format) const -{ - const VkFormat vulkanFormat = GetVkFormat(format); - const VkPhysicalDevice physicalDevice = m_PhysicalDevices.front(); + const size_t nameLength = strlen(name); + constexpr size_t deviceIndexSuffixLength = 16; // " (PD%u)" - VkFormatProperties formatProperties = {}; - m_VK.GetPhysicalDeviceFormatProperties(physicalDevice, vulkanFormat, &formatProperties); + char* nameWithDeviceIndex = STACK_ALLOC(char, nameLength + deviceIndexSuffixLength); + memcpy(nameWithDeviceIndex, name, nameLength); - constexpr uint32_t transferBits = VK_FORMAT_FEATURE_TRANSFER_DST_BIT | VK_FORMAT_FEATURE_TRANSFER_SRC_BIT; + VkDebugUtilsObjectNameInfoEXT info = { + VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT, + nullptr, + objectType, + (uint64_t)0, + nameWithDeviceIndex + }; - constexpr uint32_t textureBits = VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | transferBits; - constexpr uint32_t storageTextureBits = VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT | transferBits; - constexpr uint32_t bufferBits = VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT | transferBits; - constexpr uint32_t storageBufferBits = VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT | transferBits; - constexpr uint32_t colorAttachmentBits = VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT | VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT | transferBits; - constexpr uint32_t depthAttachmentBits = VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT | transferBits; - constexpr uint32_t vertexBufferBits = VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT | transferBits; + for (uint32_t i = 0; i < m_DeviceDesc.physicalDeviceNum; i++) + { + if (handles[i] != 0) + { + info.objectHandle = (uint64_t)handles[i]; + snprintf(nameWithDeviceIndex + nameLength, deviceIndexSuffixLength, " (PD%u)", i); - FormatSupportBits mask = FormatSupportBits::UNSUPPORTED; + const VkResult result = m_VK.SetDebugUtilsObjectNameEXT(m_Device, &info); + RETURN_ON_FAILURE(GetLog(), result == VK_SUCCESS, ReturnVoid(), + "Can't set a debug name to an object: vkSetDebugUtilsObjectNameEXT returned %d.", (int32_t)result); + } + } +} - if (formatProperties.optimalTilingFeatures & textureBits) - mask |= FormatSupportBits::TEXTURE; +void DeviceVK::ReportDeviceGroupInfo() +{ + REPORT_INFO(GetLog(), "Available device memory heaps:"); - if (formatProperties.optimalTilingFeatures & storageTextureBits) - mask |= FormatSupportBits::STORAGE_TEXTURE; + for (uint32_t i = 0; i < m_MemoryProps.memoryHeapCount; i++) + { + String text(GetStdAllocator()); + if (m_MemoryProps.memoryHeaps[i].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) + text += "DEVICE_LOCAL_BIT "; + if (m_MemoryProps.memoryHeaps[i].flags & VK_MEMORY_HEAP_MULTI_INSTANCE_BIT) + text += "MULTI_INSTANCE_BIT "; - if (formatProperties.optimalTilingFeatures & colorAttachmentBits) - mask |= FormatSupportBits::COLOR_ATTACHMENT; + const double size = double(m_MemoryProps.memoryHeaps[i].size) / (1024.0 * 1024.0); - if (formatProperties.optimalTilingFeatures & depthAttachmentBits) - mask |= FormatSupportBits::DEPTH_STENCIL_ATTACHMENT; + REPORT_INFO(GetLog(), " Heap%u %.1lfMiB - %s", i, size, text.c_str()); - if (formatProperties.bufferFeatures & bufferBits) - mask |= FormatSupportBits::BUFFER; + if (m_DeviceDesc.physicalDeviceNum == 1) + continue; - if (formatProperties.bufferFeatures & storageBufferBits) - mask |= FormatSupportBits::STORAGE_BUFFER; + for (uint32_t j = 0; j < m_DeviceDesc.physicalDeviceNum; j++) + { + REPORT_INFO(GetLog(), " PhysicalDevice%u", j); - if (formatProperties.bufferFeatures & vertexBufferBits) - mask |= FormatSupportBits::VERTEX_BUFFER; + for (uint32_t k = 0; k < m_DeviceDesc.physicalDeviceNum; k++) + { + if (j == k) + continue; - return mask; -} + VkPeerMemoryFeatureFlags flags = 0; + m_VK.GetDeviceGroupPeerMemoryFeatures(m_Device, i, j, k, &flags); -inline uint32_t DeviceVK::CalculateAllocationNumber(const ResourceGroupDesc& resourceGroupDesc) const -{ - HelperDeviceMemoryAllocator allocator(m_CoreInterface, (Device&)*this, m_StdAllocator); + text.clear(); + if (flags & VK_PEER_MEMORY_FEATURE_COPY_SRC_BIT) + text += "COPY_SRC_BIT "; + if (flags & VK_PEER_MEMORY_FEATURE_COPY_DST_BIT) + text += "COPY_DST_BIT "; + if (flags & VK_PEER_MEMORY_FEATURE_GENERIC_SRC_BIT) + text += "GENERIC_SRC_BIT "; + if (flags & VK_PEER_MEMORY_FEATURE_GENERIC_DST_BIT) + text += "GENERIC_DST_BIT "; - return allocator.CalculateAllocationNumber(resourceGroupDesc); + REPORT_INFO(GetLog(), " PhysicalDevice%u - %s", k, text.c_str()); + } + } + } } -inline Result DeviceVK::AllocateAndBindMemory(const ResourceGroupDesc& resourceGroupDesc, nri::Memory** allocations) -{ - HelperDeviceMemoryAllocator allocator(m_CoreInterface, (Device&)*this, m_StdAllocator); +#define RESOLVE_OPTIONAL_DEVICE_FUNCTION( name ) \ + m_VK.name = (PFN_vk ## name)m_VK.GetDeviceProcAddr(m_Device, "vk" #name) - return allocator.AllocateAndBindMemory(resourceGroupDesc, allocations); -} - -const char* GetObjectTypeName(VkObjectType objectType) -{ - switch(objectType) - { - case VK_OBJECT_TYPE_INSTANCE: - return "VkInstance"; - case VK_OBJECT_TYPE_PHYSICAL_DEVICE: - return "VkPhysicalDevice"; - case VK_OBJECT_TYPE_DEVICE: - return "VkDevice"; - case VK_OBJECT_TYPE_QUEUE: - return "VkQueue"; - case VK_OBJECT_TYPE_SEMAPHORE: - return "VkSemaphore"; - case VK_OBJECT_TYPE_COMMAND_BUFFER: - return "VkCommandBuffer"; - case VK_OBJECT_TYPE_FENCE: - return "VkFence"; - case VK_OBJECT_TYPE_DEVICE_MEMORY: - return "VkDeviceMemory"; - case VK_OBJECT_TYPE_BUFFER: - return "VkBuffer"; - case VK_OBJECT_TYPE_IMAGE: - return "VkImage"; - case VK_OBJECT_TYPE_EVENT: - return "VkEvent"; - case VK_OBJECT_TYPE_QUERY_POOL: - return "VkQueryPool"; - case VK_OBJECT_TYPE_BUFFER_VIEW: - return "VkBufferView"; - case VK_OBJECT_TYPE_IMAGE_VIEW: - return "VkImageView"; - case VK_OBJECT_TYPE_SHADER_MODULE: - return "VkShaderModule"; - case VK_OBJECT_TYPE_PIPELINE_CACHE: - return "VkPipelineCache"; - case VK_OBJECT_TYPE_PIPELINE_LAYOUT: - return "VkPipelineLayout"; - case VK_OBJECT_TYPE_RENDER_PASS: - return "VkRenderPass"; - case VK_OBJECT_TYPE_PIPELINE: - return "VkPipeline"; - case VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT: - return "VkDescriptorSetLayout"; - case VK_OBJECT_TYPE_SAMPLER: - return "VkSampler"; - case VK_OBJECT_TYPE_DESCRIPTOR_POOL: - return "VkDescriptorPool"; - case VK_OBJECT_TYPE_DESCRIPTOR_SET: - return "VkDescriptorSet"; - case VK_OBJECT_TYPE_FRAMEBUFFER: - return "VkFramebuffer"; - case VK_OBJECT_TYPE_COMMAND_POOL: - return "VkCommandPool"; - case VK_OBJECT_TYPE_SAMPLER_YCBCR_CONVERSION: - return "VkSamplerYcbcrConversion"; - case VK_OBJECT_TYPE_DESCRIPTOR_UPDATE_TEMPLATE: - return "VkDescriptorUpdateTemplate"; - case VK_OBJECT_TYPE_SURFACE_KHR: - return "VkSurfaceKHR"; - case VK_OBJECT_TYPE_SWAPCHAIN_KHR: - return "VkSwapchainKHR"; - case VK_OBJECT_TYPE_DISPLAY_KHR: - return "VkDisplayKHR"; - case VK_OBJECT_TYPE_DISPLAY_MODE_KHR: - return "VkDisplayModeKHR"; - case VK_OBJECT_TYPE_DEBUG_REPORT_CALLBACK_EXT: - return "VkDebugReportCallbackEXT"; - case VK_OBJECT_TYPE_DEBUG_UTILS_MESSENGER_EXT: - return "VkDebugUtilsMessengerEXT"; - case VK_OBJECT_TYPE_ACCELERATION_STRUCTURE_KHR: - return "VkAccelerationStructureKHR"; - case VK_OBJECT_TYPE_VALIDATION_CACHE_EXT: - return "VkValidationCacheEXT"; - case VK_OBJECT_TYPE_PERFORMANCE_CONFIGURATION_INTEL: - return "VkPerformanceConfigurationINTEL"; - case VK_OBJECT_TYPE_DEFERRED_OPERATION_KHR: - return "VkDeferredOperationKHR"; - case VK_OBJECT_TYPE_INDIRECT_COMMANDS_LAYOUT_NV: - return "VkIndirectCommandsLayoutNV"; - default: - return "unknown"; - } -} - -VkBool32 VKAPI_PTR DebugUtilsMessenger( - VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, - VkDebugUtilsMessageTypeFlagsEXT messageType, - const VkDebugUtilsMessengerCallbackDataEXT* callbackData, - void* userData) -{ - MaybeUnused(messageType); - - bool isError = false; - bool isWarning = false; - - // UNASSIGNED-CoreValidation-Shader-InconsistentSpirv - if (callbackData->messageIdNumber == 7060244) - messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT; - - // VUID-VkShaderModuleCreateInfo-pCode-01090 - if (callbackData->messageIdNumber == 738239446) - messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT; - - // VUID-RuntimeSpirv-OpImageWrite-07112 - if (callbackData->messageIdNumber == 1842853234 || callbackData->messageIdNumber == 2144011273) - messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT; - - const char* type = "unknown"; - switch( messageSeverity ) - { - case VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT: - type = "verbose"; - break; - case VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT: - type = "info"; - break; - case VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT: - type = "warning"; - isWarning = true; - break; - case VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT: - type = "error"; - isError = true; - break; +#define RESOLVE_DEVICE_FUNCTION( name ) \ + RESOLVE_OPTIONAL_DEVICE_FUNCTION(name); \ + if (!m_VK.name) \ + { \ + REPORT_ERROR(GetLog(), "Failed to get device function: '%s'.", #name); \ + return Result::UNSUPPORTED; \ } - if (!isWarning && !isError) - return VK_FALSE; - - DeviceVK& device = *(DeviceVK*)userData; - - String message(device.GetStdAllocator()); - message += std::to_string(callbackData->messageIdNumber); - message += " "; - message += callbackData->pMessageIdName; - message += " "; - message += callbackData->pMessage; - - // vkCmdCopyBufferToImage: For optimal performance VkImage 0x984b920000000104 layout should be VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL instead of GENERAL. - if (callbackData->messageIdNumber == 1303270965) - return VK_FALSE; - - if (messageSeverity == VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) - { - message += "\nObjectNum: " + std::to_string(callbackData->objectCount); - - char buffer[64]; - - for (uint32_t i = 0; i < callbackData->objectCount; i++) - { - const VkDebugUtilsObjectNameInfoEXT& object = callbackData->pObjects[i]; - snprintf(buffer, sizeof(buffer), "0x%llx", (unsigned long long)object.objectHandle); - - message += "\n\tObject "; - message += object.pObjectName != nullptr ? object.pObjectName : ""; - message += " "; - message += GetObjectTypeName(object.objectType); - message += " ("; - message += buffer; - message += ")"; - } - - REPORT_ERROR(device.GetLog(), "DebugUtilsMessenger: %s, %s", type, message.c_str()); +#define RESOLVE_DEVICE_FUNCTION_WITH_OTHER_NAME( functionName, otherName ) \ + m_VK.functionName = (PFN_vk ## functionName)m_VK.GetDeviceProcAddr(m_Device, otherName); \ + if (!m_VK.functionName) \ + { \ + REPORT_ERROR(GetLog(), "Failed to get device function: '" otherName "'."); \ + return Result::UNSUPPORTED; \ } - else if (messageSeverity == VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) - REPORT_WARNING(device.GetLog(), "DebugUtilsMessenger: %s, %s", type, message.c_str()); - else - REPORT_INFO(device.GetLog(), "DebugUtilsMessenger: %s, %s", type, message.c_str()); - - return VK_FALSE; -} - -void DeviceVK::FilterInstanceLayers(Vector& layers) -{ - uint32_t layerNum = 0; - m_VK.EnumerateInstanceLayerProperties(&layerNum, nullptr); - - Vector supportedLayers(layerNum, GetStdAllocator()); - m_VK.EnumerateInstanceLayerProperties(&layerNum, supportedLayers.data()); - - for (size_t i = 0; i < layers.size(); i++) - { - bool found = false; - for (uint32_t j = 0; j < layerNum && !found; j++) - { - if (strcmp(supportedLayers[j].layerName, layers[i]) == 0) - found = true; - } - if (!found) - layers.erase(layers.begin() + i--); +#define RESOLVE_INSTANCE_FUNCTION( name ) \ + m_VK.name = (PFN_vk ## name)m_VK.GetInstanceProcAddr(m_Instance, "vk" #name); \ + if (!m_VK.name) \ + { \ + REPORT_ERROR(GetLog(), "Failed to get instance function: '%s'.", #name); \ + return Result::UNSUPPORTED; \ } -} - -bool DeviceVK::FilterInstanceExtensions(Vector& extensions) -{ - uint32_t extensionNum = 0; - m_VK.EnumerateInstanceExtensionProperties(nullptr, &extensionNum, nullptr); - - Vector supportedExtensions(extensionNum, GetStdAllocator()); - m_VK.EnumerateInstanceExtensionProperties(nullptr, &extensionNum, supportedExtensions.data()); - - bool allFound = true; - for (size_t i = 0; i < extensions.size(); i++) - { - bool found = false; - for (uint32_t j = 0; j < extensionNum && !found; j++) - { - if (strcmp(supportedExtensions[j].extensionName, extensions[i]) == 0) - found = true; - } - if (!found) - { - extensions.erase(extensions.begin() + i--); - allFound = false; - } +#define RESOLVE_PRE_INSTANCE_FUNCTION( name ) \ + m_VK.name = (PFN_vk ## name)m_VK.GetInstanceProcAddr(VK_NULL_HANDLE, "vk" #name); \ + if (!m_VK.name) \ + { \ + REPORT_ERROR(GetLog(), "Failed to get instance function: '%s'.", #name); \ + return Result::UNSUPPORTED; \ } - return allFound; -} - -Result DeviceVK::CreateInstance(const DeviceCreationDesc& deviceCreationDesc) +Result DeviceVK::ResolvePreInstanceDispatchTable() { - Vector layers(GetStdAllocator()); - Vector extensions(GetStdAllocator()); - - #ifdef VK_USE_PLATFORM_WIN32_KHR - extensions.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME); - #endif - #if VK_USE_PLATFORM_METAL_EXT - extensions.push_back(VK_EXT_METAL_SURFACE_EXTENSION_NAME); - #endif - #ifdef VK_USE_PLATFORM_XLIB_KHR - extensions.push_back(VK_KHR_XLIB_SURFACE_EXTENSION_NAME); - #endif - #if VK_USE_PLATFORM_WAYLAND_KHR - extensions.push_back(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME); - #endif - extensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME); - - for (uint32_t i = 0; i < deviceCreationDesc.vulkanExtensions.instanceExtensionNum; i++) - extensions.push_back(deviceCreationDesc.vulkanExtensions.instanceExtensions[i]); + m_VK = {}; - if (!FilterInstanceExtensions(extensions)) + m_VK.GetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)GetSharedLibraryFunction(*m_Loader, "vkGetInstanceProcAddr"); + if (!m_VK.GetInstanceProcAddr) { - REPORT_ERROR(GetLog(), "Can't create VkInstance: the required extensions are not supported."); + REPORT_ERROR(GetLog(), "Failed to get vkGetInstanceProcAddr."); return Result::UNSUPPORTED; } - extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME); - - if (deviceCreationDesc.enableAPIValidation) - layers.push_back("VK_LAYER_KHRONOS_validation"); - - FilterInstanceLayers(layers); - FilterInstanceExtensions(extensions); - - CheckSupportedInstanceExtensions(extensions); - - const VkApplicationInfo appInfo = { - VK_STRUCTURE_TYPE_APPLICATION_INFO, - nullptr, - nullptr, - 0, - nullptr, - 0, - VK_API_VERSION_1_2 - }; - - const VkInstanceCreateInfo info = { - VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, - nullptr, - (VkInstanceCreateFlags)0, - &appInfo, - (uint32_t)layers.size(), - layers.data(), - (uint32_t)extensions.size(), - extensions.data(), - }; - - VkResult result = m_VK.CreateInstance(&info, m_AllocationCallbackPtr, &m_Instance); - - RETURN_ON_FAILURE(GetLog(), result == VK_SUCCESS, GetReturnCode(result), - "Can't create a VkInstance: vkCreateInstance returned %d.", (int32_t)result); - - if (deviceCreationDesc.enableAPIValidation) - { - typedef PFN_vkCreateDebugUtilsMessengerEXT Func; - Func vkCreateDebugUtilsMessengerEXT = nullptr; - vkCreateDebugUtilsMessengerEXT = (Func)m_VK.GetInstanceProcAddr(m_Instance, "vkCreateDebugUtilsMessengerEXT"); - - VkDebugUtilsMessengerCreateInfoEXT createInfo = { VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT }; - - createInfo.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT; - createInfo.messageSeverity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT; - - createInfo.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT; - createInfo.messageType |= VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT; + RESOLVE_PRE_INSTANCE_FUNCTION(CreateInstance); + RESOLVE_PRE_INSTANCE_FUNCTION(EnumerateInstanceExtensionProperties); + RESOLVE_PRE_INSTANCE_FUNCTION(EnumerateInstanceLayerProperties); - createInfo.pUserData = this; - createInfo.pfnUserCallback = DebugUtilsMessenger; + return Result::SUCCESS; +} - result = vkCreateDebugUtilsMessengerEXT(m_Instance, &createInfo, m_AllocationCallbackPtr, &m_Messenger); +Result DeviceVK::ResolveInstanceDispatchTable() +{ + RESOLVE_INSTANCE_FUNCTION(GetDeviceProcAddr); + RESOLVE_INSTANCE_FUNCTION(DestroyInstance); + RESOLVE_INSTANCE_FUNCTION(DestroyDevice); + RESOLVE_INSTANCE_FUNCTION(GetPhysicalDeviceMemoryProperties); + RESOLVE_INSTANCE_FUNCTION(GetDeviceGroupPeerMemoryFeatures); + RESOLVE_INSTANCE_FUNCTION(GetPhysicalDeviceFormatProperties); + RESOLVE_INSTANCE_FUNCTION(CreateDevice); + RESOLVE_INSTANCE_FUNCTION(GetDeviceQueue); + RESOLVE_INSTANCE_FUNCTION(EnumeratePhysicalDeviceGroups); + RESOLVE_INSTANCE_FUNCTION(GetPhysicalDeviceProperties); + RESOLVE_INSTANCE_FUNCTION(GetPhysicalDeviceProperties2); + RESOLVE_INSTANCE_FUNCTION(GetPhysicalDeviceFeatures); + RESOLVE_INSTANCE_FUNCTION(GetPhysicalDeviceFeatures2); + RESOLVE_INSTANCE_FUNCTION(GetPhysicalDeviceQueueFamilyProperties); + RESOLVE_INSTANCE_FUNCTION(EnumerateDeviceExtensionProperties); + RESOLVE_INSTANCE_FUNCTION(GetPhysicalDeviceSurfaceFormatsKHR); + RESOLVE_INSTANCE_FUNCTION(GetPhysicalDeviceSurfaceSupportKHR); + RESOLVE_INSTANCE_FUNCTION(GetPhysicalDeviceSurfaceCapabilitiesKHR); + RESOLVE_INSTANCE_FUNCTION(GetPhysicalDeviceSurfacePresentModesKHR); + RESOLVE_INSTANCE_FUNCTION(DestroySurfaceKHR); - RETURN_ON_FAILURE(GetLog(), result == VK_SUCCESS, GetReturnCode(result), - "Can't create a debug utils messenger callback: vkCreateDebugUtilsMessengerEXT returned %d.", (int32_t)result); - } +#if VK_USE_PLATFORM_WIN32_KHR + RESOLVE_INSTANCE_FUNCTION(CreateWin32SurfaceKHR); +#endif +#if VK_USE_PLATFORM_METAL_EXT + RESOLVE_INSTANCE_FUNCTION(CreateMetalSurfaceEXT); +#endif +#if VK_USE_PLATFORM_XLIB_KHR + RESOLVE_INSTANCE_FUNCTION(CreateXlibSurfaceKHR); +#endif +#if VK_USE_PLATFORM_WAYLAND_KHR + RESOLVE_INSTANCE_FUNCTION(CreateWaylandSurfaceKHR); +#endif return Result::SUCCESS; } -Result DeviceVK::FindPhysicalDeviceGroup(const PhysicalDeviceGroup* physicalDeviceGroup, bool enableMGPU) +Result DeviceVK::ResolveDispatchTable() { - uint32_t deviceGroupNum = 0; - m_VK.EnumeratePhysicalDeviceGroups(m_Instance, &deviceGroupNum, nullptr); + RESOLVE_DEVICE_FUNCTION(CreateBuffer); + RESOLVE_DEVICE_FUNCTION(CreateImage); + RESOLVE_DEVICE_FUNCTION(CreateBufferView); + RESOLVE_DEVICE_FUNCTION(CreateImageView); + RESOLVE_DEVICE_FUNCTION(CreateSampler); + RESOLVE_DEVICE_FUNCTION(CreateRenderPass); + RESOLVE_DEVICE_FUNCTION(CreateFramebuffer); + RESOLVE_DEVICE_FUNCTION(CreateQueryPool); + RESOLVE_DEVICE_FUNCTION(CreateCommandPool); + RESOLVE_DEVICE_FUNCTION(CreateSemaphore); + RESOLVE_DEVICE_FUNCTION(CreateDescriptorPool); + RESOLVE_DEVICE_FUNCTION(CreatePipelineLayout); + RESOLVE_DEVICE_FUNCTION(CreateDescriptorSetLayout); + RESOLVE_DEVICE_FUNCTION(CreateShaderModule); + RESOLVE_DEVICE_FUNCTION(CreateGraphicsPipelines); + RESOLVE_DEVICE_FUNCTION(CreateComputePipelines); + RESOLVE_DEVICE_FUNCTION(CreateSwapchainKHR); - VkPhysicalDeviceGroupProperties* deviceGroups = STACK_ALLOC(VkPhysicalDeviceGroupProperties, deviceGroupNum); - VkResult result = m_VK.EnumeratePhysicalDeviceGroups(m_Instance, &deviceGroupNum, deviceGroups); + RESOLVE_DEVICE_FUNCTION(DestroyBuffer); + RESOLVE_DEVICE_FUNCTION(DestroyImage); + RESOLVE_DEVICE_FUNCTION(DestroyBufferView); + RESOLVE_DEVICE_FUNCTION(DestroyImageView); + RESOLVE_DEVICE_FUNCTION(DestroySampler); + RESOLVE_DEVICE_FUNCTION(DestroyRenderPass); + RESOLVE_DEVICE_FUNCTION(DestroyFramebuffer); + RESOLVE_DEVICE_FUNCTION(DestroyQueryPool); + RESOLVE_DEVICE_FUNCTION(DestroyCommandPool); + RESOLVE_DEVICE_FUNCTION(DestroySemaphore); + RESOLVE_DEVICE_FUNCTION(DestroyDescriptorPool); + RESOLVE_DEVICE_FUNCTION(DestroyPipelineLayout); + RESOLVE_DEVICE_FUNCTION(DestroyDescriptorSetLayout); + RESOLVE_DEVICE_FUNCTION(DestroyShaderModule); + RESOLVE_DEVICE_FUNCTION(DestroyPipeline); + RESOLVE_DEVICE_FUNCTION(DestroySwapchainKHR); - RETURN_ON_FAILURE(GetLog(), result == VK_SUCCESS, GetReturnCode(result), - "Can't enumerate physical devices: vkEnumeratePhysicalDevices returned %d.", (int32_t)result); + RESOLVE_DEVICE_FUNCTION(AllocateMemory); + RESOLVE_DEVICE_FUNCTION(MapMemory); + RESOLVE_DEVICE_FUNCTION(UnmapMemory); + RESOLVE_DEVICE_FUNCTION(FreeMemory); - VkPhysicalDeviceIDProperties idProps = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES }; - VkPhysicalDeviceProperties2 props = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2 }; - props.pNext = &idProps; + RESOLVE_OPTIONAL_DEVICE_FUNCTION(BindBufferMemory2); + if (!m_VK.BindBufferMemory2) + RESOLVE_DEVICE_FUNCTION_WITH_OTHER_NAME(BindBufferMemory2, "vkBindBufferMemory2KHR"); - bool isVulkan11Supported = false; + RESOLVE_OPTIONAL_DEVICE_FUNCTION(BindImageMemory2); + if (!m_VK.BindImageMemory2) + RESOLVE_DEVICE_FUNCTION_WITH_OTHER_NAME(BindImageMemory2, "vkBindImageMemory2KHR"); - uint32_t i = 0; - for (; i < deviceGroupNum && m_PhysicalDevices.empty(); i++) - { - const VkPhysicalDeviceGroupProperties& group = deviceGroups[i]; - m_VK.GetPhysicalDeviceProperties2(group.physicalDevices[0], &props); + RESOLVE_OPTIONAL_DEVICE_FUNCTION(GetBufferMemoryRequirements2); + if (!m_VK.GetBufferMemoryRequirements2) + RESOLVE_DEVICE_FUNCTION_WITH_OTHER_NAME(GetBufferMemoryRequirements2, "vkGetBufferMemoryRequirements2KHR"); - const uint32_t majorVersion = VK_VERSION_MAJOR(props.properties.apiVersion); - const uint32_t minorVersion = VK_VERSION_MINOR(props.properties.apiVersion); - isVulkan11Supported = majorVersion > 1 || (majorVersion == 1 && minorVersion >= 1); + RESOLVE_OPTIONAL_DEVICE_FUNCTION(GetImageMemoryRequirements2); + if (!m_VK.GetImageMemoryRequirements2) + RESOLVE_DEVICE_FUNCTION_WITH_OTHER_NAME(GetImageMemoryRequirements2, "vkGetImageMemoryRequirements2KHR"); - if (physicalDeviceGroup) - { - const uint64_t luid = *(uint64_t*)idProps.deviceLUID; - if (luid == physicalDeviceGroup->luid) - { - RETURN_ON_FAILURE(GetLog(), isVulkan11Supported, Result::UNSUPPORTED, - "Can't create a device: the specified physical device does not support Vulkan 1.1."); - break; - } - } - else - { - if (isVulkan11Supported) - break; - } - } + RESOLVE_DEVICE_FUNCTION(QueueWaitIdle); + RESOLVE_DEVICE_FUNCTION(AcquireNextImageKHR); + RESOLVE_DEVICE_FUNCTION(QueueSubmit); + RESOLVE_DEVICE_FUNCTION(QueuePresentKHR); + + RESOLVE_DEVICE_FUNCTION(GetSemaphoreCounterValue); + RESOLVE_DEVICE_FUNCTION(WaitSemaphores); + + RESOLVE_DEVICE_FUNCTION(ResetCommandPool); + RESOLVE_DEVICE_FUNCTION(ResetDescriptorPool); + RESOLVE_DEVICE_FUNCTION(AllocateCommandBuffers); + RESOLVE_DEVICE_FUNCTION(AllocateDescriptorSets); + RESOLVE_DEVICE_FUNCTION(FreeCommandBuffers); + RESOLVE_DEVICE_FUNCTION(FreeDescriptorSets); + RESOLVE_DEVICE_FUNCTION(UpdateDescriptorSets); + + RESOLVE_DEVICE_FUNCTION(BeginCommandBuffer); + RESOLVE_DEVICE_FUNCTION(CmdSetDepthBounds); + RESOLVE_DEVICE_FUNCTION(CmdSetViewport); + RESOLVE_DEVICE_FUNCTION(CmdSetScissor); + RESOLVE_DEVICE_FUNCTION(CmdSetStencilReference); + RESOLVE_DEVICE_FUNCTION(CmdClearAttachments); + RESOLVE_DEVICE_FUNCTION(CmdClearColorImage); + RESOLVE_DEVICE_FUNCTION(CmdBeginRenderPass); + RESOLVE_DEVICE_FUNCTION(CmdBindVertexBuffers); + RESOLVE_DEVICE_FUNCTION(CmdBindIndexBuffer); + RESOLVE_DEVICE_FUNCTION(CmdBindPipeline); + RESOLVE_DEVICE_FUNCTION(CmdBindDescriptorSets); + RESOLVE_DEVICE_FUNCTION(CmdPushConstants); + RESOLVE_DEVICE_FUNCTION(CmdDispatch); + RESOLVE_DEVICE_FUNCTION(CmdDispatchIndirect); + RESOLVE_DEVICE_FUNCTION(CmdDraw); + RESOLVE_DEVICE_FUNCTION(CmdDrawIndexed); + RESOLVE_DEVICE_FUNCTION(CmdDrawIndirect); + RESOLVE_DEVICE_FUNCTION(CmdDrawIndexedIndirect); + RESOLVE_DEVICE_FUNCTION(CmdCopyBuffer); + RESOLVE_DEVICE_FUNCTION(CmdCopyImage); + RESOLVE_DEVICE_FUNCTION(CmdCopyBufferToImage); + RESOLVE_DEVICE_FUNCTION(CmdCopyImageToBuffer); + RESOLVE_DEVICE_FUNCTION(CmdPipelineBarrier); + RESOLVE_DEVICE_FUNCTION(CmdBeginQuery); + RESOLVE_DEVICE_FUNCTION(CmdEndQuery); + RESOLVE_DEVICE_FUNCTION(CmdWriteTimestamp); + RESOLVE_DEVICE_FUNCTION(CmdCopyQueryPoolResults); + RESOLVE_DEVICE_FUNCTION(CmdResetQueryPool); + RESOLVE_DEVICE_FUNCTION(CmdEndRenderPass); + RESOLVE_DEVICE_FUNCTION(CmdFillBuffer); + RESOLVE_DEVICE_FUNCTION(EndCommandBuffer); - RETURN_ON_FAILURE(GetLog(), i != deviceGroupNum, Result::UNSUPPORTED, - "Can't create a device: physical device not found."); + RESOLVE_DEVICE_FUNCTION(GetSwapchainImagesKHR); - const VkPhysicalDeviceGroupProperties& group = deviceGroups[i]; + if (m_IsDebugUtilsSupported) + { + RESOLVE_DEVICE_FUNCTION(SetDebugUtilsObjectNameEXT); + RESOLVE_DEVICE_FUNCTION(CmdBeginDebugUtilsLabelEXT); + RESOLVE_DEVICE_FUNCTION(CmdEndDebugUtilsLabelEXT); + } - m_IsSubsetAllocationSupported = true; - if (group.subsetAllocation == VK_FALSE && group.physicalDeviceCount > 1) + if (m_IsHDRExtSupported) { - m_IsSubsetAllocationSupported = false; - REPORT_WARNING(GetLog(), "The device group does not support memory allocation on a subset of the physical devices."); + RESOLVE_OPTIONAL_DEVICE_FUNCTION(SetHdrMetadataEXT); + m_IsHDRExtSupported = m_VK.SetHdrMetadataEXT != nullptr; } - m_PhysicalDevices.insert(m_PhysicalDevices.begin(), group.physicalDevices, group.physicalDevices + group.physicalDeviceCount); + if (m_IsRayTracingExtSupported) + { + RESOLVE_DEVICE_FUNCTION(CreateAccelerationStructureKHR); + RESOLVE_DEVICE_FUNCTION(CreateRayTracingPipelinesKHR); + RESOLVE_DEVICE_FUNCTION(DestroyAccelerationStructureKHR); + RESOLVE_DEVICE_FUNCTION(GetAccelerationStructureDeviceAddressKHR); + RESOLVE_DEVICE_FUNCTION(GetAccelerationStructureBuildSizesKHR); + RESOLVE_DEVICE_FUNCTION(GetRayTracingShaderGroupHandlesKHR); + RESOLVE_DEVICE_FUNCTION(CmdBuildAccelerationStructuresKHR); + RESOLVE_DEVICE_FUNCTION(CmdCopyAccelerationStructureKHR); + RESOLVE_DEVICE_FUNCTION(CmdWriteAccelerationStructuresPropertiesKHR); + RESOLVE_DEVICE_FUNCTION(CmdTraceRaysKHR); + RESOLVE_DEVICE_FUNCTION(GetBufferDeviceAddress); + } - if (!enableMGPU) - m_PhysicalDevices.resize(1); + if (m_IsMeshShaderExtSupported) + { + RESOLVE_DEVICE_FUNCTION(CmdDrawMeshTasksEXT); + } return Result::SUCCESS; } -inline uint8_t GetMaxSampleCount(VkSampleCountFlags flags) +void DeviceVK::Destroy() { - return (uint8_t)flags; + Deallocate(GetStdAllocator(), this); } -void DeviceVK::SetDeviceLimits(bool enableValidation) +Result CreateDeviceVK(const DeviceCreationDesc& deviceCreationDesc, DeviceBase*& device) { - uint8_t conservativeRasterTier = 0; - if (m_IsConservativeRasterExtSupported) - { - VkPhysicalDeviceConservativeRasterizationPropertiesEXT cr = { - VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CONSERVATIVE_RASTERIZATION_PROPERTIES_EXT - }; - VkPhysicalDeviceProperties2 props = { - VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, - &cr - }; - m_VK.GetPhysicalDeviceProperties2(m_PhysicalDevices.front(), &props); - - if ( cr.fullyCoveredFragmentShaderInputVariable && cr.primitiveOverestimationSize <= (1.0 / 256.0f) ) - conservativeRasterTier = 3; - else if ( cr.degenerateTrianglesRasterized && cr.primitiveOverestimationSize < (1.0f / 2.0f) ) - conservativeRasterTier = 2; - else - conservativeRasterTier = 1; - } - - VkPhysicalDeviceFeatures features = {}; - m_VK.GetPhysicalDeviceFeatures(m_PhysicalDevices.front(), &features); - - uint32_t familyNum = 0; - m_VK.GetPhysicalDeviceQueueFamilyProperties(m_PhysicalDevices.front(), &familyNum, nullptr); - - Vector familyProperties(familyNum, m_StdAllocator); - m_VK.GetPhysicalDeviceQueueFamilyProperties(m_PhysicalDevices.front(), &familyNum, familyProperties.data()); - - uint32_t copyQueueTimestampValidBits = 0; - const uint32_t copyQueueFamilyIndex = m_FamilyIndices[(uint32_t)CommandQueueType::COPY]; - if (copyQueueFamilyIndex != std::numeric_limits::max()) - copyQueueTimestampValidBits = familyProperties[copyQueueFamilyIndex].timestampValidBits; - - VkPhysicalDeviceIDProperties IDProperties = {}; - IDProperties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES; - - VkPhysicalDeviceProperties2 props = {}; - props.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2; - props.pNext = &IDProperties; - - m_VK.GetPhysicalDeviceProperties2(m_PhysicalDevices.front(), &props); - const VkPhysicalDeviceLimits& limits = props.properties.limits; - - static_assert(VK_LUID_SIZE == sizeof(uint64_t), "invalid sizeof"); - m_LUID = *(uint64_t*)IDProperties.deviceLUID; - - m_DeviceDesc.graphicsAPI = GraphicsAPI::VULKAN; - m_DeviceDesc.vendor = GetVendorFromID(props.properties.vendorID); - m_DeviceDesc.nriVersionMajor = NRI_VERSION_MAJOR; - m_DeviceDesc.nriVersionMinor = NRI_VERSION_MINOR; - - m_DeviceDesc.viewportMaxNum = limits.maxViewports; - m_DeviceDesc.viewportSubPixelBits = limits.viewportSubPixelBits; - m_DeviceDesc.viewportBoundsRange[0] = int32_t(limits.viewportBoundsRange[0]); - m_DeviceDesc.viewportBoundsRange[1] = int32_t(limits.viewportBoundsRange[1]); - - m_DeviceDesc.frameBufferMaxDim = std::min(limits.maxFramebufferWidth, limits.maxFramebufferHeight); - m_DeviceDesc.frameBufferLayerMaxNum = limits.maxFramebufferLayers; - m_DeviceDesc.framebufferColorAttachmentMaxNum = limits.maxColorAttachments; - - m_DeviceDesc.frameBufferColorSampleMaxNum = GetMaxSampleCount(limits.framebufferColorSampleCounts); - m_DeviceDesc.frameBufferDepthSampleMaxNum = GetMaxSampleCount(limits.framebufferDepthSampleCounts); - m_DeviceDesc.frameBufferStencilSampleMaxNum = GetMaxSampleCount(limits.framebufferStencilSampleCounts); - m_DeviceDesc.frameBufferNoAttachmentsSampleMaxNum = GetMaxSampleCount(limits.framebufferNoAttachmentsSampleCounts); - m_DeviceDesc.textureColorSampleMaxNum = GetMaxSampleCount(limits.sampledImageColorSampleCounts); - m_DeviceDesc.textureIntegerSampleMaxNum = GetMaxSampleCount(limits.sampledImageIntegerSampleCounts); - m_DeviceDesc.textureDepthSampleMaxNum = GetMaxSampleCount(limits.sampledImageDepthSampleCounts); - m_DeviceDesc.textureStencilSampleMaxNum = GetMaxSampleCount(limits.sampledImageStencilSampleCounts); - m_DeviceDesc.storageTextureSampleMaxNum = GetMaxSampleCount(limits.storageImageSampleCounts); - - m_DeviceDesc.texture1DMaxDim = limits.maxImageDimension1D; - m_DeviceDesc.texture2DMaxDim = limits.maxImageDimension2D; - m_DeviceDesc.texture3DMaxDim = limits.maxImageDimension3D; - m_DeviceDesc.textureArrayMaxDim = limits.maxImageArrayLayers; - m_DeviceDesc.texelBufferMaxDim = limits.maxTexelBufferElements; - - m_DeviceDesc.memoryAllocationMaxNum = limits.maxMemoryAllocationCount; - m_DeviceDesc.samplerAllocationMaxNum = limits.maxSamplerAllocationCount; - m_DeviceDesc.uploadBufferTextureRowAlignment = 1; - m_DeviceDesc.uploadBufferTextureSliceAlignment = 1; - m_DeviceDesc.typedBufferOffsetAlignment = (uint32_t)limits.minTexelBufferOffsetAlignment; - m_DeviceDesc.constantBufferOffsetAlignment = (uint32_t)limits.minUniformBufferOffsetAlignment; - m_DeviceDesc.constantBufferMaxRange = limits.maxUniformBufferRange; - m_DeviceDesc.storageBufferOffsetAlignment = (uint32_t)limits.minStorageBufferOffsetAlignment; - m_DeviceDesc.storageBufferMaxRange = limits.maxStorageBufferRange; - m_DeviceDesc.pushConstantsMaxSize = limits.maxPushConstantsSize; - m_DeviceDesc.bufferMaxSize = std::numeric_limits::max(); - m_DeviceDesc.bufferTextureGranularity = (uint32_t)limits.bufferImageGranularity; - - m_DeviceDesc.boundDescriptorSetMaxNum = limits.maxBoundDescriptorSets; - m_DeviceDesc.perStageDescriptorSamplerMaxNum = limits.maxPerStageDescriptorSamplers; - m_DeviceDesc.perStageDescriptorConstantBufferMaxNum = limits.maxPerStageDescriptorUniformBuffers; - m_DeviceDesc.perStageDescriptorStorageBufferMaxNum = limits.maxPerStageDescriptorStorageBuffers; - m_DeviceDesc.perStageDescriptorTextureMaxNum = limits.maxPerStageDescriptorSampledImages; - m_DeviceDesc.perStageDescriptorStorageTextureMaxNum = limits.maxPerStageDescriptorStorageImages; - m_DeviceDesc.perStageResourceMaxNum = limits.maxPerStageResources; - - m_DeviceDesc.descriptorSetSamplerMaxNum = limits.maxDescriptorSetSamplers; - m_DeviceDesc.descriptorSetConstantBufferMaxNum = limits.maxDescriptorSetUniformBuffers; - m_DeviceDesc.descriptorSetStorageBufferMaxNum = limits.maxDescriptorSetStorageBuffers; - m_DeviceDesc.descriptorSetTextureMaxNum = limits.maxDescriptorSetSampledImages; - m_DeviceDesc.descriptorSetStorageTextureMaxNum = limits.maxDescriptorSetStorageImages; - - m_DeviceDesc.vertexShaderAttributeMaxNum = limits.maxVertexInputAttributes; - m_DeviceDesc.vertexShaderStreamMaxNum = limits.maxVertexInputBindings; - m_DeviceDesc.vertexShaderOutputComponentMaxNum = limits.maxVertexOutputComponents; - - m_DeviceDesc.tessControlShaderGenerationMaxLevel = (float)limits.maxTessellationGenerationLevel; - m_DeviceDesc.tessControlShaderPatchPointMaxNum = limits.maxTessellationPatchSize; - m_DeviceDesc.tessControlShaderPerVertexInputComponentMaxNum = limits.maxTessellationControlPerVertexInputComponents; - m_DeviceDesc.tessControlShaderPerVertexOutputComponentMaxNum = limits.maxTessellationControlPerVertexOutputComponents; - m_DeviceDesc.tessControlShaderPerPatchOutputComponentMaxNum = limits.maxTessellationControlPerPatchOutputComponents; - m_DeviceDesc.tessControlShaderTotalOutputComponentMaxNum = limits.maxTessellationControlTotalOutputComponents; - - m_DeviceDesc.tessEvaluationShaderInputComponentMaxNum = limits.maxTessellationEvaluationInputComponents; - m_DeviceDesc.tessEvaluationShaderOutputComponentMaxNum = limits.maxTessellationEvaluationOutputComponents; - - m_DeviceDesc.geometryShaderInvocationMaxNum = limits.maxGeometryShaderInvocations; - m_DeviceDesc.geometryShaderInputComponentMaxNum = limits.maxGeometryInputComponents; - m_DeviceDesc.geometryShaderOutputComponentMaxNum = limits.maxGeometryOutputComponents; - m_DeviceDesc.geometryShaderOutputVertexMaxNum = limits.maxGeometryOutputVertices; - m_DeviceDesc.geometryShaderTotalOutputComponentMaxNum = limits.maxGeometryTotalOutputComponents; + Log log(GraphicsAPI::VULKAN, deviceCreationDesc.callbackInterface); + StdAllocator allocator(deviceCreationDesc.memoryAllocatorInterface); - m_DeviceDesc.fragmentShaderInputComponentMaxNum = limits.maxFragmentInputComponents; - m_DeviceDesc.fragmentShaderOutputAttachmentMaxNum = limits.maxFragmentOutputAttachments; - m_DeviceDesc.fragmentShaderDualSourceAttachmentMaxNum = limits.maxFragmentDualSrcAttachments; - m_DeviceDesc.fragmentShaderCombinedOutputResourceMaxNum = limits.maxFragmentCombinedOutputResources; + DeviceVK* implementation = Allocate(allocator, log, allocator); - m_DeviceDesc.computeShaderSharedMemoryMaxSize = limits.maxComputeSharedMemorySize; - m_DeviceDesc.computeShaderWorkGroupMaxNum[0] = limits.maxComputeWorkGroupCount[0]; - m_DeviceDesc.computeShaderWorkGroupMaxNum[1] = limits.maxComputeWorkGroupCount[1]; - m_DeviceDesc.computeShaderWorkGroupMaxNum[2] = limits.maxComputeWorkGroupCount[2]; - m_DeviceDesc.computeShaderWorkGroupInvocationMaxNum = limits.maxComputeWorkGroupInvocations; - m_DeviceDesc.computeShaderWorkGroupMaxDim[0] = limits.maxComputeWorkGroupSize[0]; - m_DeviceDesc.computeShaderWorkGroupMaxDim[1] = limits.maxComputeWorkGroupSize[1]; - m_DeviceDesc.computeShaderWorkGroupMaxDim[2] = limits.maxComputeWorkGroupSize[2]; + const Result res = implementation->Create(deviceCreationDesc); - m_DeviceDesc.timestampFrequencyHz = uint64_t( 1e9 / double(limits.timestampPeriod) + 0.5 ); - m_DeviceDesc.subPixelPrecisionBits = limits.subPixelPrecisionBits; - m_DeviceDesc.subTexelPrecisionBits = limits.subTexelPrecisionBits; - m_DeviceDesc.mipmapPrecisionBits = limits.mipmapPrecisionBits; - m_DeviceDesc.drawIndexedIndex16ValueMax = std::min(std::numeric_limits::max(), limits.maxDrawIndexedIndexValue); - m_DeviceDesc.drawIndexedIndex32ValueMax = limits.maxDrawIndexedIndexValue; - m_DeviceDesc.drawIndirectMaxNum = limits.maxDrawIndirectCount; - m_DeviceDesc.samplerLodBiasMin = -limits.maxSamplerLodBias; - m_DeviceDesc.samplerLodBiasMax = limits.maxSamplerLodBias; - m_DeviceDesc.samplerAnisotropyMax = limits.maxSamplerAnisotropy; - m_DeviceDesc.texelOffsetMin = limits.minTexelOffset; - m_DeviceDesc.texelOffsetMax = limits.maxTexelOffset; - m_DeviceDesc.texelGatherOffsetMin = limits.minTexelGatherOffset; - m_DeviceDesc.texelGatherOffsetMax = limits.maxTexelGatherOffset; - m_DeviceDesc.clipDistanceMaxNum = limits.maxClipDistances; - m_DeviceDesc.cullDistanceMaxNum = limits.maxCullDistances; - m_DeviceDesc.combinedClipAndCullDistanceMaxNum = limits.maxCombinedClipAndCullDistances; - m_DeviceDesc.conservativeRasterTier = conservativeRasterTier; - m_DeviceDesc.physicalDeviceNum = (uint8_t)m_PhysicalDevices.size(); + if (res == Result::SUCCESS) + { + device = implementation; + return Result::SUCCESS; + } - m_DeviceDesc.isAPIValidationEnabled = enableValidation; - m_DeviceDesc.isTextureFilterMinMaxSupported = m_IsMinMaxFilterExtSupported; - m_DeviceDesc.isLogicOpSupported = features.logicOp; - m_DeviceDesc.isDepthBoundsTestSupported = features.depthBounds; - m_DeviceDesc.isProgrammableSampleLocationsSupported = m_IsSampleLocationExtSupported; - m_DeviceDesc.isComputeQueueSupported = m_Queues[(uint32_t)CommandQueueType::COMPUTE] != nullptr; - m_DeviceDesc.isCopyQueueSupported = m_Queues[(uint32_t)CommandQueueType::COPY] != nullptr; - m_DeviceDesc.isCopyQueueTimestampSupported = copyQueueTimestampValidBits == 64; - m_DeviceDesc.isRegisterAliasingSupported = true; - m_DeviceDesc.isSubsetAllocationSupported = m_IsSubsetAllocationSupported; - m_DeviceDesc.isFloat16Supported = m_IsFP16Supported; + Deallocate(allocator, implementation); + return res; } -bool DeviceVK::FilterDeviceExtensions(Vector& extensions) +Result CreateDeviceVK(const DeviceCreationVulkanDesc& deviceCreationDesc, DeviceBase*& device) { - uint32_t extensionNum = 0; - m_VK.EnumerateDeviceExtensionProperties(m_PhysicalDevices.front(), nullptr, &extensionNum, nullptr); + Log log(GraphicsAPI::VULKAN, deviceCreationDesc.callbackInterface); + StdAllocator allocator(deviceCreationDesc.memoryAllocatorInterface); - Vector supportedExtensions(extensionNum, GetStdAllocator()); - m_VK.EnumerateDeviceExtensionProperties(m_PhysicalDevices.front(), nullptr, &extensionNum, supportedExtensions.data()); + DeviceVK* implementation = Allocate(allocator, log, allocator); + const Result res = implementation->Create(deviceCreationDesc); - bool allFound = true; - for (size_t i = 0; i < extensions.size(); i++) + if (res == Result::SUCCESS) { - bool found = false; - for (uint32_t j = 0; j < extensionNum && !found; j++) - { - if (strcmp(supportedExtensions[j].extensionName, extensions[i]) == 0) - found = true; - } - - if (!found) - { - extensions.erase(extensions.begin() + i--); - allFound = false; - } + device = implementation; + return Result::SUCCESS; } - return allFound; + Deallocate(allocator, implementation); + return res; } -void DeviceVK::FillFamilyIndices(bool useEnabledFamilyIndices, const uint32_t* enabledFamilyIndices, uint32_t familyIndexNum) +//================================================================================================================ +// NRI +//================================================================================================================ + +inline void DeviceVK::SetDebugName(const char* name) { - uint32_t familyNum = 0; - m_VK.GetPhysicalDeviceQueueFamilyProperties(m_PhysicalDevices.front(), &familyNum, nullptr); + SetDebugNameToTrivialObject(VK_OBJECT_TYPE_DEVICE, (uint64_t)m_Device, name); +} - Vector familyProps(familyNum, GetStdAllocator()); - m_VK.GetPhysicalDeviceQueueFamilyProperties(m_PhysicalDevices.front(), &familyNum, familyProps.data()); +inline Result DeviceVK::GetCommandQueue(CommandQueueType commandQueueType, CommandQueue*& commandQueue) +{ + SharedScope sharedScope(m_Lock); - memset(m_FamilyIndices.data(), 0xff, m_FamilyIndices.size() * sizeof(uint32_t)); + if (m_FamilyIndices[(uint32_t)commandQueueType] == INVALID_FAMILY_INDEX) + return Result::UNSUPPORTED; - for (uint32_t i = 0; i < familyProps.size(); i++) - { - const VkQueueFlags mask = familyProps[i].queueFlags; - const bool graphics = mask & VK_QUEUE_GRAPHICS_BIT; - const bool compute = mask & VK_QUEUE_COMPUTE_BIT; - const bool copy = mask & VK_QUEUE_TRANSFER_BIT; + commandQueue = (CommandQueue*)m_Queues[(uint32_t)commandQueueType]; - if (useEnabledFamilyIndices) - { - bool isFamilyEnabled = false; - for (uint32_t j = 0; j < familyIndexNum && !isFamilyEnabled; j++) - isFamilyEnabled = enabledFamilyIndices[j] == i; + return Result::SUCCESS; +} - if (!isFamilyEnabled) - continue; - } +inline Result DeviceVK::CreateCommandAllocator(const CommandQueue& commandQueue, uint32_t physicalDeviceMask, CommandAllocator*& commandAllocator) +{ + return CreateImplementation(commandAllocator, commandQueue, physicalDeviceMask); +} - if (graphics) - m_FamilyIndices[(uint32_t)CommandQueueType::GRAPHICS] = i; - else if (compute) - m_FamilyIndices[(uint32_t)CommandQueueType::COMPUTE] = i; - else if (copy) - m_FamilyIndices[(uint32_t)CommandQueueType::COPY] = i; - } +inline Result DeviceVK::CreateDescriptorPool(const DescriptorPoolDesc& descriptorPoolDesc, DescriptorPool*& descriptorPool) +{ + return CreateImplementation(descriptorPool, descriptorPoolDesc); } -bool IsExtensionInList( const char* extension, const Vector& list ) +Result DeviceVK::CreateBuffer(const BufferDesc& bufferDesc, Buffer*& buffer) // TODO: not inline { - for (auto& extensionFromList : list) - { - if (strcmp(extension, extensionFromList) == 0) - return true; - } + return CreateImplementation(buffer, bufferDesc); +} - return false; +inline Result DeviceVK::CreateTexture(const TextureDesc& textureDesc, Texture*& texture) +{ + return CreateImplementation(texture, textureDesc); } -void EraseIncompatibleExtension(Vector& extensions, const char* extensionToErase) +inline Result DeviceVK::CreateBufferView(const BufferViewDesc& bufferViewDesc, Descriptor*& bufferView) { - size_t i = 0; - for (; i < extensions.size() && strcmp(extensions[i], extensionToErase) != 0; i++) - {} + return CreateImplementation(bufferView, bufferViewDesc); +} - if (i < extensions.size()) - extensions.erase(extensions.begin() + i); +inline Result DeviceVK::CreateTexture1DView(const Texture1DViewDesc& textureViewDesc, Descriptor*& textureView) +{ + return CreateImplementation(textureView, textureViewDesc); } -void DeviceVK::CheckSupportedInstanceExtensions(const Vector& extensions) +inline Result DeviceVK::CreateTexture2DView(const Texture2DViewDesc& textureViewDesc, Descriptor*& textureView) { - m_IsDebugUtilsSupported = IsExtensionInList(VK_EXT_DEBUG_UTILS_EXTENSION_NAME, extensions); + return CreateImplementation(textureView, textureViewDesc); } -void DeviceVK::CheckSupportedDeviceExtensions(const Vector& extensions) +inline Result DeviceVK::CreateTexture3DView(const Texture3DViewDesc& textureViewDesc, Descriptor*& textureView) { - m_IsDescriptorIndexingExtSupported = IsExtensionInList(VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME, extensions); - m_IsSampleLocationExtSupported = IsExtensionInList(VK_EXT_SAMPLE_LOCATIONS_EXTENSION_NAME, extensions); - m_IsMinMaxFilterExtSupported = IsExtensionInList(VK_EXT_SAMPLE_LOCATIONS_EXTENSION_NAME, extensions); - m_IsConservativeRasterExtSupported = IsExtensionInList(VK_EXT_CONSERVATIVE_RASTERIZATION_EXTENSION_NAME, extensions); - m_IsMeshShaderExtSupported = IsExtensionInList(VK_NV_MESH_SHADER_EXTENSION_NAME, extensions); - m_IsHDRExtSupported = IsExtensionInList(VK_EXT_HDR_METADATA_EXTENSION_NAME, extensions); - m_IsFP16Supported = IsExtensionInList(VK_KHR_SHADER_FLOAT16_INT8_EXTENSION_NAME, extensions); - m_IsBufferDeviceAddressSupported = IsExtensionInList(VK_KHR_BUFFER_DEVICE_ADDRESS_EXTENSION_NAME, extensions); - m_IsMicroMapSupported = IsExtensionInList(VK_EXT_OPACITY_MICROMAP_EXTENSION_NAME, extensions); + return CreateImplementation(textureView, textureViewDesc); +} - m_IsRayTracingExtSupported = m_IsDescriptorIndexingExtSupported; - m_IsRayTracingExtSupported = m_IsRayTracingExtSupported && IsExtensionInList(VK_KHR_DEFERRED_HOST_OPERATIONS_EXTENSION_NAME, extensions); - m_IsRayTracingExtSupported = m_IsRayTracingExtSupported && IsExtensionInList(VK_KHR_ACCELERATION_STRUCTURE_EXTENSION_NAME, extensions); - m_IsRayTracingExtSupported = m_IsRayTracingExtSupported && IsExtensionInList(VK_KHR_PIPELINE_LIBRARY_EXTENSION_NAME, extensions); - m_IsRayTracingExtSupported = m_IsRayTracingExtSupported && IsExtensionInList(VK_KHR_RAY_TRACING_PIPELINE_EXTENSION_NAME, extensions); +inline Result DeviceVK::CreateSampler(const SamplerDesc& samplerDesc, Descriptor*& sampler) +{ + return CreateImplementation(sampler, samplerDesc); +} - m_IsDemoteToHelperInvocationSupported = IsExtensionInList(VK_EXT_SHADER_DEMOTE_TO_HELPER_INVOCATION_EXTENSION_NAME, extensions); +inline Result DeviceVK::CreatePipelineLayout(const PipelineLayoutDesc& pipelineLayoutDesc, PipelineLayout*& pipelineLayout) +{ + return CreateImplementation(pipelineLayout, pipelineLayoutDesc); } -Result DeviceVK::CreateLogicalDevice(const DeviceCreationDesc& deviceCreationDesc) +inline Result DeviceVK::CreatePipeline(const GraphicsPipelineDesc& graphicsPipelineDesc, Pipeline*& pipeline) { - Vector extensions(GetStdAllocator()); + return CreateImplementation(pipeline, graphicsPipelineDesc); +} - extensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME); +inline Result DeviceVK::CreatePipeline(const ComputePipelineDesc& computePipelineDesc, Pipeline*& pipeline) +{ + return CreateImplementation(pipeline, computePipelineDesc); +} - for (uint32_t i = 0; i < deviceCreationDesc.vulkanExtensions.deviceExtensionNum; i++) - extensions.push_back(deviceCreationDesc.vulkanExtensions.deviceExtensions[i]); +inline Result DeviceVK::CreateFrameBuffer(const FrameBufferDesc& frameBufferDesc, FrameBuffer*& frameBuffer) +{ + return CreateImplementation(frameBuffer, frameBufferDesc); +} - if (!FilterDeviceExtensions(extensions)) - { - REPORT_ERROR(GetLog(), "Can't create VkDevice: Swapchain extension is unsupported."); - return Result::UNSUPPORTED; - } +inline Result DeviceVK::CreateQueryPool(const QueryPoolDesc& queryPoolDesc, QueryPool*& queryPool) +{ + return CreateImplementation(queryPool, queryPoolDesc); +} - extensions.push_back(VK_KHR_DEFERRED_HOST_OPERATIONS_EXTENSION_NAME); - extensions.push_back(VK_KHR_ACCELERATION_STRUCTURE_EXTENSION_NAME); - extensions.push_back(VK_KHR_PIPELINE_LIBRARY_EXTENSION_NAME); - extensions.push_back(VK_KHR_RAY_TRACING_PIPELINE_EXTENSION_NAME); - extensions.push_back(VK_KHR_RAY_QUERY_EXTENSION_NAME); - extensions.push_back(VK_NV_MESH_SHADER_EXTENSION_NAME); - extensions.push_back(VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME); - extensions.push_back(VK_EXT_SAMPLE_LOCATIONS_EXTENSION_NAME); - extensions.push_back(VK_EXT_SAMPLER_FILTER_MINMAX_EXTENSION_NAME); - extensions.push_back(VK_EXT_CONSERVATIVE_RASTERIZATION_EXTENSION_NAME); - extensions.push_back(VK_EXT_SHADER_DEMOTE_TO_HELPER_INVOCATION_EXTENSION_NAME); - extensions.push_back(VK_EXT_HDR_METADATA_EXTENSION_NAME); - extensions.push_back(VK_KHR_SHADER_FLOAT16_INT8_EXTENSION_NAME); - extensions.push_back(VK_EXT_OPACITY_MICROMAP_EXTENSION_NAME); - extensions.push_back(VK_KHR_SYNCHRONIZATION_2_EXTENSION_NAME); +inline Result DeviceVK::CreateFence(uint64_t initialValue, Fence*& fence) +{ + return CreateImplementation(fence, initialValue); +} - FilterDeviceExtensions(extensions); +inline Result DeviceVK::CreateSwapChain(const SwapChainDesc& swapChainDesc, SwapChain*& swapChain) +{ + return CreateImplementation(swapChain, swapChainDesc); +} - EraseIncompatibleExtension(extensions, VK_EXT_BUFFER_DEVICE_ADDRESS_EXTENSION_NAME); +inline Result DeviceVK::CreatePipeline(const RayTracingPipelineDesc& rayTracingPipelineDesc, Pipeline*& pipeline) +{ + return CreateImplementation(pipeline, rayTracingPipelineDesc); +} - CheckSupportedDeviceExtensions(extensions); +inline Result DeviceVK::CreateAccelerationStructure(const AccelerationStructureDesc& accelerationStructureDesc, AccelerationStructure*& accelerationStructure) +{ + return CreateImplementation(accelerationStructure, accelerationStructureDesc); +} - VkPhysicalDeviceFeatures2 deviceFeatures2 = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2 }; +inline Result DeviceVK::CreateCommandQueue(const CommandQueueVulkanDesc& commandQueueVulkanDesc, CommandQueue*& commandQueue) +{ + const uint32_t commandQueueTypeIndex = (uint32_t)commandQueueVulkanDesc.commandQueueType; - VkPhysicalDeviceDescriptorIndexingFeaturesEXT descriptorIndexingFeatures = - { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_INDEXING_FEATURES_EXT }; + ExclusiveScope exclusiveScope(m_Lock); - VkPhysicalDeviceBufferDeviceAddressFeatures bufferDeviceAddressFeatures = - { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BUFFER_DEVICE_ADDRESS_FEATURES }; + const bool isFamilyIndexSame = m_FamilyIndices[commandQueueTypeIndex] == commandQueueVulkanDesc.familyIndex; + const bool isQueueSame = (VkQueue)m_Queues[commandQueueTypeIndex] == (VkQueue)commandQueueVulkanDesc.vkQueue; + if (isFamilyIndexSame && isQueueSame) + { + commandQueue = (CommandQueue*)m_Queues[commandQueueTypeIndex]; + return Result::SUCCESS; + } - VkPhysicalDeviceShaderDemoteToHelperInvocationFeaturesEXT demoteToHelperInvocationFeatures = - { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_DEMOTE_TO_HELPER_INVOCATION_FEATURES_EXT }; + CreateImplementation(commandQueue, commandQueueVulkanDesc); - VkPhysicalDeviceMeshShaderFeaturesNV meshShaderFeatures = - { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MESH_SHADER_FEATURES_NV }; + if (m_Queues[commandQueueTypeIndex] != nullptr) + Deallocate(GetStdAllocator(), m_Queues[commandQueueTypeIndex]); - VkPhysicalDeviceRayTracingPipelineFeaturesKHR rayTracingFeatures = - { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_TRACING_PIPELINE_FEATURES_KHR }; + m_FamilyIndices[commandQueueTypeIndex] = commandQueueVulkanDesc.familyIndex; + m_Queues[commandQueueTypeIndex] = (CommandQueueVK*)commandQueue; - VkPhysicalDeviceAccelerationStructureFeaturesKHR accelerationStructureFeatures = - { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ACCELERATION_STRUCTURE_FEATURES_KHR }; + return Result::SUCCESS; +} - VkPhysicalDeviceRayQueryFeaturesKHR rayQueryFeatures = - { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_QUERY_FEATURES_KHR }; +inline Result DeviceVK::CreateCommandAllocator(const CommandAllocatorVulkanDesc& commandAllocatorVulkanDesc, CommandAllocator*& commandAllocator) +{ + return CreateImplementation(commandAllocator, commandAllocatorVulkanDesc); +} - VkPhysicalDevice16BitStorageFeatures storageFeatures = - { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES }; +inline Result DeviceVK::CreateCommandBuffer(const CommandBufferVulkanDesc& commandBufferVulkanDesc, CommandBuffer*& commandBuffer) +{ + return CreateImplementation(commandBuffer, commandBufferVulkanDesc); +} - VkPhysicalDeviceFloat16Int8FeaturesKHR float16Int8Features = - { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_FLOAT16_INT8_FEATURES_KHR }; +inline Result DeviceVK::CreateDescriptorPool(NRIVkDescriptorPool vkDescriptorPool, DescriptorPool*& descriptorPool) +{ + return CreateImplementation(descriptorPool, vkDescriptorPool); +} - VkPhysicalDeviceOpacityMicromapFeaturesEXT micromapFeatures = - { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_OPACITY_MICROMAP_FEATURES_EXT }; +inline Result DeviceVK::CreateBuffer(const BufferVulkanDesc& bufferDesc, Buffer*& buffer) +{ + return CreateImplementation(buffer, bufferDesc); +} - VkPhysicalDeviceSynchronization2Features syncronization2Fetures = - { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SYNCHRONIZATION_2_FEATURES }; +inline Result DeviceVK::CreateTexture(const TextureVulkanDesc& textureVulkanDesc, Texture*& texture) +{ + return CreateImplementation(texture, textureVulkanDesc); +} - deviceFeatures2.pNext = &bufferDeviceAddressFeatures; +inline Result DeviceVK::CreateMemory(const MemoryVulkanDesc& memoryVulkanDesc, Memory*& memory) +{ + return CreateImplementation(memory, memoryVulkanDesc); +} - storageFeatures.pNext = deviceFeatures2.pNext; - deviceFeatures2.pNext = &storageFeatures; +inline Result DeviceVK::CreateGraphicsPipeline(NRIVkPipeline vkPipeline, Pipeline*& pipeline) +{ + PipelineVK* implementation = Allocate(GetStdAllocator(), *this); + const Result result = implementation->CreateGraphics(vkPipeline); - if (m_IsDescriptorIndexingExtSupported) + if (result != Result::SUCCESS) { - descriptorIndexingFeatures.pNext = deviceFeatures2.pNext; - deviceFeatures2.pNext = &descriptorIndexingFeatures; + pipeline = (Pipeline*)implementation; + return result; } - if (m_IsDemoteToHelperInvocationSupported) - { - demoteToHelperInvocationFeatures.pNext = deviceFeatures2.pNext; - deviceFeatures2.pNext = &demoteToHelperInvocationFeatures; - } + Deallocate(GetStdAllocator(), implementation); - if (m_IsMeshShaderExtSupported) - { - meshShaderFeatures.pNext = deviceFeatures2.pNext; - deviceFeatures2.pNext = &meshShaderFeatures; - } + return result; +} - if (m_IsRayTracingExtSupported) - { - rayTracingFeatures.pNext = deviceFeatures2.pNext; - deviceFeatures2.pNext = &rayTracingFeatures; - accelerationStructureFeatures.pNext = deviceFeatures2.pNext; - deviceFeatures2.pNext = &accelerationStructureFeatures; - rayQueryFeatures.pNext = deviceFeatures2.pNext; - deviceFeatures2.pNext = &rayQueryFeatures; - } +inline Result DeviceVK::CreateComputePipeline(NRIVkPipeline vkPipeline, Pipeline*& pipeline) +{ + PipelineVK* implementation = Allocate(GetStdAllocator(), *this); + const Result result = implementation->CreateCompute(vkPipeline); - if (m_IsFP16Supported) + if (result != Result::SUCCESS) { - float16Int8Features.pNext = deviceFeatures2.pNext; - deviceFeatures2.pNext = &float16Int8Features; + pipeline = (Pipeline*)implementation; + return result; } - if (m_IsMicroMapSupported) - { - micromapFeatures.pNext = deviceFeatures2.pNext; - deviceFeatures2.pNext = µmapFeatures; - syncronization2Fetures.pNext = deviceFeatures2.pNext; - deviceFeatures2.pNext = &syncronization2Fetures; - } + Deallocate(GetStdAllocator(), implementation); - m_VK.GetPhysicalDeviceFeatures2(m_PhysicalDevices.front(), &deviceFeatures2); + return result; +} - m_IsBufferDeviceAddressSupported = bufferDeviceAddressFeatures.bufferDeviceAddress; +inline Result DeviceVK::CreateQueryPool(const QueryPoolVulkanDesc& queryPoolVulkanDesc, QueryPool*& queryPool) +{ + return CreateImplementation(queryPool, queryPoolVulkanDesc); +} - if (!deviceCreationDesc.enableAPIValidation) - deviceFeatures2.features.robustBufferAccess = false; - deviceFeatures2.features.inheritedQueries = false; - deviceFeatures2.features.occlusionQueryPrecise = false; +inline Result DeviceVK::CreateAccelerationStructure(const AccelerationStructureVulkanDesc& accelerationStructureDesc, AccelerationStructure*& accelerationStructure) +{ + return CreateImplementation(accelerationStructure, accelerationStructureDesc); +} - Vector queues(GetStdAllocator()); - const float priorities = 1.0f; - for (size_t i = 0; i < m_FamilyIndices.size(); i++) - { - if (m_FamilyIndices[i] == std::numeric_limits::max()) - continue; +inline void DeviceVK::DestroyCommandAllocator(CommandAllocator& commandAllocator) +{ + Deallocate(GetStdAllocator(), (CommandAllocatorVK*)&commandAllocator); +} - VkDeviceQueueCreateInfo info = { VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO }; - info.queueCount = 1; - info.queueFamilyIndex = m_FamilyIndices[i]; - info.pQueuePriorities = &priorities; - queues.push_back(info); - } +inline void DeviceVK::DestroyDescriptorPool(DescriptorPool& descriptorPool) +{ + Deallocate(GetStdAllocator(), (DescriptorPoolVK*)&descriptorPool); +} - VkDeviceCreateInfo deviceCreateInfo = { VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO }; - deviceCreateInfo.pNext = &deviceFeatures2; - deviceCreateInfo.queueCreateInfoCount = (uint32_t)queues.size(); - deviceCreateInfo.pQueueCreateInfos = queues.data(); - deviceCreateInfo.enabledExtensionCount = (uint32_t)extensions.size(); - deviceCreateInfo.ppEnabledExtensionNames = extensions.data(); +void DeviceVK::DestroyBuffer(Buffer& buffer) // TODO: not inline +{ + Deallocate(GetStdAllocator(), (BufferVK*)&buffer); +} - VkDeviceGroupDeviceCreateInfo deviceGroupInfo; - if (m_PhysicalDevices.size() > 1) - { - deviceGroupInfo = { VK_STRUCTURE_TYPE_DEVICE_GROUP_DEVICE_CREATE_INFO }; - deviceGroupInfo.pNext = deviceCreateInfo.pNext; - deviceGroupInfo.physicalDeviceCount = (uint32_t)m_PhysicalDevices.size(); - deviceGroupInfo.pPhysicalDevices = m_PhysicalDevices.data(); - deviceCreateInfo.pNext = &deviceGroupInfo; - } +inline void DeviceVK::DestroyTexture(Texture& texture) +{ + Deallocate(GetStdAllocator(), (TextureVK*)&texture); +} - const VkResult result = m_VK.CreateDevice(m_PhysicalDevices.front(), &deviceCreateInfo, m_AllocationCallbackPtr, &m_Device); +inline void DeviceVK::DestroyDescriptor(Descriptor& descriptor) +{ + Deallocate(GetStdAllocator(), (DescriptorVK*)&descriptor); +} - RETURN_ON_FAILURE(GetLog(), result == VK_SUCCESS, GetReturnCode(result), "Can't create a device: " - "vkCreateDevice returned %d.", (int32_t)result); +inline void DeviceVK::DestroyPipelineLayout(PipelineLayout& pipelineLayout) +{ + Deallocate(GetStdAllocator(), (PipelineLayoutVK*)&pipelineLayout); +} - m_IsFP16Supported = float16Int8Features.shaderFloat16 != VK_FALSE; +inline void DeviceVK::DestroyPipeline(Pipeline& pipeline) +{ + Deallocate(GetStdAllocator(), (PipelineVK*)&pipeline); +} - return Result::SUCCESS; +inline void DeviceVK::DestroyFrameBuffer(FrameBuffer& frameBuffer) +{ + Deallocate(GetStdAllocator(), (FrameBufferVK*)&frameBuffer); } -void DeviceVK::CreateCommandQueues() +inline void DeviceVK::DestroyQueryPool(QueryPool& queryPool) { - for (uint32_t i = 0; i < m_FamilyIndices.size(); i++) - { - if (m_FamilyIndices[i] == std::numeric_limits::max()) - continue; + Deallocate(GetStdAllocator(), (QueryPoolVK*)&queryPool); +} - VkQueue handle = VK_NULL_HANDLE; - m_VK.GetDeviceQueue(m_Device, m_FamilyIndices[i], 0, &handle); +inline void DeviceVK::DestroyFence(Fence& fence) +{ + Deallocate(GetStdAllocator(), (FenceVK*)&fence); +} - m_Queues[i] = Allocate(GetStdAllocator(), *this, handle, m_FamilyIndices[i], (CommandQueueType)i); +inline void DeviceVK::DestroySwapChain(SwapChain& swapChain) +{ + Deallocate(GetStdAllocator(), (SwapChainVK*)&swapChain); +} - m_ConcurrentSharingModeQueueIndices.push_back(m_FamilyIndices[i]); +inline void DeviceVK::DestroyAccelerationStructure(AccelerationStructure& accelerationStructure) +{ + Deallocate(GetStdAllocator(), (AccelerationStructureVK*)&accelerationStructure); +} + +inline Result DeviceVK::GetDisplays(Display** displays, uint32_t& displayNum) +{ + MaybeUnused(displays, displayNum); + +#if _WIN32 + if (!m_Adapter) + return Result::UNSUPPORTED; + + HRESULT result = S_OK; + + if (displays == nullptr || displayNum == 0) + { + UINT i = 0; + for(; result != DXGI_ERROR_NOT_FOUND; i++) + { + ComPtr output; + result = m_Adapter->EnumOutputs(i, &output); + } + + displayNum = i; + return Result::SUCCESS; + } + + UINT i = 0; + for(; result != DXGI_ERROR_NOT_FOUND && i < displayNum; i++) + { + ComPtr output; + result = m_Adapter->EnumOutputs(i, &output); + if (result != DXGI_ERROR_NOT_FOUND) + displays[i] = (Display*)(size_t)(i + 1); } + + for(; i < displayNum; i++) + displays[i] = nullptr; + + return Result::SUCCESS; +#else + return Result::UNSUPPORTED; +#endif } -void DeviceVK::RetrieveRayTracingInfo() +inline Result DeviceVK::GetDisplaySize(Display& display, uint16_t& width, uint16_t& height) { - m_RayTracingDeviceProperties = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_TRACING_PIPELINE_PROPERTIES_KHR }; + MaybeUnused(display, width, height); - if (!m_IsRayTracingExtSupported) - return; +#if _WIN32 + if (!m_Adapter) + return Result::UNSUPPORTED; - VkPhysicalDeviceAccelerationStructurePropertiesKHR accelerationStructureProperties = - { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ACCELERATION_STRUCTURE_PROPERTIES_KHR }; + Display* address = &display; + if (!address) + return Result::UNSUPPORTED; - VkPhysicalDeviceProperties2 props = { - VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, - &m_RayTracingDeviceProperties - }; + const uint32_t index = (*(uint32_t*)&address) - 1; - m_RayTracingDeviceProperties.pNext = &accelerationStructureProperties; + ComPtr output; + HRESULT result = m_Adapter->EnumOutputs(index, &output); - m_VK.GetPhysicalDeviceProperties2(m_PhysicalDevices.front(), &props); + if (FAILED(result)) + return Result::UNSUPPORTED; - m_DeviceDesc.rayTracingShaderGroupIdentifierSize = m_RayTracingDeviceProperties.shaderGroupHandleSize; - m_DeviceDesc.rayTracingShaderRecursionMaxDepth = m_RayTracingDeviceProperties.maxRayRecursionDepth; - m_DeviceDesc.rayTracingGeometryObjectMaxNum = (uint32_t)accelerationStructureProperties.maxGeometryCount; - m_DeviceDesc.rayTracingShaderTableAligment = m_RayTracingDeviceProperties.shaderGroupBaseAlignment; - m_DeviceDesc.rayTracingShaderTableMaxStride = m_RayTracingDeviceProperties.maxShaderGroupStride; -} + DXGI_OUTPUT_DESC outputDesc = {}; + result = output->GetDesc(&outputDesc); -void DeviceVK::RetrieveMeshShaderInfo() -{ - VkPhysicalDeviceMeshShaderPropertiesNV meshShaderProperties = - { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MESH_SHADER_PROPERTIES_NV }; + if (FAILED(result)) + return Result::UNSUPPORTED; - if (!m_IsMeshShaderExtSupported) - return; + MONITORINFO monitorInfo = {}; + monitorInfo.cbSize = sizeof(monitorInfo); - VkPhysicalDeviceProperties2 props = { - VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, - &meshShaderProperties - }; + if (!GetMonitorInfoA(outputDesc.Monitor, &monitorInfo)) + return Result::UNSUPPORTED; - m_VK.GetPhysicalDeviceProperties2(m_PhysicalDevices.front(), &props); + const RECT rect = monitorInfo.rcMonitor; + + width = uint16_t(rect.right - rect.left); + height = uint16_t(rect.bottom - rect.top); - m_DeviceDesc.meshTaskMaxNum = meshShaderProperties.maxDrawMeshTasksCount; - m_DeviceDesc.meshTaskWorkGroupInvocationMaxNum = meshShaderProperties.maxTaskWorkGroupInvocations; - m_DeviceDesc.meshTaskWorkGroupMaxDim[0] = meshShaderProperties.maxTaskWorkGroupSize[0]; - m_DeviceDesc.meshTaskWorkGroupMaxDim[1] = meshShaderProperties.maxTaskWorkGroupSize[1]; - m_DeviceDesc.meshTaskWorkGroupMaxDim[2] = meshShaderProperties.maxTaskWorkGroupSize[2]; - m_DeviceDesc.meshTaskTotalMemoryMaxSize = meshShaderProperties.maxTaskTotalMemorySize; - m_DeviceDesc.meshTaskOutputMaxNum = meshShaderProperties.maxTaskOutputCount; - m_DeviceDesc.meshWorkGroupInvocationMaxNum = meshShaderProperties.maxMeshWorkGroupInvocations; - m_DeviceDesc.meshWorkGroupMaxDim[0] = meshShaderProperties.maxMeshWorkGroupSize[0]; - m_DeviceDesc.meshWorkGroupMaxDim[1] = meshShaderProperties.maxMeshWorkGroupSize[1]; - m_DeviceDesc.meshWorkGroupMaxDim[2] = meshShaderProperties.maxMeshWorkGroupSize[2]; - m_DeviceDesc.meshTotalMemoryMaxSize = meshShaderProperties.maxMeshTotalMemorySize; - m_DeviceDesc.meshOutputVertexMaxNum = meshShaderProperties.maxMeshOutputVertices; - m_DeviceDesc.meshOutputPrimitiveMaxNum = meshShaderProperties.maxMeshOutputPrimitives; - m_DeviceDesc.meshMultiviewViewMaxNum = meshShaderProperties.maxMeshMultiviewViewCount; - m_DeviceDesc.meshOutputPerVertexGranularity = meshShaderProperties.meshOutputPerVertexGranularity; - m_DeviceDesc.meshOutputPerPrimitiveGranularity = meshShaderProperties.meshOutputPerPrimitiveGranularity; + return Result::SUCCESS; +#else + return Result::UNSUPPORTED; +#endif } -void DeviceVK::SetDebugNameToTrivialObject(VkObjectType objectType, uint64_t handle, const char* name) +inline Result DeviceVK::AllocateMemory(uint32_t physicalDeviceMask, MemoryType memoryType, uint64_t size, Memory*& memory) { - if (m_VK.SetDebugUtilsObjectNameEXT == nullptr) - return; - - VkDebugUtilsObjectNameInfoEXT info = { - VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT, - nullptr, - objectType, - (uint64_t)handle, - name - }; - - const VkResult result = m_VK.SetDebugUtilsObjectNameEXT(m_Device, &info); - - RETURN_ON_FAILURE(GetLog(), result == VK_SUCCESS, ReturnVoid(), - "Can't set a debug name to an object: vkSetDebugUtilsObjectNameEXT returned %d.", (int32_t)result); + return CreateImplementation(memory, physicalDeviceMask, memoryType, size); } -void DeviceVK::SetDebugNameToDeviceGroupObject(VkObjectType objectType, const uint64_t* handles, const char* name) +inline Result DeviceVK::BindBufferMemory(const BufferMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum) { - if (m_VK.SetDebugUtilsObjectNameEXT == nullptr) - return; + if (memoryBindingDescNum == 0) + return Result::SUCCESS; - const size_t nameLength = strlen(name); - constexpr size_t deviceIndexSuffixLength = 16; // " (PD%u)" + const uint32_t infoMaxNum = memoryBindingDescNum * m_DeviceDesc.physicalDeviceNum; - char* nameWithDeviceIndex = STACK_ALLOC(char, nameLength + deviceIndexSuffixLength); - memcpy(nameWithDeviceIndex, name, nameLength); + VkBindBufferMemoryInfo* infos = STACK_ALLOC(VkBindBufferMemoryInfo, infoMaxNum); + uint32_t infoNum = 0; - VkDebugUtilsObjectNameInfoEXT info = { - VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT, - nullptr, - objectType, - (uint64_t)0, - nameWithDeviceIndex - }; + VkBindBufferMemoryDeviceGroupInfo* deviceGroupInfos = nullptr; + if (m_DeviceDesc.physicalDeviceNum > 1) + deviceGroupInfos = STACK_ALLOC(VkBindBufferMemoryDeviceGroupInfo, infoMaxNum); - for (uint32_t i = 0; i < m_DeviceDesc.physicalDeviceNum; i++) + for (uint32_t i = 0; i < memoryBindingDescNum; i++) { - if (handles[i] != 0) - { - info.objectHandle = (uint64_t)handles[i]; - snprintf(nameWithDeviceIndex + nameLength, deviceIndexSuffixLength, " (PD%u)", i); - - const VkResult result = m_VK.SetDebugUtilsObjectNameEXT(m_Device, &info); - RETURN_ON_FAILURE(GetLog(), result == VK_SUCCESS, ReturnVoid(), - "Can't set a debug name to an object: vkSetDebugUtilsObjectNameEXT returned %d.", (int32_t)result); - } - } -} + const BufferMemoryBindingDesc& bindingDesc = memoryBindingDescs[i]; -void DeviceVK::ReportDeviceGroupInfo() -{ - REPORT_INFO(GetLog(), "Available device memory heaps:"); + MemoryVK& memoryImpl = *(MemoryVK*)bindingDesc.memory; + BufferVK& bufferImpl = *(BufferVK*)bindingDesc.buffer; - for (uint32_t i = 0; i < m_MemoryProps.memoryHeapCount; i++) - { - String text(GetStdAllocator()); - if (m_MemoryProps.memoryHeaps[i].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) - text += "DEVICE_LOCAL_BIT "; - if (m_MemoryProps.memoryHeaps[i].flags & VK_MEMORY_HEAP_MULTI_INSTANCE_BIT) - text += "MULTI_INSTANCE_BIT "; + const MemoryTypeUnpack unpack = { memoryImpl.GetType() }; + const MemoryTypeInfo& memoryTypeInfo = unpack.info; - const double size = double(m_MemoryProps.memoryHeaps[i].size) / (1024.0 * 1024.0); + const MemoryLocation memoryLocation = (MemoryLocation)memoryTypeInfo.location; - REPORT_INFO(GetLog(), "\tHeap%u %.1lfMiB - %s", i, size, text.c_str()); + uint32_t physicalDeviceMask = GetPhysicalDeviceGroupMask(bindingDesc.physicalDeviceMask); + if (IsHostVisibleMemory(memoryLocation)) + physicalDeviceMask = 0x1; - if (m_DeviceDesc.physicalDeviceNum == 1) - continue; + if (memoryTypeInfo.isDedicated == 1) + memoryImpl.CreateDedicated(bufferImpl, physicalDeviceMask); for (uint32_t j = 0; j < m_DeviceDesc.physicalDeviceNum; j++) { - REPORT_INFO(GetLog(), "\t\tPhysicalDevice%u", j); - - for (uint32_t k = 0; k < m_DeviceDesc.physicalDeviceNum; k++) + if ((1u << j) & physicalDeviceMask) { - if (j == k) - continue; + VkBindBufferMemoryInfo& info = infos[infoNum++]; - VkPeerMemoryFeatureFlags flags = 0; - m_VK.GetDeviceGroupPeerMemoryFeatures(m_Device, i, j, k, &flags); + info = {}; + info.sType = VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO; + info.buffer = bufferImpl.GetHandle(j); + info.memory = memoryImpl.GetHandle(j); + info.memoryOffset = bindingDesc.offset; - text.clear(); - if (flags & VK_PEER_MEMORY_FEATURE_COPY_SRC_BIT) - text += "COPY_SRC_BIT "; - if (flags & VK_PEER_MEMORY_FEATURE_COPY_DST_BIT) - text += "COPY_DST_BIT "; - if (flags & VK_PEER_MEMORY_FEATURE_GENERIC_SRC_BIT) - text += "GENERIC_SRC_BIT "; - if (flags & VK_PEER_MEMORY_FEATURE_GENERIC_DST_BIT) - text += "GENERIC_DST_BIT "; + if (IsHostVisibleMemory(memoryLocation)) + bufferImpl.SetHostMemory(memoryImpl, info.memoryOffset); - REPORT_INFO(GetLog(), "\t\t\tPhysicalDevice%u - %s", k, text.c_str()); + if (deviceGroupInfos != nullptr) + { + VkBindBufferMemoryDeviceGroupInfo& deviceGroupInfo = deviceGroupInfos[infoNum - 1]; + deviceGroupInfo = {}; + deviceGroupInfo.sType = VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_DEVICE_GROUP_INFO; + deviceGroupInfo.deviceIndexCount = m_DeviceDesc.physicalDeviceNum; + deviceGroupInfo.pDeviceIndices = &m_PhysicalDeviceIndices[j * m_DeviceDesc.physicalDeviceNum]; + info.pNext = &deviceGroupInfo; + } } } } -} - -#define RESOLVE_OPTIONAL_DEVICE_FUNCTION( name ) \ - m_VK.name = (PFN_vk ## name)m_VK.GetDeviceProcAddr(m_Device, "vk" #name) - -#define RESOLVE_DEVICE_FUNCTION( name ) \ - RESOLVE_OPTIONAL_DEVICE_FUNCTION(name); \ - if (m_VK.name == nullptr) \ - { \ - REPORT_ERROR(GetLog(), "Failed to get device function: '%s'.", #name); \ - return Result::UNSUPPORTED; \ - } - -#define RESOLVE_DEVICE_FUNCTION_WITH_OTHER_NAME( functionName, otherName ) \ - m_VK.functionName = (PFN_vk ## functionName)m_VK.GetDeviceProcAddr(m_Device, otherName); \ - if (m_VK.functionName == nullptr) \ - { \ - REPORT_ERROR(GetLog(), "Failed to get device function: '" otherName "'."); \ - return Result::UNSUPPORTED; \ - } - -#define RESOLVE_INSTANCE_FUNCTION( name ) \ - m_VK.name = (PFN_vk ## name)m_VK.GetInstanceProcAddr(m_Instance, "vk" #name); \ - if (m_VK.name == nullptr) \ - { \ - REPORT_ERROR(GetLog(), "Failed to get instance function: '%s'.", #name); \ - return Result::UNSUPPORTED; \ - } - -#define RESOLVE_PRE_INSTANCE_FUNCTION( name ) \ - m_VK.name = (PFN_vk ## name)m_VK.GetInstanceProcAddr(VK_NULL_HANDLE, "vk" #name); \ - if (m_VK.name == nullptr) \ - { \ - REPORT_ERROR(GetLog(), "Failed to get instance function: '%s'.", #name); \ - return Result::UNSUPPORTED; \ - } - -Result DeviceVK::ResolvePreInstanceDispatchTable() -{ - m_VK = {}; - - m_VK.GetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)GetSharedLibraryFunction(*m_Loader, "vkGetInstanceProcAddr"); - if (m_VK.GetInstanceProcAddr == nullptr) - { - REPORT_ERROR(GetLog(), "Failed to get vkGetInstanceProcAddr."); - return Result::UNSUPPORTED; - } - - RESOLVE_PRE_INSTANCE_FUNCTION(CreateInstance); - RESOLVE_PRE_INSTANCE_FUNCTION(EnumerateInstanceExtensionProperties); - RESOLVE_PRE_INSTANCE_FUNCTION(EnumerateInstanceLayerProperties); - - return Result::SUCCESS; -} - -Result DeviceVK::ResolveInstanceDispatchTable() -{ - RESOLVE_INSTANCE_FUNCTION(GetPhysicalDeviceSurfaceFormatsKHR); - RESOLVE_INSTANCE_FUNCTION(GetPhysicalDeviceSurfaceSupportKHR); - RESOLVE_INSTANCE_FUNCTION(GetPhysicalDeviceSurfaceCapabilitiesKHR); - RESOLVE_INSTANCE_FUNCTION(GetPhysicalDeviceSurfacePresentModesKHR); -#if VK_USE_PLATFORM_WIN32_KHR - RESOLVE_INSTANCE_FUNCTION(CreateWin32SurfaceKHR); -#endif -#if VK_USE_PLATFORM_METAL_EXT - RESOLVE_INSTANCE_FUNCTION(CreateMetalSurfaceEXT); -#endif -#if VK_USE_PLATFORM_XLIB_KHR - RESOLVE_INSTANCE_FUNCTION(CreateXlibSurfaceKHR); -#endif -#if VK_USE_PLATFORM_WAYLAND_KHR - RESOLVE_INSTANCE_FUNCTION(CreateWaylandSurfaceKHR); -#endif - RESOLVE_INSTANCE_FUNCTION(DestroySurfaceKHR); - RESOLVE_INSTANCE_FUNCTION(GetDeviceProcAddr); - RESOLVE_INSTANCE_FUNCTION(DestroyInstance); - RESOLVE_INSTANCE_FUNCTION(DestroyDevice); - RESOLVE_INSTANCE_FUNCTION(GetPhysicalDeviceMemoryProperties); - RESOLVE_INSTANCE_FUNCTION(GetDeviceGroupPeerMemoryFeatures); - RESOLVE_INSTANCE_FUNCTION(CreateDevice); - RESOLVE_INSTANCE_FUNCTION(GetDeviceQueue); - RESOLVE_INSTANCE_FUNCTION(EnumeratePhysicalDeviceGroups); - RESOLVE_INSTANCE_FUNCTION(GetPhysicalDeviceProperties); - RESOLVE_INSTANCE_FUNCTION(GetPhysicalDeviceProperties2); - RESOLVE_INSTANCE_FUNCTION(GetPhysicalDeviceFeatures); - RESOLVE_INSTANCE_FUNCTION(GetPhysicalDeviceFeatures2); - RESOLVE_INSTANCE_FUNCTION(GetPhysicalDeviceQueueFamilyProperties); - RESOLVE_INSTANCE_FUNCTION(EnumerateDeviceExtensionProperties); - - return Result::SUCCESS; -} - -Result DeviceVK::ResolveDispatchTable() -{ - RESOLVE_DEVICE_FUNCTION(CreateBuffer); - RESOLVE_DEVICE_FUNCTION(CreateImage); - RESOLVE_DEVICE_FUNCTION(CreateBufferView); - RESOLVE_DEVICE_FUNCTION(CreateImageView); - RESOLVE_DEVICE_FUNCTION(CreateSampler); - RESOLVE_DEVICE_FUNCTION(CreateRenderPass); - RESOLVE_DEVICE_FUNCTION(CreateFramebuffer); - RESOLVE_DEVICE_FUNCTION(CreateQueryPool); - RESOLVE_DEVICE_FUNCTION(CreateCommandPool); - RESOLVE_DEVICE_FUNCTION(CreateFence); - RESOLVE_DEVICE_FUNCTION(CreateSemaphore); - RESOLVE_DEVICE_FUNCTION(CreateDescriptorPool); - RESOLVE_DEVICE_FUNCTION(CreatePipelineLayout); - RESOLVE_DEVICE_FUNCTION(CreateDescriptorSetLayout); - RESOLVE_DEVICE_FUNCTION(CreateShaderModule); - RESOLVE_DEVICE_FUNCTION(CreateGraphicsPipelines); - RESOLVE_DEVICE_FUNCTION(CreateComputePipelines); - RESOLVE_DEVICE_FUNCTION(CreateSwapchainKHR); - RESOLVE_DEVICE_FUNCTION(DestroyBuffer); - RESOLVE_DEVICE_FUNCTION(DestroyImage); - RESOLVE_DEVICE_FUNCTION(DestroyBufferView); - RESOLVE_DEVICE_FUNCTION(DestroyImageView); - RESOLVE_DEVICE_FUNCTION(DestroySampler); - RESOLVE_DEVICE_FUNCTION(DestroyRenderPass); - RESOLVE_DEVICE_FUNCTION(DestroyFramebuffer); - RESOLVE_DEVICE_FUNCTION(DestroyQueryPool); - RESOLVE_DEVICE_FUNCTION(DestroyCommandPool); - RESOLVE_DEVICE_FUNCTION(DestroyFence); - RESOLVE_DEVICE_FUNCTION(DestroySemaphore); - RESOLVE_DEVICE_FUNCTION(DestroyDescriptorPool); - RESOLVE_DEVICE_FUNCTION(DestroyPipelineLayout); - RESOLVE_DEVICE_FUNCTION(DestroyDescriptorSetLayout); - RESOLVE_DEVICE_FUNCTION(DestroyShaderModule); - RESOLVE_DEVICE_FUNCTION(DestroyPipeline); - RESOLVE_DEVICE_FUNCTION(DestroySwapchainKHR); + VkResult result = VK_SUCCESS; + if (infoNum > 0) + result = m_VK.BindBufferMemory2(m_Device, infoNum, infos); - RESOLVE_DEVICE_FUNCTION(AllocateMemory); - RESOLVE_DEVICE_FUNCTION(MapMemory); - RESOLVE_DEVICE_FUNCTION(UnmapMemory); - RESOLVE_DEVICE_FUNCTION(FreeMemory); + RETURN_ON_FAILURE(GetLog(), result == VK_SUCCESS, GetReturnCode(result), + "Can't bind a memory to a buffer: vkBindBufferMemory2 returned %d.", (int32_t)result); - RESOLVE_OPTIONAL_DEVICE_FUNCTION(BindBufferMemory2); - if (m_VK.BindBufferMemory2 == nullptr) - RESOLVE_DEVICE_FUNCTION_WITH_OTHER_NAME(BindBufferMemory2, "vkBindBufferMemory2KHR"); + for (uint32_t i = 0; i < memoryBindingDescNum; i++) + { + BufferVK& bufferImpl = *(BufferVK*)memoryBindingDescs[i].buffer; + bufferImpl.ReadDeviceAddress(); + } - RESOLVE_OPTIONAL_DEVICE_FUNCTION(BindImageMemory2); - if (m_VK.BindImageMemory2 == nullptr) - RESOLVE_DEVICE_FUNCTION_WITH_OTHER_NAME(BindImageMemory2, "vkBindImageMemory2KHR"); + return Result::SUCCESS; +} - RESOLVE_OPTIONAL_DEVICE_FUNCTION(GetBufferMemoryRequirements2); - if (m_VK.GetBufferMemoryRequirements2 == nullptr) - RESOLVE_DEVICE_FUNCTION_WITH_OTHER_NAME(GetBufferMemoryRequirements2, "vkGetBufferMemoryRequirements2KHR"); +inline Result DeviceVK::BindTextureMemory(const TextureMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum) +{ + const uint32_t infoMaxNum = memoryBindingDescNum * m_DeviceDesc.physicalDeviceNum; - RESOLVE_OPTIONAL_DEVICE_FUNCTION(GetImageMemoryRequirements2); - if (m_VK.GetImageMemoryRequirements2 == nullptr) - RESOLVE_DEVICE_FUNCTION_WITH_OTHER_NAME(GetImageMemoryRequirements2, "vkGetImageMemoryRequirements2KHR"); + VkBindImageMemoryInfo* infos = STACK_ALLOC(VkBindImageMemoryInfo, infoMaxNum); + uint32_t infoNum = 0; - RESOLVE_DEVICE_FUNCTION(QueueWaitIdle); - RESOLVE_DEVICE_FUNCTION(WaitForFences); - RESOLVE_DEVICE_FUNCTION(ResetFences); - RESOLVE_DEVICE_FUNCTION(AcquireNextImageKHR); - RESOLVE_DEVICE_FUNCTION(QueueSubmit); - RESOLVE_DEVICE_FUNCTION(QueuePresentKHR); + VkBindImageMemoryDeviceGroupInfo* deviceGroupInfos = nullptr; + if (m_DeviceDesc.physicalDeviceNum > 1) + deviceGroupInfos = STACK_ALLOC(VkBindImageMemoryDeviceGroupInfo, infoMaxNum); - RESOLVE_DEVICE_FUNCTION(ResetCommandPool); - RESOLVE_DEVICE_FUNCTION(ResetDescriptorPool); - RESOLVE_DEVICE_FUNCTION(AllocateCommandBuffers); - RESOLVE_DEVICE_FUNCTION(AllocateDescriptorSets); - RESOLVE_DEVICE_FUNCTION(FreeCommandBuffers); - RESOLVE_DEVICE_FUNCTION(FreeDescriptorSets); + for (uint32_t i = 0; i < memoryBindingDescNum; i++) + { + const TextureMemoryBindingDesc& bindingDesc = memoryBindingDescs[i]; - RESOLVE_DEVICE_FUNCTION(UpdateDescriptorSets); + const uint32_t physicalDeviceMask = GetPhysicalDeviceGroupMask(bindingDesc.physicalDeviceMask); - RESOLVE_DEVICE_FUNCTION(BeginCommandBuffer); - RESOLVE_DEVICE_FUNCTION(CmdSetDepthBounds); - RESOLVE_DEVICE_FUNCTION(CmdSetViewport); - RESOLVE_DEVICE_FUNCTION(CmdSetScissor); - RESOLVE_DEVICE_FUNCTION(CmdSetStencilReference); - RESOLVE_DEVICE_FUNCTION(CmdClearAttachments); - RESOLVE_DEVICE_FUNCTION(CmdClearColorImage); - RESOLVE_DEVICE_FUNCTION(CmdBeginRenderPass); - RESOLVE_DEVICE_FUNCTION(CmdBindVertexBuffers); - RESOLVE_DEVICE_FUNCTION(CmdBindIndexBuffer); - RESOLVE_DEVICE_FUNCTION(CmdBindPipeline); - RESOLVE_DEVICE_FUNCTION(CmdBindDescriptorSets); - RESOLVE_DEVICE_FUNCTION(CmdPushConstants); - RESOLVE_DEVICE_FUNCTION(CmdDispatch); - RESOLVE_DEVICE_FUNCTION(CmdDispatchIndirect); - RESOLVE_DEVICE_FUNCTION(CmdDraw); - RESOLVE_DEVICE_FUNCTION(CmdDrawIndexed); - RESOLVE_DEVICE_FUNCTION(CmdDrawIndirect); - RESOLVE_DEVICE_FUNCTION(CmdDrawIndexedIndirect); - RESOLVE_DEVICE_FUNCTION(CmdCopyBuffer); - RESOLVE_DEVICE_FUNCTION(CmdCopyImage); - RESOLVE_DEVICE_FUNCTION(CmdCopyBufferToImage); - RESOLVE_DEVICE_FUNCTION(CmdCopyImageToBuffer); - RESOLVE_DEVICE_FUNCTION(CmdPipelineBarrier); - RESOLVE_DEVICE_FUNCTION(CmdBeginQuery); - RESOLVE_DEVICE_FUNCTION(CmdEndQuery); - RESOLVE_DEVICE_FUNCTION(CmdWriteTimestamp); - RESOLVE_DEVICE_FUNCTION(CmdCopyQueryPoolResults); - RESOLVE_DEVICE_FUNCTION(CmdResetQueryPool); - RESOLVE_DEVICE_FUNCTION(CmdEndRenderPass); - RESOLVE_DEVICE_FUNCTION(CmdFillBuffer); - RESOLVE_DEVICE_FUNCTION(EndCommandBuffer); + MemoryVK& memoryImpl = *(MemoryVK*)bindingDesc.memory; + TextureVK& textureImpl = *(TextureVK*)bindingDesc.texture; - RESOLVE_DEVICE_FUNCTION(GetSwapchainImagesKHR); + const MemoryTypeUnpack unpack = { memoryImpl.GetType() }; + const MemoryTypeInfo& memoryTypeInfo = unpack.info; - if (m_IsDebugUtilsSupported) - { - RESOLVE_DEVICE_FUNCTION(SetDebugUtilsObjectNameEXT); - RESOLVE_DEVICE_FUNCTION(CmdBeginDebugUtilsLabelEXT); - RESOLVE_DEVICE_FUNCTION(CmdEndDebugUtilsLabelEXT); - } + if (memoryTypeInfo.isDedicated == 1) + memoryImpl.CreateDedicated(textureImpl, physicalDeviceMask); - if (m_IsRayTracingExtSupported) - { - RESOLVE_DEVICE_FUNCTION(CreateAccelerationStructureKHR); - RESOLVE_DEVICE_FUNCTION(CreateRayTracingPipelinesKHR); - RESOLVE_DEVICE_FUNCTION(DestroyAccelerationStructureKHR); - RESOLVE_DEVICE_FUNCTION(GetAccelerationStructureDeviceAddressKHR); - RESOLVE_DEVICE_FUNCTION(GetAccelerationStructureBuildSizesKHR); - RESOLVE_DEVICE_FUNCTION(GetRayTracingShaderGroupHandlesKHR); - RESOLVE_DEVICE_FUNCTION(CmdBuildAccelerationStructuresKHR); - RESOLVE_DEVICE_FUNCTION(CmdCopyAccelerationStructureKHR); - RESOLVE_DEVICE_FUNCTION(CmdWriteAccelerationStructuresPropertiesKHR); - RESOLVE_DEVICE_FUNCTION(CmdTraceRaysKHR); - RESOLVE_DEVICE_FUNCTION(GetBufferDeviceAddress); + for (uint32_t j = 0; j < m_DeviceDesc.physicalDeviceNum; j++) + { + if ((1u << j) & physicalDeviceMask) + { + VkBindImageMemoryInfo& info = infos[infoNum++]; + info.sType = VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_INFO; + info.pNext = nullptr; + info.image = textureImpl.GetHandle(j); + info.memory = memoryImpl.GetHandle(j); + info.memoryOffset = bindingDesc.offset; + + if (deviceGroupInfos != nullptr) + { + VkBindImageMemoryDeviceGroupInfo& deviceGroupInfo = deviceGroupInfos[infoNum - 1]; + deviceGroupInfo = {}; + deviceGroupInfo.sType = VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_DEVICE_GROUP_INFO; + deviceGroupInfo.deviceIndexCount = m_DeviceDesc.physicalDeviceNum; + deviceGroupInfo.pDeviceIndices = &m_PhysicalDeviceIndices[j * m_DeviceDesc.physicalDeviceNum]; + info.pNext = &deviceGroupInfo; + } + } + } } - if (m_IsMeshShaderExtSupported) + VkResult result = VK_SUCCESS; + if (infoNum > 0) + result = m_VK.BindImageMemory2(m_Device, infoNum, infos); + + RETURN_ON_FAILURE(GetLog(), result == VK_SUCCESS, GetReturnCode(result), + "Can't bind a memory to a texture: vkBindImageMemory2 returned %d.", (int32_t)result); + + return Result::SUCCESS; +} + +inline Result DeviceVK::BindAccelerationStructureMemory(const AccelerationStructureMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum) +{ + if (memoryBindingDescNum == 0) + return Result::SUCCESS; + + BufferMemoryBindingDesc* infos = ALLOCATE_SCRATCH(*this, BufferMemoryBindingDesc, memoryBindingDescNum); + + for (uint32_t i = 0; i < memoryBindingDescNum; i++) { - RESOLVE_DEVICE_FUNCTION(CmdDrawMeshTasksNV); + const AccelerationStructureMemoryBindingDesc& bindingDesc = memoryBindingDescs[i]; + AccelerationStructureVK& accelerationStructure = *(AccelerationStructureVK*)bindingDesc.accelerationStructure; + + BufferMemoryBindingDesc& bufferMemoryBinding = infos[i]; + bufferMemoryBinding = {}; + bufferMemoryBinding.buffer = (Buffer*)accelerationStructure.GetBuffer(); + bufferMemoryBinding.memory = bindingDesc.memory; + bufferMemoryBinding.offset = bindingDesc.offset; + bufferMemoryBinding.physicalDeviceMask = bindingDesc.physicalDeviceMask; } - RESOLVE_INSTANCE_FUNCTION(GetPhysicalDeviceFormatProperties); + Result result = BindBufferMemory(infos, memoryBindingDescNum); - if (m_IsHDRExtSupported) + for (uint32_t i = 0; i < memoryBindingDescNum && result == Result::SUCCESS; i++) { - RESOLVE_OPTIONAL_DEVICE_FUNCTION(SetHdrMetadataEXT); - m_IsHDRExtSupported = m_VK.SetHdrMetadataEXT != nullptr; + AccelerationStructureVK& accelerationStructure = *(AccelerationStructureVK*)memoryBindingDescs[i].accelerationStructure; + result = accelerationStructure.FinishCreation(); } - return Result::SUCCESS; + FREE_SCRATCH(*this, infos, memoryBindingDescNum); + + return result; } -void DeviceVK::Destroy() +inline void DeviceVK::FreeMemory(Memory& memory) { - Deallocate(GetStdAllocator(), this); + Deallocate(GetStdAllocator(), (MemoryVK*)&memory); } -Result CreateDeviceVK(const DeviceCreationDesc& deviceCreationDesc, DeviceBase*& device) +inline FormatSupportBits DeviceVK::GetFormatSupport(Format format) const { - Log log(GraphicsAPI::VULKAN, deviceCreationDesc.callbackInterface); - StdAllocator allocator(deviceCreationDesc.memoryAllocatorInterface); + const VkFormat vulkanFormat = GetVkFormat(format); + const VkPhysicalDevice physicalDevice = m_PhysicalDevices.front(); - DeviceVK* implementation = Allocate(allocator, log, allocator); + VkFormatProperties formatProperties = {}; + m_VK.GetPhysicalDeviceFormatProperties(physicalDevice, vulkanFormat, &formatProperties); - const Result res = implementation->Create(deviceCreationDesc); + constexpr uint32_t transferBits = VK_FORMAT_FEATURE_TRANSFER_DST_BIT | VK_FORMAT_FEATURE_TRANSFER_SRC_BIT; - if (res == Result::SUCCESS) - { - device = implementation; - return Result::SUCCESS; - } + constexpr uint32_t textureBits = VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | transferBits; + constexpr uint32_t storageTextureBits = VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT | transferBits; + constexpr uint32_t bufferBits = VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT | transferBits; + constexpr uint32_t storageBufferBits = VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT | transferBits; + constexpr uint32_t colorAttachmentBits = VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT | VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT | transferBits; + constexpr uint32_t depthAttachmentBits = VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT | transferBits; + constexpr uint32_t vertexBufferBits = VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT | transferBits; - Deallocate(allocator, implementation); - return res; + FormatSupportBits mask = FormatSupportBits::UNSUPPORTED; + + if (formatProperties.optimalTilingFeatures & textureBits) + mask |= FormatSupportBits::TEXTURE; + + if (formatProperties.optimalTilingFeatures & storageTextureBits) + mask |= FormatSupportBits::STORAGE_TEXTURE; + + if (formatProperties.optimalTilingFeatures & colorAttachmentBits) + mask |= FormatSupportBits::COLOR_ATTACHMENT; + + if (formatProperties.optimalTilingFeatures & depthAttachmentBits) + mask |= FormatSupportBits::DEPTH_STENCIL_ATTACHMENT; + + if (formatProperties.bufferFeatures & bufferBits) + mask |= FormatSupportBits::BUFFER; + + if (formatProperties.bufferFeatures & storageBufferBits) + mask |= FormatSupportBits::STORAGE_BUFFER; + + if (formatProperties.bufferFeatures & vertexBufferBits) + mask |= FormatSupportBits::VERTEX_BUFFER; + + return mask; } -Result CreateDeviceVK(const DeviceCreationVulkanDesc& deviceCreationDesc, DeviceBase*& device) +inline uint32_t DeviceVK::CalculateAllocationNumber(const ResourceGroupDesc& resourceGroupDesc) const { - Log log(GraphicsAPI::VULKAN, deviceCreationDesc.callbackInterface); - StdAllocator allocator(deviceCreationDesc.memoryAllocatorInterface); + HelperDeviceMemoryAllocator allocator(m_CoreInterface, (Device&)*this, m_StdAllocator); - DeviceVK* implementation = Allocate(allocator, log, allocator); - const Result res = implementation->Create(deviceCreationDesc); + return allocator.CalculateAllocationNumber(resourceGroupDesc); +} - if (res == Result::SUCCESS) - { - device = implementation; - return Result::SUCCESS; - } +inline Result DeviceVK::AllocateAndBindMemory(const ResourceGroupDesc& resourceGroupDesc, nri::Memory** allocations) +{ + HelperDeviceMemoryAllocator allocator(m_CoreInterface, (Device&)*this, m_StdAllocator); - Deallocate(allocator, implementation); - return res; + return allocator.AllocateAndBindMemory(resourceGroupDesc, allocations); } #include "DeviceVK.hpp" diff --git a/Source/VK/DeviceVK.h b/Source/VK/DeviceVK.h index fa833709..58d94999 100644 --- a/Source/VK/DeviceVK.h +++ b/Source/VK/DeviceVK.h @@ -12,257 +12,186 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct CommandQueueVK; - struct DeviceVK final : public DeviceBase - { - DeviceVK(const Log& log, const StdAllocator& stdAllocator); - ~DeviceVK(); +struct CommandQueueVK; - operator VkDevice() const; - operator VkPhysicalDevice() const; - operator VkInstance() const; - - const DispatchTable& GetDispatchTable() const; - const VkAllocationCallbacks* GetAllocationCallbacks() const; - const std::array& GetQueueFamilyIndices() const; - const SPIRVBindingOffsets& GetSPIRVBindingOffsets() const; - const CoreInterface& GetCoreInterface() const; - - Result Create(const DeviceCreationVulkanDesc& deviceCreationVulkanDesc); - Result Create(const DeviceCreationDesc& deviceCreationDesc); - bool GetMemoryType(MemoryLocation memoryLocation, uint32_t memoryTypeMask, MemoryTypeInfo& memoryTypeInfo) const; - bool GetMemoryType(uint32_t index, MemoryTypeInfo& memoryTypeInfo) const; - - uint32_t GetPhysicalDeviceGroupSize() const; - bool IsDescriptorIndexingExtSupported() const; - bool IsConcurrentSharingModeEnabledForBuffers() const; - bool IsConcurrentSharingModeEnabledForImages() const; - bool IsBufferDeviceAddressSupported() const; - const Vector& GetConcurrentSharingModeQueueIndices() const; - - void SetDebugNameToTrivialObject(VkObjectType objectType, uint64_t handle, const char* name); - void SetDebugNameToDeviceGroupObject(VkObjectType objectType, const uint64_t* handles, const char* name); - - //================================================================================================================ - // NRI - //================================================================================================================ - void SetDebugName(const char* name); - - inline const DeviceDesc& GetDesc() const - { return m_DeviceDesc; } - - Result GetCommandQueue(CommandQueueType commandQueueType, CommandQueue*& commandQueue); - - Result CreateCommandAllocator(const CommandQueue& commandQueue, uint32_t physicalDeviceMask, CommandAllocator*& commandAllocator); - Result CreateDescriptorPool(const DescriptorPoolDesc& descriptorPoolDesc, DescriptorPool*& descriptorPool); - Result CreateBuffer(const BufferDesc& bufferDesc, Buffer*& buffer); - Result CreateTexture(const TextureDesc& textureDesc, Texture*& texture); - Result CreateBufferView(const BufferViewDesc& bufferViewDesc, Descriptor*& bufferView); - Result CreateTexture1DView(const Texture1DViewDesc& textureViewDesc, Descriptor*& textureView); - Result CreateTexture2DView(const Texture2DViewDesc& textureViewDesc, Descriptor*& textureView); - Result CreateTexture3DView(const Texture3DViewDesc& textureViewDesc, Descriptor*& textureView); - Result CreateSampler(const SamplerDesc& samplerDesc, Descriptor*& sampler); - Result CreatePipelineLayout(const PipelineLayoutDesc& pipelineLayoutDesc, PipelineLayout*& pipelineLayout); - Result CreatePipeline(const GraphicsPipelineDesc& graphicsPipelineDesc, Pipeline*& pipeline); - Result CreatePipeline(const ComputePipelineDesc& computePipelineDesc, Pipeline*& pipeline); - Result CreateFrameBuffer(const FrameBufferDesc& frameBufferDesc, FrameBuffer*& frameBuffer); - Result CreateQueryPool(const QueryPoolDesc& queryPoolDesc, QueryPool*& queryPool); - Result CreateQueueSemaphore(QueueSemaphore*& queueSemaphore); - Result CreateDeviceSemaphore(bool signaled, DeviceSemaphore*& deviceSemaphore); - Result CreateSwapChain(const SwapChainDesc& swapChainDesc, SwapChain*& swapChain); - Result CreatePipeline(const RayTracingPipelineDesc& rayTracingPipelineDesc, Pipeline*& pipeline); - Result CreateAccelerationStructure(const AccelerationStructureDesc& accelerationStructureDesc, AccelerationStructure*& accelerationStructure); - - Result CreateCommandQueue(const CommandQueueVulkanDesc& commandQueueDesc, CommandQueue*& commandQueue); - Result CreateCommandAllocator(const CommandAllocatorVulkanDesc& commandAllocatorDesc, CommandAllocator*& commandAllocator); - Result CreateCommandBuffer(const CommandBufferVulkanDesc& commandBufferDesc, CommandBuffer*& commandBuffer); - Result CreateDescriptorPool(NRIVkDescriptorPool vkDescriptorPool, DescriptorPool*& descriptorPool); - Result CreateBuffer(const BufferVulkanDesc& bufferDesc, Buffer*& buffer); - Result CreateTexture(const TextureVulkanDesc& textureVulkanDesc, Texture*& texture); - Result CreateMemory(const MemoryVulkanDesc& memoryVulkanDesc, Memory*& memory); - Result CreateGraphicsPipeline(NRIVkPipeline vkPipeline, Pipeline*& pipeline); - Result CreateComputePipeline(NRIVkPipeline vkPipeline, Pipeline*& pipeline); - Result CreateQueryPool(const QueryPoolVulkanDesc& queryPoolVulkanDesc, QueryPool*& queryPool); - Result CreateQueueSemaphore(NRIVkSemaphore vkSemaphore, QueueSemaphore*& queueSemaphore); - Result CreateDeviceSemaphore(NRIVkFence vkFence, DeviceSemaphore*& deviceSemaphore); - Result CreateAccelerationStructure(const AccelerationStructureVulkanDesc& accelerationStructureDesc, AccelerationStructure*& accelerationStructure); - - void DestroyCommandAllocator(CommandAllocator& commandAllocator); - void DestroyDescriptorPool(DescriptorPool& descriptorPool); - void DestroyBuffer(Buffer& buffer); - void DestroyTexture(Texture& texture); - void DestroyDescriptor(Descriptor& descriptor); - void DestroyPipelineLayout(PipelineLayout& pipelineLayout); - void DestroyPipeline(Pipeline& pipeline); - void DestroyFrameBuffer(FrameBuffer& frameBuffer); - void DestroyQueryPool(QueryPool& queryPool); - void DestroyQueueSemaphore(QueueSemaphore& queueSemaphore); - void DestroyDeviceSemaphore(DeviceSemaphore& deviceSemaphore); - void DestroySwapChain(SwapChain& swapChain); - void DestroyAccelerationStructure(AccelerationStructure& accelerationStructure); - - Result GetDisplays(Display** displays, uint32_t& displayNum); - Result GetDisplaySize(Display& display, uint16_t& width, uint16_t& height); - - Result AllocateMemory(uint32_t physicalDeviceMask, MemoryType memoryType, uint64_t size, Memory*& memory); - Result BindBufferMemory(const BufferMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum); - Result BindTextureMemory(const TextureMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum); - Result BindAccelerationStructureMemory(const AccelerationStructureMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum); - void FreeMemory(Memory& memory); - - FormatSupportBits GetFormatSupport(Format format) const; - - uint32_t CalculateAllocationNumber(const ResourceGroupDesc& resourceGroupDesc) const; - Result AllocateAndBindMemory(const ResourceGroupDesc& resourceGroupDesc, Memory** allocations); - - //================================================================================================================ - // DeviceBase - //================================================================================================================ - void Destroy(); - Result FillFunctionTable(CoreInterface& table) const; - Result FillFunctionTable(SwapChainInterface& table) const; - Result FillFunctionTable(WrapperVKInterface& wrapperVKInterface) const; - Result FillFunctionTable(RayTracingInterface& rayTracingInterface) const; - Result FillFunctionTable(MeshShaderInterface& meshShaderInterface) const; - Result FillFunctionTable(HelperInterface& helperInterface) const; - - private: - Result CreateInstance(const DeviceCreationDesc& deviceCreationDesc); - Result FindPhysicalDeviceGroup(const PhysicalDeviceGroup* physicalDeviceGroup, bool enableMGPU); - Result CreateLogicalDevice(const DeviceCreationDesc& deviceCreationDesc); - void FillFamilyIndices(bool useEnabledFamilyIndices, const uint32_t* enabledFamilyIndices, uint32_t familyIndexNum); - void SetDeviceLimits(bool enableValidation); - void CreateCommandQueues(); - Result ResolvePreInstanceDispatchTable(); - Result ResolveInstanceDispatchTable(); - Result ResolveDispatchTable(); - void FilterInstanceLayers(Vector& layers); - bool FilterInstanceExtensions(Vector& extensions); - bool FilterDeviceExtensions(Vector& extensions); - void RetrieveRayTracingInfo(); - void RetrieveMeshShaderInfo(); - void ReportDeviceGroupInfo(); - void CheckSupportedDeviceExtensions(const Vector& extensions); - void CheckSupportedInstanceExtensions(const Vector& extensions); - void FindDXGIAdapter(); - - template< typename Implementation, typename Interface, typename ... Args > - Result CreateImplementation(Interface*& entity, const Args&... args); - - VkDevice m_Device = VK_NULL_HANDLE; - Vector m_PhysicalDevices; - VkInstance m_Instance = VK_NULL_HANDLE; - VkPhysicalDeviceMemoryProperties m_MemoryProps = {}; - DispatchTable m_VK = {}; - DeviceDesc m_DeviceDesc = {}; - VkPhysicalDeviceRayTracingPipelinePropertiesKHR m_RayTracingDeviceProperties = {}; - std::array m_FamilyIndices = {}; - std::array m_Queues = {}; - Vector m_PhysicalDeviceIndices; - Vector m_ConcurrentSharingModeQueueIndices; - VkAllocationCallbacks* m_AllocationCallbackPtr = nullptr; - VkAllocationCallbacks m_AllocationCallbacks = {}; - VkDebugUtilsMessengerEXT m_Messenger = VK_NULL_HANDLE; - SPIRVBindingOffsets m_SPIRVBindingOffsets = {}; - CoreInterface m_CoreInterface = {}; - Lock m_Lock; - uint64_t m_LUID = 0; - bool m_OwnsNativeObjects = false; - bool m_IsRayTracingExtSupported = false; - bool m_IsDescriptorIndexingExtSupported = false; - bool m_IsSampleLocationExtSupported = false; - bool m_IsMinMaxFilterExtSupported = false; - bool m_IsConservativeRasterExtSupported = false; - bool m_IsMeshShaderExtSupported = false; - bool m_IsHDRExtSupported = false; - bool m_IsDemoteToHelperInvocationSupported = false; - bool m_IsSubsetAllocationSupported = false; - bool m_IsConcurrentSharingModeEnabledForBuffers = true; - bool m_IsConcurrentSharingModeEnabledForImages = true; - bool m_IsDebugUtilsSupported = false; - bool m_IsFP16Supported = false; - bool m_IsBufferDeviceAddressSupported = false; - bool m_IsMicroMapSupported = false; - Library* m_Loader = nullptr; +struct DeviceVK final : public DeviceBase +{ + inline operator VkDevice() const + { return m_Device; } + + inline operator VkPhysicalDevice() const + { return m_PhysicalDevices.front(); } + + inline operator VkInstance() const + { return m_Instance; } + + inline const DispatchTable& GetDispatchTable() const + { return m_VK; } + + inline const VkAllocationCallbacks* GetAllocationCallbacks() const + { return m_AllocationCallbackPtr; } + + inline const std::array& GetQueueFamilyIndices() const + { return m_FamilyIndices; } + + inline const SPIRVBindingOffsets& GetSPIRVBindingOffsets() const + { return m_SPIRVBindingOffsets; } + + inline const CoreInterface& GetCoreInterface() const + { return m_CoreInterface; } + + inline uint32_t GetPhysicalDeviceGroupSize() const + { return m_DeviceDesc.physicalDeviceNum; } + + inline bool IsDescriptorIndexingExtSupported() const + { return m_IsDescriptorIndexingSupported; } + + inline bool IsConcurrentSharingModeEnabledForBuffers() const + { return m_ConcurrentSharingModeQueueIndices.size() > 1; } + + inline bool IsConcurrentSharingModeEnabledForImages() const + { return m_ConcurrentSharingModeQueueIndices.size() > 1; } + + inline bool IsBufferDeviceAddressSupported() const + { return m_IsBufferDeviceAddressSupported; } + + inline const Vector& GetConcurrentSharingModeQueueIndices() const + { return m_ConcurrentSharingModeQueueIndices; } + + DeviceVK(const Log& log, const StdAllocator& stdAllocator); + ~DeviceVK(); + + Result Create(const DeviceCreationVulkanDesc& deviceCreationVulkanDesc); + Result Create(const DeviceCreationDesc& deviceCreationDesc); + bool GetMemoryType(MemoryLocation memoryLocation, uint32_t memoryTypeMask, MemoryTypeInfo& memoryTypeInfo) const; + bool GetMemoryType(uint32_t index, MemoryTypeInfo& memoryTypeInfo) const; + void SetDebugNameToTrivialObject(VkObjectType objectType, uint64_t handle, const char* name); + void SetDebugNameToDeviceGroupObject(VkObjectType objectType, const uint64_t* handles, const char* name); + + //================================================================================================================ + // NRI + //================================================================================================================ + + inline const DeviceDesc& GetDesc() const + { return m_DeviceDesc; } + + void SetDebugName(const char* name); + Result GetCommandQueue(CommandQueueType commandQueueType, CommandQueue*& commandQueue); + Result CreateCommandAllocator(const CommandQueue& commandQueue, uint32_t physicalDeviceMask, CommandAllocator*& commandAllocator); + Result CreateDescriptorPool(const DescriptorPoolDesc& descriptorPoolDesc, DescriptorPool*& descriptorPool); + Result CreateBuffer(const BufferDesc& bufferDesc, Buffer*& buffer); + Result CreateTexture(const TextureDesc& textureDesc, Texture*& texture); + Result CreateBufferView(const BufferViewDesc& bufferViewDesc, Descriptor*& bufferView); + Result CreateTexture1DView(const Texture1DViewDesc& textureViewDesc, Descriptor*& textureView); + Result CreateTexture2DView(const Texture2DViewDesc& textureViewDesc, Descriptor*& textureView); + Result CreateTexture3DView(const Texture3DViewDesc& textureViewDesc, Descriptor*& textureView); + Result CreateSampler(const SamplerDesc& samplerDesc, Descriptor*& sampler); + Result CreatePipelineLayout(const PipelineLayoutDesc& pipelineLayoutDesc, PipelineLayout*& pipelineLayout); + Result CreatePipeline(const GraphicsPipelineDesc& graphicsPipelineDesc, Pipeline*& pipeline); + Result CreatePipeline(const ComputePipelineDesc& computePipelineDesc, Pipeline*& pipeline); + Result CreateFrameBuffer(const FrameBufferDesc& frameBufferDesc, FrameBuffer*& frameBuffer); + Result CreateQueryPool(const QueryPoolDesc& queryPoolDesc, QueryPool*& queryPool); + Result CreateFence(uint64_t initialValue, Fence*& fence); + Result CreateSwapChain(const SwapChainDesc& swapChainDesc, SwapChain*& swapChain); + Result CreatePipeline(const RayTracingPipelineDesc& rayTracingPipelineDesc, Pipeline*& pipeline); + Result CreateAccelerationStructure(const AccelerationStructureDesc& accelerationStructureDesc, AccelerationStructure*& accelerationStructure); + Result CreateCommandQueue(const CommandQueueVulkanDesc& commandQueueDesc, CommandQueue*& commandQueue); + Result CreateCommandAllocator(const CommandAllocatorVulkanDesc& commandAllocatorDesc, CommandAllocator*& commandAllocator); + Result CreateCommandBuffer(const CommandBufferVulkanDesc& commandBufferDesc, CommandBuffer*& commandBuffer); + Result CreateDescriptorPool(NRIVkDescriptorPool vkDescriptorPool, DescriptorPool*& descriptorPool); + Result CreateBuffer(const BufferVulkanDesc& bufferDesc, Buffer*& buffer); + Result CreateTexture(const TextureVulkanDesc& textureVulkanDesc, Texture*& texture); + Result CreateMemory(const MemoryVulkanDesc& memoryVulkanDesc, Memory*& memory); + Result CreateGraphicsPipeline(NRIVkPipeline vkPipeline, Pipeline*& pipeline); + Result CreateComputePipeline(NRIVkPipeline vkPipeline, Pipeline*& pipeline); + Result CreateQueryPool(const QueryPoolVulkanDesc& queryPoolVulkanDesc, QueryPool*& queryPool); + Result CreateAccelerationStructure(const AccelerationStructureVulkanDesc& accelerationStructureDesc, AccelerationStructure*& accelerationStructure); + void DestroyCommandAllocator(CommandAllocator& commandAllocator); + void DestroyDescriptorPool(DescriptorPool& descriptorPool); + void DestroyBuffer(Buffer& buffer); + void DestroyTexture(Texture& texture); + void DestroyDescriptor(Descriptor& descriptor); + void DestroyPipelineLayout(PipelineLayout& pipelineLayout); + void DestroyPipeline(Pipeline& pipeline); + void DestroyFrameBuffer(FrameBuffer& frameBuffer); + void DestroyQueryPool(QueryPool& queryPool); + void DestroyFence(Fence& fence); + void DestroySwapChain(SwapChain& swapChain); + void DestroyAccelerationStructure(AccelerationStructure& accelerationStructure); + Result GetDisplays(Display** displays, uint32_t& displayNum); + Result GetDisplaySize(Display& display, uint16_t& width, uint16_t& height); + Result AllocateMemory(uint32_t physicalDeviceMask, MemoryType memoryType, uint64_t size, Memory*& memory); + Result BindBufferMemory(const BufferMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum); + Result BindTextureMemory(const TextureMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum); + Result BindAccelerationStructureMemory(const AccelerationStructureMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum); + void FreeMemory(Memory& memory); + FormatSupportBits GetFormatSupport(Format format) const; + uint32_t CalculateAllocationNumber(const ResourceGroupDesc& resourceGroupDesc) const; + Result AllocateAndBindMemory(const ResourceGroupDesc& resourceGroupDesc, Memory** allocations); + + //================================================================================================================ + // DeviceBase + //================================================================================================================ + + void Destroy(); + Result FillFunctionTable(CoreInterface& table) const; + Result FillFunctionTable(SwapChainInterface& table) const; + Result FillFunctionTable(WrapperVKInterface& wrapperVKInterface) const; + Result FillFunctionTable(RayTracingInterface& rayTracingInterface) const; + Result FillFunctionTable(MeshShaderInterface& meshShaderInterface) const; + Result FillFunctionTable(HelperInterface& helperInterface) const; + +private: + Result CreateInstance(const DeviceCreationDesc& deviceCreationDesc); + Result FindPhysicalDeviceGroup(const PhysicalDeviceGroup* physicalDeviceGroup, bool enableMGPU); + Result CreateLogicalDevice(const DeviceCreationDesc& deviceCreationDesc); + void FillFamilyIndices(bool useEnabledFamilyIndices, const uint32_t* enabledFamilyIndices, uint32_t familyIndexNum); + void SetDeviceLimits(bool enableValidation); + void CreateCommandQueues(); + Result ResolvePreInstanceDispatchTable(); + Result ResolveInstanceDispatchTable(); + Result ResolveDispatchTable(); + void FilterInstanceLayers(Vector& layers); + void ReportDeviceGroupInfo(); + void FindDXGIAdapter(); + + template< typename Implementation, typename Interface, typename ... Args > + Result CreateImplementation(Interface*& entity, const Args&... args); + +private: + Lock m_Lock; + Vector m_PhysicalDevices; + Vector m_PhysicalDeviceIndices; + Vector m_ConcurrentSharingModeQueueIndices; + std::array m_FamilyIndices = {}; + std::array m_Queues = {}; + DispatchTable m_VK = {}; + DeviceDesc m_DeviceDesc = {}; + VkPhysicalDeviceMemoryProperties m_MemoryProps = {}; + VkAllocationCallbacks m_AllocationCallbacks = {}; + SPIRVBindingOffsets m_SPIRVBindingOffsets = {}; + CoreInterface m_CoreInterface = {}; + Library* m_Loader = nullptr; + VkDevice m_Device = VK_NULL_HANDLE; + VkInstance m_Instance = VK_NULL_HANDLE; + VkAllocationCallbacks* m_AllocationCallbackPtr = nullptr; + VkDebugUtilsMessengerEXT m_Messenger = VK_NULL_HANDLE; + uint64_t m_LUID = 0; #if _WIN32 - ComPtr m_Adapter; + ComPtr m_Adapter; #endif - }; - - inline DeviceVK::operator VkDevice() const - { - return m_Device; - } - - inline DeviceVK::operator VkPhysicalDevice() const - { - return m_PhysicalDevices.front(); - } - - inline DeviceVK::operator VkInstance() const - { - return m_Instance; - } - - inline const DispatchTable& DeviceVK::GetDispatchTable() const - { - return m_VK; - } - - inline const VkAllocationCallbacks* DeviceVK::GetAllocationCallbacks() const - { - return m_AllocationCallbackPtr; - } - - inline const std::array& DeviceVK::GetQueueFamilyIndices() const - { - return m_FamilyIndices; - } - - inline const SPIRVBindingOffsets& DeviceVK::GetSPIRVBindingOffsets() const - { - return m_SPIRVBindingOffsets; - } + bool m_OwnsNativeObjects = false; + bool m_IsDebugUtilsSupported = false; + bool m_IsSubsetAllocationSupported = false; + bool m_IsDescriptorIndexingSupported = false; + bool m_IsBufferDeviceAddressSupported = false; + bool m_IsSampleLocationExtSupported = false; + bool m_IsConservativeRasterExtSupported = false; + bool m_IsRayTracingExtSupported = false; + bool m_IsMicroMapSupported = false; + bool m_IsMeshShaderExtSupported = false; + bool m_IsHDRExtSupported = false; +}; - inline const CoreInterface& DeviceVK::GetCoreInterface() const - { - return m_CoreInterface; - } - - inline uint32_t DeviceVK::GetPhysicalDeviceGroupSize() const - { - return m_DeviceDesc.physicalDeviceNum; - } - - inline bool DeviceVK::IsDescriptorIndexingExtSupported() const - { - return m_IsDescriptorIndexingExtSupported; - } - - inline bool DeviceVK::IsConcurrentSharingModeEnabledForBuffers() const - { - return m_IsConcurrentSharingModeEnabledForBuffers; - } - - inline bool DeviceVK::IsConcurrentSharingModeEnabledForImages() const - { - return m_IsConcurrentSharingModeEnabledForImages; - } - - inline bool DeviceVK::IsBufferDeviceAddressSupported() const - { - return m_IsBufferDeviceAddressSupported; - } - - inline const Vector& DeviceVK::GetConcurrentSharingModeQueueIndices() const - { - return m_ConcurrentSharingModeQueueIndices; - } } - -/* - TODO: - - Cubemaps and 3D textures - Some flags are missing in VkImageCreateInfo -*/ \ No newline at end of file diff --git a/Source/VK/DeviceVK.hpp b/Source/VK/DeviceVK.hpp index a9ecabb6..f2372363 100644 --- a/Source/VK/DeviceVK.hpp +++ b/Source/VK/DeviceVK.hpp @@ -8,9 +8,9 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma once +Declare_PartiallyFillFunctionTable_Functions(VK) -#pragma region [ CoreInterface ] +#pragma region [ Core ] static const DeviceDesc& NRI_CALL GetDeviceDesc(const Device& device) { @@ -97,14 +97,9 @@ static Result NRI_CALL CreateQueryPool(Device& device, const QueryPoolDesc& quer return ((DeviceVK&)device).CreateQueryPool(queryPoolDesc, queryPool); } -static Result NRI_CALL CreateQueueSemaphore(Device& device, QueueSemaphore*& queueSemaphore) +static Result NRI_CALL CreateFence(Device& device, uint64_t initialValue, Fence*& fence) { - return ((DeviceVK&)device).CreateQueueSemaphore(queueSemaphore); -} - -static Result NRI_CALL CreateDeviceSemaphore(Device& device, bool signaled, DeviceSemaphore*& deviceSemaphore) -{ - return ((DeviceVK&)device).CreateDeviceSemaphore(signaled, deviceSemaphore); + return ((DeviceVK&)device).CreateFence(initialValue, fence); } static void NRI_CALL DestroyCommandAllocator(CommandAllocator& commandAllocator) @@ -152,14 +147,9 @@ static void NRI_CALL DestroyQueryPool(QueryPool& queryPool) ((QueryPoolVK&)queryPool).GetDevice().DestroyQueryPool(queryPool); } -static void NRI_CALL DestroyQueueSemaphore(QueueSemaphore& queueSemaphore) -{ - ((QueueSemaphoreVK&)queueSemaphore).GetDevice().DestroyQueueSemaphore(queueSemaphore); -} - -static void NRI_CALL DestroyDeviceSemaphore(DeviceSemaphore& deviceSemaphore) +static void NRI_CALL DestroyFence(Fence& fence) { - ((DeviceSemaphoreVK&)deviceSemaphore).GetDevice().DestroyDeviceSemaphore(deviceSemaphore); + ((FenceVK&)fence).GetDevice().DestroyFence(fence); } static Result NRI_CALL AllocateMemory(Device& device, uint32_t physicalDeviceMask, MemoryType memoryType, uint64_t size, Memory*& memory) @@ -192,51 +182,37 @@ static void NRI_CALL SetDeviceDebugName(Device& device, const char* name) ((DeviceVK&)device).SetDebugName(name); } +static void NRI_CALL SetPipelineDebugName(Pipeline& pipeline, const char* name) +{ + ((PipelineVK&)pipeline).SetDebugName(name); +} + +static void NRI_CALL SetPipelineLayoutDebugName(PipelineLayout& pipelineLayout, const char* name) +{ + ((PipelineLayoutVK&)pipelineLayout).SetDebugName(name); +} + +static void NRI_CALL SetFrameBufferDebugName(FrameBuffer& frameBuffer, const char* name) +{ + ((FrameBufferVK&)frameBuffer).SetDebugName(name); +} + +static void NRI_CALL SetMemoryDebugName(Memory& memory, const char* name) +{ + ((MemoryVK&)memory).SetDebugName(name); +} + static void* NRI_CALL GetDeviceNativeObject(const Device& device) { return (VkDevice)((DeviceVK&)device); } -void FillFunctionTableBufferVK(CoreInterface& coreInterface); -void FillFunctionTableCommandAllocatorVK(CoreInterface& coreInterface); -void FillFunctionTableCommandBufferVK(CoreInterface& coreInterface); -void FillFunctionTableCommandQueueVK(CoreInterface& coreInterface); -void FillFunctionTableDescriptorPoolVK(CoreInterface& coreInterface); -void FillFunctionTableDescriptorSetVK(CoreInterface& coreInterface); -void FillFunctionTableQueryPoolVK(CoreInterface& coreInterface); -void FillFunctionTableTextureVK(CoreInterface& coreInterface); -void FillFunctionTableDescriptorVK(CoreInterface& coreInterface); -void FillFunctionTableDeviceSemaphoreVK(CoreInterface& coreInterface); -void FillFunctionTableFrameBufferVK(CoreInterface& coreInterface); -void FillFunctionTableMemoryVK(CoreInterface& coreInterface); -void FillFunctionTablePipelineLayoutVK(CoreInterface& coreInterface); -void FillFunctionTablePipelineVK(CoreInterface& coreInterface); -void FillFunctionTableQueueSemaphoreVK(CoreInterface& coreInterface); - Result DeviceVK::FillFunctionTable(CoreInterface& coreInterface) const { coreInterface = {}; - - FillFunctionTableBufferVK(coreInterface); - FillFunctionTableCommandAllocatorVK(coreInterface); - FillFunctionTableCommandBufferVK(coreInterface); - FillFunctionTableCommandQueueVK(coreInterface); - FillFunctionTableDescriptorPoolVK(coreInterface); - FillFunctionTableDescriptorSetVK(coreInterface); - FillFunctionTableQueryPoolVK(coreInterface); - FillFunctionTableTextureVK(coreInterface); - FillFunctionTableDescriptorVK(coreInterface); - FillFunctionTableDeviceSemaphoreVK(coreInterface); - FillFunctionTableFrameBufferVK(coreInterface); - FillFunctionTableMemoryVK(coreInterface); - FillFunctionTablePipelineLayoutVK(coreInterface); - FillFunctionTablePipelineVK(coreInterface); - FillFunctionTableQueueSemaphoreVK(coreInterface); - coreInterface.GetDeviceDesc = ::GetDeviceDesc; coreInterface.GetFormatSupport = ::GetFormatSupport; coreInterface.GetCommandQueue = ::GetCommandQueue; - coreInterface.CreateCommandAllocator = ::CreateCommandAllocator; coreInterface.CreateDescriptorPool = ::CreateDescriptorPool; coreInterface.CreateBuffer = ::CreateBuffer; @@ -251,9 +227,7 @@ Result DeviceVK::FillFunctionTable(CoreInterface& coreInterface) const coreInterface.CreateComputePipeline = ::CreateComputePipeline; coreInterface.CreateFrameBuffer = ::CreateFrameBuffer; coreInterface.CreateQueryPool = ::CreateQueryPool; - coreInterface.CreateQueueSemaphore = ::CreateQueueSemaphore; - coreInterface.CreateDeviceSemaphore = ::CreateDeviceSemaphore; - + coreInterface.CreateFence = ::CreateFence; coreInterface.DestroyCommandAllocator = ::DestroyCommandAllocator; coreInterface.DestroyDescriptorPool = ::DestroyDescriptorPool; coreInterface.DestroyBuffer = ::DestroyBuffer; @@ -263,24 +237,35 @@ Result DeviceVK::FillFunctionTable(CoreInterface& coreInterface) const coreInterface.DestroyPipeline = ::DestroyPipeline; coreInterface.DestroyFrameBuffer = ::DestroyFrameBuffer; coreInterface.DestroyQueryPool = ::DestroyQueryPool; - coreInterface.DestroyQueueSemaphore = ::DestroyQueueSemaphore; - coreInterface.DestroyDeviceSemaphore = ::DestroyDeviceSemaphore; - + coreInterface.DestroyFence = ::DestroyFence; coreInterface.AllocateMemory = ::AllocateMemory; coreInterface.BindBufferMemory = ::BindBufferMemory; coreInterface.BindTextureMemory = ::BindTextureMemory; coreInterface.FreeMemory = ::FreeMemory; - coreInterface.SetDeviceDebugName = ::SetDeviceDebugName; - + coreInterface.SetPipelineDebugName = ::SetPipelineDebugName; + coreInterface.SetPipelineLayoutDebugName = ::SetPipelineLayoutDebugName; + coreInterface.SetFrameBufferDebugName = ::SetFrameBufferDebugName; + coreInterface.SetMemoryDebugName = ::SetMemoryDebugName; coreInterface.GetDeviceNativeObject = ::GetDeviceNativeObject; + Core_Buffer_PartiallyFillFunctionTableVK(coreInterface); + Core_CommandAllocator_PartiallyFillFunctionTableVK(coreInterface); + Core_CommandBuffer_PartiallyFillFunctionTableVK(coreInterface); + Core_CommandQueue_PartiallyFillFunctionTableVK(coreInterface); + Core_Descriptor_PartiallyFillFunctionTableVK(coreInterface); + Core_DescriptorPool_PartiallyFillFunctionTableVK(coreInterface); + Core_DescriptorSet_PartiallyFillFunctionTableVK(coreInterface); + Core_Fence_PartiallyFillFunctionTableVK(coreInterface); + Core_QueryPool_PartiallyFillFunctionTableVK(coreInterface); + Core_Texture_PartiallyFillFunctionTableVK(coreInterface); + return ValidateFunctionTable(GetLog(), coreInterface); } #pragma endregion -#pragma region [ SwapChainInterface ] +#pragma region [ SwapChain ] static Result NRI_CALL CreateSwapChain(Device& device, const SwapChainDesc& swapChainDesc, SwapChain*& swapChain) { @@ -302,87 +287,74 @@ static Result NRI_CALL GetDisplaySize(Device& device, nri::Display& display, uin return ((DeviceVK&)device).GetDisplaySize(display, width, height); } -void FillFunctionTableSwapChainVK(SwapChainInterface& swapChainInterface); - Result DeviceVK::FillFunctionTable(SwapChainInterface& swapChainInterface) const { swapChainInterface = {}; - - FillFunctionTableSwapChainVK(swapChainInterface); - swapChainInterface.CreateSwapChain = ::CreateSwapChain; swapChainInterface.DestroySwapChain = ::DestroySwapChain; swapChainInterface.GetDisplays = ::GetDisplays; swapChainInterface.GetDisplaySize = ::GetDisplaySize; + SwapChain_PartiallyFillFunctionTableVK(swapChainInterface); + return ValidateFunctionTable(GetLog(), swapChainInterface); } #pragma endregion -#pragma region [ WrapperVKInterface ] +#pragma region [ WrapperVK ] -static Result NRI_CALL CreateCommandQueueVK(Device& device, const CommandQueueVulkanDesc& commandQueueVulkanDesc, CommandQueue*& commandQueue) +static Result NRI_CALL CreateCommandQueue(Device& device, const CommandQueueVulkanDesc& commandQueueVulkanDesc, CommandQueue*& commandQueue) { return ((DeviceVK&)device).CreateCommandQueue(commandQueueVulkanDesc, commandQueue); } -static Result NRI_CALL CreateCommandAllocatorVK(Device& device, const CommandAllocatorVulkanDesc& commandAllocatorVulkanDesc, CommandAllocator*& commandAllocator) +static Result NRI_CALL CreateCommandAllocator(Device& device, const CommandAllocatorVulkanDesc& commandAllocatorVulkanDesc, CommandAllocator*& commandAllocator) { return ((DeviceVK&)device).CreateCommandAllocator(commandAllocatorVulkanDesc, commandAllocator); } -static Result NRI_CALL CreateCommandBufferVK(Device& device, const CommandBufferVulkanDesc& commandBufferVulkanDesc, CommandBuffer*& commandBuffer) +static Result NRI_CALL CreateCommandBuffer(Device& device, const CommandBufferVulkanDesc& commandBufferVulkanDesc, CommandBuffer*& commandBuffer) { return ((DeviceVK&)device).CreateCommandBuffer(commandBufferVulkanDesc, commandBuffer); } -static Result NRI_CALL CreateDescriptorPoolVK(Device& device, NRIVkDescriptorPool vkDescriptorPool, DescriptorPool*& descriptorPool) +static Result NRI_CALL CreateDescriptorPool(Device& device, NRIVkDescriptorPool vkDescriptorPool, DescriptorPool*& descriptorPool) { return ((DeviceVK&)device).CreateDescriptorPool(vkDescriptorPool, descriptorPool); } -static Result NRI_CALL CreateBufferVK(Device& device, const BufferVulkanDesc& bufferVulkanDesc, Buffer*& buffer) +static Result NRI_CALL CreateBuffer(Device& device, const BufferVulkanDesc& bufferVulkanDesc, Buffer*& buffer) { return ((DeviceVK&)device).CreateBuffer(bufferVulkanDesc, buffer); } -static Result NRI_CALL CreateTextureVK(Device& device, const TextureVulkanDesc& textureVulkanDesc, Texture*& texture) +static Result NRI_CALL CreateTexture(Device& device, const TextureVulkanDesc& textureVulkanDesc, Texture*& texture) { return ((DeviceVK&)device).CreateTexture(textureVulkanDesc, texture); } -static Result NRI_CALL CreateMemoryVK(Device& device, const MemoryVulkanDesc& memoryVulkanDesc, Memory*& memory) +static Result NRI_CALL CreateMemory(Device& device, const MemoryVulkanDesc& memoryVulkanDesc, Memory*& memory) { return ((DeviceVK&)device).CreateMemory(memoryVulkanDesc, memory); } -static Result NRI_CALL CreateGraphicsPipelineVK(Device& device, NRIVkPipeline vkPipeline, Pipeline*& pipeline) +static Result NRI_CALL CreateGraphicsPipeline(Device& device, NRIVkPipeline vkPipeline, Pipeline*& pipeline) { return ((DeviceVK&)device).CreateGraphicsPipeline(vkPipeline, pipeline); } -static Result NRI_CALL CreateComputePipelineVK(Device& device, NRIVkPipeline vkPipeline, Pipeline*& pipeline) +static Result NRI_CALL CreateComputePipeline(Device& device, NRIVkPipeline vkPipeline, Pipeline*& pipeline) { return ((DeviceVK&)device).CreateComputePipeline(vkPipeline, pipeline); } -static Result NRI_CALL CreateQueryPoolVK(Device& device, const QueryPoolVulkanDesc& queryPoolVulkanDesc, QueryPool*& queryPool) +static Result NRI_CALL CreateQueryPool(Device& device, const QueryPoolVulkanDesc& queryPoolVulkanDesc, QueryPool*& queryPool) { return ((DeviceVK&)device).CreateQueryPool(queryPoolVulkanDesc, queryPool); } -static Result NRI_CALL CreateQueueSemaphoreVK(Device& device, NRIVkSemaphore vkSemaphore, QueueSemaphore*& queueSemaphore) -{ - return ((DeviceVK&)device).CreateQueueSemaphore(vkSemaphore, queueSemaphore); -} - -static Result NRI_CALL CreateDeviceSemaphoreVK(Device& device, NRIVkFence vkFence, DeviceSemaphore*& deviceSemaphore) -{ - return ((DeviceVK&)device).CreateDeviceSemaphore(vkFence, deviceSemaphore); -} - -static Result NRI_CALL CreateAccelerationStructureVK(Device& device, const AccelerationStructureVulkanDesc& accelerationStructureDesc, AccelerationStructure*& accelerationStructure) +static Result NRI_CALL CreateAccelerationStructure(Device& device, const AccelerationStructureVulkanDesc& accelerationStructureDesc, AccelerationStructure*& accelerationStructure) { return ((DeviceVK&)device).CreateAccelerationStructure(accelerationStructureDesc, accelerationStructure); } @@ -410,20 +382,17 @@ static NRIVkProcAddress NRI_CALL GetVkGetDeviceProcAddr(const Device& device) Result DeviceVK::FillFunctionTable(WrapperVKInterface& wrapperVKInterface) const { wrapperVKInterface = {}; - wrapperVKInterface.CreateCommandQueueVK = ::CreateCommandQueueVK; - wrapperVKInterface.CreateCommandAllocatorVK = ::CreateCommandAllocatorVK; - wrapperVKInterface.CreateCommandBufferVK = ::CreateCommandBufferVK; - wrapperVKInterface.CreateDescriptorPoolVK = ::CreateDescriptorPoolVK; - wrapperVKInterface.CreateBufferVK = ::CreateBufferVK; - wrapperVKInterface.CreateTextureVK = ::CreateTextureVK; - wrapperVKInterface.CreateMemoryVK = ::CreateMemoryVK; - wrapperVKInterface.CreateGraphicsPipelineVK = ::CreateGraphicsPipelineVK; - wrapperVKInterface.CreateComputePipelineVK = ::CreateComputePipelineVK; - wrapperVKInterface.CreateQueryPoolVK = ::CreateQueryPoolVK; - wrapperVKInterface.CreateQueueSemaphoreVK = ::CreateQueueSemaphoreVK; - wrapperVKInterface.CreateDeviceSemaphoreVK = ::CreateDeviceSemaphoreVK; - wrapperVKInterface.CreateAccelerationStructureVK = ::CreateAccelerationStructureVK; - + wrapperVKInterface.CreateCommandQueueVK = ::CreateCommandQueue; + wrapperVKInterface.CreateCommandAllocatorVK = ::CreateCommandAllocator; + wrapperVKInterface.CreateCommandBufferVK = ::CreateCommandBuffer; + wrapperVKInterface.CreateDescriptorPoolVK = ::CreateDescriptorPool; + wrapperVKInterface.CreateBufferVK = ::CreateBuffer; + wrapperVKInterface.CreateTextureVK = ::CreateTexture; + wrapperVKInterface.CreateMemoryVK = ::CreateMemory; + wrapperVKInterface.CreateGraphicsPipelineVK = ::CreateGraphicsPipeline; + wrapperVKInterface.CreateComputePipelineVK = ::CreateComputePipeline; + wrapperVKInterface.CreateQueryPoolVK = ::CreateQueryPool; + wrapperVKInterface.CreateAccelerationStructureVK = ::CreateAccelerationStructure; wrapperVKInterface.GetVkPhysicalDevice = ::GetVkPhysicalDevice; wrapperVKInterface.GetVkInstance = ::GetVkInstance; wrapperVKInterface.GetVkGetDeviceProcAddr = ::GetVkGetDeviceProcAddr; @@ -434,7 +403,7 @@ Result DeviceVK::FillFunctionTable(WrapperVKInterface& wrapperVKInterface) const #pragma endregion -#pragma region [ RayTracingInterface ] +#pragma region [ RayTracing ] static Result NRI_CALL CreateRayTracingPipeline(Device& device, const RayTracingPipelineDesc& pipelineDesc, Pipeline*& pipeline) { @@ -456,9 +425,7 @@ static void NRI_CALL DestroyAccelerationStructure(AccelerationStructure& acceler return ((AccelerationStructureVK&)accelerationStructure).GetDevice().DestroyAccelerationStructure(accelerationStructure); } -void FillFunctionTableCommandBufferVK(RayTracingInterface& rayTracingInterface); void FillFunctionTablePipelineVK(RayTracingInterface& rayTracingInterface); -void FillFunctionTableAccelerationStructureVK(RayTracingInterface& rayTracingInterface); Result DeviceVK::FillFunctionTable(RayTracingInterface& rayTracingInterface) const { @@ -466,24 +433,21 @@ Result DeviceVK::FillFunctionTable(RayTracingInterface& rayTracingInterface) con return Result::UNSUPPORTED; rayTracingInterface = {}; - rayTracingInterface.CreateRayTracingPipeline = ::CreateRayTracingPipeline; rayTracingInterface.CreateAccelerationStructure = ::CreateAccelerationStructure; rayTracingInterface.BindAccelerationStructureMemory = ::BindAccelerationStructureMemory; rayTracingInterface.DestroyAccelerationStructure = ::DestroyAccelerationStructure; - FillFunctionTableCommandBufferVK(rayTracingInterface); + RayTracing_CommandBuffer_PartiallyFillFunctionTableVK(rayTracingInterface); + RayTracing_AccelerationStructure_PartiallyFillFunctionTableVK(rayTracingInterface); FillFunctionTablePipelineVK(rayTracingInterface); - FillFunctionTableAccelerationStructureVK(rayTracingInterface); return ValidateFunctionTable(GetLog(), rayTracingInterface); } #pragma endregion -#pragma region [ MeshShaderInterface ] - -void FillFunctionTableCommandBufferVK(MeshShaderInterface& meshShaderInterface); +#pragma region [ MeshShader ] Result DeviceVK::FillFunctionTable(MeshShaderInterface& meshShaderInterface) const { @@ -492,34 +456,32 @@ Result DeviceVK::FillFunctionTable(MeshShaderInterface& meshShaderInterface) con meshShaderInterface = {}; - FillFunctionTableCommandBufferVK(meshShaderInterface); + MeshShader_CommandBuffer_PartiallyFillFunctionTableVK(meshShaderInterface); return ValidateFunctionTable(GetLog(), meshShaderInterface); } #pragma endregion -#pragma region [ HelperInterface ] +#pragma region [ Helper ] -static uint32_t NRI_CALL CountAllocationNumVK(Device& device, const ResourceGroupDesc& resourceGroupDesc) +static uint32_t NRI_CALL CountAllocationNum(Device& device, const ResourceGroupDesc& resourceGroupDesc) { return ((DeviceVK&)device).CalculateAllocationNumber(resourceGroupDesc); } -static Result NRI_CALL AllocateAndBindMemoryVK(Device& device, const ResourceGroupDesc& resourceGroupDesc, Memory** allocations) +static Result NRI_CALL AllocateAndBindMemory(Device& device, const ResourceGroupDesc& resourceGroupDesc, Memory** allocations) { return ((DeviceVK&)device).AllocateAndBindMemory(resourceGroupDesc, allocations); } -void FillFunctionTableCommandQueueVK(HelperInterface& helperInterface); - Result DeviceVK::FillFunctionTable(HelperInterface& helperInterface) const { helperInterface = {}; + helperInterface.CalculateAllocationNumber = ::CountAllocationNum; + helperInterface.AllocateAndBindMemory = ::AllocateAndBindMemory; - helperInterface.CalculateAllocationNumber = ::CountAllocationNumVK; - helperInterface.AllocateAndBindMemory = ::AllocateAndBindMemoryVK; - FillFunctionTableCommandQueueVK(helperInterface); + Helper_CommandQueue_PartiallyFillFunctionTableVK(helperInterface); return ValidateFunctionTable(GetLog(), helperInterface); } diff --git a/Source/VK/DispatchTable.h b/Source/VK/DispatchTable.h index 58f9e5d1..3ee03a1e 100644 --- a/Source/VK/DispatchTable.h +++ b/Source/VK/DispatchTable.h @@ -14,15 +14,32 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. struct DispatchTable { + VULKAN_FUNCTION(GetInstanceProcAddr); VULKAN_FUNCTION(CreateInstance); VULKAN_FUNCTION(EnumerateInstanceExtensionProperties); VULKAN_FUNCTION(EnumerateInstanceLayerProperties); - VULKAN_FUNCTION(GetInstanceProcAddr); + VULKAN_FUNCTION(GetDeviceProcAddr); + VULKAN_FUNCTION(DestroyInstance); + VULKAN_FUNCTION(DestroyDevice); + VULKAN_FUNCTION(GetPhysicalDeviceMemoryProperties); + VULKAN_FUNCTION(GetDeviceGroupPeerMemoryFeatures); + VULKAN_FUNCTION(GetPhysicalDeviceFormatProperties); + VULKAN_FUNCTION(CreateDevice); + VULKAN_FUNCTION(GetDeviceQueue); + VULKAN_FUNCTION(EnumeratePhysicalDeviceGroups); + VULKAN_FUNCTION(GetPhysicalDeviceProperties); + VULKAN_FUNCTION(GetPhysicalDeviceProperties2); + VULKAN_FUNCTION(GetPhysicalDeviceFeatures); + VULKAN_FUNCTION(GetPhysicalDeviceFeatures2); + VULKAN_FUNCTION(GetPhysicalDeviceQueueFamilyProperties); + VULKAN_FUNCTION(EnumerateDeviceExtensionProperties); VULKAN_FUNCTION(GetPhysicalDeviceSurfaceFormatsKHR); VULKAN_FUNCTION(GetPhysicalDeviceSurfaceSupportKHR); VULKAN_FUNCTION(GetPhysicalDeviceSurfaceCapabilitiesKHR); VULKAN_FUNCTION(GetPhysicalDeviceSurfacePresentModesKHR); + VULKAN_FUNCTION(DestroySurfaceKHR); + #if VK_USE_PLATFORM_WIN32_KHR VULKAN_FUNCTION(CreateWin32SurfaceKHR); #endif @@ -35,21 +52,6 @@ struct DispatchTable #if VK_USE_PLATFORM_WAYLAND_KHR VULKAN_FUNCTION(CreateWaylandSurfaceKHR); #endif - VULKAN_FUNCTION(DestroySurfaceKHR); - VULKAN_FUNCTION(GetDeviceProcAddr); - VULKAN_FUNCTION(DestroyInstance); - VULKAN_FUNCTION(DestroyDevice); - VULKAN_FUNCTION(GetPhysicalDeviceMemoryProperties); - VULKAN_FUNCTION(GetDeviceGroupPeerMemoryFeatures); - VULKAN_FUNCTION(CreateDevice); - VULKAN_FUNCTION(GetDeviceQueue); - VULKAN_FUNCTION(EnumeratePhysicalDeviceGroups); - VULKAN_FUNCTION(GetPhysicalDeviceProperties); - VULKAN_FUNCTION(GetPhysicalDeviceProperties2); - VULKAN_FUNCTION(GetPhysicalDeviceFeatures); - VULKAN_FUNCTION(GetPhysicalDeviceFeatures2); - VULKAN_FUNCTION(GetPhysicalDeviceQueueFamilyProperties); - VULKAN_FUNCTION(EnumerateDeviceExtensionProperties); VULKAN_FUNCTION(CreateBuffer); VULKAN_FUNCTION(CreateImage); @@ -60,7 +62,6 @@ struct DispatchTable VULKAN_FUNCTION(CreateFramebuffer); VULKAN_FUNCTION(CreateQueryPool); VULKAN_FUNCTION(CreateCommandPool); - VULKAN_FUNCTION(CreateFence); VULKAN_FUNCTION(CreateSemaphore); VULKAN_FUNCTION(CreateDescriptorPool); VULKAN_FUNCTION(CreatePipelineLayout); @@ -79,7 +80,6 @@ struct DispatchTable VULKAN_FUNCTION(DestroyFramebuffer); VULKAN_FUNCTION(DestroyQueryPool); VULKAN_FUNCTION(DestroyCommandPool); - VULKAN_FUNCTION(DestroyFence); VULKAN_FUNCTION(DestroySemaphore); VULKAN_FUNCTION(DestroyDescriptorPool); VULKAN_FUNCTION(DestroyPipelineLayout); @@ -95,20 +95,23 @@ struct DispatchTable VULKAN_FUNCTION(BindBufferMemory2); VULKAN_FUNCTION(BindImageMemory2); + VULKAN_FUNCTION(GetBufferMemoryRequirements2); + VULKAN_FUNCTION(GetImageMemoryRequirements2); + VULKAN_FUNCTION(QueueWaitIdle); - VULKAN_FUNCTION(WaitForFences); - VULKAN_FUNCTION(ResetFences); VULKAN_FUNCTION(AcquireNextImageKHR); VULKAN_FUNCTION(QueueSubmit); VULKAN_FUNCTION(QueuePresentKHR); + VULKAN_FUNCTION(GetSemaphoreCounterValue); + VULKAN_FUNCTION(WaitSemaphores); + VULKAN_FUNCTION(ResetCommandPool); VULKAN_FUNCTION(ResetDescriptorPool); VULKAN_FUNCTION(AllocateCommandBuffers); VULKAN_FUNCTION(AllocateDescriptorSets); VULKAN_FUNCTION(FreeCommandBuffers); VULKAN_FUNCTION(FreeDescriptorSets); - VULKAN_FUNCTION(UpdateDescriptorSets); VULKAN_FUNCTION(BeginCommandBuffer); @@ -140,18 +143,18 @@ struct DispatchTable VULKAN_FUNCTION(CmdWriteTimestamp); VULKAN_FUNCTION(CmdCopyQueryPoolResults); VULKAN_FUNCTION(CmdResetQueryPool); - VULKAN_FUNCTION(CmdBeginDebugUtilsLabelEXT); - VULKAN_FUNCTION(CmdEndDebugUtilsLabelEXT); VULKAN_FUNCTION(CmdEndRenderPass); VULKAN_FUNCTION(CmdFillBuffer); VULKAN_FUNCTION(EndCommandBuffer); - VULKAN_FUNCTION(GetBufferMemoryRequirements2); - VULKAN_FUNCTION(GetImageMemoryRequirements2); - VULKAN_FUNCTION(GetPhysicalDeviceFormatProperties); - VULKAN_FUNCTION(GetSwapchainImagesKHR); + + VULKAN_FUNCTION(SetDebugUtilsObjectNameEXT); + VULKAN_FUNCTION(CmdBeginDebugUtilsLabelEXT); + VULKAN_FUNCTION(CmdEndDebugUtilsLabelEXT); + VULKAN_FUNCTION(SetHdrMetadataEXT); + VULKAN_FUNCTION(CreateAccelerationStructureKHR); VULKAN_FUNCTION(CreateRayTracingPipelinesKHR); VULKAN_FUNCTION(DestroyAccelerationStructureKHR); @@ -164,9 +167,7 @@ struct DispatchTable VULKAN_FUNCTION(CmdTraceRaysKHR); VULKAN_FUNCTION(GetBufferDeviceAddress); - VULKAN_FUNCTION(CmdDrawMeshTasksNV); - - VULKAN_FUNCTION(SetDebugUtilsObjectNameEXT); + VULKAN_FUNCTION(CmdDrawMeshTasksEXT); }; -#undef VULKAN_FUNCTION \ No newline at end of file +#undef VULKAN_FUNCTION diff --git a/Source/VK/FenceVK.cpp b/Source/VK/FenceVK.cpp new file mode 100644 index 00000000..cd437a48 --- /dev/null +++ b/Source/VK/FenceVK.cpp @@ -0,0 +1,83 @@ +/* +Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. + +NVIDIA CORPORATION and its licensors retain all intellectual property +and proprietary rights in and to this software, related documentation +and any modifications thereto. Any use, reproduction, disclosure or +distribution of this software and related documentation without an express +license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "SharedVK.h" +#include "FenceVK.h" +#include "CommandQueueVK.h" + +using namespace nri; + +FenceVK::~FenceVK() +{ + const auto& vk = m_Device.GetDispatchTable(); + if (m_Fence != VK_NULL_HANDLE) + vk.DestroySemaphore(m_Device, m_Fence, m_Device.GetAllocationCallbacks()); +} + +Result FenceVK::Create(uint64_t initialValue) +{ + VkSemaphoreTypeCreateInfo timelineCreateInfo = { VK_STRUCTURE_TYPE_SEMAPHORE_TYPE_CREATE_INFO, nullptr, VK_SEMAPHORE_TYPE_TIMELINE, initialValue }; + VkSemaphoreCreateInfo createInfo = { VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, &timelineCreateInfo, 0 }; + + const auto& vk = m_Device.GetDispatchTable(); + VkResult result = vk.CreateSemaphore((VkDevice)m_Device, &createInfo, m_Device.GetAllocationCallbacks(), &m_Fence); + + RETURN_ON_FAILURE(m_Device.GetLog(), result == VK_SUCCESS, GetReturnCode(result), + "Can't create a semaphore: vk.CreateSemaphore returned %d.", (int32_t)result); + + return Result::SUCCESS; +} + +//================================================================================================================ +// NRI +//================================================================================================================ + +inline void FenceVK::SetDebugName(const char* name) +{ + m_Device.SetDebugNameToTrivialObject(VK_OBJECT_TYPE_SEMAPHORE, (uint64_t)m_Fence, name); +} + +inline uint64_t FenceVK::GetFenceValue() const +{ + uint64_t value = 0; + + const auto& vk = m_Device.GetDispatchTable(); + vk.GetSemaphoreCounterValue((VkDevice)m_Device, m_Fence, &value); + + return value; +} + +inline void FenceVK::QueueSignal(CommandQueueVK& commandQueue, uint64_t value) +{ + VkTimelineSemaphoreSubmitInfo timelineInfo = { VK_STRUCTURE_TYPE_TIMELINE_SEMAPHORE_SUBMIT_INFO, nullptr, 0, nullptr, 1, &value }; + VkSubmitInfo submitInfo = { VK_STRUCTURE_TYPE_SUBMIT_INFO, &timelineInfo, 0, nullptr, nullptr, 0, nullptr, 1, &m_Fence }; + + const auto& vk = m_Device.GetDispatchTable(); + vk.QueueSubmit((VkQueue)commandQueue, 1, &submitInfo, VK_NULL_HANDLE); +} + +inline void FenceVK::QueueWait(CommandQueueVK& commandQueue, uint64_t value) +{ + VkTimelineSemaphoreSubmitInfo timelineInfo = { VK_STRUCTURE_TYPE_TIMELINE_SEMAPHORE_SUBMIT_INFO, nullptr, 1, &value, 0, nullptr }; + VkSubmitInfo submitInfo = { VK_STRUCTURE_TYPE_SUBMIT_INFO, &timelineInfo, 1, &m_Fence, nullptr, 0, nullptr, 0, nullptr }; + + const auto& vk = m_Device.GetDispatchTable(); + vk.QueueSubmit((VkQueue)commandQueue, 1, &submitInfo, VK_NULL_HANDLE); +} + +inline void FenceVK::Wait(uint64_t value) +{ + VkSemaphoreWaitInfo waitInfo = { VK_STRUCTURE_TYPE_SEMAPHORE_WAIT_INFO, nullptr, 0, 1, &m_Fence, &value }; + + const auto& vk = m_Device.GetDispatchTable(); + vk.WaitSemaphores((VkDevice)m_Device, &waitInfo, DEFAULT_TIMEOUT); +} + +#include "FenceVK.hpp" diff --git a/Source/VK/FenceVK.h b/Source/VK/FenceVK.h new file mode 100644 index 00000000..c8f98a41 --- /dev/null +++ b/Source/VK/FenceVK.h @@ -0,0 +1,47 @@ +/* +Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. + +NVIDIA CORPORATION and its licensors retain all intellectual property +and proprietary rights in and to this software, related documentation +and any modifications thereto. Any use, reproduction, disclosure or +distribution of this software and related documentation without an express +license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#pragma once + +namespace nri +{ + +struct DeviceVK; +struct CommandQueueVK; + +struct FenceVK +{ + inline FenceVK(DeviceVK& device) : + m_Device(device) + {} + + inline DeviceVK& GetDevice() const + { return m_Device; } + + ~FenceVK(); + + Result Create(uint64_t initialValue); + + //================================================================================================================ + // NRI + //================================================================================================================ + + void SetDebugName(const char* name); + uint64_t GetFenceValue() const; + void QueueSignal(CommandQueueVK& commandQueue, uint64_t value); + void QueueWait(CommandQueueVK& commandQueue, uint64_t value); + void Wait(uint64_t value); + +private: + DeviceVK& m_Device; + VkSemaphore m_Fence = VK_NULL_HANDLE; +}; + +} diff --git a/Source/VK/FenceVK.hpp b/Source/VK/FenceVK.hpp new file mode 100644 index 00000000..b2deb026 --- /dev/null +++ b/Source/VK/FenceVK.hpp @@ -0,0 +1,40 @@ +/* +Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. + +NVIDIA CORPORATION and its licensors retain all intellectual property +and proprietary rights in and to this software, related documentation +and any modifications thereto. Any use, reproduction, disclosure or +distribution of this software and related documentation without an express +license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#pragma region [ Core ] + +static uint64_t NRI_CALL GetFenceValue(Fence& fence) +{ + return ((FenceVK&)fence).GetFenceValue(); +} + +static void NRI_CALL QueueSignal(CommandQueue& commandQueue, Fence& fence, uint64_t value) +{ + return ((FenceVK&)fence).QueueSignal((CommandQueueVK&)commandQueue, value); +} + +static void NRI_CALL QueueWait(CommandQueue& commandQueue, Fence& fence, uint64_t value) +{ + return ((FenceVK&)fence).QueueWait((CommandQueueVK&)commandQueue, value); +} + +static void NRI_CALL Wait(Fence& fence, uint64_t value) +{ + ((FenceVK&)fence).Wait(value); +} + +static void NRI_CALL SetFenceDebugName(Fence& fence, const char* name) +{ + ((FenceVK&)fence).SetDebugName(name); +} + +#pragma endregion + +Define_Core_Fence_PartiallyFillFunctionTable(VK) diff --git a/Source/VK/FrameBufferVK.cpp b/Source/VK/FrameBufferVK.cpp index 16258818..66f37ca2 100644 --- a/Source/VK/FrameBufferVK.cpp +++ b/Source/VK/FrameBufferVK.cpp @@ -12,7 +12,6 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include "DescriptorVK.h" #include "TextureVK.h" #include "FrameBufferVK.h" -#include "DeviceVK.h" using namespace nri; @@ -223,7 +222,11 @@ Result FrameBufferVK::Create(const FrameBufferDesc& frameBufferDesc) return Result::SUCCESS; } -inline void FrameBufferVK::SetDebugName(const char* name) +//================================================================================================================ +// NRI +//================================================================================================================ + +void FrameBufferVK::SetDebugName(const char* name) { std::array handles; for (size_t i = 0; i < handles.size(); i++) @@ -233,5 +236,3 @@ inline void FrameBufferVK::SetDebugName(const char* name) m_Device.SetDebugNameToTrivialObject(VK_OBJECT_TYPE_RENDER_PASS, (uint64_t)m_RenderPass, name); m_Device.SetDebugNameToTrivialObject(VK_OBJECT_TYPE_RENDER_PASS, (uint64_t)m_RenderPassWithClear, name); } - -#include "FrameBufferVK.hpp" \ No newline at end of file diff --git a/Source/VK/FrameBufferVK.h b/Source/VK/FrameBufferVK.h index 4897c5fa..83e7eb19 100644 --- a/Source/VK/FrameBufferVK.h +++ b/Source/VK/FrameBufferVK.h @@ -12,78 +12,62 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct DeviceVK; - - struct FrameBufferVK - { - FrameBufferVK(DeviceVK& device); - ~FrameBufferVK(); - - DeviceVK& GetDevice() const; - Result Create(const FrameBufferDesc& frameBufferDesc); - VkFramebuffer GetHandle(uint32_t physicalDeviceIndex) const; - const VkRect2D& GetRenderArea() const; - uint32_t GetLayerNum() const; - VkRenderPass GetRenderPass(RenderPassBeginFlag renderPassBeginFlag) const; - uint32_t GetAttachmentNum() const; - void GetClearValues(VkClearValue* values) const; - - void SetDebugName(const char* name); - - private: - void FillDescriptionsAndFormats(const FrameBufferDesc& frameBufferDesc, VkAttachmentDescription* descriptions, VkFormat* formats); - Result SaveClearColors(const FrameBufferDesc& frameBufferDesc); - Result CreateRenderPass(const FrameBufferDesc& frameBufferDesc); - - static constexpr uint32_t ATTACHMENT_MAX_NUM = 8; - - std::array m_Handles = {}; - VkRenderPass m_RenderPassWithClear = VK_NULL_HANDLE; - VkRenderPass m_RenderPass = VK_NULL_HANDLE; - std::array m_ClearValues = {}; - VkRect2D m_RenderArea = {}; - uint32_t m_LayerNum = 0; - uint32_t m_AttachmentNum = 0; - DeviceVK& m_Device; - }; - - inline FrameBufferVK::FrameBufferVK(DeviceVK& device) : + +constexpr uint32_t ATTACHMENT_MAX_NUM = 8; + +struct DeviceVK; + +struct FrameBufferVK +{ + inline FrameBufferVK(DeviceVK& device) : m_Device(device) - { - } - - inline DeviceVK& FrameBufferVK::GetDevice() const - { - return m_Device; - } - - inline VkFramebuffer FrameBufferVK::GetHandle(uint32_t physicalDeviceIndex) const - { - return m_Handles[physicalDeviceIndex]; - } - - inline VkRenderPass FrameBufferVK::GetRenderPass(RenderPassBeginFlag renderPassBeginFlag) const - { - return (renderPassBeginFlag == RenderPassBeginFlag::SKIP_FRAME_BUFFER_CLEAR) ? m_RenderPass : m_RenderPassWithClear; - } - - inline uint32_t FrameBufferVK::GetLayerNum() const - { - return m_LayerNum; - } - - inline const VkRect2D& FrameBufferVK::GetRenderArea() const - { - return m_RenderArea; - } - - inline uint32_t FrameBufferVK::GetAttachmentNum() const - { - return m_AttachmentNum; - } - - inline void FrameBufferVK::GetClearValues(VkClearValue* values) const - { - memcpy(values, m_ClearValues.data(), m_AttachmentNum * sizeof(VkClearValue)); - } + {} + + inline DeviceVK& GetDevice() const + { return m_Device; } + + inline VkFramebuffer GetHandle(uint32_t physicalDeviceIndex) const + { return m_Handles[physicalDeviceIndex]; } + + inline VkRenderPass GetRenderPass(RenderPassBeginFlag renderPassBeginFlag) const + { return (renderPassBeginFlag == RenderPassBeginFlag::SKIP_FRAME_BUFFER_CLEAR) ? m_RenderPass : m_RenderPassWithClear; } + + inline uint32_t GetLayerNum() const + { return m_LayerNum; } + + inline const VkRect2D& GetRenderArea() const + { return m_RenderArea; } + + inline uint32_t GetAttachmentNum() const + { return m_AttachmentNum; } + + inline void GetClearValues(VkClearValue* values) const + { memcpy(values, m_ClearValues.data(), m_AttachmentNum * sizeof(VkClearValue)); } + + ~FrameBufferVK(); + + Result Create(const FrameBufferDesc& frameBufferDesc); + + //================================================================================================================ + // NRI + //================================================================================================================ + + void SetDebugName(const char* name); + +private: + void FillDescriptionsAndFormats(const FrameBufferDesc& frameBufferDesc, VkAttachmentDescription* descriptions, VkFormat* formats); + Result SaveClearColors(const FrameBufferDesc& frameBufferDesc); + Result CreateRenderPass(const FrameBufferDesc& frameBufferDesc); + +private: + DeviceVK& m_Device; + std::array m_Handles = {}; + VkRenderPass m_RenderPassWithClear = VK_NULL_HANDLE; + VkRenderPass m_RenderPass = VK_NULL_HANDLE; + std::array m_ClearValues = {}; + VkRect2D m_RenderArea = {}; + uint32_t m_LayerNum = 0; + uint32_t m_AttachmentNum = 0; +}; + } \ No newline at end of file diff --git a/Source/VK/FrameBufferVK.hpp b/Source/VK/FrameBufferVK.hpp deleted file mode 100644 index d0132e18..00000000 --- a/Source/VK/FrameBufferVK.hpp +++ /dev/null @@ -1,25 +0,0 @@ -/* -Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. - -NVIDIA CORPORATION and its licensors retain all intellectual property -and proprietary rights in and to this software, related documentation -and any modifications thereto. Any use, reproduction, disclosure or -distribution of this software and related documentation without an express -license agreement from NVIDIA CORPORATION is strictly prohibited. -*/ - -#pragma once - -#pragma region [ CoreInterface ] - -static void NRI_CALL SetFrameBufferDebugName(FrameBuffer& frameBuffer, const char* name) -{ - ((FrameBufferVK&)frameBuffer).SetDebugName(name); -} - -void FillFunctionTableFrameBufferVK(CoreInterface& coreInterface) -{ - coreInterface.SetFrameBufferDebugName = ::SetFrameBufferDebugName; -} - -#pragma endregion \ No newline at end of file diff --git a/Source/VK/MemoryVK.cpp b/Source/VK/MemoryVK.cpp index 97de0023..85cabda3 100644 --- a/Source/VK/MemoryVK.cpp +++ b/Source/VK/MemoryVK.cpp @@ -12,7 +12,6 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include "MemoryVK.h" #include "BufferVK.h" #include "TextureVK.h" -#include "DeviceVK.h" using namespace nri; @@ -250,7 +249,11 @@ Result MemoryVK::CreateDedicated(TextureVK& texture, uint32_t physicalDeviceMask return Result::SUCCESS; } -inline void MemoryVK::SetDebugName(const char* name) +//================================================================================================================ +// NRI +//================================================================================================================ + +void MemoryVK::SetDebugName(const char* name) { std::array handles; for (size_t i = 0; i < handles.size(); i++) @@ -258,5 +261,3 @@ inline void MemoryVK::SetDebugName(const char* name) m_Device.SetDebugNameToDeviceGroupObject(VK_OBJECT_TYPE_DEVICE_MEMORY, handles.data(), name); } - -#include "MemoryVK.hpp" \ No newline at end of file diff --git a/Source/VK/MemoryVK.h b/Source/VK/MemoryVK.h index c3a86d68..19eb760c 100644 --- a/Source/VK/MemoryVK.h +++ b/Source/VK/MemoryVK.h @@ -12,56 +12,48 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct DeviceVK; - struct BufferVK; - struct TextureVK; - struct MemoryVK - { - MemoryVK(DeviceVK& device); - ~MemoryVK(); +struct DeviceVK; +struct BufferVK; +struct TextureVK; - DeviceVK& GetDevice() const; - Result Create(uint32_t physicalDeviceMask, const MemoryType memoryType, uint64_t size); - Result Create(const MemoryVulkanDesc& memoryDesc); - Result CreateDedicated(BufferVK& buffer, uint32_t physicalDeviceMask); - Result CreateDedicated(TextureVK& texture, uint32_t physicalDeviceMask); - VkDeviceMemory GetHandle(uint32_t physicalDeviceIndex) const; - MemoryType GetType() const; - uint8_t* GetMappedMemory(uint32_t physicalDeviceIndex) const; +struct MemoryVK +{ + inline MemoryVK(DeviceVK& device) : + m_Device(device) + {} - void SetDebugName(const char* name); + inline VkDeviceMemory GetHandle(uint32_t physicalDeviceIndex) const + { return m_Handles[physicalDeviceIndex]; } - private: - std::array m_Handles = {}; - std::array m_MappedMemory = {}; - MemoryType m_Type = std::numeric_limits::max(); - DeviceVK& m_Device; - bool m_OwnsNativeObjects = false; - }; + inline DeviceVK& GetDevice() const + { return m_Device; } - inline MemoryVK::MemoryVK(DeviceVK& device) : - m_Device(device) - { - } + inline MemoryType GetType() const + { return m_Type; } + + inline uint8_t* GetMappedMemory(uint32_t physicalDeviceIndex) const + { return m_MappedMemory[physicalDeviceIndex]; } + + ~MemoryVK(); + + Result Create(uint32_t physicalDeviceMask, const MemoryType memoryType, uint64_t size); + Result Create(const MemoryVulkanDesc& memoryDesc); + Result CreateDedicated(BufferVK& buffer, uint32_t physicalDeviceMask); + Result CreateDedicated(TextureVK& texture, uint32_t physicalDeviceMask); - inline VkDeviceMemory MemoryVK::GetHandle(uint32_t physicalDeviceIndex) const - { - return m_Handles[physicalDeviceIndex]; - } + //================================================================================================================ + // NRI + //================================================================================================================ - inline DeviceVK& MemoryVK::GetDevice() const - { - return m_Device; - } + void SetDebugName(const char* name); - inline MemoryType MemoryVK::GetType() const - { - return m_Type; - } +private: + DeviceVK& m_Device; + std::array m_Handles = {}; + std::array m_MappedMemory = {}; + MemoryType m_Type = std::numeric_limits::max(); + bool m_OwnsNativeObjects = false; +}; - inline uint8_t* MemoryVK::GetMappedMemory(uint32_t physicalDeviceIndex) const - { - return m_MappedMemory[physicalDeviceIndex]; - } } \ No newline at end of file diff --git a/Source/VK/MemoryVK.hpp b/Source/VK/MemoryVK.hpp deleted file mode 100644 index ecfd1510..00000000 --- a/Source/VK/MemoryVK.hpp +++ /dev/null @@ -1,25 +0,0 @@ -/* -Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. - -NVIDIA CORPORATION and its licensors retain all intellectual property -and proprietary rights in and to this software, related documentation -and any modifications thereto. Any use, reproduction, disclosure or -distribution of this software and related documentation without an express -license agreement from NVIDIA CORPORATION is strictly prohibited. -*/ - -#pragma once - -#pragma region [ CoreInterface ] - -static void NRI_CALL SetMemoryDebugName(Memory& memory, const char* name) -{ - ((MemoryVK&)memory).SetDebugName(name); -} - -void FillFunctionTableMemoryVK(CoreInterface& coreInterface) -{ - coreInterface.SetMemoryDebugName = ::SetMemoryDebugName; -} - -#pragma endregion \ No newline at end of file diff --git a/Source/VK/PipelineLayoutVK.cpp b/Source/VK/PipelineLayoutVK.cpp index a31b9d4b..ea2e11cc 100644 --- a/Source/VK/PipelineLayoutVK.cpp +++ b/Source/VK/PipelineLayoutVK.cpp @@ -11,22 +11,12 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include "SharedVK.h" #include "PipelineLayoutVK.h" #include "DescriptorVK.h" -#include "DeviceVK.h" using namespace nri; -PipelineLayoutVK::PipelineLayoutVK(DeviceVK& device) : - m_RuntimeBindingInfo(device.GetStdAllocator()), - m_DescriptorSetLayouts(device.GetStdAllocator()), - m_DescriptorSetSpaces(device.GetStdAllocator()), - m_Device(device) -{ -} - PipelineLayoutVK::~PipelineLayoutVK() { const auto& vk = m_Device.GetDispatchTable(); - const auto allocationCallbacks = m_Device.GetAllocationCallbacks(); if (m_Handle) @@ -110,11 +100,6 @@ Result PipelineLayoutVK::Create(const PipelineLayoutDesc& pipelineLayoutDesc) return Result::SUCCESS; } -inline void PipelineLayoutVK::SetDebugName(const char* name) -{ - m_Device.SetDebugNameToTrivialObject(VK_OBJECT_TYPE_PIPELINE_LAYOUT, (uint64_t)m_Handle, name); -} - void PipelineLayoutVK::FillBindingOffsets(bool ignoreGlobalSPIRVOffsets, uint32_t* bindingOffsets) { SPIRVBindingOffsets spirvBindingOffsets; @@ -339,4 +324,11 @@ RuntimeBindingInfo::RuntimeBindingInfo(StdAllocator& allocator) : { } -#include "PipelineLayoutVK.hpp" \ No newline at end of file +//================================================================================================================ +// NRI +//================================================================================================================ + +void PipelineLayoutVK::SetDebugName(const char* name) +{ + m_Device.SetDebugNameToTrivialObject(VK_OBJECT_TYPE_PIPELINE_LAYOUT, (uint64_t)m_Handle, name); +} diff --git a/Source/VK/PipelineLayoutVK.h b/Source/VK/PipelineLayoutVK.h index c9de3869..313ec7bf 100644 --- a/Source/VK/PipelineLayoutVK.h +++ b/Source/VK/PipelineLayoutVK.h @@ -12,89 +12,84 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct DeviceVK; - struct DescriptorVK; - - struct PushConstantRangeBindingDesc - { - VkShaderStageFlags flags; - uint32_t offset; - }; - - struct RuntimeBindingInfo - { - RuntimeBindingInfo(StdAllocator& allocator); - - Vector hasVariableDescriptorNum; - Vector descriptorSetRangeDescs; - Vector dynamicConstantBufferDescs; - Vector descriptorSetDescs; - Vector pushConstantDescs; - Vector pushConstantBindings; - }; - - struct PipelineLayoutVK - { - PipelineLayoutVK(DeviceVK& device); - ~PipelineLayoutVK(); - - operator VkPipelineLayout() const; - DeviceVK& GetDevice() const; - Result Create(const PipelineLayoutDesc& pipelineLayoutDesc); - const RuntimeBindingInfo& GetRuntimeBindingInfo() const; - VkDescriptorSetLayout GetDescriptorSetLayout(uint32_t index) const; - VkPipelineBindPoint GetPipelineBindPoint() const; - uint32_t GetDescriptorSetSpace(uint32_t setIndexInPipelineLayout) const; - - void SetDebugName(const char* name); - - private: - void FillBindingOffsets(bool ignoreGlobalSPIRVOffsets, uint32_t* bindingOffsets); - VkDescriptorSetLayout CreateSetLayout(const DescriptorSetDesc& descriptorSetDesc, const uint32_t* bindingOffsets); - - void FillDescriptorBindings(const DescriptorSetDesc& descriptorSetDesc, const uint32_t* bindingOffsets, - VkDescriptorSetLayoutBinding*& bindings, VkDescriptorBindingFlagsEXT*& bindingFlags) const; - void FillDynamicConstantBufferBindings(const DescriptorSetDesc& descriptorSetDesc, const uint32_t* bindingOffsets, - VkDescriptorSetLayoutBinding*& bindings, VkDescriptorBindingFlagsEXT*& bindingFlags) const; - - void FillPushConstantRanges(const PipelineLayoutDesc& pipelineLayoutDesc, VkPushConstantRange* pushConstantRanges) const; - void FillRuntimeBindingInfo(const PipelineLayoutDesc& pipelineLayoutDesc, const uint32_t* bindingOffsets); - - VkPipelineLayout m_Handle = VK_NULL_HANDLE; - VkPipelineBindPoint m_PipelineBindPoint = VK_PIPELINE_BIND_POINT_MAX_ENUM; - RuntimeBindingInfo m_RuntimeBindingInfo; - Vector m_DescriptorSetLayouts; - Vector m_DescriptorSetSpaces; - DeviceVK& m_Device; - }; - - inline PipelineLayoutVK::operator VkPipelineLayout() const - { - return m_Handle; - } - - inline DeviceVK& PipelineLayoutVK::GetDevice() const - { - return m_Device; - } - - inline const RuntimeBindingInfo& PipelineLayoutVK::GetRuntimeBindingInfo() const - { - return m_RuntimeBindingInfo; - } - - inline VkDescriptorSetLayout PipelineLayoutVK::GetDescriptorSetLayout(uint32_t index) const - { - return m_DescriptorSetLayouts[index]; - } - - inline VkPipelineBindPoint PipelineLayoutVK::GetPipelineBindPoint() const - { - return m_PipelineBindPoint; - } - - inline uint32_t PipelineLayoutVK::GetDescriptorSetSpace(uint32_t setIndexInPipelineLayout) const - { - return m_DescriptorSetSpaces[setIndexInPipelineLayout]; - } + +struct DeviceVK; +struct DescriptorVK; + +struct PushConstantRangeBindingDesc +{ + VkShaderStageFlags flags; + uint32_t offset; +}; + +struct RuntimeBindingInfo +{ + RuntimeBindingInfo(StdAllocator& allocator); + + Vector hasVariableDescriptorNum; + Vector descriptorSetRangeDescs; + Vector dynamicConstantBufferDescs; + Vector descriptorSetDescs; + Vector pushConstantDescs; + Vector pushConstantBindings; +}; + +struct PipelineLayoutVK +{ + inline PipelineLayoutVK(DeviceVK& device) : + m_Device(device) + , m_RuntimeBindingInfo(device.GetStdAllocator()) + , m_DescriptorSetLayouts(device.GetStdAllocator()) + , m_DescriptorSetSpaces(device.GetStdAllocator()) + {} + + inline operator VkPipelineLayout() const + { return m_Handle; } + + inline DeviceVK& GetDevice() const + { return m_Device; } + + inline const RuntimeBindingInfo& GetRuntimeBindingInfo() const + { return m_RuntimeBindingInfo; } + + inline VkDescriptorSetLayout GetDescriptorSetLayout(uint32_t index) const + { return m_DescriptorSetLayouts[index]; } + + inline VkPipelineBindPoint GetPipelineBindPoint() const + { return m_PipelineBindPoint; } + + inline uint32_t GetDescriptorSetSpace(uint32_t setIndexInPipelineLayout) const + { return m_DescriptorSetSpaces[setIndexInPipelineLayout]; } + + ~PipelineLayoutVK(); + + Result Create(const PipelineLayoutDesc& pipelineLayoutDesc); + + //================================================================================================================ + // NRI + //================================================================================================================ + + void SetDebugName(const char* name); + +private: + void FillBindingOffsets(bool ignoreGlobalSPIRVOffsets, uint32_t* bindingOffsets); + VkDescriptorSetLayout CreateSetLayout(const DescriptorSetDesc& descriptorSetDesc, const uint32_t* bindingOffsets); + + void FillDescriptorBindings(const DescriptorSetDesc& descriptorSetDesc, const uint32_t* bindingOffsets, + VkDescriptorSetLayoutBinding*& bindings, VkDescriptorBindingFlagsEXT*& bindingFlags) const; + void FillDynamicConstantBufferBindings(const DescriptorSetDesc& descriptorSetDesc, const uint32_t* bindingOffsets, + VkDescriptorSetLayoutBinding*& bindings, VkDescriptorBindingFlagsEXT*& bindingFlags) const; + + void FillPushConstantRanges(const PipelineLayoutDesc& pipelineLayoutDesc, VkPushConstantRange* pushConstantRanges) const; + void FillRuntimeBindingInfo(const PipelineLayoutDesc& pipelineLayoutDesc, const uint32_t* bindingOffsets); + +private: + DeviceVK& m_Device; + VkPipelineLayout m_Handle = VK_NULL_HANDLE; + VkPipelineBindPoint m_PipelineBindPoint = VK_PIPELINE_BIND_POINT_MAX_ENUM; + RuntimeBindingInfo m_RuntimeBindingInfo; + Vector m_DescriptorSetLayouts; + Vector m_DescriptorSetSpaces; +}; + } \ No newline at end of file diff --git a/Source/VK/PipelineLayoutVK.hpp b/Source/VK/PipelineLayoutVK.hpp deleted file mode 100644 index 6f01d280..00000000 --- a/Source/VK/PipelineLayoutVK.hpp +++ /dev/null @@ -1,25 +0,0 @@ -/* -Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. - -NVIDIA CORPORATION and its licensors retain all intellectual property -and proprietary rights in and to this software, related documentation -and any modifications thereto. Any use, reproduction, disclosure or -distribution of this software and related documentation without an express -license agreement from NVIDIA CORPORATION is strictly prohibited. -*/ - -#pragma once - -#pragma region [ CoreInterface ] - -static void NRI_CALL SetPipelineLayoutDebugName(PipelineLayout& pipelineLayout, const char* name) -{ - ((PipelineLayoutVK&)pipelineLayout).SetDebugName(name); -} - -void FillFunctionTablePipelineLayoutVK(CoreInterface& coreInterface) -{ - coreInterface.SetPipelineLayoutDebugName = ::SetPipelineLayoutDebugName; -} - -#pragma endregion \ No newline at end of file diff --git a/Source/VK/PipelineVK.cpp b/Source/VK/PipelineVK.cpp index d615023d..a8d119e4 100644 --- a/Source/VK/PipelineVK.cpp +++ b/Source/VK/PipelineVK.cpp @@ -11,15 +11,9 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include "SharedVK.h" #include "PipelineVK.h" #include "PipelineLayoutVK.h" -#include "DeviceVK.h" using namespace nri; -PipelineVK::PipelineVK(DeviceVK& device) : - m_Device(device) -{ -} - PipelineVK::~PipelineVK() { if (!m_OwnsNativeObjects) @@ -293,24 +287,6 @@ Result PipelineVK::CreateCompute(NRIVkPipeline vkPipeline) return Result::SUCCESS; } -inline void PipelineVK::SetDebugName(const char* name) -{ - m_Device.SetDebugNameToTrivialObject(VK_OBJECT_TYPE_PIPELINE, (uint64_t)m_Handle, name); -} - -inline Result PipelineVK::WriteShaderGroupIdentifiers(uint32_t baseShaderGroupIndex, uint32_t shaderGroupNum, void* buffer) const -{ - const size_t dataSize = (size_t)(shaderGroupNum * m_Device.GetDesc().rayTracingShaderGroupIdentifierSize); - - const auto& vk = m_Device.GetDispatchTable(); - const VkResult result = vk.GetRayTracingShaderGroupHandlesKHR(m_Device, m_Handle, baseShaderGroupIndex, shaderGroupNum, dataSize, buffer); - - RETURN_ON_FAILURE(m_Device.GetLog(), result == VK_SUCCESS, GetReturnCode(result), - "Can't get shader group identifiers: vkGetRayTracingShaderGroupHandlesKHR returned %d.", (int32_t)result); - - return Result::SUCCESS; -} - Result PipelineVK::SetupShaderStage(VkPipelineShaderStageCreateInfo& stage, const ShaderDesc& shaderDesc, VkShaderModule*& modules) { const VkShaderModuleCreateInfo moduleInfo = { @@ -641,4 +617,26 @@ void PipelineVK::FillGroupIndices(const RayTracingPipelineDesc& rayTracingPipeli } } +//================================================================================================================ +// NRI +//================================================================================================================ + +void PipelineVK::SetDebugName(const char* name) +{ + m_Device.SetDebugNameToTrivialObject(VK_OBJECT_TYPE_PIPELINE, (uint64_t)m_Handle, name); +} + +inline Result PipelineVK::WriteShaderGroupIdentifiers(uint32_t baseShaderGroupIndex, uint32_t shaderGroupNum, void* buffer) const +{ + const size_t dataSize = (size_t)(shaderGroupNum * m_Device.GetDesc().rayTracingShaderGroupIdentifierSize); + + const auto& vk = m_Device.GetDispatchTable(); + const VkResult result = vk.GetRayTracingShaderGroupHandlesKHR(m_Device, m_Handle, baseShaderGroupIndex, shaderGroupNum, dataSize, buffer); + + RETURN_ON_FAILURE(m_Device.GetLog(), result == VK_SUCCESS, GetReturnCode(result), + "Can't get shader group identifiers: vkGetRayTracingShaderGroupHandlesKHR returned %d.", (int32_t)result); + + return Result::SUCCESS; +} + #include "PipelineVK.hpp" \ No newline at end of file diff --git a/Source/VK/PipelineVK.h b/Source/VK/PipelineVK.h index f39f460f..a07e08d3 100644 --- a/Source/VK/PipelineVK.h +++ b/Source/VK/PipelineVK.h @@ -12,59 +12,60 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct DeviceVK; - struct PipelineLayoutVK; - struct PipelineVK - { - PipelineVK(DeviceVK& device); - ~PipelineVK(); +struct DeviceVK; +struct PipelineLayoutVK; - operator VkPipeline() const; - DeviceVK& GetDevice() const; - VkPipelineBindPoint GetBindPoint() const; - Result Create(const GraphicsPipelineDesc& graphicsPipelineDesc); - Result Create(const ComputePipelineDesc& computePipelineDesc); - Result Create(const RayTracingPipelineDesc& rayTracingPipelineDesc); - Result CreateGraphics(NRIVkPipeline vkPipeline); - Result CreateCompute(NRIVkPipeline vkPipeline); +struct PipelineVK +{ + inline PipelineVK(DeviceVK& device) : + m_Device(device) + {} + + inline operator VkPipeline() const + { return m_Handle; } + + inline DeviceVK& GetDevice() const + { return m_Device; } + + inline VkPipelineBindPoint GetBindPoint() const + { return m_BindPoint; } + + ~PipelineVK(); - void SetDebugName(const char* name); - Result WriteShaderGroupIdentifiers(uint32_t baseShaderGroupIndex, uint32_t shaderGroupNum, void* buffer) const; + Result Create(const GraphicsPipelineDesc& graphicsPipelineDesc); + Result Create(const ComputePipelineDesc& computePipelineDesc); + Result Create(const RayTracingPipelineDesc& rayTracingPipelineDesc); + Result CreateGraphics(NRIVkPipeline vkPipeline); + Result CreateCompute(NRIVkPipeline vkPipeline); - private: - Result CreateRenderPass(const OutputMergerDesc* outputMerger, const RasterizationDesc* rasterizationDesc); - void FillVertexInputState(const GraphicsPipelineDesc& graphicsPipelineDesc, VkPipelineVertexInputStateCreateInfo& state) const; - void FillInputAssemblyState(const GraphicsPipelineDesc& graphicsPipelineDesc, VkPipelineInputAssemblyStateCreateInfo& state) const; - void FillTessellationState(const GraphicsPipelineDesc& graphicsPipelineDesc, VkPipelineTessellationStateCreateInfo& state) const; - void FillViewportState(const GraphicsPipelineDesc& graphicsPipelineDesc, VkPipelineViewportStateCreateInfo& state); - void FillRasterizationState(const GraphicsPipelineDesc& graphicsPipelineDesc, VkPipelineRasterizationStateCreateInfo& state, VkPipelineRasterizationConservativeStateCreateInfoEXT& conservativeRasterState) const; - void FillMultisampleState(const GraphicsPipelineDesc& graphicsPipelineDesc, VkPipelineMultisampleStateCreateInfo& state) const; - void FillDepthStencilState(const GraphicsPipelineDesc& graphicsPipelineDesc, VkPipelineDepthStencilStateCreateInfo& state) const; - void FillColorBlendState(const GraphicsPipelineDesc& graphicsPipelineDesc, VkPipelineColorBlendStateCreateInfo& state) const; - void FillDynamicState(VkPipelineDynamicStateCreateInfo& state) const; - Result SetupShaderStage(VkPipelineShaderStageCreateInfo& stage, const ShaderDesc& shaderDesc, VkShaderModule*& modules); - void FillGroupIndices(const RayTracingPipelineDesc& rayTracingPipelineDesc, uint32_t* groupIndices); + //================================================================================================================ + // NRI + //================================================================================================================ - VkPipeline m_Handle = VK_NULL_HANDLE; - VkPipelineBindPoint m_BindPoint = (VkPipelineBindPoint)0; - VkRenderPass m_RenderPass = VK_NULL_HANDLE; - DeviceVK& m_Device; - bool m_OwnsNativeObjects = false; - }; + void SetDebugName(const char* name); + Result WriteShaderGroupIdentifiers(uint32_t baseShaderGroupIndex, uint32_t shaderGroupNum, void* buffer) const; - inline PipelineVK::operator VkPipeline() const - { - return m_Handle; - } +private: + Result SetupShaderStage(VkPipelineShaderStageCreateInfo& stage, const ShaderDesc& shaderDesc, VkShaderModule*& modules); + Result CreateRenderPass(const OutputMergerDesc* outputMerger, const RasterizationDesc* rasterizationDesc); + void FillVertexInputState(const GraphicsPipelineDesc& graphicsPipelineDesc, VkPipelineVertexInputStateCreateInfo& state) const; + void FillInputAssemblyState(const GraphicsPipelineDesc& graphicsPipelineDesc, VkPipelineInputAssemblyStateCreateInfo& state) const; + void FillTessellationState(const GraphicsPipelineDesc& graphicsPipelineDesc, VkPipelineTessellationStateCreateInfo& state) const; + void FillViewportState(const GraphicsPipelineDesc& graphicsPipelineDesc, VkPipelineViewportStateCreateInfo& state); + void FillRasterizationState(const GraphicsPipelineDesc& graphicsPipelineDesc, VkPipelineRasterizationStateCreateInfo& state, VkPipelineRasterizationConservativeStateCreateInfoEXT& conservativeRasterState) const; + void FillMultisampleState(const GraphicsPipelineDesc& graphicsPipelineDesc, VkPipelineMultisampleStateCreateInfo& state) const; + void FillDepthStencilState(const GraphicsPipelineDesc& graphicsPipelineDesc, VkPipelineDepthStencilStateCreateInfo& state) const; + void FillColorBlendState(const GraphicsPipelineDesc& graphicsPipelineDesc, VkPipelineColorBlendStateCreateInfo& state) const; + void FillDynamicState(VkPipelineDynamicStateCreateInfo& state) const; + void FillGroupIndices(const RayTracingPipelineDesc& rayTracingPipelineDesc, uint32_t* groupIndices); - inline DeviceVK& PipelineVK::GetDevice() const - { - return m_Device; - } +private: + DeviceVK& m_Device; + VkPipeline m_Handle = VK_NULL_HANDLE; + VkPipelineBindPoint m_BindPoint = (VkPipelineBindPoint)0; + VkRenderPass m_RenderPass = VK_NULL_HANDLE; + bool m_OwnsNativeObjects = false; +}; - inline VkPipelineBindPoint PipelineVK::GetBindPoint() const - { - return m_BindPoint; - } } \ No newline at end of file diff --git a/Source/VK/PipelineVK.hpp b/Source/VK/PipelineVK.hpp index 4933174d..5d94ca8f 100644 --- a/Source/VK/PipelineVK.hpp +++ b/Source/VK/PipelineVK.hpp @@ -8,23 +8,7 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma once - -#pragma region [ CoreInterface ] - -static void NRI_CALL SetPipelineDebugName(Pipeline& pipeline, const char* name) -{ - ((PipelineVK&)pipeline).SetDebugName(name); -} - -void FillFunctionTablePipelineVK(CoreInterface& coreInterface) -{ - coreInterface.SetPipelineDebugName = ::SetPipelineDebugName; -} - -#pragma endregion - -#pragma region [ RayTracingInterface ] +#pragma region [ RayTracing ] static Result NRI_CALL WriteShaderGroupIdentifiers(const Pipeline& pipeline, uint32_t baseShaderGroupIndex, uint32_t shaderGroupNum, void* buffer) { diff --git a/Source/VK/QueryPoolVK.cpp b/Source/VK/QueryPoolVK.cpp index f3d6b82c..3e081e3f 100644 --- a/Source/VK/QueryPoolVK.cpp +++ b/Source/VK/QueryPoolVK.cpp @@ -10,7 +10,6 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include "SharedVK.h" #include "QueryPoolVK.h" -#include "DeviceVK.h" using namespace nri; @@ -81,6 +80,10 @@ Result QueryPoolVK::Create(const QueryPoolVulkanDesc& queryPoolDesc) return Result::SUCCESS; } +//================================================================================================================ +// NRI +//================================================================================================================ + inline void QueryPoolVK::SetDebugName(const char* name) { std::array handles; @@ -90,7 +93,7 @@ inline void QueryPoolVK::SetDebugName(const char* name) m_Device.SetDebugNameToDeviceGroupObject(VK_OBJECT_TYPE_QUERY_POOL, handles.data(), name); } -uint32_t QueryPoolVK::GetQuerySize() const +inline uint32_t QueryPoolVK::GetQuerySize() const { const uint32_t highestBitInPipelineStatsBits = 11; diff --git a/Source/VK/QueryPoolVK.h b/Source/VK/QueryPoolVK.h index 81e3b94f..7e15cb5b 100644 --- a/Source/VK/QueryPoolVK.h +++ b/Source/VK/QueryPoolVK.h @@ -12,53 +12,45 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct DeviceVK; - struct QueryPoolVK - { - QueryPoolVK(DeviceVK& device); - ~QueryPoolVK(); +struct DeviceVK; - VkQueryPool GetHandle(uint32_t physicalDeviceIndex) const; - DeviceVK& GetDevice() const; - uint32_t GetStride() const; - VkQueryType GetType() const; - Result Create(const QueryPoolDesc& queryPoolDesc); - Result Create(const QueryPoolVulkanDesc& queryPoolDesc); +struct QueryPoolVK +{ + inline QueryPoolVK(DeviceVK& device) : + m_Device(device) + {} - void SetDebugName(const char* name); - uint32_t GetQuerySize() const; + inline VkQueryPool GetHandle(uint32_t physicalDeviceIndex) const + { return m_Handles[physicalDeviceIndex]; } - private: - std::array m_Handles = {}; - uint32_t m_Stride = 0; - VkQueryType m_Type = (VkQueryType)0; - DeviceVK& m_Device; - bool m_OwnsNativeObjects = false; - }; + inline DeviceVK& GetDevice() const + { return m_Device; } - inline QueryPoolVK::QueryPoolVK(DeviceVK& device) : - m_Device(device) - { - } + inline uint32_t GetStride() const + { return m_Stride; } + + inline VkQueryType GetType() const + { return m_Type; } + + ~QueryPoolVK(); + + Result Create(const QueryPoolDesc& queryPoolDesc); + Result Create(const QueryPoolVulkanDesc& queryPoolDesc); - inline VkQueryPool QueryPoolVK::GetHandle(uint32_t physicalDeviceIndex) const - { - return m_Handles[physicalDeviceIndex]; - } + //================================================================================================================ + // NRI + //================================================================================================================ - inline DeviceVK& QueryPoolVK::GetDevice() const - { - return m_Device; - } + void SetDebugName(const char* name); + uint32_t GetQuerySize() const; - inline uint32_t QueryPoolVK::GetStride() const - { - return m_Stride; - } +private: + DeviceVK& m_Device; + std::array m_Handles = {}; + uint32_t m_Stride = 0; + VkQueryType m_Type = (VkQueryType)0; + bool m_OwnsNativeObjects = false; +}; - inline VkQueryType QueryPoolVK::GetType() const - { - return m_Type; - } } \ No newline at end of file diff --git a/Source/VK/QueryPoolVK.hpp b/Source/VK/QueryPoolVK.hpp index b60f8c59..4550dfac 100644 --- a/Source/VK/QueryPoolVK.hpp +++ b/Source/VK/QueryPoolVK.hpp @@ -8,9 +8,7 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma once - -#pragma region [ CoreInterface ] +#pragma region [ Core ] static void NRI_CALL SetQueryPoolDebugName(QueryPool& queryPool, const char* name) { @@ -22,10 +20,6 @@ static uint32_t NRI_CALL GetQuerySize(const QueryPool& queryPool) return ((QueryPoolVK&)queryPool).GetQuerySize(); } -void FillFunctionTableQueryPoolVK(CoreInterface& coreInterface) -{ - coreInterface.SetQueryPoolDebugName = ::SetQueryPoolDebugName; - coreInterface.GetQuerySize = ::GetQuerySize; -} +#pragma endregion -#pragma endregion \ No newline at end of file +Define_Core_QueryPool_PartiallyFillFunctionTable(VK) diff --git a/Source/VK/QueueSemaphoreVK.cpp b/Source/VK/QueueSemaphoreVK.cpp deleted file mode 100644 index dfc4b05d..00000000 --- a/Source/VK/QueueSemaphoreVK.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/* -Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. - -NVIDIA CORPORATION and its licensors retain all intellectual property -and proprietary rights in and to this software, related documentation -and any modifications thereto. Any use, reproduction, disclosure or -distribution of this software and related documentation without an express -license agreement from NVIDIA CORPORATION is strictly prohibited. -*/ - -#include "SharedVK.h" -#include "QueueSemaphoreVK.h" -#include "DeviceVK.h" - -using namespace nri; - -QueueSemaphoreVK::~QueueSemaphoreVK() -{ - const auto& vk = m_Device.GetDispatchTable(); - if (m_Handle != VK_NULL_HANDLE && m_OwnsNativeObjects) - vk.DestroySemaphore(m_Device, m_Handle, m_Device.GetAllocationCallbacks()); -} - -Result QueueSemaphoreVK::Create() -{ - m_OwnsNativeObjects = true; - - const VkSemaphoreCreateInfo semaphoreInfo = { - VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, - nullptr, - (VkSemaphoreCreateFlags)0 - }; - - const auto& vk = m_Device.GetDispatchTable(); - const VkResult result = vk.CreateSemaphore(m_Device, &semaphoreInfo, m_Device.GetAllocationCallbacks(), &m_Handle); - - RETURN_ON_FAILURE(m_Device.GetLog(), result == VK_SUCCESS, GetReturnCode(result), - "Can't create a semaphore: vkCreateSemaphore returned %d.", (int32_t)result); - - return Result::SUCCESS; -} - -Result QueueSemaphoreVK::Create(NRIVkSemaphore vkSemaphore) -{ - m_OwnsNativeObjects = false; - m_Handle = (VkSemaphore)vkSemaphore; - - return Result::SUCCESS; -} - -inline void QueueSemaphoreVK::SetDebugName(const char* name) -{ - m_Device.SetDebugNameToTrivialObject(VK_OBJECT_TYPE_SEMAPHORE, (uint64_t)m_Handle, name); -} - -#include "QueueSemaphoreVK.hpp" \ No newline at end of file diff --git a/Source/VK/QueueSemaphoreVK.h b/Source/VK/QueueSemaphoreVK.h deleted file mode 100644 index 60320202..00000000 --- a/Source/VK/QueueSemaphoreVK.h +++ /dev/null @@ -1,49 +0,0 @@ -/* -Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. - -NVIDIA CORPORATION and its licensors retain all intellectual property -and proprietary rights in and to this software, related documentation -and any modifications thereto. Any use, reproduction, disclosure or -distribution of this software and related documentation without an express -license agreement from NVIDIA CORPORATION is strictly prohibited. -*/ - -#pragma once - -namespace nri -{ - struct DeviceVK; - - struct QueueSemaphoreVK - { - QueueSemaphoreVK(DeviceVK& device); - ~QueueSemaphoreVK(); - - operator VkSemaphore() const; - DeviceVK& GetDevice() const; - Result Create(); - Result Create(NRIVkSemaphore vkSemaphore); - - void SetDebugName(const char* name); - - private: - VkSemaphore m_Handle = VK_NULL_HANDLE; - DeviceVK& m_Device; - bool m_OwnsNativeObjects = false; - }; - - inline QueueSemaphoreVK::QueueSemaphoreVK(DeviceVK& device) : - m_Device(device) - { - } - - inline QueueSemaphoreVK::operator VkSemaphore() const - { - return m_Handle; - } - - inline DeviceVK& QueueSemaphoreVK::GetDevice() const - { - return m_Device; - } -} \ No newline at end of file diff --git a/Source/VK/SharedVK.h b/Source/VK/SharedVK.h index e2dfc79d..21ebb4e4 100644 --- a/Source/VK/SharedVK.h +++ b/Source/VK/SharedVK.h @@ -19,11 +19,7 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include "DispatchTable.h" #include "ConversionVK.h" -template< typename HandleType, typename ImplType, typename NRIType > -constexpr HandleType GetVulkanHandle(NRIType* object, uint32_t physicalDeviceIndex) -{ - return (object != nullptr) ? (*(ImplType*)object).GetHandle(physicalDeviceIndex) : HandleType(VK_NULL_HANDLE); -} +constexpr uint64_t DEFAULT_TIMEOUT = uint64_t(-1); struct MemoryTypeInfo { @@ -40,7 +36,11 @@ union MemoryTypeUnpack MemoryTypeInfo info; }; +template< typename HandleType, typename ImplType, typename NRIType > +constexpr HandleType GetVulkanHandle(NRIType* object, uint32_t physicalDeviceIndex) +{ return (object != nullptr) ? (*(ImplType*)object).GetHandle(physicalDeviceIndex) : HandleType(VK_NULL_HANDLE); } + constexpr bool IsHostVisibleMemory(nri::MemoryLocation location) -{ - return location > nri::MemoryLocation::DEVICE; -} +{ return location > nri::MemoryLocation::DEVICE; } + +#include "DeviceVK.h" diff --git a/Source/VK/SwapChainVK.cpp b/Source/VK/SwapChainVK.cpp index 252c7788..685e1213 100644 --- a/Source/VK/SwapChainVK.cpp +++ b/Source/VK/SwapChainVK.cpp @@ -11,9 +11,7 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include "SharedVK.h" #include "SwapChainVK.h" #include "CommandQueueVK.h" -#include "QueueSemaphoreVK.h" #include "TextureVK.h" -#include "DeviceVK.h" using namespace nri; @@ -39,6 +37,9 @@ SwapChainVK::~SwapChainVK() if (m_Surface != VK_NULL_HANDLE) vk.DestroySurfaceKHR(m_Device, m_Surface, m_Device.GetAllocationCallbacks()); + + if (m_Semaphore != VK_NULL_HANDLE) + vk.DestroySemaphore(m_Device, m_Semaphore, m_Device.GetAllocationCallbacks()); } Result SwapChainVK::CreateSurface(const SwapChainDesc& swapChainDesc) @@ -115,15 +116,20 @@ Result SwapChainVK::CreateSurface(const SwapChainDesc& swapChainDesc) Result SwapChainVK::Create(const SwapChainDesc& swapChainDesc) { - m_CommandQueue = (CommandQueueVK*)swapChainDesc.commandQueue; + const auto& vk = m_Device.GetDispatchTable(); - { - const Result result = CreateSurface(swapChainDesc); - if (result != Result::SUCCESS) - return result; - } + VkSemaphoreTypeCreateInfo timelineCreateInfo = { VK_STRUCTURE_TYPE_SEMAPHORE_TYPE_CREATE_INFO, nullptr, VK_SEMAPHORE_TYPE_BINARY, 0 }; + VkSemaphoreCreateInfo createInfo = { VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, &timelineCreateInfo, 0 }; + VkResult result = vk.CreateSemaphore((VkDevice)m_Device, &createInfo, m_Device.GetAllocationCallbacks(), &m_Semaphore); - const auto& vk = m_Device.GetDispatchTable(); + RETURN_ON_FAILURE(m_Device.GetLog(), result == VK_SUCCESS, GetReturnCode(result), + "Can't create a semaphore: vk.CreateSemaphore returned %d.", (int32_t)result); + + m_CommandQueue = (CommandQueueVK*)swapChainDesc.commandQueue; + + const Result nriResult = CreateSurface(swapChainDesc); + if (nriResult != Result::SUCCESS) + return nriResult; VkBool32 supported = VK_FALSE; vk.GetPhysicalDeviceSurfaceSupportKHR(m_Device, m_CommandQueue->GetFamilyIndex(), m_Surface, &supported); @@ -135,7 +141,7 @@ Result SwapChainVK::Create(const SwapChainDesc& swapChainDesc) } VkSurfaceCapabilitiesKHR capabilites = {}; - VkResult result = vk.GetPhysicalDeviceSurfaceCapabilitiesKHR(m_Device, m_Surface, &capabilites); + result = vk.GetPhysicalDeviceSurfaceCapabilitiesKHR(m_Device, m_Surface, &capabilites); RETURN_ON_FAILURE(m_Device.GetLog(), result == VK_SUCCESS, GetReturnCode(result), "Can't get physical device surface capabilities: vkGetPhysicalDeviceSurfaceCapabilitiesKHR returned %d.", (int32_t)result); @@ -242,6 +248,10 @@ Result SwapChainVK::Create(const SwapChainDesc& swapChainDesc) return Result::SUCCESS; } +//================================================================================================================ +// NRI +//================================================================================================================ + inline void SwapChainVK::SetDebugName(const char* name) { m_Device.SetDebugNameToTrivialObject(VK_OBJECT_TYPE_SURFACE_KHR, (uint64_t)m_Surface, name); @@ -255,39 +265,36 @@ inline Texture* const* SwapChainVK::GetTextures(uint32_t& textureNum, Format& fo return (Texture* const*)m_Textures.data(); } -inline uint32_t SwapChainVK::AcquireNextTexture(QueueSemaphore& textureReadyForRender) +inline uint32_t SwapChainVK::AcquireNextTexture() { - const uint64_t timeout = 5000000000; // 5 seconds - m_TextureIndex = std::numeric_limits::max(); - const auto& vk = m_Device.GetDispatchTable(); - const VkResult result = vk.AcquireNextImageKHR(m_Device, m_Handle, timeout, *(QueueSemaphoreVK*)&textureReadyForRender, - VK_NULL_HANDLE, &m_TextureIndex); + const VkResult result = vk.AcquireNextImageKHR(m_Device, m_Handle, DEFAULT_TIMEOUT, m_Semaphore, VK_NULL_HANDLE, &m_TextureIndex); RETURN_ON_FAILURE(m_Device.GetLog(), result == VK_SUCCESS, m_TextureIndex, "Can't acquire the next texture of the swapchain: vkAcquireNextImageKHR returned %d.", (int32_t)result); + VkSubmitInfo submitInfo = { VK_STRUCTURE_TYPE_SUBMIT_INFO, nullptr, 1, &m_Semaphore, nullptr, 0, nullptr, 0, nullptr }; + vk.QueueSubmit(*m_CommandQueue, 1, &submitInfo, VK_NULL_HANDLE); + return m_TextureIndex; } -inline Result SwapChainVK::Present(QueueSemaphore& textureReadyForPresent) +inline Result SwapChainVK::Present() { - const VkSemaphore semaphore = (QueueSemaphoreVK&)textureReadyForPresent; + const auto& vk = m_Device.GetDispatchTable(); - VkResult presentRes = VK_SUCCESS; + VkSubmitInfo submitInfo = { VK_STRUCTURE_TYPE_SUBMIT_INFO, nullptr, 0, nullptr, nullptr, 0, nullptr, 1, &m_Semaphore }; + vk.QueueSubmit(*m_CommandQueue, 1, &submitInfo, VK_NULL_HANDLE); const VkPresentInfoKHR info = { VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, nullptr, - 1, - &semaphore, - 1, - &m_Handle, + 1, &m_Semaphore, + 1, &m_Handle, &m_TextureIndex, - &presentRes + nullptr }; - const auto& vk = m_Device.GetDispatchTable(); const VkResult result = vk.QueuePresentKHR(*m_CommandQueue, &info); RETURN_ON_FAILURE(m_Device.GetLog(), result == VK_SUCCESS, GetReturnCode(result), @@ -316,6 +323,7 @@ inline Result SwapChainVK::SetHdrMetadata(const HdrMetadata& hdrMetadata) }; vk.SetHdrMetadataEXT(m_Device, 1, &m_Handle, &data); + return Result::SUCCESS; } diff --git a/Source/VK/SwapChainVK.h b/Source/VK/SwapChainVK.h index 09531be0..a6a78148 100644 --- a/Source/VK/SwapChainVK.h +++ b/Source/VK/SwapChainVK.h @@ -12,38 +12,43 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct DeviceVK; - struct CommandQueueVK; - struct TextureVK; - - struct SwapChainVK - { - SwapChainVK(DeviceVK& device); - ~SwapChainVK(); - - DeviceVK& GetDevice() const; - Result Create(const SwapChainDesc& swapChainDesc); - - void SetDebugName(const char* name); - Texture* const* GetTextures(uint32_t& textureNum, Format& format) const; - uint32_t AcquireNextTexture(QueueSemaphore& textureReadyForRender); - Result Present(QueueSemaphore& textureReadyForPresent); - Result SetHdrMetadata(const HdrMetadata& hdrMetadata); - - private: - Result CreateSurface(const SwapChainDesc& swapChainDesc); - - VkSwapchainKHR m_Handle = VK_NULL_HANDLE; - const CommandQueueVK* m_CommandQueue = nullptr; - uint32_t m_TextureIndex = std::numeric_limits::max(); - VkSurfaceKHR m_Surface = VK_NULL_HANDLE; - Vector m_Textures; - Format m_Format = Format::UNKNOWN; - DeviceVK& m_Device; - }; - - inline DeviceVK& SwapChainVK::GetDevice() const - { - return m_Device; - } + +struct DeviceVK; +struct CommandQueueVK; +struct TextureVK; + +struct SwapChainVK +{ + inline DeviceVK& GetDevice() const + { return m_Device; } + + SwapChainVK(DeviceVK& device); + ~SwapChainVK(); + + Result Create(const SwapChainDesc& swapChainDesc); + + //================================================================================================================ + // NRI + //================================================================================================================ + + void SetDebugName(const char* name); + Texture* const* GetTextures(uint32_t& textureNum, Format& format) const; + uint32_t AcquireNextTexture(); + Result Present(); + Result SetHdrMetadata(const HdrMetadata& hdrMetadata); + +private: + Result CreateSurface(const SwapChainDesc& swapChainDesc); + +private: + VkSwapchainKHR m_Handle = VK_NULL_HANDLE; + const CommandQueueVK* m_CommandQueue = nullptr; + uint32_t m_TextureIndex = 0; + VkSurfaceKHR m_Surface = VK_NULL_HANDLE; + Vector m_Textures; + Format m_Format = Format::UNKNOWN; + DeviceVK& m_Device; + VkSemaphore m_Semaphore = VK_NULL_HANDLE; +}; + } \ No newline at end of file diff --git a/Source/VK/SwapChainVK.hpp b/Source/VK/SwapChainVK.hpp index 657a1e78..4cce578b 100644 --- a/Source/VK/SwapChainVK.hpp +++ b/Source/VK/SwapChainVK.hpp @@ -8,10 +8,6 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma once - -#pragma region [ SwapChainInterface ] - static void NRI_CALL SetSwapChainDebugName(SwapChain& swapChain, const char* name) { ((SwapChainVK&)swapChain).SetDebugName(name); @@ -22,14 +18,14 @@ static Texture* const* NRI_CALL GetSwapChainTextures(const SwapChain& swapChain, return ((SwapChainVK&)swapChain).GetTextures(textureNum, format); } -static uint32_t NRI_CALL AcquireNextSwapChainTexture(SwapChain& swapChain, QueueSemaphore& textureReadyForRender) +static uint32_t NRI_CALL AcquireNextSwapChainTexture(SwapChain& swapChain) { - return ((SwapChainVK&)swapChain).AcquireNextTexture(textureReadyForRender); + return ((SwapChainVK&)swapChain).AcquireNextTexture(); } -static Result NRI_CALL SwapChainPresent(SwapChain& swapChain, QueueSemaphore& textureReadyForPresent) +static Result NRI_CALL SwapChainPresent(SwapChain& swapChain) { - return ((SwapChainVK&)swapChain).Present(textureReadyForPresent); + return ((SwapChainVK&)swapChain).Present(); } static Result NRI_CALL SetSwapChainHdrMetadata(SwapChain& swapChain, const HdrMetadata& hdrMetadata) @@ -37,13 +33,4 @@ static Result NRI_CALL SetSwapChainHdrMetadata(SwapChain& swapChain, const HdrMe return ((SwapChainVK&)swapChain).SetHdrMetadata(hdrMetadata); } -void FillFunctionTableSwapChainVK(SwapChainInterface& swapChainInterface) -{ - swapChainInterface.SetSwapChainDebugName = ::SetSwapChainDebugName; - swapChainInterface.GetSwapChainTextures = ::GetSwapChainTextures; - swapChainInterface.AcquireNextSwapChainTexture = ::AcquireNextSwapChainTexture; - swapChainInterface.SwapChainPresent = ::SwapChainPresent; - swapChainInterface.SetSwapChainHdrMetadata = ::SetSwapChainHdrMetadata; -} - -#pragma endregion +Define_SwapChain_PartiallyFillFunctionTable(VK) diff --git a/Source/VK/TextureVK.cpp b/Source/VK/TextureVK.cpp index bd96e005..d173d186 100644 --- a/Source/VK/TextureVK.cpp +++ b/Source/VK/TextureVK.cpp @@ -11,7 +11,6 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include "SharedVK.h" #include "TextureVK.h" #include "CommandQueueVK.h" -#include "DeviceVK.h" using namespace nri; @@ -119,6 +118,10 @@ Result TextureVK::Create(const TextureVulkanDesc& textureDesc) return Result::SUCCESS; } +//================================================================================================================ +// NRI +//================================================================================================================ + inline void TextureVK::SetDebugName(const char* name) { std::array handles; @@ -169,24 +172,4 @@ void TextureVK::GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryD memoryDesc.type = unpack.type; } -inline void TextureVK::GetTextureVK(uint32_t physicalDeviceIndex, TextureVulkanDesc& textureVulkanDesc) const -{ - textureVulkanDesc = {}; - textureVulkanDesc.vkImage = (NRIVkImage)GetHandle(physicalDeviceIndex); - textureVulkanDesc.vkFormat = GetVkFormat(GetFormat()); - textureVulkanDesc.vkImageAspectFlags = GetImageAspectFlags(); - textureVulkanDesc.vkImageType = GetImageType(GetType()); - textureVulkanDesc.size[0] = GetSize(0); - textureVulkanDesc.size[1] = GetSize(1); - textureVulkanDesc.size[2] = GetSize(2); - textureVulkanDesc.mipNum = GetMipNum(); - textureVulkanDesc.arraySize = GetArraySize(); - textureVulkanDesc.sampleNum = (uint8_t)GetSampleCount(); - textureVulkanDesc.physicalDeviceMask = 1 << physicalDeviceIndex; -} - #include "TextureVK.hpp" - -static_assert((uint32_t)nri::TextureType::TEXTURE_1D == 0u, "TextureVK::GetSize() depends on nri::TextureType"); -static_assert((uint32_t)nri::TextureType::TEXTURE_2D == 1u, "TextureVK::GetSize() depends on nri::TextureType"); -static_assert((uint32_t)nri::TextureType::TEXTURE_3D == 2u, "TextureVK::GetSize() depends on nri::TextureType"); \ No newline at end of file diff --git a/Source/VK/TextureVK.h b/Source/VK/TextureVK.h index 484541fc..22a709fb 100644 --- a/Source/VK/TextureVK.h +++ b/Source/VK/TextureVK.h @@ -12,66 +12,49 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct DeviceVK; - struct TextureVK - { - TextureVK(DeviceVK& device); - ~TextureVK(); - - DeviceVK& GetDevice() const; - void Create(VkImage handle, VkImageAspectFlags aspectFlags, VkImageType imageType, const VkExtent3D& extent, Format format); - Result Create(const TextureDesc& textureDesc); - Result Create(const TextureVulkanDesc& textureDesc); - VkImage GetHandle(uint32_t physicalDeviceIndex) const; - VkImageAspectFlags GetImageAspectFlags() const; - const VkExtent3D& GetExtent() const; - uint16_t GetSize(uint32_t dim, uint32_t mipOffset = 0) const; - uint16_t GetMipNum() const; - uint16_t GetArraySize() const; - Format GetFormat() const; - TextureType GetType() const; - VkSampleCountFlagBits GetSampleCount() const; - void ClearHandle(); - - void SetDebugName(const char* name); - void GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const; - void GetTextureVK(uint32_t physicalDeviceIndex, TextureVulkanDesc& textureVulkanDesc) const; - - private: - std::array m_Handles = {}; - VkImageAspectFlags m_ImageAspectFlags = (VkImageAspectFlags)0; - VkExtent3D m_Extent = {}; - uint16_t m_MipNum = 0; - uint16_t m_ArraySize = 0; - Format m_Format = nri::Format::UNKNOWN; - TextureType m_TextureType = (TextureType)0; - VkSampleCountFlagBits m_SampleCount = (VkSampleCountFlagBits)0; - DeviceVK& m_Device; - bool m_OwnsNativeObjects = false; - }; - - inline TextureVK::TextureVK(DeviceVK& device) : +struct DeviceVK; + +struct TextureVK +{ + inline TextureVK(DeviceVK& device) : m_Device(device) - { - } + {} - inline VkImage TextureVK::GetHandle(uint32_t physicalDeviceIndex) const - { - return m_Handles[physicalDeviceIndex]; - } + inline VkImage GetHandle(uint32_t physicalDeviceIndex) const + { return m_Handles[physicalDeviceIndex]; } - inline DeviceVK& TextureVK::GetDevice() const - { - return m_Device; - } + inline DeviceVK& GetDevice() const + { return m_Device; } - inline VkImageAspectFlags TextureVK::GetImageAspectFlags() const + inline VkImageAspectFlags GetImageAspectFlags() const + { return m_ImageAspectFlags; } + + inline const VkExtent3D& GetExtent() const + { return m_Extent; } + + inline uint16_t GetMipNum() const + { return m_MipNum; } + + inline uint16_t GetArraySize() const + { return m_ArraySize; } + + inline Format GetFormat() const + { return m_Format; } + + inline TextureType GetType() const + { return m_TextureType; } + + inline VkSampleCountFlagBits GetSampleCount() const + { return m_SampleCount; } + + inline void ClearHandle() { - return m_ImageAspectFlags; + for (uint32_t i = 0; i < GetCountOf(m_Handles); i++) + m_Handles[i] = VK_NULL_HANDLE; } - inline uint16_t TextureVK::GetSize(uint32_t dimension, uint32_t mipOffset) const + inline uint16_t GetSize(uint32_t dimension, uint32_t mipOffset) const { assert(dimension < 3); @@ -84,39 +67,30 @@ namespace nri return size; } - inline const VkExtent3D& TextureVK::GetExtent() const - { - return m_Extent; - } + ~TextureVK(); - inline uint16_t TextureVK::GetMipNum() const - { - return m_MipNum; - } + void Create(VkImage handle, VkImageAspectFlags aspectFlags, VkImageType imageType, const VkExtent3D& extent, Format format); + Result Create(const TextureDesc& textureDesc); + Result Create(const TextureVulkanDesc& textureDesc); - inline uint16_t TextureVK::GetArraySize() const - { - return m_ArraySize; - } + //================================================================================================================ + // NRI + //================================================================================================================ - inline Format TextureVK::GetFormat() const - { - return m_Format; - } + void SetDebugName(const char* name); + void GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const; - inline TextureType TextureVK::GetType() const - { - return m_TextureType; - } - - inline VkSampleCountFlagBits TextureVK::GetSampleCount() const - { - return m_SampleCount; - } +private: + DeviceVK& m_Device; + std::array m_Handles = {}; + VkImageAspectFlags m_ImageAspectFlags = (VkImageAspectFlags)0; + VkExtent3D m_Extent = {}; + Format m_Format = nri::Format::UNKNOWN; + TextureType m_TextureType = (TextureType)0; + VkSampleCountFlagBits m_SampleCount = (VkSampleCountFlagBits)0; + uint16_t m_MipNum = 0; + uint16_t m_ArraySize = 0; + bool m_OwnsNativeObjects = false; +}; - inline void TextureVK::ClearHandle() - { - for (uint32_t i = 0; i < GetCountOf(m_Handles); i++) - m_Handles[i] = VK_NULL_HANDLE; - } } \ No newline at end of file diff --git a/Source/VK/TextureVK.hpp b/Source/VK/TextureVK.hpp index 0d72ef02..3d8114e2 100644 --- a/Source/VK/TextureVK.hpp +++ b/Source/VK/TextureVK.hpp @@ -8,9 +8,7 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma once - -#pragma region [ CoreInterface ] +#pragma region [ Core ] static void NRI_CALL SetTextureDebugName(Texture& texture, const char* name) { @@ -27,11 +25,6 @@ static void NRI_CALL GetTextureMemoryInfo(const Texture& texture, MemoryLocation ((TextureVK&)texture).GetMemoryInfo(memoryLocation, memoryDesc); } -void FillFunctionTableTextureVK(CoreInterface& coreInterface) -{ - coreInterface.SetTextureDebugName = ::SetTextureDebugName; - coreInterface.GetTextureNativeObject = ::GetTextureNativeObject; - coreInterface.GetTextureMemoryInfo = ::GetTextureMemoryInfo; -} - #pragma endregion + +Define_Core_Texture_PartiallyFillFunctionTable(VK) diff --git a/Source/Validation/AccelerationStructureVal.h b/Source/Validation/AccelerationStructureVal.h index 47549ae7..9fd14b25 100644 --- a/Source/Validation/AccelerationStructureVal.h +++ b/Source/Validation/AccelerationStructureVal.h @@ -12,41 +12,43 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct MemoryVal; - - struct AccelerationStructureVal final : public DeviceObjectVal - { - AccelerationStructureVal(DeviceVal& device, AccelerationStructure& accelerationStructure, bool isBoundToMemory); - ~AccelerationStructureVal(); - - inline bool IsBoundToMemory() const; - inline void SetBoundToMemory(MemoryVal& memory); - - //====================================================================================================================== - // NRI - //====================================================================================================================== - void GetMemoryInfo(MemoryDesc& memoryDesc) const; - uint64_t GetUpdateScratchBufferSize() const; - uint64_t GetBuildScratchBufferSize() const; - uint64_t GetHandle(uint32_t physicalDeviceIndex) const; - uint64_t GetNativeObject(uint32_t physicalDeviceIndex) const; - Result CreateDescriptor(uint32_t physicalDeviceIndex, Descriptor*& descriptor); - void SetDebugName(const char* name); - - private: - const RayTracingInterface& m_RayTracingAPI; - MemoryVal* m_Memory = nullptr; - bool m_IsBoundToMemory = false; - }; - - inline bool AccelerationStructureVal::IsBoundToMemory() const - { - return m_IsBoundToMemory; - } - - inline void AccelerationStructureVal::SetBoundToMemory(MemoryVal& memory) - { - m_Memory = &memory; - m_IsBoundToMemory = true; - } + +struct MemoryVal; + +struct AccelerationStructureVal final : public DeviceObjectVal +{ + AccelerationStructureVal(DeviceVal& device, AccelerationStructure& accelerationStructure, bool isBoundToMemory); + ~AccelerationStructureVal(); + + inline bool IsBoundToMemory() const; + inline void SetBoundToMemory(MemoryVal& memory); + + //================================================================================================================ + // NRI + //================================================================================================================ + void GetMemoryInfo(MemoryDesc& memoryDesc) const; + uint64_t GetUpdateScratchBufferSize() const; + uint64_t GetBuildScratchBufferSize() const; + uint64_t GetHandle(uint32_t physicalDeviceIndex) const; + uint64_t GetNativeObject(uint32_t physicalDeviceIndex) const; + Result CreateDescriptor(uint32_t physicalDeviceIndex, Descriptor*& descriptor); + void SetDebugName(const char* name); + +private: + const RayTracingInterface& m_RayTracingAPI; + MemoryVal* m_Memory = nullptr; + bool m_IsBoundToMemory = false; +}; + +inline bool AccelerationStructureVal::IsBoundToMemory() const +{ + return m_IsBoundToMemory; +} + +inline void AccelerationStructureVal::SetBoundToMemory(MemoryVal& memory) +{ + m_Memory = &memory; + m_IsBoundToMemory = true; +} + } diff --git a/Source/Validation/AccelerationStructureVal.hpp b/Source/Validation/AccelerationStructureVal.hpp index bd6a8a13..c2dbfba2 100644 --- a/Source/Validation/AccelerationStructureVal.hpp +++ b/Source/Validation/AccelerationStructureVal.hpp @@ -8,7 +8,7 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ RayTracingInterface ] +#pragma region [ RayTracing ] static void NRI_CALL GetAccelerationStructureMemoryInfo(const AccelerationStructure& accelerationStructure, MemoryDesc& memoryDesc) { @@ -45,15 +45,6 @@ static uint64_t NRI_CALL GetAccelerationStructureNativeObject(const Acceleration return ((AccelerationStructureVal&)accelerationStructure).GetNativeObject(physicalDeviceIndex); } -void FillFunctionTableAccelarationStructureVal(RayTracingInterface& rayTracingInterface) -{ - rayTracingInterface.GetAccelerationStructureMemoryInfo = ::GetAccelerationStructureMemoryInfo; - rayTracingInterface.GetAccelerationStructureUpdateScratchBufferSize = ::GetAccelerationStructureUpdateScratchBufferSize; - rayTracingInterface.GetAccelerationStructureBuildScratchBufferSize = ::GetAccelerationStructureBuildScratchBufferSize; - rayTracingInterface.GetAccelerationStructureHandle = ::GetAccelerationStructureHandle; - rayTracingInterface.CreateAccelerationStructureDescriptor = ::CreateAccelerationStructureDescriptor; - rayTracingInterface.SetAccelerationStructureDebugName = ::SetAccelerationStructureDebugName; - rayTracingInterface.GetAccelerationStructureNativeObject = ::GetAccelerationStructureNativeObject; -} - #pragma endregion + +Define_RayTracing_AccelerationStructure_PartiallyFillFunctionTable(Val) diff --git a/Source/Validation/BufferVal.h b/Source/Validation/BufferVal.h index 263ae8b6..77383563 100644 --- a/Source/Validation/BufferVal.h +++ b/Source/Validation/BufferVal.h @@ -12,46 +12,48 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct MemoryVal; - struct BufferVal final : public DeviceObjectVal +struct MemoryVal; + +struct BufferVal final : public DeviceObjectVal +{ + BufferVal(DeviceVal& device, Buffer& buffer, const BufferDesc& bufferDesc); + BufferVal(DeviceVal& device, Buffer& buffer, const BufferD3D11Desc& bufferD3D11Desc); + BufferVal(DeviceVal& device, Buffer& buffer, const BufferD3D12Desc& bufferD3D12Desc); + BufferVal(DeviceVal& device, Buffer& buffer, const BufferVulkanDesc& bufferVulkanDesc); + ~BufferVal(); + + inline const BufferDesc& GetDesc() const + { return m_BufferDesc; } + + inline uint64_t GetNativeObject(uint32_t physicalDeviceIndex) const + { return m_CoreAPI.GetBufferNativeObject(m_ImplObject, physicalDeviceIndex); } + + inline bool IsBoundToMemory() const + { return m_IsBoundToMemory; } + + inline void SetBoundToMemory() + { m_IsBoundToMemory = true; } + + inline void SetBoundToMemory(MemoryVal& memory) { - BufferVal(DeviceVal& device, Buffer& buffer, const BufferDesc& bufferDesc); - BufferVal(DeviceVal& device, Buffer& buffer, const BufferD3D11Desc& bufferD3D11Desc); - BufferVal(DeviceVal& device, Buffer& buffer, const BufferD3D12Desc& bufferD3D12Desc); - BufferVal(DeviceVal& device, Buffer& buffer, const BufferVulkanDesc& bufferVulkanDesc); - ~BufferVal(); - - inline const BufferDesc& GetDesc() const - { return m_BufferDesc; } - - inline uint64_t GetNativeObject(uint32_t physicalDeviceIndex) const - { return m_CoreAPI.GetBufferNativeObject(m_ImplObject, physicalDeviceIndex); } - - inline bool IsBoundToMemory() const - { return m_IsBoundToMemory; } - - inline void SetBoundToMemory() - { m_IsBoundToMemory = true; } - - inline void SetBoundToMemory(MemoryVal& memory) - { - m_Memory = &memory; - m_IsBoundToMemory = true; - } - - //====================================================================================================================== - // NRI - //====================================================================================================================== - void SetDebugName(const char* name); - void GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const; - void* Map(uint64_t offset, uint64_t size); - void Unmap(); - - private: - MemoryVal* m_Memory = nullptr; - BufferDesc m_BufferDesc = {}; - bool m_IsBoundToMemory = false; - bool m_IsMapped = false; - }; + m_Memory = &memory; + m_IsBoundToMemory = true; + } + + //================================================================================================================ + // NRI + //================================================================================================================ + void SetDebugName(const char* name); + void GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const; + void* Map(uint64_t offset, uint64_t size); + void Unmap(); + +private: + MemoryVal* m_Memory = nullptr; + BufferDesc m_BufferDesc = {}; + bool m_IsBoundToMemory = false; + bool m_IsMapped = false; +}; + } diff --git a/Source/Validation/BufferVal.hpp b/Source/Validation/BufferVal.hpp index e9740fc9..2e52ff79 100644 --- a/Source/Validation/BufferVal.hpp +++ b/Source/Validation/BufferVal.hpp @@ -8,40 +8,33 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ CoreInterface ] +#pragma region [ Core ] static void NRI_CALL SetBufferDebugName(Buffer& buffer, const char* name) { - ((BufferVal*)&buffer)->SetDebugName(name); + ((BufferVal&)buffer).SetDebugName(name); } static uint64_t NRI_CALL GetBufferNativeObject(const Buffer& buffer, uint32_t physicalDeviceIndex) { - return ((BufferVal*)&buffer)->GetNativeObject(physicalDeviceIndex); + return ((BufferVal&)buffer).GetNativeObject(physicalDeviceIndex); } static void NRI_CALL GetBufferMemoryInfo(const Buffer& buffer, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) { - ((BufferVal*)&buffer)->GetMemoryInfo(memoryLocation, memoryDesc); + ((BufferVal&)buffer).GetMemoryInfo(memoryLocation, memoryDesc); } static void* NRI_CALL MapBuffer(Buffer& buffer, uint64_t offset, uint64_t size) { - return ((BufferVal*)&buffer)->Map(offset, size); + return ((BufferVal&)buffer).Map(offset, size); } static void NRI_CALL UnmapBuffer(Buffer& buffer) { - ((BufferVal*)&buffer)->Unmap(); -} - -void FillFunctionTableBufferVal(CoreInterface& coreInterface) -{ - coreInterface.SetBufferDebugName = ::SetBufferDebugName; - coreInterface.GetBufferNativeObject = ::GetBufferNativeObject; - coreInterface.GetBufferMemoryInfo = ::GetBufferMemoryInfo; - coreInterface.MapBuffer = ::MapBuffer; - coreInterface.UnmapBuffer = ::UnmapBuffer; + ((BufferVal&)buffer).Unmap(); } #pragma endregion + +Define_Core_Buffer_PartiallyFillFunctionTable(Val) diff --git a/Source/Validation/CommandAllocatorVal.h b/Source/Validation/CommandAllocatorVal.h index 952586aa..3616c225 100644 --- a/Source/Validation/CommandAllocatorVal.h +++ b/Source/Validation/CommandAllocatorVal.h @@ -12,15 +12,17 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct CommandAllocatorVal final : public DeviceObjectVal - { - CommandAllocatorVal(DeviceVal& device, CommandAllocator& commandAllocator); - //====================================================================================================================== - // NRI - //====================================================================================================================== - void SetDebugName(const char* name); - Result CreateCommandBuffer(CommandBuffer*& commandBuffer); - void Reset(); - }; +struct CommandAllocatorVal final : public DeviceObjectVal +{ + CommandAllocatorVal(DeviceVal& device, CommandAllocator& commandAllocator); + + //================================================================================================================ + // NRI + //================================================================================================================ + void SetDebugName(const char* name); + Result CreateCommandBuffer(CommandBuffer*& commandBuffer); + void Reset(); +}; + } diff --git a/Source/Validation/CommandAllocatorVal.hpp b/Source/Validation/CommandAllocatorVal.hpp index a3182ffd..32b3f9e6 100644 --- a/Source/Validation/CommandAllocatorVal.hpp +++ b/Source/Validation/CommandAllocatorVal.hpp @@ -8,28 +8,23 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ CoreInterface ] +#pragma region [ Core ] static void NRI_CALL SetCommandAllocatorDebugName(CommandAllocator& commandAllocator, const char* name) { - ((CommandAllocatorVal*)&commandAllocator)->SetDebugName(name); + ((CommandAllocatorVal&)commandAllocator).SetDebugName(name); } static Result NRI_CALL CreateCommandBuffer(CommandAllocator& commandAllocator, CommandBuffer*& commandBuffer) { - return ((CommandAllocatorVal*)&commandAllocator)->CreateCommandBuffer(commandBuffer); + return ((CommandAllocatorVal&)commandAllocator).CreateCommandBuffer(commandBuffer); } static void NRI_CALL ResetCommandAllocator(CommandAllocator& commandAllocator) { - ((CommandAllocatorVal*)&commandAllocator)->Reset(); -} - -void FillFunctionTableCommandAllocatorVal(CoreInterface& coreInterface) -{ - coreInterface.SetCommandAllocatorDebugName = ::SetCommandAllocatorDebugName; - coreInterface.CreateCommandBuffer = ::CreateCommandBuffer; - coreInterface.ResetCommandAllocator = ::ResetCommandAllocator; + ((CommandAllocatorVal&)commandAllocator).Reset(); } #pragma endregion + +Define_Core_CommandAllocator_PartiallyFillFunctionTable(Val) diff --git a/Source/Validation/CommandBufferVal.cpp b/Source/Validation/CommandBufferVal.cpp index 4ffdfa52..66e030ae 100644 --- a/Source/Validation/CommandBufferVal.cpp +++ b/Source/Validation/CommandBufferVal.cpp @@ -805,17 +805,9 @@ void CommandBufferVal::DispatchRays(const DispatchRaysDesc& dispatchRaysDesc) m_RayTracingAPI.CmdDispatchRays(m_ImplObject, dispatchRaysDescImpl); } -void CommandBufferVal::DispatchMeshTasks(uint32_t taskNum) +void CommandBufferVal::DispatchMeshTasks(uint32_t x, uint32_t y, uint32_t z) { - const uint32_t meshTaskMaxNum = m_Device.GetDesc().meshTaskMaxNum; - - if (taskNum > meshTaskMaxNum) - { - REPORT_ERROR(m_Device.GetLog(), - "Can't dispatch the specified number of mesh tasks: the number exceeds the maximum number of mesh tasks."); - } - - m_MeshShaderAPI.CmdDispatchMeshTasks(m_ImplObject, std::min(taskNum, meshTaskMaxNum)); + m_MeshShaderAPI.CmdDispatchMeshTasks(m_ImplObject, x, y, z); } template diff --git a/Source/Validation/CommandBufferVal.h b/Source/Validation/CommandBufferVal.h index 44940a9f..c2911458 100644 --- a/Source/Validation/CommandBufferVal.h +++ b/Source/Validation/CommandBufferVal.h @@ -12,107 +12,109 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct CommandBufferVal : public DeviceObjectVal - { - CommandBufferVal(DeviceVal& device, CommandBuffer& commandBuffer, bool isWrapped); - - inline const Vector& GetValidationCommands() const - { return m_ValidationCommands; } - - inline void* GetNativeObject() const - { return m_CoreAPI.GetCommandBufferNativeObject(m_ImplObject); } - - //================================================================================================================ - // NRI - //================================================================================================================ - void SetDebugName(const char* name); - Result Begin(const DescriptorPool* descriptorPool, uint32_t physicalDeviceIndex); - Result End(); - void SetViewports(const Viewport* viewports, uint32_t viewportNum); - void SetScissors(const Rect* rects, uint32_t rectNum); - void SetDepthBounds(float boundsMin, float boundsMax); - void SetStencilReference(uint8_t reference); - void SetSamplePositions(const SamplePosition* positions, uint32_t positionNum); - void ClearAttachments(const ClearDesc* clearDescs, uint32_t clearDescNum, const Rect* rects, uint32_t rectNum); - void ClearStorageBuffer(const ClearStorageBufferDesc& clearDesc); - void ClearStorageTexture(const ClearStorageTextureDesc& clearDesc); - void BeginRenderPass(const FrameBuffer& frameBuffer, RenderPassBeginFlag renderPassBeginFlag); - void EndRenderPass(); - void SetVertexBuffers(uint32_t baseSlot, uint32_t bufferNum, const Buffer* const* buffers, const uint64_t* offsets); - void SetIndexBuffer(const Buffer& buffer, uint64_t offset, IndexType indexType); - void SetPipelineLayout(const PipelineLayout& pipelineLayout); - void SetPipeline(const Pipeline& pipeline); - void SetDescriptorPool(const DescriptorPool& descriptorPool); - void SetDescriptorSet(uint32_t setIndexInPipelineLayout, const DescriptorSet& descriptorSet, const uint32_t* dynamicConstantBufferOffsets); - void SetConstants(uint32_t pushConstantIndex, const void* data, uint32_t size); - void Draw(uint32_t vertexNum, uint32_t instanceNum, uint32_t baseVertex, uint32_t baseInstance); - void DrawIndexed(uint32_t indexNum, uint32_t instanceNum, uint32_t baseIndex, uint32_t baseVertex, uint32_t baseInstance); - void DrawIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride); - void DrawIndexedIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride); - void CopyBuffer(Buffer& dstBuffer, uint32_t dstPhysicalDeviceIndex, uint64_t dstOffset, const Buffer& srcBuffer, uint32_t srcPhysicalDeviceIndex, uint64_t srcOffset, uint64_t size); - void CopyTexture(Texture& dstTexture, uint32_t dstPhysicalDeviceIndex, const TextureRegionDesc* dstRegionDesc, const Texture& srcTexture, uint32_t srcPhysicalDeviceIndex, const TextureRegionDesc* srcRegionDesc); - void UploadBufferToTexture(Texture& dstTexture, const TextureRegionDesc& dstRegionDesc, const Buffer& srcBuffer, const TextureDataLayoutDesc& srcDataLayoutDesc); - void ReadbackTextureToBuffer(Buffer& dstBuffer, TextureDataLayoutDesc& dstDataLayoutDesc, const Texture& srcTexture, const TextureRegionDesc& srcRegionDesc); - void Dispatch(uint32_t x, uint32_t y, uint32_t z); - void DispatchIndirect(const Buffer& buffer, uint64_t offset); - void PipelineBarrier(const TransitionBarrierDesc* transitionBarriers, const AliasingBarrierDesc* aliasingBarriers, BarrierDependency dependency); - void BeginQuery(const QueryPool& queryPool, uint32_t offset); - void EndQuery(const QueryPool& queryPool, uint32_t offset); - void CopyQueries(const QueryPool& queryPool, uint32_t offset, uint32_t num, Buffer& dstBuffer, uint64_t dstOffset); - void ResetQueries(const QueryPool& queryPool, uint32_t offset, uint32_t num); - void BeginAnnotation(const char* name); - void EndAnnotation(); - void Destroy(); - - void BuildTopLevelAccelerationStructure(uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, - AccelerationStructureBuildBits flags, AccelerationStructure& dst, Buffer& scratch, uint64_t scratchOffset); - void BuildBottomLevelAccelerationStructure(uint32_t geometryObjectNum, const GeometryObject* geometryObjects, - AccelerationStructureBuildBits flags, AccelerationStructure& dst, Buffer& scratch, uint64_t scratchOffset); - void UpdateTopLevelAccelerationStructure(uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, - AccelerationStructureBuildBits flags, AccelerationStructure& dst, AccelerationStructure& src, Buffer& scratch, uint64_t scratchOffset); - void UpdateBottomLevelAccelerationStructure(uint32_t geometryObjectNum, const GeometryObject* geometryObjects, - AccelerationStructureBuildBits flags, AccelerationStructure& dst, AccelerationStructure& src, Buffer& scratch, uint64_t scratchOffset); - - void CopyAccelerationStructure(AccelerationStructure& dst, AccelerationStructure& src, CopyMode copyMode); - void WriteAccelerationStructureSize(const AccelerationStructure* const* accelerationStructures, uint32_t accelerationStructureNum, QueryPool& queryPool, uint32_t queryOffset); - - void DispatchRays(const DispatchRaysDesc& dispatchRaysDesc); - void DispatchMeshTasks(uint32_t taskNum); - - private: - template - Command& AllocateValidationCommand(); - - Vector m_ValidationCommands; - bool m_IsRecordingStarted = false; - bool m_IsWrapped = false; - const FrameBuffer* m_FrameBuffer = nullptr; - int32_t m_AnnotationStack = 0; - const RayTracingInterface& m_RayTracingAPI; - const MeshShaderInterface& m_MeshShaderAPI; - }; - - enum class ValidationCommandType : uint32_t - { - NONE, - BEGIN_QUERY, - END_QUERY, - RESET_QUERY, - MAX_NUM - }; - - struct ValidationCommandUseQuery - { - ValidationCommandType type; - QueryPool* queryPool; - uint32_t queryPoolOffset; - }; - - struct ValidationCommandResetQuery - { - ValidationCommandType type; - QueryPool* queryPool; - uint32_t queryPoolOffset; - uint32_t queryNum; - }; + +struct CommandBufferVal : public DeviceObjectVal +{ + CommandBufferVal(DeviceVal& device, CommandBuffer& commandBuffer, bool isWrapped); + + inline const Vector& GetValidationCommands() const + { return m_ValidationCommands; } + + inline void* GetNativeObject() const + { return m_CoreAPI.GetCommandBufferNativeObject(m_ImplObject); } + + //================================================================================================================ + // NRI + //================================================================================================================ + void SetDebugName(const char* name); + Result Begin(const DescriptorPool* descriptorPool, uint32_t physicalDeviceIndex); + Result End(); + void SetViewports(const Viewport* viewports, uint32_t viewportNum); + void SetScissors(const Rect* rects, uint32_t rectNum); + void SetDepthBounds(float boundsMin, float boundsMax); + void SetStencilReference(uint8_t reference); + void SetSamplePositions(const SamplePosition* positions, uint32_t positionNum); + void ClearAttachments(const ClearDesc* clearDescs, uint32_t clearDescNum, const Rect* rects, uint32_t rectNum); + void ClearStorageBuffer(const ClearStorageBufferDesc& clearDesc); + void ClearStorageTexture(const ClearStorageTextureDesc& clearDesc); + void BeginRenderPass(const FrameBuffer& frameBuffer, RenderPassBeginFlag renderPassBeginFlag); + void EndRenderPass(); + void SetVertexBuffers(uint32_t baseSlot, uint32_t bufferNum, const Buffer* const* buffers, const uint64_t* offsets); + void SetIndexBuffer(const Buffer& buffer, uint64_t offset, IndexType indexType); + void SetPipelineLayout(const PipelineLayout& pipelineLayout); + void SetPipeline(const Pipeline& pipeline); + void SetDescriptorPool(const DescriptorPool& descriptorPool); + void SetDescriptorSet(uint32_t setIndexInPipelineLayout, const DescriptorSet& descriptorSet, const uint32_t* dynamicConstantBufferOffsets); + void SetConstants(uint32_t pushConstantIndex, const void* data, uint32_t size); + void Draw(uint32_t vertexNum, uint32_t instanceNum, uint32_t baseVertex, uint32_t baseInstance); + void DrawIndexed(uint32_t indexNum, uint32_t instanceNum, uint32_t baseIndex, uint32_t baseVertex, uint32_t baseInstance); + void DrawIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride); + void DrawIndexedIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride); + void CopyBuffer(Buffer& dstBuffer, uint32_t dstPhysicalDeviceIndex, uint64_t dstOffset, const Buffer& srcBuffer, uint32_t srcPhysicalDeviceIndex, uint64_t srcOffset, uint64_t size); + void CopyTexture(Texture& dstTexture, uint32_t dstPhysicalDeviceIndex, const TextureRegionDesc* dstRegionDesc, const Texture& srcTexture, uint32_t srcPhysicalDeviceIndex, const TextureRegionDesc* srcRegionDesc); + void UploadBufferToTexture(Texture& dstTexture, const TextureRegionDesc& dstRegionDesc, const Buffer& srcBuffer, const TextureDataLayoutDesc& srcDataLayoutDesc); + void ReadbackTextureToBuffer(Buffer& dstBuffer, TextureDataLayoutDesc& dstDataLayoutDesc, const Texture& srcTexture, const TextureRegionDesc& srcRegionDesc); + void Dispatch(uint32_t x, uint32_t y, uint32_t z); + void DispatchIndirect(const Buffer& buffer, uint64_t offset); + void PipelineBarrier(const TransitionBarrierDesc* transitionBarriers, const AliasingBarrierDesc* aliasingBarriers, BarrierDependency dependency); + void BeginQuery(const QueryPool& queryPool, uint32_t offset); + void EndQuery(const QueryPool& queryPool, uint32_t offset); + void CopyQueries(const QueryPool& queryPool, uint32_t offset, uint32_t num, Buffer& dstBuffer, uint64_t dstOffset); + void ResetQueries(const QueryPool& queryPool, uint32_t offset, uint32_t num); + void BeginAnnotation(const char* name); + void EndAnnotation(); + void Destroy(); + + void BuildTopLevelAccelerationStructure(uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, + AccelerationStructureBuildBits flags, AccelerationStructure& dst, Buffer& scratch, uint64_t scratchOffset); + void BuildBottomLevelAccelerationStructure(uint32_t geometryObjectNum, const GeometryObject* geometryObjects, + AccelerationStructureBuildBits flags, AccelerationStructure& dst, Buffer& scratch, uint64_t scratchOffset); + void UpdateTopLevelAccelerationStructure(uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, + AccelerationStructureBuildBits flags, AccelerationStructure& dst, AccelerationStructure& src, Buffer& scratch, uint64_t scratchOffset); + void UpdateBottomLevelAccelerationStructure(uint32_t geometryObjectNum, const GeometryObject* geometryObjects, + AccelerationStructureBuildBits flags, AccelerationStructure& dst, AccelerationStructure& src, Buffer& scratch, uint64_t scratchOffset); + + void CopyAccelerationStructure(AccelerationStructure& dst, AccelerationStructure& src, CopyMode copyMode); + void WriteAccelerationStructureSize(const AccelerationStructure* const* accelerationStructures, uint32_t accelerationStructureNum, QueryPool& queryPool, uint32_t queryOffset); + + void DispatchRays(const DispatchRaysDesc& dispatchRaysDesc); + void DispatchMeshTasks(uint32_t x, uint32_t y, uint32_t z); + +private: + template + Command& AllocateValidationCommand(); + + Vector m_ValidationCommands; + bool m_IsRecordingStarted = false; + bool m_IsWrapped = false; + const FrameBuffer* m_FrameBuffer = nullptr; + int32_t m_AnnotationStack = 0; + const RayTracingInterface& m_RayTracingAPI; + const MeshShaderInterface& m_MeshShaderAPI; +}; + +enum class ValidationCommandType : uint32_t +{ + NONE, + BEGIN_QUERY, + END_QUERY, + RESET_QUERY, + MAX_NUM +}; + +struct ValidationCommandUseQuery +{ + ValidationCommandType type; + QueryPool* queryPool; + uint32_t queryPoolOffset; +}; + +struct ValidationCommandResetQuery +{ + ValidationCommandType type; + QueryPool* queryPool; + uint32_t queryPoolOffset; + uint32_t queryNum; +}; + } diff --git a/Source/Validation/CommandBufferVal.hpp b/Source/Validation/CommandBufferVal.hpp index 7722c2e1..f5f3b8f8 100644 --- a/Source/Validation/CommandBufferVal.hpp +++ b/Source/Validation/CommandBufferVal.hpp @@ -8,321 +8,258 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ CoreInterface ] +#pragma region [ Core ] static void NRI_CALL SetCommandBufferDebugName(CommandBuffer& commandBuffer, const char* name) { - ((CommandBufferVal*)&commandBuffer)->SetDebugName(name); + ((CommandBufferVal&)commandBuffer).SetDebugName(name); } static Result NRI_CALL BeginCommandBuffer(CommandBuffer& commandBuffer, const DescriptorPool* descriptorPool, uint32_t physicalDeviceIndex) { - return ((CommandBufferVal*)&commandBuffer)->Begin(descriptorPool, physicalDeviceIndex); + return ((CommandBufferVal&)commandBuffer).Begin(descriptorPool, physicalDeviceIndex); } static Result NRI_CALL EndCommandBuffer(CommandBuffer& commandBuffer) { - return ((CommandBufferVal*)&commandBuffer)->End(); + return ((CommandBufferVal&)commandBuffer).End(); } static void NRI_CALL CmdSetPipelineLayout(CommandBuffer& commandBuffer, const PipelineLayout& pipelineLayout) { - ((CommandBufferVal*)&commandBuffer)->SetPipelineLayout(pipelineLayout); + ((CommandBufferVal&)commandBuffer).SetPipelineLayout(pipelineLayout); } static void NRI_CALL CmdSetPipeline(CommandBuffer& commandBuffer, const Pipeline& pipeline) { - ((CommandBufferVal*)&commandBuffer)->SetPipeline(pipeline); + ((CommandBufferVal&)commandBuffer).SetPipeline(pipeline); } static void NRI_CALL CmdPipelineBarrier(CommandBuffer& commandBuffer, const TransitionBarrierDesc* transitionBarriers, const AliasingBarrierDesc* aliasingBarriers, BarrierDependency dependency) { - ((CommandBufferVal*)&commandBuffer)->PipelineBarrier(transitionBarriers, aliasingBarriers, dependency); + ((CommandBufferVal&)commandBuffer).PipelineBarrier(transitionBarriers, aliasingBarriers, dependency); } static void NRI_CALL CmdSetDescriptorPool(CommandBuffer& commandBuffer, const DescriptorPool& descriptorPool) { - ((CommandBufferVal*)&commandBuffer)->SetDescriptorPool(descriptorPool); + ((CommandBufferVal&)commandBuffer).SetDescriptorPool(descriptorPool); } static void NRI_CALL CmdSetDescriptorSet(CommandBuffer& commandBuffer, uint32_t setIndexInPipelineLayout, const DescriptorSet& descriptorSet, const uint32_t* dynamicConstantBufferOffsets) { - ((CommandBufferVal*)&commandBuffer)->SetDescriptorSet(setIndexInPipelineLayout, descriptorSet, dynamicConstantBufferOffsets); + ((CommandBufferVal&)commandBuffer).SetDescriptorSet(setIndexInPipelineLayout, descriptorSet, dynamicConstantBufferOffsets); } static void NRI_CALL CmdSetConstants(CommandBuffer& commandBuffer, uint32_t pushConstantIndex, const void* data, uint32_t size) { - ((CommandBufferVal*)&commandBuffer)->SetConstants(pushConstantIndex, data, size); + ((CommandBufferVal&)commandBuffer).SetConstants(pushConstantIndex, data, size); } static void NRI_CALL CmdBeginRenderPass(CommandBuffer& commandBuffer, const FrameBuffer& frameBuffer, RenderPassBeginFlag renderPassBeginFlag) { - ((CommandBufferVal*)&commandBuffer)->BeginRenderPass(frameBuffer, renderPassBeginFlag); + ((CommandBufferVal&)commandBuffer).BeginRenderPass(frameBuffer, renderPassBeginFlag); } static void NRI_CALL CmdEndRenderPass(CommandBuffer& commandBuffer) { - ((CommandBufferVal*)&commandBuffer)->EndRenderPass(); + ((CommandBufferVal&)commandBuffer).EndRenderPass(); } static void NRI_CALL CmdSetViewports(CommandBuffer& commandBuffer, const Viewport* viewports, uint32_t viewportNum) { - ((CommandBufferVal*)&commandBuffer)->SetViewports(viewports, viewportNum); + ((CommandBufferVal&)commandBuffer).SetViewports(viewports, viewportNum); } static void NRI_CALL CmdSetScissors(CommandBuffer& commandBuffer, const Rect* rects, uint32_t rectNum) { - ((CommandBufferVal*)&commandBuffer)->SetScissors(rects, rectNum); + ((CommandBufferVal&)commandBuffer).SetScissors(rects, rectNum); } static void NRI_CALL CmdSetDepthBounds(CommandBuffer& commandBuffer, float boundsMin, float boundsMax) { - ((CommandBufferVal*)&commandBuffer)->SetDepthBounds(boundsMin, boundsMax); + ((CommandBufferVal&)commandBuffer).SetDepthBounds(boundsMin, boundsMax); } static void NRI_CALL CmdSetStencilReference(CommandBuffer& commandBuffer, uint8_t reference) { - ((CommandBufferVal*)&commandBuffer)->SetStencilReference(reference); + ((CommandBufferVal&)commandBuffer).SetStencilReference(reference); } static void NRI_CALL CmdSetSamplePositions(CommandBuffer& commandBuffer, const SamplePosition* positions, uint32_t positionNum) { - ((CommandBufferVal*)&commandBuffer)->SetSamplePositions(positions, positionNum); + ((CommandBufferVal&)commandBuffer).SetSamplePositions(positions, positionNum); } static void NRI_CALL CmdClearAttachments(CommandBuffer& commandBuffer, const ClearDesc* clearDescs, uint32_t clearDescNum, const Rect* rects, uint32_t rectNum) { - ((CommandBufferVal*)&commandBuffer)->ClearAttachments(clearDescs, clearDescNum, rects, rectNum); + ((CommandBufferVal&)commandBuffer).ClearAttachments(clearDescs, clearDescNum, rects, rectNum); } static void NRI_CALL CmdSetIndexBuffer(CommandBuffer& commandBuffer, const Buffer& buffer, uint64_t offset, IndexType indexType) { - ((CommandBufferVal*)&commandBuffer)->SetIndexBuffer(buffer, offset, indexType); + ((CommandBufferVal&)commandBuffer).SetIndexBuffer(buffer, offset, indexType); } static void NRI_CALL CmdSetVertexBuffers(CommandBuffer& commandBuffer, uint32_t baseSlot, uint32_t bufferNum, const Buffer* const* buffers, const uint64_t* offsets) { - ((CommandBufferVal*)&commandBuffer)->SetVertexBuffers(baseSlot, bufferNum, buffers, offsets); + ((CommandBufferVal&)commandBuffer).SetVertexBuffers(baseSlot, bufferNum, buffers, offsets); } static void NRI_CALL CmdDraw(CommandBuffer& commandBuffer, uint32_t vertexNum, uint32_t instanceNum, uint32_t baseVertex, uint32_t baseInstance) { - ((CommandBufferVal*)&commandBuffer)->Draw(vertexNum, instanceNum, baseVertex, baseInstance); + ((CommandBufferVal&)commandBuffer).Draw(vertexNum, instanceNum, baseVertex, baseInstance); } static void NRI_CALL CmdDrawIndexed(CommandBuffer& commandBuffer, uint32_t indexNum, uint32_t instanceNum, uint32_t baseIndex, uint32_t baseVertex, uint32_t baseInstance) { - ((CommandBufferVal*)&commandBuffer)->DrawIndexed(indexNum, instanceNum, baseIndex, baseVertex, baseInstance); + ((CommandBufferVal&)commandBuffer).DrawIndexed(indexNum, instanceNum, baseIndex, baseVertex, baseInstance); } static void NRI_CALL CmdDrawIndirect(CommandBuffer& commandBuffer, const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride) { - ((CommandBufferVal*)&commandBuffer)->DrawIndirect(buffer, offset, drawNum, stride); + ((CommandBufferVal&)commandBuffer).DrawIndirect(buffer, offset, drawNum, stride); } static void NRI_CALL CmdDrawIndexedIndirect(CommandBuffer& commandBuffer, const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride) { - ((CommandBufferVal*)&commandBuffer)->DrawIndexedIndirect(buffer, offset, drawNum, stride); + ((CommandBufferVal&)commandBuffer).DrawIndexedIndirect(buffer, offset, drawNum, stride); } static void NRI_CALL CmdDispatch(CommandBuffer& commandBuffer, uint32_t x, uint32_t y, uint32_t z) { - ((CommandBufferVal*)&commandBuffer)->Dispatch(x, y, z); + ((CommandBufferVal&)commandBuffer).Dispatch(x, y, z); } static void NRI_CALL CmdDispatchIndirect(CommandBuffer& commandBuffer, const Buffer& buffer, uint64_t offset) { - ((CommandBufferVal*)&commandBuffer)->DispatchIndirect(buffer, offset); + ((CommandBufferVal&)commandBuffer).DispatchIndirect(buffer, offset); } static void NRI_CALL CmdBeginQuery(CommandBuffer& commandBuffer, const QueryPool& queryPool, uint32_t offset) { - ((CommandBufferVal*)&commandBuffer)->BeginQuery(queryPool, offset); + ((CommandBufferVal&)commandBuffer).BeginQuery(queryPool, offset); } static void NRI_CALL CmdEndQuery(CommandBuffer& commandBuffer, const QueryPool& queryPool, uint32_t offset) { - ((CommandBufferVal*)&commandBuffer)->EndQuery(queryPool, offset); + ((CommandBufferVal&)commandBuffer).EndQuery(queryPool, offset); } static void NRI_CALL CmdBeginAnnotation(CommandBuffer& commandBuffer, const char* name) { - ((CommandBufferVal*)&commandBuffer)->BeginAnnotation(name); + ((CommandBufferVal&)commandBuffer).BeginAnnotation(name); } static void NRI_CALL CmdEndAnnotation(CommandBuffer& commandBuffer) { - ((CommandBufferVal*)&commandBuffer)->EndAnnotation(); + ((CommandBufferVal&)commandBuffer).EndAnnotation(); } static void NRI_CALL CmdClearStorageBuffer(CommandBuffer& commandBuffer, const ClearStorageBufferDesc& clearDesc) { - ((CommandBufferVal*)&commandBuffer)->ClearStorageBuffer(clearDesc); + ((CommandBufferVal&)commandBuffer).ClearStorageBuffer(clearDesc); } static void NRI_CALL CmdClearStorageTexture(CommandBuffer& commandBuffer, const ClearStorageTextureDesc& clearDesc) { - ((CommandBufferVal*)&commandBuffer)->ClearStorageTexture(clearDesc); + ((CommandBufferVal&)commandBuffer).ClearStorageTexture(clearDesc); } static void NRI_CALL CmdCopyBuffer(CommandBuffer& commandBuffer, Buffer& dstBuffer, uint32_t dstPhysicalDeviceIndex, uint64_t dstOffset, const Buffer& srcBuffer, uint32_t srcPhysicalDeviceIndex, uint64_t srcOffset, uint64_t size) { - ((CommandBufferVal*)&commandBuffer)->CopyBuffer(dstBuffer, dstPhysicalDeviceIndex, dstOffset, srcBuffer, srcPhysicalDeviceIndex, srcOffset, size); + ((CommandBufferVal&)commandBuffer).CopyBuffer(dstBuffer, dstPhysicalDeviceIndex, dstOffset, srcBuffer, srcPhysicalDeviceIndex, srcOffset, size); } static void NRI_CALL CmdCopyTexture(CommandBuffer& commandBuffer, Texture& dstTexture, uint32_t dstPhysicalDeviceIndex, const TextureRegionDesc* dstRegionDesc, const Texture& srcTexture, uint32_t srcPhysicalDeviceIndex, const TextureRegionDesc* srcRegionDesc) { - ((CommandBufferVal*)&commandBuffer)->CopyTexture(dstTexture, dstPhysicalDeviceIndex, dstRegionDesc, srcTexture, srcPhysicalDeviceIndex, srcRegionDesc); + ((CommandBufferVal&)commandBuffer).CopyTexture(dstTexture, dstPhysicalDeviceIndex, dstRegionDesc, srcTexture, srcPhysicalDeviceIndex, srcRegionDesc); } static void NRI_CALL CmdUploadBufferToTexture(CommandBuffer& commandBuffer, Texture& dstTexture, const TextureRegionDesc& dstRegionDesc, const Buffer& srcBuffer, const TextureDataLayoutDesc& srcDataLayoutDesc) { - ((CommandBufferVal*)&commandBuffer)->UploadBufferToTexture(dstTexture, dstRegionDesc, srcBuffer, srcDataLayoutDesc); + ((CommandBufferVal&)commandBuffer).UploadBufferToTexture(dstTexture, dstRegionDesc, srcBuffer, srcDataLayoutDesc); } static void NRI_CALL CmdReadbackTextureToBuffer(CommandBuffer& commandBuffer, Buffer& dstBuffer, TextureDataLayoutDesc& dstDataLayoutDesc, const Texture& srcTexture, const TextureRegionDesc& srcRegionDesc) { - ((CommandBufferVal*)&commandBuffer)->ReadbackTextureToBuffer(dstBuffer, dstDataLayoutDesc, srcTexture, srcRegionDesc); + ((CommandBufferVal&)commandBuffer).ReadbackTextureToBuffer(dstBuffer, dstDataLayoutDesc, srcTexture, srcRegionDesc); } static void NRI_CALL CmdCopyQueries(CommandBuffer& commandBuffer, const QueryPool& queryPool, uint32_t offset, uint32_t num, Buffer& dstBuffer, uint64_t dstOffset) { - ((CommandBufferVal*)&commandBuffer)->CopyQueries(queryPool, offset, num, dstBuffer, dstOffset); + ((CommandBufferVal&)commandBuffer).CopyQueries(queryPool, offset, num, dstBuffer, dstOffset); } static void NRI_CALL CmdResetQueries(CommandBuffer& commandBuffer, const QueryPool& queryPool, uint32_t offset, uint32_t num) { - ((CommandBufferVal*)&commandBuffer)->ResetQueries(queryPool, offset, num); + ((CommandBufferVal&)commandBuffer).ResetQueries(queryPool, offset, num); } static void NRI_CALL DestroyCommandBuffer(CommandBuffer& commandBuffer) { - ((CommandBufferVal*)&commandBuffer)->Destroy(); + ((CommandBufferVal&)commandBuffer).Destroy(); } static void* NRI_CALL GetCommandBufferNativeObject(const CommandBuffer& commandBuffer) { - return ((CommandBufferVal*)&commandBuffer)->GetNativeObject(); -} - -void FillFunctionTableCommandBufferVal(CoreInterface& coreInterface) -{ - coreInterface.DestroyCommandBuffer = ::DestroyCommandBuffer; - - coreInterface.BeginCommandBuffer = ::BeginCommandBuffer; - coreInterface.EndCommandBuffer = ::EndCommandBuffer; - - coreInterface.CmdSetPipelineLayout = ::CmdSetPipelineLayout; - coreInterface.CmdSetPipeline = ::CmdSetPipeline; - coreInterface.CmdPipelineBarrier = ::CmdPipelineBarrier; - coreInterface.CmdSetDescriptorPool = ::CmdSetDescriptorPool; - coreInterface.CmdSetDescriptorSet = ::CmdSetDescriptorSet; - coreInterface.CmdSetConstants = ::CmdSetConstants; - - coreInterface.CmdBeginRenderPass = ::CmdBeginRenderPass; - coreInterface.CmdEndRenderPass = ::CmdEndRenderPass; - coreInterface.CmdSetViewports = ::CmdSetViewports; - coreInterface.CmdSetScissors = ::CmdSetScissors; - coreInterface.CmdSetDepthBounds = ::CmdSetDepthBounds; - coreInterface.CmdSetStencilReference = ::CmdSetStencilReference; - coreInterface.CmdSetSamplePositions = ::CmdSetSamplePositions; - coreInterface.CmdClearAttachments = ::CmdClearAttachments; - coreInterface.CmdSetIndexBuffer = ::CmdSetIndexBuffer; - coreInterface.CmdSetVertexBuffers = ::CmdSetVertexBuffers; - - coreInterface.CmdDraw = ::CmdDraw; - coreInterface.CmdDrawIndexed = ::CmdDrawIndexed; - coreInterface.CmdDrawIndirect = ::CmdDrawIndirect; - coreInterface.CmdDrawIndexedIndirect = ::CmdDrawIndexedIndirect; - coreInterface.CmdDispatch = ::CmdDispatch; - coreInterface.CmdDispatchIndirect = ::CmdDispatchIndirect; - coreInterface.CmdBeginQuery = ::CmdBeginQuery; - coreInterface.CmdEndQuery = ::CmdEndQuery; - coreInterface.CmdBeginAnnotation = ::CmdBeginAnnotation; - coreInterface.CmdEndAnnotation = ::CmdEndAnnotation; - - coreInterface.CmdClearStorageBuffer = ::CmdClearStorageBuffer; - coreInterface.CmdClearStorageTexture = ::CmdClearStorageTexture; - coreInterface.CmdCopyBuffer = ::CmdCopyBuffer; - coreInterface.CmdCopyTexture = ::CmdCopyTexture; - coreInterface.CmdUploadBufferToTexture = ::CmdUploadBufferToTexture; - coreInterface.CmdReadbackTextureToBuffer = ::CmdReadbackTextureToBuffer; - coreInterface.CmdCopyQueries = ::CmdCopyQueries; - coreInterface.CmdResetQueries = ::CmdResetQueries; - - coreInterface.SetCommandBufferDebugName = ::SetCommandBufferDebugName; - - coreInterface.GetCommandBufferNativeObject = ::GetCommandBufferNativeObject; + return ((CommandBufferVal&)commandBuffer).GetNativeObject(); } #pragma endregion -#pragma region [ RayTracingInterface ] +#pragma region [ RayTracing ] static void NRI_CALL CmdBuildTopLevelAccelerationStructure(CommandBuffer& commandBuffer, uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, AccelerationStructureBuildBits flags, AccelerationStructure& dst, Buffer& scratch, uint64_t scratchOffset) { - ((CommandBufferVal*)&commandBuffer)->BuildTopLevelAccelerationStructure(instanceNum, buffer, bufferOffset, flags, dst, scratch, scratchOffset); + ((CommandBufferVal&)commandBuffer).BuildTopLevelAccelerationStructure(instanceNum, buffer, bufferOffset, flags, dst, scratch, scratchOffset); } static void NRI_CALL CmdBuildBottomLevelAccelerationStructure(CommandBuffer& commandBuffer, uint32_t geometryObjectNum, const GeometryObject* geometryObjects, AccelerationStructureBuildBits flags, AccelerationStructure& dst, Buffer& scratch, uint64_t scratchOffset) { - ((CommandBufferVal*)&commandBuffer)->BuildBottomLevelAccelerationStructure(geometryObjectNum, geometryObjects, flags, dst, scratch, scratchOffset); + ((CommandBufferVal&)commandBuffer).BuildBottomLevelAccelerationStructure(geometryObjectNum, geometryObjects, flags, dst, scratch, scratchOffset); } static void NRI_CALL CmdUpdateTopLevelAccelerationStructure(CommandBuffer& commandBuffer, uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, AccelerationStructureBuildBits flags, AccelerationStructure& dst, AccelerationStructure& src, Buffer& scratch, uint64_t scratchOffset) { - ((CommandBufferVal*)&commandBuffer)->UpdateTopLevelAccelerationStructure(instanceNum, buffer, bufferOffset, flags, dst, src, scratch, scratchOffset); + ((CommandBufferVal&)commandBuffer).UpdateTopLevelAccelerationStructure(instanceNum, buffer, bufferOffset, flags, dst, src, scratch, scratchOffset); } static void NRI_CALL CmdUpdateBottomLevelAccelerationStructure(CommandBuffer& commandBuffer, uint32_t geometryObjectNum, const GeometryObject* geometryObjects, AccelerationStructureBuildBits flags, AccelerationStructure& dst, AccelerationStructure& src, Buffer& scratch, uint64_t scratchOffset) { - ((CommandBufferVal*)&commandBuffer)->UpdateBottomLevelAccelerationStructure(geometryObjectNum, geometryObjects, flags, dst, src, scratch, scratchOffset); + ((CommandBufferVal&)commandBuffer).UpdateBottomLevelAccelerationStructure(geometryObjectNum, geometryObjects, flags, dst, src, scratch, scratchOffset); } static void NRI_CALL CmdCopyAccelerationStructure(CommandBuffer& commandBuffer, AccelerationStructure& dst, AccelerationStructure& src, CopyMode mode) { - ((CommandBufferVal*)&commandBuffer)->CopyAccelerationStructure(dst, src, mode); + ((CommandBufferVal&)commandBuffer).CopyAccelerationStructure(dst, src, mode); } static void NRI_CALL CmdWriteAccelerationStructureSize(CommandBuffer& commandBuffer, const AccelerationStructure* const* accelerationStructures, uint32_t accelerationStructureNum, QueryPool& queryPool, uint32_t queryOffset) { - ((CommandBufferVal*)&commandBuffer)->WriteAccelerationStructureSize(accelerationStructures, accelerationStructureNum, queryPool, queryOffset); + ((CommandBufferVal&)commandBuffer).WriteAccelerationStructureSize(accelerationStructures, accelerationStructureNum, queryPool, queryOffset); } static void NRI_CALL CmdDispatchRays(CommandBuffer& commandBuffer, const DispatchRaysDesc& dispatchRaysDesc) { - ((CommandBufferVal*)&commandBuffer)->DispatchRays(dispatchRaysDesc); -} - -void FillFunctionTableCommandBufferVal(RayTracingInterface& rayTracingInterface) -{ - rayTracingInterface.CmdBuildTopLevelAccelerationStructure = ::CmdBuildTopLevelAccelerationStructure; - rayTracingInterface.CmdBuildBottomLevelAccelerationStructure = ::CmdBuildBottomLevelAccelerationStructure; - rayTracingInterface.CmdUpdateTopLevelAccelerationStructure = ::CmdUpdateTopLevelAccelerationStructure; - rayTracingInterface.CmdUpdateBottomLevelAccelerationStructure = ::CmdUpdateBottomLevelAccelerationStructure; - rayTracingInterface.CmdCopyAccelerationStructure = ::CmdCopyAccelerationStructure; - rayTracingInterface.CmdWriteAccelerationStructureSize = ::CmdWriteAccelerationStructureSize; - rayTracingInterface.CmdDispatchRays = ::CmdDispatchRays; - + ((CommandBufferVal&)commandBuffer).DispatchRays(dispatchRaysDesc); } #pragma endregion -#pragma region [ MeshShaderInterface ] +#pragma region [ MeshShader ] -static void NRI_CALL CmdDispatchMeshTasks(CommandBuffer& commandBuffer, uint32_t taskNum) +static void NRI_CALL CmdDispatchMeshTasks(CommandBuffer& commandBuffer, uint32_t x, uint32_t y, uint32_t z) { - ((CommandBufferVal&)commandBuffer).DispatchMeshTasks(taskNum); -} - -void FillFunctionTableCommandBufferVal(MeshShaderInterface& meshShaderInterface) -{ - meshShaderInterface.CmdDispatchMeshTasks = CmdDispatchMeshTasks; + ((CommandBufferVal&)commandBuffer).DispatchMeshTasks(x, y, z); } #pragma endregion + +Define_Core_CommandBuffer_PartiallyFillFunctionTable(Val) +Define_RayTracing_CommandBuffer_PartiallyFillFunctionTable(Val) +Define_MeshShader_CommandBuffer_PartiallyFillFunctionTable(Val) diff --git a/Source/Validation/CommandQueueVal.cpp b/Source/Validation/CommandQueueVal.cpp index a73f532f..16d967d2 100644 --- a/Source/Validation/CommandQueueVal.cpp +++ b/Source/Validation/CommandQueueVal.cpp @@ -13,8 +13,6 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include "CommandQueueVal.h" #include "CommandBufferVal.h" -#include "DeviceSemaphoreVal.h" -#include "QueueSemaphoreVal.h" #include "TextureVal.h" #include "BufferVal.h" #include "QueryPoolVal.h" @@ -38,49 +36,16 @@ void CommandQueueVal::SetDebugName(const char* name) m_CoreAPI.SetCommandQueueDebugName(m_ImplObject, name); } -void CommandQueueVal::Submit(const WorkSubmissionDesc& workSubmissionDesc, DeviceSemaphore* deviceSemaphore) +void CommandQueueVal::Submit(const QueueSubmitDesc& queueSubmitDesc) { - ProcessValidationCommands((const CommandBufferVal* const*)workSubmissionDesc.commandBuffers, workSubmissionDesc.commandBufferNum); - - auto workSubmissionDescImpl = workSubmissionDesc; - workSubmissionDescImpl.commandBuffers = STACK_ALLOC(CommandBuffer*, workSubmissionDesc.commandBufferNum); - for (uint32_t i = 0; i < workSubmissionDesc.commandBufferNum; i++) - ((CommandBuffer**)workSubmissionDescImpl.commandBuffers)[i] = NRI_GET_IMPL_PTR(CommandBuffer, workSubmissionDesc.commandBuffers[i]); - workSubmissionDescImpl.wait = STACK_ALLOC(QueueSemaphore*, workSubmissionDesc.waitNum); - for (uint32_t i = 0; i < workSubmissionDesc.waitNum; i++) - ((QueueSemaphore**)workSubmissionDescImpl.wait)[i] = NRI_GET_IMPL_PTR(QueueSemaphore, workSubmissionDesc.wait[i]); - workSubmissionDescImpl.signal = STACK_ALLOC(QueueSemaphore*, workSubmissionDesc.signalNum); - for (uint32_t i = 0; i < workSubmissionDesc.signalNum; i++) - ((QueueSemaphore**)workSubmissionDescImpl.signal)[i] = NRI_GET_IMPL_PTR(QueueSemaphore, workSubmissionDesc.signal[i]); - - DeviceSemaphore* deviceSemaphoreImpl = nullptr; - if (deviceSemaphore) - deviceSemaphoreImpl = NRI_GET_IMPL_PTR(DeviceSemaphore, deviceSemaphore); - - for (uint32_t i = 0; i < workSubmissionDesc.waitNum; i++) - { - QueueSemaphoreVal* semaphore = (QueueSemaphoreVal*)workSubmissionDesc.wait[i]; - semaphore->Wait(); - } - - m_CoreAPI.SubmitQueueWork(m_ImplObject, workSubmissionDescImpl, deviceSemaphoreImpl); + ProcessValidationCommands((const CommandBufferVal* const*)queueSubmitDesc.commandBuffers, queueSubmitDesc.commandBufferNum); - for (uint32_t i = 0; i < workSubmissionDesc.signalNum; i++) - { - QueueSemaphoreVal* semaphore = (QueueSemaphoreVal*)workSubmissionDesc.signal[i]; - semaphore->Signal(); - } - - if (deviceSemaphore) - ((DeviceSemaphoreVal*)deviceSemaphore)->Signal(); -} - -void CommandQueueVal::Wait(DeviceSemaphore& deviceSemaphore) -{ - ((DeviceSemaphoreVal&)deviceSemaphore).Wait(); - DeviceSemaphore* deviceSemaphoreImpl = NRI_GET_IMPL_REF(DeviceSemaphore, &deviceSemaphore); + auto queueSubmitDescImpl = queueSubmitDesc; + queueSubmitDescImpl.commandBuffers = STACK_ALLOC(CommandBuffer*, queueSubmitDesc.commandBufferNum); + for (uint32_t i = 0; i < queueSubmitDesc.commandBufferNum; i++) + ((CommandBuffer**)queueSubmitDescImpl.commandBuffers)[i] = NRI_GET_IMPL_PTR(CommandBuffer, queueSubmitDesc.commandBuffers[i]); - m_CoreAPI.WaitForSemaphore(m_ImplObject, *deviceSemaphoreImpl); + m_CoreAPI.QueueSubmit(m_ImplObject, queueSubmitDescImpl); } Result CommandQueueVal::ChangeResourceStates(const TransitionBarrierDesc& transitionBarriers) diff --git a/Source/Validation/CommandQueueVal.h b/Source/Validation/CommandQueueVal.h index 843f4681..94a4679a 100644 --- a/Source/Validation/CommandQueueVal.h +++ b/Source/Validation/CommandQueueVal.h @@ -12,30 +12,31 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct CommandBufferVal; - - struct CommandQueueVal : public DeviceObjectVal - { - CommandQueueVal(DeviceVal& device, CommandQueue& commandQueue); - - //====================================================================================================================== - // NRI - //====================================================================================================================== - void SetDebugName(const char* name); - void Submit(const WorkSubmissionDesc& workSubmissions, DeviceSemaphore* deviceSemaphore); - void Wait(DeviceSemaphore& deviceSemaphore); - - Result WaitForIdle(); - Result ChangeResourceStates(const TransitionBarrierDesc& transitionBarriers); - Result UploadData(const TextureUploadDesc* textureUploadDescs, uint32_t textureUploadDescNum, - const BufferUploadDesc* bufferUploadDescs, uint32_t bufferUploadDescNum); - - private: - void ProcessValidationCommands(const CommandBufferVal* const* commandBuffers, uint32_t commandBufferNum); - void ProcessValidationCommandBeginQuery(const uint8_t*& begin, const uint8_t* end); - void ProcessValidationCommandEndQuery(const uint8_t*& begin, const uint8_t* end); - void ProcessValidationCommandResetQuery(const uint8_t*& begin, const uint8_t* end); - - const HelperInterface& m_HelperAPI; - }; + +struct CommandBufferVal; + +struct CommandQueueVal : public DeviceObjectVal +{ + CommandQueueVal(DeviceVal& device, CommandQueue& commandQueue); + + //================================================================================================================ + // NRI + //================================================================================================================ + void SetDebugName(const char* name); + void Submit(const QueueSubmitDesc& queueSubmitDesc); + + Result WaitForIdle(); + Result ChangeResourceStates(const TransitionBarrierDesc& transitionBarriers); + Result UploadData(const TextureUploadDesc* textureUploadDescs, uint32_t textureUploadDescNum, + const BufferUploadDesc* bufferUploadDescs, uint32_t bufferUploadDescNum); + +private: + void ProcessValidationCommands(const CommandBufferVal* const* commandBuffers, uint32_t commandBufferNum); + void ProcessValidationCommandBeginQuery(const uint8_t*& begin, const uint8_t* end); + void ProcessValidationCommandEndQuery(const uint8_t*& begin, const uint8_t* end); + void ProcessValidationCommandResetQuery(const uint8_t*& begin, const uint8_t* end); + + const HelperInterface& m_HelperAPI; +}; + } diff --git a/Source/Validation/CommandQueueVal.hpp b/Source/Validation/CommandQueueVal.hpp index 828c665e..d0784a0d 100644 --- a/Source/Validation/CommandQueueVal.hpp +++ b/Source/Validation/CommandQueueVal.hpp @@ -8,55 +8,39 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ CoreInterface ] +#pragma region [ Core ] static void NRI_CALL SetCommandQueueDebugName(CommandQueue& commandQueue, const char* name) { - ((CommandQueueVal*)&commandQueue)->SetDebugName(name); + ((CommandQueueVal&)commandQueue).SetDebugName(name); } -static void NRI_CALL SubmitQueueWork(CommandQueue& commandQueue, const WorkSubmissionDesc& workSubmissionDesc, DeviceSemaphore* deviceSemaphore) +static void NRI_CALL QueueSubmit(CommandQueue& commandQueue, const QueueSubmitDesc& queueSubmitDesc) { - ((CommandQueueVal*)&commandQueue)->Submit(workSubmissionDesc, deviceSemaphore); -} - -static void NRI_CALL WaitForSemaphore(CommandQueue& commandQueue, DeviceSemaphore& deviceSemaphore) -{ - ((CommandQueueVal*)&commandQueue)->Wait(deviceSemaphore); -} - -void FillFunctionTableCommandQueueVal(CoreInterface& coreInterface) -{ - coreInterface.SetCommandQueueDebugName = ::SetCommandQueueDebugName; - coreInterface.SubmitQueueWork = ::SubmitQueueWork; - coreInterface.WaitForSemaphore = ::WaitForSemaphore; + ((CommandQueueVal&)commandQueue).Submit(queueSubmitDesc); } #pragma endregion -#pragma region [ HelperInterface ] +#pragma region [ Helper ] -static Result NRI_CALL ChangeResourceStatesVal(CommandQueue& commandQueue, const TransitionBarrierDesc& transitionBarriers) +static Result NRI_CALL ChangeResourceStates(CommandQueue& commandQueue, const TransitionBarrierDesc& transitionBarriers) { return ((CommandQueueVal&)commandQueue).ChangeResourceStates(transitionBarriers); } -static nri::Result NRI_CALL UploadDataVal(CommandQueue& commandQueue, const TextureUploadDesc* textureUploadDescs, uint32_t textureUploadDescNum, +static nri::Result NRI_CALL UploadData(CommandQueue& commandQueue, const TextureUploadDesc* textureUploadDescs, uint32_t textureUploadDescNum, const BufferUploadDesc* bufferUploadDescs, uint32_t bufferUploadDescNum) { return ((CommandQueueVal&)commandQueue).UploadData(textureUploadDescs, textureUploadDescNum, bufferUploadDescs, bufferUploadDescNum); } -static nri::Result NRI_CALL WaitForIdleVal(CommandQueue& commandQueue) +static nri::Result NRI_CALL WaitForIdle(CommandQueue& commandQueue) { return ((CommandQueueVal&)commandQueue).WaitForIdle(); } -void FillFunctionTableCommandQueueVal(HelperInterface& helperInterface) -{ - helperInterface.ChangeResourceStates = ::ChangeResourceStatesVal; - helperInterface.UploadData = ::UploadDataVal; - helperInterface.WaitForIdle = ::WaitForIdleVal; -} +#pragma endregion -#pragma endregion \ No newline at end of file +Define_Core_CommandQueue_PartiallyFillFunctionTable(Val) +Define_Helper_CommandQueue_PartiallyFillFunctionTable(Val) diff --git a/Source/Validation/DescriptorPoolVal.h b/Source/Validation/DescriptorPoolVal.h index 55997d11..41b8488f 100644 --- a/Source/Validation/DescriptorPoolVal.h +++ b/Source/Validation/DescriptorPoolVal.h @@ -12,40 +12,42 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct DescriptorSetVal; - - struct DescriptorPoolVal : public DeviceObjectVal - { - DescriptorPoolVal(DeviceVal& device, DescriptorPool& descriptorPool); - DescriptorPoolVal(DeviceVal& device, DescriptorPool& descriptorPool, const DescriptorPoolDesc& descriptorPoolDesc); - ~DescriptorPoolVal(); - - //====================================================================================================================== - // NRI - //====================================================================================================================== - void SetDebugName(const char* name); - void Reset(); - - Result AllocateDescriptorSets(const PipelineLayout& pipelineLayout, uint32_t setIndexInPipelineLayout, - DescriptorSet** descriptorSets, uint32_t instanceNum, uint32_t physicalDeviceMask, uint32_t variableDescriptorNum); - - private: - bool CheckDescriptorRange(const DescriptorRangeDesc& rangeDesc, uint32_t variableDescriptorNum); - void IncrementDescriptorNum(const DescriptorRangeDesc& rangeDesc, uint32_t variableDescriptorNum); - - Vector m_DescriptorSets; - DescriptorPoolDesc m_Desc = {}; - uint32_t m_DescriptorSetNum = 0; - uint32_t m_SamplerNum = 0; - uint32_t m_ConstantBufferNum = 0; - uint32_t m_DynamicConstantBufferNum = 0; - uint32_t m_TextureNum = 0; - uint32_t m_StorageTextureNum = 0; - uint32_t m_BufferNum = 0; - uint32_t m_StorageBufferNum = 0; - uint32_t m_StructuredBufferNum = 0; - uint32_t m_StorageStructuredBufferNum = 0; - uint32_t m_AccelerationStructureNum = 0; - bool m_SkipValidation = false; - }; + +struct DescriptorSetVal; + +struct DescriptorPoolVal : public DeviceObjectVal +{ + DescriptorPoolVal(DeviceVal& device, DescriptorPool& descriptorPool); + DescriptorPoolVal(DeviceVal& device, DescriptorPool& descriptorPool, const DescriptorPoolDesc& descriptorPoolDesc); + ~DescriptorPoolVal(); + + //================================================================================================================ + // NRI + //================================================================================================================ + void SetDebugName(const char* name); + void Reset(); + + Result AllocateDescriptorSets(const PipelineLayout& pipelineLayout, uint32_t setIndexInPipelineLayout, + DescriptorSet** descriptorSets, uint32_t instanceNum, uint32_t physicalDeviceMask, uint32_t variableDescriptorNum); + +private: + bool CheckDescriptorRange(const DescriptorRangeDesc& rangeDesc, uint32_t variableDescriptorNum); + void IncrementDescriptorNum(const DescriptorRangeDesc& rangeDesc, uint32_t variableDescriptorNum); + + Vector m_DescriptorSets; + DescriptorPoolDesc m_Desc = {}; + uint32_t m_DescriptorSetNum = 0; + uint32_t m_SamplerNum = 0; + uint32_t m_ConstantBufferNum = 0; + uint32_t m_DynamicConstantBufferNum = 0; + uint32_t m_TextureNum = 0; + uint32_t m_StorageTextureNum = 0; + uint32_t m_BufferNum = 0; + uint32_t m_StorageBufferNum = 0; + uint32_t m_StructuredBufferNum = 0; + uint32_t m_StorageStructuredBufferNum = 0; + uint32_t m_AccelerationStructureNum = 0; + bool m_SkipValidation = false; +}; + } diff --git a/Source/Validation/DescriptorPoolVal.hpp b/Source/Validation/DescriptorPoolVal.hpp index 67dc0e57..b15420fc 100644 --- a/Source/Validation/DescriptorPoolVal.hpp +++ b/Source/Validation/DescriptorPoolVal.hpp @@ -8,29 +8,24 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ CoreInterface ] +#pragma region [ Core ] static void NRI_CALL SetDescriptorPoolDebugName(DescriptorPool& descriptorPool, const char* name) { - ((DescriptorPoolVal*)&descriptorPool)->SetDebugName(name); + ((DescriptorPoolVal&)descriptorPool).SetDebugName(name); } static Result NRI_CALL AllocateDescriptorSets(DescriptorPool& descriptorPool, const PipelineLayout& pipelineLayout, uint32_t setIndexInPipelineLayout, DescriptorSet** descriptorSets, uint32_t instanceNum, uint32_t physicalDeviceMask, uint32_t variableDescriptorNum) { - return ((DescriptorPoolVal*)&descriptorPool)->AllocateDescriptorSets(pipelineLayout, setIndexInPipelineLayout, descriptorSets, instanceNum, physicalDeviceMask, variableDescriptorNum); + return ((DescriptorPoolVal&)descriptorPool).AllocateDescriptorSets(pipelineLayout, setIndexInPipelineLayout, descriptorSets, instanceNum, physicalDeviceMask, variableDescriptorNum); } static void NRI_CALL ResetDescriptorPool(DescriptorPool& descriptorPool) { - ((DescriptorPoolVal*)&descriptorPool)->Reset(); -} - -void FillFunctionTableDescriptorPoolVal(CoreInterface& coreInterface) -{ - coreInterface.SetDescriptorPoolDebugName = ::SetDescriptorPoolDebugName; - coreInterface.AllocateDescriptorSets = ::AllocateDescriptorSets; - coreInterface.ResetDescriptorPool = ::ResetDescriptorPool; + ((DescriptorPoolVal&)descriptorPool).Reset(); } #pragma endregion + +Define_Core_DescriptorPool_PartiallyFillFunctionTable(Val) diff --git a/Source/Validation/DescriptorSetVal.h b/Source/Validation/DescriptorSetVal.h index ff04e566..cd8df67c 100644 --- a/Source/Validation/DescriptorSetVal.h +++ b/Source/Validation/DescriptorSetVal.h @@ -12,22 +12,24 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct DescriptorSetVal : public DeviceObjectVal - { - DescriptorSetVal(DeviceVal& device, DescriptorSet& descriptorSet, const DescriptorSetDesc& descriptorSetDesc); - - inline const DescriptorSetDesc& GetDesc() const - { return m_Desc; } - - //====================================================================================================================== - // NRI - //====================================================================================================================== - void SetDebugName(const char* name); - void UpdateDescriptorRanges(uint32_t physicalDeviceMask, uint32_t rangeOffset, uint32_t rangeNum, const DescriptorRangeUpdateDesc* rangeUpdateDescs); - void UpdateDynamicConstantBuffers(uint32_t physicalDeviceMask, uint32_t baseBuffer, uint32_t bufferNum, const Descriptor* const* descriptors); - void Copy(const DescriptorSetCopyDesc& descriptorSetCopyDesc); - - private: - const DescriptorSetDesc& m_Desc; - }; + +struct DescriptorSetVal : public DeviceObjectVal +{ + DescriptorSetVal(DeviceVal& device, DescriptorSet& descriptorSet, const DescriptorSetDesc& descriptorSetDesc); + + inline const DescriptorSetDesc& GetDesc() const + { return m_Desc; } + + //================================================================================================================ + // NRI + //================================================================================================================ + void SetDebugName(const char* name); + void UpdateDescriptorRanges(uint32_t physicalDeviceMask, uint32_t rangeOffset, uint32_t rangeNum, const DescriptorRangeUpdateDesc* rangeUpdateDescs); + void UpdateDynamicConstantBuffers(uint32_t physicalDeviceMask, uint32_t baseBuffer, uint32_t bufferNum, const Descriptor* const* descriptors); + void Copy(const DescriptorSetCopyDesc& descriptorSetCopyDesc); + +private: + const DescriptorSetDesc& m_Desc; +}; + } diff --git a/Source/Validation/DescriptorSetVal.hpp b/Source/Validation/DescriptorSetVal.hpp index 7b4958ba..99a9c8f4 100644 --- a/Source/Validation/DescriptorSetVal.hpp +++ b/Source/Validation/DescriptorSetVal.hpp @@ -8,34 +8,29 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ CoreInterface ] +#pragma region [ Core ] static void NRI_CALL SetDescriptorSetDebugName(DescriptorSet& descriptorSet, const char* name) { - ((DescriptorSetVal*)&descriptorSet)->SetDebugName(name); + ((DescriptorSetVal&)descriptorSet).SetDebugName(name); } static void NRI_CALL UpdateDescriptorRanges(DescriptorSet& descriptorSet, uint32_t physicalDeviceMask, uint32_t baseRange, uint32_t rangeNum, const DescriptorRangeUpdateDesc* rangeUpdateDescs) { - ((DescriptorSetVal*)&descriptorSet)->UpdateDescriptorRanges(physicalDeviceMask, baseRange, rangeNum, rangeUpdateDescs); + ((DescriptorSetVal&)descriptorSet).UpdateDescriptorRanges(physicalDeviceMask, baseRange, rangeNum, rangeUpdateDescs); } static void NRI_CALL UpdateDynamicConstantBuffers(DescriptorSet& descriptorSet, uint32_t physicalDeviceMask, uint32_t baseBuffer, uint32_t bufferNum, const Descriptor* const* descriptors) { - ((DescriptorSetVal*)&descriptorSet)->UpdateDynamicConstantBuffers(physicalDeviceMask, baseBuffer, bufferNum, descriptors); + ((DescriptorSetVal&)descriptorSet).UpdateDynamicConstantBuffers(physicalDeviceMask, baseBuffer, bufferNum, descriptors); } static void NRI_CALL CopyDescriptorSet(DescriptorSet& descriptorSet, const DescriptorSetCopyDesc& descriptorSetCopyDesc) { - ((DescriptorSetVal*)&descriptorSet)->Copy(descriptorSetCopyDesc); -} - -void FillFunctionTableDescriptorSetVal(CoreInterface& coreInterface) -{ - coreInterface.SetDescriptorSetDebugName = ::SetDescriptorSetDebugName; - coreInterface.UpdateDescriptorRanges = ::UpdateDescriptorRanges; - coreInterface.UpdateDynamicConstantBuffers = ::UpdateDynamicConstantBuffers; - coreInterface.CopyDescriptorSet = ::CopyDescriptorSet; + ((DescriptorSetVal&)descriptorSet).Copy(descriptorSetCopyDesc); } #pragma endregion + +Define_Core_DescriptorSet_PartiallyFillFunctionTable(Val) + diff --git a/Source/Validation/DescriptorVal.h b/Source/Validation/DescriptorVal.h index f82c5ba7..16021c11 100644 --- a/Source/Validation/DescriptorVal.h +++ b/Source/Validation/DescriptorVal.h @@ -12,71 +12,73 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - enum class ResourceType - { - NONE, - BUFFER, - TEXTURE, - SAMPLER, - ACCELERATION_STRUCTURE - }; - - enum class ResourceViewType - { - NONE, - COLOR_ATTACHMENT, - DEPTH_STENCIL_ATTACHMENT, - SHADER_RESOURCE, - SHADER_RESOURCE_STORAGE, - CONSTANT_BUFFER_VIEW - }; - - struct DescriptorVal : public DeviceObjectVal - { - DescriptorVal(DeviceVal& device, Descriptor& descriptor, ResourceType resourceType); - DescriptorVal(DeviceVal& device, Descriptor& descriptor, const BufferViewDesc& bufferViewDesc); - DescriptorVal(DeviceVal& device, Descriptor& descriptor, const Texture1DViewDesc& textureViewDesc); - DescriptorVal(DeviceVal& device, Descriptor& descriptor, const Texture2DViewDesc& textureViewDesc); - DescriptorVal(DeviceVal& device, Descriptor& descriptor, const Texture3DViewDesc& textureViewDesc); - DescriptorVal(DeviceVal& device, Descriptor& descriptor); - - inline uint64_t GetNativeObject(uint32_t physicalDeviceIndex) const - { return m_CoreAPI.GetDescriptorNativeObject(m_ImplObject, physicalDeviceIndex); } - - inline bool IsBufferView() const - { return m_ResourceType == ResourceType::BUFFER; } - - inline bool IsTextureView() const - { return m_ResourceType == ResourceType::TEXTURE; } - - inline bool IsSampler() const - { return m_ResourceType == ResourceType::SAMPLER; } - - inline bool IsAccelerationStructure() const - { return m_ResourceType == ResourceType::ACCELERATION_STRUCTURE; } - - inline bool IsConstantBufferView() const - { return m_ResourceType == ResourceType::BUFFER && m_ResourceViewType == ResourceViewType::CONSTANT_BUFFER_VIEW; } - - inline bool IsColorAttachment() const - { return IsTextureView() && m_ResourceViewType == ResourceViewType::COLOR_ATTACHMENT; } - - inline bool IsDepthStencilAttachment() const - { return IsTextureView() && m_ResourceViewType == ResourceViewType::DEPTH_STENCIL_ATTACHMENT; } - - inline bool IsShaderResource() const - { return m_ResourceType != ResourceType::NONE && !IsSampler() && m_ResourceViewType == ResourceViewType::SHADER_RESOURCE; } - - inline bool IsShaderResourceStorage() const - { return m_ResourceType != ResourceType::NONE && !IsSampler() && m_ResourceViewType == ResourceViewType::SHADER_RESOURCE_STORAGE; } - - //====================================================================================================================== - // NRI - //====================================================================================================================== - void SetDebugName(const char* name); - - private: - ResourceType m_ResourceType = ResourceType::NONE; - ResourceViewType m_ResourceViewType = ResourceViewType::NONE; - }; + +enum class ResourceType +{ + NONE, + BUFFER, + TEXTURE, + SAMPLER, + ACCELERATION_STRUCTURE +}; + +enum class ResourceViewType +{ + NONE, + COLOR_ATTACHMENT, + DEPTH_STENCIL_ATTACHMENT, + SHADER_RESOURCE, + SHADER_RESOURCE_STORAGE, + CONSTANT_BUFFER_VIEW +}; + +struct DescriptorVal : public DeviceObjectVal +{ + DescriptorVal(DeviceVal& device, Descriptor& descriptor, ResourceType resourceType); + DescriptorVal(DeviceVal& device, Descriptor& descriptor, const BufferViewDesc& bufferViewDesc); + DescriptorVal(DeviceVal& device, Descriptor& descriptor, const Texture1DViewDesc& textureViewDesc); + DescriptorVal(DeviceVal& device, Descriptor& descriptor, const Texture2DViewDesc& textureViewDesc); + DescriptorVal(DeviceVal& device, Descriptor& descriptor, const Texture3DViewDesc& textureViewDesc); + DescriptorVal(DeviceVal& device, Descriptor& descriptor); + + inline uint64_t GetNativeObject(uint32_t physicalDeviceIndex) const + { return m_CoreAPI.GetDescriptorNativeObject(m_ImplObject, physicalDeviceIndex); } + + inline bool IsBufferView() const + { return m_ResourceType == ResourceType::BUFFER; } + + inline bool IsTextureView() const + { return m_ResourceType == ResourceType::TEXTURE; } + + inline bool IsSampler() const + { return m_ResourceType == ResourceType::SAMPLER; } + + inline bool IsAccelerationStructure() const + { return m_ResourceType == ResourceType::ACCELERATION_STRUCTURE; } + + inline bool IsConstantBufferView() const + { return m_ResourceType == ResourceType::BUFFER && m_ResourceViewType == ResourceViewType::CONSTANT_BUFFER_VIEW; } + + inline bool IsColorAttachment() const + { return IsTextureView() && m_ResourceViewType == ResourceViewType::COLOR_ATTACHMENT; } + + inline bool IsDepthStencilAttachment() const + { return IsTextureView() && m_ResourceViewType == ResourceViewType::DEPTH_STENCIL_ATTACHMENT; } + + inline bool IsShaderResource() const + { return m_ResourceType != ResourceType::NONE && !IsSampler() && m_ResourceViewType == ResourceViewType::SHADER_RESOURCE; } + + inline bool IsShaderResourceStorage() const + { return m_ResourceType != ResourceType::NONE && !IsSampler() && m_ResourceViewType == ResourceViewType::SHADER_RESOURCE_STORAGE; } + + //================================================================================================================ + // NRI + //================================================================================================================ + void SetDebugName(const char* name); + +private: + ResourceType m_ResourceType = ResourceType::NONE; + ResourceViewType m_ResourceViewType = ResourceViewType::NONE; +}; + } diff --git a/Source/Validation/DescriptorVal.hpp b/Source/Validation/DescriptorVal.hpp index 547d8c70..de14e968 100644 --- a/Source/Validation/DescriptorVal.hpp +++ b/Source/Validation/DescriptorVal.hpp @@ -8,7 +8,7 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ CoreInterface ] +#pragma region [ Core ] static void NRI_CALL SetDescriptorDebugName(Descriptor& descriptor, const char* name) { @@ -20,11 +20,6 @@ static uint64_t NRI_CALL GetDescriptorNativeObject(const Descriptor& descriptor, return ((DescriptorVal&)descriptor).GetNativeObject(physicalDeviceIndex); } -void FillFunctionTableDescriptorVal(CoreInterface& coreInterface) -{ - coreInterface.SetDescriptorDebugName = ::SetDescriptorDebugName; - - coreInterface.GetDescriptorNativeObject = ::GetDescriptorNativeObject; -} - #pragma endregion + +Define_Core_Descriptor_PartiallyFillFunctionTable(Val) diff --git a/Source/Validation/DeviceSemaphoreVal.cpp b/Source/Validation/DeviceSemaphoreVal.cpp deleted file mode 100644 index 9ba62dde..00000000 --- a/Source/Validation/DeviceSemaphoreVal.cpp +++ /dev/null @@ -1,49 +0,0 @@ -/* -Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. - -NVIDIA CORPORATION and its licensors retain all intellectual property -and proprietary rights in and to this software, related documentation -and any modifications thereto. Any use, reproduction, disclosure or -distribution of this software and related documentation without an express -license agreement from NVIDIA CORPORATION is strictly prohibited. -*/ - -#include "SharedExternal.h" -#include "SharedVal.h" -#include "DeviceSemaphoreVal.h" - -using namespace nri; - -DeviceSemaphoreVal::DeviceSemaphoreVal(DeviceVal& device, DeviceSemaphore& deviceSemaphore) : - DeviceObjectVal(device, deviceSemaphore) -{ -} - -void DeviceSemaphoreVal::Create(bool signaled) -{ - m_Value = signaled ? 1 : 0; -} - -void DeviceSemaphoreVal::Signal() -{ - if (!IsUnsignaled()) - REPORT_ERROR(m_Device.GetLog(), "Semaphore is already in signaled state!"); - - m_Value++; -} - -void DeviceSemaphoreVal::Wait() -{ - if (IsUnsignaled()) - REPORT_ERROR(m_Device.GetLog(), "Semaphore is already in unsignaled state!"); - - m_Value++; -} - -void DeviceSemaphoreVal::SetDebugName(const char* name) -{ - m_Name = name; - m_CoreAPI.SetDeviceSemaphoreDebugName(m_ImplObject, name); -} - -#include "DeviceSemaphoreVal.hpp" diff --git a/Source/Validation/DeviceSemaphoreVal.h b/Source/Validation/DeviceSemaphoreVal.h deleted file mode 100644 index f359f009..00000000 --- a/Source/Validation/DeviceSemaphoreVal.h +++ /dev/null @@ -1,39 +0,0 @@ -/* -Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. - -NVIDIA CORPORATION and its licensors retain all intellectual property -and proprietary rights in and to this software, related documentation -and any modifications thereto. Any use, reproduction, disclosure or -distribution of this software and related documentation without an express -license agreement from NVIDIA CORPORATION is strictly prohibited. -*/ - -#pragma once - -namespace nri -{ - struct DeviceSemaphoreVal : public DeviceObjectVal - { - DeviceSemaphoreVal(DeviceVal& device, DeviceSemaphore& deviceSemaphore); - - void Create(bool signaled); - - inline bool IsUnsignaled() const; - - //====================================================================================================================== - // NRI - //====================================================================================================================== - void Signal(); - void Wait(); - - void SetDebugName(const char* name); - - private: - uint64_t m_Value = 0; - }; - - inline bool DeviceSemaphoreVal::IsUnsignaled() const - { - return (m_Value & 0x1) == 0; - } -} diff --git a/Source/Validation/DeviceSemaphoreVal.hpp b/Source/Validation/DeviceSemaphoreVal.hpp deleted file mode 100644 index 03149061..00000000 --- a/Source/Validation/DeviceSemaphoreVal.hpp +++ /dev/null @@ -1,23 +0,0 @@ -/* -Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. - -NVIDIA CORPORATION and its licensors retain all intellectual property -and proprietary rights in and to this software, related documentation -and any modifications thereto. Any use, reproduction, disclosure or -distribution of this software and related documentation without an express -license agreement from NVIDIA CORPORATION is strictly prohibited. -*/ - -#pragma region [ CoreInterface ] - -static void NRI_CALL SetDeviceSemaphoreDebugName(DeviceSemaphore& deviceSemaphore, const char* name) -{ - ((DeviceSemaphoreVal*)&deviceSemaphore)->SetDebugName(name); -} - -void FillFunctionTableDeviceSemaphoreVal(CoreInterface& coreInterface) -{ - coreInterface.SetDeviceSemaphoreDebugName = ::SetDeviceSemaphoreDebugName; -} - -#pragma endregion diff --git a/Source/Validation/DeviceVal.cpp b/Source/Validation/DeviceVal.cpp index 2c5f3b49..8775ff4f 100644 --- a/Source/Validation/DeviceVal.cpp +++ b/Source/Validation/DeviceVal.cpp @@ -19,13 +19,12 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include "CommandBufferVal.h" #include "DescriptorVal.h" #include "DescriptorPoolVal.h" -#include "DeviceSemaphoreVal.h" +#include "FenceVal.h" #include "FrameBufferVal.h" #include "MemoryVal.h" #include "PipelineLayoutVal.h" #include "PipelineVal.h" #include "QueryPoolVal.h" -#include "QueueSemaphoreVal.h" #include "SwapChainVal.h" #include "TextureVal.h" #include "AccelerationStructureVal.h" @@ -690,31 +689,15 @@ Result DeviceVal::CreateQueryPool(const QueryPoolDesc& queryPoolDesc, QueryPool* return result; } -Result DeviceVal::CreateQueueSemaphore(QueueSemaphore*& queueSemaphore) +Result DeviceVal::CreateFence(uint64_t initialValue, Fence*& fence) { - QueueSemaphore* queueSemaphoreImpl; - const Result result = m_CoreAPI.CreateQueueSemaphore(m_Device, queueSemaphoreImpl); + Fence* fenceImpl; + const Result result = m_CoreAPI.CreateFence(m_Device, initialValue, fenceImpl); if (result == Result::SUCCESS) { - RETURN_ON_FAILURE(GetLog(), queueSemaphoreImpl != nullptr, Result::FAILURE, "Unexpected error: 'queueSemaphoreImpl' is NULL!"); - queueSemaphore = (QueueSemaphore*)Allocate(GetStdAllocator(), *this, *queueSemaphoreImpl); - } - - return result; -} - -Result DeviceVal::CreateDeviceSemaphore(bool signaled, DeviceSemaphore*& deviceSemaphore) -{ - DeviceSemaphore* deviceSemaphoreImpl; - const Result result = m_CoreAPI.CreateDeviceSemaphore(m_Device, signaled, deviceSemaphoreImpl); - - if (result == Result::SUCCESS) - { - RETURN_ON_FAILURE(GetLog(), deviceSemaphoreImpl != nullptr, Result::FAILURE, "Unexpected error: 'queueSemaphoreImpl' is NULL!"); - DeviceSemaphoreVal* deviceSemaphoreVal = Allocate(GetStdAllocator(), *this, *deviceSemaphoreImpl); - deviceSemaphoreVal->Create(signaled); - deviceSemaphore = (DeviceSemaphore*)deviceSemaphoreVal; + RETURN_ON_FAILURE(GetLog(), fenceImpl != nullptr, Result::FAILURE, "Unexpected error: 'fenceImpl' is NULL!"); + fence = (Fence*)Allocate(GetStdAllocator(), *this, *fenceImpl); } return result; @@ -774,16 +757,10 @@ void DeviceVal::DestroyQueryPool(QueryPool& queryPool) Deallocate(GetStdAllocator(), (QueryPoolVal*)&queryPool); } -void DeviceVal::DestroyQueueSemaphore(QueueSemaphore& queueSemaphore) +void DeviceVal::DestroyFence(Fence& fence) { - m_CoreAPI.DestroyQueueSemaphore(*NRI_GET_IMPL_REF(QueueSemaphore, &queueSemaphore)); - Deallocate(GetStdAllocator(), (QueueSemaphoreVal*)&queueSemaphore); -} - -void DeviceVal::DestroyDeviceSemaphore(DeviceSemaphore& deviceSemaphore) -{ - m_CoreAPI.DestroyDeviceSemaphore(*NRI_GET_IMPL_REF(DeviceSemaphore, &deviceSemaphore)); - Deallocate(GetStdAllocator(), (DeviceSemaphoreVal*)&deviceSemaphore); + m_CoreAPI.DestroyFence(*NRI_GET_IMPL_REF(Fence, &fence)); + Deallocate(GetStdAllocator(), (FenceVal*)&fence); } Result DeviceVal::AllocateMemory(uint32_t physicalDeviceMask, MemoryType memoryType, uint64_t size, Memory*& memory) @@ -1189,25 +1166,6 @@ Result DeviceVal::CreateQueryPoolVK(const QueryPoolVulkanDesc& queryPoolVulkanDe return result; } -Result DeviceVal::CreateQueueSemaphoreVK(NRIVkSemaphore vkSemaphore, QueueSemaphore*& queueSemaphore) -{ - RETURN_ON_FAILURE(GetLog(), vkSemaphore != 0, Result::INVALID_ARGUMENT, - "Can't create QueueSemaphore: 'vkSemaphore' is invalid."); - - QueueSemaphore* queueSemaphoreImpl = nullptr; - const Result result = m_WrapperVKAPI.CreateQueueSemaphoreVK(m_Device, vkSemaphore, queueSemaphoreImpl); - - if (result == Result::SUCCESS) - { - RETURN_ON_FAILURE(GetLog(), queueSemaphoreImpl != nullptr, Result::FAILURE, - "Can't create QueueSemaphore: unexpected error."); - - queueSemaphore = (QueueSemaphore*)Allocate(GetStdAllocator(), *this, *queueSemaphoreImpl); - } - - return result; -} - Result DeviceVal::CreateAccelerationStructureVK(const AccelerationStructureVulkanDesc& accelerationStructureDesc, AccelerationStructure*& accelerationStructure) { RETURN_ON_FAILURE(GetLog(), accelerationStructureDesc.vkAccelerationStructure != 0, Result::INVALID_ARGUMENT, @@ -1227,25 +1185,6 @@ Result DeviceVal::CreateAccelerationStructureVK(const AccelerationStructureVulka return result; } -Result DeviceVal::CreateDeviceSemaphoreVK(NRIVkFence vkFence, DeviceSemaphore*& deviceSemaphore) -{ - RETURN_ON_FAILURE(GetLog(), vkFence != 0, Result::INVALID_ARGUMENT, - "Can't create DeviceSemaphore: 'vkFence' is invalid."); - - DeviceSemaphore* deviceSemaphoreImpl = nullptr; - const Result result = m_WrapperVKAPI.CreateDeviceSemaphoreVK(m_Device, vkFence, deviceSemaphoreImpl); - - if (result == Result::SUCCESS) - { - RETURN_ON_FAILURE(GetLog(), deviceSemaphoreImpl != nullptr, Result::FAILURE, - "Can't create DeviceSemaphore: unexpected error."); - - deviceSemaphore = (DeviceSemaphore*)Allocate(GetStdAllocator(), *this, *deviceSemaphoreImpl); - } - - return result; -} - #endif #if NRI_USE_D3D11 diff --git a/Source/Validation/DeviceVal.h b/Source/Validation/DeviceVal.h index bf5444d1..384a342d 100644 --- a/Source/Validation/DeviceVal.h +++ b/Source/Validation/DeviceVal.h @@ -12,165 +12,163 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct CommandQueueVal; - - struct DeviceVal final : public DeviceBase - { - DeviceVal(const Log& log, const StdAllocator& stdAllocator, DeviceBase& device); - ~DeviceVal(); - - bool Create(); - void RegisterMemoryType(MemoryType memoryType, MemoryLocation memoryLocation); - - inline const CoreInterface& GetCoreInterface() const - { return m_CoreAPI; } - - inline const SwapChainInterface& GetSwapChainInterface() const - { return m_SwapChainAPI; } - - inline const WrapperD3D11Interface& GetWrapperD3D11Interface() const - { return m_WrapperD3D11API; } - - inline const WrapperD3D12Interface& GetWrapperD3D12Interface() const - { return m_WrapperD3D12API; } - - inline const WrapperVKInterface& GetWrapperVKInterface() const - { return m_WrapperVKAPI; } - - inline const RayTracingInterface& GetRayTracingInterface() const - { return m_RayTracingAPI; } - - inline const MeshShaderInterface& GetMeshShaderInterface() const - { return m_MeshShaderAPI; } - - inline const HelperInterface& GetHelperInterface() const - { return m_HelperAPI; } - - inline void* GetNativeObject() const - { return m_CoreAPI.GetDeviceNativeObject(m_Device); } - - NRIVkPhysicalDevice GetVkPhysicalDevice() const - { return m_WrapperVKAPI.GetVkPhysicalDevice(m_Device); } - - NRIVkInstance GetVkInstance() const - { return m_WrapperVKAPI.GetVkInstance(m_Device); } - - NRIVkProcAddress GetVkGetInstanceProcAddr() const - { return m_WrapperVKAPI.GetVkGetInstanceProcAddr(m_Device); } - - NRIVkProcAddress GetVkGetDeviceProcAddr() const - { return m_WrapperVKAPI.GetVkGetDeviceProcAddr(m_Device); } - - inline Lock& GetLock() - { return m_Lock; } - - //====================================================================================================================== - // NRI - //====================================================================================================================== - Result CreateSwapChain(const SwapChainDesc& swapChainDesc, SwapChain*& swapChain); - void DestroySwapChain(SwapChain& swapChain); - Result GetDisplays(Display** displays, uint32_t& displayNum); - Result GetDisplaySize(Display& display, uint16_t& width, uint16_t& height); - void SetDebugName(const char* name); - const DeviceDesc& GetDesc() const; - Result GetCommandQueue(CommandQueueType commandQueueType, CommandQueue*& commandQueue); - Result CreateCommandAllocator(const CommandQueue& commandQueue, uint32_t physicalDeviceMask, CommandAllocator*& commandAllocator); - Result CreateDescriptorPool(const DescriptorPoolDesc& descriptorPoolDesc, DescriptorPool*& descriptorPool); - Result CreateBuffer(const BufferDesc& bufferDesc, Buffer*& buffer); - Result CreateTexture(const TextureDesc& textureDesc, Texture*& texture); - Result CreateDescriptor(const BufferViewDesc& bufferViewDesc, Descriptor*& bufferView); - Result CreateDescriptor(const Texture1DViewDesc& textureViewDesc, Descriptor*& textureView); - Result CreateDescriptor(const Texture2DViewDesc& textureViewDesc, Descriptor*& textureView); - Result CreateDescriptor(const Texture3DViewDesc& textureViewDesc, Descriptor*& textureView); - Result CreateDescriptor(const SamplerDesc& samplerDesc, Descriptor*& sampler); - Result CreatePipelineLayout(const PipelineLayoutDesc& pipelineLayoutDesc, PipelineLayout*& pipelineLayout); - Result CreatePipeline(const GraphicsPipelineDesc& graphicsPipelineDesc, Pipeline*& pipeline); - Result CreatePipeline(const ComputePipelineDesc& computePipelineDesc, Pipeline*& pipeline); - Result CreateFrameBuffer(const FrameBufferDesc& frameBufferDesc, FrameBuffer*& frameBuffer); - Result CreateQueryPool(const QueryPoolDesc& queryPoolDesc, QueryPool*& queryPool); - Result CreateQueueSemaphore(QueueSemaphore*& queueSemaphore); - Result CreateDeviceSemaphore(bool signaled, DeviceSemaphore*& deviceSemaphore); - void DestroyCommandAllocator(CommandAllocator& commandAllocator); - void DestroyDescriptorPool(DescriptorPool& descriptorPool); - void DestroyBuffer(Buffer& buffer); - void DestroyTexture(Texture& texture); - void DestroyDescriptor(Descriptor& descriptor); - void DestroyPipelineLayout(PipelineLayout& pipelineLayout); - void DestroyPipeline(Pipeline& pipeline); - void DestroyFrameBuffer(FrameBuffer& frameBuffer); - void DestroyQueryPool(QueryPool& queryPool); - void DestroyQueueSemaphore(QueueSemaphore& queueSemaphore); - void DestroyDeviceSemaphore(DeviceSemaphore& deviceSemaphore); - Result AllocateMemory(uint32_t physicalDeviceMask, MemoryType memoryType, uint64_t size, Memory*& memory); - Result BindBufferMemory(const BufferMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum); - Result BindTextureMemory(const TextureMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum); - void FreeMemory(Memory& memory); - - Result CreateRayTracingPipeline(const RayTracingPipelineDesc& pipelineDesc, Pipeline*& pipeline); - Result CreateAccelerationStructure(const AccelerationStructureDesc& accelerationStructureDesc, AccelerationStructure*& accelerationStructure); - Result BindAccelerationStructureMemory(const AccelerationStructureMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum); - void DestroyAccelerationStructure(AccelerationStructure& accelerationStructure); - FormatSupportBits GetFormatSupport(Format format) const; - - Result CreateCommandQueueVK(const CommandQueueVulkanDesc& commandQueueDesc, CommandQueue*& commandQueue); - Result CreateCommandAllocatorVK(const CommandAllocatorVulkanDesc& commandAllocatorDesc, CommandAllocator*& commandAllocator); - Result CreateCommandBufferVK(const CommandBufferVulkanDesc& commandBufferDesc, CommandBuffer*& commandBuffer); - Result CreateDescriptorPoolVK(NRIVkDescriptorPool vkDescriptorPool, DescriptorPool*& descriptorPool); - Result CreateBufferVK(const BufferVulkanDesc& bufferDesc, Buffer*& buffer); - Result CreateTextureVK(const TextureVulkanDesc& textureVulkanDesc, Texture*& texture); - Result CreateMemoryVK(const MemoryVulkanDesc& memoryVulkanDesc, Memory*& memory); - Result CreateGraphicsPipelineVK(NRIVkPipeline vkPipeline, Pipeline*& pipeline); - Result CreateComputePipelineVK(NRIVkPipeline vkPipeline, Pipeline*& pipeline); - Result CreateQueryPoolVK(const QueryPoolVulkanDesc& queryPoolVulkanDesc, QueryPool*& queryPool); - Result CreateQueueSemaphoreVK(NRIVkSemaphore vkSemaphore, QueueSemaphore*& queueSemaphore); - Result CreateDeviceSemaphoreVK(NRIVkFence vkFence, DeviceSemaphore*& deviceSemaphore); - Result CreateAccelerationStructureVK(const AccelerationStructureVulkanDesc& accelerationStructureDesc, AccelerationStructure*& accelerationStructure); - - Result CreateCommandBufferD3D11(const CommandBufferD3D11Desc& commandBufferDesc, CommandBuffer*& commandBuffer); - Result CreateBufferD3D11(const BufferD3D11Desc& bufferDesc, Buffer*& buffer); - Result CreateTextureD3D11(const TextureD3D11Desc& textureDesc, Texture*& texture); - - Result CreateCommandBufferD3D12(const CommandBufferD3D12Desc& commandBufferDesc, CommandBuffer*& commandBuffer); - Result CreateBufferD3D12(const BufferD3D12Desc& bufferDesc, Buffer*& buffer); - Result CreateTextureD3D12(const TextureD3D12Desc& textureDesc, Texture*& texture); - Result CreateMemoryD3D12(const MemoryD3D12Desc& memoryDesc, Memory*& memory); - Result CreateAccelerationStructureD3D12(const AccelerationStructureD3D12Desc& accelerationStructureDesc, AccelerationStructure*& accelerationStructure); - - uint32_t CalculateAllocationNumber(const ResourceGroupDesc& resourceGroupDesc) const; - Result AllocateAndBindMemory(const ResourceGroupDesc& resourceGroupDesc, Memory** allocations); - - //================================================================================================================ - // DeviceBase - //================================================================================================================ - void Destroy(); - Result FillFunctionTable(CoreInterface& table) const; - Result FillFunctionTable(SwapChainInterface& table) const; - Result FillFunctionTable(WrapperD3D11Interface& table) const; - Result FillFunctionTable(WrapperD3D12Interface& table) const; - Result FillFunctionTable(WrapperVKInterface& table) const; - Result FillFunctionTable(RayTracingInterface& table) const; - Result FillFunctionTable(MeshShaderInterface& table) const; - Result FillFunctionTable(HelperInterface& table) const; - - private: - Device& m_Device; - String m_Name; - CoreInterface m_CoreAPI = {}; - SwapChainInterface m_SwapChainAPI = {}; - WrapperD3D11Interface m_WrapperD3D11API = {}; - WrapperD3D12Interface m_WrapperD3D12API = {}; - WrapperVKInterface m_WrapperVKAPI = {}; - RayTracingInterface m_RayTracingAPI = {}; - MeshShaderInterface m_MeshShaderAPI = {}; - HelperInterface m_HelperAPI = {}; - std::array m_CommandQueues = {}; - UnorderedMap m_MemoryTypeMap; - Lock m_Lock; - bool m_IsSwapChainSupported = false; - bool m_IsWrapperD3D11Supported = false; - bool m_IsWrapperD3D12Supported = false; - bool m_IsWrapperVKSupported = false; - bool m_IsRayTracingSupported = false; - bool m_IsMeshShaderExtSupported = false; - }; + +struct CommandQueueVal; + +struct DeviceVal final : public DeviceBase +{ + DeviceVal(const Log& log, const StdAllocator& stdAllocator, DeviceBase& device); + ~DeviceVal(); + + bool Create(); + void RegisterMemoryType(MemoryType memoryType, MemoryLocation memoryLocation); + + inline const CoreInterface& GetCoreInterface() const + { return m_CoreAPI; } + + inline const SwapChainInterface& GetSwapChainInterface() const + { return m_SwapChainAPI; } + + inline const WrapperD3D11Interface& GetWrapperD3D11Interface() const + { return m_WrapperD3D11API; } + + inline const WrapperD3D12Interface& GetWrapperD3D12Interface() const + { return m_WrapperD3D12API; } + + inline const WrapperVKInterface& GetWrapperVKInterface() const + { return m_WrapperVKAPI; } + + inline const RayTracingInterface& GetRayTracingInterface() const + { return m_RayTracingAPI; } + + inline const MeshShaderInterface& GetMeshShaderInterface() const + { return m_MeshShaderAPI; } + + inline const HelperInterface& GetHelperInterface() const + { return m_HelperAPI; } + + inline void* GetNativeObject() const + { return m_CoreAPI.GetDeviceNativeObject(m_Device); } + + NRIVkPhysicalDevice GetVkPhysicalDevice() const + { return m_WrapperVKAPI.GetVkPhysicalDevice(m_Device); } + + NRIVkInstance GetVkInstance() const + { return m_WrapperVKAPI.GetVkInstance(m_Device); } + + NRIVkProcAddress GetVkGetInstanceProcAddr() const + { return m_WrapperVKAPI.GetVkGetInstanceProcAddr(m_Device); } + + NRIVkProcAddress GetVkGetDeviceProcAddr() const + { return m_WrapperVKAPI.GetVkGetDeviceProcAddr(m_Device); } + + inline Lock& GetLock() + { return m_Lock; } + + //================================================================================================================ + // NRI + //================================================================================================================ + Result CreateSwapChain(const SwapChainDesc& swapChainDesc, SwapChain*& swapChain); + void DestroySwapChain(SwapChain& swapChain); + Result GetDisplays(Display** displays, uint32_t& displayNum); + Result GetDisplaySize(Display& display, uint16_t& width, uint16_t& height); + void SetDebugName(const char* name); + const DeviceDesc& GetDesc() const; + Result GetCommandQueue(CommandQueueType commandQueueType, CommandQueue*& commandQueue); + Result CreateCommandAllocator(const CommandQueue& commandQueue, uint32_t physicalDeviceMask, CommandAllocator*& commandAllocator); + Result CreateDescriptorPool(const DescriptorPoolDesc& descriptorPoolDesc, DescriptorPool*& descriptorPool); + Result CreateBuffer(const BufferDesc& bufferDesc, Buffer*& buffer); + Result CreateTexture(const TextureDesc& textureDesc, Texture*& texture); + Result CreateDescriptor(const BufferViewDesc& bufferViewDesc, Descriptor*& bufferView); + Result CreateDescriptor(const Texture1DViewDesc& textureViewDesc, Descriptor*& textureView); + Result CreateDescriptor(const Texture2DViewDesc& textureViewDesc, Descriptor*& textureView); + Result CreateDescriptor(const Texture3DViewDesc& textureViewDesc, Descriptor*& textureView); + Result CreateDescriptor(const SamplerDesc& samplerDesc, Descriptor*& sampler); + Result CreatePipelineLayout(const PipelineLayoutDesc& pipelineLayoutDesc, PipelineLayout*& pipelineLayout); + Result CreatePipeline(const GraphicsPipelineDesc& graphicsPipelineDesc, Pipeline*& pipeline); + Result CreatePipeline(const ComputePipelineDesc& computePipelineDesc, Pipeline*& pipeline); + Result CreateFrameBuffer(const FrameBufferDesc& frameBufferDesc, FrameBuffer*& frameBuffer); + Result CreateQueryPool(const QueryPoolDesc& queryPoolDesc, QueryPool*& queryPool); + Result CreateFence(uint64_t initialValue, Fence*& fence); + void DestroyCommandAllocator(CommandAllocator& commandAllocator); + void DestroyDescriptorPool(DescriptorPool& descriptorPool); + void DestroyBuffer(Buffer& buffer); + void DestroyTexture(Texture& texture); + void DestroyDescriptor(Descriptor& descriptor); + void DestroyPipelineLayout(PipelineLayout& pipelineLayout); + void DestroyPipeline(Pipeline& pipeline); + void DestroyFrameBuffer(FrameBuffer& frameBuffer); + void DestroyQueryPool(QueryPool& queryPool); + void DestroyFence(Fence& queueSemaphore); + Result AllocateMemory(uint32_t physicalDeviceMask, MemoryType memoryType, uint64_t size, Memory*& memory); + Result BindBufferMemory(const BufferMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum); + Result BindTextureMemory(const TextureMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum); + void FreeMemory(Memory& memory); + + Result CreateRayTracingPipeline(const RayTracingPipelineDesc& pipelineDesc, Pipeline*& pipeline); + Result CreateAccelerationStructure(const AccelerationStructureDesc& accelerationStructureDesc, AccelerationStructure*& accelerationStructure); + Result BindAccelerationStructureMemory(const AccelerationStructureMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum); + void DestroyAccelerationStructure(AccelerationStructure& accelerationStructure); + FormatSupportBits GetFormatSupport(Format format) const; + + Result CreateCommandQueueVK(const CommandQueueVulkanDesc& commandQueueDesc, CommandQueue*& commandQueue); + Result CreateCommandAllocatorVK(const CommandAllocatorVulkanDesc& commandAllocatorDesc, CommandAllocator*& commandAllocator); + Result CreateCommandBufferVK(const CommandBufferVulkanDesc& commandBufferDesc, CommandBuffer*& commandBuffer); + Result CreateDescriptorPoolVK(NRIVkDescriptorPool vkDescriptorPool, DescriptorPool*& descriptorPool); + Result CreateBufferVK(const BufferVulkanDesc& bufferDesc, Buffer*& buffer); + Result CreateTextureVK(const TextureVulkanDesc& textureVulkanDesc, Texture*& texture); + Result CreateMemoryVK(const MemoryVulkanDesc& memoryVulkanDesc, Memory*& memory); + Result CreateGraphicsPipelineVK(NRIVkPipeline vkPipeline, Pipeline*& pipeline); + Result CreateComputePipelineVK(NRIVkPipeline vkPipeline, Pipeline*& pipeline); + Result CreateQueryPoolVK(const QueryPoolVulkanDesc& queryPoolVulkanDesc, QueryPool*& queryPool); + Result CreateAccelerationStructureVK(const AccelerationStructureVulkanDesc& accelerationStructureDesc, AccelerationStructure*& accelerationStructure); + + Result CreateCommandBufferD3D11(const CommandBufferD3D11Desc& commandBufferDesc, CommandBuffer*& commandBuffer); + Result CreateBufferD3D11(const BufferD3D11Desc& bufferDesc, Buffer*& buffer); + Result CreateTextureD3D11(const TextureD3D11Desc& textureDesc, Texture*& texture); + + Result CreateCommandBufferD3D12(const CommandBufferD3D12Desc& commandBufferDesc, CommandBuffer*& commandBuffer); + Result CreateBufferD3D12(const BufferD3D12Desc& bufferDesc, Buffer*& buffer); + Result CreateTextureD3D12(const TextureD3D12Desc& textureDesc, Texture*& texture); + Result CreateMemoryD3D12(const MemoryD3D12Desc& memoryDesc, Memory*& memory); + Result CreateAccelerationStructureD3D12(const AccelerationStructureD3D12Desc& accelerationStructureDesc, AccelerationStructure*& accelerationStructure); + + uint32_t CalculateAllocationNumber(const ResourceGroupDesc& resourceGroupDesc) const; + Result AllocateAndBindMemory(const ResourceGroupDesc& resourceGroupDesc, Memory** allocations); + + //================================================================================================================ + // DeviceBase + //================================================================================================================ + void Destroy(); + Result FillFunctionTable(CoreInterface& table) const; + Result FillFunctionTable(SwapChainInterface& table) const; + Result FillFunctionTable(WrapperD3D11Interface& table) const; + Result FillFunctionTable(WrapperD3D12Interface& table) const; + Result FillFunctionTable(WrapperVKInterface& table) const; + Result FillFunctionTable(RayTracingInterface& table) const; + Result FillFunctionTable(MeshShaderInterface& table) const; + Result FillFunctionTable(HelperInterface& table) const; + +private: + Device& m_Device; + String m_Name; + CoreInterface m_CoreAPI = {}; + SwapChainInterface m_SwapChainAPI = {}; + WrapperD3D11Interface m_WrapperD3D11API = {}; + WrapperD3D12Interface m_WrapperD3D12API = {}; + WrapperVKInterface m_WrapperVKAPI = {}; + RayTracingInterface m_RayTracingAPI = {}; + MeshShaderInterface m_MeshShaderAPI = {}; + HelperInterface m_HelperAPI = {}; + std::array m_CommandQueues = {}; + UnorderedMap m_MemoryTypeMap; + Lock m_Lock; + bool m_IsSwapChainSupported = false; + bool m_IsWrapperD3D11Supported = false; + bool m_IsWrapperD3D12Supported = false; + bool m_IsWrapperVKSupported = false; + bool m_IsRayTracingSupported = false; + bool m_IsMeshShaderExtSupported = false; +}; + } diff --git a/Source/Validation/DeviceVal.hpp b/Source/Validation/DeviceVal.hpp index 21c04f1c..16029d6b 100644 --- a/Source/Validation/DeviceVal.hpp +++ b/Source/Validation/DeviceVal.hpp @@ -8,11 +8,13 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ CoreInterface ] +Declare_PartiallyFillFunctionTable_Functions(Val) + +#pragma region [ Core ] static const DeviceDesc& NRI_CALL GetDeviceDesc(const Device& device) { - return ((const DeviceVal*)&device)->GetDesc(); + return ((const DeviceVal&)device).GetDesc(); } static Result NRI_CALL GetCommandQueue(Device& device, CommandQueueType commandQueueType, CommandQueue*& commandQueue) @@ -110,14 +112,9 @@ static Result NRI_CALL CreateQueryPool(Device& device, const QueryPoolDesc& quer return ((DeviceVal&)device).CreateQueryPool(queryPoolDesc, queryPool); } -static Result NRI_CALL CreateQueueSemaphore(Device& device, QueueSemaphore*& queueSemaphore) -{ - return ((DeviceVal&)device).CreateQueueSemaphore(queueSemaphore); -} - -static Result NRI_CALL CreateDeviceSemaphore(Device& device, bool signaled, DeviceSemaphore*& deviceSemaphore) +static Result NRI_CALL CreateFence(Device& device, uint64_t initialValue, Fence*& fence) { - return ((DeviceVal&)device).CreateDeviceSemaphore(signaled, deviceSemaphore); + return ((DeviceVal&)device).CreateFence(initialValue, fence); } static void NRI_CALL DestroyCommandAllocator(CommandAllocator& commandAllocator) @@ -165,14 +162,9 @@ static void NRI_CALL DestroyQueryPool(QueryPool& queryPool) GetDeviceVal(queryPool).DestroyQueryPool(queryPool); } -static void NRI_CALL DestroyQueueSemaphore(QueueSemaphore& queueSemaphore) -{ - GetDeviceVal(queueSemaphore).DestroyQueueSemaphore(queueSemaphore); -} - -static void NRI_CALL DestroyDeviceSemaphore(DeviceSemaphore& deviceSemaphore) +static void NRI_CALL DestroyFence(Fence& fence) { - GetDeviceVal(deviceSemaphore).DestroyDeviceSemaphore(deviceSemaphore); + GetDeviceVal(fence).DestroyFence(fence); } static Result NRI_CALL AllocateMemory(Device& device, uint32_t physicalDeviceMask, MemoryType memoryType, uint64_t size, Memory*& memory) @@ -205,51 +197,37 @@ static void NRI_CALL SetDeviceDebugName(Device& device, const char* name) ((DeviceVal&)device).SetDebugName(name); } +static void NRI_CALL SetPipelineDebugName(Pipeline& pipeline, const char* name) +{ + ((PipelineVal&)pipeline).SetDebugName(name); +} + +static void NRI_CALL SetPipelineLayoutDebugName(PipelineLayout& pipelineLayout, const char* name) +{ + ((PipelineLayoutVal&)pipelineLayout).SetDebugName(name); +} + +static void NRI_CALL SetFrameBufferDebugName(FrameBuffer& frameBuffer, const char* name) +{ + ((FrameBufferVal&)frameBuffer).SetDebugName(name); +} + +static void NRI_CALL SetMemoryDebugName(Memory& memory, const char* name) +{ + ((MemoryVal&)memory).SetDebugName(name); +} + static void* NRI_CALL GetDeviceNativeObject(const Device& device) { - return ((DeviceVal*)&device)->GetNativeObject(); -} - -void FillFunctionTableBufferVal(CoreInterface& coreInterface); -void FillFunctionTableCommandAllocatorVal(CoreInterface& coreInterface); -void FillFunctionTableCommandBufferVal(CoreInterface& coreInterface); -void FillFunctionTableCommandQueueVal(CoreInterface& coreInterface); -void FillFunctionTableDescriptorVal(CoreInterface& coreInterface); -void FillFunctionTableDescriptorPoolVal(CoreInterface& coreInterface); -void FillFunctionTableDescriptorSetVal(CoreInterface& coreInterface); -void FillFunctionTableDeviceSemaphoreVal(CoreInterface& coreInterface); -void FillFunctionTableFrameBufferVal(CoreInterface& coreInterface); -void FillFunctionTableMemoryVal(CoreInterface& coreInterface); -void FillFunctionTablePipelineLayoutVal(CoreInterface& coreInterface); -void FillFunctionTablePipelineVal(CoreInterface& coreInterface); -void FillFunctionTableQueryPoolVal(CoreInterface& coreInterface); -void FillFunctionTableQueueSemaphoreVal(CoreInterface& coreInterface); -void FillFunctionTableTextureVal(CoreInterface& coreInterface); + return ((DeviceVal&)device).GetNativeObject(); +} Result DeviceVal::FillFunctionTable(CoreInterface& coreInterface) const { coreInterface = {}; - - FillFunctionTableBufferVal(coreInterface); - FillFunctionTableCommandAllocatorVal(coreInterface); - FillFunctionTableCommandBufferVal(coreInterface); - FillFunctionTableCommandQueueVal(coreInterface); - FillFunctionTableDescriptorVal(coreInterface); - FillFunctionTableDescriptorPoolVal(coreInterface); - FillFunctionTableDescriptorSetVal(coreInterface); - FillFunctionTableDeviceSemaphoreVal(coreInterface); - FillFunctionTableFrameBufferVal(coreInterface); - FillFunctionTableMemoryVal(coreInterface); - FillFunctionTablePipelineLayoutVal(coreInterface); - FillFunctionTablePipelineVal(coreInterface); - FillFunctionTableQueryPoolVal(coreInterface); - FillFunctionTableQueueSemaphoreVal(coreInterface); - FillFunctionTableTextureVal(coreInterface); - coreInterface.GetDeviceDesc = ::GetDeviceDesc; coreInterface.GetFormatSupport = ::GetFormatSupport; coreInterface.GetCommandQueue = ::GetCommandQueue; - coreInterface.CreateCommandAllocator = ::CreateCommandAllocator; coreInterface.CreateDescriptorPool = ::CreateDescriptorPool; coreInterface.CreateBuffer = ::CreateBuffer; @@ -264,9 +242,7 @@ Result DeviceVal::FillFunctionTable(CoreInterface& coreInterface) const coreInterface.CreateComputePipeline = ::CreateComputePipeline; coreInterface.CreateFrameBuffer = ::CreateFrameBuffer; coreInterface.CreateQueryPool = ::CreateQueryPool; - coreInterface.CreateQueueSemaphore = ::CreateQueueSemaphore; - coreInterface.CreateDeviceSemaphore = ::CreateDeviceSemaphore; - + coreInterface.CreateFence = ::CreateFence; coreInterface.DestroyCommandAllocator = ::DestroyCommandAllocator; coreInterface.DestroyDescriptorPool = ::DestroyDescriptorPool; coreInterface.DestroyBuffer = ::DestroyBuffer; @@ -276,24 +252,35 @@ Result DeviceVal::FillFunctionTable(CoreInterface& coreInterface) const coreInterface.DestroyPipeline = ::DestroyPipeline; coreInterface.DestroyFrameBuffer = ::DestroyFrameBuffer; coreInterface.DestroyQueryPool = ::DestroyQueryPool; - coreInterface.DestroyQueueSemaphore = ::DestroyQueueSemaphore; - coreInterface.DestroyDeviceSemaphore = ::DestroyDeviceSemaphore; - + coreInterface.DestroyFence = ::DestroyFence; coreInterface.AllocateMemory = ::AllocateMemory; coreInterface.BindBufferMemory = ::BindBufferMemory; coreInterface.BindTextureMemory = ::BindTextureMemory; coreInterface.FreeMemory = ::FreeMemory; - coreInterface.SetDeviceDebugName = ::SetDeviceDebugName; - + coreInterface.SetPipelineDebugName = ::SetPipelineDebugName; + coreInterface.SetPipelineLayoutDebugName = ::SetPipelineLayoutDebugName; + coreInterface.SetFrameBufferDebugName = ::SetFrameBufferDebugName; + coreInterface.SetMemoryDebugName = ::SetMemoryDebugName; coreInterface.GetDeviceNativeObject = ::GetDeviceNativeObject; + Core_Buffer_PartiallyFillFunctionTableVal(coreInterface); + Core_CommandAllocator_PartiallyFillFunctionTableVal(coreInterface); + Core_CommandBuffer_PartiallyFillFunctionTableVal(coreInterface); + Core_CommandQueue_PartiallyFillFunctionTableVal(coreInterface); + Core_Descriptor_PartiallyFillFunctionTableVal(coreInterface); + Core_DescriptorPool_PartiallyFillFunctionTableVal(coreInterface); + Core_DescriptorSet_PartiallyFillFunctionTableVal(coreInterface); + Core_Fence_PartiallyFillFunctionTableVal(coreInterface); + Core_QueryPool_PartiallyFillFunctionTableVal(coreInterface); + Core_Texture_PartiallyFillFunctionTableVal(coreInterface); + return ValidateFunctionTable(GetLog(), coreInterface); } #pragma endregion -#pragma region [ SwapChainInterface ] +#pragma region [ SwapChain ] static Result NRI_CALL CreateSwapChain(Device& device, const SwapChainDesc& swapChainDesc, SwapChain*& swapChain) { @@ -315,28 +302,25 @@ static Result NRI_CALL GetDisplaySize(Device& device, Display& display, uint16_t return ((DeviceVal&)device).GetDisplaySize(display, width, height); } -void FillFunctionTableSwapChainVal(SwapChainInterface& swapChainInterface); - Result DeviceVal::FillFunctionTable(SwapChainInterface& swapChainInterface) const { if (!m_IsSwapChainSupported) return Result::UNSUPPORTED; swapChainInterface = {}; - - FillFunctionTableSwapChainVal(swapChainInterface); - swapChainInterface.CreateSwapChain = ::CreateSwapChain; swapChainInterface.DestroySwapChain = ::DestroySwapChain; swapChainInterface.GetDisplays = ::GetDisplays; swapChainInterface.GetDisplaySize = ::GetDisplaySize; + SwapChain_PartiallyFillFunctionTableVal(swapChainInterface); + return ValidateFunctionTable(GetLog(), swapChainInterface); } #pragma endregion -#pragma region [ WrapperD3D11Interface ] +#pragma region [ WrapperD3D11 ] #if NRI_USE_D3D11 @@ -359,25 +343,23 @@ static Result NRI_CALL CreateTextureD3D11(Device& device, const TextureD3D11Desc Result DeviceVal::FillFunctionTable(WrapperD3D11Interface& wrapperD3D11Interface) const { - MaybeUnused(wrapperD3D11Interface); - #if NRI_USE_D3D11 - wrapperD3D11Interface = {}; wrapperD3D11Interface.CreateCommandBufferD3D11 = ::CreateCommandBufferD3D11; wrapperD3D11Interface.CreateBufferD3D11 = ::CreateBufferD3D11; wrapperD3D11Interface.CreateTextureD3D11 = ::CreateTextureD3D11; return ValidateFunctionTable(GetLog(), wrapperD3D11Interface); - #else + MaybeUnused(wrapperD3D11Interface); + return Result::UNSUPPORTED; #endif } #pragma endregion -#pragma region [ WrapperD3D12Interface ] +#pragma region [ WrapperD3D12 ] #if NRI_USE_D3D12 @@ -410,10 +392,7 @@ static Result NRI_CALL CreateAccelerationStructureD3D12(Device& device, const Ac Result DeviceVal::FillFunctionTable(WrapperD3D12Interface& wrapperD3D12Interface) const { - MaybeUnused(wrapperD3D12Interface); - #if NRI_USE_D3D12 - wrapperD3D12Interface = {}; wrapperD3D12Interface.CreateCommandBufferD3D12 = ::CreateCommandBufferD3D12; wrapperD3D12Interface.CreateBufferD3D12 = ::CreateBufferD3D12; @@ -424,13 +403,15 @@ Result DeviceVal::FillFunctionTable(WrapperD3D12Interface& wrapperD3D12Interface return ValidateFunctionTable(GetLog(), wrapperD3D12Interface); #else + MaybeUnused(wrapperD3D12Interface); + return Result::UNSUPPORTED; #endif } #pragma endregion -#pragma region [ WrapperVKInterface ] +#pragma region [ WrapperVK ] #if NRI_USE_VULKAN @@ -484,16 +465,6 @@ static Result NRI_CALL CreateQueryPoolVK(Device& device, const QueryPoolVulkanDe return ((DeviceVal&)device).CreateQueryPoolVK(queryPoolVulkanDesc, queryPool); } -static Result NRI_CALL CreateQueueSemaphoreVK(Device& device, NRIVkSemaphore vkSemaphore, QueueSemaphore*& queueSemaphore) -{ - return ((DeviceVal&)device).CreateQueueSemaphoreVK(vkSemaphore, queueSemaphore); -} - -static Result NRI_CALL CreateDeviceSemaphoreVK(Device& device, NRIVkFence vkFence, DeviceSemaphore*& deviceSemaphore) -{ - return ((DeviceVal&)device).CreateDeviceSemaphoreVK(vkFence, deviceSemaphore); -} - static Result NRI_CALL CreateAccelerationStructureVK(Device& device, const AccelerationStructureVulkanDesc& accelerationStructureDesc, AccelerationStructure*& accelerationStructure) { return ((DeviceVal&)device).CreateAccelerationStructureVK(accelerationStructureDesc, accelerationStructure); @@ -524,7 +495,6 @@ static NRIVkInstance NRI_CALL GetVkGetDeviceProcAddr(const Device& device) Result DeviceVal::FillFunctionTable(WrapperVKInterface& wrapperVKInterface) const { #if NRI_USE_VULKAN - wrapperVKInterface = {}; wrapperVKInterface.CreateCommandQueueVK = ::CreateCommandQueueVK; wrapperVKInterface.CreateCommandAllocatorVK = ::CreateCommandAllocatorVK; @@ -536,8 +506,6 @@ Result DeviceVal::FillFunctionTable(WrapperVKInterface& wrapperVKInterface) cons wrapperVKInterface.CreateGraphicsPipelineVK = ::CreateGraphicsPipelineVK; wrapperVKInterface.CreateComputePipelineVK = ::CreateComputePipelineVK; wrapperVKInterface.CreateQueryPoolVK = ::CreateQueryPoolVK; - wrapperVKInterface.CreateQueueSemaphoreVK = ::CreateQueueSemaphoreVK; - wrapperVKInterface.CreateDeviceSemaphoreVK = ::CreateDeviceSemaphoreVK; wrapperVKInterface.CreateAccelerationStructureVK = ::CreateAccelerationStructureVK; wrapperVKInterface.GetVkPhysicalDevice = ::GetVkPhysicalDevice; @@ -546,15 +514,16 @@ Result DeviceVal::FillFunctionTable(WrapperVKInterface& wrapperVKInterface) cons wrapperVKInterface.GetVkGetInstanceProcAddr = ::GetVkGetInstanceProcAddr; return ValidateFunctionTable(GetLog(), wrapperVKInterface); - #else + MaybeUnused(wrapperVKInterface); + return Result::UNSUPPORTED; #endif } #pragma endregion -#pragma region [ RayTracingInterface ] +#pragma region [ RayTracing ] static Result NRI_CALL CreateRayTracingPipeline(Device& device, const RayTracingPipelineDesc& pipelineDesc, Pipeline*& pipeline) { @@ -577,8 +546,6 @@ static void NRI_CALL DestroyAccelerationStructure(AccelerationStructure& acceler } void FillFunctionTablePipelineVal(RayTracingInterface& rayTracingInterface); -void FillFunctionTableCommandBufferVal(RayTracingInterface& rayTracingInterface); -void FillFunctionTableAccelarationStructureVal(RayTracingInterface& rayTracingInterface); Result DeviceVal::FillFunctionTable(RayTracingInterface& rayTracingInterface) const { @@ -591,18 +558,16 @@ Result DeviceVal::FillFunctionTable(RayTracingInterface& rayTracingInterface) co rayTracingInterface.BindAccelerationStructureMemory = ::BindAccelerationStructureMemory; rayTracingInterface.DestroyAccelerationStructure = ::DestroyAccelerationStructure; + RayTracing_CommandBuffer_PartiallyFillFunctionTableVal(rayTracingInterface); + RayTracing_AccelerationStructure_PartiallyFillFunctionTableVal(rayTracingInterface); FillFunctionTablePipelineVal(rayTracingInterface); - FillFunctionTableCommandBufferVal(rayTracingInterface); - FillFunctionTableAccelarationStructureVal(rayTracingInterface); return ValidateFunctionTable(GetLog(), rayTracingInterface); } #pragma endregion -#pragma region [ MeshShaderInterface ] - -void FillFunctionTableCommandBufferVal(MeshShaderInterface& meshShaderInterface); +#pragma region [ MeshShader ] Result DeviceVal::FillFunctionTable(MeshShaderInterface& meshShaderInterface) const { @@ -611,34 +576,32 @@ Result DeviceVal::FillFunctionTable(MeshShaderInterface& meshShaderInterface) co meshShaderInterface = {}; - FillFunctionTableCommandBufferVal(meshShaderInterface); + MeshShader_CommandBuffer_PartiallyFillFunctionTableVal(meshShaderInterface); return ValidateFunctionTable(GetLog(), meshShaderInterface); } #pragma endregion -#pragma region [ HelperInterface ] +#pragma region [ Helper ] -static uint32_t NRI_CALL CountAllocationNumVal(Device& device, const ResourceGroupDesc& resourceGroupDesc) +static uint32_t NRI_CALL CountAllocationNum(Device& device, const ResourceGroupDesc& resourceGroupDesc) { return ((DeviceVal&)device).CalculateAllocationNumber(resourceGroupDesc); } -static Result NRI_CALL AllocateAndBindMemoryVal(Device& device, const ResourceGroupDesc& resourceGroupDesc, Memory** allocations) +static Result NRI_CALL AllocateAndBindMemory(Device& device, const ResourceGroupDesc& resourceGroupDesc, Memory** allocations) { return ((DeviceVal&)device).AllocateAndBindMemory(resourceGroupDesc, allocations); } -void FillFunctionTableCommandQueueVal(HelperInterface& helperInterface); - Result DeviceVal::FillFunctionTable(HelperInterface& helperInterface) const { helperInterface = {}; + helperInterface.CalculateAllocationNumber = ::CountAllocationNum; + helperInterface.AllocateAndBindMemory = ::AllocateAndBindMemory; - helperInterface.CalculateAllocationNumber = ::CountAllocationNumVal; - helperInterface.AllocateAndBindMemory = ::AllocateAndBindMemoryVal; - FillFunctionTableCommandQueueVal(helperInterface); + Helper_CommandQueue_PartiallyFillFunctionTableVal(helperInterface); return ValidateFunctionTable(GetLog(), helperInterface); } diff --git a/Source/Validation/FenceVal.cpp b/Source/Validation/FenceVal.cpp new file mode 100644 index 00000000..a88e8bfe --- /dev/null +++ b/Source/Validation/FenceVal.cpp @@ -0,0 +1,48 @@ +/* +Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. + +NVIDIA CORPORATION and its licensors retain all intellectual property +and proprietary rights in and to this software, related documentation +and any modifications thereto. Any use, reproduction, disclosure or +distribution of this software and related documentation without an express +license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "SharedExternal.h" +#include "SharedVal.h" +#include "FenceVal.h" +#include "CommandQueueVal.h" + +using namespace nri; + +//================================================================================================================ +// NRI +//================================================================================================================ + +inline uint64_t FenceVal::GetFenceValue() const +{ + return m_CoreAPI.GetFenceValue(m_ImplObject); +} + +inline void FenceVal::QueueSignal(CommandQueueVal& commandQueue, uint64_t value) +{ + m_CoreAPI.QueueSignal(commandQueue.GetImpl(), m_ImplObject, value); +} + +inline void FenceVal::QueueWait(CommandQueueVal& commandQueue, uint64_t value) +{ + m_CoreAPI.QueueWait(commandQueue.GetImpl(), m_ImplObject, value); +} + +inline void FenceVal::Wait(uint64_t value) +{ + m_CoreAPI.Wait(m_ImplObject, value); +} + +inline void FenceVal::SetDebugName(const char* name) +{ + m_Name = name; + m_CoreAPI.SetFenceDebugName(m_ImplObject, name); +} + +#include "FenceVal.hpp" diff --git a/Source/Validation/FenceVal.h b/Source/Validation/FenceVal.h new file mode 100644 index 00000000..a9a58be6 --- /dev/null +++ b/Source/Validation/FenceVal.h @@ -0,0 +1,37 @@ +/* +Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. + +NVIDIA CORPORATION and its licensors retain all intellectual property +and proprietary rights in and to this software, related documentation +and any modifications thereto. Any use, reproduction, disclosure or +distribution of this software and related documentation without an express +license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#pragma once + +namespace nri +{ + +struct CommandQueueVal; + +struct FenceVal : public DeviceObjectVal +{ + inline FenceVal(DeviceVal& device, Fence& queueSemaphore) : DeviceObjectVal(device, queueSemaphore) + {} + + inline ~FenceVal() + {} + + //================================================================================================================ + // NRI + //================================================================================================================ + + uint64_t GetFenceValue() const; + void QueueSignal(CommandQueueVal& commandQueue, uint64_t value); + void QueueWait(CommandQueueVal& commandQueue, uint64_t value); + void Wait(uint64_t value); + void SetDebugName(const char* name); +}; + +} diff --git a/Source/Validation/FenceVal.hpp b/Source/Validation/FenceVal.hpp new file mode 100644 index 00000000..a02c2f8f --- /dev/null +++ b/Source/Validation/FenceVal.hpp @@ -0,0 +1,41 @@ +/* +Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. + +NVIDIA CORPORATION and its licensors retain all intellectual property +and proprietary rights in and to this software, related documentation +and any modifications thereto. Any use, reproduction, disclosure or +distribution of this software and related documentation without an express +license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#pragma region [ Core ] + +static uint64_t NRI_CALL GetFenceValue(Fence& fence) +{ + return ((FenceVal&)fence).GetFenceValue(); +} + +static void NRI_CALL QueueSignal(CommandQueue& commandQueue, Fence& fence, uint64_t value) +{ + return ((FenceVal&)fence).QueueSignal((CommandQueueVal&)commandQueue, value); +} + +static void NRI_CALL QueueWait(CommandQueue& commandQueue, Fence& fence, uint64_t value) +{ + return ((FenceVal&)fence).QueueWait((CommandQueueVal&)commandQueue, value); +} + +static void NRI_CALL Wait(Fence& fence, uint64_t value) +{ + ((FenceVal&)fence).Wait(value); +} + +static void NRI_CALL SetFenceDebugName(Fence& fence, const char* name) +{ + ((FenceVal&)fence).SetDebugName(name); +} + +#pragma endregion + +Define_Core_Fence_PartiallyFillFunctionTable(Val) + diff --git a/Source/Validation/FrameBufferVal.cpp b/Source/Validation/FrameBufferVal.cpp index a3b65f8f..c4486931 100644 --- a/Source/Validation/FrameBufferVal.cpp +++ b/Source/Validation/FrameBufferVal.cpp @@ -24,5 +24,3 @@ void FrameBufferVal::SetDebugName(const char* name) m_Name = name; m_CoreAPI.SetFrameBufferDebugName(m_ImplObject, name); } - -#include "FrameBufferVal.hpp" diff --git a/Source/Validation/FrameBufferVal.h b/Source/Validation/FrameBufferVal.h index 2738c470..63026fd6 100644 --- a/Source/Validation/FrameBufferVal.h +++ b/Source/Validation/FrameBufferVal.h @@ -12,13 +12,15 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct FrameBufferVal : public DeviceObjectVal - { - FrameBufferVal(DeviceVal& device, FrameBuffer& frameBuffer); - //====================================================================================================================== - // NRI - //====================================================================================================================== - void SetDebugName(const char* name); - }; +struct FrameBufferVal : public DeviceObjectVal +{ + FrameBufferVal(DeviceVal& device, FrameBuffer& frameBuffer); + + //================================================================================================================ + // NRI + //================================================================================================================ + void SetDebugName(const char* name); +}; + } diff --git a/Source/Validation/FrameBufferVal.hpp b/Source/Validation/FrameBufferVal.hpp deleted file mode 100644 index b42e0be6..00000000 --- a/Source/Validation/FrameBufferVal.hpp +++ /dev/null @@ -1,23 +0,0 @@ -/* -Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. - -NVIDIA CORPORATION and its licensors retain all intellectual property -and proprietary rights in and to this software, related documentation -and any modifications thereto. Any use, reproduction, disclosure or -distribution of this software and related documentation without an express -license agreement from NVIDIA CORPORATION is strictly prohibited. -*/ - -#pragma region [ CoreInterface ] - -static void NRI_CALL SetFrameBufferDebugName(FrameBuffer& frameBuffer, const char* name) -{ - ((FrameBufferVal*)&frameBuffer)->SetDebugName(name); -} - -void FillFunctionTableFrameBufferVal(CoreInterface& coreInterface) -{ - coreInterface.SetFrameBufferDebugName = ::SetFrameBufferDebugName; -} - -#pragma endregion diff --git a/Source/Validation/MemoryVal.cpp b/Source/Validation/MemoryVal.cpp index 76120ffa..78d7f1d6 100644 --- a/Source/Validation/MemoryVal.cpp +++ b/Source/Validation/MemoryVal.cpp @@ -132,5 +132,3 @@ void MemoryVal::SetDebugName(const char* name) m_Name = name; m_CoreAPI.SetMemoryDebugName(m_ImplObject, name); } - -#include "MemoryVal.hpp" diff --git a/Source/Validation/MemoryVal.h b/Source/Validation/MemoryVal.h index a3fac1e5..702cf454 100644 --- a/Source/Validation/MemoryVal.h +++ b/Source/Validation/MemoryVal.h @@ -12,43 +12,45 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct BufferVal; - struct TextureVal; - struct AccelerationStructureVal; - - struct MemoryVal : public DeviceObjectVal - { - MemoryVal(DeviceVal& device, Memory& memory, uint64_t size, MemoryLocation memoryLocation); - MemoryVal(DeviceVal& device, Memory& memory, const MemoryD3D12Desc& memoryD3D12Desc); - - bool HasBoundResources(); - void ReportBoundResources(); - - inline uint64_t GetSize() const - { return m_Size; } - - inline MemoryLocation GetMemoryLocation() const - { return m_MemoryLocation; } - - //====================================================================================================================== - // NRI - //====================================================================================================================== - void BindBuffer(BufferVal& buffer); - void BindTexture(TextureVal& texture); - void BindAccelerationStructure(AccelerationStructureVal& accelerationStructure); - void UnbindBuffer(BufferVal& buffer); - void UnbindTexture(TextureVal& texture); - void UnbindAccelerationStructure(AccelerationStructureVal& accelerationStructure); - - void SetDebugName(const char* name); - - private: - std::vector m_Buffers; - std::vector m_Textures; - std::vector m_AccelerationStructures; - Lock m_Lock; - - uint64_t m_Size = 0; - MemoryLocation m_MemoryLocation = MemoryLocation::MAX_NUM; - }; + +struct BufferVal; +struct TextureVal; +struct AccelerationStructureVal; + +struct MemoryVal : public DeviceObjectVal +{ + MemoryVal(DeviceVal& device, Memory& memory, uint64_t size, MemoryLocation memoryLocation); + MemoryVal(DeviceVal& device, Memory& memory, const MemoryD3D12Desc& memoryD3D12Desc); + + bool HasBoundResources(); + void ReportBoundResources(); + + inline uint64_t GetSize() const + { return m_Size; } + + inline MemoryLocation GetMemoryLocation() const + { return m_MemoryLocation; } + + //================================================================================================================ + // NRI + //================================================================================================================ + void BindBuffer(BufferVal& buffer); + void BindTexture(TextureVal& texture); + void BindAccelerationStructure(AccelerationStructureVal& accelerationStructure); + void UnbindBuffer(BufferVal& buffer); + void UnbindTexture(TextureVal& texture); + void UnbindAccelerationStructure(AccelerationStructureVal& accelerationStructure); + + void SetDebugName(const char* name); + +private: + std::vector m_Buffers; + std::vector m_Textures; + std::vector m_AccelerationStructures; + Lock m_Lock; + + uint64_t m_Size = 0; + MemoryLocation m_MemoryLocation = MemoryLocation::MAX_NUM; +}; + } diff --git a/Source/Validation/MemoryVal.hpp b/Source/Validation/MemoryVal.hpp deleted file mode 100644 index 2b1e493e..00000000 --- a/Source/Validation/MemoryVal.hpp +++ /dev/null @@ -1,23 +0,0 @@ -/* -Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. - -NVIDIA CORPORATION and its licensors retain all intellectual property -and proprietary rights in and to this software, related documentation -and any modifications thereto. Any use, reproduction, disclosure or -distribution of this software and related documentation without an express -license agreement from NVIDIA CORPORATION is strictly prohibited. -*/ - -#pragma region [ CoreInterface ] - -static void NRI_CALL SetMemoryDebugName(Memory& memory, const char* name) -{ - ((MemoryVal*)&memory)->SetDebugName(name); -} - -void FillFunctionTableMemoryVal(CoreInterface& coreInterface) -{ - coreInterface.SetMemoryDebugName = ::SetMemoryDebugName; -} - -#pragma endregion diff --git a/Source/Validation/PipelineLayoutVal.cpp b/Source/Validation/PipelineLayoutVal.cpp index f5e544ab..763801a6 100644 --- a/Source/Validation/PipelineLayoutVal.cpp +++ b/Source/Validation/PipelineLayoutVal.cpp @@ -66,5 +66,3 @@ void PipelineLayoutVal::SetDebugName(const char* name) m_Name = name; m_CoreAPI.SetPipelineLayoutDebugName(m_ImplObject, name); } - -#include "PipelineLayoutVal.hpp" diff --git a/Source/Validation/PipelineLayoutVal.h b/Source/Validation/PipelineLayoutVal.h index d091efe6..14e275f0 100644 --- a/Source/Validation/PipelineLayoutVal.h +++ b/Source/Validation/PipelineLayoutVal.h @@ -12,23 +12,25 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct PipelineLayoutVal : public DeviceObjectVal - { - PipelineLayoutVal(DeviceVal& device, PipelineLayout& pipelineLayout, const PipelineLayoutDesc& pipelineLayoutDesc); - - inline const PipelineLayoutDesc& GetPipelineLayoutDesc() const - { return m_PipelineLayoutDesc; } - - //====================================================================================================================== - // NRI - //====================================================================================================================== - void SetDebugName(const char* name); - - private: - PipelineLayoutDesc m_PipelineLayoutDesc; - Vector m_DescriptorSets; - Vector m_PushConstants; - Vector m_DescriptorRangeDescs; - Vector m_DynamicConstantBufferDescs; - }; + +struct PipelineLayoutVal : public DeviceObjectVal +{ + PipelineLayoutVal(DeviceVal& device, PipelineLayout& pipelineLayout, const PipelineLayoutDesc& pipelineLayoutDesc); + + inline const PipelineLayoutDesc& GetPipelineLayoutDesc() const + { return m_PipelineLayoutDesc; } + + //================================================================================================================ + // NRI + //================================================================================================================ + void SetDebugName(const char* name); + +private: + PipelineLayoutDesc m_PipelineLayoutDesc; + Vector m_DescriptorSets; + Vector m_PushConstants; + Vector m_DescriptorRangeDescs; + Vector m_DynamicConstantBufferDescs; +}; + } diff --git a/Source/Validation/PipelineLayoutVal.hpp b/Source/Validation/PipelineLayoutVal.hpp deleted file mode 100644 index 4ba9f5d7..00000000 --- a/Source/Validation/PipelineLayoutVal.hpp +++ /dev/null @@ -1,23 +0,0 @@ -/* -Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. - -NVIDIA CORPORATION and its licensors retain all intellectual property -and proprietary rights in and to this software, related documentation -and any modifications thereto. Any use, reproduction, disclosure or -distribution of this software and related documentation without an express -license agreement from NVIDIA CORPORATION is strictly prohibited. -*/ - -#pragma region [ CoreInterface ] - -static void NRI_CALL SetPipelineLayoutDebugName(PipelineLayout& pipelineLayout, const char* name) -{ - ((PipelineLayoutVal*)&pipelineLayout)->SetDebugName(name); -} - -void FillFunctionTablePipelineLayoutVal(CoreInterface& coreInterface) -{ - coreInterface.SetPipelineLayoutDebugName = ::SetPipelineLayoutDebugName; -} - -#pragma endregion diff --git a/Source/Validation/PipelineVal.h b/Source/Validation/PipelineVal.h index 6cde69db..0d5731c5 100644 --- a/Source/Validation/PipelineVal.h +++ b/Source/Validation/PipelineVal.h @@ -12,24 +12,26 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct PipelineVal : public DeviceObjectVal - { - PipelineVal(DeviceVal& device, Pipeline& pipeline); - PipelineVal(DeviceVal& device, Pipeline& pipeline, const GraphicsPipelineDesc& graphicsPipelineDesc); - PipelineVal(DeviceVal& device, Pipeline& pipeline, const ComputePipelineDesc& computePipelineDesc); - PipelineVal(DeviceVal& device, Pipeline& pipeline, const RayTracingPipelineDesc& rayTracingPipelineDesc); - - inline const PipelineLayout* GetPipelineLayout() const - { return m_PipelineLayout; } - - //====================================================================================================================== - // NRI - //====================================================================================================================== - void SetDebugName(const char* name); - Result WriteShaderGroupIdentifiers(uint32_t baseShaderGroupIndex, uint32_t shaderGroupNum, void* buffer); - - private: - const RayTracingInterface& m_RayTracingAPI; - const PipelineLayout* m_PipelineLayout = nullptr; - }; + +struct PipelineVal : public DeviceObjectVal +{ + PipelineVal(DeviceVal& device, Pipeline& pipeline); + PipelineVal(DeviceVal& device, Pipeline& pipeline, const GraphicsPipelineDesc& graphicsPipelineDesc); + PipelineVal(DeviceVal& device, Pipeline& pipeline, const ComputePipelineDesc& computePipelineDesc); + PipelineVal(DeviceVal& device, Pipeline& pipeline, const RayTracingPipelineDesc& rayTracingPipelineDesc); + + inline const PipelineLayout* GetPipelineLayout() const + { return m_PipelineLayout; } + + //================================================================================================================ + // NRI + //================================================================================================================ + void SetDebugName(const char* name); + Result WriteShaderGroupIdentifiers(uint32_t baseShaderGroupIndex, uint32_t shaderGroupNum, void* buffer); + +private: + const RayTracingInterface& m_RayTracingAPI; + const PipelineLayout* m_PipelineLayout = nullptr; +}; + } diff --git a/Source/Validation/PipelineVal.hpp b/Source/Validation/PipelineVal.hpp index 47df2a60..cbaad758 100644 --- a/Source/Validation/PipelineVal.hpp +++ b/Source/Validation/PipelineVal.hpp @@ -8,17 +8,7 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ CoreInterface ] - -static void NRI_CALL SetPipelineDebugName(Pipeline& pipeline, const char* name) -{ - ((PipelineVal*)&pipeline)->SetDebugName(name); -} - -void FillFunctionTablePipelineVal(CoreInterface& coreInterface) -{ - coreInterface.SetPipelineDebugName = ::SetPipelineDebugName; -} +#pragma region [ RayTracing ] static Result NRI_CALL WriteShaderGroupIdentifiers(const Pipeline& pipeline, uint32_t baseShaderGroupIndex, uint32_t shaderGroupNum, void* buffer) { diff --git a/Source/Validation/QueryPoolVal.h b/Source/Validation/QueryPoolVal.h index 61c8a563..8a44beb1 100644 --- a/Source/Validation/QueryPoolVal.h +++ b/Source/Validation/QueryPoolVal.h @@ -12,31 +12,33 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct QueryPoolVal : public DeviceObjectVal - { - QueryPoolVal(DeviceVal& device, QueryPool& queryPool, QueryType queryType, uint32_t queryNum); - bool SetQueryState(uint32_t offset, bool state); - void ResetQueries(uint32_t offset, uint32_t number); +struct QueryPoolVal : public DeviceObjectVal +{ + QueryPoolVal(DeviceVal& device, QueryPool& queryPool, QueryType queryType, uint32_t queryNum); + + bool SetQueryState(uint32_t offset, bool state); + void ResetQueries(uint32_t offset, uint32_t number); + + inline uint32_t GetQueryNum() const + { return m_QueryNum; } - inline uint32_t GetQueryNum() const - { return m_QueryNum; } + inline QueryType GetQueryType() const + { return m_QueryType; } - inline QueryType GetQueryType() const - { return m_QueryType; } + inline bool IsImported() const + { return m_QueryNum == 0; } - inline bool IsImported() const - { return m_QueryNum == 0; } + //================================================================================================================ + // NRI + //================================================================================================================ + void SetDebugName(const char* name); + uint32_t GetQuerySize() const; - //====================================================================================================================== - // NRI - //====================================================================================================================== - void SetDebugName(const char* name); - uint32_t GetQuerySize() const; +private: + Vector m_DeviceState; + uint32_t m_QueryNum; + QueryType m_QueryType; +}; - private: - Vector m_DeviceState; - uint32_t m_QueryNum; - QueryType m_QueryType; - }; } diff --git a/Source/Validation/QueryPoolVal.hpp b/Source/Validation/QueryPoolVal.hpp index 14dd74f3..b156a353 100644 --- a/Source/Validation/QueryPoolVal.hpp +++ b/Source/Validation/QueryPoolVal.hpp @@ -8,22 +8,18 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ CoreInterface ] +#pragma region [ Core ] static void NRI_CALL SetQueryPoolDebugName(QueryPool& queryPool, const char* name) { - ((QueryPoolVal*)&queryPool)->SetDebugName(name); + ((QueryPoolVal&)queryPool).SetDebugName(name); } static uint32_t NRI_CALL GetQuerySize(const QueryPool& queryPool) { - return ((QueryPoolVal*)&queryPool)->GetQuerySize(); -} - -void FillFunctionTableQueryPoolVal(CoreInterface& coreInterface) -{ - coreInterface.SetQueryPoolDebugName = ::SetQueryPoolDebugName; - coreInterface.GetQuerySize = ::GetQuerySize; + return ((QueryPoolVal&)queryPool).GetQuerySize(); } #pragma endregion + +Define_Core_QueryPool_PartiallyFillFunctionTable(Val) diff --git a/Source/Validation/QueueSemaphoreVal.cpp b/Source/Validation/QueueSemaphoreVal.cpp deleted file mode 100644 index b0550df9..00000000 --- a/Source/Validation/QueueSemaphoreVal.cpp +++ /dev/null @@ -1,44 +0,0 @@ -/* -Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. - -NVIDIA CORPORATION and its licensors retain all intellectual property -and proprietary rights in and to this software, related documentation -and any modifications thereto. Any use, reproduction, disclosure or -distribution of this software and related documentation without an express -license agreement from NVIDIA CORPORATION is strictly prohibited. -*/ - -#include "SharedExternal.h" -#include "SharedVal.h" -#include "QueueSemaphoreVal.h" - -using namespace nri; - -QueueSemaphoreVal::QueueSemaphoreVal(DeviceVal& device, QueueSemaphore& queueSemaphore) : - DeviceObjectVal(device, queueSemaphore) -{ -} - -void QueueSemaphoreVal::Signal() -{ - if (m_isSignaled) - REPORT_ERROR(m_Device.GetLog(), "Can't signal QueueSemaphore: it's already in signaled state."); - - m_isSignaled = true; -} - -void QueueSemaphoreVal::Wait() -{ - if (!m_isSignaled) - REPORT_ERROR(m_Device.GetLog(), "Can't wait for QueueSemaphore: it's already in unsignaled state."); - - m_isSignaled = false; -} - -void QueueSemaphoreVal::SetDebugName(const char* name) -{ - m_Name = name; - m_CoreAPI.SetQueueSemaphoreDebugName(m_ImplObject, name); -} - -#include "QueueSemaphoreVal.hpp" diff --git a/Source/Validation/QueueSemaphoreVal.h b/Source/Validation/QueueSemaphoreVal.h deleted file mode 100644 index bf0ed491..00000000 --- a/Source/Validation/QueueSemaphoreVal.h +++ /dev/null @@ -1,30 +0,0 @@ -/* -Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. - -NVIDIA CORPORATION and its licensors retain all intellectual property -and proprietary rights in and to this software, related documentation -and any modifications thereto. Any use, reproduction, disclosure or -distribution of this software and related documentation without an express -license agreement from NVIDIA CORPORATION is strictly prohibited. -*/ - -#pragma once - -namespace nri -{ - struct QueueSemaphoreVal : public DeviceObjectVal - { - QueueSemaphoreVal(DeviceVal& device, QueueSemaphore& queueSemaphore); - - //====================================================================================================================== - // NRI - //====================================================================================================================== - void Signal(); - void Wait(); - - void SetDebugName(const char* name); - - private: - bool m_isSignaled = false; - }; -} diff --git a/Source/Validation/QueueSemaphoreVal.hpp b/Source/Validation/QueueSemaphoreVal.hpp deleted file mode 100644 index bc1527ce..00000000 --- a/Source/Validation/QueueSemaphoreVal.hpp +++ /dev/null @@ -1,24 +0,0 @@ -/* -Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. - -NVIDIA CORPORATION and its licensors retain all intellectual property -and proprietary rights in and to this software, related documentation -and any modifications thereto. Any use, reproduction, disclosure or -distribution of this software and related documentation without an express -license agreement from NVIDIA CORPORATION is strictly prohibited. -*/ - -#pragma region [ CoreInterface ] - -static void NRI_CALL SetQueueSemaphoreDebugName(QueueSemaphore& queueSemaphore, const char* name) -{ - ((QueueSemaphoreVal*)&queueSemaphore)->SetDebugName(name); -} - -void FillFunctionTableQueueSemaphoreVal(CoreInterface& coreInterface) -{ - coreInterface.SetQueueSemaphoreDebugName = ::SetQueueSemaphoreDebugName; -} - -#pragma endregion - diff --git a/Source/Validation/SharedVal.h b/Source/Validation/SharedVal.h index 9dc3f6c6..2e68a0f6 100644 --- a/Source/Validation/SharedVal.h +++ b/Source/Validation/SharedVal.h @@ -16,184 +16,186 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct DeviceObjectBaseVal - { - DeviceObjectBaseVal(DeviceVal& device); - - DeviceVal& GetDevice() const; - - protected: - DeviceVal& m_Device; - const CoreInterface& m_CoreAPI; - }; - - inline DeviceVal& DeviceObjectBaseVal::GetDevice() const - { - return m_Device; - } - - template< typename T > - struct DeviceObjectVal : public DeviceObjectBaseVal - { - DeviceObjectVal(DeviceVal& device, T& object); - - T& GetImpl() const; - const String& GetDebugName() const; - - protected: - T& m_ImplObject; - String m_Name; - }; - - template< typename T > - inline DeviceObjectVal::DeviceObjectVal(DeviceVal& device, T& object) : - DeviceObjectBaseVal(device), - m_ImplObject(object), - m_Name(device.GetStdAllocator()) - { - } - - template< typename T > - inline T& DeviceObjectVal::GetImpl() const - { - return m_ImplObject; - } - template< typename T > - const String& DeviceObjectVal::GetDebugName() const - { - return m_Name; - } +struct DeviceObjectBaseVal +{ + DeviceObjectBaseVal(DeviceVal& device); + + DeviceVal& GetDevice() const; + +protected: + DeviceVal& m_Device; + const CoreInterface& m_CoreAPI; +}; + +inline DeviceVal& DeviceObjectBaseVal::GetDevice() const +{ + return m_Device; +} + +template< typename T > +struct DeviceObjectVal : public DeviceObjectBaseVal +{ + DeviceObjectVal(DeviceVal& device, T& object); + + T& GetImpl() const; + const String& GetDebugName() const; + +protected: + T& m_ImplObject; + String m_Name; +}; + +template< typename T > +inline DeviceObjectVal::DeviceObjectVal(DeviceVal& device, T& object) : + DeviceObjectBaseVal(device), + m_ImplObject(object), + m_Name(device.GetStdAllocator()) +{ +} + +template< typename T > +inline T& DeviceObjectVal::GetImpl() const +{ + return m_ImplObject; +} + +template< typename T > +const String& DeviceObjectVal::GetDebugName() const +{ + return m_Name; +} + +template< typename T > +inline DeviceVal& GetDeviceVal(T& object) +{ + return ((DeviceObjectBaseVal&)object).GetDevice(); +} + +uint64_t GetMemorySizeD3D12(const MemoryD3D12Desc& memoryD3D12Desc); +void GetTextureDescD3D12(const TextureD3D12Desc& textureD3D12Desc, TextureDesc& textureDesc); +void GetBufferDescD3D12(const BufferD3D12Desc& bufferD3D12Desc, BufferDesc& bufferDesc); + +void GetTextureDescD3D11(const TextureD3D11Desc& textureD3D11Desc, TextureDesc& textureDesc); +void GetBufferDescD3D11(const BufferD3D11Desc& bufferD3D11Desc, BufferDesc& bufferDesc); + +constexpr const char* DESCRIPTOR_TYPE_NAME[] = { + "SAMPLER", + "CONSTANT_BUFFER", + "TEXTURE", + "STORAGE_TEXTURE", + "BUFFER", + "STORAGE_BUFFER", + "STRUCTURED_BUFFER", + "STORAGE_STRUCTURED_BUFFER", + "ACCELERATION_STRUCTURE" +}; +static_assert(GetCountOf(DESCRIPTOR_TYPE_NAME) == (uint32_t)nri::DescriptorType::MAX_NUM, "descriptor type name array is out of date"); + +constexpr const char* GetDescriptorTypeName(nri::DescriptorType descriptorType) +{ + return DESCRIPTOR_TYPE_NAME[(uint32_t)descriptorType]; +} + +constexpr bool IsAccessMaskSupported(BufferUsageBits usageMask, AccessBits accessMask) +{ + BufferUsageBits requiredUsageMask = BufferUsageBits::NONE; - template< typename T > - inline DeviceVal& GetDeviceVal(T& object) - { - return ((DeviceObjectBaseVal&)object).GetDevice(); - } + if (accessMask & AccessBits::VERTEX_BUFFER) + requiredUsageMask |= BufferUsageBits::VERTEX_BUFFER; - uint64_t GetMemorySizeD3D12(const MemoryD3D12Desc& memoryD3D12Desc); - void GetTextureDescD3D12(const TextureD3D12Desc& textureD3D12Desc, TextureDesc& textureDesc); - void GetBufferDescD3D12(const BufferD3D12Desc& bufferD3D12Desc, BufferDesc& bufferDesc); + if (accessMask & AccessBits::INDEX_BUFFER) + requiredUsageMask |= BufferUsageBits::INDEX_BUFFER; - void GetTextureDescD3D11(const TextureD3D11Desc& textureD3D11Desc, TextureDesc& textureDesc); - void GetBufferDescD3D11(const BufferD3D11Desc& bufferD3D11Desc, BufferDesc& bufferDesc); + if (accessMask & AccessBits::CONSTANT_BUFFER) + requiredUsageMask |= BufferUsageBits::CONSTANT_BUFFER; - constexpr const char* DESCRIPTOR_TYPE_NAME[] = { - "SAMPLER", - "CONSTANT_BUFFER", - "TEXTURE", - "STORAGE_TEXTURE", - "BUFFER", - "STORAGE_BUFFER", - "STRUCTURED_BUFFER", - "STORAGE_STRUCTURED_BUFFER", - "ACCELERATION_STRUCTURE" - }; - static_assert(GetCountOf(DESCRIPTOR_TYPE_NAME) == (uint32_t)nri::DescriptorType::MAX_NUM, "descriptor type name array is out of date"); - - constexpr const char* GetDescriptorTypeName(nri::DescriptorType descriptorType) - { - return DESCRIPTOR_TYPE_NAME[(uint32_t)descriptorType]; - } - - constexpr bool IsAccessMaskSupported(BufferUsageBits usageMask, AccessBits accessMask) - { - BufferUsageBits requiredUsageMask = BufferUsageBits::NONE; - - if (accessMask & AccessBits::VERTEX_BUFFER) - requiredUsageMask |= BufferUsageBits::VERTEX_BUFFER; - - if (accessMask & AccessBits::INDEX_BUFFER) - requiredUsageMask |= BufferUsageBits::INDEX_BUFFER; - - if (accessMask & AccessBits::CONSTANT_BUFFER) - requiredUsageMask |= BufferUsageBits::CONSTANT_BUFFER; - - if (accessMask & AccessBits::ARGUMENT_BUFFER) - requiredUsageMask |= BufferUsageBits::ARGUMENT_BUFFER; - - if (accessMask & AccessBits::SHADER_RESOURCE) - requiredUsageMask |= BufferUsageBits::SHADER_RESOURCE; - - if (accessMask & AccessBits::SHADER_RESOURCE_STORAGE) - requiredUsageMask |= BufferUsageBits::SHADER_RESOURCE_STORAGE; - - if (accessMask & AccessBits::COLOR_ATTACHMENT) - return false; - - if (accessMask & AccessBits::DEPTH_STENCIL_WRITE) - return false; - - if (accessMask & AccessBits::DEPTH_STENCIL_READ) - return false; - - if (accessMask & AccessBits::ACCELERATION_STRUCTURE_READ) - return false; - - if (accessMask & AccessBits::ACCELERATION_STRUCTURE_WRITE) - return false; - - return (uint32_t)(requiredUsageMask & usageMask) == (uint32_t)requiredUsageMask; - } + if (accessMask & AccessBits::ARGUMENT_BUFFER) + requiredUsageMask |= BufferUsageBits::ARGUMENT_BUFFER; - constexpr bool IsAccessMaskSupported(TextureUsageBits usageMask, AccessBits accessMask) - { - TextureUsageBits requiredUsageMask = TextureUsageBits::NONE; + if (accessMask & AccessBits::SHADER_RESOURCE) + requiredUsageMask |= BufferUsageBits::SHADER_RESOURCE; - if (accessMask & AccessBits::VERTEX_BUFFER) - return false; - - if (accessMask & AccessBits::INDEX_BUFFER) - return false; - - if (accessMask & AccessBits::CONSTANT_BUFFER) - return false; + if (accessMask & AccessBits::SHADER_RESOURCE_STORAGE) + requiredUsageMask |= BufferUsageBits::SHADER_RESOURCE_STORAGE; - if (accessMask & AccessBits::ARGUMENT_BUFFER) - return false; + if (accessMask & AccessBits::COLOR_ATTACHMENT) + return false; - if (accessMask & AccessBits::SHADER_RESOURCE) - requiredUsageMask |= TextureUsageBits::SHADER_RESOURCE; + if (accessMask & AccessBits::DEPTH_STENCIL_WRITE) + return false; - if (accessMask & AccessBits::SHADER_RESOURCE_STORAGE) - requiredUsageMask |= TextureUsageBits::SHADER_RESOURCE_STORAGE; + if (accessMask & AccessBits::DEPTH_STENCIL_READ) + return false; - if (accessMask & AccessBits::COLOR_ATTACHMENT) - requiredUsageMask |= TextureUsageBits::COLOR_ATTACHMENT; + if (accessMask & AccessBits::ACCELERATION_STRUCTURE_READ) + return false; + + if (accessMask & AccessBits::ACCELERATION_STRUCTURE_WRITE) + return false; + + return (uint32_t)(requiredUsageMask & usageMask) == (uint32_t)requiredUsageMask; +} + +constexpr bool IsAccessMaskSupported(TextureUsageBits usageMask, AccessBits accessMask) +{ + TextureUsageBits requiredUsageMask = TextureUsageBits::NONE; - if (accessMask & AccessBits::DEPTH_STENCIL_WRITE) - requiredUsageMask |= TextureUsageBits::DEPTH_STENCIL_ATTACHMENT; + if (accessMask & AccessBits::VERTEX_BUFFER) + return false; - if (accessMask & AccessBits::DEPTH_STENCIL_READ) - requiredUsageMask |= TextureUsageBits::DEPTH_STENCIL_ATTACHMENT; + if (accessMask & AccessBits::INDEX_BUFFER) + return false; - if (accessMask & AccessBits::ACCELERATION_STRUCTURE_READ) - return false; + if (accessMask & AccessBits::CONSTANT_BUFFER) + return false; - if (accessMask & AccessBits::ACCELERATION_STRUCTURE_WRITE) - return false; + if (accessMask & AccessBits::ARGUMENT_BUFFER) + return false; - return (uint32_t)(requiredUsageMask & usageMask) == (uint32_t)requiredUsageMask; - } + if (accessMask & AccessBits::SHADER_RESOURCE) + requiredUsageMask |= TextureUsageBits::SHADER_RESOURCE; - constexpr std::array TEXTURE_USAGE_FOR_TEXTURE_LAYOUT_TABLE = { - TextureUsageBits::NONE, // GENERAL - TextureUsageBits::COLOR_ATTACHMENT, // COLOR_ATTACHMENT - TextureUsageBits::DEPTH_STENCIL_ATTACHMENT, // DEPTH_STENCIL - TextureUsageBits::DEPTH_STENCIL_ATTACHMENT, // DEPTH_STENCIL_READONLY - TextureUsageBits::DEPTH_STENCIL_ATTACHMENT, // DEPTH_READONLY - TextureUsageBits::DEPTH_STENCIL_ATTACHMENT, // STENCIL_READONLY - TextureUsageBits::SHADER_RESOURCE, // SHADER_RESOURCE - TextureUsageBits::NONE, // PRESENT - TextureUsageBits::NONE // UNKNOWN - }; + if (accessMask & AccessBits::SHADER_RESOURCE_STORAGE) + requiredUsageMask |= TextureUsageBits::SHADER_RESOURCE_STORAGE; - constexpr bool IsTextureLayoutSupported(TextureUsageBits usageMask, TextureLayout textureLayout) - { - const TextureUsageBits requiredMask = TEXTURE_USAGE_FOR_TEXTURE_LAYOUT_TABLE[(size_t)textureLayout]; + if (accessMask & AccessBits::COLOR_ATTACHMENT) + requiredUsageMask |= TextureUsageBits::COLOR_ATTACHMENT; + + if (accessMask & AccessBits::DEPTH_STENCIL_WRITE) + requiredUsageMask |= TextureUsageBits::DEPTH_STENCIL_ATTACHMENT; + + if (accessMask & AccessBits::DEPTH_STENCIL_READ) + requiredUsageMask |= TextureUsageBits::DEPTH_STENCIL_ATTACHMENT; + + if (accessMask & AccessBits::ACCELERATION_STRUCTURE_READ) + return false; + + if (accessMask & AccessBits::ACCELERATION_STRUCTURE_WRITE) + return false; + + return (uint32_t)(requiredUsageMask & usageMask) == (uint32_t)requiredUsageMask; +} + +constexpr std::array TEXTURE_USAGE_FOR_TEXTURE_LAYOUT_TABLE = { + TextureUsageBits::NONE, // GENERAL + TextureUsageBits::COLOR_ATTACHMENT, // COLOR_ATTACHMENT + TextureUsageBits::DEPTH_STENCIL_ATTACHMENT, // DEPTH_STENCIL + TextureUsageBits::DEPTH_STENCIL_ATTACHMENT, // DEPTH_STENCIL_READONLY + TextureUsageBits::DEPTH_STENCIL_ATTACHMENT, // DEPTH_READONLY + TextureUsageBits::DEPTH_STENCIL_ATTACHMENT, // STENCIL_READONLY + TextureUsageBits::SHADER_RESOURCE, // SHADER_RESOURCE + TextureUsageBits::NONE, // PRESENT + TextureUsageBits::NONE // UNKNOWN +}; + +constexpr bool IsTextureLayoutSupported(TextureUsageBits usageMask, TextureLayout textureLayout) +{ + const TextureUsageBits requiredMask = TEXTURE_USAGE_FOR_TEXTURE_LAYOUT_TABLE[(size_t)textureLayout]; + + return (uint32_t)(requiredMask & usageMask) == (uint32_t)requiredMask; +} - return (uint32_t)(requiredMask & usageMask) == (uint32_t)requiredMask; - } } #define NRI_GET_IMPL_PTR(className, object) \ diff --git a/Source/Validation/SwapChainVal.cpp b/Source/Validation/SwapChainVal.cpp index be604fb9..b0c584ec 100644 --- a/Source/Validation/SwapChainVal.cpp +++ b/Source/Validation/SwapChainVal.cpp @@ -13,8 +13,6 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. #include "DeviceVal.h" #include "SharedVal.h" #include "SwapChainVal.h" - -#include "QueueSemaphoreVal.h" #include "TextureVal.h" using namespace nri; @@ -62,20 +60,14 @@ inline Texture* const* SwapChainVal::GetTextures(uint32_t& textureNum, Format& f return (Texture* const*)m_Textures.data(); } -inline uint32_t SwapChainVal::AcquireNextTexture(QueueSemaphore& textureReadyForRender) +inline uint32_t SwapChainVal::AcquireNextTexture() { - ((QueueSemaphoreVal*)&textureReadyForRender)->Signal(); - QueueSemaphore* queueSemaphoreImpl = NRI_GET_IMPL_REF(QueueSemaphore, &textureReadyForRender); - - return m_SwapChainAPI.AcquireNextSwapChainTexture(m_ImplObject, *queueSemaphoreImpl); + return m_SwapChainAPI.AcquireNextSwapChainTexture(m_ImplObject); } -inline Result SwapChainVal::Present(QueueSemaphore& textureReadyForPresent) +inline Result SwapChainVal::Present() { - ((QueueSemaphoreVal*)&textureReadyForPresent)->Wait(); - QueueSemaphore* queueSemaphoreImpl = NRI_GET_IMPL_REF(QueueSemaphore, &textureReadyForPresent); - - return m_SwapChainAPI.SwapChainPresent(m_ImplObject, *queueSemaphoreImpl); + return m_SwapChainAPI.SwapChainPresent(m_ImplObject); } inline Result SwapChainVal::SetHdrMetadata(const HdrMetadata& hdrMetadata) diff --git a/Source/Validation/SwapChainVal.h b/Source/Validation/SwapChainVal.h index db329c9c..f6764147 100644 --- a/Source/Validation/SwapChainVal.h +++ b/Source/Validation/SwapChainVal.h @@ -12,25 +12,27 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct TextureVal; - - struct SwapChainVal : public DeviceObjectVal - { - SwapChainVal(DeviceVal& device, SwapChain& swapChain, const SwapChainDesc& swapChainDesc); - ~SwapChainVal(); - - //====================================================================================================================== - // NRI - //====================================================================================================================== - void SetDebugName(const char* name); - Texture* const* GetTextures(uint32_t& textureNum, Format& format) const; - uint32_t AcquireNextTexture(QueueSemaphore& textureReadyForRender); - Result Present(QueueSemaphore& textureReadyForPresent); - Result SetHdrMetadata(const HdrMetadata& hdrMetadata); - - private: - const SwapChainInterface& m_SwapChainAPI; - mutable Vector m_Textures; - SwapChainDesc m_SwapChainDesc = {}; - }; + +struct TextureVal; + +struct SwapChainVal : public DeviceObjectVal +{ + SwapChainVal(DeviceVal& device, SwapChain& swapChain, const SwapChainDesc& swapChainDesc); + ~SwapChainVal(); + + //================================================================================================================ + // NRI + //================================================================================================================ + void SetDebugName(const char* name); + Texture* const* GetTextures(uint32_t& textureNum, Format& format) const; + uint32_t AcquireNextTexture(); + Result Present(); + Result SetHdrMetadata(const HdrMetadata& hdrMetadata); + +private: + const SwapChainInterface& m_SwapChainAPI; + mutable Vector m_Textures; + SwapChainDesc m_SwapChainDesc = {}; +}; + } diff --git a/Source/Validation/SwapChainVal.hpp b/Source/Validation/SwapChainVal.hpp index 16a5b800..7baed441 100644 --- a/Source/Validation/SwapChainVal.hpp +++ b/Source/Validation/SwapChainVal.hpp @@ -8,40 +8,33 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ SwapChainInterface ] +#pragma region [ SwapChain ] static void NRI_CALL SetSwapChainDebugName(SwapChain& swapChain, const char* name) { - ((SwapChainVal*)&swapChain)->SetDebugName(name); + ((SwapChainVal&)swapChain).SetDebugName(name); } static Texture* const* NRI_CALL GetSwapChainTextures(const SwapChain& swapChain, uint32_t& textureNum, Format& format) { - return ((SwapChainVal*)&swapChain)->GetTextures(textureNum, format); + return ((SwapChainVal&)swapChain).GetTextures(textureNum, format); } -static uint32_t NRI_CALL AcquireNextSwapChainTexture(SwapChain& swapChain, QueueSemaphore& textureReadyForRender) +static uint32_t NRI_CALL AcquireNextSwapChainTexture(SwapChain& swapChain) { - return ((SwapChainVal*)&swapChain)->AcquireNextTexture(textureReadyForRender); + return ((SwapChainVal&)swapChain).AcquireNextTexture(); } -static Result NRI_CALL SwapChainPresent(SwapChain& swapChain, QueueSemaphore& textureReadyForPresent) +static Result NRI_CALL SwapChainPresent(SwapChain& swapChain) { - return ((SwapChainVal*)&swapChain)->Present(textureReadyForPresent); + return ((SwapChainVal&)swapChain).Present(); } static Result NRI_CALL SetSwapChainHdrMetadata(SwapChain& swapChain, const HdrMetadata& hdrMetadata) { - return ((SwapChainVal*)&swapChain)->SetHdrMetadata(hdrMetadata); -} - -void FillFunctionTableSwapChainVal(SwapChainInterface& swapChainInterface) -{ - swapChainInterface.SetSwapChainDebugName = ::SetSwapChainDebugName; - swapChainInterface.GetSwapChainTextures = ::GetSwapChainTextures; - swapChainInterface.AcquireNextSwapChainTexture = ::AcquireNextSwapChainTexture; - swapChainInterface.SwapChainPresent = ::SwapChainPresent; - swapChainInterface.SetSwapChainHdrMetadata = ::SetSwapChainHdrMetadata; + return ((SwapChainVal&)swapChain).SetHdrMetadata(hdrMetadata); } #pragma endregion + +Define_SwapChain_PartiallyFillFunctionTable(Val) diff --git a/Source/Validation/TextureVal.h b/Source/Validation/TextureVal.h index 414da303..7cc7b9f1 100644 --- a/Source/Validation/TextureVal.h +++ b/Source/Validation/TextureVal.h @@ -12,43 +12,45 @@ license agreement from NVIDIA CORPORATION is strictly prohibited. namespace nri { - struct MemoryVal; - struct TextureVal : public DeviceObjectVal +struct MemoryVal; + +struct TextureVal : public DeviceObjectVal +{ + TextureVal(DeviceVal& device, Texture& texture, const TextureDesc& textureDesc); + TextureVal(DeviceVal& device, Texture& texture, const TextureD3D11Desc& textureD3D11Desc); + TextureVal(DeviceVal& device, Texture& texture, const TextureD3D12Desc& textureD3D12Desc); + TextureVal(DeviceVal& device, Texture& texture, const TextureVulkanDesc& textureVulkanDesc); + ~TextureVal(); + + inline const TextureDesc& GetDesc() const + { return m_TextureDesc; } + + inline uint64_t GetNativeObject(uint32_t physicalDeviceIndex) const + { return m_CoreAPI.GetTextureNativeObject(m_ImplObject, physicalDeviceIndex); } + + inline bool IsBoundToMemory() const + { return m_IsBoundToMemory; } + + inline void SetBoundToMemory() + { m_IsBoundToMemory = true; } + + inline void SetBoundToMemory(MemoryVal& memory) { - TextureVal(DeviceVal& device, Texture& texture, const TextureDesc& textureDesc); - TextureVal(DeviceVal& device, Texture& texture, const TextureD3D11Desc& textureD3D11Desc); - TextureVal(DeviceVal& device, Texture& texture, const TextureD3D12Desc& textureD3D12Desc); - TextureVal(DeviceVal& device, Texture& texture, const TextureVulkanDesc& textureVulkanDesc); - ~TextureVal(); - - inline const TextureDesc& GetDesc() const - { return m_TextureDesc; } - - inline uint64_t GetNativeObject(uint32_t physicalDeviceIndex) const - { return m_CoreAPI.GetTextureNativeObject(m_ImplObject, physicalDeviceIndex); } - - inline bool IsBoundToMemory() const - { return m_IsBoundToMemory; } - - inline void SetBoundToMemory() - { m_IsBoundToMemory = true; } - - inline void SetBoundToMemory(MemoryVal& memory) - { - m_Memory = &memory; - m_IsBoundToMemory = true; - } - - //====================================================================================================================== - // NRI - //====================================================================================================================== - void SetDebugName(const char* name); - void GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const; - - private: - MemoryVal* m_Memory = nullptr; - bool m_IsBoundToMemory = false; - TextureDesc m_TextureDesc = {}; - }; + m_Memory = &memory; + m_IsBoundToMemory = true; + } + + //================================================================================================================ + // NRI + //================================================================================================================ + void SetDebugName(const char* name); + void GetMemoryInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const; + +private: + MemoryVal* m_Memory = nullptr; + bool m_IsBoundToMemory = false; + TextureDesc m_TextureDesc = {}; +}; + } diff --git a/Source/Validation/TextureVal.hpp b/Source/Validation/TextureVal.hpp index c914d402..4446aa1f 100644 --- a/Source/Validation/TextureVal.hpp +++ b/Source/Validation/TextureVal.hpp @@ -8,7 +8,7 @@ distribution of this software and related documentation without an express license agreement from NVIDIA CORPORATION is strictly prohibited. */ -#pragma region [ CoreInterface ] +#pragma region [ Core ] static void NRI_CALL SetTextureDebugName(Texture& texture, const char* name) { @@ -25,11 +25,6 @@ static void NRI_CALL GetTextureMemoryInfo(const Texture& texture, MemoryLocation ((TextureVal&)texture).GetMemoryInfo(memoryLocation, memoryDesc); } -void FillFunctionTableTextureVal(CoreInterface& coreInterface) -{ - coreInterface.SetTextureDebugName = ::SetTextureDebugName; - coreInterface.GetTextureNativeObject = ::GetTextureNativeObject; - coreInterface.GetTextureMemoryInfo = ::GetTextureMemoryInfo; -} - #pragma endregion + +Define_Core_Texture_PartiallyFillFunctionTable(Val)