2 * Copyright © 2020 Igalia S.L.
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 * Eleni Maria Stea <estea@igalia.com>
27 #include <piglit-util.h>
30 PIGLIT_GL_TEST_CONFIG_BEGIN
32 config
.supports_gl_compat_version
= 30;
33 config
.window_visual
= PIGLIT_GL_VISUAL_RGBA
| PIGLIT_GL_VISUAL_DOUBLE
;
34 config
.khr_no_error_support
= PIGLIT_HAS_ERRORS
;
36 PIGLIT_GL_TEST_CONFIG_END
38 static void cleanup();
39 static void vk_cleanup();
40 static void gl_cleanup();
42 static bool vk_init();
43 static bool gl_init();
45 static struct vk_ctx vk_core
;
46 static struct vk_buf vk_vb
;
47 static VkBufferUsageFlagBits vk_vb_usage
= VK_BUFFER_USAGE_TRANSFER_DST_BIT
|
48 VK_BUFFER_USAGE_TRANSFER_SRC_BIT
|
49 VK_BUFFER_USAGE_VERTEX_BUFFER_BIT
;
55 static const float bgc
[3] = { 1.0, 0.0, 0.0 };
56 static void gen_checkerboard_quads(struct Vec2
*vptr
);
58 #define WHITE_QUADS 32
59 #define WHITE_TRIANGLES (WHITE_QUADS * 2)
60 #define WHITE_VERTS (WHITE_TRIANGLES * 3)
62 static GLuint gl_prog
;
63 static GLuint gl_memobj
;
64 static GLuint gl_vk_vb
;
66 static const char vs
[] =
71 " gl_Position = vec4(vertex, 0.0, 1.0);\n"
74 static const char fs
[] =
79 " color = vec4(0.0, 0.0, 1.0, 1.0);\n"
83 piglit_init(int argc
, char **argv
)
85 piglit_require_extension("GL_EXT_memory_object");
86 piglit_require_extension("GL_EXT_memory_object_fd");
87 piglit_require_extension("GL_ARB_texture_storage");
88 piglit_require_extension("GL_ARB_pixel_buffer_object");
93 fprintf(stderr
, "Failed to initialize Vulkan, skipping the test.\n");
94 piglit_report_result(PIGLIT_SKIP
);
97 if (!gl_create_mem_obj_from_vk_mem(&vk_core
, &vk_vb
.mobj
, &gl_memobj
)) {
98 fprintf(stderr
, "Failed to create GL memory object from Vulkan memory.\n");
99 piglit_report_result(PIGLIT_FAIL
);
102 if (!gl_gen_buf_from_mem_obj(gl_memobj
, GL_ARRAY_BUFFER
, vk_vb
.mobj
.mem_sz
, 0, &gl_vk_vb
)) {
103 fprintf(stderr
, "Failed to create GL buffer from memory object.\n");
104 piglit_report_result(PIGLIT_FAIL
);
108 fprintf(stderr
, "Failed to initialize OpenGL\n");
109 piglit_report_result(PIGLIT_FAIL
);
113 static enum piglit_result
114 check_chessboard_pattern()
117 float expected_color
[2][4] = {
118 {0.0, 0.0, 1.0, 1.0},
119 {1.0, 0.0, 0.0, 1.0},
122 for (i
= 0; i
< 8; i
++) {
123 int y
= i
* piglit_height
/ 8 + piglit_height
/ 16;
124 for (j
= 0; j
< 8; j
++) {
125 int x
= j
* piglit_width
/ 8 + piglit_width
/ 16;
126 int chess
= (i
& 1) ^ (j
& 1);
127 if (!piglit_probe_pixel_rgba(x
, y
, expected_color
[chess
]))
138 enum piglit_result res
= PIGLIT_PASS
;
139 float checkerboard
[WHITE_VERTS
* 2];
141 /* Fill with checkerboard data. */
142 gen_checkerboard_quads((struct Vec2
*) &checkerboard
);
144 glUseProgram(gl_prog
);
146 /* We are going to use the Vulkan allocated vertex buffer
147 * in an OpenGL shader that paints the pixels blue.
148 * As the vertices are set to render quads following a checkerboard
149 * pattern (quad, no geometry, quad, no geometry) we should
150 * see a checkerboard pattern where the color is blue when
151 * we have geometry and red (framebuffer color) where there's no
155 glBindBuffer(GL_ARRAY_BUFFER
, gl_vk_vb
);
156 glVertexAttribPointer(0, 2, GL_FLOAT
, GL_FALSE
, 0, 0);
157 glEnableVertexAttribArray(0);
159 glClear(GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
160 glDrawArrays(GL_TRIANGLES
, 0, WHITE_VERTS
);
161 glBindBuffer(GL_ARRAY_BUFFER
, 0);
163 /* Since Vk buffer is initially empty, expectation is bg color. */
164 piglit_probe_rect_rgb(0, 0, piglit_width
, piglit_height
, bgc
);
166 piglit_present_results();
168 /* Checking that calling glBufferSubData updates buffer succesfully. */
169 glBindBuffer(GL_ARRAY_BUFFER
, gl_vk_vb
);
170 glBufferSubData(GL_ARRAY_BUFFER
, 0, vk_vb
.mobj
.mem_sz
, checkerboard
);
171 if (glGetError() != GL_NO_ERROR
) {
172 fprintf(stderr
, "glBufferSubData should not throw error!\n");
176 /* Render again, check that the result matches checkerboard pattern */
177 glClear(GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
178 glDrawArrays(GL_TRIANGLES
, 0, WHITE_VERTS
);
179 glBindBuffer(GL_ARRAY_BUFFER
, 0);
181 if ((res
= check_chessboard_pattern()) == PIGLIT_FAIL
) {
182 fprintf(stderr
, "Vulkan buffer has not been modified.\n");
186 piglit_present_results();
193 vk_destroy_buffer(&vk_core
, &vk_vb
);
194 vk_cleanup_ctx(&vk_core
);
200 glDeleteProgram(gl_prog
);
201 glDeleteMemoryObjectsEXT(1, &gl_memobj
);
202 glDeleteBuffers(1, &gl_vk_vb
);
215 if (!vk_init_ctx(&vk_core
)) {
216 fprintf(stderr
, "Failed to initialize Vulkan context.\n");
220 if (!vk_create_ext_buffer(&vk_core
, WHITE_VERTS
* sizeof(struct Vec2
), vk_vb_usage
, &vk_vb
)) {
221 fprintf(stderr
, "Failed to create external Vulkan vertex buffer.\n");
225 /* Filling the Vulkan vertex buffer with vdata */
227 if (vkMapMemory(vk_core
.dev
, vk_vb
.mobj
.mem
, 0,
228 vk_vb
.mobj
.mem_sz
, 0, (void**)&pdata
) != VK_SUCCESS
) {
229 fprintf(stderr
, "Failed to map Vulkan buffer memory.\n");
230 piglit_report_result(PIGLIT_FAIL
);
232 memset(pdata
, 0, vk_vb
.mobj
.mem_sz
);
233 vkUnmapMemory(vk_core
.dev
, vk_vb
.mobj
.mem
);
241 glClearColor(bgc
[0], bgc
[1], bgc
[2], 1.0);
242 glClear(GL_COLOR_BUFFER_BIT
);
244 gl_prog
= piglit_build_simple_program(vs
, fs
);
248 #define QUAD_SIZE (2.0 / 8.0)
249 #define ADD_QUAD_VERT(a, b) do {vptr->x = a; vptr->y = b; vptr++;} while(0)
251 gen_checkerboard_quads(struct Vec2
*vptr
)
256 for (i
= 0; i
< 8; i
++) {
257 pos
.x
= -1 + (i
% 2) * QUAD_SIZE
;
258 pos
.y
= -1 + i
* QUAD_SIZE
;
260 for (j
= 0; j
< 4; j
++) {
261 ADD_QUAD_VERT(pos
.x
, pos
.y
);
262 ADD_QUAD_VERT(pos
.x
+ QUAD_SIZE
, pos
.y
);
263 ADD_QUAD_VERT(pos
.x
+ QUAD_SIZE
, pos
.y
+ QUAD_SIZE
);
264 ADD_QUAD_VERT(pos
.x
, pos
.y
);
265 ADD_QUAD_VERT(pos
.x
+ QUAD_SIZE
, pos
.y
+ QUAD_SIZE
);
266 ADD_QUAD_VERT(pos
.x
, pos
.y
+ QUAD_SIZE
);
268 pos
.x
+= QUAD_SIZE
* 2.0;