diff --git a/docs/translations.md b/docs/translations.md index ed8431d073..ed4fc029b0 100644 --- a/docs/translations.md +++ b/docs/translations.md @@ -104,6 +104,43 @@ decorated with `NonWritable` or each member of the struct can be decorated with | tanh | GLSLstd450Tanh | tanh | tanh | | trunc | GLSLstd450Trunc | trunc | trunc | +# Types +## Sampler Types +| WGSL | SPIR-V | MSL | HLSL | +|------|--------|-----|------| +| sampler | OpTypeSampler | | SamplerState | +| sampler_comparison | OpTypeSampler | | SamplerComparisonState | + +## Texture Types +| WGSL | SPIR-V | MSL | HLSL | +|------|--------|-----|------| +| texture_1d | OpTypeImage | | Texture1D | +| texture_1d_array | OpTypeImage | | Texture1DArray | +| texture_2d | OpTypeImage | | Texture2D | +| texture_2d_array | OpTypeImage | | Texture2DArray | +| texture_3d | OpTypeImage | | Texture3D | +| texture_cube | OpTypeImage | | TextureCube | +| texture_cube_array | OpTypeImage | | TextureCubeArray | +| | | | +| texture_multisampled_2d<type> | OpTypeImage | | Texture2D | +| | | | +| texture_depth_2d | OpTypeImage | | Texture2D | +| texture_depth_2d_array | OpTypeImage | | Texture2DArray | +| texture_depth_cube | OpTypeImage | | TextureCube | +| texture_depth_cube_array | OpTypeImage | | TextureCubeArray | +| | | | +| texture_storage_ro_1d<image_storage_type> | OpTypeImage | | RWTexture1D | +| texture_storage_ro_1d_array<image_storage_type> | OpTypeImage | | RWTexture1DArray | +| texture_storage_ro_2d<image_storage_type> | OpTypeImage | | RWTexture2D | +| texture_storage_ro_2d_array<image_storage_type> | OpTypeImage | | RWTexture2DArray | +| texture_storage_ro_3d<image_storage_type> | OpTypeImage | | RWTexture3D | +| | | | +| texture_storage_wo_1d<image_storage_type> | OpTypeImage | | RWTexture1D | +| texture_storage_wo_1d_array<image_storage_type> | OpTypeImage | | RWTexture1DArray | +| texture_storage_wo_2d<image_storage_type> | OpTypeImage | | RWTexture2D | +| texture_storage_wo_2d_array<image_storage_type> | OpTypeImage | | RWTexture2DArray | +| texture_storage_wo_3d<image_storage_type> | OpTypeImage | | RWTexture3D| + # Short-circuting ## HLSL TODO(dsinclair): Nested if's diff --git a/src/writer/hlsl/generator_impl.cc b/src/writer/hlsl/generator_impl.cc index 763bb66b68..1ae094c216 100644 --- a/src/writer/hlsl/generator_impl.cc +++ b/src/writer/hlsl/generator_impl.cc @@ -44,7 +44,9 @@ #include "src/ast/type/f32_type.h" #include "src/ast/type/i32_type.h" #include "src/ast/type/matrix_type.h" +#include "src/ast/type/sampler_type.h" #include "src/ast/type/struct_type.h" +#include "src/ast/type/texture_type.h" #include "src/ast/type/vector_type.h" #include "src/ast/uint_literal.h" #include "src/ast/unary_op_expression.h" @@ -1984,8 +1986,49 @@ bool GeneratorImpl::EmitType(std::ostream& out, // https://bugs.chromium.org/p/tint/issues/detail?id=183 error_ = "pointers not supported in HLSL"; return false; + } else if (type->IsSampler()) { + auto* sampler = type->AsSampler(); + out << "Sampler"; + if (sampler->IsComparison()) { + out << "Comparison"; + } + out << "State"; } else if (type->IsStruct()) { out << type->AsStruct()->name(); + } else if (type->IsTexture()) { + auto* tex = type->AsTexture(); + if (tex->IsStorage()) { + out << "RW"; + } + out << "Texture"; + + switch (tex->dim()) { + case ast::type::TextureDimension::k1d: + out << "1D"; + break; + case ast::type::TextureDimension::k1dArray: + out << "1DArray"; + break; + case ast::type::TextureDimension::k2d: + out << "2D"; + break; + case ast::type::TextureDimension::k2dArray: + out << "2DArray"; + break; + case ast::type::TextureDimension::k3d: + out << "3D"; + break; + case ast::type::TextureDimension::kCube: + out << "Cube"; + break; + case ast::type::TextureDimension::kCubeArray: + out << "CubeArray"; + break; + default: + error_ = "Invalid texture dimensions"; + return false; + } + } else if (type->IsU32()) { out << "uint"; } else if (type->IsVector()) { diff --git a/src/writer/hlsl/generator_impl_type_test.cc b/src/writer/hlsl/generator_impl_type_test.cc index f91756e5b1..8cac13093d 100644 --- a/src/writer/hlsl/generator_impl_type_test.cc +++ b/src/writer/hlsl/generator_impl_type_test.cc @@ -21,10 +21,15 @@ #include "src/ast/struct_member_offset_decoration.h" #include "src/ast/type/array_type.h" #include "src/ast/type/bool_type.h" +#include "src/ast/type/depth_texture_type.h" #include "src/ast/type/f32_type.h" #include "src/ast/type/i32_type.h" #include "src/ast/type/matrix_type.h" +#include "src/ast/type/multisampled_texture_type.h" #include "src/ast/type/pointer_type.h" +#include "src/ast/type/sampled_texture_type.h" +#include "src/ast/type/sampler_type.h" +#include "src/ast/type/storage_texture_type.h" #include "src/ast/type/struct_type.h" #include "src/ast/type/u32_type.h" #include "src/ast/type/vector_type.h" @@ -329,6 +334,139 @@ TEST_F(HlslGeneratorImplTest_Type, EmitType_Void) { EXPECT_EQ(result(), "void"); } +TEST_F(HlslGeneratorImplTest_Type, EmitSampler) { + ast::type::SamplerType sampler(ast::type::SamplerKind::kSampler); + + ASSERT_TRUE(gen().EmitType(out(), &sampler, "")) << gen().error(); + EXPECT_EQ(result(), "SamplerState"); +} + +TEST_F(HlslGeneratorImplTest_Type, EmitSamplerComparison) { + ast::type::SamplerType sampler(ast::type::SamplerKind::kComparisonSampler); + + ASSERT_TRUE(gen().EmitType(out(), &sampler, "")) << gen().error(); + EXPECT_EQ(result(), "SamplerComparisonState"); +} + +struct HlslDepthTextureData { + ast::type::TextureDimension dim; + std::string result; +}; +inline std::ostream& operator<<(std::ostream& out, HlslDepthTextureData data) { + out << data.dim; + return out; +} +using HlslDepthtexturesTest = + TestHelperBase>; +TEST_P(HlslDepthtexturesTest, Emit) { + auto params = GetParam(); + + ast::type::DepthTextureType s(params.dim); + + ASSERT_TRUE(gen().EmitType(out(), &s, "")) << gen().error(); + EXPECT_EQ(result(), params.result); +} +INSTANTIATE_TEST_SUITE_P( + HlslGeneratorImplTest_Type, + HlslDepthtexturesTest, + testing::Values( + HlslDepthTextureData{ast::type::TextureDimension::k2d, "Texture2D"}, + HlslDepthTextureData{ast::type::TextureDimension::k2dArray, + "Texture2DArray"}, + HlslDepthTextureData{ast::type::TextureDimension::kCube, "TextureCube"}, + HlslDepthTextureData{ast::type::TextureDimension::kCubeArray, + "TextureCubeArray"})); + +struct HlslTextureData { + ast::type::TextureDimension dim; + std::string result; +}; +inline std::ostream& operator<<(std::ostream& out, HlslTextureData data) { + out << data.dim; + return out; +} +using HlslSampledtexturesTest = + TestHelperBase>; +TEST_P(HlslSampledtexturesTest, Emit) { + auto params = GetParam(); + + ast::type::F32Type f32; + ast::type::SampledTextureType s(params.dim, &f32); + + ASSERT_TRUE(gen().EmitType(out(), &s, "")) << gen().error(); + EXPECT_EQ(result(), params.result); +} +INSTANTIATE_TEST_SUITE_P( + HlslGeneratorImplTest_Type, + HlslSampledtexturesTest, + testing::Values( + HlslTextureData{ast::type::TextureDimension::k1d, "Texture1D"}, + HlslTextureData{ast::type::TextureDimension::k1dArray, + "Texture1DArray"}, + HlslTextureData{ast::type::TextureDimension::k2d, "Texture2D"}, + HlslTextureData{ast::type::TextureDimension::k2dArray, + "Texture2DArray"}, + HlslTextureData{ast::type::TextureDimension::k3d, "Texture3D"}, + HlslTextureData{ast::type::TextureDimension::kCube, "TextureCube"}, + HlslTextureData{ast::type::TextureDimension::kCubeArray, + "TextureCubeArray"})); + +TEST_F(HlslGeneratorImplTest_Type, EmitMultisampledTexture) { + ast::type::F32Type f32; + ast::type::MultisampledTextureType s(ast::type::TextureDimension::k2d, &f32); + + ASSERT_TRUE(gen().EmitType(out(), &s, "")) << gen().error(); + EXPECT_EQ(result(), "Texture2D"); +} + +struct HlslStorageTextureData { + ast::type::TextureDimension dim; + bool ro; + std::string result; +}; +inline std::ostream& operator<<(std::ostream& out, + HlslStorageTextureData data) { + out << data.dim << (data.ro ? "ReadOnly" : "WriteOnly"); + return out; +} +using HlslStoragetexturesTest = + TestHelperBase>; +TEST_P(HlslStoragetexturesTest, Emit) { + auto params = GetParam(); + + ast::type::StorageTextureType s(params.dim, + params.ro ? ast::AccessControl::kReadOnly + : ast::AccessControl::kWriteOnly, + ast::type::ImageFormat::kR16Float); + + ASSERT_TRUE(gen().EmitType(out(), &s, "")) << gen().error(); + EXPECT_EQ(result(), params.result); +} +INSTANTIATE_TEST_SUITE_P( + HlslGeneratorImplTest_Type, + HlslStoragetexturesTest, + testing::Values( + HlslStorageTextureData{ast::type::TextureDimension::k1d, true, + "RWTexture1D"}, + HlslStorageTextureData{ast::type::TextureDimension::k1dArray, true, + "RWTexture1DArray"}, + HlslStorageTextureData{ast::type::TextureDimension::k2d, true, + "RWTexture2D"}, + HlslStorageTextureData{ast::type::TextureDimension::k2dArray, true, + "RWTexture2DArray"}, + HlslStorageTextureData{ast::type::TextureDimension::k3d, true, + "RWTexture3D"}, + HlslStorageTextureData{ast::type::TextureDimension::k1d, false, + "RWTexture1D"}, + HlslStorageTextureData{ast::type::TextureDimension::k1dArray, false, + "RWTexture1DArray"}, + HlslStorageTextureData{ast::type::TextureDimension::k2d, false, + "RWTexture2D"}, + HlslStorageTextureData{ast::type::TextureDimension::k2dArray, false, + "RWTexture2DArray"}, + HlslStorageTextureData{ast::type::TextureDimension::k3d, false, + "RWTexture3D"})); + } // namespace } // namespace hlsl } // namespace writer