diff options
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | b.c | 22 | ||||
-rw-r--r-- | src/cplusplus.cpp | 1 | ||||
-rw-r--r-- | src/render.c | 89 | ||||
-rw-r--r-- | src/shader.frag | 52 | ||||
-rw-r--r-- | src/shader.vert | 2 | ||||
-rw-r--r-- | src/state.h | 19 | ||||
-rw-r--r-- | src/vksetup.h | 21 | ||||
-rw-r--r-- | src/vkutil.h | 58 |
9 files changed, 113 insertions, 154 deletions
@@ -7,6 +7,7 @@ synth # backups *~ +.gdb_history imgui.ini b -build/
\ No newline at end of file +build/ @@ -210,9 +210,29 @@ build_objects(const char* lang, return true; } +#include <time.h> +float +current_time() +{ + static struct timespec startTime; + static int isStartTimeInitialized = 0; + + if (!isStartTimeInitialized) { + clock_gettime(CLOCK_MONOTONIC, &startTime); + isStartTimeInitialized = 1; + } + + struct timespec currentTime; + clock_gettime(CLOCK_MONOTONIC, ¤tTime); + + return (currentTime.tv_sec - startTime.tv_sec) + + (currentTime.tv_nsec - startTime.tv_nsec) / 1e9f; +} + int main(int argc, char *argv[]) { + float before_build = current_time(); B_GO_REBUILD_URSELF(argc, argv); const char *program_name = b_shift_args(&argc, &argv); @@ -275,6 +295,8 @@ main(int argc, char *argv[]) if (!build_objects("C++", "src/cplusplus.cpp", cplusplus_deps, B_ARRAY_LEN(cplusplus_deps), objects, &it)) return 1; if (!build_c(force, &cmd, render_paths, B_ARRAY_LEN(render_paths), render_deps, B_ARRAY_LEN(render_deps), objects, B_ARRAY_LEN(objects), BUILD_DIR"render")) return 1; + b_log(B_INFO, "Build time: %.3f seconds", current_time() - before_build); + return 0; } /* diff --git a/src/cplusplus.cpp b/src/cplusplus.cpp index 68cb8c3..b8dd787 100644 --- a/src/cplusplus.cpp +++ b/src/cplusplus.cpp @@ -56,6 +56,7 @@ imgui_proc_event(SDL_Event* e) } static VkDescriptorPool imguiPool; + void init_imgui(vks_context *vk) { diff --git a/src/render.c b/src/render.c index f625dba..5e9202b 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()); @@ -413,10 +395,10 @@ vulkan_create_graphics_pipeline(VkPolygonMode polygon_mode) long vert_size = shaderc_result_get_length(vert_result); const char * frag_data = shaderc_result_get_bytes(frag_result); long frag_size = shaderc_result_get_length(frag_result); - vk_log(VK_INFO, "Shaders loaded\n"); VkShaderModule vertShaderModule = createShaderModule(vert_data, vert_size); VkShaderModule fragShaderModule = createShaderModule(frag_data, frag_size); + vk_log(VK_INFO, "Shaders loaded\n"); VkPipelineShaderStageCreateInfo vertShaderStageInfo = {0}; vertShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; @@ -636,6 +618,19 @@ recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex) // TODO Make polygon mode dynamic //vkCmdSetPolygonModeEXT(commandBuffer, s.polygon_mode); + + + /* vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, s.vk_compute_pipeline); */ + /* vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, s.vk_compute_pipeline_layout, 0, 1, &s.frames[imageIndex].vk_compute_descriptor_set, 0, 0); */ + + /* vkCmdDispatch(commandBuffer, PARTICLE_COUNT / 256, 1, 1); */ + + + + + + + vks_transition_image_layout_info transition_info = { 0 }; transition_info.image = s.vk.swapchain.images[imageIndex]; @@ -710,7 +705,7 @@ recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex) vkCmdDrawIndexed(commandBuffer, s.indices_count, 1, 0, 0, 0); - imgui_draw(); // does this need to be here? + imgui_draw(); imgui_draw_cmd(commandBuffer); vkCmdEndRendering(commandBuffer); @@ -880,9 +875,11 @@ void vulkan_create_descriptor_sets() { VkDescriptorSetLayout layouts[MAX_FRAMES_IN_FLIGHT] = {0}; + /* VkDescriptorSetLayout compute_layouts[MAX_FRAMES_IN_FLIGHT] = {0}; */ for (int i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) { layouts[i] = s.vk_descriptor_set_layout; + /* compute_layouts[i] = s.vk_compute_descriptor_set_layout; */ } // TODO Find a way to group allocation @@ -894,15 +891,23 @@ vulkan_create_descriptor_sets() allocInfo.pSetLayouts = layouts; VK_CHECK(vkAllocateDescriptorSets(s.vk.device, &allocInfo, &s.frames[i].vk_descriptor_set)); + + /* allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; */ + /* allocInfo.descriptorPool = s.vk_descriptor_pool; */ + /* allocInfo.descriptorSetCount = 1; */ + /* allocInfo.pSetLayouts = compute_layouts; */ + + /* VK_CHECK(vkAllocateDescriptorSets(s.vk.device, &allocInfo, &s.frames[i].vk_compute_descriptor_set)); */ } for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) { + VkWriteDescriptorSet descriptorWrites[2] = {0}; + VkDescriptorBufferInfo bufferInfo = {0}; bufferInfo.buffer = s.frames[i].uniform_buffer.handle; bufferInfo.offset = 0; bufferInfo.range = sizeof(UniformBufferObject); - VkWriteDescriptorSet descriptorWrites[2] = {0}; descriptorWrites[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; descriptorWrites[0].dstSet = s.frames[i].vk_descriptor_set; descriptorWrites[0].dstBinding = 0; @@ -925,7 +930,7 @@ vulkan_create_descriptor_sets() descriptorWrites[1].pImageInfo = &imageInfo; /* VkDescriptorBufferInfo storageBufferInfoLastFrame = {0}; */ - /* storageBufferInfoLastFrame.buffer = s.frames[(i - 1) % MAX_FRAMES_IN_FLIGHT].vk_shader_storage_buffer; */ + /* storageBufferInfoLastFrame.buffer = s.frames[(i - 1) % MAX_FRAMES_IN_FLIGHT].shader_storage_buffer.handle; */ /* storageBufferInfoLastFrame.offset = 0; */ /* storageBufferInfoLastFrame.range = sizeof(Particle) * PARTICLE_COUNT; */ @@ -938,7 +943,7 @@ vulkan_create_descriptor_sets() /* descriptorWrites[2].pBufferInfo = &storageBufferInfoLastFrame; */ /* VkDescriptorBufferInfo storageBufferInfoCurrentFrame = {0}; */ - /* storageBufferInfoCurrentFrame.buffer = s.frames[i].vk_shader_storage_buffer; */ + /* storageBufferInfoCurrentFrame.buffer = s.frames[i].shader_storage_buffer.handle; */ /* storageBufferInfoCurrentFrame.offset = 0; */ /* storageBufferInfoCurrentFrame.range = sizeof(Particle) * PARTICLE_COUNT; */ @@ -950,7 +955,6 @@ vulkan_create_descriptor_sets() /* descriptorWrites[3].descriptorCount = 1; */ /* descriptorWrites[3].pBufferInfo = &storageBufferInfoCurrentFrame; */ - vkUpdateDescriptorSets(s.vk.device, VK_ARRAY_LEN(descriptorWrites), descriptorWrites, 0, NULL); } } @@ -1288,7 +1292,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 +1370,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 +1403,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 +1428,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); - // 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_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); } VkDescriptorSetLayoutBinding layoutBindings[3]; @@ -1452,23 +1456,20 @@ void vulkan_create_compute_stuff() layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; layoutInfo.bindingCount = 3; layoutInfo.pBindings = layoutBindings; - VK_CHECK(vkCreateDescriptorSetLayout(s.vk.device, &layoutInfo, NULL, &s.vk_compute_descriptor_set_layout)); - VkComputePipelineCreateInfo pipelineInfo = {0}; - pipelineInfo.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO; - pipelineInfo.layout = s.vk_compute_pipeline_layout; - pipelineInfo.stage = computeShaderStageInfo; - - VK_CHECK(vkCreateComputePipelines(s.vk.device, VK_NULL_HANDLE, 1, &pipelineInfo, NULL, &s.vk_compute_pipeline)); - VkPipelineLayoutCreateInfo pipelineLayoutInfo = {0}; pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; pipelineLayoutInfo.setLayoutCount = 1; pipelineLayoutInfo.pSetLayouts = &s.vk_compute_descriptor_set_layout; - VK_CHECK(vkCreatePipelineLayout(s.vk.device, &pipelineLayoutInfo, NULL, &s.vk_compute_pipeline_layout)); + VkComputePipelineCreateInfo pipelineInfo = {0}; + pipelineInfo.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO; + pipelineInfo.layout = s.vk_compute_pipeline_layout; + pipelineInfo.stage = computeShaderStageInfo; + VK_CHECK(vkCreateComputePipelines(s.vk.device, VK_NULL_HANDLE, 1, &pipelineInfo, NULL, &s.vk_compute_pipeline)); + if (s.prev_comp_result != comp_result) { shaderc_result_release(comp_result); } @@ -1488,15 +1489,15 @@ init_vulkan() vulkan_create_descriptor_set_layout(); vulkan_create_graphics_pipeline(s.polygon_mode); - //vulkan_create_compute_stuff(); + vulkan_create_compute_stuff(); vulkan_create_texture_image(); vulkan_create_texture_sampler(); init_imgui(&s.vk); - load_model_obj(); - //load_model_gltf(); + //load_model_obj(); + load_model_gltf(); vulkan_create_vertex_buffer(); vulkan_create_index_buffer(); @@ -1717,7 +1718,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..6f5abb6 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 * 20) * .5 + .5; + outColor = vec4(fragColor * texture(texSampler, fragTexCoord).rgb , 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 @@ -34,22 +34,6 @@ typedef struct { } 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; } V2; @@ -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..923083b 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 }; @@ -1331,7 +1335,12 @@ vks_create_buffer(const vks_context vk, allocInfo.usage = VMA_MEMORY_USAGE_AUTO; allocInfo.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT; - vmaCreateBuffer(vk.allocator, &bufferInfo, &allocInfo, &buffer->handle, &buffer->allocation, NULL); + VK_CHECK(vmaCreateBuffer(vk.allocator, &bufferInfo, &allocInfo, &buffer->handle, &buffer->allocation, NULL)); + + VmaAllocationInfo info = {0}; + + vmaGetAllocationInfo(vk.allocator, buffer->allocation, &info); + buffer->memory = info.deviceMemory; } #endif /* VKSETUP_IMPLEMENTATION */ diff --git a/src/vkutil.h b/src/vkutil.h deleted file mode 100644 index 123b97a..0000000 --- a/src/vkutil.h +++ /dev/null @@ -1,58 +0,0 @@ -#ifndef _VKUTIL_H -#define _VKUTIL_H - -/** - * vkutil.h -- helper functions and macros for working with vulkan in C - */ - -#include <stdio.h> -#include <stdarg.h> - -#include <vulkan/vulkan.h> -#include <vulkan/vk_enum_string_helper.h> // for string_VkResult - -#define VK_ARRAY_LEN(arr) sizeof((arr))/sizeof((arr)[0]) - -#define VK_CHECK(x) \ -do { \ - VkResult err = x; \ - if (err) { \ - fprintf(stderr, "%s:%d:0: vulkan error: %s \n", \ - __FILE__, __LINE__, string_VkResult(err)); \ - abort(); \ - } \ -} while (0) - -typedef enum { - VK_INFO = 0, - VK_WARN, - VK_ERROR, -} log_type; - -static inline void -vk_log(log_type t, const char * f, ...) -{ -#ifdef VKDEBUG - va_list args; - va_start(args, f); - switch (t) { - case VK_INFO: - printf("INFO: "); - vprintf(f, args); - break; - case VK_WARN: - fprintf(stderr, "WARN: "); - vfprintf(stderr, f, args); - break; - case VK_ERROR: - fprintf(stderr, "ERROR: "); - vfprintf(stderr, f, args); - break; - } - va_end(args); -#else - return; -#endif -} - -#endif /* _VKUTIL_H */ |