1/**************************************************************************/
2/* resolve.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 "resolve.h"
32#include "servers/rendering/renderer_rd/renderer_compositor_rd.h"
33#include "servers/rendering/renderer_rd/storage_rd/material_storage.h"
34#include "servers/rendering/renderer_rd/uniform_set_cache_rd.h"
35
36using namespace RendererRD;
37
38Resolve::Resolve() {
39 Vector<String> resolve_modes;
40 resolve_modes.push_back("\n#define MODE_RESOLVE_GI\n");
41 resolve_modes.push_back("\n#define MODE_RESOLVE_GI\n#define VOXEL_GI_RESOLVE\n");
42 resolve_modes.push_back("\n#define MODE_RESOLVE_DEPTH\n");
43
44 resolve.shader.initialize(resolve_modes);
45
46 resolve.shader_version = resolve.shader.version_create();
47
48 for (int i = 0; i < RESOLVE_MODE_MAX; i++) {
49 resolve.pipelines[i] = RD::get_singleton()->compute_pipeline_create(resolve.shader.version_get_shader(resolve.shader_version, i));
50 }
51}
52
53Resolve::~Resolve() {
54 resolve.shader.version_free(resolve.shader_version);
55}
56
57void Resolve::resolve_gi(RID p_source_depth, RID p_source_normal_roughness, RID p_source_voxel_gi, RID p_dest_depth, RID p_dest_normal_roughness, RID p_dest_voxel_gi, Vector2i p_screen_size, int p_samples, uint32_t p_barrier) {
58 UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
59 ERR_FAIL_NULL(uniform_set_cache);
60 MaterialStorage *material_storage = MaterialStorage::get_singleton();
61 ERR_FAIL_NULL(material_storage);
62
63 ResolvePushConstant push_constant;
64 push_constant.screen_size[0] = p_screen_size.x;
65 push_constant.screen_size[1] = p_screen_size.y;
66 push_constant.samples = p_samples;
67
68 // setup our uniforms
69 RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
70
71 RD::Uniform u_source_depth(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_depth }));
72 RD::Uniform u_source_normal_roughness(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 1, Vector<RID>({ default_sampler, p_source_normal_roughness }));
73 RD::Uniform u_dest_depth(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_dest_depth }));
74 RD::Uniform u_dest_normal_roughness(RD::UNIFORM_TYPE_IMAGE, 1, Vector<RID>({ p_dest_normal_roughness }));
75
76 ResolveMode mode = p_source_voxel_gi.is_valid() ? RESOLVE_MODE_GI_VOXEL_GI : RESOLVE_MODE_GI;
77 RID shader = resolve.shader.version_get_shader(resolve.shader_version, mode);
78 ERR_FAIL_COND(shader.is_null());
79
80 RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
81 RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, resolve.pipelines[mode]);
82 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_depth, u_source_normal_roughness), 0);
83 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_dest_depth, u_dest_normal_roughness), 1);
84 if (p_source_voxel_gi.is_valid()) {
85 RD::Uniform u_source_voxel_gi(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_voxel_gi }));
86 RD::Uniform u_dest_voxel_gi(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_voxel_gi);
87
88 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 2, u_source_voxel_gi), 2);
89 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_dest_voxel_gi), 3);
90 }
91
92 RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(ResolvePushConstant));
93
94 RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.x, p_screen_size.y, 1);
95
96 RD::get_singleton()->compute_list_end(p_barrier);
97}
98
99void Resolve::resolve_depth(RID p_source_depth, RID p_dest_depth, Vector2i p_screen_size, int p_samples, uint32_t p_barrier) {
100 UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
101 ERR_FAIL_NULL(uniform_set_cache);
102 MaterialStorage *material_storage = MaterialStorage::get_singleton();
103 ERR_FAIL_NULL(material_storage);
104
105 ResolvePushConstant push_constant;
106 push_constant.screen_size[0] = p_screen_size.x;
107 push_constant.screen_size[1] = p_screen_size.y;
108 push_constant.samples = p_samples;
109
110 // setup our uniforms
111 RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
112
113 RD::Uniform u_source_depth(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_depth }));
114 RD::Uniform u_dest_depth(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_depth);
115
116 ResolveMode mode = RESOLVE_MODE_DEPTH;
117 RID shader = resolve.shader.version_get_shader(resolve.shader_version, mode);
118 ERR_FAIL_COND(shader.is_null());
119
120 RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
121 RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, resolve.pipelines[mode]);
122 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_depth), 0);
123 RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_dest_depth), 1);
124
125 RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(ResolvePushConstant));
126
127 RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_screen_size.x, p_screen_size.y, 1);
128
129 RD::get_singleton()->compute_list_end(p_barrier);
130}
131