[Mesa-dev] [RFC 04/16] glsl: Add half float type generation

Topi Pohjolainen topi.pohjolainen at intel.com
Fri May 15 02:39:31 PDT 2015


Signed-off-by: Topi Pohjolainen <topi.pohjolainen at intel.com>
---
 src/glsl/builtin_type_macros.h | 16 +++++++++++++++
 src/glsl/builtin_types.cpp     | 31 +++++++++++++++++++++++++++++
 src/glsl/glsl_types.cpp        | 44 +++++++++++++++++++++++++++++++++++++++---
 src/glsl/glsl_types.h          |  6 +++++-
 4 files changed, 93 insertions(+), 4 deletions(-)

diff --git a/src/glsl/builtin_type_macros.h b/src/glsl/builtin_type_macros.h
index 8e16ae4..a20198d 100644
--- a/src/glsl/builtin_type_macros.h
+++ b/src/glsl/builtin_type_macros.h
@@ -48,6 +48,22 @@ DECL_TYPE(uvec2,  GL_UNSIGNED_INT_VEC2, GLSL_TYPE_UINT, 2, 1)
 DECL_TYPE(uvec3,  GL_UNSIGNED_INT_VEC3, GLSL_TYPE_UINT, 3, 1)
 DECL_TYPE(uvec4,  GL_UNSIGNED_INT_VEC4, GLSL_TYPE_UINT, 4, 1)
 
+DECL_TYPE(hfloat,  GL_FLOAT,        GLSL_TYPE_HALF, 1, 1)
+DECL_TYPE(hvec2,   GL_FLOAT_VEC2,   GLSL_TYPE_HALF, 2, 1)
+DECL_TYPE(hvec3,   GL_FLOAT_VEC3,   GLSL_TYPE_HALF, 3, 1)
+DECL_TYPE(hvec4,   GL_FLOAT_VEC4,   GLSL_TYPE_HALF, 4, 1)
+
+DECL_TYPE(hmat2,   GL_FLOAT_MAT2,   GLSL_TYPE_HALF, 2, 2)
+DECL_TYPE(hmat3,   GL_FLOAT_MAT3,   GLSL_TYPE_HALF, 3, 3)
+DECL_TYPE(hmat4,   GL_FLOAT_MAT4,   GLSL_TYPE_HALF, 4, 4)
+
+DECL_TYPE(hmat2x3, GL_FLOAT_MAT2x3, GLSL_TYPE_HALF, 3, 2)
+DECL_TYPE(hmat2x4, GL_FLOAT_MAT2x4, GLSL_TYPE_HALF, 4, 2)
+DECL_TYPE(hmat3x2, GL_FLOAT_MAT3x2, GLSL_TYPE_HALF, 2, 3)
+DECL_TYPE(hmat3x4, GL_FLOAT_MAT3x4, GLSL_TYPE_HALF, 4, 3)
+DECL_TYPE(hmat4x2, GL_FLOAT_MAT4x2, GLSL_TYPE_HALF, 2, 4)
+DECL_TYPE(hmat4x3, GL_FLOAT_MAT4x3, GLSL_TYPE_HALF, 3, 4)
+
 DECL_TYPE(float,  GL_FLOAT,        GLSL_TYPE_FLOAT, 1, 1)
 DECL_TYPE(vec2,   GL_FLOAT_VEC2,   GLSL_TYPE_FLOAT, 2, 1)
 DECL_TYPE(vec3,   GL_FLOAT_VEC3,   GLSL_TYPE_FLOAT, 3, 1)
diff --git a/src/glsl/builtin_types.cpp b/src/glsl/builtin_types.cpp
index d92e2eb..de2c476 100644
--- a/src/glsl/builtin_types.cpp
+++ b/src/glsl/builtin_types.cpp
@@ -145,6 +145,21 @@ const static struct builtin_type_versions {
    T(uvec2,                           130, 300)
    T(uvec3,                           130, 300)
    T(uvec4,                           130, 300)
+
+   T(hfloat,                          110, 100)
+   T(hvec2,                           110, 100)
+   T(hvec3,                           110, 100)
+   T(hvec4,                           110, 100)
+   T(hmat2,                           110, 100)
+   T(hmat3,                           110, 100)
+   T(hmat4,                           110, 100)
+   T(hmat2x3,                         120, 300)
+   T(hmat2x4,                         120, 300)
+   T(hmat3x2,                         120, 300)
+   T(hmat3x4,                         120, 300)
+   T(hmat4x2,                         120, 300)
+   T(hmat4x3,                         120, 300)
+
    T(float,                           110, 100)
    T(vec2,                            110, 100)
    T(vec3,                            110, 100)
@@ -391,5 +406,21 @@ _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state)
       add_type(symbols, glsl_type::dmat4x2_type);
       add_type(symbols, glsl_type::dmat4x3_type);
    }
+
+   if (state->es_shader) {
+      add_type(symbols, glsl_type::hfloat_type);
+      add_type(symbols, glsl_type::hvec2_type);
+      add_type(symbols, glsl_type::hvec3_type);
+      add_type(symbols, glsl_type::hvec4_type);
+      add_type(symbols, glsl_type::hmat2_type);
+      add_type(symbols, glsl_type::hmat3_type);
+      add_type(symbols, glsl_type::hmat4_type);
+      add_type(symbols, glsl_type::hmat2x3_type);
+      add_type(symbols, glsl_type::hmat2x4_type);
+      add_type(symbols, glsl_type::hmat3x2_type);
+      add_type(symbols, glsl_type::hmat3x4_type);
+      add_type(symbols, glsl_type::hmat4x2_type);
+      add_type(symbols, glsl_type::hmat4x3_type);
+   }
 }
 /** @} */
diff --git a/src/glsl/glsl_types.cpp b/src/glsl/glsl_types.cpp
index 51bc19c..647eb4a 100644
--- a/src/glsl/glsl_types.cpp
+++ b/src/glsl/glsl_types.cpp
@@ -283,6 +283,8 @@ const glsl_type *glsl_type::get_base_type() const
       return uint_type;
    case GLSL_TYPE_INT:
       return int_type;
+   case GLSL_TYPE_HALF:
+      return hfloat_type;
    case GLSL_TYPE_FLOAT:
       return float_type;
    case GLSL_TYPE_DOUBLE:
@@ -309,6 +311,8 @@ const glsl_type *glsl_type::get_scalar_type() const
       return uint_type;
    case GLSL_TYPE_INT:
       return int_type;
+   case GLSL_TYPE_HALF:
+      return hfloat_type;
    case GLSL_TYPE_FLOAT:
       return float_type;
    case GLSL_TYPE_DOUBLE:
@@ -387,6 +391,18 @@ glsl_type::glsl_type(const glsl_type *array, unsigned length) :
 
 
 const glsl_type *
+glsl_type::hvec(unsigned components)
+{
+   if (components == 0 || components > 4)
+      return error_type;
+
+   static const glsl_type *const ts[] = {
+      hfloat_type, hvec2_type, hvec3_type, hvec4_type
+   };
+   return ts[components - 1];
+}
+
+const glsl_type *
 glsl_type::vec(unsigned components)
 {
    if (components == 0 || components > 4)
@@ -466,6 +482,8 @@ glsl_type::get_instance(unsigned base_type, unsigned rows, unsigned columns)
 	 return uvec(rows);
       case GLSL_TYPE_INT:
 	 return ivec(rows);
+      case GLSL_TYPE_HALF:
+	 return hvec(rows);
       case GLSL_TYPE_FLOAT:
 	 return vec(rows);
       case GLSL_TYPE_DOUBLE:
@@ -476,7 +494,11 @@ glsl_type::get_instance(unsigned base_type, unsigned rows, unsigned columns)
 	 return error_type;
       }
    } else {
-      if ((base_type != GLSL_TYPE_FLOAT && base_type != GLSL_TYPE_DOUBLE) || (rows == 1))
+      const bool is_float = base_type == GLSL_TYPE_HALF ||
+                            base_type == GLSL_TYPE_FLOAT ||
+                            base_type == GLSL_TYPE_DOUBLE;
+
+      if (!is_float || rows == 1)
 	 return error_type;
 
       /* GLSL matrix types are named mat{COLUMNS}x{ROWS}.  Only the following
@@ -490,7 +512,21 @@ glsl_type::get_instance(unsigned base_type, unsigned rows, unsigned columns)
        */
 #define IDX(c,r) (((c-1)*3) + (r-1))
 
-      if (base_type == GLSL_TYPE_DOUBLE) {
+      switch (base_type) {
+      case GLSL_TYPE_HALF:
+         switch (IDX(columns, rows)) {
+         case IDX(2,2): return hmat2_type;
+         case IDX(2,3): return hmat2x3_type;
+         case IDX(2,4): return hmat2x4_type;
+         case IDX(3,2): return hmat3x2_type;
+         case IDX(3,3): return hmat3_type;
+         case IDX(3,4): return hmat3x4_type;
+         case IDX(4,2): return hmat4x2_type;
+         case IDX(4,3): return hmat4x3_type;
+         case IDX(4,4): return hmat4_type;
+         default: return error_type;
+         }
+      case GLSL_TYPE_DOUBLE:
          switch (IDX(columns, rows)) {
          case IDX(2,2): return dmat2_type;
          case IDX(2,3): return dmat2x3_type;
@@ -503,7 +539,8 @@ glsl_type::get_instance(unsigned base_type, unsigned rows, unsigned columns)
          case IDX(4,4): return dmat4_type;
          default: return error_type;
          }
-      } else {
+         break;
+      default:
          switch (IDX(columns, rows)) {
          case IDX(2,2): return mat2_type;
          case IDX(2,3): return mat2x3_type;
@@ -978,6 +1015,7 @@ glsl_type::uniform_locations() const
    switch (this->base_type) {
    case GLSL_TYPE_UINT:
    case GLSL_TYPE_INT:
+   case GLSL_TYPE_HALF:
    case GLSL_TYPE_FLOAT:
    case GLSL_TYPE_DOUBLE:
    case GLSL_TYPE_BOOL:
diff --git a/src/glsl/glsl_types.h b/src/glsl/glsl_types.h
index 86686e3..d58718e 100644
--- a/src/glsl/glsl_types.h
+++ b/src/glsl/glsl_types.h
@@ -207,6 +207,7 @@ struct glsl_type {
     * Convenience accessors for vector types (shorter than get_instance()).
     * @{
     */
+   static const glsl_type *hvec(unsigned components);
    static const glsl_type *vec(unsigned components);
    static const glsl_type *dvec(unsigned components);
    static const glsl_type *ivec(unsigned components);
@@ -403,7 +404,10 @@ struct glsl_type {
    bool is_matrix() const
    {
       /* GLSL only has float matrices. */
-      return (matrix_columns > 1) && (base_type == GLSL_TYPE_FLOAT || base_type == GLSL_TYPE_DOUBLE);
+      return (matrix_columns > 1) &&
+             (base_type == GLSL_TYPE_HALF ||
+              base_type == GLSL_TYPE_FLOAT ||
+              base_type == GLSL_TYPE_DOUBLE);
    }
 
    /**
-- 
1.9.3



More information about the mesa-dev mailing list