i965: Add names for all instructions to dump_instruction() in FS and VS.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_shader.cpp
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
24 extern "C" {
25 #include "main/macros.h"
26 #include "brw_context.h"
27 #include "brw_vs.h"
28 }
29 #include "brw_fs.h"
30 #include "glsl/ir_optimization.h"
31 #include "glsl/ir_print_visitor.h"
32
33 struct gl_shader *
34 brw_new_shader(struct gl_context *ctx, GLuint name, GLuint type)
35 {
36 struct brw_shader *shader;
37
38 shader = rzalloc(NULL, struct brw_shader);
39 if (shader) {
40 shader->base.Type = type;
41 shader->base.Name = name;
42 _mesa_init_shader(ctx, &shader->base);
43 }
44
45 return &shader->base;
46 }
47
48 struct gl_shader_program *
49 brw_new_shader_program(struct gl_context *ctx, GLuint name)
50 {
51 struct gl_shader_program *prog = rzalloc(NULL, struct gl_shader_program);
52 if (prog) {
53 prog->Name = name;
54 _mesa_init_shader_program(ctx, prog);
55 }
56 return prog;
57 }
58
59 /**
60 * Performs a compile of the shader stages even when we don't know
61 * what non-orthogonal state will be set, in the hope that it reflects
62 * the eventual NOS used, and thus allows us to produce link failures.
63 */
64 static bool
65 brw_shader_precompile(struct gl_context *ctx, struct gl_shader_program *prog)
66 {
67 struct brw_context *brw = brw_context(ctx);
68
69 if (brw->precompile && !brw_fs_precompile(ctx, prog))
70 return false;
71
72 if (brw->precompile && !brw_vs_precompile(ctx, prog))
73 return false;
74
75 return true;
76 }
77
78 static void
79 brw_lower_packing_builtins(struct brw_context *brw,
80 gl_shader_type shader_type,
81 exec_list *ir)
82 {
83 int ops = LOWER_PACK_SNORM_2x16
84 | LOWER_UNPACK_SNORM_2x16
85 | LOWER_PACK_UNORM_2x16
86 | LOWER_UNPACK_UNORM_2x16
87 | LOWER_PACK_SNORM_4x8
88 | LOWER_UNPACK_SNORM_4x8
89 | LOWER_PACK_UNORM_4x8
90 | LOWER_UNPACK_UNORM_4x8;
91
92 if (brw->intel.gen >= 7) {
93 /* Gen7 introduced the f32to16 and f16to32 instructions, which can be
94 * used to execute packHalf2x16 and unpackHalf2x16. For AOS code, no
95 * lowering is needed. For SOA code, the Half2x16 ops must be
96 * scalarized.
97 */
98 if (shader_type == MESA_SHADER_FRAGMENT) {
99 ops |= LOWER_PACK_HALF_2x16_TO_SPLIT
100 | LOWER_UNPACK_HALF_2x16_TO_SPLIT;
101 }
102 } else {
103 ops |= LOWER_PACK_HALF_2x16
104 | LOWER_UNPACK_HALF_2x16;
105 }
106
107 lower_packing_builtins(ir, ops);
108 }
109
110 GLboolean
111 brw_link_shader(struct gl_context *ctx, struct gl_shader_program *shProg)
112 {
113 struct brw_context *brw = brw_context(ctx);
114 struct intel_context *intel = &brw->intel;
115 unsigned int stage;
116
117 for (stage = 0; stage < ARRAY_SIZE(shProg->_LinkedShaders); stage++) {
118 struct brw_shader *shader =
119 (struct brw_shader *)shProg->_LinkedShaders[stage];
120 static const GLenum targets[] = {
121 GL_VERTEX_PROGRAM_ARB,
122 GL_FRAGMENT_PROGRAM_ARB,
123 GL_GEOMETRY_PROGRAM_NV
124 };
125
126 if (!shader)
127 continue;
128
129 struct gl_program *prog =
130 ctx->Driver.NewProgram(ctx, targets[stage], shader->base.Name);
131 if (!prog)
132 return false;
133 prog->Parameters = _mesa_new_parameter_list();
134
135 if (stage == 0) {
136 struct gl_vertex_program *vp = (struct gl_vertex_program *) prog;
137 vp->UsesClipDistance = shProg->Vert.UsesClipDistance;
138 }
139
140 void *mem_ctx = ralloc_context(NULL);
141 bool progress;
142
143 if (shader->ir)
144 ralloc_free(shader->ir);
145 shader->ir = new(shader) exec_list;
146 clone_ir_list(mem_ctx, shader->ir, shader->base.ir);
147
148 /* lower_packing_builtins() inserts arithmetic instructions, so it
149 * must precede lower_instructions().
150 */
151 brw_lower_packing_builtins(brw, (gl_shader_type) stage, shader->ir);
152 do_mat_op_to_vec(shader->ir);
153 const int lrp_to_arith = (intel->gen < 6 || stage != MESA_SHADER_FRAGMENT)
154 ? LRP_TO_ARITH : 0;
155 lower_instructions(shader->ir,
156 MOD_TO_FRACT |
157 DIV_TO_MUL_RCP |
158 SUB_TO_ADD_NEG |
159 EXP_TO_EXP2 |
160 LOG_TO_LOG2 |
161 lrp_to_arith);
162
163 /* Pre-gen6 HW can only nest if-statements 16 deep. Beyond this,
164 * if-statements need to be flattened.
165 */
166 if (intel->gen < 6)
167 lower_if_to_cond_assign(shader->ir, 16);
168
169 do_lower_texture_projection(shader->ir);
170 if (intel->gen < 8 && !intel->is_haswell)
171 brw_lower_texture_gradients(shader->ir);
172 do_vec_index_to_cond_assign(shader->ir);
173 brw_do_cubemap_normalize(shader->ir);
174 lower_noise(shader->ir);
175 lower_quadop_vector(shader->ir, false);
176
177 bool input = true;
178 bool output = stage == MESA_SHADER_FRAGMENT;
179 bool temp = stage == MESA_SHADER_FRAGMENT;
180 bool uniform = false;
181
182 bool lowered_variable_indexing =
183 lower_variable_index_to_cond_assign(shader->ir,
184 input, output, temp, uniform);
185
186 if (unlikely((intel->perf_debug) && lowered_variable_indexing)) {
187 perf_debug("Unsupported form of variable indexing in FS; falling "
188 "back to very inefficient code generation\n");
189 }
190
191 /* FINISHME: Do this before the variable index lowering. */
192 lower_ubo_reference(&shader->base, shader->ir);
193
194 do {
195 progress = false;
196
197 if (stage == MESA_SHADER_FRAGMENT) {
198 brw_do_channel_expressions(shader->ir);
199 brw_do_vector_splitting(shader->ir);
200 }
201
202 progress = do_lower_jumps(shader->ir, true, true,
203 true, /* main return */
204 false, /* continue */
205 false /* loops */
206 ) || progress;
207
208 progress = do_common_optimization(shader->ir, true, true, 32)
209 || progress;
210 } while (progress);
211
212 /* Make a pass over the IR to add state references for any built-in
213 * uniforms that are used. This has to be done now (during linking).
214 * Code generation doesn't happen until the first time this shader is
215 * used for rendering. Waiting until then to generate the parameters is
216 * too late. At that point, the values for the built-in uniforms won't
217 * get sent to the shader.
218 */
219 foreach_list(node, shader->ir) {
220 ir_variable *var = ((ir_instruction *) node)->as_variable();
221
222 if ((var == NULL) || (var->mode != ir_var_uniform)
223 || (strncmp(var->name, "gl_", 3) != 0))
224 continue;
225
226 const ir_state_slot *const slots = var->state_slots;
227 assert(var->state_slots != NULL);
228
229 for (unsigned int i = 0; i < var->num_state_slots; i++) {
230 _mesa_add_state_reference(prog->Parameters,
231 (gl_state_index *) slots[i].tokens);
232 }
233 }
234
235 validate_ir_tree(shader->ir);
236
237 reparent_ir(shader->ir, shader->ir);
238 ralloc_free(mem_ctx);
239
240 do_set_program_inouts(shader->ir, prog,
241 shader->base.Type == GL_FRAGMENT_SHADER);
242
243 prog->SamplersUsed = shader->base.active_samplers;
244 _mesa_update_shader_textures_used(shProg, prog);
245
246 _mesa_reference_program(ctx, &shader->base.Program, prog);
247
248 brw_add_texrect_params(prog);
249
250 /* This has to be done last. Any operation that can cause
251 * prog->ParameterValues to get reallocated (e.g., anything that adds a
252 * program constant) has to happen before creating this linkage.
253 */
254 _mesa_associate_uniform_storage(ctx, shProg, prog->Parameters);
255
256 _mesa_reference_program(ctx, &prog, NULL);
257
258 if (ctx->Shader.Flags & GLSL_DUMP) {
259 static const char *target_strings[]
260 = { "vertex", "fragment", "geometry" };
261 printf("\n");
262 printf("GLSL IR for linked %s program %d:\n", target_strings[stage],
263 shProg->Name);
264 _mesa_print_ir(shader->base.ir, NULL);
265 }
266 }
267
268 if (!brw_shader_precompile(ctx, shProg))
269 return false;
270
271 return true;
272 }
273
274
275 int
276 brw_type_for_base_type(const struct glsl_type *type)
277 {
278 switch (type->base_type) {
279 case GLSL_TYPE_FLOAT:
280 return BRW_REGISTER_TYPE_F;
281 case GLSL_TYPE_INT:
282 case GLSL_TYPE_BOOL:
283 return BRW_REGISTER_TYPE_D;
284 case GLSL_TYPE_UINT:
285 return BRW_REGISTER_TYPE_UD;
286 case GLSL_TYPE_ARRAY:
287 return brw_type_for_base_type(type->fields.array);
288 case GLSL_TYPE_STRUCT:
289 case GLSL_TYPE_SAMPLER:
290 /* These should be overridden with the type of the member when
291 * dereferenced into. BRW_REGISTER_TYPE_UD seems like a likely
292 * way to trip up if we don't.
293 */
294 return BRW_REGISTER_TYPE_UD;
295 case GLSL_TYPE_VOID:
296 case GLSL_TYPE_ERROR:
297 case GLSL_TYPE_INTERFACE:
298 assert(!"not reached");
299 break;
300 }
301
302 return BRW_REGISTER_TYPE_F;
303 }
304
305 uint32_t
306 brw_conditional_for_comparison(unsigned int op)
307 {
308 switch (op) {
309 case ir_binop_less:
310 return BRW_CONDITIONAL_L;
311 case ir_binop_greater:
312 return BRW_CONDITIONAL_G;
313 case ir_binop_lequal:
314 return BRW_CONDITIONAL_LE;
315 case ir_binop_gequal:
316 return BRW_CONDITIONAL_GE;
317 case ir_binop_equal:
318 case ir_binop_all_equal: /* same as equal for scalars */
319 return BRW_CONDITIONAL_Z;
320 case ir_binop_nequal:
321 case ir_binop_any_nequal: /* same as nequal for scalars */
322 return BRW_CONDITIONAL_NZ;
323 default:
324 assert(!"not reached: bad operation for comparison");
325 return BRW_CONDITIONAL_NZ;
326 }
327 }
328
329 uint32_t
330 brw_math_function(enum opcode op)
331 {
332 switch (op) {
333 case SHADER_OPCODE_RCP:
334 return BRW_MATH_FUNCTION_INV;
335 case SHADER_OPCODE_RSQ:
336 return BRW_MATH_FUNCTION_RSQ;
337 case SHADER_OPCODE_SQRT:
338 return BRW_MATH_FUNCTION_SQRT;
339 case SHADER_OPCODE_EXP2:
340 return BRW_MATH_FUNCTION_EXP;
341 case SHADER_OPCODE_LOG2:
342 return BRW_MATH_FUNCTION_LOG;
343 case SHADER_OPCODE_POW:
344 return BRW_MATH_FUNCTION_POW;
345 case SHADER_OPCODE_SIN:
346 return BRW_MATH_FUNCTION_SIN;
347 case SHADER_OPCODE_COS:
348 return BRW_MATH_FUNCTION_COS;
349 case SHADER_OPCODE_INT_QUOTIENT:
350 return BRW_MATH_FUNCTION_INT_DIV_QUOTIENT;
351 case SHADER_OPCODE_INT_REMAINDER:
352 return BRW_MATH_FUNCTION_INT_DIV_REMAINDER;
353 default:
354 assert(!"not reached: unknown math function");
355 return 0;
356 }
357 }
358
359 uint32_t
360 brw_texture_offset(ir_constant *offset)
361 {
362 assert(offset != NULL);
363
364 signed char offsets[3];
365 for (unsigned i = 0; i < offset->type->vector_elements; i++)
366 offsets[i] = (signed char) offset->value.i[i];
367
368 /* Combine all three offsets into a single unsigned dword:
369 *
370 * bits 11:8 - U Offset (X component)
371 * bits 7:4 - V Offset (Y component)
372 * bits 3:0 - R Offset (Z component)
373 */
374 unsigned offset_bits = 0;
375 for (unsigned i = 0; i < offset->type->vector_elements; i++) {
376 const unsigned shift = 4 * (2 - i);
377 offset_bits |= (offsets[i] << shift) & (0xF << shift);
378 }
379 return offset_bits;
380 }
381
382 const char *
383 brw_instruction_name(enum opcode op)
384 {
385 char *fallback;
386
387 if (op < ARRAY_SIZE(opcode_descs) && opcode_descs[op].name)
388 return opcode_descs[op].name;
389
390 switch (op) {
391 case FS_OPCODE_FB_WRITE:
392 return "fb_write";
393
394 case SHADER_OPCODE_RCP:
395 return "rcp";
396 case SHADER_OPCODE_RSQ:
397 return "rsq";
398 case SHADER_OPCODE_SQRT:
399 return "sqrt";
400 case SHADER_OPCODE_EXP2:
401 return "exp2";
402 case SHADER_OPCODE_LOG2:
403 return "log2";
404 case SHADER_OPCODE_POW:
405 return "pow";
406 case SHADER_OPCODE_INT_QUOTIENT:
407 return "int_quot";
408 case SHADER_OPCODE_INT_REMAINDER:
409 return "int_rem";
410 case SHADER_OPCODE_SIN:
411 return "sin";
412 case SHADER_OPCODE_COS:
413 return "cos";
414
415 case SHADER_OPCODE_TEX:
416 return "tex";
417 case SHADER_OPCODE_TXD:
418 return "txd";
419 case SHADER_OPCODE_TXF:
420 return "txf";
421 case SHADER_OPCODE_TXL:
422 return "txl";
423 case SHADER_OPCODE_TXS:
424 return "txs";
425 case FS_OPCODE_TXB:
426 return "txb";
427 case SHADER_OPCODE_TXF_MS:
428 return "txf_ms";
429
430 case FS_OPCODE_DDX:
431 return "ddx";
432 case FS_OPCODE_DDY:
433 return "ddy";
434
435 case FS_OPCODE_PIXEL_X:
436 return "pixel_x";
437 case FS_OPCODE_PIXEL_Y:
438 return "pixel_y";
439
440 case FS_OPCODE_CINTERP:
441 return "cinterp";
442 case FS_OPCODE_LINTERP:
443 return "linterp";
444
445 case FS_OPCODE_SPILL:
446 return "spill";
447 case FS_OPCODE_UNSPILL:
448 return "unspill";
449
450 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD:
451 return "uniform_pull_const";
452 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7:
453 return "uniform_pull_const_gen7";
454 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD:
455 return "varying_pull_const";
456 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN7:
457 return "varying_pull_const_gen7";
458
459 case FS_OPCODE_MOV_DISPATCH_TO_FLAGS:
460 return "mov_dispatch_to_flags";
461 case FS_OPCODE_DISCARD_JUMP:
462 return "discard_jump";
463
464 case FS_OPCODE_SET_SIMD4X2_OFFSET:
465 return "set_simd4x2_offset";
466
467 case FS_OPCODE_PACK_HALF_2x16_SPLIT:
468 return "pack_half_2x16_split";
469 case FS_OPCODE_UNPACK_HALF_2x16_SPLIT_X:
470 return "unpack_half_2x16_split_x";
471 case FS_OPCODE_UNPACK_HALF_2x16_SPLIT_Y:
472 return "unpack_half_2x16_split_y";
473
474 case VS_OPCODE_URB_WRITE:
475 return "urb_write";
476 case VS_OPCODE_SCRATCH_READ:
477 return "scratch_read";
478 case VS_OPCODE_SCRATCH_WRITE:
479 return "scratch_write";
480 case VS_OPCODE_PULL_CONSTANT_LOAD:
481 return "pull_constant_load";
482
483 default:
484 /* Yes, this leaks. It's in debug code, it should never occur, and if
485 * it does, you should just add the case to the list above.
486 */
487 asprintf(&fallback, "op%d", op);
488 return fallback;
489 }
490 }