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"
29 #include "brw_vec4_gs.h"
31 #include "glsl/ir_optimization.h"
32 #include "glsl/glsl_parser_extras.h"
33 #include "main/shaderapi.h"
36 brw_new_shader(struct gl_context
*ctx
, GLuint name
, GLuint type
)
38 struct brw_shader
*shader
;
40 shader
= rzalloc(NULL
, struct brw_shader
);
42 shader
->base
.Type
= type
;
43 shader
->base
.Name
= name
;
44 _mesa_init_shader(ctx
, &shader
->base
);
50 struct gl_shader_program
*
51 brw_new_shader_program(struct gl_context
*ctx
, GLuint name
)
53 struct gl_shader_program
*prog
= rzalloc(NULL
, struct gl_shader_program
);
56 _mesa_init_shader_program(ctx
, prog
);
62 * Performs a compile of the shader stages even when we don't know
63 * what non-orthogonal state will be set, in the hope that it reflects
64 * the eventual NOS used, and thus allows us to produce link failures.
67 brw_shader_precompile(struct gl_context
*ctx
, struct gl_shader_program
*prog
)
69 struct brw_context
*brw
= brw_context(ctx
);
71 if (brw
->precompile
&& !brw_fs_precompile(ctx
, prog
))
74 if (brw
->precompile
&& !brw_gs_precompile(ctx
, prog
))
77 if (brw
->precompile
&& !brw_vs_precompile(ctx
, prog
))
84 brw_lower_packing_builtins(struct brw_context
*brw
,
85 gl_shader_type shader_type
,
88 int ops
= LOWER_PACK_SNORM_2x16
89 | LOWER_UNPACK_SNORM_2x16
90 | LOWER_PACK_UNORM_2x16
91 | LOWER_UNPACK_UNORM_2x16
92 | LOWER_PACK_SNORM_4x8
93 | LOWER_UNPACK_SNORM_4x8
94 | LOWER_PACK_UNORM_4x8
95 | LOWER_UNPACK_UNORM_4x8
;
98 /* Gen7 introduced the f32to16 and f16to32 instructions, which can be
99 * used to execute packHalf2x16 and unpackHalf2x16. For AOS code, no
100 * lowering is needed. For SOA code, the Half2x16 ops must be
103 if (shader_type
== MESA_SHADER_FRAGMENT
) {
104 ops
|= LOWER_PACK_HALF_2x16_TO_SPLIT
105 | LOWER_UNPACK_HALF_2x16_TO_SPLIT
;
108 ops
|= LOWER_PACK_HALF_2x16
109 | LOWER_UNPACK_HALF_2x16
;
112 lower_packing_builtins(ir
, ops
);
116 brw_link_shader(struct gl_context
*ctx
, struct gl_shader_program
*shProg
)
118 struct brw_context
*brw
= brw_context(ctx
);
121 for (stage
= 0; stage
< ARRAY_SIZE(shProg
->_LinkedShaders
); stage
++) {
122 struct brw_shader
*shader
=
123 (struct brw_shader
*)shProg
->_LinkedShaders
[stage
];
128 struct gl_program
*prog
=
129 ctx
->Driver
.NewProgram(ctx
, _mesa_program_index_to_target(stage
),
133 prog
->Parameters
= _mesa_new_parameter_list();
135 _mesa_copy_linked_program_data((gl_shader_type
) stage
, shProg
, prog
);
137 void *mem_ctx
= ralloc_context(NULL
);
141 ralloc_free(shader
->ir
);
142 shader
->ir
= new(shader
) exec_list
;
143 clone_ir_list(mem_ctx
, shader
->ir
, shader
->base
.ir
);
145 /* lower_packing_builtins() inserts arithmetic instructions, so it
146 * must precede lower_instructions().
148 brw_lower_packing_builtins(brw
, (gl_shader_type
) stage
, shader
->ir
);
149 do_mat_op_to_vec(shader
->ir
);
150 const int bitfield_insert
= brw
->gen
>= 7
151 ? BITFIELD_INSERT_TO_BFM_BFI
153 const int lrp_to_arith
= brw
->gen
< 6 ? LRP_TO_ARITH
: 0;
154 lower_instructions(shader
->ir
,
164 /* Pre-gen6 HW can only nest if-statements 16 deep. Beyond this,
165 * if-statements need to be flattened.
168 lower_if_to_cond_assign(shader
->ir
, 16);
170 do_lower_texture_projection(shader
->ir
);
171 brw_lower_texture_gradients(brw
, shader
->ir
);
172 do_vec_index_to_cond_assign(shader
->ir
);
173 lower_vector_insert(shader
->ir
, true);
174 brw_do_cubemap_normalize(shader
->ir
);
175 lower_noise(shader
->ir
);
176 lower_quadop_vector(shader
->ir
, false);
179 bool output
= stage
== MESA_SHADER_FRAGMENT
;
180 bool temp
= stage
== MESA_SHADER_FRAGMENT
;
181 bool uniform
= false;
183 bool lowered_variable_indexing
=
184 lower_variable_index_to_cond_assign(shader
->ir
,
185 input
, output
, temp
, uniform
);
187 if (unlikely(brw
->perf_debug
&& lowered_variable_indexing
)) {
188 perf_debug("Unsupported form of variable indexing in FS; falling "
189 "back to very inefficient code generation\n");
192 /* FINISHME: Do this before the variable index lowering. */
193 lower_ubo_reference(&shader
->base
, shader
->ir
);
198 if (stage
== MESA_SHADER_FRAGMENT
) {
199 brw_do_channel_expressions(shader
->ir
);
200 brw_do_vector_splitting(shader
->ir
);
203 progress
= do_lower_jumps(shader
->ir
, true, true,
204 true, /* main return */
205 false, /* continue */
209 progress
= do_common_optimization(shader
->ir
, true, true, 32,
210 &ctx
->ShaderCompilerOptions
[stage
])
214 /* Make a pass over the IR to add state references for any built-in
215 * uniforms that are used. This has to be done now (during linking).
216 * Code generation doesn't happen until the first time this shader is
217 * used for rendering. Waiting until then to generate the parameters is
218 * too late. At that point, the values for the built-in uniforms won't
219 * get sent to the shader.
221 foreach_list(node
, shader
->ir
) {
222 ir_variable
*var
= ((ir_instruction
*) node
)->as_variable();
224 if ((var
== NULL
) || (var
->mode
!= ir_var_uniform
)
225 || (strncmp(var
->name
, "gl_", 3) != 0))
228 const ir_state_slot
*const slots
= var
->state_slots
;
229 assert(var
->state_slots
!= NULL
);
231 for (unsigned int i
= 0; i
< var
->num_state_slots
; i
++) {
232 _mesa_add_state_reference(prog
->Parameters
,
233 (gl_state_index
*) slots
[i
].tokens
);
237 validate_ir_tree(shader
->ir
);
239 reparent_ir(shader
->ir
, shader
->ir
);
240 ralloc_free(mem_ctx
);
242 do_set_program_inouts(shader
->ir
, prog
, shader
->base
.Type
);
244 prog
->SamplersUsed
= shader
->base
.active_samplers
;
245 _mesa_update_shader_textures_used(shProg
, prog
);
247 _mesa_reference_program(ctx
, &shader
->base
.Program
, prog
);
249 brw_add_texrect_params(prog
);
251 /* This has to be done last. Any operation that can cause
252 * prog->ParameterValues to get reallocated (e.g., anything that adds a
253 * program constant) has to happen before creating this linkage.
255 _mesa_associate_uniform_storage(ctx
, shProg
, prog
->Parameters
);
257 _mesa_reference_program(ctx
, &prog
, NULL
);
259 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
261 printf("GLSL IR for linked %s program %d:\n",
262 _mesa_glsl_shader_target_name(shader
->base
.Type
), shProg
->Name
);
263 _mesa_print_ir(shader
->base
.ir
, NULL
);
268 if (ctx
->Shader
.Flags
& GLSL_DUMP
) {
269 for (unsigned i
= 0; i
< shProg
->NumShaders
; i
++) {
270 const struct gl_shader
*sh
= shProg
->Shaders
[i
];
274 printf("GLSL %s shader %d source for linked program %d:\n",
275 _mesa_glsl_shader_target_name(sh
->Type
),
278 printf("%s", sh
->Source
);
283 if (!brw_shader_precompile(ctx
, shProg
))
291 brw_type_for_base_type(const struct glsl_type
*type
)
293 switch (type
->base_type
) {
294 case GLSL_TYPE_FLOAT
:
295 return BRW_REGISTER_TYPE_F
;
298 return BRW_REGISTER_TYPE_D
;
300 return BRW_REGISTER_TYPE_UD
;
301 case GLSL_TYPE_ARRAY
:
302 return brw_type_for_base_type(type
->fields
.array
);
303 case GLSL_TYPE_STRUCT
:
304 case GLSL_TYPE_SAMPLER
:
305 /* These should be overridden with the type of the member when
306 * dereferenced into. BRW_REGISTER_TYPE_UD seems like a likely
307 * way to trip up if we don't.
309 return BRW_REGISTER_TYPE_UD
;
311 case GLSL_TYPE_ERROR
:
312 case GLSL_TYPE_INTERFACE
:
313 assert(!"not reached");
317 return BRW_REGISTER_TYPE_F
;
321 brw_conditional_for_comparison(unsigned int op
)
325 return BRW_CONDITIONAL_L
;
326 case ir_binop_greater
:
327 return BRW_CONDITIONAL_G
;
328 case ir_binop_lequal
:
329 return BRW_CONDITIONAL_LE
;
330 case ir_binop_gequal
:
331 return BRW_CONDITIONAL_GE
;
333 case ir_binop_all_equal
: /* same as equal for scalars */
334 return BRW_CONDITIONAL_Z
;
335 case ir_binop_nequal
:
336 case ir_binop_any_nequal
: /* same as nequal for scalars */
337 return BRW_CONDITIONAL_NZ
;
339 assert(!"not reached: bad operation for comparison");
340 return BRW_CONDITIONAL_NZ
;
345 brw_math_function(enum opcode op
)
348 case SHADER_OPCODE_RCP
:
349 return BRW_MATH_FUNCTION_INV
;
350 case SHADER_OPCODE_RSQ
:
351 return BRW_MATH_FUNCTION_RSQ
;
352 case SHADER_OPCODE_SQRT
:
353 return BRW_MATH_FUNCTION_SQRT
;
354 case SHADER_OPCODE_EXP2
:
355 return BRW_MATH_FUNCTION_EXP
;
356 case SHADER_OPCODE_LOG2
:
357 return BRW_MATH_FUNCTION_LOG
;
358 case SHADER_OPCODE_POW
:
359 return BRW_MATH_FUNCTION_POW
;
360 case SHADER_OPCODE_SIN
:
361 return BRW_MATH_FUNCTION_SIN
;
362 case SHADER_OPCODE_COS
:
363 return BRW_MATH_FUNCTION_COS
;
364 case SHADER_OPCODE_INT_QUOTIENT
:
365 return BRW_MATH_FUNCTION_INT_DIV_QUOTIENT
;
366 case SHADER_OPCODE_INT_REMAINDER
:
367 return BRW_MATH_FUNCTION_INT_DIV_REMAINDER
;
369 assert(!"not reached: unknown math function");
375 brw_texture_offset(struct gl_context
*ctx
, ir_constant
*offset
)
377 /* If the driver does not support GL_ARB_gpu_shader5, the offset
380 assert(offset
!= NULL
|| ctx
->Extensions
.ARB_gpu_shader5
);
382 if (!offset
) return 0; /* nonconstant offset; caller will handle it. */
384 signed char offsets
[3];
385 for (unsigned i
= 0; i
< offset
->type
->vector_elements
; i
++)
386 offsets
[i
] = (signed char) offset
->value
.i
[i
];
388 /* Combine all three offsets into a single unsigned dword:
390 * bits 11:8 - U Offset (X component)
391 * bits 7:4 - V Offset (Y component)
392 * bits 3:0 - R Offset (Z component)
394 unsigned offset_bits
= 0;
395 for (unsigned i
= 0; i
< offset
->type
->vector_elements
; i
++) {
396 const unsigned shift
= 4 * (2 - i
);
397 offset_bits
|= (offsets
[i
] << shift
) & (0xF << shift
);
403 brw_instruction_name(enum opcode op
)
407 if (op
< ARRAY_SIZE(opcode_descs
) && opcode_descs
[op
].name
)
408 return opcode_descs
[op
].name
;
411 case FS_OPCODE_FB_WRITE
:
414 case SHADER_OPCODE_RCP
:
416 case SHADER_OPCODE_RSQ
:
418 case SHADER_OPCODE_SQRT
:
420 case SHADER_OPCODE_EXP2
:
422 case SHADER_OPCODE_LOG2
:
424 case SHADER_OPCODE_POW
:
426 case SHADER_OPCODE_INT_QUOTIENT
:
428 case SHADER_OPCODE_INT_REMAINDER
:
430 case SHADER_OPCODE_SIN
:
432 case SHADER_OPCODE_COS
:
435 case SHADER_OPCODE_TEX
:
437 case SHADER_OPCODE_TXD
:
439 case SHADER_OPCODE_TXF
:
441 case SHADER_OPCODE_TXL
:
443 case SHADER_OPCODE_TXS
:
447 case SHADER_OPCODE_TXF_MS
:
449 case SHADER_OPCODE_TG4
:
451 case SHADER_OPCODE_TG4_OFFSET
:
459 case FS_OPCODE_PIXEL_X
:
461 case FS_OPCODE_PIXEL_Y
:
464 case FS_OPCODE_CINTERP
:
466 case FS_OPCODE_LINTERP
:
469 case FS_OPCODE_SPILL
:
471 case FS_OPCODE_UNSPILL
:
474 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
:
475 return "uniform_pull_const";
476 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7
:
477 return "uniform_pull_const_gen7";
478 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD
:
479 return "varying_pull_const";
480 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN7
:
481 return "varying_pull_const_gen7";
483 case FS_OPCODE_MOV_DISPATCH_TO_FLAGS
:
484 return "mov_dispatch_to_flags";
485 case FS_OPCODE_DISCARD_JUMP
:
486 return "discard_jump";
488 case FS_OPCODE_SET_SIMD4X2_OFFSET
:
489 return "set_simd4x2_offset";
491 case FS_OPCODE_PACK_HALF_2x16_SPLIT
:
492 return "pack_half_2x16_split";
493 case FS_OPCODE_UNPACK_HALF_2x16_SPLIT_X
:
494 return "unpack_half_2x16_split_x";
495 case FS_OPCODE_UNPACK_HALF_2x16_SPLIT_Y
:
496 return "unpack_half_2x16_split_y";
498 case FS_OPCODE_PLACEHOLDER_HALT
:
499 return "placeholder_halt";
501 case VS_OPCODE_URB_WRITE
:
502 return "vs_urb_write";
503 case VS_OPCODE_SCRATCH_READ
:
504 return "scratch_read";
505 case VS_OPCODE_SCRATCH_WRITE
:
506 return "scratch_write";
507 case VS_OPCODE_PULL_CONSTANT_LOAD
:
508 return "pull_constant_load";
509 case VS_OPCODE_PULL_CONSTANT_LOAD_GEN7
:
510 return "pull_constant_load_gen7";
511 case VS_OPCODE_UNPACK_FLAGS_SIMD4X2
:
512 return "unpack_flags_simd4x2";
514 case GS_OPCODE_URB_WRITE
:
515 return "gs_urb_write";
516 case GS_OPCODE_THREAD_END
:
517 return "gs_thread_end";
518 case GS_OPCODE_SET_WRITE_OFFSET
:
519 return "set_write_offset";
520 case GS_OPCODE_SET_VERTEX_COUNT
:
521 return "set_vertex_count";
522 case GS_OPCODE_SET_DWORD_2_IMMED
:
523 return "set_dword_2_immed";
524 case GS_OPCODE_PREPARE_CHANNEL_MASKS
:
525 return "prepare_channel_masks";
526 case GS_OPCODE_SET_CHANNEL_MASKS
:
527 return "set_channel_masks";
530 /* Yes, this leaks. It's in debug code, it should never occur, and if
531 * it does, you should just add the case to the list above.
533 asprintf(&fallback
, "op%d", op
);
539 backend_instruction::is_tex()
541 return (opcode
== SHADER_OPCODE_TEX
||
542 opcode
== FS_OPCODE_TXB
||
543 opcode
== SHADER_OPCODE_TXD
||
544 opcode
== SHADER_OPCODE_TXF
||
545 opcode
== SHADER_OPCODE_TXF_MS
||
546 opcode
== SHADER_OPCODE_TXL
||
547 opcode
== SHADER_OPCODE_TXS
||
548 opcode
== SHADER_OPCODE_LOD
||
549 opcode
== SHADER_OPCODE_TG4
||
550 opcode
== SHADER_OPCODE_TG4_OFFSET
);
554 backend_instruction::is_math()
556 return (opcode
== SHADER_OPCODE_RCP
||
557 opcode
== SHADER_OPCODE_RSQ
||
558 opcode
== SHADER_OPCODE_SQRT
||
559 opcode
== SHADER_OPCODE_EXP2
||
560 opcode
== SHADER_OPCODE_LOG2
||
561 opcode
== SHADER_OPCODE_SIN
||
562 opcode
== SHADER_OPCODE_COS
||
563 opcode
== SHADER_OPCODE_INT_QUOTIENT
||
564 opcode
== SHADER_OPCODE_INT_REMAINDER
||
565 opcode
== SHADER_OPCODE_POW
);
569 backend_instruction::is_control_flow()
573 case BRW_OPCODE_WHILE
:
575 case BRW_OPCODE_ELSE
:
576 case BRW_OPCODE_ENDIF
:
577 case BRW_OPCODE_BREAK
:
578 case BRW_OPCODE_CONTINUE
:
586 backend_instruction::can_do_source_mods()
589 case BRW_OPCODE_ADDC
:
591 case BRW_OPCODE_BFI1
:
592 case BRW_OPCODE_BFI2
:
593 case BRW_OPCODE_BFREV
:
594 case BRW_OPCODE_CBIT
:
597 case BRW_OPCODE_SUBB
:
605 backend_visitor::dump_instructions()
608 foreach_list(node
, &this->instructions
) {
609 backend_instruction
*inst
= (backend_instruction
*)node
;
610 printf("%d: ", ip
++);
611 dump_instruction(inst
);
617 * Sets up the starting offsets for the groups of binding table entries
618 * commong to all pipeline stages.
620 * Unused groups are initialized to 0xd0d0d0d0 to make it obvious that they're
621 * unused but also make sure that addition of small offsets to them will
622 * trigger some of our asserts that surface indices are < BRW_MAX_SURFACES.
625 backend_visitor::assign_common_binding_table_offsets(uint32_t next_binding_table_offset
)
627 int num_textures
= _mesa_fls(prog
->SamplersUsed
);
629 stage_prog_data
->binding_table
.texture_start
= next_binding_table_offset
;
630 next_binding_table_offset
+= num_textures
;
633 stage_prog_data
->binding_table
.ubo_start
= next_binding_table_offset
;
634 next_binding_table_offset
+= shader
->base
.NumUniformBlocks
;
636 stage_prog_data
->binding_table
.ubo_start
= 0xd0d0d0d0;
639 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
) {
640 stage_prog_data
->binding_table
.shader_time_start
= next_binding_table_offset
;
641 next_binding_table_offset
++;
643 stage_prog_data
->binding_table
.shader_time_start
= 0xd0d0d0d0;
646 if (prog
->UsesGather
) {
647 stage_prog_data
->binding_table
.gather_texture_start
= next_binding_table_offset
;
648 next_binding_table_offset
+= num_textures
;
650 stage_prog_data
->binding_table
.gather_texture_start
= 0xd0d0d0d0;
653 /* This may or may not be used depending on how the compile goes. */
654 stage_prog_data
->binding_table
.pull_constants_start
= next_binding_table_offset
;
655 next_binding_table_offset
++;
657 assert(next_binding_table_offset
<= BRW_MAX_SURFACES
);
659 /* prog_data->base.binding_table.size will be set by mark_surface_used. */