vc4: Clamp the shadow comparison value.
[mesa.git] / src / gallium / drivers / vc4 / vc4_program.c
1 /*
2 * Copyright (c) 2014 Scott Mansell
3 * Copyright © 2014 Broadcom
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25 #include <inttypes.h>
26 #include "util/u_format.h"
27 #include "util/u_hash.h"
28 #include "util/u_math.h"
29 #include "util/u_memory.h"
30 #include "util/ralloc.h"
31 #include "util/hash_table.h"
32 #include "tgsi/tgsi_dump.h"
33 #include "tgsi/tgsi_parse.h"
34 #include "compiler/nir/nir.h"
35 #include "compiler/nir/nir_builder.h"
36 #include "nir/tgsi_to_nir.h"
37 #include "vc4_context.h"
38 #include "vc4_qpu.h"
39 #include "vc4_qir.h"
40 #include "mesa/state_tracker/st_glsl_types.h"
41
42 static struct qreg
43 ntq_get_src(struct vc4_compile *c, nir_src src, int i);
44 static void
45 ntq_emit_cf_list(struct vc4_compile *c, struct exec_list *list);
46
47 static void
48 resize_qreg_array(struct vc4_compile *c,
49 struct qreg **regs,
50 uint32_t *size,
51 uint32_t decl_size)
52 {
53 if (*size >= decl_size)
54 return;
55
56 uint32_t old_size = *size;
57 *size = MAX2(*size * 2, decl_size);
58 *regs = reralloc(c, *regs, struct qreg, *size);
59 if (!*regs) {
60 fprintf(stderr, "Malloc failure\n");
61 abort();
62 }
63
64 for (uint32_t i = old_size; i < *size; i++)
65 (*regs)[i] = c->undef;
66 }
67
68 static struct qreg
69 indirect_uniform_load(struct vc4_compile *c, nir_intrinsic_instr *intr)
70 {
71 struct qreg indirect_offset = ntq_get_src(c, intr->src[0], 0);
72 uint32_t offset = nir_intrinsic_base(intr);
73 struct vc4_compiler_ubo_range *range = NULL;
74 unsigned i;
75 for (i = 0; i < c->num_uniform_ranges; i++) {
76 range = &c->ubo_ranges[i];
77 if (offset >= range->src_offset &&
78 offset < range->src_offset + range->size) {
79 break;
80 }
81 }
82 /* The driver-location-based offset always has to be within a declared
83 * uniform range.
84 */
85 assert(range);
86 if (!range->used) {
87 range->used = true;
88 range->dst_offset = c->next_ubo_dst_offset;
89 c->next_ubo_dst_offset += range->size;
90 c->num_ubo_ranges++;
91 }
92
93 offset -= range->src_offset;
94
95 /* Adjust for where we stored the TGSI register base. */
96 indirect_offset = qir_ADD(c, indirect_offset,
97 qir_uniform_ui(c, (range->dst_offset +
98 offset)));
99
100 /* Clamp to [0, array size). Note that MIN/MAX are signed. */
101 indirect_offset = qir_MAX(c, indirect_offset, qir_uniform_ui(c, 0));
102 indirect_offset = qir_MIN(c, indirect_offset,
103 qir_uniform_ui(c, (range->dst_offset +
104 range->size - 4)));
105
106 qir_TEX_DIRECT(c, indirect_offset, qir_uniform(c, QUNIFORM_UBO_ADDR, 0));
107 c->num_texture_samples++;
108 return qir_TEX_RESULT(c);
109 }
110
111 nir_ssa_def *
112 vc4_nir_get_swizzled_channel(nir_builder *b, nir_ssa_def **srcs, int swiz)
113 {
114 switch (swiz) {
115 default:
116 case PIPE_SWIZZLE_NONE:
117 fprintf(stderr, "warning: unknown swizzle\n");
118 /* FALLTHROUGH */
119 case PIPE_SWIZZLE_0:
120 return nir_imm_float(b, 0.0);
121 case PIPE_SWIZZLE_1:
122 return nir_imm_float(b, 1.0);
123 case PIPE_SWIZZLE_X:
124 case PIPE_SWIZZLE_Y:
125 case PIPE_SWIZZLE_Z:
126 case PIPE_SWIZZLE_W:
127 return srcs[swiz];
128 }
129 }
130
131 static struct qreg *
132 ntq_init_ssa_def(struct vc4_compile *c, nir_ssa_def *def)
133 {
134 struct qreg *qregs = ralloc_array(c->def_ht, struct qreg,
135 def->num_components);
136 _mesa_hash_table_insert(c->def_ht, def, qregs);
137 return qregs;
138 }
139
140 /**
141 * This function is responsible for getting QIR results into the associated
142 * storage for a NIR instruction.
143 *
144 * If it's a NIR SSA def, then we just set the associated hash table entry to
145 * the new result.
146 *
147 * If it's a NIR reg, then we need to update the existing qreg assigned to the
148 * NIR destination with the incoming value. To do that without introducing
149 * new MOVs, we require that the incoming qreg either be a uniform, or be
150 * SSA-defined by the previous QIR instruction in the block and rewritable by
151 * this function. That lets us sneak ahead and insert the SF flag beforehand
152 * (knowing that the previous instruction doesn't depend on flags) and rewrite
153 * its destination to be the NIR reg's destination
154 */
155 static void
156 ntq_store_dest(struct vc4_compile *c, nir_dest *dest, int chan,
157 struct qreg result)
158 {
159 struct qinst *last_inst = NULL;
160 if (!list_empty(&c->cur_block->instructions))
161 last_inst = (struct qinst *)c->cur_block->instructions.prev;
162
163 assert(result.file == QFILE_UNIF ||
164 (result.file == QFILE_TEMP &&
165 last_inst && last_inst == c->defs[result.index]));
166
167 if (dest->is_ssa) {
168 assert(chan < dest->ssa.num_components);
169
170 struct qreg *qregs;
171 struct hash_entry *entry =
172 _mesa_hash_table_search(c->def_ht, &dest->ssa);
173
174 if (entry)
175 qregs = entry->data;
176 else
177 qregs = ntq_init_ssa_def(c, &dest->ssa);
178
179 qregs[chan] = result;
180 } else {
181 nir_register *reg = dest->reg.reg;
182 assert(dest->reg.base_offset == 0);
183 assert(reg->num_array_elems == 0);
184 struct hash_entry *entry =
185 _mesa_hash_table_search(c->def_ht, reg);
186 struct qreg *qregs = entry->data;
187
188 /* Insert a MOV if the source wasn't an SSA def in the
189 * previous instruction.
190 */
191 if (result.file == QFILE_UNIF) {
192 result = qir_MOV(c, result);
193 last_inst = c->defs[result.index];
194 }
195
196 /* We know they're both temps, so just rewrite index. */
197 c->defs[last_inst->dst.index] = NULL;
198 last_inst->dst.index = qregs[chan].index;
199
200 /* If we're in control flow, then make this update of the reg
201 * conditional on the execution mask.
202 */
203 if (c->execute.file != QFILE_NULL) {
204 last_inst->dst.index = qregs[chan].index;
205
206 /* Set the flags to the current exec mask. To insert
207 * the SF, we temporarily remove our SSA instruction.
208 */
209 list_del(&last_inst->link);
210 qir_SF(c, c->execute);
211 list_addtail(&last_inst->link,
212 &c->cur_block->instructions);
213
214 last_inst->cond = QPU_COND_ZS;
215 last_inst->cond_is_exec_mask = true;
216 }
217 }
218 }
219
220 static struct qreg *
221 ntq_get_dest(struct vc4_compile *c, nir_dest *dest)
222 {
223 if (dest->is_ssa) {
224 struct qreg *qregs = ntq_init_ssa_def(c, &dest->ssa);
225 for (int i = 0; i < dest->ssa.num_components; i++)
226 qregs[i] = c->undef;
227 return qregs;
228 } else {
229 nir_register *reg = dest->reg.reg;
230 assert(dest->reg.base_offset == 0);
231 assert(reg->num_array_elems == 0);
232 struct hash_entry *entry =
233 _mesa_hash_table_search(c->def_ht, reg);
234 return entry->data;
235 }
236 }
237
238 static struct qreg
239 ntq_get_src(struct vc4_compile *c, nir_src src, int i)
240 {
241 struct hash_entry *entry;
242 if (src.is_ssa) {
243 entry = _mesa_hash_table_search(c->def_ht, src.ssa);
244 assert(i < src.ssa->num_components);
245 } else {
246 nir_register *reg = src.reg.reg;
247 entry = _mesa_hash_table_search(c->def_ht, reg);
248 assert(reg->num_array_elems == 0);
249 assert(src.reg.base_offset == 0);
250 assert(i < reg->num_components);
251 }
252
253 struct qreg *qregs = entry->data;
254 return qregs[i];
255 }
256
257 static struct qreg
258 ntq_get_alu_src(struct vc4_compile *c, nir_alu_instr *instr,
259 unsigned src)
260 {
261 assert(util_is_power_of_two(instr->dest.write_mask));
262 unsigned chan = ffs(instr->dest.write_mask) - 1;
263 struct qreg r = ntq_get_src(c, instr->src[src].src,
264 instr->src[src].swizzle[chan]);
265
266 assert(!instr->src[src].abs);
267 assert(!instr->src[src].negate);
268
269 return r;
270 };
271
272 static inline struct qreg
273 qir_SAT(struct vc4_compile *c, struct qreg val)
274 {
275 return qir_FMAX(c,
276 qir_FMIN(c, val, qir_uniform_f(c, 1.0)),
277 qir_uniform_f(c, 0.0));
278 }
279
280 static struct qreg
281 ntq_rcp(struct vc4_compile *c, struct qreg x)
282 {
283 struct qreg r = qir_RCP(c, x);
284
285 /* Apply a Newton-Raphson step to improve the accuracy. */
286 r = qir_FMUL(c, r, qir_FSUB(c,
287 qir_uniform_f(c, 2.0),
288 qir_FMUL(c, x, r)));
289
290 return r;
291 }
292
293 static struct qreg
294 ntq_rsq(struct vc4_compile *c, struct qreg x)
295 {
296 struct qreg r = qir_RSQ(c, x);
297
298 /* Apply a Newton-Raphson step to improve the accuracy. */
299 r = qir_FMUL(c, r, qir_FSUB(c,
300 qir_uniform_f(c, 1.5),
301 qir_FMUL(c,
302 qir_uniform_f(c, 0.5),
303 qir_FMUL(c, x,
304 qir_FMUL(c, r, r)))));
305
306 return r;
307 }
308
309 static struct qreg
310 ntq_umul(struct vc4_compile *c, struct qreg src0, struct qreg src1)
311 {
312 struct qreg src0_hi = qir_SHR(c, src0,
313 qir_uniform_ui(c, 24));
314 struct qreg src1_hi = qir_SHR(c, src1,
315 qir_uniform_ui(c, 24));
316
317 struct qreg hilo = qir_MUL24(c, src0_hi, src1);
318 struct qreg lohi = qir_MUL24(c, src0, src1_hi);
319 struct qreg lolo = qir_MUL24(c, src0, src1);
320
321 return qir_ADD(c, lolo, qir_SHL(c,
322 qir_ADD(c, hilo, lohi),
323 qir_uniform_ui(c, 24)));
324 }
325
326 static struct qreg
327 ntq_scale_depth_texture(struct vc4_compile *c, struct qreg src)
328 {
329 struct qreg depthf = qir_ITOF(c, qir_SHR(c, src,
330 qir_uniform_ui(c, 8)));
331 return qir_FMUL(c, depthf, qir_uniform_f(c, 1.0f/0xffffff));
332 }
333
334 /**
335 * Emits a lowered TXF_MS from an MSAA texture.
336 *
337 * The addressing math has been lowered in NIR, and now we just need to read
338 * it like a UBO.
339 */
340 static void
341 ntq_emit_txf(struct vc4_compile *c, nir_tex_instr *instr)
342 {
343 uint32_t tile_width = 32;
344 uint32_t tile_height = 32;
345 uint32_t tile_size = (tile_height * tile_width *
346 VC4_MAX_SAMPLES * sizeof(uint32_t));
347
348 unsigned unit = instr->texture_index;
349 uint32_t w = align(c->key->tex[unit].msaa_width, tile_width);
350 uint32_t w_tiles = w / tile_width;
351 uint32_t h = align(c->key->tex[unit].msaa_height, tile_height);
352 uint32_t h_tiles = h / tile_height;
353 uint32_t size = w_tiles * h_tiles * tile_size;
354
355 struct qreg addr;
356 assert(instr->num_srcs == 1);
357 assert(instr->src[0].src_type == nir_tex_src_coord);
358 addr = ntq_get_src(c, instr->src[0].src, 0);
359
360 /* Perform the clamping required by kernel validation. */
361 addr = qir_MAX(c, addr, qir_uniform_ui(c, 0));
362 addr = qir_MIN(c, addr, qir_uniform_ui(c, size - 4));
363
364 qir_TEX_DIRECT(c, addr, qir_uniform(c, QUNIFORM_TEXTURE_MSAA_ADDR, unit));
365
366 struct qreg tex = qir_TEX_RESULT(c);
367 c->num_texture_samples++;
368
369 enum pipe_format format = c->key->tex[unit].format;
370 if (util_format_is_depth_or_stencil(format)) {
371 struct qreg scaled = ntq_scale_depth_texture(c, tex);
372 for (int i = 0; i < 4; i++)
373 ntq_store_dest(c, &instr->dest, i, qir_MOV(c, scaled));
374 } else {
375 for (int i = 0; i < 4; i++)
376 ntq_store_dest(c, &instr->dest, i,
377 qir_UNPACK_8_F(c, tex, i));
378 }
379 }
380
381 static void
382 ntq_emit_tex(struct vc4_compile *c, nir_tex_instr *instr)
383 {
384 struct qreg s, t, r, lod, compare;
385 bool is_txb = false, is_txl = false;
386 unsigned unit = instr->texture_index;
387
388 if (instr->op == nir_texop_txf) {
389 ntq_emit_txf(c, instr);
390 return;
391 }
392
393 for (unsigned i = 0; i < instr->num_srcs; i++) {
394 switch (instr->src[i].src_type) {
395 case nir_tex_src_coord:
396 s = ntq_get_src(c, instr->src[i].src, 0);
397 if (instr->sampler_dim == GLSL_SAMPLER_DIM_1D)
398 t = qir_uniform_f(c, 0.5);
399 else
400 t = ntq_get_src(c, instr->src[i].src, 1);
401 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE)
402 r = ntq_get_src(c, instr->src[i].src, 2);
403 break;
404 case nir_tex_src_bias:
405 lod = ntq_get_src(c, instr->src[i].src, 0);
406 is_txb = true;
407 break;
408 case nir_tex_src_lod:
409 lod = ntq_get_src(c, instr->src[i].src, 0);
410 is_txl = true;
411 break;
412 case nir_tex_src_comparitor:
413 compare = ntq_get_src(c, instr->src[i].src, 0);
414 break;
415 default:
416 unreachable("unknown texture source");
417 }
418 }
419
420 if (c->stage != QSTAGE_FRAG && !is_txl) {
421 /* From the GLSL 1.20 spec:
422 *
423 * "If it is mip-mapped and running on the vertex shader,
424 * then the base texture is used."
425 */
426 is_txl = true;
427 lod = qir_uniform_ui(c, 0);
428 }
429
430 if (c->key->tex[unit].force_first_level) {
431 lod = qir_uniform(c, QUNIFORM_TEXTURE_FIRST_LEVEL, unit);
432 is_txl = true;
433 is_txb = false;
434 }
435
436 struct qreg texture_u[] = {
437 qir_uniform(c, QUNIFORM_TEXTURE_CONFIG_P0, unit),
438 qir_uniform(c, QUNIFORM_TEXTURE_CONFIG_P1, unit),
439 qir_uniform(c, QUNIFORM_CONSTANT, 0),
440 qir_uniform(c, QUNIFORM_CONSTANT, 0),
441 };
442 uint32_t next_texture_u = 0;
443
444 /* There is no native support for GL texture rectangle coordinates, so
445 * we have to rescale from ([0, width], [0, height]) to ([0, 1], [0,
446 * 1]).
447 */
448 if (instr->sampler_dim == GLSL_SAMPLER_DIM_RECT) {
449 s = qir_FMUL(c, s,
450 qir_uniform(c, QUNIFORM_TEXRECT_SCALE_X, unit));
451 t = qir_FMUL(c, t,
452 qir_uniform(c, QUNIFORM_TEXRECT_SCALE_Y, unit));
453 }
454
455 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE || is_txl) {
456 texture_u[2] = qir_uniform(c, QUNIFORM_TEXTURE_CONFIG_P2,
457 unit | (is_txl << 16));
458 }
459
460 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE) {
461 qir_TEX_R(c, r, texture_u[next_texture_u++]);
462 } else if (c->key->tex[unit].wrap_s == PIPE_TEX_WRAP_CLAMP_TO_BORDER ||
463 c->key->tex[unit].wrap_s == PIPE_TEX_WRAP_CLAMP ||
464 c->key->tex[unit].wrap_t == PIPE_TEX_WRAP_CLAMP_TO_BORDER ||
465 c->key->tex[unit].wrap_t == PIPE_TEX_WRAP_CLAMP) {
466 qir_TEX_R(c, qir_uniform(c, QUNIFORM_TEXTURE_BORDER_COLOR, unit),
467 texture_u[next_texture_u++]);
468 }
469
470 if (c->key->tex[unit].wrap_s == PIPE_TEX_WRAP_CLAMP) {
471 s = qir_SAT(c, s);
472 }
473
474 if (c->key->tex[unit].wrap_t == PIPE_TEX_WRAP_CLAMP) {
475 t = qir_SAT(c, t);
476 }
477
478 qir_TEX_T(c, t, texture_u[next_texture_u++]);
479
480 if (is_txl || is_txb)
481 qir_TEX_B(c, lod, texture_u[next_texture_u++]);
482
483 qir_TEX_S(c, s, texture_u[next_texture_u++]);
484
485 c->num_texture_samples++;
486 struct qreg tex = qir_TEX_RESULT(c);
487
488 enum pipe_format format = c->key->tex[unit].format;
489
490 struct qreg *dest = ntq_get_dest(c, &instr->dest);
491 if (util_format_is_depth_or_stencil(format)) {
492 struct qreg normalized = ntq_scale_depth_texture(c, tex);
493 struct qreg depth_output;
494
495 struct qreg u0 = qir_uniform_f(c, 0.0f);
496 struct qreg u1 = qir_uniform_f(c, 1.0f);
497 if (c->key->tex[unit].compare_mode) {
498 /* From the GL_ARB_shadow spec:
499 *
500 * "Let Dt (D subscript t) be the depth texture
501 * value, in the range [0, 1]. Let R be the
502 * interpolated texture coordinate clamped to the
503 * range [0, 1]."
504 */
505 compare = qir_SAT(c, compare);
506
507 switch (c->key->tex[unit].compare_func) {
508 case PIPE_FUNC_NEVER:
509 depth_output = qir_uniform_f(c, 0.0f);
510 break;
511 case PIPE_FUNC_ALWAYS:
512 depth_output = u1;
513 break;
514 case PIPE_FUNC_EQUAL:
515 qir_SF(c, qir_FSUB(c, compare, normalized));
516 depth_output = qir_SEL(c, QPU_COND_ZS, u1, u0);
517 break;
518 case PIPE_FUNC_NOTEQUAL:
519 qir_SF(c, qir_FSUB(c, compare, normalized));
520 depth_output = qir_SEL(c, QPU_COND_ZC, u1, u0);
521 break;
522 case PIPE_FUNC_GREATER:
523 qir_SF(c, qir_FSUB(c, compare, normalized));
524 depth_output = qir_SEL(c, QPU_COND_NC, u1, u0);
525 break;
526 case PIPE_FUNC_GEQUAL:
527 qir_SF(c, qir_FSUB(c, normalized, compare));
528 depth_output = qir_SEL(c, QPU_COND_NS, u1, u0);
529 break;
530 case PIPE_FUNC_LESS:
531 qir_SF(c, qir_FSUB(c, compare, normalized));
532 depth_output = qir_SEL(c, QPU_COND_NS, u1, u0);
533 break;
534 case PIPE_FUNC_LEQUAL:
535 qir_SF(c, qir_FSUB(c, normalized, compare));
536 depth_output = qir_SEL(c, QPU_COND_NC, u1, u0);
537 break;
538 }
539 } else {
540 depth_output = normalized;
541 }
542
543 for (int i = 0; i < 4; i++)
544 dest[i] = depth_output;
545 } else {
546 for (int i = 0; i < 4; i++)
547 dest[i] = qir_UNPACK_8_F(c, tex, i);
548 }
549 }
550
551 /**
552 * Computes x - floor(x), which is tricky because our FTOI truncates (rounds
553 * to zero).
554 */
555 static struct qreg
556 ntq_ffract(struct vc4_compile *c, struct qreg src)
557 {
558 struct qreg trunc = qir_ITOF(c, qir_FTOI(c, src));
559 struct qreg diff = qir_FSUB(c, src, trunc);
560 qir_SF(c, diff);
561 return qir_MOV(c, qir_SEL(c, QPU_COND_NS,
562 qir_FADD(c, diff, qir_uniform_f(c, 1.0)),
563 diff));
564 }
565
566 /**
567 * Computes floor(x), which is tricky because our FTOI truncates (rounds to
568 * zero).
569 */
570 static struct qreg
571 ntq_ffloor(struct vc4_compile *c, struct qreg src)
572 {
573 struct qreg trunc = qir_ITOF(c, qir_FTOI(c, src));
574
575 /* This will be < 0 if we truncated and the truncation was of a value
576 * that was < 0 in the first place.
577 */
578 qir_SF(c, qir_FSUB(c, src, trunc));
579
580 return qir_MOV(c, qir_SEL(c, QPU_COND_NS,
581 qir_FSUB(c, trunc, qir_uniform_f(c, 1.0)),
582 trunc));
583 }
584
585 /**
586 * Computes ceil(x), which is tricky because our FTOI truncates (rounds to
587 * zero).
588 */
589 static struct qreg
590 ntq_fceil(struct vc4_compile *c, struct qreg src)
591 {
592 struct qreg trunc = qir_ITOF(c, qir_FTOI(c, src));
593
594 /* This will be < 0 if we truncated and the truncation was of a value
595 * that was > 0 in the first place.
596 */
597 qir_SF(c, qir_FSUB(c, trunc, src));
598
599 return qir_MOV(c, qir_SEL(c, QPU_COND_NS,
600 qir_FADD(c, trunc, qir_uniform_f(c, 1.0)),
601 trunc));
602 }
603
604 static struct qreg
605 ntq_fsin(struct vc4_compile *c, struct qreg src)
606 {
607 float coeff[] = {
608 -2.0 * M_PI,
609 pow(2.0 * M_PI, 3) / (3 * 2 * 1),
610 -pow(2.0 * M_PI, 5) / (5 * 4 * 3 * 2 * 1),
611 pow(2.0 * M_PI, 7) / (7 * 6 * 5 * 4 * 3 * 2 * 1),
612 -pow(2.0 * M_PI, 9) / (9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1),
613 };
614
615 struct qreg scaled_x =
616 qir_FMUL(c,
617 src,
618 qir_uniform_f(c, 1.0 / (M_PI * 2.0)));
619
620 struct qreg x = qir_FADD(c,
621 ntq_ffract(c, scaled_x),
622 qir_uniform_f(c, -0.5));
623 struct qreg x2 = qir_FMUL(c, x, x);
624 struct qreg sum = qir_FMUL(c, x, qir_uniform_f(c, coeff[0]));
625 for (int i = 1; i < ARRAY_SIZE(coeff); i++) {
626 x = qir_FMUL(c, x, x2);
627 sum = qir_FADD(c,
628 sum,
629 qir_FMUL(c,
630 x,
631 qir_uniform_f(c, coeff[i])));
632 }
633 return sum;
634 }
635
636 static struct qreg
637 ntq_fcos(struct vc4_compile *c, struct qreg src)
638 {
639 float coeff[] = {
640 -1.0f,
641 pow(2.0 * M_PI, 2) / (2 * 1),
642 -pow(2.0 * M_PI, 4) / (4 * 3 * 2 * 1),
643 pow(2.0 * M_PI, 6) / (6 * 5 * 4 * 3 * 2 * 1),
644 -pow(2.0 * M_PI, 8) / (8 * 7 * 6 * 5 * 4 * 3 * 2 * 1),
645 pow(2.0 * M_PI, 10) / (10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1),
646 };
647
648 struct qreg scaled_x =
649 qir_FMUL(c, src,
650 qir_uniform_f(c, 1.0f / (M_PI * 2.0f)));
651 struct qreg x_frac = qir_FADD(c,
652 ntq_ffract(c, scaled_x),
653 qir_uniform_f(c, -0.5));
654
655 struct qreg sum = qir_uniform_f(c, coeff[0]);
656 struct qreg x2 = qir_FMUL(c, x_frac, x_frac);
657 struct qreg x = x2; /* Current x^2, x^4, or x^6 */
658 for (int i = 1; i < ARRAY_SIZE(coeff); i++) {
659 if (i != 1)
660 x = qir_FMUL(c, x, x2);
661
662 struct qreg mul = qir_FMUL(c,
663 x,
664 qir_uniform_f(c, coeff[i]));
665 if (i == 0)
666 sum = mul;
667 else
668 sum = qir_FADD(c, sum, mul);
669 }
670 return sum;
671 }
672
673 static struct qreg
674 ntq_fsign(struct vc4_compile *c, struct qreg src)
675 {
676 struct qreg t = qir_get_temp(c);
677
678 qir_SF(c, src);
679 qir_MOV_dest(c, t, qir_uniform_f(c, 0.0));
680 qir_MOV_dest(c, t, qir_uniform_f(c, 1.0))->cond = QPU_COND_ZC;
681 qir_MOV_dest(c, t, qir_uniform_f(c, -1.0))->cond = QPU_COND_NS;
682 return qir_MOV(c, t);
683 }
684
685 static void
686 emit_vertex_input(struct vc4_compile *c, int attr)
687 {
688 enum pipe_format format = c->vs_key->attr_formats[attr];
689 uint32_t attr_size = util_format_get_blocksize(format);
690
691 c->vattr_sizes[attr] = align(attr_size, 4);
692 for (int i = 0; i < align(attr_size, 4) / 4; i++) {
693 c->inputs[attr * 4 + i] =
694 qir_MOV(c, qir_reg(QFILE_VPM, attr * 4 + i));
695 c->num_inputs++;
696 }
697 }
698
699 static void
700 emit_fragcoord_input(struct vc4_compile *c, int attr)
701 {
702 c->inputs[attr * 4 + 0] = qir_ITOF(c, qir_reg(QFILE_FRAG_X, 0));
703 c->inputs[attr * 4 + 1] = qir_ITOF(c, qir_reg(QFILE_FRAG_Y, 0));
704 c->inputs[attr * 4 + 2] =
705 qir_FMUL(c,
706 qir_ITOF(c, qir_FRAG_Z(c)),
707 qir_uniform_f(c, 1.0 / 0xffffff));
708 c->inputs[attr * 4 + 3] = qir_RCP(c, qir_FRAG_W(c));
709 }
710
711 static struct qreg
712 emit_fragment_varying(struct vc4_compile *c, gl_varying_slot slot,
713 uint8_t swizzle)
714 {
715 uint32_t i = c->num_input_slots++;
716 struct qreg vary = {
717 QFILE_VARY,
718 i
719 };
720
721 if (c->num_input_slots >= c->input_slots_array_size) {
722 c->input_slots_array_size =
723 MAX2(4, c->input_slots_array_size * 2);
724
725 c->input_slots = reralloc(c, c->input_slots,
726 struct vc4_varying_slot,
727 c->input_slots_array_size);
728 }
729
730 c->input_slots[i].slot = slot;
731 c->input_slots[i].swizzle = swizzle;
732
733 return qir_VARY_ADD_C(c, qir_FMUL(c, vary, qir_FRAG_W(c)));
734 }
735
736 static void
737 emit_fragment_input(struct vc4_compile *c, int attr, gl_varying_slot slot)
738 {
739 for (int i = 0; i < 4; i++) {
740 c->inputs[attr * 4 + i] =
741 emit_fragment_varying(c, slot, i);
742 c->num_inputs++;
743 }
744 }
745
746 static void
747 add_output(struct vc4_compile *c,
748 uint32_t decl_offset,
749 uint8_t slot,
750 uint8_t swizzle)
751 {
752 uint32_t old_array_size = c->outputs_array_size;
753 resize_qreg_array(c, &c->outputs, &c->outputs_array_size,
754 decl_offset + 1);
755
756 if (old_array_size != c->outputs_array_size) {
757 c->output_slots = reralloc(c,
758 c->output_slots,
759 struct vc4_varying_slot,
760 c->outputs_array_size);
761 }
762
763 c->output_slots[decl_offset].slot = slot;
764 c->output_slots[decl_offset].swizzle = swizzle;
765 }
766
767 static void
768 declare_uniform_range(struct vc4_compile *c, uint32_t start, uint32_t size)
769 {
770 unsigned array_id = c->num_uniform_ranges++;
771 if (array_id >= c->ubo_ranges_array_size) {
772 c->ubo_ranges_array_size = MAX2(c->ubo_ranges_array_size * 2,
773 array_id + 1);
774 c->ubo_ranges = reralloc(c, c->ubo_ranges,
775 struct vc4_compiler_ubo_range,
776 c->ubo_ranges_array_size);
777 }
778
779 c->ubo_ranges[array_id].dst_offset = 0;
780 c->ubo_ranges[array_id].src_offset = start;
781 c->ubo_ranges[array_id].size = size;
782 c->ubo_ranges[array_id].used = false;
783 }
784
785 static bool
786 ntq_src_is_only_ssa_def_user(nir_src *src)
787 {
788 if (!src->is_ssa)
789 return false;
790
791 if (!list_empty(&src->ssa->if_uses))
792 return false;
793
794 return (src->ssa->uses.next == &src->use_link &&
795 src->ssa->uses.next->next == &src->ssa->uses);
796 }
797
798 /**
799 * In general, emits a nir_pack_unorm_4x8 as a series of MOVs with the pack
800 * bit set.
801 *
802 * However, as an optimization, it tries to find the instructions generating
803 * the sources to be packed and just emit the pack flag there, if possible.
804 */
805 static void
806 ntq_emit_pack_unorm_4x8(struct vc4_compile *c, nir_alu_instr *instr)
807 {
808 struct qreg result = qir_get_temp(c);
809 struct nir_alu_instr *vec4 = NULL;
810
811 /* If packing from a vec4 op (as expected), identify it so that we can
812 * peek back at what generated its sources.
813 */
814 if (instr->src[0].src.is_ssa &&
815 instr->src[0].src.ssa->parent_instr->type == nir_instr_type_alu &&
816 nir_instr_as_alu(instr->src[0].src.ssa->parent_instr)->op ==
817 nir_op_vec4) {
818 vec4 = nir_instr_as_alu(instr->src[0].src.ssa->parent_instr);
819 }
820
821 /* If the pack is replicating the same channel 4 times, use the 8888
822 * pack flag. This is common for blending using the alpha
823 * channel.
824 */
825 if (instr->src[0].swizzle[0] == instr->src[0].swizzle[1] &&
826 instr->src[0].swizzle[0] == instr->src[0].swizzle[2] &&
827 instr->src[0].swizzle[0] == instr->src[0].swizzle[3]) {
828 struct qreg rep = ntq_get_src(c,
829 instr->src[0].src,
830 instr->src[0].swizzle[0]);
831 ntq_store_dest(c, &instr->dest.dest, 0, qir_PACK_8888_F(c, rep));
832 return;
833 }
834
835 for (int i = 0; i < 4; i++) {
836 int swiz = instr->src[0].swizzle[i];
837 struct qreg src;
838 if (vec4) {
839 src = ntq_get_src(c, vec4->src[swiz].src,
840 vec4->src[swiz].swizzle[0]);
841 } else {
842 src = ntq_get_src(c, instr->src[0].src, swiz);
843 }
844
845 if (vec4 &&
846 ntq_src_is_only_ssa_def_user(&vec4->src[swiz].src) &&
847 src.file == QFILE_TEMP &&
848 c->defs[src.index] &&
849 qir_is_mul(c->defs[src.index]) &&
850 !c->defs[src.index]->dst.pack) {
851 struct qinst *rewrite = c->defs[src.index];
852 c->defs[src.index] = NULL;
853 rewrite->dst = result;
854 rewrite->dst.pack = QPU_PACK_MUL_8A + i;
855 continue;
856 }
857
858 qir_PACK_8_F(c, result, src, i);
859 }
860
861 ntq_store_dest(c, &instr->dest.dest, 0, qir_MOV(c, result));
862 }
863
864 /** Handles sign-extended bitfield extracts for 16 bits. */
865 static struct qreg
866 ntq_emit_ibfe(struct vc4_compile *c, struct qreg base, struct qreg offset,
867 struct qreg bits)
868 {
869 assert(bits.file == QFILE_UNIF &&
870 c->uniform_contents[bits.index] == QUNIFORM_CONSTANT &&
871 c->uniform_data[bits.index] == 16);
872
873 assert(offset.file == QFILE_UNIF &&
874 c->uniform_contents[offset.index] == QUNIFORM_CONSTANT);
875 int offset_bit = c->uniform_data[offset.index];
876 assert(offset_bit % 16 == 0);
877
878 return qir_UNPACK_16_I(c, base, offset_bit / 16);
879 }
880
881 /** Handles unsigned bitfield extracts for 8 bits. */
882 static struct qreg
883 ntq_emit_ubfe(struct vc4_compile *c, struct qreg base, struct qreg offset,
884 struct qreg bits)
885 {
886 assert(bits.file == QFILE_UNIF &&
887 c->uniform_contents[bits.index] == QUNIFORM_CONSTANT &&
888 c->uniform_data[bits.index] == 8);
889
890 assert(offset.file == QFILE_UNIF &&
891 c->uniform_contents[offset.index] == QUNIFORM_CONSTANT);
892 int offset_bit = c->uniform_data[offset.index];
893 assert(offset_bit % 8 == 0);
894
895 return qir_UNPACK_8_I(c, base, offset_bit / 8);
896 }
897
898 /**
899 * If compare_instr is a valid comparison instruction, emits the
900 * compare_instr's comparison and returns the sel_instr's return value based
901 * on the compare_instr's result.
902 */
903 static bool
904 ntq_emit_comparison(struct vc4_compile *c, struct qreg *dest,
905 nir_alu_instr *compare_instr,
906 nir_alu_instr *sel_instr)
907 {
908 enum qpu_cond cond;
909
910 switch (compare_instr->op) {
911 case nir_op_feq:
912 case nir_op_ieq:
913 case nir_op_seq:
914 cond = QPU_COND_ZS;
915 break;
916 case nir_op_fne:
917 case nir_op_ine:
918 case nir_op_sne:
919 cond = QPU_COND_ZC;
920 break;
921 case nir_op_fge:
922 case nir_op_ige:
923 case nir_op_uge:
924 case nir_op_sge:
925 cond = QPU_COND_NC;
926 break;
927 case nir_op_flt:
928 case nir_op_ilt:
929 case nir_op_slt:
930 cond = QPU_COND_NS;
931 break;
932 default:
933 return false;
934 }
935
936 struct qreg src0 = ntq_get_alu_src(c, compare_instr, 0);
937 struct qreg src1 = ntq_get_alu_src(c, compare_instr, 1);
938
939 unsigned unsized_type =
940 nir_alu_type_get_base_type(nir_op_infos[compare_instr->op].input_types[0]);
941 if (unsized_type == nir_type_float)
942 qir_SF(c, qir_FSUB(c, src0, src1));
943 else
944 qir_SF(c, qir_SUB(c, src0, src1));
945
946 switch (sel_instr->op) {
947 case nir_op_seq:
948 case nir_op_sne:
949 case nir_op_sge:
950 case nir_op_slt:
951 *dest = qir_SEL(c, cond,
952 qir_uniform_f(c, 1.0), qir_uniform_f(c, 0.0));
953 break;
954
955 case nir_op_bcsel:
956 *dest = qir_SEL(c, cond,
957 ntq_get_alu_src(c, sel_instr, 1),
958 ntq_get_alu_src(c, sel_instr, 2));
959 break;
960
961 default:
962 *dest = qir_SEL(c, cond,
963 qir_uniform_ui(c, ~0), qir_uniform_ui(c, 0));
964 break;
965 }
966
967 /* Make the temporary for nir_store_dest(). */
968 *dest = qir_MOV(c, *dest);
969
970 return true;
971 }
972
973 /**
974 * Attempts to fold a comparison generating a boolean result into the
975 * condition code for selecting between two values, instead of comparing the
976 * boolean result against 0 to generate the condition code.
977 */
978 static struct qreg ntq_emit_bcsel(struct vc4_compile *c, nir_alu_instr *instr,
979 struct qreg *src)
980 {
981 if (!instr->src[0].src.is_ssa)
982 goto out;
983 if (instr->src[0].src.ssa->parent_instr->type != nir_instr_type_alu)
984 goto out;
985 nir_alu_instr *compare =
986 nir_instr_as_alu(instr->src[0].src.ssa->parent_instr);
987 if (!compare)
988 goto out;
989
990 struct qreg dest;
991 if (ntq_emit_comparison(c, &dest, compare, instr))
992 return dest;
993
994 out:
995 qir_SF(c, src[0]);
996 return qir_MOV(c, qir_SEL(c, QPU_COND_NS, src[1], src[2]));
997 }
998
999 static struct qreg
1000 ntq_fddx(struct vc4_compile *c, struct qreg src)
1001 {
1002 /* Make sure that we have a bare temp to use for MUL rotation, so it
1003 * can be allocated to an accumulator.
1004 */
1005 if (src.pack || src.file != QFILE_TEMP)
1006 src = qir_MOV(c, src);
1007
1008 struct qreg from_left = qir_ROT_MUL(c, src, 1);
1009 struct qreg from_right = qir_ROT_MUL(c, src, 15);
1010
1011 /* Distinguish left/right pixels of the quad. */
1012 qir_SF(c, qir_AND(c, qir_reg(QFILE_QPU_ELEMENT, 0),
1013 qir_uniform_ui(c, 1)));
1014
1015 return qir_MOV(c, qir_SEL(c, QPU_COND_ZS,
1016 qir_FSUB(c, from_right, src),
1017 qir_FSUB(c, src, from_left)));
1018 }
1019
1020 static struct qreg
1021 ntq_fddy(struct vc4_compile *c, struct qreg src)
1022 {
1023 if (src.pack || src.file != QFILE_TEMP)
1024 src = qir_MOV(c, src);
1025
1026 struct qreg from_bottom = qir_ROT_MUL(c, src, 2);
1027 struct qreg from_top = qir_ROT_MUL(c, src, 14);
1028
1029 /* Distinguish top/bottom pixels of the quad. */
1030 qir_SF(c, qir_AND(c,
1031 qir_reg(QFILE_QPU_ELEMENT, 0),
1032 qir_uniform_ui(c, 2)));
1033
1034 return qir_MOV(c, qir_SEL(c, QPU_COND_ZS,
1035 qir_FSUB(c, from_top, src),
1036 qir_FSUB(c, src, from_bottom)));
1037 }
1038
1039 static void
1040 ntq_emit_alu(struct vc4_compile *c, nir_alu_instr *instr)
1041 {
1042 /* This should always be lowered to ALU operations for VC4. */
1043 assert(!instr->dest.saturate);
1044
1045 /* Vectors are special in that they have non-scalarized writemasks,
1046 * and just take the first swizzle channel for each argument in order
1047 * into each writemask channel.
1048 */
1049 if (instr->op == nir_op_vec2 ||
1050 instr->op == nir_op_vec3 ||
1051 instr->op == nir_op_vec4) {
1052 struct qreg srcs[4];
1053 for (int i = 0; i < nir_op_infos[instr->op].num_inputs; i++)
1054 srcs[i] = ntq_get_src(c, instr->src[i].src,
1055 instr->src[i].swizzle[0]);
1056 for (int i = 0; i < nir_op_infos[instr->op].num_inputs; i++)
1057 ntq_store_dest(c, &instr->dest.dest, i,
1058 qir_MOV(c, srcs[i]));
1059 return;
1060 }
1061
1062 if (instr->op == nir_op_pack_unorm_4x8) {
1063 ntq_emit_pack_unorm_4x8(c, instr);
1064 return;
1065 }
1066
1067 if (instr->op == nir_op_unpack_unorm_4x8) {
1068 struct qreg src = ntq_get_src(c, instr->src[0].src,
1069 instr->src[0].swizzle[0]);
1070 for (int i = 0; i < 4; i++) {
1071 if (instr->dest.write_mask & (1 << i))
1072 ntq_store_dest(c, &instr->dest.dest, i,
1073 qir_UNPACK_8_F(c, src, i));
1074 }
1075 return;
1076 }
1077
1078 /* General case: We can just grab the one used channel per src. */
1079 struct qreg src[nir_op_infos[instr->op].num_inputs];
1080 for (int i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
1081 src[i] = ntq_get_alu_src(c, instr, i);
1082 }
1083
1084 struct qreg result;
1085
1086 switch (instr->op) {
1087 case nir_op_fmov:
1088 case nir_op_imov:
1089 result = qir_MOV(c, src[0]);
1090 break;
1091 case nir_op_fmul:
1092 result = qir_FMUL(c, src[0], src[1]);
1093 break;
1094 case nir_op_fadd:
1095 result = qir_FADD(c, src[0], src[1]);
1096 break;
1097 case nir_op_fsub:
1098 result = qir_FSUB(c, src[0], src[1]);
1099 break;
1100 case nir_op_fmin:
1101 result = qir_FMIN(c, src[0], src[1]);
1102 break;
1103 case nir_op_fmax:
1104 result = qir_FMAX(c, src[0], src[1]);
1105 break;
1106
1107 case nir_op_f2i:
1108 case nir_op_f2u:
1109 result = qir_FTOI(c, src[0]);
1110 break;
1111 case nir_op_i2f:
1112 case nir_op_u2f:
1113 result = qir_ITOF(c, src[0]);
1114 break;
1115 case nir_op_b2f:
1116 result = qir_AND(c, src[0], qir_uniform_f(c, 1.0));
1117 break;
1118 case nir_op_b2i:
1119 result = qir_AND(c, src[0], qir_uniform_ui(c, 1));
1120 break;
1121 case nir_op_i2b:
1122 case nir_op_f2b:
1123 qir_SF(c, src[0]);
1124 result = qir_MOV(c, qir_SEL(c, QPU_COND_ZC,
1125 qir_uniform_ui(c, ~0),
1126 qir_uniform_ui(c, 0)));
1127 break;
1128
1129 case nir_op_iadd:
1130 result = qir_ADD(c, src[0], src[1]);
1131 break;
1132 case nir_op_ushr:
1133 result = qir_SHR(c, src[0], src[1]);
1134 break;
1135 case nir_op_isub:
1136 result = qir_SUB(c, src[0], src[1]);
1137 break;
1138 case nir_op_ishr:
1139 result = qir_ASR(c, src[0], src[1]);
1140 break;
1141 case nir_op_ishl:
1142 result = qir_SHL(c, src[0], src[1]);
1143 break;
1144 case nir_op_imin:
1145 result = qir_MIN(c, src[0], src[1]);
1146 break;
1147 case nir_op_imax:
1148 result = qir_MAX(c, src[0], src[1]);
1149 break;
1150 case nir_op_iand:
1151 result = qir_AND(c, src[0], src[1]);
1152 break;
1153 case nir_op_ior:
1154 result = qir_OR(c, src[0], src[1]);
1155 break;
1156 case nir_op_ixor:
1157 result = qir_XOR(c, src[0], src[1]);
1158 break;
1159 case nir_op_inot:
1160 result = qir_NOT(c, src[0]);
1161 break;
1162
1163 case nir_op_imul:
1164 result = ntq_umul(c, src[0], src[1]);
1165 break;
1166
1167 case nir_op_seq:
1168 case nir_op_sne:
1169 case nir_op_sge:
1170 case nir_op_slt:
1171 case nir_op_feq:
1172 case nir_op_fne:
1173 case nir_op_fge:
1174 case nir_op_flt:
1175 case nir_op_ieq:
1176 case nir_op_ine:
1177 case nir_op_ige:
1178 case nir_op_uge:
1179 case nir_op_ilt:
1180 if (!ntq_emit_comparison(c, &result, instr, instr)) {
1181 fprintf(stderr, "Bad comparison instruction\n");
1182 }
1183 break;
1184
1185 case nir_op_bcsel:
1186 result = ntq_emit_bcsel(c, instr, src);
1187 break;
1188 case nir_op_fcsel:
1189 qir_SF(c, src[0]);
1190 result = qir_MOV(c, qir_SEL(c, QPU_COND_ZC, src[1], src[2]));
1191 break;
1192
1193 case nir_op_frcp:
1194 result = ntq_rcp(c, src[0]);
1195 break;
1196 case nir_op_frsq:
1197 result = ntq_rsq(c, src[0]);
1198 break;
1199 case nir_op_fexp2:
1200 result = qir_EXP2(c, src[0]);
1201 break;
1202 case nir_op_flog2:
1203 result = qir_LOG2(c, src[0]);
1204 break;
1205
1206 case nir_op_ftrunc:
1207 result = qir_ITOF(c, qir_FTOI(c, src[0]));
1208 break;
1209 case nir_op_fceil:
1210 result = ntq_fceil(c, src[0]);
1211 break;
1212 case nir_op_ffract:
1213 result = ntq_ffract(c, src[0]);
1214 break;
1215 case nir_op_ffloor:
1216 result = ntq_ffloor(c, src[0]);
1217 break;
1218
1219 case nir_op_fsin:
1220 result = ntq_fsin(c, src[0]);
1221 break;
1222 case nir_op_fcos:
1223 result = ntq_fcos(c, src[0]);
1224 break;
1225
1226 case nir_op_fsign:
1227 result = ntq_fsign(c, src[0]);
1228 break;
1229
1230 case nir_op_fabs:
1231 result = qir_FMAXABS(c, src[0], src[0]);
1232 break;
1233 case nir_op_iabs:
1234 result = qir_MAX(c, src[0],
1235 qir_SUB(c, qir_uniform_ui(c, 0), src[0]));
1236 break;
1237
1238 case nir_op_ibitfield_extract:
1239 result = ntq_emit_ibfe(c, src[0], src[1], src[2]);
1240 break;
1241
1242 case nir_op_ubitfield_extract:
1243 result = ntq_emit_ubfe(c, src[0], src[1], src[2]);
1244 break;
1245
1246 case nir_op_usadd_4x8:
1247 result = qir_V8ADDS(c, src[0], src[1]);
1248 break;
1249
1250 case nir_op_ussub_4x8:
1251 result = qir_V8SUBS(c, src[0], src[1]);
1252 break;
1253
1254 case nir_op_umin_4x8:
1255 result = qir_V8MIN(c, src[0], src[1]);
1256 break;
1257
1258 case nir_op_umax_4x8:
1259 result = qir_V8MAX(c, src[0], src[1]);
1260 break;
1261
1262 case nir_op_umul_unorm_4x8:
1263 result = qir_V8MULD(c, src[0], src[1]);
1264 break;
1265
1266 case nir_op_fddx:
1267 case nir_op_fddx_coarse:
1268 case nir_op_fddx_fine:
1269 result = ntq_fddx(c, src[0]);
1270 break;
1271
1272 case nir_op_fddy:
1273 case nir_op_fddy_coarse:
1274 case nir_op_fddy_fine:
1275 result = ntq_fddy(c, src[0]);
1276 break;
1277
1278 default:
1279 fprintf(stderr, "unknown NIR ALU inst: ");
1280 nir_print_instr(&instr->instr, stderr);
1281 fprintf(stderr, "\n");
1282 abort();
1283 }
1284
1285 /* We have a scalar result, so the instruction should only have a
1286 * single channel written to.
1287 */
1288 assert(util_is_power_of_two(instr->dest.write_mask));
1289 ntq_store_dest(c, &instr->dest.dest,
1290 ffs(instr->dest.write_mask) - 1, result);
1291 }
1292
1293 static void
1294 emit_frag_end(struct vc4_compile *c)
1295 {
1296 struct qreg color;
1297 if (c->output_color_index != -1) {
1298 color = c->outputs[c->output_color_index];
1299 } else {
1300 color = qir_uniform_ui(c, 0);
1301 }
1302
1303 uint32_t discard_cond = QPU_COND_ALWAYS;
1304 if (c->s->info->fs.uses_discard) {
1305 qir_SF(c, c->discard);
1306 discard_cond = QPU_COND_ZS;
1307 }
1308
1309 if (c->fs_key->stencil_enabled) {
1310 qir_MOV_dest(c, qir_reg(QFILE_TLB_STENCIL_SETUP, 0),
1311 qir_uniform(c, QUNIFORM_STENCIL, 0));
1312 if (c->fs_key->stencil_twoside) {
1313 qir_MOV_dest(c, qir_reg(QFILE_TLB_STENCIL_SETUP, 0),
1314 qir_uniform(c, QUNIFORM_STENCIL, 1));
1315 }
1316 if (c->fs_key->stencil_full_writemasks) {
1317 qir_MOV_dest(c, qir_reg(QFILE_TLB_STENCIL_SETUP, 0),
1318 qir_uniform(c, QUNIFORM_STENCIL, 2));
1319 }
1320 }
1321
1322 if (c->output_sample_mask_index != -1) {
1323 qir_MS_MASK(c, c->outputs[c->output_sample_mask_index]);
1324 }
1325
1326 if (c->fs_key->depth_enabled) {
1327 if (c->output_position_index != -1) {
1328 qir_FTOI_dest(c, qir_reg(QFILE_TLB_Z_WRITE, 0),
1329 qir_FMUL(c,
1330 c->outputs[c->output_position_index],
1331 qir_uniform_f(c, 0xffffff)))->cond = discard_cond;
1332 } else {
1333 qir_MOV_dest(c, qir_reg(QFILE_TLB_Z_WRITE, 0),
1334 qir_FRAG_Z(c))->cond = discard_cond;
1335 }
1336 }
1337
1338 if (!c->msaa_per_sample_output) {
1339 qir_MOV_dest(c, qir_reg(QFILE_TLB_COLOR_WRITE, 0),
1340 color)->cond = discard_cond;
1341 } else {
1342 for (int i = 0; i < VC4_MAX_SAMPLES; i++) {
1343 qir_MOV_dest(c, qir_reg(QFILE_TLB_COLOR_WRITE_MS, 0),
1344 c->sample_colors[i])->cond = discard_cond;
1345 }
1346 }
1347 }
1348
1349 static void
1350 emit_scaled_viewport_write(struct vc4_compile *c, struct qreg rcp_w)
1351 {
1352 struct qreg packed = qir_get_temp(c);
1353
1354 for (int i = 0; i < 2; i++) {
1355 struct qreg scale =
1356 qir_uniform(c, QUNIFORM_VIEWPORT_X_SCALE + i, 0);
1357
1358 struct qreg packed_chan = packed;
1359 packed_chan.pack = QPU_PACK_A_16A + i;
1360
1361 qir_FTOI_dest(c, packed_chan,
1362 qir_FMUL(c,
1363 qir_FMUL(c,
1364 c->outputs[c->output_position_index + i],
1365 scale),
1366 rcp_w));
1367 }
1368
1369 qir_VPM_WRITE(c, packed);
1370 }
1371
1372 static void
1373 emit_zs_write(struct vc4_compile *c, struct qreg rcp_w)
1374 {
1375 struct qreg zscale = qir_uniform(c, QUNIFORM_VIEWPORT_Z_SCALE, 0);
1376 struct qreg zoffset = qir_uniform(c, QUNIFORM_VIEWPORT_Z_OFFSET, 0);
1377
1378 qir_VPM_WRITE(c, qir_FADD(c, qir_FMUL(c, qir_FMUL(c,
1379 c->outputs[c->output_position_index + 2],
1380 zscale),
1381 rcp_w),
1382 zoffset));
1383 }
1384
1385 static void
1386 emit_rcp_wc_write(struct vc4_compile *c, struct qreg rcp_w)
1387 {
1388 qir_VPM_WRITE(c, rcp_w);
1389 }
1390
1391 static void
1392 emit_point_size_write(struct vc4_compile *c)
1393 {
1394 struct qreg point_size;
1395
1396 if (c->output_point_size_index != -1)
1397 point_size = c->outputs[c->output_point_size_index];
1398 else
1399 point_size = qir_uniform_f(c, 1.0);
1400
1401 /* Workaround: HW-2726 PTB does not handle zero-size points (BCM2835,
1402 * BCM21553).
1403 */
1404 point_size = qir_FMAX(c, point_size, qir_uniform_f(c, .125));
1405
1406 qir_VPM_WRITE(c, point_size);
1407 }
1408
1409 /**
1410 * Emits a VPM read of the stub vertex attribute set up by vc4_draw.c.
1411 *
1412 * The simulator insists that there be at least one vertex attribute, so
1413 * vc4_draw.c will emit one if it wouldn't have otherwise. The simulator also
1414 * insists that all vertex attributes loaded get read by the VS/CS, so we have
1415 * to consume it here.
1416 */
1417 static void
1418 emit_stub_vpm_read(struct vc4_compile *c)
1419 {
1420 if (c->num_inputs)
1421 return;
1422
1423 c->vattr_sizes[0] = 4;
1424 (void)qir_MOV(c, qir_reg(QFILE_VPM, 0));
1425 c->num_inputs++;
1426 }
1427
1428 static void
1429 emit_vert_end(struct vc4_compile *c,
1430 struct vc4_varying_slot *fs_inputs,
1431 uint32_t num_fs_inputs)
1432 {
1433 struct qreg rcp_w = ntq_rcp(c, c->outputs[c->output_position_index + 3]);
1434
1435 emit_stub_vpm_read(c);
1436
1437 emit_scaled_viewport_write(c, rcp_w);
1438 emit_zs_write(c, rcp_w);
1439 emit_rcp_wc_write(c, rcp_w);
1440 if (c->vs_key->per_vertex_point_size)
1441 emit_point_size_write(c);
1442
1443 for (int i = 0; i < num_fs_inputs; i++) {
1444 struct vc4_varying_slot *input = &fs_inputs[i];
1445 int j;
1446
1447 for (j = 0; j < c->num_outputs; j++) {
1448 struct vc4_varying_slot *output =
1449 &c->output_slots[j];
1450
1451 if (input->slot == output->slot &&
1452 input->swizzle == output->swizzle) {
1453 qir_VPM_WRITE(c, c->outputs[j]);
1454 break;
1455 }
1456 }
1457 /* Emit padding if we didn't find a declared VS output for
1458 * this FS input.
1459 */
1460 if (j == c->num_outputs)
1461 qir_VPM_WRITE(c, qir_uniform_f(c, 0.0));
1462 }
1463 }
1464
1465 static void
1466 emit_coord_end(struct vc4_compile *c)
1467 {
1468 struct qreg rcp_w = qir_RCP(c, c->outputs[c->output_position_index + 3]);
1469
1470 emit_stub_vpm_read(c);
1471
1472 for (int i = 0; i < 4; i++)
1473 qir_VPM_WRITE(c, c->outputs[c->output_position_index + i]);
1474
1475 emit_scaled_viewport_write(c, rcp_w);
1476 emit_zs_write(c, rcp_w);
1477 emit_rcp_wc_write(c, rcp_w);
1478 if (c->vs_key->per_vertex_point_size)
1479 emit_point_size_write(c);
1480 }
1481
1482 static void
1483 vc4_optimize_nir(struct nir_shader *s)
1484 {
1485 bool progress;
1486
1487 do {
1488 progress = false;
1489
1490 NIR_PASS_V(s, nir_lower_vars_to_ssa);
1491 NIR_PASS(progress, s, nir_lower_alu_to_scalar);
1492 NIR_PASS(progress, s, nir_lower_phis_to_scalar);
1493 NIR_PASS(progress, s, nir_copy_prop);
1494 NIR_PASS(progress, s, nir_opt_remove_phis);
1495 NIR_PASS(progress, s, nir_opt_dce);
1496 NIR_PASS(progress, s, nir_opt_dead_cf);
1497 NIR_PASS(progress, s, nir_opt_cse);
1498 NIR_PASS(progress, s, nir_opt_peephole_select, 8);
1499 NIR_PASS(progress, s, nir_opt_algebraic);
1500 NIR_PASS(progress, s, nir_opt_constant_folding);
1501 NIR_PASS(progress, s, nir_opt_undef);
1502 } while (progress);
1503 }
1504
1505 static int
1506 driver_location_compare(const void *in_a, const void *in_b)
1507 {
1508 const nir_variable *const *a = in_a;
1509 const nir_variable *const *b = in_b;
1510
1511 return (*a)->data.driver_location - (*b)->data.driver_location;
1512 }
1513
1514 static void
1515 ntq_setup_inputs(struct vc4_compile *c)
1516 {
1517 unsigned num_entries = 0;
1518 nir_foreach_variable(var, &c->s->inputs)
1519 num_entries++;
1520
1521 nir_variable *vars[num_entries];
1522
1523 unsigned i = 0;
1524 nir_foreach_variable(var, &c->s->inputs)
1525 vars[i++] = var;
1526
1527 /* Sort the variables so that we emit the input setup in
1528 * driver_location order. This is required for VPM reads, whose data
1529 * is fetched into the VPM in driver_location (TGSI register index)
1530 * order.
1531 */
1532 qsort(&vars, num_entries, sizeof(*vars), driver_location_compare);
1533
1534 for (unsigned i = 0; i < num_entries; i++) {
1535 nir_variable *var = vars[i];
1536 unsigned array_len = MAX2(glsl_get_length(var->type), 1);
1537 unsigned loc = var->data.driver_location;
1538
1539 assert(array_len == 1);
1540 (void)array_len;
1541 resize_qreg_array(c, &c->inputs, &c->inputs_array_size,
1542 (loc + 1) * 4);
1543
1544 if (c->stage == QSTAGE_FRAG) {
1545 if (var->data.location == VARYING_SLOT_POS) {
1546 emit_fragcoord_input(c, loc);
1547 } else if (var->data.location == VARYING_SLOT_PNTC ||
1548 (var->data.location >= VARYING_SLOT_VAR0 &&
1549 (c->fs_key->point_sprite_mask &
1550 (1 << (var->data.location -
1551 VARYING_SLOT_VAR0))))) {
1552 c->inputs[loc * 4 + 0] = c->point_x;
1553 c->inputs[loc * 4 + 1] = c->point_y;
1554 } else {
1555 emit_fragment_input(c, loc, var->data.location);
1556 }
1557 } else {
1558 emit_vertex_input(c, loc);
1559 }
1560 }
1561 }
1562
1563 static void
1564 ntq_setup_outputs(struct vc4_compile *c)
1565 {
1566 nir_foreach_variable(var, &c->s->outputs) {
1567 unsigned array_len = MAX2(glsl_get_length(var->type), 1);
1568 unsigned loc = var->data.driver_location * 4;
1569
1570 assert(array_len == 1);
1571 (void)array_len;
1572
1573 for (int i = 0; i < 4; i++)
1574 add_output(c, loc + i, var->data.location, i);
1575
1576 if (c->stage == QSTAGE_FRAG) {
1577 switch (var->data.location) {
1578 case FRAG_RESULT_COLOR:
1579 case FRAG_RESULT_DATA0:
1580 c->output_color_index = loc;
1581 break;
1582 case FRAG_RESULT_DEPTH:
1583 c->output_position_index = loc;
1584 break;
1585 case FRAG_RESULT_SAMPLE_MASK:
1586 c->output_sample_mask_index = loc;
1587 break;
1588 }
1589 } else {
1590 switch (var->data.location) {
1591 case VARYING_SLOT_POS:
1592 c->output_position_index = loc;
1593 break;
1594 case VARYING_SLOT_PSIZ:
1595 c->output_point_size_index = loc;
1596 break;
1597 }
1598 }
1599 }
1600 }
1601
1602 static void
1603 ntq_setup_uniforms(struct vc4_compile *c)
1604 {
1605 nir_foreach_variable(var, &c->s->uniforms) {
1606 uint32_t vec4_count = st_glsl_type_size(var->type);
1607 unsigned vec4_size = 4 * sizeof(float);
1608
1609 declare_uniform_range(c, var->data.driver_location * vec4_size,
1610 vec4_count * vec4_size);
1611
1612 }
1613 }
1614
1615 /**
1616 * Sets up the mapping from nir_register to struct qreg *.
1617 *
1618 * Each nir_register gets a struct qreg per 32-bit component being stored.
1619 */
1620 static void
1621 ntq_setup_registers(struct vc4_compile *c, struct exec_list *list)
1622 {
1623 foreach_list_typed(nir_register, nir_reg, node, list) {
1624 unsigned array_len = MAX2(nir_reg->num_array_elems, 1);
1625 struct qreg *qregs = ralloc_array(c->def_ht, struct qreg,
1626 array_len *
1627 nir_reg->num_components);
1628
1629 _mesa_hash_table_insert(c->def_ht, nir_reg, qregs);
1630
1631 for (int i = 0; i < array_len * nir_reg->num_components; i++)
1632 qregs[i] = qir_get_temp(c);
1633 }
1634 }
1635
1636 static void
1637 ntq_emit_load_const(struct vc4_compile *c, nir_load_const_instr *instr)
1638 {
1639 struct qreg *qregs = ntq_init_ssa_def(c, &instr->def);
1640 for (int i = 0; i < instr->def.num_components; i++)
1641 qregs[i] = qir_uniform_ui(c, instr->value.u32[i]);
1642
1643 _mesa_hash_table_insert(c->def_ht, &instr->def, qregs);
1644 }
1645
1646 static void
1647 ntq_emit_ssa_undef(struct vc4_compile *c, nir_ssa_undef_instr *instr)
1648 {
1649 struct qreg *qregs = ntq_init_ssa_def(c, &instr->def);
1650
1651 /* QIR needs there to be *some* value, so pick 0 (same as for
1652 * ntq_setup_registers().
1653 */
1654 for (int i = 0; i < instr->def.num_components; i++)
1655 qregs[i] = qir_uniform_ui(c, 0);
1656 }
1657
1658 static void
1659 ntq_emit_intrinsic(struct vc4_compile *c, nir_intrinsic_instr *instr)
1660 {
1661 nir_const_value *const_offset;
1662 unsigned offset;
1663
1664 switch (instr->intrinsic) {
1665 case nir_intrinsic_load_uniform:
1666 assert(instr->num_components == 1);
1667 const_offset = nir_src_as_const_value(instr->src[0]);
1668 if (const_offset) {
1669 offset = nir_intrinsic_base(instr) + const_offset->u32[0];
1670 assert(offset % 4 == 0);
1671 /* We need dwords */
1672 offset = offset / 4;
1673 ntq_store_dest(c, &instr->dest, 0,
1674 qir_uniform(c, QUNIFORM_UNIFORM,
1675 offset));
1676 } else {
1677 ntq_store_dest(c, &instr->dest, 0,
1678 indirect_uniform_load(c, instr));
1679 }
1680 break;
1681
1682 case nir_intrinsic_load_user_clip_plane:
1683 for (int i = 0; i < instr->num_components; i++) {
1684 ntq_store_dest(c, &instr->dest, i,
1685 qir_uniform(c, QUNIFORM_USER_CLIP_PLANE,
1686 nir_intrinsic_ucp_id(instr) *
1687 4 + i));
1688 }
1689 break;
1690
1691 case nir_intrinsic_load_blend_const_color_r_float:
1692 case nir_intrinsic_load_blend_const_color_g_float:
1693 case nir_intrinsic_load_blend_const_color_b_float:
1694 case nir_intrinsic_load_blend_const_color_a_float:
1695 ntq_store_dest(c, &instr->dest, 0,
1696 qir_uniform(c, QUNIFORM_BLEND_CONST_COLOR_X +
1697 (instr->intrinsic -
1698 nir_intrinsic_load_blend_const_color_r_float),
1699 0));
1700 break;
1701
1702 case nir_intrinsic_load_blend_const_color_rgba8888_unorm:
1703 ntq_store_dest(c, &instr->dest, 0,
1704 qir_uniform(c, QUNIFORM_BLEND_CONST_COLOR_RGBA,
1705 0));
1706 break;
1707
1708 case nir_intrinsic_load_blend_const_color_aaaa8888_unorm:
1709 ntq_store_dest(c, &instr->dest, 0,
1710 qir_uniform(c, QUNIFORM_BLEND_CONST_COLOR_AAAA,
1711 0));
1712 break;
1713
1714 case nir_intrinsic_load_alpha_ref_float:
1715 ntq_store_dest(c, &instr->dest, 0,
1716 qir_uniform(c, QUNIFORM_ALPHA_REF, 0));
1717 break;
1718
1719 case nir_intrinsic_load_sample_mask_in:
1720 ntq_store_dest(c, &instr->dest, 0,
1721 qir_uniform(c, QUNIFORM_SAMPLE_MASK, 0));
1722 break;
1723
1724 case nir_intrinsic_load_front_face:
1725 /* The register contains 0 (front) or 1 (back), and we need to
1726 * turn it into a NIR bool where true means front.
1727 */
1728 ntq_store_dest(c, &instr->dest, 0,
1729 qir_ADD(c,
1730 qir_uniform_ui(c, -1),
1731 qir_reg(QFILE_FRAG_REV_FLAG, 0)));
1732 break;
1733
1734 case nir_intrinsic_load_input:
1735 assert(instr->num_components == 1);
1736 const_offset = nir_src_as_const_value(instr->src[0]);
1737 assert(const_offset && "vc4 doesn't support indirect inputs");
1738 if (c->stage == QSTAGE_FRAG &&
1739 nir_intrinsic_base(instr) >= VC4_NIR_TLB_COLOR_READ_INPUT) {
1740 assert(const_offset->u32[0] == 0);
1741 /* Reads of the per-sample color need to be done in
1742 * order.
1743 */
1744 int sample_index = (nir_intrinsic_base(instr) -
1745 VC4_NIR_TLB_COLOR_READ_INPUT);
1746 for (int i = 0; i <= sample_index; i++) {
1747 if (c->color_reads[i].file == QFILE_NULL) {
1748 c->color_reads[i] =
1749 qir_TLB_COLOR_READ(c);
1750 }
1751 }
1752 ntq_store_dest(c, &instr->dest, 0,
1753 qir_MOV(c, c->color_reads[sample_index]));
1754 } else {
1755 offset = nir_intrinsic_base(instr) + const_offset->u32[0];
1756 int comp = nir_intrinsic_component(instr);
1757 ntq_store_dest(c, &instr->dest, 0,
1758 qir_MOV(c, c->inputs[offset * 4 + comp]));
1759 }
1760 break;
1761
1762 case nir_intrinsic_store_output:
1763 const_offset = nir_src_as_const_value(instr->src[1]);
1764 assert(const_offset && "vc4 doesn't support indirect outputs");
1765 offset = nir_intrinsic_base(instr) + const_offset->u32[0];
1766
1767 /* MSAA color outputs are the only case where we have an
1768 * output that's not lowered to being a store of a single 32
1769 * bit value.
1770 */
1771 if (c->stage == QSTAGE_FRAG && instr->num_components == 4) {
1772 assert(offset == c->output_color_index);
1773 for (int i = 0; i < 4; i++) {
1774 c->sample_colors[i] =
1775 qir_MOV(c, ntq_get_src(c, instr->src[0],
1776 i));
1777 }
1778 } else {
1779 offset = offset * 4 + nir_intrinsic_component(instr);
1780 assert(instr->num_components == 1);
1781 c->outputs[offset] =
1782 qir_MOV(c, ntq_get_src(c, instr->src[0], 0));
1783 c->num_outputs = MAX2(c->num_outputs, offset + 1);
1784 }
1785 break;
1786
1787 case nir_intrinsic_discard:
1788 if (c->execute.file != QFILE_NULL) {
1789 qir_SF(c, c->execute);
1790 qir_MOV_cond(c, QPU_COND_ZS, c->discard,
1791 qir_uniform_ui(c, ~0));
1792 } else {
1793 qir_MOV_dest(c, c->discard, qir_uniform_ui(c, ~0));
1794 }
1795 break;
1796
1797 case nir_intrinsic_discard_if: {
1798 /* true (~0) if we're discarding */
1799 struct qreg cond = ntq_get_src(c, instr->src[0], 0);
1800
1801 if (c->execute.file != QFILE_NULL) {
1802 /* execute == 0 means the channel is active. Invert
1803 * the condition so that we can use zero as "executing
1804 * and discarding."
1805 */
1806 qir_SF(c, qir_AND(c, c->execute, qir_NOT(c, cond)));
1807 qir_MOV_cond(c, QPU_COND_ZS, c->discard, cond);
1808 } else {
1809 qir_OR_dest(c, c->discard, c->discard,
1810 ntq_get_src(c, instr->src[0], 0));
1811 }
1812
1813 break;
1814 }
1815
1816 default:
1817 fprintf(stderr, "Unknown intrinsic: ");
1818 nir_print_instr(&instr->instr, stderr);
1819 fprintf(stderr, "\n");
1820 break;
1821 }
1822 }
1823
1824 /* Clears (activates) the execute flags for any channels whose jump target
1825 * matches this block.
1826 */
1827 static void
1828 ntq_activate_execute_for_block(struct vc4_compile *c)
1829 {
1830 qir_SF(c, qir_SUB(c,
1831 c->execute,
1832 qir_uniform_ui(c, c->cur_block->index)));
1833 qir_MOV_cond(c, QPU_COND_ZS, c->execute, qir_uniform_ui(c, 0));
1834 }
1835
1836 static void
1837 ntq_emit_if(struct vc4_compile *c, nir_if *if_stmt)
1838 {
1839 if (!c->vc4->screen->has_control_flow) {
1840 fprintf(stderr,
1841 "IF statement support requires updated kernel.\n");
1842 return;
1843 }
1844
1845 nir_block *nir_else_block = nir_if_first_else_block(if_stmt);
1846 bool empty_else_block =
1847 (nir_else_block == nir_if_last_else_block(if_stmt) &&
1848 exec_list_is_empty(&nir_else_block->instr_list));
1849
1850 struct qblock *then_block = qir_new_block(c);
1851 struct qblock *after_block = qir_new_block(c);
1852 struct qblock *else_block;
1853 if (empty_else_block)
1854 else_block = after_block;
1855 else
1856 else_block = qir_new_block(c);
1857
1858 bool was_top_level = false;
1859 if (c->execute.file == QFILE_NULL) {
1860 c->execute = qir_MOV(c, qir_uniform_ui(c, 0));
1861 was_top_level = true;
1862 }
1863
1864 /* Set ZS for executing (execute == 0) and jumping (if->condition ==
1865 * 0) channels, and then update execute flags for those to point to
1866 * the ELSE block.
1867 */
1868 qir_SF(c, qir_OR(c,
1869 c->execute,
1870 ntq_get_src(c, if_stmt->condition, 0)));
1871 qir_MOV_cond(c, QPU_COND_ZS, c->execute,
1872 qir_uniform_ui(c, else_block->index));
1873
1874 /* Jump to ELSE if nothing is active for THEN, otherwise fall
1875 * through.
1876 */
1877 qir_SF(c, c->execute);
1878 qir_BRANCH(c, QPU_COND_BRANCH_ALL_ZC);
1879 qir_link_blocks(c->cur_block, else_block);
1880 qir_link_blocks(c->cur_block, then_block);
1881
1882 /* Process the THEN block. */
1883 qir_set_emit_block(c, then_block);
1884 ntq_emit_cf_list(c, &if_stmt->then_list);
1885
1886 if (!empty_else_block) {
1887 /* Handle the end of the THEN block. First, all currently
1888 * active channels update their execute flags to point to
1889 * ENDIF
1890 */
1891 qir_SF(c, c->execute);
1892 qir_MOV_cond(c, QPU_COND_ZS, c->execute,
1893 qir_uniform_ui(c, after_block->index));
1894
1895 /* If everything points at ENDIF, then jump there immediately. */
1896 qir_SF(c, qir_SUB(c, c->execute, qir_uniform_ui(c, after_block->index)));
1897 qir_BRANCH(c, QPU_COND_BRANCH_ALL_ZS);
1898 qir_link_blocks(c->cur_block, after_block);
1899 qir_link_blocks(c->cur_block, else_block);
1900
1901 qir_set_emit_block(c, else_block);
1902 ntq_activate_execute_for_block(c);
1903 ntq_emit_cf_list(c, &if_stmt->else_list);
1904 }
1905
1906 qir_link_blocks(c->cur_block, after_block);
1907
1908 qir_set_emit_block(c, after_block);
1909 if (was_top_level)
1910 c->execute = c->undef;
1911 else
1912 ntq_activate_execute_for_block(c);
1913
1914 }
1915
1916 static void
1917 ntq_emit_jump(struct vc4_compile *c, nir_jump_instr *jump)
1918 {
1919 switch (jump->type) {
1920 case nir_jump_break:
1921 qir_SF(c, c->execute);
1922 qir_MOV_cond(c, QPU_COND_ZS, c->execute,
1923 qir_uniform_ui(c, c->loop_break_block->index));
1924 break;
1925
1926 case nir_jump_continue:
1927 qir_SF(c, c->execute);
1928 qir_MOV_cond(c, QPU_COND_ZS, c->execute,
1929 qir_uniform_ui(c, c->loop_cont_block->index));
1930 break;
1931
1932 case nir_jump_return:
1933 unreachable("All returns shouold be lowered\n");
1934 }
1935 }
1936
1937 static void
1938 ntq_emit_instr(struct vc4_compile *c, nir_instr *instr)
1939 {
1940 switch (instr->type) {
1941 case nir_instr_type_alu:
1942 ntq_emit_alu(c, nir_instr_as_alu(instr));
1943 break;
1944
1945 case nir_instr_type_intrinsic:
1946 ntq_emit_intrinsic(c, nir_instr_as_intrinsic(instr));
1947 break;
1948
1949 case nir_instr_type_load_const:
1950 ntq_emit_load_const(c, nir_instr_as_load_const(instr));
1951 break;
1952
1953 case nir_instr_type_ssa_undef:
1954 ntq_emit_ssa_undef(c, nir_instr_as_ssa_undef(instr));
1955 break;
1956
1957 case nir_instr_type_tex:
1958 ntq_emit_tex(c, nir_instr_as_tex(instr));
1959 break;
1960
1961 case nir_instr_type_jump:
1962 ntq_emit_jump(c, nir_instr_as_jump(instr));
1963 break;
1964
1965 default:
1966 fprintf(stderr, "Unknown NIR instr type: ");
1967 nir_print_instr(instr, stderr);
1968 fprintf(stderr, "\n");
1969 abort();
1970 }
1971 }
1972
1973 static void
1974 ntq_emit_block(struct vc4_compile *c, nir_block *block)
1975 {
1976 nir_foreach_instr(instr, block) {
1977 ntq_emit_instr(c, instr);
1978 }
1979 }
1980
1981 static void ntq_emit_cf_list(struct vc4_compile *c, struct exec_list *list);
1982
1983 static void
1984 ntq_emit_loop(struct vc4_compile *c, nir_loop *loop)
1985 {
1986 if (!c->vc4->screen->has_control_flow) {
1987 fprintf(stderr,
1988 "loop support requires updated kernel.\n");
1989 ntq_emit_cf_list(c, &loop->body);
1990 return;
1991 }
1992
1993 bool was_top_level = false;
1994 if (c->execute.file == QFILE_NULL) {
1995 c->execute = qir_MOV(c, qir_uniform_ui(c, 0));
1996 was_top_level = true;
1997 }
1998
1999 struct qblock *save_loop_cont_block = c->loop_cont_block;
2000 struct qblock *save_loop_break_block = c->loop_break_block;
2001
2002 c->loop_cont_block = qir_new_block(c);
2003 c->loop_break_block = qir_new_block(c);
2004
2005 qir_link_blocks(c->cur_block, c->loop_cont_block);
2006 qir_set_emit_block(c, c->loop_cont_block);
2007 ntq_activate_execute_for_block(c);
2008
2009 ntq_emit_cf_list(c, &loop->body);
2010
2011 /* If anything had explicitly continued, or is here at the end of the
2012 * loop, then we need to loop again. SF updates are masked by the
2013 * instruction's condition, so we can do the OR of the two conditions
2014 * within SF.
2015 */
2016 qir_SF(c, c->execute);
2017 struct qinst *cont_check =
2018 qir_SUB_dest(c,
2019 c->undef,
2020 c->execute,
2021 qir_uniform_ui(c, c->loop_cont_block->index));
2022 cont_check->cond = QPU_COND_ZC;
2023 cont_check->sf = true;
2024
2025 qir_BRANCH(c, QPU_COND_BRANCH_ANY_ZS);
2026 qir_link_blocks(c->cur_block, c->loop_cont_block);
2027 qir_link_blocks(c->cur_block, c->loop_break_block);
2028
2029 qir_set_emit_block(c, c->loop_break_block);
2030 if (was_top_level)
2031 c->execute = c->undef;
2032 else
2033 ntq_activate_execute_for_block(c);
2034
2035 c->loop_break_block = save_loop_break_block;
2036 c->loop_cont_block = save_loop_cont_block;
2037 }
2038
2039 static void
2040 ntq_emit_function(struct vc4_compile *c, nir_function_impl *func)
2041 {
2042 fprintf(stderr, "FUNCTIONS not handled.\n");
2043 abort();
2044 }
2045
2046 static void
2047 ntq_emit_cf_list(struct vc4_compile *c, struct exec_list *list)
2048 {
2049 foreach_list_typed(nir_cf_node, node, node, list) {
2050 switch (node->type) {
2051 case nir_cf_node_block:
2052 ntq_emit_block(c, nir_cf_node_as_block(node));
2053 break;
2054
2055 case nir_cf_node_if:
2056 ntq_emit_if(c, nir_cf_node_as_if(node));
2057 break;
2058
2059 case nir_cf_node_loop:
2060 ntq_emit_loop(c, nir_cf_node_as_loop(node));
2061 break;
2062
2063 case nir_cf_node_function:
2064 ntq_emit_function(c, nir_cf_node_as_function(node));
2065 break;
2066
2067 default:
2068 fprintf(stderr, "Unknown NIR node type\n");
2069 abort();
2070 }
2071 }
2072 }
2073
2074 static void
2075 ntq_emit_impl(struct vc4_compile *c, nir_function_impl *impl)
2076 {
2077 ntq_setup_registers(c, &impl->registers);
2078 ntq_emit_cf_list(c, &impl->body);
2079 }
2080
2081 static void
2082 nir_to_qir(struct vc4_compile *c)
2083 {
2084 if (c->stage == QSTAGE_FRAG && c->s->info->fs.uses_discard)
2085 c->discard = qir_MOV(c, qir_uniform_ui(c, 0));
2086
2087 ntq_setup_inputs(c);
2088 ntq_setup_outputs(c);
2089 ntq_setup_uniforms(c);
2090 ntq_setup_registers(c, &c->s->registers);
2091
2092 /* Find the main function and emit the body. */
2093 nir_foreach_function(function, c->s) {
2094 assert(strcmp(function->name, "main") == 0);
2095 assert(function->impl);
2096 ntq_emit_impl(c, function->impl);
2097 }
2098 }
2099
2100 static const nir_shader_compiler_options nir_options = {
2101 .lower_extract_byte = true,
2102 .lower_extract_word = true,
2103 .lower_ffma = true,
2104 .lower_flrp32 = true,
2105 .lower_fpow = true,
2106 .lower_fsat = true,
2107 .lower_fsqrt = true,
2108 .lower_negate = true,
2109 .native_integers = true,
2110 };
2111
2112 const void *
2113 vc4_screen_get_compiler_options(struct pipe_screen *pscreen,
2114 enum pipe_shader_ir ir, unsigned shader)
2115 {
2116 return &nir_options;
2117 }
2118
2119 static int
2120 count_nir_instrs(nir_shader *nir)
2121 {
2122 int count = 0;
2123 nir_foreach_function(function, nir) {
2124 if (!function->impl)
2125 continue;
2126 nir_foreach_block(block, function->impl) {
2127 nir_foreach_instr(instr, block)
2128 count++;
2129 }
2130 }
2131 return count;
2132 }
2133
2134 static struct vc4_compile *
2135 vc4_shader_ntq(struct vc4_context *vc4, enum qstage stage,
2136 struct vc4_key *key)
2137 {
2138 struct vc4_compile *c = qir_compile_init();
2139
2140 c->vc4 = vc4;
2141 c->stage = stage;
2142 c->shader_state = &key->shader_state->base;
2143 c->program_id = key->shader_state->program_id;
2144 c->variant_id =
2145 p_atomic_inc_return(&key->shader_state->compiled_variant_count);
2146
2147 c->key = key;
2148 switch (stage) {
2149 case QSTAGE_FRAG:
2150 c->fs_key = (struct vc4_fs_key *)key;
2151 if (c->fs_key->is_points) {
2152 c->point_x = emit_fragment_varying(c, ~0, 0);
2153 c->point_y = emit_fragment_varying(c, ~0, 0);
2154 } else if (c->fs_key->is_lines) {
2155 c->line_x = emit_fragment_varying(c, ~0, 0);
2156 }
2157 break;
2158 case QSTAGE_VERT:
2159 c->vs_key = (struct vc4_vs_key *)key;
2160 break;
2161 case QSTAGE_COORD:
2162 c->vs_key = (struct vc4_vs_key *)key;
2163 break;
2164 }
2165
2166 c->s = nir_shader_clone(c, key->shader_state->base.ir.nir);
2167
2168 if (stage == QSTAGE_FRAG)
2169 NIR_PASS_V(c->s, vc4_nir_lower_blend, c);
2170
2171 struct nir_lower_tex_options tex_options = {
2172 /* We would need to implement txs, but we don't want the
2173 * int/float conversions
2174 */
2175 .lower_rect = false,
2176
2177 .lower_txp = ~0,
2178
2179 /* Apply swizzles to all samplers. */
2180 .swizzle_result = ~0,
2181 };
2182
2183 /* Lower the format swizzle and ARB_texture_swizzle-style swizzle.
2184 * The format swizzling applies before sRGB decode, and
2185 * ARB_texture_swizzle is the last thing before returning the sample.
2186 */
2187 for (int i = 0; i < ARRAY_SIZE(key->tex); i++) {
2188 enum pipe_format format = c->key->tex[i].format;
2189
2190 if (!format)
2191 continue;
2192
2193 const uint8_t *format_swizzle = vc4_get_format_swizzle(format);
2194
2195 for (int j = 0; j < 4; j++) {
2196 uint8_t arb_swiz = c->key->tex[i].swizzle[j];
2197
2198 if (arb_swiz <= 3) {
2199 tex_options.swizzles[i][j] =
2200 format_swizzle[arb_swiz];
2201 } else {
2202 tex_options.swizzles[i][j] = arb_swiz;
2203 }
2204 }
2205
2206 if (util_format_is_srgb(format))
2207 tex_options.lower_srgb |= (1 << i);
2208 }
2209
2210 NIR_PASS_V(c->s, nir_lower_tex, &tex_options);
2211
2212 if (c->fs_key && c->fs_key->light_twoside)
2213 NIR_PASS_V(c->s, nir_lower_two_sided_color);
2214
2215 if (c->vs_key && c->vs_key->clamp_color)
2216 NIR_PASS_V(c->s, nir_lower_clamp_color_outputs);
2217
2218 if (c->key->ucp_enables) {
2219 if (stage == QSTAGE_FRAG) {
2220 NIR_PASS_V(c->s, nir_lower_clip_fs, c->key->ucp_enables);
2221 } else {
2222 NIR_PASS_V(c->s, nir_lower_clip_vs, c->key->ucp_enables);
2223 NIR_PASS_V(c->s, nir_lower_io_to_scalar,
2224 nir_var_shader_out);
2225 }
2226 }
2227
2228 /* FS input scalarizing must happen after nir_lower_two_sided_color,
2229 * which only handles a vec4 at a time. Similarly, VS output
2230 * scalarizing must happen after nir_lower_clip_vs.
2231 */
2232 if (c->stage == QSTAGE_FRAG)
2233 NIR_PASS_V(c->s, nir_lower_io_to_scalar, nir_var_shader_in);
2234 else
2235 NIR_PASS_V(c->s, nir_lower_io_to_scalar, nir_var_shader_out);
2236
2237 NIR_PASS_V(c->s, vc4_nir_lower_io, c);
2238 NIR_PASS_V(c->s, vc4_nir_lower_txf_ms, c);
2239 NIR_PASS_V(c->s, nir_lower_idiv);
2240
2241 vc4_optimize_nir(c->s);
2242
2243 NIR_PASS_V(c->s, nir_convert_from_ssa, true);
2244
2245 if (vc4_debug & VC4_DEBUG_SHADERDB) {
2246 fprintf(stderr, "SHADER-DB: %s prog %d/%d: %d NIR instructions\n",
2247 qir_get_stage_name(c->stage),
2248 c->program_id, c->variant_id,
2249 count_nir_instrs(c->s));
2250 }
2251
2252 if (vc4_debug & VC4_DEBUG_NIR) {
2253 fprintf(stderr, "%s prog %d/%d NIR:\n",
2254 qir_get_stage_name(c->stage),
2255 c->program_id, c->variant_id);
2256 nir_print_shader(c->s, stderr);
2257 }
2258
2259 nir_to_qir(c);
2260
2261 switch (stage) {
2262 case QSTAGE_FRAG:
2263 emit_frag_end(c);
2264 break;
2265 case QSTAGE_VERT:
2266 emit_vert_end(c,
2267 c->vs_key->fs_inputs->input_slots,
2268 c->vs_key->fs_inputs->num_inputs);
2269 break;
2270 case QSTAGE_COORD:
2271 emit_coord_end(c);
2272 break;
2273 }
2274
2275 if (vc4_debug & VC4_DEBUG_QIR) {
2276 fprintf(stderr, "%s prog %d/%d pre-opt QIR:\n",
2277 qir_get_stage_name(c->stage),
2278 c->program_id, c->variant_id);
2279 qir_dump(c);
2280 fprintf(stderr, "\n");
2281 }
2282
2283 qir_optimize(c);
2284 qir_lower_uniforms(c);
2285
2286 qir_schedule_instructions(c);
2287 qir_emit_uniform_stream_resets(c);
2288
2289 if (vc4_debug & VC4_DEBUG_QIR) {
2290 fprintf(stderr, "%s prog %d/%d QIR:\n",
2291 qir_get_stage_name(c->stage),
2292 c->program_id, c->variant_id);
2293 qir_dump(c);
2294 fprintf(stderr, "\n");
2295 }
2296
2297 qir_reorder_uniforms(c);
2298 vc4_generate_code(vc4, c);
2299
2300 if (vc4_debug & VC4_DEBUG_SHADERDB) {
2301 fprintf(stderr, "SHADER-DB: %s prog %d/%d: %d instructions\n",
2302 qir_get_stage_name(c->stage),
2303 c->program_id, c->variant_id,
2304 c->qpu_inst_count);
2305 fprintf(stderr, "SHADER-DB: %s prog %d/%d: %d uniforms\n",
2306 qir_get_stage_name(c->stage),
2307 c->program_id, c->variant_id,
2308 c->num_uniforms);
2309 }
2310
2311 ralloc_free(c->s);
2312
2313 return c;
2314 }
2315
2316 static void *
2317 vc4_shader_state_create(struct pipe_context *pctx,
2318 const struct pipe_shader_state *cso)
2319 {
2320 struct vc4_context *vc4 = vc4_context(pctx);
2321 struct vc4_uncompiled_shader *so = CALLOC_STRUCT(vc4_uncompiled_shader);
2322 if (!so)
2323 return NULL;
2324
2325 so->program_id = vc4->next_uncompiled_program_id++;
2326
2327 nir_shader *s;
2328
2329 if (cso->type == PIPE_SHADER_IR_NIR) {
2330 /* The backend takes ownership of the NIR shader on state
2331 * creation.
2332 */
2333 s = cso->ir.nir;
2334 } else {
2335 assert(cso->type == PIPE_SHADER_IR_TGSI);
2336
2337 if (vc4_debug & VC4_DEBUG_TGSI) {
2338 fprintf(stderr, "prog %d TGSI:\n",
2339 so->program_id);
2340 tgsi_dump(cso->tokens, 0);
2341 fprintf(stderr, "\n");
2342 }
2343 s = tgsi_to_nir(cso->tokens, &nir_options);
2344 }
2345
2346 NIR_PASS_V(s, nir_opt_global_to_local);
2347 NIR_PASS_V(s, nir_convert_to_ssa);
2348 NIR_PASS_V(s, nir_normalize_cubemap_coords);
2349
2350 NIR_PASS_V(s, nir_lower_load_const_to_scalar);
2351
2352 vc4_optimize_nir(s);
2353
2354 NIR_PASS_V(s, nir_remove_dead_variables, nir_var_local);
2355
2356 /* Garbage collect dead instructions */
2357 nir_sweep(s);
2358
2359 so->base.type = PIPE_SHADER_IR_NIR;
2360 so->base.ir.nir = s;
2361
2362 if (vc4_debug & VC4_DEBUG_NIR) {
2363 fprintf(stderr, "%s prog %d NIR:\n",
2364 gl_shader_stage_name(s->stage),
2365 so->program_id);
2366 nir_print_shader(s, stderr);
2367 fprintf(stderr, "\n");
2368 }
2369
2370 return so;
2371 }
2372
2373 static void
2374 copy_uniform_state_to_shader(struct vc4_compiled_shader *shader,
2375 struct vc4_compile *c)
2376 {
2377 int count = c->num_uniforms;
2378 struct vc4_shader_uniform_info *uinfo = &shader->uniforms;
2379
2380 uinfo->count = count;
2381 uinfo->data = ralloc_array(shader, uint32_t, count);
2382 memcpy(uinfo->data, c->uniform_data,
2383 count * sizeof(*uinfo->data));
2384 uinfo->contents = ralloc_array(shader, enum quniform_contents, count);
2385 memcpy(uinfo->contents, c->uniform_contents,
2386 count * sizeof(*uinfo->contents));
2387 uinfo->num_texture_samples = c->num_texture_samples;
2388
2389 vc4_set_shader_uniform_dirty_flags(shader);
2390 }
2391
2392 static void
2393 vc4_setup_compiled_fs_inputs(struct vc4_context *vc4, struct vc4_compile *c,
2394 struct vc4_compiled_shader *shader)
2395 {
2396 struct vc4_fs_inputs inputs;
2397
2398 memset(&inputs, 0, sizeof(inputs));
2399 inputs.input_slots = ralloc_array(shader,
2400 struct vc4_varying_slot,
2401 c->num_input_slots);
2402
2403 bool input_live[c->num_input_slots];
2404
2405 memset(input_live, 0, sizeof(input_live));
2406 qir_for_each_inst_inorder(inst, c) {
2407 for (int i = 0; i < qir_get_op_nsrc(inst->op); i++) {
2408 if (inst->src[i].file == QFILE_VARY)
2409 input_live[inst->src[i].index] = true;
2410 }
2411 }
2412
2413 for (int i = 0; i < c->num_input_slots; i++) {
2414 struct vc4_varying_slot *slot = &c->input_slots[i];
2415
2416 if (!input_live[i])
2417 continue;
2418
2419 /* Skip non-VS-output inputs. */
2420 if (slot->slot == (uint8_t)~0)
2421 continue;
2422
2423 if (slot->slot == VARYING_SLOT_COL0 ||
2424 slot->slot == VARYING_SLOT_COL1 ||
2425 slot->slot == VARYING_SLOT_BFC0 ||
2426 slot->slot == VARYING_SLOT_BFC1) {
2427 shader->color_inputs |= (1 << inputs.num_inputs);
2428 }
2429
2430 inputs.input_slots[inputs.num_inputs] = *slot;
2431 inputs.num_inputs++;
2432 }
2433 shader->num_inputs = inputs.num_inputs;
2434
2435 /* Add our set of inputs to the set of all inputs seen. This way, we
2436 * can have a single pointer that identifies an FS inputs set,
2437 * allowing VS to avoid recompiling when the FS is recompiled (or a
2438 * new one is bound using separate shader objects) but the inputs
2439 * don't change.
2440 */
2441 struct set_entry *entry = _mesa_set_search(vc4->fs_inputs_set, &inputs);
2442 if (entry) {
2443 shader->fs_inputs = entry->key;
2444 ralloc_free(inputs.input_slots);
2445 } else {
2446 struct vc4_fs_inputs *alloc_inputs;
2447
2448 alloc_inputs = rzalloc(vc4->fs_inputs_set, struct vc4_fs_inputs);
2449 memcpy(alloc_inputs, &inputs, sizeof(inputs));
2450 ralloc_steal(alloc_inputs, inputs.input_slots);
2451 _mesa_set_add(vc4->fs_inputs_set, alloc_inputs);
2452
2453 shader->fs_inputs = alloc_inputs;
2454 }
2455 }
2456
2457 static struct vc4_compiled_shader *
2458 vc4_get_compiled_shader(struct vc4_context *vc4, enum qstage stage,
2459 struct vc4_key *key)
2460 {
2461 struct hash_table *ht;
2462 uint32_t key_size;
2463 if (stage == QSTAGE_FRAG) {
2464 ht = vc4->fs_cache;
2465 key_size = sizeof(struct vc4_fs_key);
2466 } else {
2467 ht = vc4->vs_cache;
2468 key_size = sizeof(struct vc4_vs_key);
2469 }
2470
2471 struct vc4_compiled_shader *shader;
2472 struct hash_entry *entry = _mesa_hash_table_search(ht, key);
2473 if (entry)
2474 return entry->data;
2475
2476 struct vc4_compile *c = vc4_shader_ntq(vc4, stage, key);
2477 shader = rzalloc(NULL, struct vc4_compiled_shader);
2478
2479 shader->program_id = vc4->next_compiled_program_id++;
2480 if (stage == QSTAGE_FRAG) {
2481 vc4_setup_compiled_fs_inputs(vc4, c, shader);
2482
2483 /* Note: the temporary clone in c->s has been freed. */
2484 nir_shader *orig_shader = key->shader_state->base.ir.nir;
2485 if (orig_shader->info->outputs_written & (1 << FRAG_RESULT_DEPTH))
2486 shader->disable_early_z = true;
2487 } else {
2488 shader->num_inputs = c->num_inputs;
2489
2490 shader->vattr_offsets[0] = 0;
2491 for (int i = 0; i < 8; i++) {
2492 shader->vattr_offsets[i + 1] =
2493 shader->vattr_offsets[i] + c->vattr_sizes[i];
2494
2495 if (c->vattr_sizes[i])
2496 shader->vattrs_live |= (1 << i);
2497 }
2498 }
2499
2500 shader->failed = c->failed;
2501 if (c->failed) {
2502 shader->failed = true;
2503 } else {
2504 copy_uniform_state_to_shader(shader, c);
2505 shader->bo = vc4_bo_alloc_shader(vc4->screen, c->qpu_insts,
2506 c->qpu_inst_count *
2507 sizeof(uint64_t));
2508 }
2509
2510 /* Copy the compiler UBO range state to the compiled shader, dropping
2511 * out arrays that were never referenced by an indirect load.
2512 *
2513 * (Note that QIR dead code elimination of an array access still
2514 * leaves that array alive, though)
2515 */
2516 if (c->num_ubo_ranges) {
2517 shader->num_ubo_ranges = c->num_ubo_ranges;
2518 shader->ubo_ranges = ralloc_array(shader, struct vc4_ubo_range,
2519 c->num_ubo_ranges);
2520 uint32_t j = 0;
2521 for (int i = 0; i < c->num_uniform_ranges; i++) {
2522 struct vc4_compiler_ubo_range *range =
2523 &c->ubo_ranges[i];
2524 if (!range->used)
2525 continue;
2526
2527 shader->ubo_ranges[j].dst_offset = range->dst_offset;
2528 shader->ubo_ranges[j].src_offset = range->src_offset;
2529 shader->ubo_ranges[j].size = range->size;
2530 shader->ubo_size += c->ubo_ranges[i].size;
2531 j++;
2532 }
2533 }
2534 if (shader->ubo_size) {
2535 if (vc4_debug & VC4_DEBUG_SHADERDB) {
2536 fprintf(stderr, "SHADER-DB: %s prog %d/%d: %d UBO uniforms\n",
2537 qir_get_stage_name(c->stage),
2538 c->program_id, c->variant_id,
2539 shader->ubo_size / 4);
2540 }
2541 }
2542
2543 qir_compile_destroy(c);
2544
2545 struct vc4_key *dup_key;
2546 dup_key = rzalloc_size(shader, key_size); /* TODO: don't use rzalloc */
2547 memcpy(dup_key, key, key_size);
2548 _mesa_hash_table_insert(ht, dup_key, shader);
2549
2550 return shader;
2551 }
2552
2553 static void
2554 vc4_setup_shared_key(struct vc4_context *vc4, struct vc4_key *key,
2555 struct vc4_texture_stateobj *texstate)
2556 {
2557 for (int i = 0; i < texstate->num_textures; i++) {
2558 struct pipe_sampler_view *sampler = texstate->textures[i];
2559 struct vc4_sampler_view *vc4_sampler = vc4_sampler_view(sampler);
2560 struct pipe_sampler_state *sampler_state =
2561 texstate->samplers[i];
2562
2563 if (!sampler)
2564 continue;
2565
2566 key->tex[i].format = sampler->format;
2567 key->tex[i].swizzle[0] = sampler->swizzle_r;
2568 key->tex[i].swizzle[1] = sampler->swizzle_g;
2569 key->tex[i].swizzle[2] = sampler->swizzle_b;
2570 key->tex[i].swizzle[3] = sampler->swizzle_a;
2571
2572 if (sampler->texture->nr_samples > 1) {
2573 key->tex[i].msaa_width = sampler->texture->width0;
2574 key->tex[i].msaa_height = sampler->texture->height0;
2575 } else if (sampler){
2576 key->tex[i].compare_mode = sampler_state->compare_mode;
2577 key->tex[i].compare_func = sampler_state->compare_func;
2578 key->tex[i].wrap_s = sampler_state->wrap_s;
2579 key->tex[i].wrap_t = sampler_state->wrap_t;
2580 key->tex[i].force_first_level =
2581 vc4_sampler->force_first_level;
2582 }
2583 }
2584
2585 key->ucp_enables = vc4->rasterizer->base.clip_plane_enable;
2586 }
2587
2588 static void
2589 vc4_update_compiled_fs(struct vc4_context *vc4, uint8_t prim_mode)
2590 {
2591 struct vc4_job *job = vc4->job;
2592 struct vc4_fs_key local_key;
2593 struct vc4_fs_key *key = &local_key;
2594
2595 if (!(vc4->dirty & (VC4_DIRTY_PRIM_MODE |
2596 VC4_DIRTY_BLEND |
2597 VC4_DIRTY_FRAMEBUFFER |
2598 VC4_DIRTY_ZSA |
2599 VC4_DIRTY_RASTERIZER |
2600 VC4_DIRTY_SAMPLE_MASK |
2601 VC4_DIRTY_FRAGTEX |
2602 VC4_DIRTY_UNCOMPILED_FS))) {
2603 return;
2604 }
2605
2606 memset(key, 0, sizeof(*key));
2607 vc4_setup_shared_key(vc4, &key->base, &vc4->fragtex);
2608 key->base.shader_state = vc4->prog.bind_fs;
2609 key->is_points = (prim_mode == PIPE_PRIM_POINTS);
2610 key->is_lines = (prim_mode >= PIPE_PRIM_LINES &&
2611 prim_mode <= PIPE_PRIM_LINE_STRIP);
2612 key->blend = vc4->blend->rt[0];
2613 if (vc4->blend->logicop_enable) {
2614 key->logicop_func = vc4->blend->logicop_func;
2615 } else {
2616 key->logicop_func = PIPE_LOGICOP_COPY;
2617 }
2618 if (job->msaa) {
2619 key->msaa = vc4->rasterizer->base.multisample;
2620 key->sample_coverage = (vc4->rasterizer->base.multisample &&
2621 vc4->sample_mask != (1 << VC4_MAX_SAMPLES) - 1);
2622 key->sample_alpha_to_coverage = vc4->blend->alpha_to_coverage;
2623 key->sample_alpha_to_one = vc4->blend->alpha_to_one;
2624 }
2625
2626 if (vc4->framebuffer.cbufs[0])
2627 key->color_format = vc4->framebuffer.cbufs[0]->format;
2628
2629 key->stencil_enabled = vc4->zsa->stencil_uniforms[0] != 0;
2630 key->stencil_twoside = vc4->zsa->stencil_uniforms[1] != 0;
2631 key->stencil_full_writemasks = vc4->zsa->stencil_uniforms[2] != 0;
2632 key->depth_enabled = (vc4->zsa->base.depth.enabled ||
2633 key->stencil_enabled);
2634 if (vc4->zsa->base.alpha.enabled) {
2635 key->alpha_test = true;
2636 key->alpha_test_func = vc4->zsa->base.alpha.func;
2637 }
2638
2639 if (key->is_points) {
2640 key->point_sprite_mask =
2641 vc4->rasterizer->base.sprite_coord_enable;
2642 key->point_coord_upper_left =
2643 (vc4->rasterizer->base.sprite_coord_mode ==
2644 PIPE_SPRITE_COORD_UPPER_LEFT);
2645 }
2646
2647 key->light_twoside = vc4->rasterizer->base.light_twoside;
2648
2649 struct vc4_compiled_shader *old_fs = vc4->prog.fs;
2650 vc4->prog.fs = vc4_get_compiled_shader(vc4, QSTAGE_FRAG, &key->base);
2651 if (vc4->prog.fs == old_fs)
2652 return;
2653
2654 vc4->dirty |= VC4_DIRTY_COMPILED_FS;
2655
2656 if (vc4->rasterizer->base.flatshade &&
2657 old_fs && vc4->prog.fs->color_inputs != old_fs->color_inputs) {
2658 vc4->dirty |= VC4_DIRTY_FLAT_SHADE_FLAGS;
2659 }
2660
2661 if (old_fs && vc4->prog.fs->fs_inputs != old_fs->fs_inputs)
2662 vc4->dirty |= VC4_DIRTY_FS_INPUTS;
2663 }
2664
2665 static void
2666 vc4_update_compiled_vs(struct vc4_context *vc4, uint8_t prim_mode)
2667 {
2668 struct vc4_vs_key local_key;
2669 struct vc4_vs_key *key = &local_key;
2670
2671 if (!(vc4->dirty & (VC4_DIRTY_PRIM_MODE |
2672 VC4_DIRTY_RASTERIZER |
2673 VC4_DIRTY_VERTTEX |
2674 VC4_DIRTY_VTXSTATE |
2675 VC4_DIRTY_UNCOMPILED_VS |
2676 VC4_DIRTY_FS_INPUTS))) {
2677 return;
2678 }
2679
2680 memset(key, 0, sizeof(*key));
2681 vc4_setup_shared_key(vc4, &key->base, &vc4->verttex);
2682 key->base.shader_state = vc4->prog.bind_vs;
2683 key->fs_inputs = vc4->prog.fs->fs_inputs;
2684 key->clamp_color = vc4->rasterizer->base.clamp_vertex_color;
2685
2686 for (int i = 0; i < ARRAY_SIZE(key->attr_formats); i++)
2687 key->attr_formats[i] = vc4->vtx->pipe[i].src_format;
2688
2689 key->per_vertex_point_size =
2690 (prim_mode == PIPE_PRIM_POINTS &&
2691 vc4->rasterizer->base.point_size_per_vertex);
2692
2693 struct vc4_compiled_shader *vs =
2694 vc4_get_compiled_shader(vc4, QSTAGE_VERT, &key->base);
2695 if (vs != vc4->prog.vs) {
2696 vc4->prog.vs = vs;
2697 vc4->dirty |= VC4_DIRTY_COMPILED_VS;
2698 }
2699
2700 key->is_coord = true;
2701 /* Coord shaders don't care what the FS inputs are. */
2702 key->fs_inputs = NULL;
2703 struct vc4_compiled_shader *cs =
2704 vc4_get_compiled_shader(vc4, QSTAGE_COORD, &key->base);
2705 if (cs != vc4->prog.cs) {
2706 vc4->prog.cs = cs;
2707 vc4->dirty |= VC4_DIRTY_COMPILED_CS;
2708 }
2709 }
2710
2711 bool
2712 vc4_update_compiled_shaders(struct vc4_context *vc4, uint8_t prim_mode)
2713 {
2714 vc4_update_compiled_fs(vc4, prim_mode);
2715 vc4_update_compiled_vs(vc4, prim_mode);
2716
2717 return !(vc4->prog.cs->failed ||
2718 vc4->prog.vs->failed ||
2719 vc4->prog.fs->failed);
2720 }
2721
2722 static uint32_t
2723 fs_cache_hash(const void *key)
2724 {
2725 return _mesa_hash_data(key, sizeof(struct vc4_fs_key));
2726 }
2727
2728 static uint32_t
2729 vs_cache_hash(const void *key)
2730 {
2731 return _mesa_hash_data(key, sizeof(struct vc4_vs_key));
2732 }
2733
2734 static bool
2735 fs_cache_compare(const void *key1, const void *key2)
2736 {
2737 return memcmp(key1, key2, sizeof(struct vc4_fs_key)) == 0;
2738 }
2739
2740 static bool
2741 vs_cache_compare(const void *key1, const void *key2)
2742 {
2743 return memcmp(key1, key2, sizeof(struct vc4_vs_key)) == 0;
2744 }
2745
2746 static uint32_t
2747 fs_inputs_hash(const void *key)
2748 {
2749 const struct vc4_fs_inputs *inputs = key;
2750
2751 return _mesa_hash_data(inputs->input_slots,
2752 sizeof(*inputs->input_slots) *
2753 inputs->num_inputs);
2754 }
2755
2756 static bool
2757 fs_inputs_compare(const void *key1, const void *key2)
2758 {
2759 const struct vc4_fs_inputs *inputs1 = key1;
2760 const struct vc4_fs_inputs *inputs2 = key2;
2761
2762 return (inputs1->num_inputs == inputs2->num_inputs &&
2763 memcmp(inputs1->input_slots,
2764 inputs2->input_slots,
2765 sizeof(*inputs1->input_slots) *
2766 inputs1->num_inputs) == 0);
2767 }
2768
2769 static void
2770 delete_from_cache_if_matches(struct hash_table *ht,
2771 struct hash_entry *entry,
2772 struct vc4_uncompiled_shader *so)
2773 {
2774 const struct vc4_key *key = entry->key;
2775
2776 if (key->shader_state == so) {
2777 struct vc4_compiled_shader *shader = entry->data;
2778 _mesa_hash_table_remove(ht, entry);
2779 vc4_bo_unreference(&shader->bo);
2780 ralloc_free(shader);
2781 }
2782 }
2783
2784 static void
2785 vc4_shader_state_delete(struct pipe_context *pctx, void *hwcso)
2786 {
2787 struct vc4_context *vc4 = vc4_context(pctx);
2788 struct vc4_uncompiled_shader *so = hwcso;
2789
2790 struct hash_entry *entry;
2791 hash_table_foreach(vc4->fs_cache, entry)
2792 delete_from_cache_if_matches(vc4->fs_cache, entry, so);
2793 hash_table_foreach(vc4->vs_cache, entry)
2794 delete_from_cache_if_matches(vc4->vs_cache, entry, so);
2795
2796 ralloc_free(so->base.ir.nir);
2797 free(so);
2798 }
2799
2800 static void
2801 vc4_fp_state_bind(struct pipe_context *pctx, void *hwcso)
2802 {
2803 struct vc4_context *vc4 = vc4_context(pctx);
2804 vc4->prog.bind_fs = hwcso;
2805 vc4->dirty |= VC4_DIRTY_UNCOMPILED_FS;
2806 }
2807
2808 static void
2809 vc4_vp_state_bind(struct pipe_context *pctx, void *hwcso)
2810 {
2811 struct vc4_context *vc4 = vc4_context(pctx);
2812 vc4->prog.bind_vs = hwcso;
2813 vc4->dirty |= VC4_DIRTY_UNCOMPILED_VS;
2814 }
2815
2816 void
2817 vc4_program_init(struct pipe_context *pctx)
2818 {
2819 struct vc4_context *vc4 = vc4_context(pctx);
2820
2821 pctx->create_vs_state = vc4_shader_state_create;
2822 pctx->delete_vs_state = vc4_shader_state_delete;
2823
2824 pctx->create_fs_state = vc4_shader_state_create;
2825 pctx->delete_fs_state = vc4_shader_state_delete;
2826
2827 pctx->bind_fs_state = vc4_fp_state_bind;
2828 pctx->bind_vs_state = vc4_vp_state_bind;
2829
2830 vc4->fs_cache = _mesa_hash_table_create(pctx, fs_cache_hash,
2831 fs_cache_compare);
2832 vc4->vs_cache = _mesa_hash_table_create(pctx, vs_cache_hash,
2833 vs_cache_compare);
2834 vc4->fs_inputs_set = _mesa_set_create(pctx, fs_inputs_hash,
2835 fs_inputs_compare);
2836 }
2837
2838 void
2839 vc4_program_fini(struct pipe_context *pctx)
2840 {
2841 struct vc4_context *vc4 = vc4_context(pctx);
2842
2843 struct hash_entry *entry;
2844 hash_table_foreach(vc4->fs_cache, entry) {
2845 struct vc4_compiled_shader *shader = entry->data;
2846 vc4_bo_unreference(&shader->bo);
2847 ralloc_free(shader);
2848 _mesa_hash_table_remove(vc4->fs_cache, entry);
2849 }
2850
2851 hash_table_foreach(vc4->vs_cache, entry) {
2852 struct vc4_compiled_shader *shader = entry->data;
2853 vc4_bo_unreference(&shader->bo);
2854 ralloc_free(shader);
2855 _mesa_hash_table_remove(vc4->vs_cache, entry);
2856 }
2857 }