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.
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
[] =
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"
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"
57 static const char frag_shader_text
[] =
59 "layout(std140) uniform;\n"
60 "uniform ub_color { vec4 color; float color_scale; };\n"
65 " col = 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
] = {
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
);
118 /* we use alignment as the offset */
122 glCreateBuffers(NUM_UBOS
, buffers
);
124 for (i
= 0; i
< NUM_UBOS
; i
++) {
127 /* query UBO index */
128 index
= glGetUniformBlockIndex(prog
, names
[i
]);
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
);
138 glNamedBufferData(buffers
[i
], size
+ alignment
,
139 NULL
, GL_DYNAMIC_DRAW
);
142 glBindBufferRange(GL_UNIFORM_BUFFER
, i
, buffers
[i
],
143 alignment
, /* offset */
145 glUniformBlockBinding(prog
, index
, i
);
147 if (!piglit_check_gl_error(GL_NO_ERROR
))
148 piglit_report_result(PIGLIT_FAIL
);
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
);
168 glClearColor(0.2, 0.2, 0.2, 0.2);
173 probe(int x
, int y
, int color_index
)
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
);
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;
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]),
205 glNamedBufferSubData(buffers
[1], alignment
, sizeof(color
[0]),
207 glNamedBufferSubData(buffers
[2], alignment
, sizeof(rotation
[0]),
210 if (!piglit_check_gl_error(GL_NO_ERROR
))
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
;