~mmach/netext73/mesa-haswell

« back to all changes in this revision

Viewing changes to src/compiler/glsl/tests/uniform_initializer_utils.cpp

  • Committer: mmach
  • Date: 2022-09-22 19:56:13 UTC
  • Revision ID: netbit73@gmail.com-20220922195613-wtik9mmy20tmor0i
2022-09-22 21:17:09

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright © 2012 Intel Corporation
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
20
 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
 
 * DEALINGS IN THE SOFTWARE.
22
 
 */
23
 
#include <gtest/gtest.h>
24
 
#include "main/macros.h"
25
 
#include "util/ralloc.h"
26
 
#include "uniform_initializer_utils.h"
27
 
#include <stdio.h>
28
 
 
29
 
void
30
 
fill_storage_array_with_sentinels(gl_constant_value *storage,
31
 
                                  unsigned data_size,
32
 
                                  unsigned red_zone_size)
33
 
{
34
 
   for (unsigned i = 0; i < data_size; i++)
35
 
      storage[i].u = 0xDEADBEEF;
36
 
 
37
 
   for (unsigned i = 0; i < red_zone_size; i++)
38
 
      storage[data_size + i].u = 0xBADDC0DE;
39
 
}
40
 
 
41
 
/**
42
 
 * Verfiy that markers past the end of the real uniform are unmodified
43
 
 */
44
 
static ::testing::AssertionResult
45
 
red_zone_is_intact(gl_constant_value *storage,
46
 
                   unsigned data_size,
47
 
                   unsigned red_zone_size)
48
 
{
49
 
   for (unsigned i = 0; i < red_zone_size; i++) {
50
 
      const unsigned idx = data_size + i;
51
 
 
52
 
      if (storage[idx].u != 0xBADDC0DE)
53
 
         return ::testing::AssertionFailure()
54
 
            << "storage[" << idx << "].u = "  << storage[idx].u
55
 
            << ", exepected data values = " << data_size
56
 
            << ", red-zone size = " << red_zone_size;
57
 
   }
58
 
 
59
 
   return ::testing::AssertionSuccess();
60
 
}
61
 
 
62
 
static const int values[] = {
63
 
   2, 0, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53
64
 
};
65
 
 
66
 
/**
67
 
 * Generate a single data element.
68
 
 *
69
 
 * This is by both \c generate_data and \c generate_array_data to create the
70
 
 * data.
71
 
 */
72
 
static void
73
 
generate_data_element(void *mem_ctx, const glsl_type *type,
74
 
                      ir_constant *&val, unsigned data_index_base)
75
 
{
76
 
   /* Set the initial data values for the generated constant.
77
 
    */
78
 
   ir_constant_data data;
79
 
   memset(&data, 0, sizeof(data));
80
 
   for (unsigned i = 0; i < type->components(); i++) {
81
 
      const unsigned idx = (i + data_index_base) % ARRAY_SIZE(values);
82
 
      switch (type->base_type) {
83
 
      case GLSL_TYPE_UINT:
84
 
      case GLSL_TYPE_INT:
85
 
      case GLSL_TYPE_SAMPLER:
86
 
      case GLSL_TYPE_TEXTURE:
87
 
      case GLSL_TYPE_IMAGE:
88
 
         data.i[i] = values[idx];
89
 
         break;
90
 
      case GLSL_TYPE_FLOAT:
91
 
         data.f[i] = float(values[idx]);
92
 
         break;
93
 
      case GLSL_TYPE_BOOL:
94
 
         data.b[i] = bool(values[idx]);
95
 
         break;
96
 
      case GLSL_TYPE_DOUBLE:
97
 
         data.d[i] = double(values[idx]);
98
 
         break;
99
 
      case GLSL_TYPE_UINT64:
100
 
         data.u64[i] = (uint64_t) values[idx];
101
 
         break;
102
 
      case GLSL_TYPE_INT64:
103
 
         data.i64[i] = (int64_t) values[idx];
104
 
         break;
105
 
      case GLSL_TYPE_ATOMIC_UINT:
106
 
      case GLSL_TYPE_STRUCT:
107
 
      case GLSL_TYPE_ARRAY:
108
 
      case GLSL_TYPE_VOID:
109
 
      case GLSL_TYPE_ERROR:
110
 
      case GLSL_TYPE_INTERFACE:
111
 
      case GLSL_TYPE_SUBROUTINE:
112
 
      case GLSL_TYPE_FUNCTION:
113
 
      case GLSL_TYPE_FLOAT16:
114
 
      case GLSL_TYPE_UINT16:
115
 
      case GLSL_TYPE_INT16:
116
 
      case GLSL_TYPE_UINT8:
117
 
      case GLSL_TYPE_INT8:
118
 
         ASSERT_TRUE(false);
119
 
         break;
120
 
      }
121
 
   }
122
 
 
123
 
   /* Generate and verify the constant.
124
 
    */
125
 
   val = new(mem_ctx) ir_constant(type, &data);
126
 
 
127
 
   for (unsigned i = 0; i < type->components(); i++) {
128
 
      switch (type->base_type) {
129
 
      case GLSL_TYPE_UINT:
130
 
      case GLSL_TYPE_INT:
131
 
      case GLSL_TYPE_SAMPLER:
132
 
      case GLSL_TYPE_TEXTURE:
133
 
      case GLSL_TYPE_IMAGE:
134
 
         ASSERT_EQ(data.i[i], val->value.i[i]);
135
 
         break;
136
 
      case GLSL_TYPE_FLOAT:
137
 
         ASSERT_EQ(data.f[i], val->value.f[i]);
138
 
         break;
139
 
      case GLSL_TYPE_BOOL:
140
 
         ASSERT_EQ(data.b[i], val->value.b[i]);
141
 
         break;
142
 
      case GLSL_TYPE_DOUBLE:
143
 
         ASSERT_EQ(data.d[i], val->value.d[i]);
144
 
         break;
145
 
      case GLSL_TYPE_UINT64:
146
 
         ASSERT_EQ(data.u64[i], val->value.u64[i]);
147
 
         break;
148
 
      case GLSL_TYPE_INT64:
149
 
         ASSERT_EQ(data.i64[i], val->value.i64[i]);
150
 
         break;
151
 
      case GLSL_TYPE_ATOMIC_UINT:
152
 
      case GLSL_TYPE_STRUCT:
153
 
      case GLSL_TYPE_ARRAY:
154
 
      case GLSL_TYPE_VOID:
155
 
      case GLSL_TYPE_ERROR:
156
 
      case GLSL_TYPE_INTERFACE:
157
 
      case GLSL_TYPE_SUBROUTINE:
158
 
      case GLSL_TYPE_FUNCTION:
159
 
      case GLSL_TYPE_FLOAT16:
160
 
      case GLSL_TYPE_UINT16:
161
 
      case GLSL_TYPE_INT16:
162
 
      case GLSL_TYPE_UINT8:
163
 
      case GLSL_TYPE_INT8:
164
 
         ASSERT_TRUE(false);
165
 
         break;
166
 
      }
167
 
   }
168
 
}
169
 
 
170
 
void
171
 
generate_data(void *mem_ctx, enum glsl_base_type base_type,
172
 
              unsigned columns, unsigned rows,
173
 
              ir_constant *&val)
174
 
{
175
 
   /* Determine what the type of the generated constant should be.
176
 
    */
177
 
   const glsl_type *const type =
178
 
      glsl_type::get_instance(base_type, rows, columns);
179
 
   ASSERT_FALSE(type->is_error());
180
 
 
181
 
   generate_data_element(mem_ctx, type, val, 0);
182
 
}
183
 
 
184
 
void
185
 
generate_array_data(void *mem_ctx, enum glsl_base_type base_type,
186
 
                    unsigned columns, unsigned rows, unsigned array_size,
187
 
                    ir_constant *&val)
188
 
{
189
 
   /* Determine what the type of the generated constant should be.
190
 
    */
191
 
   const glsl_type *const element_type =
192
 
      glsl_type::get_instance(base_type, rows, columns);
193
 
   ASSERT_FALSE(element_type->is_error());
194
 
 
195
 
   const glsl_type *const array_type =
196
 
      glsl_type::get_array_instance(element_type, array_size);
197
 
   ASSERT_FALSE(array_type->is_error());
198
 
 
199
 
   /* Set the initial data values for the generated constant.
200
 
    */
201
 
   exec_list values_for_array;
202
 
   for (unsigned i = 0; i < array_size; i++) {
203
 
      ir_constant *element;
204
 
 
205
 
      generate_data_element(mem_ctx, element_type, element, i);
206
 
      values_for_array.push_tail(element);
207
 
   }
208
 
 
209
 
   val = new(mem_ctx) ir_constant(array_type, &values_for_array);
210
 
}
211
 
 
212
 
static uint64_t
213
 
uint64_storage(union gl_constant_value *storage)
214
 
{
215
 
   uint64_t val;
216
 
   memcpy(&val, &storage->i, sizeof(uint64_t));
217
 
   return val;
218
 
}
219
 
 
220
 
static uint64_t
221
 
double_storage(union gl_constant_value *storage)
222
 
{
223
 
   double val;
224
 
   memcpy(&val, &storage->i, sizeof(double));
225
 
   return val;
226
 
}
227
 
 
228
 
/**
229
 
 * Verify that the data stored for the uniform matches the initializer
230
 
 *
231
 
 * \param storage              Backing storage for the uniform
232
 
 * \param storage_array_size  Array size of the backing storage.  This must be
233
 
 *                            less than or equal to the array size of the type
234
 
 *                            of \c val.  If \c val is not an array, this must
235
 
 *                            be zero.
236
 
 * \param val                 Value of the initializer for the unifrom.
237
 
 * \param red_zone
238
 
 */
239
 
void
240
 
verify_data(gl_constant_value *storage, unsigned storage_array_size,
241
 
            ir_constant *val, unsigned red_zone_size,
242
 
            unsigned int boolean_true)
243
 
{
244
 
   if (val->type->is_array()) {
245
 
      const glsl_type *const element_type = val->const_elements[0]->type;
246
 
 
247
 
      for (unsigned i = 0; i < storage_array_size; i++) {
248
 
         verify_data(storage + (i * element_type->components()), 0,
249
 
                     val->const_elements[i], 0, boolean_true);
250
 
      }
251
 
 
252
 
      const unsigned components = element_type->components();
253
 
 
254
 
      if (red_zone_size > 0) {
255
 
         EXPECT_TRUE(red_zone_is_intact(storage,
256
 
                                        storage_array_size * components,
257
 
                                        red_zone_size));
258
 
      }
259
 
   } else {
260
 
      ASSERT_EQ(0u, storage_array_size);
261
 
      for (unsigned i = 0; i < val->type->components(); i++) {
262
 
         switch (val->type->base_type) {
263
 
         case GLSL_TYPE_UINT:
264
 
         case GLSL_TYPE_INT:
265
 
         case GLSL_TYPE_SAMPLER:
266
 
         case GLSL_TYPE_TEXTURE:
267
 
         case GLSL_TYPE_IMAGE:
268
 
            EXPECT_EQ(val->value.i[i], storage[i].i);
269
 
            break;
270
 
         case GLSL_TYPE_FLOAT:
271
 
            EXPECT_EQ(val->value.f[i], storage[i].f);
272
 
            break;
273
 
         case GLSL_TYPE_BOOL:
274
 
            EXPECT_EQ(val->value.b[i] ? boolean_true : 0, storage[i].i);
275
 
            break;
276
 
         case GLSL_TYPE_DOUBLE:
277
 
            EXPECT_EQ(val->value.d[i], double_storage(&storage[i*2]));
278
 
            break;
279
 
         case GLSL_TYPE_UINT64:
280
 
            EXPECT_EQ(val->value.u64[i], uint64_storage(&storage[i*2]));
281
 
            break;
282
 
         case GLSL_TYPE_INT64:
283
 
            EXPECT_EQ(val->value.i64[i], uint64_storage(&storage[i*2]));
284
 
            break;
285
 
         case GLSL_TYPE_ATOMIC_UINT:
286
 
         case GLSL_TYPE_STRUCT:
287
 
         case GLSL_TYPE_ARRAY:
288
 
         case GLSL_TYPE_VOID:
289
 
         case GLSL_TYPE_ERROR:
290
 
         case GLSL_TYPE_INTERFACE:
291
 
         case GLSL_TYPE_SUBROUTINE:
292
 
         case GLSL_TYPE_FUNCTION:
293
 
         case GLSL_TYPE_FLOAT16:
294
 
         case GLSL_TYPE_UINT16:
295
 
         case GLSL_TYPE_INT16:
296
 
         case GLSL_TYPE_UINT8:
297
 
         case GLSL_TYPE_INT8:
298
 
            ASSERT_TRUE(false);
299
 
            break;
300
 
         }
301
 
      }
302
 
 
303
 
      if (red_zone_size > 0) {
304
 
         EXPECT_TRUE(red_zone_is_intact(storage,
305
 
                                        val->type->components(),
306
 
                                        red_zone_size));
307
 
      }
308
 
   }
309
 
}