Merge branch 'xa_branch'
[mesa.git] / src / mesa / drivers / dri / i965 / brw_fs.h
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28 extern "C" {
29
30 #include <sys/types.h>
31
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"
42 #include "brw_eu.h"
43 #include "brw_wm.h"
44 }
45 #include "../glsl/glsl_types.h"
46 #include "../glsl/ir.h"
47
48 enum register_file {
49 ARF = BRW_ARCHITECTURE_REGISTER_FILE,
50 GRF = BRW_GENERAL_REGISTER_FILE,
51 MRF = BRW_MESSAGE_REGISTER_FILE,
52 IMM = BRW_IMMEDIATE_VALUE,
53 FIXED_HW_REG, /* a struct brw_reg */
54 UNIFORM, /* prog_data->params[hw_reg] */
55 BAD_FILE
56 };
57
58 enum fs_opcodes {
59 FS_OPCODE_FB_WRITE = 256,
60 FS_OPCODE_RCP,
61 FS_OPCODE_RSQ,
62 FS_OPCODE_SQRT,
63 FS_OPCODE_EXP2,
64 FS_OPCODE_LOG2,
65 FS_OPCODE_POW,
66 FS_OPCODE_SIN,
67 FS_OPCODE_COS,
68 FS_OPCODE_DDX,
69 FS_OPCODE_DDY,
70 FS_OPCODE_PIXEL_X,
71 FS_OPCODE_PIXEL_Y,
72 FS_OPCODE_CINTERP,
73 FS_OPCODE_LINTERP,
74 FS_OPCODE_TEX,
75 FS_OPCODE_TXB,
76 FS_OPCODE_TXD,
77 FS_OPCODE_TXL,
78 FS_OPCODE_DISCARD,
79 FS_OPCODE_SPILL,
80 FS_OPCODE_UNSPILL,
81 FS_OPCODE_PULL_CONSTANT_LOAD,
82 };
83
84
85 class fs_reg {
86 public:
87 /* Callers of this ralloc-based new need not call delete. It's
88 * easier to just ralloc_free 'ctx' (or any of its ancestors). */
89 static void* operator new(size_t size, void *ctx)
90 {
91 void *node;
92
93 node = ralloc_size(ctx, size);
94 assert(node != NULL);
95
96 return node;
97 }
98
99 void init()
100 {
101 memset(this, 0, sizeof(*this));
102 this->hw_reg = -1;
103 this->smear = -1;
104 }
105
106 /** Generic unset register constructor. */
107 fs_reg()
108 {
109 init();
110 this->file = BAD_FILE;
111 }
112
113 /** Immediate value constructor. */
114 fs_reg(float f)
115 {
116 init();
117 this->file = IMM;
118 this->type = BRW_REGISTER_TYPE_F;
119 this->imm.f = f;
120 }
121
122 /** Immediate value constructor. */
123 fs_reg(int32_t i)
124 {
125 init();
126 this->file = IMM;
127 this->type = BRW_REGISTER_TYPE_D;
128 this->imm.i = i;
129 }
130
131 /** Immediate value constructor. */
132 fs_reg(uint32_t u)
133 {
134 init();
135 this->file = IMM;
136 this->type = BRW_REGISTER_TYPE_UD;
137 this->imm.u = u;
138 }
139
140 /** Fixed brw_reg Immediate value constructor. */
141 fs_reg(struct brw_reg fixed_hw_reg)
142 {
143 init();
144 this->file = FIXED_HW_REG;
145 this->fixed_hw_reg = fixed_hw_reg;
146 this->type = fixed_hw_reg.type;
147 }
148
149 fs_reg(enum register_file file, int hw_reg);
150 fs_reg(enum register_file file, int hw_reg, uint32_t type);
151 fs_reg(class fs_visitor *v, const struct glsl_type *type);
152
153 bool equals(fs_reg *r)
154 {
155 return (file == r->file &&
156 reg == r->reg &&
157 reg_offset == r->reg_offset &&
158 hw_reg == r->hw_reg &&
159 type == r->type &&
160 negate == r->negate &&
161 abs == r->abs &&
162 memcmp(&fixed_hw_reg, &r->fixed_hw_reg,
163 sizeof(fixed_hw_reg)) == 0 &&
164 smear == r->smear &&
165 imm.u == r->imm.u);
166 }
167
168 /** Register file: ARF, GRF, MRF, IMM. */
169 enum register_file file;
170 /** virtual register number. 0 = fixed hw reg */
171 int reg;
172 /** Offset within the virtual register. */
173 int reg_offset;
174 /** HW register number. Generally unset until register allocation. */
175 int hw_reg;
176 /** Register type. BRW_REGISTER_TYPE_* */
177 int type;
178 bool negate;
179 bool abs;
180 bool sechalf;
181 struct brw_reg fixed_hw_reg;
182 int smear; /* -1, or a channel of the reg to smear to all channels. */
183
184 /** Value for file == BRW_IMMMEDIATE_FILE */
185 union {
186 int32_t i;
187 uint32_t u;
188 float f;
189 } imm;
190 };
191
192 static const fs_reg reg_undef;
193 static const fs_reg reg_null_f(ARF, BRW_ARF_NULL, BRW_REGISTER_TYPE_F);
194 static const fs_reg reg_null_d(ARF, BRW_ARF_NULL, BRW_REGISTER_TYPE_D);
195
196 class fs_inst : public exec_node {
197 public:
198 /* Callers of this ralloc-based new need not call delete. It's
199 * easier to just ralloc_free 'ctx' (or any of its ancestors). */
200 static void* operator new(size_t size, void *ctx)
201 {
202 void *node;
203
204 node = rzalloc_size(ctx, size);
205 assert(node != NULL);
206
207 return node;
208 }
209
210 void init()
211 {
212 memset(this, 0, sizeof(*this));
213 this->opcode = BRW_OPCODE_NOP;
214 this->conditional_mod = BRW_CONDITIONAL_NONE;
215
216 this->dst = reg_undef;
217 this->src[0] = reg_undef;
218 this->src[1] = reg_undef;
219 this->src[2] = reg_undef;
220 }
221
222 fs_inst()
223 {
224 init();
225 }
226
227 fs_inst(int opcode)
228 {
229 init();
230 this->opcode = opcode;
231 }
232
233 fs_inst(int opcode, fs_reg dst)
234 {
235 init();
236 this->opcode = opcode;
237 this->dst = dst;
238
239 if (dst.file == GRF)
240 assert(dst.reg_offset >= 0);
241 }
242
243 fs_inst(int opcode, fs_reg dst, fs_reg src0)
244 {
245 init();
246 this->opcode = opcode;
247 this->dst = dst;
248 this->src[0] = src0;
249
250 if (dst.file == GRF)
251 assert(dst.reg_offset >= 0);
252 if (src[0].file == GRF)
253 assert(src[0].reg_offset >= 0);
254 }
255
256 fs_inst(int opcode, fs_reg dst, fs_reg src0, fs_reg src1)
257 {
258 init();
259 this->opcode = opcode;
260 this->dst = dst;
261 this->src[0] = src0;
262 this->src[1] = src1;
263
264 if (dst.file == GRF)
265 assert(dst.reg_offset >= 0);
266 if (src[0].file == GRF)
267 assert(src[0].reg_offset >= 0);
268 if (src[1].file == GRF)
269 assert(src[1].reg_offset >= 0);
270 }
271
272 fs_inst(int opcode, fs_reg dst, fs_reg src0, fs_reg src1, fs_reg src2)
273 {
274 init();
275 this->opcode = opcode;
276 this->dst = dst;
277 this->src[0] = src0;
278 this->src[1] = src1;
279 this->src[2] = src2;
280
281 if (dst.file == GRF)
282 assert(dst.reg_offset >= 0);
283 if (src[0].file == GRF)
284 assert(src[0].reg_offset >= 0);
285 if (src[1].file == GRF)
286 assert(src[1].reg_offset >= 0);
287 if (src[2].file == GRF)
288 assert(src[2].reg_offset >= 0);
289 }
290
291 bool equals(fs_inst *inst)
292 {
293 return (opcode == inst->opcode &&
294 dst.equals(&inst->dst) &&
295 src[0].equals(&inst->src[0]) &&
296 src[1].equals(&inst->src[1]) &&
297 src[2].equals(&inst->src[2]) &&
298 saturate == inst->saturate &&
299 predicated == inst->predicated &&
300 conditional_mod == inst->conditional_mod &&
301 mlen == inst->mlen &&
302 base_mrf == inst->base_mrf &&
303 sampler == inst->sampler &&
304 target == inst->target &&
305 eot == inst->eot &&
306 header_present == inst->header_present &&
307 shadow_compare == inst->shadow_compare &&
308 offset == inst->offset);
309 }
310
311 bool is_tex()
312 {
313 return (opcode == FS_OPCODE_TEX ||
314 opcode == FS_OPCODE_TXB ||
315 opcode == FS_OPCODE_TXD ||
316 opcode == FS_OPCODE_TXL);
317 }
318
319 bool is_math()
320 {
321 return (opcode == FS_OPCODE_RCP ||
322 opcode == FS_OPCODE_RSQ ||
323 opcode == FS_OPCODE_SQRT ||
324 opcode == FS_OPCODE_EXP2 ||
325 opcode == FS_OPCODE_LOG2 ||
326 opcode == FS_OPCODE_SIN ||
327 opcode == FS_OPCODE_COS ||
328 opcode == FS_OPCODE_POW);
329 }
330
331 int opcode; /* BRW_OPCODE_* or FS_OPCODE_* */
332 fs_reg dst;
333 fs_reg src[3];
334 bool saturate;
335 bool predicated;
336 bool predicate_inverse;
337 int conditional_mod; /**< BRW_CONDITIONAL_* */
338
339 int mlen; /**< SEND message length */
340 int base_mrf; /**< First MRF in the SEND message, if mlen is nonzero. */
341 int sampler;
342 int target; /**< MRT target. */
343 bool eot;
344 bool header_present;
345 bool shadow_compare;
346 bool force_uncompressed;
347 bool force_sechalf;
348 uint32_t offset; /* spill/unspill offset */
349
350 /** @{
351 * Annotation for the generated IR. One of the two can be set.
352 */
353 ir_instruction *ir;
354 const char *annotation;
355 /** @} */
356 };
357
358 class fs_visitor : public ir_visitor
359 {
360 public:
361
362 fs_visitor(struct brw_wm_compile *c, struct gl_shader_program *prog,
363 struct brw_shader *shader)
364 {
365 this->c = c;
366 this->p = &c->func;
367 this->brw = p->brw;
368 this->fp = prog->FragmentProgram;
369 this->prog = prog;
370 this->intel = &brw->intel;
371 this->ctx = &intel->ctx;
372 this->mem_ctx = ralloc_context(NULL);
373 this->shader = shader;
374 this->failed = false;
375 this->variable_ht = hash_table_ctor(0,
376 hash_table_pointer_hash,
377 hash_table_pointer_compare);
378
379 /* There's a question that appears to be left open in the spec:
380 * How do implicit dst conversions interact with the CMP
381 * instruction or conditional mods? On gen6, the instruction:
382 *
383 * CMP null<d> src0<f> src1<f>
384 *
385 * will do src1 - src0 and compare that result as if it was an
386 * integer. On gen4, it will do src1 - src0 as float, convert
387 * the result to int, and compare as int. In between, it
388 * appears that it does src1 - src0 and does the compare in the
389 * execution type so dst type doesn't matter.
390 */
391 if (this->intel->gen > 4)
392 this->reg_null_cmp = reg_null_d;
393 else
394 this->reg_null_cmp = reg_null_f;
395
396 this->frag_color = NULL;
397 this->frag_data = NULL;
398 this->frag_depth = NULL;
399 this->first_non_payload_grf = 0;
400
401 this->current_annotation = NULL;
402 this->base_ir = NULL;
403
404 this->virtual_grf_sizes = NULL;
405 this->virtual_grf_next = 1;
406 this->virtual_grf_array_size = 0;
407 this->virtual_grf_def = NULL;
408 this->virtual_grf_use = NULL;
409 this->live_intervals_valid = false;
410
411 this->kill_emitted = false;
412 this->force_uncompressed_stack = 0;
413 this->force_sechalf_stack = 0;
414 }
415
416 ~fs_visitor()
417 {
418 ralloc_free(this->mem_ctx);
419 hash_table_dtor(this->variable_ht);
420 }
421
422 fs_reg *variable_storage(ir_variable *var);
423 int virtual_grf_alloc(int size);
424 void import_uniforms(struct hash_table *src_variable_ht);
425
426 void visit(ir_variable *ir);
427 void visit(ir_assignment *ir);
428 void visit(ir_dereference_variable *ir);
429 void visit(ir_dereference_record *ir);
430 void visit(ir_dereference_array *ir);
431 void visit(ir_expression *ir);
432 void visit(ir_texture *ir);
433 void visit(ir_if *ir);
434 void visit(ir_constant *ir);
435 void visit(ir_swizzle *ir);
436 void visit(ir_return *ir);
437 void visit(ir_loop *ir);
438 void visit(ir_loop_jump *ir);
439 void visit(ir_discard *ir);
440 void visit(ir_call *ir);
441 void visit(ir_function *ir);
442 void visit(ir_function_signature *ir);
443
444 void swizzle_result(ir_texture *ir, fs_reg orig_val, int sampler);
445
446 fs_inst *emit(fs_inst inst);
447
448 fs_inst *emit(int opcode)
449 {
450 return emit(fs_inst(opcode));
451 }
452
453 fs_inst *emit(int opcode, fs_reg dst)
454 {
455 return emit(fs_inst(opcode, dst));
456 }
457
458 fs_inst *emit(int opcode, fs_reg dst, fs_reg src0)
459 {
460 return emit(fs_inst(opcode, dst, src0));
461 }
462
463 fs_inst *emit(int opcode, fs_reg dst, fs_reg src0, fs_reg src1)
464 {
465 return emit(fs_inst(opcode, dst, src0, src1));
466 }
467
468 fs_inst *emit(int opcode, fs_reg dst, fs_reg src0, fs_reg src1, fs_reg src2)
469 {
470 return emit(fs_inst(opcode, dst, src0, src1, src2));
471 }
472
473 int type_size(const struct glsl_type *type);
474
475 bool run();
476 void setup_paramvalues_refs();
477 void assign_curb_setup();
478 void calculate_urb_setup();
479 void assign_urb_setup();
480 bool assign_regs();
481 void assign_regs_trivial();
482 int choose_spill_reg(struct ra_graph *g);
483 void spill_reg(int spill_reg);
484 void split_virtual_grfs();
485 void setup_pull_constants();
486 void calculate_live_intervals();
487 bool propagate_constants();
488 bool register_coalesce();
489 bool compute_to_mrf();
490 bool dead_code_eliminate();
491 bool remove_duplicate_mrf_writes();
492 bool virtual_grf_interferes(int a, int b);
493 void schedule_instructions();
494 void fail(const char *msg, ...);
495
496 void push_force_uncompressed();
497 void pop_force_uncompressed();
498 void push_force_sechalf();
499 void pop_force_sechalf();
500
501 void generate_code();
502 void generate_fb_write(fs_inst *inst);
503 void generate_pixel_xy(struct brw_reg dst, bool is_x);
504 void generate_linterp(fs_inst *inst, struct brw_reg dst,
505 struct brw_reg *src);
506 void generate_tex(fs_inst *inst, struct brw_reg dst, struct brw_reg src);
507 void generate_math(fs_inst *inst, struct brw_reg dst, struct brw_reg *src);
508 void generate_discard(fs_inst *inst);
509 void generate_ddx(fs_inst *inst, struct brw_reg dst, struct brw_reg src);
510 void generate_ddy(fs_inst *inst, struct brw_reg dst, struct brw_reg src);
511 void generate_spill(fs_inst *inst, struct brw_reg src);
512 void generate_unspill(fs_inst *inst, struct brw_reg dst);
513 void generate_pull_constant_load(fs_inst *inst, struct brw_reg dst);
514
515 void emit_dummy_fs();
516 fs_reg *emit_fragcoord_interpolation(ir_variable *ir);
517 fs_reg *emit_frontfacing_interpolation(ir_variable *ir);
518 fs_reg *emit_general_interpolation(ir_variable *ir);
519 void emit_interpolation_setup_gen4();
520 void emit_interpolation_setup_gen6();
521 fs_inst *emit_texture_gen4(ir_texture *ir, fs_reg dst, fs_reg coordinate,
522 int sampler);
523 fs_inst *emit_texture_gen5(ir_texture *ir, fs_reg dst, fs_reg coordinate,
524 int sampler);
525 fs_inst *emit_texture_gen7(ir_texture *ir, fs_reg dst, fs_reg coordinate,
526 int sampler);
527 fs_inst *emit_math(fs_opcodes op, fs_reg dst, fs_reg src0);
528 fs_inst *emit_math(fs_opcodes op, fs_reg dst, fs_reg src0, fs_reg src1);
529 bool try_emit_saturate(ir_expression *ir);
530 void emit_bool_to_cond_code(ir_rvalue *condition);
531 void emit_if_gen6(ir_if *ir);
532 void emit_unspill(fs_inst *inst, fs_reg reg, uint32_t spill_offset);
533
534 void emit_color_write(int index, int first_color_mrf, fs_reg color);
535 void emit_fb_writes();
536 void emit_assignment_writes(fs_reg &l, fs_reg &r,
537 const glsl_type *type, bool predicated);
538
539 struct brw_reg interp_reg(int location, int channel);
540 int setup_uniform_values(int loc, const glsl_type *type);
541 void setup_builtin_uniform_values(ir_variable *ir);
542 int implied_mrf_writes(fs_inst *inst);
543
544 struct brw_context *brw;
545 const struct gl_fragment_program *fp;
546 struct intel_context *intel;
547 struct gl_context *ctx;
548 struct brw_wm_compile *c;
549 struct brw_compile *p;
550 struct brw_shader *shader;
551 struct gl_shader_program *prog;
552 void *mem_ctx;
553 exec_list instructions;
554
555 /* Delayed setup of c->prog_data.params[] due to realloc of
556 * ParamValues[] during compile.
557 */
558 int param_index[MAX_UNIFORMS * 4];
559 int param_offset[MAX_UNIFORMS * 4];
560
561 int *virtual_grf_sizes;
562 int virtual_grf_next;
563 int virtual_grf_array_size;
564 int *virtual_grf_def;
565 int *virtual_grf_use;
566 bool live_intervals_valid;
567
568 struct hash_table *variable_ht;
569 ir_variable *frag_color, *frag_data, *frag_depth;
570 int first_non_payload_grf;
571 int urb_setup[FRAG_ATTRIB_MAX];
572 bool kill_emitted;
573
574 /** @{ debug annotation info */
575 const char *current_annotation;
576 ir_instruction *base_ir;
577 /** @} */
578
579 bool failed;
580 char *fail_msg;
581
582 /* On entry to a visit() method, this is the storage for the
583 * result. On exit, the visit() called may have changed it, in
584 * which case the parent must use the new storage instead.
585 */
586 fs_reg result;
587
588 fs_reg pixel_x;
589 fs_reg pixel_y;
590 fs_reg wpos_w;
591 fs_reg pixel_w;
592 fs_reg delta_x;
593 fs_reg delta_y;
594 fs_reg reg_null_cmp;
595
596 int grf_used;
597
598 int force_uncompressed_stack;
599 int force_sechalf_stack;
600 };
601
602 GLboolean brw_do_channel_expressions(struct exec_list *instructions);
603 GLboolean brw_do_vector_splitting(struct exec_list *instructions);
604 bool brw_fs_precompile(struct gl_context *ctx, struct gl_shader_program *prog);