Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 Google LLC |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Ethan Nicholas | 24c1772 | 2021-03-09 13:10:59 -0500 | [diff] [blame] | 8 | #include "include/private/SkSLProgramElement.h" |
| 9 | #include "include/private/SkSLStatement.h" |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 10 | #include "include/private/SkTArray.h" |
Brian Osman | b8ebe23 | 2021-01-19 16:33:11 -0500 | [diff] [blame] | 11 | #include "include/private/SkTPin.h" |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 12 | #include "src/sksl/SkSLCompiler.h" |
| 13 | #include "src/sksl/SkSLOperators.h" |
John Stiles | 3738ef5 | 2021-04-13 10:41:57 -0400 | [diff] [blame] | 14 | #include "src/sksl/codegen/SkSLCodeGenerator.h" |
| 15 | #include "src/sksl/codegen/SkSLVMCodeGenerator.h" |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 16 | #include "src/sksl/ir/SkSLBinaryExpression.h" |
| 17 | #include "src/sksl/ir/SkSLBlock.h" |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 18 | #include "src/sksl/ir/SkSLBreakStatement.h" |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 19 | #include "src/sksl/ir/SkSLChildCall.h" |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 20 | #include "src/sksl/ir/SkSLConstructor.h" |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 21 | #include "src/sksl/ir/SkSLConstructorArray.h" |
John Stiles | e3ae968 | 2021-08-05 10:35:01 -0400 | [diff] [blame] | 22 | #include "src/sksl/ir/SkSLConstructorArrayCast.h" |
John Stiles | e118278 | 2021-03-30 22:09:37 -0400 | [diff] [blame] | 23 | #include "src/sksl/ir/SkSLConstructorDiagonalMatrix.h" |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 24 | #include "src/sksl/ir/SkSLConstructorMatrixResize.h" |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 25 | #include "src/sksl/ir/SkSLConstructorSplat.h" |
John Stiles | d47330f | 2021-04-08 23:25:52 -0400 | [diff] [blame] | 26 | #include "src/sksl/ir/SkSLConstructorStruct.h" |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 27 | #include "src/sksl/ir/SkSLContinueStatement.h" |
| 28 | #include "src/sksl/ir/SkSLDoStatement.h" |
| 29 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
| 30 | #include "src/sksl/ir/SkSLExternalFunctionCall.h" |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 31 | #include "src/sksl/ir/SkSLExternalFunctionReference.h" |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 32 | #include "src/sksl/ir/SkSLFieldAccess.h" |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 33 | #include "src/sksl/ir/SkSLForStatement.h" |
| 34 | #include "src/sksl/ir/SkSLFunctionCall.h" |
| 35 | #include "src/sksl/ir/SkSLFunctionDeclaration.h" |
| 36 | #include "src/sksl/ir/SkSLFunctionDefinition.h" |
| 37 | #include "src/sksl/ir/SkSLIfStatement.h" |
| 38 | #include "src/sksl/ir/SkSLIndexExpression.h" |
John Stiles | 7591d4b | 2021-09-13 13:32:06 -0400 | [diff] [blame] | 39 | #include "src/sksl/ir/SkSLLiteral.h" |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 40 | #include "src/sksl/ir/SkSLPostfixExpression.h" |
| 41 | #include "src/sksl/ir/SkSLPrefixExpression.h" |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 42 | #include "src/sksl/ir/SkSLReturnStatement.h" |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 43 | #include "src/sksl/ir/SkSLSwitchStatement.h" |
| 44 | #include "src/sksl/ir/SkSLSwizzle.h" |
| 45 | #include "src/sksl/ir/SkSLTernaryExpression.h" |
| 46 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
| 47 | #include "src/sksl/ir/SkSLVariableReference.h" |
| 48 | |
| 49 | #include <algorithm> |
| 50 | #include <unordered_map> |
| 51 | |
Mike Klein | ff4decc | 2021-02-10 16:13:35 -0600 | [diff] [blame] | 52 | namespace { |
| 53 | // sksl allows the optimizations of fast_mul(), so we want to use that most of the time. |
| 54 | // This little sneaky snippet of code lets us use ** as a fast multiply infix operator. |
| 55 | struct FastF32 { skvm::F32 val; }; |
| 56 | static FastF32 operator*(skvm::F32 y) { return {y}; } |
| 57 | static skvm::F32 operator*(skvm::F32 x, FastF32 y) { return fast_mul(x, y.val); } |
| 58 | static skvm::F32 operator*(float x, FastF32 y) { return fast_mul(x, y.val); } |
| 59 | } |
| 60 | |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 61 | namespace SkSL { |
| 62 | |
| 63 | namespace { |
| 64 | |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 65 | // Holds scalars, vectors, or matrices |
| 66 | struct Value { |
| 67 | Value() = default; |
| 68 | explicit Value(size_t slots) { |
| 69 | fVals.resize(slots); |
| 70 | } |
| 71 | Value(skvm::F32 x) : fVals({ x.id }) {} |
| 72 | Value(skvm::I32 x) : fVals({ x.id }) {} |
| 73 | |
| 74 | explicit operator bool() const { return !fVals.empty(); } |
| 75 | |
| 76 | size_t slots() const { return fVals.size(); } |
| 77 | |
| 78 | struct ValRef { |
| 79 | ValRef(skvm::Val& val) : fVal(val) {} |
| 80 | |
| 81 | ValRef& operator=(ValRef v) { fVal = v.fVal; return *this; } |
| 82 | ValRef& operator=(skvm::Val v) { fVal = v; return *this; } |
| 83 | ValRef& operator=(skvm::F32 v) { fVal = v.id; return *this; } |
| 84 | ValRef& operator=(skvm::I32 v) { fVal = v.id; return *this; } |
| 85 | |
| 86 | operator skvm::Val() { return fVal; } |
| 87 | |
| 88 | skvm::Val& fVal; |
| 89 | }; |
| 90 | |
Brian Osman | f932c69 | 2021-01-26 13:54:07 -0500 | [diff] [blame] | 91 | ValRef operator[](size_t i) { |
| 92 | // These redundant asserts work around what we think is a codegen bug in GCC 8.x for |
| 93 | // 32-bit x86 Debug builds. |
| 94 | SkASSERT(i < fVals.size()); |
| 95 | return fVals[i]; |
| 96 | } |
| 97 | skvm::Val operator[](size_t i) const { |
| 98 | // These redundant asserts work around what we think is a codegen bug in GCC 8.x for |
| 99 | // 32-bit x86 Debug builds. |
| 100 | SkASSERT(i < fVals.size()); |
| 101 | return fVals[i]; |
| 102 | } |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 103 | |
Brian Osman | ae87bf1 | 2021-05-11 13:36:10 -0400 | [diff] [blame] | 104 | SkSpan<skvm::Val> asSpan() { return SkMakeSpan(fVals); } |
Brian Osman | 54515b7 | 2021-01-07 14:38:08 -0500 | [diff] [blame] | 105 | |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 106 | private: |
| 107 | SkSTArray<4, skvm::Val, true> fVals; |
| 108 | }; |
| 109 | |
| 110 | } // namespace |
| 111 | |
| 112 | class SkVMGenerator { |
| 113 | public: |
| 114 | SkVMGenerator(const Program& program, |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 115 | skvm::Builder* builder, |
John Stiles | 137482f | 2021-07-23 10:38:57 -0400 | [diff] [blame] | 116 | SampleShaderFn sampleShader, |
John Stiles | 2955c26 | 2021-07-23 15:51:05 -0400 | [diff] [blame] | 117 | SampleColorFilterFn sampleColorFilter, |
| 118 | SampleBlenderFn sampleBlender); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 119 | |
John Stiles | a4f5683 | 2021-09-22 14:22:33 -0400 | [diff] [blame] | 120 | void writeProgram(SkSpan<skvm::Val> uniforms, |
| 121 | skvm::Coord device, |
| 122 | const FunctionDefinition& function, |
| 123 | SkSpan<skvm::Val> arguments, |
| 124 | SkSpan<skvm::Val> outReturn); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 125 | |
| 126 | private: |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 127 | /** |
| 128 | * In SkSL, a Variable represents a named, typed value (along with qualifiers, etc). |
Brian Osman | 21f5707 | 2021-01-25 13:51:57 -0500 | [diff] [blame] | 129 | * Every Variable is mapped to one (or several, contiguous) indices into our vector of |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 130 | * skvm::Val. Those skvm::Val entries hold the current actual value of that variable. |
| 131 | * |
| 132 | * NOTE: Conceptually, each Variable is just mapped to a Value. We could implement it that way, |
Brian Osman | 21f5707 | 2021-01-25 13:51:57 -0500 | [diff] [blame] | 133 | * (and eliminate the indirection), but it would add overhead for each Variable, |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 134 | * and add additional (different) bookkeeping for things like lvalue-swizzles. |
| 135 | * |
| 136 | * Any time a variable appears in an expression, that's a VariableReference, which is a kind of |
| 137 | * Expression. Evaluating that VariableReference (or any other Expression) produces a Value, |
| 138 | * which is a set of skvm::Val. (This allows an Expression to produce a vector or matrix, in |
| 139 | * addition to a scalar). |
| 140 | * |
Brian Osman | 21f5707 | 2021-01-25 13:51:57 -0500 | [diff] [blame] | 141 | * For a VariableReference, producing a Value is straightforward - we get the slot of the |
| 142 | * Variable (from fVariableMap), use that to look up the current skvm::Vals holding the |
| 143 | * variable's contents, and construct a Value with those ids. |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 144 | */ |
| 145 | |
| 146 | /** |
Brian Osman | 21f5707 | 2021-01-25 13:51:57 -0500 | [diff] [blame] | 147 | * Returns the slot holding v's Val(s). Allocates storage if this is first time 'v' is |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 148 | * referenced. Compound variables (e.g. vectors) will consume more than one slot, with |
| 149 | * getSlot returning the start of the contiguous chunk of slots. |
| 150 | */ |
Brian Osman | 21f5707 | 2021-01-25 13:51:57 -0500 | [diff] [blame] | 151 | size_t getSlot(const Variable& v); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 152 | |
John Stiles | a4f5683 | 2021-09-22 14:22:33 -0400 | [diff] [blame] | 153 | /** Initializes uniforms and global variables at the start of main(). */ |
| 154 | void setupGlobals(SkSpan<skvm::Val> uniforms, skvm::Coord device); |
| 155 | |
| 156 | /** Emits an SkSL function. */ |
| 157 | void writeFunction(const FunctionDefinition& function, |
| 158 | SkSpan<skvm::Val> arguments, |
| 159 | SkSpan<skvm::Val> outReturn); |
| 160 | |
Mike Klein | aebcf73 | 2021-01-14 10:15:00 -0600 | [diff] [blame] | 161 | skvm::F32 f32(skvm::Val id) { SkASSERT(id != skvm::NA); return {fBuilder, id}; } |
| 162 | skvm::I32 i32(skvm::Val id) { SkASSERT(id != skvm::NA); return {fBuilder, id}; } |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 163 | |
| 164 | // Shorthand for scalars |
| 165 | skvm::F32 f32(const Value& v) { SkASSERT(v.slots() == 1); return f32(v[0]); } |
| 166 | skvm::I32 i32(const Value& v) { SkASSERT(v.slots() == 1); return i32(v[0]); } |
| 167 | |
| 168 | template <typename Fn> |
| 169 | Value unary(const Value& v, Fn&& fn) { |
| 170 | Value result(v.slots()); |
| 171 | for (size_t i = 0; i < v.slots(); ++i) { |
| 172 | result[i] = fn({fBuilder, v[i]}); |
| 173 | } |
| 174 | return result; |
| 175 | } |
| 176 | |
Brian Osman | 54515b7 | 2021-01-07 14:38:08 -0500 | [diff] [blame] | 177 | skvm::I32 mask() { |
| 178 | // As we encounter (possibly conditional) return statements, fReturned is updated to store |
| 179 | // the lanes that have already returned. For the remainder of the current function, those |
| 180 | // lanes should be disabled. |
Brian Osman | 9333c87 | 2021-01-13 15:06:17 -0500 | [diff] [blame] | 181 | return fConditionMask & fLoopMask & ~currentFunction().fReturned; |
Brian Osman | 54515b7 | 2021-01-07 14:38:08 -0500 | [diff] [blame] | 182 | } |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 183 | |
Brian Osman | fa71ffa | 2021-01-26 14:05:31 -0500 | [diff] [blame] | 184 | size_t fieldSlotOffset(const FieldAccess& expr); |
| 185 | size_t indexSlotOffset(const IndexExpression& expr); |
| 186 | |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 187 | Value writeExpression(const Expression& expr); |
| 188 | Value writeBinaryExpression(const BinaryExpression& b); |
John Stiles | d986f47 | 2021-04-06 15:54:43 -0400 | [diff] [blame] | 189 | Value writeAggregationConstructor(const AnyConstructor& c); |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 190 | Value writeChildCall(const ChildCall& c); |
John Stiles | e118278 | 2021-03-30 22:09:37 -0400 | [diff] [blame] | 191 | Value writeConstructorDiagonalMatrix(const ConstructorDiagonalMatrix& c); |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 192 | Value writeConstructorMatrixResize(const ConstructorMatrixResize& c); |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 193 | Value writeConstructorCast(const AnyConstructor& c); |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 194 | Value writeConstructorSplat(const ConstructorSplat& c); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 195 | Value writeFunctionCall(const FunctionCall& c); |
Brian Osman | dd50b0c | 2021-01-11 17:04:29 -0500 | [diff] [blame] | 196 | Value writeExternalFunctionCall(const ExternalFunctionCall& c); |
Brian Osman | fa71ffa | 2021-01-26 14:05:31 -0500 | [diff] [blame] | 197 | Value writeFieldAccess(const FieldAccess& expr); |
John Stiles | 7591d4b | 2021-09-13 13:32:06 -0400 | [diff] [blame] | 198 | Value writeLiteral(const Literal& l); |
Brian Osman | fa71ffa | 2021-01-26 14:05:31 -0500 | [diff] [blame] | 199 | Value writeIndexExpression(const IndexExpression& expr); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 200 | Value writeIntrinsicCall(const FunctionCall& c); |
| 201 | Value writePostfixExpression(const PostfixExpression& p); |
| 202 | Value writePrefixExpression(const PrefixExpression& p); |
| 203 | Value writeSwizzle(const Swizzle& swizzle); |
| 204 | Value writeTernaryExpression(const TernaryExpression& t); |
Brian Osman | fa71ffa | 2021-01-26 14:05:31 -0500 | [diff] [blame] | 205 | Value writeVariableExpression(const VariableReference& expr); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 206 | |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 207 | Value writeTypeConversion(const Value& src, Type::NumberKind srcKind, Type::NumberKind dstKind); |
| 208 | |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 209 | void writeStatement(const Statement& s); |
| 210 | void writeBlock(const Block& b); |
Brian Osman | 9333c87 | 2021-01-13 15:06:17 -0500 | [diff] [blame] | 211 | void writeBreakStatement(); |
| 212 | void writeContinueStatement(); |
| 213 | void writeForStatement(const ForStatement& f); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 214 | void writeIfStatement(const IfStatement& stmt); |
| 215 | void writeReturnStatement(const ReturnStatement& r); |
John Stiles | cb40008 | 2021-09-23 11:07:32 -0400 | [diff] [blame] | 216 | void writeSwitchStatement(const SwitchStatement& s); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 217 | void writeVarDeclaration(const VarDeclaration& decl); |
| 218 | |
| 219 | Value writeStore(const Expression& lhs, const Value& rhs); |
John Stiles | d71fac6 | 2021-09-23 17:14:04 -0400 | [diff] [blame^] | 220 | skvm::Val writeConditionalStore(skvm::Val lhs, skvm::Val rhs, skvm::I32 mask); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 221 | |
| 222 | Value writeMatrixInverse2x2(const Value& m); |
| 223 | Value writeMatrixInverse3x3(const Value& m); |
| 224 | Value writeMatrixInverse4x4(const Value& m); |
| 225 | |
Brian Osman | db2dad5 | 2021-01-07 14:08:30 -0500 | [diff] [blame] | 226 | // |
| 227 | // Global state for the lifetime of the generator: |
| 228 | // |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 229 | const Program& fProgram; |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 230 | skvm::Builder* fBuilder; |
| 231 | |
John Stiles | 137482f | 2021-07-23 10:38:57 -0400 | [diff] [blame] | 232 | const SampleShaderFn fSampleShader; |
| 233 | const SampleColorFilterFn fSampleColorFilter; |
John Stiles | 2955c26 | 2021-07-23 15:51:05 -0400 | [diff] [blame] | 234 | const SampleBlenderFn fSampleBlender; |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 235 | |
| 236 | // [Variable, first slot in fSlots] |
Brian Osman | 21f5707 | 2021-01-25 13:51:57 -0500 | [diff] [blame] | 237 | std::unordered_map<const Variable*, size_t> fVariableMap; |
Brian Osman | db2dad5 | 2021-01-07 14:08:30 -0500 | [diff] [blame] | 238 | std::vector<skvm::Val> fSlots; |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 239 | |
Brian Osman | 9333c87 | 2021-01-13 15:06:17 -0500 | [diff] [blame] | 240 | // Conditional execution mask (managed by ScopedCondition, and tied to control-flow scopes) |
| 241 | skvm::I32 fConditionMask; |
| 242 | |
| 243 | // Similar: loop execution masks. Each loop starts with all lanes active (fLoopMask). |
| 244 | // 'break' disables a lane in fLoopMask until the loop finishes |
| 245 | // 'continue' disables a lane in fLoopMask, and sets fContinueMask to be re-enabled on the next |
| 246 | // iteration |
| 247 | skvm::I32 fLoopMask; |
| 248 | skvm::I32 fContinueMask; |
Brian Osman | 54515b7 | 2021-01-07 14:38:08 -0500 | [diff] [blame] | 249 | |
Brian Osman | db2dad5 | 2021-01-07 14:08:30 -0500 | [diff] [blame] | 250 | // |
| 251 | // State that's local to the generation of a single function: |
| 252 | // |
Brian Osman | 54515b7 | 2021-01-07 14:38:08 -0500 | [diff] [blame] | 253 | struct Function { |
| 254 | const SkSpan<skvm::Val> fReturnValue; |
| 255 | skvm::I32 fReturned; |
| 256 | }; |
| 257 | std::vector<Function> fFunctionStack; |
| 258 | Function& currentFunction() { return fFunctionStack.back(); } |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 259 | |
Brian Osman | 9333c87 | 2021-01-13 15:06:17 -0500 | [diff] [blame] | 260 | class ScopedCondition { |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 261 | public: |
Brian Osman | 9333c87 | 2021-01-13 15:06:17 -0500 | [diff] [blame] | 262 | ScopedCondition(SkVMGenerator* generator, skvm::I32 mask) |
| 263 | : fGenerator(generator), fOldConditionMask(fGenerator->fConditionMask) { |
| 264 | fGenerator->fConditionMask &= mask; |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 265 | } |
| 266 | |
Brian Osman | 9333c87 | 2021-01-13 15:06:17 -0500 | [diff] [blame] | 267 | ~ScopedCondition() { fGenerator->fConditionMask = fOldConditionMask; } |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 268 | |
| 269 | private: |
| 270 | SkVMGenerator* fGenerator; |
Brian Osman | 9333c87 | 2021-01-13 15:06:17 -0500 | [diff] [blame] | 271 | skvm::I32 fOldConditionMask; |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 272 | }; |
| 273 | }; |
| 274 | |
| 275 | static Type::NumberKind base_number_kind(const Type& type) { |
| 276 | if (type.typeKind() == Type::TypeKind::kMatrix || type.typeKind() == Type::TypeKind::kVector) { |
| 277 | return base_number_kind(type.componentType()); |
| 278 | } |
| 279 | return type.numberKind(); |
| 280 | } |
| 281 | |
| 282 | static inline bool is_uniform(const SkSL::Variable& var) { |
| 283 | return var.modifiers().fFlags & Modifiers::kUniform_Flag; |
| 284 | } |
| 285 | |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 286 | SkVMGenerator::SkVMGenerator(const Program& program, |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 287 | skvm::Builder* builder, |
John Stiles | 137482f | 2021-07-23 10:38:57 -0400 | [diff] [blame] | 288 | SampleShaderFn sampleShader, |
John Stiles | 2955c26 | 2021-07-23 15:51:05 -0400 | [diff] [blame] | 289 | SampleColorFilterFn sampleColorFilter, |
| 290 | SampleBlenderFn sampleBlender) |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 291 | : fProgram(program) |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 292 | , fBuilder(builder) |
John Stiles | 137482f | 2021-07-23 10:38:57 -0400 | [diff] [blame] | 293 | , fSampleShader(std::move(sampleShader)) |
John Stiles | 2955c26 | 2021-07-23 15:51:05 -0400 | [diff] [blame] | 294 | , fSampleColorFilter(std::move(sampleColorFilter)) |
John Stiles | a4f5683 | 2021-09-22 14:22:33 -0400 | [diff] [blame] | 295 | , fSampleBlender(std::move(sampleBlender)) {} |
| 296 | |
| 297 | void SkVMGenerator::writeProgram(SkSpan<skvm::Val> uniforms, |
| 298 | skvm::Coord device, |
| 299 | const FunctionDefinition& function, |
| 300 | SkSpan<skvm::Val> arguments, |
| 301 | SkSpan<skvm::Val> outReturn) { |
Brian Osman | 9333c87 | 2021-01-13 15:06:17 -0500 | [diff] [blame] | 302 | fConditionMask = fLoopMask = fBuilder->splat(0xffff'ffff); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 303 | |
John Stiles | a4f5683 | 2021-09-22 14:22:33 -0400 | [diff] [blame] | 304 | this->setupGlobals(uniforms, device); |
| 305 | this->writeFunction(function, arguments, outReturn); |
| 306 | } |
| 307 | |
| 308 | void SkVMGenerator::setupGlobals(SkSpan<skvm::Val> uniforms, skvm::Coord device) { |
| 309 | // Add storage for each global variable (including uniforms) to fSlots, and entries in |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 310 | // fVariableMap to remember where every variable is stored. |
| 311 | const skvm::Val* uniformIter = uniforms.begin(); |
| 312 | size_t fpCount = 0; |
| 313 | for (const ProgramElement* e : fProgram.elements()) { |
| 314 | if (e->is<GlobalVarDeclaration>()) { |
Brian Osman | c057669 | 2021-02-17 13:52:35 -0500 | [diff] [blame] | 315 | const GlobalVarDeclaration& gvd = e->as<GlobalVarDeclaration>(); |
| 316 | const VarDeclaration& decl = gvd.declaration()->as<VarDeclaration>(); |
| 317 | const Variable& var = decl.var(); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 318 | SkASSERT(fVariableMap.find(&var) == fVariableMap.end()); |
| 319 | |
Brian Osman | 14d0096 | 2021-04-02 17:04:35 -0400 | [diff] [blame] | 320 | // For most variables, fVariableMap stores an index into fSlots, but for children, |
John Stiles | 2955c26 | 2021-07-23 15:51:05 -0400 | [diff] [blame] | 321 | // fVariableMap stores the index to pass to fSample(Shader|ColorFilter|Blender) |
Brian Osman | 14d0096 | 2021-04-02 17:04:35 -0400 | [diff] [blame] | 322 | if (var.type().isEffectChild()) { |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 323 | fVariableMap[&var] = fpCount++; |
| 324 | continue; |
| 325 | } |
| 326 | |
| 327 | // Opaque types include fragment processors, GL objects (samplers, textures, etc), and |
| 328 | // special types like 'void'. Of those, only fragment processors are legal variables. |
| 329 | SkASSERT(!var.type().isOpaque()); |
| 330 | |
Brian Osman | c057669 | 2021-02-17 13:52:35 -0500 | [diff] [blame] | 331 | // getSlot() allocates space for the variable's value in fSlots, initializes it to zero, |
| 332 | // and populates fVariableMap. |
| 333 | size_t slot = this->getSlot(var), |
John Stiles | 47b087e | 2021-04-06 13:19:35 -0400 | [diff] [blame] | 334 | nslots = var.type().slotCount(); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 335 | |
John Stiles | a4f5683 | 2021-09-22 14:22:33 -0400 | [diff] [blame] | 336 | // builtin variables are system-defined, with special semantics. The only builtin |
| 337 | // variable exposed to runtime effects is sk_FragCoord. |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 338 | if (int builtin = var.modifiers().fLayout.fBuiltin; builtin >= 0) { |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 339 | switch (builtin) { |
| 340 | case SK_FRAGCOORD_BUILTIN: |
| 341 | SkASSERT(nslots == 4); |
Brian Osman | c057669 | 2021-02-17 13:52:35 -0500 | [diff] [blame] | 342 | fSlots[slot + 0] = device.x.id; |
| 343 | fSlots[slot + 1] = device.y.id; |
| 344 | fSlots[slot + 2] = fBuilder->splat(0.0f).id; |
| 345 | fSlots[slot + 3] = fBuilder->splat(1.0f).id; |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 346 | break; |
| 347 | default: |
John Stiles | cb40008 | 2021-09-23 11:07:32 -0400 | [diff] [blame] | 348 | SkDEBUGFAILF("Unsupported builtin %d", builtin); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 349 | } |
John Stiles | a4f5683 | 2021-09-22 14:22:33 -0400 | [diff] [blame] | 350 | continue; |
| 351 | } |
| 352 | |
| 353 | // For uniforms, copy the supplied IDs over |
| 354 | if (is_uniform(var)) { |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 355 | SkASSERT(uniformIter + nslots <= uniforms.end()); |
Brian Osman | c057669 | 2021-02-17 13:52:35 -0500 | [diff] [blame] | 356 | std::copy(uniformIter, uniformIter + nslots, fSlots.begin() + slot); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 357 | uniformIter += nslots; |
John Stiles | a4f5683 | 2021-09-22 14:22:33 -0400 | [diff] [blame] | 358 | continue; |
| 359 | } |
| 360 | |
| 361 | // For other globals, populate with the initializer expression (if there is one) |
| 362 | if (decl.value()) { |
Brian Osman | c057669 | 2021-02-17 13:52:35 -0500 | [diff] [blame] | 363 | Value val = this->writeExpression(*decl.value()); |
| 364 | for (size_t i = 0; i < nslots; ++i) { |
| 365 | fSlots[slot + i] = val[i]; |
| 366 | } |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | } |
| 370 | SkASSERT(uniformIter == uniforms.end()); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 371 | } |
| 372 | |
Brian Osman | db2dad5 | 2021-01-07 14:08:30 -0500 | [diff] [blame] | 373 | void SkVMGenerator::writeFunction(const FunctionDefinition& function, |
| 374 | SkSpan<skvm::Val> arguments, |
| 375 | SkSpan<skvm::Val> outReturn) { |
Brian Osman | db2dad5 | 2021-01-07 14:08:30 -0500 | [diff] [blame] | 376 | const FunctionDeclaration& decl = function.declaration(); |
John Stiles | 47b087e | 2021-04-06 13:19:35 -0400 | [diff] [blame] | 377 | SkASSERT(decl.returnType().slotCount() == outReturn.size()); |
Brian Osman | db2dad5 | 2021-01-07 14:08:30 -0500 | [diff] [blame] | 378 | |
Brian Osman | 54515b7 | 2021-01-07 14:38:08 -0500 | [diff] [blame] | 379 | fFunctionStack.push_back({outReturn, /*returned=*/fBuilder->splat(0)}); |
Brian Osman | db2dad5 | 2021-01-07 14:08:30 -0500 | [diff] [blame] | 380 | |
| 381 | // For all parameters, copy incoming argument IDs to our vector of (all) variable IDs |
Brian Osman | 5933d4c | 2021-01-05 13:02:20 -0500 | [diff] [blame] | 382 | size_t argIdx = 0; |
Brian Osman | db2dad5 | 2021-01-07 14:08:30 -0500 | [diff] [blame] | 383 | for (const Variable* p : decl.parameters()) { |
Brian Osman | 21f5707 | 2021-01-25 13:51:57 -0500 | [diff] [blame] | 384 | size_t paramSlot = this->getSlot(*p), |
John Stiles | 47b087e | 2021-04-06 13:19:35 -0400 | [diff] [blame] | 385 | nslots = p->type().slotCount(); |
Brian Osman | 5933d4c | 2021-01-05 13:02:20 -0500 | [diff] [blame] | 386 | |
Brian Osman | db2dad5 | 2021-01-07 14:08:30 -0500 | [diff] [blame] | 387 | for (size_t i = 0; i < nslots; ++i) { |
| 388 | fSlots[paramSlot + i] = arguments[argIdx + i]; |
| 389 | } |
| 390 | argIdx += nslots; |
| 391 | } |
| 392 | SkASSERT(argIdx == arguments.size()); |
| 393 | |
| 394 | this->writeStatement(*function.body()); |
| 395 | |
| 396 | // Copy 'out' and 'inout' parameters back to their caller-supplied argument storage |
| 397 | argIdx = 0; |
| 398 | for (const Variable* p : decl.parameters()) { |
John Stiles | 47b087e | 2021-04-06 13:19:35 -0400 | [diff] [blame] | 399 | size_t nslots = p->type().slotCount(); |
Brian Osman | db2dad5 | 2021-01-07 14:08:30 -0500 | [diff] [blame] | 400 | |
Brian Osman | 5933d4c | 2021-01-05 13:02:20 -0500 | [diff] [blame] | 401 | if (p->modifiers().fFlags & Modifiers::kOut_Flag) { |
Brian Osman | 21f5707 | 2021-01-25 13:51:57 -0500 | [diff] [blame] | 402 | size_t paramSlot = this->getSlot(*p); |
Brian Osman | 5933d4c | 2021-01-05 13:02:20 -0500 | [diff] [blame] | 403 | for (size_t i = 0; i < nslots; ++i) { |
Brian Osman | db2dad5 | 2021-01-07 14:08:30 -0500 | [diff] [blame] | 404 | arguments[argIdx + i] = fSlots[paramSlot + i]; |
Brian Osman | 5933d4c | 2021-01-05 13:02:20 -0500 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | argIdx += nslots; |
| 408 | } |
Brian Osman | db2dad5 | 2021-01-07 14:08:30 -0500 | [diff] [blame] | 409 | SkASSERT(argIdx == arguments.size()); |
Brian Osman | 54515b7 | 2021-01-07 14:38:08 -0500 | [diff] [blame] | 410 | |
| 411 | fFunctionStack.pop_back(); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 412 | } |
| 413 | |
Brian Osman | 21f5707 | 2021-01-25 13:51:57 -0500 | [diff] [blame] | 414 | size_t SkVMGenerator::getSlot(const Variable& v) { |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 415 | auto entry = fVariableMap.find(&v); |
| 416 | if (entry != fVariableMap.end()) { |
| 417 | return entry->second; |
| 418 | } |
| 419 | |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 420 | size_t slot = fSlots.size(), |
John Stiles | 47b087e | 2021-04-06 13:19:35 -0400 | [diff] [blame] | 421 | nslots = v.type().slotCount(); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 422 | fSlots.resize(slot + nslots, fBuilder->splat(0.0f).id); |
| 423 | fVariableMap[&v] = slot; |
| 424 | return slot; |
| 425 | } |
| 426 | |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 427 | Value SkVMGenerator::writeBinaryExpression(const BinaryExpression& b) { |
| 428 | const Expression& left = *b.left(); |
| 429 | const Expression& right = *b.right(); |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 430 | Operator op = b.getOperator(); |
| 431 | if (op.kind() == Token::Kind::TK_EQ) { |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 432 | return this->writeStore(left, this->writeExpression(right)); |
| 433 | } |
| 434 | |
| 435 | const Type& lType = left.type(); |
| 436 | const Type& rType = right.type(); |
| 437 | bool lVecOrMtx = (lType.isVector() || lType.isMatrix()); |
| 438 | bool rVecOrMtx = (rType.isVector() || rType.isMatrix()); |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 439 | bool isAssignment = op.isAssignment(); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 440 | if (isAssignment) { |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 441 | op = op.removeAssignment(); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 442 | } |
| 443 | Type::NumberKind nk = base_number_kind(lType); |
| 444 | |
| 445 | // A few ops require special treatment: |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 446 | switch (op.kind()) { |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 447 | case Token::Kind::TK_LOGICALAND: { |
| 448 | SkASSERT(!isAssignment); |
| 449 | SkASSERT(nk == Type::NumberKind::kBoolean); |
| 450 | skvm::I32 lVal = i32(this->writeExpression(left)); |
Brian Osman | 9333c87 | 2021-01-13 15:06:17 -0500 | [diff] [blame] | 451 | ScopedCondition shortCircuit(this, lVal); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 452 | skvm::I32 rVal = i32(this->writeExpression(right)); |
| 453 | return lVal & rVal; |
| 454 | } |
| 455 | case Token::Kind::TK_LOGICALOR: { |
| 456 | SkASSERT(!isAssignment); |
| 457 | SkASSERT(nk == Type::NumberKind::kBoolean); |
| 458 | skvm::I32 lVal = i32(this->writeExpression(left)); |
Brian Osman | 9333c87 | 2021-01-13 15:06:17 -0500 | [diff] [blame] | 459 | ScopedCondition shortCircuit(this, ~lVal); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 460 | skvm::I32 rVal = i32(this->writeExpression(right)); |
| 461 | return lVal | rVal; |
| 462 | } |
John Stiles | 94e72b9 | 2021-01-30 11:06:18 -0500 | [diff] [blame] | 463 | case Token::Kind::TK_COMMA: |
| 464 | // We write the left side of the expression to preserve its side effects, even though we |
| 465 | // immediately discard the result. |
| 466 | this->writeExpression(left); |
| 467 | return this->writeExpression(right); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 468 | default: |
| 469 | break; |
| 470 | } |
| 471 | |
| 472 | // All of the other ops always evaluate both sides of the expression |
| 473 | Value lVal = this->writeExpression(left), |
| 474 | rVal = this->writeExpression(right); |
| 475 | |
| 476 | // Special case for M*V, V*M, M*M (but not V*V!) |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 477 | if (op.kind() == Token::Kind::TK_STAR |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 478 | && lVecOrMtx && rVecOrMtx && !(lType.isVector() && rType.isVector())) { |
| 479 | int rCols = rType.columns(), |
| 480 | rRows = rType.rows(), |
| 481 | lCols = lType.columns(), |
| 482 | lRows = lType.rows(); |
| 483 | // M*V treats the vector as a column |
| 484 | if (rType.isVector()) { |
| 485 | std::swap(rCols, rRows); |
| 486 | } |
| 487 | SkASSERT(lCols == rRows); |
John Stiles | 47b087e | 2021-04-06 13:19:35 -0400 | [diff] [blame] | 488 | SkASSERT(b.type().slotCount() == static_cast<size_t>(lRows * rCols)); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 489 | Value result(lRows * rCols); |
| 490 | size_t resultIdx = 0; |
John Stiles | 727adfe | 2021-09-14 14:07:23 -0400 | [diff] [blame] | 491 | const skvm::F32 zero = fBuilder->splat(0.0f); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 492 | for (int c = 0; c < rCols; ++c) |
| 493 | for (int r = 0; r < lRows; ++r) { |
John Stiles | 727adfe | 2021-09-14 14:07:23 -0400 | [diff] [blame] | 494 | skvm::F32 sum = zero; |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 495 | for (int j = 0; j < lCols; ++j) { |
| 496 | sum += f32(lVal[j*lRows + r]) * f32(rVal[c*rRows + j]); |
| 497 | } |
| 498 | result[resultIdx++] = sum; |
| 499 | } |
| 500 | SkASSERT(resultIdx == result.slots()); |
| 501 | return isAssignment ? this->writeStore(left, result) : result; |
| 502 | } |
| 503 | |
| 504 | size_t nslots = std::max(lVal.slots(), rVal.slots()); |
| 505 | |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 506 | auto binary = [&](auto&& f_fn, auto&& i_fn) { |
| 507 | Value result(nslots); |
| 508 | for (size_t i = 0; i < nslots; ++i) { |
| 509 | // If one side is scalar, replicate it to all channels |
| 510 | skvm::Val L = lVal.slots() == 1 ? lVal[0] : lVal[i], |
| 511 | R = rVal.slots() == 1 ? rVal[0] : rVal[i]; |
| 512 | if (nk == Type::NumberKind::kFloat) { |
| 513 | result[i] = f_fn(f32(L), f32(R)); |
| 514 | } else { |
| 515 | result[i] = i_fn(i32(L), i32(R)); |
| 516 | } |
| 517 | } |
| 518 | return isAssignment ? this->writeStore(left, result) : result; |
| 519 | }; |
| 520 | |
| 521 | auto unsupported_f = [&](skvm::F32, skvm::F32) { |
| 522 | SkDEBUGFAIL("Unsupported operator"); |
| 523 | return skvm::F32{}; |
| 524 | }; |
| 525 | |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 526 | switch (op.kind()) { |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 527 | case Token::Kind::TK_EQEQ: { |
| 528 | SkASSERT(!isAssignment); |
| 529 | Value cmp = binary([](skvm::F32 x, skvm::F32 y) { return x == y; }, |
| 530 | [](skvm::I32 x, skvm::I32 y) { return x == y; }); |
| 531 | skvm::I32 folded = i32(cmp[0]); |
| 532 | for (size_t i = 1; i < nslots; ++i) { |
| 533 | folded &= i32(cmp[i]); |
| 534 | } |
| 535 | return folded; |
| 536 | } |
| 537 | case Token::Kind::TK_NEQ: { |
| 538 | SkASSERT(!isAssignment); |
| 539 | Value cmp = binary([](skvm::F32 x, skvm::F32 y) { return x != y; }, |
| 540 | [](skvm::I32 x, skvm::I32 y) { return x != y; }); |
| 541 | skvm::I32 folded = i32(cmp[0]); |
| 542 | for (size_t i = 1; i < nslots; ++i) { |
| 543 | folded |= i32(cmp[i]); |
| 544 | } |
| 545 | return folded; |
| 546 | } |
| 547 | case Token::Kind::TK_GT: |
| 548 | return binary([](skvm::F32 x, skvm::F32 y) { return x > y; }, |
| 549 | [](skvm::I32 x, skvm::I32 y) { return x > y; }); |
| 550 | case Token::Kind::TK_GTEQ: |
| 551 | return binary([](skvm::F32 x, skvm::F32 y) { return x >= y; }, |
| 552 | [](skvm::I32 x, skvm::I32 y) { return x >= y; }); |
| 553 | case Token::Kind::TK_LT: |
| 554 | return binary([](skvm::F32 x, skvm::F32 y) { return x < y; }, |
| 555 | [](skvm::I32 x, skvm::I32 y) { return x < y; }); |
| 556 | case Token::Kind::TK_LTEQ: |
| 557 | return binary([](skvm::F32 x, skvm::F32 y) { return x <= y; }, |
| 558 | [](skvm::I32 x, skvm::I32 y) { return x <= y; }); |
| 559 | |
| 560 | case Token::Kind::TK_PLUS: |
| 561 | return binary([](skvm::F32 x, skvm::F32 y) { return x + y; }, |
| 562 | [](skvm::I32 x, skvm::I32 y) { return x + y; }); |
| 563 | case Token::Kind::TK_MINUS: |
| 564 | return binary([](skvm::F32 x, skvm::F32 y) { return x - y; }, |
| 565 | [](skvm::I32 x, skvm::I32 y) { return x - y; }); |
| 566 | case Token::Kind::TK_STAR: |
Mike Klein | ff4decc | 2021-02-10 16:13:35 -0600 | [diff] [blame] | 567 | return binary([](skvm::F32 x, skvm::F32 y) { return x ** y; }, |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 568 | [](skvm::I32 x, skvm::I32 y) { return x * y; }); |
| 569 | case Token::Kind::TK_SLASH: |
| 570 | // Minimum spec (GLSL ES 1.0) has very loose requirements for integer operations. |
| 571 | // (Low-end GPUs may not have integer ALUs). Given that, we are allowed to do floating |
| 572 | // point division plus rounding. Section 10.28 of the spec even clarifies that the |
| 573 | // rounding mode is undefined (but round-towards-zero is the obvious/common choice). |
| 574 | return binary([](skvm::F32 x, skvm::F32 y) { return x / y; }, |
| 575 | [](skvm::I32 x, skvm::I32 y) { |
| 576 | return skvm::trunc(skvm::to_F32(x) / skvm::to_F32(y)); |
| 577 | }); |
| 578 | |
| 579 | case Token::Kind::TK_BITWISEXOR: |
| 580 | case Token::Kind::TK_LOGICALXOR: |
| 581 | return binary(unsupported_f, [](skvm::I32 x, skvm::I32 y) { return x ^ y; }); |
| 582 | case Token::Kind::TK_BITWISEAND: |
| 583 | return binary(unsupported_f, [](skvm::I32 x, skvm::I32 y) { return x & y; }); |
| 584 | case Token::Kind::TK_BITWISEOR: |
| 585 | return binary(unsupported_f, [](skvm::I32 x, skvm::I32 y) { return x | y; }); |
| 586 | |
| 587 | // These three operators are all 'reserved' (illegal) in our minimum spec, but will require |
| 588 | // implementation in the future. |
| 589 | case Token::Kind::TK_PERCENT: |
| 590 | case Token::Kind::TK_SHL: |
| 591 | case Token::Kind::TK_SHR: |
| 592 | default: |
| 593 | SkDEBUGFAIL("Unsupported operator"); |
| 594 | return {}; |
| 595 | } |
| 596 | } |
| 597 | |
John Stiles | d986f47 | 2021-04-06 15:54:43 -0400 | [diff] [blame] | 598 | Value SkVMGenerator::writeAggregationConstructor(const AnyConstructor& c) { |
John Stiles | 47b087e | 2021-04-06 13:19:35 -0400 | [diff] [blame] | 599 | Value result(c.type().slotCount()); |
John Stiles | 626b62e | 2021-03-31 22:06:07 -0400 | [diff] [blame] | 600 | size_t resultIdx = 0; |
John Stiles | d986f47 | 2021-04-06 15:54:43 -0400 | [diff] [blame] | 601 | for (const auto &arg : c.argumentSpan()) { |
John Stiles | 626b62e | 2021-03-31 22:06:07 -0400 | [diff] [blame] | 602 | Value tmp = this->writeExpression(*arg); |
| 603 | for (size_t tmpSlot = 0; tmpSlot < tmp.slots(); ++tmpSlot) { |
| 604 | result[resultIdx++] = tmp[tmpSlot]; |
| 605 | } |
| 606 | } |
| 607 | return result; |
| 608 | } |
| 609 | |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 610 | Value SkVMGenerator::writeTypeConversion(const Value& src, |
| 611 | Type::NumberKind srcKind, |
| 612 | Type::NumberKind dstKind) { |
| 613 | // Conversion among "similar" types (floatN <-> halfN), (shortN <-> intN), etc. is a no-op. |
| 614 | if (srcKind == dstKind) { |
| 615 | return src; |
| 616 | } |
| 617 | |
| 618 | // TODO: Handle signed vs. unsigned. GLSL ES 1.0 only has 'int', so no problem yet. |
| 619 | Value dst(src.slots()); |
| 620 | switch (dstKind) { |
| 621 | case Type::NumberKind::kFloat: |
| 622 | if (srcKind == Type::NumberKind::kSigned) { |
| 623 | // int -> float |
| 624 | for (size_t i = 0; i < src.slots(); ++i) { |
| 625 | dst[i] = skvm::to_F32(i32(src[i])); |
| 626 | } |
| 627 | return dst; |
| 628 | } |
| 629 | if (srcKind == Type::NumberKind::kBoolean) { |
| 630 | // bool -> float |
| 631 | for (size_t i = 0; i < src.slots(); ++i) { |
| 632 | dst[i] = skvm::select(i32(src[i]), 1.0f, 0.0f); |
| 633 | } |
| 634 | return dst; |
| 635 | } |
| 636 | break; |
| 637 | |
| 638 | case Type::NumberKind::kSigned: |
| 639 | if (srcKind == Type::NumberKind::kFloat) { |
| 640 | // float -> int |
| 641 | for (size_t i = 0; i < src.slots(); ++i) { |
| 642 | dst[i] = skvm::trunc(f32(src[i])); |
| 643 | } |
| 644 | return dst; |
| 645 | } |
| 646 | if (srcKind == Type::NumberKind::kBoolean) { |
| 647 | // bool -> int |
| 648 | for (size_t i = 0; i < src.slots(); ++i) { |
| 649 | dst[i] = skvm::select(i32(src[i]), 1, 0); |
| 650 | } |
| 651 | return dst; |
| 652 | } |
| 653 | break; |
| 654 | |
| 655 | case Type::NumberKind::kBoolean: |
| 656 | if (srcKind == Type::NumberKind::kSigned) { |
| 657 | // int -> bool |
| 658 | for (size_t i = 0; i < src.slots(); ++i) { |
| 659 | dst[i] = i32(src[i]) != 0; |
| 660 | } |
| 661 | return dst; |
| 662 | } |
| 663 | if (srcKind == Type::NumberKind::kFloat) { |
| 664 | // float -> bool |
| 665 | for (size_t i = 0; i < src.slots(); ++i) { |
| 666 | dst[i] = f32(src[i]) != 0.0; |
| 667 | } |
| 668 | return dst; |
| 669 | } |
| 670 | break; |
| 671 | |
| 672 | default: |
| 673 | break; |
| 674 | } |
John Stiles | 7bf7999 | 2021-06-25 11:05:20 -0400 | [diff] [blame] | 675 | SkDEBUGFAILF("Unsupported type conversion: %d -> %d", (int)srcKind, (int)dstKind); |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 676 | return {}; |
| 677 | } |
| 678 | |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 679 | Value SkVMGenerator::writeConstructorCast(const AnyConstructor& c) { |
| 680 | auto arguments = c.argumentSpan(); |
| 681 | SkASSERT(arguments.size() == 1); |
| 682 | const Expression& argument = *arguments.front(); |
| 683 | |
| 684 | const Type& srcType = argument.type(); |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 685 | const Type& dstType = c.type(); |
| 686 | Type::NumberKind srcKind = base_number_kind(srcType); |
| 687 | Type::NumberKind dstKind = base_number_kind(dstType); |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 688 | Value src = this->writeExpression(argument); |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 689 | return this->writeTypeConversion(src, srcKind, dstKind); |
| 690 | } |
| 691 | |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 692 | Value SkVMGenerator::writeConstructorSplat(const ConstructorSplat& c) { |
| 693 | SkASSERT(c.type().isVector()); |
| 694 | SkASSERT(c.argument()->type().isScalar()); |
| 695 | int columns = c.type().columns(); |
| 696 | |
| 697 | // Splat the argument across all components of a vector. |
| 698 | Value src = this->writeExpression(*c.argument()); |
| 699 | Value dst(columns); |
| 700 | for (int i = 0; i < columns; ++i) { |
| 701 | dst[i] = src[0]; |
| 702 | } |
| 703 | return dst; |
| 704 | } |
| 705 | |
John Stiles | 68f5606 | 2021-08-03 12:31:56 -0400 | [diff] [blame] | 706 | Value SkVMGenerator::writeConstructorDiagonalMatrix(const ConstructorDiagonalMatrix& ctor) { |
| 707 | const Type& dstType = ctor.type(); |
John Stiles | e118278 | 2021-03-30 22:09:37 -0400 | [diff] [blame] | 708 | SkASSERT(dstType.isMatrix()); |
John Stiles | 68f5606 | 2021-08-03 12:31:56 -0400 | [diff] [blame] | 709 | SkASSERT(ctor.argument()->type() == dstType.componentType()); |
John Stiles | e118278 | 2021-03-30 22:09:37 -0400 | [diff] [blame] | 710 | |
John Stiles | 68f5606 | 2021-08-03 12:31:56 -0400 | [diff] [blame] | 711 | Value src = this->writeExpression(*ctor.argument()); |
John Stiles | e118278 | 2021-03-30 22:09:37 -0400 | [diff] [blame] | 712 | Value dst(dstType.rows() * dstType.columns()); |
| 713 | size_t dstIndex = 0; |
| 714 | |
| 715 | // Matrix-from-scalar builds a diagonal scale matrix |
John Stiles | 727adfe | 2021-09-14 14:07:23 -0400 | [diff] [blame] | 716 | const skvm::F32 zero = fBuilder->splat(0.0f); |
John Stiles | e118278 | 2021-03-30 22:09:37 -0400 | [diff] [blame] | 717 | for (int c = 0; c < dstType.columns(); ++c) { |
| 718 | for (int r = 0; r < dstType.rows(); ++r) { |
John Stiles | 727adfe | 2021-09-14 14:07:23 -0400 | [diff] [blame] | 719 | dst[dstIndex++] = (c == r ? f32(src) : zero); |
John Stiles | e118278 | 2021-03-30 22:09:37 -0400 | [diff] [blame] | 720 | } |
| 721 | } |
| 722 | |
| 723 | SkASSERT(dstIndex == dst.slots()); |
| 724 | return dst; |
| 725 | } |
| 726 | |
John Stiles | 68f5606 | 2021-08-03 12:31:56 -0400 | [diff] [blame] | 727 | Value SkVMGenerator::writeConstructorMatrixResize(const ConstructorMatrixResize& ctor) { |
| 728 | const Type& srcType = ctor.argument()->type(); |
| 729 | const Type& dstType = ctor.type(); |
| 730 | Value src = this->writeExpression(*ctor.argument()); |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 731 | Value dst(dstType.rows() * dstType.columns()); |
| 732 | |
| 733 | // Matrix-from-matrix uses src where it overlaps, and fills in missing fields with identity. |
| 734 | size_t dstIndex = 0; |
| 735 | for (int c = 0; c < dstType.columns(); ++c) { |
| 736 | for (int r = 0; r < dstType.rows(); ++r) { |
| 737 | if (c < srcType.columns() && r < srcType.rows()) { |
| 738 | dst[dstIndex++] = src[c * srcType.rows() + r]; |
| 739 | } else { |
| 740 | dst[dstIndex++] = fBuilder->splat(c == r ? 1.0f : 0.0f); |
| 741 | } |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | SkASSERT(dstIndex == dst.slots()); |
| 746 | return dst; |
| 747 | } |
| 748 | |
Brian Osman | fa71ffa | 2021-01-26 14:05:31 -0500 | [diff] [blame] | 749 | size_t SkVMGenerator::fieldSlotOffset(const FieldAccess& expr) { |
Brian Osman | 21f5707 | 2021-01-25 13:51:57 -0500 | [diff] [blame] | 750 | size_t offset = 0; |
Brian Osman | fa71ffa | 2021-01-26 14:05:31 -0500 | [diff] [blame] | 751 | for (int i = 0; i < expr.fieldIndex(); ++i) { |
John Stiles | 47b087e | 2021-04-06 13:19:35 -0400 | [diff] [blame] | 752 | offset += (*expr.base()->type().fields()[i].fType).slotCount(); |
Brian Osman | fa71ffa | 2021-01-26 14:05:31 -0500 | [diff] [blame] | 753 | } |
| 754 | return offset; |
| 755 | } |
| 756 | |
| 757 | Value SkVMGenerator::writeFieldAccess(const FieldAccess& expr) { |
| 758 | Value base = this->writeExpression(*expr.base()); |
John Stiles | 47b087e | 2021-04-06 13:19:35 -0400 | [diff] [blame] | 759 | Value field(expr.type().slotCount()); |
Brian Osman | fa71ffa | 2021-01-26 14:05:31 -0500 | [diff] [blame] | 760 | size_t offset = this->fieldSlotOffset(expr); |
| 761 | for (size_t i = 0; i < field.slots(); ++i) { |
| 762 | field[i] = base[offset + i]; |
| 763 | } |
| 764 | return field; |
| 765 | } |
| 766 | |
| 767 | size_t SkVMGenerator::indexSlotOffset(const IndexExpression& expr) { |
| 768 | Value index = this->writeExpression(*expr.index()); |
| 769 | int indexValue = -1; |
| 770 | SkAssertResult(fBuilder->allImm(index[0], &indexValue)); |
| 771 | |
| 772 | // When indexing by a literal, the front-end guarantees that we don't go out of bounds. |
| 773 | // But when indexing by a loop variable, it's possible to generate out-of-bounds access. |
| 774 | // The GLSL spec leaves that behavior undefined - we'll just clamp everything here. |
| 775 | indexValue = SkTPin(indexValue, 0, expr.base()->type().columns() - 1); |
| 776 | |
John Stiles | 47b087e | 2021-04-06 13:19:35 -0400 | [diff] [blame] | 777 | size_t stride = expr.type().slotCount(); |
Brian Osman | fa71ffa | 2021-01-26 14:05:31 -0500 | [diff] [blame] | 778 | return indexValue * stride; |
| 779 | } |
| 780 | |
| 781 | Value SkVMGenerator::writeIndexExpression(const IndexExpression& expr) { |
| 782 | Value base = this->writeExpression(*expr.base()); |
John Stiles | 47b087e | 2021-04-06 13:19:35 -0400 | [diff] [blame] | 783 | Value element(expr.type().slotCount()); |
Brian Osman | fa71ffa | 2021-01-26 14:05:31 -0500 | [diff] [blame] | 784 | size_t offset = this->indexSlotOffset(expr); |
| 785 | for (size_t i = 0; i < element.slots(); ++i) { |
| 786 | element[i] = base[offset + i]; |
| 787 | } |
| 788 | return element; |
| 789 | } |
| 790 | |
| 791 | Value SkVMGenerator::writeVariableExpression(const VariableReference& expr) { |
Brian Osman | 21f5707 | 2021-01-25 13:51:57 -0500 | [diff] [blame] | 792 | size_t slot = this->getSlot(*expr.variable()); |
John Stiles | 47b087e | 2021-04-06 13:19:35 -0400 | [diff] [blame] | 793 | Value val(expr.type().slotCount()); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 794 | for (size_t i = 0; i < val.slots(); ++i) { |
| 795 | val[i] = fSlots[slot + i]; |
| 796 | } |
| 797 | return val; |
| 798 | } |
| 799 | |
| 800 | Value SkVMGenerator::writeMatrixInverse2x2(const Value& m) { |
| 801 | SkASSERT(m.slots() == 4); |
| 802 | skvm::F32 a = f32(m[0]), |
| 803 | b = f32(m[1]), |
| 804 | c = f32(m[2]), |
| 805 | d = f32(m[3]); |
| 806 | skvm::F32 idet = 1.0f / (a*d - b*c); |
| 807 | |
| 808 | Value result(m.slots()); |
Mike Klein | ff4decc | 2021-02-10 16:13:35 -0600 | [diff] [blame] | 809 | result[0] = ( d ** idet); |
| 810 | result[1] = (-b ** idet); |
| 811 | result[2] = (-c ** idet); |
| 812 | result[3] = ( a ** idet); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 813 | return result; |
| 814 | } |
| 815 | |
| 816 | Value SkVMGenerator::writeMatrixInverse3x3(const Value& m) { |
| 817 | SkASSERT(m.slots() == 9); |
| 818 | skvm::F32 a11 = f32(m[0]), a12 = f32(m[3]), a13 = f32(m[6]), |
| 819 | a21 = f32(m[1]), a22 = f32(m[4]), a23 = f32(m[7]), |
| 820 | a31 = f32(m[2]), a32 = f32(m[5]), a33 = f32(m[8]); |
| 821 | skvm::F32 idet = 1.0f / (a11*a22*a33 + a12*a23*a31 + a13*a21*a32 - |
| 822 | a11*a23*a32 - a12*a21*a33 - a13*a22*a31); |
| 823 | |
| 824 | Value result(m.slots()); |
Mike Klein | ff4decc | 2021-02-10 16:13:35 -0600 | [diff] [blame] | 825 | result[0] = ((a22**a33 - a23**a32) ** idet); |
| 826 | result[1] = ((a23**a31 - a21**a33) ** idet); |
| 827 | result[2] = ((a21**a32 - a22**a31) ** idet); |
| 828 | result[3] = ((a13**a32 - a12**a33) ** idet); |
| 829 | result[4] = ((a11**a33 - a13**a31) ** idet); |
| 830 | result[5] = ((a12**a31 - a11**a32) ** idet); |
| 831 | result[6] = ((a12**a23 - a13**a22) ** idet); |
| 832 | result[7] = ((a13**a21 - a11**a23) ** idet); |
| 833 | result[8] = ((a11**a22 - a12**a21) ** idet); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 834 | return result; |
| 835 | } |
| 836 | |
| 837 | Value SkVMGenerator::writeMatrixInverse4x4(const Value& m) { |
| 838 | SkASSERT(m.slots() == 16); |
| 839 | skvm::F32 a00 = f32(m[0]), a10 = f32(m[4]), a20 = f32(m[ 8]), a30 = f32(m[12]), |
| 840 | a01 = f32(m[1]), a11 = f32(m[5]), a21 = f32(m[ 9]), a31 = f32(m[13]), |
| 841 | a02 = f32(m[2]), a12 = f32(m[6]), a22 = f32(m[10]), a32 = f32(m[14]), |
| 842 | a03 = f32(m[3]), a13 = f32(m[7]), a23 = f32(m[11]), a33 = f32(m[15]); |
| 843 | |
Mike Klein | ff4decc | 2021-02-10 16:13:35 -0600 | [diff] [blame] | 844 | skvm::F32 b00 = a00**a11 - a01**a10, |
| 845 | b01 = a00**a12 - a02**a10, |
| 846 | b02 = a00**a13 - a03**a10, |
| 847 | b03 = a01**a12 - a02**a11, |
| 848 | b04 = a01**a13 - a03**a11, |
| 849 | b05 = a02**a13 - a03**a12, |
| 850 | b06 = a20**a31 - a21**a30, |
| 851 | b07 = a20**a32 - a22**a30, |
| 852 | b08 = a20**a33 - a23**a30, |
| 853 | b09 = a21**a32 - a22**a31, |
| 854 | b10 = a21**a33 - a23**a31, |
| 855 | b11 = a22**a33 - a23**a32; |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 856 | |
Mike Klein | ff4decc | 2021-02-10 16:13:35 -0600 | [diff] [blame] | 857 | skvm::F32 idet = 1.0f / (b00**b11 - b01**b10 + b02**b09 + b03**b08 - b04**b07 + b05**b06); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 858 | |
| 859 | b00 *= idet; |
| 860 | b01 *= idet; |
| 861 | b02 *= idet; |
| 862 | b03 *= idet; |
| 863 | b04 *= idet; |
| 864 | b05 *= idet; |
| 865 | b06 *= idet; |
| 866 | b07 *= idet; |
| 867 | b08 *= idet; |
| 868 | b09 *= idet; |
| 869 | b10 *= idet; |
| 870 | b11 *= idet; |
| 871 | |
| 872 | Value result(m.slots()); |
| 873 | result[ 0] = (a11*b11 - a12*b10 + a13*b09); |
| 874 | result[ 1] = (a02*b10 - a01*b11 - a03*b09); |
| 875 | result[ 2] = (a31*b05 - a32*b04 + a33*b03); |
| 876 | result[ 3] = (a22*b04 - a21*b05 - a23*b03); |
| 877 | result[ 4] = (a12*b08 - a10*b11 - a13*b07); |
| 878 | result[ 5] = (a00*b11 - a02*b08 + a03*b07); |
| 879 | result[ 6] = (a32*b02 - a30*b05 - a33*b01); |
| 880 | result[ 7] = (a20*b05 - a22*b02 + a23*b01); |
| 881 | result[ 8] = (a10*b10 - a11*b08 + a13*b06); |
| 882 | result[ 9] = (a01*b08 - a00*b10 - a03*b06); |
| 883 | result[10] = (a30*b04 - a31*b02 + a33*b00); |
| 884 | result[11] = (a21*b02 - a20*b04 - a23*b00); |
| 885 | result[12] = (a11*b07 - a10*b09 - a12*b06); |
| 886 | result[13] = (a00*b09 - a01*b07 + a02*b06); |
| 887 | result[14] = (a31*b01 - a30*b03 - a32*b00); |
| 888 | result[15] = (a20*b03 - a21*b01 + a22*b00); |
| 889 | return result; |
| 890 | } |
| 891 | |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 892 | Value SkVMGenerator::writeChildCall(const ChildCall& c) { |
| 893 | auto child_it = fVariableMap.find(&c.child()); |
| 894 | SkASSERT(child_it != fVariableMap.end()); |
| 895 | |
| 896 | const Expression* arg = c.arguments()[0].get(); |
| 897 | Value argVal = this->writeExpression(*arg); |
| 898 | skvm::Color color; |
| 899 | |
| 900 | switch (c.child().type().typeKind()) { |
| 901 | case Type::TypeKind::kShader: { |
| 902 | SkASSERT(c.arguments().size() == 1); |
| 903 | SkASSERT(arg->type() == *fProgram.fContext->fTypes.fFloat2); |
| 904 | skvm::Coord coord = {f32(argVal[0]), f32(argVal[1])}; |
| 905 | color = fSampleShader(child_it->second, coord); |
| 906 | break; |
| 907 | } |
| 908 | case Type::TypeKind::kColorFilter: { |
| 909 | SkASSERT(c.arguments().size() == 1); |
| 910 | SkASSERT(arg->type() == *fProgram.fContext->fTypes.fHalf4 || |
| 911 | arg->type() == *fProgram.fContext->fTypes.fFloat4); |
| 912 | skvm::Color inColor = {f32(argVal[0]), f32(argVal[1]), f32(argVal[2]), f32(argVal[3])}; |
| 913 | color = fSampleColorFilter(child_it->second, inColor); |
| 914 | break; |
| 915 | } |
| 916 | case Type::TypeKind::kBlender: { |
| 917 | SkASSERT(c.arguments().size() == 2); |
| 918 | SkASSERT(arg->type() == *fProgram.fContext->fTypes.fHalf4 || |
| 919 | arg->type() == *fProgram.fContext->fTypes.fFloat4); |
| 920 | skvm::Color srcColor = {f32(argVal[0]), f32(argVal[1]), f32(argVal[2]), f32(argVal[3])}; |
| 921 | |
| 922 | arg = c.arguments()[1].get(); |
| 923 | argVal = this->writeExpression(*arg); |
| 924 | SkASSERT(arg->type() == *fProgram.fContext->fTypes.fHalf4 || |
| 925 | arg->type() == *fProgram.fContext->fTypes.fFloat4); |
| 926 | skvm::Color dstColor = {f32(argVal[0]), f32(argVal[1]), f32(argVal[2]), f32(argVal[3])}; |
| 927 | |
| 928 | color = fSampleBlender(child_it->second, srcColor, dstColor); |
| 929 | break; |
| 930 | } |
| 931 | default: { |
| 932 | SkDEBUGFAILF("cannot sample from type '%s'", c.child().type().description().c_str()); |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | Value result(4); |
| 937 | result[0] = color.r; |
| 938 | result[1] = color.g; |
| 939 | result[2] = color.b; |
| 940 | result[3] = color.a; |
| 941 | return result; |
| 942 | } |
| 943 | |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 944 | Value SkVMGenerator::writeIntrinsicCall(const FunctionCall& c) { |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 945 | IntrinsicKind intrinsicKind = c.function().intrinsicKind(); |
| 946 | SkASSERT(intrinsicKind != kNotIntrinsic); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 947 | |
| 948 | const size_t nargs = c.arguments().size(); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 949 | const size_t kMaxArgs = 3; // eg: clamp, mix, smoothstep |
| 950 | Value args[kMaxArgs]; |
| 951 | SkASSERT(nargs >= 1 && nargs <= SK_ARRAY_COUNT(args)); |
| 952 | |
| 953 | // All other intrinsics have at most three args, and those can all be evaluated up front: |
| 954 | for (size_t i = 0; i < nargs; ++i) { |
| 955 | args[i] = this->writeExpression(*c.arguments()[i]); |
| 956 | } |
| 957 | Type::NumberKind nk = base_number_kind(c.arguments()[0]->type()); |
| 958 | |
| 959 | auto binary = [&](auto&& fn) { |
| 960 | // Binary intrinsics are (vecN, vecN), (vecN, float), or (float, vecN) |
| 961 | size_t nslots = std::max(args[0].slots(), args[1].slots()); |
| 962 | Value result(nslots); |
| 963 | SkASSERT(args[0].slots() == nslots || args[0].slots() == 1); |
| 964 | SkASSERT(args[1].slots() == nslots || args[1].slots() == 1); |
| 965 | |
| 966 | for (size_t i = 0; i < nslots; ++i) { |
| 967 | result[i] = fn({fBuilder, args[0][args[0].slots() == 1 ? 0 : i]}, |
| 968 | {fBuilder, args[1][args[1].slots() == 1 ? 0 : i]}); |
| 969 | } |
| 970 | return result; |
| 971 | }; |
| 972 | |
| 973 | auto ternary = [&](auto&& fn) { |
| 974 | // Ternary intrinsics are some combination of vecN and float |
| 975 | size_t nslots = std::max({args[0].slots(), args[1].slots(), args[2].slots()}); |
| 976 | Value result(nslots); |
| 977 | SkASSERT(args[0].slots() == nslots || args[0].slots() == 1); |
| 978 | SkASSERT(args[1].slots() == nslots || args[1].slots() == 1); |
| 979 | SkASSERT(args[2].slots() == nslots || args[2].slots() == 1); |
| 980 | |
| 981 | for (size_t i = 0; i < nslots; ++i) { |
| 982 | result[i] = fn({fBuilder, args[0][args[0].slots() == 1 ? 0 : i]}, |
| 983 | {fBuilder, args[1][args[1].slots() == 1 ? 0 : i]}, |
| 984 | {fBuilder, args[2][args[2].slots() == 1 ? 0 : i]}); |
| 985 | } |
| 986 | return result; |
| 987 | }; |
| 988 | |
| 989 | auto dot = [&](const Value& x, const Value& y) { |
| 990 | SkASSERT(x.slots() == y.slots()); |
| 991 | skvm::F32 result = f32(x[0]) * f32(y[0]); |
| 992 | for (size_t i = 1; i < x.slots(); ++i) { |
| 993 | result += f32(x[i]) * f32(y[i]); |
| 994 | } |
| 995 | return result; |
| 996 | }; |
| 997 | |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 998 | switch (intrinsicKind) { |
| 999 | case k_radians_IntrinsicKind: |
Brian Osman | 22cc3be | 2020-12-30 10:38:15 -0500 | [diff] [blame] | 1000 | return unary(args[0], [](skvm::F32 deg) { return deg * (SK_FloatPI / 180); }); |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 1001 | case k_degrees_IntrinsicKind: |
Brian Osman | 22cc3be | 2020-12-30 10:38:15 -0500 | [diff] [blame] | 1002 | return unary(args[0], [](skvm::F32 rad) { return rad * (180 / SK_FloatPI); }); |
| 1003 | |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 1004 | case k_sin_IntrinsicKind: return unary(args[0], skvm::approx_sin); |
| 1005 | case k_cos_IntrinsicKind: return unary(args[0], skvm::approx_cos); |
| 1006 | case k_tan_IntrinsicKind: return unary(args[0], skvm::approx_tan); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1007 | |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 1008 | case k_asin_IntrinsicKind: return unary(args[0], skvm::approx_asin); |
| 1009 | case k_acos_IntrinsicKind: return unary(args[0], skvm::approx_acos); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1010 | |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 1011 | case k_atan_IntrinsicKind: return nargs == 1 ? unary(args[0], skvm::approx_atan) |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1012 | : binary(skvm::approx_atan2); |
| 1013 | |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 1014 | case k_pow_IntrinsicKind: |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1015 | return binary([](skvm::F32 x, skvm::F32 y) { return skvm::approx_powf(x, y); }); |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 1016 | case k_exp_IntrinsicKind: return unary(args[0], skvm::approx_exp); |
| 1017 | case k_log_IntrinsicKind: return unary(args[0], skvm::approx_log); |
| 1018 | case k_exp2_IntrinsicKind: return unary(args[0], skvm::approx_pow2); |
| 1019 | case k_log2_IntrinsicKind: return unary(args[0], skvm::approx_log2); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1020 | |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 1021 | case k_sqrt_IntrinsicKind: return unary(args[0], skvm::sqrt); |
| 1022 | case k_inversesqrt_IntrinsicKind: |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1023 | return unary(args[0], [](skvm::F32 x) { return 1.0f / skvm::sqrt(x); }); |
| 1024 | |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 1025 | case k_abs_IntrinsicKind: return unary(args[0], skvm::abs); |
| 1026 | case k_sign_IntrinsicKind: |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1027 | return unary(args[0], [](skvm::F32 x) { return select(x < 0, -1.0f, |
| 1028 | select(x > 0, +1.0f, 0.0f)); }); |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 1029 | case k_floor_IntrinsicKind: return unary(args[0], skvm::floor); |
| 1030 | case k_ceil_IntrinsicKind: return unary(args[0], skvm::ceil); |
| 1031 | case k_fract_IntrinsicKind: return unary(args[0], skvm::fract); |
| 1032 | case k_mod_IntrinsicKind: |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1033 | return binary([](skvm::F32 x, skvm::F32 y) { return x - y*skvm::floor(x / y); }); |
| 1034 | |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 1035 | case k_min_IntrinsicKind: |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1036 | return binary([](skvm::F32 x, skvm::F32 y) { return skvm::min(x, y); }); |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 1037 | case k_max_IntrinsicKind: |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1038 | return binary([](skvm::F32 x, skvm::F32 y) { return skvm::max(x, y); }); |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 1039 | case k_clamp_IntrinsicKind: |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1040 | return ternary( |
| 1041 | [](skvm::F32 x, skvm::F32 lo, skvm::F32 hi) { return skvm::clamp(x, lo, hi); }); |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 1042 | case k_saturate_IntrinsicKind: |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1043 | return unary(args[0], [](skvm::F32 x) { return skvm::clamp01(x); }); |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 1044 | case k_mix_IntrinsicKind: |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1045 | return ternary( |
| 1046 | [](skvm::F32 x, skvm::F32 y, skvm::F32 t) { return skvm::lerp(x, y, t); }); |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 1047 | case k_step_IntrinsicKind: |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1048 | return binary([](skvm::F32 edge, skvm::F32 x) { return select(x < edge, 0.0f, 1.0f); }); |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 1049 | case k_smoothstep_IntrinsicKind: |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1050 | return ternary([](skvm::F32 edge0, skvm::F32 edge1, skvm::F32 x) { |
| 1051 | skvm::F32 t = skvm::clamp01((x - edge0) / (edge1 - edge0)); |
Mike Klein | ff4decc | 2021-02-10 16:13:35 -0600 | [diff] [blame] | 1052 | return t ** t ** (3 - 2 ** t); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1053 | }); |
| 1054 | |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 1055 | case k_length_IntrinsicKind: return skvm::sqrt(dot(args[0], args[0])); |
| 1056 | case k_distance_IntrinsicKind: { |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1057 | Value vec = binary([](skvm::F32 x, skvm::F32 y) { return x - y; }); |
| 1058 | return skvm::sqrt(dot(vec, vec)); |
| 1059 | } |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 1060 | case k_dot_IntrinsicKind: return dot(args[0], args[1]); |
| 1061 | case k_cross_IntrinsicKind: { |
Brian Osman | 22cc3be | 2020-12-30 10:38:15 -0500 | [diff] [blame] | 1062 | skvm::F32 ax = f32(args[0][0]), ay = f32(args[0][1]), az = f32(args[0][2]), |
| 1063 | bx = f32(args[1][0]), by = f32(args[1][1]), bz = f32(args[1][2]); |
| 1064 | Value result(3); |
Mike Klein | ff4decc | 2021-02-10 16:13:35 -0600 | [diff] [blame] | 1065 | result[0] = ay**bz - az**by; |
| 1066 | result[1] = az**bx - ax**bz; |
| 1067 | result[2] = ax**by - ay**bx; |
Brian Osman | 22cc3be | 2020-12-30 10:38:15 -0500 | [diff] [blame] | 1068 | return result; |
| 1069 | } |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 1070 | case k_normalize_IntrinsicKind: { |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1071 | skvm::F32 invLen = 1.0f / skvm::sqrt(dot(args[0], args[0])); |
Mike Klein | ff4decc | 2021-02-10 16:13:35 -0600 | [diff] [blame] | 1072 | return unary(args[0], [&](skvm::F32 x) { return x ** invLen; }); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1073 | } |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 1074 | case k_faceforward_IntrinsicKind: { |
Brian Osman | 22cc3be | 2020-12-30 10:38:15 -0500 | [diff] [blame] | 1075 | const Value &N = args[0], |
| 1076 | &I = args[1], |
| 1077 | &Nref = args[2]; |
| 1078 | |
| 1079 | skvm::F32 dotNrefI = dot(Nref, I); |
| 1080 | return unary(N, [&](skvm::F32 n) { return select(dotNrefI<0, n, -n); }); |
| 1081 | } |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 1082 | case k_reflect_IntrinsicKind: { |
Brian Osman | 22cc3be | 2020-12-30 10:38:15 -0500 | [diff] [blame] | 1083 | const Value &I = args[0], |
| 1084 | &N = args[1]; |
| 1085 | |
| 1086 | skvm::F32 dotNI = dot(N, I); |
| 1087 | return binary([&](skvm::F32 i, skvm::F32 n) { |
Mike Klein | ff4decc | 2021-02-10 16:13:35 -0600 | [diff] [blame] | 1088 | return i - 2**dotNI**n; |
Brian Osman | 22cc3be | 2020-12-30 10:38:15 -0500 | [diff] [blame] | 1089 | }); |
| 1090 | } |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 1091 | case k_refract_IntrinsicKind: { |
Brian Osman | 22cc3be | 2020-12-30 10:38:15 -0500 | [diff] [blame] | 1092 | const Value &I = args[0], |
| 1093 | &N = args[1]; |
| 1094 | skvm::F32 eta = f32(args[2]); |
| 1095 | |
| 1096 | skvm::F32 dotNI = dot(N, I), |
Mike Klein | ff4decc | 2021-02-10 16:13:35 -0600 | [diff] [blame] | 1097 | k = 1 - eta**eta**(1 - dotNI**dotNI); |
Brian Osman | 22cc3be | 2020-12-30 10:38:15 -0500 | [diff] [blame] | 1098 | return binary([&](skvm::F32 i, skvm::F32 n) { |
Mike Klein | ff4decc | 2021-02-10 16:13:35 -0600 | [diff] [blame] | 1099 | return select(k<0, 0.0f, eta**i - (eta**dotNI + sqrt(k))**n); |
Brian Osman | 22cc3be | 2020-12-30 10:38:15 -0500 | [diff] [blame] | 1100 | }); |
| 1101 | } |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1102 | |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 1103 | case k_matrixCompMult_IntrinsicKind: |
Mike Klein | ff4decc | 2021-02-10 16:13:35 -0600 | [diff] [blame] | 1104 | return binary([](skvm::F32 x, skvm::F32 y) { return x ** y; }); |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 1105 | case k_inverse_IntrinsicKind: { |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1106 | switch (args[0].slots()) { |
| 1107 | case 4: return this->writeMatrixInverse2x2(args[0]); |
| 1108 | case 9: return this->writeMatrixInverse3x3(args[0]); |
| 1109 | case 16: return this->writeMatrixInverse4x4(args[0]); |
| 1110 | default: |
| 1111 | SkDEBUGFAIL("Invalid call to inverse"); |
| 1112 | return {}; |
| 1113 | } |
| 1114 | } |
| 1115 | |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 1116 | case k_lessThan_IntrinsicKind: |
Brian Osman | 30b6729 | 2020-12-23 13:02:09 -0500 | [diff] [blame] | 1117 | return nk == Type::NumberKind::kFloat |
| 1118 | ? binary([](skvm::F32 x, skvm::F32 y) { return x < y; }) |
| 1119 | : binary([](skvm::I32 x, skvm::I32 y) { return x < y; }); |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 1120 | case k_lessThanEqual_IntrinsicKind: |
Brian Osman | 30b6729 | 2020-12-23 13:02:09 -0500 | [diff] [blame] | 1121 | return nk == Type::NumberKind::kFloat |
| 1122 | ? binary([](skvm::F32 x, skvm::F32 y) { return x <= y; }) |
| 1123 | : binary([](skvm::I32 x, skvm::I32 y) { return x <= y; }); |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 1124 | case k_greaterThan_IntrinsicKind: |
Brian Osman | 30b6729 | 2020-12-23 13:02:09 -0500 | [diff] [blame] | 1125 | return nk == Type::NumberKind::kFloat |
| 1126 | ? binary([](skvm::F32 x, skvm::F32 y) { return x > y; }) |
| 1127 | : binary([](skvm::I32 x, skvm::I32 y) { return x > y; }); |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 1128 | case k_greaterThanEqual_IntrinsicKind: |
Brian Osman | 30b6729 | 2020-12-23 13:02:09 -0500 | [diff] [blame] | 1129 | return nk == Type::NumberKind::kFloat |
| 1130 | ? binary([](skvm::F32 x, skvm::F32 y) { return x >= y; }) |
| 1131 | : binary([](skvm::I32 x, skvm::I32 y) { return x >= y; }); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1132 | |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 1133 | case k_equal_IntrinsicKind: |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1134 | return nk == Type::NumberKind::kFloat |
| 1135 | ? binary([](skvm::F32 x, skvm::F32 y) { return x == y; }) |
| 1136 | : binary([](skvm::I32 x, skvm::I32 y) { return x == y; }); |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 1137 | case k_notEqual_IntrinsicKind: |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1138 | return nk == Type::NumberKind::kFloat |
| 1139 | ? binary([](skvm::F32 x, skvm::F32 y) { return x != y; }) |
| 1140 | : binary([](skvm::I32 x, skvm::I32 y) { return x != y; }); |
| 1141 | |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 1142 | case k_any_IntrinsicKind: { |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1143 | skvm::I32 result = i32(args[0][0]); |
| 1144 | for (size_t i = 1; i < args[0].slots(); ++i) { |
| 1145 | result |= i32(args[0][i]); |
| 1146 | } |
| 1147 | return result; |
| 1148 | } |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 1149 | case k_all_IntrinsicKind: { |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1150 | skvm::I32 result = i32(args[0][0]); |
| 1151 | for (size_t i = 1; i < args[0].slots(); ++i) { |
| 1152 | result &= i32(args[0][i]); |
| 1153 | } |
| 1154 | return result; |
| 1155 | } |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 1156 | case k_not_IntrinsicKind: return unary(args[0], [](skvm::I32 x) { return ~x; }); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1157 | |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 1158 | default: |
| 1159 | SkDEBUGFAILF("unsupported intrinsic %s", c.function().description().c_str()); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1160 | return {}; |
| 1161 | } |
| 1162 | SkUNREACHABLE; |
| 1163 | } |
| 1164 | |
| 1165 | Value SkVMGenerator::writeFunctionCall(const FunctionCall& f) { |
John Stiles | 032fcba | 2021-05-06 11:33:08 -0400 | [diff] [blame] | 1166 | if (f.function().isIntrinsic() && !f.function().definition()) { |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1167 | return this->writeIntrinsicCall(f); |
| 1168 | } |
| 1169 | |
Brian Osman | 54515b7 | 2021-01-07 14:38:08 -0500 | [diff] [blame] | 1170 | const FunctionDeclaration& decl = f.function(); |
| 1171 | |
| 1172 | // Evaluate all arguments, gather the results into a contiguous list of IDs |
| 1173 | std::vector<skvm::Val> argVals; |
| 1174 | for (const auto& arg : f.arguments()) { |
| 1175 | Value v = this->writeExpression(*arg); |
| 1176 | for (size_t i = 0; i < v.slots(); ++i) { |
| 1177 | argVals.push_back(v[i]); |
| 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | // Create storage for the return value |
John Stiles | 727adfe | 2021-09-14 14:07:23 -0400 | [diff] [blame] | 1182 | const skvm::F32 zero = fBuilder->splat(0.0f); |
John Stiles | 47b087e | 2021-04-06 13:19:35 -0400 | [diff] [blame] | 1183 | size_t nslots = f.type().slotCount(); |
Brian Osman | 54515b7 | 2021-01-07 14:38:08 -0500 | [diff] [blame] | 1184 | Value result(nslots); |
| 1185 | for (size_t i = 0; i < nslots; ++i) { |
John Stiles | 727adfe | 2021-09-14 14:07:23 -0400 | [diff] [blame] | 1186 | result[i] = zero; |
Brian Osman | 54515b7 | 2021-01-07 14:38:08 -0500 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | { |
Brian Osman | 9333c87 | 2021-01-13 15:06:17 -0500 | [diff] [blame] | 1190 | // This merges currentFunction().fReturned into fConditionMask. Lanes that conditionally |
Brian Osman | 54515b7 | 2021-01-07 14:38:08 -0500 | [diff] [blame] | 1191 | // returned in the current function would otherwise resume execution within the child. |
Brian Osman | 9333c87 | 2021-01-13 15:06:17 -0500 | [diff] [blame] | 1192 | ScopedCondition m(this, ~currentFunction().fReturned); |
Ethan Nicholas | 624a529 | 2021-04-16 14:54:43 -0400 | [diff] [blame] | 1193 | SkASSERTF(f.function().definition(), "no definition for function '%s'", |
| 1194 | f.function().description().c_str()); |
Brian Osman | ae87bf1 | 2021-05-11 13:36:10 -0400 | [diff] [blame] | 1195 | this->writeFunction(*f.function().definition(), SkMakeSpan(argVals), result.asSpan()); |
Brian Osman | 54515b7 | 2021-01-07 14:38:08 -0500 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | // Propagate new values of any 'out' params back to the original arguments |
| 1199 | const std::unique_ptr<Expression>* argIter = f.arguments().begin(); |
| 1200 | size_t valIdx = 0; |
| 1201 | for (const Variable* p : decl.parameters()) { |
John Stiles | 68f5606 | 2021-08-03 12:31:56 -0400 | [diff] [blame] | 1202 | nslots = p->type().slotCount(); |
Brian Osman | 54515b7 | 2021-01-07 14:38:08 -0500 | [diff] [blame] | 1203 | if (p->modifiers().fFlags & Modifiers::kOut_Flag) { |
| 1204 | Value v(nslots); |
| 1205 | for (size_t i = 0; i < nslots; ++i) { |
| 1206 | v[i] = argVals[valIdx + i]; |
| 1207 | } |
| 1208 | const std::unique_ptr<Expression>& arg = *argIter; |
| 1209 | this->writeStore(*arg, v); |
| 1210 | } |
| 1211 | valIdx += nslots; |
| 1212 | argIter++; |
| 1213 | } |
| 1214 | |
| 1215 | return result; |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1216 | } |
| 1217 | |
Brian Osman | dd50b0c | 2021-01-11 17:04:29 -0500 | [diff] [blame] | 1218 | Value SkVMGenerator::writeExternalFunctionCall(const ExternalFunctionCall& c) { |
| 1219 | // Evaluate all arguments, gather the results into a contiguous list of F32 |
| 1220 | std::vector<skvm::F32> args; |
| 1221 | for (const auto& arg : c.arguments()) { |
| 1222 | Value v = this->writeExpression(*arg); |
| 1223 | for (size_t i = 0; i < v.slots(); ++i) { |
| 1224 | args.push_back(f32(v[i])); |
| 1225 | } |
| 1226 | } |
| 1227 | |
| 1228 | // Create storage for the return value |
John Stiles | 47b087e | 2021-04-06 13:19:35 -0400 | [diff] [blame] | 1229 | size_t nslots = c.type().slotCount(); |
Brian Osman | dd50b0c | 2021-01-11 17:04:29 -0500 | [diff] [blame] | 1230 | std::vector<skvm::F32> result(nslots, fBuilder->splat(0.0f)); |
| 1231 | |
| 1232 | c.function().call(fBuilder, args.data(), result.data(), this->mask()); |
| 1233 | |
| 1234 | // Convert from 'vector of F32' to Value |
| 1235 | Value resultVal(nslots); |
| 1236 | for (size_t i = 0; i < nslots; ++i) { |
| 1237 | resultVal[i] = result[i]; |
| 1238 | } |
| 1239 | |
| 1240 | return resultVal; |
| 1241 | } |
| 1242 | |
John Stiles | 7591d4b | 2021-09-13 13:32:06 -0400 | [diff] [blame] | 1243 | Value SkVMGenerator::writeLiteral(const Literal& l) { |
| 1244 | if (l.type().isFloat()) { |
| 1245 | return fBuilder->splat(l.as<Literal>().floatValue()); |
| 1246 | } |
| 1247 | if (l.type().isInteger()) { |
| 1248 | return fBuilder->splat(static_cast<int>(l.as<Literal>().intValue())); |
| 1249 | } |
| 1250 | SkASSERT(l.type().isBoolean()); |
| 1251 | return fBuilder->splat(l.as<Literal>().boolValue() ? ~0 : 0); |
| 1252 | } |
| 1253 | |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1254 | Value SkVMGenerator::writePrefixExpression(const PrefixExpression& p) { |
| 1255 | Value val = this->writeExpression(*p.operand()); |
| 1256 | |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 1257 | switch (p.getOperator().kind()) { |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1258 | case Token::Kind::TK_PLUSPLUS: |
| 1259 | case Token::Kind::TK_MINUSMINUS: { |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 1260 | bool incr = p.getOperator().kind() == Token::Kind::TK_PLUSPLUS; |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1261 | |
| 1262 | switch (base_number_kind(p.type())) { |
| 1263 | case Type::NumberKind::kFloat: |
| 1264 | val = f32(val) + fBuilder->splat(incr ? 1.0f : -1.0f); |
| 1265 | break; |
| 1266 | case Type::NumberKind::kSigned: |
| 1267 | val = i32(val) + fBuilder->splat(incr ? 1 : -1); |
| 1268 | break; |
| 1269 | default: |
| 1270 | SkASSERT(false); |
| 1271 | return {}; |
| 1272 | } |
| 1273 | return this->writeStore(*p.operand(), val); |
| 1274 | } |
| 1275 | case Token::Kind::TK_MINUS: { |
| 1276 | switch (base_number_kind(p.type())) { |
| 1277 | case Type::NumberKind::kFloat: |
| 1278 | return this->unary(val, [](skvm::F32 x) { return -x; }); |
| 1279 | case Type::NumberKind::kSigned: |
| 1280 | return this->unary(val, [](skvm::I32 x) { return -x; }); |
| 1281 | default: |
| 1282 | SkASSERT(false); |
| 1283 | return {}; |
| 1284 | } |
| 1285 | } |
| 1286 | case Token::Kind::TK_LOGICALNOT: |
| 1287 | case Token::Kind::TK_BITWISENOT: |
| 1288 | return this->unary(val, [](skvm::I32 x) { return ~x; }); |
| 1289 | default: |
| 1290 | SkASSERT(false); |
| 1291 | return {}; |
| 1292 | } |
| 1293 | } |
| 1294 | |
| 1295 | Value SkVMGenerator::writePostfixExpression(const PostfixExpression& p) { |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 1296 | switch (p.getOperator().kind()) { |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1297 | case Token::Kind::TK_PLUSPLUS: |
| 1298 | case Token::Kind::TK_MINUSMINUS: { |
| 1299 | Value old = this->writeExpression(*p.operand()), |
| 1300 | val = old; |
| 1301 | SkASSERT(val.slots() == 1); |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 1302 | bool incr = p.getOperator().kind() == Token::Kind::TK_PLUSPLUS; |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1303 | |
| 1304 | switch (base_number_kind(p.type())) { |
| 1305 | case Type::NumberKind::kFloat: |
| 1306 | val = f32(val) + fBuilder->splat(incr ? 1.0f : -1.0f); |
| 1307 | break; |
| 1308 | case Type::NumberKind::kSigned: |
| 1309 | val = i32(val) + fBuilder->splat(incr ? 1 : -1); |
| 1310 | break; |
| 1311 | default: |
| 1312 | SkASSERT(false); |
| 1313 | return {}; |
| 1314 | } |
| 1315 | this->writeStore(*p.operand(), val); |
| 1316 | return old; |
| 1317 | } |
| 1318 | default: |
| 1319 | SkASSERT(false); |
| 1320 | return {}; |
| 1321 | } |
| 1322 | } |
| 1323 | |
| 1324 | Value SkVMGenerator::writeSwizzle(const Swizzle& s) { |
| 1325 | Value base = this->writeExpression(*s.base()); |
| 1326 | Value swizzled(s.components().size()); |
| 1327 | for (size_t i = 0; i < s.components().size(); ++i) { |
| 1328 | swizzled[i] = base[s.components()[i]]; |
| 1329 | } |
| 1330 | return swizzled; |
| 1331 | } |
| 1332 | |
| 1333 | Value SkVMGenerator::writeTernaryExpression(const TernaryExpression& t) { |
| 1334 | skvm::I32 test = i32(this->writeExpression(*t.test())); |
| 1335 | Value ifTrue, ifFalse; |
| 1336 | |
| 1337 | { |
Brian Osman | 9333c87 | 2021-01-13 15:06:17 -0500 | [diff] [blame] | 1338 | ScopedCondition m(this, test); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1339 | ifTrue = this->writeExpression(*t.ifTrue()); |
| 1340 | } |
| 1341 | { |
Brian Osman | 9333c87 | 2021-01-13 15:06:17 -0500 | [diff] [blame] | 1342 | ScopedCondition m(this, ~test); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1343 | ifFalse = this->writeExpression(*t.ifFalse()); |
| 1344 | } |
| 1345 | |
| 1346 | size_t nslots = ifTrue.slots(); |
| 1347 | SkASSERT(nslots == ifFalse.slots()); |
| 1348 | |
| 1349 | Value result(nslots); |
| 1350 | for (size_t i = 0; i < nslots; ++i) { |
| 1351 | result[i] = skvm::select(test, i32(ifTrue[i]), i32(ifFalse[i])); |
| 1352 | } |
| 1353 | return result; |
| 1354 | } |
| 1355 | |
| 1356 | Value SkVMGenerator::writeExpression(const Expression& e) { |
| 1357 | switch (e.kind()) { |
| 1358 | case Expression::Kind::kBinary: |
| 1359 | return this->writeBinaryExpression(e.as<BinaryExpression>()); |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 1360 | case Expression::Kind::kChildCall: |
| 1361 | return this->writeChildCall(e.as<ChildCall>()); |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 1362 | case Expression::Kind::kConstructorArray: |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 1363 | case Expression::Kind::kConstructorCompound: |
John Stiles | d47330f | 2021-04-08 23:25:52 -0400 | [diff] [blame] | 1364 | case Expression::Kind::kConstructorStruct: |
John Stiles | d986f47 | 2021-04-06 15:54:43 -0400 | [diff] [blame] | 1365 | return this->writeAggregationConstructor(e.asAnyConstructor()); |
John Stiles | e3ae968 | 2021-08-05 10:35:01 -0400 | [diff] [blame] | 1366 | case Expression::Kind::kConstructorArrayCast: |
| 1367 | return this->writeExpression(*e.as<ConstructorArrayCast>().argument()); |
John Stiles | e118278 | 2021-03-30 22:09:37 -0400 | [diff] [blame] | 1368 | case Expression::Kind::kConstructorDiagonalMatrix: |
| 1369 | return this->writeConstructorDiagonalMatrix(e.as<ConstructorDiagonalMatrix>()); |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 1370 | case Expression::Kind::kConstructorMatrixResize: |
| 1371 | return this->writeConstructorMatrixResize(e.as<ConstructorMatrixResize>()); |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 1372 | case Expression::Kind::kConstructorScalarCast: |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 1373 | case Expression::Kind::kConstructorCompoundCast: |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 1374 | return this->writeConstructorCast(e.asAnyConstructor()); |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 1375 | case Expression::Kind::kConstructorSplat: |
| 1376 | return this->writeConstructorSplat(e.as<ConstructorSplat>()); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1377 | case Expression::Kind::kFieldAccess: |
Brian Osman | fa71ffa | 2021-01-26 14:05:31 -0500 | [diff] [blame] | 1378 | return this->writeFieldAccess(e.as<FieldAccess>()); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1379 | case Expression::Kind::kIndex: |
Brian Osman | fa71ffa | 2021-01-26 14:05:31 -0500 | [diff] [blame] | 1380 | return this->writeIndexExpression(e.as<IndexExpression>()); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1381 | case Expression::Kind::kVariableReference: |
Brian Osman | fa71ffa | 2021-01-26 14:05:31 -0500 | [diff] [blame] | 1382 | return this->writeVariableExpression(e.as<VariableReference>()); |
John Stiles | 7591d4b | 2021-09-13 13:32:06 -0400 | [diff] [blame] | 1383 | case Expression::Kind::kLiteral: |
| 1384 | return this->writeLiteral(e.as<Literal>()); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1385 | case Expression::Kind::kFunctionCall: |
| 1386 | return this->writeFunctionCall(e.as<FunctionCall>()); |
Brian Osman | dd50b0c | 2021-01-11 17:04:29 -0500 | [diff] [blame] | 1387 | case Expression::Kind::kExternalFunctionCall: |
| 1388 | return this->writeExternalFunctionCall(e.as<ExternalFunctionCall>()); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1389 | case Expression::Kind::kPrefix: |
| 1390 | return this->writePrefixExpression(e.as<PrefixExpression>()); |
| 1391 | case Expression::Kind::kPostfix: |
| 1392 | return this->writePostfixExpression(e.as<PostfixExpression>()); |
| 1393 | case Expression::Kind::kSwizzle: |
| 1394 | return this->writeSwizzle(e.as<Swizzle>()); |
| 1395 | case Expression::Kind::kTernary: |
| 1396 | return this->writeTernaryExpression(e.as<TernaryExpression>()); |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 1397 | case Expression::Kind::kExternalFunctionReference: |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1398 | default: |
| 1399 | SkDEBUGFAIL("Unsupported expression"); |
| 1400 | return {}; |
| 1401 | } |
| 1402 | } |
| 1403 | |
| 1404 | Value SkVMGenerator::writeStore(const Expression& lhs, const Value& rhs) { |
John Stiles | 47b087e | 2021-04-06 13:19:35 -0400 | [diff] [blame] | 1405 | SkASSERTF(rhs.slots() == lhs.type().slotCount(), |
John Stiles | 7bf7999 | 2021-06-25 11:05:20 -0400 | [diff] [blame] | 1406 | "lhs=%s (%s)\nrhs=%zu slot", |
John Stiles | 94e72b9 | 2021-01-30 11:06:18 -0500 | [diff] [blame] | 1407 | lhs.type().description().c_str(), lhs.description().c_str(), rhs.slots()); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1408 | |
Brian Osman | 21f5707 | 2021-01-25 13:51:57 -0500 | [diff] [blame] | 1409 | // We need to figure out the collection of slots that we're storing into. The l-value (lhs) |
| 1410 | // is always a VariableReference, possibly wrapped by one or more Swizzle, FieldAccess, or |
| 1411 | // IndexExpressions. The underlying VariableReference has a range of slots for its storage, |
| 1412 | // and each expression wrapped around that selects a sub-set of those slots (Field/Index), |
| 1413 | // or rearranges them (Swizzle). |
| 1414 | SkSTArray<4, size_t, true> slots; |
| 1415 | slots.resize(rhs.slots()); |
| 1416 | |
| 1417 | // Start with the identity slot map - this basically says that the values from rhs belong in |
| 1418 | // slots [0, 1, 2 ... N] of the lhs. |
| 1419 | for (size_t i = 0; i < slots.size(); ++i) { |
| 1420 | slots[i] = i; |
| 1421 | } |
| 1422 | |
| 1423 | // Now, as we peel off each outer expression, adjust 'slots' to be the locations relative to |
| 1424 | // the next (inner) expression: |
| 1425 | const Expression* expr = &lhs; |
| 1426 | while (!expr->is<VariableReference>()) { |
| 1427 | switch (expr->kind()) { |
| 1428 | case Expression::Kind::kFieldAccess: { |
| 1429 | const FieldAccess& fld = expr->as<FieldAccess>(); |
| 1430 | size_t offset = this->fieldSlotOffset(fld); |
| 1431 | for (size_t& s : slots) { |
| 1432 | s += offset; |
| 1433 | } |
| 1434 | expr = fld.base().get(); |
| 1435 | } break; |
| 1436 | case Expression::Kind::kIndex: { |
| 1437 | const IndexExpression& idx = expr->as<IndexExpression>(); |
| 1438 | size_t offset = this->indexSlotOffset(idx); |
| 1439 | for (size_t& s : slots) { |
| 1440 | s += offset; |
| 1441 | } |
| 1442 | expr = idx.base().get(); |
| 1443 | } break; |
| 1444 | case Expression::Kind::kSwizzle: { |
| 1445 | const Swizzle& swz = expr->as<Swizzle>(); |
| 1446 | for (size_t& s : slots) { |
| 1447 | s = swz.components()[s]; |
| 1448 | } |
| 1449 | expr = swz.base().get(); |
| 1450 | } break; |
| 1451 | default: |
| 1452 | // No other kinds of expressions are valid in lvalues. (see Analysis::IsAssignable) |
| 1453 | SkDEBUGFAIL("Invalid expression type"); |
| 1454 | return {}; |
| 1455 | } |
| 1456 | } |
| 1457 | |
| 1458 | // When we get here, 'slots' are all relative to the first slot holding 'var's storage |
| 1459 | const Variable& var = *expr->as<VariableReference>().variable(); |
| 1460 | size_t varSlot = this->getSlot(var); |
John Stiles | cb40008 | 2021-09-23 11:07:32 -0400 | [diff] [blame] | 1461 | for (size_t& slot : slots) { |
| 1462 | SkASSERT(slot < var.type().slotCount()); |
| 1463 | slot += varSlot; |
| 1464 | } |
| 1465 | |
| 1466 | // `slots` are now absolute indices into `fSlots`. |
John Stiles | d71fac6 | 2021-09-23 17:14:04 -0400 | [diff] [blame^] | 1467 | skvm::I32 mask = this->mask(); |
| 1468 | for (size_t i = rhs.slots(); i --> 0;) { |
| 1469 | skvm::Val& slotVal = fSlots[slots[i]]; |
| 1470 | slotVal = this->writeConditionalStore(slotVal, rhs[i], mask); |
| 1471 | } |
| 1472 | |
John Stiles | cb40008 | 2021-09-23 11:07:32 -0400 | [diff] [blame] | 1473 | return rhs; |
| 1474 | } |
| 1475 | |
John Stiles | d71fac6 | 2021-09-23 17:14:04 -0400 | [diff] [blame^] | 1476 | skvm::Val SkVMGenerator::writeConditionalStore(skvm::Val lhs, skvm::Val rhs, skvm::I32 mask) { |
| 1477 | return select(mask, f32(rhs), f32(lhs)).id; |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | void SkVMGenerator::writeBlock(const Block& b) { |
| 1481 | for (const std::unique_ptr<Statement>& stmt : b.children()) { |
| 1482 | this->writeStatement(*stmt); |
| 1483 | } |
| 1484 | } |
| 1485 | |
Brian Osman | 9333c87 | 2021-01-13 15:06:17 -0500 | [diff] [blame] | 1486 | void SkVMGenerator::writeBreakStatement() { |
| 1487 | // Any active lanes stop executing for the duration of the current loop |
| 1488 | fLoopMask &= ~this->mask(); |
| 1489 | } |
| 1490 | |
| 1491 | void SkVMGenerator::writeContinueStatement() { |
| 1492 | // Any active lanes stop executing for the current iteration. |
| 1493 | // Remember them in fContinueMask, to be re-enabled later. |
| 1494 | skvm::I32 mask = this->mask(); |
| 1495 | fLoopMask &= ~mask; |
| 1496 | fContinueMask |= mask; |
| 1497 | } |
| 1498 | |
| 1499 | void SkVMGenerator::writeForStatement(const ForStatement& f) { |
| 1500 | // We require that all loops be ES2-compliant (unrollable), and actually unroll them here |
John Stiles | 9c975c5 | 2021-08-31 10:18:57 -0400 | [diff] [blame] | 1501 | SkASSERT(f.unrollInfo()); |
| 1502 | const LoopUnrollInfo& loop = *f.unrollInfo(); |
John Stiles | 47b087e | 2021-04-06 13:19:35 -0400 | [diff] [blame] | 1503 | SkASSERT(loop.fIndex->type().slotCount() == 1); |
Brian Osman | 9333c87 | 2021-01-13 15:06:17 -0500 | [diff] [blame] | 1504 | |
Brian Osman | 21f5707 | 2021-01-25 13:51:57 -0500 | [diff] [blame] | 1505 | size_t indexSlot = this->getSlot(*loop.fIndex); |
Brian Osman | 9333c87 | 2021-01-13 15:06:17 -0500 | [diff] [blame] | 1506 | double val = loop.fStart; |
| 1507 | |
John Stiles | 727adfe | 2021-09-14 14:07:23 -0400 | [diff] [blame] | 1508 | const skvm::I32 zero = fBuilder->splat(0); |
Brian Osman | 9333c87 | 2021-01-13 15:06:17 -0500 | [diff] [blame] | 1509 | skvm::I32 oldLoopMask = fLoopMask, |
| 1510 | oldContinueMask = fContinueMask; |
| 1511 | |
| 1512 | for (int i = 0; i < loop.fCount; ++i) { |
Brian Osman | 21f5707 | 2021-01-25 13:51:57 -0500 | [diff] [blame] | 1513 | fSlots[indexSlot] = loop.fIndex->type().isInteger() |
| 1514 | ? fBuilder->splat(static_cast<int>(val)).id |
| 1515 | : fBuilder->splat(static_cast<float>(val)).id; |
Brian Osman | 9333c87 | 2021-01-13 15:06:17 -0500 | [diff] [blame] | 1516 | |
John Stiles | 727adfe | 2021-09-14 14:07:23 -0400 | [diff] [blame] | 1517 | fContinueMask = zero; |
Brian Osman | 9333c87 | 2021-01-13 15:06:17 -0500 | [diff] [blame] | 1518 | this->writeStatement(*f.statement()); |
| 1519 | fLoopMask |= fContinueMask; |
| 1520 | |
| 1521 | val += loop.fDelta; |
| 1522 | } |
| 1523 | |
| 1524 | fLoopMask = oldLoopMask; |
| 1525 | fContinueMask = oldContinueMask; |
| 1526 | } |
| 1527 | |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1528 | void SkVMGenerator::writeIfStatement(const IfStatement& i) { |
| 1529 | Value test = this->writeExpression(*i.test()); |
| 1530 | { |
Brian Osman | 9333c87 | 2021-01-13 15:06:17 -0500 | [diff] [blame] | 1531 | ScopedCondition ifTrue(this, i32(test)); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1532 | this->writeStatement(*i.ifTrue()); |
| 1533 | } |
| 1534 | if (i.ifFalse()) { |
Brian Osman | 9333c87 | 2021-01-13 15:06:17 -0500 | [diff] [blame] | 1535 | ScopedCondition ifFalse(this, ~i32(test)); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1536 | this->writeStatement(*i.ifFalse()); |
| 1537 | } |
| 1538 | } |
| 1539 | |
| 1540 | void SkVMGenerator::writeReturnStatement(const ReturnStatement& r) { |
Brian Osman | 54515b7 | 2021-01-07 14:38:08 -0500 | [diff] [blame] | 1541 | skvm::I32 returnsHere = this->mask(); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1542 | |
Brian Osman | 54515b7 | 2021-01-07 14:38:08 -0500 | [diff] [blame] | 1543 | if (r.expression()) { |
| 1544 | Value val = this->writeExpression(*r.expression()); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1545 | |
Brian Osman | 54515b7 | 2021-01-07 14:38:08 -0500 | [diff] [blame] | 1546 | int i = 0; |
| 1547 | for (skvm::Val& slot : currentFunction().fReturnValue) { |
| 1548 | slot = select(returnsHere, f32(val[i]), f32(slot)).id; |
| 1549 | i++; |
| 1550 | } |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1551 | } |
| 1552 | |
Brian Osman | 54515b7 | 2021-01-07 14:38:08 -0500 | [diff] [blame] | 1553 | currentFunction().fReturned |= returnsHere; |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1554 | } |
| 1555 | |
John Stiles | cb40008 | 2021-09-23 11:07:32 -0400 | [diff] [blame] | 1556 | void SkVMGenerator::writeSwitchStatement(const SwitchStatement& s) { |
John Stiles | 4144211 | 2021-09-23 16:15:15 -0400 | [diff] [blame] | 1557 | skvm::I32 falseValue = fBuilder->splat( 0); |
| 1558 | skvm::I32 trueValue = fBuilder->splat(~0); |
John Stiles | cb40008 | 2021-09-23 11:07:32 -0400 | [diff] [blame] | 1559 | |
John Stiles | d71fac6 | 2021-09-23 17:14:04 -0400 | [diff] [blame^] | 1560 | // Create a "switchFallthough" scratch variable, initialized to false. |
| 1561 | skvm::I32 switchFallthrough = falseValue; |
John Stiles | cb40008 | 2021-09-23 11:07:32 -0400 | [diff] [blame] | 1562 | |
| 1563 | // Loop masks behave just like for statements. When a break is encountered, it masks off all |
| 1564 | // lanes for the rest of the body of the switch. |
| 1565 | skvm::I32 oldLoopMask = fLoopMask; |
| 1566 | Value switchValue = this->writeExpression(*s.value()); |
| 1567 | |
| 1568 | for (const std::unique_ptr<Statement>& stmt : s.cases()) { |
| 1569 | const SwitchCase& c = stmt->as<SwitchCase>(); |
| 1570 | if (c.value()) { |
| 1571 | Value caseValue = this->writeExpression(*c.value()); |
| 1572 | |
| 1573 | // We want to execute this switch case if we're falling through from a previous case, or |
| 1574 | // if the case value matches. |
John Stiles | 4144211 | 2021-09-23 16:15:15 -0400 | [diff] [blame] | 1575 | ScopedCondition conditionalCaseBlock( |
| 1576 | this, |
John Stiles | d71fac6 | 2021-09-23 17:14:04 -0400 | [diff] [blame^] | 1577 | switchFallthrough | (i32(caseValue) == i32(switchValue))); |
John Stiles | cb40008 | 2021-09-23 11:07:32 -0400 | [diff] [blame] | 1578 | this->writeStatement(*c.statement()); |
| 1579 | |
John Stiles | d71fac6 | 2021-09-23 17:14:04 -0400 | [diff] [blame^] | 1580 | // If we are inside the case block, we set the fallthrough flag to true (`break` still |
| 1581 | // works to stop the flow of execution regardless, since it zeroes out the loop-mask). |
| 1582 | switchFallthrough.id = this->writeConditionalStore(switchFallthrough.id, trueValue.id, |
| 1583 | this->mask()); |
John Stiles | cb40008 | 2021-09-23 11:07:32 -0400 | [diff] [blame] | 1584 | } else { |
| 1585 | // This is the default case. Since it's always last, we can just dump in the code. |
| 1586 | this->writeStatement(*c.statement()); |
| 1587 | } |
| 1588 | } |
| 1589 | |
| 1590 | // Restore state. |
| 1591 | fLoopMask = oldLoopMask; |
John Stiles | cb40008 | 2021-09-23 11:07:32 -0400 | [diff] [blame] | 1592 | } |
| 1593 | |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1594 | void SkVMGenerator::writeVarDeclaration(const VarDeclaration& decl) { |
Brian Osman | 21f5707 | 2021-01-25 13:51:57 -0500 | [diff] [blame] | 1595 | size_t slot = this->getSlot(decl.var()), |
John Stiles | 47b087e | 2021-04-06 13:19:35 -0400 | [diff] [blame] | 1596 | nslots = decl.var().type().slotCount(); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1597 | |
| 1598 | Value val = decl.value() ? this->writeExpression(*decl.value()) : Value{}; |
| 1599 | for (size_t i = 0; i < nslots; ++i) { |
| 1600 | fSlots[slot + i] = val ? val[i] : fBuilder->splat(0.0f).id; |
| 1601 | } |
| 1602 | } |
| 1603 | |
| 1604 | void SkVMGenerator::writeStatement(const Statement& s) { |
| 1605 | switch (s.kind()) { |
| 1606 | case Statement::Kind::kBlock: |
| 1607 | this->writeBlock(s.as<Block>()); |
| 1608 | break; |
Brian Osman | 9333c87 | 2021-01-13 15:06:17 -0500 | [diff] [blame] | 1609 | case Statement::Kind::kBreak: |
| 1610 | this->writeBreakStatement(); |
| 1611 | break; |
| 1612 | case Statement::Kind::kContinue: |
| 1613 | this->writeContinueStatement(); |
| 1614 | break; |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1615 | case Statement::Kind::kExpression: |
| 1616 | this->writeExpression(*s.as<ExpressionStatement>().expression()); |
| 1617 | break; |
Brian Osman | 9333c87 | 2021-01-13 15:06:17 -0500 | [diff] [blame] | 1618 | case Statement::Kind::kFor: |
| 1619 | this->writeForStatement(s.as<ForStatement>()); |
| 1620 | break; |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1621 | case Statement::Kind::kIf: |
| 1622 | this->writeIfStatement(s.as<IfStatement>()); |
| 1623 | break; |
| 1624 | case Statement::Kind::kReturn: |
| 1625 | this->writeReturnStatement(s.as<ReturnStatement>()); |
| 1626 | break; |
John Stiles | cb40008 | 2021-09-23 11:07:32 -0400 | [diff] [blame] | 1627 | case Statement::Kind::kSwitch: |
| 1628 | this->writeSwitchStatement(s.as<SwitchStatement>()); |
| 1629 | break; |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1630 | case Statement::Kind::kVarDeclaration: |
| 1631 | this->writeVarDeclaration(s.as<VarDeclaration>()); |
| 1632 | break; |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1633 | case Statement::Kind::kDiscard: |
| 1634 | case Statement::Kind::kDo: |
Brian Osman | 57e353f | 2021-01-07 15:55:20 -0500 | [diff] [blame] | 1635 | SkDEBUGFAIL("Unsupported control flow"); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1636 | break; |
| 1637 | case Statement::Kind::kInlineMarker: |
| 1638 | case Statement::Kind::kNop: |
| 1639 | break; |
| 1640 | default: |
| 1641 | SkASSERT(false); |
| 1642 | } |
| 1643 | } |
| 1644 | |
| 1645 | skvm::Color ProgramToSkVM(const Program& program, |
| 1646 | const FunctionDefinition& function, |
| 1647 | skvm::Builder* builder, |
| 1648 | SkSpan<skvm::Val> uniforms, |
| 1649 | skvm::Coord device, |
| 1650 | skvm::Coord local, |
Brian Osman | 577c606 | 2021-04-12 17:17:19 -0400 | [diff] [blame] | 1651 | skvm::Color inputColor, |
John Stiles | 50d0d09 | 2021-06-09 17:24:31 -0400 | [diff] [blame] | 1652 | skvm::Color destColor, |
John Stiles | 137482f | 2021-07-23 10:38:57 -0400 | [diff] [blame] | 1653 | SampleShaderFn sampleShader, |
John Stiles | 2955c26 | 2021-07-23 15:51:05 -0400 | [diff] [blame] | 1654 | SampleColorFilterFn sampleColorFilter, |
| 1655 | SampleBlenderFn sampleBlender) { |
Mike Klein | aebcf73 | 2021-01-14 10:15:00 -0600 | [diff] [blame] | 1656 | skvm::Val zero = builder->splat(0.0f).id; |
| 1657 | skvm::Val result[4] = {zero,zero,zero,zero}; |
Brian Osman | 577c606 | 2021-04-12 17:17:19 -0400 | [diff] [blame] | 1658 | |
John Stiles | f7f36ae | 2021-06-08 14:06:22 -0400 | [diff] [blame] | 1659 | skvm::Val args[8]; // At most 8 arguments (half4 srcColor, half4 dstColor) |
Brian Osman | 577c606 | 2021-04-12 17:17:19 -0400 | [diff] [blame] | 1660 | size_t argSlots = 0; |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1661 | for (const SkSL::Variable* param : function.declaration().parameters()) { |
Brian Osman | 577c606 | 2021-04-12 17:17:19 -0400 | [diff] [blame] | 1662 | switch (param->modifiers().fLayout.fBuiltin) { |
| 1663 | case SK_MAIN_COORDS_BUILTIN: |
| 1664 | SkASSERT(param->type().slotCount() == 2); |
John Stiles | f7f36ae | 2021-06-08 14:06:22 -0400 | [diff] [blame] | 1665 | SkASSERT((argSlots + 2) <= SK_ARRAY_COUNT(args)); |
Brian Osman | 577c606 | 2021-04-12 17:17:19 -0400 | [diff] [blame] | 1666 | args[argSlots++] = local.x.id; |
| 1667 | args[argSlots++] = local.y.id; |
| 1668 | break; |
| 1669 | case SK_INPUT_COLOR_BUILTIN: |
| 1670 | SkASSERT(param->type().slotCount() == 4); |
John Stiles | f7f36ae | 2021-06-08 14:06:22 -0400 | [diff] [blame] | 1671 | SkASSERT((argSlots + 4) <= SK_ARRAY_COUNT(args)); |
Brian Osman | 577c606 | 2021-04-12 17:17:19 -0400 | [diff] [blame] | 1672 | args[argSlots++] = inputColor.r.id; |
| 1673 | args[argSlots++] = inputColor.g.id; |
| 1674 | args[argSlots++] = inputColor.b.id; |
| 1675 | args[argSlots++] = inputColor.a.id; |
| 1676 | break; |
John Stiles | 50d0d09 | 2021-06-09 17:24:31 -0400 | [diff] [blame] | 1677 | case SK_DEST_COLOR_BUILTIN: |
| 1678 | SkASSERT(param->type().slotCount() == 4); |
| 1679 | SkASSERT((argSlots + 4) <= SK_ARRAY_COUNT(args)); |
| 1680 | args[argSlots++] = destColor.r.id; |
| 1681 | args[argSlots++] = destColor.g.id; |
| 1682 | args[argSlots++] = destColor.b.id; |
| 1683 | args[argSlots++] = destColor.a.id; |
| 1684 | break; |
Brian Osman | 577c606 | 2021-04-12 17:17:19 -0400 | [diff] [blame] | 1685 | default: |
| 1686 | SkDEBUGFAIL("Invalid parameter to main()"); |
| 1687 | return {}; |
| 1688 | } |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1689 | } |
Brian Osman | 577c606 | 2021-04-12 17:17:19 -0400 | [diff] [blame] | 1690 | SkASSERT(argSlots <= SK_ARRAY_COUNT(args)); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1691 | |
John Stiles | 428af4c | 2021-09-22 14:28:19 -0400 | [diff] [blame] | 1692 | SkVMGenerator generator(program, builder, std::move(sampleShader), |
John Stiles | 2955c26 | 2021-07-23 15:51:05 -0400 | [diff] [blame] | 1693 | std::move(sampleColorFilter), std::move(sampleBlender)); |
John Stiles | a4f5683 | 2021-09-22 14:22:33 -0400 | [diff] [blame] | 1694 | generator.writeProgram(uniforms, device, function, {args, argSlots}, SkMakeSpan(result)); |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1695 | |
Brian Osman | 57e353f | 2021-01-07 15:55:20 -0500 | [diff] [blame] | 1696 | return skvm::Color{{builder, result[0]}, |
| 1697 | {builder, result[1]}, |
| 1698 | {builder, result[2]}, |
| 1699 | {builder, result[3]}}; |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1700 | } |
| 1701 | |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 1702 | bool ProgramToSkVM(const Program& program, |
| 1703 | const FunctionDefinition& function, |
| 1704 | skvm::Builder* b, |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 1705 | SkSpan<skvm::Val> uniforms, |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 1706 | SkVMSignature* outSignature) { |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 1707 | SkVMSignature ignored, |
| 1708 | *signature = outSignature ? outSignature : &ignored; |
| 1709 | |
Mike Klein | 00e43df | 2021-01-08 13:45:42 -0600 | [diff] [blame] | 1710 | std::vector<skvm::Ptr> argPtrs; |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 1711 | std::vector<skvm::Val> argVals; |
| 1712 | |
| 1713 | for (const Variable* p : function.declaration().parameters()) { |
John Stiles | 47b087e | 2021-04-06 13:19:35 -0400 | [diff] [blame] | 1714 | size_t slots = p->type().slotCount(); |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 1715 | signature->fParameterSlots += slots; |
| 1716 | for (size_t i = 0; i < slots; ++i) { |
| 1717 | argPtrs.push_back(b->varying<float>()); |
| 1718 | argVals.push_back(b->loadF(argPtrs.back()).id); |
| 1719 | } |
| 1720 | } |
| 1721 | |
Mike Klein | 00e43df | 2021-01-08 13:45:42 -0600 | [diff] [blame] | 1722 | std::vector<skvm::Ptr> returnPtrs; |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 1723 | std::vector<skvm::Val> returnVals; |
| 1724 | |
John Stiles | 47b087e | 2021-04-06 13:19:35 -0400 | [diff] [blame] | 1725 | signature->fReturnSlots = function.declaration().returnType().slotCount(); |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 1726 | for (size_t i = 0; i < signature->fReturnSlots; ++i) { |
| 1727 | returnPtrs.push_back(b->varying<float>()); |
| 1728 | returnVals.push_back(b->splat(0.0f).id); |
| 1729 | } |
| 1730 | |
Brian Osman | a8b897b | 2021-08-30 16:40:44 -0400 | [diff] [blame] | 1731 | bool sampledChildEffects = false; |
| 1732 | auto sampleShader = [&](int, skvm::Coord) { |
| 1733 | sampledChildEffects = true; |
| 1734 | return skvm::Color{}; |
| 1735 | }; |
| 1736 | auto sampleColorFilter = [&](int, skvm::Color) { |
| 1737 | sampledChildEffects = true; |
| 1738 | return skvm::Color{}; |
| 1739 | }; |
| 1740 | auto sampleBlender = [&](int, skvm::Color, skvm::Color) { |
| 1741 | sampledChildEffects = true; |
| 1742 | return skvm::Color{}; |
| 1743 | }; |
| 1744 | |
Brian Osman | c9125aa | 2021-04-21 09:57:19 -0400 | [diff] [blame] | 1745 | skvm::F32 zero = b->splat(0.0f); |
| 1746 | skvm::Coord zeroCoord = {zero, zero}; |
John Stiles | 428af4c | 2021-09-22 14:28:19 -0400 | [diff] [blame] | 1747 | SkVMGenerator generator(program, b, sampleShader, sampleColorFilter, sampleBlender); |
John Stiles | a4f5683 | 2021-09-22 14:22:33 -0400 | [diff] [blame] | 1748 | generator.writeProgram(uniforms, /*device=*/zeroCoord, |
| 1749 | function, SkMakeSpan(argVals), SkMakeSpan(returnVals)); |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 1750 | |
Brian Osman | a8b897b | 2021-08-30 16:40:44 -0400 | [diff] [blame] | 1751 | // If the SkSL tried to use any shader, colorFilter, or blender objects - we don't have a |
| 1752 | // mechanism (yet) for binding to those. |
| 1753 | if (sampledChildEffects) { |
| 1754 | return false; |
| 1755 | } |
| 1756 | |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 1757 | // generateCode has updated the contents of 'argVals' for any 'out' or 'inout' parameters. |
| 1758 | // Propagate those changes back to our varying buffers: |
| 1759 | size_t argIdx = 0; |
| 1760 | for (const Variable* p : function.declaration().parameters()) { |
John Stiles | 47b087e | 2021-04-06 13:19:35 -0400 | [diff] [blame] | 1761 | size_t nslots = p->type().slotCount(); |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 1762 | if (p->modifiers().fFlags & Modifiers::kOut_Flag) { |
| 1763 | for (size_t i = 0; i < nslots; ++i) { |
| 1764 | b->storeF(argPtrs[argIdx + i], skvm::F32{b, argVals[argIdx + i]}); |
| 1765 | } |
| 1766 | } |
| 1767 | argIdx += nslots; |
| 1768 | } |
| 1769 | |
| 1770 | // It's also updated the contents of 'returnVals' with the return value of the entry point. |
| 1771 | // Store that as well: |
| 1772 | for (size_t i = 0; i < signature->fReturnSlots; ++i) { |
| 1773 | b->storeF(returnPtrs[i], skvm::F32{b, returnVals[i]}); |
| 1774 | } |
| 1775 | |
| 1776 | return true; |
| 1777 | } |
| 1778 | |
Brian Osman | 5933d4c | 2021-01-05 13:02:20 -0500 | [diff] [blame] | 1779 | const FunctionDefinition* Program_GetFunction(const Program& program, const char* function) { |
| 1780 | for (const ProgramElement* e : program.elements()) { |
| 1781 | if (e->is<FunctionDefinition>() && |
| 1782 | e->as<FunctionDefinition>().declaration().name() == function) { |
| 1783 | return &e->as<FunctionDefinition>(); |
| 1784 | } |
| 1785 | } |
| 1786 | return nullptr; |
| 1787 | } |
| 1788 | |
Brian Osman | e89d8ea | 2021-01-20 14:01:30 -0500 | [diff] [blame] | 1789 | static void gather_uniforms(UniformInfo* info, const Type& type, const String& name) { |
| 1790 | switch (type.typeKind()) { |
| 1791 | case Type::TypeKind::kStruct: |
| 1792 | for (const auto& f : type.fields()) { |
| 1793 | gather_uniforms(info, *f.fType, name + "." + f.fName); |
| 1794 | } |
| 1795 | break; |
| 1796 | case Type::TypeKind::kArray: |
| 1797 | for (int i = 0; i < type.columns(); ++i) { |
| 1798 | gather_uniforms(info, type.componentType(), |
| 1799 | String::printf("%s[%d]", name.c_str(), i)); |
| 1800 | } |
| 1801 | break; |
| 1802 | case Type::TypeKind::kScalar: |
| 1803 | case Type::TypeKind::kVector: |
| 1804 | case Type::TypeKind::kMatrix: |
| 1805 | info->fUniforms.push_back({name, base_number_kind(type), type.rows(), type.columns(), |
| 1806 | info->fUniformSlotCount}); |
| 1807 | info->fUniformSlotCount += type.columns() * type.rows(); |
| 1808 | break; |
| 1809 | default: |
| 1810 | break; |
| 1811 | } |
| 1812 | } |
| 1813 | |
| 1814 | std::unique_ptr<UniformInfo> Program_GetUniformInfo(const Program& program) { |
| 1815 | auto info = std::make_unique<UniformInfo>(); |
| 1816 | for (const ProgramElement* e : program.elements()) { |
| 1817 | if (!e->is<GlobalVarDeclaration>()) { |
| 1818 | continue; |
| 1819 | } |
| 1820 | const GlobalVarDeclaration& decl = e->as<GlobalVarDeclaration>(); |
| 1821 | const Variable& var = decl.declaration()->as<VarDeclaration>().var(); |
| 1822 | if (var.modifiers().fFlags & Modifiers::kUniform_Flag) { |
Ethan Nicholas | d2e0960 | 2021-06-10 11:21:59 -0400 | [diff] [blame] | 1823 | gather_uniforms(info.get(), var.type(), String(var.name())); |
Brian Osman | e89d8ea | 2021-01-20 14:01:30 -0500 | [diff] [blame] | 1824 | } |
| 1825 | } |
| 1826 | return info; |
| 1827 | } |
| 1828 | |
Brian Osman | 47726a1 | 2020-12-17 16:02:08 -0500 | [diff] [blame] | 1829 | /* |
| 1830 | * Testing utility function that emits program's "main" with a minimal harness. Used to create |
| 1831 | * representative skvm op sequences for SkSL tests. |
| 1832 | */ |
| 1833 | bool testingOnly_ProgramToSkVMShader(const Program& program, skvm::Builder* builder) { |
Brian Osman | 5933d4c | 2021-01-05 13:02:20 -0500 | [diff] [blame] | 1834 | const SkSL::FunctionDefinition* main = Program_GetFunction(program, "main"); |
| 1835 | if (!main) { |
| 1836 | return false; |
| 1837 | } |
| 1838 | |
Brian Osman | 47726a1 | 2020-12-17 16:02:08 -0500 | [diff] [blame] | 1839 | size_t uniformSlots = 0; |
| 1840 | int childSlots = 0; |
| 1841 | for (const SkSL::ProgramElement* e : program.elements()) { |
Brian Osman | 47726a1 | 2020-12-17 16:02:08 -0500 | [diff] [blame] | 1842 | if (e->is<GlobalVarDeclaration>()) { |
| 1843 | const GlobalVarDeclaration& decl = e->as<GlobalVarDeclaration>(); |
| 1844 | const Variable& var = decl.declaration()->as<VarDeclaration>().var(); |
Brian Osman | 14d0096 | 2021-04-02 17:04:35 -0400 | [diff] [blame] | 1845 | if (var.type().isEffectChild()) { |
Brian Osman | 47726a1 | 2020-12-17 16:02:08 -0500 | [diff] [blame] | 1846 | childSlots++; |
| 1847 | } else if (is_uniform(var)) { |
John Stiles | 47b087e | 2021-04-06 13:19:35 -0400 | [diff] [blame] | 1848 | uniformSlots += var.type().slotCount(); |
Brian Osman | 47726a1 | 2020-12-17 16:02:08 -0500 | [diff] [blame] | 1849 | } |
| 1850 | } |
| 1851 | } |
Brian Osman | 0a442b7 | 2020-12-02 11:12:51 -0500 | [diff] [blame] | 1852 | |
Mike Klein | ae562bd | 2021-01-08 14:15:55 -0600 | [diff] [blame] | 1853 | skvm::Uniforms uniforms(builder->uniform(), 0); |
Brian Osman | 47726a1 | 2020-12-17 16:02:08 -0500 | [diff] [blame] | 1854 | |
| 1855 | auto new_uni = [&]() { return builder->uniformF(uniforms.pushF(0.0f)); }; |
| 1856 | |
| 1857 | // Assume identity CTM |
| 1858 | skvm::Coord device = {pun_to_F32(builder->index()), new_uni()}; |
| 1859 | skvm::Coord local = device; |
| 1860 | |
| 1861 | struct Child { |
| 1862 | skvm::Uniform addr; |
| 1863 | skvm::I32 rowBytesAsPixels; |
| 1864 | }; |
| 1865 | |
| 1866 | std::vector<Child> children; |
| 1867 | for (int i = 0; i < childSlots; ++i) { |
| 1868 | children.push_back({uniforms.pushPtr(nullptr), builder->uniform32(uniforms.push(0))}); |
| 1869 | } |
| 1870 | |
John Stiles | d9a56b9 | 2021-07-23 15:50:39 -0400 | [diff] [blame] | 1871 | auto sampleShader = [&](int i, skvm::Coord coord) { |
Mike Klein | 447f331 | 2021-02-08 09:46:59 -0600 | [diff] [blame] | 1872 | skvm::PixelFormat pixelFormat = skvm::SkColorType_to_PixelFormat(kRGBA_F32_SkColorType); |
Brian Osman | 3f904db | 2021-01-28 13:24:31 -0500 | [diff] [blame] | 1873 | skvm::I32 index = trunc(coord.x); |
| 1874 | index += trunc(coord.y) * children[i].rowBytesAsPixels; |
Brian Osman | 47726a1 | 2020-12-17 16:02:08 -0500 | [diff] [blame] | 1875 | return gather(pixelFormat, children[i].addr, index); |
| 1876 | }; |
| 1877 | |
| 1878 | std::vector<skvm::Val> uniformVals; |
| 1879 | for (size_t i = 0; i < uniformSlots; ++i) { |
| 1880 | uniformVals.push_back(new_uni().id); |
| 1881 | } |
| 1882 | |
Brian Osman | 577c606 | 2021-04-12 17:17:19 -0400 | [diff] [blame] | 1883 | skvm::Color inColor = builder->uniformColor(SkColors::kWhite, &uniforms); |
John Stiles | 50d0d09 | 2021-06-09 17:24:31 -0400 | [diff] [blame] | 1884 | skvm::Color destColor = builder->uniformColor(SkColors::kBlack, &uniforms); |
Brian Osman | 577c606 | 2021-04-12 17:17:19 -0400 | [diff] [blame] | 1885 | |
John Stiles | 50d0d09 | 2021-06-09 17:24:31 -0400 | [diff] [blame] | 1886 | skvm::Color result = SkSL::ProgramToSkVM(program, *main, builder, SkMakeSpan(uniformVals), |
John Stiles | 137482f | 2021-07-23 10:38:57 -0400 | [diff] [blame] | 1887 | device, local, inColor, destColor, sampleShader, |
John Stiles | 2955c26 | 2021-07-23 15:51:05 -0400 | [diff] [blame] | 1888 | /*sampleColorFilter=*/nullptr, |
| 1889 | /*sampleBlender=*/nullptr); |
Brian Osman | 47726a1 | 2020-12-17 16:02:08 -0500 | [diff] [blame] | 1890 | |
| 1891 | storeF(builder->varying<float>(), result.r); |
| 1892 | storeF(builder->varying<float>(), result.g); |
| 1893 | storeF(builder->varying<float>(), result.b); |
| 1894 | storeF(builder->varying<float>(), result.a); |
| 1895 | |
| 1896 | return true; |
| 1897 | |
| 1898 | } |
| 1899 | |
| 1900 | } // namespace SkSL |