Skip to content

Commit 698ba86

Browse files
jsmall-zzzTim Foley
authored and
Tim Foley
committed
1st stage renderer binding refactor (shader-slang#587)
* First pass at support for textures in vulkan. * Binding state has first pass support for VkImageView VkSampler. * Split out _calcImageViewType * Fix bug in debug build around constant buffer being added but not part of the binding description for the test. * Offset recalculated for vk texture construction just store the texture size for each mip level. * When outputing a vector type with a size of 1 in GLSL, it needs to be output as the underlying type. For example vector<float,1> should be output as float in GLSL. * Vulkan render-test produces right output for the test tests/compute/textureSamplingTest.slang -slang -gcompute -o tests/compute/textureSamplingTest.slang.actual.txt -vk * Small improvement around xml encoding a string. * More generalized test synthesis. * Fix image usage flags for Vulkan. * Improvements to what gets synthesized vulkan tests. * Do transition on all mip levels. * Fixing problems appearing from vulkan debug layer. * Disable Vulkan synthesized tests for now. * Add Resource::Type member to Resource::DescBase. * Removed the CompactIndexSlice from binding. Just bind the indices needed. * BindingRegister -> RegisterSet * RegisterSet -> RegisterRange * Typo fix for debug build. * Remove comment that no longer applied.
1 parent 8d77db3 commit 698ba86

9 files changed

+236
-301
lines changed

tools/render-test/main.cpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,9 @@ SlangResult RenderTestApp::initialize(Renderer* renderer, ShaderCompiler* shader
131131
// in the test file
132132

133133
if ((gOptions.shaderType == Options::ShaderProgramType::Graphics || gOptions.shaderType == Options::ShaderProgramType::GraphicsCompute)
134-
&& bindingStateDesc.findBindingIndex(Resource::BindFlag::ConstantBuffer, -1, 0) < 0)
134+
&& bindingStateDesc.findBindingIndex(Resource::BindFlag::ConstantBuffer, 0) < 0)
135135
{
136-
BindingState::ShaderBindSet shaderBindSet;
137-
shaderBindSet.setAll(bindingStateDesc.makeCompactSlice(0));
138-
139-
bindingStateDesc.addResource(BindingType::Buffer, m_constantBuffer, shaderBindSet);
136+
bindingStateDesc.addResource(BindingType::Buffer, m_constantBuffer, BindingState::RegisterRange::makeSingle(0) );
140137

141138
m_numAddedConstantBuffers++;
142139
}

tools/render-test/render-d3d11.cpp

+28-28
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class D3D11Renderer : public Renderer, public ShaderCompiler
5050
virtual void setClearColor(const float color[4]) override;
5151
virtual void clearFrame() override;
5252
virtual void presentFrame() override;
53-
virtual TextureResource* createTextureResource(Resource::Type type, Resource::Usage initialUsage, const TextureResource::Desc& desc, const TextureResource::Data* initData) override;
53+
virtual TextureResource* createTextureResource(Resource::Usage initialUsage, const TextureResource::Desc& desc, const TextureResource::Data* initData) override;
5454
virtual BufferResource* createBufferResource(Resource::Usage initialUsage, const BufferResource::Desc& bufferDesc, const void* initData) override;
5555
virtual SlangResult captureScreenSurface(Surface& surfaceOut) override;
5656
virtual InputLayout* createInputLayout( const InputElementDesc* inputElements, UInt inputElementCount) override;
@@ -79,8 +79,6 @@ class D3D11Renderer : public Renderer, public ShaderCompiler
7979
ComPtr<ID3D11ShaderResourceView> m_srv;
8080
ComPtr<ID3D11UnorderedAccessView> m_uav;
8181
ComPtr<ID3D11SamplerState> m_samplerState;
82-
83-
int m_binding = 0;
8482
};
8583

8684
class BindingStateImpl: public BindingState
@@ -125,8 +123,8 @@ class D3D11Renderer : public Renderer, public ShaderCompiler
125123
public:
126124
typedef TextureResource Parent;
127125

128-
TextureResourceImpl(Type type, const Desc& desc, Usage initialUsage) :
129-
Parent(type, desc),
126+
TextureResourceImpl(const Desc& desc, Usage initialUsage) :
127+
Parent(desc),
130128
m_initialUsage(initialUsage)
131129
{
132130
}
@@ -431,12 +429,12 @@ static int _calcResourceAccessFlags(int accessFlags)
431429
}
432430
}
433431

434-
TextureResource* D3D11Renderer::createTextureResource(Resource::Type type, Resource::Usage initialUsage, const TextureResource::Desc& descIn, const TextureResource::Data* initData)
432+
TextureResource* D3D11Renderer::createTextureResource(Resource::Usage initialUsage, const TextureResource::Desc& descIn, const TextureResource::Data* initData)
435433
{
436434
TextureResource::Desc srcDesc(descIn);
437-
srcDesc.setDefaults(type, initialUsage);
435+
srcDesc.setDefaults(initialUsage);
438436

439-
const int effectiveArraySize = srcDesc.calcEffectiveArraySize(type);
437+
const int effectiveArraySize = srcDesc.calcEffectiveArraySize();
440438

441439
assert(initData);
442440
assert(initData->numSubResources == srcDesc.numMipLevels * effectiveArraySize * srcDesc.size.depth);
@@ -474,9 +472,9 @@ TextureResource* D3D11Renderer::createTextureResource(Resource::Type type, Resou
474472

475473
const int accessFlags = _calcResourceAccessFlags(srcDesc.cpuAccessFlags);
476474

477-
RefPtr<TextureResourceImpl> texture(new TextureResourceImpl(type, srcDesc, initialUsage));
475+
RefPtr<TextureResourceImpl> texture(new TextureResourceImpl(srcDesc, initialUsage));
478476

479-
switch (type)
477+
switch (srcDesc.type)
480478
{
481479
case Resource::Type::Texture1D:
482480
{
@@ -513,7 +511,7 @@ TextureResource* D3D11Renderer::createTextureResource(Resource::Type type, Resou
513511
desc.SampleDesc.Count = srcDesc.sampleDesc.numSamples;
514512
desc.SampleDesc.Quality = srcDesc.sampleDesc.quality;
515513

516-
if (type == Resource::Type::TextureCube)
514+
if (srcDesc.type == Resource::Type::TextureCube)
517515
{
518516
desc.MiscFlags |= D3D11_RESOURCE_MISC_TEXTURECUBE;
519517
}
@@ -840,7 +838,7 @@ BindingState* D3D11Renderer::createBindingState(const BindingState::Desc& bindin
840838
auto& dstDetail = dstDetails[i];
841839
const auto& srcBinding = srcBindings[i];
842840

843-
dstDetail.m_binding = bindingStateDesc.getFirst(BindingState::ShaderStyle::Hlsl, srcBinding.shaderBindSet);
841+
assert(srcBinding.registerRange.isSingle());
844842

845843
switch (srcBinding.bindingType)
846844
{
@@ -1028,6 +1026,8 @@ void D3D11Renderer::_applyBindingState(bool isCompute)
10281026
const auto& binding = bindings[i];
10291027
const auto& detail = details[i];
10301028

1029+
const int bindingIndex = binding.registerRange.getSingleIndex();
1030+
10311031
switch (binding.bindingType)
10321032
{
10331033
case BindingType::Buffer:
@@ -1037,29 +1037,29 @@ void D3D11Renderer::_applyBindingState(bool isCompute)
10371037
{
10381038
ID3D11Buffer* buffer = static_cast<BufferResourceImpl*>(binding.resource.Ptr())->m_buffer;
10391039
if (isCompute)
1040-
context->CSSetConstantBuffers(detail.m_binding, 1, &buffer);
1040+
context->CSSetConstantBuffers(bindingIndex, 1, &buffer);
10411041
else
10421042
{
1043-
context->VSSetConstantBuffers(detail.m_binding, 1, &buffer);
1044-
context->PSSetConstantBuffers(detail.m_binding, 1, &buffer);
1043+
context->VSSetConstantBuffers(bindingIndex, 1, &buffer);
1044+
context->PSSetConstantBuffers(bindingIndex, 1, &buffer);
10451045
}
10461046
}
10471047
else if (detail.m_uav)
10481048
{
10491049
if (isCompute)
1050-
context->CSSetUnorderedAccessViews(detail.m_binding, 1, detail.m_uav.readRef(), nullptr);
1050+
context->CSSetUnorderedAccessViews(bindingIndex, 1, detail.m_uav.readRef(), nullptr);
10511051
else
10521052
context->OMSetRenderTargetsAndUnorderedAccessViews(m_currentBindings->getDesc().m_numRenderTargets,
1053-
m_renderTargetViews.Buffer()->readRef(), nullptr, detail.m_binding, 1, detail.m_uav.readRef(), nullptr);
1053+
m_renderTargetViews.Buffer()->readRef(), nullptr, bindingIndex, 1, detail.m_uav.readRef(), nullptr);
10541054
}
10551055
else
10561056
{
10571057
if (isCompute)
1058-
context->CSSetShaderResources(detail.m_binding, 1, detail.m_srv.readRef());
1058+
context->CSSetShaderResources(bindingIndex, 1, detail.m_srv.readRef());
10591059
else
10601060
{
1061-
context->PSSetShaderResources(detail.m_binding, 1, detail.m_srv.readRef());
1062-
context->VSSetShaderResources(detail.m_binding, 1, detail.m_srv.readRef());
1061+
context->PSSetShaderResources(bindingIndex, 1, detail.m_srv.readRef());
1062+
context->VSSetShaderResources(bindingIndex, 1, detail.m_srv.readRef());
10631063
}
10641064
}
10651065
break;
@@ -1069,31 +1069,31 @@ void D3D11Renderer::_applyBindingState(bool isCompute)
10691069
if (detail.m_uav)
10701070
{
10711071
if (isCompute)
1072-
context->CSSetUnorderedAccessViews(detail.m_binding, 1, detail.m_uav.readRef(), nullptr);
1072+
context->CSSetUnorderedAccessViews(bindingIndex, 1, detail.m_uav.readRef(), nullptr);
10731073
else
10741074
context->OMSetRenderTargetsAndUnorderedAccessViews(D3D11_KEEP_RENDER_TARGETS_AND_DEPTH_STENCIL,
1075-
nullptr, nullptr, detail.m_binding, 1, detail.m_uav.readRef(), nullptr);
1075+
nullptr, nullptr, bindingIndex, 1, detail.m_uav.readRef(), nullptr);
10761076
}
10771077
else
10781078
{
10791079
if (isCompute)
1080-
context->CSSetShaderResources(detail.m_binding, 1, detail.m_srv.readRef());
1080+
context->CSSetShaderResources(bindingIndex, 1, detail.m_srv.readRef());
10811081
else
10821082
{
1083-
context->PSSetShaderResources(detail.m_binding, 1, detail.m_srv.readRef());
1084-
context->VSSetShaderResources(detail.m_binding, 1, detail.m_srv.readRef());
1083+
context->PSSetShaderResources(bindingIndex, 1, detail.m_srv.readRef());
1084+
context->VSSetShaderResources(bindingIndex, 1, detail.m_srv.readRef());
10851085
}
10861086
}
10871087
break;
10881088
}
10891089
case BindingType::Sampler:
10901090
{
10911091
if (isCompute)
1092-
context->CSSetSamplers(detail.m_binding, 1, detail.m_samplerState.readRef());
1092+
context->CSSetSamplers(bindingIndex, 1, detail.m_samplerState.readRef());
10931093
else
10941094
{
1095-
context->PSSetSamplers(detail.m_binding, 1, detail.m_samplerState.readRef());
1096-
context->VSSetSamplers(detail.m_binding, 1, detail.m_samplerState.readRef());
1095+
context->PSSetSamplers(bindingIndex, 1, detail.m_samplerState.readRef());
1096+
context->VSSetSamplers(bindingIndex, 1, detail.m_samplerState.readRef());
10971097
}
10981098
break;
10991099
}

tools/render-test/render-d3d12.cpp

+16-15
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class D3D12Renderer : public Renderer, public ShaderCompiler
5757
virtual void setClearColor(const float color[4]) override;
5858
virtual void clearFrame() override;
5959
virtual void presentFrame() override;
60-
virtual TextureResource* createTextureResource(Resource::Type type, Resource::Usage initialUsage, const TextureResource::Desc& desc, const TextureResource::Data* initData) override;
60+
virtual TextureResource* createTextureResource(Resource::Usage initialUsage, const TextureResource::Desc& desc, const TextureResource::Data* initData) override;
6161
virtual BufferResource* createBufferResource(Resource::Usage initialUsage, const BufferResource::Desc& bufferDesc, const void* initData) override;
6262
virtual SlangResult captureScreenSurface(Surface& surfaceOut) override;
6363
virtual InputLayout* createInputLayout(const InputElementDesc* inputElements, UInt inputElementCount) override;
@@ -178,8 +178,8 @@ class D3D12Renderer : public Renderer, public ShaderCompiler
178178
public:
179179
typedef TextureResource Parent;
180180

181-
TextureResourceImpl(Type type, const Desc& desc):
182-
Parent(type, desc)
181+
TextureResourceImpl(const Desc& desc):
182+
Parent(desc)
183183
{
184184
}
185185

@@ -198,7 +198,6 @@ class D3D12Renderer : public Renderer, public ShaderCompiler
198198
int m_srvIndex = -1;
199199
int m_uavIndex = -1;
200200
int m_samplerIndex = -1;
201-
int m_binding = 0;
202201
};
203202

204203
class BindingStateImpl: public BindingState
@@ -1089,6 +1088,8 @@ Result D3D12Renderer::_calcBindParameters(BindParameters& params)
10891088
const auto& binding = bindings[i];
10901089
const auto& detail = details[i];
10911090

1091+
const int bindingIndex = binding.registerRange.getSingleIndex();
1092+
10921093
if (binding.bindingType == BindingType::Buffer)
10931094
{
10941095
assert(binding.resource && binding.resource->isBuffer());
@@ -1102,7 +1103,7 @@ Result D3D12Renderer::_calcBindParameters(BindParameters& params)
11021103
param.ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
11031104

11041105
D3D12_ROOT_DESCRIPTOR& descriptor = param.Descriptor;
1105-
descriptor.ShaderRegister = detail.m_binding;
1106+
descriptor.ShaderRegister = bindingIndex;
11061107
descriptor.RegisterSpace = 0;
11071108

11081109
numConstantBuffers++;
@@ -1115,7 +1116,7 @@ Result D3D12Renderer::_calcBindParameters(BindParameters& params)
11151116

11161117
range.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
11171118
range.NumDescriptors = 1;
1118-
range.BaseShaderRegister = detail.m_binding;
1119+
range.BaseShaderRegister = bindingIndex;
11191120
range.RegisterSpace = 0;
11201121
range.OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
11211122

@@ -1135,7 +1136,7 @@ Result D3D12Renderer::_calcBindParameters(BindParameters& params)
11351136

11361137
range.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_UAV;
11371138
range.NumDescriptors = 1;
1138-
range.BaseShaderRegister = detail.m_binding;
1139+
range.BaseShaderRegister = bindingIndex;
11391140
range.RegisterSpace = 0;
11401141
range.OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
11411142

@@ -1750,23 +1751,23 @@ static D3D12_RESOURCE_DIMENSION _calcResourceDimension(Resource::Type type)
17501751
}
17511752
}
17521753

1753-
TextureResource* D3D12Renderer::createTextureResource(Resource::Type type, Resource::Usage initialUsage, const TextureResource::Desc& descIn, const TextureResource::Data* initData)
1754+
TextureResource* D3D12Renderer::createTextureResource(Resource::Usage initialUsage, const TextureResource::Desc& descIn, const TextureResource::Data* initData)
17541755
{
17551756
// Description of uploading on Dx12
17561757
// https://msdn.microsoft.com/en-us/library/windows/desktop/dn899215%28v=vs.85%29.aspx
17571758

17581759
TextureResource::Desc srcDesc(descIn);
1759-
srcDesc.setDefaults(type, initialUsage);
1760+
srcDesc.setDefaults(initialUsage);
17601761

17611762
const DXGI_FORMAT pixelFormat = D3DUtil::getMapFormat(srcDesc.format);
17621763
if (pixelFormat == DXGI_FORMAT_UNKNOWN)
17631764
{
17641765
return nullptr;
17651766
}
17661767

1767-
const int arraySize = srcDesc.calcEffectiveArraySize(type);
1768+
const int arraySize = srcDesc.calcEffectiveArraySize();
17681769

1769-
const D3D12_RESOURCE_DIMENSION dimension = _calcResourceDimension(type);
1770+
const D3D12_RESOURCE_DIMENSION dimension = _calcResourceDimension(srcDesc.type);
17701771
if (dimension == D3D12_RESOURCE_DIMENSION_UNKNOWN)
17711772
{
17721773
return nullptr;
@@ -1791,7 +1792,7 @@ TextureResource* D3D12Renderer::createTextureResource(Resource::Type type, Resou
17911792
resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
17921793
resourceDesc.Alignment = 0;
17931794

1794-
RefPtr<TextureResourceImpl> texture(new TextureResourceImpl(type, srcDesc));
1795+
RefPtr<TextureResourceImpl> texture(new TextureResourceImpl(srcDesc));
17951796

17961797
// Create the target resource
17971798
{
@@ -2297,8 +2298,8 @@ BindingState* D3D12Renderer::createBindingState(const BindingState::Desc& bindin
22972298
const auto& srcEntry = srcBindings[i];
22982299
auto& dstDetail = dstDetails[i];
22992300

2300-
dstDetail.m_binding = bindingStateDesc.getFirst(BindingState::ShaderStyle::Hlsl, srcEntry.shaderBindSet);
2301-
2301+
const int bindingIndex = srcEntry.registerRange.getSingleIndex();
2302+
23022303
switch (srcEntry.bindingType)
23032304
{
23042305
case BindingType::Buffer:
@@ -2405,7 +2406,7 @@ BindingState* D3D12Renderer::createBindingState(const BindingState::Desc& bindin
24052406
{
24062407
const BindingState::SamplerDesc& samplerDesc = bindingStateDesc.m_samplerDescs[srcEntry.descIndex];
24072408

2408-
const int samplerIndex = bindingStateDesc.getFirst(BindingState::ShaderStyle::Hlsl, srcEntry.shaderBindSet);
2409+
const int samplerIndex = bindingIndex;
24092410
dstDetail.m_samplerIndex = samplerIndex;
24102411
bindingState->m_samplerHeap.placeAt(samplerIndex);
24112412

0 commit comments

Comments
 (0)