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"
33 brw_new_shader(struct gl_context
*ctx
, GLuint name
, GLuint type
)
35 struct brw_shader
*shader
;
37 shader
= rzalloc(NULL
, struct brw_shader
);
39 shader
->base
.Type
= type
;
40 shader
->base
.Name
= name
;
41 _mesa_init_shader(ctx
, &shader
->base
);
47 struct gl_shader_program
*
48 brw_new_shader_program(struct gl_context
*ctx
, GLuint name
)
50 struct gl_shader_program
*prog
= rzalloc(NULL
, struct gl_shader_program
);
53 _mesa_init_shader_program(ctx
, prog
);
59 * Performs a compile of the shader stages even when we don't know
60 * what non-orthogonal state will be set, in the hope that it reflects
61 * the eventual NOS used, and thus allows us to produce link failures.
64 brw_shader_precompile(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
66 struct brw_context
*brw
= brw_context(ctx
);
68 if (brw
->precompile
&& !brw_fs_precompile(ctx
, prog
))
71 if (brw
->precompile
&& !brw_vs_precompile(ctx
, prog
))
78 brw_lower_packing_builtins(struct brw_context
*brw
,
79 gl_shader_type shader_type
,
82 int ops
= LOWER_PACK_SNORM_2x16
83 | LOWER_UNPACK_SNORM_2x16
84 | LOWER_PACK_UNORM_2x16
85 | LOWER_UNPACK_UNORM_2x16
86 | LOWER_PACK_SNORM_4x8
87 | LOWER_UNPACK_SNORM_4x8
88 | LOWER_PACK_UNORM_4x8
89 | LOWER_UNPACK_UNORM_4x8
;
91 if (brw
->intel
.gen
>= 7) {
92 /* Gen7 introduced the f32to16 and f16to32 instructions, which can be
93 * used to execute packHalf2x16 and unpackHalf2x16. For AOS code, no
94 * lowering is needed. For SOA code, the Half2x16 ops must be
97 if (shader_type
== MESA_SHADER_FRAGMENT
) {
98 ops
|= LOWER_PACK_HALF_2x16_TO_SPLIT
99 | LOWER_UNPACK_HALF_2x16_TO_SPLIT
;
102 ops
|= LOWER_PACK_HALF_2x16
103 | LOWER_UNPACK_HALF_2x16
;
106 lower_packing_builtins(ir
, ops
);
110 brw_link_shader(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
112 struct brw_context
*brw
= brw_context(ctx
);
113 struct intel_context
*intel
= &brw
->intel
;
115 static const char *target_strings
[]
116 = { "vertex", "fragment", "geometry" };
118 for (stage
= 0; stage
< ARRAY_SIZE(shProg
->_LinkedShaders
); stage
++) {
119 struct brw_shader
*shader
=
120 (struct brw_shader
*)shProg
->_LinkedShaders
[stage
];
121 static const GLenum targets
[] = {
122 GL_VERTEX_PROGRAM_ARB
,
123 GL_FRAGMENT_PROGRAM_ARB
,
124 GL_GEOMETRY_PROGRAM_NV
130 struct gl_program
*prog
=
131 ctx
->Driver
.NewProgram(ctx
, targets
[stage
], shader
->base
.Name
);
134 prog
->Parameters
= _mesa_new_parameter_list();
137 struct gl_vertex_program
*vp
= (struct gl_vertex_program
*) prog
;
138 vp
->UsesClipDistance
= shProg
->Vert
.UsesClipDistance
;
141 void *mem_ctx
= ralloc_context(NULL
);
145 ralloc_free(shader
->ir
);
146 shader
->ir
= new(shader
) exec_list
;
147 clone_ir_list(mem_ctx
, shader
->ir
, shader
->base
.ir
);
149 /* lower_packing_builtins() inserts arithmetic instructions, so it
150 * must precede lower_instructions().
152 brw_lower_packing_builtins(brw
, (gl_shader_type
) stage
, shader
->ir
);
153 do_mat_op_to_vec(shader
->ir
);
154 const int bitfield_insert
= intel
->gen
>= 7
155 ? BITFIELD_INSERT_TO_BFM_BFI
157 const int lrp_to_arith
= intel
->gen
< 6 ? LRP_TO_ARITH
: 0;
158 lower_instructions(shader
->ir
,
167 /* Pre-gen6 HW can only nest if-statements 16 deep. Beyond this,
168 * if-statements need to be flattened.
171 lower_if_to_cond_assign(shader
->ir
, 16);
173 do_lower_texture_projection(shader
->ir
);
174 brw_lower_texture_gradients(intel
, shader
->ir
);
175 do_vec_index_to_cond_assign(shader
->ir
);
176 lower_vector_insert(shader
->ir
, true);
177 brw_do_cubemap_normalize(shader
->ir
);
178 lower_noise(shader
->ir
);
179 lower_quadop_vector(shader
->ir
, false);
182 bool output
= stage
== MESA_SHADER_FRAGMENT
;
183 bool temp
= stage
== MESA_SHADER_FRAGMENT
;
184 bool uniform
= false;
186 bool lowered_variable_indexing
=
187 lower_variable_index_to_cond_assign(shader
->ir
,
188 input
, output
, temp
, uniform
);
190 if (unlikely((intel
->perf_debug
) && lowered_variable_indexing
)) {
191 perf_debug("Unsupported form of variable indexing in FS; falling "
192 "back to very inefficient code generation\n");
195 /* FINISHME: Do this before the variable index lowering. */
196 lower_ubo_reference(&shader
->base
, shader
->ir
);
201 if (stage
== MESA_SHADER_FRAGMENT
) {
202 brw_do_channel_expressions(shader
->ir
);
203 brw_do_vector_splitting(shader
->ir
);
206 progress
= do_lower_jumps(shader
->ir
, true, true,
207 true, /* main return */
208 false, /* continue */
212 progress
= do_common_optimization(shader
->ir
, true, true, 32,
213 &ctx
->ShaderCompilerOptions
[stage
])
217 /* Make a pass over the IR to add state references for any built-in
218 * uniforms that are used. This has to be done now (during linking).
219 * Code generation doesn't happen until the first time this shader is
220 * used for rendering. Waiting until then to generate the parameters is
221 * too late. At that point, the values for the built-in uniforms won't
222 * get sent to the shader.
224 foreach_list(node
, shader
->ir
) {
225 ir_variable
*var
= ((ir_instruction
*) node
)->as_variable();
227 if ((var
== NULL
) || (var
->mode
!= ir_var_uniform
)
228 || (strncmp(var
->name
, "gl_", 3) != 0))
231 const ir_state_slot
*const slots
= var
->state_slots
;
232 assert(var
->state_slots
!= NULL
);
234 for (unsigned int i
= 0; i
< var
->num_state_slots
; i
++) {
235 _mesa_add_state_reference(prog
->Parameters
,
236 (gl_state_index
*) slots
[i
].tokens
);
240 validate_ir_tree(shader
->ir
);
242 reparent_ir(shader
->ir
, shader
->ir
);
243 ralloc_free(mem_ctx
);
245 do_set_program_inouts(shader
->ir
, prog
,
246 shader
->base
.Type
== GL_FRAGMENT_SHADER
);
248 prog
->SamplersUsed
= shader
->base
.active_samplers
;
249 _mesa_update_shader_textures_used(shProg
, prog
);
251 _mesa_reference_program(ctx
, &shader
->base
.Program
, prog
);
253 brw_add_texrect_params(prog
);
255 /* This has to be done last. Any operation that can cause
256 * prog->ParameterValues to get reallocated (e.g., anything that adds a
257 * program constant) has to happen before creating this linkage.
259 _mesa_associate_uniform_storage(ctx
, shProg
, prog
->Parameters
);
261 _mesa_reference_program(ctx
, &prog
, NULL
);
263 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
265 printf("GLSL IR for linked %s program %d:\n", target_strings
[stage
],
267 _mesa_print_ir(shader
->base
.ir
, NULL
);
272 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
273 for (unsigned i
= 0; i
< shProg
->NumShaders
; i
++) {
274 const struct gl_shader
*sh
= shProg
->Shaders
[i
];
278 printf("GLSL %s shader %d source for linked program %d:\n",
279 target_strings
[_mesa_shader_type_to_index(sh
->Type
)],
282 printf("%s", sh
->Source
);
287 if (!brw_shader_precompile(ctx
, shProg
))
295 brw_type_for_base_type(const struct glsl_type
*type
)
297 switch (type
->base_type
) {
298 case GLSL_TYPE_FLOAT
:
299 return BRW_REGISTER_TYPE_F
;
302 return BRW_REGISTER_TYPE_D
;
304 return BRW_REGISTER_TYPE_UD
;
305 case GLSL_TYPE_ARRAY
:
306 return brw_type_for_base_type(type
->fields
.array
);
307 case GLSL_TYPE_STRUCT
:
308 case GLSL_TYPE_SAMPLER
:
309 /* These should be overridden with the type of the member when
310 * dereferenced into. BRW_REGISTER_TYPE_UD seems like a likely
311 * way to trip up if we don't.
313 return BRW_REGISTER_TYPE_UD
;
315 case GLSL_TYPE_ERROR
:
316 case GLSL_TYPE_INTERFACE
:
317 assert(!"not reached");
321 return BRW_REGISTER_TYPE_F
;
325 brw_conditional_for_comparison(unsigned int op
)
329 return BRW_CONDITIONAL_L
;
330 case ir_binop_greater
:
331 return BRW_CONDITIONAL_G
;
332 case ir_binop_lequal
:
333 return BRW_CONDITIONAL_LE
;
334 case ir_binop_gequal
:
335 return BRW_CONDITIONAL_GE
;
337 case ir_binop_all_equal
: /* same as equal for scalars */
338 return BRW_CONDITIONAL_Z
;
339 case ir_binop_nequal
:
340 case ir_binop_any_nequal
: /* same as nequal for scalars */
341 return BRW_CONDITIONAL_NZ
;
343 assert(!"not reached: bad operation for comparison");
344 return BRW_CONDITIONAL_NZ
;
349 brw_math_function(enum opcode op
)
352 case SHADER_OPCODE_RCP
:
353 return BRW_MATH_FUNCTION_INV
;
354 case SHADER_OPCODE_RSQ
:
355 return BRW_MATH_FUNCTION_RSQ
;
356 case SHADER_OPCODE_SQRT
:
357 return BRW_MATH_FUNCTION_SQRT
;
358 case SHADER_OPCODE_EXP2
:
359 return BRW_MATH_FUNCTION_EXP
;
360 case SHADER_OPCODE_LOG2
:
361 return BRW_MATH_FUNCTION_LOG
;
362 case SHADER_OPCODE_POW
:
363 return BRW_MATH_FUNCTION_POW
;
364 case SHADER_OPCODE_SIN
:
365 return BRW_MATH_FUNCTION_SIN
;
366 case SHADER_OPCODE_COS
:
367 return BRW_MATH_FUNCTION_COS
;
368 case SHADER_OPCODE_INT_QUOTIENT
:
369 return BRW_MATH_FUNCTION_INT_DIV_QUOTIENT
;
370 case SHADER_OPCODE_INT_REMAINDER
:
371 return BRW_MATH_FUNCTION_INT_DIV_REMAINDER
;
373 assert(!"not reached: unknown math function");
379 brw_texture_offset(ir_constant
*offset
)
381 assert(offset
!= NULL
);
383 signed char offsets
[3];
384 for (unsigned i
= 0; i
< offset
->type
->vector_elements
; i
++)
385 offsets
[i
] = (signed char) offset
->value
.i
[i
];
387 /* Combine all three offsets into a single unsigned dword:
389 * bits 11:8 - U Offset (X component)
390 * bits 7:4 - V Offset (Y component)
391 * bits 3:0 - R Offset (Z component)
393 unsigned offset_bits
= 0;
394 for (unsigned i
= 0; i
< offset
->type
->vector_elements
; i
++) {
395 const unsigned shift
= 4 * (2 - i
);
396 offset_bits
|= (offsets
[i
] << shift
) & (0xF << shift
);
402 brw_instruction_name(enum opcode op
)
406 if (op
< ARRAY_SIZE(opcode_descs
) && opcode_descs
[op
].name
)
407 return opcode_descs
[op
].name
;
410 case FS_OPCODE_FB_WRITE
:
413 case SHADER_OPCODE_RCP
:
415 case SHADER_OPCODE_RSQ
:
417 case SHADER_OPCODE_SQRT
:
419 case SHADER_OPCODE_EXP2
:
421 case SHADER_OPCODE_LOG2
:
423 case SHADER_OPCODE_POW
:
425 case SHADER_OPCODE_INT_QUOTIENT
:
427 case SHADER_OPCODE_INT_REMAINDER
:
429 case SHADER_OPCODE_SIN
:
431 case SHADER_OPCODE_COS
:
434 case SHADER_OPCODE_TEX
:
436 case SHADER_OPCODE_TXD
:
438 case SHADER_OPCODE_TXF
:
440 case SHADER_OPCODE_TXL
:
442 case SHADER_OPCODE_TXS
:
446 case SHADER_OPCODE_TXF_MS
:
454 case FS_OPCODE_PIXEL_X
:
456 case FS_OPCODE_PIXEL_Y
:
459 case FS_OPCODE_CINTERP
:
461 case FS_OPCODE_LINTERP
:
464 case FS_OPCODE_SPILL
:
466 case FS_OPCODE_UNSPILL
:
469 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
:
470 return "uniform_pull_const";
471 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7
:
472 return "uniform_pull_const_gen7";
473 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD
:
474 return "varying_pull_const";
475 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN7
:
476 return "varying_pull_const_gen7";
478 case FS_OPCODE_MOV_DISPATCH_TO_FLAGS
:
479 return "mov_dispatch_to_flags";
480 case FS_OPCODE_DISCARD_JUMP
:
481 return "discard_jump";
483 case FS_OPCODE_SET_SIMD4X2_OFFSET
:
484 return "set_simd4x2_offset";
486 case FS_OPCODE_PACK_HALF_2x16_SPLIT
:
487 return "pack_half_2x16_split";
488 case FS_OPCODE_UNPACK_HALF_2x16_SPLIT_X
:
489 return "unpack_half_2x16_split_x";
490 case FS_OPCODE_UNPACK_HALF_2x16_SPLIT_Y
:
491 return "unpack_half_2x16_split_y";
493 case FS_OPCODE_PLACEHOLDER_HALT
:
494 return "placeholder_halt";
496 case VS_OPCODE_URB_WRITE
:
498 case VS_OPCODE_SCRATCH_READ
:
499 return "scratch_read";
500 case VS_OPCODE_SCRATCH_WRITE
:
501 return "scratch_write";
502 case VS_OPCODE_PULL_CONSTANT_LOAD
:
503 return "pull_constant_load";
504 case VS_OPCODE_PULL_CONSTANT_LOAD_GEN7
:
505 return "pull_constant_load_gen7";
508 /* Yes, this leaks. It's in debug code, it should never occur, and if
509 * it does, you should just add the case to the list above.
511 asprintf(&fallback
, "op%d", op
);
517 backend_instruction::is_tex()
519 return (opcode
== SHADER_OPCODE_TEX
||
520 opcode
== FS_OPCODE_TXB
||
521 opcode
== SHADER_OPCODE_TXD
||
522 opcode
== SHADER_OPCODE_TXF
||
523 opcode
== SHADER_OPCODE_TXF_MS
||
524 opcode
== SHADER_OPCODE_TXL
||
525 opcode
== SHADER_OPCODE_TXS
||
526 opcode
== SHADER_OPCODE_LOD
);
530 backend_instruction::is_math()
532 return (opcode
== SHADER_OPCODE_RCP
||
533 opcode
== SHADER_OPCODE_RSQ
||
534 opcode
== SHADER_OPCODE_SQRT
||
535 opcode
== SHADER_OPCODE_EXP2
||
536 opcode
== SHADER_OPCODE_LOG2
||
537 opcode
== SHADER_OPCODE_SIN
||
538 opcode
== SHADER_OPCODE_COS
||
539 opcode
== SHADER_OPCODE_INT_QUOTIENT
||
540 opcode
== SHADER_OPCODE_INT_REMAINDER
||
541 opcode
== SHADER_OPCODE_POW
);
545 backend_instruction::is_control_flow()
549 case BRW_OPCODE_WHILE
:
551 case BRW_OPCODE_ELSE
:
552 case BRW_OPCODE_ENDIF
:
553 case BRW_OPCODE_BREAK
:
554 case BRW_OPCODE_CONTINUE
:
562 backend_visitor::dump_instructions()
565 foreach_list(node
, &this->instructions
) {
566 backend_instruction
*inst
= (backend_instruction
*)node
;
567 printf("%d: ", ip
++);
568 dump_instruction(inst
);