Skip to content

Commit

Permalink
Merge pull request microsoft#403 from Microsoft/develop-dxr
Browse files Browse the repository at this point in the history
Adding initial Fallback Layer support for cheap AABB refitting when update build flag is used
Triangle intersection fixes for close geometry
Fallback layer support for geometry flags
Small sample fixes
  • Loading branch information
wallisc authored Jun 15, 2018
2 parents ea065cc + 2dcfc70 commit 8f68bf0
Show file tree
Hide file tree
Showing 37 changed files with 1,463 additions and 569 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ void main( uint3 DTid : SV_DispatchThreadID )
uint NumberOfAABBs = NumberOfInternalNodes + Constants.TotalPrimitiveCount;

uint3 indicies = GetIndex(localTriangleIndex);

uint globalTriangleIndex = localTriangleIndex + Constants.PrimitiveOffset;

Triangle tri;
tri.v0 = GetVertex(elementBuffer, indicies[0], Constants.ElementBufferStride);
Expand All @@ -120,6 +118,9 @@ void main( uint3 DTid : SV_DispatchThreadID )
tri.v2 = TransformVertex(tri.v2, transform);
}

PrimitiveBuffer[globalTriangleIndex] = CreateTrianglePrimitive(tri);
StorePrimitiveMetadata(globalTriangleIndex, localTriangleIndex);
uint globalTriangleIndex = localTriangleIndex + Constants.PrimitiveOffset;
uint outputIndex = GetOutputIndex(globalTriangleIndex);

PrimitiveBuffer[outputIndex] = CreateTrianglePrimitive(tri);
StorePrimitiveMetadata(outputIndex, localTriangleIndex);
}
32 changes: 32 additions & 0 deletions Libraries/D3D12RaytracingFallback/src/ComObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,35 @@ public: \
} \
private: \
UINT m_ReferenceCount = 1;

#define COM_IMPLEMENTATION_WITH_QUERYINTERFACE(pParentObject) \
public: \
virtual HRESULT STDMETHODCALLTYPE QueryInterface( \
REFIID riid, \
void **ppvObject) \
{ \
if (riid == __uuidof(*pParentObject)) \
{ \
pParentObject->AddRef(); \
*ppvObject = pParentObject; \
return S_OK; \
} \
return pParentObject->QueryInterface(riid, ppvObject); \
} \
\
virtual ULONG STDMETHODCALLTYPE AddRef(void) \
{ \
return InterlockedIncrement(&m_ReferenceCount); \
} \
\
virtual ULONG STDMETHODCALLTYPE Release(void) \
{ \
ULONG currentRefCount = InterlockedDecrement(&m_ReferenceCount); \
if (currentRefCount == 0) \
{ \
delete this; \
} \
return currentRefCount; \
} \
private: \
UINT m_ReferenceCount = 1;
59 changes: 51 additions & 8 deletions Libraries/D3D12RaytracingFallback/src/ComputeAABBs.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,47 @@ uint GetLeafCount(uint boxIndex)
uint4 GetNodeData(uint boxIndex, out uint2 flags)
{
uint nodeAddress = GetBoxAddress(offsetToBoxes, boxIndex);
uint4 data = outputBVH.Load4(nodeAddress);
flags = data.wz;
return data;
uint4 data1 = outputBVH.Load4(nodeAddress);
uint4 data2 = outputBVH.Load4(nodeAddress + 16);
flags = float2(data1.w, data2.w);
return data1;
}

uint2 GetNodeFlags(uint boxIndex)
{
uint2 flags;
GetNodeData(boxIndex, flags);
return flags;
}

uint GetLeftChildIndex(uint boxIndex)
{
if (ShouldPerformUpdate)
{
return GetLeftNodeIndex(GetNodeFlags(boxIndex));
}

return hierarchyBuffer[boxIndex].LeftChildIndex;
}

uint GetRightChildIndex(uint boxIndex)
{
if (ShouldPerformUpdate)
{
return GetRightNodeIndex(GetNodeFlags(boxIndex));
}

return hierarchyBuffer[boxIndex].RightChildIndex;
}

uint GetParentIndex(uint boxIndex)
{
if (ShouldPerformUpdate)
{
return aabbParentBuffer[boxIndex];
}

return hierarchyBuffer[boxIndex].ParentIndex;
}

[numthreads(THREAD_GROUP_1D_WIDTH, 1, 1)]
Expand Down Expand Up @@ -63,8 +101,8 @@ void main(uint3 DTid : SV_DispatchThreadID)
}
else
{
uint leftNodeIndex = hierarchyBuffer[nodeIndex].LeftChildIndex;
uint rightNodeIndex = hierarchyBuffer[nodeIndex].RightChildIndex;
uint leftNodeIndex = GetLeftChildIndex(nodeIndex);
uint rightNodeIndex = GetRightChildIndex(nodeIndex);
if (swapChildIndices)
{
uint temp = leftNodeIndex;
Expand Down Expand Up @@ -104,21 +142,26 @@ void main(uint3 DTid : SV_DispatchThreadID)
break;
}

uint parentNodeIndex = hierarchyBuffer[nodeIndex].ParentIndex;
uint parentNodeIndex = GetParentIndex(nodeIndex);
uint trianglesFromOtherChild;
// If this counter was already incremented, that means both children for the parent
// node have computed their AABB, and the parent is ready to be processed
childNodesProcessedCounter.InterlockedAdd(parentNodeIndex * SizeOfUINT32, numTriangles, trianglesFromOtherChild);
if (trianglesFromOtherChild != 0)
{

// Prioritize having the smaller nodes on the left
// TODO: Investigate better heuristics for node ordering
bool IsLeft = hierarchyBuffer[parentNodeIndex].LeftChildIndex == nodeIndex;
bool IsLeft = GetLeftChildIndex(parentNodeIndex) == nodeIndex;
bool IsOtherSideSmaller = numTriangles > trianglesFromOtherChild;
swapChildIndices = IsLeft ? IsOtherSideSmaller : !IsOtherSideSmaller;
nodeIndex = parentNodeIndex;
numTriangles += trianglesFromOtherChild;

if (ShouldPrepareUpdate)
{
aabbParentBuffer[GetLeftChildIndex(nodeIndex)] = nodeIndex;
aabbParentBuffer[GetRightChildIndex(nodeIndex)] = nodeIndex;
}
}
else
{
Expand Down
12 changes: 9 additions & 3 deletions Libraries/D3D12RaytracingFallback/src/ConstructAABBBindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,33 @@
struct InputConstants
{
uint NumberOfElements;
uint UpdateFlags;
};

// UAVs
#define OutputBVHRegister 0
#define ScratchBufferRegister 1
#define ChildNodesProcessedBufferRegister 2
#define MortonCodesBufferRegister 3
#define HierarchyBufferRegister 4
#define HierarchyBufferRegister 3
#define AABBParentBufferRegister 4

#define GlobalDescriptorHeapRegister 0
#define GlobalDescriptorHeapRegisterSpace 1

// CBVs
#define InputConstantsRegister 0

#define PREPARE_UPDATE_FLAG 0x1
#define PERFORM_UPDATE_FLAG 0x2
#define ShouldPrepareUpdate Constants.UpdateFlags & PREPARE_UPDATE_FLAG
#define ShouldPerformUpdate Constants.UpdateFlags & PERFORM_UPDATE_FLAG

#ifdef HLSL
globallycoherent RWByteAddressBuffer outputBVH : UAV_REGISTER(OutputBVHRegister);
RWByteAddressBuffer scratchMemory : UAV_REGISTER(ScratchBufferRegister);
RWByteAddressBuffer childNodesProcessedCounter : UAV_REGISTER(ChildNodesProcessedBufferRegister);
RWStructuredBuffer<uint> mortonCodes : UAV_REGISTER(MortonCodesBufferRegister);
RWStructuredBuffer<HierarchyNode> hierarchyBuffer : UAV_REGISTER(HierarchyBufferRegister);
RWStructuredBuffer<uint> aabbParentBuffer : UAV_REGISTER(AABBParentBufferRegister);
RWByteAddressBuffer DescriptorHeapBufferTable[] : UAV_REGISTER_SPACE(GlobalDescriptorHeapRegister, GlobalDescriptorHeapRegisterSpace);

cbuffer ConstructHierarchyConstants : CONSTANT_REGISTER(InputConstantsRegister)
Expand Down
16 changes: 12 additions & 4 deletions Libraries/D3D12RaytracingFallback/src/ConstructAABBPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ namespace FallbackLayer
rootParameters[OutputBVHRootUAVParam].InitAsUnorderedAccessView(OutputBVHRegister);
rootParameters[ScratchUAVParam].InitAsUnorderedAccessView(ScratchBufferRegister);
rootParameters[HierarchyUAVParam].InitAsUnorderedAccessView(HierarchyBufferRegister);
rootParameters[AABBParentBufferParam].InitAsUnorderedAccessView(AABBParentBufferRegister);
rootParameters[ChildNodesProcessedCountBufferParam].InitAsUnorderedAccessView(ChildNodesProcessedBufferRegister);
rootParameters[MortonCodesBufferParam].InitAsUnorderedAccessView(MortonCodesBufferRegister);
rootParameters[InputRootConstants].InitAsConstants(SizeOfInUint32(InputConstants), InputConstantsRegister);
rootParameters[GlobalDescriptorHeap].InitAsDescriptorTable(1, &globalDescriptorHeapRange);

Expand All @@ -44,15 +44,19 @@ namespace FallbackLayer
D3D12_GPU_VIRTUAL_ADDRESS outputVH,
D3D12_GPU_VIRTUAL_ADDRESS scratchBuffer,
D3D12_GPU_VIRTUAL_ADDRESS childNodesProcessedCountBuffer,
D3D12_GPU_VIRTUAL_ADDRESS mortonCodeBuffer,
D3D12_GPU_VIRTUAL_ADDRESS hierarchyBuffer,
D3D12_GPU_VIRTUAL_ADDRESS outputAABBParentBuffer,
D3D12_GPU_DESCRIPTOR_HANDLE globalDescriptorHeap,
const bool prepareUpdate,
const bool performUpdate,
UINT numElements)
{
bool isEmptyAccelerationStructure = numElements == 0;
Level level = (sceneType == SceneType::Triangles) ? Level::Bottom : Level::Top;

InputConstants constants = { numElements };
InputConstants constants = {};
constants.NumberOfElements = numElements;
constants.UpdateFlags = ((UINT) prepareUpdate) | (performUpdate << 1);

pCommandList->SetComputeRootSignature(m_pRootSignature);
pCommandList->SetComputeRoot32BitConstants(InputRootConstants, SizeOfInUint32(InputConstants), &constants, 0);
Expand All @@ -61,10 +65,14 @@ namespace FallbackLayer
{
pCommandList->SetComputeRootUnorderedAccessView(ScratchUAVParam, scratchBuffer);
pCommandList->SetComputeRootUnorderedAccessView(ChildNodesProcessedCountBufferParam, childNodesProcessedCountBuffer);
pCommandList->SetComputeRootUnorderedAccessView(MortonCodesBufferParam, mortonCodeBuffer);
pCommandList->SetComputeRootUnorderedAccessView(HierarchyUAVParam, hierarchyBuffer);
}

if (prepareUpdate || performUpdate)
{
pCommandList->SetComputeRootUnorderedAccessView(AABBParentBufferParam, outputAABBParentBuffer);
}

if (level == Top)
{
pCommandList->SetComputeRootDescriptorTable(GlobalDescriptorHeap, globalDescriptorHeap);
Expand Down
6 changes: 4 additions & 2 deletions Libraries/D3D12RaytracingFallback/src/ConstructAABBPass.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@ namespace FallbackLayer
D3D12_GPU_VIRTUAL_ADDRESS outputVH,
D3D12_GPU_VIRTUAL_ADDRESS scratchBuffer,
D3D12_GPU_VIRTUAL_ADDRESS childNodesProcessedCountBuffer,
D3D12_GPU_VIRTUAL_ADDRESS mortonCodeBuffer,
D3D12_GPU_VIRTUAL_ADDRESS hierarchyBuffer,
D3D12_GPU_VIRTUAL_ADDRESS outputAABBParentBuffer,
D3D12_GPU_DESCRIPTOR_HANDLE globalDescriptorHeap,
const bool prepareUpdate,
const bool performUpdate,
UINT numElements);
private:
enum RootParameterSlot
{
OutputBVHRootUAVParam = 0,
ScratchUAVParam,
HierarchyUAVParam,
AABBParentBufferParam,
ChildNodesProcessedCountBufferParam,
MortonCodesBufferParam,
InputRootConstants,
GlobalDescriptorHeap,
NumRootParameters,
Expand Down
1 change: 1 addition & 0 deletions Libraries/D3D12RaytracingFallback/src/CpuBVH2Builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,7 @@ namespace FallbackLayer
PrimitiveMetaData metadata;
metadata.GeometryContributionToHitGroupIndex = i;
metadata.PrimitiveIndex = triangleIndex;
metadata.GeometryFlags = geometry.Flags;
primitiveMetaData[triangleIndex] = metadata;

// Next triangle
Expand Down
2 changes: 1 addition & 1 deletion Libraries/D3D12RaytracingFallback/src/FallbackLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ namespace FallbackLayer
{
if (range.RangeType != D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER)
{
range.Flags = D3D12_DESCRIPTOR_RANGE_FLAG_DATA_VOLATILE;
range.Flags = D3D12_DESCRIPTOR_RANGE_FLAG_DATA_VOLATILE | D3D12_DESCRIPTOR_RANGE_FLAG_DESCRIPTORS_VOLATILE;
}
}

Expand Down
4 changes: 2 additions & 2 deletions Libraries/D3D12RaytracingFallback/src/FallbackLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ namespace FallbackLayer
ID3D12DescriptorHeap *m_pBoundDescriptorHeaps[DescriptorHeapType::NumTypes] = {};
ATL::CComPtr<ID3D12GraphicsCommandList> m_pCommandList;
RaytracingDevice &m_device;
COM_IMPLEMENTATION();
COM_IMPLEMENTATION_WITH_QUERYINTERFACE(m_pCommandList.p);
};

class RaytracingStateObject : public ID3D12RaytracingFallbackStateObject
Expand Down Expand Up @@ -190,7 +190,7 @@ namespace FallbackLayer
RaytracingProgramFactory m_RaytracingProgramFactory;
DWORD m_flags;

COM_IMPLEMENTATION();
COM_IMPLEMENTATION_WITH_QUERYINTERFACE(m_pDevice.p)

#if ENABLE_ACCELERATION_STRUCTURE_VISUALIZATION
public:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,12 @@
<AdditionalOptions Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> -T lib_6_1 -HV 2017 -O4 %(AdditionalOptions)</AdditionalOptions>
<AdditionalOptions Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> -T lib_6_1 -HV 2017 -O4 %(AdditionalOptions)</AdditionalOptions>
<AdditionalOptions Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> -T lib_6_1 -HV 2017 -O4 %(AdditionalOptions)</AdditionalOptions>
<VariableName Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">g_p%(Filename)</VariableName>
<VariableName Condition="'$(Configuration)|$(Platform)'=='Release|x64'">g_p%(Filename)</VariableName>
<HeaderFileOutput Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(SolutionDir)..\Build_VS15\$(Platform)\$(Configuration)\Output\$(ProjectName)\CompiledShaders\%(Filename).h</HeaderFileOutput>
<HeaderFileOutput Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(SolutionDir)..\Build_VS15\$(Platform)\$(Configuration)\Output\$(ProjectName)\CompiledShaders\%(Filename).h</HeaderFileOutput>
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(SolutionDir)..\Build_VS15\$(Platform)\$(Configuration)\Output\$(ProjectName)\Shaders\%(Filename).cso</ObjectFileOutput>
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(SolutionDir)..\Build_VS15\$(Platform)\$(Configuration)\Output\$(ProjectName)\Shaders\%(Filename).cso</ObjectFileOutput>
</FxCompile>
<FxCompile Include="SimpleRayTracing.hlsl">
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,6 @@
<FxCompile Include="ReadDataTexture1DArray.hlsl" />
<FxCompile Include="ReadDataTexture2D.hlsl" />
<FxCompile Include="ReadDataTexture2DArray.hlsl" />
<FxCompile Include="ReadDataRWBuffer.hlsl" />
<FxCompile Include="ReadDataRWByteAddressBuffer.hlsl" />
<FxCompile Include="ReadDataRWTexture1D.hlsl" />
<FxCompile Include="ReadDataRWTexture1DArray.hlsl" />
<FxCompile Include="ReadDataRWTexture2D.hlsl" />
<FxCompile Include="ReadDataRWTexture2DArray.hlsl" />
<FxCompile Include="ReadDataRWTexture3D.hlsl" />
<FxCompile Include="ReadDataTexture1D.hlsl" />
<FxCompile Include="ReadDataTexture1DArray.hlsl" />
<FxCompile Include="ReadDataTexture2D.hlsl" />
<FxCompile Include="ReadDataTexture2DArray.hlsl" />
<FxCompile Include="ReadData*.hlsl">
<Filter>Shaders</Filter>
</FxCompile>
Expand Down Expand Up @@ -282,6 +271,17 @@
<FxCompile Include="ReadData*.hlsl" />
<FxCompile Include="ReadData*.hlsl" />
<FxCompile Include="ReadData*.hlsl" />
<FxCompile Include="ReadData*.hlsl" />
<FxCompile Include="ReadData*.hlsl" />
<FxCompile Include="ReadData*.hlsl" />
<FxCompile Include="ReadData*.hlsl" />
<FxCompile Include="ReadData*.hlsl" />
<FxCompile Include="ReadData*.hlsl" />
<FxCompile Include="ReadData*.hlsl" />
<FxCompile Include="ReadData*.hlsl" />
<FxCompile Include="ReadData*.hlsl" />
<FxCompile Include="ReadData*.hlsl" />
<FxCompile Include="ReadData*.hlsl" />
</ItemGroup>
<ItemGroup>
<None Include="SharedReadData.hlsli">
Expand Down
Loading

0 comments on commit 8f68bf0

Please sign in to comment.