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
24 * Eric Anholt <eric@anholt.net>
30 #include <sys/types.h>
32 #include "main/macros.h"
33 #include "main/shaderobj.h"
34 #include "program/prog_parameter.h"
35 #include "program/prog_print.h"
36 #include "program/prog_optimize.h"
37 #include "program/sampler.h"
38 #include "program/hash_table.h"
39 #include "brw_context.h"
44 #include "../glsl/glsl_types.h"
45 #include "../glsl/ir_optimization.h"
46 #include "../glsl/ir_print_visitor.h"
49 ARF
= BRW_ARCHITECTURE_REGISTER_FILE
,
50 GRF
= BRW_GENERAL_REGISTER_FILE
,
51 MRF
= BRW_MESSAGE_REGISTER_FILE
,
52 IMM
= BRW_IMMEDIATE_VALUE
,
53 FIXED_HW_REG
, /* a struct brw_reg */
54 UNIFORM
, /* prog_data->params[hw_reg] */
59 FS_OPCODE_FB_WRITE
= 256,
77 static int using_new_fs
= -1;
78 static struct brw_reg
brw_reg_from_fs_reg(class fs_reg
*reg
);
81 brw_new_shader(GLcontext
*ctx
, GLuint name
, GLuint type
)
83 struct brw_shader
*shader
;
85 shader
= talloc_zero(NULL
, struct brw_shader
);
87 shader
->base
.Type
= type
;
88 shader
->base
.Name
= name
;
89 _mesa_init_shader(ctx
, &shader
->base
);
95 struct gl_shader_program
*
96 brw_new_shader_program(GLcontext
*ctx
, GLuint name
)
98 struct brw_shader_program
*prog
;
99 prog
= talloc_zero(NULL
, struct brw_shader_program
);
101 prog
->base
.Name
= name
;
102 _mesa_init_shader_program(ctx
, &prog
->base
);
108 brw_compile_shader(GLcontext
*ctx
, struct gl_shader
*shader
)
110 if (!_mesa_ir_compile_shader(ctx
, shader
))
117 brw_link_shader(GLcontext
*ctx
, struct gl_shader_program
*prog
)
119 if (using_new_fs
== -1)
120 using_new_fs
= getenv("INTEL_NEW_FS") != NULL
;
122 for (unsigned i
= 0; i
< prog
->_NumLinkedShaders
; i
++) {
123 struct brw_shader
*shader
= (struct brw_shader
*)prog
->_LinkedShaders
[i
];
125 if (using_new_fs
&& shader
->base
.Type
== GL_FRAGMENT_SHADER
) {
126 void *mem_ctx
= talloc_new(NULL
);
130 talloc_free(shader
->ir
);
131 shader
->ir
= new(shader
) exec_list
;
132 clone_ir_list(mem_ctx
, shader
->ir
, shader
->base
.ir
);
134 do_mat_op_to_vec(shader
->ir
);
135 do_mod_to_fract(shader
->ir
);
136 do_div_to_mul_rcp(shader
->ir
);
137 do_sub_to_add_neg(shader
->ir
);
138 do_explog_to_explog2(shader
->ir
);
143 brw_do_channel_expressions(shader
->ir
);
144 brw_do_vector_splitting(shader
->ir
);
146 progress
= do_lower_jumps(shader
->ir
, true, true,
147 true, /* main return */
148 false, /* continue */
152 progress
= do_common_optimization(shader
->ir
, true, 32) || progress
;
154 progress
= lower_noise(shader
->ir
) || progress
;
156 lower_variable_index_to_cond_assign(shader
->ir
,
158 GL_TRUE
, /* output */
160 GL_TRUE
/* uniform */
164 validate_ir_tree(shader
->ir
);
166 reparent_ir(shader
->ir
, shader
->ir
);
167 talloc_free(mem_ctx
);
171 if (!_mesa_ir_link_shader(ctx
, prog
))
178 type_size(const struct glsl_type
*type
)
180 unsigned int size
, i
;
182 switch (type
->base_type
) {
185 case GLSL_TYPE_FLOAT
:
187 return type
->components();
188 case GLSL_TYPE_ARRAY
:
189 /* FINISHME: uniform/varying arrays. */
190 return type_size(type
->fields
.array
) * type
->length
;
191 case GLSL_TYPE_STRUCT
:
193 for (i
= 0; i
< type
->length
; i
++) {
194 size
+= type_size(type
->fields
.structure
[i
].type
);
197 case GLSL_TYPE_SAMPLER
:
198 /* Samplers take up no register space, since they're baked in at
203 assert(!"not reached");
210 /* Callers of this talloc-based new need not call delete. It's
211 * easier to just talloc_free 'ctx' (or any of its ancestors). */
212 static void* operator new(size_t size
, void *ctx
)
216 node
= talloc_size(ctx
, size
);
217 assert(node
!= NULL
);
225 this->reg_offset
= 0;
231 /** Generic unset register constructor. */
235 this->file
= BAD_FILE
;
238 /** Immediate value constructor. */
243 this->type
= BRW_REGISTER_TYPE_F
;
247 /** Immediate value constructor. */
252 this->type
= BRW_REGISTER_TYPE_D
;
256 /** Immediate value constructor. */
261 this->type
= BRW_REGISTER_TYPE_UD
;
265 /** Fixed brw_reg Immediate value constructor. */
266 fs_reg(struct brw_reg fixed_hw_reg
)
269 this->file
= FIXED_HW_REG
;
270 this->fixed_hw_reg
= fixed_hw_reg
;
271 this->type
= fixed_hw_reg
.type
;
274 fs_reg(enum register_file file
, int hw_reg
);
275 fs_reg(class fs_visitor
*v
, const struct glsl_type
*type
);
277 /** Register file: ARF, GRF, MRF, IMM. */
278 enum register_file file
;
279 /** Abstract register number. 0 = fixed hw reg */
281 /** Offset within the abstract register. */
283 /** HW register number. Generally unset until register allocation. */
285 /** Register type. BRW_REGISTER_TYPE_* */
289 struct brw_reg fixed_hw_reg
;
291 /** Value for file == BRW_IMMMEDIATE_FILE */
299 static const fs_reg reg_undef
;
300 static const fs_reg
reg_null(ARF
, BRW_ARF_NULL
);
302 class fs_inst
: public exec_node
{
304 /* Callers of this talloc-based new need not call delete. It's
305 * easier to just talloc_free 'ctx' (or any of its ancestors). */
306 static void* operator new(size_t size
, void *ctx
)
310 node
= talloc_zero_size(ctx
, size
);
311 assert(node
!= NULL
);
318 this->opcode
= BRW_OPCODE_NOP
;
319 this->saturate
= false;
320 this->conditional_mod
= BRW_CONDITIONAL_NONE
;
321 this->predicated
= false;
325 this->shadow_compare
= false;
336 this->opcode
= opcode
;
339 fs_inst(int opcode
, fs_reg dst
, fs_reg src0
)
342 this->opcode
= opcode
;
347 fs_inst(int opcode
, fs_reg dst
, fs_reg src0
, fs_reg src1
)
350 this->opcode
= opcode
;
356 fs_inst(int opcode
, fs_reg dst
, fs_reg src0
, fs_reg src1
, fs_reg src2
)
359 this->opcode
= opcode
;
366 int opcode
; /* BRW_OPCODE_* or FS_OPCODE_* */
371 int conditional_mod
; /**< BRW_CONDITIONAL_* */
373 int mlen
; /**< SEND message length */
375 int target
; /**< MRT target. */
380 * Annotation for the generated IR. One of the two can be set.
383 const char *annotation
;
387 class fs_visitor
: public ir_visitor
391 fs_visitor(struct brw_wm_compile
*c
, struct brw_shader
*shader
)
396 this->fp
= brw
->fragment_program
;
397 this->intel
= &brw
->intel
;
398 this->ctx
= &intel
->ctx
;
399 this->mem_ctx
= talloc_new(NULL
);
400 this->shader
= shader
;
402 this->next_abstract_grf
= 1;
403 this->variable_ht
= hash_table_ctor(0,
404 hash_table_pointer_hash
,
405 hash_table_pointer_compare
);
407 this->frag_color
= NULL
;
408 this->frag_data
= NULL
;
409 this->frag_depth
= NULL
;
410 this->first_non_payload_grf
= 0;
412 this->current_annotation
= NULL
;
413 this->annotation_string
= NULL
;
414 this->annotation_ir
= NULL
;
415 this->base_ir
= NULL
;
419 talloc_free(this->mem_ctx
);
420 hash_table_dtor(this->variable_ht
);
423 fs_reg
*variable_storage(ir_variable
*var
);
425 void visit(ir_variable
*ir
);
426 void visit(ir_assignment
*ir
);
427 void visit(ir_dereference_variable
*ir
);
428 void visit(ir_dereference_record
*ir
);
429 void visit(ir_dereference_array
*ir
);
430 void visit(ir_expression
*ir
);
431 void visit(ir_texture
*ir
);
432 void visit(ir_if
*ir
);
433 void visit(ir_constant
*ir
);
434 void visit(ir_swizzle
*ir
);
435 void visit(ir_return
*ir
);
436 void visit(ir_loop
*ir
);
437 void visit(ir_loop_jump
*ir
);
438 void visit(ir_discard
*ir
);
439 void visit(ir_call
*ir
);
440 void visit(ir_function
*ir
);
441 void visit(ir_function_signature
*ir
);
443 fs_inst
*emit(fs_inst inst
);
444 void assign_curb_setup();
445 void assign_urb_setup();
447 void generate_code();
448 void generate_fb_write(fs_inst
*inst
);
449 void generate_linterp(fs_inst
*inst
, struct brw_reg dst
,
450 struct brw_reg
*src
);
451 void generate_tex(fs_inst
*inst
, struct brw_reg dst
, struct brw_reg src
);
452 void generate_math(fs_inst
*inst
, struct brw_reg dst
, struct brw_reg
*src
);
453 void generate_discard(fs_inst
*inst
);
454 void generate_ddx(fs_inst
*inst
, struct brw_reg dst
, struct brw_reg src
);
455 void generate_ddy(fs_inst
*inst
, struct brw_reg dst
, struct brw_reg src
);
457 void emit_dummy_fs();
458 void emit_interpolation();
459 void emit_pinterp(int location
);
460 void emit_fb_writes();
462 struct brw_reg
interp_reg(int location
, int channel
);
463 int setup_uniform_values(int loc
, const glsl_type
*type
);
465 struct brw_context
*brw
;
466 const struct gl_fragment_program
*fp
;
467 struct intel_context
*intel
;
469 struct brw_wm_compile
*c
;
470 struct brw_compile
*p
;
471 struct brw_shader
*shader
;
473 exec_list instructions
;
474 int next_abstract_grf
;
475 struct hash_table
*variable_ht
;
476 ir_variable
*frag_color
, *frag_data
, *frag_depth
;
477 int first_non_payload_grf
;
479 /** @{ debug annotation info */
480 const char *current_annotation
;
481 ir_instruction
*base_ir
;
482 const char **annotation_string
;
483 ir_instruction
**annotation_ir
;
488 /* Result of last visit() method. */
496 fs_reg interp_attrs
[64];
502 /** Fixed HW reg constructor. */
503 fs_reg::fs_reg(enum register_file file
, int hw_reg
)
507 this->hw_reg
= hw_reg
;
508 this->type
= BRW_REGISTER_TYPE_F
;
512 brw_type_for_base_type(const struct glsl_type
*type
)
514 switch (type
->base_type
) {
515 case GLSL_TYPE_FLOAT
:
516 return BRW_REGISTER_TYPE_F
;
519 return BRW_REGISTER_TYPE_D
;
521 return BRW_REGISTER_TYPE_UD
;
522 case GLSL_TYPE_ARRAY
:
523 case GLSL_TYPE_STRUCT
:
524 /* These should be overridden with the type of the member when
525 * dereferenced into. BRW_REGISTER_TYPE_UD seems like a likely
526 * way to trip up if we don't.
528 return BRW_REGISTER_TYPE_UD
;
530 assert(!"not reached");
531 return BRW_REGISTER_TYPE_F
;
535 /** Automatic reg constructor. */
536 fs_reg::fs_reg(class fs_visitor
*v
, const struct glsl_type
*type
)
541 this->reg
= v
->next_abstract_grf
;
542 this->reg_offset
= 0;
543 v
->next_abstract_grf
+= type_size(type
);
544 this->type
= brw_type_for_base_type(type
);
548 fs_visitor::variable_storage(ir_variable
*var
)
550 return (fs_reg
*)hash_table_find(this->variable_ht
, var
);
553 /* Our support for uniforms is piggy-backed on the struct
554 * gl_fragment_program, because that's where the values actually
555 * get stored, rather than in some global gl_shader_program uniform
559 fs_visitor::setup_uniform_values(int loc
, const glsl_type
*type
)
561 unsigned int offset
= 0;
564 if (type
->is_matrix()) {
565 const glsl_type
*column
= glsl_type::get_instance(GLSL_TYPE_FLOAT
,
566 type
->vector_elements
,
569 for (unsigned int i
= 0; i
< type
->matrix_columns
; i
++) {
570 offset
+= setup_uniform_values(loc
+ offset
, column
);
576 switch (type
->base_type
) {
577 case GLSL_TYPE_FLOAT
:
581 vec_values
= fp
->Base
.Parameters
->ParameterValues
[loc
];
582 for (unsigned int i
= 0; i
< type
->vector_elements
; i
++) {
583 c
->prog_data
.param
[c
->prog_data
.nr_params
++] = &vec_values
[i
];
587 case GLSL_TYPE_STRUCT
:
588 for (unsigned int i
= 0; i
< type
->length
; i
++) {
589 offset
+= setup_uniform_values(loc
+ offset
,
590 type
->fields
.structure
[i
].type
);
594 case GLSL_TYPE_ARRAY
:
595 for (unsigned int i
= 0; i
< type
->length
; i
++) {
596 offset
+= setup_uniform_values(loc
+ offset
, type
->fields
.array
);
600 case GLSL_TYPE_SAMPLER
:
601 /* The sampler takes up a slot, but we don't use any values from it. */
605 assert(!"not reached");
611 fs_visitor::visit(ir_variable
*ir
)
615 if (strcmp(ir
->name
, "gl_FragColor") == 0) {
616 this->frag_color
= ir
;
617 } else if (strcmp(ir
->name
, "gl_FragData") == 0) {
618 this->frag_data
= ir
;
619 } else if (strcmp(ir
->name
, "gl_FragDepth") == 0) {
620 this->frag_depth
= ir
;
623 if (ir
->mode
== ir_var_in
) {
624 if (strcmp(ir
->name
, "gl_FrontFacing") == 0) {
625 reg
= new(this->mem_ctx
) fs_reg(this, ir
->type
);
626 struct brw_reg r1_6ud
= retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_UD
);
627 /* bit 31 is "primitive is back face", so checking < (1 << 31) gives
630 fs_inst
*inst
= emit(fs_inst(BRW_OPCODE_CMP
,
634 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
635 emit(fs_inst(BRW_OPCODE_AND
, *reg
, *reg
, fs_reg(1u)));
637 reg
= &this->interp_attrs
[ir
->location
];
641 if (ir
->mode
== ir_var_uniform
) {
642 int param_index
= c
->prog_data
.nr_params
;
644 setup_uniform_values(ir
->location
, ir
->type
);
646 reg
= new(this->mem_ctx
) fs_reg(UNIFORM
, param_index
);
650 reg
= new(this->mem_ctx
) fs_reg(this, ir
->type
);
652 hash_table_insert(this->variable_ht
, reg
, ir
);
656 fs_visitor::visit(ir_dereference_variable
*ir
)
658 fs_reg
*reg
= variable_storage(ir
->var
);
663 fs_visitor::visit(ir_dereference_record
*ir
)
665 const glsl_type
*struct_type
= ir
->record
->type
;
667 ir
->record
->accept(this);
669 unsigned int offset
= 0;
670 for (unsigned int i
= 0; i
< struct_type
->length
; i
++) {
671 if (strcmp(struct_type
->fields
.structure
[i
].name
, ir
->field
) == 0)
673 offset
+= type_size(struct_type
->fields
.structure
[i
].type
);
675 this->result
.reg_offset
+= offset
;
676 this->result
.type
= brw_type_for_base_type(ir
->type
);
680 fs_visitor::visit(ir_dereference_array
*ir
)
685 ir
->array
->accept(this);
686 index
= ir
->array_index
->as_constant();
688 if (ir
->type
->is_matrix()) {
689 element_size
= ir
->type
->vector_elements
;
691 element_size
= type_size(ir
->type
);
692 this->result
.type
= brw_type_for_base_type(ir
->type
);
696 assert(this->result
.file
== UNIFORM
||
697 (this->result
.file
== GRF
&&
698 this->result
.reg
!= 0));
699 this->result
.reg_offset
+= index
->value
.i
[0] * element_size
;
701 assert(!"FINISHME: non-constant matrix column");
706 fs_visitor::visit(ir_expression
*ir
)
708 unsigned int operand
;
713 for (operand
= 0; operand
< ir
->get_num_operands(); operand
++) {
714 ir
->operands
[operand
]->accept(this);
715 if (this->result
.file
== BAD_FILE
) {
717 printf("Failed to get tree for expression operand:\n");
718 ir
->operands
[operand
]->accept(&v
);
721 op
[operand
] = this->result
;
723 /* Matrix expression operands should have been broken down to vector
724 * operations already.
726 assert(!ir
->operands
[operand
]->type
->is_matrix());
727 /* And then those vector operands should have been broken down to scalar.
729 assert(!ir
->operands
[operand
]->type
->is_vector());
732 /* Storage for our result. If our result goes into an assignment, it will
733 * just get copy-propagated out, so no worries.
735 this->result
= fs_reg(this, ir
->type
);
737 switch (ir
->operation
) {
738 case ir_unop_logic_not
:
739 emit(fs_inst(BRW_OPCODE_ADD
, this->result
, op
[0], fs_reg(-1)));
742 op
[0].negate
= !op
[0].negate
;
743 this->result
= op
[0];
747 this->result
= op
[0];
750 temp
= fs_reg(this, ir
->type
);
752 emit(fs_inst(BRW_OPCODE_MOV
, this->result
, fs_reg(0.0f
)));
754 inst
= emit(fs_inst(BRW_OPCODE_CMP
, reg_null
, op
[0], fs_reg(0.0f
)));
755 inst
->conditional_mod
= BRW_CONDITIONAL_G
;
756 inst
= emit(fs_inst(BRW_OPCODE_MOV
, this->result
, fs_reg(1.0f
)));
757 inst
->predicated
= true;
759 inst
= emit(fs_inst(BRW_OPCODE_CMP
, reg_null
, op
[0], fs_reg(0.0f
)));
760 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
761 inst
= emit(fs_inst(BRW_OPCODE_MOV
, this->result
, fs_reg(-1.0f
)));
762 inst
->predicated
= true;
766 emit(fs_inst(FS_OPCODE_RCP
, this->result
, op
[0]));
770 emit(fs_inst(FS_OPCODE_EXP2
, this->result
, op
[0]));
773 emit(fs_inst(FS_OPCODE_LOG2
, this->result
, op
[0]));
777 assert(!"not reached: should be handled by ir_explog_to_explog2");
780 emit(fs_inst(FS_OPCODE_SIN
, this->result
, op
[0]));
783 emit(fs_inst(FS_OPCODE_COS
, this->result
, op
[0]));
787 emit(fs_inst(FS_OPCODE_DDX
, this->result
, op
[0]));
790 emit(fs_inst(FS_OPCODE_DDY
, this->result
, op
[0]));
794 emit(fs_inst(BRW_OPCODE_ADD
, this->result
, op
[0], op
[1]));
797 assert(!"not reached: should be handled by ir_sub_to_add_neg");
801 emit(fs_inst(BRW_OPCODE_MUL
, this->result
, op
[0], op
[1]));
804 assert(!"not reached: should be handled by ir_div_to_mul_rcp");
807 assert(!"ir_binop_mod should have been converted to b * fract(a/b)");
811 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], op
[1]));
812 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
813 emit(fs_inst(BRW_OPCODE_AND
, this->result
, this->result
, fs_reg(0x1)));
815 case ir_binop_greater
:
816 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], op
[1]));
817 inst
->conditional_mod
= BRW_CONDITIONAL_G
;
818 emit(fs_inst(BRW_OPCODE_AND
, this->result
, this->result
, fs_reg(0x1)));
820 case ir_binop_lequal
:
821 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], op
[1]));
822 inst
->conditional_mod
= BRW_CONDITIONAL_LE
;
823 emit(fs_inst(BRW_OPCODE_AND
, this->result
, this->result
, fs_reg(0x1)));
825 case ir_binop_gequal
:
826 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], op
[1]));
827 inst
->conditional_mod
= BRW_CONDITIONAL_GE
;
828 emit(fs_inst(BRW_OPCODE_AND
, this->result
, this->result
, fs_reg(0x1)));
831 case ir_binop_all_equal
: /* same as nequal for scalars */
832 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], op
[1]));
833 inst
->conditional_mod
= BRW_CONDITIONAL_Z
;
834 emit(fs_inst(BRW_OPCODE_AND
, this->result
, this->result
, fs_reg(0x1)));
836 case ir_binop_nequal
:
837 case ir_binop_any_nequal
: /* same as nequal for scalars */
838 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], op
[1]));
839 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
840 emit(fs_inst(BRW_OPCODE_AND
, this->result
, this->result
, fs_reg(0x1)));
843 case ir_binop_logic_xor
:
844 emit(fs_inst(BRW_OPCODE_XOR
, this->result
, op
[0], op
[1]));
847 case ir_binop_logic_or
:
848 emit(fs_inst(BRW_OPCODE_OR
, this->result
, op
[0], op
[1]));
851 case ir_binop_logic_and
:
852 emit(fs_inst(BRW_OPCODE_AND
, this->result
, op
[0], op
[1]));
858 assert(!"not reached: should be handled by brw_fs_channel_expressions");
862 assert(!"not reached: should be handled by lower_noise");
866 emit(fs_inst(FS_OPCODE_SQRT
, this->result
, op
[0]));
870 emit(fs_inst(FS_OPCODE_RSQ
, this->result
, op
[0]));
876 emit(fs_inst(BRW_OPCODE_MOV
, this->result
, op
[0]));
879 emit(fs_inst(BRW_OPCODE_MOV
, this->result
, op
[0]));
883 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], fs_reg(0.0f
)));
884 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
887 emit(fs_inst(BRW_OPCODE_RNDD
, this->result
, op
[0]));
890 op
[0].negate
= ~op
[0].negate
;
891 inst
= emit(fs_inst(BRW_OPCODE_RNDD
, this->result
, op
[0]));
892 this->result
.negate
= true;
895 inst
= emit(fs_inst(BRW_OPCODE_RNDD
, this->result
, op
[0]));
898 inst
= emit(fs_inst(BRW_OPCODE_FRC
, this->result
, op
[0]));
902 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], op
[1]));
903 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
905 inst
= emit(fs_inst(BRW_OPCODE_SEL
, this->result
, op
[0], op
[1]));
906 inst
->predicated
= true;
909 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], op
[1]));
910 inst
->conditional_mod
= BRW_CONDITIONAL_G
;
912 inst
= emit(fs_inst(BRW_OPCODE_SEL
, this->result
, op
[0], op
[1]));
913 inst
->predicated
= true;
917 inst
= emit(fs_inst(FS_OPCODE_POW
, this->result
, op
[0], op
[1]));
920 case ir_unop_bit_not
:
922 case ir_binop_lshift
:
923 case ir_binop_rshift
:
924 case ir_binop_bit_and
:
925 case ir_binop_bit_xor
:
926 case ir_binop_bit_or
:
927 assert(!"GLSL 1.30 features unsupported");
933 fs_visitor::visit(ir_assignment
*ir
)
940 /* FINISHME: arrays on the lhs */
941 ir
->lhs
->accept(this);
944 ir
->rhs
->accept(this);
947 /* FINISHME: This should really set to the correct maximal writemask for each
948 * FINISHME: component written (in the loops below). This case can only
949 * FINISHME: occur for matrices, arrays, and structures.
951 if (ir
->write_mask
== 0) {
952 assert(!ir
->lhs
->type
->is_scalar() && !ir
->lhs
->type
->is_vector());
953 write_mask
= WRITEMASK_XYZW
;
955 assert(ir
->lhs
->type
->is_vector() || ir
->lhs
->type
->is_scalar());
956 write_mask
= ir
->write_mask
;
959 assert(l
.file
!= BAD_FILE
);
960 assert(r
.file
!= BAD_FILE
);
963 /* Get the condition bool into the predicate. */
964 ir
->condition
->accept(this);
965 inst
= emit(fs_inst(BRW_OPCODE_CMP
, reg_null
, this->result
, fs_reg(0)));
966 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
969 for (i
= 0; i
< type_size(ir
->lhs
->type
); i
++) {
970 if (i
>= 4 || (write_mask
& (1 << i
))) {
971 inst
= emit(fs_inst(BRW_OPCODE_MOV
, l
, r
));
973 inst
->predicated
= true;
981 fs_visitor::visit(ir_texture
*ir
)
984 fs_inst
*inst
= NULL
;
985 unsigned int mlen
= 0;
987 ir
->coordinate
->accept(this);
988 fs_reg coordinate
= this->result
;
991 fs_reg inv_proj
= fs_reg(this, glsl_type::float_type
);
993 ir
->projector
->accept(this);
994 emit(fs_inst(FS_OPCODE_RCP
, inv_proj
, this->result
));
996 fs_reg proj_coordinate
= fs_reg(this, ir
->coordinate
->type
);
997 for (unsigned int i
= 0; i
< ir
->coordinate
->type
->vector_elements
; i
++) {
998 emit(fs_inst(BRW_OPCODE_MUL
, proj_coordinate
, coordinate
, inv_proj
));
999 coordinate
.reg_offset
++;
1000 proj_coordinate
.reg_offset
++;
1002 proj_coordinate
.reg_offset
= 0;
1004 coordinate
= proj_coordinate
;
1007 for (mlen
= 0; mlen
< ir
->coordinate
->type
->vector_elements
; mlen
++) {
1008 emit(fs_inst(BRW_OPCODE_MOV
, fs_reg(MRF
, base_mrf
+ mlen
), coordinate
));
1009 coordinate
.reg_offset
++;
1012 /* Pre-Ironlake, the 8-wide sampler always took u,v,r. */
1016 if (ir
->shadow_comparitor
) {
1017 /* For shadow comparisons, we have to supply u,v,r. */
1020 ir
->shadow_comparitor
->accept(this);
1021 emit(fs_inst(BRW_OPCODE_MOV
, fs_reg(MRF
, base_mrf
+ mlen
), this->result
));
1025 /* Do we ever want to handle writemasking on texture samples? Is it
1026 * performance relevant?
1028 fs_reg dst
= fs_reg(this, glsl_type::vec4_type
);
1032 inst
= emit(fs_inst(FS_OPCODE_TEX
, dst
, fs_reg(MRF
, base_mrf
)));
1035 ir
->lod_info
.bias
->accept(this);
1036 emit(fs_inst(BRW_OPCODE_MOV
, fs_reg(MRF
, base_mrf
+ mlen
), this->result
));
1039 inst
= emit(fs_inst(FS_OPCODE_TXB
, dst
, fs_reg(MRF
, base_mrf
)));
1042 ir
->lod_info
.lod
->accept(this);
1043 emit(fs_inst(BRW_OPCODE_MOV
, fs_reg(MRF
, base_mrf
+ mlen
), this->result
));
1046 inst
= emit(fs_inst(FS_OPCODE_TXL
, dst
, fs_reg(MRF
, base_mrf
)));
1050 assert(!"GLSL 1.30 features unsupported");
1055 _mesa_get_sampler_uniform_value(ir
->sampler
,
1056 ctx
->Shader
.CurrentProgram
,
1057 &brw
->fragment_program
->Base
);
1058 inst
->sampler
= c
->fp
->program
.Base
.SamplerUnits
[inst
->sampler
];
1062 if (ir
->shadow_comparitor
)
1063 inst
->shadow_compare
= true;
1068 fs_visitor::visit(ir_swizzle
*ir
)
1070 ir
->val
->accept(this);
1071 fs_reg val
= this->result
;
1073 fs_reg result
= fs_reg(this, ir
->type
);
1074 this->result
= result
;
1076 for (unsigned int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1077 fs_reg channel
= val
;
1095 channel
.reg_offset
+= swiz
;
1096 emit(fs_inst(BRW_OPCODE_MOV
, result
, channel
));
1097 result
.reg_offset
++;
1102 fs_visitor::visit(ir_discard
*ir
)
1104 assert(ir
->condition
== NULL
); /* FINISHME */
1106 emit(fs_inst(FS_OPCODE_DISCARD
));
1110 fs_visitor::visit(ir_constant
*ir
)
1112 fs_reg
reg(this, ir
->type
);
1115 for (unsigned int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1116 switch (ir
->type
->base_type
) {
1117 case GLSL_TYPE_FLOAT
:
1118 emit(fs_inst(BRW_OPCODE_MOV
, reg
, fs_reg(ir
->value
.f
[i
])));
1120 case GLSL_TYPE_UINT
:
1121 emit(fs_inst(BRW_OPCODE_MOV
, reg
, fs_reg(ir
->value
.u
[i
])));
1124 emit(fs_inst(BRW_OPCODE_MOV
, reg
, fs_reg(ir
->value
.i
[i
])));
1126 case GLSL_TYPE_BOOL
:
1127 emit(fs_inst(BRW_OPCODE_MOV
, reg
, fs_reg((int)ir
->value
.b
[i
])));
1130 assert(!"Non-float/uint/int/bool constant");
1137 fs_visitor::visit(ir_if
*ir
)
1141 /* Don't point the annotation at the if statement, because then it plus
1142 * the then and else blocks get printed.
1144 this->base_ir
= ir
->condition
;
1146 /* Generate the condition into the condition code. */
1147 ir
->condition
->accept(this);
1148 inst
= emit(fs_inst(BRW_OPCODE_MOV
, fs_reg(brw_null_reg()), this->result
));
1149 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1151 inst
= emit(fs_inst(BRW_OPCODE_IF
));
1152 inst
->predicated
= true;
1154 foreach_iter(exec_list_iterator
, iter
, ir
->then_instructions
) {
1155 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
1161 if (!ir
->else_instructions
.is_empty()) {
1162 emit(fs_inst(BRW_OPCODE_ELSE
));
1164 foreach_iter(exec_list_iterator
, iter
, ir
->else_instructions
) {
1165 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
1172 emit(fs_inst(BRW_OPCODE_ENDIF
));
1176 fs_visitor::visit(ir_loop
*ir
)
1180 assert(!ir
->increment
);
1181 assert(!ir
->counter
);
1183 emit(fs_inst(BRW_OPCODE_DO
));
1185 /* Start a safety counter. If the user messed up their loop
1186 * counting, we don't want to hang the GPU.
1188 fs_reg max_iter
= fs_reg(this, glsl_type::int_type
);
1189 emit(fs_inst(BRW_OPCODE_MOV
, max_iter
, fs_reg(10000)));
1191 foreach_iter(exec_list_iterator
, iter
, ir
->body_instructions
) {
1192 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
1198 /* Check the maximum loop iters counter. */
1199 inst
= emit(fs_inst(BRW_OPCODE_ADD
, max_iter
, max_iter
, fs_reg(-1)));
1200 inst
->conditional_mod
= BRW_CONDITIONAL_Z
;
1202 inst
= emit(fs_inst(BRW_OPCODE_BREAK
));
1203 inst
->predicated
= true;
1206 emit(fs_inst(BRW_OPCODE_WHILE
));
1210 fs_visitor::visit(ir_loop_jump
*ir
)
1213 case ir_loop_jump::jump_break
:
1214 emit(fs_inst(BRW_OPCODE_BREAK
));
1216 case ir_loop_jump::jump_continue
:
1217 emit(fs_inst(BRW_OPCODE_CONTINUE
));
1223 fs_visitor::visit(ir_call
*ir
)
1225 assert(!"FINISHME");
1229 fs_visitor::visit(ir_return
*ir
)
1231 assert(!"FINISHME");
1235 fs_visitor::visit(ir_function
*ir
)
1237 /* Ignore function bodies other than main() -- we shouldn't see calls to
1238 * them since they should all be inlined before we get to ir_to_mesa.
1240 if (strcmp(ir
->name
, "main") == 0) {
1241 const ir_function_signature
*sig
;
1244 sig
= ir
->matching_signature(&empty
);
1248 foreach_iter(exec_list_iterator
, iter
, sig
->body
) {
1249 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
1258 fs_visitor::visit(ir_function_signature
*ir
)
1260 assert(!"not reached");
1265 fs_visitor::emit(fs_inst inst
)
1267 fs_inst
*list_inst
= new(mem_ctx
) fs_inst
;
1270 list_inst
->annotation
= this->current_annotation
;
1271 list_inst
->ir
= this->base_ir
;
1273 this->instructions
.push_tail(list_inst
);
1278 /** Emits a dummy fragment shader consisting of magenta for bringup purposes. */
1280 fs_visitor::emit_dummy_fs()
1282 /* Everyone's favorite color. */
1283 emit(fs_inst(BRW_OPCODE_MOV
,
1286 emit(fs_inst(BRW_OPCODE_MOV
,
1289 emit(fs_inst(BRW_OPCODE_MOV
,
1292 emit(fs_inst(BRW_OPCODE_MOV
,
1297 write
= emit(fs_inst(FS_OPCODE_FB_WRITE
,
1302 /* The register location here is relative to the start of the URB
1303 * data. It will get adjusted to be a real location before
1304 * generate_code() time.
1307 fs_visitor::interp_reg(int location
, int channel
)
1309 int regnr
= location
* 2 + channel
/ 2;
1310 int stride
= (channel
& 1) * 4;
1312 return brw_vec1_grf(regnr
, stride
);
1315 /** Emits the interpolation for the varying inputs. */
1317 fs_visitor::emit_interpolation()
1319 struct brw_reg g1_uw
= retype(brw_vec1_grf(1, 0), BRW_REGISTER_TYPE_UW
);
1320 /* For now, the source regs for the setup URB data will be unset,
1321 * since we don't know until codegen how many push constants we'll
1322 * use, and therefore what the setup URB offset is.
1324 fs_reg src_reg
= reg_undef
;
1326 this->current_annotation
= "compute pixel centers";
1327 this->pixel_x
= fs_reg(this, glsl_type::uint_type
);
1328 this->pixel_y
= fs_reg(this, glsl_type::uint_type
);
1329 this->pixel_x
.type
= BRW_REGISTER_TYPE_UW
;
1330 this->pixel_y
.type
= BRW_REGISTER_TYPE_UW
;
1331 emit(fs_inst(BRW_OPCODE_ADD
,
1333 fs_reg(stride(suboffset(g1_uw
, 4), 2, 4, 0)),
1334 fs_reg(brw_imm_v(0x10101010))));
1335 emit(fs_inst(BRW_OPCODE_ADD
,
1337 fs_reg(stride(suboffset(g1_uw
, 5), 2, 4, 0)),
1338 fs_reg(brw_imm_v(0x11001100))));
1340 this->current_annotation
= "compute pixel deltas from v0";
1341 this->delta_x
= fs_reg(this, glsl_type::float_type
);
1342 this->delta_y
= fs_reg(this, glsl_type::float_type
);
1343 emit(fs_inst(BRW_OPCODE_ADD
,
1346 fs_reg(negate(brw_vec1_grf(1, 0)))));
1347 emit(fs_inst(BRW_OPCODE_ADD
,
1350 fs_reg(negate(brw_vec1_grf(1, 1)))));
1352 this->current_annotation
= "compute pos.w and 1/pos.w";
1353 /* Compute wpos. Unlike many other varying inputs, we usually need it
1354 * to produce 1/w, and the varying variable wouldn't show up.
1356 fs_reg wpos
= fs_reg(this, glsl_type::vec4_type
);
1357 this->interp_attrs
[FRAG_ATTRIB_WPOS
] = wpos
;
1358 emit(fs_inst(BRW_OPCODE_MOV
, wpos
, this->pixel_x
)); /* FINISHME: ARB_fcc */
1360 emit(fs_inst(BRW_OPCODE_MOV
, wpos
, this->pixel_y
)); /* FINISHME: ARB_fcc */
1362 emit(fs_inst(FS_OPCODE_LINTERP
, wpos
, this->delta_x
, this->delta_y
,
1363 interp_reg(FRAG_ATTRIB_WPOS
, 2)));
1365 emit(fs_inst(FS_OPCODE_LINTERP
, wpos
, this->delta_x
, this->delta_y
,
1366 interp_reg(FRAG_ATTRIB_WPOS
, 3)));
1367 /* Compute the pixel W value from wpos.w. */
1368 this->pixel_w
= fs_reg(this, glsl_type::float_type
);
1369 emit(fs_inst(FS_OPCODE_RCP
, this->pixel_w
, wpos
));
1371 foreach_iter(exec_list_iterator
, iter
, *this->shader
->ir
) {
1372 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
1373 ir_variable
*var
= ir
->as_variable();
1378 if (var
->mode
!= ir_var_in
)
1381 /* If it's already set up (WPOS), skip. */
1382 if (var
->location
== 0)
1385 this->current_annotation
= talloc_asprintf(this->mem_ctx
,
1387 "(FRAG_ATTRIB[%d])",
1390 emit_pinterp(var
->location
);
1392 this->current_annotation
= NULL
;
1396 fs_visitor::emit_pinterp(int location
)
1398 fs_reg interp_attr
= fs_reg(this, glsl_type::vec4_type
);
1399 this->interp_attrs
[location
] = interp_attr
;
1401 for (unsigned int i
= 0; i
< 4; i
++) {
1402 struct brw_reg interp
= interp_reg(location
, i
);
1403 emit(fs_inst(FS_OPCODE_LINTERP
,
1408 interp_attr
.reg_offset
++;
1410 interp_attr
.reg_offset
-= 4;
1412 for (unsigned int i
= 0; i
< 4; i
++) {
1413 emit(fs_inst(BRW_OPCODE_MUL
,
1417 interp_attr
.reg_offset
++;
1422 fs_visitor::emit_fb_writes()
1424 this->current_annotation
= "FB write header";
1430 if (c
->key
.aa_dest_stencil_reg
) {
1431 emit(fs_inst(BRW_OPCODE_MOV
, fs_reg(MRF
, nr
++),
1432 fs_reg(brw_vec8_grf(c
->key
.aa_dest_stencil_reg
, 0))));
1435 /* Reserve space for color. It'll be filled in per MRT below. */
1439 if (c
->key
.source_depth_to_render_target
) {
1440 if (c
->key
.computes_depth
) {
1441 /* Hand over gl_FragDepth. */
1442 assert(this->frag_depth
);
1443 fs_reg depth
= *(variable_storage(this->frag_depth
));
1445 emit(fs_inst(BRW_OPCODE_MOV
, fs_reg(MRF
, nr
++), depth
));
1447 /* Pass through the payload depth. */
1448 emit(fs_inst(BRW_OPCODE_MOV
, fs_reg(MRF
, nr
++),
1449 fs_reg(brw_vec8_grf(c
->key
.source_depth_reg
, 0))));
1453 if (c
->key
.dest_depth_reg
) {
1454 emit(fs_inst(BRW_OPCODE_MOV
, fs_reg(MRF
, nr
++),
1455 fs_reg(brw_vec8_grf(c
->key
.dest_depth_reg
, 0))));
1458 fs_reg color
= reg_undef
;
1459 if (this->frag_color
)
1460 color
= *(variable_storage(this->frag_color
));
1461 else if (this->frag_data
)
1462 color
= *(variable_storage(this->frag_data
));
1464 for (int target
= 0; target
< c
->key
.nr_color_regions
; target
++) {
1465 this->current_annotation
= talloc_asprintf(this->mem_ctx
,
1466 "FB write target %d",
1468 if (this->frag_color
|| this->frag_data
) {
1469 for (int i
= 0; i
< 4; i
++) {
1470 emit(fs_inst(BRW_OPCODE_MOV
,
1471 fs_reg(MRF
, color_mrf
+ i
),
1477 if (this->frag_color
)
1478 color
.reg_offset
-= 4;
1480 fs_inst
*inst
= emit(fs_inst(FS_OPCODE_FB_WRITE
,
1481 reg_undef
, reg_undef
));
1482 inst
->target
= target
;
1484 if (target
== c
->key
.nr_color_regions
- 1)
1488 if (c
->key
.nr_color_regions
== 0) {
1489 fs_inst
*inst
= emit(fs_inst(FS_OPCODE_FB_WRITE
,
1490 reg_undef
, reg_undef
));
1495 this->current_annotation
= NULL
;
1499 fs_visitor::generate_fb_write(fs_inst
*inst
)
1501 GLboolean eot
= inst
->eot
;
1503 /* Header is 2 regs, g0 and g1 are the contents. g0 will be implied
1506 brw_push_insn_state(p
);
1507 brw_set_mask_control(p
, BRW_MASK_DISABLE
);
1508 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
1511 brw_vec8_grf(1, 0));
1512 brw_pop_insn_state(p
);
1515 8, /* dispatch_width */
1516 retype(vec8(brw_null_reg()), BRW_REGISTER_TYPE_UW
),
1518 retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UW
),
1526 fs_visitor::generate_linterp(fs_inst
*inst
,
1527 struct brw_reg dst
, struct brw_reg
*src
)
1529 struct brw_reg delta_x
= src
[0];
1530 struct brw_reg delta_y
= src
[1];
1531 struct brw_reg interp
= src
[2];
1534 delta_y
.nr
== delta_x
.nr
+ 1 &&
1535 (intel
->gen
>= 6 || (delta_x
.nr
& 1) == 0)) {
1536 brw_PLN(p
, dst
, interp
, delta_x
);
1538 brw_LINE(p
, brw_null_reg(), interp
, delta_x
);
1539 brw_MAC(p
, dst
, suboffset(interp
, 1), delta_y
);
1544 fs_visitor::generate_math(fs_inst
*inst
,
1545 struct brw_reg dst
, struct brw_reg
*src
)
1549 switch (inst
->opcode
) {
1551 op
= BRW_MATH_FUNCTION_INV
;
1554 op
= BRW_MATH_FUNCTION_RSQ
;
1556 case FS_OPCODE_SQRT
:
1557 op
= BRW_MATH_FUNCTION_SQRT
;
1559 case FS_OPCODE_EXP2
:
1560 op
= BRW_MATH_FUNCTION_EXP
;
1562 case FS_OPCODE_LOG2
:
1563 op
= BRW_MATH_FUNCTION_LOG
;
1566 op
= BRW_MATH_FUNCTION_POW
;
1569 op
= BRW_MATH_FUNCTION_SIN
;
1572 op
= BRW_MATH_FUNCTION_COS
;
1575 assert(!"not reached: unknown math function");
1580 if (inst
->opcode
== FS_OPCODE_POW
) {
1581 brw_MOV(p
, brw_message_reg(3), src
[1]);
1586 inst
->saturate
? BRW_MATH_SATURATE_SATURATE
:
1587 BRW_MATH_SATURATE_NONE
,
1589 BRW_MATH_DATA_VECTOR
,
1590 BRW_MATH_PRECISION_FULL
);
1594 fs_visitor::generate_tex(fs_inst
*inst
, struct brw_reg dst
, struct brw_reg src
)
1599 if (intel
->gen
== 5) {
1600 switch (inst
->opcode
) {
1602 if (inst
->shadow_compare
) {
1603 msg_type
= BRW_SAMPLER_MESSAGE_SAMPLE_COMPARE_GEN5
;
1605 msg_type
= BRW_SAMPLER_MESSAGE_SAMPLE_GEN5
;
1609 if (inst
->shadow_compare
) {
1610 msg_type
= BRW_SAMPLER_MESSAGE_SAMPLE_BIAS_COMPARE_GEN5
;
1612 msg_type
= BRW_SAMPLER_MESSAGE_SAMPLE_BIAS_GEN5
;
1617 switch (inst
->opcode
) {
1619 /* Note that G45 and older determines shadow compare and dispatch width
1620 * from message length for most messages.
1622 if (inst
->shadow_compare
) {
1623 msg_type
= BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE_COMPARE
;
1625 msg_type
= BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE
;
1628 if (inst
->shadow_compare
) {
1629 assert(!"FINISHME: shadow compare with bias.");
1630 msg_type
= BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE_BIAS
;
1632 msg_type
= BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE_BIAS
;
1638 assert(msg_type
!= -1);
1644 retype(dst
, BRW_REGISTER_TYPE_UW
),
1646 retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UW
),
1647 SURF_INDEX_TEXTURE(inst
->sampler
),
1655 BRW_SAMPLER_SIMD_MODE_SIMD8
);
1659 /* For OPCODE_DDX and OPCODE_DDY, per channel of output we've got input
1662 * arg0: ss0.tl ss0.tr ss0.bl ss0.br ss1.tl ss1.tr ss1.bl ss1.br
1664 * and we're trying to produce:
1667 * dst: (ss0.tr - ss0.tl) (ss0.tl - ss0.bl)
1668 * (ss0.tr - ss0.tl) (ss0.tr - ss0.br)
1669 * (ss0.br - ss0.bl) (ss0.tl - ss0.bl)
1670 * (ss0.br - ss0.bl) (ss0.tr - ss0.br)
1671 * (ss1.tr - ss1.tl) (ss1.tl - ss1.bl)
1672 * (ss1.tr - ss1.tl) (ss1.tr - ss1.br)
1673 * (ss1.br - ss1.bl) (ss1.tl - ss1.bl)
1674 * (ss1.br - ss1.bl) (ss1.tr - ss1.br)
1676 * and add another set of two more subspans if in 16-pixel dispatch mode.
1678 * For DDX, it ends up being easy: width = 2, horiz=0 gets us the same result
1679 * for each pair, and vertstride = 2 jumps us 2 elements after processing a
1680 * pair. But for DDY, it's harder, as we want to produce the pairs swizzled
1681 * between each other. We could probably do it like ddx and swizzle the right
1682 * order later, but bail for now and just produce
1683 * ((ss0.tl - ss0.bl)x4 (ss1.tl - ss1.bl)x4)
1686 fs_visitor::generate_ddx(fs_inst
*inst
, struct brw_reg dst
, struct brw_reg src
)
1688 struct brw_reg src0
= brw_reg(src
.file
, src
.nr
, 1,
1689 BRW_REGISTER_TYPE_F
,
1690 BRW_VERTICAL_STRIDE_2
,
1692 BRW_HORIZONTAL_STRIDE_0
,
1693 BRW_SWIZZLE_XYZW
, WRITEMASK_XYZW
);
1694 struct brw_reg src1
= brw_reg(src
.file
, src
.nr
, 0,
1695 BRW_REGISTER_TYPE_F
,
1696 BRW_VERTICAL_STRIDE_2
,
1698 BRW_HORIZONTAL_STRIDE_0
,
1699 BRW_SWIZZLE_XYZW
, WRITEMASK_XYZW
);
1700 brw_ADD(p
, dst
, src0
, negate(src1
));
1704 fs_visitor::generate_ddy(fs_inst
*inst
, struct brw_reg dst
, struct brw_reg src
)
1706 struct brw_reg src0
= brw_reg(src
.file
, src
.nr
, 0,
1707 BRW_REGISTER_TYPE_F
,
1708 BRW_VERTICAL_STRIDE_4
,
1710 BRW_HORIZONTAL_STRIDE_0
,
1711 BRW_SWIZZLE_XYZW
, WRITEMASK_XYZW
);
1712 struct brw_reg src1
= brw_reg(src
.file
, src
.nr
, 2,
1713 BRW_REGISTER_TYPE_F
,
1714 BRW_VERTICAL_STRIDE_4
,
1716 BRW_HORIZONTAL_STRIDE_0
,
1717 BRW_SWIZZLE_XYZW
, WRITEMASK_XYZW
);
1718 brw_ADD(p
, dst
, src0
, negate(src1
));
1722 fs_visitor::generate_discard(fs_inst
*inst
)
1724 struct brw_reg g0
= retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UW
);
1725 brw_push_insn_state(p
);
1726 brw_set_mask_control(p
, BRW_MASK_DISABLE
);
1727 brw_NOT(p
, c
->emit_mask_reg
, brw_mask_reg(1)); /* IMASK */
1728 brw_AND(p
, g0
, c
->emit_mask_reg
, g0
);
1729 brw_pop_insn_state(p
);
1733 trivial_assign_reg(int header_size
, fs_reg
*reg
)
1735 if (reg
->file
== GRF
&& reg
->reg
!= 0) {
1736 reg
->hw_reg
= header_size
+ reg
->reg
- 1 + reg
->reg_offset
;
1742 fs_visitor::assign_curb_setup()
1744 c
->prog_data
.first_curbe_grf
= c
->key
.nr_payload_regs
;
1745 c
->prog_data
.curb_read_length
= ALIGN(c
->prog_data
.nr_params
, 8) / 8;
1747 if (intel
->gen
== 5 && (c
->prog_data
.first_curbe_grf
+
1748 c
->prog_data
.curb_read_length
) & 1) {
1749 /* Align the start of the interpolation coefficients so that we can use
1750 * the PLN instruction.
1752 c
->prog_data
.first_curbe_grf
++;
1755 /* Map the offsets in the UNIFORM file to fixed HW regs. */
1756 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
1757 fs_inst
*inst
= (fs_inst
*)iter
.get();
1759 for (unsigned int i
= 0; i
< 3; i
++) {
1760 if (inst
->src
[i
].file
== UNIFORM
) {
1761 int constant_nr
= inst
->src
[i
].hw_reg
+ inst
->src
[i
].reg_offset
;
1762 struct brw_reg brw_reg
= brw_vec1_grf(c
->prog_data
.first_curbe_grf
+
1766 inst
->src
[i
].file
= FIXED_HW_REG
;
1767 inst
->src
[i
].fixed_hw_reg
= brw_reg
;
1774 fs_visitor::assign_urb_setup()
1776 int urb_start
= c
->prog_data
.first_curbe_grf
+ c
->prog_data
.curb_read_length
;
1777 int interp_reg_nr
[FRAG_ATTRIB_MAX
];
1779 c
->prog_data
.urb_read_length
= 0;
1781 /* Figure out where each of the incoming setup attributes lands. */
1782 for (unsigned int i
= 0; i
< FRAG_ATTRIB_MAX
; i
++) {
1783 interp_reg_nr
[i
] = -1;
1785 if (i
!= FRAG_ATTRIB_WPOS
&&
1786 !(brw
->fragment_program
->Base
.InputsRead
& BITFIELD64_BIT(i
)))
1789 /* Each attribute is 4 setup channels, each of which is half a reg. */
1790 interp_reg_nr
[i
] = urb_start
+ c
->prog_data
.urb_read_length
;
1791 c
->prog_data
.urb_read_length
+= 2;
1794 /* Map the register numbers for FS_OPCODE_LINTERP so that it uses
1795 * the correct setup input.
1797 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
1798 fs_inst
*inst
= (fs_inst
*)iter
.get();
1800 if (inst
->opcode
!= FS_OPCODE_LINTERP
)
1803 assert(inst
->src
[2].file
== FIXED_HW_REG
);
1805 int location
= inst
->src
[2].fixed_hw_reg
.nr
/ 2;
1806 assert(interp_reg_nr
[location
] != -1);
1807 inst
->src
[2].fixed_hw_reg
.nr
= (interp_reg_nr
[location
] +
1808 (inst
->src
[2].fixed_hw_reg
.nr
& 1));
1811 this->first_non_payload_grf
= urb_start
+ c
->prog_data
.urb_read_length
;
1815 fs_visitor::assign_regs()
1817 int header_size
= this->first_non_payload_grf
;
1820 /* FINISHME: trivial assignment of register numbers */
1821 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
1822 fs_inst
*inst
= (fs_inst
*)iter
.get();
1824 trivial_assign_reg(header_size
, &inst
->dst
);
1825 trivial_assign_reg(header_size
, &inst
->src
[0]);
1826 trivial_assign_reg(header_size
, &inst
->src
[1]);
1828 last_grf
= MAX2(last_grf
, inst
->dst
.hw_reg
);
1829 last_grf
= MAX2(last_grf
, inst
->src
[0].hw_reg
);
1830 last_grf
= MAX2(last_grf
, inst
->src
[1].hw_reg
);
1833 this->grf_used
= last_grf
+ 1;
1836 static struct brw_reg
brw_reg_from_fs_reg(fs_reg
*reg
)
1838 struct brw_reg brw_reg
;
1840 switch (reg
->file
) {
1844 brw_reg
= brw_vec8_reg(reg
->file
,
1846 brw_reg
= retype(brw_reg
, reg
->type
);
1849 switch (reg
->type
) {
1850 case BRW_REGISTER_TYPE_F
:
1851 brw_reg
= brw_imm_f(reg
->imm
.f
);
1853 case BRW_REGISTER_TYPE_D
:
1854 brw_reg
= brw_imm_d(reg
->imm
.i
);
1856 case BRW_REGISTER_TYPE_UD
:
1857 brw_reg
= brw_imm_ud(reg
->imm
.u
);
1860 assert(!"not reached");
1865 brw_reg
= reg
->fixed_hw_reg
;
1868 /* Probably unused. */
1869 brw_reg
= brw_null_reg();
1872 assert(!"not reached");
1873 brw_reg
= brw_null_reg();
1877 brw_reg
= brw_abs(brw_reg
);
1879 brw_reg
= negate(brw_reg
);
1885 fs_visitor::generate_code()
1887 unsigned int annotation_len
= 0;
1888 int last_native_inst
= 0;
1889 struct brw_instruction
*if_stack
[16], *loop_stack
[16];
1890 int if_stack_depth
= 0, loop_stack_depth
= 0;
1891 int if_depth_in_loop
[16];
1893 if_depth_in_loop
[loop_stack_depth
] = 0;
1895 memset(&if_stack
, 0, sizeof(if_stack
));
1896 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
1897 fs_inst
*inst
= (fs_inst
*)iter
.get();
1898 struct brw_reg src
[3], dst
;
1900 for (unsigned int i
= 0; i
< 3; i
++) {
1901 src
[i
] = brw_reg_from_fs_reg(&inst
->src
[i
]);
1903 dst
= brw_reg_from_fs_reg(&inst
->dst
);
1905 brw_set_conditionalmod(p
, inst
->conditional_mod
);
1906 brw_set_predicate_control(p
, inst
->predicated
);
1908 switch (inst
->opcode
) {
1909 case BRW_OPCODE_MOV
:
1910 brw_MOV(p
, dst
, src
[0]);
1912 case BRW_OPCODE_ADD
:
1913 brw_ADD(p
, dst
, src
[0], src
[1]);
1915 case BRW_OPCODE_MUL
:
1916 brw_MUL(p
, dst
, src
[0], src
[1]);
1919 case BRW_OPCODE_FRC
:
1920 brw_FRC(p
, dst
, src
[0]);
1922 case BRW_OPCODE_RNDD
:
1923 brw_RNDD(p
, dst
, src
[0]);
1925 case BRW_OPCODE_RNDZ
:
1926 brw_RNDZ(p
, dst
, src
[0]);
1929 case BRW_OPCODE_AND
:
1930 brw_AND(p
, dst
, src
[0], src
[1]);
1933 brw_OR(p
, dst
, src
[0], src
[1]);
1935 case BRW_OPCODE_XOR
:
1936 brw_XOR(p
, dst
, src
[0], src
[1]);
1939 case BRW_OPCODE_CMP
:
1940 brw_CMP(p
, dst
, inst
->conditional_mod
, src
[0], src
[1]);
1942 case BRW_OPCODE_SEL
:
1943 brw_SEL(p
, dst
, src
[0], src
[1]);
1947 assert(if_stack_depth
< 16);
1948 if_stack
[if_stack_depth
] = brw_IF(p
, BRW_EXECUTE_8
);
1949 if_depth_in_loop
[loop_stack_depth
]++;
1952 case BRW_OPCODE_ELSE
:
1953 if_stack
[if_stack_depth
- 1] =
1954 brw_ELSE(p
, if_stack
[if_stack_depth
- 1]);
1956 case BRW_OPCODE_ENDIF
:
1958 brw_ENDIF(p
, if_stack
[if_stack_depth
]);
1959 if_depth_in_loop
[loop_stack_depth
]--;
1963 loop_stack
[loop_stack_depth
++] = brw_DO(p
, BRW_EXECUTE_8
);
1964 if_depth_in_loop
[loop_stack_depth
] = 0;
1967 case BRW_OPCODE_BREAK
:
1968 brw_BREAK(p
, if_depth_in_loop
[loop_stack_depth
]);
1969 brw_set_predicate_control(p
, BRW_PREDICATE_NONE
);
1971 case BRW_OPCODE_CONTINUE
:
1972 brw_CONT(p
, if_depth_in_loop
[loop_stack_depth
]);
1973 brw_set_predicate_control(p
, BRW_PREDICATE_NONE
);
1976 case BRW_OPCODE_WHILE
: {
1977 struct brw_instruction
*inst0
, *inst1
;
1980 if (intel
->gen
== 5)
1983 assert(loop_stack_depth
> 0);
1985 inst0
= inst1
= brw_WHILE(p
, loop_stack
[loop_stack_depth
]);
1986 /* patch all the BREAK/CONT instructions from last BGNLOOP */
1987 while (inst0
> loop_stack
[loop_stack_depth
]) {
1989 if (inst0
->header
.opcode
== BRW_OPCODE_BREAK
&&
1990 inst0
->bits3
.if_else
.jump_count
== 0) {
1991 inst0
->bits3
.if_else
.jump_count
= br
* (inst1
- inst0
+ 1);
1993 else if (inst0
->header
.opcode
== BRW_OPCODE_CONTINUE
&&
1994 inst0
->bits3
.if_else
.jump_count
== 0) {
1995 inst0
->bits3
.if_else
.jump_count
= br
* (inst1
- inst0
);
2003 case FS_OPCODE_SQRT
:
2004 case FS_OPCODE_EXP2
:
2005 case FS_OPCODE_LOG2
:
2009 generate_math(inst
, dst
, src
);
2011 case FS_OPCODE_LINTERP
:
2012 generate_linterp(inst
, dst
, src
);
2017 generate_tex(inst
, dst
, src
[0]);
2019 case FS_OPCODE_DISCARD
:
2020 generate_discard(inst
);
2023 generate_ddx(inst
, dst
, src
[0]);
2026 generate_ddy(inst
, dst
, src
[0]);
2028 case FS_OPCODE_FB_WRITE
:
2029 generate_fb_write(inst
);
2032 if (inst
->opcode
< (int)ARRAY_SIZE(brw_opcodes
)) {
2033 _mesa_problem(ctx
, "Unsupported opcode `%s' in FS",
2034 brw_opcodes
[inst
->opcode
].name
);
2036 _mesa_problem(ctx
, "Unsupported opcode %d in FS", inst
->opcode
);
2041 if (annotation_len
< p
->nr_insn
) {
2042 annotation_len
*= 2;
2043 if (annotation_len
< 16)
2044 annotation_len
= 16;
2046 this->annotation_string
= talloc_realloc(this->mem_ctx
,
2050 this->annotation_ir
= talloc_realloc(this->mem_ctx
,
2056 for (unsigned int i
= last_native_inst
; i
< p
->nr_insn
; i
++) {
2057 this->annotation_string
[i
] = inst
->annotation
;
2058 this->annotation_ir
[i
] = inst
->ir
;
2060 last_native_inst
= p
->nr_insn
;
2065 brw_wm_fs_emit(struct brw_context
*brw
, struct brw_wm_compile
*c
)
2067 struct brw_compile
*p
= &c
->func
;
2068 struct intel_context
*intel
= &brw
->intel
;
2069 GLcontext
*ctx
= &intel
->ctx
;
2070 struct brw_shader
*shader
= NULL
;
2071 struct gl_shader_program
*prog
= ctx
->Shader
.CurrentProgram
;
2079 for (unsigned int i
= 0; i
< prog
->_NumLinkedShaders
; i
++) {
2080 if (prog
->_LinkedShaders
[i
]->Type
== GL_FRAGMENT_SHADER
) {
2081 shader
= (struct brw_shader
*)prog
->_LinkedShaders
[i
];
2088 /* We always use 8-wide mode, at least for now. For one, flow
2089 * control only works in 8-wide. Also, when we're fragment shader
2090 * bound, we're almost always under register pressure as well, so
2091 * 8-wide would save us from the performance cliff of spilling
2094 c
->dispatch_width
= 8;
2096 if (INTEL_DEBUG
& DEBUG_WM
) {
2097 printf("GLSL IR for native fragment shader %d:\n", prog
->Name
);
2098 _mesa_print_ir(shader
->ir
, NULL
);
2102 /* Now the main event: Visit the shader IR and generate our FS IR for it.
2104 fs_visitor
v(c
, shader
);
2109 v
.emit_interpolation();
2111 /* Generate FS IR for main(). (the visitor only descends into
2112 * functions called "main").
2114 foreach_iter(exec_list_iterator
, iter
, *shader
->ir
) {
2115 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
2121 v
.assign_curb_setup();
2122 v
.assign_urb_setup();
2128 assert(!v
.fail
); /* FINISHME: Cleanly fail, tested at link time, etc. */
2133 if (INTEL_DEBUG
& DEBUG_WM
) {
2134 const char *last_annotation_string
= NULL
;
2135 ir_instruction
*last_annotation_ir
= NULL
;
2137 printf("Native code for fragment shader %d:\n", prog
->Name
);
2138 for (unsigned int i
= 0; i
< p
->nr_insn
; i
++) {
2139 if (last_annotation_ir
!= v
.annotation_ir
[i
]) {
2140 last_annotation_ir
= v
.annotation_ir
[i
];
2141 if (last_annotation_ir
) {
2143 last_annotation_ir
->print();
2147 if (last_annotation_string
!= v
.annotation_string
[i
]) {
2148 last_annotation_string
= v
.annotation_string
[i
];
2149 if (last_annotation_string
)
2150 printf(" %s\n", last_annotation_string
);
2152 brw_disasm(stdout
, &p
->store
[i
], intel
->gen
);
2157 c
->prog_data
.total_grf
= v
.grf_used
;
2158 c
->prog_data
.total_scratch
= 0;