brw_fs.h revision 4847f802c28e595130bda14055cd52c9b1f51cd7
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
28extern "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
48enum 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
58enum 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
84class fs_reg {
85public:
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
190static const fs_reg reg_undef;
191static const fs_reg reg_null_f(ARF, BRW_ARF_NULL, BRW_REGISTER_TYPE_F);
192static const fs_reg reg_null_d(ARF, BRW_ARF_NULL, BRW_REGISTER_TYPE_D);
193
194class fs_inst : public exec_node {
195public:
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   bool predicate_inverse;
335   int conditional_mod; /**< BRW_CONDITIONAL_* */
336
337   int mlen; /**< SEND message length */
338   int base_mrf; /**< First MRF in the SEND message, if mlen is nonzero. */
339   int sampler;
340   int target; /**< MRT target. */
341   bool eot;
342   bool header_present;
343   bool shadow_compare;
344   uint32_t offset; /* spill/unspill offset */
345
346   /** @{
347    * Annotation for the generated IR.  One of the two can be set.
348    */
349   ir_instruction *ir;
350   const char *annotation;
351   /** @} */
352};
353
354class fs_visitor : public ir_visitor
355{
356public:
357
358   fs_visitor(struct brw_wm_compile *c, struct brw_shader *shader)
359   {
360      this->c = c;
361      this->p = &c->func;
362      this->brw = p->brw;
363      this->fp = brw->fragment_program;
364      this->intel = &brw->intel;
365      this->ctx = &intel->ctx;
366      this->mem_ctx = ralloc_context(NULL);
367      this->shader = shader;
368      this->failed = false;
369      this->variable_ht = hash_table_ctor(0,
370					  hash_table_pointer_hash,
371					  hash_table_pointer_compare);
372
373      /* There's a question that appears to be left open in the spec:
374       * How do implicit dst conversions interact with the CMP
375       * instruction or conditional mods?  On gen6, the instruction:
376       *
377       * CMP null<d> src0<f> src1<f>
378       *
379       * will do src1 - src0 and compare that result as if it was an
380       * integer.  On gen4, it will do src1 - src0 as float, convert
381       * the result to int, and compare as int.  In between, it
382       * appears that it does src1 - src0 and does the compare in the
383       * execution type so dst type doesn't matter.
384       */
385      if (this->intel->gen > 4)
386	 this->reg_null_cmp = reg_null_d;
387      else
388	 this->reg_null_cmp = reg_null_f;
389
390      this->frag_color = NULL;
391      this->frag_data = NULL;
392      this->frag_depth = NULL;
393      this->first_non_payload_grf = 0;
394
395      this->current_annotation = NULL;
396      this->base_ir = NULL;
397
398      this->virtual_grf_sizes = NULL;
399      this->virtual_grf_next = 1;
400      this->virtual_grf_array_size = 0;
401      this->virtual_grf_def = NULL;
402      this->virtual_grf_use = NULL;
403      this->live_intervals_valid = false;
404
405      this->kill_emitted = false;
406   }
407
408   ~fs_visitor()
409   {
410      ralloc_free(this->mem_ctx);
411      hash_table_dtor(this->variable_ht);
412   }
413
414   fs_reg *variable_storage(ir_variable *var);
415   int virtual_grf_alloc(int size);
416
417   void visit(ir_variable *ir);
418   void visit(ir_assignment *ir);
419   void visit(ir_dereference_variable *ir);
420   void visit(ir_dereference_record *ir);
421   void visit(ir_dereference_array *ir);
422   void visit(ir_expression *ir);
423   void visit(ir_texture *ir);
424   void visit(ir_if *ir);
425   void visit(ir_constant *ir);
426   void visit(ir_swizzle *ir);
427   void visit(ir_return *ir);
428   void visit(ir_loop *ir);
429   void visit(ir_loop_jump *ir);
430   void visit(ir_discard *ir);
431   void visit(ir_call *ir);
432   void visit(ir_function *ir);
433   void visit(ir_function_signature *ir);
434
435   fs_inst *emit(fs_inst inst);
436
437   fs_inst *emit(int opcode)
438   {
439      return emit(fs_inst(opcode));
440   }
441
442   fs_inst *emit(int opcode, fs_reg dst)
443   {
444      return emit(fs_inst(opcode, dst));
445   }
446
447   fs_inst *emit(int opcode, fs_reg dst, fs_reg src0)
448   {
449      return emit(fs_inst(opcode, dst, src0));
450   }
451
452   fs_inst *emit(int opcode, fs_reg dst, fs_reg src0, fs_reg src1)
453   {
454      return emit(fs_inst(opcode, dst, src0, src1));
455   }
456
457   fs_inst *emit(int opcode, fs_reg dst, fs_reg src0, fs_reg src1, fs_reg src2)
458   {
459      return emit(fs_inst(opcode, dst, src0, src1, src2));
460   }
461
462   void setup_paramvalues_refs();
463   void assign_curb_setup();
464   void calculate_urb_setup();
465   void assign_urb_setup();
466   bool assign_regs();
467   void assign_regs_trivial();
468   int choose_spill_reg(struct ra_graph *g);
469   void spill_reg(int spill_reg);
470   void split_virtual_grfs();
471   void setup_pull_constants();
472   void calculate_live_intervals();
473   bool propagate_constants();
474   bool register_coalesce();
475   bool compute_to_mrf();
476   bool dead_code_eliminate();
477   bool remove_duplicate_mrf_writes();
478   bool virtual_grf_interferes(int a, int b);
479   void schedule_instructions();
480   void fail(const char *msg, ...);
481
482   void generate_code();
483   void generate_fb_write(fs_inst *inst);
484   void generate_linterp(fs_inst *inst, struct brw_reg dst,
485			 struct brw_reg *src);
486   void generate_tex(fs_inst *inst, struct brw_reg dst, struct brw_reg src);
487   void generate_math(fs_inst *inst, struct brw_reg dst, struct brw_reg *src);
488   void generate_discard_not(fs_inst *inst, struct brw_reg temp);
489   void generate_discard_and(fs_inst *inst, struct brw_reg temp);
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   fs_inst *emit_texture_gen5(ir_texture *ir, fs_reg dst, fs_reg coordinate);
504   fs_inst *emit_math(fs_opcodes op, fs_reg dst, fs_reg src0);
505   fs_inst *emit_math(fs_opcodes op, fs_reg dst, fs_reg src0, fs_reg src1);
506   bool try_emit_saturate(ir_expression *ir);
507   void emit_bool_to_cond_code(ir_rvalue *condition);
508   void emit_if_gen6(ir_if *ir);
509   void emit_unspill(fs_inst *inst, fs_reg reg, uint32_t spill_offset);
510
511   void emit_fb_writes();
512   void emit_assignment_writes(fs_reg &l, fs_reg &r,
513			       const glsl_type *type, bool predicated);
514
515   struct brw_reg interp_reg(int location, int channel);
516   int setup_uniform_values(int loc, const glsl_type *type);
517   void setup_builtin_uniform_values(ir_variable *ir);
518   int implied_mrf_writes(fs_inst *inst);
519
520   struct brw_context *brw;
521   const struct gl_fragment_program *fp;
522   struct intel_context *intel;
523   struct gl_context *ctx;
524   struct brw_wm_compile *c;
525   struct brw_compile *p;
526   struct brw_shader *shader;
527   void *mem_ctx;
528   exec_list instructions;
529
530   /* Delayed setup of c->prog_data.params[] due to realloc of
531    * ParamValues[] during compile.
532    */
533   int param_index[MAX_UNIFORMS * 4];
534   int param_offset[MAX_UNIFORMS * 4];
535
536   int *virtual_grf_sizes;
537   int virtual_grf_next;
538   int virtual_grf_array_size;
539   int *virtual_grf_def;
540   int *virtual_grf_use;
541   bool live_intervals_valid;
542
543   struct hash_table *variable_ht;
544   ir_variable *frag_color, *frag_data, *frag_depth;
545   int first_non_payload_grf;
546   int urb_setup[FRAG_ATTRIB_MAX];
547   bool kill_emitted;
548
549   /** @{ debug annotation info */
550   const char *current_annotation;
551   ir_instruction *base_ir;
552   /** @} */
553
554   bool failed;
555
556   /* Result of last visit() method. */
557   fs_reg result;
558
559   fs_reg pixel_x;
560   fs_reg pixel_y;
561   fs_reg wpos_w;
562   fs_reg pixel_w;
563   fs_reg delta_x;
564   fs_reg delta_y;
565   fs_reg reg_null_cmp;
566
567   int grf_used;
568};
569
570GLboolean brw_do_channel_expressions(struct exec_list *instructions);
571GLboolean brw_do_vector_splitting(struct exec_list *instructions);
572