From 2e20e7674cc01520a6fecf81ecf0c2525a3c44d0 Mon Sep 17 00:00:00 2001
From: John Stiles <johnstiles@google.com>
Date: Mon, 25 Oct 2021 14:07:10 -0400
Subject: [PATCH] Use virtual method for slotCount.

Using a switch is overkill now that Types are implemented in subclasses.

Change-Id: If7874dfa9a5b02f168ac7c6c3f69a2bd6f9c2a80
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/463156
Commit-Queue: John Stiles <johnstiles@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
Auto-Submit: John Stiles <johnstiles@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
---
 src/sksl/ir/SkSLType.cpp | 29 +++++++++++++++++++++++++++++
 src/sksl/ir/SkSLType.h   | 37 ++-----------------------------------
 2 files changed, 31 insertions(+), 35 deletions(-)

diff --git a/src/sksl/ir/SkSLType.cpp b/src/sksl/ir/SkSLType.cpp
index aa2428c097..56c34d066e 100644
--- a/src/sksl/ir/SkSLType.cpp
+++ b/src/sksl/ir/SkSLType.cpp
@@ -63,6 +63,11 @@ public:
         return fComponentType.isAllowedInES2();
     }
 
+    size_t slotCount() const override {
+        SkASSERT(fCount > 0);
+        return fCount * fComponentType.slotCount();
+    }
+
 private:
     using INHERITED = Type;
 
@@ -129,6 +134,10 @@ public:
         return true;
     }
 
+    size_t slotCount() const override {
+        return 1;
+    }
+
 private:
     using INHERITED = Type;
 
@@ -176,6 +185,10 @@ public:
         return fNumberKind != NumberKind::kUnsigned;
     }
 
+    size_t slotCount() const override {
+        return 1;
+    }
+
 private:
     using INHERITED = Type;
 
@@ -222,6 +235,10 @@ public:
         return fColumns == fRows;
     }
 
+    size_t slotCount() const override {
+        return fColumns * fRows;
+    }
+
 private:
     using INHERITED = Type;
 
@@ -339,6 +356,14 @@ public:
         });
     }
 
+    size_t slotCount() const override {
+        size_t slots = 0;
+        for (const Field& field : fFields) {
+            slots += field.fType->slotCount();
+        }
+        return slots;
+    }
+
 private:
     using INHERITED = Type;
 
@@ -381,6 +406,10 @@ public:
         return fComponentType.isAllowedInES2();
     }
 
+    size_t slotCount() const override {
+        return fColumns;
+    }
+
 private:
     using INHERITED = Type;
 
diff --git a/src/sksl/ir/SkSLType.h b/src/sksl/ir/SkSLType.h
index f8fee9b958..db75066332 100644
--- a/src/sksl/ir/SkSLType.h
+++ b/src/sksl/ir/SkSLType.h
@@ -387,41 +387,8 @@ public:
     /**
      * Returns the number of scalars needed to hold this type.
      */
-    size_t slotCount() const {
-        switch (this->typeKind()) {
-            case Type::TypeKind::kBlender:
-            case Type::TypeKind::kColorFilter:
-            case Type::TypeKind::kGeneric:
-            case Type::TypeKind::kOther:
-            case Type::TypeKind::kSampler:
-            case Type::TypeKind::kSeparateSampler:
-            case Type::TypeKind::kShader:
-            case Type::TypeKind::kTexture:
-            case Type::TypeKind::kVoid:
-                return 0;
-
-            case Type::TypeKind::kLiteral:
-            case Type::TypeKind::kScalar:
-                return 1;
-
-            case Type::TypeKind::kVector:
-                return this->columns();
-
-            case Type::TypeKind::kMatrix:
-                return this->columns() * this->rows();
-
-            case Type::TypeKind::kStruct: {
-                size_t slots = 0;
-                for (const Field& field : this->fields()) {
-                    slots += field.fType->slotCount();
-                }
-                return slots;
-            }
-            case Type::TypeKind::kArray:
-                SkASSERT(this->columns() > 0);
-                return this->columns() * this->componentType().slotCount();
-        }
-        SkUNREACHABLE;
+    virtual size_t slotCount() const {
+        return 0;
     }
 
     virtual const std::vector<Field>& fields() const {