31#if defined(WITH_OPENCOLORIO)
32# include <OpenColorIO/OpenColorIO.h>
43#if defined(WITH_OPENCOLORIO)
45namespace OCIO = OCIO_NAMESPACE;
61class GPUShaderCreator :
public OCIO::GpuShaderCreator {
65 std::shared_ptr<GPUShaderCreator> instance = std::make_shared<GPUShaderCreator>();
66 instance->setLanguage(OCIO::GPU_LANGUAGE_GLSL_4_0);
67 instance->precision_ = precision;
72 OCIO::GpuShaderCreatorRcPtr clone()
const override
74 return OCIO::GpuShaderCreatorRcPtr();
78 void setTextureMaxWidth(
uint )
override {}
80 uint getTextureMaxWidth() const noexcept
override
85# if OCIO_VERSION_HEX >= 0x02030000
86 void setAllowTexture1D(
bool allowed)
override
88 allow_texture_1D_ = allowed;
91 bool getAllowTexture1D()
const override
93 return allow_texture_1D_;
97 bool addUniform(
const char *
name,
const DoubleGetter &get_double)
override
101 if (!resource_names_.add(std::make_unique<std::string>(
name))) {
108 std::string &resource_name = *resource_names_[resource_names_.size() - 1];
109 shader_create_info_.push_constant(Type::float_t, resource_name);
111 float_uniforms_.add(resource_name, get_double);
116 bool addUniform(
const char *
name,
const BoolGetter &get_bool)
override
120 if (!resource_names_.add(std::make_unique<std::string>(
name))) {
127 const std::string &resource_name = *resource_names_[resource_names_.size() - 1];
128 shader_create_info_.push_constant(Type::bool_t, resource_name);
130 boolean_uniforms_.add(
name, get_bool);
135 bool addUniform(
const char *
name,
const Float3Getter &
get_float3)
override
139 if (!resource_names_.add(std::make_unique<std::string>(
name))) {
146 std::string &resource_name = *resource_names_[resource_names_.size() - 1];
147 shader_create_info_.push_constant(Type::float3_t, resource_name);
149 vector_uniforms_.add(resource_name,
get_float3);
154 bool addUniform(
const char *
name,
155 const SizeGetter &get_size,
156 const VectorFloatGetter &get_vector_float
157#
if OCIO_VERSION_HEX >= 0x02050000
165 if (!resource_names_.add(std::make_unique<std::string>(
name))) {
172 std::string &resource_name = *resource_names_[resource_names_.size() - 1];
173 shader_create_info_.uniform_buf(buffers_sizes_.size(),
"float", resource_name);
175 float_buffers_.add(resource_name, get_vector_float);
176 buffers_sizes_.add(resource_name, get_size);
181 bool addUniform(
const char *
name,
182 const SizeGetter &get_size,
183 const VectorIntGetter &get_vector_int
184#
if OCIO_VERSION_HEX >= 0x02050000
192 if (!resource_names_.add(std::make_unique<std::string>(
name))) {
199 std::string &resource_name = *resource_names_[resource_names_.size() - 1];
200 shader_create_info_.uniform_buf(buffers_sizes_.size(),
"int", resource_name);
202 int_buffers_.add(
name, get_vector_int);
203 buffers_sizes_.add(
name, get_size);
208# if OCIO_VERSION_HEX >= 0x02050000
213 addTexture(
const char *texture_name,
214 const char *sampler_name,
218#
if OCIO_VERSION_HEX >= 0x02030000
219 OCIO::GpuShaderDesc::TextureDimensions dimensions,
221 OCIO::Interpolation interpolation,
222 const float *values)
override
225 if (!resource_names_.add(std::make_unique<std::string>(sampler_name))) {
231 const std::string &resource_name = *resource_names_[resource_names_.size() - 1];
233 blender::gpu::Texture *
texture;
235 (channel == TEXTURE_RGB_CHANNEL) ? blender::gpu::TextureFormat::SFLOAT_32_32_32 :
240# if OCIO_VERSION_HEX >= 0x02030000
241 if (dimensions == OCIO::GpuShaderDesc::TEXTURE_1D)
248 shader_create_info_.sampler(textures_.size() + 1, ImageType::Float1D, resource_name);
253 shader_create_info_.sampler(textures_.size() + 1, ImageType::Float2D, resource_name);
257 textures_.add(sampler_name,
texture);
258# if OCIO_VERSION_HEX >= 0x02050000
259 return textures_.size() - 1;
263# if OCIO_VERSION_HEX >= 0x02050000
268 add3DTexture(
const char *texture_name,
269 const char *sampler_name,
271 OCIO::Interpolation interpolation,
272 const float *values)
override
275 if (!resource_names_.add(std::make_unique<std::string>(sampler_name))) {
281 const std::string &resource_name = *resource_names_[resource_names_.size() - 1];
282 shader_create_info_.sampler(textures_.size() + 1, ImageType::Float3D, resource_name);
295 textures_.add(sampler_name,
texture);
296# if OCIO_VERSION_HEX >= 0x02050000
297 return textures_.size() - 1;
304 void createShaderText(
const char * ,
305#
if OCIO_VERSION_HEX >= 0x02050000
308 const char *helper_methods,
309 const char *function_header,
310 const char *function_body,
311 const char *function_footer)
override
313 shader_code_ += helper_methods;
314 shader_code_ += function_header;
315 shader_code_ += function_body;
316 shader_code_ += function_footer;
322 void finalize()
override
324 GpuShaderCreator::finalize();
326 shader_create_info_.local_group_size(16, 16);
328 shader_create_info_.image(0,
331 ImageReadWriteType::Float2D,
333 shader_create_info_.compute_source(
"gpu_shader_compositor_ocio_processor.glsl");
336 GPUShaderCreateInfo *info =
reinterpret_cast<GPUShaderCreateInfo *
>(&shader_create_info_);
348 for (
auto item : float_uniforms_.items()) {
352 for (
auto item : boolean_uniforms_.items()) {
356 for (
auto item : vector_uniforms_.items()) {
360 for (
auto item : float_buffers_.items()) {
362 buffers_sizes_.lookup(item.key)(), item.value(), item.key.c_str());
365 uniform_buffers_.append(buffer);
368 for (
auto item : int_buffers_.items()) {
370 buffers_sizes_.lookup(item.key)(), item.value(), item.key.c_str());
373 uniform_buffers_.append(buffer);
376 for (
auto item : textures_.items()) {
386 for (gpu::UniformBuf *buffer : uniform_buffers_) {
391 for (blender::gpu::Texture *
texture : textures_.values()) {
408 ~GPUShaderCreator()
override
410 for (blender::gpu::Texture *
texture : textures_.values()) {
420 gpu::Shader *shader_ =
nullptr;
421 ShaderCreateInfo shader_create_info_ = ShaderCreateInfo(
"OCIO Processor");
425 std::string shader_code_;
429 Map<std::string, DoubleGetter> float_uniforms_;
430 Map<std::string, BoolGetter> boolean_uniforms_;
431 Map<std::string, Float3Getter> vector_uniforms_;
435 Map<std::string, VectorFloatGetter> float_buffers_;
436 Map<std::string, VectorIntGetter> int_buffers_;
440 Map<std::string, SizeGetter> buffers_sizes_;
444 Map<std::string, blender::gpu::Texture *> textures_;
450 VectorSet<std::unique_ptr<std::string>> resource_names_;
454 Vector<gpu::UniformBuf *> uniform_buffers_;
456# if OCIO_VERSION_HEX >= 0x02030000
458 bool allow_texture_1D_ =
true;
472 return std::make_shared<GPUShaderCreator>();
528#if defined(WITH_OPENCOLORIO)
531 OCIO::ConstConfigRcPtr config = OCIO::GetCurrentConfig();
532 OCIO::ConstProcessorRcPtr processor = config->getProcessor(source.c_str(), target.c_str());
533 OCIO::ConstGPUProcessorRcPtr gpu_processor = processor->getDefaultGPUProcessor();
535 auto ocio_shader_creator = std::static_pointer_cast<OCIO::GpuShaderCreator>(shader_creator_);
536 gpu_processor->extractGpuShaderInfo(ocio_shader_creator);
538 catch (
const OCIO::Exception &
e) {
539 CLOG_ERROR(&
LOG,
"Failed to create OpenColorIO shader: %s",
e.what());
549 return shader_creator_->bind_shader_and_resources();
554 shader_creator_->unbind_shader_and_resources();
559 return shader_creator_->input_sampler_name();
564 return shader_creator_->output_image_name();
574 map_.remove_if([](
auto item) {
return !item.value->needed; });
578 for (
auto &value : map_.values()) {
579 value->needed =
false;
587#if defined(WITH_OPENCOLORIO)
589 std::string config_cache_id = OCIO::GetCurrentConfig()->getCacheID();
591 std::string config_cache_id;
597 return std::make_unique<OCIOColorSpaceConversionShader>(context, source, target);
646#if defined(WITH_OPENCOLORIO)
650 OCIO::ConstConfigRcPtr config = OCIO::GetCurrentConfig();
652 OCIO::TransformRcPtr group = ocio::create_ocio_display_transform(
660 group->setDirection(OCIO::TRANSFORM_DIR_INVERSE);
663 OCIO::ConstProcessorRcPtr processor = config->getProcessor(group);
664 OCIO::ConstGPUProcessorRcPtr gpu_processor = processor->getDefaultGPUProcessor();
666 auto ocio_shader_creator = std::static_pointer_cast<OCIO::GpuShaderCreator>(shader_creator_);
667 gpu_processor->extractGpuShaderInfo(ocio_shader_creator);
669 catch (
const OCIO::Exception &
e) {
670 CLOG_ERROR(&
LOG,
"Failed to create OpenColorIO shader: %s",
e.what());
679 return shader_creator_->bind_shader_and_resources();
684 shader_creator_->unbind_shader_and_resources();
689 return shader_creator_->input_sampler_name();
694 return shader_creator_->output_image_name();
704 map_.remove_if([](
auto item) {
return !item.value->needed; });
708 for (
auto &value : map_.values()) {
709 value->needed =
false;
719#if defined(WITH_OPENCOLORIO)
721 std::string config_cache_id = OCIO::GetCurrentConfig()->getCacheID();
723 std::string config_cache_id;
729 return std::make_unique<OCIOToDisplayShader>(
730 context, display_settings, view_settings,
inverse);
#define BLI_assert_unreachable()
#define CLOG_ERROR(clg_ref,...)
int GPU_max_texture_size()
int GPU_shader_get_ubo_binding(blender::gpu::Shader *shader, const char *name)
void GPU_shader_uniform_1b(blender::gpu::Shader *sh, const char *name, bool value)
void GPU_shader_uniform_1f(blender::gpu::Shader *sh, const char *name, float value)
void GPU_shader_free(blender::gpu::Shader *shader)
blender::gpu::Shader * GPU_shader_create_from_info(const GPUShaderCreateInfo *_info)
int GPU_shader_get_sampler_binding(blender::gpu::Shader *shader, const char *name)
void GPU_shader_uniform_3fv(blender::gpu::Shader *sh, const char *name, const float data[3])
void GPU_shader_bind(blender::gpu::Shader *shader, const blender::gpu::shader::SpecializationConstants *constants_state=nullptr)
std::string GPU_shader_preprocess_source(blender::StringRefNull original)
void GPU_texture_unbind(blender::gpu::Texture *texture)
@ GPU_TEXTURE_USAGE_SHADER_READ
blender::gpu::Texture * GPU_texture_create_2d(const char *name, int width, int height, int mip_len, blender::gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
void GPU_texture_filter_mode(blender::gpu::Texture *texture, bool use_filter)
void GPU_texture_bind(blender::gpu::Texture *texture, int unit)
void GPU_texture_free(blender::gpu::Texture *texture)
blender::gpu::Texture * GPU_texture_create_3d(const char *name, int width, int height, int depth, int mip_len, blender::gpu::TextureFormat format, eGPUTextureUsage usage, const void *data)
blender::gpu::Texture * GPU_texture_create_1d(const char *name, int width, int mip_len, blender::gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
unsigned long long int uint64_t
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
const char * output_image_name()
const char * input_sampler_name()
void unbind_shader_and_resources()
gpu::Shader * bind_shader_and_resources()
static std::shared_ptr< GPUShaderCreator > Create(ResultPrecision)
OCIOColorSpaceConversionShader & get(Context &context, std::string source, std::string target)
OCIOColorSpaceConversionShaderKey(const std::string &source, const std::string &target, const std::string &config_cache_id)
std::string config_cache_id
void unbind_shader_and_resources()
const char * input_sampler_name()
gpu::Shader * bind_shader_and_resources()
const char * output_image_name()
OCIOColorSpaceConversionShader(Context &context, std::string source, std::string target)
OCIOToDisplayShader & get(Context &context, const ColorManagedDisplaySettings &display_settings, const ColorManagedViewSettings &view_settings, const bool inverse)
std::string view_transform
std::string config_cache_id
std::string display_device
OCIOToDisplayShaderKey(const ColorManagedDisplaySettings &display_settings, const ColorManagedViewSettings &view_settings, const bool inverse, const std::string &config_cache_id)
const char * input_sampler_name()
const char * output_image_name()
void unbind_shader_and_resources()
gpu::Shader * bind_shader_and_resources()
OCIOToDisplayShader(Context &context, const ColorManagedDisplaySettings &display_settings, const ColorManagedViewSettings &view_settings, const bool inverse)
static blender::gpu::TextureFormat gpu_texture_format(ResultType type, ResultPrecision precision)
static float3 get_float3(const BL::Array< float, 2 > &array)
MatBase< C, R > inverse(MatBase< C, R >) RET
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
bool operator==(const BokehKernelKey &a, const BokehKernelKey &b)
uint64_t get_default_hash(const T &v, const Args &...args)