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