• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define XXH_INLINE_ALL
28 #include "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: return BIFROST_TEXTURE_FORMAT_F32;
64         case BI_OPCODE_TEXS_2D_F16: return BIFROST_TEXTURE_FORMAT_F16;
65         default:                    unreachable("Invalid TEXS_2D instruction");
66         }
67 }
68 
69 static void
bi_fuse_dual(bi_context * ctx,bi_instr * I1,bi_instr * I2)70 bi_fuse_dual(bi_context *ctx, bi_instr *I1, bi_instr *I2)
71 {
72         /* Construct a texture operation descriptor for the dual texture */
73         struct bifrost_dual_texture_operation desc = {
74                 .mode = BIFROST_TEXTURE_OPERATION_DUAL,
75 
76                 .primary_texture_index = I1->texture_index,
77                 .primary_sampler_index = I1->sampler_index,
78                 .primary_format = bi_format_for_texs_2d(I1->op),
79                 .primary_mask = 0xF,
80 
81                 .secondary_texture_index = I2->texture_index,
82                 .secondary_sampler_index = I2->sampler_index,
83                 .secondary_format = bi_format_for_texs_2d(I2->op),
84                 .secondary_mask = 0xF,
85         };
86 
87         /* LOD mode is implied in a shader stage */
88         assert(I1->lod_mode == I2->lod_mode);
89 
90         /* Insert before the earlier instruction in case its result is consumed
91          * before the later instruction
92          */
93         bi_builder b = bi_init_builder(ctx, bi_before_instr(I1));
94 
95         bi_instr *I = bi_texc_to(&b,
96                         I1->dest[0], I2->dest[0], bi_null(), /* staging */
97                         I1->src[0], I1->src[1], /* coordinates */
98                         bi_imm_u32(bi_dual_tex_as_u32(desc)), I1->lod_mode,
99                         bi_count_write_registers(I1, 0),
100                         bi_count_write_registers(I2, 0));
101 
102         I->skip = I1->skip && I2->skip;
103 
104         bi_remove_instruction(I1);
105         bi_remove_instruction(I2);
106 }
107 
108 #define HASH(hash, data) XXH32(&(data), sizeof(data), hash)
109 
110 static uint32_t
coord_hash(const void * key)111 coord_hash(const void *key)
112 {
113         const bi_instr *I = key;
114 
115         return XXH32(&I->src[0], sizeof(I->src[0]) + sizeof(I->src[1]), 0);
116 }
117 
118 static bool
coord_equal(const void * key1,const void * key2)119 coord_equal(const void *key1, const void *key2)
120 {
121         const bi_instr *I = key1;
122         const bi_instr *J = key2;
123 
124         return memcmp(&I->src[0], &J->src[0],
125                       sizeof(I->src[0]) + sizeof(I->src[1])) == 0;
126 }
127 
128 static void
bi_opt_fuse_dual_texture_block(bi_context * ctx,bi_block * block)129 bi_opt_fuse_dual_texture_block(bi_context *ctx, bi_block *block)
130 {
131         struct set *set = _mesa_set_create(ctx, coord_hash, coord_equal);
132         bool fuse_zero_lod = (ctx->stage != MESA_SHADER_FRAGMENT);
133         bool found = false;
134 
135         bi_foreach_instr_in_block_safe(block, I) {
136                 if (!bi_can_fuse_dual_tex(I, fuse_zero_lod)) continue;
137 
138                 struct set_entry *ent = _mesa_set_search_or_add(set, I, &found);
139 
140                 if (found) {
141                         bi_fuse_dual(ctx, (bi_instr *) ent->key, I);
142                         _mesa_set_remove(set, ent);
143                 }
144         }
145 }
146 
147 void
bi_opt_fuse_dual_texture(bi_context * ctx)148 bi_opt_fuse_dual_texture(bi_context *ctx)
149 {
150         bi_foreach_block(ctx, block) {
151                 bi_opt_fuse_dual_texture_block(ctx, block);
152         }
153 
154 }
155