blob: 20c9713745419dae6e71041eb7baa3c00a0289ee [file] [log] [blame]
Brian Salomonf9f45122016-11-29 11:59:17 -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 "Test.h"
9
10#if SK_SUPPORT_GPU
11
12#include "GrFragmentProcessor.h"
13#include "GrInvariantOutput.h"
14#include "GrRenderTargetContext.h"
15#include "GrTexture.h"
16#include "glsl/GrGLSLFragmentProcessor.h"
17#include "glsl/GrGLSLFragmentShaderBuilder.h"
18
19DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageStorageLoad, reporter, ctxInfo) {
20 class TestFP : public GrFragmentProcessor {
21 public:
22 static sk_sp<GrFragmentProcessor> Make(sk_sp<GrTexture> texture, GrSLMemoryModel mm,
23 GrSLRestrict restrict) {
24 return sk_sp<GrFragmentProcessor>(new TestFP(std::move(texture), mm, restrict));
25 }
26
27 const char* name() const override { return "Image Load Test FP"; }
28
29 private:
30 TestFP(sk_sp<GrTexture> texture, GrSLMemoryModel mm, GrSLRestrict restrict)
Brian Salomon587e08f2017-01-27 10:59:27 -050031 : INHERITED(kNone_OptimizationFlags)
32 , fImageStorageAccess(std::move(texture), kRead_GrIOType, mm, restrict) {
Brian Salomonf9f45122016-11-29 11:59:17 -050033 this->initClassID<TestFP>();
Ethan Nicholascae3a4c2017-02-02 10:43:58 -050034 this->setWillReadFragmentPosition();
Brian Salomonf9f45122016-11-29 11:59:17 -050035 this->addImageStorageAccess(&fImageStorageAccess);
36 }
37
Brian Salomon94efbf52016-11-29 13:43:05 -050038 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
Brian Salomonf9f45122016-11-29 11:59:17 -050039
40 void onComputeInvariantOutput(GrInvariantOutput* inout) const override {
Brian Salomon5f13fba2017-01-23 14:35:25 -050041 inout->setToUnknown();
Brian Salomonf9f45122016-11-29 11:59:17 -050042 }
43
44 bool onIsEqual(const GrFragmentProcessor& that) const override { return true; }
45
46 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
47 class GLSLProcessor : public GrGLSLFragmentProcessor {
48 public:
49 GLSLProcessor() = default;
50 void emitCode(EmitArgs& args) override {
51 const TestFP& tfp = args.fFp.cast<TestFP>();
52 GrGLSLFPFragmentBuilder* fb = args.fFragBuilder;
53 SkString imageLoadStr;
Ethan Nicholascae3a4c2017-02-02 10:43:58 -050054 fb->codeAppendf("highp vec2 coord = %s.xy;",
55 args.fFragBuilder->fragmentPosition());
Brian Salomonf9f45122016-11-29 11:59:17 -050056 fb->appendImageStorageLoad(&imageLoadStr, args.fImageStorages[0],
57 "ivec2(coord)");
58 if (GrPixelConfigIsSint(tfp.fImageStorageAccess.texture()->config())) {
59 // Map the signed bytes so that when then get read back as unorm values they
60 // will have their original bit pattern.
61 fb->codeAppendf("highp ivec4 ivals = %s;", imageLoadStr.c_str());
62 // NV gives a linker error for this:
63 // fb->codeAppend("ivals +=
64 // "mix(ivec4(0), ivec4(256), lessThan(ivals, ivec4(0)));");
65 fb->codeAppend("if (ivals.r < 0) { ivals.r += 256; }");
66 fb->codeAppend("if (ivals.g < 0) { ivals.g += 256; }");
67 fb->codeAppend("if (ivals.b < 0) { ivals.b += 256; }");
68 fb->codeAppend("if (ivals.a < 0) { ivals.a += 256; }");
69 fb->codeAppendf("%s = vec4(ivals)/255;", args.fOutputColor);
70 } else {
71 fb->codeAppendf("%s = %s;", args.fOutputColor, imageLoadStr.c_str());
72 }
73 }
74 };
75 return new GLSLProcessor;
76 }
77
78 ImageStorageAccess fImageStorageAccess;
Brian Salomon587e08f2017-01-27 10:59:27 -050079 typedef GrFragmentProcessor INHERITED;
Brian Salomonf9f45122016-11-29 11:59:17 -050080 };
81
82 static constexpr int kS = 256;
83 GrContext* context = ctxInfo.grContext();
Brian Salomon8b7a7dd2016-11-29 15:29:41 -050084 if (context->caps()->shaderCaps()->maxFragmentImageStorages() < 1) {
Brian Salomonf9f45122016-11-29 11:59:17 -050085 return;
86 }
87
88 std::unique_ptr<uint32_t[]> data(new uint32_t[kS * kS]);
89 for (int j = 0; j < kS; ++j) {
90 for (int i = 0; i < kS; ++i) {
91 data[i + kS * j] = GrColorPackRGBA(i, j, 0, 0);
92 }
93 }
94
95 std::unique_ptr<uint32_t[]> idata(new uint32_t[kS * kS]);
96 for (int j = 0; j < kS; ++j) {
97 for (int i = 0; i < kS; ++i) {
98 int8_t r = i - 128;
99 int8_t g = j - 128;
100 int8_t b = -128;
101 int8_t a = -128;
102 idata[i + kS * j] = ((uint8_t)a << 24) | ((uint8_t)b << 16) |
103 ((uint8_t)g << 8) | (uint8_t)r;
104 }
105 }
106
107 // Currently image accesses always have "top left" semantics.
108 GrSurfaceDesc desc;
109 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
110 desc.fWidth = kS;
111 desc.fHeight = kS;
112 struct {
113 GrPixelConfig fConfig;
114 std::unique_ptr<uint32_t[]> fData;
115 } tests[] = {
116 {
117 kRGBA_8888_GrPixelConfig,
118 std::move(data)
119 },
120 {
121 kRGBA_8888_sint_GrPixelConfig,
122 std::move(idata)
123 },
124 };
125 for (const auto& test : tests) {
126 // This test should work with any memory model and with or without restrict
127 for (auto mm : {GrSLMemoryModel::kNone,
128 GrSLMemoryModel::kCoherent,
129 GrSLMemoryModel::kVolatile}) {
130 for (auto restrict : {GrSLRestrict::kNo, GrSLRestrict::kYes}) {
131 if (!context->caps()->canConfigBeImageStorage(test.fConfig)) {
132 continue;
133 }
134 desc.fConfig = test.fConfig;
135 sk_sp<GrTexture> imageStorageTexture(context->textureProvider()->createTexture(desc,
136 SkBudgeted::kYes, test.fData.get(), 0));
137
138 sk_sp<GrRenderTargetContext> rtContext =
139 context->makeRenderTargetContext(SkBackingFit::kExact, kS, kS,
140 kRGBA_8888_GrPixelConfig, nullptr);
141 GrPaint paint;
142 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
143 paint.addColorFragmentProcessor(TestFP::Make(imageStorageTexture, mm, restrict));
Brian Salomon82f44312017-01-11 13:42:54 -0500144 rtContext->drawPaint(GrNoClip(), std::move(paint), SkMatrix::I());
Brian Salomonf9f45122016-11-29 11:59:17 -0500145 std::unique_ptr<uint32_t[]> readData(new uint32_t[kS * kS]);
146 SkImageInfo info = SkImageInfo::Make(kS, kS, kRGBA_8888_SkColorType,
147 kPremul_SkAlphaType);
148 rtContext->readPixels(info, readData.get(), 0, 0, 0);
149 int failed = false;
150 for (int j = 0; j < kS && !failed; ++j) {
151 for (int i = 0; i < kS && !failed; ++i) {
152 uint32_t d = test.fData[j * kS + i];
153 uint32_t rd = readData[j * kS + i];
154 if (d != rd) {
155 failed = true;
156 ERRORF(reporter, "Expected 0x%08x, got 0x%08x at %d, %d.", d, rd, i, j);
157 }
158 }
159 }
160 }
161 }
162 }
163}
164
165#endif