Chromium Code Reviews| Index: gm/victor-shadows.cpp |
| diff --git a/gm/victor-shadows.cpp b/gm/victor-shadows.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0fd53924fc2266d78d80efab5e485aad70ab2fac |
| --- /dev/null |
| +++ b/gm/victor-shadows.cpp |
| @@ -0,0 +1,388 @@ |
| +/* |
|
jvanverth1
2016/07/26 15:01:24
I'd rename this to something more generic, like sh
vjiaoblack
2016/07/27 15:26:26
Done.
|
| + * 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/26 16:15:04
Do you still need SkDrawFilter.h ?
vjiaoblack
2016/07/27 15:26:26
Done.
|
| +#include "SkDrawFilter.h" |
| +#include "SkLights.h" |
| +#include "SkPictureRecorder.h" |
| +#include "SkSurface.h" |
| +#include "SkPaintFilterCanvas.h" |
| +#include "SkMatrix.h" |
| +#include "SkShadowShader.h" |
| +#include "SkBitmapProcShader.h" |
| +#include "SkNormalSource.h" |
| + |
|
robertphillips
2016/07/26 16:15:04
overlength line
file static methods are make_shado
vjiaoblack
2016/07/27 15:26:26
Done.
|
| +static sk_sp<SkShader> makeShadowShader(sk_sp<SkImage> povDepth, sk_sp<SkImage> diffuse, sk_sp<SkLights> lights) { |
| + // TODO: Victor continues to figure this out |
| + SkMatrix matrix1 = SkMatrix::I(); |
| + SkMatrix matrix2 = SkMatrix::I(); |
| + |
| + // this is the 0, 0, 1 light |
|
jvanverth1
2016/07/26 15:01:24
It's unclear based on this comment what this means
vjiaoblack
2016/07/27 15:26:27
Is that necessarily better? Asking because IDK. Th
|
| + sk_sp<SkShader> povDepthShader = povDepth->makeShader( |
| + SkShader::kClamp_TileMode, |
| + SkShader::kClamp_TileMode, |
| + &matrix1); |
|
jvanverth1
2016/07/26 15:01:24
If the matrix is identity, you don't need to pass
vjiaoblack
2016/07/27 15:26:26
Done.
|
| + |
| + sk_sp<SkShader> diffuseShader = diffuse->makeShader( |
| + SkShader::kClamp_TileMode, |
| + SkShader::kClamp_TileMode, |
| + &matrix2); |
| + |
|
robertphillips
2016/07/26 16:15:05
std::move(lights) ?
vjiaoblack
2016/07/27 15:26:26
Done.
|
| + return SkShadowShader::Make(std::move(povDepthShader), std::move(diffuseShader), lights); |
| +} |
| + |
|
robertphillips
2016/07/26 16:15:05
If it is named this way it should be file static
/
vjiaoblack
2016/07/27 15:26:26
Done.
|
| +sk_sp<SkPicture> make_picture(int width, int height, sk_sp<SkLights> lights) { |
|
jvanverth1
2016/07/26 15:01:24
This name seems a little generic. Maybe make_test_
vjiaoblack
2016/07/27 15:26:26
Done.
|
| + 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 { |
| + |
|
robertphillips
2016/07/26 16:15:04
// Why does this class exist
vjiaoblack
2016/07/27 15:26:27
Done.
|
| +class SkShadowPaintFilterCanvas : public SkPaintFilterCanvas { |
| +public: |
| + |
|
robertphillips
2016/07/26 16:15:04
Do you need to this entry point ?
It isn't virtual
vjiaoblack
2016/07/27 15:26:27
Done.
|
| + sk_sp<SkLights> getLights() { |
| + return INHERITED::getLights(); |
| + } |
| + |
|
robertphillips
2016/07/26 16:15:05
Can you fit this all on one line. Also no spaces i
vjiaoblack
2016/07/27 15:26:26
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 white |
| + color |= z; // Put the index into the blue component |
| + paint->writable()->setColor(color); |
| + } |
| + |
| + return true; |
| + } |
| + |
| + void onDrawPicture(const SkPicture* picture, const SkMatrix* matrix, const SkPaint* paint) { |
|
robertphillips
2016/07/26 16:15:04
Maybe some words here about how the updating of th
vjiaoblack
2016/07/27 15:26:27
Done.
|
| + SkTCopyOnFirstWrite<SkPaint> filteredPaint(paint); |
|
robertphillips
2016/07/26 16:15:05
Calls to member functions are preceded with "this-
vjiaoblack
2016/07/27 15:26:25
Filtering is necessary for calling onFilter, which
|
| + if (onFilter(&filteredPaint, kPicture_Type)) |
|
robertphillips
2016/07/26 16:15:04
Need {}s around this line
Not "this->INHERITED::on
vjiaoblack
2016/07/27 15:26:27
Doing that didn't work.
|
| + SkCanvas::onDrawPicture(picture, matrix, filteredPaint); |
| + } |
| + |
| + void updateMatrix() { |
|
robertphillips
2016/07/26 16:15:04
this->
vjiaoblack
2016/07/27 15:26:27
Done.
|
| + save(); |
| + |
|
robertphillips
2016/07/26 16:15:04
Shouldn't we only ever have one light when we get
vjiaoblack
2016/07/27 15:26:27
Done.
|
| + // TODO which light do we set? |
| + SkVector3 lightDir = this->fLights->light(0).dir(); |
|
jvanverth1
2016/07/26 15:01:24
Some sort of comment here that describes what you'
vjiaoblack
2016/07/27 15:26:25
I did!
I can't really pass into these override-ed
|
| + SkScalar x = lightDir.fX * this->getZ(); |
| + SkScalar y = lightDir.fY * this->getZ(); |
| + |
| + this->translate(x, y); |
| + } |
| + |
| + void onDrawPaint(const SkPaint& paint) { |
|
robertphillips
2016/07/26 16:15:05
this-> & all following instances too
vjiaoblack
2016/07/27 15:26:26
Done.
|
| + updateMatrix(); |
| + this->INHERITED::onDrawPaint(paint); |
|
robertphillips
2016/07/26 16:15:04
this-> & all following instances too
vjiaoblack
2016/07/27 15:26:26
Done.
|
| + restore(); |
| + } |
| + |
| + void onDrawPoints(PointMode mode, size_t count, const SkPoint pts[], |
|
robertphillips
2016/07/26 16:15:05
align the "const SkPint& paint" somehow
vjiaoblack
2016/07/27 15:26:27
Done.
|
| + const SkPaint& paint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawPoints(mode, count, pts, paint); |
| + restore(); |
| + } |
| + |
| + void onDrawRect(const SkRect& rect, const SkPaint& paint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawRect(rect, paint); |
| + restore(); |
| + } |
| + |
| + void onDrawRRect(const SkRRect& rrect, const SkPaint& paint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawRRect(rrect, paint); |
| + restore(); |
| + } |
| + |
| + void onDrawDRRect(const SkRRect& outer, const SkRRect& inner, |
| + const SkPaint& paint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawDRRect(outer, inner, paint); |
| + restore(); |
| + } |
| + |
| + void onDrawOval(const SkRect& rect, const SkPaint& paint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawOval(rect, paint); |
| + restore(); |
| + } |
| + |
| + void onDrawPath(const SkPath& path, const SkPaint& paint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawPath(path, paint); |
| + restore(); |
| + } |
| + |
| + void onDrawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top, |
| + const SkPaint* paint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawBitmap(bm, left, top, paint); |
| + restore(); |
| + } |
| + |
| + void onDrawBitmapRect(const SkBitmap& bm, const SkRect* src, const SkRect& dst, |
| + const SkPaint* paint, SrcRectConstraint constraint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawBitmapRect(bm, src, dst, paint, constraint); |
| + restore(); |
| + } |
| + |
| + void onDrawBitmapNine(const SkBitmap& bm, const SkIRect& center, |
| + const SkRect& dst, const SkPaint* paint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawBitmapNine(bm, center, dst, paint); |
| + restore(); |
| + } |
| + |
| + void onDrawImage(const SkImage* image, SkScalar left, SkScalar top, |
| + const SkPaint* paint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawImage(image, left, top, paint); |
| + restore(); |
| + } |
| + |
| + void onDrawImageRect(const SkImage* image, const SkRect* src, |
| + const SkRect& dst, const SkPaint* paint, |
| + SrcRectConstraint constraint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawImageRect(image, src, dst, paint, constraint); |
| + restore(); |
| + } |
| + |
| + void onDrawImageNine(const SkImage* image, const SkIRect& center, |
| + const SkRect& dst, const SkPaint* paint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawImageNine(image, center, dst, paint); |
| + 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) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawVertices(vmode, vertexCount, vertices, texs, colors, |
| + xmode, indices, indexCount, paint); |
| + restore(); |
| + } |
| + |
| + void onDrawPatch(const SkPoint cubics[], const SkColor colors[], |
| + const SkPoint texCoords[], SkXfermode* xmode, |
| + const SkPaint& paint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawPatch(cubics, colors, texCoords, xmode, paint); |
| + restore(); |
| + } |
| + |
| + void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y, |
| + const SkPaint& paint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawText(text, byteLength, x, y, paint); |
| + restore(); |
| + } |
| + |
| + void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[], |
| + const SkPaint& paint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawPosText(text, byteLength, pos, paint); |
| + restore(); |
| + } |
| + |
| + void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[], |
| + SkScalar constY, const SkPaint& paint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawPosTextH(text, byteLength, xpos, constY, paint); |
| + restore(); |
| + } |
| + |
| + void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path, |
| + const SkMatrix* matrix, const SkPaint& paint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawTextOnPath(text, byteLength, path, matrix, paint); |
| + restore(); |
| + } |
| + |
| + void onDrawTextRSXform(const void* text, size_t byteLength, |
| + const SkRSXform xform[], const SkRect* cull, |
| + const SkPaint& paint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawTextRSXform(text, byteLength, xform, cull, paint); |
| + restore(); |
| + } |
| + |
| + void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawTextBlob(blob, x, y, paint); |
| + restore(); |
| + } |
| + |
| +private: |
| + typedef SkPaintFilterCanvas INHERITED; |
| + |
| +}; |
| + |
| +class VictorShadowsGM : public GM { |
| +public: |
| + VictorShadowsGM() { |
| + |
| + this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); |
|
robertphillips
2016/07/26 16:15:04
move the construction of the lights into onOnceBef
vjiaoblack
2016/07/27 15:26:26
Done.
|
| + SkLights::Builder builder; |
| + builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.3f, 0.4f), |
| + SkVector3::Make(0.22f, 0.12f, 0.9747f))); |
| + builder.add(SkLights::Light(SkColor3f::Make(0.4f, 0.3f, 0.2f), |
| + SkVector3::Make(0.12f, 0.22f, 0.9747f))); |
| + 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 { |
|
robertphillips
2016/07/26 16:15:05
Probably rename to shadowmap-shadows or something
vjiaoblack
2016/07/27 15:26:26
Done.
|
| + return SkString("victor-shadows"); |
| + } |
| + |
| + SkISize onISize() override { |
| + return SkISize::Make(kWidth, kHeight); |
| + } |
| + |
| + void onDraw(SkCanvas* canvas) override { |
| + // This picture stores the picture of the scene. |
|
robertphillips
2016/07/26 16:15:04
depth maps or shadow maps ?
vjiaoblack
2016/07/27 15:26:26
Technically, shadow maps === depth maps, right?
(a
|
| + // It's used to generate the depth maps. |
| + sk_sp<SkPicture> pic(make_picture(kWidth, kHeight, fLights)); |
| + |
|
robertphillips
2016/07/26 16:15:04
Unless I'm missing something this isn't a cast. Do
vjiaoblack
2016/07/27 15:26:26
Done.
|
| + // The general ShadowPaintFilterCanvas cast of input canvas |
| + 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 shadow map from the light properties |
| + // TODO: maybe add a kDepth_8_SkColorType |
| + // TODO: change to kIndex_8_SkColorType |
|
jvanverth1
2016/07/26 15:01:24
Or use kAlpha_8_SkColorType?
robertphillips
2016/07/26 16:15:04
We should probably avoid trying to render to Index
vjiaoblack
2016/07/27 15:26:26
Is that possible through SkPaint?
vjiaoblack
2016/07/27 15:26:27
I don't think this is actually possible with the c
|
| + |
| + SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, |
| + kBGRA_8888_SkColorType, |
| + kOpaque_SkAlphaType); |
| + |
| + sk_sp<SkSurface> surf(SPFCanvas->makeSurface(info)); |
|
robertphillips
2016/07/26 16:15:04
// Wrap the new canvas in a ShadowPaintFilterCanva
vjiaoblack
2016/07/27 15:26:25
Done.
|
| + 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()); |
| + } |
| + |
|
robertphillips
2016/07/26 16:15:05
// TODO: plumb down each primitive's Z so we don't
vjiaoblack
2016/07/27 15:26:25
Done.
|
| + sk_sp<SkImage> povDepthMap; |
| + sk_sp<SkImage> diffuseMap; |
| + |
| + // 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); |
| + |
| + sk_sp<SkSurface> surf(SPFCanvas->makeSurface(info)); |
|
robertphillips
2016/07/26 16:15:04
// Wrap ...
vjiaoblack
2016/07/27 15:26:26
Done.
|
| + sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas = |
| + sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas()); |
| + |
| + // set the depth map canvas to have the light we're drawing. |
|
robertphillips
2016/07/26 16:15:05
How does curLight differ from povLight ?
vjiaoblack
2016/07/27 15:26:27
Done.
|
| + sk_sp<SkLights> curLight = builder.finish(); |
| + depthMapCanvas->setLights(curLight); |
| + |
| + 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(); |
| + } |
| + |
| + sk_sp<SkShader> shadowShader = makeShadowShader(povDepthMap, diffuseMap, fLights); |
| + SkPaint paint; |
| + paint.setShader(shadowShader); |
| + |
| + canvas->drawRect(SkRect::MakeIWH(400, 400), paint); |
|
jvanverth1
2016/07/26 15:01:24
kWidth, kHeight?
vjiaoblack
2016/07/27 15:26:26
Done.
|
| + } |
| + |
| +private: |
| + sk_sp<SkLights> fLights; |
| + |
| + typedef GM INHERITED; |
| +}; |
| + |
| +////////////////////////////////////////////////////////////////////////////// |
| + |
| +DEF_GM(return new VictorShadowsGM;) |
| +} |