This commit is contained in:
Bill Hollings 2017-10-02 17:27:34 -04:00
commit bd021395f0
28 changed files with 1093 additions and 147 deletions

View File

@ -8,9 +8,9 @@ osx_image: xcode8.2
sudo: required
dist: trusty
# We check out glslang at a specific revision to avoid test output mismatches
# We check out glslang and SPIRV-Tools at specific revisions to avoid test output mismatches
env:
- GLSLANG_REV=f0bc598dd7871689f25514b22a82f7455d762bef
- GLSLANG_REV=f0bc598dd7871689f25514b22a82f7455d762bef SPIRV_TOOLS_REV=40e9c60ffea56f45f388835e6945b01d4d8b022d
before_script:
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew install python3; fi
@ -20,6 +20,7 @@ before_script:
script:
- git -C glslang checkout $GLSLANG_REV
- git -C SPIRV-Tools checkout $SPIRV_TOOLS_REV
- cd glslang && cmake . && make -j2 && cd ..
- cd SPIRV-Tools && cmake . && make -j2 && cd ..
- make -j2

View File

@ -102,6 +102,9 @@ spirv_cross_add_library(spirv-cross-hlsl spirv_cross_hlsl STATIC
${CMAKE_CURRENT_SOURCE_DIR}/spirv_hlsl.cpp)
add_executable(spirv-cross main.cpp)
target_compile_options(spirv-cross PRIVATE ${spirv-compiler-options})
target_compile_definitions(spirv-cross PRIVATE ${spirv-compiler-defines})
install(TARGETS spirv-cross RUNTIME DESTINATION bin)
target_link_libraries(spirv-cross spirv-cross-glsl spirv-cross-hlsl spirv-cross-cpp spirv-cross-msl spirv-cross-core)
target_link_libraries(spirv-cross-glsl spirv-cross-core)

View File

@ -37,12 +37,12 @@ using namespace spirv_cross;
using namespace std;
#ifdef SPIRV_CROSS_EXCEPTIONS_TO_ASSERTIONS
#define THROW(x) \
do \
{ \
fprintf(stderr, "%s.", x); \
exit(1); \
} while (0)
static inline void THROW(const char *str)
{
fprintf(stderr, "SPIRV-Cross will abort: %s\n", str);
fflush(stderr);
abort();
}
#else
#define THROW(x) throw runtime_error(x)
#endif
@ -472,6 +472,7 @@ struct CLIArguments
bool hlsl_compat = false;
bool vulkan_semantics = false;
bool flatten_multidimensional_arrays = false;
bool use_420pack_extension = true;
bool remove_unused = false;
bool cfg_analysis = true;
};
@ -487,7 +488,7 @@ static void print_help()
"[--separate-shader-objects]"
"[--pls-in format input-name] [--pls-out format output-name] [--remap source_name target_name "
"components] [--extension ext] [--entry name] [--remove-unused-variables] "
"[--flatten-multidimensional-arrays] "
"[--flatten-multidimensional-arrays] [--no-420pack-extension] "
"[--remap-variable-type <variable_name> <new_variable_type>] "
"[--rename-interface-variable <in|out> <location> <new_variable_name>] "
"\n");
@ -593,7 +594,7 @@ void rename_interface_variable(Compiler &compiler, const vector<Resource> &resou
}
}
int main(int argc, char *argv[])
static int main_inner(int argc, char *argv[])
{
CLIArguments args;
CLICallbacks cbs;
@ -630,6 +631,7 @@ int main(int argc, char *argv[])
cbs.add("--hlsl-enable-compat", [&args](CLIParser &) { args.hlsl_compat = true; });
cbs.add("--vulkan-semantics", [&args](CLIParser &) { args.vulkan_semantics = true; });
cbs.add("--flatten-multidimensional-arrays", [&args](CLIParser &) { args.flatten_multidimensional_arrays = true; });
cbs.add("--no-420pack-extension", [&args](CLIParser &) { args.use_420pack_extension = false; });
cbs.add("--extension", [&args](CLIParser &parser) { args.extensions.push_back(parser.next_string()); });
cbs.add("--entry", [&args](CLIParser &parser) { args.entry = parser.next_string(); });
cbs.add("--separate-shader-objects", [&args](CLIParser &) { args.sso = true; });
@ -751,6 +753,7 @@ int main(int argc, char *argv[])
opts.force_temporary = args.force_temporary;
opts.separate_shader_objects = args.sso;
opts.flatten_multidimensional_arrays = args.flatten_multidimensional_arrays;
opts.enable_420pack_extension = args.use_420pack_extension;
opts.vulkan_semantics = args.vulkan_semantics;
opts.vertex.fixup_clipspace = args.fixup;
opts.vertex.flip_vert_y = args.yflip;
@ -856,4 +859,24 @@ int main(int argc, char *argv[])
write_string_to_file(args.output, glsl.c_str());
else
printf("%s", glsl.c_str());
return EXIT_SUCCESS;
}
int main(int argc, char *argv[])
{
#ifdef SPIRV_CROSS_EXCEPTIONS_TO_ASSERTIONS
return main_inner(argc, argv);
#else
// Make sure we catch the exception or it just disappears into the aether on Windows.
try
{
return main_inner(argc, argv);
}
catch (const std::exception &e)
{
fprintf(stderr, "SPIRV-Cross threw an exception: %s\n", e.what());
return EXIT_FAILURE;
}
#endif
}

View File

@ -6,7 +6,7 @@ struct _CBO
float4 d;
};
cbuffer CBO : register(c4)
cbuffer CBO : register(b4)
{
_CBO cbo[2][4];
};

View File

@ -0,0 +1,145 @@
Texture1D<uint4> uSampler1DUint : register(t0);
SamplerState _uSampler1DUint_sampler : register(s0);
Texture1D<int4> uSampler1DInt : register(t0);
SamplerState _uSampler1DInt_sampler : register(s0);
Texture1D<float4> uSampler1DFloat : register(t0);
SamplerState _uSampler1DFloat_sampler : register(s0);
Texture2DArray<int4> uSampler2DArray : register(t2);
SamplerState _uSampler2DArray_sampler : register(s2);
Texture3D<float4> uSampler3D : register(t3);
SamplerState _uSampler3D_sampler : register(s3);
TextureCube<float4> uSamplerCube : register(t4);
SamplerState _uSamplerCube_sampler : register(s4);
TextureCubeArray<uint4> uSamplerCubeArray : register(t5);
SamplerState _uSamplerCubeArray_sampler : register(s5);
Buffer<float4> uSamplerBuffer : register(t6);
Texture2DMS<int4> uSamplerMS : register(t7);
SamplerState _uSamplerMS_sampler : register(s7);
Texture2DMSArray<float4> uSamplerMSArray : register(t8);
SamplerState _uSamplerMSArray_sampler : register(s8);
Texture2D<float4> uSampler2D : register(t1);
SamplerState _uSampler2D_sampler : register(s1);
uint SPIRV_Cross_textureSize(Texture1D<float4> Tex, uint Level, out uint Param)
{
uint ret;
Tex.GetDimensions(Level, ret.x, Param);
return ret;
}
uint SPIRV_Cross_textureSize(Texture1D<int4> Tex, uint Level, out uint Param)
{
uint ret;
Tex.GetDimensions(Level, ret.x, Param);
return ret;
}
uint SPIRV_Cross_textureSize(Texture1D<uint4> Tex, uint Level, out uint Param)
{
uint ret;
Tex.GetDimensions(Level, ret.x, Param);
return ret;
}
uint2 SPIRV_Cross_textureSize(Texture2D<float4> Tex, uint Level, out uint Param)
{
uint2 ret;
Tex.GetDimensions(Level, ret.x, ret.y, Param);
return ret;
}
uint3 SPIRV_Cross_textureSize(Texture2DArray<int4> Tex, uint Level, out uint Param)
{
uint3 ret;
Tex.GetDimensions(Level, ret.x, ret.y, ret.z, Param);
return ret;
}
uint3 SPIRV_Cross_textureSize(Texture3D<float4> Tex, uint Level, out uint Param)
{
uint3 ret;
Tex.GetDimensions(Level, ret.x, ret.y, ret.z, Param);
return ret;
}
uint SPIRV_Cross_textureSize(Buffer<float4> Tex, uint Level, out uint Param)
{
uint ret;
Tex.GetDimensions(ret.x);
return ret;
}
uint2 SPIRV_Cross_textureSize(TextureCube<float4> Tex, uint Level, out uint Param)
{
uint2 ret;
Tex.GetDimensions(Level, ret.x, ret.y, Param);
return ret;
}
uint3 SPIRV_Cross_textureSize(TextureCubeArray<uint4> Tex, uint Level, out uint Param)
{
uint3 ret;
Tex.GetDimensions(Level, ret.x, ret.y, ret.z, Param);
return ret;
}
uint2 SPIRV_Cross_textureSize(Texture2DMS<int4> Tex, uint Level, out uint Param)
{
uint2 ret;
Tex.GetDimensions(ret.x, ret.y, Param);
return ret;
}
uint3 SPIRV_Cross_textureSize(Texture2DMSArray<float4> Tex, uint Level, out uint Param)
{
uint3 ret;
Tex.GetDimensions(ret.x, ret.y, ret.z, Param);
return ret;
}
void frag_main()
{
uint _17_dummy_parameter;
int a = int(SPIRV_Cross_textureSize(uSampler1DUint, uint(0), _17_dummy_parameter));
uint _24_dummy_parameter;
a = int(SPIRV_Cross_textureSize(uSampler1DInt, uint(0), _24_dummy_parameter));
uint _32_dummy_parameter;
a = int(SPIRV_Cross_textureSize(uSampler1DFloat, uint(0), _32_dummy_parameter));
uint _42_dummy_parameter;
int3 c = int3(SPIRV_Cross_textureSize(uSampler2DArray, uint(0), _42_dummy_parameter));
uint _50_dummy_parameter;
int3 d = int3(SPIRV_Cross_textureSize(uSampler3D, uint(0), _50_dummy_parameter));
uint _60_dummy_parameter;
int2 e = int2(SPIRV_Cross_textureSize(uSamplerCube, uint(0), _60_dummy_parameter));
uint _68_dummy_parameter;
int3 f = int3(SPIRV_Cross_textureSize(uSamplerCubeArray, uint(0), _68_dummy_parameter));
uint _76_dummy_parameter;
int g = int(SPIRV_Cross_textureSize(uSamplerBuffer, 0u, _76_dummy_parameter));
uint _84_dummy_parameter;
int2 h = int2(SPIRV_Cross_textureSize(uSamplerMS, 0u, _84_dummy_parameter));
uint _92_dummy_parameter;
int3 i = int3(SPIRV_Cross_textureSize(uSamplerMSArray, 0u, _92_dummy_parameter));
int _100;
SPIRV_Cross_textureSize(uSampler2D, 0u, _100);
int l1 = int(_100);
int _104;
SPIRV_Cross_textureSize(uSampler2DArray, 0u, _104);
int l2 = int(_104);
int _108;
SPIRV_Cross_textureSize(uSampler3D, 0u, _108);
int l3 = int(_108);
int _112;
SPIRV_Cross_textureSize(uSamplerCube, 0u, _112);
int l4 = int(_112);
int _116;
SPIRV_Cross_textureSize(uSamplerMS, 0u, _116);
int s0 = int(_116);
int _120;
SPIRV_Cross_textureSize(uSamplerMSArray, 0u, _120);
int s1 = int(_120);
}
void main()
{
frag_main();
}

View File

@ -0,0 +1,131 @@
Texture1D<float4> uSampler1D : register(t0);
SamplerState _uSampler1D_sampler : register(s0);
Texture2D<float4> uSampler2D : register(t1);
SamplerState _uSampler2D_sampler : register(s1);
Texture2DArray<float4> uSampler2DArray : register(t2);
SamplerState _uSampler2DArray_sampler : register(s2);
Texture3D<float4> uSampler3D : register(t3);
SamplerState _uSampler3D_sampler : register(s3);
TextureCube<float4> uSamplerCube : register(t4);
SamplerState _uSamplerCube_sampler : register(s4);
TextureCubeArray<float4> uSamplerCubeArray : register(t5);
SamplerState _uSamplerCubeArray_sampler : register(s5);
Buffer<float4> uSamplerBuffer : register(t6);
Texture2DMS<float4> uSamplerMS : register(t7);
SamplerState _uSamplerMS_sampler : register(s7);
Texture2DMSArray<float4> uSamplerMSArray : register(t8);
SamplerState _uSamplerMSArray_sampler : register(s8);
uint SPIRV_Cross_textureSize(Texture1D<float4> Tex, uint Level, out uint Param)
{
uint ret;
Tex.GetDimensions(Level, ret.x, Param);
return ret;
}
uint2 SPIRV_Cross_textureSize(Texture2D<float4> Tex, uint Level, out uint Param)
{
uint2 ret;
Tex.GetDimensions(Level, ret.x, ret.y, Param);
return ret;
}
uint3 SPIRV_Cross_textureSize(Texture2DArray<float4> Tex, uint Level, out uint Param)
{
uint3 ret;
Tex.GetDimensions(Level, ret.x, ret.y, ret.z, Param);
return ret;
}
uint3 SPIRV_Cross_textureSize(Texture3D<float4> Tex, uint Level, out uint Param)
{
uint3 ret;
Tex.GetDimensions(Level, ret.x, ret.y, ret.z, Param);
return ret;
}
uint SPIRV_Cross_textureSize(Buffer<float4> Tex, uint Level, out uint Param)
{
uint ret;
Tex.GetDimensions(ret.x);
return ret;
}
uint2 SPIRV_Cross_textureSize(TextureCube<float4> Tex, uint Level, out uint Param)
{
uint2 ret;
Tex.GetDimensions(Level, ret.x, ret.y, Param);
return ret;
}
uint3 SPIRV_Cross_textureSize(TextureCubeArray<float4> Tex, uint Level, out uint Param)
{
uint3 ret;
Tex.GetDimensions(Level, ret.x, ret.y, ret.z, Param);
return ret;
}
uint2 SPIRV_Cross_textureSize(Texture2DMS<float4> Tex, uint Level, out uint Param)
{
uint2 ret;
Tex.GetDimensions(ret.x, ret.y, Param);
return ret;
}
uint3 SPIRV_Cross_textureSize(Texture2DMSArray<float4> Tex, uint Level, out uint Param)
{
uint3 ret;
Tex.GetDimensions(ret.x, ret.y, ret.z, Param);
return ret;
}
void frag_main()
{
uint _17_dummy_parameter;
int a = int(SPIRV_Cross_textureSize(uSampler1D, uint(0), _17_dummy_parameter));
uint _27_dummy_parameter;
int2 b = int2(SPIRV_Cross_textureSize(uSampler2D, uint(0), _27_dummy_parameter));
uint _37_dummy_parameter;
int3 c = int3(SPIRV_Cross_textureSize(uSampler2DArray, uint(0), _37_dummy_parameter));
uint _45_dummy_parameter;
int3 d = int3(SPIRV_Cross_textureSize(uSampler3D, uint(0), _45_dummy_parameter));
uint _53_dummy_parameter;
int2 e = int2(SPIRV_Cross_textureSize(uSamplerCube, uint(0), _53_dummy_parameter));
uint _61_dummy_parameter;
int3 f = int3(SPIRV_Cross_textureSize(uSamplerCubeArray, uint(0), _61_dummy_parameter));
uint _69_dummy_parameter;
int g = int(SPIRV_Cross_textureSize(uSamplerBuffer, 0u, _69_dummy_parameter));
uint _77_dummy_parameter;
int2 h = int2(SPIRV_Cross_textureSize(uSamplerMS, 0u, _77_dummy_parameter));
uint _85_dummy_parameter;
int3 i = int3(SPIRV_Cross_textureSize(uSamplerMSArray, 0u, _85_dummy_parameter));
int _89;
SPIRV_Cross_textureSize(uSampler1D, 0u, _89);
int l0 = int(_89);
int _93;
SPIRV_Cross_textureSize(uSampler2D, 0u, _93);
int l1 = int(_93);
int _97;
SPIRV_Cross_textureSize(uSampler2DArray, 0u, _97);
int l2 = int(_97);
int _101;
SPIRV_Cross_textureSize(uSampler3D, 0u, _101);
int l3 = int(_101);
int _105;
SPIRV_Cross_textureSize(uSamplerCube, 0u, _105);
int l4 = int(_105);
int _109;
SPIRV_Cross_textureSize(uSamplerCubeArray, 0u, _109);
int l5 = int(_109);
int _113;
SPIRV_Cross_textureSize(uSamplerMS, 0u, _113);
int s0 = int(_113);
int _117;
SPIRV_Cross_textureSize(uSamplerMSArray, 0u, _117);
int s1 = int(_117);
}
void main()
{
frag_main();
}

View File

@ -9,7 +9,7 @@ struct _UBO
int some_value;
};
cbuffer UBO : register(c0)
cbuffer UBO : register(b0)
{
_UBO _42;
};

View File

@ -3,7 +3,7 @@ struct _CBuffer
float4 a;
};
cbuffer CBuffer : register(c3)
cbuffer CBuffer : register(b3)
{
_CBuffer cbuf;
};

View File

@ -89,7 +89,16 @@ void frag_main()
texcolor += tex1dArray.Sample(_tex1dArray_sampler, texCoord2d);
texcolor += tex2dArray.Sample(_tex2dArray_sampler, texCoord3d);
texcolor += texCubeArray.Sample(_texCubeArray_sampler, texCoord4d);
texcolor += tex2d.Gather(_tex2d_sampler, texCoord2d, 0);
texcolor += tex2d.GatherRed(_tex2d_sampler, texCoord2d);
texcolor += tex2d.GatherRed(_tex2d_sampler, texCoord2d);
texcolor += tex2d.GatherGreen(_tex2d_sampler, texCoord2d);
texcolor += tex2d.GatherBlue(_tex2d_sampler, texCoord2d);
texcolor += tex2d.GatherAlpha(_tex2d_sampler, texCoord2d);
texcolor += tex2d.GatherRed(_tex2d_sampler, texCoord2d, int2(1, 1));
texcolor += tex2d.GatherRed(_tex2d_sampler, texCoord2d, int2(1, 1));
texcolor += tex2d.GatherGreen(_tex2d_sampler, texCoord2d, int2(1, 1));
texcolor += tex2d.GatherBlue(_tex2d_sampler, texCoord2d, int2(1, 1));
texcolor += tex2d.GatherAlpha(_tex2d_sampler, texCoord2d, int2(1, 1));
texcolor += tex2d.Load(int3(int2(1, 2), 0));
texcolor += separateTex2d.Sample(samplerNonDepth, texCoord2d);
texcolor.w += separateTex2dDepth.SampleCmp(samplerDepth, texCoord3d.xy, texCoord3d.z);

View File

@ -1,27 +0,0 @@
#version 310 es
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
layout(binding = 0, std430) buffer _3
{
ivec4 _m0;
uvec4 _m1;
} _5;
layout(binding = 1, std430) buffer _4
{
uvec4 _m0;
ivec4 _m1;
} _6;
void main()
{
_6._m0 = _5._m1 / uvec4(_5._m0);
_6._m0 = uvec4(_5._m0) / _5._m1;
_6._m0 = _5._m1 / _5._m1;
_6._m0 = uvec4(_5._m0) / uvec4(_5._m0);
_6._m1 = ivec4(_5._m1 / _5._m1);
_6._m1 = ivec4(uvec4(_5._m0) / uvec4(_5._m0));
_6._m1 = ivec4(_5._m1 / uvec4(_5._m0));
_6._m1 = ivec4(uvec4(_5._m0) / _5._m1);
}

View File

@ -0,0 +1,9 @@
#version 450
void main()
{
for (int _22 = 35; _22 >= 0; _22--)
{
}
}

View File

@ -0,0 +1,27 @@
#version 310 es
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
layout(binding = 0, std430) buffer SSBO
{
int a;
} _9;
void main()
{
_9.a += 10;
_9.a -= 10;
_9.a *= 10;
_9.a /= 10;
_9.a = _9.a << 2;
_9.a = _9.a >> 3;
_9.a &= 40;
_9.a ^= 10;
_9.a %= 40;
_9.a |= 1;
bool c = false;
bool d = true;
c = c && d;
d = d || c;
_9.a = int(c && d);
}

View File

@ -0,0 +1,53 @@
#version 450
layout(binding = 0) uniform sampler1D uSampler1D;
layout(binding = 1) uniform sampler2D uSampler2D;
layout(binding = 2) uniform sampler2DArray uSampler2DArray;
layout(binding = 3) uniform sampler3D uSampler3D;
layout(binding = 4) uniform samplerCube uSamplerCube;
layout(binding = 5) uniform samplerCubeArray uSamplerCubeArray;
layout(binding = 6) uniform samplerBuffer uSamplerBuffer;
layout(binding = 7) uniform sampler2DMS uSamplerMS;
layout(binding = 8) uniform sampler2DMSArray uSamplerMSArray;
layout(binding = 9, r32f) uniform readonly writeonly image1D uImage1D;
layout(binding = 10, r32f) uniform readonly writeonly image2D uImage2D;
layout(binding = 11, r32f) uniform readonly writeonly image2DArray uImage2DArray;
layout(binding = 12, r32f) uniform readonly writeonly image3D uImage3D;
layout(binding = 13, r32f) uniform readonly writeonly imageCube uImageCube;
layout(binding = 14, r32f) uniform readonly writeonly imageCubeArray uImageCubeArray;
layout(binding = 15, r32f) uniform readonly writeonly imageBuffer uImageBuffer;
layout(binding = 16, r32f) uniform readonly writeonly image2DMS uImageMS;
layout(binding = 17, r32f) uniform readonly writeonly image2DMSArray uImageMSArray;
void main()
{
int a = textureSize(uSampler1D, 0);
ivec2 b = textureSize(uSampler2D, 0);
ivec3 c = textureSize(uSampler2DArray, 0);
ivec3 d = textureSize(uSampler3D, 0);
ivec2 e = textureSize(uSamplerCube, 0);
ivec3 f = textureSize(uSamplerCubeArray, 0);
int g = textureSize(uSamplerBuffer);
ivec2 h = textureSize(uSamplerMS);
ivec3 i = textureSize(uSamplerMSArray);
int l0 = textureQueryLevels(uSampler1D);
int l1 = textureQueryLevels(uSampler2D);
int l2 = textureQueryLevels(uSampler2DArray);
int l3 = textureQueryLevels(uSampler3D);
int l4 = textureQueryLevels(uSamplerCube);
int l5 = textureQueryLevels(uSamplerCubeArray);
a = imageSize(uImage1D);
b = imageSize(uImage2D);
c = imageSize(uImage2DArray);
d = imageSize(uImage3D);
e = imageSize(uImageCube);
f = imageSize(uImageCubeArray);
g = imageSize(uImageBuffer);
h = imageSize(uImageMS);
i = imageSize(uImageMSArray);
int s0 = textureSamples(uSamplerMS);
int s1 = textureSamples(uSamplerMSArray);
int s2 = imageSamples(uImageMS);
int s3 = imageSamples(uImageMSArray);
}

View File

@ -0,0 +1,43 @@
#version 310 es
precision mediump float;
precision highp int;
struct ResType
{
highp float _m0;
int _m1;
};
struct ResType_1
{
highp vec2 _m0;
ivec2 _m1;
};
layout(location = 0) in float v0;
layout(location = 1) in vec2 v1;
layout(location = 0) out float FragColor;
void main()
{
ResType _16;
_16._m0 = frexp(v0, _16._m1);
mediump int e0 = _16._m1;
float f0 = _16._m0;
ResType _22;
_22._m0 = frexp(v0 + 1.0, _22._m1);
e0 = _22._m1;
f0 = _22._m0;
ResType_1 _35;
_35._m0 = frexp(v1, _35._m1);
mediump ivec2 e1 = _35._m1;
vec2 f1 = _35._m0;
float r0;
float _41 = modf(v0, r0);
float m0 = _41;
vec2 r1;
vec2 _45 = modf(v1, r1);
vec2 m1 = _45;
FragColor = ((((f0 + f1.x) + f1.y) + m0) + m1.x) + m1.y;
}

View File

@ -0,0 +1,35 @@
#version 450
layout(binding = 0) uniform usampler1D uSampler1DUint;
layout(binding = 0) uniform isampler1D uSampler1DInt;
layout(binding = 0) uniform sampler1D uSampler1DFloat;
layout(binding = 1) uniform sampler2D uSampler2D;
layout(binding = 2) uniform isampler2DArray uSampler2DArray;
layout(binding = 3) uniform sampler3D uSampler3D;
layout(binding = 4) uniform samplerCube uSamplerCube;
layout(binding = 5) uniform usamplerCubeArray uSamplerCubeArray;
layout(binding = 6) uniform samplerBuffer uSamplerBuffer;
layout(binding = 7) uniform isampler2DMS uSamplerMS;
layout(binding = 8) uniform sampler2DMSArray uSamplerMSArray;
void main()
{
int a = textureSize(uSampler1DUint, 0);
a = textureSize(uSampler1DInt, 0);
a = textureSize(uSampler1DFloat, 0);
ivec3 c = textureSize(uSampler2DArray, 0);
ivec3 d = textureSize(uSampler3D, 0);
ivec2 e = textureSize(uSamplerCube, 0);
ivec3 f = textureSize(uSamplerCubeArray, 0);
int g = textureSize(uSamplerBuffer);
ivec2 h = textureSize(uSamplerMS);
ivec3 i = textureSize(uSamplerMSArray);
int l1 = textureQueryLevels(uSampler2D);
int l2 = textureQueryLevels(uSampler2DArray);
int l3 = textureQueryLevels(uSampler3D);
int l4 = textureQueryLevels(uSamplerCube);
int s0 = textureSamples(uSamplerMS);
int s1 = textureSamples(uSamplerMSArray);
}

View File

@ -0,0 +1,33 @@
#version 450
layout(binding = 0) uniform sampler1D uSampler1D;
layout(binding = 1) uniform sampler2D uSampler2D;
layout(binding = 2) uniform sampler2DArray uSampler2DArray;
layout(binding = 3) uniform sampler3D uSampler3D;
layout(binding = 4) uniform samplerCube uSamplerCube;
layout(binding = 5) uniform samplerCubeArray uSamplerCubeArray;
layout(binding = 6) uniform samplerBuffer uSamplerBuffer;
layout(binding = 7) uniform sampler2DMS uSamplerMS;
layout(binding = 8) uniform sampler2DMSArray uSamplerMSArray;
void main()
{
int a = textureSize(uSampler1D, 0);
ivec2 b = textureSize(uSampler2D, 0);
ivec3 c = textureSize(uSampler2DArray, 0);
ivec3 d = textureSize(uSampler3D, 0);
ivec2 e = textureSize(uSamplerCube, 0);
ivec3 f = textureSize(uSamplerCubeArray, 0);
int g = textureSize(uSamplerBuffer);
ivec2 h = textureSize(uSamplerMS);
ivec3 i = textureSize(uSamplerMSArray);
int l0 = textureQueryLevels(uSampler1D);
int l1 = textureQueryLevels(uSampler2D);
int l2 = textureQueryLevels(uSampler2DArray);
int l3 = textureQueryLevels(uSampler3D);
int l4 = textureQueryLevels(uSamplerCube);
int l5 = textureQueryLevels(uSamplerCubeArray);
int s0 = textureSamples(uSamplerMS);
int s1 = textureSamples(uSamplerMSArray);
}

View File

@ -61,6 +61,16 @@ void main()
texcolor += texture(texCubeArray, texCoord4d);
texcolor += textureGather(tex2d, texCoord2d);
texcolor += textureGather(tex2d, texCoord2d, 0);
texcolor += textureGather(tex2d, texCoord2d, 1);
texcolor += textureGather(tex2d, texCoord2d, 2);
texcolor += textureGather(tex2d, texCoord2d, 3);
texcolor += textureGatherOffset(tex2d, texCoord2d, ivec2(1, 1));
texcolor += textureGatherOffset(tex2d, texCoord2d, ivec2(1, 1), 0);
texcolor += textureGatherOffset(tex2d, texCoord2d, ivec2(1, 1), 1);
texcolor += textureGatherOffset(tex2d, texCoord2d, ivec2(1, 1), 2);
texcolor += textureGatherOffset(tex2d, texCoord2d, ivec2(1, 1), 3);
texcolor += texelFetch(tex2d, ivec2(1, 2), 0);

View File

@ -1,77 +0,0 @@
; SPIR-V
; Version: 1.0
; Generator: Khronos Glslang Reference Front End; 1
; Bound: 30
; Schema: 0
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %func "main"
OpExecutionMode %func LocalSize 1 1 1
OpSource ESSL 310
OpSourceExtension "GL_GOOGLE_cpp_style_line_directive"
OpSourceExtension "GL_GOOGLE_include_directive"
OpMemberDecorate %input_struct 0 Offset 0
OpMemberDecorate %input_struct 1 Offset 16
OpMemberDecorate %output_struct 0 Offset 0
OpMemberDecorate %output_struct 1 Offset 16
OpDecorate %input_struct BufferBlock
OpDecorate %inputs DescriptorSet 0
OpDecorate %inputs Binding 0
OpDecorate %output_struct BufferBlock
OpDecorate %outputs DescriptorSet 0
OpDecorate %outputs Binding 1
%void = OpTypeVoid
%main_func = OpTypeFunction %void
%uint = OpTypeInt 32 0
%uvec4 = OpTypeVector %uint 4
%int = OpTypeInt 32 1
%ivec4 = OpTypeVector %int 4
%ivec4_ptr = OpTypePointer Uniform %ivec4
%uvec4_ptr = OpTypePointer Uniform %uvec4
%zero = OpConstant %int 0
%one = OpConstant %int 1
%input_struct = OpTypeStruct %ivec4 %uvec4
%input_struct_ptr = OpTypePointer Uniform %input_struct
%inputs = OpVariable %input_struct_ptr Uniform
%output_struct = OpTypeStruct %uvec4 %ivec4
%output_struct_ptr = OpTypePointer Uniform %output_struct
%outputs = OpVariable %output_struct_ptr Uniform
%func = OpFunction %void None %main_func
%block = OpLabel
%input1_ptr = OpAccessChain %ivec4_ptr %inputs %zero
%input0_ptr = OpAccessChain %uvec4_ptr %inputs %one
%input1 = OpLoad %ivec4 %input1_ptr
%input0 = OpLoad %uvec4 %input0_ptr
%output_ptr_uvec4 = OpAccessChain %uvec4_ptr %outputs %zero
%output_ptr_ivec4 = OpAccessChain %ivec4_ptr %outputs %one
; Test all variants of UDiv
%result_iadd_0 = OpUDiv %uvec4 %input0 %input1
%result_iadd_1 = OpUDiv %uvec4 %input1 %input0
%result_iadd_2 = OpUDiv %uvec4 %input0 %input0
%result_iadd_3 = OpUDiv %uvec4 %input1 %input1
%result_iadd_4 = OpUDiv %ivec4 %input0 %input0
%result_iadd_5 = OpUDiv %ivec4 %input1 %input1
%result_iadd_6 = OpUDiv %ivec4 %input0 %input1
%result_iadd_7 = OpUDiv %ivec4 %input1 %input0
OpStore %output_ptr_uvec4 %result_iadd_0
OpStore %output_ptr_uvec4 %result_iadd_1
OpStore %output_ptr_uvec4 %result_iadd_2
OpStore %output_ptr_uvec4 %result_iadd_3
OpStore %output_ptr_ivec4 %result_iadd_4
OpStore %output_ptr_ivec4 %result_iadd_5
OpStore %output_ptr_ivec4 %result_iadd_6
OpStore %output_ptr_ivec4 %result_iadd_7
OpReturn
OpFunctionEnd

View File

@ -0,0 +1,71 @@
; SPIR-V
; Version: 1.0
; Generator: Khronos Glslang Reference Front End; 1
; Bound: 59
; Schema: 0
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %4 "main"
OpExecutionMode %4 OriginUpperLeft
%void = OpTypeVoid
%3 = OpTypeFunction %void
%float = OpTypeFloat 32
%v2float = OpTypeVector %float 2
%mat2v2float = OpTypeMatrix %v2float 2
%_ptr_Function_mat2v2float = OpTypePointer Function %mat2v2float
%v3float = OpTypeVector %float 3
%11 = OpTypeFunction %v3float %_ptr_Function_mat2v2float
%_ptr_Function_v3float = OpTypePointer Function %v3float
%float_1 = OpConstant %float 1
%18 = OpConstantComposite %v3float %float_1 %float_1 %float_1
%int = OpTypeInt 32 1
%_ptr_Function_int = OpTypePointer Function %int
%int_35 = OpConstant %int 35
%int_0 = OpConstant %int 0
%bool = OpTypeBool
%int_1 = OpConstant %int 1
%4 = OpFunction %void None %3
%5 = OpLabel
OpBranch %48
%48 = OpLabel
%58 = OpPhi %int %int_35 %5 %56 %50
OpLoopMerge %49 %50 None
OpBranch %51
%51 = OpLabel
%53 = OpSGreaterThanEqual %bool %58 %int_0
OpBranchConditional %53 %54 %49
%54 = OpLabel
OpBranch %50
%50 = OpLabel
%56 = OpISub %int %58 %int_1
OpBranch %48
%49 = OpLabel
OpReturn
OpFunctionEnd
%13 = OpFunction %v3float None %11
%12 = OpFunctionParameter %_ptr_Function_mat2v2float
%14 = OpLabel
%16 = OpVariable %_ptr_Function_v3float Function
%21 = OpVariable %_ptr_Function_int Function
OpStore %16 %18
OpStore %21 %int_35
OpBranch %23
%23 = OpLabel
OpLoopMerge %25 %26 None
OpBranch %27
%27 = OpLabel
%28 = OpLoad %int %21
%31 = OpSGreaterThanEqual %bool %28 %int_0
OpBranchConditional %31 %24 %25
%24 = OpLabel
OpBranch %26
%26 = OpLabel
%32 = OpLoad %int %21
%34 = OpISub %int %32 %int_1
OpStore %21 %34
OpBranch %23
%25 = OpLabel
%35 = OpLoad %v3float %16
OpReturnValue %35
OpFunctionEnd

27
shaders/comp/rmw-opt.comp Normal file
View File

@ -0,0 +1,27 @@
#version 310 es
layout(local_size_x = 1) in;
layout(std430, binding = 0) buffer SSBO
{
int a;
};
void main()
{
a += 10;
a -= 10;
a *= 10;
a /= 10;
a <<= 2;
a >>= 3;
a &= 40;
a ^= 10;
a %= 40;
a |= 1;
bool c = false;
bool d = true;
c = c && d;
d = d || c;
a = c && d ? 1 : 0;
}

View File

@ -0,0 +1,56 @@
#version 450
layout(binding = 0) uniform sampler1D uSampler1D;
layout(binding = 1) uniform sampler2D uSampler2D;
layout(binding = 2) uniform sampler2DArray uSampler2DArray;
layout(binding = 3) uniform sampler3D uSampler3D;
layout(binding = 4) uniform samplerCube uSamplerCube;
layout(binding = 5) uniform samplerCubeArray uSamplerCubeArray;
layout(binding = 6) uniform samplerBuffer uSamplerBuffer;
layout(binding = 7) uniform sampler2DMS uSamplerMS;
layout(binding = 8) uniform sampler2DMSArray uSamplerMSArray;
layout(r32f, binding = 9) uniform image1D uImage1D;
layout(r32f, binding = 10) uniform image2D uImage2D;
layout(r32f, binding = 11) uniform image2DArray uImage2DArray;
layout(r32f, binding = 12) uniform image3D uImage3D;
layout(r32f, binding = 13) uniform imageCube uImageCube;
layout(r32f, binding = 14) uniform imageCubeArray uImageCubeArray;
layout(r32f, binding = 15) uniform imageBuffer uImageBuffer;
layout(r32f, binding = 16) uniform image2DMS uImageMS;
layout(r32f, binding = 17) uniform image2DMSArray uImageMSArray;
void main()
{
int a = textureSize(uSampler1D, 0);
ivec2 b = textureSize(uSampler2D, 0);
ivec3 c = textureSize(uSampler2DArray, 0);
ivec3 d = textureSize(uSampler3D, 0);
ivec2 e = textureSize(uSamplerCube, 0);
ivec3 f = textureSize(uSamplerCubeArray, 0);
int g = textureSize(uSamplerBuffer);
ivec2 h = textureSize(uSamplerMS);
ivec3 i = textureSize(uSamplerMSArray);
int l0 = textureQueryLevels(uSampler1D);
int l1 = textureQueryLevels(uSampler2D);
int l2 = textureQueryLevels(uSampler2DArray);
int l3 = textureQueryLevels(uSampler3D);
int l4 = textureQueryLevels(uSamplerCube);
int l5 = textureQueryLevels(uSamplerCubeArray);
a = imageSize(uImage1D);
b = imageSize(uImage2D);
c = imageSize(uImage2DArray);
d = imageSize(uImage3D);
e = imageSize(uImageCube);
f = imageSize(uImageCubeArray);
g = imageSize(uImageBuffer);
h = imageSize(uImageMS);
i = imageSize(uImageMSArray);
int s0 = textureSamples(uSamplerMS);
int s1 = textureSamples(uSamplerMSArray);
int s2 = imageSamples(uImageMS);
int s3 = imageSamples(uImageMSArray);
}

View File

@ -0,0 +1,24 @@
#version 310 es
precision mediump float;
layout(location = 0) out float FragColor;
layout(location = 0) in float v0;
layout(location = 1) in vec2 v1;
void main()
{
int e0;
float f0 = frexp(v0, e0);
f0 = frexp(v0 + 1.0, e0);
ivec2 e1;
vec2 f1 = frexp(v1, e1);
float r0;
float m0 = modf(v0, r0);
vec2 r1;
vec2 m1 = modf(v1, r1);
FragColor = f0 + f1.x + f1.y + m0 + m1.x + m1.y;
}

View File

@ -48,6 +48,7 @@ namespace spirv_cross
#else
fprintf(stderr, "There was a compiler error: %s\n", msg.c_str());
#endif
fflush(stderr);
abort();
}

View File

@ -2629,7 +2629,6 @@ bool Compiler::CombinedImageSamplerHandler::end_function_scope(const uint32_t *a
auto &callee = compiler.get<SPIRFunction>(args[2]);
args += 3;
length -= 3;
// There are two types of cases we have to handle,
// a callee might call sampler2D(texture2D, sampler) directly where

View File

@ -349,7 +349,7 @@ void CompilerGLSL::emit_header()
statement("#version ", options.version, options.es && options.version > 100 ? " es" : "");
// Needed for binding = # on UBOs, etc.
if (!options.es && options.version < 420)
if (!options.es && options.version < 420 && options.enable_420pack_extension)
{
statement("#ifdef GL_ARB_shading_language_420pack");
statement("#extension GL_ARB_shading_language_420pack : require");
@ -999,8 +999,15 @@ string CompilerGLSL::layout_for_variable(const SPIRVariable &var)
attr.push_back(join("set = ", dec.set));
}
if (flags & (1ull << DecorationBinding))
bool can_use_binding;
if (options.es)
can_use_binding = options.version >= 310;
else
can_use_binding = options.enable_420pack_extension || (options.version >= 420);
if (can_use_binding && (flags & (1ull << DecorationBinding)))
attr.push_back(join("binding = ", dec.binding));
if (flags & (1ull << DecorationOffset))
attr.push_back(join("offset = ", dec.offset));
@ -3291,6 +3298,19 @@ void CompilerGLSL::emit_glsl_op(uint32_t result_type, uint32_t id, uint32_t eop,
emit_binary_func_op(result_type, id, args[0], args[1], "modf");
break;
case GLSLstd450ModfStruct:
{
forced_temporaries.insert(id);
auto &type = get<SPIRType>(result_type);
auto flags = meta[id].decoration.decoration_flags;
statement(flags_to_precision_qualifiers_glsl(type, flags), variable_decl(type, to_name(id)), ";");
set<SPIRExpression>(id, to_name(id), result_type, true);
statement(to_expression(id), ".", to_member_name(type, 0), " = ", "modf(", to_expression(args[0]), ", ",
to_expression(id), ".", to_member_name(type, 1), ");");
break;
}
// Minmax
case GLSLstd450FMin:
case GLSLstd450UMin:
@ -3400,6 +3420,20 @@ void CompilerGLSL::emit_glsl_op(uint32_t result_type, uint32_t id, uint32_t eop,
forced_temporaries.insert(id);
emit_binary_func_op(result_type, id, args[0], args[1], "frexp");
break;
case GLSLstd450FrexpStruct:
{
forced_temporaries.insert(id);
auto &type = get<SPIRType>(result_type);
auto flags = meta[id].decoration.decoration_flags;
statement(flags_to_precision_qualifiers_glsl(type, flags), variable_decl(type, to_name(id)), ";");
set<SPIRExpression>(id, to_name(id), result_type, true);
statement(to_expression(id), ".", to_member_name(type, 0), " = ", "frexp(", to_expression(args[0]), ", ",
to_expression(id), ".", to_member_name(type, 1), ");");
break;
}
case GLSLstd450Ldexp:
emit_binary_func_op(result_type, id, args[0], args[1], "ldexp");
break;
@ -3529,6 +3563,31 @@ string CompilerGLSL::bitcast_glsl(const SPIRType &result_type, uint32_t argument
return join(op, "(", to_expression(argument), ")");
}
std::string CompilerGLSL::bitcast_expression(SPIRType::BaseType target_type, uint32_t arg)
{
auto expr = to_expression(arg);
auto &src_type = expression_type(arg);
if (src_type.basetype != target_type)
{
auto target = src_type;
target.basetype = target_type;
expr = join(bitcast_glsl_op(target, src_type), "(", expr, ")");
}
return expr;
}
std::string CompilerGLSL::bitcast_expression(const SPIRType &target_type, SPIRType::BaseType expr_type,
const std::string &expr)
{
if (target_type.basetype == expr_type)
return expr;
auto src_type = target_type;
src_type.basetype = expr_type;
return join(bitcast_glsl_op(target_type, src_type), "(", expr, ")");
}
string CompilerGLSL::builtin_to_glsl(BuiltIn builtin, StorageClass storage)
{
switch (builtin)
@ -4492,6 +4551,10 @@ bool CompilerGLSL::optimize_read_modify_write(const string &lhs, const string &r
if (op != lhs.size() + 1)
return false;
// Check that the op is followed by space. This excludes && and ||.
if (rhs[op + 1] != ' ')
return false;
char bop = rhs[op];
auto expr = rhs.substr(lhs.size() + 3);
// Try to find increments and decrements. Makes it look neater as += 1, -= 1 is fairly rare to see in real code.
@ -5603,27 +5666,36 @@ void CompilerGLSL::emit_instruction(const Instruction &instruction)
case OpImageQueryLevels:
{
uint32_t result_type = ops[0];
uint32_t id = ops[1];
if (!options.es && options.version < 430)
require_extension("GL_ARB_texture_query_levels");
if (options.es)
SPIRV_CROSS_THROW("textureQueryLevels not supported in ES profile.");
UFOP(textureQueryLevels);
auto expr = join("textureQueryLevels(", to_expression(ops[2]), ")");
auto &restype = get<SPIRType>(ops[0]);
expr = bitcast_expression(restype, SPIRType::Int, expr);
emit_op(result_type, id, expr, true);
break;
}
case OpImageQuerySamples:
{
auto *var = maybe_get_backing_variable(ops[2]);
if (!var)
SPIRV_CROSS_THROW(
"Bug. OpImageQuerySamples must have a backing variable so we know if the image is sampled or not.");
auto &type = expression_type(ops[2]);
uint32_t result_type = ops[0];
uint32_t id = ops[1];
auto &type = get<SPIRType>(var->basetype);
bool image = type.image.sampled == 2;
if (image)
UFOP(imageSamples);
string expr;
if (type.image.sampled == 2)
expr = join("imageSamples(", to_expression(ops[2]), ")");
else
UFOP(textureSamples);
expr = join("textureSamples(", to_expression(ops[2]), ")");
auto &restype = get<SPIRType>(ops[0]);
expr = bitcast_expression(restype, SPIRType::Int, expr);
emit_op(result_type, id, expr, true);
break;
}
@ -5636,8 +5708,16 @@ void CompilerGLSL::emit_instruction(const Instruction &instruction)
}
case OpImageQuerySizeLod:
BFOP(textureSize);
{
uint32_t result_type = ops[0];
uint32_t id = ops[1];
auto expr = join("textureSize(", to_expression(ops[2]), ", ", bitcast_expression(SPIRType::Int, ops[3]), ")");
auto &restype = get<SPIRType>(ops[0]);
expr = bitcast_expression(restype, SPIRType::Int, expr);
emit_op(result_type, id, expr, true);
break;
}
// Image load/store
case OpImageRead:
@ -5816,8 +5896,21 @@ void CompilerGLSL::emit_instruction(const Instruction &instruction)
if (type.basetype == SPIRType::Image)
{
// The size of an image is always constant.
emit_op(result_type, id, join("imageSize(", to_expression(ops[2]), ")"), true);
string expr;
if (type.image.sampled == 2)
{
// The size of an image is always constant.
expr = join("imageSize(", to_expression(ops[2]), ")");
}
else
{
// This path is hit for samplerBuffers and multisampled images which do not have LOD.
expr = join("textureSize(", to_expression(ops[2]), ")");
}
auto &restype = get<SPIRType>(ops[0]);
expr = bitcast_expression(restype, SPIRType::Int, expr);
emit_op(result_type, id, expr, true);
}
else
SPIRV_CROSS_THROW("Invalid type for OpImageQuerySize.");
@ -6722,8 +6815,27 @@ void CompilerGLSL::flush_phi(uint32_t from, uint32_t to)
auto &child = get<SPIRBlock>(to);
for (auto &phi : child.phi_variables)
{
if (phi.parent == from)
statement(to_expression(phi.function_variable), " = ", to_expression(phi.local_variable), ";");
{
auto &var = get<SPIRVariable>(phi.function_variable);
// A Phi variable might be a loop variable, so flush to static expression.
if (var.loop_variable && !var.loop_variable_enable)
var.static_expression = phi.local_variable;
else
{
flush_variable_declaration(phi.function_variable);
// This might be called in continue block, so make sure we
// use this to emit ESSL 1.0 compliant increments/decrements.
auto lhs = to_expression(phi.function_variable);
auto rhs = to_expression(phi.local_variable);
if (!optimize_read_modify_write(lhs, rhs))
statement(lhs, " = ", rhs, ";");
}
}
}
}
void CompilerGLSL::branch(uint32_t from, uint32_t to)

View File

@ -56,8 +56,13 @@ class CompilerGLSL : public Compiler
public:
struct Options
{
// The shading language version. Corresponds to #version $VALUE.
uint32_t version = 450;
// Emit the OpenGL ES shading language instead of desktop OpenGL.
bool es = false;
// Debug option to always emit temporary variables for all expressions.
bool force_temporary = false;
// If true, variables will be moved to their appropriate scope through CFG analysis.
@ -79,6 +84,12 @@ public:
// Only the generated code, including declarations of interface variables are changed to be single array dimension.
bool flatten_multidimensional_arrays = false;
// For older desktop GLSL targets than version 420, the
// GL_ARB_shading_language_420pack extensions is used to be able to support
// layout(binding) on UBOs and samplers.
// If disabled on older targets, binding decorations will be stripped.
bool enable_420pack_extension = true;
enum Precision
{
DontCare,
@ -395,6 +406,10 @@ protected:
std::string bitcast_glsl(const SPIRType &result_type, uint32_t arg);
virtual std::string bitcast_glsl_op(const SPIRType &result_type, const SPIRType &argument_type);
std::string bitcast_expression(SPIRType::BaseType target_type, uint32_t arg);
std::string bitcast_expression(const SPIRType &target_type, SPIRType::BaseType expr_type, const std::string &expr);
std::string build_composite_combiner(const uint32_t *elems, uint32_t length);
bool remove_duplicate_swizzle(std::string &op);
bool remove_unity_swizzle(uint32_t base, std::string &op);

View File

@ -83,7 +83,8 @@ string CompilerHLSL::image_type_hlsl_modern(const SPIRType &type)
SPIRV_CROSS_THROW("Invalid dimension.");
}
const char *arrayed = type.image.arrayed ? "Array" : "";
return join("Texture", dim, arrayed, "<", type_to_glsl(imagetype), components, ">");
const char *ms = type.image.ms ? "MS" : "";
return join("Texture", dim, ms, arrayed, "<", type_to_glsl(imagetype), components, ">");
}
string CompilerHLSL::image_type_hlsl_legacy(const SPIRType &type)
@ -938,6 +939,66 @@ void CompilerHLSL::emit_resources()
statement("");
}
}
if (required_textureSizeVariants != 0)
{
static const char *types[QueryTypeCount] = { "float4", "int4", "uint4" };
static const char *dims[QueryDimCount] = { "Texture1D", "Texture1DArray", "Texture2D", "Texture2DArray",
"Texture3D", "Buffer", "TextureCube", "TextureCubeArray",
"Texture2DMS", "Texture2DMSArray" };
static const bool has_lod[QueryDimCount] = { true, true, true, true, true, false, true, true, false, false };
static const char *ret_types[QueryDimCount] = {
"uint", "uint2", "uint2", "uint3", "uint3", "uint", "uint2", "uint3", "uint2", "uint3",
};
static const uint32_t return_arguments[QueryDimCount] = {
1, 2, 2, 3, 3, 1, 2, 3, 2, 3,
};
for (uint32_t index = 0; index < QueryDimCount; index++)
{
for (uint32_t type_index = 0; type_index < QueryTypeCount; type_index++)
{
uint32_t bit = 16 * type_index + index;
uint64_t mask = 1ull << bit;
if ((required_textureSizeVariants & mask) == 0)
continue;
statement(ret_types[index], " SPIRV_Cross_textureSize(", dims[index], "<", types[type_index],
"> Tex, uint Level, out uint Param)");
begin_scope();
statement(ret_types[index], " ret;");
switch (return_arguments[index])
{
case 1:
if (has_lod[index])
statement("Tex.GetDimensions(Level, ret.x, Param);");
else
statement("Tex.GetDimensions(ret.x);");
break;
case 2:
if (has_lod[index])
statement("Tex.GetDimensions(Level, ret.x, ret.y, Param);");
else
statement("Tex.GetDimensions(ret.x, ret.y, Param);");
break;
case 3:
if (has_lod[index])
statement("Tex.GetDimensions(Level, ret.x, ret.y, ret.z, Param);");
else
statement("Tex.GetDimensions(ret.x, ret.y, ret.z, Param);");
break;
}
statement("return ret;");
end_scope();
statement("");
}
}
}
}
string CompilerHLSL::layout_for_member(const SPIRType &, uint32_t)
@ -1463,7 +1524,36 @@ void CompilerHLSL::emit_texture_op(const Instruction &i)
if (imgtype.image.depth)
texop += ".SampleCmp";
else if (gather)
texop += ".Gather";
{
uint32_t comp_num = get<SPIRConstant>(comp).scalar();
if (options.shader_model >= 50)
{
switch (comp_num)
{
case 0:
texop += ".GatherRed";
break;
case 1:
texop += ".GatherGreen";
break;
case 2:
texop += ".GatherBlue";
break;
case 3:
texop += ".GatherAlpha";
break;
default:
SPIRV_CROSS_THROW("Invalid component.");
}
}
else
{
if (comp_num == 0)
texop += ".Gather";
else
SPIRV_CROSS_THROW("HLSL shader model 4 can only gather from the red component.");
}
}
else if (bias)
texop += ".SampleBias";
else if (grad_x || grad_y)
@ -1641,13 +1731,6 @@ void CompilerHLSL::emit_texture_op(const Instruction &i)
expr += to_expression(offset);
}
if (comp)
{
forward = forward && should_forward(comp);
expr += ", ";
expr += to_expression(comp);
}
if (sample)
{
expr += ", ";
@ -1690,7 +1773,7 @@ string CompilerHLSL::to_resource_binding(const SPIRVariable &var)
space = "u"; // UAV
else if (has_decoration(type.self, DecorationBlock))
{
if (options.shader_model >= 51)
if (options.shader_model >= 40)
space = "b"; // Constant buffers
else
space = "c"; // Constant buffers
@ -1698,7 +1781,7 @@ string CompilerHLSL::to_resource_binding(const SPIRVariable &var)
}
else if (storage == StorageClassPushConstant)
{
if (options.shader_model >= 51)
if (options.shader_model >= 40)
space = "b"; // Constant buffers
else
space = "c"; // Constant buffers
@ -1833,7 +1916,7 @@ void CompilerHLSL::emit_glsl_op(uint32_t result_type, uint32_t id, uint32_t eop,
break;
case GLSLstd450Atan2:
emit_binary_func_op(result_type, id, args[1], args[0], "atan2");
emit_binary_func_op(result_type, id, args[0], args[1], "atan2");
break;
case GLSLstd450Fma:
@ -2315,12 +2398,127 @@ void CompilerHLSL::emit_instruction(const Instruction &instruction)
break;
}
case OpImageQuerySizeLod:
{
auto result_type = ops[0];
auto id = ops[1];
require_texture_query_variant(expression_type(ops[2]));
auto dummy_samples_levels = join(get_fallback_name(id), "_dummy_parameter");
statement("uint ", dummy_samples_levels, ";");
auto expr = join("SPIRV_Cross_textureSize(", to_expression(ops[2]), ", ",
bitcast_expression(SPIRType::UInt, ops[3]), ", ", dummy_samples_levels, ")");
auto &restype = get<SPIRType>(ops[0]);
expr = bitcast_expression(restype, SPIRType::UInt, expr);
emit_op(result_type, id, expr, true);
break;
}
case OpImageQuerySize:
{
auto result_type = ops[0];
auto id = ops[1];
require_texture_query_variant(expression_type(ops[2]));
auto dummy_samples_levels = join(get_fallback_name(id), "_dummy_parameter");
statement("uint ", dummy_samples_levels, ";");
auto expr = join("SPIRV_Cross_textureSize(", to_expression(ops[2]), ", 0u, ", dummy_samples_levels, ")");
auto &restype = get<SPIRType>(ops[0]);
expr = bitcast_expression(restype, SPIRType::UInt, expr);
emit_op(result_type, id, expr, true);
break;
}
case OpImageQuerySamples:
case OpImageQueryLevels:
{
auto result_type = ops[0];
auto id = ops[1];
require_texture_query_variant(expression_type(ops[2]));
// Keep it simple and do not emit special variants to make this look nicer ...
// This stuff is barely, if ever, used.
forced_temporaries.insert(id);
auto &type = get<SPIRType>(result_type);
statement(variable_decl(type, to_name(id)), ";");
statement("SPIRV_Cross_textureSize(", to_expression(ops[2]), ", 0u, ", to_name(id), ");");
auto &restype = get<SPIRType>(ops[0]);
auto expr = bitcast_expression(restype, SPIRType::UInt, to_name(id));
set<SPIRExpression>(id, expr, result_type, true);
break;
}
default:
CompilerGLSL::emit_instruction(instruction);
break;
}
}
void CompilerHLSL::require_texture_query_variant(const SPIRType &type)
{
uint32_t bit = 0;
switch (type.image.dim)
{
case Dim1D:
bit = type.image.arrayed ? Query1DArray : Query1D;
break;
case Dim2D:
if (type.image.ms)
bit = type.image.arrayed ? Query2DMSArray : Query2DMS;
else
bit = type.image.arrayed ? Query2DArray : Query2D;
break;
case Dim3D:
bit = Query3D;
break;
case DimCube:
bit = type.image.arrayed ? QueryCubeArray : QueryCube;
break;
case DimBuffer:
bit = QueryBuffer;
break;
default:
SPIRV_CROSS_THROW("Unsupported query type.");
}
switch (get<SPIRType>(type.image.type).basetype)
{
case SPIRType::Float:
bit += QueryTypeFloat;
break;
case SPIRType::Int:
bit += QueryTypeInt;
break;
case SPIRType::UInt:
bit += QueryTypeUInt;
break;
default:
SPIRV_CROSS_THROW("Unsupported query type.");
}
uint64_t mask = 1ull << bit;
if ((required_textureSizeVariants & mask) == 0)
{
force_recompile = true;
required_textureSizeVariants |= mask;
}
}
string CompilerHLSL::compile()
{
// Do not deal with ES-isms like precision, older extensions and such.

View File

@ -98,6 +98,31 @@ private:
Options options;
bool requires_op_fmod = false;
bool requires_textureProj = false;
uint64_t required_textureSizeVariants = 0;
void require_texture_query_variant(const SPIRType &type);
enum TextureQueryVariantDim
{
Query1D = 0,
Query1DArray,
Query2D,
Query2DArray,
Query3D,
QueryBuffer,
QueryCube,
QueryCubeArray,
Query2DMS,
Query2DMSArray,
QueryDimCount
};
enum TextureQueryVariantType
{
QueryTypeFloat = 0,
QueryTypeInt = 16,
QueryTypeUInt = 32,
QueryTypeCount = 3
};
void emit_builtin_variables();
bool require_output = false;