mirror of
https://github.com/id-Software/DOOM-3-BFG.git
synced 2025-03-14 22:50:45 +00:00
Added Experiment to compile GLSL to SPIR-V using shaderc
This commit is contained in:
parent
377562506a
commit
a9c2e316b6
3 changed files with 94 additions and 20 deletions
|
@ -27,6 +27,9 @@ option(BINKDEC
|
|||
option(USE_VULKAN
|
||||
"Use Vulkan instead of OpenGL" OFF)
|
||||
|
||||
option(SPIRV_SHADERC
|
||||
"Compile SPIR-V shader byte code using shaderc instead of using Glslang directly" OFF)
|
||||
|
||||
option(ONATIVE
|
||||
"Optimize for the host CPU" OFF)
|
||||
|
||||
|
@ -293,22 +296,34 @@ if(USE_VULKAN)
|
|||
endif()
|
||||
|
||||
add_definitions(-DUSE_VULKAN)
|
||||
include_directories($ENV{VK_SDK_PATH}/Include)
|
||||
include_directories($ENV{VULKAN_SDK}/Include)
|
||||
|
||||
# override Glslang build options
|
||||
SET_OPTION(ENABLE_SPVREMAPPER OFF)
|
||||
SET_OPTION(ENABLE_GLSLANG_BINARIES OFF)
|
||||
SET_OPTION(ENABLE_HLSL OFF)
|
||||
#SET_OPTION(ENABLE_OPT OFF)
|
||||
if(SPIRV_SHADERC)
|
||||
add_definitions(-DSPIRV_SHADERC)
|
||||
|
||||
if(CMAKE_CL_64)
|
||||
link_directories($ENV{VULKAN_SDK}/Lib)
|
||||
else()
|
||||
link_directories($ENV{VULKAN_SDK}/Lib32)
|
||||
endif()
|
||||
|
||||
else()
|
||||
# override Glslang build options
|
||||
SET_OPTION(ENABLE_SPVREMAPPER OFF)
|
||||
SET_OPTION(ENABLE_GLSLANG_BINARIES OFF)
|
||||
SET_OPTION(ENABLE_HLSL OFF)
|
||||
SET_OPTION(ENABLE_OPT ON)
|
||||
SET_OPTION(SPIRV_SKIP_EXECUTABLES ON)
|
||||
|
||||
#option(ENABLE_AMD_EXTENSIONS "Enables support of AMD-specific extensions" ON)
|
||||
#option(ENABLE_NV_EXTENSIONS "Enables support of Nvidia-specific extensions" ON)
|
||||
#option(ENABLE_OPT "Enables spirv-opt capability if present" ON)
|
||||
#option(ENABLE_AMD_EXTENSIONS "Enables support of AMD-specific extensions" ON)
|
||||
#option(ENABLE_NV_EXTENSIONS "Enables support of Nvidia-specific extensions" ON)
|
||||
#option(ENABLE_OPT "Enables spirv-opt capability if present" ON)
|
||||
|
||||
set(GLSLANG_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libs/glslang)
|
||||
add_subdirectory(${GLSLANG_DIR})
|
||||
set(GLSLANG_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libs/glslang)
|
||||
add_subdirectory(${GLSLANG_DIR})
|
||||
|
||||
include_directories(${GLSLANG_DIR}/glslang)
|
||||
include_directories(${GLSLANG_DIR}/glslang)
|
||||
endif()
|
||||
|
||||
else()
|
||||
|
||||
|
@ -1309,12 +1324,16 @@ if(MSVC)
|
|||
|
||||
set(Vulkan_LIBRARIES
|
||||
${Vulkan_LIBRARY}
|
||||
glslang
|
||||
SPIRV
|
||||
)
|
||||
|
||||
if(ENABLE_GLSLANG_BINARIES)
|
||||
list(APPEND Vulkan_LIBRARIES glslang-default-resource-limits)
|
||||
|
||||
if(SPIRV_SHADERC)
|
||||
list(APPEND Vulkan_LIBRARIES shaderc_combined)
|
||||
else()
|
||||
list(APPEND Vulkan_LIBRARIES glslang SPIRV)
|
||||
|
||||
if(ENABLE_GLSLANG_BINARIES)
|
||||
list(APPEND Vulkan_LIBRARIES glslang-default-resource-limits)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
else()
|
||||
|
|
|
@ -2,5 +2,5 @@ cd ..
|
|||
del /s /q build
|
||||
mkdir build
|
||||
cd build
|
||||
cmake -G "Visual Studio 15 Win64" -DWINDOWS10=ON -DUSE_VULKAN=ON -DUSE_FFMPEG=ON ../neo
|
||||
cmake -G "Visual Studio 15 Win64" -DWINDOWS10=ON -DUSE_VULKAN=ON -DSPIRV_SHADERC=OFF -DFFMPEG=OFF ../neo
|
||||
pause
|
|
@ -337,9 +337,64 @@ void idRenderProgManager::LoadShader( int index, rpStage_t stage )
|
|||
CompileGLSLtoSPIRV
|
||||
================================================================================================
|
||||
*/
|
||||
#define USE_GLSLANG 1
|
||||
|
||||
#if defined(USE_GLSLANG)
|
||||
|
||||
#if defined(SPIRV_SHADERC)
|
||||
|
||||
#include <shaderc/shaderc.hpp>
|
||||
|
||||
static int CompileGLSLtoSPIRV( const char* filename, const idStr& dataGLSL, const rpStage_t stage, uint32** spirvBuffer )
|
||||
{
|
||||
shaderc::Compiler compiler;
|
||||
shaderc::CompileOptions options;
|
||||
|
||||
// Like -DMY_DEFINE=1
|
||||
//options.AddMacroDefinition("MY_DEFINE", "1");
|
||||
|
||||
//if (optimize)
|
||||
{
|
||||
options.SetOptimizationLevel( shaderc_optimization_level_size );
|
||||
}
|
||||
|
||||
shaderc_shader_kind shaderKind;
|
||||
if( stage == SHADER_STAGE_VERTEX )
|
||||
{
|
||||
shaderKind = shaderc_glsl_vertex_shader;
|
||||
}
|
||||
else if( stage == SHADER_STAGE_COMPUTE )
|
||||
{
|
||||
shaderKind = shaderc_glsl_compute_shader;
|
||||
}
|
||||
else
|
||||
{
|
||||
shaderKind = shaderc_glsl_fragment_shader;
|
||||
}
|
||||
|
||||
std::string source = dataGLSL.c_str();
|
||||
|
||||
shaderc::SpvCompilationResult module = compiler.CompileGlslToSpv( source, shaderKind, filename, options );
|
||||
|
||||
if( module.GetCompilationStatus() != shaderc_compilation_status_success )
|
||||
{
|
||||
idLib::Printf( "Comping GLSL to SPIR-V using shaderc failed for: %s\n", filename );
|
||||
idLib::Printf( "%s\n", module.GetErrorMessage().c_str() );
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::vector<uint32_t> spirV = { module.cbegin(), module.cend() };
|
||||
|
||||
// copy to spirvBuffer
|
||||
int32 spirvLen = spirV.size() * sizeof( uint32_t );
|
||||
|
||||
void* buffer = Mem_Alloc( spirvLen, TAG_RENDERPROG );
|
||||
memcpy( buffer, spirV.data(), spirvLen );
|
||||
|
||||
*spirvBuffer = ( uint32_t* ) buffer;
|
||||
return spirvLen;
|
||||
|
||||
|
||||
}
|
||||
#else
|
||||
|
||||
#include <glslang/public/ShaderLang.h>
|
||||
#include <glslang/Include/ResourceLimits.h>
|
||||
|
|
Loading…
Reference in a new issue