memoryformat: Add gdk_memory_format_get_depth()
authorBenjamin Otte <otte@redhat.com>
Thu, 15 Jun 2023 18:02:46 +0000 (20:02 +0200)
committerBenjamin Otte <otte@redhat.com>
Sun, 18 Jun 2023 12:26:18 +0000 (14:26 +0200)
Replace gdk_memory_format_prefers_high_depth with the more generic
gdk_memory_format_get_depth() that returns the depth of the individual
channels.

Also make the GL renderer use that to pick the generic F16 format
instead of immediately going for F32 when uploading textures.

gdk/gdkmemoryformat.c
gdk/gdkmemoryformatprivate.h
gsk/gl/gskglcommandqueue.c
gsk/gskrendernodeimpl.c

index 191bb22db287522eab8e23f21026432852973404..58b42f01c4806e8234c967f9ecdf6f33a67d9d50 100644 (file)
@@ -258,7 +258,7 @@ struct _GdkMemoryFormatDescription
   GdkMemoryAlpha alpha;
   gsize bytes_per_pixel;
   gsize alignment;
-  gboolean prefers_high_depth;
+  GdkMemoryDepth depth;
   struct {
     guint gl_major;
     guint gl_minor;
@@ -289,7 +289,7 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_PREMULTIPLIED,
     4,
     G_ALIGNOF (guchar),
-    FALSE,
+    GDK_MEMORY_U8,
     { 0, 0, G_MAXUINT, G_MAXUINT },
     { GL_RGBA8, GL_BGRA, GL_UNSIGNED_BYTE, { GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA } },
     b8g8r8a8_premultiplied_to_float,
@@ -299,7 +299,7 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_PREMULTIPLIED,
     4,
     G_ALIGNOF (guchar),
-    FALSE,
+    GDK_MEMORY_U8,
     { 0, 0, G_MAXUINT, G_MAXUINT },
     { GL_RGBA8, GL_BGRA, GDK_GL_UNSIGNED_BYTE_FLIPPED, { GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA } },
     a8r8g8b8_premultiplied_to_float,
@@ -309,7 +309,7 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_PREMULTIPLIED,
     4,
     G_ALIGNOF (guchar),
-    FALSE,
+    GDK_MEMORY_U8,
     { 0, 0, 0, 0 },
     { GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, { GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA } },
     r8g8b8a8_premultiplied_to_float,
@@ -319,7 +319,7 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_STRAIGHT,
     4,
     G_ALIGNOF (guchar),
-    FALSE,
+    GDK_MEMORY_U8,
     { 0, 0, G_MAXUINT, G_MAXUINT },
     { GL_RGBA8, GL_BGRA, GL_UNSIGNED_BYTE, { GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA } },
     b8g8r8a8_to_float,
@@ -329,7 +329,7 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_STRAIGHT,
     4,
     G_ALIGNOF (guchar),
-    FALSE,
+    GDK_MEMORY_U8,
     { 0, 0, G_MAXUINT, G_MAXUINT },
     { GL_RGBA8, GL_RGBA, GDK_GL_UNSIGNED_BYTE_FLIPPED, { GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA } },
     a8r8g8b8_to_float,
@@ -339,7 +339,7 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_STRAIGHT,
     4,
     G_ALIGNOF (guchar),
-    FALSE,
+    GDK_MEMORY_U8,
     { 0, 0, 0, 0 },
     { GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, { GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA } },
     r8g8b8a8_to_float,
@@ -349,7 +349,7 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_STRAIGHT,
     4,
     G_ALIGNOF (guchar),
-    FALSE,
+    GDK_MEMORY_U8,
     { 0, 0, G_MAXUINT, G_MAXUINT },
     { GL_RGBA8, GL_BGRA, GDK_GL_UNSIGNED_BYTE_FLIPPED, { GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA } },
     a8b8g8r8_to_float,
@@ -359,7 +359,7 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_OPAQUE,
     3,
     G_ALIGNOF (guchar),
-    FALSE,
+    GDK_MEMORY_U8,
     { 0, 0, 0, 0 },
     { GL_RGB8, GL_RGB, GL_UNSIGNED_BYTE, { GL_RED, GL_GREEN, GL_BLUE, GL_ONE } },
     r8g8b8_to_float,
@@ -369,7 +369,7 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_OPAQUE,
     3,
     G_ALIGNOF (guchar),
-    FALSE,
+    GDK_MEMORY_U8,
     { 0, 0, G_MAXUINT, G_MAXUINT },
     { GL_RGB8, GL_BGR, GL_UNSIGNED_BYTE, { GL_RED, GL_GREEN, GL_BLUE, GL_ONE } },
     b8g8r8_to_float,
@@ -379,7 +379,7 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_OPAQUE,
     6,
     G_ALIGNOF (guint16),
-    TRUE,
+    GDK_MEMORY_U16,
     { 0, 0, 3, 0 },
     { GL_RGB16, GL_RGB, GL_UNSIGNED_SHORT, { GL_RED, GL_GREEN, GL_BLUE, GL_ONE } },
     r16g16b16_to_float,
@@ -389,7 +389,7 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_PREMULTIPLIED,
     8,
     G_ALIGNOF (guint16),
-    TRUE,
+    GDK_MEMORY_U16,
     { 0, 0, 3, 0 },
     { GL_RGBA16, GL_RGBA, GL_UNSIGNED_SHORT, { GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA } },
     r16g16b16a16_to_float,
@@ -399,7 +399,7 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_STRAIGHT,
     8,
     G_ALIGNOF (guint16),
-    TRUE,
+    GDK_MEMORY_U16,
     { 0, 0, 3, 0 },
     { GL_RGBA16, GL_RGBA, GL_UNSIGNED_SHORT, { GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA } },
     r16g16b16a16_to_float,
@@ -409,7 +409,7 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_OPAQUE,
     6,
     G_ALIGNOF (guint16),
-    TRUE,
+    GDK_MEMORY_FLOAT16,
     { 0, 0, 3, 0 },
     { GL_RGB16F, GL_RGB, GL_HALF_FLOAT, { GL_RED, GL_GREEN, GL_BLUE, GL_ONE } },
     r16g16b16_float_to_float,
@@ -419,7 +419,7 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_PREMULTIPLIED,
     8,
     G_ALIGNOF (guint16),
-    TRUE,
+    GDK_MEMORY_FLOAT16,
     { 0, 0, 3, 0 },
     { GL_RGBA16F, GL_RGBA, GL_HALF_FLOAT, { GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA } },
     r16g16b16a16_float_to_float,
@@ -429,7 +429,7 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_STRAIGHT,
     8,
     G_ALIGNOF (guint16),
-    TRUE,
+    GDK_MEMORY_FLOAT16,
     { 0, 0, 3, 0 },
     { GL_RGBA16F, GL_RGBA, GL_HALF_FLOAT, { GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA } },
     r16g16b16a16_float_to_float,
@@ -439,7 +439,7 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_OPAQUE,
     12,
     G_ALIGNOF (float),
-    TRUE,
+    GDK_MEMORY_FLOAT32,
     { 0, 0, 3, 0 },
     { GL_RGB32F, GL_RGB, GL_FLOAT, { GL_RED, GL_GREEN, GL_BLUE, GL_ONE } },
     r32g32b32_float_to_float,
@@ -459,7 +459,7 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_STRAIGHT,
     16,
     G_ALIGNOF (float),
-    TRUE,
+    GDK_MEMORY_FLOAT32,
     { 0, 0, 3, 0 },
     { GL_RGBA32F, GL_RGBA, GL_FLOAT, { GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA } },
     r32g32b32a32_float_to_float,
@@ -469,7 +469,7 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_PREMULTIPLIED,
     2,
     G_ALIGNOF (guchar),
-    FALSE,
+    GDK_MEMORY_U8,
     { 0, 0, 3, 0 },
     { GL_RG8, GL_RG, GL_UNSIGNED_BYTE, { GL_RED, GL_RED, GL_RED, GL_GREEN } },
     g8a8_premultiplied_to_float,
@@ -479,7 +479,7 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_STRAIGHT,
     2,
     G_ALIGNOF (guchar),
-    FALSE,
+    GDK_MEMORY_U8,
     { 0, 0, 3, 0 },
     { GL_RG8, GL_RG, GL_UNSIGNED_BYTE, { GL_RED, GL_RED, GL_RED, GL_GREEN } },
     g8a8_to_float,
@@ -489,7 +489,7 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_OPAQUE,
     1,
     G_ALIGNOF (guchar),
-    FALSE,
+    GDK_MEMORY_U8,
     { 0, 0, 3, 0 },
     { GL_R8, GL_RED, GL_UNSIGNED_BYTE, { GL_RED, GL_RED, GL_RED, GL_ONE } },
     g8_to_float,
@@ -499,7 +499,7 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_PREMULTIPLIED,
     4,
     G_ALIGNOF (guint16),
-    TRUE,
+    GDK_MEMORY_U16,
     { 0, 0, 3, 0 },
     { GL_RG16, GL_RG, GL_UNSIGNED_SHORT, { GL_RED, GL_RED, GL_RED, GL_GREEN } },
     g16a16_premultiplied_to_float,
@@ -509,7 +509,7 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_STRAIGHT,
     4,
     G_ALIGNOF (guint16),
-    TRUE,
+    GDK_MEMORY_U16,
     { 0, 0, 3, 0 },
     { GL_RG16, GL_RG, GL_UNSIGNED_SHORT, { GL_RED, GL_RED, GL_RED, GL_GREEN } },
     g16a16_to_float,
@@ -519,7 +519,7 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_OPAQUE,
     2,
     G_ALIGNOF (guint16),
-    TRUE,
+    GDK_MEMORY_U16,
     { 0, 0, 3, 0 },
     { GL_R16, GL_RED, GL_UNSIGNED_SHORT, { GL_RED, GL_RED, GL_RED, GL_ONE } },
     g16_to_float,
@@ -529,7 +529,7 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_STRAIGHT,
     1,
     G_ALIGNOF (guchar),
-    FALSE,
+    GDK_MEMORY_U8,
     { 0, 0, 3, 0 },
     { GL_R8, GL_RED, GL_UNSIGNED_BYTE, { GL_ONE, GL_ONE, GL_ONE, GL_RED } },
     a8_to_float,
@@ -539,7 +539,7 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_STRAIGHT,
     2,
     G_ALIGNOF (guint16),
-    TRUE,
+    GDK_MEMORY_U16,
     { 0, 0, 3, 0 },
     { GL_R16, GL_RED, GL_UNSIGNED_SHORT, { GL_ONE, GL_ONE, GL_ONE, GL_RED } },
     a16_to_float,
@@ -566,23 +566,22 @@ gdk_memory_format_alignment (GdkMemoryFormat format)
 }
 
 /*<private>
- * gdk_memory_format_prefers_high_depth:
+ * gdk_memory_format_get_depth:
  * @format: a memory format
  *
- * Checks if the given format benefits from being rendered
- * in bit depths higher than 8bits per pixel. See
- * gsk_render_node_prefers_high_depth() for more information
- * on this.
- * Usually this is the case when
- * gdk_memory_format_bytes_per_pixel() is larger than 4.
+ * Gets the depth of the individual channels of the format.
+ * See gsk_render_node_prefers_high_depth() for more
+ * information on this.
+ *
+ * Usually renderers want to use higher depth for render
+ * targets to match these formats.
  *
- * Returns: %TRUE if the format benefits from being
- *     composited in hgiher bit depths.
+ * Returns: The depth of this format
  **/
-gboolean
-gdk_memory_format_prefers_high_depth (GdkMemoryFormat format)
+GdkMemoryDepth
+gdk_memory_format_get_depth (GdkMemoryFormat format)
 {
-  return memory_formats[format].prefers_high_depth;
+  return memory_formats[format].depth;
 }
 
 gboolean
index 5f2b4c06ad8446e2fdcefdfd3e08d590d1bc9cd3..df1fa6033a85b60e6777be17612eb4639ad45254 100644 (file)
@@ -31,10 +31,17 @@ typedef enum {
   GDK_MEMORY_ALPHA_OPAQUE
 } GdkMemoryAlpha;
 
+typedef enum {
+  GDK_MEMORY_U8,
+  GDK_MEMORY_U16,
+  GDK_MEMORY_FLOAT16,
+  GDK_MEMORY_FLOAT32
+} GdkMemoryDepth;
+
 gsize                   gdk_memory_format_alignment         (GdkMemoryFormat             format) G_GNUC_CONST;
 GdkMemoryAlpha          gdk_memory_format_alpha             (GdkMemoryFormat             format) G_GNUC_CONST;
 gsize                   gdk_memory_format_bytes_per_pixel   (GdkMemoryFormat             format) G_GNUC_CONST;
-gboolean                gdk_memory_format_prefers_high_depth(GdkMemoryFormat             format) G_GNUC_CONST;
+GdkMemoryDepth          gdk_memory_format_get_depth         (GdkMemoryFormat             format) G_GNUC_CONST;
 gboolean                gdk_memory_format_gl_format         (GdkMemoryFormat             format,
                                                              gboolean                    gles,
                                                              guint                       gl_major,
index 645f32f7d996f6fb8b1b4a89e27b62e8eac206ff..4dd222b5d0b58aa0c5ba629c0c35cd3220433b7f 100644 (file)
@@ -1458,6 +1458,9 @@ memory_format_gl_format (GdkMemoryFormat  data_format,
                          guint           *gl_type,
                          GLint          (*gl_swizzle)[4])
 {
+  GdkMemoryDepth depth;
+
+  /* First, try the format itself */
   if (gdk_memory_format_gl_format (data_format,
                                    use_es,
                                    major,
@@ -1469,7 +1472,48 @@ memory_format_gl_format (GdkMemoryFormat  data_format,
       gdk_memory_format_alpha (data_format) != GDK_MEMORY_ALPHA_STRAIGHT)
     return data_format;
 
-  if (gdk_memory_format_prefers_high_depth (data_format))
+  depth = gdk_memory_format_get_depth (data_format);
+
+  /* Next, try the generic format for the given bit depth */
+  switch (depth)
+    {
+      case GDK_MEMORY_FLOAT16:
+        data_format = GDK_MEMORY_R16G16B16A16_FLOAT_PREMULTIPLIED;
+        if (gdk_memory_format_gl_format (data_format,
+                                         use_es,
+                                         major,
+                                         minor,
+                                         gl_internalformat,
+                                         gl_format,
+                                         gl_type,
+                                         gl_swizzle))
+          return data_format;
+        break;
+
+      case GDK_MEMORY_U16:
+        data_format = GDK_MEMORY_R16G16B16A16_PREMULTIPLIED;
+        if (gdk_memory_format_gl_format (data_format,
+                                         use_es,
+                                         major,
+                                         minor,
+                                         gl_internalformat,
+                                         gl_format,
+                                         gl_type,
+                                         gl_swizzle))
+          return data_format;
+        break;
+
+      case GDK_MEMORY_FLOAT32:
+      case GDK_MEMORY_U8:
+        break;
+
+      default:
+        g_assert_not_reached ();
+        break;
+    }
+
+  /* If the format is high depth, also try float32 */
+  if (depth != GDK_MEMORY_U8)
     {
       data_format = GDK_MEMORY_R32G32B32A32_FLOAT_PREMULTIPLIED;
       if (gdk_memory_format_gl_format (data_format,
@@ -1483,6 +1527,7 @@ memory_format_gl_format (GdkMemoryFormat  data_format,
         return data_format;
     }
 
+  /* If all else fails, pick the one format that's always supported */
   data_format = GDK_MEMORY_R8G8B8A8_PREMULTIPLIED;
   if (!gdk_memory_format_gl_format (data_format,
                                     use_es,
index 964d1f81128d81b08bb8a9be997767a01ad3890c..8d2e498419b691f954e740ddcef490e694b89daa 100644 (file)
@@ -1734,7 +1734,7 @@ gsk_texture_node_new (GdkTexture            *texture,
   self->texture = g_object_ref (texture);
   graphene_rect_init_from_rect (&node->bounds, bounds);
 
-  node->prefers_high_depth = gdk_memory_format_prefers_high_depth (gdk_texture_get_format (texture));
+  node->prefers_high_depth = gdk_memory_format_get_depth (gdk_texture_get_format (texture)) != GDK_MEMORY_U8;
 
   return node;
 }
@@ -1960,7 +1960,7 @@ gsk_texture_scale_node_new (GdkTexture            *texture,
   graphene_rect_init_from_rect (&node->bounds, bounds);
   self->filter = filter;
 
-  node->prefers_high_depth = gdk_memory_format_prefers_high_depth (gdk_texture_get_format (texture));
+  node->prefers_high_depth = gdk_memory_format_get_depth (gdk_texture_get_format (texture)) != GDK_MEMORY_U8;
 
   return node;
 }