blob: dc4d21294db4c048a3ea04e18b5f551d1a9eb165 [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
Brian Salomonc65aec92017-03-09 09:03:58 -050012#include "GrClip.h"
Brian Salomonf9f45122016-11-29 11:59:17 -050013#include "GrFragmentProcessor.h"
Brian Salomonf9f45122016-11-29 11:59:17 -050014#include "GrRenderTargetContext.h"
Brian Osman32342f02017-03-04 08:12:46 -050015#include "GrResourceProvider.h"
Brian Salomonf9f45122016-11-29 11:59:17 -050016#include "GrTexture.h"
17#include "glsl/GrGLSLFragmentProcessor.h"
18#include "glsl/GrGLSLFragmentShaderBuilder.h"
19
20DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageStorageLoad, reporter, ctxInfo) {
21 class TestFP : public GrFragmentProcessor {
22 public:
Robert Phillipse78b7252017-04-06 07:59:41 -040023 static sk_sp<GrFragmentProcessor> Make(GrResourceProvider* resourceProvider,
24 sk_sp<GrTextureProxy> proxy,
25 GrSLMemoryModel mm,
Brian Salomonf9f45122016-11-29 11:59:17 -050026 GrSLRestrict restrict) {
Robert Phillipse78b7252017-04-06 07:59:41 -040027 // MDB TODO: remove this once ImageStorageAccess is converted to GrTextureProxy
28 sk_sp<GrTexture> tex(sk_ref_sp(proxy->instantiate(resourceProvider)));
29 if (!tex) {
30 return nullptr;
31 }
32
33 return sk_sp<GrFragmentProcessor>(new TestFP(std::move(tex), mm, restrict));
Brian Salomonf9f45122016-11-29 11:59:17 -050034 }
35
36 const char* name() const override { return "Image Load Test FP"; }
37
38 private:
39 TestFP(sk_sp<GrTexture> texture, GrSLMemoryModel mm, GrSLRestrict restrict)
Brian Salomon587e08f2017-01-27 10:59:27 -050040 : INHERITED(kNone_OptimizationFlags)
41 , fImageStorageAccess(std::move(texture), kRead_GrIOType, mm, restrict) {
Brian Salomonf9f45122016-11-29 11:59:17 -050042 this->initClassID<TestFP>();
Brian Salomonf9f45122016-11-29 11:59:17 -050043 this->addImageStorageAccess(&fImageStorageAccess);
44 }
45
Brian Salomon94efbf52016-11-29 13:43:05 -050046 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
Brian Salomonf9f45122016-11-29 11:59:17 -050047
Brian Salomonf9f45122016-11-29 11:59:17 -050048 bool onIsEqual(const GrFragmentProcessor& that) const override { return true; }
49
50 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
51 class GLSLProcessor : public GrGLSLFragmentProcessor {
52 public:
53 GLSLProcessor() = default;
54 void emitCode(EmitArgs& args) override {
55 const TestFP& tfp = args.fFp.cast<TestFP>();
56 GrGLSLFPFragmentBuilder* fb = args.fFragBuilder;
57 SkString imageLoadStr;
Ethan Nicholas38657112017-02-09 17:01:22 -050058 fb->codeAppend("highp vec2 coord = sk_FragCoord.xy;");
Brian Salomonf9f45122016-11-29 11:59:17 -050059 fb->appendImageStorageLoad(&imageLoadStr, args.fImageStorages[0],
60 "ivec2(coord)");
61 if (GrPixelConfigIsSint(tfp.fImageStorageAccess.texture()->config())) {
62 // Map the signed bytes so that when then get read back as unorm values they
63 // will have their original bit pattern.
64 fb->codeAppendf("highp ivec4 ivals = %s;", imageLoadStr.c_str());
65 // NV gives a linker error for this:
66 // fb->codeAppend("ivals +=
67 // "mix(ivec4(0), ivec4(256), lessThan(ivals, ivec4(0)));");
68 fb->codeAppend("if (ivals.r < 0) { ivals.r += 256; }");
69 fb->codeAppend("if (ivals.g < 0) { ivals.g += 256; }");
70 fb->codeAppend("if (ivals.b < 0) { ivals.b += 256; }");
71 fb->codeAppend("if (ivals.a < 0) { ivals.a += 256; }");
72 fb->codeAppendf("%s = vec4(ivals)/255;", args.fOutputColor);
73 } else {
74 fb->codeAppendf("%s = %s;", args.fOutputColor, imageLoadStr.c_str());
75 }
76 }
77 };
78 return new GLSLProcessor;
79 }
80
81 ImageStorageAccess fImageStorageAccess;
Brian Salomon587e08f2017-01-27 10:59:27 -050082 typedef GrFragmentProcessor INHERITED;
Brian Salomonf9f45122016-11-29 11:59:17 -050083 };
84
85 static constexpr int kS = 256;
86 GrContext* context = ctxInfo.grContext();
Brian Salomon8b7a7dd2016-11-29 15:29:41 -050087 if (context->caps()->shaderCaps()->maxFragmentImageStorages() < 1) {
Brian Salomonf9f45122016-11-29 11:59:17 -050088 return;
89 }
90
91 std::unique_ptr<uint32_t[]> data(new uint32_t[kS * kS]);
92 for (int j = 0; j < kS; ++j) {
93 for (int i = 0; i < kS; ++i) {
94 data[i + kS * j] = GrColorPackRGBA(i, j, 0, 0);
95 }
96 }
97
98 std::unique_ptr<uint32_t[]> idata(new uint32_t[kS * kS]);
99 for (int j = 0; j < kS; ++j) {
100 for (int i = 0; i < kS; ++i) {
101 int8_t r = i - 128;
102 int8_t g = j - 128;
103 int8_t b = -128;
104 int8_t a = -128;
105 idata[i + kS * j] = ((uint8_t)a << 24) | ((uint8_t)b << 16) |
106 ((uint8_t)g << 8) | (uint8_t)r;
107 }
108 }
109
110 // Currently image accesses always have "top left" semantics.
111 GrSurfaceDesc desc;
112 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
113 desc.fWidth = kS;
114 desc.fHeight = kS;
115 struct {
116 GrPixelConfig fConfig;
117 std::unique_ptr<uint32_t[]> fData;
118 } tests[] = {
119 {
120 kRGBA_8888_GrPixelConfig,
121 std::move(data)
122 },
123 {
124 kRGBA_8888_sint_GrPixelConfig,
125 std::move(idata)
126 },
127 };
128 for (const auto& test : tests) {
129 // This test should work with any memory model and with or without restrict
130 for (auto mm : {GrSLMemoryModel::kNone,
131 GrSLMemoryModel::kCoherent,
132 GrSLMemoryModel::kVolatile}) {
133 for (auto restrict : {GrSLRestrict::kNo, GrSLRestrict::kYes}) {
134 if (!context->caps()->canConfigBeImageStorage(test.fConfig)) {
135 continue;
136 }
137 desc.fConfig = test.fConfig;
Robert Phillipse78b7252017-04-06 07:59:41 -0400138 sk_sp<GrTextureProxy> imageStorageTexture =
139 GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc,
140 SkBudgeted::kYes, test.fData.get(), 0);
Brian Salomonf9f45122016-11-29 11:59:17 -0500141
142 sk_sp<GrRenderTargetContext> rtContext =
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400143 context->makeDeferredRenderTargetContext(SkBackingFit::kExact, kS, kS,
144 kRGBA_8888_GrPixelConfig, nullptr);
Brian Salomonf9f45122016-11-29 11:59:17 -0500145 GrPaint paint;
146 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Robert Phillipse78b7252017-04-06 07:59:41 -0400147 paint.addColorFragmentProcessor(TestFP::Make(context->resourceProvider(),
148 imageStorageTexture, mm, restrict));
Brian Salomon82f44312017-01-11 13:42:54 -0500149 rtContext->drawPaint(GrNoClip(), std::move(paint), SkMatrix::I());
Brian Salomonf9f45122016-11-29 11:59:17 -0500150 std::unique_ptr<uint32_t[]> readData(new uint32_t[kS * kS]);
151 SkImageInfo info = SkImageInfo::Make(kS, kS, kRGBA_8888_SkColorType,
152 kPremul_SkAlphaType);
153 rtContext->readPixels(info, readData.get(), 0, 0, 0);
154 int failed = false;
155 for (int j = 0; j < kS && !failed; ++j) {
156 for (int i = 0; i < kS && !failed; ++i) {
157 uint32_t d = test.fData[j * kS + i];
158 uint32_t rd = readData[j * kS + i];
159 if (d != rd) {
160 failed = true;
161 ERRORF(reporter, "Expected 0x%08x, got 0x%08x at %d, %d.", d, rd, i, j);
162 }
163 }
164 }
165 }
166 }
167 }
168}
169
170#endif