OSDN Git Service

1f6c5635db2c0f2307ec66c8078f60f6f4fa2f0e
[android-x86/external-mesa.git] / src / mesa / drivers / dri / i965 / brw_vec4.h
1 /*
2  * Copyright © 2011 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
24 #ifndef BRW_VEC4_H
25 #define BRW_VEC4_H
26
27 #include <stdint.h>
28 #include "brw_shader.h"
29 #include "main/compiler.h"
30 #include "program/hash_table.h"
31
32 extern "C" {
33 #include "brw_vs.h"
34 #include "brw_context.h"
35 #include "brw_eu.h"
36 };
37
38 #include "glsl/ir.h"
39
40 namespace brw {
41
42 class dst_reg;
43
44 unsigned
45 swizzle_for_size(int size);
46
47 enum register_file {
48    ARF = BRW_ARCHITECTURE_REGISTER_FILE,
49    GRF = BRW_GENERAL_REGISTER_FILE,
50    MRF = BRW_MESSAGE_REGISTER_FILE,
51    IMM = BRW_IMMEDIATE_VALUE,
52    HW_REG, /* a struct brw_reg */
53    ATTR,
54    UNIFORM, /* prog_data->params[hw_reg] */
55    BAD_FILE
56 };
57
58 class reg
59 {
60 public:
61    /** Register file: ARF, GRF, MRF, IMM. */
62    enum register_file file;
63    /** virtual register number.  0 = fixed hw reg */
64    int reg;
65    /** Offset within the virtual register. */
66    int reg_offset;
67    /** Register type.  BRW_REGISTER_TYPE_* */
68    int type;
69    struct brw_reg fixed_hw_reg;
70
71    /** Value for file == BRW_IMMMEDIATE_FILE */
72    union {
73       int32_t i;
74       uint32_t u;
75       float f;
76    } imm;
77 };
78
79 class src_reg : public reg
80 {
81 public:
82    /* Callers of this ralloc-based new need not call delete. It's
83     * easier to just ralloc_free 'ctx' (or any of its ancestors). */
84    static void* operator new(size_t size, void *ctx)
85    {
86       void *node;
87
88       node = ralloc_size(ctx, size);
89       assert(node != NULL);
90
91       return node;
92    }
93
94    void init();
95
96    src_reg(register_file file, int reg, const glsl_type *type);
97    src_reg();
98    src_reg(float f);
99    src_reg(uint32_t u);
100    src_reg(int32_t i);
101
102    bool equals(src_reg *r);
103    bool is_zero() const;
104    bool is_one() const;
105
106    src_reg(class vec4_visitor *v, const struct glsl_type *type);
107
108    explicit src_reg(dst_reg reg);
109
110    GLuint swizzle; /**< SWIZZLE_XYZW swizzles from Mesa. */
111    bool negate;
112    bool abs;
113
114    src_reg *reladdr;
115 };
116
117 class dst_reg : public reg
118 {
119 public:
120    /* Callers of this ralloc-based new need not call delete. It's
121     * easier to just ralloc_free 'ctx' (or any of its ancestors). */
122    static void* operator new(size_t size, void *ctx)
123    {
124       void *node;
125
126       node = ralloc_size(ctx, size);
127       assert(node != NULL);
128
129       return node;
130    }
131
132    void init();
133
134    dst_reg();
135    dst_reg(register_file file, int reg);
136    dst_reg(register_file file, int reg, const glsl_type *type, int writemask);
137    dst_reg(struct brw_reg reg);
138    dst_reg(class vec4_visitor *v, const struct glsl_type *type);
139
140    explicit dst_reg(src_reg reg);
141
142    int writemask; /**< Bitfield of WRITEMASK_[XYZW] */
143
144    src_reg *reladdr;
145 };
146
147 class vec4_instruction : public backend_instruction {
148 public:
149    /* Callers of this ralloc-based new need not call delete. It's
150     * easier to just ralloc_free 'ctx' (or any of its ancestors). */
151    static void* operator new(size_t size, void *ctx)
152    {
153       void *node;
154
155       node = rzalloc_size(ctx, size);
156       assert(node != NULL);
157
158       return node;
159    }
160
161    vec4_instruction(vec4_visitor *v, enum opcode opcode,
162                     dst_reg dst = dst_reg(),
163                     src_reg src0 = src_reg(),
164                     src_reg src1 = src_reg(),
165                     src_reg src2 = src_reg());
166
167    struct brw_reg get_dst(void);
168    struct brw_reg get_src(int i);
169
170    dst_reg dst;
171    src_reg src[3];
172
173    bool saturate;
174    bool force_writemask_all;
175    bool no_dd_clear, no_dd_check;
176
177    int conditional_mod; /**< BRW_CONDITIONAL_* */
178
179    int sampler;
180    uint32_t texture_offset; /**< Texture Offset bitfield */
181    int target; /**< MRT target. */
182    bool shadow_compare;
183
184    bool eot;
185    bool header_present;
186    int mlen; /**< SEND message length */
187    int base_mrf; /**< First MRF in the SEND message, if mlen is nonzero. */
188
189    uint32_t offset; /* spill/unspill offset */
190    /** @{
191     * Annotation for the generated IR.  One of the two can be set.
192     */
193    const void *ir;
194    const char *annotation;
195
196    bool is_tex();
197    bool is_math();
198    bool is_send_from_grf();
199    bool can_reswizzle_dst(int dst_writemask, int swizzle, int swizzle_mask);
200    void reswizzle_dst(int dst_writemask, int swizzle);
201 };
202
203 /**
204  * The vertex shader front-end.
205  *
206  * Translates either GLSL IR or Mesa IR (for ARB_vertex_program and
207  * fixed-function) into VS IR.
208  */
209 class vec4_visitor : public backend_visitor
210 {
211 public:
212    vec4_visitor(struct brw_context *brw,
213                 struct brw_vec4_compile *c,
214                 struct gl_program *prog,
215                 const struct brw_vec4_prog_key *key,
216                 struct brw_vec4_prog_data *prog_data,
217                 struct gl_shader_program *shader_prog,
218                 struct brw_shader *shader,
219                 void *mem_ctx);
220    ~vec4_visitor();
221
222    dst_reg dst_null_f()
223    {
224       return dst_reg(brw_null_reg());
225    }
226
227    dst_reg dst_null_d()
228    {
229       return dst_reg(retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
230    }
231
232    struct gl_program *prog;
233    struct brw_vec4_compile *c;
234    const struct brw_vec4_prog_key *key;
235    struct brw_vec4_prog_data *prog_data;
236    unsigned int sanity_param_count;
237
238    char *fail_msg;
239    bool failed;
240
241    /**
242     * GLSL IR currently being processed, which is associated with our
243     * driver IR instructions for debugging purposes.
244     */
245    const void *base_ir;
246    const char *current_annotation;
247
248    int *virtual_grf_sizes;
249    int virtual_grf_count;
250    int virtual_grf_array_size;
251    int first_non_payload_grf;
252    unsigned int max_grf;
253    int *virtual_grf_def;
254    int *virtual_grf_use;
255    dst_reg userplane[MAX_CLIP_PLANES];
256
257    /**
258     * This is the size to be used for an array with an element per
259     * reg_offset
260     */
261    int virtual_grf_reg_count;
262    /** Per-virtual-grf indices into an array of size virtual_grf_reg_count */
263    int *virtual_grf_reg_map;
264
265    bool live_intervals_valid;
266
267    dst_reg *variable_storage(ir_variable *var);
268
269    void reladdr_to_temp(ir_instruction *ir, src_reg *reg, int *num_reladdr);
270
271    bool need_all_constants_in_pull_buffer;
272
273    /**
274     * \name Visit methods
275     *
276     * As typical for the visitor pattern, there must be one \c visit method for
277     * each concrete subclass of \c ir_instruction.  Virtual base classes within
278     * the hierarchy should not have \c visit methods.
279     */
280    /*@{*/
281    virtual void visit(ir_variable *);
282    virtual void visit(ir_loop *);
283    virtual void visit(ir_loop_jump *);
284    virtual void visit(ir_function_signature *);
285    virtual void visit(ir_function *);
286    virtual void visit(ir_expression *);
287    virtual void visit(ir_swizzle *);
288    virtual void visit(ir_dereference_variable  *);
289    virtual void visit(ir_dereference_array *);
290    virtual void visit(ir_dereference_record *);
291    virtual void visit(ir_assignment *);
292    virtual void visit(ir_constant *);
293    virtual void visit(ir_call *);
294    virtual void visit(ir_return *);
295    virtual void visit(ir_discard *);
296    virtual void visit(ir_texture *);
297    virtual void visit(ir_if *);
298    /*@}*/
299
300    src_reg result;
301
302    /* Regs for vertex results.  Generated at ir_variable visiting time
303     * for the ir->location's used.
304     */
305    dst_reg output_reg[BRW_VARYING_SLOT_COUNT];
306    const char *output_reg_annotation[BRW_VARYING_SLOT_COUNT];
307    int uniform_size[MAX_UNIFORMS];
308    int uniform_vector_size[MAX_UNIFORMS];
309    int uniforms;
310
311    src_reg shader_start_time;
312
313    struct hash_table *variable_ht;
314
315    bool run(void);
316    void fail(const char *msg, ...);
317
318    int virtual_grf_alloc(int size);
319    void setup_uniform_clipplane_values();
320    void setup_uniform_values(ir_variable *ir);
321    void setup_builtin_uniform_values(ir_variable *ir);
322    int setup_uniforms(int payload_reg);
323    void setup_payload();
324    bool reg_allocate_trivial();
325    bool reg_allocate();
326    void evaluate_spill_costs(float *spill_costs, bool *no_spill);
327    int choose_spill_reg(struct ra_graph *g);
328    void spill_reg(int spill_reg);
329    void move_grf_array_access_to_scratch();
330    void move_uniform_array_access_to_pull_constants();
331    void move_push_constants_to_pull_constants();
332    void split_uniform_registers();
333    void pack_uniform_registers();
334    void calculate_live_intervals();
335    void split_virtual_grfs();
336    bool dead_code_eliminate();
337    bool virtual_grf_interferes(int a, int b);
338    bool opt_copy_propagation();
339    bool opt_algebraic();
340    bool opt_register_coalesce();
341    void opt_set_dependency_control();
342
343    bool can_do_source_mods(vec4_instruction *inst);
344
345    vec4_instruction *emit(vec4_instruction *inst);
346
347    vec4_instruction *emit(enum opcode opcode);
348
349    vec4_instruction *emit(enum opcode opcode, dst_reg dst, src_reg src0);
350
351    vec4_instruction *emit(enum opcode opcode, dst_reg dst,
352                           src_reg src0, src_reg src1);
353
354    vec4_instruction *emit(enum opcode opcode, dst_reg dst,
355                           src_reg src0, src_reg src1, src_reg src2);
356
357    vec4_instruction *emit_before(vec4_instruction *inst,
358                                  vec4_instruction *new_inst);
359
360    vec4_instruction *MOV(dst_reg dst, src_reg src0);
361    vec4_instruction *NOT(dst_reg dst, src_reg src0);
362    vec4_instruction *RNDD(dst_reg dst, src_reg src0);
363    vec4_instruction *RNDE(dst_reg dst, src_reg src0);
364    vec4_instruction *RNDZ(dst_reg dst, src_reg src0);
365    vec4_instruction *FRC(dst_reg dst, src_reg src0);
366    vec4_instruction *F32TO16(dst_reg dst, src_reg src0);
367    vec4_instruction *F16TO32(dst_reg dst, src_reg src0);
368    vec4_instruction *ADD(dst_reg dst, src_reg src0, src_reg src1);
369    vec4_instruction *MUL(dst_reg dst, src_reg src0, src_reg src1);
370    vec4_instruction *MACH(dst_reg dst, src_reg src0, src_reg src1);
371    vec4_instruction *MAC(dst_reg dst, src_reg src0, src_reg src1);
372    vec4_instruction *AND(dst_reg dst, src_reg src0, src_reg src1);
373    vec4_instruction *OR(dst_reg dst, src_reg src0, src_reg src1);
374    vec4_instruction *XOR(dst_reg dst, src_reg src0, src_reg src1);
375    vec4_instruction *DP3(dst_reg dst, src_reg src0, src_reg src1);
376    vec4_instruction *DP4(dst_reg dst, src_reg src0, src_reg src1);
377    vec4_instruction *DPH(dst_reg dst, src_reg src0, src_reg src1);
378    vec4_instruction *SHL(dst_reg dst, src_reg src0, src_reg src1);
379    vec4_instruction *SHR(dst_reg dst, src_reg src0, src_reg src1);
380    vec4_instruction *ASR(dst_reg dst, src_reg src0, src_reg src1);
381    vec4_instruction *CMP(dst_reg dst, src_reg src0, src_reg src1,
382                          uint32_t condition);
383    vec4_instruction *IF(src_reg src0, src_reg src1, uint32_t condition);
384    vec4_instruction *IF(uint32_t predicate);
385    vec4_instruction *PULL_CONSTANT_LOAD(dst_reg dst, src_reg index);
386    vec4_instruction *SCRATCH_READ(dst_reg dst, src_reg index);
387    vec4_instruction *SCRATCH_WRITE(dst_reg dst, src_reg src, src_reg index);
388
389    int implied_mrf_writes(vec4_instruction *inst);
390
391    bool try_rewrite_rhs_to_dst(ir_assignment *ir,
392                                dst_reg dst,
393                                src_reg src,
394                                vec4_instruction *pre_rhs_inst,
395                                vec4_instruction *last_rhs_inst);
396
397    bool try_copy_propagation(struct intel_context *intel,
398                              vec4_instruction *inst, int arg,
399                              src_reg *values[4]);
400
401    /** Walks an exec_list of ir_instruction and sends it through this visitor. */
402    void visit_instructions(const exec_list *list);
403
404    void emit_vp_sop(uint32_t condmod, dst_reg dst,
405                     src_reg src0, src_reg src1, src_reg one);
406
407    void emit_bool_to_cond_code(ir_rvalue *ir, uint32_t *predicate);
408    void emit_bool_comparison(unsigned int op, dst_reg dst, src_reg src0, src_reg src1);
409    void emit_if_gen6(ir_if *ir);
410
411    void emit_minmax(uint32_t condmod, dst_reg dst, src_reg src0, src_reg src1);
412
413    void emit_block_move(dst_reg *dst, src_reg *src,
414                         const struct glsl_type *type, uint32_t predicate);
415
416    void emit_constant_values(dst_reg *dst, ir_constant *value);
417
418    /**
419     * Emit the correct dot-product instruction for the type of arguments
420     */
421    void emit_dp(dst_reg dst, src_reg src0, src_reg src1, unsigned elements);
422
423    void emit_scalar(ir_instruction *ir, enum prog_opcode op,
424                     dst_reg dst, src_reg src0);
425
426    void emit_scalar(ir_instruction *ir, enum prog_opcode op,
427                     dst_reg dst, src_reg src0, src_reg src1);
428
429    void emit_scs(ir_instruction *ir, enum prog_opcode op,
430                  dst_reg dst, const src_reg &src);
431
432    void emit_math1_gen6(enum opcode opcode, dst_reg dst, src_reg src);
433    void emit_math1_gen4(enum opcode opcode, dst_reg dst, src_reg src);
434    void emit_math(enum opcode opcode, dst_reg dst, src_reg src);
435    void emit_math2_gen6(enum opcode opcode, dst_reg dst, src_reg src0, src_reg src1);
436    void emit_math2_gen4(enum opcode opcode, dst_reg dst, src_reg src0, src_reg src1);
437    void emit_math(enum opcode opcode, dst_reg dst, src_reg src0, src_reg src1);
438    src_reg fix_math_operand(src_reg src);
439
440    void emit_pack_half_2x16(dst_reg dst, src_reg src0);
441    void emit_unpack_half_2x16(dst_reg dst, src_reg src0);
442
443    void swizzle_result(ir_texture *ir, src_reg orig_val, int sampler);
444
445    void emit_ndc_computation();
446    void emit_psiz_and_flags(struct brw_reg reg);
447    void emit_clip_distances(struct brw_reg reg, int offset);
448    void emit_generic_urb_slot(dst_reg reg, int varying);
449    void emit_urb_slot(int mrf, int varying);
450
451    void emit_shader_time_begin();
452    void emit_shader_time_end();
453    void emit_shader_time_write(enum shader_time_shader_type type,
454                                src_reg value);
455
456    src_reg get_scratch_offset(vec4_instruction *inst,
457                               src_reg *reladdr, int reg_offset);
458    src_reg get_pull_constant_offset(vec4_instruction *inst,
459                                     src_reg *reladdr, int reg_offset);
460    void emit_scratch_read(vec4_instruction *inst,
461                           dst_reg dst,
462                           src_reg orig_src,
463                           int base_offset);
464    void emit_scratch_write(vec4_instruction *inst,
465                            int base_offset);
466    void emit_pull_constant_load(vec4_instruction *inst,
467                                 dst_reg dst,
468                                 src_reg orig_src,
469                                 int base_offset);
470
471    bool try_emit_sat(ir_expression *ir);
472    void resolve_ud_negate(src_reg *reg);
473
474    src_reg get_timestamp();
475
476    bool process_move_condition(ir_rvalue *ir);
477
478    void dump_instruction(vec4_instruction *inst);
479    void dump_instructions();
480
481 protected:
482    void emit_vertex();
483    void lower_attributes_to_hw_regs(const int *attribute_map);
484    virtual dst_reg *make_reg_for_system_value(ir_variable *ir) = 0;
485    virtual int setup_attributes(int payload_reg) = 0;
486    virtual void emit_prolog() = 0;
487    virtual void emit_program_code() = 0;
488    virtual void emit_thread_end() = 0;
489    virtual void emit_urb_write_header(int mrf) = 0;
490    virtual vec4_instruction *emit_urb_write_opcode(bool complete) = 0;
491    virtual int compute_array_stride(ir_dereference_array *ir);
492 };
493
494 class vec4_vs_visitor : public vec4_visitor
495 {
496 public:
497    vec4_vs_visitor(struct brw_context *brw,
498                    struct brw_vs_compile *vs_compile,
499                    struct brw_vs_prog_data *vs_prog_data,
500                    struct gl_shader_program *prog,
501                    struct brw_shader *shader,
502                    void *mem_ctx);
503
504 protected:
505    virtual dst_reg *make_reg_for_system_value(ir_variable *ir);
506    virtual int setup_attributes(int payload_reg);
507    virtual void emit_prolog();
508    virtual void emit_program_code();
509    virtual void emit_thread_end();
510    virtual void emit_urb_write_header(int mrf);
511    virtual vec4_instruction *emit_urb_write_opcode(bool complete);
512
513 private:
514    void setup_vp_regs();
515    dst_reg get_vp_dst_reg(const prog_dst_register &dst);
516    src_reg get_vp_src_reg(const prog_src_register &src);
517
518    struct brw_vs_compile * const vs_compile;
519    struct brw_vs_prog_data * const vs_prog_data;
520    src_reg *vp_temp_regs;
521    src_reg vp_addr_reg;
522 };
523
524 /**
525  * The vertex shader code generator.
526  *
527  * Translates VS IR to actual i965 assembly code.
528  */
529 class vec4_generator
530 {
531 public:
532    vec4_generator(struct brw_context *brw,
533                   struct gl_shader_program *shader_prog,
534                   struct gl_program *prog,
535                   void *mem_ctx);
536    ~vec4_generator();
537
538    const unsigned *generate_assembly(exec_list *insts, unsigned *asm_size);
539
540 private:
541    void generate_code(exec_list *instructions);
542    void generate_vec4_instruction(vec4_instruction *inst,
543                                   struct brw_reg dst,
544                                   struct brw_reg *src);
545
546    void generate_math1_gen4(vec4_instruction *inst,
547                             struct brw_reg dst,
548                             struct brw_reg src);
549    void generate_math1_gen6(vec4_instruction *inst,
550                             struct brw_reg dst,
551                             struct brw_reg src);
552    void generate_math2_gen4(vec4_instruction *inst,
553                             struct brw_reg dst,
554                             struct brw_reg src0,
555                             struct brw_reg src1);
556    void generate_math2_gen6(vec4_instruction *inst,
557                             struct brw_reg dst,
558                             struct brw_reg src0,
559                             struct brw_reg src1);
560    void generate_math2_gen7(vec4_instruction *inst,
561                             struct brw_reg dst,
562                             struct brw_reg src0,
563                             struct brw_reg src1);
564
565    void generate_tex(vec4_instruction *inst,
566                      struct brw_reg dst,
567                      struct brw_reg src);
568
569    void generate_urb_write(vec4_instruction *inst);
570    void generate_oword_dual_block_offsets(struct brw_reg m1,
571                                           struct brw_reg index);
572    void generate_scratch_write(vec4_instruction *inst,
573                                struct brw_reg dst,
574                                struct brw_reg src,
575                                struct brw_reg index);
576    void generate_scratch_read(vec4_instruction *inst,
577                               struct brw_reg dst,
578                               struct brw_reg index);
579    void generate_pull_constant_load(vec4_instruction *inst,
580                                     struct brw_reg dst,
581                                     struct brw_reg index,
582                                     struct brw_reg offset);
583    void generate_pull_constant_load_gen7(vec4_instruction *inst,
584                                          struct brw_reg dst,
585                                          struct brw_reg surf_index,
586                                          struct brw_reg offset);
587
588    struct brw_context *brw;
589    struct intel_context *intel;
590    struct gl_context *ctx;
591
592    struct brw_compile *p;
593
594    struct gl_shader_program *shader_prog;
595    struct gl_shader *shader;
596    const struct gl_program *prog;
597
598    void *mem_ctx;
599 };
600
601 } /* namespace brw */
602
603 #endif /* BRW_VEC4_H */