1 /*
2 * Copyright (C) 2021 Collabora, Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include "compiler.h"
25 #include "bi_builder.h"
26
27 /* Bifrost v7 can preload up to two messages of the form:
28 *
29 * 1. +LD_VAR_IMM, register_format f32/f16, sample mode
30 * 2. +VAR_TEX, register format f32/f16, sample mode (TODO)
31 *
32 * Analyze the shader for these instructions and push accordingly.
33 */
34
35 static bool
bi_is_regfmt_float(enum bi_register_format regfmt)36 bi_is_regfmt_float(enum bi_register_format regfmt)
37 {
38 return (regfmt == BI_REGISTER_FORMAT_F32) ||
39 (regfmt == BI_REGISTER_FORMAT_F16);
40 }
41
42 /*
43 * Preloaded varyings are interpolated at the sample location. Check if an
44 * instruction can use this interpolation mode.
45 */
46 static bool
bi_can_interp_at_sample(bi_instr * I)47 bi_can_interp_at_sample(bi_instr *I)
48 {
49 /* .sample mode with r61 corresponds to per-sample interpolation */
50 if (I->sample == BI_SAMPLE_SAMPLE)
51 return bi_is_value_equiv(I->src[0], bi_register(61));
52
53 /* If the shader runs with pixel-frequency shading, .sample is
54 * equivalent to .center, so allow .center
55 *
56 * If the shader runs with sample-frequency shading, .sample and .center
57 * are not equivalent. However, the ESSL 3.20 specification
58 * stipulates in section 4.5 ("Interpolation Qualifiers"):
59 *
60 * for fragment shader input variables qualified with neither
61 * centroid nor sample, the value of the assigned variable may be
62 * interpolated anywhere within the pixel and a single value may be
63 * assigned to each sample within the pixel, to the extent permitted
64 * by the OpenGL ES Specification.
65 *
66 * We only produce .center for variables qualified with neither centroid
67 * nor sample, so if .center is specified this section applies. This
68 * suggests that, although per-pixel interpolation is allowed, it is not
69 * mandated ("may" rather than "must" or "should"). Therefore it appears
70 * safe to substitute sample.
71 */
72 return (I->sample == BI_SAMPLE_CENTER);
73 }
74
75 static bool
bi_can_preload_ld_var(bi_instr * I)76 bi_can_preload_ld_var(bi_instr *I)
77 {
78 return (I->op == BI_OPCODE_LD_VAR_IMM) &&
79 bi_can_interp_at_sample(I) &&
80 bi_is_regfmt_float(I->register_format);
81 }
82
83 static bool
bi_is_var_tex(enum bi_opcode op)84 bi_is_var_tex(enum bi_opcode op)
85 {
86 return (op == BI_OPCODE_VAR_TEX_F32) || (op == BI_OPCODE_VAR_TEX_F16);
87 }
88
89 void
bi_opt_message_preload(bi_context * ctx)90 bi_opt_message_preload(bi_context *ctx)
91 {
92 unsigned nr_preload = 0;
93
94 /* We only preload from the first block */
95 bi_block *block = bi_start_block(&ctx->blocks);
96 bi_builder b = bi_init_builder(ctx, bi_before_nonempty_block(block));
97
98 bi_foreach_instr_in_block_safe(block, I) {
99 if (!bi_is_ssa(I->dest[0])) continue;
100
101 struct bifrost_message_preload msg;
102
103 if (bi_can_preload_ld_var(I)) {
104 msg = (struct bifrost_message_preload) {
105 .enabled = true,
106 .varying_index = I->varying_index,
107 .fp16 = (I->register_format == BI_REGISTER_FORMAT_F16),
108 .num_components = I->vecsize + 1
109 };
110 } else if (bi_is_var_tex(I->op)) {
111 msg = (struct bifrost_message_preload) {
112 .enabled = true,
113 .texture = true,
114 .varying_index = I->varying_index,
115 .texture_index = I->texture_index,
116 .fp16 = (I->op == BI_OPCODE_VAR_TEX_F16),
117 .skip = I->skip,
118 .zero_lod = I->lod_mode
119 };
120 } else {
121 continue;
122 }
123
124 /* Report the preloading */
125 ctx->info.bifrost->messages[nr_preload] = msg;
126
127 /* Replace with a collect of preloaded registers. The collect
128 * kills the moves, so the collect is free (it is coalesced).
129 */
130 b.cursor = bi_before_instr(I);
131
132 bi_instr *collect = bi_collect_i32_to(&b, I->dest[0]);
133 collect->nr_srcs = bi_count_write_registers(I, 0);
134
135 /* The registers themselves must be preloaded at the start of
136 * the program. Preloaded registers are coalesced, so these
137 * moves are free.
138 */
139 b.cursor = bi_before_block(block);
140 for (unsigned i = 0; i < collect->nr_srcs; ++i) {
141 unsigned reg = (nr_preload * 4) + i;
142
143 collect->src[i] = bi_mov_i32(&b, bi_register(reg));
144 }
145
146 bi_remove_instruction(I);
147
148 /* Maximum number of preloaded messages */
149 if ((++nr_preload) == 2)
150 break;
151 }
152 }
153