| 1 | /**************************************************************************/ | 
|---|
| 2 | /*  pipeline_cache_rd.cpp                                                 */ | 
|---|
| 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 | #include "pipeline_cache_rd.h" | 
|---|
| 32 |  | 
|---|
| 33 | #include "core/os/memory.h" | 
|---|
| 34 |  | 
|---|
| 35 | RID PipelineCacheRD::_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) { | 
|---|
| 36 | RD::PipelineMultisampleState multisample_state_version = multisample_state; | 
|---|
| 37 | multisample_state_version.sample_count = RD::get_singleton()->framebuffer_format_get_texture_samples(p_framebuffer_format_id, p_render_pass); | 
|---|
| 38 |  | 
|---|
| 39 | bool wireframe = p_wireframe; | 
|---|
| 40 |  | 
|---|
| 41 | RD::PipelineRasterizationState raster_state_version = rasterization_state; | 
|---|
| 42 | raster_state_version.wireframe = wireframe; | 
|---|
| 43 |  | 
|---|
| 44 | Vector<RD::PipelineSpecializationConstant> specialization_constants = base_specialization_constants; | 
|---|
| 45 |  | 
|---|
| 46 | uint32_t bool_index = 0; | 
|---|
| 47 | uint32_t bool_specializations = p_bool_specializations; | 
|---|
| 48 | while (bool_specializations) { | 
|---|
| 49 | if (bool_specializations & (1 << bool_index)) { | 
|---|
| 50 | RD::PipelineSpecializationConstant sc; | 
|---|
| 51 | sc.bool_value = true; | 
|---|
| 52 | sc.constant_id = bool_index; | 
|---|
| 53 | sc.type = RD::PIPELINE_SPECIALIZATION_CONSTANT_TYPE_BOOL; | 
|---|
| 54 | specialization_constants.push_back(sc); | 
|---|
| 55 | bool_specializations &= ~(1 << bool_index); | 
|---|
| 56 | } | 
|---|
| 57 | bool_index++; | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 | RID pipeline = RD::get_singleton()->render_pipeline_create(shader, p_framebuffer_format_id, p_vertex_format_id, render_primitive, raster_state_version, multisample_state_version, depth_stencil_state, blend_state, dynamic_state_flags, p_render_pass, specialization_constants); | 
|---|
| 61 | ERR_FAIL_COND_V(pipeline.is_null(), RID()); | 
|---|
| 62 | versions = static_cast<Version *>(memrealloc(versions, sizeof(Version) * (version_count + 1))); | 
|---|
| 63 | versions[version_count].framebuffer_id = p_framebuffer_format_id; | 
|---|
| 64 | versions[version_count].vertex_id = p_vertex_format_id; | 
|---|
| 65 | versions[version_count].wireframe = wireframe; | 
|---|
| 66 | versions[version_count].pipeline = pipeline; | 
|---|
| 67 | versions[version_count].render_pass = p_render_pass; | 
|---|
| 68 | versions[version_count].bool_specializations = p_bool_specializations; | 
|---|
| 69 | version_count++; | 
|---|
| 70 | return pipeline; | 
|---|
| 71 | } | 
|---|
| 72 |  | 
|---|
| 73 | void PipelineCacheRD::_clear() { | 
|---|
| 74 | // TODO: Clear should probably recompile all the variants already compiled instead to avoid stalls? Needs discussion. | 
|---|
| 75 | if (versions) { | 
|---|
| 76 | for (uint32_t i = 0; i < version_count; i++) { | 
|---|
| 77 | //shader may be gone, so this may not be valid | 
|---|
| 78 | if (RD::get_singleton()->render_pipeline_is_valid(versions[i].pipeline)) { | 
|---|
| 79 | RD::get_singleton()->free(versions[i].pipeline); | 
|---|
| 80 | } | 
|---|
| 81 | } | 
|---|
| 82 | version_count = 0; | 
|---|
| 83 | memfree(versions); | 
|---|
| 84 | versions = nullptr; | 
|---|
| 85 | } | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | void PipelineCacheRD::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, const Vector<RD::PipelineSpecializationConstant> &p_base_specialization_constants) { | 
|---|
| 89 | ERR_FAIL_COND(p_shader.is_null()); | 
|---|
| 90 | _clear(); | 
|---|
| 91 | shader = p_shader; | 
|---|
| 92 | input_mask = 0; | 
|---|
| 93 | render_primitive = p_primitive; | 
|---|
| 94 | rasterization_state = p_rasterization_state; | 
|---|
| 95 | multisample_state = p_multisample; | 
|---|
| 96 | depth_stencil_state = p_depth_stencil_state; | 
|---|
| 97 | blend_state = p_blend_state; | 
|---|
| 98 | dynamic_state_flags = p_dynamic_state_flags; | 
|---|
| 99 | base_specialization_constants = p_base_specialization_constants; | 
|---|
| 100 | } | 
|---|
| 101 | void PipelineCacheRD::update_specialization_constants(const Vector<RD::PipelineSpecializationConstant> &p_base_specialization_constants) { | 
|---|
| 102 | base_specialization_constants = p_base_specialization_constants; | 
|---|
| 103 | _clear(); | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | void PipelineCacheRD::update_shader(RID p_shader) { | 
|---|
| 107 | ERR_FAIL_COND(p_shader.is_null()); | 
|---|
| 108 | _clear(); | 
|---|
| 109 | setup(p_shader, render_primitive, rasterization_state, multisample_state, depth_stencil_state, blend_state, dynamic_state_flags); | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | void PipelineCacheRD::clear() { | 
|---|
| 113 | _clear(); | 
|---|
| 114 | shader = RID(); //clear shader | 
|---|
| 115 | input_mask = 0; | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | PipelineCacheRD::PipelineCacheRD() { | 
|---|
| 119 | version_count = 0; | 
|---|
| 120 | versions = nullptr; | 
|---|
| 121 | input_mask = 0; | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 | PipelineCacheRD::~PipelineCacheRD() { | 
|---|
| 125 | _clear(); | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|