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"
38 #include "../glsl/ir.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 struct brw_reg
get_dst(void);
243 struct brw_reg
get_src(int i
);
245 enum opcode opcode
; /* BRW_OPCODE_* or FS_OPCODE_* */
250 bool predicate_inverse
;
253 int conditional_mod
; /**< BRW_CONDITIONAL_* */
256 int target
; /**< MRT target. */
261 int mlen
; /**< SEND message length */
262 int base_mrf
; /**< First MRF in the SEND message, if mlen is nonzero. */
264 uint32_t offset
; /* spill/unspill offset */
266 * Annotation for the generated IR. One of the two can be set.
269 const char *annotation
;
272 class vec4_visitor
: public ir_visitor
275 vec4_visitor(struct brw_vs_compile
*c
,
276 struct gl_shader_program
*prog
, struct brw_shader
*shader
);
281 return dst_reg(brw_null_reg());
286 return dst_reg(retype(brw_null_reg(), BRW_REGISTER_TYPE_D
));
289 dst_reg
dst_null_cmp()
297 struct brw_context
*brw
;
298 const struct gl_vertex_program
*vp
;
299 struct intel_context
*intel
;
300 struct gl_context
*ctx
;
301 struct brw_vs_compile
*c
;
302 struct brw_vs_prog_data
*prog_data
;
303 struct brw_compile
*p
;
304 struct brw_shader
*shader
;
305 struct gl_shader_program
*prog
;
307 exec_list instructions
;
313 * GLSL IR currently being processed, which is associated with our
314 * driver IR instructions for debugging purposes.
316 ir_instruction
*base_ir
;
317 const char *current_annotation
;
319 int *virtual_grf_sizes
;
320 int virtual_grf_count
;
321 int virtual_grf_array_size
;
322 int first_non_payload_grf
;
324 dst_reg
*variable_storage(ir_variable
*var
);
326 void reladdr_to_temp(ir_instruction
*ir
, src_reg
*reg
, int *num_reladdr
);
328 src_reg
src_reg_for_float(float val
);
331 * \name Visit methods
333 * As typical for the visitor pattern, there must be one \c visit method for
334 * each concrete subclass of \c ir_instruction. Virtual base classes within
335 * the hierarchy should not have \c visit methods.
338 virtual void visit(ir_variable
*);
339 virtual void visit(ir_loop
*);
340 virtual void visit(ir_loop_jump
*);
341 virtual void visit(ir_function_signature
*);
342 virtual void visit(ir_function
*);
343 virtual void visit(ir_expression
*);
344 virtual void visit(ir_swizzle
*);
345 virtual void visit(ir_dereference_variable
*);
346 virtual void visit(ir_dereference_array
*);
347 virtual void visit(ir_dereference_record
*);
348 virtual void visit(ir_assignment
*);
349 virtual void visit(ir_constant
*);
350 virtual void visit(ir_call
*);
351 virtual void visit(ir_return
*);
352 virtual void visit(ir_discard
*);
353 virtual void visit(ir_texture
*);
354 virtual void visit(ir_if
*);
359 /* Regs for vertex results. Generated at ir_variable visiting time
360 * for the ir->location's used.
362 dst_reg output_reg
[VERT_RESULT_MAX
];
363 int uniform_size
[MAX_UNIFORMS
];
366 struct hash_table
*variable_ht
;
369 void fail(const char *msg
, ...);
371 int virtual_grf_alloc(int size
);
372 int setup_uniform_values(int loc
, const glsl_type
*type
);
373 void setup_builtin_uniform_values(ir_variable
*ir
);
374 int setup_attributes(int payload_reg
);
375 int setup_uniforms(int payload_reg
);
376 void setup_payload();
377 void reg_allocate_trivial();
379 void move_grf_array_access_to_scratch();
381 vec4_instruction
*emit(enum opcode opcode
);
383 vec4_instruction
*emit(enum opcode opcode
, dst_reg dst
, src_reg src0
);
385 vec4_instruction
*emit(enum opcode opcode
, dst_reg dst
,
386 src_reg src0
, src_reg src1
);
388 vec4_instruction
*emit(enum opcode opcode
, dst_reg dst
,
389 src_reg src0
, src_reg src1
, src_reg src2
);
391 /** Walks an exec_list of ir_instruction and sends it through this visitor. */
392 void visit_instructions(const exec_list
*list
);
394 void emit_bool_to_cond_code(ir_rvalue
*ir
);
395 void emit_bool_comparison(unsigned int op
, dst_reg dst
, src_reg src0
, src_reg src1
);
396 void emit_if_gen6(ir_if
*ir
);
398 void emit_block_move(dst_reg
*dst
, src_reg
*src
,
399 const struct glsl_type
*type
, bool predicated
);
401 void emit_constant_values(dst_reg
*dst
, ir_constant
*value
);
404 * Emit the correct dot-product instruction for the type of arguments
406 void emit_dp(dst_reg dst
, src_reg src0
, src_reg src1
, unsigned elements
);
408 void emit_scalar(ir_instruction
*ir
, enum prog_opcode op
,
409 dst_reg dst
, src_reg src0
);
411 void emit_scalar(ir_instruction
*ir
, enum prog_opcode op
,
412 dst_reg dst
, src_reg src0
, src_reg src1
);
414 void emit_scs(ir_instruction
*ir
, enum prog_opcode op
,
415 dst_reg dst
, const src_reg
&src
);
417 void emit_math1_gen6(enum opcode opcode
, dst_reg dst
, src_reg src
);
418 void emit_math1_gen4(enum opcode opcode
, dst_reg dst
, src_reg src
);
419 void emit_math(enum opcode opcode
, dst_reg dst
, src_reg src
);
420 void emit_math2_gen6(enum opcode opcode
, dst_reg dst
, src_reg src0
, src_reg src1
);
421 void emit_math2_gen4(enum opcode opcode
, dst_reg dst
, src_reg src0
, src_reg src1
);
422 void emit_math(enum opcode opcode
, dst_reg dst
, src_reg src0
, src_reg src1
);
424 int emit_vue_header_gen6(int header_mrf
);
425 int emit_vue_header_gen4(int header_mrf
);
426 void emit_urb_writes(void);
428 src_reg
get_scratch_offset(vec4_instruction
*inst
,
429 src_reg
*reladdr
, int reg_offset
);
430 void emit_scratch_read(vec4_instruction
*inst
,
434 void emit_scratch_write(vec4_instruction
*inst
,
439 GLboolean
try_emit_sat(ir_expression
*ir
);
441 bool process_move_condition(ir_rvalue
*ir
);
443 void generate_code();
444 void generate_vs_instruction(vec4_instruction
*inst
,
446 struct brw_reg
*src
);
447 void generate_math1_gen4(vec4_instruction
*inst
,
450 void generate_math1_gen6(vec4_instruction
*inst
,
453 void generate_urb_write(vec4_instruction
*inst
);
456 } /* namespace brw */
458 #endif /* BRW_VEC4_H */