Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/servers/rendering/renderer_rd/pipeline_deferred_rd.h
12222 views
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
free();
86
}
87
88
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>()) {
89
CreationParameters c;
90
c.shader = p_shader;
91
c.framebuffer_format = p_framebuffer_format;
92
c.vertex_format = p_vertex_format;
93
c.render_primitive = p_render_primitive;
94
c.rasterization_state = p_rasterization_state;
95
c.multisample_state = p_multisample_state;
96
c.depth_stencil_state = p_depth_stencil_state;
97
c.blend_state = p_blend_state;
98
c.dynamic_state_flags = p_dynamic_state_flags;
99
c.for_render_pass = p_for_render_pass;
100
c.specialization_constants = p_specialization_constants;
101
c.is_compute = false;
102
_start(c);
103
}
104
105
void create_compute_pipeline(RID p_shader, const Vector<RD::PipelineSpecializationConstant> &p_specialization_constants = Vector<RD::PipelineSpecializationConstant>()) {
106
CreationParameters c = {};
107
c.shader = p_shader;
108
c.specialization_constants = p_specialization_constants;
109
c.is_compute = true;
110
_start(c);
111
}
112
113
RID get_rid() {
114
_wait();
115
return pipeline;
116
}
117
118
void free() {
119
_wait();
120
121
if (pipeline.is_valid()) {
122
RD::get_singleton()->free_rid(pipeline);
123
pipeline = RID();
124
}
125
}
126
};
127
128