diff --git a/build.sh b/build.sh index f0cb8d3..1df185b 100755 --- a/build.sh +++ b/build.sh @@ -12,7 +12,7 @@ if [ -v vulkan ]; then render_flag="-DSTG_VULKAN_RENDERER"; echo "[v if [ -v opengl ] && [ ! -v render_flag ]; then render_flag="-DSTG_OPENGL_RENDERER"; echo "[opengl renderer]"; fi if [ -v webgl ] && [ ! -v render_flag ]; then render_flag="-DSTG_WEBGL_RENDERER"; echo "[webgl renderer]"; fi if [ -v dx11 ] && [ ! -v render_flag ]; then render_flag="-DSTG_DX11_RENDERER"; echo "[dx11 renderer]"; fi -if [ ! -v render_flag ]; then render_flag="-DSTG_VULKAN_RENDERER"; echo "[default renderer - vulkan]"; fi +if [ ! -v render_flag ]; then render_flag="-DSTG_VULKAN_RENDERER"; vulkan="vulkan"; echo "[default renderer - vulkan]"; fi # for command line build args auto_compile_flags="${render_flag}" @@ -42,7 +42,7 @@ glsl_stage_tesc="-fshader-stage=tesc" glsl_stage_tese="-fshader-stage=tese" glsl_stage_geom="-fshader-stage=geom" glsl_stage_comp="-fshader-stage=comp" -glsl_out="-o/build/shaders/glsl/" +glsl_out="-o./shaders/glsl/" clang_common="${include_flags} -g -Xclang -flto-visibility-public-std -Wno-unknown-warning-option -fdiagnostics-absolute-paths -Wall -Wno-missing-braces -Wno-unused-function -Wno-writable-strings -Wno-unused-value -Wno-unused-variable -Wno-unused-local-typedef -Wno-deprecated-register -Wno-deprecated-declarations -Wno-unused-but-set-variable -Wno-single-bit-bitfield-constant-conversion -Wno-compare-distinct-pointer-types -Wno-initializer-overrides -Wno-incompatible-pointer-types-discards-qualifiers -Wno-for-loop-analysis -DVMA_STATIC_VULKAN_FUNCTIONS=0" @@ -82,35 +82,36 @@ if [ ! -f $vma_obj ]; then fi -mkdir -p ./shaders/glsl -for file in ../src/shaders/glsl/*.glsl; do - base_name=$(basename -- "$file" .glsl) - echo $base_name - echo $file +if [ -v vulkan ]; then + mkdir -p ./shaders/glsl + mkdir -p ../src/file_data + + for file in ../src/shaders/glsl/*.glsl; do + base_name=$(basename -- "$file" .glsl) - case base_name in - *.vert) - glsl_stage="${glsl_stage_vert}" - ;; - *.frag) - glsl_stage="${glsl_stage_vert}" - ;; - *.tesc) - glsl_stage="${glsl_stage_tesc}" - ;; - *.tese) - glsl_stage="${glsl_stage_tese}" - ;; - *.geom) - glsl_stage="${glsl_stage_geom}" - ;; - *.comp) - glsl_stage="${glsl_stage_comp}" - ;; - esac + case "$base_name" in + *.vert) glsl_stage="${glsl_stage_vert}" ;; + *.frag) glsl_stage="${glsl_stage_frag}" ;; + *.tesc) glsl_stage="${glsl_stage_tesc}" ;; + *.tese) glsl_stage="${glsl_stage_tese}" ;; + *.geom) glsl_stage="${glsl_stage_geom}" ;; + *.comp) glsl_stage="${glsl_stage_comp}" ;; + *) continue ;; + esac - $glsl_compile $glsl_flags $glsl_stage file "${glsl_out}${base_name}.spv" -done + $glsl_compile $glsl_flags $glsl_stage $file "${glsl_out}${base_name}.spv" + done + + rm -f ../src/file_data/spv.c + touch ../src/file_data/spv.c + + for file in ./shaders/glsl/*.spv; do + base_name=$(basename -- "$file" .spv) + + xxd -n "shader_${base_name}" -i $file >> ../src/file_data/spv.c + done + +fi $cpp_compiler $vma_compile_flags $vma_source_files $vma_out $vma_obj diff --git a/src/file_data/spv.c b/src/file_data/spv.c new file mode 100644 index 0000000..bd07df7 --- /dev/null +++ b/src/file_data/spv.c @@ -0,0 +1,180 @@ +unsigned char shader_quad_frag[] = { + 0x03, 0x02, 0x23, 0x07, 0x00, 0x06, 0x01, 0x00, 0x0b, 0x00, 0x0d, 0x00, + 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x02, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x47, 0x4c, 0x53, 0x4c, 0x2e, 0x73, 0x74, 0x64, 0x2e, 0x34, 0x35, 0x30, + 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x07, 0x00, 0x04, 0x00, 0x00, 0x00, + 0x04, 0x00, 0x00, 0x00, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x00, 0x00, 0x00, + 0x09, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x10, 0x00, 0x03, 0x00, + 0x04, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x03, 0x00, 0x03, 0x00, + 0x02, 0x00, 0x00, 0x00, 0xcc, 0x01, 0x00, 0x00, 0x04, 0x00, 0x0a, 0x00, + 0x47, 0x4c, 0x5f, 0x47, 0x4f, 0x4f, 0x47, 0x4c, 0x45, 0x5f, 0x63, 0x70, + 0x70, 0x5f, 0x73, 0x74, 0x79, 0x6c, 0x65, 0x5f, 0x6c, 0x69, 0x6e, 0x65, + 0x5f, 0x64, 0x69, 0x72, 0x65, 0x63, 0x74, 0x69, 0x76, 0x65, 0x00, 0x00, + 0x04, 0x00, 0x08, 0x00, 0x47, 0x4c, 0x5f, 0x47, 0x4f, 0x4f, 0x47, 0x4c, + 0x45, 0x5f, 0x69, 0x6e, 0x63, 0x6c, 0x75, 0x64, 0x65, 0x5f, 0x64, 0x69, + 0x72, 0x65, 0x63, 0x74, 0x69, 0x76, 0x65, 0x00, 0x05, 0x00, 0x04, 0x00, + 0x04, 0x00, 0x00, 0x00, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x00, 0x00, 0x00, + 0x05, 0x00, 0x05, 0x00, 0x09, 0x00, 0x00, 0x00, 0x6f, 0x75, 0x74, 0x43, + 0x6f, 0x6c, 0x6f, 0x72, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x00, + 0x0c, 0x00, 0x00, 0x00, 0x66, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, + 0x72, 0x00, 0x00, 0x00, 0x47, 0x00, 0x04, 0x00, 0x09, 0x00, 0x00, 0x00, + 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x47, 0x00, 0x04, 0x00, + 0x0c, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x13, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x21, 0x00, 0x03, 0x00, + 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x16, 0x00, 0x03, 0x00, + 0x06, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x17, 0x00, 0x04, 0x00, + 0x07, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, + 0x20, 0x00, 0x04, 0x00, 0x08, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, + 0x07, 0x00, 0x00, 0x00, 0x3b, 0x00, 0x04, 0x00, 0x08, 0x00, 0x00, 0x00, + 0x09, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x17, 0x00, 0x04, 0x00, + 0x0a, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, + 0x20, 0x00, 0x04, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x0a, 0x00, 0x00, 0x00, 0x3b, 0x00, 0x04, 0x00, 0x0b, 0x00, 0x00, 0x00, + 0x0c, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x04, 0x00, + 0x06, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, + 0x36, 0x00, 0x05, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x02, 0x00, + 0x05, 0x00, 0x00, 0x00, 0x3d, 0x00, 0x04, 0x00, 0x0a, 0x00, 0x00, 0x00, + 0x0d, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x51, 0x00, 0x05, 0x00, + 0x06, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x51, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00, 0x00, + 0x10, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x51, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, + 0x0d, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x50, 0x00, 0x07, 0x00, + 0x07, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, + 0x10, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, + 0x3e, 0x00, 0x03, 0x00, 0x09, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, + 0xfd, 0x00, 0x01, 0x00, 0x38, 0x00, 0x01, 0x00 +}; +unsigned int shader_quad_frag_len = 572; +unsigned char shader_quad_vert[] = { + 0x03, 0x02, 0x23, 0x07, 0x00, 0x06, 0x01, 0x00, 0x0b, 0x00, 0x0d, 0x00, + 0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x02, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x47, 0x4c, 0x53, 0x4c, 0x2e, 0x73, 0x74, 0x64, 0x2e, 0x34, 0x35, 0x30, + 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x04, 0x00, 0x00, 0x00, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x00, 0x00, 0x00, + 0x0c, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00, + 0x26, 0x00, 0x00, 0x00, 0x31, 0x00, 0x00, 0x00, 0x03, 0x00, 0x03, 0x00, + 0x02, 0x00, 0x00, 0x00, 0xcc, 0x01, 0x00, 0x00, 0x04, 0x00, 0x0a, 0x00, + 0x47, 0x4c, 0x5f, 0x47, 0x4f, 0x4f, 0x47, 0x4c, 0x45, 0x5f, 0x63, 0x70, + 0x70, 0x5f, 0x73, 0x74, 0x79, 0x6c, 0x65, 0x5f, 0x6c, 0x69, 0x6e, 0x65, + 0x5f, 0x64, 0x69, 0x72, 0x65, 0x63, 0x74, 0x69, 0x76, 0x65, 0x00, 0x00, + 0x04, 0x00, 0x08, 0x00, 0x47, 0x4c, 0x5f, 0x47, 0x4f, 0x4f, 0x47, 0x4c, + 0x45, 0x5f, 0x69, 0x6e, 0x63, 0x6c, 0x75, 0x64, 0x65, 0x5f, 0x64, 0x69, + 0x72, 0x65, 0x63, 0x74, 0x69, 0x76, 0x65, 0x00, 0x05, 0x00, 0x04, 0x00, + 0x04, 0x00, 0x00, 0x00, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x00, 0x00, 0x00, + 0x05, 0x00, 0x05, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x70, 0x6f, 0x73, 0x69, + 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x00, 0x00, 0x00, 0x05, 0x00, 0x04, 0x00, + 0x17, 0x00, 0x00, 0x00, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x73, 0x00, 0x00, + 0x05, 0x00, 0x06, 0x00, 0x20, 0x00, 0x00, 0x00, 0x67, 0x6c, 0x5f, 0x50, + 0x65, 0x72, 0x56, 0x65, 0x72, 0x74, 0x65, 0x78, 0x00, 0x00, 0x00, 0x00, + 0x06, 0x00, 0x06, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x67, 0x6c, 0x5f, 0x50, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x00, + 0x06, 0x00, 0x07, 0x00, 0x20, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x67, 0x6c, 0x5f, 0x50, 0x6f, 0x69, 0x6e, 0x74, 0x53, 0x69, 0x7a, 0x65, + 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x07, 0x00, 0x20, 0x00, 0x00, 0x00, + 0x02, 0x00, 0x00, 0x00, 0x67, 0x6c, 0x5f, 0x43, 0x6c, 0x69, 0x70, 0x44, + 0x69, 0x73, 0x74, 0x61, 0x6e, 0x63, 0x65, 0x00, 0x06, 0x00, 0x07, 0x00, + 0x20, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x67, 0x6c, 0x5f, 0x43, + 0x75, 0x6c, 0x6c, 0x44, 0x69, 0x73, 0x74, 0x61, 0x6e, 0x63, 0x65, 0x00, + 0x05, 0x00, 0x03, 0x00, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x05, 0x00, 0x06, 0x00, 0x26, 0x00, 0x00, 0x00, 0x67, 0x6c, 0x5f, 0x56, + 0x65, 0x72, 0x74, 0x65, 0x78, 0x49, 0x6e, 0x64, 0x65, 0x78, 0x00, 0x00, + 0x05, 0x00, 0x05, 0x00, 0x31, 0x00, 0x00, 0x00, 0x66, 0x72, 0x61, 0x67, + 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x00, 0x00, 0x00, 0x47, 0x00, 0x03, 0x00, + 0x20, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x48, 0x00, 0x05, 0x00, + 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x48, 0x00, 0x05, 0x00, 0x20, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x48, 0x00, 0x05, 0x00, 0x20, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, + 0x0b, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x48, 0x00, 0x05, 0x00, + 0x20, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, + 0x04, 0x00, 0x00, 0x00, 0x47, 0x00, 0x04, 0x00, 0x26, 0x00, 0x00, 0x00, + 0x0b, 0x00, 0x00, 0x00, 0x2a, 0x00, 0x00, 0x00, 0x47, 0x00, 0x04, 0x00, + 0x31, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x13, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x21, 0x00, 0x03, 0x00, + 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x16, 0x00, 0x03, 0x00, + 0x06, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x17, 0x00, 0x04, 0x00, + 0x07, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, + 0x15, 0x00, 0x04, 0x00, 0x08, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x04, 0x00, 0x08, 0x00, 0x00, 0x00, + 0x09, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x04, 0x00, + 0x0a, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, + 0x20, 0x00, 0x04, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, + 0x0a, 0x00, 0x00, 0x00, 0x3b, 0x00, 0x04, 0x00, 0x0b, 0x00, 0x00, 0x00, + 0x0c, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x04, 0x00, + 0x06, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x2b, 0x00, 0x04, 0x00, 0x06, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0xbf, 0x2c, 0x00, 0x05, 0x00, 0x07, 0x00, 0x00, 0x00, + 0x0f, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, + 0x2b, 0x00, 0x04, 0x00, 0x06, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x3f, 0x2c, 0x00, 0x05, 0x00, 0x07, 0x00, 0x00, 0x00, + 0x11, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, + 0x2c, 0x00, 0x05, 0x00, 0x07, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, + 0x0e, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x06, 0x00, + 0x0a, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, + 0x11, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x17, 0x00, 0x04, 0x00, + 0x14, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, + 0x1c, 0x00, 0x04, 0x00, 0x15, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, + 0x09, 0x00, 0x00, 0x00, 0x20, 0x00, 0x04, 0x00, 0x16, 0x00, 0x00, 0x00, + 0x06, 0x00, 0x00, 0x00, 0x15, 0x00, 0x00, 0x00, 0x3b, 0x00, 0x04, 0x00, + 0x16, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, + 0x2b, 0x00, 0x04, 0x00, 0x06, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x80, 0x3f, 0x2c, 0x00, 0x06, 0x00, 0x14, 0x00, 0x00, 0x00, + 0x19, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, + 0x0d, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x06, 0x00, 0x14, 0x00, 0x00, 0x00, + 0x1a, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, + 0x0d, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x06, 0x00, 0x14, 0x00, 0x00, 0x00, + 0x1b, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, + 0x18, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x06, 0x00, 0x15, 0x00, 0x00, 0x00, + 0x1c, 0x00, 0x00, 0x00, 0x19, 0x00, 0x00, 0x00, 0x1a, 0x00, 0x00, 0x00, + 0x1b, 0x00, 0x00, 0x00, 0x17, 0x00, 0x04, 0x00, 0x1d, 0x00, 0x00, 0x00, + 0x06, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x04, 0x00, + 0x08, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x1c, 0x00, 0x04, 0x00, 0x1f, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, + 0x1e, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x06, 0x00, 0x20, 0x00, 0x00, 0x00, + 0x1d, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x00, + 0x1f, 0x00, 0x00, 0x00, 0x20, 0x00, 0x04, 0x00, 0x21, 0x00, 0x00, 0x00, + 0x03, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x3b, 0x00, 0x04, 0x00, + 0x21, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, + 0x15, 0x00, 0x04, 0x00, 0x23, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x04, 0x00, 0x23, 0x00, 0x00, 0x00, + 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x04, 0x00, + 0x25, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00, + 0x3b, 0x00, 0x04, 0x00, 0x25, 0x00, 0x00, 0x00, 0x26, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x20, 0x00, 0x04, 0x00, 0x28, 0x00, 0x00, 0x00, + 0x06, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x20, 0x00, 0x04, 0x00, + 0x2e, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x1d, 0x00, 0x00, 0x00, + 0x20, 0x00, 0x04, 0x00, 0x30, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, + 0x14, 0x00, 0x00, 0x00, 0x3b, 0x00, 0x04, 0x00, 0x30, 0x00, 0x00, 0x00, + 0x31, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x20, 0x00, 0x04, 0x00, + 0x33, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, + 0x36, 0x00, 0x05, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x02, 0x00, + 0x05, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x03, 0x00, 0x0c, 0x00, 0x00, 0x00, + 0x13, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x03, 0x00, 0x17, 0x00, 0x00, 0x00, + 0x1c, 0x00, 0x00, 0x00, 0x3d, 0x00, 0x04, 0x00, 0x23, 0x00, 0x00, 0x00, + 0x27, 0x00, 0x00, 0x00, 0x26, 0x00, 0x00, 0x00, 0x41, 0x00, 0x05, 0x00, + 0x28, 0x00, 0x00, 0x00, 0x29, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, + 0x27, 0x00, 0x00, 0x00, 0x3d, 0x00, 0x04, 0x00, 0x07, 0x00, 0x00, 0x00, + 0x2a, 0x00, 0x00, 0x00, 0x29, 0x00, 0x00, 0x00, 0x51, 0x00, 0x05, 0x00, + 0x06, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x00, 0x00, 0x2a, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x51, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00, 0x00, + 0x2c, 0x00, 0x00, 0x00, 0x2a, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x50, 0x00, 0x07, 0x00, 0x1d, 0x00, 0x00, 0x00, 0x2d, 0x00, 0x00, 0x00, + 0x2b, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, + 0x18, 0x00, 0x00, 0x00, 0x41, 0x00, 0x05, 0x00, 0x2e, 0x00, 0x00, 0x00, + 0x2f, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, + 0x3e, 0x00, 0x03, 0x00, 0x2f, 0x00, 0x00, 0x00, 0x2d, 0x00, 0x00, 0x00, + 0x3d, 0x00, 0x04, 0x00, 0x23, 0x00, 0x00, 0x00, 0x32, 0x00, 0x00, 0x00, + 0x26, 0x00, 0x00, 0x00, 0x41, 0x00, 0x05, 0x00, 0x33, 0x00, 0x00, 0x00, + 0x34, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, 0x32, 0x00, 0x00, 0x00, + 0x3d, 0x00, 0x04, 0x00, 0x14, 0x00, 0x00, 0x00, 0x35, 0x00, 0x00, 0x00, + 0x34, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x03, 0x00, 0x31, 0x00, 0x00, 0x00, + 0x35, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x01, 0x00, 0x38, 0x00, 0x01, 0x00 +}; +unsigned int shader_quad_vert_len = 1512; diff --git a/src/main.c b/src/main.c index 7185475..9b8ef56 100644 --- a/src/main.c +++ b/src/main.c @@ -35,6 +35,10 @@ int main(int argc, char **argv) break; } } + + BeginFrame(); + DrawTriangle(); + FinishFrame(); } DestroyRenderer(); diff --git a/src/platform_linux.c b/src/platform_linux.c index eb89370..e137a8f 100644 --- a/src/platform_linux.c +++ b/src/platform_linux.c @@ -297,7 +297,7 @@ b32 ChangeWorkingDir(const char *) u8 *OpenFile(const char *) { - u8 *bytes; + u8 *bytes = NULL; return bytes; } diff --git a/src/render.c b/src/render.c index a3af3fa..f86348b 100644 --- a/src/render.c +++ b/src/render.c @@ -22,3 +22,17 @@ void DestroyRenderer() { _DestroyRenderer(); } +b32 BeginFrame() +{ + return _BeginFrame(); +} + +void DrawTriangle() +{ + _DrawTriangle(); +} + +b32 FinishFrame() +{ + return _FinishFrame(); +} diff --git a/src/render.h b/src/render.h index 74c86f5..2cb773f 100644 --- a/src/render.h +++ b/src/render.h @@ -19,3 +19,8 @@ b32 InitRenderer(Arena *arena); void DestroyRenderer(); +// Drawing +static b32 DrawFrame(); +static void DrawTriangle(); +static b32 FinishFrame(); + diff --git a/src/render_vulkan.c b/src/render_vulkan.c index e14eaf2..786b764 100644 --- a/src/render_vulkan.c +++ b/src/render_vulkan.c @@ -15,6 +15,237 @@ void _DestroyRenderer() DestroyVulkan(); } +b32 _BeginFrame() +{ + b32 success = true; + VkResult result; + + u8 f_ix = renderer.frame_state.frame_cnt % FRAME_OVERLAP; + + VkDevice device = renderer.vk.device; + VkCommandBuffer cmd = renderer.vk.frame.buffers[f_ix]; + VkFence fence = renderer.vk.frame.render_fences[f_ix]; + VkSemaphore sc_sem = renderer.vk.frame.swapchain_sems[f_ix]; + VkSemaphore rndr_sem = renderer.vk.frame.render_sems[f_ix]; + VkSwapchainKHR swapchain = renderer.vk.swapchain; + + // TODO(MA): make this work with VK_PRESENT_MODE_MAILBOX_KHR and remove assignment of present mode to FIFO + result = vkWaitForFences(device, 1, &fence, VK_TRUE, 1000000000); + if (result != VK_SUCCESS) + { + Printf("vkWaitForFences failure: %s", VkResultStr(result)); + success = false; + } + + if (success) + { + result = vkAcquireNextImageKHR(device, swapchain, 1000000000, sc_sem, 0, &renderer.frame_state.img_ix); + if (result != VK_SUCCESS) + { + Printf("vkAcquireNextImageKHR failure: %s", VkResultStr(result)); + success = false; + } + } + + if (success) + { + result = vkResetFences(device, 1, &fence); + if (result != VK_SUCCESS) + { + Printf("vkResetFences failure: %s", VkResultStr(result)); + success = false; + } + } + + if (success) + { + result = vkResetCommandBuffer(cmd, 0); + if (result != VK_SUCCESS) + { + Printf("vkResetCommandBuffer failure: %s", VkResultStr(result)); + success = false; + } + } + + if (success) + { + VkCommandBufferBeginInfo cmd_info = { + .sType = STYPE(COMMAND_BUFFER_BEGIN_INFO), + .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT, + }; + + result = vkBeginCommandBuffer(cmd, &cmd_info); + if (result != VK_SUCCESS) + { + Printf("vkBeginCommandBuffer failure: %s", VkResultStr(result)); + success = false; + } + } + + return success; +} + +void _DrawTriangle() +{ + u8 f_ix = renderer.frame_state.frame_cnt % FRAME_OVERLAP; + + VkCommandBuffer cmd = renderer.vk.frame.buffers[f_ix]; + + TransitionImage(cmd, renderer.vk.sc.draw_img.img, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); + TransitionImage(cmd, renderer.vk.sc.depth_img.img, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL); + + VkRenderingAttachmentInfo col_attach_info = { + .sType = STYPE(RENDERING_ATTACHMENT_INFO), + .imageView = renderer.vk.sc.draw_img.view, + .imageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, + .loadOp = VK_ATTACHMENT_LOAD_OP_LOAD, + .storeOp = VK_ATTACHMENT_STORE_OP_STORE, + }; + + VkRenderingAttachmentInfo depth_attach_info = { + .sType = STYPE(RENDERING_ATTACHMENT_INFO), + .imageView = renderer.vk.sc.depth_img.view, + .imageLayout = VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL, + .loadOp = VK_ATTACHMENT_LOAD_OP_LOAD, + .storeOp = VK_ATTACHMENT_STORE_OP_STORE, + }; + + VkRenderingInfo render_info = { + .sType = STYPE(RENDERING_INFO), + .layerCount = 1, + .colorAttachmentCount = 1, + .pColorAttachments = &col_attach_info, + .pDepthAttachment = &depth_attach_info, + .renderArea = { + .extent = { + .width = renderer.vk.sc.extent.width, + .height = renderer.vk.sc.extent.height, + }, + }, + }; + + vkCmdBeginRendering(cmd, &render_info); + + vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, renderer.vk.pipe.pipelines[PIPELINE_CUBE]); + vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, renderer.vk.pipe.pipeline_layout, 0, DESC_TYPE_MAX, renderer.vk.pipe.sets, 0, NULL); + + VkViewport viewport = { + .width = (f32)renderer.vk.sc.extent.width, + .height = (f32)renderer.vk.sc.extent.height, + .maxDepth = 1.0, + }; + + vkCmdSetViewport(cmd, 0, 1, &viewport); + + VkRect2D scissor = { + .extent = { + .width = renderer.vk.sc.extent.width, + .height = renderer.vk.sc.extent.height, + }, + }; + + vkCmdSetScissor(cmd, 0, 1, &scissor); + + vkCmdDrawIndexed(cmd, 3, 1, 0, 0, 0); + + // TODO(MA): maybe store current image layout between functions +} + +b32 _FinishFrame() +{ + b32 success = true; + VkResult result; + + u32 f_ix = renderer.frame_state.frame_cnt % FRAME_OVERLAP; + + VkCommandBuffer cmd = renderer.vk.frame.buffers[f_ix]; + VkSemaphore sc_sem = renderer.vk.frame.swapchain_sems[f_ix]; + VkSemaphore rndr_sem = renderer.vk.frame.render_sems[f_ix]; + VkFence fence = renderer.vk.frame.render_fences[f_ix]; + + vkCmdEndRendering(cmd); + + TransitionImage(cmd, renderer.vk.sc.draw_img.img, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); + TransitionImage(cmd, renderer.vk.sc.imgs[f_ix], VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL); + + VkExtent2D extent = { + .width = renderer.vk.sc.extent.width, + .height = renderer.vk.sc.extent.height, + }; + + CopyImageToImage(cmd, renderer.vk.sc.draw_img.img, renderer.vk.sc.imgs[f_ix], extent, extent); + + TransitionImage(cmd, renderer.vk.sc.imgs[f_ix], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR); + + result = vkEndCommandBuffer(cmd); + if (result != VK_SUCCESS) + { + Printf("vkEndCommandBuffer failure: %s", VkResultStr(result)); + success = false; + } + + if (success) + { + VkCommandBufferSubmitInfo cmd_info = { + .sType = STYPE(COMMAND_BUFFER_SUBMIT_INFO), + .commandBuffer = cmd, + }; + + VkSemaphoreSubmitInfo wait_info = { + .sType = STYPE(SEMAPHORE_SUBMIT_INFO), + .semaphore = sc_sem, + .stageMask = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT, + .value = 1, + }; + + VkSemaphoreSubmitInfo signal_info = { + .sType = STYPE(SEMAPHORE_SUBMIT_INFO), + .semaphore = rndr_sem, + .stageMask = VK_PIPELINE_STAGE_2_ALL_GRAPHICS_BIT, + .value = 1, + }; + + VkSubmitInfo2 submit_info = { + .sType = STYPE(SUBMIT_INFO_2), + .waitSemaphoreInfoCount = 1, + .pWaitSemaphoreInfos = &wait_info, + .signalSemaphoreInfoCount = 1, + .pSignalSemaphoreInfos = &signal_info, + .commandBufferInfoCount = 1, + .pCommandBufferInfos = &cmd_info, + }; + + result = vkQueueSubmit2(renderer.vk.queues.graphics_queue, 1, &submit_info, fence); + if (result != VK_SUCCESS) + { + Printf("vkQueueSubmit2 failure: %s", VkResultStr(result)); + success = false; + } + } + + if (success) + { + VkPresentInfoKHR present_info = { + .sType = STYPE(PRESENT_INFO_KHR), + .pSwapchains = &renderer.vk.swapchain, + .swapchainCount = 1, + .pWaitSemaphores = &rndr_sem, + .waitSemaphoreCount = 1, + .pImageIndices = &f_ix, + }; + + result = vkQueuePresentKHR(renderer.vk.queues.graphics_queue, &present_info); + if (result != VK_SUCCESS) + { + Printf("vkQueuePresentKHR failure: %s", VkResultStr(result)); + success = false; + } + } + + return success; +} + + /** * BACK END API END */ @@ -23,6 +254,79 @@ void _DestroyRenderer() * INTERNAL API */ +// UTIL + +static void CopyImageToImage(VkCommandBuffer cmd, VkImage src, VkImage dst, VkExtent2D src_ext, VkExtent2D dst_ext) +{ + VkImageBlit2 blit = { + .sType = STYPE(IMAGE_BLIT_2), + .srcOffsets = { + {}, + { .x = (i32)src_ext.width, .y = (i32)src_ext.height, .z = 1 }, + }, + .dstOffsets = { + {}, + { .x = (i32)dst_ext.width, .y = (i32)dst_ext.height, .z = 1 }, + }, + .srcSubresource = { + .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, + .baseArrayLayer = 0, + .layerCount = 1, + .mipLevel = 0, + }, + .dstSubresource = { + .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, + .baseArrayLayer = 0, + .layerCount = 1, + .mipLevel = 0, + }, + }; + + VkBlitImageInfo2 blit_info = { + .sType = STYPE(BLIT_IMAGE_INFO_2), + .srcImage = src, + .srcImageLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, + .dstImage = dst, + .dstImageLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, + .filter = VK_FILTER_LINEAR, + .regionCount = 1, + .pRegions = &blit, + }; + + vkCmdBlitImage2(cmd, &blit_info); +} + +static void TransitionImage(VkCommandBuffer cmd, VkImage img, VkImageLayout curr, VkImageLayout new) +{ + VkImageMemoryBarrier2 barrier = { + .sType = STYPE(IMAGE_MEMORY_BARRIER_2), + .srcStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, + .srcAccessMask = VK_ACCESS_2_MEMORY_WRITE_BIT, + .dstStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, + .dstAccessMask = VK_ACCESS_2_MEMORY_WRITE_BIT | VK_ACCESS_2_MEMORY_READ_BIT, + .oldLayout = curr, + .newLayout = new, + .image = img, + .subresourceRange = { + .aspectMask = new == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL ? VK_IMAGE_ASPECT_DEPTH_BIT : VK_IMAGE_ASPECT_COLOR_BIT, + .baseMipLevel = 0, + .levelCount = VK_REMAINING_MIP_LEVELS, + .baseArrayLayer = 0, + .layerCount = VK_REMAINING_ARRAY_LAYERS, + }, + }; + + VkDependencyInfo dep_info = { + .sType = STYPE(DEPENDENCY_INFO), + .imageMemoryBarrierCount = 1, + .pImageMemoryBarriers = &barrier, + }; + + vkCmdPipelineBarrier2(cmd, &dep_info); +} + +// INIT + static b32 InitVulkan(Arena *arena) { Assert(arena != NULL, "Vulkan memory is null"); @@ -50,6 +354,7 @@ static b32 InitVulkan(Arena *arena) Assert(CreateImmediateStructures(), "Unable to create immediate structures"); Assert(CreateSwapchain(), "Unable to initialize swapchain and draw images"); Assert(CreateDescriptors(), "Unable to initialize descriptors."); + Assert(CreatePipelines(), "Unable to initialize pipelines."); ArenaFree(renderer.arena); @@ -354,6 +659,8 @@ static b32 InitVkDeviceFunctions() { INIT_DEV_FN(vkResetCommandBuffer); INIT_DEV_FN(vkFreeCommandBuffers); INIT_DEV_FN(vkDestroyDescriptorSetLayout); + INIT_DEV_FN(vkDestroyShaderModule); + INIT_DEV_FN(vkQueuePresentKHR); return true; } @@ -520,6 +827,11 @@ static b32 CreateSwapchain() if (present_mode != VK_PRESENT_MODE_MAILBOX_KHR) present_mode = VK_PRESENT_MODE_FIFO_KHR; + + // + // TODO(MA): REMOVE THIS LATER + present_mode = VK_PRESENT_MODE_FIFO_KHR; + swapchain_create_info.minImageCount = capabilities.minImageCount + 1; swapchain_create_info.surface = surface; swapchain_create_info.imageFormat = renderer.vk.sc.format; @@ -560,8 +872,6 @@ static b32 CreateSwapchain() .depth = 1, }; - renderer.vk.sc.extent = extent_3d; - VmaAllocationCreateInfo alloc_create_info = { .usage = VMA_MEMORY_USAGE_GPU_ONLY, .requiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, @@ -598,6 +908,11 @@ static b32 CreateSwapchain() if (result != VK_SUCCESS) success = false; + // Setting values + renderer.vk.sc.extent = extent_3d; + renderer.vk.sc.depth_img.fmt = depth_image_create_info.format; + renderer.vk.sc.draw_img.fmt = draw_image_create_info.format; + return success; } @@ -639,33 +954,33 @@ static b32 CreateDescriptors() VkDevice device = renderer.vk.device; VkResult result; - result = vkCreateDescriptorPool(device, &desc_pool_info, NULL, &renderer.vk.desc.pool); + result = vkCreateDescriptorPool(device, &desc_pool_info, NULL, &renderer.vk.pipe.pool); if (result != VK_SUCCESS) success = false; - result = vkCreateDescriptorSetLayout(device, &shared_layout_create_info, NULL, &renderer.vk.desc.layouts[DESC_TYPE_SHARED]); + result = vkCreateDescriptorSetLayout(device, &shared_layout_create_info, NULL, &renderer.vk.pipe.layouts[DESC_TYPE_SHARED]); if (result != VK_SUCCESS) success = false; for (u32 i = DESC_TYPE_COMBINED_SAMPLER; i < DESC_TYPE_MAX; i++) { bindless_layout_binding.descriptorType = desc_type_map[i]; - result = vkCreateDescriptorSetLayout(device, &bindless_layout_create_info, NULL, &renderer.vk.desc.layouts[i]); + result = vkCreateDescriptorSetLayout(device, &bindless_layout_create_info, NULL, &renderer.vk.pipe.layouts[i]); if (result != VK_SUCCESS) success = false; } - set_allocate_info.descriptorPool = renderer.vk.desc.pool; - set_allocate_info.pSetLayouts = renderer.vk.desc.layouts; + set_allocate_info.descriptorPool = renderer.vk.pipe.pool; + set_allocate_info.pSetLayouts = renderer.vk.pipe.layouts; - result = vkAllocateDescriptorSets(device, &set_allocate_info, renderer.vk.desc.sets); + result = vkAllocateDescriptorSets(device, &set_allocate_info, renderer.vk.pipe.sets); if (result != VK_SUCCESS) success = false; pipeline_layout_create_info.setLayoutCount = DESC_TYPE_MAX; - pipeline_layout_create_info.pSetLayouts = renderer.vk.desc.layouts; + pipeline_layout_create_info.pSetLayouts = renderer.vk.pipe.layouts; - result = vkCreatePipelineLayout(device, &pipeline_layout_create_info, NULL, &renderer.vk.desc.pipeline_layout); + result = vkCreatePipelineLayout(device, &pipeline_layout_create_info, NULL, &renderer.vk.pipe.pipeline_layout); if (result != VK_SUCCESS) success = false; @@ -675,13 +990,72 @@ static b32 CreateDescriptors() static b32 CreatePipelines() { b32 success = true; + VkResult result; + VkDevice device = renderer.vk.device; + + VkShaderModule cube_vert, cube_frag; + success &= CreateShaderModule(shader_quad_vert, shader_quad_vert_len, &cube_vert); + success &= CreateShaderModule(shader_quad_frag, shader_quad_frag_len, &cube_frag); + + VkPipelineShaderStageCreateInfo cube_shader_stages[] = { + { + .sType = STYPE(PIPELINE_SHADER_STAGE_CREATE_INFO), + .stage = VK_SHADER_STAGE_VERTEX_BIT, + .module = cube_vert, + .pName = "main", + }, + { + .sType = STYPE(PIPELINE_SHADER_STAGE_CREATE_INFO), + .stage = VK_SHADER_STAGE_FRAGMENT_BIT, + .module = cube_frag, + .pName = "main", + }, + }; + + VkPipelineRenderingCreateInfo pipeline_render_info = { + .sType = STYPE(PIPELINE_RENDERING_CREATE_INFO), + .colorAttachmentCount = 1, + .pColorAttachmentFormats = &renderer.vk.sc.draw_img.fmt, + .depthAttachmentFormat = renderer.vk.sc.depth_img.fmt, + }; + + cube_create_info.pStages = cube_shader_stages; + cube_create_info.stageCount = Len(cube_shader_stages); + cube_create_info.layout = renderer.vk.pipe.pipeline_layout; + cube_create_info.pNext = &pipeline_render_info; + + result = vkCreateGraphicsPipelines(device, 0, 1, &cube_create_info, NULL, &renderer.vk.pipe.pipelines[PIPELINE_CUBE]); + if (result != VK_SUCCESS) + { + Printf("vkCreateGraphicsPipelines failure: %s", VkResultStr(result)); + success = false; + } + + vkDestroyShaderModule(device, cube_vert, NULL); + vkDestroyShaderModule(device, cube_frag, NULL); return success; } -static VkShaderModule CreateShaderModule(const char *file_name) +static b32 CreateShaderModule(u8 *bytes, u32 len, VkShaderModule *module) { - return NULL; + VkResult result; + b32 success = true; + + VkShaderModuleCreateInfo module_info = { + .sType = STYPE(SHADER_MODULE_CREATE_INFO), + .codeSize = len, + .pCode = (u32 *)bytes, + }; + + result = vkCreateShaderModule(renderer.vk.device, &module_info, NULL, module); + if (result != VK_SUCCESS) + { + Printf("vkCreateShaderModule failure: %s", VkResultStr(result)); + success = false; + } + + return success; } static void DestroyVulkan() @@ -693,14 +1067,17 @@ static void DestroyVulkan() SwapchainStructures sc = renderer.vk.sc; VmaAllocator vma_alloc = renderer.vk.alloc; VkSwapchainKHR swapchain = renderer.vk.swapchain; - DescriptorStructures desc = renderer.vk.desc; + PipelineStructures pipe = renderer.vk.pipe; - vkDestroyPipelineLayout(device, desc.pipeline_layout, NULL); + for (u32 i = PIPELINE_CUBE; i < PIPELINE_MAX; i++) + vkDestroyPipeline(device, pipe.pipelines[i], NULL); + + vkDestroyPipelineLayout(device, pipe.pipeline_layout, NULL); for (u32 i = DESC_TYPE_SHARED; i < DESC_TYPE_MAX; i++) - vkDestroyDescriptorSetLayout(device, desc.layouts[i], NULL); + vkDestroyDescriptorSetLayout(device, pipe.layouts[i], NULL); - vkDestroyDescriptorPool(device, desc.pool, NULL); + vkDestroyDescriptorPool(device, pipe.pool, NULL); vkDestroyImageView(device, sc.draw_img.view, NULL); vmaDestroyImage(vma_alloc, sc.draw_img.img, sc.draw_img.alloc); diff --git a/src/render_vulkan.h b/src/render_vulkan.h index 850575c..7b5260e 100644 --- a/src/render_vulkan.h +++ b/src/render_vulkan.h @@ -5,6 +5,8 @@ #include +#include "file_data/spv.c" + // Macros #define VK_DECLARE(fn) static PFN_##fn fn = NULL @@ -114,6 +116,8 @@ VK_DECLARE(vkResetFences); VK_DECLARE(vkResetCommandBuffer); VK_DECLARE(vkFreeCommandBuffers); VK_DECLARE(vkDestroyDescriptorSetLayout); +VK_DECLARE(vkDestroyShaderModule); +VK_DECLARE(vkQueuePresentKHR); // Vulkan Functions END @@ -152,7 +156,8 @@ typedef struct VkDescriptorSet sets[DESC_TYPE_MAX]; DescBindings *bindings; u16 bindings_count; -} DescriptorStructures; + VkPipeline pipelines[PIPELINE_MAX]; +} PipelineStructures; typedef struct { @@ -202,6 +207,11 @@ typedef struct { Image depth_img; } SwapchainStructures; +typedef struct { + u32 img_ix; + u64 frame_cnt; +} FrameState; + typedef struct { Library lib; VkInstance inst; @@ -214,7 +224,7 @@ typedef struct { FrameStructures frame; ImmediateStructures imm; SwapchainStructures sc; - DescriptorStructures desc; + PipelineStructures pipe; #ifdef BUILD_DEBUG VkDebugUtilsMessengerEXT debug; #endif @@ -222,6 +232,7 @@ typedef struct { typedef struct { Vulkan_t vk; + FrameState frame_state; Arena *arena; Arena *perm_arena; } Renderer_t; @@ -257,10 +268,15 @@ static b32 CreateSwapchain(); static VkFormat GetImageFormat(); static b32 CreateDescriptors(); static b32 CreatePipelines(); +static b32 CreateShaderModule(u8 *bytes, u32 len, VkShaderModule *module); // Destroy static void DestroyVulkan(); +// Util +static void TransitionImage(VkCommandBuffer cmd, VkImage img, VkImageLayout curr, VkImageLayout new); +static void CopyImageToImage(VkCommandBuffer cmd, VkImage src, VkImage dst, VkExtent2D src_ext, VkExtent2D dst_ext); + // Renderer Functions Declarations END #include "vulkan_config.c" @@ -296,5 +312,13 @@ static char *vulkan_libs[] = { #endif +// BACKEND API + +// Init b32 _InitRenderer(Arena *arena); void _DestroyRenderer(); + +// Rendering +static b32 _BeginFrame(); +static void _DrawTriangle(); +static b32 _FinishFrame(); diff --git a/src/shaders/glsl/quad.vert.glsl b/src/shaders/glsl/quad.vert.glsl index dbae84a..0b79844 100644 --- a/src/shaders/glsl/quad.vert.glsl +++ b/src/shaders/glsl/quad.vert.glsl @@ -2,7 +2,7 @@ layout (location = 0) out vec3 fragColor; -vec2 triangle[3] = vec2[]( +vec2 positions[3] = vec2[]( vec2(0.0, -0.5), vec2(0.5, 0.5), vec2(-0.5, 0.5) diff --git a/src/shaders/glsl/structures.glsl b/src/shaders/glsl/structures.glsl new file mode 100644 index 0000000..55d8b56 --- /dev/null +++ b/src/shaders/glsl/structures.glsl @@ -0,0 +1,57 @@ +// ========================================== +// ====== TODO: Research values here ======== +// ========================================== + +struct Vertex { + float uv_x; + float uv_y; + float x, y, z; +}; + +layout (set = 0, binding = 0) uniform GlobalUniform { + mat4 camera_view; + mat4 camera_projection; + mat4 camera_view_projection; + mat4 camera_view_inverse; + mat4 camera_projection_inverse; + mat4 camera_previous_view; + mat4 camera_previous_projection; + vec2 render_resolution; + vec2 jitter_offset; + float delta; + uint frame_count; +} Globals; + +layout (set = 0, binding = 1) uniform ShaderOptions { + float exposure; + float alpha; + int debug_display; + uint albedo_texture_indices[]; +} Options; + +layout (rgba16f, set = 0, binding = 2) uniform image2D image; + +layout (set = 1, binding = 0) uniform sampler2D Textures[]; + +layout (rgba8_snorm, set = 2, binding = 0) uniform image2D Images[]; + +layout (set = 3, binding = 0) uniform PBRMaterials { + vec3 albedo; + vec3 normal; + float metallic; + float roughness; + float ao; +} Materials[]; + + +layout (std430, buffer_reference) readonly buffer VertexBuffer { + Vertex vertices[]; +}; + +layout (push_constant) uniform constants { + mat4 render_matrix; + VertexBuffer vertex_buffer; + uint sampler_index; + uint image_index; + uint material_index; +} PC;