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>
28 #include "brw_shader.h"
32 #include <sys/types.h>
34 #include "main/macros.h"
35 #include "main/shaderobj.h"
36 #include "main/uniforms.h"
37 #include "program/prog_parameter.h"
38 #include "program/prog_print.h"
39 #include "program/prog_optimize.h"
40 #include "program/register_allocate.h"
41 #include "program/sampler.h"
42 #include "program/hash_table.h"
43 #include "brw_context.h"
47 #include "glsl/glsl_types.h"
56 FIXED_HW_REG
, /* a struct brw_reg */
57 UNIFORM
, /* prog_data->params[reg] */
62 /* Callers of this ralloc-based new need not call delete. It's
63 * easier to just ralloc_free 'ctx' (or any of its ancestors). */
64 static void* operator new(size_t size
, void *ctx
)
68 node
= ralloc_size(ctx
, size
);
76 memset(this, 0, sizeof(*this));
80 /** Generic unset register constructor. */
84 this->file
= BAD_FILE
;
87 /** Immediate value constructor. */
92 this->type
= BRW_REGISTER_TYPE_F
;
96 /** Immediate value constructor. */
101 this->type
= BRW_REGISTER_TYPE_D
;
105 /** Immediate value constructor. */
110 this->type
= BRW_REGISTER_TYPE_UD
;
114 /** Fixed brw_reg Immediate value constructor. */
115 fs_reg(struct brw_reg fixed_hw_reg
)
118 this->file
= FIXED_HW_REG
;
119 this->fixed_hw_reg
= fixed_hw_reg
;
120 this->type
= fixed_hw_reg
.type
;
123 fs_reg(enum register_file file
, int reg
);
124 fs_reg(enum register_file file
, int reg
, uint32_t type
);
125 fs_reg(class fs_visitor
*v
, const struct glsl_type
*type
);
127 bool equals(fs_reg
*r
)
129 return (file
== r
->file
&&
131 reg_offset
== r
->reg_offset
&&
133 negate
== r
->negate
&&
135 memcmp(&fixed_hw_reg
, &r
->fixed_hw_reg
,
136 sizeof(fixed_hw_reg
)) == 0 &&
141 /** Register file: ARF, GRF, MRF, IMM. */
142 enum register_file file
;
144 * Register number. For ARF/MRF, it's the hardware register. For
145 * GRF, it's a virtual register number until register allocation
149 * For virtual registers, this is a hardware register offset from
150 * the start of the register block (for example, a constant index
151 * in an array access).
154 /** Register type. BRW_REGISTER_TYPE_* */
159 struct brw_reg fixed_hw_reg
;
160 int smear
; /* -1, or a channel of the reg to smear to all channels. */
162 /** Value for file == IMM */
170 static const fs_reg reg_undef
;
171 static const fs_reg
reg_null_f(ARF
, BRW_ARF_NULL
, BRW_REGISTER_TYPE_F
);
172 static const fs_reg
reg_null_d(ARF
, BRW_ARF_NULL
, BRW_REGISTER_TYPE_D
);
174 class fs_inst
: public exec_node
{
176 /* Callers of this ralloc-based new need not call delete. It's
177 * easier to just ralloc_free 'ctx' (or any of its ancestors). */
178 static void* operator new(size_t size
, void *ctx
)
182 node
= rzalloc_size(ctx
, size
);
183 assert(node
!= NULL
);
190 memset(this, 0, sizeof(*this));
191 this->opcode
= BRW_OPCODE_NOP
;
192 this->conditional_mod
= BRW_CONDITIONAL_NONE
;
194 this->dst
= reg_undef
;
195 this->src
[0] = reg_undef
;
196 this->src
[1] = reg_undef
;
197 this->src
[2] = reg_undef
;
205 fs_inst(enum opcode opcode
)
208 this->opcode
= opcode
;
211 fs_inst(enum opcode opcode
, fs_reg dst
)
214 this->opcode
= opcode
;
218 assert(dst
.reg_offset
>= 0);
221 fs_inst(enum opcode opcode
, fs_reg dst
, fs_reg src0
)
224 this->opcode
= opcode
;
229 assert(dst
.reg_offset
>= 0);
230 if (src
[0].file
== GRF
)
231 assert(src
[0].reg_offset
>= 0);
234 fs_inst(enum opcode opcode
, fs_reg dst
, fs_reg src0
, fs_reg src1
)
237 this->opcode
= opcode
;
243 assert(dst
.reg_offset
>= 0);
244 if (src
[0].file
== GRF
)
245 assert(src
[0].reg_offset
>= 0);
246 if (src
[1].file
== GRF
)
247 assert(src
[1].reg_offset
>= 0);
250 fs_inst(enum opcode opcode
, fs_reg dst
, fs_reg src0
, fs_reg src1
, fs_reg src2
)
253 this->opcode
= opcode
;
260 assert(dst
.reg_offset
>= 0);
261 if (src
[0].file
== GRF
)
262 assert(src
[0].reg_offset
>= 0);
263 if (src
[1].file
== GRF
)
264 assert(src
[1].reg_offset
>= 0);
265 if (src
[2].file
== GRF
)
266 assert(src
[2].reg_offset
>= 0);
269 bool equals(fs_inst
*inst
)
271 return (opcode
== inst
->opcode
&&
272 dst
.equals(&inst
->dst
) &&
273 src
[0].equals(&inst
->src
[0]) &&
274 src
[1].equals(&inst
->src
[1]) &&
275 src
[2].equals(&inst
->src
[2]) &&
276 saturate
== inst
->saturate
&&
277 predicated
== inst
->predicated
&&
278 conditional_mod
== inst
->conditional_mod
&&
279 mlen
== inst
->mlen
&&
280 base_mrf
== inst
->base_mrf
&&
281 sampler
== inst
->sampler
&&
282 target
== inst
->target
&&
284 header_present
== inst
->header_present
&&
285 shadow_compare
== inst
->shadow_compare
&&
286 offset
== inst
->offset
);
291 return (opcode
== SHADER_OPCODE_TEX
||
292 opcode
== FS_OPCODE_TXB
||
293 opcode
== SHADER_OPCODE_TXD
||
294 opcode
== SHADER_OPCODE_TXF
||
295 opcode
== SHADER_OPCODE_TXL
||
296 opcode
== SHADER_OPCODE_TXS
);
301 return (opcode
== SHADER_OPCODE_RCP
||
302 opcode
== SHADER_OPCODE_RSQ
||
303 opcode
== SHADER_OPCODE_SQRT
||
304 opcode
== SHADER_OPCODE_EXP2
||
305 opcode
== SHADER_OPCODE_LOG2
||
306 opcode
== SHADER_OPCODE_SIN
||
307 opcode
== SHADER_OPCODE_COS
||
308 opcode
== SHADER_OPCODE_INT_QUOTIENT
||
309 opcode
== SHADER_OPCODE_INT_REMAINDER
||
310 opcode
== SHADER_OPCODE_POW
);
313 enum opcode opcode
; /* BRW_OPCODE_* or FS_OPCODE_* */
318 bool predicate_inverse
;
319 int conditional_mod
; /**< BRW_CONDITIONAL_* */
321 int mlen
; /**< SEND message length */
322 int base_mrf
; /**< First MRF in the SEND message, if mlen is nonzero. */
324 int target
; /**< MRT target. */
328 bool force_uncompressed
;
330 uint32_t offset
; /* spill/unspill offset */
333 * Annotation for the generated IR. One of the two can be set.
336 const char *annotation
;
340 class fs_visitor
: public ir_visitor
344 fs_visitor(struct brw_wm_compile
*c
, struct gl_shader_program
*prog
,
345 struct brw_shader
*shader
)
350 this->fp
= (struct gl_fragment_program
*)
351 prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
;
353 this->intel
= &brw
->intel
;
354 this->ctx
= &intel
->ctx
;
355 this->mem_ctx
= ralloc_context(NULL
);
356 this->shader
= shader
;
357 this->failed
= false;
358 this->variable_ht
= hash_table_ctor(0,
359 hash_table_pointer_hash
,
360 hash_table_pointer_compare
);
362 /* There's a question that appears to be left open in the spec:
363 * How do implicit dst conversions interact with the CMP
364 * instruction or conditional mods? On gen6, the instruction:
366 * CMP null<d> src0<f> src1<f>
368 * will do src1 - src0 and compare that result as if it was an
369 * integer. On gen4, it will do src1 - src0 as float, convert
370 * the result to int, and compare as int. In between, it
371 * appears that it does src1 - src0 and does the compare in the
372 * execution type so dst type doesn't matter.
374 if (this->intel
->gen
> 4)
375 this->reg_null_cmp
= reg_null_d
;
377 this->reg_null_cmp
= reg_null_f
;
379 this->frag_depth
= NULL
;
380 memset(this->outputs
, 0, sizeof(this->outputs
));
381 this->first_non_payload_grf
= 0;
383 this->current_annotation
= NULL
;
384 this->base_ir
= NULL
;
386 this->virtual_grf_sizes
= NULL
;
387 this->virtual_grf_next
= 0;
388 this->virtual_grf_array_size
= 0;
389 this->virtual_grf_def
= NULL
;
390 this->virtual_grf_use
= NULL
;
391 this->live_intervals_valid
= false;
393 this->kill_emitted
= false;
394 this->force_uncompressed_stack
= 0;
395 this->force_sechalf_stack
= 0;
400 ralloc_free(this->mem_ctx
);
401 hash_table_dtor(this->variable_ht
);
404 fs_reg
*variable_storage(ir_variable
*var
);
405 int virtual_grf_alloc(int size
);
406 void import_uniforms(fs_visitor
*v
);
408 void visit(ir_variable
*ir
);
409 void visit(ir_assignment
*ir
);
410 void visit(ir_dereference_variable
*ir
);
411 void visit(ir_dereference_record
*ir
);
412 void visit(ir_dereference_array
*ir
);
413 void visit(ir_expression
*ir
);
414 void visit(ir_texture
*ir
);
415 void visit(ir_if
*ir
);
416 void visit(ir_constant
*ir
);
417 void visit(ir_swizzle
*ir
);
418 void visit(ir_return
*ir
);
419 void visit(ir_loop
*ir
);
420 void visit(ir_loop_jump
*ir
);
421 void visit(ir_discard
*ir
);
422 void visit(ir_call
*ir
);
423 void visit(ir_function
*ir
);
424 void visit(ir_function_signature
*ir
);
426 void swizzle_result(ir_texture
*ir
, fs_reg orig_val
, int sampler
);
428 fs_inst
*emit(fs_inst inst
);
430 fs_inst
*emit(enum opcode opcode
)
432 return emit(fs_inst(opcode
));
435 fs_inst
*emit(enum opcode opcode
, fs_reg dst
)
437 return emit(fs_inst(opcode
, dst
));
440 fs_inst
*emit(enum opcode opcode
, fs_reg dst
, fs_reg src0
)
442 return emit(fs_inst(opcode
, dst
, src0
));
445 fs_inst
*emit(enum opcode opcode
, fs_reg dst
, fs_reg src0
, fs_reg src1
)
447 return emit(fs_inst(opcode
, dst
, src0
, src1
));
450 fs_inst
*emit(enum opcode opcode
, fs_reg dst
,
451 fs_reg src0
, fs_reg src1
, fs_reg src2
)
453 return emit(fs_inst(opcode
, dst
, src0
, src1
, src2
));
456 int type_size(const struct glsl_type
*type
);
459 void setup_paramvalues_refs();
460 void assign_curb_setup();
461 void calculate_urb_setup();
462 void assign_urb_setup();
464 void assign_regs_trivial();
465 int choose_spill_reg(struct ra_graph
*g
);
466 void spill_reg(int spill_reg
);
467 void split_virtual_grfs();
468 void setup_pull_constants();
469 void calculate_live_intervals();
470 bool propagate_constants();
471 bool opt_algebraic();
472 bool register_coalesce();
473 bool compute_to_mrf();
474 bool dead_code_eliminate();
475 bool remove_dead_constants();
476 bool remove_duplicate_mrf_writes();
477 bool virtual_grf_interferes(int a
, int b
);
478 void schedule_instructions();
479 void fail(const char *msg
, ...);
481 void push_force_uncompressed();
482 void pop_force_uncompressed();
483 void push_force_sechalf();
484 void pop_force_sechalf();
486 void generate_code();
487 void generate_fb_write(fs_inst
*inst
);
488 void generate_pixel_xy(struct brw_reg dst
, bool is_x
);
489 void generate_linterp(fs_inst
*inst
, struct brw_reg dst
,
490 struct brw_reg
*src
);
491 void generate_tex(fs_inst
*inst
, struct brw_reg dst
, struct brw_reg src
);
492 void generate_math1_gen7(fs_inst
*inst
,
495 void generate_math2_gen7(fs_inst
*inst
,
498 struct brw_reg src1
);
499 void generate_math1_gen6(fs_inst
*inst
,
502 void generate_math2_gen6(fs_inst
*inst
,
505 struct brw_reg src1
);
506 void generate_math_gen4(fs_inst
*inst
,
509 void generate_discard(fs_inst
*inst
);
510 void generate_ddx(fs_inst
*inst
, struct brw_reg dst
, struct brw_reg src
);
511 void generate_ddy(fs_inst
*inst
, struct brw_reg dst
, struct brw_reg src
);
512 void generate_spill(fs_inst
*inst
, struct brw_reg src
);
513 void generate_unspill(fs_inst
*inst
, struct brw_reg dst
);
514 void generate_pull_constant_load(fs_inst
*inst
, struct brw_reg dst
);
516 void emit_dummy_fs();
517 fs_reg
*emit_fragcoord_interpolation(ir_variable
*ir
);
518 fs_reg
*emit_frontfacing_interpolation(ir_variable
*ir
);
519 fs_reg
*emit_general_interpolation(ir_variable
*ir
);
520 void emit_interpolation_setup_gen4();
521 void emit_interpolation_setup_gen6();
522 fs_inst
*emit_texture_gen4(ir_texture
*ir
, fs_reg dst
, fs_reg coordinate
,
524 fs_inst
*emit_texture_gen5(ir_texture
*ir
, fs_reg dst
, fs_reg coordinate
,
526 fs_inst
*emit_texture_gen7(ir_texture
*ir
, fs_reg dst
, fs_reg coordinate
,
528 fs_inst
*emit_math(enum opcode op
, fs_reg dst
, fs_reg src0
);
529 fs_inst
*emit_math(enum opcode op
, fs_reg dst
, fs_reg src0
, fs_reg src1
);
530 bool try_emit_saturate(ir_expression
*ir
);
531 void emit_bool_to_cond_code(ir_rvalue
*condition
);
532 void emit_if_gen6(ir_if
*ir
);
533 void emit_unspill(fs_inst
*inst
, fs_reg reg
, uint32_t spill_offset
);
535 void emit_color_write(int target
, int index
, int first_color_mrf
);
536 void emit_fb_writes();
537 bool try_rewrite_rhs_to_dst(ir_assignment
*ir
,
540 fs_inst
*pre_rhs_inst
,
541 fs_inst
*last_rhs_inst
);
542 void emit_assignment_writes(fs_reg
&l
, fs_reg
&r
,
543 const glsl_type
*type
, bool predicated
);
544 void resolve_ud_negate(fs_reg
*reg
);
546 struct brw_reg
interp_reg(int location
, int channel
);
547 int setup_uniform_values(int loc
, const glsl_type
*type
);
548 void setup_builtin_uniform_values(ir_variable
*ir
);
549 int implied_mrf_writes(fs_inst
*inst
);
551 struct brw_context
*brw
;
552 const struct gl_fragment_program
*fp
;
553 struct intel_context
*intel
;
554 struct gl_context
*ctx
;
555 struct brw_wm_compile
*c
;
556 struct brw_compile
*p
;
557 struct brw_shader
*shader
;
558 struct gl_shader_program
*prog
;
560 exec_list instructions
;
562 /* Delayed setup of c->prog_data.params[] due to realloc of
563 * ParamValues[] during compile.
565 int param_index
[MAX_UNIFORMS
* 4];
566 int param_offset
[MAX_UNIFORMS
* 4];
568 int *virtual_grf_sizes
;
569 int virtual_grf_next
;
570 int virtual_grf_array_size
;
571 int *virtual_grf_def
;
572 int *virtual_grf_use
;
573 bool live_intervals_valid
;
575 /* This is the map from UNIFORM hw_reg + reg_offset as generated by
576 * the visitor to the packed uniform number after
577 * remove_dead_constants() that represents the actual uploaded
582 struct hash_table
*variable_ht
;
583 ir_variable
*frag_depth
;
584 fs_reg outputs
[BRW_MAX_DRAW_BUFFERS
];
585 int first_non_payload_grf
;
586 int urb_setup
[FRAG_ATTRIB_MAX
];
589 /** @{ debug annotation info */
590 const char *current_annotation
;
591 ir_instruction
*base_ir
;
597 /* Result of last visit() method. */
604 fs_reg delta_x
[BRW_WM_BARYCENTRIC_INTERP_MODE_COUNT
];
605 fs_reg delta_y
[BRW_WM_BARYCENTRIC_INTERP_MODE_COUNT
];
610 int force_uncompressed_stack
;
611 int force_sechalf_stack
;
614 bool brw_do_channel_expressions(struct exec_list
*instructions
);
615 bool brw_do_vector_splitting(struct exec_list
*instructions
);
616 bool brw_fs_precompile(struct gl_context
*ctx
, struct gl_shader_program
*prog
);