Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 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 | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 8 | #include "include/sksl/DSLStatement.h" |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 9 | |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 10 | #include "include/sksl/DSLBlock.h" |
| 11 | #include "include/sksl/DSLExpression.h" |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 12 | #include "src/sksl/SkSLCompiler.h" |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 13 | #include "src/sksl/dsl/priv/DSLWriter.h" |
Ethan Nicholas | db2326b | 2021-04-19 10:55:18 -0400 | [diff] [blame] | 14 | #include "src/sksl/ir/SkSLBlock.h" |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 15 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 16 | #include "src/sksl/ir/SkSLNop.h" |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 17 | |
Ethan Nicholas | 3af656b | 2021-02-17 11:17:56 -0500 | [diff] [blame] | 18 | #if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU |
| 19 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 20 | #endif |
| 21 | |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 22 | namespace SkSL { |
| 23 | |
| 24 | namespace dsl { |
| 25 | |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 26 | DSLStatement::DSLStatement() {} |
| 27 | |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 28 | DSLStatement::DSLStatement(DSLBlock block) |
| 29 | : fStatement(block.release()) {} |
| 30 | |
| 31 | DSLStatement::DSLStatement(DSLExpression expr) { |
| 32 | std::unique_ptr<SkSL::Expression> skslExpr = expr.release(); |
| 33 | if (skslExpr) { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 34 | fStatement = SkSL::ExpressionStatement::Make(DSLWriter::Context(), std::move(skslExpr)); |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 35 | } |
| 36 | } |
| 37 | |
| 38 | DSLStatement::DSLStatement(std::unique_ptr<SkSL::Expression> expr) |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 39 | : fStatement(SkSL::ExpressionStatement::Make(DSLWriter::Context(), std::move(expr))) { |
Ethan Nicholas | 51b4b86 | 2021-08-31 16:12:40 -0400 | [diff] [blame] | 40 | SkASSERT(this->hasValue()); |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 41 | } |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 42 | |
| 43 | DSLStatement::DSLStatement(std::unique_ptr<SkSL::Statement> stmt) |
| 44 | : fStatement(std::move(stmt)) { |
Ethan Nicholas | 51b4b86 | 2021-08-31 16:12:40 -0400 | [diff] [blame] | 45 | SkASSERT(this->hasValue()); |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 46 | } |
| 47 | |
Ethan Nicholas | b956304 | 2021-02-25 09:45:49 -0500 | [diff] [blame] | 48 | DSLStatement::DSLStatement(DSLPossibleExpression expr, PositionInfo pos) |
| 49 | : DSLStatement(DSLExpression(std::move(expr), pos)) {} |
| 50 | |
| 51 | DSLStatement::DSLStatement(DSLPossibleStatement stmt, PositionInfo pos) { |
Ethan Nicholas | b4f8b7a | 2021-06-23 10:27:09 -0400 | [diff] [blame] | 52 | DSLWriter::ReportErrors(pos); |
Ethan Nicholas | 51b4b86 | 2021-08-31 16:12:40 -0400 | [diff] [blame] | 53 | if (stmt.hasValue()) { |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 54 | fStatement = std::move(stmt.fStatement); |
| 55 | } else { |
| 56 | fStatement = SkSL::Nop::Make(); |
| 57 | } |
Brian Osman | cc91452 | 2021-09-24 18:58:37 +0000 | [diff] [blame] | 58 | if (pos.offset() != -1) { |
| 59 | fStatement->fOffset = pos.offset(); |
Ethan Nicholas | 81b7060 | 2021-09-15 13:28:29 -0400 | [diff] [blame] | 60 | } |
Ethan Nicholas | b956304 | 2021-02-25 09:45:49 -0500 | [diff] [blame] | 61 | } |
| 62 | |
Ethan Nicholas | 3af656b | 2021-02-17 11:17:56 -0500 | [diff] [blame] | 63 | DSLStatement::~DSLStatement() { |
| 64 | #if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU |
| 65 | if (fStatement && DSLWriter::InFragmentProcessor()) { |
| 66 | DSLWriter::CurrentEmitArgs()->fFragBuilder->codeAppend(this->release()); |
| 67 | return; |
| 68 | } |
| 69 | #endif |
Ethan Nicholas | 459777a | 2021-07-16 11:16:27 -0400 | [diff] [blame] | 70 | SkASSERTF(!fStatement || !DSLWriter::Settings().fAssertDSLObjectsReleased, |
| 71 | "Statement destroyed without being incorporated into program (see " |
| 72 | "ProgramSettings::fAssertDSLObjectsReleased)"); |
Ethan Nicholas | 3af656b | 2021-02-17 11:17:56 -0500 | [diff] [blame] | 73 | } |
| 74 | |
Ethan Nicholas | b956304 | 2021-02-25 09:45:49 -0500 | [diff] [blame] | 75 | DSLPossibleStatement::DSLPossibleStatement(std::unique_ptr<SkSL::Statement> statement) |
| 76 | : fStatement(std::move(statement)) {} |
| 77 | |
| 78 | DSLPossibleStatement::~DSLPossibleStatement() { |
| 79 | if (fStatement) { |
| 80 | // this handles incorporating the expression into the output tree |
| 81 | DSLStatement(std::move(fStatement)); |
| 82 | } |
| 83 | } |
| 84 | |
Ethan Nicholas | db2326b | 2021-04-19 10:55:18 -0400 | [diff] [blame] | 85 | DSLStatement operator,(DSLStatement left, DSLStatement right) { |
Ethan Nicholas | 5fad2b8 | 2021-09-27 10:39:18 -0400 | [diff] [blame^] | 86 | int line = left.fStatement->fOffset; |
Ethan Nicholas | db2326b | 2021-04-19 10:55:18 -0400 | [diff] [blame] | 87 | StatementArray stmts; |
| 88 | stmts.reserve_back(2); |
| 89 | stmts.push_back(left.release()); |
| 90 | stmts.push_back(right.release()); |
Ethan Nicholas | 5fad2b8 | 2021-09-27 10:39:18 -0400 | [diff] [blame^] | 91 | return DSLStatement(SkSL::Block::MakeUnscoped(line, std::move(stmts))); |
Ethan Nicholas | db2326b | 2021-04-19 10:55:18 -0400 | [diff] [blame] | 92 | } |
| 93 | |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 94 | } // namespace dsl |
| 95 | |
| 96 | } // namespace SkSL |