ext_gpu_shader4: add compiler tests for everything
[piglit.git] / tests / spec / arb_uniform_buffer_object / rendering.c
blobf5b1e7f5d001d93c4330669c2f1d0bcfcf524951
1 /*
2 * Copyright (c) 2014 VMware, Inc.
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 rendering.c
26 * Test rendering with UBOs. We draw four squares with different positions,
27 * sizes, rotations and colors where those parameters come from UBOs.
30 #include "piglit-util-gl.h"
32 PIGLIT_GL_TEST_CONFIG_BEGIN
34 config.supports_gl_compat_version = 20;
35 config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGBA;
36 config.khr_no_error_support = PIGLIT_NO_ERRORS;
38 PIGLIT_GL_TEST_CONFIG_END
40 static const char vert_shader_text[] =
41 "#extension GL_ARB_uniform_buffer_object : require\n"
42 "\n"
43 "layout(std140) uniform;\n"
44 "uniform ub_pos_size { vec2 pos; float size; };\n"
45 "uniform ub_rot {float rotation; };\n"
46 "\n"
47 "void main()\n"
48 "{\n"
49 " mat2 m;\n"
50 " m[0][0] = m[1][1] = cos(rotation); \n"
51 " m[0][1] = sin(rotation); \n"
52 " m[1][0] = -m[0][1]; \n"
53 " gl_Position.xy = m * gl_Vertex.xy * vec2(size) + pos;\n"
54 " gl_Position.zw = vec2(0, 1);\n"
55 "}\n";
57 static const char frag_shader_text[] =
58 "#extension GL_ARB_uniform_buffer_object : require\n"
59 "\n"
60 "layout(std140) uniform;\n"
61 "uniform ub_color { vec4 color; float color_scale; };\n"
62 "\n"
63 "void main()\n"
64 "{\n"
65 " gl_FragColor = color * color_scale;\n"
66 "}\n";
68 #define NUM_SQUARES 4
69 #define NUM_UBOS 3
71 /* Square positions and sizes */
72 static const float pos_size[NUM_SQUARES][3] = {
73 { -0.5, -0.5, 0.1 },
74 { 0.5, -0.5, 0.2 },
75 { -0.5, 0.5, 0.3 },
76 { 0.5, 0.5, 0.4 }
79 /* Square color and color_scales */
80 static const float color[NUM_SQUARES][8] = {
81 { 2.0, 0.0, 0.0, 1.0, 0.50, 0.0, 0.0, 0.0 },
82 { 0.0, 4.0, 0.0, 1.0, 0.25, 0.0, 0.0, 0.0 },
83 { 0.0, 0.0, 5.0, 1.0, 0.20, 0.0, 0.0, 0.0 },
84 { 0.2, 0.2, 0.2, 0.2, 5.00, 0.0, 0.0, 0.0 }
87 /* Square rotations */
88 static const float rotation[NUM_SQUARES] = {
89 0.0,
90 0.1,
91 0.2,
92 0.3
95 static GLuint prog;
96 static GLuint buffers[NUM_UBOS];
97 static GLint alignment;
98 static bool test_buffer_offset = false;
101 static void
102 setup_ubos(void)
104 static const char *names[NUM_UBOS] = {
105 "ub_pos_size",
106 "ub_color",
107 "ub_rot"
109 static GLubyte zeros[1000] = {0};
110 int i;
112 glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &alignment);
113 printf("GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT = %d\n", alignment);
115 if (test_buffer_offset) {
116 printf("Testing buffer offset %d\n", alignment);
118 else {
119 /* we use alignment as the offset */
120 alignment = 0;
123 glGenBuffers(NUM_UBOS, buffers);
125 for (i = 0; i < NUM_UBOS; i++) {
126 GLint index, size;
128 /* query UBO index */
129 index = glGetUniformBlockIndex(prog, names[i]);
131 /* query UBO size */
132 glGetActiveUniformBlockiv(prog, index,
133 GL_UNIFORM_BLOCK_DATA_SIZE, &size);
135 printf("UBO %s: index = %d, size = %d\n",
136 names[i], index, size);
138 /* Allocate UBO */
139 /* XXX for some reason, this test doesn't work at all with
140 * nvidia if we pass NULL instead of zeros here. The UBO data
141 * is set/overwritten in the piglit_display() function so this
142 * really shouldn't matter.
144 glBindBuffer(GL_UNIFORM_BUFFER, buffers[i]);
145 glBufferData(GL_UNIFORM_BUFFER, size + alignment,
146 zeros, GL_DYNAMIC_DRAW);
148 /* Attach UBO */
149 glBindBufferRange(GL_UNIFORM_BUFFER, i, buffers[i],
150 alignment, /* offset */
151 size);
152 glUniformBlockBinding(prog, index, i);
154 if (!piglit_check_gl_error(GL_NO_ERROR))
155 piglit_report_result(PIGLIT_FAIL);
160 void
161 piglit_init(int argc, char **argv)
163 piglit_require_extension("GL_ARB_uniform_buffer_object");
165 if (argc > 1 && strcmp(argv[1], "offset") == 0) {
166 test_buffer_offset = true;
169 prog = piglit_build_simple_program(vert_shader_text, frag_shader_text);
170 assert(prog);
171 glUseProgram(prog);
173 setup_ubos();
175 glClearColor(0.2, 0.2, 0.2, 0.2);
179 static bool
180 probe(int x, int y, int color_index)
182 float expected[4];
184 /* mul color by color_scale */
185 expected[0] = color[color_index][0] * color[color_index][4];
186 expected[1] = color[color_index][1] * color[color_index][4];
187 expected[2] = color[color_index][2] * color[color_index][4];
188 expected[3] = color[color_index][3] * color[color_index][4];
190 return piglit_probe_pixel_rgba(x, y, expected);
194 enum piglit_result
195 piglit_display(void)
197 bool pass = true;
198 int x0 = piglit_width / 4;
199 int x1 = piglit_width * 3 / 4;
200 int y0 = piglit_height / 4;
201 int y1 = piglit_height * 3 / 4;
202 int i;
204 glViewport(0, 0, piglit_width, piglit_height);
206 glClear(GL_COLOR_BUFFER_BIT);
208 for (i = 0; i < NUM_SQUARES; i++) {
209 /* Load UBO data, at offset=alignment */
210 glBindBuffer(GL_UNIFORM_BUFFER, buffers[0]);
211 glBufferSubData(GL_UNIFORM_BUFFER, alignment, sizeof(pos_size[0]),
212 pos_size[i]);
213 glBindBuffer(GL_UNIFORM_BUFFER, buffers[1]);
214 glBufferSubData(GL_UNIFORM_BUFFER, alignment, sizeof(color[0]),
215 color[i]);
216 glBindBuffer(GL_UNIFORM_BUFFER, buffers[2]);
217 glBufferSubData(GL_UNIFORM_BUFFER, alignment, sizeof(rotation[0]),
218 &rotation[i]);
220 if (!piglit_check_gl_error(GL_NO_ERROR))
221 return PIGLIT_FAIL;
223 piglit_draw_rect(-1, -1, 2, 2);
226 pass = probe(x0, y0, 0) && pass;
227 pass = probe(x1, y0, 1) && pass;
228 pass = probe(x0, y1, 2) && pass;
229 pass = probe(x1, y1, 3) && pass;
231 piglit_present_results();
233 return pass ? PIGLIT_PASS : PIGLIT_FAIL;