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
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-gl.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
[] =
41 "in vec4 piglit_vertex;\n"
42 "in vec2 piglit_texcoord;\n"
43 "out vec2 tex_coords;\n"
46 " gl_Position = piglit_vertex;\n"
47 " tex_coords = piglit_texcoord;\n"
50 static const char fs
[] =
52 "in vec2 tex_coords;\n"
53 "uniform sampler2D tex; \n"
57 " color = texture(tex, tex_coords);\n"
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
);
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
;
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");
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
,
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
);
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,
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,
165 /* no gl rendering takes place before display */
171 enum piglit_result res
= PIGLIT_PASS
;
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},
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,
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,
200 /* OpenGL rendering */
201 glBindTexture(gl_target
, gl_tex
);
202 piglit_draw_rect_tex(-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]))
215 piglit_present_results();
223 uint32_t num_samples
,
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
)
240 if (!vk_init_ctx_for_rendering(&vk_core
)) {
241 fprintf(stderr
, "Failed to create Vulkan context.\n");
245 if (!vk_check_gl_compatibility(&vk_core
)) {
246 fprintf(stderr
, "Mismatch in driver/device UUID\n");
250 /* creating external images */
252 if (!vk_fill_ext_image_props(&vk_core
,
262 &vk_color_att
.props
)) {
263 fprintf(stderr
, "Unsupported color image properties.\n");
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");
272 if (!vk_fill_ext_image_props(&vk_core
,
282 &vk_depth_att
.props
)) {
283 fprintf(stderr
, "Unsupported depth image properties.\n");
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");
293 if (!(vs_src
= load_shader(VK_BANDS_VERT
, &vs_sz
)))
296 if (!(fs_src
= load_shader(VK_BANDS_FRAG
, &fs_sz
)))
299 /* create Vulkan renderer */
300 if (!vk_create_renderer(&vk_core
, vs_src
, vs_sz
, fs_src
, fs_sz
,
302 &vk_color_att
, &vk_depth_att
, 0, &vk_rnd
)) {
303 fprintf(stderr
, "Failed to create Vulkan renderer.\n");
307 if (!vk_create_semaphores(&vk_core
, &vk_sem
)) {
308 fprintf(stderr
, "Failed to create semaphores.\n");
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
);
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
;