From 355b2bf85bbb6df718d522f5aabf887e7dc92c78 Mon Sep 17 00:00:00 2001 From: bjorn Date: Sun, 22 May 2022 14:58:07 -0700 Subject: [PATCH] Switch to glslang fork; The reason is that the glslang C API doesn't support the extra overloads that let you provide multiple strings or the lengths for strings. In our case our shader blobs are not null terminated, so sending them to glslang would overrun the buffer. I forked glslang and modified the C API to support a length parameter. --- .gitmodules | 2 +- CMakeLists.txt | 11 +++++++++-- deps/glslang | 2 +- src/modules/graphics/graphics.c | 7 +++++++ 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/.gitmodules b/.gitmodules index 49c0b6e6..40b280c4 100644 --- a/.gitmodules +++ b/.gitmodules @@ -21,4 +21,4 @@ url = https://github.com/lovr-org/ovr_openxr_mobile_sdk [submodule "deps/glslang"] path = deps/glslang - url = https://github.com/KhronosGroup/glslang + url = https://github.com/bjornbytes/glslang diff --git a/CMakeLists.txt b/CMakeLists.txt index 10cbfbbd..f1c7f44c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -180,9 +180,12 @@ if(LOVR_USE_GLSLANG) set(ENABLE_OPT OFF CACHE BOOL "") set(ENABLE_CTEST OFF CACHE BOOL "") set(BUILD_EXTERNAL OFF CACHE BOOL "") + set(BUILD_SHARED_LIBS OFF) + include_directories(deps/glslang/glslang/Include deps/glslang/StandAlone) add_subdirectory(deps/glslang glslang) - set_target_properties(SPIRV PROPERTIES EXCLUDE_FROM_ALL 1) - set(LOVR_GLSLANG glslang) + target_sources(glslang PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/deps/glslang/StandAlone/ResourceLimits.cpp") + target_sources(glslang PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/deps/glslang/StandAlone/resource_limits_c.cpp") + set(LOVR_GLSLANG glslang SPIRV) endif() # Vulkan @@ -443,6 +446,10 @@ if(LOVR_ENABLE_GRAPHICS) src/api/l_graphics_pass.c ) + if(LOVR_USE_GLSLANG) + target_compile_definitions(lovr PRIVATE LOVR_USE_GLSLANG) + endif() + if(LOVR_USE_VULKAN) target_compile_definitions(lovr PRIVATE LOVR_VK) target_sources(lovr PRIVATE src/core/gpu_vk.c) diff --git a/deps/glslang b/deps/glslang index 14f6e273..fe9e73e9 160000 --- a/deps/glslang +++ b/deps/glslang @@ -1 +1 @@ -Subproject commit 14f6e2730457bac14a64cb3d962d0a3da8c15ae4 +Subproject commit fe9e73e9c818a9608b7d91fc701a4be4142f47b9 diff --git a/src/modules/graphics/graphics.c b/src/modules/graphics/graphics.c index 13f80201..2f14acfb 100644 --- a/src/modules/graphics/graphics.c +++ b/src/modules/graphics/graphics.c @@ -619,6 +619,12 @@ const SamplerInfo* lovrSamplerGetInfo(Sampler* sampler) { // Shader Blob* lovrGraphicsCompileShader(ShaderStage stage, Blob* source) { + uint32_t spirv = 0x07230203; + + if (source->size % 4 == 0 && source->size >= 4 && !memcmp(source->data, &spirv, 4)) { + return lovrRetain(source), source; + } + #ifdef LOVR_USE_GLSLANG const glslang_stage_t stages[] = { [STAGE_VERTEX] = GLSLANG_STAGE_VERTEX, @@ -636,6 +642,7 @@ Blob* lovrGraphicsCompileShader(ShaderStage stage, Blob* source) { .target_language = GLSLANG_TARGET_SPV, .target_language_version = GLSLANG_TARGET_SPV_1_3, .code = source->data, + .length = source->size, .default_version = 460, .default_profile = GLSLANG_NO_PROFILE, .resource = resource