• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "compiler/brw_nir.h"
25 #include "compiler/glsl/ir_uniform.h"
26 #include "brw_program.h"
27 
28 static void
brw_nir_setup_glsl_builtin_uniform(nir_variable * var,const struct gl_program * prog,struct brw_stage_prog_data * stage_prog_data,bool is_scalar)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             BRW_PARAM_PARAMETER(index, swiz);
65       }
66    }
67 }
68 
69 static void
setup_vec4_image_param(uint32_t * params,uint32_t idx,unsigned offset,unsigned n)70 setup_vec4_image_param(uint32_t *params, uint32_t idx,
71                        unsigned offset, unsigned n)
72 {
73    assert(offset % sizeof(uint32_t) == 0);
74    for (unsigned i = 0; i < n; ++i)
75       params[i] = BRW_PARAM_IMAGE(idx, offset / sizeof(uint32_t) + i);
76 
77    for (unsigned i = n; i < 4; ++i)
78       params[i] = BRW_PARAM_BUILTIN_ZERO;
79 }
80 
81 static void
brw_setup_image_uniform_values(gl_shader_stage stage,struct brw_stage_prog_data * stage_prog_data,unsigned param_start_index,const gl_uniform_storage * storage)82 brw_setup_image_uniform_values(gl_shader_stage stage,
83                                struct brw_stage_prog_data *stage_prog_data,
84                                unsigned param_start_index,
85                                const gl_uniform_storage *storage)
86 {
87    uint32_t *param = &stage_prog_data->param[param_start_index];
88 
89    for (unsigned i = 0; i < MAX2(storage->array_elements, 1); i++) {
90       const unsigned image_idx = storage->opaque[stage].index + i;
91 
92       /* Upload the brw_image_param structure.  The order is expected to match
93        * the BRW_IMAGE_PARAM_*_OFFSET defines.
94        */
95       setup_vec4_image_param(param + BRW_IMAGE_PARAM_SURFACE_IDX_OFFSET,
96                              image_idx,
97                              offsetof(brw_image_param, surface_idx), 1);
98       setup_vec4_image_param(param + BRW_IMAGE_PARAM_OFFSET_OFFSET,
99                              image_idx,
100                              offsetof(brw_image_param, offset), 2);
101       setup_vec4_image_param(param + BRW_IMAGE_PARAM_SIZE_OFFSET,
102                              image_idx,
103                              offsetof(brw_image_param, size), 3);
104       setup_vec4_image_param(param + BRW_IMAGE_PARAM_STRIDE_OFFSET,
105                              image_idx,
106                              offsetof(brw_image_param, stride), 4);
107       setup_vec4_image_param(param + BRW_IMAGE_PARAM_TILING_OFFSET,
108                              image_idx,
109                              offsetof(brw_image_param, tiling), 3);
110       setup_vec4_image_param(param + BRW_IMAGE_PARAM_SWIZZLING_OFFSET,
111                              image_idx,
112                              offsetof(brw_image_param, swizzling), 2);
113       param += BRW_IMAGE_PARAM_SIZE;
114 
115       brw_mark_surface_used(
116          stage_prog_data,
117          stage_prog_data->binding_table.image_start + image_idx);
118    }
119 }
120 
121 static void
brw_nir_setup_glsl_uniform(gl_shader_stage stage,nir_variable * var,const struct gl_program * prog,struct brw_stage_prog_data * stage_prog_data,bool is_scalar)122 brw_nir_setup_glsl_uniform(gl_shader_stage stage, nir_variable *var,
123                            const struct gl_program *prog,
124                            struct brw_stage_prog_data *stage_prog_data,
125                            bool is_scalar)
126 {
127    int namelen = strlen(var->name);
128 
129    /* The data for our (non-builtin) uniforms is stored in a series of
130     * gl_uniform_storage structs for each subcomponent that
131     * glGetUniformLocation() could name.  We know it's been set up in the same
132     * order we'd walk the type, so walk the list of storage and find anything
133     * with our name, or the prefix of a component that starts with our name.
134     */
135    unsigned uniform_index = var->data.driver_location / 4;
136    for (unsigned u = 0; u < prog->sh.data->NumUniformStorage; u++) {
137       struct gl_uniform_storage *storage =
138          &prog->sh.data->UniformStorage[u];
139 
140       if (storage->builtin || storage->type->is_sampler())
141          continue;
142 
143       if (strncmp(var->name, storage->name, namelen) != 0 ||
144           (storage->name[namelen] != 0 &&
145            storage->name[namelen] != '.' &&
146            storage->name[namelen] != '[')) {
147          continue;
148       }
149 
150       if (storage->type->is_image()) {
151          brw_setup_image_uniform_values(stage, stage_prog_data,
152                                         uniform_index, storage);
153          uniform_index +=
154             BRW_IMAGE_PARAM_SIZE * MAX2(storage->array_elements, 1);
155       } else {
156          gl_constant_value *components = storage->storage;
157          unsigned vector_count = (MAX2(storage->array_elements, 1) *
158                                   storage->type->matrix_columns);
159          unsigned vector_size = storage->type->vector_elements;
160          unsigned max_vector_size = 4;
161          if (storage->type->base_type == GLSL_TYPE_DOUBLE ||
162              storage->type->base_type == GLSL_TYPE_UINT64 ||
163              storage->type->base_type == GLSL_TYPE_INT64) {
164             vector_size *= 2;
165             if (vector_size > 4)
166                max_vector_size = 8;
167          }
168 
169          for (unsigned s = 0; s < vector_count; s++) {
170             unsigned i;
171             for (i = 0; i < vector_size; i++) {
172                uint32_t idx = components - prog->sh.data->UniformDataSlots;
173                stage_prog_data->param[uniform_index++] = BRW_PARAM_UNIFORM(idx);
174                components++;
175             }
176 
177             if (!is_scalar) {
178                /* Pad out with zeros if needed (only needed for vec4) */
179                for (; i < max_vector_size; i++) {
180                   stage_prog_data->param[uniform_index++] =
181                      BRW_PARAM_BUILTIN_ZERO;
182                }
183             }
184          }
185       }
186    }
187 }
188 
189 void
brw_nir_setup_glsl_uniforms(void * mem_ctx,nir_shader * shader,const struct gl_program * prog,struct brw_stage_prog_data * stage_prog_data,bool is_scalar)190 brw_nir_setup_glsl_uniforms(void *mem_ctx, nir_shader *shader,
191                             const struct gl_program *prog,
192                             struct brw_stage_prog_data *stage_prog_data,
193                             bool is_scalar)
194 {
195    unsigned nr_params = shader->num_uniforms / 4;
196    stage_prog_data->nr_params = nr_params;
197    stage_prog_data->param = rzalloc_array(mem_ctx, uint32_t, nr_params);
198 
199    nir_foreach_variable(var, &shader->uniforms) {
200       /* UBO's, atomics and samplers don't take up space in the
201          uniform file */
202       if (var->interface_type != NULL || var->type->contains_atomic())
203          continue;
204 
205       if (strncmp(var->name, "gl_", 3) == 0) {
206          brw_nir_setup_glsl_builtin_uniform(var, prog, stage_prog_data,
207                                             is_scalar);
208       } else {
209          brw_nir_setup_glsl_uniform(shader->info.stage, var, prog,
210                                     stage_prog_data, is_scalar);
211       }
212    }
213 }
214 
215 void
brw_nir_setup_arb_uniforms(void * mem_ctx,nir_shader * shader,struct gl_program * prog,struct brw_stage_prog_data * stage_prog_data)216 brw_nir_setup_arb_uniforms(void *mem_ctx, nir_shader *shader,
217                            struct gl_program *prog,
218                            struct brw_stage_prog_data *stage_prog_data)
219 {
220    struct gl_program_parameter_list *plist = prog->Parameters;
221 
222    unsigned nr_params = plist->NumParameters * 4;
223    stage_prog_data->nr_params = nr_params;
224    stage_prog_data->param = rzalloc_array(mem_ctx, uint32_t, nr_params);
225 
226    /* For ARB programs, prog_to_nir generates a single "parameters" variable
227     * for all uniform data.  nir_lower_wpos_ytransform may also create an
228     * additional variable.
229     */
230    assert(shader->uniforms.length() <= 2);
231 
232    for (unsigned p = 0; p < plist->NumParameters; p++) {
233       /* Parameters should be either vec4 uniforms or single component
234        * constants; matrices and other larger types should have been broken
235        * down earlier.
236        */
237       assert(plist->Parameters[p].Size <= 4);
238 
239       unsigned i;
240       for (i = 0; i < plist->Parameters[p].Size; i++)
241          stage_prog_data->param[4 * p + i] = BRW_PARAM_PARAMETER(p, i);
242       for (; i < 4; i++)
243          stage_prog_data->param[4 * p + i] = BRW_PARAM_BUILTIN_ZERO;
244    }
245 }
246 
247 void
brw_nir_lower_patch_vertices_in_to_uniform(nir_shader * nir)248 brw_nir_lower_patch_vertices_in_to_uniform(nir_shader *nir)
249 {
250    nir_foreach_variable_safe(var, &nir->system_values) {
251       if (var->data.location != SYSTEM_VALUE_VERTICES_IN)
252          continue;
253 
254       gl_state_index tokens[STATE_LENGTH] = {
255          STATE_INTERNAL,
256          nir->info.stage == MESA_SHADER_TESS_CTRL ?
257             STATE_TCS_PATCH_VERTICES_IN : STATE_TES_PATCH_VERTICES_IN,
258       };
259       var->num_state_slots = 1;
260       var->state_slots =
261          ralloc_array(var, nir_state_slot, var->num_state_slots);
262       memcpy(var->state_slots[0].tokens, tokens, sizeof(tokens));
263       var->state_slots[0].swizzle = SWIZZLE_XXXX;
264 
265       var->data.mode = nir_var_uniform;
266       var->data.location = -1;
267       exec_node_remove(&var->node);
268       exec_list_push_tail(&nir->uniforms, &var->node);
269    }
270 }
271