2 * Copyright © 2014 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.
25 * \file binding-layout.c
26 * Try some shaders with UBOs that use layout(binding=N). Verify that the API
27 * reports back the correct binding, and verify that the correct thing is used
31 #include "piglit-util-gl.h"
33 PIGLIT_GL_TEST_CONFIG_BEGIN
35 config
.supports_gl_core_version
= 31;
37 config
.window_width
= 100;
38 config
.window_height
= 100;
39 config
.window_visual
= PIGLIT_GL_VISUAL_RGBA
| PIGLIT_GL_VISUAL_DOUBLE
;
40 config
.khr_no_error_support
= PIGLIT_NO_ERRORS
;
42 PIGLIT_GL_TEST_CONFIG_END
44 static const char vert140_source
[] =
46 "#extension GL_ARB_explicit_attrib_location: require\n"
48 "layout(location=0) in vec4 piglit_vertex;\n"
49 "void main() { gl_Position = piglit_vertex; }\n"
52 static const char *frag140_source
=
54 "#extension GL_ARB_shading_language_420pack: require\n"
55 "#extension GL_ARB_explicit_attrib_location: require\n"
56 "#extension GL_ARB_uniform_buffer_object: require\n"
58 "layout(location=0) out vec4 o;\n"
59 "layout(binding=2, std140) uniform U { vec4 a; };\n"
60 "void main() { o = a; }\n"
63 static const char vert150_source
[] =
65 "#extension GL_ARB_explicit_attrib_location: require\n"
67 "layout(location=0) in vec4 piglit_vertex;\n"
68 "void main() { gl_Position = piglit_vertex; }\n"
71 static const char *frag150_source
=
73 "#extension GL_ARB_shading_language_420pack: require\n"
74 "#extension GL_ARB_explicit_attrib_location: require\n"
76 "layout(location=0) out vec4 o;\n"
77 "layout(binding=3, std140) uniform U { vec4 a; } u[2];\n"
78 "void main() { o = (u[0].a + u[1].a) / 5.0; }\n"
81 static GLuint prog140
= 0;
82 static GLuint prog150
= 0;
91 prog140
= piglit_build_simple_program(vert140_source
, frag140_source
);
93 idx
= glGetUniformBlockIndex(prog140
, "U");
95 printf("Failed to get index for \"U\"\n");
99 glGetActiveUniformBlockiv(prog140
, idx
, GL_UNIFORM_BLOCK_BINDING
,
102 printf("Expected block binding = 2, got %d\n", binding
);
106 pass
= piglit_check_gl_error(GL_NO_ERROR
) && pass
;
119 prog150
= piglit_build_simple_program(vert150_source
, frag150_source
);
121 for (i
= 0; i
< 2; i
++) {
122 char name
[5] = "U[0]";
126 idx
= glGetUniformBlockIndex(prog150
, name
);
128 printf("Failed to get index for \"%s\"\n", name
);
132 glGetActiveUniformBlockiv(prog150
, idx
,
133 GL_UNIFORM_BLOCK_BINDING
, &binding
);
134 if (binding
!= (3 + i
)) {
135 printf("Expected block binding = %d, got %d\n",
141 pass
= piglit_check_gl_error(GL_NO_ERROR
) && pass
;
147 piglit_init(int argc
, char **argv
)
149 static const float data
[] = {
158 piglit_require_extension("GL_ARB_shading_language_420pack");
159 piglit_require_extension("GL_ARB_explicit_attrib_location");
161 pass
= try_140_test() && pass
;
163 if (piglit_get_gl_version() >= 32)
164 pass
= try_150_test() && pass
;
166 /* If the set-up tests failed, don't even bother trying to render.
167 * That can only lead to more failure. We don't need to rub it in.
170 piglit_report_result(PIGLIT_FAIL
);
172 /* Pad out to the alignment or the size of a vec4. */
173 glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT
, &alignment
);
174 alignment
= MAX2(alignment
, 4 * sizeof(float));
176 glGenBuffers(1, &bo
);
177 glBindBuffer(GL_UNIFORM_BUFFER
, bo
);
178 glBufferData(GL_UNIFORM_BUFFER
, 3 * alignment
, NULL
, GL_STATIC_DRAW
);
179 glBufferSubData(GL_UNIFORM_BUFFER
, 0 * alignment
, 16, &data
[0]);
180 glBufferSubData(GL_UNIFORM_BUFFER
, 1 * alignment
, 16, &data
[4]);
181 glBufferSubData(GL_UNIFORM_BUFFER
, 2 * alignment
, 16, &data
[8]);
182 glBindBuffer(GL_UNIFORM_BUFFER
, 0);
184 glBindBufferRange(GL_UNIFORM_BUFFER
, 2, bo
, 0 * alignment
, 16);
185 glBindBufferRange(GL_UNIFORM_BUFFER
, 3, bo
, 1 * alignment
, 16);
186 glBindBufferRange(GL_UNIFORM_BUFFER
, 4, bo
, 2 * alignment
, 16);
188 glClearColor(0.5, 0.5, 0.5, 1.0);
191 enum piglit_result
piglit_display(void)
193 static const float green
[] = { 0.0, 1.0, 0.0, 1.0 };
195 glClear(GL_COLOR_BUFFER_BIT
);
197 glUseProgram(prog140
);
198 piglit_draw_rect(-1.0f
, -1.0f
, 1.0f
, 2.0f
);
199 piglit_probe_rect_rgb(0, 0,
200 piglit_width
/ 2, piglit_height
,
204 glUseProgram(prog150
);
205 piglit_draw_rect(0.0f
, -1.0f
, 1.0f
, 2.0f
);
206 piglit_probe_rect_rgb(piglit_width
/ 2, 0,
207 piglit_width
/ 2, piglit_height
,
211 piglit_present_results();