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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « no previous file | include/gpu/GrFragmentProcessor.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2016 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
8 #include "gm.h"
9
10 #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.
11 #include "SkBitmapProcShader.h"
12 #include "SkLightingShader.h"
13 #include "SkNormalSource.h"
14 #include "SkPoint3.h"
15 #include "SkShader.h"
16
17 // Create a truncated pyramid normal map
18 static SkBitmap make_frustum_normalmap(int texSize) {
19 SkBitmap frustum;
20 frustum.allocN32Pixels(texSize, texSize);
21
22 sk_tool_utils::create_frustum_normal_map(&frustum, SkIRect::MakeWH(texSize, texSize));
23 return frustum;
24 }
25
26 namespace skiagm {
27
28 // This GM exercises lighting shaders. Specifically, nullptr arguments, scaling when using
29 // normal maps, and paint transparency.
30 class LightingShader2GM : public GM {
31 public:
32 LightingShader2GM() {
33 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
34 }
35
36 protected:
37 SkString onShortName() override {
38 return SkString("lightingshader2");
39 }
40
41 SkISize onISize() override {
42 return SkISize::Make(kGMSize, kGMSize);
43 }
44
45 void onOnceBeforeDraw() override {
46 SkLights::Builder builder;
47 const SkVector3 kLightFromUpperRight = SkVector3::Make(0.788f, 0.394f, 0 .473f);
48
49 builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, 1.0f),
50 kLightFromUpperRight));
51 builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.2f, 0.2f)));
52 fLights = builder.finish();
53
54 fOpaqueDiffuse = sk_tool_utils::create_checkerboard_bitmap(
55 kTexSize, kTexSize,
56 sk_tool_utils::color_to_565(0x0),
57 sk_tool_utils::color_to_565(0xFF804020),
58 8);
59
60 fTranslucentDiffuse = sk_tool_utils::create_checkerboard_bitmap(
61 kTexSize, kTexSize,
62 SkColorSetARGB(0x55, 0x00, 0x00, 0x00),
63 SkColorSetARGB(0x55, 0x80, 0x40, 0x20),
64 8);
65
66 fNormalMap = make_frustum_normalmap(kTexSize);
67 }
68
69 // Scales shape around origin, rotates shape around origin, then translates shape to origin
70 void positionCTM(SkCanvas *canvas, SkScalar scaleX, SkScalar scaleY, SkScala r rotate) const {
71 canvas->translate(kTexSize/2.0f, kTexSize/2.0f);
72 canvas->scale(scaleX, scaleY);
73 canvas->rotate(rotate);
74 canvas->translate(-kTexSize/2.0f, -kTexSize/2.0f);
75 }
76
77 static constexpr const char* BOOL_PARAM_LABELS[] = {
78 "normalSource",
79 "diffuseShader",
80 "transparentPaint",
81 "translucentShader"};
82
83 static constexpr int NUM_BOOLEAN_PARAMS = sizeof(BOOL_PARAM_LABELS)/sizeof(c har*);
84
85 void drawRect(SkCanvas* canvas, const SkRect& r, SkScalar scaleX, SkScalar s caleY,
86 SkScalar rotate, bool params[]) {
87 bool useNormalSource = params[0];
88 bool useDiffuseShader = params[1];
89 bool useTransparentPaint = params[2];
90 bool useTranslucentShader = params[3];
91
92 canvas->save();
93
94 this->positionCTM(canvas, scaleX, scaleY, rotate);
95
96 SkRect bitmapBounds = SkRect::MakeIWH(fOpaqueDiffuse.width(), fOpaqueDif fuse.height());
97 SkMatrix matrix;
98 matrix.setRectToRect(bitmapBounds, r, SkMatrix::kFill_ScaleToFit);
99
100 const SkMatrix& ctm = canvas->getTotalMatrix();
101
102 SkPaint paint;
103 sk_sp<SkNormalSource> normalSource = nullptr;
104 sk_sp<SkShader> diffuseShader = nullptr;
105
106 if (useNormalSource) {
107 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.
108 SkShader::kClamp_Til eMode, &matrix,
109 nullptr);
110 normalSource = SkNormalSource::MakeFromNormalMap(
111 std::move(normalMap),
112 ctm);
113 }
114
115 if (useDiffuseShader && !useTranslucentShader) {
116 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.
117 SkShader::kClamp_TileMode, &matri x, nullptr);
118 } else if (useDiffuseShader && useTranslucentShader) {
119 diffuseShader = SkMakeBitmapShader(fTranslucentDiffuse, SkShader::kC lamp_TileMode,
egdaniel 2016/07/19 22:26:15 Again here.
dvonbeck 2016/07/20 15:21:25 Done.
120 SkShader::kClamp_TileMode, &matri x, nullptr);
121 } else {
122 paint.setColor(0xFF00FF00);
123 }
124
125 if (useTransparentPaint) {
126 paint.setAlpha(0x99);
127 }
128
129 paint.setShader(SkLightingShader::Make(std::move(diffuseShader), std::mo ve(normalSource),
130 fLights));
131 canvas->drawRect(r, paint);
132
133 canvas->restore();
134 }
135
136 void onDraw(SkCanvas* canvas) override {
137 SkRect r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSiz e));
138
139 constexpr int LABEL_LEN = 15;
140 constexpr SkScalar LABEL_SIZE = 10.0f;
141 SkPaint labelPaint;
142 labelPaint.setTypeface(sk_tool_utils::create_portable_typeface("sans-ser if",
143 SkFontSty le()));
144 labelPaint.setAntiAlias(true);
145 labelPaint.setTextSize(LABEL_SIZE);
146
147 constexpr int GRID_COLUMN_NUM = 4;
148 constexpr SkScalar GRID_CELL_WIDTH = kTexSize + 20.0f + NUM_BOOLEAN_PARA MS * LABEL_SIZE;
149
150 constexpr int NUM_PARAM_COMBINATIONS = std::pow(2,NUM_BOOLEAN_PARAMS);
151 int gridNum = 0;
152 bool params[NUM_BOOLEAN_PARAMS];
153 // Running through all possible bool parameter combinations
154 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
155 // Setting params array to the low-order bits of gridNum
156 for (int j = 0; j < NUM_BOOLEAN_PARAMS; j++) {
157 params[j] = (gridNum & (1 << j));
158 }
159
160 // Determining position
161 SkScalar xPos = (gridNum % GRID_COLUMN_NUM) * GRID_CELL_WIDTH;
162 SkScalar yPos = (gridNum / GRID_COLUMN_NUM) * GRID_CELL_WIDTH;
163
164 canvas->save();
165
166 canvas->translate(xPos, yPos);
167 this->drawRect(canvas, r, 1.0f, 1.0f, 0.f, params);
168 // Drawing label
169 canvas->translate(0.0f, kTexSize);
170 for (int j = 0; j < NUM_BOOLEAN_PARAMS; j++) {
171 char label[(LABEL_LEN + 1)];
172 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.
173
174 canvas->translate(0.0f, LABEL_SIZE);
175 canvas->drawText(label, strlen(label), 0.0f, 0.0f, labelPaint);
176 }
177
178 canvas->restore();
179 }
180
181 // Rotation/scale test
182 {
183 SkScalar xPos = (gridNum % GRID_COLUMN_NUM) * GRID_CELL_WIDTH;
184 SkScalar yPos = (gridNum / GRID_COLUMN_NUM) * GRID_CELL_WIDTH;
185
186 canvas->save();
187 canvas->translate(xPos, yPos);
188 this->drawRect(canvas, r, 0.6f, 0.6f, 45.0f, params);
189 canvas->restore();
190
191 gridNum++;
192 }
193
194 // Anisotropic scale test
195 {
196 SkScalar xPos = (gridNum % GRID_COLUMN_NUM) * GRID_CELL_WIDTH;
197 SkScalar yPos = (gridNum / GRID_COLUMN_NUM) * GRID_CELL_WIDTH;
198
199 canvas->save();
200 canvas->translate(xPos, yPos);
201 this->drawRect(canvas, r, 0.6f, 0.4f, 30.0f, params);
202 canvas->restore();
203
204 gridNum++;
205 }
206 }
207
208 private:
209 static const int kTexSize = 96;
210 static const int kGMSize = 512;
211
212 SkBitmap fOpaqueDiffuse;
213 SkBitmap fTranslucentDiffuse;
214 SkBitmap fNormalMap;
215
216 sk_sp<SkLights> fLights;
217
218 typedef GM INHERITED;
219 };
220
221 constexpr const char* skiagm::LightingShader2GM::BOOL_PARAM_LABELS[];
222
223 //////////////////////////////////////////////////////////////////////////////
224
225 DEF_GM(return new LightingShader2GM;)
226 }
OLDNEW
« 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