vk::DeviceMemory mem;
vk::ImageView view;
- int32_t tex_width{0};
- int32_t tex_height{0};
+ uint32_t tex_width{0};
+ uint32_t tex_height{0};
};
static char const *const tex_files[] = {"lunarg.ppm"};
void set_image_layout(vk::Image image, vk::ImageAspectFlags aspectMask, vk::ImageLayout oldLayout, vk::ImageLayout newLayout,
vk::AccessFlags srcAccessMask, vk::PipelineStageFlags src_stages, vk::PipelineStageFlags dest_stages);
void update_data_buffer();
- bool loadTexture(const char *filename, uint8_t *rgba_data, vk::SubresourceLayout &layout, int32_t &width, int32_t &height);
+ bool loadTexture(const char *filename, uint8_t *rgba_data, vk::SubresourceLayout &layout, uint32_t &width, uint32_t &height);
bool memory_type_from_properties(uint32_t typeBits, vk::MemoryPropertyFlags requirements_mask, uint32_t &typeIndex);
vk::SurfaceFormatKHR pick_surface_format(const std::vector<vk::SurfaceFormatKHR> &surface_formats);
std::vector<const char *> enabled_layers;
std::vector<const char *> enabled_device_extensions;
- int32_t width = 0;
- int32_t height = 0;
+ uint32_t width = 0;
+ uint32_t height = 0;
vk::Format format;
vk::ColorSpaceKHR color_space;
static void pointer_handle_button(void *data, struct wl_pointer *wl_pointer, uint32_t serial, uint32_t time, uint32_t button,
uint32_t state) {
- Demo *demo = (Demo *)data;
+ Demo &demo = *static_cast<Demo *>(data);
if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED) {
- xdg_toplevel_move(demo->window_toplevel, demo->seat, serial);
+ xdg_toplevel_move(demo.window_toplevel, demo.seat, serial);
}
}
static void keyboard_handle_key(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key,
uint32_t state) {
if (state != WL_KEYBOARD_KEY_STATE_RELEASED) return;
- Demo *demo = (Demo *)data;
+ Demo &demo = *static_cast<Demo *>(data);
switch (key) {
case KEY_ESC: // Escape
- demo->quit = true;
+ demo.quit = true;
break;
case KEY_LEFT: // left arrow key
- demo->spin_angle -= demo->spin_increment;
+ demo.spin_angle -= demo.spin_increment;
break;
case KEY_RIGHT: // right arrow key
- demo->spin_angle += demo->spin_increment;
+ demo.spin_angle += demo.spin_increment;
break;
case KEY_SPACE: // space bar
- demo->pause = !demo->pause;
+ demo.pause = !demo.pause;
break;
}
}
static void seat_handle_capabilities(void *data, wl_seat *seat, uint32_t caps) {
// Subscribe to pointer events
- Demo *demo = (Demo *)data;
- if ((caps & WL_SEAT_CAPABILITY_POINTER) && !demo->pointer) {
- demo->pointer = wl_seat_get_pointer(seat);
- wl_pointer_add_listener(demo->pointer, &pointer_listener, demo);
- } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && demo->pointer) {
- wl_pointer_destroy(demo->pointer);
- demo->pointer = nullptr;
+ Demo &demo = *static_cast<Demo *>(data);
+ if ((caps & WL_SEAT_CAPABILITY_POINTER) && !demo.pointer) {
+ demo.pointer = wl_seat_get_pointer(seat);
+ wl_pointer_add_listener(demo.pointer, &pointer_listener, demo);
+ } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && demo.pointer) {
+ wl_pointer_destroy(demo.pointer);
+ demo.pointer = nullptr;
}
// Subscribe to keyboard events
if (caps & WL_SEAT_CAPABILITY_KEYBOARD) {
- demo->keyboard = wl_seat_get_keyboard(seat);
- wl_keyboard_add_listener(demo->keyboard, &keyboard_listener, demo);
+ demo.keyboard = wl_seat_get_keyboard(seat);
+ wl_keyboard_add_listener(demo.keyboard, &keyboard_listener, demo);
} else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD)) {
- wl_keyboard_destroy(demo->keyboard);
- demo->keyboard = nullptr;
+ wl_keyboard_destroy(demo.keyboard);
+ demo.keyboard = nullptr;
}
}
static const struct xdg_wm_base_listener wm_base_listener = {wm_base_ping};
static void registry_handle_global(void *data, wl_registry *registry, uint32_t id, const char *interface, uint32_t version) {
- Demo *demo = (Demo *)data;
+ Demo &demo = *static_cast<Demo *>(data);
// pickup wayland objects when they appear
if (strcmp(interface, wl_compositor_interface.name) == 0) {
- demo->compositor = (wl_compositor *)wl_registry_bind(registry, id, &wl_compositor_interface, 1);
+ demo.compositor = (wl_compositor *)wl_registry_bind(registry, id, &wl_compositor_interface, 1);
} else if (strcmp(interface, xdg_wm_base_interface.name) == 0) {
- demo->wm_base = (xdg_wm_base *)wl_registry_bind(registry, id, &xdg_wm_base_interface, 1);
- xdg_wm_base_add_listener(demo->wm_base, &wm_base_listener, nullptr);
+ demo.wm_base = (xdg_wm_base *)wl_registry_bind(registry, id, &xdg_wm_base_interface, 1);
+ xdg_wm_base_add_listener(demo.wm_base, &wm_base_listener, nullptr);
} else if (strcmp(interface, wl_seat_interface.name) == 0) {
- demo->seat = (wl_seat *)wl_registry_bind(registry, id, &wl_seat_interface, 1);
- wl_seat_add_listener(demo->seat, &seat_listener, demo);
+ demo.seat = (wl_seat *)wl_registry_bind(registry, id, &wl_seat_interface, 1);
+ wl_seat_add_listener(demo.seat, &seat_listener, demo);
} else if (strcmp(interface, zxdg_decoration_manager_v1_interface.name) == 0) {
- demo->xdg_decoration_mgr =
+ demo.xdg_decoration_mgr =
(zxdg_decoration_manager_v1 *)wl_registry_bind(registry, id, &zxdg_decoration_manager_v1_interface, 1);
}
}
acquire_result =
device.acquireNextImageKHR(swapchain, UINT64_MAX, image_acquired_semaphores[frame_index], vk::Fence(), ¤t_buffer);
if (acquire_result == vk::Result::eErrorOutOfDateKHR) {
- // demo->swapchain is out of date (e.g. the window was resized) and
+ // demo.swapchain is out of date (e.g. the window was resized) and
// must be recreated:
resize();
} else if (acquire_result == vk::Result::eSuboptimalKHR) {
vk::SurfaceCapabilitiesKHR surfCapabilities;
auto caps_result = gpu.getSurfaceCapabilitiesKHR(surface, &surfCapabilities);
VERIFY(caps_result == vk::Result::eSuccess);
- if (surfCapabilities.currentExtent.width != static_cast<uint32_t>(width) ||
- surfCapabilities.currentExtent.height != static_cast<uint32_t>(height)) {
+ if (surfCapabilities.currentExtent.width != width || surfCapabilities.currentExtent.height != height) {
resize();
}
} else if (present_result == vk::Result::eErrorSurfaceLostKHR) {
commandBuffer.beginRenderPass(vk::RenderPassBeginInfo()
.setRenderPass(render_pass)
.setFramebuffer(swapchain_image_resource.framebuffer)
- .setRenderArea(vk::Rect2D(vk::Offset2D(0, 0), vk::Extent2D(static_cast<uint32_t>(width),
- static_cast<uint32_t>(height))))
+ .setRenderArea(vk::Rect2D(vk::Offset2D{}, vk::Extent2D(width, height)))
.setClearValueCount(2)
.setPClearValues(clearValues),
vk::SubpassContents::eInline);
.setMinDepth(0.0f)
.setMaxDepth(1.0f));
- commandBuffer.setScissor(0, vk::Rect2D(vk::Offset2D(0, 0), vk::Extent2D(width, height)));
+ commandBuffer.setScissor(0, vk::Rect2D(vk::Offset2D{}, vk::Extent2D(width, height)));
commandBuffer.draw(12 * 3, 1, 0, 0);
// Note that ending the renderpass changes the image's layout from
// COLOR_ATTACHMENT_OPTIMAL to PRESENT_SRC_KHR
}
void Demo::flush_init_cmd() {
- // TODO: hmm.
- // This function could get called twice if the texture uses a staging
- // buffer
- // In that case the second call should be ignored
- if (!cmd) {
- return;
- }
-
auto result = cmd.end();
VERIFY(result == vk::Result::eSuccess);
device.freeCommandBuffers(cmd_pool, cmd);
device.destroyFence(fence);
-
- cmd = vk::CommandBuffer();
}
void Demo::init(int argc, char **argv) {
continue;
}
if ((strcmp(argv[i], "--present_mode") == 0) && (i < argc - 1)) {
- presentMode = (vk::PresentModeKHR)atoi(argv[i + 1]);
+ presentMode = static_cast<vk::PresentModeKHR>(atoi(argv[i + 1]));
i++;
continue;
}
i++;
continue;
}
- if (strcmp(argv[i], "--width") == 0 && i < argc - 1 && sscanf(argv[i + 1], "%" SCNi32, &width) == 1 && width > 0) {
+ if (strcmp(argv[i], "--width") == 0 && i < argc - 1 && sscanf(argv[i + 1], "%" SCNu32, &width) == 1) {
i++;
continue;
}
- if (strcmp(argv[i], "--height") == 0 && i < argc - 1 && sscanf(argv[i + 1], "%" SCNi32, &height) == 1 && height > 0) {
+ if (strcmp(argv[i], "--height") == 0 && i < argc - 1 && sscanf(argv[i + 1], "%" SCNu32, &height) == 1) {
i++;
continue;
}
auto const image = vk::ImageCreateInfo()
.setImageType(vk::ImageType::e2D)
.setFormat(depth.format)
- .setExtent({static_cast<uint32_t>(width), static_cast<uint32_t>(height), 1})
+ .setExtent({width, height, 1})
.setMipLevels(1)
.setArrayLayers(1)
.setSamples(vk::SampleCountFlagBits::e1)
auto const framebuffer_return = device.createFramebuffer(vk::FramebufferCreateInfo()
.setRenderPass(render_pass)
.setAttachments(attachments)
- .setWidth(static_cast<uint32_t>(width))
- .setHeight(static_cast<uint32_t>(height))
+ .setWidth(width)
+ .setHeight(height)
.setLayers(1));
VERIFY(framebuffer_return.result == vk::Result::eSuccess);
swapchain_image_resource.framebuffer = framebuffer_return.value;
void Demo::prepare_texture_buffer(const char *filename, texture_object &tex_obj) {
vk::SubresourceLayout tex_layout;
- int32_t tex_width;
- int32_t tex_height;
- if (!loadTexture(filename, nullptr, tex_layout, tex_width, tex_height)) {
+
+ if (!loadTexture(filename, nullptr, tex_layout, tex_obj.tex_width, tex_obj.tex_height)) {
ERR_EXIT("Failed to load textures", "Load Texture Failure");
}
- tex_obj.tex_width = tex_width;
- tex_obj.tex_height = tex_height;
-
auto const buffer_create_info = vk::BufferCreateInfo()
- .setSize(tex_width * tex_height * 4)
+ .setSize(tex_obj.tex_width * tex_obj.tex_height * 4)
.setUsage(vk::BufferUsageFlagBits::eTransferSrc)
.setSharingMode(vk::SharingMode::eExclusive);
VERIFY(result == vk::Result::eSuccess);
vk::SubresourceLayout layout;
- layout.rowPitch = tex_width * 4;
+ layout.rowPitch = tex_obj.tex_width * 4;
auto data = device.mapMemory(tex_obj.mem, 0, tex_obj.mem_alloc.allocationSize);
VERIFY(data.result == vk::Result::eSuccess);
- if (!loadTexture(filename, (uint8_t *)data.value, layout, tex_width, tex_height)) {
+ if (!loadTexture(filename, (uint8_t *)data.value, layout, tex_obj.tex_width, tex_obj.tex_height)) {
fprintf(stderr, "Error loading texture: %s\n", filename);
}
void Demo::prepare_texture_image(const char *filename, texture_object &tex_obj, vk::ImageTiling tiling, vk::ImageUsageFlags usage,
vk::MemoryPropertyFlags required_props) {
vk::SubresourceLayout tex_layout;
- int32_t tex_width;
- int32_t tex_height;
- if (!loadTexture(filename, nullptr, tex_layout, tex_width, tex_height)) {
+ if (!loadTexture(filename, nullptr, tex_layout, tex_obj.tex_width, tex_obj.tex_height)) {
ERR_EXIT("Failed to load textures", "Load Texture Failure");
}
- tex_obj.tex_width = tex_width;
- tex_obj.tex_height = tex_height;
-
auto const image_create_info = vk::ImageCreateInfo()
.setImageType(vk::ImageType::e2D)
.setFormat(vk::Format::eR8G8B8A8Unorm)
- .setExtent({static_cast<uint32_t>(tex_width), static_cast<uint32_t>(tex_height), 1})
+ .setExtent({tex_obj.tex_width, tex_obj.tex_height, 1})
.setMipLevels(1)
.setArrayLayers(1)
.setSamples(vk::SampleCountFlagBits::e1)
auto data = device.mapMemory(tex_obj.mem, 0, tex_obj.mem_alloc.allocationSize);
VERIFY(data.result == vk::Result::eSuccess);
- if (!loadTexture(filename, (uint8_t *)data.value, layout, tex_width, tex_height)) {
+ if (!loadTexture(filename, (uint8_t *)data.value, layout, tex_obj.tex_width, tex_obj.tex_height)) {
fprintf(stderr, "Error loading texture: %s\n", filename);
}
.setBufferImageHeight(staging_texture.tex_height)
.setImageSubresource(subresource)
.setImageOffset({0, 0, 0})
- .setImageExtent({static_cast<uint32_t>(staging_texture.tex_width),
- static_cast<uint32_t>(staging_texture.tex_height), 1});
+ .setImageExtent({staging_texture.tex_width, staging_texture.tex_height, 1});
cmd.copyBufferToImage(staging_texture.buffer, textures[i].image, vk::ImageLayout::eTransferDstOptimal, 1, ©_region);
/* Convert ppm image data from header file into RGBA texture image */
#include "lunarg.ppm.h"
-bool Demo::loadTexture(const char *filename, uint8_t *rgba_data, vk::SubresourceLayout &layout, int32_t &width, int32_t &height) {
+bool Demo::loadTexture(const char *filename, uint8_t *rgba_data, vk::SubresourceLayout &layout, uint32_t &width, uint32_t &height) {
(void)filename;
char *cPtr;
cPtr = (char *)lunarg_ppm;
}
while (strncmp(cPtr++, "\n", 1))
;
- for (int y = 0; y < height; y++) {
+ for (uint32_t y = 0; y < height; y++) {
uint8_t *rowPtr = rgba_data;
- for (int x = 0; x < width; x++) {
+ for (uint32_t x = 0; x < width; x++) {
memcpy(rowPtr, cPtr, 3);
rowPtr[3] = 255; /* Alpha of 1 */
rowPtr += 4;
wl_display_dispatch(display);
} else {
wl_display_dispatch_pending(display);
- // update_data_buffer();
draw();
curFrame++;
if (frameCount != UINT32_MAX && curFrame == frameCount) {
}
static void handle_surface_configure(void *data, xdg_surface *xdg_surface, uint32_t serial) {
- Demo *demo = (Demo *)data;
+ Demo &demo = *static_cast<Demo *>(data);
xdg_surface_ack_configure(xdg_surface, serial);
- if (demo->xdg_surface_has_been_configured) {
- demo->resize();
+ if (demo.xdg_surface_has_been_configured) {
+ demo.resize();
}
- demo->xdg_surface_has_been_configured = true;
+ demo.xdg_surface_has_been_configured = true;
}
static const xdg_surface_listener surface_listener = {handle_surface_configure};
static void handle_toplevel_configure(void *data, xdg_toplevel *xdg_toplevel, int32_t width, int32_t height,
struct wl_array *states) {
- Demo *demo = (Demo *)data;
+ Demo &demo = *static_cast<Demo *>(data);
/* zero values imply the program may choose its own size, so in that case
* stay with the existing value (which on startup is the default) */
if (width > 0) {
- demo->width = width;
+ demo.width = static_cast<uint32_t>(width);
}
if (height > 0) {
- demo->height = height;
+ demo.height = static_cast<uint32_t>(height);
}
// This will be followed by a surface configure
}
static void handle_toplevel_close(void *data, xdg_toplevel *xdg_toplevel) {
- Demo *demo = (Demo *)data;
- demo->quit = true;
+ Demo &demo = *static_cast<Demo *>(data);
+ demo.quit = true;
}
static const xdg_toplevel_listener toplevel_listener = {handle_toplevel_configure, handle_toplevel_close};