diff --git a/tools/render-test/main.cpp b/tools/render-test/main.cpp index f72a058248..767c33e14f 100644 --- a/tools/render-test/main.cpp +++ b/tools/render-test/main.cpp @@ -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++; } diff --git a/tools/render-test/render-d3d11.cpp b/tools/render-test/render-d3d11.cpp index 9523d37cde..d47a4a559c 100644 --- a/tools/render-test/render-d3d11.cpp +++ b/tools/render-test/render-d3d11.cpp @@ -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; @@ -79,8 +79,6 @@ class D3D11Renderer : public Renderer, public ShaderCompiler ComPtr m_srv; ComPtr m_uav; ComPtr m_samplerState; - - int m_binding = 0; }; class BindingStateImpl: public BindingState @@ -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) { } @@ -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); @@ -474,9 +472,9 @@ TextureResource* D3D11Renderer::createTextureResource(Resource::Type type, Resou const int accessFlags = _calcResourceAccessFlags(srcDesc.cpuAccessFlags); - RefPtr texture(new TextureResourceImpl(type, srcDesc, initialUsage)); + RefPtr texture(new TextureResourceImpl(srcDesc, initialUsage)); - switch (type) + switch (srcDesc.type) { case Resource::Type::Texture1D: { @@ -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; } @@ -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) { @@ -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: @@ -1037,29 +1037,29 @@ void D3D11Renderer::_applyBindingState(bool isCompute) { ID3D11Buffer* buffer = static_cast(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; @@ -1069,19 +1069,19 @@ 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; @@ -1089,11 +1089,11 @@ void D3D11Renderer::_applyBindingState(bool isCompute) 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; } diff --git a/tools/render-test/render-d3d12.cpp b/tools/render-test/render-d3d12.cpp index a582e3feb0..691ab7d254 100644 --- a/tools/render-test/render-d3d12.cpp +++ b/tools/render-test/render-d3d12.cpp @@ -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; @@ -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) { } @@ -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 @@ -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()); @@ -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++; @@ -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; @@ -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; @@ -1750,13 +1751,13 @@ 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) @@ -1764,9 +1765,9 @@ TextureResource* D3D12Renderer::createTextureResource(Resource::Type type, Resou 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; @@ -1791,7 +1792,7 @@ TextureResource* D3D12Renderer::createTextureResource(Resource::Type type, Resou resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; resourceDesc.Alignment = 0; - RefPtr texture(new TextureResourceImpl(type, srcDesc)); + RefPtr texture(new TextureResourceImpl(srcDesc)); // Create the target resource { @@ -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: @@ -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); diff --git a/tools/render-test/render-gl.cpp b/tools/render-test/render-gl.cpp index 88537583b9..00e4427b8c 100644 --- a/tools/render-test/render-gl.cpp +++ b/tools/render-test/render-gl.cpp @@ -84,7 +84,7 @@ class GLRenderer : 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& descIn, const void* initData) override; virtual SlangResult captureScreenSurface(Surface& surfaceOut) override; virtual InputLayout* createInputLayout(const InputElementDesc* inputElements, UInt inputElementCount) override; @@ -164,8 +164,8 @@ class GLRenderer : public Renderer, public ShaderCompiler public: typedef TextureResource Parent; - TextureResourceImpl(Type type, Usage initialUsage, const Desc& desc, GLRenderer* renderer): - Parent(type, desc), + TextureResourceImpl(Usage initialUsage, const Desc& desc, GLRenderer* renderer): + Parent(desc), m_initialUsage(initialUsage), m_renderer(renderer) { @@ -190,7 +190,6 @@ class GLRenderer : public Renderer, public ShaderCompiler struct BindingDetail { GLuint m_samplerHandle = 0; - int m_firstBinding; //< Holds binding index if not sampler (which has multiple, and can be read from the BindingState::Desc) }; class BindingStateImpl: public BindingState @@ -598,10 +597,10 @@ ShaderCompiler* GLRenderer::getShaderCompiler() return this; } -TextureResource* GLRenderer::createTextureResource(Resource::Type type, Resource::Usage initialUsage, const TextureResource::Desc& descIn, const TextureResource::Data* initData) +TextureResource* GLRenderer::createTextureResource(Resource::Usage initialUsage, const TextureResource::Desc& descIn, const TextureResource::Data* initData) { TextureResource::Desc srcDesc(descIn); - srcDesc.setDefaults(type, initialUsage); + srcDesc.setDefaults(initialUsage); GlPixelFormat pixelFormat = _getGlPixelFormat(srcDesc.format); if (pixelFormat == GlPixelFormat::Unknown) @@ -615,13 +614,13 @@ TextureResource* GLRenderer::createTextureResource(Resource::Type type, Resource const GLenum format = info.format; const GLenum formatType = info.formatType; - RefPtr texture(new TextureResourceImpl(type, initialUsage, srcDesc, this)); + RefPtr texture(new TextureResourceImpl(initialUsage, srcDesc, this)); GLenum target = 0; GLuint handle = 0; glGenTextures(1, &handle); - const int effectiveArraySize = srcDesc.calcEffectiveArraySize(type); + const int effectiveArraySize = srcDesc.calcEffectiveArraySize(); assert(initData); assert(initData->numSubResources == srcDesc.numMipLevels * srcDesc.size.depth * effectiveArraySize); @@ -630,7 +629,7 @@ TextureResource* GLRenderer::createTextureResource(Resource::Type type, Resource texture->m_handle = handle; const void*const*const data = initData->subResources; - switch (type) + switch (srcDesc.type) { case Resource::Type::Texture1D: { @@ -664,7 +663,7 @@ TextureResource* GLRenderer::createTextureResource(Resource::Type type, Resource { if (srcDesc.arraySize > 0) { - if (type == Resource::Type::TextureCube) + if (srcDesc.type == Resource::Type::TextureCube) { target = GL_TEXTURE_CUBE_MAP_ARRAY; } @@ -686,7 +685,7 @@ TextureResource* GLRenderer::createTextureResource(Resource::Type type, Resource } else { - if (type == Resource::Type::TextureCube) + if (srcDesc.type == Resource::Type::TextureCube) { target = GL_TEXTURE_CUBE_MAP; glBindTexture(target, handle); @@ -893,8 +892,6 @@ BindingState* GLRenderer::createBindingState(const BindingState::Desc& bindingSt auto& dstDetail = dstDetails[i]; const auto& srcBinding = srcBindings[i]; - // Copy over the bindings - dstDetail.m_firstBinding = bindingStateDesc.getFirst(BindingState::ShaderStyle::Glsl, srcBinding.shaderBindSet); switch (srcBinding.bindingType) { @@ -973,16 +970,17 @@ void GLRenderer::setBindingState(BindingState* stateIn) { case BindingType::Buffer: { + const int bindingIndex = binding.registerRange.getSingleIndex(); + BufferResourceImpl* buffer = static_cast(binding.resource.Ptr()); - glBindBufferBase(buffer->m_target, detail.m_firstBinding, buffer->m_handle); + glBindBufferBase(buffer->m_target, bindingIndex, buffer->m_handle); break; } case BindingType::Sampler: { - auto bindings = bindingDesc.asSlice(BindingState::ShaderStyle::Glsl, binding.shaderBindSet); - for (auto b : bindings) + for (int index = binding.registerRange.index; index < binding.registerRange.index + binding.registerRange.size; ++index) { - glBindSampler(b, detail.m_samplerHandle); + glBindSampler(index, detail.m_samplerHandle); } break; } @@ -991,7 +989,9 @@ void GLRenderer::setBindingState(BindingState* stateIn) { BufferResourceImpl* buffer = static_cast(binding.resource.Ptr()); - glActiveTexture(GL_TEXTURE0 + detail.m_firstBinding); + const int bindingIndex = binding.registerRange.getSingleIndex(); + + glActiveTexture(GL_TEXTURE0 + bindingIndex); glBindTexture(buffer->m_target, buffer->m_handle); break; } diff --git a/tools/render-test/render-vk.cpp b/tools/render-test/render-vk.cpp index ba517be687..5be1f5684a 100644 --- a/tools/render-test/render-vk.cpp +++ b/tools/render-test/render-vk.cpp @@ -39,7 +39,7 @@ class VKRenderer : 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& surface) override; virtual InputLayout* createInputLayout(const InputElementDesc* inputElements, UInt inputElementCount) override; @@ -129,8 +129,8 @@ class VKRenderer : public Renderer, public ShaderCompiler public: typedef TextureResource Parent; - TextureResourceImpl(Type type, const Desc& desc, Usage initialUsage, const VulkanApi* api) : - Parent(type, desc), + TextureResourceImpl(const Desc& desc, Usage initialUsage, const VulkanApi* api) : + Parent(desc), m_initialUsage(initialUsage), m_api(api) { @@ -180,7 +180,6 @@ class VKRenderer : public Renderer, public ShaderCompiler VkImageView m_srv = VK_NULL_HANDLE; VkBufferView m_uav = VK_NULL_HANDLE; VkSampler m_sampler = VK_NULL_HANDLE; - int m_binding = 0; }; class BindingStateImpl: public BindingState @@ -416,7 +415,7 @@ Slang::Result VKRenderer::_createPipeline(RefPtr& pipelineOut) const auto& srcBinding = srcBindings[i]; VkDescriptorSetLayoutBinding dstBinding = {}; - dstBinding.binding = srcDetail.m_binding; + dstBinding.descriptorCount = 1; switch (srcBinding.bindingType) @@ -551,10 +550,12 @@ Slang::Result VKRenderer::_createPipeline(RefPtr& pipelineOut) const auto& srcDetail = srcDetails[i]; const auto& srcBinding = srcBindings[i]; + const int bindingIndex = srcBinding.registerRange.getSingleIndex(); + VkWriteDescriptorSet writeInfo = { VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET }; writeInfo.descriptorCount = 1; writeInfo.dstSet = pipeline->m_descriptorSet; - writeInfo.dstBinding = srcDetail.m_binding; + writeInfo.dstBinding = bindingIndex; writeInfo.dstArrayElement = 0; switch (srcBinding.bindingType) @@ -1346,10 +1347,10 @@ void VKRenderer::_transitionImageLayout(VkImage image, VkFormat format, const Te m_api.vkCmdPipelineBarrier(commandBuffer, sourceStage, destinationStage, 0, 0, nullptr, 0, nullptr, 1, &barrier); } -TextureResource* VKRenderer::createTextureResource(Resource::Type type, Resource::Usage initialUsage, const TextureResource::Desc& descIn, const TextureResource::Data* initData) +TextureResource* VKRenderer::createTextureResource(Resource::Usage initialUsage, const TextureResource::Desc& descIn, const TextureResource::Data* initData) { TextureResource::Desc desc(descIn); - desc.setDefaults(type, initialUsage); + desc.setDefaults(initialUsage); const VkFormat format = VulkanUtil::getVkFormat(desc.format); if (format == VK_FORMAT_UNDEFINED) @@ -1358,15 +1359,15 @@ TextureResource* VKRenderer::createTextureResource(Resource::Type type, Resource return nullptr; } - const int arraySize = desc.calcEffectiveArraySize(type); + const int arraySize = desc.calcEffectiveArraySize(); - RefPtr texture(new TextureResourceImpl(type, desc, initialUsage, &m_api)); + RefPtr texture(new TextureResourceImpl(desc, initialUsage, &m_api)); // Create the image { VkImageCreateInfo imageInfo = {VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO}; - switch (type) + switch (desc.type) { case Resource::Type::Texture1D: { @@ -1872,9 +1873,6 @@ BindingState* VKRenderer::createBindingState(const BindingState::Desc& bindingSt auto& dstDetail = dstDetails[i]; const auto& srcBinding = srcBindings[i]; - // For now use Glsl binding - dstDetail.m_binding = bindingStateDesc.getFirst(BindingState::ShaderStyle::Glsl, srcBinding.shaderBindSet); - switch (srcBinding.bindingType) { case BindingType::Buffer: diff --git a/tools/render-test/render.cpp b/tools/render-test/render.cpp index d366413cac..b22a41ca72 100644 --- a/tools/render-test/render.cpp +++ b/tools/render-test/render.cpp @@ -59,15 +59,24 @@ const Resource::DescBase& Resource::getDescBase() const uint8_t(sizeof(uint32_t)), // D_Unorm24_S8, }; +/* static */const BindingStyle RendererUtil::s_rendererTypeToBindingStyle[] = +{ + BindingStyle::Unknown, // Unknown, + BindingStyle::DirectX, // DirectX11, + BindingStyle::DirectX, // DirectX12, + BindingStyle::OpenGl, // OpenGl, + BindingStyle::Vulkan, // Vulkan +}; /* static */void RendererUtil::compileTimeAsserts() { SLANG_COMPILE_TIME_ASSERT(SLANG_COUNT_OF(s_formatSize) == int(Format::CountOf)); + SLANG_COMPILE_TIME_ASSERT(SLANG_COUNT_OF(s_rendererTypeToBindingStyle) == int(RendererType::CountOf)); } /* !!!!!!!!!!!!!!!!!!!!!!!!!!! BindingState::Desc !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ -void BindingState::Desc::addSampler(const SamplerDesc& desc, const ShaderBindSet& shaderBindSet) +void BindingState::Desc::addSampler(const SamplerDesc& desc, const RegisterRange& registerRange) { int descIndex = int(m_samplerDescs.Count()); m_samplerDescs.Add(desc); @@ -75,13 +84,13 @@ void BindingState::Desc::addSampler(const SamplerDesc& desc, const ShaderBindSet Binding binding; binding.bindingType = BindingType::Sampler; binding.resource = nullptr; - binding.shaderBindSet = shaderBindSet; + binding.registerRange = registerRange; binding.descIndex = descIndex; m_bindings.Add(binding); } -void BindingState::Desc::addResource(BindingType bindingType, Resource* resource, const ShaderBindSet& shaderBindSet) +void BindingState::Desc::addResource(BindingType bindingType, Resource* resource, const RegisterRange& registerRange) { assert(resource); @@ -89,11 +98,11 @@ void BindingState::Desc::addResource(BindingType bindingType, Resource* resource binding.bindingType = bindingType; binding.resource = resource; binding.descIndex = -1; - binding.shaderBindSet = shaderBindSet; + binding.registerRange = registerRange; m_bindings.Add(binding); } -void BindingState::Desc::addCombinedTextureSampler(TextureResource* resource, const SamplerDesc& samplerDesc, const ShaderBindSet& shaderBindSet) +void BindingState::Desc::addCombinedTextureSampler(TextureResource* resource, const SamplerDesc& samplerDesc, const RegisterRange& registerRange) { assert(resource); @@ -104,81 +113,18 @@ void BindingState::Desc::addCombinedTextureSampler(TextureResource* resource, co binding.bindingType = BindingType::CombinedTextureSampler; binding.resource = resource; binding.descIndex = samplerDescIndex; - binding.shaderBindSet = shaderBindSet; + binding.registerRange = registerRange; m_bindings.Add(binding); } -BindingState::CompactBindIndexSlice BindingState::Desc::makeCompactSlice(int index) -{ - if (index < 0) - { - return CompactBindIndexSlice(); - } - return CompactBindIndexSlice(index, 1); -} - -BindingState::CompactBindIndexSlice BindingState::Desc::makeCompactSlice(const int* srcIndices, int numIndices) -{ - assert(numIndices >= 0); - switch (numIndices) - { - case 0: return CompactBindIndexSlice(); - case 1: return CompactBindIndexSlice(srcIndices[0], 1); - default: - { - int startIndex = int(m_sharedBindIndices.Count()); - m_sharedBindIndices.SetSize(startIndex + numIndices); - uint16_t* dstIndices = m_sharedBindIndices.Buffer() + startIndex; - for (int i = 0; i < numIndices; i++) - { - assert(srcIndices[i] >= 0); - dstIndices[i] = uint16_t(srcIndices[i]); - } - return CompactBindIndexSlice(startIndex, numIndices); - } - } -} - -int BindingState::Desc::getFirst(const CompactBindIndexSlice& set) const -{ - switch (set.m_size) - { - case 0: return -1; - case 1: return set.m_indexOrBase; - default: return m_sharedBindIndices[set.m_indexOrBase]; - } -} - -int BindingState::Desc::getFirst(ShaderStyle style, const ShaderBindSet& shaderBindSet) const -{ - return getFirst(shaderBindSet.shaderSlices[int(style)]); -} - void BindingState::Desc::clear() { m_bindings.Clear(); m_samplerDescs.Clear(); - m_sharedBindIndices.Clear(); m_numRenderTargets = 1; } -BindingState::BindIndexSlice BindingState::Desc::asSlice(const CompactBindIndexSlice& compactSlice) const -{ - switch (compactSlice.m_size) - { - case 0: return BindIndexSlice{ nullptr, 0 }; - case 1: return BindIndexSlice{ &compactSlice.m_indexOrBase, 1 }; - default: return BindIndexSlice{ m_sharedBindIndices.Buffer() + compactSlice.m_indexOrBase, compactSlice.m_size }; - } -} - -BindingState::BindIndexSlice BindingState::Desc::asSlice(ShaderStyle style, const ShaderBindSet& shaderBindSet) const -{ - return asSlice(shaderBindSet.shaderSlices[int(style)]); -} - - -int BindingState::Desc::findBindingIndex(Resource::BindFlag::Enum bindFlag, ShaderStyleFlags shaderStyleFlags, BindIndex index) const +int BindingState::Desc::findBindingIndex(Resource::BindFlag::Enum bindFlag, int registerIndex) const { const int numBindings = int(m_bindings.Count()); for (int i = 0; i < numBindings; ++i) @@ -186,18 +132,16 @@ int BindingState::Desc::findBindingIndex(Resource::BindFlag::Enum bindFlag, Shad const Binding& binding = m_bindings[i]; if (binding.resource && (binding.resource->getDescBase().bindFlags & bindFlag) != 0) { - for (int j = 0; j < int(ShaderStyle::CountOf); ++j) + if (binding.registerRange.hasRegister(registerIndex)) { - if (indexOf(binding.shaderBindSet.shaderSlices[j], index) >= 0) - { - return i; - } + return i; } } } return -1; } + /* !!!!!!!!!!!!!!!!!!!!!!!!!!! TextureResource::Size !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ int TextureResource::Size::calcMaxDimension(Type type) const @@ -236,15 +180,15 @@ void BufferResource::Desc::setDefaults(Usage initialUsage) /* !!!!!!!!!!!!!!!!!!!!!!!!! TextureResource::Desc !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ -int TextureResource::Desc::calcNumMipLevels(Type type) const +int TextureResource::Desc::calcNumMipLevels() const { const int maxDimensionSize = this->size.calcMaxDimension(type); return (maxDimensionSize > 0) ? (Math::Log2Floor(maxDimensionSize) + 1) : 0; } -int TextureResource::Desc::calcNumSubResources(Type type) const +int TextureResource::Desc::calcNumSubResources() const { - const int numMipMaps = (this->numMipLevels > 0) ? this->numMipLevels : calcNumMipLevels(type); + const int numMipMaps = (this->numMipLevels > 0) ? this->numMipLevels : calcNumMipLevels(); const int arrSize = (this->arraySize > 0) ? this->arraySize : 1; switch (type) @@ -269,7 +213,7 @@ int TextureResource::Desc::calcNumSubResources(Type type) const } } -void TextureResource::Desc::fixSize(Type type) +void TextureResource::Desc::fixSize() { switch (type) { @@ -295,20 +239,20 @@ void TextureResource::Desc::fixSize(Type type) } } -void TextureResource::Desc::setDefaults(Type type, Usage initialUsage) +void TextureResource::Desc::setDefaults(Usage initialUsage) { - fixSize(type); + fixSize(); if (this->bindFlags == 0) { this->bindFlags = Resource::s_requiredBinding[int(initialUsage)]; } if (this->numMipLevels <= 0) { - this->numMipLevels = calcNumMipLevels(type); + this->numMipLevels = calcNumMipLevels(); } } -int TextureResource::Desc::calcEffectiveArraySize(Type type) const +int TextureResource::Desc::calcEffectiveArraySize() const { const int arrSize = (this->arraySize > 0) ? this->arraySize : 1; @@ -325,8 +269,9 @@ int TextureResource::Desc::calcEffectiveArraySize(Type type) const } } -void TextureResource::Desc::init() +void TextureResource::Desc::init(Type typeIn) { + this->type = typeIn; this->size.init(); this->format = Format::Unknown; @@ -340,6 +285,7 @@ void TextureResource::Desc::init() void TextureResource::Desc::init1D(Format formatIn, int widthIn, int numMipMapsIn) { + this->type = Type::Texture1D; this->size.init(widthIn); this->format = format; @@ -351,8 +297,11 @@ void TextureResource::Desc::init1D(Format formatIn, int widthIn, int numMipMapsI this->cpuAccessFlags = 0; } -void TextureResource::Desc::init2D(Format formatIn, int widthIn, int heightIn, int numMipMapsIn) +void TextureResource::Desc::init2D(Type typeIn, Format formatIn, int widthIn, int heightIn, int numMipMapsIn) { + assert(typeIn == Type::Texture2D || typeIn == Type::TextureCube); + + this->type = type; this->size.init(widthIn, heightIn); this->format = format; @@ -366,6 +315,7 @@ void TextureResource::Desc::init2D(Format formatIn, int widthIn, int heightIn, i void TextureResource::Desc::init3D(Format formatIn, int widthIn, int heightIn, int depthIn, int numMipMapsIn) { + this->type = Type::Texture3D; this->size.init(widthIn, heightIn, depthIn); this->format = format; diff --git a/tools/render-test/render.h b/tools/render-test/render.h index f664547122..9dd87437d5 100644 --- a/tools/render-test/render.h +++ b/tools/render-test/render.h @@ -51,6 +51,16 @@ enum class ProjectionStyle CountOf, }; +/// The style of the binding +enum class BindingStyle +{ + Unknown, + DirectX, + OpenGl, + Vulkan, + CountOf, +}; + class ShaderProgram: public Slang::RefObject { public: @@ -200,6 +210,8 @@ class Resource: public Slang::RefObject bool canBind(BindFlag::Enum bindFlag) const { return (bindFlags & bindFlag) != 0; } bool hasCpuAccessFlag(AccessFlag::Enum accessFlag) { return (cpuAccessFlags & accessFlag) != 0; } + Type type = Type::Unknown; + int bindFlags = 0; ///< Combination of Resource::BindFlag or 0 (and will use initialUsage to set) int cpuAccessFlags = 0; ///< Combination of Resource::AccessFlag }; @@ -303,28 +315,28 @@ class TextureResource: public Resource struct Desc: public DescBase { /// Initialize with default values - void init(); + void init(Type typeIn); /// Initialize different dimensions. For cubemap, use init2D void init1D(Format format, int width, int numMipMaps = 0); - void init2D(Format format, int width, int height, int numMipMaps = 0); + void init2D(Type typeIn, Format format, int width, int height, int numMipMaps = 0); void init3D(Format format, int width, int height, int depth, int numMipMaps = 0); /// Given the type, calculates the number of mip maps. 0 on error - int calcNumMipLevels(Type type) const; + int calcNumMipLevels() const; /// Calculate the total number of sub resources. 0 on error. - int calcNumSubResources(Type type) const; + int calcNumSubResources() const; /// Calculate the effective array size - in essence the amount if mip map sets needed. /// In practice takes into account if the arraySize is 0 (it's not an array, but it will still have at least one mip set) /// and if the type is a cubemap (multiplies the amount of mip sets by 6) - int calcEffectiveArraySize(Type type) const; + int calcEffectiveArraySize() const; /// Use type to fix the size values (and array size). /// For example a 1d texture, should have height and depth set to 1. - void fixSize(Type type); + void fixSize(); /// Set up default parameters based on type and usage - void setDefaults(Type type, Usage initialUsage); + void setDefaults(Usage initialUsage); Size size; @@ -351,8 +363,8 @@ class TextureResource: public Resource SLANG_FORCE_INLINE const Desc& getDesc() const { return m_desc; } /// Ctor - TextureResource(Type type, const Desc& desc): - Parent(type), + TextureResource(const Desc& desc): + Parent(desc.type), m_desc(desc) { } @@ -380,90 +392,29 @@ enum class BindingType class BindingState : public Slang::RefObject { public: - - typedef uint16_t BindIndex; - - /// Shader binding style - enum class ShaderStyle - { - Hlsl, - Glsl, - CountOf, - }; - - struct ShaderStyleFlag - { - enum Enum - { - Hlsl = 1 << int(ShaderStyle::Hlsl), - Glsl = 1 << int(ShaderStyle::Glsl), - }; - }; - typedef int ShaderStyleFlags; ///< Combination of ShaderStyleFlag - - /// A 'compact' representation of a 0 or more BindIndices. - /// A Slice in this context is effectively an unowned array. - /// If only a single index is he held (which is common) it's held directly in the m_indexOrBase member, otherwise m_indexOrBase is an index into the - /// m_indices list of the Desc. Can be turned into a BindIndexSlice (which is easier to use, and iterable) using asBindIndexSlice method on Desc - struct CompactBindIndexSlice - { - typedef uint16_t SizeType; - /// Default Ctor makes an empty set - SLANG_FORCE_INLINE CompactBindIndexSlice() : - m_size(0), - m_indexOrBase(0) - {} - /// Ctor for one or more. NOTE! Meaning if indexIn changes depending if numIndices > 1. - SLANG_FORCE_INLINE CompactBindIndexSlice(int indexIn, int sizeIn) : - m_size(SizeType(sizeIn)), - m_indexOrBase(BindIndex(indexIn)) - { - } - SizeType m_size; - BindIndex m_indexOrBase; ///< Meaning changes depending on numIndices. If 1, it is the index if larger than 1, then is an index into 'indices' - }; - - /// Holds the BindIndex slice associated with each ShaderStyle - struct ShaderBindSet + /// A register set consists of one or more contiguous indices. + /// To be valid index >= 0 and size >= 1 + struct RegisterRange { - void set(ShaderStyle style, const CompactBindIndexSlice& slice) { shaderSlices[int(style)] = slice; } - void setAll(const CompactBindIndexSlice& slice) - { - for (int i = 0; i < int(ShaderStyle::CountOf); ++i) - { - shaderSlices[i] = slice; - } - } - - CompactBindIndexSlice shaderSlices[int(ShaderStyle::CountOf)]; + /// True if contains valid contents + bool isValid() const { return size > 0; } + /// True if valid single value + bool isSingle() const { return size == 1; } + /// Get as a single index (must be at least one index) + int getSingleIndex() const { return (size == 1) ? index : -1; } + /// Return the first index + int getFirstIndex() const { return (size > 0) ? index : -1; } + /// True if contains register index + bool hasRegister(int registerIndex) const { return registerIndex >= index && registerIndex < index + size; } + + static RegisterRange makeInvalid() { return RegisterRange{ -1, 0 }; } + static RegisterRange makeSingle(int index) { return RegisterRange{ int16_t(index), 1 }; } + static RegisterRange makeRange(int index, int size) { return RegisterRange{ int16_t(index), uint16_t(size) }; } + + int16_t index; ///< The base index + uint16_t size; ///< The amount of register indices }; - - /// A slice (non owned array) of BindIndices - /// TODO: have a generic Slice type instead of this specific type - struct BindIndexSlice - { - const BindIndex* begin() const { return data; } - const BindIndex* end() const { return data + size; } - - int indexOf(BindIndex index) const - { - for (int i = 0; i < size; ++i) - { - if (data[i] == index) - { - return i; - } - } - return -1; - } - - int getSize() const { return int(size); } - BindIndex operator[](int i) const { assert(i >= 0 && i < size); return data[i]; } - - const BindIndex* data; - int size; - }; - + struct SamplerDesc { bool isCompareSampler; @@ -474,50 +425,31 @@ class BindingState : public Slang::RefObject BindingType bindingType; ///< Type of binding int descIndex; ///< The description index associated with type. -1 if not used. For example if bindingType is Sampler, the descIndex is into m_samplerDescs. Slang::RefPtr resource; ///< Associated resource. nullptr if not used - ShaderBindSet shaderBindSet; ///< Holds BindIndices associated with each ShaderStyle + RegisterRange registerRange; /// Defines the registers for binding }; struct Desc { - /// Given a RegisterSet, return as a RegisterList, that can be easily iterated over - BindIndexSlice asSlice(const CompactBindIndexSlice& set) const; - /// Given a RegisterDesc and a style returns a RegisterList, that can be easily iterated over - BindIndexSlice asSlice(ShaderStyle style, const ShaderBindSet& shaderBindSet) const; - - /// Returns the first member of the set, or returns -1 if is empty - int getFirst(const CompactBindIndexSlice& set) const; - /// Returns the first member of the set, or returns -1 if is empty - int getFirst(ShaderStyle style, const ShaderBindSet& shaderBindSet) const; - /// Add a resource - assumed that the binding will match the Desc of the resource - void addResource(BindingType bindingType, Resource* resource, const ShaderBindSet& shaderBindSet); + void addResource(BindingType bindingType, Resource* resource, const RegisterRange& registerRange); /// Add a sampler - void addSampler(const SamplerDesc& desc, const ShaderBindSet& shaderBindSet); + void addSampler(const SamplerDesc& desc, const RegisterRange& registerRange); /// Add a BufferResource - void addBufferResource(BufferResource* resource, const ShaderBindSet& shaderBindSet) { addResource(BindingType::Buffer, resource, shaderBindSet); } + void addBufferResource(BufferResource* resource, const RegisterRange& registerRange) { addResource(BindingType::Buffer, resource, registerRange); } /// Add a texture - void addTextureResource(TextureResource* resource, const ShaderBindSet& shaderBindSet) { addResource(BindingType::Texture, resource, shaderBindSet); } + void addTextureResource(TextureResource* resource, const RegisterRange& registerRange) { addResource(BindingType::Texture, resource, registerRange); } /// Add combined texture a - void addCombinedTextureSampler(TextureResource* resource, const SamplerDesc& samplerDesc, const ShaderBindSet& shaderBindSet); + void addCombinedTextureSampler(TextureResource* resource, const SamplerDesc& samplerDesc, const RegisterRange& registerRange); + + /// Returns the bind index, that has the bind flag, and indexes the specified register + int findBindingIndex(Resource::BindFlag::Enum bindFlag, int registerIndex) const; /// Clear the contents void clear(); - /// Given an index, makes a CompactBindIndexSlice. If index is < 0, assumes means no indices, and just returns the empty slice - CompactBindIndexSlice makeCompactSlice(int index); - /// Given a list of indices, makes a CompactBindIndexSlice. Note does check for indices being unique and the order is maintained. - /// Only >= 0 indices are valid - CompactBindIndexSlice makeCompactSlice(const int* indices, int numIndices); - - /// Returns the index of the element in the slice - int indexOf(const CompactBindIndexSlice& slice, BindIndex index) const { return asSlice(slice).indexOf(index); } - - /// Find the - int findBindingIndex(Resource::BindFlag::Enum bindFlag, ShaderStyleFlags shaderStyleFlags, BindIndex index) const; - Slang::List m_bindings; ///< All of the bindings in order Slang::List m_samplerDescs; ///< Holds the SamplerDesc for the binding - indexed by the descIndex member of Binding - Slang::List m_sharedBindIndices; ///< Used to store BindIndex slices that don't fit into CompactBindIndexSlice + int m_numRenderTargets = 1; }; @@ -551,7 +483,7 @@ class Renderer: public Slang::RefObject virtual void presentFrame() = 0; /// Create a texture resource. initData holds the initialize data to set the contents of the texture when constructed. - virtual TextureResource* createTextureResource(Resource::Type type, Resource::Usage initialUsage, const TextureResource::Desc& desc, const TextureResource::Data* initData = nullptr) { return nullptr; } + virtual TextureResource* createTextureResource(Resource::Usage initialUsage, const TextureResource::Desc& desc, const TextureResource::Data* initData = nullptr) { return nullptr; } /// Create a buffer resource virtual BufferResource* createBufferResource(Resource::Usage initialUsage, const BufferResource::Desc& desc, const void* initData = nullptr) { return nullptr; } @@ -604,9 +536,13 @@ struct RendererUtil /// Given the projection style returns an 'identity' matrix, which ensures x,y mapping to pixels is the same on all targets static void getIdentityProjection(ProjectionStyle style, float projMatrix[16]); + /// Get the binding style from the type + static BindingStyle getBindingStyle(RendererType type) { return s_rendererTypeToBindingStyle[int(type)]; } + private: static void compileTimeAsserts(); static const uint8_t s_formatSize[]; // Maps Format::XXX to a size in bytes; + static const BindingStyle s_rendererTypeToBindingStyle[]; ///< Maps a RendererType to a BindingStyle }; } // renderer_test diff --git a/tools/render-test/shader-renderer-util.cpp b/tools/render-test/shader-renderer-util.cpp index c0cf982c98..6e11757fb9 100644 --- a/tools/render-test/shader-renderer-util.cpp +++ b/tools/render-test/shader-renderer-util.cpp @@ -16,7 +16,7 @@ using namespace Slang; /* static */Result ShaderRendererUtil::createTextureResource(const InputTextureDesc& inputDesc, const TextureData& texData, int bindFlags, Renderer* renderer, RefPtr& textureOut) { TextureResource::Desc textureResourceDesc; - textureResourceDesc.init(); + textureResourceDesc.init(Resource::Type::Unknown); textureResourceDesc.format = Format::RGBA_Unorm_UInt8; textureResourceDesc.numMipLevels = texData.mipLevels; @@ -24,31 +24,30 @@ using namespace Slang; textureResourceDesc.bindFlags = bindFlags; // It's the same size in all dimensions - Resource::Type type = Resource::Type::Unknown; switch (inputDesc.dimension) { case 1: { - type = Resource::Type::Texture1D; + textureResourceDesc.type = Resource::Type::Texture1D; textureResourceDesc.size.init(inputDesc.size); break; } case 2: { - type = inputDesc.isCube ? Resource::Type::TextureCube : Resource::Type::Texture2D; + textureResourceDesc.type = inputDesc.isCube ? Resource::Type::TextureCube : Resource::Type::Texture2D; textureResourceDesc.size.init(inputDesc.size, inputDesc.size); break; } case 3: { - type = Resource::Type::Texture3D; + textureResourceDesc.type = Resource::Type::Texture3D; textureResourceDesc.size.init(inputDesc.size, inputDesc.size, inputDesc.size); break; } } - const int effectiveArraySize = textureResourceDesc.calcEffectiveArraySize(type); - const int numSubResources = textureResourceDesc.calcNumSubResources(type); + const int effectiveArraySize = textureResourceDesc.calcEffectiveArraySize(); + const int numSubResources = textureResourceDesc.calcNumSubResources(); Resource::Usage initialUsage = Resource::Usage::GenericRead; TextureResource::Data initData; @@ -82,7 +81,7 @@ using namespace Slang; initData.numSubResources = numSubResources; initData.subResources = subResources.Buffer(); - textureOut = renderer->createTextureResource(type, Resource::Usage::GenericRead, textureResourceDesc, &initData); + textureOut = renderer->createTextureResource(Resource::Usage::GenericRead, textureResourceDesc, &initData); return textureOut ? SLANG_OK : SLANG_FAIL; } @@ -132,6 +131,53 @@ static BindingState::SamplerDesc _calcSamplerDesc(const InputSamplerDesc& srcDes return dstDesc; } +/* static */BindingState::RegisterRange ShaderRendererUtil::calcRegisterRange(Renderer* renderer, const ShaderInputLayoutEntry& entry) +{ + typedef BindingState::RegisterRange RegisterRange; + + BindingStyle bindingStyle = RendererUtil::getBindingStyle(renderer->getRendererType()); + + switch (bindingStyle) + { + case BindingStyle::DirectX: + { + return RegisterRange::makeSingle(entry.hlslBinding); + } + case BindingStyle::Vulkan: + { + // USe OpenGls for now + // fallthru + } + case BindingStyle::OpenGl: + { + const int count = int(entry.glslBinding.Count()); + + if (count <= 0) + { + break; + } + + int baseIndex = entry.glslBinding[0]; + // Make sure they are contiguous + for (int i = 1; i < int(entry.glslBinding.Count()); ++i) + { + if (baseIndex + i != entry.glslBinding[i]) + { + assert("Bindings must be contiguous"); + break; + } + } + return RegisterRange::makeRange(baseIndex, count); + } + /* case BindingStyle::Vulkan: + { + } */ + default: break; + } + // Return invalid + return RegisterRange::makeInvalid(); +} + /* static */Result ShaderRendererUtil::createBindingStateDesc(ShaderInputLayoutEntry* srcEntries, int numEntries, Renderer* renderer, BindingState::Desc& descOut) { const int textureBindFlags = Resource::BindFlag::NonPixelShaderResource | Resource::BindFlag::PixelShaderResource; @@ -141,9 +187,12 @@ static BindingState::SamplerDesc _calcSamplerDesc(const InputSamplerDesc& srcDes { const ShaderInputLayoutEntry& srcEntry = srcEntries[i]; - BindingState::ShaderBindSet shaderBindSet; - shaderBindSet.set(BindingState::ShaderStyle::Hlsl, descOut.makeCompactSlice(srcEntry.hlslBinding)); - shaderBindSet.set(BindingState::ShaderStyle::Glsl, descOut.makeCompactSlice(srcEntry.glslBinding.Buffer(), int(srcEntry.glslBinding.Count()))); + const BindingState::RegisterRange registerSet = calcRegisterRange(renderer, srcEntry); + if (!registerSet.isValid()) + { + assert(!"Couldn't find a binding"); + return SLANG_FAIL; + } switch (srcEntry.type) { @@ -156,14 +205,14 @@ static BindingState::SamplerDesc _calcSamplerDesc(const InputSamplerDesc& srcDes RefPtr bufferResource; SLANG_RETURN_ON_FAIL(createBufferResource(srcEntry.bufferDesc, srcEntry.isOutput, bufferSize, srcEntry.bufferData.Buffer(), renderer, bufferResource)); - descOut.addBufferResource(bufferResource, shaderBindSet); + descOut.addBufferResource(bufferResource, registerSet); break; } case ShaderInputType::CombinedTextureSampler: { RefPtr texture; SLANG_RETURN_ON_FAIL(generateTextureResource(srcEntry.textureDesc, textureBindFlags, renderer, texture)); - descOut.addCombinedTextureSampler(texture, _calcSamplerDesc(srcEntry.samplerDesc), shaderBindSet); + descOut.addCombinedTextureSampler(texture, _calcSamplerDesc(srcEntry.samplerDesc), registerSet); break; } case ShaderInputType::Texture: @@ -171,12 +220,12 @@ static BindingState::SamplerDesc _calcSamplerDesc(const InputSamplerDesc& srcDes RefPtr texture; SLANG_RETURN_ON_FAIL(generateTextureResource(srcEntry.textureDesc, textureBindFlags, renderer, texture)); - descOut.addTextureResource(texture, shaderBindSet); + descOut.addTextureResource(texture, registerSet); break; } case ShaderInputType::Sampler: { - descOut.addSampler(_calcSamplerDesc(srcEntry.samplerDesc), shaderBindSet); + descOut.addSampler(_calcSamplerDesc(srcEntry.samplerDesc), registerSet); break; } default: diff --git a/tools/render-test/shader-renderer-util.h b/tools/render-test/shader-renderer-util.h index b481d52dbb..849e68754b 100644 --- a/tools/render-test/shader-renderer-util.h +++ b/tools/render-test/shader-renderer-util.h @@ -22,6 +22,10 @@ struct ShaderRendererUtil static Slang::Result createBindingStateDesc(const ShaderInputLayout& layout, Renderer* renderer, BindingState::Desc& descOut); /// Create BindingState::Desc from a list of ShaderInputLayout entries static Slang::Result createBindingStateDesc(ShaderInputLayoutEntry* srcEntries, int numEntries, Renderer* renderer, BindingState::Desc& descOut); + + /// Get the binding register associated with this binding (or -1 if none defined) + static BindingState::RegisterRange calcRegisterRange(Renderer* renderer, const ShaderInputLayoutEntry& entry); + }; } // renderer_test