Path: blob/master/servers/rendering/renderer_rd/pipeline_cache_rd.cpp
10279 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) {48if (bool_specializations & (1 << bool_index)) {49RD::PipelineSpecializationConstant sc;50sc.bool_value = true;51sc.constant_id = bool_index;52sc.type = RD::PIPELINE_SPECIALIZATION_CONSTANT_TYPE_BOOL;53specialization_constants.push_back(sc);54bool_specializations &= ~(1 << bool_index);55}56bool_index++;57}5859RID 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);60ERR_FAIL_COND_V(pipeline.is_null(), RID());61versions = static_cast<Version *>(memrealloc(versions, sizeof(Version) * (version_count + 1)));62versions[version_count].framebuffer_id = p_framebuffer_format_id;63versions[version_count].vertex_id = p_vertex_format_id;64versions[version_count].wireframe = wireframe;65versions[version_count].pipeline = pipeline;66versions[version_count].render_pass = p_render_pass;67versions[version_count].bool_specializations = p_bool_specializations;68version_count++;69return pipeline;70}7172void PipelineCacheRD::_clear() {73// TODO: Clear should probably recompile all the variants already compiled instead to avoid stalls? Needs discussion.74if (versions) {75for (uint32_t i = 0; i < version_count; i++) {76//shader may be gone, so this may not be valid77if (RD::get_singleton()->render_pipeline_is_valid(versions[i].pipeline)) {78RD::get_singleton()->free(versions[i].pipeline);79}80}81version_count = 0;82memfree(versions);83versions = nullptr;84}85}8687void 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) {88ERR_FAIL_COND(p_shader.is_null());89_clear();90shader = p_shader;91render_primitive = p_primitive;92rasterization_state = p_rasterization_state;93multisample_state = p_multisample;94depth_stencil_state = p_depth_stencil_state;95blend_state = p_blend_state;96dynamic_state_flags = p_dynamic_state_flags;97base_specialization_constants = p_base_specialization_constants;98}99void PipelineCacheRD::update_specialization_constants(const Vector<RD::PipelineSpecializationConstant> &p_base_specialization_constants) {100base_specialization_constants = p_base_specialization_constants;101_clear();102}103104void PipelineCacheRD::update_shader(RID p_shader) {105ERR_FAIL_COND(p_shader.is_null());106_clear();107setup(p_shader, render_primitive, rasterization_state, multisample_state, depth_stencil_state, blend_state, dynamic_state_flags);108}109110void PipelineCacheRD::clear() {111_clear();112shader = RID(); //clear shader113}114115PipelineCacheRD::PipelineCacheRD() {116version_count = 0;117versions = nullptr;118}119120PipelineCacheRD::~PipelineCacheRD() {121_clear();122}123124125