Debian Codegraph @

servers/rendering/renderer_rd/effects/sort_effects.cpp

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