Debian Codegraph @

servers/rendering/renderer_rd/pipeline_deferred_rd.h

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