| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "servers/rendering/rendering_device.h" |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 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 |
|
| 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 |
}; |