further vulkan
This commit is contained in:
		
							
								
								
									
										185
									
								
								initVulkan.c
									
									
									
									
									
								
							
							
						
						
									
										185
									
								
								initVulkan.c
									
									
									
									
									
								
							@@ -5,7 +5,7 @@ int initVulkan(VkInstance *vkInstance, VkDevice *device, VkSurfaceKHR *surface,
 | 
			
		||||
{
 | 
			
		||||
    // VkApplicationInfo
 | 
			
		||||
    VkApplicationInfo appInfo;
 | 
			
		||||
    initAppInfo(&appInfo);
 | 
			
		||||
    createAppInfo(&appInfo);
 | 
			
		||||
 | 
			
		||||
    // VkInstanceCreateInfo
 | 
			
		||||
    uint32_t  amountOfLayers;
 | 
			
		||||
@@ -14,7 +14,7 @@ int initVulkan(VkInstance *vkInstance, VkDevice *device, VkSurfaceKHR *surface,
 | 
			
		||||
    vkEnumerateInstanceLayerProperties(&amountOfLayers, layers);
 | 
			
		||||
 | 
			
		||||
    VkInstanceCreateInfo instanceInfo;
 | 
			
		||||
    initCreateInfo(&appInfo, &instanceInfo);
 | 
			
		||||
    createInstanceInfo(&appInfo, &instanceInfo);
 | 
			
		||||
 | 
			
		||||
    // Vulkan Instance
 | 
			
		||||
    ASSERT_VK_SUCCESS(vkCreateInstance(&instanceInfo, NULL, vkInstance))
 | 
			
		||||
@@ -32,12 +32,12 @@ int initVulkan(VkInstance *vkInstance, VkDevice *device, VkSurfaceKHR *surface,
 | 
			
		||||
 | 
			
		||||
    // Queue info
 | 
			
		||||
    VkDeviceQueueCreateInfo queueInfo;
 | 
			
		||||
    initQueueInfo(&queueInfo);
 | 
			
		||||
    createQueueInfo(&queueInfo);
 | 
			
		||||
 | 
			
		||||
    // Device info
 | 
			
		||||
    VkPhysicalDeviceFeatures usedFeatures = {};
 | 
			
		||||
    VkDeviceCreateInfo deviceInfo;
 | 
			
		||||
    initDeviceInfo(&queueInfo, &deviceInfo, &usedFeatures);
 | 
			
		||||
    createDeviceInfo(&queueInfo, &deviceInfo, &usedFeatures);
 | 
			
		||||
 | 
			
		||||
    // Logical device
 | 
			
		||||
    ASSERT_VK_SUCCESS(vkCreateDevice(physicalDevices[0], &deviceInfo, NULL, device))
 | 
			
		||||
@@ -57,7 +57,7 @@ int initVulkan(VkInstance *vkInstance, VkDevice *device, VkSurfaceKHR *surface,
 | 
			
		||||
 | 
			
		||||
    // Swap chain info
 | 
			
		||||
    VkSwapchainCreateInfoKHR swapChainCreateInfo;
 | 
			
		||||
    initSwapChainInfo(&swapChainCreateInfo, surface);
 | 
			
		||||
    createSwapChainInfo(&swapChainCreateInfo, surface);
 | 
			
		||||
 | 
			
		||||
    // Swap chain
 | 
			
		||||
    ASSERT_VK_SUCCESS(vkCreateSwapchainKHR(*device, &swapChainCreateInfo, NULL, swapChain))
 | 
			
		||||
@@ -72,16 +72,14 @@ int initVulkan(VkInstance *vkInstance, VkDevice *device, VkSurfaceKHR *surface,
 | 
			
		||||
    VkImageViewCreateInfo imageViewInfo;
 | 
			
		||||
    for (int i = 0; i < *amountImages; i++)
 | 
			
		||||
    {
 | 
			
		||||
        initImageViewInfo(&imageViewInfo, swapChainImages, i);
 | 
			
		||||
        createImageViewInfo(&imageViewInfo, swapChainImages, i);
 | 
			
		||||
        ASSERT_VK_SUCCESS(vkCreateImageView(*device, &imageViewInfo, NULL, &*imageViews[i]))
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    return SUCCESS;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void initAppInfo(VkApplicationInfo *appInfo)
 | 
			
		||||
void createAppInfo(VkApplicationInfo *appInfo)
 | 
			
		||||
{
 | 
			
		||||
    appInfo->sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
 | 
			
		||||
    appInfo->pNext = NULL;
 | 
			
		||||
@@ -92,7 +90,7 @@ void initAppInfo(VkApplicationInfo *appInfo)
 | 
			
		||||
    appInfo->apiVersion = VK_API_VERSION_1_1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void initCreateInfo(VkApplicationInfo *appInfo, VkInstanceCreateInfo *instanceInfo)
 | 
			
		||||
void createInstanceInfo(VkApplicationInfo *appInfo, VkInstanceCreateInfo *instanceInfo)
 | 
			
		||||
{
 | 
			
		||||
    GLuint amountOfGLFWExtensions;
 | 
			
		||||
    const char **glfwExtensions = glfwGetRequiredInstanceExtensions(&amountOfGLFWExtensions);
 | 
			
		||||
@@ -107,7 +105,7 @@ void initCreateInfo(VkApplicationInfo *appInfo, VkInstanceCreateInfo *instanceIn
 | 
			
		||||
    instanceInfo->ppEnabledExtensionNames = glfwExtensions;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void initQueueInfo(VkDeviceQueueCreateInfo *queueInfo)
 | 
			
		||||
void createQueueInfo(VkDeviceQueueCreateInfo *queueInfo)
 | 
			
		||||
{
 | 
			
		||||
    queueInfo->sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
 | 
			
		||||
    queueInfo->pNext = NULL;
 | 
			
		||||
@@ -117,7 +115,7 @@ void initQueueInfo(VkDeviceQueueCreateInfo *queueInfo)
 | 
			
		||||
    queueInfo->pQueuePriorities = NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void initDeviceInfo(VkDeviceQueueCreateInfo *queueInfo, VkDeviceCreateInfo *deviceInfo, VkPhysicalDeviceFeatures *features)
 | 
			
		||||
void createDeviceInfo(VkDeviceQueueCreateInfo *queueInfo, VkDeviceCreateInfo *deviceInfo, VkPhysicalDeviceFeatures *features)
 | 
			
		||||
{
 | 
			
		||||
    const char *deviceExtensions[1] = { VK_KHR_SWAPCHAIN_EXTENSION_NAME };
 | 
			
		||||
 | 
			
		||||
@@ -133,7 +131,7 @@ void initDeviceInfo(VkDeviceQueueCreateInfo *queueInfo, VkDeviceCreateInfo *devi
 | 
			
		||||
    deviceInfo->pEnabledFeatures = features;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void initSwapChainInfo(VkSwapchainCreateInfoKHR *swapChainCreateInfo, VkSurfaceKHR *surface)
 | 
			
		||||
void createSwapChainInfo(VkSwapchainCreateInfoKHR *swapChainCreateInfo, VkSurfaceKHR *surface)
 | 
			
		||||
{
 | 
			
		||||
    VkExtent2D imageExtent = { WIDTH, HEIGHT };
 | 
			
		||||
 | 
			
		||||
@@ -157,7 +155,7 @@ void initSwapChainInfo(VkSwapchainCreateInfoKHR *swapChainCreateInfo, VkSurfaceK
 | 
			
		||||
    swapChainCreateInfo->oldSwapchain = VK_NULL_HANDLE;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void initImageViewInfo(VkImageViewCreateInfo *imageViewInfo, VkImage *swapChainImages, int index)
 | 
			
		||||
void createImageViewInfo(VkImageViewCreateInfo *imageViewInfo, VkImage *swapChainImages, int index)
 | 
			
		||||
{
 | 
			
		||||
    VkComponentMapping componentMapping = {
 | 
			
		||||
            VK_COMPONENT_SWIZZLE_IDENTITY,
 | 
			
		||||
@@ -179,12 +177,33 @@ void initImageViewInfo(VkImageViewCreateInfo *imageViewInfo, VkImage *swapChainI
 | 
			
		||||
    imageViewInfo->subresourceRange = subresourceRange;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void createAttachmentDescription(VkAttachmentDescription *attachmentDescription)
 | 
			
		||||
{
 | 
			
		||||
    attachmentDescription->flags = 0;
 | 
			
		||||
    attachmentDescription->format = VK_FORMAT_B8G8R8A8_UNORM;
 | 
			
		||||
    attachmentDescription->samples = VK_SAMPLE_COUNT_1_BIT;
 | 
			
		||||
    attachmentDescription->loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
 | 
			
		||||
    attachmentDescription->storeOp = VK_ATTACHMENT_STORE_OP_STORE;
 | 
			
		||||
    attachmentDescription->stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
 | 
			
		||||
    attachmentDescription->stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
 | 
			
		||||
    attachmentDescription->initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
 | 
			
		||||
    attachmentDescription->finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void shutdownVulkan(VkInstance *vkInstance, VkDevice *device, VkSurfaceKHR *surface, VkSwapchainKHR *swapChain,
 | 
			
		||||
                    VkImageView *imageViews, uint32_t amountImages)
 | 
			
		||||
                    VkImageView *imageViews, uint32_t imagesSize, VkShaderModule *modules, uint32_t shaderModulesSize,
 | 
			
		||||
                    VkPipelineLayout *pipelineLayout)
 | 
			
		||||
{
 | 
			
		||||
    vkDeviceWaitIdle(*device);
 | 
			
		||||
 | 
			
		||||
    for (int i = 0; i < amountImages; i++)
 | 
			
		||||
    vkDestroyPipelineLayout(*device, *pipelineLayout, NULL);
 | 
			
		||||
 | 
			
		||||
    for (int i = 0; i < shaderModulesSize; ++i)
 | 
			
		||||
    {
 | 
			
		||||
        vkDestroyShaderModule(*device, modules[i], NULL);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    for (int i = 0; i < imagesSize; i++)
 | 
			
		||||
    {
 | 
			
		||||
        vkDestroyImageView(*device, imageViews[i], NULL);
 | 
			
		||||
    }
 | 
			
		||||
@@ -201,6 +220,140 @@ void shutdownGLFW(GLFWwindow *window)
 | 
			
		||||
    glfwTerminate();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int createShaderModule(VkDevice device, VkShaderModule *shaderModule, const char *shaderSource, long sourceSize)
 | 
			
		||||
{
 | 
			
		||||
    VkShaderModuleCreateInfo shaderModuleInfo;
 | 
			
		||||
    shaderModuleInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
 | 
			
		||||
    shaderModuleInfo.pNext = NULL;
 | 
			
		||||
    shaderModuleInfo.flags = 0;
 | 
			
		||||
    shaderModuleInfo.codeSize = sourceSize;
 | 
			
		||||
    shaderModuleInfo.pCode = (uint32_t *) shaderSource;
 | 
			
		||||
 | 
			
		||||
    ASSERT_VK_SUCCESS(vkCreateShaderModule(device, &shaderModuleInfo, NULL, shaderModule))
 | 
			
		||||
 | 
			
		||||
    return SUCCESS;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void createShaderStageInfo(VkPipelineShaderStageCreateInfo *shaderStageInfo, VkShaderStageFlagBits shaderStageBit, VkShaderModule shaderModule, const char *entryPointName)
 | 
			
		||||
{
 | 
			
		||||
    shaderStageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
 | 
			
		||||
    shaderStageInfo->pNext = NULL;
 | 
			
		||||
    shaderStageInfo->flags = 0;
 | 
			
		||||
    shaderStageInfo->stage = shaderStageBit;
 | 
			
		||||
    shaderStageInfo->module = shaderModule;
 | 
			
		||||
    shaderStageInfo->pName = entryPointName;
 | 
			
		||||
    shaderStageInfo->pSpecializationInfo = NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void createPipelineVertexInputStateInfo(VkPipelineVertexInputStateCreateInfo *vertexInputStateInfo, VkVertexInputAttributeDescription *attributes, uint32_t atrributesSize)
 | 
			
		||||
{
 | 
			
		||||
    vertexInputStateInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
 | 
			
		||||
    vertexInputStateInfo->pNext = NULL;
 | 
			
		||||
    vertexInputStateInfo->flags = 0;
 | 
			
		||||
    vertexInputStateInfo->vertexBindingDescriptionCount = 0;
 | 
			
		||||
    vertexInputStateInfo->pVertexBindingDescriptions = NULL;
 | 
			
		||||
    vertexInputStateInfo->vertexAttributeDescriptionCount = atrributesSize;
 | 
			
		||||
    vertexInputStateInfo->pVertexAttributeDescriptions = attributes;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void createInputAssemblyStateInfo(VkPipelineInputAssemblyStateCreateInfo *inputAssemblyStateInfo, VkPrimitiveTopology topology)
 | 
			
		||||
{
 | 
			
		||||
    inputAssemblyStateInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
 | 
			
		||||
    inputAssemblyStateInfo->pNext = NULL;
 | 
			
		||||
    inputAssemblyStateInfo->flags = 0;
 | 
			
		||||
    inputAssemblyStateInfo->topology = topology;
 | 
			
		||||
    inputAssemblyStateInfo->primitiveRestartEnable = VK_FALSE;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void createViewportStateInfo(VkPipelineViewportStateCreateInfo *viewportStateInfo, float width, float height)
 | 
			
		||||
{
 | 
			
		||||
    VkViewport viewport;
 | 
			
		||||
    viewport.x = 0.0f;
 | 
			
		||||
    viewport.y = 0.0f;
 | 
			
		||||
    viewport.width = width;
 | 
			
		||||
    viewport.height = height;
 | 
			
		||||
    viewport.minDepth = 0.0f;
 | 
			
		||||
    viewport.maxDepth = 1.0f;
 | 
			
		||||
 | 
			
		||||
    VkRect2D scissor = { {0, 0}, {width, height} };
 | 
			
		||||
 | 
			
		||||
    viewportStateInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
 | 
			
		||||
    viewportStateInfo->pNext = NULL;
 | 
			
		||||
    viewportStateInfo->flags = 0;
 | 
			
		||||
    viewportStateInfo->viewportCount = 1;
 | 
			
		||||
    viewportStateInfo->pViewports = &viewport;
 | 
			
		||||
    viewportStateInfo->scissorCount = 1;
 | 
			
		||||
    viewportStateInfo->pScissors = &scissor;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void createRasterizationStateInfo(VkPipelineRasterizationStateCreateInfo *rasterizationStateInfo, VkPolygonMode polygonMode)
 | 
			
		||||
{
 | 
			
		||||
    rasterizationStateInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
 | 
			
		||||
    rasterizationStateInfo->pNext = NULL;
 | 
			
		||||
    rasterizationStateInfo->flags = 0;
 | 
			
		||||
    rasterizationStateInfo->depthClampEnable = VK_FALSE;
 | 
			
		||||
    rasterizationStateInfo->rasterizerDiscardEnable = VK_FALSE;
 | 
			
		||||
    rasterizationStateInfo->polygonMode = polygonMode;
 | 
			
		||||
    rasterizationStateInfo->cullMode = VK_CULL_MODE_BACK_BIT;
 | 
			
		||||
    rasterizationStateInfo->frontFace = VK_FRONT_FACE_CLOCKWISE;
 | 
			
		||||
    rasterizationStateInfo->depthBiasEnable = VK_FALSE;
 | 
			
		||||
    rasterizationStateInfo->depthBiasConstantFactor = 0.0f;
 | 
			
		||||
    rasterizationStateInfo->depthBiasClamp = 0.0f;
 | 
			
		||||
    rasterizationStateInfo->depthBiasSlopeFactor = 0.0f;
 | 
			
		||||
    rasterizationStateInfo->lineWidth = 1.0f;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void createMultisampleStateInfo(VkPipelineMultisampleStateCreateInfo *multisampleStateInfo)
 | 
			
		||||
{
 | 
			
		||||
    multisampleStateInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
 | 
			
		||||
    multisampleStateInfo->pNext = NULL;
 | 
			
		||||
    multisampleStateInfo->flags = 0;
 | 
			
		||||
    multisampleStateInfo->rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
 | 
			
		||||
    multisampleStateInfo->sampleShadingEnable = VK_FALSE;
 | 
			
		||||
    multisampleStateInfo->minSampleShading = 1.0f;
 | 
			
		||||
    multisampleStateInfo->pSampleMask = NULL;
 | 
			
		||||
    multisampleStateInfo->alphaToCoverageEnable = VK_FALSE;
 | 
			
		||||
    multisampleStateInfo->alphaToOneEnable = VK_FALSE;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void createColorBlendAttachmentStateInfo(VkPipelineColorBlendAttachmentState  *colorBlendAttachmentState)
 | 
			
		||||
{
 | 
			
		||||
    colorBlendAttachmentState->blendEnable = VK_FALSE;
 | 
			
		||||
    colorBlendAttachmentState->srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
 | 
			
		||||
    colorBlendAttachmentState->dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
 | 
			
		||||
    colorBlendAttachmentState->colorBlendOp = VK_BLEND_OP_ADD;
 | 
			
		||||
    colorBlendAttachmentState->srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
 | 
			
		||||
    colorBlendAttachmentState->dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
 | 
			
		||||
    colorBlendAttachmentState->alphaBlendOp = VK_BLEND_OP_ADD;
 | 
			
		||||
    colorBlendAttachmentState->colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void createColorBlendStateInfo(VkPipelineColorBlendStateCreateInfo  *colorBlendStateInfo, VkPipelineColorBlendAttachmentState  *blendAttachments, uint32_t blendAttachmentsSize)
 | 
			
		||||
{
 | 
			
		||||
    colorBlendStateInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
 | 
			
		||||
    colorBlendStateInfo->pNext = NULL;
 | 
			
		||||
    colorBlendStateInfo->flags = 0;
 | 
			
		||||
    colorBlendStateInfo->logicOpEnable = VK_FALSE;
 | 
			
		||||
    colorBlendStateInfo->logicOp = VK_LOGIC_OP_NO_OP;
 | 
			
		||||
    colorBlendStateInfo->attachmentCount = blendAttachmentsSize;
 | 
			
		||||
    colorBlendStateInfo->pAttachments = blendAttachments;
 | 
			
		||||
    colorBlendStateInfo->blendConstants[0] = 0.0f;
 | 
			
		||||
    colorBlendStateInfo->blendConstants[1] = 0.0f;
 | 
			
		||||
    colorBlendStateInfo->blendConstants[2] = 0.0f;
 | 
			
		||||
    colorBlendStateInfo->blendConstants[3] = 0.0f;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void createLayoutInfo(VkPipelineLayoutCreateInfo  *layoutInfo, VkDescriptorSetLayout *setLayouts, uint32_t setLayoutSize)
 | 
			
		||||
{
 | 
			
		||||
    layoutInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
 | 
			
		||||
    layoutInfo->pNext = NULL;
 | 
			
		||||
    layoutInfo->flags = 0;
 | 
			
		||||
    layoutInfo->setLayoutCount = setLayoutSize;
 | 
			
		||||
    layoutInfo->pSetLayouts = setLayouts;
 | 
			
		||||
    layoutInfo->pushConstantRangeCount = 0;
 | 
			
		||||
    layoutInfo->pPushConstantRanges = NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void printStats(VkPhysicalDevice *physicalDevice, VkSurfaceKHR *surface)
 | 
			
		||||
{
 | 
			
		||||
    // Device Properties
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										35
									
								
								initVulkan.h
									
									
									
									
									
								
							
							
						
						
									
										35
									
								
								initVulkan.h
									
									
									
									
									
								
							@@ -27,16 +27,29 @@
 | 
			
		||||
 | 
			
		||||
int initVulkan(VkInstance *vkInstance, VkDevice *device, VkSurfaceKHR *surface, GLFWwindow *window,
 | 
			
		||||
               VkSwapchainKHR *swapChain, VkImageView **imageViews, uint32_t *amountImages);
 | 
			
		||||
void initAppInfo(VkApplicationInfo *appInfo);
 | 
			
		||||
void initCreateInfo(VkApplicationInfo *appInfo, VkInstanceCreateInfo *instanceInfo);
 | 
			
		||||
void initQueueInfo(VkDeviceQueueCreateInfo *queueInfo);
 | 
			
		||||
void initDeviceInfo(VkDeviceQueueCreateInfo *queueInfo, VkDeviceCreateInfo *deviceInfo, VkPhysicalDeviceFeatures *features);
 | 
			
		||||
void initImageViewInfo(VkImageViewCreateInfo *imageViewInfo, VkImage *swapChainImages, int index);
 | 
			
		||||
void printStats(VkPhysicalDevice *physicalDevice, VkSurfaceKHR *surface);
 | 
			
		||||
 | 
			
		||||
void initSwapChainInfo(VkSwapchainCreateInfoKHR *swapChainCreateInfo, VkSurfaceKHR *surface);
 | 
			
		||||
 | 
			
		||||
void createAppInfo(VkApplicationInfo *appInfo);
 | 
			
		||||
void createInstanceInfo(VkApplicationInfo *appInfo, VkInstanceCreateInfo *instanceInfo);
 | 
			
		||||
void createQueueInfo(VkDeviceQueueCreateInfo *queueInfo);
 | 
			
		||||
void createDeviceInfo(VkDeviceQueueCreateInfo *queueInfo, VkDeviceCreateInfo *deviceInfo,
 | 
			
		||||
        VkPhysicalDeviceFeatures *features);
 | 
			
		||||
void createImageViewInfo(VkImageViewCreateInfo *imageViewInfo, VkImage *swapChainImages, int index);
 | 
			
		||||
void createSwapChainInfo(VkSwapchainCreateInfoKHR *swapChainCreateInfo, VkSurfaceKHR *surface);
 | 
			
		||||
void createShaderStageInfo(VkPipelineShaderStageCreateInfo *shaderStageInfo, VkShaderStageFlagBits shaderStageBit,
 | 
			
		||||
        VkShaderModule shaderModule, const char *entryPointName);
 | 
			
		||||
int createShaderModule(VkDevice device, VkShaderModule *shaderModule, const char *shaderSource, long sourceSize);
 | 
			
		||||
void createPipelineVertexInputStateInfo(VkPipelineVertexInputStateCreateInfo *vertexInputStateInfo,
 | 
			
		||||
        VkVertexInputAttributeDescription *attributes, uint32_t atrributesSize);
 | 
			
		||||
void createInputAssemblyStateInfo(VkPipelineInputAssemblyStateCreateInfo *inputAssemblyStateInfo, VkPrimitiveTopology topology);
 | 
			
		||||
void createViewportStateInfo(VkPipelineViewportStateCreateInfo *viewportStateInfo, float width, float height);
 | 
			
		||||
void createRasterizationStateInfo(VkPipelineRasterizationStateCreateInfo *rasterizationStateInfo, VkPolygonMode polygonMode);
 | 
			
		||||
void createMultisampleStateInfo(VkPipelineMultisampleStateCreateInfo *multisampleStateInfo);
 | 
			
		||||
void createColorBlendAttachmentStateInfo(VkPipelineColorBlendAttachmentState  *colorBlendAttachmentState);
 | 
			
		||||
void createColorBlendStateInfo(VkPipelineColorBlendStateCreateInfo  *colorBlendStateInfo,
 | 
			
		||||
                               VkPipelineColorBlendAttachmentState  *blendAttachments, uint32_t blendAttachmentsSize);
 | 
			
		||||
void createLayoutInfo(VkPipelineLayoutCreateInfo  *layoutInfo, VkDescriptorSetLayout *setLayouts, uint32_t setLayoutSize);
 | 
			
		||||
void createAttachmentDescription(VkAttachmentDescription *attachmentDescription);
 | 
			
		||||
void shutdownVulkan(VkInstance *vkInstance, VkDevice *device, VkSurfaceKHR *surface, VkSwapchainKHR *swapChain,
 | 
			
		||||
                    VkImageView *imageViews, uint32_t amountImages);
 | 
			
		||||
 | 
			
		||||
                    VkImageView *imageViews, uint32_t imagesSize, VkShaderModule *modules, uint32_t shaderModulesSize,
 | 
			
		||||
                    VkPipelineLayout *pipelineLayout);
 | 
			
		||||
void shutdownGLFW(GLFWwindow *window);
 | 
			
		||||
void printStats(VkPhysicalDevice *physicalDevice, VkSurfaceKHR *surface);
 | 
			
		||||
 
 | 
			
		||||
@@ -28,9 +28,9 @@ int main()
 | 
			
		||||
    initRandomParticles(e1);
 | 
			
		||||
 | 
			
		||||
    /************* SHADER *************/
 | 
			
		||||
    const GLchar *computeShaderSource = readFile("./opengl/ComputeShader.glsl");
 | 
			
		||||
    const GLchar *vertexShaderSource = readFile("./opengl/VertexShader.glsl");
 | 
			
		||||
    const GLchar *fragmentShaderSource = readFile("./opengl/FragmentShader.glsl");
 | 
			
		||||
    const GLchar *computeShaderSource = readFile("./opengl/ComputeShader.glsl", "r", NULL);
 | 
			
		||||
    const GLchar *vertexShaderSource = readFile("./opengl/VertexShader.glsl", "r", NULL);
 | 
			
		||||
    const GLchar *fragmentShaderSource = readFile("./opengl/FragmentShader.glsl", "r", NULL);
 | 
			
		||||
 | 
			
		||||
    GLuint computeShader = compileShader(computeShaderSource, GL_COMPUTE_SHADER);
 | 
			
		||||
    GLuint vertexShader = compileShader(vertexShaderSource, GL_VERTEX_SHADER);
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										21
									
								
								utils.c
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								utils.c
									
									
									
									
									
								
							@@ -4,28 +4,33 @@
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include "utils.h"
 | 
			
		||||
 | 
			
		||||
char *readFile(char *filename)
 | 
			
		||||
char *readFile(char *filename, char *mode, long *size)
 | 
			
		||||
{
 | 
			
		||||
    FILE    *file;
 | 
			
		||||
    char    *buffer;
 | 
			
		||||
    long    numbytes;
 | 
			
		||||
    FILE *file;
 | 
			
		||||
    char *buffer;
 | 
			
		||||
    long bytes;
 | 
			
		||||
 | 
			
		||||
    if((file = fopen(filename, "r")) == NULL)
 | 
			
		||||
    if((file = fopen(filename, mode)) == NULL)
 | 
			
		||||
    {
 | 
			
		||||
        printf("ERROR open file %s: %s\n", filename, strerror(errno));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fseek(file, 0L, SEEK_END);
 | 
			
		||||
    numbytes = ftell(file);
 | 
			
		||||
    bytes = ftell(file);
 | 
			
		||||
    fseek(file, 0L, SEEK_SET);
 | 
			
		||||
 | 
			
		||||
    if((buffer = calloc(numbytes, sizeof(char))) == NULL)
 | 
			
		||||
    if((buffer = calloc(bytes, sizeof(char))) == NULL)
 | 
			
		||||
    {
 | 
			
		||||
        printf("ERROR allocating memory: %s\n", strerror(errno));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fread(buffer, sizeof(char), numbytes, file);
 | 
			
		||||
    fread(buffer, sizeof(char), bytes, file);
 | 
			
		||||
    fclose(file);
 | 
			
		||||
 | 
			
		||||
    if (size != NULL)
 | 
			
		||||
    {
 | 
			
		||||
        *size = bytes;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return buffer;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										2
									
								
								utils.h
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								utils.h
									
									
									
									
									
								
							@@ -4,4 +4,4 @@
 | 
			
		||||
#define UPPER_AGE 250
 | 
			
		||||
#define LOWER_AGE 60
 | 
			
		||||
 | 
			
		||||
char *readFile(char *filename);
 | 
			
		||||
char *readFile(char *filename, char *mode, long *size);
 | 
			
		||||
							
								
								
									
										84
									
								
								vulkanMain.c
									
									
									
									
									
								
							
							
						
						
									
										84
									
								
								vulkanMain.c
									
									
									
									
									
								
							@@ -1,6 +1,10 @@
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
 | 
			
		||||
#include "initVulkan.h"
 | 
			
		||||
#include "particlesystem.h"
 | 
			
		||||
 | 
			
		||||
#define PARTICLE_AMOUNT 100
 | 
			
		||||
 | 
			
		||||
int main()
 | 
			
		||||
{
 | 
			
		||||
@@ -10,7 +14,9 @@ int main()
 | 
			
		||||
    VkSwapchainKHR swapChain;
 | 
			
		||||
    VkImageView *imageViews = NULL;
 | 
			
		||||
    uint32_t amountImages;
 | 
			
		||||
    VkPipelineLayout pipelineLayout;
 | 
			
		||||
 | 
			
		||||
    /************* INIT *************/
 | 
			
		||||
    // GLFW
 | 
			
		||||
    ASSERT_GLFW_SUCCESS(glfwInit())
 | 
			
		||||
    glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
 | 
			
		||||
@@ -20,17 +26,87 @@ int main()
 | 
			
		||||
    // Init Vulkan
 | 
			
		||||
    ASSERT_SUCCESS(initVulkan(&vkInstance, &device, &surface, window, &swapChain, &imageViews, &amountImages))
 | 
			
		||||
 | 
			
		||||
    // Render Loop
 | 
			
		||||
    /************* PARTICLE SYSTEM *************/
 | 
			
		||||
    vector3f *epos1 = initVector3f(0, 0, 0);
 | 
			
		||||
    emitter *e1 = initEmitter(epos1, PARTICLE_AMOUNT);
 | 
			
		||||
    particle_system *ps = initParticleSystem(1);
 | 
			
		||||
    (ps->emitters)[0] = e1;
 | 
			
		||||
    initRandomParticles(e1);
 | 
			
		||||
 | 
			
		||||
    /************* SHADER *************/
 | 
			
		||||
    // Shader Modules
 | 
			
		||||
    long computeShaderSourceSize, vertexShaderSourceSize, fragmentShaderSourceSize;
 | 
			
		||||
    char *computeShaderSource = readFile("./vulkan/comp.spv", "rb", &computeShaderSourceSize);
 | 
			
		||||
    char *vertexShaderSource = readFile("./vulkan/vert.spv", "rb", &vertexShaderSourceSize);
 | 
			
		||||
    char *fragmentShaderSource = readFile("./vulkan/frag.spv", "rb", &fragmentShaderSourceSize);
 | 
			
		||||
 | 
			
		||||
    VkShaderModule computeShaderModule;
 | 
			
		||||
    createShaderModule(device, &computeShaderModule, computeShaderSource, computeShaderSourceSize);
 | 
			
		||||
    VkShaderModule vertexShaderModule;
 | 
			
		||||
    createShaderModule(device, &vertexShaderModule, vertexShaderSource, vertexShaderSourceSize);
 | 
			
		||||
    VkShaderModule fragmentShaderModule;
 | 
			
		||||
    createShaderModule(device, &fragmentShaderModule, fragmentShaderSource, fragmentShaderSourceSize);
 | 
			
		||||
 | 
			
		||||
    // Shader stages
 | 
			
		||||
    VkPipelineShaderStageCreateInfo computeShaderStageInfo;
 | 
			
		||||
    createShaderStageInfo(&computeShaderStageInfo, VK_SHADER_STAGE_COMPUTE_BIT, computeShaderModule, "main");
 | 
			
		||||
    VkPipelineShaderStageCreateInfo vertexShaderStageInfo;
 | 
			
		||||
    createShaderStageInfo(&vertexShaderStageInfo, VK_SHADER_STAGE_VERTEX_BIT, vertexShaderModule, "main");
 | 
			
		||||
    VkPipelineShaderStageCreateInfo fragmentShaderStageInfo;
 | 
			
		||||
    createShaderStageInfo(&fragmentShaderStageInfo, VK_SHADER_STAGE_FRAGMENT_BIT, fragmentShaderModule, "main");
 | 
			
		||||
 | 
			
		||||
    VkPipelineShaderStageCreateInfo shaderStages[3] = { computeShaderStageInfo, vertexShaderStageInfo, fragmentShaderStageInfo };
 | 
			
		||||
 | 
			
		||||
    /************* PIPELINE *************/
 | 
			
		||||
    // Vertex input
 | 
			
		||||
    VkPipelineVertexInputStateCreateInfo vertexInputStateInfo;
 | 
			
		||||
    createPipelineVertexInputStateInfo(&vertexInputStateInfo, NULL, 0);
 | 
			
		||||
 | 
			
		||||
    // Input assembly
 | 
			
		||||
    VkPipelineInputAssemblyStateCreateInfo inputAssemblyStateInfo;
 | 
			
		||||
    createInputAssemblyStateInfo(&inputAssemblyStateInfo, VK_PRIMITIVE_TOPOLOGY_POINT_LIST);
 | 
			
		||||
 | 
			
		||||
    // Viewport
 | 
			
		||||
    VkPipelineViewportStateCreateInfo viewportStateInfo;
 | 
			
		||||
    createViewportStateInfo(&viewportStateInfo, WIDTH, HEIGHT);
 | 
			
		||||
 | 
			
		||||
    // Rasterization
 | 
			
		||||
    VkPipelineRasterizationStateCreateInfo rasterizationStateInfo;
 | 
			
		||||
    createRasterizationStateInfo(&rasterizationStateInfo, VK_POLYGON_MODE_POINT);
 | 
			
		||||
 | 
			
		||||
    // Multisampling
 | 
			
		||||
    VkPipelineMultisampleStateCreateInfo multisampleStateInfo;
 | 
			
		||||
    createMultisampleStateInfo(&multisampleStateInfo);
 | 
			
		||||
 | 
			
		||||
    // Blending
 | 
			
		||||
    VkPipelineColorBlendAttachmentState colorBlendAttachmentState;
 | 
			
		||||
    createColorBlendAttachmentStateInfo(&colorBlendAttachmentState);
 | 
			
		||||
    VkPipelineColorBlendStateCreateInfo colorBlendStateInfo;
 | 
			
		||||
    createColorBlendStateInfo(&colorBlendStateInfo, &colorBlendAttachmentState, 1);
 | 
			
		||||
 | 
			
		||||
    // Layout
 | 
			
		||||
    VkPipelineLayoutCreateInfo layoutInfo;
 | 
			
		||||
    createLayoutInfo(&layoutInfo, NULL, 0);
 | 
			
		||||
    ASSERT_VK_SUCCESS(vkCreatePipelineLayout(device, &layoutInfo, NULL, &pipelineLayout))
 | 
			
		||||
 | 
			
		||||
    VkAttachmentDescription attachmentDescription;
 | 
			
		||||
    createAttachmentDescription(&attachmentDescription);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    /************* RENDER LOOP *************/
 | 
			
		||||
    while (!glfwWindowShouldClose(window))
 | 
			
		||||
    {
 | 
			
		||||
        glfwPollEvents();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Stop Vulkan
 | 
			
		||||
    shutdownVulkan(&vkInstance, &device, &surface, &swapChain, imageViews, amountImages);
 | 
			
		||||
    // Shutdown Vulkan
 | 
			
		||||
    VkShaderModule modules[3] = { computeShaderModule, vertexShaderModule, fragmentShaderModule };
 | 
			
		||||
    shutdownVulkan(&vkInstance, &device, &surface, &swapChain, imageViews, amountImages, modules, 3, &pipelineLayout);
 | 
			
		||||
 | 
			
		||||
    // Stop GLFW
 | 
			
		||||
    // Shutdown GLFW
 | 
			
		||||
    shutdownGLFW(window);
 | 
			
		||||
 | 
			
		||||
    return SUCCESS;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user