fbo-mrt-alphatest: Actually require MRTs to be available.
[piglit.git] / tests / spec / arb_shader_storage_buffer_object / layout-std140-write-shader.c
blob1f263faeb3793d06f2df706f391e04ff6555a1b0
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-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
42 #define SSBO_SIZE 44
44 static const char vs_pass_thru_text[] =
45 "#version 130\n"
46 "#extension GL_ARB_shader_storage_buffer_object : require\n"
47 "#extension GL_ARB_uniform_buffer_object : require\n"
48 "\n"
49 "struct A {\n"
50 " float a1;\n"
51 " vec2 a2[2];\n"
52 " mat2 a4;\n"
53 "};\n"
54 "layout(std140, row_major, binding=2) buffer ssbo {\n"
55 " vec4 v;\n"
56 " float f;\n"
57 " A s;\n"
58 " float unsized_array[];\n"
59 "};\n"
60 "in vec4 piglit_vertex;\n"
61 "void main() {\n"
62 " gl_Position = piglit_vertex;\n"
63 " f = 4.0;\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"
68 "}\n";
70 static const char fs_source[] =
71 "#version 130\n"
72 "#extension GL_ARB_shader_storage_buffer_object : require\n"
73 "#extension GL_ARB_uniform_buffer_object : require\n"
74 "\n"
75 "struct A {\n"
76 " float a1;\n"
77 " vec2 a2[2];\n"
78 " mat2 a4;\n"
79 "};\n"
80 "layout(std140, row_major, binding=2) buffer ssbo {\n"
81 " vec4 v;\n"
82 " float f;\n"
83 " A s;\n"
84 " float unsized_array[];\n"
85 "};\n"
86 "out vec4 color;\n"
87 "\n"
88 "void main() {\n"
89 " color = vec4(0,1,0,1);\n"
90 " v = vec4(0.0, 1.0, 2.0, 3.0);\n"
91 " s.a1 = 5.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"
97 "}\n";
99 GLuint prog;
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]
114 void
115 piglit_init(int argc, char **argv)
117 bool pass = true;
118 GLuint buffer;
119 unsigned int i;
120 float ssbo_values[SSBO_SIZE] = {0};
121 float *map;
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);
131 glUseProgram(prog);
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]);
151 pass = false;
155 glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
157 if (!piglit_check_gl_error(GL_NO_ERROR))
158 pass = false;
160 piglit_report_result(pass ? PIGLIT_PASS : PIGLIT_FAIL);
163 enum piglit_result piglit_display(void)
165 /* UNREACHED */
166 return PIGLIT_FAIL;