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