Skip to content

Commit 429e35a

Browse files
more improve around shader caches
1 parent 55fe51c commit 429e35a

File tree

8 files changed

+216
-171
lines changed

8 files changed

+216
-171
lines changed

ZEngine/ZEngine/Core/Memory/Allocator.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,19 @@ namespace ZEngine::Core::Memory
7878

7979
void ArenaAllocator::Clear()
8080
{
81-
m_previous_offset = 0;
82-
m_current_offset = 0;
81+
m_previous_offset = m_initial_previous_offset;
82+
m_current_offset = m_initial_current_offset;
8383
}
8484

8585
void ArenaAllocator::CreateSubArena(size_t size, ArenaAllocator* out_arena)
8686
{
87-
out_arena->m_memory = reinterpret_cast<uint8_t*>(Allocate(size));
88-
out_arena->m_previous_offset = m_previous_offset;
89-
out_arena->m_current_offset = m_previous_offset;
90-
out_arena->m_total_size = m_previous_offset + size;
87+
out_arena->m_memory = reinterpret_cast<uint8_t*>(Allocate(size));
88+
out_arena->m_initial_previous_offset = m_previous_offset;
89+
out_arena->m_initial_current_offset = m_previous_offset;
90+
91+
out_arena->m_previous_offset = out_arena->m_initial_previous_offset;
92+
out_arena->m_current_offset = out_arena->m_initial_current_offset;
93+
out_arena->m_total_size = m_previous_offset + size;
9194
}
9295

9396
ArenaTemp BeginTempArena(ArenaAllocator* arena)

ZEngine/ZEngine/Core/Memory/Allocator.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ namespace ZEngine::Core::Memory
3030

3131
void CreateSubArena(size_t size, ArenaAllocator* out_arena);
3232

33-
uint8_t* m_memory = nullptr;
34-
size_t m_total_size = 0;
35-
size_t m_current_offset = 0;
36-
size_t m_previous_offset = 0;
33+
uint8_t* m_memory = nullptr;
34+
size_t m_total_size = 0;
35+
size_t m_initial_current_offset = 0;
36+
size_t m_initial_previous_offset = 0;
37+
size_t m_current_offset = 0;
38+
size_t m_previous_offset = 0;
3739
}; // struct ArenaAllocator
3840

3941
struct PoolFreeNode

ZEngine/ZEngine/Hardwares/VulkanDevice.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ namespace ZEngine::Hardwares
4545
DirtyResources.Initialize(arena, 300);
4646
DirtyBuffers.Initialize(arena, 500);
4747
DirtyBufferImages.Initialize(arena, 300);
48-
49-
// m_queue_map.init(arena, 4);
48+
ShaderCaches.init(arena, 10);
49+
m_queue_map.init(arena, 4);
5050

5151
m_layer.QueryInstanceLayerProperties(arena);
5252

@@ -299,14 +299,14 @@ namespace ZEngine::Hardwares
299299
/*Create Vulkan Graphic Queue*/
300300
VkQueue graphic_queue = VK_NULL_HANDLE;
301301
vkGetDeviceQueue(LogicalDevice, GraphicFamilyIndex, 0, &graphic_queue);
302-
m_queue_map.emplace(Rendering::QueueType::GRAPHIC_QUEUE, std::move(graphic_queue));
302+
m_queue_map.insert(Rendering::QueueType::GRAPHIC_QUEUE, std::move(graphic_queue));
303303

304304
/*Create Vulkan Transfer Queue*/
305305
if (HasSeperateTransfertQueueFamily)
306306
{
307307
VkQueue transfer_queue = VK_NULL_HANDLE;
308308
vkGetDeviceQueue(LogicalDevice, TransferFamilyIndex, 0, &transfer_queue);
309-
m_queue_map.emplace(Rendering::QueueType::TRANSFER_QUEUE, std::move(transfer_queue));
309+
m_queue_map.insert(Rendering::QueueType::TRANSFER_QUEUE, std::move(transfer_queue));
310310
}
311311

312312
/* Surface format selection */
@@ -423,6 +423,7 @@ namespace ZEngine::Hardwares
423423
IndirectBufferSetManager.Dispose();
424424
IndexBufferSetManager.Dispose();
425425
UniformBufferSetManager.Dispose();
426+
ShaderManager.Dispose();
426427

427428
EnqueuedCommandbuffers.clear();
428429
SwapchainSignalFences.clear();
@@ -1363,8 +1364,13 @@ namespace ZEngine::Hardwares
13631364
const char* vertex_name_part = "_vertex.spv";
13641365
const char* fragment_name_part = "_fragment.spv";
13651366

1366-
auto handle = ShaderManager.Create();
1367-
auto shader = ShaderManager.Access(handle);
1367+
if (ShaderCaches.contains(spec.Name))
1368+
{
1369+
return ShaderCaches[spec.Name];
1370+
}
1371+
1372+
auto handle = ShaderManager.Create();
1373+
auto shader = ShaderManager.Access(handle);
13681374

13691375
if (shader)
13701376
{

ZEngine/ZEngine/Hardwares/VulkanDevice.h

Lines changed: 103 additions & 103 deletions
Large diffs are not rendered by default.

ZEngine/ZEngine/Rendering/Renderers/Pipelines/RendererPipeline.cpp

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,15 @@ namespace ZEngine::Rendering::Renderers::Pipelines
5454
* Vertex Input
5555
*/
5656
Array<VkVertexInputBindingDescription> vertex_input_bindings = {};
57-
vertex_input_bindings.init(scratch.Arena, 5, Specification.VertexInputBindingSpecifications.size());
57+
vertex_input_bindings.init(scratch.Arena, 5);
5858
for (unsigned i = 0; i < Specification.VertexInputBindingSpecifications.size(); ++i)
5959
{
6060
auto& input = Specification.VertexInputBindingSpecifications[i];
6161
vertex_input_bindings.push(VkVertexInputBindingDescription{.binding = input.Binding, .stride = input.Stride, .inputRate = (VkVertexInputRate) input.Rate});
6262
}
6363

6464
Array<VkVertexInputAttributeDescription> vertex_input_attributes = {};
65+
vertex_input_attributes.init(scratch.Arena, 5);
6566
for (unsigned i = 0; i < Specification.VertexInputAttributeSpecifications.size(); ++i)
6667
{
6768
auto& input = Specification.VertexInputAttributeSpecifications[i];
@@ -147,40 +148,37 @@ namespace ZEngine::Rendering::Renderers::Pipelines
147148
/*
148149
* Pipeline layout
149150
*/
150-
auto& descriptor_set_layout_collection = Shader->SetLayouts;
151-
const auto& push_constant_collection = Shader->PushConstants;
152151
VkPipelineLayoutCreateInfo pipeline_layout_create_info = {};
153152
pipeline_layout_create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
154-
pipeline_layout_create_info.setLayoutCount = descriptor_set_layout_collection.size(); // Optional
155-
pipeline_layout_create_info.pSetLayouts = descriptor_set_layout_collection.data(); // Optional
156-
pipeline_layout_create_info.pushConstantRangeCount = push_constant_collection.size();
157-
pipeline_layout_create_info.pPushConstantRanges = push_constant_collection.data();
153+
pipeline_layout_create_info.setLayoutCount = Shader->SetLayouts.size(); // Optional
154+
pipeline_layout_create_info.pSetLayouts = Shader->SetLayouts.data(); // Optional
155+
pipeline_layout_create_info.pushConstantRangeCount = Shader->PushConstants.size();
156+
pipeline_layout_create_info.pPushConstantRanges = Shader->PushConstants.data();
158157
pipeline_layout_create_info.flags = 0;
159158
pipeline_layout_create_info.pNext = nullptr;
160159
ZENGINE_VALIDATE_ASSERT(vkCreatePipelineLayout(Device->LogicalDevice, &(pipeline_layout_create_info), nullptr, &Layout) == VK_SUCCESS, "Failed to create pipeline layout")
161160
/*
162161
* Graphic Pipeline Creation
163162
*/
164-
const auto& shader_create_info_collection = Shader->ShaderCreateInfos;
165-
VkGraphicsPipelineCreateInfo graphic_pipeline_create_info = {};
166-
graphic_pipeline_create_info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
167-
graphic_pipeline_create_info.stageCount = shader_create_info_collection.size();
168-
graphic_pipeline_create_info.pStages = shader_create_info_collection.data();
169-
graphic_pipeline_create_info.pVertexInputState = &(vertex_input_state_create_info);
170-
graphic_pipeline_create_info.pInputAssemblyState = &(input_assembly_state_create_info);
171-
graphic_pipeline_create_info.pViewportState = &(viewport_state_create_info);
172-
graphic_pipeline_create_info.pRasterizationState = &(rasterization_create_info);
173-
graphic_pipeline_create_info.pMultisampleState = &(multisample_state_create_info);
174-
graphic_pipeline_create_info.pDepthStencilState = Specification.EnableDepthTest ? &(depth_stencil_state_create_info) : nullptr;
175-
graphic_pipeline_create_info.pColorBlendState = &(color_blend_state_create_info);
176-
graphic_pipeline_create_info.pDynamicState = &(dynamic_state_create_info);
177-
graphic_pipeline_create_info.layout = Layout;
178-
graphic_pipeline_create_info.renderPass = Specification.Attachment->GetHandle();
179-
graphic_pipeline_create_info.subpass = 0;
180-
graphic_pipeline_create_info.basePipelineHandle = VK_NULL_HANDLE; // Optional
181-
graphic_pipeline_create_info.basePipelineIndex = -1; // Optional
182-
graphic_pipeline_create_info.flags = 0; // Optional
183-
graphic_pipeline_create_info.pNext = nullptr; // Optional
163+
VkGraphicsPipelineCreateInfo graphic_pipeline_create_info = {};
164+
graphic_pipeline_create_info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
165+
graphic_pipeline_create_info.stageCount = Shader->ShaderCreateInfos.size();
166+
graphic_pipeline_create_info.pStages = Shader->ShaderCreateInfos.data();
167+
graphic_pipeline_create_info.pVertexInputState = &(vertex_input_state_create_info);
168+
graphic_pipeline_create_info.pInputAssemblyState = &(input_assembly_state_create_info);
169+
graphic_pipeline_create_info.pViewportState = &(viewport_state_create_info);
170+
graphic_pipeline_create_info.pRasterizationState = &(rasterization_create_info);
171+
graphic_pipeline_create_info.pMultisampleState = &(multisample_state_create_info);
172+
graphic_pipeline_create_info.pDepthStencilState = Specification.EnableDepthTest ? &(depth_stencil_state_create_info) : nullptr;
173+
graphic_pipeline_create_info.pColorBlendState = &(color_blend_state_create_info);
174+
graphic_pipeline_create_info.pDynamicState = &(dynamic_state_create_info);
175+
graphic_pipeline_create_info.layout = Layout;
176+
graphic_pipeline_create_info.renderPass = Specification.Attachment->GetHandle();
177+
graphic_pipeline_create_info.subpass = 0;
178+
graphic_pipeline_create_info.basePipelineHandle = VK_NULL_HANDLE; // Optional
179+
graphic_pipeline_create_info.basePipelineIndex = -1; // Optional
180+
graphic_pipeline_create_info.flags = 0; // Optional
181+
graphic_pipeline_create_info.pNext = nullptr; // Optional
184182
ZENGINE_VALIDATE_ASSERT(vkCreateGraphicsPipelines(Device->LogicalDevice, VK_NULL_HANDLE, 1, &graphic_pipeline_create_info, nullptr, &Handle) == VK_SUCCESS, "Failed to create Graphics Pipeline")
185183

186184
ZReleaseScratch(scratch);

ZEngine/ZEngine/Rendering/Renderers/RenderPasses/RenderPass.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@ namespace ZEngine::Rendering::Renderers::RenderPasses
3030
{
3131
Specifications::AttachmentSpecification attachment_specification = {};
3232
attachment_specification.BindPoint = PipelineBindPoint::GRAPHIC;
33+
attachment_specification.ColorAttachements.init(device->Arena, 4);
34+
attachment_specification.SubpassSpecifications.init(device->Arena, 4);
35+
attachment_specification.ColorsMap.init(device->Arena, 4);
36+
attachment_specification.DependenciesMap.init(device->Arena, 4);
37+
attachment_specification.SubpassDependencies.init(device->Arena, 4);
3338

34-
uint32_t color_map_index = 0;
39+
uint32_t color_map_index = 0;
3540
for (const auto& handle : Specification.Inputs)
3641
{
3742
const auto& texture = device->GlobalTextures.Access(handle);
@@ -451,24 +456,41 @@ namespace ZEngine::Rendering::Renderers::RenderPasses
451456

452457
RenderPassBuilder& RenderPassBuilder::UseRenderTarget(const Textures::TextureHandle& target)
453458
{
459+
if (m_spec.ExternalOutputs.capacity() <= 0)
460+
{
461+
m_spec.ExternalOutputs.init(Arena, 4);
462+
}
463+
454464
m_spec.ExternalOutputs.push(target);
455465
return *this;
456466
}
457467

458468
RenderPassBuilder& RenderPassBuilder::AddRenderTarget(const Specifications::TextureSpecification& target_spec)
459469
{
470+
if (m_spec.Outputs.capacity() <= 0)
471+
{
472+
m_spec.Outputs.init(Arena, 4);
473+
}
460474
m_spec.Outputs.push(target_spec);
461475
return *this;
462476
}
463477

464478
RenderPassBuilder& RenderPassBuilder::AddInputAttachment(const Textures::TextureHandle& input)
465479
{
480+
if (m_spec.Inputs.capacity() <= 0)
481+
{
482+
m_spec.Inputs.init(Arena, 4);
483+
}
466484
m_spec.Inputs.push(input);
467485
return *this;
468486
}
469487

470488
RenderPassBuilder& RenderPassBuilder::AddInputTexture(std::string_view key, const Textures::TextureHandle& input)
471489
{
490+
if (m_spec.InputTextures.capacity() <= 0)
491+
{
492+
m_spec.InputTextures.init(Arena, 4);
493+
}
472494
m_spec.InputTextures[key.data()] = input;
473495
return *this;
474496
}

0 commit comments

Comments
 (0)