diff --git a/.gitmodules b/.gitmodules index df4212a..f4387cb 100644 --- a/.gitmodules +++ b/.gitmodules @@ -21,7 +21,4 @@ url = https://github.com/zeux/volk [submodule "dependencies/VulkanMemoryAllocator"] path = dependencies/VulkanMemoryAllocator - url = https://github.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator -[submodule "dependencies/shaderc"] - path = dependencies/shaderc - url = https://github.com/google/shaderc + url = https://github.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 9185352..49b84ed 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.23) +cmake_minimum_required(VERSION 3.24) # options option(ENGINE_BUILD_TEST "Compile the test program" ON) @@ -147,6 +147,9 @@ endif() if(ENGINE_BUILD_VULKAN) + include(FindVulkan) + find_package(Vulkan COMPONENTS shaderc_combined) + # Volk set(VOLK_STATIC_DEFINES "") set(VOLK_PULL_IN_VULKAN ON) @@ -158,6 +161,10 @@ if(ENGINE_BUILD_VULKAN) # Vulkan Memory Allocator target_include_directories(${PROJECT_NAME} PRIVATE dependencies/VulkanMemoryAllocator/include) + # shaderc + + target_link_libraries(${PROJECT_NAME} PRIVATE Vulkan::shaderc_combined) + endif() # SDL2: diff --git a/dependencies/shaderc b/dependencies/shaderc deleted file mode 160000 index dc9d28a..0000000 --- a/dependencies/shaderc +++ /dev/null @@ -1 +0,0 @@ -Subproject commit dc9d28ae95594edaef725012acd9de15a7342048 diff --git a/src/gfx_device_vulkan.cpp b/src/gfx_device_vulkan.cpp index 86845fd..f90bcb7 100644 --- a/src/gfx_device_vulkan.cpp +++ b/src/gfx_device_vulkan.cpp @@ -30,6 +30,7 @@ #include #include #include +#include namespace engine { @@ -153,7 +154,12 @@ namespace engine { shaderc::Compiler compiler; shaderc::CompileOptions options; - options.SetOptimizationLevel(shaderc_optimization_level_performance); + options.SetSourceLanguage(shaderc_source_language_glsl); + options.SetTargetEnvironment(shaderc_target_env_vulkan, shaderc_env_version_vulkan_1_3); + options.SetOptimizationLevel(shaderc_optimization_level_size); + options.SetTargetSpirv(shaderc_spirv_version_1_6); + options.SetAutoBindUniforms(false); + options.SetInvertY(false); // preprocess shaderc::PreprocessedSourceCompilationResult preprocessed = compiler.PreprocessGlsl(source, kind, filename, options); @@ -163,10 +169,12 @@ namespace engine { throw std::runtime_error(preprocessed.GetErrorMessage()); } + + std::string shaderStr = { preprocessed.cbegin(), preprocessed.cend() }; // compile - shaderc::SpvCompilationResult module = compiler.CompileGlslToSpv(shaderStr, kind, filename, options); + shaderc::SpvCompilationResult module = compiler.CompileGlslToSpv(shaderStr.data(), kind, filename, options); if (module.GetCompilationStatus() != shaderc_compilation_status_success) { @@ -177,8 +185,8 @@ namespace engine { VkShaderModuleCreateInfo createInfo{}; createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; - createInfo.codeSize = shaderBytecode.size(); - createInfo.pCode = shaderBytecode.data(); + createInfo.codeSize = shaderBytecode.size() * sizeof(uint32_t); + createInfo.pCode = module.cbegin(); VkShaderModule shaderModule; if (vkCreateShaderModule(device, &createInfo, nullptr, &shaderModule) != VK_SUCCESS) { @@ -189,9 +197,9 @@ namespace engine { } - static std::vector readFile(const std::string& filename) + static std::vector readTextFile(const std::string& filename) { - std::ifstream file(filename, std::ios::ate | std::ios::binary); + std::ifstream file(filename, std::ios::ate); if (file.is_open() == false) { throw std::runtime_error("Unable to open file " + filename); } @@ -463,7 +471,7 @@ namespace engine { for (const auto& presMode : presentModes) { if (presMode == VK_PRESENT_MODE_MAILBOX_KHR) { -// swapchain->presentMode = presMode; // this mode allows uncapped FPS while also avoiding screen tearing + swapchain->presentMode = presMode; // this mode allows uncapped FPS while also avoiding screen tearing } } @@ -790,8 +798,6 @@ namespace engine { useValidation = true; // debug mode #endif - - // get the both the engine and application versions int appVersionMajor = 0, appVersionMinor = 0, appVersionPatch = 0; assert(versionFromCharArray(appVersion, &appVersionMajor, &appVersionMinor, &appVersionPatch)); @@ -1368,6 +1374,13 @@ namespace engine { gfx::Pipeline* pipeline = new gfx::Pipeline; + auto vertShaderCode = readTextFile(vertShaderPath); + auto fragShaderCode = readTextFile(fragShaderPath); + INFO("Opened shader: {}", std::filesystem::path(vertShaderPath).filename().string()); + + VkShaderModule vertShaderModule = compileShader(pimpl->device, shaderc_vertex_shader, vertShaderCode.data(), vertShaderPath); + VkShaderModule fragShaderModule = compileShader(pimpl->device, shaderc_fragment_shader, fragShaderCode.data(), fragShaderPath); + // create uniform buffers pipeline->uniformBuffers.resize(FRAMES_IN_FLIGHT); for (int i = 0; i < FRAMES_IN_FLIGHT; i++) { @@ -1448,13 +1461,6 @@ namespace engine { attribDescs.push_back(vulkanAttribDesc); } - auto vertShaderCode = readFile(vertShaderPath); - auto fragShaderCode = readFile(fragShaderPath); - INFO("Opened shader: {}", std::filesystem::path(vertShaderPath).filename().string()); - - VkShaderModule vertShaderModule = compileShader(pimpl->device, shaderc_vertex_shader, vertShaderCode.data(), vertShaderPath); - VkShaderModule fragShaderModule = compileShader(pimpl->device, shaderc_fragment_shader, fragShaderCode.data(), fragShaderPath); - VkPipelineShaderStageCreateInfo vertShaderStageInfo{ VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO }; vertShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT; vertShaderStageInfo.module = vertShaderModule; diff --git a/src/window.cpp b/src/window.cpp index f2d6ab0..1cdb3a2 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -291,7 +291,7 @@ namespace engine { void Window::toggleFullscreen() { - setFullscreen(!m_fullscreen); + setFullscreen(!m_fullscreen, true); } bool Window::isFullscreen() const diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 133cbdb..ed7d8e2 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -23,6 +23,9 @@ set(GAME_SOURCES if (WIN32) add_executable(${PROJECT_NAME} WIN32 ${GAME_SOURCES} "game.rc") + + file(GLOB_RECURSE RES_FILES "${PROJECT_SOURCE_DIR}/res/*") + source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/res" PREFIX "Resources" FILES ${RES_FILES}) else() add_executable(${PROJECT_NAME} ${GAME_SOURCES}) endif() diff --git a/test/res/shader.frag b/test/res/shader.frag index e21bd30..6f6671e 100644 --- a/test/res/shader.frag +++ b/test/res/shader.frag @@ -8,13 +8,15 @@ layout(location = 4) in vec3 fragColor; layout(location = 0) out vec4 outColor; +float snoise(vec2 v); + void main() { // constants vec3 lightColor = vec3(1.0, 1.0, 1.0); vec3 ambientColor = vec3(1.0, 1.0, 1.0); float ambientStrength = 0.1; - vec3 baseColor = fragColor; + vec3 baseColor = vec3(fragColor.x * snoise(fragUV * 10.0), fragColor.yz); vec3 emission = vec3(0.0, 0.0, 0.0); // code @@ -33,4 +35,37 @@ void main() { vec3 lighting = min(diffuse + ambient + specular, 1.0); outColor = min( ( vec4(baseColor, 1.0) ) * vec4(lighting + emission, 1.0), vec4(1.0)); +} + + + +// Simplex 2D noise +// +vec3 permute(vec3 x) { return mod(((x*34.0)+1.0)*x, 289.0); } + +float snoise(vec2 v){ + const vec4 C = vec4(0.211324865405187, 0.366025403784439, + -0.577350269189626, 0.024390243902439); + vec2 i = floor(v + dot(v, C.yy) ); + vec2 x0 = v - i + dot(i, C.xx); + vec2 i1; + i1 = (x0.x > x0.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0); + vec4 x12 = x0.xyxy + C.xxzz; + x12.xy -= i1; + i = mod(i, 289.0); + vec3 p = permute( permute( i.y + vec3(0.0, i1.y, 1.0 )) + + i.x + vec3(0.0, i1.x, 1.0 )); + vec3 m = max(0.5 - vec3(dot(x0,x0), dot(x12.xy,x12.xy), + dot(x12.zw,x12.zw)), 0.0); + m = m*m ; + m = m*m ; + vec3 x = 2.0 * fract(p * C.www) - 1.0; + vec3 h = abs(x) - 0.5; + vec3 ox = floor(x + 0.5); + vec3 a0 = x - ox; + m *= 1.79284291400159 - 0.85373472095314 * ( a0*a0 + h*h ); + vec3 g; + g.x = a0.x * x0.x + h.x * x0.y; + g.yz = a0.yz * x12.xz + h.yz * x12.yw; + return 130.0 * dot(m, g); } \ No newline at end of file diff --git a/test/res/shader.frag.spv b/test/res/shader.frag.spv new file mode 100644 index 0000000..a66deba Binary files /dev/null and b/test/res/shader.frag.spv differ diff --git a/test/res/shader.vert.spv b/test/res/shader.vert.spv new file mode 100644 index 0000000..ae4717f Binary files /dev/null and b/test/res/shader.vert.spv differ diff --git a/test/src/main.cpp b/test/src/main.cpp index 1a82ba9..fe4e5d5 100644 --- a/test/src/main.cpp +++ b/test/src/main.cpp @@ -20,9 +20,8 @@ int main(int argc, char *argv[]) CRITICAL("{}", e.what()); -#ifdef NDEBUG engine::Window::errorBox(e.what()); -#else +#ifndef NDEBUG fputs(e.what(), stderr); fputc('\n', stderr); #endif