2 * Copyright © 2010 Intel Corporation
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:
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
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
25 #include "main/macros.h"
26 #include "brw_context.h"
30 #include "glsl/ir_optimization.h"
31 #include "glsl/ir_print_visitor.h"
34 brw_new_shader(struct gl_context *ctx, GLuint name, GLuint type)
36 struct brw_shader *shader;
38 shader = rzalloc(NULL, struct brw_shader);
40 shader->base.Type = type;
41 shader->base.Name = name;
42 _mesa_init_shader(ctx, &shader->base);
48 struct gl_shader_program *
49 brw_new_shader_program(struct gl_context *ctx, GLuint name)
51 struct brw_shader_program *prog;
52 prog = rzalloc(NULL, struct brw_shader_program);
54 prog->base.Name = name;
55 _mesa_init_shader_program(ctx, &prog->base);
61 * Performs a compile of the shader stages even when we don't know
62 * what non-orthogonal state will be set, in the hope that it reflects
63 * the eventual NOS used, and thus allows us to produce link failures.
66 brw_shader_precompile(struct gl_context *ctx, struct gl_shader_program *prog)
68 struct brw_context *brw = brw_context(ctx);
70 if (brw->precompile && !brw_fs_precompile(ctx, prog))
73 if (!brw_vs_precompile(ctx, prog))
80 brw_link_shader(struct gl_context *ctx, struct gl_shader_program *shProg)
82 struct brw_context *brw = brw_context(ctx);
83 struct intel_context *intel = &brw->intel;
86 for (stage = 0; stage < ARRAY_SIZE(shProg->_LinkedShaders); stage++) {
87 struct brw_shader *shader =
88 (struct brw_shader *)shProg->_LinkedShaders[stage];
89 static const GLenum targets[] = {
90 GL_VERTEX_PROGRAM_ARB,
91 GL_FRAGMENT_PROGRAM_ARB,
92 GL_GEOMETRY_PROGRAM_NV
98 struct gl_program *prog =
99 ctx->Driver.NewProgram(ctx, targets[stage], shader->base.Name);
102 prog->Parameters = _mesa_new_parameter_list();
104 _mesa_generate_parameters_list_for_uniforms(shProg, &shader->base,
108 struct gl_vertex_program *vp = (struct gl_vertex_program *) prog;
109 vp->UsesClipDistance = shProg->Vert.UsesClipDistance;
112 void *mem_ctx = ralloc_context(NULL);
116 ralloc_free(shader->ir);
117 shader->ir = new(shader) exec_list;
118 clone_ir_list(mem_ctx, shader->ir, shader->base.ir);
120 do_mat_op_to_vec(shader->ir);
121 lower_instructions(shader->ir,
128 /* Pre-gen6 HW can only nest if-statements 16 deep. Beyond this,
129 * if-statements need to be flattened.
132 lower_if_to_cond_assign(shader->ir, 16);
134 do_lower_texture_projection(shader->ir);
135 brw_lower_texture_gradients(shader->ir);
136 do_vec_index_to_cond_assign(shader->ir);
137 brw_do_cubemap_normalize(shader->ir);
138 lower_noise(shader->ir);
139 lower_quadop_vector(shader->ir, false);
142 bool output = stage == MESA_SHADER_FRAGMENT;
143 bool temp = stage == MESA_SHADER_FRAGMENT;
144 bool uniform = stage == MESA_SHADER_FRAGMENT;
146 lower_variable_index_to_cond_assign(shader->ir,
147 input, output, temp, uniform);
152 if (stage == MESA_SHADER_FRAGMENT) {
153 brw_do_channel_expressions(shader->ir);
154 brw_do_vector_splitting(shader->ir);
157 progress = do_lower_jumps(shader->ir, true, true,
158 true, /* main return */
159 false, /* continue */
163 progress = do_common_optimization(shader->ir, true, true, 32)
167 /* Make a pass over the IR to add state references for any built-in
168 * uniforms that are used. This has to be done now (during linking).
169 * Code generation doesn't happen until the first time this shader is
170 * used for rendering. Waiting until then to generate the parameters is
171 * too late. At that point, the values for the built-in informs won't
172 * get sent to the shader.
174 foreach_list(node, shader->ir) {
175 ir_variable *var = ((ir_instruction *) node)->as_variable();
177 if ((var == NULL) || (var->mode != ir_var_uniform)
178 || (strncmp(var->name, "gl_", 3) != 0))
181 const ir_state_slot *const slots = var->state_slots;
182 assert(var->state_slots != NULL);
184 for (unsigned int i = 0; i < var->num_state_slots; i++) {
185 _mesa_add_state_reference(prog->Parameters,
186 (gl_state_index *) slots[i].tokens);
190 validate_ir_tree(shader->ir);
192 reparent_ir(shader->ir, shader->ir);
193 ralloc_free(mem_ctx);
195 do_set_program_inouts(shader->ir, prog,
196 shader->base.Type == GL_FRAGMENT_SHADER);
198 prog->SamplersUsed = shader->base.active_samplers;
199 _mesa_update_shader_textures_used(shProg, prog);
201 _mesa_reference_program(ctx, &shader->base.Program, prog);
203 /* This has to be done last. Any operation that can cause
204 * prog->ParameterValues to get reallocated (e.g., anything that adds a
205 * program constant) has to happen before creating this linkage.
207 _mesa_associate_uniform_storage(ctx, shProg, prog->Parameters);
209 _mesa_reference_program(ctx, &prog, NULL);
212 if (!brw_shader_precompile(ctx, shProg))
220 brw_type_for_base_type(const struct glsl_type *type)
222 switch (type->base_type) {
223 case GLSL_TYPE_FLOAT:
224 return BRW_REGISTER_TYPE_F;
227 return BRW_REGISTER_TYPE_D;
229 return BRW_REGISTER_TYPE_UD;
230 case GLSL_TYPE_ARRAY:
231 return brw_type_for_base_type(type->fields.array);
232 case GLSL_TYPE_STRUCT:
233 case GLSL_TYPE_SAMPLER:
234 /* These should be overridden with the type of the member when
235 * dereferenced into. BRW_REGISTER_TYPE_UD seems like a likely
236 * way to trip up if we don't.
238 return BRW_REGISTER_TYPE_UD;
240 assert(!"not reached");
241 return BRW_REGISTER_TYPE_F;
246 brw_conditional_for_comparison(unsigned int op)
250 return BRW_CONDITIONAL_L;
251 case ir_binop_greater:
252 return BRW_CONDITIONAL_G;
253 case ir_binop_lequal:
254 return BRW_CONDITIONAL_LE;
255 case ir_binop_gequal:
256 return BRW_CONDITIONAL_GE;
258 case ir_binop_all_equal: /* same as equal for scalars */
259 return BRW_CONDITIONAL_Z;
260 case ir_binop_nequal:
261 case ir_binop_any_nequal: /* same as nequal for scalars */
262 return BRW_CONDITIONAL_NZ;
264 assert(!"not reached: bad operation for comparison");
265 return BRW_CONDITIONAL_NZ;
270 brw_math_function(enum opcode op)
273 case SHADER_OPCODE_RCP:
274 return BRW_MATH_FUNCTION_INV;
275 case SHADER_OPCODE_RSQ:
276 return BRW_MATH_FUNCTION_RSQ;
277 case SHADER_OPCODE_SQRT:
278 return BRW_MATH_FUNCTION_SQRT;
279 case SHADER_OPCODE_EXP2:
280 return BRW_MATH_FUNCTION_EXP;
281 case SHADER_OPCODE_LOG2:
282 return BRW_MATH_FUNCTION_LOG;
283 case SHADER_OPCODE_POW:
284 return BRW_MATH_FUNCTION_POW;
285 case SHADER_OPCODE_SIN:
286 return BRW_MATH_FUNCTION_SIN;
287 case SHADER_OPCODE_COS:
288 return BRW_MATH_FUNCTION_COS;
289 case SHADER_OPCODE_INT_QUOTIENT:
290 return BRW_MATH_FUNCTION_INT_DIV_QUOTIENT;
291 case SHADER_OPCODE_INT_REMAINDER:
292 return BRW_MATH_FUNCTION_INT_DIV_REMAINDER;
294 assert(!"not reached: unknown math function");
300 brw_texture_offset(ir_constant *offset)
302 assert(offset != NULL);
304 signed char offsets[3];
305 for (unsigned i = 0; i < offset->type->vector_elements; i++)
306 offsets[i] = (signed char) offset->value.i[i];
308 /* Combine all three offsets into a single unsigned dword:
310 * bits 11:8 - U Offset (X component)
311 * bits 7:4 - V Offset (Y component)
312 * bits 3:0 - R Offset (Z component)
314 unsigned offset_bits = 0;
315 for (unsigned i = 0; i < offset->type->vector_elements; i++) {
316 const unsigned shift = 4 * (2 - i);
317 offset_bits |= (offsets[i] << shift) & (0xF << shift);