xref: /aosp_15_r20/external/mesa3d/src/panfrost/compiler/bi_opt_dual_tex.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 "bi_builder.h"
25 #include "compiler.h"
26 
27 #define XXH_INLINE_ALL
28 #include "util/xxhash.h"
29 
30 /* Fuse pairs of TEXS_2D instructions into a single dual texture TEXC, if both
31  * sample at the same coordinate with the default LOD mode for the shader stage
32  * (computed LOD in fragment shaders, else zero LOD) and immediate
33  * texture/sampler indices 0...3.
34  *
35  * Fusing across basic block boundaries is not expected to be useful, as it
36  * increases register pressure and causes redundant memory traffic. As such, we
37  * use a local optimization pass.
38  *
39  * To pair ops efficiently, we maintain a set (backed by a hash table) using
40  * only the coordinate sources for hashing and equality. Hence the pass runs in
41  * O(n) worst case expected time for n insturctions in a block. We reject
42  * invalid texture instructions quickly to reduce the constant factor.
43  *
44  * Dual texture instructions have skip flags, like normal texture instructions.
45  * Adding a skip flag to an instruction that doesn't have it is illegal, but
46  * removing a skip flag from one that has it is legal. Accordingly, set the
47  * fused TEXC's skip to the logical AND of the unfused TEXS flags. We run the
48  * optimization pass to run after bi_analyze_helper_requirements.
49  */
50 
51 static inline bool
bi_can_fuse_dual_tex(bi_instr * I,bool fuse_zero_lod)52 bi_can_fuse_dual_tex(bi_instr *I, bool fuse_zero_lod)
53 {
54    return (I->op == BI_OPCODE_TEXS_2D_F32 || I->op == BI_OPCODE_TEXS_2D_F16) &&
55           (I->texture_index < 4 && I->sampler_index < 4) &&
56           (I->lod_mode == fuse_zero_lod);
57 }
58 
59 static enum bifrost_texture_format
bi_format_for_texs_2d(enum bi_opcode op)60 bi_format_for_texs_2d(enum bi_opcode op)
61 {
62    switch (op) {
63    case BI_OPCODE_TEXS_2D_F32:
64       return BIFROST_TEXTURE_FORMAT_F32;
65    case BI_OPCODE_TEXS_2D_F16:
66       return BIFROST_TEXTURE_FORMAT_F16;
67    default:
68       unreachable("Invalid TEXS_2D instruction");
69    }
70 }
71 
72 static void
bi_fuse_dual(bi_context * ctx,bi_instr * I1,bi_instr * I2)73 bi_fuse_dual(bi_context *ctx, bi_instr *I1, bi_instr *I2)
74 {
75    /* Construct a texture operation descriptor for the dual texture */
76    struct bifrost_dual_texture_operation desc = {
77       .mode = BIFROST_TEXTURE_OPERATION_DUAL,
78 
79       .primary_texture_index = I1->texture_index,
80       .primary_sampler_index = I1->sampler_index,
81       .primary_format = bi_format_for_texs_2d(I1->op),
82       .primary_mask = 0xF,
83 
84       .secondary_texture_index = I2->texture_index,
85       .secondary_sampler_index = I2->sampler_index,
86       .secondary_format = bi_format_for_texs_2d(I2->op),
87       .secondary_mask = 0xF,
88    };
89 
90    /* LOD mode is implied in a shader stage */
91    assert(I1->lod_mode == I2->lod_mode);
92 
93    /* Insert before the earlier instruction in case its result is consumed
94     * before the later instruction
95     */
96    bi_builder b = bi_init_builder(ctx, bi_before_instr(I1));
97 
98    bi_instr *I = bi_texc_dual_to(
99       &b, I1->dest[0], I2->dest[0], bi_null(), /* staging */
100       I1->src[0], I1->src[1],                  /* coordinates */
101       bi_imm_u32(bi_dual_tex_as_u32(desc)), I1->lod_mode,
102       bi_count_write_registers(I1, 0), bi_count_write_registers(I2, 0));
103 
104    I->skip = I1->skip && I2->skip;
105 
106    bi_remove_instruction(I1);
107    bi_remove_instruction(I2);
108 }
109 
110 #define HASH(hash, data) XXH32(&(data), sizeof(data), hash)
111 
112 static uint32_t
coord_hash(const void * key)113 coord_hash(const void *key)
114 {
115    const bi_instr *I = key;
116 
117    return XXH32(&I->src[0], sizeof(I->src[0]) + sizeof(I->src[1]), 0);
118 }
119 
120 static bool
coord_equal(const void * key1,const void * key2)121 coord_equal(const void *key1, const void *key2)
122 {
123    const bi_instr *I = key1;
124    const bi_instr *J = key2;
125 
126    return memcmp(&I->src[0], &J->src[0],
127                  sizeof(I->src[0]) + sizeof(I->src[1])) == 0;
128 }
129 
130 static void
bi_opt_fuse_dual_texture_block(bi_context * ctx,bi_block * block)131 bi_opt_fuse_dual_texture_block(bi_context *ctx, bi_block *block)
132 {
133    struct set *set = _mesa_set_create(ctx, coord_hash, coord_equal);
134    bool fuse_zero_lod = (ctx->stage != MESA_SHADER_FRAGMENT);
135    bool found = false;
136 
137    bi_foreach_instr_in_block_safe(block, I) {
138       if (!bi_can_fuse_dual_tex(I, fuse_zero_lod))
139          continue;
140 
141       struct set_entry *ent = _mesa_set_search_or_add(set, I, &found);
142 
143       if (found) {
144          bi_fuse_dual(ctx, (bi_instr *)ent->key, I);
145          _mesa_set_remove(set, ent);
146       }
147    }
148 }
149 
150 void
bi_opt_fuse_dual_texture(bi_context * ctx)151 bi_opt_fuse_dual_texture(bi_context *ctx)
152 {
153    bi_foreach_block(ctx, block) {
154       bi_opt_fuse_dual_texture_block(ctx, block);
155    }
156 }
157