Deal with OpImageFetch without explicit LOD.

This commit is contained in:
Hans-Kristian Arntzen 2018-04-30 10:53:21 +02:00
parent 454691f434
commit d93807a625
15 changed files with 348 additions and 2 deletions

View File

@ -0,0 +1,29 @@
Texture2D<float4> uTexture : register(t0);
SamplerState _uTexture_sampler : register(s0);
static float4 gl_FragCoord;
static float4 FragColor;
struct SPIRV_Cross_Input
{
float4 gl_FragCoord : SV_Position;
};
struct SPIRV_Cross_Output
{
float4 FragColor : SV_Target0;
};
void frag_main()
{
FragColor = uTexture.Load(int3(int2(gl_FragCoord.xy), 0));
}
SPIRV_Cross_Output main(SPIRV_Cross_Input stage_input)
{
gl_FragCoord = stage_input.gl_FragCoord;
frag_main();
SPIRV_Cross_Output stage_output;
stage_output.FragColor = FragColor;
return stage_output;
}

View File

@ -0,0 +1,33 @@
Texture2DMS<float4> uTex : register(t0);
SamplerState _uTex_sampler : register(s0);
static float4 gl_FragCoord;
static float4 FragColor;
struct SPIRV_Cross_Input
{
float4 gl_FragCoord : SV_Position;
};
struct SPIRV_Cross_Output
{
float4 FragColor : SV_Target0;
};
void frag_main()
{
int2 _22 = int2(gl_FragCoord.xy);
FragColor = uTex.Load(_22, 0);
FragColor += uTex.Load(_22, 1);
FragColor += uTex.Load(_22, 2);
FragColor += uTex.Load(_22, 3);
}
SPIRV_Cross_Output main(SPIRV_Cross_Input stage_input)
{
gl_FragCoord = stage_input.gl_FragCoord;
frag_main();
SPIRV_Cross_Output stage_output;
stage_output.FragColor = FragColor;
return stage_output;
}

View File

@ -0,0 +1,17 @@
#include <metal_stdlib>
#include <simd/simd.h>
using namespace metal;
struct main0_out
{
float4 FragColor [[color(0)]];
};
fragment main0_out main0(texture2d<float> uTexture [[texture(0)]], sampler uTextureSmplr [[sampler(0)]], float4 gl_FragCoord [[position]])
{
main0_out out = {};
out.FragColor = uTexture.read(uint2(int2(gl_FragCoord.xy)), 0);
return out;
}

View File

@ -0,0 +1,11 @@
#version 450
layout(binding = 0) uniform sampler2D uTexture;
layout(location = 0) out vec4 FragColor;
void main()
{
FragColor = texelFetch(uTexture, ivec2(gl_FragCoord.xy), 0);
}

View File

@ -0,0 +1,29 @@
Texture2D<float4> uTexture : register(t0);
SamplerState _uTexture_sampler : register(s0);
static float4 gl_FragCoord;
static float4 FragColor;
struct SPIRV_Cross_Input
{
float4 gl_FragCoord : SV_Position;
};
struct SPIRV_Cross_Output
{
float4 FragColor : SV_Target0;
};
void frag_main()
{
FragColor = uTexture.Load(int3(int2(gl_FragCoord.xy), 0));
}
SPIRV_Cross_Output main(SPIRV_Cross_Input stage_input)
{
gl_FragCoord = stage_input.gl_FragCoord;
frag_main();
SPIRV_Cross_Output stage_output;
stage_output.FragColor = FragColor;
return stage_output;
}

View File

@ -0,0 +1,32 @@
Texture2DMS<float4> uTex : register(t0);
SamplerState _uTex_sampler : register(s0);
static float4 gl_FragCoord;
static float4 FragColor;
struct SPIRV_Cross_Input
{
float4 gl_FragCoord : SV_Position;
};
struct SPIRV_Cross_Output
{
float4 FragColor : SV_Target0;
};
void frag_main()
{
FragColor = uTex.Load(int2(gl_FragCoord.xy), 0);
FragColor += uTex.Load(int2(gl_FragCoord.xy), 1);
FragColor += uTex.Load(int2(gl_FragCoord.xy), 2);
FragColor += uTex.Load(int2(gl_FragCoord.xy), 3);
}
SPIRV_Cross_Output main(SPIRV_Cross_Input stage_input)
{
gl_FragCoord = stage_input.gl_FragCoord;
frag_main();
SPIRV_Cross_Output stage_output;
stage_output.FragColor = FragColor;
return stage_output;
}

View File

@ -0,0 +1,17 @@
#include <metal_stdlib>
#include <simd/simd.h>
using namespace metal;
struct main0_out
{
float4 FragColor [[color(0)]];
};
fragment main0_out main0(texture2d<float> uTexture [[texture(0)]], sampler uTextureSmplr [[sampler(0)]], float4 gl_FragCoord [[position]])
{
main0_out out = {};
out.FragColor = uTexture.read(uint2(int2(gl_FragCoord.xy)), 0);
return out;
}

View File

@ -0,0 +1,11 @@
#version 450
layout(binding = 0) uniform sampler2D uTexture;
layout(location = 0) out vec4 FragColor;
void main()
{
FragColor = texelFetch(uTexture, ivec2(gl_FragCoord.xy), 0);
}

View File

@ -0,0 +1,46 @@
; SPIR-V
; Version: 1.0
; Generator: Khronos Glslang Reference Front End; 6
; Bound: 26
; Schema: 0
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main" %FragColor %gl_FragCoord
OpExecutionMode %main OriginUpperLeft
OpSource GLSL 450
OpName %main "main"
OpName %FragColor "FragColor"
OpName %uTexture "uTexture"
OpName %gl_FragCoord "gl_FragCoord"
OpDecorate %FragColor Location 0
OpDecorate %uTexture DescriptorSet 0
OpDecorate %uTexture Binding 0
OpDecorate %gl_FragCoord BuiltIn FragCoord
%void = OpTypeVoid
%3 = OpTypeFunction %void
%float = OpTypeFloat 32
%v4float = OpTypeVector %float 4
%_ptr_Output_v4float = OpTypePointer Output %v4float
%FragColor = OpVariable %_ptr_Output_v4float Output
%10 = OpTypeImage %float 2D 0 0 0 1 Unknown
%11 = OpTypeSampledImage %10
%_ptr_UniformConstant_11 = OpTypePointer UniformConstant %11
%uTexture = OpVariable %_ptr_UniformConstant_11 UniformConstant
%_ptr_Input_v4float = OpTypePointer Input %v4float
%gl_FragCoord = OpVariable %_ptr_Input_v4float Input
%v2float = OpTypeVector %float 2
%int = OpTypeInt 32 1
%v2int = OpTypeVector %int 2
%int_0 = OpConstant %int 0
%main = OpFunction %void None %3
%5 = OpLabel
%14 = OpLoad %11 %uTexture
%18 = OpLoad %v4float %gl_FragCoord
%19 = OpVectorShuffle %v2float %18 %18 0 1
%22 = OpConvertFToS %v2int %19
%24 = OpImage %10 %14
%25 = OpImageFetch %v4float %24 %22
OpStore %FragColor %25
OpReturn
OpFunctionEnd

View File

@ -0,0 +1,16 @@
#version 450
layout(location = 0) out vec4 FragColor;
layout(binding = 0) uniform sampler2DMS uTex;
void main()
{
FragColor =
texelFetch(uTex, ivec2(gl_FragCoord.xy), 0);
FragColor +=
texelFetch(uTex, ivec2(gl_FragCoord.xy), 1);
FragColor +=
texelFetch(uTex, ivec2(gl_FragCoord.xy), 2);
FragColor +=
texelFetch(uTex, ivec2(gl_FragCoord.xy), 3);
}

View File

@ -0,0 +1,46 @@
; SPIR-V
; Version: 1.0
; Generator: Khronos Glslang Reference Front End; 6
; Bound: 26
; Schema: 0
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main" %FragColor %gl_FragCoord
OpExecutionMode %main OriginUpperLeft
OpSource GLSL 450
OpName %main "main"
OpName %FragColor "FragColor"
OpName %uTexture "uTexture"
OpName %gl_FragCoord "gl_FragCoord"
OpDecorate %FragColor Location 0
OpDecorate %uTexture DescriptorSet 0
OpDecorate %uTexture Binding 0
OpDecorate %gl_FragCoord BuiltIn FragCoord
%void = OpTypeVoid
%3 = OpTypeFunction %void
%float = OpTypeFloat 32
%v4float = OpTypeVector %float 4
%_ptr_Output_v4float = OpTypePointer Output %v4float
%FragColor = OpVariable %_ptr_Output_v4float Output
%10 = OpTypeImage %float 2D 0 0 0 1 Unknown
%11 = OpTypeSampledImage %10
%_ptr_UniformConstant_11 = OpTypePointer UniformConstant %11
%uTexture = OpVariable %_ptr_UniformConstant_11 UniformConstant
%_ptr_Input_v4float = OpTypePointer Input %v4float
%gl_FragCoord = OpVariable %_ptr_Input_v4float Input
%v2float = OpTypeVector %float 2
%int = OpTypeInt 32 1
%v2int = OpTypeVector %int 2
%int_0 = OpConstant %int 0
%main = OpFunction %void None %3
%5 = OpLabel
%14 = OpLoad %11 %uTexture
%18 = OpLoad %v4float %gl_FragCoord
%19 = OpVectorShuffle %v2float %18 %18 0 1
%22 = OpConvertFToS %v2int %19
%24 = OpImage %10 %14
%25 = OpImageFetch %v4float %24 %22
OpStore %FragColor %25
OpReturn
OpFunctionEnd

View File

@ -0,0 +1,46 @@
; SPIR-V
; Version: 1.0
; Generator: Khronos Glslang Reference Front End; 6
; Bound: 26
; Schema: 0
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main" %FragColor %gl_FragCoord
OpExecutionMode %main OriginUpperLeft
OpSource GLSL 450
OpName %main "main"
OpName %FragColor "FragColor"
OpName %uTexture "uTexture"
OpName %gl_FragCoord "gl_FragCoord"
OpDecorate %FragColor Location 0
OpDecorate %uTexture DescriptorSet 0
OpDecorate %uTexture Binding 0
OpDecorate %gl_FragCoord BuiltIn FragCoord
%void = OpTypeVoid
%3 = OpTypeFunction %void
%float = OpTypeFloat 32
%v4float = OpTypeVector %float 4
%_ptr_Output_v4float = OpTypePointer Output %v4float
%FragColor = OpVariable %_ptr_Output_v4float Output
%10 = OpTypeImage %float 2D 0 0 0 1 Unknown
%11 = OpTypeSampledImage %10
%_ptr_UniformConstant_11 = OpTypePointer UniformConstant %11
%uTexture = OpVariable %_ptr_UniformConstant_11 UniformConstant
%_ptr_Input_v4float = OpTypePointer Input %v4float
%gl_FragCoord = OpVariable %_ptr_Input_v4float Input
%v2float = OpTypeVector %float 2
%int = OpTypeInt 32 1
%v2int = OpTypeVector %int 2
%int_0 = OpConstant %int 0
%main = OpFunction %void None %3
%5 = OpLabel
%14 = OpLoad %11 %uTexture
%18 = OpLoad %v4float %gl_FragCoord
%19 = OpVectorShuffle %v2float %18 %18 0 1
%22 = OpConvertFToS %v2int %19
%24 = OpImage %10 %14
%25 = OpImageFetch %v4float %24 %22
OpStore %FragColor %25
OpReturn
OpFunctionEnd

View File

@ -4078,6 +4078,11 @@ string CompilerGLSL::to_function_args(uint32_t img, const SPIRType &imgtype, boo
}
}
}
else if (is_fetch && imgtype.image.dim != DimBuffer && !imgtype.image.ms)
{
// Lod argument is optional in OpImageFetch, but we require a LOD value, pick 0 as the default.
farg_str += ", 0";
}
if (coffset)
{

View File

@ -2714,8 +2714,9 @@ void CompilerHLSL::emit_texture_op(const Instruction &i)
if (op == OpImageFetch)
{
auto &coordtype = expression_type(coord);
if (imgtype.image.dim != DimBuffer)
coord_expr = join("int", coordtype.vecsize + 1, "(", coord_expr, ", ", to_expression(lod), ")");
if (imgtype.image.dim != DimBuffer && !imgtype.image.ms)
coord_expr =
join("int", coordtype.vecsize + 1, "(", coord_expr, ", ", lod ? to_expression(lod) : string("0"), ")");
}
else
expr += ", ";

View File

@ -2609,6 +2609,13 @@ string CompilerMSL::to_function_args(uint32_t img, const SPIRType &imgtype, bool
farg_str += ", level(" + to_expression(lod) + ")";
}
}
else if (is_fetch && !lod && imgtype.image.dim != Dim1D && imgtype.image.dim != DimBuffer && !imgtype.image.ms &&
imgtype.image.sampled != 2)
{
// Lod argument is optional in OpImageFetch, but we require a LOD value, pick 0 as the default.
// Check for sampled type as well, because is_fetch is also used for OpImageRead in MSL.
farg_str += ", 0";
}
// Metal does not support LOD for 1D textures.
if ((grad_x || grad_y) && imgtype.image.dim != Dim1D)