i965/nir/fs: Implement nir_intrinsic_ssbo_atomic_*
[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 #pragma once
29
30 #include "brw_shader.h"
31 #include "brw_ir_fs.h"
32 #include "brw_fs_builder.h"
33
34 extern "C" {
35
36 #include <sys/types.h>
37
38 #include "main/macros.h"
39 #include "main/shaderobj.h"
40 #include "main/uniforms.h"
41 #include "program/prog_parameter.h"
42 #include "program/prog_print.h"
43 #include "program/prog_optimize.h"
44 #include "util/register_allocate.h"
45 #include "program/hash_table.h"
46 #include "brw_context.h"
47 #include "brw_eu.h"
48 #include "brw_wm.h"
49 #include "intel_asm_annotation.h"
50 }
51 #include "glsl/glsl_types.h"
52 #include "glsl/ir.h"
53 #include "glsl/nir/nir.h"
54 #include "program/sampler.h"
55
56 struct bblock_t;
57 namespace {
58 struct acp_entry;
59 }
60
61 namespace brw {
62 class fs_live_variables;
63 }
64
65 static inline fs_reg
66 offset(fs_reg reg, const brw::fs_builder& bld, unsigned delta)
67 {
68 switch (reg.file) {
69 case BAD_FILE:
70 break;
71 case GRF:
72 case MRF:
73 case HW_REG:
74 case ATTR:
75 return byte_offset(reg,
76 delta * reg.component_size(bld.dispatch_width()));
77 case UNIFORM:
78 reg.reg_offset += delta;
79 break;
80 case IMM:
81 assert(delta == 0);
82 }
83 return reg;
84 }
85
86 /**
87 * The fragment shader front-end.
88 *
89 * Translates either GLSL IR or Mesa IR (for ARB_fragment_program) into FS IR.
90 */
91 class fs_visitor : public backend_shader
92 {
93 public:
94 fs_visitor(const struct brw_compiler *compiler, void *log_data,
95 void *mem_ctx,
96 gl_shader_stage stage,
97 const void *key,
98 struct brw_stage_prog_data *prog_data,
99 struct gl_shader_program *shader_prog,
100 struct gl_program *prog,
101 unsigned dispatch_width,
102 int shader_time_index);
103
104 ~fs_visitor();
105
106 fs_reg vgrf(const glsl_type *const type);
107 void import_uniforms(fs_visitor *v);
108 void setup_uniform_clipplane_values(gl_clip_plane *clip_planes);
109 void compute_clip_distance(gl_clip_plane *clip_planes);
110
111 uint32_t gather_channel(int orig_chan, uint32_t sampler);
112 void swizzle_result(ir_texture_opcode op, int dest_components,
113 fs_reg orig_val, uint32_t sampler);
114
115 fs_inst *get_instruction_generating_reg(fs_inst *start,
116 fs_inst *end,
117 const fs_reg &reg);
118
119 void VARYING_PULL_CONSTANT_LOAD(const brw::fs_builder &bld,
120 const fs_reg &dst,
121 const fs_reg &surf_index,
122 const fs_reg &varying_offset,
123 uint32_t const_offset);
124 void DEP_RESOLVE_MOV(const brw::fs_builder &bld, int grf);
125
126 bool run_fs(bool do_rep_send);
127 bool run_vs(gl_clip_plane *clip_planes);
128 bool run_cs();
129 void optimize();
130 void allocate_registers();
131 void assign_binding_table_offsets();
132 void setup_payload_gen4();
133 void setup_payload_gen6();
134 void setup_vs_payload();
135 void setup_cs_payload();
136 void fixup_3src_null_dest();
137 void assign_curb_setup();
138 void calculate_urb_setup();
139 void assign_urb_setup();
140 void assign_vs_urb_setup();
141 bool assign_regs(bool allow_spilling);
142 void assign_regs_trivial();
143 void setup_payload_interference(struct ra_graph *g, int payload_reg_count,
144 int first_payload_node);
145 int choose_spill_reg(struct ra_graph *g);
146 void spill_reg(int spill_reg);
147 void split_virtual_grfs();
148 bool compact_virtual_grfs();
149 void assign_constant_locations();
150 void demote_pull_constants();
151 void invalidate_live_intervals();
152 void calculate_live_intervals();
153 void calculate_register_pressure();
154 void validate();
155 bool opt_algebraic();
156 bool opt_redundant_discard_jumps();
157 bool opt_cse();
158 bool opt_cse_local(bblock_t *block);
159 bool opt_copy_propagate();
160 bool try_copy_propagate(fs_inst *inst, int arg, acp_entry *entry);
161 bool try_constant_propagate(fs_inst *inst, acp_entry *entry);
162 bool opt_copy_propagate_local(void *mem_ctx, bblock_t *block,
163 exec_list *acp);
164 bool opt_register_renaming();
165 bool register_coalesce();
166 bool compute_to_mrf();
167 bool eliminate_find_live_channel();
168 bool dead_code_eliminate();
169 bool remove_duplicate_mrf_writes();
170
171 bool opt_sampler_eot();
172 bool virtual_grf_interferes(int a, int b);
173 void schedule_instructions(instruction_scheduler_mode mode);
174 void insert_gen4_send_dependency_workarounds();
175 void insert_gen4_pre_send_dependency_workarounds(bblock_t *block,
176 fs_inst *inst);
177 void insert_gen4_post_send_dependency_workarounds(bblock_t *block,
178 fs_inst *inst);
179 void vfail(const char *msg, va_list args);
180 void fail(const char *msg, ...);
181 void no16(const char *msg);
182 void lower_uniform_pull_constant_loads();
183 bool lower_load_payload();
184 bool lower_logical_sends();
185 bool lower_integer_multiplication();
186 bool lower_simd_width();
187 bool opt_combine_constants();
188
189 void emit_dummy_fs();
190 void emit_repclear_shader();
191 fs_reg *emit_fragcoord_interpolation(bool pixel_center_integer,
192 bool origin_upper_left);
193 fs_inst *emit_linterp(const fs_reg &attr, const fs_reg &interp,
194 glsl_interp_qualifier interpolation_mode,
195 bool is_centroid, bool is_sample);
196 fs_reg *emit_frontfacing_interpolation();
197 fs_reg *emit_samplepos_setup();
198 fs_reg *emit_sampleid_setup();
199 void emit_general_interpolation(fs_reg attr, const char *name,
200 const glsl_type *type,
201 glsl_interp_qualifier interpolation_mode,
202 int location, bool mod_centroid,
203 bool mod_sample);
204 fs_reg *emit_vs_system_value(int location);
205 void emit_interpolation_setup_gen4();
206 void emit_interpolation_setup_gen6();
207 void compute_sample_position(fs_reg dst, fs_reg int_sample_pos);
208 fs_reg rescale_texcoord(fs_reg coordinate, int coord_components,
209 bool is_rect, uint32_t sampler, int texunit);
210 void emit_texture(ir_texture_opcode op,
211 const glsl_type *dest_type,
212 fs_reg coordinate, int components,
213 fs_reg shadow_c,
214 fs_reg lod, fs_reg dpdy, int grad_components,
215 fs_reg sample_index,
216 fs_reg offset,
217 fs_reg mcs,
218 int gather_component,
219 bool is_cube_array,
220 bool is_rect,
221 uint32_t sampler,
222 fs_reg sampler_reg,
223 int texunit);
224 fs_reg emit_mcs_fetch(const fs_reg &coordinate, unsigned components,
225 const fs_reg &sampler);
226 void emit_gen6_gather_wa(uint8_t wa, fs_reg dst);
227 fs_reg resolve_source_modifiers(const fs_reg &src);
228 void emit_discard_jump();
229 bool opt_peephole_sel();
230 bool opt_peephole_predicated_break();
231 bool opt_saturate_propagation();
232 bool opt_cmod_propagation();
233 bool opt_zero_samples();
234 void emit_unspill(bblock_t *block, fs_inst *inst, fs_reg reg,
235 uint32_t spill_offset, int count);
236 void emit_spill(bblock_t *block, fs_inst *inst, fs_reg reg,
237 uint32_t spill_offset, int count);
238
239 void emit_nir_code();
240 void nir_setup_inputs(nir_shader *shader);
241 void nir_setup_outputs(nir_shader *shader);
242 void nir_setup_uniforms(nir_shader *shader);
243 void nir_setup_uniform(nir_variable *var);
244 void nir_setup_builtin_uniform(nir_variable *var);
245 void nir_emit_system_values(nir_shader *shader);
246 void nir_emit_impl(nir_function_impl *impl);
247 void nir_emit_cf_list(exec_list *list);
248 void nir_emit_if(nir_if *if_stmt);
249 void nir_emit_loop(nir_loop *loop);
250 void nir_emit_block(nir_block *block);
251 void nir_emit_instr(nir_instr *instr);
252 void nir_emit_alu(const brw::fs_builder &bld, nir_alu_instr *instr);
253 void nir_emit_load_const(const brw::fs_builder &bld,
254 nir_load_const_instr *instr);
255 void nir_emit_undef(const brw::fs_builder &bld,
256 nir_ssa_undef_instr *instr);
257 void nir_emit_intrinsic(const brw::fs_builder &bld,
258 nir_intrinsic_instr *instr);
259 void nir_emit_ssbo_atomic(const brw::fs_builder &bld,
260 int op, nir_intrinsic_instr *instr);
261 void nir_emit_texture(const brw::fs_builder &bld,
262 nir_tex_instr *instr);
263 void nir_emit_jump(const brw::fs_builder &bld,
264 nir_jump_instr *instr);
265 fs_reg get_nir_src(nir_src src);
266 fs_reg get_nir_dest(nir_dest dest);
267 fs_reg get_nir_image_deref(const nir_deref_var *deref);
268 void emit_percomp(const brw::fs_builder &bld, const fs_inst &inst,
269 unsigned wr_mask);
270
271 bool optimize_frontfacing_ternary(nir_alu_instr *instr,
272 const fs_reg &result);
273
274 void emit_alpha_test();
275 fs_inst *emit_single_fb_write(const brw::fs_builder &bld,
276 fs_reg color1, fs_reg color2,
277 fs_reg src0_alpha, unsigned components);
278 void emit_fb_writes();
279 void emit_urb_writes();
280 void emit_cs_terminate();
281 fs_reg *emit_cs_local_invocation_id_setup();
282 fs_reg *emit_cs_work_group_id_setup();
283
284 void emit_barrier();
285
286 void emit_shader_time_begin();
287 void emit_shader_time_end();
288 void SHADER_TIME_ADD(const brw::fs_builder &bld,
289 int shader_time_subindex,
290 fs_reg value);
291
292 fs_reg get_timestamp(const brw::fs_builder &bld);
293
294 struct brw_reg interp_reg(int location, int channel);
295
296 virtual void setup_vec4_uniform_value(unsigned param_offset,
297 const gl_constant_value *values,
298 unsigned n);
299
300 int implied_mrf_writes(fs_inst *inst);
301
302 virtual void dump_instructions();
303 virtual void dump_instructions(const char *name);
304 void dump_instruction(backend_instruction *inst);
305 void dump_instruction(backend_instruction *inst, FILE *file);
306
307 const void *const key;
308 const struct brw_sampler_prog_key_data *key_tex;
309
310 struct brw_stage_prog_data *prog_data;
311 unsigned int sanity_param_count;
312
313 int *param_size;
314
315 int *virtual_grf_start;
316 int *virtual_grf_end;
317 brw::fs_live_variables *live_intervals;
318
319 int *regs_live_at_ip;
320
321 /** Number of uniform variable components visited. */
322 unsigned uniforms;
323
324 /** Byte-offset for the next available spot in the scratch space buffer. */
325 unsigned last_scratch;
326
327 /**
328 * Array mapping UNIFORM register numbers to the pull parameter index,
329 * or -1 if this uniform register isn't being uploaded as a pull constant.
330 */
331 int *pull_constant_loc;
332
333 /**
334 * Array mapping UNIFORM register numbers to the push parameter index,
335 * or -1 if this uniform register isn't being uploaded as a push constant.
336 */
337 int *push_constant_loc;
338
339 fs_reg frag_depth;
340 fs_reg sample_mask;
341 fs_reg outputs[VARYING_SLOT_MAX];
342 unsigned output_components[VARYING_SLOT_MAX];
343 fs_reg dual_src_output;
344 bool do_dual_src;
345 int first_non_payload_grf;
346 /** Either BRW_MAX_GRF or GEN7_MRF_HACK_START */
347 unsigned max_grf;
348
349 fs_reg *nir_locals;
350 fs_reg *nir_ssa_values;
351 fs_reg nir_inputs;
352 fs_reg nir_outputs;
353 fs_reg *nir_system_values;
354
355 bool failed;
356 char *fail_msg;
357 bool simd16_unsupported;
358 char *no16_msg;
359
360 /* Result of last visit() method. Still used by emit_texture() */
361 fs_reg result;
362
363 /** Register numbers for thread payload fields. */
364 struct thread_payload {
365 uint8_t source_depth_reg;
366 uint8_t source_w_reg;
367 uint8_t aa_dest_stencil_reg;
368 uint8_t dest_depth_reg;
369 uint8_t sample_pos_reg;
370 uint8_t sample_mask_in_reg;
371 uint8_t barycentric_coord_reg[BRW_WM_BARYCENTRIC_INTERP_MODE_COUNT];
372 uint8_t local_invocation_id_reg;
373
374 /** The number of thread payload registers the hardware will supply. */
375 uint8_t num_regs;
376 } payload;
377
378 bool source_depth_to_render_target;
379 bool runtime_check_aads_emit;
380
381 fs_reg pixel_x;
382 fs_reg pixel_y;
383 fs_reg wpos_w;
384 fs_reg pixel_w;
385 fs_reg delta_xy[BRW_WM_BARYCENTRIC_INTERP_MODE_COUNT];
386 fs_reg shader_start_time;
387 fs_reg userplane[MAX_CLIP_PLANES];
388
389 unsigned grf_used;
390 bool spilled_any_registers;
391
392 const unsigned dispatch_width; /**< 8 or 16 */
393
394 int shader_time_index;
395
396 unsigned promoted_constants;
397 brw::fs_builder bld;
398 };
399
400 /**
401 * The fragment shader code generator.
402 *
403 * Translates FS IR to actual i965 assembly code.
404 */
405 class fs_generator
406 {
407 public:
408 fs_generator(const struct brw_compiler *compiler, void *log_data,
409 void *mem_ctx,
410 const void *key,
411 struct brw_stage_prog_data *prog_data,
412 struct gl_program *fp,
413 unsigned promoted_constants,
414 bool runtime_check_aads_emit,
415 const char *stage_abbrev);
416 ~fs_generator();
417
418 void enable_debug(const char *shader_name);
419 int generate_code(const cfg_t *cfg, int dispatch_width);
420 const unsigned *get_assembly(unsigned int *assembly_size);
421
422 private:
423 void fire_fb_write(fs_inst *inst,
424 struct brw_reg payload,
425 struct brw_reg implied_header,
426 GLuint nr);
427 void generate_fb_write(fs_inst *inst, struct brw_reg payload);
428 void generate_urb_write(fs_inst *inst, struct brw_reg payload);
429 void generate_cs_terminate(fs_inst *inst, struct brw_reg payload);
430 void generate_barrier(fs_inst *inst, struct brw_reg src);
431 void generate_blorp_fb_write(fs_inst *inst);
432 void generate_linterp(fs_inst *inst, struct brw_reg dst,
433 struct brw_reg *src);
434 void generate_tex(fs_inst *inst, struct brw_reg dst, struct brw_reg src,
435 struct brw_reg sampler_index);
436 void generate_get_buffer_size(fs_inst *inst, struct brw_reg dst,
437 struct brw_reg src,
438 struct brw_reg surf_index);
439 void generate_math_gen6(fs_inst *inst,
440 struct brw_reg dst,
441 struct brw_reg src0,
442 struct brw_reg src1);
443 void generate_math_gen4(fs_inst *inst,
444 struct brw_reg dst,
445 struct brw_reg src);
446 void generate_math_g45(fs_inst *inst,
447 struct brw_reg dst,
448 struct brw_reg src);
449 void generate_ddx(enum opcode op, struct brw_reg dst, struct brw_reg src);
450 void generate_ddy(enum opcode op, struct brw_reg dst, struct brw_reg src,
451 bool negate_value);
452 void generate_scratch_write(fs_inst *inst, struct brw_reg src);
453 void generate_scratch_read(fs_inst *inst, struct brw_reg dst);
454 void generate_scratch_read_gen7(fs_inst *inst, struct brw_reg dst);
455 void generate_uniform_pull_constant_load(fs_inst *inst, struct brw_reg dst,
456 struct brw_reg index,
457 struct brw_reg offset);
458 void generate_uniform_pull_constant_load_gen7(fs_inst *inst,
459 struct brw_reg dst,
460 struct brw_reg surf_index,
461 struct brw_reg offset);
462 void generate_varying_pull_constant_load(fs_inst *inst, struct brw_reg dst,
463 struct brw_reg index,
464 struct brw_reg offset);
465 void generate_varying_pull_constant_load_gen7(fs_inst *inst,
466 struct brw_reg dst,
467 struct brw_reg index,
468 struct brw_reg offset);
469 void generate_mov_dispatch_to_flags(fs_inst *inst);
470
471 void generate_pixel_interpolator_query(fs_inst *inst,
472 struct brw_reg dst,
473 struct brw_reg src,
474 struct brw_reg msg_data,
475 unsigned msg_type);
476
477 void generate_set_sample_id(fs_inst *inst,
478 struct brw_reg dst,
479 struct brw_reg src0,
480 struct brw_reg src1);
481
482 void generate_set_simd4x2_offset(fs_inst *inst,
483 struct brw_reg dst,
484 struct brw_reg offset);
485 void generate_discard_jump(fs_inst *inst);
486
487 void generate_pack_half_2x16_split(fs_inst *inst,
488 struct brw_reg dst,
489 struct brw_reg x,
490 struct brw_reg y);
491 void generate_unpack_half_2x16_split(fs_inst *inst,
492 struct brw_reg dst,
493 struct brw_reg src);
494
495 void generate_shader_time_add(fs_inst *inst,
496 struct brw_reg payload,
497 struct brw_reg offset,
498 struct brw_reg value);
499
500 bool patch_discard_jumps_to_fb_writes();
501
502 const struct brw_compiler *compiler;
503 void *log_data; /* Passed to compiler->*_log functions */
504
505 const struct brw_device_info *devinfo;
506
507 struct brw_codegen *p;
508 const void * const key;
509 struct brw_stage_prog_data * const prog_data;
510
511 const struct gl_program *prog;
512
513 unsigned dispatch_width; /**< 8 or 16 */
514
515 exec_list discard_halt_patches;
516 unsigned promoted_constants;
517 bool runtime_check_aads_emit;
518 bool debug_flag;
519 const char *shader_name;
520 const char *stage_abbrev;
521 void *mem_ctx;
522 };
523
524 bool brw_do_channel_expressions(struct exec_list *instructions);
525 bool brw_do_vector_splitting(struct exec_list *instructions);