ext_gpu_shader4: add compiler tests for everything
[piglit.git] / tests / spec / arb_shading_language_420pack / execution / binding-layout.c
blob94d868dc7ec17bb266de33dd72fb8bb59073e982
1 /*
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
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 /**
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
28 * for rendering.
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[] =
45 "#version 140\n"
46 "#extension GL_ARB_explicit_attrib_location: require\n"
47 "\n"
48 "layout(location=0) in vec4 piglit_vertex;\n"
49 "void main() { gl_Position = piglit_vertex; }\n"
52 static const char *frag140_source =
53 "#version 140\n"
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"
57 "\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[] =
64 "#version 150 core\n"
65 "#extension GL_ARB_explicit_attrib_location: require\n"
66 "\n"
67 "layout(location=0) in vec4 piglit_vertex;\n"
68 "void main() { gl_Position = piglit_vertex; }\n"
71 static const char *frag150_source =
72 "#version 150 core\n"
73 "#extension GL_ARB_shading_language_420pack: require\n"
74 "#extension GL_ARB_explicit_attrib_location: require\n"
75 "\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;
84 static bool
85 try_140_test()
87 bool pass = true;
88 GLint idx;
89 GLint binding;
91 prog140 = piglit_build_simple_program(vert140_source, frag140_source);
93 idx = glGetUniformBlockIndex(prog140, "U");
94 if (idx == -1) {
95 printf("Failed to get index for \"U\"\n");
96 pass = false;
99 glGetActiveUniformBlockiv(prog140, idx, GL_UNIFORM_BLOCK_BINDING,
100 &binding);
101 if (binding != 2) {
102 printf("Expected block binding = 2, got %d\n", binding);
103 pass = false;
106 pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
108 return pass;
111 static bool
112 try_150_test()
114 bool pass = true;
115 GLint idx;
116 GLint binding;
117 unsigned i;
119 prog150 = piglit_build_simple_program(vert150_source, frag150_source);
121 for (i = 0; i < 2; i++) {
122 char name[5] = "U[0]";
124 name[2] = '0' + i;
126 idx = glGetUniformBlockIndex(prog150, name);
127 if (idx == -1) {
128 printf("Failed to get index for \"%s\"\n", name);
129 pass = false;
132 glGetActiveUniformBlockiv(prog150, idx,
133 GL_UNIFORM_BLOCK_BINDING, &binding);
134 if (binding != (3 + i)) {
135 printf("Expected block binding = %d, got %d\n",
136 3 + i, binding);
137 pass = false;
141 pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
143 return pass;
146 void
147 piglit_init(int argc, char **argv)
149 static const float data[] = {
150 0.0, 1.0, 0.0, 1.0,
151 0.0, 2.0, 0.0, 1.0,
152 0.0, 3.0, 0.0, 0.0,
154 bool pass = true;
155 GLuint bo;
156 GLint alignment;
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.
169 if (!pass)
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,
201 green);
203 if (prog150 != 0) {
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,
208 green);
211 piglit_present_results();
213 return PIGLIT_PASS;