1/**************************************************************************/
2/* vrs.cpp */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#include "vrs.h"
32#include "../renderer_compositor_rd.h"
33#include "../storage_rd/texture_storage.h"
34#include "../uniform_set_cache_rd.h"
35#include "servers/xr_server.h"
36
37using namespace RendererRD;
38
39VRS::VRS() {
40 {
41 Vector<String> vrs_modes;
42 vrs_modes.push_back("\n"); // VRS_DEFAULT
43 vrs_modes.push_back("\n#define MULTIVIEW\n"); // VRS_MULTIVIEW
44
45 vrs_shader.shader.initialize(vrs_modes);
46
47 if (!RendererCompositorRD::get_singleton()->is_xr_enabled()) {
48 vrs_shader.shader.set_variant_enabled(VRS_MULTIVIEW, false);
49 }
50
51 vrs_shader.shader_version = vrs_shader.shader.version_create();
52
53 //use additive
54
55 for (int i = 0; i < VRS_MAX; i++) {
56 if (vrs_shader.shader.is_variant_enabled(i)) {
57 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);
58 } else {
59 vrs_shader.pipelines[i].clear();
60 }
61 }
62 }
63}
64
65VRS::~VRS() {
66 vrs_shader.shader.version_free(vrs_shader.shader_version);
67}
68
69void VRS::copy_vrs(RID p_source_rd_texture, RID p_dest_framebuffer, bool p_multiview) {
70 UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
71 ERR_FAIL_NULL(uniform_set_cache);
72 MaterialStorage *material_storage = MaterialStorage::get_singleton();
73 ERR_FAIL_NULL(material_storage);
74
75 // setup our uniforms
76 RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
77
78 RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
79
80 VRSMode mode = p_multiview ? VRS_MULTIVIEW : VRS_DEFAULT;
81
82 RID shader = vrs_shader.shader.version_get_shader(vrs_shader.shader_version, mode);
83 ERR_FAIL_COND(shader.is_null());
84
85 RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD, Vector<Color>());
86 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)));
87 RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
88 // RD::get_singleton()->draw_list_set_push_constant(draw_list, &vrs_shader.push_constant, sizeof(VRSPushConstant));
89 RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
90 RD::get_singleton()->draw_list_end();
91}
92
93Size2i VRS::get_vrs_texture_size(const Size2i p_base_size) const {
94 int32_t texel_width = RD::get_singleton()->limit_get(RD::LIMIT_VRS_TEXEL_WIDTH);
95 int32_t texel_height = RD::get_singleton()->limit_get(RD::LIMIT_VRS_TEXEL_HEIGHT);
96
97 int width = p_base_size.x / texel_width;
98 if (p_base_size.x % texel_width != 0) {
99 width++;
100 }
101 int height = p_base_size.y / texel_height;
102 if (p_base_size.y % texel_height != 0) {
103 height++;
104 }
105 return Size2i(width, height);
106}
107
108void VRS::update_vrs_texture(RID p_vrs_fb, RID p_render_target) {
109 TextureStorage *texture_storage = TextureStorage::get_singleton();
110 RS::ViewportVRSMode vrs_mode = texture_storage->render_target_get_vrs_mode(p_render_target);
111
112 if (vrs_mode != RS::VIEWPORT_VRS_DISABLED) {
113 RD::get_singleton()->draw_command_begin_label("VRS Setup");
114
115 // TODO figure out if image has changed since it was last copied so we can save some resources..
116
117 if (vrs_mode == RS::VIEWPORT_VRS_TEXTURE) {
118 RID vrs_texture = texture_storage->render_target_get_vrs_texture(p_render_target);
119 if (vrs_texture.is_valid()) {
120 RID rd_texture = texture_storage->texture_get_rd_texture(vrs_texture);
121 int layers = texture_storage->texture_get_layers(vrs_texture);
122 if (rd_texture.is_valid()) {
123 // Copy into our density buffer
124 copy_vrs(rd_texture, p_vrs_fb, layers > 1);
125 }
126 }
127 } else if (vrs_mode == RS::VIEWPORT_VRS_XR) {
128 Ref<XRInterface> interface = XRServer::get_singleton()->get_primary_interface();
129 if (interface.is_valid()) {
130 RID vrs_texture = interface->get_vrs_texture();
131 if (vrs_texture.is_valid()) {
132 RID rd_texture = texture_storage->texture_get_rd_texture(vrs_texture);
133 int layers = texture_storage->texture_get_layers(vrs_texture);
134
135 if (rd_texture.is_valid()) {
136 // Copy into our density buffer
137 copy_vrs(rd_texture, p_vrs_fb, layers > 1);
138 }
139 }
140 }
141 }
142
143 RD::get_singleton()->draw_command_end_label();
144 }
145}
146