Support mediump in desktop Vulkan GLSL.

This commit is contained in:
Hans-Kristian Arntzen 2017-12-06 10:25:30 +01:00
parent ed461b44d9
commit 2e68675ef7
9 changed files with 96 additions and 18 deletions

View File

@ -0,0 +1,12 @@
#version 450
layout(location = 0) out vec4 FragColor;
layout(location = 0) in vec4 F;
layout(location = 1) flat in ivec4 I;
layout(location = 2) flat in uvec4 U;
void main()
{
FragColor = (F + vec4(I)) + vec4(U);
}

View File

@ -0,0 +1,12 @@
#version 450
layout(location = 0) out mediump vec4 FragColor;
layout(location = 0) in mediump vec4 F;
layout(location = 1) flat in mediump ivec4 I;
layout(location = 2) flat in mediump uvec4 U;
void main()
{
FragColor = (F + vec4(I)) + vec4(U);
}

View File

@ -0,0 +1,12 @@
#version 450
layout(location = 0) out vec4 FragColor;
layout(location = 0) in vec4 F;
layout(location = 1) flat in ivec4 I;
layout(location = 2) flat in uvec4 U;
void main()
{
FragColor = (F + vec4(I)) + vec4(U);
}

View File

@ -0,0 +1,12 @@
#version 450
layout(location = 0) out mediump vec4 FragColor;
layout(location = 0) in mediump vec4 F;
layout(location = 1) flat in mediump ivec4 I;
layout(location = 2) flat in mediump uvec4 U;
void main()
{
FragColor = (F + vec4(I)) + vec4(U);
}

View File

@ -0,0 +1,11 @@
#version 450
layout(location = 0) in mediump vec4 F;
layout(location = 1) flat in mediump ivec4 I;
layout(location = 2) flat in mediump uvec4 U;
layout(location = 0) out mediump vec4 FragColor;
void main()
{
FragColor = F + vec4(I) + vec4(U);
}

View File

@ -368,6 +368,9 @@ string CompilerGLSL::compile()
// Force a classic "C" locale, reverts when function returns
ClassicLocale classic_locale;
if (options.vulkan_semantics)
backend.allow_precision_qualifiers = true;
// Scan the SPIR-V to find trivial uses of extensions.
find_static_extensions();
fixup_image_load_store_access();
@ -6860,16 +6863,16 @@ void CompilerGLSL::emit_struct_member(const SPIRType &type, uint32_t member_type
const char *CompilerGLSL::flags_to_precision_qualifiers_glsl(const SPIRType &type, uint64_t flags)
{
// Structs do not have precision qualifiers, neither do doubles (desktop only anyways, so no mediump/highp).
if (type.basetype != SPIRType::Float && type.basetype != SPIRType::Int && type.basetype != SPIRType::UInt &&
type.basetype != SPIRType::Image && type.basetype != SPIRType::SampledImage &&
type.basetype != SPIRType::Sampler)
return "";
if (options.es)
{
auto &execution = get_entry_point();
// Structs do not have precision qualifiers, neither do doubles (desktop only anyways, so no mediump/highp).
if (type.basetype != SPIRType::Float && type.basetype != SPIRType::Int && type.basetype != SPIRType::UInt &&
type.basetype != SPIRType::Image && type.basetype != SPIRType::SampledImage &&
type.basetype != SPIRType::Sampler)
return "";
if (flags & (1ull << DecorationRelaxedPrecision))
{
bool implied_fmediump = type.basetype == SPIRType::Float &&
@ -6897,6 +6900,19 @@ const char *CompilerGLSL::flags_to_precision_qualifiers_glsl(const SPIRType &typ
return implied_fhighp || implied_ihighp ? "" : "highp ";
}
}
else if (backend.allow_precision_qualifiers)
{
// Vulkan GLSL supports precision qualifiers, even in desktop profiles, which is convenient.
// The default is highp however, so only emit mediump in the rare case that a shader has these.
if (flags & (1ull << DecorationRelaxedPrecision))
{
bool can_use_mediump =
type.basetype == SPIRType::Float || type.basetype == SPIRType::Int || type.basetype == SPIRType::UInt;
return can_use_mediump ? "mediump " : "";
}
else
return "";
}
else
return "";
}

View File

@ -319,6 +319,7 @@ protected:
bool native_row_major_matrix = true;
bool use_constructor_splatting = true;
bool boolean_mix_support = true;
bool allow_precision_qualifiers = false;
} backend;
void emit_struct(SPIRType &type);

View File

@ -7,9 +7,10 @@ export PATH="./external/glslang-build/StandAlone:./external/spirv-tools-build/to
echo "Using glslangValidation in: $(which glslangValidator)."
echo "Using spirv-opt in: $(which spirv-opt)."
./test_shaders.py shaders
./test_shaders.py shaders --opt
./test_shaders.py shaders-msl --msl
./test_shaders.py shaders-msl --msl --opt
./test_shaders.py shaders-hlsl --hlsl
./test_shaders.py shaders-hlsl --hlsl --opt
./test_shaders.py shaders --update || exit 1
./test_shaders.py shaders --update --opt || exit 1
./test_shaders.py shaders-msl --msl --update || exit 1
./test_shaders.py shaders-msl --msl --update --opt || exit 1
./test_shaders.py shaders-hlsl --hlsl --update || exit 1
./test_shaders.py shaders-hlsl --hlsl --update --opt || exit 1

View File

@ -7,9 +7,10 @@ export PATH="./external/glslang-build/StandAlone:./external/spirv-tools-build/to
echo "Using glslangValidation in: $(which glslangValidator)."
echo "Using spirv-opt in: $(which spirv-opt)."
./test_shaders.py shaders --update
./test_shaders.py shaders --update --opt
./test_shaders.py shaders-msl --msl --update
./test_shaders.py shaders-msl --msl --update --opt
./test_shaders.py shaders-hlsl --hlsl --update
./test_shaders.py shaders-hlsl --hlsl --update --opt
./test_shaders.py shaders --update || exit 1
./test_shaders.py shaders --update --opt || exit 1
./test_shaders.py shaders-msl --msl --update || exit 1
./test_shaders.py shaders-msl --msl --update --opt || exit 1
./test_shaders.py shaders-hlsl --hlsl --update || exit 1
./test_shaders.py shaders-hlsl --hlsl --update --opt || exit 1