From 1a698128c15bce0c05b0664bb1458842e1e55511 Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Tue, 5 Jun 2018 21:35:48 -0700 Subject: Fix atomic operations on RWBuffer (#593) * Fix atomic operations on RWBuffer An earlier change added support for passing true pointers to `__ref` parameters to fix the global `Interlocked*()` functions when applied to `groupshared` variables or `RWStructureBuffer` elements. That change didn't apply to `RWBuffer` or `RWTexture2D`, etc. because those types had so far only declared `get` and `set` accessors, but not any `ref` accessors (which return a pointer). The main fixes here are: * Add `ref` accessors to the subscript oeprations on the `RW*` resource types * Adjust the logic for emitting calls to subscript accessors so that we don't get quite as eager about invoking a `ref` accessor, and instead try to invoke just a `get` or `set` accessor when these will suffice. This is important for Vulkan cross-compilation, where we don't yet support the semantics of our `ref` accessors. * Add a test case for atomics on a `RWBuffer` * Fix up `render-test` so that we can specify a format for a buffer resource, which allows us to use things other than `*StructuredBuffer` and `*ByteAddressBuffer`. The work there is probably not complete; I just did what I could to get the test working. * A bunch of files got whitespace edits thanks to the fact that I'm using editorconfig and others on the project seemingly arent... * fixup: remove ifdefed-out code --- tools/render-test/d3d-util.cpp | 10 +- tools/render-test/render-d3d11.cpp | 69 ++++++----- tools/render-test/render-d3d12.cpp | 182 ++++++++++++++--------------- tools/render-test/render.cpp | 36 +++--- tools/render-test/render.h | 88 +++++++------- tools/render-test/shader-input-layout.cpp | 15 ++- tools/render-test/shader-input-layout.h | 7 +- tools/render-test/shader-renderer-util.cpp | 15 +-- tools/render-test/vk-util.cpp | 1 + 9 files changed, 220 insertions(+), 203 deletions(-) (limited to 'tools') diff --git a/tools/render-test/d3d-util.cpp b/tools/render-test/d3d-util.cpp index fb3cb49bf..bef0e6baa 100644 --- a/tools/render-test/d3d-util.cpp +++ b/tools/render-test/d3d-util.cpp @@ -31,6 +31,7 @@ using namespace Slang; case Format::RG_Float32: return DXGI_FORMAT_R32G32_FLOAT; case Format::R_Float32: return DXGI_FORMAT_R32_FLOAT; case Format::RGBA_Unorm_UInt8: return DXGI_FORMAT_R8G8B8A8_UNORM; + case Format::R_UInt32: return DXGI_FORMAT_R32_UINT; case Format::D_Float32: return DXGI_FORMAT_D32_FLOAT; case Format::D_Unorm24_S8: return DXGI_FORMAT_D24_UNORM_S8_UINT; @@ -47,7 +48,8 @@ using namespace Slang; switch (format) { case DXGI_FORMAT_R32_FLOAT: /* fallthru */ - case DXGI_FORMAT_D32_FLOAT: + case DXGI_FORMAT_R32_UINT: + case DXGI_FORMAT_D32_FLOAT: { return DXGI_FORMAT_R32_TYPELESS; } @@ -73,7 +75,7 @@ using namespace Slang; switch (format) { case DXGI_FORMAT_D32_FLOAT: /* fallthru */ - case DXGI_FORMAT_R32_TYPELESS: + case DXGI_FORMAT_R32_TYPELESS: { return DXGI_FORMAT_D32_FLOAT; } @@ -88,7 +90,7 @@ using namespace Slang; switch (format) { case DXGI_FORMAT_D32_FLOAT: /* fallthru */ - case DXGI_FORMAT_D24_UNORM_S8_UINT: + case DXGI_FORMAT_D24_UNORM_S8_UINT: { return DXGI_FORMAT_UNKNOWN; } @@ -102,7 +104,7 @@ using namespace Slang; switch (format) { case DXGI_FORMAT_D32_FLOAT: /* fallthru */ - case DXGI_FORMAT_R32_TYPELESS: + case DXGI_FORMAT_R32_TYPELESS: { return DXGI_FORMAT_R32_FLOAT; } diff --git a/tools/render-test/render-d3d11.cpp b/tools/render-test/render-d3d11.cpp index d47a4a559..0ba2c8dc3 100644 --- a/tools/render-test/render-d3d11.cpp +++ b/tools/render-test/render-d3d11.cpp @@ -61,7 +61,7 @@ public: virtual void setInputLayout(InputLayout* inputLayout) override; virtual void setPrimitiveTopology(PrimitiveTopology topology) override; virtual void setBindingState(BindingState * state); - virtual void setVertexBuffers(UInt startSlot, UInt slotCount, BufferResource*const* buffers, const UInt* strides, const UInt* offsets) override; + virtual void setVertexBuffers(UInt startSlot, UInt slotCount, BufferResource*const* buffers, const UInt* strides, const UInt* offsets) override; virtual void setShaderProgram(ShaderProgram* inProgram) override; virtual void draw(UInt vertexCount, UInt startVertex) override; virtual void dispatchCompute(int x, int y, int z) override; @@ -133,7 +133,7 @@ public: }; class InputLayoutImpl: public InputLayout - { + { public: ComPtr m_layout; }; @@ -180,7 +180,7 @@ Renderer* createD3D11Renderer() HRESULT hr = S_OK; ComPtr stagingTexture; - + if (textureDesc.Usage == D3D11_USAGE_STAGING && (textureDesc.CPUAccessFlags & D3D11_CPU_ACCESS_READ)) { stagingTexture = texture; @@ -275,7 +275,7 @@ SlangResult D3D11Renderer::initialize(const Desc& desc, void* inWindowHandle) }; D3D_FEATURE_LEVEL featureLevel = D3D_FEATURE_LEVEL_9_1; const int totalNumFeatureLevels = SLANG_COUNT_OF(featureLevels); - + // On a machine that does not have an up-to-date version of D3D installed, // the `D3D11CreateDeviceAndSwapChain` call will fail with `E_INVALIDARG` // if you ask for featuer level 11_1. The workaround is to call @@ -301,7 +301,7 @@ SlangResult D3D11Renderer::initialize(const Desc& desc, void* inWindowHandle) m_immediateContext.writeRef()); // Failures with `E_INVALIDARG` might be due to feature level 11_1 - // not being supported. + // not being supported. if (hr == E_INVALIDARG) { continue; @@ -348,7 +348,7 @@ SlangResult D3D11Renderer::initialize(const Desc& desc, void* inWindowHandle) viewport.TopLeftX = 0; viewport.TopLeftY = 0; viewport.Width = (float)desc.width; - viewport.Height = (float)desc.height; + viewport.Height = (float)desc.height; viewport.MaxDepth = 1; // TODO(tfoley): use reversed depth viewport.MinDepth = 0; m_immediateContext->RSSetViewports(1, &viewport); @@ -433,9 +433,9 @@ TextureResource* D3D11Renderer::createTextureResource(Resource::Usage initialUsa { TextureResource::Desc srcDesc(descIn); srcDesc.setDefaults(initialUsage); - + const int effectiveArraySize = srcDesc.calcEffectiveArraySize(); - + assert(initData); assert(initData->numSubResources == srcDesc.numMipLevels * effectiveArraySize * srcDesc.size.depth); @@ -484,10 +484,10 @@ TextureResource* D3D11Renderer::createTextureResource(Resource::Usage initialUsa desc.Format = format; desc.MiscFlags = 0; desc.MipLevels = srcDesc.numMipLevels; - desc.ArraySize = effectiveArraySize; - desc.Width = srcDesc.size.width; + desc.ArraySize = effectiveArraySize; + desc.Width = srcDesc.size.width; desc.Usage = D3D11_USAGE_DEFAULT; - + ComPtr texture1D; SLANG_RETURN_NULL_ON_FAIL(m_device->CreateTexture1D(&desc, subRes.Buffer(), texture1D.writeRef())); @@ -503,8 +503,8 @@ TextureResource* D3D11Renderer::createTextureResource(Resource::Usage initialUsa desc.Format = format; desc.MiscFlags = 0; desc.MipLevels = srcDesc.numMipLevels; - desc.ArraySize = effectiveArraySize; - + desc.ArraySize = effectiveArraySize; + desc.Width = srcDesc.size.width; desc.Height = srcDesc.size.height; desc.Usage = D3D11_USAGE_DEFAULT; @@ -530,11 +530,11 @@ TextureResource* D3D11Renderer::createTextureResource(Resource::Usage initialUsa desc.Format = format; desc.MiscFlags = 0; desc.MipLevels = srcDesc.numMipLevels; - desc.Width = srcDesc.size.width; + desc.Width = srcDesc.size.width; desc.Height = srcDesc.size.height; desc.Depth = srcDesc.size.depth; desc.Usage = D3D11_USAGE_DEFAULT; - + ComPtr texture3D; SLANG_RETURN_NULL_ON_FAIL(m_device->CreateTexture3D(&desc, subRes.Buffer(), texture3D.writeRef())); @@ -548,13 +548,13 @@ TextureResource* D3D11Renderer::createTextureResource(Resource::Usage initialUsa } BufferResource* D3D11Renderer::createBufferResource(Resource::Usage initialUsage, const BufferResource::Desc& descIn, const void* initData) -{ +{ BufferResource::Desc srcDesc(descIn); srcDesc.setDefaults(initialUsage); // Make aligned to 256 bytes... not sure why, but if you remove this the tests do fail. const size_t alignedSizeInBytes = D3DUtil::calcAligned(srcDesc.sizeInBytes, 256); - + // Hack to make the initialization never read from out of bounds memory, by copying into a buffer List initDataBuffer; if (initData && alignedSizeInBytes > srcDesc.sizeInBytes) @@ -608,7 +608,7 @@ BufferResource* D3D11Renderer::createBufferResource(Resource::Usage initialUsage RefPtr buffer(new BufferResourceImpl(srcDesc, initialUsage)); SLANG_RETURN_NULL_ON_FAIL(m_device->CreateBuffer(&bufferDesc, initData ? &subResourceData : nullptr, buffer->m_buffer.writeRef())); - + if (srcDesc.cpuAccessFlags & Resource::AccessFlag::Read) { D3D11_BUFFER_DESC bufDesc = {}; @@ -650,7 +650,7 @@ InputLayout* D3D11Renderer::createInputLayout(const InputElementDesc* inputEleme char const* typeName = "Unknown"; switch (inputElementsIn[ii].format) { - case Format::RGBA_Float32: + case Format::RGBA_Float32: typeName = "float4"; break; case Format::RGB_Float32: @@ -705,13 +705,13 @@ void* D3D11Renderer::map(BufferResource* bufferIn, MapFlavor flavor) break; case MapFlavor::HostRead: mapType = D3D11_MAP_READ; - + buffer = bufferResource->m_staging; if (!buffer) { return nullptr; } - + // Okay copy the data over m_immediateContext->CopyResource(buffer, bufferResource->m_buffer); @@ -746,7 +746,7 @@ void D3D11Renderer::setInputLayout(InputLayout* inputLayoutIn) void D3D11Renderer::setPrimitiveTopology(PrimitiveTopology topology) { - m_immediateContext->IASetPrimitiveTopology(D3DUtil::getPrimitiveTopology(topology)); + m_immediateContext->IASetPrimitiveTopology(D3DUtil::getPrimitiveTopology(topology)); } void D3D11Renderer::setVertexBuffers(UInt startSlot, UInt slotCount, BufferResource*const* buffersIn, const UInt* stridesIn, const UInt* offsetsIn) @@ -803,7 +803,7 @@ ShaderProgram* D3D11Renderer::compileProgram(const ShaderCompileRequest& request ComPtr vertexShaderBlob, fragmentShaderBlob; SLANG_RETURN_NULL_ON_FAIL(D3DUtil::compileHLSLShader(request.vertexShader.source.path, request.vertexShader.source.dataBegin, request.vertexShader.name, request.vertexShader.profile, vertexShaderBlob)); SLANG_RETURN_NULL_ON_FAIL(D3DUtil::compileHLSLShader(request.fragmentShader.source.path, request.fragmentShader.source.dataBegin, request.fragmentShader.name, request.fragmentShader.profile, fragmentShaderBlob)); - + ComPtr vertexShader; ComPtr pixelShader; @@ -829,7 +829,7 @@ BindingState* D3D11Renderer::createBindingState(const BindingState::Desc& bindin const auto& srcBindings = bindingStateDesc.m_bindings; const int numBindings = int(srcBindings.Count()); - + auto& dstDetails = bindingState->m_bindingDetails; dstDetails.SetSize(numBindings); @@ -837,7 +837,7 @@ BindingState* D3D11Renderer::createBindingState(const BindingState::Desc& bindin { auto& dstDetail = dstDetails[i]; const auto& srcBinding = srcBindings[i]; - + assert(srcBinding.registerRange.isSingle()); switch (srcBinding.bindingType) @@ -846,7 +846,7 @@ BindingState* D3D11Renderer::createBindingState(const BindingState::Desc& bindin { assert(srcBinding.resource && srcBinding.resource->isBuffer()); - BufferResourceImpl* buffer = static_cast(srcBinding.resource.Ptr()); + BufferResourceImpl* buffer = static_cast(srcBinding.resource.Ptr()); const BufferResource::Desc& bufferDesc = buffer->getDesc(); const int elemSize = bufferDesc.elementSize <= 0 ? 1 : bufferDesc.elementSize; @@ -859,20 +859,17 @@ BindingState* D3D11Renderer::createBindingState(const BindingState::Desc& bindin viewDesc.Buffer.NumElements = (UINT)(bufferDesc.sizeInBytes / elemSize); viewDesc.Buffer.Flags = 0; viewDesc.ViewDimension = D3D11_UAV_DIMENSION_BUFFER; - viewDesc.Format = DXGI_FORMAT_UNKNOWN; + viewDesc.Format = D3DUtil::getMapFormat(bufferDesc.format); - if (bufferDesc.elementSize == 0) + if (bufferDesc.elementSize == 0 && bufferDesc.format == Format::Unknown) { - // TODO: are there UAV cases we need to handle that are neither - // raw nor structured? RWBuffer would be one... - viewDesc.Buffer.Flags |= D3D11_BUFFER_UAV_FLAG_RAW; viewDesc.Format = DXGI_FORMAT_R32_TYPELESS; } SLANG_RETURN_NULL_ON_FAIL(m_device->CreateUnorderedAccessView(buffer->m_buffer, &viewDesc, dstDetail.m_uav.writeRef())); } - if (bufferDesc.bindFlags & (Resource::BindFlag::NonPixelShaderResource | Resource::BindFlag::PixelShaderResource)) + if (bufferDesc.bindFlags & (Resource::BindFlag::NonPixelShaderResource | Resource::BindFlag::PixelShaderResource)) { D3D11_SHADER_RESOURCE_VIEW_DESC viewDesc; memset(&viewDesc, 0, sizeof(viewDesc)); @@ -896,7 +893,7 @@ BindingState* D3D11Renderer::createBindingState(const BindingState::Desc& bindin case BindingType::CombinedTextureSampler: { assert(srcBinding.resource && srcBinding.resource->isTexture()); - + TextureResourceImpl* texture = static_cast(srcBinding.resource.Ptr()); const TextureResource::Desc& textureDesc = texture->getDesc(); @@ -1037,7 +1034,7 @@ void D3D11Renderer::_applyBindingState(bool isCompute) { ID3D11Buffer* buffer = static_cast(binding.resource.Ptr())->m_buffer; if (isCompute) - context->CSSetConstantBuffers(bindingIndex, 1, &buffer); + context->CSSetConstantBuffers(bindingIndex, 1, &buffer); else { context->VSSetConstantBuffers(bindingIndex, 1, &buffer); @@ -1064,7 +1061,7 @@ void D3D11Renderer::_applyBindingState(bool isCompute) } break; } - case BindingType::Texture: + case BindingType::Texture: { if (detail.m_uav) { @@ -1086,7 +1083,7 @@ void D3D11Renderer::_applyBindingState(bool isCompute) } break; } - case BindingType::Sampler: + case BindingType::Sampler: { if (isCompute) context->CSSetSamplers(bindingIndex, 1, detail.m_samplerState.readRef()); diff --git a/tools/render-test/render-d3d12.cpp b/tools/render-test/render-d3d12.cpp index 691ab7d25..430a273ef 100644 --- a/tools/render-test/render-d3d12.cpp +++ b/tools/render-test/render-d3d12.cpp @@ -78,13 +78,13 @@ public: // ShaderCompiler implementation virtual ShaderProgram* compileProgram(const ShaderCompileRequest& request) override; - + ~D3D12Renderer(); protected: static const Int kMaxNumRenderFrames = 4; static const Int kMaxNumRenderTargets = 3; - + struct Submitter { virtual void setRootConstantBufferView(int index, D3D12_GPU_VIRTUAL_ADDRESS gpuBufferLocation) = 0; @@ -106,7 +106,7 @@ protected: class ShaderProgramImpl: public ShaderProgram { public: - PipelineType m_pipelineType; + PipelineType m_pipelineType; List m_vertexShader; List m_pixelShader; List m_computeShader; @@ -116,7 +116,7 @@ protected: { public: typedef BufferResource Parent; - + enum class BackingStyle { Unknown, @@ -164,8 +164,8 @@ protected: } BackingStyle m_backingStyle; ///< How the resource is 'backed' - either as a resource or cpu memory. Cpu memory is typically used for constant buffers. - D3D12Resource m_resource; ///< The resource typically in gpu memory - D3D12Resource m_uploadResource; ///< If the resource can be written to, and is in gpu memory (ie not Memory backed), will have upload resource + D3D12Resource m_resource; ///< The resource typically in gpu memory + D3D12Resource m_uploadResource; ///< If the resource can be written to, and is in gpu memory (ie not Memory backed), will have upload resource Usage m_initialUsage; @@ -219,8 +219,8 @@ protected: {} List m_bindingDetails; ///< These match 1-1 to the bindings in the m_desc - - D3D12DescriptorHeap m_viewHeap; ///< Cbv, Srv, Uav + + D3D12DescriptorHeap m_viewHeap; ///< Cbv, Srv, Uav D3D12DescriptorHeap m_samplerHeap; ///< Heap for samplers }; @@ -245,10 +245,10 @@ protected: struct BindParameters { - enum - { - kMaxRanges = 16, - kMaxParameters = 32 + enum + { + kMaxRanges = 16, + kMaxParameters = 32 }; D3D12_DESCRIPTOR_RANGE& nextRange() { return m_ranges[m_rangeIndex++]; } @@ -273,7 +273,7 @@ protected: } virtual void setRootDescriptorTable(int index, D3D12_GPU_DESCRIPTOR_HANDLE baseDescriptor) override { - m_commandList->SetGraphicsRootDescriptorTable(index, baseDescriptor); + m_commandList->SetGraphicsRootDescriptorTable(index, baseDescriptor); } void setRootSignature(ID3D12RootSignature* rootSignature) { @@ -317,7 +317,7 @@ protected: void releaseFrameResources(); Result createBuffer(const D3D12_RESOURCE_DESC& resourceDesc, const void* srcData, D3D12Resource& uploadResource, D3D12_RESOURCE_STATES finalState, D3D12Resource& resourceOut); - + void beginRender(); void endRender(); @@ -338,7 +338,7 @@ protected: Result calcComputePipelineState(ComPtr& signatureOut, ComPtr& pipelineStateOut); Result _bindRenderState(RenderState* renderState, ID3D12GraphicsCommandList* commandList, Submitter* submitter); - + Result _calcBindParameters(BindParameters& params); RenderState* findRenderState(PipelineType pipelineType); @@ -364,7 +364,7 @@ protected: int m_targetSampleQuality = 0; ///< The multi sample quality Desc m_desc; - + bool m_isInitialized = false; D3D12_PRIMITIVE_TOPOLOGY_TYPE m_primitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE; @@ -556,7 +556,7 @@ static void _initBufferResourceDesc(size_t bufferSize, D3D12_RESOURCE_DESC& out) Result D3D12Renderer::createBuffer(const D3D12_RESOURCE_DESC& resourceDesc, const void* srcData, D3D12Resource& uploadResource, D3D12_RESOURCE_STATES finalState, D3D12Resource& resourceOut) { const size_t bufferSize = size_t(resourceDesc.Width); - + { D3D12_HEAP_PROPERTIES heapProps; heapProps.Type = D3D12_HEAP_TYPE_DEFAULT; @@ -580,25 +580,25 @@ Result D3D12Renderer::createBuffer(const D3D12_RESOURCE_DESC& resourceDesc, cons D3D12_RESOURCE_DESC uploadResourceDesc(resourceDesc); uploadResourceDesc.Flags = D3D12_RESOURCE_FLAG_NONE; - + SLANG_RETURN_ON_FAIL(uploadResource.initCommitted(m_device, heapProps, D3D12_HEAP_FLAG_NONE, uploadResourceDesc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr)); } if (srcData) - { - // Copy data to the intermediate upload heap and then schedule a copy + { + // Copy data to the intermediate upload heap and then schedule a copy // from the upload heap to the vertex buffer. UINT8* dstData; D3D12_RANGE readRange = {}; // We do not intend to read from this resource on the CPU. ID3D12Resource* dxUploadResource = uploadResource.getResource(); - + SLANG_RETURN_ON_FAIL(dxUploadResource->Map(0, &readRange, reinterpret_cast(&dstData))); ::memcpy(dstData, srcData, bufferSize); dxUploadResource->Unmap(0, nullptr); m_commandList->CopyBufferRegion(resourceOut, 0, uploadResource, 0, bufferSize); - + // Make sure it's in the right state { D3D12BarrierSubmitter submitter(m_commandList); @@ -641,7 +641,7 @@ void D3D12Renderer::beginRender() m_circularResourceHeap.updateCompleted(); getFrame().m_commandAllocator->Reset(); - + _resetCommandList(); // Indicate that the render target needs to be writable @@ -665,7 +665,7 @@ void D3D12Renderer::endRender() D3D12Resource& backBuffer = *m_backBuffers[m_renderTargetIndex]; if (m_isMultiSampled) { - // MSAA resolve + // MSAA resolve D3D12Resource& renderTarget = *m_renderTargets[m_renderTargetIndex]; assert(&renderTarget != &backBuffer); // Barriers to wait for the render target, and the backbuffer to be in correct state @@ -674,7 +674,7 @@ void D3D12Renderer::endRender() renderTarget.transition(D3D12_RESOURCE_STATE_RESOLVE_SOURCE, submitter); backBuffer.transition(D3D12_RESOURCE_STATE_RESOLVE_DEST, submitter); } - + // Do the resolve... m_commandList->ResolveSubresource(backBuffer, 0, renderTarget, 0, m_targetFormat); } @@ -760,16 +760,16 @@ Result D3D12Renderer::captureTextureToSurface(D3D12Resource& resource, Surface& // Do the copy { D3D12_TEXTURE_COPY_LOCATION srcLoc; - srcLoc.pResource = resource; + srcLoc.pResource = resource; srcLoc.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; srcLoc.SubresourceIndex = 0; D3D12_TEXTURE_COPY_LOCATION dstLoc; - dstLoc.pResource = stagingResource; + dstLoc.pResource = stagingResource; dstLoc.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT; dstLoc.PlacedFootprint.Offset = 0; dstLoc.PlacedFootprint.Footprint.Format = desc.Format; - dstLoc.PlacedFootprint.Footprint.Width = UINT(desc.Width); + dstLoc.PlacedFootprint.Footprint.Width = UINT(desc.Width); dstLoc.PlacedFootprint.Footprint.Height = UINT(desc.Height); dstLoc.PlacedFootprint.Footprint.Depth = 1; dstLoc.PlacedFootprint.Footprint.RowPitch = UINT(rowPitch); @@ -789,10 +789,10 @@ Result D3D12Renderer::captureTextureToSurface(D3D12Resource& resource, Surface& ID3D12Resource* dxResource = stagingResource; UINT8* data; - D3D12_RANGE readRange = {0, bufferSize}; - + D3D12_RANGE readRange = {0, bufferSize}; + SLANG_RETURN_ON_FAIL(dxResource->Map(0, &readRange, reinterpret_cast(&data))); - + Result res = surfaceOut.set(int(desc.Width), int(desc.Height), Format::RGBA_Unorm_UInt8, int(rowPitch), data, SurfaceAllocator::getMallocAllocator()); dxResource->Unmap(0, nullptr); @@ -823,7 +823,7 @@ Result D3D12Renderer::calcComputePipelineState(ComPtr& sign } { - // Describe and create the compute pipeline state object + // Describe and create the compute pipeline state object D3D12_COMPUTE_PIPELINE_STATE_DESC computeDesc = {}; computeDesc.pRootSignature = rootSignature; computeDesc.CS = { m_boundShaderProgram->m_computeShader.Buffer(), m_boundShaderProgram->m_computeShader.Count() }; @@ -933,8 +933,8 @@ Result D3D12Renderer::calcGraphicsPipelineState(ComPtr& sig ds.StencilReadMask = D3D12_DEFAULT_STENCIL_READ_MASK; ds.StencilWriteMask = D3D12_DEFAULT_STENCIL_WRITE_MASK; const D3D12_DEPTH_STENCILOP_DESC defaultStencilOp = - { - D3D12_STENCIL_OP_KEEP, D3D12_STENCIL_OP_KEEP, D3D12_STENCIL_OP_KEEP, D3D12_COMPARISON_FUNC_ALWAYS + { + D3D12_STENCIL_OP_KEEP, D3D12_STENCIL_OP_KEEP, D3D12_STENCIL_OP_KEEP, D3D12_COMPARISON_FUNC_ALWAYS }; ds.FrontFace = defaultStencilOp; ds.BackFace = defaultStencilOp; @@ -1057,7 +1057,7 @@ D3D12Renderer::RenderState* D3D12Renderer::calcRenderState() renderState->m_bindingState = m_boundBindingState; renderState->m_inputLayout = m_boundInputLayout; renderState->m_shaderProgram = m_boundShaderProgram; - + renderState->m_rootSignature.swap(rootSignature); renderState->m_pipelineState.swap(pipelineState); @@ -1077,7 +1077,7 @@ Result D3D12Renderer::_calcBindParameters(BindParameters& params) const int numBoundConstantBuffers = numConstantBuffers; const BindingState::Desc& bindingStateDesc = m_boundBindingState->getDesc(); - + const auto& bindings = bindingStateDesc.m_bindings; const auto& details = m_boundBindingState->m_bindingDetails; @@ -1109,11 +1109,11 @@ Result D3D12Renderer::_calcBindParameters(BindParameters& params) numConstantBuffers++; } } - + if (detail.m_srvIndex >= 0) { D3D12_DESCRIPTOR_RANGE& range = params.nextRange(); - + range.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV; range.NumDescriptors = 1; range.BaseShaderRegister = bindingIndex; @@ -1121,7 +1121,7 @@ Result D3D12Renderer::_calcBindParameters(BindParameters& params) range.OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND; D3D12_ROOT_PARAMETER& param = params.nextParameter(); - + param.ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE; param.ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL; @@ -1133,7 +1133,7 @@ Result D3D12Renderer::_calcBindParameters(BindParameters& params) if (detail.m_uavIndex >= 0) { D3D12_DESCRIPTOR_RANGE& range = params.nextRange(); - + range.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_UAV; range.NumDescriptors = 1; range.BaseShaderRegister = bindingIndex; @@ -1141,7 +1141,7 @@ Result D3D12Renderer::_calcBindParameters(BindParameters& params) range.OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND; D3D12_ROOT_PARAMETER& param = params.nextParameter(); - + param.ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE; param.ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL; @@ -1157,7 +1157,7 @@ Result D3D12Renderer::_calcBindParameters(BindParameters& params) if (m_boundBindingState && m_boundBindingState->m_samplerHeap.getUsedSize() > 0) { D3D12_DESCRIPTOR_RANGE& range = params.nextRange(); - + range.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER; range.NumDescriptors = m_boundBindingState->m_samplerHeap.getUsedSize(); range.BaseShaderRegister = 0; @@ -1165,7 +1165,7 @@ Result D3D12Renderer::_calcBindParameters(BindParameters& params) range.OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND; D3D12_ROOT_PARAMETER& param = params.nextParameter(); - + param.ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE; param.ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL; @@ -1213,7 +1213,7 @@ Result D3D12Renderer::_bindRenderState(RenderState* renderState, ID3D12GraphicsC { const auto& detail = details[i]; const auto& binding = bindings[i]; - + if (binding.bindingType == BindingType::Buffer) { assert(binding.resource && binding.resource->isBuffer()); @@ -1270,9 +1270,9 @@ Result D3D12Renderer::initialize(const Desc& desc, void* inWindowHandle) #define LOAD_D3D_PROC(TYPE, NAME) \ - TYPE NAME##_ = (TYPE) loadProc(d3dModule, #NAME); + TYPE NAME##_ = (TYPE) loadProc(d3dModule, #NAME); #define LOAD_DXGI_PROC(TYPE, NAME) \ - TYPE NAME##_ = (TYPE) loadProc(dxgiModule, #NAME); + TYPE NAME##_ = (TYPE) loadProc(dxgiModule, #NAME); UINT dxgiFactoryFlags = 0; @@ -1307,12 +1307,12 @@ Result D3D12Renderer::initialize(const Desc& desc, void* inWindowHandle) } SLANG_RETURN_ON_FAIL(CreateDXGIFactory2_(dxgiFactoryFlags, IID_PPV_ARGS(dxgiFactory.writeRef()))); } - + D3D_FEATURE_LEVEL featureLevel = D3D_FEATURE_LEVEL_11_0; // Search for an adapter that meets our requirements ComPtr adapter; - + LOAD_D3D_PROC(PFN_D3D12_CREATE_DEVICE, D3D12CreateDevice); if (!D3D12CreateDevice_) { @@ -1358,12 +1358,12 @@ Result D3D12Renderer::initialize(const Desc& desc, void* inWindowHandle) // Couldn't find an adapter return SLANG_FAIL; } - + m_numRenderFrames = 3; m_numRenderTargets = 2; - + m_desc = desc; - + // set viewport { m_viewport.Width = float(m_desc.width); @@ -1392,7 +1392,7 @@ Result D3D12Renderer::initialize(const Desc& desc, void* inWindowHandle) DXGI_SWAP_CHAIN_DESC swapChainDesc = {}; swapChainDesc.BufferCount = m_numRenderTargets; swapChainDesc.BufferDesc.Width = m_desc.width; - swapChainDesc.BufferDesc.Height = m_desc.height; + swapChainDesc.BufferDesc.Height = m_desc.height; swapChainDesc.BufferDesc.Format = m_targetFormat; swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD; @@ -1493,7 +1493,7 @@ Result D3D12Renderer::createFrameResources() { D3D12_CPU_DESCRIPTOR_HANDLE rtvStart(m_rtvHeap->GetCPUDescriptorHandleForHeapStart()); - // Work out target format + // Work out target format D3D12_RESOURCE_DESC resourceDesc; { ComPtr backBuffer; @@ -1527,7 +1527,7 @@ Result D3D12Renderer::createFrameResources() heapProps.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; heapProps.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; heapProps.CreationNodeMask = 1; - heapProps.VisibleNodeMask = 1; + heapProps.VisibleNodeMask = 1; D3D12_CLEAR_VALUE clearValue = {}; clearValue.Format = m_targetFormat; @@ -1539,7 +1539,7 @@ Result D3D12Renderer::createFrameResources() desc.Format = resourceFormat; desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; desc.SampleDesc.Count = m_numTargetSamples; - desc.SampleDesc.Quality = m_targetSampleQuality; + desc.SampleDesc.Quality = m_targetSampleQuality; desc.Alignment = 0; SLANG_RETURN_ON_FAIL(m_renderTargetResources[i].initCommitted(m_device, heapProps, D3D12_HEAP_FLAG_NONE, desc, D3D12_RESOURCE_STATE_RENDER_TARGET, &clearValue)); @@ -1587,7 +1587,7 @@ Result D3D12Renderer::createFrameResources() D3D12_RESOURCE_DESC resourceDesc = {}; resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; resourceDesc.Format = resourceFormat; - resourceDesc.Width = m_desc.width; + resourceDesc.Width = m_desc.width; resourceDesc.Height = m_desc.height; resourceDesc.DepthOrArraySize = 1; resourceDesc.MipLevels = 1; @@ -1661,7 +1661,7 @@ void D3D12Renderer::presentFrame() } } - // Increment the fence value. Save on the frame - we'll know that frame is done when the fence value >= + // Increment the fence value. Save on the frame - we'll know that frame is done when the fence value >= m_frameInfos[m_frameIndex].m_fenceValue = m_fence.nextSignal(m_commandQueue); // increment frame index after signal @@ -1669,7 +1669,7 @@ void D3D12Renderer::presentFrame() // Update the render target index. m_renderTargetIndex = m_swapChain->GetCurrentBackBufferIndex(); - // On the current frame wait until it is completed + // On the current frame wait until it is completed { FrameInfo& frame = m_frameInfos[m_frameIndex]; // If the next frame is not ready to be rendered yet, wait until it is ready. @@ -1685,7 +1685,7 @@ SlangResult D3D12Renderer::captureScreenSurface(Surface& surfaceOut) return captureTextureToSurface(*m_renderTargets[m_renderTargetIndex], surfaceOut); } -ShaderCompiler* D3D12Renderer::getShaderCompiler() +ShaderCompiler* D3D12Renderer::getShaderCompiler() { return this; } @@ -1764,26 +1764,26 @@ TextureResource* D3D12Renderer::createTextureResource(Resource::Usage initialUsa { return nullptr; } - + const int arraySize = srcDesc.calcEffectiveArraySize(); - + const D3D12_RESOURCE_DIMENSION dimension = _calcResourceDimension(srcDesc.type); if (dimension == D3D12_RESOURCE_DIMENSION_UNKNOWN) - { + { return nullptr; } const int numMipMaps = srcDesc.numMipLevels; - + // Setup desc D3D12_RESOURCE_DESC resourceDesc; - resourceDesc.Dimension = dimension; + resourceDesc.Dimension = dimension; resourceDesc.Format = pixelFormat; resourceDesc.Width = srcDesc.size.width; resourceDesc.Height = srcDesc.size.height; - resourceDesc.DepthOrArraySize = (srcDesc.size.depth > 1) ? srcDesc.size.depth : arraySize; - + resourceDesc.DepthOrArraySize = (srcDesc.size.depth > 1) ? srcDesc.size.depth : arraySize; + resourceDesc.MipLevels = numMipMaps; resourceDesc.SampleDesc.Count = srcDesc.sampleDesc.numSamples; resourceDesc.SampleDesc.Quality = srcDesc.sampleDesc.quality; @@ -1809,7 +1809,7 @@ TextureResource* D3D12Renderer::createTextureResource(Resource::Usage initialUsa texture->m_resource.setDebugName(L"Texture"); } - // Calculate the layout + // Calculate the layout List layouts; layouts.SetSize(numMipMaps); List mipRowSizeInBytes; @@ -1877,8 +1877,8 @@ TextureResource* D3D12Renderer::createTextureResource(Resource::Usage initialUsa assert(footprint.Width == mipSize.width && footprint.Height == mipSize.height && footprint.Depth == mipSize.depth); const ptrdiff_t dstMipRowPitch = ptrdiff_t(layouts[j].Footprint.RowPitch); - const ptrdiff_t srcMipRowPitch = ptrdiff_t(initData->mipRowStrides[j]); - + const ptrdiff_t srcMipRowPitch = ptrdiff_t(initData->mipRowStrides[j]); + assert(dstMipRowPitch >= srcMipRowPitch); const uint8_t* srcRow = (const uint8_t*)initData->subResources[subResourceIndex]; @@ -1937,7 +1937,7 @@ TextureResource* D3D12Renderer::createTextureResource(Resource::Usage initialUsa BufferResource* D3D12Renderer::createBufferResource(Resource::Usage initialUsage, const BufferResource::Desc& descIn, const void* initData) { typedef BufferResourceImpl::BackingStyle Style; - + BufferResource::Desc srcDesc(descIn); srcDesc.setDefaults(initialUsage); @@ -1955,8 +1955,8 @@ BufferResource* D3D12Renderer::createBufferResource(Resource::Usage initialUsage { case Style::MemoryBacked: { - // Assume the constant buffer will change every frame. We'll just keep a copy of the contents - // in regular memory until it needed + // Assume the constant buffer will change every frame. We'll just keep a copy of the contents + // in regular memory until it needed buffer->m_memory.SetSize(UInt(srcDesc.sizeInBytes)); // Initialize if (initData) @@ -1977,7 +1977,7 @@ BufferResource* D3D12Renderer::createBufferResource(Resource::Usage initialUsage return buffer.detach(); } -InputLayout* D3D12Renderer::createInputLayout(const InputElementDesc* inputElements, UInt inputElementCount) +InputLayout* D3D12Renderer::createInputLayout(const InputElementDesc* inputElements, UInt inputElementCount) { RefPtr layout(new InputLayoutImpl); @@ -1991,7 +1991,7 @@ InputLayout* D3D12Renderer::createInputLayout(const InputElementDesc* inputEleme layout->m_text.SetSize(textSize); char* textPos = layout->m_text.Buffer(); - // + // List& elements = layout->m_elements; elements.SetSize(inputElementCount); @@ -2023,7 +2023,7 @@ InputLayout* D3D12Renderer::createInputLayout(const InputElementDesc* inputEleme return layout.detach(); } -void* D3D12Renderer::map(BufferResource* bufferIn, MapFlavor flavor) +void* D3D12Renderer::map(BufferResource* bufferIn, MapFlavor flavor) { typedef BufferResourceImpl::BackingStyle Style; @@ -2031,7 +2031,7 @@ void* D3D12Renderer::map(BufferResource* bufferIn, MapFlavor flavor) buffer->m_mapFlavor = flavor; const size_t bufferSize = buffer->getDesc().sizeInBytes; - + switch (buffer->m_backingStyle) { case Style::ResourceBacked: @@ -2098,11 +2098,11 @@ void* D3D12Renderer::map(BufferResource* bufferIn, MapFlavor flavor) D3D12_RANGE readRange = { 0, bufferSize }; SLANG_RETURN_NULL_ON_FAIL(stageBuf.getResource()->Map(0, &readRange, reinterpret_cast(&data))); - + // Copy to memory buffer buffer->m_memory.SetSize(bufferSize); ::memcpy(buffer->m_memory.Buffer(), data, bufferSize); - + stageBuf.getResource()->Unmap(0, nullptr); } @@ -2172,12 +2172,12 @@ void D3D12Renderer::unmap(BufferResource* bufferIn) } } -void D3D12Renderer::setInputLayout(InputLayout* inputLayout) +void D3D12Renderer::setInputLayout(InputLayout* inputLayout) { m_boundInputLayout = static_cast(inputLayout); } -void D3D12Renderer::setPrimitiveTopology(PrimitiveTopology topology) +void D3D12Renderer::setPrimitiveTopology(PrimitiveTopology topology) { switch (topology) { @@ -2209,7 +2209,7 @@ void D3D12Renderer::setVertexBuffers(UInt startSlot, UInt slotCount, BufferResou BufferResourceImpl* buffer = static_cast(buffers[i]); if (buffer) { - assert(buffer->m_initialUsage == Resource::Usage::VertexBuffer); + assert(buffer->m_initialUsage == Resource::Usage::VertexBuffer); } BoundVertexBuffer& boundBuffer = m_boundVertexBuffers[startSlot + i]; @@ -2227,16 +2227,16 @@ void D3D12Renderer::setShaderProgram(ShaderProgram* inProgram) void D3D12Renderer::draw(UInt vertexCount, UInt startVertex) { ID3D12GraphicsCommandList* commandList = m_commandList; - + RenderState* renderState = calcRenderState(); if (!renderState) { assert(!"Couldn't create render state"); - return; + return; } BindingStateImpl* bindingState = m_boundBindingState; - + // Submit - setting for graphics { GraphicsSubmitter submitter(commandList); @@ -2286,7 +2286,7 @@ BindingState* D3D12Renderer::createBindingState(const BindingState::Desc& bindin RefPtr bindingState(new BindingStateImpl(bindingStateDesc)); SLANG_RETURN_NULL_ON_FAIL(bindingState->init(m_device)); - + const auto& srcBindings = bindingStateDesc.m_bindings; const int numBindings = int(srcBindings.Count()); @@ -2307,7 +2307,7 @@ BindingState* D3D12Renderer::createBindingState(const BindingState::Desc& bindin assert(srcEntry.resource && srcEntry.resource->isBuffer()); BufferResourceImpl* bufferResource = static_cast(srcEntry.resource.Ptr()); const BufferResource::Desc& bufferDesc = bufferResource->getDesc(); - + const size_t bufferSize = bufferDesc.sizeInBytes; const int elemSize = bufferDesc.elementSize <= 0 ? sizeof(uint32_t) : bufferDesc.elementSize; @@ -2315,7 +2315,7 @@ BindingState* D3D12Renderer::createBindingState(const BindingState::Desc& bindin // NOTE! In this arrangement the buffer can either be a ConstantBuffer or a 'StorageBuffer'. // If it's a storage buffer then it has a 'uav'. - // In neither circumstance is there an associated srv + // In neither circumstance is there an associated srv // This departs a little from dx11 code - in that it will create srv and uav for a storage buffer. if (bufferDesc.bindFlags & Resource::BindFlag::UnorderedAccess) { @@ -2328,7 +2328,7 @@ BindingState* D3D12Renderer::createBindingState(const BindingState::Desc& bindin D3D12_UNORDERED_ACCESS_VIEW_DESC uavDesc = {}; uavDesc.ViewDimension = D3D12_UAV_DIMENSION_BUFFER; - uavDesc.Format = DXGI_FORMAT_UNKNOWN; + uavDesc.Format = D3DUtil::getMapFormat(bufferDesc.format); uavDesc.Buffer.StructureByteStride = elemSize; @@ -2336,10 +2336,8 @@ BindingState* D3D12Renderer::createBindingState(const BindingState::Desc& bindin uavDesc.Buffer.NumElements = (UINT)(bufferSize / elemSize); uavDesc.Buffer.Flags = D3D12_BUFFER_UAV_FLAG_NONE; - if (bufferDesc.elementSize == 0) + if (bufferDesc.elementSize == 0 && bufferDesc.format == Format::Unknown) { - // TODO: are there UAV cases we need to handle that are neither - // raw nor structured? RWBuffer would be one... uavDesc.Buffer.Flags |= D3D12_BUFFER_UAV_FLAG_RAW; uavDesc.Format = DXGI_FORMAT_R32_TYPELESS; @@ -2388,7 +2386,7 @@ BindingState* D3D12Renderer::createBindingState(const BindingState::Desc& bindin { return nullptr; } - + { const D3D12_RESOURCE_DESC resourceDesc = textureResource->m_resource.getResource()->GetDesc(); const DXGI_FORMAT pixelFormat = resourceDesc.Format; diff --git a/tools/render-test/render.cpp b/tools/render-test/render.cpp index b22a41ca7..bfb7aeb94 100644 --- a/tools/render-test/render.cpp +++ b/tools/render-test/render.cpp @@ -6,7 +6,7 @@ namespace renderer_test { using namespace Slang; -/* static */const Resource::BindFlag::Enum Resource::s_requiredBinding[] = +/* static */const Resource::BindFlag::Enum Resource::s_requiredBinding[] = { BindFlag::VertexBuffer, // VertexBuffer BindFlag::IndexBuffer, // IndexBuffer @@ -19,7 +19,7 @@ using namespace Slang; BindFlag::PixelShaderResource, // PixelShaderResource BindFlag::NonPixelShaderResource, // NonPixelShaderResource BindFlag::Enum(BindFlag::PixelShaderResource | BindFlag::NonPixelShaderResource), // GenericRead -}; +}; /* static */void Resource::compileTimeAsserts() @@ -44,7 +44,7 @@ const Resource::DescBase& Resource::getDescBase() const /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! RendererUtil !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ -/* static */const uint8_t RendererUtil::s_formatSize[] = +/* static */const uint8_t RendererUtil::s_formatSize[] = { 0, // Unknown, @@ -55,6 +55,8 @@ const Resource::DescBase& Resource::getDescBase() const uint8_t(sizeof(uint32_t)), // RGBA_Unorm_UInt8, + uint8_t(sizeof(uint32_t)), // R_UInt32, + uint8_t(sizeof(float)), // D_Float32, uint8_t(sizeof(uint32_t)), // D_Unorm24_S8, }; @@ -151,7 +153,7 @@ int TextureResource::Size::calcMaxDimension(Type type) const case Resource::Type::Texture1D: return this->width; case Resource::Type::Texture3D: return std::max(std::max(this->width, this->height), this->depth); case Resource::Type::TextureCube: // fallthru - case Resource::Type::Texture2D: + case Resource::Type::Texture2D: { return std::max(this->width, this->height); } @@ -193,24 +195,24 @@ int TextureResource::Desc::calcNumSubResources() const switch (type) { - case Resource::Type::Texture1D: - case Resource::Type::Texture2D: + case Resource::Type::Texture1D: + case Resource::Type::Texture2D: { return numMipMaps * arrSize; } - case Resource::Type::Texture3D: + case Resource::Type::Texture3D: { // can't have arrays of 3d textures assert(this->arraySize <= 1); - return numMipMaps * this->size.depth; + return numMipMaps * this->size.depth; } - case Resource::Type::TextureCube: + case Resource::Type::TextureCube: { // There are 6 faces to a cubemap return numMipMaps * arrSize * 6; } default: return 0; - } + } } void TextureResource::Desc::fixSize() @@ -259,7 +261,7 @@ int TextureResource::Desc::calcEffectiveArraySize() const switch (type) { case Resource::Type::Texture1D: // fallthru - case Resource::Type::Texture2D: + case Resource::Type::Texture2D: { return arrSize; } @@ -273,7 +275,7 @@ void TextureResource::Desc::init(Type typeIn) { this->type = typeIn; this->size.init(); - + this->format = Format::Unknown; this->arraySize = 0; this->numMipLevels = 0; @@ -287,14 +289,14 @@ void TextureResource::Desc::init1D(Format formatIn, int widthIn, int numMipMapsI { this->type = Type::Texture1D; this->size.init(widthIn); - + this->format = format; this->arraySize = 0; this->numMipLevels = numMipMapsIn; this->sampleDesc.init(); this->bindFlags = 0; - this->cpuAccessFlags = 0; + this->cpuAccessFlags = 0; } void TextureResource::Desc::init2D(Type typeIn, Format formatIn, int widthIn, int heightIn, int numMipMapsIn) @@ -303,7 +305,7 @@ void TextureResource::Desc::init2D(Type typeIn, Format formatIn, int widthIn, in this->type = type; this->size.init(widthIn, heightIn); - + this->format = format; this->arraySize = 0; this->numMipLevels = numMipMapsIn; @@ -357,7 +359,7 @@ ProjectionStyle RendererUtil::getProjectionStyle(RendererType type) case ProjectionStyle::OpenGl: { static const float kIdentity[] = - { + { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, @@ -378,7 +380,7 @@ ProjectionStyle RendererUtil::getProjectionStyle(RendererType type) ::memcpy(projMatrix, kIdentity, sizeof(kIdentity)); break; } - default: + default: { assert(!"Not handled"); } diff --git a/tools/render-test/render.h b/tools/render-test/render.h index 9dd87437d..807fabf23 100644 --- a/tools/render-test/render.h +++ b/tools/render-test/render.h @@ -47,7 +47,7 @@ enum class ProjectionStyle Unknown, OpenGl, DirectX, - Vulkan, + Vulkan, CountOf, }; @@ -103,7 +103,7 @@ public: }; /// Different formats of things like pixels or elements of vertices -/// NOTE! Any change to this type (adding, removing, changing order) - must also be reflected in changes to RendererUtil +/// NOTE! Any change to this type (adding, removing, changing order) - must also be reflected in changes to RendererUtil enum class Format { Unknown, @@ -115,10 +115,12 @@ enum class Format RGBA_Unorm_UInt8, - D_Float32, + R_UInt32, + + D_Float32, D_Unorm24_S8, - CountOf, + CountOf, }; struct InputElementDesc @@ -183,14 +185,14 @@ class Resource: public Slang::RefObject enum Enum { VertexBuffer = 0x001, - IndexBuffer = 0x002, - ConstantBuffer = 0x004, - StreamOutput = 0x008, - RenderTarget = 0x010, - DepthStencil = 0x020, - UnorderedAccess = 0x040, - PixelShaderResource = 0x080, - NonPixelShaderResource = 0x100, + IndexBuffer = 0x002, + ConstantBuffer = 0x004, + StreamOutput = 0x008, + RenderTarget = 0x010, + DepthStencil = 0x020, + UnorderedAccess = 0x040, + PixelShaderResource = 0x080, + NonPixelShaderResource = 0x100, }; }; @@ -213,7 +215,7 @@ class Resource: public Slang::RefObject 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 + int cpuAccessFlags = 0; ///< Combination of Resource::AccessFlag }; /// Get the type @@ -229,7 +231,7 @@ class Resource: public Slang::RefObject bool canBind(BindFlag::Enum bindFlag) const { return getDescBase().canBind(bindFlag); } /// For a usage gives the required binding flags - static const BindFlag::Enum s_requiredBinding[]; /// Maps Usage to bind flags required + static const BindFlag::Enum s_requiredBinding[]; /// Maps Usage to bind flags required protected: Resource(Type type): @@ -252,12 +254,14 @@ class BufferResource: public Resource { sizeInBytes = sizeInBytesIn; elementSize = 0; + format = Format::Unknown; } /// Set up default parameters based on usage void setDefaults(Usage initialUsage); - size_t sizeInBytes; ///< Total size in bytes + size_t sizeInBytes; ///< Total size in bytes int elementSize; ///< Get the element stride. If > 0, this is a structured buffer + Format format; }; /// Get the buffer description @@ -289,7 +293,7 @@ class TextureResource: public Resource int numSamples; ///< Number of samples per pixel int quality; ///< The quality measure for the samples }; - + struct Size { void init() @@ -299,8 +303,8 @@ class TextureResource: public Resource void init(int widthIn, int heightIn = 1, int depthIn = 1) { width = widthIn; - height = heightIn; - depth = depthIn; + height = heightIn; + depth = depthIn; } /// Given the type works out the maximum dimension size int calcMaxDimension(Type type) const; @@ -309,7 +313,7 @@ class TextureResource: public Resource int width; ///< Width in pixels int height; ///< Height in pixels (if 2d or 3d) - int depth; ///< Depth (if 3d) + int depth; ///< Depth (if 3d) }; struct Desc: public DescBase @@ -326,21 +330,21 @@ class TextureResource: public Resource /// Calculate the total number of sub resources. 0 on error. 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) + /// 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() const; - /// Use type to fix the size values (and array size). + /// 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(); /// Set up default parameters based on type and usage void setDefaults(Usage initialUsage); - Size size; + Size size; - int arraySize; ///< Array size + int arraySize; ///< Array size int numMipLevels; ///< Number of mip levels - if 0 will create all mip levels Format format; ///< The resources format @@ -354,9 +358,9 @@ class TextureResource: public Resource struct Data { ptrdiff_t* mipRowStrides; ///< The row stride for a mip map - int numMips; ///< The number of mip maps - const void*const* subResources; ///< Pointers to each full mip subResource - int numSubResources; ///< The total amount of subResources. Typically = numMips * depth * arraySize + int numMips; ///< The number of mip maps + const void*const* subResources; ///< Pointers to each full mip subResource + int numSubResources; ///< The total amount of subResources. Typically = numMips * depth * arraySize }; /// Get the description of the texture @@ -374,7 +378,7 @@ class TextureResource: public Resource width = width >> mipLevel; return width > 0 ? width : 1; } - + protected: Desc m_desc; }; @@ -392,7 +396,7 @@ enum class BindingType class BindingState : public Slang::RefObject { public: - /// A register set consists of one or more contiguous indices. + /// A register set consists of one or more contiguous indices. /// To be valid index >= 0 and size >= 1 struct RegisterRange { @@ -414,7 +418,7 @@ public: int16_t index; ///< The base index uint16_t size; ///< The amount of register indices }; - + struct SamplerDesc { bool isCompareSampler; @@ -429,14 +433,14 @@ public: }; struct Desc - { + { /// Add a resource - assumed that the binding will match the Desc of the resource void addResource(BindingType bindingType, Resource* resource, const RegisterRange& registerRange); - /// Add a sampler + /// Add a sampler void addSampler(const SamplerDesc& desc, const RegisterRange& registerRange); - /// Add a BufferResource + /// Add a BufferResource void addBufferResource(BufferResource* resource, const RegisterRange& registerRange) { addResource(BindingType::Buffer, resource, registerRange); } - /// Add a texture + /// Add a texture void addTextureResource(TextureResource* resource, const RegisterRange& registerRange) { addResource(BindingType::Texture, resource, registerRange); } /// Add combined texture a void addCombinedTextureSampler(TextureResource* resource, const SamplerDesc& samplerDesc, const RegisterRange& registerRange); @@ -444,12 +448,12 @@ public: /// 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 + /// Clear the contents void clear(); 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_samplerDescs; ///< Holds the SamplerDesc for the binding - indexed by the descIndex member of Binding + int m_numRenderTargets = 1; }; @@ -468,7 +472,7 @@ public: class Renderer: public Slang::RefObject { public: - + struct Desc { int width; ///< Width in pixels @@ -482,10 +486,10 @@ public: virtual void presentFrame() = 0; - /// Create a texture resource. initData holds the initialize data to set the contents of the texture when constructed. + /// Create a texture resource. initData holds the initialize data to set the contents of the texture when constructed. 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; } + virtual BufferResource* createBufferResource(Resource::Usage initialUsage, const BufferResource::Desc& desc, const void* initData = nullptr) { return nullptr; } /// Captures the back buffer and stores the result in surfaceOut. If the surface contains data - it will either be overwritten (if same size and format), or freed and a re-allocated. virtual SlangResult captureScreenSurface(Surface& surfaceOut) = 0; @@ -509,7 +513,7 @@ public: virtual void draw(UInt vertexCount, UInt startVertex = 0) = 0; virtual void dispatchCompute(int x, int y, int z) = 0; - /// Commit any buffered state changes or draw calls. + /// Commit any buffered state changes or draw calls. /// presentFrame will commitAll implicitly before doing a present virtual void submitGpuWork() = 0; /// Blocks until Gpu work is complete diff --git a/tools/render-test/shader-input-layout.cpp b/tools/render-test/shader-input-layout.cpp index fcf25f376..0257c9b53 100644 --- a/tools/render-test/shader-input-layout.cpp +++ b/tools/render-test/shader-input-layout.cpp @@ -1,6 +1,8 @@ #include "shader-input-layout.h" #include "core/token-reader.h" +#include "render.h" + namespace renderer_test { using namespace Slang; @@ -177,6 +179,13 @@ namespace renderer_test else entry.textureDesc.content = InputTextureContent::Gradient; } + else if(word == "format") + { + parser.Read("="); + auto formatWord = parser.ReadWord(); + if(formatWord == "R_UInt32") + entry.bufferDesc.format = Format::R_UInt32; + } if (parser.LookAhead(",")) parser.Read(","); else @@ -229,8 +238,8 @@ namespace renderer_test } } } - - + + } void generateTextureData(TextureData & output, const InputTextureDesc & inputDesc) { @@ -308,4 +317,4 @@ namespace renderer_test } } } -} \ No newline at end of file +} diff --git a/tools/render-test/shader-input-layout.h b/tools/render-test/shader-input-layout.h index 0f5a54326..7537527ab 100644 --- a/tools/render-test/shader-input-layout.h +++ b/tools/render-test/shader-input-layout.h @@ -3,6 +3,8 @@ #include "core/basic.h" +#include "render.h" + namespace renderer_test { enum class ShaderInputType @@ -35,6 +37,7 @@ struct InputBufferDesc { InputBufferType type = InputBufferType::ConstantBuffer; int stride = 0; // stride == 0 indicates an unstructured buffer. + Format format = Format::Unknown; }; struct InputSamplerDesc @@ -52,7 +55,7 @@ public: InputSamplerDesc samplerDesc; bool isOutput = false; int hlslBinding = -1; - Slang::List glslBinding; + Slang::List glslBinding; }; struct TextureData @@ -76,4 +79,4 @@ void generateTextureData(TextureData & output, const InputTextureDesc & desc); } // namespace render_test -#endif \ No newline at end of file +#endif diff --git a/tools/render-test/shader-renderer-util.cpp b/tools/render-test/shader-renderer-util.cpp index 6e11757fb..e46c725bc 100644 --- a/tools/render-test/shader-renderer-util.cpp +++ b/tools/render-test/shader-renderer-util.cpp @@ -23,7 +23,7 @@ using namespace Slang; textureResourceDesc.arraySize = inputDesc.arrayLength; textureResourceDesc.bindFlags = bindFlags; - // It's the same size in all dimensions + // It's the same size in all dimensions switch (inputDesc.dimension) { case 1: @@ -92,6 +92,7 @@ using namespace Slang; BufferResource::Desc srcDesc; srcDesc.init(bufferSize); + srcDesc.format = inputDesc.format; int bindFlags = 0; if (inputDesc.type == InputBufferType::ConstantBuffer) @@ -123,7 +124,7 @@ using namespace Slang; bufferOut = bufferResource; return SLANG_OK; } - + static BindingState::SamplerDesc _calcSamplerDesc(const InputSamplerDesc& srcDesc) { BindingState::SamplerDesc dstDesc; @@ -139,7 +140,7 @@ static BindingState::SamplerDesc _calcSamplerDesc(const InputSamplerDesc& srcDes switch (bindingStyle) { - case BindingStyle::DirectX: + case BindingStyle::DirectX: { return RegisterRange::makeSingle(entry.hlslBinding); } @@ -167,7 +168,7 @@ static BindingState::SamplerDesc _calcSamplerDesc(const InputSamplerDesc& srcDes break; } } - return RegisterRange::makeRange(baseIndex, count); + return RegisterRange::makeRange(baseIndex, count); } /* case BindingStyle::Vulkan: { @@ -204,7 +205,7 @@ 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, registerSet); break; } @@ -228,13 +229,13 @@ static BindingState::SamplerDesc _calcSamplerDesc(const InputSamplerDesc& srcDes descOut.addSampler(_calcSamplerDesc(srcEntry.samplerDesc), registerSet); break; } - default: + default: { assert(!"Unhandled type"); return SLANG_FAIL; } } - } + } return SLANG_OK; } diff --git a/tools/render-test/vk-util.cpp b/tools/render-test/vk-util.cpp index 022d15a32..925001144 100644 --- a/tools/render-test/vk-util.cpp +++ b/tools/render-test/vk-util.cpp @@ -15,6 +15,7 @@ namespace renderer_test { case Format::RG_Float32: return VK_FORMAT_R32G32_SFLOAT; case Format::R_Float32: return VK_FORMAT_R32_SFLOAT; case Format::RGBA_Unorm_UInt8: return VK_FORMAT_R8G8B8A8_UNORM; + case Format::R_UInt32: return VK_FORMAT_R32_UINT; case Format::D_Float32: return VK_FORMAT_D32_SFLOAT; case Format::D_Unorm24_S8: return VK_FORMAT_D24_UNORM_S8_UINT; -- cgit v1.2.3