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
->precompile
&& !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
);
149 /* FINISHME: Do this before the variable index lowering. */
150 lower_ubo_reference(&shader
->base
, shader
->ir
);
155 if (stage
== MESA_SHADER_FRAGMENT
) {
156 brw_do_channel_expressions(shader
->ir
);
157 brw_do_vector_splitting(shader
->ir
);
160 progress
= do_lower_jumps(shader
->ir
, true, true,
161 true, /* main return */
162 false, /* continue */
166 progress
= do_common_optimization(shader
->ir
, true, true, 32)
170 /* Make a pass over the IR to add state references for any built-in
171 * uniforms that are used. This has to be done now (during linking).
172 * Code generation doesn't happen until the first time this shader is
173 * used for rendering. Waiting until then to generate the parameters is
174 * too late. At that point, the values for the built-in informs won't
175 * get sent to the shader.
177 foreach_list(node
, shader
->ir
) {
178 ir_variable
*var
= ((ir_instruction
*) node
)->as_variable();
180 if ((var
== NULL
) || (var
->mode
!= ir_var_uniform
)
181 || (strncmp(var
->name
, "gl_", 3) != 0))
184 const ir_state_slot
*const slots
= var
->state_slots
;
185 assert(var
->state_slots
!= NULL
);
187 for (unsigned int i
= 0; i
< var
->num_state_slots
; i
++) {
188 _mesa_add_state_reference(prog
->Parameters
,
189 (gl_state_index
*) slots
[i
].tokens
);
193 validate_ir_tree(shader
->ir
);
195 reparent_ir(shader
->ir
, shader
->ir
);
196 ralloc_free(mem_ctx
);
198 do_set_program_inouts(shader
->ir
, prog
,
199 shader
->base
.Type
== GL_FRAGMENT_SHADER
);
201 prog
->SamplersUsed
= shader
->base
.active_samplers
;
202 _mesa_update_shader_textures_used(shProg
, prog
);
204 _mesa_reference_program(ctx
, &shader
->base
.Program
, prog
);
206 /* This has to be done last. Any operation that can cause
207 * prog->ParameterValues to get reallocated (e.g., anything that adds a
208 * program constant) has to happen before creating this linkage.
210 _mesa_associate_uniform_storage(ctx
, shProg
, prog
->Parameters
);
212 _mesa_reference_program(ctx
, &prog
, NULL
);
214 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
215 static const char *target_strings
[]
216 = { "vertex", "fragment", "geometry" };
218 printf("GLSL IR for linked %s program %d:\n", target_strings
[stage
],
220 _mesa_print_ir(shader
->base
.ir
, NULL
);
224 if (!brw_shader_precompile(ctx
, shProg
))
232 brw_type_for_base_type(const struct glsl_type
*type
)
234 switch (type
->base_type
) {
235 case GLSL_TYPE_FLOAT
:
236 return BRW_REGISTER_TYPE_F
;
239 return BRW_REGISTER_TYPE_D
;
241 return BRW_REGISTER_TYPE_UD
;
242 case GLSL_TYPE_ARRAY
:
243 return brw_type_for_base_type(type
->fields
.array
);
244 case GLSL_TYPE_STRUCT
:
245 case GLSL_TYPE_SAMPLER
:
246 /* These should be overridden with the type of the member when
247 * dereferenced into. BRW_REGISTER_TYPE_UD seems like a likely
248 * way to trip up if we don't.
250 return BRW_REGISTER_TYPE_UD
;
252 assert(!"not reached");
253 return BRW_REGISTER_TYPE_F
;
258 brw_conditional_for_comparison(unsigned int op
)
262 return BRW_CONDITIONAL_L
;
263 case ir_binop_greater
:
264 return BRW_CONDITIONAL_G
;
265 case ir_binop_lequal
:
266 return BRW_CONDITIONAL_LE
;
267 case ir_binop_gequal
:
268 return BRW_CONDITIONAL_GE
;
270 case ir_binop_all_equal
: /* same as equal for scalars */
271 return BRW_CONDITIONAL_Z
;
272 case ir_binop_nequal
:
273 case ir_binop_any_nequal
: /* same as nequal for scalars */
274 return BRW_CONDITIONAL_NZ
;
276 assert(!"not reached: bad operation for comparison");
277 return BRW_CONDITIONAL_NZ
;
282 brw_math_function(enum opcode op
)
285 case SHADER_OPCODE_RCP
:
286 return BRW_MATH_FUNCTION_INV
;
287 case SHADER_OPCODE_RSQ
:
288 return BRW_MATH_FUNCTION_RSQ
;
289 case SHADER_OPCODE_SQRT
:
290 return BRW_MATH_FUNCTION_SQRT
;
291 case SHADER_OPCODE_EXP2
:
292 return BRW_MATH_FUNCTION_EXP
;
293 case SHADER_OPCODE_LOG2
:
294 return BRW_MATH_FUNCTION_LOG
;
295 case SHADER_OPCODE_POW
:
296 return BRW_MATH_FUNCTION_POW
;
297 case SHADER_OPCODE_SIN
:
298 return BRW_MATH_FUNCTION_SIN
;
299 case SHADER_OPCODE_COS
:
300 return BRW_MATH_FUNCTION_COS
;
301 case SHADER_OPCODE_INT_QUOTIENT
:
302 return BRW_MATH_FUNCTION_INT_DIV_QUOTIENT
;
303 case SHADER_OPCODE_INT_REMAINDER
:
304 return BRW_MATH_FUNCTION_INT_DIV_REMAINDER
;
306 assert(!"not reached: unknown math function");
312 brw_texture_offset(ir_constant
*offset
)
314 assert(offset
!= NULL
);
316 signed char offsets
[3];
317 for (unsigned i
= 0; i
< offset
->type
->vector_elements
; i
++)
318 offsets
[i
] = (signed char) offset
->value
.i
[i
];
320 /* Combine all three offsets into a single unsigned dword:
322 * bits 11:8 - U Offset (X component)
323 * bits 7:4 - V Offset (Y component)
324 * bits 3:0 - R Offset (Z component)
326 unsigned offset_bits
= 0;
327 for (unsigned i
= 0; i
< offset
->type
->vector_elements
; i
++) {
328 const unsigned shift
= 4 * (2 - i
);
329 offset_bits
|= (offsets
[i
] << shift
) & (0xF << shift
);