Welcome to mirror list, hosted at ThFree Co, Russian Federation.

algorithm_parallel_reduction.cc « intern « algorithms « realtime_compositor « compositor « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3266ccd14ebb329cd02cb7277be6fa873b572988 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* SPDX-License-Identifier: GPL-2.0-or-later */

#include "BLI_math_vec_types.hh"
#include "BLI_math_vector.hh"

#include "MEM_guardedalloc.h"

#include "GPU_compute.h"
#include "GPU_shader.h"
#include "GPU_texture.h"

#include "COM_context.hh"
#include "COM_utilities.hh"

#include "COM_algorithm_parallel_reduction.hh"

namespace blender::realtime_compositor {

/* Reduces the given texture into a single value and returns it. The return value should be freed
 * by a call to MEM_freeN. The return value is either a pointer to a float, or a pointer to an
 * array of floats that represents a vector. This depends on the given format, which should be
 * compatible with the reduction shader.
 *
 * The given reduction shader should be bound when calling the function and the shader is expected
 * to be derived from the compositor_parallel_reduction.glsl shader, see that file for more
 * information. Also see the compositor_parallel_reduction_info.hh file for example shader
 * definitions. */
static float *parallel_reduction_dispatch(Context &context,
                                          GPUTexture *texture,
                                          GPUShader *shader,
                                          eGPUTextureFormat format)
{
  GPU_shader_uniform_1b(shader, "is_initial_reduction", true);

  GPUTexture *texture_to_reduce = texture;
  int2 size_to_reduce = int2(GPU_texture_width(texture), GPU_texture_height(texture));

  /* Dispatch the reduction shader until the texture reduces to a single pixel. */
  while (size_to_reduce != int2(1)) {
    const int2 reduced_size = math::divide_ceil(size_to_reduce, int2(16));
    GPUTexture *reduced_texture = context.texture_pool().acquire(reduced_size, format);

    GPU_memory_barrier(GPU_BARRIER_TEXTURE_FETCH);
    const int texture_image_unit = GPU_shader_get_texture_binding(shader, "input_tx");
    GPU_texture_bind(texture_to_reduce, texture_image_unit);

    const int image_unit = GPU_shader_get_texture_binding(shader, "output_img");
    GPU_texture_image_bind(reduced_texture, image_unit);

    GPU_compute_dispatch(shader, reduced_size.x, reduced_size.y, 1);

    GPU_texture_image_unbind(reduced_texture);
    GPU_texture_unbind(texture_to_reduce);

    /* Release the input texture only if it is not the source texture, since the source texture is
     * not acquired or owned by the function. */
    if (texture_to_reduce != texture) {
      context.texture_pool().release(texture_to_reduce);
    }

    texture_to_reduce = reduced_texture;
    size_to_reduce = reduced_size;

    GPU_shader_uniform_1b(shader, "is_initial_reduction", false);
  }

  GPU_memory_barrier(GPU_BARRIER_TEXTURE_UPDATE);
  float *pixel = static_cast<float *>(GPU_texture_read(texture_to_reduce, GPU_DATA_FLOAT, 0));

  /* Release the final texture only if it is not the source texture, since the source texture is
   * not acquired or owned by the function. */
  if (texture_to_reduce != texture) {
    context.texture_pool().release(texture_to_reduce);
  }

  return pixel;
}

/* --------------------------------------------------------------------
 * Sum Reductions.
 */

float sum_red(Context &context, GPUTexture *texture)
{
  GPUShader *shader = context.shader_manager().get("compositor_sum_red");
  GPU_shader_bind(shader);

  float *reduced_value = parallel_reduction_dispatch(context, texture, shader, GPU_R32F);
  const float sum = *reduced_value;
  MEM_freeN(reduced_value);
  GPU_shader_unbind();

  return sum;
}

float sum_green(Context &context, GPUTexture *texture)
{
  GPUShader *shader = context.shader_manager().get("compositor_sum_green");
  GPU_shader_bind(shader);

  float *reduced_value = parallel_reduction_dispatch(context, texture, shader, GPU_R32F);
  const float sum = *reduced_value;
  MEM_freeN(reduced_value);
  GPU_shader_unbind();

  return sum;
}

float sum_blue(Context &context, GPUTexture *texture)
{
  GPUShader *shader = context.shader_manager().get("compositor_sum_blue");
  GPU_shader_bind(shader);

  float *reduced_value = parallel_reduction_dispatch(context, texture, shader, GPU_R32F);
  const float sum = *reduced_value;
  MEM_freeN(reduced_value);
  GPU_shader_unbind();

  return sum;
}

float sum_luminance(Context &context, GPUTexture *texture, float3 luminance_coefficients)
{
  GPUShader *shader = context.shader_manager().get("compositor_sum_luminance");
  GPU_shader_bind(shader);

  GPU_shader_uniform_3fv(shader, "luminance_coefficients", luminance_coefficients);

  float *reduced_value = parallel_reduction_dispatch(context, texture, shader, GPU_R32F);
  const float sum = *reduced_value;
  MEM_freeN(reduced_value);
  GPU_shader_unbind();

  return sum;
}

/* --------------------------------------------------------------------
 * Sum Of Squared Difference Reductions.
 */

float sum_red_squared_difference(Context &context, GPUTexture *texture, float subtrahend)
{
  GPUShader *shader = context.shader_manager().get("compositor_sum_red_squared_difference");
  GPU_shader_bind(shader);

  GPU_shader_uniform_1f(shader, "subtrahend", subtrahend);

  float *reduced_value = parallel_reduction_dispatch(context, texture, shader, GPU_R32F);
  const float sum = *reduced_value;
  MEM_freeN(reduced_value);
  GPU_shader_unbind();

  return sum;
}

float sum_green_squared_difference(Context &context, GPUTexture *texture, float subtrahend)
{
  GPUShader *shader = context.shader_manager().get("compositor_sum_green_squared_difference");
  GPU_shader_bind(shader);

  GPU_shader_uniform_1f(shader, "subtrahend", subtrahend);

  float *reduced_value = parallel_reduction_dispatch(context, texture, shader, GPU_R32F);
  const float sum = *reduced_value;
  MEM_freeN(reduced_value);
  GPU_shader_unbind();

  return sum;
}

float sum_blue_squared_difference(Context &context, GPUTexture *texture, float subtrahend)
{
  GPUShader *shader = context.shader_manager().get("compositor_sum_blue_squared_difference");
  GPU_shader_bind(shader);

  GPU_shader_uniform_1f(shader, "subtrahend", subtrahend);

  float *reduced_value = parallel_reduction_dispatch(context, texture, shader, GPU_R32F);
  const float sum = *reduced_value;
  MEM_freeN(reduced_value);
  GPU_shader_unbind();

  return sum;
}

float sum_luminance_squared_difference(Context &context,
                                       GPUTexture *texture,
                                       float3 luminance_coefficients,
                                       float subtrahend)
{
  GPUShader *shader = context.shader_manager().get("compositor_sum_luminance_squared_difference");
  GPU_shader_bind(shader);

  GPU_shader_uniform_3fv(shader, "luminance_coefficients", luminance_coefficients);
  GPU_shader_uniform_1f(shader, "subtrahend", subtrahend);

  float *reduced_value = parallel_reduction_dispatch(context, texture, shader, GPU_R32F);
  const float sum = *reduced_value;
  MEM_freeN(reduced_value);
  GPU_shader_unbind();

  return sum;
}

}  // namespace blender::realtime_compositor