Compare commits
2 Commits
a425166018
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 2cdfcde5a3 | |||
| ef12ba03d8 |
@@ -170,8 +170,8 @@ int main() {
|
|||||||
check(volkInitialize());
|
check(volkInitialize());
|
||||||
|
|
||||||
display_scale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay());
|
display_scale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay());
|
||||||
window = SDL_CreateWindow("How To Vulkan", (i32)(display_scale * 1920),
|
window = SDL_CreateWindow("How To Vulkan", (i32)(display_scale * 1920), (i32)(display_scale * 1080),
|
||||||
(i32)(display_scale * 1080), SDL_WINDOW_VULKAN);
|
SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE);
|
||||||
check(window != nullptr, EXIT_CODE_WINDOW_CREATION_FAILED);
|
check(window != nullptr, EXIT_CODE_WINDOW_CREATION_FAILED);
|
||||||
// }}}
|
// }}}
|
||||||
|
|
||||||
@@ -374,7 +374,8 @@ int main() {
|
|||||||
|
|
||||||
VmaAllocatorCreateInfo allocator_create_info = {};
|
VmaAllocatorCreateInfo allocator_create_info = {};
|
||||||
allocator_create_info.flags = VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT |
|
allocator_create_info.flags = VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT |
|
||||||
VMA_ALLOCATOR_CREATE_EXT_MEMORY_PRIORITY_BIT;
|
VMA_ALLOCATOR_CREATE_EXT_MEMORY_PRIORITY_BIT |
|
||||||
|
VMA_ALLOCATOR_CREATE_KHR_MAINTENANCE5_BIT;
|
||||||
allocator_create_info.instance = instance;
|
allocator_create_info.instance = instance;
|
||||||
allocator_create_info.physicalDevice = physical_device;
|
allocator_create_info.physicalDevice = physical_device;
|
||||||
allocator_create_info.device = device;
|
allocator_create_info.device = device;
|
||||||
@@ -529,9 +530,9 @@ int main() {
|
|||||||
1.0 - attrib.texcoords[index.texcoord_index * 2 + 1]
|
1.0 - attrib.texcoords[index.texcoord_index * 2 + 1]
|
||||||
};
|
};
|
||||||
|
|
||||||
u16 idx = (u16)wapp_array_count(indices);
|
u16 idx = (u16)wapp_array_count(indices);
|
||||||
wapp_array_append_alloc(Vertex, &arena, vertices, &v, ARRAY_INIT_NONE);
|
vertices = wapp_array_append_alloc(Vertex, &arena, vertices, &v, ARRAY_INIT_NONE);
|
||||||
wapp_array_append_alloc(u16, &arena, indices, &idx, ARRAY_INIT_NONE);
|
indices = wapp_array_append_alloc(u16, &arena, indices, &idx, ARRAY_INIT_NONE);
|
||||||
}
|
}
|
||||||
// }}}
|
// }}}
|
||||||
|
|
||||||
@@ -565,20 +566,20 @@ int main() {
|
|||||||
shader_data_bufs = wapp_array_with_capacity(ShaderDataBuffer, max_frames_in_flight, ARRAY_INIT_FILLED);
|
shader_data_bufs = wapp_array_with_capacity(ShaderDataBuffer, max_frames_in_flight, ARRAY_INIT_FILLED);
|
||||||
for (u32 i = 0; i < max_frames_in_flight; ++i) {
|
for (u32 i = 0; i < max_frames_in_flight; ++i) {
|
||||||
// {{{ Create Buffer
|
// {{{ Create Buffer
|
||||||
VkBufferUsageFlags2CreateInfo data_buf_usage_flags = {};
|
VkBufferUsageFlags2CreateInfo buf_usage_flags = {};
|
||||||
data_buf_usage_flags.sType = VK_STRUCTURE_TYPE_BUFFER_USAGE_FLAGS_2_CREATE_INFO;
|
buf_usage_flags.sType = VK_STRUCTURE_TYPE_BUFFER_USAGE_FLAGS_2_CREATE_INFO;
|
||||||
data_buf_usage_flags.usage = VK_BUFFER_USAGE_2_SHADER_DEVICE_ADDRESS_BIT;
|
buf_usage_flags.usage = VK_BUFFER_USAGE_2_SHADER_DEVICE_ADDRESS_BIT;
|
||||||
|
|
||||||
VkBufferCreateInfo data_buf_create_info = {};
|
VkBufferCreateInfo data_buf_create_info = {};
|
||||||
data_buf_create_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
data_buf_create_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
||||||
data_buf_create_info.size = sizeof(ShaderData);
|
data_buf_create_info.size = sizeof(ShaderData);
|
||||||
data_buf_create_info.usage = VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT;
|
data_buf_create_info.pNext = &buf_usage_flags;
|
||||||
data_buf_create_info.pNext = &data_buf_usage_flags;
|
|
||||||
|
|
||||||
VmaAllocationCreateInfo data_buf_alloc_create_info = {};
|
VmaAllocationCreateInfo data_buf_alloc_create_info = {};
|
||||||
data_buf_alloc_create_info.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT |
|
data_buf_alloc_create_info.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT |
|
||||||
VMA_ALLOCATION_CREATE_HOST_ACCESS_ALLOW_TRANSFER_INSTEAD_BIT |
|
VMA_ALLOCATION_CREATE_HOST_ACCESS_ALLOW_TRANSFER_INSTEAD_BIT |
|
||||||
VMA_ALLOCATION_CREATE_MAPPED_BIT;
|
VMA_ALLOCATION_CREATE_MAPPED_BIT;
|
||||||
data_buf_create_info.usage = VMA_MEMORY_USAGE_AUTO;
|
data_buf_alloc_create_info.usage = VMA_MEMORY_USAGE_AUTO;
|
||||||
|
|
||||||
check(vmaCreateBuffer(allocator, &data_buf_create_info, &data_buf_alloc_create_info,
|
check(vmaCreateBuffer(allocator, &data_buf_create_info, &data_buf_alloc_create_info,
|
||||||
&shader_data_bufs[i].buffer, &shader_data_bufs[i].allocation,
|
&shader_data_bufs[i].buffer, &shader_data_bufs[i].allocation,
|
||||||
@@ -1124,7 +1125,7 @@ int main() {
|
|||||||
* - Poll events
|
* - Poll events
|
||||||
*/
|
*/
|
||||||
|
|
||||||
object_rotations = wapp_array_with_capacity(glm::vec3, 3, ARRAY_INIT_FILLED);
|
object_rotations = wapp_array_with_capacity(glm::vec3, instance_count, ARRAY_INIT_FILLED);
|
||||||
u64 last_time = SDL_GetTicks();
|
u64 last_time = SDL_GetTicks();
|
||||||
SDL_Event event = {};
|
SDL_Event event = {};
|
||||||
|
|
||||||
@@ -1144,7 +1145,7 @@ int main() {
|
|||||||
shader_data.projection = glm::perspective(glm::radians(45.0f), (f32)window_size.x / (f32)window_size.y,
|
shader_data.projection = glm::perspective(glm::radians(45.0f), (f32)window_size.x / (f32)window_size.y,
|
||||||
0.1f, 32.0f);
|
0.1f, 32.0f);
|
||||||
shader_data.view = glm::translate(glm::mat4(1.0f), camera_position);
|
shader_data.view = glm::translate(glm::mat4(1.0f), camera_position);
|
||||||
for (u32 i = 0; i < instance_count; ++i) {
|
for (i32 i = 0; i < instance_count; ++i) {
|
||||||
glm::vec3 instance_pos = glm::vec3((f32)(i - 1) * 3.0f, 0.0f, 0.0f);
|
glm::vec3 instance_pos = glm::vec3((f32)(i - 1) * 3.0f, 0.0f, 0.0f);
|
||||||
shader_data.model[i] = glm::translate(glm::mat4(1.0f), instance_pos) *
|
shader_data.model[i] = glm::translate(glm::mat4(1.0f), instance_pos) *
|
||||||
glm::mat4_cast(glm::quat(object_rotations[i]));
|
glm::mat4_cast(glm::quat(object_rotations[i]));
|
||||||
@@ -1179,17 +1180,17 @@ int main() {
|
|||||||
memory_barriers[0].subresourceRange.layerCount = 1;
|
memory_barriers[0].subresourceRange.layerCount = 1;
|
||||||
|
|
||||||
// Depth Attachment
|
// Depth Attachment
|
||||||
memory_barriers[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2;
|
memory_barriers[1].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2;
|
||||||
memory_barriers[0].srcStageMask = VK_PIPELINE_STAGE_2_LATE_FRAGMENT_TESTS_BIT;
|
memory_barriers[1].srcStageMask = VK_PIPELINE_STAGE_2_LATE_FRAGMENT_TESTS_BIT;
|
||||||
memory_barriers[0].srcAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
|
memory_barriers[1].srcAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
|
||||||
memory_barriers[0].dstStageMask = VK_PIPELINE_STAGE_2_EARLY_FRAGMENT_TESTS_BIT;
|
memory_barriers[1].dstStageMask = VK_PIPELINE_STAGE_2_EARLY_FRAGMENT_TESTS_BIT;
|
||||||
memory_barriers[0].dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
|
memory_barriers[1].dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
|
||||||
memory_barriers[0].oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
memory_barriers[1].oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
||||||
memory_barriers[0].newLayout = VK_IMAGE_LAYOUT_ATTACHMENT_OPTIMAL;
|
memory_barriers[1].newLayout = VK_IMAGE_LAYOUT_ATTACHMENT_OPTIMAL;
|
||||||
memory_barriers[0].image = depth_image;
|
memory_barriers[1].image = depth_image;
|
||||||
memory_barriers[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
|
memory_barriers[1].subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
|
||||||
memory_barriers[0].subresourceRange.levelCount = 1;
|
memory_barriers[1].subresourceRange.levelCount = 1;
|
||||||
memory_barriers[0].subresourceRange.layerCount = 1;
|
memory_barriers[1].subresourceRange.layerCount = 1;
|
||||||
|
|
||||||
// {{{ Transition Layout Color And Depth Images
|
// {{{ Transition Layout Color And Depth Images
|
||||||
VkDependencyInfo barrier_dependency_info = {};
|
VkDependencyInfo barrier_dependency_info = {};
|
||||||
@@ -1202,7 +1203,7 @@ int main() {
|
|||||||
|
|
||||||
// {{{ Dynamic Rendering Commands
|
// {{{ Dynamic Rendering Commands
|
||||||
VkClearValue color_clear_value = {};
|
VkClearValue color_clear_value = {};
|
||||||
color_clear_value.color = { 0.0f, 0.0f, 0.2f, 1.0f };
|
color_clear_value.color = { 0.0f, 0.0f, 0.0f, 0.0f };
|
||||||
VkClearValue depth_clear_value = {};
|
VkClearValue depth_clear_value = {};
|
||||||
depth_clear_value.depthStencil = { 1.0f, 0 };
|
depth_clear_value.depthStencil = { 1.0f, 0 };
|
||||||
|
|
||||||
@@ -1347,7 +1348,7 @@ int main() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// Select active model instance
|
// Select active model instance
|
||||||
if (event.key.key == SDLK_PLUS || event.key.key == SDLK_KP_PLUS) {
|
if (event.key.key == SDLK_PLUS || event.key.key == SDLK_KP_PLUS || event.key.key == SDLK_EQUALS) {
|
||||||
shader_data.selected = (shader_data.selected < 2) ? shader_data.selected + 1 : 0;
|
shader_data.selected = (shader_data.selected < 2) ? shader_data.selected + 1 : 0;
|
||||||
}
|
}
|
||||||
if (event.key.key == SDLK_MINUS || event.key.key == SDLK_KP_MINUS) {
|
if (event.key.key == SDLK_MINUS || event.key.key == SDLK_KP_MINUS) {
|
||||||
@@ -1372,10 +1373,92 @@ int main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// }}}
|
// }}}
|
||||||
|
|
||||||
|
// {{{ Recreate Swapchain If Needed
|
||||||
|
if (update_swapchain) {
|
||||||
|
// TODO (Abdelrahman): This doesn't work well. Debug it.
|
||||||
|
update_swapchain = false;
|
||||||
|
check(vkDeviceWaitIdle(device));
|
||||||
|
|
||||||
|
check(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physical_device, surface, &surface_caps));
|
||||||
|
|
||||||
|
// Create new swapchain
|
||||||
|
swapchain_create_info.oldSwapchain = swapchain;
|
||||||
|
swapchain_create_info.imageExtent = { (u32)window_size.x, (u32)window_size.y };
|
||||||
|
check(vkCreateSwapchainKHR(device, &swapchain_create_info, NULL, &swapchain));
|
||||||
|
|
||||||
|
// Clean old image views
|
||||||
|
for (u32 i = 0; i < swapchain_image_count; ++i) {
|
||||||
|
vkDestroyImageView(device, swapchain_views[i], NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean old render_completed semaphores
|
||||||
|
for (u32 i = 0; i < swapchain_image_count; ++i) {
|
||||||
|
vkDestroySemaphore(device, render_completed_semaphores[i], NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get new swapchain images
|
||||||
|
check(vkGetSwapchainImagesKHR(device, swapchain, &swapchain_image_count, nullptr));
|
||||||
|
if (swapchain_image_count != wapp_array_count(swapchain_images)) {
|
||||||
|
swapchain_images = wapp_array_alloc_capacity(VkImage, &arena, swapchain_image_count, ARRAY_INIT_FILLED);
|
||||||
|
swapchain_views = wapp_array_alloc_capacity(VkImageView, &arena, swapchain_image_count, ARRAY_INIT_FILLED);
|
||||||
|
render_completed_semaphores = wapp_array_alloc_capacity(VkSemaphore, &arena, swapchain_image_count, ARRAY_INIT_FILLED);
|
||||||
|
}
|
||||||
|
check(swapchain_images != nullptr, EXIT_CODE_ALLOCATION_FAILURE);
|
||||||
|
check(vkGetSwapchainImagesKHR(device, swapchain, &swapchain_image_count, swapchain_images));
|
||||||
|
|
||||||
|
check(swapchain_views != nullptr, EXIT_CODE_ALLOCATION_FAILURE);
|
||||||
|
|
||||||
|
for (u32 i = 0; i < swapchain_image_count; ++i) {
|
||||||
|
VkImageViewCreateInfo view_create_info = {};
|
||||||
|
view_create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
|
||||||
|
view_create_info.image = swapchain_images[i];
|
||||||
|
view_create_info.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
||||||
|
view_create_info.format = image_format;
|
||||||
|
view_create_info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||||
|
view_create_info.subresourceRange.levelCount = 1;
|
||||||
|
view_create_info.subresourceRange.layerCount = 1;
|
||||||
|
|
||||||
|
check(vkCreateImageView(device, &view_create_info, nullptr, &swapchain_views[i]));
|
||||||
|
}
|
||||||
|
|
||||||
|
check(render_completed_semaphores != nullptr, EXIT_CODE_SYNC_OBJ_CREATE_FAILED);
|
||||||
|
|
||||||
|
for (u32 i = 0; i < swapchain_image_count; ++i) {
|
||||||
|
check(vkCreateSemaphore(device, &semaphore_create_info, NULL, &render_completed_semaphores[i]));
|
||||||
|
}
|
||||||
|
|
||||||
|
vkDestroySwapchainKHR(device, swapchain_create_info.oldSwapchain, NULL);
|
||||||
|
vmaDestroyImage(allocator, depth_image, depth_allocation);
|
||||||
|
vkDestroyImageView(device, depth_view, NULL);
|
||||||
|
|
||||||
|
depth_image_create_info.extent = { (u32)window_size.x, (u32)window_size.y, 1 };
|
||||||
|
|
||||||
|
VmaAllocationCreateInfo alloc_create_info = {};
|
||||||
|
alloc_create_info.flags = VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT;
|
||||||
|
alloc_create_info.usage = VMA_MEMORY_USAGE_AUTO;
|
||||||
|
|
||||||
|
check(vmaCreateImage(allocator, &depth_image_create_info, &alloc_create_info, &depth_image,
|
||||||
|
&depth_allocation, nullptr));
|
||||||
|
|
||||||
|
VkImageViewCreateInfo view_create_info = {};
|
||||||
|
view_create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
|
||||||
|
view_create_info.image = depth_image;
|
||||||
|
view_create_info.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
||||||
|
view_create_info.format = depth_format;
|
||||||
|
view_create_info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
|
||||||
|
view_create_info.subresourceRange.levelCount = 1;
|
||||||
|
view_create_info.subresourceRange.layerCount = 1;
|
||||||
|
|
||||||
|
check(vkCreateImageView(device, &view_create_info, NULL, &depth_view));
|
||||||
|
}
|
||||||
|
// }}}
|
||||||
}
|
}
|
||||||
// }}}
|
// }}}
|
||||||
|
|
||||||
// {{{ Cleanup
|
// {{{ Cleanup
|
||||||
|
check(vkDeviceWaitIdle(device));
|
||||||
|
|
||||||
vkDestroyPipeline(device, graphics_pipeline, NULL);
|
vkDestroyPipeline(device, graphics_pipeline, NULL);
|
||||||
vkDestroyPipelineLayout(device, graphics_pipeline_layout, NULL);
|
vkDestroyPipelineLayout(device, graphics_pipeline_layout, NULL);
|
||||||
vkDestroyShaderModule(device, shader_module, NULL);
|
vkDestroyShaderModule(device, shader_module, NULL);
|
||||||
|
|||||||
Reference in New Issue
Block a user