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.h"
50 ARF
= BRW_ARCHITECTURE_REGISTER_FILE
,
51 GRF
= BRW_GENERAL_REGISTER_FILE
,
52 MRF
= BRW_MESSAGE_REGISTER_FILE
,
53 IMM
= BRW_IMMEDIATE_VALUE
,
54 FIXED_HW_REG
, /* a struct brw_reg */
55 UNIFORM
, /* prog_data->params[hw_reg] */
60 FS_OPCODE_FB_WRITE
= 256,
75 FS_OPCODE_DISCARD_NOT
,
76 FS_OPCODE_DISCARD_AND
,
82 /* Callers of this talloc-based new need not call delete. It's
83 * easier to just talloc_free 'ctx' (or any of its ancestors). */
84 static void* operator new(size_t size
, void *ctx
)
88 node
= talloc_size(ctx
, size
);
103 /** Generic unset register constructor. */
107 this->file
= BAD_FILE
;
110 /** Immediate value constructor. */
115 this->type
= BRW_REGISTER_TYPE_F
;
119 /** Immediate value constructor. */
124 this->type
= BRW_REGISTER_TYPE_D
;
128 /** Immediate value constructor. */
133 this->type
= BRW_REGISTER_TYPE_UD
;
137 /** Fixed brw_reg Immediate value constructor. */
138 fs_reg(struct brw_reg fixed_hw_reg
)
141 this->file
= FIXED_HW_REG
;
142 this->fixed_hw_reg
= fixed_hw_reg
;
143 this->type
= fixed_hw_reg
.type
;
146 fs_reg(enum register_file file
, int hw_reg
);
147 fs_reg(enum register_file file
, int hw_reg
, uint32_t type
);
148 fs_reg(class fs_visitor
*v
, const struct glsl_type
*type
);
150 /** Register file: ARF, GRF, MRF, IMM. */
151 enum register_file file
;
152 /** virtual register number. 0 = fixed hw reg */
154 /** Offset within the virtual register. */
156 /** HW register number. Generally unset until register allocation. */
158 /** Register type. BRW_REGISTER_TYPE_* */
162 struct brw_reg fixed_hw_reg
;
164 /** Value for file == BRW_IMMMEDIATE_FILE */
172 class fs_inst
: public exec_node
{
174 /* Callers of this talloc-based new need not call delete. It's
175 * easier to just talloc_free 'ctx' (or any of its ancestors). */
176 static void* operator new(size_t size
, void *ctx
)
180 node
= talloc_zero_size(ctx
, size
);
181 assert(node
!= NULL
);
188 this->opcode
= BRW_OPCODE_NOP
;
189 this->saturate
= false;
190 this->conditional_mod
= BRW_CONDITIONAL_NONE
;
191 this->predicated
= false;
195 this->header_present
= false;
196 this->shadow_compare
= false;
209 this->opcode
= opcode
;
212 fs_inst(int opcode
, fs_reg dst
)
215 this->opcode
= opcode
;
219 assert(dst
.reg_offset
>= 0);
222 fs_inst(int opcode
, fs_reg dst
, fs_reg src0
)
225 this->opcode
= opcode
;
230 assert(dst
.reg_offset
>= 0);
231 if (src
[0].file
== GRF
)
232 assert(src
[0].reg_offset
>= 0);
235 fs_inst(int opcode
, fs_reg dst
, fs_reg src0
, fs_reg src1
)
238 this->opcode
= opcode
;
244 assert(dst
.reg_offset
>= 0);
245 if (src
[0].file
== GRF
)
246 assert(src
[0].reg_offset
>= 0);
247 if (src
[1].file
== GRF
)
248 assert(src
[1].reg_offset
>= 0);
251 fs_inst(int opcode
, fs_reg dst
, fs_reg src0
, fs_reg src1
, fs_reg src2
)
254 this->opcode
= opcode
;
261 assert(dst
.reg_offset
>= 0);
262 if (src
[0].file
== GRF
)
263 assert(src
[0].reg_offset
>= 0);
264 if (src
[1].file
== GRF
)
265 assert(src
[1].reg_offset
>= 0);
266 if (src
[2].file
== GRF
)
267 assert(src
[2].reg_offset
>= 0);
270 int opcode
; /* BRW_OPCODE_* or FS_OPCODE_* */
275 int conditional_mod
; /**< BRW_CONDITIONAL_* */
277 int mlen
; /**< SEND message length */
278 int base_mrf
; /**< First MRF in the SEND message, if mlen is nonzero. */
280 int target
; /**< MRT target. */
286 * Annotation for the generated IR. One of the two can be set.
289 const char *annotation
;
293 class fs_visitor
: public ir_visitor
297 fs_visitor(struct brw_wm_compile
*c
, struct brw_shader
*shader
)
302 this->fp
= brw
->fragment_program
;
303 this->intel
= &brw
->intel
;
304 this->ctx
= &intel
->ctx
;
305 this->mem_ctx
= talloc_new(NULL
);
306 this->shader
= shader
;
308 this->variable_ht
= hash_table_ctor(0,
309 hash_table_pointer_hash
,
310 hash_table_pointer_compare
);
312 this->frag_color
= NULL
;
313 this->frag_data
= NULL
;
314 this->frag_depth
= NULL
;
315 this->first_non_payload_grf
= 0;
317 this->current_annotation
= NULL
;
318 this->annotation_string
= NULL
;
319 this->annotation_ir
= NULL
;
320 this->base_ir
= NULL
;
322 this->virtual_grf_sizes
= NULL
;
323 this->virtual_grf_next
= 1;
324 this->virtual_grf_array_size
= 0;
325 this->virtual_grf_def
= NULL
;
326 this->virtual_grf_use
= NULL
;
328 this->kill_emitted
= false;
333 talloc_free(this->mem_ctx
);
334 hash_table_dtor(this->variable_ht
);
337 fs_reg
*variable_storage(ir_variable
*var
);
338 int virtual_grf_alloc(int size
);
340 void visit(ir_variable
*ir
);
341 void visit(ir_assignment
*ir
);
342 void visit(ir_dereference_variable
*ir
);
343 void visit(ir_dereference_record
*ir
);
344 void visit(ir_dereference_array
*ir
);
345 void visit(ir_expression
*ir
);
346 void visit(ir_texture
*ir
);
347 void visit(ir_if
*ir
);
348 void visit(ir_constant
*ir
);
349 void visit(ir_swizzle
*ir
);
350 void visit(ir_return
*ir
);
351 void visit(ir_loop
*ir
);
352 void visit(ir_loop_jump
*ir
);
353 void visit(ir_discard
*ir
);
354 void visit(ir_call
*ir
);
355 void visit(ir_function
*ir
);
356 void visit(ir_function_signature
*ir
);
358 fs_inst
*emit(fs_inst inst
);
359 void assign_curb_setup();
360 void calculate_urb_setup();
361 void assign_urb_setup();
363 void assign_regs_trivial();
364 void split_virtual_grfs();
365 void calculate_live_intervals();
366 bool propagate_constants();
367 bool register_coalesce();
368 bool compute_to_mrf();
369 bool dead_code_eliminate();
370 bool virtual_grf_interferes(int a
, int b
);
371 void generate_code();
372 void generate_fb_write(fs_inst
*inst
);
373 void generate_linterp(fs_inst
*inst
, struct brw_reg dst
,
374 struct brw_reg
*src
);
375 void generate_tex(fs_inst
*inst
, struct brw_reg dst
);
376 void generate_math(fs_inst
*inst
, struct brw_reg dst
, struct brw_reg
*src
);
377 void generate_discard_not(fs_inst
*inst
, struct brw_reg temp
);
378 void generate_discard_and(fs_inst
*inst
, struct brw_reg temp
);
379 void generate_ddx(fs_inst
*inst
, struct brw_reg dst
, struct brw_reg src
);
380 void generate_ddy(fs_inst
*inst
, struct brw_reg dst
, struct brw_reg src
);
382 void emit_dummy_fs();
383 fs_reg
*emit_fragcoord_interpolation(ir_variable
*ir
);
384 fs_reg
*emit_frontfacing_interpolation(ir_variable
*ir
);
385 fs_reg
*emit_general_interpolation(ir_variable
*ir
);
386 void emit_interpolation_setup_gen4();
387 void emit_interpolation_setup_gen6();
388 fs_inst
*emit_texture_gen4(ir_texture
*ir
, fs_reg dst
, fs_reg coordinate
);
389 fs_inst
*emit_texture_gen5(ir_texture
*ir
, fs_reg dst
, fs_reg coordinate
);
390 fs_inst
*emit_math(fs_opcodes op
, fs_reg dst
, fs_reg src0
);
391 fs_inst
*emit_math(fs_opcodes op
, fs_reg dst
, fs_reg src0
, fs_reg src1
);
392 void emit_bool_to_cond_code(ir_rvalue
*condition
);
393 void emit_if_gen6(ir_if
*ir
);
395 void emit_fb_writes();
396 void emit_assignment_writes(fs_reg
&l
, fs_reg
&r
,
397 const glsl_type
*type
, bool predicated
);
399 struct brw_reg
interp_reg(int location
, int channel
);
400 int setup_uniform_values(int loc
, const glsl_type
*type
);
401 void setup_builtin_uniform_values(ir_variable
*ir
);
403 struct brw_context
*brw
;
404 const struct gl_fragment_program
*fp
;
405 struct intel_context
*intel
;
406 struct gl_context
*ctx
;
407 struct brw_wm_compile
*c
;
408 struct brw_compile
*p
;
409 struct brw_shader
*shader
;
411 exec_list instructions
;
413 int *virtual_grf_sizes
;
414 int virtual_grf_next
;
415 int virtual_grf_array_size
;
416 int *virtual_grf_def
;
417 int *virtual_grf_use
;
419 struct hash_table
*variable_ht
;
420 ir_variable
*frag_color
, *frag_data
, *frag_depth
;
421 int first_non_payload_grf
;
422 int urb_setup
[FRAG_ATTRIB_MAX
];
425 /** @{ debug annotation info */
426 const char *current_annotation
;
427 ir_instruction
*base_ir
;
428 const char **annotation_string
;
429 ir_instruction
**annotation_ir
;
434 /* Result of last visit() method. */
447 GLboolean
brw_do_channel_expressions(struct exec_list
*instructions
);
448 GLboolean
brw_do_vector_splitting(struct exec_list
*instructions
);