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"
34 #include "brw_context.h"
45 * Common helper for constructing swizzles. When only a subset of
46 * channels of a vec4 are used, we don't want to reference the other
47 * channels, as that will tell optimization passes that those other
51 swizzle_for_size(int size
)
53 int size_swizzles
[4] = {
54 BRW_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
),
55 BRW_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Y
, SWIZZLE_Y
),
56 BRW_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_Z
),
57 BRW_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
),
60 assert((size
>= 1) && (size
<= 4));
61 return size_swizzles
[size
- 1];
65 ARF
= BRW_ARCHITECTURE_REGISTER_FILE
,
66 GRF
= BRW_GENERAL_REGISTER_FILE
,
67 MRF
= BRW_MESSAGE_REGISTER_FILE
,
68 IMM
= BRW_IMMEDIATE_VALUE
,
69 HW_REG
, /* a struct brw_reg */
71 UNIFORM
, /* prog_data->params[hw_reg] */
78 /** Register file: ARF, GRF, MRF, IMM. */
79 enum register_file file
;
80 /** virtual register number. 0 = fixed hw reg */
82 /** Offset within the virtual register. */
84 /** Register type. BRW_REGISTER_TYPE_* */
87 struct brw_reg fixed_hw_reg
;
88 int smear
; /* -1, or a channel of the reg to smear to all channels. */
90 /** Value for file == BRW_IMMMEDIATE_FILE */
98 class src_reg
: public reg
101 /* Callers of this ralloc-based new need not call delete. It's
102 * easier to just ralloc_free 'ctx' (or any of its ancestors). */
103 static void* operator new(size_t size
, void *ctx
)
107 node
= ralloc_size(ctx
, size
);
108 assert(node
!= NULL
);
115 memset(this, 0, sizeof(*this));
117 this->file
= BAD_FILE
;
120 src_reg(register_file file
, int reg
, const glsl_type
*type
)
126 if (type
&& (type
->is_scalar() || type
->is_vector() || type
->is_matrix()))
127 this->swizzle
= swizzle_for_size(type
->vector_elements
);
129 this->swizzle
= SWIZZLE_XYZW
;
132 /** Generic unset register constructor. */
143 this->type
= BRW_REGISTER_TYPE_F
;
152 this->type
= BRW_REGISTER_TYPE_UD
;
161 this->type
= BRW_REGISTER_TYPE_D
;
165 src_reg(class vec4_visitor
*v
, const struct glsl_type
*type
);
167 explicit src_reg(dst_reg reg
);
169 GLuint swizzle
; /**< SWIZZLE_XYZW swizzles from Mesa. */
176 class dst_reg
: public reg
179 /* Callers of this ralloc-based new need not call delete. It's
180 * easier to just ralloc_free 'ctx' (or any of its ancestors). */
181 static void* operator new(size_t size
, void *ctx
)
185 node
= ralloc_size(ctx
, size
);
186 assert(node
!= NULL
);
193 memset(this, 0, sizeof(*this));
194 this->file
= BAD_FILE
;
195 this->writemask
= WRITEMASK_XYZW
;
203 dst_reg(register_file file
, int reg
)
211 dst_reg(struct brw_reg reg
)
216 this->fixed_hw_reg
= reg
;
219 dst_reg(class vec4_visitor
*v
, const struct glsl_type
*type
);
221 explicit dst_reg(src_reg reg
);
223 int writemask
; /**< Bitfield of WRITEMASK_[XYZW] */
228 class vec4_instruction
: public exec_node
{
230 /* Callers of this ralloc-based new need not call delete. It's
231 * easier to just ralloc_free 'ctx' (or any of its ancestors). */
232 static void* operator new(size_t size
, void *ctx
)
236 node
= rzalloc_size(ctx
, size
);
237 assert(node
!= NULL
);
242 vec4_instruction(vec4_visitor
*v
, enum opcode opcode
,
243 dst_reg dst
= dst_reg(),
244 src_reg src0
= src_reg(),
245 src_reg src1
= src_reg(),
246 src_reg src2
= src_reg());
248 struct brw_reg
get_dst(void);
249 struct brw_reg
get_src(int i
);
251 enum opcode opcode
; /* BRW_OPCODE_* or FS_OPCODE_* */
256 bool predicate_inverse
;
259 int conditional_mod
; /**< BRW_CONDITIONAL_* */
262 int target
; /**< MRT target. */
267 int mlen
; /**< SEND message length */
268 int base_mrf
; /**< First MRF in the SEND message, if mlen is nonzero. */
270 uint32_t offset
; /* spill/unspill offset */
272 * Annotation for the generated IR. One of the two can be set.
275 const char *annotation
;
278 class vec4_visitor
: public ir_visitor
281 vec4_visitor(struct brw_vs_compile
*c
,
282 struct gl_shader_program
*prog
, struct brw_shader
*shader
);
287 return dst_reg(brw_null_reg());
292 return dst_reg(retype(brw_null_reg(), BRW_REGISTER_TYPE_D
));
295 dst_reg
dst_null_cmp()
303 struct brw_context
*brw
;
304 const struct gl_vertex_program
*vp
;
305 struct intel_context
*intel
;
306 struct gl_context
*ctx
;
307 struct brw_vs_compile
*c
;
308 struct brw_vs_prog_data
*prog_data
;
309 struct brw_compile
*p
;
310 struct brw_shader
*shader
;
311 struct gl_shader_program
*prog
;
313 exec_list instructions
;
319 * GLSL IR currently being processed, which is associated with our
320 * driver IR instructions for debugging purposes.
322 ir_instruction
*base_ir
;
323 const char *current_annotation
;
325 int *virtual_grf_sizes
;
326 int virtual_grf_count
;
327 int virtual_grf_array_size
;
328 int first_non_payload_grf
;
329 int *virtual_grf_def
;
330 int *virtual_grf_use
;
331 bool live_intervals_valid
;
333 dst_reg
*variable_storage(ir_variable
*var
);
335 void reladdr_to_temp(ir_instruction
*ir
, src_reg
*reg
, int *num_reladdr
);
337 src_reg
src_reg_for_float(float val
);
340 * \name Visit methods
342 * As typical for the visitor pattern, there must be one \c visit method for
343 * each concrete subclass of \c ir_instruction. Virtual base classes within
344 * the hierarchy should not have \c visit methods.
347 virtual void visit(ir_variable
*);
348 virtual void visit(ir_loop
*);
349 virtual void visit(ir_loop_jump
*);
350 virtual void visit(ir_function_signature
*);
351 virtual void visit(ir_function
*);
352 virtual void visit(ir_expression
*);
353 virtual void visit(ir_swizzle
*);
354 virtual void visit(ir_dereference_variable
*);
355 virtual void visit(ir_dereference_array
*);
356 virtual void visit(ir_dereference_record
*);
357 virtual void visit(ir_assignment
*);
358 virtual void visit(ir_constant
*);
359 virtual void visit(ir_call
*);
360 virtual void visit(ir_return
*);
361 virtual void visit(ir_discard
*);
362 virtual void visit(ir_texture
*);
363 virtual void visit(ir_if
*);
368 /* Regs for vertex results. Generated at ir_variable visiting time
369 * for the ir->location's used.
371 dst_reg output_reg
[VERT_RESULT_MAX
];
372 int uniform_size
[MAX_UNIFORMS
];
373 int uniform_vector_size
[MAX_UNIFORMS
];
376 struct hash_table
*variable_ht
;
379 void fail(const char *msg
, ...);
381 int virtual_grf_alloc(int size
);
382 int setup_uniform_values(int loc
, const glsl_type
*type
);
383 void setup_builtin_uniform_values(ir_variable
*ir
);
384 int setup_attributes(int payload_reg
);
385 int setup_uniforms(int payload_reg
);
386 void setup_payload();
387 void reg_allocate_trivial();
389 void move_grf_array_access_to_scratch();
390 void move_uniform_array_access_to_pull_constants();
391 void split_uniform_registers();
392 void pack_uniform_registers();
393 void calculate_live_intervals();
394 bool dead_code_eliminate();
395 bool virtual_grf_interferes(int a
, int b
);
397 vec4_instruction
*emit(vec4_instruction
*inst
);
399 vec4_instruction
*emit(enum opcode opcode
);
401 vec4_instruction
*emit(enum opcode opcode
, dst_reg dst
, src_reg src0
);
403 vec4_instruction
*emit(enum opcode opcode
, dst_reg dst
,
404 src_reg src0
, src_reg src1
);
406 vec4_instruction
*emit(enum opcode opcode
, dst_reg dst
,
407 src_reg src0
, src_reg src1
, src_reg src2
);
409 vec4_instruction
*MOV(dst_reg dst
, src_reg src0
);
410 vec4_instruction
*NOT(dst_reg dst
, src_reg src0
);
411 vec4_instruction
*RNDD(dst_reg dst
, src_reg src0
);
412 vec4_instruction
*RNDE(dst_reg dst
, src_reg src0
);
413 vec4_instruction
*RNDZ(dst_reg dst
, src_reg src0
);
414 vec4_instruction
*FRC(dst_reg dst
, src_reg src0
);
415 vec4_instruction
*ADD(dst_reg dst
, src_reg src0
, src_reg src1
);
416 vec4_instruction
*MUL(dst_reg dst
, src_reg src0
, src_reg src1
);
417 vec4_instruction
*MACH(dst_reg dst
, src_reg src0
, src_reg src1
);
418 vec4_instruction
*MAC(dst_reg dst
, src_reg src0
, src_reg src1
);
419 vec4_instruction
*AND(dst_reg dst
, src_reg src0
, src_reg src1
);
420 vec4_instruction
*OR(dst_reg dst
, src_reg src0
, src_reg src1
);
421 vec4_instruction
*XOR(dst_reg dst
, src_reg src0
, src_reg src1
);
422 vec4_instruction
*DP3(dst_reg dst
, src_reg src0
, src_reg src1
);
423 vec4_instruction
*DP4(dst_reg dst
, src_reg src0
, src_reg src1
);
424 vec4_instruction
*CMP(dst_reg dst
, src_reg src0
, src_reg src1
,
426 vec4_instruction
*IF(src_reg src0
, src_reg src1
, uint32_t condition
);
427 vec4_instruction
*IF(uint32_t predicate
);
429 bool try_rewrite_rhs_to_dst(ir_assignment
*ir
,
432 vec4_instruction
*pre_rhs_inst
,
433 vec4_instruction
*last_rhs_inst
);
435 /** Walks an exec_list of ir_instruction and sends it through this visitor. */
436 void visit_instructions(const exec_list
*list
);
438 void emit_bool_to_cond_code(ir_rvalue
*ir
);
439 void emit_bool_comparison(unsigned int op
, dst_reg dst
, src_reg src0
, src_reg src1
);
440 void emit_if_gen6(ir_if
*ir
);
442 void emit_block_move(dst_reg
*dst
, src_reg
*src
,
443 const struct glsl_type
*type
, bool predicated
);
445 void emit_constant_values(dst_reg
*dst
, ir_constant
*value
);
448 * Emit the correct dot-product instruction for the type of arguments
450 void emit_dp(dst_reg dst
, src_reg src0
, src_reg src1
, unsigned elements
);
452 void emit_scalar(ir_instruction
*ir
, enum prog_opcode op
,
453 dst_reg dst
, src_reg src0
);
455 void emit_scalar(ir_instruction
*ir
, enum prog_opcode op
,
456 dst_reg dst
, src_reg src0
, src_reg src1
);
458 void emit_scs(ir_instruction
*ir
, enum prog_opcode op
,
459 dst_reg dst
, const src_reg
&src
);
461 void emit_math1_gen6(enum opcode opcode
, dst_reg dst
, src_reg src
);
462 void emit_math1_gen4(enum opcode opcode
, dst_reg dst
, src_reg src
);
463 void emit_math(enum opcode opcode
, dst_reg dst
, src_reg src
);
464 void emit_math2_gen6(enum opcode opcode
, dst_reg dst
, src_reg src0
, src_reg src1
);
465 void emit_math2_gen4(enum opcode opcode
, dst_reg dst
, src_reg src0
, src_reg src1
);
466 void emit_math(enum opcode opcode
, dst_reg dst
, src_reg src0
, src_reg src1
);
468 int emit_vue_header_gen6(int header_mrf
);
469 int emit_vue_header_gen4(int header_mrf
);
470 void emit_urb_writes(void);
472 src_reg
get_scratch_offset(vec4_instruction
*inst
,
473 src_reg
*reladdr
, int reg_offset
);
474 src_reg
get_pull_constant_offset(vec4_instruction
*inst
,
475 src_reg
*reladdr
, int reg_offset
);
476 void emit_scratch_read(vec4_instruction
*inst
,
480 void emit_scratch_write(vec4_instruction
*inst
,
484 void emit_pull_constant_load(vec4_instruction
*inst
,
489 GLboolean
try_emit_sat(ir_expression
*ir
);
491 bool process_move_condition(ir_rvalue
*ir
);
493 void generate_code();
494 void generate_vs_instruction(vec4_instruction
*inst
,
496 struct brw_reg
*src
);
498 void generate_math1_gen4(vec4_instruction
*inst
,
501 void generate_math1_gen6(vec4_instruction
*inst
,
504 void generate_math2_gen4(vec4_instruction
*inst
,
507 struct brw_reg src1
);
508 void generate_math2_gen6(vec4_instruction
*inst
,
511 struct brw_reg src1
);
513 void generate_urb_write(vec4_instruction
*inst
);
514 void generate_oword_dual_block_offsets(struct brw_reg m1
,
515 struct brw_reg index
);
516 void generate_scratch_write(vec4_instruction
*inst
,
519 struct brw_reg index
);
520 void generate_scratch_read(vec4_instruction
*inst
,
522 struct brw_reg index
);
523 void generate_pull_constant_load(vec4_instruction
*inst
,
525 struct brw_reg index
);
528 } /* namespace brw */
530 #endif /* BRW_VEC4_H */