Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ocio_impl_glsl.cc « opencolorio « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0213646345cbb395b0df8f87d37d62e391964d1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/*
 * Adapted from OpenColorIO with this license:
 *
 * Copyright (c) 2003-2010 Sony Pictures Imageworks Inc., et al.
 * All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * * Neither the name of Sony Pictures Imageworks nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Modifications Copyright 2013, Blender Foundation.
 */

#include <limits>
#include <sstream>
#include <string.h>

#ifdef _MSC_VER
#  pragma warning(push)
#  pragma warning(disable : 4251 4275)
#endif
#include <OpenColorIO/OpenColorIO.h>
#ifdef _MSC_VER
#  pragma warning(pop)
#endif

#include "GPU_immediate.h"
#include "GPU_shader.h"
#include "GPU_uniform_buffer.h"

using namespace OCIO_NAMESPACE;

#include "MEM_guardedalloc.h"

#include "ocio_impl.h"

static const int LUT3D_EDGE_SIZE = 64;
static const int LUT3D_TEXTURE_SIZE = sizeof(float) * 3 * LUT3D_EDGE_SIZE * LUT3D_EDGE_SIZE *
                                      LUT3D_EDGE_SIZE;
static const int SHADER_CACHE_SIZE = 4;

#define UBO_BIND_LOC 0

extern "C" char datatoc_gpu_shader_display_transform_glsl[];
extern "C" char datatoc_gpu_shader_display_transform_vertex_glsl[];

/* **** OpenGL drawing routines using GLSL for color space transform ***** */

/* Curve mapping parameters
 *
 * See documentation for OCIO_CurveMappingSettings to get fields descriptions.
 * (this ones pretty much copies stuff from C structure.)
 */
struct OCIO_GLSLCurveMappingParameters {
  float curve_mapping_mintable[4];
  float curve_mapping_range[4];
  float curve_mapping_ext_in_x[4];
  float curve_mapping_ext_in_y[4];
  float curve_mapping_ext_out_x[4];
  float curve_mapping_ext_out_y[4];
  float curve_mapping_first_x[4];
  float curve_mapping_first_y[4];
  float curve_mapping_last_x[4];
  float curve_mapping_last_y[4];
  float curve_mapping_black[4];
  float curve_mapping_bwmul[4];
  int curve_mapping_lut_size;
  int curve_mapping_use_extend_extrapolate;
  int _pad[2];
  /** WARNING: Needs to be 16byte aligned. Used as UBO data. */
};

struct OCIO_GLSLShader {
  /** Cache IDs */
  std::string cacheId;

  struct GPUShader *shader;
  /** Uniform locations. */
  int dither_loc;
  int overlay_loc;
  int predivide_loc;
  int curve_mapping_loc;
  int ubo_bind;
  /** Error checking. */
  bool valid;
};

struct OCIO_GLSLLut3d {
  /** Cache IDs */
  std::string cacheId;
  /** OpenGL Texture handles. NULL if not allocated. */
  GPUTexture *texture;
  GPUTexture *texture_display;
  GPUTexture *texture_dummy;
  /** Error checking. */
  bool valid;
};

struct OCIO_GLSLCurveMappping {
  /** Cache IDs */
  size_t cacheId;
  /** GPU Uniform Buffer handle. 0 if not allocated. */
  GPUUniformBuf *buffer;
  /** OpenGL Texture handles. 0 if not allocated. */
  GPUTexture *texture;
  /** Error checking. */
  bool valid;
};

struct OCIO_GLSLCacheHandle {
  size_t cache_id;
  void *data;
};

struct OCIO_GLSLDrawState {
  /* Shader Cache */
  OCIO_GLSLCacheHandle shader_cache[SHADER_CACHE_SIZE];
  OCIO_GLSLCacheHandle lut3d_cache[SHADER_CACHE_SIZE];
  OCIO_GLSLCacheHandle curvemap_cache[SHADER_CACHE_SIZE];
};

static OCIO_GLSLDrawState *allocateOpenGLState(void)
{
  return (OCIO_GLSLDrawState *)MEM_callocN(sizeof(OCIO_GLSLDrawState), "OCIO OpenGL State struct");
}

/* -------------------------------------------------------------------- */
/** \name Shader
 * \{ */

static void updateGLSLShader(OCIO_GLSLShader *shader,
                             ConstProcessorRcPtr *processor_scene_to_ui,
                             ConstProcessorRcPtr *processpr_ui_to_display,
                             GpuShaderDesc *shader_desc,
                             const std::string &cache_id)
{
  if (shader->cacheId == cache_id) {
    return;
  }

  /* Delete any previous shader. */
  if (shader->shader) {
    GPU_shader_free(shader->shader);
  }

  std::ostringstream os;
  {
    /* Fragment shader */

    /* Work around OpenColorIO not supporting latest GLSL yet. */
    os << "#define texture2D texture\n";
    os << "#define texture3D texture\n";

    shader_desc->setFunctionName("OCIO_to_display_linear_with_look");
    os << (*processor_scene_to_ui)->getGpuShaderText(*shader_desc) << "\n";

    shader_desc->setFunctionName("OCIO_to_display_encoded");
    os << (*processpr_ui_to_display)->getGpuShaderText(*shader_desc) << "\n";

    os << datatoc_gpu_shader_display_transform_glsl;
  }

  shader->shader = GPU_shader_create(datatoc_gpu_shader_display_transform_vertex_glsl,
                                     os.str().c_str(),
                                     NULL,
                                     NULL,
                                     NULL,
                                     "OCIOShader");

  if (shader->shader) {
    shader->dither_loc = GPU_shader_get_uniform(shader->shader, "dither");
    shader->overlay_loc = GPU_shader_get_uniform(shader->shader, "overlay");
    shader->predivide_loc = GPU_shader_get_uniform(shader->shader, "predivide");
    shader->curve_mapping_loc = GPU_shader_get_uniform(shader->shader, "curve_mapping");
    shader->ubo_bind = GPU_shader_get_uniform_block_binding(shader->shader,
                                                            "OCIO_GLSLCurveMappingParameters");

    GPU_shader_bind(shader->shader);

    /* Set texture bind point uniform once. This is saved by the shader. */
    GPUShader *sh = shader->shader;
    GPU_shader_uniform_int(sh, GPU_shader_get_uniform(sh, "image_texture"), 0);
    GPU_shader_uniform_int(sh, GPU_shader_get_uniform(sh, "overlay_texture"), 1);
    GPU_shader_uniform_int(sh, GPU_shader_get_uniform(sh, "lut3d_texture"), 2);
    GPU_shader_uniform_int(sh, GPU_shader_get_uniform(sh, "lut3d_display_texture"), 3);
    GPU_shader_uniform_int(sh, GPU_shader_get_uniform(sh, "curve_mapping_texture"), 4);
  }

  shader->cacheId = cache_id;
  shader->valid = (shader->shader != NULL);
}

static void ensureGLSLShader(OCIO_GLSLShader **shader_ptr,
                             ConstProcessorRcPtr *processor_scene_to_ui,
                             ConstProcessorRcPtr *processpr_ui_to_display,
                             GpuShaderDesc *shader_desc,
                             const std::string &cache_id)
{
  if (*shader_ptr != NULL) {
    return;
  }

  OCIO_GLSLShader *shader = OBJECT_GUARDED_NEW(OCIO_GLSLShader);

  updateGLSLShader(shader, processor_scene_to_ui, processpr_ui_to_display, shader_desc, cache_id);

  *shader_ptr = shader;
}

static void freeGLSLShader(OCIO_GLSLShader *shader)
{
  if (shader->shader) {
    GPU_shader_free(shader->shader);
  }

  OBJECT_GUARDED_DELETE(shader, OCIO_GLSLShader);
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Lut3D
 * \{ */

static void updateGLSLLut3d(OCIO_GLSLLut3d *lut3d,
                            ConstProcessorRcPtr *processor_scene_to_ui,
                            ConstProcessorRcPtr *processpr_ui_to_display,
                            GpuShaderDesc *shader_desc,
                            const std::string &cache_id)
{
  if (lut3d->cacheId == cache_id)
    return;

  float *lut_data = (float *)MEM_mallocN(LUT3D_TEXTURE_SIZE, __func__);

  ConstProcessorRcPtr *ocio_processors[2] = {processor_scene_to_ui, processpr_ui_to_display};

  for (int i = 0; i < 2; i++) {
    ConstProcessorRcPtr *processor = ocio_processors[i];
    GPUTexture *texture = (&lut3d->texture)[i];

    (*processor)->getGpuLut3D(lut_data, *shader_desc);

    int offset[3] = {0, 0, 0};
    int extent[3] = {LUT3D_EDGE_SIZE, LUT3D_EDGE_SIZE, LUT3D_EDGE_SIZE};
    GPU_texture_update_sub(texture, GPU_DATA_FLOAT, lut_data, UNPACK3(offset), UNPACK3(extent));
  }

  MEM_freeN(lut_data);

  lut3d->cacheId = cache_id;
}

static void ensureGLSLLut3d(OCIO_GLSLLut3d **lut3d_ptr,
                            ConstProcessorRcPtr *processor_scene_to_ui,
                            ConstProcessorRcPtr *processpr_ui_to_display,
                            GpuShaderDesc *shaderDesc,
                            const std::string &cache_id)
{
  if (*lut3d_ptr != NULL) {
    return;
  }

  OCIO_GLSLLut3d *lut3d = OBJECT_GUARDED_NEW(OCIO_GLSLLut3d);

  int extent[3] = {LUT3D_EDGE_SIZE, LUT3D_EDGE_SIZE, LUT3D_EDGE_SIZE};

  lut3d->texture = GPU_texture_create_3d(
      "OCIOLut", UNPACK3(extent), 1, GPU_RGB16F, GPU_DATA_FLOAT, NULL);
  GPU_texture_filter_mode(lut3d->texture, true);
  GPU_texture_wrap_mode(lut3d->texture, false, true);

  lut3d->texture_display = GPU_texture_create_3d(
      "OCIOLutDisplay", UNPACK3(extent), 1, GPU_RGB16F, GPU_DATA_FLOAT, NULL);
  GPU_texture_filter_mode(lut3d->texture_display, true);
  GPU_texture_wrap_mode(lut3d->texture_display, false, true);

  lut3d->texture_dummy = GPU_texture_create_error(2, false);

  updateGLSLLut3d(lut3d, processor_scene_to_ui, processpr_ui_to_display, shaderDesc, cache_id);

  lut3d->valid = (lut3d->texture && lut3d->texture_display);

  *lut3d_ptr = lut3d;
}

static void freeGLSLLut3d(OCIO_GLSLLut3d *lut3d)
{
  GPU_texture_free(lut3d->texture);
  GPU_texture_free(lut3d->texture_display);
  GPU_texture_free(lut3d->texture_dummy);

  OBJECT_GUARDED_DELETE(lut3d, OCIO_GLSLLut3d);
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Curve Mapping
 * \{ */
static void allocateCurveMappingTexture(OCIO_GLSLCurveMappping *curvemap,
                                        OCIO_CurveMappingSettings *curve_mapping_settings)
{
  int lut_size = curve_mapping_settings ? curve_mapping_settings->lut_size : 1;
  /* Do not initialize. Only if used. */
  curvemap->texture = GPU_texture_create_1d("OCIOCurveMap", lut_size, 1, GPU_RGBA16F, NULL);
  GPU_texture_filter_mode(curvemap->texture, false);
  GPU_texture_wrap_mode(curvemap->texture, false, true);
}

/* curve_mapping_settings can be null. In this case we alloc a dummy curvemap. */
static void ensureGLSLCurveMapping(OCIO_GLSLCurveMappping **curvemap_ptr,
                                   OCIO_CurveMappingSettings *curve_mapping_settings)
{
  if (*curvemap_ptr != NULL) {
    return;
  }

  OCIO_GLSLCurveMappping *curvemap = OBJECT_GUARDED_NEW(OCIO_GLSLCurveMappping);

  /* Texture. */
  allocateCurveMappingTexture(curvemap, curve_mapping_settings);

  /* Uniform buffer object. */
  curvemap->buffer = GPU_uniformbuf_create(sizeof(OCIO_GLSLCurveMappingParameters));

  curvemap->valid = (curvemap->texture != 0);
  curvemap->cacheId = 0;

  *curvemap_ptr = curvemap;
}

static void freeGLSLCurveMapping(OCIO_GLSLCurveMappping *curvemap)
{
  GPU_texture_free(curvemap->texture);
  GPU_uniformbuf_free(curvemap->buffer);

  OBJECT_GUARDED_DELETE(curvemap, OCIO_GLSLCurveMappping);
}

static void updateGLSLCurveMapping(OCIO_GLSLCurveMappping *curvemap,
                                   OCIO_CurveMappingSettings *curve_mapping_settings,
                                   size_t cacheId)
{
  /* No need to continue if curvemapping is not used. Just use whatever is in this cache. */
  if (curve_mapping_settings == NULL)
    return;

  if (curvemap->cacheId == cacheId)
    return;

  if (curvemap->cacheId == 0) {
    /* This cache was previously used as dummy. Recreate the texture. */
    GPU_texture_free(curvemap->texture);
    allocateCurveMappingTexture(curvemap, curve_mapping_settings);
  }

  /* Update texture. */
  int offset[3] = {0, 0, 0};
  int extent[3] = {curve_mapping_settings->lut_size, 0, 0};
  const float *pixels = curve_mapping_settings->lut;
  GPU_texture_update_sub(
      curvemap->texture, GPU_DATA_FLOAT, pixels, UNPACK3(offset), UNPACK3(extent));

  /* Update uniforms. */
  OCIO_GLSLCurveMappingParameters data;
  for (int i = 0; i < 4; i++) {
    data.curve_mapping_range[i] = curve_mapping_settings->range[i];
    data.curve_mapping_mintable[i] = curve_mapping_settings->mintable[i];
    data.curve_mapping_ext_in_x[i] = curve_mapping_settings->ext_in_x[i];
    data.curve_mapping_ext_in_y[i] = curve_mapping_settings->ext_in_y[i];
    data.curve_mapping_ext_out_x[i] = curve_mapping_settings->ext_out_x[i];
    data.curve_mapping_ext_out_y[i] = curve_mapping_settings->ext_out_y[i];
    data.curve_mapping_first_x[i] = curve_mapping_settings->first_x[i];
    data.curve_mapping_first_y[i] = curve_mapping_settings->first_y[i];
    data.curve_mapping_last_x[i] = curve_mapping_settings->last_x[i];
    data.curve_mapping_last_y[i] = curve_mapping_settings->last_y[i];
  }
  for (int i = 0; i < 3; i++) {
    data.curve_mapping_black[i] = curve_mapping_settings->black[i];
    data.curve_mapping_bwmul[i] = curve_mapping_settings->bwmul[i];
  }
  data.curve_mapping_lut_size = curve_mapping_settings->lut_size;
  data.curve_mapping_use_extend_extrapolate = curve_mapping_settings->use_extend_extrapolate;

  GPU_uniformbuf_update(curvemap->buffer, &data);

  curvemap->cacheId = cacheId;
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name LRU cache
 * \{ */

static size_t hash_string(const char *str)
{
  size_t i = 0, c;
  while ((c = *str++)) {
    i = i * 37 + c;
  }
  return i;
}

static OCIO_GLSLCacheHandle *cacheSearch(OCIO_GLSLCacheHandle cache[SHADER_CACHE_SIZE],
                                         size_t cache_id)
{
  OCIO_GLSLCacheHandle *cached_item = &cache[0];
  for (int i = 0; i < SHADER_CACHE_SIZE; i++, cached_item++) {
    if (cached_item->data == NULL) {
      continue;
    }
    else if (cached_item->cache_id == cache_id) {
      /* LRU cache, so move to front. */
      OCIO_GLSLCacheHandle found_item = *cached_item;
      for (int j = i; j > 0; j--) {
        cache[j] = cache[j - 1];
      }
      cache[0] = found_item;
      return &cache[0];
    }
  }
  /* LRU cache, shift other items back so we can insert at the front. */
  OCIO_GLSLCacheHandle last_item = cache[SHADER_CACHE_SIZE - 1];
  for (int j = SHADER_CACHE_SIZE - 1; j > 0; j--) {
    cache[j] = cache[j - 1];
  }
  /* Copy last to front and let the caller initialize it. */
  cache[0] = last_item;
  return &cache[0];
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name OCIO GLSL Implementation
 * \{ */

/* Detect if we can support GLSL drawing */
bool OCIOImpl::supportGLSLDraw()
{
  /* Minimum supported version 3.3 does meet all requirements. */
  return true;
}

/**
 * Setup OpenGL contexts for a transform defined by processor using GLSL
 * All LUT allocating baking and shader compilation happens here.
 *
 * Once this function is called, callee could start drawing images
 * using regular 2D texture.
 *
 * When all drawing is finished, finishGLSLDraw shall be called to
 * restore OpenGL context to its pre-GLSL draw state.
 */
bool OCIOImpl::setupGLSLDraw(OCIO_GLSLDrawState **state_r,
                             OCIO_ConstProcessorRcPtr *ocio_processor_scene_to_ui,
                             OCIO_ConstProcessorRcPtr *ocio_processor_ui_to_display,
                             OCIO_CurveMappingSettings *curve_mapping_settings,
                             float dither,
                             bool use_predivide,
                             bool use_overlay)
{
  ConstProcessorRcPtr processor_scene_to_ui = *(ConstProcessorRcPtr *)ocio_processor_scene_to_ui;
  ConstProcessorRcPtr processpr_ui_to_display = *(
      ConstProcessorRcPtr *)ocio_processor_ui_to_display;
  bool use_curve_mapping = curve_mapping_settings != NULL;

  if (!processor_scene_to_ui || !processor_scene_to_ui) {
    return false;
  }

  /* Create state if needed. */
  OCIO_GLSLDrawState *state;
  if (!*state_r)
    *state_r = allocateOpenGLState();
  state = *state_r;

  /* Compute cache IDs. */
  GpuShaderDesc shaderDesc;
  shaderDesc.setLanguage(GPU_LANGUAGE_GLSL_1_3);
  shaderDesc.setFunctionName("OCIODisplay");
  shaderDesc.setLut3DEdgeLen(LUT3D_EDGE_SIZE);

  const char *shader_cache_str = processor_scene_to_ui->getGpuShaderTextCacheID(shaderDesc);
  const char *lut3d_cache_str = processor_scene_to_ui->getGpuLut3DCacheID(shaderDesc);
  /* Used for comparison. */
  std::string shaderCacheID = shader_cache_str;
  std::string lut3dCacheID = lut3d_cache_str;

  size_t shader_cache_id = hash_string(shader_cache_str);
  size_t lut3d_cache_id = hash_string(lut3d_cache_str);
  size_t curvemap_cache_id = curve_mapping_settings ? curve_mapping_settings->cache_id : 0;

  OCIO_GLSLCacheHandle *shader_handle = cacheSearch(state->shader_cache, shader_cache_id);
  OCIO_GLSLCacheHandle *lut3d_handle = cacheSearch(state->lut3d_cache, lut3d_cache_id);
  /* We cannot keep more than one cache for curvemap because their cache id is a pointer.
   * The pointer cannot be the same for one update but can be the same after a second update. */
  OCIO_GLSLCacheHandle *curvemap_handle = &state->curvemap_cache[0];

  OCIO_GLSLShader **shader_ptr = (OCIO_GLSLShader **)&shader_handle->data;
  OCIO_GLSLLut3d **lut3d_ptr = (OCIO_GLSLLut3d **)&lut3d_handle->data;
  OCIO_GLSLCurveMappping **curvemap_ptr = (OCIO_GLSLCurveMappping **)&curvemap_handle->data;

  ensureGLSLShader(
      shader_ptr, &processor_scene_to_ui, &processpr_ui_to_display, &shaderDesc, shaderCacheID);
  ensureGLSLLut3d(
      lut3d_ptr, &processor_scene_to_ui, &processpr_ui_to_display, &shaderDesc, shaderCacheID);
  ensureGLSLCurveMapping(curvemap_ptr, curve_mapping_settings);

  OCIO_GLSLShader *shader = (OCIO_GLSLShader *)shader_handle->data;
  OCIO_GLSLLut3d *shader_lut = (OCIO_GLSLLut3d *)lut3d_handle->data;
  OCIO_GLSLCurveMappping *shader_curvemap = (OCIO_GLSLCurveMappping *)curvemap_handle->data;

  updateGLSLShader(
      shader, &processor_scene_to_ui, &processpr_ui_to_display, &shaderDesc, shaderCacheID);
  updateGLSLLut3d(
      shader_lut, &processor_scene_to_ui, &processpr_ui_to_display, &shaderDesc, lut3dCacheID);
  updateGLSLCurveMapping(shader_curvemap, curve_mapping_settings, curvemap_cache_id);

  /* Update handles cache keys. */
  shader_handle->cache_id = shader_cache_id;
  lut3d_handle->cache_id = lut3d_cache_id;
  curvemap_handle->cache_id = curvemap_cache_id;

  if (shader->valid && shader_lut->valid && shader_curvemap->valid) {
    /* Bind textures to sampler units. Texture 0 is set by caller.
     * Uniforms have already been set for texture bind points.*/

    if (!use_overlay) {
      /* Avoid missing binds. */
      GPU_texture_bind(shader_lut->texture_dummy, 1);
    }
    GPU_texture_bind(shader_lut->texture, 2);
    GPU_texture_bind(shader_lut->texture_display, 3);
    GPU_texture_bind(shader_curvemap->texture, 4);

    /* Bind UBO. */
    GPU_uniformbuf_bind(shader_curvemap->buffer, shader->ubo_bind);

    /* TODO(fclem): remove remains of IMM. */
    immBindShader(shader->shader);

    /* Bind Shader and set uniforms. */
    // GPU_shader_bind(shader->shader);
    GPU_shader_uniform_float(shader->shader, shader->dither_loc, dither);
    GPU_shader_uniform_int(shader->shader, shader->overlay_loc, use_overlay);
    GPU_shader_uniform_int(shader->shader, shader->predivide_loc, use_predivide);
    GPU_shader_uniform_int(shader->shader, shader->curve_mapping_loc, use_curve_mapping);

    return true;
  }

  return false;
}

void OCIOImpl::finishGLSLDraw(OCIO_GLSLDrawState * /*state*/)
{
  immUnbindProgram();
}

void OCIOImpl::freeGLState(OCIO_GLSLDrawState *state)
{
  for (int i = 0; i < SHADER_CACHE_SIZE; i++) {
    if (state->shader_cache[i].data) {
      freeGLSLShader((OCIO_GLSLShader *)state->shader_cache[i].data);
    }
    if (state->lut3d_cache[i].data) {
      freeGLSLLut3d((OCIO_GLSLLut3d *)state->lut3d_cache[i].data);
    }
    if (state->curvemap_cache[i].data) {
      freeGLSLCurveMapping((OCIO_GLSLCurveMappping *)state->curvemap_cache[i].data);
    }
  }

  MEM_freeN(state);
}

/** \} */