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