Debian Codegraph @

servers/rendering/renderer_rd/storage_rd/render_scene_buffers_rd.h

raw symbols SCIP
1 /**************************************************************************/
2 /* render_scene_buffers_rd.h */
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 #pragma once
32
33 #ifdef METAL_ENABLED
34 #include "../effects/metal_fx.h"
35 #endif
36 #include "../effects/vrs.h"
37 #include "core/templates/hash_map.h"
38 #include "material_storage.h"
39 #include "render_buffer_custom_data_rd.h"
40 #include "servers/rendering/rendering_device.h"
41 #include "servers/rendering/rendering_device_binds.h"
42 #include "servers/rendering/storage/render_scene_buffers.h"
43
44 #define RB_SCOPE_BUFFERS SNAME("render_buffers")
45 #define RB_SCOPE_VRS SNAME("VRS")
46
47 #define RB_TEXTURE SNAME("texture")
48 #define RB_TEX_COLOR SNAME("color")
49 #define RB_TEX_COLOR_MSAA SNAME("color_msaa")
50 #define RB_TEX_COLOR_UPSCALED SNAME("color_upscaled")
51 #define RB_TEX_DEPTH SNAME("depth")
52 #define RB_TEX_DEPTH_MSAA SNAME("depth_msaa")
53 #define RB_TEX_VELOCITY SNAME("velocity")
54 #define RB_TEX_VELOCITY_MSAA SNAME("velocity_msaa")
55
56 #define RB_TEX_BLUR_0 SNAME("blur_0")
57 #define RB_TEX_BLUR_1 SNAME("blur_1")
58
59 #define RB_TEX_BACK_COLOR SNAME("back_color")
60 #define RB_TEX_BACK_DEPTH SNAME("back_depth")
61
62 class RenderSceneBuffersRD : public RenderSceneBuffers {
63 GDCLASS(RenderSceneBuffersRD, RenderSceneBuffers);
64
65 private:
66 bool can_be_storage = true;
67 bool force_hdr = false;
68 uint32_t max_cluster_elements = 512;
69 RD::DataFormat preferred_data_format = RD::DATA_FORMAT_MAX;
70 RendererRD::VRS *vrs = nullptr;
71 uint64_t auto_exposure_version = 1;
72 RS::ViewportVRSMode vrs_mode = RS::VIEWPORT_VRS_DISABLED;
73
74 // Our render target represents our final destination that we display on screen.
75 RID render_target;
76 Size2i target_size = Size2i(0, 0);
77 uint32_t view_count = 1;
78
79 // The internal size of the textures we render 3D to in case we render at a lower resolution and upscale
80 Size2i internal_size = Size2i(0, 0);
81 RS::ViewportScaling3DMode scaling_3d_mode = RS::VIEWPORT_SCALING_3D_MODE_OFF;
82 float fsr_sharpness = 0.2f;
83 float texture_mipmap_bias = 0.0f;
84 RS::ViewportAnisotropicFiltering anisotropic_filtering_level = RS::VIEWPORT_ANISOTROPY_4X;
85
86 #ifdef METAL_ENABLED
87 RendererRD::MFXSpatialContext *mfx_spatial_context = nullptr;
88 #endif
89
90 // Aliasing settings
91 RS::ViewportMSAA msaa_3d = RS::VIEWPORT_MSAA_DISABLED;
92 RS::ViewportScreenSpaceAA screen_space_aa = RS::VIEWPORT_SCREEN_SPACE_AA_DISABLED;
93 bool use_taa = false;
94 bool use_debanding = false;
95 RD::TextureSamples texture_samples = RD::TEXTURE_SAMPLES_1;
96
97 // Named Textures
98
99 struct NTKey {
100 StringName context;
101 StringName buffer_name;
102
103 bool operator==(const NTKey &p_val) const {
104 return (context == p_val.context) && (buffer_name == p_val.buffer_name);
105 }
106
107 static uint32_t hash(const NTKey &p_val) {
108 uint32_t h = p_val.context.hash();
109 h = hash_murmur3_one_32(p_val.buffer_name.hash(), h);
110 return hash_fmix32(h);
111 }
112
113 NTKey() {}
114 NTKey(const StringName &p_context, const StringName &p_texture_name) {
115 context = p_context;
116 buffer_name = p_texture_name;
117 }
118 };
119
120 struct NTSliceKey {
121 uint32_t layer;
122 uint32_t layers;
123 uint32_t mipmap;
124 uint32_t mipmaps;
125 RD::TextureView texture_view;
126
127 bool operator==(const NTSliceKey &p_val) const {
128 return (layer == p_val.layer) && (layers == p_val.layers) && (mipmap == p_val.mipmap) && (mipmaps == p_val.mipmaps) && (texture_view == p_val.texture_view);
129 }
130
131 static uint32_t hash(const NTSliceKey &p_val) {
132 uint32_t h = hash_murmur3_one_32(p_val.layer);
133 h = hash_murmur3_one_32(p_val.layers, h);
134 h = hash_murmur3_one_32(p_val.mipmap, h);
135 h = hash_murmur3_one_32(p_val.mipmaps, h);
136 h = hash_murmur3_one_32(p_val.texture_view.format_override, h);
137 h = hash_murmur3_one_32(p_val.texture_view.swizzle_r, h);
138 h = hash_murmur3_one_32(p_val.texture_view.swizzle_g, h);
139 h = hash_murmur3_one_32(p_val.texture_view.swizzle_b, h);
140 h = hash_murmur3_one_32(p_val.texture_view.swizzle_a, h);
141 return hash_fmix32(h);
142 }
143
144 NTSliceKey() {}
145 NTSliceKey(uint32_t p_layer, uint32_t p_layers, uint32_t p_mipmap, uint32_t p_mipmaps, RD::TextureView p_texture_view) {
146 layer = p_layer;
147 layers = p_layers;
148 mipmap = p_mipmap;
149 mipmaps = p_mipmaps;
150 texture_view = p_texture_view;
151 }
152 };
153
154 struct NamedTexture {
155 // Cache the data used to create our texture
156 RD::TextureFormat format;
157 bool is_unique; // If marked as unique, we return it into our pool
158
159 // Our texture objects, slices are lazy (i.e. only created when requested).
160 RID texture;
161 mutable HashMap<NTSliceKey, RID, NTSliceKey> slices;
162 Vector<Size2i> sizes;
163 };
164
165 mutable HashMap<NTKey, NamedTexture, NTKey> named_textures;
166 void update_sizes(NamedTexture &p_named_texture);
167 void free_named_texture(NamedTexture &p_named_texture);
168
169 // Data buffers
170 mutable HashMap<StringName, Ref<RenderBufferCustomDataRD>> data_buffers;
171
172 // Samplers.
173 RendererRD::MaterialStorage::Samplers samplers;
174
175 void update_samplers();
176
177 protected:
178 static void _bind_methods();
179
180 public:
181 RenderSceneBuffersRD();
182 virtual ~RenderSceneBuffersRD();
183
184 // info from our renderer
185 void set_can_be_storage(const bool p_can_be_storage) { can_be_storage = p_can_be_storage; }
186 bool get_can_be_storage() const { return can_be_storage; }
187 void set_max_cluster_elements(const uint32_t p_max_elements) { max_cluster_elements = p_max_elements; }
188 uint32_t get_max_cluster_elements() { return max_cluster_elements; }
189 void set_preferred_data_format(const RD::DataFormat p_preferred_data_format) { preferred_data_format = p_preferred_data_format; }
190 RD::DataFormat get_preferred_data_format() const { return preferred_data_format; }
191 RD::DataFormat get_base_data_format() const { return force_hdr ? RD::DATA_FORMAT_R16G16B16A16_SFLOAT : preferred_data_format; }
192 float get_luminance_multiplier() const;
193 void set_vrs(RendererRD::VRS *p_vrs) { vrs = p_vrs; }
194 RS::ViewportVRSMode get_vrs_mode() { return vrs_mode; }
195
196 void cleanup();
197 virtual void configure(const RenderSceneBuffersConfiguration *p_config) override;
198 void configure_for_reflections(const Size2i p_reflection_size);
199 virtual void set_fsr_sharpness(float p_fsr_sharpness) override;
200 virtual void set_texture_mipmap_bias(float p_texture_mipmap_bias) override;
201 virtual void set_anisotropic_filtering_level(RS::ViewportAnisotropicFiltering p_anisotropic_filtering_level) override;
202 virtual void set_use_debanding(bool p_use_debanding) override;
203
204 #ifdef METAL_ENABLED
205 void ensure_mfx(RendererRD::MFXSpatialEffect *p_effect);
206 _FORCE_INLINE_ RendererRD::MFXSpatialContext *get_mfx_spatial_context() const { return mfx_spatial_context; }
207 #endif
208
209 // Named Textures
210
211 bool has_texture(const StringName &p_context, const StringName &p_texture_name) const;
212 RID create_texture(const StringName &p_context, const StringName &p_texture_name, const RD::DataFormat p_data_format, const uint32_t p_usage_bits, const RD::TextureSamples p_texture_samples = RD::TEXTURE_SAMPLES_1, const Size2i p_size = Size2i(0, 0), const uint32_t p_layers = 0, const uint32_t p_mipmaps = 1, bool p_unique = true, bool p_discardable = false);
213 RID create_texture_from_format(const StringName &p_context, const StringName &p_texture_name, const RD::TextureFormat &p_texture_format, RD::TextureView p_view = RD::TextureView(), bool p_unique = true);
214 RID create_texture_view(const StringName &p_context, const StringName &p_texture_name, const StringName &p_view_name, RD::TextureView p_view = RD::TextureView());
215 RID get_texture(const StringName &p_context, const StringName &p_texture_name) const;
216 const RD::TextureFormat get_texture_format(const StringName &p_context, const StringName &p_texture_name) const;
217 RID get_texture_slice(const StringName &p_context, const StringName &p_texture_name, const uint32_t p_layer, const uint32_t p_mipmap, const uint32_t p_layers = 1, const uint32_t p_mipmaps = 1);
218 RID get_texture_slice_view(const StringName &p_context, const StringName &p_texture_name, const uint32_t p_layer, const uint32_t p_mipmap, const uint32_t p_layers = 1, const uint32_t p_mipmaps = 1, RD::TextureView p_view = RD::TextureView());
219 Size2i get_texture_slice_size(const StringName &p_context, const StringName &p_texture_name, const uint32_t p_mipmap);
220
221 void clear_context(const StringName &p_context);
222
223 // Allocate shared buffers
224 void allocate_blur_textures();
225
226 // Custom data
227 bool has_custom_data(const StringName &p_name);
228 void set_custom_data(const StringName &p_name, Ref<RenderBufferCustomDataRD> p_data);
229 Ref<RenderBufferCustomDataRD> get_custom_data(const StringName &p_name) const;
230
231 // Getters
232
233 _FORCE_INLINE_ RID get_render_target() const { return render_target; }
234 _FORCE_INLINE_ uint32_t get_view_count() const { return view_count; }
235 _FORCE_INLINE_ Size2i get_internal_size() const { return internal_size; }
236 _FORCE_INLINE_ Size2i get_target_size() const { return target_size; }
237 _FORCE_INLINE_ RS::ViewportScaling3DMode get_scaling_3d_mode() const { return scaling_3d_mode; }
238 _FORCE_INLINE_ float get_fsr_sharpness() const { return fsr_sharpness; }
239 _FORCE_INLINE_ RS::ViewportMSAA get_msaa_3d() const { return msaa_3d; }
240 _FORCE_INLINE_ RD::TextureSamples get_texture_samples() const { return texture_samples; }
241 _FORCE_INLINE_ RS::ViewportScreenSpaceAA get_screen_space_aa() const { return screen_space_aa; }
242 _FORCE_INLINE_ bool get_use_taa() const { return use_taa; }
243 _FORCE_INLINE_ bool get_use_debanding() const { return use_debanding; }
244
245 uint64_t get_auto_exposure_version() const { return auto_exposure_version; }
246 void set_auto_exposure_version(const uint64_t p_auto_exposure_version) { auto_exposure_version = p_auto_exposure_version; }
247
248 // For our internal textures we provide some easy access methods.
249
250 _FORCE_INLINE_ bool has_internal_texture() const {
251 return has_texture(RB_SCOPE_BUFFERS, RB_TEX_COLOR);
252 }
253 _FORCE_INLINE_ RID get_internal_texture() const {
254 return get_texture(RB_SCOPE_BUFFERS, RB_TEX_COLOR);
255 }
256 _FORCE_INLINE_ RID get_internal_texture(const uint32_t p_layer) {
257 return get_texture_slice(RB_SCOPE_BUFFERS, RB_TEX_COLOR, p_layer, 0);
258 }
259 _FORCE_INLINE_ RID get_internal_texture_reactive(const uint32_t p_layer) {
260 RD::TextureView alpha_only_view;
261 alpha_only_view.swizzle_r = RD::TEXTURE_SWIZZLE_A;
262 alpha_only_view.swizzle_g = RD::TEXTURE_SWIZZLE_A;
263 alpha_only_view.swizzle_b = RD::TEXTURE_SWIZZLE_A;
264 alpha_only_view.swizzle_a = RD::TEXTURE_SWIZZLE_A;
265 return get_texture_slice_view(RB_SCOPE_BUFFERS, RB_TEX_COLOR, p_layer, 0, 1, 1, alpha_only_view);
266 }
267 _FORCE_INLINE_ RID get_color_msaa() const {
268 return get_texture(RB_SCOPE_BUFFERS, RB_TEX_COLOR_MSAA);
269 }
270 _FORCE_INLINE_ RID get_color_msaa(uint32_t p_layer) {
271 return get_texture_slice(RB_SCOPE_BUFFERS, RB_TEX_COLOR_MSAA, p_layer, 0);
272 }
273
274 bool has_depth_texture();
275 RID get_depth_texture();
276 RID get_depth_texture(const uint32_t p_layer);
277
278 RID get_depth_msaa() const {
279 return get_texture(RB_SCOPE_BUFFERS, RB_TEX_DEPTH_MSAA);
280 }
281 RID get_depth_msaa(uint32_t p_layer) {
282 return get_texture_slice(RB_SCOPE_BUFFERS, RB_TEX_DEPTH_MSAA, p_layer, 0);
283 }
284
285 // back buffer (color)
286 RID get_back_buffer_texture() const {
287 // Prefer returning the dedicated backbuffer color texture if it was created. Return the reused blur texture otherwise.
288 if (has_texture(RB_SCOPE_BUFFERS, RB_TEX_BACK_COLOR)) {
289 return get_texture(RB_SCOPE_BUFFERS, RB_TEX_BACK_COLOR);
290 } else if (has_texture(RB_SCOPE_BUFFERS, RB_TEX_BLUR_0)) {
291 return get_texture(RB_SCOPE_BUFFERS, RB_TEX_BLUR_0);
292 } else {
293 return RID();
294 }
295 }
296
297 // Upscaled.
298 void ensure_upscaled();
299
300 _FORCE_INLINE_ bool has_upscaled_texture() const {
301 return has_texture(RB_SCOPE_BUFFERS, RB_TEX_COLOR_UPSCALED);
302 }
303 _FORCE_INLINE_ RID get_upscaled_texture() const {
304 return get_texture(RB_SCOPE_BUFFERS, RB_TEX_COLOR_UPSCALED);
305 }
306 _FORCE_INLINE_ RID get_upscaled_texture(const uint32_t p_layer) {
307 return get_texture_slice(RB_SCOPE_BUFFERS, RB_TEX_COLOR_UPSCALED, p_layer, 0);
308 }
309
310 // Velocity, currently only used by TAA (Clustered) but we'll be using this in other places soon too.
311
312 void ensure_velocity();
313 bool has_velocity_buffer(bool p_has_msaa);
314 RID get_velocity_buffer(bool p_get_msaa);
315 RID get_velocity_buffer(bool p_get_msaa, uint32_t p_layer);
316
317 RID get_velocity_depth_buffer();
318
319 // Samplers adjusted with the mipmap bias that is best fit for the configuration of these render buffers.
320
321 _FORCE_INLINE_ RendererRD::MaterialStorage::Samplers get_samplers() const {
322 return samplers;
323 }
324
325 _FORCE_INLINE_ static RD::TextureSamples msaa_to_samples(RS::ViewportMSAA p_msaa) {
326 switch (p_msaa) {
327 case RS::VIEWPORT_MSAA_DISABLED:
328 return RD::TEXTURE_SAMPLES_1;
329 case RS::VIEWPORT_MSAA_2X:
330 return RD::TEXTURE_SAMPLES_2;
331 case RS::VIEWPORT_MSAA_4X:
332 return RD::TEXTURE_SAMPLES_4;
333 case RS::VIEWPORT_MSAA_8X:
334 return RD::TEXTURE_SAMPLES_8;
335 default:
336 DEV_ASSERT(false && "Unknown MSAA option.");
337 return RD::TEXTURE_SAMPLES_1;
338 }
339 }
340
341 static uint32_t get_color_usage_bits(bool p_resolve, bool p_msaa, bool p_storage);
342 static RD::DataFormat get_depth_format(bool p_resolve, bool p_msaa, bool p_storage);
343 static uint32_t get_depth_usage_bits(bool p_resolve, bool p_msaa, bool p_storage);
344 static RD::DataFormat get_velocity_format();
345 static uint32_t get_velocity_usage_bits(bool p_resolve, bool p_msaa, bool p_storage);
346 static RD::DataFormat get_vrs_format();
347 static uint32_t get_vrs_usage_bits();
348
349 private:
350 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
351 // Our classDB doesn't support calling our normal exposed functions
352
353 RID _create_texture_from_format(const StringName &p_context, const StringName &p_texture_name, const Ref<RDTextureFormat> &p_texture_format, const Ref<RDTextureView> &p_view = Ref<RDTextureView>(), bool p_unique = true);
354 RID _create_texture_view(const StringName &p_context, const StringName &p_texture_name, const StringName &p_view_name, const Ref<RDTextureView> p_view = Ref<RDTextureView>());
355 Ref<RDTextureFormat> _get_texture_format(const StringName &p_context, const StringName &p_texture_name) const;
356 RID _get_texture_slice_view(const StringName &p_context, const StringName &p_texture_name, const uint32_t p_layer, const uint32_t p_mipmap, const uint32_t p_layers = 1, const uint32_t p_mipmaps = 1, const Ref<RDTextureView> p_view = Ref<RDTextureView>());
357
358 // For color and depth as exposed to extensions:
359 // - we need separately named functions to access the layer,
360 // - we don't output an error for missing buffers but just return an empty RID.
361 RID _get_color_texture(bool p_msaa = false) {
362 if (p_msaa) {
363 if (has_texture(RB_SCOPE_BUFFERS, RB_TEX_COLOR_MSAA)) {
364 return get_texture(RB_SCOPE_BUFFERS, RB_TEX_COLOR_MSAA);
365 } else {
366 return RID();
367 }
368 } else if (has_internal_texture()) {
369 return get_internal_texture();
370 } else {
371 return RID();
372 }
373 }
374
375 RID _get_color_layer(const uint32_t p_layer, bool p_msaa = false) {
376 if (p_msaa) {
377 if (has_texture(RB_SCOPE_BUFFERS, RB_TEX_COLOR_MSAA)) {
378 return get_texture_slice(RB_SCOPE_BUFFERS, RB_TEX_COLOR_MSAA, p_layer, 0);
379 } else {
380 return RID();
381 }
382 } else if (has_internal_texture()) {
383 return get_internal_texture(p_layer);
384 } else {
385 return RID();
386 }
387 }
388
389 RID _get_depth_texture(bool p_msaa = false) {
390 if (p_msaa) {
391 if (has_texture(RB_SCOPE_BUFFERS, RB_TEX_DEPTH_MSAA)) {
392 return get_texture(RB_SCOPE_BUFFERS, RB_TEX_DEPTH_MSAA);
393 } else {
394 return RID();
395 }
396 } else if (has_depth_texture()) {
397 return get_depth_texture();
398 } else {
399 return RID();
400 }
401 }
402
403 RID _get_depth_layer(const uint32_t p_layer, bool p_msaa = false) {
404 if (p_msaa) {
405 if (has_texture(RB_SCOPE_BUFFERS, RB_TEX_DEPTH_MSAA)) {
406 return get_texture_slice(RB_SCOPE_BUFFERS, RB_TEX_DEPTH_MSAA, p_layer, 0);
407 } else {
408 return RID();
409 }
410 } else if (has_depth_texture()) {
411 return get_depth_texture(p_layer);
412 } else {
413 return RID();
414 }
415 }
416
417 RID _get_velocity_texture(bool p_msaa = false) {
418 if (has_velocity_buffer(p_msaa)) {
419 return get_velocity_buffer(p_msaa);
420 } else {
421 return RID();
422 }
423 }
424
425 RID _get_velocity_layer(const uint32_t p_layer, bool p_msaa = false) {
426 if (has_velocity_buffer(p_msaa)) {
427 return get_velocity_buffer(p_msaa, p_layer);
428 } else {
429 return RID();
430 }
431 }
432
433 #ifndef DISABLE_DEPRECATED
434
435 RID _get_color_texture_compat_80214();
436 RID _get_color_layer_compat_80214(const uint32_t p_layer);
437 RID _get_depth_texture_compat_80214();
438 RID _get_depth_layer_compat_80214(const uint32_t p_layer);
439 RID _get_velocity_texture_compat_80214();
440 RID _get_velocity_layer_compat_80214(const uint32_t p_layer);
441
442 RID _create_texture_bind_compat_98670(const StringName &p_context, const StringName &p_texture_name, const RD::DataFormat p_data_format, const uint32_t p_usage_bits, const RD::TextureSamples p_texture_samples, const Size2i p_size, const uint32_t p_layers, const uint32_t p_mipmaps, bool p_unique);
443
444 static void _bind_compatibility_methods();
445
446 #endif // DISABLE_DEPRECATED
447
448 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
449 // Everything after this needs to be re-evaluated, this is all old implementation
450 public:
451 struct WeightBuffers {
452 RID weight;
453 RID fb; // FB with both texture and weight writing into one level lower
454 };
455
456 // 2 full size, 2 half size
457 WeightBuffers weight_buffers[4]; // Only used in raster
458 };