sort_effects.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**************************************************************************/
  2. /* sort_effects.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 "sort_effects.h"
  31. // #include "servers/rendering/renderer_rd/renderer_compositor_rd.h"
  32. #include "servers/rendering/renderer_rd/storage_rd/material_storage.h"
  33. #include "servers/rendering/renderer_rd/uniform_set_cache_rd.h"
  34. using namespace RendererRD;
  35. SortEffects::SortEffects() {
  36. Vector<String> sort_modes;
  37. sort_modes.push_back("\n#define MODE_SORT_BLOCK\n");
  38. sort_modes.push_back("\n#define MODE_SORT_STEP\n");
  39. sort_modes.push_back("\n#define MODE_SORT_INNER\n");
  40. shader.initialize(sort_modes);
  41. shader_version = shader.version_create();
  42. for (int i = 0; i < SORT_MODE_MAX; i++) {
  43. pipelines[i] = RD::get_singleton()->compute_pipeline_create(shader.version_get_shader(shader_version, i));
  44. }
  45. }
  46. SortEffects::~SortEffects() {
  47. shader.version_free(shader_version);
  48. }
  49. void SortEffects::sort_buffer(RID p_uniform_set, int p_size) {
  50. PushConstant push_constant;
  51. push_constant.total_elements = p_size;
  52. bool done = true;
  53. int numThreadGroups = ((p_size - 1) >> 9) + 1;
  54. if (numThreadGroups > 1) {
  55. done = false;
  56. }
  57. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  58. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, pipelines[SORT_MODE_BLOCK]);
  59. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, p_uniform_set, 1);
  60. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(PushConstant));
  61. RD::get_singleton()->compute_list_dispatch(compute_list, numThreadGroups, 1, 1);
  62. int presorted = 512;
  63. while (!done) {
  64. RD::get_singleton()->compute_list_add_barrier(compute_list);
  65. done = true;
  66. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, pipelines[SORT_MODE_STEP]);
  67. numThreadGroups = 0;
  68. if (p_size > presorted) {
  69. if (p_size > presorted * 2) {
  70. done = false;
  71. }
  72. int pow2 = presorted;
  73. while (pow2 < p_size) {
  74. pow2 *= 2;
  75. }
  76. numThreadGroups = pow2 >> 9;
  77. }
  78. unsigned int nMergeSize = presorted * 2;
  79. for (unsigned int nMergeSubSize = nMergeSize >> 1; nMergeSubSize > 256; nMergeSubSize = nMergeSubSize >> 1) {
  80. push_constant.job_params[0] = nMergeSubSize;
  81. if (nMergeSubSize == nMergeSize >> 1) {
  82. push_constant.job_params[1] = (2 * nMergeSubSize - 1);
  83. push_constant.job_params[2] = -1;
  84. } else {
  85. push_constant.job_params[1] = nMergeSubSize;
  86. push_constant.job_params[2] = 1;
  87. }
  88. push_constant.job_params[3] = 0;
  89. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(PushConstant));
  90. RD::get_singleton()->compute_list_dispatch(compute_list, numThreadGroups, 1, 1);
  91. RD::get_singleton()->compute_list_add_barrier(compute_list);
  92. }
  93. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, pipelines[SORT_MODE_INNER]);
  94. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(PushConstant));
  95. RD::get_singleton()->compute_list_dispatch(compute_list, numThreadGroups, 1, 1);
  96. presorted *= 2;
  97. }
  98. RD::get_singleton()->compute_list_end();
  99. }