1 | /**************************************************************************/ |
2 | /* renderer_compositor_rd.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 "renderer_compositor_rd.h" |
32 | |
33 | #include "core/config/project_settings.h" |
34 | #include "core/io/dir_access.h" |
35 | |
36 | void RendererCompositorRD::prepare_for_blitting_render_targets() { |
37 | RD::get_singleton()->prepare_screen_for_drawing(); |
38 | } |
39 | |
40 | void RendererCompositorRD::blit_render_targets_to_screen(DisplayServer::WindowID p_screen, const BlitToScreen *p_render_targets, int p_amount) { |
41 | RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin_for_screen(p_screen); |
42 | if (draw_list == RD::INVALID_ID) { |
43 | return; // Window is minimized and does not have valid swapchain, skip drawing without printing errors. |
44 | } |
45 | |
46 | for (int i = 0; i < p_amount; i++) { |
47 | RID rd_texture = texture_storage->render_target_get_rd_texture(p_render_targets[i].render_target); |
48 | ERR_CONTINUE(rd_texture.is_null()); |
49 | |
50 | if (!render_target_descriptors.has(rd_texture) || !RD::get_singleton()->uniform_set_is_valid(render_target_descriptors[rd_texture])) { |
51 | Vector<RD::Uniform> uniforms; |
52 | RD::Uniform u; |
53 | u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE; |
54 | u.binding = 0; |
55 | u.append_id(blit.sampler); |
56 | u.append_id(rd_texture); |
57 | uniforms.push_back(u); |
58 | RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, blit.shader.version_get_shader(blit.shader_version, BLIT_MODE_NORMAL), 0); |
59 | |
60 | render_target_descriptors[rd_texture] = uniform_set; |
61 | } |
62 | |
63 | Size2 screen_size(RD::get_singleton()->screen_get_width(p_screen), RD::get_singleton()->screen_get_height(p_screen)); |
64 | BlitMode mode = p_render_targets[i].lens_distortion.apply ? BLIT_MODE_LENS : (p_render_targets[i].multi_view.use_layer ? BLIT_MODE_USE_LAYER : BLIT_MODE_NORMAL); |
65 | RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, blit.pipelines[mode]); |
66 | RD::get_singleton()->draw_list_bind_index_array(draw_list, blit.array); |
67 | RD::get_singleton()->draw_list_bind_uniform_set(draw_list, render_target_descriptors[rd_texture], 0); |
68 | |
69 | blit.push_constant.src_rect[0] = p_render_targets[i].src_rect.position.x; |
70 | blit.push_constant.src_rect[1] = p_render_targets[i].src_rect.position.y; |
71 | blit.push_constant.src_rect[2] = p_render_targets[i].src_rect.size.width; |
72 | blit.push_constant.src_rect[3] = p_render_targets[i].src_rect.size.height; |
73 | blit.push_constant.dst_rect[0] = p_render_targets[i].dst_rect.position.x / screen_size.width; |
74 | blit.push_constant.dst_rect[1] = p_render_targets[i].dst_rect.position.y / screen_size.height; |
75 | blit.push_constant.dst_rect[2] = p_render_targets[i].dst_rect.size.width / screen_size.width; |
76 | blit.push_constant.dst_rect[3] = p_render_targets[i].dst_rect.size.height / screen_size.height; |
77 | blit.push_constant.layer = p_render_targets[i].multi_view.layer; |
78 | blit.push_constant.eye_center[0] = p_render_targets[i].lens_distortion.eye_center.x; |
79 | blit.push_constant.eye_center[1] = p_render_targets[i].lens_distortion.eye_center.y; |
80 | blit.push_constant.k1 = p_render_targets[i].lens_distortion.k1; |
81 | blit.push_constant.k2 = p_render_targets[i].lens_distortion.k2; |
82 | blit.push_constant.upscale = p_render_targets[i].lens_distortion.upscale; |
83 | blit.push_constant.aspect_ratio = p_render_targets[i].lens_distortion.aspect_ratio; |
84 | blit.push_constant.convert_to_srgb = texture_storage->render_target_is_using_hdr(p_render_targets[i].render_target); |
85 | |
86 | RD::get_singleton()->draw_list_set_push_constant(draw_list, &blit.push_constant, sizeof(BlitPushConstant)); |
87 | RD::get_singleton()->draw_list_draw(draw_list, true); |
88 | } |
89 | |
90 | RD::get_singleton()->draw_list_end(); |
91 | } |
92 | |
93 | void RendererCompositorRD::begin_frame(double frame_step) { |
94 | frame++; |
95 | delta = frame_step; |
96 | time += frame_step; |
97 | |
98 | double time_roll_over = GLOBAL_GET("rendering/limits/time/time_rollover_secs" ); |
99 | time = Math::fmod(time, time_roll_over); |
100 | |
101 | canvas->set_time(time); |
102 | scene->set_time(time, frame_step); |
103 | } |
104 | |
105 | void RendererCompositorRD::end_frame(bool p_swap_buffers) { |
106 | // TODO: Likely pass a bool to swap buffers to avoid display? |
107 | RD::get_singleton()->swap_buffers(); |
108 | } |
109 | |
110 | void RendererCompositorRD::initialize() { |
111 | { |
112 | // Initialize blit |
113 | Vector<String> blit_modes; |
114 | blit_modes.push_back("\n" ); |
115 | blit_modes.push_back("\n#define USE_LAYER\n" ); |
116 | blit_modes.push_back("\n#define USE_LAYER\n#define APPLY_LENS_DISTORTION\n" ); |
117 | blit_modes.push_back("\n" ); |
118 | |
119 | blit.shader.initialize(blit_modes); |
120 | |
121 | blit.shader_version = blit.shader.version_create(); |
122 | |
123 | for (int i = 0; i < BLIT_MODE_MAX; i++) { |
124 | blit.pipelines[i] = RD::get_singleton()->render_pipeline_create(blit.shader.version_get_shader(blit.shader_version, i), RD::get_singleton()->screen_get_framebuffer_format(), RD::INVALID_ID, RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), i == BLIT_MODE_NORMAL_ALPHA ? RenderingDevice::PipelineColorBlendState::create_blend() : RenderingDevice::PipelineColorBlendState::create_disabled(), 0); |
125 | } |
126 | |
127 | //create index array for copy shader |
128 | Vector<uint8_t> pv; |
129 | pv.resize(6 * 2); |
130 | { |
131 | uint8_t *w = pv.ptrw(); |
132 | uint16_t *p16 = (uint16_t *)w; |
133 | p16[0] = 0; |
134 | p16[1] = 1; |
135 | p16[2] = 2; |
136 | p16[3] = 0; |
137 | p16[4] = 2; |
138 | p16[5] = 3; |
139 | } |
140 | blit.index_buffer = RD::get_singleton()->index_buffer_create(6, RenderingDevice::INDEX_BUFFER_FORMAT_UINT16, pv); |
141 | blit.array = RD::get_singleton()->index_array_create(blit.index_buffer, 0, 6); |
142 | |
143 | blit.sampler = RD::get_singleton()->sampler_create(RD::SamplerState()); |
144 | } |
145 | } |
146 | |
147 | uint64_t RendererCompositorRD::frame = 1; |
148 | |
149 | void RendererCompositorRD::finalize() { |
150 | memdelete(scene); |
151 | memdelete(canvas); |
152 | memdelete(fog); |
153 | memdelete(particles_storage); |
154 | memdelete(light_storage); |
155 | memdelete(mesh_storage); |
156 | memdelete(material_storage); |
157 | memdelete(texture_storage); |
158 | memdelete(utilities); |
159 | |
160 | //only need to erase these, the rest are erased by cascade |
161 | blit.shader.version_free(blit.shader_version); |
162 | RD::get_singleton()->free(blit.index_buffer); |
163 | RD::get_singleton()->free(blit.sampler); |
164 | } |
165 | |
166 | void RendererCompositorRD::set_boot_image(const Ref<Image> &p_image, const Color &p_color, bool p_scale, bool p_use_filter) { |
167 | if (p_image.is_null() || p_image->is_empty()) { |
168 | return; |
169 | } |
170 | |
171 | RD::get_singleton()->prepare_screen_for_drawing(); |
172 | |
173 | RID texture = texture_storage->texture_allocate(); |
174 | texture_storage->texture_2d_initialize(texture, p_image); |
175 | RID rd_texture = texture_storage->texture_get_rd_texture(texture, false); |
176 | |
177 | RD::SamplerState sampler_state; |
178 | sampler_state.min_filter = p_use_filter ? RD::SAMPLER_FILTER_LINEAR : RD::SAMPLER_FILTER_NEAREST; |
179 | sampler_state.mag_filter = p_use_filter ? RD::SAMPLER_FILTER_LINEAR : RD::SAMPLER_FILTER_NEAREST; |
180 | sampler_state.max_lod = 0; |
181 | RID sampler = RD::get_singleton()->sampler_create(sampler_state); |
182 | |
183 | RID uset; |
184 | { |
185 | Vector<RD::Uniform> uniforms; |
186 | RD::Uniform u; |
187 | u.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE; |
188 | u.binding = 0; |
189 | u.append_id(sampler); |
190 | u.append_id(rd_texture); |
191 | uniforms.push_back(u); |
192 | uset = RD::get_singleton()->uniform_set_create(uniforms, blit.shader.version_get_shader(blit.shader_version, BLIT_MODE_NORMAL), 0); |
193 | } |
194 | |
195 | Size2 window_size = DisplayServer::get_singleton()->window_get_size(); |
196 | |
197 | Rect2 imgrect(0, 0, p_image->get_width(), p_image->get_height()); |
198 | Rect2 screenrect; |
199 | if (p_scale) { |
200 | if (window_size.width > window_size.height) { |
201 | //scale horizontally |
202 | screenrect.size.y = window_size.height; |
203 | screenrect.size.x = imgrect.size.x * window_size.height / imgrect.size.y; |
204 | screenrect.position.x = (window_size.width - screenrect.size.x) / 2; |
205 | |
206 | } else { |
207 | //scale vertically |
208 | screenrect.size.x = window_size.width; |
209 | screenrect.size.y = imgrect.size.y * window_size.width / imgrect.size.x; |
210 | screenrect.position.y = (window_size.height - screenrect.size.y) / 2; |
211 | } |
212 | } else { |
213 | screenrect = imgrect; |
214 | screenrect.position += ((window_size - screenrect.size) / 2.0).floor(); |
215 | } |
216 | |
217 | screenrect.position /= window_size; |
218 | screenrect.size /= window_size; |
219 | |
220 | RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin_for_screen(DisplayServer::MAIN_WINDOW_ID, p_color); |
221 | |
222 | RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, blit.pipelines[BLIT_MODE_NORMAL_ALPHA]); |
223 | RD::get_singleton()->draw_list_bind_index_array(draw_list, blit.array); |
224 | RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uset, 0); |
225 | |
226 | blit.push_constant.src_rect[0] = 0.0; |
227 | blit.push_constant.src_rect[1] = 0.0; |
228 | blit.push_constant.src_rect[2] = 1.0; |
229 | blit.push_constant.src_rect[3] = 1.0; |
230 | blit.push_constant.dst_rect[0] = screenrect.position.x; |
231 | blit.push_constant.dst_rect[1] = screenrect.position.y; |
232 | blit.push_constant.dst_rect[2] = screenrect.size.width; |
233 | blit.push_constant.dst_rect[3] = screenrect.size.height; |
234 | blit.push_constant.layer = 0; |
235 | blit.push_constant.eye_center[0] = 0; |
236 | blit.push_constant.eye_center[1] = 0; |
237 | blit.push_constant.k1 = 0; |
238 | blit.push_constant.k2 = 0; |
239 | blit.push_constant.upscale = 1.0; |
240 | blit.push_constant.aspect_ratio = 1.0; |
241 | blit.push_constant.convert_to_srgb = false; |
242 | |
243 | RD::get_singleton()->draw_list_set_push_constant(draw_list, &blit.push_constant, sizeof(BlitPushConstant)); |
244 | RD::get_singleton()->draw_list_draw(draw_list, true); |
245 | |
246 | RD::get_singleton()->draw_list_end(); |
247 | |
248 | RD::get_singleton()->swap_buffers(); |
249 | |
250 | texture_storage->texture_free(texture); |
251 | RD::get_singleton()->free(sampler); |
252 | } |
253 | |
254 | RendererCompositorRD *RendererCompositorRD::singleton = nullptr; |
255 | |
256 | RendererCompositorRD::RendererCompositorRD() { |
257 | uniform_set_cache = memnew(UniformSetCacheRD); |
258 | framebuffer_cache = memnew(FramebufferCacheRD); |
259 | |
260 | { |
261 | String shader_cache_dir = Engine::get_singleton()->get_shader_cache_path(); |
262 | if (shader_cache_dir.is_empty()) { |
263 | shader_cache_dir = "user://" ; |
264 | } |
265 | Ref<DirAccess> da = DirAccess::open(shader_cache_dir); |
266 | if (da.is_null()) { |
267 | ERR_PRINT("Can't create shader cache folder, no shader caching will happen: " + shader_cache_dir); |
268 | } else { |
269 | Error err = da->change_dir("shader_cache" ); |
270 | if (err != OK) { |
271 | err = da->make_dir("shader_cache" ); |
272 | } |
273 | if (err != OK) { |
274 | ERR_PRINT("Can't create shader cache folder, no shader caching will happen: " + shader_cache_dir); |
275 | } else { |
276 | shader_cache_dir = shader_cache_dir.path_join("shader_cache" ); |
277 | |
278 | bool shader_cache_enabled = GLOBAL_GET("rendering/shader_compiler/shader_cache/enabled" ); |
279 | if (!Engine::get_singleton()->is_editor_hint() && !shader_cache_enabled) { |
280 | shader_cache_dir = String(); //disable only if not editor |
281 | } |
282 | |
283 | if (!shader_cache_dir.is_empty()) { |
284 | bool compress = GLOBAL_GET("rendering/shader_compiler/shader_cache/compress" ); |
285 | bool use_zstd = GLOBAL_GET("rendering/shader_compiler/shader_cache/use_zstd_compression" ); |
286 | bool strip_debug = GLOBAL_GET("rendering/shader_compiler/shader_cache/strip_debug" ); |
287 | |
288 | ShaderRD::set_shader_cache_dir(shader_cache_dir); |
289 | ShaderRD::set_shader_cache_save_compressed(compress); |
290 | ShaderRD::set_shader_cache_save_compressed_zstd(use_zstd); |
291 | ShaderRD::set_shader_cache_save_debug(!strip_debug); |
292 | } |
293 | } |
294 | } |
295 | } |
296 | |
297 | singleton = this; |
298 | |
299 | utilities = memnew(RendererRD::Utilities); |
300 | texture_storage = memnew(RendererRD::TextureStorage); |
301 | material_storage = memnew(RendererRD::MaterialStorage); |
302 | mesh_storage = memnew(RendererRD::MeshStorage); |
303 | light_storage = memnew(RendererRD::LightStorage); |
304 | particles_storage = memnew(RendererRD::ParticlesStorage); |
305 | fog = memnew(RendererRD::Fog); |
306 | canvas = memnew(RendererCanvasRenderRD()); |
307 | |
308 | String rendering_method = GLOBAL_GET("rendering/renderer/rendering_method" ); |
309 | uint64_t textures_per_stage = RD::get_singleton()->limit_get(RD::LIMIT_MAX_TEXTURES_PER_SHADER_STAGE); |
310 | |
311 | if (rendering_method == "mobile" || textures_per_stage < 48) { |
312 | scene = memnew(RendererSceneRenderImplementation::RenderForwardMobile()); |
313 | if (rendering_method == "forward_plus" ) { |
314 | WARN_PRINT_ONCE("Platform supports less than 48 textures per stage which is less than required by the Clustered renderer. Defaulting to Mobile renderer." ); |
315 | } |
316 | } else if (rendering_method == "forward_plus" ) { |
317 | // default to our high end renderer |
318 | scene = memnew(RendererSceneRenderImplementation::RenderForwardClustered()); |
319 | } else { |
320 | ERR_FAIL_MSG("Cannot instantiate RenderingDevice-based renderer with renderer type " + rendering_method); |
321 | } |
322 | |
323 | scene->init(); |
324 | } |
325 | |
326 | RendererCompositorRD::~RendererCompositorRD() { |
327 | memdelete(uniform_set_cache); |
328 | memdelete(framebuffer_cache); |
329 | ShaderRD::set_shader_cache_dir(String()); |
330 | } |
331 | |