Revert of added support for push_constant layout (patchset #7 id:140001 of https...
authoregdaniel <egdaniel@google.com>
Wed, 16 Nov 2016 15:29:51 +0000 (07:29 -0800)
committerCommit bot <commit-bot@chromium.org>
Wed, 16 Nov 2016 15:29:52 +0000 (07:29 -0800)
Reason for revert:
hitting assert in vulkan

Original issue's description:
> added support for push_constant layout
>
> BUG=skia:
> GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2187433003
>
> Committed: https://skia.googlesource.com/skia/+/fa5f65ac61fd525029aa9dab161ffe4896c10f6d

TBR=ethannicholas@google.com
# Skipping CQ checks because original CL landed less than 1 days ago.
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true
BUG=skia:

Review-Url: https://codereview.chromium.org/2509673002

13 files changed:
gn/tests.gni
src/sksl/SkSLMemoryLayout.h [deleted file]
src/sksl/SkSLParser.cpp
src/sksl/SkSLSPIRVCodeGenerator.cpp
src/sksl/SkSLSPIRVCodeGenerator.h
src/sksl/SkSLUtil.h
src/sksl/ast/SkSLASTLayout.h
src/sksl/ir/SkSLLayout.h
src/sksl/ir/SkSLModifiers.h
src/sksl/ir/SkSLType.h
tests/SkSLErrorTest.cpp
tests/SkSLGLSLTest.cpp
tests/SkSLMemoryLayoutTest.cpp [deleted file]

index 647160f..d828297 100644 (file)
@@ -203,7 +203,6 @@ tests_sources = [
   "$_tests/SkSharedMutexTest.cpp",
   "$_tests/SkSLErrorTest.cpp",
   "$_tests/SkSLGLSLTest.cpp",
-  "$_tests/SkSLMemoryLayoutTest.cpp",
   "$_tests/SmallAllocatorTest.cpp",
   "$_tests/SortTest.cpp",
   "$_tests/SpecialImageTest.cpp",
diff --git a/src/sksl/SkSLMemoryLayout.h b/src/sksl/SkSLMemoryLayout.h
deleted file mode 100644 (file)
index 95a292f..0000000
+++ /dev/null
@@ -1,128 +0,0 @@
-/*
- * Copyright 2016 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-#ifndef SKIASL_MEMORYLAYOUT
-#define SKIASL_MEMORYLAYOUT
-
-#include "ir/SkSLType.h"
-
-namespace SkSL {
-
-class MemoryLayout {
-public:
-    enum Standard {
-        k140_Standard,
-        k430_Standard
-    };
-
-    MemoryLayout(Standard std) 
-    : fStd(std) {}
-
-    static size_t vector_alignment(size_t componentSize, int columns) {
-        return componentSize * (columns + columns % 2);
-    }
-
-    /** 
-     * Rounds up to the nearest multiple of 16 if in std140, otherwise returns the parameter
-     * unchanged (std140 requires various things to be rounded up to the nearest multiple of 16,
-     * std430 does not).
-     */
-    size_t roundUpIfNeeded(size_t raw) const {
-        switch (fStd) {
-            case k140_Standard: return (raw + 15) & ~15;
-            case k430_Standard: return raw;
-        }
-        ABORT("unreachable");
-    }
-
-    /**
-     * Returns a type's required alignment when used as a standalone variable.
-     */
-    size_t alignment(const Type& type) const {
-        // See OpenGL Spec 7.6.2.2 Standard Uniform Block Layout
-        switch (type.kind()) {
-            case Type::kScalar_Kind:
-                return this->size(type);
-            case Type::kVector_Kind:
-                return vector_alignment(this->size(type.componentType()), type.columns());
-            case Type::kMatrix_Kind:
-                return this->roundUpIfNeeded(vector_alignment(this->size(type.componentType()), 
-                                                              type.rows()));
-            case Type::kArray_Kind:
-                return this->roundUpIfNeeded(this->alignment(type.componentType()));
-            case Type::kStruct_Kind: {
-                size_t result = 0;
-                for (const auto& f : type.fields()) {
-                    size_t alignment = this->alignment(*f.fType);
-                    if (alignment > result) {
-                        result = alignment;
-                    }
-                }
-                return this->roundUpIfNeeded(result);
-            }
-            default:
-                ABORT(("cannot determine size of type " + type.name()).c_str());
-        }
-    }
-
-    /**
-     * For matrices and arrays, returns the number of bytes from the start of one entry (row, in
-     * the case of matrices) to the start of the next.
-     */
-    size_t stride(const Type& type) const {
-        switch (type.kind()) {
-            case Type::kMatrix_Kind: // fall through
-            case Type::kArray_Kind:
-                return this->alignment(type);
-            default:
-                ABORT("type does not have a stride");
-        }
-    }
-
-    /**
-     * Returns the size of a type in bytes.
-     */
-    size_t size(const Type& type) const {
-        switch (type.kind()) {
-            case Type::kScalar_Kind:
-                if (type.name() == "bool") {
-                    return 1;
-                }
-                // FIXME need to take precision into account, once we figure out how we want to
-                // handle it...
-                return 4;
-            case Type::kVector_Kind:
-                return type.columns() * this->size(type.componentType());
-            case Type::kMatrix_Kind: // fall through
-            case Type::kArray_Kind:
-                return type.columns() * this->stride(type);
-            case Type::kStruct_Kind: {
-                size_t total = 0;
-                for (const auto& f : type.fields()) {
-                    size_t alignment = this->alignment(*f.fType);
-                    if (total % alignment != 0) {
-                        total += alignment - total % alignment;
-                    }
-                    ASSERT(total % alignment == 0);
-                    total += this->size(*f.fType);
-                }
-                size_t alignment = this->alignment(type);
-                ASSERT(!type.fields().size() || 
-                       (0 == alignment % this->alignment(*type.fields()[0].fType)));
-                return (total + alignment - 1) & ~(alignment - 1);
-            }
-            default:
-                ABORT(("cannot determine size of type " + type.name()).c_str());
-        }
-    }
-
-    const Standard fStd;
-};
-
-} // namespace
-
-#endif
index 154989f..324bd41 100644 (file)
@@ -526,7 +526,8 @@ int Parser::layoutInt() {
     return -1;
 }
 
-/* LAYOUT LPAREN IDENTIFIER (EQ INT_LITERAL)? (COMMA IDENTIFIER (EQ INT_LITERAL)?)* RPAREN */
+/* LAYOUT LPAREN IDENTIFIER EQ INT_LITERAL (COMMA IDENTIFIER EQ INT_LITERAL)* 
+   RPAREN */
 ASTLayout Parser::layout() {
     int location = -1;
     int binding = -1;
@@ -536,12 +537,11 @@ ASTLayout Parser::layout() {
     bool originUpperLeft = false;
     bool overrideCoverage = false;
     bool blendSupportAllEquations = false;
-    bool pushConstant = false;
     if (this->peek().fKind == Token::LAYOUT) {
         this->nextToken();
         if (!this->expect(Token::LPAREN, "'('")) {
             return ASTLayout(location, binding, index, set, builtin, originUpperLeft,
-                             overrideCoverage, blendSupportAllEquations, pushConstant);
+                             overrideCoverage, blendSupportAllEquations);
         }
         for (;;) {
             Token t = this->nextToken();
@@ -561,8 +561,6 @@ ASTLayout Parser::layout() {
                 overrideCoverage = true;
             } else if (t.fText == "blend_support_all_equations") {
                 blendSupportAllEquations = true;
-            } else if (t.fText == "push_constant") {
-                pushConstant = true;
             } else {
                 this->error(t.fPosition, ("'" + t.fText + 
                                           "' is not a valid layout qualifier").c_str());
@@ -577,7 +575,7 @@ ASTLayout Parser::layout() {
         }
     }
     return ASTLayout(location, binding, index, set, builtin, originUpperLeft, overrideCoverage,
-                     blendSupportAllEquations, pushConstant);
+                     blendSupportAllEquations);
 }
 
 /* layout? (UNIFORM | CONST | IN | OUT | INOUT | LOWP | MEDIUMP | HIGHP | FLAT | NOPERSPECTIVE)* */
index b899e4f..a491968 100644 (file)
@@ -968,13 +968,13 @@ SpvId SPIRVCodeGenerator::nextId() {
     return fIdCount++;
 }
 
-void SPIRVCodeGenerator::writeStruct(const Type& type, const MemoryLayout& layout, SpvId resultId) {
+void SPIRVCodeGenerator::writeStruct(const Type& type, SpvId resultId) {
     this->writeInstruction(SpvOpName, resultId, type.name().c_str(), fNameBuffer);
     // go ahead and write all of the field types, so we don't inadvertently write them while we're
     // in the middle of writing the struct instruction
     std::vector<SpvId> types;
     for (const auto& f : type.fields()) {
-        types.push_back(this->getType(*f.fType, layout));
+        types.push_back(this->getType(*f.fType));
     }
     this->writeOpCode(SpvOpTypeStruct, 2 + (int32_t) types.size(), fConstantBuffer);
     this->writeWord(resultId, fConstantBuffer);
@@ -983,8 +983,8 @@ void SPIRVCodeGenerator::writeStruct(const Type& type, const MemoryLayout& layou
     }
     size_t offset = 0;
     for (int32_t i = 0; i < (int32_t) type.fields().size(); i++) {
-        size_t size = layout.size(*type.fields()[i].fType);
-        size_t alignment = layout.alignment(*type.fields()[i].fType);
+        size_t size = type.fields()[i].fType->size();
+        size_t alignment = type.fields()[i].fType->alignment();
         size_t mod = offset % alignment;
         if (mod != 0) {
             offset += alignment - mod;
@@ -1000,8 +1000,7 @@ void SPIRVCodeGenerator::writeStruct(const Type& type, const MemoryLayout& layou
             this->writeInstruction(SpvOpMemberDecorate, resultId, i, SpvDecorationColMajor, 
                                    fDecorationBuffer);
             this->writeInstruction(SpvOpMemberDecorate, resultId, i, SpvDecorationMatrixStride, 
-                                   (SpvId) layout.stride(*type.fields()[i].fType), 
-                                   fDecorationBuffer);
+                                   (SpvId) type.fields()[i].fType->stride(), fDecorationBuffer);
         }
         offset += size;
         Type::Kind kind = type.fields()[i].fType->kind();
@@ -1013,12 +1012,7 @@ void SPIRVCodeGenerator::writeStruct(const Type& type, const MemoryLayout& layou
 }
 
 SpvId SPIRVCodeGenerator::getType(const Type& type) {
-    return this->getType(type, fDefaultLayout);
-}
-
-SpvId SPIRVCodeGenerator::getType(const Type& type, const MemoryLayout& layout) {
-    std::string key = type.name() + to_string((int) layout.fStd);
-    auto entry = fTypeMap.find(key);
+    auto entry = fTypeMap.find(type.name());
     if (entry == fTypeMap.end()) {
         SpvId result = this->nextId();
         switch (type.kind()) {
@@ -1039,38 +1033,35 @@ SpvId SPIRVCodeGenerator::getType(const Type& type, const MemoryLayout& layout)
                 break;
             case Type::kVector_Kind:
                 this->writeInstruction(SpvOpTypeVector, result, 
-                                       this->getType(type.componentType(), layout),
+                                       this->getType(type.componentType()),
                                        type.columns(), fConstantBuffer);
                 break;
             case Type::kMatrix_Kind:
                 this->writeInstruction(SpvOpTypeMatrix, result, 
-                                       this->getType(index_type(fContext, type), layout), 
+                                       this->getType(index_type(fContext, type)), 
                                        type.columns(), fConstantBuffer);
                 break;
             case Type::kStruct_Kind:
-                this->writeStruct(type, layout, result);
+                this->writeStruct(type, result);
                 break;
             case Type::kArray_Kind: {
                 if (type.columns() > 0) {
                     IntLiteral count(fContext, Position(), type.columns());
                     this->writeInstruction(SpvOpTypeArray, result, 
-                                           this->getType(type.componentType(), layout), 
+                                           this->getType(type.componentType()), 
                                            this->writeIntLiteral(count), fConstantBuffer);
                     this->writeInstruction(SpvOpDecorate, result, SpvDecorationArrayStride, 
-                                           (int32_t) layout.stride(type), 
-                                           fDecorationBuffer);
+                                           (int32_t) type.stride(), fDecorationBuffer);
                 } else {
                     ABORT("runtime-sized arrays are not yet supported");
                     this->writeInstruction(SpvOpTypeRuntimeArray, result, 
-                                           this->getType(type.componentType(), layout), 
-                                           fConstantBuffer);
+                                           this->getType(type.componentType()), fConstantBuffer);
                 }
                 break;
             }
             case Type::kSampler_Kind: {
                 SpvId image = this->nextId();
-                this->writeInstruction(SpvOpTypeImage, image, 
-                                       this->getType(*fContext.fFloat_Type, layout), 
+                this->writeInstruction(SpvOpTypeImage, image, this->getType(*fContext.fFloat_Type), 
                                        type.dimensions(), type.isDepth(), type.isArrayed(),
                                        type.isMultisampled(), type.isSampled(), 
                                        SpvImageFormatUnknown, fConstantBuffer);
@@ -1084,7 +1075,7 @@ SpvId SPIRVCodeGenerator::getType(const Type& type, const MemoryLayout& layout)
                     ABORT("invalid type: %s", type.description().c_str());
                 }
         }
-        fTypeMap[key] = result;
+        fTypeMap[type.name()] = result;
         return result;
     }
     return entry->second;
@@ -1147,13 +1138,9 @@ SpvId SPIRVCodeGenerator::getFunctionType(const FunctionDeclaration& function) {
     return entry->second;
 }
 
-SpvId SPIRVCodeGenerator::getPointerType(const Type& type, SpvStorageClass_ storageClass) {
-    return this->getPointerType(type, fDefaultLayout, storageClass);
-}
-
-SpvId SPIRVCodeGenerator::getPointerType(const Type& type, const MemoryLayout& layout,
+SpvId SPIRVCodeGenerator::getPointerType(const Type& type, 
                                          SpvStorageClass_ storageClass) {
-    std::string key = type.description() + "*" + to_string(layout.fStd) + to_string(storageClass);
+    std::string key = type.description() + "*" + to_string(storageClass);
     auto entry = fTypeMap.find(key);
     if (entry == fTypeMap.end()) {
         SpvId result = this->nextId();
@@ -1564,15 +1551,10 @@ SpvId SPIRVCodeGenerator::writeConstructor(const Constructor& c, std::ostream& o
 
 SpvStorageClass_ get_storage_class(const Modifiers& modifiers) {
     if (modifiers.fFlags & Modifiers::kIn_Flag) {
-        ASSERT(!modifiers.fLayout.fPushConstant);
         return SpvStorageClassInput;
     } else if (modifiers.fFlags & Modifiers::kOut_Flag) {
-        ASSERT(!modifiers.fLayout.fPushConstant);
         return SpvStorageClassOutput;
     } else if (modifiers.fFlags & Modifiers::kUniform_Flag) {
-        if (modifiers.fLayout.fPushConstant) {
-            return SpvStorageClassPushConstant;
-        }
         return SpvStorageClassUniform;
     } else {
         return SpvStorageClassFunction;
@@ -2376,10 +2358,7 @@ void SPIRVCodeGenerator::writeLayout(const Layout& layout, SpvId target, int mem
 }
 
 SpvId SPIRVCodeGenerator::writeInterfaceBlock(const InterfaceBlock& intf) {
-    MemoryLayout layout = intf.fVariable.fModifiers.fLayout.fPushConstant ?
-                          MemoryLayout(MemoryLayout::k430_Standard) :
-                          fDefaultLayout;
-    SpvId type = this->getType(intf.fVariable.fType, layout);
+    SpvId type = this->getType(intf.fVariable.fType);
     SpvId result = this->nextId();
     this->writeInstruction(SpvOpDecorate, type, SpvDecorationBlock, fDecorationBuffer);
     SpvStorageClass_ storageClass = get_storage_class(intf.fVariable.fModifiers);
@@ -2432,11 +2411,10 @@ void SPIRVCodeGenerator::writeGlobalVars(Program::Kind kind, const VarDeclaratio
         this->writeInstruction(SpvOpVariable, type, id, storageClass, fConstantBuffer);
         this->writeInstruction(SpvOpName, id, var->fName.c_str(), fNameBuffer);
         if (var->fType.kind() == Type::kMatrix_Kind) {
-            this->writeInstruction(SpvOpMemberDecorate, id, (SpvId) i, SpvDecorationColMajor,
-                                   fDecorationBuffer);
-            this->writeInstruction(SpvOpMemberDecorate, id, (SpvId) i, SpvDecorationMatrixStride,
-                                   (SpvId) fDefaultLayout.stride(var->fType),
+            this->writeInstruction(SpvOpMemberDecorate, id, (SpvId) i, SpvDecorationColMajor, 
                                    fDecorationBuffer);
+            this->writeInstruction(SpvOpMemberDecorate, id, (SpvId) i, SpvDecorationMatrixStride, 
+                                   (SpvId) var->fType.stride(), fDecorationBuffer);
         }
         if (varDecl.fValue) {
             ASSERT(!fCurrentBlock);
index 6459e5b..e6fc28e 100644 (file)
@@ -14,7 +14,6 @@
 #include <unordered_map>
 
 #include "SkSLCodeGenerator.h"
-#include "SkSLMemoryLayout.h"
 #include "ir/SkSLBinaryExpression.h"
 #include "ir/SkSLBoolLiteral.h"
 #include "ir/SkSLConstructor.h"
@@ -64,7 +63,6 @@ public:
 
     SPIRVCodeGenerator(const Context* context)
     : fContext(*context)
-    , fDefaultLayout(MemoryLayout::k140_Standard)
     , fCapabilities(1 << SpvCapabilityShader)
     , fIdCount(1)
     , fBoolTrue(0)
@@ -95,22 +93,17 @@ private:
 
     SpvId getType(const Type& type);
 
-    SpvId getType(const Type& type, const MemoryLayout& layout);
-
     SpvId getFunctionType(const FunctionDeclaration& function);
 
     SpvId getPointerType(const Type& type, SpvStorageClass_ storageClass);
 
-    SpvId getPointerType(const Type& type, const MemoryLayout& layout, 
-                         SpvStorageClass_ storageClass);
-
     std::vector<SpvId> getAccessChain(const Expression& expr, std::ostream& out);
 
     void writeLayout(const Layout& layout, SpvId target);
 
     void writeLayout(const Layout& layout, SpvId target, int member);
 
-    void writeStruct(const Type& type, const MemoryLayout& layout, SpvId resultId);
+    void writeStruct(const Type& type, SpvId resultId);
 
     void writeProgramElement(const ProgramElement& pe, std::ostream& out);
 
@@ -236,7 +229,6 @@ private:
                           std::ostream& out);
 
     const Context& fContext;
-    const MemoryLayout fDefaultLayout;
 
     uint64_t fCapabilities;
     SpvId fIdCount;
index c648152..ede2183 100644 (file)
@@ -100,8 +100,13 @@ NORETURN void sksl_abort();
 
 } // namespace
 
-#define ASSERT(x) SkASSERT(x)
-#define ASSERT_RESULT(x) SkAssertResult(x);
+#ifdef DEBUG
+#define ASSERT(x) assert(x)
+#define ASSERT_RESULT(x) ASSERT(x);
+#else
+#define ASSERT(x)
+#define ASSERT_RESULT(x) x
+#endif
 
 #ifdef SKIA
 #define ABORT(...) { SkDebugf(__VA_ARGS__); sksl_abort(); }
index 2aec31e..515eb2b 100644 (file)
@@ -19,9 +19,9 @@ namespace SkSL {
  * layout (location = 0) int x;
  */
 struct ASTLayout : public ASTNode {
-    // For int parameters, a -1 means no value
+    // For all parameters, a -1 means no value
     ASTLayout(int location, int binding, int index, int set, int builtin, bool originUpperLeft,
-              bool overrideCoverage, bool blendSupportAllEquations, bool pushConstant)
+              bool overrideCoverage, bool blendSupportAllEquations)
     : fLocation(location)
     , fBinding(binding)
     , fIndex(index)
@@ -29,8 +29,7 @@ struct ASTLayout : public ASTNode {
     , fBuiltin(builtin)
     , fOriginUpperLeft(originUpperLeft)
     , fOverrideCoverage(overrideCoverage)
-    , fBlendSupportAllEquations(blendSupportAllEquations)
-    , fPushConstant(pushConstant) {}
+    , fBlendSupportAllEquations(blendSupportAllEquations) {}
 
     std::string description() const {
         std::string result;
@@ -67,10 +66,6 @@ struct ASTLayout : public ASTNode {
             result += separator + "blend_support_all_equations";
             separator = ", ";
         }
-        if (fPushConstant) {
-            result += separator + "push_constant";
-            separator = ", ";
-        }
         if (result.length() > 0) {
             result = "layout (" + result + ")";
         }
@@ -85,7 +80,6 @@ struct ASTLayout : public ASTNode {
     const bool fOriginUpperLeft;
     const bool fOverrideCoverage;
     const bool fBlendSupportAllEquations;
-    const bool fPushConstant;
 };
 
 } // namespace
index dfa3967..24087d0 100644 (file)
@@ -24,11 +24,10 @@ struct Layout {
     , fBuiltin(layout.fBuiltin)
     , fOriginUpperLeft(layout.fOriginUpperLeft)
     , fOverrideCoverage(layout.fOverrideCoverage)
-    , fBlendSupportAllEquations(layout.fBlendSupportAllEquations)
-    , fPushConstant(layout.fPushConstant) {}
+    , fBlendSupportAllEquations(layout.fBlendSupportAllEquations) {}
 
     Layout(int location, int binding, int index, int set, int builtin, bool originUpperLeft,
-           bool overrideCoverage, bool blendSupportAllEquations, bool pushconstant)
+           bool overrideCoverage, bool blendSupportAllEquations)
     : fLocation(location)
     , fBinding(binding)
     , fIndex(index)
@@ -36,19 +35,7 @@ struct Layout {
     , fBuiltin(builtin)
     , fOriginUpperLeft(originUpperLeft)
     , fOverrideCoverage(overrideCoverage)
-    , fBlendSupportAllEquations(blendSupportAllEquations)
-    , fPushConstant(pushconstant) {}
-
-    Layout() 
-    : fLocation(-1)
-    , fBinding(-1)
-    , fIndex(-1)
-    , fSet(-1)
-    , fBuiltin(-1)
-    , fOriginUpperLeft(false)
-    , fOverrideCoverage(false)
-    , fBlendSupportAllEquations(false)
-    , fPushConstant(false) {}
+    , fBlendSupportAllEquations(blendSupportAllEquations) {}
 
     std::string description() const {
         std::string result;
@@ -85,10 +72,6 @@ struct Layout {
             result += separator + "blend_support_all_equations";
             separator = ", ";
         }
-        if (fPushConstant) {
-            result += separator + "push_constant";
-            separator = ", ";
-        }
         if (result.length() > 0) {
             result = "layout (" + result + ")";
         }
@@ -110,16 +93,16 @@ struct Layout {
         return !(*this == other);
     }
 
+    // everything but builtin is in the GLSL spec; builtin comes from SPIR-V and identifies which
+    // particular builtin value this object represents.
     int fLocation;
     int fBinding;
     int fIndex;
     int fSet;
     int fBuiltin;
-    int fOffset;
     bool fOriginUpperLeft;
     bool fOverrideCoverage;
     bool fBlendSupportAllEquations;
-    bool fPushConstant;
 };
 
 } // namespace
index 5e3c0fd..f39e929 100644 (file)
@@ -38,10 +38,6 @@ struct Modifiers {
     : fLayout(layout)
     , fFlags(flags) {}
 
-    Modifiers()
-    : fLayout(Layout())
-    , fFlags(0) {}
-
     std::string description() const {
         std::string result = fLayout.description();
         if (fFlags & kUniform_Flag) {
index 3c31b6b..afd00d8 100644 (file)
@@ -238,6 +238,87 @@ public:
         return fIsSampled;
     }
 
+    static size_t vector_alignment(size_t componentSize, int columns) {
+        return componentSize * (columns + columns % 2);
+    }
+
+    /**
+     * Returns the type's required alignment (when putting this type into a struct, the offset must
+     * be a multiple of the alignment).
+     */
+    size_t alignment() const {
+        // See OpenGL Spec 7.6.2.2 Standard Uniform Block Layout
+        switch (fTypeKind) {
+            case kScalar_Kind:
+                return this->size();
+            case kVector_Kind:
+                return vector_alignment(fComponentType->size(), fColumns);
+            case kMatrix_Kind:
+                return (vector_alignment(fComponentType->size(), fRows) + 15) & ~15;
+            case kArray_Kind:
+                // round up to next multiple of 16
+                return (fComponentType->alignment() + 15) & ~15;
+            case kStruct_Kind: {
+                size_t result = 16;
+                for (size_t i = 0; i < fFields.size(); i++) {
+                    size_t alignment = fFields[i].fType->alignment();
+                    if (alignment > result) {
+                        result = alignment;
+                    }
+                }
+            }
+            default:
+                ABORT(("cannot determine size of type " + fName).c_str());
+        }
+    }
+
+    /**
+     * For matrices and arrays, returns the number of bytes from the start of one entry (row, in
+     * the case of matrices) to the start of the next.
+     */
+    size_t stride() const {
+        switch (fTypeKind) {
+            case kMatrix_Kind: // fall through
+            case kArray_Kind:
+                return this->alignment();
+            default:
+                ABORT("type does not have a stride");
+        }
+    }
+
+    /**
+     * Returns the size of this type in bytes.
+     */
+    size_t size() const {
+        switch (fTypeKind) {
+            case kScalar_Kind:
+                // FIXME need to take precision into account, once we figure out how we want to
+                // handle it...
+                return 4;
+            case kVector_Kind:
+                return fColumns * fComponentType->size();
+            case kMatrix_Kind:
+                return vector_alignment(fComponentType->size(), fRows) * fColumns;
+            case kArray_Kind:
+                return fColumns * this->stride();
+            case kStruct_Kind: {
+                size_t total = 0;
+                for (size_t i = 0; i < fFields.size(); i++) {
+                    size_t alignment = fFields[i].fType->alignment();
+                    if (total % alignment != 0) {
+                        total += alignment - total % alignment;
+                    }
+                    ASSERT(false);
+                    ASSERT(total % alignment == 0);
+                    total += fFields[i].fType->size();
+                }
+                return total;
+            }
+            default:
+                ABORT(("cannot determine size of type " + fName).c_str());
+        }
+    }
+
     /**
      * Returns the corresponding vector or matrix type with the specified number of columns and 
      * rows.
index 9a97a8c..e9e05c1 100644 (file)
@@ -9,7 +9,7 @@
 
 #include "Test.h"
 
-#if SK_SUPPORT_GPU
+#if SKIA_SUPPORT_GPU
 
 static void test_failure(skiatest::Reporter* r, const char* src, const char* error) {
     SkSL::Compiler compiler;
index 3cff1c2..b01aad8 100644 (file)
@@ -9,7 +9,7 @@
 
 #include "Test.h"
 
-#if SK_SUPPORT_GPU
+#if SKIA_SUPPORT_GPU
 
 static void test(skiatest::Reporter* r, const char* src, const GrGLSLCaps& caps, 
                  const char* expected) {
diff --git a/tests/SkSLMemoryLayoutTest.cpp b/tests/SkSLMemoryLayoutTest.cpp
deleted file mode 100644 (file)
index 142ee9e..0000000
+++ /dev/null
@@ -1,176 +0,0 @@
-/*
- * Copyright 2016 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include "SkSLContext.h"
-#include "SkSLMemoryLayout.h"
-
-#include "Test.h"
-
-#if SK_SUPPORT_GPU
-
-DEF_TEST(SkSLMemoryLayout140Test, r) {
-    SkSL::Context context;
-    SkSL::MemoryLayout layout(SkSL::MemoryLayout::k140_Standard);
-
-    // basic types
-    REPORTER_ASSERT(r,  4 == layout.size(*context.fFloat_Type));
-    REPORTER_ASSERT(r,  8 == layout.size(*context.fVec2_Type));
-    REPORTER_ASSERT(r, 12 == layout.size(*context.fVec3_Type));
-    REPORTER_ASSERT(r, 16 == layout.size(*context.fVec4_Type));
-    REPORTER_ASSERT(r,  4 == layout.size(*context.fInt_Type));
-    REPORTER_ASSERT(r,  8 == layout.size(*context.fIVec2_Type));
-    REPORTER_ASSERT(r, 12 == layout.size(*context.fIVec3_Type));
-    REPORTER_ASSERT(r, 16 == layout.size(*context.fIVec4_Type));
-    REPORTER_ASSERT(r,  1 == layout.size(*context.fBool_Type));
-    REPORTER_ASSERT(r,  2 == layout.size(*context.fBVec2_Type));
-    REPORTER_ASSERT(r,  3 == layout.size(*context.fBVec3_Type));
-    REPORTER_ASSERT(r,  4 == layout.size(*context.fBVec4_Type));
-    REPORTER_ASSERT(r, 32 == layout.size(*context.fMat2x2_Type));
-    REPORTER_ASSERT(r, 32 == layout.size(*context.fMat2x4_Type));
-    REPORTER_ASSERT(r, 48 == layout.size(*context.fMat3x3_Type));
-    REPORTER_ASSERT(r, 64 == layout.size(*context.fMat4x2_Type));
-    REPORTER_ASSERT(r, 64 == layout.size(*context.fMat4x4_Type));
-    REPORTER_ASSERT(r,  4 == layout.alignment(*context.fFloat_Type));
-    REPORTER_ASSERT(r,  8 == layout.alignment(*context.fVec2_Type));
-    REPORTER_ASSERT(r, 16 == layout.alignment(*context.fVec3_Type));
-    REPORTER_ASSERT(r, 16 == layout.alignment(*context.fVec4_Type));
-    REPORTER_ASSERT(r,  4 == layout.alignment(*context.fInt_Type));
-    REPORTER_ASSERT(r,  8 == layout.alignment(*context.fIVec2_Type));
-    REPORTER_ASSERT(r, 16 == layout.alignment(*context.fIVec3_Type));
-    REPORTER_ASSERT(r, 16 == layout.alignment(*context.fIVec4_Type));
-    REPORTER_ASSERT(r,  1 == layout.alignment(*context.fBool_Type));
-    REPORTER_ASSERT(r,  2 == layout.alignment(*context.fBVec2_Type));
-    REPORTER_ASSERT(r,  4 == layout.alignment(*context.fBVec3_Type));
-    REPORTER_ASSERT(r,  4 == layout.alignment(*context.fBVec4_Type));
-    REPORTER_ASSERT(r, 16 == layout.alignment(*context.fMat2x2_Type));
-    REPORTER_ASSERT(r, 16 == layout.alignment(*context.fMat2x4_Type));
-    REPORTER_ASSERT(r, 16 == layout.alignment(*context.fMat3x3_Type));
-    REPORTER_ASSERT(r, 16 == layout.alignment(*context.fMat4x2_Type));
-    REPORTER_ASSERT(r, 16 == layout.alignment(*context.fMat4x4_Type));
-
-    // struct 1
-    std::vector<SkSL::Type::Field> fields1;
-    fields1.emplace_back(SkSL::Modifiers(), "a", context.fVec3_Type.get());
-    SkSL::Type s1("s1", fields1);
-    REPORTER_ASSERT(r, 16 == layout.size(s1));
-    REPORTER_ASSERT(r, 16 == layout.alignment(s1));
-
-    fields1.emplace_back(SkSL::Modifiers(), "b", context.fFloat_Type.get());
-    SkSL::Type s2("s2", fields1);
-    REPORTER_ASSERT(r, 16 == layout.size(s2));
-    REPORTER_ASSERT(r, 16 == layout.alignment(s2));
-
-    fields1.emplace_back(SkSL::Modifiers(), "c", context.fBool_Type.get());
-    SkSL::Type s3("s3", fields1);
-    REPORTER_ASSERT(r, 32 == layout.size(s3));
-    REPORTER_ASSERT(r, 16 == layout.alignment(s3));
-
-    // struct 2
-    std::vector<SkSL::Type::Field> fields2;
-    fields2.emplace_back(SkSL::Modifiers(), "a", context.fInt_Type.get());
-    SkSL::Type s4("s4", fields2);
-    REPORTER_ASSERT(r, 16 == layout.size(s4));
-    REPORTER_ASSERT(r, 16 == layout.alignment(s4));
-
-    fields2.emplace_back(SkSL::Modifiers(), "b", context.fVec3_Type.get());
-    SkSL::Type s5("s5", fields2);
-    REPORTER_ASSERT(r, 32 == layout.size(s5));
-    REPORTER_ASSERT(r, 16 == layout.alignment(s5));
-
-    // arrays
-    SkSL::Type array1("float[4]", SkSL::Type::kArray_Kind, *context.fFloat_Type, 4);
-    REPORTER_ASSERT(r, 64 == layout.size(array1));
-    REPORTER_ASSERT(r, 16 == layout.alignment(array1));
-    REPORTER_ASSERT(r, 16 == layout.stride(array1));
-
-    SkSL::Type array2("vec4[4]", SkSL::Type::kArray_Kind, *context.fVec4_Type, 4);
-    REPORTER_ASSERT(r, 64 == layout.size(array2));
-    REPORTER_ASSERT(r, 16 == layout.alignment(array2));
-    REPORTER_ASSERT(r, 16 == layout.stride(array2));
-}
-
-DEF_TEST(SkSLMemoryLayout430Test, r) {
-    SkSL::Context context;
-    SkSL::MemoryLayout layout(SkSL::MemoryLayout::k430_Standard);
-
-    // basic types
-    REPORTER_ASSERT(r,  4 == layout.size(*context.fFloat_Type));
-    REPORTER_ASSERT(r,  8 == layout.size(*context.fVec2_Type));
-    REPORTER_ASSERT(r, 12 == layout.size(*context.fVec3_Type));
-    REPORTER_ASSERT(r, 16 == layout.size(*context.fVec4_Type));
-    REPORTER_ASSERT(r,  4 == layout.size(*context.fInt_Type));
-    REPORTER_ASSERT(r,  8 == layout.size(*context.fIVec2_Type));
-    REPORTER_ASSERT(r, 12 == layout.size(*context.fIVec3_Type));
-    REPORTER_ASSERT(r, 16 == layout.size(*context.fIVec4_Type));
-    REPORTER_ASSERT(r,  1 == layout.size(*context.fBool_Type));
-    REPORTER_ASSERT(r,  2 == layout.size(*context.fBVec2_Type));
-    REPORTER_ASSERT(r,  3 == layout.size(*context.fBVec3_Type));
-    REPORTER_ASSERT(r,  4 == layout.size(*context.fBVec4_Type));
-    REPORTER_ASSERT(r, 16 == layout.size(*context.fMat2x2_Type));
-    REPORTER_ASSERT(r, 32 == layout.size(*context.fMat2x4_Type));
-    REPORTER_ASSERT(r, 48 == layout.size(*context.fMat3x3_Type));
-    REPORTER_ASSERT(r, 32 == layout.size(*context.fMat4x2_Type));
-    REPORTER_ASSERT(r, 64 == layout.size(*context.fMat4x4_Type));
-    REPORTER_ASSERT(r,  4 == layout.alignment(*context.fFloat_Type));
-    REPORTER_ASSERT(r,  8 == layout.alignment(*context.fVec2_Type));
-    REPORTER_ASSERT(r, 16 == layout.alignment(*context.fVec3_Type));
-    REPORTER_ASSERT(r, 16 == layout.alignment(*context.fVec4_Type));
-    REPORTER_ASSERT(r,  4 == layout.alignment(*context.fInt_Type));
-    REPORTER_ASSERT(r,  8 == layout.alignment(*context.fIVec2_Type));
-    REPORTER_ASSERT(r, 16 == layout.alignment(*context.fIVec3_Type));
-    REPORTER_ASSERT(r, 16 == layout.alignment(*context.fIVec4_Type));
-    REPORTER_ASSERT(r,  1 == layout.alignment(*context.fBool_Type));
-    REPORTER_ASSERT(r,  2 == layout.alignment(*context.fBVec2_Type));
-    REPORTER_ASSERT(r,  4 == layout.alignment(*context.fBVec3_Type));
-    REPORTER_ASSERT(r,  4 == layout.alignment(*context.fBVec4_Type));
-    REPORTER_ASSERT(r,  8 == layout.alignment(*context.fMat2x2_Type));
-    REPORTER_ASSERT(r, 16 == layout.alignment(*context.fMat2x4_Type));
-    REPORTER_ASSERT(r, 16 == layout.alignment(*context.fMat3x3_Type));
-    REPORTER_ASSERT(r,  8 == layout.alignment(*context.fMat4x2_Type));
-    REPORTER_ASSERT(r, 16 == layout.alignment(*context.fMat4x4_Type));
-
-    // struct 1
-    std::vector<SkSL::Type::Field> fields1;
-    fields1.emplace_back(SkSL::Modifiers(), "a", context.fVec3_Type.get());
-    SkSL::Type s1("s1", fields1);
-    REPORTER_ASSERT(r, 16 == layout.size(s1));
-    REPORTER_ASSERT(r, 16 == layout.alignment(s1));
-
-    fields1.emplace_back(SkSL::Modifiers(), "b", context.fFloat_Type.get());
-    SkSL::Type s2("s2", fields1);
-    REPORTER_ASSERT(r, 16 == layout.size(s2));
-    REPORTER_ASSERT(r, 16 == layout.alignment(s2));
-
-    fields1.emplace_back(SkSL::Modifiers(), "c", context.fBool_Type.get());
-    SkSL::Type s3("s3", fields1);
-    REPORTER_ASSERT(r, 32 == layout.size(s3));
-    REPORTER_ASSERT(r, 16 == layout.alignment(s3));
-
-    // struct 2
-    std::vector<SkSL::Type::Field> fields2;
-    fields2.emplace_back(SkSL::Modifiers(), "a", context.fInt_Type.get());
-    SkSL::Type s4("s4", fields2);
-    REPORTER_ASSERT(r, 4 == layout.size(s4));
-    REPORTER_ASSERT(r, 4 == layout.alignment(s4));
-
-    fields2.emplace_back(SkSL::Modifiers(), "b", context.fVec3_Type.get());
-    SkSL::Type s5("s5", fields2);
-    REPORTER_ASSERT(r, 32 == layout.size(s5));
-    REPORTER_ASSERT(r, 16 == layout.alignment(s5));
-
-    // arrays
-    SkSL::Type array1("float[4]", SkSL::Type::kArray_Kind, *context.fFloat_Type, 4);
-    REPORTER_ASSERT(r, 16 == layout.size(array1));
-    REPORTER_ASSERT(r, 4 == layout.alignment(array1));
-    REPORTER_ASSERT(r, 4 == layout.stride(array1));
-
-    SkSL::Type array2("vec4[4]", SkSL::Type::kArray_Kind, *context.fVec4_Type, 4);
-    REPORTER_ASSERT(r, 64 == layout.size(array2));
-    REPORTER_ASSERT(r, 16 == layout.alignment(array2));
-    REPORTER_ASSERT(r, 16 == layout.stride(array2));
-}
-#endif