summaryrefslogtreecommitdiff
path: root/src/glsl/tests/builtin_variable_test.cpp
blob: 11e384a072280353e5b47f1003ed263eab35ffd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
 * Copyright © 2013 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */
#include <gtest/gtest.h>
#include "standalone_scaffolding.h"
#include "main/compiler.h"
#include "main/mtypes.h"
#include "main/macros.h"
#include "ir.h"
#include "glsl_parser_extras.h"
#include "glsl_symbol_table.h"

class common_builtin : public ::testing::Test {
public:
   common_builtin(GLenum shader_type)
      : shader_type(shader_type)
   {
      /* empty */
   }

   virtual void SetUp();
   virtual void TearDown();

   void string_starts_with_prefix(const char *str, const char *prefix);
   void names_start_with_gl();
   void uniforms_and_system_values_dont_have_explicit_location();
   void constants_are_constant();
   void no_invalid_variable_modes();

   GLenum shader_type;
   struct _mesa_glsl_parse_state *state;
   struct gl_shader *shader;
   void *mem_ctx;
   gl_context ctx;
   exec_list ir;
};

void
common_builtin::SetUp()
{
   this->mem_ctx = ralloc_context(NULL);
   this->ir.make_empty();

   initialize_context_to_defaults(&this->ctx, API_OPENGL_COMPAT);

   this->shader = rzalloc(this->mem_ctx, gl_shader);
   this->shader->Type = this->shader_type;
   this->shader->Stage = _mesa_shader_enum_to_shader_stage(this->shader_type);

   this->state =
      new(mem_ctx) _mesa_glsl_parse_state(&this->ctx, this->shader->Stage,
                                          this->shader);

   _mesa_glsl_initialize_types(this->state);
   _mesa_glsl_initialize_variables(&this->ir, this->state);
}

void
common_builtin::TearDown()
{
   ralloc_free(this->mem_ctx);
   this->mem_ctx = NULL;
}

void
common_builtin::string_starts_with_prefix(const char *str, const char *prefix)
{
   const size_t len = strlen(prefix);
   char *const name_prefix = new char[len + 1];

   strncpy(name_prefix, str, len);
   name_prefix[len] = '\0';
   EXPECT_STREQ(prefix, name_prefix) << "Bad name " << str;

   delete [] name_prefix;
}

void
common_builtin::names_start_with_gl()
{
   foreach_in_list(ir_instruction, node, &this->ir) {
      ir_variable *const var = node->as_variable();

      string_starts_with_prefix(var->name, "gl_");
   }
}

void
common_builtin::uniforms_and_system_values_dont_have_explicit_location()
{
   foreach_in_list(ir_instruction, node, &this->ir) {
      ir_variable *const var = node->as_variable();

      if (var->data.mode != ir_var_uniform && var->data.mode != ir_var_system_value)
         continue;

      EXPECT_FALSE(var->data.explicit_location);
      EXPECT_EQ(-1, var->data.location);
   }
}

void
common_builtin::constants_are_constant()
{
   foreach_in_list(ir_instruction, node, &this->ir) {
      ir_variable *const var = node->as_variable();

      if (var->data.mode != ir_var_auto)
         continue;

      EXPECT_FALSE(var->data.explicit_location);
      EXPECT_EQ(-1, var->data.location);
      EXPECT_TRUE(var->data.read_only);
   }
}

void
common_builtin::no_invalid_variable_modes()
{
   foreach_in_list(ir_instruction, node, &this->ir) {
      ir_variable *const var = node->as_variable();

      switch (var->data.mode) {
      case ir_var_auto:
      case ir_var_uniform:
      case ir_var_shader_in:
      case ir_var_shader_out:
      case ir_var_system_value:
         break;

      default:
         ADD_FAILURE() << "Built-in variable " << var->name
                       << " has an invalid mode " << int(var->data.mode);
         break;
      }
   }
}

/************************************************************/

class vertex_builtin : public common_builtin {
public:
   vertex_builtin()
      : common_builtin(GL_VERTEX_SHADER)
   {
      /* empty */
   }
};

TEST_F(vertex_builtin, names_start_with_gl)
{
   common_builtin::names_start_with_gl();
}

TEST_F(vertex_builtin, inputs_have_explicit_location)
{
   foreach_in_list(ir_instruction, node, &this->ir) {
      ir_variable *const var = node->as_variable();

      if (var->data.mode != ir_var_shader_in)
         continue;

      EXPECT_TRUE(var->data.explicit_location);
      EXPECT_NE(-1, var->data.location);
      EXPECT_GT(VERT_ATTRIB_GENERIC0, var->data.location);
      EXPECT_EQ(0u, var->data.location_frac);
   }
}

TEST_F(vertex_builtin, outputs_have_explicit_location)
{
   foreach_in_list(ir_instruction, node, &this->ir) {
      ir_variable *const var = node->as_variable();

      if (var->data.mode != ir_var_shader_out)
         continue;

      EXPECT_TRUE(var->data.explicit_location);
      EXPECT_NE(-1, var->data.location);
      EXPECT_GT(VARYING_SLOT_VAR0, var->data.location);
      EXPECT_EQ(0u, var->data.location_frac);

      /* Several varyings only exist in the fragment shader.  Be sure that no
       * outputs with these locations exist.
       */
      EXPECT_NE(VARYING_SLOT_PNTC, var->data.location);
      EXPECT_NE(VARYING_SLOT_FACE, var->data.location);
      EXPECT_NE(VARYING_SLOT_PRIMITIVE_ID, var->data.location);
   }
}

TEST_F(vertex_builtin, uniforms_and_system_values_dont_have_explicit_location)
{
   common_builtin::uniforms_and_system_values_dont_have_explicit_location();
}

TEST_F(vertex_builtin, constants_are_constant)
{
   common_builtin::constants_are_constant();
}

TEST_F(vertex_builtin, no_invalid_variable_modes)
{
   common_builtin::no_invalid_variable_modes();
}

/********************************************************************/

class fragment_builtin : public common_builtin {
public:
   fragment_builtin()
      : common_builtin(GL_FRAGMENT_SHADER)
   {
      /* empty */
   }
};

TEST_F(fragment_builtin, names_start_with_gl)
{
   common_builtin::names_start_with_gl();
}

TEST_F(fragment_builtin, inputs_have_explicit_location)
{
   foreach_in_list(ir_instruction, node, &this->ir) {
      ir_variable *const var = node->as_variable();

      if (var->data.mode != ir_var_shader_in)
	 continue;

      EXPECT_TRUE(var->data.explicit_location);
      EXPECT_NE(-1, var->data.location);
      EXPECT_GT(VARYING_SLOT_VAR0, var->data.location);
      EXPECT_EQ(0u, var->data.location_frac);

      /* Several varyings only exist in the vertex / geometry shader.  Be sure
       * that no inputs with these locations exist.
       */
      EXPECT_TRUE(_mesa_varying_slot_in_fs((gl_varying_slot) var->data.location));
   }
}

TEST_F(fragment_builtin, outputs_have_explicit_location)
{
   foreach_in_list(ir_instruction, node, &this->ir) {
      ir_variable *const var = node->as_variable();

      if (var->data.mode != ir_var_shader_out)
	 continue;

      EXPECT_TRUE(var->data.explicit_location);
      EXPECT_NE(-1, var->data.location);

      /* gl_FragData[] has location FRAG_RESULT_DATA0.  Locations beyond that
       * are invalid.
       */
      EXPECT_GE(FRAG_RESULT_DATA0, var->data.location);

      EXPECT_EQ(0u, var->data.location_frac);
   }
}

TEST_F(fragment_builtin, uniforms_and_system_values_dont_have_explicit_location)
{
   common_builtin::uniforms_and_system_values_dont_have_explicit_location();
}

TEST_F(fragment_builtin, constants_are_constant)
{
   common_builtin::constants_are_constant();
}

TEST_F(fragment_builtin, no_invalid_variable_modes)
{
   common_builtin::no_invalid_variable_modes();
}

/********************************************************************/

class geometry_builtin : public common_builtin {
public:
   geometry_builtin()
      : common_builtin(GL_GEOMETRY_SHADER)
   {
      /* empty */
   }
};

TEST_F(geometry_builtin, names_start_with_gl)
{
   common_builtin::names_start_with_gl();
}

TEST_F(geometry_builtin, inputs_have_explicit_location)
{
   foreach_in_list(ir_instruction, node, &this->ir) {
      ir_variable *const var = node->as_variable();

      if (var->data.mode != ir_var_shader_in)
	 continue;

      if (var->is_interface_instance()) {
         EXPECT_STREQ("gl_in", var->name);
         EXPECT_FALSE(var->data.explicit_location);
         EXPECT_EQ(-1, var->data.location);

         ASSERT_TRUE(var->type->is_array());

         const glsl_type *const instance_type = var->type->fields.array;

         for (unsigned i = 0; i < instance_type->length; i++) {
            const glsl_struct_field *const input =
               &instance_type->fields.structure[i];

            string_starts_with_prefix(input->name, "gl_");
            EXPECT_NE(-1, input->location);
            EXPECT_GT(VARYING_SLOT_VAR0, input->location);

            /* Several varyings only exist in the fragment shader.  Be sure
             * that no inputs with these locations exist.
             */
            EXPECT_NE(VARYING_SLOT_PNTC, input->location);
            EXPECT_NE(VARYING_SLOT_FACE, input->location);
         }
      } else {
         EXPECT_TRUE(var->data.explicit_location);
         EXPECT_NE(-1, var->data.location);
         EXPECT_GT(VARYING_SLOT_VAR0, var->data.location);
         EXPECT_EQ(0u, var->data.location_frac);
      }

      /* Several varyings only exist in the fragment shader.  Be sure that no
       * inputs with these locations exist.
       */
      EXPECT_NE(VARYING_SLOT_PNTC, var->data.location);
      EXPECT_NE(VARYING_SLOT_FACE, var->data.location);
   }
}

TEST_F(geometry_builtin, outputs_have_explicit_location)
{
   foreach_in_list(ir_instruction, node, &this->ir) {
      ir_variable *const var = node->as_variable();

      if (var->data.mode != ir_var_shader_out)
	 continue;

      EXPECT_TRUE(var->data.explicit_location);
      EXPECT_NE(-1, var->data.location);
      EXPECT_GT(VARYING_SLOT_VAR0, var->data.location);
      EXPECT_EQ(0u, var->data.location_frac);

      /* Several varyings only exist in the fragment shader.  Be sure that no
       * outputs with these locations exist.
       */
      EXPECT_NE(VARYING_SLOT_PNTC, var->data.location);
      EXPECT_NE(VARYING_SLOT_FACE, var->data.location);
   }
}

TEST_F(geometry_builtin, uniforms_and_system_values_dont_have_explicit_location)
{
   common_builtin::uniforms_and_system_values_dont_have_explicit_location();
}

TEST_F(geometry_builtin, constants_are_constant)
{
   common_builtin::constants_are_constant();
}

TEST_F(geometry_builtin, no_invalid_variable_modes)
{
   common_builtin::no_invalid_variable_modes();
}