framework/replay: disable AA accounting when comparing with no tolerance
[piglit.git] / tests / spec / ext_external_objects / vk_vert_buf_update_errors.c
blobf0af16854729ac69be29292ee35ca4a84d964f63
1 /*
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
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.
23 * Author:
24 * Eleni Maria Stea <estea@igalia.com>
27 #include <piglit-util.h>
28 #include "interop.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;
50 struct Vec2 {
51 float x;
52 float y;
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[] =
67 "#version 130\n"
68 "in vec2 vertex;\n"
69 "void main()\n"
70 "{\n"
71 " gl_Position = vec4(vertex, 0.0, 1.0);\n"
72 "}\n";
74 static const char fs[] =
75 "#version 130\n"
76 "out vec4 color;\n"
77 "void main() \n"
78 "{\n"
79 " color = vec4(0.0, 0.0, 1.0, 1.0);\n"
80 "}\n";
82 void
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");
90 atexit(cleanup);
92 if (!vk_init()) {
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);
107 if (!gl_init()) {
108 fprintf(stderr, "Failed to initialize OpenGL\n");
109 piglit_report_result(PIGLIT_FAIL);
113 static enum piglit_result
114 check_chessboard_pattern()
116 int i, j;
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]))
128 return PIGLIT_FAIL;
132 return PIGLIT_PASS;
135 enum piglit_result
136 piglit_display(void)
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
152 * geometry.
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");
173 return PIGLIT_FAIL;
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");
183 return res;
186 piglit_present_results();
187 return res;
190 static void
191 vk_cleanup()
193 vk_destroy_buffer(&vk_core, &vk_vb);
194 vk_cleanup_ctx(&vk_core);
197 static void
198 gl_cleanup()
200 glDeleteProgram(gl_prog);
201 glDeleteMemoryObjectsEXT(1, &gl_memobj);
202 glDeleteBuffers(1, &gl_vk_vb);
205 static void
206 cleanup()
208 vk_cleanup();
209 gl_cleanup();
212 static bool
213 vk_init()
215 if (!vk_init_ctx(&vk_core)) {
216 fprintf(stderr, "Failed to initialize Vulkan context.\n");
217 return false;
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");
222 return false;
225 /* Filling the Vulkan vertex buffer with vdata */
226 struct Vec2 *pdata;
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);
235 return true;
238 static bool
239 gl_init()
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);
245 return true;
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)
250 static void
251 gen_checkerboard_quads(struct Vec2 *vptr)
253 int i, j;
254 struct Vec2 pos;
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;