blob: 678fbaa245734becbe6d7b710b6f701ba024b313 [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;
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500274
275 // Put premul data into the RGBA texture that the test FPs can optionally use.
276 std::unique_ptr<GrColor[]> rgbaData(new GrColor[256 * 256]);
277 for (int y = 0; y < 256; ++y) {
278 for (int x = 0; x < 256; ++x) {
279 rgbaData.get()[256 * y + x] =
280 texel_color(random.nextULessThan(256), random.nextULessThan(256));
281 }
282 }
283 sk_sp<GrTexture> tex0(context->textureProvider()->createTexture(
284 desc, SkBudgeted::kYes, rgbaData.get(), 256 * sizeof(GrColor)));
285
286 // Put random values into the alpha texture that the test FPs can optionally use.
Brian Salomon587e08f2017-01-27 10:59:27 -0500287 desc.fConfig = kAlpha_8_GrPixelConfig;
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500288 std::unique_ptr<uint8_t[]> alphaData(new uint8_t[256 * 256]);
289 for (int y = 0; y < 256; ++y) {
290 for (int x = 0; x < 256; ++x) {
291 alphaData.get()[256 * y + x] = random.nextULessThan(256);
292 }
293 }
294 sk_sp<GrTexture> tex1(context->textureProvider()->createTexture(desc, SkBudgeted::kYes,
295 alphaData.get(), 256));
Brian Salomon587e08f2017-01-27 10:59:27 -0500296 GrTexture* textures[] = {tex0.get(), tex1.get()};
297 GrProcessorTestData testData(&random, context, rtc.get(), textures);
298
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500299 // Use a different array of premul colors for the output of the fragment processor that preceeds
300 // the fragment processor under test.
Brian Salomon587e08f2017-01-27 10:59:27 -0500301 for (int y = 0; y < 256; ++y) {
302 for (int x = 0; x < 256; ++x) {
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500303 rgbaData.get()[256 * y + x] = texel_color(x, y);
Brian Salomon587e08f2017-01-27 10:59:27 -0500304 }
305 }
306 desc.fConfig = kRGBA_8888_GrPixelConfig;
307 sk_sp<GrTexture> dataTexture(context->textureProvider()->createTexture(
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500308 desc, SkBudgeted::kYes, rgbaData.get(), 256 * sizeof(GrColor)));
Brian Salomon587e08f2017-01-27 10:59:27 -0500309
310 // Because processors factories configure themselves in random ways, this is not exhaustive.
311 for (int i = 0; i < FPFactory::Count(); ++i) {
312 int timesToInvokeFactory = 5;
313 // Increase the number of attempts if the FP has child FPs since optimizations likely depend
314 // on child optimizations being present.
315 sk_sp<GrFragmentProcessor> fp = FPFactory::MakeIdx(i, &testData);
316 for (int j = 0; j < fp->numChildProcessors(); ++j) {
317 // This value made a reasonable trade off between time and coverage when this test was
318 // written.
319 timesToInvokeFactory *= FPFactory::Count() / 2;
320 }
321 for (int j = 0; j < timesToInvokeFactory; ++j) {
322 fp = FPFactory::MakeIdx(i, &testData);
323 if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() &&
324 !fp->modulatesInput()) {
325 continue;
326 }
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500327 test_draw_op(rtc.get(), fp, dataTexture.get());
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500328 memset(rgbaData.get(), 0x0, sizeof(GrColor) * 256 * 256);
Brian Salomon587e08f2017-01-27 10:59:27 -0500329 rtc->readPixels(
330 SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType, kPremul_SkAlphaType),
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500331 rgbaData.get(), 0, 0, 0);
Brian Salomon587e08f2017-01-27 10:59:27 -0500332 bool passing = true;
333 if (0) { // Useful to see what FPs are being tested.
334 SkString children;
335 for (int c = 0; c < fp->numChildProcessors(); ++c) {
336 if (!c) {
337 children.append("(");
338 }
339 children.append(fp->childProcessor(c).name());
340 children.append(c == fp->numChildProcessors() - 1 ? ")" : ", ");
341 }
342 SkDebugf("%s %s\n", fp->name(), children.c_str());
343 }
344 for (int y = 0; y < 256 && passing; ++y) {
345 for (int x = 0; x < 256 && passing; ++x) {
346 GrColor input = texel_color(x, y);
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500347 GrColor output = rgbaData.get()[y * 256 + x];
Brian Salomon587e08f2017-01-27 10:59:27 -0500348 if (fp->modulatesInput()) {
349 // A modulating processor is allowed to modulate either the input color or
350 // just the input alpha.
351 bool legalColorModulation =
352 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
353 GrColorUnpackR(output) <= GrColorUnpackR(input) &&
354 GrColorUnpackG(output) <= GrColorUnpackG(input) &&
355 GrColorUnpackB(output) <= GrColorUnpackB(input);
356 bool legalAlphaModulation =
357 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
358 GrColorUnpackR(output) <= GrColorUnpackA(input) &&
359 GrColorUnpackG(output) <= GrColorUnpackA(input) &&
360 GrColorUnpackB(output) <= GrColorUnpackA(input);
361 if (!legalColorModulation && !legalAlphaModulation) {
362 ERRORF(reporter,
363 "\"Modulating\" processor %s made color/alpha value larger. "
364 "Input: 0x%0x8, Output: 0x%08x.",
365 fp->name(), input, output);
366 passing = false;
367 }
368 }
369 GrColor4f input4f = texel_color4f(x, y);
370 GrColor4f output4f = GrColor4f::FromGrColor(output);
371 GrColor4f expected4f;
372 if (fp->hasConstantOutputForConstantInput(input4f, &expected4f)) {
373 float rDiff = fabsf(output4f.fRGBA[0] - expected4f.fRGBA[0]);
374 float gDiff = fabsf(output4f.fRGBA[1] - expected4f.fRGBA[1]);
375 float bDiff = fabsf(output4f.fRGBA[2] - expected4f.fRGBA[2]);
376 float aDiff = fabsf(output4f.fRGBA[3] - expected4f.fRGBA[3]);
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500377 static constexpr float kTol = 4 / 255.f;
Brian Salomon587e08f2017-01-27 10:59:27 -0500378 if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) {
379 ERRORF(reporter,
380 "Processor %s claimed output for const input doesn't match "
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500381 "actual output. Error: %f, Tolerance: %f, input: (%f, %f, %f, "
382 "%f), actual: (%f, %f, %f, %f), expected(%f, %f, %f, %f)",
383 fp->name(), SkTMax(rDiff, SkTMax(gDiff, SkTMax(bDiff, aDiff))),
384 kTol, input4f.fRGBA[0], input4f.fRGBA[1], input4f.fRGBA[2],
385 input4f.fRGBA[3], output4f.fRGBA[0], output4f.fRGBA[1],
386 output4f.fRGBA[2], output4f.fRGBA[3], expected4f.fRGBA[0],
387 expected4f.fRGBA[1], expected4f.fRGBA[2], expected4f.fRGBA[3]);
Brian Salomon587e08f2017-01-27 10:59:27 -0500388 passing = false;
389 }
390 }
391 if (GrColorIsOpaque(input) && fp->preservesOpaqueInput() &&
392 !GrColorIsOpaque(output)) {
393 ERRORF(reporter,
394 "Processor %s claimed opaqueness is preserved but it is not. Input: "
395 "0x%0x8, Output: 0x%08x.",
396 fp->name(), input, output);
397 passing = false;
398 }
399 }
400 }
401 }
402 }
403}
Hal Canary6f6961e2017-01-31 13:50:44 -0500404#endif // GR_TEST_UTILS
405#endif // SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
406#endif // SK_SUPPORT_GPU