Skip to content

Commit

Permalink
1st stage renderer binding refactor (#587)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
jsmall-zzz authored and Tim Foley committed Jun 1, 2018
1 parent 8d77db3 commit 698ba86
Show file tree
Hide file tree
Showing 9 changed files with 236 additions and 301 deletions.
7 changes: 2 additions & 5 deletions tools/render-test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,9 @@ SlangResult RenderTestApp::initialize(Renderer* renderer, ShaderCompiler* shader
// in the test file

if ((gOptions.shaderType == Options::ShaderProgramType::Graphics || gOptions.shaderType == Options::ShaderProgramType::GraphicsCompute)
&& bindingStateDesc.findBindingIndex(Resource::BindFlag::ConstantBuffer, -1, 0) < 0)
&& bindingStateDesc.findBindingIndex(Resource::BindFlag::ConstantBuffer, 0) < 0)
{
BindingState::ShaderBindSet shaderBindSet;
shaderBindSet.setAll(bindingStateDesc.makeCompactSlice(0));

bindingStateDesc.addResource(BindingType::Buffer, m_constantBuffer, shaderBindSet);
bindingStateDesc.addResource(BindingType::Buffer, m_constantBuffer, BindingState::RegisterRange::makeSingle(0) );

m_numAddedConstantBuffers++;
}
Expand Down
56 changes: 28 additions & 28 deletions tools/render-test/render-d3d11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class D3D11Renderer : public Renderer, public ShaderCompiler
virtual void setClearColor(const float color[4]) override;
virtual void clearFrame() override;
virtual void presentFrame() override;
virtual TextureResource* createTextureResource(Resource::Type type, Resource::Usage initialUsage, const TextureResource::Desc& desc, const TextureResource::Data* initData) override;
virtual TextureResource* createTextureResource(Resource::Usage initialUsage, const TextureResource::Desc& desc, const TextureResource::Data* initData) override;
virtual BufferResource* createBufferResource(Resource::Usage initialUsage, const BufferResource::Desc& bufferDesc, const void* initData) override;
virtual SlangResult captureScreenSurface(Surface& surfaceOut) override;
virtual InputLayout* createInputLayout( const InputElementDesc* inputElements, UInt inputElementCount) override;
Expand Down Expand Up @@ -79,8 +79,6 @@ class D3D11Renderer : public Renderer, public ShaderCompiler
ComPtr<ID3D11ShaderResourceView> m_srv;
ComPtr<ID3D11UnorderedAccessView> m_uav;
ComPtr<ID3D11SamplerState> m_samplerState;

int m_binding = 0;
};

class BindingStateImpl: public BindingState
Expand Down Expand Up @@ -125,8 +123,8 @@ class D3D11Renderer : public Renderer, public ShaderCompiler
public:
typedef TextureResource Parent;

TextureResourceImpl(Type type, const Desc& desc, Usage initialUsage) :
Parent(type, desc),
TextureResourceImpl(const Desc& desc, Usage initialUsage) :
Parent(desc),
m_initialUsage(initialUsage)
{
}
Expand Down Expand Up @@ -431,12 +429,12 @@ static int _calcResourceAccessFlags(int accessFlags)
}
}

TextureResource* D3D11Renderer::createTextureResource(Resource::Type type, Resource::Usage initialUsage, const TextureResource::Desc& descIn, const TextureResource::Data* initData)
TextureResource* D3D11Renderer::createTextureResource(Resource::Usage initialUsage, const TextureResource::Desc& descIn, const TextureResource::Data* initData)
{
TextureResource::Desc srcDesc(descIn);
srcDesc.setDefaults(type, initialUsage);
srcDesc.setDefaults(initialUsage);

const int effectiveArraySize = srcDesc.calcEffectiveArraySize(type);
const int effectiveArraySize = srcDesc.calcEffectiveArraySize();

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

const int accessFlags = _calcResourceAccessFlags(srcDesc.cpuAccessFlags);

RefPtr<TextureResourceImpl> texture(new TextureResourceImpl(type, srcDesc, initialUsage));
RefPtr<TextureResourceImpl> texture(new TextureResourceImpl(srcDesc, initialUsage));

switch (type)
switch (srcDesc.type)
{
case Resource::Type::Texture1D:
{
Expand Down Expand Up @@ -513,7 +511,7 @@ TextureResource* D3D11Renderer::createTextureResource(Resource::Type type, Resou
desc.SampleDesc.Count = srcDesc.sampleDesc.numSamples;
desc.SampleDesc.Quality = srcDesc.sampleDesc.quality;

if (type == Resource::Type::TextureCube)
if (srcDesc.type == Resource::Type::TextureCube)
{
desc.MiscFlags |= D3D11_RESOURCE_MISC_TEXTURECUBE;
}
Expand Down Expand Up @@ -840,7 +838,7 @@ BindingState* D3D11Renderer::createBindingState(const BindingState::Desc& bindin
auto& dstDetail = dstDetails[i];
const auto& srcBinding = srcBindings[i];

dstDetail.m_binding = bindingStateDesc.getFirst(BindingState::ShaderStyle::Hlsl, srcBinding.shaderBindSet);
assert(srcBinding.registerRange.isSingle());

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

const int bindingIndex = binding.registerRange.getSingleIndex();

switch (binding.bindingType)
{
case BindingType::Buffer:
Expand All @@ -1037,29 +1037,29 @@ void D3D11Renderer::_applyBindingState(bool isCompute)
{
ID3D11Buffer* buffer = static_cast<BufferResourceImpl*>(binding.resource.Ptr())->m_buffer;
if (isCompute)
context->CSSetConstantBuffers(detail.m_binding, 1, &buffer);
context->CSSetConstantBuffers(bindingIndex, 1, &buffer);
else
{
context->VSSetConstantBuffers(detail.m_binding, 1, &buffer);
context->PSSetConstantBuffers(detail.m_binding, 1, &buffer);
context->VSSetConstantBuffers(bindingIndex, 1, &buffer);
context->PSSetConstantBuffers(bindingIndex, 1, &buffer);
}
}
else if (detail.m_uav)
{
if (isCompute)
context->CSSetUnorderedAccessViews(detail.m_binding, 1, detail.m_uav.readRef(), nullptr);
context->CSSetUnorderedAccessViews(bindingIndex, 1, detail.m_uav.readRef(), nullptr);
else
context->OMSetRenderTargetsAndUnorderedAccessViews(m_currentBindings->getDesc().m_numRenderTargets,
m_renderTargetViews.Buffer()->readRef(), nullptr, detail.m_binding, 1, detail.m_uav.readRef(), nullptr);
m_renderTargetViews.Buffer()->readRef(), nullptr, bindingIndex, 1, detail.m_uav.readRef(), nullptr);
}
else
{
if (isCompute)
context->CSSetShaderResources(detail.m_binding, 1, detail.m_srv.readRef());
context->CSSetShaderResources(bindingIndex, 1, detail.m_srv.readRef());
else
{
context->PSSetShaderResources(detail.m_binding, 1, detail.m_srv.readRef());
context->VSSetShaderResources(detail.m_binding, 1, detail.m_srv.readRef());
context->PSSetShaderResources(bindingIndex, 1, detail.m_srv.readRef());
context->VSSetShaderResources(bindingIndex, 1, detail.m_srv.readRef());
}
}
break;
Expand All @@ -1069,31 +1069,31 @@ void D3D11Renderer::_applyBindingState(bool isCompute)
if (detail.m_uav)
{
if (isCompute)
context->CSSetUnorderedAccessViews(detail.m_binding, 1, detail.m_uav.readRef(), nullptr);
context->CSSetUnorderedAccessViews(bindingIndex, 1, detail.m_uav.readRef(), nullptr);
else
context->OMSetRenderTargetsAndUnorderedAccessViews(D3D11_KEEP_RENDER_TARGETS_AND_DEPTH_STENCIL,
nullptr, nullptr, detail.m_binding, 1, detail.m_uav.readRef(), nullptr);
nullptr, nullptr, bindingIndex, 1, detail.m_uav.readRef(), nullptr);
}
else
{
if (isCompute)
context->CSSetShaderResources(detail.m_binding, 1, detail.m_srv.readRef());
context->CSSetShaderResources(bindingIndex, 1, detail.m_srv.readRef());
else
{
context->PSSetShaderResources(detail.m_binding, 1, detail.m_srv.readRef());
context->VSSetShaderResources(detail.m_binding, 1, detail.m_srv.readRef());
context->PSSetShaderResources(bindingIndex, 1, detail.m_srv.readRef());
context->VSSetShaderResources(bindingIndex, 1, detail.m_srv.readRef());
}
}
break;
}
case BindingType::Sampler:
{
if (isCompute)
context->CSSetSamplers(detail.m_binding, 1, detail.m_samplerState.readRef());
context->CSSetSamplers(bindingIndex, 1, detail.m_samplerState.readRef());
else
{
context->PSSetSamplers(detail.m_binding, 1, detail.m_samplerState.readRef());
context->VSSetSamplers(detail.m_binding, 1, detail.m_samplerState.readRef());
context->PSSetSamplers(bindingIndex, 1, detail.m_samplerState.readRef());
context->VSSetSamplers(bindingIndex, 1, detail.m_samplerState.readRef());
}
break;
}
Expand Down
31 changes: 16 additions & 15 deletions tools/render-test/render-d3d12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class D3D12Renderer : public Renderer, public ShaderCompiler
virtual void setClearColor(const float color[4]) override;
virtual void clearFrame() override;
virtual void presentFrame() override;
virtual TextureResource* createTextureResource(Resource::Type type, Resource::Usage initialUsage, const TextureResource::Desc& desc, const TextureResource::Data* initData) override;
virtual TextureResource* createTextureResource(Resource::Usage initialUsage, const TextureResource::Desc& desc, const TextureResource::Data* initData) override;
virtual BufferResource* createBufferResource(Resource::Usage initialUsage, const BufferResource::Desc& bufferDesc, const void* initData) override;
virtual SlangResult captureScreenSurface(Surface& surfaceOut) override;
virtual InputLayout* createInputLayout(const InputElementDesc* inputElements, UInt inputElementCount) override;
Expand Down Expand Up @@ -178,8 +178,8 @@ class D3D12Renderer : public Renderer, public ShaderCompiler
public:
typedef TextureResource Parent;

TextureResourceImpl(Type type, const Desc& desc):
Parent(type, desc)
TextureResourceImpl(const Desc& desc):
Parent(desc)
{
}

Expand All @@ -198,7 +198,6 @@ class D3D12Renderer : public Renderer, public ShaderCompiler
int m_srvIndex = -1;
int m_uavIndex = -1;
int m_samplerIndex = -1;
int m_binding = 0;
};

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

const int bindingIndex = binding.registerRange.getSingleIndex();

if (binding.bindingType == BindingType::Buffer)
{
assert(binding.resource && binding.resource->isBuffer());
Expand All @@ -1102,7 +1103,7 @@ Result D3D12Renderer::_calcBindParameters(BindParameters& params)
param.ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;

D3D12_ROOT_DESCRIPTOR& descriptor = param.Descriptor;
descriptor.ShaderRegister = detail.m_binding;
descriptor.ShaderRegister = bindingIndex;
descriptor.RegisterSpace = 0;

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

range.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
range.NumDescriptors = 1;
range.BaseShaderRegister = detail.m_binding;
range.BaseShaderRegister = bindingIndex;
range.RegisterSpace = 0;
range.OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;

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

range.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_UAV;
range.NumDescriptors = 1;
range.BaseShaderRegister = detail.m_binding;
range.BaseShaderRegister = bindingIndex;
range.RegisterSpace = 0;
range.OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;

Expand Down Expand Up @@ -1750,23 +1751,23 @@ static D3D12_RESOURCE_DIMENSION _calcResourceDimension(Resource::Type type)
}
}

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

TextureResource::Desc srcDesc(descIn);
srcDesc.setDefaults(type, initialUsage);
srcDesc.setDefaults(initialUsage);

const DXGI_FORMAT pixelFormat = D3DUtil::getMapFormat(srcDesc.format);
if (pixelFormat == DXGI_FORMAT_UNKNOWN)
{
return nullptr;
}

const int arraySize = srcDesc.calcEffectiveArraySize(type);
const int arraySize = srcDesc.calcEffectiveArraySize();

const D3D12_RESOURCE_DIMENSION dimension = _calcResourceDimension(type);
const D3D12_RESOURCE_DIMENSION dimension = _calcResourceDimension(srcDesc.type);
if (dimension == D3D12_RESOURCE_DIMENSION_UNKNOWN)
{
return nullptr;
Expand All @@ -1791,7 +1792,7 @@ TextureResource* D3D12Renderer::createTextureResource(Resource::Type type, Resou
resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
resourceDesc.Alignment = 0;

RefPtr<TextureResourceImpl> texture(new TextureResourceImpl(type, srcDesc));
RefPtr<TextureResourceImpl> texture(new TextureResourceImpl(srcDesc));

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

dstDetail.m_binding = bindingStateDesc.getFirst(BindingState::ShaderStyle::Hlsl, srcEntry.shaderBindSet);
const int bindingIndex = srcEntry.registerRange.getSingleIndex();

switch (srcEntry.bindingType)
{
case BindingType::Buffer:
Expand Down Expand Up @@ -2405,7 +2406,7 @@ BindingState* D3D12Renderer::createBindingState(const BindingState::Desc& bindin
{
const BindingState::SamplerDesc& samplerDesc = bindingStateDesc.m_samplerDescs[srcEntry.descIndex];

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

Expand Down
Loading

0 comments on commit 698ba86

Please sign in to comment.