Skip to content

Commit 0995067

Browse files
authored
Implement gfx inline ray tracing on D3D12. (shader-slang#1902)
* Update VS projects to 2019. * Empty commit to trigger build * Implement gfx inline ray tracing on D3D12.
1 parent 06c4926 commit 0995067

15 files changed

+707
-296
lines changed

examples/ray-tracing/main.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ void onMouseUp(platform::MouseEventArgs args) { isMouseDown = false; }
287287

288288
Slang::Result initialize()
289289
{
290-
initializeBase("Ray Tracing", 1024, 768, DeviceType::Vulkan);
290+
initializeBase("Ray Tracing", 1024, 768);
291291
gWindow->events.mouseMove = [this](const platform::MouseEventArgs& e) { onMouseMove(e); };
292292
gWindow->events.mouseUp = [this](const platform::MouseEventArgs& e) { onMouseUp(e); };
293293
gWindow->events.mouseDown = [this](const platform::MouseEventArgs& e) { onMouseDown(e); };
@@ -297,14 +297,14 @@ Slang::Result initialize()
297297
IBufferResource::Desc vertexBufferDesc;
298298
vertexBufferDesc.type = IResource::Type::Buffer;
299299
vertexBufferDesc.sizeInBytes = kVertexCount * sizeof(Vertex);
300-
vertexBufferDesc.defaultState = ResourceState::UnorderedAccess;
300+
vertexBufferDesc.defaultState = ResourceState::ShaderResource;
301301
gVertexBuffer = gDevice->createBufferResource(vertexBufferDesc, &kVertexData[0]);
302302
if(!gVertexBuffer) return SLANG_FAIL;
303303

304304
IBufferResource::Desc indexBufferDesc;
305305
indexBufferDesc.type = IResource::Type::Buffer;
306306
indexBufferDesc.sizeInBytes = kIndexCount * sizeof(int32_t);
307-
indexBufferDesc.defaultState = ResourceState::UnorderedAccess;
307+
indexBufferDesc.defaultState = ResourceState::ShaderResource;
308308
gIndexBuffer = gDevice->createBufferResource(indexBufferDesc, &kIndexData[0]);
309309
if (!gIndexBuffer)
310310
return SLANG_FAIL;
@@ -325,7 +325,7 @@ Slang::Result initialize()
325325
IBufferResource::Desc transformBufferDesc;
326326
transformBufferDesc.type = IResource::Type::Buffer;
327327
transformBufferDesc.sizeInBytes = sizeof(float) * 16;
328-
transformBufferDesc.defaultState = ResourceState::UnorderedAccess;
328+
transformBufferDesc.defaultState = ResourceState::ShaderResource;
329329
float transformData[12] = {
330330
1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f};
331331
gTransformBuffer = gDevice->createBufferResource(transformBufferDesc, &transformData);
@@ -439,7 +439,7 @@ Slang::Result initialize()
439439
instanceBufferDesc.type = IResource::Type::Buffer;
440440
instanceBufferDesc.sizeInBytes =
441441
instanceDescs.getCount() * sizeof(IAccelerationStructure::InstanceDesc);
442-
instanceBufferDesc.defaultState = ResourceState::UnorderedAccess;
442+
instanceBufferDesc.defaultState = ResourceState::ShaderResource;
443443
gInstanceBuffer = gDevice->createBufferResource(instanceBufferDesc, instanceDescs.getBuffer());
444444
if (!gInstanceBuffer)
445445
return SLANG_FAIL;

slang-gfx.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ class IShaderProgram: public ISlangUnknown
139139
\
140140
x(RGBA_Unorm_UInt8, sizeof(uint32_t)) \
141141
x(BGRA_Unorm_UInt8, sizeof(uint32_t)) \
142+
x(RGBA_Snorm_UInt16, sizeof(uint32_t)*2) \
143+
x(RG_Snorm_UInt16, sizeof(uint32_t)) \
142144
\
143145
x(R_UInt16, sizeof(uint16_t)) \
144146
x(R_UInt32, sizeof(uint32_t)) \
@@ -164,6 +166,9 @@ enum class Format
164166
RGBA_Unorm_UInt8,
165167
BGRA_Unorm_UInt8,
166168

169+
RGBA_Snorm_UInt16,
170+
RG_Snorm_UInt16,
171+
167172
R_UInt16,
168173
R_UInt32,
169174

@@ -497,6 +502,7 @@ class IResourceView : public ISlangUnknown
497502
// Fields for `RenderTarget` and `DepthStencil` views.
498503
RenderTargetDesc renderTarget;
499504
};
505+
virtual SLANG_NO_THROW Desc* SLANG_MCALL getViewDesc() = 0;
500506
};
501507
#define SLANG_UUID_IResourceView \
502508
{ \
@@ -1153,7 +1159,7 @@ class IRayTracingCommandEncoder : public ICommandEncoder
11531159
serializeAccelerationStructure(DeviceAddress dest, IAccelerationStructure* source) = 0;
11541160
virtual SLANG_NO_THROW void SLANG_MCALL
11551161
deserializeAccelerationStructure(IAccelerationStructure* dest, DeviceAddress source) = 0;
1156-
virtual SLANG_NO_THROW void memoryBarrier(
1162+
virtual SLANG_NO_THROW void SLANG_MCALL memoryBarrier(
11571163
int count,
11581164
IAccelerationStructure* const* structures,
11591165
AccessFlag::Enum sourceAccess,

tools/copy-hlsl-libs.bat

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ set PLATFORM=%~3
77

88
robocopy "../../external/slang-binaries/bin/%PLATFORM%" "%TARGET_DIR%" d3dcompiler_47.dll /r:0 >nul
99

10-
robocopy "%SOURCE_DIR%" "%TARGET_DIR%" dxcompiler.dll /r:0 >nul
11-
robocopy "%SOURCE_DIR%" "%TARGET_DIR%" dxil.dll /r:0 >nul
10+
robocopy "%SOURCE_DIR%" "%TARGET_DIR%" dxcompiler.dll /xn /r:0 >nul
11+
robocopy "%SOURCE_DIR%" "%TARGET_DIR%" dxil.dll /xn /r:0 >nul
1212

1313
exit /b 0

tools/gfx/cpu/render-cpu.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -379,12 +379,12 @@ class CPUResourceView : public ResourceViewBase
379379
protected:
380380
CPUResourceView(Kind kind, Desc const& desc)
381381
: m_kind(kind)
382-
, m_desc(desc)
383-
{}
382+
{
383+
m_desc = desc;
384+
}
384385

385386
private:
386387
Kind m_kind;
387-
Desc m_desc;
388388
};
389389

390390
class CPUBufferView : public CPUResourceView

tools/gfx/cuda/render-cuda.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ class TextureCUDAResource : public TextureResource
239239
class CUDAResourceView : public ResourceViewBase
240240
{
241241
public:
242-
Desc desc;
243242
RefPtr<MemoryCUDAResource> memoryResource = nullptr;
244243
RefPtr<TextureCUDAResource> textureResource = nullptr;
245244
void* proxyBuffer = nullptr;
@@ -447,7 +446,7 @@ class CUDAShaderObjectData
447446
viewDesc.type = IResourceView::Type::UnorderedAccess;
448447
m_bufferView = new CUDAResourceView();
449448
m_bufferView->proxyBuffer = m_cpuBuffer.getBuffer();
450-
m_bufferView->desc = viewDesc;
449+
m_bufferView->m_desc = viewDesc;
451450
}
452451
return SLANG_OK;
453452
}
@@ -466,7 +465,7 @@ class CUDAShaderObjectData
466465
viewDesc.type = IResourceView::Type::UnorderedAccess;
467466
m_bufferView = new CUDAResourceView();
468467
m_bufferView->memoryResource = m_bufferResource;
469-
m_bufferView->desc = viewDesc;
468+
m_bufferView->m_desc = viewDesc;
470469
}
471470
auto oldSize = m_bufferResource->getDesc()->sizeInBytes;
472471
if ((size_t)count != oldSize)
@@ -571,7 +570,7 @@ class CUDAShaderObject
571570

572571
if (cudaView->textureResource)
573572
{
574-
if (cudaView->desc.type == IResourceView::Type::UnorderedAccess)
573+
if (cudaView->m_desc.type == IResourceView::Type::UnorderedAccess)
575574
{
576575
auto handle = cudaView->textureResource->m_cudaSurfObj;
577576
setData(offset, &handle, sizeof(uint64_t));
@@ -1727,7 +1726,7 @@ class CUDADevice : public RendererBase
17271726
ITextureResource* texture, IResourceView::Desc const& desc, IResourceView** outView) override
17281727
{
17291728
RefPtr<CUDAResourceView> view = new CUDAResourceView();
1730-
view->desc = desc;
1729+
view->m_desc = desc;
17311730
view->textureResource = dynamic_cast<TextureCUDAResource*>(texture);
17321731
returnComPtr(outView, view);
17331732
return SLANG_OK;
@@ -1737,7 +1736,7 @@ class CUDADevice : public RendererBase
17371736
IBufferResource* buffer, IResourceView::Desc const& desc, IResourceView** outView) override
17381737
{
17391738
RefPtr<CUDAResourceView> view = new CUDAResourceView();
1740-
view->desc = desc;
1739+
view->m_desc = desc;
17411740
view->memoryResource = dynamic_cast<MemoryCUDAResource*>(buffer);
17421741
returnComPtr(outView, view);
17431742
return SLANG_OK;

tools/gfx/d3d/d3d-util.cpp

+81-12
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ D3D12_DEPTH_STENCILOP_DESC D3DUtil::translateStencilOpDesc(DepthStencilOpDesc de
114114
case Format::R_Float32: return DXGI_FORMAT_R32_FLOAT;
115115
case Format::RGBA_Unorm_UInt8: return DXGI_FORMAT_R8G8B8A8_UNORM;
116116
case Format::BGRA_Unorm_UInt8: return DXGI_FORMAT_B8G8R8A8_UNORM;
117+
case Format::RGBA_Snorm_UInt16: return DXGI_FORMAT_R16G16B16A16_SNORM;
118+
case Format::RG_Snorm_UInt16: return DXGI_FORMAT_R16G16_SNORM;
117119

118120
case Format::RGBA_Float16: return DXGI_FORMAT_R16G16B16A16_FLOAT;
119121
case Format::RG_Float16: return DXGI_FORMAT_R16G16_FLOAT;
@@ -133,29 +135,33 @@ D3D12_RESOURCE_STATES D3DUtil::translateResourceState(ResourceState state)
133135
{
134136
switch (state)
135137
{
136-
case gfx::ResourceState::Undefined:
138+
case ResourceState::Undefined:
137139
return D3D12_RESOURCE_STATE_COMMON;
138-
case gfx::ResourceState::ShaderResource:
140+
case ResourceState::ShaderResource:
139141
return D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE |
140142
D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;
141-
case gfx::ResourceState::UnorderedAccess:
143+
case ResourceState::UnorderedAccess:
142144
return D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
143-
case gfx::ResourceState::RenderTarget:
145+
case ResourceState::RenderTarget:
144146
return D3D12_RESOURCE_STATE_RENDER_TARGET;
145-
case gfx::ResourceState::DepthRead:
147+
case ResourceState::DepthRead:
146148
return D3D12_RESOURCE_STATE_DEPTH_READ;
147-
case gfx::ResourceState::DepthWrite:
149+
case ResourceState::DepthWrite:
148150
return D3D12_RESOURCE_STATE_DEPTH_WRITE;
149-
case gfx::ResourceState::Present:
151+
case ResourceState::Present:
150152
return D3D12_RESOURCE_STATE_PRESENT;
151-
case gfx::ResourceState::CopySource:
153+
case ResourceState::CopySource:
152154
return D3D12_RESOURCE_STATE_COPY_SOURCE;
153-
case gfx::ResourceState::CopyDestination:
155+
case ResourceState::CopyDestination:
154156
return D3D12_RESOURCE_STATE_COPY_DEST;
155-
case gfx::ResourceState::ResolveSource:
157+
case ResourceState::ResolveSource:
156158
return D3D12_RESOURCE_STATE_RESOLVE_SOURCE;
157-
case gfx::ResourceState::ResolveDestination:
159+
case ResourceState::ResolveDestination:
158160
return D3D12_RESOURCE_STATE_RESOLVE_DEST;
161+
#if SLANG_GFX_HAS_DXR_SUPPORT
162+
case ResourceState::AccelerationStructure:
163+
return D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE;
164+
#endif
159165
default:
160166
return D3D12_RESOURCE_STATE_COMMON;
161167
}
@@ -583,4 +589,67 @@ bool D3DUtil::isUAVBinding(slang::BindingType bindingType)
583589
return SLANG_OK;
584590
}
585591

586-
} // renderer_test
592+
#if SLANG_GFX_HAS_DXR_SUPPORT
593+
Result D3DAccelerationStructureInputsBuilder::build(
594+
const IAccelerationStructure::BuildInputs& buildInputs,
595+
IDebugCallback* callback)
596+
{
597+
if (buildInputs.geometryDescs)
598+
{
599+
geomDescs.setCount(buildInputs.descCount);
600+
for (Index i = 0; i < geomDescs.getCount(); i++)
601+
{
602+
auto& inputGeomDesc = buildInputs.geometryDescs[i];
603+
geomDescs[i].Flags = translateGeometryFlags(inputGeomDesc.flags);
604+
switch (inputGeomDesc.type)
605+
{
606+
case IAccelerationStructure::GeometryType::Triangles:
607+
geomDescs[i].Type = D3D12_RAYTRACING_GEOMETRY_TYPE_TRIANGLES;
608+
geomDescs[i].Triangles.IndexBuffer = inputGeomDesc.content.triangles.indexData;
609+
geomDescs[i].Triangles.IndexCount = inputGeomDesc.content.triangles.indexCount;
610+
geomDescs[i].Triangles.IndexFormat =
611+
D3DUtil::getMapFormat(inputGeomDesc.content.triangles.indexFormat);
612+
geomDescs[i].Triangles.Transform3x4 = inputGeomDesc.content.triangles.transform3x4;
613+
geomDescs[i].Triangles.VertexBuffer.StartAddress =
614+
inputGeomDesc.content.triangles.vertexData;
615+
geomDescs[i].Triangles.VertexBuffer.StrideInBytes =
616+
inputGeomDesc.content.triangles.vertexStride;
617+
geomDescs[i].Triangles.VertexCount = inputGeomDesc.content.triangles.vertexCount;
618+
geomDescs[i].Triangles.VertexFormat =
619+
D3DUtil::getMapFormat(inputGeomDesc.content.triangles.vertexFormat);
620+
break;
621+
case IAccelerationStructure::GeometryType::ProcedurePrimitives:
622+
geomDescs[i].Type = D3D12_RAYTRACING_GEOMETRY_TYPE_PROCEDURAL_PRIMITIVE_AABBS;
623+
geomDescs[i].AABBs.AABBCount = inputGeomDesc.content.proceduralAABBs.count;
624+
geomDescs[i].AABBs.AABBs.StartAddress = inputGeomDesc.content.proceduralAABBs.data;
625+
geomDescs[i].AABBs.AABBs.StrideInBytes =
626+
inputGeomDesc.content.proceduralAABBs.stride;
627+
break;
628+
default:
629+
callback->handleMessage(
630+
DebugMessageType::Error,
631+
DebugMessageSource::Layer,
632+
"invalid value of IAccelerationStructure::GeometryType.");
633+
return SLANG_E_INVALID_ARG;
634+
}
635+
}
636+
}
637+
desc.DescsLayout = D3D12_ELEMENTS_LAYOUT_ARRAY;
638+
desc.NumDescs = buildInputs.descCount;
639+
switch (buildInputs.kind)
640+
{
641+
case IAccelerationStructure::Kind::TopLevel:
642+
desc.Type = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL;
643+
desc.InstanceDescs = buildInputs.instanceDescs;
644+
break;
645+
case IAccelerationStructure::Kind::BottomLevel:
646+
desc.Type = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL;
647+
desc.pGeometryDescs = geomDescs.getBuffer();
648+
break;
649+
}
650+
desc.Flags = (D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAGS)buildInputs.flags;
651+
return SLANG_OK;
652+
}
653+
#endif
654+
655+
} // namespace gfx

tools/gfx/d3d/d3d-util.h

+27
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@
1717
#include <dxgi.h>
1818
#include <d3d12.h>
1919

20+
#if defined(__ID3D12Device5_FWD_DEFINED__) && defined(__ID3D12GraphicsCommandList4_FWD_DEFINED__)
21+
# define SLANG_GFX_HAS_DXR_SUPPORT 1
22+
#else
23+
# define SLANG_GFX_HAS_DXR_SUPPORT 0
24+
typedef ISlangUnknown ID3D12Device5;
25+
typedef ISlangUnknown ID3D12GraphicsCommandList4;
26+
27+
#endif
28+
2029
namespace gfx {
2130

2231
class D3DUtil
@@ -87,4 +96,22 @@ class D3DUtil
8796

8897
};
8998

99+
#if SLANG_GFX_HAS_DXR_SUPPORT
100+
struct D3DAccelerationStructureInputsBuilder
101+
{
102+
D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS desc = {};
103+
D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO prebuildInfo = {};
104+
Slang::List<D3D12_RAYTRACING_GEOMETRY_DESC> geomDescs;
105+
Slang::Result build(
106+
const IAccelerationStructure::BuildInputs& buildInputs,
107+
IDebugCallback* callback);
108+
109+
private:
110+
D3D12_RAYTRACING_GEOMETRY_FLAGS translateGeometryFlags(
111+
IAccelerationStructure::GeometryFlags::Enum flags)
112+
{
113+
return (D3D12_RAYTRACING_GEOMETRY_FLAGS)flags;
114+
}
115+
};
116+
#endif
90117
} // renderer_test

tools/gfx/d3d11/render-d3d11.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -2863,6 +2863,8 @@ Result D3D11Device::createTextureView(ITextureResource* texture, IResourceView::
28632863
RefPtr<RenderTargetViewImpl> viewImpl = new RenderTargetViewImpl();
28642864
viewImpl->m_type = ResourceViewImpl::Type::RTV;
28652865
viewImpl->m_rtv = rtv;
2866+
viewImpl->m_desc = desc;
2867+
28662868
memcpy(
28672869
viewImpl->m_clearValue,
28682870
&resourceImpl->getDesc()->optimalClearValue.color,
@@ -2881,6 +2883,8 @@ Result D3D11Device::createTextureView(ITextureResource* texture, IResourceView::
28812883
viewImpl->m_type = ResourceViewImpl::Type::DSV;
28822884
viewImpl->m_dsv = dsv;
28832885
viewImpl->m_clearValue = resourceImpl->getDesc()->optimalClearValue.depthStencil;
2886+
viewImpl->m_desc = desc;
2887+
28842888
returnComPtr(outView, viewImpl);
28852889
return SLANG_OK;
28862890
}
@@ -2894,6 +2898,8 @@ Result D3D11Device::createTextureView(ITextureResource* texture, IResourceView::
28942898
RefPtr<UnorderedAccessViewImpl> viewImpl = new UnorderedAccessViewImpl();
28952899
viewImpl->m_type = ResourceViewImpl::Type::UAV;
28962900
viewImpl->m_uav = uav;
2901+
viewImpl->m_desc = desc;
2902+
28972903
returnComPtr(outView, viewImpl);
28982904
return SLANG_OK;
28992905
}
@@ -2907,6 +2913,8 @@ Result D3D11Device::createTextureView(ITextureResource* texture, IResourceView::
29072913
RefPtr<ShaderResourceViewImpl> viewImpl = new ShaderResourceViewImpl();
29082914
viewImpl->m_type = ResourceViewImpl::Type::SRV;
29092915
viewImpl->m_srv = srv;
2916+
viewImpl->m_desc = desc;
2917+
29102918
returnComPtr(outView, viewImpl);
29112919
return SLANG_OK;
29122920
}
@@ -2952,6 +2960,8 @@ Result D3D11Device::createBufferView(IBufferResource* buffer, IResourceView::Des
29522960
RefPtr<UnorderedAccessViewImpl> viewImpl = new UnorderedAccessViewImpl();
29532961
viewImpl->m_type = ResourceViewImpl::Type::UAV;
29542962
viewImpl->m_uav = uav;
2963+
viewImpl->m_desc = desc;
2964+
29552965
returnComPtr(outView, viewImpl);
29562966
return SLANG_OK;
29572967
}
@@ -2997,6 +3007,7 @@ Result D3D11Device::createBufferView(IBufferResource* buffer, IResourceView::Des
29973007
RefPtr<ShaderResourceViewImpl> viewImpl = new ShaderResourceViewImpl();
29983008
viewImpl->m_type = ResourceViewImpl::Type::SRV;
29993009
viewImpl->m_srv = srv;
3010+
viewImpl->m_desc = desc;
30003011
returnComPtr(outView, viewImpl);
30013012
return SLANG_OK;
30023013
}

0 commit comments

Comments
 (0)