blob: 49938c6619df7207c3e8598ec41e7f1c841f33fe [file] [log] [blame]
Brian Salomonbc6b99d2017-01-11 10:32:34 -05001/*
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 "SkTypes.h"
9#include "Test.h"
10
11#if SK_SUPPORT_GPU
12#include "GrContext.h"
13#include "GrGpuResource.h"
Brian Salomon5d4cd9e2017-02-09 11:16:46 -050014#include "GrPipelineBuilder.h"
Brian Salomonbc6b99d2017-01-11 10:32:34 -050015#include "GrRenderTargetContext.h"
16#include "GrRenderTargetContextPriv.h"
17#include "GrResourceProvider.h"
18#include "glsl/GrGLSLFragmentProcessor.h"
19#include "glsl/GrGLSLFragmentShaderBuilder.h"
Brian Salomon5d4cd9e2017-02-09 11:16:46 -050020#include "ops/GrNonAAFillRectOp.h"
Brian Salomonbc6b99d2017-01-11 10:32:34 -050021#include "ops/GrTestMeshDrawOp.h"
22
23namespace {
24class TestOp : public GrTestMeshDrawOp {
25public:
26 DEFINE_OP_CLASS_ID
27 const char* name() const override { return "TestOp"; }
28
29 static std::unique_ptr<GrDrawOp> Make() { return std::unique_ptr<GrDrawOp>(new TestOp); }
30
31private:
32 TestOp() : INHERITED(ClassID(), SkRect::MakeWH(100, 100), 0xFFFFFFFF) {}
33
34 void onPrepareDraws(Target* target) const override { return; }
35
36 typedef GrTestMeshDrawOp INHERITED;
37};
38
39/**
40 * FP used to test ref/IO counts on owned GrGpuResources. Can also be a parent FP to test counts
41 * of resources owned by child FPs.
42 */
43class TestFP : public GrFragmentProcessor {
44public:
45 struct Image {
46 Image(sk_sp<GrTexture> texture, GrIOType ioType) : fTexture(texture), fIOType(ioType) {}
47 sk_sp<GrTexture> fTexture;
48 GrIOType fIOType;
49 };
50 static sk_sp<GrFragmentProcessor> Make(sk_sp<GrFragmentProcessor> child) {
51 return sk_sp<GrFragmentProcessor>(new TestFP(std::move(child)));
52 }
53 static sk_sp<GrFragmentProcessor> Make(const SkTArray<sk_sp<GrTexture>>& textures,
54 const SkTArray<sk_sp<GrBuffer>>& buffers,
55 const SkTArray<Image>& images) {
56 return sk_sp<GrFragmentProcessor>(new TestFP(textures, buffers, images));
57 }
58
59 const char* name() const override { return "test"; }
60
61 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
62 // We don't really care about reusing these.
63 static int32_t gKey = 0;
64 b->add32(sk_atomic_inc(&gKey));
65 }
66
67 void onComputeInvariantOutput(GrInvariantOutput* inout) const override {
68 // We don't care about optimizing these processors.
Brian Salomon5f13fba2017-01-23 14:35:25 -050069 inout->setToUnknown();
Brian Salomonbc6b99d2017-01-11 10:32:34 -050070 }
71
72private:
73 TestFP(const SkTArray<sk_sp<GrTexture>>& textures, const SkTArray<sk_sp<GrBuffer>>& buffers,
74 const SkTArray<Image>& images)
Brian Salomon587e08f2017-01-27 10:59:27 -050075 : INHERITED(kNone_OptimizationFlags), fSamplers(4), fBuffers(4), fImages(4) {
Brian Salomonbc6b99d2017-01-11 10:32:34 -050076 for (const auto& texture : textures) {
77 this->addTextureSampler(&fSamplers.emplace_back(texture.get()));
78 }
79 for (const auto& buffer : buffers) {
80 this->addBufferAccess(&fBuffers.emplace_back(kRGBA_8888_GrPixelConfig, buffer.get()));
81 }
82 for (const Image& image : images) {
83 this->addImageStorageAccess(&fImages.emplace_back(
84 image.fTexture, image.fIOType, GrSLMemoryModel::kNone, GrSLRestrict::kNo));
85 }
86 }
87
Brian Salomon587e08f2017-01-27 10:59:27 -050088 TestFP(sk_sp<GrFragmentProcessor> child)
89 : INHERITED(kNone_OptimizationFlags), fSamplers(4), fBuffers(4), fImages(4) {
Brian Salomonbc6b99d2017-01-11 10:32:34 -050090 this->registerChildProcessor(std::move(child));
91 }
92
93 virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
94 class TestGLSLFP : public GrGLSLFragmentProcessor {
95 public:
96 TestGLSLFP() {}
97 void emitCode(EmitArgs& args) override {
98 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
99 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, args.fInputColor);
100 }
101
102 private:
103 };
104 return new TestGLSLFP();
105 }
106
107 bool onIsEqual(const GrFragmentProcessor&) const override { return false; }
108
109 GrTAllocator<TextureSampler> fSamplers;
110 GrTAllocator<BufferAccess> fBuffers;
111 GrTAllocator<ImageStorageAccess> fImages;
Brian Salomon587e08f2017-01-27 10:59:27 -0500112 typedef GrFragmentProcessor INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500113};
114}
115
Brian Salomonf046e152017-01-11 15:24:47 -0500116template <typename T>
117inline void testingOnly_getIORefCnts(const T* resource, int* refCnt, int* readCnt, int* writeCnt) {
118 *refCnt = resource->fRefCnt;
119 *readCnt = resource->fPendingReads;
120 *writeCnt = resource->fPendingWrites;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500121}
122
123DEF_GPUTEST_FOR_ALL_CONTEXTS(ProcessorRefTest, reporter, ctxInfo) {
124 GrContext* context = ctxInfo.grContext();
125
126 GrTextureDesc desc;
127 desc.fConfig = kRGBA_8888_GrPixelConfig;
128 desc.fWidth = 10;
129 desc.fHeight = 10;
130
131 for (int parentCnt = 0; parentCnt < 2; parentCnt++) {
132 sk_sp<GrRenderTargetContext> renderTargetContext(context->makeRenderTargetContext(
133 SkBackingFit::kApprox, 1, 1, kRGBA_8888_GrPixelConfig, nullptr));
134 {
135 bool texelBufferSupport = context->caps()->shaderCaps()->texelBufferSupport();
136 bool imageLoadStoreSupport = context->caps()->shaderCaps()->imageLoadStoreSupport();
137 sk_sp<GrTexture> texture1(
138 context->resourceProvider()->createTexture(desc, SkBudgeted::kYes));
139 sk_sp<GrTexture> texture2(
140 context->resourceProvider()->createTexture(desc, SkBudgeted::kYes));
141 sk_sp<GrTexture> texture3(
142 context->resourceProvider()->createTexture(desc, SkBudgeted::kYes));
143 sk_sp<GrTexture> texture4(
144 context->resourceProvider()->createTexture(desc, SkBudgeted::kYes));
145 sk_sp<GrBuffer> buffer(texelBufferSupport
146 ? context->resourceProvider()->createBuffer(
147 1024, GrBufferType::kTexel_GrBufferType,
148 GrAccessPattern::kStatic_GrAccessPattern, 0)
149 : nullptr);
150 {
151 SkTArray<sk_sp<GrTexture>> textures;
152 SkTArray<sk_sp<GrBuffer>> buffers;
153 SkTArray<TestFP::Image> images;
154 textures.push_back(texture1);
155 if (texelBufferSupport) {
156 buffers.push_back(buffer);
157 }
158 if (imageLoadStoreSupport) {
159 images.emplace_back(texture2, GrIOType::kRead_GrIOType);
160 images.emplace_back(texture3, GrIOType::kWrite_GrIOType);
161 images.emplace_back(texture4, GrIOType::kRW_GrIOType);
162 }
163 std::unique_ptr<GrDrawOp> op(TestOp::Make());
164 GrPaint paint;
165 auto fp = TestFP::Make(std::move(textures), std::move(buffers), std::move(images));
166 for (int i = 0; i < parentCnt; ++i) {
167 fp = TestFP::Make(std::move(fp));
168 }
169 paint.addColorFragmentProcessor(std::move(fp));
Brian Salomon82f44312017-01-11 13:42:54 -0500170 renderTargetContext->priv().testingOnly_addDrawOp(std::move(paint), GrAAType::kNone,
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500171 std::move(op));
172 }
173 int refCnt, readCnt, writeCnt;
174
Brian Salomonf046e152017-01-11 15:24:47 -0500175 testingOnly_getIORefCnts(texture1.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500176 REPORTER_ASSERT(reporter, 1 == refCnt);
177 REPORTER_ASSERT(reporter, 1 == readCnt);
178 REPORTER_ASSERT(reporter, 0 == writeCnt);
179
180 if (texelBufferSupport) {
Brian Salomonf046e152017-01-11 15:24:47 -0500181 testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500182 REPORTER_ASSERT(reporter, 1 == refCnt);
183 REPORTER_ASSERT(reporter, 1 == readCnt);
184 REPORTER_ASSERT(reporter, 0 == writeCnt);
185 }
186
187 if (imageLoadStoreSupport) {
Brian Salomonf046e152017-01-11 15:24:47 -0500188 testingOnly_getIORefCnts(texture2.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500189 REPORTER_ASSERT(reporter, 1 == refCnt);
190 REPORTER_ASSERT(reporter, 1 == readCnt);
191 REPORTER_ASSERT(reporter, 0 == writeCnt);
192
Brian Salomonf046e152017-01-11 15:24:47 -0500193 testingOnly_getIORefCnts(texture3.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500194 REPORTER_ASSERT(reporter, 1 == refCnt);
195 REPORTER_ASSERT(reporter, 0 == readCnt);
196 REPORTER_ASSERT(reporter, 1 == writeCnt);
197
Brian Salomonf046e152017-01-11 15:24:47 -0500198 testingOnly_getIORefCnts(texture4.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500199 REPORTER_ASSERT(reporter, 1 == refCnt);
200 REPORTER_ASSERT(reporter, 1 == readCnt);
201 REPORTER_ASSERT(reporter, 1 == writeCnt);
202 }
203
204 context->flush();
205
Brian Salomonf046e152017-01-11 15:24:47 -0500206 testingOnly_getIORefCnts(texture1.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500207 REPORTER_ASSERT(reporter, 1 == refCnt);
208 REPORTER_ASSERT(reporter, 0 == readCnt);
209 REPORTER_ASSERT(reporter, 0 == writeCnt);
210
211 if (texelBufferSupport) {
Brian Salomonf046e152017-01-11 15:24:47 -0500212 testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500213 REPORTER_ASSERT(reporter, 1 == refCnt);
214 REPORTER_ASSERT(reporter, 0 == readCnt);
215 REPORTER_ASSERT(reporter, 0 == writeCnt);
216 }
217
218 if (texelBufferSupport) {
Brian Salomonf046e152017-01-11 15:24:47 -0500219 testingOnly_getIORefCnts(texture2.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500220 REPORTER_ASSERT(reporter, 1 == refCnt);
221 REPORTER_ASSERT(reporter, 0 == readCnt);
222 REPORTER_ASSERT(reporter, 0 == writeCnt);
223
Brian Salomonf046e152017-01-11 15:24:47 -0500224 testingOnly_getIORefCnts(texture3.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500225 REPORTER_ASSERT(reporter, 1 == refCnt);
226 REPORTER_ASSERT(reporter, 0 == readCnt);
227 REPORTER_ASSERT(reporter, 0 == writeCnt);
228
Brian Salomonf046e152017-01-11 15:24:47 -0500229 testingOnly_getIORefCnts(texture4.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500230 REPORTER_ASSERT(reporter, 1 == refCnt);
231 REPORTER_ASSERT(reporter, 0 == readCnt);
232 REPORTER_ASSERT(reporter, 0 == writeCnt);
233 }
234 }
235 }
236}
Brian Salomon587e08f2017-01-27 10:59:27 -0500237
238// This test uses the random GrFragmentProcessor test factory, which relies on static initializers.
239#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
240
241static GrColor texel_color(int i, int j) {
242 SkASSERT((unsigned)i < 256 && (unsigned)j < 256);
243 GrColor color = GrColorPackRGBA(j, (uint8_t)(i + j), (uint8_t)(2 * j - i), i);
244 return GrPremulColor(color);
245}
246
247static GrColor4f texel_color4f(int i, int j) { return GrColor4f::FromGrColor(texel_color(i, j)); }
248
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500249void test_draw_op(GrRenderTargetContext* rtc, sk_sp<GrFragmentProcessor> fp,
250 GrTexture* inputDataTexture) {
251 GrPaint paint;
252 paint.addColorTextureProcessor(inputDataTexture, nullptr, SkMatrix::I());
253 paint.addColorFragmentProcessor(std::move(fp));
254 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
255 GrPipelineBuilder pb(std::move(paint), GrAAType::kNone);
256 auto op =
257 GrNonAAFillRectOp::Make(GrColor_WHITE, SkMatrix::I(),
258 SkRect::MakeWH(rtc->width(), rtc->height()), nullptr, nullptr);
259 rtc->addDrawOp(pb, GrNoClip(), std::move(op));
260}
261
Hal Canary6f6961e2017-01-31 13:50:44 -0500262#if GR_TEST_UTILS
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500263DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500264 GrContext* context = ctxInfo.grContext();
265 using FPFactory = GrProcessorTestFactory<GrFragmentProcessor>;
266 SkRandom random;
267 sk_sp<GrRenderTargetContext> rtc = context->makeRenderTargetContext(
268 SkBackingFit::kExact, 256, 256, kRGBA_8888_GrPixelConfig, nullptr);
269 GrSurfaceDesc desc;
270 desc.fWidth = 256;
271 desc.fHeight = 256;
272 desc.fFlags = kRenderTarget_GrSurfaceFlag;
273 desc.fConfig = kRGBA_8888_GrPixelConfig;
274 sk_sp<GrTexture> tex0(context->textureProvider()->createTexture(desc, SkBudgeted::kYes));
275 desc.fConfig = kAlpha_8_GrPixelConfig;
276 sk_sp<GrTexture> tex1(context->textureProvider()->createTexture(desc, SkBudgeted::kYes));
277 GrTexture* textures[] = {tex0.get(), tex1.get()};
278 GrProcessorTestData testData(&random, context, rtc.get(), textures);
279
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500280 std::unique_ptr<GrColor[]> data(new GrColor[256 * 256]);
Brian Salomon587e08f2017-01-27 10:59:27 -0500281 for (int y = 0; y < 256; ++y) {
282 for (int x = 0; x < 256; ++x) {
283 data.get()[256 * y + x] = texel_color(x, y);
284 }
285 }
286 desc.fConfig = kRGBA_8888_GrPixelConfig;
287 sk_sp<GrTexture> dataTexture(context->textureProvider()->createTexture(
288 desc, SkBudgeted::kYes, data.get(), 256 * sizeof(GrColor)));
289
290 // Because processors factories configure themselves in random ways, this is not exhaustive.
291 for (int i = 0; i < FPFactory::Count(); ++i) {
292 int timesToInvokeFactory = 5;
293 // Increase the number of attempts if the FP has child FPs since optimizations likely depend
294 // on child optimizations being present.
295 sk_sp<GrFragmentProcessor> fp = FPFactory::MakeIdx(i, &testData);
296 for (int j = 0; j < fp->numChildProcessors(); ++j) {
297 // This value made a reasonable trade off between time and coverage when this test was
298 // written.
299 timesToInvokeFactory *= FPFactory::Count() / 2;
300 }
301 for (int j = 0; j < timesToInvokeFactory; ++j) {
302 fp = FPFactory::MakeIdx(i, &testData);
303 if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() &&
304 !fp->modulatesInput()) {
305 continue;
306 }
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500307 test_draw_op(rtc.get(), fp, dataTexture.get());
Brian Salomon587e08f2017-01-27 10:59:27 -0500308 memset(data.get(), 0x0, sizeof(GrColor) * 256 * 256);
309 rtc->readPixels(
310 SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType, kPremul_SkAlphaType),
311 data.get(), 0, 0, 0);
312 bool passing = true;
313 if (0) { // Useful to see what FPs are being tested.
314 SkString children;
315 for (int c = 0; c < fp->numChildProcessors(); ++c) {
316 if (!c) {
317 children.append("(");
318 }
319 children.append(fp->childProcessor(c).name());
320 children.append(c == fp->numChildProcessors() - 1 ? ")" : ", ");
321 }
322 SkDebugf("%s %s\n", fp->name(), children.c_str());
323 }
324 for (int y = 0; y < 256 && passing; ++y) {
325 for (int x = 0; x < 256 && passing; ++x) {
326 GrColor input = texel_color(x, y);
327 GrColor output = data.get()[y * 256 + x];
328 if (fp->modulatesInput()) {
329 // A modulating processor is allowed to modulate either the input color or
330 // just the input alpha.
331 bool legalColorModulation =
332 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
333 GrColorUnpackR(output) <= GrColorUnpackR(input) &&
334 GrColorUnpackG(output) <= GrColorUnpackG(input) &&
335 GrColorUnpackB(output) <= GrColorUnpackB(input);
336 bool legalAlphaModulation =
337 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
338 GrColorUnpackR(output) <= GrColorUnpackA(input) &&
339 GrColorUnpackG(output) <= GrColorUnpackA(input) &&
340 GrColorUnpackB(output) <= GrColorUnpackA(input);
341 if (!legalColorModulation && !legalAlphaModulation) {
342 ERRORF(reporter,
343 "\"Modulating\" processor %s made color/alpha value larger. "
344 "Input: 0x%0x8, Output: 0x%08x.",
345 fp->name(), input, output);
346 passing = false;
347 }
348 }
349 GrColor4f input4f = texel_color4f(x, y);
350 GrColor4f output4f = GrColor4f::FromGrColor(output);
351 GrColor4f expected4f;
352 if (fp->hasConstantOutputForConstantInput(input4f, &expected4f)) {
353 float rDiff = fabsf(output4f.fRGBA[0] - expected4f.fRGBA[0]);
354 float gDiff = fabsf(output4f.fRGBA[1] - expected4f.fRGBA[1]);
355 float bDiff = fabsf(output4f.fRGBA[2] - expected4f.fRGBA[2]);
356 float aDiff = fabsf(output4f.fRGBA[3] - expected4f.fRGBA[3]);
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500357 static constexpr float kTol = 4 / 255.f;
Brian Salomon587e08f2017-01-27 10:59:27 -0500358 if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) {
359 ERRORF(reporter,
360 "Processor %s claimed output for const input doesn't match "
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500361 "actual output. Error: %f, Tolerance: %f, input: (%f, %f, %f, "
362 "%f), actual: (%f, %f, %f, %f), expected(%f, %f, %f, %f)",
363 fp->name(), SkTMax(rDiff, SkTMax(gDiff, SkTMax(bDiff, aDiff))),
364 kTol, input4f.fRGBA[0], input4f.fRGBA[1], input4f.fRGBA[2],
365 input4f.fRGBA[3], output4f.fRGBA[0], output4f.fRGBA[1],
366 output4f.fRGBA[2], output4f.fRGBA[3], expected4f.fRGBA[0],
367 expected4f.fRGBA[1], expected4f.fRGBA[2], expected4f.fRGBA[3]);
Brian Salomon587e08f2017-01-27 10:59:27 -0500368 passing = false;
369 }
370 }
371 if (GrColorIsOpaque(input) && fp->preservesOpaqueInput() &&
372 !GrColorIsOpaque(output)) {
373 ERRORF(reporter,
374 "Processor %s claimed opaqueness is preserved but it is not. Input: "
375 "0x%0x8, Output: 0x%08x.",
376 fp->name(), input, output);
377 passing = false;
378 }
379 }
380 }
381 }
382 }
383}
Hal Canary6f6961e2017-01-31 13:50:44 -0500384#endif // GR_TEST_UTILS
385#endif // SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
386#endif // SK_SUPPORT_GPU