| // Copyright 2025 The Dawn & Tint Authors | |
| // | |
| // Redistribution and use in source and binary forms, with or without | |
| // modification, are permitted provided that the following conditions are met: | |
| // | |
| // 1. Redistributions of source code must retain the above copyright notice, this | |
| // list of conditions and the following disclaimer. | |
| // | |
| // 2. Redistributions in binary form must reproduce the above copyright notice, | |
| // this list of conditions and the following disclaimer in the documentation | |
| // and/or other materials provided with the distribution. | |
| // | |
| // 3. Neither the name of the copyright holder nor the names of its | |
| // contributors may be used to endorse or promote products derived from | |
| // this software without specific prior written permission. | |
| // | |
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
| // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | |
| // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
| // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
| // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
| // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| #include "src/tint/lang/spirv/reader/lower/texture.h" | |
| #include "src/tint/lang/core/ir/transform/helper_test.h" | |
| #include "src/tint/lang/spirv/ir/builtin_call.h" | |
| #include "src/tint/lang/spirv/type/image.h" | |
| #include "src/tint/lang/spirv/type/literal.h" | |
| #include "src/tint/lang/spirv/type/sampled_image.h" | |
| namespace tint::spirv::reader::lower { | |
| namespace { | |
| using namespace tint::core::fluent_types; // NOLINT | |
| using namespace tint::core::number_suffixes; // NOLINT | |
| class SpirvReader_TextureTest : public core::ir::transform::TransformTest { | |
| protected: | |
| void SetUp() override { | |
| core::ir::transform::TransformTest::SetUp(); | |
| mod.properties.Add(core::ir::Property::kAllowNonCoreTypes); | |
| mod.properties.Add(core::ir::Property::kAllowPointerToHandle); | |
| } | |
| /// @returns a literal operand with the given value | |
| core::ir::Value* Literal(uint32_t value) { | |
| return b.Constant(mod.constant_values.Get<core::constant::Scalar<u32>>( | |
| ty.Get<spirv::type::Literal>(), u32(value))); | |
| } | |
| }; | |
| using Dim = spirv::type::Dim; | |
| using Depth = spirv::type::Depth; | |
| using Arrayed = spirv::type::Arrayed; | |
| using Multisampled = spirv::type::Multisampled; | |
| using Sampled = spirv::type::Sampled; | |
| TEST_F(SpirvReader_TextureTest, Type_Image_1d) { | |
| b.Append(mod.root_block, [&] { | |
| auto* v = b.Var("wg", ty.ptr(handle, | |
| ty.Get<spirv::type::Image>( | |
| ty.f32(), Dim::kD1, Depth::kNotDepth, Arrayed::kNonArrayed, | |
| Multisampled::kSingleSampled, Sampled::kSamplingCompatible, | |
| core::TexelFormat::kUndefined, core::Access::kRead), | |
| read)); | |
| v->SetBindingPoint(1, 2); | |
| }); | |
| auto* src = R"( | |
| $B1: { # root | |
| %wg:ptr<handle, spirv.image<f32, 1d, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read>, read> = var @binding_point(1, 2) | |
| } | |
| )"; | |
| ASSERT_EQ(src, str()); | |
| Run(Texture); | |
| auto* expect = R"( | |
| $B1: { # root | |
| %wg:ptr<handle, texture_1d<f32>, read> = var @binding_point(1, 2) | |
| } | |
| )"; | |
| ASSERT_EQ(expect, str()); | |
| } | |
| TEST_F(SpirvReader_TextureTest, Type_Image_2d) { | |
| b.Append(mod.root_block, [&] { | |
| auto* v = b.Var("wg", ty.ptr(handle, | |
| ty.Get<spirv::type::Image>( | |
| ty.f32(), Dim::kD2, Depth::kNotDepth, Arrayed::kNonArrayed, | |
| Multisampled::kSingleSampled, Sampled::kSamplingCompatible, | |
| core::TexelFormat::kUndefined, core::Access::kRead), | |
| read)); | |
| v->SetBindingPoint(1, 2); | |
| }); | |
| auto* src = R"( | |
| $B1: { # root | |
| %wg:ptr<handle, spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read>, read> = var @binding_point(1, 2) | |
| } | |
| )"; | |
| ASSERT_EQ(src, str()); | |
| Run(Texture); | |
| auto* expect = R"( | |
| $B1: { # root | |
| %wg:ptr<handle, texture_2d<f32>, read> = var @binding_point(1, 2) | |
| } | |
| )"; | |
| ASSERT_EQ(expect, str()); | |
| } | |
| TEST_F(SpirvReader_TextureTest, Type_Image_3d) { | |
| b.Append(mod.root_block, [&] { | |
| auto* v = b.Var("wg", ty.ptr(handle, | |
| ty.Get<spirv::type::Image>( | |
| ty.f32(), Dim::kD3, Depth::kNotDepth, Arrayed::kNonArrayed, | |
| Multisampled::kSingleSampled, Sampled::kSamplingCompatible, | |
| core::TexelFormat::kUndefined, core::Access::kRead), | |
| read)); | |
| v->SetBindingPoint(1, 2); | |
| }); | |
| auto* src = R"( | |
| $B1: { # root | |
| %wg:ptr<handle, spirv.image<f32, 3d, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read>, read> = var @binding_point(1, 2) | |
| } | |
| )"; | |
| ASSERT_EQ(src, str()); | |
| Run(Texture); | |
| auto* expect = R"( | |
| $B1: { # root | |
| %wg:ptr<handle, texture_3d<f32>, read> = var @binding_point(1, 2) | |
| } | |
| )"; | |
| ASSERT_EQ(expect, str()); | |
| } | |
| TEST_F(SpirvReader_TextureTest, Type_Image_Cube) { | |
| b.Append(mod.root_block, [&] { | |
| auto* v = | |
| b.Var("wg", ty.ptr(handle, | |
| ty.Get<spirv::type::Image>( | |
| ty.f32(), Dim::kCube, Depth::kNotDepth, Arrayed::kNonArrayed, | |
| Multisampled::kSingleSampled, Sampled::kSamplingCompatible, | |
| core::TexelFormat::kUndefined, core::Access::kRead), | |
| read)); | |
| v->SetBindingPoint(1, 2); | |
| }); | |
| auto* src = R"( | |
| $B1: { # root | |
| %wg:ptr<handle, spirv.image<f32, cube, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read>, read> = var @binding_point(1, 2) | |
| } | |
| )"; | |
| ASSERT_EQ(src, str()); | |
| Run(Texture); | |
| auto* expect = R"( | |
| $B1: { # root | |
| %wg:ptr<handle, texture_cube<f32>, read> = var @binding_point(1, 2) | |
| } | |
| )"; | |
| ASSERT_EQ(expect, str()); | |
| } | |
| TEST_F(SpirvReader_TextureTest, Type_Image_SubpassData) { | |
| b.Append(mod.root_block, [&] { | |
| auto* v = b.Var( | |
| "wg", ty.ptr(handle, | |
| ty.Get<spirv::type::Image>( | |
| ty.f32(), Dim::kSubpassData, Depth::kNotDepth, Arrayed::kNonArrayed, | |
| Multisampled::kSingleSampled, Sampled::kSamplingCompatible, | |
| core::TexelFormat::kUndefined, core::Access::kRead), | |
| read)); | |
| v->SetBindingPoint(1, 2); | |
| }); | |
| auto* src = R"( | |
| $B1: { # root | |
| %wg:ptr<handle, spirv.image<f32, subpass_data, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read>, read> = var @binding_point(1, 2) | |
| } | |
| )"; | |
| ASSERT_EQ(src, str()); | |
| Run(Texture); | |
| auto* expect = R"( | |
| $B1: { # root | |
| %wg:ptr<handle, input_attachment<f32>, read> = var @binding_point(1, 2) | |
| } | |
| )"; | |
| ASSERT_EQ(expect, str()); | |
| } | |
| TEST_F(SpirvReader_TextureTest, Type_Image_Depth) { | |
| b.Append(mod.root_block, [&] { | |
| auto* v = b.Var("wg", ty.ptr(handle, | |
| ty.Get<spirv::type::Image>( | |
| ty.f32(), Dim::kD2, Depth::kDepth, Arrayed::kNonArrayed, | |
| Multisampled::kSingleSampled, Sampled::kSamplingCompatible, | |
| core::TexelFormat::kUndefined, core::Access::kRead), | |
| read)); | |
| v->SetBindingPoint(1, 2); | |
| }); | |
| auto* src = R"( | |
| $B1: { # root | |
| %wg:ptr<handle, spirv.image<f32, 2d, depth, non_arrayed, single_sampled, sampling_compatible, undefined, read>, read> = var @binding_point(1, 2) | |
| } | |
| )"; | |
| ASSERT_EQ(src, str()); | |
| Run(Texture); | |
| auto* expect = R"( | |
| $B1: { # root | |
| %wg:ptr<handle, texture_depth_2d, read> = var @binding_point(1, 2) | |
| } | |
| )"; | |
| ASSERT_EQ(expect, str()); | |
| } | |
| TEST_F(SpirvReader_TextureTest, Type_Image_DepthUnknown) { | |
| b.Append(mod.root_block, [&] { | |
| auto* v = b.Var("wg", ty.ptr(handle, | |
| ty.Get<spirv::type::Image>( | |
| ty.f32(), Dim::kD2, Depth::kUnknown, Arrayed::kNonArrayed, | |
| Multisampled::kSingleSampled, Sampled::kSamplingCompatible, | |
| core::TexelFormat::kUndefined, core::Access::kRead), | |
| read)); | |
| v->SetBindingPoint(1, 2); | |
| }); | |
| auto* src = R"( | |
| $B1: { # root | |
| %wg:ptr<handle, spirv.image<f32, 2d, depth_unknown, non_arrayed, single_sampled, sampling_compatible, undefined, read>, read> = var @binding_point(1, 2) | |
| } | |
| )"; | |
| ASSERT_EQ(src, str()); | |
| Run(Texture); | |
| auto* expect = R"( | |
| $B1: { # root | |
| %wg:ptr<handle, texture_2d<f32>, read> = var @binding_point(1, 2) | |
| } | |
| )"; | |
| ASSERT_EQ(expect, str()); | |
| } | |
| TEST_F(SpirvReader_TextureTest, Type_Image_Arrayed) { | |
| b.Append(mod.root_block, [&] { | |
| auto* v = b.Var("wg", ty.ptr(handle, | |
| ty.Get<spirv::type::Image>( | |
| ty.f32(), Dim::kD2, Depth::kNotDepth, Arrayed::kArrayed, | |
| Multisampled::kSingleSampled, Sampled::kSamplingCompatible, | |
| core::TexelFormat::kUndefined, core::Access::kRead), | |
| read)); | |
| v->SetBindingPoint(1, 2); | |
| }); | |
| auto* src = R"( | |
| $B1: { # root | |
| %wg:ptr<handle, spirv.image<f32, 2d, not_depth, arrayed, single_sampled, sampling_compatible, undefined, read>, read> = var @binding_point(1, 2) | |
| } | |
| )"; | |
| ASSERT_EQ(src, str()); | |
| Run(Texture); | |
| auto* expect = R"( | |
| $B1: { # root | |
| %wg:ptr<handle, texture_2d_array<f32>, read> = var @binding_point(1, 2) | |
| } | |
| )"; | |
| ASSERT_EQ(expect, str()); | |
| } | |
| TEST_F(SpirvReader_TextureTest, Type_Image_RW_Ops) { | |
| b.Append(mod.root_block, [&] { | |
| auto* v = | |
| b.Var("wg", ty.ptr(handle, | |
| ty.Get<spirv::type::Image>( | |
| ty.f32(), Dim::kD1, Depth::kNotDepth, Arrayed::kNonArrayed, | |
| Multisampled::kSingleSampled, Sampled::kReadWriteOpCompatible, | |
| core::TexelFormat::kRg32Float, core::Access::kRead), | |
| read)); | |
| v->SetBindingPoint(1, 2); | |
| }); | |
| auto* src = R"( | |
| $B1: { # root | |
| %wg:ptr<handle, spirv.image<f32, 1d, not_depth, non_arrayed, single_sampled, rw_op_compatible, rg32float, read>, read> = var @binding_point(1, 2) | |
| } | |
| )"; | |
| ASSERT_EQ(src, str()); | |
| Run(Texture); | |
| auto* expect = R"( | |
| $B1: { # root | |
| %wg:ptr<handle, texture_storage_1d<rg32float, read>, read> = var @binding_point(1, 2) | |
| } | |
| )"; | |
| ASSERT_EQ(expect, str()); | |
| } | |
| TEST_F(SpirvReader_TextureTest, Type_Image_TexelFormat) { | |
| b.Append(mod.root_block, [&] { | |
| auto* v = b.Var("wg", ty.ptr(handle, | |
| ty.Get<spirv::type::Image>( | |
| ty.f32(), Dim::kD1, Depth::kNotDepth, Arrayed::kNonArrayed, | |
| Multisampled::kSingleSampled, Sampled::kSamplingCompatible, | |
| core::TexelFormat::kRg32Float, core::Access::kRead), | |
| read)); | |
| v->SetBindingPoint(1, 2); | |
| }); | |
| auto* src = R"( | |
| $B1: { # root | |
| %wg:ptr<handle, spirv.image<f32, 1d, not_depth, non_arrayed, single_sampled, sampling_compatible, rg32float, read>, read> = var @binding_point(1, 2) | |
| } | |
| )"; | |
| ASSERT_EQ(src, str()); | |
| Run(Texture); | |
| auto* expect = R"( | |
| $B1: { # root | |
| %wg:ptr<handle, texture_1d<f32>, read> = var @binding_point(1, 2) | |
| } | |
| )"; | |
| ASSERT_EQ(expect, str()); | |
| } | |
| TEST_F(SpirvReader_TextureTest, UserCall) { | |
| auto* img_ty = ty.Get<spirv::type::Image>( | |
| ty.f32(), Dim::kD2, Depth::kNotDepth, Arrayed::kNonArrayed, Multisampled::kSingleSampled, | |
| Sampled::kSamplingCompatible, core::TexelFormat::kUndefined, core::Access::kRead); | |
| core::ir::Var* var = nullptr; | |
| b.Append(mod.root_block, [&] { | |
| var = b.Var("wg", ty.ptr(handle, img_ty, read)); | |
| var->SetBindingPoint(0, 0); | |
| }); | |
| auto* ld = b.Function("load", ty.void_()); | |
| auto* pt = b.FunctionParam(img_ty); | |
| ld->AppendParam(pt); | |
| ld->Block()->Append(b.Return(ld)); | |
| auto* main = b.Function("main", ty.void_()); | |
| b.Append(main->Block(), [&] { | |
| auto* tex = b.Load(var); | |
| b.Call(ld, tex); | |
| b.Return(main); | |
| }); | |
| auto* src = R"( | |
| $B1: { # root | |
| %wg:ptr<handle, spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read>, read> = var @binding_point(0, 0) | |
| } | |
| %load = func(%3:spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read>):void { | |
| $B2: { | |
| ret | |
| } | |
| } | |
| %main = func():void { | |
| $B3: { | |
| %5:spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read> = load %wg | |
| %6:void = call %load, %5 | |
| ret | |
| } | |
| } | |
| )"; | |
| ASSERT_EQ(src, str()); | |
| Run(Texture); | |
| auto* expect = R"( | |
| $B1: { # root | |
| %wg:ptr<handle, texture_2d<f32>, read> = var @binding_point(0, 0) | |
| } | |
| %main = func():void { | |
| $B2: { | |
| %3:texture_2d<f32> = load %wg | |
| %4:void = call %load, %3 | |
| ret | |
| } | |
| } | |
| %load = func(%6:texture_2d<f32>):void { | |
| $B3: { | |
| ret | |
| } | |
| } | |
| )"; | |
| ASSERT_EQ(expect, str()); | |
| } | |
| TEST_F(SpirvReader_TextureTest, FunctionParamPtr) { | |
| auto* img_ty = ty.Get<spirv::type::Image>( | |
| ty.f32(), Dim::kD2, Depth::kNotDepth, Arrayed::kNonArrayed, Multisampled::kSingleSampled, | |
| Sampled::kSamplingCompatible, core::TexelFormat::kUndefined, core::Access::kRead); | |
| core::ir::Var* var = nullptr; | |
| b.Append(mod.root_block, [&] { | |
| var = b.Var("wg", ty.ptr(handle, img_ty, read)); | |
| var->SetBindingPoint(0, 0); | |
| }); | |
| auto* ld = b.Function("load", ty.void_()); | |
| auto* pt = b.FunctionParam(ty.ptr(handle, img_ty, read)); | |
| ld->AppendParam(pt); | |
| ld->Block()->Append(b.Return(ld)); | |
| auto* main = b.Function("main", ty.void_()); | |
| b.Append(main->Block(), [&] { | |
| b.Call(ld, var); | |
| b.Return(main); | |
| }); | |
| auto* src = R"( | |
| $B1: { # root | |
| %wg:ptr<handle, spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read>, read> = var @binding_point(0, 0) | |
| } | |
| %load = func(%3:ptr<handle, spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read>, read>):void { | |
| $B2: { | |
| ret | |
| } | |
| } | |
| %main = func():void { | |
| $B3: { | |
| %5:void = call %load, %wg | |
| ret | |
| } | |
| } | |
| )"; | |
| ASSERT_EQ(src, str()); | |
| Run(Texture); | |
| auto* expect = R"( | |
| $B1: { # root | |
| %wg:ptr<handle, texture_2d<f32>, read> = var @binding_point(0, 0) | |
| } | |
| %main = func():void { | |
| $B2: { | |
| %3:texture_2d<f32> = load %wg | |
| %4:void = call %load, %3 | |
| ret | |
| } | |
| } | |
| %load = func(%6:texture_2d<f32>):void { | |
| $B3: { | |
| ret | |
| } | |
| } | |
| )"; | |
| ASSERT_EQ(expect, str()); | |
| } | |
| TEST_F(SpirvReader_TextureTest, FunctionParamPtr_Loads) { | |
| auto* img_ty = ty.Get<spirv::type::Image>( | |
| ty.f32(), Dim::kD2, Depth::kNotDepth, Arrayed::kNonArrayed, Multisampled::kSingleSampled, | |
| Sampled::kSamplingCompatible, core::TexelFormat::kUndefined, core::Access::kRead); | |
| core::ir::Var* var = nullptr; | |
| b.Append(mod.root_block, [&] { | |
| var = b.Var("wg", ty.ptr(handle, img_ty, read)); | |
| var->SetBindingPoint(0, 0); | |
| }); | |
| auto* ld = b.Function("load", ty.void_()); | |
| auto* pt = b.FunctionParam(ty.ptr(handle, img_ty, read)); | |
| ld->AppendParam(pt); | |
| b.Append(ld->Block(), [&] { | |
| auto* w = b.Let("w", pt); | |
| auto* x = b.Let("x", w); | |
| auto* y = b.Let("y", x); | |
| auto* z = b.Let("z", y); | |
| b.Load(z); | |
| b.Return(ld); | |
| }); | |
| auto* main = b.Function("main", ty.void_()); | |
| b.Append(main->Block(), [&] { | |
| b.Call(ld, var); | |
| b.Return(main); | |
| }); | |
| auto* src = R"( | |
| $B1: { # root | |
| %wg:ptr<handle, spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read>, read> = var @binding_point(0, 0) | |
| } | |
| %load = func(%3:ptr<handle, spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read>, read>):void { | |
| $B2: { | |
| %w:ptr<handle, spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read>, read> = let %3 | |
| %x:ptr<handle, spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read>, read> = let %w | |
| %y:ptr<handle, spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read>, read> = let %x | |
| %z:ptr<handle, spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read>, read> = let %y | |
| %8:spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read> = load %z | |
| ret | |
| } | |
| } | |
| %main = func():void { | |
| $B3: { | |
| %10:void = call %load, %wg | |
| ret | |
| } | |
| } | |
| )"; | |
| ASSERT_EQ(src, str()); | |
| Run(Texture); | |
| auto* expect = R"( | |
| $B1: { # root | |
| %wg:ptr<handle, texture_2d<f32>, read> = var @binding_point(0, 0) | |
| } | |
| %main = func():void { | |
| $B2: { | |
| %3:texture_2d<f32> = load %wg | |
| %4:void = call %load, %3 | |
| ret | |
| } | |
| } | |
| %load = func(%6:texture_2d<f32>):void { | |
| $B3: { | |
| ret | |
| } | |
| } | |
| )"; | |
| ASSERT_EQ(expect, str()); | |
| } | |
| TEST_F(SpirvReader_TextureTest, FunctionParamMultiPtr) { | |
| auto* img_ty = ty.Get<spirv::type::Image>( | |
| ty.f32(), Dim::kD2, Depth::kNotDepth, Arrayed::kNonArrayed, Multisampled::kSingleSampled, | |
| Sampled::kSamplingCompatible, core::TexelFormat::kUndefined, core::Access::kRead); | |
| core::ir::Var* var = nullptr; | |
| b.Append(mod.root_block, [&] { | |
| var = b.Var("wg", ty.ptr(handle, img_ty, read)); | |
| var->SetBindingPoint(0, 0); | |
| }); | |
| auto* ld3 = b.Function("load3", ty.void_()); | |
| auto* pt3 = b.FunctionParam(ty.ptr(handle, img_ty, read)); | |
| ld3->AppendParam(pt3); | |
| b.Append(ld3->Block(), [&] { | |
| b.Load(pt3); | |
| b.Return(ld3); | |
| }); | |
| auto* ld2 = b.Function("load2", ty.void_()); | |
| auto* pt2 = b.FunctionParam(ty.ptr(handle, img_ty, read)); | |
| ld2->AppendParam(pt2); | |
| b.Append(ld2->Block(), [&] { | |
| b.Call(ld3, pt2); | |
| b.Return(ld2); | |
| }); | |
| auto* ld = b.Function("load", ty.void_()); | |
| auto* pt = b.FunctionParam(ty.ptr(handle, img_ty, read)); | |
| ld->AppendParam(pt); | |
| b.Append(ld->Block(), [&] { | |
| b.Call(ld2, pt); | |
| b.Return(ld); | |
| }); | |
| auto* main = b.Function("main", ty.void_()); | |
| b.Append(main->Block(), [&] { | |
| b.Call(ld, var); | |
| b.Return(main); | |
| }); | |
| auto* src = R"( | |
| $B1: { # root | |
| %wg:ptr<handle, spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read>, read> = var @binding_point(0, 0) | |
| } | |
| %load3 = func(%3:ptr<handle, spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read>, read>):void { | |
| $B2: { | |
| %4:spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read> = load %3 | |
| ret | |
| } | |
| } | |
| %load2 = func(%6:ptr<handle, spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read>, read>):void { | |
| $B3: { | |
| %7:void = call %load3, %6 | |
| ret | |
| } | |
| } | |
| %load = func(%9:ptr<handle, spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read>, read>):void { | |
| $B4: { | |
| %10:void = call %load2, %9 | |
| ret | |
| } | |
| } | |
| %main = func():void { | |
| $B5: { | |
| %12:void = call %load, %wg | |
| ret | |
| } | |
| } | |
| )"; | |
| ASSERT_EQ(src, str()); | |
| Run(Texture); | |
| auto* expect = R"( | |
| $B1: { # root | |
| %wg:ptr<handle, texture_2d<f32>, read> = var @binding_point(0, 0) | |
| } | |
| %main = func():void { | |
| $B2: { | |
| %3:texture_2d<f32> = load %wg | |
| %4:void = call %load, %3 | |
| ret | |
| } | |
| } | |
| %load = func(%6:texture_2d<f32>):void { | |
| $B3: { | |
| %7:void = call %load2, %6 | |
| ret | |
| } | |
| } | |
| %load2 = func(%9:texture_2d<f32>):void { | |
| $B4: { | |
| %10:void = call %load3, %9 | |
| ret | |
| } | |
| } | |
| %load3 = func(%12:texture_2d<f32>):void { | |
| $B5: { | |
| ret | |
| } | |
| } | |
| )"; | |
| ASSERT_EQ(expect, str()); | |
| } | |
| // A sampling function is reached through a forwarding function that has two callers. | |
| TEST_F(SpirvReader_TextureTest, FunctionParamPtr_ForwardedTwice) { | |
| auto* img_ty = ty.Get<spirv::type::Image>( | |
| ty.f32(), Dim::kD2, Depth::kNotDepth, Arrayed::kNonArrayed, Multisampled::kSingleSampled, | |
| Sampled::kSamplingCompatible, core::TexelFormat::kUndefined, core::Access::kRead); | |
| core::ir::Var* tex = nullptr; | |
| core::ir::Var* samp = nullptr; | |
| b.Append(mod.root_block, [&] { | |
| tex = b.Var("tex", ty.ptr(handle, img_ty, read)); | |
| tex->SetBindingPoint(0, 0); | |
| samp = b.Var("samp", ty.ptr(handle, ty.sampler(), read)); | |
| samp->SetBindingPoint(0, 1); | |
| }); | |
| auto* f1 = b.Function("f1", ty.vec4f()); | |
| auto* f1_t = b.FunctionParam(ty.ptr(handle, img_ty, read)); | |
| auto* f1_s = b.FunctionParam(ty.ptr(handle, ty.sampler(), read)); | |
| f1->AppendParam(f1_t); | |
| f1->AppendParam(f1_s); | |
| b.Append(f1->Block(), [&] { | |
| auto* t = b.Load(f1_t); | |
| auto* s = b.Load(f1_s); | |
| auto* sampled = b.CallExplicit<spirv::ir::BuiltinCall>( | |
| ty.Get<spirv::type::SampledImage>(img_ty), spirv::BuiltinFn::kOpSampledImage, | |
| Vector<core::ir::TemplateParameter, 1>{img_ty}, t, s); | |
| auto* result = | |
| b.Call<spirv::ir::BuiltinCall>(ty.vec4f(), spirv::BuiltinFn::kImageSampleExplicitLod, | |
| sampled, b.Zero<vec2f>(), Literal(2u), 0_f); | |
| b.Return(f1, result); | |
| }); | |
| auto* f2 = b.Function("f2", ty.vec4f()); | |
| auto* f2_t = b.FunctionParam(ty.ptr(handle, img_ty, read)); | |
| auto* f2_s = b.FunctionParam(ty.ptr(handle, ty.sampler(), read)); | |
| f2->AppendParam(f2_t); | |
| f2->AppendParam(f2_s); | |
| b.Append(f2->Block(), [&] { b.Return(f2, b.Call(f1, f2_t, f2_s)); }); | |
| auto* f3 = b.Function("f3", ty.vec4f()); | |
| auto* f3_t = b.FunctionParam(ty.ptr(handle, img_ty, read)); | |
| auto* f3_s = b.FunctionParam(ty.ptr(handle, ty.sampler(), read)); | |
| f3->AppendParam(f3_t); | |
| f3->AppendParam(f3_s); | |
| b.Append(f3->Block(), [&] { b.Return(f3, b.Call(f2, f3_t, f3_s)); }); | |
| auto* main = b.Function("main", ty.void_()); | |
| b.Append(main->Block(), [&] { | |
| b.Call(f2, tex, samp); | |
| b.Call(f3, tex, samp); | |
| b.Return(main); | |
| }); | |
| auto* src = R"( | |
| $B1: { # root | |
| %tex:ptr<handle, spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read>, read> = var @binding_point(0, 0) | |
| %samp:ptr<handle, sampler, read> = var @binding_point(0, 1) | |
| } | |
| %f1 = func(%4:ptr<handle, spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read>, read>, %5:ptr<handle, sampler, read>):vec4<f32> { | |
| $B2: { | |
| %6:spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read> = load %4 | |
| %7:sampler = load %5 | |
| %8:spirv.sampled_image<spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read>> = spirv.op_sampled_image<spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read>> %6, %7 | |
| %9:vec4<f32> = spirv.image_sample_explicit_lod %8, vec2<f32>(0.0f), 2u, 0.0f | |
| ret %9 | |
| } | |
| } | |
| %f2 = func(%11:ptr<handle, spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read>, read>, %12:ptr<handle, sampler, read>):vec4<f32> { | |
| $B3: { | |
| %13:vec4<f32> = call %f1, %11, %12 | |
| ret %13 | |
| } | |
| } | |
| %f3 = func(%15:ptr<handle, spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, sampling_compatible, undefined, read>, read>, %16:ptr<handle, sampler, read>):vec4<f32> { | |
| $B4: { | |
| %17:vec4<f32> = call %f2, %15, %16 | |
| ret %17 | |
| } | |
| } | |
| %main = func():void { | |
| $B5: { | |
| %19:vec4<f32> = call %f2, %tex, %samp | |
| %20:vec4<f32> = call %f3, %tex, %samp | |
| ret | |
| } | |
| } | |
| )"; | |
| ASSERT_EQ(src, str()); | |
| Run(Texture); | |
| auto* expect = R"( | |
| $B1: { # root | |
| %tex:ptr<handle, texture_2d<f32>, read> = var @binding_point(0, 0) | |
| %samp:ptr<handle, sampler, read> = var @binding_point(0, 1) | |
| } | |
| %main = func():void { | |
| $B2: { | |
| %4:texture_2d<f32> = load %tex | |
| %5:sampler = load %samp | |
| %6:vec4<f32> = call %f2, %4, %5 | |
| %8:texture_2d<f32> = load %tex | |
| %9:sampler = load %samp | |
| %10:vec4<f32> = call %f3, %8, %9 | |
| ret | |
| } | |
| } | |
| %f2 = func(%12:texture_2d<f32>, %13:sampler):vec4<f32> { | |
| $B3: { | |
| %14:vec4<f32> = call %f1, %12, %13 | |
| ret %14 | |
| } | |
| } | |
| %f3 = func(%16:texture_2d<f32>, %17:sampler):vec4<f32> { | |
| $B4: { | |
| %18:vec4<f32> = call %f2, %16, %17 | |
| ret %18 | |
| } | |
| } | |
| %f1 = func(%19:texture_2d<f32>, %20:sampler):vec4<f32> { | |
| $B5: { | |
| %21:vec4<f32> = textureSampleLevel %19, %20, vec2<f32>(0.0f), 0.0f | |
| ret %21 | |
| } | |
| } | |
| )"; | |
| ASSERT_EQ(expect, str()); | |
| } | |
| // Vulkan memory model image operands: the mask bits and the scope values they take. | |
| // https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#Image_Operands | |
| constexpr uint32_t kMakeTexelAvailable = 0x100; | |
| constexpr uint32_t kMakeTexelVisible = 0x200; | |
| constexpr uint32_t kNonPrivateTexel = 0x400; | |
| constexpr uint32_t kVolatileTexel = 0x800; | |
| constexpr uint32_t kDeviceScope = 1; | |
| constexpr uint32_t kWorkgroupScope = 2; | |
| class SpirvReader_TextureMemoryModelTest : public SpirvReader_TextureTest { | |
| protected: | |
| core::ir::Value* Literal(uint32_t value) { | |
| return b.Constant(mod.constant_values.Get<core::constant::Scalar<u32>>( | |
| ty.Get<spirv::type::Literal>(), u32(value))); | |
| } | |
| /// Builds a compute entry point that reads a storage texture with `read_mask` and the given | |
| /// extra read operands, then writes it back with `write_mask` and the extra write operands. | |
| void Build(uint32_t read_mask, | |
| Vector<core::ir::Value*, 1> read_operands, | |
| uint32_t write_mask, | |
| Vector<core::ir::Value*, 1> write_operands) { | |
| auto* img_ty = ty.Get<spirv::type::Image>( | |
| ty.f32(), Dim::kD2, Depth::kNotDepth, Arrayed::kNonArrayed, | |
| Multisampled::kSingleSampled, Sampled::kReadWriteOpCompatible, | |
| core::TexelFormat::kRgba32Float, core::Access::kReadWrite); | |
| core::ir::Var* v = nullptr; | |
| b.Append(mod.root_block, [&] { | |
| v = b.Var("img", ty.ptr(handle, img_ty, read)); | |
| v->SetBindingPoint(0, 0); | |
| }); | |
| auto* ep = b.ComputeFunction("main"); | |
| b.Append(ep->Block(), [&] { | |
| core::ir::Value* img = b.Load(v)->Result(); | |
| core::ir::Value* coords = b.Zero(ty.vec2<i32>()); | |
| Vector<core::ir::Value*, 5> read_args = {img, coords, Literal(read_mask)}; | |
| for (auto* operand : read_operands) { | |
| read_args.Push(operand); | |
| } | |
| core::ir::Value* texel = b.Call<spirv::ir::BuiltinCall>( | |
| ty.vec4<f32>(), spirv::BuiltinFn::kImageRead, read_args) | |
| ->Result(); | |
| Vector<core::ir::Value*, 6> write_args = {img, coords, texel, Literal(write_mask)}; | |
| for (auto* operand : write_operands) { | |
| write_args.Push(operand); | |
| } | |
| b.Call<spirv::ir::BuiltinCall>(ty.void_(), spirv::BuiltinFn::kImageWrite, write_args); | |
| b.Return(ep); | |
| }); | |
| } | |
| }; | |
| // NonPrivateTexel and Workgroup scope MakeTexelVisible/MakeTexelAvailable are dropped. | |
| TEST_F(SpirvReader_TextureMemoryModelTest, WorkgroupScope_Dropped) { | |
| Build(kMakeTexelVisible | kNonPrivateTexel, {b.Constant(u32(kWorkgroupScope))}, | |
| kMakeTexelAvailable | kNonPrivateTexel, {b.Constant(u32(kWorkgroupScope))}); | |
| auto* src = R"( | |
| $B1: { # root | |
| %img:ptr<handle, spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, rw_op_compatible, rgba32float, read_write>, read> = var @binding_point(0, 0) | |
| } | |
| %main = @compute @workgroup_size(1u, 1u, 1u) func():void { | |
| $B2: { | |
| %3:spirv.image<f32, 2d, not_depth, non_arrayed, single_sampled, rw_op_compatible, rgba32float, read_write> = load %img | |
| %4:vec4<f32> = spirv.image_read %3, vec2<i32>(0i), 1536u, 2u | |
| %5:void = spirv.image_write %3, vec2<i32>(0i), %4, 1280u, 2u | |
| ret | |
| } | |
| } | |
| )"; | |
| ASSERT_EQ(src, str()); | |
| Run(Texture); | |
| auto* expect = R"( | |
| $B1: { # root | |
| %img:ptr<handle, texture_storage_2d<rgba32float, read_write>, read> = var @binding_point(0, 0) | |
| } | |
| %main = @compute @workgroup_size(1u, 1u, 1u) func():void { | |
| $B2: { | |
| %3:texture_storage_2d<rgba32float, read_write> = load %img | |
| %4:vec4<f32> = textureLoad %3, vec2<i32>(0i) | |
| %5:void = textureStore %3, vec2<i32>(0i), %4 | |
| ret | |
| } | |
| } | |
| )"; | |
| ASSERT_EQ(expect, str()); | |
| } | |
| // MakeTexelVisible with a scope wider than Workgroup is rejected. | |
| TEST_F(SpirvReader_TextureMemoryModelTest, VisibleDeviceScope_Rejected) { | |
| Build(kMakeTexelVisible | kNonPrivateTexel, {b.Constant(u32(kDeviceScope))}, 0, {}); | |
| auto res = RunWithFailure(Texture); | |
| ASSERT_NE(res, Success); | |
| EXPECT_EQ(res.Failure().reason, | |
| "MakeTexelVisible image operand with Device scope is not supported"); | |
| } | |
| // MakeTexelAvailable with a scope wider than Workgroup is rejected. | |
| TEST_F(SpirvReader_TextureMemoryModelTest, AvailableDeviceScope_Rejected) { | |
| Build(0, {}, kMakeTexelAvailable | kNonPrivateTexel, {b.Constant(u32(kDeviceScope))}); | |
| auto res = RunWithFailure(Texture); | |
| ASSERT_NE(res, Success); | |
| EXPECT_EQ(res.Failure().reason, | |
| "MakeTexelAvailable image operand with Device scope is not supported"); | |
| } | |
| // VolatileTexel is rejected. | |
| TEST_F(SpirvReader_TextureMemoryModelTest, VolatileTexel_Rejected) { | |
| Build(0, {}, kVolatileTexel, {}); | |
| auto res = RunWithFailure(Texture); | |
| ASSERT_NE(res, Success); | |
| EXPECT_EQ(res.Failure().reason, "VolatileTexel image operand is not supported"); | |
| } | |
| } // namespace | |
| } // namespace tint::spirv::reader::lower |