blob: 61ea6db2abccb401cd874abd13eeb95969752153 [file] [log] [blame]
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +00001/*
2 * Copyright 2013 Google Inc.
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBlendMode.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040011#include "include/core/SkColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkColorFilter.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040013#include "include/core/SkMatrix.h"
14#include "include/core/SkPaint.h"
15#include "include/core/SkPoint.h"
16#include "include/core/SkRefCnt.h"
17#include "include/core/SkScalar.h"
18#include "include/core/SkShader.h"
19#include "include/core/SkSize.h"
20#include "include/core/SkString.h"
21#include "include/core/SkTileMode.h"
22#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "include/core/SkVertices.h"
24#include "include/effects/SkGradientShader.h"
Brian Osman3c358422020-03-23 10:44:12 -040025#include "include/effects/SkRuntimeEffect.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040026#include "include/private/SkTDArray.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "include/utils/SkRandom.h"
Brian Osman3c358422020-03-23 10:44:12 -040028#include "src/core/SkVerticesPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/shaders/SkLocalMatrixShader.h"
Brian Osman3c358422020-03-23 10:44:12 -040030#include "src/utils/SkPatchUtils.h"
31#include "tools/Resources.h"
Brian Osmane41fa2d2020-03-20 15:45:49 -040032#include "tools/ToolUtils.h"
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000033
Ben Wagner7fde8e12019-05-01 17:28:53 -040034#include <initializer_list>
35#include <utility>
36
Brian Salomon3f363692017-02-02 21:05:19 -050037static constexpr SkScalar kShaderSize = 40;
Florin Malitaf614ba22017-02-16 22:12:41 -050038static sk_sp<SkShader> make_shader1(SkScalar shaderScale) {
reed@google.com85e143c2013-12-30 15:51:25 +000039 const SkColor colors[] = {
40 SK_ColorRED, SK_ColorCYAN, SK_ColorGREEN, SK_ColorWHITE,
41 SK_ColorMAGENTA, SK_ColorBLUE, SK_ColorYELLOW,
42 };
Brian Salomon3f363692017-02-02 21:05:19 -050043 const SkPoint pts[] = {{kShaderSize / 4, 0}, {3 * kShaderSize / 4, kShaderSize}};
Mike Reed1f607332020-05-21 12:11:27 -040044 const SkMatrix localMatrix = SkMatrix::Scale(shaderScale, shaderScale);
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000045
Florin Malitaf614ba22017-02-16 22:12:41 -050046 sk_sp<SkShader> grad = SkGradientShader::MakeLinear(pts, colors, nullptr,
47 SK_ARRAY_COUNT(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -040048 SkTileMode::kMirror, 0,
Florin Malitaf614ba22017-02-16 22:12:41 -050049 &localMatrix);
50 // Throw in a couple of local matrix wrappers for good measure.
51 return shaderScale == 1
52 ? grad
53 : sk_make_sp<SkLocalMatrixShader>(
Mike Reed1f607332020-05-21 12:11:27 -040054 sk_make_sp<SkLocalMatrixShader>(std::move(grad), SkMatrix::Translate(-10, 0)),
55 SkMatrix::Translate(10, 0));
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +000056}
57
reed1a9b9642016-03-13 14:13:58 -070058static sk_sp<SkShader> make_shader2() {
Mike Reedc8bea7d2019-04-09 13:55:36 -040059 return SkShaders::Color(SK_ColorBLUE);
bsalomon093779c2016-03-08 11:53:31 -080060}
61
reedd053ce92016-03-22 10:17:23 -070062static sk_sp<SkColorFilter> make_color_filter() {
Mike Reedb286bc22019-04-08 16:23:20 -040063 return SkColorFilters::Blend(0xFFAABBCC, SkBlendMode::kDarken);
bsalomon093779c2016-03-08 11:53:31 -080064}
65
Brian Salomon3f363692017-02-02 21:05:19 -050066static constexpr SkScalar kMeshSize = 30;
67
68// start with the center of a 3x3 grid of vertices.
69static constexpr uint16_t kMeshFan[] = {
70 4,
71 0, 1, 2, 5, 8, 7, 6, 3, 0
72};
73
Brian Salomon199fb872017-02-06 09:41:10 -050074static const int kMeshIndexCnt = (int)SK_ARRAY_COUNT(kMeshFan);
Brian Salomon3f363692017-02-02 21:05:19 -050075static const int kMeshVertexCnt = 9;
76
77static void fill_mesh(SkPoint pts[kMeshVertexCnt], SkPoint texs[kMeshVertexCnt],
Florin Malitaf614ba22017-02-16 22:12:41 -050078 SkColor colors[kMeshVertexCnt], SkScalar shaderScale) {
Brian Salomon3f363692017-02-02 21:05:19 -050079 pts[0].set(0, 0);
80 pts[1].set(kMeshSize / 2, 3);
81 pts[2].set(kMeshSize, 0);
82 pts[3].set(3, kMeshSize / 2);
83 pts[4].set(kMeshSize / 2, kMeshSize / 2);
84 pts[5].set(kMeshSize - 3, kMeshSize / 2);
85 pts[6].set(0, kMeshSize);
86 pts[7].set(kMeshSize / 2, kMeshSize - 3);
87 pts[8].set(kMeshSize, kMeshSize);
88
Florin Malitaf614ba22017-02-16 22:12:41 -050089 const auto shaderSize = kShaderSize * shaderScale;
Brian Salomon3f363692017-02-02 21:05:19 -050090 texs[0].set(0, 0);
Florin Malitaf614ba22017-02-16 22:12:41 -050091 texs[1].set(shaderSize / 2, 0);
92 texs[2].set(shaderSize, 0);
93 texs[3].set(0, shaderSize / 2);
94 texs[4].set(shaderSize / 2, shaderSize / 2);
95 texs[5].set(shaderSize, shaderSize / 2);
96 texs[6].set(0, shaderSize);
97 texs[7].set(shaderSize / 2, shaderSize);
98 texs[8].set(shaderSize, shaderSize);
Brian Salomon3f363692017-02-02 21:05:19 -050099
100 SkRandom rand;
101 for (size_t i = 0; i < kMeshVertexCnt; ++i) {
102 colors[i] = rand.nextU() | 0xFF000000;
103 }
104}
105
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000106class VerticesGM : public skiagm::GM {
Brian Salomon3f363692017-02-02 21:05:19 -0500107 SkPoint fPts[kMeshVertexCnt];
108 SkPoint fTexs[kMeshVertexCnt];
109 SkColor fColors[kMeshVertexCnt];
reedd053ce92016-03-22 10:17:23 -0700110 sk_sp<SkShader> fShader1;
111 sk_sp<SkShader> fShader2;
112 sk_sp<SkColorFilter> fColorFilter;
Florin Malitaf614ba22017-02-16 22:12:41 -0500113 SkScalar fShaderScale;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000114
115public:
Mike Reed887cdf12017-04-03 11:11:09 -0400116 VerticesGM(SkScalar shaderScale) : fShaderScale(shaderScale) {}
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000117
118protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000119
mtklein36352bf2015-03-25 18:17:31 -0700120 void onOnceBeforeDraw() override {
Florin Malitaf614ba22017-02-16 22:12:41 -0500121 fill_mesh(fPts, fTexs, fColors, fShaderScale);
122 fShader1 = make_shader1(fShaderScale);
reed1a9b9642016-03-13 14:13:58 -0700123 fShader2 = make_shader2();
reedd053ce92016-03-22 10:17:23 -0700124 fColorFilter = make_color_filter();
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000125 }
126
mtklein36352bf2015-03-25 18:17:31 -0700127 SkString onShortName() override {
reed@google.com60da8f32014-05-05 20:41:21 +0000128 SkString name("vertices");
Florin Malitaf614ba22017-02-16 22:12:41 -0500129 if (fShaderScale != 1) {
130 name.append("_scaled_shader");
131 }
reed@google.com60da8f32014-05-05 20:41:21 +0000132 return name;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000133 }
134
mtklein36352bf2015-03-25 18:17:31 -0700135 SkISize onISize() override {
Brian Salomon32161802017-02-10 10:48:15 -0500136 return SkISize::Make(975, 1175);
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000137 }
138
mtklein36352bf2015-03-25 18:17:31 -0700139 void onDraw(SkCanvas* canvas) override {
Mike Reed7d954ad2016-10-28 15:42:34 -0400140 const SkBlendMode modes[] = {
141 SkBlendMode::kClear,
142 SkBlendMode::kSrc,
143 SkBlendMode::kDst,
144 SkBlendMode::kSrcOver,
145 SkBlendMode::kDstOver,
146 SkBlendMode::kSrcIn,
147 SkBlendMode::kDstIn,
148 SkBlendMode::kSrcOut,
149 SkBlendMode::kDstOut,
150 SkBlendMode::kSrcATop,
151 SkBlendMode::kDstATop,
152 SkBlendMode::kXor,
153 SkBlendMode::kPlus,
154 SkBlendMode::kModulate,
155 SkBlendMode::kScreen,
156 SkBlendMode::kOverlay,
157 SkBlendMode::kDarken,
158 SkBlendMode::kLighten,
159 SkBlendMode::kColorDodge,
160 SkBlendMode::kColorBurn,
161 SkBlendMode::kHardLight,
162 SkBlendMode::kSoftLight,
163 SkBlendMode::kDifference,
164 SkBlendMode::kExclusion,
165 SkBlendMode::kMultiply,
166 SkBlendMode::kHue,
167 SkBlendMode::kSaturation,
168 SkBlendMode::kColor,
169 SkBlendMode::kLuminosity,
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000170 };
skia.committer@gmail.com4c912862013-12-30 07:01:37 +0000171
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000172 SkPaint paint;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000173
bsalomon093779c2016-03-08 11:53:31 -0800174 canvas->translate(4, 4);
175 int x = 0;
Brian Salomon199fb872017-02-06 09:41:10 -0500176 for (auto mode : modes) {
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000177 canvas->save();
Mike Reed9407e242019-02-15 16:13:57 -0500178 for (float alpha : {1.0f, 0.5f}) {
Brian Salomon199fb872017-02-06 09:41:10 -0500179 for (const auto& cf : {sk_sp<SkColorFilter>(nullptr), fColorFilter}) {
180 for (const auto& shader : {fShader1, fShader2}) {
181 static constexpr struct {
182 bool fHasColors;
183 bool fHasTexs;
184 } kAttrs[] = {{true, false}, {false, true}, {true, true}};
185 for (auto attrs : kAttrs) {
186 paint.setShader(shader);
187 paint.setColorFilter(cf);
Mike Reed9407e242019-02-15 16:13:57 -0500188 paint.setAlphaf(alpha);
Mike Reed5fa66452017-03-16 09:06:34 -0400189
190 const SkColor* colors = attrs.fHasColors ? fColors : nullptr;
191 const SkPoint* texs = attrs.fHasTexs ? fTexs : nullptr;
Mike Reed887cdf12017-04-03 11:11:09 -0400192 auto v = SkVertices::MakeCopy(SkVertices::kTriangleFan_VertexMode,
193 kMeshVertexCnt, fPts, texs, colors,
194 kMeshIndexCnt, kMeshFan);
195 canvas->drawVertices(v, mode, paint);
Brian Salomon199fb872017-02-06 09:41:10 -0500196 canvas->translate(40, 0);
197 ++x;
198 }
199 }
200 }
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000201 }
202 canvas->restore();
bsalomon093779c2016-03-08 11:53:31 -0800203 canvas->translate(0, 40);
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000204 }
205 }
206
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000207private:
John Stiles7571f9e2020-09-02 22:42:33 -0400208 using INHERITED = skiagm::GM;
mike@reedtribe.org0c87ea82013-12-30 04:07:34 +0000209};
210
reed@google.com60da8f32014-05-05 20:41:21 +0000211/////////////////////////////////////////////////////////////////////////////////////
212
Mike Reed887cdf12017-04-03 11:11:09 -0400213DEF_GM(return new VerticesGM(1);)
214DEF_GM(return new VerticesGM(1 / kShaderSize);)
Brian Salomon3f363692017-02-02 21:05:19 -0500215
Mike Reed887cdf12017-04-03 11:11:09 -0400216static void draw_batching(SkCanvas* canvas) {
Mike Reed97eb4fe2017-03-14 12:04:16 -0400217 // Triangle fans can't batch so we convert to regular triangles,
218 static constexpr int kNumTris = kMeshIndexCnt - 2;
Mike Reed887cdf12017-04-03 11:11:09 -0400219 SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, kMeshVertexCnt, 3 * kNumTris,
Mike Reedaa9e3322017-03-16 14:38:48 -0400220 SkVertices::kHasColors_BuilderFlag |
221 SkVertices::kHasTexCoords_BuilderFlag);
Mike Reed97eb4fe2017-03-14 12:04:16 -0400222
223 SkPoint* pts = builder.positions();
224 SkPoint* texs = builder.texCoords();
225 SkColor* colors = builder.colors();
226 fill_mesh(pts, texs, colors, 1);
Brian Salomon199fb872017-02-06 09:41:10 -0500227
Brian Salomon3f363692017-02-02 21:05:19 -0500228 SkTDArray<SkMatrix> matrices;
229 matrices.push()->reset();
230 matrices.push()->setTranslate(0, 40);
231 SkMatrix* m = matrices.push();
232 m->setRotate(45, kMeshSize / 2, kMeshSize / 2);
233 m->postScale(1.2f, .8f, kMeshSize / 2, kMeshSize / 2);
234 m->postTranslate(0, 80);
235
Florin Malitaf614ba22017-02-16 22:12:41 -0500236 auto shader = make_shader1(1);
Brian Salomon3f363692017-02-02 21:05:19 -0500237
Mike Reed97eb4fe2017-03-14 12:04:16 -0400238 uint16_t* indices = builder.indices();
Brian Salomon3f363692017-02-02 21:05:19 -0500239 for (size_t i = 0; i < kNumTris; ++i) {
240 indices[3 * i] = kMeshFan[0];
241 indices[3 * i + 1] = kMeshFan[i + 1];
242 indices[3 * i + 2] = kMeshFan[i + 2];
Mike Reed97eb4fe2017-03-14 12:04:16 -0400243
Brian Salomon3f363692017-02-02 21:05:19 -0500244 }
Brian Salomon199fb872017-02-06 09:41:10 -0500245
Brian Salomon199fb872017-02-06 09:41:10 -0500246 canvas->save();
Brian Salomon3f363692017-02-02 21:05:19 -0500247 canvas->translate(10, 10);
248 for (bool useShader : {false, true}) {
249 for (bool useTex : {false, true}) {
250 for (const auto& m : matrices) {
251 canvas->save();
252 canvas->concat(m);
253 SkPaint paint;
Brian Salomon3f363692017-02-02 21:05:19 -0500254 paint.setShader(useShader ? shader : nullptr);
Mike Reed5fa66452017-03-16 09:06:34 -0400255
256 const SkPoint* t = useTex ? texs : nullptr;
Mike Reed887cdf12017-04-03 11:11:09 -0400257 auto v = SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode, kMeshVertexCnt,
258 pts, t, colors, kNumTris * 3, indices);
259 canvas->drawVertices(v, SkBlendMode::kModulate, paint);
Brian Salomon3f363692017-02-02 21:05:19 -0500260 canvas->restore();
261 }
262 canvas->translate(0, 120);
263 }
264 }
Brian Salomon199fb872017-02-06 09:41:10 -0500265 canvas->restore();
266}
267
268// This test exists to exercise batching in the gpu backend.
269DEF_SIMPLE_GM(vertices_batching, canvas, 100, 500) {
Mike Reed887cdf12017-04-03 11:11:09 -0400270 draw_batching(canvas);
Brian Salomon199fb872017-02-06 09:41:10 -0500271 canvas->translate(50, 0);
Mike Reed887cdf12017-04-03 11:11:09 -0400272 draw_batching(canvas);
Brian Salomon3f363692017-02-02 21:05:19 -0500273}
Mike Reedd32bdaf2020-03-06 12:52:52 -0500274
Brian Osmanffd11f4a2020-03-30 09:57:53 -0400275using AttrType = SkVertices::Attribute::Type;
Mike Reedd32bdaf2020-03-06 12:52:52 -0500276
Brian Osmanffd11f4a2020-03-30 09:57:53 -0400277DEF_SIMPLE_GM(vertices_data, canvas, 512, 256) {
278 for (auto attrType : {AttrType::kFloat4, AttrType::kByte4_unorm}) {
279 SkRect r = SkRect::MakeWH(256, 256);
280 int vcount = 4; // just a quad
281 int icount = 0;
282 SkVertices::Attribute attrs[] = { attrType };
283 SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, vcount, icount, attrs, 1);
Mike Reedd32bdaf2020-03-06 12:52:52 -0500284
Brian Osmanffd11f4a2020-03-30 09:57:53 -0400285 r.toQuad(builder.positions());
Mike Reedd32bdaf2020-03-06 12:52:52 -0500286
Brian Osmanffd11f4a2020-03-30 09:57:53 -0400287 if (attrType == AttrType::kFloat4) {
288 SkV4* col = (SkV4*)builder.customData();
289 col[0] = {1, 0, 0, 1}; // red
290 col[1] = {0, 1, 0, 1}; // green
291 col[2] = {0, 0, 1, 1}; // blue
292 col[3] = {0.5, 0.5, 0.5, 1}; // gray
293 } else {
294 uint32_t* col = (uint32_t*)builder.customData();
295 col[0] = 0xFF0000FF;
296 col[1] = 0xFF00FF00;
297 col[2] = 0xFFFF0000;
298 col[3] = 0xFF7F7F7F;
Brian Osmanf11e3312020-03-24 14:57:38 -0400299 }
Brian Osmanffd11f4a2020-03-30 09:57:53 -0400300
301 SkPaint paint;
302 const char* gProg = R"(
303 varying float4 vtx_color;
Brian Osman767f4442020-08-13 16:59:48 -0400304 half4 main(float2 p) {
305 return half4(vtx_color);
Brian Osmanffd11f4a2020-03-30 09:57:53 -0400306 }
307 )";
308 auto[effect, errorText] = SkRuntimeEffect::Make(SkString(gProg));
Ethan Nicholas63d7ee32020-08-17 10:57:12 -0400309 if (!effect) {
310 SK_ABORT("RuntimeEffect error: %s\n", errorText.c_str());
311 }
Brian Osmanffd11f4a2020-03-30 09:57:53 -0400312 paint.setShader(effect->makeShader(nullptr, nullptr, 0, nullptr, true));
313 canvas->drawVertices(builder.detach(), paint);
314 canvas->translate(r.width(), 0);
315 }
Mike Reedd32bdaf2020-03-06 12:52:52 -0500316}
Brian Osmane41fa2d2020-03-20 15:45:49 -0400317
318// Test case for skbug.com/10069. We need to draw the vertices twice (with different matrices) to
319// trigger the bug.
320DEF_SIMPLE_GM(vertices_perspective, canvas, 256, 256) {
321 SkPaint paint;
322 paint.setShader(ToolUtils::create_checkerboard_shader(SK_ColorBLACK, SK_ColorWHITE, 32));
323
324 SkRect r = SkRect::MakeWH(128, 128);
325
326 SkPoint pos[4];
327 r.toQuad(pos);
328 auto verts = SkVertices::MakeCopy(SkVertices::kTriangleFan_VertexMode, 4, pos, pos, nullptr);
329
330 SkMatrix persp;
331 persp.setPerspY(SK_Scalar1 / 100);
332
333 canvas->save();
334 canvas->concat(persp);
335 canvas->drawRect(r, paint);
336 canvas->restore();
337
338 canvas->save();
339 canvas->translate(r.width(), 0);
340 canvas->concat(persp);
341 canvas->drawRect(r, paint);
342 canvas->restore();
343
344 canvas->save();
345 canvas->translate(0, r.height());
346 canvas->concat(persp);
347 canvas->drawVertices(verts, paint);
348 canvas->restore();
349
350 canvas->save();
351 canvas->translate(r.width(), r.height());
352 canvas->concat(persp);
353 canvas->drawVertices(verts, paint);
354 canvas->restore();
355}
Brian Osman3c358422020-03-23 10:44:12 -0400356
357DEF_SIMPLE_GM(vertices_data_lerp, canvas, 256, 256) {
358 SkPoint pts[12] = {{0, 0}, {85, 0}, {171, 0}, {256, 0}, {256, 85}, {256, 171},
359 {256, 256}, {171, 256}, {85, 256}, {0, 256}, {0, 171}, {0, 85}};
360
361 auto patchVerts = SkPatchUtils::MakeVertices(pts, nullptr, nullptr, 12, 12);
Brian Osman8cbedf92020-03-31 10:38:31 -0400362 SkVerticesPriv pv(patchVerts->priv());
Brian Osman3c358422020-03-23 10:44:12 -0400363
Brian Osmanffd11f4a2020-03-30 09:57:53 -0400364 SkVertices::Attribute attrs[1] = { AttrType::kFloat };
365 SkVertices::Builder builder(pv.mode(), pv.vertexCount(), pv.indexCount(), attrs, 1);
Brian Osman3c358422020-03-23 10:44:12 -0400366
Brian Osman8cbedf92020-03-31 10:38:31 -0400367 memcpy(builder.positions(), pv.positions(), pv.vertexCount() * sizeof(SkPoint));
368 memcpy(builder.indices(), pv.indices(), pv.indexCount() * sizeof(uint16_t));
Brian Osman3c358422020-03-23 10:44:12 -0400369
370 SkRandom rnd;
Brian Osmanffd11f4a2020-03-30 09:57:53 -0400371 float* lerpData = (float*)builder.customData();
Brian Osman8cbedf92020-03-31 10:38:31 -0400372 for (int i = 0; i < pv.vertexCount(); ++i) {
Brian Osmanffd11f4a2020-03-30 09:57:53 -0400373 lerpData[i] = rnd.nextBool() ? 1.0f : 0.0f;
Brian Osman3c358422020-03-23 10:44:12 -0400374 }
375
376 auto verts = builder.detach();
377
378 SkPaint paint;
379 const char* gProg = R"(
Brian Osman28590d52020-03-23 16:59:08 -0400380 in shader c0;
381 in shader c1;
Brian Osman3c358422020-03-23 10:44:12 -0400382 varying float vtx_lerp;
Brian Osman767f4442020-08-13 16:59:48 -0400383 half4 main(float2 p) {
Brian Osman3c358422020-03-23 10:44:12 -0400384 half4 col0 = sample(c0, p);
385 half4 col1 = sample(c1, p);
Brian Osman767f4442020-08-13 16:59:48 -0400386 return mix(col0, col1, half(vtx_lerp));
Brian Osman3c358422020-03-23 10:44:12 -0400387 }
388 )";
389 auto [effect, errorText] = SkRuntimeEffect::Make(SkString(gProg));
Mike Reed1f607332020-05-21 12:11:27 -0400390 SkMatrix scale = SkMatrix::Scale(2, 2);
Brian Osman3c358422020-03-23 10:44:12 -0400391 sk_sp<SkShader> children[] = {
392 GetResourceAsImage("images/mandrill_256.png")->makeShader(),
393 GetResourceAsImage("images/color_wheel.png")->makeShader(scale),
394 };
395 paint.setShader(effect->makeShader(nullptr, children, 2, nullptr, false));
396
397 canvas->drawVertices(verts, paint);
398}
Brian Osman68219bf2020-04-07 16:04:24 -0400399
400static constexpr SkScalar kSin60 = 0.8660254f; // sqrt(3) / 2
401static constexpr SkPoint kHexVerts[] = {
402 { 0, 0 },
403 { 0, -1 },
404 { kSin60, -0.5f },
405 { kSin60, 0.5f },
406 { 0, 1 },
407 { -kSin60, 0.5f },
408 { -kSin60, -0.5f },
409 { 0, -1 },
410};
411
412static constexpr SkColor4f kColors[] = {
413 SkColors::kWhite,
414 SkColors::kRed,
415 SkColors::kYellow,
416 SkColors::kGreen,
417 SkColors::kCyan,
418 SkColors::kBlue,
419 SkColors::kMagenta,
420 SkColors::kRed,
421};
422
Brian Osmand1afef62020-04-09 16:24:23 -0400423using Attr = SkVertices::Attribute;
424
425DEF_SIMPLE_GM(vertices_custom_colors, canvas, 400, 200) {
Brian Osman68219bf2020-04-07 16:04:24 -0400426 ToolUtils::draw_checkerboard(canvas);
427
428 auto draw = [=](SkScalar cx, SkScalar cy, SkVertices::Builder& builder, const SkPaint& paint) {
429 memcpy(builder.positions(), kHexVerts, sizeof(kHexVerts));
430
431 canvas->save();
432 canvas->translate(cx, cy);
433 canvas->scale(45, 45);
434 canvas->drawVertices(builder.detach(), paint);
435 canvas->restore();
436 };
437
438 auto transColor = [](int i) {
439 return SkColor4f { kColors[i].fR, kColors[i].fG, kColors[i].fB, i % 2 ? 0.5f : 1.0f };
440 };
441
442 // Fixed function SkVertices, opaque
443 {
444 SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, 8, 0,
445 SkVertices::kHasColors_BuilderFlag);
446 for (int i = 0; i < 8; ++i) {
447 builder.colors()[i] = kColors[i].toSkColor();
448 }
449 draw(50, 50, builder, SkPaint());
450 }
451
452 // Fixed function SkVertices, w/transparency
453 {
454 SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, 8, 0,
455 SkVertices::kHasColors_BuilderFlag);
456 for (int i = 0; i < 8; ++i) {
457 builder.colors()[i] = transColor(i).toSkColor();
458 }
459 draw(50, 150, builder, SkPaint());
460 }
461
462 const char* gProg = R"(
463 varying half4 vtx_color;
Brian Osman767f4442020-08-13 16:59:48 -0400464 half4 main(float2 p) {
465 return vtx_color;
Brian Osman68219bf2020-04-07 16:04:24 -0400466 }
467 )";
468 SkPaint skslPaint;
469 auto [effect, errorText] = SkRuntimeEffect::Make(SkString(gProg));
470 skslPaint.setShader(effect->makeShader(nullptr, nullptr, 0, nullptr, false));
471
Brian Osman68219bf2020-04-07 16:04:24 -0400472 Attr byteColorAttr(Attr::Type::kByte4_unorm, Attr::Usage::kColor);
473 Attr float4ColorAttr(Attr::Type::kFloat4, Attr::Usage::kColor);
474 Attr float3ColorAttr(Attr::Type::kFloat3, Attr::Usage::kColor);
475
476 // Custom vertices, byte colors, opaque
477 {
478 SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, 8, 0, &byteColorAttr, 1);
479 for (int i = 0; i < 8; ++i) {
480 ((uint32_t*)builder.customData())[i] = kColors[i].toBytes_RGBA();
481 }
482 draw(150, 50, builder, skslPaint);
483 }
484
485 // Custom vertices, byte colors, w/transparency
486 {
487 SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, 8, 0, &byteColorAttr, 1);
488 for (int i = 0; i < 8; ++i) {
489 ((uint32_t*)builder.customData())[i] = transColor(i).toBytes_RGBA();
490 }
491 draw(150, 150, builder, skslPaint);
492 }
493
494 // Custom vertices, float4 colors, opaque
495 {
496 SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, 8, 0, &float4ColorAttr, 1);
497 for (int i = 0; i < 8; ++i) {
498 ((SkColor4f*)builder.customData())[i] = kColors[i];
499 }
500 draw(250, 50, builder, skslPaint);
501 }
502
503 // Custom vertices, float4 colors, w/transparency
504 {
505 SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, 8, 0, &float4ColorAttr, 1);
506 SkColor4f* clr = (SkColor4f*)builder.customData();
507 for (int i = 0; i < 8; ++i) {
508 clr[i] = transColor(i);
509 }
510 draw(250, 150, builder, skslPaint);
511 }
512
513 // Custom vertices, float3 colors, opaque
514 {
515 SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, 8, 0, &float3ColorAttr, 1);
516 for (int i = 0; i < 8; ++i) {
517 ((SkV3*)builder.customData())[i] = { kColors[i].fR, kColors[i].fG, kColors[i].fB };
518 }
519 draw(350, 50, builder, skslPaint);
520 }
Brian Osmand1afef62020-04-09 16:24:23 -0400521}
Brian Osman68219bf2020-04-07 16:04:24 -0400522
Brian Osman548de742020-04-24 12:02:25 -0400523static sk_sp<SkVertices> make_cone(Attr::Usage u, const char* markerName) {
524 Attr attr(Attr::Type::kFloat3, u, markerName);
Brian Osmand1afef62020-04-09 16:24:23 -0400525
526 constexpr int kPerimeterVerts = 64;
527 // +1 for the center, +1 to repeat the first perimeter point (so we draw a complete circle)
528 constexpr int kNumVerts = kPerimeterVerts + 2;
529
John Stiles8cc118d2020-07-30 11:22:35 -0400530 SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, kNumVerts, /*indexCount=*/0,
531 &attr, /*attrCount=*/1);
Brian Osmand1afef62020-04-09 16:24:23 -0400532
533 SkPoint* pos = builder.positions();
534 SkPoint3* vec = static_cast<SkPoint3*>(builder.customData());
535
536 pos[0] = { 0, 0 };
537 vec[0] = { 0, 0, 1 };
538
539 for (int i = 0; i < kPerimeterVerts + 1; ++i) {
540 SkScalar t = (i / SkIntToScalar(kPerimeterVerts)) * 2 * SK_ScalarPI;
541 SkScalar s = SkScalarSin(t),
542 c = SkScalarCos(t);
543 pos[i + 1] = { c, s };
544 vec[i + 1] = { c, s, 0 };
545 }
546
547 return builder.detach();
548}
549
Brian Osman2ad3dfb2020-05-15 15:47:25 -0400550DEF_SIMPLE_GM(vertices_custom_matrices, canvas, 400, 400) {
Brian Osmand1afef62020-04-09 16:24:23 -0400551 ToolUtils::draw_checkerboard(canvas);
552
Brian Osman548de742020-04-24 12:02:25 -0400553 const char* kViewSpace = "local_to_view";
554 const char* kWorldSpace = "local_to_world";
555 const char* kLocalSpace = "local_to_local";
Brian Osmand1afef62020-04-09 16:24:23 -0400556
557 auto draw = [=](SkScalar cx, SkScalar cy, sk_sp<SkVertices> vertices, const char* prog,
558 SkScalar squish = 1.0f) {
559 SkPaint paint;
560 auto [effect, errorText] = SkRuntimeEffect::Make(SkString(prog));
561 paint.setShader(effect->makeShader(nullptr, nullptr, 0, nullptr, false));
562
563 canvas->save();
564
565 // Device space: mesh is upright, translated to its "cell"
566 canvas->translate(cx, cy);
567
568 // View (camera) space: Mesh is upright, centered on origin, device scale
569 canvas->markCTM(kViewSpace);
570 canvas->rotate(90);
571
572 // World space: Mesh is sideways, centered on origin, device scale (possibly squished)
573 canvas->markCTM(kWorldSpace);
574 canvas->rotate(-90);
575 canvas->scale(45, 45 * squish);
576
577 // Local space: Mesh is upright, centered on origin, unit scale
578 canvas->markCTM(kLocalSpace);
579 canvas->drawVertices(vertices, paint);
580
581 canvas->restore();
582 };
583
584 const char* vectorProg = R"(
585 varying float3 vtx_vec;
Brian Osman767f4442020-08-13 16:59:48 -0400586 half4 main(float2 p) {
587 return (half3(vtx_vec) * 0.5 + 0.5).rgb1;
Brian Osmand1afef62020-04-09 16:24:23 -0400588 })";
589
590 // raw, local vectors, normals, and positions should all look the same (no real transform)
Brian Osman548de742020-04-24 12:02:25 -0400591 draw(50, 50, make_cone(Attr::Usage::kRaw, nullptr), vectorProg);
Brian Osmand1afef62020-04-09 16:24:23 -0400592 draw(150, 50, make_cone(Attr::Usage::kVector, kLocalSpace), vectorProg);
593 draw(250, 50, make_cone(Attr::Usage::kNormalVector, kLocalSpace), vectorProg);
594 draw(350, 50, make_cone(Attr::Usage::kPosition, kLocalSpace), vectorProg);
595
596 // world-space vectors and normals are rotated 90 degrees, positions are centered but scaled up
597 draw(150, 150, make_cone(Attr::Usage::kVector, kWorldSpace), vectorProg);
598 draw(250, 150, make_cone(Attr::Usage::kNormalVector, kWorldSpace), vectorProg);
599 draw(350, 150, make_cone(Attr::Usage::kPosition, kWorldSpace), vectorProg);
600
601 // Squished vectors are "wrong", but normals are correct (because we use the inverse transpose)
602 // Positions remain scaled up (saturated), but otherwise correct
603 draw(150, 250, make_cone(Attr::Usage::kVector, kWorldSpace), vectorProg, 0.5f);
604 draw(250, 250, make_cone(Attr::Usage::kNormalVector, kWorldSpace), vectorProg, 0.5f);
605 draw(350, 250, make_cone(Attr::Usage::kPosition, kWorldSpace), vectorProg, 0.5f);
Brian Osman2ad3dfb2020-05-15 15:47:25 -0400606
607 draw( 50, 350, make_cone(Attr::Usage::kVector, nullptr), vectorProg, 0.5f);
608 draw(150, 350, make_cone(Attr::Usage::kNormalVector, nullptr), vectorProg, 0.5f);
609
610 // For canvas-space positions, color them according to their position relative to the center.
611 // We do this test twice, with and without saveLayer. That ensures that we get the canvas CTM,
612 // not just a local-to-device matrix, which exposes effect authors to an implementation detail.
613
614 const char* ctmPositionProg250 = R"(
615 varying float3 vtx_pos;
Brian Osman767f4442020-08-13 16:59:48 -0400616 half4 main(float2 p) {
617 return ((half3(vtx_pos) - half3(250, 350, 0)) / 50 + 0.5).rgb1;
Brian Osman2ad3dfb2020-05-15 15:47:25 -0400618 }
619 )";
620 draw(250, 350, make_cone(Attr::Usage::kPosition, nullptr), ctmPositionProg250, 0.5f);
621
622 const char* ctmPositionProg350 = R"(
623 varying float3 vtx_pos;
Brian Osman767f4442020-08-13 16:59:48 -0400624 half4 main(float2 p) {
625 return ((half3(vtx_pos) - half3(350, 350, 0)) / 50 + 0.5).rgb1;
Brian Osman2ad3dfb2020-05-15 15:47:25 -0400626 }
627 )";
628 canvas->saveLayer({ 300, 300, 400, 400 }, nullptr);
629 draw(350, 350, make_cone(Attr::Usage::kPosition, nullptr), ctmPositionProg350, 0.5f);
630 canvas->restore();
Brian Osman68219bf2020-04-07 16:04:24 -0400631}