2021-01-11 20:42:44 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2020 Google LLC
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SKSL_DSL_VAR
|
|
|
|
#define SKSL_DSL_VAR
|
|
|
|
|
2021-03-04 19:30:25 +00:00
|
|
|
#include "include/sksl/DSLExpression.h"
|
|
|
|
#include "include/sksl/DSLModifiers.h"
|
2021-03-25 21:49:08 +00:00
|
|
|
#include "include/sksl/DSLType.h"
|
2021-01-11 20:42:44 +00:00
|
|
|
|
|
|
|
namespace SkSL {
|
|
|
|
|
2021-05-18 14:12:58 +00:00
|
|
|
class IRGenerator;
|
2021-01-11 20:42:44 +00:00
|
|
|
class Variable;
|
2021-03-25 21:49:08 +00:00
|
|
|
enum class VariableStorage : int8_t;
|
2021-01-11 20:42:44 +00:00
|
|
|
|
|
|
|
namespace dsl {
|
|
|
|
|
|
|
|
class DSLVar {
|
|
|
|
public:
|
2021-04-26 13:35:10 +00:00
|
|
|
/**
|
|
|
|
* Creates an empty, unpopulated DSLVar. Can be replaced with a real DSLVar later via `swap`.
|
|
|
|
*/
|
|
|
|
DSLVar() : fType(kVoid_Type), fDeclared(true) {}
|
|
|
|
|
2021-01-11 20:42:44 +00:00
|
|
|
/**
|
|
|
|
* Constructs a new variable with the specified type and name. The name is used (in mangled
|
|
|
|
* form) in the resulting shader code; it is not otherwise important. Since mangling prevents
|
|
|
|
* name conflicts and the variable's name is only important when debugging shaders, the name
|
|
|
|
* parameter is optional.
|
|
|
|
*/
|
2021-06-23 17:51:55 +00:00
|
|
|
DSLVar(DSLType type, skstd::string_view name = "var",
|
|
|
|
DSLExpression initialValue = DSLExpression());
|
|
|
|
|
|
|
|
DSLVar(DSLType type, const char* name, DSLExpression initialValue = DSLExpression())
|
|
|
|
: DSLVar(type, skstd::string_view(name), std::move(initialValue)) {}
|
2021-01-11 20:42:44 +00:00
|
|
|
|
2021-03-05 19:23:48 +00:00
|
|
|
DSLVar(DSLType type, DSLExpression initialValue);
|
|
|
|
|
2021-06-23 17:51:55 +00:00
|
|
|
DSLVar(DSLModifiers modifiers, DSLType type, skstd::string_view name = "var",
|
2021-03-05 19:23:48 +00:00
|
|
|
DSLExpression initialValue = DSLExpression());
|
|
|
|
|
2021-06-23 17:51:55 +00:00
|
|
|
DSLVar(DSLModifiers modifiers, DSLType type, const char* name,
|
|
|
|
DSLExpression initialValue = DSLExpression())
|
|
|
|
: DSLVar(modifiers, type, skstd::string_view(name), std::move(initialValue)) {}
|
|
|
|
|
2021-03-05 19:23:48 +00:00
|
|
|
DSLVar(DSLModifiers modifiers, DSLType type, DSLExpression initialValue);
|
2021-01-27 12:53:46 +00:00
|
|
|
|
2021-05-04 16:22:02 +00:00
|
|
|
DSLVar(DSLVar&&) = default;
|
2021-01-11 20:42:44 +00:00
|
|
|
|
2021-03-04 19:30:25 +00:00
|
|
|
~DSLVar();
|
|
|
|
|
2021-06-23 17:51:55 +00:00
|
|
|
skstd::string_view name() const {
|
2021-04-16 18:54:43 +00:00
|
|
|
return fName;
|
|
|
|
}
|
|
|
|
|
2021-06-23 14:27:09 +00:00
|
|
|
DSLModifiers modifiers() const {
|
|
|
|
return fModifiers;
|
|
|
|
}
|
|
|
|
|
2021-04-26 13:35:10 +00:00
|
|
|
void swap(DSLVar& other);
|
|
|
|
|
2021-01-26 19:31:29 +00:00
|
|
|
DSLExpression x() {
|
|
|
|
return DSLExpression(*this).x();
|
|
|
|
}
|
|
|
|
|
|
|
|
DSLExpression y() {
|
|
|
|
return DSLExpression(*this).y();
|
|
|
|
}
|
|
|
|
|
|
|
|
DSLExpression z() {
|
|
|
|
return DSLExpression(*this).z();
|
|
|
|
}
|
|
|
|
|
|
|
|
DSLExpression w() {
|
|
|
|
return DSLExpression(*this).w();
|
|
|
|
}
|
|
|
|
|
|
|
|
DSLExpression r() {
|
|
|
|
return DSLExpression(*this).r();
|
|
|
|
}
|
|
|
|
|
|
|
|
DSLExpression g() {
|
|
|
|
return DSLExpression(*this).g();
|
|
|
|
}
|
|
|
|
|
|
|
|
DSLExpression b() {
|
|
|
|
return DSLExpression(*this).b();
|
|
|
|
}
|
|
|
|
|
|
|
|
DSLExpression a() {
|
|
|
|
return DSLExpression(*this).a();
|
|
|
|
}
|
|
|
|
|
2021-06-23 17:51:55 +00:00
|
|
|
DSLExpression field(skstd::string_view name) {
|
2021-02-11 20:18:31 +00:00
|
|
|
return DSLExpression(*this).field(name);
|
|
|
|
}
|
|
|
|
|
2021-03-25 21:49:08 +00:00
|
|
|
DSLPossibleExpression operator=(DSLVar& var) {
|
2021-01-13 15:38:59 +00:00
|
|
|
return this->operator=(DSLExpression(var));
|
|
|
|
}
|
|
|
|
|
2021-02-26 01:50:32 +00:00
|
|
|
DSLPossibleExpression operator=(DSLExpression expr);
|
2021-01-13 15:38:59 +00:00
|
|
|
|
2021-02-26 01:50:32 +00:00
|
|
|
DSLPossibleExpression operator=(int expr) {
|
2021-01-13 15:38:59 +00:00
|
|
|
return this->operator=(DSLExpression(expr));
|
|
|
|
}
|
|
|
|
|
2021-02-26 01:50:32 +00:00
|
|
|
DSLPossibleExpression operator=(float expr) {
|
2021-01-13 15:38:59 +00:00
|
|
|
return this->operator=(DSLExpression(expr));
|
|
|
|
}
|
|
|
|
|
2021-04-23 20:15:11 +00:00
|
|
|
DSLPossibleExpression operator=(double expr) {
|
|
|
|
return this->operator=(DSLExpression(expr));
|
|
|
|
}
|
|
|
|
|
2021-02-26 01:50:32 +00:00
|
|
|
DSLPossibleExpression operator[](DSLExpression&& index);
|
2021-01-26 15:07:01 +00:00
|
|
|
|
2021-02-26 01:50:32 +00:00
|
|
|
DSLPossibleExpression operator++() {
|
2021-01-13 15:38:59 +00:00
|
|
|
return ++DSLExpression(*this);
|
|
|
|
}
|
|
|
|
|
2021-02-26 01:50:32 +00:00
|
|
|
DSLPossibleExpression operator++(int) {
|
2021-01-13 15:38:59 +00:00
|
|
|
return DSLExpression(*this)++;
|
|
|
|
}
|
|
|
|
|
2021-02-26 01:50:32 +00:00
|
|
|
DSLPossibleExpression operator--() {
|
|
|
|
return --DSLExpression(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
DSLPossibleExpression operator--(int) {
|
|
|
|
return DSLExpression(*this)--;
|
|
|
|
}
|
|
|
|
|
2021-01-11 20:42:44 +00:00
|
|
|
private:
|
|
|
|
/**
|
|
|
|
* Constructs a reference to a variable that already exists in the symbol table. This is used
|
|
|
|
* internally to reference built-in vars.
|
|
|
|
*/
|
|
|
|
DSLVar(const char* name);
|
|
|
|
|
2021-03-25 21:49:08 +00:00
|
|
|
DSLModifiers fModifiers;
|
|
|
|
// We only need to keep track of the type here so that we can create the SkSL::Variable. For
|
|
|
|
// predefined variables this field is unnecessary, so we don't bother tracking it and just set
|
|
|
|
// it to kVoid; in other words, you shouldn't generally be relying on this field to be correct.
|
|
|
|
// If you need to determine the variable's type, look at DSLWriter::Var(...).type() instead.
|
|
|
|
DSLType fType;
|
2021-04-26 13:35:10 +00:00
|
|
|
int fUniformHandle = -1;
|
2021-01-22 20:18:25 +00:00
|
|
|
std::unique_ptr<SkSL::Statement> fDeclaration;
|
2021-03-25 21:49:08 +00:00
|
|
|
const SkSL::Variable* fVar = nullptr;
|
2021-06-23 17:51:55 +00:00
|
|
|
skstd::string_view fRawName; // for error reporting
|
|
|
|
skstd::string_view fName;
|
2021-03-25 21:49:08 +00:00
|
|
|
DSLExpression fInitialValue;
|
|
|
|
VariableStorage fStorage;
|
|
|
|
bool fDeclared = false;
|
2021-01-11 20:42:44 +00:00
|
|
|
|
2021-02-17 20:52:09 +00:00
|
|
|
friend DSLVar sk_SampleCoord();
|
|
|
|
|
2021-01-22 20:18:25 +00:00
|
|
|
friend class DSLCore;
|
2021-01-11 20:42:44 +00:00
|
|
|
friend class DSLExpression;
|
2021-03-16 20:37:29 +00:00
|
|
|
friend class DSLFunction;
|
2021-01-13 15:38:59 +00:00
|
|
|
friend class DSLWriter;
|
2021-05-18 14:12:58 +00:00
|
|
|
friend class ::SkSL::IRGenerator;
|
2021-01-11 20:42:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dsl
|
|
|
|
|
|
|
|
} // namespace SkSL
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|