2
* Copyright (C) 2021 Collabora, Ltd.
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:
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
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
25
#include "bi_builder.h"
27
/* Bifrost v7 can preload up to two messages of the form:
29
* 1. +LD_VAR_IMM, register_format f32/f16, sample mode
30
* 2. +VAR_TEX, register format f32/f16, sample mode (TODO)
32
* Analyze the shader for these instructions and push accordingly.
36
bi_is_regfmt_float(enum bi_register_format regfmt)
38
return (regfmt == BI_REGISTER_FORMAT_F32) ||
39
(regfmt == BI_REGISTER_FORMAT_F16);
43
* Preloaded varyings are interpolated at the sample location. Check if an
44
* instruction can use this interpolation mode.
47
bi_can_interp_at_sample(bi_instr *I)
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));
53
/* If the shader runs with pixel-frequency shading, .sample is
54
* equivalent to .center, so allow .center
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"):
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.
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.
72
return (I->sample == BI_SAMPLE_CENTER);
76
bi_can_preload_ld_var(bi_instr *I)
78
return (I->op == BI_OPCODE_LD_VAR_IMM) &&
79
bi_can_interp_at_sample(I) &&
80
bi_is_regfmt_float(I->register_format);
84
bi_is_var_tex(enum bi_opcode op)
86
return (op == BI_OPCODE_VAR_TEX_F32) || (op == BI_OPCODE_VAR_TEX_F16);
90
bi_opt_message_preload(bi_context *ctx)
92
unsigned nr_preload = 0;
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));
98
bi_foreach_instr_in_block_safe(block, I) {
99
if (!bi_is_ssa(I->dest[0])) continue;
101
struct bifrost_message_preload msg;
103
if (bi_can_preload_ld_var(I)) {
104
msg = (struct bifrost_message_preload) {
106
.varying_index = I->varying_index,
107
.fp16 = (I->register_format == BI_REGISTER_FORMAT_F16),
108
.num_components = I->vecsize + 1
110
} else if (bi_is_var_tex(I->op)) {
111
msg = (struct bifrost_message_preload) {
114
.varying_index = I->varying_index,
115
.texture_index = I->texture_index,
116
.fp16 = (I->op == BI_OPCODE_VAR_TEX_F16),
118
.zero_lod = I->lod_mode
124
/* Report the preloading */
125
ctx->info.bifrost->messages[nr_preload] = msg;
127
/* Replace with moves at the start. Ideally, they will be
128
* coalesced out or copy propagated.
130
for (unsigned i = 0; i < bi_count_write_registers(I, 0); ++i) {
131
bi_mov_i32_to(&b, bi_word(I->dest[0], i),
132
bi_register((nr_preload * 4) + i));
135
bi_remove_instruction(I);
137
/* Maximum number of preloaded messages */
138
if ((++nr_preload) == 2)