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_info.h"
34 #include "tgsi/tgsi_lowering.h"
35 #include "tgsi/tgsi_parse.h"
36 #include "glsl/nir/nir.h"
37 #include "glsl/nir/nir_builder.h"
38 #include "nir/tgsi_to_nir.h"
39 #include "vc4_context.h"
42 #ifdef USE_VC4_SIMULATOR
43 #include "simpenrose/simpenrose.h"
47 ntq_get_src(struct vc4_compile
*c
, nir_src src
, int i
);
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
;
120 intr
->num_components
= 1;
121 nir_ssa_dest_init(&intr
->instr
, &intr
->dest
, 1, NULL
);
122 nir_builder_instr_insert(b
, &intr
->instr
);
123 return &intr
->dest
.ssa
;
127 vc4_nir_get_swizzled_channel(nir_builder
*b
, nir_ssa_def
**srcs
, int swiz
)
131 case UTIL_FORMAT_SWIZZLE_NONE
:
132 fprintf(stderr
, "warning: unknown swizzle\n");
134 case UTIL_FORMAT_SWIZZLE_0
:
135 return nir_imm_float(b
, 0.0);
136 case UTIL_FORMAT_SWIZZLE_1
:
137 return nir_imm_float(b
, 1.0);
138 case UTIL_FORMAT_SWIZZLE_X
:
139 case UTIL_FORMAT_SWIZZLE_Y
:
140 case UTIL_FORMAT_SWIZZLE_Z
:
141 case UTIL_FORMAT_SWIZZLE_W
:
147 ntq_init_ssa_def(struct vc4_compile
*c
, nir_ssa_def
*def
)
149 struct qreg
*qregs
= ralloc_array(c
->def_ht
, struct qreg
,
150 def
->num_components
);
151 _mesa_hash_table_insert(c
->def_ht
, def
, qregs
);
156 ntq_get_dest(struct vc4_compile
*c
, nir_dest
*dest
)
159 struct qreg
*qregs
= ntq_init_ssa_def(c
, &dest
->ssa
);
160 for (int i
= 0; i
< dest
->ssa
.num_components
; i
++)
164 nir_register
*reg
= dest
->reg
.reg
;
165 assert(dest
->reg
.base_offset
== 0);
166 assert(reg
->num_array_elems
== 0);
167 struct hash_entry
*entry
=
168 _mesa_hash_table_search(c
->def_ht
, reg
);
174 ntq_get_src(struct vc4_compile
*c
, nir_src src
, int i
)
176 struct hash_entry
*entry
;
178 entry
= _mesa_hash_table_search(c
->def_ht
, src
.ssa
);
179 assert(i
< src
.ssa
->num_components
);
181 nir_register
*reg
= src
.reg
.reg
;
182 entry
= _mesa_hash_table_search(c
->def_ht
, reg
);
183 assert(reg
->num_array_elems
== 0);
184 assert(src
.reg
.base_offset
== 0);
185 assert(i
< reg
->num_components
);
188 struct qreg
*qregs
= entry
->data
;
193 ntq_get_alu_src(struct vc4_compile
*c
, nir_alu_instr
*instr
,
196 assert(util_is_power_of_two(instr
->dest
.write_mask
));
197 unsigned chan
= ffs(instr
->dest
.write_mask
) - 1;
198 struct qreg r
= ntq_get_src(c
, instr
->src
[src
].src
,
199 instr
->src
[src
].swizzle
[chan
]);
201 assert(!instr
->src
[src
].abs
);
202 assert(!instr
->src
[src
].negate
);
208 get_swizzled_channel(struct vc4_compile
*c
,
209 struct qreg
*srcs
, int swiz
)
213 case UTIL_FORMAT_SWIZZLE_NONE
:
214 fprintf(stderr
, "warning: unknown swizzle\n");
216 case UTIL_FORMAT_SWIZZLE_0
:
217 return qir_uniform_f(c
, 0.0);
218 case UTIL_FORMAT_SWIZZLE_1
:
219 return qir_uniform_f(c
, 1.0);
220 case UTIL_FORMAT_SWIZZLE_X
:
221 case UTIL_FORMAT_SWIZZLE_Y
:
222 case UTIL_FORMAT_SWIZZLE_Z
:
223 case UTIL_FORMAT_SWIZZLE_W
:
228 static inline struct qreg
229 qir_SAT(struct vc4_compile
*c
, struct qreg val
)
232 qir_FMIN(c
, val
, qir_uniform_f(c
, 1.0)),
233 qir_uniform_f(c
, 0.0));
237 ntq_rcp(struct vc4_compile
*c
, struct qreg x
)
239 struct qreg r
= qir_RCP(c
, x
);
241 /* Apply a Newton-Raphson step to improve the accuracy. */
242 r
= qir_FMUL(c
, r
, qir_FSUB(c
,
243 qir_uniform_f(c
, 2.0),
250 ntq_rsq(struct vc4_compile
*c
, struct qreg x
)
252 struct qreg r
= qir_RSQ(c
, x
);
254 /* Apply a Newton-Raphson step to improve the accuracy. */
255 r
= qir_FMUL(c
, r
, qir_FSUB(c
,
256 qir_uniform_f(c
, 1.5),
258 qir_uniform_f(c
, 0.5),
260 qir_FMUL(c
, r
, r
)))));
266 qir_srgb_decode(struct vc4_compile
*c
, struct qreg srgb
)
268 struct qreg low
= qir_FMUL(c
, srgb
, qir_uniform_f(c
, 1.0 / 12.92));
269 struct qreg high
= qir_POW(c
,
273 qir_uniform_f(c
, 0.055)),
274 qir_uniform_f(c
, 1.0 / 1.055)),
275 qir_uniform_f(c
, 2.4));
277 qir_SF(c
, qir_FSUB(c
, srgb
, qir_uniform_f(c
, 0.04045)));
278 return qir_SEL_X_Y_NS(c
, low
, high
);
282 ntq_umul(struct vc4_compile
*c
, struct qreg src0
, struct qreg src1
)
284 struct qreg src0_hi
= qir_SHR(c
, src0
,
285 qir_uniform_ui(c
, 24));
286 struct qreg src1_hi
= qir_SHR(c
, src1
,
287 qir_uniform_ui(c
, 24));
289 struct qreg hilo
= qir_MUL24(c
, src0_hi
, src1
);
290 struct qreg lohi
= qir_MUL24(c
, src0
, src1_hi
);
291 struct qreg lolo
= qir_MUL24(c
, src0
, src1
);
293 return qir_ADD(c
, lolo
, qir_SHL(c
,
294 qir_ADD(c
, hilo
, lohi
),
295 qir_uniform_ui(c
, 24)));
299 ntq_emit_tex(struct vc4_compile
*c
, nir_tex_instr
*instr
)
301 struct qreg s
, t
, r
, lod
, proj
, compare
;
302 bool is_txb
= false, is_txl
= false, has_proj
= false;
303 unsigned unit
= instr
->sampler_index
;
305 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
306 switch (instr
->src
[i
].src_type
) {
307 case nir_tex_src_coord
:
308 s
= ntq_get_src(c
, instr
->src
[i
].src
, 0);
309 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_1D
)
310 t
= qir_uniform_f(c
, 0.5);
312 t
= ntq_get_src(c
, instr
->src
[i
].src
, 1);
313 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
)
314 r
= ntq_get_src(c
, instr
->src
[i
].src
, 2);
316 case nir_tex_src_bias
:
317 lod
= ntq_get_src(c
, instr
->src
[i
].src
, 0);
320 case nir_tex_src_lod
:
321 lod
= ntq_get_src(c
, instr
->src
[i
].src
, 0);
324 case nir_tex_src_comparitor
:
325 compare
= ntq_get_src(c
, instr
->src
[i
].src
, 0);
327 case nir_tex_src_projector
:
328 proj
= qir_RCP(c
, ntq_get_src(c
, instr
->src
[i
].src
, 0));
329 s
= qir_FMUL(c
, s
, proj
);
330 t
= qir_FMUL(c
, t
, proj
);
334 unreachable("unknown texture source");
338 struct qreg texture_u
[] = {
339 qir_uniform(c
, QUNIFORM_TEXTURE_CONFIG_P0
, unit
),
340 qir_uniform(c
, QUNIFORM_TEXTURE_CONFIG_P1
, unit
),
341 qir_uniform(c
, QUNIFORM_CONSTANT
, 0),
342 qir_uniform(c
, QUNIFORM_CONSTANT
, 0),
344 uint32_t next_texture_u
= 0;
346 /* There is no native support for GL texture rectangle coordinates, so
347 * we have to rescale from ([0, width], [0, height]) to ([0, 1], [0,
350 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_RECT
) {
352 qir_uniform(c
, QUNIFORM_TEXRECT_SCALE_X
, unit
));
354 qir_uniform(c
, QUNIFORM_TEXRECT_SCALE_Y
, unit
));
357 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
|| is_txl
) {
358 texture_u
[2] = qir_uniform(c
, QUNIFORM_TEXTURE_CONFIG_P2
,
359 unit
| (is_txl
<< 16));
362 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
363 struct qreg ma
= qir_FMAXABS(c
, qir_FMAXABS(c
, s
, t
), r
);
364 struct qreg rcp_ma
= qir_RCP(c
, ma
);
365 s
= qir_FMUL(c
, s
, rcp_ma
);
366 t
= qir_FMUL(c
, t
, rcp_ma
);
367 r
= qir_FMUL(c
, r
, rcp_ma
);
369 qir_TEX_R(c
, r
, texture_u
[next_texture_u
++]);
370 } else if (c
->key
->tex
[unit
].wrap_s
== PIPE_TEX_WRAP_CLAMP_TO_BORDER
||
371 c
->key
->tex
[unit
].wrap_s
== PIPE_TEX_WRAP_CLAMP
||
372 c
->key
->tex
[unit
].wrap_t
== PIPE_TEX_WRAP_CLAMP_TO_BORDER
||
373 c
->key
->tex
[unit
].wrap_t
== PIPE_TEX_WRAP_CLAMP
) {
374 qir_TEX_R(c
, qir_uniform(c
, QUNIFORM_TEXTURE_BORDER_COLOR
, unit
),
375 texture_u
[next_texture_u
++]);
378 if (c
->key
->tex
[unit
].wrap_s
== PIPE_TEX_WRAP_CLAMP
) {
382 if (c
->key
->tex
[unit
].wrap_t
== PIPE_TEX_WRAP_CLAMP
) {
386 qir_TEX_T(c
, t
, texture_u
[next_texture_u
++]);
388 if (is_txl
|| is_txb
)
389 qir_TEX_B(c
, lod
, texture_u
[next_texture_u
++]);
391 qir_TEX_S(c
, s
, texture_u
[next_texture_u
++]);
393 c
->num_texture_samples
++;
394 struct qreg tex
= qir_TEX_RESULT(c
);
396 enum pipe_format format
= c
->key
->tex
[unit
].format
;
398 struct qreg unpacked
[4];
399 if (util_format_is_depth_or_stencil(format
)) {
400 struct qreg depthf
= qir_ITOF(c
, qir_SHR(c
, tex
,
401 qir_uniform_ui(c
, 8)));
402 struct qreg normalized
= qir_FMUL(c
, depthf
,
403 qir_uniform_f(c
, 1.0f
/0xffffff));
405 struct qreg depth_output
;
407 struct qreg one
= qir_uniform_f(c
, 1.0f
);
408 if (c
->key
->tex
[unit
].compare_mode
) {
410 compare
= qir_FMUL(c
, compare
, proj
);
412 switch (c
->key
->tex
[unit
].compare_func
) {
413 case PIPE_FUNC_NEVER
:
414 depth_output
= qir_uniform_f(c
, 0.0f
);
416 case PIPE_FUNC_ALWAYS
:
419 case PIPE_FUNC_EQUAL
:
420 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
421 depth_output
= qir_SEL_X_0_ZS(c
, one
);
423 case PIPE_FUNC_NOTEQUAL
:
424 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
425 depth_output
= qir_SEL_X_0_ZC(c
, one
);
427 case PIPE_FUNC_GREATER
:
428 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
429 depth_output
= qir_SEL_X_0_NC(c
, one
);
431 case PIPE_FUNC_GEQUAL
:
432 qir_SF(c
, qir_FSUB(c
, normalized
, compare
));
433 depth_output
= qir_SEL_X_0_NS(c
, one
);
436 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
437 depth_output
= qir_SEL_X_0_NS(c
, one
);
439 case PIPE_FUNC_LEQUAL
:
440 qir_SF(c
, qir_FSUB(c
, normalized
, compare
));
441 depth_output
= qir_SEL_X_0_NC(c
, one
);
445 depth_output
= normalized
;
448 for (int i
= 0; i
< 4; i
++)
449 unpacked
[i
] = depth_output
;
451 for (int i
= 0; i
< 4; i
++)
452 unpacked
[i
] = qir_UNPACK_8_F(c
, tex
, i
);
455 const uint8_t *format_swiz
= vc4_get_format_swizzle(format
);
456 struct qreg texture_output
[4];
457 for (int i
= 0; i
< 4; i
++) {
458 texture_output
[i
] = get_swizzled_channel(c
, unpacked
,
462 if (util_format_is_srgb(format
)) {
463 for (int i
= 0; i
< 3; i
++)
464 texture_output
[i
] = qir_srgb_decode(c
,
468 struct qreg
*dest
= ntq_get_dest(c
, &instr
->dest
);
469 for (int i
= 0; i
< 4; i
++) {
470 dest
[i
] = get_swizzled_channel(c
, texture_output
,
471 c
->key
->tex
[unit
].swizzle
[i
]);
476 * Computes x - floor(x), which is tricky because our FTOI truncates (rounds
480 ntq_ffract(struct vc4_compile
*c
, struct qreg src
)
482 struct qreg trunc
= qir_ITOF(c
, qir_FTOI(c
, src
));
483 struct qreg diff
= qir_FSUB(c
, src
, trunc
);
485 return qir_SEL_X_Y_NS(c
,
486 qir_FADD(c
, diff
, qir_uniform_f(c
, 1.0)),
491 * Computes floor(x), which is tricky because our FTOI truncates (rounds to
495 ntq_ffloor(struct vc4_compile
*c
, struct qreg src
)
497 struct qreg trunc
= qir_ITOF(c
, qir_FTOI(c
, src
));
499 /* This will be < 0 if we truncated and the truncation was of a value
500 * that was < 0 in the first place.
502 qir_SF(c
, qir_FSUB(c
, src
, trunc
));
504 return qir_SEL_X_Y_NS(c
,
505 qir_FSUB(c
, trunc
, qir_uniform_f(c
, 1.0)),
510 * Computes ceil(x), which is tricky because our FTOI truncates (rounds to
514 ntq_fceil(struct vc4_compile
*c
, struct qreg src
)
516 struct qreg trunc
= qir_ITOF(c
, qir_FTOI(c
, src
));
518 /* This will be < 0 if we truncated and the truncation was of a value
519 * that was > 0 in the first place.
521 qir_SF(c
, qir_FSUB(c
, trunc
, src
));
523 return qir_SEL_X_Y_NS(c
,
524 qir_FADD(c
, trunc
, qir_uniform_f(c
, 1.0)),
529 ntq_fsin(struct vc4_compile
*c
, struct qreg src
)
533 pow(2.0 * M_PI
, 3) / (3 * 2 * 1),
534 -pow(2.0 * M_PI
, 5) / (5 * 4 * 3 * 2 * 1),
535 pow(2.0 * M_PI
, 7) / (7 * 6 * 5 * 4 * 3 * 2 * 1),
536 -pow(2.0 * M_PI
, 9) / (9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1),
539 struct qreg scaled_x
=
542 qir_uniform_f(c
, 1.0 / (M_PI
* 2.0)));
544 struct qreg x
= qir_FADD(c
,
545 ntq_ffract(c
, scaled_x
),
546 qir_uniform_f(c
, -0.5));
547 struct qreg x2
= qir_FMUL(c
, x
, x
);
548 struct qreg sum
= qir_FMUL(c
, x
, qir_uniform_f(c
, coeff
[0]));
549 for (int i
= 1; i
< ARRAY_SIZE(coeff
); i
++) {
550 x
= qir_FMUL(c
, x
, x2
);
555 qir_uniform_f(c
, coeff
[i
])));
561 ntq_fcos(struct vc4_compile
*c
, struct qreg src
)
565 pow(2.0 * M_PI
, 2) / (2 * 1),
566 -pow(2.0 * M_PI
, 4) / (4 * 3 * 2 * 1),
567 pow(2.0 * M_PI
, 6) / (6 * 5 * 4 * 3 * 2 * 1),
568 -pow(2.0 * M_PI
, 8) / (8 * 7 * 6 * 5 * 4 * 3 * 2 * 1),
569 pow(2.0 * M_PI
, 10) / (10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1),
572 struct qreg scaled_x
=
574 qir_uniform_f(c
, 1.0f
/ (M_PI
* 2.0f
)));
575 struct qreg x_frac
= qir_FADD(c
,
576 ntq_ffract(c
, scaled_x
),
577 qir_uniform_f(c
, -0.5));
579 struct qreg sum
= qir_uniform_f(c
, coeff
[0]);
580 struct qreg x2
= qir_FMUL(c
, x_frac
, x_frac
);
581 struct qreg x
= x2
; /* Current x^2, x^4, or x^6 */
582 for (int i
= 1; i
< ARRAY_SIZE(coeff
); i
++) {
584 x
= qir_FMUL(c
, x
, x2
);
586 struct qreg mul
= qir_FMUL(c
,
588 qir_uniform_f(c
, coeff
[i
]));
592 sum
= qir_FADD(c
, sum
, mul
);
598 ntq_fsign(struct vc4_compile
*c
, struct qreg src
)
601 return qir_SEL_X_Y_NC(c
,
602 qir_SEL_X_0_ZC(c
, qir_uniform_f(c
, 1.0)),
603 qir_uniform_f(c
, -1.0));
607 get_channel_from_vpm(struct vc4_compile
*c
,
608 struct qreg
*vpm_reads
,
610 const struct util_format_description
*desc
)
612 const struct util_format_channel_description
*chan
=
613 &desc
->channel
[swiz
];
616 if (swiz
> UTIL_FORMAT_SWIZZLE_W
)
617 return get_swizzled_channel(c
, vpm_reads
, swiz
);
618 else if (chan
->size
== 32 &&
619 chan
->type
== UTIL_FORMAT_TYPE_FLOAT
) {
620 return get_swizzled_channel(c
, vpm_reads
, swiz
);
621 } else if (chan
->size
== 32 &&
622 chan
->type
== UTIL_FORMAT_TYPE_SIGNED
) {
623 if (chan
->normalized
) {
625 qir_ITOF(c
, vpm_reads
[swiz
]),
629 return qir_ITOF(c
, vpm_reads
[swiz
]);
631 } else if (chan
->size
== 8 &&
632 (chan
->type
== UTIL_FORMAT_TYPE_UNSIGNED
||
633 chan
->type
== UTIL_FORMAT_TYPE_SIGNED
)) {
634 struct qreg vpm
= vpm_reads
[0];
635 if (chan
->type
== UTIL_FORMAT_TYPE_SIGNED
) {
636 temp
= qir_XOR(c
, vpm
, qir_uniform_ui(c
, 0x80808080));
637 if (chan
->normalized
) {
638 return qir_FSUB(c
, qir_FMUL(c
,
639 qir_UNPACK_8_F(c
, temp
, swiz
),
640 qir_uniform_f(c
, 2.0)),
641 qir_uniform_f(c
, 1.0));
645 qir_UNPACK_8_I(c
, temp
,
647 qir_uniform_f(c
, -128.0));
650 if (chan
->normalized
) {
651 return qir_UNPACK_8_F(c
, vpm
, swiz
);
653 return qir_ITOF(c
, qir_UNPACK_8_I(c
, vpm
, swiz
));
656 } else if (chan
->size
== 16 &&
657 (chan
->type
== UTIL_FORMAT_TYPE_UNSIGNED
||
658 chan
->type
== UTIL_FORMAT_TYPE_SIGNED
)) {
659 struct qreg vpm
= vpm_reads
[swiz
/ 2];
661 /* Note that UNPACK_16F eats a half float, not ints, so we use
662 * UNPACK_16_I for all of these.
664 if (chan
->type
== UTIL_FORMAT_TYPE_SIGNED
) {
665 temp
= qir_ITOF(c
, qir_UNPACK_16_I(c
, vpm
, swiz
% 2));
666 if (chan
->normalized
) {
667 return qir_FMUL(c
, temp
,
668 qir_uniform_f(c
, 1/32768.0f
));
673 /* UNPACK_16I sign-extends, so we have to emit ANDs. */
675 if (swiz
== 1 || swiz
== 3)
676 temp
= qir_UNPACK_16_I(c
, temp
, 1);
677 temp
= qir_AND(c
, temp
, qir_uniform_ui(c
, 0xffff));
678 temp
= qir_ITOF(c
, temp
);
680 if (chan
->normalized
) {
681 return qir_FMUL(c
, temp
,
682 qir_uniform_f(c
, 1 / 65535.0));
693 emit_vertex_input(struct vc4_compile
*c
, int attr
)
695 enum pipe_format format
= c
->vs_key
->attr_formats
[attr
];
696 uint32_t attr_size
= util_format_get_blocksize(format
);
697 struct qreg vpm_reads
[4];
699 c
->vattr_sizes
[attr
] = align(attr_size
, 4);
700 for (int i
= 0; i
< align(attr_size
, 4) / 4; i
++) {
701 struct qreg vpm
= { QFILE_VPM
, attr
* 4 + i
};
702 vpm_reads
[i
] = qir_MOV(c
, vpm
);
706 bool format_warned
= false;
707 const struct util_format_description
*desc
=
708 util_format_description(format
);
710 for (int i
= 0; i
< 4; i
++) {
711 uint8_t swiz
= desc
->swizzle
[i
];
712 struct qreg result
= get_channel_from_vpm(c
, vpm_reads
,
715 if (result
.file
== QFILE_NULL
) {
716 if (!format_warned
) {
718 "vtx element %d unsupported type: %s\n",
719 attr
, util_format_name(format
));
720 format_warned
= true;
722 result
= qir_uniform_f(c
, 0.0);
724 c
->inputs
[attr
* 4 + i
] = result
;
729 emit_fragcoord_input(struct vc4_compile
*c
, int attr
)
731 c
->inputs
[attr
* 4 + 0] = qir_FRAG_X(c
);
732 c
->inputs
[attr
* 4 + 1] = qir_FRAG_Y(c
);
733 c
->inputs
[attr
* 4 + 2] =
735 qir_ITOF(c
, qir_FRAG_Z(c
)),
736 qir_uniform_f(c
, 1.0 / 0xffffff));
737 c
->inputs
[attr
* 4 + 3] = qir_RCP(c
, qir_FRAG_W(c
));
741 emit_fragment_varying(struct vc4_compile
*c
, uint8_t semantic
,
742 uint8_t index
, uint8_t swizzle
)
744 uint32_t i
= c
->num_input_semantics
++;
750 if (c
->num_input_semantics
>= c
->input_semantics_array_size
) {
751 c
->input_semantics_array_size
=
752 MAX2(4, c
->input_semantics_array_size
* 2);
754 c
->input_semantics
= reralloc(c
, c
->input_semantics
,
755 struct vc4_varying_semantic
,
756 c
->input_semantics_array_size
);
759 c
->input_semantics
[i
].semantic
= semantic
;
760 c
->input_semantics
[i
].index
= index
;
761 c
->input_semantics
[i
].swizzle
= swizzle
;
763 return qir_VARY_ADD_C(c
, qir_FMUL(c
, vary
, qir_FRAG_W(c
)));
767 emit_fragment_input(struct vc4_compile
*c
, int attr
,
768 unsigned semantic_name
, unsigned semantic_index
)
770 for (int i
= 0; i
< 4; i
++) {
771 c
->inputs
[attr
* 4 + i
] =
772 emit_fragment_varying(c
,
781 add_output(struct vc4_compile
*c
,
782 uint32_t decl_offset
,
783 uint8_t semantic_name
,
784 uint8_t semantic_index
,
785 uint8_t semantic_swizzle
)
787 uint32_t old_array_size
= c
->outputs_array_size
;
788 resize_qreg_array(c
, &c
->outputs
, &c
->outputs_array_size
,
791 if (old_array_size
!= c
->outputs_array_size
) {
792 c
->output_semantics
= reralloc(c
,
794 struct vc4_varying_semantic
,
795 c
->outputs_array_size
);
798 c
->output_semantics
[decl_offset
].semantic
= semantic_name
;
799 c
->output_semantics
[decl_offset
].index
= semantic_index
;
800 c
->output_semantics
[decl_offset
].swizzle
= semantic_swizzle
;
804 declare_uniform_range(struct vc4_compile
*c
, uint32_t start
, uint32_t size
)
806 unsigned array_id
= c
->num_uniform_ranges
++;
807 if (array_id
>= c
->ubo_ranges_array_size
) {
808 c
->ubo_ranges_array_size
= MAX2(c
->ubo_ranges_array_size
* 2,
810 c
->ubo_ranges
= reralloc(c
, c
->ubo_ranges
,
811 struct vc4_compiler_ubo_range
,
812 c
->ubo_ranges_array_size
);
815 c
->ubo_ranges
[array_id
].dst_offset
= 0;
816 c
->ubo_ranges
[array_id
].src_offset
= start
;
817 c
->ubo_ranges
[array_id
].size
= size
;
818 c
->ubo_ranges
[array_id
].used
= false;
822 ntq_emit_alu(struct vc4_compile
*c
, nir_alu_instr
*instr
)
824 /* Vectors are special in that they have non-scalarized writemasks,
825 * and just take the first swizzle channel for each argument in order
826 * into each writemask channel.
828 if (instr
->op
== nir_op_vec2
||
829 instr
->op
== nir_op_vec3
||
830 instr
->op
== nir_op_vec4
) {
832 for (int i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
833 srcs
[i
] = ntq_get_src(c
, instr
->src
[i
].src
,
834 instr
->src
[i
].swizzle
[0]);
835 struct qreg
*dest
= ntq_get_dest(c
, &instr
->dest
.dest
);
836 for (int i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
841 if (instr
->op
== nir_op_pack_unorm_4x8
) {
843 for (int i
= 0; i
< 4; i
++) {
844 struct qreg src
= ntq_get_src(c
, instr
->src
[0].src
,
845 instr
->src
[0].swizzle
[i
]);
847 result
= qir_PACK_8888_F(c
, src
);
849 result
= qir_PACK_8_F(c
, result
, src
, i
);
851 struct qreg
*dest
= ntq_get_dest(c
, &instr
->dest
.dest
);
856 if (instr
->op
== nir_op_unpack_unorm_4x8
) {
857 struct qreg src
= ntq_get_src(c
, instr
->src
[0].src
,
858 instr
->src
[0].swizzle
[0]);
859 struct qreg
*dest
= ntq_get_dest(c
, &instr
->dest
.dest
);
860 for (int i
= 0; i
< 4; i
++) {
861 if (instr
->dest
.write_mask
& (1 << i
))
862 dest
[i
] = qir_UNPACK_8_F(c
, src
, i
);
867 /* General case: We can just grab the one used channel per src. */
868 struct qreg src
[nir_op_infos
[instr
->op
].num_inputs
];
869 for (int i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
870 src
[i
] = ntq_get_alu_src(c
, instr
, i
);
873 /* Pick the channel to store the output in. */
874 assert(!instr
->dest
.saturate
);
875 struct qreg
*dest
= ntq_get_dest(c
, &instr
->dest
.dest
);
876 assert(util_is_power_of_two(instr
->dest
.write_mask
));
877 dest
+= ffs(instr
->dest
.write_mask
) - 1;
882 *dest
= qir_MOV(c
, src
[0]);
885 *dest
= qir_FMUL(c
, src
[0], src
[1]);
888 *dest
= qir_FADD(c
, src
[0], src
[1]);
891 *dest
= qir_FSUB(c
, src
[0], src
[1]);
894 *dest
= qir_FMIN(c
, src
[0], src
[1]);
897 *dest
= qir_FMAX(c
, src
[0], src
[1]);
902 *dest
= qir_FTOI(c
, src
[0]);
906 *dest
= qir_ITOF(c
, src
[0]);
909 *dest
= qir_AND(c
, src
[0], qir_uniform_f(c
, 1.0));
912 *dest
= qir_AND(c
, src
[0], qir_uniform_ui(c
, 1));
917 *dest
= qir_SEL_X_0_ZC(c
, qir_uniform_ui(c
, ~0));
921 *dest
= qir_ADD(c
, src
[0], src
[1]);
924 *dest
= qir_SHR(c
, src
[0], src
[1]);
927 *dest
= qir_SUB(c
, src
[0], src
[1]);
930 *dest
= qir_ASR(c
, src
[0], src
[1]);
933 *dest
= qir_SHL(c
, src
[0], src
[1]);
936 *dest
= qir_MIN(c
, src
[0], src
[1]);
939 *dest
= qir_MAX(c
, src
[0], src
[1]);
942 *dest
= qir_AND(c
, src
[0], src
[1]);
945 *dest
= qir_OR(c
, src
[0], src
[1]);
948 *dest
= qir_XOR(c
, src
[0], src
[1]);
951 *dest
= qir_NOT(c
, src
[0]);
955 *dest
= ntq_umul(c
, src
[0], src
[1]);
959 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
960 *dest
= qir_SEL_X_0_ZS(c
, qir_uniform_f(c
, 1.0));
963 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
964 *dest
= qir_SEL_X_0_ZC(c
, qir_uniform_f(c
, 1.0));
967 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
968 *dest
= qir_SEL_X_0_NC(c
, qir_uniform_f(c
, 1.0));
971 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
972 *dest
= qir_SEL_X_0_NS(c
, qir_uniform_f(c
, 1.0));
975 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
976 *dest
= qir_SEL_X_0_ZS(c
, qir_uniform_ui(c
, ~0));
979 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
980 *dest
= qir_SEL_X_0_ZC(c
, qir_uniform_ui(c
, ~0));
983 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
984 *dest
= qir_SEL_X_0_NC(c
, qir_uniform_ui(c
, ~0));
987 qir_SF(c
, qir_FSUB(c
, src
[0], src
[1]));
988 *dest
= qir_SEL_X_0_NS(c
, qir_uniform_ui(c
, ~0));
991 qir_SF(c
, qir_SUB(c
, src
[0], src
[1]));
992 *dest
= qir_SEL_X_0_ZS(c
, qir_uniform_ui(c
, ~0));
995 qir_SF(c
, qir_SUB(c
, src
[0], src
[1]));
996 *dest
= qir_SEL_X_0_ZC(c
, qir_uniform_ui(c
, ~0));
999 qir_SF(c
, qir_SUB(c
, src
[0], src
[1]));
1000 *dest
= qir_SEL_X_0_NC(c
, qir_uniform_ui(c
, ~0));
1003 qir_SF(c
, qir_SUB(c
, src
[0], src
[1]));
1004 *dest
= qir_SEL_X_0_NS(c
, qir_uniform_ui(c
, ~0));
1009 *dest
= qir_SEL_X_Y_NS(c
, src
[1], src
[2]);
1013 *dest
= qir_SEL_X_Y_ZC(c
, src
[1], src
[2]);
1017 *dest
= ntq_rcp(c
, src
[0]);
1020 *dest
= ntq_rsq(c
, src
[0]);
1023 *dest
= qir_EXP2(c
, src
[0]);
1026 *dest
= qir_LOG2(c
, src
[0]);
1030 *dest
= qir_ITOF(c
, qir_FTOI(c
, src
[0]));
1033 *dest
= ntq_fceil(c
, src
[0]);
1036 *dest
= ntq_ffract(c
, src
[0]);
1039 *dest
= ntq_ffloor(c
, src
[0]);
1043 *dest
= ntq_fsin(c
, src
[0]);
1046 *dest
= ntq_fcos(c
, src
[0]);
1050 *dest
= ntq_fsign(c
, src
[0]);
1054 *dest
= qir_FMAXABS(c
, src
[0], src
[0]);
1057 *dest
= qir_MAX(c
, src
[0],
1058 qir_SUB(c
, qir_uniform_ui(c
, 0), src
[0]));
1062 fprintf(stderr
, "unknown NIR ALU inst: ");
1063 nir_print_instr(&instr
->instr
, stderr
);
1064 fprintf(stderr
, "\n");
1070 clip_distance_discard(struct vc4_compile
*c
)
1072 for (int i
= 0; i
< PIPE_MAX_CLIP_PLANES
; i
++) {
1073 if (!(c
->key
->ucp_enables
& (1 << i
)))
1076 struct qreg dist
= emit_fragment_varying(c
,
1077 TGSI_SEMANTIC_CLIPDIST
,
1083 if (c
->discard
.file
== QFILE_NULL
)
1084 c
->discard
= qir_uniform_ui(c
, 0);
1086 c
->discard
= qir_SEL_X_Y_NS(c
, qir_uniform_ui(c
, ~0),
1092 emit_frag_end(struct vc4_compile
*c
)
1094 clip_distance_discard(c
);
1097 if (c
->output_color_index
!= -1) {
1098 color
= c
->outputs
[c
->output_color_index
];
1100 color
= qir_uniform_ui(c
, 0);
1103 if (c
->discard
.file
!= QFILE_NULL
)
1104 qir_TLB_DISCARD_SETUP(c
, c
->discard
);
1106 if (c
->fs_key
->stencil_enabled
) {
1107 qir_TLB_STENCIL_SETUP(c
, qir_uniform(c
, QUNIFORM_STENCIL
, 0));
1108 if (c
->fs_key
->stencil_twoside
) {
1109 qir_TLB_STENCIL_SETUP(c
, qir_uniform(c
, QUNIFORM_STENCIL
, 1));
1111 if (c
->fs_key
->stencil_full_writemasks
) {
1112 qir_TLB_STENCIL_SETUP(c
, qir_uniform(c
, QUNIFORM_STENCIL
, 2));
1116 if (c
->fs_key
->depth_enabled
) {
1118 if (c
->output_position_index
!= -1) {
1119 z
= qir_FTOI(c
, qir_FMUL(c
, c
->outputs
[c
->output_position_index
+ 2],
1120 qir_uniform_f(c
, 0xffffff)));
1124 qir_TLB_Z_WRITE(c
, z
);
1127 qir_TLB_COLOR_WRITE(c
, color
);
1131 emit_scaled_viewport_write(struct vc4_compile
*c
, struct qreg rcp_w
)
1135 for (int i
= 0; i
< 2; i
++) {
1137 qir_uniform(c
, QUNIFORM_VIEWPORT_X_SCALE
+ i
, 0);
1139 xyi
[i
] = qir_FTOI(c
, qir_FMUL(c
,
1141 c
->outputs
[c
->output_position_index
+ i
],
1146 qir_VPM_WRITE(c
, qir_PACK_SCALED(c
, xyi
[0], xyi
[1]));
1150 emit_zs_write(struct vc4_compile
*c
, struct qreg rcp_w
)
1152 struct qreg zscale
= qir_uniform(c
, QUNIFORM_VIEWPORT_Z_SCALE
, 0);
1153 struct qreg zoffset
= qir_uniform(c
, QUNIFORM_VIEWPORT_Z_OFFSET
, 0);
1155 qir_VPM_WRITE(c
, qir_FADD(c
, qir_FMUL(c
, qir_FMUL(c
,
1156 c
->outputs
[c
->output_position_index
+ 2],
1163 emit_rcp_wc_write(struct vc4_compile
*c
, struct qreg rcp_w
)
1165 qir_VPM_WRITE(c
, rcp_w
);
1169 emit_point_size_write(struct vc4_compile
*c
)
1171 struct qreg point_size
;
1173 if (c
->output_point_size_index
!= -1)
1174 point_size
= c
->outputs
[c
->output_point_size_index
+ 3];
1176 point_size
= qir_uniform_f(c
, 1.0);
1178 /* Workaround: HW-2726 PTB does not handle zero-size points (BCM2835,
1181 point_size
= qir_FMAX(c
, point_size
, qir_uniform_f(c
, .125));
1183 qir_VPM_WRITE(c
, point_size
);
1187 * Emits a VPM read of the stub vertex attribute set up by vc4_draw.c.
1189 * The simulator insists that there be at least one vertex attribute, so
1190 * vc4_draw.c will emit one if it wouldn't have otherwise. The simulator also
1191 * insists that all vertex attributes loaded get read by the VS/CS, so we have
1192 * to consume it here.
1195 emit_stub_vpm_read(struct vc4_compile
*c
)
1200 c
->vattr_sizes
[0] = 4;
1201 struct qreg vpm
= { QFILE_VPM
, 0 };
1202 (void)qir_MOV(c
, vpm
);
1207 emit_ucp_clipdistance(struct vc4_compile
*c
)
1210 if (c
->output_clipvertex_index
!= -1)
1211 cv
= c
->output_clipvertex_index
;
1212 else if (c
->output_position_index
!= -1)
1213 cv
= c
->output_position_index
;
1217 for (int plane
= 0; plane
< PIPE_MAX_CLIP_PLANES
; plane
++) {
1218 if (!(c
->key
->ucp_enables
& (1 << plane
)))
1221 /* Pick the next outputs[] that hasn't been written to, since
1222 * there are no other program writes left to be processed at
1223 * this point. If something had been declared but not written
1224 * (like a w component), we'll just smash over the top of it.
1226 uint32_t output_index
= c
->num_outputs
++;
1227 add_output(c
, output_index
,
1228 TGSI_SEMANTIC_CLIPDIST
,
1233 struct qreg dist
= qir_uniform_f(c
, 0.0);
1234 for (int i
= 0; i
< 4; i
++) {
1235 struct qreg pos_chan
= c
->outputs
[cv
+ i
];
1237 qir_uniform(c
, QUNIFORM_USER_CLIP_PLANE
,
1239 dist
= qir_FADD(c
, dist
, qir_FMUL(c
, pos_chan
, ucp
));
1242 c
->outputs
[output_index
] = dist
;
1247 emit_vert_end(struct vc4_compile
*c
,
1248 struct vc4_varying_semantic
*fs_inputs
,
1249 uint32_t num_fs_inputs
)
1251 struct qreg rcp_w
= qir_RCP(c
, c
->outputs
[c
->output_position_index
+ 3]);
1253 emit_stub_vpm_read(c
);
1254 emit_ucp_clipdistance(c
);
1256 emit_scaled_viewport_write(c
, rcp_w
);
1257 emit_zs_write(c
, rcp_w
);
1258 emit_rcp_wc_write(c
, rcp_w
);
1259 if (c
->vs_key
->per_vertex_point_size
)
1260 emit_point_size_write(c
);
1262 for (int i
= 0; i
< num_fs_inputs
; i
++) {
1263 struct vc4_varying_semantic
*input
= &fs_inputs
[i
];
1266 for (j
= 0; j
< c
->num_outputs
; j
++) {
1267 struct vc4_varying_semantic
*output
=
1268 &c
->output_semantics
[j
];
1270 if (input
->semantic
== output
->semantic
&&
1271 input
->index
== output
->index
&&
1272 input
->swizzle
== output
->swizzle
) {
1273 qir_VPM_WRITE(c
, c
->outputs
[j
]);
1277 /* Emit padding if we didn't find a declared VS output for
1280 if (j
== c
->num_outputs
)
1281 qir_VPM_WRITE(c
, qir_uniform_f(c
, 0.0));
1286 emit_coord_end(struct vc4_compile
*c
)
1288 struct qreg rcp_w
= qir_RCP(c
, c
->outputs
[c
->output_position_index
+ 3]);
1290 emit_stub_vpm_read(c
);
1292 for (int i
= 0; i
< 4; i
++)
1293 qir_VPM_WRITE(c
, c
->outputs
[c
->output_position_index
+ i
]);
1295 emit_scaled_viewport_write(c
, rcp_w
);
1296 emit_zs_write(c
, rcp_w
);
1297 emit_rcp_wc_write(c
, rcp_w
);
1298 if (c
->vs_key
->per_vertex_point_size
)
1299 emit_point_size_write(c
);
1303 vc4_optimize_nir(struct nir_shader
*s
)
1310 nir_lower_vars_to_ssa(s
);
1311 nir_lower_alu_to_scalar(s
);
1313 progress
= nir_copy_prop(s
) || progress
;
1314 progress
= nir_opt_dce(s
) || progress
;
1315 progress
= nir_opt_cse(s
) || progress
;
1316 progress
= nir_opt_peephole_select(s
) || progress
;
1317 progress
= nir_opt_algebraic(s
) || progress
;
1318 progress
= nir_opt_constant_folding(s
) || progress
;
1319 progress
= nir_opt_undef(s
) || progress
;
1324 driver_location_compare(const void *in_a
, const void *in_b
)
1326 const nir_variable
*const *a
= in_a
;
1327 const nir_variable
*const *b
= in_b
;
1329 return (*a
)->data
.driver_location
- (*b
)->data
.driver_location
;
1333 ntq_setup_inputs(struct vc4_compile
*c
)
1335 unsigned num_entries
= 0;
1336 foreach_list_typed(nir_variable
, var
, node
, &c
->s
->inputs
)
1339 nir_variable
*vars
[num_entries
];
1342 foreach_list_typed(nir_variable
, var
, node
, &c
->s
->inputs
)
1345 /* Sort the variables so that we emit the input setup in
1346 * driver_location order. This is required for VPM reads, whose data
1347 * is fetched into the VPM in driver_location (TGSI register index)
1350 qsort(&vars
, num_entries
, sizeof(*vars
), driver_location_compare
);
1352 for (unsigned i
= 0; i
< num_entries
; i
++) {
1353 nir_variable
*var
= vars
[i
];
1354 unsigned array_len
= MAX2(glsl_get_length(var
->type
), 1);
1355 /* XXX: map loc slots to semantics */
1356 unsigned semantic_name
= var
->data
.location
;
1357 unsigned semantic_index
= var
->data
.index
;
1358 unsigned loc
= var
->data
.driver_location
;
1360 assert(array_len
== 1);
1362 resize_qreg_array(c
, &c
->inputs
, &c
->inputs_array_size
,
1365 if (c
->stage
== QSTAGE_FRAG
) {
1366 if (semantic_name
== TGSI_SEMANTIC_POSITION
) {
1367 emit_fragcoord_input(c
, loc
);
1368 } else if (semantic_name
== TGSI_SEMANTIC_FACE
) {
1369 c
->inputs
[loc
* 4 + 0] = qir_FRAG_REV_FLAG(c
);
1370 } else if (semantic_name
== TGSI_SEMANTIC_GENERIC
&&
1371 (c
->fs_key
->point_sprite_mask
&
1372 (1 << semantic_index
))) {
1373 c
->inputs
[loc
* 4 + 0] = c
->point_x
;
1374 c
->inputs
[loc
* 4 + 1] = c
->point_y
;
1376 emit_fragment_input(c
, loc
,
1381 emit_vertex_input(c
, loc
);
1387 ntq_setup_outputs(struct vc4_compile
*c
)
1389 foreach_list_typed(nir_variable
, var
, node
, &c
->s
->outputs
) {
1390 unsigned array_len
= MAX2(glsl_get_length(var
->type
), 1);
1391 /* XXX: map loc slots to semantics */
1392 unsigned semantic_name
= var
->data
.location
;
1393 unsigned semantic_index
= var
->data
.index
;
1394 unsigned loc
= var
->data
.driver_location
* 4;
1396 assert(array_len
== 1);
1399 /* NIR hack to pass through
1400 * TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS */
1401 if (semantic_name
== TGSI_SEMANTIC_COLOR
&&
1402 semantic_index
== -1)
1405 for (int i
= 0; i
< 4; i
++) {
1413 switch (semantic_name
) {
1414 case TGSI_SEMANTIC_POSITION
:
1415 c
->output_position_index
= loc
;
1417 case TGSI_SEMANTIC_CLIPVERTEX
:
1418 c
->output_clipvertex_index
= loc
;
1420 case TGSI_SEMANTIC_COLOR
:
1421 c
->output_color_index
= loc
;
1423 case TGSI_SEMANTIC_PSIZE
:
1424 c
->output_point_size_index
= loc
;
1432 ntq_setup_uniforms(struct vc4_compile
*c
)
1434 foreach_list_typed(nir_variable
, var
, node
, &c
->s
->uniforms
) {
1435 unsigned array_len
= MAX2(glsl_get_length(var
->type
), 1);
1436 unsigned array_elem_size
= 4 * sizeof(float);
1438 declare_uniform_range(c
, var
->data
.driver_location
* array_elem_size
,
1439 array_len
* array_elem_size
);
1445 * Sets up the mapping from nir_register to struct qreg *.
1447 * Each nir_register gets a struct qreg per 32-bit component being stored.
1450 ntq_setup_registers(struct vc4_compile
*c
, struct exec_list
*list
)
1452 foreach_list_typed(nir_register
, nir_reg
, node
, list
) {
1453 unsigned array_len
= MAX2(nir_reg
->num_array_elems
, 1);
1454 struct qreg
*qregs
= ralloc_array(c
->def_ht
, struct qreg
,
1456 nir_reg
->num_components
);
1458 _mesa_hash_table_insert(c
->def_ht
, nir_reg
, qregs
);
1460 for (int i
= 0; i
< array_len
* nir_reg
->num_components
; i
++)
1461 qregs
[i
] = qir_uniform_ui(c
, 0);
1466 ntq_emit_load_const(struct vc4_compile
*c
, nir_load_const_instr
*instr
)
1468 struct qreg
*qregs
= ntq_init_ssa_def(c
, &instr
->def
);
1469 for (int i
= 0; i
< instr
->def
.num_components
; i
++)
1470 qregs
[i
] = qir_uniform_ui(c
, instr
->value
.u
[i
]);
1472 _mesa_hash_table_insert(c
->def_ht
, &instr
->def
, qregs
);
1476 ntq_emit_ssa_undef(struct vc4_compile
*c
, nir_ssa_undef_instr
*instr
)
1478 struct qreg
*qregs
= ntq_init_ssa_def(c
, &instr
->def
);
1480 /* QIR needs there to be *some* value, so pick 0 (same as for
1481 * ntq_setup_registers().
1483 for (int i
= 0; i
< instr
->def
.num_components
; i
++)
1484 qregs
[i
] = qir_uniform_ui(c
, 0);
1488 ntq_emit_intrinsic(struct vc4_compile
*c
, nir_intrinsic_instr
*instr
)
1490 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[instr
->intrinsic
];
1491 struct qreg
*dest
= NULL
;
1493 if (info
->has_dest
) {
1494 dest
= ntq_get_dest(c
, &instr
->dest
);
1497 switch (instr
->intrinsic
) {
1498 case nir_intrinsic_load_uniform
:
1499 assert(instr
->num_components
== 1);
1500 if (instr
->const_index
[0] < VC4_NIR_STATE_UNIFORM_OFFSET
) {
1501 *dest
= qir_uniform(c
, QUNIFORM_UNIFORM
,
1502 instr
->const_index
[0]);
1504 *dest
= qir_uniform(c
, instr
->const_index
[0] -
1505 VC4_NIR_STATE_UNIFORM_OFFSET
,
1510 case nir_intrinsic_load_uniform_indirect
:
1511 *dest
= indirect_uniform_load(c
, instr
);
1515 case nir_intrinsic_load_input
:
1516 assert(instr
->num_components
== 1);
1517 if (instr
->const_index
[0] == VC4_NIR_TLB_COLOR_READ_INPUT
) {
1518 *dest
= qir_TLB_COLOR_READ(c
);
1520 *dest
= c
->inputs
[instr
->const_index
[0]];
1524 case nir_intrinsic_store_output
:
1525 assert(instr
->num_components
== 1);
1526 c
->outputs
[instr
->const_index
[0]] =
1527 qir_MOV(c
, ntq_get_src(c
, instr
->src
[0], 0));
1528 c
->num_outputs
= MAX2(c
->num_outputs
, instr
->const_index
[0] + 1);
1531 case nir_intrinsic_discard
:
1532 c
->discard
= qir_uniform_ui(c
, ~0);
1535 case nir_intrinsic_discard_if
:
1536 if (c
->discard
.file
== QFILE_NULL
)
1537 c
->discard
= qir_uniform_ui(c
, 0);
1538 c
->discard
= qir_OR(c
, c
->discard
,
1539 ntq_get_src(c
, instr
->src
[0], 0));
1543 fprintf(stderr
, "Unknown intrinsic: ");
1544 nir_print_instr(&instr
->instr
, stderr
);
1545 fprintf(stderr
, "\n");
1551 ntq_emit_if(struct vc4_compile
*c
, nir_if
*if_stmt
)
1553 fprintf(stderr
, "general IF statements not handled.\n");
1557 ntq_emit_instr(struct vc4_compile
*c
, nir_instr
*instr
)
1559 switch (instr
->type
) {
1560 case nir_instr_type_alu
:
1561 ntq_emit_alu(c
, nir_instr_as_alu(instr
));
1564 case nir_instr_type_intrinsic
:
1565 ntq_emit_intrinsic(c
, nir_instr_as_intrinsic(instr
));
1568 case nir_instr_type_load_const
:
1569 ntq_emit_load_const(c
, nir_instr_as_load_const(instr
));
1572 case nir_instr_type_ssa_undef
:
1573 ntq_emit_ssa_undef(c
, nir_instr_as_ssa_undef(instr
));
1576 case nir_instr_type_tex
:
1577 ntq_emit_tex(c
, nir_instr_as_tex(instr
));
1581 fprintf(stderr
, "Unknown NIR instr type: ");
1582 nir_print_instr(instr
, stderr
);
1583 fprintf(stderr
, "\n");
1589 ntq_emit_block(struct vc4_compile
*c
, nir_block
*block
)
1591 nir_foreach_instr(block
, instr
) {
1592 ntq_emit_instr(c
, instr
);
1597 ntq_emit_cf_list(struct vc4_compile
*c
, struct exec_list
*list
)
1599 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
1600 switch (node
->type
) {
1601 /* case nir_cf_node_loop: */
1602 case nir_cf_node_block
:
1603 ntq_emit_block(c
, nir_cf_node_as_block(node
));
1606 case nir_cf_node_if
:
1607 ntq_emit_if(c
, nir_cf_node_as_if(node
));
1617 ntq_emit_impl(struct vc4_compile
*c
, nir_function_impl
*impl
)
1619 ntq_setup_registers(c
, &impl
->registers
);
1620 ntq_emit_cf_list(c
, &impl
->body
);
1624 nir_to_qir(struct vc4_compile
*c
)
1626 ntq_setup_inputs(c
);
1627 ntq_setup_outputs(c
);
1628 ntq_setup_uniforms(c
);
1629 ntq_setup_registers(c
, &c
->s
->registers
);
1631 /* Find the main function and emit the body. */
1632 nir_foreach_overload(c
->s
, overload
) {
1633 assert(strcmp(overload
->function
->name
, "main") == 0);
1634 assert(overload
->impl
);
1635 ntq_emit_impl(c
, overload
->impl
);
1639 static const nir_shader_compiler_options nir_options
= {
1644 .lower_fsqrt
= true,
1645 .lower_negate
= true,
1649 count_nir_instrs_in_block(nir_block
*block
, void *state
)
1651 int *count
= (int *) state
;
1652 nir_foreach_instr(block
, instr
) {
1653 *count
= *count
+ 1;
1659 count_nir_instrs(nir_shader
*nir
)
1662 nir_foreach_overload(nir
, overload
) {
1663 if (!overload
->impl
)
1665 nir_foreach_block(overload
->impl
, count_nir_instrs_in_block
, &count
);
1670 static struct vc4_compile
*
1671 vc4_shader_ntq(struct vc4_context
*vc4
, enum qstage stage
,
1672 struct vc4_key
*key
)
1674 struct vc4_compile
*c
= qir_compile_init();
1677 c
->shader_state
= &key
->shader_state
->base
;
1678 c
->program_id
= key
->shader_state
->program_id
;
1679 c
->variant_id
= key
->shader_state
->compiled_variant_count
++;
1684 c
->fs_key
= (struct vc4_fs_key
*)key
;
1685 if (c
->fs_key
->is_points
) {
1686 c
->point_x
= emit_fragment_varying(c
, ~0, ~0, 0);
1687 c
->point_y
= emit_fragment_varying(c
, ~0, ~0, 0);
1688 } else if (c
->fs_key
->is_lines
) {
1689 c
->line_x
= emit_fragment_varying(c
, ~0, ~0, 0);
1693 c
->vs_key
= (struct vc4_vs_key
*)key
;
1696 c
->vs_key
= (struct vc4_vs_key
*)key
;
1700 const struct tgsi_token
*tokens
= key
->shader_state
->base
.tokens
;
1701 if (c
->fs_key
&& c
->fs_key
->light_twoside
) {
1702 if (!key
->shader_state
->twoside_tokens
) {
1703 const struct tgsi_lowering_config lowering_config
= {
1704 .color_two_side
= true,
1706 struct tgsi_shader_info info
;
1707 key
->shader_state
->twoside_tokens
=
1708 tgsi_transform_lowering(&lowering_config
,
1709 key
->shader_state
->base
.tokens
,
1712 /* If no transformation occurred, then NULL is
1713 * returned and we just use our original tokens.
1715 if (!key
->shader_state
->twoside_tokens
) {
1716 key
->shader_state
->twoside_tokens
=
1717 key
->shader_state
->base
.tokens
;
1720 tokens
= key
->shader_state
->twoside_tokens
;
1723 if (vc4_debug
& VC4_DEBUG_TGSI
) {
1724 fprintf(stderr
, "%s prog %d/%d TGSI:\n",
1725 qir_get_stage_name(c
->stage
),
1726 c
->program_id
, c
->variant_id
);
1727 tgsi_dump(tokens
, 0);
1730 c
->s
= tgsi_to_nir(tokens
, &nir_options
);
1731 nir_opt_global_to_local(c
->s
);
1732 nir_convert_to_ssa(c
->s
);
1733 if (stage
== QSTAGE_FRAG
)
1734 vc4_nir_lower_blend(c
);
1735 vc4_nir_lower_io(c
);
1736 nir_lower_idiv(c
->s
);
1737 nir_lower_load_const_to_scalar(c
->s
);
1739 vc4_optimize_nir(c
->s
);
1741 nir_remove_dead_variables(c
->s
);
1743 nir_convert_from_ssa(c
->s
, true);
1745 if (vc4_debug
& VC4_DEBUG_SHADERDB
) {
1746 fprintf(stderr
, "SHADER-DB: %s prog %d/%d: %d NIR instructions\n",
1747 qir_get_stage_name(c
->stage
),
1748 c
->program_id
, c
->variant_id
,
1749 count_nir_instrs(c
->s
));
1752 if (vc4_debug
& VC4_DEBUG_NIR
) {
1753 fprintf(stderr
, "%s prog %d/%d NIR:\n",
1754 qir_get_stage_name(c
->stage
),
1755 c
->program_id
, c
->variant_id
);
1756 nir_print_shader(c
->s
, stderr
);
1767 vc4
->prog
.fs
->input_semantics
,
1768 vc4
->prog
.fs
->num_inputs
);
1775 if (vc4_debug
& VC4_DEBUG_QIR
) {
1776 fprintf(stderr
, "%s prog %d/%d pre-opt QIR:\n",
1777 qir_get_stage_name(c
->stage
),
1778 c
->program_id
, c
->variant_id
);
1783 qir_lower_uniforms(c
);
1785 if (vc4_debug
& VC4_DEBUG_QIR
) {
1786 fprintf(stderr
, "%s prog %d/%d QIR:\n",
1787 qir_get_stage_name(c
->stage
),
1788 c
->program_id
, c
->variant_id
);
1791 qir_reorder_uniforms(c
);
1792 vc4_generate_code(vc4
, c
);
1794 if (vc4_debug
& VC4_DEBUG_SHADERDB
) {
1795 fprintf(stderr
, "SHADER-DB: %s prog %d/%d: %d instructions\n",
1796 qir_get_stage_name(c
->stage
),
1797 c
->program_id
, c
->variant_id
,
1799 fprintf(stderr
, "SHADER-DB: %s prog %d/%d: %d uniforms\n",
1800 qir_get_stage_name(c
->stage
),
1801 c
->program_id
, c
->variant_id
,
1811 vc4_shader_state_create(struct pipe_context
*pctx
,
1812 const struct pipe_shader_state
*cso
)
1814 struct vc4_context
*vc4
= vc4_context(pctx
);
1815 struct vc4_uncompiled_shader
*so
= CALLOC_STRUCT(vc4_uncompiled_shader
);
1819 so
->base
.tokens
= tgsi_dup_tokens(cso
->tokens
);
1820 so
->program_id
= vc4
->next_uncompiled_program_id
++;
1826 copy_uniform_state_to_shader(struct vc4_compiled_shader
*shader
,
1827 struct vc4_compile
*c
)
1829 int count
= c
->num_uniforms
;
1830 struct vc4_shader_uniform_info
*uinfo
= &shader
->uniforms
;
1832 uinfo
->count
= count
;
1833 uinfo
->data
= ralloc_array(shader
, uint32_t, count
);
1834 memcpy(uinfo
->data
, c
->uniform_data
,
1835 count
* sizeof(*uinfo
->data
));
1836 uinfo
->contents
= ralloc_array(shader
, enum quniform_contents
, count
);
1837 memcpy(uinfo
->contents
, c
->uniform_contents
,
1838 count
* sizeof(*uinfo
->contents
));
1839 uinfo
->num_texture_samples
= c
->num_texture_samples
;
1841 vc4_set_shader_uniform_dirty_flags(shader
);
1844 static struct vc4_compiled_shader
*
1845 vc4_get_compiled_shader(struct vc4_context
*vc4
, enum qstage stage
,
1846 struct vc4_key
*key
)
1848 struct hash_table
*ht
;
1850 if (stage
== QSTAGE_FRAG
) {
1852 key_size
= sizeof(struct vc4_fs_key
);
1855 key_size
= sizeof(struct vc4_vs_key
);
1858 struct vc4_compiled_shader
*shader
;
1859 struct hash_entry
*entry
= _mesa_hash_table_search(ht
, key
);
1863 struct vc4_compile
*c
= vc4_shader_ntq(vc4
, stage
, key
);
1864 shader
= rzalloc(NULL
, struct vc4_compiled_shader
);
1866 shader
->program_id
= vc4
->next_compiled_program_id
++;
1867 if (stage
== QSTAGE_FRAG
) {
1868 bool input_live
[c
->num_input_semantics
];
1870 memset(input_live
, 0, sizeof(input_live
));
1871 list_for_each_entry(struct qinst
, inst
, &c
->instructions
, link
) {
1872 for (int i
= 0; i
< qir_get_op_nsrc(inst
->op
); i
++) {
1873 if (inst
->src
[i
].file
== QFILE_VARY
)
1874 input_live
[inst
->src
[i
].index
] = true;
1878 shader
->input_semantics
= ralloc_array(shader
,
1879 struct vc4_varying_semantic
,
1880 c
->num_input_semantics
);
1882 for (int i
= 0; i
< c
->num_input_semantics
; i
++) {
1883 struct vc4_varying_semantic
*sem
= &c
->input_semantics
[i
];
1888 /* Skip non-VS-output inputs. */
1889 if (sem
->semantic
== (uint8_t)~0)
1892 if (sem
->semantic
== TGSI_SEMANTIC_COLOR
||
1893 sem
->semantic
== TGSI_SEMANTIC_BCOLOR
) {
1894 shader
->color_inputs
|= (1 << shader
->num_inputs
);
1897 shader
->input_semantics
[shader
->num_inputs
] = *sem
;
1898 shader
->num_inputs
++;
1901 shader
->num_inputs
= c
->num_inputs
;
1903 shader
->vattr_offsets
[0] = 0;
1904 for (int i
= 0; i
< 8; i
++) {
1905 shader
->vattr_offsets
[i
+ 1] =
1906 shader
->vattr_offsets
[i
] + c
->vattr_sizes
[i
];
1908 if (c
->vattr_sizes
[i
])
1909 shader
->vattrs_live
|= (1 << i
);
1913 copy_uniform_state_to_shader(shader
, c
);
1914 shader
->bo
= vc4_bo_alloc_shader(vc4
->screen
, c
->qpu_insts
,
1915 c
->qpu_inst_count
* sizeof(uint64_t));
1917 /* Copy the compiler UBO range state to the compiled shader, dropping
1918 * out arrays that were never referenced by an indirect load.
1920 * (Note that QIR dead code elimination of an array access still
1921 * leaves that array alive, though)
1923 if (c
->num_ubo_ranges
) {
1924 shader
->num_ubo_ranges
= c
->num_ubo_ranges
;
1925 shader
->ubo_ranges
= ralloc_array(shader
, struct vc4_ubo_range
,
1928 for (int i
= 0; i
< c
->num_uniform_ranges
; i
++) {
1929 struct vc4_compiler_ubo_range
*range
=
1934 shader
->ubo_ranges
[j
].dst_offset
= range
->dst_offset
;
1935 shader
->ubo_ranges
[j
].src_offset
= range
->src_offset
;
1936 shader
->ubo_ranges
[j
].size
= range
->size
;
1937 shader
->ubo_size
+= c
->ubo_ranges
[i
].size
;
1941 if (shader
->ubo_size
) {
1942 if (vc4_debug
& VC4_DEBUG_SHADERDB
) {
1943 fprintf(stderr
, "SHADER-DB: %s prog %d/%d: %d UBO uniforms\n",
1944 qir_get_stage_name(c
->stage
),
1945 c
->program_id
, c
->variant_id
,
1946 shader
->ubo_size
/ 4);
1950 qir_compile_destroy(c
);
1952 struct vc4_key
*dup_key
;
1953 dup_key
= ralloc_size(shader
, key_size
);
1954 memcpy(dup_key
, key
, key_size
);
1955 _mesa_hash_table_insert(ht
, dup_key
, shader
);
1961 vc4_setup_shared_key(struct vc4_context
*vc4
, struct vc4_key
*key
,
1962 struct vc4_texture_stateobj
*texstate
)
1964 for (int i
= 0; i
< texstate
->num_textures
; i
++) {
1965 struct pipe_sampler_view
*sampler
= texstate
->textures
[i
];
1966 struct pipe_sampler_state
*sampler_state
=
1967 texstate
->samplers
[i
];
1970 key
->tex
[i
].format
= sampler
->format
;
1971 key
->tex
[i
].swizzle
[0] = sampler
->swizzle_r
;
1972 key
->tex
[i
].swizzle
[1] = sampler
->swizzle_g
;
1973 key
->tex
[i
].swizzle
[2] = sampler
->swizzle_b
;
1974 key
->tex
[i
].swizzle
[3] = sampler
->swizzle_a
;
1975 key
->tex
[i
].compare_mode
= sampler_state
->compare_mode
;
1976 key
->tex
[i
].compare_func
= sampler_state
->compare_func
;
1977 key
->tex
[i
].wrap_s
= sampler_state
->wrap_s
;
1978 key
->tex
[i
].wrap_t
= sampler_state
->wrap_t
;
1982 key
->ucp_enables
= vc4
->rasterizer
->base
.clip_plane_enable
;
1986 vc4_update_compiled_fs(struct vc4_context
*vc4
, uint8_t prim_mode
)
1988 struct vc4_fs_key local_key
;
1989 struct vc4_fs_key
*key
= &local_key
;
1991 if (!(vc4
->dirty
& (VC4_DIRTY_PRIM_MODE
|
1993 VC4_DIRTY_FRAMEBUFFER
|
1995 VC4_DIRTY_RASTERIZER
|
1997 VC4_DIRTY_TEXSTATE
|
1998 VC4_DIRTY_UNCOMPILED_FS
))) {
2002 memset(key
, 0, sizeof(*key
));
2003 vc4_setup_shared_key(vc4
, &key
->base
, &vc4
->fragtex
);
2004 key
->base
.shader_state
= vc4
->prog
.bind_fs
;
2005 key
->is_points
= (prim_mode
== PIPE_PRIM_POINTS
);
2006 key
->is_lines
= (prim_mode
>= PIPE_PRIM_LINES
&&
2007 prim_mode
<= PIPE_PRIM_LINE_STRIP
);
2008 key
->blend
= vc4
->blend
->rt
[0];
2009 if (vc4
->blend
->logicop_enable
) {
2010 key
->logicop_func
= vc4
->blend
->logicop_func
;
2012 key
->logicop_func
= PIPE_LOGICOP_COPY
;
2014 if (vc4
->framebuffer
.cbufs
[0])
2015 key
->color_format
= vc4
->framebuffer
.cbufs
[0]->format
;
2017 key
->stencil_enabled
= vc4
->zsa
->stencil_uniforms
[0] != 0;
2018 key
->stencil_twoside
= vc4
->zsa
->stencil_uniforms
[1] != 0;
2019 key
->stencil_full_writemasks
= vc4
->zsa
->stencil_uniforms
[2] != 0;
2020 key
->depth_enabled
= (vc4
->zsa
->base
.depth
.enabled
||
2021 key
->stencil_enabled
);
2022 if (vc4
->zsa
->base
.alpha
.enabled
) {
2023 key
->alpha_test
= true;
2024 key
->alpha_test_func
= vc4
->zsa
->base
.alpha
.func
;
2027 if (key
->is_points
) {
2028 key
->point_sprite_mask
=
2029 vc4
->rasterizer
->base
.sprite_coord_enable
;
2030 key
->point_coord_upper_left
=
2031 (vc4
->rasterizer
->base
.sprite_coord_mode
==
2032 PIPE_SPRITE_COORD_UPPER_LEFT
);
2035 key
->light_twoside
= vc4
->rasterizer
->base
.light_twoside
;
2037 struct vc4_compiled_shader
*old_fs
= vc4
->prog
.fs
;
2038 vc4
->prog
.fs
= vc4_get_compiled_shader(vc4
, QSTAGE_FRAG
, &key
->base
);
2039 if (vc4
->prog
.fs
== old_fs
)
2042 vc4
->dirty
|= VC4_DIRTY_COMPILED_FS
;
2043 if (vc4
->rasterizer
->base
.flatshade
&&
2044 old_fs
&& vc4
->prog
.fs
->color_inputs
!= old_fs
->color_inputs
) {
2045 vc4
->dirty
|= VC4_DIRTY_FLAT_SHADE_FLAGS
;
2050 vc4_update_compiled_vs(struct vc4_context
*vc4
, uint8_t prim_mode
)
2052 struct vc4_vs_key local_key
;
2053 struct vc4_vs_key
*key
= &local_key
;
2055 if (!(vc4
->dirty
& (VC4_DIRTY_PRIM_MODE
|
2056 VC4_DIRTY_RASTERIZER
|
2058 VC4_DIRTY_TEXSTATE
|
2059 VC4_DIRTY_VTXSTATE
|
2060 VC4_DIRTY_UNCOMPILED_VS
|
2061 VC4_DIRTY_COMPILED_FS
))) {
2065 memset(key
, 0, sizeof(*key
));
2066 vc4_setup_shared_key(vc4
, &key
->base
, &vc4
->verttex
);
2067 key
->base
.shader_state
= vc4
->prog
.bind_vs
;
2068 key
->compiled_fs_id
= vc4
->prog
.fs
->program_id
;
2070 for (int i
= 0; i
< ARRAY_SIZE(key
->attr_formats
); i
++)
2071 key
->attr_formats
[i
] = vc4
->vtx
->pipe
[i
].src_format
;
2073 key
->per_vertex_point_size
=
2074 (prim_mode
== PIPE_PRIM_POINTS
&&
2075 vc4
->rasterizer
->base
.point_size_per_vertex
);
2077 struct vc4_compiled_shader
*vs
=
2078 vc4_get_compiled_shader(vc4
, QSTAGE_VERT
, &key
->base
);
2079 if (vs
!= vc4
->prog
.vs
) {
2081 vc4
->dirty
|= VC4_DIRTY_COMPILED_VS
;
2084 key
->is_coord
= true;
2085 struct vc4_compiled_shader
*cs
=
2086 vc4_get_compiled_shader(vc4
, QSTAGE_COORD
, &key
->base
);
2087 if (cs
!= vc4
->prog
.cs
) {
2089 vc4
->dirty
|= VC4_DIRTY_COMPILED_CS
;
2094 vc4_update_compiled_shaders(struct vc4_context
*vc4
, uint8_t prim_mode
)
2096 vc4_update_compiled_fs(vc4
, prim_mode
);
2097 vc4_update_compiled_vs(vc4
, prim_mode
);
2101 fs_cache_hash(const void *key
)
2103 return _mesa_hash_data(key
, sizeof(struct vc4_fs_key
));
2107 vs_cache_hash(const void *key
)
2109 return _mesa_hash_data(key
, sizeof(struct vc4_vs_key
));
2113 fs_cache_compare(const void *key1
, const void *key2
)
2115 return memcmp(key1
, key2
, sizeof(struct vc4_fs_key
)) == 0;
2119 vs_cache_compare(const void *key1
, const void *key2
)
2121 return memcmp(key1
, key2
, sizeof(struct vc4_vs_key
)) == 0;
2125 delete_from_cache_if_matches(struct hash_table
*ht
,
2126 struct hash_entry
*entry
,
2127 struct vc4_uncompiled_shader
*so
)
2129 const struct vc4_key
*key
= entry
->key
;
2131 if (key
->shader_state
== so
) {
2132 struct vc4_compiled_shader
*shader
= entry
->data
;
2133 _mesa_hash_table_remove(ht
, entry
);
2134 vc4_bo_unreference(&shader
->bo
);
2135 ralloc_free(shader
);
2140 vc4_shader_state_delete(struct pipe_context
*pctx
, void *hwcso
)
2142 struct vc4_context
*vc4
= vc4_context(pctx
);
2143 struct vc4_uncompiled_shader
*so
= hwcso
;
2145 struct hash_entry
*entry
;
2146 hash_table_foreach(vc4
->fs_cache
, entry
)
2147 delete_from_cache_if_matches(vc4
->fs_cache
, entry
, so
);
2148 hash_table_foreach(vc4
->vs_cache
, entry
)
2149 delete_from_cache_if_matches(vc4
->vs_cache
, entry
, so
);
2151 if (so
->twoside_tokens
!= so
->base
.tokens
)
2152 free((void *)so
->twoside_tokens
);
2153 free((void *)so
->base
.tokens
);
2158 vc4_fp_state_bind(struct pipe_context
*pctx
, void *hwcso
)
2160 struct vc4_context
*vc4
= vc4_context(pctx
);
2161 vc4
->prog
.bind_fs
= hwcso
;
2162 vc4
->dirty
|= VC4_DIRTY_UNCOMPILED_FS
;
2166 vc4_vp_state_bind(struct pipe_context
*pctx
, void *hwcso
)
2168 struct vc4_context
*vc4
= vc4_context(pctx
);
2169 vc4
->prog
.bind_vs
= hwcso
;
2170 vc4
->dirty
|= VC4_DIRTY_UNCOMPILED_VS
;
2174 vc4_program_init(struct pipe_context
*pctx
)
2176 struct vc4_context
*vc4
= vc4_context(pctx
);
2178 pctx
->create_vs_state
= vc4_shader_state_create
;
2179 pctx
->delete_vs_state
= vc4_shader_state_delete
;
2181 pctx
->create_fs_state
= vc4_shader_state_create
;
2182 pctx
->delete_fs_state
= vc4_shader_state_delete
;
2184 pctx
->bind_fs_state
= vc4_fp_state_bind
;
2185 pctx
->bind_vs_state
= vc4_vp_state_bind
;
2187 vc4
->fs_cache
= _mesa_hash_table_create(pctx
, fs_cache_hash
,
2189 vc4
->vs_cache
= _mesa_hash_table_create(pctx
, vs_cache_hash
,
2194 vc4_program_fini(struct pipe_context
*pctx
)
2196 struct vc4_context
*vc4
= vc4_context(pctx
);
2198 struct hash_entry
*entry
;
2199 hash_table_foreach(vc4
->fs_cache
, entry
) {
2200 struct vc4_compiled_shader
*shader
= entry
->data
;
2201 vc4_bo_unreference(&shader
->bo
);
2202 ralloc_free(shader
);
2203 _mesa_hash_table_remove(vc4
->fs_cache
, entry
);
2206 hash_table_foreach(vc4
->vs_cache
, entry
) {
2207 struct vc4_compiled_shader
*shader
= entry
->data
;
2208 vc4_bo_unreference(&shader
->bo
);
2209 ralloc_free(shader
);
2210 _mesa_hash_table_remove(vc4
->vs_cache
, entry
);