2
* Copyright (c) 2020 Etnaviv Project
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, sub license,
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
12
* next paragraph) shall be included in all copies or substantial portions
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 NON-INFRINGEMENT. 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
20
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
* DEALINGS IN THE SOFTWARE.
24
* Christian Gmeiner <christian.gmeiner@gmail.com>
27
#include <gtest/gtest.h>
30
#include "nir_builder.h"
33
/* we really do not want to include etnaviv_nir.h as it makes it
34
* harder to get this test compiling. as we are only working with
35
* nir we do not need any etnaviv specifc stuff here. */
38
etna_nir_lower_ubo_to_uniform(nir_shader *shader);
41
class nir_lower_ubo_test : public ::testing::Test {
44
~nir_lower_ubo_test();
46
nir_intrinsic_instr *intrinsic(nir_intrinsic_op op);
47
unsigned count_intrinsic(nir_intrinsic_op op);
52
nir_lower_ubo_test::nir_lower_ubo_test()
54
glsl_type_singleton_init_or_ref();
56
static const nir_shader_compiler_options options = { };
57
b = nir_builder_init_simple_shader(MESA_SHADER_VERTEX, &options, "ubo lowering tests");
60
nir_lower_ubo_test::~nir_lower_ubo_test()
63
printf("\nShader from the failed test:\n\n");
64
nir_print_shader(b.shader, stdout);
67
ralloc_free(b.shader);
69
glsl_type_singleton_decref();
73
nir_lower_ubo_test::intrinsic(nir_intrinsic_op op)
75
nir_foreach_block(block, b.impl) {
76
nir_foreach_instr(instr, block) {
77
if (instr->type != nir_instr_type_intrinsic)
80
nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
81
if (intr->intrinsic == op)
89
nir_lower_ubo_test::count_intrinsic(nir_intrinsic_op op)
93
nir_foreach_block(block, b.impl) {
94
nir_foreach_instr(instr, block) {
95
if (instr->type != nir_instr_type_intrinsic)
98
nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
99
if (intr->intrinsic == op)
106
TEST_F(nir_lower_ubo_test, nothing_to_lower)
108
nir_ssa_def *offset = nir_imm_int(&b, 4);
110
nir_load_uniform(&b, 1, 32, offset);
112
nir_validate_shader(b.shader, NULL);
114
ASSERT_FALSE(etna_nir_lower_ubo_to_uniform(b.shader));
115
nir_validate_shader(b.shader, NULL);
117
ASSERT_EQ(count_intrinsic(nir_intrinsic_load_ubo), 0);
118
ASSERT_EQ(count_intrinsic(nir_intrinsic_load_uniform), 1);
121
TEST_F(nir_lower_ubo_test, basic)
123
nir_ssa_def *offset = nir_imm_int(&b, 4);
124
nir_load_uniform(&b, 1, 32, offset);
126
nir_lower_uniforms_to_ubo(b.shader, false, false);
127
nir_opt_constant_folding(b.shader);
129
ASSERT_TRUE(etna_nir_lower_ubo_to_uniform(b.shader));
130
nir_validate_shader(b.shader, NULL);
131
nir_opt_constant_folding(b.shader);
133
nir_intrinsic_instr *load_uniform = intrinsic(nir_intrinsic_load_uniform);
134
ASSERT_EQ(nir_src_as_uint(load_uniform->src[0]), 4);
135
ASSERT_EQ(intrinsic(nir_intrinsic_load_ubo), nullptr);
138
TEST_F(nir_lower_ubo_test, index_not_null)
140
nir_ssa_def *index = nir_imm_int(&b, 1);
141
nir_ssa_def *offset = nir_imm_int(&b, 4);
143
nir_load_ubo(&b, 1, 32, index, offset, .align_mul = 16, .align_offset = 0, .range_base = 0, .range = 8);
145
nir_validate_shader(b.shader, NULL);
147
ASSERT_FALSE(etna_nir_lower_ubo_to_uniform(b.shader));
148
ASSERT_EQ(count_intrinsic(nir_intrinsic_load_ubo), 1);
149
ASSERT_EQ(count_intrinsic(nir_intrinsic_load_uniform), 0);
152
TEST_F(nir_lower_ubo_test, indirect_index)
154
nir_ssa_def *one = nir_imm_int(&b, 1);
155
nir_ssa_def *index = nir_fadd(&b, one, one);
156
nir_ssa_def *offset = nir_imm_int(&b, 4);
158
nir_load_ubo(&b, 1, 32, index, offset, .align_mul = 16, .align_offset = 0, .range_base = 0, .range = 8);
160
nir_validate_shader(b.shader, NULL);
162
ASSERT_FALSE(etna_nir_lower_ubo_to_uniform(b.shader));
163
nir_validate_shader(b.shader, NULL);
165
ASSERT_EQ(count_intrinsic(nir_intrinsic_load_ubo), 1);
166
ASSERT_EQ(count_intrinsic(nir_intrinsic_load_uniform), 0);
169
TEST_F(nir_lower_ubo_test, indirect_offset)
171
nir_ssa_def *one = nir_imm_int(&b, 1);
172
nir_ssa_def *index = nir_imm_int(&b, 0);
173
nir_ssa_def *offset = nir_fadd(&b, one, one);
175
nir_load_ubo(&b, 1, 32, index, offset, .align_mul = 16, .align_offset = 0, .range_base = 0, .range = 8);
177
nir_validate_shader(b.shader, NULL);
179
ASSERT_FALSE(etna_nir_lower_ubo_to_uniform(b.shader));
180
nir_validate_shader(b.shader, NULL);
182
ASSERT_EQ(count_intrinsic(nir_intrinsic_load_ubo), 1);
183
ASSERT_EQ(count_intrinsic(nir_intrinsic_load_uniform), 0);