ext_gpu_shader4: add compiler tests for everything
[piglit.git] / tests / spec / arb_uniform_buffer_object / rendering-dsa.c
blob599a6c30494d148d800216c2a7a8d62fcb052ed5
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-dsa.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_core_version = 31;
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 "#version 140\n"
42 "in vec4 piglit_vertex;\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 * piglit_vertex.xy * vec2(size) + pos;\n"
54 " gl_Position.zw = vec2(0, 1);\n"
55 "}\n";
57 static const char frag_shader_text[] =
58 "#version 140\n"
59 "layout(std140) uniform;\n"
60 "uniform ub_color { vec4 color; float color_scale; };\n"
61 "out vec4 col;\n"
62 "\n"
63 "void main()\n"
64 "{\n"
65 " col = 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 int i;
111 glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &alignment);
112 printf("GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT = %d\n", alignment);
114 if (test_buffer_offset) {
115 printf("Testing buffer offset %d\n", alignment);
117 else {
118 /* we use alignment as the offset */
119 alignment = 0;
122 glCreateBuffers(NUM_UBOS, buffers);
124 for (i = 0; i < NUM_UBOS; i++) {
125 GLint index, size;
127 /* query UBO index */
128 index = glGetUniformBlockIndex(prog, names[i]);
130 /* query UBO size */
131 glGetActiveUniformBlockiv(prog, index,
132 GL_UNIFORM_BLOCK_DATA_SIZE, &size);
134 printf("UBO %s: index = %d, size = %d\n",
135 names[i], index, size);
137 /* Allocate UBO */
138 glNamedBufferData(buffers[i], size + alignment,
139 NULL, GL_DYNAMIC_DRAW);
141 /* Attach UBO */
142 glBindBufferRange(GL_UNIFORM_BUFFER, i, buffers[i],
143 alignment, /* offset */
144 size);
145 glUniformBlockBinding(prog, index, i);
147 if (!piglit_check_gl_error(GL_NO_ERROR))
148 piglit_report_result(PIGLIT_FAIL);
153 void
154 piglit_init(int argc, char **argv)
156 piglit_require_extension("GL_ARB_direct_state_access");
158 if (argc > 1 && strcmp(argv[1], "offset") == 0) {
159 test_buffer_offset = true;
162 prog = piglit_build_simple_program(vert_shader_text, frag_shader_text);
163 assert(prog);
164 glUseProgram(prog);
166 setup_ubos();
168 glClearColor(0.2, 0.2, 0.2, 0.2);
172 static bool
173 probe(int x, int y, int color_index)
175 float expected[4];
177 /* mul color by color_scale */
178 expected[0] = color[color_index][0] * color[color_index][4];
179 expected[1] = color[color_index][1] * color[color_index][4];
180 expected[2] = color[color_index][2] * color[color_index][4];
181 expected[3] = color[color_index][3] * color[color_index][4];
183 return piglit_probe_pixel_rgba(x, y, expected);
187 enum piglit_result
188 piglit_display(void)
190 bool pass = true;
191 int x0 = piglit_width / 4;
192 int x1 = piglit_width * 3 / 4;
193 int y0 = piglit_height / 4;
194 int y1 = piglit_height * 3 / 4;
195 int i;
197 glViewport(0, 0, piglit_width, piglit_height);
199 glClear(GL_COLOR_BUFFER_BIT);
201 for (i = 0; i < NUM_SQUARES; i++) {
202 /* Load UBO data, at offset=alignment */
203 glNamedBufferSubData(buffers[0], alignment, sizeof(pos_size[0]),
204 pos_size[i]);
205 glNamedBufferSubData(buffers[1], alignment, sizeof(color[0]),
206 color[i]);
207 glNamedBufferSubData(buffers[2], alignment, sizeof(rotation[0]),
208 &rotation[i]);
210 if (!piglit_check_gl_error(GL_NO_ERROR))
211 return PIGLIT_FAIL;
213 piglit_draw_rect(-1, -1, 2, 2);
216 pass = probe(x0, y0, 0) && pass;
217 pass = probe(x1, y0, 1) && pass;
218 pass = probe(x0, y1, 2) && pass;
219 pass = probe(x1, y1, 3) && pass;
221 piglit_present_results();
223 return pass ? PIGLIT_PASS : PIGLIT_FAIL;