framework/replay: disable AA accounting when comparing with no tolerance
[piglit.git] / tests / spec / ext_external_objects / vk_buf_exchange.c
blobf564c68d0cbca18c2a72c724cea5bf1724dd134d
1 /*
2 * Copyright © 2020 Intel Corporation
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-gl.h>
28 #include "interop.h"
29 #include "params.h"
30 #include "helpers.h"
32 PIGLIT_GL_TEST_CONFIG_BEGIN
34 config.supports_gl_compat_version = 30;
35 config.window_visual = PIGLIT_GL_VISUAL_RGBA | PIGLIT_GL_VISUAL_DOUBLE;
36 config.khr_no_error_support = PIGLIT_HAS_ERRORS;
38 PIGLIT_GL_TEST_CONFIG_END
40 static const char vs[] =
41 "#version 130\n"
42 "in vec4 piglit_vertex;\n"
43 "in vec2 piglit_texcoord;\n"
44 "out vec2 tex_coords;\n"
45 "void main()\n"
46 "{\n"
47 " gl_Position = piglit_vertex;\n"
48 " tex_coords = piglit_texcoord;\n"
49 "}\n";
51 static const char fs[] =
52 "#version 130\n"
53 "in vec2 tex_coords;\n"
54 "uniform sampler2D tex; \n"
55 "out vec4 color;\n"
56 "void main() \n"
57 "{\n"
58 " color = texture(tex, tex_coords);\n"
59 "}\n";
61 static bool
62 vk_init(uint32_t w,
63 uint32_t h,
64 uint32_t d,
65 uint32_t num_samples,
66 uint32_t num_levels,
67 uint32_t num_layers,
68 VkFormat color_format,
69 VkFormat depth_format,
70 VkImageTiling color_tiling,
71 VkImageTiling depth_tiling,
72 VkImageLayout color_in_layout,
73 VkImageLayout depth_in_layout,
74 VkImageLayout color_end_layout,
75 VkImageLayout depth_end_layout);
77 static void
78 cleanup(void);
80 static void
81 vk_cleanup(void);
83 static bool
84 gl_init();
86 static void
87 gl_cleanup(void);
89 static struct vk_ctx vk_core;
90 static struct vk_image_att vk_color_att;
91 static struct vk_image_att vk_depth_att;
92 static struct vk_renderer vk_rnd;
93 static struct vk_buf vk_bo;
94 static VkBufferUsageFlagBits vk_bo_usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT |
95 VK_BUFFER_USAGE_TRANSFER_SRC_BIT |
96 VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT;
98 static GLuint gl_prog;
99 static GLuint gl_memobj;
100 static GLuint gl_bo;
101 static GLuint gl_tex;
103 static float vk_fb_color[4] = { 1.0, 1.0, 1.0, 1.0 };
105 void
106 piglit_init(int argc, char **argv)
108 piglit_require_extension("GL_EXT_memory_object");
109 piglit_require_extension("GL_EXT_memory_object_fd");
110 piglit_require_extension("GL_ARB_texture_storage");
111 piglit_require_extension("GL_ARB_pixel_buffer_object");
113 atexit(cleanup);
115 w = piglit_width;
116 h = piglit_height;
118 if (!vk_init(w, h, d, num_samples, num_levels, num_layers,
119 color_format, depth_format,
120 color_tiling, depth_tiling,
121 color_in_layout, depth_in_layout,
122 color_end_layout, depth_end_layout)) {
123 fprintf(stderr, "Failed to initialize Vulkan, skipping the test.\n");
124 piglit_report_result(PIGLIT_SKIP);
127 if (!gl_create_mem_obj_from_vk_mem(&vk_core, &vk_bo.mobj, &gl_memobj)) {
128 fprintf(stderr, "Failed to create GL memory object from Vulkan memory.\n");
129 piglit_report_result(PIGLIT_FAIL);
132 if (!gl_gen_buf_from_mem_obj(gl_memobj, GL_PIXEL_UNPACK_BUFFER, vk_bo.mobj.mem_sz, 0, &gl_bo)) {
133 fprintf(stderr, "Failed to create GL buffer from memory object.\n");
134 piglit_report_result(PIGLIT_FAIL);
137 vk_draw(&vk_core, 0, &vk_rnd, vk_fb_color, 4, 0,
138 false, false, NULL, 0, 0, 0, w, h);
140 vk_copy_image_to_buffer(&vk_core, &vk_color_att, &vk_bo, w, h);
142 if (!gl_init()) {
143 fprintf(stderr, "Failed to initialize OpenGL.\n");
144 piglit_report_result(PIGLIT_FAIL);
148 enum piglit_result
149 piglit_display(void)
151 int i;
152 float colors[6][4] = {
153 {1.0, 0.0, 0.0, 1.0},
154 {0.0, 1.0, 0.0, 1.0},
155 {0.0, 0.0, 1.0, 1.0},
156 {1.0, 1.0, 0.0, 1.0},
157 {1.0, 0.0, 1.0, 1.0},
158 {0.0, 1.0, 1.0, 1.0}
161 glUseProgram(gl_prog);
162 glBindTexture(GL_TEXTURE_2D, gl_tex);
164 piglit_draw_rect_tex(-1, -1, 2, 2, 0, 0, 1, 1);
166 for (i = 0; i < 6; i++) {
167 float x = i * (float)piglit_width / 6.0 + (float)piglit_width / 12.0;
168 float y = (float)piglit_height / 2.0;
170 if (!piglit_probe_pixel_rgba(x, y, colors[i])) {
171 piglit_present_results();
172 return PIGLIT_FAIL;
176 piglit_present_results();
178 return PIGLIT_PASS;
181 static bool
182 vk_init(uint32_t w,
183 uint32_t h,
184 uint32_t d,
185 uint32_t num_samples,
186 uint32_t num_levels,
187 uint32_t num_layers,
188 VkFormat color_format,
189 VkFormat depth_format,
190 VkImageTiling color_tiling,
191 VkImageTiling depth_tiling,
192 VkImageLayout color_in_layout,
193 VkImageLayout depth_in_layout,
194 VkImageLayout color_end_layout,
195 VkImageLayout depth_end_layout)
197 char *vs_src = 0;
198 char *fs_src = 0;
199 unsigned int vs_sz;
200 unsigned int fs_sz;
202 if (!vk_init_ctx_for_rendering(&vk_core)) {
203 fprintf(stderr, "Failed to create Vulkan context.\n");
204 return false;
207 if (!vk_check_gl_compatibility(&vk_core)) {
208 fprintf(stderr, "Mismatch in driver/device UUID\n");
209 return false;
212 if (!vk_fill_ext_image_props(&vk_core,
213 w, h, d,
214 num_samples,
215 num_levels,
216 num_layers,
217 color_format,
218 color_tiling,
219 color_in_layout,
220 color_end_layout,
221 false,
222 &vk_color_att.props)) {
223 fprintf(stderr, "Unsupported color image properties.\n");
224 return false;
226 if (!vk_create_ext_image(&vk_core, &vk_color_att.props, &vk_color_att.obj)) {
227 fprintf(stderr, "Failed to create color image.\n");
228 return false;
231 if (!vk_fill_ext_image_props(&vk_core,
232 w, h, d,
233 num_samples,
234 num_levels,
235 num_layers,
236 depth_format,
237 depth_tiling,
238 depth_in_layout,
239 depth_end_layout,
240 false,
241 &vk_depth_att.props)) {
242 fprintf(stderr, "Unsupported depth image properties.\n");
243 return false;
246 if (!vk_create_ext_image(&vk_core, &vk_depth_att.props, &vk_depth_att.obj)) {
247 fprintf(stderr, "Failed to create depth image.\n");
248 goto fail;
251 if (!(vs_src = load_shader(VK_BANDS_VERT, &vs_sz)))
252 goto fail;
254 if (!(fs_src = load_shader(VK_BANDS_FRAG, &fs_sz)))
255 goto fail;
257 if (!vk_create_renderer(&vk_core, vs_src, vs_sz, fs_src, fs_sz,
258 false, false,
259 &vk_color_att, &vk_depth_att, 0, &vk_rnd)) {
260 fprintf(stderr, "Failed to create Vulkan renderer.\n");
261 goto fail;
264 if (!vk_create_ext_buffer(&vk_core, w * h * 4 * sizeof(float), vk_bo_usage, &vk_bo)) {
265 fprintf(stderr, "Failed to create Vulkan buffer.\n");
266 goto fail;
269 free(vs_src);
270 free(fs_src);
272 return true;
274 fail:
275 free(vs_src);
276 free(fs_src);
278 return false;
281 static bool
282 gl_init()
284 gl_prog = piglit_build_simple_program(vs, fs);
286 glClearColor(1.0, 0.0, 0.0, 1.0);
287 glClear(GL_COLOR_BUFFER_BIT);
289 glGenTextures(1, &gl_tex);
290 glBindTexture(GL_TEXTURE_2D, gl_tex);
291 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
292 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
293 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
294 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
296 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, gl_bo);
297 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, w, h, 0, GL_RGBA, GL_FLOAT, 0);
298 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
300 glBindTexture(GL_TEXTURE_2D, 0);
301 return glGetError() == GL_NO_ERROR;
304 static void
305 cleanup(void)
307 gl_cleanup();
308 vk_cleanup();
311 static void
312 vk_cleanup(void)
314 vk_destroy_ext_image(&vk_core, &vk_color_att.obj);
315 vk_destroy_ext_image(&vk_core, &vk_depth_att.obj);
316 vk_destroy_renderer(&vk_core, &vk_rnd);
317 vk_destroy_buffer(&vk_core, &vk_bo);
318 vk_cleanup_ctx(&vk_core);
321 static void
322 gl_cleanup(void)
324 glDeleteProgram(gl_prog);
325 glDeleteMemoryObjectsEXT(1, &gl_memobj);
326 glDeleteBuffers(1, &gl_bo);