Skip to content

Commit

Permalink
MaterialPipelineCache: solve render pass dependency in a neater way
Browse files Browse the repository at this point in the history
  • Loading branch information
fleroviux committed Aug 9, 2023
1 parent 440197e commit e513a30
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
4 changes: 3 additions & 1 deletion app/runtime/src/main_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ namespace zephyr {
const bool new_mesh = &mesh != current_mesh;

if(new_material || new_mesh_layout) {
GraphicsPipeline* pipeline = m_material_pipeline_cache->GetGraphicsPipeline(Technique::Forward, &material, &mesh, m_render_pass);
GraphicsPipeline* pipeline = m_material_pipeline_cache->GetGraphicsPipeline(Technique::Forward, &material, &mesh);

command_buffer->BindPipeline(pipeline);
command_buffer->BindBindGroup(PipelineBindPoint::Graphics, pipeline->GetLayout(), 0, bind_group.get());
Expand Down Expand Up @@ -255,6 +255,8 @@ namespace zephyr {

void MainWindow::CreateMaterialPipelineCache() {
m_material_pipeline_cache = std::make_shared<MaterialPipelineCache>(m_render_device);

m_material_pipeline_cache->RegisterTechnique(Technique::Forward, m_render_pass);
}

void MainWindow::CreateFences() {
Expand Down
14 changes: 13 additions & 1 deletion app/runtime/src/renderer/material_pipeline_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ namespace zephyr {

MaterialPipelineCache::MaterialPipelineCache(std::shared_ptr<RenderDevice> render_device)
: m_render_device{std::move(render_device)} {
m_technique_render_pass.fill(nullptr);

CreateBindGroupLayout();
}

GraphicsPipeline* MaterialPipelineCache::GetGraphicsPipeline(Technique technique, const Material* material, const Mesh3D* mesh, const std::shared_ptr<RenderPass>& render_pass) {
void MaterialPipelineCache::RegisterTechnique(Technique technique, std::shared_ptr<RenderPass> render_pass) {
m_technique_render_pass[(u8)technique] = std::move(render_pass);
}

GraphicsPipeline* MaterialPipelineCache::GetGraphicsPipeline(Technique technique, const Material* material, const Mesh3D* mesh) {
const u64 variant_key = BuildVariantKey(technique, material, mesh);
const Key key{&material->GetShader(), variant_key, technique};

Expand All @@ -26,6 +32,12 @@ namespace zephyr {
std::shared_ptr<ShaderModule> vert_shader = m_render_device->CreateShaderModule(mesh_vert, sizeof(mesh_vert));
std::shared_ptr<ShaderModule> frag_shader = m_render_device->CreateShaderModule(mesh_frag, sizeof(mesh_frag));

std::shared_ptr<RenderPass> render_pass = m_technique_render_pass[(u8)technique];

if(!render_pass) {
ZEPHYR_PANIC("No render pass has been registered for technique: {}", (u8)technique);
}

pipeline_builder->SetDynamicViewportEnable(true);
pipeline_builder->SetShaderModule(ShaderStage::Vertex, vert_shader);
pipeline_builder->SetShaderModule(ShaderStage::Fragment, frag_shader);
Expand Down
7 changes: 6 additions & 1 deletion app/runtime/src/renderer/material_pipeline_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ namespace zephyr {
public:
explicit MaterialPipelineCache(std::shared_ptr<RenderDevice> render_device);

void RegisterTechnique(Technique technique, std::shared_ptr<RenderPass> render_pass);

// @todo: use references (and just buffer, texture and sampler caches accordingly)
// @todo: get rid of the render pass parameter
GraphicsPipeline* GetGraphicsPipeline(Technique technique, const Material* material, const Mesh3D* mesh, const std::shared_ptr<RenderPass>& render_pass);
GraphicsPipeline* GetGraphicsPipeline(Technique technique, const Material* material, const Mesh3D* mesh);

private:
struct Key {
Expand Down Expand Up @@ -58,6 +60,9 @@ namespace zephyr {
std::shared_ptr<RenderDevice> m_render_device;
std::shared_ptr<BindGroupLayout> m_bind_group_layout;
std::unordered_map<const Key, Entry, KeyHash> m_cache;
std::array<std::shared_ptr<RenderPass>, 256> m_technique_render_pass;

static_assert(std::is_same_v<std::underlying_type_t<Technique>, u8>);
};

} // namespace zephyr

0 comments on commit e513a30

Please sign in to comment.