[Bf-blender-cvs] [0eb26e349d7] tmp-ocio-v2: OpenColorIO: update processors and transforms for version 2.0

Brecht Van Lommel noreply at git.blender.org
Mon Feb 1 15:49:16 CET 2021


Commit: 0eb26e349d7e671c1a293932b168ee753458a19e
Author: Brecht Van Lommel
Date:   Sun Jan 31 19:48:12 2021 +0100
Branches: tmp-ocio-v2
https://developer.blender.org/rB0eb26e349d7e671c1a293932b168ee753458a19e

OpenColorIO: update processors and transforms for version 2.0

CPU processors now need to be created to do CPU processing. These are cached
internally, but the cache lookup is not fast enough to execute per pixel or
texture sample, so for performance these are now also exposed in the C API.

The C API for transform will no longer be needed afer all changes, so remove
it to simplify the API and fallback implementation.

===================================================================

M	intern/cycles/render/colorspace.cpp
M	intern/cycles/render/shader.cpp
M	intern/opencolorio/fallback_impl.cc
M	intern/opencolorio/ocio_capi.cc
M	intern/opencolorio/ocio_capi.h
M	intern/opencolorio/ocio_impl.cc
M	intern/opencolorio/ocio_impl.h
M	source/blender/imbuf/intern/IMB_colormanagement_intern.h
M	source/blender/imbuf/intern/colormanagement.c

===================================================================

diff --git a/intern/cycles/render/colorspace.cpp b/intern/cycles/render/colorspace.cpp
index 57979d5f225..cf7cbaa9137 100644
--- a/intern/cycles/render/colorspace.cpp
+++ b/intern/cycles/render/colorspace.cpp
@@ -192,6 +192,7 @@ void ColorSpaceManager::is_builtin_colorspace(ustring colorspace,
     return;
   }
 
+  OCIO::ConstCPUProcessorRcPtr device_processor = processor->getDefaultCPUProcessor();
   is_scene_linear = true;
   is_srgb = true;
   for (int i = 0; i < 256; i++) {
@@ -201,10 +202,10 @@ void ColorSpaceManager::is_builtin_colorspace(ustring colorspace,
     float cG[3] = {0, v, 0};
     float cB[3] = {0, 0, v};
     float cW[3] = {v, v, v};
-    processor->applyRGB(cR);
-    processor->applyRGB(cG);
-    processor->applyRGB(cB);
-    processor->applyRGB(cW);
+    device_processor->applyRGB(cR);
+    device_processor->applyRGB(cG);
+    device_processor->applyRGB(cB);
+    device_processor->applyRGB(cW);
 
     /* Make sure that there is no channel crosstalk. */
     if (fabsf(cR[1]) > 1e-5f || fabsf(cR[2]) > 1e-5f || fabsf(cG[0]) > 1e-5f ||
@@ -267,6 +268,7 @@ inline void processor_apply_pixels(const OCIO::Processor *processor, T *pixels,
   /* TODO: implement faster version for when we know the conversion
    * is a simple matrix transform between linear spaces. In that case
    * un-premultiply is not needed. */
+  OCIO::ConstCPUProcessorRcPtr device_processor = processor->getDefaultCPUProcessor();
 
   /* Process large images in chunks to keep temporary memory requirement down. */
   const size_t chunk_size = std::min((size_t)(16 * 1024 * 1024), num_pixels);
@@ -289,7 +291,7 @@ inline void processor_apply_pixels(const OCIO::Processor *processor, T *pixels,
     }
 
     OCIO::PackedImageDesc desc((float *)float_pixels.data(), width, 1, 4);
-    processor->apply(desc);
+    device_processor->apply(desc);
 
     for (size_t i = 0; i < width; i++) {
       float4 value = float_pixels[i];
@@ -345,13 +347,14 @@ void ColorSpaceManager::to_scene_linear(ColorSpaceProcessor *processor_,
   const OCIO::Processor *processor = (const OCIO::Processor *)processor_;
 
   if (processor) {
+    OCIO::ConstCPUProcessorRcPtr device_processor = processor->getDefaultCPUProcessor();
     if (channels == 3) {
-      processor->applyRGB(pixel);
+      device_processor->applyRGB(pixel);
     }
     else if (channels == 4) {
       if (pixel[3] == 1.0f || pixel[3] == 0.0f) {
         /* Fast path for RGBA. */
-        processor->applyRGB(pixel);
+        device_processor->applyRGB(pixel);
       }
       else {
         /* Un-associate and associate alpha since color management should not
@@ -363,7 +366,7 @@ void ColorSpaceManager::to_scene_linear(ColorSpaceProcessor *processor_,
         pixel[1] *= inv_alpha;
         pixel[2] *= inv_alpha;
 
-        processor->applyRGB(pixel);
+        device_processor->applyRGB(pixel);
 
         pixel[0] *= alpha;
         pixel[1] *= alpha;
diff --git a/intern/cycles/render/shader.cpp b/intern/cycles/render/shader.cpp
index 332599be708..736eb79b09b 100644
--- a/intern/cycles/render/shader.cpp
+++ b/intern/cycles/render/shader.cpp
@@ -411,20 +411,24 @@ ShaderManager::ShaderManager()
       OCIO::ConstProcessorRcPtr to_rgb_processor = config->getProcessor("XYZ", "scene_linear");
       OCIO::ConstProcessorRcPtr to_xyz_processor = config->getProcessor("scene_linear", "XYZ");
       if (to_rgb_processor && to_xyz_processor) {
+        OCIO::ConstCPUProcessorRcPtr to_xyz_device_processor =
+            to_xyz_processor->getDefaultCPUProcessor();
+        OCIO::ConstCPUProcessorRcPtr to_rgb_device_processor =
+            to_rgb_processor->getDefaultCPUProcessor();
         float r[] = {1.0f, 0.0f, 0.0f};
         float g[] = {0.0f, 1.0f, 0.0f};
         float b[] = {0.0f, 0.0f, 1.0f};
-        to_xyz_processor->applyRGB(r);
-        to_xyz_processor->applyRGB(g);
-        to_xyz_processor->applyRGB(b);
+        to_xyz_device_processor->applyRGB(r);
+        to_xyz_device_processor->applyRGB(g);
+        to_xyz_device_processor->applyRGB(b);
         rgb_to_y = make_float3(r[1], g[1], b[1]);
 
         float x[] = {1.0f, 0.0f, 0.0f};
         float y[] = {0.0f, 1.0f, 0.0f};
         float z[] = {0.0f, 0.0f, 1.0f};
-        to_rgb_processor->applyRGB(x);
-        to_rgb_processor->applyRGB(y);
-        to_rgb_processor->applyRGB(z);
+        to_rgb_device_processor->applyRGB(x);
+        to_rgb_device_processor->applyRGB(y);
+        to_rgb_device_processor->applyRGB(z);
         xyz_to_r = make_float3(x[0], y[0], z[0]);
         xyz_to_g = make_float3(x[1], y[1], z[1]);
         xyz_to_b = make_float3(x[2], y[2], z[2]);
diff --git a/intern/opencolorio/fallback_impl.cc b/intern/opencolorio/fallback_impl.cc
index a6b93ac4959..dc5cc222505 100644
--- a/intern/opencolorio/fallback_impl.cc
+++ b/intern/opencolorio/fallback_impl.cc
@@ -34,7 +34,7 @@ using std::max;
 enum TransformType {
   TRANSFORM_LINEAR_TO_SRGB,
   TRANSFORM_SRGB_TO_LINEAR,
-  TRANSFORM_MATRIX,
+  TRANSFORM_SCALE,
   TRANSFORM_EXPONENT,
   TRANSFORM_UNKNOWN,
 };
@@ -53,129 +53,64 @@ typedef struct OCIO_PackedImageDescription {
 } OCIO_PackedImageDescription;
 
 struct FallbackTransform {
-  FallbackTransform() : type(TRANSFORM_UNKNOWN), linear_transform(NULL), display_transform(NULL)
+  FallbackTransform() : type(TRANSFORM_UNKNOWN), scale(1.0f), exponent(1.0f)
   {
   }
 
   virtual ~FallbackTransform()
   {
-    delete linear_transform;
-    delete display_transform;
   }
 
   void applyRGB(float *pixel)
   {
     if (type == TRANSFORM_LINEAR_TO_SRGB) {
-      applyLinearRGB(pixel);
+      pixel[0] *= scale;
+      pixel[1] *= scale;
+      pixel[2] *= scale;
+
       linearrgb_to_srgb_v3_v3(pixel, pixel);
-      applyDisplayRGB(pixel);
+
+      pixel[0] = powf(max(0.0f, pixel[0]), exponent);
+      pixel[1] = powf(max(0.0f, pixel[1]), exponent);
+      pixel[2] = powf(max(0.0f, pixel[2]), exponent);
     }
     else if (type == TRANSFORM_SRGB_TO_LINEAR) {
       srgb_to_linearrgb_v3_v3(pixel, pixel);
     }
     else if (type == TRANSFORM_EXPONENT) {
-      pixel[0] = powf(max(0.0f, pixel[0]), exponent[0]);
-      pixel[1] = powf(max(0.0f, pixel[1]), exponent[1]);
-      pixel[2] = powf(max(0.0f, pixel[2]), exponent[2]);
+      pixel[0] = powf(max(0.0f, pixel[0]), exponent);
+      pixel[1] = powf(max(0.0f, pixel[1]), exponent);
+      pixel[2] = powf(max(0.0f, pixel[2]), exponent);
     }
-    else if (type == TRANSFORM_MATRIX) {
-      float r = pixel[0];
-      float g = pixel[1];
-      float b = pixel[2];
-      pixel[0] = r * matrix[0] + g * matrix[1] + b * matrix[2];
-      pixel[1] = r * matrix[4] + g * matrix[5] + b * matrix[6];
-      pixel[2] = r * matrix[8] + g * matrix[9] + b * matrix[10];
-      pixel[0] += offset[0];
-      pixel[1] += offset[1];
-      pixel[2] += offset[2];
+    else if (type == TRANSFORM_SCALE) {
+      pixel[0] *= scale;
+      pixel[1] *= scale;
+      pixel[2] *= scale;
     }
   }
 
   void applyRGBA(float *pixel)
   {
-    if (type == TRANSFORM_LINEAR_TO_SRGB) {
-      applyLinearRGBA(pixel);
-      linearrgb_to_srgb_v4(pixel, pixel);
-      applyDisplayRGBA(pixel);
-    }
-    else if (type == TRANSFORM_SRGB_TO_LINEAR) {
-      srgb_to_linearrgb_v4(pixel, pixel);
-    }
-    else if (type == TRANSFORM_EXPONENT) {
-      pixel[0] = powf(max(0.0f, pixel[0]), exponent[0]);
-      pixel[1] = powf(max(0.0f, pixel[1]), exponent[1]);
-      pixel[2] = powf(max(0.0f, pixel[2]), exponent[2]);
-      pixel[3] = powf(max(0.0f, pixel[3]), exponent[3]);
-    }
-    else if (type == TRANSFORM_MATRIX) {
-      float r = pixel[0];
-      float g = pixel[1];
-      float b = pixel[2];
-      float a = pixel[3];
-      pixel[0] = r * matrix[0] + g * matrix[1] + b * matrix[2] + a * matrix[3];
-      pixel[1] = r * matrix[4] + g * matrix[5] + b * matrix[6] + a * matrix[7];
-      pixel[2] = r * matrix[8] + g * matrix[9] + b * matrix[10] + a * matrix[11];
-      pixel[3] = r * matrix[12] + g * matrix[13] + b * matrix[14] + a * matrix[15];
-      pixel[0] += offset[0];
-      pixel[1] += offset[1];
-      pixel[2] += offset[2];
-      pixel[3] += offset[3];
-    }
-  }
-
-  void applyLinearRGB(float *pixel)
-  {
-    if (linear_transform != NULL) {
-      linear_transform->applyRGB(pixel);
-    }
-  }
-
-  void applyLinearRGBA(float *pixel)
-  {
-    if (linear_transform != NULL) {
-      linear_transform->applyRGBA(pixel);
-    }
-  }
-
-  void applyDisplayRGB(float *pixel)
-  {
-    if (display_transform != NULL) {
-      display_transform->applyRGB(pixel);
-    }
-  }
-
-  void applyDisplayRGBA(float *pixel)
-  {
-    if (display_transform != NULL) {
-      display_transform->applyRGBA(pixel);
-    }
+    applyRGB(pixel);
   }
 
   TransformType type;
-  FallbackTransform *linear_transform;
-  FallbackTransform *display_transform;
+  /* Scale transform. */
+  float scale;
   /* Exponent transform. */
-  float exponent[4];
-  /* Matrix transform. */
-  float matrix[16];
-  float offset[4];
+  float exponent;
 
   MEM_CXX_CLASS_ALLOC_FUNCS("FallbackTransform");
 };
 
-struct FallbackGroupTransform : FallbackTransform {
-  ~FallbackGroupTransform()
+struct FallbackProcessor {
+  FallbackProcessor(FallbackTransform *transform) : transform(transform)
   {
-    for (auto transform : list) {
-      delete transform;
-    }
   }
-  std::vector<FallbackTransform *> list;
-};
 
-struct FallbackProcessor {
-  FallbackProcessor() : transform(NULL)
+  FallbackProcessor(const FallbackProcessor &other)
   {
+    transform = new FallbackTransform(*other.transform);
   }
 
   ~FallbackProcessor()
@@ -413,20 +348,17 @@ OCIO_ConstProcessorRcPtr *FallbackImpl::configGetProcessorWithNames(OCIO_ConstCo
   else {
     transform->type = TRANSFORM_UNKNOWN;
   }
-  FallbackProcessor *processor = new FallbackProcessor();
-  processor->transform = transform;
-  return (OCIO_ConstProcessorRcPtr *)processor;
+  return (OCIO_ConstProcessorRcPtr *)new FallbackProcessor(transform);
 }
 
-OCIO_ConstProcessorRcPtr *FallbackImpl::configGetProcessor(OCIO_ConstConfigRcPtr * /*config*/,
-                                                           OCIO_ConstTransformRcPtr *transform)
+OCIO_ConstCPUProcessorRcPtr *FallbackImpl::processorGetCPUProcessor(OCIO_ConstProcessorRcP

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list