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