[Mesa-dev] [PATCH 05/19] glsl: add gl_SubGroup*ARB builtins

Nicolai Hähnle nhaehnle at gmail.com
Fri Mar 31 17:14:05 UTC 2017


From: Nicolai Hähnle <nicolai.haehnle at amd.com>

---
 src/compiler/glsl/builtin_variables.cpp | 22 ++++++++++++
 src/compiler/shader_enums.c             |  7 ++++
 src/compiler/shader_enums.h             | 59 +++++++++++++++++++++++++++++++++
 3 files changed, 88 insertions(+)

diff --git a/src/compiler/glsl/builtin_variables.cpp b/src/compiler/glsl/builtin_variables.cpp
index fc0443e..c232571 100644
--- a/src/compiler/glsl/builtin_variables.cpp
+++ b/src/compiler/glsl/builtin_variables.cpp
@@ -358,20 +358,21 @@ per_vertex_accumulator::construct_interface_instance() const
 }
 
 
 class builtin_variable_generator
 {
 public:
    builtin_variable_generator(exec_list *instructions,
                               struct _mesa_glsl_parse_state *state);
    void generate_constants();
    void generate_uniforms();
+   void generate_special_vars();
    void generate_vs_special_vars();
    void generate_tcs_special_vars();
    void generate_tes_special_vars();
    void generate_gs_special_vars();
    void generate_fs_special_vars();
    void generate_cs_special_vars();
    void generate_varyings();
 
 private:
    const glsl_type *array(const glsl_type *base, unsigned elements)
@@ -421,39 +422,41 @@ private:
    /**
     * True if compatibility-profile-only variables should be included.  (In
     * desktop GL, these are always included when the GLSL version is 1.30 and
     * or below).
     */
    const bool compatibility;
 
    const glsl_type * const bool_t;
    const glsl_type * const int_t;
    const glsl_type * const uint_t;
+   const glsl_type * const uint64_t;
    const glsl_type * const float_t;
    const glsl_type * const vec2_t;
    const glsl_type * const vec3_t;
    const glsl_type * const vec4_t;
    const glsl_type * const uvec3_t;
    const glsl_type * const mat3_t;
    const glsl_type * const mat4_t;
 
    per_vertex_accumulator per_vertex_in;
    per_vertex_accumulator per_vertex_out;
 };
 
 
 builtin_variable_generator::builtin_variable_generator(
    exec_list *instructions, struct _mesa_glsl_parse_state *state)
    : instructions(instructions), state(state), symtab(state->symbols),
      compatibility(state->compat_shader || !state->is_version(140, 100)),
      bool_t(glsl_type::bool_type), int_t(glsl_type::int_type),
      uint_t(glsl_type::uint_type),
+     uint64_t(glsl_type::uint64_t_type),
      float_t(glsl_type::float_type), vec2_t(glsl_type::vec2_type),
      vec3_t(glsl_type::vec3_type), vec4_t(glsl_type::vec4_type),
      uvec3_t(glsl_type::uvec3_type),
      mat3_t(glsl_type::mat3_type), mat4_t(glsl_type::mat4_type)
 {
 }
 
 ir_variable *
 builtin_variable_generator::add_index_variable(const char *name,
                                          const glsl_type *type,
@@ -977,20 +980,38 @@ builtin_variable_generator::generate_uniforms()
       add_uniform(texcoords_vec4, "gl_ObjectPlaneT");
       add_uniform(texcoords_vec4, "gl_ObjectPlaneR");
       add_uniform(texcoords_vec4, "gl_ObjectPlaneQ");
 
       add_uniform(type("gl_FogParameters"), "gl_Fog");
    }
 }
 
 
 /**
+ * Generate special variables which exist in all shaders.
+ */
+void
+builtin_variable_generator::generate_special_vars()
+{
+   if (state->ARB_shader_ballot_enable) {
+      add_system_value(SYSTEM_VALUE_SUBGROUP_SIZE, uint_t, "gl_SubGroupSizeARB");
+      add_system_value(SYSTEM_VALUE_SUBGROUP_INVOCATION, uint_t, "gl_SubGroupInvocationARB");
+      add_system_value(SYSTEM_VALUE_SUBGROUP_EQ_MASK, uint64_t, "gl_SubGroupEqMaskARB");
+      add_system_value(SYSTEM_VALUE_SUBGROUP_GE_MASK, uint64_t, "gl_SubGroupGeMaskARB");
+      add_system_value(SYSTEM_VALUE_SUBGROUP_GT_MASK, uint64_t, "gl_SubGroupGtMaskARB");
+      add_system_value(SYSTEM_VALUE_SUBGROUP_LE_MASK, uint64_t, "gl_SubGroupLeMaskARB");
+      add_system_value(SYSTEM_VALUE_SUBGROUP_LT_MASK, uint64_t, "gl_SubGroupLtMaskARB");
+   }
+}
+
+
+/**
  * Generate variables which only exist in vertex shaders.
  */
 void
 builtin_variable_generator::generate_vs_special_vars()
 {
    ir_variable *var;
 
    if (state->is_version(130, 300))
       add_system_value(SYSTEM_VALUE_VERTEX_ID, int_t, "gl_VertexID");
    if (state->ARB_draw_instanced_enable)
@@ -1409,20 +1430,21 @@ builtin_variable_generator::generate_varyings()
 
 
 void
 _mesa_glsl_initialize_variables(exec_list *instructions,
 				struct _mesa_glsl_parse_state *state)
 {
    builtin_variable_generator gen(instructions, state);
 
    gen.generate_constants();
    gen.generate_uniforms();
+   gen.generate_special_vars();
 
    gen.generate_varyings();
 
    switch (state->stage) {
    case MESA_SHADER_VERTEX:
       gen.generate_vs_special_vars();
       break;
    case MESA_SHADER_TESS_CTRL:
       gen.generate_tcs_special_vars();
       break;
diff --git a/src/compiler/shader_enums.c b/src/compiler/shader_enums.c
index e704c95..ca62cda 100644
--- a/src/compiler/shader_enums.c
+++ b/src/compiler/shader_enums.c
@@ -196,20 +196,27 @@ gl_varying_slot_name(gl_varying_slot slot)
       ENUM(VARYING_SLOT_VAR31),
    };
    STATIC_ASSERT(ARRAY_SIZE(names) == VARYING_SLOT_MAX);
    return NAME(slot);
 }
 
 const char *
 gl_system_value_name(gl_system_value sysval)
 {
    static const char *names[] = {
+     ENUM(SYSTEM_VALUE_SUBGROUP_SIZE),
+     ENUM(SYSTEM_VALUE_SUBGROUP_INVOCATION),
+     ENUM(SYSTEM_VALUE_SUBGROUP_EQ_MASK),
+     ENUM(SYSTEM_VALUE_SUBGROUP_GE_MASK),
+     ENUM(SYSTEM_VALUE_SUBGROUP_GT_MASK),
+     ENUM(SYSTEM_VALUE_SUBGROUP_LE_MASK),
+     ENUM(SYSTEM_VALUE_SUBGROUP_LT_MASK),
      ENUM(SYSTEM_VALUE_VERTEX_ID),
      ENUM(SYSTEM_VALUE_INSTANCE_ID),
      ENUM(SYSTEM_VALUE_INSTANCE_INDEX),
      ENUM(SYSTEM_VALUE_VERTEX_ID_ZERO_BASE),
      ENUM(SYSTEM_VALUE_BASE_VERTEX),
      ENUM(SYSTEM_VALUE_BASE_INSTANCE),
      ENUM(SYSTEM_VALUE_DRAW_ID),
      ENUM(SYSTEM_VALUE_INVOCATION_ID),
      ENUM(SYSTEM_VALUE_FRONT_FACE),
      ENUM(SYSTEM_VALUE_SAMPLE_ID),
diff --git a/src/compiler/shader_enums.h b/src/compiler/shader_enums.h
index 98565c8..930d997 100644
--- a/src/compiler/shader_enums.h
+++ b/src/compiler/shader_enums.h
@@ -311,20 +311,79 @@ const char *gl_varying_slot_name(gl_varying_slot slot);
 #define SYSTEM_BIT_LOCAL_INVOCATION_ID ((uint64_t)1 << SYSTEM_VALUE_LOCAL_INVOCATION_ID)
 
 /**
  * If the gl_register_file is PROGRAM_SYSTEM_VALUE, the register index will be
  * one of these values.  If a NIR variable's mode is nir_var_system_value, it
  * will be one of these values.
  */
 typedef enum
 {
    /**
+    * \name System values applicable to all shaders
+    */
+   /*@{*/
+
+   /**
+    * Builtin variables added by GL_ARB_shader_ballot.
+    */
+   /*@{*/
+
+   /**
+    * From the GL_ARB_shader-ballot spec:
+    *
+    *    "A sub-group is a collection of invocations which execute in lockstep.
+    *     The variable <gl_SubGroupSizeARB> is the maximum number of
+    *     invocations in a sub-group. The maximum <gl_SubGroupSizeARB>
+    *     supported in this extension is 64."
+    *
+    * The spec defines this as a uniform. However, it's highly unlikely that
+    * implementations actually treat it as a uniform (which is loaded from a
+    * constant buffer). Most likely, this is an implementation-wide constant,
+    * or perhaps something that depends on the shader stage.
+    */
+   SYSTEM_VALUE_SUBGROUP_SIZE,
+
+   /**
+    * From the GL_ARB_shader_ballot spec:
+    *
+    *    "The variable <gl_SubGroupInvocationARB> holds the index of the
+    *     invocation within sub-group. This variable is in the range 0 to
+    *     <gl_SubGroupSizeARB>-1, where <gl_SubGroupSizeARB> is the total
+    *     number of invocations in a sub-group."
+    */
+   SYSTEM_VALUE_SUBGROUP_INVOCATION,
+
+   /**
+    * From the GL_ARB_shader_ballot spec:
+    *
+    *    "The <gl_SubGroup??MaskARB> variables provide a bitmask for all
+    *     invocations, with one bit per invocation starting with the least
+    *     significant bit, according to the following table,
+    *
+    *       variable               equation for bit values
+    *       --------------------   ------------------------------------
+    *       gl_SubGroupEqMaskARB   bit index == gl_SubGroupInvocationARB
+    *       gl_SubGroupGeMaskARB   bit index >= gl_SubGroupInvocationARB
+    *       gl_SubGroupGtMaskARB   bit index >  gl_SubGroupInvocationARB
+    *       gl_SubGroupLeMaskARB   bit index <= gl_SubGroupInvocationARB
+    *       gl_SubGroupLtMaskARB   bit index <  gl_SubGroupInvocationARB
+    */
+   SYSTEM_VALUE_SUBGROUP_EQ_MASK,
+   SYSTEM_VALUE_SUBGROUP_GE_MASK,
+   SYSTEM_VALUE_SUBGROUP_GT_MASK,
+   SYSTEM_VALUE_SUBGROUP_LE_MASK,
+   SYSTEM_VALUE_SUBGROUP_LT_MASK,
+   /*@}*/
+
+   /*@}*/
+
+   /**
     * \name Vertex shader system values
     */
    /*@{*/
    /**
     * OpenGL-style vertex ID.
     *
     * Section 2.11.7 (Shader Execution), subsection Shader Inputs, of the
     * OpenGL 3.3 core profile spec says:
     *
     *     "gl_VertexID holds the integer index i implicitly passed by
-- 
2.9.3



More information about the mesa-dev mailing list