Skip to content

Commit

Permalink
HLSL: Support depth comparison texture sampling in SM 2/3.
Browse files Browse the repository at this point in the history
  • Loading branch information
rdb committed Oct 31, 2020
1 parent 8884b34 commit d4e31d1
Showing 1 changed file with 31 additions and 28 deletions.
59 changes: 31 additions & 28 deletions spirv_hlsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,6 @@ string CompilerHLSL::image_type_hlsl_legacy(const SPIRType &type, uint32_t id)
res += "MS";
if (type.image.arrayed)
res += "Array";
if (image_is_comparison(type, id))
res += "Shadow";

return res;
}
Expand Down Expand Up @@ -2926,14 +2924,15 @@ void CompilerHLSL::emit_texture_op(const Instruction &i, bool sparse)
SPIRV_CROSS_THROW("textureGather is not supported in HLSL shader model 2/3.");
if (offset || coffset)
SPIRV_CROSS_THROW("textureOffset is not supported in HLSL shader model 2/3.");
if (proj)
texop += "proj";

if (grad_x || grad_y)
texop += "grad";
if (lod)
else if (lod)
texop += "lod";
if (bias)
else if (bias)
texop += "bias";
else if (proj || dref)
texop += "proj";
}
}

Expand Down Expand Up @@ -2987,35 +2986,42 @@ void CompilerHLSL::emit_texture_op(const Instruction &i, bool sparse)

if (hlsl_options.shader_model < 40)
{
string coord_filler;
uint32_t modifier_count = 0;

if (lod)
if (dref)
{
if (imgtype.image.dim != spv::Dim1D || imgtype.image.dim != spv::Dim2D)
SPIRV_CROSS_THROW("Depth comparison is only supported for 1D and 2D textures in HLSL shader model 2/3.");

forward = forward && should_forward(dref);
for (uint32_t size = coord_components; size < 3; ++size)
coord_filler += ", 0.0";
coord_expr = "float4(" + coord_expr + coord_filler + ", " + to_expression(lod) + ")";
modifier_count++;
coord_expr += ", " + to_expression(dref);
}

if (bias)
else if (lod || bias || proj)
{
for (uint32_t size = coord_components; size < 3; ++size)
coord_filler += ", 0.0";
coord_expr = "float4(" + coord_expr + coord_filler + ", " + to_expression(bias) + ")";
modifier_count++;
coord_expr += ", 0.0";
}

if (proj)
if (lod)
{
for (uint32_t size = coord_components; size < 3; ++size)
coord_filler += ", 0.0";
coord_expr = "float4(" + coord_expr + coord_filler + ", " +
coord_expr = "float4(" + coord_expr + ", " + to_expression(lod) + ")";
}
else if (bias)
{
coord_expr = "float4(" + coord_expr + ", " + to_expression(bias) + ")";
}
else if (proj)
{
coord_expr = "float4(" + coord_expr + ", " +
to_extract_component_expression(coord, coord_components) + ")";
modifier_count++;
}
else if (dref)
{
// A "normal" sample gets fed into tex2Dproj as well, because the
// regular tex2D accepts only two coordinates.
coord_expr = "float4(" + coord_expr + ", 1.0)";
}

if (modifier_count > 1)
if (!!lod + !!bias + !!proj > 1)
SPIRV_CROSS_THROW("Legacy HLSL can only use one of lod/bias/proj modifiers.");
}

Expand All @@ -3029,11 +3035,8 @@ void CompilerHLSL::emit_texture_op(const Instruction &i, bool sparse)
expr += ", ";
expr += coord_expr;

if (dref)
if (dref && hlsl_options.shader_model >= 40)
{
if (hlsl_options.shader_model < 40)
SPIRV_CROSS_THROW("Legacy HLSL does not support comparison sampling.");

forward = forward && should_forward(dref);
expr += ", ";

Expand Down

0 comments on commit d4e31d1

Please sign in to comment.