summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2024-02-14 01:29:12 +0100
committerMitya Selivanov <automainint@guattari.tech>2024-02-14 01:29:12 +0100
commitc6a68596f1b1b37df6db28583137b6f5868c9028 (patch)
tree2d665ce991326f35a5a7cdd040484481f45f8a06 /main.c
parent3df8bd3091ad64382d5559aa69460a6761d72a1a (diff)
downloadvulkan_compute_minimal_example-dev.zip
formatHEADdev
Diffstat (limited to 'main.c')
-rw-r--r--main.c376
1 files changed, 183 insertions, 193 deletions
diff --git a/main.c b/main.c
index 2c0364d..05e9999 100644
--- a/main.c
+++ b/main.c
@@ -12,20 +12,17 @@ exit 0
#include "compute_module.inl.h"
-typedef signed char i8;
-typedef signed short i16;
-typedef signed int i32;
-typedef signed long long i64;
-
+typedef signed char i8;
+typedef signed short i16;
+typedef signed int i32;
+typedef signed long long i64;
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef unsigned long long u64;
-
-typedef float f32;
-typedef double f64;
-
-typedef signed char c8;
+typedef float f32;
+typedef double f64;
+typedef char c8;
enum {
MAX_DEVICE_COUNT = 16,
@@ -57,25 +54,21 @@ VkPipeline pipeline;
VkCommandPool command_pool;
VkCommandBuffer command_buffer;
-int main(int argc, char **argv) {
+i32 main(i32 argc, c8 **argv) {
// Create instance
//
- {
- VkApplicationInfo info_application = {
- .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
- .apiVersion = VK_API_VERSION_1_1,
- };
-
- VkInstanceCreateInfo info_instance_create = {
- .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
- .pApplicationInfo = &info_application,
- };
-
- if (vkCreateInstance(&info_instance_create, NULL, &instance) !=
- VK_SUCCESS) {
- printf("vkCreateInstance failed.\n");
- return -1;
- }
+ if (vkCreateInstance(
+ &(VkInstanceCreateInfo) {
+ .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
+ .pApplicationInfo =
+ &(VkApplicationInfo) {
+ .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
+ .apiVersion = VK_API_VERSION_1_1,
+ },
+ },
+ NULL, &instance) != VK_SUCCESS) {
+ printf("vkCreateInstance failed.\n");
+ return -1;
}
// Find physical device
@@ -145,25 +138,25 @@ int main(int argc, char **argv) {
// Create logical device
//
{
- VkDeviceQueueCreateInfo info_queue_create = {
- .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
- .queueCount = 1,
- .pQueuePriorities = queue_priorities,
- .queueFamilyIndex = queue_family_index,
- };
-
VkPhysicalDeviceFeatures device_features;
memset(&device_features, 0, sizeof device_features);
- VkDeviceCreateInfo info_device_create = {
- .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
- .pQueueCreateInfos = &info_queue_create,
- .queueCreateInfoCount = 1,
- .pEnabledFeatures = &device_features,
- };
-
- if (vkCreateDevice(physical_device, &info_device_create, NULL,
- &device) != VK_SUCCESS) {
+ if (vkCreateDevice(
+ physical_device,
+ &(VkDeviceCreateInfo) {
+ .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
+ .pQueueCreateInfos =
+ &(VkDeviceQueueCreateInfo) {
+ .sType =
+ VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
+ .queueCount = 1,
+ .pQueuePriorities = queue_priorities,
+ .queueFamilyIndex = queue_family_index,
+ },
+ .queueCreateInfoCount = 1,
+ .pEnabledFeatures = &device_features,
+ },
+ NULL, &device) != VK_SUCCESS) {
printf("vkCreateDevice failed.\n");
return -1;
}
@@ -174,15 +167,15 @@ int main(int argc, char **argv) {
// Create buffer
//
{
- VkBufferCreateInfo info_buffer_create = {
- .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
- .size = BUFFER_SIZE,
- .usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
- .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
- };
-
- if (vkCreateBuffer(device, &info_buffer_create, NULL, &buffer) !=
- VK_SUCCESS) {
+ if (vkCreateBuffer(
+ device,
+ &(VkBufferCreateInfo) {
+ .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
+ .size = BUFFER_SIZE,
+ .usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
+ .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
+ },
+ NULL, &buffer) != VK_SUCCESS) {
printf("vkCreateBuffer failed.\n");
return -1;
}
@@ -193,23 +186,19 @@ int main(int argc, char **argv) {
vkGetBufferMemoryRequirements(device, buffer,
&memory_requirements);
- VkMemoryAllocateInfo info_allocate = {
- .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
- .allocationSize = memory_requirements.size,
- };
-
vkGetPhysicalDeviceMemoryProperties(physical_device,
&memory_properties);
u32 i = 0;
u32 properties = VK_MEMORY_PROPERTY_HOST_COHERENT_BIT |
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
+ u32 memory_type_index = 0;
for (; i < memory_properties.memoryTypeCount; ++i)
if ((memory_requirements.memoryTypeBits & (1 << i)) &&
((memory_properties.memoryTypes[i].propertyFlags &
properties) == properties)) {
- info_allocate.memoryTypeIndex = i;
+ memory_type_index = i;
break;
}
@@ -218,8 +207,14 @@ int main(int argc, char **argv) {
return -1;
}
- if (vkAllocateMemory(device, &info_allocate, NULL,
- &buffer_memory) != VK_SUCCESS) {
+ if (vkAllocateMemory(
+ device,
+ &(VkMemoryAllocateInfo) {
+ .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
+ .allocationSize = memory_requirements.size,
+ .memoryTypeIndex = memory_type_index,
+ },
+ NULL, &buffer_memory) != VK_SUCCESS) {
printf("vkAllocateMemory failed.\n");
return -1;
}
@@ -233,128 +228,124 @@ int main(int argc, char **argv) {
// Create descriptor set layout
//
- {
- VkDescriptorSetLayoutBinding descriptor_set_layout_binding = {
- .binding = 0,
- .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
- .descriptorCount = 1,
- .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,
- };
-
- VkDescriptorSetLayoutCreateInfo
- info_descriptor_set_layout_create = {
- .sType =
- VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
- .bindingCount = 1,
- .pBindings = &descriptor_set_layout_binding,
- };
-
- if (vkCreateDescriptorSetLayout(
- device, &info_descriptor_set_layout_create, NULL,
- &descriptor_set_layout) != VK_SUCCESS) {
- printf("vkCreateDescriptorSetLayout failed.\n");
- return -1;
- }
+ if (vkCreateDescriptorSetLayout(
+ device,
+ &(VkDescriptorSetLayoutCreateInfo) {
+ .sType =
+ VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
+ .bindingCount = 1,
+ .pBindings =
+ &(VkDescriptorSetLayoutBinding) {
+ .binding = 0,
+ .descriptorType =
+ VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
+ .descriptorCount = 1,
+ .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,
+ },
+ },
+ NULL, &descriptor_set_layout) != VK_SUCCESS) {
+ printf("vkCreateDescriptorSetLayout failed.\n");
+ return -1;
}
// Create descriptor set
//
{
- VkDescriptorPoolSize descriptor_pool_size = {
- .type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
- .descriptorCount = 1,
- };
-
- VkDescriptorPoolCreateInfo info_descriptor_pool_create = {
- .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
- .maxSets = 1,
- .poolSizeCount = 1,
- .pPoolSizes = &descriptor_pool_size,
- };
-
- if (vkCreateDescriptorPool(device, &info_descriptor_pool_create,
- NULL,
- &descriptor_pool) != VK_SUCCESS) {
+ if (vkCreateDescriptorPool(
+ device,
+ &(VkDescriptorPoolCreateInfo) {
+ .sType =
+ VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
+ .maxSets = 1,
+ .poolSizeCount = 1,
+ .pPoolSizes =
+ &(VkDescriptorPoolSize) {
+ .type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
+ .descriptorCount = 1,
+ },
+ },
+ NULL, &descriptor_pool) != VK_SUCCESS) {
printf("vkCreateDescriptorPool failed.\n");
return -1;
}
- VkDescriptorSetAllocateInfo info_descriptor_set_allocate = {
- .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
- .descriptorPool = descriptor_pool,
- .descriptorSetCount = 1,
- .pSetLayouts = &descriptor_set_layout,
- };
-
- if (vkAllocateDescriptorSets(device,
- &info_descriptor_set_allocate,
- &descriptor_set) != VK_SUCCESS) {
+ if (vkAllocateDescriptorSets(
+ device,
+ &(VkDescriptorSetAllocateInfo) {
+ .sType =
+ VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
+ .descriptorPool = descriptor_pool,
+ .descriptorSetCount = 1,
+ .pSetLayouts = &descriptor_set_layout,
+ },
+ &descriptor_set) != VK_SUCCESS) {
printf("vkAllocateDescriptorSets failed.\n");
return -1;
}
- VkDescriptorBufferInfo info_descriptor_buffer = {
- .buffer = buffer,
- .offset = 0,
- .range = BUFFER_SIZE,
- };
-
- VkWriteDescriptorSet write_descriptor_set = {
- .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
- .dstSet = descriptor_set,
- .dstBinding = 0,
- .descriptorCount = 1,
- .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
- .pBufferInfo = &info_descriptor_buffer,
- };
-
- vkUpdateDescriptorSets(device, 1, &write_descriptor_set, 0, NULL);
+ vkUpdateDescriptorSets(
+ device, 1,
+ &(VkWriteDescriptorSet) {
+ .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
+ .dstSet = descriptor_set,
+ .dstBinding = 0,
+ .descriptorCount = 1,
+ .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
+ .pBufferInfo =
+ &(VkDescriptorBufferInfo) {
+ .buffer = buffer,
+ .offset = 0,
+ .range = BUFFER_SIZE,
+ },
+ },
+ 0, NULL);
}
// Create compute pipeline
//
{
- VkShaderModuleCreateInfo info_module_create = {
- .sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
- .pCode = COMPUTE_MODULE_CODE,
- .codeSize = sizeof COMPUTE_MODULE_CODE,
- };
-
- if (vkCreateShaderModule(device, &info_module_create, NULL,
- &compute_module) != VK_SUCCESS) {
+ if (vkCreateShaderModule(
+ device,
+ &(VkShaderModuleCreateInfo) {
+ .sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
+ .pCode = COMPUTE_MODULE_CODE,
+ .codeSize = sizeof COMPUTE_MODULE_CODE,
+ },
+ NULL, &compute_module) != VK_SUCCESS) {
printf("vkCreateShaderModule failed.\n");
return -1;
}
- VkPipelineShaderStageCreateInfo info_shader_stage_create = {
- .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
- .stage = VK_SHADER_STAGE_COMPUTE_BIT,
- .module = compute_module,
- .pName = "main",
- };
-
- VkPipelineLayoutCreateInfo info_pipeline_layout_create = {
- .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
- .setLayoutCount = 1,
- .pSetLayouts = &descriptor_set_layout,
- };
-
- if (vkCreatePipelineLayout(device, &info_pipeline_layout_create,
- NULL,
- &pipeline_layout) != VK_SUCCESS) {
+ if (vkCreatePipelineLayout(
+ device,
+ &(VkPipelineLayoutCreateInfo) {
+ .sType =
+ VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
+ .setLayoutCount = 1,
+ .pSetLayouts = &descriptor_set_layout,
+ },
+ NULL, &pipeline_layout) != VK_SUCCESS) {
printf("vkCreatePipelineLayout failed.\n");
return -1;
}
- VkComputePipelineCreateInfo info_pipeline_create = {
- .sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,
- .stage = info_shader_stage_create,
- .layout = pipeline_layout,
- };
-
- if (vkCreateComputePipelines(device, VK_NULL_HANDLE, 1,
- &info_pipeline_create, NULL,
- &pipeline)) {
+ if (vkCreateComputePipelines(
+ device,
+ VK_NULL_HANDLE,
+ 1,
+ &(VkComputePipelineCreateInfo) {
+ .sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,
+ .stage = {
+ .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
+ .stage = VK_SHADER_STAGE_COMPUTE_BIT,
+ .module = compute_module,
+ .pName = "main",
+ },
+ .layout = pipeline_layout,
+ },
+ NULL,
+ &pipeline
+ ) != VK_SUCCESS) {
printf("vkCreateComputePipelines failed.\n");
return 0;
}
@@ -363,38 +354,37 @@ int main(int argc, char **argv) {
// Create command buffer
//
{
- VkCommandPoolCreateInfo info_command_pool_create = {
- .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
- .queueFamilyIndex = queue_family_index,
- };
-
- if (vkCreateCommandPool(device, &info_command_pool_create, NULL,
- &command_pool) != VK_SUCCESS) {
+ if (vkCreateCommandPool(
+ device,
+ &(VkCommandPoolCreateInfo) {
+ .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
+ .queueFamilyIndex = queue_family_index,
+ },
+ NULL, &command_pool) != VK_SUCCESS) {
printf("vkCreateCommandPool failed.\n");
return -1;
}
- VkCommandBufferAllocateInfo info_command_buffer_allocate = {
- .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
- .commandPool = command_pool,
- .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
- .commandBufferCount = 1,
- };
-
- if (vkAllocateCommandBuffers(device,
- &info_command_buffer_allocate,
- &command_buffer) != VK_SUCCESS) {
+ if (vkAllocateCommandBuffers(
+ device,
+ &(VkCommandBufferAllocateInfo) {
+ .sType =
+ VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
+ .commandPool = command_pool,
+ .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
+ .commandBufferCount = 1,
+ },
+ &command_buffer) != VK_SUCCESS) {
printf("vkAllocateCommandBuffers failed.\n");
return -1;
}
- VkCommandBufferBeginInfo info_begin = {
- .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
- .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,
- };
-
- if (vkBeginCommandBuffer(command_buffer, &info_begin) !=
- VK_SUCCESS) {
+ if (vkBeginCommandBuffer(
+ command_buffer,
+ &(VkCommandBufferBeginInfo) {
+ .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
+ .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,
+ }) != VK_SUCCESS) {
printf("vkBeginCommandBuffer failed.\n");
return -1;
}
@@ -418,25 +408,25 @@ int main(int argc, char **argv) {
// Run command buffer
//
{
- VkSubmitInfo info_submit = {
- .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
- .commandBufferCount = 1,
- .pCommandBuffers = &command_buffer,
- };
-
- VkFenceCreateInfo info_fence_create = {
- .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
- };
-
VkFence fence;
- if (vkCreateFence(device, &info_fence_create, NULL, &fence) !=
- VK_SUCCESS) {
+ if (vkCreateFence(
+ device,
+ &(VkFenceCreateInfo) {
+ .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
+ },
+ NULL, &fence) != VK_SUCCESS) {
printf("vkCreateFence failed.\n");
return -1;
}
- if (vkQueueSubmit(queue, 1, &info_submit, fence) != VK_SUCCESS) {
+ if (vkQueueSubmit(queue, 1,
+ &(VkSubmitInfo) {
+ .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
+ .commandBufferCount = 1,
+ .pCommandBuffers = &command_buffer,
+ },
+ fence) != VK_SUCCESS) {
printf("vkQueueSubmit failed.\n");
return -1;
}