1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
// d3d12-resource-views.cpp
#include "d3d12-resource-views.h"
#include "d3d12-device.h"
namespace gfx
{
namespace d3d12
{
using namespace Slang;
ResourceViewInternalImpl::~ResourceViewInternalImpl()
{
if (m_descriptor.cpuHandle.ptr)
m_allocator->free(m_descriptor);
for (auto desc : m_mapBufferStrideToDescriptor)
{
m_allocator->free(desc.second);
}
}
SlangResult createD3D12BufferDescriptor(
BufferResourceImpl* buffer,
BufferResourceImpl* counterBuffer,
IResourceView::Desc const& desc,
uint32_t bufferStride,
DeviceImpl* device,
D3D12GeneralExpandingDescriptorHeap* descriptorHeap,
D3D12Descriptor* outDescriptor)
{
auto resourceImpl = (BufferResourceImpl*)buffer;
auto resourceDesc = *resourceImpl->getDesc();
const auto counterResourceImpl = static_cast<BufferResourceImpl*>(counterBuffer);
uint64_t offset = desc.bufferRange.offset;
uint64_t size = desc.bufferRange.size == 0 ? buffer->getDesc()->sizeInBytes - offset
: desc.bufferRange.size;
switch (desc.type)
{
default:
return SLANG_FAIL;
case IResourceView::Type::UnorderedAccess:
{
D3D12_UNORDERED_ACCESS_VIEW_DESC uavDesc = {};
uavDesc.ViewDimension = D3D12_UAV_DIMENSION_BUFFER;
uavDesc.Format = D3DUtil::getMapFormat(desc.format);
if (bufferStride)
{
uavDesc.Buffer.FirstElement = offset / bufferStride;
uavDesc.Buffer.NumElements = UINT(size / bufferStride);
uavDesc.Buffer.StructureByteStride = bufferStride;
}
else if (desc.format == Format::Unknown)
{
uavDesc.Format = DXGI_FORMAT_R32_TYPELESS;
uavDesc.Buffer.FirstElement = offset / 4;
uavDesc.Buffer.NumElements = UINT(size / 4);
uavDesc.Buffer.Flags |= D3D12_BUFFER_UAV_FLAG_RAW;
}
else
{
FormatInfo sizeInfo;
gfxGetFormatInfo(desc.format, &sizeInfo);
assert(sizeInfo.pixelsPerBlock == 1);
uavDesc.Buffer.FirstElement = offset / sizeInfo.blockSizeInBytes;
uavDesc.Buffer.NumElements = UINT(size / sizeInfo.blockSizeInBytes);
}
if (size >= (1ull << 32) - 8)
{
// D3D12 does not support view descriptors that has size near 4GB.
// We will not create actual SRV/UAVs for such large buffers.
// However, a buffer this large can still be bound as root parameter.
// So instead of failing, we quietly ignore descriptor creation.
outDescriptor->cpuHandle.ptr = 0;
}
else
{
SLANG_RETURN_ON_FAIL(descriptorHeap->allocate(outDescriptor));
device->m_device->CreateUnorderedAccessView(
resourceImpl->m_resource,
counterResourceImpl ? counterResourceImpl->m_resource.getResource() : nullptr,
&uavDesc,
outDescriptor->cpuHandle);
}
}
break;
case IResourceView::Type::ShaderResource:
{
D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc = {};
srvDesc.ViewDimension = D3D12_SRV_DIMENSION_BUFFER;
srvDesc.Format = D3DUtil::getMapFormat(desc.format);
srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
if (bufferStride)
{
srvDesc.Buffer.FirstElement = offset / bufferStride;
srvDesc.Buffer.NumElements = UINT(size / bufferStride);
srvDesc.Buffer.StructureByteStride = bufferStride;
}
else if (desc.format == Format::Unknown)
{
srvDesc.Format = DXGI_FORMAT_R32_TYPELESS;
srvDesc.Buffer.FirstElement = offset / 4;
srvDesc.Buffer.NumElements = UINT(size / 4);
srvDesc.Buffer.Flags |= D3D12_BUFFER_SRV_FLAG_RAW;
}
else
{
FormatInfo sizeInfo;
gfxGetFormatInfo(desc.format, &sizeInfo);
assert(sizeInfo.pixelsPerBlock == 1);
srvDesc.Buffer.FirstElement = offset / sizeInfo.blockSizeInBytes;
srvDesc.Buffer.NumElements = UINT(size / sizeInfo.blockSizeInBytes);
}
if (size >= (1ull << 32) - 8)
{
// D3D12 does not support view descriptors that has size near 4GB.
// We will not create actual SRV/UAVs for such large buffers.
// However, a buffer this large can still be bound as root parameter.
// So instead of failing, we quietly ignore descriptor creation.
outDescriptor->cpuHandle.ptr = 0;
}
else
{
SLANG_RETURN_ON_FAIL(descriptorHeap->allocate(outDescriptor));
device->m_device->CreateShaderResourceView(
resourceImpl->m_resource,
&srvDesc,
outDescriptor->cpuHandle);
}
}
break;
}
return SLANG_OK;
}
SlangResult ResourceViewInternalImpl::getBufferDescriptorForBinding(
DeviceImpl* device,
ResourceViewImpl* view,
uint32_t bufferStride,
D3D12Descriptor& outDescriptor)
{
// Look for an existing descriptor from the cache if it exists.
if (auto descriptor = m_mapBufferStrideToDescriptor.tryGetValue(bufferStride))
{
outDescriptor = *descriptor;
return SLANG_OK;
}
// We need to create and cache a d3d12 descriptor for the resource view that encodes
// the given buffer stride.
auto bufferResImpl = static_cast<BufferResourceImpl*>(view->m_resource.get());
auto desc = view->m_desc;
SLANG_RETURN_ON_FAIL(createD3D12BufferDescriptor(
bufferResImpl,
static_cast<BufferResourceImpl*>(view->m_counterResource.get()),
desc,
bufferStride,
device,
m_allocator,
&outDescriptor));
m_mapBufferStrideToDescriptor[bufferStride] = outDescriptor;
return SLANG_OK;
}
Result ResourceViewImpl::getNativeHandle(InteropHandle* outHandle)
{
outHandle->api = InteropHandleAPI::D3D12CpuDescriptorHandle;
outHandle->handleValue = m_descriptor.cpuHandle.ptr;
return SLANG_OK;
}
#if SLANG_GFX_HAS_DXR_SUPPORT
DeviceAddress AccelerationStructureImpl::getDeviceAddress()
{
return m_buffer->getDeviceAddress() + m_offset;
}
Result AccelerationStructureImpl::getNativeHandle(InteropHandle* outHandle)
{
outHandle->api = InteropHandleAPI::DeviceAddress;
outHandle->handleValue = getDeviceAddress();
return SLANG_OK;
}
#endif // SLANG_GFX_HAS_DXR_SUPPORT
} // namespace d3d12
} // namespace gfx
|