27#if defined(WITH_OPENCOLORIO)
28# include <OpenColorIO/OpenColorIO.h>
58#if defined(WITH_OPENCOLORIO)
60namespace OCIO = OCIO_NAMESPACE;
76class GPUShaderCreator :
public OCIO::GpuShaderCreator {
80 std::shared_ptr<GPUShaderCreator> instance = std::make_shared<GPUShaderCreator>();
81 instance->setLanguage(OCIO::GPU_LANGUAGE_GLSL_4_0);
82 instance->precision_ = precision;
87 OCIO::GpuShaderCreatorRcPtr clone()
const override
89 return OCIO::GpuShaderCreatorRcPtr();
93 void setTextureMaxWidth(
uint )
override {}
95 uint getTextureMaxWidth() const noexcept
override
100# if OCIO_VERSION_HEX >= 0x02030000
101 void setAllowTexture1D(
bool allowed)
override
103 allow_texture_1D_ = allowed;
106 bool getAllowTexture1D()
const override
108 return allow_texture_1D_;
112 bool addUniform(
const char *name,
const DoubleGetter &get_double)
override
116 if (!resource_names_.add(std::make_unique<std::string>(name))) {
123 std::string &resource_name = *resource_names_[resource_names_.size() - 1];
124 shader_create_info_.push_constant(Type::float_t, resource_name);
126 float_uniforms_.add(resource_name, get_double);
131 bool addUniform(
const char *name,
const BoolGetter &get_bool)
override
135 if (!resource_names_.add(std::make_unique<std::string>(name))) {
142 const std::string &resource_name = *resource_names_[resource_names_.size() - 1];
143 shader_create_info_.push_constant(Type::bool_t, resource_name);
145 boolean_uniforms_.add(name, get_bool);
150 bool addUniform(
const char *name,
const Float3Getter &
get_float3)
override
154 if (!resource_names_.add(std::make_unique<std::string>(name))) {
161 std::string &resource_name = *resource_names_[resource_names_.size() - 1];
162 shader_create_info_.push_constant(Type::float3_t, resource_name);
164 vector_uniforms_.add(resource_name,
get_float3);
169 bool addUniform(
const char *name,
170 const SizeGetter &get_size,
171 const VectorFloatGetter &get_vector_float
172#
if OCIO_VERSION_HEX >= 0x02050000
180 if (!resource_names_.add(std::make_unique<std::string>(name))) {
187 std::string &resource_name = *resource_names_[resource_names_.size() - 1];
188 shader_create_info_.uniform_buf(buffers_sizes_.size(),
"float", resource_name);
190 float_buffers_.add(resource_name, get_vector_float);
191 buffers_sizes_.add(resource_name, get_size);
196 bool addUniform(
const char *name,
197 const SizeGetter &get_size,
198 const VectorIntGetter &get_vector_int
199#
if OCIO_VERSION_HEX >= 0x02050000
207 if (!resource_names_.add(std::make_unique<std::string>(name))) {
214 std::string &resource_name = *resource_names_[resource_names_.size() - 1];
215 shader_create_info_.uniform_buf(buffers_sizes_.size(),
"int", resource_name);
217 int_buffers_.add(name, get_vector_int);
218 buffers_sizes_.add(name, get_size);
223# if OCIO_VERSION_HEX >= 0x02050000
228 addTexture(
const char *texture_name,
229 const char *sampler_name,
233#
if OCIO_VERSION_HEX >= 0x02030000
234 OCIO::GpuShaderDesc::TextureDimensions dimensions,
236 OCIO::Interpolation interpolation,
237 const float *values)
override
240 if (!resource_names_.add(std::make_unique<std::string>(sampler_name))) {
246 const std::string &resource_name = *resource_names_[resource_names_.size() - 1];
252# if OCIO_VERSION_HEX >= 0x02030000
253 if (dimensions == OCIO::GpuShaderDesc::TEXTURE_1D)
260 shader_create_info_.sampler(textures_.size() + 1, ImageType::Float1D, resource_name);
265 shader_create_info_.sampler(textures_.size() + 1, ImageType::Float2D, resource_name);
269 textures_.add(sampler_name,
texture);
270# if OCIO_VERSION_HEX >= 0x02050000
271 return textures_.size() - 1;
275# if OCIO_VERSION_HEX >= 0x02050000
280 add3DTexture(
const char *texture_name,
281 const char *sampler_name,
283 OCIO::Interpolation interpolation,
284 const float *values)
override
287 if (!resource_names_.add(std::make_unique<std::string>(sampler_name))) {
293 const std::string &resource_name = *resource_names_[resource_names_.size() - 1];
294 shader_create_info_.sampler(textures_.size() + 1, ImageType::Float3D, resource_name);
306 textures_.add(sampler_name,
texture);
307# if OCIO_VERSION_HEX >= 0x02050000
308 return textures_.size() - 1;
315 void createShaderText(
const char * ,
316#
if OCIO_VERSION_HEX >= 0x02050000
319 const char *helper_methods,
320 const char *function_header,
321 const char *function_body,
322 const char *function_footer)
override
324 shader_code_ += helper_methods;
325 shader_code_ += function_header;
326 shader_code_ += function_body;
327 shader_code_ += function_footer;
333 void finalize()
override
335 GpuShaderCreator::finalize();
337 shader_create_info_.local_group_size(16, 16);
339 shader_create_info_.image(0,
342 ImageReadWriteType::Float2D,
344 shader_create_info_.compute_source(
"gpu_shader_compositor_ocio_processor.glsl");
345 shader_create_info_.compute_source_generated += shader_code_;
347 GPUShaderCreateInfo *info =
reinterpret_cast<GPUShaderCreateInfo *
>(&shader_create_info_);
359 for (
auto item : float_uniforms_.items()) {
363 for (
auto item : boolean_uniforms_.items()) {
367 for (
auto item : vector_uniforms_.items()) {
371 for (
auto item : float_buffers_.items()) {
373 buffers_sizes_.lookup(item.key)(), item.value(), item.key.c_str());
376 uniform_buffers_.append(buffer);
379 for (
auto item : int_buffers_.items()) {
381 buffers_sizes_.lookup(item.key)(), item.value(), item.key.c_str());
384 uniform_buffers_.append(buffer);
387 for (
auto item : textures_.items()) {
397 for (GPUUniformBuf *buffer : uniform_buffers_) {
402 for (GPUTexture *
texture : textures_.values()) {
419 ~GPUShaderCreator()
override
421 for (GPUTexture *
texture : textures_.values()) {
431 GPUShader *shader_ =
nullptr;
432 ShaderCreateInfo shader_create_info_ = ShaderCreateInfo(
"OCIO Processor");
436 std::string shader_code_;
440 Map<std::string, DoubleGetter> float_uniforms_;
441 Map<std::string, BoolGetter> boolean_uniforms_;
442 Map<std::string, Float3Getter> vector_uniforms_;
446 Map<std::string, VectorFloatGetter> float_buffers_;
447 Map<std::string, VectorIntGetter> int_buffers_;
451 Map<std::string, SizeGetter> buffers_sizes_;
455 Map<std::string, GPUTexture *> textures_;
461 VectorSet<std::unique_ptr<std::string>> resource_names_;
467# if OCIO_VERSION_HEX >= 0x02030000
469 bool allow_texture_1D_ =
true;
483 return std::make_shared<GPUShaderCreator>();
518#if defined(WITH_OPENCOLORIO)
521 OCIO::ConstConfigRcPtr config = OCIO::GetCurrentConfig();
522 OCIO::ConstProcessorRcPtr processor = config->getProcessor(source.c_str(), target.c_str());
523 OCIO::ConstGPUProcessorRcPtr gpu_processor = processor->getDefaultGPUProcessor();
525 auto ocio_shader_creator = std::static_pointer_cast<OCIO::GpuShaderCreator>(shader_creator_);
526 gpu_processor->extractGpuShaderInfo(ocio_shader_creator);
528 catch (
const OCIO::Exception &) {
537 return shader_creator_->bind_shader_and_resources();
542 shader_creator_->unbind_shader_and_resources();
547 return shader_creator_->input_sampler_name();
552 return shader_creator_->output_image_name();
562 map_.remove_if([](
auto item) {
return !item.value->needed; });
566 for (
auto &value : map_.values()) {
567 value->needed =
false;
575#if defined(WITH_OPENCOLORIO)
577 std::string config_cache_id = OCIO::GetCurrentConfig()->getCacheID();
579 std::string config_cache_id;
585 return std::make_unique<OCIOColorSpaceConversionShader>(context, source, target);
#define BLI_assert_unreachable()
int GPU_max_texture_size()
int GPU_shader_get_sampler_binding(GPUShader *shader, const char *name)
int GPU_shader_get_ubo_binding(GPUShader *shader, const char *name)
void GPU_shader_uniform_1f(GPUShader *sh, const char *name, float value)
void GPU_shader_bind(GPUShader *shader, const blender::gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_uniform_3fv(GPUShader *sh, const char *name, const float data[3])
void GPU_shader_uniform_1b(GPUShader *sh, const char *name, bool value)
GPUShader * GPU_shader_create_from_info(const GPUShaderCreateInfo *_info)
void GPU_shader_free(GPUShader *shader)
void GPU_texture_bind(GPUTexture *texture, int unit)
GPUTexture * GPU_texture_create_2d(const char *name, int width, int height, int mip_len, eGPUTextureFormat format, eGPUTextureUsage usage, const float *data)
GPUTexture * GPU_texture_create_1d(const char *name, int width, int mip_len, eGPUTextureFormat format, eGPUTextureUsage usage, const float *data)
void GPU_texture_free(GPUTexture *texture)
void GPU_texture_unbind(GPUTexture *texture)
@ GPU_TEXTURE_USAGE_SHADER_READ
GPUTexture * GPU_texture_create_3d(const char *name, int width, int height, int depth, int mip_len, eGPUTextureFormat format, eGPUTextureUsage usage, const void *data)
void GPU_texture_filter_mode(GPUTexture *texture, bool use_filter)
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()
GPUShader * 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()
GPUShader * bind_shader_and_resources()
const char * input_sampler_name()
const char * output_image_name()
OCIOColorSpaceConversionShader(Context &context, std::string source, std::string target)
static eGPUTextureFormat gpu_texture_format(ResultType type, ResultPrecision precision)
static float3 get_float3(const BL::Array< float, 2 > &array)
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)