Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1 | /* |
Tony-LunarG | 495604b | 2019-06-13 15:32:05 -0600 | [diff] [blame] | 2 | * Copyright (c) 2015-2019 The Khronos Group Inc. |
| 3 | * Copyright (c) 2015-2019 Valve Corporation |
| 4 | * Copyright (c) 2015-2019 LunarG, Inc. |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * |
| 18 | * Author: Jeremy Hayes <jeremy@lunarg.com> |
| 19 | */ |
Richard S. Wright Jr | a782574 | 2021-01-06 16:02:12 -0500 | [diff] [blame] | 20 | |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 21 | #if defined(VK_USE_PLATFORM_XLIB_KHR) || defined(VK_USE_PLATFORM_XCB_KHR) |
| 22 | #include <X11/Xutil.h> |
Joey Bzdek | 15eb070 | 2017-06-07 09:40:36 -0600 | [diff] [blame] | 23 | #elif defined(VK_USE_PLATFORM_WAYLAND_KHR) |
| 24 | #include <linux/input.h> |
Manuel Stoeckl | ac93933 | 2019-06-26 20:55:53 -0400 | [diff] [blame] | 25 | #include "xdg-shell-client-header.h" |
| 26 | #include "xdg-decoration-client-header.h" |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 27 | #endif |
| 28 | |
| 29 | #include <cassert> |
Petr Kraus | bc0ab75 | 2017-12-09 00:22:39 +0100 | [diff] [blame] | 30 | #include <cinttypes> |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 31 | #include <cstdio> |
| 32 | #include <cstdlib> |
| 33 | #include <cstring> |
| 34 | #include <csignal> |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 35 | |
Tony-LunarG | d74734f | 2019-06-04 14:11:43 -0600 | [diff] [blame] | 36 | #include <sstream> |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 37 | #include <iostream> |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 38 | #include <memory> |
| 39 | |
| 40 | #define VULKAN_HPP_NO_EXCEPTIONS |
Shannon McPherson | 2abb699 | 2019-04-05 10:46:16 -0600 | [diff] [blame] | 41 | #define VULKAN_HPP_TYPESAFE_CONVERSION |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 42 | #include <vulkan/vulkan.hpp> |
| 43 | #include <vulkan/vk_sdk_platform.h> |
| 44 | |
| 45 | #include "linmath.h" |
| 46 | |
| 47 | #ifndef NDEBUG |
| 48 | #define VERIFY(x) assert(x) |
| 49 | #else |
| 50 | #define VERIFY(x) ((void)(x)) |
| 51 | #endif |
| 52 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 53 | #define APP_SHORT_NAME "vkcubepp" |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 54 | |
| 55 | // Allow a maximum of two outstanding presentation operations. |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 56 | constexpr uint32_t FRAME_LAG = 2; |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 57 | |
| 58 | #ifdef _WIN32 |
Mark Lobodzinski | 85dbd82 | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 59 | #define ERR_EXIT(err_msg, err_class) \ |
| 60 | do { \ |
| 61 | if (!suppress_popups) MessageBox(nullptr, err_msg, err_class, MB_OK); \ |
| 62 | exit(1); \ |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 63 | } while (0) |
| 64 | #else |
Mark Lobodzinski | 85dbd82 | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 65 | #define ERR_EXIT(err_msg, err_class) \ |
| 66 | do { \ |
Robert Morell | 4ccc652 | 2017-02-01 14:51:00 -0800 | [diff] [blame] | 67 | printf("%s\n", err_msg); \ |
Mark Lobodzinski | 85dbd82 | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 68 | fflush(stdout); \ |
| 69 | exit(1); \ |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 70 | } while (0) |
| 71 | #endif |
| 72 | |
Jeremy Hayes | 9d30478 | 2016-10-09 11:48:12 -0600 | [diff] [blame] | 73 | struct texture_object { |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 74 | vk::Sampler sampler; |
| 75 | |
| 76 | vk::Image image; |
Tony-LunarG | bc9fc05 | 2018-09-21 13:47:06 -0600 | [diff] [blame] | 77 | vk::Buffer buffer; |
Mark Lobodzinski | 2dbc266 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 78 | vk::ImageLayout imageLayout{vk::ImageLayout::eUndefined}; |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 79 | |
| 80 | vk::MemoryAllocateInfo mem_alloc; |
| 81 | vk::DeviceMemory mem; |
| 82 | vk::ImageView view; |
| 83 | |
Mark Lobodzinski | 2dbc266 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 84 | int32_t tex_width{0}; |
| 85 | int32_t tex_height{0}; |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 86 | }; |
| 87 | |
Jeremy Hayes | 9d30478 | 2016-10-09 11:48:12 -0600 | [diff] [blame] | 88 | static char const *const tex_files[] = {"lunarg.ppm"}; |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 89 | |
| 90 | static int validation_error = 0; |
| 91 | |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 92 | struct vktexcube_vs_uniform { |
| 93 | // Must start with MVP |
| 94 | float mvp[4][4]; |
| 95 | float position[12 * 3][4]; |
| 96 | float attr[12 * 3][4]; |
| 97 | }; |
| 98 | |
| 99 | //-------------------------------------------------------------------------------------- |
| 100 | // Mesh and VertexFormat Data |
| 101 | //-------------------------------------------------------------------------------------- |
| 102 | // clang-format off |
| 103 | static const float g_vertex_buffer_data[] = { |
| 104 | -1.0f,-1.0f,-1.0f, // -X side |
| 105 | -1.0f,-1.0f, 1.0f, |
| 106 | -1.0f, 1.0f, 1.0f, |
| 107 | -1.0f, 1.0f, 1.0f, |
| 108 | -1.0f, 1.0f,-1.0f, |
| 109 | -1.0f,-1.0f,-1.0f, |
| 110 | |
| 111 | -1.0f,-1.0f,-1.0f, // -Z side |
| 112 | 1.0f, 1.0f,-1.0f, |
| 113 | 1.0f,-1.0f,-1.0f, |
| 114 | -1.0f,-1.0f,-1.0f, |
| 115 | -1.0f, 1.0f,-1.0f, |
| 116 | 1.0f, 1.0f,-1.0f, |
| 117 | |
| 118 | -1.0f,-1.0f,-1.0f, // -Y side |
| 119 | 1.0f,-1.0f,-1.0f, |
| 120 | 1.0f,-1.0f, 1.0f, |
| 121 | -1.0f,-1.0f,-1.0f, |
| 122 | 1.0f,-1.0f, 1.0f, |
| 123 | -1.0f,-1.0f, 1.0f, |
| 124 | |
| 125 | -1.0f, 1.0f,-1.0f, // +Y side |
| 126 | -1.0f, 1.0f, 1.0f, |
| 127 | 1.0f, 1.0f, 1.0f, |
| 128 | -1.0f, 1.0f,-1.0f, |
| 129 | 1.0f, 1.0f, 1.0f, |
| 130 | 1.0f, 1.0f,-1.0f, |
| 131 | |
| 132 | 1.0f, 1.0f,-1.0f, // +X side |
| 133 | 1.0f, 1.0f, 1.0f, |
| 134 | 1.0f,-1.0f, 1.0f, |
| 135 | 1.0f,-1.0f, 1.0f, |
| 136 | 1.0f,-1.0f,-1.0f, |
| 137 | 1.0f, 1.0f,-1.0f, |
| 138 | |
| 139 | -1.0f, 1.0f, 1.0f, // +Z side |
| 140 | -1.0f,-1.0f, 1.0f, |
| 141 | 1.0f, 1.0f, 1.0f, |
| 142 | -1.0f,-1.0f, 1.0f, |
| 143 | 1.0f,-1.0f, 1.0f, |
| 144 | 1.0f, 1.0f, 1.0f, |
| 145 | }; |
| 146 | |
| 147 | static const float g_uv_buffer_data[] = { |
| 148 | 0.0f, 1.0f, // -X side |
| 149 | 1.0f, 1.0f, |
| 150 | 1.0f, 0.0f, |
| 151 | 1.0f, 0.0f, |
| 152 | 0.0f, 0.0f, |
| 153 | 0.0f, 1.0f, |
| 154 | |
| 155 | 1.0f, 1.0f, // -Z side |
| 156 | 0.0f, 0.0f, |
| 157 | 0.0f, 1.0f, |
| 158 | 1.0f, 1.0f, |
| 159 | 1.0f, 0.0f, |
| 160 | 0.0f, 0.0f, |
| 161 | |
| 162 | 1.0f, 0.0f, // -Y side |
| 163 | 1.0f, 1.0f, |
| 164 | 0.0f, 1.0f, |
| 165 | 1.0f, 0.0f, |
| 166 | 0.0f, 1.0f, |
| 167 | 0.0f, 0.0f, |
| 168 | |
| 169 | 1.0f, 0.0f, // +Y side |
| 170 | 0.0f, 0.0f, |
| 171 | 0.0f, 1.0f, |
| 172 | 1.0f, 0.0f, |
| 173 | 0.0f, 1.0f, |
| 174 | 1.0f, 1.0f, |
| 175 | |
| 176 | 1.0f, 0.0f, // +X side |
| 177 | 0.0f, 0.0f, |
| 178 | 0.0f, 1.0f, |
| 179 | 0.0f, 1.0f, |
| 180 | 1.0f, 1.0f, |
| 181 | 1.0f, 0.0f, |
| 182 | |
| 183 | 0.0f, 0.0f, // +Z side |
| 184 | 0.0f, 1.0f, |
| 185 | 1.0f, 0.0f, |
| 186 | 0.0f, 1.0f, |
| 187 | 1.0f, 1.0f, |
| 188 | 1.0f, 0.0f, |
| 189 | }; |
Jeremy Hayes | 9d30478 | 2016-10-09 11:48:12 -0600 | [diff] [blame] | 190 | // clang-format on |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 191 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 192 | struct SwapchainImageResources { |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 193 | vk::Image image; |
| 194 | vk::CommandBuffer cmd; |
| 195 | vk::CommandBuffer graphics_to_present_cmd; |
| 196 | vk::ImageView view; |
Jeremy Hayes | 00399e3 | 2017-06-14 15:07:32 -0600 | [diff] [blame] | 197 | vk::Buffer uniform_buffer; |
| 198 | vk::DeviceMemory uniform_memory; |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 199 | void *uniform_memory_ptr = nullptr; |
Jeremy Hayes | 00399e3 | 2017-06-14 15:07:32 -0600 | [diff] [blame] | 200 | vk::Framebuffer framebuffer; |
| 201 | vk::DescriptorSet descriptor_set; |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 202 | }; |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 203 | |
Joey Bzdek | baf6647 | 2017-06-07 09:37:37 -0600 | [diff] [blame] | 204 | struct Demo { |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 205 | void build_image_ownership_cmd(const SwapchainImageResources &swapchain_image_resource); |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 206 | vk::Bool32 check_layers(const std::vector<const char *> &check_names, const std::vector<vk::LayerProperties> &layers); |
Joey Bzdek | baf6647 | 2017-06-07 09:37:37 -0600 | [diff] [blame] | 207 | void cleanup(); |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 208 | void destroy_swapchain_related_resources(); |
Joey Bzdek | baf6647 | 2017-06-07 09:37:37 -0600 | [diff] [blame] | 209 | void create_device(); |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 210 | void destroy_texture(texture_object &tex_objs); |
Joey Bzdek | baf6647 | 2017-06-07 09:37:37 -0600 | [diff] [blame] | 211 | void draw(); |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 212 | void draw_build_cmd(const SwapchainImageResources &swapchain_image_resource); |
| 213 | void prepare_init_cmd(); |
Joey Bzdek | baf6647 | 2017-06-07 09:37:37 -0600 | [diff] [blame] | 214 | void flush_init_cmd(); |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 215 | void init(int argc, char **argv); |
Joey Bzdek | baf6647 | 2017-06-07 09:37:37 -0600 | [diff] [blame] | 216 | void init_connection(); |
| 217 | void init_vk(); |
| 218 | void init_vk_swapchain(); |
| 219 | void prepare(); |
| 220 | void prepare_buffers(); |
| 221 | void prepare_cube_data_buffers(); |
| 222 | void prepare_depth(); |
| 223 | void prepare_descriptor_layout(); |
| 224 | void prepare_descriptor_pool(); |
| 225 | void prepare_descriptor_set(); |
| 226 | void prepare_framebuffers(); |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 227 | vk::ShaderModule prepare_shader_module(const uint32_t *code, size_t size); |
Petr Kraus | 9a4eb6a | 2017-11-30 14:49:20 +0100 | [diff] [blame] | 228 | vk::ShaderModule prepare_vs(); |
Joey Bzdek | baf6647 | 2017-06-07 09:37:37 -0600 | [diff] [blame] | 229 | vk::ShaderModule prepare_fs(); |
| 230 | void prepare_pipeline(); |
| 231 | void prepare_render_pass(); |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 232 | void prepare_texture_image(const char *filename, texture_object &tex_obj, vk::ImageTiling tiling, vk::ImageUsageFlags usage, |
| 233 | vk::MemoryPropertyFlags required_props); |
| 234 | void prepare_texture_buffer(const char *filename, texture_object &tex_obj); |
Joey Bzdek | baf6647 | 2017-06-07 09:37:37 -0600 | [diff] [blame] | 235 | void prepare_textures(); |
Petr Kraus | 9a4eb6a | 2017-11-30 14:49:20 +0100 | [diff] [blame] | 236 | |
Joey Bzdek | baf6647 | 2017-06-07 09:37:37 -0600 | [diff] [blame] | 237 | void resize(); |
Tony-LunarG | 6cebf14 | 2019-09-11 14:32:23 -0600 | [diff] [blame] | 238 | void create_surface(); |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 239 | void set_image_layout(vk::Image image, vk::ImageAspectFlags aspectMask, vk::ImageLayout oldLayout, vk::ImageLayout newLayout, |
| 240 | vk::AccessFlags srcAccessMask, vk::PipelineStageFlags src_stages, vk::PipelineStageFlags dest_stages); |
Joey Bzdek | baf6647 | 2017-06-07 09:37:37 -0600 | [diff] [blame] | 241 | void update_data_buffer(); |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 242 | bool loadTexture(const char *filename, uint8_t *rgba_data, vk::SubresourceLayout &layout, int32_t &width, int32_t &height); |
| 243 | bool memory_type_from_properties(uint32_t typeBits, vk::MemoryPropertyFlags requirements_mask, uint32_t &typeIndex); |
Charles Giessen | 7dc2dd5 | 2022-02-03 13:55:48 -0700 | [diff] [blame] | 244 | vk::SurfaceFormatKHR pick_surface_format(const std::vector<vk::SurfaceFormatKHR> &surface_formats); |
Joey Bzdek | baf6647 | 2017-06-07 09:37:37 -0600 | [diff] [blame] | 245 | |
| 246 | #if defined(VK_USE_PLATFORM_WIN32_KHR) |
| 247 | void run(); |
| 248 | void create_window(); |
| 249 | #elif defined(VK_USE_PLATFORM_XLIB_KHR) |
| 250 | void create_xlib_window(); |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 251 | void handle_xlib_event(const XEvent *event); |
Joey Bzdek | baf6647 | 2017-06-07 09:37:37 -0600 | [diff] [blame] | 252 | void run_xlib(); |
| 253 | #elif defined(VK_USE_PLATFORM_XCB_KHR) |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 254 | void handle_xcb_event(const xcb_generic_event_t *event); |
Joey Bzdek | baf6647 | 2017-06-07 09:37:37 -0600 | [diff] [blame] | 255 | void run_xcb(); |
| 256 | void create_xcb_window(); |
| 257 | #elif defined(VK_USE_PLATFORM_WAYLAND_KHR) |
| 258 | void run(); |
| 259 | void create_window(); |
Nicolas Caramelli | c8be8c8 | 2020-07-13 17:22:09 +0200 | [diff] [blame] | 260 | #elif defined(VK_USE_PLATFORM_DIRECTFB_EXT) |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 261 | void handle_directfb_event(const DFBInputEvent *event); |
Nicolas Caramelli | c8be8c8 | 2020-07-13 17:22:09 +0200 | [diff] [blame] | 262 | void run_directfb(); |
| 263 | void create_directfb_window(); |
Bill Hollings | 0a0625a | 2019-07-15 17:39:18 -0400 | [diff] [blame] | 264 | #elif defined(VK_USE_PLATFORM_METAL_EXT) |
Karl Schultz | 206b1c5 | 2018-04-13 18:02:07 -0600 | [diff] [blame] | 265 | void run(); |
Joey Bzdek | baf6647 | 2017-06-07 09:37:37 -0600 | [diff] [blame] | 266 | #elif defined(VK_USE_PLATFORM_DISPLAY_KHR) |
| 267 | vk::Result create_display_surface(); |
| 268 | void run_display(); |
| 269 | #endif |
| 270 | |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 271 | std::string name = "vkcubepp"; // Name to put on the window/icon |
Joey Bzdek | baf6647 | 2017-06-07 09:37:37 -0600 | [diff] [blame] | 272 | #if defined(VK_USE_PLATFORM_WIN32_KHR) |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 273 | HINSTANCE connection = nullptr; // hInstance - Windows Instance |
| 274 | HWND window = nullptr; // hWnd - window handle |
| 275 | POINT minsize = {0, 0}; // minimum window size |
Joey Bzdek | baf6647 | 2017-06-07 09:37:37 -0600 | [diff] [blame] | 276 | #elif defined(VK_USE_PLATFORM_XLIB_KHR) |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 277 | Window xlib_window = 0; |
| 278 | Atom xlib_wm_delete_window = 0; |
| 279 | Display *display = nullptr; |
Joey Bzdek | baf6647 | 2017-06-07 09:37:37 -0600 | [diff] [blame] | 280 | #elif defined(VK_USE_PLATFORM_XCB_KHR) |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 281 | xcb_window_t xcb_window = 0; |
| 282 | xcb_screen_t *screen = nullptr; |
| 283 | xcb_connection_t *connection = nullptr; |
| 284 | xcb_intern_atom_reply_t *atom_wm_delete_window = nullptr; |
Joey Bzdek | baf6647 | 2017-06-07 09:37:37 -0600 | [diff] [blame] | 285 | #elif defined(VK_USE_PLATFORM_WAYLAND_KHR) |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 286 | wl_display *display = nullptr; |
| 287 | wl_registry *registry = nullptr; |
| 288 | wl_compositor *compositor = nullptr; |
| 289 | wl_surface *window = nullptr; |
| 290 | xdg_wm_base *wm_base = nullptr; |
| 291 | zxdg_decoration_manager_v1 *xdg_decoration_mgr = nullptr; |
| 292 | zxdg_toplevel_decoration_v1 *toplevel_decoration = nullptr; |
| 293 | xdg_surface *window_surface = nullptr; |
| 294 | bool xdg_surface_has_been_configured = false; |
| 295 | xdg_toplevel *window_toplevel = nullptr; |
| 296 | wl_seat *seat = nullptr; |
| 297 | wl_pointer *pointer = nullptr; |
| 298 | wl_keyboard *keyboard = nullptr; |
Nicolas Caramelli | c8be8c8 | 2020-07-13 17:22:09 +0200 | [diff] [blame] | 299 | #elif defined(VK_USE_PLATFORM_DIRECTFB_EXT) |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 300 | IDirectFB *dfb = nullptr; |
| 301 | IDirectFBSurface *window = nullptr; |
| 302 | IDirectFBEventBuffer *event_buffer = nullptr; |
Bill Hollings | 0a0625a | 2019-07-15 17:39:18 -0400 | [diff] [blame] | 303 | #elif defined(VK_USE_PLATFORM_METAL_EXT) |
| 304 | void *caMetalLayer; |
Joey Bzdek | baf6647 | 2017-06-07 09:37:37 -0600 | [diff] [blame] | 305 | #endif |
| 306 | |
| 307 | vk::SurfaceKHR surface; |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 308 | bool prepared = false; |
| 309 | bool use_staging_buffer = false; |
| 310 | bool use_xlib = false; |
| 311 | bool separate_present_queue = false; |
| 312 | int32_t gpu_number = 0; |
Joey Bzdek | baf6647 | 2017-06-07 09:37:37 -0600 | [diff] [blame] | 313 | |
| 314 | vk::Instance inst; |
| 315 | vk::PhysicalDevice gpu; |
| 316 | vk::Device device; |
| 317 | vk::Queue graphics_queue; |
| 318 | vk::Queue present_queue; |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 319 | uint32_t graphics_queue_family_index = 0; |
| 320 | uint32_t present_queue_family_index = 0; |
| 321 | std::array<vk::Semaphore, FRAME_LAG> image_acquired_semaphores; |
| 322 | std::array<vk::Semaphore, FRAME_LAG> draw_complete_semaphores; |
| 323 | std::array<vk::Semaphore, FRAME_LAG> image_ownership_semaphores; |
Joey Bzdek | baf6647 | 2017-06-07 09:37:37 -0600 | [diff] [blame] | 324 | vk::PhysicalDeviceProperties gpu_props; |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 325 | std::vector<vk::QueueFamilyProperties> queue_props; |
Joey Bzdek | baf6647 | 2017-06-07 09:37:37 -0600 | [diff] [blame] | 326 | vk::PhysicalDeviceMemoryProperties memory_properties; |
| 327 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 328 | std::vector<const char *> enabled_instance_extensions; |
| 329 | std::vector<const char *> enabled_layers; |
| 330 | std::vector<const char *> enabled_device_extensions; |
Joey Bzdek | baf6647 | 2017-06-07 09:37:37 -0600 | [diff] [blame] | 331 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 332 | int32_t width = 0; |
| 333 | int32_t height = 0; |
Joey Bzdek | baf6647 | 2017-06-07 09:37:37 -0600 | [diff] [blame] | 334 | vk::Format format; |
| 335 | vk::ColorSpaceKHR color_space; |
| 336 | |
Joey Bzdek | baf6647 | 2017-06-07 09:37:37 -0600 | [diff] [blame] | 337 | vk::SwapchainKHR swapchain; |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 338 | std::vector<SwapchainImageResources> swapchain_image_resources; |
| 339 | vk::PresentModeKHR presentMode = vk::PresentModeKHR::eFifo; |
| 340 | std::array<vk::Fence, FRAME_LAG> fences; |
| 341 | uint32_t frame_index = 0; |
Joey Bzdek | baf6647 | 2017-06-07 09:37:37 -0600 | [diff] [blame] | 342 | |
| 343 | vk::CommandPool cmd_pool; |
| 344 | vk::CommandPool present_cmd_pool; |
| 345 | |
| 346 | struct { |
| 347 | vk::Format format; |
| 348 | vk::Image image; |
| 349 | vk::MemoryAllocateInfo mem_alloc; |
| 350 | vk::DeviceMemory mem; |
| 351 | vk::ImageView view; |
| 352 | } depth; |
| 353 | |
| 354 | static int32_t const texture_count = 1; |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 355 | std::array<texture_object, texture_count> textures; |
Joey Bzdek | baf6647 | 2017-06-07 09:37:37 -0600 | [diff] [blame] | 356 | texture_object staging_texture; |
| 357 | |
| 358 | struct { |
| 359 | vk::Buffer buf; |
| 360 | vk::MemoryAllocateInfo mem_alloc; |
| 361 | vk::DeviceMemory mem; |
| 362 | vk::DescriptorBufferInfo buffer_info; |
| 363 | } uniform_data; |
| 364 | |
| 365 | vk::CommandBuffer cmd; // Buffer for initialization commands |
| 366 | vk::PipelineLayout pipeline_layout; |
| 367 | vk::DescriptorSetLayout desc_layout; |
| 368 | vk::PipelineCache pipelineCache; |
| 369 | vk::RenderPass render_pass; |
| 370 | vk::Pipeline pipeline; |
| 371 | |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 372 | mat4x4 projection_matrix = {}; |
| 373 | mat4x4 view_matrix = {}; |
| 374 | mat4x4 model_matrix = {}; |
Joey Bzdek | baf6647 | 2017-06-07 09:37:37 -0600 | [diff] [blame] | 375 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 376 | float spin_angle = 0.0f; |
| 377 | float spin_increment = 0.0f; |
| 378 | bool pause = false; |
Joey Bzdek | baf6647 | 2017-06-07 09:37:37 -0600 | [diff] [blame] | 379 | |
| 380 | vk::ShaderModule vert_shader_module; |
| 381 | vk::ShaderModule frag_shader_module; |
| 382 | |
| 383 | vk::DescriptorPool desc_pool; |
| 384 | vk::DescriptorSet desc_set; |
| 385 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 386 | std::vector<vk::Framebuffer> framebuffers; |
Joey Bzdek | baf6647 | 2017-06-07 09:37:37 -0600 | [diff] [blame] | 387 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 388 | bool quit = false; |
| 389 | uint32_t curFrame = 0; |
| 390 | uint32_t frameCount = 0; |
| 391 | bool validate = false; |
| 392 | bool use_break = false; |
| 393 | bool suppress_popups = false; |
| 394 | bool force_errors = false; |
Joey Bzdek | baf6647 | 2017-06-07 09:37:37 -0600 | [diff] [blame] | 395 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 396 | uint32_t current_buffer = 0; |
Joey Bzdek | baf6647 | 2017-06-07 09:37:37 -0600 | [diff] [blame] | 397 | }; |
| 398 | |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 399 | #ifdef _WIN32 |
| 400 | // MS-Windows event handling function: |
| 401 | LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); |
| 402 | #endif |
| 403 | |
Karl Schultz | 23cc218 | 2016-11-23 17:15:17 -0700 | [diff] [blame] | 404 | #if defined(VK_USE_PLATFORM_WAYLAND_KHR) |
Joey Bzdek | 15eb070 | 2017-06-07 09:40:36 -0600 | [diff] [blame] | 405 | static void pointer_handle_enter(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface, wl_fixed_t sx, |
| 406 | wl_fixed_t sy) {} |
Karl Schultz | 23cc218 | 2016-11-23 17:15:17 -0700 | [diff] [blame] | 407 | |
Joey Bzdek | 15eb070 | 2017-06-07 09:40:36 -0600 | [diff] [blame] | 408 | static void pointer_handle_leave(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface) {} |
Karl Schultz | 23cc218 | 2016-11-23 17:15:17 -0700 | [diff] [blame] | 409 | |
Joey Bzdek | 15eb070 | 2017-06-07 09:40:36 -0600 | [diff] [blame] | 410 | static void pointer_handle_motion(void *data, struct wl_pointer *pointer, uint32_t time, wl_fixed_t sx, wl_fixed_t sy) {} |
| 411 | |
| 412 | static void pointer_handle_button(void *data, struct wl_pointer *wl_pointer, uint32_t serial, uint32_t time, uint32_t button, |
| 413 | uint32_t state) { |
Joey Bzdek | 33bc5c8 | 2017-06-14 10:33:36 -0600 | [diff] [blame] | 414 | Demo *demo = (Demo *)data; |
Joey Bzdek | 15eb070 | 2017-06-07 09:40:36 -0600 | [diff] [blame] | 415 | if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED) { |
Manuel Stoeckl | ac93933 | 2019-06-26 20:55:53 -0400 | [diff] [blame] | 416 | xdg_toplevel_move(demo->window_toplevel, demo->seat, serial); |
Joey Bzdek | 15eb070 | 2017-06-07 09:40:36 -0600 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | |
| 420 | static void pointer_handle_axis(void *data, struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis, wl_fixed_t value) {} |
| 421 | |
| 422 | static const struct wl_pointer_listener pointer_listener = { |
| 423 | pointer_handle_enter, pointer_handle_leave, pointer_handle_motion, pointer_handle_button, pointer_handle_axis, |
| 424 | }; |
| 425 | |
| 426 | static void keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format, int fd, uint32_t size) {} |
| 427 | |
| 428 | static void keyboard_handle_enter(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface, |
| 429 | struct wl_array *keys) {} |
| 430 | |
| 431 | static void keyboard_handle_leave(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface) {} |
| 432 | |
| 433 | static void keyboard_handle_key(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key, |
| 434 | uint32_t state) { |
| 435 | if (state != WL_KEYBOARD_KEY_STATE_RELEASED) return; |
| 436 | Demo *demo = (Demo *)data; |
| 437 | switch (key) { |
| 438 | case KEY_ESC: // Escape |
| 439 | demo->quit = true; |
| 440 | break; |
| 441 | case KEY_LEFT: // left arrow key |
| 442 | demo->spin_angle -= demo->spin_increment; |
| 443 | break; |
| 444 | case KEY_RIGHT: // right arrow key |
| 445 | demo->spin_angle += demo->spin_increment; |
| 446 | break; |
| 447 | case KEY_SPACE: // space bar |
| 448 | demo->pause = !demo->pause; |
| 449 | break; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | static void keyboard_handle_modifiers(void *data, wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed, |
| 454 | uint32_t mods_latched, uint32_t mods_locked, uint32_t group) {} |
| 455 | |
| 456 | static const struct wl_keyboard_listener keyboard_listener = { |
| 457 | keyboard_handle_keymap, keyboard_handle_enter, keyboard_handle_leave, keyboard_handle_key, keyboard_handle_modifiers, |
| 458 | }; |
| 459 | |
| 460 | static void seat_handle_capabilities(void *data, wl_seat *seat, uint32_t caps) { |
| 461 | // Subscribe to pointer events |
| 462 | Demo *demo = (Demo *)data; |
| 463 | if ((caps & WL_SEAT_CAPABILITY_POINTER) && !demo->pointer) { |
| 464 | demo->pointer = wl_seat_get_pointer(seat); |
| 465 | wl_pointer_add_listener(demo->pointer, &pointer_listener, demo); |
| 466 | } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && demo->pointer) { |
| 467 | wl_pointer_destroy(demo->pointer); |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 468 | demo->pointer = nullptr; |
Joey Bzdek | 15eb070 | 2017-06-07 09:40:36 -0600 | [diff] [blame] | 469 | } |
| 470 | // Subscribe to keyboard events |
| 471 | if (caps & WL_SEAT_CAPABILITY_KEYBOARD) { |
| 472 | demo->keyboard = wl_seat_get_keyboard(seat); |
| 473 | wl_keyboard_add_listener(demo->keyboard, &keyboard_listener, demo); |
| 474 | } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD)) { |
| 475 | wl_keyboard_destroy(demo->keyboard); |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 476 | demo->keyboard = nullptr; |
Joey Bzdek | 15eb070 | 2017-06-07 09:40:36 -0600 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
| 480 | static const wl_seat_listener seat_listener = { |
| 481 | seat_handle_capabilities, |
| 482 | }; |
| 483 | |
Manuel Stoeckl | ac93933 | 2019-06-26 20:55:53 -0400 | [diff] [blame] | 484 | static void wm_base_ping(void *data, xdg_wm_base *xdg_wm_base, uint32_t serial) { xdg_wm_base_pong(xdg_wm_base, serial); } |
| 485 | |
| 486 | static const struct xdg_wm_base_listener wm_base_listener = {wm_base_ping}; |
| 487 | |
Joey Bzdek | 15eb070 | 2017-06-07 09:40:36 -0600 | [diff] [blame] | 488 | static void registry_handle_global(void *data, wl_registry *registry, uint32_t id, const char *interface, uint32_t version) { |
| 489 | Demo *demo = (Demo *)data; |
| 490 | // pickup wayland objects when they appear |
Manuel Stoeckl | ac93933 | 2019-06-26 20:55:53 -0400 | [diff] [blame] | 491 | if (strcmp(interface, wl_compositor_interface.name) == 0) { |
Joey Bzdek | 15eb070 | 2017-06-07 09:40:36 -0600 | [diff] [blame] | 492 | demo->compositor = (wl_compositor *)wl_registry_bind(registry, id, &wl_compositor_interface, 1); |
Manuel Stoeckl | ac93933 | 2019-06-26 20:55:53 -0400 | [diff] [blame] | 493 | } else if (strcmp(interface, xdg_wm_base_interface.name) == 0) { |
| 494 | demo->wm_base = (xdg_wm_base *)wl_registry_bind(registry, id, &xdg_wm_base_interface, 1); |
| 495 | xdg_wm_base_add_listener(demo->wm_base, &wm_base_listener, nullptr); |
| 496 | } else if (strcmp(interface, wl_seat_interface.name) == 0) { |
Joey Bzdek | 15eb070 | 2017-06-07 09:40:36 -0600 | [diff] [blame] | 497 | demo->seat = (wl_seat *)wl_registry_bind(registry, id, &wl_seat_interface, 1); |
| 498 | wl_seat_add_listener(demo->seat, &seat_listener, demo); |
Manuel Stoeckl | ac93933 | 2019-06-26 20:55:53 -0400 | [diff] [blame] | 499 | } else if (strcmp(interface, zxdg_decoration_manager_v1_interface.name) == 0) { |
| 500 | demo->xdg_decoration_mgr = |
| 501 | (zxdg_decoration_manager_v1 *)wl_registry_bind(registry, id, &zxdg_decoration_manager_v1_interface, 1); |
Joey Bzdek | 15eb070 | 2017-06-07 09:40:36 -0600 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | |
| 505 | static void registry_handle_global_remove(void *data, wl_registry *registry, uint32_t name) {} |
| 506 | |
| 507 | static const wl_registry_listener registry_listener = {registry_handle_global, registry_handle_global_remove}; |
Karl Schultz | 23cc218 | 2016-11-23 17:15:17 -0700 | [diff] [blame] | 508 | #endif |
| 509 | |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 510 | void Demo::build_image_ownership_cmd(const SwapchainImageResources &swapchain_image_resource) { |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 511 | auto result = swapchain_image_resource.graphics_to_present_cmd.begin( |
| 512 | vk::CommandBufferBeginInfo().setFlags(vk::CommandBufferUsageFlagBits::eSimultaneousUse)); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 513 | VERIFY(result == vk::Result::eSuccess); |
| 514 | |
| 515 | auto const image_ownership_barrier = |
| 516 | vk::ImageMemoryBarrier() |
| 517 | .setSrcAccessMask(vk::AccessFlags()) |
Tony-LunarG | 4ba65cd | 2018-05-30 14:53:14 -0600 | [diff] [blame] | 518 | .setDstAccessMask(vk::AccessFlags()) |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 519 | .setOldLayout(vk::ImageLayout::ePresentSrcKHR) |
| 520 | .setNewLayout(vk::ImageLayout::ePresentSrcKHR) |
| 521 | .setSrcQueueFamilyIndex(graphics_queue_family_index) |
| 522 | .setDstQueueFamilyIndex(present_queue_family_index) |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 523 | .setImage(swapchain_image_resource.image) |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 524 | .setSubresourceRange(vk::ImageSubresourceRange(vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1)); |
| 525 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 526 | swapchain_image_resource.graphics_to_present_cmd.pipelineBarrier(vk::PipelineStageFlagBits::eBottomOfPipe, |
| 527 | vk::PipelineStageFlagBits::eBottomOfPipe, |
| 528 | vk::DependencyFlagBits(), {}, {}, image_ownership_barrier); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 529 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 530 | result = swapchain_image_resource.graphics_to_present_cmd.end(); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 531 | VERIFY(result == vk::Result::eSuccess); |
| 532 | } |
| 533 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 534 | vk::Bool32 Demo::check_layers(const std::vector<const char *> &check_names, const std::vector<vk::LayerProperties> &layers) { |
| 535 | for (const auto &name : check_names) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 536 | vk::Bool32 found = VK_FALSE; |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 537 | for (const auto &layer : layers) { |
| 538 | if (!strcmp(name, layer.layerName)) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 539 | found = VK_TRUE; |
| 540 | break; |
| 541 | } |
| 542 | } |
| 543 | if (!found) { |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 544 | fprintf(stderr, "Cannot find layer: %s\n", name); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 545 | return 0; |
| 546 | } |
| 547 | } |
| 548 | return VK_TRUE; |
| 549 | } |
| 550 | |
| 551 | void Demo::cleanup() { |
| 552 | prepared = false; |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 553 | auto result = device.waitIdle(); |
| 554 | VERIFY(result == vk::Result::eSuccess); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 555 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 556 | destroy_swapchain_related_resources(); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 557 | // Wait for fences from present operations |
| 558 | for (uint32_t i = 0; i < FRAME_LAG; i++) { |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 559 | device.destroyFence(fences[i]); |
| 560 | device.destroySemaphore(image_acquired_semaphores[i]); |
| 561 | device.destroySemaphore(draw_complete_semaphores[i]); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 562 | if (separate_present_queue) { |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 563 | device.destroySemaphore(image_ownership_semaphores[i]); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 564 | } |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 565 | } |
| 566 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 567 | device.destroySwapchainKHR(swapchain); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 568 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 569 | device.destroy(); |
| 570 | inst.destroySurfaceKHR(surface); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 571 | |
| 572 | #if defined(VK_USE_PLATFORM_XLIB_KHR) |
| 573 | XDestroyWindow(display, xlib_window); |
| 574 | XCloseDisplay(display); |
| 575 | #elif defined(VK_USE_PLATFORM_XCB_KHR) |
| 576 | xcb_destroy_window(connection, xcb_window); |
| 577 | xcb_disconnect(connection); |
| 578 | free(atom_wm_delete_window); |
| 579 | #elif defined(VK_USE_PLATFORM_WAYLAND_KHR) |
| 580 | wl_keyboard_destroy(keyboard); |
| 581 | wl_pointer_destroy(pointer); |
| 582 | wl_seat_destroy(seat); |
Manuel Stoeckl | ac93933 | 2019-06-26 20:55:53 -0400 | [diff] [blame] | 583 | xdg_toplevel_destroy(window_toplevel); |
| 584 | xdg_surface_destroy(window_surface); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 585 | wl_surface_destroy(window); |
Manuel Stoeckl | ac93933 | 2019-06-26 20:55:53 -0400 | [diff] [blame] | 586 | xdg_wm_base_destroy(wm_base); |
| 587 | if (xdg_decoration_mgr) { |
| 588 | zxdg_toplevel_decoration_v1_destroy(toplevel_decoration); |
| 589 | zxdg_decoration_manager_v1_destroy(xdg_decoration_mgr); |
| 590 | } |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 591 | wl_compositor_destroy(compositor); |
| 592 | wl_registry_destroy(registry); |
| 593 | wl_display_disconnect(display); |
Nicolas Caramelli | c8be8c8 | 2020-07-13 17:22:09 +0200 | [diff] [blame] | 594 | #elif defined(VK_USE_PLATFORM_DIRECTFB_EXT) |
| 595 | event_buffer->Release(event_buffer); |
| 596 | window->Release(window); |
| 597 | dfb->Release(dfb); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 598 | #endif |
| 599 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 600 | inst.destroy(); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | void Demo::create_device() { |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 604 | float priorities = 0.0; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 605 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 606 | std::vector<vk::DeviceQueueCreateInfo> queues; |
| 607 | queues.push_back(vk::DeviceQueueCreateInfo().setQueueFamilyIndex(graphics_queue_family_index).setQueuePriorities(priorities)); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 608 | |
| 609 | if (separate_present_queue) { |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 610 | queues.push_back( |
| 611 | vk::DeviceQueueCreateInfo().setQueueFamilyIndex(present_queue_family_index).setQueuePriorities(priorities)); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 612 | } |
| 613 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 614 | auto deviceInfo = vk::DeviceCreateInfo().setQueueCreateInfos(queues).setPEnabledExtensionNames(enabled_device_extensions); |
| 615 | auto device_return = gpu.createDevice(deviceInfo); |
| 616 | VERIFY(device_return.result == vk::Result::eSuccess); |
| 617 | device = device_return.value; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 618 | } |
| 619 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 620 | void Demo::destroy_texture(texture_object &tex_objs) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 621 | // clean up staging resources |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 622 | device.freeMemory(tex_objs.mem); |
| 623 | if (tex_objs.image) device.destroyImage(tex_objs.image); |
| 624 | if (tex_objs.buffer) device.destroyBuffer(tex_objs.buffer); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | void Demo::draw() { |
| 628 | // Ensure no more than FRAME_LAG renderings are outstanding |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 629 | device.waitForFences(fences[frame_index], VK_TRUE, UINT64_MAX); |
Mike Schuchardt | 7bcbfd3 | 2020-05-07 15:33:52 -0700 | [diff] [blame] | 630 | device.resetFences({fences[frame_index]}); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 631 | |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 632 | vk::Result acquire_result; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 633 | do { |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 634 | acquire_result = |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 635 | device.acquireNextImageKHR(swapchain, UINT64_MAX, image_acquired_semaphores[frame_index], vk::Fence(), ¤t_buffer); |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 636 | if (acquire_result == vk::Result::eErrorOutOfDateKHR) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 637 | // demo->swapchain is out of date (e.g. the window was resized) and |
| 638 | // must be recreated: |
| 639 | resize(); |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 640 | } else if (acquire_result == vk::Result::eSuboptimalKHR) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 641 | // swapchain is not as optimal as it could be, but the platform's |
| 642 | // presentation engine will still present the image correctly. |
| 643 | break; |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 644 | } else if (acquire_result == vk::Result::eErrorSurfaceLostKHR) { |
| 645 | inst.destroySurfaceKHR(surface); |
Tony-LunarG | 6cebf14 | 2019-09-11 14:32:23 -0600 | [diff] [blame] | 646 | create_surface(); |
| 647 | resize(); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 648 | } else { |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 649 | VERIFY(acquire_result == vk::Result::eSuccess); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 650 | } |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 651 | } while (acquire_result != vk::Result::eSuccess); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 652 | |
| 653 | update_data_buffer(); |
| 654 | |
| 655 | // Wait for the image acquired semaphore to be signaled to ensure |
| 656 | // that the image won't be rendered to until the presentation |
| 657 | // engine has fully released ownership to the application, and it is |
| 658 | // okay to render to the image. |
| 659 | vk::PipelineStageFlags const pipe_stage_flags = vk::PipelineStageFlagBits::eColorAttachmentOutput; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 660 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 661 | auto submit_result = graphics_queue.submit(vk::SubmitInfo() |
| 662 | .setWaitDstStageMask(pipe_stage_flags) |
| 663 | .setWaitSemaphores(image_acquired_semaphores[frame_index]) |
| 664 | .setCommandBuffers(swapchain_image_resources[current_buffer].cmd) |
| 665 | .setSignalSemaphores(draw_complete_semaphores[frame_index]), |
| 666 | fences[frame_index]); |
| 667 | VERIFY(submit_result == vk::Result::eSuccess); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 668 | |
| 669 | if (separate_present_queue) { |
| 670 | // If we are using separate queues, change image ownership to the |
| 671 | // present queue before presenting, waiting for the draw complete |
| 672 | // semaphore and signalling the ownership released semaphore when |
| 673 | // finished |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 674 | auto change_owner_result = |
| 675 | present_queue.submit(vk::SubmitInfo() |
| 676 | .setWaitDstStageMask(pipe_stage_flags) |
| 677 | .setWaitSemaphores(draw_complete_semaphores[frame_index]) |
| 678 | .setCommandBuffers(swapchain_image_resources[current_buffer].graphics_to_present_cmd) |
| 679 | .setSignalSemaphores(image_ownership_semaphores[frame_index])); |
| 680 | VERIFY(change_owner_result == vk::Result::eSuccess); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | // If we are using separate queues we have to wait for image ownership, |
| 684 | // otherwise wait for draw complete |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 685 | auto present_result = |
| 686 | present_queue.presentKHR(vk::PresentInfoKHR() |
| 687 | .setWaitSemaphores(separate_present_queue ? image_ownership_semaphores[frame_index] |
| 688 | : draw_complete_semaphores[frame_index]) |
| 689 | .setSwapchains(swapchain) |
| 690 | .setImageIndices(current_buffer)); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 691 | frame_index += 1; |
| 692 | frame_index %= FRAME_LAG; |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 693 | if (present_result == vk::Result::eErrorOutOfDateKHR) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 694 | // swapchain is out of date (e.g. the window was resized) and |
| 695 | // must be recreated: |
| 696 | resize(); |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 697 | } else if (present_result == vk::Result::eSuboptimalKHR) { |
Tony-LunarG | a8e9634 | 2021-06-04 15:12:55 -0600 | [diff] [blame] | 698 | // SUBOPTIMAL could be due to resize |
| 699 | vk::SurfaceCapabilitiesKHR surfCapabilities; |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 700 | auto caps_result = gpu.getSurfaceCapabilitiesKHR(surface, &surfCapabilities); |
| 701 | VERIFY(caps_result == vk::Result::eSuccess); |
| 702 | if (surfCapabilities.currentExtent.width != static_cast<uint32_t>(width) || |
| 703 | surfCapabilities.currentExtent.height != static_cast<uint32_t>(height)) { |
Tony-LunarG | a8e9634 | 2021-06-04 15:12:55 -0600 | [diff] [blame] | 704 | resize(); |
| 705 | } |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 706 | } else if (present_result == vk::Result::eErrorSurfaceLostKHR) { |
| 707 | inst.destroySurfaceKHR(surface); |
Tony-LunarG | 6cebf14 | 2019-09-11 14:32:23 -0600 | [diff] [blame] | 708 | create_surface(); |
| 709 | resize(); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 710 | } else { |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 711 | VERIFY(present_result == vk::Result::eSuccess); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 712 | } |
| 713 | } |
| 714 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 715 | void Demo::draw_build_cmd(const SwapchainImageResources &swapchain_image_resource) { |
| 716 | const auto commandBuffer = swapchain_image_resource.cmd; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 717 | vk::ClearValue const clearValues[2] = {vk::ClearColorValue(std::array<float, 4>({{0.2f, 0.2f, 0.2f, 0.2f}})), |
| 718 | vk::ClearDepthStencilValue(1.0f, 0u)}; |
| 719 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 720 | auto result = commandBuffer.begin(vk::CommandBufferBeginInfo().setFlags(vk::CommandBufferUsageFlagBits::eSimultaneousUse)); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 721 | VERIFY(result == vk::Result::eSuccess); |
| 722 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 723 | commandBuffer.beginRenderPass(vk::RenderPassBeginInfo() |
| 724 | .setRenderPass(render_pass) |
| 725 | .setFramebuffer(swapchain_image_resource.framebuffer) |
| 726 | .setRenderArea(vk::Rect2D(vk::Offset2D(0, 0), vk::Extent2D(static_cast<uint32_t>(width), |
| 727 | static_cast<uint32_t>(height)))) |
| 728 | .setClearValueCount(2) |
| 729 | .setPClearValues(clearValues), |
| 730 | vk::SubpassContents::eInline); |
| 731 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 732 | commandBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, pipeline); |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 733 | commandBuffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, pipeline_layout, 0, swapchain_image_resource.descriptor_set, |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 734 | {}); |
Jeremy Kniager | 979b531 | 2019-11-22 14:55:57 -0700 | [diff] [blame] | 735 | float viewport_dimension; |
| 736 | float viewport_x = 0.0f; |
| 737 | float viewport_y = 0.0f; |
| 738 | if (width < height) { |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 739 | viewport_dimension = static_cast<float>(width); |
Jeremy Kniager | 979b531 | 2019-11-22 14:55:57 -0700 | [diff] [blame] | 740 | viewport_y = (height - width) / 2.0f; |
| 741 | } else { |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 742 | viewport_dimension = static_cast<float>(height); |
Jeremy Kniager | 979b531 | 2019-11-22 14:55:57 -0700 | [diff] [blame] | 743 | viewport_x = (width - height) / 2.0f; |
| 744 | } |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 745 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 746 | commandBuffer.setViewport(0, vk::Viewport() |
| 747 | .setX(viewport_x) |
| 748 | .setY(viewport_y) |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 749 | .setWidth(viewport_dimension) |
| 750 | .setHeight(viewport_dimension) |
| 751 | .setMinDepth(0.0f) |
| 752 | .setMaxDepth(1.0f)); |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 753 | |
| 754 | commandBuffer.setScissor(0, vk::Rect2D(vk::Offset2D(0, 0), vk::Extent2D(width, height))); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 755 | commandBuffer.draw(12 * 3, 1, 0, 0); |
| 756 | // Note that ending the renderpass changes the image's layout from |
| 757 | // COLOR_ATTACHMENT_OPTIMAL to PRESENT_SRC_KHR |
| 758 | commandBuffer.endRenderPass(); |
| 759 | |
| 760 | if (separate_present_queue) { |
| 761 | // We have to transfer ownership from the graphics queue family to |
| 762 | // the |
| 763 | // present queue family to be able to present. Note that we don't |
| 764 | // have |
| 765 | // to transfer from present queue family back to graphics queue |
| 766 | // family at |
| 767 | // the start of the next frame because we don't care about the |
| 768 | // image's |
| 769 | // contents at that point. |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 770 | commandBuffer.pipelineBarrier( |
| 771 | vk::PipelineStageFlagBits::eBottomOfPipe, vk::PipelineStageFlagBits::eBottomOfPipe, vk::DependencyFlagBits(), {}, {}, |
Jeremy Hayes | 9d30478 | 2016-10-09 11:48:12 -0600 | [diff] [blame] | 772 | vk::ImageMemoryBarrier() |
| 773 | .setSrcAccessMask(vk::AccessFlags()) |
Tony-LunarG | 4ba65cd | 2018-05-30 14:53:14 -0600 | [diff] [blame] | 774 | .setDstAccessMask(vk::AccessFlags()) |
Jeremy Hayes | 9d30478 | 2016-10-09 11:48:12 -0600 | [diff] [blame] | 775 | .setOldLayout(vk::ImageLayout::ePresentSrcKHR) |
| 776 | .setNewLayout(vk::ImageLayout::ePresentSrcKHR) |
| 777 | .setSrcQueueFamilyIndex(graphics_queue_family_index) |
| 778 | .setDstQueueFamilyIndex(present_queue_family_index) |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 779 | .setImage(swapchain_image_resource.image) |
| 780 | .setSubresourceRange(vk::ImageSubresourceRange(vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1))); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 781 | } |
| 782 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 783 | result = commandBuffer.end(); |
| 784 | VERIFY(result == vk::Result::eSuccess); |
| 785 | } |
| 786 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 787 | void Demo::prepare_init_cmd() { |
| 788 | auto cmd_pool_return = device.createCommandPool(vk::CommandPoolCreateInfo().setQueueFamilyIndex(graphics_queue_family_index)); |
| 789 | VERIFY(cmd_pool_return.result == vk::Result::eSuccess); |
| 790 | cmd_pool = cmd_pool_return.value; |
| 791 | |
| 792 | auto cmd_return = device.allocateCommandBuffers(vk::CommandBufferAllocateInfo() |
| 793 | .setCommandPool(cmd_pool) |
| 794 | .setLevel(vk::CommandBufferLevel::ePrimary) |
| 795 | .setCommandBufferCount(1)); |
| 796 | VERIFY(cmd_return.result == vk::Result::eSuccess); |
| 797 | cmd = cmd_return.value[0]; |
| 798 | |
| 799 | auto result = cmd.begin(vk::CommandBufferBeginInfo()); |
| 800 | VERIFY(result == vk::Result::eSuccess); |
| 801 | } |
| 802 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 803 | void Demo::flush_init_cmd() { |
| 804 | // TODO: hmm. |
| 805 | // This function could get called twice if the texture uses a staging |
| 806 | // buffer |
| 807 | // In that case the second call should be ignored |
| 808 | if (!cmd) { |
| 809 | return; |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 810 | } |
| 811 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 812 | auto result = cmd.end(); |
| 813 | VERIFY(result == vk::Result::eSuccess); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 814 | |
Mark Young | 43c08f8 | 2021-11-30 16:38:25 -0700 | [diff] [blame] | 815 | auto fenceInfo = vk::FenceCreateInfo(); |
| 816 | if (force_errors) { |
| 817 | // Remove sType to intentionally force validation layer errors. |
| 818 | fenceInfo.sType = vk::StructureType::eRenderPassBeginInfo; |
| 819 | } |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 820 | auto fence_return = device.createFence(fenceInfo); |
| 821 | VERIFY(fence_return.result == vk::Result::eSuccess); |
| 822 | auto fence = fence_return.value; |
| 823 | |
| 824 | result = graphics_queue.submit(vk::SubmitInfo().setCommandBuffers(cmd), fence); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 825 | VERIFY(result == vk::Result::eSuccess); |
| 826 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 827 | result = device.waitForFences(fence, VK_TRUE, UINT64_MAX); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 828 | VERIFY(result == vk::Result::eSuccess); |
| 829 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 830 | device.freeCommandBuffers(cmd_pool, cmd); |
| 831 | device.destroyFence(fence); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 832 | |
| 833 | cmd = vk::CommandBuffer(); |
| 834 | } |
| 835 | |
| 836 | void Demo::init(int argc, char **argv) { |
| 837 | vec3 eye = {0.0f, 3.0f, 5.0f}; |
| 838 | vec3 origin = {0, 0, 0}; |
| 839 | vec3 up = {0.0f, 1.0f, 0.0}; |
| 840 | |
| 841 | presentMode = vk::PresentModeKHR::eFifo; |
| 842 | frameCount = UINT32_MAX; |
Nicolas Caramelli | 7c25ce9 | 2021-01-08 17:46:07 +0100 | [diff] [blame] | 843 | width = 500; |
| 844 | height = 500; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 845 | use_xlib = false; |
Witold Baryluk | a3b988f | 2021-01-07 19:03:02 +0000 | [diff] [blame] | 846 | /* Autodetect suitable / best GPU by default */ |
| 847 | gpu_number = -1; |
Tony-LunarG | 6cebf14 | 2019-09-11 14:32:23 -0600 | [diff] [blame] | 848 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 849 | for (int i = 1; i < argc; i++) { |
| 850 | if (strcmp(argv[i], "--use_staging") == 0) { |
| 851 | use_staging_buffer = true; |
| 852 | continue; |
| 853 | } |
| 854 | if ((strcmp(argv[i], "--present_mode") == 0) && (i < argc - 1)) { |
| 855 | presentMode = (vk::PresentModeKHR)atoi(argv[i + 1]); |
| 856 | i++; |
| 857 | continue; |
| 858 | } |
| 859 | if (strcmp(argv[i], "--break") == 0) { |
| 860 | use_break = true; |
| 861 | continue; |
| 862 | } |
| 863 | if (strcmp(argv[i], "--validate") == 0) { |
| 864 | validate = true; |
| 865 | continue; |
| 866 | } |
| 867 | if (strcmp(argv[i], "--xlib") == 0) { |
| 868 | fprintf(stderr, "--xlib is deprecated and no longer does anything"); |
| 869 | continue; |
| 870 | } |
| 871 | if (strcmp(argv[i], "--c") == 0 && frameCount == UINT32_MAX && i < argc - 1 && |
| 872 | sscanf(argv[i + 1], "%" SCNu32, &frameCount) == 1) { |
| 873 | i++; |
| 874 | continue; |
| 875 | } |
Jeremy Hayes | cf9d49e | 2021-08-30 18:04:09 -0600 | [diff] [blame] | 876 | if (strcmp(argv[i], "--width") == 0 && i < argc - 1 && sscanf(argv[i + 1], "%" SCNi32, &width) == 1 && width > 0) { |
Nicolas Caramelli | 7c25ce9 | 2021-01-08 17:46:07 +0100 | [diff] [blame] | 877 | i++; |
| 878 | continue; |
| 879 | } |
Jeremy Hayes | cf9d49e | 2021-08-30 18:04:09 -0600 | [diff] [blame] | 880 | if (strcmp(argv[i], "--height") == 0 && i < argc - 1 && sscanf(argv[i + 1], "%" SCNi32, &height) == 1 && height > 0) { |
Nicolas Caramelli | 7c25ce9 | 2021-01-08 17:46:07 +0100 | [diff] [blame] | 881 | i++; |
| 882 | continue; |
| 883 | } |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 884 | if (strcmp(argv[i], "--suppress_popups") == 0) { |
| 885 | suppress_popups = true; |
| 886 | continue; |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 887 | } |
Tony-LunarG | 50e737c | 2020-07-16 14:26:03 -0600 | [diff] [blame] | 888 | if ((strcmp(argv[i], "--gpu_number") == 0) && (i < argc - 1)) { |
| 889 | gpu_number = atoi(argv[i + 1]); |
Witold Baryluk | a3b988f | 2021-01-07 19:03:02 +0000 | [diff] [blame] | 890 | assert(gpu_number >= 0); |
Tony-LunarG | 50e737c | 2020-07-16 14:26:03 -0600 | [diff] [blame] | 891 | i++; |
| 892 | continue; |
| 893 | } |
Mark Young | 43c08f8 | 2021-11-30 16:38:25 -0700 | [diff] [blame] | 894 | if (strcmp(argv[i], "--force_errors") == 0) { |
| 895 | force_errors = true; |
| 896 | continue; |
| 897 | } |
Tony-LunarG | d74734f | 2019-06-04 14:11:43 -0600 | [diff] [blame] | 898 | std::stringstream usage; |
| 899 | usage << "Usage:\n " << APP_SHORT_NAME << "\t[--use_staging] [--validate]\n" |
| 900 | << "\t[--break] [--c <framecount>] [--suppress_popups]\n" |
Tony-LunarG | 50e737c | 2020-07-16 14:26:03 -0600 | [diff] [blame] | 901 | << "\t[--gpu_number <index of physical device>]\n" |
Tony-LunarG | d74734f | 2019-06-04 14:11:43 -0600 | [diff] [blame] | 902 | << "\t[--present_mode <present mode enum>]\n" |
Nicolas Caramelli | 7c25ce9 | 2021-01-08 17:46:07 +0100 | [diff] [blame] | 903 | << "\t[--width <width>] [--height <height>]\n" |
Mark Young | 43c08f8 | 2021-11-30 16:38:25 -0700 | [diff] [blame] | 904 | << "\t[--force_errors]\n" |
Tony-LunarG | d74734f | 2019-06-04 14:11:43 -0600 | [diff] [blame] | 905 | << "\t<present_mode_enum>\n" |
| 906 | << "\t\tVK_PRESENT_MODE_IMMEDIATE_KHR = " << VK_PRESENT_MODE_IMMEDIATE_KHR << "\n" |
| 907 | << "\t\tVK_PRESENT_MODE_MAILBOX_KHR = " << VK_PRESENT_MODE_MAILBOX_KHR << "\n" |
| 908 | << "\t\tVK_PRESENT_MODE_FIFO_KHR = " << VK_PRESENT_MODE_FIFO_KHR << "\n" |
Witold Baryluk | a3b988f | 2021-01-07 19:03:02 +0000 | [diff] [blame] | 909 | << "\t\tVK_PRESENT_MODE_FIFO_RELAXED_KHR = " << VK_PRESENT_MODE_FIFO_RELAXED_KHR << "\n"; |
Tony-LunarG | d74734f | 2019-06-04 14:11:43 -0600 | [diff] [blame] | 910 | |
| 911 | #if defined(_WIN32) |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 912 | if (!suppress_popups) MessageBox(nullptr, usage.str().c_str(), "Usage Error", MB_OK); |
Tony-LunarG | d74734f | 2019-06-04 14:11:43 -0600 | [diff] [blame] | 913 | #else |
| 914 | std::cerr << usage.str(); |
| 915 | std::cerr.flush(); |
| 916 | #endif |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 917 | exit(1); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 918 | } |
| 919 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 920 | if (!use_xlib) { |
| 921 | init_connection(); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 922 | } |
| 923 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 924 | init_vk(); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 925 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 926 | spin_angle = 4.0f; |
| 927 | spin_increment = 0.2f; |
| 928 | pause = false; |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 929 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 930 | mat4x4_perspective(projection_matrix, static_cast<float>(degreesToRadians(45.0f)), 1.0f, 0.1f, 100.0f); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 931 | mat4x4_look_at(view_matrix, eye, origin, up); |
| 932 | mat4x4_identity(model_matrix); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 933 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 934 | projection_matrix[1][1] *= -1; // Flip projection matrix from GL to Vulkan orientation. |
| 935 | } |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 936 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 937 | void Demo::init_connection() { |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 938 | #if defined(VK_USE_PLATFORM_XCB_KHR) |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 939 | const xcb_setup_t *setup; |
| 940 | xcb_screen_iterator_t iter; |
| 941 | int scr; |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 942 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 943 | const char *display_envar = getenv("DISPLAY"); |
| 944 | if (display_envar == nullptr || display_envar[0] == '\0') { |
| 945 | printf("Environment variable DISPLAY requires a valid value.\nExiting ...\n"); |
| 946 | fflush(stdout); |
| 947 | exit(1); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 948 | } |
| 949 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 950 | connection = xcb_connect(nullptr, &scr); |
| 951 | if (xcb_connection_has_error(connection) > 0) { |
C Stout | 05c6171 | 2021-08-20 13:19:01 -0700 | [diff] [blame] | 952 | printf("Cannot connect to XCB.\nExiting ...\n"); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 953 | fflush(stdout); |
| 954 | exit(1); |
| 955 | } |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 956 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 957 | setup = xcb_get_setup(connection); |
| 958 | iter = xcb_setup_roots_iterator(setup); |
| 959 | while (scr-- > 0) xcb_screen_next(&iter); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 960 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 961 | screen = iter.data; |
| 962 | #elif defined(VK_USE_PLATFORM_WAYLAND_KHR) |
| 963 | display = wl_display_connect(nullptr); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 964 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 965 | if (display == nullptr) { |
C Stout | 05c6171 | 2021-08-20 13:19:01 -0700 | [diff] [blame] | 966 | printf("Cannot connect to wayland.\nExiting ...\n"); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 967 | fflush(stdout); |
| 968 | exit(1); |
| 969 | } |
| 970 | |
| 971 | registry = wl_display_get_registry(display); |
| 972 | wl_registry_add_listener(registry, ®istry_listener, this); |
| 973 | wl_display_dispatch(display); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 974 | #endif |
| 975 | } |
Tony-LunarG | 27c2124 | 2021-03-11 16:28:19 -0700 | [diff] [blame] | 976 | #if defined(VK_USE_PLATFORM_DISPLAY_KHR) |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 977 | int find_display_gpu(int gpu_number, const std::vector<vk::PhysicalDevice> &physical_devices) { |
Tony-LunarG | 27c2124 | 2021-03-11 16:28:19 -0700 | [diff] [blame] | 978 | uint32_t display_count = 0; |
Tony-LunarG | 27c2124 | 2021-03-11 16:28:19 -0700 | [diff] [blame] | 979 | int gpu_return = gpu_number; |
| 980 | if (gpu_number >= 0) { |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 981 | auto display_props_return = physical_devices[gpu_number].getDisplayPropertiesKHR(); |
| 982 | VERIFY(display_props_return.result == vk::Result::eSuccess); |
Tony-LunarG | 27c2124 | 2021-03-11 16:28:19 -0700 | [diff] [blame] | 983 | } else { |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 984 | for (uint32_t i = 0; i < physical_devices.size(); i++) { |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 985 | auto display_props_return = physical_devices[i].getDisplayPropertiesKHR(); |
| 986 | VERIFY(display_props_return.result == vk::Result::eSuccess); |
| 987 | if (display_props_return.value.size() > 0) { |
Tony-LunarG | 27c2124 | 2021-03-11 16:28:19 -0700 | [diff] [blame] | 988 | gpu_return = i; |
| 989 | break; |
| 990 | } |
| 991 | } |
| 992 | } |
| 993 | if (display_count > 0) |
| 994 | return gpu_return; |
| 995 | else |
| 996 | return -1; |
| 997 | } |
| 998 | #endif |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 999 | void Demo::init_vk() { |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1000 | std::vector<char const *> instance_validation_layers = {"VK_LAYER_KHRONOS_validation"}; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1001 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1002 | // Look for validation layers |
| 1003 | vk::Bool32 validation_found = VK_FALSE; |
| 1004 | if (validate) { |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1005 | auto layers = vk::enumerateInstanceLayerProperties(); |
| 1006 | VERIFY(layers.result == vk::Result::eSuccess); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1007 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1008 | validation_found = check_layers(instance_validation_layers, layers.value); |
| 1009 | if (validation_found) { |
| 1010 | enabled_layers.push_back("VK_LAYER_KHRONOS_validation"); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1011 | } |
| 1012 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1013 | else { |
Mark Lobodzinski | 85dbd82 | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 1014 | ERR_EXIT( |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1015 | "vkEnumerateInstanceLayerProperties failed to find required validation layer.\n\n" |
| 1016 | "Please look at the Getting Started guide for additional information.\n", |
Mark Lobodzinski | 85dbd82 | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 1017 | "vkCreateInstance Failure"); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1018 | } |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1019 | } |
| 1020 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1021 | /* Look for instance extensions */ |
| 1022 | vk::Bool32 surfaceExtFound = VK_FALSE; |
| 1023 | vk::Bool32 platformSurfaceExtFound = VK_FALSE; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1024 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1025 | auto instance_extensions_return = vk::enumerateInstanceExtensionProperties(); |
| 1026 | VERIFY(instance_extensions_return.result == vk::Result::eSuccess); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1027 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1028 | for (const auto &extension : instance_extensions_return.value) { |
| 1029 | if (!strcmp(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME, extension.extensionName)) { |
| 1030 | enabled_instance_extensions.push_back(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME); |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 1031 | } else if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME, extension.extensionName)) { |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1032 | surfaceExtFound = 1; |
| 1033 | enabled_instance_extensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME); |
| 1034 | } |
| 1035 | #if defined(VK_USE_PLATFORM_WIN32_KHR) |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 1036 | else if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME, extension.extensionName)) { |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1037 | platformSurfaceExtFound = 1; |
| 1038 | enabled_instance_extensions.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME); |
| 1039 | } |
| 1040 | #elif defined(VK_USE_PLATFORM_XLIB_KHR) |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 1041 | else if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME, extension.extensionName)) { |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1042 | platformSurfaceExtFound = 1; |
| 1043 | enabled_instance_extensions.push_back(VK_KHR_XLIB_SURFACE_EXTENSION_NAME); |
| 1044 | } |
| 1045 | #elif defined(VK_USE_PLATFORM_XCB_KHR) |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 1046 | else if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME, extension.extensionName)) { |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1047 | platformSurfaceExtFound = 1; |
| 1048 | enabled_instance_extensions.push_back(VK_KHR_XCB_SURFACE_EXTENSION_NAME); |
| 1049 | } |
| 1050 | #elif defined(VK_USE_PLATFORM_WAYLAND_KHR) |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 1051 | else if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME, extension.extensionName)) { |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1052 | platformSurfaceExtFound = 1; |
| 1053 | enabled_instance_extensions.push_back(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME); |
| 1054 | } |
| 1055 | #elif defined(VK_USE_PLATFORM_DIRECTFB_EXT) |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 1056 | else if (!strcmp(VK_EXT_DIRECTFB_SURFACE_EXTENSION_NAME, extension.extensionName)) { |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1057 | platformSurfaceExtFound = 1; |
| 1058 | enabled_instance_extensions.push_back(VK_EXT_DIRECTFB_SURFACE_EXTENSION_NAME); |
| 1059 | } |
| 1060 | #elif defined(VK_USE_PLATFORM_DISPLAY_KHR) |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 1061 | else if (!strcmp(VK_KHR_DISPLAY_EXTENSION_NAME, extension.extensionName)) { |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1062 | platformSurfaceExtFound = 1; |
| 1063 | enabled_instance_extensions.push_back(VK_KHR_DISPLAY_EXTENSION_NAME); |
| 1064 | } |
| 1065 | #elif defined(VK_USE_PLATFORM_METAL_EXT) |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 1066 | else if (!strcmp(VK_EXT_METAL_SURFACE_EXTENSION_NAME, extension.extensionName)) { |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1067 | platformSurfaceExtFound = 1; |
| 1068 | enabled_instance_extensions.push_back(VK_EXT_METAL_SURFACE_EXTENSION_NAME); |
| 1069 | } |
| 1070 | #endif |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | if (!surfaceExtFound) { |
| 1074 | ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_SURFACE_EXTENSION_NAME |
| 1075 | " extension.\n\n" |
| 1076 | "Do you have a compatible Vulkan installable client driver (ICD) installed?\n" |
| 1077 | "Please look at the Getting Started guide for additional information.\n", |
| 1078 | "vkCreateInstance Failure"); |
| 1079 | } |
| 1080 | |
| 1081 | if (!platformSurfaceExtFound) { |
| 1082 | #if defined(VK_USE_PLATFORM_WIN32_KHR) |
| 1083 | ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME |
| 1084 | " extension.\n\n" |
| 1085 | "Do you have a compatible Vulkan installable client driver (ICD) installed?\n" |
| 1086 | "Please look at the Getting Started guide for additional information.\n", |
| 1087 | "vkCreateInstance Failure"); |
| 1088 | #elif defined(VK_USE_PLATFORM_XCB_KHR) |
| 1089 | ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_XCB_SURFACE_EXTENSION_NAME |
| 1090 | " extension.\n\n" |
| 1091 | "Do you have a compatible Vulkan installable client driver (ICD) installed?\n" |
| 1092 | "Please look at the Getting Started guide for additional information.\n", |
| 1093 | "vkCreateInstance Failure"); |
| 1094 | #elif defined(VK_USE_PLATFORM_WAYLAND_KHR) |
| 1095 | ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME |
| 1096 | " extension.\n\n" |
| 1097 | "Do you have a compatible Vulkan installable client driver (ICD) installed?\n" |
| 1098 | "Please look at the Getting Started guide for additional information.\n", |
| 1099 | "vkCreateInstance Failure"); |
Tony Barbour | 153cb06 | 2016-12-07 13:43:36 -0700 | [diff] [blame] | 1100 | #elif defined(VK_USE_PLATFORM_XLIB_KHR) |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1101 | ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME |
| 1102 | " extension.\n\n" |
| 1103 | "Do you have a compatible Vulkan installable client driver (ICD) installed?\n" |
| 1104 | "Please look at the Getting Started guide for additional information.\n", |
| 1105 | "vkCreateInstance Failure"); |
Nicolas Caramelli | c8be8c8 | 2020-07-13 17:22:09 +0200 | [diff] [blame] | 1106 | #elif defined(VK_USE_PLATFORM_DIRECTFB_EXT) |
| 1107 | ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_EXT_DIRECTFB_SURFACE_EXTENSION_NAME |
| 1108 | " extension.\n\n" |
| 1109 | "Do you have a compatible Vulkan installable client driver (ICD) installed?\n" |
| 1110 | "Please look at the Getting Started guide for additional information.\n", |
| 1111 | "vkCreateInstance Failure"); |
Damien Leone | 600c305 | 2017-01-31 10:26:07 -0700 | [diff] [blame] | 1112 | #elif defined(VK_USE_PLATFORM_DISPLAY_KHR) |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1113 | ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_DISPLAY_EXTENSION_NAME |
| 1114 | " extension.\n\n" |
| 1115 | "Do you have a compatible Vulkan installable client driver (ICD) installed?\n" |
| 1116 | "Please look at the Getting Started guide for additional information.\n", |
| 1117 | "vkCreateInstance Failure"); |
Bill Hollings | 0a0625a | 2019-07-15 17:39:18 -0400 | [diff] [blame] | 1118 | #elif defined(VK_USE_PLATFORM_METAL_EXT) |
| 1119 | ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_EXT_METAL_SURFACE_EXTENSION_NAME |
Karl Schultz | 9ceac06 | 2017-12-12 10:33:01 -0500 | [diff] [blame] | 1120 | " extension.\n\nDo you have a compatible " |
| 1121 | "Vulkan installable client driver (ICD) installed?\nPlease " |
| 1122 | "look at the Getting Started guide for additional " |
| 1123 | "information.\n", |
| 1124 | "vkCreateInstance Failure"); |
Tony Barbour | 153cb06 | 2016-12-07 13:43:36 -0700 | [diff] [blame] | 1125 | #endif |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1126 | } |
| 1127 | auto const app = vk::ApplicationInfo() |
| 1128 | .setPApplicationName(APP_SHORT_NAME) |
| 1129 | .setApplicationVersion(0) |
| 1130 | .setPEngineName(APP_SHORT_NAME) |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1131 | .setEngineVersion(0); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1132 | auto const inst_info = vk::InstanceCreateInfo() |
| 1133 | .setPApplicationInfo(&app) |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1134 | .setPEnabledLayerNames(enabled_layers) |
| 1135 | .setPEnabledExtensionNames(enabled_instance_extensions); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1136 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1137 | auto instance_result = vk::createInstance(inst_info); |
| 1138 | if (instance_result.result == vk::Result::eErrorIncompatibleDriver) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1139 | ERR_EXIT( |
| 1140 | "Cannot find a compatible Vulkan installable client driver (ICD).\n\n" |
| 1141 | "Please look at the Getting Started guide for additional information.\n", |
| 1142 | "vkCreateInstance Failure"); |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1143 | } else if (instance_result.result == vk::Result::eErrorExtensionNotPresent) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1144 | ERR_EXIT( |
| 1145 | "Cannot find a specified extension library.\n" |
| 1146 | "Make sure your layers path is set appropriately.\n", |
| 1147 | "vkCreateInstance Failure"); |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1148 | } else if (instance_result.result != vk::Result::eSuccess) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1149 | ERR_EXIT( |
| 1150 | "vkCreateInstance failed.\n\n" |
| 1151 | "Do you have a compatible Vulkan installable client driver (ICD) installed?\n" |
| 1152 | "Please look at the Getting Started guide for additional information.\n", |
| 1153 | "vkCreateInstance Failure"); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1154 | } |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1155 | inst = instance_result.value; |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1156 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1157 | auto physical_device_return = inst.enumeratePhysicalDevices(); |
| 1158 | VERIFY(physical_device_return.result == vk::Result::eSuccess); |
| 1159 | auto physical_devices = physical_device_return.value; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1160 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1161 | if (physical_devices.size() <= 0) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1162 | ERR_EXIT( |
| 1163 | "vkEnumeratePhysicalDevices reported zero accessible devices.\n\n" |
| 1164 | "Do you have a compatible Vulkan installable client driver (ICD) installed?\n" |
| 1165 | "Please look at the Getting Started guide for additional information.\n", |
| 1166 | "vkEnumeratePhysicalDevices Failure"); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1167 | } |
| 1168 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1169 | if (gpu_number >= 0 && !(static_cast<uint32_t>(gpu_number) < physical_devices.size())) { |
| 1170 | fprintf(stderr, "GPU %d specified is not present, GPU count = %zu\n", gpu_number, physical_devices.size()); |
Witold Baryluk | a3b988f | 2021-01-07 19:03:02 +0000 | [diff] [blame] | 1171 | ERR_EXIT("Specified GPU number is not present", "User Error"); |
| 1172 | } |
Tony-LunarG | 27c2124 | 2021-03-11 16:28:19 -0700 | [diff] [blame] | 1173 | #if defined(VK_USE_PLATFORM_DISPLAY_KHR) |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1174 | gpu_number = find_display_gpu(gpu_number, physical_devices); |
Tony-LunarG | 27c2124 | 2021-03-11 16:28:19 -0700 | [diff] [blame] | 1175 | if (gpu_number < 0) { |
| 1176 | printf("Cannot find any display!\n"); |
| 1177 | fflush(stdout); |
| 1178 | exit(1); |
| 1179 | } |
| 1180 | #else |
Witold Baryluk | a3b988f | 2021-01-07 19:03:02 +0000 | [diff] [blame] | 1181 | /* Try to auto select most suitable device */ |
| 1182 | if (gpu_number == -1) { |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1183 | constexpr uint32_t device_type_count = static_cast<uint32_t>(vk::PhysicalDeviceType::eCpu) + 1; |
| 1184 | std::array<uint32_t, device_type_count> count_device_type{}; |
Witold Baryluk | a3b988f | 2021-01-07 19:03:02 +0000 | [diff] [blame] | 1185 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1186 | for (uint32_t i = 0; i < physical_devices.size(); i++) { |
Witold Baryluk | a3b988f | 2021-01-07 19:03:02 +0000 | [diff] [blame] | 1187 | const auto physicalDeviceProperties = physical_devices[i].getProperties(); |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1188 | assert(physicalDeviceProperties.deviceType <= vk::PhysicalDeviceType::eCpu); |
Witold Baryluk | a3b988f | 2021-01-07 19:03:02 +0000 | [diff] [blame] | 1189 | count_device_type[static_cast<int>(physicalDeviceProperties.deviceType)]++; |
| 1190 | } |
| 1191 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1192 | std::array<vk::PhysicalDeviceType, device_type_count> const device_type_preference = { |
Witold Baryluk | a3b988f | 2021-01-07 19:03:02 +0000 | [diff] [blame] | 1193 | vk::PhysicalDeviceType::eDiscreteGpu, vk::PhysicalDeviceType::eIntegratedGpu, vk::PhysicalDeviceType::eVirtualGpu, |
| 1194 | vk::PhysicalDeviceType::eCpu, vk::PhysicalDeviceType::eOther}; |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1195 | |
Witold Baryluk | a3b988f | 2021-01-07 19:03:02 +0000 | [diff] [blame] | 1196 | vk::PhysicalDeviceType search_for_device_type = vk::PhysicalDeviceType::eDiscreteGpu; |
| 1197 | for (uint32_t i = 0; i < sizeof(device_type_preference) / sizeof(vk::PhysicalDeviceType); i++) { |
| 1198 | if (count_device_type[static_cast<int>(device_type_preference[i])]) { |
| 1199 | search_for_device_type = device_type_preference[i]; |
| 1200 | break; |
| 1201 | } |
| 1202 | } |
| 1203 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1204 | for (uint32_t i = 0; i < physical_devices.size(); i++) { |
Witold Baryluk | a3b988f | 2021-01-07 19:03:02 +0000 | [diff] [blame] | 1205 | const auto physicalDeviceProperties = physical_devices[i].getProperties(); |
| 1206 | if (physicalDeviceProperties.deviceType == search_for_device_type) { |
| 1207 | gpu_number = i; |
| 1208 | break; |
| 1209 | } |
| 1210 | } |
| 1211 | } |
Tony-LunarG | 27c2124 | 2021-03-11 16:28:19 -0700 | [diff] [blame] | 1212 | #endif |
Witold Baryluk | a3b988f | 2021-01-07 19:03:02 +0000 | [diff] [blame] | 1213 | assert(gpu_number >= 0); |
| 1214 | gpu = physical_devices[gpu_number]; |
| 1215 | { |
| 1216 | auto physicalDeviceProperties = gpu.getProperties(); |
| 1217 | fprintf(stderr, "Selected GPU %d: %s, type: %s\n", gpu_number, physicalDeviceProperties.deviceName.data(), |
| 1218 | to_string(physicalDeviceProperties.deviceType).c_str()); |
| 1219 | } |
Witold Baryluk | a3b988f | 2021-01-07 19:03:02 +0000 | [diff] [blame] | 1220 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1221 | /* Look for device extensions */ |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1222 | vk::Bool32 swapchainExtFound = VK_FALSE; |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1223 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1224 | auto device_extension_return = gpu.enumerateDeviceExtensionProperties(); |
| 1225 | VERIFY(device_extension_return.result == vk::Result::eSuccess); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1226 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1227 | for (const auto &extension : device_extension_return.value) { |
| 1228 | if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME, extension.extensionName)) { |
| 1229 | swapchainExtFound = 1; |
| 1230 | enabled_device_extensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME); |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 1231 | } else if (!strcmp("VK_KHR_portability_subset", extension.extensionName)) { |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1232 | enabled_device_extensions.push_back("VK_KHR_portability_subset"); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1233 | } |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1234 | } |
Jeremy Hayes | 6ae1f8a | 2016-11-16 14:47:13 -0700 | [diff] [blame] | 1235 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1236 | if (!swapchainExtFound) { |
| 1237 | ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find the " VK_KHR_SWAPCHAIN_EXTENSION_NAME |
| 1238 | " extension.\n\n" |
| 1239 | "Do you have a compatible Vulkan installable client driver (ICD) installed?\n" |
| 1240 | "Please look at the Getting Started guide for additional information.\n", |
| 1241 | "vkCreateInstance Failure"); |
| 1242 | } |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1243 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1244 | gpu.getProperties(&gpu_props); |
Jeremy Hayes | 00399e3 | 2017-06-14 15:07:32 -0600 | [diff] [blame] | 1245 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1246 | /* Call with nullptr data to get count */ |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1247 | queue_props = gpu.getQueueFamilyProperties(); |
| 1248 | assert(queue_props.size() >= 1); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1249 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1250 | // Query fine-grained feature support for this device. |
| 1251 | // If app has specific feature requirements it should check supported |
| 1252 | // features based on this query |
| 1253 | vk::PhysicalDeviceFeatures physDevFeatures; |
| 1254 | gpu.getFeatures(&physDevFeatures); |
| 1255 | } |
| 1256 | |
Tony-LunarG | 6cebf14 | 2019-09-11 14:32:23 -0600 | [diff] [blame] | 1257 | void Demo::create_surface() { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1258 | // Create a WSI surface for the window: |
| 1259 | #if defined(VK_USE_PLATFORM_WIN32_KHR) |
| 1260 | { |
| 1261 | auto const createInfo = vk::Win32SurfaceCreateInfoKHR().setHinstance(connection).setHwnd(window); |
| 1262 | |
| 1263 | auto result = inst.createWin32SurfaceKHR(&createInfo, nullptr, &surface); |
| 1264 | VERIFY(result == vk::Result::eSuccess); |
| 1265 | } |
| 1266 | #elif defined(VK_USE_PLATFORM_WAYLAND_KHR) |
| 1267 | { |
| 1268 | auto const createInfo = vk::WaylandSurfaceCreateInfoKHR().setDisplay(display).setSurface(window); |
| 1269 | |
| 1270 | auto result = inst.createWaylandSurfaceKHR(&createInfo, nullptr, &surface); |
| 1271 | VERIFY(result == vk::Result::eSuccess); |
| 1272 | } |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1273 | #elif defined(VK_USE_PLATFORM_XLIB_KHR) |
| 1274 | { |
| 1275 | auto const createInfo = vk::XlibSurfaceCreateInfoKHR().setDpy(display).setWindow(xlib_window); |
| 1276 | |
| 1277 | auto result = inst.createXlibSurfaceKHR(&createInfo, nullptr, &surface); |
| 1278 | VERIFY(result == vk::Result::eSuccess); |
| 1279 | } |
| 1280 | #elif defined(VK_USE_PLATFORM_XCB_KHR) |
| 1281 | { |
| 1282 | auto const createInfo = vk::XcbSurfaceCreateInfoKHR().setConnection(connection).setWindow(xcb_window); |
| 1283 | |
| 1284 | auto result = inst.createXcbSurfaceKHR(&createInfo, nullptr, &surface); |
| 1285 | VERIFY(result == vk::Result::eSuccess); |
| 1286 | } |
Nicolas Caramelli | c8be8c8 | 2020-07-13 17:22:09 +0200 | [diff] [blame] | 1287 | #elif defined(VK_USE_PLATFORM_DIRECTFB_EXT) |
| 1288 | { |
| 1289 | auto const createInfo = vk::DirectFBSurfaceCreateInfoEXT().setDfb(dfb).setSurface(window); |
| 1290 | |
| 1291 | auto result = inst.createDirectFBSurfaceEXT(&createInfo, nullptr, &surface); |
| 1292 | VERIFY(result == vk::Result::eSuccess); |
| 1293 | } |
Bill Hollings | 0a0625a | 2019-07-15 17:39:18 -0400 | [diff] [blame] | 1294 | #elif defined(VK_USE_PLATFORM_METAL_EXT) |
Karl Schultz | 9ceac06 | 2017-12-12 10:33:01 -0500 | [diff] [blame] | 1295 | { |
Bill Hollings | 0a0625a | 2019-07-15 17:39:18 -0400 | [diff] [blame] | 1296 | auto const createInfo = vk::MetalSurfaceCreateInfoEXT().setPLayer(static_cast<CAMetalLayer *>(caMetalLayer)); |
Karl Schultz | 9ceac06 | 2017-12-12 10:33:01 -0500 | [diff] [blame] | 1297 | |
Bill Hollings | 0a0625a | 2019-07-15 17:39:18 -0400 | [diff] [blame] | 1298 | auto result = inst.createMetalSurfaceEXT(&createInfo, nullptr, &surface); |
Karl Schultz | 9ceac06 | 2017-12-12 10:33:01 -0500 | [diff] [blame] | 1299 | VERIFY(result == vk::Result::eSuccess); |
| 1300 | } |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1301 | #elif defined(VK_USE_PLATFORM_DISPLAY_KHR) |
| 1302 | { |
| 1303 | auto result = create_display_surface(); |
| 1304 | VERIFY(result == vk::Result::eSuccess); |
| 1305 | } |
| 1306 | #endif |
Tony-LunarG | 6cebf14 | 2019-09-11 14:32:23 -0600 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | void Demo::init_vk_swapchain() { |
| 1310 | create_surface(); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1311 | // Iterate over each queue to learn whether it supports presenting: |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 1312 | std::vector<vk::Bool32> supportsPresent; |
| 1313 | for (uint32_t i = 0; i < static_cast<uint32_t>(queue_props.size()); i++) { |
| 1314 | auto supports = gpu.getSurfaceSupportKHR(i, surface); |
| 1315 | VERIFY(supports.result == vk::Result::eSuccess); |
| 1316 | supportsPresent.push_back(supports.value); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | uint32_t graphicsQueueFamilyIndex = UINT32_MAX; |
| 1320 | uint32_t presentQueueFamilyIndex = UINT32_MAX; |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 1321 | for (uint32_t i = 0; i < static_cast<uint32_t>(queue_props.size()); i++) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1322 | if (queue_props[i].queueFlags & vk::QueueFlagBits::eGraphics) { |
| 1323 | if (graphicsQueueFamilyIndex == UINT32_MAX) { |
| 1324 | graphicsQueueFamilyIndex = i; |
| 1325 | } |
| 1326 | |
| 1327 | if (supportsPresent[i] == VK_TRUE) { |
| 1328 | graphicsQueueFamilyIndex = i; |
| 1329 | presentQueueFamilyIndex = i; |
Jeremy Hayes | 00399e3 | 2017-06-14 15:07:32 -0600 | [diff] [blame] | 1330 | break; |
| 1331 | } |
| 1332 | } |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1333 | } |
Jeremy Hayes | 00399e3 | 2017-06-14 15:07:32 -0600 | [diff] [blame] | 1334 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1335 | if (presentQueueFamilyIndex == UINT32_MAX) { |
| 1336 | // If didn't find a queue that supports both graphics and present, |
| 1337 | // then |
| 1338 | // find a separate present queue. |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1339 | for (uint32_t i = 0; i < queue_props.size(); ++i) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1340 | if (supportsPresent[i] == VK_TRUE) { |
| 1341 | presentQueueFamilyIndex = i; |
| 1342 | break; |
| 1343 | } |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1344 | } |
| 1345 | } |
| 1346 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1347 | // Generate error if could not find both a graphics and a present queue |
| 1348 | if (graphicsQueueFamilyIndex == UINT32_MAX || presentQueueFamilyIndex == UINT32_MAX) { |
| 1349 | ERR_EXIT("Could not find both graphics and present queues\n", "Swapchain Initialization Failure"); |
| 1350 | } |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1351 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1352 | graphics_queue_family_index = graphicsQueueFamilyIndex; |
| 1353 | present_queue_family_index = presentQueueFamilyIndex; |
| 1354 | separate_present_queue = (graphics_queue_family_index != present_queue_family_index); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1355 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1356 | create_device(); |
Jeremy Hayes | 00399e3 | 2017-06-14 15:07:32 -0600 | [diff] [blame] | 1357 | |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 1358 | graphics_queue = device.getQueue(graphics_queue_family_index, 0); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1359 | if (!separate_present_queue) { |
| 1360 | present_queue = graphics_queue; |
| 1361 | } else { |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 1362 | present_queue = device.getQueue(present_queue_family_index, 0); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1363 | } |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1364 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1365 | // Get the list of VkFormat's that are supported: |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1366 | auto surface_formats_return = gpu.getSurfaceFormatsKHR(surface); |
| 1367 | VERIFY(surface_formats_return.result == vk::Result::eSuccess); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1368 | |
Charles Giessen | 7dc2dd5 | 2022-02-03 13:55:48 -0700 | [diff] [blame] | 1369 | vk::SurfaceFormatKHR surfaceFormat = pick_surface_format(surface_formats_return.value); |
| 1370 | format = surfaceFormat.format; |
| 1371 | color_space = surfaceFormat.colorSpace; |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1372 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1373 | quit = false; |
| 1374 | curFrame = 0; |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1375 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1376 | // Create semaphores to synchronize acquiring presentable buffers before |
| 1377 | // rendering and waiting for drawing to be complete before presenting |
| 1378 | auto const semaphoreCreateInfo = vk::SemaphoreCreateInfo(); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1379 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1380 | // Create fences that we can use to throttle if we get too far |
| 1381 | // ahead of the image presents |
| 1382 | auto const fence_ci = vk::FenceCreateInfo().setFlags(vk::FenceCreateFlagBits::eSignaled); |
| 1383 | for (uint32_t i = 0; i < FRAME_LAG; i++) { |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1384 | vk::Result result = device.createFence(&fence_ci, nullptr, &fences[i]); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1385 | VERIFY(result == vk::Result::eSuccess); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1386 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1387 | result = device.createSemaphore(&semaphoreCreateInfo, nullptr, &image_acquired_semaphores[i]); |
| 1388 | VERIFY(result == vk::Result::eSuccess); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1389 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1390 | result = device.createSemaphore(&semaphoreCreateInfo, nullptr, &draw_complete_semaphores[i]); |
| 1391 | VERIFY(result == vk::Result::eSuccess); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1392 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1393 | if (separate_present_queue) { |
| 1394 | result = device.createSemaphore(&semaphoreCreateInfo, nullptr, &image_ownership_semaphores[i]); |
Jeremy Hayes | 00399e3 | 2017-06-14 15:07:32 -0600 | [diff] [blame] | 1395 | VERIFY(result == vk::Result::eSuccess); |
| 1396 | } |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1397 | } |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1398 | frame_index = 0; |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1399 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1400 | // Get Memory information and properties |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1401 | memory_properties = gpu.getMemoryProperties(); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1402 | } |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1403 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1404 | void Demo::prepare() { |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1405 | prepare_init_cmd(); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1406 | |
| 1407 | prepare_buffers(); |
| 1408 | prepare_depth(); |
| 1409 | prepare_textures(); |
| 1410 | prepare_cube_data_buffers(); |
| 1411 | |
| 1412 | prepare_descriptor_layout(); |
| 1413 | prepare_render_pass(); |
| 1414 | prepare_pipeline(); |
| 1415 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1416 | for (auto &swapchain_image_resource : swapchain_image_resources) { |
| 1417 | auto alloc_return = device.allocateCommandBuffers(vk::CommandBufferAllocateInfo() |
| 1418 | .setCommandPool(cmd_pool) |
| 1419 | .setLevel(vk::CommandBufferLevel::ePrimary) |
| 1420 | .setCommandBufferCount(1)); |
| 1421 | VERIFY(alloc_return.result == vk::Result::eSuccess); |
| 1422 | swapchain_image_resource.cmd = alloc_return.value[0]; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | if (separate_present_queue) { |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1426 | auto present_cmd_pool_return = |
| 1427 | device.createCommandPool(vk::CommandPoolCreateInfo().setQueueFamilyIndex(present_queue_family_index)); |
| 1428 | VERIFY(present_cmd_pool_return.result == vk::Result::eSuccess); |
| 1429 | present_cmd_pool = present_cmd_pool_return.value; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1430 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1431 | for (auto &swapchain_image_resource : swapchain_image_resources) { |
| 1432 | auto alloc_cmd_return = device.allocateCommandBuffers(vk::CommandBufferAllocateInfo() |
| 1433 | .setCommandPool(present_cmd_pool) |
| 1434 | .setLevel(vk::CommandBufferLevel::ePrimary) |
| 1435 | .setCommandBufferCount(1)); |
| 1436 | VERIFY(alloc_cmd_return.result == vk::Result::eSuccess); |
| 1437 | swapchain_image_resource.graphics_to_present_cmd = alloc_cmd_return.value[0]; |
| 1438 | build_image_ownership_cmd(swapchain_image_resource); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1439 | } |
| 1440 | } |
| 1441 | |
| 1442 | prepare_descriptor_pool(); |
| 1443 | prepare_descriptor_set(); |
| 1444 | |
| 1445 | prepare_framebuffers(); |
| 1446 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1447 | for (const auto &swapchain_image_resource : swapchain_image_resources) { |
| 1448 | draw_build_cmd(swapchain_image_resource); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | /* |
| 1452 | * Prepare functions above may generate pipeline commands |
| 1453 | * that need to be flushed before beginning the render loop. |
| 1454 | */ |
| 1455 | flush_init_cmd(); |
Tony-LunarG | bc9fc05 | 2018-09-21 13:47:06 -0600 | [diff] [blame] | 1456 | if (staging_texture.buffer) { |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1457 | destroy_texture(staging_texture); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1458 | } |
| 1459 | |
| 1460 | current_buffer = 0; |
| 1461 | prepared = true; |
| 1462 | } |
| 1463 | |
| 1464 | void Demo::prepare_buffers() { |
| 1465 | vk::SwapchainKHR oldSwapchain = swapchain; |
| 1466 | |
| 1467 | // Check the surface capabilities and formats |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1468 | auto surface_capabilities_return = gpu.getSurfaceCapabilitiesKHR(surface); |
| 1469 | VERIFY(surface_capabilities_return.result == vk::Result::eSuccess); |
| 1470 | auto surfCapabilities = surface_capabilities_return.value; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1471 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1472 | auto present_modes_return = gpu.getSurfacePresentModesKHR(surface); |
| 1473 | VERIFY(present_modes_return.result == vk::Result::eSuccess); |
| 1474 | auto present_modes = present_modes_return.value; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1475 | |
| 1476 | vk::Extent2D swapchainExtent; |
| 1477 | // width and height are either both -1, or both not -1. |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1478 | if (surfCapabilities.currentExtent.width == static_cast<uint32_t>(-1)) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1479 | // If the surface size is undefined, the size is set to |
| 1480 | // the size of the images requested. |
| 1481 | swapchainExtent.width = width; |
| 1482 | swapchainExtent.height = height; |
| 1483 | } else { |
| 1484 | // If the surface size is defined, the swap chain size must match |
| 1485 | swapchainExtent = surfCapabilities.currentExtent; |
| 1486 | width = surfCapabilities.currentExtent.width; |
| 1487 | height = surfCapabilities.currentExtent.height; |
| 1488 | } |
| 1489 | |
| 1490 | // The FIFO present mode is guaranteed by the spec to be supported |
| 1491 | // and to have no tearing. It's a great default present mode to use. |
| 1492 | vk::PresentModeKHR swapchainPresentMode = vk::PresentModeKHR::eFifo; |
| 1493 | |
| 1494 | // There are times when you may wish to use another present mode. The |
| 1495 | // following code shows how to select them, and the comments provide some |
| 1496 | // reasons you may wish to use them. |
| 1497 | // |
| 1498 | // It should be noted that Vulkan 1.0 doesn't provide a method for |
| 1499 | // synchronizing rendering with the presentation engine's display. There |
| 1500 | // is a method provided for throttling rendering with the display, but |
| 1501 | // there are some presentation engines for which this method will not work. |
| 1502 | // If an application doesn't throttle its rendering, and if it renders much |
| 1503 | // faster than the refresh rate of the display, this can waste power on |
| 1504 | // mobile devices. That is because power is being spent rendering images |
| 1505 | // that may never be seen. |
| 1506 | |
| 1507 | // VK_PRESENT_MODE_IMMEDIATE_KHR is for applications that don't care |
| 1508 | // about |
| 1509 | // tearing, or have some way of synchronizing their rendering with the |
| 1510 | // display. |
| 1511 | // VK_PRESENT_MODE_MAILBOX_KHR may be useful for applications that |
| 1512 | // generally render a new presentable image every refresh cycle, but are |
| 1513 | // occasionally early. In this case, the application wants the new |
| 1514 | // image |
| 1515 | // to be displayed instead of the previously-queued-for-presentation |
| 1516 | // image |
| 1517 | // that has not yet been displayed. |
| 1518 | // VK_PRESENT_MODE_FIFO_RELAXED_KHR is for applications that generally |
| 1519 | // render a new presentable image every refresh cycle, but are |
| 1520 | // occasionally |
| 1521 | // late. In this case (perhaps because of stuttering/latency concerns), |
| 1522 | // the application wants the late image to be immediately displayed, |
| 1523 | // even |
| 1524 | // though that may mean some tearing. |
| 1525 | |
| 1526 | if (presentMode != swapchainPresentMode) { |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1527 | for (const auto &mode : present_modes) { |
| 1528 | if (mode == presentMode) { |
| 1529 | swapchainPresentMode = mode; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1530 | break; |
| 1531 | } |
| 1532 | } |
| 1533 | } |
| 1534 | |
| 1535 | if (swapchainPresentMode != presentMode) { |
| 1536 | ERR_EXIT("Present mode specified is not supported\n", "Present mode unsupported"); |
| 1537 | } |
| 1538 | |
| 1539 | // Determine the number of VkImages to use in the swap chain. |
| 1540 | // Application desires to acquire 3 images at a time for triple |
| 1541 | // buffering |
| 1542 | uint32_t desiredNumOfSwapchainImages = 3; |
| 1543 | if (desiredNumOfSwapchainImages < surfCapabilities.minImageCount) { |
| 1544 | desiredNumOfSwapchainImages = surfCapabilities.minImageCount; |
| 1545 | } |
| 1546 | |
| 1547 | // If maxImageCount is 0, we can ask for as many images as we want, |
| 1548 | // otherwise |
| 1549 | // we're limited to maxImageCount |
| 1550 | if ((surfCapabilities.maxImageCount > 0) && (desiredNumOfSwapchainImages > surfCapabilities.maxImageCount)) { |
| 1551 | // Application must settle for fewer images than desired: |
| 1552 | desiredNumOfSwapchainImages = surfCapabilities.maxImageCount; |
| 1553 | } |
| 1554 | |
| 1555 | vk::SurfaceTransformFlagBitsKHR preTransform; |
| 1556 | if (surfCapabilities.supportedTransforms & vk::SurfaceTransformFlagBitsKHR::eIdentity) { |
| 1557 | preTransform = vk::SurfaceTransformFlagBitsKHR::eIdentity; |
| 1558 | } else { |
| 1559 | preTransform = surfCapabilities.currentTransform; |
| 1560 | } |
| 1561 | |
| 1562 | // Find a supported composite alpha mode - one of these is guaranteed to be set |
| 1563 | vk::CompositeAlphaFlagBitsKHR compositeAlpha = vk::CompositeAlphaFlagBitsKHR::eOpaque; |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 1564 | std::array<vk::CompositeAlphaFlagBitsKHR, 4> compositeAlphaFlags = { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1565 | vk::CompositeAlphaFlagBitsKHR::eOpaque, |
| 1566 | vk::CompositeAlphaFlagBitsKHR::ePreMultiplied, |
| 1567 | vk::CompositeAlphaFlagBitsKHR::ePostMultiplied, |
| 1568 | vk::CompositeAlphaFlagBitsKHR::eInherit, |
| 1569 | }; |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 1570 | for (const auto &compositeAlphaFlag : compositeAlphaFlags) { |
| 1571 | if (surfCapabilities.supportedCompositeAlpha & compositeAlphaFlag) { |
| 1572 | compositeAlpha = compositeAlphaFlag; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1573 | break; |
| 1574 | } |
| 1575 | } |
| 1576 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1577 | auto swapchain_return = device.createSwapchainKHR(vk::SwapchainCreateInfoKHR() |
| 1578 | .setSurface(surface) |
| 1579 | .setMinImageCount(desiredNumOfSwapchainImages) |
| 1580 | .setImageFormat(format) |
| 1581 | .setImageColorSpace(color_space) |
| 1582 | .setImageExtent({swapchainExtent.width, swapchainExtent.height}) |
| 1583 | .setImageArrayLayers(1) |
| 1584 | .setImageUsage(vk::ImageUsageFlagBits::eColorAttachment) |
| 1585 | .setImageSharingMode(vk::SharingMode::eExclusive) |
| 1586 | .setPreTransform(preTransform) |
| 1587 | .setCompositeAlpha(compositeAlpha) |
| 1588 | .setPresentMode(swapchainPresentMode) |
| 1589 | .setClipped(true) |
| 1590 | .setOldSwapchain(oldSwapchain)); |
| 1591 | VERIFY(swapchain_return.result == vk::Result::eSuccess); |
| 1592 | swapchain = swapchain_return.value; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1593 | |
| 1594 | // If we just re-created an existing swapchain, we should destroy the |
| 1595 | // old |
| 1596 | // swapchain at this point. |
| 1597 | // Note: destroying the swapchain also cleans up all its associated |
| 1598 | // presentable images once the platform is done with them. |
| 1599 | if (oldSwapchain) { |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 1600 | device.destroySwapchainKHR(oldSwapchain); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1601 | } |
| 1602 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1603 | auto swapchain_images_return = device.getSwapchainImagesKHR(swapchain); |
| 1604 | VERIFY(swapchain_images_return.result == vk::Result::eSuccess); |
| 1605 | swapchain_image_resources.resize(swapchain_images_return.value.size()); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1606 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1607 | for (uint32_t i = 0; i < swapchain_image_resources.size(); ++i) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1608 | auto color_image_view = vk::ImageViewCreateInfo() |
| 1609 | .setViewType(vk::ImageViewType::e2D) |
| 1610 | .setFormat(format) |
| 1611 | .setSubresourceRange(vk::ImageSubresourceRange(vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1)); |
| 1612 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1613 | swapchain_image_resources[i].image = swapchain_images_return.value[i]; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1614 | |
| 1615 | color_image_view.image = swapchain_image_resources[i].image; |
| 1616 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1617 | auto image_view_return = device.createImageView(color_image_view); |
| 1618 | VERIFY(image_view_return.result == vk::Result::eSuccess); |
| 1619 | swapchain_image_resources[i].view = image_view_return.value; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1620 | } |
| 1621 | } |
| 1622 | |
| 1623 | void Demo::prepare_cube_data_buffers() { |
| 1624 | mat4x4 VP; |
| 1625 | mat4x4_mul(VP, projection_matrix, view_matrix); |
| 1626 | |
| 1627 | mat4x4 MVP; |
| 1628 | mat4x4_mul(MVP, VP, model_matrix); |
| 1629 | |
| 1630 | vktexcube_vs_uniform data; |
| 1631 | memcpy(data.mvp, MVP, sizeof(MVP)); |
| 1632 | // dumpMatrix("MVP", MVP) |
| 1633 | |
| 1634 | for (int32_t i = 0; i < 12 * 3; i++) { |
| 1635 | data.position[i][0] = g_vertex_buffer_data[i * 3]; |
| 1636 | data.position[i][1] = g_vertex_buffer_data[i * 3 + 1]; |
| 1637 | data.position[i][2] = g_vertex_buffer_data[i * 3 + 2]; |
| 1638 | data.position[i][3] = 1.0f; |
| 1639 | data.attr[i][0] = g_uv_buffer_data[2 * i]; |
| 1640 | data.attr[i][1] = g_uv_buffer_data[2 * i + 1]; |
| 1641 | data.attr[i][2] = 0; |
| 1642 | data.attr[i][3] = 0; |
| 1643 | } |
| 1644 | |
| 1645 | auto const buf_info = vk::BufferCreateInfo().setSize(sizeof(data)).setUsage(vk::BufferUsageFlagBits::eUniformBuffer); |
| 1646 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1647 | for (auto &swapchain_image_resource : swapchain_image_resources) { |
| 1648 | auto result = device.createBuffer(&buf_info, nullptr, &swapchain_image_resource.uniform_buffer); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1649 | VERIFY(result == vk::Result::eSuccess); |
| 1650 | |
| 1651 | vk::MemoryRequirements mem_reqs; |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1652 | device.getBufferMemoryRequirements(swapchain_image_resource.uniform_buffer, &mem_reqs); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1653 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1654 | auto mem_alloc = vk::MemoryAllocateInfo().setAllocationSize(mem_reqs.size).setMemoryTypeIndex(0); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1655 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1656 | bool const pass = memory_type_from_properties( |
| 1657 | mem_reqs.memoryTypeBits, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent, |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1658 | mem_alloc.memoryTypeIndex); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1659 | VERIFY(pass); |
| 1660 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1661 | result = device.allocateMemory(&mem_alloc, nullptr, &swapchain_image_resource.uniform_memory); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1662 | VERIFY(result == vk::Result::eSuccess); |
| 1663 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1664 | result = device.mapMemory(swapchain_image_resource.uniform_memory, 0, VK_WHOLE_SIZE, vk::MemoryMapFlags(), |
| 1665 | &swapchain_image_resource.uniform_memory_ptr); |
Tony-LunarG | 37af49f | 2019-12-19 12:04:19 -0700 | [diff] [blame] | 1666 | VERIFY(result == vk::Result::eSuccess); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1667 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1668 | memcpy(swapchain_image_resource.uniform_memory_ptr, &data, sizeof data); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1669 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1670 | result = device.bindBufferMemory(swapchain_image_resource.uniform_buffer, swapchain_image_resource.uniform_memory, 0); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1671 | VERIFY(result == vk::Result::eSuccess); |
| 1672 | } |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1673 | } |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1674 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1675 | void Demo::prepare_depth() { |
| 1676 | depth.format = vk::Format::eD16Unorm; |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1677 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1678 | auto const image = vk::ImageCreateInfo() |
| 1679 | .setImageType(vk::ImageType::e2D) |
| 1680 | .setFormat(depth.format) |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1681 | .setExtent({static_cast<uint32_t>(width), static_cast<uint32_t>(height), 1}) |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1682 | .setMipLevels(1) |
| 1683 | .setArrayLayers(1) |
| 1684 | .setSamples(vk::SampleCountFlagBits::e1) |
| 1685 | .setTiling(vk::ImageTiling::eOptimal) |
| 1686 | .setUsage(vk::ImageUsageFlagBits::eDepthStencilAttachment) |
| 1687 | .setSharingMode(vk::SharingMode::eExclusive) |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1688 | .setInitialLayout(vk::ImageLayout::eUndefined); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1689 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1690 | auto result = device.createImage(&image, nullptr, &depth.image); |
| 1691 | VERIFY(result == vk::Result::eSuccess); |
| 1692 | |
| 1693 | vk::MemoryRequirements mem_reqs; |
| 1694 | device.getImageMemoryRequirements(depth.image, &mem_reqs); |
| 1695 | |
| 1696 | depth.mem_alloc.setAllocationSize(mem_reqs.size); |
| 1697 | depth.mem_alloc.setMemoryTypeIndex(0); |
| 1698 | |
| 1699 | auto const pass = memory_type_from_properties(mem_reqs.memoryTypeBits, vk::MemoryPropertyFlagBits::eDeviceLocal, |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1700 | depth.mem_alloc.memoryTypeIndex); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1701 | VERIFY(pass); |
| 1702 | |
| 1703 | result = device.allocateMemory(&depth.mem_alloc, nullptr, &depth.mem); |
| 1704 | VERIFY(result == vk::Result::eSuccess); |
| 1705 | |
| 1706 | result = device.bindImageMemory(depth.image, depth.mem, 0); |
| 1707 | VERIFY(result == vk::Result::eSuccess); |
| 1708 | |
Mark Young | 43c08f8 | 2021-11-30 16:38:25 -0700 | [diff] [blame] | 1709 | auto view = vk::ImageViewCreateInfo() |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1710 | .setImage(depth.image) |
| 1711 | .setViewType(vk::ImageViewType::e2D) |
| 1712 | .setFormat(depth.format) |
| 1713 | .setSubresourceRange(vk::ImageSubresourceRange(vk::ImageAspectFlagBits::eDepth, 0, 1, 0, 1)); |
Mark Young | 43c08f8 | 2021-11-30 16:38:25 -0700 | [diff] [blame] | 1714 | if (force_errors) { |
| 1715 | // Intentionally force a bad pNext value to generate a validation layer error |
| 1716 | view.pNext = ℑ |
| 1717 | } |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1718 | result = device.createImageView(&view, nullptr, &depth.view); |
| 1719 | VERIFY(result == vk::Result::eSuccess); |
| 1720 | } |
| 1721 | |
| 1722 | void Demo::prepare_descriptor_layout() { |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1723 | std::array<vk::DescriptorSetLayoutBinding, 2> const layout_bindings = { |
| 1724 | vk::DescriptorSetLayoutBinding() |
| 1725 | .setBinding(0) |
| 1726 | .setDescriptorType(vk::DescriptorType::eUniformBuffer) |
| 1727 | .setDescriptorCount(1) |
| 1728 | .setStageFlags(vk::ShaderStageFlagBits::eVertex) |
| 1729 | .setPImmutableSamplers(nullptr), |
| 1730 | vk::DescriptorSetLayoutBinding() |
| 1731 | .setBinding(1) |
| 1732 | .setDescriptorType(vk::DescriptorType::eCombinedImageSampler) |
| 1733 | .setDescriptorCount(texture_count) |
| 1734 | .setStageFlags(vk::ShaderStageFlagBits::eFragment) |
| 1735 | .setPImmutableSamplers(nullptr)}; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1736 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1737 | auto const descriptor_layout = vk::DescriptorSetLayoutCreateInfo().setBindings(layout_bindings); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1738 | |
| 1739 | auto result = device.createDescriptorSetLayout(&descriptor_layout, nullptr, &desc_layout); |
| 1740 | VERIFY(result == vk::Result::eSuccess); |
| 1741 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1742 | auto const pPipelineLayoutCreateInfo = vk::PipelineLayoutCreateInfo().setSetLayouts(desc_layout); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1743 | |
| 1744 | result = device.createPipelineLayout(&pPipelineLayoutCreateInfo, nullptr, &pipeline_layout); |
| 1745 | VERIFY(result == vk::Result::eSuccess); |
| 1746 | } |
| 1747 | |
| 1748 | void Demo::prepare_descriptor_pool() { |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1749 | std::array<vk::DescriptorPoolSize, 2> const poolSizes = { |
| 1750 | vk::DescriptorPoolSize() |
| 1751 | .setType(vk::DescriptorType::eUniformBuffer) |
| 1752 | .setDescriptorCount(static_cast<uint32_t>(swapchain_image_resources.size())), |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1753 | vk::DescriptorPoolSize() |
| 1754 | .setType(vk::DescriptorType::eCombinedImageSampler) |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1755 | .setDescriptorCount(static_cast<uint32_t>(swapchain_image_resources.size()) * texture_count)}; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1756 | |
| 1757 | auto const descriptor_pool = |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1758 | vk::DescriptorPoolCreateInfo().setMaxSets(static_cast<uint32_t>(swapchain_image_resources.size())).setPoolSizes(poolSizes); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1759 | |
| 1760 | auto result = device.createDescriptorPool(&descriptor_pool, nullptr, &desc_pool); |
| 1761 | VERIFY(result == vk::Result::eSuccess); |
| 1762 | } |
| 1763 | |
| 1764 | void Demo::prepare_descriptor_set() { |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1765 | auto const alloc_info = vk::DescriptorSetAllocateInfo().setDescriptorPool(desc_pool).setSetLayouts(desc_layout); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1766 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1767 | auto buffer_info = vk::DescriptorBufferInfo().setOffset(0).setRange(sizeof(vktexcube_vs_uniform)); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1768 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1769 | std::array<vk::DescriptorImageInfo, texture_count> tex_descs; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1770 | for (uint32_t i = 0; i < texture_count; i++) { |
| 1771 | tex_descs[i].setSampler(textures[i].sampler); |
| 1772 | tex_descs[i].setImageView(textures[i].view); |
Karl Schultz | e88bc84 | 2018-09-11 16:23:14 -0600 | [diff] [blame] | 1773 | tex_descs[i].setImageLayout(vk::ImageLayout::eShaderReadOnlyOptimal); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1774 | } |
| 1775 | |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 1776 | std::array<vk::WriteDescriptorSet, 2> writes; |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1777 | writes[0].setDescriptorCount(1).setDescriptorType(vk::DescriptorType::eUniformBuffer).setPBufferInfo(&buffer_info); |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1778 | writes[1] |
| 1779 | .setDstBinding(1) |
| 1780 | .setDescriptorCount(texture_count) |
| 1781 | .setDescriptorType(vk::DescriptorType::eCombinedImageSampler) |
| 1782 | .setImageInfo(tex_descs); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1783 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1784 | for (auto &swapchain_image_resource : swapchain_image_resources) { |
| 1785 | auto result = device.allocateDescriptorSets(&alloc_info, &swapchain_image_resource.descriptor_set); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1786 | VERIFY(result == vk::Result::eSuccess); |
| 1787 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1788 | buffer_info.setBuffer(swapchain_image_resource.uniform_buffer); |
| 1789 | writes[0].setDstSet(swapchain_image_resource.descriptor_set); |
| 1790 | writes[1].setDstSet(swapchain_image_resource.descriptor_set); |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 1791 | device.updateDescriptorSets(writes, {}); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1792 | } |
| 1793 | } |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1794 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1795 | void Demo::prepare_framebuffers() { |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1796 | std::array<vk::ImageView, 2> attachments; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1797 | attachments[1] = depth.view; |
| 1798 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1799 | for (auto &swapchain_image_resource : swapchain_image_resources) { |
| 1800 | attachments[0] = swapchain_image_resource.view; |
| 1801 | auto const framebuffer_return = device.createFramebuffer(vk::FramebufferCreateInfo() |
| 1802 | .setRenderPass(render_pass) |
| 1803 | .setAttachments(attachments) |
| 1804 | .setWidth(static_cast<uint32_t>(width)) |
| 1805 | .setHeight(static_cast<uint32_t>(height)) |
| 1806 | .setLayers(1)); |
| 1807 | VERIFY(framebuffer_return.result == vk::Result::eSuccess); |
| 1808 | swapchain_image_resource.framebuffer = framebuffer_return.value; |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1809 | } |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1810 | } |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1811 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1812 | vk::ShaderModule Demo::prepare_fs() { |
| 1813 | const uint32_t fragShaderCode[] = { |
Petr Kraus | 9a4eb6a | 2017-11-30 14:49:20 +0100 | [diff] [blame] | 1814 | #include "cube.frag.inc" |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1815 | }; |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1816 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1817 | frag_shader_module = prepare_shader_module(fragShaderCode, sizeof(fragShaderCode)); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 1818 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1819 | return frag_shader_module; |
| 1820 | } |
| 1821 | |
| 1822 | void Demo::prepare_pipeline() { |
| 1823 | vk::PipelineCacheCreateInfo const pipelineCacheInfo; |
| 1824 | auto result = device.createPipelineCache(&pipelineCacheInfo, nullptr, &pipelineCache); |
| 1825 | VERIFY(result == vk::Result::eSuccess); |
| 1826 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1827 | std::array<vk::PipelineShaderStageCreateInfo, 2> const shaderStageInfo = { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1828 | vk::PipelineShaderStageCreateInfo().setStage(vk::ShaderStageFlagBits::eVertex).setModule(prepare_vs()).setPName("main"), |
| 1829 | vk::PipelineShaderStageCreateInfo().setStage(vk::ShaderStageFlagBits::eFragment).setModule(prepare_fs()).setPName("main")}; |
| 1830 | |
| 1831 | vk::PipelineVertexInputStateCreateInfo const vertexInputInfo; |
| 1832 | |
| 1833 | auto const inputAssemblyInfo = vk::PipelineInputAssemblyStateCreateInfo().setTopology(vk::PrimitiveTopology::eTriangleList); |
| 1834 | |
| 1835 | // TODO: Where are pViewports and pScissors set? |
| 1836 | auto const viewportInfo = vk::PipelineViewportStateCreateInfo().setViewportCount(1).setScissorCount(1); |
| 1837 | |
| 1838 | auto const rasterizationInfo = vk::PipelineRasterizationStateCreateInfo() |
| 1839 | .setDepthClampEnable(VK_FALSE) |
| 1840 | .setRasterizerDiscardEnable(VK_FALSE) |
| 1841 | .setPolygonMode(vk::PolygonMode::eFill) |
| 1842 | .setCullMode(vk::CullModeFlagBits::eBack) |
| 1843 | .setFrontFace(vk::FrontFace::eCounterClockwise) |
| 1844 | .setDepthBiasEnable(VK_FALSE) |
| 1845 | .setLineWidth(1.0f); |
| 1846 | |
| 1847 | auto const multisampleInfo = vk::PipelineMultisampleStateCreateInfo(); |
| 1848 | |
| 1849 | auto const stencilOp = |
| 1850 | vk::StencilOpState().setFailOp(vk::StencilOp::eKeep).setPassOp(vk::StencilOp::eKeep).setCompareOp(vk::CompareOp::eAlways); |
| 1851 | |
| 1852 | auto const depthStencilInfo = vk::PipelineDepthStencilStateCreateInfo() |
| 1853 | .setDepthTestEnable(VK_TRUE) |
| 1854 | .setDepthWriteEnable(VK_TRUE) |
| 1855 | .setDepthCompareOp(vk::CompareOp::eLessOrEqual) |
| 1856 | .setDepthBoundsTestEnable(VK_FALSE) |
| 1857 | .setStencilTestEnable(VK_FALSE) |
| 1858 | .setFront(stencilOp) |
| 1859 | .setBack(stencilOp); |
| 1860 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1861 | std::array<vk::PipelineColorBlendAttachmentState, 1> const colorBlendAttachments = { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1862 | vk::PipelineColorBlendAttachmentState().setColorWriteMask(vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG | |
| 1863 | vk::ColorComponentFlagBits::eB | vk::ColorComponentFlagBits::eA)}; |
| 1864 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1865 | auto const colorBlendInfo = vk::PipelineColorBlendStateCreateInfo().setAttachments(colorBlendAttachments); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1866 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1867 | std::array<vk::DynamicState, 2> const dynamicStates = {vk::DynamicState::eViewport, vk::DynamicState::eScissor}; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1868 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1869 | auto const dynamicStateInfo = vk::PipelineDynamicStateCreateInfo().setDynamicStates(dynamicStates); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1870 | |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 1871 | auto pipline_return = device.createGraphicsPipelines(pipelineCache, vk::GraphicsPipelineCreateInfo() |
| 1872 | .setStages(shaderStageInfo) |
| 1873 | .setPVertexInputState(&vertexInputInfo) |
| 1874 | .setPInputAssemblyState(&inputAssemblyInfo) |
| 1875 | .setPViewportState(&viewportInfo) |
| 1876 | .setPRasterizationState(&rasterizationInfo) |
| 1877 | .setPMultisampleState(&multisampleInfo) |
| 1878 | .setPDepthStencilState(&depthStencilInfo) |
| 1879 | .setPColorBlendState(&colorBlendInfo) |
| 1880 | .setPDynamicState(&dynamicStateInfo) |
| 1881 | .setLayout(pipeline_layout) |
| 1882 | .setRenderPass(render_pass)); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1883 | VERIFY(result == vk::Result::eSuccess); |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 1884 | pipeline = pipline_return.value.at(0); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1885 | |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 1886 | device.destroyShaderModule(frag_shader_module); |
| 1887 | device.destroyShaderModule(vert_shader_module); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1888 | } |
| 1889 | |
| 1890 | void Demo::prepare_render_pass() { |
| 1891 | // The initial layout for the color and depth attachments will be LAYOUT_UNDEFINED |
| 1892 | // because at the start of the renderpass, we don't care about their contents. |
| 1893 | // At the start of the subpass, the color attachment's layout will be transitioned |
| 1894 | // to LAYOUT_COLOR_ATTACHMENT_OPTIMAL and the depth stencil attachment's layout |
| 1895 | // will be transitioned to LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL. At the end of |
| 1896 | // the renderpass, the color attachment's layout will be transitioned to |
| 1897 | // LAYOUT_PRESENT_SRC_KHR to be ready to present. This is all done as part of |
| 1898 | // the renderpass, no barriers are necessary. |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1899 | std::array<vk::AttachmentDescription, 2> const attachments = { |
| 1900 | vk::AttachmentDescription() |
| 1901 | .setFormat(format) |
| 1902 | .setSamples(vk::SampleCountFlagBits::e1) |
| 1903 | .setLoadOp(vk::AttachmentLoadOp::eClear) |
| 1904 | .setStoreOp(vk::AttachmentStoreOp::eStore) |
| 1905 | .setStencilLoadOp(vk::AttachmentLoadOp::eDontCare) |
| 1906 | .setStencilStoreOp(vk::AttachmentStoreOp::eDontCare) |
| 1907 | .setInitialLayout(vk::ImageLayout::eUndefined) |
| 1908 | .setFinalLayout(vk::ImageLayout::ePresentSrcKHR), |
| 1909 | vk::AttachmentDescription() |
| 1910 | .setFormat(depth.format) |
| 1911 | .setSamples(vk::SampleCountFlagBits::e1) |
| 1912 | .setLoadOp(vk::AttachmentLoadOp::eClear) |
| 1913 | .setStoreOp(vk::AttachmentStoreOp::eDontCare) |
| 1914 | .setStencilLoadOp(vk::AttachmentLoadOp::eDontCare) |
| 1915 | .setStencilStoreOp(vk::AttachmentStoreOp::eDontCare) |
| 1916 | .setInitialLayout(vk::ImageLayout::eUndefined) |
| 1917 | .setFinalLayout(vk::ImageLayout::eDepthStencilAttachmentOptimal)}; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1918 | |
| 1919 | auto const color_reference = vk::AttachmentReference().setAttachment(0).setLayout(vk::ImageLayout::eColorAttachmentOptimal); |
| 1920 | |
| 1921 | auto const depth_reference = |
| 1922 | vk::AttachmentReference().setAttachment(1).setLayout(vk::ImageLayout::eDepthStencilAttachmentOptimal); |
| 1923 | |
| 1924 | auto const subpass = vk::SubpassDescription() |
| 1925 | .setPipelineBindPoint(vk::PipelineBindPoint::eGraphics) |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1926 | .setColorAttachments(color_reference) |
| 1927 | .setPDepthStencilAttachment(&depth_reference); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1928 | |
Tony-LunarG | 495604b | 2019-06-13 15:32:05 -0600 | [diff] [blame] | 1929 | vk::PipelineStageFlags stages = vk::PipelineStageFlagBits::eEarlyFragmentTests | vk::PipelineStageFlagBits::eLateFragmentTests; |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1930 | std::array<vk::SubpassDependency, 2> const dependencies = { |
Tony-LunarG | 495604b | 2019-06-13 15:32:05 -0600 | [diff] [blame] | 1931 | vk::SubpassDependency() // Depth buffer is shared between swapchain images |
| 1932 | .setSrcSubpass(VK_SUBPASS_EXTERNAL) |
| 1933 | .setDstSubpass(0) |
| 1934 | .setSrcStageMask(stages) |
| 1935 | .setDstStageMask(stages) |
| 1936 | .setSrcAccessMask(vk::AccessFlagBits::eDepthStencilAttachmentWrite) |
| 1937 | .setDstAccessMask(vk::AccessFlagBits::eDepthStencilAttachmentRead | vk::AccessFlagBits::eDepthStencilAttachmentWrite) |
| 1938 | .setDependencyFlags(vk::DependencyFlags()), |
| 1939 | vk::SubpassDependency() // Image layout transition |
| 1940 | .setSrcSubpass(VK_SUBPASS_EXTERNAL) |
| 1941 | .setDstSubpass(0) |
| 1942 | .setSrcStageMask(vk::PipelineStageFlagBits::eColorAttachmentOutput) |
| 1943 | .setDstStageMask(vk::PipelineStageFlagBits::eColorAttachmentOutput) |
| 1944 | .setSrcAccessMask(vk::AccessFlagBits()) |
| 1945 | .setDstAccessMask(vk::AccessFlagBits::eColorAttachmentWrite | vk::AccessFlagBits::eColorAttachmentRead) |
| 1946 | .setDependencyFlags(vk::DependencyFlags()), |
| 1947 | }; |
| 1948 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1949 | const auto render_pass_result = device.createRenderPass( |
| 1950 | vk::RenderPassCreateInfo().setAttachments(attachments).setSubpasses(subpass).setDependencies(dependencies)); |
| 1951 | VERIFY(render_pass_result.result == vk::Result::eSuccess); |
| 1952 | render_pass = render_pass_result.value; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1953 | } |
| 1954 | |
| 1955 | vk::ShaderModule Demo::prepare_shader_module(const uint32_t *code, size_t size) { |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1956 | const auto shader_module_return = device.createShaderModule(vk::ShaderModuleCreateInfo().setCodeSize(size).setPCode(code)); |
| 1957 | VERIFY(shader_module_return.result == vk::Result::eSuccess); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1958 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1959 | return shader_module_return.value; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 1960 | } |
| 1961 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1962 | void Demo::prepare_texture_buffer(const char *filename, texture_object &tex_obj) { |
| 1963 | vk::SubresourceLayout tex_layout; |
Tony-LunarG | bc9fc05 | 2018-09-21 13:47:06 -0600 | [diff] [blame] | 1964 | int32_t tex_width; |
| 1965 | int32_t tex_height; |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 1966 | if (!loadTexture(filename, nullptr, tex_layout, tex_width, tex_height)) { |
Tony-LunarG | bc9fc05 | 2018-09-21 13:47:06 -0600 | [diff] [blame] | 1967 | ERR_EXIT("Failed to load textures", "Load Texture Failure"); |
| 1968 | } |
| 1969 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1970 | tex_obj.tex_width = tex_width; |
| 1971 | tex_obj.tex_height = tex_height; |
Tony-LunarG | bc9fc05 | 2018-09-21 13:47:06 -0600 | [diff] [blame] | 1972 | |
| 1973 | auto const buffer_create_info = vk::BufferCreateInfo() |
| 1974 | .setSize(tex_width * tex_height * 4) |
| 1975 | .setUsage(vk::BufferUsageFlagBits::eTransferSrc) |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1976 | .setSharingMode(vk::SharingMode::eExclusive); |
Tony-LunarG | bc9fc05 | 2018-09-21 13:47:06 -0600 | [diff] [blame] | 1977 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1978 | auto result = device.createBuffer(&buffer_create_info, nullptr, &tex_obj.buffer); |
Tony-LunarG | bc9fc05 | 2018-09-21 13:47:06 -0600 | [diff] [blame] | 1979 | VERIFY(result == vk::Result::eSuccess); |
| 1980 | |
| 1981 | vk::MemoryRequirements mem_reqs; |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1982 | device.getBufferMemoryRequirements(tex_obj.buffer, &mem_reqs); |
Tony-LunarG | bc9fc05 | 2018-09-21 13:47:06 -0600 | [diff] [blame] | 1983 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1984 | tex_obj.mem_alloc.setAllocationSize(mem_reqs.size); |
| 1985 | tex_obj.mem_alloc.setMemoryTypeIndex(0); |
Tony-LunarG | bc9fc05 | 2018-09-21 13:47:06 -0600 | [diff] [blame] | 1986 | |
| 1987 | vk::MemoryPropertyFlags requirements = vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent; |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1988 | auto pass = memory_type_from_properties(mem_reqs.memoryTypeBits, requirements, tex_obj.mem_alloc.memoryTypeIndex); |
Tony-LunarG | bc9fc05 | 2018-09-21 13:47:06 -0600 | [diff] [blame] | 1989 | VERIFY(pass == true); |
| 1990 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1991 | result = device.allocateMemory(&tex_obj.mem_alloc, nullptr, &(tex_obj.mem)); |
Tony-LunarG | bc9fc05 | 2018-09-21 13:47:06 -0600 | [diff] [blame] | 1992 | VERIFY(result == vk::Result::eSuccess); |
| 1993 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1994 | result = device.bindBufferMemory(tex_obj.buffer, tex_obj.mem, 0); |
Tony-LunarG | bc9fc05 | 2018-09-21 13:47:06 -0600 | [diff] [blame] | 1995 | VERIFY(result == vk::Result::eSuccess); |
| 1996 | |
| 1997 | vk::SubresourceLayout layout; |
Tony-LunarG | bc9fc05 | 2018-09-21 13:47:06 -0600 | [diff] [blame] | 1998 | layout.rowPitch = tex_width * 4; |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 1999 | auto data = device.mapMemory(tex_obj.mem, 0, tex_obj.mem_alloc.allocationSize); |
Tony-LunarG | bc9fc05 | 2018-09-21 13:47:06 -0600 | [diff] [blame] | 2000 | VERIFY(data.result == vk::Result::eSuccess); |
| 2001 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2002 | if (!loadTexture(filename, (uint8_t *)data.value, layout, tex_width, tex_height)) { |
Tony-LunarG | bc9fc05 | 2018-09-21 13:47:06 -0600 | [diff] [blame] | 2003 | fprintf(stderr, "Error loading texture: %s\n", filename); |
| 2004 | } |
| 2005 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2006 | device.unmapMemory(tex_obj.mem); |
Tony-LunarG | bc9fc05 | 2018-09-21 13:47:06 -0600 | [diff] [blame] | 2007 | } |
| 2008 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2009 | void Demo::prepare_texture_image(const char *filename, texture_object &tex_obj, vk::ImageTiling tiling, vk::ImageUsageFlags usage, |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2010 | vk::MemoryPropertyFlags required_props) { |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2011 | vk::SubresourceLayout tex_layout; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2012 | int32_t tex_width; |
| 2013 | int32_t tex_height; |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2014 | if (!loadTexture(filename, nullptr, tex_layout, tex_width, tex_height)) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2015 | ERR_EXIT("Failed to load textures", "Load Texture Failure"); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2016 | } |
| 2017 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2018 | tex_obj.tex_width = tex_width; |
| 2019 | tex_obj.tex_height = tex_height; |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2020 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2021 | auto const image_create_info = vk::ImageCreateInfo() |
| 2022 | .setImageType(vk::ImageType::e2D) |
| 2023 | .setFormat(vk::Format::eR8G8B8A8Unorm) |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2024 | .setExtent({static_cast<uint32_t>(tex_width), static_cast<uint32_t>(tex_height), 1}) |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2025 | .setMipLevels(1) |
| 2026 | .setArrayLayers(1) |
| 2027 | .setSamples(vk::SampleCountFlagBits::e1) |
| 2028 | .setTiling(tiling) |
| 2029 | .setUsage(usage) |
| 2030 | .setSharingMode(vk::SharingMode::eExclusive) |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2031 | .setInitialLayout(vk::ImageLayout::ePreinitialized); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2032 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2033 | auto result = device.createImage(&image_create_info, nullptr, &tex_obj.image); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2034 | VERIFY(result == vk::Result::eSuccess); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2035 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2036 | vk::MemoryRequirements mem_reqs; |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2037 | device.getImageMemoryRequirements(tex_obj.image, &mem_reqs); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2038 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2039 | tex_obj.mem_alloc.setAllocationSize(mem_reqs.size); |
| 2040 | tex_obj.mem_alloc.setMemoryTypeIndex(0); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2041 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2042 | auto pass = memory_type_from_properties(mem_reqs.memoryTypeBits, required_props, tex_obj.mem_alloc.memoryTypeIndex); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2043 | VERIFY(pass == true); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2044 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2045 | result = device.allocateMemory(&tex_obj.mem_alloc, nullptr, &tex_obj.mem); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2046 | VERIFY(result == vk::Result::eSuccess); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2047 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2048 | result = device.bindImageMemory(tex_obj.image, tex_obj.mem, 0); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2049 | VERIFY(result == vk::Result::eSuccess); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2050 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2051 | if (required_props & vk::MemoryPropertyFlagBits::eHostVisible) { |
| 2052 | auto const subres = vk::ImageSubresource().setAspectMask(vk::ImageAspectFlagBits::eColor).setMipLevel(0).setArrayLayer(0); |
| 2053 | vk::SubresourceLayout layout; |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2054 | device.getImageSubresourceLayout(tex_obj.image, &subres, &layout); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2055 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2056 | auto data = device.mapMemory(tex_obj.mem, 0, tex_obj.mem_alloc.allocationSize); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2057 | VERIFY(data.result == vk::Result::eSuccess); |
| 2058 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2059 | if (!loadTexture(filename, (uint8_t *)data.value, layout, tex_width, tex_height)) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2060 | fprintf(stderr, "Error loading texture: %s\n", filename); |
| 2061 | } |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2062 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2063 | device.unmapMemory(tex_obj.mem); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2064 | } |
| 2065 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2066 | tex_obj.imageLayout = vk::ImageLayout::eShaderReadOnlyOptimal; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2067 | } |
| 2068 | |
| 2069 | void Demo::prepare_textures() { |
| 2070 | vk::Format const tex_format = vk::Format::eR8G8B8A8Unorm; |
| 2071 | vk::FormatProperties props; |
| 2072 | gpu.getFormatProperties(tex_format, &props); |
| 2073 | |
| 2074 | for (uint32_t i = 0; i < texture_count; i++) { |
| 2075 | if ((props.linearTilingFeatures & vk::FormatFeatureFlagBits::eSampledImage) && !use_staging_buffer) { |
| 2076 | /* Device can texture using linear textures */ |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2077 | prepare_texture_image(tex_files[i], textures[i], vk::ImageTiling::eLinear, vk::ImageUsageFlagBits::eSampled, |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2078 | vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent); |
| 2079 | // Nothing in the pipeline needs to be complete to start, and don't allow fragment |
| 2080 | // shader to run until layout transition completes |
| 2081 | set_image_layout(textures[i].image, vk::ImageAspectFlagBits::eColor, vk::ImageLayout::ePreinitialized, |
| 2082 | textures[i].imageLayout, vk::AccessFlagBits(), vk::PipelineStageFlagBits::eTopOfPipe, |
| 2083 | vk::PipelineStageFlagBits::eFragmentShader); |
| 2084 | staging_texture.image = vk::Image(); |
| 2085 | } else if (props.optimalTilingFeatures & vk::FormatFeatureFlagBits::eSampledImage) { |
| 2086 | /* Must use staging buffer to copy linear texture to optimized */ |
| 2087 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2088 | prepare_texture_buffer(tex_files[i], staging_texture); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2089 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2090 | prepare_texture_image(tex_files[i], textures[i], vk::ImageTiling::eOptimal, |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2091 | vk::ImageUsageFlagBits::eTransferDst | vk::ImageUsageFlagBits::eSampled, |
| 2092 | vk::MemoryPropertyFlagBits::eDeviceLocal); |
| 2093 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2094 | set_image_layout(textures[i].image, vk::ImageAspectFlagBits::eColor, vk::ImageLayout::ePreinitialized, |
| 2095 | vk::ImageLayout::eTransferDstOptimal, vk::AccessFlagBits(), vk::PipelineStageFlagBits::eTopOfPipe, |
| 2096 | vk::PipelineStageFlagBits::eTransfer); |
| 2097 | |
| 2098 | auto const subresource = vk::ImageSubresourceLayers() |
| 2099 | .setAspectMask(vk::ImageAspectFlagBits::eColor) |
| 2100 | .setMipLevel(0) |
| 2101 | .setBaseArrayLayer(0) |
| 2102 | .setLayerCount(1); |
| 2103 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2104 | auto const copy_region = vk::BufferImageCopy() |
| 2105 | .setBufferOffset(0) |
| 2106 | .setBufferRowLength(staging_texture.tex_width) |
| 2107 | .setBufferImageHeight(staging_texture.tex_height) |
| 2108 | .setImageSubresource(subresource) |
| 2109 | .setImageOffset({0, 0, 0}) |
| 2110 | .setImageExtent({static_cast<uint32_t>(staging_texture.tex_width), |
| 2111 | static_cast<uint32_t>(staging_texture.tex_height), 1}); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2112 | |
Tony-LunarG | bc9fc05 | 2018-09-21 13:47:06 -0600 | [diff] [blame] | 2113 | cmd.copyBufferToImage(staging_texture.buffer, textures[i].image, vk::ImageLayout::eTransferDstOptimal, 1, ©_region); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2114 | |
| 2115 | set_image_layout(textures[i].image, vk::ImageAspectFlagBits::eColor, vk::ImageLayout::eTransferDstOptimal, |
| 2116 | textures[i].imageLayout, vk::AccessFlagBits::eTransferWrite, vk::PipelineStageFlagBits::eTransfer, |
| 2117 | vk::PipelineStageFlagBits::eFragmentShader); |
| 2118 | } else { |
| 2119 | assert(!"No support for R8G8B8A8_UNORM as texture image format"); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2120 | } |
| 2121 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2122 | auto const samplerInfo = vk::SamplerCreateInfo() |
| 2123 | .setMagFilter(vk::Filter::eNearest) |
| 2124 | .setMinFilter(vk::Filter::eNearest) |
| 2125 | .setMipmapMode(vk::SamplerMipmapMode::eNearest) |
| 2126 | .setAddressModeU(vk::SamplerAddressMode::eClampToEdge) |
| 2127 | .setAddressModeV(vk::SamplerAddressMode::eClampToEdge) |
| 2128 | .setAddressModeW(vk::SamplerAddressMode::eClampToEdge) |
| 2129 | .setMipLodBias(0.0f) |
| 2130 | .setAnisotropyEnable(VK_FALSE) |
| 2131 | .setMaxAnisotropy(1) |
| 2132 | .setCompareEnable(VK_FALSE) |
| 2133 | .setCompareOp(vk::CompareOp::eNever) |
| 2134 | .setMinLod(0.0f) |
| 2135 | .setMaxLod(0.0f) |
| 2136 | .setBorderColor(vk::BorderColor::eFloatOpaqueWhite) |
| 2137 | .setUnnormalizedCoordinates(VK_FALSE); |
| 2138 | |
| 2139 | auto result = device.createSampler(&samplerInfo, nullptr, &textures[i].sampler); |
| 2140 | VERIFY(result == vk::Result::eSuccess); |
| 2141 | |
| 2142 | auto const viewInfo = vk::ImageViewCreateInfo() |
| 2143 | .setImage(textures[i].image) |
| 2144 | .setViewType(vk::ImageViewType::e2D) |
| 2145 | .setFormat(tex_format) |
| 2146 | .setSubresourceRange(vk::ImageSubresourceRange(vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1)); |
| 2147 | |
| 2148 | result = device.createImageView(&viewInfo, nullptr, &textures[i].view); |
| 2149 | VERIFY(result == vk::Result::eSuccess); |
| 2150 | } |
| 2151 | } |
| 2152 | |
| 2153 | vk::ShaderModule Demo::prepare_vs() { |
| 2154 | const uint32_t vertShaderCode[] = { |
| 2155 | #include "cube.vert.inc" |
| 2156 | }; |
| 2157 | |
| 2158 | vert_shader_module = prepare_shader_module(vertShaderCode, sizeof(vertShaderCode)); |
| 2159 | |
| 2160 | return vert_shader_module; |
| 2161 | } |
| 2162 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2163 | void Demo::destroy_swapchain_related_resources() { |
| 2164 | device.destroyDescriptorPool(desc_pool); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2165 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2166 | device.destroyPipeline(pipeline); |
| 2167 | device.destroyPipelineCache(pipelineCache); |
| 2168 | device.destroyRenderPass(render_pass); |
| 2169 | device.destroyPipelineLayout(pipeline_layout); |
| 2170 | device.destroyDescriptorSetLayout(desc_layout); |
| 2171 | |
| 2172 | for (const auto &tex : textures) { |
| 2173 | device.destroyImageView(tex.view); |
| 2174 | device.destroyImage(tex.image); |
| 2175 | device.freeMemory(tex.mem); |
| 2176 | device.destroySampler(tex.sampler); |
| 2177 | } |
| 2178 | |
| 2179 | device.destroyImageView(depth.view); |
| 2180 | device.destroyImage(depth.image); |
| 2181 | device.freeMemory(depth.mem); |
| 2182 | |
| 2183 | for (const auto &resource : swapchain_image_resources) { |
| 2184 | device.destroyFramebuffer(resource.framebuffer); |
| 2185 | device.destroyImageView(resource.view); |
| 2186 | device.freeCommandBuffers(cmd_pool, {resource.cmd}); |
| 2187 | device.destroyBuffer(resource.uniform_buffer); |
| 2188 | device.unmapMemory(resource.uniform_memory); |
| 2189 | device.freeMemory(resource.uniform_memory); |
| 2190 | } |
| 2191 | |
| 2192 | device.destroyCommandPool(cmd_pool); |
| 2193 | if (separate_present_queue) { |
| 2194 | device.destroyCommandPool(present_cmd_pool); |
| 2195 | } |
| 2196 | } |
| 2197 | |
| 2198 | void Demo::resize() { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2199 | // Don't react to resize until after first initialization. |
| 2200 | if (!prepared) { |
| 2201 | return; |
| 2202 | } |
| 2203 | |
| 2204 | // In order to properly resize the window, we must re-create the |
| 2205 | // swapchain |
| 2206 | // AND redo the command buffers, etc. |
| 2207 | // |
| 2208 | // First, perform part of the cleanup() function: |
| 2209 | prepared = false; |
| 2210 | auto result = device.waitIdle(); |
| 2211 | VERIFY(result == vk::Result::eSuccess); |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2212 | destroy_swapchain_related_resources(); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2213 | |
| 2214 | // Second, re-perform the prepare() function, which will re-create the |
| 2215 | // swapchain. |
| 2216 | prepare(); |
| 2217 | } |
| 2218 | |
| 2219 | void Demo::set_image_layout(vk::Image image, vk::ImageAspectFlags aspectMask, vk::ImageLayout oldLayout, vk::ImageLayout newLayout, |
| 2220 | vk::AccessFlags srcAccessMask, vk::PipelineStageFlags src_stages, vk::PipelineStageFlags dest_stages) { |
| 2221 | assert(cmd); |
| 2222 | |
| 2223 | auto DstAccessMask = [](vk::ImageLayout const &layout) { |
| 2224 | vk::AccessFlags flags; |
| 2225 | |
| 2226 | switch (layout) { |
| 2227 | case vk::ImageLayout::eTransferDstOptimal: |
| 2228 | // Make sure anything that was copying from this image has |
| 2229 | // completed |
| 2230 | flags = vk::AccessFlagBits::eTransferWrite; |
| 2231 | break; |
| 2232 | case vk::ImageLayout::eColorAttachmentOptimal: |
| 2233 | flags = vk::AccessFlagBits::eColorAttachmentWrite; |
| 2234 | break; |
| 2235 | case vk::ImageLayout::eDepthStencilAttachmentOptimal: |
| 2236 | flags = vk::AccessFlagBits::eDepthStencilAttachmentWrite; |
| 2237 | break; |
| 2238 | case vk::ImageLayout::eShaderReadOnlyOptimal: |
| 2239 | // Make sure any Copy or CPU writes to image are flushed |
| 2240 | flags = vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eInputAttachmentRead; |
| 2241 | break; |
| 2242 | case vk::ImageLayout::eTransferSrcOptimal: |
| 2243 | flags = vk::AccessFlagBits::eTransferRead; |
| 2244 | break; |
| 2245 | case vk::ImageLayout::ePresentSrcKHR: |
| 2246 | flags = vk::AccessFlagBits::eMemoryRead; |
| 2247 | break; |
| 2248 | default: |
| 2249 | break; |
| 2250 | } |
| 2251 | |
| 2252 | return flags; |
| 2253 | }; |
| 2254 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2255 | cmd.pipelineBarrier(src_stages, dest_stages, vk::DependencyFlagBits(), {}, {}, |
| 2256 | vk::ImageMemoryBarrier() |
| 2257 | .setSrcAccessMask(srcAccessMask) |
| 2258 | .setDstAccessMask(DstAccessMask(newLayout)) |
| 2259 | .setOldLayout(oldLayout) |
| 2260 | .setNewLayout(newLayout) |
| 2261 | .setSrcQueueFamilyIndex(VK_QUEUE_FAMILY_IGNORED) |
| 2262 | .setDstQueueFamilyIndex(VK_QUEUE_FAMILY_IGNORED) |
| 2263 | .setImage(image) |
| 2264 | .setSubresourceRange(vk::ImageSubresourceRange(aspectMask, 0, 1, 0, 1))); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2265 | } |
| 2266 | |
| 2267 | void Demo::update_data_buffer() { |
| 2268 | mat4x4 VP; |
| 2269 | mat4x4_mul(VP, projection_matrix, view_matrix); |
| 2270 | |
| 2271 | // Rotate around the Y axis |
| 2272 | mat4x4 Model; |
| 2273 | mat4x4_dup(Model, model_matrix); |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2274 | mat4x4_rotate_Y(model_matrix, Model, static_cast<float>(degreesToRadians(spin_angle))); |
Arman Uguray | 3ae0889 | 2021-05-25 00:07:24 -0700 | [diff] [blame] | 2275 | mat4x4_orthonormalize(model_matrix, model_matrix); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2276 | |
| 2277 | mat4x4 MVP; |
| 2278 | mat4x4_mul(MVP, VP, model_matrix); |
| 2279 | |
Tony-LunarG | 37af49f | 2019-12-19 12:04:19 -0700 | [diff] [blame] | 2280 | memcpy(swapchain_image_resources[current_buffer].uniform_memory_ptr, (const void *)&MVP[0][0], sizeof(MVP)); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2281 | } |
| 2282 | |
Karl Schultz | b794040 | 2018-05-29 13:09:22 -0600 | [diff] [blame] | 2283 | /* Convert ppm image data from header file into RGBA texture image */ |
| 2284 | #include "lunarg.ppm.h" |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2285 | bool Demo::loadTexture(const char *filename, uint8_t *rgba_data, vk::SubresourceLayout &layout, int32_t &width, int32_t &height) { |
Karl Schultz | b794040 | 2018-05-29 13:09:22 -0600 | [diff] [blame] | 2286 | (void)filename; |
| 2287 | char *cPtr; |
| 2288 | cPtr = (char *)lunarg_ppm; |
| 2289 | if ((unsigned char *)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2290 | return false; |
| 2291 | } |
Karl Schultz | b794040 | 2018-05-29 13:09:22 -0600 | [diff] [blame] | 2292 | while (strncmp(cPtr++, "\n", 1)) |
| 2293 | ; |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2294 | sscanf(cPtr, "%u %u", &width, &height); |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 2295 | if (rgba_data == nullptr) { |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2296 | return true; |
| 2297 | } |
Karl Schultz | b794040 | 2018-05-29 13:09:22 -0600 | [diff] [blame] | 2298 | while (strncmp(cPtr++, "\n", 1)) |
| 2299 | ; |
| 2300 | if ((unsigned char *)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) { |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2301 | return false; |
| 2302 | } |
Karl Schultz | b794040 | 2018-05-29 13:09:22 -0600 | [diff] [blame] | 2303 | while (strncmp(cPtr++, "\n", 1)) |
| 2304 | ; |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2305 | for (int y = 0; y < height; y++) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2306 | uint8_t *rowPtr = rgba_data; |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2307 | for (int x = 0; x < width; x++) { |
Karl Schultz | b794040 | 2018-05-29 13:09:22 -0600 | [diff] [blame] | 2308 | memcpy(rowPtr, cPtr, 3); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2309 | rowPtr[3] = 255; /* Alpha of 1 */ |
| 2310 | rowPtr += 4; |
Karl Schultz | b794040 | 2018-05-29 13:09:22 -0600 | [diff] [blame] | 2311 | cPtr += 3; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2312 | } |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2313 | rgba_data += layout.rowPitch; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2314 | } |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2315 | return true; |
| 2316 | } |
| 2317 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2318 | bool Demo::memory_type_from_properties(uint32_t typeBits, vk::MemoryPropertyFlags requirements_mask, uint32_t &typeIndex) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2319 | // Search memtypes to find first index with those properties |
| 2320 | for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) { |
| 2321 | if ((typeBits & 1) == 1) { |
| 2322 | // Type is available, does it match user properties? |
| 2323 | if ((memory_properties.memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) { |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2324 | typeIndex = i; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2325 | return true; |
| 2326 | } |
| 2327 | } |
| 2328 | typeBits >>= 1; |
| 2329 | } |
| 2330 | |
| 2331 | // No memory types matched, return failure |
| 2332 | return false; |
| 2333 | } |
| 2334 | |
Charles Giessen | 7dc2dd5 | 2022-02-03 13:55:48 -0700 | [diff] [blame] | 2335 | vk::SurfaceFormatKHR Demo::pick_surface_format(const std::vector<vk::SurfaceFormatKHR> &surface_formats) { |
| 2336 | // Prefer non-SRGB formats... |
| 2337 | for (const auto &surface_format : surface_formats) { |
| 2338 | const vk::Format format = surface_format.format; |
| 2339 | |
| 2340 | if (format == vk::Format::eR8G8B8A8Unorm || format == vk::Format::eB8G8R8A8Unorm || |
| 2341 | format == vk::Format::eA2B10G10R10UnormPack32 || format == vk::Format::eA2R10G10B10UnormPack32 || |
| 2342 | format == vk::Format::eR16G16B16A16Sfloat) { |
| 2343 | return surface_format; |
| 2344 | } |
| 2345 | } |
| 2346 | |
| 2347 | printf("Can't find our preferred formats... Falling back to first exposed format. Rendering may be incorrect.\n"); |
| 2348 | |
| 2349 | assert(surface_formats.size() >= 1); |
| 2350 | return surface_formats[0]; |
| 2351 | } |
| 2352 | |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2353 | #if defined(VK_USE_PLATFORM_WIN32_KHR) |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2354 | void Demo::run() { |
| 2355 | if (!prepared) { |
| 2356 | return; |
| 2357 | } |
| 2358 | |
| 2359 | draw(); |
| 2360 | curFrame++; |
| 2361 | |
Petr Kraus | d88e4e5 | 2019-01-23 18:45:04 +0100 | [diff] [blame] | 2362 | if (frameCount != UINT32_MAX && curFrame == frameCount) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2363 | PostQuitMessage(validation_error); |
| 2364 | } |
| 2365 | } |
| 2366 | |
| 2367 | void Demo::create_window() { |
| 2368 | WNDCLASSEX win_class; |
| 2369 | |
| 2370 | // Initialize the window class structure: |
| 2371 | win_class.cbSize = sizeof(WNDCLASSEX); |
| 2372 | win_class.style = CS_HREDRAW | CS_VREDRAW; |
| 2373 | win_class.lpfnWndProc = WndProc; |
| 2374 | win_class.cbClsExtra = 0; |
| 2375 | win_class.cbWndExtra = 0; |
| 2376 | win_class.hInstance = connection; // hInstance |
| 2377 | win_class.hIcon = LoadIcon(nullptr, IDI_APPLICATION); |
| 2378 | win_class.hCursor = LoadCursor(nullptr, IDC_ARROW); |
| 2379 | win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); |
| 2380 | win_class.lpszMenuName = nullptr; |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2381 | win_class.lpszClassName = name.c_str(); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2382 | win_class.hIconSm = LoadIcon(nullptr, IDI_WINLOGO); |
| 2383 | |
| 2384 | // Register window class: |
| 2385 | if (!RegisterClassEx(&win_class)) { |
| 2386 | // It didn't work, so try to give a useful error: |
| 2387 | printf("Unexpected error trying to start the application!\n"); |
| 2388 | fflush(stdout); |
| 2389 | exit(1); |
| 2390 | } |
| 2391 | |
| 2392 | // Create window with the registered class: |
| 2393 | RECT wr = {0, 0, static_cast<LONG>(width), static_cast<LONG>(height)}; |
| 2394 | AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE); |
| 2395 | window = CreateWindowEx(0, |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2396 | name.c_str(), // class name |
| 2397 | name.c_str(), // app name |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2398 | WS_OVERLAPPEDWINDOW | // window style |
| 2399 | WS_VISIBLE | WS_SYSMENU, |
| 2400 | 100, 100, // x/y coords |
| 2401 | wr.right - wr.left, // width |
| 2402 | wr.bottom - wr.top, // height |
| 2403 | nullptr, // handle to parent |
| 2404 | nullptr, // handle to menu |
| 2405 | connection, // hInstance |
| 2406 | nullptr); // no extra parameters |
| 2407 | |
| 2408 | if (!window) { |
| 2409 | // It didn't work, so try to give a useful error: |
| 2410 | printf("Cannot create a window in which to draw!\n"); |
| 2411 | fflush(stdout); |
| 2412 | exit(1); |
| 2413 | } |
| 2414 | |
| 2415 | // Window client area size must be at least 1 pixel high, to prevent |
| 2416 | // crash. |
| 2417 | minsize.x = GetSystemMetrics(SM_CXMINTRACK); |
| 2418 | minsize.y = GetSystemMetrics(SM_CYMINTRACK) + 1; |
| 2419 | } |
| 2420 | #elif defined(VK_USE_PLATFORM_XLIB_KHR) |
| 2421 | |
| 2422 | void Demo::create_xlib_window() { |
| 2423 | const char *display_envar = getenv("DISPLAY"); |
| 2424 | if (display_envar == nullptr || display_envar[0] == '\0') { |
| 2425 | printf("Environment variable DISPLAY requires a valid value.\nExiting ...\n"); |
| 2426 | fflush(stdout); |
| 2427 | exit(1); |
| 2428 | } |
| 2429 | |
| 2430 | XInitThreads(); |
| 2431 | display = XOpenDisplay(nullptr); |
| 2432 | long visualMask = VisualScreenMask; |
| 2433 | int numberOfVisuals; |
| 2434 | XVisualInfo vInfoTemplate = {}; |
| 2435 | vInfoTemplate.screen = DefaultScreen(display); |
| 2436 | XVisualInfo *visualInfo = XGetVisualInfo(display, visualMask, &vInfoTemplate, &numberOfVisuals); |
| 2437 | |
| 2438 | Colormap colormap = XCreateColormap(display, RootWindow(display, vInfoTemplate.screen), visualInfo->visual, AllocNone); |
| 2439 | |
| 2440 | XSetWindowAttributes windowAttributes = {}; |
| 2441 | windowAttributes.colormap = colormap; |
| 2442 | windowAttributes.background_pixel = 0xFFFFFFFF; |
| 2443 | windowAttributes.border_pixel = 0; |
| 2444 | windowAttributes.event_mask = KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask; |
| 2445 | |
| 2446 | xlib_window = |
| 2447 | XCreateWindow(display, RootWindow(display, vInfoTemplate.screen), 0, 0, width, height, 0, visualInfo->depth, InputOutput, |
| 2448 | visualInfo->visual, CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes); |
| 2449 | |
| 2450 | XSelectInput(display, xlib_window, ExposureMask | KeyPressMask); |
| 2451 | XMapWindow(display, xlib_window); |
| 2452 | XFlush(display); |
| 2453 | xlib_wm_delete_window = XInternAtom(display, "WM_DELETE_WINDOW", False); |
| 2454 | } |
| 2455 | |
| 2456 | void Demo::handle_xlib_event(const XEvent *event) { |
| 2457 | switch (event->type) { |
| 2458 | case ClientMessage: |
| 2459 | if ((Atom)event->xclient.data.l[0] == xlib_wm_delete_window) { |
| 2460 | quit = true; |
| 2461 | } |
| 2462 | break; |
| 2463 | case KeyPress: |
| 2464 | switch (event->xkey.keycode) { |
| 2465 | case 0x9: // Escape |
| 2466 | quit = true; |
| 2467 | break; |
| 2468 | case 0x71: // left arrow key |
| 2469 | spin_angle -= spin_increment; |
| 2470 | break; |
| 2471 | case 0x72: // right arrow key |
| 2472 | spin_angle += spin_increment; |
| 2473 | break; |
| 2474 | case 0x41: // space bar |
| 2475 | pause = !pause; |
| 2476 | break; |
| 2477 | } |
| 2478 | break; |
| 2479 | case ConfigureNotify: |
| 2480 | if (((int32_t)width != event->xconfigure.width) || ((int32_t)height != event->xconfigure.height)) { |
| 2481 | width = event->xconfigure.width; |
| 2482 | height = event->xconfigure.height; |
| 2483 | resize(); |
| 2484 | } |
| 2485 | break; |
| 2486 | default: |
| 2487 | break; |
| 2488 | } |
| 2489 | } |
| 2490 | |
| 2491 | void Demo::run_xlib() { |
| 2492 | while (!quit) { |
| 2493 | XEvent event; |
| 2494 | |
| 2495 | if (pause) { |
| 2496 | XNextEvent(display, &event); |
| 2497 | handle_xlib_event(&event); |
| 2498 | } |
| 2499 | while (XPending(display) > 0) { |
| 2500 | XNextEvent(display, &event); |
| 2501 | handle_xlib_event(&event); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2502 | } |
| 2503 | |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2504 | draw(); |
| 2505 | curFrame++; |
| 2506 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2507 | if (frameCount != UINT32_MAX && curFrame == frameCount) { |
| 2508 | quit = true; |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2509 | } |
| 2510 | } |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2511 | } |
Tony Barbour | 153cb06 | 2016-12-07 13:43:36 -0700 | [diff] [blame] | 2512 | #elif defined(VK_USE_PLATFORM_XCB_KHR) |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2513 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2514 | void Demo::handle_xcb_event(const xcb_generic_event_t *event) { |
| 2515 | uint8_t event_code = event->response_type & 0x7f; |
| 2516 | switch (event_code) { |
| 2517 | case XCB_EXPOSE: |
| 2518 | // TODO: Resize window |
| 2519 | break; |
| 2520 | case XCB_CLIENT_MESSAGE: |
| 2521 | if ((*(xcb_client_message_event_t *)event).data.data32[0] == (*atom_wm_delete_window).atom) { |
| 2522 | quit = true; |
| 2523 | } |
| 2524 | break; |
| 2525 | case XCB_KEY_RELEASE: { |
| 2526 | const xcb_key_release_event_t *key = (const xcb_key_release_event_t *)event; |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2527 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2528 | switch (key->detail) { |
| 2529 | case 0x9: // Escape |
| 2530 | quit = true; |
| 2531 | break; |
| 2532 | case 0x71: // left arrow key |
| 2533 | spin_angle -= spin_increment; |
| 2534 | break; |
| 2535 | case 0x72: // right arrow key |
| 2536 | spin_angle += spin_increment; |
| 2537 | break; |
| 2538 | case 0x41: // space bar |
| 2539 | pause = !pause; |
| 2540 | break; |
| 2541 | } |
| 2542 | } break; |
| 2543 | case XCB_CONFIGURE_NOTIFY: { |
| 2544 | const xcb_configure_notify_event_t *cfg = (const xcb_configure_notify_event_t *)event; |
| 2545 | if ((width != cfg->width) || (height != cfg->height)) { |
| 2546 | width = cfg->width; |
| 2547 | height = cfg->height; |
| 2548 | resize(); |
| 2549 | } |
| 2550 | } break; |
| 2551 | default: |
| 2552 | break; |
| 2553 | } |
| 2554 | } |
| 2555 | |
| 2556 | void Demo::run_xcb() { |
| 2557 | xcb_flush(connection); |
| 2558 | |
| 2559 | while (!quit) { |
| 2560 | xcb_generic_event_t *event; |
| 2561 | |
| 2562 | if (pause) { |
| 2563 | event = xcb_wait_for_event(connection); |
| 2564 | } else { |
| 2565 | event = xcb_poll_for_event(connection); |
| 2566 | } |
| 2567 | while (event) { |
| 2568 | handle_xcb_event(event); |
| 2569 | free(event); |
| 2570 | event = xcb_poll_for_event(connection); |
| 2571 | } |
| 2572 | |
| 2573 | draw(); |
| 2574 | curFrame++; |
| 2575 | if (frameCount != UINT32_MAX && curFrame == frameCount) { |
| 2576 | quit = true; |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2577 | } |
| 2578 | } |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2579 | } |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2580 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2581 | void Demo::create_xcb_window() { |
| 2582 | uint32_t value_mask, value_list[32]; |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2583 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2584 | xcb_window = xcb_generate_id(connection); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2585 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2586 | value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; |
| 2587 | value_list[0] = screen->black_pixel; |
| 2588 | value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY; |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2589 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2590 | xcb_create_window(connection, XCB_COPY_FROM_PARENT, xcb_window, screen->root, 0, 0, width, height, 0, |
| 2591 | XCB_WINDOW_CLASS_INPUT_OUTPUT, screen->root_visual, value_mask, value_list); |
| 2592 | |
| 2593 | /* Magic code that will send notification when window is destroyed */ |
| 2594 | xcb_intern_atom_cookie_t cookie = xcb_intern_atom(connection, 1, 12, "WM_PROTOCOLS"); |
| 2595 | xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(connection, cookie, 0); |
| 2596 | |
| 2597 | xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(connection, 0, 16, "WM_DELETE_WINDOW"); |
| 2598 | atom_wm_delete_window = xcb_intern_atom_reply(connection, cookie2, 0); |
| 2599 | |
| 2600 | xcb_change_property(connection, XCB_PROP_MODE_REPLACE, xcb_window, (*reply).atom, 4, 32, 1, &(*atom_wm_delete_window).atom); |
| 2601 | |
| 2602 | free(reply); |
| 2603 | |
| 2604 | xcb_map_window(connection, xcb_window); |
| 2605 | |
| 2606 | // Force the x/y coordinates to 100,100 results are identical in |
| 2607 | // consecutive |
| 2608 | // runs |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2609 | std::array<uint32_t, 2> const coords = {100, 100}; |
| 2610 | xcb_configure_window(connection, xcb_window, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords.data()); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2611 | } |
| 2612 | #elif defined(VK_USE_PLATFORM_WAYLAND_KHR) |
| 2613 | |
| 2614 | void Demo::run() { |
| 2615 | while (!quit) { |
| 2616 | if (pause) { |
| 2617 | wl_display_dispatch(display); |
| 2618 | } else { |
| 2619 | wl_display_dispatch_pending(display); |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2620 | // update_data_buffer(); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2621 | draw(); |
| 2622 | curFrame++; |
Jeremy Hayes | 9d30478 | 2016-10-09 11:48:12 -0600 | [diff] [blame] | 2623 | if (frameCount != UINT32_MAX && curFrame == frameCount) { |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2624 | quit = true; |
| 2625 | } |
| 2626 | } |
| 2627 | } |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2628 | } |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2629 | |
Manuel Stoeckl | ac93933 | 2019-06-26 20:55:53 -0400 | [diff] [blame] | 2630 | static void handle_surface_configure(void *data, xdg_surface *xdg_surface, uint32_t serial) { |
| 2631 | Demo *demo = (Demo *)data; |
| 2632 | xdg_surface_ack_configure(xdg_surface, serial); |
| 2633 | if (demo->xdg_surface_has_been_configured) { |
| 2634 | demo->resize(); |
| 2635 | } |
| 2636 | demo->xdg_surface_has_been_configured = true; |
| 2637 | } |
| 2638 | |
| 2639 | static const xdg_surface_listener surface_listener = {handle_surface_configure}; |
| 2640 | |
| 2641 | static void handle_toplevel_configure(void *data, xdg_toplevel *xdg_toplevel, int32_t width, int32_t height, |
| 2642 | struct wl_array *states) { |
| 2643 | Demo *demo = (Demo *)data; |
| 2644 | /* zero values imply the program may choose its own size, so in that case |
| 2645 | * stay with the existing value (which on startup is the default) */ |
| 2646 | if (width > 0) { |
| 2647 | demo->width = width; |
| 2648 | } |
| 2649 | if (height > 0) { |
| 2650 | demo->height = height; |
| 2651 | } |
| 2652 | // This will be followed by a surface configure |
| 2653 | } |
| 2654 | |
| 2655 | static void handle_toplevel_close(void *data, xdg_toplevel *xdg_toplevel) { |
| 2656 | Demo *demo = (Demo *)data; |
| 2657 | demo->quit = true; |
| 2658 | } |
| 2659 | |
| 2660 | static const xdg_toplevel_listener toplevel_listener = {handle_toplevel_configure, handle_toplevel_close}; |
| 2661 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2662 | void Demo::create_window() { |
Manuel Stoeckl | ac93933 | 2019-06-26 20:55:53 -0400 | [diff] [blame] | 2663 | if (!wm_base) { |
| 2664 | printf("Compositor did not provide the standard protocol xdg-wm-base\n"); |
| 2665 | fflush(stdout); |
| 2666 | exit(1); |
| 2667 | } |
| 2668 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2669 | window = wl_compositor_create_surface(compositor); |
| 2670 | if (!window) { |
| 2671 | printf("Can not create wayland_surface from compositor!\n"); |
| 2672 | fflush(stdout); |
| 2673 | exit(1); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2674 | } |
| 2675 | |
Manuel Stoeckl | ac93933 | 2019-06-26 20:55:53 -0400 | [diff] [blame] | 2676 | window_surface = xdg_wm_base_get_xdg_surface(wm_base, window); |
| 2677 | if (!window_surface) { |
| 2678 | printf("Can not get xdg_surface from wayland_surface!\n"); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2679 | fflush(stdout); |
| 2680 | exit(1); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2681 | } |
Manuel Stoeckl | ac93933 | 2019-06-26 20:55:53 -0400 | [diff] [blame] | 2682 | window_toplevel = xdg_surface_get_toplevel(window_surface); |
| 2683 | if (!window_toplevel) { |
| 2684 | printf("Can not allocate xdg_toplevel for xdg_surface!\n"); |
| 2685 | fflush(stdout); |
| 2686 | exit(1); |
| 2687 | } |
| 2688 | xdg_surface_add_listener(window_surface, &surface_listener, this); |
| 2689 | xdg_toplevel_add_listener(window_toplevel, &toplevel_listener, this); |
| 2690 | xdg_toplevel_set_title(window_toplevel, APP_SHORT_NAME); |
| 2691 | if (xdg_decoration_mgr) { |
| 2692 | // if supported, let the compositor render titlebars for us |
| 2693 | toplevel_decoration = zxdg_decoration_manager_v1_get_toplevel_decoration(xdg_decoration_mgr, window_toplevel); |
| 2694 | zxdg_toplevel_decoration_v1_set_mode(toplevel_decoration, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE); |
| 2695 | } |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2696 | |
Manuel Stoeckl | ac93933 | 2019-06-26 20:55:53 -0400 | [diff] [blame] | 2697 | wl_surface_commit(window); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2698 | } |
Nicolas Caramelli | c8be8c8 | 2020-07-13 17:22:09 +0200 | [diff] [blame] | 2699 | #elif defined(VK_USE_PLATFORM_DIRECTFB_EXT) |
| 2700 | |
| 2701 | void Demo::handle_directfb_event(const DFBInputEvent *event) { |
| 2702 | if (event->type != DIET_KEYPRESS) return; |
| 2703 | switch (event->key_symbol) { |
| 2704 | case DIKS_ESCAPE: // Escape |
| 2705 | quit = true; |
| 2706 | break; |
| 2707 | case DIKS_CURSOR_LEFT: // left arrow key |
| 2708 | spin_angle -= spin_increment; |
| 2709 | break; |
| 2710 | case DIKS_CURSOR_RIGHT: // right arrow key |
| 2711 | spin_angle += spin_increment; |
| 2712 | break; |
| 2713 | case DIKS_SPACE: // space bar |
| 2714 | pause = !pause; |
| 2715 | break; |
| 2716 | default: |
| 2717 | break; |
| 2718 | } |
| 2719 | } |
| 2720 | |
| 2721 | void Demo::run_directfb() { |
| 2722 | while (!quit) { |
| 2723 | DFBInputEvent event; |
| 2724 | |
| 2725 | if (pause) { |
| 2726 | event_buffer->WaitForEvent(event_buffer); |
Nicolas Caramelli | 6d4f1c6 | 2020-07-13 18:10:07 +0200 | [diff] [blame] | 2727 | if (!event_buffer->GetEvent(event_buffer, DFB_EVENT(&event))) handle_directfb_event(&event); |
Nicolas Caramelli | c8be8c8 | 2020-07-13 17:22:09 +0200 | [diff] [blame] | 2728 | } else { |
Nicolas Caramelli | 6d4f1c6 | 2020-07-13 18:10:07 +0200 | [diff] [blame] | 2729 | if (!event_buffer->GetEvent(event_buffer, DFB_EVENT(&event))) handle_directfb_event(&event); |
Nicolas Caramelli | c8be8c8 | 2020-07-13 17:22:09 +0200 | [diff] [blame] | 2730 | |
| 2731 | draw(); |
| 2732 | curFrame++; |
| 2733 | if (frameCount != UINT32_MAX && curFrame == frameCount) { |
| 2734 | quit = true; |
| 2735 | } |
| 2736 | } |
| 2737 | } |
| 2738 | } |
| 2739 | |
| 2740 | void Demo::create_directfb_window() { |
| 2741 | DFBResult ret; |
| 2742 | |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 2743 | ret = DirectFBInit(nullptr, nullptr); |
Nicolas Caramelli | c8be8c8 | 2020-07-13 17:22:09 +0200 | [diff] [blame] | 2744 | if (ret) { |
| 2745 | printf("DirectFBInit failed to initialize DirectFB!\n"); |
| 2746 | fflush(stdout); |
| 2747 | exit(1); |
| 2748 | } |
| 2749 | |
| 2750 | ret = DirectFBCreate(&dfb); |
| 2751 | if (ret) { |
| 2752 | printf("DirectFBCreate failed to create main interface of DirectFB!\n"); |
| 2753 | fflush(stdout); |
| 2754 | exit(1); |
| 2755 | } |
| 2756 | |
| 2757 | DFBSurfaceDescription desc; |
| 2758 | desc.flags = (DFBSurfaceDescriptionFlags)(DSDESC_CAPS | DSDESC_WIDTH | DSDESC_HEIGHT); |
| 2759 | desc.caps = DSCAPS_PRIMARY; |
Nicolas Caramelli | 6d4f1c6 | 2020-07-13 18:10:07 +0200 | [diff] [blame] | 2760 | desc.width = width; |
Nicolas Caramelli | c8be8c8 | 2020-07-13 17:22:09 +0200 | [diff] [blame] | 2761 | desc.height = height; |
| 2762 | ret = dfb->CreateSurface(dfb, &desc, &window); |
| 2763 | if (ret) { |
| 2764 | printf("CreateSurface failed to create DirectFB surface interface!\n"); |
| 2765 | fflush(stdout); |
| 2766 | exit(1); |
| 2767 | } |
| 2768 | |
| 2769 | ret = dfb->CreateInputEventBuffer(dfb, DICAPS_KEYS, DFB_FALSE, &event_buffer); |
| 2770 | if (ret) { |
| 2771 | printf("CreateInputEventBuffer failed to create DirectFB event buffer interface!\n"); |
| 2772 | fflush(stdout); |
| 2773 | exit(1); |
| 2774 | } |
| 2775 | } |
Bill Hollings | 0a0625a | 2019-07-15 17:39:18 -0400 | [diff] [blame] | 2776 | #elif defined(VK_USE_PLATFORM_METAL_EXT) |
Karl Schultz | 206b1c5 | 2018-04-13 18:02:07 -0600 | [diff] [blame] | 2777 | void Demo::run() { |
| 2778 | draw(); |
| 2779 | curFrame++; |
| 2780 | if (frameCount != UINT32_MAX && curFrame == frameCount) { |
| 2781 | quit = true; |
| 2782 | } |
| 2783 | } |
Damien Leone | 600c305 | 2017-01-31 10:26:07 -0700 | [diff] [blame] | 2784 | #elif defined(VK_USE_PLATFORM_DISPLAY_KHR) |
| 2785 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2786 | vk::Result Demo::create_display_surface() { |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 2787 | auto display_properties_return = gpu.getDisplayPropertiesKHR(); |
| 2788 | VERIFY((display_properties_return.result == vk::Result::eSuccess) || |
| 2789 | (display_properties_return.result == vk::Result::eIncomplete)); |
Damien Leone | 600c305 | 2017-01-31 10:26:07 -0700 | [diff] [blame] | 2790 | |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 2791 | auto display = display_properties_return.value.at(0).display; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2792 | |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 2793 | auto display_mode_props_return = gpu.getDisplayModePropertiesKHR(display); |
| 2794 | VERIFY(display_mode_props_return.result == vk::Result::eSuccess); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2795 | |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 2796 | if (display_mode_props_return.value.size() == 0) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2797 | printf("Cannot find any mode for the display!\n"); |
| 2798 | fflush(stdout); |
| 2799 | exit(1); |
| 2800 | } |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 2801 | auto display_mode_prop = display_mode_props_return.value.at(0); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2802 | |
| 2803 | // Get the list of planes |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 2804 | auto display_plane_props_return = gpu.getDisplayPlanePropertiesKHR(); |
| 2805 | VERIFY(display_plane_props_return.result == vk::Result::eSuccess); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2806 | |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 2807 | if (display_plane_props_return.value.size() == 0) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2808 | printf("Cannot find any plane!\n"); |
| 2809 | fflush(stdout); |
| 2810 | exit(1); |
| 2811 | } |
| 2812 | |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 2813 | vk::Bool32 found_plane = VK_FALSE; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2814 | // Find a plane compatible with the display |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 2815 | for (uint32_t plane_index = 0; plane_index < display_plane_props_return.value.size(); plane_index++) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2816 | // Disqualify planes that are bound to a different display |
| 2817 | if (plane_props[plane_index].currentDisplay && (plane_props[plane_index].currentDisplay != display)) { |
| 2818 | continue; |
Damien Leone | 600c305 | 2017-01-31 10:26:07 -0700 | [diff] [blame] | 2819 | } |
| 2820 | |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 2821 | auto display_plane_supported_displays_return = gpu.getDisplayPlaneSupportedDisplaysKHR(plane_index); |
| 2822 | VERIFY(display_plane_supported_displays_return.result == vk::Result::eSuccess); |
Damien Leone | 600c305 | 2017-01-31 10:26:07 -0700 | [diff] [blame] | 2823 | |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 2824 | if (display_plane_supported_displays_return.value.size() == 0) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2825 | continue; |
Damien Leone | 600c305 | 2017-01-31 10:26:07 -0700 | [diff] [blame] | 2826 | } |
| 2827 | |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 2828 | for (const auto &supported_display : display_plane_supported_displays_return.value) { |
| 2829 | if (supported_display == display) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2830 | found_plane = VK_TRUE; |
Damien Leone | 600c305 | 2017-01-31 10:26:07 -0700 | [diff] [blame] | 2831 | break; |
| 2832 | } |
| 2833 | } |
| 2834 | |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2835 | if (found_plane) { |
| 2836 | break; |
Damien Leone | 600c305 | 2017-01-31 10:26:07 -0700 | [diff] [blame] | 2837 | } |
| 2838 | } |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2839 | |
| 2840 | if (!found_plane) { |
| 2841 | printf("Cannot find a plane compatible with the display!\n"); |
| 2842 | fflush(stdout); |
| 2843 | exit(1); |
| 2844 | } |
| 2845 | |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 2846 | vk::DisplayPlaneCapabilitiesKHR planeCaps = gpu.getDisplayPlaneCapabilitiesKHR(mode_props.displayMode, plane_index); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2847 | // Find a supported alpha mode |
| 2848 | vk::DisplayPlaneAlphaFlagBitsKHR alphaMode = vk::DisplayPlaneAlphaFlagBitsKHR::eOpaque; |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2849 | std::array<vk::DisplayPlaneAlphaFlagBitsKHR, 4> alphaModes = { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2850 | vk::DisplayPlaneAlphaFlagBitsKHR::eOpaque, |
| 2851 | vk::DisplayPlaneAlphaFlagBitsKHR::eGlobal, |
| 2852 | vk::DisplayPlaneAlphaFlagBitsKHR::ePerPixel, |
| 2853 | vk::DisplayPlaneAlphaFlagBitsKHR::ePerPixelPremultiplied, |
| 2854 | }; |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2855 | for (const auto &alpha_mode : alphaModes) { |
| 2856 | if (planeCaps.supportedAlpha & alpha_mode) { |
| 2857 | alphaMode = alpha_mode; |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2858 | break; |
| 2859 | } |
| 2860 | } |
| 2861 | |
Charles Giessen | c2f1a94 | 2022-02-06 23:38:39 -0700 | [diff] [blame^] | 2862 | vk::Extent2D image_extent{}; |
| 2863 | image_extent.setWidth(mode_props.parameters.visibleRegion.width).setHeight(mode_props.parameters.visibleRegion.height); |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2864 | |
| 2865 | auto const createInfo = vk::DisplaySurfaceCreateInfoKHR() |
| 2866 | .setDisplayMode(mode_props.displayMode) |
| 2867 | .setPlaneIndex(plane_index) |
| 2868 | .setPlaneStackIndex(plane_props[plane_index].currentStackIndex) |
| 2869 | .setGlobalAlpha(1.0f) |
| 2870 | .setAlphaMode(alphaMode) |
| 2871 | .setImageExtent(image_extent); |
| 2872 | |
| 2873 | return inst.createDisplayPlaneSurfaceKHR(&createInfo, nullptr, &surface); |
| 2874 | } |
| 2875 | |
| 2876 | void Demo::run_display() { |
| 2877 | while (!quit) { |
| 2878 | draw(); |
| 2879 | curFrame++; |
| 2880 | |
Petr Kraus | d88e4e5 | 2019-01-23 18:45:04 +0100 | [diff] [blame] | 2881 | if (frameCount != UINT32_MAX && curFrame == frameCount) { |
Dave Houlton | 5fa4791 | 2018-02-16 11:02:26 -0700 | [diff] [blame] | 2882 | quit = true; |
| 2883 | } |
| 2884 | } |
| 2885 | } |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2886 | #endif |
| 2887 | |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2888 | #if _WIN32 |
| 2889 | // Include header required for parsing the command line options. |
| 2890 | #include <shellapi.h> |
| 2891 | |
| 2892 | Demo demo; |
| 2893 | |
| 2894 | // MS-Windows event handling function: |
Jeremy Hayes | 9d30478 | 2016-10-09 11:48:12 -0600 | [diff] [blame] | 2895 | LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { |
| 2896 | switch (uMsg) { |
Mark Lobodzinski | 85dbd82 | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 2897 | case WM_CLOSE: |
| 2898 | PostQuitMessage(validation_error); |
| 2899 | break; |
| 2900 | case WM_PAINT: |
| 2901 | demo.run(); |
| 2902 | break; |
| 2903 | case WM_GETMINMAXINFO: // set window's minimum size |
| 2904 | ((MINMAXINFO *)lParam)->ptMinTrackSize = demo.minsize; |
| 2905 | return 0; |
Aaron Hagan | 500b9c3 | 2018-10-03 21:56:29 -0400 | [diff] [blame] | 2906 | case WM_ERASEBKGND: |
| 2907 | return 1; |
Mark Lobodzinski | 85dbd82 | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 2908 | case WM_SIZE: |
| 2909 | // Resize the application to the new window size, except when |
| 2910 | // it was minimized. Vulkan doesn't support images or swapchains |
| 2911 | // with width=0 and height=0. |
| 2912 | if (wParam != SIZE_MINIMIZED) { |
| 2913 | demo.width = lParam & 0xffff; |
| 2914 | demo.height = (lParam & 0xffff0000) >> 16; |
| 2915 | demo.resize(); |
| 2916 | } |
| 2917 | break; |
Petr Kraus | d88e4e5 | 2019-01-23 18:45:04 +0100 | [diff] [blame] | 2918 | case WM_KEYDOWN: |
| 2919 | switch (wParam) { |
| 2920 | case VK_ESCAPE: |
| 2921 | PostQuitMessage(validation_error); |
| 2922 | break; |
| 2923 | case VK_LEFT: |
| 2924 | demo.spin_angle -= demo.spin_increment; |
| 2925 | break; |
| 2926 | case VK_RIGHT: |
| 2927 | demo.spin_angle += demo.spin_increment; |
| 2928 | break; |
| 2929 | case VK_SPACE: |
| 2930 | demo.pause = !demo.pause; |
| 2931 | break; |
| 2932 | } |
| 2933 | return 0; |
Mark Lobodzinski | 85dbd82 | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 2934 | default: |
| 2935 | break; |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2936 | } |
| 2937 | |
| 2938 | return (DefWindowProc(hWnd, uMsg, wParam, lParam)); |
| 2939 | } |
| 2940 | |
Mark Lobodzinski | 2dbc266 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 2941 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow) { |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2942 | // TODO: Gah.. refactor. This isn't 1989. |
Mark Lobodzinski | 85dbd82 | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 2943 | MSG msg; // message |
| 2944 | bool done; // flag saying when app is complete |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2945 | int argc; |
| 2946 | char **argv; |
| 2947 | |
Jamie Madill | b2ff650 | 2017-03-15 16:17:46 -0400 | [diff] [blame] | 2948 | // Ensure wParam is initialized. |
| 2949 | msg.wParam = 0; |
| 2950 | |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2951 | // Use the CommandLine functions to get the command line arguments. |
| 2952 | // Unfortunately, Microsoft outputs |
| 2953 | // this information as wide characters for Unicode, and we simply want the |
| 2954 | // Ascii version to be compatible |
| 2955 | // with the non-Windows side. So, we have to convert the information to |
| 2956 | // Ascii character strings. |
| 2957 | LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc); |
Jeremy Hayes | 9d30478 | 2016-10-09 11:48:12 -0600 | [diff] [blame] | 2958 | if (nullptr == commandLineArgs) { |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2959 | argc = 0; |
| 2960 | } |
| 2961 | |
Jeremy Hayes | 9d30478 | 2016-10-09 11:48:12 -0600 | [diff] [blame] | 2962 | if (argc > 0) { |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2963 | argv = (char **)malloc(sizeof(char *) * argc); |
Jeremy Hayes | 9d30478 | 2016-10-09 11:48:12 -0600 | [diff] [blame] | 2964 | if (argv == nullptr) { |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2965 | argc = 0; |
Jeremy Hayes | 9d30478 | 2016-10-09 11:48:12 -0600 | [diff] [blame] | 2966 | } else { |
| 2967 | for (int iii = 0; iii < argc; iii++) { |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2968 | size_t wideCharLen = wcslen(commandLineArgs[iii]); |
| 2969 | size_t numConverted = 0; |
| 2970 | |
| 2971 | argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1)); |
Jeremy Hayes | 9d30478 | 2016-10-09 11:48:12 -0600 | [diff] [blame] | 2972 | if (argv[iii] != nullptr) { |
Mark Lobodzinski | 2dbc266 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 2973 | wcstombs_s(&numConverted, argv[iii], wideCharLen + 1, commandLineArgs[iii], wideCharLen + 1); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2974 | } |
| 2975 | } |
| 2976 | } |
Jeremy Hayes | 9d30478 | 2016-10-09 11:48:12 -0600 | [diff] [blame] | 2977 | } else { |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2978 | argv = nullptr; |
| 2979 | } |
| 2980 | |
| 2981 | demo.init(argc, argv); |
| 2982 | |
| 2983 | // Free up the items we had to allocate for the command line arguments. |
Jeremy Hayes | 9d30478 | 2016-10-09 11:48:12 -0600 | [diff] [blame] | 2984 | if (argc > 0 && argv != nullptr) { |
| 2985 | for (int iii = 0; iii < argc; iii++) { |
| 2986 | if (argv[iii] != nullptr) { |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2987 | free(argv[iii]); |
| 2988 | } |
| 2989 | } |
| 2990 | free(argv); |
| 2991 | } |
| 2992 | |
| 2993 | demo.connection = hInstance; |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 2994 | demo.name = "Vulkan Cube"; |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 2995 | demo.create_window(); |
| 2996 | demo.init_vk_swapchain(); |
| 2997 | |
| 2998 | demo.prepare(); |
| 2999 | |
Mark Lobodzinski | 85dbd82 | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 3000 | done = false; // initialize loop condition variable |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 3001 | |
| 3002 | // main message loop |
Jeremy Hayes | 9d30478 | 2016-10-09 11:48:12 -0600 | [diff] [blame] | 3003 | while (!done) { |
Petr Kraus | d88e4e5 | 2019-01-23 18:45:04 +0100 | [diff] [blame] | 3004 | if (demo.pause) { |
| 3005 | const BOOL succ = WaitMessage(); |
| 3006 | |
| 3007 | if (!succ) { |
| 3008 | const auto &suppress_popups = demo.suppress_popups; |
| 3009 | ERR_EXIT("WaitMessage() failed on paused demo", "event loop error"); |
| 3010 | } |
| 3011 | } |
| 3012 | |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 3013 | PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE); |
Mark Lobodzinski | 85dbd82 | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 3014 | if (msg.message == WM_QUIT) // check for a quit message |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 3015 | { |
Mark Lobodzinski | 85dbd82 | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 3016 | done = true; // if found, quit app |
Jeremy Hayes | 9d30478 | 2016-10-09 11:48:12 -0600 | [diff] [blame] | 3017 | } else { |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 3018 | /* Translate and dispatch to event queue*/ |
| 3019 | TranslateMessage(&msg); |
| 3020 | DispatchMessage(&msg); |
| 3021 | } |
| 3022 | RedrawWindow(demo.window, nullptr, nullptr, RDW_INTERNALPAINT); |
| 3023 | } |
| 3024 | |
| 3025 | demo.cleanup(); |
| 3026 | |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 3027 | return static_cast<int>(msg.wParam); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 3028 | } |
| 3029 | |
Eleni Maria Stea | 8b86fba | 2021-07-03 17:12:30 +0300 | [diff] [blame] | 3030 | #elif defined(__linux__) || defined(__FreeBSD__) |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 3031 | |
Jeremy Hayes | 9d30478 | 2016-10-09 11:48:12 -0600 | [diff] [blame] | 3032 | int main(int argc, char **argv) { |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 3033 | Demo demo; |
| 3034 | |
| 3035 | demo.init(argc, argv); |
| 3036 | |
Tony Barbour | 153cb06 | 2016-12-07 13:43:36 -0700 | [diff] [blame] | 3037 | #if defined(VK_USE_PLATFORM_XCB_KHR) |
Jeremy Hayes | 9d30478 | 2016-10-09 11:48:12 -0600 | [diff] [blame] | 3038 | demo.create_xcb_window(); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 3039 | #elif defined(VK_USE_PLATFORM_XLIB_KHR) |
Tony Barbour | 78d6b57 | 2016-11-14 14:46:33 -0700 | [diff] [blame] | 3040 | demo.use_xlib = true; |
Jeremy Hayes | 9d30478 | 2016-10-09 11:48:12 -0600 | [diff] [blame] | 3041 | demo.create_xlib_window(); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 3042 | #elif defined(VK_USE_PLATFORM_WAYLAND_KHR) |
Jeremy Hayes | 9d30478 | 2016-10-09 11:48:12 -0600 | [diff] [blame] | 3043 | demo.create_window(); |
Nicolas Caramelli | c8be8c8 | 2020-07-13 17:22:09 +0200 | [diff] [blame] | 3044 | #elif defined(VK_USE_PLATFORM_DIRECTFB_EXT) |
| 3045 | demo.create_directfb_window(); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 3046 | #endif |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 3047 | |
| 3048 | demo.init_vk_swapchain(); |
| 3049 | |
| 3050 | demo.prepare(); |
| 3051 | |
Tony Barbour | 153cb06 | 2016-12-07 13:43:36 -0700 | [diff] [blame] | 3052 | #if defined(VK_USE_PLATFORM_XCB_KHR) |
Mark Lobodzinski | 2dbc266 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 3053 | demo.run_xcb(); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 3054 | #elif defined(VK_USE_PLATFORM_XLIB_KHR) |
Mark Lobodzinski | 2dbc266 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 3055 | demo.run_xlib(); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 3056 | #elif defined(VK_USE_PLATFORM_WAYLAND_KHR) |
Mark Lobodzinski | 2dbc266 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 3057 | demo.run(); |
Nicolas Caramelli | c8be8c8 | 2020-07-13 17:22:09 +0200 | [diff] [blame] | 3058 | #elif defined(VK_USE_PLATFORM_DIRECTFB_EXT) |
| 3059 | demo.run_directfb(); |
Damien Leone | 600c305 | 2017-01-31 10:26:07 -0700 | [diff] [blame] | 3060 | #elif defined(VK_USE_PLATFORM_DISPLAY_KHR) |
| 3061 | demo.run_display(); |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 3062 | #endif |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 3063 | |
| 3064 | demo.cleanup(); |
| 3065 | |
| 3066 | return validation_error; |
| 3067 | } |
| 3068 | |
Bill Hollings | 0a0625a | 2019-07-15 17:39:18 -0400 | [diff] [blame] | 3069 | #elif defined(VK_USE_PLATFORM_METAL_EXT) |
Karl Schultz | 9ceac06 | 2017-12-12 10:33:01 -0500 | [diff] [blame] | 3070 | |
| 3071 | // Global function invoked from NS or UI views and controllers to create demo |
Charles Giessen | 87dd88d | 2022-02-03 13:46:29 -0700 | [diff] [blame] | 3072 | static void demo_main(Demo &demo, void *caMetalLayer, int argc, const char *argv[]) { |
Karl Schultz | 9ceac06 | 2017-12-12 10:33:01 -0500 | [diff] [blame] | 3073 | demo.init(argc, (char **)argv); |
Bill Hollings | 0a0625a | 2019-07-15 17:39:18 -0400 | [diff] [blame] | 3074 | demo.caMetalLayer = caMetalLayer; |
Karl Schultz | 9ceac06 | 2017-12-12 10:33:01 -0500 | [diff] [blame] | 3075 | demo.init_vk_swapchain(); |
| 3076 | demo.prepare(); |
| 3077 | demo.spin_angle = 0.4f; |
| 3078 | } |
| 3079 | |
Jeremy Hayes | f56427a | 2016-09-07 15:55:11 -0600 | [diff] [blame] | 3080 | #else |
| 3081 | #error "Platform not supported" |
| 3082 | #endif |