renderer_compositor_rd.cpp 14 KB

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