Path: blob/master/servers/rendering/renderer_rd/effects/vrs.cpp
10279 views
/**************************************************************************/1/* vrs.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 "vrs.h"31#include "../renderer_compositor_rd.h"32#include "../storage_rd/texture_storage.h"33#include "../uniform_set_cache_rd.h"3435#ifndef XR_DISABLED36#include "servers/xr_server.h"37#endif // XR_DISABLED3839using namespace RendererRD;4041VRS::VRS() {42{43Vector<String> vrs_modes;44vrs_modes.push_back("\n"); // VRS_DEFAULT45vrs_modes.push_back("\n#define USE_MULTIVIEW\n"); // VRS_MULTIVIEW46vrs_modes.push_back("\n#define SPLIT_RG\n"); // VRS_RG47vrs_modes.push_back("\n#define SPLIT_RG\n#define USE_MULTIVIEW\n"); // VRS_RG_MULTIVIEW4849vrs_shader.shader.initialize(vrs_modes);5051if (!RendererCompositorRD::get_singleton()->is_xr_enabled()) {52vrs_shader.shader.set_variant_enabled(VRS_MULTIVIEW, false);53}5455vrs_shader.shader_version = vrs_shader.shader.version_create();5657//use additive5859for (int i = 0; i < VRS_MAX; i++) {60if (vrs_shader.shader.is_variant_enabled(i)) {61vrs_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);62} else {63vrs_shader.pipelines[i].clear();64}65}66}67}6869VRS::~VRS() {70vrs_shader.shader.version_free(vrs_shader.shader_version);71}7273void VRS::copy_vrs(RID p_source_rd_texture, RID p_dest_framebuffer, bool p_multiview) {74UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();75ERR_FAIL_NULL(uniform_set_cache);76MaterialStorage *material_storage = MaterialStorage::get_singleton();77ERR_FAIL_NULL(material_storage);7879// setup our uniforms80RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);8182RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));8384int mode = 0;85VRSPushConstant push_constant = {};86bool uses_rg_format = RD::get_singleton()->vrs_get_format() == RD::DATA_FORMAT_R8G8_UNORM;87if (uses_rg_format) {88mode = p_multiview ? VRS_RG_MULTIVIEW : VRS_RG;89} else {90mode = p_multiview ? VRS_MULTIVIEW : VRS_DEFAULT;9192// Default to 4x4 as it's not possible to query the max fragment size from RenderingDevice. This can be improved to use the largest size93// available if this code is moved over to RenderingDevice at some point.94push_constant.max_texel_factor = 2.0;95}9697RID shader = vrs_shader.shader.version_get_shader(vrs_shader.shader_version, mode);98ERR_FAIL_COND(shader.is_null());99100RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer);101RD::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)));102RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);103RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(VRSPushConstant));104RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);105RD::get_singleton()->draw_list_end();106}107108Size2i VRS::get_vrs_texture_size(const Size2i p_base_size) const {109Size2i vrs_texel_size = RD::get_singleton()->vrs_get_texel_size();110return Size2i((p_base_size.x + vrs_texel_size.x - 1) / vrs_texel_size.x, (p_base_size.y + vrs_texel_size.y - 1) / vrs_texel_size.y);111}112113void VRS::update_vrs_texture(RID p_vrs_fb, RID p_render_target) {114TextureStorage *texture_storage = TextureStorage::get_singleton();115RS::ViewportVRSMode vrs_mode = texture_storage->render_target_get_vrs_mode(p_render_target);116RS::ViewportVRSUpdateMode vrs_update_mode = texture_storage->render_target_get_vrs_update_mode(p_render_target);117118if (vrs_mode != RS::VIEWPORT_VRS_DISABLED && vrs_update_mode != RS::VIEWPORT_VRS_UPDATE_DISABLED) {119RD::get_singleton()->draw_command_begin_label("VRS Setup");120121if (vrs_mode == RS::VIEWPORT_VRS_TEXTURE) {122RID vrs_texture = texture_storage->render_target_get_vrs_texture(p_render_target);123if (vrs_texture.is_valid()) {124RID rd_texture = texture_storage->texture_get_rd_texture(vrs_texture);125int layers = texture_storage->texture_get_layers(vrs_texture);126if (rd_texture.is_valid()) {127// Copy into our density buffer128copy_vrs(rd_texture, p_vrs_fb, layers > 1);129}130}131#ifndef XR_DISABLED132} else if (vrs_mode == RS::VIEWPORT_VRS_XR) {133Ref<XRInterface> interface = XRServer::get_singleton()->get_primary_interface();134if (interface.is_valid() && interface->get_vrs_texture_format() == XRInterface::XR_VRS_TEXTURE_FORMAT_UNIFIED) {135RID vrs_texture = interface->get_vrs_texture();136if (vrs_texture.is_valid()) {137RID rd_texture = texture_storage->texture_get_rd_texture(vrs_texture);138int layers = texture_storage->texture_get_layers(vrs_texture);139140if (rd_texture.is_valid()) {141// Copy into our density buffer142copy_vrs(rd_texture, p_vrs_fb, layers > 1);143}144}145}146#endif // XR_DISABLED147}148149if (vrs_update_mode == RS::VIEWPORT_VRS_UPDATE_ONCE) {150texture_storage->render_target_set_vrs_update_mode(p_render_target, RS::VIEWPORT_VRS_UPDATE_DISABLED);151}152153RD::get_singleton()->draw_command_end_label();154}155}156157158