Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright © 2010 Intel Corporation
  3.  *
  4.  * Permission is hereby granted, free of charge, to any person obtaining a
  5.  * copy of this software and associated documentation files (the "Software"),
  6.  * to deal in the Software without restriction, including without limitation
  7.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8.  * and/or sell copies of the Software, and to permit persons to whom the
  9.  * Software is furnished to do so, subject to the following conditions:
  10.  *
  11.  * The above copyright notice and this permission notice (including the next
  12.  * paragraph) shall be included in all copies or substantial portions of the
  13.  * Software.
  14.  *
  15.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  18.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20.  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21.  * DEALINGS IN THE SOFTWARE.
  22.  */
  23.  
  24. #include "ir.h"
  25. #include "glsl_parser_extras.h"
  26. #include "glsl_symbol_table.h"
  27. #include "main/core.h"
  28. #include "main/uniforms.h"
  29. #include "program/prog_statevars.h"
  30. #include "program/prog_instruction.h"
  31.  
  32. static const struct gl_builtin_uniform_element gl_NumSamples_elements[] = {
  33.    {NULL, {STATE_NUM_SAMPLES, 0, 0}, SWIZZLE_XXXX}
  34. };
  35.  
  36. static const struct gl_builtin_uniform_element gl_DepthRange_elements[] = {
  37.    {"near", {STATE_DEPTH_RANGE, 0, 0}, SWIZZLE_XXXX},
  38.    {"far", {STATE_DEPTH_RANGE, 0, 0}, SWIZZLE_YYYY},
  39.    {"diff", {STATE_DEPTH_RANGE, 0, 0}, SWIZZLE_ZZZZ},
  40. };
  41.  
  42. static const struct gl_builtin_uniform_element gl_ClipPlane_elements[] = {
  43.    {NULL, {STATE_CLIPPLANE, 0, 0}, SWIZZLE_XYZW}
  44. };
  45.  
  46. static const struct gl_builtin_uniform_element gl_Point_elements[] = {
  47.    {"size", {STATE_POINT_SIZE}, SWIZZLE_XXXX},
  48.    {"sizeMin", {STATE_POINT_SIZE}, SWIZZLE_YYYY},
  49.    {"sizeMax", {STATE_POINT_SIZE}, SWIZZLE_ZZZZ},
  50.    {"fadeThresholdSize", {STATE_POINT_SIZE}, SWIZZLE_WWWW},
  51.    {"distanceConstantAttenuation", {STATE_POINT_ATTENUATION}, SWIZZLE_XXXX},
  52.    {"distanceLinearAttenuation", {STATE_POINT_ATTENUATION}, SWIZZLE_YYYY},
  53.    {"distanceQuadraticAttenuation", {STATE_POINT_ATTENUATION}, SWIZZLE_ZZZZ},
  54. };
  55.  
  56. static const struct gl_builtin_uniform_element gl_FrontMaterial_elements[] = {
  57.    {"emission", {STATE_MATERIAL, 0, STATE_EMISSION}, SWIZZLE_XYZW},
  58.    {"ambient", {STATE_MATERIAL, 0, STATE_AMBIENT}, SWIZZLE_XYZW},
  59.    {"diffuse", {STATE_MATERIAL, 0, STATE_DIFFUSE}, SWIZZLE_XYZW},
  60.    {"specular", {STATE_MATERIAL, 0, STATE_SPECULAR}, SWIZZLE_XYZW},
  61.    {"shininess", {STATE_MATERIAL, 0, STATE_SHININESS}, SWIZZLE_XXXX},
  62. };
  63.  
  64. static const struct gl_builtin_uniform_element gl_BackMaterial_elements[] = {
  65.    {"emission", {STATE_MATERIAL, 1, STATE_EMISSION}, SWIZZLE_XYZW},
  66.    {"ambient", {STATE_MATERIAL, 1, STATE_AMBIENT}, SWIZZLE_XYZW},
  67.    {"diffuse", {STATE_MATERIAL, 1, STATE_DIFFUSE}, SWIZZLE_XYZW},
  68.    {"specular", {STATE_MATERIAL, 1, STATE_SPECULAR}, SWIZZLE_XYZW},
  69.    {"shininess", {STATE_MATERIAL, 1, STATE_SHININESS}, SWIZZLE_XXXX},
  70. };
  71.  
  72. static const struct gl_builtin_uniform_element gl_LightSource_elements[] = {
  73.    {"ambient", {STATE_LIGHT, 0, STATE_AMBIENT}, SWIZZLE_XYZW},
  74.    {"diffuse", {STATE_LIGHT, 0, STATE_DIFFUSE}, SWIZZLE_XYZW},
  75.    {"specular", {STATE_LIGHT, 0, STATE_SPECULAR}, SWIZZLE_XYZW},
  76.    {"position", {STATE_LIGHT, 0, STATE_POSITION}, SWIZZLE_XYZW},
  77.    {"halfVector", {STATE_LIGHT, 0, STATE_HALF_VECTOR}, SWIZZLE_XYZW},
  78.    {"spotDirection", {STATE_LIGHT, 0, STATE_SPOT_DIRECTION},
  79.     MAKE_SWIZZLE4(SWIZZLE_X,
  80.                   SWIZZLE_Y,
  81.                   SWIZZLE_Z,
  82.                   SWIZZLE_Z)},
  83.    {"spotCosCutoff", {STATE_LIGHT, 0, STATE_SPOT_DIRECTION}, SWIZZLE_WWWW},
  84.    {"spotCutoff", {STATE_LIGHT, 0, STATE_SPOT_CUTOFF}, SWIZZLE_XXXX},
  85.    {"spotExponent", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_WWWW},
  86.    {"constantAttenuation", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_XXXX},
  87.    {"linearAttenuation", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_YYYY},
  88.    {"quadraticAttenuation", {STATE_LIGHT, 0, STATE_ATTENUATION}, SWIZZLE_ZZZZ},
  89. };
  90.  
  91. static const struct gl_builtin_uniform_element gl_LightModel_elements[] = {
  92.    {"ambient", {STATE_LIGHTMODEL_AMBIENT, 0}, SWIZZLE_XYZW},
  93. };
  94.  
  95. static const struct gl_builtin_uniform_element gl_FrontLightModelProduct_elements[] = {
  96.    {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR, 0}, SWIZZLE_XYZW},
  97. };
  98.  
  99. static const struct gl_builtin_uniform_element gl_BackLightModelProduct_elements[] = {
  100.    {"sceneColor", {STATE_LIGHTMODEL_SCENECOLOR, 1}, SWIZZLE_XYZW},
  101. };
  102.  
  103. static const struct gl_builtin_uniform_element gl_FrontLightProduct_elements[] = {
  104.    {"ambient", {STATE_LIGHTPROD, 0, 0, STATE_AMBIENT}, SWIZZLE_XYZW},
  105.    {"diffuse", {STATE_LIGHTPROD, 0, 0, STATE_DIFFUSE}, SWIZZLE_XYZW},
  106.    {"specular", {STATE_LIGHTPROD, 0, 0, STATE_SPECULAR}, SWIZZLE_XYZW},
  107. };
  108.  
  109. static const struct gl_builtin_uniform_element gl_BackLightProduct_elements[] = {
  110.    {"ambient", {STATE_LIGHTPROD, 0, 1, STATE_AMBIENT}, SWIZZLE_XYZW},
  111.    {"diffuse", {STATE_LIGHTPROD, 0, 1, STATE_DIFFUSE}, SWIZZLE_XYZW},
  112.    {"specular", {STATE_LIGHTPROD, 0, 1, STATE_SPECULAR}, SWIZZLE_XYZW},
  113. };
  114.  
  115. static const struct gl_builtin_uniform_element gl_TextureEnvColor_elements[] = {
  116.    {NULL, {STATE_TEXENV_COLOR, 0}, SWIZZLE_XYZW},
  117. };
  118.  
  119. static const struct gl_builtin_uniform_element gl_EyePlaneS_elements[] = {
  120.    {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_S}, SWIZZLE_XYZW},
  121. };
  122.  
  123. static const struct gl_builtin_uniform_element gl_EyePlaneT_elements[] = {
  124.    {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_T}, SWIZZLE_XYZW},
  125. };
  126.  
  127. static const struct gl_builtin_uniform_element gl_EyePlaneR_elements[] = {
  128.    {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_R}, SWIZZLE_XYZW},
  129. };
  130.  
  131. static const struct gl_builtin_uniform_element gl_EyePlaneQ_elements[] = {
  132.    {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_EYE_Q}, SWIZZLE_XYZW},
  133. };
  134.  
  135. static const struct gl_builtin_uniform_element gl_ObjectPlaneS_elements[] = {
  136.    {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_S}, SWIZZLE_XYZW},
  137. };
  138.  
  139. static const struct gl_builtin_uniform_element gl_ObjectPlaneT_elements[] = {
  140.    {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_T}, SWIZZLE_XYZW},
  141. };
  142.  
  143. static const struct gl_builtin_uniform_element gl_ObjectPlaneR_elements[] = {
  144.    {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_R}, SWIZZLE_XYZW},
  145. };
  146.  
  147. static const struct gl_builtin_uniform_element gl_ObjectPlaneQ_elements[] = {
  148.    {NULL, {STATE_TEXGEN, 0, STATE_TEXGEN_OBJECT_Q}, SWIZZLE_XYZW},
  149. };
  150.  
  151. static const struct gl_builtin_uniform_element gl_Fog_elements[] = {
  152.    {"color", {STATE_FOG_COLOR}, SWIZZLE_XYZW},
  153.    {"density", {STATE_FOG_PARAMS}, SWIZZLE_XXXX},
  154.    {"start", {STATE_FOG_PARAMS}, SWIZZLE_YYYY},
  155.    {"end", {STATE_FOG_PARAMS}, SWIZZLE_ZZZZ},
  156.    {"scale", {STATE_FOG_PARAMS}, SWIZZLE_WWWW},
  157. };
  158.  
  159. static const struct gl_builtin_uniform_element gl_NormalScale_elements[] = {
  160.    {NULL, {STATE_NORMAL_SCALE}, SWIZZLE_XXXX},
  161. };
  162.  
  163. static const struct gl_builtin_uniform_element gl_FogParamsOptimizedMESA_elements[] = {
  164.    {NULL, {STATE_INTERNAL, STATE_FOG_PARAMS_OPTIMIZED}, SWIZZLE_XYZW},
  165. };
  166.  
  167. static const struct gl_builtin_uniform_element gl_CurrentAttribVertMESA_elements[] = {
  168.    {NULL, {STATE_INTERNAL, STATE_CURRENT_ATTRIB, 0}, SWIZZLE_XYZW},
  169. };
  170.  
  171. static const struct gl_builtin_uniform_element gl_CurrentAttribFragMESA_elements[] = {
  172.    {NULL, {STATE_INTERNAL, STATE_CURRENT_ATTRIB_MAYBE_VP_CLAMPED, 0}, SWIZZLE_XYZW},
  173. };
  174.  
  175. #define MATRIX(name, statevar, modifier)                                \
  176.    static const struct gl_builtin_uniform_element name ## _elements[] = { \
  177.       { NULL, { statevar, 0, 0, 0, modifier}, SWIZZLE_XYZW },           \
  178.       { NULL, { statevar, 0, 1, 1, modifier}, SWIZZLE_XYZW },           \
  179.       { NULL, { statevar, 0, 2, 2, modifier}, SWIZZLE_XYZW },           \
  180.       { NULL, { statevar, 0, 3, 3, modifier}, SWIZZLE_XYZW },           \
  181.    }
  182.  
  183. MATRIX(gl_ModelViewMatrix,
  184.        STATE_MODELVIEW_MATRIX, STATE_MATRIX_TRANSPOSE);
  185. MATRIX(gl_ModelViewMatrixInverse,
  186.        STATE_MODELVIEW_MATRIX, STATE_MATRIX_INVTRANS);
  187. MATRIX(gl_ModelViewMatrixTranspose,
  188.        STATE_MODELVIEW_MATRIX, 0);
  189. MATRIX(gl_ModelViewMatrixInverseTranspose,
  190.        STATE_MODELVIEW_MATRIX, STATE_MATRIX_INVERSE);
  191.  
  192. MATRIX(gl_ProjectionMatrix,
  193.        STATE_PROJECTION_MATRIX, STATE_MATRIX_TRANSPOSE);
  194. MATRIX(gl_ProjectionMatrixInverse,
  195.        STATE_PROJECTION_MATRIX, STATE_MATRIX_INVTRANS);
  196. MATRIX(gl_ProjectionMatrixTranspose,
  197.        STATE_PROJECTION_MATRIX, 0);
  198. MATRIX(gl_ProjectionMatrixInverseTranspose,
  199.        STATE_PROJECTION_MATRIX, STATE_MATRIX_INVERSE);
  200.  
  201. MATRIX(gl_ModelViewProjectionMatrix,
  202.        STATE_MVP_MATRIX, STATE_MATRIX_TRANSPOSE);
  203. MATRIX(gl_ModelViewProjectionMatrixInverse,
  204.        STATE_MVP_MATRIX, STATE_MATRIX_INVTRANS);
  205. MATRIX(gl_ModelViewProjectionMatrixTranspose,
  206.        STATE_MVP_MATRIX, 0);
  207. MATRIX(gl_ModelViewProjectionMatrixInverseTranspose,
  208.        STATE_MVP_MATRIX, STATE_MATRIX_INVERSE);
  209.  
  210. MATRIX(gl_TextureMatrix,
  211.        STATE_TEXTURE_MATRIX, STATE_MATRIX_TRANSPOSE);
  212. MATRIX(gl_TextureMatrixInverse,
  213.        STATE_TEXTURE_MATRIX, STATE_MATRIX_INVTRANS);
  214. MATRIX(gl_TextureMatrixTranspose,
  215.        STATE_TEXTURE_MATRIX, 0);
  216. MATRIX(gl_TextureMatrixInverseTranspose,
  217.        STATE_TEXTURE_MATRIX, STATE_MATRIX_INVERSE);
  218.  
  219. static const struct gl_builtin_uniform_element gl_NormalMatrix_elements[] = {
  220.    { NULL, { STATE_MODELVIEW_MATRIX, 0, 0, 0, STATE_MATRIX_INVERSE},
  221.      MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z) },
  222.    { NULL, { STATE_MODELVIEW_MATRIX, 0, 1, 1, STATE_MATRIX_INVERSE},
  223.      MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z) },
  224.    { NULL, { STATE_MODELVIEW_MATRIX, 0, 2, 2, STATE_MATRIX_INVERSE},
  225.      MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z) },
  226. };
  227.  
  228. #undef MATRIX
  229.  
  230. #define STATEVAR(name) {#name, name ## _elements, ARRAY_SIZE(name ## _elements)}
  231.  
  232. static const struct gl_builtin_uniform_desc _mesa_builtin_uniform_desc[] = {
  233.    STATEVAR(gl_NumSamples),
  234.    STATEVAR(gl_DepthRange),
  235.    STATEVAR(gl_ClipPlane),
  236.    STATEVAR(gl_Point),
  237.    STATEVAR(gl_FrontMaterial),
  238.    STATEVAR(gl_BackMaterial),
  239.    STATEVAR(gl_LightSource),
  240.    STATEVAR(gl_LightModel),
  241.    STATEVAR(gl_FrontLightModelProduct),
  242.    STATEVAR(gl_BackLightModelProduct),
  243.    STATEVAR(gl_FrontLightProduct),
  244.    STATEVAR(gl_BackLightProduct),
  245.    STATEVAR(gl_TextureEnvColor),
  246.    STATEVAR(gl_EyePlaneS),
  247.    STATEVAR(gl_EyePlaneT),
  248.    STATEVAR(gl_EyePlaneR),
  249.    STATEVAR(gl_EyePlaneQ),
  250.    STATEVAR(gl_ObjectPlaneS),
  251.    STATEVAR(gl_ObjectPlaneT),
  252.    STATEVAR(gl_ObjectPlaneR),
  253.    STATEVAR(gl_ObjectPlaneQ),
  254.    STATEVAR(gl_Fog),
  255.  
  256.    STATEVAR(gl_ModelViewMatrix),
  257.    STATEVAR(gl_ModelViewMatrixInverse),
  258.    STATEVAR(gl_ModelViewMatrixTranspose),
  259.    STATEVAR(gl_ModelViewMatrixInverseTranspose),
  260.  
  261.    STATEVAR(gl_ProjectionMatrix),
  262.    STATEVAR(gl_ProjectionMatrixInverse),
  263.    STATEVAR(gl_ProjectionMatrixTranspose),
  264.    STATEVAR(gl_ProjectionMatrixInverseTranspose),
  265.  
  266.    STATEVAR(gl_ModelViewProjectionMatrix),
  267.    STATEVAR(gl_ModelViewProjectionMatrixInverse),
  268.    STATEVAR(gl_ModelViewProjectionMatrixTranspose),
  269.    STATEVAR(gl_ModelViewProjectionMatrixInverseTranspose),
  270.  
  271.    STATEVAR(gl_TextureMatrix),
  272.    STATEVAR(gl_TextureMatrixInverse),
  273.    STATEVAR(gl_TextureMatrixTranspose),
  274.    STATEVAR(gl_TextureMatrixInverseTranspose),
  275.  
  276.    STATEVAR(gl_NormalMatrix),
  277.    STATEVAR(gl_NormalScale),
  278.  
  279.    STATEVAR(gl_FogParamsOptimizedMESA),
  280.    STATEVAR(gl_CurrentAttribVertMESA),
  281.    STATEVAR(gl_CurrentAttribFragMESA),
  282.  
  283.    {NULL, NULL, 0}
  284. };
  285.  
  286.  
  287. namespace {
  288.  
  289. /**
  290.  * Data structure that accumulates fields for the gl_PerVertex interface
  291.  * block.
  292.  */
  293. class per_vertex_accumulator
  294. {
  295. public:
  296.    per_vertex_accumulator();
  297.    void add_field(int slot, const glsl_type *type, const char *name);
  298.    const glsl_type *construct_interface_instance() const;
  299.  
  300. private:
  301.    glsl_struct_field fields[10];
  302.    unsigned num_fields;
  303. };
  304.  
  305.  
  306. per_vertex_accumulator::per_vertex_accumulator()
  307.    : fields(),
  308.      num_fields(0)
  309. {
  310. }
  311.  
  312.  
  313. void
  314. per_vertex_accumulator::add_field(int slot, const glsl_type *type,
  315.                                   const char *name)
  316. {
  317.    assert(this->num_fields < ARRAY_SIZE(this->fields));
  318.    this->fields[this->num_fields].type = type;
  319.    this->fields[this->num_fields].name = name;
  320.    this->fields[this->num_fields].matrix_layout = GLSL_MATRIX_LAYOUT_INHERITED;
  321.    this->fields[this->num_fields].location = slot;
  322.    this->fields[this->num_fields].interpolation = INTERP_QUALIFIER_NONE;
  323.    this->fields[this->num_fields].centroid = 0;
  324.    this->fields[this->num_fields].sample = 0;
  325.    this->num_fields++;
  326. }
  327.  
  328.  
  329. const glsl_type *
  330. per_vertex_accumulator::construct_interface_instance() const
  331. {
  332.    return glsl_type::get_interface_instance(this->fields, this->num_fields,
  333.                                             GLSL_INTERFACE_PACKING_STD140,
  334.                                             "gl_PerVertex");
  335. }
  336.  
  337.  
  338. class builtin_variable_generator
  339. {
  340. public:
  341.    builtin_variable_generator(exec_list *instructions,
  342.                               struct _mesa_glsl_parse_state *state);
  343.    void generate_constants();
  344.    void generate_uniforms();
  345.    void generate_vs_special_vars();
  346.    void generate_gs_special_vars();
  347.    void generate_fs_special_vars();
  348.    void generate_cs_special_vars();
  349.    void generate_varyings();
  350.  
  351. private:
  352.    const glsl_type *array(const glsl_type *base, unsigned elements)
  353.    {
  354.       return glsl_type::get_array_instance(base, elements);
  355.    }
  356.  
  357.    const glsl_type *type(const char *name)
  358.    {
  359.       return symtab->get_type(name);
  360.    }
  361.  
  362.    ir_variable *add_input(int slot, const glsl_type *type, const char *name)
  363.    {
  364.       return add_variable(name, type, ir_var_shader_in, slot);
  365.    }
  366.  
  367.    ir_variable *add_output(int slot, const glsl_type *type, const char *name)
  368.    {
  369.       return add_variable(name, type, ir_var_shader_out, slot);
  370.    }
  371.  
  372.    ir_variable *add_system_value(int slot, const glsl_type *type,
  373.                                  const char *name)
  374.    {
  375.       return add_variable(name, type, ir_var_system_value, slot);
  376.    }
  377.  
  378.    ir_variable *add_variable(const char *name, const glsl_type *type,
  379.                              enum ir_variable_mode mode, int slot);
  380.    ir_variable *add_uniform(const glsl_type *type, const char *name);
  381.    ir_variable *add_const(const char *name, int value);
  382.    ir_variable *add_const_ivec3(const char *name, int x, int y, int z);
  383.    void add_varying(int slot, const glsl_type *type, const char *name,
  384.                     const char *name_as_gs_input);
  385.  
  386.    exec_list * const instructions;
  387.    struct _mesa_glsl_parse_state * const state;
  388.    glsl_symbol_table * const symtab;
  389.  
  390.    /**
  391.     * True if compatibility-profile-only variables should be included.  (In
  392.     * desktop GL, these are always included when the GLSL version is 1.30 and
  393.     * or below).
  394.     */
  395.    const bool compatibility;
  396.  
  397.    const glsl_type * const bool_t;
  398.    const glsl_type * const int_t;
  399.    const glsl_type * const float_t;
  400.    const glsl_type * const vec2_t;
  401.    const glsl_type * const vec3_t;
  402.    const glsl_type * const vec4_t;
  403.    const glsl_type * const mat3_t;
  404.    const glsl_type * const mat4_t;
  405.  
  406.    per_vertex_accumulator per_vertex_in;
  407.    per_vertex_accumulator per_vertex_out;
  408. };
  409.  
  410.  
  411. builtin_variable_generator::builtin_variable_generator(
  412.    exec_list *instructions, struct _mesa_glsl_parse_state *state)
  413.    : instructions(instructions), state(state), symtab(state->symbols),
  414.      compatibility(!state->is_version(140, 100)),
  415.      bool_t(glsl_type::bool_type), int_t(glsl_type::int_type),
  416.      float_t(glsl_type::float_type), vec2_t(glsl_type::vec2_type),
  417.      vec3_t(glsl_type::vec3_type), vec4_t(glsl_type::vec4_type),
  418.      mat3_t(glsl_type::mat3_type), mat4_t(glsl_type::mat4_type)
  419. {
  420. }
  421.  
  422.  
  423. ir_variable *
  424. builtin_variable_generator::add_variable(const char *name,
  425.                                          const glsl_type *type,
  426.                                          enum ir_variable_mode mode, int slot)
  427. {
  428.    ir_variable *var = new(symtab) ir_variable(type, name, mode);
  429.    var->data.how_declared = ir_var_declared_implicitly;
  430.  
  431.    switch (var->data.mode) {
  432.    case ir_var_auto:
  433.    case ir_var_shader_in:
  434.    case ir_var_uniform:
  435.    case ir_var_system_value:
  436.       var->data.read_only = true;
  437.       break;
  438.    case ir_var_shader_out:
  439.       break;
  440.    default:
  441.       /* The only variables that are added using this function should be
  442.        * uniforms, shader inputs, and shader outputs, constants (which use
  443.        * ir_var_auto), and system values.
  444.        */
  445.       assert(0);
  446.       break;
  447.    }
  448.  
  449.    var->data.location = slot;
  450.    var->data.explicit_location = (slot >= 0);
  451.    var->data.explicit_index = 0;
  452.  
  453.    /* Once the variable is created an initialized, add it to the symbol table
  454.     * and add the declaration to the IR stream.
  455.     */
  456.    instructions->push_tail(var);
  457.  
  458.    symtab->add_variable(var);
  459.    return var;
  460. }
  461.  
  462.  
  463. ir_variable *
  464. builtin_variable_generator::add_uniform(const glsl_type *type,
  465.                                         const char *name)
  466. {
  467.    ir_variable *const uni = add_variable(name, type, ir_var_uniform, -1);
  468.  
  469.    unsigned i;
  470.    for (i = 0; _mesa_builtin_uniform_desc[i].name != NULL; i++) {
  471.       if (strcmp(_mesa_builtin_uniform_desc[i].name, name) == 0) {
  472.          break;
  473.       }
  474.    }
  475.  
  476.    assert(_mesa_builtin_uniform_desc[i].name != NULL);
  477.    const struct gl_builtin_uniform_desc* const statevar =
  478.       &_mesa_builtin_uniform_desc[i];
  479.  
  480.    const unsigned array_count = type->is_array() ? type->length : 1;
  481.  
  482.    ir_state_slot *slots =
  483.       uni->allocate_state_slots(array_count * statevar->num_elements);
  484.  
  485.    for (unsigned a = 0; a < array_count; a++) {
  486.       for (unsigned j = 0; j < statevar->num_elements; j++) {
  487.          const struct gl_builtin_uniform_element *element =
  488.             &statevar->elements[j];
  489.  
  490.          memcpy(slots->tokens, element->tokens, sizeof(element->tokens));
  491.          if (type->is_array()) {
  492.             if (strcmp(name, "gl_CurrentAttribVertMESA") == 0 ||
  493.                 strcmp(name, "gl_CurrentAttribFragMESA") == 0) {
  494.                slots->tokens[2] = a;
  495.             } else {
  496.                slots->tokens[1] = a;
  497.             }
  498.          }
  499.  
  500.          slots->swizzle = element->swizzle;
  501.          slots++;
  502.       }
  503.    }
  504.  
  505.    return uni;
  506. }
  507.  
  508.  
  509. ir_variable *
  510. builtin_variable_generator::add_const(const char *name, int value)
  511. {
  512.    ir_variable *const var = add_variable(name, glsl_type::int_type,
  513.                                          ir_var_auto, -1);
  514.    var->constant_value = new(var) ir_constant(value);
  515.    var->constant_initializer = new(var) ir_constant(value);
  516.    var->data.has_initializer = true;
  517.    return var;
  518. }
  519.  
  520.  
  521. ir_variable *
  522. builtin_variable_generator::add_const_ivec3(const char *name, int x, int y,
  523.                                             int z)
  524. {
  525.    ir_variable *const var = add_variable(name, glsl_type::ivec3_type,
  526.                                          ir_var_auto, -1);
  527.    ir_constant_data data;
  528.    memset(&data, 0, sizeof(data));
  529.    data.i[0] = x;
  530.    data.i[1] = y;
  531.    data.i[2] = z;
  532.    var->constant_value = new(var) ir_constant(glsl_type::ivec3_type, &data);
  533.    var->constant_initializer =
  534.       new(var) ir_constant(glsl_type::ivec3_type, &data);
  535.    var->data.has_initializer = true;
  536.    return var;
  537. }
  538.  
  539.  
  540. void
  541. builtin_variable_generator::generate_constants()
  542. {
  543.    add_const("gl_MaxVertexAttribs", state->Const.MaxVertexAttribs);
  544.    add_const("gl_MaxVertexTextureImageUnits",
  545.              state->Const.MaxVertexTextureImageUnits);
  546.    add_const("gl_MaxCombinedTextureImageUnits",
  547.              state->Const.MaxCombinedTextureImageUnits);
  548.    add_const("gl_MaxTextureImageUnits", state->Const.MaxTextureImageUnits);
  549.    add_const("gl_MaxDrawBuffers", state->Const.MaxDrawBuffers);
  550.  
  551.    /* Max uniforms/varyings: GLSL ES counts these in units of vectors; desktop
  552.     * GL counts them in units of "components" or "floats".
  553.     */
  554.    if (state->es_shader) {
  555.       add_const("gl_MaxVertexUniformVectors",
  556.                 state->Const.MaxVertexUniformComponents / 4);
  557.       add_const("gl_MaxFragmentUniformVectors",
  558.                 state->Const.MaxFragmentUniformComponents / 4);
  559.  
  560.       /* In GLSL ES 3.00, gl_MaxVaryingVectors was split out to separate
  561.        * vertex and fragment shader constants.
  562.        */
  563.       if (state->is_version(0, 300)) {
  564.          add_const("gl_MaxVertexOutputVectors",
  565.                    state->ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4);
  566.          add_const("gl_MaxFragmentInputVectors",
  567.                    state->ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents / 4);
  568.       } else {
  569.          add_const("gl_MaxVaryingVectors",
  570.                    state->ctx->Const.MaxVarying);
  571.       }
  572.    } else {
  573.       add_const("gl_MaxVertexUniformComponents",
  574.                 state->Const.MaxVertexUniformComponents);
  575.  
  576.       /* Note: gl_MaxVaryingFloats was deprecated in GLSL 1.30+, but not
  577.        * removed
  578.        */
  579.       add_const("gl_MaxVaryingFloats", state->ctx->Const.MaxVarying * 4);
  580.  
  581.       add_const("gl_MaxFragmentUniformComponents",
  582.                 state->Const.MaxFragmentUniformComponents);
  583.    }
  584.  
  585.    /* Texel offsets were introduced in ARB_shading_language_420pack (which
  586.     * requires desktop GLSL version 130), and adopted into desktop GLSL
  587.     * version 4.20 and GLSL ES version 3.00.
  588.     */
  589.    if ((state->is_version(130, 0) &&
  590.         state->ARB_shading_language_420pack_enable) ||
  591.       state->is_version(420, 300)) {
  592.       add_const("gl_MinProgramTexelOffset",
  593.                 state->Const.MinProgramTexelOffset);
  594.       add_const("gl_MaxProgramTexelOffset",
  595.                 state->Const.MaxProgramTexelOffset);
  596.    }
  597.  
  598.    if (state->is_version(130, 0)) {
  599.       add_const("gl_MaxClipDistances", state->Const.MaxClipPlanes);
  600.       add_const("gl_MaxVaryingComponents", state->ctx->Const.MaxVarying * 4);
  601.    }
  602.  
  603.    if (state->is_version(150, 0)) {
  604.       add_const("gl_MaxVertexOutputComponents",
  605.                 state->Const.MaxVertexOutputComponents);
  606.       add_const("gl_MaxGeometryInputComponents",
  607.                 state->Const.MaxGeometryInputComponents);
  608.       add_const("gl_MaxGeometryOutputComponents",
  609.                 state->Const.MaxGeometryOutputComponents);
  610.       add_const("gl_MaxFragmentInputComponents",
  611.                 state->Const.MaxFragmentInputComponents);
  612.       add_const("gl_MaxGeometryTextureImageUnits",
  613.                 state->Const.MaxGeometryTextureImageUnits);
  614.       add_const("gl_MaxGeometryOutputVertices",
  615.                 state->Const.MaxGeometryOutputVertices);
  616.       add_const("gl_MaxGeometryTotalOutputComponents",
  617.                 state->Const.MaxGeometryTotalOutputComponents);
  618.       add_const("gl_MaxGeometryUniformComponents",
  619.                 state->Const.MaxGeometryUniformComponents);
  620.  
  621.       /* Note: the GLSL 1.50-4.40 specs require
  622.        * gl_MaxGeometryVaryingComponents to be present, and to be at least 64.
  623.        * But they do not define what it means (and there does not appear to be
  624.        * any corresponding constant in the GL specs).  However,
  625.        * ARB_geometry_shader4 defines MAX_GEOMETRY_VARYING_COMPONENTS_ARB to
  626.        * be the maximum number of components available for use as geometry
  627.        * outputs.  So we assume this is a synonym for
  628.        * gl_MaxGeometryOutputComponents.
  629.        */
  630.       add_const("gl_MaxGeometryVaryingComponents",
  631.                 state->Const.MaxGeometryOutputComponents);
  632.    }
  633.  
  634.    if (compatibility) {
  635.       /* Note: gl_MaxLights stopped being listed as an explicit constant in
  636.        * GLSL 1.30, however it continues to be referred to (as a minimum size
  637.        * for compatibility-mode uniforms) all the way up through GLSL 4.30, so
  638.        * this seems like it was probably an oversight.
  639.        */
  640.       add_const("gl_MaxLights", state->Const.MaxLights);
  641.  
  642.       add_const("gl_MaxClipPlanes", state->Const.MaxClipPlanes);
  643.  
  644.       /* Note: gl_MaxTextureUnits wasn't made compatibility-only until GLSL
  645.        * 1.50, however this seems like it was probably an oversight.
  646.        */
  647.       add_const("gl_MaxTextureUnits", state->Const.MaxTextureUnits);
  648.  
  649.       /* Note: gl_MaxTextureCoords was left out of GLSL 1.40, but it was
  650.        * re-introduced in GLSL 1.50, so this seems like it was probably an
  651.        * oversight.
  652.        */
  653.       add_const("gl_MaxTextureCoords", state->Const.MaxTextureCoords);
  654.    }
  655.  
  656.    if (state->has_atomic_counters()) {
  657.       add_const("gl_MaxVertexAtomicCounters",
  658.                 state->Const.MaxVertexAtomicCounters);
  659.       add_const("gl_MaxFragmentAtomicCounters",
  660.                 state->Const.MaxFragmentAtomicCounters);
  661.       add_const("gl_MaxCombinedAtomicCounters",
  662.                 state->Const.MaxCombinedAtomicCounters);
  663.       add_const("gl_MaxAtomicCounterBindings",
  664.                 state->Const.MaxAtomicBufferBindings);
  665.  
  666.       /* When Mesa adds support for GL_OES_geometry_shader and
  667.        * GL_OES_tessellation_shader, this will need to change.
  668.        */
  669.       if (!state->es_shader) {
  670.          add_const("gl_MaxGeometryAtomicCounters",
  671.                    state->Const.MaxGeometryAtomicCounters);
  672.          add_const("gl_MaxTessControlAtomicCounters", 0);
  673.          add_const("gl_MaxTessEvaluationAtomicCounters", 0);
  674.       }
  675.    }
  676.  
  677.    if (state->is_version(420, 310)) {
  678.       add_const("gl_MaxVertexAtomicCounterBuffers",
  679.                 state->Const.MaxVertexAtomicCounterBuffers);
  680.       add_const("gl_MaxFragmentAtomicCounterBuffers",
  681.                 state->Const.MaxFragmentAtomicCounterBuffers);
  682.       add_const("gl_MaxCombinedAtomicCounterBuffers",
  683.                 state->Const.MaxCombinedAtomicCounterBuffers);
  684.       add_const("gl_MaxAtomicCounterBufferSize",
  685.                 state->Const.MaxAtomicCounterBufferSize);
  686.  
  687.       /* When Mesa adds support for GL_OES_geometry_shader and
  688.        * GL_OES_tessellation_shader, this will need to change.
  689.        */
  690.       if (!state->es_shader) {
  691.          add_const("gl_MaxGeometryAtomicCounterBuffers",
  692.                    state->Const.MaxGeometryAtomicCounterBuffers);
  693.          add_const("gl_MaxTessControlAtomicCounterBuffers", 0);
  694.          add_const("gl_MaxTessEvaluationAtomicCounterBuffers", 0);
  695.       }
  696.    }
  697.  
  698.    if (state->is_version(430, 0) || state->ARB_compute_shader_enable) {
  699.       add_const("gl_MaxComputeAtomicCounterBuffers", MAX_COMPUTE_ATOMIC_COUNTER_BUFFERS);
  700.       add_const("gl_MaxComputeAtomicCounters", MAX_COMPUTE_ATOMIC_COUNTERS);
  701.       add_const("gl_MaxComputeImageUniforms", MAX_COMPUTE_IMAGE_UNIFORMS);
  702.       add_const("gl_MaxComputeTextureImageUnits", MAX_COMPUTE_TEXTURE_IMAGE_UNITS);
  703.       add_const("gl_MaxComputeUniformComponents", MAX_COMPUTE_UNIFORM_COMPONENTS);
  704.  
  705.       add_const_ivec3("gl_MaxComputeWorkGroupCount",
  706.                       state->Const.MaxComputeWorkGroupCount[0],
  707.                       state->Const.MaxComputeWorkGroupCount[1],
  708.                       state->Const.MaxComputeWorkGroupCount[2]);
  709.       add_const_ivec3("gl_MaxComputeWorkGroupSize",
  710.                       state->Const.MaxComputeWorkGroupSize[0],
  711.                       state->Const.MaxComputeWorkGroupSize[1],
  712.                       state->Const.MaxComputeWorkGroupSize[2]);
  713.  
  714.       /* From the GLSL 4.40 spec, section 7.1 (Built-In Language Variables):
  715.        *
  716.        *     The built-in constant gl_WorkGroupSize is a compute-shader
  717.        *     constant containing the local work-group size of the shader.  The
  718.        *     size of the work group in the X, Y, and Z dimensions is stored in
  719.        *     the x, y, and z components.  The constants values in
  720.        *     gl_WorkGroupSize will match those specified in the required
  721.        *     local_size_x, local_size_y, and local_size_z layout qualifiers
  722.        *     for the current shader.  This is a constant so that it can be
  723.        *     used to size arrays of memory that can be shared within the local
  724.        *     work group.  It is a compile-time error to use gl_WorkGroupSize
  725.        *     in a shader that does not declare a fixed local group size, or
  726.        *     before that shader has declared a fixed local group size, using
  727.        *     local_size_x, local_size_y, and local_size_z.
  728.        *
  729.        * To prevent the shader from trying to refer to gl_WorkGroupSize before
  730.        * the layout declaration, we don't define it here.  Intead we define it
  731.        * in ast_cs_input_layout::hir().
  732.        */
  733.    }
  734.  
  735.    if (state->is_version(420, 0) ||
  736.        state->ARB_shader_image_load_store_enable) {
  737.       add_const("gl_MaxImageUnits",
  738.                 state->Const.MaxImageUnits);
  739.       add_const("gl_MaxCombinedImageUnitsAndFragmentOutputs",
  740.                 state->Const.MaxCombinedImageUnitsAndFragmentOutputs);
  741.       add_const("gl_MaxImageSamples",
  742.                 state->Const.MaxImageSamples);
  743.       add_const("gl_MaxVertexImageUniforms",
  744.                 state->Const.MaxVertexImageUniforms);
  745.       add_const("gl_MaxTessControlImageUniforms", 0);
  746.       add_const("gl_MaxTessEvaluationImageUniforms", 0);
  747.       add_const("gl_MaxGeometryImageUniforms",
  748.                 state->Const.MaxGeometryImageUniforms);
  749.       add_const("gl_MaxFragmentImageUniforms",
  750.                 state->Const.MaxFragmentImageUniforms);
  751.       add_const("gl_MaxCombinedImageUniforms",
  752.                 state->Const.MaxCombinedImageUniforms);
  753.    }
  754.  
  755.    if (state->is_version(410, 0) ||
  756.        state->ARB_viewport_array_enable)
  757.       add_const("gl_MaxViewports", state->Const.MaxViewports);
  758. }
  759.  
  760.  
  761. /**
  762.  * Generate uniform variables (which exist in all types of shaders).
  763.  */
  764. void
  765. builtin_variable_generator::generate_uniforms()
  766. {
  767.    add_uniform(int_t, "gl_NumSamples");
  768.    add_uniform(type("gl_DepthRangeParameters"), "gl_DepthRange");
  769.    add_uniform(array(vec4_t, VERT_ATTRIB_MAX), "gl_CurrentAttribVertMESA");
  770.    add_uniform(array(vec4_t, VARYING_SLOT_MAX), "gl_CurrentAttribFragMESA");
  771.  
  772.    if (compatibility) {
  773.       add_uniform(mat4_t, "gl_ModelViewMatrix");
  774.       add_uniform(mat4_t, "gl_ProjectionMatrix");
  775.       add_uniform(mat4_t, "gl_ModelViewProjectionMatrix");
  776.       add_uniform(mat3_t, "gl_NormalMatrix");
  777.       add_uniform(mat4_t, "gl_ModelViewMatrixInverse");
  778.       add_uniform(mat4_t, "gl_ProjectionMatrixInverse");
  779.       add_uniform(mat4_t, "gl_ModelViewProjectionMatrixInverse");
  780.       add_uniform(mat4_t, "gl_ModelViewMatrixTranspose");
  781.       add_uniform(mat4_t, "gl_ProjectionMatrixTranspose");
  782.       add_uniform(mat4_t, "gl_ModelViewProjectionMatrixTranspose");
  783.       add_uniform(mat4_t, "gl_ModelViewMatrixInverseTranspose");
  784.       add_uniform(mat4_t, "gl_ProjectionMatrixInverseTranspose");
  785.       add_uniform(mat4_t, "gl_ModelViewProjectionMatrixInverseTranspose");
  786.       add_uniform(float_t, "gl_NormalScale");
  787.       add_uniform(type("gl_LightModelParameters"), "gl_LightModel");
  788.       add_uniform(vec4_t, "gl_FogParamsOptimizedMESA");
  789.  
  790.       const glsl_type *const mat4_array_type =
  791.          array(mat4_t, state->Const.MaxTextureCoords);
  792.       add_uniform(mat4_array_type, "gl_TextureMatrix");
  793.       add_uniform(mat4_array_type, "gl_TextureMatrixInverse");
  794.       add_uniform(mat4_array_type, "gl_TextureMatrixTranspose");
  795.       add_uniform(mat4_array_type, "gl_TextureMatrixInverseTranspose");
  796.  
  797.       add_uniform(array(vec4_t, state->Const.MaxClipPlanes), "gl_ClipPlane");
  798.       add_uniform(type("gl_PointParameters"), "gl_Point");
  799.  
  800.       const glsl_type *const material_parameters_type =
  801.          type("gl_MaterialParameters");
  802.       add_uniform(material_parameters_type, "gl_FrontMaterial");
  803.       add_uniform(material_parameters_type, "gl_BackMaterial");
  804.  
  805.       add_uniform(array(type("gl_LightSourceParameters"),
  806.                         state->Const.MaxLights),
  807.                   "gl_LightSource");
  808.  
  809.       const glsl_type *const light_model_products_type =
  810.          type("gl_LightModelProducts");
  811.       add_uniform(light_model_products_type, "gl_FrontLightModelProduct");
  812.       add_uniform(light_model_products_type, "gl_BackLightModelProduct");
  813.  
  814.       const glsl_type *const light_products_type =
  815.          array(type("gl_LightProducts"), state->Const.MaxLights);
  816.       add_uniform(light_products_type, "gl_FrontLightProduct");
  817.       add_uniform(light_products_type, "gl_BackLightProduct");
  818.  
  819.       add_uniform(array(vec4_t, state->Const.MaxTextureUnits),
  820.                   "gl_TextureEnvColor");
  821.  
  822.       const glsl_type *const texcoords_vec4 =
  823.          array(vec4_t, state->Const.MaxTextureCoords);
  824.       add_uniform(texcoords_vec4, "gl_EyePlaneS");
  825.       add_uniform(texcoords_vec4, "gl_EyePlaneT");
  826.       add_uniform(texcoords_vec4, "gl_EyePlaneR");
  827.       add_uniform(texcoords_vec4, "gl_EyePlaneQ");
  828.       add_uniform(texcoords_vec4, "gl_ObjectPlaneS");
  829.       add_uniform(texcoords_vec4, "gl_ObjectPlaneT");
  830.       add_uniform(texcoords_vec4, "gl_ObjectPlaneR");
  831.       add_uniform(texcoords_vec4, "gl_ObjectPlaneQ");
  832.  
  833.       add_uniform(type("gl_FogParameters"), "gl_Fog");
  834.    }
  835. }
  836.  
  837.  
  838. /**
  839.  * Generate variables which only exist in vertex shaders.
  840.  */
  841. void
  842. builtin_variable_generator::generate_vs_special_vars()
  843. {
  844.    if (state->is_version(130, 300))
  845.       add_system_value(SYSTEM_VALUE_VERTEX_ID, int_t, "gl_VertexID");
  846.    if (state->ARB_draw_instanced_enable)
  847.       add_system_value(SYSTEM_VALUE_INSTANCE_ID, int_t, "gl_InstanceIDARB");
  848.    if (state->ARB_draw_instanced_enable || state->is_version(140, 300))
  849.       add_system_value(SYSTEM_VALUE_INSTANCE_ID, int_t, "gl_InstanceID");
  850.    if (state->AMD_vertex_shader_layer_enable)
  851.       add_output(VARYING_SLOT_LAYER, int_t, "gl_Layer");
  852.    if (state->AMD_vertex_shader_viewport_index_enable)
  853.       add_output(VARYING_SLOT_VIEWPORT, int_t, "gl_ViewportIndex");
  854.    if (compatibility) {
  855.       add_input(VERT_ATTRIB_POS, vec4_t, "gl_Vertex");
  856.       add_input(VERT_ATTRIB_NORMAL, vec3_t, "gl_Normal");
  857.       add_input(VERT_ATTRIB_COLOR0, vec4_t, "gl_Color");
  858.       add_input(VERT_ATTRIB_COLOR1, vec4_t, "gl_SecondaryColor");
  859.       add_input(VERT_ATTRIB_TEX0, vec4_t, "gl_MultiTexCoord0");
  860.       add_input(VERT_ATTRIB_TEX1, vec4_t, "gl_MultiTexCoord1");
  861.       add_input(VERT_ATTRIB_TEX2, vec4_t, "gl_MultiTexCoord2");
  862.       add_input(VERT_ATTRIB_TEX3, vec4_t, "gl_MultiTexCoord3");
  863.       add_input(VERT_ATTRIB_TEX4, vec4_t, "gl_MultiTexCoord4");
  864.       add_input(VERT_ATTRIB_TEX5, vec4_t, "gl_MultiTexCoord5");
  865.       add_input(VERT_ATTRIB_TEX6, vec4_t, "gl_MultiTexCoord6");
  866.       add_input(VERT_ATTRIB_TEX7, vec4_t, "gl_MultiTexCoord7");
  867.       add_input(VERT_ATTRIB_FOG, float_t, "gl_FogCoord");
  868.    }
  869. }
  870.  
  871.  
  872. /**
  873.  * Generate variables which only exist in geometry shaders.
  874.  */
  875. void
  876. builtin_variable_generator::generate_gs_special_vars()
  877. {
  878.    add_output(VARYING_SLOT_LAYER, int_t, "gl_Layer");
  879.    if (state->ARB_viewport_array_enable)
  880.       add_output(VARYING_SLOT_VIEWPORT, int_t, "gl_ViewportIndex");
  881.    if (state->ARB_gpu_shader5_enable)
  882.       add_system_value(SYSTEM_VALUE_INVOCATION_ID, int_t, "gl_InvocationID");
  883.  
  884.    /* Although gl_PrimitiveID appears in tessellation control and tessellation
  885.     * evaluation shaders, it has a different function there than it has in
  886.     * geometry shaders, so we treat it (and its counterpart gl_PrimitiveIDIn)
  887.     * as special geometry shader variables.
  888.     *
  889.     * Note that although the general convention of suffixing geometry shader
  890.     * input varyings with "In" was not adopted into GLSL 1.50, it is used in
  891.     * the specific case of gl_PrimitiveIDIn.  So we don't need to treat
  892.     * gl_PrimitiveIDIn as an {ARB,EXT}_geometry_shader4-only variable.
  893.     */
  894.    ir_variable *var;
  895.    var = add_input(VARYING_SLOT_PRIMITIVE_ID, int_t, "gl_PrimitiveIDIn");
  896.    var->data.interpolation = INTERP_QUALIFIER_FLAT;
  897.    var = add_output(VARYING_SLOT_PRIMITIVE_ID, int_t, "gl_PrimitiveID");
  898.    var->data.interpolation = INTERP_QUALIFIER_FLAT;
  899. }
  900.  
  901.  
  902. /**
  903.  * Generate variables which only exist in fragment shaders.
  904.  */
  905. void
  906. builtin_variable_generator::generate_fs_special_vars()
  907. {
  908.    add_input(VARYING_SLOT_POS, vec4_t, "gl_FragCoord");
  909.    add_input(VARYING_SLOT_FACE, bool_t, "gl_FrontFacing");
  910.    if (state->is_version(120, 100))
  911.       add_input(VARYING_SLOT_PNTC, vec2_t, "gl_PointCoord");
  912.  
  913.    if (state->is_version(150, 0)) {
  914.       ir_variable *var =
  915.          add_input(VARYING_SLOT_PRIMITIVE_ID, int_t, "gl_PrimitiveID");
  916.       var->data.interpolation = INTERP_QUALIFIER_FLAT;
  917.    }
  918.  
  919.    /* gl_FragColor and gl_FragData were deprecated starting in desktop GLSL
  920.     * 1.30, and were relegated to the compatibility profile in GLSL 4.20.
  921.     * They were removed from GLSL ES 3.00.
  922.     */
  923.    if (compatibility || !state->is_version(420, 300)) {
  924.       add_output(FRAG_RESULT_COLOR, vec4_t, "gl_FragColor");
  925.       add_output(FRAG_RESULT_DATA0,
  926.                  array(vec4_t, state->Const.MaxDrawBuffers), "gl_FragData");
  927.    }
  928.  
  929.    /* gl_FragDepth has always been in desktop GLSL, but did not appear in GLSL
  930.     * ES 1.00.
  931.     */
  932.    if (state->is_version(110, 300))
  933.       add_output(FRAG_RESULT_DEPTH, float_t, "gl_FragDepth");
  934.  
  935.    if (state->ARB_shader_stencil_export_enable) {
  936.       ir_variable *const var =
  937.          add_output(FRAG_RESULT_STENCIL, int_t, "gl_FragStencilRefARB");
  938.       if (state->ARB_shader_stencil_export_warn)
  939.          var->enable_extension_warning("GL_ARB_shader_stencil_export");
  940.    }
  941.  
  942.    if (state->AMD_shader_stencil_export_enable) {
  943.       ir_variable *const var =
  944.          add_output(FRAG_RESULT_STENCIL, int_t, "gl_FragStencilRefAMD");
  945.       if (state->AMD_shader_stencil_export_warn)
  946.          var->enable_extension_warning("GL_AMD_shader_stencil_export");
  947.    }
  948.  
  949.    if (state->ARB_sample_shading_enable) {
  950.       add_system_value(SYSTEM_VALUE_SAMPLE_ID, int_t, "gl_SampleID");
  951.       add_system_value(SYSTEM_VALUE_SAMPLE_POS, vec2_t, "gl_SamplePosition");
  952.       /* From the ARB_sample_shading specification:
  953.        *    "The number of elements in the array is ceil(<s>/32), where
  954.        *    <s> is the maximum number of color samples supported by the
  955.        *    implementation."
  956.        * Since no drivers expose more than 32x MSAA, we can simply set
  957.        * the array size to 1 rather than computing it.
  958.        */
  959.       add_output(FRAG_RESULT_SAMPLE_MASK, array(int_t, 1), "gl_SampleMask");
  960.    }
  961.  
  962.    if (state->ARB_gpu_shader5_enable) {
  963.       add_system_value(SYSTEM_VALUE_SAMPLE_MASK_IN, array(int_t, 1), "gl_SampleMaskIn");
  964.    }
  965.  
  966.    if (state->ARB_fragment_layer_viewport_enable) {
  967.       add_input(VARYING_SLOT_LAYER, int_t, "gl_Layer");
  968.       add_input(VARYING_SLOT_VIEWPORT, int_t, "gl_ViewportIndex");
  969.    }
  970. }
  971.  
  972.  
  973. /**
  974.  * Generate variables which only exist in compute shaders.
  975.  */
  976. void
  977. builtin_variable_generator::generate_cs_special_vars()
  978. {
  979.    /* TODO: finish this. */
  980. }
  981.  
  982.  
  983. /**
  984.  * Add a single "varying" variable.  The variable's type and direction (input
  985.  * or output) are adjusted as appropriate for the type of shader being
  986.  * compiled.  For geometry shaders using {ARB,EXT}_geometry_shader4,
  987.  * name_as_gs_input is used for the input (to avoid ambiguity).
  988.  */
  989. void
  990. builtin_variable_generator::add_varying(int slot, const glsl_type *type,
  991.                                         const char *name,
  992.                                         const char *name_as_gs_input)
  993. {
  994.    switch (state->stage) {
  995.    case MESA_SHADER_GEOMETRY:
  996.       this->per_vertex_in.add_field(slot, type, name);
  997.       /* FALLTHROUGH */
  998.    case MESA_SHADER_VERTEX:
  999.       this->per_vertex_out.add_field(slot, type, name);
  1000.       break;
  1001.    case MESA_SHADER_FRAGMENT:
  1002.       add_input(slot, type, name);
  1003.       break;
  1004.    case MESA_SHADER_COMPUTE:
  1005.       /* Compute shaders don't have varyings. */
  1006.       break;
  1007.    }
  1008. }
  1009.  
  1010.  
  1011. /**
  1012.  * Generate variables that are used to communicate data from one shader stage
  1013.  * to the next ("varyings").
  1014.  */
  1015. void
  1016. builtin_variable_generator::generate_varyings()
  1017. {
  1018. #define ADD_VARYING(loc, type, name) \
  1019.    add_varying(loc, type, name, name "In")
  1020.  
  1021.    /* gl_Position and gl_PointSize are not visible from fragment shaders. */
  1022.    if (state->stage != MESA_SHADER_FRAGMENT) {
  1023.       ADD_VARYING(VARYING_SLOT_POS, vec4_t, "gl_Position");
  1024.       ADD_VARYING(VARYING_SLOT_PSIZ, float_t, "gl_PointSize");
  1025.    }
  1026.  
  1027.    if (state->is_version(130, 0)) {
  1028.        ADD_VARYING(VARYING_SLOT_CLIP_DIST0, array(float_t, 0),
  1029.                    "gl_ClipDistance");
  1030.    }
  1031.  
  1032.    if (compatibility) {
  1033.       ADD_VARYING(VARYING_SLOT_TEX0, array(vec4_t, 0), "gl_TexCoord");
  1034.       ADD_VARYING(VARYING_SLOT_FOGC, float_t, "gl_FogFragCoord");
  1035.       if (state->stage == MESA_SHADER_FRAGMENT) {
  1036.          ADD_VARYING(VARYING_SLOT_COL0, vec4_t, "gl_Color");
  1037.          ADD_VARYING(VARYING_SLOT_COL1, vec4_t, "gl_SecondaryColor");
  1038.       } else {
  1039.          ADD_VARYING(VARYING_SLOT_CLIP_VERTEX, vec4_t, "gl_ClipVertex");
  1040.          ADD_VARYING(VARYING_SLOT_COL0, vec4_t, "gl_FrontColor");
  1041.          ADD_VARYING(VARYING_SLOT_BFC0, vec4_t, "gl_BackColor");
  1042.          ADD_VARYING(VARYING_SLOT_COL1, vec4_t, "gl_FrontSecondaryColor");
  1043.          ADD_VARYING(VARYING_SLOT_BFC1, vec4_t, "gl_BackSecondaryColor");
  1044.       }
  1045.    }
  1046.  
  1047.    if (state->stage == MESA_SHADER_GEOMETRY) {
  1048.       const glsl_type *per_vertex_in_type =
  1049.          this->per_vertex_in.construct_interface_instance();
  1050.       add_variable("gl_in", array(per_vertex_in_type, 0),
  1051.                    ir_var_shader_in, -1);
  1052.    }
  1053.    if (state->stage == MESA_SHADER_VERTEX || state->stage == MESA_SHADER_GEOMETRY) {
  1054.       const glsl_type *per_vertex_out_type =
  1055.          this->per_vertex_out.construct_interface_instance();
  1056.       const glsl_struct_field *fields = per_vertex_out_type->fields.structure;
  1057.       for (unsigned i = 0; i < per_vertex_out_type->length; i++) {
  1058.          ir_variable *var =
  1059.             add_variable(fields[i].name, fields[i].type, ir_var_shader_out,
  1060.                          fields[i].location);
  1061.          var->data.interpolation = fields[i].interpolation;
  1062.          var->data.centroid = fields[i].centroid;
  1063.          var->data.sample = fields[i].sample;
  1064.          var->init_interface_type(per_vertex_out_type);
  1065.       }
  1066.    }
  1067. }
  1068.  
  1069.  
  1070. }; /* Anonymous namespace */
  1071.  
  1072.  
  1073. void
  1074. _mesa_glsl_initialize_variables(exec_list *instructions,
  1075.                                 struct _mesa_glsl_parse_state *state)
  1076. {
  1077.    builtin_variable_generator gen(instructions, state);
  1078.  
  1079.    gen.generate_constants();
  1080.    gen.generate_uniforms();
  1081.  
  1082.    gen.generate_varyings();
  1083.  
  1084.    switch (state->stage) {
  1085.    case MESA_SHADER_VERTEX:
  1086.       gen.generate_vs_special_vars();
  1087.       break;
  1088.    case MESA_SHADER_GEOMETRY:
  1089.       gen.generate_gs_special_vars();
  1090.       break;
  1091.    case MESA_SHADER_FRAGMENT:
  1092.       gen.generate_fs_special_vars();
  1093.       break;
  1094.    case MESA_SHADER_COMPUTE:
  1095.       gen.generate_cs_special_vars();
  1096.       break;
  1097.    }
  1098. }
  1099.