mesa: translate into gallium vertex formats in mesa/main
authorMarek Olšák <marek.olsak@amd.com>
Tue, 21 Jan 2020 03:06:30 +0000 (22:06 -0500)
committerMarek Olšák <marek.olsak@amd.com>
Fri, 14 Feb 2020 23:16:27 +0000 (18:16 -0500)
Reviewed-by: Mathias Fröhlich <mathias.froehlich@web.de>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/merge_requests/3829>

src/mesa/main/mtypes.h
src/mesa/main/varray.c
src/mesa/state_tracker/st_atom_array.c

index bcca2b8c3b5a7024ca04fbc33e51792d5d9b132e..0da926e67ffd4cfdb306d1c7a9bc158c64073b98 100644 (file)
@@ -459,6 +459,7 @@ struct gl_vertex_format
 {
    GLenum16 Type;        /**< datatype: GL_FLOAT, GL_INT, etc */
    GLenum16 Format;      /**< default: GL_RGBA, but may be GL_BGRA */
+   enum pipe_format _PipeFormat:16; /**< pipe_format for Gallium */
    GLubyte Size:5;       /**< components per element (1,2,3,4) */
    GLubyte Normalized:1; /**< GL_ARB_vertex_program */
    GLubyte Integer:1;    /**< Integer-valued? */
index bea3e8945abf2d8119db6f75575b122d5b02b67a..7ef0de7734ad31ddb75d713bb1bcb39b6b4a5e1b 100644 (file)
@@ -254,6 +254,259 @@ vertex_binding_divisor(struct gl_context *ctx,
    }
 }
 
+/* vertex_formats[gltype - GL_BYTE][integer*2 + normalized][size - 1] */
+static const uint16_t vertex_formats[][4][4] = {
+   { /* GL_BYTE */
+      {
+         PIPE_FORMAT_R8_SSCALED,
+         PIPE_FORMAT_R8G8_SSCALED,
+         PIPE_FORMAT_R8G8B8_SSCALED,
+         PIPE_FORMAT_R8G8B8A8_SSCALED
+      },
+      {
+         PIPE_FORMAT_R8_SNORM,
+         PIPE_FORMAT_R8G8_SNORM,
+         PIPE_FORMAT_R8G8B8_SNORM,
+         PIPE_FORMAT_R8G8B8A8_SNORM
+      },
+      {
+         PIPE_FORMAT_R8_SINT,
+         PIPE_FORMAT_R8G8_SINT,
+         PIPE_FORMAT_R8G8B8_SINT,
+         PIPE_FORMAT_R8G8B8A8_SINT
+      },
+   },
+   { /* GL_UNSIGNED_BYTE */
+      {
+         PIPE_FORMAT_R8_USCALED,
+         PIPE_FORMAT_R8G8_USCALED,
+         PIPE_FORMAT_R8G8B8_USCALED,
+         PIPE_FORMAT_R8G8B8A8_USCALED
+      },
+      {
+         PIPE_FORMAT_R8_UNORM,
+         PIPE_FORMAT_R8G8_UNORM,
+         PIPE_FORMAT_R8G8B8_UNORM,
+         PIPE_FORMAT_R8G8B8A8_UNORM
+      },
+      {
+         PIPE_FORMAT_R8_UINT,
+         PIPE_FORMAT_R8G8_UINT,
+         PIPE_FORMAT_R8G8B8_UINT,
+         PIPE_FORMAT_R8G8B8A8_UINT
+      },
+   },
+   { /* GL_SHORT */
+      {
+         PIPE_FORMAT_R16_SSCALED,
+         PIPE_FORMAT_R16G16_SSCALED,
+         PIPE_FORMAT_R16G16B16_SSCALED,
+         PIPE_FORMAT_R16G16B16A16_SSCALED
+      },
+      {
+         PIPE_FORMAT_R16_SNORM,
+         PIPE_FORMAT_R16G16_SNORM,
+         PIPE_FORMAT_R16G16B16_SNORM,
+         PIPE_FORMAT_R16G16B16A16_SNORM
+      },
+      {
+         PIPE_FORMAT_R16_SINT,
+         PIPE_FORMAT_R16G16_SINT,
+         PIPE_FORMAT_R16G16B16_SINT,
+         PIPE_FORMAT_R16G16B16A16_SINT
+      },
+   },
+   { /* GL_UNSIGNED_SHORT */
+      {
+         PIPE_FORMAT_R16_USCALED,
+         PIPE_FORMAT_R16G16_USCALED,
+         PIPE_FORMAT_R16G16B16_USCALED,
+         PIPE_FORMAT_R16G16B16A16_USCALED
+      },
+      {
+         PIPE_FORMAT_R16_UNORM,
+         PIPE_FORMAT_R16G16_UNORM,
+         PIPE_FORMAT_R16G16B16_UNORM,
+         PIPE_FORMAT_R16G16B16A16_UNORM
+      },
+      {
+         PIPE_FORMAT_R16_UINT,
+         PIPE_FORMAT_R16G16_UINT,
+         PIPE_FORMAT_R16G16B16_UINT,
+         PIPE_FORMAT_R16G16B16A16_UINT
+      },
+   },
+   { /* GL_INT */
+      {
+         PIPE_FORMAT_R32_SSCALED,
+         PIPE_FORMAT_R32G32_SSCALED,
+         PIPE_FORMAT_R32G32B32_SSCALED,
+         PIPE_FORMAT_R32G32B32A32_SSCALED
+      },
+      {
+         PIPE_FORMAT_R32_SNORM,
+         PIPE_FORMAT_R32G32_SNORM,
+         PIPE_FORMAT_R32G32B32_SNORM,
+         PIPE_FORMAT_R32G32B32A32_SNORM
+      },
+      {
+         PIPE_FORMAT_R32_SINT,
+         PIPE_FORMAT_R32G32_SINT,
+         PIPE_FORMAT_R32G32B32_SINT,
+         PIPE_FORMAT_R32G32B32A32_SINT
+      },
+   },
+   { /* GL_UNSIGNED_INT */
+      {
+         PIPE_FORMAT_R32_USCALED,
+         PIPE_FORMAT_R32G32_USCALED,
+         PIPE_FORMAT_R32G32B32_USCALED,
+         PIPE_FORMAT_R32G32B32A32_USCALED
+      },
+      {
+         PIPE_FORMAT_R32_UNORM,
+         PIPE_FORMAT_R32G32_UNORM,
+         PIPE_FORMAT_R32G32B32_UNORM,
+         PIPE_FORMAT_R32G32B32A32_UNORM
+      },
+      {
+         PIPE_FORMAT_R32_UINT,
+         PIPE_FORMAT_R32G32_UINT,
+         PIPE_FORMAT_R32G32B32_UINT,
+         PIPE_FORMAT_R32G32B32A32_UINT
+      },
+   },
+   { /* GL_FLOAT */
+      {
+         PIPE_FORMAT_R32_FLOAT,
+         PIPE_FORMAT_R32G32_FLOAT,
+         PIPE_FORMAT_R32G32B32_FLOAT,
+         PIPE_FORMAT_R32G32B32A32_FLOAT
+      },
+      {
+         PIPE_FORMAT_R32_FLOAT,
+         PIPE_FORMAT_R32G32_FLOAT,
+         PIPE_FORMAT_R32G32B32_FLOAT,
+         PIPE_FORMAT_R32G32B32A32_FLOAT
+      },
+   },
+   {{0}}, /* GL_2_BYTES */
+   {{0}}, /* GL_3_BYTES */
+   {{0}}, /* GL_4_BYTES */
+   { /* GL_DOUBLE */
+      {
+         PIPE_FORMAT_R64_FLOAT,
+         PIPE_FORMAT_R64G64_FLOAT,
+         PIPE_FORMAT_R64G64B64_FLOAT,
+         PIPE_FORMAT_R64G64B64A64_FLOAT
+      },
+      {
+         PIPE_FORMAT_R64_FLOAT,
+         PIPE_FORMAT_R64G64_FLOAT,
+         PIPE_FORMAT_R64G64B64_FLOAT,
+         PIPE_FORMAT_R64G64B64A64_FLOAT
+      },
+   },
+   { /* GL_HALF_FLOAT */
+      {
+         PIPE_FORMAT_R16_FLOAT,
+         PIPE_FORMAT_R16G16_FLOAT,
+         PIPE_FORMAT_R16G16B16_FLOAT,
+         PIPE_FORMAT_R16G16B16A16_FLOAT
+      },
+      {
+         PIPE_FORMAT_R16_FLOAT,
+         PIPE_FORMAT_R16G16_FLOAT,
+         PIPE_FORMAT_R16G16B16_FLOAT,
+         PIPE_FORMAT_R16G16B16A16_FLOAT
+      },
+   },
+   { /* GL_FIXED */
+      {
+         PIPE_FORMAT_R32_FIXED,
+         PIPE_FORMAT_R32G32_FIXED,
+         PIPE_FORMAT_R32G32B32_FIXED,
+         PIPE_FORMAT_R32G32B32A32_FIXED
+      },
+      {
+         PIPE_FORMAT_R32_FIXED,
+         PIPE_FORMAT_R32G32_FIXED,
+         PIPE_FORMAT_R32G32B32_FIXED,
+         PIPE_FORMAT_R32G32B32A32_FIXED
+      },
+   },
+};
+
+/**
+ * Return a PIPE_FORMAT_x for the given GL datatype and size.
+ */
+static enum pipe_format
+vertex_format_to_pipe_format(GLubyte size, GLenum16 type, GLenum16 format,
+                             GLboolean normalized, GLboolean integer,
+                             GLboolean doubles)
+{
+   assert(size >= 1 && size <= 4);
+   assert(format == GL_RGBA || format == GL_BGRA);
+
+   /* 64-bit attributes are translated by drivers. */
+   if (doubles)
+      return PIPE_FORMAT_NONE;
+
+   switch (type) {
+   case GL_HALF_FLOAT_OES:
+      type = GL_HALF_FLOAT;
+      break;
+
+   case GL_INT_2_10_10_10_REV:
+      assert(size == 4 && !integer);
+
+      if (format == GL_BGRA) {
+         if (normalized)
+            return PIPE_FORMAT_B10G10R10A2_SNORM;
+         else
+            return PIPE_FORMAT_B10G10R10A2_SSCALED;
+      } else {
+         if (normalized)
+            return PIPE_FORMAT_R10G10B10A2_SNORM;
+         else
+            return PIPE_FORMAT_R10G10B10A2_SSCALED;
+      }
+      break;
+
+   case GL_UNSIGNED_INT_2_10_10_10_REV:
+      assert(size == 4 && !integer);
+
+      if (format == GL_BGRA) {
+         if (normalized)
+            return PIPE_FORMAT_B10G10R10A2_UNORM;
+         else
+            return PIPE_FORMAT_B10G10R10A2_USCALED;
+      } else {
+         if (normalized)
+            return PIPE_FORMAT_R10G10B10A2_UNORM;
+         else
+            return PIPE_FORMAT_R10G10B10A2_USCALED;
+      }
+      break;
+
+   case GL_UNSIGNED_INT_10F_11F_11F_REV:
+      assert(size == 3 && !integer && format == GL_RGBA);
+      return PIPE_FORMAT_R11G11B10_FLOAT;
+
+   case GL_UNSIGNED_BYTE:
+      if (format == GL_BGRA) {
+         /* this is an odd-ball case */
+         assert(normalized);
+         return PIPE_FORMAT_B8G8R8A8_UNORM;
+      }
+      break;
+   }
+
+   unsigned index = integer*2 + normalized;
+   assert(index <= 2);
+   assert(type >= GL_BYTE && type <= GL_FIXED);
+   return vertex_formats[type - GL_BYTE][index][size-1];
+}
 
 void
 _mesa_set_vertex_format(struct gl_vertex_format *vertex_format,
@@ -270,6 +523,9 @@ _mesa_set_vertex_format(struct gl_vertex_format *vertex_format,
    vertex_format->Doubles = doubles;
    vertex_format->_ElementSize = _mesa_bytes_per_vertex_attrib(size, type);
    assert(vertex_format->_ElementSize <= 4*sizeof(double));
+   vertex_format->_PipeFormat =
+      vertex_format_to_pipe_format(size, type, format, normalized, integer,
+                                   doubles);
 }
 
 
index 5c258ed99566ceeaebb135f2b300fcae3971b8be..be1729311e2e4bd4f083c3bf65a3d38ee3f1ef4d 100644 (file)
 #include "main/varray.h"
 #include "main/arrayobj.h"
 
-/* vertex_formats[gltype - GL_BYTE][integer*2 + normalized][size - 1] */
-static const uint16_t vertex_formats[][4][4] = {
-   { /* GL_BYTE */
-      {
-         PIPE_FORMAT_R8_SSCALED,
-         PIPE_FORMAT_R8G8_SSCALED,
-         PIPE_FORMAT_R8G8B8_SSCALED,
-         PIPE_FORMAT_R8G8B8A8_SSCALED
-      },
-      {
-         PIPE_FORMAT_R8_SNORM,
-         PIPE_FORMAT_R8G8_SNORM,
-         PIPE_FORMAT_R8G8B8_SNORM,
-         PIPE_FORMAT_R8G8B8A8_SNORM
-      },
-      {
-         PIPE_FORMAT_R8_SINT,
-         PIPE_FORMAT_R8G8_SINT,
-         PIPE_FORMAT_R8G8B8_SINT,
-         PIPE_FORMAT_R8G8B8A8_SINT
-      },
-   },
-   { /* GL_UNSIGNED_BYTE */
-      {
-         PIPE_FORMAT_R8_USCALED,
-         PIPE_FORMAT_R8G8_USCALED,
-         PIPE_FORMAT_R8G8B8_USCALED,
-         PIPE_FORMAT_R8G8B8A8_USCALED
-      },
-      {
-         PIPE_FORMAT_R8_UNORM,
-         PIPE_FORMAT_R8G8_UNORM,
-         PIPE_FORMAT_R8G8B8_UNORM,
-         PIPE_FORMAT_R8G8B8A8_UNORM
-      },
-      {
-         PIPE_FORMAT_R8_UINT,
-         PIPE_FORMAT_R8G8_UINT,
-         PIPE_FORMAT_R8G8B8_UINT,
-         PIPE_FORMAT_R8G8B8A8_UINT
-      },
-   },
-   { /* GL_SHORT */
-      {
-         PIPE_FORMAT_R16_SSCALED,
-         PIPE_FORMAT_R16G16_SSCALED,
-         PIPE_FORMAT_R16G16B16_SSCALED,
-         PIPE_FORMAT_R16G16B16A16_SSCALED
-      },
-      {
-         PIPE_FORMAT_R16_SNORM,
-         PIPE_FORMAT_R16G16_SNORM,
-         PIPE_FORMAT_R16G16B16_SNORM,
-         PIPE_FORMAT_R16G16B16A16_SNORM
-      },
-      {
-         PIPE_FORMAT_R16_SINT,
-         PIPE_FORMAT_R16G16_SINT,
-         PIPE_FORMAT_R16G16B16_SINT,
-         PIPE_FORMAT_R16G16B16A16_SINT
-      },
-   },
-   { /* GL_UNSIGNED_SHORT */
-      {
-         PIPE_FORMAT_R16_USCALED,
-         PIPE_FORMAT_R16G16_USCALED,
-         PIPE_FORMAT_R16G16B16_USCALED,
-         PIPE_FORMAT_R16G16B16A16_USCALED
-      },
-      {
-         PIPE_FORMAT_R16_UNORM,
-         PIPE_FORMAT_R16G16_UNORM,
-         PIPE_FORMAT_R16G16B16_UNORM,
-         PIPE_FORMAT_R16G16B16A16_UNORM
-      },
-      {
-         PIPE_FORMAT_R16_UINT,
-         PIPE_FORMAT_R16G16_UINT,
-         PIPE_FORMAT_R16G16B16_UINT,
-         PIPE_FORMAT_R16G16B16A16_UINT
-      },
-   },
-   { /* GL_INT */
-      {
-         PIPE_FORMAT_R32_SSCALED,
-         PIPE_FORMAT_R32G32_SSCALED,
-         PIPE_FORMAT_R32G32B32_SSCALED,
-         PIPE_FORMAT_R32G32B32A32_SSCALED
-      },
-      {
-         PIPE_FORMAT_R32_SNORM,
-         PIPE_FORMAT_R32G32_SNORM,
-         PIPE_FORMAT_R32G32B32_SNORM,
-         PIPE_FORMAT_R32G32B32A32_SNORM
-      },
-      {
-         PIPE_FORMAT_R32_SINT,
-         PIPE_FORMAT_R32G32_SINT,
-         PIPE_FORMAT_R32G32B32_SINT,
-         PIPE_FORMAT_R32G32B32A32_SINT
-      },
-   },
-   { /* GL_UNSIGNED_INT */
-      {
-         PIPE_FORMAT_R32_USCALED,
-         PIPE_FORMAT_R32G32_USCALED,
-         PIPE_FORMAT_R32G32B32_USCALED,
-         PIPE_FORMAT_R32G32B32A32_USCALED
-      },
-      {
-         PIPE_FORMAT_R32_UNORM,
-         PIPE_FORMAT_R32G32_UNORM,
-         PIPE_FORMAT_R32G32B32_UNORM,
-         PIPE_FORMAT_R32G32B32A32_UNORM
-      },
-      {
-         PIPE_FORMAT_R32_UINT,
-         PIPE_FORMAT_R32G32_UINT,
-         PIPE_FORMAT_R32G32B32_UINT,
-         PIPE_FORMAT_R32G32B32A32_UINT
-      },
-   },
-   { /* GL_FLOAT */
-      {
-         PIPE_FORMAT_R32_FLOAT,
-         PIPE_FORMAT_R32G32_FLOAT,
-         PIPE_FORMAT_R32G32B32_FLOAT,
-         PIPE_FORMAT_R32G32B32A32_FLOAT
-      },
-      {
-         PIPE_FORMAT_R32_FLOAT,
-         PIPE_FORMAT_R32G32_FLOAT,
-         PIPE_FORMAT_R32G32B32_FLOAT,
-         PIPE_FORMAT_R32G32B32A32_FLOAT
-      },
-   },
-   {{0}}, /* GL_2_BYTES */
-   {{0}}, /* GL_3_BYTES */
-   {{0}}, /* GL_4_BYTES */
-   { /* GL_DOUBLE */
-      {
-         PIPE_FORMAT_R64_FLOAT,
-         PIPE_FORMAT_R64G64_FLOAT,
-         PIPE_FORMAT_R64G64B64_FLOAT,
-         PIPE_FORMAT_R64G64B64A64_FLOAT
-      },
-      {
-         PIPE_FORMAT_R64_FLOAT,
-         PIPE_FORMAT_R64G64_FLOAT,
-         PIPE_FORMAT_R64G64B64_FLOAT,
-         PIPE_FORMAT_R64G64B64A64_FLOAT
-      },
-   },
-   { /* GL_HALF_FLOAT */
-      {
-         PIPE_FORMAT_R16_FLOAT,
-         PIPE_FORMAT_R16G16_FLOAT,
-         PIPE_FORMAT_R16G16B16_FLOAT,
-         PIPE_FORMAT_R16G16B16A16_FLOAT
-      },
-      {
-         PIPE_FORMAT_R16_FLOAT,
-         PIPE_FORMAT_R16G16_FLOAT,
-         PIPE_FORMAT_R16G16B16_FLOAT,
-         PIPE_FORMAT_R16G16B16A16_FLOAT
-      },
-   },
-   { /* GL_FIXED */
-      {
-         PIPE_FORMAT_R32_FIXED,
-         PIPE_FORMAT_R32G32_FIXED,
-         PIPE_FORMAT_R32G32B32_FIXED,
-         PIPE_FORMAT_R32G32B32A32_FIXED
-      },
-      {
-         PIPE_FORMAT_R32_FIXED,
-         PIPE_FORMAT_R32G32_FIXED,
-         PIPE_FORMAT_R32G32B32_FIXED,
-         PIPE_FORMAT_R32G32B32A32_FIXED
-      },
-   },
-};
-
-
-/**
- * Return a PIPE_FORMAT_x for the given GL datatype and size.
- */
-static enum pipe_format
-st_pipe_vertex_format(const struct gl_vertex_format *vformat)
-{
-   const GLubyte size = vformat->Size;
-   const GLenum16 format = vformat->Format;
-   const bool normalized = vformat->Normalized;
-   const bool integer = vformat->Integer;
-   GLenum16 type = vformat->Type;
-   unsigned index;
-
-   assert(size >= 1 && size <= 4);
-   assert(format == GL_RGBA || format == GL_BGRA);
-   assert(vformat->_ElementSize == _mesa_bytes_per_vertex_attrib(size, type));
-
-   switch (type) {
-   case GL_HALF_FLOAT_OES:
-      type = GL_HALF_FLOAT;
-      break;
-
-   case GL_INT_2_10_10_10_REV:
-      assert(size == 4 && !integer);
-
-      if (format == GL_BGRA) {
-         if (normalized)
-            return PIPE_FORMAT_B10G10R10A2_SNORM;
-         else
-            return PIPE_FORMAT_B10G10R10A2_SSCALED;
-      } else {
-         if (normalized)
-            return PIPE_FORMAT_R10G10B10A2_SNORM;
-         else
-            return PIPE_FORMAT_R10G10B10A2_SSCALED;
-      }
-      break;
-
-   case GL_UNSIGNED_INT_2_10_10_10_REV:
-      assert(size == 4 && !integer);
-
-      if (format == GL_BGRA) {
-         if (normalized)
-            return PIPE_FORMAT_B10G10R10A2_UNORM;
-         else
-            return PIPE_FORMAT_B10G10R10A2_USCALED;
-      } else {
-         if (normalized)
-            return PIPE_FORMAT_R10G10B10A2_UNORM;
-         else
-            return PIPE_FORMAT_R10G10B10A2_USCALED;
-      }
-      break;
-
-   case GL_UNSIGNED_INT_10F_11F_11F_REV:
-      assert(size == 3 && !integer && format == GL_RGBA);
-      return PIPE_FORMAT_R11G11B10_FLOAT;
-
-   case GL_UNSIGNED_BYTE:
-      if (format == GL_BGRA) {
-         /* this is an odd-ball case */
-         assert(normalized);
-         return PIPE_FORMAT_B8G8R8A8_UNORM;
-      }
-      break;
-   }
-
-   index = integer*2 + normalized;
-   assert(index <= 2);
-   assert(type >= GL_BYTE && type <= GL_FIXED);
-   return vertex_formats[type - GL_BYTE][index][size-1];
-}
-
 static void init_velement(struct pipe_vertex_element *velement,
                           int src_offset, int format,
                           int instance_divisor, int vbo_index)
@@ -357,10 +100,8 @@ static void init_velement_lowered(const struct st_vertex_program *vp,
          }
       }
    } else {
-      const unsigned format = st_pipe_vertex_format(vformat);
-
-      init_velement(&velements[idx], src_offset,
-                    format, instance_divisor, vbo_index);
+      init_velement(&velements[idx], src_offset, vformat->_PipeFormat,
+                    instance_divisor, vbo_index);
    }
 }