fbo-mrt-alphatest: Actually require MRTs to be available.
[piglit.git] / tests / spec / ext_external_objects / vk_image_display_multiple_textures.c
blobe7169a49ad5574df14aba702ad150bbd7c39b6d4
1 /*
2 * Copyright © 2021 Eleni Maria Stea
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 <elene.mst@gmail.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;
94 static GLenum gl_target = GL_TEXTURE_2D;
95 static GLenum gl_tex_storage_format = GL_RGBA32F;
97 #define NUM_TEX 4
98 static GLuint gl_textures[NUM_TEX];
99 static GLuint gl_combined_tex;
101 static GLint gl_prog;
102 static GLuint gl_mem_obj;
104 static float vk_fb_color[4] = { 1.0, 1.0, 1.0, 1.0 };
106 void piglit_init(int argc, char **argv)
108 int i;
110 piglit_require_extension("GL_ARB_texture_storage");
111 piglit_require_extension("GL_EXT_memory_object");
112 piglit_require_extension("GL_EXT_memory_object_fd");
113 piglit_require_extension("GL_EXT_semaphore");
114 piglit_require_extension("GL_EXT_semaphore_fd");
116 atexit(cleanup);
118 if (!vk_init(piglit_width, piglit_height, 1, 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 /* create memory object and gl texture */
128 if (!gl_create_mem_obj_from_vk_mem(&vk_core, &vk_color_att.obj.mobj,
129 &gl_mem_obj)) {
130 fprintf(stderr, "Failed to create GL memory object from Vulkan memory.\n");
131 piglit_report_result(PIGLIT_FAIL);
134 struct vk_image_att images[] = { vk_color_att, vk_depth_att };
135 vk_draw(&vk_core, 0, &vk_rnd, vk_fb_color, 4, 0,
136 false, false, images, ARRAY_SIZE(images), 0, 0, piglit_width, piglit_height);
138 /* generate NUM_TEX textures */
139 for (i = 0; i < NUM_TEX; i++) {
140 if (!gl_gen_tex_from_mem_obj(&vk_color_att.props,
141 gl_tex_storage_format,
142 gl_mem_obj, 0, &gl_textures[i])) {
143 fprintf(stderr, "Failed to create texture gl_textures[%d] from GL memory object.\n", i);
144 piglit_report_result(PIGLIT_FAIL);
148 if (!gl_init()) {
149 fprintf(stderr, "Failed to initialize structs for GL rendering.\n");
150 piglit_report_result(PIGLIT_FAIL);
154 enum piglit_result
155 piglit_display(void)
157 enum piglit_result res = PIGLIT_PASS;
158 unsigned int i;
159 float colors[6][4] = {
160 {1.0, 0.0, 0.0, 1.0},
161 {0.0, 1.0, 0.0, 1.0},
162 {0.0, 0.0, 1.0, 1.0},
163 {1.0, 1.0, 0.0, 1.0},
164 {1.0, 0.0, 1.0, 1.0},
165 {0.0, 1.0, 1.0, 1.0}
168 /* bind all textures */
169 for (i = 0; i < NUM_TEX; i++) {
170 glBindTexture(gl_target, gl_textures[i]);
171 switch(i) {
172 case 0:
173 piglit_draw_rect_tex(-1, -1, 1, 1, 0, 0, 1, 1);
174 break;
175 case 1:
176 piglit_draw_rect_tex(-1, 0, 1, 1, 0, 0, 1, 1);
177 break;
178 case 2:
179 piglit_draw_rect_tex(0, 0, 1, 1, 0, 0, 1, 1);
180 break;
181 case 3:
182 piglit_draw_rect_tex(0, -1, 1, 1, 0, 0, 1, 1);
183 break;
184 default:
185 break;
190 for (i = 0; i < 6; i++) {
191 float x = i * (float)piglit_width / 12.0 + (float)piglit_width / 24.0;
192 float y = (float)piglit_height / 4.0;
194 if (!piglit_probe_pixel_rgba(x, y, colors[i])) {
195 res = PIGLIT_FAIL;
196 break;
200 piglit_present_results();
201 return res;
204 static bool
205 vk_init(uint32_t w,
206 uint32_t h,
207 uint32_t d,
208 uint32_t num_samples,
209 uint32_t num_levels,
210 uint32_t num_layers,
211 VkFormat color_format,
212 VkFormat depth_format,
213 VkImageTiling color_tiling,
214 VkImageTiling depth_tiling,
215 VkImageLayout color_in_layout,
216 VkImageLayout depth_in_layout,
217 VkImageLayout color_end_layout,
218 VkImageLayout depth_end_layout)
220 char *vs_src = 0;
221 char *fs_src = 0;
222 unsigned int vs_sz;
223 unsigned int fs_sz;
225 if (!vk_init_ctx_for_rendering(&vk_core)) {
226 fprintf(stderr, "Failed to create Vulkan context.\n");
227 return false;
230 if (!vk_check_gl_compatibility(&vk_core)) {
231 fprintf(stderr, "Mismatch in driver/device UUID\n");
232 return false;
235 /* creating external images */
236 /* color image */
237 if (!vk_fill_ext_image_props(&vk_core,
238 w, h, d,
239 num_samples,
240 num_levels,
241 num_layers,
242 color_format,
243 color_tiling,
244 color_in_layout,
245 color_end_layout,
246 true,
247 &vk_color_att.props)) {
248 fprintf(stderr, "Unsupported color image properties.\n");
249 return false;
251 if (!vk_create_ext_image(&vk_core, &vk_color_att.props, &vk_color_att.obj)) {
252 fprintf(stderr, "Failed to create color image.\n");
253 return false;
256 /* depth image */
257 if (!vk_fill_ext_image_props(&vk_core,
258 w, h, d,
259 num_samples,
260 num_levels,
261 num_layers,
262 depth_format,
263 depth_tiling,
264 depth_in_layout,
265 depth_end_layout,
266 false,
267 &vk_depth_att.props)) {
268 fprintf(stderr, "Unsupported depth image properties.\n");
269 return false;
272 if (!vk_create_ext_image(&vk_core, &vk_depth_att.props, &vk_depth_att.obj)) {
273 fprintf(stderr, "Failed to create depth image.\n");
274 goto fail;
277 /* load shaders */
278 if (!(vs_src = load_shader(VK_BANDS_VERT, &vs_sz)))
279 goto fail;
281 if (!(fs_src = load_shader(VK_BANDS_FRAG, &fs_sz)))
282 goto fail;
284 /* create Vulkan renderer */
285 if (!vk_create_renderer(&vk_core, vs_src, vs_sz, fs_src, fs_sz,
286 false, false,
287 &vk_color_att, &vk_depth_att, 0, &vk_rnd)) {
288 fprintf(stderr, "Failed to create Vulkan renderer.\n");
289 goto fail;
292 free(vs_src);
293 free(fs_src);
295 return true;
297 fail:
298 free(vs_src);
299 free(fs_src);
301 return false;
304 static void
305 vk_cleanup(void)
307 vk_destroy_ext_image(&vk_core, &vk_color_att.obj);
308 vk_destroy_ext_image(&vk_core, &vk_depth_att.obj);
310 vk_destroy_renderer(&vk_core, &vk_rnd);
312 vk_cleanup_ctx(&vk_core);
315 static void
316 cleanup(void)
318 gl_cleanup();
319 vk_cleanup();
322 static bool
323 gl_init()
325 gl_prog = piglit_build_simple_program(vs, fs);
326 glUseProgram(gl_prog);
327 glGenTextures(1, &gl_combined_tex);
329 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
330 return glGetError() == GL_NO_ERROR;
333 static void
334 gl_cleanup(void)
336 glBindTexture(gl_target, 0);
338 glDeleteTextures(NUM_TEX, gl_textures);
339 glDeleteProgram(gl_prog);
341 glDeleteMemoryObjectsEXT(1, &gl_mem_obj);