Blender V4.3
reduce_to_single_value_operation.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_assert.h"
6
7#include "GPU_state.hh"
8#include "GPU_texture.hh"
9
10#include "MEM_guardedalloc.h"
11
12#include "COM_context.hh"
15#include "COM_result.hh"
16
18
20 : SimpleOperation(context)
21{
22 InputDescriptor input_descriptor;
23 input_descriptor.type = type;
24 declare_input_descriptor(input_descriptor);
25 populate_result(context.create_result(type));
26}
27
29{
30 /* Make sure any prior writes to the texture are reflected before downloading it. */
32
33 const Result &input = get_input();
34 float *pixel = static_cast<float *>(GPU_texture_read(input, GPU_DATA_FLOAT, 0));
35
36 Result &result = get_result();
37 result.allocate_single_value();
38 switch (result.type()) {
40 result.set_color_value(pixel);
41 break;
43 result.set_vector_value(pixel);
44 break;
46 result.set_float_value(*pixel);
47 break;
48 default:
49 /* Other types are internal and needn't be handled by operations. */
51 break;
52 }
53
54 MEM_freeN(pixel);
55}
56
58 const Result &input_result)
59{
60 /* Input result is already a single value, the operation is not needed. */
61 if (input_result.is_single_value()) {
62 return nullptr;
63 }
64
65 /* The input is a full sized texture and can't be reduced to a single value, the operation is not
66 * needed. */
67 if (input_result.domain().size != int2(1)) {
68 return nullptr;
69 }
70
71 /* The input is a texture of a single pixel and can be reduced to a single value. */
72 return new ReduceToSingleValueOperation(context, input_result.type());
73}
74
75} // namespace blender::realtime_compositor
#define BLI_assert_unreachable()
Definition BLI_assert.h:97
void GPU_memory_barrier(eGPUBarrier barrier)
Definition gpu_state.cc:374
@ GPU_BARRIER_TEXTURE_UPDATE
Definition GPU_state.hh:39
void * GPU_texture_read(GPUTexture *texture, eGPUDataFormat data_format, int mip_level)
@ GPU_DATA_FLOAT
Read Guarded memory(de)allocation.
static SimpleOperation * construct_if_needed(Context &context, const Result &input_result)
const Domain & domain() const
Definition result.cc:712
static ResultType type(eGPUTextureFormat format)
Definition result.cc:148
void declare_input_descriptor(InputDescriptor descriptor)
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105