aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/sksl/ir/SkSLSwizzle.h
diff options
context:
space:
mode:
authorGravatar ethannicholas <ethannicholas@google.com>2016-07-01 08:22:01 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2016-07-01 08:22:01 -0700
commitb3058bdb1049ca75d526eb9f11e1a42a49e63585 (patch)
tree9d72636cd37c2100869fcd02041072b19b86a717 /src/sksl/ir/SkSLSwizzle.h
parente7d1b24ff0a04686aef54fcb9feaca7a03e19891 (diff)
initial checkin of SkSL compiler
BUG=skia: GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1984363002 CQ_EXTRA_TRYBOTS=client.skia.compile:Build-Ubuntu-GCC-x86_64-Release-CMake-Trybot,Build-Mac-Clang-x86_64-Release-CMake-Trybot Review-Url: https://codereview.chromium.org/1984363002
Diffstat (limited to 'src/sksl/ir/SkSLSwizzle.h')
-rw-r--r--src/sksl/ir/SkSLSwizzle.h88
1 files changed, 88 insertions, 0 deletions
diff --git a/src/sksl/ir/SkSLSwizzle.h b/src/sksl/ir/SkSLSwizzle.h
new file mode 100644
index 0000000000..ce360d1847
--- /dev/null
+++ b/src/sksl/ir/SkSLSwizzle.h
@@ -0,0 +1,88 @@
+/*
+ * 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 SKSL_SWIZZLE
+#define SKSL_SWIZZLE
+
+#include "SkSLExpression.h"
+#include "SkSLUtil.h"
+
+namespace SkSL {
+
+/**
+ * Given a type and a swizzle component count, returns the type that will result from swizzling. For
+ * instance, swizzling a vec3 with two components will result in a vec2. It is possible to swizzle
+ * with more components than the source vector, as in 'vec2(1).xxxx'.
+ */
+static std::shared_ptr<Type> get_type(Expression& value,
+ size_t count) {
+ std::shared_ptr<Type> base = value.fType->componentType();
+ if (count == 1) {
+ return base;
+ }
+ if (base == kFloat_Type) {
+ switch (count) {
+ case 2: return kVec2_Type;
+ case 3: return kVec3_Type;
+ case 4: return kVec4_Type;
+ }
+ } else if (base == kDouble_Type) {
+ switch (count) {
+ case 2: return kDVec2_Type;
+ case 3: return kDVec3_Type;
+ case 4: return kDVec4_Type;
+ }
+ } else if (base == kInt_Type) {
+ switch (count) {
+ case 2: return kIVec2_Type;
+ case 3: return kIVec3_Type;
+ case 4: return kIVec4_Type;
+ }
+ } else if (base == kUInt_Type) {
+ switch (count) {
+ case 2: return kUVec2_Type;
+ case 3: return kUVec3_Type;
+ case 4: return kUVec4_Type;
+ }
+ } else if (base == kBool_Type) {
+ switch (count) {
+ case 2: return kBVec2_Type;
+ case 3: return kBVec3_Type;
+ case 4: return kBVec4_Type;
+ }
+ }
+ ABORT("cannot swizzle %s\n", value.description().c_str());
+}
+
+/**
+ * Represents a vector swizzle operation such as 'vec2(1, 2, 3).zyx'.
+ */
+struct Swizzle : public Expression {
+ Swizzle(std::unique_ptr<Expression> base, std::vector<int> components)
+ : INHERITED(base->fPosition, kSwizzle_Kind, get_type(*base, components.size()))
+ , fBase(std::move(base))
+ , fComponents(std::move(components)) {
+ ASSERT(fComponents.size() >= 1 && fComponents.size() <= 4);
+ }
+
+ std::string description() const override {
+ std::string result = fBase->description() + ".";
+ for (int x : fComponents) {
+ result += "xyzw"[x];
+ }
+ return result;
+ }
+
+ const std::unique_ptr<Expression> fBase;
+ const std::vector<int> fComponents;
+
+ typedef Expression INHERITED;
+};
+
+} // namespace
+
+#endif