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-std140-write-shader.c
26 * Tests that shader storage block writes in GLSL works correctly (offsets and
27 * values) when interface packing qualifier is std140 and row_major.
30 #include "piglit-util-gl.h"
32 PIGLIT_GL_TEST_CONFIG_BEGIN
33 config
.window_width
= 100;
34 config
.window_height
= 100;
35 config
.supports_gl_compat_version
= 32;
36 config
.supports_gl_core_version
= 32;
37 config
.window_visual
= PIGLIT_GL_VISUAL_DOUBLE
| PIGLIT_GL_VISUAL_RGBA
;
38 config
.khr_no_error_support
= PIGLIT_NO_ERRORS
;
40 PIGLIT_GL_TEST_CONFIG_END
44 static const char vs_pass_thru_text
[] =
46 "#extension GL_ARB_shader_storage_buffer_object : require\n"
47 "#extension GL_ARB_uniform_buffer_object : require\n"
54 "layout(std140, row_major, binding=2) buffer ssbo {\n"
58 " float unsized_array[];\n"
60 "in vec4 piglit_vertex;\n"
62 " gl_Position = piglit_vertex;\n"
64 " s.a2[0] = vec2(6.0, 7.0); \n"
65 " int index = int(v.x); // index should be zero\n"
66 " if ((index + gl_VertexID) < 4)\n"
67 " unsized_array[index + gl_VertexID] = unsized_array.length();\n"
70 static const char fs_source
[] =
72 "#extension GL_ARB_shader_storage_buffer_object : require\n"
73 "#extension GL_ARB_uniform_buffer_object : require\n"
80 "layout(std140, row_major, binding=2) buffer ssbo {\n"
84 " float unsized_array[];\n"
89 " color = vec4(0,1,0,1);\n"
90 " v = vec4(0.0, 1.0, 2.0, 3.0);\n"
92 " s.a2[1] = vec2(8.0, 9.0);\n"
93 " s.a4 = mat2(10.0, 11.0, 12.0, 13.0);\n"
94 " int index = int(v.z + gl_FragCoord.x);\n"
95 " if (index >= 0 && index < 4)\n"
96 " unsized_array[index] = unsized_array.length() * 2.0;\n"
101 float expected
[SSBO_SIZE
] = { 0.0, 1.0, 2.0, 3.0, // vec4 v
102 4.0, 0.0, 0.0, 0.0, // float f
103 5.0, 0.0, 0.0, 0.0, // float s.a1
104 6.0, 7.0, 0.0, 0.0, // vec2 s.a2[0]
105 8.0, 9.0, 0.0, 0.0, // vec2 s.a2[1]
106 10.0, 12.0, 0.0, 0.0, // mat2 a4
107 11.0, 13.0, 0.0, 0.0, // mat2 a4
108 4.0, 0.0, 0.0, 0.0, // float unsized_array[0]
109 4.0, 0.0, 0.0, 0.0, // float unsized_array[1]
110 8.0, 0.0, 0.0, 0.0, // float unsized_array[2]
111 8.0, 0.0, 0.0, 0.0, // float unsized_array[3]
115 piglit_init(int argc
, char **argv
)
120 float ssbo_values
[SSBO_SIZE
] = {0};
122 GLint num_vertex_ssbo
;
124 piglit_require_extension("GL_ARB_shader_storage_buffer_object");
126 glGetIntegerv(GL_MAX_VERTEX_SHADER_STORAGE_BLOCKS
, &num_vertex_ssbo
);
127 if (num_vertex_ssbo
< 1)
128 piglit_report_result(PIGLIT_SKIP
);
129 prog
= piglit_build_simple_program(vs_pass_thru_text
, fs_source
);
133 glClearColor(0, 0, 0, 0);
135 glGenBuffers(1, &buffer
);
136 glBindBufferBase(GL_SHADER_STORAGE_BUFFER
, 2, buffer
);
137 glBufferData(GL_SHADER_STORAGE_BUFFER
, SSBO_SIZE
*sizeof(GLfloat
),
138 &ssbo_values
[0], GL_DYNAMIC_DRAW
);
140 glViewport(0, 0, piglit_width
, piglit_height
);
142 piglit_draw_rect(-1, -1, 2, 2);
144 glBindBuffer(GL_SHADER_STORAGE_BUFFER
, buffer
);
145 map
= (float *) glMapBuffer(GL_SHADER_STORAGE_BUFFER
, GL_READ_ONLY
);
147 for (i
= 0; i
< SSBO_SIZE
; i
++) {
148 if (map
[i
] != expected
[i
]) {
149 printf("expected[%d] = %.2f. Read value: %.2f\n",
150 i
, expected
[i
], map
[i
]);
155 glUnmapBuffer(GL_SHADER_STORAGE_BUFFER
);
157 if (!piglit_check_gl_error(GL_NO_ERROR
))
160 piglit_report_result(pass
? PIGLIT_PASS
: PIGLIT_FAIL
);
163 enum piglit_result
piglit_display(void)