From af57524fb3d6914ab413023af4b197d2eb8e6894 Mon Sep 17 00:00:00 2001
From: grm <grm@eyesin.space>
Date: Mon, 16 Dec 2024 11:16:24 +0200
Subject: smol progress

---
 src/render.c    | 32 +++++++-------------------------
 src/shader.frag | 52 ++++++++++++++++++++++++++--------------------------
 src/shader.vert |  2 +-
 src/state.h     | 19 +------------------
 src/vksetup.h   | 14 +++++++++-----
 src/vkutil.h    |  1 +
 6 files changed, 45 insertions(+), 75 deletions(-)

diff --git a/src/render.c b/src/render.c
index f625dba..6ff1f9e 100644
--- a/src/render.c
+++ b/src/render.c
@@ -3,20 +3,6 @@
 #include <stdlib.h>
 #include <time.h>
 
-#include <shaderc/shaderc.h>
-#include <vulkan/vulkan_core.h>
-
-#define SDL_MAIN_HANDLED
-#define VK_USE_PLATFORM_XCB_KHR
-#include <SDL2/SDL.h>
-#include <SDL2/SDL_vulkan.h>
-
-#include <vulkan/vulkan.h>
-
-/* #define VMA_STATIC_VULKAN_FUNCTIONS 0 */
-/* #define VMA_DYNAMIC_VULKAN_FUNCTIONS 1 */
-/* #include "vk_mem_alloc.h" */
-
 #define STB_IMAGE_IMPLEMENTATION
 #include <stb_image.h>
 
@@ -30,10 +16,6 @@
 //#include "vkutil.h"
 #include "state.h"
 
-#define VMA_STATIC_VULKAN_FUNCTIONS 0
-#define VMA_DYNAMIC_VULKAN_FUNCTIONS 1
-#include "vk_mem_alloc.h"
-
 #define MODEL_PATH "assets/human.obj"
 #define TEXTURE_PATH "assets/viking_room.png"
 
@@ -127,7 +109,7 @@ move_towards(vec3 position, vec3 front, float step)
 }
 
 bool
-init()
+sdl_init()
 {
   if (SDL_Init(SDL_INIT_VIDEO) < 0) {
     vk_log(VK_INFO, "SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
@@ -1288,7 +1270,7 @@ load_model_obj()
 void load_model_gltf() {
   // TODO maybe copy the raylib implemenetation
 
-  char * path = "assets/monkey.glb";
+  char * path = "assets/bugati.glb";
   cgltf_options options;
   memset(&options, 0, sizeof(cgltf_options));
   cgltf_data* data = NULL;
@@ -1366,7 +1348,7 @@ void vulkan_create_compute_stuff()
 {
   shaderc_compilation_result_t comp_result = load_compile_shader_data("src/shader.comp", shaderc_compute_shader);
   if (!comp_result) {
-    vk_log(VK_ERROR, "Can't load compupte shader\n");
+    vk_log(VK_ERROR, "Can't load compute shader\n");
     if (s.prev_comp_result) {
       comp_result = s.prev_comp_result;
     }
@@ -1399,7 +1381,7 @@ void vulkan_create_compute_stuff()
 
     particles[i].position[0] = x;
     particles[i].position[1] = y;
-
+    
     vec2 norm;
     glm_vec2_copy((vec2){x, y}, norm);
     glm_vec2_normalize(norm);
@@ -1424,9 +1406,9 @@ 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, &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);
+    //    vks_copy_buffer(s.vk, stagingBuffer.handle, s.frames[i].shader_storage_buffer.handle, bufferSize);
   }
 
   VkDescriptorSetLayoutBinding layoutBindings[3];
@@ -1717,7 +1699,7 @@ main(int argc, char* argv[])
   phash_destroy(&tbl);
 
   init_state(&s);
-  if (!init()) {
+  if (!sdl_init()) {
     vk_log(VK_INFO, "Failed to initialize!\n");
     abort();
   }
diff --git a/src/shader.frag b/src/shader.frag
index ca895d2..88015c4 100644
--- a/src/shader.frag
+++ b/src/shader.frag
@@ -16,20 +16,20 @@ layout(binding = 0) uniform UniformBufferObject {
   float dt;
 } ubo;
 
-// void main() {
-//   // float pulse = sin(ubo.time * .2) * .5 + .5;
-//   // outColor = vec4(fragColor * texture(texSampler, fragTexCoord).rgb * pulse, 1.0);
-//   //outColor = texture(texSampler, fragTexCoord);
-//   outColor = vec4(fragColor, 1);
-// //   float repeat = 10;
-// //   float f = mod(ubo.time, repeat);
-// //   if (f < repeat / 2) {
-// //     outColor = vec4(fragColor * radians(f) ,1);
-// //   } else {
-// //     outColor = vec4(fragColor * radians(1 - f) ,1);
-// //   }
-// //   //outColor = (vec4(1) * ubo.model + vec4(1)) * vec4(texture(texSampler, fragTexCoord).rgb * fragColor, 1.0);
-// }
+void main() {
+  // float pulse = sin(ubo.time * .2) * .5 + .5;
+  // outColor = vec4(fragColor * texture(texSampler, fragTexCoord).rgb * pulse, 1.0);
+  outColor = texture(texSampler, fragTexCoord);
+  //outColor = vec4(fragColor, 1);
+//   float repeat = 10;
+//   float f = zmod(ubo.time, repeat);
+//   if (f < repeat / 2) {
+//     outColor = vec4(fragColor * radians(f) ,1);
+//   } else {
+//     outColor = vec4(fragColor * radians(1 - f) ,1);
+//   }
+//   //outColor = (vec4(1) * ubo.model + vec4(1)) * vec4(texture(texSampler, fragTexCoord).rgb * fragColor, 1.0);
+}
 
 float noise(vec2 p) {
     return fract(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453);
@@ -54,18 +54,18 @@ float fbm(vec2 p) {
     return value;
 }
 
-void main() {
-  vec2 uv = gl_FragCoord.xy / ubo.resolution.xy;
-  vec2 p = uv * 2.0 - vec2(1.0);
-  float n = fbm(p * 5.0 - vec2(0.0, ubo.time * 2.0));
-  // outColor = vec4(n, n * 0.5, 0.0, 1.0) * vec4(texture(texSampler, fragTexCoord).rgb, 1.0);
-  if (mod(gl_FragCoord.x, 20) > 19 || mod(gl_FragCoord.y, 20) > 19) {
-    outColor = vec4(0,0,0, 1);
-  } else {
-    float x = clamp(sin(ubo.time * 0.1), 0.0, 1.0);
-    outColor = vec4(x, n * .2, n * 0.5, 1.0);
-  }
-}
+// void main() {
+//   vec2 uv = gl_FragCoord.xy / ubo.resolution.xy;
+//   vec2 p = uv * 2.0 - vec2(1.0);
+//   float n = fbm(p * 5.0 - vec2(0.0, ubo.time * 2.0));
+//   // outColor = vec4(n, n * 0.5, 0.0, 1.0) * vec4(texture(texSampler, fragTexCoord).rgb, 1.0);
+//   // if (mod(gl_FragCoord.x, 20) > 19 || mod(gl_FragCoord.y, 20) > 19) {
+//   //   outColor = vec4(0,0,0, 1);
+//   // } else {
+//     float x = clamp(sin(ubo.time * 0.1), 0.0, 1.0);
+//     outColor = vec4(x, n * .2, n * 0.5, 1.0);
+// //  }
+// }
 
 // void main() {
 
diff --git a/src/shader.vert b/src/shader.vert
index 182bd51..58f8712 100644
--- a/src/shader.vert
+++ b/src/shader.vert
@@ -17,7 +17,7 @@ layout(location = 0) out vec3 fragColor;
 layout(location = 1) out vec2 fragTexCoord;
 
 void main() {
-  gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 1.0);
+  gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 1);
   //gl_Position = vec4(inPosition * 2, 1.0);
   gl_PointSize = 1.5f;
 
diff --git a/src/state.h b/src/state.h
index 6e78d49..8177c77 100644
--- a/src/state.h
+++ b/src/state.h
@@ -33,22 +33,6 @@ typedef struct {
   float dt;
 } UniformBufferObject;
 
-typedef struct {
-  VkCommandBuffer vk_command_buffer;
-
-  VkSemaphore image_available_semaphore;
-  VkSemaphore render_finished_semaphore;
-  VkFence in_flight_fence;
-
-  vks_buffer uniform_buffer;
-  void * uniform_buffer_mapped;
-
-  VkDescriptorSet vk_descriptor_set;
-
-  VkDescriptorSet vk_compute_descriptor_set;
-  vks_buffer shader_storage_buffer;
-} frame_data;
-
 typedef struct {
   float x;
   float y;
@@ -72,7 +56,6 @@ typedef struct {
   vec4 color;
 } Particle;
 
-
 typedef struct state {
   camera3d camera;
 
@@ -102,7 +85,7 @@ typedef struct state {
   VkDescriptorSetLayout vk_descriptor_set_layout;
   vks_pipeline graphics_pipeline;
 
-  frame_data frames[MAX_FRAMES_IN_FLIGHT];
+  vks_frame_data frames[MAX_FRAMES_IN_FLIGHT];
 
   vks_buffer vertex_buffer;
   vks_buffer index_buffer;
diff --git a/src/vksetup.h b/src/vksetup.h
index 8e14637..daa9148 100644
--- a/src/vksetup.h
+++ b/src/vksetup.h
@@ -15,9 +15,9 @@
   - Using shaderc for compiling glsl
   - Using vulkan(!)
 
-  This is not meant to be a generic werapper around vulkan. It is actively
+  This is not meant to be a generic wrapper around vulkan. It is actively
   paired and chaned by the currently in development application, whatever that
-  might be. Think of it as the vulkan setup configuratior and helper.
+  might be. Think of it as the vulkan setup configurator and helper.
 
   USAGE:
   ~~~~~~
@@ -34,11 +34,10 @@
 
  */
 
-#include <vulkan/vulkan_core.h>
-#define SDL_MAIN_HANDLED
-#define VK_USE_PLATFORM_XCB_KHR
 #include <stdarg.h>
 
+#define SDL_MAIN_HANDLED
+#define VK_USE_PLATFORM_XCB_KHR
 #include <SDL2/SDL.h>
 #include <SDL2/SDL_vulkan.h>
 
@@ -212,6 +211,10 @@ typedef struct vks_frame_data {
   void * uniform_buffer_mapped;
 
   VkDescriptorSet vk_descriptor_set;
+
+  /* compute stuff */
+  /* VkDescriptorSet vk_compute_descriptor_set; */
+  /* vks_buffer shader_storage_buffer; */
 } vks_frame_data;
 
 /* Info structs  */
@@ -1101,6 +1104,7 @@ vks_generate_mipmaps(const vks_context       vk,
                      const int32_t           texHeight,
                      const uint32_t          mipLevels)
 {
+  (void)imageFormat;
   VkCommandBuffer command_buffer = _vks_begin_single_time_commands(vk);
 
   VkImageMemoryBarrier barrier        = { 0 };
diff --git a/src/vkutil.h b/src/vkutil.h
index 123b97a..7252ba8 100644
--- a/src/vkutil.h
+++ b/src/vkutil.h
@@ -2,6 +2,7 @@
 #define _VKUTIL_H
 
 /**
+  * DEPRECATED
   * vkutil.h -- helper functions and macros for working with vulkan in C
   */
 
-- 
cgit v1.2.3