diff --git a/Include/Extensions/NRIHelper.h b/Include/Extensions/NRIHelper.h index a09b088..b60bbf5 100644 --- a/Include/Extensions/NRIHelper.h +++ b/Include/Extensions/NRIHelper.h @@ -23,6 +23,7 @@ NRI_STRUCT(TextureUploadDesc) NRI_OPTIONAL const NRI_NAME(TextureSubresourceUploadDesc)* subresources; // if provided, must include ALL subresources = layerNum * mipNum NRI_NAME(Texture)* texture; NRI_NAME(AccessLayoutStage) after; + NRI_NAME(PlaneBits) planes; }; NRI_STRUCT(BufferUploadDesc) diff --git a/Include/NRI.h b/Include/NRI.h index 5e9e7c1..fcb4b78 100644 --- a/Include/NRI.h +++ b/Include/NRI.h @@ -25,8 +25,8 @@ Non-goals: #include #define NRI_VERSION_MAJOR 1 -#define NRI_VERSION_MINOR 143 -#define NRI_VERSION_DATE "29 August 2024" +#define NRI_VERSION_MINOR 144 +#define NRI_VERSION_DATE "30 August 2024" #ifdef _WIN32 #define NRI_CALL __fastcall diff --git a/Include/NRIDescs.h b/Include/NRIDescs.h index 3c3201c..ed7ee4f 100644 --- a/Include/NRIDescs.h +++ b/Include/NRIDescs.h @@ -181,6 +181,16 @@ NRI_ENUM MAX_NUM ); +NRI_ENUM_BITS +( + PlaneBits, uint8_t, + + ALL = 0, + COLOR = NRI_SET_BIT(0), + DEPTH = NRI_SET_BIT(1), + STENCIL = NRI_SET_BIT(2) +); + NRI_ENUM_BITS ( FormatSupportBits, uint16_t, @@ -365,6 +375,9 @@ NRI_ENUM SHADER_RESOURCE_STORAGE_1D_ARRAY, COLOR_ATTACHMENT, DEPTH_STENCIL_ATTACHMENT, + DEPTH_READONLY_STENCIL_ATTACHMENT, + DEPTH_ATTACHMENT_STENCIL_READONLY, + DEPTH_STENCIL_READONLY, MAX_NUM ); @@ -381,6 +394,9 @@ NRI_ENUM SHADER_RESOURCE_STORAGE_2D_ARRAY, COLOR_ATTACHMENT, DEPTH_STENCIL_ATTACHMENT, + DEPTH_READONLY_STENCIL_ATTACHMENT, + DEPTH_ATTACHMENT_STENCIL_READONLY, + DEPTH_STENCIL_READONLY, SHADING_RATE_ATTACHMENT, MAX_NUM @@ -452,15 +468,6 @@ NRI_ENUM_BITS ACCELERATION_STRUCTURE_BUILD_READ = NRI_SET_BIT(7) ); -NRI_ENUM_BITS -( - ResourceViewBits, uint8_t, - - NONE = 0, - READONLY_DEPTH = NRI_SET_BIT(0), - READONLY_STENCIL = NRI_SET_BIT(1) -); - // Resources NRI_STRUCT(TextureDesc) { @@ -492,7 +499,6 @@ NRI_STRUCT(Texture1DViewDesc) NRI_NAME(Mip_t) mipNum; NRI_NAME(Dim_t) layerOffset; NRI_NAME(Dim_t) layerNum; - NRI_NAME(ResourceViewBits) flags; }; NRI_STRUCT(Texture2DViewDesc) @@ -504,7 +510,6 @@ NRI_STRUCT(Texture2DViewDesc) NRI_NAME(Mip_t) mipNum; NRI_NAME(Dim_t) layerOffset; NRI_NAME(Dim_t) layerNum; - NRI_NAME(ResourceViewBits) flags; }; NRI_STRUCT(Texture3DViewDesc) @@ -912,18 +917,6 @@ NRI_ENUM MAX_NUM ); -NRI_ENUM -( - AttachmentContentType, uint8_t, - - COLOR, - DEPTH, - STENCIL, - DEPTH_STENCIL, - - MAX_NUM -); - NRI_ENUM_BITS ( ColorWriteBits, uint8_t, @@ -952,7 +945,7 @@ NRI_UNION(ClearValue) NRI_STRUCT(ClearDesc) { NRI_NAME(ClearValue) value; - NRI_NAME(AttachmentContentType) attachmentContentType; + NRI_NAME(PlaneBits) planes; uint32_t colorAttachmentIndex; }; @@ -1209,6 +1202,7 @@ NRI_STRUCT(TextureBarrierDesc) NRI_NAME(Mip_t) mipNum; NRI_NAME(Dim_t) layerOffset; NRI_NAME(Dim_t) layerNum; + NRI_NAME(PlaneBits) planes; }; NRI_STRUCT(BarrierGroupDesc) diff --git a/Resources/Version.h b/Resources/Version.h index 0a56a5d..afbacc9 100644 --- a/Resources/Version.h +++ b/Resources/Version.h @@ -4,7 +4,7 @@ #define STR(x) STR_HELPER(x) #define VERSION_MAJOR 1 -#define VERSION_MINOR 143 +#define VERSION_MINOR 144 #define VERSION_BUILD 0 #define VERSION_REVISION 0 diff --git a/Source/D3D11/CommandBufferD3D11.cpp b/Source/D3D11/CommandBufferD3D11.cpp index 9aea17a..51bd2ed 100644 --- a/Source/D3D11/CommandBufferD3D11.cpp +++ b/Source/D3D11/CommandBufferD3D11.cpp @@ -96,6 +96,8 @@ Result CommandBufferD3D11::Begin(const DescriptorPool* descriptorPool) { m_IndexBuffer = nullptr; m_VertexBuffer = nullptr; + ResetAttachments(); + // Dynamic state m_SamplePositionsState.Reset(); m_StencilRef = 0; @@ -170,24 +172,23 @@ void CommandBufferD3D11::SetShadingRate(const ShadingRateDesc& shadingRateDesc) } void CommandBufferD3D11::ClearAttachments(const ClearDesc* clearDescs, uint32_t clearDescNum, const Rect* rects, uint32_t rectNum) { - if (!rects || !rectNum) { + if (!clearDescNum) + return; + + if (!rectNum) { for (uint32_t i = 0; i < clearDescNum; i++) { const ClearDesc& clearDesc = clearDescs[i]; - switch (clearDesc.attachmentContentType) { - case AttachmentContentType::COLOR: - m_DeferredContext->ClearRenderTargetView(m_RenderTargets[clearDesc.colorAttachmentIndex], &clearDesc.value.color32f.x); - break; - case AttachmentContentType::DEPTH: - m_DeferredContext->ClearDepthStencilView(m_DepthStencil, D3D11_CLEAR_DEPTH, clearDesc.value.depthStencil.depth, 0); - break; - case AttachmentContentType::STENCIL: - m_DeferredContext->ClearDepthStencilView(m_DepthStencil, D3D11_CLEAR_STENCIL, 0.0f, clearDesc.value.depthStencil.stencil); - break; - case AttachmentContentType::DEPTH_STENCIL: - m_DeferredContext->ClearDepthStencilView( - m_DepthStencil, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, clearDesc.value.depthStencil.depth, clearDesc.value.depthStencil.stencil); - break; + if (clearDesc.planes & PlaneBits::COLOR) + m_DeferredContext->ClearRenderTargetView(m_RenderTargets[clearDesc.colorAttachmentIndex], &clearDesc.value.color32f.x); + else { + uint32_t clearFlags = 0; + if (clearDescs[i].planes & PlaneBits::DEPTH) + clearFlags |= D3D11_CLEAR_DEPTH; + if (clearDescs[i].planes & PlaneBits::STENCIL) + clearFlags |= D3D11_CLEAR_STENCIL; + + m_DeferredContext->ClearDepthStencilView(m_DepthStencil, clearFlags, clearDesc.value.depthStencil.depth, clearDesc.value.depthStencil.stencil); } } } else { @@ -202,20 +203,14 @@ void CommandBufferD3D11::ClearAttachments(const ClearDesc* clearDescs, uint32_t FLOAT color[4] = {}; for (uint32_t i = 0; i < clearDescNum; i++) { const ClearDesc& clearDesc = clearDescs[i]; - switch (clearDesc.attachmentContentType) { - case AttachmentContentType::COLOR: - m_DeferredContext->ClearView(m_RenderTargets[clearDesc.colorAttachmentIndex], &clearDesc.value.color32f.x, rectsD3D, rectNum); - break; - case AttachmentContentType::DEPTH: - case AttachmentContentType::DEPTH_STENCIL: - color[0] = clearDesc.value.depthStencil.depth; - m_DeferredContext->ClearView(m_DepthStencil, color, rectsD3D, rectNum); - break; - case AttachmentContentType::STENCIL: - color[0] = clearDesc.value.depthStencil.stencil; - m_DeferredContext->ClearView(m_DepthStencil, color, rectsD3D, rectNum); - break; - } + + if (clearDesc.planes & PlaneBits::COLOR) + m_DeferredContext->ClearView(m_RenderTargets[clearDesc.colorAttachmentIndex], &clearDesc.value.color32f.x, rectsD3D, rectNum); + else if (clearDesc.planes & PlaneBits::DEPTH) { + color[0] = clearDesc.value.depthStencil.depth; + m_DeferredContext->ClearView(m_DepthStencil, color, rectsD3D, rectNum); + } else + CHECK(false, "Bad or unsupported plane"); } } else CHECK(false, "'ClearView' emulation for 11.0 is not implemented!"); diff --git a/Source/D3D11/CommandBufferD3D11.h b/Source/D3D11/CommandBufferD3D11.h index 91ae10d..f4f72d9 100644 --- a/Source/D3D11/CommandBufferD3D11.h +++ b/Source/D3D11/CommandBufferD3D11.h @@ -24,6 +24,14 @@ struct CommandBufferD3D11 final : public CommandBufferHelper { return m_DeferredContext; } + inline void ResetAttachments() { + m_RenderTargetNum = 0; + for (size_t i = 0; i < m_RenderTargets.size(); i++) + m_RenderTargets[i] = nullptr; + + m_DepthStencil = nullptr; + } + Result Create(ID3D11DeviceContext* precreatedContext); void Submit(); StdAllocator& GetStdAllocator() const; @@ -37,9 +45,6 @@ struct CommandBufferD3D11 final : public CommandBufferHelper { SET_D3D_DEBUG_OBJECT_NAME(m_CommandList, name); } - inline void EndRendering() { - } - Result Begin(const DescriptorPool* descriptorPool); Result End(); void SetViewports(const Viewport* viewports, uint32_t viewportNum); diff --git a/Source/D3D11/CommandBufferD3D11.hpp b/Source/D3D11/CommandBufferD3D11.hpp index 6059d6a..2db277d 100644 --- a/Source/D3D11/CommandBufferD3D11.hpp +++ b/Source/D3D11/CommandBufferD3D11.hpp @@ -44,7 +44,7 @@ static void NRI_CALL CmdBeginRendering(CommandBuffer& commandBuffer, const Attac } static void NRI_CALL CmdEndRendering(CommandBuffer& commandBuffer) { - ((CommandBufferD3D11&)commandBuffer).EndRendering(); + ((CommandBufferD3D11&)commandBuffer).ResetAttachments(); } static void NRI_CALL CmdSetViewports(CommandBuffer& commandBuffer, const Viewport* viewports, uint32_t viewportNum) { diff --git a/Source/D3D11/CommandBufferEmuD3D11.cpp b/Source/D3D11/CommandBufferEmuD3D11.cpp index 58d443b..f0a620b 100644 --- a/Source/D3D11/CommandBufferEmuD3D11.cpp +++ b/Source/D3D11/CommandBufferEmuD3D11.cpp @@ -215,7 +215,7 @@ void CommandBufferEmuD3D11::Submit() { commandBuffer.BeginRendering(attachmentsDesc); } break; case END_RENDERING: { - commandBuffer.EndRendering(); + commandBuffer.ResetAttachments(); } break; case BIND_VERTEX_BUFFERS: { uint32_t baseSlot; diff --git a/Source/D3D11/DescriptorD3D11.cpp b/Source/D3D11/DescriptorD3D11.cpp index 6013299..72a72e1 100644 --- a/Source/D3D11/DescriptorD3D11.cpp +++ b/Source/D3D11/DescriptorD3D11.cpp @@ -119,7 +119,10 @@ Result DescriptorD3D11::Create(const Texture1DViewDesc& textureViewDesc) { m_Type = DescriptorTypeDX11::NO_SHADER_VISIBLE; } break; - case Texture1DViewType::DEPTH_STENCIL_ATTACHMENT: { + case Texture1DViewType::DEPTH_STENCIL_ATTACHMENT: + case Texture1DViewType::DEPTH_READONLY_STENCIL_ATTACHMENT: + case Texture1DViewType::DEPTH_ATTACHMENT_STENCIL_READONLY: + case Texture1DViewType::DEPTH_STENCIL_READONLY: { D3D11_DEPTH_STENCIL_VIEW_DESC desc = {}; desc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE1DARRAY; desc.Texture1DArray.MipSlice = textureViewDesc.mipOffset; @@ -127,10 +130,12 @@ Result DescriptorD3D11::Create(const Texture1DViewDesc& textureViewDesc) { desc.Texture1DArray.ArraySize = remainingLayers; desc.Format = format; - if (textureViewDesc.flags & ResourceViewBits::READONLY_DEPTH) - desc.Flags |= D3D11_DSV_READ_ONLY_DEPTH; - if (textureViewDesc.flags & ResourceViewBits::READONLY_STENCIL) - desc.Flags |= D3D11_DSV_READ_ONLY_STENCIL; + if (textureViewDesc.viewType == Texture1DViewType::DEPTH_READONLY_STENCIL_ATTACHMENT) + desc.Flags = D3D11_DSV_READ_ONLY_DEPTH; + else if (textureViewDesc.viewType == Texture1DViewType::DEPTH_ATTACHMENT_STENCIL_READONLY) + desc.Flags = D3D11_DSV_READ_ONLY_STENCIL; + else if (textureViewDesc.viewType == Texture1DViewType::DEPTH_STENCIL_READONLY) + desc.Flags = D3D11_DSV_READ_ONLY_DEPTH | D3D11_DSV_READ_ONLY_STENCIL; hr = m_Device->CreateDepthStencilView(texture, &desc, (ID3D11DepthStencilView**)&m_Descriptor); @@ -269,7 +274,10 @@ Result DescriptorD3D11::Create(const Texture2DViewDesc& textureViewDesc) { break; } - case Texture2DViewType::DEPTH_STENCIL_ATTACHMENT: { + case Texture2DViewType::DEPTH_STENCIL_ATTACHMENT: + case Texture2DViewType::DEPTH_READONLY_STENCIL_ATTACHMENT: + case Texture2DViewType::DEPTH_ATTACHMENT_STENCIL_READONLY: + case Texture2DViewType::DEPTH_STENCIL_READONLY: { D3D11_DEPTH_STENCIL_VIEW_DESC desc = {}; if (textureDesc.sampleNum > 1) { desc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMSARRAY; @@ -283,10 +291,12 @@ Result DescriptorD3D11::Create(const Texture2DViewDesc& textureViewDesc) { } desc.Format = format; - if (textureViewDesc.flags & ResourceViewBits::READONLY_DEPTH) - desc.Flags |= D3D11_DSV_READ_ONLY_DEPTH; - if (textureViewDesc.flags & ResourceViewBits::READONLY_STENCIL) - desc.Flags |= D3D11_DSV_READ_ONLY_STENCIL; + if (textureViewDesc.viewType == Texture2DViewType::DEPTH_READONLY_STENCIL_ATTACHMENT) + desc.Flags = D3D11_DSV_READ_ONLY_DEPTH; + else if (textureViewDesc.viewType == Texture2DViewType::DEPTH_ATTACHMENT_STENCIL_READONLY) + desc.Flags = D3D11_DSV_READ_ONLY_STENCIL; + else if (textureViewDesc.viewType == Texture2DViewType::DEPTH_STENCIL_READONLY) + desc.Flags = D3D11_DSV_READ_ONLY_DEPTH | D3D11_DSV_READ_ONLY_STENCIL; hr = m_Device->CreateDepthStencilView(texture, &desc, (ID3D11DepthStencilView**)&m_Descriptor); diff --git a/Source/D3D11/DeviceD3D11.cpp b/Source/D3D11/DeviceD3D11.cpp index e0de2a8..fe70655 100644 --- a/Source/D3D11/DeviceD3D11.cpp +++ b/Source/D3D11/DeviceD3D11.cpp @@ -341,7 +341,7 @@ void DeviceD3D11::FillDesc() { m_Desc.bufferMaxSize = D3D11_REQ_RESOURCE_SIZE_IN_MEGABYTES_EXPRESSION_C_TERM * 1024ull * 1024ull; m_Desc.pushConstantsMaxSize = D3D11_REQ_IMMEDIATE_CONSTANT_BUFFER_ELEMENT_COUNT * 16; - m_Desc.boundDescriptorSetMaxNum = D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT; + m_Desc.boundDescriptorSetMaxNum = BOUND_DESCRIPTOR_SET_MAX_NUM; m_Desc.perStageDescriptorSamplerMaxNum = D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT; m_Desc.perStageDescriptorConstantBufferMaxNum = D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT; m_Desc.perStageDescriptorStorageBufferMaxNum = m_Version >= 1 ? D3D11_1_UAV_SLOT_COUNT : D3D11_PS_CS_UAV_REGISTER_COUNT; diff --git a/Source/D3D11/TextureD3D11.h b/Source/D3D11/TextureD3D11.h index 54bb8ba..5f987e5 100644 --- a/Source/D3D11/TextureD3D11.h +++ b/Source/D3D11/TextureD3D11.h @@ -40,6 +40,7 @@ struct TextureD3D11 { } inline uint32_t GetSubresourceIndex(const TextureRegionDesc& regionDesc) const { + // https://learn.microsoft.com/en-us/windows/win32/direct3d12/subresources#plane-slice return regionDesc.mipOffset + regionDesc.layerOffset * m_Desc.mipNum; } diff --git a/Source/D3D12/CommandBufferD3D12.cpp b/Source/D3D12/CommandBufferD3D12.cpp index 009aa4d..54a2cc0 100644 --- a/Source/D3D12/CommandBufferD3D12.cpp +++ b/Source/D3D12/CommandBufferD3D12.cpp @@ -208,8 +208,7 @@ static inline D3D12_RESOURCE_STATES GetResourceStates(AccessBits accessMask, D3D return resourceStates; } -static void AddResourceBarrier( - D3D12_COMMAND_LIST_TYPE commandListType, ID3D12Resource* resource, AccessBits before, AccessBits after, D3D12_RESOURCE_BARRIER& resourceBarrier, uint32_t subresource) { +static void AddResourceBarrier(D3D12_COMMAND_LIST_TYPE commandListType, ID3D12Resource* resource, AccessBits before, AccessBits after, D3D12_RESOURCE_BARRIER& resourceBarrier, uint32_t subresource) { D3D12_RESOURCE_STATES resourceStateBefore = GetResourceStates(before, commandListType); D3D12_RESOURCE_STATES resourceStateAfter = GetResourceStates(after, commandListType); @@ -267,6 +266,8 @@ inline Result CommandBufferD3D12::Begin(const DescriptorPool* descriptorPool) { m_Pipeline = nullptr; m_PrimitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED; + ResetAttachments(); + return Result::SUCCESS; } @@ -278,10 +279,10 @@ inline Result CommandBufferD3D12::End() { } inline void CommandBufferD3D12::SetViewports(const Viewport* viewports, uint32_t viewportNum) { - static_assert(offsetof(Viewport, x) == 0, "Unsupported viewport data layout."); - static_assert(offsetof(Viewport, width) == 8, "Unsupported viewport data layout."); - static_assert(offsetof(Viewport, depthRangeMin) == 16, "Unsupported viewport data layout."); - static_assert(offsetof(Viewport, depthRangeMax) == 20, "Unsupported viewport data layout."); + static_assert(offsetof(Viewport, x) == 0, "Unsupported viewport data layout"); + static_assert(offsetof(Viewport, width) == 8, "Unsupported viewport data layout"); + static_assert(offsetof(Viewport, depthRangeMin) == 16, "Unsupported viewport data layout"); + static_assert(offsetof(Viewport, depthRangeMax) == 20, "Unsupported viewport data layout"); m_GraphicsCommandList->RSSetViewports(viewportNum, (D3D12_VIEWPORT*)viewports); } @@ -331,28 +332,23 @@ inline void CommandBufferD3D12::SetShadingRate(const ShadingRateDesc& shadingRat } inline void CommandBufferD3D12::ClearAttachments(const ClearDesc* clearDescs, uint32_t clearDescNum, const Rect* rects, uint32_t rectNum) { + if (!clearDescNum) + return; + D3D12_RECT* rectsD3D12 = StackAlloc(D3D12_RECT, rectNum); ConvertRects(rectsD3D12, rects, rectNum); for (uint32_t i = 0; i < clearDescNum; i++) { - if (AttachmentContentType::COLOR == clearDescs[i].attachmentContentType) + if (clearDescs[i].planes & PlaneBits::COLOR) m_GraphicsCommandList->ClearRenderTargetView(m_RenderTargets[clearDescs[i].colorAttachmentIndex], &clearDescs[i].value.color32f.x, rectNum, rectsD3D12); - else if (m_DepthStencil.ptr) { + else { D3D12_CLEAR_FLAGS clearFlags = (D3D12_CLEAR_FLAGS)0; - switch (clearDescs[i].attachmentContentType) { - case AttachmentContentType::DEPTH: - clearFlags = D3D12_CLEAR_FLAG_DEPTH; - break; - case AttachmentContentType::STENCIL: - clearFlags = D3D12_CLEAR_FLAG_STENCIL; - break; - case AttachmentContentType::DEPTH_STENCIL: - clearFlags = D3D12_CLEAR_FLAG_DEPTH | D3D12_CLEAR_FLAG_STENCIL; - break; - } + if (clearDescs[i].planes & PlaneBits::DEPTH) + clearFlags |= D3D12_CLEAR_FLAG_DEPTH; + if (clearDescs[i].planes & PlaneBits::STENCIL) + clearFlags |= D3D12_CLEAR_FLAG_STENCIL; - m_GraphicsCommandList->ClearDepthStencilView( - m_DepthStencil, clearFlags, clearDescs[i].value.depthStencil.depth, clearDescs[i].value.depthStencil.stencil, rectNum, rectsD3D12); + m_GraphicsCommandList->ClearDepthStencilView(m_DepthStencil, clearFlags, clearDescs[i].value.depthStencil.depth, clearDescs[i].value.depthStencil.stencil, rectNum, rectsD3D12); } } } @@ -561,8 +557,7 @@ inline void CommandBufferD3D12::CopyTexture(Texture& dstTexture, const TextureRe } } -inline void CommandBufferD3D12::UploadBufferToTexture( - Texture& dstTexture, const TextureRegionDesc& dstRegionDesc, const Buffer& srcBuffer, const TextureDataLayoutDesc& srcDataLayoutDesc) { +inline void CommandBufferD3D12::UploadBufferToTexture(Texture& dstTexture, const TextureRegionDesc& dstRegionDesc, const Buffer& srcBuffer, const TextureDataLayoutDesc& srcDataLayoutDesc) { TextureD3D12& dstTextureD3D12 = (TextureD3D12&)dstTexture; D3D12_TEXTURE_COPY_LOCATION dstTextureCopyLocation = { dstTextureD3D12, @@ -598,8 +593,7 @@ inline void CommandBufferD3D12::UploadBufferToTexture( m_GraphicsCommandList->CopyTextureRegion(&dstTextureCopyLocation, dstRegionDesc.x, dstRegionDesc.y, dstRegionDesc.z, &srcTextureCopyLocation, &box); } -inline void CommandBufferD3D12::ReadbackTextureToBuffer( - Buffer& dstBuffer, TextureDataLayoutDesc& dstDataLayoutDesc, const Texture& srcTexture, const TextureRegionDesc& srcRegionDesc) { +inline void CommandBufferD3D12::ReadbackTextureToBuffer(Buffer& dstBuffer, TextureDataLayoutDesc& dstDataLayoutDesc, const Texture& srcTexture, const TextureRegionDesc& srcRegionDesc) { TextureD3D12& srcTextureD3D12 = (TextureD3D12&)srcTexture; D3D12_TEXTURE_COPY_LOCATION srcTextureCopyLocation = { srcTextureD3D12, @@ -788,12 +782,12 @@ inline void CommandBufferD3D12::Barrier(const BarrierGroupDesc& barrierGroupDesc const Dim_t layerNum = barrierDesc.layerNum == REMAINING_LAYERS ? textureDesc.layerNum : barrierDesc.layerNum; const Mip_t mipNum = barrierDesc.mipNum == REMAINING_MIPS ? textureDesc.mipNum : barrierDesc.mipNum; - if (barrierDesc.layerOffset == 0 && layerNum == textureDesc.layerNum && barrierDesc.mipOffset == 0 && mipNum == textureDesc.mipNum) + if (barrierDesc.layerOffset == 0 && layerNum == textureDesc.layerNum && barrierDesc.mipOffset == 0 && mipNum == textureDesc.mipNum && barrierDesc.planes == PlaneBits::ALL) AddResourceBarrier(commandListType, texture, barrierDesc.before.access, barrierDesc.after.access, *ptr++, D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES); else { for (Dim_t layerOffset = barrierDesc.layerOffset; layerOffset < barrierDesc.layerOffset + layerNum; layerOffset++) { for (Mip_t mipOffset = barrierDesc.mipOffset; mipOffset < barrierDesc.mipOffset + mipNum; mipOffset++) { - uint32_t subresource = texture.GetSubresourceIndex(layerOffset, mipOffset); + uint32_t subresource = texture.GetSubresourceIndex(layerOffset, mipOffset, barrierDesc.planes); AddResourceBarrier(commandListType, texture, barrierDesc.before.access, barrierDesc.after.access, *ptr++, subresource); } } diff --git a/Source/D3D12/CommandBufferD3D12.h b/Source/D3D12/CommandBufferD3D12.h index 1e21b9e..c539ed2 100644 --- a/Source/D3D12/CommandBufferD3D12.h +++ b/Source/D3D12/CommandBufferD3D12.h @@ -37,6 +37,14 @@ struct CommandBufferD3D12 { return m_Device; } + inline void ResetAttachments() { + m_RenderTargetNum = 0; + for (size_t i = 0; i < m_RenderTargets.size(); i++) + m_RenderTargets[i].ptr = NULL; + + m_DepthStencil.ptr = NULL; + } + Result Create(D3D12_COMMAND_LIST_TYPE commandListType, ID3D12CommandAllocator* commandAllocator); Result Create(const CommandBufferD3D12Desc& commandBufferDesc); @@ -48,9 +56,6 @@ struct CommandBufferD3D12 { SET_D3D_DEBUG_OBJECT_NAME(m_GraphicsCommandList, name); } - inline void EndRendering() { - } - Result Begin(const DescriptorPool* descriptorPool); Result End(); void SetViewports(const Viewport* viewports, uint32_t viewportNum); @@ -87,20 +92,14 @@ struct CommandBufferD3D12 { void CopyQueries(const QueryPool& queryPool, uint32_t offset, uint32_t num, Buffer& buffer, uint64_t alignedBufferOffset); void BeginAnnotation(const char* name); void EndAnnotation(); - - void BuildTopLevelAccelerationStructure(uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, AccelerationStructureBuildBits flags, AccelerationStructure& dst, - Buffer& scratch, uint64_t scratchOffset); - void BuildBottomLevelAccelerationStructure(uint32_t geometryObjectNum, const GeometryObject* geometryObjects, AccelerationStructureBuildBits flags, AccelerationStructure& dst, - Buffer& scratch, uint64_t scratchOffset); - void UpdateTopLevelAccelerationStructure(uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, AccelerationStructureBuildBits flags, AccelerationStructure& dst, - AccelerationStructure& src, Buffer& scratch, uint64_t scratchOffset); - void UpdateBottomLevelAccelerationStructure(uint32_t geometryObjectNum, const GeometryObject* geometryObjects, AccelerationStructureBuildBits flags, AccelerationStructure& dst, - AccelerationStructure& src, Buffer& scratch, uint64_t scratchOffset); + void BuildTopLevelAccelerationStructure(uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, AccelerationStructureBuildBits flags, AccelerationStructure& dst, Buffer& scratch, uint64_t scratchOffset); + void BuildBottomLevelAccelerationStructure(uint32_t geometryObjectNum, const GeometryObject* geometryObjects, AccelerationStructureBuildBits flags, AccelerationStructure& dst, Buffer& scratch, uint64_t scratchOffset); + void UpdateTopLevelAccelerationStructure(uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, AccelerationStructureBuildBits flags, AccelerationStructure& dst, AccelerationStructure& src, Buffer& scratch, uint64_t scratchOffset); + void UpdateBottomLevelAccelerationStructure(uint32_t geometryObjectNum, const GeometryObject* geometryObjects, AccelerationStructureBuildBits flags, AccelerationStructure& dst, AccelerationStructure& src, Buffer& scratch, uint64_t scratchOffset); void CopyAccelerationStructure(AccelerationStructure& dst, AccelerationStructure& src, CopyMode copyMode); void WriteAccelerationStructureSize(const AccelerationStructure* const* accelerationStructures, uint32_t accelerationStructureNum, QueryPool& queryPool, uint32_t queryOffset); void DispatchRays(const DispatchRaysDesc& dispatchRaysDesc); void DispatchRaysIndirect(const Buffer& buffer, uint64_t offset); - void DrawMeshTasks(const DrawMeshTasksDesc& drawMeshTasksDesc); void DrawMeshTasksIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride); @@ -113,7 +112,7 @@ struct CommandBufferD3D12 { const PipelineLayoutD3D12* m_PipelineLayout = nullptr; PipelineD3D12* m_Pipeline = nullptr; D3D12_PRIMITIVE_TOPOLOGY m_PrimitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED; - std::array m_DescriptorSets = {}; // in VK "maxBoundDescriptorSets" is 32 + std::array m_DescriptorSets = {}; uint32_t m_RenderTargetNum = 0; uint8_t m_Version = 0; bool m_IsGraphicsPipelineLayout = false; diff --git a/Source/D3D12/CommandBufferD3D12.hpp b/Source/D3D12/CommandBufferD3D12.hpp index fab7e5e..6d491a8 100644 --- a/Source/D3D12/CommandBufferD3D12.hpp +++ b/Source/D3D12/CommandBufferD3D12.hpp @@ -44,7 +44,7 @@ static void NRI_CALL CmdBeginRendering(CommandBuffer& commandBuffer, const Attac } static void NRI_CALL CmdEndRendering(CommandBuffer& commandBuffer) { - ((CommandBufferD3D12&)commandBuffer).EndRendering(); + ((CommandBufferD3D12&)commandBuffer).ResetAttachments(); } static void NRI_CALL CmdSetViewports(CommandBuffer& commandBuffer, const Viewport* viewports, uint32_t viewportNum) { diff --git a/Source/D3D12/DescriptorD3D12.cpp b/Source/D3D12/DescriptorD3D12.cpp index 5bddf4a..b6478a6 100644 --- a/Source/D3D12/DescriptorD3D12.cpp +++ b/Source/D3D12/DescriptorD3D12.cpp @@ -85,7 +85,10 @@ Result DescriptorD3D12::Create(const Texture1DViewDesc& textureViewDesc) { return CreateRenderTargetView(texture, desc); } - case Texture1DViewType::DEPTH_STENCIL_ATTACHMENT: { + case Texture1DViewType::DEPTH_STENCIL_ATTACHMENT: + case Texture1DViewType::DEPTH_READONLY_STENCIL_ATTACHMENT: + case Texture1DViewType::DEPTH_ATTACHMENT_STENCIL_READONLY: + case Texture1DViewType::DEPTH_STENCIL_READONLY: { D3D12_DEPTH_STENCIL_VIEW_DESC desc = {}; desc.Format = format; desc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE1DARRAY; @@ -93,10 +96,12 @@ Result DescriptorD3D12::Create(const Texture1DViewDesc& textureViewDesc) { desc.Texture1DArray.FirstArraySlice = textureViewDesc.layerOffset; desc.Texture1DArray.ArraySize = remainingLayers; - if (textureViewDesc.flags & ResourceViewBits::READONLY_DEPTH) - desc.Flags |= D3D12_DSV_FLAG_READ_ONLY_DEPTH; - if (textureViewDesc.flags & ResourceViewBits::READONLY_STENCIL) - desc.Flags |= D3D12_DSV_FLAG_READ_ONLY_STENCIL; + if (textureViewDesc.viewType == Texture1DViewType::DEPTH_READONLY_STENCIL_ATTACHMENT) + desc.Flags = D3D12_DSV_FLAG_READ_ONLY_DEPTH; + else if (textureViewDesc.viewType == Texture1DViewType::DEPTH_ATTACHMENT_STENCIL_READONLY) + desc.Flags = D3D12_DSV_FLAG_READ_ONLY_STENCIL; + else if (textureViewDesc.viewType == Texture1DViewType::DEPTH_STENCIL_READONLY) + desc.Flags = D3D12_DSV_FLAG_READ_ONLY_DEPTH | D3D12_DSV_FLAG_READ_ONLY_STENCIL; return CreateDepthStencilView(texture, desc); } @@ -203,7 +208,10 @@ Result DescriptorD3D12::Create(const Texture2DViewDesc& textureViewDesc) { return CreateRenderTargetView(texture, desc); } - case Texture2DViewType::DEPTH_STENCIL_ATTACHMENT: { + case Texture2DViewType::DEPTH_STENCIL_ATTACHMENT: + case Texture2DViewType::DEPTH_READONLY_STENCIL_ATTACHMENT: + case Texture2DViewType::DEPTH_ATTACHMENT_STENCIL_READONLY: + case Texture2DViewType::DEPTH_STENCIL_READONLY: { D3D12_DEPTH_STENCIL_VIEW_DESC desc = {}; desc.Format = format; desc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2DARRAY; @@ -211,10 +219,12 @@ Result DescriptorD3D12::Create(const Texture2DViewDesc& textureViewDesc) { desc.Texture2DArray.FirstArraySlice = textureViewDesc.layerOffset; desc.Texture2DArray.ArraySize = remainingLayers; - if (textureViewDesc.flags & ResourceViewBits::READONLY_DEPTH) - desc.Flags |= D3D12_DSV_FLAG_READ_ONLY_DEPTH; - if (textureViewDesc.flags & ResourceViewBits::READONLY_STENCIL) - desc.Flags |= D3D12_DSV_FLAG_READ_ONLY_STENCIL; + if (textureViewDesc.viewType == Texture2DViewType::DEPTH_READONLY_STENCIL_ATTACHMENT) + desc.Flags = D3D12_DSV_FLAG_READ_ONLY_DEPTH; + else if (textureViewDesc.viewType == Texture2DViewType::DEPTH_ATTACHMENT_STENCIL_READONLY) + desc.Flags = D3D12_DSV_FLAG_READ_ONLY_STENCIL; + else if (textureViewDesc.viewType == Texture2DViewType::DEPTH_STENCIL_READONLY) + desc.Flags = D3D12_DSV_FLAG_READ_ONLY_DEPTH | D3D12_DSV_FLAG_READ_ONLY_STENCIL; return CreateDepthStencilView(texture, desc); } diff --git a/Source/D3D12/DeviceD3D12.cpp b/Source/D3D12/DeviceD3D12.cpp index 9ffe30c..3e6a2dd 100644 --- a/Source/D3D12/DeviceD3D12.cpp +++ b/Source/D3D12/DeviceD3D12.cpp @@ -482,7 +482,7 @@ void DeviceD3D12::FillDesc(const DeviceCreationDesc& deviceCreationDesc) { m_Desc.bufferMaxSize = D3D12_REQ_RESOURCE_SIZE_IN_MEGABYTES_EXPRESSION_C_TERM * 1024ull * 1024ull; m_Desc.pushConstantsMaxSize = D3D12_REQ_IMMEDIATE_CONSTANT_BUFFER_ELEMENT_COUNT * 16; - m_Desc.boundDescriptorSetMaxNum = D3D12_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT; + m_Desc.boundDescriptorSetMaxNum = BOUND_DESCRIPTOR_SET_MAX_NUM; m_Desc.perStageDescriptorSamplerMaxNum = D3D12_COMMONSHADER_SAMPLER_SLOT_COUNT; m_Desc.perStageDescriptorConstantBufferMaxNum = D3D12_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT; m_Desc.perStageDescriptorStorageBufferMaxNum = levels.MaxSupportedFeatureLevel >= D3D_FEATURE_LEVEL_11_1 ? D3D12_UAV_SLOT_COUNT : D3D12_PS_CS_UAV_REGISTER_COUNT; diff --git a/Source/D3D12/PipelineD3D12.cpp b/Source/D3D12/PipelineD3D12.cpp index 8b5ab56..10ef6e2 100644 --- a/Source/D3D12/PipelineD3D12.cpp +++ b/Source/D3D12/PipelineD3D12.cpp @@ -44,9 +44,9 @@ typedef PipelineDescComponent PipelineDepthStencilFormat; typedef PipelineDescComponent PipelineRenderTargetFormats; -static_assert((uint32_t)PrimitiveRestart::DISABLED == D3D12_INDEX_BUFFER_STRIP_CUT_VALUE_DISABLED, "Enum mismatch."); -static_assert((uint32_t)PrimitiveRestart::INDICES_UINT16 == D3D12_INDEX_BUFFER_STRIP_CUT_VALUE_0xFFFF, "Enum mismatch."); -static_assert((uint32_t)PrimitiveRestart::INDICES_UINT32 == D3D12_INDEX_BUFFER_STRIP_CUT_VALUE_0xFFFFFFFF, "Enum mismatch."); +static_assert((uint32_t)PrimitiveRestart::DISABLED == D3D12_INDEX_BUFFER_STRIP_CUT_VALUE_DISABLED, "Enum mismatch"); +static_assert((uint32_t)PrimitiveRestart::INDICES_UINT16 == D3D12_INDEX_BUFFER_STRIP_CUT_VALUE_0xFFFF, "Enum mismatch"); +static_assert((uint32_t)PrimitiveRestart::INDICES_UINT32 == D3D12_INDEX_BUFFER_STRIP_CUT_VALUE_0xFFFFFFFF, "Enum mismatch"); static void FillRasterizerState(D3D12_RASTERIZER_DESC& rasterizerDesc, const GraphicsPipelineDesc& graphicsPipelineDesc) { const RasterizationDesc& r = graphicsPipelineDesc.rasterization; diff --git a/Source/D3D12/SharedD3D12.cpp b/Source/D3D12/SharedD3D12.cpp index 863e6cf..7ed1d83 100644 --- a/Source/D3D12/SharedD3D12.cpp +++ b/Source/D3D12/SharedD3D12.cpp @@ -6,7 +6,7 @@ using namespace nri; -constexpr std::array COMMAND_LIST_TYPES = { +constexpr std::array COMMAND_LIST_TYPES = { D3D12_COMMAND_LIST_TYPE_DIRECT, // GRAPHICS, D3D12_COMMAND_LIST_TYPE_COMPUTE, // COMPUTE, D3D12_COMMAND_LIST_TYPE_COPY, // COPY, @@ -17,7 +17,7 @@ D3D12_COMMAND_LIST_TYPE nri::GetCommandListType(CommandQueueType commandQueueTyp return COMMAND_LIST_TYPES[(size_t)commandQueueType]; } -constexpr std::array RESOURCE_DIMENSIONS = { +constexpr std::array RESOURCE_DIMENSIONS = { D3D12_RESOURCE_DIMENSION_TEXTURE1D, // TEXTURE_1D, D3D12_RESOURCE_DIMENSION_TEXTURE2D, // TEXTURE_2D, D3D12_RESOURCE_DIMENSION_TEXTURE3D, // TEXTURE_3D, @@ -27,7 +27,7 @@ D3D12_RESOURCE_DIMENSION nri::GetResourceDimension(TextureType textureType) { return RESOURCE_DIMENSIONS[(size_t)textureType]; } -constexpr std::array DESCRIPTOR_RANGE_TYPES = { +constexpr std::array DESCRIPTOR_RANGE_TYPES = { D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER, // SAMPLER D3D12_DESCRIPTOR_RANGE_TYPE_CBV, // CONSTANT_BUFFER D3D12_DESCRIPTOR_RANGE_TYPE_SRV, // TEXTURE @@ -43,7 +43,7 @@ D3D12_DESCRIPTOR_RANGE_TYPE nri::GetDescriptorRangesType(DescriptorType descript return DESCRIPTOR_RANGE_TYPES[(size_t)descriptorType]; } -constexpr std::array PRIMITIVE_TOPOLOGY_TYPES = { +constexpr std::array PRIMITIVE_TOPOLOGY_TYPES = { D3D12_PRIMITIVE_TOPOLOGY_TYPE_POINT, // POINT_LIST D3D12_PRIMITIVE_TOPOLOGY_TYPE_LINE, // LINE_LIST D3D12_PRIMITIVE_TOPOLOGY_TYPE_LINE, // LINE_STRIP @@ -79,7 +79,7 @@ D3D_PRIMITIVE_TOPOLOGY nri::GetPrimitiveTopology(Topology topology, uint8_t tess return PRIMITIVE_TOPOLOGIES[(size_t)topology]; } -constexpr std::array FILL_MODES = { +constexpr std::array FILL_MODES = { D3D12_FILL_MODE_SOLID, // SOLID D3D12_FILL_MODE_WIREFRAME // WIREFRAME }; @@ -88,7 +88,7 @@ D3D12_FILL_MODE nri::GetFillMode(FillMode fillMode) { return FILL_MODES[(size_t)fillMode]; } -constexpr std::array CULL_MODES = { +constexpr std::array CULL_MODES = { D3D12_CULL_MODE_NONE, // NONE D3D12_CULL_MODE_FRONT, // FRONT D3D12_CULL_MODE_BACK // BACK @@ -98,7 +98,7 @@ D3D12_CULL_MODE nri::GetCullMode(CullMode cullMode) { return CULL_MODES[(size_t)cullMode]; } -constexpr std::array COMPARISON_FUNCS = { +constexpr std::array COMPARISON_FUNCS = { #ifdef NRI_USE_AGILITY_SDK D3D12_COMPARISON_FUNC_NONE, // NONE #else @@ -118,7 +118,7 @@ D3D12_COMPARISON_FUNC nri::GetComparisonFunc(CompareFunc compareFunc) { return COMPARISON_FUNCS[(size_t)compareFunc]; } -constexpr std::array STENCIL_OPS = { +constexpr std::array STENCIL_OPS = { D3D12_STENCIL_OP_KEEP, // KEEP D3D12_STENCIL_OP_ZERO, // ZERO D3D12_STENCIL_OP_REPLACE, // REPLACE @@ -133,7 +133,7 @@ D3D12_STENCIL_OP nri::GetStencilOp(StencilFunc stencilFunc) { return STENCIL_OPS[(size_t)stencilFunc]; } -constexpr std::array LOGIC_OPS = { +constexpr std::array LOGIC_OPS = { D3D12_LOGIC_OP_NOOP, // NONE D3D12_LOGIC_OP_CLEAR, // CLEAR D3D12_LOGIC_OP_AND, // AND @@ -156,7 +156,7 @@ D3D12_LOGIC_OP nri::GetLogicOp(LogicFunc logicFunc) { return LOGIC_OPS[(size_t)logicFunc]; } -constexpr std::array BLENDS = { +constexpr std::array BLENDS = { D3D12_BLEND_ZERO, // ZERO D3D12_BLEND_ONE, // ONE D3D12_BLEND_SRC_COLOR, // SRC_COLOR @@ -188,7 +188,7 @@ D3D12_BLEND nri::GetBlend(BlendFactor blendFactor) { return BLENDS[(size_t)blendFactor]; } -constexpr std::array BLEND_OPS = { +constexpr std::array BLEND_OPS = { D3D12_BLEND_OP_ADD, // ADD D3D12_BLEND_OP_SUBTRACT, // SUBTRACT D3D12_BLEND_OP_REV_SUBTRACT, // REVERSE_SUBTRACT @@ -200,7 +200,7 @@ D3D12_BLEND_OP nri::GetBlendOp(BlendFunc blendFunc) { return BLEND_OPS[(size_t)blendFunc]; } -constexpr std::array TEXTURE_ADDRESS_MODES = { +constexpr std::array TEXTURE_ADDRESS_MODES = { D3D12_TEXTURE_ADDRESS_MODE_WRAP, // REPEAT D3D12_TEXTURE_ADDRESS_MODE_MIRROR, // MIRRORED_REPEAT D3D12_TEXTURE_ADDRESS_MODE_CLAMP, // CLAMP_TO_EDGE @@ -211,7 +211,7 @@ D3D12_TEXTURE_ADDRESS_MODE nri::GetAddressMode(AddressMode addressMode) { return TEXTURE_ADDRESS_MODES[(size_t)addressMode]; } -constexpr std::array HEAP_TYPES = { +constexpr std::array HEAP_TYPES = { D3D12_HEAP_TYPE_DEFAULT, // DEVICE #ifdef NRI_USE_AGILITY_SDK D3D12_HEAP_TYPE_GPU_UPLOAD, // DEVICE_UPLOAD (Prerequisite: D3D12_FEATURE_D3D12_OPTIONS16) @@ -226,7 +226,7 @@ D3D12_HEAP_TYPE nri::GetHeapType(MemoryLocation memoryLocation) { return HEAP_TYPES[(size_t)memoryLocation]; } -constexpr std::array SHADING_RATES = { +constexpr std::array SHADING_RATES = { D3D12_SHADING_RATE_1X1, // _1x1, D3D12_SHADING_RATE_1X2, // _1x2, D3D12_SHADING_RATE_2X1, // _2x1, @@ -240,7 +240,7 @@ D3D12_SHADING_RATE nri::GetShadingRate(ShadingRate shadingRate) { return SHADING_RATES[(size_t)shadingRate]; } -constexpr std::array SHADING_RATE_COMBINERS = { +constexpr std::array SHADING_RATE_COMBINERS = { D3D12_SHADING_RATE_COMBINER_OVERRIDE, // REPLACE, D3D12_SHADING_RATE_COMBINER_PASSTHROUGH, // KEEP, D3D12_SHADING_RATE_COMBINER_MIN, // MIN, diff --git a/Source/D3D12/TextureD3D12.h b/Source/D3D12/TextureD3D12.h index efdd9d3..b92d933 100644 --- a/Source/D3D12/TextureD3D12.h +++ b/Source/D3D12/TextureD3D12.h @@ -35,8 +35,19 @@ struct TextureD3D12 { return m_Texture.GetInterface(); } - inline uint32_t GetSubresourceIndex(Dim_t layerOffset, Mip_t mipOffset) const { - return layerOffset * m_Desc.mipNum + mipOffset; + inline uint32_t GetSubresourceIndex(uint32_t layerOffset, uint32_t mipOffset, PlaneBits planes = PlaneBits::ALL) const { + // https://learn.microsoft.com/en-us/windows/win32/direct3d12/subresources#plane-slice + uint32_t planeIndex = 0; + if (planes != PlaneBits::ALL) { + if (planes & PlaneBits::DEPTH) + planeIndex = 0; + else if (planes & PlaneBits::STENCIL) + planeIndex = 1; + else + CHECK(false, "Bad plane"); + } + + return mipOffset + (layerOffset + planeIndex * m_Desc.layerNum) * m_Desc.mipNum; } inline Dim_t GetSize(Dim_t dimensionIndex, Mip_t mip = 0) const { diff --git a/Source/Shared/HelperDataUpload.cpp b/Source/Shared/HelperDataUpload.cpp index 740b0e2..8bf71c7 100644 --- a/Source/Shared/HelperDataUpload.cpp +++ b/Source/Shared/HelperDataUpload.cpp @@ -28,6 +28,7 @@ static void DoTransition(const CoreInterface& NRI, CommandBuffer* commandBuffer, barrier.layerNum = textureDesc.layerNum; barrier.before = isInitial ? initialState : state; barrier.after = isInitial ? state : textureUploadDesc.after; + barrier.planes = isInitial ? PlaneBits::ALL : textureUploadDesc.planes; } BarrierGroupDesc barrierGroup = {}; diff --git a/Source/Shared/SharedExternal.h b/Source/Shared/SharedExternal.h index f7a2a7f..c95f466 100644 --- a/Source/Shared/SharedExternal.h +++ b/Source/Shared/SharedExternal.h @@ -125,8 +125,8 @@ typedef uint32_t DXGI_FORMAT; constexpr uint32_t TIMEOUT_PRESENT = 1000; // 1 sec constexpr uint32_t TIMEOUT_FENCE = 5000; // 5 sec - constexpr uint64_t PRESENT_INDEX_BIT_NUM = 56ull; +constexpr uint32_t BOUND_DESCRIPTOR_SET_MAX_NUM = 32; // like in VK constexpr uint64_t MsToUs(uint32_t x) { return x * 1000000ull; diff --git a/Source/VK/CommandBufferVK.cpp b/Source/VK/CommandBufferVK.cpp index 5277897..1325ec2 100644 --- a/Source/VK/CommandBufferVK.cpp +++ b/Source/VK/CommandBufferVK.cpp @@ -158,58 +158,62 @@ inline void CommandBufferVK::SetShadingRate(const ShadingRateDesc& shadingRateDe } inline void CommandBufferVK::ClearAttachments(const ClearDesc* clearDescs, uint32_t clearDescNum, const Rect* rects, uint32_t rectNum) { + static_assert(sizeof(VkClearValue) == sizeof(ClearValue), "Sizeof mismatch"); + + if (!clearDescNum) + return; + + // Attachments + uint32_t attachmentNum = 0; VkClearAttachment* attachments = StackAlloc(VkClearAttachment, clearDescNum); for (uint32_t i = 0; i < clearDescNum; i++) { const ClearDesc& desc = clearDescs[i]; - VkClearAttachment& attachment = attachments[i]; - - switch (desc.attachmentContentType) { - case AttachmentContentType::COLOR: - attachment.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; - break; - case AttachmentContentType::DEPTH: - attachment.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT; - break; - case AttachmentContentType::STENCIL: - attachment.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT; - break; - case AttachmentContentType::DEPTH_STENCIL: - attachment.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT; - break; - default: - attachment.aspectMask = 0; - break; - } - attachment.colorAttachment = desc.colorAttachmentIndex; - memcpy(&attachment.clearValue, &desc.value, sizeof(VkClearValue)); + VkImageAspectFlags aspectMask = 0; + if (desc.planes & PlaneBits::COLOR) + aspectMask |= VK_IMAGE_ASPECT_COLOR_BIT; + if ((desc.planes & PlaneBits::DEPTH) && m_DepthStencil->IsDepthWritable()) + aspectMask |= VK_IMAGE_ASPECT_DEPTH_BIT; + if ((desc.planes & PlaneBits::STENCIL) && m_DepthStencil->IsStencilWritable()) + aspectMask |= VK_IMAGE_ASPECT_STENCIL_BIT; + + if (aspectMask) { + VkClearAttachment& attachment = attachments[attachmentNum++]; + + attachment = {}; + attachment.aspectMask = aspectMask; + attachment.colorAttachment = desc.colorAttachmentIndex; + attachment.clearValue = *(VkClearValue*)&desc.value; + } } + // Rects bool hasRects = rectNum != 0; if (!hasRects) - rectNum = clearDescNum; + rectNum = 1; VkClearRect* clearRects = StackAlloc(VkClearRect, rectNum); - if (hasRects) { - for (uint32_t i = 0; i < rectNum; i++) { + + for (uint32_t i = 0; i < rectNum; i++) { + VkClearRect& clearRect = clearRects[i]; + + clearRect = {}; + clearRect.baseArrayLayer = 0; + if (hasRects) { const Rect& rect = rects[i]; - VkClearRect& clearRect = clearRects[i]; - clearRect.baseArrayLayer = 0; clearRect.layerCount = 1; clearRect.rect = {{rect.x, rect.y}, {rect.width, rect.height}}; - } - } else { - for (uint32_t i = 0; i < rectNum; i++) { - VkClearRect& clearRect = clearRects[i]; - clearRect.baseArrayLayer = 0; + } else { clearRect.layerCount = m_RenderLayerNum; clearRect.rect = {{0, 0}, {m_RenderWidth, m_RenderHeight}}; } } - const auto& vk = m_Device.GetDispatchTable(); - vk.CmdClearAttachments(m_Handle, clearDescNum, attachments, rectNum, clearRects); + if (attachmentNum) { + const auto& vk = m_Device.GetDispatchTable(); + vk.CmdClearAttachments(m_Handle, attachmentNum, attachments, rectNum, clearRects); + } } inline void CommandBufferVK::ClearStorageBuffer(const ClearStorageBufferDesc& clearDesc) { @@ -239,11 +243,12 @@ inline void CommandBufferVK::BeginRendering(const AttachmentsDesc& attachmentsDe VkRenderingAttachmentInfo* colors = StackAlloc(VkRenderingAttachmentInfo, attachmentsDesc.colorNum); for (uint32_t i = 0; i < attachmentsDesc.colorNum; i++) { const DescriptorVK& descriptor = *(DescriptorVK*)attachmentsDesc.colors[i]; + const DescriptorTexDesc& desc = descriptor.GetTexDesc(); VkRenderingAttachmentInfo& color = colors[i]; color = {VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO}; color.imageView = descriptor.GetImageView(); - color.imageLayout = descriptor.GetImageLayout(); + color.imageLayout = descriptor.GetTexDesc().layout; color.resolveMode = VK_RESOLVE_MODE_NONE; color.resolveImageView = VK_NULL_HANDLE; color.resolveImageLayout = VK_IMAGE_LAYOUT_UNDEFINED; @@ -251,7 +256,6 @@ inline void CommandBufferVK::BeginRendering(const AttachmentsDesc& attachmentsDe color.storeOp = VK_ATTACHMENT_STORE_OP_STORE; color.clearValue = {}; - const DescriptorTextureDesc& desc = descriptor.GetTextureDesc(); Dim_t w = desc.texture->GetSize(0, desc.mipOffset); Dim_t h = desc.texture->GetSize(1, desc.mipOffset); @@ -265,9 +269,10 @@ inline void CommandBufferVK::BeginRendering(const AttachmentsDesc& attachmentsDe bool hasStencil = false; if (attachmentsDesc.depthStencil) { const DescriptorVK& descriptor = *(DescriptorVK*)attachmentsDesc.depthStencil; + const DescriptorTexDesc& desc = descriptor.GetTexDesc(); depthStencil.imageView = descriptor.GetImageView(); - depthStencil.imageLayout = descriptor.GetImageLayout(); + depthStencil.imageLayout = desc.layout; depthStencil.resolveMode = VK_RESOLVE_MODE_NONE; depthStencil.resolveImageView = VK_NULL_HANDLE; depthStencil.resolveImageLayout = VK_IMAGE_LAYOUT_UNDEFINED; @@ -275,7 +280,6 @@ inline void CommandBufferVK::BeginRendering(const AttachmentsDesc& attachmentsDe depthStencil.storeOp = VK_ATTACHMENT_STORE_OP_STORE; depthStencil.clearValue = {}; - const DescriptorTextureDesc& desc = descriptor.GetTextureDesc(); Dim_t w = desc.texture->GetSize(0, desc.mipOffset); Dim_t h = desc.texture->GetSize(1, desc.mipOffset); @@ -284,7 +288,10 @@ inline void CommandBufferVK::BeginRendering(const AttachmentsDesc& attachmentsDe m_RenderHeight = std::min(m_RenderHeight, h); hasStencil = HasStencil(descriptor.GetTexture().GetDesc().format); - } + + m_DepthStencil = &descriptor; + } else + m_DepthStencil = nullptr; // Shading rate VkRenderingFragmentShadingRateAttachmentInfoKHR shadingRate = {VK_STRUCTURE_TYPE_RENDERING_FRAGMENT_SHADING_RATE_ATTACHMENT_INFO_KHR}; @@ -293,7 +300,7 @@ inline void CommandBufferVK::BeginRendering(const AttachmentsDesc& attachmentsDe const DescriptorVK& descriptor = *(DescriptorVK*)attachmentsDesc.shadingRate; shadingRate.imageView = descriptor.GetImageView(); - shadingRate.imageLayout = descriptor.GetImageLayout(); + shadingRate.imageLayout = descriptor.GetTexDesc().layout; shadingRate.shadingRateAttachmentTexelSize = {tileSize, tileSize}; } @@ -325,6 +332,8 @@ inline void CommandBufferVK::BeginRendering(const AttachmentsDesc& attachmentsDe inline void CommandBufferVK::EndRendering() { const auto& vk = m_Device.GetDispatchTable(); vk.CmdEndRendering(m_Handle); + + m_DepthStencil = nullptr; } inline void CommandBufferVK::SetVertexBuffers(uint32_t baseSlot, uint32_t bufferNum, const Buffer* const* buffers, const uint64_t* offsets) { @@ -436,13 +445,11 @@ inline void CommandBufferVK::CopyTexture(Texture& dstTexture, const TextureRegio return; } - VkImageCopy region; + VkImageCopy region = {}; - if (srcRegionDesc != nullptr) { + if (srcRegionDesc) { region.srcSubresource = {srcTextureImpl.GetImageAspectFlags(), srcRegionDesc->mipOffset, srcRegionDesc->layerOffset, 1}; - region.srcOffset = {(int32_t)srcRegionDesc->x, (int32_t)srcRegionDesc->y, (int32_t)srcRegionDesc->z}; - region.extent = { (srcRegionDesc->width == WHOLE_SIZE) ? srcTextureImpl.GetSize(0, srcRegionDesc->mipOffset) : srcRegionDesc->width, (srcRegionDesc->height == WHOLE_SIZE) ? srcTextureImpl.GetSize(1, srcRegionDesc->mipOffset) : srcRegionDesc->height, @@ -450,18 +457,15 @@ inline void CommandBufferVK::CopyTexture(Texture& dstTexture, const TextureRegio }; } else { region.srcSubresource = {srcTextureImpl.GetImageAspectFlags(), 0, 0, 1}; - region.srcOffset = {}; region.extent = srcTextureImpl.GetExtent(); } - if (dstRegionDesc != nullptr) { + if (dstRegionDesc) { region.dstSubresource = {dstTextureImpl.GetImageAspectFlags(), dstRegionDesc->mipOffset, dstRegionDesc->layerOffset, 1}; - region.dstOffset = {(int32_t)dstRegionDesc->x, (int32_t)dstRegionDesc->y, (int32_t)dstRegionDesc->z}; } else { region.dstSubresource = {dstTextureImpl.GetImageAspectFlags(), 0, 0, 1}; - region.dstOffset = {}; } @@ -469,12 +473,11 @@ inline void CommandBufferVK::CopyTexture(Texture& dstTexture, const TextureRegio vk.CmdCopyImage(m_Handle, srcTextureImpl.GetHandle(), IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, dstTextureImpl.GetHandle(), IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion); } -inline void CommandBufferVK::UploadBufferToTexture( - Texture& dstTexture, const TextureRegionDesc& dstRegionDesc, const Buffer& srcBuffer, const TextureDataLayoutDesc& srcDataLayoutDesc) { +inline void CommandBufferVK::UploadBufferToTexture(Texture& dstTexture, const TextureRegionDesc& dstRegionDesc, const Buffer& srcBuffer, const TextureDataLayoutDesc& srcDataLayoutDesc) { const BufferVK& srcBufferImpl = (const BufferVK&)srcBuffer; const TextureVK& dstTextureImpl = (const TextureVK&)dstTexture; - const FormatProps& formatProps = GetFormatProps(dstTextureImpl.GetDesc().format); + const uint32_t rowBlockNum = srcDataLayoutDesc.rowPitch / formatProps.stride; const uint32_t bufferRowLength = rowBlockNum * formatProps.blockWidth; @@ -485,8 +488,17 @@ inline void CommandBufferVK::UploadBufferToTexture( srcDataLayoutDesc.offset, bufferRowLength, bufferImageHeight, - VkImageSubresourceLayers{dstTextureImpl.GetImageAspectFlags(), dstRegionDesc.mipOffset, dstRegionDesc.layerOffset, 1}, - VkOffset3D{dstRegionDesc.x, dstRegionDesc.y, dstRegionDesc.z}, + VkImageSubresourceLayers{ + dstTextureImpl.GetImageAspectFlags(), + dstRegionDesc.mipOffset, + dstRegionDesc.layerOffset, + 1, + }, + VkOffset3D{ + dstRegionDesc.x, + dstRegionDesc.y, + dstRegionDesc.z, + }, VkExtent3D{ (dstRegionDesc.width == WHOLE_SIZE) ? dstTextureImpl.GetSize(0, dstRegionDesc.mipOffset) : dstRegionDesc.width, (dstRegionDesc.height == WHOLE_SIZE) ? dstTextureImpl.GetSize(1, dstRegionDesc.mipOffset) : dstRegionDesc.height, @@ -498,8 +510,7 @@ inline void CommandBufferVK::UploadBufferToTexture( vk.CmdCopyBufferToImage(m_Handle, srcBufferImpl.GetHandle(), dstTextureImpl.GetHandle(), IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion); } -inline void CommandBufferVK::ReadbackTextureToBuffer( - Buffer& dstBuffer, TextureDataLayoutDesc& dstDataLayoutDesc, const Texture& srcTexture, const TextureRegionDesc& srcRegionDesc) { +inline void CommandBufferVK::ReadbackTextureToBuffer(Buffer& dstBuffer, TextureDataLayoutDesc& dstDataLayoutDesc, const Texture& srcTexture, const TextureRegionDesc& srcRegionDesc) { const TextureVK& srcTextureImpl = (const TextureVK&)srcTexture; const BufferVK& dstBufferImpl = (const BufferVK&)dstBuffer; @@ -627,6 +638,18 @@ inline void CommandBufferVK::Barrier(const BarrierGroupDesc& barrierGroupDesc) { const TextureBarrierDesc& in = barrierGroupDesc.textures[i]; const TextureVK& textureImpl = *(const TextureVK*)in.texture; + VkImageAspectFlags aspectFlags = 0; + if (in.planes == PlaneBits::ALL) + aspectFlags = textureImpl.GetImageAspectFlags(); + else { + if (in.planes & PlaneBits::COLOR) + aspectFlags |= VK_IMAGE_ASPECT_COLOR_BIT; + if (in.planes & PlaneBits::DEPTH) + aspectFlags |= VK_IMAGE_ASPECT_DEPTH_BIT; + if (in.planes & PlaneBits::STENCIL) + aspectFlags |= VK_IMAGE_ASPECT_STENCIL_BIT; + } + VkImageMemoryBarrier2& out = textureBarriers[i]; out = {VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2}; out.srcStageMask = GetPipelineStageFlags(in.before.stages); @@ -639,7 +662,7 @@ inline void CommandBufferVK::Barrier(const BarrierGroupDesc& barrierGroupDesc) { out.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; out.image = textureImpl.GetHandle(); out.subresourceRange = { - textureImpl.GetImageAspectFlags(), + aspectFlags, in.mipOffset, (in.mipNum == REMAINING_MIPS) ? VK_REMAINING_MIP_LEVELS : in.mipNum, in.layerOffset, @@ -730,8 +753,7 @@ inline void CommandBufferVK::CopyWholeTexture(const TextureVK& dstTexture, const vk.CmdCopyImage(m_Handle, srcTexture.GetHandle(), IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, dstTexture.GetHandle(), IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, dstTextureDesc.mipNum, regions); } -inline void CommandBufferVK::BuildTopLevelAccelerationStructure( - uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, AccelerationStructureBuildBits flags, AccelerationStructure& dst, Buffer& scratch, uint64_t scratchOffset) { +inline void CommandBufferVK::BuildTopLevelAccelerationStructure(uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, AccelerationStructureBuildBits flags, AccelerationStructure& dst, Buffer& scratch, uint64_t scratchOffset) { const VkAccelerationStructureKHR dstASHandle = ((const AccelerationStructureVK&)dst).GetHandle(); const VkDeviceAddress scratchAddress = ((BufferVK&)scratch).GetDeviceAddress() + scratchOffset; const VkDeviceAddress bufferAddress = ((BufferVK&)buffer).GetDeviceAddress() + bufferOffset; @@ -761,8 +783,7 @@ inline void CommandBufferVK::BuildTopLevelAccelerationStructure( vk.CmdBuildAccelerationStructuresKHR(m_Handle, 1, &buildGeometryInfo, rangeArrays); } -inline void CommandBufferVK::BuildBottomLevelAccelerationStructure( - uint32_t geometryObjectNum, const GeometryObject* geometryObjects, AccelerationStructureBuildBits flags, AccelerationStructure& dst, Buffer& scratch, uint64_t scratchOffset) { +inline void CommandBufferVK::BuildBottomLevelAccelerationStructure(uint32_t geometryObjectNum, const GeometryObject* geometryObjects, AccelerationStructureBuildBits flags, AccelerationStructure& dst, Buffer& scratch, uint64_t scratchOffset) { const VkAccelerationStructureKHR dstASHandle = ((const AccelerationStructureVK&)dst).GetHandle(); const VkDeviceAddress scratchAddress = ((BufferVK&)scratch).GetDeviceAddress() + scratchOffset; @@ -859,8 +880,7 @@ inline void CommandBufferVK::CopyAccelerationStructure(AccelerationStructure& ds vk.CmdCopyAccelerationStructureKHR(m_Handle, &info); } -inline void CommandBufferVK::WriteAccelerationStructureSize( - const AccelerationStructure* const* accelerationStructures, uint32_t accelerationStructureNum, QueryPool& queryPool, uint32_t queryPoolOffset) { +inline void CommandBufferVK::WriteAccelerationStructureSize(const AccelerationStructure* const* accelerationStructures, uint32_t accelerationStructureNum, QueryPool& queryPool, uint32_t queryPoolOffset) { Scratch ASes = AllocateScratch(m_Device, VkAccelerationStructureKHR, accelerationStructureNum); for (uint32_t i = 0; i < accelerationStructureNum; i++) diff --git a/Source/VK/CommandBufferVK.h b/Source/VK/CommandBufferVK.h index 803aa5f..46372ef 100644 --- a/Source/VK/CommandBufferVK.h +++ b/Source/VK/CommandBufferVK.h @@ -8,6 +8,7 @@ struct DeviceVK; struct PipelineVK; struct PipelineLayoutVK; struct TextureVK; +struct DescriptorVK; struct CommandBufferVK { inline CommandBufferVK(DeviceVK& device) @@ -34,14 +35,12 @@ struct CommandBufferVK { void SetDebugName(const char* name); Result Begin(const DescriptorPool* descriptorPool); Result End(); - void SetPipeline(const Pipeline& pipeline); void SetPipelineLayout(const PipelineLayout& pipelineLayout); void SetDescriptorSet(uint32_t setIndexInPipelineLayout, const DescriptorSet& descriptorSet, const uint32_t* dynamicConstantBufferOffsets); void SetConstants(uint32_t pushConstantIndex, const void* data, uint32_t size); void SetDescriptorPool(const DescriptorPool& descriptorPool); void Barrier(const BarrierGroupDesc& barrierGroupDesc); - void BeginRendering(const AttachmentsDesc& attachmentsDesc); void EndRendering(); void SetViewports(const Viewport* viewports, uint32_t viewportNum); @@ -54,7 +53,6 @@ struct CommandBufferVK { void ClearAttachments(const ClearDesc* clearDescs, uint32_t clearDescNum, const Rect* rects, uint32_t rectNum); void SetIndexBuffer(const Buffer& buffer, uint64_t offset, IndexType indexType); void SetVertexBuffers(uint32_t baseSlot, uint32_t bufferNum, const Buffer* const* buffers, const uint64_t* offsets); - void Draw(const DrawDesc& drawDesc); void DrawIndexed(const DrawIndexedDesc& drawIndexedDesc); void DrawIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride, const Buffer* countBuffer, uint64_t countBufferOffset); @@ -65,42 +63,22 @@ struct CommandBufferVK { void EndQuery(const QueryPool& queryPool, uint32_t offset); void BeginAnnotation(const char* name); void EndAnnotation(); - void ClearStorageBuffer(const ClearStorageBufferDesc& clearDesc); - void ClearStorageTexture(const ClearStorageTextureDesc& clearDesc); - void CopyBuffer(Buffer& dstBuffer, uint64_t dstOffset, const Buffer& srcBuffer, uint64_t srcOffset, uint64_t size); - void CopyTexture(Texture& dstTexture, const TextureRegionDesc* dstRegionDesc, const Texture& srcTexture, const TextureRegionDesc* srcRegionDesc); - void UploadBufferToTexture(Texture& dstTexture, const TextureRegionDesc& dstRegionDesc, const Buffer& srcBuffer, const TextureDataLayoutDesc& srcDataLayoutDesc); - void ReadbackTextureToBuffer(Buffer& dstBuffer, TextureDataLayoutDesc& dstDataLayoutDesc, const Texture& srcTexture, const TextureRegionDesc& srcRegionDesc); - void CopyQueries(const QueryPool& queryPool, uint32_t offset, uint32_t num, Buffer& dstBuffer, uint64_t dstOffset); void ResetQueries(const QueryPool& queryPool, uint32_t offset, uint32_t num); - - void BuildTopLevelAccelerationStructure(uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, AccelerationStructureBuildBits flags, AccelerationStructure& dst, - Buffer& scratch, uint64_t scratchOffset); - - void BuildBottomLevelAccelerationStructure(uint32_t geometryObjectNum, const GeometryObject* geometryObjects, AccelerationStructureBuildBits flags, AccelerationStructure& dst, - Buffer& scratch, uint64_t scratchOffset); - - void UpdateTopLevelAccelerationStructure(uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, AccelerationStructureBuildBits flags, AccelerationStructure& dst, - AccelerationStructure& src, Buffer& scratch, uint64_t scratchOffset); - - void UpdateBottomLevelAccelerationStructure(uint32_t geometryObjectNum, const GeometryObject* geometryObjects, AccelerationStructureBuildBits flags, AccelerationStructure& dst, - AccelerationStructure& src, Buffer& scratch, uint64_t scratchOffset); - + void BuildTopLevelAccelerationStructure(uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, AccelerationStructureBuildBits flags, AccelerationStructure& dst, Buffer& scratch, uint64_t scratchOffset); + void BuildBottomLevelAccelerationStructure(uint32_t geometryObjectNum, const GeometryObject* geometryObjects, AccelerationStructureBuildBits flags, AccelerationStructure& dst, Buffer& scratch, uint64_t scratchOffset); + void UpdateTopLevelAccelerationStructure(uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, AccelerationStructureBuildBits flags, AccelerationStructure& dst, AccelerationStructure& src, Buffer& scratch, uint64_t scratchOffset); + void UpdateBottomLevelAccelerationStructure(uint32_t geometryObjectNum, const GeometryObject* geometryObjects, AccelerationStructureBuildBits flags, AccelerationStructure& dst, AccelerationStructure& src, Buffer& scratch, uint64_t scratchOffset); void CopyAccelerationStructure(AccelerationStructure& dst, AccelerationStructure& src, CopyMode copyMode); - - void WriteAccelerationStructureSize( - const AccelerationStructure* const* accelerationStructures, uint32_t accelerationStructureNum, QueryPool& queryPool, uint32_t queryPoolOffset); - + void WriteAccelerationStructureSize(const AccelerationStructure* const* accelerationStructures, uint32_t accelerationStructureNum, QueryPool& queryPool, uint32_t queryPoolOffset); void DispatchRays(const DispatchRaysDesc& dispatchRaysDesc); void DispatchRaysIndirect(const Buffer& buffer, uint64_t offset); - void DrawMeshTasks(const DrawMeshTasksDesc& drawMeshTasksDesc); void DrawMeshTasksIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride); @@ -111,6 +89,7 @@ struct CommandBufferVK { DeviceVK& m_Device; const PipelineVK* m_CurrentPipeline = nullptr; const PipelineLayoutVK* m_CurrentPipelineLayout = nullptr; + const DescriptorVK* m_DepthStencil = nullptr; VkCommandBuffer m_Handle = VK_NULL_HANDLE; VkCommandPool m_CommandPool = VK_NULL_HANDLE; CommandQueueType m_Type = (CommandQueueType)0; diff --git a/Source/VK/ConversionVK.h b/Source/VK/ConversionVK.h index 939a020..b9f163c 100644 --- a/Source/VK/ConversionVK.h +++ b/Source/VK/ConversionVK.h @@ -4,16 +4,16 @@ namespace nri { -constexpr std::array INDEX_TYPE_TABLE = { +constexpr std::array INDEX_TYPE_TABLE = { VK_INDEX_TYPE_UINT16, // UINT16 VK_INDEX_TYPE_UINT32, // UINT32 }; constexpr VkIndexType GetIndexType(IndexType indexType) { - return INDEX_TYPE_TABLE[(uint32_t)indexType]; + return INDEX_TYPE_TABLE[(size_t)indexType]; } -constexpr std::array LAYOUT_TABLE = { +constexpr std::array LAYOUT_TABLE = { VK_IMAGE_LAYOUT_UNDEFINED, // UNKNOWN VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // COLOR_ATTACHMENT VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, // DEPTH_STENCIL_ATTACHMENT @@ -27,10 +27,10 @@ constexpr std::array LAYOUT_TABLE = { }; constexpr VkImageLayout GetImageLayout(Layout layout) { - return LAYOUT_TABLE[(uint32_t)layout]; + return LAYOUT_TABLE[(size_t)layout]; } -constexpr std::array DESCRIPTOR_TYPES = { +constexpr std::array DESCRIPTOR_TYPES = { VK_DESCRIPTOR_TYPE_SAMPLER, // SAMPLER VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, // CONSTANT_BUFFER VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, // TEXTURE @@ -43,10 +43,10 @@ constexpr std::array DESCRI }; constexpr VkDescriptorType GetDescriptorType(DescriptorType type) { - return DESCRIPTOR_TYPES[(uint32_t)type]; + return DESCRIPTOR_TYPES[(size_t)type]; } -constexpr std::array TOPOLOGIES = { +constexpr std::array TOPOLOGIES = { VK_PRIMITIVE_TOPOLOGY_POINT_LIST, // POINT_LIST VK_PRIMITIVE_TOPOLOGY_LINE_LIST, // LINE_LIST VK_PRIMITIVE_TOPOLOGY_LINE_STRIP, // LINE_STRIP @@ -60,29 +60,29 @@ constexpr std::array TOPOLOGIE }; constexpr VkPrimitiveTopology GetTopology(Topology topology) { - return TOPOLOGIES[(uint32_t)topology]; + return TOPOLOGIES[(size_t)topology]; } -constexpr std::array CULL_MODES = { +constexpr std::array CULL_MODES = { VK_CULL_MODE_NONE, // NONE VK_CULL_MODE_FRONT_BIT, // FRONT VK_CULL_MODE_BACK_BIT // BACK }; constexpr VkCullModeFlags GetCullMode(CullMode cullMode) { - return CULL_MODES[(uint32_t)cullMode]; + return CULL_MODES[(size_t)cullMode]; } -constexpr std::array POLYGON_MODES = { +constexpr std::array POLYGON_MODES = { VK_POLYGON_MODE_FILL, // SOLID VK_POLYGON_MODE_LINE, // WIREFRAME }; constexpr VkPolygonMode GetPolygonMode(FillMode fillMode) { - return POLYGON_MODES[(uint32_t)fillMode]; + return POLYGON_MODES[(size_t)fillMode]; } -constexpr std::array COMPARE_OP = { +constexpr std::array COMPARE_OP = { VK_COMPARE_OP_NEVER, // NONE VK_COMPARE_OP_ALWAYS, // ALWAYS VK_COMPARE_OP_NEVER, // NEVER @@ -95,10 +95,10 @@ constexpr std::array COMPARE_OP = { }; constexpr VkCompareOp GetCompareOp(CompareFunc compareFunc) { - return COMPARE_OP[(uint32_t)compareFunc]; + return COMPARE_OP[(size_t)compareFunc]; } -constexpr std::array STENCIL_OP = { +constexpr std::array STENCIL_OP = { VK_STENCIL_OP_KEEP, // KEEP, VK_STENCIL_OP_ZERO, // ZERO, VK_STENCIL_OP_REPLACE, // REPLACE, @@ -110,10 +110,10 @@ constexpr std::array STENCIL_OP = { }; constexpr VkStencilOp GetStencilOp(StencilFunc stencilFunc) { - return STENCIL_OP[(uint32_t)stencilFunc]; + return STENCIL_OP[(size_t)stencilFunc]; } -constexpr std::array LOGIC_OP = { +constexpr std::array LOGIC_OP = { VK_LOGIC_OP_MAX_ENUM, // NONE VK_LOGIC_OP_CLEAR, // CLEAR VK_LOGIC_OP_AND, // AND @@ -133,10 +133,10 @@ constexpr std::array LOGIC_OP = { }; constexpr VkLogicOp GetLogicOp(LogicFunc logicFunc) { - return LOGIC_OP[(uint32_t)logicFunc]; + return LOGIC_OP[(size_t)logicFunc]; } -constexpr std::array BLEND_FACTOR = { +constexpr std::array BLEND_FACTOR = { VK_BLEND_FACTOR_ZERO, // ZERO VK_BLEND_FACTOR_ONE, // ONE VK_BLEND_FACTOR_SRC_COLOR, // SRC_COLOR @@ -159,10 +159,10 @@ constexpr std::array BLEND_FACTOR }; constexpr VkBlendFactor GetBlendFactor(BlendFactor blendFactor) { - return BLEND_FACTOR[(uint32_t)blendFactor]; + return BLEND_FACTOR[(size_t)blendFactor]; } -constexpr std::array BLEND_OP = { +constexpr std::array BLEND_OP = { VK_BLEND_OP_ADD, // ADD VK_BLEND_OP_SUBTRACT, // SUBTRACT VK_BLEND_OP_REVERSE_SUBTRACT, // REVERSE_SUBTRACT @@ -171,38 +171,38 @@ constexpr std::array BLEND_OP = { }; constexpr VkBlendOp GetBlendOp(BlendFunc blendFunc) { - return BLEND_OP[(uint32_t)blendFunc]; + return BLEND_OP[(size_t)blendFunc]; } -constexpr std::array IMAGE_TYPES = { +constexpr std::array IMAGE_TYPES = { VK_IMAGE_TYPE_1D, // TEXTURE_1D VK_IMAGE_TYPE_2D, // TEXTURE_2D VK_IMAGE_TYPE_3D, // TEXTURE_3D }; constexpr VkImageType GetImageType(TextureType type) { - return IMAGE_TYPES[(uint32_t)type]; + return IMAGE_TYPES[(size_t)type]; } -constexpr std::array FILTER = { +constexpr std::array FILTER = { VK_FILTER_NEAREST, // NEAREST VK_FILTER_LINEAR, // LINEAR }; constexpr VkFilter GetFilter(Filter filter) { - return FILTER[(uint32_t)filter]; + return FILTER[(size_t)filter]; } -constexpr std::array SAMPLER_MIPMAP_MODE = { +constexpr std::array SAMPLER_MIPMAP_MODE = { VK_SAMPLER_MIPMAP_MODE_NEAREST, // NEAREST VK_SAMPLER_MIPMAP_MODE_LINEAR, // LINEAR }; constexpr VkSamplerMipmapMode GetSamplerMipmapMode(Filter filter) { - return SAMPLER_MIPMAP_MODE[(uint32_t)filter]; + return SAMPLER_MIPMAP_MODE[(size_t)filter]; } -constexpr std::array SAMPLER_ADDRESS_MODE = { +constexpr std::array SAMPLER_ADDRESS_MODE = { VK_SAMPLER_ADDRESS_MODE_REPEAT, // REPEAT VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT, // MIRRORED_REPEAT VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, // CLAMP_TO_EDGE @@ -210,23 +210,26 @@ constexpr std::array SAMPL }; constexpr VkSamplerAddressMode GetSamplerAddressMode(AddressMode addressMode) { - return SAMPLER_ADDRESS_MODE[(uint32_t)addressMode]; + return SAMPLER_ADDRESS_MODE[(size_t)addressMode]; } -constexpr std::array IMAGE_VIEW_TYPE_1D = { +constexpr std::array IMAGE_VIEW_TYPE_1D = { VK_IMAGE_VIEW_TYPE_1D, // SHADER_RESOURCE_1D, VK_IMAGE_VIEW_TYPE_1D_ARRAY, // SHADER_RESOURCE_1D_ARRAY, VK_IMAGE_VIEW_TYPE_1D, // SHADER_RESOURCE_STORAGE_1D, VK_IMAGE_VIEW_TYPE_1D_ARRAY, // SHADER_RESOURCE_STORAGE_1D_ARRAY, VK_IMAGE_VIEW_TYPE_1D, // COLOR_ATTACHMENT, VK_IMAGE_VIEW_TYPE_1D, // DEPTH_STENCIL_ATTACHMENT + VK_IMAGE_VIEW_TYPE_1D, // DEPTH_READONLY_STENCIL_ATTACHMENT, + VK_IMAGE_VIEW_TYPE_1D, // DEPTH_ATTACHMENT_STENCIL_READONLY, + VK_IMAGE_VIEW_TYPE_1D, // DEPTH_STENCIL_READONLY, }; constexpr VkImageViewType GetImageViewType(Texture1DViewType type) { - return IMAGE_VIEW_TYPE_1D[(uint32_t)type]; + return IMAGE_VIEW_TYPE_1D[(size_t)type]; } -constexpr std::array IMAGE_VIEW_TYPE_2D = { +constexpr std::array IMAGE_VIEW_TYPE_2D = { VK_IMAGE_VIEW_TYPE_2D, // SHADER_RESOURCE_2D, VK_IMAGE_VIEW_TYPE_2D_ARRAY, // SHADER_RESOURCE_2D_ARRAY, VK_IMAGE_VIEW_TYPE_CUBE, // SHADER_RESOURCE_CUBE, @@ -235,37 +238,43 @@ constexpr std::array IMAG VK_IMAGE_VIEW_TYPE_2D_ARRAY, // SHADER_RESOURCE_STORAGE_2D_ARRAY, VK_IMAGE_VIEW_TYPE_2D, // COLOR_ATTACHMENT, VK_IMAGE_VIEW_TYPE_2D, // DEPTH_STENCIL_ATTACHMENT + VK_IMAGE_VIEW_TYPE_2D, // DEPTH_READONLY_STENCIL_ATTACHMENT, + VK_IMAGE_VIEW_TYPE_2D, // DEPTH_ATTACHMENT_STENCIL_READONLY, + VK_IMAGE_VIEW_TYPE_2D, // DEPTH_STENCIL_READONLY, VK_IMAGE_VIEW_TYPE_2D, // SHADING_RATE_ATTACHMENT }; constexpr VkImageViewType GetImageViewType(Texture2DViewType type) { - return IMAGE_VIEW_TYPE_2D[(uint32_t)type]; + return IMAGE_VIEW_TYPE_2D[(size_t)type]; } -constexpr std::array IMAGE_VIEW_TYPE_3D = { +constexpr std::array IMAGE_VIEW_TYPE_3D = { VK_IMAGE_VIEW_TYPE_3D, // SHADER_RESOURCE_3D, VK_IMAGE_VIEW_TYPE_3D, // SHADER_RESOURCE_STORAGE_3D, VK_IMAGE_VIEW_TYPE_3D, // COLOR_ATTACHMENT }; constexpr VkImageViewType GetImageViewType(Texture3DViewType type) { - return IMAGE_VIEW_TYPE_3D[(uint32_t)type]; + return IMAGE_VIEW_TYPE_3D[(size_t)type]; } -constexpr std::array IMAGE_VIEW_USAGE_1D = { +constexpr std::array IMAGE_VIEW_USAGE_1D = { VK_IMAGE_USAGE_SAMPLED_BIT, // SHADER_RESOURCE_1D, VK_IMAGE_USAGE_SAMPLED_BIT, // SHADER_RESOURCE_1D_ARRAY, VK_IMAGE_USAGE_STORAGE_BIT, // SHADER_RESOURCE_STORAGE_1D, VK_IMAGE_USAGE_STORAGE_BIT, // SHADER_RESOURCE_STORAGE_1D_ARRAY, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, // COLOR_ATTACHMENT, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, // DEPTH_STENCIL_ATTACHMENT + VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, // DEPTH_READONLY_STENCIL_ATTACHMENT, + VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, // DEPTH_ATTACHMENT_STENCIL_READONLY, + VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, // DEPTH_STENCIL_READONLY, }; constexpr VkImageUsageFlags GetImageViewUsage(Texture1DViewType type) { - return IMAGE_VIEW_USAGE_1D[(uint32_t)type]; + return IMAGE_VIEW_USAGE_1D[(size_t)type]; } -constexpr std::array IMAGE_VIEW_USAGE_2D = { +constexpr std::array IMAGE_VIEW_USAGE_2D = { VK_IMAGE_USAGE_SAMPLED_BIT, // SHADER_RESOURCE_2D, VK_IMAGE_USAGE_SAMPLED_BIT, // SHADER_RESOURCE_2D_ARRAY, VK_IMAGE_USAGE_SAMPLED_BIT, // SHADER_RESOURCE_CUBE, @@ -274,48 +283,43 @@ constexpr std::array IM VK_IMAGE_USAGE_STORAGE_BIT, // SHADER_RESOURCE_STORAGE_2D_ARRAY, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, // COLOR_ATTACHMENT, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, // DEPTH_STENCIL_ATTACHMENT + VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, // DEPTH_READONLY_STENCIL_ATTACHMENT, + VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, // DEPTH_ATTACHMENT_STENCIL_READONLY, + VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, // DEPTH_STENCIL_READONLY, VK_IMAGE_USAGE_FRAGMENT_SHADING_RATE_ATTACHMENT_BIT_KHR, // SHADING_RATE_ATTACHMENT }; constexpr VkImageUsageFlags GetImageViewUsage(Texture2DViewType type) { - return IMAGE_VIEW_USAGE_2D[(uint32_t)type]; + return IMAGE_VIEW_USAGE_2D[(size_t)type]; } -constexpr std::array IMAGE_VIEW_USAGE_3D = { +constexpr std::array IMAGE_VIEW_USAGE_3D = { VK_IMAGE_USAGE_SAMPLED_BIT, // SHADER_RESOURCE_3D, VK_IMAGE_USAGE_STORAGE_BIT, // SHADER_RESOURCE_STORAGE_3D, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, // COLOR_ATTACHMENT }; constexpr VkImageUsageFlags GetImageViewUsage(Texture3DViewType type) { - return IMAGE_VIEW_USAGE_3D[(uint32_t)type]; -} - -constexpr std::array IMAGE_LAYOUT_1D = { - VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, // SHADER_RESOURCE_1D, - VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, // SHADER_RESOURCE_1D_ARRAY, - VK_IMAGE_LAYOUT_GENERAL, // SHADER_RESOURCE_STORAGE_1D, - VK_IMAGE_LAYOUT_GENERAL, // SHADER_RESOURCE_STORAGE_1D_ARRAY, - VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // COLOR_ATTACHMENT, - VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, // DEPTH_STENCIL_ATTACHMENT + return IMAGE_VIEW_USAGE_3D[(size_t)type]; +} + +constexpr std::array IMAGE_LAYOUT_1D = { + VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, // SHADER_RESOURCE_1D, + VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, // SHADER_RESOURCE_1D_ARRAY, + VK_IMAGE_LAYOUT_GENERAL, // SHADER_RESOURCE_STORAGE_1D, + VK_IMAGE_LAYOUT_GENERAL, // SHADER_RESOURCE_STORAGE_1D_ARRAY, + VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // COLOR_ATTACHMENT, + VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, // DEPTH_STENCIL_ATTACHMENT + VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL, // DEPTH_READONLY_STENCIL_ATTACHMENT, + VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL, // DEPTH_ATTACHMENT_STENCIL_READONLY, + VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL, // DEPTH_STENCIL_READONLY, }; -constexpr VkImageLayout GetImageLayoutForView(Texture1DViewType type, ResourceViewBits flags) { - if (type == Texture1DViewType::DEPTH_STENCIL_ATTACHMENT && flags != ResourceViewBits::NONE) { - if (flags == (ResourceViewBits::READONLY_DEPTH | ResourceViewBits::READONLY_STENCIL)) - return VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL; - else if (flags == ResourceViewBits::READONLY_DEPTH) - return VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL; - else if (flags == ResourceViewBits::READONLY_STENCIL) - return VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL_KHR; - else - CHECK(false, "Bad flags"); - } - - return IMAGE_LAYOUT_1D[(uint32_t)type]; +constexpr VkImageLayout GetImageLayoutForView(Texture1DViewType type) { + return IMAGE_LAYOUT_1D[(size_t)type]; } -constexpr std::array IMAGE_LAYOUT_2D = { +constexpr std::array IMAGE_LAYOUT_2D = { VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, // SHADER_RESOURCE_2D, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, // SHADER_RESOURCE_2D_ARRAY, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, // SHADER_RESOURCE_CUBE, @@ -324,35 +328,27 @@ constexpr std::array IMAGE_ VK_IMAGE_LAYOUT_GENERAL, // SHADER_RESOURCE_STORAGE_2D_ARRAY, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // COLOR_ATTACHMENT, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, // DEPTH_STENCIL_ATTACHMENT + VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL, // DEPTH_READONLY_STENCIL_ATTACHMENT, + VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL, // DEPTH_ATTACHMENT_STENCIL_READONLY, + VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL, // DEPTH_STENCIL_READONLY, VK_IMAGE_LAYOUT_FRAGMENT_SHADING_RATE_ATTACHMENT_OPTIMAL_KHR, // SHADING_RATE_ATTACHMENT }; -constexpr VkImageLayout GetImageLayoutForView(Texture2DViewType type, ResourceViewBits flags) { - if (type == Texture2DViewType::DEPTH_STENCIL_ATTACHMENT && flags != ResourceViewBits::NONE) { - if (flags == (ResourceViewBits::READONLY_DEPTH | ResourceViewBits::READONLY_STENCIL)) - return VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL; - else if (flags == ResourceViewBits::READONLY_DEPTH) - return VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL; - else if (flags == ResourceViewBits::READONLY_STENCIL) - return VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL_KHR; - else - CHECK(false, "Bad flags"); - } - - return IMAGE_LAYOUT_2D[(uint32_t)type]; +constexpr VkImageLayout GetImageLayoutForView(Texture2DViewType type) { + return IMAGE_LAYOUT_2D[(size_t)type]; } -constexpr std::array IMAGE_LAYOUT_3D = { +constexpr std::array IMAGE_LAYOUT_3D = { VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, // SHADER_RESOURCE_3D, VK_IMAGE_LAYOUT_GENERAL, // SHADER_RESOURCE_STORAGE_3D, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // COLOR_ATTACHMENT }; constexpr VkImageLayout GetImageLayoutForView(Texture3DViewType type) { - return IMAGE_LAYOUT_3D[(uint32_t)type]; + return IMAGE_LAYOUT_3D[(size_t)type]; } -constexpr std::array SHADING_RATE_COMBINER = { +constexpr std::array SHADING_RATE_COMBINER = { VK_FRAGMENT_SHADING_RATE_COMBINER_OP_REPLACE_KHR, // REPLACE, VK_FRAGMENT_SHADING_RATE_COMBINER_OP_KEEP_KHR, // KEEP, VK_FRAGMENT_SHADING_RATE_COMBINER_OP_MIN_KHR, // MIN, @@ -361,10 +357,10 @@ constexpr std::array TEXTURE_TYPE_TABLE = { +constexpr std::array TEXTURE_TYPE_TABLE = { TextureType::TEXTURE_1D, // VK_IMAGE_TYPE_1D TextureType::TEXTURE_2D, // VK_IMAGE_TYPE_2D TextureType::TEXTURE_3D, // VK_IMAGE_TYPE_3D @@ -372,7 +368,7 @@ constexpr std::array TEXTURE_TYPE_T constexpr TextureType GetTextureType(VkImageType type) { if ((size_t)type < TEXTURE_TYPE_TABLE.size()) - return TEXTURE_TYPE_TABLE[(uint32_t)type]; + return TEXTURE_TYPE_TABLE[(size_t)type]; return TextureType::MAX_NUM; } diff --git a/Source/VK/DescriptorSetVK.cpp b/Source/VK/DescriptorSetVK.cpp index 2a59812..10f4572 100644 --- a/Source/VK/DescriptorSetVK.cpp +++ b/Source/VK/DescriptorSetVK.cpp @@ -33,8 +33,7 @@ struct SlabAllocator { uint8_t* m_Memory; }; -static bool WriteTextures( - const DescriptorRangeDesc& rangeDesc, const DescriptorRangeUpdateDesc& update, uint32_t& descriptorOffset, VkWriteDescriptorSet& write, SlabAllocator& slab) { +static bool WriteTextures(const DescriptorRangeDesc& rangeDesc, const DescriptorRangeUpdateDesc& update, uint32_t& descriptorOffset, VkWriteDescriptorSet& write, SlabAllocator& slab) { const uint32_t totalItemNum = update.descriptorNum - descriptorOffset; uint32_t itemNumForWriting = totalItemNum; VkDescriptorImageInfo* infoArray = slab.Allocate(itemNumForWriting); @@ -43,7 +42,7 @@ static bool WriteTextures( const DescriptorVK& descriptorImpl = *(DescriptorVK*)update.descriptors[descriptorOffset + i]; infoArray[i].imageView = descriptorImpl.GetImageView(); - infoArray[i].imageLayout = descriptorImpl.GetImageLayout(); + infoArray[i].imageLayout = descriptorImpl.GetTexDesc().layout; infoArray[i].sampler = VK_NULL_HANDLE; } @@ -56,8 +55,7 @@ static bool WriteTextures( return itemNumForWriting == totalItemNum; } -static bool WriteTypedBuffers( - const DescriptorRangeDesc& rangeDesc, const DescriptorRangeUpdateDesc& update, uint32_t& descriptorOffset, VkWriteDescriptorSet& write, SlabAllocator& slab) { +static bool WriteTypedBuffers(const DescriptorRangeDesc& rangeDesc, const DescriptorRangeUpdateDesc& update, uint32_t& descriptorOffset, VkWriteDescriptorSet& write, SlabAllocator& slab) { const uint32_t totalItemNum = update.descriptorNum - descriptorOffset; uint32_t itemNumForWriting = totalItemNum; VkBufferView* viewArray = slab.Allocate(itemNumForWriting); @@ -76,8 +74,7 @@ static bool WriteTypedBuffers( return itemNumForWriting == totalItemNum; } -static bool WriteBuffers( - const DescriptorRangeDesc& rangeDesc, const DescriptorRangeUpdateDesc& update, uint32_t& descriptorOffset, VkWriteDescriptorSet& write, SlabAllocator& slab) { +static bool WriteBuffers(const DescriptorRangeDesc& rangeDesc, const DescriptorRangeUpdateDesc& update, uint32_t& descriptorOffset, VkWriteDescriptorSet& write, SlabAllocator& slab) { const uint32_t totalItemNum = update.descriptorNum - descriptorOffset; uint32_t itemNumForWriting = totalItemNum; VkDescriptorBufferInfo* infoArray = slab.Allocate(itemNumForWriting); @@ -96,8 +93,7 @@ static bool WriteBuffers( return itemNumForWriting == totalItemNum; } -static bool WriteSamplers( - const DescriptorRangeDesc& rangeDesc, const DescriptorRangeUpdateDesc& update, uint32_t& descriptorOffset, VkWriteDescriptorSet& write, SlabAllocator& slab) { +static bool WriteSamplers(const DescriptorRangeDesc& rangeDesc, const DescriptorRangeUpdateDesc& update, uint32_t& descriptorOffset, VkWriteDescriptorSet& write, SlabAllocator& slab) { MaybeUnused(rangeDesc); const uint32_t totalItemNum = update.descriptorNum - descriptorOffset; @@ -120,8 +116,7 @@ static bool WriteSamplers( return itemNumForWriting == totalItemNum; } -static bool WriteAccelerationStructures( - const DescriptorRangeDesc& rangeDesc, const DescriptorRangeUpdateDesc& update, uint32_t& descriptorOffset, VkWriteDescriptorSet& write, SlabAllocator& slab) { +static bool WriteAccelerationStructures(const DescriptorRangeDesc& rangeDesc, const DescriptorRangeUpdateDesc& update, uint32_t& descriptorOffset, VkWriteDescriptorSet& write, SlabAllocator& slab) { MaybeUnused(rangeDesc); const uint32_t totalItemNum = update.descriptorNum - descriptorOffset; @@ -154,8 +149,7 @@ static bool WriteAccelerationStructures( return itemNumForWriting == totalItemNum; } -typedef bool (*WriteDescriptorsFunc)( - const DescriptorRangeDesc& rangeDesc, const DescriptorRangeUpdateDesc& update, uint32_t& descriptorOffset, VkWriteDescriptorSet& write, SlabAllocator& slab); +typedef bool (*WriteDescriptorsFunc)(const DescriptorRangeDesc& rangeDesc, const DescriptorRangeUpdateDesc& update, uint32_t& descriptorOffset, VkWriteDescriptorSet& write, SlabAllocator& slab); constexpr std::array WRITE_FUNCS = { (WriteDescriptorsFunc)&WriteSamplers, // SAMPLER diff --git a/Source/VK/DescriptorVK.cpp b/Source/VK/DescriptorVK.cpp index 2f5d389..5a6014d 100644 --- a/Source/VK/DescriptorVK.cpp +++ b/Source/VK/DescriptorVK.cpp @@ -63,7 +63,7 @@ Result DescriptorVK::CreateTextureView(const T& textureViewDesc) { m_Format = createInfo.format; m_TextureDesc.handle = texture.GetHandle(); m_TextureDesc.texture = &texture; - m_TextureDesc.layout = GetImageLayoutForView(textureViewDesc.viewType, textureViewDesc.flags); + m_TextureDesc.layout = GetImageLayoutForView(textureViewDesc.viewType); m_TextureDesc.aspectFlags = GetImageAspectFlags(textureViewDesc.format); m_TextureDesc.layerOffset = textureViewDesc.layerOffset; m_TextureDesc.layerNum = (Dim_t)subresource.layerCount; diff --git a/Source/VK/DescriptorVK.h b/Source/VK/DescriptorVK.h index bd7772b..5cd481f 100644 --- a/Source/VK/DescriptorVK.h +++ b/Source/VK/DescriptorVK.h @@ -15,13 +15,13 @@ enum class DescriptorTypeVK { ACCELERATION_STRUCTURE }; -struct DescriptorBufferDesc { +struct DescriptorBufDesc { VkBuffer handle; uint64_t offset; uint64_t size; }; -struct DescriptorTextureDesc { +struct DescriptorTexDesc { VkImage handle; const TextureVK* texture; VkImageLayout layout; @@ -79,18 +79,22 @@ struct DescriptorVK { return m_Format; } - inline VkImageLayout GetImageLayout() const { - return m_TextureDesc.layout; - } - - inline const DescriptorTextureDesc& GetTextureDesc() const { + inline const DescriptorTexDesc& GetTexDesc() const { return m_TextureDesc; } - inline const DescriptorBufferDesc& GetBufferDesc() const { + inline const DescriptorBufDesc& GetBufDesc() const { return m_BufferDesc; } + inline bool IsDepthWritable() const { + return m_TextureDesc.layout != VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL && m_TextureDesc.layout != VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL; + } + + inline bool IsStencilWritable() const { + return m_TextureDesc.layout != VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL && m_TextureDesc.layout != VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL; + } + inline void GetBufferInfo(VkDescriptorBufferInfo& info) const { info.buffer = m_BufferDesc.handle; info.offset = m_BufferDesc.offset; @@ -128,15 +132,15 @@ struct DescriptorVK { DeviceVK& m_Device; union { - VkBufferView m_BufferView = VK_NULL_HANDLE; - VkImageView m_ImageView; + VkImageView m_ImageView = VK_NULL_HANDLE; + VkBufferView m_BufferView; VkAccelerationStructureKHR m_AccelerationStructure; VkSampler m_Sampler; }; union { - DescriptorBufferDesc m_BufferDesc; - DescriptorTextureDesc m_TextureDesc = {}; + DescriptorTexDesc m_TextureDesc = {}; + DescriptorBufDesc m_BufferDesc; }; DescriptorTypeVK m_Type = DescriptorTypeVK::NONE; diff --git a/Source/Validation/CommandBufferVal.cpp b/Source/Validation/CommandBufferVal.cpp index f366626..26a74ab 100644 --- a/Source/Validation/CommandBufferVal.cpp +++ b/Source/Validation/CommandBufferVal.cpp @@ -61,6 +61,9 @@ Result CommandBufferVal::Begin(const DescriptorPool* descriptorPool) { m_IsRecordingStarted = true; m_ValidationCommands.clear(); + m_Pipeline = nullptr; + + ResetAttachments(); return result; } @@ -74,7 +77,6 @@ Result CommandBufferVal::End() { REPORT_ERROR(&m_Device, "'CmdEndAnnotation' is called more times than 'CmdBeginAnnotation'"); Result result = GetCoreInterface().EndCommandBuffer(*GetImpl()); - if (result == Result::SUCCESS) m_IsRecordingStarted = m_IsWrapped; @@ -84,10 +86,10 @@ Result CommandBufferVal::End() { void CommandBufferVal::SetViewports(const Viewport* viewports, uint32_t viewportNum) { RETURN_ON_FAILURE(&m_Device, m_IsRecordingStarted, ReturnVoid(), "the command buffer must be in the recording state"); - if (viewportNum == 0) + if (!viewportNum) return; - RETURN_ON_FAILURE(&m_Device, viewports != nullptr, ReturnVoid(), "'viewports' is NULL"); + RETURN_ON_FAILURE(&m_Device, viewports, ReturnVoid(), "'viewports' is NULL"); GetCoreInterface().CmdSetViewports(*GetImpl(), viewports, viewportNum); } @@ -95,10 +97,10 @@ void CommandBufferVal::SetViewports(const Viewport* viewports, uint32_t viewport void CommandBufferVal::SetScissors(const Rect* rects, uint32_t rectNum) { RETURN_ON_FAILURE(&m_Device, m_IsRecordingStarted, ReturnVoid(), "the command buffer must be in the recording state"); - if (rectNum == 0) + if (!rectNum) return; - RETURN_ON_FAILURE(&m_Device, rects != nullptr, ReturnVoid(), "'rects' is NULL"); + RETURN_ON_FAILURE(&m_Device, rects, ReturnVoid(), "'rects' is NULL"); GetCoreInterface().CmdSetScissors(*GetImpl(), rects, rectNum); } @@ -140,13 +142,29 @@ void CommandBufferVal::ClearAttachments(const ClearDesc* clearDescs, uint32_t cl RETURN_ON_FAILURE(&m_Device, m_IsRecordingStarted, ReturnVoid(), "the command buffer must be in the recording state"); RETURN_ON_FAILURE(&m_Device, m_IsRenderPass, ReturnVoid(), "must be called inside 'CmdBeginRendering/CmdEndRendering'"); + const DeviceDesc& deviceDesc = m_Device.GetDesc(); + for (uint32_t i = 0; i < clearDescNum; i++) { + RETURN_ON_FAILURE(&m_Device, (clearDescs[i].planes & (PlaneBits::COLOR | PlaneBits::DEPTH | PlaneBits::STENCIL)) != 0, ReturnVoid(), "'clearDesc[%u].planes' is not COLOR, DEPTH or STENCIL", i); + + if (clearDescs[i].planes & PlaneBits::COLOR) { + RETURN_ON_FAILURE(&m_Device, clearDescs[i].colorAttachmentIndex < deviceDesc.colorAttachmentMaxNum, ReturnVoid(), "'clearDesc[%u].colorAttachmentIndex = %u' is out of bounds", i, clearDescs[i].colorAttachmentIndex); + RETURN_ON_FAILURE(&m_Device, m_RenderTargets[clearDescs[i].colorAttachmentIndex], ReturnVoid(), "'clearDesc[%u].colorAttachmentIndex = %u' references a NULL COLOR attachment", i, clearDescs[i].colorAttachmentIndex); + } + + if (clearDescs[i].planes & (PlaneBits::DEPTH | PlaneBits::STENCIL)) + RETURN_ON_FAILURE(&m_Device, m_DepthStencil, ReturnVoid(), "DEPTH_STENCIL attachment is NULL", i); + + if (clearDescs[i].colorAttachmentIndex != 0) + RETURN_ON_FAILURE(&m_Device, (clearDescs[i].planes & PlaneBits::COLOR), ReturnVoid(), "'clearDesc[%u].planes' is not COLOR, but `colorAttachmentIndex != 0`", i); + } + GetCoreInterface().CmdClearAttachments(*GetImpl(), clearDescs, clearDescNum, rects, rectNum); } void CommandBufferVal::ClearStorageBuffer(const ClearStorageBufferDesc& clearDesc) { RETURN_ON_FAILURE(&m_Device, m_IsRecordingStarted, ReturnVoid(), "the command buffer must be in the recording state"); RETURN_ON_FAILURE(&m_Device, !m_IsRenderPass, ReturnVoid(), "must be called outside of 'CmdBeginRendering/CmdEndRendering'"); - RETURN_ON_FAILURE(&m_Device, clearDesc.storageBuffer != nullptr, ReturnVoid(), "'clearDesc.storageBuffer' is NULL"); + RETURN_ON_FAILURE(&m_Device, clearDesc.storageBuffer, ReturnVoid(), "'clearDesc.storageBuffer' is NULL"); auto clearDescImpl = clearDesc; clearDescImpl.storageBuffer = NRI_GET_IMPL(Descriptor, clearDesc.storageBuffer); @@ -157,7 +175,7 @@ void CommandBufferVal::ClearStorageBuffer(const ClearStorageBufferDesc& clearDes void CommandBufferVal::ClearStorageTexture(const ClearStorageTextureDesc& clearDesc) { RETURN_ON_FAILURE(&m_Device, m_IsRecordingStarted, ReturnVoid(), "the command buffer must be in the recording state"); RETURN_ON_FAILURE(&m_Device, !m_IsRenderPass, ReturnVoid(), "must be called outside of 'CmdBeginRendering/CmdEndRendering'"); - RETURN_ON_FAILURE(&m_Device, clearDesc.storageTexture != nullptr, ReturnVoid(), "'clearDesc.storageTexture' is NULL"); + RETURN_ON_FAILURE(&m_Device, clearDesc.storageTexture, ReturnVoid(), "'clearDesc.storageTexture' is NULL"); auto clearDescImpl = clearDesc; clearDescImpl.storageTexture = NRI_GET_IMPL(Descriptor, clearDesc.storageTexture); @@ -179,18 +197,34 @@ void CommandBufferVal::BeginRendering(const AttachmentsDesc& attachmentsDesc) { attachmentsDescImpl.colors = colors; attachmentsDescImpl.colorNum = attachmentsDesc.colorNum; - GetCoreInterface().CmdBeginRendering(*GetImpl(), attachmentsDescImpl); - m_IsRenderPass = true; + m_RenderTargetNum = attachmentsDesc.colors ? attachmentsDesc.colorNum : 0; + + size_t i = 0; + for (; i < m_RenderTargetNum; i++) + m_RenderTargets[i] = (DescriptorVal*)attachmentsDesc.colors[i]; + for (; i < m_RenderTargets.size(); i++) + m_RenderTargets[i] = nullptr; + + if (attachmentsDesc.depthStencil) + m_DepthStencil = (DescriptorVal*)attachmentsDesc.depthStencil; + else + m_DepthStencil = nullptr; + + ValidateReadonlyDepthStencil(); + + GetCoreInterface().CmdBeginRendering(*GetImpl(), attachmentsDescImpl); } void CommandBufferVal::EndRendering() { RETURN_ON_FAILURE(&m_Device, m_IsRecordingStarted, ReturnVoid(), "the command buffer must be in the recording state"); RETURN_ON_FAILURE(&m_Device, m_IsRenderPass, ReturnVoid(), "'CmdBeginRendering' has not been called"); - GetCoreInterface().CmdEndRendering(*GetImpl()); - m_IsRenderPass = false; + + ResetAttachments(); + + GetCoreInterface().CmdEndRendering(*GetImpl()); } void CommandBufferVal::SetVertexBuffers(uint32_t baseSlot, uint32_t bufferNum, const Buffer* const* buffers, const uint64_t* offsets) { @@ -224,6 +258,9 @@ void CommandBufferVal::SetPipeline(const Pipeline& pipeline) { Pipeline* pipelineImpl = NRI_GET_IMPL(Pipeline, &pipeline); + m_Pipeline = (PipelineVal*)&pipeline; + ValidateReadonlyDepthStencil(); + GetCoreInterface().CmdSetPipeline(*GetImpl(), *pipelineImpl); } @@ -495,7 +532,7 @@ void CommandBufferVal::BuildBottomLevelAccelerationStructure( RETURN_ON_FAILURE(&m_Device, m_IsRecordingStarted, ReturnVoid(), "the command buffer must be in the recording state"); RETURN_ON_FAILURE(&m_Device, !m_IsRenderPass, ReturnVoid(), "must be called outside of 'CmdBeginRendering/CmdEndRendering'"); - RETURN_ON_FAILURE(&m_Device, geometryObjects != nullptr, ReturnVoid(), "'geometryObjects' is NULL"); + RETURN_ON_FAILURE(&m_Device, geometryObjects, ReturnVoid(), "'geometryObjects' is NULL"); RETURN_ON_FAILURE(&m_Device, scratchOffset < scratchVal.GetDesc().size, ReturnVoid(), "'scratchOffset = %llu' is out of bounds", scratchOffset); AccelerationStructure& dstImpl = *NRI_GET_IMPL(AccelerationStructure, &dst); @@ -530,7 +567,7 @@ void CommandBufferVal::UpdateBottomLevelAccelerationStructure(uint32_t geometryO AccelerationStructure& dst, AccelerationStructure& src, Buffer& scratch, uint64_t scratchOffset) { RETURN_ON_FAILURE(&m_Device, m_IsRecordingStarted, ReturnVoid(), "the command buffer must be in the recording state"); RETURN_ON_FAILURE(&m_Device, !m_IsRenderPass, ReturnVoid(), "must be called outside of 'CmdBeginRendering/CmdEndRendering'"); - RETURN_ON_FAILURE(&m_Device, geometryObjects != nullptr, ReturnVoid(), "'geometryObjects' is NULL"); + RETURN_ON_FAILURE(&m_Device, geometryObjects, ReturnVoid(), "'geometryObjects' is NULL"); BufferVal& scratchVal = (BufferVal&)scratch; @@ -561,11 +598,11 @@ void CommandBufferVal::WriteAccelerationStructureSize( const AccelerationStructure* const* accelerationStructures, uint32_t accelerationStructureNum, QueryPool& queryPool, uint32_t queryOffset) { RETURN_ON_FAILURE(&m_Device, m_IsRecordingStarted, ReturnVoid(), "the command buffer must be in the recording state"); RETURN_ON_FAILURE(&m_Device, !m_IsRenderPass, ReturnVoid(), "must be called outside of 'CmdBeginRendering/CmdEndRendering'"); - RETURN_ON_FAILURE(&m_Device, accelerationStructures != nullptr, ReturnVoid(), "'accelerationStructures' is NULL"); + RETURN_ON_FAILURE(&m_Device, accelerationStructures, ReturnVoid(), "'accelerationStructures' is NULL"); AccelerationStructure** accelerationStructureArray = StackAlloc(AccelerationStructure*, accelerationStructureNum); for (uint32_t i = 0; i < accelerationStructureNum; i++) { - RETURN_ON_FAILURE(&m_Device, accelerationStructures[i] != nullptr, ReturnVoid(), "'accelerationStructures[%u]' is NULL", i); + RETURN_ON_FAILURE(&m_Device, accelerationStructures[i], ReturnVoid(), "'accelerationStructures[%u]' is NULL", i); accelerationStructureArray[i] = NRI_GET_IMPL(AccelerationStructure, accelerationStructures[i]); } @@ -579,7 +616,7 @@ void CommandBufferVal::DispatchRays(const DispatchRaysDesc& dispatchRaysDesc) { uint64_t align = m_Device.GetDesc().rayTracingShaderTableAlignment; RETURN_ON_FAILURE(&m_Device, m_IsRecordingStarted, ReturnVoid(), "the command buffer must be in the recording state"); RETURN_ON_FAILURE(&m_Device, !m_IsRenderPass, ReturnVoid(), "must be called outside of 'CmdBeginRendering/CmdEndRendering'"); - RETURN_ON_FAILURE(&m_Device, dispatchRaysDesc.raygenShader.buffer != nullptr, ReturnVoid(), "'dispatchRaysDesc.raygenShader.buffer' is NULL"); + RETURN_ON_FAILURE(&m_Device, dispatchRaysDesc.raygenShader.buffer, ReturnVoid(), "'dispatchRaysDesc.raygenShader.buffer' is NULL"); RETURN_ON_FAILURE(&m_Device, dispatchRaysDesc.raygenShader.size != 0, ReturnVoid(), "'dispatchRaysDesc.raygenShader.size' is 0"); RETURN_ON_FAILURE(&m_Device, dispatchRaysDesc.raygenShader.offset % align == 0, ReturnVoid(), "'dispatchRaysDesc.raygenShader.offset' is misaligned"); RETURN_ON_FAILURE(&m_Device, dispatchRaysDesc.missShaders.offset % align == 0, ReturnVoid(), "'dispatchRaysDesc.missShaders.offset' is misaligned"); @@ -615,12 +652,22 @@ void CommandBufferVal::DrawMeshTasksIndirect(const Buffer& buffer, uint64_t offs RETURN_ON_FAILURE(&m_Device, m_IsRenderPass, ReturnVoid(), "must be called inside 'CmdBeginRendering/CmdEndRendering'"); const BufferDesc& bufferDesc = ((BufferVal&)buffer).GetDesc(); - RETURN_ON_FAILURE(&m_Device, offset < bufferDesc.size, ReturnVoid(), "Cmdoffset is greater than the buffer size"); + RETURN_ON_FAILURE(&m_Device, offset < bufferDesc.size, ReturnVoid(), "'offset' is greater than the buffer size"); Buffer* bufferImpl = NRI_GET_IMPL(Buffer, &buffer); GetMeshShaderInterface().CmdDrawMeshTasksIndirect(*GetImpl(), *bufferImpl, offset, drawNum, stride); } +void CommandBufferVal::ValidateReadonlyDepthStencil() { + if (m_Pipeline && m_DepthStencil) { + if (m_DepthStencil->IsDepthReadonly() && m_Pipeline->WritesToDepth()) + REPORT_WARNING(&m_Device, "Depth is read-only, but the pipeline writes to depth. Writing happens only in VK!"); + + if (m_DepthStencil->IsStencilReadonly() && m_Pipeline->WritesToStencil()) + REPORT_WARNING(&m_Device, "Stencil is read-only, but the pipeline writes to stencil. Writing happens only in VK!"); + } +} + template Command& CommandBufferVal::AllocateValidationCommand() { const size_t commandSize = sizeof(Command); diff --git a/Source/Validation/CommandBufferVal.h b/Source/Validation/CommandBufferVal.h index cbca855..0ebd90e 100644 --- a/Source/Validation/CommandBufferVal.h +++ b/Source/Validation/CommandBufferVal.h @@ -4,6 +4,9 @@ namespace nri { +struct DescriptorVal; +struct PipelineVal; + struct CommandBufferVal : public DeviceObjectVal { CommandBufferVal(DeviceVal& device, CommandBuffer* commandBuffer, bool isWrapped) : DeviceObjectVal(device, commandBuffer) @@ -20,6 +23,14 @@ struct CommandBufferVal : public DeviceObjectVal { return GetCoreInterface().GetCommandBufferNativeObject(*GetImpl()); } + inline void ResetAttachments() { + m_RenderTargetNum = 0; + for (size_t i = 0; i < m_RenderTargets.size(); i++) + m_RenderTargets[i] = nullptr; + + m_DepthStencil = nullptr; + } + //================================================================================================================ // NRI //================================================================================================================ @@ -62,30 +73,27 @@ struct CommandBufferVal : public DeviceObjectVal { void ResetQueries(const QueryPool& queryPool, uint32_t offset, uint32_t num); void BeginAnnotation(const char* name); void EndAnnotation(); - - void BuildTopLevelAccelerationStructure(uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, AccelerationStructureBuildBits flags, AccelerationStructure& dst, - Buffer& scratch, uint64_t scratchOffset); - void BuildBottomLevelAccelerationStructure(uint32_t geometryObjectNum, const GeometryObject* geometryObjects, AccelerationStructureBuildBits flags, AccelerationStructure& dst, - Buffer& scratch, uint64_t scratchOffset); - void UpdateTopLevelAccelerationStructure(uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, AccelerationStructureBuildBits flags, AccelerationStructure& dst, - AccelerationStructure& src, Buffer& scratch, uint64_t scratchOffset); - void UpdateBottomLevelAccelerationStructure(uint32_t geometryObjectNum, const GeometryObject* geometryObjects, AccelerationStructureBuildBits flags, AccelerationStructure& dst, - AccelerationStructure& src, Buffer& scratch, uint64_t scratchOffset); - + void BuildTopLevelAccelerationStructure(uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, AccelerationStructureBuildBits flags, AccelerationStructure& dst, Buffer& scratch, uint64_t scratchOffset); + void BuildBottomLevelAccelerationStructure(uint32_t geometryObjectNum, const GeometryObject* geometryObjects, AccelerationStructureBuildBits flags, AccelerationStructure& dst, Buffer& scratch, uint64_t scratchOffset); + void UpdateTopLevelAccelerationStructure(uint32_t instanceNum, const Buffer& buffer, uint64_t bufferOffset, AccelerationStructureBuildBits flags, AccelerationStructure& dst, AccelerationStructure& src, Buffer& scratch, uint64_t scratchOffset); + void UpdateBottomLevelAccelerationStructure(uint32_t geometryObjectNum, const GeometryObject* geometryObjects, AccelerationStructureBuildBits flags, AccelerationStructure& dst, AccelerationStructure& src, Buffer& scratch, uint64_t scratchOffset); void CopyAccelerationStructure(AccelerationStructure& dst, AccelerationStructure& src, CopyMode copyMode); void WriteAccelerationStructureSize(const AccelerationStructure* const* accelerationStructures, uint32_t accelerationStructureNum, QueryPool& queryPool, uint32_t queryOffset); - void DispatchRays(const DispatchRaysDesc& dispatchRaysDesc); void DispatchRaysIndirect(const Buffer& buffer, uint64_t offset); - void DrawMeshTasks(const DrawMeshTasksDesc& drawMeshTasksDesc); void DrawMeshTasksIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride); private: template Command& AllocateValidationCommand(); + void ValidateReadonlyDepthStencil(); Vector m_ValidationCommands; + std::array m_RenderTargets = {}; + DescriptorVal* m_DepthStencil = nullptr; + PipelineVal* m_Pipeline = nullptr; + uint32_t m_RenderTargetNum = 0; int32_t m_AnnotationStack = 0; bool m_IsRecordingStarted = false; bool m_IsWrapped = false; diff --git a/Source/Validation/DescriptorVal.cpp b/Source/Validation/DescriptorVal.cpp index 1bc91d3..cef58ec 100644 --- a/Source/Validation/DescriptorVal.cpp +++ b/Source/Validation/DescriptorVal.cpp @@ -47,7 +47,12 @@ DescriptorVal::DescriptorVal(DeviceVal& device, Descriptor* descriptor, const Te m_ResourceViewType = ResourceViewType::COLOR_ATTACHMENT; break; case Texture1DViewType::DEPTH_STENCIL_ATTACHMENT: + case Texture1DViewType::DEPTH_READONLY_STENCIL_ATTACHMENT: + case Texture1DViewType::DEPTH_ATTACHMENT_STENCIL_READONLY: + case Texture1DViewType::DEPTH_STENCIL_READONLY: m_ResourceViewType = ResourceViewType::DEPTH_STENCIL_ATTACHMENT; + m_IsDepthReadonly = textureViewDesc.viewType == Texture1DViewType::DEPTH_READONLY_STENCIL_ATTACHMENT || textureViewDesc.viewType == Texture1DViewType::DEPTH_STENCIL_READONLY; + m_IsStencilReadonly = textureViewDesc.viewType == Texture1DViewType::DEPTH_ATTACHMENT_STENCIL_READONLY || textureViewDesc.viewType == Texture1DViewType::DEPTH_STENCIL_READONLY; break; default: CHECK(false, "unexpected TextureView"); @@ -73,7 +78,12 @@ DescriptorVal::DescriptorVal(DeviceVal& device, Descriptor* descriptor, const Te m_ResourceViewType = ResourceViewType::COLOR_ATTACHMENT; break; case Texture2DViewType::DEPTH_STENCIL_ATTACHMENT: + case Texture2DViewType::DEPTH_READONLY_STENCIL_ATTACHMENT: + case Texture2DViewType::DEPTH_ATTACHMENT_STENCIL_READONLY: + case Texture2DViewType::DEPTH_STENCIL_READONLY: m_ResourceViewType = ResourceViewType::DEPTH_STENCIL_ATTACHMENT; + m_IsDepthReadonly = textureViewDesc.viewType == Texture2DViewType::DEPTH_READONLY_STENCIL_ATTACHMENT || textureViewDesc.viewType == Texture2DViewType::DEPTH_STENCIL_READONLY; + m_IsStencilReadonly = textureViewDesc.viewType == Texture2DViewType::DEPTH_ATTACHMENT_STENCIL_READONLY || textureViewDesc.viewType == Texture2DViewType::DEPTH_STENCIL_READONLY; break; case Texture2DViewType::SHADING_RATE_ATTACHMENT: m_ResourceViewType = ResourceViewType::SHADING_RATE_ATTACHMENT; diff --git a/Source/Validation/DescriptorVal.h b/Source/Validation/DescriptorVal.h index 7818eb3..7029934 100644 --- a/Source/Validation/DescriptorVal.h +++ b/Source/Validation/DescriptorVal.h @@ -70,6 +70,14 @@ struct DescriptorVal : public DeviceObjectVal { return m_ResourceType != ResourceType::NONE && !IsSampler() && m_ResourceViewType == ResourceViewType::SHADER_RESOURCE_STORAGE; } + inline bool IsDepthReadonly() const { + return m_IsDepthReadonly; + } + + inline bool IsStencilReadonly() const { + return m_IsStencilReadonly; + } + //================================================================================================================ // NRI //================================================================================================================ @@ -78,6 +86,8 @@ struct DescriptorVal : public DeviceObjectVal { private: ResourceType m_ResourceType = ResourceType::NONE; ResourceViewType m_ResourceViewType = ResourceViewType::NONE; + bool m_IsDepthReadonly = false; + bool m_IsStencilReadonly = false; }; } // namespace nri diff --git a/Source/Validation/PipelineVal.cpp b/Source/Validation/PipelineVal.cpp index 102674e..235c797 100644 --- a/Source/Validation/PipelineVal.cpp +++ b/Source/Validation/PipelineVal.cpp @@ -14,6 +14,8 @@ PipelineVal::PipelineVal(DeviceVal& device, Pipeline* pipeline) PipelineVal::PipelineVal(DeviceVal& device, Pipeline* pipeline, const GraphicsPipelineDesc& graphicsPipelineDesc) : DeviceObjectVal(device, pipeline) , m_PipelineLayout(graphicsPipelineDesc.pipelineLayout) { + m_WritesToDepth = graphicsPipelineDesc.outputMerger.depth.write; + m_WritesToStencil = graphicsPipelineDesc.outputMerger.stencil.front.writeMask != 0 || graphicsPipelineDesc.outputMerger.stencil.back.writeMask != 0; } PipelineVal::PipelineVal(DeviceVal& device, Pipeline* pipeline, const ComputePipelineDesc& computePipelineDesc) diff --git a/Source/Validation/PipelineVal.h b/Source/Validation/PipelineVal.h index 1a4b5e9..f942e4d 100644 --- a/Source/Validation/PipelineVal.h +++ b/Source/Validation/PipelineVal.h @@ -14,6 +14,14 @@ struct PipelineVal : public DeviceObjectVal { return m_PipelineLayout; } + inline bool WritesToDepth() const { + return m_WritesToDepth; + } + + inline bool WritesToStencil() const { + return m_WritesToStencil; + } + //================================================================================================================ // NRI //================================================================================================================ @@ -22,6 +30,8 @@ struct PipelineVal : public DeviceObjectVal { private: const PipelineLayout* m_PipelineLayout = nullptr; + bool m_WritesToDepth = false; + bool m_WritesToStencil = false; }; } // namespace nri