| // Copyright 2023 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/core/type/pointer.h" |
| #include "src/tint/lang/core/type/sampled_texture.h" |
| #include "src/tint/lang/spirv/writer/common/helper_test.h" |
| |
| namespace tint::spirv::writer { |
| namespace { |
| |
| using namespace tint::core::fluent_types; // NOLINT |
| using namespace tint::core::number_suffixes; // NOLINT |
| |
| TEST_F(SpirvWriterTest, FunctionVar_NoInit) { |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| b.Var("v", ty.ptr<function, i32>()); |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST("%v = OpVariable %_ptr_Function_int Function"); |
| } |
| |
| TEST_F(SpirvWriterTest, FunctionVar_WithInit) { |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| auto* v = b.Var("v", ty.ptr<function, i32>()); |
| v->SetInitializer(b.Constant(42_i)); |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST("%v = OpVariable %_ptr_Function_int Function"); |
| EXPECT_INST("OpStore %v %int_42"); |
| } |
| |
| TEST_F(SpirvWriterTest, FunctionVar_DeclInsideBlock) { |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| auto* i = b.If(true); |
| b.Append(i->True(), [&] { |
| auto* v = b.Var("v", ty.ptr<function, i32>()); |
| v->SetInitializer(b.Constant(42_i)); |
| b.ExitIf(i); |
| }); |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST(R"( |
| %foo = OpFunction %void None %3 |
| %4 = OpLabel |
| %v = OpVariable %_ptr_Function_int Function |
| OpSelectionMerge %5 None |
| OpBranchConditional %true %6 %5 |
| %6 = OpLabel |
| OpStore %v %int_42 |
| OpBranch %5 |
| %5 = OpLabel |
| OpReturn |
| OpFunctionEnd |
| )"); |
| } |
| |
| TEST_F(SpirvWriterTest, FunctionVar_Load) { |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| auto* v = b.Var("v", ty.ptr<function, i32>()); |
| auto* result = b.Load(v); |
| b.Return(func); |
| mod.SetName(result, "result"); |
| }); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST("%v = OpVariable %_ptr_Function_int Function"); |
| EXPECT_INST("%result = OpLoad %int %v"); |
| } |
| |
| TEST_F(SpirvWriterTest, FunctionVar_Store) { |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| auto* v = b.Var("v", ty.ptr<function, i32>()); |
| b.Store(v, 42_i); |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST("%v = OpVariable %_ptr_Function_int Function"); |
| EXPECT_INST("OpStore %v %int_42"); |
| } |
| |
| TEST_F(SpirvWriterTest, PrivateVar_NoInit) { |
| mod.root_block->Append(b.Var("v", ty.ptr<private_, i32>())); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST("%v = OpVariable %_ptr_Private_int Private"); |
| } |
| |
| TEST_F(SpirvWriterTest, PrivateVar_WithInit) { |
| auto* v = b.Var("v", ty.ptr<private_, i32>()); |
| v->SetInitializer(b.Constant(42_i)); |
| mod.root_block->Append(v); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST("%v = OpVariable %_ptr_Private_int Private %int_42"); |
| } |
| |
| TEST_F(SpirvWriterTest, PrivateVar_LoadAndStore) { |
| auto* v = b.Var("v", ty.ptr<private_, i32>()); |
| v->SetInitializer(b.Constant(42_i)); |
| mod.root_block->Append(v); |
| |
| auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment); |
| b.Append(func->Block(), [&] { |
| auto* load = b.Load(v); |
| auto* add = b.Add(ty.i32(), load, 1_i); |
| b.Store(v, add); |
| b.Return(func); |
| mod.SetName(load, "load"); |
| mod.SetName(add, "add"); |
| }); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST("%v = OpVariable %_ptr_Private_int Private %int_42"); |
| EXPECT_INST("%load = OpLoad %int %v"); |
| EXPECT_INST("OpStore %v %add"); |
| } |
| |
| TEST_F(SpirvWriterTest, WorkgroupVar) { |
| mod.root_block->Append(b.Var("v", ty.ptr<workgroup, i32>())); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST("%v = OpVariable %_ptr_Workgroup_int Workgroup"); |
| } |
| |
| TEST_F(SpirvWriterTest, WorkgroupVar_LoadAndStore) { |
| auto* v = mod.root_block->Append(b.Var("v", ty.ptr<workgroup, i32>())); |
| |
| auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kCompute, |
| std::array{1u, 1u, 1u}); |
| b.Append(func->Block(), [&] { |
| auto* load = b.Load(v); |
| auto* add = b.Add(ty.i32(), load, 1_i); |
| b.Store(v, add); |
| b.Return(func); |
| mod.SetName(load, "load"); |
| mod.SetName(add, "add"); |
| }); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST("%v = OpVariable %_ptr_Workgroup_int Workgroup"); |
| EXPECT_INST("%load = OpLoad %int %v"); |
| EXPECT_INST("OpStore %v %add"); |
| } |
| |
| TEST_F(SpirvWriterTest, WorkgroupVar_ZeroInitializeWithExtension) { |
| mod.root_block->Append(b.Var("v", ty.ptr<workgroup, i32>())); |
| |
| Options opts{}; |
| opts.disable_workgroup_init = false; |
| opts.use_zero_initialize_workgroup_memory_extension = true; |
| |
| // Create a writer with the zero_init_workgroup_memory flag set to `true`. |
| ASSERT_TRUE(Generate(opts)) << Error() << output_; |
| EXPECT_INST("%4 = OpConstantNull %int"); |
| EXPECT_INST("%v = OpVariable %_ptr_Workgroup_int Workgroup %4"); |
| } |
| |
| TEST_F(SpirvWriterTest, StorageVar_ReadOnly) { |
| auto* v = b.Var("v", ty.ptr<storage, i32, read>()); |
| v->SetBindingPoint(0, 0); |
| mod.root_block->Append(v); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST(R"( |
| OpDecorate %v_block Block |
| OpDecorate %1 DescriptorSet 0 |
| OpDecorate %1 Binding 0 |
| OpDecorate %1 NonWritable |
| )"); |
| EXPECT_INST(R"( |
| %v_block = OpTypeStruct %int ; Block |
| %_ptr_StorageBuffer_v_block = OpTypePointer StorageBuffer %v_block |
| %1 = OpVariable %_ptr_StorageBuffer_v_block StorageBuffer ; DescriptorSet 0, Binding 0, NonWritable |
| )"); |
| } |
| |
| TEST_F(SpirvWriterTest, StorageVar_LoadAndStore) { |
| auto* v = b.Var("v", ty.ptr<storage, i32, read_write>()); |
| v->SetBindingPoint(0, 0); |
| mod.root_block->Append(v); |
| |
| auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kCompute, |
| std::array{1u, 1u, 1u}); |
| b.Append(func->Block(), [&] { |
| auto* load = b.Load(v); |
| auto* add = b.Add(ty.i32(), load, 1_i); |
| b.Store(v, add); |
| b.Return(func); |
| mod.SetName(load, "load"); |
| mod.SetName(add, "add"); |
| }); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST(R"( |
| OpDecorate %1 Coherent |
| )"); |
| |
| EXPECT_INST(R"( |
| %9 = OpAccessChain %_ptr_StorageBuffer_int %1 %uint_0 |
| %load = OpLoad %int %9 None |
| %add = OpIAdd %int %load %int_1 |
| %16 = OpAccessChain %_ptr_StorageBuffer_int %1 %uint_0 |
| OpStore %16 %add None |
| )"); |
| } |
| |
| TEST_F(SpirvWriterTest, StorageVar_WithVulkan) { |
| auto* v = b.Var("v", ty.ptr<storage, i32, read_write>()); |
| v->SetBindingPoint(0, 0); |
| mod.root_block->Append(v); |
| |
| auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kCompute, |
| std::array{1u, 1u, 1u}); |
| b.Append(func->Block(), [&] { |
| auto* load = b.Load(v); |
| auto* add = b.Add(ty.i32(), load, 1_i); |
| b.Store(v, add); |
| b.Return(func); |
| mod.SetName(load, "load"); |
| mod.SetName(add, "add"); |
| }); |
| |
| Options opts; |
| opts.use_vulkan_memory_model = true; |
| |
| ASSERT_TRUE(Generate(opts)) << Error() << output_; |
| EXPECT_INST(R"( OpCapability Shader |
| OpCapability VulkanMemoryModel |
| OpCapability VulkanMemoryModelDeviceScope |
| OpExtension "SPV_KHR_vulkan_memory_model" |
| OpMemoryModel Logical Vulkan |
| OpEntryPoint GLCompute %foo "foo" |
| OpExecutionMode %foo LocalSize 1 1 1 |
| |
| ; Debug Information |
| OpMemberName %v_block 0 "inner" |
| OpName %v_block "v_block" ; id %3 |
| OpName %foo "foo" ; id %5 |
| OpName %load "load" ; id %13 |
| OpName %add "add" ; id %14 |
| |
| ; Annotations |
| OpMemberDecorate %v_block 0 Offset 0 |
| OpDecorate %v_block Block |
| OpDecorate %1 DescriptorSet 0 |
| OpDecorate %1 Binding 0 |
| |
| ; Types, variables and constants |
| %int = OpTypeInt 32 1 |
| %v_block = OpTypeStruct %int ; Block |
| %_ptr_StorageBuffer_v_block = OpTypePointer StorageBuffer %v_block |
| %1 = OpVariable %_ptr_StorageBuffer_v_block StorageBuffer ; DescriptorSet 0, Binding 0 |
| %void = OpTypeVoid |
| %7 = OpTypeFunction %void |
| %_ptr_StorageBuffer_int = OpTypePointer StorageBuffer %int |
| %uint = OpTypeInt 32 0 |
| %uint_0 = OpConstant %uint 0 |
| %int_1 = OpConstant %int 1 |
| |
| ; Function foo |
| %foo = OpFunction %void None %7 |
| %8 = OpLabel |
| %9 = OpAccessChain %_ptr_StorageBuffer_int %1 %uint_0 |
| %load = OpLoad %int %9 NonPrivatePointer |
| %add = OpIAdd %int %load %int_1 |
| %16 = OpAccessChain %_ptr_StorageBuffer_int %1 %uint_0 |
| OpStore %16 %add NonPrivatePointer |
| OpReturn |
| OpFunctionEnd)"); |
| } |
| |
| TEST_F(SpirvWriterTest, StorageVar_Workgroup_WithVulkan) { |
| auto* v = b.Var("v", ty.ptr<workgroup, i32, read_write>()); |
| v->SetBindingPoint(0, 0); |
| mod.root_block->Append(v); |
| |
| auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kCompute, |
| std::array{1u, 1u, 1u}); |
| b.Append(func->Block(), [&] { |
| auto* load = b.Load(v); |
| auto* add = b.Add(ty.i32(), load, 1_i); |
| b.Store(v, add); |
| b.Return(func); |
| mod.SetName(load, "load"); |
| mod.SetName(add, "add"); |
| }); |
| |
| Options opts; |
| opts.use_vulkan_memory_model = true; |
| |
| ASSERT_TRUE(Generate(opts)) << Error() << output_; |
| EXPECT_INST(R"( OpCapability Shader |
| OpCapability VulkanMemoryModel |
| OpCapability VulkanMemoryModelDeviceScope |
| OpExtension "SPV_KHR_vulkan_memory_model" |
| OpMemoryModel Logical Vulkan |
| OpEntryPoint GLCompute %foo "foo" %foo_local_invocation_index_Input |
| OpExecutionMode %foo LocalSize 1 1 1 |
| |
| ; Debug Information |
| OpName %v "v" ; id %1 |
| OpName %foo_local_invocation_index_Input "foo_local_invocation_index_Input" ; id %4 |
| OpName %foo_inner "foo_inner" ; id %7 |
| OpName %tint_local_index "tint_local_index" ; id %9 |
| OpName %load "load" ; id %21 |
| OpName %add "add" ; id %22 |
| OpName %foo "foo" ; id %24 |
| |
| ; Annotations |
| OpDecorate %foo_local_invocation_index_Input BuiltIn LocalInvocationIndex |
| |
| ; Types, variables and constants |
| %int = OpTypeInt 32 1 |
| %_ptr_Workgroup_int = OpTypePointer Workgroup %int |
| %v = OpVariable %_ptr_Workgroup_int Workgroup |
| %uint = OpTypeInt 32 0 |
| %_ptr_Input_uint = OpTypePointer Input %uint |
| %foo_local_invocation_index_Input = OpVariable %_ptr_Input_uint Input ; BuiltIn LocalInvocationIndex |
| %void = OpTypeVoid |
| %10 = OpTypeFunction %void %uint |
| %uint_0 = OpConstant %uint 0 |
| %bool = OpTypeBool |
| %int_0 = OpConstant %int 0 |
| %uint_2 = OpConstant %uint 2 |
| %uint_24840 = OpConstant %uint 24840 |
| %int_1 = OpConstant %int 1 |
| %25 = OpTypeFunction %void |
| |
| ; Function foo_inner |
| %foo_inner = OpFunction %void None %10 |
| %tint_local_index = OpFunctionParameter %uint |
| %11 = OpLabel |
| %12 = OpIEqual %bool %tint_local_index %uint_0 |
| OpSelectionMerge %15 None |
| OpBranchConditional %12 %16 %15 |
| %16 = OpLabel |
| OpStore %v %int_0 NonPrivatePointer |
| OpBranch %15 |
| %15 = OpLabel |
| OpControlBarrier %uint_2 %uint_2 %uint_24840 |
| %load = OpLoad %int %v NonPrivatePointer |
| %add = OpIAdd %int %load %int_1 |
| OpStore %v %add NonPrivatePointer |
| OpReturn |
| OpFunctionEnd |
| |
| ; Function foo |
| %foo = OpFunction %void None %25 |
| %26 = OpLabel |
| %27 = OpLoad %uint %foo_local_invocation_index_Input None |
| %28 = OpFunctionCall %void %foo_inner %27 |
| OpReturn |
| OpFunctionEnd)"); |
| } |
| |
| TEST_F(SpirvWriterTest, StorageVar_WriteOnly) { |
| auto* v = b.Var("v", ty.ptr<storage, i32, write>()); |
| v->SetBindingPoint(0, 0); |
| mod.root_block->Append(v); |
| |
| auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kCompute, |
| std::array{1u, 1u, 1u}); |
| b.Append(func->Block(), [&] { |
| b.Store(v, 42_i); |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST(R"( |
| OpDecorate %v_block Block |
| OpDecorate %1 DescriptorSet 0 |
| OpDecorate %1 Binding 0 |
| OpDecorate %1 NonReadable |
| )"); |
| EXPECT_INST(R"( |
| %9 = OpAccessChain %_ptr_StorageBuffer_int %1 %uint_0 |
| OpStore %9 %int_42 None |
| )"); |
| } |
| |
| TEST_F(SpirvWriterTest, UniformVar) { |
| auto* v = b.Var("v", ty.ptr<uniform, i32>()); |
| v->SetBindingPoint(0, 0); |
| mod.root_block->Append(v); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST(R"( |
| OpDecorate %v_block Block |
| OpDecorate %1 DescriptorSet 0 |
| OpDecorate %1 Binding 0 |
| )"); |
| EXPECT_INST(R"( |
| %v_block = OpTypeStruct %int ; Block |
| %_ptr_Uniform_v_block = OpTypePointer Uniform %v_block |
| %1 = OpVariable %_ptr_Uniform_v_block Uniform ; DescriptorSet 0, Binding 0, NonWritable |
| )"); |
| } |
| |
| TEST_F(SpirvWriterTest, UniformVar_Load) { |
| auto* v = b.Var("v", ty.ptr<uniform, i32>()); |
| v->SetBindingPoint(0, 0); |
| mod.root_block->Append(v); |
| |
| auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kCompute, |
| std::array{1u, 1u, 1u}); |
| b.Append(func->Block(), [&] { |
| auto* load = b.Load(v); |
| b.Return(func); |
| mod.SetName(load, "load"); |
| }); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST(R"( |
| %9 = OpAccessChain %_ptr_Uniform_int %1 %uint_0 |
| %load = OpLoad %int %9 None |
| )"); |
| } |
| |
| TEST_F(SpirvWriterTest, PushConstantVar) { |
| auto* v = b.Var("v", ty.ptr<push_constant, i32>()); |
| mod.root_block->Append(v); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST(R"( |
| OpDecorate %v_block Block |
| )"); |
| EXPECT_INST(R"( |
| %v_block = OpTypeStruct %int ; Block |
| %_ptr_PushConstant_v_block = OpTypePointer PushConstant %v_block |
| %1 = OpVariable %_ptr_PushConstant_v_block PushConstant |
| )"); |
| } |
| |
| TEST_F(SpirvWriterTest, PushConstantVar_Load) { |
| auto* v = b.Var("v", ty.ptr<push_constant, i32>()); |
| mod.root_block->Append(v); |
| |
| auto* func = b.Function("foo", ty.i32()); |
| b.Append(func->Block(), [&] { |
| auto* load = b.Load(v); |
| b.Return(func, load); |
| mod.SetName(load, "load"); |
| }); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST(R"( |
| %8 = OpAccessChain %_ptr_PushConstant_int %1 %uint_0 |
| %load = OpLoad %int %8 None |
| OpReturnValue %load |
| )"); |
| } |
| |
| TEST_F(SpirvWriterTest, SamplerVar) { |
| auto* v = b.Var("v", ty.ptr(core::AddressSpace::kHandle, ty.sampler(), core::Access::kRead)); |
| v->SetBindingPoint(0, 0); |
| mod.root_block->Append(v); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST(R"( |
| OpDecorate %v DescriptorSet 0 |
| OpDecorate %v Binding 0 |
| )"); |
| EXPECT_INST(R"( |
| %3 = OpTypeSampler |
| %_ptr_UniformConstant_3 = OpTypePointer UniformConstant %3 |
| %v = OpVariable %_ptr_UniformConstant_3 UniformConstant ; DescriptorSet 0, Binding 0 |
| )"); |
| } |
| |
| TEST_F(SpirvWriterTest, SamplerVar_Load) { |
| auto* v = b.Var("v", ty.ptr(core::AddressSpace::kHandle, ty.sampler(), core::Access::kRead)); |
| v->SetBindingPoint(0, 0); |
| mod.root_block->Append(v); |
| |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| auto* load = b.Load(v); |
| b.Return(func); |
| mod.SetName(load, "load"); |
| }); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST("%load = OpLoad %3 %v"); |
| } |
| |
| TEST_F(SpirvWriterTest, TextureVar) { |
| auto* v = b.Var( |
| "v", ty.ptr(core::AddressSpace::kHandle, |
| ty.Get<core::type::SampledTexture>(core::type::TextureDimension::k2d, ty.f32()), |
| core::Access::kRead)); |
| v->SetBindingPoint(0, 0); |
| mod.root_block->Append(v); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST(R"( |
| OpDecorate %v DescriptorSet 0 |
| OpDecorate %v Binding 0 |
| )"); |
| EXPECT_INST(R"( |
| %3 = OpTypeImage %float 2D 0 0 0 1 Unknown |
| %_ptr_UniformConstant_3 = OpTypePointer UniformConstant %3 |
| %v = OpVariable %_ptr_UniformConstant_3 UniformConstant ; DescriptorSet 0, Binding 0 |
| )"); |
| } |
| |
| TEST_F(SpirvWriterTest, TextureVar_Load) { |
| auto* v = b.Var( |
| "v", ty.ptr(core::AddressSpace::kHandle, |
| ty.Get<core::type::SampledTexture>(core::type::TextureDimension::k2d, ty.f32()), |
| core::Access::kRead)); |
| v->SetBindingPoint(0, 0); |
| mod.root_block->Append(v); |
| |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| auto* load = b.Load(v); |
| b.Return(func); |
| mod.SetName(load, "load"); |
| }); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST("%load = OpLoad %3 %v"); |
| } |
| |
| TEST_F(SpirvWriterTest, ReadOnlyStorageTextureVar) { |
| auto format = core::TexelFormat::kRgba8Unorm; |
| auto* v = b.Var("v", ty.ptr(core::AddressSpace::kHandle, |
| ty.Get<core::type::StorageTexture>( |
| core::type::TextureDimension::k2d, format, read, |
| core::type::StorageTexture::SubtypeFor(format, ty)), |
| core::Access::kRead)); |
| v->SetBindingPoint(0, 0); |
| mod.root_block->Append(v); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST(R"( |
| OpDecorate %v DescriptorSet 0 |
| OpDecorate %v Binding 0 |
| OpDecorate %v NonWritable |
| )"); |
| EXPECT_INST(R"( |
| %3 = OpTypeImage %float 2D 0 0 0 2 Rgba8 |
| %_ptr_UniformConstant_3 = OpTypePointer UniformConstant %3 |
| %v = OpVariable %_ptr_UniformConstant_3 UniformConstant ; DescriptorSet 0, Binding 0, NonWritable |
| )"); |
| } |
| |
| TEST_F(SpirvWriterTest, ReadWriteStorageTextureVar) { |
| auto format = core::TexelFormat::kRgba8Unorm; |
| auto* v = b.Var("v", ty.ptr(core::AddressSpace::kHandle, |
| ty.Get<core::type::StorageTexture>( |
| core::type::TextureDimension::k2d, format, read_write, |
| core::type::StorageTexture::SubtypeFor(format, ty)), |
| core::Access::kRead)); |
| v->SetBindingPoint(0, 0); |
| mod.root_block->Append(v); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST(R"( |
| OpDecorate %v DescriptorSet 0 |
| OpDecorate %v Binding 0 |
| OpDecorate %v Coherent |
| )"); |
| EXPECT_INST(R"( |
| %3 = OpTypeImage %float 2D 0 0 0 2 Rgba8 |
| %_ptr_UniformConstant_3 = OpTypePointer UniformConstant %3 |
| %v = OpVariable %_ptr_UniformConstant_3 UniformConstant ; DescriptorSet 0, Binding 0, Coherent |
| )"); |
| } |
| |
| TEST_F(SpirvWriterTest, WriteOnlyStorageTextureVar) { |
| auto format = core::TexelFormat::kRgba8Unorm; |
| auto* v = b.Var("v", ty.ptr(core::AddressSpace::kHandle, |
| ty.Get<core::type::StorageTexture>( |
| core::type::TextureDimension::k2d, format, write, |
| core::type::StorageTexture::SubtypeFor(format, ty)), |
| core::Access::kRead)); |
| v->SetBindingPoint(0, 0); |
| mod.root_block->Append(v); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST(R"( |
| OpDecorate %v DescriptorSet 0 |
| OpDecorate %v Binding 0 |
| OpDecorate %v NonReadable |
| )"); |
| EXPECT_INST(R"( |
| %3 = OpTypeImage %float 2D 0 0 0 2 Rgba8 |
| %_ptr_UniformConstant_3 = OpTypePointer UniformConstant %3 |
| %v = OpVariable %_ptr_UniformConstant_3 UniformConstant ; DescriptorSet 0, Binding 0, NonReadable |
| )"); |
| } |
| |
| } // namespace |
| } // namespace tint::spirv::writer |