Blender V5.3
conversion_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 <fmt/format.h>
6
7#include "BLI_generic_span.hh"
9#include "BLI_utildefines.hh"
10
11#include "GPU_shader.hh"
12
14
16
17#include "COM_context.hh"
20#include "COM_result.hh"
21#include "COM_utilities.hh"
22
23namespace blender::compositor {
24
26 const ResultType input_type,
27 const ResultType expected_type)
29{
31 this->populate_result(expected_type);
32}
33
35{
36 Result &result = this->get_result();
37 const Result &input = this->get_input();
38
40 if (!conversions.is_convertible(input.get_cpp_type(), result.get_cpp_type())) {
42 return;
43 }
44
45 if (input.is_single_value()) {
46 result.allocate_single_value();
47 this->execute_single(input, result);
48 return;
49 }
50
51 result.allocate_texture(input.domain());
52 if (this->context().use_gpu()) {
53 const std::string shader_name = fmt::format("compositor_convert_{}_to_{}",
54 Result::type_name(this->get_input().type()),
55 Result::type_name(this->get_result().type()));
56 gpu::Shader *shader = this->context().get_shader(shader_name.c_str());
58
59 if (this->get_input().type() == ResultType::Color &&
61 {
62 float luminance_coefficients[3];
64 GPU_shader_uniform_3fv(shader, "luminance_coefficients_u", luminance_coefficients);
65 }
66
67 input.bind_as_texture(shader, "input_tx");
68 result.bind_as_image(shader, "output_img");
69
71
72 input.unbind_as_texture();
73 result.unbind_as_image();
75 }
76 else {
77 this->execute_cpu(input, result);
78 }
79}
80
82 const Result &input_result,
83 const InputDescriptor &input_descriptor)
84{
85 if (input_descriptor.skip_type_conversion) {
86 return nullptr;
87 }
88
89 const ResultType result_type = input_result.type();
90 const ResultType expected_type = input_descriptor.type;
91 if (result_type != expected_type) {
92 return new ConversionOperation(context, result_type, expected_type);
93 }
94 return nullptr;
95}
96
97void ConversionOperation::execute_single(const Result &input, Result &output)
98{
100 conversions.convert_to_initialized_n(
101 GSpan(input.single_value().type(), input.single_value().get(), 1),
102 GMutableSpan(output.single_value().type(), output.single_value().get(), 1));
103}
104
105void ConversionOperation::execute_cpu(const Result &input, Result &output)
106{
108 conversions.convert_to_initialized_n(input.cpu_data(), output.cpu_data_for_write());
109}
110
111} // namespace blender::compositor
#define ELEM(...)
#define output
const CPPType * type() const
const void * get() const
GSpan()=default
void convert_to_initialized_n(GSpan from_span, GMutableSpan to_span) const
bool is_convertible(const CPPType &from_type, const CPPType &to_type) const
gpu::Shader * get_shader(const char *info_name, ResultPrecision precision)
static SimpleOperation * construct_if_needed(Context &context, const Result &input_result, const InputDescriptor &input_descriptor)
ConversionOperation(Context &context, const ResultType input_type, const ResultType expected_type)
Context & context() const
Definition operation.cc:57
const CPPType & get_cpp_type() const
Definition result.cc:482
void unbind_as_texture() const
Definition result.cc:964
static const char * type_name(const ResultType type)
Definition result.cc:424
GPointer single_value() const
Definition result.cc:1234
static ResultType type(gpu::TextureFormat format)
Definition result.cc:337
void bind_as_texture(gpu::Shader *shader, const char *texture_name) const
Definition result.cc:854
void declare_input_descriptor(InputDescriptor descriptor)
const DataTypeConversions & get_implicit_type_conversions()
void compute_dispatch_threads_at_least(gpu::Shader *shader, int2 threads_range, int2 local_size=int2(16))
Definition utilities.cc:235
BLI_INLINE void IMB_colormanagement_get_luminance_coefficients(float r_rgb[3])
void GPU_shader_unbind()
void GPU_shader_uniform_3fv(gpu::Shader *sh, const char *name, const float data[3])
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)