Plasma Engine  2.0
Loading...
Searching...
No Matches
ResourceCacheVulkan.h
1#pragma once
2
3#include <RendererVulkan/RendererVulkanDLL.h>
4
5#include <Foundation/Algorithm/HashStream.h>
6#include <Foundation/Math/Size.h>
7#include <RendererFoundation/Resources/RenderTargetSetup.h>
8#include <RendererVulkan/Device/DeviceVulkan.h>
9#include <RendererVulkan/Resources/RenderTargetViewVulkan.h>
10#include <RendererVulkan/Shader/ShaderVulkan.h>
11
12#include <vulkan/vulkan.hpp>
13
19class plRefCounted;
20
21PL_DEFINE_AS_POD_TYPE(vk::DynamicState);
22
24class PL_RENDERERVULKAN_DLL plResourceCacheVulkan
25{
26public:
27 static void Initialize(plGALDeviceVulkan* pDevice, vk::Device device);
28 static void DeInitialize();
29
30 static vk::RenderPass RequestRenderPass(const plGALRenderingSetup& renderingSetup);
31 static vk::Framebuffer RequestFrameBuffer(vk::RenderPass renderPass, const plGALRenderTargetSetup& renderTargetSetup, plSizeU32& out_Size, plEnum<plGALMSAASampleCount>& out_msaa, plUInt32& out_uiLayers);
32
34 {
36 vk::PushConstantRange m_pushConstants;
37 };
38
40 {
41 PL_DECLARE_POD_TYPE();
42 vk::RenderPass m_renderPass; // Created from plGALRenderingSetup
43 vk::PipelineLayout m_layout; // Created from shader
46 plUInt8 m_uiAttachmentCount = 0; // DX12 requires format list for RT and DT
47 const plGALRasterizerStateVulkan* m_pCurrentRasterizerState = nullptr;
48 const plGALBlendStateVulkan* m_pCurrentBlendState = nullptr;
49 const plGALDepthStencilStateVulkan* m_pCurrentDepthStencilState = nullptr;
50 const plGALShaderVulkan* m_pCurrentShader = nullptr;
51 const plGALVertexDeclarationVulkan* m_pCurrentVertexDecl = nullptr;
52 plUInt32 m_VertexBufferStrides[PL_GAL_MAX_VERTEX_BUFFER_COUNT] = {};
53 };
54
56 {
57 PL_DECLARE_POD_TYPE();
58 vk::PipelineLayout m_layout;
59 const plGALShaderVulkan* m_pCurrentShader = nullptr;
60 };
61
62 static vk::PipelineLayout RequestPipelineLayout(const PipelineLayoutDesc& desc);
63 static vk::Pipeline RequestGraphicsPipeline(const GraphicsPipelineDesc& desc);
64 static vk::Pipeline RequestComputePipeline(const ComputePipelineDesc& desc);
65
67 {
68 mutable plUInt32 m_uiHash = 0;
70 };
71 static vk::DescriptorSetLayout RequestDescriptorSetLayout(const plGALShaderVulkan::DescriptorSetLayoutDesc& desc);
72
74 static void ResourceDeleted(const plRefCounted* pResource);
76 static void ShaderDeleted(const plGALShaderVulkan* pShader);
77
78private:
79 struct FramebufferKey
80 {
81 vk::RenderPass m_renderPass;
82 plGALRenderTargetSetup m_renderTargetSetup;
83 };
84
86 struct FramebufferDesc
87 {
88 VkRenderPass renderPass;
89 plSizeU32 m_size = {0, 0};
90 uint32_t layers = 1;
93 };
94
96 struct AttachmentDesc
97 {
98 PL_DECLARE_POD_TYPE();
99 vk::Format format = vk::Format::eUndefined;
100 vk::SampleCountFlagBits samples = vk::SampleCountFlagBits::e1;
101 // Not set at all right now
102 vk::ImageUsageFlags usage = vk::ImageUsageFlagBits::eSampled;
103 // Not set at all right now
104 vk::ImageLayout initialLayout = vk::ImageLayout::eUndefined;
105 // No support for eDontCare in PL right now
106 vk::AttachmentLoadOp loadOp = vk::AttachmentLoadOp::eClear;
107 vk::AttachmentStoreOp storeOp = vk::AttachmentStoreOp::eStore;
108 vk::AttachmentLoadOp stencilLoadOp = vk::AttachmentLoadOp::eClear;
109 vk::AttachmentStoreOp stencilStoreOp = vk::AttachmentStoreOp::eStore;
110 };
111
113 struct RenderPassDesc
114 {
116 };
117
118 struct ResourceCacheHash
119 {
120 static plUInt32 Hash(const RenderPassDesc& renderingSetup);
121 static bool Equal(const RenderPassDesc& a, const RenderPassDesc& b);
122
123 static plUInt32 Hash(const plGALRenderingSetup& renderingSetup);
124 static bool Equal(const plGALRenderingSetup& a, const plGALRenderingSetup& b);
125
126 static plUInt32 Hash(const FramebufferKey& renderTargetSetup);
127 static bool Equal(const FramebufferKey& a, const FramebufferKey& b);
128
129 static plUInt32 Hash(const PipelineLayoutDesc& desc);
130 static bool Equal(const PipelineLayoutDesc& a, const PipelineLayoutDesc& b);
131
132 static bool Less(const GraphicsPipelineDesc& a, const GraphicsPipelineDesc& b);
133 static plUInt32 Hash(const GraphicsPipelineDesc& desc);
134 static bool Equal(const GraphicsPipelineDesc& a, const GraphicsPipelineDesc& b);
135
136 static bool Less(const ComputePipelineDesc& a, const ComputePipelineDesc& b);
137 static bool Equal(const ComputePipelineDesc& a, const ComputePipelineDesc& b);
138
139 static plUInt32 Hash(const plGALShaderVulkan::DescriptorSetLayoutDesc& desc) { return desc.m_uiHash; }
141 };
142
143 struct FrameBufferCache
144 {
145 vk::Framebuffer m_frameBuffer;
146 plSizeU32 m_size;
148 plUInt32 m_layers = 0;
149 PL_DECLARE_POD_TYPE();
150 };
151
152 static vk::RenderPass RequestRenderPassInternal(const RenderPassDesc& desc);
153 static void GetRenderPassDesc(const plGALRenderingSetup& renderingSetup, RenderPassDesc& out_desc);
154 static void GetFrameBufferDesc(vk::RenderPass renderPass, const plGALRenderTargetSetup& renderTargetSetup, FramebufferDesc& out_desc);
155
156public:
159
160
161private:
162 static plGALDeviceVulkan* s_pDevice;
163 static vk::Device s_device;
164 // We have a N to 1 mapping for plGALRenderingSetup to vk::RenderPass as multiple plGALRenderingSetup can share the same RenderPassDesc.
165 // Thus, we have a two stage resolve to the vk::RenderPass. If a plGALRenderingSetup is not present in s_shallowRenderPasses we create the RenderPassDesc which has a 1 to 1 relationship with vk::RenderPass and look that one up in s_renderPasses. Finally we add the entry to s_shallowRenderPasses to make sure a shallow lookup will work on the next query.
166 static plHashTable<plGALRenderingSetup, vk::RenderPass, ResourceCacheHash> s_shallowRenderPasses; // #TODO_VULKAN cache invalidation
168 static plHashTable<FramebufferKey, FrameBufferCache, ResourceCacheHash> s_frameBuffers; // #TODO_VULKAN cache invalidation
169
171 static GraphicsPipelineMap s_graphicsPipelines;
172 static ComputePipelineMap s_computePipelines;
175
177};
Definition StateVulkan.h:9
Definition StateVulkan.h:30
The Vulkan device implementation of the graphics abstraction layer.
Definition DeviceVulkan.h:58
Definition StateVulkan.h:50
Definition RenderTargetSetup.h:19
Definition ShaderVulkan.h:13
Definition VertexDeclarationVulkan.h:11
Definition HashTable.h:333
A hybrid array uses in-place storage to handle the first few elements without any allocation....
Definition HybridArray.h:12
Definition Map.h:408
Base class for reference counted objects.
Definition RefCounted.h:52
Creates and caches persistent Vulkan resources. Resources are never freed until the device is shut do...
Definition ResourceCacheVulkan.h:25
A custom enum implementation that allows to define the underlying storage type to control its memory ...
Definition Enum.h:37
Definition RenderTargetSetup.h:44
Used as input to plResourceCacheVulkan::RequestDescriptorSetLayout to create a vk::DescriptorSetLayou...
Definition ShaderVulkan.h:17
Definition ResourceCacheVulkan.h:56
Definition ResourceCacheVulkan.h:67
Definition ResourceCacheVulkan.h:40
Definition ResourceCacheVulkan.h:34