Procházet zdrojové kódy

build top level aceleration structure for raytracing with directx 12

Kolja Strohm před 1 měsícem
rodič
revize
c74cfc915a
14 změnil soubory, kde provedl 347 přidání a 69 odebrání
  1. 5 3
      DX12BLAS.cpp
  2. 16 16
      DX12CommandQueue.cpp
  3. 8 8
      DX12CommandQueue.h
  4. 30 30
      DX12GraphicsApi.cpp
  5. 3 1
      DX12GraphicsApi.h
  6. 212 0
      DX12TLAS.cpp
  7. 33 0
      DX12TLAS.h
  8. 4 4
      DX12Texture.cpp
  9. 4 5
      Dialog.cpp
  10. 2 0
      Framework.vcxproj
  11. 6 0
      Framework.vcxproj.filters
  12. 4 0
      OperatingSystem.h
  13. 13 2
      World3D.cpp
  14. 7 0
      World3D.h

+ 5 - 3
DX12BLAS.cpp

@@ -58,9 +58,11 @@ void Framework::DX12BLAS::build(
     zDevice->GetRaytracingAccelerationStructurePrebuildInfo(
         &prebuildDesc, &info);
 
-    scratchBuffer->setLength((int)info.ScratchDataSizeInBytes);
+    scratchBuffer->setLength(
+        ROUND_UP_POWER_OF_2((int)info.ScratchDataSizeInBytes, 256));
     scratchBuffer->createBufferWithoutData(D3D12_RESOURCE_STATE_COMMON);
-    resultBuffer->setLength((int)info.ResultDataMaxSizeInBytes);
+    resultBuffer->setLength(
+        ROUND_UP_POWER_OF_2((int)info.ResultDataMaxSizeInBytes, 256));
     resultBuffer->createBufferWithoutData(
         D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE);
 
@@ -79,7 +81,7 @@ void Framework::DX12BLAS::build(
         = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_NONE;
 
     // Build the AS
-    zDirectQueue->getCommandList()->BuildRaytracingAccelerationStructure(
+    zDirectQueue->zCommandList()->BuildRaytracingAccelerationStructure(
         &buildDesc, 0, nullptr);
 }
 

+ 16 - 16
DX12CommandQueue.cpp

@@ -3,15 +3,15 @@
 #include <d3d12.h>
 #include <iostream>
 
-#include "Window.h"
 #include "Logging.h"
 #include "Text.h"
+#include "Window.h"
 
 using namespace Framework;
 
-DX12CommandQueue::DX12CommandQueue(int typ, ID3D12Device* device)
+DX12CommandQueue::DX12CommandQueue(int typ, ID3D12Device* zDevice)
     : ReferenceCounter(),
-      device(device),
+      zDevice(zDevice),
       event(CreateEvent(0, 0, 0, 0)),
       fenceValue(0)
 {
@@ -20,7 +20,7 @@ DX12CommandQueue::DX12CommandQueue(int typ, ID3D12Device* device)
     desc.Priority = D3D12_COMMAND_QUEUE_PRIORITY_NORMAL;
     desc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
     desc.NodeMask = 0;
-    HRESULT res = device->CreateCommandQueue(
+    HRESULT res = zDevice->CreateCommandQueue(
         &desc, __uuidof(ID3D12CommandQueue), (void**)&queue);
     if (FAILED(res))
     {
@@ -32,7 +32,7 @@ DX12CommandQueue::DX12CommandQueue(int typ, ID3D12Device* device)
             MB_ICONERROR);
     }
 
-    res = device->CreateCommandAllocator((D3D12_COMMAND_LIST_TYPE)typ,
+    res = zDevice->CreateCommandAllocator((D3D12_COMMAND_LIST_TYPE)typ,
         __uuidof(ID3D12CommandAllocator),
         (void**)&allocator);
     if (FAILED(res))
@@ -45,7 +45,7 @@ DX12CommandQueue::DX12CommandQueue(int typ, ID3D12Device* device)
             MB_ICONERROR);
     }
 
-    res = device->CreateCommandList(0,
+    res = zDevice->CreateCommandList(0,
         (D3D12_COMMAND_LIST_TYPE)typ,
         allocator,
         nullptr,
@@ -60,7 +60,7 @@ DX12CommandQueue::DX12CommandQueue(int typ, ID3D12Device* device)
             MB_ICONERROR);
     }
 
-    res = device->CreateFence(
+    res = zDevice->CreateFence(
         0, D3D12_FENCE_FLAG_NONE, __uuidof(ID3D12Fence), (void**)&fence);
     if (FAILED(res))
     {
@@ -109,17 +109,17 @@ void DX12CommandQueue::flush()
     whaitForGPUSignal(addSignalFromGPU());
 }
 
-ID3D12CommandAllocator* DX12CommandQueue::getAllocator() const
+ID3D12CommandAllocator* DX12CommandQueue::zAllocator() const
 {
     return allocator;
 }
 
-ID3D12GraphicsCommandList4* DX12CommandQueue::getCommandList() const
+ID3D12GraphicsCommandList4* DX12CommandQueue::zCommandList() const
 {
     return commandList;
 }
 
-ID3D12CommandQueue* DX12CommandQueue::getQueue() const
+ID3D12CommandQueue* DX12CommandQueue::zQueue() const
 {
     return queue;
 }
@@ -133,14 +133,14 @@ void DX12CommandQueue::execute()
     commandList->Reset(allocator, nullptr);
 }
 
-DX12DirectCommandQueue::DX12DirectCommandQueue(ID3D12Device* device)
-    : DX12CommandQueue(D3D12_COMMAND_LIST_TYPE_DIRECT, device)
+DX12DirectCommandQueue::DX12DirectCommandQueue(ID3D12Device* zDevice)
+    : DX12CommandQueue(D3D12_COMMAND_LIST_TYPE_DIRECT, zDevice)
 {}
 
-DX12CopyCommandQueue::DX12CopyCommandQueue(ID3D12Device* device)
-    : DX12CommandQueue(D3D12_COMMAND_LIST_TYPE_COPY, device)
+DX12CopyCommandQueue::DX12CopyCommandQueue(ID3D12Device* zDevice)
+    : DX12CommandQueue(D3D12_COMMAND_LIST_TYPE_COPY, zDevice)
 {}
 
-DX12ComputeCommandQueue::DX12ComputeCommandQueue(ID3D12Device* device)
-    : DX12CommandQueue(D3D12_COMMAND_LIST_TYPE_COMPUTE, device)
+DX12ComputeCommandQueue::DX12ComputeCommandQueue(ID3D12Device* zDevice)
+    : DX12CommandQueue(D3D12_COMMAND_LIST_TYPE_COMPUTE, zDevice)
 {}

+ 8 - 8
DX12CommandQueue.h

@@ -21,11 +21,11 @@ namespace Framework
         ID3D12GraphicsCommandList4* commandList;
         ID3D12CommandQueue* queue;
         ID3D12Fence* fence;
-        ID3D12Device* device;
+        ID3D12Device* zDevice;
         HANDLE event;
         unsigned __int64 fenceValue;
 
-        DX12CommandQueue(int typ, ID3D12Device* device);
+        DX12CommandQueue(int typ, ID3D12Device* zDevice);
 
     public:
         virtual ~DX12CommandQueue();
@@ -33,27 +33,27 @@ namespace Framework
         void whaitForGPUSignal();
         void whaitForGPUSignal(unsigned __int64 value);
         void flush();
-        ID3D12CommandAllocator* getAllocator() const;
-        ID3D12GraphicsCommandList4* getCommandList() const;
-        ID3D12CommandQueue* getQueue() const;
+        ID3D12CommandAllocator* zAllocator() const;
+        ID3D12GraphicsCommandList4* zCommandList() const;
+        ID3D12CommandQueue* zQueue() const;
         void execute();
     };
 
     class DX12DirectCommandQueue : public DX12CommandQueue
     {
     public:
-        DX12DirectCommandQueue(ID3D12Device* device);
+        DX12DirectCommandQueue(ID3D12Device* zDevice);
     };
 
     class DX12CopyCommandQueue : public DX12CommandQueue
     {
     public:
-        DX12CopyCommandQueue(ID3D12Device* device);
+        DX12CopyCommandQueue(ID3D12Device* zDevice);
     };
 
     class DX12ComputeCommandQueue : public DX12CommandQueue
     {
     public:
-        DX12ComputeCommandQueue(ID3D12Device* device);
+        DX12ComputeCommandQueue(ID3D12Device* zDevice);
     };
 }; // namespace Framework

+ 30 - 30
DX12GraphicsApi.cpp

@@ -10,10 +10,8 @@
 #include "DLLRegister.h"
 #include "DX12BLASModel.h"
 #include "DX12CommandQueue.h"
-#include "DX12PixelShader.h"
-#include "DX12Shader.h"
 #include "DX12Texture.h"
-#include "DX12VertexShader.h"
+#include "DX12TLAS.h"
 #include "Globals.h"
 #include "Image.h"
 #include "Model3D.h"
@@ -50,6 +48,8 @@ DirectX12::DirectX12()
       uiTexture(0),
       texturRegister(new TextureList()),
       blasModels(0),
+      worldTLAS(0),
+      lastTLASId(-1),
       lastModelId(-1)
 {
     for (int i = 0; i < 2; i++)
@@ -66,6 +66,14 @@ DirectX12::~DirectX12()
         }
         delete[] blasModels;
     }
+    if (worldTLAS)
+    {
+        for (int i = 0; i <= lastTLASId; i++)
+        {
+            if (worldTLAS[i]) worldTLAS[i]->release();
+        }
+        delete[] worldTLAS;
+    }
     if (directCommandQueue)
     {
         directCommandQueue->flush();
@@ -105,10 +113,6 @@ DirectX12::~DirectX12()
         getDLLRegister()->releaseDLL("d3d12.dll");
     }
     if (debug) debug->Release();
-    for (const D3D12_RAYTRACING_INSTANCE_DESC* desc : instanceDescs)
-    {
-        delete desc;
-    }
 }
 
 void DirectX12::updateBottomLevelAccelerationStructure()
@@ -401,7 +405,7 @@ void DirectX12::initialize(
     swapChainDesc.AlphaMode = DXGI_ALPHA_MODE_IGNORE;
     swapChainDesc.Flags = tearing ? DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING : 0;
     IDXGISwapChain1* tmpSwapChain;
-    res = factory->CreateSwapChainForHwnd(directCommandQueue->getQueue(),
+    res = factory->CreateSwapChainForHwnd(directCommandQueue->zQueue(),
         fenster->getWindowHandle(),
         &swapChainDesc,
         0,
@@ -737,30 +741,32 @@ void DirectX12::renderKamera(Cam3D* zKamera)
     Mat4<float> identity = Mat4<float>::identity();
 
     World3D* w = zKamera->zWorld();
-    ArrayIterator<D3D12_RAYTRACING_INSTANCE_DESC*> instanceIterator
-        = instanceDescs.begin();
+
+    if (w->getId() < 0)
+    {
+        w->setId(++lastTLASId);
+        DX12TLAS** tmp = new DX12TLAS*[lastTLASId + 1];
+        if (lastTLASId)
+        {
+            memcpy(tmp, worldTLAS, sizeof(DX12TLAS*) * lastTLASId);
+        }
+        tmp[lastTLASId] = new DX12TLAS(device, directCommandQueue);
+        delete[] worldTLAS;
+        worldTLAS = tmp;
+    }
+    DX12TLAS* tlas = worldTLAS[w->getId()];
+    tlas->startUpdate();
     int objectIndex = 0;
     int instanceIndex = 0;
     w->render(
-        [this, &instanceIterator, &objectIndex, &instanceIndex, &identity](
-            Model3D* obj) {
+        [this, &tlas, &objectIndex, &instanceIndex, &identity](Model3D* obj) {
             obj->calculateMatrices(identity, matrixBuffer);
             int modelId = obj->zModelData()->getId();
             DX12BLASModel* blasModel = blasModels[modelId];
             ArrayIterator<int> boneIds = blasModel->zBoneIds()->begin();
             for (const DX12BLAS* blas : *blasModel->zBLAS())
             {
-                D3D12_RAYTRACING_INSTANCE_DESC* desc = 0;
-                if (instanceIndex < instanceDescs.getEntryCount())
-                {
-                    desc = instanceIterator.val();
-                    instanceIterator++;
-                }
-                else
-                {
-                    desc = new D3D12_RAYTRACING_INSTANCE_DESC();
-                    instanceDescs.add(desc);
-                }
+                D3D12_RAYTRACING_INSTANCE_DESC* desc = tlas->nextInstanceDesc();
                 desc->InstanceID = objectIndex;
                 desc->InstanceContributionToHitGroupIndex = objectIndex;
                 desc->Flags = D3D12_RAYTRACING_INSTANCE_FLAG_NONE;
@@ -775,13 +781,7 @@ void DirectX12::renderKamera(Cam3D* zKamera)
             }
             objectIndex++;
         });
-    // TODO: add gui model to instanceDescs if the gui should be rendered in
-    // this kamera
-    while (instanceIterator)
-    {
-        instanceIterator.val()->InstanceMask = 0;
-        instanceIterator++;
-    }
+    tlas->endUpdate();
 
     // TODO: call ray tracing
 }

+ 3 - 1
DX12GraphicsApi.h

@@ -35,6 +35,7 @@ namespace Framework
     class DX12BLASModel;
     class TextureList;
     class TextureModel;
+    class DX12TLAS;
 
     class DirectX12 : public GraphicsApi
     {
@@ -64,7 +65,8 @@ namespace Framework
         Texture* uiTexture;
         TextureList* texturRegister;
         DX12BLASModel** blasModels;
-        Array<D3D12_RAYTRACING_INSTANCE_DESC*> instanceDescs;
+        DX12TLAS** worldTLAS;
+        int lastTLASId;
         int lastModelId;
 
         DLLEXPORT void updateBottomLevelAccelerationStructure();

+ 212 - 0
DX12TLAS.cpp

@@ -0,0 +1,212 @@
+#include "DX12TLAS.h"
+
+#include "DX12CommandQueue.h"
+
+Framework::DX12TLAS::DX12TLAS(
+    ID3D12Device5* zDevice, Framework::DX12DirectCommandQueue* zDirectQueue)
+    : ReferenceCounter(),
+      scratchBuffer(0),
+      resultBuffer(0),
+      descriptorBuffer(0),
+      previousResultBuffer(0),
+      zDevice(zDevice),
+      zDirectQueue(zDirectQueue),
+      overflowInstanceIterator(0, 0),
+      currentInstanceIndex(0),
+      mappedDescriptorBuffer(0)
+{}
+
+Framework::DX12TLAS::~DX12TLAS()
+{
+    if (scratchBuffer)
+    {
+        scratchBuffer->release();
+    }
+    if (resultBuffer)
+    {
+        resultBuffer->release();
+    }
+    if (descriptorBuffer)
+    {
+        descriptorBuffer->release();
+    }
+    if (previousResultBuffer)
+    {
+        previousResultBuffer->release();
+    }
+    for (const D3D12_RAYTRACING_INSTANCE_DESC* desc : overflowInstanceDescs)
+    {
+        delete desc;
+    }
+}
+
+void Framework::DX12TLAS::startUpdate()
+{
+    currentInstanceIndex = -1;
+    if (descriptorBuffer)
+    {
+        descriptorBuffer->zBuffer()->Map(0, 0, (void**)&mappedDescriptorBuffer);
+    }
+    else
+    {
+        mappedDescriptorBuffer = 0;
+    }
+    overflowInstanceIterator = overflowInstanceDescs.begin();
+}
+
+D3D12_RAYTRACING_INSTANCE_DESC* Framework::DX12TLAS::nextInstanceDesc()
+{
+    currentInstanceIndex++;
+    if (mappedDescriptorBuffer
+        && currentInstanceIndex < descriptorBuffer->getElementCount())
+    {
+        return mappedDescriptorBuffer
+             + currentInstanceIndex * sizeof(D3D12_RAYTRACING_INSTANCE_DESC);
+    }
+    else if (overflowInstanceIterator)
+    {
+        return *overflowInstanceIterator++;
+    }
+    else
+    {
+        D3D12_RAYTRACING_INSTANCE_DESC* desc
+            = new D3D12_RAYTRACING_INSTANCE_DESC();
+        overflowInstanceDescs.add(desc);
+        return desc;
+    }
+}
+
+void Framework::DX12TLAS::endUpdate()
+{
+    while (currentInstanceIndex < descriptorBuffer->getElementCount() - 1)
+    {
+        D3D12_RAYTRACING_INSTANCE_DESC* desc = nextInstanceDesc();
+        desc->InstanceMask = 0; // Mark unused instances with a mask of 0
+    }
+    descriptorBuffer->zBuffer()->Unmap(0, nullptr);
+    mappedDescriptorBuffer = 0;
+    if (currentInstanceIndex >= descriptorBuffer->getElementCount())
+    {
+        // recalculate the size of the descriptor buffer and reallocate it
+        D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS
+        prebuildDesc = {};
+        prebuildDesc.Type
+            = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL;
+        prebuildDesc.DescsLayout = D3D12_ELEMENTS_LAYOUT_ARRAY;
+        prebuildDesc.NumDescs = (unsigned)currentInstanceIndex + 1;
+        prebuildDesc.Flags
+            = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_ALLOW_UPDATE;
+
+        D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO info = {};
+
+        zDevice->GetRaytracingAccelerationStructurePrebuildInfo(
+            &prebuildDesc, &info);
+
+        // create new buffers witch fit the TLAS
+        if (resultBuffer)
+        {
+            if (previousResultBuffer)
+            {
+                previousResultBuffer->release();
+            }
+            previousResultBuffer = resultBuffer;
+        }
+        resultBuffer = new DX12Buffer(
+            1, zDevice, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS);
+        resultBuffer->setLength(
+            ROUND_UP_POWER_OF_2(info.ResultDataMaxSizeInBytes, 256));
+        resultBuffer->createBufferWithoutData(
+            D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE);
+        if (!scratchBuffer)
+        {
+            scratchBuffer = new DX12Buffer(
+                1, zDevice, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS);
+        }
+        scratchBuffer->setLength(
+            ROUND_UP_POWER_OF_2(info.ScratchDataSizeInBytes, 256));
+        scratchBuffer->createBufferWithoutData(
+            D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
+        ID3D12Resource* oldDescriptorBuffer
+            = descriptorBuffer ? descriptorBuffer->zBuffer() : 0;
+        __int64 oldDescriptorBufferElementCount
+            = descriptorBuffer ? descriptorBuffer->getElementCount() : 0;
+        if (oldDescriptorBuffer)
+        {
+            oldDescriptorBuffer->AddRef();
+            oldDescriptorBuffer->Map(0, 0, (void**)&mappedDescriptorBuffer);
+        }
+        if (!descriptorBuffer)
+        {
+            descriptorBuffer
+                = new DX12Buffer(sizeof(D3D12_RAYTRACING_INSTANCE_DESC),
+                    zDevice,
+                    D3D12_RESOURCE_FLAG_NONE);
+        }
+        descriptorBuffer->setLength(ROUND_UP_POWER_OF_2(
+            (currentInstanceIndex + 1) * sizeof(D3D12_RAYTRACING_INSTANCE_DESC),
+            256));
+        descriptorBuffer->createBufferWithoutData(
+            D3D12_RESOURCE_STATE_GENERIC_READ);
+        D3D12_RAYTRACING_INSTANCE_DESC* newMappedDescriptorBuffer = 0;
+
+        // copy the old and new instance descriptions to the new buffer
+        descriptorBuffer->zBuffer()->Map(
+            0, 0, (void**)&newMappedDescriptorBuffer);
+        if (mappedDescriptorBuffer)
+        {
+            memcpy(newMappedDescriptorBuffer,
+                mappedDescriptorBuffer,
+                oldDescriptorBufferElementCount
+                    * sizeof(D3D12_RAYTRACING_INSTANCE_DESC));
+            D3D12_RANGE range = {0, 0}; // do not write to the old buffer
+            oldDescriptorBuffer->Unmap(0, &range);
+            oldDescriptorBuffer->Release();
+        }
+        overflowInstanceIterator = overflowInstanceDescs.begin();
+        for (__int64 i = oldDescriptorBufferElementCount;
+            i <= currentInstanceIndex;
+            i++)
+        {
+            memcpy(newMappedDescriptorBuffer + i,
+                overflowInstanceIterator.val(),
+                sizeof(D3D12_RAYTRACING_INSTANCE_DESC));
+            overflowInstanceIterator++;
+        }
+        descriptorBuffer->zBuffer()->Unmap(0, nullptr);
+    }
+    D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC buildDesc = {};
+    buildDesc.Inputs.Type
+        = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL;
+    buildDesc.Inputs.DescsLayout = D3D12_ELEMENTS_LAYOUT_ARRAY;
+    buildDesc.Inputs.InstanceDescs
+        = descriptorBuffer->zBuffer()->GetGPUVirtualAddress();
+    buildDesc.Inputs.NumDescs = (unsigned)currentInstanceIndex + 1;
+    buildDesc.DestAccelerationStructureData
+        = {resultBuffer->zBuffer()->GetGPUVirtualAddress()};
+    buildDesc.ScratchAccelerationStructureData
+        = {scratchBuffer->zBuffer()->GetGPUVirtualAddress()};
+    buildDesc.SourceAccelerationStructureData
+        = previousResultBuffer
+            ? previousResultBuffer->zBuffer()->GetGPUVirtualAddress()
+            : 0;
+    buildDesc.Inputs.Flags
+        = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PERFORM_UPDATE;
+
+    // Build the top-level AS
+    zDirectQueue->zCommandList()->BuildRaytracingAccelerationStructure(
+        &buildDesc, 0, nullptr);
+
+    // Wait for the builder to complete by setting a barrier on the resulting
+    // buffer. This can be important in case the rendering is triggered
+    // immediately afterwards, without executing the command list
+    D3D12_RESOURCE_BARRIER uavBarrier;
+    uavBarrier.Type = D3D12_RESOURCE_BARRIER_TYPE_UAV;
+    uavBarrier.UAV.pResource = resultBuffer->zBuffer();
+    uavBarrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
+    zDirectQueue->zCommandList()->ResourceBarrier(1, &uavBarrier);
+}
+
+Framework::DX12Buffer* Framework::DX12TLAS::zResultBuffer() const
+{
+    return resultBuffer;
+}

+ 33 - 0
DX12TLAS.h

@@ -0,0 +1,33 @@
+#pragma once
+
+#include "Array.h"
+#include "DX12Buffer.h"
+
+namespace Framework
+{
+    class DX12DirectCommandQueue;
+
+    class DX12TLAS : public ReferenceCounter
+    {
+    private:
+        DX12Buffer* scratchBuffer;
+        DX12Buffer* resultBuffer;
+        DX12Buffer* descriptorBuffer;
+        DX12Buffer* previousResultBuffer;
+        ID3D12Device5* zDevice;
+        DX12DirectCommandQueue* zDirectQueue;
+        Array<D3D12_RAYTRACING_INSTANCE_DESC*> overflowInstanceDescs;
+        ArrayIterator<D3D12_RAYTRACING_INSTANCE_DESC*> overflowInstanceIterator;
+        __int64 currentInstanceIndex;
+        D3D12_RAYTRACING_INSTANCE_DESC* mappedDescriptorBuffer;
+
+    public:
+        DLLEXPORT DX12TLAS(
+            ID3D12Device5* zDevice, DX12DirectCommandQueue* zDirectQueue);
+        DLLEXPORT virtual ~DX12TLAS();
+        DLLEXPORT void startUpdate();
+        DLLEXPORT D3D12_RAYTRACING_INSTANCE_DESC* nextInstanceDesc();
+        DLLEXPORT void endUpdate();
+        DLLEXPORT DX12Buffer* zResultBuffer() const;
+    };
+} // namespace Framework

+ 4 - 4
DX12Texture.cpp

@@ -1,8 +1,8 @@
 #include "DX12Texture.h"
 
-#include "Image.h"
 #include "d3dx12.h"
 #include "DX12CommandQueue.h"
+#include "Image.h"
 
 using namespace Framework;
 
@@ -96,14 +96,14 @@ bool DX12Texture::updateTextur()
             barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_DEST;
             barrier.Transition.Subresource = 0;
             barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
-            direct->getCommandList()->ResourceBarrier(1, &barrier);
+            direct->zCommandList()->ResourceBarrier(1, &barrier);
             shaderResource = 0;
         }
         D3D12_SUBRESOURCE_DATA textureData = {};
         textureData.pData = bild->getBuffer();
         textureData.RowPitch = bild->getWidth() * sizeof(int);
         textureData.SlicePitch = textureData.RowPitch * bild->getHeight();
-        UpdateSubresources(direct->getCommandList(),
+        UpdateSubresources(direct->zCommandList(),
             buffer,
             intermediate,
             0,
@@ -119,7 +119,7 @@ bool DX12Texture::updateTextur()
             | D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;
         barrier.Transition.Subresource = 0;
         barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
-        direct->getCommandList()->ResourceBarrier(1, &barrier);
+        direct->zCommandList()->ResourceBarrier(1, &barrier);
         shaderResource = 1;
     }
 #endif

+ 4 - 5
Dialog.cpp

@@ -1,13 +1,13 @@
 #include "Dialog.h"
 
 #include "AsynchronCall.h"
-#include "SelectionBox.h"
-#include "Screen.h"
-#include "Window.h"
 #include "Button.h"
-#include "RenderThread.h"
 #include "Font.h"
+#include "RenderThread.h"
+#include "Screen.h"
+#include "SelectionBox.h"
 #include "Text.h"
+#include "Window.h"
 
 using namespace Framework;
 
@@ -51,7 +51,6 @@ void* MultiplChoiceDialog::show(Font* zFont)
     f->setDisplayMode(1);
     Screen* b = new Screen2D(dynamic_cast<NativeWindow*>(f->getThis()));
     f->setScreen(dynamic_cast<Screen*>(b->getThis()));
-    b->update();
 
     RenderTh* r = new RenderTh();
     r->setScreen(dynamic_cast<Screen*>(b->getThis()));

+ 2 - 0
Framework.vcxproj

@@ -211,6 +211,7 @@ copy "x64\Release\Framework.dll" "..\..\Spiele Platform\SMP\Fertig\x64\framework
     <ClInclude Include="DX12BLAS.h" />
     <ClInclude Include="DX12BLASModel.h" />
     <ClInclude Include="DX12GraphicsApi.h" />
+    <ClInclude Include="DX12TLAS.h" />
     <ClInclude Include="DX9GraphicsApi.h" />
     <ClInclude Include="SelectionBox.h" />
     <ClInclude Include="Console.h" />
@@ -324,6 +325,7 @@ copy "x64\Release\Framework.dll" "..\..\Spiele Platform\SMP\Fertig\x64\framework
     <ClCompile Include="DX11Shader.cpp" />
     <ClCompile Include="DX12BLAS.cpp" />
     <ClCompile Include="DX12BLASModel.cpp" />
+    <ClCompile Include="DX12TLAS.cpp" />
     <ClCompile Include="SelectionBox.cpp" />
     <ClCompile Include="Console.cpp" />
     <ClCompile Include="DataValidator.cpp" />

+ 6 - 0
Framework.vcxproj.filters

@@ -418,6 +418,9 @@
     <ClInclude Include="DX12BLAS.h">
       <Filter>Framework\Graphics\DX\DX12</Filter>
     </ClInclude>
+    <ClInclude Include="DX12TLAS.h">
+      <Filter>Framework\Graphics\DX\DX12</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="Model3DCollection.h">
@@ -693,6 +696,9 @@
     <ClCompile Include="DX12BLAS.cpp">
       <Filter>Framework\Graphics\DX\DX12</Filter>
     </ClCompile>
+    <ClCompile Include="DX12TLAS.cpp">
+      <Filter>Framework\Graphics\DX\DX12</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <FxCompile Include="DX11VertexShader.hlsl">

+ 4 - 0
OperatingSystem.h

@@ -4,6 +4,10 @@
 #define _NOHEAP
 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
+#ifndef ROUND_UP
+#    define ROUND_UP_POWER_OF_2(v, powerOf2Alignment) \
+        (((v) + (powerOf2Alignment) - 1) & ~((powerOf2Alignment) - 1))
+#endif
 #ifdef _WIN32
 
 #    ifdef _DEBUG

+ 13 - 2
World3D.cpp

@@ -12,7 +12,8 @@ using namespace Framework;
 // Contents of the World3D class from World3D.h
 // Konstructor
 World3D::World3D()
-    : Model3DCollection()
+    : Model3DCollection(),
+      id(-1)
 {
     members = new RCArray<Model3D>();
     pointLightCount = 0;
@@ -328,4 +329,14 @@ DLLEXPORT void Framework::World3D::removePointLight(int index)
     }
     pointLightCount--;
     lock.unlockWrite();
-}
+}
+
+void Framework::World3D::setId(int id)
+{
+    this->id = id;
+}
+
+int Framework::World3D::getId() const
+{
+    return id;
+}

+ 7 - 0
World3D.h

@@ -24,6 +24,7 @@ namespace Framework
         int diffuseLightCount;
         PointLight* pointLights;
         int pointLightCount;
+        int id;
 
     private:
         RCArray<Model3D>* members;
@@ -96,5 +97,11 @@ namespace Framework
         //! removes a specific point light from the world
         //! \param index the index of the light
         DLLEXPORT void removePointLight(int index);
+        //! Sets the id of the world
+        //! this wil be called by the used graphics api when the world is
+        //! rendered to asociate specific gpu resources with this world instance
+        DLLEXPORT void setId(int id);
+        //! Returns the id of the world
+        DLLEXPORT int getId() const;
     };
 } // namespace Framework