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
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.
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"
43 "layout(std140) uniform;\n"
44 "uniform ub_pos_size { vec2 pos; float size; };\n"
45 "uniform ub_rot {float rotation; };\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"
57 static const char frag_shader_text
[] =
58 "#extension GL_ARB_uniform_buffer_object : require\n"
60 "layout(std140) uniform;\n"
61 "uniform ub_color { vec4 color; float color_scale; };\n"
65 " gl_FragColor = color * color_scale;\n"
71 /* Square positions and sizes */
72 static const float pos_size
[NUM_SQUARES
][3] = {
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
] = {
96 static GLuint buffers
[NUM_UBOS
];
97 static GLint alignment
;
98 static bool test_buffer_offset
= false;
104 static const char *names
[NUM_UBOS
] = {
109 static GLubyte zeros
[1000] = {0};
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
);
119 /* we use alignment as the offset */
123 glGenBuffers(NUM_UBOS
, buffers
);
125 for (i
= 0; i
< NUM_UBOS
; i
++) {
128 /* query UBO index */
129 index
= glGetUniformBlockIndex(prog
, names
[i
]);
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
);
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
);
149 glBindBufferRange(GL_UNIFORM_BUFFER
, i
, buffers
[i
],
150 alignment
, /* offset */
152 glUniformBlockBinding(prog
, index
, i
);
154 if (!piglit_check_gl_error(GL_NO_ERROR
))
155 piglit_report_result(PIGLIT_FAIL
);
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
);
175 glClearColor(0.2, 0.2, 0.2, 0.2);
180 probe(int x
, int y
, int color_index
)
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
);
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;
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]),
213 glBindBuffer(GL_UNIFORM_BUFFER
, buffers
[1]);
214 glBufferSubData(GL_UNIFORM_BUFFER
, alignment
, sizeof(color
[0]),
216 glBindBuffer(GL_UNIFORM_BUFFER
, buffers
[2]);
217 glBufferSubData(GL_UNIFORM_BUFFER
, alignment
, sizeof(rotation
[0]),
220 if (!piglit_check_gl_error(GL_NO_ERROR
))
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
;