Home | History | Annotate | Download | only in i965
      1 /*
      2  * Copyright  2015 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 DEALINGS
     21  * IN THE SOFTWARE.
     22  */
     23 
     24 #include "brw_shader.h"
     25 #include "brw_nir.h"
     26 #include "compiler/glsl/ir_uniform.h"
     27 
     28 static void
     29 brw_nir_setup_glsl_builtin_uniform(nir_variable *var,
     30                                    const struct gl_program *prog,
     31                                    struct brw_stage_prog_data *stage_prog_data,
     32                                    bool is_scalar)
     33 {
     34    const nir_state_slot *const slots = var->state_slots;
     35    assert(var->state_slots != NULL);
     36 
     37    unsigned uniform_index = var->data.driver_location / 4;
     38    for (unsigned int i = 0; i < var->num_state_slots; i++) {
     39       /* This state reference has already been setup by ir_to_mesa, but we'll
     40        * get the same index back here.
     41        */
     42       int index = _mesa_add_state_reference(prog->Parameters,
     43 					    (gl_state_index *)slots[i].tokens);
     44 
     45       /* Add each of the unique swizzles of the element as a parameter.
     46        * This'll end up matching the expected layout of the
     47        * array/matrix/structure we're trying to fill in.
     48        */
     49       int last_swiz = -1;
     50       for (unsigned j = 0; j < 4; j++) {
     51          int swiz = GET_SWZ(slots[i].swizzle, j);
     52 
     53          /* If we hit a pair of identical swizzles, this means we've hit the
     54           * end of the builtin variable.  In scalar mode, we should just quit
     55           * and move on to the next one.  In vec4, we need to continue and pad
     56           * it out to 4 components.
     57           */
     58          if (swiz == last_swiz && is_scalar)
     59             break;
     60 
     61          last_swiz = swiz;
     62 
     63          stage_prog_data->param[uniform_index++] =
     64             &prog->Parameters->ParameterValues[index][swiz];
     65       }
     66    }
     67 }
     68 
     69 static void
     70 brw_nir_setup_glsl_uniform(gl_shader_stage stage, nir_variable *var,
     71                            const struct gl_program *prog,
     72                            struct brw_stage_prog_data *stage_prog_data,
     73                            bool is_scalar)
     74 {
     75    int namelen = strlen(var->name);
     76 
     77    /* The data for our (non-builtin) uniforms is stored in a series of
     78     * gl_uniform_storage structs for each subcomponent that
     79     * glGetUniformLocation() could name.  We know it's been set up in the same
     80     * order we'd walk the type, so walk the list of storage and find anything
     81     * with our name, or the prefix of a component that starts with our name.
     82     */
     83    unsigned uniform_index = var->data.driver_location / 4;
     84    for (unsigned u = 0; u < prog->sh.data->NumUniformStorage; u++) {
     85       struct gl_uniform_storage *storage =
     86          &prog->sh.data->UniformStorage[u];
     87 
     88       if (storage->builtin)
     89          continue;
     90 
     91       if (strncmp(var->name, storage->name, namelen) != 0 ||
     92           (storage->name[namelen] != 0 &&
     93            storage->name[namelen] != '.' &&
     94            storage->name[namelen] != '[')) {
     95          continue;
     96       }
     97 
     98       if (storage->type->is_image()) {
     99          brw_setup_image_uniform_values(stage, stage_prog_data,
    100                                         uniform_index, storage);
    101          uniform_index +=
    102             BRW_IMAGE_PARAM_SIZE * MAX2(storage->array_elements, 1);
    103       } else {
    104          gl_constant_value *components = storage->storage;
    105          unsigned vector_count = (MAX2(storage->array_elements, 1) *
    106                                   storage->type->matrix_columns);
    107          unsigned vector_size = storage->type->vector_elements;
    108          unsigned max_vector_size = 4;
    109          if (storage->type->base_type == GLSL_TYPE_DOUBLE) {
    110             vector_size *= 2;
    111             if (vector_size > 4)
    112                max_vector_size = 8;
    113          }
    114 
    115          for (unsigned s = 0; s < vector_count; s++) {
    116             unsigned i;
    117             for (i = 0; i < vector_size; i++) {
    118                stage_prog_data->param[uniform_index++] = components++;
    119             }
    120 
    121             if (!is_scalar) {
    122                /* Pad out with zeros if needed (only needed for vec4) */
    123                for (; i < max_vector_size; i++) {
    124                   static const gl_constant_value zero = { 0.0 };
    125                   stage_prog_data->param[uniform_index++] = &zero;
    126                }
    127             }
    128          }
    129       }
    130    }
    131 }
    132 
    133 void
    134 brw_nir_setup_glsl_uniforms(nir_shader *shader, const struct gl_program *prog,
    135                             struct brw_stage_prog_data *stage_prog_data,
    136                             bool is_scalar)
    137 {
    138    nir_foreach_variable(var, &shader->uniforms) {
    139       /* UBO's, atomics and samplers don't take up space in the
    140          uniform file */
    141       if (var->interface_type != NULL || var->type->contains_atomic())
    142          continue;
    143 
    144       if (strncmp(var->name, "gl_", 3) == 0) {
    145          brw_nir_setup_glsl_builtin_uniform(var, prog, stage_prog_data,
    146                                             is_scalar);
    147       } else {
    148          brw_nir_setup_glsl_uniform(shader->stage, var, prog, stage_prog_data,
    149                                     is_scalar);
    150       }
    151    }
    152 }
    153 
    154 void
    155 brw_nir_setup_arb_uniforms(nir_shader *shader, struct gl_program *prog,
    156                            struct brw_stage_prog_data *stage_prog_data)
    157 {
    158    struct gl_program_parameter_list *plist = prog->Parameters;
    159 
    160    /* For ARB programs, prog_to_nir generates a single "parameters" variable
    161     * for all uniform data.  nir_lower_wpos_ytransform may also create an
    162     * additional variable.
    163     */
    164    assert(shader->uniforms.length() <= 2);
    165 
    166    for (unsigned p = 0; p < plist->NumParameters; p++) {
    167       /* Parameters should be either vec4 uniforms or single component
    168        * constants; matrices and other larger types should have been broken
    169        * down earlier.
    170        */
    171       assert(plist->Parameters[p].Size <= 4);
    172 
    173       unsigned i;
    174       for (i = 0; i < plist->Parameters[p].Size; i++) {
    175          stage_prog_data->param[4 * p + i] = &plist->ParameterValues[p][i];
    176       }
    177       for (; i < 4; i++) {
    178          static const gl_constant_value zero = { 0.0 };
    179          stage_prog_data->param[4 * p + i] = &zero;
    180       }
    181    }
    182 }
    183