glsl: test loop unroll with uint overflow
[piglit.git] / tests / spec / arb_shader_storage_buffer_object / layout-std430-write-shader.c
blob9fe054ad7ce6820b66e03ac43b9b142447ede9e0
1 /*
2 * Copyright © 2015 Intel Corporation
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:
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.
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.
24 /** @file layout-std430-write-shader.c
26 * Tests that shader storage block writes in GLSL works correctly (offsets and
27 * values) when interface packing qualifier is std430.
29 * From GL_ARB_shader_storage_buffer_object:
31 * "When using the "std430" storage layout, shader storage
32 * blocks will be laid out in buffer storage identically to uniform and
33 * shader storage blocks using the "std140" layout, except that the base
34 * alignment of arrays of scalars and vectors in rule (4) and of structures
35 * in rule (9) are not rounded up a multiple of the base alignment of a vec4."
38 #include "piglit-util-gl.h"
40 PIGLIT_GL_TEST_CONFIG_BEGIN
41 config.window_width = 100;
42 config.window_height = 100;
43 config.supports_gl_compat_version = 32;
44 config.supports_gl_core_version = 32;
45 config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGBA;
46 config.khr_no_error_support = PIGLIT_NO_ERRORS;
48 PIGLIT_GL_TEST_CONFIG_END
50 #define SSBO_SIZE 64
52 static const char vs_pass_thru_text[] =
53 "#version 130\n"
54 "#extension GL_ARB_shader_storage_buffer_object : require\n"
55 "#extension GL_ARB_uniform_buffer_object : require\n"
56 "\n"
57 "struct B { float b1[3]; };\n"
58 "struct A {\n"
59 " float a1;\n"
60 " vec3 a2;\n"
61 " mat2 a4[2];\n"
62 " B sb[2];\n"
63 "};\n"
64 "layout(std430, binding=2) buffer ssbo {\n"
65 " vec4 v;\n"
66 " float f;\n"
67 " A s;\n"
68 " mat3x4 m;\n"
69 " vec2 v2a[3];\n"
70 " vec3 v3a[2];\n"
71 " float unsized_array[];\n"
72 "};\n"
73 "in vec4 piglit_vertex;\n"
74 "void main() {\n"
75 " gl_Position = piglit_vertex;\n"
76 " v.yz = vec2(1.0, 2.0);\n"
77 " f = 4.0;\n"
78 " s.a2.x = 6.0; \n"
79 " s.a2.y = 7.0; \n"
80 " s.a4[0] = mat2(10.0, 11.0, 12.0, 13.0);\n"
81 " s.sb[0].b1[0] = 18.0;\n"
82 " s.sb[0].b1[1] = 19.0;\n"
83 " m[1] = vec4(25.0, 26.0, 27.0, 28.0);\n"
84 " v2a[0].yx = vec2(34.0, 33.0);\n"
85 " v2a[1].y = 36.0;\n"
86 " v3a[0].xz = vec2(39.0, 41.0);\n"
87 " v3a[1].y = 43.0;\n"
88 " int index = int(v.x); // index should be zero\n"
89 " if ((index + gl_VertexID) < 4)\n"
90 " unsized_array[index + gl_VertexID] = unsized_array.length();\n"
91 "}\n";
93 static const char fs_source[] =
94 "#version 130\n"
95 "#extension GL_ARB_shader_storage_buffer_object : require\n"
96 "#extension GL_ARB_uniform_buffer_object : require\n"
97 "\n"
98 "struct B { float b1[3]; };\n"
99 "struct A {\n"
100 " float a1;\n"
101 " vec3 a2;\n"
102 " mat2 a4[2];\n"
103 " B sb[2];\n"
104 "};\n"
105 "layout(std430, binding=2) buffer ssbo {\n"
106 " vec4 v;\n"
107 " float f;\n"
108 " A s;\n"
109 " mat3x4 m;\n"
110 " vec2 v2a[3];\n"
111 " vec3 v3a[2];\n"
112 " float unsized_array[];\n"
113 "};\n"
114 "out vec4 color;\n"
115 "\n"
116 "void main() {\n"
117 " color = vec4(0,1,0,1);\n"
118 " v.xw = vec2(0.0, 3.0);\n"
119 " s.a1 = 5.0;\n"
120 " s.a2.z = 8.0;\n"
121 " s.a4[1] = mat2(14.0, 15.0, 16.0, 17.0);\n"
122 " s.sb[1].b1[2] = 20.0;\n"
123 " m[0] = vec4(21.0, 22.0, 23.0, 24.0);\n"
124 " m[2] = vec4(29.0, 30.0, 31.0, 32.0);\n"
125 " v2a[1].x = 35.0;\n"
126 " v2a[2].xy = vec2(37.0, 38.0);\n"
127 " v3a[0].y = 40.0;\n"
128 " v3a[1].xz = vec2(42.0, 44.0);\n"
129 " int index = int(v.z + gl_FragCoord.x);\n"
130 " if (index >= 0 && index < 4)\n"
131 " unsized_array[index] = unsized_array.length() * 2.0;\n"
132 "}\n";
134 GLuint prog;
136 float expected[SSBO_SIZE] = { 0.0, 1.0, 2.0, 3.0, // vec4 v
137 4.0, 0.0, 0.0, 0.0, // float f
138 5.0, 0.0, 0.0, 0.0, // float s.a1
139 6.0, 7.0, 8.0, 0.0, // vec3 s.a2
140 10.0, 11.0, 12.0, 13.0, // mat2 s.a4[0]
141 14.0, 15.0, 16.0, 17.0, // mat2 s.a4[1]
142 18.0, 19.0, 0.0, 0.0, // float s.sb[0].b1
143 0.0, 20.0, 0.0, 0.0, // float s.sb[1].b1
144 21.0, 22.0, 23.0, 24.0, // mat3x4 m[0]
145 25.0, 26.0, 27.0, 28.0, // mat3x4 m[1]
146 29.0, 30.0, 31.0, 32.0, // mat3x4 m[2]
147 33.0, 34.0, 35.0, 36.0, // vec2 v2a[3]
148 37.0, 38.0, 0.0, 0.0, //
149 39.0, 40.0, 41.0, 0.0, // vec3 v3a[2]
150 42.0, 43.0, 44.0, 0.0, //
151 4.0, 4.0, 8.0, 8.0 // float unsized_array[0]
154 void
155 piglit_init(int argc, char **argv)
157 bool pass = true;
158 GLuint buffer;
159 unsigned int i;
160 float ssbo_values[SSBO_SIZE] = {0};
161 float *map;
162 GLint num_vertex_ssbo;
164 piglit_require_extension("GL_ARB_shader_storage_buffer_object");
165 piglit_require_GLSL_version(130);
167 glGetIntegerv(GL_MAX_VERTEX_SHADER_STORAGE_BLOCKS, &num_vertex_ssbo);
168 if (num_vertex_ssbo < 1)
169 piglit_report_result(PIGLIT_SKIP);
171 prog = piglit_build_simple_program(vs_pass_thru_text, fs_source);
173 glUseProgram(prog);
175 glClearColor(0, 0, 0, 0);
177 glGenBuffers(1, &buffer);
178 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, buffer);
179 glBufferData(GL_SHADER_STORAGE_BUFFER, SSBO_SIZE*sizeof(GLfloat),
180 &ssbo_values[0], GL_DYNAMIC_DRAW);
182 glViewport(0, 0, piglit_width, piglit_height);
184 piglit_draw_rect(-1, -1, 2, 2);
186 glBindBuffer(GL_SHADER_STORAGE_BUFFER, buffer);
187 map = (float *) glMapBuffer(GL_SHADER_STORAGE_BUFFER, GL_READ_ONLY);
189 for (i = 0; i < SSBO_SIZE; i++) {
190 if (map[i] != expected[i]) {
191 printf("expected[%d] = %.2f. Read value: %.2f\n",
192 i, expected[i], map[i]);
193 pass = false;
197 glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
199 if (!piglit_check_gl_error(GL_NO_ERROR))
200 pass = false;
202 piglit_report_result(pass ? PIGLIT_PASS : PIGLIT_FAIL);
205 enum piglit_result piglit_display(void)
207 /* UNREACHED */
208 return PIGLIT_FAIL;