[tint][ir] Serialize Sampler types

Change-Id: I67a9a88ef2a769a40b3cc29703a69eddb379dce8
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/164889
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: James Price <jrprice@google.com>
diff --git a/src/tint/lang/core/ir/binary/decode.cc b/src/tint/lang/core/ir/binary/decode.cc
index f68d13f..907d7bf 100644
--- a/src/tint/lang/core/ir/binary/decode.cc
+++ b/src/tint/lang/core/ir/binary/decode.cc
@@ -510,6 +510,8 @@
                 return CreateTypeArray(type_in.array());
             case pb::Type::KindCase::kDepthTexture:
                 return CreateTypeDepthTexture(type_in.depth_texture());
+            case pb::Type::KindCase::kSampler:
+                return CreateTypeSampler(type_in.sampler());
             default:
                 break;
         }
@@ -619,6 +621,11 @@
         return mod_out_.Types().Get<type::DepthTexture>(dimension);
     }
 
+    const type::Sampler* CreateTypeSampler(const pb::TypeSampler& sampler_in) {
+        auto kind = SamplerKind(sampler_in.kind());
+        return mod_out_.Types().Get<type::Sampler>(kind);
+    }
+
     const type::Type* Type(size_t id) { return id > 0 ? types_[id - 1] : nullptr; }
 
     ////////////////////////////////////////////////////////////////////////////
@@ -854,6 +861,20 @@
         return core::type::TextureDimension::k1d;
     }
 
+    core::type::SamplerKind SamplerKind(pb::SamplerKind in) {
+        switch (in) {
+            case pb::SamplerKind::sampler:
+                return core::type::SamplerKind::kSampler;
+            case pb::SamplerKind::comparison:
+                return core::type::SamplerKind::kComparisonSampler;
+            default:
+                break;
+        }
+
+        TINT_ICE() << "invalid SamplerKind: " << in;
+        return core::type::SamplerKind::kSampler;
+    }
+
     core::InterpolationType InterpolationType(pb::InterpolationType in) {
         switch (in) {
             case pb::InterpolationType::flat:
diff --git a/src/tint/lang/core/ir/binary/encode.cc b/src/tint/lang/core/ir/binary/encode.cc
index 413c2dd..9c213cc 100644
--- a/src/tint/lang/core/ir/binary/encode.cc
+++ b/src/tint/lang/core/ir/binary/encode.cc
@@ -70,6 +70,7 @@
 #include "src/tint/lang/core/type/i32.h"
 #include "src/tint/lang/core/type/matrix.h"
 #include "src/tint/lang/core/type/pointer.h"
+#include "src/tint/lang/core/type/sampler.h"
 #include "src/tint/lang/core/type/u32.h"
 #include "src/tint/lang/core/type/void.h"
 #include "src/tint/utils/macros/compiler.h"
@@ -337,6 +338,7 @@
                 [&](const core::type::DepthTexture* t) {
                     TypeDepthTexture(*type_out.mutable_depth_texture(), t);
                 },
+                [&](const core::type::Sampler* s) { TypeSampler(*type_out.mutable_sampler(), s); },
                 TINT_ICE_ON_NO_MATCH);
 
             mod_out_.mutable_types()->Add(std::move(type_out));
@@ -416,6 +418,10 @@
         texture_out.set_dimension(TextureDimension(texture_in->dim()));
     }
 
+    void TypeSampler(pb::TypeSampler& sampler_out, const core::type::Sampler* sampler_in) {
+        sampler_out.set_kind(SamplerKind(sampler_in->kind()));
+    }
+
     ////////////////////////////////////////////////////////////////////////////
     // Values
     ////////////////////////////////////////////////////////////////////////////
@@ -635,6 +641,18 @@
         return pb::TextureDimension::_1d;
     }
 
+    pb::SamplerKind SamplerKind(core::type::SamplerKind in) {
+        switch (in) {
+            case core::type::SamplerKind::kSampler:
+                return pb::SamplerKind::sampler;
+            case core::type::SamplerKind::kComparisonSampler:
+                return pb::SamplerKind::comparison;
+        }
+
+        TINT_ICE() << "invalid SamplerKind: " << in;
+        return pb::SamplerKind::sampler;
+    }
+
     pb::InterpolationType InterpolationType(core::InterpolationType in) {
         switch (in) {
             case core::InterpolationType::kFlat:
diff --git a/src/tint/lang/core/ir/binary/ir.proto b/src/tint/lang/core/ir/binary/ir.proto
index 4dbb993..2dbe992 100644
--- a/src/tint/lang/core/ir/binary/ir.proto
+++ b/src/tint/lang/core/ir/binary/ir.proto
@@ -51,7 +51,7 @@
         TypeStruct struct = 6;
         TypeAtomic atomic = 7;
         TypeDepthTexture depth_texture = 8;
-        // TODO: textures, samplers
+        TypeSampler sampler = 9;
     }
 }
 
@@ -109,6 +109,10 @@
     TextureDimension dimension = 1;
 }
 
+message TypeSampler {
+    SamplerKind kind = 1;
+}
+
 ////////////////////////////////////////////////////////////////////////////////
 // Values
 ////////////////////////////////////////////////////////////////////////////////
@@ -392,6 +396,11 @@
     cube_array = 5;
 }
 
+enum SamplerKind {
+    sampler = 0;
+    comparison = 1;
+}
+
 enum InterpolationType {
     flat = 0;
     linear = 1;
diff --git a/src/tint/lang/core/ir/binary/roundtrip_test.cc b/src/tint/lang/core/ir/binary/roundtrip_test.cc
index 1c45fa1..1d71786 100644
--- a/src/tint/lang/core/ir/binary/roundtrip_test.cc
+++ b/src/tint/lang/core/ir/binary/roundtrip_test.cc
@@ -254,6 +254,18 @@
     RUN_TEST();
 }
 
+TEST_F(IRBinaryRoundtripTest, sampler) {
+    auto* sampler = ty.Get<core::type::Sampler>(core::type::SamplerKind::kSampler);
+    b.Append(b.ir.root_block, [&] { b.Var(ty.ptr(handle, sampler, read)); });
+    RUN_TEST();
+}
+
+TEST_F(IRBinaryRoundtripTest, comparision_sampler) {
+    auto* sampler = ty.Get<core::type::Sampler>(core::type::SamplerKind::kComparisonSampler);
+    b.Append(b.ir.root_block, [&] { b.Var(ty.ptr(handle, sampler, read)); });
+    RUN_TEST();
+}
+
 ////////////////////////////////////////////////////////////////////////////////
 // Instructions
 ////////////////////////////////////////////////////////////////////////////////