Path: blob/master/servers/rendering/renderer_rd/pipeline_cache_rd.cpp
20791 views
/**************************************************************************/1/* pipeline_cache_rd.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "pipeline_cache_rd.h"3132#include "core/os/memory.h"3334RID 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) {35RD::PipelineMultisampleState multisample_state_version = multisample_state;36multisample_state_version.sample_count = RD::get_singleton()->framebuffer_format_get_texture_samples(p_framebuffer_format_id, p_render_pass);3738bool wireframe = p_wireframe;3940RD::PipelineRasterizationState raster_state_version = rasterization_state;41raster_state_version.wireframe = wireframe;4243Vector<RD::PipelineSpecializationConstant> specialization_constants = base_specialization_constants;4445uint32_t bool_index = 0;46uint32_t bool_specializations = p_bool_specializations;47while (bool_specializations) {48RD::PipelineSpecializationConstant sc;49sc.bool_value = bool(bool_specializations & (1 << bool_index));50sc.constant_id = bool_index;51sc.type = RD::PIPELINE_SPECIALIZATION_CONSTANT_TYPE_BOOL;52specialization_constants.push_back(sc);53bool_specializations &= ~(1 << bool_index);54bool_index++;55}5657RID 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);58ERR_FAIL_COND_V(pipeline.is_null(), RID());59versions = static_cast<Version *>(memrealloc(versions, sizeof(Version) * (version_count + 1)));60versions[version_count].framebuffer_id = p_framebuffer_format_id;61versions[version_count].vertex_id = p_vertex_format_id;62versions[version_count].wireframe = wireframe;63versions[version_count].pipeline = pipeline;64versions[version_count].render_pass = p_render_pass;65versions[version_count].bool_specializations = p_bool_specializations;66version_count++;67return pipeline;68}6970void PipelineCacheRD::_clear() {71// TODO: Clear should probably recompile all the variants already compiled instead to avoid stalls? Needs discussion.72if (versions) {73for (uint32_t i = 0; i < version_count; i++) {74//shader may be gone, so this may not be valid75if (RD::get_singleton()->render_pipeline_is_valid(versions[i].pipeline)) {76RD::get_singleton()->free_rid(versions[i].pipeline);77}78}79version_count = 0;80memfree(versions);81versions = nullptr;82}83}8485void 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) {86ERR_FAIL_COND(p_shader.is_null());87_clear();88shader = p_shader;89render_primitive = p_primitive;90rasterization_state = p_rasterization_state;91multisample_state = p_multisample;92depth_stencil_state = p_depth_stencil_state;93blend_state = p_blend_state;94dynamic_state_flags = p_dynamic_state_flags;95base_specialization_constants = p_base_specialization_constants;96}97void PipelineCacheRD::update_specialization_constants(const Vector<RD::PipelineSpecializationConstant> &p_base_specialization_constants) {98base_specialization_constants = p_base_specialization_constants;99_clear();100}101102void PipelineCacheRD::update_shader(RID p_shader) {103ERR_FAIL_COND(p_shader.is_null());104_clear();105setup(p_shader, render_primitive, rasterization_state, multisample_state, depth_stencil_state, blend_state, dynamic_state_flags);106}107108void PipelineCacheRD::clear() {109_clear();110shader = RID(); //clear shader111}112113PipelineCacheRD::PipelineCacheRD() {114version_count = 0;115versions = nullptr;116}117118PipelineCacheRD::~PipelineCacheRD() {119_clear();120}121122123