|
| 1 | +/**************************************************************************/ |
| 2 | +/* pipeline_deferred_rd.h */ |
| 3 | +/**************************************************************************/ |
| 4 | +/* This file is part of: */ |
| 5 | +/* REDOT ENGINE */ |
| 6 | +/* https://redotengine.org */ |
| 7 | +/**************************************************************************/ |
| 8 | +/* Copyright (c) 2024-present Redot Engine contributors */ |
| 9 | +/* (see REDOT_AUTHORS.md) */ |
| 10 | +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 11 | +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 12 | +/* */ |
| 13 | +/* Permission is hereby granted, free of charge, to any person obtaining */ |
| 14 | +/* a copy of this software and associated documentation files (the */ |
| 15 | +/* "Software"), to deal in the Software without restriction, including */ |
| 16 | +/* without limitation the rights to use, copy, modify, merge, publish, */ |
| 17 | +/* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 18 | +/* permit persons to whom the Software is furnished to do so, subject to */ |
| 19 | +/* the following conditions: */ |
| 20 | +/* */ |
| 21 | +/* The above copyright notice and this permission notice shall be */ |
| 22 | +/* included in all copies or substantial portions of the Software. */ |
| 23 | +/* */ |
| 24 | +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 25 | +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 26 | +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 27 | +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 28 | +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 29 | +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 30 | +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 31 | +/**************************************************************************/ |
| 32 | + |
| 33 | +#pragma once |
| 34 | + |
| 35 | +#include "servers/rendering/rendering_device.h" |
| 36 | + |
| 37 | +// Helper class for automatically deferring compilation of a pipeline to a background task. |
| 38 | +// When attempting to retrieve the pipeline with the getter, the caller will automatically |
| 39 | +// wait for it to be ready. |
| 40 | + |
| 41 | +class PipelineDeferredRD { |
| 42 | +protected: |
| 43 | + struct CreationParameters { |
| 44 | + RID shader; |
| 45 | + RD::FramebufferFormatID framebuffer_format; |
| 46 | + RD::VertexFormatID vertex_format; |
| 47 | + RD::RenderPrimitive render_primitive; |
| 48 | + RD::PipelineRasterizationState rasterization_state; |
| 49 | + RD::PipelineMultisampleState multisample_state; |
| 50 | + RD::PipelineDepthStencilState depth_stencil_state; |
| 51 | + RD::PipelineColorBlendState blend_state; |
| 52 | + BitField<RD::PipelineDynamicStateFlags> dynamic_state_flags; |
| 53 | + uint32_t for_render_pass; |
| 54 | + Vector<RD::PipelineSpecializationConstant> specialization_constants; |
| 55 | + bool is_compute; |
| 56 | + }; |
| 57 | + |
| 58 | + RID pipeline; |
| 59 | + WorkerThreadPool::TaskID task = WorkerThreadPool::INVALID_TASK_ID; |
| 60 | + |
| 61 | + void _create(const CreationParameters &c) { |
| 62 | + if (c.is_compute) { |
| 63 | + pipeline = RD::get_singleton()->compute_pipeline_create(c.shader, c.specialization_constants); |
| 64 | + } else { |
| 65 | + pipeline = RD::get_singleton()->render_pipeline_create(c.shader, c.framebuffer_format, c.vertex_format, c.render_primitive, c.rasterization_state, c.multisample_state, c.depth_stencil_state, c.blend_state, c.dynamic_state_flags, c.for_render_pass, c.specialization_constants); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + void _start(const CreationParameters &c) { |
| 70 | + free(); |
| 71 | + task = WorkerThreadPool::get_singleton()->add_template_task(this, &PipelineDeferredRD::_create, c, true, "PipelineCompilation"); |
| 72 | + } |
| 73 | + |
| 74 | + void _wait() { |
| 75 | + if (task != WorkerThreadPool::INVALID_TASK_ID) { |
| 76 | + const Error err = WorkerThreadPool::get_singleton()->wait_for_task_completion(task); |
| 77 | + ERR_FAIL_COND_MSG(err != OK, "Failed to wait for pipeline compilation task completion."); |
| 78 | + task = WorkerThreadPool::INVALID_TASK_ID; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | +public: |
| 83 | + PipelineDeferredRD() { |
| 84 | + // Default constructor. |
| 85 | + } |
| 86 | + |
| 87 | + ~PipelineDeferredRD() { |
| 88 | + _wait(); // Block until worker thread completes |
| 89 | +#ifdef DEV_ENABLED |
| 90 | + ERR_FAIL_COND_MSG(pipeline.is_valid(), "'free()' must be called manually before deconstruction and before the corresponding shader is freed."); |
| 91 | +#endif |
| 92 | + } |
| 93 | + |
| 94 | + void create_render_pipeline(RID p_shader, RD::FramebufferFormatID p_framebuffer_format, RD::VertexFormatID p_vertex_format, RD::RenderPrimitive p_render_primitive, const RD::PipelineRasterizationState &p_rasterization_state, const RD::PipelineMultisampleState &p_multisample_state, const RD::PipelineDepthStencilState &p_depth_stencil_state, const RD::PipelineColorBlendState &p_blend_state, BitField<RD::PipelineDynamicStateFlags> p_dynamic_state_flags = 0, uint32_t p_for_render_pass = 0, const Vector<RD::PipelineSpecializationConstant> &p_specialization_constants = Vector<RD::PipelineSpecializationConstant>()) { |
| 95 | + CreationParameters c; |
| 96 | + c.shader = p_shader; |
| 97 | + c.framebuffer_format = p_framebuffer_format; |
| 98 | + c.vertex_format = p_vertex_format; |
| 99 | + c.render_primitive = p_render_primitive; |
| 100 | + c.rasterization_state = p_rasterization_state; |
| 101 | + c.multisample_state = p_multisample_state; |
| 102 | + c.depth_stencil_state = p_depth_stencil_state; |
| 103 | + c.blend_state = p_blend_state; |
| 104 | + c.dynamic_state_flags = p_dynamic_state_flags; |
| 105 | + c.for_render_pass = p_for_render_pass; |
| 106 | + c.specialization_constants = p_specialization_constants; |
| 107 | + c.is_compute = false; |
| 108 | + _start(c); |
| 109 | + } |
| 110 | + |
| 111 | + void create_compute_pipeline(RID p_shader, const Vector<RD::PipelineSpecializationConstant> &p_specialization_constants = Vector<RD::PipelineSpecializationConstant>()) { |
| 112 | + CreationParameters c = {}; |
| 113 | + c.shader = p_shader; |
| 114 | + c.specialization_constants = p_specialization_constants; |
| 115 | + c.is_compute = true; |
| 116 | + _start(c); |
| 117 | + } |
| 118 | + |
| 119 | + RID get_rid() { |
| 120 | + _wait(); |
| 121 | + return pipeline; |
| 122 | + } |
| 123 | + |
| 124 | + void free() { |
| 125 | + _wait(); |
| 126 | + |
| 127 | + if (pipeline.is_valid()) { |
| 128 | +#ifdef DEV_ENABLED |
| 129 | + ERR_FAIL_COND_MSG(!(RD::get_singleton()->render_pipeline_is_valid(pipeline) || RD::get_singleton()->compute_pipeline_is_valid(pipeline)), "`free()` must be called manually before the dependent shader is freed."); |
| 130 | +#endif |
| 131 | + RD::get_singleton()->free_rid(pipeline); |
| 132 | + pipeline = RID(); |
| 133 | + } |
| 134 | + } |
| 135 | +}; |
0 commit comments