sort_effects.cpp 5.0 KB

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