2019-03-15 09:40:45 +00:00
|
|
|
// Copyright(c) 2019, NVIDIA CORPORATION. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
|
2019-03-26 11:24:36 +00:00
|
|
|
#include "shaders.hpp"
|
2020-04-12 19:49:12 +00:00
|
|
|
|
2019-03-15 09:40:45 +00:00
|
|
|
#include "SPIRV/GlslangToSpv.h"
|
2023-10-17 10:41:14 +00:00
|
|
|
#include "glslang/Public/ResourceLimits.h"
|
2024-03-19 08:08:53 +00:00
|
|
|
#include "glslang/Public/ShaderLang.h"
|
2022-08-10 09:19:06 +00:00
|
|
|
|
|
|
|
#include <vulkan/vulkan.hpp>
|
2019-03-15 09:40:45 +00:00
|
|
|
|
|
|
|
namespace vk
|
|
|
|
{
|
|
|
|
namespace su
|
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
EShLanguage translateShaderStage( vk::ShaderStageFlagBits stage )
|
2019-03-15 09:40:45 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
switch ( stage )
|
2019-03-15 09:40:45 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
case vk::ShaderStageFlagBits::eVertex: return EShLangVertex;
|
|
|
|
case vk::ShaderStageFlagBits::eTessellationControl: return EShLangTessControl;
|
|
|
|
case vk::ShaderStageFlagBits::eTessellationEvaluation: return EShLangTessEvaluation;
|
|
|
|
case vk::ShaderStageFlagBits::eGeometry: return EShLangGeometry;
|
|
|
|
case vk::ShaderStageFlagBits::eFragment: return EShLangFragment;
|
|
|
|
case vk::ShaderStageFlagBits::eCompute: return EShLangCompute;
|
|
|
|
case vk::ShaderStageFlagBits::eRaygenNV: return EShLangRayGenNV;
|
|
|
|
case vk::ShaderStageFlagBits::eAnyHitNV: return EShLangAnyHitNV;
|
|
|
|
case vk::ShaderStageFlagBits::eClosestHitNV: return EShLangClosestHitNV;
|
|
|
|
case vk::ShaderStageFlagBits::eMissNV: return EShLangMissNV;
|
|
|
|
case vk::ShaderStageFlagBits::eIntersectionNV: return EShLangIntersectNV;
|
|
|
|
case vk::ShaderStageFlagBits::eCallableNV: return EShLangCallableNV;
|
|
|
|
case vk::ShaderStageFlagBits::eTaskNV: return EShLangTaskNV;
|
|
|
|
case vk::ShaderStageFlagBits::eMeshNV: return EShLangMeshNV;
|
|
|
|
default: assert( false && "Unknown shader stage" ); return EShLangVertex;
|
2019-03-15 09:40:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
bool GLSLtoSPV( const vk::ShaderStageFlagBits shaderType, std::string const & glslShader, std::vector<unsigned int> & spvShader )
|
2019-03-15 09:40:45 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
EShLanguage stage = translateShaderStage( shaderType );
|
2019-03-15 09:40:45 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
const char * shaderStrings[1];
|
2019-03-15 09:40:45 +00:00
|
|
|
shaderStrings[0] = glslShader.data();
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
glslang::TShader shader( stage );
|
|
|
|
shader.setStrings( shaderStrings, 1 );
|
2019-03-15 09:40:45 +00:00
|
|
|
|
|
|
|
// Enable SPIR-V and Vulkan rules when parsing GLSL
|
2022-02-28 09:11:04 +00:00
|
|
|
EShMessages messages = (EShMessages)( EShMsgSpvRules | EShMsgVulkanRules );
|
2019-03-15 09:40:45 +00:00
|
|
|
|
2023-10-17 10:41:14 +00:00
|
|
|
if ( !shader.parse( GetDefaultResources(), 100, false, messages ) )
|
2019-03-15 09:40:45 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
puts( shader.getInfoLog() );
|
|
|
|
puts( shader.getInfoDebugLog() );
|
2019-03-15 09:40:45 +00:00
|
|
|
return false; // something didn't work
|
|
|
|
}
|
|
|
|
|
|
|
|
glslang::TProgram program;
|
2020-04-12 19:49:12 +00:00
|
|
|
program.addShader( &shader );
|
2019-03-15 09:40:45 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Program-level processing...
|
|
|
|
//
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( !program.link( messages ) )
|
2019-03-15 09:40:45 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
puts( shader.getInfoLog() );
|
|
|
|
puts( shader.getInfoDebugLog() );
|
|
|
|
fflush( stdout );
|
2019-03-15 09:40:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
glslang::GlslangToSpv( *program.getIntermediate( stage ), spvShader );
|
2019-03-15 09:40:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::ShaderModule createShaderModule( vk::Device const & device, vk::ShaderStageFlagBits shaderStage, std::string const & shaderText )
|
2019-03-15 09:40:45 +00:00
|
|
|
{
|
|
|
|
std::vector<unsigned int> shaderSPV;
|
2020-10-20 07:55:19 +00:00
|
|
|
if ( !GLSLtoSPV( shaderStage, shaderText, shaderSPV ) )
|
|
|
|
{
|
|
|
|
throw std::runtime_error( "Could not convert glsl shader to spir-v -> terminating" );
|
|
|
|
}
|
2019-03-15 09:40:45 +00:00
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
return device.createShaderModule( vk::ShaderModuleCreateInfo( vk::ShaderModuleCreateFlags(), shaderSPV ) );
|
2019-03-15 09:40:45 +00:00
|
|
|
}
|
2020-04-12 19:49:12 +00:00
|
|
|
} // namespace su
|
|
|
|
} // namespace vk
|