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