diff options
| author | assiduous <assiduous@diligentgraphics.com> | 2020-05-11 00:05:19 +0000 |
|---|---|---|
| committer | assiduous <assiduous@diligentgraphics.com> | 2020-05-11 00:05:19 +0000 |
| commit | 242976bdb0efc39bdb127480d76d9838754fd4e8 (patch) | |
| tree | 9c879dcf658681b6289115dd0b6ad696ccc279eb /Graphics/GLSLTools | |
| parent | GL backend: fixed GetTextureInternalFormat function to allow texture fromat q... (diff) | |
| download | DiligentCore-242976bdb0efc39bdb127480d76d9838754fd4e8.tar.gz DiligentCore-242976bdb0efc39bdb127480d76d9838754fd4e8.zip | |
Added SHADER_SOURCE_LANGUAGE_GLSL_VERBATIM (API Version 240059).
Diffstat (limited to 'Graphics/GLSLTools')
| -rw-r--r-- | Graphics/GLSLTools/src/GLSLSourceBuilder.cpp | 382 |
1 files changed, 194 insertions, 188 deletions
diff --git a/Graphics/GLSLTools/src/GLSLSourceBuilder.cpp b/Graphics/GLSLTools/src/GLSLSourceBuilder.cpp index 984564a8..abb63d4f 100644 --- a/Graphics/GLSLTools/src/GLSLSourceBuilder.cpp +++ b/Graphics/GLSLTools/src/GLSLSourceBuilder.cpp @@ -44,240 +44,246 @@ String BuildGLSLSourceString(const ShaderCreateInfo& CreationAttribs, { String GLSLSource; - auto ShaderType = CreationAttribs.Desc.ShaderType; + if (CreationAttribs.SourceLanguage == SHADER_SOURCE_LANGUAGE_GLSL_VERBATIM && CreationAttribs.Macros != nullptr) + LOG_WARNING_MESSAGE("Shader macros are ignored when compiling GLSL verbatim"); + + if (CreationAttribs.SourceLanguage != SHADER_SOURCE_LANGUAGE_GLSL_VERBATIM) + { + auto ShaderType = CreationAttribs.Desc.ShaderType; #if PLATFORM_WIN32 || PLATFORM_LINUX - GLSLSource.append( - "#version 430 core\n" - "#define DESKTOP_GL 1\n"); + GLSLSource.append( + "#version 430 core\n" + "#define DESKTOP_GL 1\n"); # if PLATFORM_WIN32 - GLSLSource.append("#define PLATFORM_WIN32 1\n"); + GLSLSource.append("#define PLATFORM_WIN32 1\n"); # elif PLATFORM_LINUX - GLSLSource.append("#define PLATFORM_LINUX 1\n"); + GLSLSource.append("#define PLATFORM_LINUX 1\n"); # else # error Unexpected platform # endif #elif PLATFORM_MACOS - if (TargetCompiler == TargetGLSLCompiler::glslang) - GLSLSource.append("#version 430 core\n"); - else if (TargetCompiler == TargetGLSLCompiler::driver) - GLSLSource.append("#version 410 core\n"); - else - UNEXPECTED("Unexpected target GLSL compiler"); + if (TargetCompiler == TargetGLSLCompiler::glslang) + GLSLSource.append("#version 430 core\n"); + else if (TargetCompiler == TargetGLSLCompiler::driver) + GLSLSource.append("#version 410 core\n"); + else + UNEXPECTED("Unexpected target GLSL compiler"); - GLSLSource.append( - "#define DESKTOP_GL 1\n" - "#define PLATFORM_MACOS 1\n"); + GLSLSource.append( + "#define DESKTOP_GL 1\n" + "#define PLATFORM_MACOS 1\n"); #elif PLATFORM_ANDROID || PLATFORM_IOS - bool IsES30 = false; - bool IsES31OrAbove = false; - bool IsES32OrAbove = false; - if (deviceCaps.DevType == RENDER_DEVICE_TYPE_VULKAN) - { - IsES30 = false; - IsES31OrAbove = true; - IsES32OrAbove = false; - GLSLSource.append("#version 310 es\n"); - } - else if (deviceCaps.DevType == RENDER_DEVICE_TYPE_GLES) - { - IsES30 = deviceCaps.MajorVersion == 3 && deviceCaps.MinorVersion == 0; - IsES31OrAbove = deviceCaps.MajorVersion > 3 || (deviceCaps.MajorVersion == 3 && deviceCaps.MinorVersion >= 1); - IsES32OrAbove = deviceCaps.MajorVersion > 3 || (deviceCaps.MajorVersion == 3 && deviceCaps.MinorVersion >= 2); - std::stringstream versionss; - versionss << "#version " << deviceCaps.MajorVersion << deviceCaps.MinorVersion << "0 es\n"; - GLSLSource.append(versionss.str()); - } - else - { - UNEXPECTED("Unexpected device type"); - } + bool IsES30 = false; + bool IsES31OrAbove = false; + bool IsES32OrAbove = false; + if (deviceCaps.DevType == RENDER_DEVICE_TYPE_VULKAN) + { + IsES30 = false; + IsES31OrAbove = true; + IsES32OrAbove = false; + GLSLSource.append("#version 310 es\n"); + } + else if (deviceCaps.DevType == RENDER_DEVICE_TYPE_GLES) + { + IsES30 = deviceCaps.MajorVersion == 3 && deviceCaps.MinorVersion == 0; + IsES31OrAbove = deviceCaps.MajorVersion > 3 || (deviceCaps.MajorVersion == 3 && deviceCaps.MinorVersion >= 1); + IsES32OrAbove = deviceCaps.MajorVersion > 3 || (deviceCaps.MajorVersion == 3 && deviceCaps.MinorVersion >= 2); + std::stringstream versionss; + versionss << "#version " << deviceCaps.MajorVersion << deviceCaps.MinorVersion << "0 es\n"; + GLSLSource.append(versionss.str()); + } + else + { + UNEXPECTED("Unexpected device type"); + } - if (deviceCaps.Features.SeparablePrograms && !IsES31OrAbove) - GLSLSource.append("#extension GL_EXT_separate_shader_objects : enable\n"); + if (deviceCaps.Features.SeparablePrograms && !IsES31OrAbove) + GLSLSource.append("#extension GL_EXT_separate_shader_objects : enable\n"); - if (deviceCaps.TexCaps.CubemapArraysSupported && !IsES32OrAbove) - GLSLSource.append("#extension GL_EXT_texture_cube_map_array : enable\n"); + if (deviceCaps.TexCaps.CubemapArraysSupported && !IsES32OrAbove) + GLSLSource.append("#extension GL_EXT_texture_cube_map_array : enable\n"); - if (ShaderType == SHADER_TYPE_GEOMETRY && !IsES32OrAbove) - GLSLSource.append("#extension GL_EXT_geometry_shader : enable\n"); + if (ShaderType == SHADER_TYPE_GEOMETRY && !IsES32OrAbove) + GLSLSource.append("#extension GL_EXT_geometry_shader : enable\n"); - if ((ShaderType == SHADER_TYPE_HULL || ShaderType == SHADER_TYPE_DOMAIN) && !IsES32OrAbove) - GLSLSource.append("#extension GL_EXT_tessellation_shader : enable\n"); + if ((ShaderType == SHADER_TYPE_HULL || ShaderType == SHADER_TYPE_DOMAIN) && !IsES32OrAbove) + GLSLSource.append("#extension GL_EXT_tessellation_shader : enable\n"); - GLSLSource.append( - "#ifndef GL_ES\n" - "# define GL_ES 1\n" - "#endif\n"); + GLSLSource.append( + "#ifndef GL_ES\n" + "# define GL_ES 1\n" + "#endif\n"); # if PLATFORM_ANDROID - GLSLSource.append("#define PLATFORM_ANDROID 1\n"); + GLSLSource.append("#define PLATFORM_ANDROID 1\n"); # elif PLATFORM_IOS - GLSLSource.append("#define PLATFORM_IOS 1\n"); + GLSLSource.append("#define PLATFORM_IOS 1\n"); # else # error "Unexpected platform" # endif - GLSLSource.append( - "precision highp float;\n" - "precision highp int;\n" - //"precision highp uint;\n" // This line causes shader compilation error on NVidia! - - "precision highp sampler2D;\n" - "precision highp sampler3D;\n" - "precision highp samplerCube;\n" - "precision highp samplerCubeShadow;\n" - - "precision highp sampler2DShadow;\n" - "precision highp sampler2DArray;\n" - "precision highp sampler2DArrayShadow;\n" - - "precision highp isampler2D;\n" - "precision highp isampler3D;\n" - "precision highp isamplerCube;\n" - "precision highp isampler2DArray;\n" - - "precision highp usampler2D;\n" - "precision highp usampler3D;\n" - "precision highp usamplerCube;\n" - "precision highp usampler2DArray;\n" // clang-format off + GLSLSource.append( + "precision highp float;\n" + "precision highp int;\n" + //"precision highp uint;\n" // This line causes shader compilation error on NVidia! + + "precision highp sampler2D;\n" + "precision highp sampler3D;\n" + "precision highp samplerCube;\n" + "precision highp samplerCubeShadow;\n" + + "precision highp sampler2DShadow;\n" + "precision highp sampler2DArray;\n" + "precision highp sampler2DArrayShadow;\n" + + "precision highp isampler2D;\n" + "precision highp isampler3D;\n" + "precision highp isamplerCube;\n" + "precision highp isampler2DArray;\n" + + "precision highp usampler2D;\n" + "precision highp usampler3D;\n" + "precision highp usamplerCube;\n" + "precision highp usampler2DArray;\n" // clang-format off ); // clang-format on - if (IsES32OrAbove) - { - GLSLSource.append( - "precision highp samplerBuffer;\n" - "precision highp isamplerBuffer;\n" - "precision highp usamplerBuffer;\n" // clang-format off + if (IsES32OrAbove) + { + GLSLSource.append( + "precision highp samplerBuffer;\n" + "precision highp isamplerBuffer;\n" + "precision highp usamplerBuffer;\n" // clang-format off ); // clang-format on - } + } - if (deviceCaps.TexCaps.CubemapArraysSupported) - { - GLSLSource.append( - "precision highp samplerCubeArray;\n" - "precision highp samplerCubeArrayShadow;\n" - "precision highp isamplerCubeArray;\n" - "precision highp usamplerCubeArray;\n" // clang-format off + if (deviceCaps.TexCaps.CubemapArraysSupported) + { + GLSLSource.append( + "precision highp samplerCubeArray;\n" + "precision highp samplerCubeArrayShadow;\n" + "precision highp isamplerCubeArray;\n" + "precision highp usamplerCubeArray;\n" // clang-format off ); // clang-format on - } + } - if (deviceCaps.TexCaps.Texture2DMSSupported) - { - GLSLSource.append( - "precision highp sampler2DMS;\n" - "precision highp isampler2DMS;\n" - "precision highp usampler2DMS;\n" // clang-format off + if (deviceCaps.TexCaps.Texture2DMSSupported) + { + GLSLSource.append( + "precision highp sampler2DMS;\n" + "precision highp isampler2DMS;\n" + "precision highp usampler2DMS;\n" // clang-format off ); // clang-format on - } + } - if (deviceCaps.Features.ComputeShaders) - { - GLSLSource.append( - "precision highp image2D;\n" - "precision highp image3D;\n" - "precision highp imageCube;\n" - "precision highp image2DArray;\n" - - "precision highp iimage2D;\n" - "precision highp iimage3D;\n" - "precision highp iimageCube;\n" - "precision highp iimage2DArray;\n" - - "precision highp uimage2D;\n" - "precision highp uimage3D;\n" - "precision highp uimageCube;\n" - "precision highp uimage2DArray;\n" // clang-format off - ); // clang-format on - if (IsES32OrAbove) + if (deviceCaps.Features.ComputeShaders) { GLSLSource.append( - "precision highp imageBuffer;\n" - "precision highp iimageBuffer;\n" - "precision highp uimageBuffer;\n" // clang-format off + "precision highp image2D;\n" + "precision highp image3D;\n" + "precision highp imageCube;\n" + "precision highp image2DArray;\n" + + "precision highp iimage2D;\n" + "precision highp iimage3D;\n" + "precision highp iimageCube;\n" + "precision highp iimage2DArray;\n" + + "precision highp uimage2D;\n" + "precision highp uimage3D;\n" + "precision highp uimageCube;\n" + "precision highp uimage2DArray;\n" // clang-format off + ); // clang-format on + if (IsES32OrAbove) + { + GLSLSource.append( + "precision highp imageBuffer;\n" + "precision highp iimageBuffer;\n" + "precision highp uimageBuffer;\n" // clang-format off ); // clang-format on + } } - } - if (IsES30 && deviceCaps.Features.SeparablePrograms && ShaderType == SHADER_TYPE_VERTEX) - { - // From https://www.khronos.org/registry/OpenGL/extensions/EXT/EXT_separate_shader_objects.gles.txt: - // - // When using GLSL ES 3.00 shaders in separable programs, gl_Position and - // gl_PointSize built-in outputs must be redeclared according to Section 7.5 - // of the OpenGL Shading Language Specification... - // - // add to GLSL ES 3.00 new section 7.5, Built-In Redeclaration and - // Separable Programs: - // - // "The following vertex shader outputs may be redeclared at global scope to - // specify a built-in output interface, with or without special qualifiers: - // - // gl_Position - // gl_PointSize - // - // When compiling shaders using either of the above variables, both such - // variables must be redeclared prior to use. ((Note: This restriction - // applies only to shaders using version 300 that enable the - // EXT_separate_shader_objects extension; shaders not enabling the - // extension do not have this requirement.)) A separable program object - // will fail to link if any attached shader uses one of the above variables - // without redeclaration." - GLSLSource.append("out vec4 gl_Position;\n"); - } + if (IsES30 && deviceCaps.Features.SeparablePrograms && ShaderType == SHADER_TYPE_VERTEX) + { + // From https://www.khronos.org/registry/OpenGL/extensions/EXT/EXT_separate_shader_objects.gles.txt: + // + // When using GLSL ES 3.00 shaders in separable programs, gl_Position and + // gl_PointSize built-in outputs must be redeclared according to Section 7.5 + // of the OpenGL Shading Language Specification... + // + // add to GLSL ES 3.00 new section 7.5, Built-In Redeclaration and + // Separable Programs: + // + // "The following vertex shader outputs may be redeclared at global scope to + // specify a built-in output interface, with or without special qualifiers: + // + // gl_Position + // gl_PointSize + // + // When compiling shaders using either of the above variables, both such + // variables must be redeclared prior to use. ((Note: This restriction + // applies only to shaders using version 300 that enable the + // EXT_separate_shader_objects extension; shaders not enabling the + // extension do not have this requirement.)) A separable program object + // will fail to link if any attached shader uses one of the above variables + // without redeclaration." + GLSLSource.append("out vec4 gl_Position;\n"); + } #elif # error "Undefined platform" #endif - // It would be much more convenient to use row_major matrices. - // But unfortunatelly on NVIDIA, the following directive - // layout(std140, row_major) uniform; - // does not have any effect on matrices that are part of structures - // So we have to use column-major matrices which are default in both - // DX and GLSL. - GLSLSource.append( - "layout(std140) uniform;\n"); + // It would be much more convenient to use row_major matrices. + // But unfortunatelly on NVIDIA, the following directive + // layout(std140, row_major) uniform; + // does not have any effect on matrices that are part of structures + // So we have to use column-major matrices which are default in both + // DX and GLSL. + GLSLSource.append( + "layout(std140) uniform;\n"); - if (ShaderType == SHADER_TYPE_VERTEX && TargetCompiler == TargetGLSLCompiler::glslang) - { - // https://github.com/KhronosGroup/GLSL/blob/master/extensions/khr/GL_KHR_vulkan_glsl.txt - GLSLSource.append("#define gl_VertexID gl_VertexIndex\n" - "#define gl_InstanceID gl_InstanceIndex\n"); - } + if (ShaderType == SHADER_TYPE_VERTEX && TargetCompiler == TargetGLSLCompiler::glslang) + { + // https://github.com/KhronosGroup/GLSL/blob/master/extensions/khr/GL_KHR_vulkan_glsl.txt + GLSLSource.append("#define gl_VertexID gl_VertexIndex\n" + "#define gl_InstanceID gl_InstanceIndex\n"); + } - const Char* ShaderTypeDefine = nullptr; - switch (ShaderType) - { - // clang-format off - case SHADER_TYPE_VERTEX: ShaderTypeDefine = "#define VERTEX_SHADER\n"; break; - case SHADER_TYPE_PIXEL: ShaderTypeDefine = "#define FRAGMENT_SHADER\n"; break; - case SHADER_TYPE_GEOMETRY: ShaderTypeDefine = "#define GEOMETRY_SHADER\n"; break; - case SHADER_TYPE_HULL: ShaderTypeDefine = "#define TESS_CONTROL_SHADER\n"; break; - case SHADER_TYPE_DOMAIN: ShaderTypeDefine = "#define TESS_EVALUATION_SHADER\n"; break; - case SHADER_TYPE_COMPUTE: ShaderTypeDefine = "#define COMPUTE_SHADER\n"; break; - default: UNEXPECTED("Shader type is not specified"); - // clang-format on - } - GLSLSource += ShaderTypeDefine; + const Char* ShaderTypeDefine = nullptr; + switch (ShaderType) + { + // clang-format off + case SHADER_TYPE_VERTEX: ShaderTypeDefine = "#define VERTEX_SHADER\n"; break; + case SHADER_TYPE_PIXEL: ShaderTypeDefine = "#define FRAGMENT_SHADER\n"; break; + case SHADER_TYPE_GEOMETRY: ShaderTypeDefine = "#define GEOMETRY_SHADER\n"; break; + case SHADER_TYPE_HULL: ShaderTypeDefine = "#define TESS_CONTROL_SHADER\n"; break; + case SHADER_TYPE_DOMAIN: ShaderTypeDefine = "#define TESS_EVALUATION_SHADER\n"; break; + case SHADER_TYPE_COMPUTE: ShaderTypeDefine = "#define COMPUTE_SHADER\n"; break; + default: UNEXPECTED("Shader type is not specified"); + // clang-format on + } + GLSLSource += ShaderTypeDefine; - if (ExtraDefinitions != nullptr) - { - GLSLSource.append(ExtraDefinitions); - } + if (ExtraDefinitions != nullptr) + { + GLSLSource.append(ExtraDefinitions); + } - if (CreationAttribs.Macros != nullptr) - { - auto* pMacro = CreationAttribs.Macros; - while (pMacro->Name != nullptr && pMacro->Definition != nullptr) + if (CreationAttribs.Macros != nullptr) { - GLSLSource += "#define "; - GLSLSource += pMacro->Name; - GLSLSource += ' '; - GLSLSource += pMacro->Definition; - GLSLSource += "\n"; - ++pMacro; + auto* pMacro = CreationAttribs.Macros; + while (pMacro->Name != nullptr && pMacro->Definition != nullptr) + { + GLSLSource += "#define "; + GLSLSource += pMacro->Name; + GLSLSource += ' '; + GLSLSource += pMacro->Definition; + GLSLSource += "\n"; + ++pMacro; + } } } |
