From 357cca6dd53130428c9251362403f2867018b12d Mon Sep 17 00:00:00 2001 From: Niklas Birk Date: Wed, 18 Mar 2020 12:29:08 +0100 Subject: [PATCH] did vulkan stuff / opengl stuff --- CMakeLists.txt | 4 ++-- cpuMain.c | 2 +- initVulkan.c | 8 +++++--- initVulkan.h | 4 ++-- openglMain.c | 28 +++++++++++++++------------- shaders/opengl/ComputeShader.glsl | 23 ++++++++++++----------- utils.c | 27 +++++++++++++++++++++++++++ def.h => utils.h | 4 +++- vulkanMain.c | 4 ++-- 9 files changed, 69 insertions(+), 35 deletions(-) create mode 100644 utils.c rename def.h => utils.h (55%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 15b2d27..5c489c6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,8 +2,8 @@ cmake_minimum_required(VERSION 3.15) project(Informatikprojekt C) set(CMAKE_C_STANDARD 11) -set(PROJECT_SOURCES particlesystem.h particlesystem.c def.h) -set(PROJECT_SOURCES_OPENGL ${PROJECT_SOURCES} initOpenGL.h initOpenGL.c def.h glad/src/glad.c) +set(PROJECT_SOURCES particlesystem.h particlesystem.c utils.h utils.c) +set(PROJECT_SOURCES_OPENGL ${PROJECT_SOURCES} initOpenGL.h initOpenGL.c utils.h glad/src/glad.c) set(PROJECT_SOURCES_VULKAN ${PROJECT_SOURCES} initVulkan.h initVulkan.c) add_executable(Informatikprojekt cpuMain.c ${PROJECT_SOURCES_OPENGL}) diff --git a/cpuMain.c b/cpuMain.c index c98143b..aadd054 100644 --- a/cpuMain.c +++ b/cpuMain.c @@ -1,6 +1,6 @@ #include "particlesystem.h" #include "initOpenGL.h" -#include "def.h" +#include "utils.h" #define PARTICLE_AMOUNT 1000 diff --git a/initVulkan.c b/initVulkan.c index 4b13722..c4b72ec 100644 --- a/initVulkan.c +++ b/initVulkan.c @@ -1,7 +1,7 @@ #include "initVulkan.h" int initVulkan(VkInstance *vkInstance, VkDevice *device, VkSurfaceKHR *surface, GLFWwindow *window, - VkSwapchainKHR *swapChain, VkImageView *imageViews, uint32_t *amountImages) + VkSwapchainKHR *swapChain, VkImageView **imageViews, uint32_t *amountImages) { // VkApplicationInfo VkApplicationInfo appInfo; @@ -68,14 +68,16 @@ int initVulkan(VkInstance *vkInstance, VkDevice *device, VkSurfaceKHR *surface, ASSERT_VK_SUCCESS(vkGetSwapchainImagesKHR(*device, *swapChain, amountImages, swapChainImages)) // Image view - imageViews = malloc(*amountImages * sizeof(VkImageView)); + *imageViews = malloc(*amountImages * sizeof(VkImageView)); VkImageViewCreateInfo imageViewInfo; for (int i = 0; i < *amountImages; i++) { initImageViewInfo(&imageViewInfo, swapChainImages, i); - ASSERT_VK_SUCCESS(vkCreateImageView(*device, &imageViewInfo, NULL, &imageViews[i])) + ASSERT_VK_SUCCESS(vkCreateImageView(*device, &imageViewInfo, NULL, &*imageViews[i])) } + + return SUCCESS; } diff --git a/initVulkan.h b/initVulkan.h index 2a4a403..82fbb24 100644 --- a/initVulkan.h +++ b/initVulkan.h @@ -5,7 +5,7 @@ #define GLFW_INCLUDE_VULKAN #include "GLFW/glfw3.h" -#include "def.h" +#include "utils.h" #define APP_NAME "Informatikprojekt - Vulkan" #define APP_VERSION VK_MAKE_VERSION(0, 0, 0) @@ -26,7 +26,7 @@ val * 9.313226e-10 int initVulkan(VkInstance *vkInstance, VkDevice *device, VkSurfaceKHR *surface, GLFWwindow *window, - VkSwapchainKHR *swapChain, VkImageView *imageViews, uint32_t *amountImages); + VkSwapchainKHR *swapChain, VkImageView **imageViews, uint32_t *amountImages); void initAppInfo(VkApplicationInfo *appInfo); void initCreateInfo(VkApplicationInfo *appInfo, VkInstanceCreateInfo *instanceInfo); void initQueueInfo(VkDeviceQueueCreateInfo *queueInfo); diff --git a/openglMain.c b/openglMain.c index 1627e86..019bd15 100644 --- a/openglMain.c +++ b/openglMain.c @@ -1,6 +1,7 @@ +#include #include "particlesystem.h" #include "initOpenGL.h" -#include "def.h" +#include "utils.h" #define PARTICLE_AMOUNT 1000000 @@ -31,6 +32,7 @@ int main() const GLchar *computeShaderSource = "#version 460\n" "\n" "#define FLOAT_MAX 4294967296.0f\n" + "#define FLOAT_FACTOR 0.00000000023283064365386962890625f\n" "\n" "struct particle\n" "{\n" @@ -64,13 +66,17 @@ int main() "\n" "uint rand(uint seed)\n" "{\n" - " // Xorshift algorithm from George Marsaglia's paper\n" " seed ^= (seed << 13u);\n" " seed ^= (seed >> 17u);\n" " seed ^= (seed << 5u);\n" " return seed;\n" "}\n" "\n" + "int foreSign(uint seed)\n" + "{\n" + " return rand(seed) % 2 == 0 ? 1 : -1;\n" + "}\n" + "\n" "void main()\n" "{\n" " uint gid = gl_GlobalInvocationID.x;\n" @@ -82,7 +88,7 @@ int main() " uint hash1 = hash(uvec3(uint(part.px * FLOAT_MAX), uint(part.cy * FLOAT_MAX), uint(part.vz * FLOAT_MAX)));\n" " uint hash2 = hash(uvec3(uint(part.vx * FLOAT_MAX), uint(part.py * FLOAT_MAX), uint(part.cz * FLOAT_MAX)));\n" " uint hash3 = hash(uvec3(uint(part.cx * FLOAT_MAX), uint(part.vy * FLOAT_MAX), uint(part.pz * FLOAT_MAX)));\n" - " \n" + "\n" " if (part.age < 0 || part.px > 1 || part.py > 1 || part.pz > 1 || part.px < -1 || part.py < -1 || part.pz < -1)\n" " {\n" " part.px = resetPos.x;\n" @@ -91,13 +97,13 @@ int main() "\n" " part.age = rand(hash(uvec3(hash1, hash2, hash3))) % (250 - 60 + 1) + 60;\n" "\n" - " part.vx = (rand(hash1) % 2 == 0 ? 1 : -1) * float(rand(hash2)) * (1.0f / FLOAT_MAX);\n" - " part.vy = (rand(hash3) % 2 == 0 ? 1 : -1) * float(rand(hash1)) * (1.0f / FLOAT_MAX);\n" - " part.vz = (rand(hash2) % 2 == 0 ? 1 : -1) * float(rand(hash3)) * (1.0f / FLOAT_MAX);\n" + " part.vx = foreSign(hash1) * float(rand(hash2)) * FLOAT_FACTOR;\n" + " part.vy = foreSign(hash3) * float(rand(hash1)) * FLOAT_FACTOR;\n" + " part.vz = foreSign(hash2) * float(rand(hash3)) * FLOAT_FACTOR;\n" "\n" - " part.cx = float(rand(hash3)) * (1.0f / FLOAT_MAX);\n" - " part.cy = float(rand(hash2)) * (1.0f / FLOAT_MAX);\n" - " part.cz = float(rand(hash1)) * (1.0f / FLOAT_MAX);\n" + " part.cx = float(rand(hash1 ^ hash2)) * FLOAT_FACTOR;\n" + " part.cy = float(rand(hash2 ^ hash3)) * FLOAT_FACTOR;\n" + " part.cz = float(rand(hash3 ^ hash1)) * FLOAT_FACTOR;\n" " }\n" " else\n" " {\n" @@ -105,10 +111,6 @@ int main() " part.py += part.vy * dt;\n" " part.pz += part.vz * dt;\n" "\n" - " part.cx = float(rand(hash1)) * (1.0f / FLOAT_MAX);\n" - " part.cy = float(rand(hash1)) * (1.0f / FLOAT_MAX);\n" - " part.cz = float(rand(hash1)) * (1.0f / FLOAT_MAX);\n" - "\n" " part.age -= 0.01f;\n" " }\n" "\n" diff --git a/shaders/opengl/ComputeShader.glsl b/shaders/opengl/ComputeShader.glsl index 5ae8169..57ac16d 100644 --- a/shaders/opengl/ComputeShader.glsl +++ b/shaders/opengl/ComputeShader.glsl @@ -1,6 +1,7 @@ #version 460 #define FLOAT_MAX 4294967296.0f +#define FLOAT_FACTOR 0.00000000023283064365386962890625f struct particle { @@ -34,13 +35,17 @@ uint hash(uvec3 seed) uint rand(uint seed) { - // Xorshift algorithm from George Marsaglia's paper seed ^= (seed << 13u); seed ^= (seed >> 17u); seed ^= (seed << 5u); return seed; } +int foreSign(uint seed) +{ + return rand(seed) % 2 == 0 ? 1 : -1; +} + void main() { uint gid = gl_GlobalInvocationID.x; @@ -61,13 +66,13 @@ void main() part.age = rand(hash(uvec3(hash1, hash2, hash3))) % (250 - 60 + 1) + 60; - part.vx = (rand(hash1) % 2 == 0 ? 1 : -1) * float(rand(hash2)) * (1.0f / FLOAT_MAX); - part.vy = (rand(hash3) % 2 == 0 ? -1 : 1) * float(rand(hash1)) * (1.0f / FLOAT_MAX); - part.vz = (rand(hash2) % 2 == 0 ? 1 : -1) * float(rand(hash3)) * (1.0f / FLOAT_MAX); + part.vx = foreSign(hash1) * float(rand(hash2)) * FLOAT_FACTOR; + part.vy = foreSign(hash3) * float(rand(hash1)) * FLOAT_FACTOR; + part.vz = foreSign(hash2) * float(rand(hash3)) * FLOAT_FACTOR; - part.cx = float(rand(hash1)) * (1.0f / FLOAT_MAX); - part.cy = float(rand(hash1)) * (1.0f / FLOAT_MAX); - part.cz = float(rand(hash1)) * (1.0f / FLOAT_MAX); + part.cx = float(rand(hash1 ^ hash2)) * FLOAT_FACTOR; + part.cy = float(rand(hash2 ^ hash3)) * FLOAT_FACTOR; + part.cz = float(rand(hash3 ^ hash1)) * FLOAT_FACTOR; } else { @@ -75,10 +80,6 @@ void main() part.py += part.vy * dt; part.pz += part.vz * dt; - part.cx = float(rand(hash1)) * (1.0f / FLOAT_MAX); - part.cy = float(rand(hash1)) * (1.0f / FLOAT_MAX); - part.cz = float(rand(hash1)) * (1.0f / FLOAT_MAX); - part.age -= 0.01f; } diff --git a/utils.c b/utils.c new file mode 100644 index 0000000..56caa05 --- /dev/null +++ b/utils.c @@ -0,0 +1,27 @@ +#include +#include +#include "utils.h" + +#define BUFFER_SIZE 1024 + +char *readFile(char *path) +{ + FILE *file = fopen(path, "r"); + char *str = malloc(BUFFER_SIZE); + int c, i = 0, j = 1; + + while ((c = fgetc(file)) != EOF) + { + if (i == j * BUFFER_SIZE) + { + str = realloc(str, ++j * BUFFER_SIZE); + } + + str[i++] = (char) c; + } + + fclose(file); + + return str; +} + diff --git a/def.h b/utils.h similarity index 55% rename from def.h rename to utils.h index 953f25a..1941e85 100644 --- a/def.h +++ b/utils.h @@ -2,4 +2,6 @@ #define HEIGHT 800 #define UPPER_AGE 250 -#define LOWER_AGE 60 \ No newline at end of file +#define LOWER_AGE 60 + +char *readFile(char *path); \ No newline at end of file diff --git a/vulkanMain.c b/vulkanMain.c index 7bc4e1f..fd303e8 100644 --- a/vulkanMain.c +++ b/vulkanMain.c @@ -8,7 +8,7 @@ int main() VkDevice device; VkSurfaceKHR surface; VkSwapchainKHR swapChain; - VkImageView *imageViews; + VkImageView *imageViews = NULL; uint32_t amountImages; // GLFW @@ -18,7 +18,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, &amountImages)) // Render Loop while (!glfwWindowShouldClose(window))