diff --git a/plume_d3d12.cpp b/plume_d3d12.cpp index f08f984..1b19895 100644 --- a/plume_d3d12.cpp +++ b/plume_d3d12.cpp @@ -2237,6 +2237,15 @@ namespace plume { } } + void D3D12CommandList::setBlendConstants(const float rgba[4]) { + d3d->OMSetBlendFactor(rgba); + } + + void D3D12CommandList::setStencilReference(uint32_t reference) { + d3d->OMSetStencilRef(reference); + activeStencilRef = reference; + } + void D3D12CommandList::setDepthBias(float depthBias, float depthBiasClamp, float slopeScaledDepthBias) { # ifdef PLUME_D3D12_AGILITY_SDK_ENABLED assert(d3dV9 != nullptr); @@ -2472,6 +2481,10 @@ namespace plume { assert(activeGraphicsPipeline->type == D3D12Pipeline::Type::Graphics); const D3D12GraphicsPipeline *graphicsPipeline = static_cast(activeGraphicsPipeline); + if (graphicsPipeline->dynamicStencilRef) { + return; + } + if (activeStencilRef != graphicsPipeline->stencilRef) { d3d->OMSetStencilRef(graphicsPipeline->stencilRef); activeStencilRef = graphicsPipeline->stencilRef; @@ -3086,6 +3099,7 @@ namespace plume { assert(desc.pipelineLayout != nullptr); topology = toD3D12(desc.primitiveTopology); + dynamicStencilRef = desc.dynamicStencilReferenceEnabled; const D3D12PipelineLayout *pipelineLayout = static_cast(desc.pipelineLayout); const D3D12Shader *vertexShader = static_cast(desc.vertexShader); diff --git a/plume_d3d12.h b/plume_d3d12.h index 4827452..8fcd174 100644 --- a/plume_d3d12.h +++ b/plume_d3d12.h @@ -211,6 +211,8 @@ namespace plume { void setScissors(const RenderRect *scissorRects, uint32_t count) override; void setFramebuffer(const RenderFramebuffer *framebuffer) override; void setDepthBias(float depthBias, float depthBiasClamp, float slopeScaledDepthBias) override; + void setBlendConstants(const float rgba[4]) override; + void setStencilReference(uint32_t reference) override; void clearColor(uint32_t attachmentIndex, RenderColor colorValue, const RenderRect *clearRects, uint32_t clearRectsCount) override; void clearDepthStencil(bool clearDepth, bool clearStencil, float depthValue, uint32_t stencilValue, const RenderRect *clearRects, uint32_t clearRectsCount) override; void copyBufferRegion(RenderBufferReference dstBuffer, RenderBufferReference srcBuffer, uint64_t size) override; @@ -397,6 +399,7 @@ namespace plume { std::vector inputSlots; D3D12_PRIMITIVE_TOPOLOGY topology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED; uint32_t stencilRef = 0; + bool dynamicStencilRef = false; D3D12GraphicsPipeline(D3D12Device *device, const RenderGraphicsPipelineDesc &desc); ~D3D12GraphicsPipeline() override; diff --git a/plume_metal.cpp b/plume_metal.cpp index c069a44..6a04292 100644 --- a/plume_metal.cpp +++ b/plume_metal.cpp @@ -1607,6 +1607,7 @@ namespace plume { state.renderPipelineState = device->mtl->newRenderPipelineState(descriptor, &error); state.primitiveType = mapPrimitiveType(desc.primitiveTopology); state.stencilReference = desc.stencilEnabled ? desc.stencilReference : 0; + state.dynamicStencilReferenceEnabled = desc.dynamicStencilReferenceEnabled; if (desc.dynamicDepthBiasEnabled) { state.dynamicDepthBiasEnabled = true; @@ -2750,6 +2751,16 @@ namespace plume { } } + void MetalCommandList::setBlendConstants(const float rgba[4]) { + std::copy(rgba, rgba + 4, dynamicBlendConstants); + dirtyGraphicsState.blendConstants = 1; + } + + void MetalCommandList::setStencilReference(uint32_t reference) { + dynamicStencilReference = reference; + dirtyGraphicsState.depthStencil = 1; + } + void MetalCommandList::setDepthBias(float depthBias, float depthBiasClamp, float slopeScaledDepthBias) { dynamicDepthBias.depthBias = depthBias; dynamicDepthBias.depthBiasClamp = depthBiasClamp; @@ -3444,9 +3455,10 @@ namespace plume { activeRenderEncoder->setDepthStencilState(activeRenderState->depthStencilState); stateCache.lastDepthStencilState = activeRenderState->depthStencilState; } - if (activeRenderState->stencilReference != stateCache.lastStencilReference) { - activeRenderEncoder->setStencilReferenceValue(activeRenderState->stencilReference); - stateCache.lastStencilReference = activeRenderState->stencilReference; + const uint32_t stencilReference = activeRenderState->dynamicStencilReferenceEnabled ? dynamicStencilReference : activeRenderState->stencilReference; + if (stencilReference != stateCache.lastStencilReference) { + activeRenderEncoder->setStencilReferenceValue(stencilReference); + stateCache.lastStencilReference = stencilReference; } } dirtyGraphicsState.depthStencil = 0; @@ -3471,6 +3483,12 @@ namespace plume { dirtyGraphicsState.rasterizer = 0; } + // Blend constants + if (dirtyGraphicsState.blendConstants) { + activeRenderEncoder->setBlendColor(dynamicBlendConstants[0], dynamicBlendConstants[1], dynamicBlendConstants[2], dynamicBlendConstants[3]); + dirtyGraphicsState.blendConstants = 0; + } + // Depth bias if (dirtyGraphicsState.depthBias) { float newDepthBias, newSlopeScale, newClamp; diff --git a/plume_metal.h b/plume_metal.h index 59ae129..7fc20b9 100644 --- a/plume_metal.h +++ b/plume_metal.h @@ -118,6 +118,7 @@ namespace plume { uint32_t depthBias : 1; uint32_t depthStencil : 1; uint32_t rasterizer : 1; + uint32_t blendConstants : 1; }; uint32_t value; }; @@ -129,7 +130,7 @@ namespace plume { uint32_t vertexBufferSlots; void setAll() { - value = 0x1FF; // All 9 bits set + value = 0x3FF; // All 10 bits set descriptorSetDirtyIndex = 0; vertexBufferSlots = (1U << MAX_VERTEX_BUFFER_BINDINGS) - 1; } @@ -197,6 +198,7 @@ namespace plume { float depthBiasClamp; float depthBiasSlopeFactor; bool dynamicDepthBiasEnabled; + bool dynamicStencilReferenceEnabled = false; }; struct ExtendedRenderTexture : RenderTexture { @@ -428,6 +430,9 @@ namespace plume { float slopeScaledDepthBias; } dynamicDepthBias; + uint32_t dynamicStencilReference = 0; + float dynamicBlendConstants[4] = {}; + struct { uint32_t updateDirtyBits = ~0; int update[MetalBarrierStage::COUNT] = {}; @@ -481,6 +486,8 @@ namespace plume { void setScissors(const RenderRect *scissorRects, uint32_t count) override; void setFramebuffer(const RenderFramebuffer *framebuffer) override; void setDepthBias(float depthBias, float depthBiasClamp, float slopeScaledDepthBias) override; + void setBlendConstants(const float rgba[4]) override; + void setStencilReference(uint32_t reference) override; void clearColor(uint32_t attachmentIndex, RenderColor colorValue, const RenderRect *clearRects, uint32_t clearRectsCount) override; void clearDepthStencil(bool clearDepth, bool clearStencil, float depthValue, uint32_t stencilValue, const RenderRect *clearRects, uint32_t clearRectsCount) override; void copyBufferRegion(RenderBufferReference dstBuffer, RenderBufferReference srcBuffer, uint64_t size) override; diff --git a/plume_render_interface.h b/plume_render_interface.h index 2048557..19a8142 100644 --- a/plume_render_interface.h +++ b/plume_render_interface.h @@ -137,6 +137,11 @@ namespace plume { virtual void setScissors(const RenderRect *scissorRects, uint32_t count) = 0; virtual void setFramebuffer(const RenderFramebuffer *framebuffer) = 0; virtual void setDepthBias(float depthBias, float depthBiasClamp, float slopeScaledDepthBias) = 0; + // Blend constants (BLEND_FACTOR / INV_BLEND_FACTOR) for pipelines created with dynamicBlendConstantsEnabled. + virtual void setBlendConstants(const float rgba[4]) = 0; + // Stencil reference (front and back) for pipelines created with dynamicStencilReferenceEnabled. + // Like other dynamic state, set it again after binding a pipeline that has it static. + virtual void setStencilReference(uint32_t reference) = 0; virtual void clearColor(uint32_t attachmentIndex = 0, RenderColor colorValue = RenderColor(), const RenderRect *clearRects = nullptr, uint32_t clearRectsCount = 0) = 0; virtual void clearDepthStencil(bool clearDepth = true, bool clearStencil = true, float depthValue = 1.0f, uint32_t stencilValue = 0, const RenderRect *clearRects = nullptr, uint32_t clearRectsCount = 0) = 0; virtual void copyBufferRegion(RenderBufferReference dstBuffer, RenderBufferReference srcBuffer, uint64_t size) = 0; @@ -150,6 +155,10 @@ namespace plume { virtual void discardTexture(const RenderTexture* texture) = 0; // D3D12 only. virtual void resetQueryPool(const RenderQueryPool *queryPool, uint32_t queryFirstIndex, uint32_t queryCount) = 0; virtual void writeTimestamp(const RenderQueryPool *queryPool, uint32_t queryIndex) = 0; + // Occlusion queries (samples that pass the depth and stencil tests) in a pool from + // RenderDevice::createOcclusionQueryPool. + virtual void beginQuery(const RenderQueryPool *queryPool, uint32_t queryIndex) { (void)queryPool; (void)queryIndex; } + virtual void endQuery(const RenderQueryPool *queryPool, uint32_t queryIndex) { (void)queryPool; (void)queryIndex; } // Concrete implementation shortcuts. inline void barriers(RenderBarrierStages stages, const RenderBufferBarrier &barrier) { @@ -221,6 +230,8 @@ namespace plume { virtual void queryResults() = 0; virtual const uint64_t *getResults() const = 0; virtual uint32_t getCount() const = 0; + // Results of queries [first, first + count) without waiting; false if any is not available yet. + virtual bool getRawResults(uint32_t first, uint32_t count, uint64_t *out) { (void)first; (void)count; (void)out; return false; } }; struct RenderDevice { @@ -241,6 +252,8 @@ namespace plume { virtual std::unique_ptr createCommandSemaphore() = 0; virtual std::unique_ptr createFramebuffer(const RenderFramebufferDesc &desc) = 0; virtual std::unique_ptr createQueryPool(uint32_t queryCount) = 0; + // nullptr if the backend has no occlusion queries (only Vulkan has them so far). + virtual std::unique_ptr createOcclusionQueryPool(uint32_t queryCount) { (void)queryCount; return nullptr; } virtual void setBottomLevelASBuildInfo(RenderBottomLevelASBuildInfo &buildInfo, const RenderBottomLevelASMesh *meshes, uint32_t meshCount, bool preferFastBuild = true, bool preferFastTrace = false) = 0; virtual void setTopLevelASBuildInfo(RenderTopLevelASBuildInfo &buildInfo, const RenderTopLevelASInstance *instances, uint32_t instanceCount, bool preferFastBuild = true, bool preferFastTrace = false) = 0; virtual void setShaderBindingTableInfo(RenderShaderBindingTableInfo &tableInfo, const RenderShaderBindingGroups &groups, const RenderPipeline *pipeline, RenderDescriptorSet **descriptorSets, uint32_t descriptorSetCount) = 0; diff --git a/plume_render_interface_types.h b/plume_render_interface_types.h index ad63e0e..c1bb83b 100644 --- a/plume_render_interface_types.h +++ b/plume_render_interface_types.h @@ -166,6 +166,21 @@ namespace plume { BC7_TYPELESS, BC7_UNORM, BC7_UNORM_SRGB, + R16G16_SSCALED, + R16G16B16A16_SSCALED, + // Vertex formats: USCALED/SSCALED are unnormalized integers read as floats. + R16G16B16A16_USCALED, + R8G8B8A8_USCALED, + R8G8B8A8_SSCALED, + R16G16_USCALED, + R8G8_USCALED, + R8G8_SSCALED, + R16_USCALED, + R16_SSCALED, + R8_USCALED, + R8_SSCALED, + A2B10G10R10_UNORM_PACK32, + A2B10G10R10_SNORM_PACK32, MAX }; @@ -597,6 +612,24 @@ namespace plume { case RenderFormat::BC7_UNORM: case RenderFormat::BC7_UNORM_SRGB: return 16; + case RenderFormat::R16G16B16A16_SSCALED: + case RenderFormat::R16G16B16A16_USCALED: + return 8; + case RenderFormat::A2B10G10R10_UNORM_PACK32: + case RenderFormat::A2B10G10R10_SNORM_PACK32: + case RenderFormat::R8G8B8A8_USCALED: + case RenderFormat::R8G8B8A8_SSCALED: + case RenderFormat::R16G16_SSCALED: + case RenderFormat::R16G16_USCALED: + return 4; + case RenderFormat::R8G8_USCALED: + case RenderFormat::R8G8_SSCALED: + case RenderFormat::R16_USCALED: + case RenderFormat::R16_SSCALED: + return 2; + case RenderFormat::R8_USCALED: + case RenderFormat::R8_SSCALED: + return 1; default: assert(false && "Unknown format."); return 1; @@ -681,6 +714,21 @@ namespace plume { case RenderFormat::BC7_UNORM: case RenderFormat::BC7_UNORM_SRGB: return 4; + case RenderFormat::R16G16B16A16_SSCALED: + case RenderFormat::R16G16_SSCALED: + case RenderFormat::R16G16B16A16_USCALED: + case RenderFormat::R8G8B8A8_USCALED: + case RenderFormat::R8G8B8A8_SSCALED: + case RenderFormat::R16G16_USCALED: + case RenderFormat::R8G8_USCALED: + case RenderFormat::R8G8_SSCALED: + case RenderFormat::R16_USCALED: + case RenderFormat::R16_SSCALED: + case RenderFormat::R8_USCALED: + case RenderFormat::R8_SSCALED: + case RenderFormat::A2B10G10R10_UNORM_PACK32: + case RenderFormat::A2B10G10R10_SNORM_PACK32: + return 1; default: assert(false && "Unknown format."); return 1; @@ -1286,6 +1334,8 @@ namespace plume { float depthBiasClamp = 0.0f; float slopeScaledDepthBias = 0.0f; bool dynamicDepthBiasEnabled = false; + bool dynamicBlendConstantsEnabled = false; // Blend constants come from RenderCommandList::setBlendConstants. + bool dynamicStencilReferenceEnabled = false; // The stencil reference comes from RenderCommandList::setStencilReference. bool depthEnabled = false; bool depthWriteEnabled = false; bool stencilEnabled = false; diff --git a/plume_vulkan.cpp b/plume_vulkan.cpp index 58749c4..66c19a3 100644 --- a/plume_vulkan.cpp +++ b/plume_vulkan.cpp @@ -11,6 +11,7 @@ #include "plume_vulkan.h" #include +#include #include #include #include @@ -84,6 +85,7 @@ namespace plume { VK_KHR_DEFERRED_HOST_OPERATIONS_EXTENSION_NAME, VK_EXT_SAMPLE_LOCATIONS_EXTENSION_NAME, VK_EXT_LOAD_STORE_OP_NONE_EXTENSION_NAME, + VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME, VK_KHR_PRESENT_ID_EXTENSION_NAME, VK_KHR_PRESENT_WAIT_EXTENSION_NAME, VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME, @@ -249,6 +251,34 @@ namespace plume { return VK_FORMAT_BC7_UNORM_BLOCK; case RenderFormat::BC7_UNORM_SRGB: return VK_FORMAT_BC7_SRGB_BLOCK; + case RenderFormat::R16G16B16A16_SSCALED: + return VK_FORMAT_R16G16B16A16_SSCALED; + case RenderFormat::R16G16_SSCALED: + return VK_FORMAT_R16G16_SSCALED; + case RenderFormat::R16G16B16A16_USCALED: + return VK_FORMAT_R16G16B16A16_USCALED; + case RenderFormat::R8G8B8A8_USCALED: + return VK_FORMAT_R8G8B8A8_USCALED; + case RenderFormat::R8G8B8A8_SSCALED: + return VK_FORMAT_R8G8B8A8_SSCALED; + case RenderFormat::R16G16_USCALED: + return VK_FORMAT_R16G16_USCALED; + case RenderFormat::R8G8_USCALED: + return VK_FORMAT_R8G8_USCALED; + case RenderFormat::R8G8_SSCALED: + return VK_FORMAT_R8G8_SSCALED; + case RenderFormat::R16_USCALED: + return VK_FORMAT_R16_USCALED; + case RenderFormat::R16_SSCALED: + return VK_FORMAT_R16_SSCALED; + case RenderFormat::R8_USCALED: + return VK_FORMAT_R8_USCALED; + case RenderFormat::R8_SSCALED: + return VK_FORMAT_R8_SSCALED; + case RenderFormat::A2B10G10R10_UNORM_PACK32: + return VK_FORMAT_A2B10G10R10_UNORM_PACK32; + case RenderFormat::A2B10G10R10_SNORM_PACK32: + return VK_FORMAT_A2B10G10R10_SNORM_PACK32; default: assert(false && "Unknown format."); return VK_FORMAT_UNDEFINED; @@ -1260,6 +1290,78 @@ namespace plume { setLayoutHandles.emplace_back(setLayout->vk); } + // Root descriptors: one push descriptor set per register space, placed at the set index equal + // to the space (after the descriptor sets; empty sets fill any gap). + if (desc.rootDescriptorDescsCount > 0) { + if (!device->pushDescriptorSupported) { + fprintf(stderr, "Root descriptors need VK_KHR_push_descriptor, which the device does not support.\n"); + return; + } + + std::map> spaces; + for (uint32_t i = 0; i < desc.rootDescriptorDescsCount; i++) { + const RenderRootDescriptorDesc &src = desc.rootDescriptorDescs[i]; + RootDescriptor dst; + dst.setIndex = src.registerSpace; + dst.binding = src.shaderRegister; + switch (src.type) { + case RenderRootDescriptorType::CONSTANT_BUFFER: + dst.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + break; + case RenderRootDescriptorType::SHADER_RESOURCE: + case RenderRootDescriptorType::UNORDERED_ACCESS: + dst.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; + break; + default: + assert(false && "Unknown root descriptor type."); + break; + } + + rootDescriptors.emplace_back(dst); + + VkDescriptorSetLayoutBinding binding = {}; + binding.binding = dst.binding; + binding.descriptorType = dst.type; + binding.descriptorCount = 1; + binding.stageFlags = VK_SHADER_STAGE_ALL; + spaces[dst.setIndex].emplace_back(binding); + } + + for (const auto &[space, bindings] : spaces) { + if (space < setLayoutHandles.size()) { + fprintf(stderr, "Root descriptor register space %u is already used by a descriptor set.\n", space); + return; + } + + while (setLayoutHandles.size() < space) { + VkDescriptorSetLayoutCreateInfo emptyInfo = {}; + emptyInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; + VkDescriptorSetLayout empty = VK_NULL_HANDLE; + if (vkCreateDescriptorSetLayout(device->vk, &emptyInfo, nullptr, &empty) != VK_SUCCESS) { + fprintf(stderr, "vkCreateDescriptorSetLayout failed for an empty set.\n"); + return; + } + + rootSetLayouts.emplace_back(empty); + setLayoutHandles.emplace_back(empty); + } + + VkDescriptorSetLayoutCreateInfo pushInfo = {}; + pushInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; + pushInfo.flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR; + pushInfo.bindingCount = uint32_t(bindings.size()); + pushInfo.pBindings = bindings.data(); + VkDescriptorSetLayout pushLayout = VK_NULL_HANDLE; + if (vkCreateDescriptorSetLayout(device->vk, &pushInfo, nullptr, &pushLayout) != VK_SUCCESS) { + fprintf(stderr, "vkCreateDescriptorSetLayout failed for the root descriptors of space %u.\n", space); + return; + } + + rootSetLayouts.emplace_back(pushLayout); + setLayoutHandles.emplace_back(pushLayout); + } + } + layoutInfo.pSetLayouts = !setLayoutHandles.empty() ? setLayoutHandles.data() : nullptr; layoutInfo.setLayoutCount = uint32_t(setLayoutHandles.size()); @@ -1278,6 +1380,10 @@ namespace plume { for (VulkanDescriptorSetLayout *setLayout : descriptorSetLayouts) { delete setLayout; } + + for (VkDescriptorSetLayout setLayout : rootSetLayouts) { + vkDestroyDescriptorSetLayout(device->vk, setLayout, nullptr); + } } // VulkanShader @@ -1612,6 +1718,13 @@ namespace plume { dynamicStates.clear(); dynamicStates.emplace_back(VK_DYNAMIC_STATE_VIEWPORT); dynamicStates.emplace_back(VK_DYNAMIC_STATE_SCISSOR); + if (desc.dynamicBlendConstantsEnabled) { + dynamicStates.emplace_back(VK_DYNAMIC_STATE_BLEND_CONSTANTS); + } + + if (desc.dynamicStencilReferenceEnabled) { + dynamicStates.emplace_back(VK_DYNAMIC_STATE_STENCIL_REFERENCE); + } if (desc.dynamicDepthBiasEnabled) { dynamicStates.emplace_back(VK_DYNAMIC_STATE_DEPTH_BIAS); @@ -2548,8 +2661,13 @@ namespace plume { imageViews.emplace_back(colorAttachmentImageView); if (i == 0) { - width = uint32_t(colorAttachment->desc.width); - height = colorAttachment->desc.height; + // A view of one mip level renders at that level's size (the base + // size made MoltenVK write outside the attachment: GPU page faults). + uint32_t mip = 0; + if (desc.colorAttachmentViews && desc.colorAttachmentViews[i]) + mip = static_cast(desc.colorAttachmentViews[i])->desc.mipSlice; + width = std::max(1u, uint32_t(colorAttachment->desc.width) >> mip); + height = std::max(1u, uint32_t(colorAttachment->desc.height) >> mip); } VkAttachmentReference reference = {}; @@ -2593,8 +2711,10 @@ namespace plume { imageViews.emplace_back(depthAttachmentImageView); if (desc.colorAttachmentsCount == 0) { - width = uint32_t(depthAttachment->desc.width); - height = depthAttachment->desc.height; + const uint32_t depthMip = desc.depthAttachmentView != nullptr + ? static_cast(desc.depthAttachmentView)->desc.mipSlice : 0; + width = std::max(1u, uint32_t(depthAttachment->desc.width) >> depthMip); + height = std::max(1u, uint32_t(depthAttachment->desc.height) >> depthMip); } depthReference.attachment = uint32_t(attachments.size()); @@ -2685,7 +2805,7 @@ namespace plume { // VulkanQueryPool - VulkanQueryPool::VulkanQueryPool(VulkanDevice *device, uint32_t queryCount) { + VulkanQueryPool::VulkanQueryPool(VulkanDevice *device, uint32_t queryCount, bool occlusion) { assert(device != nullptr); assert(queryCount > 0); @@ -2693,7 +2813,7 @@ namespace plume { VkQueryPoolCreateInfo createInfo = {}; createInfo.sType = VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO; - createInfo.queryType = VK_QUERY_TYPE_TIMESTAMP; + createInfo.queryType = occlusion ? VK_QUERY_TYPE_OCCLUSION : VK_QUERY_TYPE_TIMESTAMP; createInfo.queryCount = queryCount; VkResult res = vkCreateQueryPool(device->vk, &createInfo, nullptr, &vk); @@ -2749,6 +2869,11 @@ namespace plume { } } + bool VulkanQueryPool::getRawResults(uint32_t first, uint32_t count, uint64_t *out) { + VkResult res = vkGetQueryPoolResults(device->vk, vk, first, count, sizeof(uint64_t) * count, out, sizeof(uint64_t), VK_QUERY_RESULT_64_BIT); + return res == VK_SUCCESS; + } + const uint64_t *VulkanQueryPool::getResults() const { return results.data(); } @@ -2800,6 +2925,8 @@ namespace plume { void VulkanCommandList::begin() { vkResetCommandBuffer(vk, 0); + passQueries.clear(); + passQueriesClosed.clear(); VkCommandBufferBeginInfo beginInfo = {}; beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; @@ -2893,6 +3020,7 @@ namespace plume { void VulkanCommandList::dispatch(uint32_t threadGroupCountX, uint32_t threadGroupCountY, uint32_t threadGroupCountZ) { assert(activeComputePipelineLayout != nullptr); + endActiveRenderPass(); vkCmdDispatch(vk, threadGroupCountX, threadGroupCountY, threadGroupCountZ); } @@ -2902,6 +3030,7 @@ namespace plume { assert(interfaceBuffer != nullptr); assert((interfaceBuffer->desc.flags & RenderBufferFlag::SHADER_BINDING_TABLE) && "Buffer must allow being used as a shader binding table."); assert(activeRaytracingPipelineLayout != nullptr); + endActiveRenderPass(); VkBufferDeviceAddressInfo tableAddressInfo = {}; tableAddressInfo.sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO; @@ -3008,7 +3137,32 @@ namespace plume { } void VulkanCommandList::setGraphicsRootDescriptor(RenderBufferReference bufferReference, uint32_t rootDescriptorIndex) { - assert(false && "Root descriptors are not supported in Vulkan."); + pushRootDescriptor(VK_PIPELINE_BIND_POINT_GRAPHICS, activeGraphicsPipelineLayout, bufferReference, rootDescriptorIndex); + } + + void VulkanCommandList::pushRootDescriptor(VkPipelineBindPoint bindPoint, const VulkanPipelineLayout *layout, RenderBufferReference bufferReference, uint32_t rootDescriptorIndex) { + assert(layout != nullptr); + assert(rootDescriptorIndex < layout->rootDescriptors.size()); + assert(bufferReference.ref != nullptr); + + const VulkanPipelineLayout::RootDescriptor &root = layout->rootDescriptors[rootDescriptorIndex]; + const VulkanBuffer *buffer = static_cast(bufferReference.ref); + const VkPhysicalDeviceLimits &limits = layout->device->physicalDeviceProperties.limits; + const uint64_t maxRange = (root.type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) ? limits.maxUniformBufferRange : limits.maxStorageBufferRange; + assert(bufferReference.offset < buffer->desc.size); + + VkDescriptorBufferInfo bufferInfo = {}; + bufferInfo.buffer = buffer->vk; + bufferInfo.offset = bufferReference.offset; + bufferInfo.range = std::min(buffer->desc.size - bufferReference.offset, maxRange); + + VkWriteDescriptorSet write = {}; + write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; + write.dstBinding = root.binding; + write.descriptorCount = 1; + write.descriptorType = root.type; + write.pBufferInfo = &bufferInfo; + vkCmdPushDescriptorSetKHR(vk, bindPoint, layout->vk, root.setIndex, 1, &write); } void VulkanCommandList::setRaytracingPipelineLayout(const RenderPipelineLayout *pipelineLayout) { @@ -3103,7 +3257,14 @@ namespace plume { } void VulkanCommandList::setFramebuffer(const RenderFramebuffer *framebuffer) { - endActiveRenderPass(); + // The active render pass ends when a draw or clear needs another framebuffer + // (checkActiveRenderPass) or a command that is not allowed in a render pass comes. + // Rebinding the active framebuffer, or binding one that gets no draws before + // switching back, no longer ends and restarts the pass (a store and reload of + // every attachment on tile GPUs). + if (framebuffer == nullptr) { + endActiveRenderPass(); + } if (framebuffer != nullptr) { const VulkanFramebuffer *interfaceFramebuffer = static_cast(framebuffer); @@ -3118,6 +3279,14 @@ namespace plume { vkCmdSetDepthBias(vk, depthBias, depthBiasClamp, slopeScaledDepthBias); } + void VulkanCommandList::setBlendConstants(const float rgba[4]) { + vkCmdSetBlendConstants(vk, rgba); + } + + void VulkanCommandList::setStencilReference(uint32_t reference) { + vkCmdSetStencilReference(vk, VK_STENCIL_FACE_FRONT_AND_BACK, reference); + } + static void clearCommonRectVector(uint32_t width, uint32_t height, const RenderRect *clearRects, uint32_t clearRectsCount, std::vector &rectVector) { rectVector.clear(); @@ -3401,6 +3570,7 @@ namespace plume { void VulkanCommandList::buildBottomLevelAS(const RenderAccelerationStructure *dstAccelerationStructure, RenderBufferReference scratchBuffer, const RenderBottomLevelASBuildInfo &buildInfo) { assert(dstAccelerationStructure != nullptr); assert(scratchBuffer.ref != nullptr); + endActiveRenderPass(); const VulkanAccelerationStructure *interfaceAccelerationStructure = static_cast(dstAccelerationStructure); assert(interfaceAccelerationStructure->type == RenderAccelerationStructureType::BOTTOM_LEVEL); @@ -3435,6 +3605,7 @@ namespace plume { void VulkanCommandList::buildTopLevelAS(const RenderAccelerationStructure *dstAccelerationStructure, RenderBufferReference scratchBuffer, RenderBufferReference instancesBuffer, const RenderTopLevelASBuildInfo &buildInfo) { assert(dstAccelerationStructure != nullptr); assert(scratchBuffer.ref != nullptr); + endActiveRenderPass(); assert(instancesBuffer.ref != nullptr); const VulkanAccelerationStructure *interfaceAccelerationStructure = static_cast(dstAccelerationStructure); @@ -3488,6 +3659,9 @@ namespace plume { void VulkanCommandList::resetQueryPool(const RenderQueryPool *queryPool, uint32_t queryFirstIndex, uint32_t queryCount) { assert(queryPool != nullptr); + // vkCmdResetQueryPool is not allowed inside a render pass instance. + endActiveRenderPass(); + const VulkanQueryPool *interfaceQueryPool = static_cast(queryPool); vkCmdResetQueryPool(vk, interfaceQueryPool->vk, queryFirstIndex, queryCount); } @@ -3499,9 +3673,52 @@ namespace plume { vkCmdWriteTimestamp(vk, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, interfaceQueryPool->vk, queryIndex); } + // A query begun inside a render pass instance is kept in it (no load and store of its attachments + // around the query) and ends with it at the latest; one begun outside spans the passes in between. + void VulkanCommandList::beginQuery(const RenderQueryPool *queryPool, uint32_t queryIndex) { + assert(queryPool != nullptr); + const VkQueryPool pool = static_cast(queryPool)->vk; + if (queriesEndRenderPass) { + endActiveRenderPass(); + } + + // Precise: sample counts, not just "any passed" (MoltenVK otherwise uses Metal's boolean mode). + vkCmdBeginQuery(vk, pool, queryIndex, queue->device->occlusionQueryPrecise ? VK_QUERY_CONTROL_PRECISE_BIT : 0); + if (activeRenderPass != VK_NULL_HANDLE) { + passQueries.push_back({ pool, queryIndex }); + } + } + + void VulkanCommandList::endQuery(const RenderQueryPool *queryPool, uint32_t queryIndex) { + assert(queryPool != nullptr); + const VkQueryPool pool = static_cast(queryPool)->vk; + auto matches = [&](const PassQuery &q) { return (q.pool == pool) && (q.index == queryIndex); }; + auto closed = std::find_if(passQueriesClosed.begin(), passQueriesClosed.end(), matches); + if (closed != passQueriesClosed.end()) { + // Its render pass ended first and closed it. + passQueriesClosed.erase(closed); + return; + } + + auto open = std::find_if(passQueries.begin(), passQueries.end(), matches); + if (open != passQueries.end()) { + passQueries.erase(open); + } + else { + // Begun outside a render pass: it must end outside one too. + endActiveRenderPass(); + } + + vkCmdEndQuery(vk, pool, queryIndex); + } + void VulkanCommandList::checkActiveRenderPass() { assert(targetFramebuffer != nullptr); - + + if ((activeRenderPass != VK_NULL_HANDLE) && (activeFramebuffer != targetFramebuffer)) { + endActiveRenderPass(); + } + if (activeRenderPass == VK_NULL_HANDLE) { VkRenderPassBeginInfo beginInfo = {}; beginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; @@ -3511,13 +3728,21 @@ namespace plume { beginInfo.renderArea.extent.height = targetFramebuffer->height; vkCmdBeginRenderPass(vk, &beginInfo, VkSubpassContents::VK_SUBPASS_CONTENTS_INLINE); activeRenderPass = targetFramebuffer->renderPass; + activeFramebuffer = targetFramebuffer; } } void VulkanCommandList::endActiveRenderPass() { if (activeRenderPass != VK_NULL_HANDLE) { + for (const PassQuery &q : passQueries) { + vkCmdEndQuery(vk, q.pool, q.index); + passQueriesClosed.push_back(q); + } + + passQueries.clear(); vkCmdEndRenderPass(vk); activeRenderPass = VK_NULL_HANDLE; + activeFramebuffer = nullptr; } } @@ -3933,6 +4158,7 @@ namespace plume { deviceFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2; deviceFeatures.pNext = featuresChain; vkGetPhysicalDeviceFeatures2(physicalDevice, &deviceFeatures); + occlusionQueryPrecise = deviceFeatures.features.occlusionQueryPrecise == VK_TRUE; // Check for properties. if (rayTracingFound) { @@ -4183,6 +4409,7 @@ namespace plume { // Fill Vulkan-only capabilities. loadStoreOpNoneSupported = supportedOptionalExtensions.find(VK_EXT_LOAD_STORE_OP_NONE_EXTENSION_NAME) != supportedOptionalExtensions.end(); + pushDescriptorSupported = supportedOptionalExtensions.find(VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME) != supportedOptionalExtensions.end(); if (!nullDescriptorSupported) { nullBuffer = createBuffer(RenderBufferDesc::DefaultBuffer(16, RenderBufferFlag::VERTEX)); @@ -4257,6 +4484,10 @@ namespace plume { return std::make_unique(this, queryCount); } + std::unique_ptr VulkanDevice::createOcclusionQueryPool(uint32_t queryCount) { + return std::make_unique(this, queryCount, true); + } + void VulkanDevice::setBottomLevelASBuildInfo(RenderBottomLevelASBuildInfo &buildInfo, const RenderBottomLevelASMesh *meshes, uint32_t meshCount, bool preferFastBuild, bool preferFastTrace) { assert(meshes != nullptr); assert(meshCount > 0); diff --git a/plume_vulkan.h b/plume_vulkan.h index 1d36b12..9cced37 100644 --- a/plume_vulkan.h +++ b/plume_vulkan.h @@ -129,9 +129,19 @@ namespace plume { }; struct VulkanPipelineLayout : RenderPipelineLayout { + // A root descriptor is a binding of a push descriptor set (VK_KHR_push_descriptor): + // the set index is the register space, the binding the shader register. + struct RootDescriptor { + uint32_t setIndex = 0; + uint32_t binding = 0; + VkDescriptorType type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + }; + VkPipelineLayout vk = VK_NULL_HANDLE; std::vector pushConstantRanges; std::vector descriptorSetLayouts; + std::vector rootDescriptors; + std::vector rootSetLayouts; // Push descriptor sets and empty gap fillers. VulkanDevice *device = nullptr; VulkanPipelineLayout(VulkanDevice *device, const RenderPipelineLayoutDesc &desc); @@ -286,9 +296,10 @@ namespace plume { std::vector results; VkQueryPool vk = VK_NULL_HANDLE; - VulkanQueryPool(VulkanDevice *device, uint32_t queryCount); + VulkanQueryPool(VulkanDevice *device, uint32_t queryCount, bool occlusion = false); virtual ~VulkanQueryPool() override; virtual void queryResults() override; + bool getRawResults(uint32_t first, uint32_t count, uint64_t *out) override; virtual const uint64_t *getResults() const override; virtual uint32_t getCount() const override; }; @@ -302,6 +313,19 @@ namespace plume { const VulkanPipelineLayout *activeGraphicsPipelineLayout = nullptr; const VulkanPipelineLayout *activeRaytracingPipelineLayout = nullptr; VkRenderPass activeRenderPass = VK_NULL_HANDLE; + // Occlusion queries begun inside the active render pass: they end with it at the latest (Vulkan + // requires a query begun in a render pass instance to end in it), and a later endQuery of one + // the pass end already closed does nothing. + struct PassQuery { + VkQueryPool pool = VK_NULL_HANDLE; + uint32_t index = 0; + }; + std::vector passQueries; + std::vector passQueriesClosed; + // true: begin and end every query outside render passes (ends the active pass). + bool queriesEndRenderPass = false; + // The framebuffer of the active render pass instance. + const VulkanFramebuffer *activeFramebuffer = nullptr; VulkanCommandList(VulkanCommandQueue *queue); ~VulkanCommandList() override; @@ -320,6 +344,7 @@ namespace plume { void setGraphicsPushConstants(uint32_t rangeIndex, const void *data, uint32_t offset = 0, uint32_t size = 0) override; void setGraphicsDescriptorSet(RenderDescriptorSet *descriptorSet, uint32_t setIndex) override; void setGraphicsRootDescriptor(RenderBufferReference bufferReference, uint32_t rootDescriptorIndex) override; + void pushRootDescriptor(VkPipelineBindPoint bindPoint, const VulkanPipelineLayout *layout, RenderBufferReference bufferReference, uint32_t rootDescriptorIndex); void setRaytracingPipelineLayout(const RenderPipelineLayout *pipelineLayout) override; void setRaytracingPushConstants(uint32_t rangeIndex, const void *data, uint32_t offset = 0, uint32_t size = 0) override; void setRaytracingDescriptorSet(RenderDescriptorSet *descriptorSet, uint32_t setIndex) override; @@ -329,6 +354,8 @@ namespace plume { void setScissors(const RenderRect *scissorRects, uint32_t count) override; void setFramebuffer(const RenderFramebuffer *framebuffer) override; void setDepthBias(float depthBias, float depthBiasClamp, float slopeScaledDepthBias) override; + void setBlendConstants(const float rgba[4]) override; + void setStencilReference(uint32_t reference) override; void clearColor(uint32_t attachmentIndex, RenderColor colorValue, const RenderRect *clearRects, uint32_t clearRectsCount) override; void clearDepthStencil(bool clearDepth, bool clearStencil, float depthValue, uint32_t stencilValue, const RenderRect *clearRects, uint32_t clearRectsCount) override; void copyBufferRegion(RenderBufferReference dstBuffer, RenderBufferReference srcBuffer, uint64_t size) override; @@ -342,6 +369,8 @@ namespace plume { void discardTexture(const RenderTexture* texture) override; void resetQueryPool(const RenderQueryPool *queryPool, uint32_t queryFirstIndex, uint32_t queryCount) override; void writeTimestamp(const RenderQueryPool *queryPool, uint32_t queryIndex) override; + void beginQuery(const RenderQueryPool *queryPool, uint32_t queryIndex) override; + void endQuery(const RenderQueryPool *queryPool, uint32_t queryIndex) override; void checkActiveRenderPass(); void endActiveRenderPass(); void setDescriptorSet(VkPipelineBindPoint bindPoint, const VulkanPipelineLayout *pipelineLayout, const RenderDescriptorSet *descriptorSet, uint32_t setIndex); @@ -412,10 +441,12 @@ namespace plume { std::vector queueFamilies; RenderDeviceCapabilities capabilities; RenderDeviceDescription description; + bool occlusionQueryPrecise = false; VkPhysicalDeviceRayTracingPipelinePropertiesKHR rtPipelineProperties = {}; VkPhysicalDeviceSampleLocationsPropertiesEXT sampleLocationProperties = {}; std::unique_ptr nullBuffer; bool loadStoreOpNoneSupported = false; + bool pushDescriptorSupported = false; bool nullDescriptorSupported = false; VulkanDevice(VulkanInterface *renderInterface, const std::string &preferredDeviceName); @@ -436,6 +467,7 @@ namespace plume { std::unique_ptr createCommandSemaphore() override; std::unique_ptr createFramebuffer(const RenderFramebufferDesc &desc) override; std::unique_ptr createQueryPool(uint32_t queryCount) override; + std::unique_ptr createOcclusionQueryPool(uint32_t queryCount) override; void setBottomLevelASBuildInfo(RenderBottomLevelASBuildInfo &buildInfo, const RenderBottomLevelASMesh *meshes, uint32_t meshCount, bool preferFastBuild, bool preferFastTrace) override; void setTopLevelASBuildInfo(RenderTopLevelASBuildInfo &buildInfo, const RenderTopLevelASInstance *instances, uint32_t instanceCount, bool preferFastBuild, bool preferFastTrace) override; void setShaderBindingTableInfo(RenderShaderBindingTableInfo &tableInfo, const RenderShaderBindingGroups &groups, const RenderPipeline *pipeline, RenderDescriptorSet **descriptorSets, uint32_t descriptorSetCount) override;