2 * Copyright (c) 2014 Scott Mansell
3 * Copyright © 2014 Broadcom
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:
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
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
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"
40 #ifdef USE_VC4_SIMULATOR
41 #include "simpenrose/simpenrose.h"
45 ntq_get_src(struct vc4_compile
*c
, nir_src src
, int i
);
47 ntq_emit_cf_list(struct vc4_compile
*c
, struct exec_list
*list
);
50 resize_qreg_array(struct vc4_compile
*c
,
55 if (*size
>= decl_size
)
58 uint32_t old_size
= *size
;
59 *size
= MAX2(*size
* 2, decl_size
);
60 *regs
= reralloc(c
, *regs
, struct qreg
, *size
);
62 fprintf(stderr
, "Malloc failure\n");
66 for (uint32_t i
= old_size
; i
< *size
; i
++)
67 (*regs
)[i
] = c
->undef
;
71 indirect_uniform_load(struct vc4_compile
*c
, nir_intrinsic_instr
*intr
)
73 struct qreg indirect_offset
= ntq_get_src(c
, intr
->src
[0], 0);
74 uint32_t offset
= intr
->const_index
[0];
75 struct vc4_compiler_ubo_range
*range
= NULL
;
77 for (i
= 0; i
< c
->num_uniform_ranges
; i
++) {
78 range
= &c
->ubo_ranges
[i
];
79 if (offset
>= range
->src_offset
&&
80 offset
< range
->src_offset
+ range
->size
) {
84 /* The driver-location-based offset always has to be within a declared
90 range
->dst_offset
= c
->next_ubo_dst_offset
;
91 c
->next_ubo_dst_offset
+= range
->size
;
95 offset
-= range
->src_offset
;
97 /* Adjust for where we stored the TGSI register base. */
98 indirect_offset
= qir_ADD(c
, indirect_offset
,
99 qir_uniform_ui(c
, (range
->dst_offset
+
102 /* Clamp to [0, array size). Note that MIN/MAX are signed. */
103 indirect_offset
= qir_MAX(c
, indirect_offset
, qir_uniform_ui(c
, 0));
104 indirect_offset
= qir_MIN(c
, indirect_offset
,
105 qir_uniform_ui(c
, (range
->dst_offset
+
108 qir_TEX_DIRECT(c
, indirect_offset
, qir_uniform(c
, QUNIFORM_UBO_ADDR
, 0));
109 c
->num_texture_samples
++;
110 return qir_TEX_RESULT(c
);
113 nir_ssa_def
*vc4_nir_get_state_uniform(struct nir_builder
*b
,
114 enum quniform_contents contents
)
116 nir_intrinsic_instr
*intr
=
117 nir_intrinsic_instr_create(b
->shader
,
118 nir_intrinsic_load_uniform
);
119 intr
->const_index
[0] = (VC4_NIR_STATE_UNIFORM_OFFSET
+ contents
) * 4;
120 intr
->num_components
= 1;
121 intr
->src
[0] = nir_src_for_ssa(nir_imm_int(b
, 0));
122 nir_ssa_dest_init(&intr
->instr
, &intr
->dest
, 1, 32, NULL
);
123 nir_builder_instr_insert(b
, &intr
->instr
);
124 return &intr
->dest
.ssa
;
128 vc4_nir_get_swizzled_channel(nir_builder
*b
, nir_ssa_def
**srcs
, int swiz
)
132 case PIPE_SWIZZLE_NONE
:
133 fprintf(stderr
, "warning: unknown swizzle\n");
136 return nir_imm_float(b
, 0.0);
138 return nir_imm_float(b
, 1.0);
148 ntq_init_ssa_def(struct vc4_compile
*c
, nir_ssa_def
*def
)
150 struct qreg
*qregs
= ralloc_array(c
->def_ht
, struct qreg
,
151 def
->num_components
);
152 _mesa_hash_table_insert(c
->def_ht
, def
, qregs
);
157 ntq_store_dest(struct vc4_compile
*c
, nir_dest
*dest
, int chan
,
161 assert(chan
< dest
->ssa
.num_components
);
164 struct hash_entry
*entry
=
165 _mesa_hash_table_search(c
->def_ht
, &dest
->ssa
);
170 qregs
= ntq_init_ssa_def(c
, &dest
->ssa
);
172 qregs
[chan
] = result
;
174 nir_register
*reg
= dest
->reg
.reg
;
175 assert(dest
->reg
.base_offset
== 0);
176 assert(reg
->num_array_elems
== 0);
177 struct hash_entry
*entry
=
178 _mesa_hash_table_search(c
->def_ht
, reg
);
179 struct qreg
*qregs
= entry
->data
;
181 /* Conditionally move the result to the destination if the
184 if (c
->execute
.file
!= QFILE_NULL
) {
185 qir_SF(c
, c
->execute
);
186 qir_MOV_cond(c
, QPU_COND_ZS
, qregs
[chan
], result
);
188 qir_MOV_dest(c
, qregs
[chan
], result
);
194 ntq_get_dest(struct vc4_compile
*c
, nir_dest
*dest
)
197 struct qreg
*qregs
= ntq_init_ssa_def(c
, &dest
->ssa
);
198 for (int i
= 0; i
< dest
->ssa
.num_components
; i
++)
202 nir_register
*reg
= dest
->reg
.reg
;
203 assert(dest
->reg
.base_offset
== 0);
204 assert(reg
->num_array_elems
== 0);
205 struct hash_entry
*entry
=
206 _mesa_hash_table_search(c
->def_ht
, reg
);
212 ntq_get_src(struct vc4_compile
*c
, nir_src src
, int i
)
214 struct hash_entry
*entry
;
216 entry
= _mesa_hash_table_search(c
->def_ht
, src
.ssa
);
217 assert(i
< src
.ssa
->num_components
);
219 nir_register
*reg
= src
.reg
.reg
;
220 entry
= _mesa_hash_table_search(c
->def_ht
, reg
);
221 assert(reg
->num_array_elems
== 0);
222 assert(src
.reg
.base_offset
== 0);
223 assert(i
< reg
->num_components
);
226 struct qreg
*qregs
= entry
->data
;
231 ntq_get_alu_src(struct vc4_compile
*c
, nir_alu_instr
*instr
,
234 assert(util_is_power_of_two(instr
->dest
.write_mask
));
235 unsigned chan
= ffs(instr
->dest
.write_mask
) - 1;
236 struct qreg r
= ntq_get_src(c
, instr
->src
[src
].src
,
237 instr
->src
[src
].swizzle
[chan
]);
239 assert(!instr
->src
[src
].abs
);
240 assert(!instr
->src
[src
].negate
);
245 static inline struct qreg
246 qir_SAT(struct vc4_compile
*c
, struct qreg val
)
249 qir_FMIN(c
, val
, qir_uniform_f(c
, 1.0)),
250 qir_uniform_f(c
, 0.0));
254 ntq_rcp(struct vc4_compile
*c
, struct qreg x
)
256 struct qreg r
= qir_RCP(c
, x
);
258 /* Apply a Newton-Raphson step to improve the accuracy. */
259 r
= qir_FMUL(c
, r
, qir_FSUB(c
,
260 qir_uniform_f(c
, 2.0),
267 ntq_rsq(struct vc4_compile
*c
, struct qreg x
)
269 struct qreg r
= qir_RSQ(c
, x
);
271 /* Apply a Newton-Raphson step to improve the accuracy. */
272 r
= qir_FMUL(c
, r
, qir_FSUB(c
,
273 qir_uniform_f(c
, 1.5),
275 qir_uniform_f(c
, 0.5),
277 qir_FMUL(c
, r
, r
)))));
283 ntq_umul(struct vc4_compile
*c
, struct qreg src0
, struct qreg src1
)
285 struct qreg src0_hi
= qir_SHR(c
, src0
,
286 qir_uniform_ui(c
, 24));
287 struct qreg src1_hi
= qir_SHR(c
, src1
,
288 qir_uniform_ui(c
, 24));
290 struct qreg hilo
= qir_MUL24(c
, src0_hi
, src1
);
291 struct qreg lohi
= qir_MUL24(c
, src0
, src1_hi
);
292 struct qreg lolo
= qir_MUL24(c
, src0
, src1
);
294 return qir_ADD(c
, lolo
, qir_SHL(c
,
295 qir_ADD(c
, hilo
, lohi
),
296 qir_uniform_ui(c
, 24)));
300 ntq_scale_depth_texture(struct vc4_compile
*c
, struct qreg src
)
302 struct qreg depthf
= qir_ITOF(c
, qir_SHR(c
, src
,
303 qir_uniform_ui(c
, 8)));
304 return qir_FMUL(c
, depthf
, qir_uniform_f(c
, 1.0f
/0xffffff));
308 * Emits a lowered TXF_MS from an MSAA texture.
310 * The addressing math has been lowered in NIR, and now we just need to read
314 ntq_emit_txf(struct vc4_compile
*c
, nir_tex_instr
*instr
)
316 uint32_t tile_width
= 32;
317 uint32_t tile_height
= 32;
318 uint32_t tile_size
= (tile_height
* tile_width
*
319 VC4_MAX_SAMPLES
* sizeof(uint32_t));
321 unsigned unit
= instr
->texture_index
;
322 uint32_t w
= align(c
->key
->tex
[unit
].msaa_width
, tile_width
);
323 uint32_t w_tiles
= w
/ tile_width
;
324 uint32_t h
= align(c
->key
->tex
[unit
].msaa_height
, tile_height
);
325 uint32_t h_tiles
= h
/ tile_height
;
326 uint32_t size
= w_tiles
* h_tiles
* tile_size
;
329 assert(instr
->num_srcs
== 1);
330 assert(instr
->src
[0].src_type
== nir_tex_src_coord
);
331 addr
= ntq_get_src(c
, instr
->src
[0].src
, 0);
333 /* Perform the clamping required by kernel validation. */
334 addr
= qir_MAX(c
, addr
, qir_uniform_ui(c
, 0));
335 addr
= qir_MIN(c
, addr
, qir_uniform_ui(c
, size
- 4));
337 qir_TEX_DIRECT(c
, addr
, qir_uniform(c
, QUNIFORM_TEXTURE_MSAA_ADDR
, unit
));
339 struct qreg tex
= qir_TEX_RESULT(c
);
340 c
->num_texture_samples
++;
343 enum pipe_format format
= c
->key
->tex
[unit
].format
;
344 if (util_format_is_depth_or_stencil(format
)) {
345 struct qreg scaled
= ntq_scale_depth_texture(c
, tex
);
346 for (int i
= 0; i
< 4; i
++)
349 for (int i
= 0; i
< 4; i
++)
350 dest
[i
] = qir_UNPACK_8_F(c
, tex
, i
);
353 for (int i
= 0; i
< 4; i
++)
354 ntq_store_dest(c
, &instr
->dest
, i
, dest
[i
]);
358 ntq_emit_tex(struct vc4_compile
*c
, nir_tex_instr
*instr
)
360 struct qreg s
, t
, r
, lod
, compare
;
361 bool is_txb
= false, is_txl
= false;
362 unsigned unit
= instr
->texture_index
;
364 if (instr
->op
== nir_texop_txf
) {
365 ntq_emit_txf(c
, instr
);
369 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
370 switch (instr
->src
[i
].src_type
) {
371 case nir_tex_src_coord
:
372 s
= ntq_get_src(c
, instr
->src
[i
].src
, 0);
373 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
)
374 t
= qir_uniform_f(c
, 0.5);
376 t
= ntq_get_src(c
, instr
->src
[i
].src
, 1);
377 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
)
378 r
= ntq_get_src(c
, instr
->src
[i
].src
, 2);
380 case nir_tex_src_bias
:
381 lod
= ntq_get_src(c
, instr
->src
[i
].src
, 0);
384 case nir_tex_src_lod
:
385 lod
= ntq_get_src(c
, instr
->src
[i
].src
, 0);
388 case nir_tex_src_comparitor
:
389 compare
= ntq_get_src(c
, instr
->src
[i
].src
, 0);
392 unreachable("unknown texture source");
396 struct qreg texture_u
[] = {
397 qir_uniform(c
, QUNIFORM_TEXTURE_CONFIG_P0
, unit
),
398 qir_uniform(c
, QUNIFORM_TEXTURE_CONFIG_P1
, unit
),
399 qir_uniform(c
, QUNIFORM_CONSTANT
, 0),
400 qir_uniform(c
, QUNIFORM_CONSTANT
, 0),
402 uint32_t next_texture_u
= 0;
404 /* There is no native support for GL texture rectangle coordinates, so
405 * we have to rescale from ([0, width], [0, height]) to ([0, 1], [0,
408 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_RECT
) {
410 qir_uniform(c
, QUNIFORM_TEXRECT_SCALE_X
, unit
));
412 qir_uniform(c
, QUNIFORM_TEXRECT_SCALE_Y
, unit
));
415 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
|| is_txl
) {
416 texture_u
[2] = qir_uniform(c
, QUNIFORM_TEXTURE_CONFIG_P2
,
417 unit
| (is_txl
<< 16));
420 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
421 qir_TEX_R(c
, r
, texture_u
[next_texture_u
++]);
422 } else if (c
->key
->tex
[unit
].wrap_s
== PIPE_TEX_WRAP_CLAMP_TO_BORDER
||
423 c
->key
->tex
[unit
].wrap_s
== PIPE_TEX_WRAP_CLAMP
||
424 c
->key
->tex
[unit
].wrap_t
== PIPE_TEX_WRAP_CLAMP_TO_BORDER
||
425 c
->key
->tex
[unit
].wrap_t
== PIPE_TEX_WRAP_CLAMP
) {
426 qir_TEX_R(c
, qir_uniform(c
, QUNIFORM_TEXTURE_BORDER_COLOR
, unit
),
427 texture_u
[next_texture_u
++]);
430 if (c
->key
->tex
[unit
].wrap_s
== PIPE_TEX_WRAP_CLAMP
) {
434 if (c
->key
->tex
[unit
].wrap_t
== PIPE_TEX_WRAP_CLAMP
) {
438 qir_TEX_T(c
, t
, texture_u
[next_texture_u
++]);
440 if (is_txl
|| is_txb
)
441 qir_TEX_B(c
, lod
, texture_u
[next_texture_u
++]);
443 qir_TEX_S(c
, s
, texture_u
[next_texture_u
++]);
445 c
->num_texture_samples
++;
446 struct qreg tex
= qir_TEX_RESULT(c
);
448 enum pipe_format format
= c
->key
->tex
[unit
].format
;
450 struct qreg
*dest
= ntq_get_dest(c
, &instr
->dest
);
451 if (util_format_is_depth_or_stencil(format
)) {
452 struct qreg normalized
= ntq_scale_depth_texture(c
, tex
);
453 struct qreg depth_output
;
455 struct qreg u0
= qir_uniform_f(c
, 0.0f
);
456 struct qreg u1
= qir_uniform_f(c
, 1.0f
);
457 if (c
->key
->tex
[unit
].compare_mode
) {
458 switch (c
->key
->tex
[unit
].compare_func
) {
459 case PIPE_FUNC_NEVER
:
460 depth_output
= qir_uniform_f(c
, 0.0f
);
462 case PIPE_FUNC_ALWAYS
:
465 case PIPE_FUNC_EQUAL
:
466 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
467 depth_output
= qir_SEL(c
, QPU_COND_ZS
, u1
, u0
);
469 case PIPE_FUNC_NOTEQUAL
:
470 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
471 depth_output
= qir_SEL(c
, QPU_COND_ZC
, u1
, u0
);
473 case PIPE_FUNC_GREATER
:
474 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
475 depth_output
= qir_SEL(c
, QPU_COND_NC
, u1
, u0
);
477 case PIPE_FUNC_GEQUAL
:
478 qir_SF(c
, qir_FSUB(c
, normalized
, compare
));
479 depth_output
= qir_SEL(c
, QPU_COND_NS
, u1
, u0
);
482 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
483 depth_output
= qir_SEL(c
, QPU_COND_NS
, u1
, u0
);
485 case PIPE_FUNC_LEQUAL
:
486 qir_SF(c
, qir_FSUB(c
, normalized
, compare
));
487 depth_output
= qir_SEL(c
, QPU_COND_NC
, u1
, u0
);
491 depth_output
= normalized
;
494 for (int i
= 0; i
< 4; i
++)
495 dest
[i
] = depth_output
;
497 for (int i
= 0; i
< 4; i
++)
498 dest
[i
] = qir_UNPACK_8_F(c
, tex
, i
);
503 * Computes x - floor(x), which is tricky because our FTOI truncates (rounds
507 ntq_ffract(struct vc4_compile
*c
, struct qreg src
)
509 struct qreg trunc
= qir_ITOF(c
, qir_FTOI(c
, src
));
510 struct qreg diff
= qir_FSUB(c
, src
, trunc
);
512 return qir_SEL(c
, QPU_COND_NS
,
513 qir_FADD(c
, diff
, qir_uniform_f(c
, 1.0)), diff
);
517 * Computes floor(x), which is tricky because our FTOI truncates (rounds to
521 ntq_ffloor(struct vc4_compile
*c
, struct qreg src
)
523 struct qreg trunc
= qir_ITOF(c
, qir_FTOI(c
, src
));
525 /* This will be < 0 if we truncated and the truncation was of a value
526 * that was < 0 in the first place.
528 qir_SF(c
, qir_FSUB(c
, src
, trunc
));
530 return qir_SEL(c
, QPU_COND_NS
,
531 qir_FSUB(c
, trunc
, qir_uniform_f(c
, 1.0)), trunc
);
535 * Computes ceil(x), which is tricky because our FTOI truncates (rounds to
539 ntq_fceil(struct vc4_compile
*c
, struct qreg src
)
541 struct qreg trunc
= qir_ITOF(c
, qir_FTOI(c
, src
));
543 /* This will be < 0 if we truncated and the truncation was of a value
544 * that was > 0 in the first place.
546 qir_SF(c
, qir_FSUB(c
, trunc
, src
));
548 return qir_SEL(c
, QPU_COND_NS
,
549 qir_FADD(c
, trunc
, qir_uniform_f(c
, 1.0)), trunc
);
553 ntq_fsin(struct vc4_compile
*c
, struct qreg src
)
557 pow(2.0 * M_PI
, 3) / (3 * 2 * 1),
558 -pow(2.0 * M_PI
, 5) / (5 * 4 * 3 * 2 * 1),
559 pow(2.0 * M_PI
, 7) / (7 * 6 * 5 * 4 * 3 * 2 * 1),
560 -pow(2.0 * M_PI
, 9) / (9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1),
563 struct qreg scaled_x
=
566 qir_uniform_f(c
, 1.0 / (M_PI
* 2.0)));
568 struct qreg x
= qir_FADD(c
,
569 ntq_ffract(c
, scaled_x
),
570 qir_uniform_f(c
, -0.5));
571 struct qreg x2
= qir_FMUL(c
, x
, x
);
572 struct qreg sum
= qir_FMUL(c
, x
, qir_uniform_f(c
, coeff
[0]));
573 for (int i
= 1; i
< ARRAY_SIZE(coeff
); i
++) {
574 x
= qir_FMUL(c
, x
, x2
);
579 qir_uniform_f(c
, coeff
[i
])));
585 ntq_fcos(struct vc4_compile
*c
, struct qreg src
)
589 pow(2.0 * M_PI
, 2) / (2 * 1),
590 -pow(2.0 * M_PI
, 4) / (4 * 3 * 2 * 1),
591 pow(2.0 * M_PI
, 6) / (6 * 5 * 4 * 3 * 2 * 1),
592 -pow(2.0 * M_PI
, 8) / (8 * 7 * 6 * 5 * 4 * 3 * 2 * 1),
593 pow(2.0 * M_PI
, 10) / (10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1),
596 struct qreg scaled_x
=
598 qir_uniform_f(c
, 1.0f
/ (M_PI
* 2.0f
)));
599 struct qreg x_frac
= qir_FADD(c
,
600 ntq_ffract(c
, scaled_x
),
601 qir_uniform_f(c
, -0.5));
603 struct qreg sum
= qir_uniform_f(c
, coeff
[0]);
604 struct qreg x2
= qir_FMUL(c
, x_frac
, x_frac
);
605 struct qreg x
= x2
; /* Current x^2, x^4, or x^6 */
606 for (int i
= 1; i
< ARRAY_SIZE(coeff
); i
++) {
608 x
= qir_FMUL(c
, x
, x2
);
610 struct qreg mul
= qir_FMUL(c
,
612 qir_uniform_f(c
, coeff
[i
]));
616 sum
= qir_FADD(c
, sum
, mul
);
622 ntq_fsign(struct vc4_compile
*c
, struct qreg src
)
624 struct qreg t
= qir_get_temp(c
);
627 qir_MOV_dest(c
, t
, qir_uniform_f(c
, 0.0));
628 qir_MOV_dest(c
, t
, qir_uniform_f(c
, 1.0))->cond
= QPU_COND_ZC
;
629 qir_MOV_dest(c
, t
, qir_uniform_f(c
, -1.0))->cond
= QPU_COND_NS
;
634 emit_vertex_input(struct vc4_compile
*c
, int attr
)
636 enum pipe_format format
= c
->vs_key
->attr_formats
[attr
];
637 uint32_t attr_size
= util_format_get_blocksize(format
);
639 c
->vattr_sizes
[attr
] = align(attr_size
, 4);
640 for (int i
= 0; i
< align(attr_size
, 4) / 4; i
++) {
641 c
->inputs
[attr
* 4 + i
] =
642 qir_MOV(c
, qir_reg(QFILE_VPM
, attr
* 4 + i
));
648 emit_fragcoord_input(struct vc4_compile
*c
, int attr
)
650 c
->inputs
[attr
* 4 + 0] = qir_ITOF(c
, qir_reg(QFILE_FRAG_X
, 0));
651 c
->inputs
[attr
* 4 + 1] = qir_ITOF(c
, qir_reg(QFILE_FRAG_Y
, 0));
652 c
->inputs
[attr
* 4 + 2] =
654 qir_ITOF(c
, qir_FRAG_Z(c
)),
655 qir_uniform_f(c
, 1.0 / 0xffffff));
656 c
->inputs
[attr
* 4 + 3] = qir_RCP(c
, qir_FRAG_W(c
));
660 emit_fragment_varying(struct vc4_compile
*c
, gl_varying_slot slot
,
663 uint32_t i
= c
->num_input_slots
++;
669 if (c
->num_input_slots
>= c
->input_slots_array_size
) {
670 c
->input_slots_array_size
=
671 MAX2(4, c
->input_slots_array_size
* 2);
673 c
->input_slots
= reralloc(c
, c
->input_slots
,
674 struct vc4_varying_slot
,
675 c
->input_slots_array_size
);
678 c
->input_slots
[i
].slot
= slot
;
679 c
->input_slots
[i
].swizzle
= swizzle
;
681 return qir_VARY_ADD_C(c
, qir_FMUL(c
, vary
, qir_FRAG_W(c
)));
685 emit_fragment_input(struct vc4_compile
*c
, int attr
, gl_varying_slot slot
)
687 for (int i
= 0; i
< 4; i
++) {
688 c
->inputs
[attr
* 4 + i
] =
689 emit_fragment_varying(c
, slot
, i
);
695 add_output(struct vc4_compile
*c
,
696 uint32_t decl_offset
,
700 uint32_t old_array_size
= c
->outputs_array_size
;
701 resize_qreg_array(c
, &c
->outputs
, &c
->outputs_array_size
,
704 if (old_array_size
!= c
->outputs_array_size
) {
705 c
->output_slots
= reralloc(c
,
707 struct vc4_varying_slot
,
708 c
->outputs_array_size
);
711 c
->output_slots
[decl_offset
].slot
= slot
;
712 c
->output_slots
[decl_offset
].swizzle
= swizzle
;
716 declare_uniform_range(struct vc4_compile
*c
, uint32_t start
, uint32_t size
)
718 unsigned array_id
= c
->num_uniform_ranges
++;
719 if (array_id
>= c
->ubo_ranges_array_size
) {
720 c
->ubo_ranges_array_size
= MAX2(c
->ubo_ranges_array_size
* 2,
722 c
->ubo_ranges
= reralloc(c
, c
->ubo_ranges
,
723 struct vc4_compiler_ubo_range
,
724 c
->ubo_ranges_array_size
);
727 c
->ubo_ranges
[array_id
].dst_offset
= 0;
728 c
->ubo_ranges
[array_id
].src_offset
= start
;
729 c
->ubo_ranges
[array_id
].size
= size
;
730 c
->ubo_ranges
[array_id
].used
= false;
734 ntq_src_is_only_ssa_def_user(nir_src
*src
)
739 if (!list_empty(&src
->ssa
->if_uses
))
742 return (src
->ssa
->uses
.next
== &src
->use_link
&&
743 src
->ssa
->uses
.next
->next
== &src
->ssa
->uses
);
747 * In general, emits a nir_pack_unorm_4x8 as a series of MOVs with the pack
750 * However, as an optimization, it tries to find the instructions generating
751 * the sources to be packed and just emit the pack flag there, if possible.
754 ntq_emit_pack_unorm_4x8(struct vc4_compile
*c
, nir_alu_instr
*instr
)
756 struct qreg result
= qir_get_temp(c
);
757 struct nir_alu_instr
*vec4
= NULL
;
759 /* If packing from a vec4 op (as expected), identify it so that we can
760 * peek back at what generated its sources.
762 if (instr
->src
[0].src
.is_ssa
&&
763 instr
->src
[0].src
.ssa
->parent_instr
->type
== nir_instr_type_alu
&&
764 nir_instr_as_alu(instr
->src
[0].src
.ssa
->parent_instr
)->op
==
766 vec4
= nir_instr_as_alu(instr
->src
[0].src
.ssa
->parent_instr
);
769 /* If the pack is replicating the same channel 4 times, use the 8888
770 * pack flag. This is common for blending using the alpha
773 if (instr
->src
[0].swizzle
[0] == instr
->src
[0].swizzle
[1] &&
774 instr
->src
[0].swizzle
[0] == instr
->src
[0].swizzle
[2] &&
775 instr
->src
[0].swizzle
[0] == instr
->src
[0].swizzle
[3]) {
776 struct qreg rep
= ntq_get_src(c
,
778 instr
->src
[0].swizzle
[0]);
779 ntq_store_dest(c
, &instr
->dest
.dest
, 0, qir_PACK_8888_F(c
, rep
));
783 for (int i
= 0; i
< 4; i
++) {
784 int swiz
= instr
->src
[0].swizzle
[i
];
787 src
= ntq_get_src(c
, vec4
->src
[swiz
].src
,
788 vec4
->src
[swiz
].swizzle
[0]);
790 src
= ntq_get_src(c
, instr
->src
[0].src
, swiz
);
794 ntq_src_is_only_ssa_def_user(&vec4
->src
[swiz
].src
) &&
795 src
.file
== QFILE_TEMP
&&
796 c
->defs
[src
.index
] &&
797 qir_is_mul(c
->defs
[src
.index
]) &&
798 !c
->defs
[src
.index
]->dst
.pack
) {
799 struct qinst
*rewrite
= c
->defs
[src
.index
];
800 c
->defs
[src
.index
] = NULL
;
801 rewrite
->dst
= result
;
802 rewrite
->dst
.pack
= QPU_PACK_MUL_8A
+ i
;
806 qir_PACK_8_F(c
, result
, src
, i
);
809 ntq_store_dest(c
, &instr
->dest
.dest
, 0, result
);
812 /** Handles sign-extended bitfield extracts for 16 bits. */
814 ntq_emit_ibfe(struct vc4_compile
*c
, struct qreg base
, struct qreg offset
,
817 assert(bits
.file
== QFILE_UNIF
&&
818 c
->uniform_contents
[bits
.index
] == QUNIFORM_CONSTANT
&&
819 c
->uniform_data
[bits
.index
] == 16);
821 assert(offset
.file
== QFILE_UNIF
&&
822 c
->uniform_contents
[offset
.index
] == QUNIFORM_CONSTANT
);
823 int offset_bit
= c
->uniform_data
[offset
.index
];
824 assert(offset_bit
% 16 == 0);
826 return qir_UNPACK_16_I(c
, base
, offset_bit
/ 16);
829 /** Handles unsigned bitfield extracts for 8 bits. */
831 ntq_emit_ubfe(struct vc4_compile
*c
, struct qreg base
, struct qreg offset
,
834 assert(bits
.file
== QFILE_UNIF
&&
835 c
->uniform_contents
[bits
.index
] == QUNIFORM_CONSTANT
&&
836 c
->uniform_data
[bits
.index
] == 8);
838 assert(offset
.file
== QFILE_UNIF
&&
839 c
->uniform_contents
[offset
.index
] == QUNIFORM_CONSTANT
);
840 int offset_bit
= c
->uniform_data
[offset
.index
];
841 assert(offset_bit
% 8 == 0);
843 return qir_UNPACK_8_I(c
, base
, offset_bit
/ 8);
847 * If compare_instr is a valid comparison instruction, emits the
848 * compare_instr's comparison and returns the sel_instr's return value based
849 * on the compare_instr's result.
852 ntq_emit_comparison(struct vc4_compile
*c
, struct qreg
*dest
,
853 nir_alu_instr
*compare_instr
,
854 nir_alu_instr
*sel_instr
)
858 switch (compare_instr
->op
) {
884 struct qreg src0
= ntq_get_alu_src(c
, compare_instr
, 0);
885 struct qreg src1
= ntq_get_alu_src(c
, compare_instr
, 1);
887 unsigned unsized_type
=
888 nir_alu_type_get_base_type(nir_op_infos
[compare_instr
->op
].input_types
[0]);
889 if (unsized_type
== nir_type_float
)
890 qir_SF(c
, qir_FSUB(c
, src0
, src1
));
892 qir_SF(c
, qir_SUB(c
, src0
, src1
));
894 switch (sel_instr
->op
) {
899 *dest
= qir_SEL(c
, cond
,
900 qir_uniform_f(c
, 1.0), qir_uniform_f(c
, 0.0));
904 *dest
= qir_SEL(c
, cond
,
905 ntq_get_alu_src(c
, sel_instr
, 1),
906 ntq_get_alu_src(c
, sel_instr
, 2));
910 *dest
= qir_SEL(c
, cond
,
911 qir_uniform_ui(c
, ~0), qir_uniform_ui(c
, 0));
919 * Attempts to fold a comparison generating a boolean result into the
920 * condition code for selecting between two values, instead of comparing the
921 * boolean result against 0 to generate the condition code.
923 static struct qreg
ntq_emit_bcsel(struct vc4_compile
*c
, nir_alu_instr
*instr
,
926 if (!instr
->src
[0].src
.is_ssa
)
928 nir_alu_instr
*compare
=
929 nir_instr_as_alu(instr
->src
[0].src
.ssa
->parent_instr
);
934 if (ntq_emit_comparison(c
, &dest
, compare
, instr
))
939 return qir_SEL(c
, QPU_COND_NS
, src
[1], src
[2]);
943 ntq_emit_alu(struct vc4_compile
*c
, nir_alu_instr
*instr
)
945 /* This should always be lowered to ALU operations for VC4. */
946 assert(!instr
->dest
.saturate
);
948 /* Vectors are special in that they have non-scalarized writemasks,
949 * and just take the first swizzle channel for each argument in order
950 * into each writemask channel.
952 if (instr
->op
== nir_op_vec2
||
953 instr
->op
== nir_op_vec3
||
954 instr
->op
== nir_op_vec4
) {
956 for (int i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
957 srcs
[i
] = ntq_get_src(c
, instr
->src
[i
].src
,
958 instr
->src
[i
].swizzle
[0]);
959 for (int i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
960 ntq_store_dest(c
, &instr
->dest
.dest
, i
, srcs
[i
]);
964 if (instr
->op
== nir_op_pack_unorm_4x8
) {
965 ntq_emit_pack_unorm_4x8(c
, instr
);
969 if (instr
->op
== nir_op_unpack_unorm_4x8
) {
970 struct qreg src
= ntq_get_src(c
, instr
->src
[0].src
,
971 instr
->src
[0].swizzle
[0]);
972 for (int i
= 0; i
< 4; i
++) {
973 if (instr
->dest
.write_mask
& (1 << i
))
974 ntq_store_dest(c
, &instr
->dest
.dest
, i
,
975 qir_UNPACK_8_F(c
, src
, i
));
980 /* General case: We can just grab the one used channel per src. */
981 struct qreg src
[nir_op_infos
[instr
->op
].num_inputs
];
982 for (int i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
983 src
[i
] = ntq_get_alu_src(c
, instr
, i
);
991 result
= qir_MOV(c
, src
[0]);
994 result
= qir_FMUL(c
, src
[0], src
[1]);
997 result
= qir_FADD(c
, src
[0], src
[1]);
1000 result
= qir_FSUB(c
, src
[0], src
[1]);
1003 result
= qir_FMIN(c
, src
[0], src
[1]);
1006 result
= qir_FMAX(c
, src
[0], src
[1]);
1011 result
= qir_FTOI(c
, src
[0]);
1015 result
= qir_ITOF(c
, src
[0]);
1018 result
= qir_AND(c
, src
[0], qir_uniform_f(c
, 1.0));
1021 result
= qir_AND(c
, src
[0], qir_uniform_ui(c
, 1));
1026 result
= qir_SEL(c
, QPU_COND_ZC
,
1027 qir_uniform_ui(c
, ~0),
1028 qir_uniform_ui(c
, 0));
1032 result
= qir_ADD(c
, src
[0], src
[1]);
1035 result
= qir_SHR(c
, src
[0], src
[1]);
1038 result
= qir_SUB(c
, src
[0], src
[1]);
1041 result
= qir_ASR(c
, src
[0], src
[1]);
1044 result
= qir_SHL(c
, src
[0], src
[1]);
1047 result
= qir_MIN(c
, src
[0], src
[1]);
1050 result
= qir_MAX(c
, src
[0], src
[1]);
1053 result
= qir_AND(c
, src
[0], src
[1]);
1056 result
= qir_OR(c
, src
[0], src
[1]);
1059 result
= qir_XOR(c
, src
[0], src
[1]);
1062 result
= qir_NOT(c
, src
[0]);
1066 result
= ntq_umul(c
, src
[0], src
[1]);
1082 if (!ntq_emit_comparison(c
, &result
, instr
, instr
)) {
1083 fprintf(stderr
, "Bad comparison instruction\n");
1088 result
= ntq_emit_bcsel(c
, instr
, src
);
1092 result
= qir_SEL(c
, QPU_COND_ZC
, src
[1], src
[2]);
1096 result
= ntq_rcp(c
, src
[0]);
1099 result
= ntq_rsq(c
, src
[0]);
1102 result
= qir_EXP2(c
, src
[0]);
1105 result
= qir_LOG2(c
, src
[0]);
1109 result
= qir_ITOF(c
, qir_FTOI(c
, src
[0]));
1112 result
= ntq_fceil(c
, src
[0]);
1115 result
= ntq_ffract(c
, src
[0]);
1118 result
= ntq_ffloor(c
, src
[0]);
1122 result
= ntq_fsin(c
, src
[0]);
1125 result
= ntq_fcos(c
, src
[0]);
1129 result
= ntq_fsign(c
, src
[0]);
1133 result
= qir_FMAXABS(c
, src
[0], src
[0]);
1136 result
= qir_MAX(c
, src
[0],
1137 qir_SUB(c
, qir_uniform_ui(c
, 0), src
[0]));
1140 case nir_op_ibitfield_extract
:
1141 result
= ntq_emit_ibfe(c
, src
[0], src
[1], src
[2]);
1144 case nir_op_ubitfield_extract
:
1145 result
= ntq_emit_ubfe(c
, src
[0], src
[1], src
[2]);
1148 case nir_op_usadd_4x8
:
1149 result
= qir_V8ADDS(c
, src
[0], src
[1]);
1152 case nir_op_ussub_4x8
:
1153 result
= qir_V8SUBS(c
, src
[0], src
[1]);
1156 case nir_op_umin_4x8
:
1157 result
= qir_V8MIN(c
, src
[0], src
[1]);
1160 case nir_op_umax_4x8
:
1161 result
= qir_V8MAX(c
, src
[0], src
[1]);
1164 case nir_op_umul_unorm_4x8
:
1165 result
= qir_V8MULD(c
, src
[0], src
[1]);
1169 fprintf(stderr
, "unknown NIR ALU inst: ");
1170 nir_print_instr(&instr
->instr
, stderr
);
1171 fprintf(stderr
, "\n");
1175 /* We have a scalar result, so the instruction should only have a
1176 * single channel written to.
1178 assert(util_is_power_of_two(instr
->dest
.write_mask
));
1179 ntq_store_dest(c
, &instr
->dest
.dest
,
1180 ffs(instr
->dest
.write_mask
) - 1, result
);
1184 emit_frag_end(struct vc4_compile
*c
)
1187 if (c
->output_color_index
!= -1) {
1188 color
= c
->outputs
[c
->output_color_index
];
1190 color
= qir_uniform_ui(c
, 0);
1193 uint32_t discard_cond
= QPU_COND_ALWAYS
;
1194 if (c
->discard
.file
!= QFILE_NULL
) {
1195 qir_SF(c
, c
->discard
);
1196 discard_cond
= QPU_COND_ZS
;
1199 if (c
->fs_key
->stencil_enabled
) {
1200 qir_MOV_dest(c
, qir_reg(QFILE_TLB_STENCIL_SETUP
, 0),
1201 qir_uniform(c
, QUNIFORM_STENCIL
, 0));
1202 if (c
->fs_key
->stencil_twoside
) {
1203 qir_MOV_dest(c
, qir_reg(QFILE_TLB_STENCIL_SETUP
, 0),
1204 qir_uniform(c
, QUNIFORM_STENCIL
, 1));
1206 if (c
->fs_key
->stencil_full_writemasks
) {
1207 qir_MOV_dest(c
, qir_reg(QFILE_TLB_STENCIL_SETUP
, 0),
1208 qir_uniform(c
, QUNIFORM_STENCIL
, 2));
1212 if (c
->output_sample_mask_index
!= -1) {
1213 qir_MS_MASK(c
, c
->outputs
[c
->output_sample_mask_index
]);
1216 if (c
->fs_key
->depth_enabled
) {
1217 if (c
->output_position_index
!= -1) {
1218 qir_FTOI_dest(c
, qir_reg(QFILE_TLB_Z_WRITE
, 0),
1220 c
->outputs
[c
->output_position_index
+ 2],
1221 qir_uniform_f(c
, 0xffffff)))->cond
= discard_cond
;
1223 qir_MOV_dest(c
, qir_reg(QFILE_TLB_Z_WRITE
, 0),
1224 qir_FRAG_Z(c
))->cond
= discard_cond
;
1228 if (!c
->msaa_per_sample_output
) {
1229 qir_MOV_dest(c
, qir_reg(QFILE_TLB_COLOR_WRITE
, 0),
1230 color
)->cond
= discard_cond
;
1232 for (int i
= 0; i
< VC4_MAX_SAMPLES
; i
++) {
1233 qir_MOV_dest(c
, qir_reg(QFILE_TLB_COLOR_WRITE_MS
, 0),
1234 c
->sample_colors
[i
])->cond
= discard_cond
;
1240 emit_scaled_viewport_write(struct vc4_compile
*c
, struct qreg rcp_w
)
1242 struct qreg packed
= qir_get_temp(c
);
1244 for (int i
= 0; i
< 2; i
++) {
1246 qir_uniform(c
, QUNIFORM_VIEWPORT_X_SCALE
+ i
, 0);
1248 struct qreg packed_chan
= packed
;
1249 packed_chan
.pack
= QPU_PACK_A_16A
+ i
;
1251 qir_FTOI_dest(c
, packed_chan
,
1254 c
->outputs
[c
->output_position_index
+ i
],
1259 qir_VPM_WRITE(c
, packed
);
1263 emit_zs_write(struct vc4_compile
*c
, struct qreg rcp_w
)
1265 struct qreg zscale
= qir_uniform(c
, QUNIFORM_VIEWPORT_Z_SCALE
, 0);
1266 struct qreg zoffset
= qir_uniform(c
, QUNIFORM_VIEWPORT_Z_OFFSET
, 0);
1268 qir_VPM_WRITE(c
, qir_FADD(c
, qir_FMUL(c
, qir_FMUL(c
,
1269 c
->outputs
[c
->output_position_index
+ 2],
1276 emit_rcp_wc_write(struct vc4_compile
*c
, struct qreg rcp_w
)
1278 qir_VPM_WRITE(c
, rcp_w
);
1282 emit_point_size_write(struct vc4_compile
*c
)
1284 struct qreg point_size
;
1286 if (c
->output_point_size_index
!= -1)
1287 point_size
= c
->outputs
[c
->output_point_size_index
];
1289 point_size
= qir_uniform_f(c
, 1.0);
1291 /* Workaround: HW-2726 PTB does not handle zero-size points (BCM2835,
1294 point_size
= qir_FMAX(c
, point_size
, qir_uniform_f(c
, .125));
1296 qir_VPM_WRITE(c
, point_size
);
1300 * Emits a VPM read of the stub vertex attribute set up by vc4_draw.c.
1302 * The simulator insists that there be at least one vertex attribute, so
1303 * vc4_draw.c will emit one if it wouldn't have otherwise. The simulator also
1304 * insists that all vertex attributes loaded get read by the VS/CS, so we have
1305 * to consume it here.
1308 emit_stub_vpm_read(struct vc4_compile
*c
)
1313 c
->vattr_sizes
[0] = 4;
1314 (void)qir_MOV(c
, qir_reg(QFILE_VPM
, 0));
1319 emit_vert_end(struct vc4_compile
*c
,
1320 struct vc4_varying_slot
*fs_inputs
,
1321 uint32_t num_fs_inputs
)
1323 struct qreg rcp_w
= qir_RCP(c
, c
->outputs
[c
->output_position_index
+ 3]);
1325 emit_stub_vpm_read(c
);
1327 emit_scaled_viewport_write(c
, rcp_w
);
1328 emit_zs_write(c
, rcp_w
);
1329 emit_rcp_wc_write(c
, rcp_w
);
1330 if (c
->vs_key
->per_vertex_point_size
)
1331 emit_point_size_write(c
);
1333 for (int i
= 0; i
< num_fs_inputs
; i
++) {
1334 struct vc4_varying_slot
*input
= &fs_inputs
[i
];
1337 for (j
= 0; j
< c
->num_outputs
; j
++) {
1338 struct vc4_varying_slot
*output
=
1339 &c
->output_slots
[j
];
1341 if (input
->slot
== output
->slot
&&
1342 input
->swizzle
== output
->swizzle
) {
1343 qir_VPM_WRITE(c
, c
->outputs
[j
]);
1347 /* Emit padding if we didn't find a declared VS output for
1350 if (j
== c
->num_outputs
)
1351 qir_VPM_WRITE(c
, qir_uniform_f(c
, 0.0));
1356 emit_coord_end(struct vc4_compile
*c
)
1358 struct qreg rcp_w
= qir_RCP(c
, c
->outputs
[c
->output_position_index
+ 3]);
1360 emit_stub_vpm_read(c
);
1362 for (int i
= 0; i
< 4; i
++)
1363 qir_VPM_WRITE(c
, c
->outputs
[c
->output_position_index
+ i
]);
1365 emit_scaled_viewport_write(c
, rcp_w
);
1366 emit_zs_write(c
, rcp_w
);
1367 emit_rcp_wc_write(c
, rcp_w
);
1368 if (c
->vs_key
->per_vertex_point_size
)
1369 emit_point_size_write(c
);
1373 vc4_optimize_nir(struct nir_shader
*s
)
1380 NIR_PASS_V(s
, nir_lower_vars_to_ssa
);
1381 NIR_PASS_V(s
, nir_lower_alu_to_scalar
);
1382 NIR_PASS_V(s
, nir_lower_phis_to_scalar
);
1384 NIR_PASS(progress
, s
, nir_copy_prop
);
1385 NIR_PASS(progress
, s
, nir_opt_remove_phis
);
1386 NIR_PASS(progress
, s
, nir_opt_dce
);
1387 NIR_PASS(progress
, s
, nir_opt_dead_cf
);
1388 NIR_PASS(progress
, s
, nir_opt_cse
);
1389 NIR_PASS(progress
, s
, nir_opt_peephole_select
);
1390 NIR_PASS(progress
, s
, nir_opt_algebraic
);
1391 NIR_PASS(progress
, s
, nir_opt_constant_folding
);
1392 NIR_PASS(progress
, s
, nir_opt_undef
);
1397 driver_location_compare(const void *in_a
, const void *in_b
)
1399 const nir_variable
*const *a
= in_a
;
1400 const nir_variable
*const *b
= in_b
;
1402 return (*a
)->data
.driver_location
- (*b
)->data
.driver_location
;
1406 ntq_setup_inputs(struct vc4_compile
*c
)
1408 unsigned num_entries
= 0;
1409 nir_foreach_variable(var
, &c
->s
->inputs
)
1412 nir_variable
*vars
[num_entries
];
1415 nir_foreach_variable(var
, &c
->s
->inputs
)
1418 /* Sort the variables so that we emit the input setup in
1419 * driver_location order. This is required for VPM reads, whose data
1420 * is fetched into the VPM in driver_location (TGSI register index)
1423 qsort(&vars
, num_entries
, sizeof(*vars
), driver_location_compare
);
1425 for (unsigned i
= 0; i
< num_entries
; i
++) {
1426 nir_variable
*var
= vars
[i
];
1427 unsigned array_len
= MAX2(glsl_get_length(var
->type
), 1);
1428 unsigned loc
= var
->data
.driver_location
;
1430 assert(array_len
== 1);
1432 resize_qreg_array(c
, &c
->inputs
, &c
->inputs_array_size
,
1435 if (c
->stage
== QSTAGE_FRAG
) {
1436 if (var
->data
.location
== VARYING_SLOT_POS
) {
1437 emit_fragcoord_input(c
, loc
);
1438 } else if (var
->data
.location
>= VARYING_SLOT_VAR0
&&
1439 (c
->fs_key
->point_sprite_mask
&
1440 (1 << (var
->data
.location
-
1441 VARYING_SLOT_VAR0
)))) {
1442 c
->inputs
[loc
* 4 + 0] = c
->point_x
;
1443 c
->inputs
[loc
* 4 + 1] = c
->point_y
;
1445 emit_fragment_input(c
, loc
, var
->data
.location
);
1448 emit_vertex_input(c
, loc
);
1454 ntq_setup_outputs(struct vc4_compile
*c
)
1456 nir_foreach_variable(var
, &c
->s
->outputs
) {
1457 unsigned array_len
= MAX2(glsl_get_length(var
->type
), 1);
1458 unsigned loc
= var
->data
.driver_location
* 4;
1460 assert(array_len
== 1);
1463 for (int i
= 0; i
< 4; i
++)
1464 add_output(c
, loc
+ i
, var
->data
.location
, i
);
1466 if (c
->stage
== QSTAGE_FRAG
) {
1467 switch (var
->data
.location
) {
1468 case FRAG_RESULT_COLOR
:
1469 case FRAG_RESULT_DATA0
:
1470 c
->output_color_index
= loc
;
1472 case FRAG_RESULT_DEPTH
:
1473 c
->output_position_index
= loc
;
1475 case FRAG_RESULT_SAMPLE_MASK
:
1476 c
->output_sample_mask_index
= loc
;
1480 switch (var
->data
.location
) {
1481 case VARYING_SLOT_POS
:
1482 c
->output_position_index
= loc
;
1484 case VARYING_SLOT_PSIZ
:
1485 c
->output_point_size_index
= loc
;
1493 ntq_setup_uniforms(struct vc4_compile
*c
)
1495 nir_foreach_variable(var
, &c
->s
->uniforms
) {
1496 unsigned array_len
= MAX2(glsl_get_length(var
->type
), 1);
1497 unsigned array_elem_size
= 4 * sizeof(float);
1499 declare_uniform_range(c
, var
->data
.driver_location
* array_elem_size
,
1500 array_len
* array_elem_size
);
1506 * Sets up the mapping from nir_register to struct qreg *.
1508 * Each nir_register gets a struct qreg per 32-bit component being stored.
1511 ntq_setup_registers(struct vc4_compile
*c
, struct exec_list
*list
)
1513 foreach_list_typed(nir_register
, nir_reg
, node
, list
) {
1514 unsigned array_len
= MAX2(nir_reg
->num_array_elems
, 1);
1515 struct qreg
*qregs
= ralloc_array(c
->def_ht
, struct qreg
,
1517 nir_reg
->num_components
);
1519 _mesa_hash_table_insert(c
->def_ht
, nir_reg
, qregs
);
1521 for (int i
= 0; i
< array_len
* nir_reg
->num_components
; i
++)
1522 qregs
[i
] = qir_get_temp(c
);
1527 ntq_emit_load_const(struct vc4_compile
*c
, nir_load_const_instr
*instr
)
1529 struct qreg
*qregs
= ntq_init_ssa_def(c
, &instr
->def
);
1530 for (int i
= 0; i
< instr
->def
.num_components
; i
++)
1531 qregs
[i
] = qir_uniform_ui(c
, instr
->value
.u32
[i
]);
1533 _mesa_hash_table_insert(c
->def_ht
, &instr
->def
, qregs
);
1537 ntq_emit_ssa_undef(struct vc4_compile
*c
, nir_ssa_undef_instr
*instr
)
1539 struct qreg
*qregs
= ntq_init_ssa_def(c
, &instr
->def
);
1541 /* QIR needs there to be *some* value, so pick 0 (same as for
1542 * ntq_setup_registers().
1544 for (int i
= 0; i
< instr
->def
.num_components
; i
++)
1545 qregs
[i
] = qir_uniform_ui(c
, 0);
1549 ntq_emit_intrinsic(struct vc4_compile
*c
, nir_intrinsic_instr
*instr
)
1551 nir_const_value
*const_offset
;
1554 switch (instr
->intrinsic
) {
1555 case nir_intrinsic_load_uniform
:
1556 assert(instr
->num_components
== 1);
1557 const_offset
= nir_src_as_const_value(instr
->src
[0]);
1559 offset
= instr
->const_index
[0] + const_offset
->u32
[0];
1560 assert(offset
% 4 == 0);
1561 /* We need dwords */
1562 offset
= offset
/ 4;
1563 if (offset
< VC4_NIR_STATE_UNIFORM_OFFSET
) {
1564 ntq_store_dest(c
, &instr
->dest
, 0,
1565 qir_uniform(c
, QUNIFORM_UNIFORM
,
1568 ntq_store_dest(c
, &instr
->dest
, 0,
1569 qir_uniform(c
, offset
-
1570 VC4_NIR_STATE_UNIFORM_OFFSET
,
1574 ntq_store_dest(c
, &instr
->dest
, 0,
1575 indirect_uniform_load(c
, instr
));
1579 case nir_intrinsic_load_user_clip_plane
:
1580 for (int i
= 0; i
< instr
->num_components
; i
++) {
1581 ntq_store_dest(c
, &instr
->dest
, i
,
1582 qir_uniform(c
, QUNIFORM_USER_CLIP_PLANE
,
1583 instr
->const_index
[0] * 4 +
1588 case nir_intrinsic_load_sample_mask_in
:
1589 ntq_store_dest(c
, &instr
->dest
, 0,
1590 qir_uniform(c
, QUNIFORM_SAMPLE_MASK
, 0));
1593 case nir_intrinsic_load_front_face
:
1594 /* The register contains 0 (front) or 1 (back), and we need to
1595 * turn it into a NIR bool where true means front.
1597 ntq_store_dest(c
, &instr
->dest
, 0,
1599 qir_uniform_ui(c
, -1),
1600 qir_reg(QFILE_FRAG_REV_FLAG
, 0)));
1603 case nir_intrinsic_load_input
:
1604 assert(instr
->num_components
== 1);
1605 const_offset
= nir_src_as_const_value(instr
->src
[0]);
1606 assert(const_offset
&& "vc4 doesn't support indirect inputs");
1607 if (instr
->const_index
[0] >= VC4_NIR_TLB_COLOR_READ_INPUT
) {
1608 assert(const_offset
->u32
[0] == 0);
1609 /* Reads of the per-sample color need to be done in
1612 int sample_index
= (instr
->const_index
[0] -
1613 VC4_NIR_TLB_COLOR_READ_INPUT
);
1614 for (int i
= 0; i
<= sample_index
; i
++) {
1615 if (c
->color_reads
[i
].file
== QFILE_NULL
) {
1617 qir_TLB_COLOR_READ(c
);
1620 ntq_store_dest(c
, &instr
->dest
, 0,
1621 c
->color_reads
[sample_index
]);
1623 offset
= instr
->const_index
[0] + const_offset
->u32
[0];
1624 ntq_store_dest(c
, &instr
->dest
, 0,
1629 case nir_intrinsic_store_output
:
1630 const_offset
= nir_src_as_const_value(instr
->src
[1]);
1631 assert(const_offset
&& "vc4 doesn't support indirect outputs");
1632 offset
= instr
->const_index
[0] + const_offset
->u32
[0];
1634 /* MSAA color outputs are the only case where we have an
1635 * output that's not lowered to being a store of a single 32
1638 if (c
->stage
== QSTAGE_FRAG
&& instr
->num_components
== 4) {
1639 assert(offset
== c
->output_color_index
);
1640 for (int i
= 0; i
< 4; i
++) {
1641 c
->sample_colors
[i
] =
1642 qir_MOV(c
, ntq_get_src(c
, instr
->src
[0],
1646 assert(instr
->num_components
== 1);
1647 c
->outputs
[offset
] =
1648 qir_MOV(c
, ntq_get_src(c
, instr
->src
[0], 0));
1649 c
->num_outputs
= MAX2(c
->num_outputs
, offset
+ 1);
1653 case nir_intrinsic_discard
:
1654 c
->discard
= qir_uniform_ui(c
, ~0);
1657 case nir_intrinsic_discard_if
:
1658 if (c
->discard
.file
== QFILE_NULL
)
1659 c
->discard
= qir_uniform_ui(c
, 0);
1660 c
->discard
= qir_OR(c
, c
->discard
,
1661 ntq_get_src(c
, instr
->src
[0], 0));
1665 fprintf(stderr
, "Unknown intrinsic: ");
1666 nir_print_instr(&instr
->instr
, stderr
);
1667 fprintf(stderr
, "\n");
1672 /* Clears (activates) the execute flags for any channels whose jump target
1673 * matches this block.
1676 ntq_activate_execute_for_block(struct vc4_compile
*c
)
1678 qir_SF(c
, qir_SUB(c
,
1680 qir_uniform_ui(c
, c
->cur_block
->index
)));
1681 qir_MOV_cond(c
, QPU_COND_ZS
, c
->execute
, qir_uniform_ui(c
, 0));
1685 ntq_emit_if(struct vc4_compile
*c
, nir_if
*if_stmt
)
1687 if (!c
->vc4
->screen
->has_control_flow
) {
1689 "IF statement support requires updated kernel.\n");
1693 nir_cf_node
*nir_first_else_node
= nir_if_first_else_node(if_stmt
);
1694 nir_cf_node
*nir_last_else_node
= nir_if_last_else_node(if_stmt
);
1695 nir_block
*nir_else_block
= nir_cf_node_as_block(nir_first_else_node
);
1696 bool empty_else_block
=
1697 (nir_first_else_node
== nir_last_else_node
&&
1698 exec_list_is_empty(&nir_else_block
->instr_list
));
1700 struct qblock
*then_block
= qir_new_block(c
);
1701 struct qblock
*after_block
= qir_new_block(c
);
1702 struct qblock
*else_block
;
1703 if (empty_else_block
)
1704 else_block
= after_block
;
1706 else_block
= qir_new_block(c
);
1708 bool was_top_level
= false;
1709 if (c
->execute
.file
== QFILE_NULL
) {
1710 c
->execute
= qir_MOV(c
, qir_uniform_ui(c
, 0));
1711 was_top_level
= true;
1714 /* Set ZS for executing (execute == 0) and jumping (if->condition ==
1715 * 0) channels, and then update execute flags for those to point to
1720 ntq_get_src(c
, if_stmt
->condition
, 0)));
1721 qir_MOV_cond(c
, QPU_COND_ZS
, c
->execute
,
1722 qir_uniform_ui(c
, else_block
->index
));
1724 /* Jump to ELSE if nothing is active for THEN, otherwise fall
1727 qir_SF(c
, c
->execute
);
1728 qir_BRANCH(c
, QPU_COND_BRANCH_ALL_ZC
);
1729 qir_link_blocks(c
->cur_block
, else_block
);
1730 qir_link_blocks(c
->cur_block
, then_block
);
1732 /* Process the THEN block. */
1733 qir_set_emit_block(c
, then_block
);
1734 ntq_emit_cf_list(c
, &if_stmt
->then_list
);
1736 if (!empty_else_block
) {
1737 /* Handle the end of the THEN block. First, all currently
1738 * active channels update their execute flags to point to
1741 qir_SF(c
, c
->execute
);
1742 qir_MOV_cond(c
, QPU_COND_ZS
, c
->execute
,
1743 qir_uniform_ui(c
, after_block
->index
));
1745 /* If everything points at ENDIF, then jump there immediately. */
1746 qir_SF(c
, qir_SUB(c
, c
->execute
, qir_uniform_ui(c
, after_block
->index
)));
1747 qir_BRANCH(c
, QPU_COND_BRANCH_ALL_ZS
);
1748 qir_link_blocks(c
->cur_block
, after_block
);
1749 qir_link_blocks(c
->cur_block
, else_block
);
1751 qir_set_emit_block(c
, else_block
);
1752 ntq_activate_execute_for_block(c
);
1753 ntq_emit_cf_list(c
, &if_stmt
->else_list
);
1756 qir_link_blocks(c
->cur_block
, after_block
);
1758 qir_set_emit_block(c
, after_block
);
1760 c
->execute
= c
->undef
;
1762 ntq_activate_execute_for_block(c
);
1767 ntq_emit_jump(struct vc4_compile
*c
, nir_jump_instr
*jump
)
1769 switch (jump
->type
) {
1770 case nir_jump_break
:
1771 qir_SF(c
, c
->execute
);
1772 qir_MOV_cond(c
, QPU_COND_ZS
, c
->execute
,
1773 qir_uniform_ui(c
, c
->loop_break_block
->index
));
1776 case nir_jump_continue
:
1777 qir_SF(c
, c
->execute
);
1778 qir_MOV_cond(c
, QPU_COND_ZS
, c
->execute
,
1779 qir_uniform_ui(c
, c
->loop_cont_block
->index
));
1782 case nir_jump_return
:
1783 unreachable("All returns shouold be lowered\n");
1788 ntq_emit_instr(struct vc4_compile
*c
, nir_instr
*instr
)
1790 switch (instr
->type
) {
1791 case nir_instr_type_alu
:
1792 ntq_emit_alu(c
, nir_instr_as_alu(instr
));
1795 case nir_instr_type_intrinsic
:
1796 ntq_emit_intrinsic(c
, nir_instr_as_intrinsic(instr
));
1799 case nir_instr_type_load_const
:
1800 ntq_emit_load_const(c
, nir_instr_as_load_const(instr
));
1803 case nir_instr_type_ssa_undef
:
1804 ntq_emit_ssa_undef(c
, nir_instr_as_ssa_undef(instr
));
1807 case nir_instr_type_tex
:
1808 ntq_emit_tex(c
, nir_instr_as_tex(instr
));
1811 case nir_instr_type_jump
:
1812 ntq_emit_jump(c
, nir_instr_as_jump(instr
));
1816 fprintf(stderr
, "Unknown NIR instr type: ");
1817 nir_print_instr(instr
, stderr
);
1818 fprintf(stderr
, "\n");
1824 ntq_emit_block(struct vc4_compile
*c
, nir_block
*block
)
1826 nir_foreach_instr(instr
, block
) {
1827 ntq_emit_instr(c
, instr
);
1831 static void ntq_emit_cf_list(struct vc4_compile
*c
, struct exec_list
*list
);
1834 ntq_emit_loop(struct vc4_compile
*c
, nir_loop
*loop
)
1836 if (!c
->vc4
->screen
->has_control_flow
) {
1838 "loop support requires updated kernel.\n");
1839 ntq_emit_cf_list(c
, &loop
->body
);
1843 bool was_top_level
= false;
1844 if (c
->execute
.file
== QFILE_NULL
) {
1845 c
->execute
= qir_MOV(c
, qir_uniform_ui(c
, 0));
1846 was_top_level
= true;
1849 struct qblock
*save_loop_cont_block
= c
->loop_cont_block
;
1850 struct qblock
*save_loop_break_block
= c
->loop_break_block
;
1852 c
->loop_cont_block
= qir_new_block(c
);
1853 c
->loop_break_block
= qir_new_block(c
);
1855 qir_link_blocks(c
->cur_block
, c
->loop_cont_block
);
1856 qir_set_emit_block(c
, c
->loop_cont_block
);
1857 ntq_activate_execute_for_block(c
);
1859 ntq_emit_cf_list(c
, &loop
->body
);
1861 /* If anything had explicitly continued, or is here at the end of the
1862 * loop, then we need to loop again. SF updates are masked by the
1863 * instruction's condition, so we can do the OR of the two conditions
1866 qir_SF(c
, c
->execute
);
1867 struct qinst
*cont_check
=
1871 qir_uniform_ui(c
, c
->loop_cont_block
->index
));
1872 cont_check
->cond
= QPU_COND_ZC
;
1873 cont_check
->sf
= true;
1875 qir_BRANCH(c
, QPU_COND_BRANCH_ANY_ZS
);
1876 qir_link_blocks(c
->cur_block
, c
->loop_cont_block
);
1877 qir_link_blocks(c
->cur_block
, c
->loop_break_block
);
1879 qir_set_emit_block(c
, c
->loop_break_block
);
1881 c
->execute
= c
->undef
;
1883 ntq_activate_execute_for_block(c
);
1885 c
->loop_break_block
= save_loop_break_block
;
1886 c
->loop_cont_block
= save_loop_cont_block
;
1890 ntq_emit_function(struct vc4_compile
*c
, nir_function_impl
*func
)
1892 fprintf(stderr
, "FUNCTIONS not handled.\n");
1897 ntq_emit_cf_list(struct vc4_compile
*c
, struct exec_list
*list
)
1899 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
1900 switch (node
->type
) {
1901 case nir_cf_node_block
:
1902 ntq_emit_block(c
, nir_cf_node_as_block(node
));
1905 case nir_cf_node_if
:
1906 ntq_emit_if(c
, nir_cf_node_as_if(node
));
1909 case nir_cf_node_loop
:
1910 ntq_emit_loop(c
, nir_cf_node_as_loop(node
));
1913 case nir_cf_node_function
:
1914 ntq_emit_function(c
, nir_cf_node_as_function(node
));
1918 fprintf(stderr
, "Unknown NIR node type\n");
1925 ntq_emit_impl(struct vc4_compile
*c
, nir_function_impl
*impl
)
1927 ntq_setup_registers(c
, &impl
->registers
);
1928 ntq_emit_cf_list(c
, &impl
->body
);
1932 nir_to_qir(struct vc4_compile
*c
)
1934 ntq_setup_inputs(c
);
1935 ntq_setup_outputs(c
);
1936 ntq_setup_uniforms(c
);
1937 ntq_setup_registers(c
, &c
->s
->registers
);
1939 /* Find the main function and emit the body. */
1940 nir_foreach_function(function
, c
->s
) {
1941 assert(strcmp(function
->name
, "main") == 0);
1942 assert(function
->impl
);
1943 ntq_emit_impl(c
, function
->impl
);
1947 static const nir_shader_compiler_options nir_options
= {
1948 .lower_extract_byte
= true,
1949 .lower_extract_word
= true,
1951 .lower_flrp32
= true,
1954 .lower_fsqrt
= true,
1955 .lower_negate
= true,
1959 count_nir_instrs(nir_shader
*nir
)
1962 nir_foreach_function(function
, nir
) {
1963 if (!function
->impl
)
1965 nir_foreach_block(block
, function
->impl
) {
1966 nir_foreach_instr(instr
, block
)
1973 static struct vc4_compile
*
1974 vc4_shader_ntq(struct vc4_context
*vc4
, enum qstage stage
,
1975 struct vc4_key
*key
)
1977 struct vc4_compile
*c
= qir_compile_init();
1981 c
->shader_state
= &key
->shader_state
->base
;
1982 c
->program_id
= key
->shader_state
->program_id
;
1984 p_atomic_inc_return(&key
->shader_state
->compiled_variant_count
);
1989 c
->fs_key
= (struct vc4_fs_key
*)key
;
1990 if (c
->fs_key
->is_points
) {
1991 c
->point_x
= emit_fragment_varying(c
, ~0, 0);
1992 c
->point_y
= emit_fragment_varying(c
, ~0, 0);
1993 } else if (c
->fs_key
->is_lines
) {
1994 c
->line_x
= emit_fragment_varying(c
, ~0, 0);
1998 c
->vs_key
= (struct vc4_vs_key
*)key
;
2001 c
->vs_key
= (struct vc4_vs_key
*)key
;
2005 c
->s
= nir_shader_clone(c
, key
->shader_state
->base
.ir
.nir
);
2006 NIR_PASS_V(c
->s
, nir_opt_global_to_local
);
2007 NIR_PASS_V(c
->s
, nir_convert_to_ssa
);
2009 if (stage
== QSTAGE_FRAG
)
2010 NIR_PASS_V(c
->s
, vc4_nir_lower_blend
, c
);
2012 struct nir_lower_tex_options tex_options
= {
2013 /* We would need to implement txs, but we don't want the
2014 * int/float conversions
2016 .lower_rect
= false,
2020 /* Apply swizzles to all samplers. */
2021 .swizzle_result
= ~0,
2024 /* Lower the format swizzle and ARB_texture_swizzle-style swizzle.
2025 * The format swizzling applies before sRGB decode, and
2026 * ARB_texture_swizzle is the last thing before returning the sample.
2028 for (int i
= 0; i
< ARRAY_SIZE(key
->tex
); i
++) {
2029 enum pipe_format format
= c
->key
->tex
[i
].format
;
2034 const uint8_t *format_swizzle
= vc4_get_format_swizzle(format
);
2036 for (int j
= 0; j
< 4; j
++) {
2037 uint8_t arb_swiz
= c
->key
->tex
[i
].swizzle
[j
];
2039 if (arb_swiz
<= 3) {
2040 tex_options
.swizzles
[i
][j
] =
2041 format_swizzle
[arb_swiz
];
2043 tex_options
.swizzles
[i
][j
] = arb_swiz
;
2047 if (util_format_is_srgb(format
))
2048 tex_options
.lower_srgb
|= (1 << i
);
2051 NIR_PASS_V(c
->s
, nir_normalize_cubemap_coords
);
2052 NIR_PASS_V(c
->s
, nir_lower_tex
, &tex_options
);
2054 if (c
->fs_key
&& c
->fs_key
->light_twoside
)
2055 NIR_PASS_V(c
->s
, nir_lower_two_sided_color
);
2057 if (c
->vs_key
&& c
->vs_key
->clamp_color
)
2058 NIR_PASS_V(c
->s
, nir_lower_clamp_color_outputs
);
2060 if (stage
== QSTAGE_FRAG
)
2061 NIR_PASS_V(c
->s
, nir_lower_clip_fs
, c
->key
->ucp_enables
);
2063 NIR_PASS_V(c
->s
, nir_lower_clip_vs
, c
->key
->ucp_enables
);
2065 NIR_PASS_V(c
->s
, vc4_nir_lower_io
, c
);
2066 NIR_PASS_V(c
->s
, vc4_nir_lower_txf_ms
, c
);
2067 NIR_PASS_V(c
->s
, nir_lower_idiv
);
2068 NIR_PASS_V(c
->s
, nir_lower_load_const_to_scalar
);
2070 vc4_optimize_nir(c
->s
);
2072 NIR_PASS_V(c
->s
, nir_remove_dead_variables
, nir_var_local
);
2073 NIR_PASS_V(c
->s
, nir_convert_from_ssa
, true);
2075 if (vc4_debug
& VC4_DEBUG_SHADERDB
) {
2076 fprintf(stderr
, "SHADER-DB: %s prog %d/%d: %d NIR instructions\n",
2077 qir_get_stage_name(c
->stage
),
2078 c
->program_id
, c
->variant_id
,
2079 count_nir_instrs(c
->s
));
2082 if (vc4_debug
& VC4_DEBUG_NIR
) {
2083 fprintf(stderr
, "%s prog %d/%d NIR:\n",
2084 qir_get_stage_name(c
->stage
),
2085 c
->program_id
, c
->variant_id
);
2086 nir_print_shader(c
->s
, stderr
);
2097 vc4
->prog
.fs
->input_slots
,
2098 vc4
->prog
.fs
->num_inputs
);
2105 if (vc4_debug
& VC4_DEBUG_QIR
) {
2106 fprintf(stderr
, "%s prog %d/%d pre-opt QIR:\n",
2107 qir_get_stage_name(c
->stage
),
2108 c
->program_id
, c
->variant_id
);
2110 fprintf(stderr
, "\n");
2114 qir_lower_uniforms(c
);
2116 qir_schedule_instructions(c
);
2118 if (vc4_debug
& VC4_DEBUG_QIR
) {
2119 fprintf(stderr
, "%s prog %d/%d QIR:\n",
2120 qir_get_stage_name(c
->stage
),
2121 c
->program_id
, c
->variant_id
);
2123 fprintf(stderr
, "\n");
2126 qir_reorder_uniforms(c
);
2127 vc4_generate_code(vc4
, c
);
2129 if (vc4_debug
& VC4_DEBUG_SHADERDB
) {
2130 fprintf(stderr
, "SHADER-DB: %s prog %d/%d: %d instructions\n",
2131 qir_get_stage_name(c
->stage
),
2132 c
->program_id
, c
->variant_id
,
2134 fprintf(stderr
, "SHADER-DB: %s prog %d/%d: %d uniforms\n",
2135 qir_get_stage_name(c
->stage
),
2136 c
->program_id
, c
->variant_id
,
2146 vc4_shader_state_create(struct pipe_context
*pctx
,
2147 const struct pipe_shader_state
*cso
)
2149 struct vc4_context
*vc4
= vc4_context(pctx
);
2150 struct vc4_uncompiled_shader
*so
= CALLOC_STRUCT(vc4_uncompiled_shader
);
2154 so
->program_id
= vc4
->next_uncompiled_program_id
++;
2156 nir_shader
*s
= tgsi_to_nir(cso
->tokens
, &nir_options
);
2158 if (vc4_debug
& VC4_DEBUG_TGSI
) {
2159 fprintf(stderr
, "%s prog %d TGSI:\n",
2160 gl_shader_stage_name(s
->stage
),
2162 tgsi_dump(cso
->tokens
, 0);
2163 fprintf(stderr
, "\n");
2166 so
->base
.type
= PIPE_SHADER_IR_NIR
;
2167 so
->base
.ir
.nir
= s
;
2173 copy_uniform_state_to_shader(struct vc4_compiled_shader
*shader
,
2174 struct vc4_compile
*c
)
2176 int count
= c
->num_uniforms
;
2177 struct vc4_shader_uniform_info
*uinfo
= &shader
->uniforms
;
2179 uinfo
->count
= count
;
2180 uinfo
->data
= ralloc_array(shader
, uint32_t, count
);
2181 memcpy(uinfo
->data
, c
->uniform_data
,
2182 count
* sizeof(*uinfo
->data
));
2183 uinfo
->contents
= ralloc_array(shader
, enum quniform_contents
, count
);
2184 memcpy(uinfo
->contents
, c
->uniform_contents
,
2185 count
* sizeof(*uinfo
->contents
));
2186 uinfo
->num_texture_samples
= c
->num_texture_samples
;
2188 vc4_set_shader_uniform_dirty_flags(shader
);
2191 static struct vc4_compiled_shader
*
2192 vc4_get_compiled_shader(struct vc4_context
*vc4
, enum qstage stage
,
2193 struct vc4_key
*key
)
2195 struct hash_table
*ht
;
2197 if (stage
== QSTAGE_FRAG
) {
2199 key_size
= sizeof(struct vc4_fs_key
);
2202 key_size
= sizeof(struct vc4_vs_key
);
2205 struct vc4_compiled_shader
*shader
;
2206 struct hash_entry
*entry
= _mesa_hash_table_search(ht
, key
);
2210 struct vc4_compile
*c
= vc4_shader_ntq(vc4
, stage
, key
);
2211 shader
= rzalloc(NULL
, struct vc4_compiled_shader
);
2213 shader
->program_id
= vc4
->next_compiled_program_id
++;
2214 if (stage
== QSTAGE_FRAG
) {
2215 bool input_live
[c
->num_input_slots
];
2217 memset(input_live
, 0, sizeof(input_live
));
2218 qir_for_each_inst_inorder(inst
, c
) {
2219 for (int i
= 0; i
< qir_get_op_nsrc(inst
->op
); i
++) {
2220 if (inst
->src
[i
].file
== QFILE_VARY
)
2221 input_live
[inst
->src
[i
].index
] = true;
2225 shader
->input_slots
= ralloc_array(shader
,
2226 struct vc4_varying_slot
,
2227 c
->num_input_slots
);
2229 for (int i
= 0; i
< c
->num_input_slots
; i
++) {
2230 struct vc4_varying_slot
*slot
= &c
->input_slots
[i
];
2235 /* Skip non-VS-output inputs. */
2236 if (slot
->slot
== (uint8_t)~0)
2239 if (slot
->slot
== VARYING_SLOT_COL0
||
2240 slot
->slot
== VARYING_SLOT_COL1
||
2241 slot
->slot
== VARYING_SLOT_BFC0
||
2242 slot
->slot
== VARYING_SLOT_BFC1
) {
2243 shader
->color_inputs
|= (1 << shader
->num_inputs
);
2246 shader
->input_slots
[shader
->num_inputs
] = *slot
;
2247 shader
->num_inputs
++;
2250 shader
->num_inputs
= c
->num_inputs
;
2252 shader
->vattr_offsets
[0] = 0;
2253 for (int i
= 0; i
< 8; i
++) {
2254 shader
->vattr_offsets
[i
+ 1] =
2255 shader
->vattr_offsets
[i
] + c
->vattr_sizes
[i
];
2257 if (c
->vattr_sizes
[i
])
2258 shader
->vattrs_live
|= (1 << i
);
2262 copy_uniform_state_to_shader(shader
, c
);
2263 shader
->bo
= vc4_bo_alloc_shader(vc4
->screen
, c
->qpu_insts
,
2264 c
->qpu_inst_count
* sizeof(uint64_t));
2266 /* Copy the compiler UBO range state to the compiled shader, dropping
2267 * out arrays that were never referenced by an indirect load.
2269 * (Note that QIR dead code elimination of an array access still
2270 * leaves that array alive, though)
2272 if (c
->num_ubo_ranges
) {
2273 shader
->num_ubo_ranges
= c
->num_ubo_ranges
;
2274 shader
->ubo_ranges
= ralloc_array(shader
, struct vc4_ubo_range
,
2277 for (int i
= 0; i
< c
->num_uniform_ranges
; i
++) {
2278 struct vc4_compiler_ubo_range
*range
=
2283 shader
->ubo_ranges
[j
].dst_offset
= range
->dst_offset
;
2284 shader
->ubo_ranges
[j
].src_offset
= range
->src_offset
;
2285 shader
->ubo_ranges
[j
].size
= range
->size
;
2286 shader
->ubo_size
+= c
->ubo_ranges
[i
].size
;
2290 if (shader
->ubo_size
) {
2291 if (vc4_debug
& VC4_DEBUG_SHADERDB
) {
2292 fprintf(stderr
, "SHADER-DB: %s prog %d/%d: %d UBO uniforms\n",
2293 qir_get_stage_name(c
->stage
),
2294 c
->program_id
, c
->variant_id
,
2295 shader
->ubo_size
/ 4);
2299 qir_compile_destroy(c
);
2301 struct vc4_key
*dup_key
;
2302 dup_key
= ralloc_size(shader
, key_size
);
2303 memcpy(dup_key
, key
, key_size
);
2304 _mesa_hash_table_insert(ht
, dup_key
, shader
);
2310 vc4_setup_shared_key(struct vc4_context
*vc4
, struct vc4_key
*key
,
2311 struct vc4_texture_stateobj
*texstate
)
2313 for (int i
= 0; i
< texstate
->num_textures
; i
++) {
2314 struct pipe_sampler_view
*sampler
= texstate
->textures
[i
];
2315 struct pipe_sampler_state
*sampler_state
=
2316 texstate
->samplers
[i
];
2321 key
->tex
[i
].format
= sampler
->format
;
2322 key
->tex
[i
].swizzle
[0] = sampler
->swizzle_r
;
2323 key
->tex
[i
].swizzle
[1] = sampler
->swizzle_g
;
2324 key
->tex
[i
].swizzle
[2] = sampler
->swizzle_b
;
2325 key
->tex
[i
].swizzle
[3] = sampler
->swizzle_a
;
2327 if (sampler
->texture
->nr_samples
> 1) {
2328 key
->tex
[i
].msaa_width
= sampler
->texture
->width0
;
2329 key
->tex
[i
].msaa_height
= sampler
->texture
->height0
;
2330 } else if (sampler
){
2331 key
->tex
[i
].compare_mode
= sampler_state
->compare_mode
;
2332 key
->tex
[i
].compare_func
= sampler_state
->compare_func
;
2333 key
->tex
[i
].wrap_s
= sampler_state
->wrap_s
;
2334 key
->tex
[i
].wrap_t
= sampler_state
->wrap_t
;
2338 key
->ucp_enables
= vc4
->rasterizer
->base
.clip_plane_enable
;
2342 vc4_update_compiled_fs(struct vc4_context
*vc4
, uint8_t prim_mode
)
2344 struct vc4_fs_key local_key
;
2345 struct vc4_fs_key
*key
= &local_key
;
2347 if (!(vc4
->dirty
& (VC4_DIRTY_PRIM_MODE
|
2349 VC4_DIRTY_FRAMEBUFFER
|
2351 VC4_DIRTY_RASTERIZER
|
2352 VC4_DIRTY_SAMPLE_MASK
|
2354 VC4_DIRTY_TEXSTATE
|
2355 VC4_DIRTY_UNCOMPILED_FS
))) {
2359 memset(key
, 0, sizeof(*key
));
2360 vc4_setup_shared_key(vc4
, &key
->base
, &vc4
->fragtex
);
2361 key
->base
.shader_state
= vc4
->prog
.bind_fs
;
2362 key
->is_points
= (prim_mode
== PIPE_PRIM_POINTS
);
2363 key
->is_lines
= (prim_mode
>= PIPE_PRIM_LINES
&&
2364 prim_mode
<= PIPE_PRIM_LINE_STRIP
);
2365 key
->blend
= vc4
->blend
->rt
[0];
2366 if (vc4
->blend
->logicop_enable
) {
2367 key
->logicop_func
= vc4
->blend
->logicop_func
;
2369 key
->logicop_func
= PIPE_LOGICOP_COPY
;
2372 key
->msaa
= vc4
->rasterizer
->base
.multisample
;
2373 key
->sample_coverage
= (vc4
->rasterizer
->base
.multisample
&&
2374 vc4
->sample_mask
!= (1 << VC4_MAX_SAMPLES
) - 1);
2375 key
->sample_alpha_to_coverage
= vc4
->blend
->alpha_to_coverage
;
2376 key
->sample_alpha_to_one
= vc4
->blend
->alpha_to_one
;
2379 if (vc4
->framebuffer
.cbufs
[0])
2380 key
->color_format
= vc4
->framebuffer
.cbufs
[0]->format
;
2382 key
->stencil_enabled
= vc4
->zsa
->stencil_uniforms
[0] != 0;
2383 key
->stencil_twoside
= vc4
->zsa
->stencil_uniforms
[1] != 0;
2384 key
->stencil_full_writemasks
= vc4
->zsa
->stencil_uniforms
[2] != 0;
2385 key
->depth_enabled
= (vc4
->zsa
->base
.depth
.enabled
||
2386 key
->stencil_enabled
);
2387 if (vc4
->zsa
->base
.alpha
.enabled
) {
2388 key
->alpha_test
= true;
2389 key
->alpha_test_func
= vc4
->zsa
->base
.alpha
.func
;
2392 if (key
->is_points
) {
2393 key
->point_sprite_mask
=
2394 vc4
->rasterizer
->base
.sprite_coord_enable
;
2395 key
->point_coord_upper_left
=
2396 (vc4
->rasterizer
->base
.sprite_coord_mode
==
2397 PIPE_SPRITE_COORD_UPPER_LEFT
);
2400 key
->light_twoside
= vc4
->rasterizer
->base
.light_twoside
;
2402 struct vc4_compiled_shader
*old_fs
= vc4
->prog
.fs
;
2403 vc4
->prog
.fs
= vc4_get_compiled_shader(vc4
, QSTAGE_FRAG
, &key
->base
);
2404 if (vc4
->prog
.fs
== old_fs
)
2407 vc4
->dirty
|= VC4_DIRTY_COMPILED_FS
;
2408 if (vc4
->rasterizer
->base
.flatshade
&&
2409 old_fs
&& vc4
->prog
.fs
->color_inputs
!= old_fs
->color_inputs
) {
2410 vc4
->dirty
|= VC4_DIRTY_FLAT_SHADE_FLAGS
;
2415 vc4_update_compiled_vs(struct vc4_context
*vc4
, uint8_t prim_mode
)
2417 struct vc4_vs_key local_key
;
2418 struct vc4_vs_key
*key
= &local_key
;
2420 if (!(vc4
->dirty
& (VC4_DIRTY_PRIM_MODE
|
2421 VC4_DIRTY_RASTERIZER
|
2423 VC4_DIRTY_TEXSTATE
|
2424 VC4_DIRTY_VTXSTATE
|
2425 VC4_DIRTY_UNCOMPILED_VS
|
2426 VC4_DIRTY_COMPILED_FS
))) {
2430 memset(key
, 0, sizeof(*key
));
2431 vc4_setup_shared_key(vc4
, &key
->base
, &vc4
->verttex
);
2432 key
->base
.shader_state
= vc4
->prog
.bind_vs
;
2433 key
->compiled_fs_id
= vc4
->prog
.fs
->program_id
;
2434 key
->clamp_color
= vc4
->rasterizer
->base
.clamp_vertex_color
;
2436 for (int i
= 0; i
< ARRAY_SIZE(key
->attr_formats
); i
++)
2437 key
->attr_formats
[i
] = vc4
->vtx
->pipe
[i
].src_format
;
2439 key
->per_vertex_point_size
=
2440 (prim_mode
== PIPE_PRIM_POINTS
&&
2441 vc4
->rasterizer
->base
.point_size_per_vertex
);
2443 struct vc4_compiled_shader
*vs
=
2444 vc4_get_compiled_shader(vc4
, QSTAGE_VERT
, &key
->base
);
2445 if (vs
!= vc4
->prog
.vs
) {
2447 vc4
->dirty
|= VC4_DIRTY_COMPILED_VS
;
2450 key
->is_coord
= true;
2451 struct vc4_compiled_shader
*cs
=
2452 vc4_get_compiled_shader(vc4
, QSTAGE_COORD
, &key
->base
);
2453 if (cs
!= vc4
->prog
.cs
) {
2455 vc4
->dirty
|= VC4_DIRTY_COMPILED_CS
;
2460 vc4_update_compiled_shaders(struct vc4_context
*vc4
, uint8_t prim_mode
)
2462 vc4_update_compiled_fs(vc4
, prim_mode
);
2463 vc4_update_compiled_vs(vc4
, prim_mode
);
2467 fs_cache_hash(const void *key
)
2469 return _mesa_hash_data(key
, sizeof(struct vc4_fs_key
));
2473 vs_cache_hash(const void *key
)
2475 return _mesa_hash_data(key
, sizeof(struct vc4_vs_key
));
2479 fs_cache_compare(const void *key1
, const void *key2
)
2481 return memcmp(key1
, key2
, sizeof(struct vc4_fs_key
)) == 0;
2485 vs_cache_compare(const void *key1
, const void *key2
)
2487 return memcmp(key1
, key2
, sizeof(struct vc4_vs_key
)) == 0;
2491 delete_from_cache_if_matches(struct hash_table
*ht
,
2492 struct hash_entry
*entry
,
2493 struct vc4_uncompiled_shader
*so
)
2495 const struct vc4_key
*key
= entry
->key
;
2497 if (key
->shader_state
== so
) {
2498 struct vc4_compiled_shader
*shader
= entry
->data
;
2499 _mesa_hash_table_remove(ht
, entry
);
2500 vc4_bo_unreference(&shader
->bo
);
2501 ralloc_free(shader
);
2506 vc4_shader_state_delete(struct pipe_context
*pctx
, void *hwcso
)
2508 struct vc4_context
*vc4
= vc4_context(pctx
);
2509 struct vc4_uncompiled_shader
*so
= hwcso
;
2511 struct hash_entry
*entry
;
2512 hash_table_foreach(vc4
->fs_cache
, entry
)
2513 delete_from_cache_if_matches(vc4
->fs_cache
, entry
, so
);
2514 hash_table_foreach(vc4
->vs_cache
, entry
)
2515 delete_from_cache_if_matches(vc4
->vs_cache
, entry
, so
);
2517 ralloc_free(so
->base
.ir
.nir
);
2522 vc4_fp_state_bind(struct pipe_context
*pctx
, void *hwcso
)
2524 struct vc4_context
*vc4
= vc4_context(pctx
);
2525 vc4
->prog
.bind_fs
= hwcso
;
2526 vc4
->dirty
|= VC4_DIRTY_UNCOMPILED_FS
;
2530 vc4_vp_state_bind(struct pipe_context
*pctx
, void *hwcso
)
2532 struct vc4_context
*vc4
= vc4_context(pctx
);
2533 vc4
->prog
.bind_vs
= hwcso
;
2534 vc4
->dirty
|= VC4_DIRTY_UNCOMPILED_VS
;
2538 vc4_program_init(struct pipe_context
*pctx
)
2540 struct vc4_context
*vc4
= vc4_context(pctx
);
2542 pctx
->create_vs_state
= vc4_shader_state_create
;
2543 pctx
->delete_vs_state
= vc4_shader_state_delete
;
2545 pctx
->create_fs_state
= vc4_shader_state_create
;
2546 pctx
->delete_fs_state
= vc4_shader_state_delete
;
2548 pctx
->bind_fs_state
= vc4_fp_state_bind
;
2549 pctx
->bind_vs_state
= vc4_vp_state_bind
;
2551 vc4
->fs_cache
= _mesa_hash_table_create(pctx
, fs_cache_hash
,
2553 vc4
->vs_cache
= _mesa_hash_table_create(pctx
, vs_cache_hash
,
2558 vc4_program_fini(struct pipe_context
*pctx
)
2560 struct vc4_context
*vc4
= vc4_context(pctx
);
2562 struct hash_entry
*entry
;
2563 hash_table_foreach(vc4
->fs_cache
, entry
) {
2564 struct vc4_compiled_shader
*shader
= entry
->data
;
2565 vc4_bo_unreference(&shader
->bo
);
2566 ralloc_free(shader
);
2567 _mesa_hash_table_remove(vc4
->fs_cache
, entry
);
2570 hash_table_foreach(vc4
->vs_cache
, entry
) {
2571 struct vc4_compiled_shader
*shader
= entry
->data
;
2572 vc4_bo_unreference(&shader
->bo
);
2573 ralloc_free(shader
);
2574 _mesa_hash_table_remove(vc4
->vs_cache
, entry
);