Support texelFetch in HLSL

This commit is contained in:
Robert Konrad 2017-04-24 11:08:55 +02:00
parent 434d9b8803
commit bb9dbd4149
3 changed files with 67 additions and 10 deletions

View File

@ -12,10 +12,17 @@ Texture2D<float4> tex2dShadow;
SamplerComparisonState _tex2dShadow_sampler;
TextureCube<float4> texCubeShadow;
SamplerComparisonState _texCubeShadow_sampler;
Texture1DArray<float4> tex1dArray;
SamplerState _tex1dArray_sampler;
Texture2DArray<float4> tex2dArray;
SamplerState _tex2dArray_sampler;
TextureCubeArray<float4> texCubeArray;
SamplerState _texCubeArray_sampler;
static float texCoord1d;
static float2 texCoord2d;
static float3 texCoord3d;
static float4 texCoord4d;
static float4 FragColor;
struct SPIRV_Cross_Input
@ -23,6 +30,7 @@ struct SPIRV_Cross_Input
float texCoord1d : TEXCOORD0;
float2 texCoord2d : TEXCOORD1;
float3 texCoord3d : TEXCOORD2;
float4 texCoord4d : TEXCOORD3;
};
struct SPIRV_Cross_Output
@ -74,6 +82,11 @@ void frag_main()
texcolor.w += tex2dShadow.SampleCmp(_tex2dShadow_sampler, _188.xy, _188.z);
float4 _204 = float4(texCoord3d, 0.0f);
texcolor.w += texCubeShadow.SampleCmp(_texCubeShadow_sampler, _204.xyz, _204.w);
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.Load(int3(int2(1, 2), 0));
FragColor = texcolor;
}
@ -82,6 +95,7 @@ SPIRV_Cross_Output main(SPIRV_Cross_Input stage_input)
texCoord1d = stage_input.texCoord1d;
texCoord2d = stage_input.texCoord2d;
texCoord3d = stage_input.texCoord3d;
texCoord4d = stage_input.texCoord4d;
frag_main();
SPIRV_Cross_Output stage_output;
stage_output.FragColor = FragColor;

View File

@ -9,9 +9,14 @@ uniform sampler1DShadow tex1dShadow;
uniform sampler2DShadow tex2dShadow;
uniform samplerCubeShadow texCubeShadow;
uniform sampler1DArray tex1dArray;
uniform sampler2DArray tex2dArray;
uniform samplerCubeArray texCubeArray;
in float texCoord1d;
in vec2 texCoord2d;
in vec3 texCoord3d;
in vec4 texCoord4d;
out vec4 FragColor;
@ -46,5 +51,13 @@ void main()
texcolor.a += texture(tex2dShadow, vec3(texCoord2d, 0.0));
texcolor.a += texture(texCubeShadow, vec4(texCoord3d, 0.0));
texcolor += texture(tex1dArray, texCoord2d);
texcolor += texture(tex2dArray, texCoord3d);
texcolor += texture(texCubeArray, texCoord4d);
texcolor += textureGather(tex2d, texCoord2d);
texcolor += texelFetch(tex2d, ivec2(1, 2), 0);
FragColor = texcolor;
}

View File

@ -1216,7 +1216,14 @@ void CompilerHLSL::emit_texture_op(const Instruction &i)
string texop;
if (op == OpImageFetch)
texop += "texelFetch";
{
if (options.shader_model < 40)
{
SPIRV_CROSS_THROW("texelFetch is not supported in HLSL shader model 2/3.");
}
texop += to_expression(img);
texop += ".Load";
}
else
{
auto &imgformat = get<SPIRType>(imgtype.image.type);
@ -1281,14 +1288,17 @@ void CompilerHLSL::emit_texture_op(const Instruction &i)
expr += texop;
expr += "(";
if (options.shader_model >= 40)
if (op != OpImageFetch)
{
expr += "_";
}
expr += to_expression(img);
if (options.shader_model >= 40)
{
expr += "_sampler";
if (options.shader_model >= 40)
{
expr += "_";
}
expr += to_expression(img);
if (options.shader_model >= 40)
{
expr += "_sampler";
}
}
bool swizz_func = backend.swizzle_is_function;
@ -1346,7 +1356,18 @@ void CompilerHLSL::emit_texture_op(const Instruction &i)
coord_expr = "float4(" + coord_expr + coord_filler + ", " + to_expression(bias) + ")";
}
expr += ", ";
if (op == OpImageFetch)
{
auto &coordtype = expression_type(coord);
stringstream str;
str << coordtype.vecsize + 1;
coord_expr = "int" + str.str() + "(" + coord_expr + ", " + to_expression(lod) + ")";
}
if (op != OpImageFetch)
{
expr += ", ";
}
expr += coord_expr;
if (dref)
@ -1366,7 +1387,7 @@ void CompilerHLSL::emit_texture_op(const Instruction &i)
expr += to_expression(grad_y);
}
if (lod && options.shader_model >= 40)
if (lod && options.shader_model >= 40 && op != OpImageFetch)
{
forward = forward && should_forward(lod);
expr += ", ";
@ -1574,6 +1595,15 @@ void CompilerHLSL::emit_instruction(const Instruction &instruction)
break;
}
case OpImage:
{
uint32_t result_type = ops[0];
uint32_t id = ops[1];
emit_op(result_type, id, to_expression(ops[2]), true, true);
// TODO: Maybe change this when separate samplers/images are supported
break;
}
case OpDPdx:
UFOP(ddx);
break;