vrs.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #include "vrs.h"
  31. #include "../renderer_compositor_rd.h"
  32. #include "../storage_rd/texture_storage.h"
  33. #include "../uniform_set_cache_rd.h"
  34. #ifndef _3D_DISABLED
  35. #include "servers/xr_server.h"
  36. #endif // _3D_DISABLED
  37. using namespace RendererRD;
  38. VRS::VRS() {
  39. {
  40. Vector<String> vrs_modes;
  41. vrs_modes.push_back("\n"); // VRS_DEFAULT
  42. vrs_modes.push_back("\n#define USE_MULTIVIEW\n"); // VRS_MULTIVIEW
  43. vrs_shader.shader.initialize(vrs_modes);
  44. if (!RendererCompositorRD::get_singleton()->is_xr_enabled()) {
  45. vrs_shader.shader.set_variant_enabled(VRS_MULTIVIEW, false);
  46. }
  47. vrs_shader.shader_version = vrs_shader.shader.version_create();
  48. //use additive
  49. for (int i = 0; i < VRS_MAX; i++) {
  50. if (vrs_shader.shader.is_variant_enabled(i)) {
  51. 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);
  52. } else {
  53. vrs_shader.pipelines[i].clear();
  54. }
  55. }
  56. }
  57. }
  58. VRS::~VRS() {
  59. vrs_shader.shader.version_free(vrs_shader.shader_version);
  60. }
  61. void VRS::copy_vrs(RID p_source_rd_texture, RID p_dest_framebuffer, bool p_multiview) {
  62. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  63. ERR_FAIL_NULL(uniform_set_cache);
  64. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  65. ERR_FAIL_NULL(material_storage);
  66. // setup our uniforms
  67. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  68. RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
  69. VRSPushConstant push_constant = {};
  70. int mode = p_multiview ? VRS_MULTIVIEW : VRS_DEFAULT;
  71. // Set maximum texel factor based on maximum fragment size, some GPUs do not support 8x8 (fragment shading rate approach).
  72. if (MIN(RD::get_singleton()->limit_get(RD::LIMIT_VRS_MAX_FRAGMENT_WIDTH), RD::get_singleton()->limit_get(RD::LIMIT_VRS_MAX_FRAGMENT_HEIGHT)) > 4) {
  73. push_constant.max_texel_factor = 3.0;
  74. } else {
  75. push_constant.max_texel_factor = 2.0;
  76. }
  77. RID shader = vrs_shader.shader.version_get_shader(vrs_shader.shader_version, mode);
  78. ERR_FAIL_COND(shader.is_null());
  79. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer);
  80. 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)));
  81. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
  82. RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(VRSPushConstant));
  83. RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
  84. RD::get_singleton()->draw_list_end();
  85. }
  86. Size2i VRS::get_vrs_texture_size(const Size2i p_base_size) const {
  87. int32_t texel_width = RD::get_singleton()->limit_get(RD::LIMIT_VRS_TEXEL_WIDTH);
  88. int32_t texel_height = RD::get_singleton()->limit_get(RD::LIMIT_VRS_TEXEL_HEIGHT);
  89. int width = p_base_size.x / texel_width;
  90. if (p_base_size.x % texel_width != 0) {
  91. width++;
  92. }
  93. int height = p_base_size.y / texel_height;
  94. if (p_base_size.y % texel_height != 0) {
  95. height++;
  96. }
  97. return Size2i(width, height);
  98. }
  99. void VRS::update_vrs_texture(RID p_vrs_fb, RID p_render_target) {
  100. TextureStorage *texture_storage = TextureStorage::get_singleton();
  101. RS::ViewportVRSMode vrs_mode = texture_storage->render_target_get_vrs_mode(p_render_target);
  102. RS::ViewportVRSUpdateMode vrs_update_mode = texture_storage->render_target_get_vrs_update_mode(p_render_target);
  103. if (vrs_mode != RS::VIEWPORT_VRS_DISABLED && vrs_update_mode != RS::VIEWPORT_VRS_UPDATE_DISABLED) {
  104. RD::get_singleton()->draw_command_begin_label("VRS Setup");
  105. if (vrs_mode == RS::VIEWPORT_VRS_TEXTURE) {
  106. RID vrs_texture = texture_storage->render_target_get_vrs_texture(p_render_target);
  107. if (vrs_texture.is_valid()) {
  108. RID rd_texture = texture_storage->texture_get_rd_texture(vrs_texture);
  109. int layers = texture_storage->texture_get_layers(vrs_texture);
  110. if (rd_texture.is_valid()) {
  111. // Copy into our density buffer
  112. copy_vrs(rd_texture, p_vrs_fb, layers > 1);
  113. }
  114. }
  115. #ifndef _3D_DISABLED
  116. } else if (vrs_mode == RS::VIEWPORT_VRS_XR) {
  117. Ref<XRInterface> interface = XRServer::get_singleton()->get_primary_interface();
  118. if (interface.is_valid()) {
  119. RID vrs_texture = interface->get_vrs_texture();
  120. if (vrs_texture.is_valid()) {
  121. RID rd_texture = texture_storage->texture_get_rd_texture(vrs_texture);
  122. int layers = texture_storage->texture_get_layers(vrs_texture);
  123. if (rd_texture.is_valid()) {
  124. // Copy into our density buffer
  125. copy_vrs(rd_texture, p_vrs_fb, layers > 1);
  126. }
  127. }
  128. }
  129. #endif // _3D_DISABLED
  130. }
  131. if (vrs_update_mode == RS::VIEWPORT_VRS_UPDATE_ONCE) {
  132. texture_storage->render_target_set_vrs_update_mode(p_render_target, RS::VIEWPORT_VRS_UPDATE_DISABLED);
  133. }
  134. RD::get_singleton()->draw_command_end_label();
  135. }
  136. }