spirv-reader: convert arity of textureLoad on depth texture

Fixed: tint:439
Change-Id: I151e388a1ea11bdcb5cebf0441a73ddcaf8a6f54
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/37063
Commit-Queue: David Neto <dneto@google.com>
Auto-Submit: David Neto <dneto@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
This commit is contained in:
David Neto 2021-01-11 19:04:49 +00:00 committed by Commit Bot service account
parent 2dc1ef47e0
commit 3f245ed362
2 changed files with 140 additions and 118 deletions

View File

@ -4073,15 +4073,17 @@ bool FunctionEmitter::EmitImageAccess(const spvtools::opt::Instruction& inst) {
std::string builtin_name; std::string builtin_name;
bool use_level_of_detail_suffix = true; bool use_level_of_detail_suffix = true;
bool is_dref = false; bool is_dref_sample = false;
bool is_non_dref_sample = false;
switch (opcode) { switch (opcode) {
case SpvOpImageSampleImplicitLod: case SpvOpImageSampleImplicitLod:
case SpvOpImageSampleExplicitLod: case SpvOpImageSampleExplicitLod:
is_non_dref_sample = true;
builtin_name = "textureSample"; builtin_name = "textureSample";
break; break;
case SpvOpImageSampleDrefImplicitLod: case SpvOpImageSampleDrefImplicitLod:
case SpvOpImageSampleDrefExplicitLod: case SpvOpImageSampleDrefExplicitLod:
is_dref = true; is_dref_sample = true;
builtin_name = "textureSampleCompare"; builtin_name = "textureSampleCompare";
if (arg_index < num_args) { if (arg_index < num_args) {
params.push_back(MakeOperand(inst, arg_index).expr); params.push_back(MakeOperand(inst, arg_index).expr);
@ -4138,7 +4140,7 @@ bool FunctionEmitter::EmitImageAccess(const spvtools::opt::Instruction& inst) {
} }
if (arg_index < num_args && if (arg_index < num_args &&
(image_operands_mask & SpvImageOperandsBiasMask)) { (image_operands_mask & SpvImageOperandsBiasMask)) {
if (is_dref) { if (is_dref_sample) {
return Fail() << "WGSL does not support depth-reference sampling with " return Fail() << "WGSL does not support depth-reference sampling with "
"level-of-detail bias: " "level-of-detail bias: "
<< inst.PrettyPrint(); << inst.PrettyPrint();
@ -4164,7 +4166,7 @@ bool FunctionEmitter::EmitImageAccess(const spvtools::opt::Instruction& inst) {
} }
if (arg_index + 1 < num_args && if (arg_index + 1 < num_args &&
(image_operands_mask & SpvImageOperandsGradMask)) { (image_operands_mask & SpvImageOperandsGradMask)) {
if (is_dref) { if (is_dref_sample) {
return Fail() << "WGSL does not support depth-reference sampling with " return Fail() << "WGSL does not support depth-reference sampling with "
"explicit gradient: " "explicit gradient: "
<< inst.PrettyPrint(); << inst.PrettyPrint();
@ -4224,19 +4226,27 @@ bool FunctionEmitter::EmitImageAccess(const spvtools::opt::Instruction& inst) {
result_component_type = result_vector_type->type(); result_component_type = result_vector_type->type();
} }
// Convert the arity of the result when operating on depth textures. // For depth textures, the arity might mot match WGSL:
// SPIR-V operations on depth textures always result in 4-element vectors. // Operation SPIR-V WGSL
// But WGSL operations on depth textures always result in a f32 scalar. // normal sampling vec4 ImplicitLod f32
// normal sampling vec4 ExplicitLod f32
// compare sample f32 DrefImplicitLod f32
// compare sample f32 DrefExplicitLod f32
// texel load vec4 ImageFetch f32
// normal gather vec4 ImageGather vec4 TODO(dneto)
// dref gather vec4 ImageFetch vec4 TODO(dneto)
// Construct a 4-element vector with the result from the builtin in the // Construct a 4-element vector with the result from the builtin in the
// first component. // first component.
if (texture_type->Is<ast::type::DepthTexture>()) { if (texture_type->Is<ast::type::DepthTexture>()) {
value = create<ast::TypeConstructorExpression>( if (is_non_dref_sample || (opcode == SpvOpImageFetch)) {
Source{}, value = create<ast::TypeConstructorExpression>(
result_type, // a vec4 Source{},
ast::ExpressionList{ result_type, // a vec4
value, parser_impl_.MakeNullValue(result_component_type), ast::ExpressionList{
parser_impl_.MakeNullValue(result_component_type), value, parser_impl_.MakeNullValue(result_component_type),
parser_impl_.MakeNullValue(result_component_type)}); parser_impl_.MakeNullValue(result_component_type),
parser_impl_.MakeNullValue(result_component_type)});
}
} }
// If necessary, convert the result to the signedness of the instruction // If necessary, convert the result to the signedness of the instruction

View File

@ -927,13 +927,13 @@ INSTANTIATE_TEST_SUITE_P(
"Usage(Texture( is_sampled ))"}, "Usage(Texture( is_sampled ))"},
// OpImageSampleDrefImplicitLod // OpImageSampleDrefImplicitLod
UsageImageAccessCase{"%result = OpImageSampleDrefImplicitLod " UsageImageAccessCase{"%result = OpImageSampleDrefImplicitLod "
"%v4float %sampled_image %coords %depth", "%float %sampled_image %coords %depth",
"Usage(Sampler( comparison ))", "Usage(Sampler( comparison ))",
"Usage(Texture( is_sampled depth ))"}, "Usage(Texture( is_sampled depth ))"},
// OpImageSampleDrefExplicitLod // OpImageSampleDrefExplicitLod
UsageImageAccessCase{ UsageImageAccessCase{
"%result = OpImageSampleDrefExplicitLod " "%result = OpImageSampleDrefExplicitLod "
"%v4float %sampled_image %coords %depth Lod %float_null", "%float %sampled_image %coords %depth Lod %float_null",
"Usage(Sampler( comparison ))", "Usage(Sampler( comparison ))",
"Usage(Texture( is_sampled depth ))"}, "Usage(Texture( is_sampled depth ))"},
@ -952,13 +952,13 @@ INSTANTIATE_TEST_SUITE_P(
"Usage(Texture( is_sampled ))"}, "Usage(Texture( is_sampled ))"},
// OpImageSampleProjDrefImplicitLod // OpImageSampleProjDrefImplicitLod
UsageImageAccessCase{"%result = OpImageSampleProjDrefImplicitLod " UsageImageAccessCase{"%result = OpImageSampleProjDrefImplicitLod "
"%v4float %sampled_image %coords %depth", "%float %sampled_image %coords %depth",
"Usage(Sampler( comparison ))", "Usage(Sampler( comparison ))",
"Usage(Texture( is_sampled depth ))"}, "Usage(Texture( is_sampled depth ))"},
// OpImageSampleProjDrefExplicitLod // OpImageSampleProjDrefExplicitLod
UsageImageAccessCase{ UsageImageAccessCase{
"%result = OpImageSampleProjDrefExplicitLod " "%result = OpImageSampleProjDrefExplicitLod "
"%v4float %sampled_image %coords %depth Lod %float_null", "%float %sampled_image %coords %depth Lod %float_null",
"Usage(Sampler( comparison ))", "Usage(Sampler( comparison ))",
"Usage(Texture( is_sampled depth ))"}, "Usage(Texture( is_sampled depth ))"},
@ -1714,7 +1714,7 @@ INSTANTIATE_TEST_SUITE_P(
%sampled_dref_image = OpSampledImage %si_ty %im %sam_dref %sampled_dref_image = OpSampledImage %si_ty %im %sam_dref
%200 = OpImageSampleImplicitLod %v4float %sampled_image %coords12 %200 = OpImageSampleImplicitLod %v4float %sampled_image %coords12
%210 = OpImageSampleDrefImplicitLod %v4float %sampled_dref_image %coords12 %depth %210 = OpImageSampleDrefImplicitLod %float %sampled_dref_image %coords12 %depth
)", )",
R"( R"(
Variable{ Variable{
@ -1772,22 +1772,16 @@ INSTANTIATE_TEST_SUITE_P(
VariableConst{ VariableConst{
x_210 x_210
none none
__vec_4__f32 __f32
{ {
TypeConstructor[not set]{ Call[not set]{
__vec_4__f32 Identifier[not set]{textureSampleCompare}
Call[not set]{ (
Identifier[not set]{textureSampleCompare} Identifier[not set]{x_20}
( Identifier[not set]{x_30}
Identifier[not set]{x_20} Identifier[not set]{coords12}
Identifier[not set]{x_30} ScalarConstructor[not set]{0.200000}
Identifier[not set]{coords12} )
ScalarConstructor[not set]{0.200000}
)
}
ScalarConstructor[not set]{0.000000}
ScalarConstructor[not set]{0.000000}
ScalarConstructor[not set]{0.000000}
} }
} }
} }
@ -1800,7 +1794,7 @@ INSTANTIATE_TEST_SUITE_P(
// ImageSampleDrefImplicitLod // ImageSampleDrefImplicitLod
ImageAccessCase{"%float 2D 0 0 0 1 Unknown", ImageAccessCase{"%float 2D 0 0 0 1 Unknown",
"%result = OpImageSampleDrefImplicitLod " "%result = OpImageSampleDrefImplicitLod "
"%v4float %sampled_image %coords12 %depth", "%float %sampled_image %coords12 %depth",
R"( R"(
Variable{ Variable{
Decorations{ Decorations{
@ -1821,25 +1815,19 @@ INSTANTIATE_TEST_SUITE_P(
__depth_texture_2d __depth_texture_2d
})", })",
R"( R"(
TypeConstructor[not set]{ Call[not set]{
__vec_4__f32 Identifier[not set]{textureSampleCompare}
Call[not set]{ (
Identifier[not set]{textureSampleCompare} Identifier[not set]{x_20}
( Identifier[not set]{x_10}
Identifier[not set]{x_20} Identifier[not set]{coords12}
Identifier[not set]{x_10} ScalarConstructor[not set]{0.200000}
Identifier[not set]{coords12} )
ScalarConstructor[not set]{0.200000}
)
}
ScalarConstructor[not set]{0.000000}
ScalarConstructor[not set]{0.000000}
ScalarConstructor[not set]{0.000000}
})"}, })"},
// ImageSampleDrefImplicitLod - arrayed // ImageSampleDrefImplicitLod - arrayed
ImageAccessCase{"%float 2D 0 1 0 1 Unknown", ImageAccessCase{"%float 2D 0 1 0 1 Unknown",
"%result = OpImageSampleDrefImplicitLod " "%result = OpImageSampleDrefImplicitLod "
"%v4float %sampled_image %coords123 %depth", "%float %sampled_image %coords123 %depth",
R"( R"(
Variable{ Variable{
Decorations{ Decorations{
@ -1860,35 +1848,29 @@ INSTANTIATE_TEST_SUITE_P(
__depth_texture_2d_array __depth_texture_2d_array
})", })",
R"( R"(
TypeConstructor[not set]{ Call[not set]{
__vec_4__f32 Identifier[not set]{textureSampleCompare}
Call[not set]{ (
Identifier[not set]{textureSampleCompare} Identifier[not set]{x_20}
( Identifier[not set]{x_10}
Identifier[not set]{x_20} MemberAccessor[not set]{
Identifier[not set]{x_10} Identifier[not set]{coords123}
Identifier[not set]{xy}
}
TypeConstructor[not set]{
__i32
MemberAccessor[not set]{ MemberAccessor[not set]{
Identifier[not set]{coords123} Identifier[not set]{coords123}
Identifier[not set]{xy} Identifier[not set]{z}
} }
TypeConstructor[not set]{ }
__i32 ScalarConstructor[not set]{0.200000}
MemberAccessor[not set]{ )
Identifier[not set]{coords123}
Identifier[not set]{z}
}
}
ScalarConstructor[not set]{0.200000}
)
}
ScalarConstructor[not set]{0.000000}
ScalarConstructor[not set]{0.000000}
ScalarConstructor[not set]{0.000000}
})"}, })"},
// ImageSampleDrefImplicitLod with ConstOffset // ImageSampleDrefImplicitLod with ConstOffset
ImageAccessCase{ ImageAccessCase{
"%float 2D 0 0 0 1 Unknown", "%float 2D 0 0 0 1 Unknown",
"%result = OpImageSampleDrefImplicitLod %v4float " "%result = OpImageSampleDrefImplicitLod %float "
"%sampled_image %coords12 %depth ConstOffset %offsets2d", "%sampled_image %coords12 %depth ConstOffset %offsets2d",
R"( R"(
Variable{ Variable{
@ -1910,26 +1892,20 @@ INSTANTIATE_TEST_SUITE_P(
__depth_texture_2d __depth_texture_2d
})", })",
R"( R"(
TypeConstructor[not set]{ Call[not set]{
__vec_4__f32 Identifier[not set]{textureSampleCompare}
Call[not set]{ (
Identifier[not set]{textureSampleCompare} Identifier[not set]{x_20}
( Identifier[not set]{x_10}
Identifier[not set]{x_20} Identifier[not set]{coords12}
Identifier[not set]{x_10} ScalarConstructor[not set]{0.200000}
Identifier[not set]{coords12} Identifier[not set]{offsets2d}
ScalarConstructor[not set]{0.200000} )
Identifier[not set]{offsets2d}
)
}
ScalarConstructor[not set]{0.000000}
ScalarConstructor[not set]{0.000000}
ScalarConstructor[not set]{0.000000}
})"}, })"},
// ImageSampleDrefImplicitLod arrayed with ConstOffset // ImageSampleDrefImplicitLod arrayed with ConstOffset
ImageAccessCase{ ImageAccessCase{
"%float 2D 0 1 0 1 Unknown", "%float 2D 0 1 0 1 Unknown",
"%result = OpImageSampleDrefImplicitLod %v4float " "%result = OpImageSampleDrefImplicitLod %float "
"%sampled_image %coords123 %depth ConstOffset %offsets2d", "%sampled_image %coords123 %depth ConstOffset %offsets2d",
R"( R"(
Variable{ Variable{
@ -1951,31 +1927,25 @@ INSTANTIATE_TEST_SUITE_P(
__depth_texture_2d_array __depth_texture_2d_array
})", })",
R"( R"(
TypeConstructor[not set]{ Call[not set]{
__vec_4__f32 Identifier[not set]{textureSampleCompare}
Call[not set]{ (
Identifier[not set]{textureSampleCompare} Identifier[not set]{x_20}
( Identifier[not set]{x_10}
Identifier[not set]{x_20} MemberAccessor[not set]{
Identifier[not set]{x_10} Identifier[not set]{coords123}
Identifier[not set]{xy}
}
TypeConstructor[not set]{
__i32
MemberAccessor[not set]{ MemberAccessor[not set]{
Identifier[not set]{coords123} Identifier[not set]{coords123}
Identifier[not set]{xy} Identifier[not set]{z}
} }
TypeConstructor[not set]{ }
__i32 ScalarConstructor[not set]{0.200000}
MemberAccessor[not set]{ Identifier[not set]{offsets2d}
Identifier[not set]{coords123} )
Identifier[not set]{z}
}
}
ScalarConstructor[not set]{0.200000}
Identifier[not set]{offsets2d}
)
}
ScalarConstructor[not set]{0.000000}
ScalarConstructor[not set]{0.000000}
ScalarConstructor[not set]{0.000000}
})"})); })"}));
INSTANTIATE_TEST_SUITE_P( INSTANTIATE_TEST_SUITE_P(
@ -3126,6 +3096,48 @@ INSTANTIATE_TEST_SUITE_P(
} }
})"}})); })"}}));
INSTANTIATE_TEST_SUITE_P(ImageFetch_Depth,
// In SPIR-V OpImageFetch always yields a vector of 4
// elements, even for depth images. But in WGSL,
// textureLoad on a depth image yields f32.
// crbug.com/tint/439
SpvParserTest_ImageAccessTest,
::testing::ValuesIn(std::vector<ImageAccessCase>{
// ImageFetch on depth image.
{"%float 2D 1 0 0 1 Unknown",
"%99 = OpImageFetch %v4float %im %vi12 ",
R"(Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
}
x_20
uniform_constant
__depth_texture_2d
})",
R"(VariableDeclStatement{
VariableConst{
x_99
none
__vec_4__f32
{
TypeConstructor[not set]{
__vec_4__f32
Call[not set]{
Identifier[not set]{textureLoad}
(
Identifier[not set]{x_20}
Identifier[not set]{vi12}
)
}
ScalarConstructor[not set]{0.000000}
ScalarConstructor[not set]{0.000000}
ScalarConstructor[not set]{0.000000}
}
}
}
})"}}));
INSTANTIATE_TEST_SUITE_P( INSTANTIATE_TEST_SUITE_P(
ImageFetch_Multisampled, ImageFetch_Multisampled,
SpvParserTest_ImageAccessTest, SpvParserTest_ImageAccessTest,
@ -3966,12 +3978,12 @@ INSTANTIATE_TEST_SUITE_P(
"", "",
{"Identifier[not set]{vf12}\n"}}, {"Identifier[not set]{vf12}\n"}},
{"%float 2D 1 0 0 1 Unknown", {"%float 2D 1 0 0 1 Unknown",
"%result = OpImageSampleDrefImplicitLod %v4float %sampled_image %vf12 " "%result = OpImageSampleDrefImplicitLod %float %sampled_image %vf12 "
"%depth", "%depth",
"", "",
{"Identifier[not set]{vf12}\n"}}, {"Identifier[not set]{vf12}\n"}},
{"%float 2D 1 0 0 1 Unknown", {"%float 2D 1 0 0 1 Unknown",
"%result = OpImageSampleDrefExplicitLod %v4float %sampled_image %vf12 " "%result = OpImageSampleDrefExplicitLod %float %sampled_image %vf12 "
"%depth Lod %f1", "%depth Lod %f1",
"", "",
{"Identifier[not set]{vf12}\n"}}, {"Identifier[not set]{vf12}\n"}},
@ -4022,7 +4034,7 @@ INSTANTIATE_TEST_SUITE_P(
} }
)"}}, )"}},
{"%float 2D 1 1 0 1 Unknown", {"%float 2D 1 1 0 1 Unknown",
"%result = OpImageSampleDrefImplicitLod %v4float %sampled_image " "%result = OpImageSampleDrefImplicitLod %float %sampled_image "
"%vf123 %depth", "%vf123 %depth",
"", "",
{ {
@ -4040,7 +4052,7 @@ INSTANTIATE_TEST_SUITE_P(
} }
)"}}, )"}},
{"%float 2D 1 1 0 1 Unknown", {"%float 2D 1 1 0 1 Unknown",
"%result = OpImageSampleDrefExplicitLod %v4float %sampled_image " "%result = OpImageSampleDrefExplicitLod %float %sampled_image "
"%vf123 %depth Lod %f1", "%vf123 %depth Lod %f1",
"", "",
{ {
@ -4354,23 +4366,23 @@ INSTANTIATE_TEST_SUITE_P(
{}}, {}},
// ImageSampleDrefImplicitLod // ImageSampleDrefImplicitLod
{"%uint 2D 0 0 0 1 Unknown", {"%uint 2D 0 0 0 1 Unknown",
"%result = OpImageSampleDrefImplicitLod %v4uint %sampled_image %vf12 " "%result = OpImageSampleDrefImplicitLod %uint %sampled_image %vf12 "
"%f1", "%f1",
"sampled image must have float component type", "sampled image must have float component type",
{}}, {}},
{"%int 2D 0 0 0 1 Unknown", {"%int 2D 0 0 0 1 Unknown",
"%result = OpImageSampleDrefImplicitLod %v4int %sampled_image %vf12 " "%result = OpImageSampleDrefImplicitLod %int %sampled_image %vf12 "
"%f1", "%f1",
"sampled image must have float component type", "sampled image must have float component type",
{}}, {}},
// ImageSampleDrefExplicitLod // ImageSampleDrefExplicitLod
{"%uint 2D 0 0 0 1 Unknown", {"%uint 2D 0 0 0 1 Unknown",
"%result = OpImageSampleDrefExplicitLod %v4uint %sampled_image %vf12 " "%result = OpImageSampleDrefExplicitLod %uint %sampled_image %vf12 "
"%f1 Lod %f1", "%f1 Lod %f1",
"sampled image must have float component type", "sampled image must have float component type",
{}}, {}},
{"%int 2D 0 0 0 1 Unknown", {"%int 2D 0 0 0 1 Unknown",
"%result = OpImageSampleDrefExplicitLod %v4int %sampled_image %vf12 " "%result = OpImageSampleDrefExplicitLod %int %sampled_image %vf12 "
"%f1 Lod %f1", "%f1 Lod %f1",
"sampled image must have float component type", "sampled image must have float component type",
{}}})); {}}}));