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>
31 /* Evil hack for using libdrm in a c++ compiler. */
34 #include "intel_bufmgr.h"
37 #include "main/macros.h"
38 #include "main/shaderobj.h"
39 #include "program/prog_parameter.h"
40 #include "program/prog_print.h"
41 #include "program/prog_optimize.h"
42 #include "program/hash_table.h"
43 #include "brw_context.h"
48 #include "../glsl/glsl_types.h"
49 #include "../glsl/ir_optimization.h"
50 #include "../glsl/ir_print_visitor.h"
53 ARF
= BRW_ARCHITECTURE_REGISTER_FILE
,
54 GRF
= BRW_GENERAL_REGISTER_FILE
,
55 MRF
= BRW_MESSAGE_REGISTER_FILE
,
56 IMM
= BRW_IMMEDIATE_VALUE
,
62 FS_OPCODE_FB_WRITE
= 256,
76 static int using_new_fs
= -1;
79 brw_new_shader(GLcontext
*ctx
, GLuint name
, GLuint type
)
81 struct brw_shader
*shader
;
83 shader
= talloc_zero(NULL
, struct brw_shader
);
84 shader
->base
.Type
= type
;
85 shader
->base
.Name
= name
;
87 _mesa_init_shader(ctx
, &shader
->base
);
93 struct gl_shader_program
*
94 brw_new_shader_program(GLcontext
*ctx
, GLuint name
)
96 struct brw_shader_program
*prog
;
97 prog
= talloc_zero(NULL
, struct brw_shader_program
);
99 _mesa_init_shader_program(ctx
, &prog
->base
);
105 brw_compile_shader(GLcontext
*ctx
, struct gl_shader
*shader
)
107 if (!_mesa_ir_compile_shader(ctx
, shader
))
114 brw_link_shader(GLcontext
*ctx
, struct gl_shader_program
*prog
)
116 if (using_new_fs
== -1)
117 using_new_fs
= getenv("INTEL_NEW_FS") != NULL
;
119 for (unsigned i
= 0; i
< prog
->_NumLinkedShaders
; i
++) {
120 struct brw_shader
*shader
= (struct brw_shader
*)prog
->_LinkedShaders
[i
];
122 if (using_new_fs
&& shader
->base
.Type
== GL_FRAGMENT_SHADER
) {
123 void *mem_ctx
= talloc_new(NULL
);
126 shader
->ir
= new(shader
) exec_list
;
127 clone_ir_list(mem_ctx
, shader
->ir
, shader
->base
.ir
);
129 do_mat_op_to_vec(shader
->ir
);
130 do_div_to_mul_rcp(shader
->ir
);
131 do_sub_to_add_neg(shader
->ir
);
132 do_explog_to_explog2(shader
->ir
);
134 brw_do_channel_expressions(shader
->ir
);
135 brw_do_vector_splitting(shader
->ir
);
140 progress
= do_common_optimization(shader
->ir
, true) || progress
;
143 reparent_ir(shader
->ir
, shader
);
144 talloc_free(mem_ctx
);
148 if (!_mesa_ir_link_shader(ctx
, prog
))
155 type_size(const struct glsl_type
*type
)
157 unsigned int size
, i
;
159 switch (type
->base_type
) {
162 case GLSL_TYPE_FLOAT
:
164 if (type
->is_matrix()) {
165 /* In case of incoming uniform/varying matrices, match their
166 * allocation behavior. FINISHME: We could just use
167 * glsl_type->components() for variables and temps within the
170 return type
->matrix_columns
* 4;
172 return type
->vector_elements
;
174 case GLSL_TYPE_ARRAY
:
175 /* FINISHME: uniform/varying arrays. */
176 return type_size(type
->fields
.array
) * type
->length
;
177 case GLSL_TYPE_STRUCT
:
179 for (i
= 0; i
< type
->length
; i
++) {
180 size
+= type_size(type
->fields
.structure
[i
].type
);
183 case GLSL_TYPE_SAMPLER
:
184 /* Samplers take up no register space, since they're baked in at
189 assert(!"not reached");
196 /* Callers of this talloc-based new need not call delete. It's
197 * easier to just talloc_free 'ctx' (or any of its ancestors). */
198 static void* operator new(size_t size
, void *ctx
)
202 node
= talloc_size(ctx
, size
);
203 assert(node
!= NULL
);
208 /** Generic unset register constructor. */
211 this->file
= BAD_FILE
;
213 this->reg_offset
= 0;
219 /** Immediate value constructor. */
225 this->type
= BRW_REGISTER_TYPE_F
;
231 /** Immediate value constructor. */
237 this->type
= BRW_REGISTER_TYPE_D
;
243 /** Immediate value constructor. */
249 this->type
= BRW_REGISTER_TYPE_UD
;
255 /** Fixed brw_reg Immediate value constructor. */
256 fs_reg(struct brw_reg fixed_hw_reg
)
258 this->file
= FIXED_HW_REG
;
259 this->fixed_hw_reg
= fixed_hw_reg
;
262 this->type
= fixed_hw_reg
.type
;
267 fs_reg(enum register_file file
, int hw_reg
);
268 fs_reg(class fs_visitor
*v
, const struct glsl_type
*type
);
270 /** Register file: ARF, GRF, MRF, IMM. */
271 enum register_file file
;
272 /** Abstract register number. 0 = fixed hw reg */
274 /** Offset within the abstract register. */
276 /** HW register number. Generally unset until register allocation. */
278 /** Register type. BRW_REGISTER_TYPE_* */
282 struct brw_reg fixed_hw_reg
;
284 /** Value for file == BRW_IMMMEDIATE_FILE */
292 static const fs_reg reg_undef
;
293 static const fs_reg
reg_null(ARF
, BRW_ARF_NULL
);
295 class fs_inst
: public exec_node
{
297 /* Callers of this talloc-based new need not call delete. It's
298 * easier to just talloc_free 'ctx' (or any of its ancestors). */
299 static void* operator new(size_t size
, void *ctx
)
303 node
= talloc_zero_size(ctx
, size
);
304 assert(node
!= NULL
);
311 this->opcode
= BRW_OPCODE_NOP
;
312 this->saturate
= false;
313 this->conditional_mod
= BRW_CONDITIONAL_NONE
;
314 this->predicated
= false;
317 fs_inst(int opcode
, fs_reg dst
, fs_reg src0
)
319 this->opcode
= opcode
;
322 this->saturate
= false;
323 this->conditional_mod
= BRW_CONDITIONAL_NONE
;
324 this->predicated
= false;
327 fs_inst(int opcode
, fs_reg dst
, fs_reg src0
, fs_reg src1
)
329 this->opcode
= opcode
;
333 this->saturate
= false;
334 this->conditional_mod
= BRW_CONDITIONAL_NONE
;
335 this->predicated
= false;
338 fs_inst(int opcode
, fs_reg dst
, fs_reg src0
, fs_reg src1
, fs_reg src2
)
340 this->opcode
= opcode
;
345 this->saturate
= false;
346 this->conditional_mod
= BRW_CONDITIONAL_NONE
;
347 this->predicated
= false;
350 int opcode
; /* BRW_OPCODE_* or FS_OPCODE_* */
355 int conditional_mod
; /**< BRW_CONDITIONAL_* */
358 * Annotation for the generated IR. One of the two can be set.
361 const char *annotation
;
365 class fs_visitor
: public ir_visitor
369 fs_visitor(struct brw_wm_compile
*c
, struct brw_shader
*shader
)
374 this->intel
= &brw
->intel
;
375 this->mem_ctx
= talloc_new(NULL
);
376 this->shader
= shader
;
378 this->next_abstract_grf
= 1;
379 this->variable_ht
= hash_table_ctor(0,
380 hash_table_pointer_hash
,
381 hash_table_pointer_compare
);
383 this->frag_color
= NULL
;
384 this->frag_data
= NULL
;
385 this->frag_depth
= NULL
;
386 this->first_non_payload_grf
= 0;
388 this->current_annotation
= NULL
;
389 this->annotation_string
= NULL
;
390 this->annotation_ir
= NULL
;
394 talloc_free(this->mem_ctx
);
395 hash_table_dtor(this->variable_ht
);
398 fs_reg
*variable_storage(ir_variable
*var
);
400 void visit(ir_variable
*ir
);
401 void visit(ir_assignment
*ir
);
402 void visit(ir_dereference_variable
*ir
);
403 void visit(ir_dereference_record
*ir
);
404 void visit(ir_dereference_array
*ir
);
405 void visit(ir_expression
*ir
);
406 void visit(ir_texture
*ir
);
407 void visit(ir_if
*ir
);
408 void visit(ir_constant
*ir
);
409 void visit(ir_swizzle
*ir
);
410 void visit(ir_return
*ir
);
411 void visit(ir_loop
*ir
);
412 void visit(ir_loop_jump
*ir
);
413 void visit(ir_discard
*ir
);
414 void visit(ir_call
*ir
);
415 void visit(ir_function
*ir
);
416 void visit(ir_function_signature
*ir
);
418 fs_inst
*emit(fs_inst inst
);
419 void assign_urb_setup();
421 void generate_code();
422 void generate_fb_write(fs_inst
*inst
);
423 void generate_linterp(fs_inst
*inst
, struct brw_reg dst
,
424 struct brw_reg
*src
);
425 void generate_math(fs_inst
*inst
, struct brw_reg dst
, struct brw_reg
*src
);
427 void emit_dummy_fs();
428 void emit_interpolation();
429 void emit_pinterp(int location
);
430 void emit_fb_writes();
432 struct brw_reg
interp_reg(int location
, int channel
);
434 struct brw_context
*brw
;
435 struct intel_context
*intel
;
436 struct brw_wm_compile
*c
;
437 struct brw_compile
*p
;
438 struct brw_shader
*shader
;
440 exec_list instructions
;
441 int next_abstract_grf
;
442 struct hash_table
*variable_ht
;
443 ir_variable
*frag_color
, *frag_data
, *frag_depth
;
444 int first_non_payload_grf
;
446 /** @{ debug annotation info */
447 const char *current_annotation
;
448 ir_instruction
*base_ir
;
449 const char **annotation_string
;
450 ir_instruction
**annotation_ir
;
455 /* Result of last visit() method. */
463 fs_reg interp_attrs
[64];
469 /** Fixed HW reg constructor. */
470 fs_reg::fs_reg(enum register_file file
, int hw_reg
)
474 this->reg_offset
= 0;
475 this->hw_reg
= hw_reg
;
476 this->type
= BRW_REGISTER_TYPE_F
;
481 /** Automatic reg constructor. */
482 fs_reg::fs_reg(class fs_visitor
*v
, const struct glsl_type
*type
)
485 this->reg
= v
->next_abstract_grf
;
486 this->reg_offset
= 0;
487 v
->next_abstract_grf
+= type_size(type
);
492 switch (type
->base_type
) {
493 case GLSL_TYPE_FLOAT
:
494 this->type
= BRW_REGISTER_TYPE_F
;
498 this->type
= BRW_REGISTER_TYPE_D
;
501 this->type
= BRW_REGISTER_TYPE_UD
;
504 assert(!"not reached");
505 this->type
= BRW_REGISTER_TYPE_F
;
511 fs_visitor::variable_storage(ir_variable
*var
)
513 return (fs_reg
*)hash_table_find(this->variable_ht
, var
);
517 fs_visitor::visit(ir_variable
*ir
)
522 assert(ir
->mode
!= ir_var_uniform
);
524 if (strcmp(ir
->name
, "gl_FragColor") == 0) {
525 this->frag_color
= ir
;
526 } else if (strcmp(ir
->name
, "gl_FragData") == 0) {
527 this->frag_data
= ir
;
528 } else if (strcmp(ir
->name
, "gl_FragDepth") == 0) {
529 this->frag_depth
= ir
;
532 if (ir
->mode
== ir_var_in
) {
533 reg
= &this->interp_attrs
[ir
->location
];
537 reg
= new(this->mem_ctx
) fs_reg(this, ir
->type
);
539 hash_table_insert(this->variable_ht
, reg
, ir
);
543 fs_visitor::visit(ir_dereference_variable
*ir
)
545 fs_reg
*reg
= variable_storage(ir
->var
);
550 fs_visitor::visit(ir_dereference_record
*ir
)
556 fs_visitor::visit(ir_dereference_array
*ir
)
562 fs_visitor::visit(ir_expression
*ir
)
564 unsigned int operand
;
569 for (operand
= 0; operand
< ir
->get_num_operands(); operand
++) {
570 ir
->operands
[operand
]->accept(this);
571 if (this->result
.file
== BAD_FILE
) {
573 printf("Failed to get tree for expression operand:\n");
574 ir
->operands
[operand
]->accept(&v
);
577 op
[operand
] = this->result
;
579 /* Matrix expression operands should have been broken down to vector
580 * operations already.
582 assert(!ir
->operands
[operand
]->type
->is_matrix());
583 /* And then those vector operands should have been broken down to scalar.
585 assert(!ir
->operands
[operand
]->type
->is_vector());
588 /* Storage for our result. If our result goes into an assignment, it will
589 * just get copy-propagated out, so no worries.
591 this->result
= fs_reg(this, ir
->type
);
593 switch (ir
->operation
) {
594 case ir_unop_logic_not
:
595 emit(fs_inst(BRW_OPCODE_ADD
, this->result
, op
[0], fs_reg(-1)));
598 this->result
= op
[0];
599 op
[0].negate
= ~op
[0].negate
;
602 this->result
= op
[0];
606 temp
= fs_reg(this, ir
->type
);
608 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], fs_reg(0.0f
)));
609 inst
->conditional_mod
= BRW_CONDITIONAL_G
;
611 inst
= emit(fs_inst(BRW_OPCODE_CMP
, temp
, op
[0], fs_reg(0.0f
)));
612 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
615 emit(fs_inst(BRW_OPCODE_ADD
, this->result
, this->result
, temp
));
619 emit(fs_inst(FS_OPCODE_RCP
, this->result
, op
[0]));
623 emit(fs_inst(FS_OPCODE_EXP2
, this->result
, op
[0]));
626 emit(fs_inst(FS_OPCODE_LOG2
, this->result
, op
[0]));
630 assert(!"not reached: should be handled by ir_explog_to_explog2");
633 emit(fs_inst(FS_OPCODE_SIN
, this->result
, op
[0]));
636 emit(fs_inst(FS_OPCODE_COS
, this->result
, op
[0]));
640 emit(fs_inst(FS_OPCODE_DDX
, this->result
, op
[0]));
643 emit(fs_inst(FS_OPCODE_DDY
, this->result
, op
[0]));
647 emit(fs_inst(BRW_OPCODE_ADD
, this->result
, op
[0], op
[1]));
650 assert(!"not reached: should be handled by ir_sub_to_add_neg");
654 emit(fs_inst(BRW_OPCODE_MUL
, this->result
, op
[0], op
[1]));
657 assert(!"not reached: should be handled by ir_div_to_mul_rcp");
660 assert(!"ir_binop_mod should have been converted to b * fract(a/b)");
664 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], op
[1]));
665 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
667 case ir_binop_greater
:
668 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], op
[1]));
669 inst
->conditional_mod
= BRW_CONDITIONAL_G
;
671 case ir_binop_lequal
:
672 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], op
[1]));
673 inst
->conditional_mod
= BRW_CONDITIONAL_LE
;
675 case ir_binop_gequal
:
676 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], op
[1]));
677 inst
->conditional_mod
= BRW_CONDITIONAL_GE
;
680 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], op
[1]));
681 inst
->conditional_mod
= BRW_CONDITIONAL_Z
;
683 case ir_binop_nequal
:
684 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], op
[1]));
685 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
688 case ir_binop_logic_xor
:
689 emit(fs_inst(BRW_OPCODE_XOR
, this->result
, op
[0], op
[1]));
692 case ir_binop_logic_or
:
693 emit(fs_inst(BRW_OPCODE_OR
, this->result
, op
[0], op
[1]));
696 case ir_binop_logic_and
:
697 emit(fs_inst(BRW_OPCODE_AND
, this->result
, op
[0], op
[1]));
703 assert(!"not reached: should be handled by brw_channel_expressions");
707 emit(fs_inst(FS_OPCODE_SQRT
, this->result
, op
[0]));
711 emit(fs_inst(FS_OPCODE_RSQ
, this->result
, op
[0]));
717 emit(fs_inst(BRW_OPCODE_MOV
, this->result
, op
[0]));
720 emit(fs_inst(BRW_OPCODE_RNDZ
, this->result
, op
[0]));
724 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], fs_reg(0.0f
)));
725 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
728 emit(fs_inst(BRW_OPCODE_RNDD
, this->result
, op
[0]));
731 op
[0].negate
= ~op
[0].negate
;
732 inst
= emit(fs_inst(BRW_OPCODE_RNDD
, this->result
, op
[0]));
733 this->result
.negate
= true;
736 inst
= emit(fs_inst(BRW_OPCODE_RNDD
, this->result
, op
[0]));
739 inst
= emit(fs_inst(BRW_OPCODE_FRC
, this->result
, op
[0]));
743 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], op
[1]));
744 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
746 inst
= emit(fs_inst(BRW_OPCODE_SEL
, this->result
, op
[0], op
[1]));
747 inst
->predicated
= true;
750 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], op
[1]));
751 inst
->conditional_mod
= BRW_CONDITIONAL_G
;
753 inst
= emit(fs_inst(BRW_OPCODE_SEL
, this->result
, op
[0], op
[1]));
754 inst
->predicated
= true;
758 inst
= emit(fs_inst(FS_OPCODE_POW
, this->result
, op
[0], op
[1]));
761 case ir_unop_bit_not
:
763 case ir_binop_lshift
:
764 case ir_binop_rshift
:
765 case ir_binop_bit_and
:
766 case ir_binop_bit_xor
:
767 case ir_binop_bit_or
:
768 assert(!"GLSL 1.30 features unsupported");
774 fs_visitor::visit(ir_assignment
*ir
)
781 /* FINISHME: arrays on the lhs */
782 ir
->lhs
->accept(this);
785 ir
->rhs
->accept(this);
788 /* FINISHME: This should really set to the correct maximal writemask for each
789 * FINISHME: component written (in the loops below). This case can only
790 * FINISHME: occur for matrices, arrays, and structures.
792 if (ir
->write_mask
== 0) {
793 assert(!ir
->lhs
->type
->is_scalar() && !ir
->lhs
->type
->is_vector());
794 write_mask
= WRITEMASK_XYZW
;
796 assert(ir
->lhs
->type
->is_vector() || ir
->lhs
->type
->is_scalar());
797 write_mask
= ir
->write_mask
;
800 assert(l
.file
!= BAD_FILE
);
801 assert(r
.file
!= BAD_FILE
);
804 /* Get the condition bool into the predicate. */
805 ir
->condition
->accept(this);
806 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, fs_reg(0)));
807 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
810 for (i
= 0; i
< type_size(ir
->lhs
->type
); i
++) {
811 if (i
< 4 && !(write_mask
& (1 << i
)))
814 inst
= emit(fs_inst(BRW_OPCODE_MOV
, l
, r
));
816 inst
->predicated
= true;
823 fs_visitor::visit(ir_texture
*ir
)
829 fs_visitor::visit(ir_swizzle
*ir
)
831 ir
->val
->accept(this);
832 fs_reg val
= this->result
;
834 fs_reg result
= fs_reg(this, ir
->type
);
835 this->result
= result
;
837 for (unsigned int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
838 fs_reg channel
= val
;
856 channel
.reg_offset
+= swiz
;
857 emit(fs_inst(BRW_OPCODE_MOV
, result
, channel
));
863 fs_visitor::visit(ir_discard
*ir
)
869 fs_visitor::visit(ir_constant
*ir
)
871 fs_reg
reg(this, ir
->type
);
874 for (unsigned int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
875 switch (ir
->type
->base_type
) {
876 case GLSL_TYPE_FLOAT
:
877 emit(fs_inst(BRW_OPCODE_MOV
, reg
, fs_reg(ir
->value
.f
[i
])));
880 emit(fs_inst(BRW_OPCODE_MOV
, reg
, fs_reg(ir
->value
.u
[i
])));
883 emit(fs_inst(BRW_OPCODE_MOV
, reg
, fs_reg(ir
->value
.i
[i
])));
886 emit(fs_inst(BRW_OPCODE_MOV
, reg
, fs_reg((int)ir
->value
.b
[i
])));
889 assert(!"Non-float/uint/int/bool constant");
896 fs_visitor::visit(ir_if
*ir
)
902 fs_visitor::visit(ir_loop
*ir
)
908 fs_visitor::visit(ir_loop_jump
*ir
)
914 fs_visitor::visit(ir_call
*ir
)
920 fs_visitor::visit(ir_return
*ir
)
926 fs_visitor::visit(ir_function
*ir
)
928 /* Ignore function bodies other than main() -- we shouldn't see calls to
929 * them since they should all be inlined before we get to ir_to_mesa.
931 if (strcmp(ir
->name
, "main") == 0) {
932 const ir_function_signature
*sig
;
935 sig
= ir
->matching_signature(&empty
);
939 foreach_iter(exec_list_iterator
, iter
, sig
->body
) {
940 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
949 fs_visitor::visit(ir_function_signature
*ir
)
951 assert(!"not reached");
956 fs_visitor::emit(fs_inst inst
)
958 fs_inst
*list_inst
= new(mem_ctx
) fs_inst
;
961 list_inst
->annotation
= this->current_annotation
;
962 list_inst
->ir
= this->base_ir
;
964 this->instructions
.push_tail(list_inst
);
969 /** Emits a dummy fragment shader consisting of magenta for bringup purposes. */
971 fs_visitor::emit_dummy_fs()
973 /* Everyone's favorite color. */
974 emit(fs_inst(BRW_OPCODE_MOV
,
977 emit(fs_inst(BRW_OPCODE_MOV
,
980 emit(fs_inst(BRW_OPCODE_MOV
,
983 emit(fs_inst(BRW_OPCODE_MOV
,
988 write
= emit(fs_inst(FS_OPCODE_FB_WRITE
,
993 /* The register location here is relative to the start of the URB
994 * data. It will get adjusted to be a real location before
995 * generate_code() time.
998 fs_visitor::interp_reg(int location
, int channel
)
1000 int regnr
= location
* 2 + channel
/ 2;
1001 int stride
= (channel
& 1) * 4;
1003 return brw_vec1_grf(regnr
, stride
);
1006 /** Emits the interpolation for the varying inputs. */
1008 fs_visitor::emit_interpolation()
1010 struct brw_reg g1_uw
= retype(brw_vec1_grf(1, 0), BRW_REGISTER_TYPE_UW
);
1011 /* For now, the source regs for the setup URB data will be unset,
1012 * since we don't know until codegen how many push constants we'll
1013 * use, and therefore what the setup URB offset is.
1015 fs_reg src_reg
= reg_undef
;
1017 this->current_annotation
= "compute pixel centers";
1018 this->pixel_x
= fs_reg(this, glsl_type::uint_type
);
1019 this->pixel_y
= fs_reg(this, glsl_type::uint_type
);
1020 emit(fs_inst(BRW_OPCODE_ADD
,
1022 fs_reg(stride(suboffset(g1_uw
, 4), 2, 4, 0)),
1023 fs_reg(brw_imm_v(0x10101010))));
1024 emit(fs_inst(BRW_OPCODE_ADD
,
1026 fs_reg(stride(suboffset(g1_uw
, 5), 2, 4, 0)),
1027 fs_reg(brw_imm_v(0x11001100))));
1029 this->current_annotation
= "compute pixel deltas from v0";
1030 this->delta_x
= fs_reg(this, glsl_type::float_type
);
1031 this->delta_y
= fs_reg(this, glsl_type::float_type
);
1032 emit(fs_inst(BRW_OPCODE_ADD
,
1035 fs_reg(negate(brw_vec1_grf(1, 0)))));
1036 emit(fs_inst(BRW_OPCODE_ADD
,
1039 fs_reg(brw_vec1_grf(1, 1))));
1041 this->current_annotation
= "compute pos.w and 1/pos.w";
1042 /* Compute wpos. Unlike many other varying inputs, we usually need it
1043 * to produce 1/w, and the varying variable wouldn't show up.
1045 fs_reg wpos
= fs_reg(this, glsl_type::vec4_type
);
1046 this->interp_attrs
[FRAG_ATTRIB_WPOS
] = wpos
;
1047 emit(fs_inst(BRW_OPCODE_MOV
, wpos
, this->pixel_x
)); /* FINISHME: ARB_fcc */
1049 emit(fs_inst(BRW_OPCODE_MOV
, wpos
, this->pixel_y
)); /* FINISHME: ARB_fcc */
1051 emit(fs_inst(FS_OPCODE_LINTERP
, wpos
, this->delta_x
, this->delta_y
,
1052 interp_reg(FRAG_ATTRIB_WPOS
, 2)));
1054 emit(fs_inst(FS_OPCODE_LINTERP
, wpos
, this->delta_x
, this->delta_y
,
1055 interp_reg(FRAG_ATTRIB_WPOS
, 3)));
1056 /* Compute the pixel W value from wpos.w. */
1057 this->pixel_w
= fs_reg(this, glsl_type::float_type
);
1058 emit(fs_inst(FS_OPCODE_RCP
, this->pixel_w
, wpos
));
1060 /* FINISHME: gl_FrontFacing */
1062 foreach_iter(exec_list_iterator
, iter
, *this->shader
->ir
) {
1063 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
1064 ir_variable
*var
= ir
->as_variable();
1069 if (var
->mode
!= ir_var_in
)
1072 /* If it's already set up (WPOS), skip. */
1073 if (var
->location
== 0)
1076 this->current_annotation
= talloc_asprintf(this->mem_ctx
,
1078 "(FRAG_ATTRIB[%d])",
1081 emit_pinterp(var
->location
);
1083 this->current_annotation
= NULL
;
1087 fs_visitor::emit_pinterp(int location
)
1089 fs_reg interp_attr
= fs_reg(this, glsl_type::vec4_type
);
1090 this->interp_attrs
[location
] = interp_attr
;
1092 for (unsigned int i
= 0; i
< 4; i
++) {
1093 struct brw_reg interp
= interp_reg(location
, i
);
1094 emit(fs_inst(FS_OPCODE_LINTERP
,
1099 interp_attr
.reg_offset
++;
1101 interp_attr
.reg_offset
-= 4;
1103 for (unsigned int i
= 0; i
< 4; i
++) {
1104 emit(fs_inst(BRW_OPCODE_MUL
,
1108 interp_attr
.reg_offset
++;
1113 fs_visitor::emit_fb_writes()
1115 this->current_annotation
= "FB write";
1117 assert(this->frag_color
|| !"FINISHME: MRT");
1118 fs_reg color
= *(variable_storage(this->frag_color
));
1120 for (int i
= 0; i
< 4; i
++) {
1121 emit(fs_inst(BRW_OPCODE_MOV
,
1127 emit(fs_inst(FS_OPCODE_FB_WRITE
,
1131 this->current_annotation
= NULL
;
1135 fs_visitor::generate_fb_write(fs_inst
*inst
)
1137 GLboolean eot
= 1; /* FINISHME: MRT */
1138 /* FINISHME: AADS */
1140 /* Header is 2 regs, g0 and g1 are the contents. g0 will be implied
1143 brw_push_insn_state(p
);
1144 brw_set_mask_control(p
, BRW_MASK_DISABLE
);
1145 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
1148 brw_vec8_grf(1, 0));
1149 brw_pop_insn_state(p
);
1154 8, /* dispatch_width */
1155 retype(vec8(brw_null_reg()), BRW_REGISTER_TYPE_UW
),
1157 retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UW
),
1158 0, /* FINISHME: MRT target */
1165 fs_visitor::generate_linterp(fs_inst
*inst
,
1166 struct brw_reg dst
, struct brw_reg
*src
)
1168 struct brw_reg delta_x
= src
[0];
1169 struct brw_reg delta_y
= src
[1];
1170 struct brw_reg interp
= src
[2];
1173 delta_y
.nr
== delta_x
.nr
+ 1 &&
1174 (intel
->gen
>= 6 || (delta_x
.nr
& 1) == 0)) {
1175 brw_PLN(p
, dst
, interp
, delta_x
);
1177 brw_LINE(p
, brw_null_reg(), interp
, delta_x
);
1178 brw_MAC(p
, dst
, suboffset(interp
, 1), delta_y
);
1183 fs_visitor::generate_math(fs_inst
*inst
,
1184 struct brw_reg dst
, struct brw_reg
*src
)
1188 switch (inst
->opcode
) {
1190 op
= BRW_MATH_FUNCTION_INV
;
1193 op
= BRW_MATH_FUNCTION_RSQ
;
1195 case FS_OPCODE_SQRT
:
1196 op
= BRW_MATH_FUNCTION_SQRT
;
1198 case FS_OPCODE_EXP2
:
1199 op
= BRW_MATH_FUNCTION_EXP
;
1201 case FS_OPCODE_LOG2
:
1202 op
= BRW_MATH_FUNCTION_LOG
;
1205 op
= BRW_MATH_FUNCTION_POW
;
1208 op
= BRW_MATH_FUNCTION_SIN
;
1211 op
= BRW_MATH_FUNCTION_COS
;
1214 assert(!"not reached: unknown math function");
1219 if (inst
->opcode
== FS_OPCODE_POW
) {
1220 brw_MOV(p
, brw_message_reg(3), src
[1]);
1225 inst
->saturate
? BRW_MATH_SATURATE_SATURATE
:
1226 BRW_MATH_SATURATE_NONE
,
1228 BRW_MATH_DATA_VECTOR
,
1229 BRW_MATH_PRECISION_FULL
);
1233 trivial_assign_reg(int header_size
, fs_reg
*reg
)
1235 if (reg
->file
== GRF
&& reg
->reg
!= 0) {
1236 reg
->hw_reg
= header_size
+ reg
->reg
- 1 + reg
->reg_offset
;
1242 fs_visitor::assign_urb_setup()
1244 int urb_start
= c
->key
.nr_payload_regs
; /* FINISHME: push constants */
1245 int interp_reg_nr
[FRAG_ATTRIB_MAX
];
1247 c
->prog_data
.urb_read_length
= 0;
1249 /* Figure out where each of the incoming setup attributes lands. */
1250 for (unsigned int i
= 0; i
< FRAG_ATTRIB_MAX
; i
++) {
1251 interp_reg_nr
[i
] = -1;
1253 if (i
!= FRAG_ATTRIB_WPOS
&&
1254 !(brw
->fragment_program
->Base
.InputsRead
& BITFIELD64_BIT(i
)))
1257 /* Each attribute is 4 setup channels, each of which is half a reg. */
1258 interp_reg_nr
[i
] = urb_start
+ c
->prog_data
.urb_read_length
;
1259 c
->prog_data
.urb_read_length
+= 2;
1262 /* Map the register numbers for FS_OPCODE_LINTERP so that it uses
1263 * the correct setup input.
1265 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
1266 fs_inst
*inst
= (fs_inst
*)iter
.get();
1268 if (inst
->opcode
!= FS_OPCODE_LINTERP
)
1271 assert(inst
->src
[2].file
== FIXED_HW_REG
);
1273 int location
= inst
->src
[2].fixed_hw_reg
.nr
/ 2;
1274 assert(interp_reg_nr
[location
] != -1);
1275 inst
->src
[2].fixed_hw_reg
.nr
= (interp_reg_nr
[location
] +
1276 (inst
->src
[2].fixed_hw_reg
.nr
& 1));
1279 this->first_non_payload_grf
= urb_start
+ c
->prog_data
.urb_read_length
;
1283 fs_visitor::assign_regs()
1285 int header_size
= this->first_non_payload_grf
;
1288 /* FINISHME: trivial assignment of register numbers */
1289 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
1290 fs_inst
*inst
= (fs_inst
*)iter
.get();
1292 trivial_assign_reg(header_size
, &inst
->dst
);
1293 trivial_assign_reg(header_size
, &inst
->src
[0]);
1294 trivial_assign_reg(header_size
, &inst
->src
[1]);
1296 last_grf
= MAX2(last_grf
, inst
->dst
.hw_reg
);
1297 last_grf
= MAX2(last_grf
, inst
->src
[0].hw_reg
);
1298 last_grf
= MAX2(last_grf
, inst
->src
[1].hw_reg
);
1301 this->grf_used
= last_grf
;
1304 static struct brw_reg
brw_reg_from_fs_reg(fs_reg
*reg
)
1306 struct brw_reg brw_reg
;
1308 switch (reg
->file
) {
1312 brw_reg
= brw_vec8_reg(reg
->file
,
1314 brw_reg
= retype(brw_reg
, reg
->type
);
1317 switch (reg
->type
) {
1318 case BRW_REGISTER_TYPE_F
:
1319 brw_reg
= brw_imm_f(reg
->imm
.f
);
1321 case BRW_REGISTER_TYPE_D
:
1322 brw_reg
= brw_imm_f(reg
->imm
.i
);
1324 case BRW_REGISTER_TYPE_UD
:
1325 brw_reg
= brw_imm_f(reg
->imm
.u
);
1328 assert(!"not reached");
1333 brw_reg
= reg
->fixed_hw_reg
;
1336 /* Probably unused. */
1337 brw_reg
= brw_null_reg();
1340 brw_reg
= brw_abs(brw_reg
);
1342 brw_reg
= negate(brw_reg
);
1348 fs_visitor::generate_code()
1350 unsigned int annotation_len
= 0;
1351 int last_native_inst
= 0;
1353 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
1354 fs_inst
*inst
= (fs_inst
*)iter
.get();
1355 struct brw_reg src
[3], dst
;
1357 for (unsigned int i
= 0; i
< 3; i
++) {
1358 src
[i
] = brw_reg_from_fs_reg(&inst
->src
[i
]);
1360 dst
= brw_reg_from_fs_reg(&inst
->dst
);
1362 brw_set_conditionalmod(p
, inst
->conditional_mod
);
1363 brw_set_predicate_control(p
, inst
->predicated
);
1365 switch (inst
->opcode
) {
1366 case BRW_OPCODE_MOV
:
1367 brw_MOV(p
, dst
, src
[0]);
1369 case BRW_OPCODE_ADD
:
1370 brw_ADD(p
, dst
, src
[0], src
[1]);
1372 case BRW_OPCODE_MUL
:
1373 brw_MUL(p
, dst
, src
[0], src
[1]);
1377 case FS_OPCODE_SQRT
:
1378 case FS_OPCODE_EXP2
:
1379 case FS_OPCODE_LOG2
:
1383 generate_math(inst
, dst
, src
);
1385 case FS_OPCODE_LINTERP
:
1386 generate_linterp(inst
, dst
, src
);
1388 case FS_OPCODE_FB_WRITE
:
1389 generate_fb_write(inst
);
1392 assert(!"not reached");
1395 if (annotation_len
< p
->nr_insn
) {
1396 annotation_len
*= 2;
1397 if (annotation_len
< 16)
1398 annotation_len
= 16;
1400 this->annotation_string
= talloc_realloc(this->mem_ctx
,
1404 this->annotation_ir
= talloc_realloc(this->mem_ctx
,
1410 for (unsigned int i
= last_native_inst
; i
< p
->nr_insn
; i
++) {
1411 this->annotation_string
[i
] = inst
->annotation
;
1412 this->annotation_ir
[i
] = inst
->ir
;
1414 last_native_inst
= p
->nr_insn
;
1419 brw_wm_fs_emit(struct brw_context
*brw
, struct brw_wm_compile
*c
)
1421 struct brw_compile
*p
= &c
->func
;
1422 struct intel_context
*intel
= &brw
->intel
;
1423 GLcontext
*ctx
= &intel
->ctx
;
1424 struct brw_shader
*shader
= NULL
;
1425 struct gl_shader_program
*prog
= ctx
->Shader
.CurrentProgram
;
1433 for (unsigned int i
= 0; i
< prog
->_NumLinkedShaders
; i
++) {
1434 if (prog
->_LinkedShaders
[i
]->Type
== GL_FRAGMENT_SHADER
) {
1435 shader
= (struct brw_shader
*)prog
->_LinkedShaders
[i
];
1442 /* We always use 8-wide mode, at least for now. For one, flow
1443 * control only works in 8-wide. Also, when we're fragment shader
1444 * bound, we're almost always under register pressure as well, so
1445 * 8-wide would save us from the performance cliff of spilling
1448 c
->dispatch_width
= 8;
1450 if (INTEL_DEBUG
& DEBUG_WM
) {
1451 printf("GLSL IR for native fragment shader %d:\n", prog
->Name
);
1452 _mesa_print_ir(shader
->ir
, NULL
);
1456 /* Now the main event: Visit the shader IR and generate our FS IR for it.
1458 fs_visitor
v(c
, shader
);
1463 v
.emit_interpolation();
1465 /* Generate FS IR for main(). (the visitor only descends into
1466 * functions called "main").
1468 foreach_iter(exec_list_iterator
, iter
, *shader
->ir
) {
1469 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
1478 v
.assign_urb_setup();
1484 if (INTEL_DEBUG
& DEBUG_WM
) {
1485 const char *last_annotation_string
= NULL
;
1486 ir_instruction
*last_annotation_ir
= NULL
;
1488 printf("Native code for fragment shader %d:\n", prog
->Name
);
1489 for (unsigned int i
= 0; i
< p
->nr_insn
; i
++) {
1490 if (last_annotation_ir
!= v
.annotation_ir
[i
]) {
1491 last_annotation_ir
= v
.annotation_ir
[i
];
1492 if (last_annotation_ir
) {
1494 last_annotation_ir
->print();
1498 if (last_annotation_string
!= v
.annotation_string
[i
]) {
1499 last_annotation_string
= v
.annotation_string
[i
];
1500 if (last_annotation_string
)
1501 printf(" %s\n", last_annotation_string
);
1503 brw_disasm(stdout
, &p
->store
[i
], intel
->gen
);
1508 c
->prog_data
.nr_params
= 0; /* FINISHME */
1509 c
->prog_data
.first_curbe_grf
= c
->key
.nr_payload_regs
;
1510 c
->prog_data
.curb_read_length
= 0; /* FINISHME */
1511 c
->prog_data
.total_grf
= v
.grf_used
;
1512 c
->prog_data
.total_scratch
= 0;