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 "bi_test.h"
26 #include "compiler.h"
27
28 #include <gtest/gtest.h>
29
30 #define CASE(shader_stage, instr, expected) \
31 do { \
32 bi_builder *A = bit_builder(mem_ctx); \
33 bi_builder *B = bit_builder(mem_ctx); \
34 { \
35 bi_builder *b = A; \
36 bi_index u = bi_temp(b->shader); \
37 bi_index v = bi_temp(b->shader); \
38 A->shader->stage = MESA_SHADER_##shader_stage; \
39 instr; \
40 } \
41 { \
42 bi_builder *b = B; \
43 bi_index u = bi_temp(b->shader); \
44 bi_index v = bi_temp(b->shader); \
45 B->shader->stage = MESA_SHADER_##shader_stage; \
46 expected; \
47 } \
48 bi_opt_fuse_dual_texture(A->shader); \
49 if (!bit_shader_equal(A->shader, B->shader)) { \
50 ADD_FAILURE(); \
51 fprintf(stderr, "Optimization produce unexpected result"); \
52 fprintf(stderr, " Actual:\n"); \
53 bi_print_shader(A->shader, stderr); \
54 fprintf(stderr, "Expected:\n"); \
55 bi_print_shader(B->shader, stderr); \
56 fprintf(stderr, "\n"); \
57 } \
58 } while (0)
59
60 #define NEGCASE(stage, instr) CASE(stage, instr, instr)
61
62 class DualTexture : public testing::Test {
63 protected:
DualTexture()64 DualTexture()
65 {
66 mem_ctx = ralloc_context(NULL);
67
68 reg = bi_register(0);
69 x = bi_register(4);
70 y = bi_register(8);
71 }
72
~DualTexture()73 ~DualTexture()
74 {
75 ralloc_free(mem_ctx);
76 }
77
78 void *mem_ctx;
79
80 bi_index reg, x, y;
81 };
82
TEST_F(DualTexture,FuseDualTexFragment)83 TEST_F(DualTexture, FuseDualTexFragment)
84 {
85 CASE(
86 FRAGMENT,
87 {
88 bi_texs_2d_f32_to(b, x, u, v, false, 0, 0);
89 bi_texs_2d_f32_to(b, y, u, v, false, 1, 1);
90 },
91 {
92 bi_texc_dual_to(b, x, y, bi_null(), u, v, bi_imm_u32(0xF9F00144),
93 false, 4, 4);
94 });
95 }
96
TEST_F(DualTexture,FuseDualTexKernel)97 TEST_F(DualTexture, FuseDualTexKernel)
98 {
99 CASE(
100 KERNEL,
101 {
102 bi_texs_2d_f32_to(b, x, u, v, true, 0, 0);
103 bi_texs_2d_f32_to(b, y, u, v, true, 1, 1);
104 },
105 {
106 bi_texc_dual_to(b, x, y, bi_null(), u, v, bi_imm_u32(0xF9F00144), true,
107 4, 4);
108 });
109 }
110
TEST_F(DualTexture,FuseDualTexVertex)111 TEST_F(DualTexture, FuseDualTexVertex)
112 {
113 CASE(
114 VERTEX,
115 {
116 bi_texs_2d_f32_to(b, x, u, v, true, 0, 0);
117 bi_texs_2d_f32_to(b, y, u, v, true, 1, 1);
118 },
119 {
120 bi_texc_dual_to(b, x, y, bi_null(), u, v, bi_imm_u32(0xF9F00144), true,
121 4, 4);
122 });
123 }
124
TEST_F(DualTexture,DontFuseDualTexWrongStage)125 TEST_F(DualTexture, DontFuseDualTexWrongStage)
126 {
127 NEGCASE(FRAGMENT, {
128 bi_texs_2d_f32_to(b, x, u, v, true, 0, 0);
129 bi_texs_2d_f32_to(b, y, u, v, true, 1, 1);
130 });
131
132 NEGCASE(KERNEL, {
133 bi_texs_2d_f32_to(b, x, u, v, false, 0, 0);
134 bi_texs_2d_f32_to(b, y, u, v, false, 1, 1);
135 });
136
137 NEGCASE(VERTEX, {
138 bi_texs_2d_f32_to(b, x, u, v, false, 0, 0);
139 bi_texs_2d_f32_to(b, y, u, v, false, 1, 1);
140 });
141 }
142
TEST_F(DualTexture,FuseDualTexMaximumIndex)143 TEST_F(DualTexture, FuseDualTexMaximumIndex)
144 {
145 CASE(
146 FRAGMENT,
147 {
148 bi_texs_2d_f32_to(b, x, u, v, false, 2, 2);
149 bi_texs_2d_f32_to(b, y, u, v, false, 3, 3);
150 },
151 {
152 bi_texc_dual_to(b, x, y, bi_null(), u, v, bi_imm_u32(0xF9F003E6),
153 false, 4, 4);
154 });
155 }
156
TEST_F(DualTexture,FuseDualTexMixedIndex)157 TEST_F(DualTexture, FuseDualTexMixedIndex)
158 {
159 CASE(
160 FRAGMENT,
161 {
162 bi_texs_2d_f32_to(b, x, u, v, false, 3, 2);
163 bi_texs_2d_f32_to(b, y, u, v, false, 2, 3);
164 },
165 {
166 bi_texc_dual_to(b, x, y, bi_null(), u, v, bi_imm_u32(0xF9F003A7),
167 false, 4, 4);
168 });
169 }
170
TEST_F(DualTexture,DontFuseDualTexOutOfBounds)171 TEST_F(DualTexture, DontFuseDualTexOutOfBounds)
172 {
173 NEGCASE(FRAGMENT, {
174 bi_texs_2d_f32_to(b, x, u, v, false, 4, 0);
175 bi_texs_2d_f32_to(b, y, u, v, false, 1, 1);
176 });
177
178 NEGCASE(FRAGMENT, {
179 bi_texs_2d_f32_to(b, x, u, v, false, 0, 4);
180 bi_texs_2d_f32_to(b, y, u, v, false, 1, 1);
181 });
182
183 NEGCASE(FRAGMENT, {
184 bi_texs_2d_f32_to(b, x, u, v, false, 0, 0);
185 bi_texs_2d_f32_to(b, y, u, v, false, 4, 1);
186 });
187
188 NEGCASE(FRAGMENT, {
189 bi_texs_2d_f32_to(b, x, u, v, false, 0, 0);
190 bi_texs_2d_f32_to(b, y, u, v, false, 1, 4);
191 });
192 }
193
TEST_F(DualTexture,FuseDualTexFP16)194 TEST_F(DualTexture, FuseDualTexFP16)
195 {
196 CASE(
197 FRAGMENT,
198 {
199 bi_texs_2d_f16_to(b, x, u, v, false, 0, 0);
200 bi_texs_2d_f16_to(b, y, u, v, false, 1, 1);
201 },
202 {
203 bi_texc_dual_to(b, x, y, bi_null(), u, v, bi_imm_u32(0xF1E00144),
204 false, 2, 2);
205 });
206 }
207
TEST_F(DualTexture,FuseDualTexMixedSize)208 TEST_F(DualTexture, FuseDualTexMixedSize)
209 {
210 CASE(
211 FRAGMENT,
212 {
213 bi_texs_2d_f32_to(b, x, u, v, false, 0, 0);
214 bi_texs_2d_f16_to(b, y, u, v, false, 1, 1);
215 },
216 {
217 bi_texc_dual_to(b, x, y, bi_null(), u, v, bi_imm_u32(0XF9E00144),
218 false, 4, 2);
219 });
220
221 CASE(
222 FRAGMENT,
223 {
224 bi_texs_2d_f16_to(b, x, u, v, false, 0, 0);
225 bi_texs_2d_f32_to(b, y, u, v, false, 1, 1);
226 },
227 {
228 bi_texc_dual_to(b, x, y, bi_null(), u, v, bi_imm_u32(0xF1F00144),
229 false, 2, 4);
230 });
231 }
232
TEST_F(DualTexture,DontFuseMixedCoordinates)233 TEST_F(DualTexture, DontFuseMixedCoordinates)
234 {
235 NEGCASE(FRAGMENT, {
236 bi_texs_2d_f32_to(b, x, bi_neg(u), v, false, 0, 0);
237 bi_texs_2d_f32_to(b, y, u, v, false, 1, 1);
238 });
239
240 NEGCASE(FRAGMENT, {
241 bi_texs_2d_f32_to(b, x, u, v, false, 0, 0);
242 bi_texs_2d_f32_to(b, y, v, u, false, 1, 1);
243 });
244 }
245