Bladeren bron

enable more directx12 debugging

Kolja Strohm 1 week geleden
bovenliggende
commit
daddbffce6
6 gewijzigde bestanden met toevoegingen van 181 en 291 verwijderingen
  1. 19 1
      Common.hlsl
  2. 112 189
      DX12GraphicsApi.cpp
  3. 10 7
      DX12GraphicsApi.h
  4. 37 69
      DX12Shader.cpp
  5. 3 8
      DX12Shader.h
  6. 0 17
      RayGen.hlsl

+ 19 - 1
Common.hlsl

@@ -23,4 +23,22 @@ struct Attributes
 
 // global root signature resources
 Texture2D<float4> textures[] : register(t0, space1);
-SamplerState gSampler : register(s0, space0);
+SamplerState gSampler : register(s0, space0);
+
+// Raytracing output texture, accessed as a UAV
+RWTexture2D<float4> gOutput : register(u0);
+
+RWTexture2D<float4> guiTexture : register(u1);
+
+// Raytracing acceleration structure, accessed as a SRV
+RaytracingAccelerationStructure TLAS : register(t0, space0);
+
+cbuffer RayGenerationSettings : register(b0)
+{
+    int renderGui;
+    int useRays;
+    float minDistance;
+    float maxDistance;
+    float4x4 inverseView;
+    float4x4 inverseProjection;
+}

+ 112 - 189
DX12GraphicsApi.cpp

@@ -58,7 +58,6 @@ DirectX12::DirectX12()
       swapChain(0),
       backBufferIndex(0),
       tearing(0),
-      signature(0),
       uiTexture(0),
       texturRegister(new TextureList()),
       blasModels(0),
@@ -74,7 +73,6 @@ DirectX12::DirectX12()
       pfnD3D12SerializeRootSignature(0),
       pipeline(0),
       globalDescriptorHeap(0),
-      textureDescriptorHeap(0),
       samplerDescriptorHeap(0),
       defaultHitGroup(0),
       defaultRayGenerationShaderFunction(0),
@@ -82,7 +80,10 @@ DirectX12::DirectX12()
       sbtTextureIdBufferOffset(0),
       sbtIndexBufferOffset(0),
       sbtVertexDataBufferOffset(0),
-      sbtPolygonSizeBufferOffset(0)
+      sbtPolygonSizeBufferOffset(0),
+      firstTextureHeapIndex(0),
+      globalRayGenerationSettingsOffset(0),
+      globalTLASoffset(0)
 {
     for (int i = 0; i < 2; i++)
         backBuffer[i] = 0;
@@ -119,10 +120,6 @@ DirectX12::~DirectX12()
     {
         globalDescriptorHeap->release();
     }
-    if (textureDescriptorHeap)
-    {
-        textureDescriptorHeap->release();
-    }
     if (samplerDescriptorHeap)
     {
         samplerDescriptorHeap->release();
@@ -148,7 +145,6 @@ DirectX12::~DirectX12()
     if (uiTexture) uiTexture->release();
     if (defaultKamera) defaultKamera->release();
     if (defaultRenderTarget) defaultRenderTarget->release();
-    if (signature) signature->Release();
     for (int i = 0; i < 2; i++)
     {
         if (backBuffer[i]) backBuffer[i]->Release();
@@ -205,7 +201,8 @@ void Framework::DirectX12::renderKamera(
     {
         if (texture->hasBufferChanged())
         {
-            textureDescriptorHeap->updateTextureInput(texture->getId(),
+            globalDescriptorHeap->updateTextureInput(
+                texture->getId() + firstTextureHeapIndex,
                 DX12_SHADER_REGISTER_T_SHADER_RESOURCE,
                 (DX12Texture*)texture);
             texture->setBufferChanged(0);
@@ -250,11 +247,15 @@ void Framework::DirectX12::renderKamera(
     // descriptor heaps must be set before filling the shader binding table with
     // hit groups
     sbt->setGlobalDescriptorHeap(globalDescriptorHeap);
-    sbt->setTextureDescriptorHeap(textureDescriptorHeap);
     sbt->setSamplerDescriptorHeap(samplerDescriptorHeap);
     int objectIndex = 0;
     renderWorld(w, tlas, sbt, objectIndex);
     tlas->endUpdate();
+    // add additional default hit group if no other hit group exists
+    if (!sbt->getHitGoupCount() && defaultHitGroup)
+    {
+        sbt->addHitGroup(defaultHitGroup);
+    }
     settings.inverseProjection = zKamera->getInverseProjectionMatrix();
     settings.inverseView = zKamera->getInverseViewMatrix();
     settings.minDistance = zKamera->getMinDistance();
@@ -263,13 +264,6 @@ void Framework::DirectX12::renderKamera(
     settings.renderGui = (int)guiVisible;
     rayGenSettingsBuffer->setData(&settings, 1);
     rayGenSettingsBuffer->copyToGPU(sizeof(settings));
-    if (defaultRayGenerationShaderFunction)
-    {
-        globalDescriptorHeap->updateTextureInput(
-            0, DX12_SHADER_REGISTER_U_UNORDERED_ACCESS, zTarget);
-        globalDescriptorHeap->updateTLASInput(
-            3, DX12_SHADER_REGISTER_T_SHADER_RESOURCE, tlas);
-    }
     sbt->endUpdate(device, directCommandQueue);
 
     D3D12_DISPATCH_RAYS_DESC desc;
@@ -278,7 +272,7 @@ void Framework::DirectX12::renderKamera(
     desc.Height = zTarget->zImage()->getHeight();
     desc.Depth = 1;
     // order of definition in DX12DescriptorHeapType must be matched
-    fillGlobalShaderParams(zKamera, tlas);
+    fillGlobalShaderParams(zKamera, tlas, zTarget);
     directCommandQueue->zCommandList()->SetPipelineState1(
         pipeline->zPipelineState());
     directCommandQueue->zCommandList()->DispatchRays(&desc);
@@ -366,25 +360,29 @@ void Framework::DirectX12::initializePipeline()
     { // add default shaders
         pipeline->zGlobalSignature()->addRegisterUsageLinkedToDescriptorHeap(
             0, DX12_SHADER_REGISTER_S_SAMPLER, 0, 0, SAMPLER_DESCRIPTOR_HEAP);
-        pipeline->zGlobalSignature()->addRegisterUsageLinkedToDescriptorHeap(0,
+        pipeline->zGlobalSignature()->addRegisterUsageLinkedToDescriptorHeap(
+            firstTextureHeapIndex,
             DX12_SHADER_REGISTER_T_SHADER_RESOURCE,
             0,
             1,
-            TEXTURE_DESCRIPTOR_HEAP,
+            GLOBAL_DESCRIPTOR_HEAP,
             1);
+        pipeline->zGlobalSignature()->addRegisterUsageLinkedToDescriptorHeap(
+            0, DX12_SHADER_REGISTER_U_UNORDERED_ACCESS, 0);
+        pipeline->zGlobalSignature()->addRegisterUsageLinkedToDescriptorHeap(
+            1, DX12_SHADER_REGISTER_U_UNORDERED_ACCESS, 1);
+        globalRayGenerationSettingsOffset
+            = pipeline->zGlobalSignature()
+                  ->addRegisterUsageLinkedToShaderBindingTable(
+                      DX12_SHADER_REGISTER_B_CONST_BUFFER, 0);
+        globalTLASoffset = pipeline->zGlobalSignature()
+                               ->addRegisterUsageLinkedToShaderBindingTable(
+                                   DX12_SHADER_REGISTER_T_SHADER_RESOURCE, 0);
 
         DX12Shader* rayGenShader = new DX12Shader(
             DX12DefaultRayGenShaderBytes, sizeof(DX12DefaultRayGenShaderBytes));
         // RayGen from RayGen.hlsl
         DX12ShaderSignature* rayGenSignature = new DX12ShaderSignature();
-        rayGenSignature->addRegisterUsageLinkedToDescriptorHeap(
-            0, DX12_SHADER_REGISTER_U_UNORDERED_ACCESS, 0);
-        rayGenSignature->addRegisterUsageLinkedToDescriptorHeap(
-            1, DX12_SHADER_REGISTER_U_UNORDERED_ACCESS, 1);
-        rayGenSignature->addRegisterUsageLinkedToDescriptorHeap(
-            2, DX12_SHADER_REGISTER_B_CONST_BUFFER, 0);
-        rayGenSignature->addRegisterUsageLinkedToDescriptorHeap(
-            3, DX12_SHADER_REGISTER_T_SHADER_RESOURCE, 0);
         defaultRayGenerationShaderFunction = new DX12ShaderFunction(
             "RayGen", rayGenSignature, DX12_SHADER_FUNCTION_TYPE_RAY_GEN);
         rayGenShader->addFunction(defaultRayGenerationShaderFunction);
@@ -446,24 +444,14 @@ void Framework::DirectX12::initializeGlobalDescriptorHeap()
         DX12_SHADER_REGISTER_U_UNORDERED_ACCESS, defaultRenderTarget);
     globalDescriptorHeap->addTextureInput(
         DX12_SHADER_REGISTER_U_UNORDERED_ACCESS, uiTexture);
-    globalDescriptorHeap->addBufferInput(
-        DX12_SHADER_REGISTER_B_CONST_BUFFER, rayGenSettingsBuffer);
-    globalDescriptorHeap->addTLASInput(
-        DX12_SHADER_REGISTER_T_SHADER_RESOURCE, 0);
-
-    globalDescriptorHeap->updateDescriptorHeap(device);
-}
-
-void Framework::DirectX12::initializeTextureDescriptorHeap()
-{
+    firstTextureHeapIndex = globalDescriptorHeap->getRegisterInputCount();
     for (Texture* tex : *texturRegister->zTextures())
     {
-        textureDescriptorHeap->addTextureInput(
+        globalDescriptorHeap->addTextureInput(
             DX12_SHADER_REGISTER_T_SHADER_RESOURCE, tex);
         tex->setBufferChanged(0);
     }
-
-    textureDescriptorHeap->updateDescriptorHeap(device);
+    globalDescriptorHeap->updateDescriptorHeap(device);
 }
 
 void Framework::DirectX12::initializeSamplerDescriptorHeap()
@@ -526,13 +514,10 @@ void Framework::DirectX12::fillShaderBindingTable(
 }
 
 int Framework::DirectX12::fillGlobalShaderParams(
-    Cam3D* zKam, DX12TLAS* zTLAS, int startIndex)
+    Cam3D* zKam, DX12TLAS* zTLAS, DX12Texture* zTarget, int startIndex)
 {
-    ID3D12DescriptorHeap* heaps[]
-        = {pipeline->zGlobalSignature()->doesUseTextureDescriptorHeap()
-                ? textureDescriptorHeap->zDescriptorHeap()
-                : globalDescriptorHeap->zDescriptorHeap(),
-            samplerDescriptorHeap->zDescriptorHeap()};
+    ID3D12DescriptorHeap* heaps[] = {globalDescriptorHeap->zDescriptorHeap(),
+        samplerDescriptorHeap->zDescriptorHeap()};
     directCommandQueue->zCommandList()->SetDescriptorHeaps(2, heaps);
     if (pipeline->zGlobalSignature()->gerShaderBindingTableParamCount()
             + pipeline->zGlobalSignature()
@@ -552,13 +537,6 @@ int Framework::DirectX12::fillGlobalShaderParams(
                 globalDescriptorHeap->zDescriptorHeap()
                     ->GetGPUDescriptorHandleForHeapStart());
         }
-        if (pipeline->zGlobalSignature()->doesUseTextureDescriptorHeap())
-        {
-            directCommandQueue->zCommandList()->SetComputeRootDescriptorTable(
-                startIndex++,
-                textureDescriptorHeap->zDescriptorHeap()
-                    ->GetGPUDescriptorHandleForHeapStart());
-        }
         if (pipeline->zGlobalSignature()->doesUseSamplerDescriptorHeap())
         {
             directCommandQueue->zCommandList()->SetComputeRootDescriptorTable(
@@ -567,6 +545,18 @@ int Framework::DirectX12::fillGlobalShaderParams(
                     ->GetGPUDescriptorHandleForHeapStart());
         }
     }
+    if (globalRayGenerationSettingsOffset)
+    {
+        directCommandQueue->zCommandList()->SetComputeRootConstantBufferView(
+            *globalRayGenerationSettingsOffset,
+            rayGenSettingsBuffer->zBuffer()->GetGPUVirtualAddress());
+    }
+    if (globalTLASoffset)
+    {
+        directCommandQueue->zCommandList()->SetComputeRootShaderResourceView(
+            *globalTLASoffset,
+            zTLAS->zResultBuffer()->zBuffer()->GetGPUVirtualAddress());
+    }
     return startIndex;
 }
 
@@ -588,9 +578,10 @@ void DirectX12::initialize(
 #ifdef _DEBUG
     if (debugDX)
     {
-        HINSTANCE pixDebugger = getDLLRegister()->loadDLL(
-            "WinPixGpuCapturer.dll",
-            "C:\\Program Files\\Microsoft PIX\\2603.25\\WinPixGpuCapturer.dll");
+        HINSTANCE pixDebugger
+            = getDLLRegister()->loadDLL("WinPixGpuCapturer.dll",
+                "C:\\Program Files\\Microsoft PIX "
+                "Preview\\2606.18-preview\\WinPixGpuCapturer.dll");
     }
 #endif
 
@@ -659,32 +650,68 @@ void DirectX12::initialize(
             MB_ICONERROR);
         return;
     }
-    // D3D12SerializeVersionedRootSignature
-    PFN_D3D12_SERIALIZE_VERSIONED_ROOT_SIGNATURE d3d12svrsf
-        = (PFN_D3D12_SERIALIZE_VERSIONED_ROOT_SIGNATURE)GetProcAddress(
-            d3d12DLL, "D3D12SerializeVersionedRootSignature");
-    // D3D12SerializeRootSignature
-    PFN_D3D12_SERIALIZE_ROOT_SIGNATURE d3d12srsf
-        = (PFN_D3D12_SERIALIZE_ROOT_SIGNATURE)GetProcAddress(
-            d3d12DLL, "D3D12SerializeRootSignature");
     bool debugDXGI = 0;
 #ifdef _DEBUG
-    IDXGIInfoQueue* dxgiInfoQueue;
+    IDXGIInfoQueue* dxgiInfoQueue = 0;
     if (debugDX)
     {
         // D3D12GetDebugInterface
         D3D12GetDebugInterfaceFunction getDebugInterface
             = (D3D12GetDebugInterfaceFunction)GetProcAddress(
                 d3d12DLL, "D3D12GetDebugInterface");
-        if (SUCCEEDED(getDebugInterface(__uuidof(ID3D12Debug), (void**)&debug)))
-            debug->EnableDebugLayer();
+        if (getDebugInterface)
+        {
+            if (SUCCEEDED(
+                    getDebugInterface(__uuidof(ID3D12Debug), (void**)&debug)))
+            {
+                debug->EnableDebugLayer();
+
+                ID3D12Debug1* debug1 = 0;
+                if (SUCCEEDED(debug->QueryInterface(
+                        __uuidof(ID3D12Debug1), (void**)&debug1)))
+                {
+                    debug1->SetEnableGPUBasedValidation(TRUE);
+                    debug1->SetEnableSynchronizedCommandQueueValidation(TRUE);
+                    debug1->Release();
+                }
+            }
+
+            ID3D12DeviceRemovedExtendedDataSettings* dredSettings = 0;
+            if (SUCCEEDED(getDebugInterface(
+                    __uuidof(ID3D12DeviceRemovedExtendedDataSettings),
+                    (void**)&dredSettings)))
+            {
+                dredSettings->SetAutoBreadcrumbsEnablement(
+                    D3D12_DRED_ENABLEMENT_FORCED_ON);
+                dredSettings->SetPageFaultEnablement(
+                    D3D12_DRED_ENABLEMENT_FORCED_ON);
+                dredSettings->Release();
+            }
+        }
+
         // DXGIGetDebugInterface1
         DXGIGetDebugInterface1Function dxgiDebugInterface
             = (DXGIGetDebugInterface1Function)GetProcAddress(
                 dxgiDLL, "DXGIGetDebugInterface1");
-        if (SUCCEEDED(dxgiDebugInterface(
+        if (dxgiDebugInterface
+            && SUCCEEDED(dxgiDebugInterface(
                 0, __uuidof(IDXGIInfoQueue), (void**)&dxgiInfoQueue)))
+        {
             debugDXGI = 1;
+            static const DXGI_DEBUG_ID DXGI_DEBUG_ALL_LOCAL = {
+                0xe48ae283,
+                0xda80,
+                0x490b,
+                {0x87, 0xe6, 0x43, 0xe9, 0xa9, 0xcf, 0xda, 0x08}
+            };
+            dxgiInfoQueue->SetBreakOnSeverity(DXGI_DEBUG_ALL_LOCAL,
+                DXGI_INFO_QUEUE_MESSAGE_SEVERITY_ERROR,
+                TRUE);
+            dxgiInfoQueue->SetBreakOnSeverity(DXGI_DEBUG_ALL_LOCAL,
+                DXGI_INFO_QUEUE_MESSAGE_SEVERITY_CORRUPTION,
+                TRUE);
+            dxgiInfoQueue->Release();
+        }
     }
 #endif
     IDXGIFactory6* factory;
@@ -929,106 +956,8 @@ void DirectX12::initialize(
     Image* renderB = new Image(1);
     renderB->setAlpha3D(1);
     renderB->newImage(this->backBufferSize.x, this->backBufferSize.y, 0);
-    uiTexture = createOrGetTexture("_f_Render_Image", renderB, RAM_TO_GPU);
-
-    D3D12_FEATURE_DATA_ROOT_SIGNATURE featureData = {};
-    featureData.HighestVersion = D3D_ROOT_SIGNATURE_VERSION_1_1;
-    if (FAILED(device->CheckFeatureSupport(
-            D3D12_FEATURE_ROOT_SIGNATURE, &featureData, sizeof(featureData))))
-        featureData.HighestVersion = D3D_ROOT_SIGNATURE_VERSION_1_0;
-
-    D3D12_ROOT_SIGNATURE_FLAGS rootSignatureFlags
-        = D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT
-        | D3D12_ROOT_SIGNATURE_FLAG_DENY_HULL_SHADER_ROOT_ACCESS
-        | D3D12_ROOT_SIGNATURE_FLAG_DENY_DOMAIN_SHADER_ROOT_ACCESS
-        | D3D12_ROOT_SIGNATURE_FLAG_DENY_GEOMETRY_SHADER_ROOT_ACCESS;
-
-    D3D12_DESCRIPTOR_RANGE1 range[2];
-    range[0].NumDescriptors = 5;
-    range[0].BaseShaderRegister = 0;
-    range[0].RegisterSpace = 0;
-    range[0].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_CBV;
-    range[0].OffsetInDescriptorsFromTableStart
-        = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
-    range[0].Flags = D3D12_DESCRIPTOR_RANGE_FLAG_NONE;
-    range[1].NumDescriptors = 1;
-    range[1].BaseShaderRegister = 0;
-    range[1].RegisterSpace = 0;
-    range[1].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
-    range[1].OffsetInDescriptorsFromTableStart
-        = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
-    range[1].Flags = D3D12_DESCRIPTOR_RANGE_FLAG_NONE;
-
-    D3D12_ROOT_PARAMETER1 rootParameters[1];
-    rootParameters[0].ParameterType
-        = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
-    rootParameters[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
-    rootParameters[0].DescriptorTable.NumDescriptorRanges = 2;
-    rootParameters[0].DescriptorTable.pDescriptorRanges = range;
-
-    D3D12_STATIC_SAMPLER_DESC sampler = {};
-    sampler.Filter = D3D12_FILTER_MIN_MAG_MIP_POINT;
-    sampler.AddressU = D3D12_TEXTURE_ADDRESS_MODE_BORDER;
-    sampler.AddressV = D3D12_TEXTURE_ADDRESS_MODE_BORDER;
-    sampler.AddressW = D3D12_TEXTURE_ADDRESS_MODE_BORDER;
-    sampler.MipLODBias = 0;
-    sampler.MaxAnisotropy = 0;
-    sampler.ComparisonFunc = D3D12_COMPARISON_FUNC_NEVER;
-    sampler.BorderColor = D3D12_STATIC_BORDER_COLOR_TRANSPARENT_BLACK;
-    sampler.MinLOD = 0.0f;
-    sampler.MaxLOD = D3D12_FLOAT32_MAX;
-    sampler.ShaderRegister = 0;
-    sampler.RegisterSpace = 0;
-    sampler.ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
-
-    D3D12_VERSIONED_ROOT_SIGNATURE_DESC rootSignatureDescription;
-    rootSignatureDescription.Version = D3D_ROOT_SIGNATURE_VERSION_1_1;
-    rootSignatureDescription.Desc_1_1.NumParameters = 1;
-    rootSignatureDescription.Desc_1_1.pParameters = rootParameters;
-    rootSignatureDescription.Desc_1_1.NumStaticSamplers = 1;
-    rootSignatureDescription.Desc_1_1.pStaticSamplers = &sampler;
-    rootSignatureDescription.Desc_1_1.Flags = rootSignatureFlags;
-
-    ID3DBlob* rootSignature;
-    ID3DBlob* error;
-    res = D3DX12SerializeVersionedRootSignature(&rootSignatureDescription,
-        featureData.HighestVersion,
-        &rootSignature,
-        &error,
-        d3d12svrsf,
-        d3d12srsf);
-    if (FAILED(res))
-    {
-        factory->Release();
-        Logging::error()
-            << "ERROR: D3DX12SerializeVersionedRootSignature returned " << res
-            << "\n";
-        WMessageBox(fenster->getWindowHandle(),
-            new Text("Fehler"),
-            new Text(
-                "D3DX12SerializeVersionedRootSignature ist Fehlgeschlagen."),
-            MB_ICONERROR);
-        if (error) error->Release();
-        return;
-    }
-
-    res = device->CreateRootSignature(0,
-        rootSignature->GetBufferPointer(),
-        rootSignature->GetBufferSize(),
-        __uuidof(ID3D12RootSignature),
-        (void**)&signature);
-    if (FAILED(res))
-    {
-        factory->Release();
-        Logging::error() << "ERROR: CreateRootSignature returned " << res
-                         << "\n";
-        WMessageBox(fenster->getWindowHandle(),
-            new Text("Fehler"),
-            new Text("CreateRootSignature ist Fehlgeschlagen."),
-            MB_ICONERROR);
-        return;
-    }
-    rootSignature->Release();
+    uiTexture = dynamic_cast<DX12Texture*>(
+        createOrGetTexture("_f_Render_Image", renderB, RAM_TO_GPU));
 
     D3D12_RASTERIZER_DESC rdesc;
     rdesc.FillMode = D3D12_FILL_MODE_SOLID;
@@ -1126,10 +1055,10 @@ void DirectX12::presentFrame()
 
     directCommandQueue->execute();
 
-    swapChain->Present(0, DXGI_PRESENT_ALLOW_TEARING);
+    UINT presentFlags = tearing ? DXGI_PRESENT_ALLOW_TEARING : 0;
+    swapChain->Present(0, presentFlags);
 
     globalDescriptorHeap->setHeapChanged(0);
-    textureDescriptorHeap->setHeapChanged(0);
     samplerDescriptorHeap->setHeapChanged(0);
 
     backBufferIndex = swapChain->GetCurrentBackBufferIndex();
@@ -1156,11 +1085,11 @@ Texture* DirectX12::createOrGetTexture(
     if (b) ret->setImageZ(b);
     texturRegister->addTexture(dynamic_cast<Texture*>(ret->getThis()), name);
     ret->updateTextur();
-    if (textureDescriptorHeap)
+    if (globalDescriptorHeap)
     {
-        textureDescriptorHeap->addTextureInput(
+        globalDescriptorHeap->addTextureInput(
             DX12_SHADER_REGISTER_T_SHADER_RESOURCE, ret);
-        textureDescriptorHeap->updateDescriptorHeap(device);
+        globalDescriptorHeap->updateDescriptorHeap(device);
         ret->setBufferChanged(0);
     }
     // directCommandQueue->execute();
@@ -1213,7 +1142,6 @@ void Framework::DirectX12::setPipeline(DX12Pipeline* pipeline)
     this->pipeline = pipeline;
     if (pipeline)
     {
-        initializePipeline();
         if (!globalDescriptorHeap
             || globalDescriptorHeap->zPipeline() != pipeline)
         {
@@ -1225,17 +1153,6 @@ void Framework::DirectX12::setPipeline(DX12Pipeline* pipeline)
                 = pipeline->createGlobalDescriptorHeap(GLOBAL_DESCRIPTOR_HEAP);
             initializeGlobalDescriptorHeap();
         }
-        if (!textureDescriptorHeap
-            || textureDescriptorHeap->zPipeline() != pipeline)
-        {
-            if (textureDescriptorHeap)
-            {
-                textureDescriptorHeap->release();
-            }
-            textureDescriptorHeap
-                = pipeline->createGlobalDescriptorHeap(TEXTURE_DESCRIPTOR_HEAP);
-            initializeTextureDescriptorHeap();
-        }
         if (!samplerDescriptorHeap
             || samplerDescriptorHeap->zPipeline() != pipeline)
         {
@@ -1247,6 +1164,7 @@ void Framework::DirectX12::setPipeline(DX12Pipeline* pipeline)
                 = pipeline->createGlobalDescriptorHeap(SAMPLER_DESCRIPTOR_HEAP);
             initializeSamplerDescriptorHeap();
         }
+        initializePipeline();
     }
 }
 
@@ -1283,8 +1201,10 @@ bool DirectX12::isAvailable()
         = (D3D12GetDebugInterfaceFunction)GetProcAddress(
             d3d12DLL, "D3D12GetDebugInterface");
     ID3D12Debug* debug = 0;
-    getDebugInterface(__uuidof(ID3D12Debug), (void**)&debug);
-    debug->EnableDebugLayer();
+    if (getDebugInterface
+        && SUCCEEDED(getDebugInterface(__uuidof(ID3D12Debug), (void**)&debug))
+        && debug)
+        debug->EnableDebugLayer();
 #endif
     IDXGIFactory4* factory;
     UINT createFactoryFlags = 0;
@@ -1319,7 +1239,7 @@ bool DirectX12::isAvailable()
                 current->Release();
                 factory->Release();
 #ifdef _DEBUG
-                debug->Release();
+                if (debug) debug->Release();
 #endif
                 getDLLRegister()->releaseDLL("dxgi.dll");
                 getDLLRegister()->releaseDLL("d3d12.dll");
@@ -1329,6 +1249,9 @@ bool DirectX12::isAvailable()
         }
     } while (res != DXGI_ERROR_NOT_FOUND);
     factory->Release();
+#ifdef _DEBUG
+    if (debug) debug->Release();
+#endif
     getDLLRegister()->releaseDLL("dxgi.dll");
     getDLLRegister()->releaseDLL("d3d12.dll");
     return 0;

+ 10 - 7
DX12GraphicsApi.h

@@ -48,6 +48,7 @@ namespace Framework
     class DX12BLAS;
     class DX12SamplerState;
     class World3D;
+    class DX12Texture;
 
     struct RayGenerationSettings
     {
@@ -68,23 +69,22 @@ namespace Framework
         ID3D12Resource* backBuffer[2];
         int backBufferIndex;
         int tearing;
-        ID3D12RootSignature* signature;
         Mat4<float> matrixBuffer[MAX_KNOCHEN_ANZ];
         Mat4<float> viewAndProj[2];
         Vec3<float> kamPos;
-        Texture* uiTexture;
         TextureList* texturRegister;
         DX12BLASModel** blasModels;
         DX12TLAS** worldTLAS;
         DX12ShaderBindingTable** worldShaderBindingTables;
         int lastTLASId;
         int lastModelId;
-        DX12Texture* defaultRenderTarget;
         Cam3D* defaultKamera;
         int cameraCount;
         Critical cs;
 
     protected:
+        DX12Texture* defaultRenderTarget;
+        DX12Texture* uiTexture;
         DX12Buffer* rayGenSettingsBuffer;
         RayGenerationSettings settings;
         ID3D12Device5* device;
@@ -92,7 +92,6 @@ namespace Framework
         PFN_D3D12_SERIALIZE_ROOT_SIGNATURE pfnD3D12SerializeRootSignature;
         DX12Pipeline* pipeline;
         DX12GlobalDescriptorHeap* globalDescriptorHeap;
-        DX12GlobalDescriptorHeap* textureDescriptorHeap;
         DX12GlobalDescriptorHeap* samplerDescriptorHeap;
         DX12ShaderHitGroup* defaultHitGroup;
         DX12ShaderFunction* defaultRayGenerationShaderFunction;
@@ -101,6 +100,9 @@ namespace Framework
         int* sbtIndexBufferOffset;
         int* sbtVertexDataBufferOffset;
         int* sbtPolygonSizeBufferOffset;
+        int firstTextureHeapIndex;
+        int* globalRayGenerationSettingsOffset;
+        int* globalTLASoffset;
 
     public:
         DLLEXPORT DirectX12();
@@ -116,7 +118,6 @@ namespace Framework
             int& objectIndex);
         DLLEXPORT virtual void initializePipeline();
         DLLEXPORT virtual void initializeGlobalDescriptorHeap();
-        DLLEXPORT virtual void initializeTextureDescriptorHeap();
         DLLEXPORT virtual void initializeSamplerDescriptorHeap();
         DLLEXPORT virtual void fillShaderBindingTable(
             DX12ShaderBindingTable* zShaderBindingTable,
@@ -124,8 +125,10 @@ namespace Framework
             int objectIndex,
             const DX12BLAS* zBLAS,
             int& lastHitGroupIndex);
-        DLLEXPORT virtual int fillGlobalShaderParams(
-            Cam3D* zCam, DX12TLAS* zTLAS, int startIndex = 0);
+        DLLEXPORT virtual int fillGlobalShaderParams(Cam3D* zCam,
+            DX12TLAS* zTLAS,
+            DX12Texture* zTarget,
+            int startIndex = 0);
 
     public:
         DLLEXPORT void initialize(NativeWindow* fenster,

+ 37 - 69
DX12Shader.cpp

@@ -202,20 +202,17 @@ void Framework::DX12ShaderSignature::createSignature(ID3D12Device5* zDevice,
         signature->Release();
         signature = 0;
     }
-    int paramCount = (descriptorHeapBindings.getEntryCount() > 0
-                             ? __DESCRIPTOR_HEAP_TYPE_COUNT__
-                             : 0)
+    int paramCount = (descriptorHeapBindings.getEntryCount() > 0 ? 2 : 0)
                    + bindingTableBindings.getEntryCount();
     D3D12_ROOT_PARAMETER* descriptorTable
         = new D3D12_ROOT_PARAMETER[paramCount];
     int index = 0;
-    D3D12_DESCRIPTOR_RANGE** descriptorRanges
-        = new D3D12_DESCRIPTOR_RANGE*[__DESCRIPTOR_HEAP_TYPE_COUNT__];
+    D3D12_DESCRIPTOR_RANGE** descriptorRanges = new D3D12_DESCRIPTOR_RANGE*[2];
     int rangeCount = 0;
     ArrayIterator<DX12ShaderRegisterUsage*> it = descriptorHeapBindings.begin();
     DX12DescriptorHeapType currentDescriptorHeapType;
     useGlobalDescriptorHeap = 0;
-    useTextureDescriptorHeap = 0;
+    useSamplerDescriptorHeap = 0;
     while (it)
     {
         descriptorTable[index].ParameterType
@@ -229,9 +226,9 @@ void Framework::DX12ShaderSignature::createSignature(ID3D12Device5* zDevice,
         {
             useGlobalDescriptorHeap = true;
         }
-        else if (currentDescriptorHeapType == TEXTURE_DESCRIPTOR_HEAP)
+        else if (currentDescriptorHeapType == SAMPLER_DESCRIPTOR_HEAP)
         {
-            useTextureDescriptorHeap = true;
+            useSamplerDescriptorHeap = true;
         }
         rangeCount = 0;
         while (it && it->descriptorHeapType == currentDescriptorHeapType)
@@ -362,11 +359,6 @@ bool Framework::DX12ShaderSignature::doesUseGlobalDescriptorHeap() const
     return useGlobalDescriptorHeap;
 }
 
-bool Framework::DX12ShaderSignature::doesUseTextureDescriptorHeap() const
-{
-    return useTextureDescriptorHeap;
-}
-
 bool Framework::DX12ShaderSignature::doesUseSamplerDescriptorHeap() const
 {
     return useSamplerDescriptorHeap;
@@ -1479,13 +1471,17 @@ bool Framework::DX12GlobalDescriptorHeap::wasHeapChanged() const
     return heapChanged;
 }
 
+int Framework::DX12GlobalDescriptorHeap::getRegisterInputCount() const
+{
+    return registerInputs.getEntryCount();
+}
+
 Framework::DX12ShaderBindingTable::DX12ShaderBindingTable(
     DX12Pipeline* pipeline)
     : ReferenceCounter(),
       pipeline(pipeline),
       shaderBindingTableBuffer(0),
       globalDescriptorHeap(0),
-      textureDescriptorHeap(0),
       samplerDescriptorHeap(0),
       rayGenRecordSize(0),
       rayGenCount(0),
@@ -1519,10 +1515,6 @@ Framework::DX12ShaderBindingTable::~DX12ShaderBindingTable()
     {
         globalDescriptorHeap->release();
     }
-    if (textureDescriptorHeap)
-    {
-        textureDescriptorHeap->release();
-    }
     if (samplerDescriptorHeap)
     {
         samplerDescriptorHeap->release();
@@ -1550,23 +1542,6 @@ void Framework::DX12ShaderBindingTable::setGlobalDescriptorHeap(
     }
 }
 
-void Framework::DX12ShaderBindingTable::setTextureDescriptorHeap(
-    DX12GlobalDescriptorHeap* zTextureDescriptorHeap)
-{
-    if (this->textureDescriptorHeap != zTextureDescriptorHeap)
-    {
-        if (this->textureDescriptorHeap)
-        {
-            this->textureDescriptorHeap->release();
-        }
-        this->textureDescriptorHeap = zTextureDescriptorHeap;
-        if (this->textureDescriptorHeap)
-        {
-            this->textureDescriptorHeap->getThis();
-        }
-    }
-}
-
 void Framework::DX12ShaderBindingTable::setSamplerDescriptorHeap(
     DX12GlobalDescriptorHeap* zSamplerDescriptorHeap)
 {
@@ -1657,10 +1632,12 @@ void Framework::DX12ShaderBindingTable::startUpdate()
     hitGroupRecordSize = ROUND_UP_POWER_OF_2(
         D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT + hitGroupRecordSize * 8,
         D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT);
-    nextHitGroupOffset = ROUND_UP_POWER_OF_2(
-        rayGenRecordSize * rayGenCount + missRecordSize * missCount
-            + callableRecordSize * callableCount,
-        64);
+    nextHitGroupOffset
+        = ROUND_UP_POWER_OF_2(rayGenRecordSize * rayGenCount, 64)
+        + ROUND_UP_POWER_OF_2(missRecordSize * missCount, 64)
+        + (callableCount > 0
+                ? ROUND_UP_POWER_OF_2(callableRecordSize * callableCount, 64)
+                : 0);
 }
 
 void Framework::DX12ShaderBindingTable::set(int index, void* data, int size)
@@ -1726,11 +1703,13 @@ void Framework::DX12ShaderBindingTable::setShaderInput(
     }
     else if (zFunction->getFunctionType() == DX12_SHADER_FUNCTION_TYPE_MISS)
     {
-        offset = rayGenRecordSize * rayGenCount + index * missRecordSize;
+        offset = ROUND_UP_POWER_OF_2(rayGenRecordSize * rayGenCount, 64)
+               + index * missRecordSize;
     }
     else if (zFunction->getFunctionType() == DX12_SHADER_FUNCTION_TYPE_CALLABLE)
     {
-        offset = rayGenRecordSize * rayGenCount + missRecordSize * missCount
+        offset = ROUND_UP_POWER_OF_2(rayGenRecordSize * rayGenCount, 64)
+               + ROUND_UP_POWER_OF_2(missRecordSize * missCount, 64)
                + index * callableRecordSize;
     }
     else
@@ -1769,17 +1748,6 @@ int Framework::DX12ShaderBindingTable::addHitGroup(
         }
         index += sizeof(__int64);
     }
-    if (zHitGroup->zSignature()->doesUseTextureDescriptorHeap())
-    {
-        if ((changed || textureDescriptorHeap->wasHeapChanged()))
-        {
-            D3D12_GPU_DESCRIPTOR_HANDLE gpuAddress
-                = textureDescriptorHeap->zDescriptorHeap()
-                      ->GetGPUDescriptorHandleForHeapStart();
-            set(index, &gpuAddress.ptr, sizeof(__int64));
-        }
-        index += sizeof(__int64);
-    }
     if (zHitGroup->zSignature()->doesUseSamplerDescriptorHeap())
     {
         if ((changed || samplerDescriptorHeap->wasHeapChanged()))
@@ -1823,14 +1791,15 @@ void Framework::DX12ShaderBindingTable::endUpdate(
         }
         else if (function->getFunctionType() == DX12_SHADER_FUNCTION_TYPE_MISS)
         {
-            offset
-                = rayGenRecordSize * rayGenCount + missIndex * missRecordSize;
+            offset = ROUND_UP_POWER_OF_2(rayGenRecordSize * rayGenCount, 64)
+                   + missIndex * missRecordSize;
             missIndex++;
         }
         else if (function->getFunctionType()
                  == DX12_SHADER_FUNCTION_TYPE_CALLABLE)
         {
-            offset = rayGenRecordSize * rayGenCount + missRecordSize * missCount
+            offset = ROUND_UP_POWER_OF_2(rayGenRecordSize * rayGenCount, 64)
+                   + ROUND_UP_POWER_OF_2(missRecordSize * missCount, 64)
                    + callableIndex * callableRecordSize;
             callableIndex++;
         }
@@ -1849,14 +1818,6 @@ void Framework::DX12ShaderBindingTable::endUpdate(
                 set(offset, &gpuAddress.ptr, sizeof(__int64));
                 offset += sizeof(__int64);
             }
-            if (function->zSignature()->doesUseTextureDescriptorHeap())
-            {
-                D3D12_GPU_DESCRIPTOR_HANDLE gpuAddress
-                    = textureDescriptorHeap->zDescriptorHeap()
-                          ->GetGPUDescriptorHandleForHeapStart();
-                set(offset, &gpuAddress.ptr, sizeof(__int64));
-                offset += sizeof(__int64);
-            }
             if (function->zSignature()->doesUseSamplerDescriptorHeap())
             {
                 D3D12_GPU_DESCRIPTOR_HANDLE gpuAddress
@@ -1923,17 +1884,18 @@ void Framework::DX12ShaderBindingTable::fillDispatchRaysDesc(
     dispatchRaysDesc->MissShaderTable.SizeInBytes = missCount * missRecordSize;
     dispatchRaysDesc->MissShaderTable.StartAddress
         = shaderBindingTableBuffer->zBuffer()->GetGPUVirtualAddress()
-        + rayGenCount * rayGenRecordSize;
+        + ROUND_UP_POWER_OF_2(rayGenCount * rayGenRecordSize, 64);
     dispatchRaysDesc->MissShaderTable.StrideInBytes = missRecordSize;
 
     dispatchRaysDesc->HitGroupTable.SizeInBytes
         = hitGroupCount * hitGroupRecordSize;
     dispatchRaysDesc->HitGroupTable.StartAddress
         = shaderBindingTableBuffer->zBuffer()->GetGPUVirtualAddress()
-        + ROUND_UP_POWER_OF_2(rayGenCount * rayGenRecordSize
-                                  + missCount * missRecordSize
-                                  + callableCount * callableRecordSize,
-            64);
+        + ROUND_UP_POWER_OF_2(rayGenCount * rayGenRecordSize, 64)
+        + ROUND_UP_POWER_OF_2(missCount * missRecordSize, 64)
+        + (callableCount > 0
+                ? ROUND_UP_POWER_OF_2(callableCount * callableRecordSize, 64)
+                : 0);
     dispatchRaysDesc->HitGroupTable.StrideInBytes = hitGroupRecordSize;
 
     dispatchRaysDesc->CallableShaderTable.SizeInBytes
@@ -1941,7 +1903,8 @@ void Framework::DX12ShaderBindingTable::fillDispatchRaysDesc(
     dispatchRaysDesc->CallableShaderTable.StartAddress
         = callableCount > 0
             ? shaderBindingTableBuffer->zBuffer()->GetGPUVirtualAddress()
-                  + rayGenCount * rayGenRecordSize + missCount * missRecordSize
+                  + ROUND_UP_POWER_OF_2(rayGenCount * rayGenRecordSize, 64)
+                  + ROUND_UP_POWER_OF_2(missCount * missRecordSize, 64)
             : 0;
     dispatchRaysDesc->CallableShaderTable.StrideInBytes
         = callableCount > 0 ? callableRecordSize : 0;
@@ -1951,3 +1914,8 @@ DX12Pipeline* Framework::DX12ShaderBindingTable::zPipeline() const
 {
     return pipeline;
 }
+
+int Framework::DX12ShaderBindingTable::getHitGoupCount() const
+{
+    return hitGroupCount;
+}

+ 3 - 8
DX12Shader.h

@@ -27,9 +27,7 @@ namespace Framework
     enum DX12DescriptorHeapType
     {
         GLOBAL_DESCRIPTOR_HEAP = 0,
-        TEXTURE_DESCRIPTOR_HEAP = 1,
-        SAMPLER_DESCRIPTOR_HEAP = 2,
-        __DESCRIPTOR_HEAP_TYPE_COUNT__
+        SAMPLER_DESCRIPTOR_HEAP = 1
     };
 
     struct DX12ShaderRegisterUsage
@@ -55,7 +53,6 @@ namespace Framework
         bool changed;
         int shaderBindingTableParamCount;
         bool useGlobalDescriptorHeap;
-        bool useTextureDescriptorHeap;
         bool useSamplerDescriptorHeap;
         bool global;
 
@@ -122,7 +119,6 @@ namespace Framework
         getDescriptorHeapBindings() const;
         DLLEXPORT int gerShaderBindingTableParamCount() const;
         DLLEXPORT bool doesUseGlobalDescriptorHeap() const;
-        DLLEXPORT bool doesUseTextureDescriptorHeap() const;
         DLLEXPORT bool doesUseSamplerDescriptorHeap() const;
     };
 
@@ -281,6 +277,7 @@ namespace Framework
         DLLEXPORT ID3D12DescriptorHeap* zDescriptorHeap() const;
         DLLEXPORT void setHeapChanged(bool changed);
         DLLEXPORT bool wasHeapChanged() const;
+        DLLEXPORT int getRegisterInputCount() const;
     };
 
     class DX12ShaderBindingTable : public ReferenceCounter
@@ -289,7 +286,6 @@ namespace Framework
         DX12Pipeline* pipeline;
         DX12Buffer* shaderBindingTableBuffer;
         DX12GlobalDescriptorHeap* globalDescriptorHeap;
-        DX12GlobalDescriptorHeap* textureDescriptorHeap;
         DX12GlobalDescriptorHeap* samplerDescriptorHeap;
         int rayGenRecordSize;
         int rayGenCount;
@@ -310,8 +306,6 @@ namespace Framework
         DLLEXPORT ~DX12ShaderBindingTable();
         DLLEXPORT void setGlobalDescriptorHeap(
             DX12GlobalDescriptorHeap* zGlobalDescriptorHeap);
-        DLLEXPORT void setTextureDescriptorHeap(
-            DX12GlobalDescriptorHeap* zTextureDescriptorHeap);
         DLLEXPORT void setSamplerDescriptorHeap(
             DX12GlobalDescriptorHeap* zSamplerDescriptorHeap);
         DLLEXPORT void startUpdate();
@@ -372,5 +366,6 @@ namespace Framework
         DLLEXPORT void fillDispatchRaysDesc(
             D3D12_DISPATCH_RAYS_DESC* dispatchRaysDesc);
         DLLEXPORT DX12Pipeline* zPipeline() const;
+        DLLEXPORT int getHitGoupCount() const;
     };
 } // namespace Framework

+ 0 - 17
RayGen.hlsl

@@ -1,22 +1,5 @@
 #include "Common.hlsl"
 
-// Raytracing output texture, accessed as a UAV
-RWTexture2D<float4> gOutput : register(u0);
-
-RWTexture2D<float4> guiTexture : register(u1);
-
-// Raytracing acceleration structure, accessed as a SRV
-RaytracingAccelerationStructure TLAS : register(t0, space0);
-
-cbuffer RayGenerationSettings : register(b0)
-{
-    int renderGui;
-    int useRays;
-    float minDistance;
-    float maxDistance;
-    float4x4 inverseView;
-    float4x4 inverseProjection;
-}
 
 [shader("raygeneration")]
 void RayGen()