2022-02-11 11:33:54 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* vrs.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/**************************************************************************/
|
|
|
|
|
|
|
|
#include "vrs.h"
|
|
|
|
#include "../renderer_compositor_rd.h"
|
|
|
|
#include "../storage_rd/texture_storage.h"
|
|
|
|
#include "../uniform_set_cache_rd.h"
|
2024-03-12 02:42:50 +00:00
|
|
|
|
|
|
|
#ifndef _3D_DISABLED
|
2022-02-11 11:33:54 +00:00
|
|
|
#include "servers/xr_server.h"
|
2024-03-12 02:42:50 +00:00
|
|
|
#endif // _3D_DISABLED
|
2022-02-11 11:33:54 +00:00
|
|
|
|
|
|
|
using namespace RendererRD;
|
|
|
|
|
|
|
|
VRS::VRS() {
|
|
|
|
{
|
|
|
|
Vector<String> vrs_modes;
|
|
|
|
vrs_modes.push_back("\n"); // VRS_DEFAULT
|
2024-03-11 03:40:00 +00:00
|
|
|
vrs_modes.push_back("\n#define USE_MULTIVIEW\n"); // VRS_MULTIVIEW
|
2022-02-11 11:33:54 +00:00
|
|
|
|
|
|
|
vrs_shader.shader.initialize(vrs_modes);
|
|
|
|
|
2023-01-29 09:14:22 +00:00
|
|
|
if (!RendererCompositorRD::get_singleton()->is_xr_enabled()) {
|
2022-02-11 11:33:54 +00:00
|
|
|
vrs_shader.shader.set_variant_enabled(VRS_MULTIVIEW, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
vrs_shader.shader_version = vrs_shader.shader.version_create();
|
|
|
|
|
|
|
|
//use additive
|
|
|
|
|
|
|
|
for (int i = 0; i < VRS_MAX; i++) {
|
|
|
|
if (vrs_shader.shader.is_variant_enabled(i)) {
|
|
|
|
vrs_shader.pipelines[i].setup(vrs_shader.shader.version_get_shader(vrs_shader.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);
|
|
|
|
} else {
|
|
|
|
vrs_shader.pipelines[i].clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VRS::~VRS() {
|
|
|
|
vrs_shader.shader.version_free(vrs_shader.shader_version);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VRS::copy_vrs(RID p_source_rd_texture, RID p_dest_framebuffer, bool p_multiview) {
|
|
|
|
UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
|
|
|
|
ERR_FAIL_NULL(uniform_set_cache);
|
|
|
|
MaterialStorage *material_storage = MaterialStorage::get_singleton();
|
|
|
|
ERR_FAIL_NULL(material_storage);
|
|
|
|
|
|
|
|
// setup our uniforms
|
|
|
|
RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
|
|
|
|
|
|
|
|
RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
|
|
|
|
|
|
|
|
VRSMode mode = p_multiview ? VRS_MULTIVIEW : VRS_DEFAULT;
|
|
|
|
|
|
|
|
RID shader = vrs_shader.shader.version_get_shader(vrs_shader.shader_version, mode);
|
|
|
|
ERR_FAIL_COND(shader.is_null());
|
|
|
|
|
2023-11-24 11:23:22 +00:00
|
|
|
RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer, RD::INITIAL_ACTION_LOAD, RD::FINAL_ACTION_STORE, RD::INITIAL_ACTION_LOAD, RD::FINAL_ACTION_DISCARD, Vector<Color>());
|
2022-02-11 11:33:54 +00:00
|
|
|
RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, vrs_shader.pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
|
|
|
|
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
|
|
|
|
// RD::get_singleton()->draw_list_set_push_constant(draw_list, &vrs_shader.push_constant, sizeof(VRSPushConstant));
|
2023-08-05 19:30:47 +00:00
|
|
|
RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
|
2022-02-11 11:33:54 +00:00
|
|
|
RD::get_singleton()->draw_list_end();
|
|
|
|
}
|
|
|
|
|
2022-08-04 08:40:39 +00:00
|
|
|
Size2i VRS::get_vrs_texture_size(const Size2i p_base_size) const {
|
2022-11-16 02:32:42 +00:00
|
|
|
int32_t texel_width = RD::get_singleton()->limit_get(RD::LIMIT_VRS_TEXEL_WIDTH);
|
|
|
|
int32_t texel_height = RD::get_singleton()->limit_get(RD::LIMIT_VRS_TEXEL_HEIGHT);
|
|
|
|
|
|
|
|
int width = p_base_size.x / texel_width;
|
|
|
|
if (p_base_size.x % texel_width != 0) {
|
2022-08-04 08:40:39 +00:00
|
|
|
width++;
|
2022-02-11 11:33:54 +00:00
|
|
|
}
|
2022-11-16 02:32:42 +00:00
|
|
|
int height = p_base_size.y / texel_height;
|
|
|
|
if (p_base_size.y % texel_height != 0) {
|
2022-08-04 08:40:39 +00:00
|
|
|
height++;
|
2022-02-11 11:33:54 +00:00
|
|
|
}
|
2022-08-04 08:40:39 +00:00
|
|
|
return Size2i(width, height);
|
2022-02-11 11:33:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VRS::update_vrs_texture(RID p_vrs_fb, RID p_render_target) {
|
|
|
|
TextureStorage *texture_storage = TextureStorage::get_singleton();
|
|
|
|
RS::ViewportVRSMode vrs_mode = texture_storage->render_target_get_vrs_mode(p_render_target);
|
|
|
|
|
|
|
|
if (vrs_mode != RS::VIEWPORT_VRS_DISABLED) {
|
|
|
|
RD::get_singleton()->draw_command_begin_label("VRS Setup");
|
|
|
|
|
|
|
|
// TODO figure out if image has changed since it was last copied so we can save some resources..
|
|
|
|
|
|
|
|
if (vrs_mode == RS::VIEWPORT_VRS_TEXTURE) {
|
|
|
|
RID vrs_texture = texture_storage->render_target_get_vrs_texture(p_render_target);
|
|
|
|
if (vrs_texture.is_valid()) {
|
2022-08-01 01:59:14 +00:00
|
|
|
RID rd_texture = texture_storage->texture_get_rd_texture(vrs_texture);
|
|
|
|
int layers = texture_storage->texture_get_layers(vrs_texture);
|
|
|
|
if (rd_texture.is_valid()) {
|
2022-02-11 11:33:54 +00:00
|
|
|
// Copy into our density buffer
|
2022-08-01 01:59:14 +00:00
|
|
|
copy_vrs(rd_texture, p_vrs_fb, layers > 1);
|
2022-02-11 11:33:54 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-12 02:42:50 +00:00
|
|
|
#ifndef _3D_DISABLED
|
2022-02-11 11:33:54 +00:00
|
|
|
} else if (vrs_mode == RS::VIEWPORT_VRS_XR) {
|
|
|
|
Ref<XRInterface> interface = XRServer::get_singleton()->get_primary_interface();
|
|
|
|
if (interface.is_valid()) {
|
|
|
|
RID vrs_texture = interface->get_vrs_texture();
|
|
|
|
if (vrs_texture.is_valid()) {
|
2022-08-01 01:59:14 +00:00
|
|
|
RID rd_texture = texture_storage->texture_get_rd_texture(vrs_texture);
|
|
|
|
int layers = texture_storage->texture_get_layers(vrs_texture);
|
|
|
|
|
|
|
|
if (rd_texture.is_valid()) {
|
2022-02-11 11:33:54 +00:00
|
|
|
// Copy into our density buffer
|
2022-08-01 01:59:14 +00:00
|
|
|
copy_vrs(rd_texture, p_vrs_fb, layers > 1);
|
2022-02-11 11:33:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-03-12 02:42:50 +00:00
|
|
|
#endif // _3D_DISABLED
|
2022-02-11 11:33:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RD::get_singleton()->draw_command_end_label();
|
|
|
|
}
|
|
|
|
}
|