diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/game.c | 41 | ||||
-rw-r--r-- | src/shader.frag | 32 | ||||
-rw-r--r-- | src/shader.vert | 5 | ||||
-rw-r--r-- | src/state.h | 12 | ||||
-rw-r--r-- | src/vkutil.h | 2 |
6 files changed, 74 insertions, 20 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 7ef364b..bf53524 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -21,6 +21,6 @@ AM_CFLAGS = -march=native -fno-math-errno -funroll-loops -flto -pthread -ggdb -D AM_CXXFLAGS = -Wall -Wextra -O2 -g -std=c++17 game_SOURCES = game.c cplusplus.cpp $(common_sources) -game_LDADD = -lSDL2 -lm -lSDL2_image -lvulkan -lshaderc_shared -lstdc++ +game_LDADD = -lSDL2 -lm -lvulkan -lshaderc_shared -lstdc++ cplusplus.$(OBJEXT) : CXXFLAGS += -Wno-nullability-completeness -Wno-unused-function -Wno-missing-field-initializers -Wno-unused-parameter -Wno-unused-variable @@ -1,4 +1,3 @@ -#include <limits.h> #include <stdbool.h> #include <stdlib.h> #include <time.h> @@ -8,7 +7,6 @@ #define SDL_MAIN_HANDLED #define VK_USE_PLATFORM_XCB_KHR #include <SDL2/SDL.h> -#include <SDL2/SDL_image.h> #include <SDL2/SDL_vulkan.h> #include <vulkan/vulkan.h> @@ -23,9 +21,6 @@ #include "state.h" // embedded clgm library -#define CGLM_FORCE_DEPTH_ZERO_TO_ONE -#include "../lib/cglm/include/cglm/cglm.h" - uint32_t currentFrame = 0; state_t s; @@ -727,11 +722,21 @@ vulkan_create_graphics_pipeline() shaderc_compilation_result_t vert_result = load_compile_shader_data("src/shader.vert", shaderc_vertex_shader); if (!vert_result) { vk_log(VK_ERROR, "Can't load vertex shader\n"); + if (s.prev_vert_result) { + vert_result = s.prev_vert_result; + } } + if (s.prev_vert_result && vert_result != s.prev_vert_result) shaderc_result_release(s.prev_vert_result); + s.prev_vert_result = vert_result; shaderc_compilation_result_t frag_result = load_compile_shader_data("src/shader.frag", shaderc_fragment_shader); if (!frag_result) { vk_log(VK_ERROR, "Can't load fragment shader\n"); + if (s.prev_frag_result) { + frag_result = s.prev_frag_result; + } } + if (s.prev_frag_result && frag_result != s.prev_frag_result) shaderc_result_release(s.prev_frag_result); + s.prev_frag_result = frag_result; const char * vert_data = shaderc_result_get_bytes(vert_result); long vert_size = shaderc_result_get_length(vert_result); @@ -857,7 +862,7 @@ vulkan_create_graphics_pipeline() VkPipelineColorBlendStateCreateInfo colorBlending = {0}; colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; - colorBlending.logicOpEnable = VK_FALSE; + colorBlending.logicOpEnable = VK_TRUE; colorBlending.logicOp = VK_LOGIC_OP_COPY; // Optional colorBlending.attachmentCount = 1; colorBlending.pAttachments = &colorBlendAttachment; @@ -900,8 +905,12 @@ vulkan_create_graphics_pipeline() vk_log(VK_ERROR, "failed to create graphics pipeline!\n"); } - shaderc_result_release(vert_result); - shaderc_result_release(frag_result); + if (s.prev_vert_result != vert_result) { + shaderc_result_release(vert_result); + } + if (s.prev_frag_result != frag_result) { + shaderc_result_release(frag_result); + } vkDestroyShaderModule(s.vk_device, fragShaderModule, NULL); vkDestroyShaderModule(s.vk_device, vertShaderModule, NULL); @@ -1233,7 +1242,8 @@ vulkan_create_descriptor_set_layout() uboLayoutBinding.binding = 0; uboLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; uboLayoutBinding.descriptorCount = 1; - uboLayoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT; + uboLayoutBinding.stageFlags = VK_SHADER_STAGE_ALL; + //uboLayoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT; uboLayoutBinding.pImmutableSamplers = NULL; // optional VkDescriptorSetLayoutCreateInfo layoutInfo = {0}; @@ -1479,6 +1489,13 @@ close_vulkan() /* DestroyDebugUtilsMessengerEXT(s.vk_instance, s.vk_debug_messenger, NULL); */ /* } */ vkDestroyInstance(s.vk_instance, NULL); + + if (s.prev_vert_result) { + shaderc_result_release(s.prev_vert_result); + } + if (s.prev_frag_result) { + shaderc_result_release(s.prev_frag_result); + } } float @@ -1504,6 +1521,10 @@ updateUniformBuffer(uint32_t currentImage) float time = current_time(); UniformBufferObject ubo = {0}; + + ubo.resolution[0] = s.vk_swap_chain_extent.width; + ubo.resolution[1] = s.vk_swap_chain_extent.height; + glm_mat4_identity(ubo.model); if (s.rotate) @@ -1627,7 +1648,7 @@ main(int argc, char* args[]) while (!quit) { handle_input(&quit); draw_frame(); - //SDL_Delay(16); + SDL_Delay(16); } close_vulkan(); diff --git a/src/shader.frag b/src/shader.frag index cad317c..4702e65 100644 --- a/src/shader.frag +++ b/src/shader.frag @@ -5,8 +5,30 @@ layout(location = 1) in vec2 fragPos; layout(location = 0) out vec4 outColor; +layout(binding = 0) uniform UniformBufferObject { + mat4 model; + mat4 view; + mat4 proj; + vec2 resolution; +} ubo; + +// vec4 ubo_proj(vec2 v) { +// return ubo.proj * ubo.view * ubo.model * vec4(v, 0.0, 1.0); +// } + // void main() { -// outColor = vec4(fragColor, 1.0); +// // float x = gl_FragCoord.x / ubo.resolution.x; +// // float y = gl_FragCoord.y / ubo.resolution.y; +// float x = ubo_proj(vec2(gl_FragCoord.x, gl_FragCoord.y)).x / ubo.resolution.x; +// float y = ubo_proj(vec2(gl_FragCoord.x, gl_FragCoord.y)).y / ubo.resolution.y; +// //bool colored = mod(gl_FragCoord.x, 10) > 9 || mod(gl_FragCoord.y, 10) > 9; +// bool colored = mod(x,0.1) > 0.09 || mod(y, 0.1) > 0.09; +// if (colored) { +// outColor = vec4(gl_FragCoord.x / ubo.resolution.x, gl_FragCoord.y / ubo.resolution.y, 1.0, 1.0); +// } else { +// outColor = vec4(0,0,0, 1.0); +// } +// //outColor = vec4(fragColor, 1.0); // } vec3 mandel(vec2 z0) { float k = 0.0; @@ -20,13 +42,13 @@ vec3 mandel(vec2 z0) { return sin(mu*0.1 + vec3(0.0,0.5,1.0)); } void main() { - float ar = fragPos.x / fragPos.y; - vec2 uv = gl_FragCoord.xy / fragPos.yy - vec2(0.66 * ar, 0.5); - //uv = uv * 2.0 + vec2(-0.3, 1.0); + float ar = ubo.resolution.x / ubo.resolution.y; + vec2 uv = gl_FragCoord.xy / ubo.resolution.yy - vec2(0.66 * ar, 0.5); + // uv = uv * 2.0 + vec2(-0.3, 0.0); float p = 30.0; float t = mod(13.0, p); if (t > p/2.0) t = p - t; - float scale = 0.5 + pow(4.0, t); + float scale = 0.5 + pow(2.0, t); vec2 offset = vec2(-1.36799, .01); uv += offset*scale; uv /= scale; diff --git a/src/shader.vert b/src/shader.vert index 76de53c..f3355d1 100644 --- a/src/shader.vert +++ b/src/shader.vert @@ -4,6 +4,7 @@ layout(binding = 0) uniform UniformBufferObject { mat4 model; mat4 view; mat4 proj; + vec2 resolution; } ubo; layout(location = 0) in vec2 inPosition; @@ -13,8 +14,8 @@ layout(location = 0) out vec3 fragColor; layout(location = 1) out vec2 fragPos; void main() { - gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 0.0, 1.0); - //gl_Position = vec4(inPosition * 1.5, 1.0, 1.0); + //gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition * 2, 0.0, 1.0); + gl_Position = vec4(inPosition * 2, 0.0, 1.0); fragColor = inColor; fragPos = inPosition; } diff --git a/src/state.h b/src/state.h index 3890694..17be1c0 100644 --- a/src/state.h +++ b/src/state.h @@ -1,7 +1,10 @@ #include <SDL2/SDL.h> +#include <vulkan/vulkan.h> +#include <shaderc/shaderc.h> + +#define CGLM_FORCE_DEPTH_ZERO_TO_ONE #include "../lib/cglm/include/cglm/cglm.h" -#include <vulkan/vulkan.h> #pragma once @@ -22,6 +25,7 @@ typedef struct { mat4 model; mat4 view; mat4 proj; + vec2 resolution; } UniformBufferObject; typedef struct { @@ -98,6 +102,9 @@ typedef struct state { VkDeviceMemory vk_index_buffer_memory; VkDescriptorPool vk_descriptor_pool; + + shaderc_compilation_result_t prev_vert_result; + shaderc_compilation_result_t prev_frag_result; } state_t ; static void @@ -144,4 +151,7 @@ init_state(state_t * s) s->vk_physical_device = VK_NULL_HANDLE; s->vk_swap_chain_framebuffers_count = 0; + + s->prev_vert_result = NULL; + s->prev_frag_result = NULL; } diff --git a/src/vkutil.h b/src/vkutil.h index 9188a07..123b97a 100644 --- a/src/vkutil.h +++ b/src/vkutil.h @@ -29,7 +29,7 @@ typedef enum { VK_ERROR, } log_type; -void +static inline void vk_log(log_type t, const char * f, ...) { #ifdef VKDEBUG |