1/**************************************************************************/
2/* pipeline_cache_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#ifndef PIPELINE_CACHE_RD_H
32#define PIPELINE_CACHE_RD_H
33
34#include "core/os/spin_lock.h"
35#include "servers/rendering/rendering_device.h"
36
37class PipelineCacheRD {
38 SpinLock spin_lock;
39
40 RID shader;
41 uint32_t input_mask;
42
43 RD::RenderPrimitive render_primitive;
44 RD::PipelineRasterizationState rasterization_state;
45 RD::PipelineMultisampleState multisample_state;
46 RD::PipelineDepthStencilState depth_stencil_state;
47 RD::PipelineColorBlendState blend_state;
48 int dynamic_state_flags = 0;
49 Vector<RD::PipelineSpecializationConstant> base_specialization_constants;
50
51 struct Version {
52 RD::VertexFormatID vertex_id;
53 RD::FramebufferFormatID framebuffer_id;
54 uint32_t render_pass;
55 bool wireframe;
56 uint32_t bool_specializations;
57 RID pipeline;
58 };
59
60 Version *versions = nullptr;
61 uint32_t version_count;
62
63 RID _generate_version(RD::VertexFormatID p_vertex_format_id, RD::FramebufferFormatID p_framebuffer_format_id, bool p_wireframe, uint32_t p_render_pass, uint32_t p_bool_specializations = 0);
64
65 void _clear();
66
67public:
68 void setup(RID p_shader, RD::RenderPrimitive p_primitive, const RD::PipelineRasterizationState &p_rasterization_state, RD::PipelineMultisampleState p_multisample, const RD::PipelineDepthStencilState &p_depth_stencil_state, const RD::PipelineColorBlendState &p_blend_state, int p_dynamic_state_flags = 0, const Vector<RD::PipelineSpecializationConstant> &p_base_specialization_constants = Vector<RD::PipelineSpecializationConstant>());
69 void update_specialization_constants(const Vector<RD::PipelineSpecializationConstant> &p_base_specialization_constants);
70 void update_shader(RID p_shader);
71
72 _FORCE_INLINE_ RID get_render_pipeline(RD::VertexFormatID p_vertex_format_id, RD::FramebufferFormatID p_framebuffer_format_id, bool p_wireframe = false, uint32_t p_render_pass = 0, uint32_t p_bool_specializations = 0) {
73#ifdef DEBUG_ENABLED
74 ERR_FAIL_COND_V_MSG(shader.is_null(), RID(),
75 "Attempted to use an unused shader variant (shader is null),");
76#endif
77
78 spin_lock.lock();
79 p_wireframe |= rasterization_state.wireframe;
80
81 RID result;
82 for (uint32_t i = 0; i < version_count; i++) {
83 if (versions[i].vertex_id == p_vertex_format_id && versions[i].framebuffer_id == p_framebuffer_format_id && versions[i].wireframe == p_wireframe && versions[i].render_pass == p_render_pass && versions[i].bool_specializations == p_bool_specializations) {
84 result = versions[i].pipeline;
85 spin_lock.unlock();
86 return result;
87 }
88 }
89 result = _generate_version(p_vertex_format_id, p_framebuffer_format_id, p_wireframe, p_render_pass, p_bool_specializations);
90 spin_lock.unlock();
91 return result;
92 }
93
94 _FORCE_INLINE_ uint32_t get_vertex_input_mask() {
95 if (input_mask == 0) {
96 ERR_FAIL_COND_V(shader.is_null(), 0);
97 input_mask = RD::get_singleton()->shader_get_vertex_input_attribute_mask(shader);
98 }
99 return input_mask;
100 }
101 void clear();
102 PipelineCacheRD();
103 ~PipelineCacheRD();
104};
105
106#endif // PIPELINE_CACHE_RD_H
107