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 "main/uniforms.h"
35 #include "program/prog_parameter.h"
36 #include "program/prog_print.h"
37 #include "program/prog_optimize.h"
38 #include "program/register_allocate.h"
39 #include "program/sampler.h"
40 #include "program/hash_table.h"
41 #include "brw_context.h"
46 #include "../glsl/glsl_types.h"
47 #include "../glsl/ir_optimization.h"
48 #include "../glsl/ir_print_visitor.h"
51 ARF
= BRW_ARCHITECTURE_REGISTER_FILE
,
52 GRF
= BRW_GENERAL_REGISTER_FILE
,
53 MRF
= BRW_MESSAGE_REGISTER_FILE
,
54 IMM
= BRW_IMMEDIATE_VALUE
,
55 FIXED_HW_REG
, /* a struct brw_reg */
56 UNIFORM
, /* prog_data->params[hw_reg] */
61 FS_OPCODE_FB_WRITE
= 256,
79 static int using_new_fs
= -1;
80 static struct brw_reg
brw_reg_from_fs_reg(class fs_reg
*reg
);
83 brw_new_shader(GLcontext
*ctx
, GLuint name
, GLuint type
)
85 struct brw_shader
*shader
;
87 shader
= talloc_zero(NULL
, struct brw_shader
);
89 shader
->base
.Type
= type
;
90 shader
->base
.Name
= name
;
91 _mesa_init_shader(ctx
, &shader
->base
);
97 struct gl_shader_program
*
98 brw_new_shader_program(GLcontext
*ctx
, GLuint name
)
100 struct brw_shader_program
*prog
;
101 prog
= talloc_zero(NULL
, struct brw_shader_program
);
103 prog
->base
.Name
= name
;
104 _mesa_init_shader_program(ctx
, &prog
->base
);
110 brw_compile_shader(GLcontext
*ctx
, struct gl_shader
*shader
)
112 if (!_mesa_ir_compile_shader(ctx
, shader
))
119 brw_link_shader(GLcontext
*ctx
, struct gl_shader_program
*prog
)
121 if (using_new_fs
== -1)
122 using_new_fs
= getenv("INTEL_NEW_FS") != NULL
;
124 for (unsigned i
= 0; i
< prog
->_NumLinkedShaders
; i
++) {
125 struct brw_shader
*shader
= (struct brw_shader
*)prog
->_LinkedShaders
[i
];
127 if (using_new_fs
&& shader
->base
.Type
== GL_FRAGMENT_SHADER
) {
128 void *mem_ctx
= talloc_new(NULL
);
132 talloc_free(shader
->ir
);
133 shader
->ir
= new(shader
) exec_list
;
134 clone_ir_list(mem_ctx
, shader
->ir
, shader
->base
.ir
);
136 do_mat_op_to_vec(shader
->ir
);
137 do_mod_to_fract(shader
->ir
);
138 do_div_to_mul_rcp(shader
->ir
);
139 do_sub_to_add_neg(shader
->ir
);
140 do_explog_to_explog2(shader
->ir
);
141 do_lower_texture_projection(shader
->ir
);
146 brw_do_channel_expressions(shader
->ir
);
147 brw_do_vector_splitting(shader
->ir
);
149 progress
= do_lower_jumps(shader
->ir
, true, true,
150 true, /* main return */
151 false, /* continue */
155 progress
= do_common_optimization(shader
->ir
, true, 32) || progress
;
157 progress
= lower_noise(shader
->ir
) || progress
;
159 lower_variable_index_to_cond_assign(shader
->ir
,
161 GL_TRUE
, /* output */
163 GL_TRUE
/* uniform */
167 validate_ir_tree(shader
->ir
);
169 reparent_ir(shader
->ir
, shader
->ir
);
170 talloc_free(mem_ctx
);
174 if (!_mesa_ir_link_shader(ctx
, prog
))
181 type_size(const struct glsl_type
*type
)
183 unsigned int size
, i
;
185 switch (type
->base_type
) {
188 case GLSL_TYPE_FLOAT
:
190 return type
->components();
191 case GLSL_TYPE_ARRAY
:
192 return type_size(type
->fields
.array
) * type
->length
;
193 case GLSL_TYPE_STRUCT
:
195 for (i
= 0; i
< type
->length
; i
++) {
196 size
+= type_size(type
->fields
.structure
[i
].type
);
199 case GLSL_TYPE_SAMPLER
:
200 /* Samplers take up no register space, since they're baked in at
205 assert(!"not reached");
212 /* Callers of this talloc-based new need not call delete. It's
213 * easier to just talloc_free 'ctx' (or any of its ancestors). */
214 static void* operator new(size_t size
, void *ctx
)
218 node
= talloc_size(ctx
, size
);
219 assert(node
!= NULL
);
227 this->reg_offset
= 0;
233 /** Generic unset register constructor. */
237 this->file
= BAD_FILE
;
240 /** Immediate value constructor. */
245 this->type
= BRW_REGISTER_TYPE_F
;
249 /** Immediate value constructor. */
254 this->type
= BRW_REGISTER_TYPE_D
;
258 /** Immediate value constructor. */
263 this->type
= BRW_REGISTER_TYPE_UD
;
267 /** Fixed brw_reg Immediate value constructor. */
268 fs_reg(struct brw_reg fixed_hw_reg
)
271 this->file
= FIXED_HW_REG
;
272 this->fixed_hw_reg
= fixed_hw_reg
;
273 this->type
= fixed_hw_reg
.type
;
276 fs_reg(enum register_file file
, int hw_reg
);
277 fs_reg(class fs_visitor
*v
, const struct glsl_type
*type
);
279 /** Register file: ARF, GRF, MRF, IMM. */
280 enum register_file file
;
281 /** virtual register number. 0 = fixed hw reg */
283 /** Offset within the virtual register. */
285 /** HW register number. Generally unset until register allocation. */
287 /** Register type. BRW_REGISTER_TYPE_* */
291 struct brw_reg fixed_hw_reg
;
293 /** Value for file == BRW_IMMMEDIATE_FILE */
301 static const fs_reg reg_undef
;
302 static const fs_reg
reg_null(ARF
, BRW_ARF_NULL
);
304 class fs_inst
: public exec_node
{
306 /* Callers of this talloc-based new need not call delete. It's
307 * easier to just talloc_free 'ctx' (or any of its ancestors). */
308 static void* operator new(size_t size
, void *ctx
)
312 node
= talloc_zero_size(ctx
, size
);
313 assert(node
!= NULL
);
320 this->opcode
= BRW_OPCODE_NOP
;
321 this->saturate
= false;
322 this->conditional_mod
= BRW_CONDITIONAL_NONE
;
323 this->predicated
= false;
327 this->shadow_compare
= false;
338 this->opcode
= opcode
;
341 fs_inst(int opcode
, fs_reg dst
, fs_reg src0
)
344 this->opcode
= opcode
;
349 fs_inst(int opcode
, fs_reg dst
, fs_reg src0
, fs_reg src1
)
352 this->opcode
= opcode
;
358 fs_inst(int opcode
, fs_reg dst
, fs_reg src0
, fs_reg src1
, fs_reg src2
)
361 this->opcode
= opcode
;
368 int opcode
; /* BRW_OPCODE_* or FS_OPCODE_* */
373 int conditional_mod
; /**< BRW_CONDITIONAL_* */
375 int mlen
; /**< SEND message length */
377 int target
; /**< MRT target. */
382 * Annotation for the generated IR. One of the two can be set.
385 const char *annotation
;
389 class fs_visitor
: public ir_visitor
393 fs_visitor(struct brw_wm_compile
*c
, struct brw_shader
*shader
)
398 this->fp
= brw
->fragment_program
;
399 this->intel
= &brw
->intel
;
400 this->ctx
= &intel
->ctx
;
401 this->mem_ctx
= talloc_new(NULL
);
402 this->shader
= shader
;
404 this->variable_ht
= hash_table_ctor(0,
405 hash_table_pointer_hash
,
406 hash_table_pointer_compare
);
408 this->frag_color
= NULL
;
409 this->frag_data
= NULL
;
410 this->frag_depth
= NULL
;
411 this->first_non_payload_grf
= 0;
413 this->current_annotation
= NULL
;
414 this->annotation_string
= NULL
;
415 this->annotation_ir
= NULL
;
416 this->base_ir
= NULL
;
418 this->virtual_grf_sizes
= NULL
;
419 this->virtual_grf_next
= 1;
420 this->virtual_grf_array_size
= 0;
421 this->virtual_grf_def
= NULL
;
422 this->virtual_grf_use
= NULL
;
426 talloc_free(this->mem_ctx
);
427 hash_table_dtor(this->variable_ht
);
430 fs_reg
*variable_storage(ir_variable
*var
);
431 int virtual_grf_alloc(int size
);
433 void visit(ir_variable
*ir
);
434 void visit(ir_assignment
*ir
);
435 void visit(ir_dereference_variable
*ir
);
436 void visit(ir_dereference_record
*ir
);
437 void visit(ir_dereference_array
*ir
);
438 void visit(ir_expression
*ir
);
439 void visit(ir_texture
*ir
);
440 void visit(ir_if
*ir
);
441 void visit(ir_constant
*ir
);
442 void visit(ir_swizzle
*ir
);
443 void visit(ir_return
*ir
);
444 void visit(ir_loop
*ir
);
445 void visit(ir_loop_jump
*ir
);
446 void visit(ir_discard
*ir
);
447 void visit(ir_call
*ir
);
448 void visit(ir_function
*ir
);
449 void visit(ir_function_signature
*ir
);
451 fs_inst
*emit(fs_inst inst
);
452 void assign_curb_setup();
453 void assign_urb_setup();
455 void assign_regs_trivial();
456 void calculate_live_intervals();
457 bool virtual_grf_interferes(int a
, int b
);
458 void generate_code();
459 void generate_fb_write(fs_inst
*inst
);
460 void generate_linterp(fs_inst
*inst
, struct brw_reg dst
,
461 struct brw_reg
*src
);
462 void generate_tex(fs_inst
*inst
, struct brw_reg dst
, struct brw_reg src
);
463 void generate_math(fs_inst
*inst
, struct brw_reg dst
, struct brw_reg
*src
);
464 void generate_discard(fs_inst
*inst
, struct brw_reg temp
);
465 void generate_ddx(fs_inst
*inst
, struct brw_reg dst
, struct brw_reg src
);
466 void generate_ddy(fs_inst
*inst
, struct brw_reg dst
, struct brw_reg src
);
468 void emit_dummy_fs();
469 void emit_fragcoord_interpolation(ir_variable
*ir
);
470 void emit_general_interpolation(ir_variable
*ir
);
471 void emit_interpolation_setup();
472 fs_inst
*emit_texture_gen4(ir_texture
*ir
, fs_reg dst
, int base_mrf
);
473 fs_inst
*emit_texture_gen5(ir_texture
*ir
, fs_reg dst
, int base_mrf
);
474 void emit_fb_writes();
475 void emit_assignment_writes(fs_reg
&l
, fs_reg
&r
,
476 const glsl_type
*type
, bool predicated
);
478 struct brw_reg
interp_reg(int location
, int channel
);
479 int setup_uniform_values(int loc
, const glsl_type
*type
);
480 void setup_builtin_uniform_values(ir_variable
*ir
);
482 struct brw_context
*brw
;
483 const struct gl_fragment_program
*fp
;
484 struct intel_context
*intel
;
486 struct brw_wm_compile
*c
;
487 struct brw_compile
*p
;
488 struct brw_shader
*shader
;
490 exec_list instructions
;
492 int *virtual_grf_sizes
;
493 int virtual_grf_next
;
494 int virtual_grf_array_size
;
495 int *virtual_grf_def
;
496 int *virtual_grf_use
;
498 struct hash_table
*variable_ht
;
499 ir_variable
*frag_color
, *frag_data
, *frag_depth
;
500 int first_non_payload_grf
;
502 /** @{ debug annotation info */
503 const char *current_annotation
;
504 ir_instruction
*base_ir
;
505 const char **annotation_string
;
506 ir_instruction
**annotation_ir
;
511 /* Result of last visit() method. */
526 fs_visitor::virtual_grf_alloc(int size
)
528 if (virtual_grf_array_size
<= virtual_grf_next
) {
529 if (virtual_grf_array_size
== 0)
530 virtual_grf_array_size
= 16;
532 virtual_grf_array_size
*= 2;
533 virtual_grf_sizes
= talloc_realloc(mem_ctx
, virtual_grf_sizes
,
534 int, virtual_grf_array_size
);
536 /* This slot is always unused. */
537 virtual_grf_sizes
[0] = 0;
539 virtual_grf_sizes
[virtual_grf_next
] = size
;
540 return virtual_grf_next
++;
543 /** Fixed HW reg constructor. */
544 fs_reg::fs_reg(enum register_file file
, int hw_reg
)
548 this->hw_reg
= hw_reg
;
549 this->type
= BRW_REGISTER_TYPE_F
;
553 brw_type_for_base_type(const struct glsl_type
*type
)
555 switch (type
->base_type
) {
556 case GLSL_TYPE_FLOAT
:
557 return BRW_REGISTER_TYPE_F
;
560 return BRW_REGISTER_TYPE_D
;
562 return BRW_REGISTER_TYPE_UD
;
563 case GLSL_TYPE_ARRAY
:
564 case GLSL_TYPE_STRUCT
:
565 /* These should be overridden with the type of the member when
566 * dereferenced into. BRW_REGISTER_TYPE_UD seems like a likely
567 * way to trip up if we don't.
569 return BRW_REGISTER_TYPE_UD
;
571 assert(!"not reached");
572 return BRW_REGISTER_TYPE_F
;
576 /** Automatic reg constructor. */
577 fs_reg::fs_reg(class fs_visitor
*v
, const struct glsl_type
*type
)
582 this->reg
= v
->virtual_grf_alloc(type_size(type
));
583 this->reg_offset
= 0;
584 this->type
= brw_type_for_base_type(type
);
588 fs_visitor::variable_storage(ir_variable
*var
)
590 return (fs_reg
*)hash_table_find(this->variable_ht
, var
);
593 /* Our support for uniforms is piggy-backed on the struct
594 * gl_fragment_program, because that's where the values actually
595 * get stored, rather than in some global gl_shader_program uniform
599 fs_visitor::setup_uniform_values(int loc
, const glsl_type
*type
)
601 unsigned int offset
= 0;
604 if (type
->is_matrix()) {
605 const glsl_type
*column
= glsl_type::get_instance(GLSL_TYPE_FLOAT
,
606 type
->vector_elements
,
609 for (unsigned int i
= 0; i
< type
->matrix_columns
; i
++) {
610 offset
+= setup_uniform_values(loc
+ offset
, column
);
616 switch (type
->base_type
) {
617 case GLSL_TYPE_FLOAT
:
621 vec_values
= fp
->Base
.Parameters
->ParameterValues
[loc
];
622 for (unsigned int i
= 0; i
< type
->vector_elements
; i
++) {
623 c
->prog_data
.param
[c
->prog_data
.nr_params
++] = &vec_values
[i
];
627 case GLSL_TYPE_STRUCT
:
628 for (unsigned int i
= 0; i
< type
->length
; i
++) {
629 offset
+= setup_uniform_values(loc
+ offset
,
630 type
->fields
.structure
[i
].type
);
634 case GLSL_TYPE_ARRAY
:
635 for (unsigned int i
= 0; i
< type
->length
; i
++) {
636 offset
+= setup_uniform_values(loc
+ offset
, type
->fields
.array
);
640 case GLSL_TYPE_SAMPLER
:
641 /* The sampler takes up a slot, but we don't use any values from it. */
645 assert(!"not reached");
651 /* Our support for builtin uniforms is even scarier than non-builtin.
652 * It sits on top of the PROG_STATE_VAR parameters that are
653 * automatically updated from GL context state.
656 fs_visitor::setup_builtin_uniform_values(ir_variable
*ir
)
658 const struct gl_builtin_uniform_desc
*statevar
= NULL
;
660 for (unsigned int i
= 0; _mesa_builtin_uniform_desc
[i
].name
; i
++) {
661 statevar
= &_mesa_builtin_uniform_desc
[i
];
662 if (strcmp(ir
->name
, _mesa_builtin_uniform_desc
[i
].name
) == 0)
666 if (!statevar
->name
) {
668 printf("Failed to find builtin uniform `%s'\n", ir
->name
);
673 if (ir
->type
->is_array()) {
674 array_count
= ir
->type
->length
;
679 for (int a
= 0; a
< array_count
; a
++) {
680 for (unsigned int i
= 0; i
< statevar
->num_elements
; i
++) {
681 struct gl_builtin_uniform_element
*element
= &statevar
->elements
[i
];
682 int tokens
[STATE_LENGTH
];
684 memcpy(tokens
, element
->tokens
, sizeof(element
->tokens
));
685 if (ir
->type
->is_array()) {
689 /* This state reference has already been setup by ir_to_mesa,
690 * but we'll get the same index back here.
692 int index
= _mesa_add_state_reference(this->fp
->Base
.Parameters
,
693 (gl_state_index
*)tokens
);
694 float *vec_values
= this->fp
->Base
.Parameters
->ParameterValues
[index
];
696 /* Add each of the unique swizzles of the element as a
697 * parameter. This'll end up matching the expected layout of
698 * the array/matrix/structure we're trying to fill in.
701 for (unsigned int i
= 0; i
< 4; i
++) {
702 int swiz
= GET_SWZ(element
->swizzle
, i
);
703 if (swiz
== last_swiz
)
707 c
->prog_data
.param
[c
->prog_data
.nr_params
++] = &vec_values
[swiz
];
714 fs_visitor::emit_fragcoord_interpolation(ir_variable
*ir
)
716 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(this, ir
->type
);
718 fs_reg neg_y
= this->pixel_y
;
722 if (ir
->pixel_center_integer
) {
723 emit(fs_inst(BRW_OPCODE_MOV
, wpos
, this->pixel_x
));
725 emit(fs_inst(BRW_OPCODE_ADD
, wpos
, this->pixel_x
, fs_reg(0.5f
)));
730 if (ir
->origin_upper_left
&& ir
->pixel_center_integer
) {
731 emit(fs_inst(BRW_OPCODE_MOV
, wpos
, this->pixel_y
));
733 fs_reg pixel_y
= this->pixel_y
;
734 float offset
= (ir
->pixel_center_integer
? 0.0 : 0.5);
736 if (!ir
->origin_upper_left
) {
737 pixel_y
.negate
= true;
738 offset
+= c
->key
.drawable_height
- 1.0;
741 emit(fs_inst(BRW_OPCODE_ADD
, wpos
, pixel_y
, fs_reg(offset
)));
746 emit(fs_inst(FS_OPCODE_LINTERP
, wpos
, this->delta_x
, this->delta_y
,
747 interp_reg(FRAG_ATTRIB_WPOS
, 2)));
750 /* gl_FragCoord.w: Already set up in emit_interpolation */
751 emit(fs_inst(BRW_OPCODE_MOV
, wpos
, this->wpos_w
));
753 hash_table_insert(this->variable_ht
, reg
, ir
);
758 fs_visitor::emit_general_interpolation(ir_variable
*ir
)
760 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(this, ir
->type
);
761 /* Interpolation is always in floating point regs. */
762 reg
->type
= BRW_REGISTER_TYPE_F
;
765 unsigned int array_elements
;
766 const glsl_type
*type
;
768 if (ir
->type
->is_array()) {
769 array_elements
= ir
->type
->length
;
770 if (array_elements
== 0) {
773 type
= ir
->type
->fields
.array
;
779 int location
= ir
->location
;
780 for (unsigned int i
= 0; i
< array_elements
; i
++) {
781 for (unsigned int j
= 0; j
< type
->matrix_columns
; j
++) {
782 if (!(fp
->Base
.InputsRead
& BITFIELD64_BIT(location
))) {
783 /* If there's no incoming setup data for this slot, don't
784 * emit interpolation for it (since it's not used, and
785 * we'd fall over later trying to find the setup data.
787 attr
.reg_offset
+= type
->vector_elements
;
791 for (unsigned int c
= 0; c
< type
->vector_elements
; c
++) {
792 struct brw_reg interp
= interp_reg(location
, c
);
793 emit(fs_inst(FS_OPCODE_LINTERP
,
800 attr
.reg_offset
-= type
->vector_elements
;
802 for (unsigned int c
= 0; c
< type
->vector_elements
; c
++) {
803 emit(fs_inst(BRW_OPCODE_MUL
,
813 hash_table_insert(this->variable_ht
, reg
, ir
);
817 fs_visitor::visit(ir_variable
*ir
)
821 if (variable_storage(ir
))
824 if (strcmp(ir
->name
, "gl_FragColor") == 0) {
825 this->frag_color
= ir
;
826 } else if (strcmp(ir
->name
, "gl_FragData") == 0) {
827 this->frag_data
= ir
;
828 } else if (strcmp(ir
->name
, "gl_FragDepth") == 0) {
829 this->frag_depth
= ir
;
832 if (ir
->mode
== ir_var_in
) {
833 if (!strcmp(ir
->name
, "gl_FragCoord")) {
834 emit_fragcoord_interpolation(ir
);
836 } else if (!strcmp(ir
->name
, "gl_FrontFacing")) {
837 reg
= new(this->mem_ctx
) fs_reg(this, ir
->type
);
838 struct brw_reg r1_6ud
= retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_UD
);
839 /* bit 31 is "primitive is back face", so checking < (1 << 31) gives
842 fs_inst
*inst
= emit(fs_inst(BRW_OPCODE_CMP
,
846 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
847 emit(fs_inst(BRW_OPCODE_AND
, *reg
, *reg
, fs_reg(1u)));
849 emit_general_interpolation(ir
);
854 if (ir
->mode
== ir_var_uniform
) {
855 int param_index
= c
->prog_data
.nr_params
;
857 if (!strncmp(ir
->name
, "gl_", 3)) {
858 setup_builtin_uniform_values(ir
);
860 setup_uniform_values(ir
->location
, ir
->type
);
863 reg
= new(this->mem_ctx
) fs_reg(UNIFORM
, param_index
);
867 reg
= new(this->mem_ctx
) fs_reg(this, ir
->type
);
869 hash_table_insert(this->variable_ht
, reg
, ir
);
873 fs_visitor::visit(ir_dereference_variable
*ir
)
875 fs_reg
*reg
= variable_storage(ir
->var
);
880 fs_visitor::visit(ir_dereference_record
*ir
)
882 const glsl_type
*struct_type
= ir
->record
->type
;
884 ir
->record
->accept(this);
886 unsigned int offset
= 0;
887 for (unsigned int i
= 0; i
< struct_type
->length
; i
++) {
888 if (strcmp(struct_type
->fields
.structure
[i
].name
, ir
->field
) == 0)
890 offset
+= type_size(struct_type
->fields
.structure
[i
].type
);
892 this->result
.reg_offset
+= offset
;
893 this->result
.type
= brw_type_for_base_type(ir
->type
);
897 fs_visitor::visit(ir_dereference_array
*ir
)
902 ir
->array
->accept(this);
903 index
= ir
->array_index
->as_constant();
905 element_size
= type_size(ir
->type
);
906 this->result
.type
= brw_type_for_base_type(ir
->type
);
909 assert(this->result
.file
== UNIFORM
||
910 (this->result
.file
== GRF
&&
911 this->result
.reg
!= 0));
912 this->result
.reg_offset
+= index
->value
.i
[0] * element_size
;
914 assert(!"FINISHME: non-constant array element");
919 fs_visitor::visit(ir_expression
*ir
)
921 unsigned int operand
;
926 for (operand
= 0; operand
< ir
->get_num_operands(); operand
++) {
927 ir
->operands
[operand
]->accept(this);
928 if (this->result
.file
== BAD_FILE
) {
930 printf("Failed to get tree for expression operand:\n");
931 ir
->operands
[operand
]->accept(&v
);
934 op
[operand
] = this->result
;
936 /* Matrix expression operands should have been broken down to vector
937 * operations already.
939 assert(!ir
->operands
[operand
]->type
->is_matrix());
940 /* And then those vector operands should have been broken down to scalar.
942 assert(!ir
->operands
[operand
]->type
->is_vector());
945 /* Storage for our result. If our result goes into an assignment, it will
946 * just get copy-propagated out, so no worries.
948 this->result
= fs_reg(this, ir
->type
);
950 switch (ir
->operation
) {
951 case ir_unop_logic_not
:
952 emit(fs_inst(BRW_OPCODE_ADD
, this->result
, op
[0], fs_reg(-1)));
955 op
[0].negate
= !op
[0].negate
;
956 this->result
= op
[0];
960 this->result
= op
[0];
963 temp
= fs_reg(this, ir
->type
);
965 emit(fs_inst(BRW_OPCODE_MOV
, this->result
, fs_reg(0.0f
)));
967 inst
= emit(fs_inst(BRW_OPCODE_CMP
, reg_null
, op
[0], fs_reg(0.0f
)));
968 inst
->conditional_mod
= BRW_CONDITIONAL_G
;
969 inst
= emit(fs_inst(BRW_OPCODE_MOV
, this->result
, fs_reg(1.0f
)));
970 inst
->predicated
= true;
972 inst
= emit(fs_inst(BRW_OPCODE_CMP
, reg_null
, op
[0], fs_reg(0.0f
)));
973 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
974 inst
= emit(fs_inst(BRW_OPCODE_MOV
, this->result
, fs_reg(-1.0f
)));
975 inst
->predicated
= true;
979 emit(fs_inst(FS_OPCODE_RCP
, this->result
, op
[0]));
983 emit(fs_inst(FS_OPCODE_EXP2
, this->result
, op
[0]));
986 emit(fs_inst(FS_OPCODE_LOG2
, this->result
, op
[0]));
990 assert(!"not reached: should be handled by ir_explog_to_explog2");
993 emit(fs_inst(FS_OPCODE_SIN
, this->result
, op
[0]));
996 emit(fs_inst(FS_OPCODE_COS
, this->result
, op
[0]));
1000 emit(fs_inst(FS_OPCODE_DDX
, this->result
, op
[0]));
1003 emit(fs_inst(FS_OPCODE_DDY
, this->result
, op
[0]));
1007 emit(fs_inst(BRW_OPCODE_ADD
, this->result
, op
[0], op
[1]));
1010 assert(!"not reached: should be handled by ir_sub_to_add_neg");
1014 emit(fs_inst(BRW_OPCODE_MUL
, this->result
, op
[0], op
[1]));
1017 assert(!"not reached: should be handled by ir_div_to_mul_rcp");
1020 assert(!"ir_binop_mod should have been converted to b * fract(a/b)");
1024 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], op
[1]));
1025 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
1026 emit(fs_inst(BRW_OPCODE_AND
, this->result
, this->result
, fs_reg(0x1)));
1028 case ir_binop_greater
:
1029 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], op
[1]));
1030 inst
->conditional_mod
= BRW_CONDITIONAL_G
;
1031 emit(fs_inst(BRW_OPCODE_AND
, this->result
, this->result
, fs_reg(0x1)));
1033 case ir_binop_lequal
:
1034 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], op
[1]));
1035 inst
->conditional_mod
= BRW_CONDITIONAL_LE
;
1036 emit(fs_inst(BRW_OPCODE_AND
, this->result
, this->result
, fs_reg(0x1)));
1038 case ir_binop_gequal
:
1039 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], op
[1]));
1040 inst
->conditional_mod
= BRW_CONDITIONAL_GE
;
1041 emit(fs_inst(BRW_OPCODE_AND
, this->result
, this->result
, fs_reg(0x1)));
1043 case ir_binop_equal
:
1044 case ir_binop_all_equal
: /* same as nequal for scalars */
1045 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], op
[1]));
1046 inst
->conditional_mod
= BRW_CONDITIONAL_Z
;
1047 emit(fs_inst(BRW_OPCODE_AND
, this->result
, this->result
, fs_reg(0x1)));
1049 case ir_binop_nequal
:
1050 case ir_binop_any_nequal
: /* same as nequal for scalars */
1051 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], op
[1]));
1052 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1053 emit(fs_inst(BRW_OPCODE_AND
, this->result
, this->result
, fs_reg(0x1)));
1056 case ir_binop_logic_xor
:
1057 emit(fs_inst(BRW_OPCODE_XOR
, this->result
, op
[0], op
[1]));
1060 case ir_binop_logic_or
:
1061 emit(fs_inst(BRW_OPCODE_OR
, this->result
, op
[0], op
[1]));
1064 case ir_binop_logic_and
:
1065 emit(fs_inst(BRW_OPCODE_AND
, this->result
, op
[0], op
[1]));
1069 case ir_binop_cross
:
1071 assert(!"not reached: should be handled by brw_fs_channel_expressions");
1075 assert(!"not reached: should be handled by lower_noise");
1079 emit(fs_inst(FS_OPCODE_SQRT
, this->result
, op
[0]));
1083 emit(fs_inst(FS_OPCODE_RSQ
, this->result
, op
[0]));
1089 emit(fs_inst(BRW_OPCODE_MOV
, this->result
, op
[0]));
1092 emit(fs_inst(BRW_OPCODE_MOV
, this->result
, op
[0]));
1096 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], fs_reg(0.0f
)));
1097 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1100 emit(fs_inst(BRW_OPCODE_RNDD
, this->result
, op
[0]));
1103 op
[0].negate
= ~op
[0].negate
;
1104 inst
= emit(fs_inst(BRW_OPCODE_RNDD
, this->result
, op
[0]));
1105 this->result
.negate
= true;
1108 inst
= emit(fs_inst(BRW_OPCODE_RNDD
, this->result
, op
[0]));
1111 inst
= emit(fs_inst(BRW_OPCODE_FRC
, this->result
, op
[0]));
1115 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], op
[1]));
1116 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
1118 inst
= emit(fs_inst(BRW_OPCODE_SEL
, this->result
, op
[0], op
[1]));
1119 inst
->predicated
= true;
1122 inst
= emit(fs_inst(BRW_OPCODE_CMP
, this->result
, op
[0], op
[1]));
1123 inst
->conditional_mod
= BRW_CONDITIONAL_G
;
1125 inst
= emit(fs_inst(BRW_OPCODE_SEL
, this->result
, op
[0], op
[1]));
1126 inst
->predicated
= true;
1130 inst
= emit(fs_inst(FS_OPCODE_POW
, this->result
, op
[0], op
[1]));
1133 case ir_unop_bit_not
:
1135 case ir_binop_lshift
:
1136 case ir_binop_rshift
:
1137 case ir_binop_bit_and
:
1138 case ir_binop_bit_xor
:
1139 case ir_binop_bit_or
:
1140 assert(!"GLSL 1.30 features unsupported");
1146 fs_visitor::emit_assignment_writes(fs_reg
&l
, fs_reg
&r
,
1147 const glsl_type
*type
, bool predicated
)
1149 switch (type
->base_type
) {
1150 case GLSL_TYPE_FLOAT
:
1151 case GLSL_TYPE_UINT
:
1153 case GLSL_TYPE_BOOL
:
1154 for (unsigned int i
= 0; i
< type
->components(); i
++) {
1155 l
.type
= brw_type_for_base_type(type
);
1156 r
.type
= brw_type_for_base_type(type
);
1158 fs_inst
*inst
= emit(fs_inst(BRW_OPCODE_MOV
, l
, r
));
1159 inst
->predicated
= predicated
;
1165 case GLSL_TYPE_ARRAY
:
1166 for (unsigned int i
= 0; i
< type
->length
; i
++) {
1167 emit_assignment_writes(l
, r
, type
->fields
.array
, predicated
);
1170 case GLSL_TYPE_STRUCT
:
1171 for (unsigned int i
= 0; i
< type
->length
; i
++) {
1172 emit_assignment_writes(l
, r
, type
->fields
.structure
[i
].type
,
1177 case GLSL_TYPE_SAMPLER
:
1181 assert(!"not reached");
1187 fs_visitor::visit(ir_assignment
*ir
)
1192 /* FINISHME: arrays on the lhs */
1193 ir
->lhs
->accept(this);
1196 ir
->rhs
->accept(this);
1199 assert(l
.file
!= BAD_FILE
);
1200 assert(r
.file
!= BAD_FILE
);
1202 if (ir
->condition
) {
1203 /* Get the condition bool into the predicate. */
1204 ir
->condition
->accept(this);
1205 inst
= emit(fs_inst(BRW_OPCODE_CMP
, reg_null
, this->result
, fs_reg(0)));
1206 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1209 if (ir
->lhs
->type
->is_scalar() ||
1210 ir
->lhs
->type
->is_vector()) {
1211 for (int i
= 0; i
< ir
->lhs
->type
->vector_elements
; i
++) {
1212 if (ir
->write_mask
& (1 << i
)) {
1213 inst
= emit(fs_inst(BRW_OPCODE_MOV
, l
, r
));
1215 inst
->predicated
= true;
1221 emit_assignment_writes(l
, r
, ir
->lhs
->type
, ir
->condition
!= NULL
);
1226 fs_visitor::emit_texture_gen4(ir_texture
*ir
, fs_reg dst
, int base_mrf
)
1228 /* gen4's SIMD8 sampler always has the slots for u,v,r present. */
1231 if (ir
->shadow_comparitor
) {
1232 if (ir
->op
== ir_tex
) {
1233 /* There's no plain shadow compare message, so we use shadow
1234 * compare with a bias of 0.0.
1236 emit(fs_inst(BRW_OPCODE_MOV
, fs_reg(MRF
, base_mrf
+ mlen
),
1239 } else if (ir
->op
== ir_txb
) {
1240 ir
->lod_info
.bias
->accept(this);
1241 emit(fs_inst(BRW_OPCODE_MOV
, fs_reg(MRF
, base_mrf
+ mlen
),
1245 assert(ir
->op
== ir_txl
);
1246 ir
->lod_info
.lod
->accept(this);
1247 emit(fs_inst(BRW_OPCODE_MOV
, fs_reg(MRF
, base_mrf
+ mlen
),
1252 ir
->shadow_comparitor
->accept(this);
1253 emit(fs_inst(BRW_OPCODE_MOV
, fs_reg(MRF
, base_mrf
+ mlen
), this->result
));
1256 /* Oh joy. gen4 doesn't have SIMD8 non-shadow-compare sampler
1257 * instructions. We'll need to do SIMD16 here.
1262 fs_inst
*inst
= NULL
;
1265 inst
= emit(fs_inst(FS_OPCODE_TEX
, dst
, fs_reg(MRF
, base_mrf
)));
1268 inst
= emit(fs_inst(FS_OPCODE_TXB
, dst
, fs_reg(MRF
, base_mrf
)));
1271 inst
= emit(fs_inst(FS_OPCODE_TXL
, dst
, fs_reg(MRF
, base_mrf
)));
1275 assert(!"GLSL 1.30 features unsupported");
1284 fs_visitor::emit_texture_gen5(ir_texture
*ir
, fs_reg dst
, int base_mrf
)
1286 /* gen5's SIMD8 sampler has slots for u, v, r, array index, then
1287 * optional parameters like shadow comparitor or LOD bias. If
1288 * optional parameters aren't present, those base slots are
1289 * optional and don't need to be included in the message.
1291 * We don't fill in the unnecessary slots regardless, which may
1292 * look surprising in the disassembly.
1294 int mlen
= ir
->coordinate
->type
->vector_elements
;
1296 if (ir
->shadow_comparitor
) {
1297 mlen
= MAX2(mlen
, 4);
1299 ir
->shadow_comparitor
->accept(this);
1300 emit(fs_inst(BRW_OPCODE_MOV
, fs_reg(MRF
, base_mrf
+ mlen
), this->result
));
1304 fs_inst
*inst
= NULL
;
1307 inst
= emit(fs_inst(FS_OPCODE_TEX
, dst
, fs_reg(MRF
, base_mrf
)));
1310 ir
->lod_info
.bias
->accept(this);
1311 mlen
= MAX2(mlen
, 4);
1312 emit(fs_inst(BRW_OPCODE_MOV
, fs_reg(MRF
, base_mrf
+ mlen
), this->result
));
1315 inst
= emit(fs_inst(FS_OPCODE_TXB
, dst
, fs_reg(MRF
, base_mrf
)));
1318 ir
->lod_info
.lod
->accept(this);
1319 mlen
= MAX2(mlen
, 4);
1320 emit(fs_inst(BRW_OPCODE_MOV
, fs_reg(MRF
, base_mrf
+ mlen
), this->result
));
1323 inst
= emit(fs_inst(FS_OPCODE_TXL
, dst
, fs_reg(MRF
, base_mrf
)));
1327 assert(!"GLSL 1.30 features unsupported");
1336 fs_visitor::visit(ir_texture
*ir
)
1339 fs_inst
*inst
= NULL
;
1340 unsigned int mlen
= 0;
1342 ir
->coordinate
->accept(this);
1343 fs_reg coordinate
= this->result
;
1345 /* Should be lowered by do_lower_texture_projection */
1346 assert(!ir
->projector
);
1348 for (mlen
= 0; mlen
< ir
->coordinate
->type
->vector_elements
; mlen
++) {
1349 emit(fs_inst(BRW_OPCODE_MOV
, fs_reg(MRF
, base_mrf
+ mlen
), coordinate
));
1350 coordinate
.reg_offset
++;
1353 /* Writemasking doesn't eliminate channels on SIMD8 texture
1354 * samples, so don't worry about them.
1356 fs_reg dst
= fs_reg(this, glsl_type::vec4_type
);
1358 if (intel
->gen
< 5) {
1359 inst
= emit_texture_gen4(ir
, dst
, base_mrf
);
1361 inst
= emit_texture_gen5(ir
, dst
, base_mrf
);
1365 _mesa_get_sampler_uniform_value(ir
->sampler
,
1366 ctx
->Shader
.CurrentProgram
,
1367 &brw
->fragment_program
->Base
);
1368 inst
->sampler
= c
->fp
->program
.Base
.SamplerUnits
[inst
->sampler
];
1372 if (ir
->shadow_comparitor
)
1373 inst
->shadow_compare
= true;
1377 fs_visitor::visit(ir_swizzle
*ir
)
1379 ir
->val
->accept(this);
1380 fs_reg val
= this->result
;
1382 fs_reg result
= fs_reg(this, ir
->type
);
1383 this->result
= result
;
1385 for (unsigned int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1386 fs_reg channel
= val
;
1404 channel
.reg_offset
+= swiz
;
1405 emit(fs_inst(BRW_OPCODE_MOV
, result
, channel
));
1406 result
.reg_offset
++;
1411 fs_visitor::visit(ir_discard
*ir
)
1413 fs_reg temp
= fs_reg(this, glsl_type::uint_type
);
1415 assert(ir
->condition
== NULL
); /* FINISHME */
1417 emit(fs_inst(FS_OPCODE_DISCARD
, temp
, temp
));
1421 fs_visitor::visit(ir_constant
*ir
)
1423 fs_reg
reg(this, ir
->type
);
1426 for (unsigned int i
= 0; i
< ir
->type
->vector_elements
; i
++) {
1427 switch (ir
->type
->base_type
) {
1428 case GLSL_TYPE_FLOAT
:
1429 emit(fs_inst(BRW_OPCODE_MOV
, reg
, fs_reg(ir
->value
.f
[i
])));
1431 case GLSL_TYPE_UINT
:
1432 emit(fs_inst(BRW_OPCODE_MOV
, reg
, fs_reg(ir
->value
.u
[i
])));
1435 emit(fs_inst(BRW_OPCODE_MOV
, reg
, fs_reg(ir
->value
.i
[i
])));
1437 case GLSL_TYPE_BOOL
:
1438 emit(fs_inst(BRW_OPCODE_MOV
, reg
, fs_reg((int)ir
->value
.b
[i
])));
1441 assert(!"Non-float/uint/int/bool constant");
1448 fs_visitor::visit(ir_if
*ir
)
1452 /* Don't point the annotation at the if statement, because then it plus
1453 * the then and else blocks get printed.
1455 this->base_ir
= ir
->condition
;
1457 /* Generate the condition into the condition code. */
1458 ir
->condition
->accept(this);
1459 inst
= emit(fs_inst(BRW_OPCODE_MOV
, fs_reg(brw_null_reg()), this->result
));
1460 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1462 inst
= emit(fs_inst(BRW_OPCODE_IF
));
1463 inst
->predicated
= true;
1465 foreach_iter(exec_list_iterator
, iter
, ir
->then_instructions
) {
1466 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
1472 if (!ir
->else_instructions
.is_empty()) {
1473 emit(fs_inst(BRW_OPCODE_ELSE
));
1475 foreach_iter(exec_list_iterator
, iter
, ir
->else_instructions
) {
1476 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
1483 emit(fs_inst(BRW_OPCODE_ENDIF
));
1487 fs_visitor::visit(ir_loop
*ir
)
1489 fs_reg counter
= reg_undef
;
1492 this->base_ir
= ir
->counter
;
1493 ir
->counter
->accept(this);
1494 counter
= *(variable_storage(ir
->counter
));
1497 this->base_ir
= ir
->from
;
1498 ir
->from
->accept(this);
1500 emit(fs_inst(BRW_OPCODE_MOV
, counter
, this->result
));
1504 emit(fs_inst(BRW_OPCODE_DO
));
1507 this->base_ir
= ir
->to
;
1508 ir
->to
->accept(this);
1510 fs_inst
*inst
= emit(fs_inst(BRW_OPCODE_CMP
, reg_null
,
1511 counter
, this->result
));
1513 case ir_binop_equal
:
1514 inst
->conditional_mod
= BRW_CONDITIONAL_Z
;
1516 case ir_binop_nequal
:
1517 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1519 case ir_binop_gequal
:
1520 inst
->conditional_mod
= BRW_CONDITIONAL_GE
;
1522 case ir_binop_lequal
:
1523 inst
->conditional_mod
= BRW_CONDITIONAL_LE
;
1525 case ir_binop_greater
:
1526 inst
->conditional_mod
= BRW_CONDITIONAL_G
;
1529 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
1532 assert(!"not reached: unknown loop condition");
1537 inst
= emit(fs_inst(BRW_OPCODE_BREAK
));
1538 inst
->predicated
= true;
1541 foreach_iter(exec_list_iterator
, iter
, ir
->body_instructions
) {
1542 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
1548 if (ir
->increment
) {
1549 this->base_ir
= ir
->increment
;
1550 ir
->increment
->accept(this);
1551 emit(fs_inst(BRW_OPCODE_ADD
, counter
, counter
, this->result
));
1554 emit(fs_inst(BRW_OPCODE_WHILE
));
1558 fs_visitor::visit(ir_loop_jump
*ir
)
1561 case ir_loop_jump::jump_break
:
1562 emit(fs_inst(BRW_OPCODE_BREAK
));
1564 case ir_loop_jump::jump_continue
:
1565 emit(fs_inst(BRW_OPCODE_CONTINUE
));
1571 fs_visitor::visit(ir_call
*ir
)
1573 assert(!"FINISHME");
1577 fs_visitor::visit(ir_return
*ir
)
1579 assert(!"FINISHME");
1583 fs_visitor::visit(ir_function
*ir
)
1585 /* Ignore function bodies other than main() -- we shouldn't see calls to
1586 * them since they should all be inlined before we get to ir_to_mesa.
1588 if (strcmp(ir
->name
, "main") == 0) {
1589 const ir_function_signature
*sig
;
1592 sig
= ir
->matching_signature(&empty
);
1596 foreach_iter(exec_list_iterator
, iter
, sig
->body
) {
1597 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
1606 fs_visitor::visit(ir_function_signature
*ir
)
1608 assert(!"not reached");
1613 fs_visitor::emit(fs_inst inst
)
1615 fs_inst
*list_inst
= new(mem_ctx
) fs_inst
;
1618 list_inst
->annotation
= this->current_annotation
;
1619 list_inst
->ir
= this->base_ir
;
1621 this->instructions
.push_tail(list_inst
);
1626 /** Emits a dummy fragment shader consisting of magenta for bringup purposes. */
1628 fs_visitor::emit_dummy_fs()
1630 /* Everyone's favorite color. */
1631 emit(fs_inst(BRW_OPCODE_MOV
,
1634 emit(fs_inst(BRW_OPCODE_MOV
,
1637 emit(fs_inst(BRW_OPCODE_MOV
,
1640 emit(fs_inst(BRW_OPCODE_MOV
,
1645 write
= emit(fs_inst(FS_OPCODE_FB_WRITE
,
1650 /* The register location here is relative to the start of the URB
1651 * data. It will get adjusted to be a real location before
1652 * generate_code() time.
1655 fs_visitor::interp_reg(int location
, int channel
)
1657 int regnr
= location
* 2 + channel
/ 2;
1658 int stride
= (channel
& 1) * 4;
1660 return brw_vec1_grf(regnr
, stride
);
1663 /** Emits the interpolation for the varying inputs. */
1665 fs_visitor::emit_interpolation_setup()
1667 struct brw_reg g1_uw
= retype(brw_vec1_grf(1, 0), BRW_REGISTER_TYPE_UW
);
1669 this->current_annotation
= "compute pixel centers";
1670 this->pixel_x
= fs_reg(this, glsl_type::uint_type
);
1671 this->pixel_y
= fs_reg(this, glsl_type::uint_type
);
1672 this->pixel_x
.type
= BRW_REGISTER_TYPE_UW
;
1673 this->pixel_y
.type
= BRW_REGISTER_TYPE_UW
;
1674 emit(fs_inst(BRW_OPCODE_ADD
,
1676 fs_reg(stride(suboffset(g1_uw
, 4), 2, 4, 0)),
1677 fs_reg(brw_imm_v(0x10101010))));
1678 emit(fs_inst(BRW_OPCODE_ADD
,
1680 fs_reg(stride(suboffset(g1_uw
, 5), 2, 4, 0)),
1681 fs_reg(brw_imm_v(0x11001100))));
1683 this->current_annotation
= "compute pixel deltas from v0";
1684 this->delta_x
= fs_reg(this, glsl_type::float_type
);
1685 this->delta_y
= fs_reg(this, glsl_type::float_type
);
1686 emit(fs_inst(BRW_OPCODE_ADD
,
1689 fs_reg(negate(brw_vec1_grf(1, 0)))));
1690 emit(fs_inst(BRW_OPCODE_ADD
,
1693 fs_reg(negate(brw_vec1_grf(1, 1)))));
1695 this->current_annotation
= "compute pos.w and 1/pos.w";
1696 /* Compute wpos.w. It's always in our setup, since it's needed to
1697 * interpolate the other attributes.
1699 this->wpos_w
= fs_reg(this, glsl_type::float_type
);
1700 emit(fs_inst(FS_OPCODE_LINTERP
, wpos_w
, this->delta_x
, this->delta_y
,
1701 interp_reg(FRAG_ATTRIB_WPOS
, 3)));
1702 /* Compute the pixel 1/W value from wpos.w. */
1703 this->pixel_w
= fs_reg(this, glsl_type::float_type
);
1704 emit(fs_inst(FS_OPCODE_RCP
, this->pixel_w
, wpos_w
));
1705 this->current_annotation
= NULL
;
1709 fs_visitor::emit_fb_writes()
1711 this->current_annotation
= "FB write header";
1717 if (c
->key
.aa_dest_stencil_reg
) {
1718 emit(fs_inst(BRW_OPCODE_MOV
, fs_reg(MRF
, nr
++),
1719 fs_reg(brw_vec8_grf(c
->key
.aa_dest_stencil_reg
, 0))));
1722 /* Reserve space for color. It'll be filled in per MRT below. */
1726 if (c
->key
.source_depth_to_render_target
) {
1727 if (c
->key
.computes_depth
) {
1728 /* Hand over gl_FragDepth. */
1729 assert(this->frag_depth
);
1730 fs_reg depth
= *(variable_storage(this->frag_depth
));
1732 emit(fs_inst(BRW_OPCODE_MOV
, fs_reg(MRF
, nr
++), depth
));
1734 /* Pass through the payload depth. */
1735 emit(fs_inst(BRW_OPCODE_MOV
, fs_reg(MRF
, nr
++),
1736 fs_reg(brw_vec8_grf(c
->key
.source_depth_reg
, 0))));
1740 if (c
->key
.dest_depth_reg
) {
1741 emit(fs_inst(BRW_OPCODE_MOV
, fs_reg(MRF
, nr
++),
1742 fs_reg(brw_vec8_grf(c
->key
.dest_depth_reg
, 0))));
1745 fs_reg color
= reg_undef
;
1746 if (this->frag_color
)
1747 color
= *(variable_storage(this->frag_color
));
1748 else if (this->frag_data
)
1749 color
= *(variable_storage(this->frag_data
));
1751 for (int target
= 0; target
< c
->key
.nr_color_regions
; target
++) {
1752 this->current_annotation
= talloc_asprintf(this->mem_ctx
,
1753 "FB write target %d",
1755 if (this->frag_color
|| this->frag_data
) {
1756 for (int i
= 0; i
< 4; i
++) {
1757 emit(fs_inst(BRW_OPCODE_MOV
,
1758 fs_reg(MRF
, color_mrf
+ i
),
1764 if (this->frag_color
)
1765 color
.reg_offset
-= 4;
1767 fs_inst
*inst
= emit(fs_inst(FS_OPCODE_FB_WRITE
,
1768 reg_undef
, reg_undef
));
1769 inst
->target
= target
;
1771 if (target
== c
->key
.nr_color_regions
- 1)
1775 if (c
->key
.nr_color_regions
== 0) {
1776 fs_inst
*inst
= emit(fs_inst(FS_OPCODE_FB_WRITE
,
1777 reg_undef
, reg_undef
));
1782 this->current_annotation
= NULL
;
1786 fs_visitor::generate_fb_write(fs_inst
*inst
)
1788 GLboolean eot
= inst
->eot
;
1790 /* Header is 2 regs, g0 and g1 are the contents. g0 will be implied
1793 brw_push_insn_state(p
);
1794 brw_set_mask_control(p
, BRW_MASK_DISABLE
);
1795 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
1798 brw_vec8_grf(1, 0));
1799 brw_pop_insn_state(p
);
1802 8, /* dispatch_width */
1803 retype(vec8(brw_null_reg()), BRW_REGISTER_TYPE_UW
),
1805 retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UW
),
1813 fs_visitor::generate_linterp(fs_inst
*inst
,
1814 struct brw_reg dst
, struct brw_reg
*src
)
1816 struct brw_reg delta_x
= src
[0];
1817 struct brw_reg delta_y
= src
[1];
1818 struct brw_reg interp
= src
[2];
1821 delta_y
.nr
== delta_x
.nr
+ 1 &&
1822 (intel
->gen
>= 6 || (delta_x
.nr
& 1) == 0)) {
1823 brw_PLN(p
, dst
, interp
, delta_x
);
1825 brw_LINE(p
, brw_null_reg(), interp
, delta_x
);
1826 brw_MAC(p
, dst
, suboffset(interp
, 1), delta_y
);
1831 fs_visitor::generate_math(fs_inst
*inst
,
1832 struct brw_reg dst
, struct brw_reg
*src
)
1836 switch (inst
->opcode
) {
1838 op
= BRW_MATH_FUNCTION_INV
;
1841 op
= BRW_MATH_FUNCTION_RSQ
;
1843 case FS_OPCODE_SQRT
:
1844 op
= BRW_MATH_FUNCTION_SQRT
;
1846 case FS_OPCODE_EXP2
:
1847 op
= BRW_MATH_FUNCTION_EXP
;
1849 case FS_OPCODE_LOG2
:
1850 op
= BRW_MATH_FUNCTION_LOG
;
1853 op
= BRW_MATH_FUNCTION_POW
;
1856 op
= BRW_MATH_FUNCTION_SIN
;
1859 op
= BRW_MATH_FUNCTION_COS
;
1862 assert(!"not reached: unknown math function");
1867 if (inst
->opcode
== FS_OPCODE_POW
) {
1868 brw_MOV(p
, brw_message_reg(3), src
[1]);
1873 inst
->saturate
? BRW_MATH_SATURATE_SATURATE
:
1874 BRW_MATH_SATURATE_NONE
,
1876 BRW_MATH_DATA_VECTOR
,
1877 BRW_MATH_PRECISION_FULL
);
1881 fs_visitor::generate_tex(fs_inst
*inst
, struct brw_reg dst
, struct brw_reg src
)
1886 if (intel
->gen
== 5) {
1887 switch (inst
->opcode
) {
1889 if (inst
->shadow_compare
) {
1890 msg_type
= BRW_SAMPLER_MESSAGE_SAMPLE_COMPARE_GEN5
;
1892 msg_type
= BRW_SAMPLER_MESSAGE_SAMPLE_GEN5
;
1896 if (inst
->shadow_compare
) {
1897 msg_type
= BRW_SAMPLER_MESSAGE_SAMPLE_BIAS_COMPARE_GEN5
;
1899 msg_type
= BRW_SAMPLER_MESSAGE_SAMPLE_BIAS_GEN5
;
1904 switch (inst
->opcode
) {
1906 /* Note that G45 and older determines shadow compare and dispatch width
1907 * from message length for most messages.
1909 if (inst
->shadow_compare
) {
1910 msg_type
= BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE_COMPARE
;
1912 msg_type
= BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE
;
1915 if (inst
->shadow_compare
) {
1916 assert(!"FINISHME: shadow compare with bias.");
1917 msg_type
= BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE_BIAS
;
1919 msg_type
= BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE_BIAS
;
1925 assert(msg_type
!= -1);
1931 retype(dst
, BRW_REGISTER_TYPE_UW
),
1933 retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UW
),
1934 SURF_INDEX_TEXTURE(inst
->sampler
),
1942 BRW_SAMPLER_SIMD_MODE_SIMD8
);
1946 /* For OPCODE_DDX and OPCODE_DDY, per channel of output we've got input
1949 * arg0: ss0.tl ss0.tr ss0.bl ss0.br ss1.tl ss1.tr ss1.bl ss1.br
1951 * and we're trying to produce:
1954 * dst: (ss0.tr - ss0.tl) (ss0.tl - ss0.bl)
1955 * (ss0.tr - ss0.tl) (ss0.tr - ss0.br)
1956 * (ss0.br - ss0.bl) (ss0.tl - ss0.bl)
1957 * (ss0.br - ss0.bl) (ss0.tr - ss0.br)
1958 * (ss1.tr - ss1.tl) (ss1.tl - ss1.bl)
1959 * (ss1.tr - ss1.tl) (ss1.tr - ss1.br)
1960 * (ss1.br - ss1.bl) (ss1.tl - ss1.bl)
1961 * (ss1.br - ss1.bl) (ss1.tr - ss1.br)
1963 * and add another set of two more subspans if in 16-pixel dispatch mode.
1965 * For DDX, it ends up being easy: width = 2, horiz=0 gets us the same result
1966 * for each pair, and vertstride = 2 jumps us 2 elements after processing a
1967 * pair. But for DDY, it's harder, as we want to produce the pairs swizzled
1968 * between each other. We could probably do it like ddx and swizzle the right
1969 * order later, but bail for now and just produce
1970 * ((ss0.tl - ss0.bl)x4 (ss1.tl - ss1.bl)x4)
1973 fs_visitor::generate_ddx(fs_inst
*inst
, struct brw_reg dst
, struct brw_reg src
)
1975 struct brw_reg src0
= brw_reg(src
.file
, src
.nr
, 1,
1976 BRW_REGISTER_TYPE_F
,
1977 BRW_VERTICAL_STRIDE_2
,
1979 BRW_HORIZONTAL_STRIDE_0
,
1980 BRW_SWIZZLE_XYZW
, WRITEMASK_XYZW
);
1981 struct brw_reg src1
= brw_reg(src
.file
, src
.nr
, 0,
1982 BRW_REGISTER_TYPE_F
,
1983 BRW_VERTICAL_STRIDE_2
,
1985 BRW_HORIZONTAL_STRIDE_0
,
1986 BRW_SWIZZLE_XYZW
, WRITEMASK_XYZW
);
1987 brw_ADD(p
, dst
, src0
, negate(src1
));
1991 fs_visitor::generate_ddy(fs_inst
*inst
, struct brw_reg dst
, struct brw_reg src
)
1993 struct brw_reg src0
= brw_reg(src
.file
, src
.nr
, 0,
1994 BRW_REGISTER_TYPE_F
,
1995 BRW_VERTICAL_STRIDE_4
,
1997 BRW_HORIZONTAL_STRIDE_0
,
1998 BRW_SWIZZLE_XYZW
, WRITEMASK_XYZW
);
1999 struct brw_reg src1
= brw_reg(src
.file
, src
.nr
, 2,
2000 BRW_REGISTER_TYPE_F
,
2001 BRW_VERTICAL_STRIDE_4
,
2003 BRW_HORIZONTAL_STRIDE_0
,
2004 BRW_SWIZZLE_XYZW
, WRITEMASK_XYZW
);
2005 brw_ADD(p
, dst
, src0
, negate(src1
));
2009 fs_visitor::generate_discard(fs_inst
*inst
, struct brw_reg temp
)
2011 struct brw_reg g0
= retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UW
);
2012 temp
= brw_uw1_reg(temp
.file
, temp
.nr
, 0);
2014 brw_push_insn_state(p
);
2015 brw_set_mask_control(p
, BRW_MASK_DISABLE
);
2016 brw_NOT(p
, temp
, brw_mask_reg(1)); /* IMASK */
2017 brw_AND(p
, g0
, temp
, g0
);
2018 brw_pop_insn_state(p
);
2022 fs_visitor::assign_curb_setup()
2024 c
->prog_data
.first_curbe_grf
= c
->key
.nr_payload_regs
;
2025 c
->prog_data
.curb_read_length
= ALIGN(c
->prog_data
.nr_params
, 8) / 8;
2027 if (intel
->gen
== 5 && (c
->prog_data
.first_curbe_grf
+
2028 c
->prog_data
.curb_read_length
) & 1) {
2029 /* Align the start of the interpolation coefficients so that we can use
2030 * the PLN instruction.
2032 c
->prog_data
.first_curbe_grf
++;
2035 /* Map the offsets in the UNIFORM file to fixed HW regs. */
2036 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
2037 fs_inst
*inst
= (fs_inst
*)iter
.get();
2039 for (unsigned int i
= 0; i
< 3; i
++) {
2040 if (inst
->src
[i
].file
== UNIFORM
) {
2041 int constant_nr
= inst
->src
[i
].hw_reg
+ inst
->src
[i
].reg_offset
;
2042 struct brw_reg brw_reg
= brw_vec1_grf(c
->prog_data
.first_curbe_grf
+
2046 inst
->src
[i
].file
= FIXED_HW_REG
;
2047 inst
->src
[i
].fixed_hw_reg
= brw_reg
;
2054 fs_visitor::assign_urb_setup()
2056 int urb_start
= c
->prog_data
.first_curbe_grf
+ c
->prog_data
.curb_read_length
;
2057 int interp_reg_nr
[FRAG_ATTRIB_MAX
];
2059 c
->prog_data
.urb_read_length
= 0;
2061 /* Figure out where each of the incoming setup attributes lands. */
2062 for (unsigned int i
= 0; i
< FRAG_ATTRIB_MAX
; i
++) {
2063 interp_reg_nr
[i
] = -1;
2065 if (i
!= FRAG_ATTRIB_WPOS
&&
2066 !(brw
->fragment_program
->Base
.InputsRead
& BITFIELD64_BIT(i
)))
2069 /* Each attribute is 4 setup channels, each of which is half a reg. */
2070 interp_reg_nr
[i
] = urb_start
+ c
->prog_data
.urb_read_length
;
2071 c
->prog_data
.urb_read_length
+= 2;
2074 /* Map the register numbers for FS_OPCODE_LINTERP so that it uses
2075 * the correct setup input.
2077 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
2078 fs_inst
*inst
= (fs_inst
*)iter
.get();
2080 if (inst
->opcode
!= FS_OPCODE_LINTERP
)
2083 assert(inst
->src
[2].file
== FIXED_HW_REG
);
2085 int location
= inst
->src
[2].fixed_hw_reg
.nr
/ 2;
2086 assert(interp_reg_nr
[location
] != -1);
2087 inst
->src
[2].fixed_hw_reg
.nr
= (interp_reg_nr
[location
] +
2088 (inst
->src
[2].fixed_hw_reg
.nr
& 1));
2091 this->first_non_payload_grf
= urb_start
+ c
->prog_data
.urb_read_length
;
2095 assign_reg(int *reg_hw_locations
, fs_reg
*reg
)
2097 if (reg
->file
== GRF
&& reg
->reg
!= 0) {
2098 reg
->hw_reg
= reg_hw_locations
[reg
->reg
] + reg
->reg_offset
;
2104 fs_visitor::assign_regs_trivial()
2107 int hw_reg_mapping
[this->virtual_grf_next
];
2110 hw_reg_mapping
[0] = 0;
2111 hw_reg_mapping
[1] = this->first_non_payload_grf
;
2112 for (i
= 2; i
< this->virtual_grf_next
; i
++) {
2113 hw_reg_mapping
[i
] = (hw_reg_mapping
[i
- 1] +
2114 this->virtual_grf_sizes
[i
- 1]);
2116 last_grf
= hw_reg_mapping
[i
- 1] + this->virtual_grf_sizes
[i
- 1];
2118 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
2119 fs_inst
*inst
= (fs_inst
*)iter
.get();
2121 assign_reg(hw_reg_mapping
, &inst
->dst
);
2122 assign_reg(hw_reg_mapping
, &inst
->src
[0]);
2123 assign_reg(hw_reg_mapping
, &inst
->src
[1]);
2126 this->grf_used
= last_grf
+ 1;
2130 fs_visitor::assign_regs()
2133 int hw_reg_mapping
[this->virtual_grf_next
+ 1];
2134 int base_reg_count
= BRW_MAX_GRF
- this->first_non_payload_grf
;
2135 int class_sizes
[base_reg_count
];
2136 int class_count
= 0;
2138 calculate_live_intervals();
2140 /* Set up the register classes.
2142 * The base registers store a scalar value. For texture samples,
2143 * we get virtual GRFs composed of 4 contiguous hw register. For
2144 * structures and arrays, we store them as contiguous larger things
2145 * than that, though we should be able to do better most of the
2148 class_sizes
[class_count
++] = 1;
2149 for (int r
= 1; r
< this->virtual_grf_next
; r
++) {
2152 for (i
= 0; i
< class_count
; i
++) {
2153 if (class_sizes
[i
] == this->virtual_grf_sizes
[r
])
2156 if (i
== class_count
) {
2157 class_sizes
[class_count
++] = this->virtual_grf_sizes
[r
];
2161 int ra_reg_count
= 0;
2162 int class_base_reg
[class_count
];
2163 int class_reg_count
[class_count
];
2164 int classes
[class_count
];
2166 for (int i
= 0; i
< class_count
; i
++) {
2167 class_base_reg
[i
] = ra_reg_count
;
2168 class_reg_count
[i
] = base_reg_count
- (class_sizes
[i
] - 1);
2169 ra_reg_count
+= class_reg_count
[i
];
2172 struct ra_regs
*regs
= ra_alloc_reg_set(ra_reg_count
);
2173 for (int i
= 0; i
< class_count
; i
++) {
2174 classes
[i
] = ra_alloc_reg_class(regs
);
2176 for (int i_r
= 0; i_r
< class_reg_count
[i
]; i_r
++) {
2177 ra_class_add_reg(regs
, classes
[i
], class_base_reg
[i
] + i_r
);
2180 /* Add conflicts between our contiguous registers aliasing
2181 * base regs and other register classes' contiguous registers
2182 * that alias base regs, or the base regs themselves for classes[0].
2184 for (int c
= 0; c
<= i
; c
++) {
2185 for (int i_r
= 0; i_r
< class_reg_count
[i
] - 1; i_r
++) {
2186 for (int c_r
= MAX2(0, i_r
- (class_sizes
[c
] - 1));
2187 c_r
<= MIN2(class_reg_count
[c
] - 1, i_r
+ class_sizes
[i
] - 1);
2191 printf("%d/%d conflicts %d/%d\n",
2192 class_sizes
[i
], i_r
,
2193 class_sizes
[c
], c_r
);
2196 ra_add_reg_conflict(regs
,
2197 class_base_reg
[i
] + i_r
,
2198 class_base_reg
[c
] + c_r
);
2204 ra_set_finalize(regs
);
2206 struct ra_graph
*g
= ra_alloc_interference_graph(regs
,
2207 this->virtual_grf_next
);
2208 /* Node 0 is just a placeholder to keep virtual_grf[] mapping 1:1
2211 ra_set_node_class(g
, 0, classes
[0]);
2213 for (int i
= 1; i
< this->virtual_grf_next
; i
++) {
2214 for (int c
= 0; c
< class_count
; c
++) {
2215 if (class_sizes
[c
] == this->virtual_grf_sizes
[i
]) {
2216 ra_set_node_class(g
, i
, classes
[c
]);
2221 for (int j
= 1; j
< i
; j
++) {
2222 if (virtual_grf_interferes(i
, j
)) {
2223 ra_add_node_interference(g
, i
, j
);
2228 /* FINISHME: Handle spilling */
2229 if (!ra_allocate_no_spills(g
)) {
2230 fprintf(stderr
, "Failed to allocate registers.\n");
2235 /* Get the chosen virtual registers for each node, and map virtual
2236 * regs in the register classes back down to real hardware reg
2239 hw_reg_mapping
[0] = 0; /* unused */
2240 for (int i
= 1; i
< this->virtual_grf_next
; i
++) {
2241 int reg
= ra_get_node_reg(g
, i
);
2244 for (int c
= 0; c
< class_count
; c
++) {
2245 if (reg
>= class_base_reg
[c
] &&
2246 reg
< class_base_reg
[c
] + class_reg_count
[c
] - 1) {
2247 hw_reg
= reg
- class_base_reg
[c
];
2252 assert(hw_reg
!= -1);
2253 hw_reg_mapping
[i
] = this->first_non_payload_grf
+ hw_reg
;
2254 last_grf
= MAX2(last_grf
,
2255 hw_reg_mapping
[i
] + this->virtual_grf_sizes
[i
] - 1);
2258 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
2259 fs_inst
*inst
= (fs_inst
*)iter
.get();
2261 assign_reg(hw_reg_mapping
, &inst
->dst
);
2262 assign_reg(hw_reg_mapping
, &inst
->src
[0]);
2263 assign_reg(hw_reg_mapping
, &inst
->src
[1]);
2266 this->grf_used
= last_grf
+ 1;
2273 fs_visitor::calculate_live_intervals()
2275 int num_vars
= this->virtual_grf_next
;
2276 int *def
= talloc_array(mem_ctx
, int, num_vars
);
2277 int *use
= talloc_array(mem_ctx
, int, num_vars
);
2281 for (int i
= 0; i
< num_vars
; i
++) {
2287 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
2288 fs_inst
*inst
= (fs_inst
*)iter
.get();
2290 if (inst
->opcode
== BRW_OPCODE_DO
) {
2291 if (loop_depth
++ == 0)
2293 } else if (inst
->opcode
== BRW_OPCODE_WHILE
) {
2296 if (loop_depth
== 0) {
2299 * Patches up any vars marked for use within the loop as
2300 * live until the end. This is conservative, as there
2301 * will often be variables defined and used inside the
2302 * loop but dead at the end of the loop body.
2304 for (int i
= 0; i
< num_vars
; i
++) {
2305 if (use
[i
] == loop_start
) {
2316 for (unsigned int i
= 0; i
< 3; i
++) {
2317 if (inst
->src
[i
].file
== GRF
&& inst
->src
[i
].reg
!= 0) {
2318 def
[inst
->src
[i
].reg
] = MIN2(def
[inst
->src
[i
].reg
], eip
);
2319 use
[inst
->src
[i
].reg
] = MAX2(use
[inst
->src
[i
].reg
], eip
);
2322 if (inst
->dst
.file
== GRF
&& inst
->dst
.reg
!= 0) {
2323 def
[inst
->dst
.reg
] = MIN2(def
[inst
->dst
.reg
], eip
);
2324 use
[inst
->dst
.reg
] = MAX2(use
[inst
->dst
.reg
], eip
);
2331 this->virtual_grf_def
= def
;
2332 this->virtual_grf_use
= use
;
2336 fs_visitor::virtual_grf_interferes(int a
, int b
)
2338 int start
= MAX2(this->virtual_grf_def
[a
], this->virtual_grf_def
[b
]);
2339 int end
= MIN2(this->virtual_grf_use
[a
], this->virtual_grf_use
[b
]);
2341 return start
<= end
;
2344 static struct brw_reg
brw_reg_from_fs_reg(fs_reg
*reg
)
2346 struct brw_reg brw_reg
;
2348 switch (reg
->file
) {
2352 brw_reg
= brw_vec8_reg(reg
->file
,
2354 brw_reg
= retype(brw_reg
, reg
->type
);
2357 switch (reg
->type
) {
2358 case BRW_REGISTER_TYPE_F
:
2359 brw_reg
= brw_imm_f(reg
->imm
.f
);
2361 case BRW_REGISTER_TYPE_D
:
2362 brw_reg
= brw_imm_d(reg
->imm
.i
);
2364 case BRW_REGISTER_TYPE_UD
:
2365 brw_reg
= brw_imm_ud(reg
->imm
.u
);
2368 assert(!"not reached");
2373 brw_reg
= reg
->fixed_hw_reg
;
2376 /* Probably unused. */
2377 brw_reg
= brw_null_reg();
2380 assert(!"not reached");
2381 brw_reg
= brw_null_reg();
2385 brw_reg
= brw_abs(brw_reg
);
2387 brw_reg
= negate(brw_reg
);
2393 fs_visitor::generate_code()
2395 unsigned int annotation_len
= 0;
2396 int last_native_inst
= 0;
2397 struct brw_instruction
*if_stack
[16], *loop_stack
[16];
2398 int if_stack_depth
= 0, loop_stack_depth
= 0;
2399 int if_depth_in_loop
[16];
2401 if_depth_in_loop
[loop_stack_depth
] = 0;
2403 memset(&if_stack
, 0, sizeof(if_stack
));
2404 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
2405 fs_inst
*inst
= (fs_inst
*)iter
.get();
2406 struct brw_reg src
[3], dst
;
2408 for (unsigned int i
= 0; i
< 3; i
++) {
2409 src
[i
] = brw_reg_from_fs_reg(&inst
->src
[i
]);
2411 dst
= brw_reg_from_fs_reg(&inst
->dst
);
2413 brw_set_conditionalmod(p
, inst
->conditional_mod
);
2414 brw_set_predicate_control(p
, inst
->predicated
);
2416 switch (inst
->opcode
) {
2417 case BRW_OPCODE_MOV
:
2418 brw_MOV(p
, dst
, src
[0]);
2420 case BRW_OPCODE_ADD
:
2421 brw_ADD(p
, dst
, src
[0], src
[1]);
2423 case BRW_OPCODE_MUL
:
2424 brw_MUL(p
, dst
, src
[0], src
[1]);
2427 case BRW_OPCODE_FRC
:
2428 brw_FRC(p
, dst
, src
[0]);
2430 case BRW_OPCODE_RNDD
:
2431 brw_RNDD(p
, dst
, src
[0]);
2433 case BRW_OPCODE_RNDZ
:
2434 brw_RNDZ(p
, dst
, src
[0]);
2437 case BRW_OPCODE_AND
:
2438 brw_AND(p
, dst
, src
[0], src
[1]);
2441 brw_OR(p
, dst
, src
[0], src
[1]);
2443 case BRW_OPCODE_XOR
:
2444 brw_XOR(p
, dst
, src
[0], src
[1]);
2447 case BRW_OPCODE_CMP
:
2448 brw_CMP(p
, dst
, inst
->conditional_mod
, src
[0], src
[1]);
2450 case BRW_OPCODE_SEL
:
2451 brw_SEL(p
, dst
, src
[0], src
[1]);
2455 assert(if_stack_depth
< 16);
2456 if_stack
[if_stack_depth
] = brw_IF(p
, BRW_EXECUTE_8
);
2457 if_depth_in_loop
[loop_stack_depth
]++;
2460 case BRW_OPCODE_ELSE
:
2461 if_stack
[if_stack_depth
- 1] =
2462 brw_ELSE(p
, if_stack
[if_stack_depth
- 1]);
2464 case BRW_OPCODE_ENDIF
:
2466 brw_ENDIF(p
, if_stack
[if_stack_depth
]);
2467 if_depth_in_loop
[loop_stack_depth
]--;
2471 loop_stack
[loop_stack_depth
++] = brw_DO(p
, BRW_EXECUTE_8
);
2472 if_depth_in_loop
[loop_stack_depth
] = 0;
2475 case BRW_OPCODE_BREAK
:
2476 brw_BREAK(p
, if_depth_in_loop
[loop_stack_depth
]);
2477 brw_set_predicate_control(p
, BRW_PREDICATE_NONE
);
2479 case BRW_OPCODE_CONTINUE
:
2480 brw_CONT(p
, if_depth_in_loop
[loop_stack_depth
]);
2481 brw_set_predicate_control(p
, BRW_PREDICATE_NONE
);
2484 case BRW_OPCODE_WHILE
: {
2485 struct brw_instruction
*inst0
, *inst1
;
2488 if (intel
->gen
== 5)
2491 assert(loop_stack_depth
> 0);
2493 inst0
= inst1
= brw_WHILE(p
, loop_stack
[loop_stack_depth
]);
2494 /* patch all the BREAK/CONT instructions from last BGNLOOP */
2495 while (inst0
> loop_stack
[loop_stack_depth
]) {
2497 if (inst0
->header
.opcode
== BRW_OPCODE_BREAK
&&
2498 inst0
->bits3
.if_else
.jump_count
== 0) {
2499 inst0
->bits3
.if_else
.jump_count
= br
* (inst1
- inst0
+ 1);
2501 else if (inst0
->header
.opcode
== BRW_OPCODE_CONTINUE
&&
2502 inst0
->bits3
.if_else
.jump_count
== 0) {
2503 inst0
->bits3
.if_else
.jump_count
= br
* (inst1
- inst0
);
2511 case FS_OPCODE_SQRT
:
2512 case FS_OPCODE_EXP2
:
2513 case FS_OPCODE_LOG2
:
2517 generate_math(inst
, dst
, src
);
2519 case FS_OPCODE_LINTERP
:
2520 generate_linterp(inst
, dst
, src
);
2525 generate_tex(inst
, dst
, src
[0]);
2527 case FS_OPCODE_DISCARD
:
2528 generate_discard(inst
, dst
/* src0 == dst */);
2531 generate_ddx(inst
, dst
, src
[0]);
2534 generate_ddy(inst
, dst
, src
[0]);
2536 case FS_OPCODE_FB_WRITE
:
2537 generate_fb_write(inst
);
2540 if (inst
->opcode
< (int)ARRAY_SIZE(brw_opcodes
)) {
2541 _mesa_problem(ctx
, "Unsupported opcode `%s' in FS",
2542 brw_opcodes
[inst
->opcode
].name
);
2544 _mesa_problem(ctx
, "Unsupported opcode %d in FS", inst
->opcode
);
2549 if (annotation_len
< p
->nr_insn
) {
2550 annotation_len
*= 2;
2551 if (annotation_len
< 16)
2552 annotation_len
= 16;
2554 this->annotation_string
= talloc_realloc(this->mem_ctx
,
2558 this->annotation_ir
= talloc_realloc(this->mem_ctx
,
2564 for (unsigned int i
= last_native_inst
; i
< p
->nr_insn
; i
++) {
2565 this->annotation_string
[i
] = inst
->annotation
;
2566 this->annotation_ir
[i
] = inst
->ir
;
2568 last_native_inst
= p
->nr_insn
;
2573 brw_wm_fs_emit(struct brw_context
*brw
, struct brw_wm_compile
*c
)
2575 struct brw_compile
*p
= &c
->func
;
2576 struct intel_context
*intel
= &brw
->intel
;
2577 GLcontext
*ctx
= &intel
->ctx
;
2578 struct brw_shader
*shader
= NULL
;
2579 struct gl_shader_program
*prog
= ctx
->Shader
.CurrentProgram
;
2587 for (unsigned int i
= 0; i
< prog
->_NumLinkedShaders
; i
++) {
2588 if (prog
->_LinkedShaders
[i
]->Type
== GL_FRAGMENT_SHADER
) {
2589 shader
= (struct brw_shader
*)prog
->_LinkedShaders
[i
];
2596 /* We always use 8-wide mode, at least for now. For one, flow
2597 * control only works in 8-wide. Also, when we're fragment shader
2598 * bound, we're almost always under register pressure as well, so
2599 * 8-wide would save us from the performance cliff of spilling
2602 c
->dispatch_width
= 8;
2604 if (INTEL_DEBUG
& DEBUG_WM
) {
2605 printf("GLSL IR for native fragment shader %d:\n", prog
->Name
);
2606 _mesa_print_ir(shader
->ir
, NULL
);
2610 /* Now the main event: Visit the shader IR and generate our FS IR for it.
2612 fs_visitor
v(c
, shader
);
2617 v
.emit_interpolation_setup();
2619 /* Generate FS IR for main(). (the visitor only descends into
2620 * functions called "main").
2622 foreach_iter(exec_list_iterator
, iter
, *shader
->ir
) {
2623 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
2629 v
.assign_curb_setup();
2630 v
.assign_urb_setup();
2632 v
.assign_regs_trivial();
2639 assert(!v
.fail
); /* FINISHME: Cleanly fail, tested at link time, etc. */
2644 if (INTEL_DEBUG
& DEBUG_WM
) {
2645 const char *last_annotation_string
= NULL
;
2646 ir_instruction
*last_annotation_ir
= NULL
;
2648 printf("Native code for fragment shader %d:\n", prog
->Name
);
2649 for (unsigned int i
= 0; i
< p
->nr_insn
; i
++) {
2650 if (last_annotation_ir
!= v
.annotation_ir
[i
]) {
2651 last_annotation_ir
= v
.annotation_ir
[i
];
2652 if (last_annotation_ir
) {
2654 last_annotation_ir
->print();
2658 if (last_annotation_string
!= v
.annotation_string
[i
]) {
2659 last_annotation_string
= v
.annotation_string
[i
];
2660 if (last_annotation_string
)
2661 printf(" %s\n", last_annotation_string
);
2663 brw_disasm(stdout
, &p
->store
[i
], intel
->gen
);
2668 c
->prog_data
.total_grf
= v
.grf_used
;
2669 c
->prog_data
.total_scratch
= 0;