[gtk/gamma-shenanigans: 5/7] Support 32-bit floating point textures




commit eaf350c034ea4156f00596f9b39445aa802a63a2
Author: Matthias Clasen <mclasen redhat com>
Date:   Tue Sep 7 12:44:08 2021 -0400

    Support 32-bit floating point textures
    
    Add support for a R32G32B32_FLOAT format
    which is using floats.
    
    The intention is to use this for HDR content.
    
    Not done here: Update memory texture tests
    to include floating point textures.

 gdk/gdkglcontext.c            |  7 +++++++
 gdk/gdkgltexture.c            | 13 +++++++++++++
 gdk/gdkmemorytexture.c        | 39 +++++++++++++++++++++++++++++++++++++--
 gdk/gdkmemorytexture.h        |  3 +++
 testsuite/gdk/memorytexture.c |  3 ++-
 5 files changed, 62 insertions(+), 3 deletions(-)
---
diff --git a/gdk/gdkglcontext.c b/gdk/gdkglcontext.c
index 774d33780c..267b0303cc 100644
--- a/gdk/gdkglcontext.c
+++ b/gdk/gdkglcontext.c
@@ -292,6 +292,13 @@ gdk_gl_context_upload_texture (GdkGLContext    *context,
           gl_type = GL_HALF_FLOAT;
           bpp = 6;
         }
+      else if (data_format == GDK_MEMORY_R32G32B32_FLOAT)
+        {
+          gl_internalformat = GL_RGB32F;
+          gl_format = GL_RGB;
+          gl_type = GL_FLOAT;
+          bpp = 12;
+        }
       else /* Fall-back, convert to cairo-surface-format */
         {
           copy = g_malloc (width * height * 4);
diff --git a/gdk/gdkgltexture.c b/gdk/gdkgltexture.c
index 075c4cd583..704b39cb17 100644
--- a/gdk/gdkgltexture.c
+++ b/gdk/gdkgltexture.c
@@ -140,6 +140,19 @@ gdk_gl_texture_download_format (GdkTexture      *texture,
       glReadPixels (0, 0, texture->width, texture->height,
                     GL_RGBA16, GL_UNSIGNED_SHORT, data);
 
+      return g_bytes_new_take (data, size);
+    }
+  else if (format == GDK_MEMORY_R32G32B32_FLOAT)
+    {
+      if (format != GL_RGB32F)
+        return NULL;
+
+      size = texture->width * texture->height * 3 * sizeof (float);
+      data = malloc (size);
+
+      glReadPixels (0, 0, texture->width, texture->height,
+                    GL_RGBA32F, GL_FLOAT, data);
+
       return g_bytes_new_take (data, size);
     }
   else
diff --git a/gdk/gdkmemorytexture.c b/gdk/gdkmemorytexture.c
index b70da86962..0672504b9e 100644
--- a/gdk/gdkmemorytexture.c
+++ b/gdk/gdkmemorytexture.c
@@ -69,6 +69,9 @@ gdk_memory_format_bytes_per_pixel (GdkMemoryFormat format)
     case GDK_MEMORY_R16G16B16_FLOAT:
       return 6;
 
+    case GDK_MEMORY_R32G32B32_FLOAT:
+      return 12;
+
     case GDK_MEMORY_N_FORMATS:
     default:
       g_assert_not_reached ();
@@ -168,7 +171,7 @@ gdk_memory_texture_new (int              width,
   return GDK_TEXTURE (self);
 }
 
-GdkMemoryFormat 
+GdkMemoryFormat
 gdk_memory_texture_get_format (GdkMemoryTexture *self)
 {
   return self->format;
@@ -367,6 +370,37 @@ SWIZZLE_FP16_OPAQUE(3,2,1,0)
 SWIZZLE_FP16_OPAQUE(0,1,2,3)
 SWIZZLE_FP16_OPAQUE(3,0,1,2)
 
+#define SWIZZLE_FLOAT_OPAQUE(A,R,G,B) \
+static void \
+convert_float_swizzle_opaque_ ## A ## R ## G ## B (guchar       *dest_data, \
+                                                   gsize         dest_stride, \
+                                                   const guchar *src_data, \
+                                                   gsize         src_stride, \
+                                                   gsize         width, \
+                                                   gsize         height) \
+{ \
+  gsize x, y; \
+\
+  for (y = 0; y < height; y++) \
+    { \
+      float *src = (float *)src_data; \
+      for (x = 0; x < width; x++) \
+        { \
+          dest_data[4 * x + A] = 255; \
+          dest_data[4 * x + R] = (guchar)(255 * src[3 * x + 0]); \
+          dest_data[4 * x + G] = (guchar)(255 * src[3 * x + 1]); \
+          dest_data[4 * x + B] = (guchar)(255 * src[3 * x + 2]); \
+        } \
+\
+      dest_data += dest_stride; \
+      src_data += src_stride; \
+    } \
+}
+
+SWIZZLE_FLOAT_OPAQUE(3,2,1,0)
+SWIZZLE_FLOAT_OPAQUE(0,1,2,3)
+SWIZZLE_FLOAT_OPAQUE(3,0,1,2)
+
 typedef void (* ConversionFunc) (guchar       *dest_data,
                                  gsize         dest_stride,
                                  const guchar *src_data,
@@ -386,7 +420,8 @@ static ConversionFunc converters[GDK_MEMORY_N_FORMATS][3] =
   { convert_swizzle_opaque_3210, convert_swizzle_opaque_0123, convert_swizzle_opaque_3012 },
   { convert_swizzle_opaque_3012, convert_swizzle_opaque_0321, convert_swizzle_opaque_3210 },
   { convert_16to8_swizzle_2103, convert_16to8_swizzle_1230, convert_16to8_swizzle_0123 },
-  { convert_fp16_swizzle_opaque_3210, convert_fp16_swizzle_opaque_0123, convert_fp16_swizzle_opaque_3012 }
+  { convert_fp16_swizzle_opaque_3210, convert_fp16_swizzle_opaque_0123, convert_fp16_swizzle_opaque_3012 },
+  { convert_float_swizzle_opaque_3210, convert_float_swizzle_opaque_0123, convert_float_swizzle_opaque_3012 }
 };
 
 void
diff --git a/gdk/gdkmemorytexture.h b/gdk/gdkmemorytexture.h
index cb00fe2c37..f2a4dc1620 100644
--- a/gdk/gdkmemorytexture.h
+++ b/gdk/gdkmemorytexture.h
@@ -47,6 +47,8 @@ G_BEGIN_DECLS
  *   Since 4.6
  * @GDK_MEMORY_B16G16R16_FLOAT: 3 half-float values; for blue, green, red.
  *   The data is opaque. Since 4.6
+ * @GDK_MEMORY_B32G32R32_FLOAT: 3 float values; for blue, green, red.
+ *   The data is opaque. Since 4.6
  * @GDK_MEMORY_N_FORMATS: The number of formats. This value will change as
  *   more formats get added, so do not rely on its concrete integer.
  *
@@ -76,6 +78,7 @@ typedef enum {
   GDK_MEMORY_B8G8R8,
   GDK_MEMORY_R16G16B16A16_PREMULTIPLIED,
   GDK_MEMORY_R16G16B16_FLOAT,
+  GDK_MEMORY_R32G32B32_FLOAT,
 
   GDK_MEMORY_N_FORMATS
 } GdkMemoryFormat;
diff --git a/testsuite/gdk/memorytexture.c b/testsuite/gdk/memorytexture.c
index 099cbacdf1..92f9d8f472 100644
--- a/testsuite/gdk/memorytexture.c
+++ b/testsuite/gdk/memorytexture.c
@@ -189,7 +189,8 @@ main (int argc, char *argv[])
 
   for (format = 0; format < GDK_MEMORY_N_FORMATS; format++)
     {
-      if (format == GDK_MEMORY_R16G16B16_FLOAT)
+      if (format == GDK_MEMORY_R16G16B16_FLOAT ||
+          format == GDK_MEMORY_R32G32B32_FLOAT)
         continue;
 
       for (color = 0; color < N_COLORS; color++)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]