2 * Copyright © 2011 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
28 #include "brw_shader.h"
29 #include "main/compiler.h"
30 #include "program/hash_table.h"
31 #include "brw_program.h"
37 #include "brw_context.h"
39 #include "intel_asm_annotation.h"
48 struct brw_vec4_compile
{
49 GLuint last_scratch
; /**< measured in 32-byte (register size) units */
57 brw_vue_setup_prog_key_for_precompile(struct gl_context
*ctx
,
58 struct brw_vue_prog_key
*key
,
59 GLuint id
, struct gl_program
*prog
);
68 class vec4_live_variables
;
71 swizzle_for_size(int size
);
73 class src_reg
: public backend_reg
76 DECLARE_RALLOC_CXX_OPERATORS(src_reg
)
80 src_reg(register_file file
, int reg
, const glsl_type
*type
);
85 src_reg(uint8_t vf0
, uint8_t vf1
, uint8_t vf2
, uint8_t vf3
);
86 src_reg(struct brw_reg reg
);
88 bool equals(const src_reg
&r
) const;
90 src_reg(class vec4_visitor
*v
, const struct glsl_type
*type
);
91 src_reg(class vec4_visitor
*v
, const struct glsl_type
*type
, int size
);
93 explicit src_reg(dst_reg reg
);
95 GLuint swizzle
; /**< BRW_SWIZZLE_XYZW macros from brw_reg.h. */
100 static inline src_reg
101 retype(src_reg reg
, enum brw_reg_type type
)
103 reg
.fixed_hw_reg
.type
= reg
.type
= type
;
107 static inline src_reg
108 offset(src_reg reg
, unsigned delta
)
110 assert(delta
== 0 || (reg
.file
!= HW_REG
&& reg
.file
!= IMM
));
111 reg
.reg_offset
+= delta
;
116 * Reswizzle a given source register.
119 static inline src_reg
120 swizzle(src_reg reg
, unsigned swizzle
)
122 assert(reg
.file
!= HW_REG
);
123 reg
.swizzle
= BRW_SWIZZLE4(
124 BRW_GET_SWZ(reg
.swizzle
, BRW_GET_SWZ(swizzle
, 0)),
125 BRW_GET_SWZ(reg
.swizzle
, BRW_GET_SWZ(swizzle
, 1)),
126 BRW_GET_SWZ(reg
.swizzle
, BRW_GET_SWZ(swizzle
, 2)),
127 BRW_GET_SWZ(reg
.swizzle
, BRW_GET_SWZ(swizzle
, 3)));
131 static inline src_reg
134 assert(reg
.file
!= HW_REG
&& reg
.file
!= IMM
);
135 reg
.negate
= !reg
.negate
;
139 class dst_reg
: public backend_reg
142 DECLARE_RALLOC_CXX_OPERATORS(dst_reg
)
147 dst_reg(register_file file
, int reg
);
148 dst_reg(register_file file
, int reg
, const glsl_type
*type
, int writemask
);
149 dst_reg(struct brw_reg reg
);
150 dst_reg(class vec4_visitor
*v
, const struct glsl_type
*type
);
152 explicit dst_reg(src_reg reg
);
154 int writemask
; /**< Bitfield of WRITEMASK_[XYZW] */
159 static inline dst_reg
160 retype(dst_reg reg
, enum brw_reg_type type
)
162 reg
.fixed_hw_reg
.type
= reg
.type
= type
;
166 static inline dst_reg
167 offset(dst_reg reg
, unsigned delta
)
169 assert(delta
== 0 || (reg
.file
!= HW_REG
&& reg
.file
!= IMM
));
170 reg
.reg_offset
+= delta
;
174 static inline dst_reg
175 writemask(dst_reg reg
, unsigned mask
)
177 assert(reg
.file
!= HW_REG
&& reg
.file
!= IMM
);
178 assert((reg
.writemask
& mask
) != 0);
179 reg
.writemask
&= mask
;
183 class vec4_instruction
: public backend_instruction
{
185 DECLARE_RALLOC_CXX_OPERATORS(vec4_instruction
)
187 vec4_instruction(vec4_visitor
*v
, enum opcode opcode
,
188 const dst_reg
&dst
= dst_reg(),
189 const src_reg
&src0
= src_reg(),
190 const src_reg
&src1
= src_reg(),
191 const src_reg
&src2
= src_reg());
193 struct brw_reg
get_dst(void);
194 struct brw_reg
get_src(const struct brw_vue_prog_data
*prog_data
, int i
);
199 enum brw_urb_write_flags urb_write_flags
;
201 unsigned sol_binding
; /**< gen6: SOL binding table index */
202 bool sol_final_write
; /**< gen6: send commit message */
203 unsigned sol_vertex
; /**< gen6: used for setting dst index in SVB header */
205 bool is_send_from_grf();
206 bool can_reswizzle(int dst_writemask
, int swizzle
, int swizzle_mask
);
207 void reswizzle(int dst_writemask
, int swizzle
);
208 bool can_do_source_mods(struct brw_context
*brw
);
212 return predicate
|| opcode
== VS_OPCODE_UNPACK_FLAGS_SIMD4X2
;
217 return (conditional_mod
&& (opcode
!= BRW_OPCODE_SEL
&&
218 opcode
!= BRW_OPCODE_IF
&&
219 opcode
!= BRW_OPCODE_WHILE
));
224 * The vertex shader front-end.
226 * Translates either GLSL IR or Mesa IR (for ARB_vertex_program and
227 * fixed-function) into VS IR.
229 class vec4_visitor
: public backend_visitor
232 vec4_visitor(struct brw_context
*brw
,
233 struct brw_vec4_compile
*c
,
234 struct gl_program
*prog
,
235 const struct brw_vue_prog_key
*key
,
236 struct brw_vue_prog_data
*prog_data
,
237 struct gl_shader_program
*shader_prog
,
238 gl_shader_stage stage
,
242 shader_time_shader_type st_base
,
243 shader_time_shader_type st_written
,
244 shader_time_shader_type st_reset
);
249 return dst_reg(brw_null_reg());
254 return dst_reg(retype(brw_null_reg(), BRW_REGISTER_TYPE_D
));
257 dst_reg
dst_null_ud()
259 return dst_reg(retype(brw_null_reg(), BRW_REGISTER_TYPE_UD
));
262 struct brw_vec4_compile
* const c
;
263 const struct brw_vue_prog_key
* const key
;
264 struct brw_vue_prog_data
* const prog_data
;
265 unsigned int sanity_param_count
;
271 * GLSL IR currently being processed, which is associated with our
272 * driver IR instructions for debugging purposes.
275 const char *current_annotation
;
277 int *virtual_grf_sizes
;
278 int virtual_grf_count
;
279 int virtual_grf_array_size
;
280 int first_non_payload_grf
;
281 unsigned int max_grf
;
282 int *virtual_grf_start
;
283 int *virtual_grf_end
;
284 brw::vec4_live_variables
*live_intervals
;
285 dst_reg userplane
[MAX_CLIP_PLANES
];
288 * This is the size to be used for an array with an element per
291 int virtual_grf_reg_count
;
292 /** Per-virtual-grf indices into an array of size virtual_grf_reg_count */
293 int *virtual_grf_reg_map
;
295 dst_reg
*variable_storage(ir_variable
*var
);
297 void reladdr_to_temp(ir_instruction
*ir
, src_reg
*reg
, int *num_reladdr
);
299 bool need_all_constants_in_pull_buffer
;
302 * \name Visit methods
304 * As typical for the visitor pattern, there must be one \c visit method for
305 * each concrete subclass of \c ir_instruction. Virtual base classes within
306 * the hierarchy should not have \c visit methods.
309 virtual void visit(ir_variable
*);
310 virtual void visit(ir_loop
*);
311 virtual void visit(ir_loop_jump
*);
312 virtual void visit(ir_function_signature
*);
313 virtual void visit(ir_function
*);
314 virtual void visit(ir_expression
*);
315 virtual void visit(ir_swizzle
*);
316 virtual void visit(ir_dereference_variable
*);
317 virtual void visit(ir_dereference_array
*);
318 virtual void visit(ir_dereference_record
*);
319 virtual void visit(ir_assignment
*);
320 virtual void visit(ir_constant
*);
321 virtual void visit(ir_call
*);
322 virtual void visit(ir_return
*);
323 virtual void visit(ir_discard
*);
324 virtual void visit(ir_texture
*);
325 virtual void visit(ir_if
*);
326 virtual void visit(ir_emit_vertex
*);
327 virtual void visit(ir_end_primitive
*);
332 /* Regs for vertex results. Generated at ir_variable visiting time
333 * for the ir->location's used.
335 dst_reg output_reg
[BRW_VARYING_SLOT_COUNT
];
336 const char *output_reg_annotation
[BRW_VARYING_SLOT_COUNT
];
338 int *uniform_vector_size
;
339 int uniform_array_size
; /*< Size of uniform_[vector_]size arrays */
342 src_reg shader_start_time
;
344 struct hash_table
*variable_ht
;
347 void fail(const char *msg
, ...);
349 int virtual_grf_alloc(int size
);
350 void setup_uniform_clipplane_values();
351 void setup_uniform_values(ir_variable
*ir
);
352 void setup_builtin_uniform_values(ir_variable
*ir
);
353 int setup_uniforms(int payload_reg
);
354 bool reg_allocate_trivial();
356 void evaluate_spill_costs(float *spill_costs
, bool *no_spill
);
357 int choose_spill_reg(struct ra_graph
*g
);
358 void spill_reg(int spill_reg
);
359 void move_grf_array_access_to_scratch();
360 void move_uniform_array_access_to_pull_constants();
361 void move_push_constants_to_pull_constants();
362 void split_uniform_registers();
363 void pack_uniform_registers();
364 void calculate_live_intervals();
365 void invalidate_live_intervals();
366 void split_virtual_grfs();
367 bool opt_reduce_swizzle();
368 bool dead_code_eliminate();
369 bool virtual_grf_interferes(int a
, int b
);
370 bool opt_copy_propagation();
371 bool opt_cse_local(bblock_t
*block
);
373 bool opt_algebraic();
374 bool opt_register_coalesce();
375 bool is_dep_ctrl_unsafe(const vec4_instruction
*inst
);
376 void opt_set_dependency_control();
377 void opt_schedule_instructions();
379 vec4_instruction
*emit(vec4_instruction
*inst
);
381 vec4_instruction
*emit(enum opcode opcode
);
382 vec4_instruction
*emit(enum opcode opcode
, const dst_reg
&dst
);
383 vec4_instruction
*emit(enum opcode opcode
, const dst_reg
&dst
,
384 const src_reg
&src0
);
385 vec4_instruction
*emit(enum opcode opcode
, const dst_reg
&dst
,
386 const src_reg
&src0
, const src_reg
&src1
);
387 vec4_instruction
*emit(enum opcode opcode
, const dst_reg
&dst
,
388 const src_reg
&src0
, const src_reg
&src1
,
389 const src_reg
&src2
);
391 vec4_instruction
*emit_before(bblock_t
*block
,
392 vec4_instruction
*inst
,
393 vec4_instruction
*new_inst
);
395 #define EMIT1(op) vec4_instruction *op(const dst_reg &, const src_reg &);
396 #define EMIT2(op) vec4_instruction *op(const dst_reg &, const src_reg &, const src_reg &);
397 #define EMIT3(op) vec4_instruction *op(const dst_reg &, const src_reg &, const src_reg &, const src_reg &);
419 vec4_instruction
*CMP(dst_reg dst
, src_reg src0
, src_reg src1
,
420 enum brw_conditional_mod condition
);
421 vec4_instruction
*IF(src_reg src0
, src_reg src1
,
422 enum brw_conditional_mod condition
);
423 vec4_instruction
*IF(enum brw_predicate predicate
);
424 EMIT1(PULL_CONSTANT_LOAD
)
442 int implied_mrf_writes(vec4_instruction
*inst
);
444 bool try_rewrite_rhs_to_dst(ir_assignment
*ir
,
447 vec4_instruction
*pre_rhs_inst
,
448 vec4_instruction
*last_rhs_inst
);
450 /** Walks an exec_list of ir_instruction and sends it through this visitor. */
451 void visit_instructions(const exec_list
*list
);
453 void emit_vp_sop(enum brw_conditional_mod condmod
, dst_reg dst
,
454 src_reg src0
, src_reg src1
, src_reg one
);
456 void emit_bool_to_cond_code(ir_rvalue
*ir
, enum brw_predicate
*predicate
);
457 void emit_if_gen6(ir_if
*ir
);
459 void emit_minmax(enum brw_conditional_mod conditionalmod
, dst_reg dst
,
460 src_reg src0
, src_reg src1
);
462 void emit_lrp(const dst_reg
&dst
,
463 const src_reg
&x
, const src_reg
&y
, const src_reg
&a
);
465 void emit_block_move(dst_reg
*dst
, src_reg
*src
,
466 const struct glsl_type
*type
, brw_predicate predicate
);
468 void emit_constant_values(dst_reg
*dst
, ir_constant
*value
);
471 * Emit the correct dot-product instruction for the type of arguments
473 void emit_dp(dst_reg dst
, src_reg src0
, src_reg src1
, unsigned elements
);
475 void emit_scalar(ir_instruction
*ir
, enum prog_opcode op
,
476 dst_reg dst
, src_reg src0
);
478 void emit_scalar(ir_instruction
*ir
, enum prog_opcode op
,
479 dst_reg dst
, src_reg src0
, src_reg src1
);
481 void emit_scs(ir_instruction
*ir
, enum prog_opcode op
,
482 dst_reg dst
, const src_reg
&src
);
484 src_reg
fix_3src_operand(src_reg src
);
486 void emit_math(enum opcode opcode
, const dst_reg
&dst
, const src_reg
&src0
,
487 const src_reg
&src1
= src_reg());
488 src_reg
fix_math_operand(src_reg src
);
490 void emit_pack_half_2x16(dst_reg dst
, src_reg src0
);
491 void emit_unpack_half_2x16(dst_reg dst
, src_reg src0
);
492 void emit_unpack_unorm_4x8(const dst_reg
&dst
, src_reg src0
);
493 void emit_unpack_snorm_4x8(const dst_reg
&dst
, src_reg src0
);
494 void emit_pack_unorm_4x8(const dst_reg
&dst
, const src_reg
&src0
);
495 void emit_pack_snorm_4x8(const dst_reg
&dst
, const src_reg
&src0
);
497 uint32_t gather_channel(ir_texture
*ir
, uint32_t sampler
);
498 src_reg
emit_mcs_fetch(ir_texture
*ir
, src_reg coordinate
, src_reg sampler
);
499 void emit_gen6_gather_wa(uint8_t wa
, dst_reg dst
);
500 void swizzle_result(ir_texture
*ir
, src_reg orig_val
, uint32_t sampler
);
502 void emit_ndc_computation();
503 void emit_psiz_and_flags(dst_reg reg
);
504 void emit_clip_distances(dst_reg reg
, int offset
);
505 vec4_instruction
*emit_generic_urb_slot(dst_reg reg
, int varying
);
506 void emit_urb_slot(dst_reg reg
, int varying
);
508 void emit_shader_time_begin();
509 void emit_shader_time_end();
510 void emit_shader_time_write(enum shader_time_shader_type type
,
513 void emit_untyped_atomic(unsigned atomic_op
, unsigned surf_index
,
514 dst_reg dst
, src_reg offset
, src_reg src0
,
517 void emit_untyped_surface_read(unsigned surf_index
, dst_reg dst
,
520 src_reg
get_scratch_offset(bblock_t
*block
, vec4_instruction
*inst
,
521 src_reg
*reladdr
, int reg_offset
);
522 src_reg
get_pull_constant_offset(bblock_t
*block
, vec4_instruction
*inst
,
523 src_reg
*reladdr
, int reg_offset
);
524 void emit_scratch_read(bblock_t
*block
, vec4_instruction
*inst
,
528 void emit_scratch_write(bblock_t
*block
, vec4_instruction
*inst
,
530 void emit_pull_constant_load(bblock_t
*block
, vec4_instruction
*inst
,
535 bool try_emit_mad(ir_expression
*ir
);
536 bool try_emit_b2f_of_compare(ir_expression
*ir
);
537 void resolve_ud_negate(src_reg
*reg
);
538 void resolve_bool_comparison(ir_rvalue
*rvalue
, src_reg
*reg
);
540 src_reg
get_timestamp();
542 bool process_move_condition(ir_rvalue
*ir
);
544 void dump_instruction(backend_instruction
*inst
);
545 void dump_instruction(backend_instruction
*inst
, FILE *file
);
547 void visit_atomic_counter_intrinsic(ir_call
*ir
);
551 void lower_attributes_to_hw_regs(const int *attribute_map
,
553 void setup_payload_interference(struct ra_graph
*g
, int first_payload_node
,
555 virtual dst_reg
*make_reg_for_system_value(ir_variable
*ir
) = 0;
556 virtual void assign_binding_table_offsets();
557 virtual void setup_payload() = 0;
558 virtual void emit_prolog() = 0;
559 virtual void emit_program_code() = 0;
560 virtual void emit_thread_end() = 0;
561 virtual void emit_urb_write_header(int mrf
) = 0;
562 virtual vec4_instruction
*emit_urb_write_opcode(bool complete
) = 0;
563 virtual int compute_array_stride(ir_dereference_array
*ir
);
565 const bool debug_flag
;
569 * If true, then register allocation should fail instead of spilling.
571 const bool no_spills
;
573 const shader_time_shader_type st_base
;
574 const shader_time_shader_type st_written
;
575 const shader_time_shader_type st_reset
;
580 * The vertex shader code generator.
582 * Translates VS IR to actual i965 assembly code.
587 vec4_generator(struct brw_context
*brw
,
588 struct gl_shader_program
*shader_prog
,
589 struct gl_program
*prog
,
590 struct brw_vue_prog_data
*prog_data
,
595 const unsigned *generate_assembly(const cfg_t
*cfg
, unsigned *asm_size
);
598 void generate_code(const cfg_t
*cfg
);
600 void generate_math1_gen4(vec4_instruction
*inst
,
603 void generate_math2_gen4(vec4_instruction
*inst
,
606 struct brw_reg src1
);
607 void generate_math_gen6(vec4_instruction
*inst
,
610 struct brw_reg src1
);
612 void generate_tex(vec4_instruction
*inst
,
615 struct brw_reg sampler_index
);
617 void generate_vs_urb_write(vec4_instruction
*inst
);
618 void generate_gs_urb_write(vec4_instruction
*inst
);
619 void generate_gs_urb_write_allocate(vec4_instruction
*inst
);
620 void generate_gs_thread_end(vec4_instruction
*inst
);
621 void generate_gs_set_write_offset(struct brw_reg dst
,
623 struct brw_reg src1
);
624 void generate_gs_set_vertex_count(struct brw_reg dst
,
626 void generate_gs_svb_write(vec4_instruction
*inst
,
629 struct brw_reg src1
);
630 void generate_gs_svb_set_destination_index(vec4_instruction
*inst
,
633 void generate_gs_set_dword_2(struct brw_reg dst
, struct brw_reg src
);
634 void generate_gs_prepare_channel_masks(struct brw_reg dst
);
635 void generate_gs_set_channel_masks(struct brw_reg dst
, struct brw_reg src
);
636 void generate_gs_get_instance_id(struct brw_reg dst
);
637 void generate_gs_ff_sync_set_primitives(struct brw_reg dst
,
640 struct brw_reg src2
);
641 void generate_gs_ff_sync(vec4_instruction
*inst
,
644 struct brw_reg src1
);
645 void generate_gs_set_primitive_id(struct brw_reg dst
);
646 void generate_oword_dual_block_offsets(struct brw_reg m1
,
647 struct brw_reg index
);
648 void generate_scratch_write(vec4_instruction
*inst
,
651 struct brw_reg index
);
652 void generate_scratch_read(vec4_instruction
*inst
,
654 struct brw_reg index
);
655 void generate_pull_constant_load(vec4_instruction
*inst
,
657 struct brw_reg index
,
658 struct brw_reg offset
);
659 void generate_pull_constant_load_gen7(vec4_instruction
*inst
,
661 struct brw_reg surf_index
,
662 struct brw_reg offset
);
663 void generate_unpack_flags(vec4_instruction
*inst
,
666 void generate_untyped_atomic(vec4_instruction
*inst
,
668 struct brw_reg atomic_op
,
669 struct brw_reg surf_index
);
671 void generate_untyped_surface_read(vec4_instruction
*inst
,
673 struct brw_reg surf_index
);
675 struct brw_context
*brw
;
677 struct brw_compile
*p
;
679 struct gl_shader_program
*shader_prog
;
680 const struct gl_program
*prog
;
682 struct brw_vue_prog_data
*prog_data
;
685 const bool debug_flag
;
688 } /* namespace brw */
689 #endif /* __cplusplus */
691 #endif /* BRW_VEC4_H */