Blender V5.3
libocio_gpu_shader_binder.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6
7#include "GPU_texture.hh"
8
9#include "error_handling.hh"
10#include "libocio_config.hh"
12#include "libocio_processor.hh"
13
15
16namespace blender::ocio {
17
18namespace {
19
20using namespace OCIO_NAMESPACE;
21
22static ConstProcessorRcPtr create_to_scene_linear_processor(
23 const ConstConfigRcPtr &ocio_config, const internal::GPUDisplayShader &display_shader)
24{
26 ocio_config, display_shader.from_colorspace.c_str(), ROLE_SCENE_LINEAR);
27}
28
29static ConstProcessorRcPtr create_to_display_processor(
30 const LibOCIOConfig &config, const internal::GPUDisplayShader &display_shader)
31{
32 DisplayParameters display_parameters;
33 display_parameters.from_colorspace = ROLE_SCENE_LINEAR;
34 display_parameters.view = display_shader.view;
35 display_parameters.display = display_shader.display;
36 display_parameters.look = display_shader.look;
37 display_parameters.use_hdr_buffer = display_shader.use_hdr_buffer;
38 display_parameters.use_display_emulation = display_shader.use_display_emulation;
39 display_parameters.use_scope_space = display_shader.use_scope_space;
40 return create_ocio_display_processor(config, display_parameters);
41}
42
43static bool add_gpu_uniform(internal::GPUTextures &textures,
44 const GpuShaderDescRcPtr &shader_desc,
45 const int index)
46{
48 uniform.name = shader_desc->getUniform(index, uniform.data);
49 if (uniform.data.m_type == UNIFORM_UNKNOWN) {
50 return false;
51 }
52
53 textures.uniforms.append(uniform);
54 return true;
55}
56
57static bool add_gpu_lut_1D2D(internal::GPUTextures &textures,
58 const GpuShaderDescRcPtr &shader_desc,
59 const int index)
60{
61 const char *texture_name = nullptr;
62 const char *sampler_name = nullptr;
63 uint width = 0;
64 uint height = 0;
65
66 GpuShaderCreator::TextureType channel = GpuShaderCreator::TEXTURE_RGB_CHANNEL;
67 Interpolation interpolation = INTERP_LINEAR;
68
69 /* Always use 2D textures in OpenColorIO 2.3, simpler and same performance. */
70 static_assert(OCIO_VERSION_HEX >= 0x02030000);
71 GpuShaderDesc::TextureDimensions dimensions = GpuShaderDesc::TEXTURE_2D;
72 shader_desc->getTexture(
73 index, texture_name, sampler_name, width, height, channel, dimensions, interpolation);
74
75 const float *values;
76 shader_desc->getTextureValues(index, values);
77 if (texture_name == nullptr || sampler_name == nullptr || width == 0 || height == 0 ||
78 values == nullptr)
79 {
80 return false;
81 }
82
83 gpu::TextureFormat format = (channel == GpuShaderCreator::TEXTURE_RGB_CHANNEL) ?
84 gpu::TextureFormat::SFLOAT_16_16_16 :
86
88 /* There does not appear to be an explicit way to check if a texture is 1D or 2D.
89 * It depends on more than height. So check instead by looking at the source.
90 * The Blender default config does not use 1D textures, but for example
91 * studio-config-v3.0.0_aces-v2.0_ocio-v2.4.ocio needs this code. */
92 std::string sampler1D_name = std::string("sampler1D ") + sampler_name;
93 if (strstr(shader_desc->getShaderText(), sampler1D_name.c_str()) != nullptr) {
94 lut.texture = GPU_texture_create_1d(
95 texture_name, width, 1, format, GPU_TEXTURE_USAGE_SHADER_READ, values);
96 }
97 else {
98 lut.texture = GPU_texture_create_2d(
99 texture_name, width, height, 1, format, GPU_TEXTURE_USAGE_SHADER_READ, values);
100 }
101 if (lut.texture == nullptr) {
102 return false;
103 }
104
105 GPU_texture_filter_mode(lut.texture, interpolation != INTERP_NEAREST);
107
108 lut.sampler_name = sampler_name;
109
110 textures.luts.append(lut);
111
112 return true;
113}
114
115static bool add_gpu_lut_3D(internal::GPUTextures &textures,
116 const GpuShaderDescRcPtr &shader_desc,
117 const int index)
118{
119 const char *texture_name = nullptr;
120 const char *sampler_name = nullptr;
121 uint edgelen = 0;
122 Interpolation interpolation = INTERP_LINEAR;
123 shader_desc->get3DTexture(index, texture_name, sampler_name, edgelen, interpolation);
124
125 const float *values;
126 shader_desc->get3DTextureValues(index, values);
127 if (texture_name == nullptr || sampler_name == nullptr || edgelen == 0 || values == nullptr) {
128 return false;
129 }
130
132 lut.texture = GPU_texture_create_3d(texture_name,
133 edgelen,
134 edgelen,
135 edgelen,
136 1,
137 gpu::TextureFormat::SFLOAT_16_16_16,
139 values);
140 if (lut.texture == nullptr) {
141 return false;
142 }
143
144 GPU_texture_filter_mode(lut.texture, interpolation != INTERP_NEAREST);
146
147 lut.sampler_name = sampler_name;
148
149 textures.luts.append(lut);
150 return true;
151}
152
153static bool create_gpu_textures(internal::GPUTextures &textures,
154 const GpuShaderDescRcPtr &shader_desc)
155{
156 for (int index = 0; index < shader_desc->getNumUniforms(); index++) {
157 if (!add_gpu_uniform(textures, shader_desc, index)) {
158 return false;
159 }
160 }
161 for (int index = 0; index < shader_desc->getNumTextures(); index++) {
162 if (!add_gpu_lut_1D2D(textures, shader_desc, index)) {
163 return false;
164 }
165 }
166 for (int index = 0; index < shader_desc->getNum3DTextures(); index++) {
167 if (!add_gpu_lut_3D(textures, shader_desc, index)) {
168 return false;
169 }
170 }
171
172 return true;
173}
174
175} // namespace
176
177void LibOCIOGPUShaderBinder::construct_shader_for_processors(
178 internal::GPUDisplayShader &display_shader,
179 const ConstProcessorRcPtr &processor_to_scene_linear,
180 const ConstProcessorRcPtr &processor_to_display,
181 const Span<std::array<StringRefNull, 2>> additional_defines) const
182{
183 std::string fragment_source;
184
185 GpuShaderDescRcPtr shaderdesc_to_scene_linear;
186 if (processor_to_scene_linear) {
187 shaderdesc_to_scene_linear = GpuShaderDesc::CreateShaderDesc();
188 shaderdesc_to_scene_linear->setLanguage(GPU_LANGUAGE_GLSL_1_3);
189 shaderdesc_to_scene_linear->setFunctionName("OCIO_to_scene_linear");
190 shaderdesc_to_scene_linear->setResourcePrefix("to_scene");
191 processor_to_scene_linear->getDefaultGPUProcessor()->extractGpuShaderInfo(
192 shaderdesc_to_scene_linear);
193 shaderdesc_to_scene_linear->finalize();
194
195 if (!create_gpu_textures(display_shader.textures, shaderdesc_to_scene_linear)) {
196 display_shader.is_valid = false;
197 return;
198 }
199
200 fragment_source += shaderdesc_to_scene_linear->getShaderText();
201 fragment_source += "\n";
202 }
203
204 GpuShaderDescRcPtr shaderdesc_to_display;
205 if (processor_to_display) {
206 shaderdesc_to_display = GpuShaderDesc::CreateShaderDesc();
207 shaderdesc_to_display->setLanguage(GPU_LANGUAGE_GLSL_1_3);
208 shaderdesc_to_display->setFunctionName("OCIO_to_display");
209 shaderdesc_to_display->setResourcePrefix("to_display");
210 processor_to_display->getDefaultGPUProcessor()->extractGpuShaderInfo(shaderdesc_to_display);
211 shaderdesc_to_display->finalize();
212
213 if (!create_gpu_textures(display_shader.textures, shaderdesc_to_display)) {
214 display_shader.is_valid = false;
215 return;
216 }
217
218 fragment_source += shaderdesc_to_display->getShaderText();
219 fragment_source += "\n";
220 }
221
222 if (!create_gpu_shader(display_shader, fragment_source, additional_defines)) {
223 display_shader.is_valid = false;
224 return;
225 }
226
227 display_shader.is_valid = true;
228}
229
231 internal::GPUDisplayShader &display_shader) const
232{
233 const LibOCIOConfig &config = static_cast<const LibOCIOConfig &>(config_);
234 const OCIO_NAMESPACE::ConstConfigRcPtr &ocio_config = config.get_ocio_config();
235
236 ConstProcessorRcPtr processor_to_scene_linear = create_to_scene_linear_processor(ocio_config,
237 display_shader);
238 ConstProcessorRcPtr processor_to_display = create_to_display_processor(config, display_shader);
239
240 if (!processor_to_scene_linear || !processor_to_display) {
241 display_shader.is_valid = false;
242 return;
243 }
244
245 construct_shader_for_processors(
246 display_shader, processor_to_scene_linear, processor_to_display, {});
247}
248
250 internal::GPUDisplayShader &display_shader) const
251{
252 const LibOCIOConfig &config = static_cast<const LibOCIOConfig &>(config_);
253 const OCIO_NAMESPACE::ConstConfigRcPtr &ocio_config = config.get_ocio_config();
254
255 ConstProcessorRcPtr processor_to_scene_linear = create_to_scene_linear_processor(ocio_config,
256 display_shader);
257 if (!processor_to_scene_linear) {
258 display_shader.is_valid = false;
259 return;
260 }
261
262 construct_shader_for_processors(
263 display_shader,
264 processor_to_scene_linear,
265 nullptr,
266 {{"USE_TO_SCENE_LINEAR_ONLY", ""}, {"OUTPUT_PREMULTIPLIED", ""}});
267}
268
269} // namespace blender::ocio
static bool create_gpu_shader(internal::GPUDisplayShader &display_shader, StringRefNull fragment_source, Span< std::array< StringRefNull, 2 > > additional_defines)
const OCIO_NAMESPACE::ConstConfigRcPtr & get_ocio_config() const
void construct_scene_linear_shader(internal::GPUDisplayShader &display_shader) const override
void construct_display_shader(internal::GPUDisplayShader &display_shader) const override
#define uniform(slot)
#define index(i)
OCIO_NAMESPACE::ConstProcessorRcPtr create_ocio_display_processor(const LibOCIOConfig &config, const DisplayParameters &display_parameters)
OCIO_NAMESPACE::ConstProcessorRcPtr create_ocio_processor(const OCIO_NAMESPACE::ConstConfigRcPtr &ocio_config, const StringRefNull from_colorspace, const StringRefNull to_colorspace)
gpu::Texture * GPU_texture_create_1d(const char *name, int width, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
unsigned int uint
Definition BKE_text.h:55
@ GPU_SAMPLER_EXTEND_MODE_EXTEND
@ GPU_TEXTURE_USAGE_SHADER_READ
void GPU_texture_filter_mode(gpu::Texture *texture, bool use_filter)
gpu::Texture * GPU_texture_create_3d(const char *name, int width, int height, int depth, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const void *data)
void GPU_texture_extend_mode(gpu::Texture *texture, GPUSamplerExtendMode extend_mode)
gpu::Texture * GPU_texture_create_2d(const char *name, int width, int height, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
std::string format(const std::string &fmt, Args &&...args)
Definition diagnostic.hh:43