Support shadow sampling in HLSL

This commit is contained in:
Robert Konrad 2017-04-21 17:52:04 +02:00
Родитель 585339f3cd
Коммит 7d8be83cb3
3 изменённых файлов: 33 добавлений и 31 удалений

Просмотреть файл

@ -6,6 +6,12 @@ Texture3D<float4> tex3d;
SamplerState _tex3d_sampler;
TextureCube<float4> texCube;
SamplerState _texCube_sampler;
Texture1D<float4> tex1dShadow;
SamplerComparisonState _tex1dShadow_sampler;
Texture2D<float4> tex2dShadow;
SamplerComparisonState _tex2dShadow_sampler;
TextureCube<float4> texCubeShadow;
SamplerComparisonState _texCubeShadow_sampler;
static float texCoord1d;
static float2 texCoord2d;
@ -62,6 +68,12 @@ void frag_main()
texcolor += texCube.Sample(_texCube_sampler, texCoord3d);
texcolor += texCube.SampleLevel(_texCube_sampler, texCoord3d, 2.0f);
texcolor += texCube.SampleBias(_texCube_sampler, texCoord3d, 1.0f);
float3 _170 = float3(texCoord1d, 0.0f, 0.0f);
texcolor.w += tex1dShadow.SampleCmp(_tex1dShadow_sampler, _170.x, _170.z);
float3 _188 = float3(texCoord2d, 0.0f);
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);
FragColor = texcolor;
}

Просмотреть файл

@ -5,13 +5,18 @@ uniform sampler2D tex2d;
uniform sampler3D tex3d;
uniform samplerCube texCube;
uniform sampler1DShadow tex1dShadow;
uniform sampler2DShadow tex2dShadow;
uniform samplerCubeShadow texCubeShadow;
in float texCoord1d;
in vec2 texCoord2d;
in vec3 texCoord3d;
out vec4 FragColor;
void main() {
void main()
{
vec4 texcolor = texture(tex1d, texCoord1d);
texcolor += textureOffset(tex1d, texCoord1d, 1);
texcolor += textureLod(tex1d, texCoord1d, 2);
@ -37,5 +42,9 @@ void main() {
texcolor += textureLod(texCube, texCoord3d, 2);
texcolor += texture(texCube, texCoord3d, 1.0);
texcolor.a += texture(tex1dShadow, vec3(texCoord1d, 0.0, 0.0));
texcolor.a += texture(tex2dShadow, vec3(texCoord2d, 0.0));
texcolor.a += texture(texCubeShadow, vec4(texCoord3d, 0.0));
FragColor = texcolor;
}

Просмотреть файл

@ -1229,7 +1229,9 @@ void CompilerHLSL::emit_texture_op(const Instruction &i)
{
texop += to_expression(img);
if (gather)
if (imgtype.image.depth)
texop += ".SampleCmp";
else if (gather)
texop += ".Gather";
else if (bias)
texop += ".SampleBias";
@ -1344,38 +1346,14 @@ void CompilerHLSL::emit_texture_op(const Instruction &i)
coord_expr = "float4(" + coord_expr + coord_filler + ", " + to_expression(bias) + ")";
}
// TODO: implement rest ... A bit intensive.
expr += ", ";
expr += coord_expr;
if (dref)
{
forward = forward && should_forward(dref);
// SPIR-V splits dref and coordinate.
if (coord_components == 4) // GLSL also splits the arguments in two.
{
expr += ", ";
expr += to_expression(coord);
expr += ", ";
expr += to_expression(dref);
}
else
{
// Create a composite which merges coord/dref into a single vector.
auto type = expression_type(coord);
type.vecsize = coord_components + 1;
expr += ", ";
expr += type_to_glsl_constructor(type);
expr += "(";
expr += coord_expr;
expr += ", ";
expr += to_expression(dref);
expr += ")";
}
}
else
{
expr += ", ";
expr += coord_expr;
expr += to_expression(dref);
}
if (grad_x || grad_y)
@ -1461,7 +1439,10 @@ void CompilerHLSL::emit_uniform(const SPIRVariable &var)
SPIRV_CROSS_THROW("Buffer texture support is not yet implemented for HLSL"); // TODO
}
statement("Texture", dim, "<", type_to_glsl(imagetype), "4> ", to_name(var.self), ";");
statement("SamplerState _", to_name(var.self), "_sampler;");
if (type.image.depth)
statement("SamplerComparisonState _", to_name(var.self), "_sampler;");
else
statement("SamplerState _", to_name(var.self), "_sampler;");
}
else
{