47 if (
data->EventFlag == ImGuiInputTextFlags_CallbackResize) {
52 data->Buf =
s->data();
63 "uniform shader child;\n"
65 "half4 main(float2 p) {\n"
66 " return child.eval(p);\n"
78 fShaders.
push_back(std::make_pair(
"Null",
nullptr));
81 fShaders.
push_back(std::make_pair(
"Linear Gradient", shader));
85 fShaders.
push_back(std::make_pair(
"Radial Gradient", shader));
88 fShaders.
push_back(std::make_pair(
"Sweep Gradient", shader));
92 fShaders.
push_back(std::make_pair(
"Mandrill", shader));
94 fResolution = { winWidth, winHeight, 1.0f };
104bool SkSLSlide::rebuild() {
108 if (fShadertoyUniforms) {
109 sksl =
"uniform float3 iResolution;\n"
110 "uniform float iTime;\n"
111 "uniform float4 iMouse;\n";
118 constexpr char kBackupFile[] =
"sksl.bak";
119 FILE* backup = fopen(kBackupFile,
"w");
121 fwrite(fSkSL.
c_str(), 1, fSkSL.
size(), backup);
134 size_t oldSize = fEffect ? fEffect->
uniformSize() : 0;
135 fInputs.
realloc(effect->uniformSize());
136 if (effect->uniformSize() > oldSize) {
137 memset(fInputs.
get() + oldSize, 0, effect->uniformSize() - oldSize);
142 fCodeIsDirty =
false;
149 ImGui::Begin(
"SkSL",
nullptr, ImGuiWindowFlags_AlwaysVerticalScrollbar);
152 ImGuiInputTextFlags
flags = ImGuiInputTextFlags_CallbackResize;
153 ImVec2 boxSize(-1.0f, ImGui::GetTextLineHeight() * 30);
154 if (ImGui::InputTextMultiline(
"Code", fSkSL.
data(), fSkSL.
size() + 1, boxSize,
flags,
159 if (ImGui::Checkbox(
"ShaderToy Uniforms (iResolution/iTime/iMouse)", &fShadertoyUniforms)) {
163 if (fCodeIsDirty || !fEffect) {
172 bool writeTrace =
false;
173 bool writeDump =
false;
175 ImGui::InputInt2(
"Trace Coordinate (X/Y)", fTraceCoord);
176 writeTrace = ImGui::Button(
"Write Debug Trace (JSON)");
177 writeDump = ImGui::Button(
"Write Debug Dump (Human-Readable)");
181 ImVec2 mousePos = ImGui::GetMousePos();
182 if (ImGui::IsMouseDown(0)) {
183 fMousePos.
x = mousePos.x;
184 fMousePos.
y = mousePos.y;
186 if (ImGui::IsMouseClicked(0)) {
187 fMousePos.
z = mousePos.x;
188 fMousePos.
w = mousePos.y;
190 fMousePos.
z =
std::abs(fMousePos.
z) * (ImGui::IsMouseDown(0) ? 1 : -1);
191 fMousePos.
w =
std::abs(fMousePos.
w) * (ImGui::IsMouseClicked(0) ? 1 : -1);
194 char*
data = fInputs.
get() + v.offset;
195 if (v.name ==
"iResolution") {
196 memcpy(
data, &fResolution,
sizeof(fResolution));
199 if (v.name ==
"iTime") {
200 memcpy(
data, &fSeconds,
sizeof(fSeconds));
203 if (v.name ==
"iMouse") {
204 memcpy(
data, &fMousePos,
sizeof(fMousePos));
213 float*
f =
reinterpret_cast<float*
>(
data);
214 for (
int c = 0; c < v.count; ++c,
f += rows) {
216 ?
SkStringPrintf(
"%.*s[%d]", (
int)v.name.size(), v.name.data(), c)
219 ImGui::DragScalarN(
name.c_str(), ImGuiDataType_Float,
f, rows, 1.0f);
229 float*
f =
reinterpret_cast<float*
>(
data);
230 for (
int e = 0;
e < v.count; ++
e) {
231 for (
int c = 0; c < cols; ++c,
f += rows) {
233 ?
SkStringPrintf(
"%.*s[%d][%d]", (
int)v.name.size(), v.name.data(),
e, c)
234 :
SkStringPrintf(
"%.*s[%d]", (
int)v.name.size(), v.name.data(), c);
235 ImGui::DragScalarN(
name.c_str(), ImGuiDataType_Float,
f, rows, 1.0f);
245 int*
i =
reinterpret_cast<int*
>(
data);
246 for (
int c = 0; c < v.count; ++c,
i += rows) {
248 ?
SkStringPrintf(
"%.*s[%d]", (
int)v.name.size(), v.name.data(), c)
251 ImGui::DragScalarN(
name.c_str(), ImGuiDataType_S32,
i, rows, 1.0f);
260 auto curShader = std::find_if(
263 [tgt = fChildren[c.index]](
const std::pair<
const char*,
sk_sp<SkShader>>&
p) {
264 return p.second == tgt;
268 if (ImGui::BeginCombo(std::string(c.name).c_str(), curShader->first)) {
269 for (
const auto& namedShader : fShaders) {
270 if (ImGui::Selectable(namedShader.first, curShader->second == namedShader.second)) {
271 fChildren[c.index] = namedShader.second;
278 static SkColor4f gPaintColor { 1.0f, 1.0f, 1.0f , 1.0f };
279 ImGui::ColorEdit4(
"Paint Color", gPaintColor.vec());
281 ImGui::RadioButton(
"Fill", &fGeometry, kFill); ImGui::SameLine();
282 ImGui::RadioButton(
"Circle", &fGeometry, kCircle); ImGui::SameLine();
283 ImGui::RadioButton(
"RoundRect", &fGeometry, kRoundRect); ImGui::SameLine();
284 ImGui::RadioButton(
"Capsule", &fGeometry, kCapsule); ImGui::SameLine();
285 ImGui::RadioButton(
"Text", &fGeometry, kText);
295 if (writeTrace || writeDump) {
296 SkIPoint traceCoord = {fTraceCoord[0], fTraceCoord[1]};
299 shader = std::move(traced.
shader);
313 p.setColor4f(gPaintColor);
314 p.setShader(std::move(shader));
340 if (debugTrace && writeTrace) {
344 if (debugTrace && writeDump) {
346 debugTrace->
dump(&dumpFile);
351 fSeconds =
static_cast<float>(nanos * 1
E-9);
static const int points[]
constexpr SkColor SK_ColorRED
constexpr SkColor SK_ColorGREEN
constexpr SkColor SK_ColorWHITE
@ kUTF8
uses bytes to represent UTF-8 or ASCII
static int InputTextCallback(ImGuiInputTextCallbackData *data)
SK_API SkString SkStringPrintf(const char *format,...) SK_PRINTF_LIKE(1
Creates a new string and writes into it using a printf()-style format.
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
void drawSimpleText(const void *text, size_t byteLength, SkTextEncoding encoding, SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
virtual GrRecordingContext * recordingContext() const
void drawPaint(const SkPaint &paint)
SkM44 getLocalToDevice() const
void clear(SkColor color)
void drawRoundRect(const SkRect &rect, SkScalar rx, SkScalar ry, const SkPaint &paint)
void setMatrix(const SkM44 &matrix)
void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, const SkPaint &paint)
static sk_sp< SkData > MakeWithoutCopy(const void *data, size_t length)
static sk_sp< SkShader > MakeSweep(SkScalar cx, SkScalar cy, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, SkScalar startAngle, SkScalar endAngle, uint32_t flags, const SkMatrix *localMatrix)
static sk_sp< SkShader > MakeRadial(const SkPoint ¢er, SkScalar radius, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
static sk_sp< SkShader > MakeLinear(const SkPoint pts[2], const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
size_t uniformSize() const
SkSpan< const Child > children() const
sk_sp< SkShader > makeShader(sk_sp< const SkData > uniforms, sk_sp< SkShader > children[], size_t childCount, const SkMatrix *localMatrix=nullptr) const
SkSpan< const Uniform > uniforms() const
static Result MakeForShader(SkString sksl, const Options &)
static TracedShader MakeTraced(sk_sp< SkShader > shader, const SkIPoint &traceCoord)
bool animate(double nanos) override
void load(SkScalar winWidth, SkScalar winHeight) override
void draw(SkCanvas *canvas) override
virtual void writeTrace(SkWStream *w) const =0
virtual void dump(SkWStream *o) const =0
const char * data() const
void append(const char text[])
const char * c_str() const
static GrContextOptions::ShaderErrorHandler * ShaderErrorHandler()
void reset(T *ptr=nullptr)
virtual void compileError(const char *shader, const char *errors)
void realloc(size_t count)
T * reset(size_t count=0)
void resize_back(int newCount)
FlutterSemanticsFlag flags
PODArray< SkColor > colors
DEF_SWITCHES_START aot vmservice shared library name
font
Font Metadata and Metrics.
SkSamplingOptions(SkFilterMode::kLinear))
SIN Vec< N, float > abs(const Vec< N, float > &x)
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
sk_sp< SkSL::DebugTrace > debugTrace
std::shared_ptr< const fml::Mapping > data