Debian Codegraph @

servers/rendering/renderer_rd/effects/vrs.cpp

raw symbols SCIP
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
36 #ifndef XR_DISABLED
37 #include "servers/xr/xr_server.h"
38 #endif // XR_DISABLED
39
40 using namespace RendererRD;
41
42 VRS::VRS() {
43 {
44 Vector<String> vrs_modes;
45 vrs_modes.push_back("\n"); // VRS_DEFAULT
46 vrs_modes.push_back("\n#define USE_MULTIVIEW\n"); // VRS_MULTIVIEW
47 vrs_modes.push_back("\n#define SPLIT_RG\n"); // VRS_RG
48 vrs_modes.push_back("\n#define SPLIT_RG\n#define USE_MULTIVIEW\n"); // VRS_RG_MULTIVIEW
49
50 vrs_shader.shader.initialize(vrs_modes);
51
52 if (!RendererCompositorRD::get_singleton()->is_xr_enabled()) {
53 vrs_shader.shader.set_variant_enabled(VRS_MULTIVIEW, false);
54 }
55
56 vrs_shader.shader_version = vrs_shader.shader.version_create();
57
58 //use additive
59
60 for (int i = 0; i < VRS_MAX; i++) {
61 if (vrs_shader.shader.is_variant_enabled(i)) {
62 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);
63 } else {
64 vrs_shader.pipelines[i].clear();
65 }
66 }
67 }
68 }
69
70 VRS::~VRS() {
71 vrs_shader.shader.version_free(vrs_shader.shader_version);
72 }
73
74 void VRS::copy_vrs(RID p_source_rd_texture, RID p_dest_framebuffer, bool p_multiview) {
75 UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
76 ERR_FAIL_NULL(uniform_set_cache);
77 MaterialStorage *material_storage = MaterialStorage::get_singleton();
78 ERR_FAIL_NULL(material_storage);
79
80 // setup our uniforms
81 RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
82
83 RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
84
85 int mode = 0;
86 VRSPushConstant push_constant = {};
87 bool uses_rg_format = RD::get_singleton()->vrs_get_format() == RD::DATA_FORMAT_R8G8_UNORM;
88 if (uses_rg_format) {
89 mode = p_multiview ? VRS_RG_MULTIVIEW : VRS_RG;
90 } else {
91 mode = p_multiview ? VRS_MULTIVIEW : VRS_DEFAULT;
92
93 // Default to 4x4 as it's not possible to query the max fragment size from RenderingDevice. This can be improved to use the largest size
94 // available if this code is moved over to RenderingDevice at some point.
95 push_constant.max_texel_factor = 2.0;
96 }
97
98 RID shader = vrs_shader.shader.version_get_shader(vrs_shader.shader_version, mode);
99 ERR_FAIL_COND(shader.is_null());
100
101 RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer);
102 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)));
103 RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
104 RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(VRSPushConstant));
105 RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
106 RD::get_singleton()->draw_list_end();
107 }
108
109 Size2i VRS::get_vrs_texture_size(const Size2i p_base_size) const {
110 Size2i vrs_texel_size = RD::get_singleton()->vrs_get_texel_size();
111 return 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);
112 }
113
114 void VRS::update_vrs_texture(RID p_vrs_fb, RID p_render_target) {
115 TextureStorage *texture_storage = TextureStorage::get_singleton();
116 RS::ViewportVRSMode vrs_mode = texture_storage->render_target_get_vrs_mode(p_render_target);
117 RS::ViewportVRSUpdateMode vrs_update_mode = texture_storage->render_target_get_vrs_update_mode(p_render_target);
118
119 if (vrs_mode != RS::VIEWPORT_VRS_DISABLED && vrs_update_mode != RS::VIEWPORT_VRS_UPDATE_DISABLED) {
120 RD::get_singleton()->draw_command_begin_label("VRS Setup");
121
122 if (vrs_mode == RS::VIEWPORT_VRS_TEXTURE) {
123 RID vrs_texture = texture_storage->render_target_get_vrs_texture(p_render_target);
124 if (vrs_texture.is_valid()) {
125 RID rd_texture = texture_storage->texture_get_rd_texture(vrs_texture);
126 int layers = texture_storage->texture_get_layers(vrs_texture);
127 if (rd_texture.is_valid()) {
128 // Copy into our density buffer
129 copy_vrs(rd_texture, p_vrs_fb, layers > 1);
130 }
131 }
132 #ifndef XR_DISABLED
133 } else if (vrs_mode == RS::VIEWPORT_VRS_XR) {
134 Ref<XRInterface> interface = XRServer::get_singleton()->get_primary_interface();
135 if (interface.is_valid() && interface->get_vrs_texture_format() == XRInterface::XR_VRS_TEXTURE_FORMAT_UNIFIED) {
136 RID vrs_texture = interface->get_vrs_texture();
137 if (vrs_texture.is_valid()) {
138 RID rd_texture = texture_storage->texture_get_rd_texture(vrs_texture);
139 int layers = texture_storage->texture_get_layers(vrs_texture);
140
141 if (rd_texture.is_valid()) {
142 // Copy into our density buffer
143 copy_vrs(rd_texture, p_vrs_fb, layers > 1);
144 }
145 }
146 }
147 #endif // XR_DISABLED
148 }
149
150 if (vrs_update_mode == RS::VIEWPORT_VRS_UPDATE_ONCE) {
151 texture_storage->render_target_set_vrs_update_mode(p_render_target, RS::VIEWPORT_VRS_UPDATE_DISABLED);
152 }
153
154 RD::get_singleton()->draw_command_end_label();
155 }
156 }