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

Unified Diff: gm/lightingshader2.cpp

Issue 2132113002: SkLS accepts nullptr for pointer args, handles alpha accurately, has new GM (Closed) Base URL: https://skia.googlesource.com/skia@dvonbeck-diffuse-api-change
Patch Set: Changed position coordinate types 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 | include/gpu/GrFragmentProcessor.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: gm/lightingshader2.cpp
diff --git a/gm/lightingshader2.cpp b/gm/lightingshader2.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..27c7ff2cab7971ed40b09386af3a51ad7d51614e
--- /dev/null
+++ b/gm/lightingshader2.cpp
@@ -0,0 +1,226 @@
+/*
+ * 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"
+
+#include <cmath>
egdaniel 2016/07/19 22:26:15 if we use simpler nested for loops shouldn't need
dvonbeck 2016/07/20 15:21:25 Done.
+#include "SkBitmapProcShader.h"
+#include "SkLightingShader.h"
+#include "SkNormalSource.h"
+#include "SkPoint3.h"
+#include "SkShader.h"
+
+// Create a truncated pyramid normal map
+static SkBitmap make_frustum_normalmap(int texSize) {
+ SkBitmap frustum;
+ frustum.allocN32Pixels(texSize, texSize);
+
+ sk_tool_utils::create_frustum_normal_map(&frustum, SkIRect::MakeWH(texSize, texSize));
+ return frustum;
+}
+
+namespace skiagm {
+
+// This GM exercises lighting shaders. Specifically, nullptr arguments, scaling when using
+// normal maps, and paint transparency.
+class LightingShader2GM : public GM {
+public:
+ LightingShader2GM() {
+ this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
+ }
+
+protected:
+ SkString onShortName() override {
+ return SkString("lightingshader2");
+ }
+
+ SkISize onISize() override {
+ return SkISize::Make(kGMSize, kGMSize);
+ }
+
+ void onOnceBeforeDraw() override {
+ SkLights::Builder builder;
+ const SkVector3 kLightFromUpperRight = SkVector3::Make(0.788f, 0.394f, 0.473f);
+
+ builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, 1.0f),
+ kLightFromUpperRight));
+ builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.2f, 0.2f)));
+ fLights = builder.finish();
+
+ fOpaqueDiffuse = sk_tool_utils::create_checkerboard_bitmap(
+ kTexSize, kTexSize,
+ sk_tool_utils::color_to_565(0x0),
+ sk_tool_utils::color_to_565(0xFF804020),
+ 8);
+
+ fTranslucentDiffuse = sk_tool_utils::create_checkerboard_bitmap(
+ kTexSize, kTexSize,
+ SkColorSetARGB(0x55, 0x00, 0x00, 0x00),
+ SkColorSetARGB(0x55, 0x80, 0x40, 0x20),
+ 8);
+
+ fNormalMap = make_frustum_normalmap(kTexSize);
+ }
+
+ // Scales shape around origin, rotates shape around origin, then translates shape to origin
+ void positionCTM(SkCanvas *canvas, SkScalar scaleX, SkScalar scaleY, SkScalar rotate) const {
+ canvas->translate(kTexSize/2.0f, kTexSize/2.0f);
+ canvas->scale(scaleX, scaleY);
+ canvas->rotate(rotate);
+ canvas->translate(-kTexSize/2.0f, -kTexSize/2.0f);
+ }
+
+ static constexpr const char* BOOL_PARAM_LABELS[] = {
+ "normalSource",
+ "diffuseShader",
+ "transparentPaint",
+ "translucentShader"};
+
+ static constexpr int NUM_BOOLEAN_PARAMS = sizeof(BOOL_PARAM_LABELS)/sizeof(char*);
+
+ void drawRect(SkCanvas* canvas, const SkRect& r, SkScalar scaleX, SkScalar scaleY,
+ SkScalar rotate, bool params[]) {
+ bool useNormalSource = params[0];
+ bool useDiffuseShader = params[1];
+ bool useTransparentPaint = params[2];
+ bool useTranslucentShader = params[3];
+
+ canvas->save();
+
+ this->positionCTM(canvas, scaleX, scaleY, rotate);
+
+ SkRect bitmapBounds = SkRect::MakeIWH(fOpaqueDiffuse.width(), fOpaqueDiffuse.height());
+ SkMatrix matrix;
+ matrix.setRectToRect(bitmapBounds, r, SkMatrix::kFill_ScaleToFit);
+
+ const SkMatrix& ctm = canvas->getTotalMatrix();
+
+ SkPaint paint;
+ sk_sp<SkNormalSource> normalSource = nullptr;
+ sk_sp<SkShader> diffuseShader = nullptr;
+
+ if (useNormalSource) {
+ sk_sp <SkShader> normalMap = SkMakeBitmapShader(fNormalMap, SkShader::kClamp_TileMode,
egdaniel 2016/07/19 22:26:15 can we premake this bitmapshader in onceBeforeDraw
dvonbeck 2016/07/20 15:21:24 Done.
+ SkShader::kClamp_TileMode, &matrix,
+ nullptr);
+ normalSource = SkNormalSource::MakeFromNormalMap(
+ std::move(normalMap),
+ ctm);
+ }
+
+ if (useDiffuseShader && !useTranslucentShader) {
+ diffuseShader = SkMakeBitmapShader(fOpaqueDiffuse, SkShader::kClamp_TileMode,
egdaniel 2016/07/19 22:26:15 Here as well, premake?
dvonbeck 2016/07/20 15:21:25 Done.
+ SkShader::kClamp_TileMode, &matrix, nullptr);
+ } else if (useDiffuseShader && useTranslucentShader) {
+ diffuseShader = SkMakeBitmapShader(fTranslucentDiffuse, SkShader::kClamp_TileMode,
egdaniel 2016/07/19 22:26:15 Again here.
dvonbeck 2016/07/20 15:21:25 Done.
+ SkShader::kClamp_TileMode, &matrix, nullptr);
+ } else {
+ paint.setColor(0xFF00FF00);
+ }
+
+ if (useTransparentPaint) {
+ paint.setAlpha(0x99);
+ }
+
+ paint.setShader(SkLightingShader::Make(std::move(diffuseShader), std::move(normalSource),
+ fLights));
+ canvas->drawRect(r, paint);
+
+ canvas->restore();
+ }
+
+ void onDraw(SkCanvas* canvas) override {
+ SkRect r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSize));
+
+ constexpr int LABEL_LEN = 15;
+ constexpr SkScalar LABEL_SIZE = 10.0f;
+ SkPaint labelPaint;
+ labelPaint.setTypeface(sk_tool_utils::create_portable_typeface("sans-serif",
+ SkFontStyle()));
+ labelPaint.setAntiAlias(true);
+ labelPaint.setTextSize(LABEL_SIZE);
+
+ constexpr int GRID_COLUMN_NUM = 4;
+ constexpr SkScalar GRID_CELL_WIDTH = kTexSize + 20.0f + NUM_BOOLEAN_PARAMS * LABEL_SIZE;
+
+ constexpr int NUM_PARAM_COMBINATIONS = std::pow(2,NUM_BOOLEAN_PARAMS);
+ int gridNum = 0;
+ bool params[NUM_BOOLEAN_PARAMS];
+ // Running through all possible bool parameter combinations
+ for (; gridNum < NUM_PARAM_COMBINATIONS; gridNum++) {
egdaniel 2016/07/19 22:26:15 Why not just use 4 nested for loops here, seems li
dvonbeck 2016/07/20 15:21:24 Done. I left the x,y adjustment as is because I fe
+ // Setting params array to the low-order bits of gridNum
+ for (int j = 0; j < NUM_BOOLEAN_PARAMS; j++) {
+ params[j] = (gridNum & (1 << j));
+ }
+
+ // Determining position
+ SkScalar xPos = (gridNum % GRID_COLUMN_NUM) * GRID_CELL_WIDTH;
+ SkScalar yPos = (gridNum / GRID_COLUMN_NUM) * GRID_CELL_WIDTH;
+
+ canvas->save();
+
+ canvas->translate(xPos, yPos);
+ this->drawRect(canvas, r, 1.0f, 1.0f, 0.f, params);
+ // Drawing label
+ canvas->translate(0.0f, kTexSize);
+ for (int j = 0; j < NUM_BOOLEAN_PARAMS; j++) {
+ char label[(LABEL_LEN + 1)];
+ sprintf(label, "%s: %d", BOOL_PARAM_LABELS[j], params[j]);
egdaniel 2016/07/19 22:26:15 its not common to use sprintf in Skia. Lets just c
dvonbeck 2016/07/20 15:21:24 Done.
+
+ canvas->translate(0.0f, LABEL_SIZE);
+ canvas->drawText(label, strlen(label), 0.0f, 0.0f, labelPaint);
+ }
+
+ canvas->restore();
+ }
+
+ // Rotation/scale test
+ {
+ SkScalar xPos = (gridNum % GRID_COLUMN_NUM) * GRID_CELL_WIDTH;
+ SkScalar yPos = (gridNum / GRID_COLUMN_NUM) * GRID_CELL_WIDTH;
+
+ canvas->save();
+ canvas->translate(xPos, yPos);
+ this->drawRect(canvas, r, 0.6f, 0.6f, 45.0f, params);
+ canvas->restore();
+
+ gridNum++;
+ }
+
+ // Anisotropic scale test
+ {
+ SkScalar xPos = (gridNum % GRID_COLUMN_NUM) * GRID_CELL_WIDTH;
+ SkScalar yPos = (gridNum / GRID_COLUMN_NUM) * GRID_CELL_WIDTH;
+
+ canvas->save();
+ canvas->translate(xPos, yPos);
+ this->drawRect(canvas, r, 0.6f, 0.4f, 30.0f, params);
+ canvas->restore();
+
+ gridNum++;
+ }
+ }
+
+private:
+ static const int kTexSize = 96;
+ static const int kGMSize = 512;
+
+ SkBitmap fOpaqueDiffuse;
+ SkBitmap fTranslucentDiffuse;
+ SkBitmap fNormalMap;
+
+ sk_sp<SkLights> fLights;
+
+ typedef GM INHERITED;
+};
+
+constexpr const char* skiagm::LightingShader2GM::BOOL_PARAM_LABELS[];
+
+//////////////////////////////////////////////////////////////////////////////
+
+DEF_GM(return new LightingShader2GM;)
+}
« no previous file with comments | « no previous file | include/gpu/GrFragmentProcessor.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698