Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(153)

Unified Diff: gm/shadowmaps.cpp

Issue 2118553002: adding new GM to demostrate new shadows (Closed) Base URL: https://skia.googlesource.com/skia@master
Patch Set: Made requested changes Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | gyp/common_variables.gypi » ('j') | src/core/SkShadowShader.cpp » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: gm/shadowmaps.cpp
diff --git a/gm/shadowmaps.cpp b/gm/shadowmaps.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..b9a6d90e59db5ce87e48f100ec36669754a914c8
--- /dev/null
+++ b/gm/shadowmaps.cpp
@@ -0,0 +1,390 @@
+/*
+ * Copyright 2016 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "gm.h"
robertphillips 2016/07/27 18:52:58 Alphabetize the Sk* headers
vjiaoblack 2016/07/28 17:17:50 Done.
+#include "SkLights.h"
+#include "SkPictureRecorder.h"
+#include "SkSurface.h"
+#include "SkPaintFilterCanvas.h"
+#include "SkMatrix.h"
+#include "SkShadowShader.h"
robertphillips 2016/07/27 18:52:58 Do we need SkBitmapProcShader.h ?
vjiaoblack 2016/07/28 17:17:51 Done.
+#include "SkBitmapProcShader.h"
+#include "SkNormalSource.h"
+
+static sk_sp<SkShader> make_shadow_shader(sk_sp<SkImage> povDepth,
+ sk_sp<SkImage> diffuse,
+ sk_sp<SkLights> lights) {
+
+ sk_sp<SkShader> povDepthShader = povDepth->makeShader(SkShader::kClamp_TileMode,
+ SkShader::kClamp_TileMode);
+
+ sk_sp<SkShader> diffuseShader = diffuse->makeShader(SkShader::kClamp_TileMode,
+ SkShader::kClamp_TileMode);
+
+ return SkShadowShader::Make(std::move(povDepthShader),
+ std::move(diffuseShader),
+ std::move(lights));
+}
+
+static sk_sp<SkPicture> make_test_picture(int width, int height) {
+ SkPictureRecorder recorder;
+
+ // LONG RANGE TODO: eventually add SkBBHFactory (bounding box factory)
+ SkCanvas* canvas = recorder.beginRecording(SkRect::MakeIWH(width, height));
+
+ SkASSERT(canvas->getTotalMatrix().isIdentity());
+ SkPaint paint;
+ paint.setColor(SK_ColorGRAY);
+
+ // LONG RANGE TODO: tag occluders
+ // LONG RANGE TODO: track number of IDs we need (hopefully less than 256)
+ // and determinate the mapping from z to id
+
+ // universal receiver, "ground"
+ canvas->drawRect(SkRect::MakeIWH(width, height), paint);
+
+ // TODO: Maybe add the ID here along with the depth
+
+ paint.setColor(0xFFEE8888);
+
+ canvas->translateZ(80);
+ canvas->drawRect(SkRect::MakeLTRB(200,150,350,300), paint);
+
+ paint.setColor(0xFF88EE88);
+
+ canvas->translateZ(80);
+ canvas->drawRect(SkRect::MakeLTRB(150,200,300,350), paint);
+
+ paint.setColor(0xFF8888EE);
+
+ canvas->translateZ(80);
+ canvas->drawRect(SkRect::MakeLTRB(100,100,250,250), paint);
+ // TODO: Add an assert that Z order matches painter's order
+ // TODO: think about if the Z-order always matching painting order is too strict
+
+ return recorder.finishRecordingAsPicture();
+}
+
+namespace skiagm {
+
+/* We override the onFilter method to draw depths into the canvas
+ * depending on the current draw depth of the canvas, throwing out
+ * the actual draw color.
+ */
+class SkShadowPaintFilterCanvas : public SkPaintFilterCanvas {
+public:
+
robertphillips 2016/07/27 18:52:58 space before the ':' use INHERITED rather than SkP
vjiaoblack 2016/07/28 17:17:50 Done.
+ SkShadowPaintFilterCanvas(SkCanvas* canvas): SkPaintFilterCanvas(canvas) { };
+
+ bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type type) const override {
+ if (*paint) {
+ uint32_t z = this->getZ();
+ SkColor color = 0xFF000000; // init color to opaque black
robertphillips 2016/07/27 18:52:58 assert that z will fit in 8 bits ?
vjiaoblack 2016/07/28 17:17:50 Done.
+ color |= z; // Put the index into the blue component
+ paint->writable()->setColor(color);
+ }
+
+ return true;
+ }
+
robertphillips 2016/07/27 18:52:58 Aren't all these onDraw* methods supposed to have
vjiaoblack 2016/07/28 17:17:50 Done.
+ void onDrawPicture(const SkPicture* picture, const SkMatrix* matrix, const SkPaint* paint) {
robertphillips 2016/07/27 18:52:58 It seems like SkPaintFilterCanvas::onDrawPicture d
vjiaoblack 2016/07/28 17:17:51 It doesn't work. I think because SkPaintFilterCanv
robertphillips 2016/07/28 19:47:25 That's odd, it should ultimately wind up calling S
+ SkTCopyOnFirstWrite<SkPaint> filteredPaint(paint);
robertphillips 2016/07/27 18:52:58 this->onFilter
vjiaoblack 2016/07/28 17:17:51 Done.
+ if (onFilter(&filteredPaint, kPicture_Type))
robertphillips 2016/07/27 18:52:58 Need enclosing brackets here I think you still nee
vjiaoblack 2016/07/28 17:17:50 Done.
+ SkCanvas::onDrawPicture(picture, matrix, filteredPaint);
+ }
+
+ void updateMatrix() {
+ this->save();
+
+ // When we use the SkShadowPaintFilterCanvas, we can only render
+ // one depth map at a time. Thus, we leave it up to the user to
+ // set SkLights to only contain (or contain at the first position)
+ // the light they intend to use for the current depth rendering.
+ SkVector3 lightDir = this->fLights->light(0).dir();
+ SkScalar x = lightDir.fX * this->getZ();
+ SkScalar y = lightDir.fY * this->getZ();
+
+ this->translate(x, y);
+ }
+
+ void onDrawPaint(const SkPaint& paint) {
+ this->updateMatrix();
+ this->INHERITED::onDrawPaint(paint);
+ this->restore();
+ }
+
+ void onDrawPoints(PointMode mode, size_t count, const SkPoint pts[], const SkPaint& paint) {
+ this->updateMatrix();
+ this->INHERITED::onDrawPoints(mode, count, pts, paint);
+ this->restore();
+ }
+
+ void onDrawRect(const SkRect& rect, const SkPaint& paint) {
+ this->updateMatrix();
+ this->INHERITED::onDrawRect(rect, paint);
+ this->restore();
+ }
+
+ void onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
+ this->updateMatrix();
+ this->INHERITED::onDrawRRect(rrect, paint);
+ this->restore();
+ }
+
+ void onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
+ this->updateMatrix();
+ this->INHERITED::onDrawDRRect(outer, inner, paint);
+ this->restore();
+ }
+
+ void onDrawOval(const SkRect& rect, const SkPaint& paint) {
+ this->updateMatrix();
+ this->INHERITED::onDrawOval(rect, paint);
+ this->restore();
+ }
+
+ void onDrawPath(const SkPath& path, const SkPaint& paint) {
+ this->updateMatrix();
+ this->INHERITED::onDrawPath(path, paint);
+ this->restore();
+ }
+
+ void onDrawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top, const SkPaint* paint) {
+ this->updateMatrix();
+ this->INHERITED::onDrawBitmap(bm, left, top, paint);
+ this->restore();
+ }
+
+ void onDrawBitmapRect(const SkBitmap& bm, const SkRect* src, const SkRect& dst,
+ const SkPaint* paint, SrcRectConstraint constraint) {
+ this->updateMatrix();
+ this->INHERITED::onDrawBitmapRect(bm, src, dst, paint, constraint);
+ this->restore();
+ }
+
+ void onDrawBitmapNine(const SkBitmap& bm, const SkIRect& center,
+ const SkRect& dst, const SkPaint* paint) {
+ this->updateMatrix();
+ this->INHERITED::onDrawBitmapNine(bm, center, dst, paint);
+ this->restore();
+ }
+
+ void onDrawImage(const SkImage* image, SkScalar left, SkScalar top, const SkPaint* paint) {
+ this->updateMatrix();
+ this->INHERITED::onDrawImage(image, left, top, paint);
+ this->restore();
+ }
+
+ void onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
+ const SkPaint* paint, SrcRectConstraint constraint) {
+ this->updateMatrix();
+ this->INHERITED::onDrawImageRect(image, src, dst, paint, constraint);
+ this->restore();
+ }
+
+ void onDrawImageNine(const SkImage* image, const SkIRect& center,
+ const SkRect& dst, const SkPaint* paint) {
+ this->updateMatrix();
+ this->INHERITED::onDrawImageNine(image, center, dst, paint);
+ this->restore();
+ }
+
+ void onDrawVertices(VertexMode vmode, int vertexCount, const SkPoint vertices[],
+ const SkPoint texs[], const SkColor colors[], SkXfermode* xmode,
+ const uint16_t indices[], int indexCount, const SkPaint& paint) {
+ this->updateMatrix();
+ this->INHERITED::onDrawVertices(vmode, vertexCount, vertices, texs, colors,
+ xmode, indices, indexCount, paint);
+ this->restore();
+ }
+
+ void onDrawPatch(const SkPoint cubics[], const SkColor colors[], const SkPoint texCoords[],
+ SkXfermode* xmode, const SkPaint& paint) {
+ this->updateMatrix();
+ this->INHERITED::onDrawPatch(cubics, colors, texCoords, xmode, paint);
+ this->restore();
+ }
+
+ void onDrawText(const void* text, size_t byteLength,
+ SkScalar x, SkScalar y, const SkPaint& paint) {
+ this->updateMatrix();
+ this->INHERITED::onDrawText(text, byteLength, x, y, paint);
+ this->restore();
+ }
+
+ void onDrawPosText(const void* text, size_t byteLength,
+ const SkPoint pos[], const SkPaint& paint) {
+ this->updateMatrix();
+ this->INHERITED::onDrawPosText(text, byteLength, pos, paint);
+ this->restore();
+ }
+
+ void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
+ SkScalar constY, const SkPaint& paint) {
+ this->updateMatrix();
+ this->INHERITED::onDrawPosTextH(text, byteLength, xpos, constY, paint);
+ this->restore();
+ }
+
+ void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
+ const SkMatrix* matrix, const SkPaint& paint) {
+ this->updateMatrix();
+ this->INHERITED::onDrawTextOnPath(text, byteLength, path, matrix, paint);
+ this->restore();
+ }
+
+ void onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[],
+ const SkRect* cull, const SkPaint& paint) {
+ this->updateMatrix();
+ this->INHERITED::onDrawTextRSXform(text, byteLength, xform, cull, paint);
+ this->restore();
+ }
+
+ void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint) {
+ this->updateMatrix();
+ this->INHERITED::onDrawTextBlob(blob, x, y, paint);
+ this->restore();
+ }
+
+private:
+ typedef SkPaintFilterCanvas INHERITED;
robertphillips 2016/07/27 18:52:58 extra '\n' here
vjiaoblack 2016/07/28 17:17:50 Done.
+
+};
+
+class ShadowMapsGM : public GM {
+public:
+ ShadowMapsGM() {
+ this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
+ }
+
+ void onOnceBeforeDraw() override {
+ // Create a light set consisting of
+ // - bluish directional light pointing more right than down
+ // - reddish directional light pointing more down than right
+ // - soft white ambient light
+
+ SkLights::Builder builder;
+ builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.3f, 0.4f),
+ SkVector3::Make(0.2f, 0.1f, 1.0f)));
+ builder.add(SkLights::Light(SkColor3f::Make(0.4f, 0.3f, 0.2f),
+ SkVector3::Make(0.1f, 0.2f, 1.0f)));
+ builder.add(SkLights::Light(SkColor3f::Make(0.4f, 0.4f, 0.4f)));
+ fLights = builder.finish();
+ }
+
+protected:
+ static const int kWidth = 400;
+ static const int kHeight = 400;
+
+ SkString onShortName() override {
+ return SkString("shadowmaps");
+ }
+
+ SkISize onISize() override {
+ return SkISize::Make(kWidth, kHeight);
+ }
+
+ void onDraw(SkCanvas* canvas) override {
+ // This picture stores the picture of the scene.
+ // It's used to generate the depth maps.
+ sk_sp<SkPicture> pic(make_test_picture(kWidth, kHeight));
+
robertphillips 2016/07/27 18:52:58 Why do we wrap 'canvas' in an SPFC? AFAICT we neve
vjiaoblack 2016/07/28 17:17:50 Done. Seemed like SPFCanvas and Canvas had differ
+ // Wrapping input canvas in an ShadowPaintFilterCanvas
+ sk_sp<SkShadowPaintFilterCanvas> SPFCanvas = sk_make_sp<SkShadowPaintFilterCanvas>(canvas);
+
+ for (int i = 0; i < fLights->numLights(); ++i) {
+ // skip over ambient lights; they don't cast shadows
+ if (SkLights::Light::kAmbient_LightType == fLights->light(i).type()) {
+ continue;
+ }
+ // TODO: compute the correct size of the depth map from the light properties
+ // TODO: maybe add a kDepth_8_SkColorType
+ // TODO: change to kIndex_8_SkColorType
robertphillips 2016/07/27 18:52:58 rm this last TODO or change to A8
vjiaoblack 2016/07/28 17:17:50 Done.
+
robertphillips 2016/07/27 18:52:58 Maybe change kWidth & kHeight here to be kDefaultS
vjiaoblack 2016/07/28 17:17:50 What do you mean? The shadow map size should be de
robertphillips 2016/07/28 19:47:25 Right, since they aren't necessarily the same you
+ SkImageInfo info = SkImageInfo::Make(kWidth, kHeight,
+ kBGRA_8888_SkColorType,
+ kOpaque_SkAlphaType);
+
robertphillips 2016/07/27 18:52:58 // Create a new surface (that matches the backend
vjiaoblack 2016/07/28 17:17:50 Done.
+ // Get the surface accessor to the SPFCanvas
+ sk_sp<SkSurface> surf(SPFCanvas->makeSurface(info));
+
+ // Wrap another SPFCanvas around the surface
+ sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas =
+ sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas());
+
+ // set the depth map canvas to have the light we're drawing.
+ SkLights::Builder builder;
+ builder.add(fLights->light(i));
+ sk_sp<SkLights> curLight = builder.finish();
+
+ depthMapCanvas->setLights(curLight);
+ depthMapCanvas->drawPicture(pic);
+
+ fLights->light(i).setShadowMap(surf->makeImageSnapshot());
+ }
+
+ sk_sp<SkImage> povDepthMap;
+ sk_sp<SkImage> diffuseMap;
+
+ // TODO: pass the depth to the shader in vertices, or uniforms
+ // so we don't have to render depth and color separately
+
+ // povDepthMap
+ {
+ SkLights::Builder builder;
+ builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, 1.0f),
+ SkVector3::Make(0.0f, 0.0f, 1.0f)));
+ sk_sp<SkLights> povLight = builder.finish();
+
+ SkImageInfo info = SkImageInfo::Make(kWidth, kHeight,
+ kBGRA_8888_SkColorType,
+ kOpaque_SkAlphaType);
+
robertphillips 2016/07/27 18:52:58 // Create a new surface (that matches the backend
vjiaoblack 2016/07/28 17:17:51 Done.
+ // Get the surface accessor to the SPFCanvas
+ sk_sp<SkSurface> surf(SPFCanvas->makeSurface(info));
+
+ // Wrap another SPFCanvas around the surface
+ sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas =
+ sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas());
+
+ // set the depth map canvas to have the light as the user's POV
+ depthMapCanvas->setLights(povLight);
+
+ depthMapCanvas->drawPicture(pic);
+
+ povDepthMap = surf->makeImageSnapshot();
+ }
+
+ // diffuseMap
+ {
+ SkImageInfo info = SkImageInfo::Make(kWidth, kHeight,
+ kBGRA_8888_SkColorType,
+ kOpaque_SkAlphaType);
+
+ sk_sp<SkSurface> surf(SPFCanvas->makeSurface(info));
+ surf->getCanvas()->drawPicture(pic);
+
+ diffuseMap = surf->makeImageSnapshot();
+ }
+
robertphillips 2016/07/27 18:52:58 Make this 2 lines: SkPaint paint; paint.setShader
vjiaoblack 2016/07/28 17:17:50 Done.
+ sk_sp<SkShader> shadowShader = make_shadow_shader(povDepthMap, diffuseMap, fLights);
+ SkPaint paint;
+ paint.setShader(shadowShader);
+
+ canvas->drawRect(SkRect::MakeIWH(kWidth, kHeight), paint);
+ }
+
+private:
+ sk_sp<SkLights> fLights;
+
+ typedef GM INHERITED;
+};
+
+//////////////////////////////////////////////////////////////////////////////
+
+DEF_GM(return new ShadowMapsGM;)
+}
« no previous file with comments | « no previous file | gyp/common_variables.gypi » ('j') | src/core/SkShadowShader.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698