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
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
52 static const char vs_pass_thru_text
[] =
54 "#extension GL_ARB_shader_storage_buffer_object : require\n"
55 "#extension GL_ARB_uniform_buffer_object : require\n"
57 "struct B { float b1[3]; };\n"
64 "layout(std430, binding=2) buffer ssbo {\n"
71 " float unsized_array[];\n"
73 "in vec4 piglit_vertex;\n"
75 " gl_Position = piglit_vertex;\n"
76 " v.yz = vec2(1.0, 2.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"
86 " v3a[0].xz = vec2(39.0, 41.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"
93 static const char fs_source
[] =
95 "#extension GL_ARB_shader_storage_buffer_object : require\n"
96 "#extension GL_ARB_uniform_buffer_object : require\n"
98 "struct B { float b1[3]; };\n"
105 "layout(std430, binding=2) buffer ssbo {\n"
112 " float unsized_array[];\n"
117 " color = vec4(0,1,0,1);\n"
118 " v.xw = vec2(0.0, 3.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"
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]
155 piglit_init(int argc
, char **argv
)
160 float ssbo_values
[SSBO_SIZE
] = {0};
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
);
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
]);
197 glUnmapBuffer(GL_SHADER_STORAGE_BUFFER
);
199 if (!piglit_check_gl_error(GL_NO_ERROR
))
202 piglit_report_result(pass
? PIGLIT_PASS
: PIGLIT_FAIL
);
205 enum piglit_result
piglit_display(void)