diff --git a/CMakeLists.txt b/CMakeLists.txt index e444c4a..8766a46 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,12 +22,10 @@ configure_file(./shaders/vulkan/FragmentShader.frag ./vulkan/FragmentShader.frag configure_file(./shaders/vulkan/runCompiler.bat ./vulkan/runCompiler.bat COPYONLY) execute_process(COMMAND runCompiler.bat WORKING_DIRECTORY ./vulkan) - # Vulkan find_package(Vulkan REQUIRED) target_include_directories(${PROJECT_NAME} PUBLIC ${Vulkan_INCLUDE_DIRS}) - # GLFW include_directories(./glfw/include) target_link_libraries(Informatikprojekt ${CMAKE_SOURCE_DIR}/glfw/lib-mingw-w64/libglfw3.a) diff --git a/cpuMain.c b/cpuMain.c index aadd054..5e628ab 100644 --- a/cpuMain.c +++ b/cpuMain.c @@ -2,7 +2,7 @@ #include "initOpenGL.h" #include "utils.h" -#define PARTICLE_AMOUNT 1000 +#define PARTICLE_AMOUNT 10000 void calcPos(particle *p, float dt); void calcCol(particle *p); diff --git a/initOpenGL.c b/initOpenGL.c index 28ce2f8..5a50d99 100644 --- a/initOpenGL.c +++ b/initOpenGL.c @@ -63,19 +63,6 @@ void framebufferSizeCallback(GLFWwindow *window, int width, int height) glViewport(0, 0, width, height); } -void initVertexBufferObject(unsigned int *VBO, float *vertices) -{ - glGenBuffers(1, VBO); - glBindBuffer(GL_SHADER_STORAGE_BUFFER, *VBO); - glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(vertices), vertices, GL_STREAM_DRAW); -} - -void initVertexArrayBuffer(unsigned int *VAO) -{ - glGenVertexArrays(1, VAO); - glBindVertexArray(*VAO); -} - GLuint compileShader(const GLchar *shaderSource, GLenum shaderType) { GLuint shader; diff --git a/initOpenGL.h b/initOpenGL.h index dfd1ca3..1086dbb 100644 --- a/initOpenGL.h +++ b/initOpenGL.h @@ -18,9 +18,6 @@ void terminateGLFW(GLFWwindow *window); void errorCallback(int error, const char* description); void framebufferSizeCallback(GLFWwindow *window, int width, int height); -void initVertexBufferObject(unsigned int *VBO, float *vertices); -void initVertexArrayBuffer(unsigned int *VAO); - GLuint compileShader(const GLchar *shaderSource, GLenum shaderType); GLuint linkShaders(GLuint *shaders, GLsizei count); void deleteShaders(GLuint *shaders, GLsizei count); diff --git a/initVulkan.c b/initVulkan.c index 10ec13a..c8e2635 100644 --- a/initVulkan.c +++ b/initVulkan.c @@ -190,20 +190,133 @@ void createAttachmentDescription(VkAttachmentDescription *attachmentDescription) attachmentDescription->finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; } +void createAttachmentReference(VkAttachmentReference *attachmentReference, uint32_t attachment) +{ + attachmentReference->attachment = attachment; + attachmentReference->layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; +} + +void createSubpassDescription(VkSubpassDescription *subpassDescription, VkPipelineBindPoint bindPoint, + VkAttachmentReference *attachmentReference) +{ + subpassDescription->flags = 0; + subpassDescription->pipelineBindPoint = bindPoint; + subpassDescription->inputAttachmentCount = 0; + subpassDescription->pInputAttachments = NULL; + subpassDescription->colorAttachmentCount = 1; + subpassDescription->pColorAttachments = attachmentReference; + subpassDescription->pResolveAttachments = NULL; + subpassDescription->pDepthStencilAttachment = NULL; + subpassDescription->preserveAttachmentCount = 0; + subpassDescription->pPreserveAttachments = NULL; +} + +void createRenderPassInfo(VkRenderPassCreateInfo *renderPassInfo, VkAttachmentDescription *attachmentDescriptions, VkSubpassDescription *subpassDescriptions) +{ + renderPassInfo->sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; + renderPassInfo->pNext = NULL; + renderPassInfo->flags = 0; + renderPassInfo->attachmentCount = 1; + renderPassInfo->pAttachments = attachmentDescriptions; + renderPassInfo->subpassCount = 1; + renderPassInfo->pSubpasses = subpassDescriptions; + renderPassInfo->dependencyCount = 0; + renderPassInfo->pDependencies = NULL; +} + +void createGraphicsPipelineInfo(VkGraphicsPipelineCreateInfo *graphicsPipelineInfo, + VkPipelineShaderStageCreateInfo *shaderStages, + VkPipelineVertexInputStateCreateInfo *vertexInputState, + VkPipelineInputAssemblyStateCreateInfo *inputAssemblyState, + VkPipelineViewportStateCreateInfo *viewportState, + VkPipelineRasterizationStateCreateInfo *rasterizationState, + VkPipelineMultisampleStateCreateInfo *multisampleState, + VkPipelineColorBlendStateCreateInfo *colorBlendState, VkPipelineLayout *pipelineLayout, + VkRenderPass *renderPass) +{ + graphicsPipelineInfo->sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; + graphicsPipelineInfo->pNext = NULL; + graphicsPipelineInfo->flags = 0; + graphicsPipelineInfo->stageCount = 3; + graphicsPipelineInfo->pStages = shaderStages; + graphicsPipelineInfo->pVertexInputState = vertexInputState; + graphicsPipelineInfo->pInputAssemblyState = inputAssemblyState; + graphicsPipelineInfo->pTessellationState = NULL; + graphicsPipelineInfo->pViewportState = viewportState; + graphicsPipelineInfo->pRasterizationState = rasterizationState; + graphicsPipelineInfo->pMultisampleState = multisampleState; + graphicsPipelineInfo->pDepthStencilState = NULL; + graphicsPipelineInfo->pColorBlendState = colorBlendState; + graphicsPipelineInfo->pDynamicState = NULL; + graphicsPipelineInfo->layout = *pipelineLayout; + graphicsPipelineInfo->renderPass = *renderPass; + graphicsPipelineInfo->subpass = 0; + graphicsPipelineInfo->basePipelineHandle = VK_NULL_HANDLE; + graphicsPipelineInfo->basePipelineIndex = -1; +} + +void createFramebufferInfo(VkFramebufferCreateInfo *framebufferInfo, VkRenderPass *renderPass, VkImageView *imageView) +{ + framebufferInfo->sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; + framebufferInfo->pNext = NULL; + framebufferInfo->flags = 0; + framebufferInfo->renderPass = *renderPass; + framebufferInfo->attachmentCount = 1; + framebufferInfo->pAttachments = imageView; + framebufferInfo->width = WIDTH; + framebufferInfo->height = HEIGHT; + framebufferInfo->layers = 1; +} + +void createCommandPoolInfo(VkCommandPoolCreateInfo *commandPoolInfo, uint32_t queueFamilyIndex) +{ + commandPoolInfo->sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; + commandPoolInfo->pNext = NULL; + commandPoolInfo->flags = 0; + commandPoolInfo->queueFamilyIndex = queueFamilyIndex; +} + +void createCommandBufferAllocateInfo(VkCommandBufferAllocateInfo *commandBufferAllocateInfo) +{ + +} + void shutdownVulkan(VkInstance *vkInstance, VkDevice *device, VkSurfaceKHR *surface, VkSwapchainKHR *swapChain, - VkImageView *imageViews, uint32_t imagesSize, VkShaderModule *modules, uint32_t shaderModulesSize, - VkPipelineLayout *pipelineLayout) + VkImageView *imageViews, uint32_t imageViewsSize, VkShaderModule *modules, + uint32_t shaderModulesSize, VkPipelineLayout *pipelineLayouts, uint32_t pipelineLayoutsSize, + VkRenderPass *renderPasses, uint32_t renderPassesSize, VkPipeline *pipelines, + uint32_t pipelinesSize, VkFramebuffer *framebuffers, VkCommandPool *commandPool) { vkDeviceWaitIdle(*device); - vkDestroyPipelineLayout(*device, *pipelineLayout, NULL); + vkDestroyCommandPool(*device, *commandPool, NULL); + + for (int i = 0; i < imageViewsSize; ++i) + { + vkDestroyFramebuffer(*device, framebuffers[i], NULL); + } + + for (int i = 0; i < pipelinesSize; ++i) + { + vkDestroyPipeline(*device, pipelines[i], NULL); + } + + for (int i = 0; i < renderPassesSize; ++i) + { + vkDestroyRenderPass(*device, renderPasses[i], NULL); + } + + for (int i = 0; i < pipelineLayoutsSize; ++i) + { + vkDestroyPipelineLayout(*device, pipelineLayouts[i], NULL); + } for (int i = 0; i < shaderModulesSize; ++i) { vkDestroyShaderModule(*device, modules[i], NULL); } - for (int i = 0; i < imagesSize; i++) + for (int i = 0; i < imageViewsSize; i++) { vkDestroyImageView(*device, imageViews[i], NULL); } diff --git a/initVulkan.h b/initVulkan.h index 7790db9..3b686d0 100644 --- a/initVulkan.h +++ b/initVulkan.h @@ -48,8 +48,26 @@ void createColorBlendStateInfo(VkPipelineColorBlendStateCreateInfo *colorBlendS VkPipelineColorBlendAttachmentState *blendAttachments, uint32_t blendAttachmentsSize); void createLayoutInfo(VkPipelineLayoutCreateInfo *layoutInfo, VkDescriptorSetLayout *setLayouts, uint32_t setLayoutSize); void createAttachmentDescription(VkAttachmentDescription *attachmentDescription); +void createAttachmentReference(VkAttachmentReference *attachmentReference, uint32_t attachment); +void createSubpassDescription(VkSubpassDescription *subpassDescription, VkPipelineBindPoint bindPoint, + VkAttachmentReference *attachmentReference); +void createRenderPassInfo(VkRenderPassCreateInfo *renderPassInfo, VkAttachmentDescription *attachmentDescriptions, + VkSubpassDescription *subpassDescriptions); +void createGraphicsPipelineInfo(VkGraphicsPipelineCreateInfo *graphicsPipelineInfo, + VkPipelineShaderStageCreateInfo *shaderStages, + VkPipelineVertexInputStateCreateInfo *vertexInputState, + VkPipelineInputAssemblyStateCreateInfo *inputAssemblyState, + VkPipelineViewportStateCreateInfo *viewportState, + VkPipelineRasterizationStateCreateInfo *rasterizationState, + VkPipelineMultisampleStateCreateInfo *multisampleState, + VkPipelineColorBlendStateCreateInfo *colorBlendState, VkPipelineLayout *pipelineLayout, + VkRenderPass *renderPass); +void createFramebufferInfo(VkFramebufferCreateInfo *framebufferInfo, VkRenderPass *renderPass, VkImageView *imageView); +void createCommandPoolInfo(VkCommandPoolCreateInfo *commandPoolInfo, uint32_t queueFamilyIndex); void shutdownVulkan(VkInstance *vkInstance, VkDevice *device, VkSurfaceKHR *surface, VkSwapchainKHR *swapChain, - VkImageView *imageViews, uint32_t imagesSize, VkShaderModule *modules, uint32_t shaderModulesSize, - VkPipelineLayout *pipelineLayout); + VkImageView *imageViews, uint32_t imageViewsSize, VkShaderModule *modules, + uint32_t shaderModulesSize, VkPipelineLayout *pipelineLayouts, uint32_t pipelineLayoutsSize, + VkRenderPass *renderPasses, uint32_t renderPassesSize, VkPipeline *pipelines, + uint32_t pipelinesSize, VkFramebuffer *framebuffers, VkCommandPool *commandPool); void shutdownGLFW(GLFWwindow *window); void printStats(VkPhysicalDevice *physicalDevice, VkSurfaceKHR *surface); diff --git a/openglMain.c b/openglMain.c index 95cb5bf..cb26cb0 100644 --- a/openglMain.c +++ b/openglMain.c @@ -1,8 +1,9 @@ +#include #include "particlesystem.h" #include "initOpenGL.h" #include "utils.h" -#define PARTICLE_AMOUNT 1000000 +#define PARTICLE_AMOUNT 10000000 int main() { @@ -49,6 +50,7 @@ int main() GLuint renderShaderProgram = linkShaders(renderShaders, 2); float *particles = serializeParticlesystem(ps); + freeParticleSystem(ps); GLsizeiptr sizeOfParticle = 3 * sizeof(vector3f) + sizeof(float); GLuint particleBuffer; @@ -58,6 +60,8 @@ int main() glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, particleBuffer); glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); + free(particles); + GLuint vertexArray; glGenVertexArrays(1, &vertexArray); glBindVertexArray(vertexArray); @@ -99,10 +103,11 @@ int main() //END deleteShaders(renderShaders, 2); + glDeleteProgram(renderShaderProgram); deleteShaders(computeShaders, 1); - + glDeleteProgram(computeShaderProgram); + glDeleteBuffers(1, &particleBuffer); terminateGLFW(window); - freeParticleSystem(ps); return 0; } \ No newline at end of file diff --git a/shaders/vulkan/runCompiler.bat b/shaders/vulkan/runCompiler.bat index a01d804..6cfcdf2 100644 --- a/shaders/vulkan/runCompiler.bat +++ b/shaders/vulkan/runCompiler.bat @@ -1,3 +1,3 @@ -D:\Programme\VulkanSDK\1.1.106.0\Bin\glslangValidator.exe -V ComputeShader.comp -D:\Programme\VulkanSDK\1.1.106.0\Bin\glslangValidator.exe -V VertexShader.vert -D:\Programme\VulkanSDK\1.1.106.0\Bin\glslangValidator.exe -V FragmentShader.frag \ No newline at end of file +D:\Programme\VulkanSDK\1.2.131.2\Bin\glslangValidator.exe -V ComputeShader.comp +D:\Programme\VulkanSDK\1.2.131.2\Bin\glslangValidator.exe -V VertexShader.vert +D:\Programme\VulkanSDK\1.2.131.2\Bin\glslangValidator.exe -V FragmentShader.frag \ No newline at end of file diff --git a/vulkanMain.c b/vulkanMain.c index f97fdb4..459c86d 100644 --- a/vulkanMain.c +++ b/vulkanMain.c @@ -4,7 +4,7 @@ #include "initVulkan.h" #include "particlesystem.h" -#define PARTICLE_AMOUNT 100 +#define PARTICLE_AMOUNT 0 int main() { @@ -13,8 +13,12 @@ int main() VkSurfaceKHR surface; VkSwapchainKHR swapChain; VkImageView *imageViews = NULL; - uint32_t amountImages; + uint32_t imageViewsSize; VkPipelineLayout pipelineLayout; + VkRenderPass renderPass; + VkPipeline pipeline; + VkFramebuffer *framebuffers; + VkCommandPool commandPool; /************* INIT *************/ // GLFW @@ -24,7 +28,7 @@ int main() GLFWwindow *window = glfwCreateWindow(WIDTH, HEIGHT, "Informatikprojekt - Vulkan", NULL, NULL); // Init Vulkan - ASSERT_SUCCESS(initVulkan(&vkInstance, &device, &surface, window, &swapChain, &imageViews, &amountImages)) + ASSERT_SUCCESS(initVulkan(&vkInstance, &device, &surface, window, &swapChain, &imageViews, &imageViewsSize)) /************* PARTICLE SYSTEM *************/ vector3f *epos1 = initVector3f(0, 0, 0); @@ -89,19 +93,58 @@ int main() createLayoutInfo(&layoutInfo, NULL, 0); ASSERT_VK_SUCCESS(vkCreatePipelineLayout(device, &layoutInfo, NULL, &pipelineLayout)) + // Attachments VkAttachmentDescription attachmentDescription; createAttachmentDescription(&attachmentDescription); + VkAttachmentReference attachmentReference; + createAttachmentReference(&attachmentReference, 0); + + // Subpasses + VkSubpassDescription subpassDescription; + createSubpassDescription(&subpassDescription, VK_PIPELINE_BIND_POINT_GRAPHICS, &attachmentReference); + + // Renderpass + VkRenderPassCreateInfo renderPassInfo; + createRenderPassInfo(&renderPassInfo, &attachmentDescription, &subpassDescription); + ASSERT_VK_SUCCESS(vkCreateRenderPass(device, &renderPassInfo, NULL, &renderPass)) + + // Graphics pipeline + VkGraphicsPipelineCreateInfo graphicsPipelineInfo; + createGraphicsPipelineInfo(&graphicsPipelineInfo, shaderStages, &vertexInputStateInfo, &inputAssemblyStateInfo, + &viewportStateInfo, &rasterizationStateInfo, &multisampleStateInfo, &colorBlendStateInfo, + &pipelineLayout, &renderPass); + ASSERT_VK_SUCCESS(vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &graphicsPipelineInfo, NULL, &pipeline)) + + // Framebuffers + framebuffers = malloc(imageViewsSize * sizeof(VkFramebuffer)); + for (int i = 0; i < imageViewsSize; ++i) + { + VkFramebufferCreateInfo framebufferInfo; + createFramebufferInfo(&framebufferInfo, &renderPass, &imageViews[i]); + + ASSERT_VK_SUCCESS(vkCreateFramebuffer(device, &framebufferInfo, NULL, &framebuffers[i])) + } + + // Command pool + VkCommandPoolCreateInfo commandPoolInfo; + createCommandPoolInfo(&commandPoolInfo, 0); + ASSERT_VK_SUCCESS(vkCreateCommandPool(device, &commandPoolInfo, NULL, &commandPool)) + + // Allocate info + VkCommandBufferAllocateInfo commandBufferAllocateInfo; + /************* RENDER LOOP *************/ while (!glfwWindowShouldClose(window)) { glfwPollEvents(); } - // Shutdown Vulkan VkShaderModule modules[3] = { computeShaderModule, vertexShaderModule, fragmentShaderModule }; - shutdownVulkan(&vkInstance, &device, &surface, &swapChain, imageViews, amountImages, modules, 3, &pipelineLayout); + shutdownVulkan(&vkInstance, &device, &surface, &swapChain, imageViews, imageViewsSize, modules, 3, &pipelineLayout, + 1, + &renderPass, 1, &pipeline, 1, framebuffers, &commandPool); // Shutdown GLFW shutdownGLFW(window);