ext_external_objects: avoid stderr when we skip the test
[piglit.git] / tests / spec / ext_external_objects / vk_semaphores.c
bloba83d8465f045f7992c5adc9fcbc75c813a86038d
1 /*
2 * Copyright © 2021 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-gl.h>
28 #include "interop.h"
29 #include "params.h"
30 #include "helpers.h"
31 PIGLIT_GL_TEST_CONFIG_BEGIN
33 config.supports_gl_compat_version = 30;
34 config.window_visual = PIGLIT_GL_VISUAL_RGBA | PIGLIT_GL_VISUAL_DOUBLE;
35 config.khr_no_error_support = PIGLIT_HAS_ERRORS;
37 PIGLIT_GL_TEST_CONFIG_END
39 static const char vs[] =
40 "#version 130\n"
41 "in vec4 piglit_vertex;\n"
42 "in vec2 piglit_texcoord;\n"
43 "out vec2 tex_coords;\n"
44 "void main()\n"
45 "{\n"
46 " gl_Position = piglit_vertex;\n"
47 " tex_coords = piglit_texcoord;\n"
48 "}\n";
50 static const char fs[] =
51 "#version 130\n"
52 "in vec2 tex_coords;\n"
53 "uniform sampler2D tex; \n"
54 "out vec4 color;\n"
55 "void main() \n"
56 "{\n"
57 " color = texture(tex, tex_coords);\n"
58 "}\n";
60 static bool
61 vk_init(uint32_t w,
62 uint32_t h,
63 uint32_t d,
64 uint32_t num_samples,
65 uint32_t num_levels,
66 uint32_t num_layers,
67 VkFormat color_format,
68 VkFormat depth_format,
69 VkImageTiling color_tiling,
70 VkImageTiling depth_tiling,
71 VkImageLayout color_in_layout,
72 VkImageLayout depth_in_layout,
73 VkImageLayout color_end_layout,
74 VkImageLayout depth_end_layout);
76 static void
77 cleanup(void);
79 static void
80 vk_cleanup(void);
82 static bool
83 gl_init();
85 static struct vk_ctx vk_core;
86 static struct vk_image_att vk_color_att;
87 static struct vk_image_att vk_depth_att;
88 static struct vk_renderer vk_rnd;
90 static GLenum gl_target = GL_TEXTURE_2D;
91 static GLenum gl_tex_storage_format = GL_RGBA32F;
92 static GLuint gl_tex;
93 static GLint gl_prog;
94 static GLuint gl_mem_obj;
96 static struct gl_ext_semaphores gl_sem;
97 static struct vk_semaphores vk_sem;
98 static bool vk_sem_has_wait = true;
99 static bool vk_sem_has_signal = true;
101 static float vk_fb_color[4] = { 1.0, 0.0, 0.0, 1.0 };
103 void piglit_init(int argc, char **argv)
105 piglit_require_extension("GL_ARB_texture_storage");
106 piglit_require_extension("GL_EXT_memory_object");
107 piglit_require_extension("GL_EXT_memory_object_fd");
108 piglit_require_extension("GL_EXT_semaphore");
109 piglit_require_extension("GL_EXT_semaphore_fd");
111 atexit(cleanup);
113 if (!vk_init(piglit_width, piglit_height, 1, num_samples, num_levels, num_layers,
114 color_format, depth_format,
115 color_tiling, depth_tiling,
116 color_in_layout, depth_in_layout,
117 color_end_layout, depth_end_layout)) {
118 fprintf(stdout, "Failed to initialize Vulkan, skipping the test.\n");
119 piglit_report_result(PIGLIT_SKIP);
121 /* create memory object and gl texture */
122 if (!gl_create_mem_obj_from_vk_mem(&vk_core, &vk_color_att.obj.mobj,
123 &gl_mem_obj)) {
124 fprintf(stderr, "Failed to create GL memory object from Vulkan memory.\n");
125 piglit_report_result(PIGLIT_FAIL);
128 if (!gl_gen_tex_from_mem_obj(&vk_color_att.props,
129 gl_tex_storage_format,
130 gl_mem_obj, 0, &gl_tex)) {
131 fprintf(stderr, "Failed to create texture from GL memory object.\n");
132 piglit_report_result(PIGLIT_FAIL);
135 if (!gl_create_semaphores_from_vk(&vk_core, &vk_sem, &gl_sem)) {
136 fprintf(stderr, "Failed to import semaphores from Vulkan.\n");
137 piglit_report_result(PIGLIT_FAIL);
140 if (!gl_init()) {
141 fprintf(stderr, "Failed to initialize structs for GL rendering.\n");
142 piglit_report_result(PIGLIT_FAIL);
145 /* one initial vk rendering to fill the texture */
146 GLuint layout = gl_get_layout_from_vk(color_in_layout);
147 if (vk_sem_has_wait) {
148 glSignalSemaphoreEXT(gl_sem.gl_frame_ready, 0, 0, 1,
149 &gl_tex, &layout);
150 glFlush();
153 struct vk_image_att images[] = { vk_color_att, vk_depth_att };
154 vk_draw(&vk_core, 0, &vk_rnd, vk_fb_color, 4, &vk_sem,
155 vk_sem_has_wait, vk_sem_has_signal, images, ARRAY_SIZE(images), 0, 0,
156 piglit_width, piglit_height);
158 layout = gl_get_layout_from_vk(color_end_layout);
159 if (vk_sem_has_signal) {
160 glWaitSemaphoreEXT(gl_sem.vk_frame_done, 0, 0, 1,
161 &gl_tex, &layout);
162 glFlush();
165 /* no gl rendering takes place before display */
168 enum piglit_result
169 piglit_display(void)
171 enum piglit_result res = PIGLIT_PASS;
172 int i;
173 float colors[6][4] = {
174 {1.0, 0.0, 0.0, 1.0},
175 {0.0, 1.0, 0.0, 1.0},
176 {0.0, 0.0, 1.0, 1.0},
177 {1.0, 1.0, 0.0, 1.0},
178 {1.0, 0.0, 1.0, 1.0},
179 {0.0, 1.0, 1.0, 1.0}
181 GLuint layout = gl_get_layout_from_vk(color_in_layout);
183 if (vk_sem_has_wait) {
184 glSignalSemaphoreEXT(gl_sem.gl_frame_ready, 0, 0, 1,
185 &gl_tex, &layout);
186 glFlush();
189 struct vk_image_att images[] = { vk_color_att, vk_depth_att };
190 vk_clear_color(&vk_core, 0, &vk_rnd, vk_fb_color, 4, &vk_sem,
191 vk_sem_has_wait, vk_sem_has_signal, images,
192 ARRAY_SIZE(images), 0, 0, piglit_width, piglit_height);
193 layout = gl_get_layout_from_vk(color_end_layout);
194 if (vk_sem_has_signal) {
195 glWaitSemaphoreEXT(gl_sem.vk_frame_done, 0, 0, 1,
196 &gl_tex, &layout);
197 glFlush();
200 /* OpenGL rendering */
201 glBindTexture(gl_target, gl_tex);
202 piglit_draw_rect_tex(-1, -1,
205 0, 0, 1, 1);
207 for (i = 0; i < 6; i++) {
208 float x = i * (float)piglit_width / 6.0 + (float)piglit_width / 12.0;
209 float y = (float)piglit_height / 2.0;
211 if (!piglit_probe_pixel_rgba(x, y, colors[0]))
212 res = PIGLIT_FAIL;
215 piglit_present_results();
216 return res;
219 static bool
220 vk_init(uint32_t w,
221 uint32_t h,
222 uint32_t d,
223 uint32_t num_samples,
224 uint32_t num_levels,
225 uint32_t num_layers,
226 VkFormat color_format,
227 VkFormat depth_format,
228 VkImageTiling color_tiling,
229 VkImageTiling depth_tiling,
230 VkImageLayout color_in_layout,
231 VkImageLayout depth_in_layout,
232 VkImageLayout color_end_layout,
233 VkImageLayout depth_end_layout)
235 char *vs_src = 0;
236 char *fs_src = 0;
237 unsigned int vs_sz;
238 unsigned int fs_sz;
240 if (!vk_init_ctx_for_rendering(&vk_core)) {
241 fprintf(stderr, "Failed to create Vulkan context.\n");
242 return false;
245 if (!vk_check_gl_compatibility(&vk_core)) {
246 fprintf(stderr, "Mismatch in driver/device UUID\n");
247 return false;
250 /* creating external images */
251 /* color image */
252 if (!vk_fill_ext_image_props(&vk_core,
253 w, h, d,
254 num_samples,
255 num_levels,
256 num_layers,
257 color_format,
258 color_tiling,
259 color_in_layout,
260 color_end_layout,
261 true,
262 &vk_color_att.props)) {
263 fprintf(stderr, "Unsupported color image properties.\n");
264 return false;
266 if (!vk_create_ext_image(&vk_core, &vk_color_att.props, &vk_color_att.obj)) {
267 fprintf(stderr, "Failed to create color image.\n");
268 return false;
271 /* depth image */
272 if (!vk_fill_ext_image_props(&vk_core,
273 w, h, d,
274 num_samples,
275 num_levels,
276 num_layers,
277 depth_format,
278 depth_tiling,
279 depth_in_layout,
280 depth_end_layout,
281 false,
282 &vk_depth_att.props)) {
283 fprintf(stderr, "Unsupported depth image properties.\n");
284 return false;
287 if (!vk_create_ext_image(&vk_core, &vk_depth_att.props, &vk_depth_att.obj)) {
288 fprintf(stderr, "Failed to create depth image.\n");
289 goto fail;
292 /* load shaders */
293 if (!(vs_src = load_shader(VK_BANDS_VERT, &vs_sz)))
294 goto fail;
296 if (!(fs_src = load_shader(VK_BANDS_FRAG, &fs_sz)))
297 goto fail;
299 /* create Vulkan renderer */
300 if (!vk_create_renderer(&vk_core, vs_src, vs_sz, fs_src, fs_sz,
301 false, false,
302 &vk_color_att, &vk_depth_att, 0, &vk_rnd)) {
303 fprintf(stderr, "Failed to create Vulkan renderer.\n");
304 goto fail;
307 if (!vk_create_semaphores(&vk_core, &vk_sem)) {
308 fprintf(stderr, "Failed to create semaphores.\n");
309 goto fail;
312 free(vs_src);
313 free(fs_src);
315 return true;
317 fail:
318 free(vs_src);
319 free(fs_src);
321 return false;
324 static void
325 vk_cleanup(void)
327 vk_destroy_ext_image(&vk_core, &vk_color_att.obj);
328 vk_destroy_ext_image(&vk_core, &vk_depth_att.obj);
330 vk_destroy_renderer(&vk_core, &vk_rnd);
331 vk_destroy_semaphores(&vk_core, &vk_sem);
333 vk_cleanup_ctx(&vk_core);
336 static void
337 cleanup(void)
339 vk_cleanup();
342 static bool
343 gl_init()
345 gl_prog = piglit_build_simple_program(vs, fs);
346 glUseProgram(gl_prog);
348 glClear(GL_COLOR_BUFFER_BIT);
349 return glGetError() == GL_NO_ERROR;