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>
29 #include "main/macros.h"
30 #include "main/shaderobj.h"
31 #include "program/prog_parameter.h"
32 #include "program/prog_print.h"
33 #include "program/prog_optimize.h"
34 #include "program/hash_table.h"
35 #include "brw_context.h"
40 #include "../glsl/glsl_types.h"
41 #include "../glsl/ir_optimization.h"
42 #include "../glsl/ir_print_visitor.h"
45 ARF
= BRW_ARCHITECTURE_REGISTER_FILE
,
46 GRF
= BRW_GENERAL_REGISTER_FILE
,
47 MRF
= BRW_MESSAGE_REGISTER_FILE
,
48 IMM
= BRW_IMMEDIATE_VALUE
,
54 FS_OPCODE_FB_WRITE
= 256,
68 static int using_new_fs
= -1;
71 brw_new_shader(GLcontext
*ctx
, GLuint name
, GLuint type
)
73 struct brw_shader
*shader
;
75 shader
= talloc_zero(NULL
, struct brw_shader
);
76 shader
->base
.Type
= type
;
77 shader
->base
.Name
= name
;
79 _mesa_init_shader(ctx
, &shader
->base
);
85 struct gl_shader_program
*
86 brw_new_shader_program(GLcontext
*ctx
, GLuint name
)
88 struct brw_shader_program
*prog
;
89 prog
= talloc_zero(NULL
, struct brw_shader_program
);
91 _mesa_init_shader_program(ctx
, &prog
->base
);
97 brw_compile_shader(GLcontext
*ctx
, struct gl_shader
*shader
)
99 if (!_mesa_ir_compile_shader(ctx
, shader
))
106 brw_link_shader(GLcontext
*ctx
, struct gl_shader_program
*prog
)
108 if (using_new_fs
== -1)
109 using_new_fs
= getenv("INTEL_NEW_FS") != NULL
;
111 for (unsigned i
= 0; i
< prog
->_NumLinkedShaders
; i
++) {
112 struct brw_shader
*shader
= (struct brw_shader
*)prog
->_LinkedShaders
[i
];
114 if (using_new_fs
&& shader
->base
.Type
== GL_FRAGMENT_SHADER
) {
115 void *mem_ctx
= talloc_new(NULL
);
118 shader
->ir
= new(shader
) exec_list
;
119 clone_ir_list(mem_ctx
, shader
->ir
, shader
->base
.ir
);
121 do_mat_op_to_vec(shader
->ir
);
122 do_div_to_mul_rcp(shader
->ir
);
123 do_sub_to_add_neg(shader
->ir
);
124 do_explog_to_explog2(shader
->ir
);
126 brw_do_channel_expressions(shader
->ir
);
127 brw_do_vector_splitting(shader
->ir
);
132 progress
= do_common_optimization(shader
->ir
, true) || progress
;
135 reparent_ir(shader
->ir
, shader
);
136 talloc_free(mem_ctx
);
140 if (!_mesa_ir_link_shader(ctx
, prog
))
147 type_size(const struct glsl_type
*type
)
149 unsigned int size
, i
;
151 switch (type
->base_type
) {
154 case GLSL_TYPE_FLOAT
:
156 if (type
->is_matrix()) {
157 /* In case of incoming uniform/varying matrices, match their
158 * allocation behavior. FINISHME: We could just use
159 * glsl_type->components() for variables and temps within the
162 return type
->matrix_columns
* 4;
164 return type
->vector_elements
;
166 case GLSL_TYPE_ARRAY
:
167 /* FINISHME: uniform/varying arrays. */
168 return type_size(type
->fields
.array
) * type
->length
;
169 case GLSL_TYPE_STRUCT
:
171 for (i
= 0; i
< type
->length
; i
++) {
172 size
+= type_size(type
->fields
.structure
[i
].type
);
175 case GLSL_TYPE_SAMPLER
:
176 /* Samplers take up no register space, since they're baked in at
181 assert(!"not reached");
188 /* Callers of this talloc-based new need not call delete. It's
189 * easier to just talloc_free 'ctx' (or any of its ancestors). */
190 static void* operator new(size_t size
, void *ctx
)
194 node
= talloc_size(ctx
, size
);
195 assert(node
!= NULL
);
200 /** Generic unset register constructor. */
203 this->file
= BAD_FILE
;
205 this->reg_offset
= 0;
211 /** Immediate value constructor. */
217 this->type
= BRW_REGISTER_TYPE_F
;
223 /** Immediate value constructor. */
229 this->type
= BRW_REGISTER_TYPE_D
;
235 /** Immediate value constructor. */
241 this->type
= BRW_REGISTER_TYPE_UD
;
247 /** Fixed brw_reg Immediate value constructor. */
248 fs_reg(struct brw_reg fixed_hw_reg
)
250 this->file
= FIXED_HW_REG
;
251 this->fixed_hw_reg
= fixed_hw_reg
;
254 this->type
= fixed_hw_reg
.type
;
259 fs_reg(enum register_file file
, int hw_reg
);
260 fs_reg(class fs_visitor
*v
, const struct glsl_type
*type
);
262 /** Register file: ARF, GRF, MRF, IMM. */
263 enum register_file file
;
264 /** Abstract register number. 0 = fixed hw reg */
266 /** Offset within the abstract register. */
268 /** HW register number. Generally unset until register allocation. */
270 /** Register type. BRW_REGISTER_TYPE_* */
274 struct brw_reg fixed_hw_reg
;
276 /** Value for file == BRW_IMMMEDIATE_FILE */
284 static const fs_reg reg_undef
;
285 static const fs_reg
reg_null(ARF
, BRW_ARF_NULL
);
287 class fs_inst
: public exec_node
{
289 /* Callers of this talloc-based new need not call delete. It's
290 * easier to just talloc_free 'ctx' (or any of its ancestors). */
291 static void* operator new(size_t size
, void *ctx
)
295 node
= talloc_zero_size(ctx
, size
);
296 assert(node
!= NULL
);
303 this->opcode
= BRW_OPCODE_NOP
;
304 this->saturate
= false;
305 this->conditional_mod
= BRW_CONDITIONAL_NONE
;
306 this->predicated
= false;
309 fs_inst(int opcode
, fs_reg dst
, fs_reg src0
)
311 this->opcode
= opcode
;
314 this->saturate
= false;
315 this->conditional_mod
= BRW_CONDITIONAL_NONE
;
316 this->predicated
= false;
319 fs_inst(int opcode
, fs_reg dst
, fs_reg src0
, fs_reg src1
)
321 this->opcode
= opcode
;
325 this->saturate
= false;
326 this->conditional_mod
= BRW_CONDITIONAL_NONE
;
327 this->predicated
= false;
330 fs_inst(int opcode
, fs_reg dst
, fs_reg src0
, fs_reg src1
, fs_reg src2
)
332 this->opcode
= opcode
;
337 this->saturate
= false;
338 this->conditional_mod
= BRW_CONDITIONAL_NONE
;
339 this->predicated
= false;
342 int opcode
; /* BRW_OPCODE_* or FS_OPCODE_* */
347 int conditional_mod
; /**< BRW_CONDITIONAL_* */
350 * Annotation for the generated IR. One of the two can be set.
353 const char *annotation
;
357 class fs_visitor
: public ir_visitor
361 fs_visitor(struct brw_wm_compile
*c
, struct brw_shader
*shader
)
366 this->intel
= &brw
->intel
;
367 this->mem_ctx
= talloc_new(NULL
);
368 this->shader
= shader
;
370 this->next_abstract_grf
= 1;
371 this->variable_ht
= hash_table_ctor(0,
372 hash_table_pointer_hash
,
373 hash_table_pointer_compare
);
375 this->frag_color
= NULL
;
376 this->frag_data
= NULL
;
377 this->frag_depth
= NULL
;
378 this->first_non_payload_grf
= 0;
380 this->current_annotation
= NULL
;
381 this->annotation_string
= NULL
;
382 this->annotation_ir
= NULL
;
386 talloc_free(this->mem_ctx
);
387 hash_table_dtor(this->variable_ht
);
390 fs_reg
*variable_storage(ir_variable
*var
);
392 void visit(ir_variable
*ir
);
393 void visit(ir_assignment
*ir
);
394 void visit(ir_dereference_variable
*ir
);
395 void visit(ir_dereference_record
*ir
);
396 void visit(ir_dereference_array
*ir
);
397 void visit(ir_expression
*ir
);
398 void visit(ir_texture
*ir
);
399 void visit(ir_if
*ir
);
400 void visit(ir_constant
*ir
);
401 void visit(ir_swizzle
*ir
);
402 void visit(ir_return
*ir
);
403 void visit(ir_loop
*ir
);
404 void visit(ir_loop_jump
*ir
);
405 void visit(ir_discard
*ir
);
406 void visit(ir_call
*ir
);
407 void visit(ir_function
*ir
);
408 void visit(ir_function_signature
*ir
);
410 fs_inst
*emit(fs_inst inst
);
411 void assign_urb_setup();
413 void generate_code();
414 void generate_fb_write(fs_inst
*inst
);
415 void generate_linterp(fs_inst
*inst
, struct brw_reg dst
,
416 struct brw_reg
*src
);
417 void generate_math(fs_inst
*inst
, struct brw_reg dst
, struct brw_reg
*src
);
419 void emit_dummy_fs();
420 void emit_interpolation();
421 void emit_pinterp(int location
);
422 void emit_fb_writes();
424 struct brw_reg
interp_reg(int location
, int channel
);
426 struct brw_context
*brw
;
427 struct intel_context
*intel
;
428 struct brw_wm_compile
*c
;
429 struct brw_compile
*p
;
430 struct brw_shader
*shader
;
432 exec_list instructions
;
433 int next_abstract_grf
;
434 struct hash_table
*variable_ht
;
435 ir_variable
*frag_color
, *frag_data
, *frag_depth
;
436 int first_non_payload_grf
;
438 /** @{ debug annotation info */
439 const char *current_annotation
;
440 ir_instruction
*base_ir
;
441 const char **annotation_string
;
442 ir_instruction
**annotation_ir
;
447 /* Result of last visit() method. */
455 fs_reg interp_attrs
[64];
461 /** Fixed HW reg constructor. */
462 fs_reg::fs_reg(enum register_file file
, int hw_reg
)
466 this->reg_offset
= 0;
467 this->hw_reg
= hw_reg
;
468 this->type
= BRW_REGISTER_TYPE_F
;
473 /** Automatic reg constructor. */
474 fs_reg::fs_reg(class fs_visitor
*v
, const struct glsl_type
*type
)
477 this->reg
= v
->next_abstract_grf
;
478 this->reg_offset
= 0;
479 v
->next_abstract_grf
+= type_size(type
);
484 switch (type
->base_type
) {
485 case GLSL_TYPE_FLOAT
:
486 this->type
= BRW_REGISTER_TYPE_F
;
490 this->type
= BRW_REGISTER_TYPE_D
;
493 this->type
= BRW_REGISTER_TYPE_UD
;
496 assert(!"not reached");
497 this->type
= BRW_REGISTER_TYPE_F
;
503 fs_visitor::variable_storage(ir_variable
*var
)
505 return (fs_reg
*)hash_table_find(this->variable_ht
, var
);
509 fs_visitor::visit(ir_variable
*ir
)
514 assert(ir
->mode
!= ir_var_uniform
);
516 if (strcmp(ir
->name
, "gl_FragColor") == 0) {
517 this->frag_color
= ir
;
518 } else if (strcmp(ir
->name
, "gl_FragData") == 0) {
519 this->frag_data
= ir
;
520 } else if (strcmp(ir
->name
, "gl_FragDepth") == 0) {
521 this->frag_depth
= ir
;
524 if (ir
->mode
== ir_var_in
) {
525 reg
= &this->interp_attrs
[ir
->location
];
529 reg
= new(this->mem_ctx
) fs_reg(this, ir
->type
);
531 hash_table_insert(this->variable_ht
, reg
, ir
);
535 fs_visitor::visit(ir_dereference_variable
*ir
)
537 fs_reg
*reg
= variable_storage(ir
->var
);
542 fs_visitor::visit(ir_dereference_record
*ir
)
548 fs_visitor::visit(ir_dereference_array
*ir
)
554 fs_visitor::visit(ir_expression
*ir
)
556 unsigned int operand
;
561 for (operand
= 0; operand
< ir
->get_num_operands(); operand
++) {
562 ir
->operands
[operand
]->accept(this);
563 if (this->result
.file
== BAD_FILE
) {
565 printf("Failed to get tree for expression operand:\n");
566 ir
->operands
[operand
]->accept(&v
);
569 op
[operand
] = this->result
;
571 /* Matrix expression operands should have been broken down to vector
572 * operations already.
574 assert(!ir
->operands
[operand
]->type
->is_matrix());
575 /* And then those vector operands should have been broken down to scalar.
577 assert(!ir
->operands
[operand
]->type
->is_vector());
580 /* Storage for our result. If our result goes into an assignment, it will
581 * just get copy-propagated out, so no worries.
583 this->result
= fs_reg(this, ir
->type
);
585 switch (ir
->operation
) {
586 case ir_unop_logic_not
:
587 emit(fs_inst(BRW_OPCODE_ADD
, this->result
, op
[0], fs_reg(-1)));
590 this->result
= op
[0];
591 op
[0].negate
= ~op
[0].negate
;
594 this->result
= op
[0];
598 temp
= fs_reg(this, ir
->type
);
600 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], fs_reg(0.0f
)));
601 inst
->conditional_mod
= BRW_CONDITIONAL_G
;
603 inst
= emit(fs_inst(BRW_OPCODE_CMP
, temp
, op
[0], fs_reg(0.0f
)));
604 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
607 emit(fs_inst(BRW_OPCODE_ADD
, this->result
, this->result
, temp
));
611 emit(fs_inst(FS_OPCODE_RCP
, this->result
, op
[0]));
615 emit(fs_inst(FS_OPCODE_EXP2
, this->result
, op
[0]));
618 emit(fs_inst(FS_OPCODE_LOG2
, this->result
, op
[0]));
622 assert(!"not reached: should be handled by ir_explog_to_explog2");
625 emit(fs_inst(FS_OPCODE_SIN
, this->result
, op
[0]));
628 emit(fs_inst(FS_OPCODE_COS
, this->result
, op
[0]));
632 emit(fs_inst(FS_OPCODE_DDX
, this->result
, op
[0]));
635 emit(fs_inst(FS_OPCODE_DDY
, this->result
, op
[0]));
639 emit(fs_inst(BRW_OPCODE_ADD
, this->result
, op
[0], op
[1]));
642 assert(!"not reached: should be handled by ir_sub_to_add_neg");
646 emit(fs_inst(BRW_OPCODE_MUL
, this->result
, op
[0], op
[1]));
649 assert(!"not reached: should be handled by ir_div_to_mul_rcp");
652 assert(!"ir_binop_mod should have been converted to b * fract(a/b)");
656 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], op
[1]));
657 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
659 case ir_binop_greater
:
660 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], op
[1]));
661 inst
->conditional_mod
= BRW_CONDITIONAL_G
;
663 case ir_binop_lequal
:
664 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], op
[1]));
665 inst
->conditional_mod
= BRW_CONDITIONAL_LE
;
667 case ir_binop_gequal
:
668 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], op
[1]));
669 inst
->conditional_mod
= BRW_CONDITIONAL_GE
;
672 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], op
[1]));
673 inst
->conditional_mod
= BRW_CONDITIONAL_Z
;
675 case ir_binop_nequal
:
676 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], op
[1]));
677 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
680 case ir_binop_logic_xor
:
681 emit(fs_inst(BRW_OPCODE_XOR
, this->result
, op
[0], op
[1]));
684 case ir_binop_logic_or
:
685 emit(fs_inst(BRW_OPCODE_OR
, this->result
, op
[0], op
[1]));
688 case ir_binop_logic_and
:
689 emit(fs_inst(BRW_OPCODE_AND
, this->result
, op
[0], op
[1]));
695 assert(!"not reached: should be handled by brw_channel_expressions");
699 emit(fs_inst(FS_OPCODE_SQRT
, this->result
, op
[0]));
703 emit(fs_inst(FS_OPCODE_RSQ
, this->result
, op
[0]));
709 emit(fs_inst(BRW_OPCODE_MOV
, this->result
, op
[0]));
712 emit(fs_inst(BRW_OPCODE_RNDZ
, this->result
, op
[0]));
716 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], fs_reg(0.0f
)));
717 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
720 emit(fs_inst(BRW_OPCODE_RNDD
, this->result
, op
[0]));
723 op
[0].negate
= ~op
[0].negate
;
724 inst
= emit(fs_inst(BRW_OPCODE_RNDD
, this->result
, op
[0]));
725 this->result
.negate
= true;
728 inst
= emit(fs_inst(BRW_OPCODE_RNDD
, this->result
, op
[0]));
731 inst
= emit(fs_inst(BRW_OPCODE_FRC
, this->result
, op
[0]));
735 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], op
[1]));
736 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
738 inst
= emit(fs_inst(BRW_OPCODE_SEL
, this->result
, op
[0], op
[1]));
739 inst
->predicated
= true;
742 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], op
[1]));
743 inst
->conditional_mod
= BRW_CONDITIONAL_G
;
745 inst
= emit(fs_inst(BRW_OPCODE_SEL
, this->result
, op
[0], op
[1]));
746 inst
->predicated
= true;
750 inst
= emit(fs_inst(FS_OPCODE_POW
, this->result
, op
[0], op
[1]));
753 case ir_unop_bit_not
:
755 case ir_binop_lshift
:
756 case ir_binop_rshift
:
757 case ir_binop_bit_and
:
758 case ir_binop_bit_xor
:
759 case ir_binop_bit_or
:
760 assert(!"GLSL 1.30 features unsupported");
766 fs_visitor::visit(ir_assignment
*ir
)
773 /* FINISHME: arrays on the lhs */
774 ir
->lhs
->accept(this);
777 ir
->rhs
->accept(this);
780 /* FINISHME: This should really set to the correct maximal writemask for each
781 * FINISHME: component written (in the loops below). This case can only
782 * FINISHME: occur for matrices, arrays, and structures.
784 if (ir
->write_mask
== 0) {
785 assert(!ir
->lhs
->type
->is_scalar() && !ir
->lhs
->type
->is_vector());
786 write_mask
= WRITEMASK_XYZW
;
788 assert(ir
->lhs
->type
->is_vector() || ir
->lhs
->type
->is_scalar());
789 write_mask
= ir
->write_mask
;
792 assert(l
.file
!= BAD_FILE
);
793 assert(r
.file
!= BAD_FILE
);
796 /* Get the condition bool into the predicate. */
797 ir
->condition
->accept(this);
798 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, fs_reg(0)));
799 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
802 for (i
= 0; i
< type_size(ir
->lhs
->type
); i
++) {
803 if (i
< 4 && !(write_mask
& (1 << i
)))
806 inst
= emit(fs_inst(BRW_OPCODE_MOV
, l
, r
));
808 inst
->predicated
= true;
815 fs_visitor::visit(ir_texture
*ir
)
821 fs_visitor::visit(ir_swizzle
*ir
)
823 ir
->val
->accept(this);
824 fs_reg val
= this->result
;
826 fs_reg result
= fs_reg(this, ir
->type
);
827 this->result
= result
;
829 for (unsigned int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
830 fs_reg channel
= val
;
848 channel
.reg_offset
+= swiz
;
849 emit(fs_inst(BRW_OPCODE_MOV
, result
, channel
));
855 fs_visitor::visit(ir_discard
*ir
)
861 fs_visitor::visit(ir_constant
*ir
)
863 fs_reg
reg(this, ir
->type
);
866 for (unsigned int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
867 switch (ir
->type
->base_type
) {
868 case GLSL_TYPE_FLOAT
:
869 emit(fs_inst(BRW_OPCODE_MOV
, reg
, fs_reg(ir
->value
.f
[i
])));
872 emit(fs_inst(BRW_OPCODE_MOV
, reg
, fs_reg(ir
->value
.u
[i
])));
875 emit(fs_inst(BRW_OPCODE_MOV
, reg
, fs_reg(ir
->value
.i
[i
])));
878 emit(fs_inst(BRW_OPCODE_MOV
, reg
, fs_reg((int)ir
->value
.b
[i
])));
881 assert(!"Non-float/uint/int/bool constant");
888 fs_visitor::visit(ir_if
*ir
)
894 fs_visitor::visit(ir_loop
*ir
)
900 fs_visitor::visit(ir_loop_jump
*ir
)
906 fs_visitor::visit(ir_call
*ir
)
912 fs_visitor::visit(ir_return
*ir
)
918 fs_visitor::visit(ir_function
*ir
)
920 /* Ignore function bodies other than main() -- we shouldn't see calls to
921 * them since they should all be inlined before we get to ir_to_mesa.
923 if (strcmp(ir
->name
, "main") == 0) {
924 const ir_function_signature
*sig
;
927 sig
= ir
->matching_signature(&empty
);
931 foreach_iter(exec_list_iterator
, iter
, sig
->body
) {
932 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
941 fs_visitor::visit(ir_function_signature
*ir
)
943 assert(!"not reached");
948 fs_visitor::emit(fs_inst inst
)
950 fs_inst
*list_inst
= new(mem_ctx
) fs_inst
;
953 list_inst
->annotation
= this->current_annotation
;
954 list_inst
->ir
= this->base_ir
;
956 this->instructions
.push_tail(list_inst
);
961 /** Emits a dummy fragment shader consisting of magenta for bringup purposes. */
963 fs_visitor::emit_dummy_fs()
965 /* Everyone's favorite color. */
966 emit(fs_inst(BRW_OPCODE_MOV
,
969 emit(fs_inst(BRW_OPCODE_MOV
,
972 emit(fs_inst(BRW_OPCODE_MOV
,
975 emit(fs_inst(BRW_OPCODE_MOV
,
980 write
= emit(fs_inst(FS_OPCODE_FB_WRITE
,
985 /* The register location here is relative to the start of the URB
986 * data. It will get adjusted to be a real location before
987 * generate_code() time.
990 fs_visitor::interp_reg(int location
, int channel
)
992 int regnr
= location
* 2 + channel
/ 2;
993 int stride
= (channel
& 1) * 4;
995 return brw_vec1_grf(regnr
, stride
);
998 /** Emits the interpolation for the varying inputs. */
1000 fs_visitor::emit_interpolation()
1002 struct brw_reg g1_uw
= retype(brw_vec1_grf(1, 0), BRW_REGISTER_TYPE_UW
);
1003 /* For now, the source regs for the setup URB data will be unset,
1004 * since we don't know until codegen how many push constants we'll
1005 * use, and therefore what the setup URB offset is.
1007 fs_reg src_reg
= reg_undef
;
1009 this->current_annotation
= "compute pixel centers";
1010 this->pixel_x
= fs_reg(this, glsl_type::uint_type
);
1011 this->pixel_y
= fs_reg(this, glsl_type::uint_type
);
1012 emit(fs_inst(BRW_OPCODE_ADD
,
1014 fs_reg(stride(suboffset(g1_uw
, 4), 2, 4, 0)),
1015 fs_reg(brw_imm_v(0x10101010))));
1016 emit(fs_inst(BRW_OPCODE_ADD
,
1018 fs_reg(stride(suboffset(g1_uw
, 5), 2, 4, 0)),
1019 fs_reg(brw_imm_v(0x11001100))));
1021 this->current_annotation
= "compute pixel deltas from v0";
1022 this->delta_x
= fs_reg(this, glsl_type::float_type
);
1023 this->delta_y
= fs_reg(this, glsl_type::float_type
);
1024 emit(fs_inst(BRW_OPCODE_ADD
,
1027 fs_reg(negate(brw_vec1_grf(1, 0)))));
1028 emit(fs_inst(BRW_OPCODE_ADD
,
1031 fs_reg(brw_vec1_grf(1, 1))));
1033 this->current_annotation
= "compute pos.w and 1/pos.w";
1034 /* Compute wpos. Unlike many other varying inputs, we usually need it
1035 * to produce 1/w, and the varying variable wouldn't show up.
1037 fs_reg wpos
= fs_reg(this, glsl_type::vec4_type
);
1038 this->interp_attrs
[FRAG_ATTRIB_WPOS
] = wpos
;
1039 emit(fs_inst(BRW_OPCODE_MOV
, wpos
, this->pixel_x
)); /* FINISHME: ARB_fcc */
1041 emit(fs_inst(BRW_OPCODE_MOV
, wpos
, this->pixel_y
)); /* FINISHME: ARB_fcc */
1043 emit(fs_inst(FS_OPCODE_LINTERP
, wpos
, this->delta_x
, this->delta_y
,
1044 interp_reg(FRAG_ATTRIB_WPOS
, 2)));
1046 emit(fs_inst(FS_OPCODE_LINTERP
, wpos
, this->delta_x
, this->delta_y
,
1047 interp_reg(FRAG_ATTRIB_WPOS
, 3)));
1048 /* Compute the pixel W value from wpos.w. */
1049 this->pixel_w
= fs_reg(this, glsl_type::float_type
);
1050 emit(fs_inst(FS_OPCODE_RCP
, this->pixel_w
, wpos
));
1052 /* FINISHME: gl_FrontFacing */
1054 foreach_iter(exec_list_iterator
, iter
, *this->shader
->ir
) {
1055 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
1056 ir_variable
*var
= ir
->as_variable();
1061 if (var
->mode
!= ir_var_in
)
1064 /* If it's already set up (WPOS), skip. */
1065 if (var
->location
== 0)
1068 this->current_annotation
= talloc_asprintf(this->mem_ctx
,
1070 "(FRAG_ATTRIB[%d])",
1073 emit_pinterp(var
->location
);
1075 this->current_annotation
= NULL
;
1079 fs_visitor::emit_pinterp(int location
)
1081 fs_reg interp_attr
= fs_reg(this, glsl_type::vec4_type
);
1082 this->interp_attrs
[location
] = interp_attr
;
1084 for (unsigned int i
= 0; i
< 4; i
++) {
1085 struct brw_reg interp
= interp_reg(location
, i
);
1086 emit(fs_inst(FS_OPCODE_LINTERP
,
1091 interp_attr
.reg_offset
++;
1093 interp_attr
.reg_offset
-= 4;
1095 for (unsigned int i
= 0; i
< 4; i
++) {
1096 emit(fs_inst(BRW_OPCODE_MUL
,
1100 interp_attr
.reg_offset
++;
1105 fs_visitor::emit_fb_writes()
1107 this->current_annotation
= "FB write";
1109 assert(this->frag_color
|| !"FINISHME: MRT");
1110 fs_reg color
= *(variable_storage(this->frag_color
));
1112 for (int i
= 0; i
< 4; i
++) {
1113 emit(fs_inst(BRW_OPCODE_MOV
,
1119 emit(fs_inst(FS_OPCODE_FB_WRITE
,
1123 this->current_annotation
= NULL
;
1127 fs_visitor::generate_fb_write(fs_inst
*inst
)
1129 GLboolean eot
= 1; /* FINISHME: MRT */
1130 /* FINISHME: AADS */
1132 /* Header is 2 regs, g0 and g1 are the contents. g0 will be implied
1135 brw_push_insn_state(p
);
1136 brw_set_mask_control(p
, BRW_MASK_DISABLE
);
1137 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
1140 brw_vec8_grf(1, 0));
1141 brw_pop_insn_state(p
);
1146 8, /* dispatch_width */
1147 retype(vec8(brw_null_reg()), BRW_REGISTER_TYPE_UW
),
1149 retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UW
),
1150 0, /* FINISHME: MRT target */
1157 fs_visitor::generate_linterp(fs_inst
*inst
,
1158 struct brw_reg dst
, struct brw_reg
*src
)
1160 struct brw_reg delta_x
= src
[0];
1161 struct brw_reg delta_y
= src
[1];
1162 struct brw_reg interp
= src
[2];
1165 delta_y
.nr
== delta_x
.nr
+ 1 &&
1166 (intel
->gen
>= 6 || (delta_x
.nr
& 1) == 0)) {
1167 brw_PLN(p
, dst
, interp
, delta_x
);
1169 brw_LINE(p
, brw_null_reg(), interp
, delta_x
);
1170 brw_MAC(p
, dst
, suboffset(interp
, 1), delta_y
);
1175 fs_visitor::generate_math(fs_inst
*inst
,
1176 struct brw_reg dst
, struct brw_reg
*src
)
1180 switch (inst
->opcode
) {
1182 op
= BRW_MATH_FUNCTION_INV
;
1185 op
= BRW_MATH_FUNCTION_RSQ
;
1187 case FS_OPCODE_SQRT
:
1188 op
= BRW_MATH_FUNCTION_SQRT
;
1190 case FS_OPCODE_EXP2
:
1191 op
= BRW_MATH_FUNCTION_EXP
;
1193 case FS_OPCODE_LOG2
:
1194 op
= BRW_MATH_FUNCTION_LOG
;
1197 op
= BRW_MATH_FUNCTION_POW
;
1200 op
= BRW_MATH_FUNCTION_SIN
;
1203 op
= BRW_MATH_FUNCTION_COS
;
1206 assert(!"not reached: unknown math function");
1211 if (inst
->opcode
== FS_OPCODE_POW
) {
1212 brw_MOV(p
, brw_message_reg(3), src
[1]);
1217 inst
->saturate
? BRW_MATH_SATURATE_SATURATE
:
1218 BRW_MATH_SATURATE_NONE
,
1220 BRW_MATH_DATA_VECTOR
,
1221 BRW_MATH_PRECISION_FULL
);
1225 trivial_assign_reg(int header_size
, fs_reg
*reg
)
1227 if (reg
->file
== GRF
&& reg
->reg
!= 0) {
1228 reg
->hw_reg
= header_size
+ reg
->reg
- 1 + reg
->reg_offset
;
1234 fs_visitor::assign_urb_setup()
1236 int urb_start
= c
->key
.nr_payload_regs
; /* FINISHME: push constants */
1237 int interp_reg_nr
[FRAG_ATTRIB_MAX
];
1239 c
->prog_data
.urb_read_length
= 0;
1241 /* Figure out where each of the incoming setup attributes lands. */
1242 for (unsigned int i
= 0; i
< FRAG_ATTRIB_MAX
; i
++) {
1243 interp_reg_nr
[i
] = -1;
1245 if (i
!= FRAG_ATTRIB_WPOS
&&
1246 !(brw
->fragment_program
->Base
.InputsRead
& BITFIELD64_BIT(i
)))
1249 /* Each attribute is 4 setup channels, each of which is half a reg. */
1250 interp_reg_nr
[i
] = urb_start
+ c
->prog_data
.urb_read_length
;
1251 c
->prog_data
.urb_read_length
+= 2;
1254 /* Map the register numbers for FS_OPCODE_LINTERP so that it uses
1255 * the correct setup input.
1257 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
1258 fs_inst
*inst
= (fs_inst
*)iter
.get();
1260 if (inst
->opcode
!= FS_OPCODE_LINTERP
)
1263 assert(inst
->src
[2].file
== FIXED_HW_REG
);
1265 int location
= inst
->src
[2].fixed_hw_reg
.nr
/ 2;
1266 assert(interp_reg_nr
[location
] != -1);
1267 inst
->src
[2].fixed_hw_reg
.nr
= (interp_reg_nr
[location
] +
1268 (inst
->src
[2].fixed_hw_reg
.nr
& 1));
1271 this->first_non_payload_grf
= urb_start
+ c
->prog_data
.urb_read_length
;
1275 fs_visitor::assign_regs()
1277 int header_size
= this->first_non_payload_grf
;
1280 /* FINISHME: trivial assignment of register numbers */
1281 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
1282 fs_inst
*inst
= (fs_inst
*)iter
.get();
1284 trivial_assign_reg(header_size
, &inst
->dst
);
1285 trivial_assign_reg(header_size
, &inst
->src
[0]);
1286 trivial_assign_reg(header_size
, &inst
->src
[1]);
1288 last_grf
= MAX2(last_grf
, inst
->dst
.hw_reg
);
1289 last_grf
= MAX2(last_grf
, inst
->src
[0].hw_reg
);
1290 last_grf
= MAX2(last_grf
, inst
->src
[1].hw_reg
);
1293 this->grf_used
= last_grf
;
1296 static struct brw_reg
brw_reg_from_fs_reg(fs_reg
*reg
)
1298 struct brw_reg brw_reg
;
1300 switch (reg
->file
) {
1304 brw_reg
= brw_vec8_reg(reg
->file
,
1306 brw_reg
= retype(brw_reg
, reg
->type
);
1309 switch (reg
->type
) {
1310 case BRW_REGISTER_TYPE_F
:
1311 brw_reg
= brw_imm_f(reg
->imm
.f
);
1313 case BRW_REGISTER_TYPE_D
:
1314 brw_reg
= brw_imm_f(reg
->imm
.i
);
1316 case BRW_REGISTER_TYPE_UD
:
1317 brw_reg
= brw_imm_f(reg
->imm
.u
);
1320 assert(!"not reached");
1325 brw_reg
= reg
->fixed_hw_reg
;
1328 /* Probably unused. */
1329 brw_reg
= brw_null_reg();
1332 brw_reg
= brw_abs(brw_reg
);
1334 brw_reg
= negate(brw_reg
);
1340 fs_visitor::generate_code()
1342 unsigned int annotation_len
= 0;
1343 int last_native_inst
= 0;
1345 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
1346 fs_inst
*inst
= (fs_inst
*)iter
.get();
1347 struct brw_reg src
[3], dst
;
1349 for (unsigned int i
= 0; i
< 3; i
++) {
1350 src
[i
] = brw_reg_from_fs_reg(&inst
->src
[i
]);
1352 dst
= brw_reg_from_fs_reg(&inst
->dst
);
1354 brw_set_conditionalmod(p
, inst
->conditional_mod
);
1355 brw_set_predicate_control(p
, inst
->predicated
);
1357 switch (inst
->opcode
) {
1358 case BRW_OPCODE_MOV
:
1359 brw_MOV(p
, dst
, src
[0]);
1361 case BRW_OPCODE_ADD
:
1362 brw_ADD(p
, dst
, src
[0], src
[1]);
1364 case BRW_OPCODE_MUL
:
1365 brw_MUL(p
, dst
, src
[0], src
[1]);
1369 case FS_OPCODE_SQRT
:
1370 case FS_OPCODE_EXP2
:
1371 case FS_OPCODE_LOG2
:
1375 generate_math(inst
, dst
, src
);
1377 case FS_OPCODE_LINTERP
:
1378 generate_linterp(inst
, dst
, src
);
1380 case FS_OPCODE_FB_WRITE
:
1381 generate_fb_write(inst
);
1384 assert(!"not reached");
1387 if (annotation_len
< p
->nr_insn
) {
1388 annotation_len
*= 2;
1389 if (annotation_len
< 16)
1390 annotation_len
= 16;
1392 this->annotation_string
= talloc_realloc(this->mem_ctx
,
1396 this->annotation_ir
= talloc_realloc(this->mem_ctx
,
1402 for (unsigned int i
= last_native_inst
; i
< p
->nr_insn
; i
++) {
1403 this->annotation_string
[i
] = inst
->annotation
;
1404 this->annotation_ir
[i
] = inst
->ir
;
1406 last_native_inst
= p
->nr_insn
;
1411 brw_wm_fs_emit(struct brw_context
*brw
, struct brw_wm_compile
*c
)
1413 struct brw_compile
*p
= &c
->func
;
1414 struct intel_context
*intel
= &brw
->intel
;
1415 GLcontext
*ctx
= &intel
->ctx
;
1416 struct brw_shader
*shader
= NULL
;
1417 struct gl_shader_program
*prog
= ctx
->Shader
.CurrentProgram
;
1425 for (unsigned int i
= 0; i
< prog
->_NumLinkedShaders
; i
++) {
1426 if (prog
->_LinkedShaders
[i
]->Type
== GL_FRAGMENT_SHADER
) {
1427 shader
= (struct brw_shader
*)prog
->_LinkedShaders
[i
];
1434 /* We always use 8-wide mode, at least for now. For one, flow
1435 * control only works in 8-wide. Also, when we're fragment shader
1436 * bound, we're almost always under register pressure as well, so
1437 * 8-wide would save us from the performance cliff of spilling
1440 c
->dispatch_width
= 8;
1442 if (INTEL_DEBUG
& DEBUG_WM
) {
1443 printf("GLSL IR for native fragment shader %d:\n", prog
->Name
);
1444 _mesa_print_ir(shader
->ir
, NULL
);
1448 /* Now the main event: Visit the shader IR and generate our FS IR for it.
1450 fs_visitor
v(c
, shader
);
1455 v
.emit_interpolation();
1457 /* Generate FS IR for main(). (the visitor only descends into
1458 * functions called "main").
1460 foreach_iter(exec_list_iterator
, iter
, *shader
->ir
) {
1461 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
1470 v
.assign_urb_setup();
1476 if (INTEL_DEBUG
& DEBUG_WM
) {
1477 const char *last_annotation_string
= NULL
;
1478 ir_instruction
*last_annotation_ir
= NULL
;
1480 printf("Native code for fragment shader %d:\n", prog
->Name
);
1481 for (unsigned int i
= 0; i
< p
->nr_insn
; i
++) {
1482 if (last_annotation_ir
!= v
.annotation_ir
[i
]) {
1483 last_annotation_ir
= v
.annotation_ir
[i
];
1484 if (last_annotation_ir
) {
1486 last_annotation_ir
->print();
1490 if (last_annotation_string
!= v
.annotation_string
[i
]) {
1491 last_annotation_string
= v
.annotation_string
[i
];
1492 if (last_annotation_string
)
1493 printf(" %s\n", last_annotation_string
);
1495 brw_disasm(stdout
, &p
->store
[i
], intel
->gen
);
1500 c
->prog_data
.nr_params
= 0; /* FINISHME */
1501 c
->prog_data
.first_curbe_grf
= c
->key
.nr_payload_regs
;
1502 c
->prog_data
.curb_read_length
= 0; /* FINISHME */
1503 c
->prog_data
.total_grf
= v
.grf_used
;
1504 c
->prog_data
.total_scratch
= 0;