From 40fbb328150d853d0d7a83ce447c3f008de38822 Mon Sep 17 00:00:00 2001 From: gramanas Date: Sun, 16 Jun 2024 22:20:07 +0300 Subject: Start integrating pds, fix warnings and add new test build object --- src/cplusplus.cpp | 2 +- src/pds.h | 34 +++++------ src/render.c | 23 +++---- src/shader.frag | 3 +- src/test.c | 8 +++ src/test.h | 6 ++ src/vksetup.h | 178 +++++++++++++++++++++++++----------------------------- 7 files changed, 123 insertions(+), 131 deletions(-) create mode 100644 src/test.c create mode 100644 src/test.h (limited to 'src') diff --git a/src/cplusplus.cpp b/src/cplusplus.cpp index 35876f5..68cb8c3 100644 --- a/src/cplusplus.cpp +++ b/src/cplusplus.cpp @@ -109,7 +109,7 @@ init_imgui(vks_context *vk) VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR; init_info.PipelineRenderingCreateInfo.colorAttachmentCount = 1; init_info.PipelineRenderingCreateInfo.pColorAttachmentFormats = &vk->swapchain.image_format; - init_info.PipelineRenderingCreateInfo.depthAttachmentFormat = findDepthFormat(*vk); + init_info.PipelineRenderingCreateInfo.depthAttachmentFormat = vks_find_depth_format(*vk); ImGui_ImplVulkan_Init(&init_info); // (this gets a bit more complicated, see example app for full reference) diff --git a/src/pds.h b/src/pds.h index e0b9df2..0c10158 100644 --- a/src/pds.h +++ b/src/pds.h @@ -31,7 +31,6 @@ extern "C" uint64_t cap; uint64_t size; uint64_t stride; - void* data; } pstack; /* Exported API */ @@ -47,17 +46,17 @@ extern "C" PDSDEF void* _pstack_push(void* list, const void* value_ptr); #ifndef pstack_push -#define pstack_push(list, val) \ - do { \ - typeof(val) tmp = val; \ - list = _pstack_push(list, &tmp); \ +#define pstack_push(list, val) \ + do { \ + typeof(val) tmp = val; \ + list = _pstack_push(list, &tmp); \ } while (0); #endif - - PDSDEF uint64_t pstack_capacity(void* list); - PDSDEF uint64_t pstack_size(void* list); - PDSDEF uint64_t pstack_stride(void* list); - PDSDEF void pstack_length_set(void* list, uint64_t value); + PDSDEF int pstack_pop(const void* list, void* dest); + PDSDEF uint64_t pstack_capacity(const void* list); + PDSDEF uint64_t pstack_size(const void* list); + PDSDEF uint64_t pstack_stride(const void* list); + PDSDEF void pstack_size_set(const void* list, uint64_t value); PDSDEF void pstack_destroy(void* list); PDSDEF void* pstack_resize(void* list); @@ -103,13 +102,12 @@ _pstack_create(uint64_t length, uint64_t stride) list->cap = length; list->size = 0; list->stride = stride; - list->data = NULL; return (void*)((uint8_t*)list + header_size); } PDSDEF uint64_t -pstack_capacity(void* list) +pstack_capacity(const void* list) { uint64_t header_size = sizeof(pstack); pstack* header = (pstack*)((uint8_t*)list - header_size); @@ -117,7 +115,7 @@ pstack_capacity(void* list) } PDSDEF uint64_t -pstack_size(void* list) +pstack_size(const void* list) { uint64_t header_size = sizeof(pstack); pstack* header = (pstack*)((uint8_t*)list - header_size); @@ -125,7 +123,7 @@ pstack_size(void* list) } PDSDEF uint64_t -pstack_stride(void* list) +pstack_stride(const void* list) { uint64_t header_size = sizeof(pstack); pstack* header = (pstack*)((uint8_t*)list - header_size); @@ -133,7 +131,7 @@ pstack_stride(void* list) } PDSDEF void -pstack_length_set(void* list, uint64_t value) +pstack_size_set(const void* list, uint64_t value) { uint64_t header_size = sizeof(pstack); pstack* header = (pstack*)((uint8_t*)list - header_size); @@ -186,12 +184,12 @@ _pstack_push(void* list, const void* value_ptr) uint64_t addr = (uint64_t)list; addr += (header->size * header->stride); memcpy((void*)addr, value_ptr, header->stride); - pstack_length_set(list, header->size + 1); + pstack_size_set(list, header->size + 1); return list; } PDSDEF int -pstack_pop(void* list, void* dest) +pstack_pop(const void* list, void* dest) { uint64_t size = pstack_size(list); uint64_t stride = pstack_stride(list); @@ -203,7 +201,7 @@ pstack_pop(void* list, void* dest) uint64_t addr = (uint64_t)list; addr += ((size - 1) * stride); memcpy(dest, (void*)addr, stride); - pstack_length_set(list, size - 1); + pstack_size_set(list, size - 1); return 0; } diff --git a/src/render.c b/src/render.c index 9b7541f..f625dba 100644 --- a/src/render.c +++ b/src/render.c @@ -569,7 +569,7 @@ vulkan_create_graphics_pipeline(VkPolygonMode polygon_mode) .sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR, .colorAttachmentCount = 1, .pColorAttachmentFormats = &s.vk.swapchain.image_format, - .depthAttachmentFormat = findDepthFormat(s.vk), + .depthAttachmentFormat = vks_find_depth_format(s.vk), }; VkGraphicsPipelineCreateInfo pipelineInfo = {0}; @@ -1147,7 +1147,7 @@ vulkan_create_texture_image() vks_create_image(s.vk, texWidth, texHeight, s.vk_mip_levels, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT, - VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, VK_SAMPLE_COUNT_1_BIT, &s.texture_image); + VK_SAMPLE_COUNT_1_BIT, &s.texture_image); vks_transition_image_layout_info transition_info = { 0 }; @@ -1416,7 +1416,7 @@ void vulkan_create_compute_stuff() VkDeviceSize bufferSize = sizeof(Particle) * PARTICLE_COUNT; vks_buffer stagingBuffer = {0}; - vks_create_buffer(s.vk, bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &stagingBuffer); + vks_create_buffer(s.vk, bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, &stagingBuffer); void* data; vkMapMemory(s.vk.device, stagingBuffer.memory, 0, bufferSize, 0, &data); @@ -1424,7 +1424,7 @@ void vulkan_create_compute_stuff() vkUnmapMemory(s.vk.device, stagingBuffer.memory); for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) { - vks_create_buffer(s.vk, bufferSize, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &s.frames[i].shader_storage_buffer); + vks_create_buffer(s.vk, bufferSize, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, &s.frames[i].shader_storage_buffer); // Copy data from the staging buffer (host) to the shader storage buffer (GPU) vks_copy_buffer(s.vk, stagingBuffer.handle, s.frames[i].shader_storage_buffer.handle, bufferSize); } @@ -1521,8 +1521,6 @@ close_vulkan() vkDestroyCommandPool(s.vk.device, s.vk.command_pool, NULL); - vks_cleanup_swapchain(s.vk); - imgui_destroy(s.vk); vkDestroySampler(s.vk.device, s.vk_texture_sampler, NULL); @@ -1542,15 +1540,8 @@ close_vulkan() vkDestroyPipeline(s.vk.device, s.graphics_pipeline.handle, NULL); vkDestroyPipelineLayout(s.vk.device, s.graphics_pipeline.layout, NULL); - vmaDestroyAllocator(s.vk.allocator); + vks_destroy_vulkan_context(&s.vk); - vkDestroyDevice(s.vk.device, NULL); - vkDestroySurfaceKHR(s.vk.instance, s.vk.surface, NULL); - /* if (enable_validation_layers) { */ - /* DestroyDebugUtilsMessengerEXT(s.vk.instance, s.vk_debug_messenger, NULL); */ - /* } */ - vkDestroyInstance(s.vk.instance, NULL); - if (s.prev_vert_result) { shaderc_result_release(s.prev_vert_result); } @@ -1688,12 +1679,16 @@ draw_frame() #define PDS_IMPLEMENTATION #include "pds.h" +#include "test.h" + int main(int argc, char* argv[]) { (void) argc; (void)argv; + test_func(); + int* lst = pstack_create(int); pstack_push(lst, 1); pstack_push(lst, 2); diff --git a/src/shader.frag b/src/shader.frag index 8f46ec0..ca895d2 100644 --- a/src/shader.frag +++ b/src/shader.frag @@ -62,7 +62,8 @@ void main() { if (mod(gl_FragCoord.x, 20) > 19 || mod(gl_FragCoord.y, 20) > 19) { outColor = vec4(0,0,0, 1); } else { - outColor = vec4(0.0, n * .2, n * 0.5, 1.0); + float x = clamp(sin(ubo.time * 0.1), 0.0, 1.0); + outColor = vec4(x, n * .2, n * 0.5, 1.0); } } diff --git a/src/test.c b/src/test.c new file mode 100644 index 0000000..9a5aeb9 --- /dev/null +++ b/src/test.c @@ -0,0 +1,8 @@ +#include "test.h" +#include + +void +test_func() +{ + printf("This is a test from %s\n", __FILE__); +} diff --git a/src/test.h b/src/test.h new file mode 100644 index 0000000..4c14fc6 --- /dev/null +++ b/src/test.h @@ -0,0 +1,6 @@ +#ifndef _TEST_H +#define _TEST_H + +void test_func(); + +#endif diff --git a/src/vksetup.h b/src/vksetup.h index 5ba2b0b..8e14637 100644 --- a/src/vksetup.h +++ b/src/vksetup.h @@ -232,14 +232,15 @@ typedef struct vks_transition_image_layout_info /* Exported API */ VKSDEF void vks_create_vulkan_context (vks_context *vk); -VKSDEF void vks_create_buffer (const vks_context vk, const VkDeviceSize size, const VkBufferUsageFlags usage, const VkMemoryPropertyFlags properties, vks_buffer *buffer); -VKSDEF void vks_create_image (const vks_context vk, const uint32_t width, const uint32_t height, const uint32_t mipLevels, const VkFormat format, const VkImageTiling tiling, const VkImageUsageFlags usage, const VkMemoryPropertyFlags properties, const VkSampleCountFlagBits numSamples, vks_image *image); +VKSDEF void vks_destroy_vulkan_context (vks_context *vk); +VKSDEF void vks_create_buffer (const vks_context vk, const VkDeviceSize size, const VkBufferUsageFlags usage, vks_buffer *buffer); +VKSDEF void vks_create_image (const vks_context vk, const uint32_t width, const uint32_t height, const uint32_t mipLevels, const VkFormat format, const VkImageTiling tiling, const VkImageUsageFlags usage, const VkSampleCountFlagBits numSamples, vks_image *image); VKSDEF void vks_transition_image_layout (const vks_context vk, const vks_transition_image_layout_info *info); VKSDEF void vks_copy_buffer (const vks_context vk, const VkBuffer src_buffer, VkBuffer dst_buffer, const VkDeviceSize size); VKSDEF void vks_copy_buffer_to_image (const vks_context vk, const VkBuffer buffer, VkImage image, const uint32_t width, const uint32_t height); VKSDEF void vks_generate_mipmaps (const vks_context vk, VkImage image, const VkFormat imageFormat, const int32_t texWidth, const int32_t texHeight, const uint32_t mipLevels); VKSDEF VkImageView vks_create_image_view (const vks_context vk, VkImage image, VkFormat format, VkImageAspectFlags aspectFlags, uint32_t mipLevels); -VKSDEF VkFormat findDepthFormat(const vks_context vk); +VKSDEF VkFormat vks_find_depth_format (const vks_context vk); /* VKSDEF void vulkan_create_descriptor_set_layout(); */ /* VKSDEF void vulkan_create_graphics_pipeline(); */ /* VKSDEF void vulkan_create_command_pool(); */ @@ -295,26 +296,6 @@ _vks_check_validation_layer_support(const char* const validation_layers[], return layerFound; } -static uint32_t -_find_memory_type(const vks_context vk, - const uint32_t typeFilter, - const VkMemoryPropertyFlags properties) -{ - VkPhysicalDeviceMemoryProperties mem_properties; - vkGetPhysicalDeviceMemoryProperties(vk.physical_device, &mem_properties); - - for (uint32_t i = 0; i < mem_properties.memoryTypeCount; i++) { - if ((typeFilter & (1 << i)) && - (mem_properties.memoryTypes[i].propertyFlags & properties) == - properties) { - return i; - } - } - - vk_log(VK_ERROR, "failed to find suitable memory type!\n"); - return 9999; -} - VKSDEF VkCommandBuffer _vks_begin_single_time_commands(const vks_context vk) { @@ -564,10 +545,8 @@ _vulkan_find_queue_families(vks_context* vk, VkPhysicalDevice device) typedef struct _swap_chain_support_details { VkSurfaceCapabilitiesKHR capabilities; - VkSurfaceFormatKHR formats[100]; - uint32_t formatCount; - VkPresentModeKHR presentModes[100]; - uint32_t presentModeCount; + VkSurfaceFormatKHR *formats; + VkPresentModeKHR *presentModes; } _swap_chain_support_details; static _swap_chain_support_details @@ -580,22 +559,26 @@ _query_swap_chain_support(const vks_context* vk, VkPhysicalDevice device) vkGetPhysicalDeviceSurfaceCapabilitiesKHR( device, vk->surface, &details.capabilities); + uint32_t format_count, present_mode_count; + vkGetPhysicalDeviceSurfaceFormatsKHR( - device, vk->surface, &details.formatCount, NULL); + device, vk->surface, &format_count, NULL); - if (details.formatCount != 0) { - // todo alloc format arrray + if (format_count != 0) { + details.formats = pstack_create_ex(VkSurfaceFormatKHR, format_count); vkGetPhysicalDeviceSurfaceFormatsKHR( - device, vk->surface, &details.formatCount, details.formats); + device, vk->surface, &format_count, details.formats); + pstack_size_set(details.formats, format_count); } - + vkGetPhysicalDeviceSurfacePresentModesKHR( - device, vk->surface, &details.presentModeCount, NULL); + device, vk->surface, &present_mode_count, NULL); - if (details.presentModeCount != 0) { - // todo alloc presentModes array + if (present_mode_count != 0) { + details.presentModes = pstack_create_ex(VkPresentModeKHR, present_mode_count); vkGetPhysicalDeviceSurfacePresentModesKHR( - device, vk->surface, &details.presentModeCount, details.presentModes); + device, vk->surface, &present_mode_count, details.presentModes); + pstack_size_set(details.presentModes, present_mode_count); } return details; @@ -611,8 +594,11 @@ _vulkan_is_device_suitable(vks_context* vk, VkPhysicalDevice device) if (extensionsSupported) { _swap_chain_support_details swapChainSupport = _query_swap_chain_support(vk, device); - swapChainAdequate = !(swapChainSupport.formatCount == 0) && - !(swapChainSupport.presentModeCount == 0); + swapChainAdequate = !(pstack_size(swapChainSupport.formats) == 0) && + !(pstack_size(swapChainSupport.presentModes) == 0); + + pstack_destroy(swapChainSupport.formats); + pstack_destroy(swapChainSupport.presentModes); } VkPhysicalDeviceFeatures supportedFeatures; @@ -749,35 +735,6 @@ _vulkan_create_logical_device(vks_context* vk) vkGetDeviceQueue(vk->device, indices.presentFamily, 0, &vk->present_queue); } -static VkSurfaceFormatKHR -_chooseSwapSurfaceFormat(const VkSurfaceFormatKHR* availableFormats, - uint32_t formatCount) -{ - for (uint32_t i = 0; i < formatCount; i++) { - if (availableFormats[i].format == VK_FORMAT_B8G8R8A8_SRGB && - availableFormats[i].colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) { - return availableFormats[i]; - } - } - - // if it fails pick the first one - return availableFormats[0]; -} - -static VkPresentModeKHR -_chooseSwapPresentMode(const VkPresentModeKHR* presentModes, - uint32_t presentModeCount) -{ - for (uint32_t i = 0; i < presentModeCount; i++) { - if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) { - return presentModes[i]; - } - } - - // if it fails pick the the FIFO one - return VK_PRESENT_MODE_FIFO_KHR; -} - static VkExtent2D _chooseSwapExtent(const vks_context* vk, const VkSurfaceCapabilitiesKHR* capabilities) @@ -817,10 +774,31 @@ _vulkan_create_swap_chain(vks_context* vk) _swap_chain_support_details swapChainSupport = _query_swap_chain_support(vk, vk->physical_device); - VkSurfaceFormatKHR surfaceFormat = _chooseSwapSurfaceFormat( - swapChainSupport.formats, swapChainSupport.formatCount); - VkPresentModeKHR presentMode = _chooseSwapPresentMode( - swapChainSupport.presentModes, swapChainSupport.presentModeCount); + VkSurfaceFormatKHR surfaceFormat; + while (!pstack_pop(swapChainSupport.formats, &surfaceFormat)) { + //for (uint32_t i = 0; i < formatCount; i++) { + if (surfaceFormat.format == VK_FORMAT_B8G8R8A8_SRGB && + surfaceFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) { + break; + } + } + + pstack_destroy(swapChainSupport.formats); + + VkPresentModeKHR presentMode; + while (!pstack_pop(swapChainSupport.presentModes, &presentMode)) { + if (presentMode == VK_PRESENT_MODE_MAILBOX_KHR) { + break; + } + } + + if (pstack_size(swapChainSupport.presentModes) == 0 && + presentMode != VK_PRESENT_MODE_MAILBOX_KHR) { + presentMode = VK_PRESENT_MODE_FIFO_KHR; + } + + pstack_destroy(swapChainSupport.presentModes); + VkExtent2D extent = _chooseSwapExtent(vk, &swapChainSupport.capabilities); uint32_t imageCount = swapChainSupport.capabilities.minImageCount + 1; @@ -935,7 +913,7 @@ _findSupportedFormat(const vks_context vk, } VKSDEF VkFormat -findDepthFormat(const vks_context vk) +vks_find_depth_format(const vks_context vk) { VkFormat formats[] = { VK_FORMAT_D32_SFLOAT, VK_FORMAT_D32_SFLOAT_S8_UINT, @@ -949,11 +927,15 @@ findDepthFormat(const vks_context vk) VKSDEF void _vulkan_create_depth_resources(vks_context *vk) { - VkFormat depth_format = findDepthFormat(*vk); - vks_create_image(*vk, vk->swapchain.extent.width, vk->swapchain.extent.height, 1, - depth_format, VK_IMAGE_TILING_OPTIMAL, + VkFormat depth_format = vks_find_depth_format(*vk); + vks_create_image(*vk, + vk->swapchain.extent.width, + vk->swapchain.extent.height, + 1, + depth_format, + VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, - VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, vk->msaa_samples, + vk->msaa_samples, &vk->depth_image); vk->depth_image.view = vks_create_image_view(*vk, vk->depth_image.handle, depth_format, VK_IMAGE_ASPECT_DEPTH_BIT, 1); @@ -978,11 +960,15 @@ _vulkan_create_color_resources(vks_context *vk) { VkFormat colorFormat = vk->swapchain.image_format; - vks_create_image(*vk, vk->swapchain.extent.width, vk->swapchain.extent.height, 1, - colorFormat, VK_IMAGE_TILING_OPTIMAL, + vks_create_image(*vk, + vk->swapchain.extent.width, + vk->swapchain.extent.height, + 1, + colorFormat, + VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, - VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, vk->msaa_samples, + vk->msaa_samples, &vk->color_image); vk->color_image.view = vks_create_image_view(*vk, vk->color_image.handle, colorFormat, VK_IMAGE_ASPECT_COLOR_BIT, 1); } @@ -1068,6 +1054,21 @@ vks_create_vulkan_context(vks_context* vk) _vulkan_create_depth_resources(vk); } + +VKSDEF void +vks_destroy_vulkan_context(vks_context* vk) +{ + vks_cleanup_swapchain(*vk); + + vmaDestroyAllocator(vk->allocator); + + vkDestroyDevice(vk->device, NULL); + vkDestroySurfaceKHR(vk->instance, vk->surface, NULL); + + vkDestroyInstance(vk->instance, NULL); +} + + VKSDEF VkImageView vks_create_image_view(const vks_context vk, VkImage image, @@ -1247,7 +1248,6 @@ vks_transition_image_layout(const vks_context vk, const vks_transition_image_lay barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; barrier.image = info->image; - // barrier.subresourceRange.aspectMask = if (info->newLayout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) { barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT; @@ -1285,7 +1285,6 @@ vks_create_image(const vks_context vk, const VkFormat format, const VkImageTiling tiling, const VkImageUsageFlags usage, - const VkMemoryPropertyFlags properties, const VkSampleCountFlagBits numSamples, vks_image* image) { @@ -1319,7 +1318,6 @@ VKSDEF void vks_create_buffer(const vks_context vk, const VkDeviceSize size, const VkBufferUsageFlags usage, - const VkMemoryPropertyFlags properties, vks_buffer* buffer) { VkBufferCreateInfo bufferInfo = { 0 }; @@ -1333,21 +1331,7 @@ vks_create_buffer(const vks_context vk, allocInfo.usage = VMA_MEMORY_USAGE_AUTO; allocInfo.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT; - //if (properties & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) - vmaCreateBuffer(vk.allocator, &bufferInfo, &allocInfo, &buffer->handle, &buffer->allocation, NULL); - - /* VkMemoryRequirements mem_requirements; */ - /* vkGetBufferMemoryRequirements(vk.device, buffer->handle, &mem_requirements); */ - - /* VkMemoryAllocateInfo allocInfo = { 0 }; */ - /* allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; */ - /* allocInfo.allocationSize = mem_requirements.size; */ - /* allocInfo.memoryTypeIndex = */ - /* _find_memory_type(vk, mem_requirements.memoryTypeBits, properties); */ - - /* // TODO: group allocations etc... (allocations limited by hardware) */ - /* VK_CHECK(vkAllocateMemory(vk.device, &allocInfo, NULL, &buffer->memory)); */ - /* VK_CHECK(vkBindBufferMemory(vk.device, buffer->handle, buffer->memory, 0)); */ } + #endif /* VKSETUP_IMPLEMENTATION */ -- cgit v1.2.3