2019-06-22 18:34:26 +02:00
|
|
|
/**************************************************************************/
|
2020-12-04 19:26:24 +01:00
|
|
|
/* pipeline_cache_rd.cpp */
|
2019-06-22 18:34:26 +02:00
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
2020-02-11 14:01:43 +01:00
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
2019-06-22 18:34:26 +02:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
|
|
|
|
2020-12-04 19:26:24 +01:00
|
|
|
#include "pipeline_cache_rd.h"
|
2022-10-10 13:04:01 +02:00
|
|
|
|
2019-06-19 22:03:19 +02:00
|
|
|
#include "core/os/memory.h"
|
|
|
|
|
2021-07-13 01:32:05 +02:00
|
|
|
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) {
|
2019-07-10 22:44:55 +02:00
|
|
|
RD::PipelineMultisampleState multisample_state_version = multisample_state;
|
2021-06-24 15:58:36 +02:00
|
|
|
multisample_state_version.sample_count = RD::get_singleton()->framebuffer_format_get_texture_samples(p_framebuffer_format_id, p_render_pass);
|
2019-07-10 22:44:55 +02:00
|
|
|
|
2023-06-14 01:42:07 +02:00
|
|
|
bool wireframe = p_wireframe;
|
2021-10-14 21:48:55 +02:00
|
|
|
|
2020-05-01 14:34:23 +02:00
|
|
|
RD::PipelineRasterizationState raster_state_version = rasterization_state;
|
2021-10-14 21:48:55 +02:00
|
|
|
raster_state_version.wireframe = wireframe;
|
2020-05-01 14:34:23 +02:00
|
|
|
|
2021-07-13 01:32:05 +02:00
|
|
|
Vector<RD::PipelineSpecializationConstant> specialization_constants = base_specialization_constants;
|
|
|
|
|
|
|
|
uint32_t bool_index = 0;
|
|
|
|
uint32_t bool_specializations = p_bool_specializations;
|
|
|
|
while (bool_specializations) {
|
|
|
|
if (bool_specializations & (1 << bool_index)) {
|
|
|
|
RD::PipelineSpecializationConstant sc;
|
|
|
|
sc.bool_value = true;
|
|
|
|
sc.constant_id = bool_index;
|
|
|
|
sc.type = RD::PIPELINE_SPECIALIZATION_CONSTANT_TYPE_BOOL;
|
|
|
|
specialization_constants.push_back(sc);
|
|
|
|
bool_specializations &= ~(1 << bool_index);
|
|
|
|
}
|
|
|
|
bool_index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2019-06-19 22:03:19 +02:00
|
|
|
ERR_FAIL_COND_V(pipeline.is_null(), RID());
|
2022-04-05 12:40:26 +02:00
|
|
|
versions = static_cast<Version *>(memrealloc(versions, sizeof(Version) * (version_count + 1)));
|
2019-07-10 22:44:55 +02:00
|
|
|
versions[version_count].framebuffer_id = p_framebuffer_format_id;
|
2019-07-27 15:23:24 +02:00
|
|
|
versions[version_count].vertex_id = p_vertex_format_id;
|
2021-10-14 21:48:55 +02:00
|
|
|
versions[version_count].wireframe = wireframe;
|
2019-07-10 22:44:55 +02:00
|
|
|
versions[version_count].pipeline = pipeline;
|
2021-06-24 15:58:36 +02:00
|
|
|
versions[version_count].render_pass = p_render_pass;
|
2021-07-13 01:32:05 +02:00
|
|
|
versions[version_count].bool_specializations = p_bool_specializations;
|
2019-07-10 22:44:55 +02:00
|
|
|
version_count++;
|
2019-06-19 22:03:19 +02:00
|
|
|
return pipeline;
|
|
|
|
}
|
|
|
|
|
2020-12-04 19:26:24 +01:00
|
|
|
void PipelineCacheRD::_clear() {
|
2022-10-10 13:04:01 +02:00
|
|
|
// TODO: Clear should probably recompile all the variants already compiled instead to avoid stalls? Needs discussion.
|
2019-07-10 22:44:55 +02:00
|
|
|
if (versions) {
|
|
|
|
for (uint32_t i = 0; i < version_count; i++) {
|
|
|
|
//shader may be gone, so this may not be valid
|
|
|
|
if (RD::get_singleton()->render_pipeline_is_valid(versions[i].pipeline)) {
|
|
|
|
RD::get_singleton()->free(versions[i].pipeline);
|
2019-06-19 22:03:19 +02:00
|
|
|
}
|
|
|
|
}
|
2019-07-10 22:44:55 +02:00
|
|
|
version_count = 0;
|
|
|
|
memfree(versions);
|
2020-04-02 01:20:12 +02:00
|
|
|
versions = nullptr;
|
2019-06-19 22:03:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-13 01:32:05 +02:00
|
|
|
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) {
|
2019-06-19 22:03:19 +02:00
|
|
|
ERR_FAIL_COND(p_shader.is_null());
|
2019-07-27 15:23:24 +02:00
|
|
|
_clear();
|
2019-06-19 22:03:19 +02:00
|
|
|
shader = p_shader;
|
2023-07-18 11:21:27 +02:00
|
|
|
input_mask = 0;
|
2019-06-19 22:03:19 +02:00
|
|
|
render_primitive = p_primitive;
|
|
|
|
rasterization_state = p_rasterization_state;
|
|
|
|
multisample_state = p_multisample;
|
|
|
|
depth_stencil_state = p_depth_stencil_state;
|
|
|
|
blend_state = p_blend_state;
|
|
|
|
dynamic_state_flags = p_dynamic_state_flags;
|
2021-07-13 01:32:05 +02:00
|
|
|
base_specialization_constants = p_base_specialization_constants;
|
2019-06-19 22:03:19 +02:00
|
|
|
}
|
2021-07-19 21:41:55 +02:00
|
|
|
void PipelineCacheRD::update_specialization_constants(const Vector<RD::PipelineSpecializationConstant> &p_base_specialization_constants) {
|
|
|
|
base_specialization_constants = p_base_specialization_constants;
|
|
|
|
_clear();
|
|
|
|
}
|
2019-06-19 22:03:19 +02:00
|
|
|
|
2020-12-04 19:26:24 +01:00
|
|
|
void PipelineCacheRD::update_shader(RID p_shader) {
|
2019-06-19 22:03:19 +02:00
|
|
|
ERR_FAIL_COND(p_shader.is_null());
|
|
|
|
_clear();
|
2019-07-10 22:44:55 +02:00
|
|
|
setup(p_shader, render_primitive, rasterization_state, multisample_state, depth_stencil_state, blend_state, dynamic_state_flags);
|
2019-06-19 22:03:19 +02:00
|
|
|
}
|
|
|
|
|
2020-12-04 19:26:24 +01:00
|
|
|
void PipelineCacheRD::clear() {
|
2019-08-19 00:40:52 +02:00
|
|
|
_clear();
|
|
|
|
shader = RID(); //clear shader
|
2019-08-20 22:54:03 +02:00
|
|
|
input_mask = 0;
|
2019-08-19 00:40:52 +02:00
|
|
|
}
|
|
|
|
|
2020-12-04 19:26:24 +01:00
|
|
|
PipelineCacheRD::PipelineCacheRD() {
|
2019-07-10 22:44:55 +02:00
|
|
|
version_count = 0;
|
2020-04-02 01:20:12 +02:00
|
|
|
versions = nullptr;
|
2019-08-20 22:54:03 +02:00
|
|
|
input_mask = 0;
|
2019-06-19 22:03:19 +02:00
|
|
|
}
|
|
|
|
|
2020-12-04 19:26:24 +01:00
|
|
|
PipelineCacheRD::~PipelineCacheRD() {
|
2019-06-19 22:03:19 +02:00
|
|
|
_clear();
|
|
|
|
}
|