blob: 6d5cab09ec34a5161a3fdf4c4941fee1c72e2d43 [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"
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:
Brian Salomonaff329b2017-08-11 09:40:37 -040022 static std::unique_ptr<GrFragmentProcessor> Make(sk_sp<GrTextureProxy> proxy,
23 GrSLMemoryModel mm,
24 GrSLRestrict restrict) {
25 return std::unique_ptr<GrFragmentProcessor>(new TestFP(std::move(proxy), mm, restrict));
Brian Salomonf9f45122016-11-29 11:59:17 -050026 }
27
28 const char* name() const override { return "Image Load Test FP"; }
29
Brian Salomonaff329b2017-08-11 09:40:37 -040030 std::unique_ptr<GrFragmentProcessor> clone() const override {
31 return std::unique_ptr<GrFragmentProcessor>(new TestFP(*this));
Brian Salomonb17e6392017-07-28 13:41:51 -040032 }
33
Brian Salomonf9f45122016-11-29 11:59:17 -050034 private:
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040035 TestFP(sk_sp<GrTextureProxy> proxy, GrSLMemoryModel mm, GrSLRestrict restrict)
Brian Salomon587e08f2017-01-27 10:59:27 -050036 : INHERITED(kNone_OptimizationFlags)
Robert Phillips8a02f652017-05-12 14:49:16 -040037 , fImageStorageAccess(std::move(proxy), kRead_GrIOType, mm, restrict) {
Brian Salomonf9f45122016-11-29 11:59:17 -050038 this->initClassID<TestFP>();
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040039 this->addImageStorageAccess(&fImageStorageAccess);
Brian Salomonf9f45122016-11-29 11:59:17 -050040 }
41
Brian Salomonb17e6392017-07-28 13:41:51 -040042 explicit TestFP(const TestFP& that)
43 : INHERITED(that.optimizationFlags())
44 , fImageStorageAccess(that.fImageStorageAccess) {
45 this->initClassID<TestFP>();
46 this->addImageStorageAccess(&fImageStorageAccess);
47 }
48
Brian Salomon94efbf52016-11-29 13:43:05 -050049 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
Brian Salomonf9f45122016-11-29 11:59:17 -050050
Brian Salomonf9f45122016-11-29 11:59:17 -050051 bool onIsEqual(const GrFragmentProcessor& that) const override { return true; }
52
53 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
54 class GLSLProcessor : public GrGLSLFragmentProcessor {
55 public:
56 GLSLProcessor() = default;
57 void emitCode(EmitArgs& args) override {
58 const TestFP& tfp = args.fFp.cast<TestFP>();
59 GrGLSLFPFragmentBuilder* fb = args.fFragBuilder;
60 SkString imageLoadStr;
Brian Salomon1d816b92017-08-17 11:07:59 -040061 fb->codeAppend("highp float2 coord = sk_FragCoord.xy;");
Brian Salomonf9f45122016-11-29 11:59:17 -050062 fb->appendImageStorageLoad(&imageLoadStr, args.fImageStorages[0],
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040063 "int2(coord)");
Robert Phillips9bee2e52017-05-29 12:37:20 -040064 if (GrPixelConfigIsSint(tfp.fImageStorageAccess.peekTexture()->config())) {
Brian Salomonf9f45122016-11-29 11:59:17 -050065 // Map the signed bytes so that when then get read back as unorm values they
66 // will have their original bit pattern.
Brian Salomon1d816b92017-08-17 11:07:59 -040067 fb->codeAppendf("highp int4 ivals = %s;", imageLoadStr.c_str());
Brian Salomonf9f45122016-11-29 11:59:17 -050068 // NV gives a linker error for this:
69 // fb->codeAppend("ivals +=
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040070 // "mix(int4(0), int4(256), lessThan(ivals, int4(0)));");
Brian Salomonf9f45122016-11-29 11:59:17 -050071 fb->codeAppend("if (ivals.r < 0) { ivals.r += 256; }");
72 fb->codeAppend("if (ivals.g < 0) { ivals.g += 256; }");
73 fb->codeAppend("if (ivals.b < 0) { ivals.b += 256; }");
74 fb->codeAppend("if (ivals.a < 0) { ivals.a += 256; }");
Brian Salomon1d816b92017-08-17 11:07:59 -040075 fb->codeAppendf("%s = float4(ivals)/255;", args.fOutputColor);
Brian Salomonf9f45122016-11-29 11:59:17 -050076 } else {
77 fb->codeAppendf("%s = %s;", args.fOutputColor, imageLoadStr.c_str());
78 }
79 }
80 };
81 return new GLSLProcessor;
82 }
83
84 ImageStorageAccess fImageStorageAccess;
Brian Salomon587e08f2017-01-27 10:59:27 -050085 typedef GrFragmentProcessor INHERITED;
Brian Salomonf9f45122016-11-29 11:59:17 -050086 };
87
88 static constexpr int kS = 256;
89 GrContext* context = ctxInfo.grContext();
Brian Salomon8b7a7dd2016-11-29 15:29:41 -050090 if (context->caps()->shaderCaps()->maxFragmentImageStorages() < 1) {
Brian Salomonf9f45122016-11-29 11:59:17 -050091 return;
92 }
93
94 std::unique_ptr<uint32_t[]> data(new uint32_t[kS * kS]);
95 for (int j = 0; j < kS; ++j) {
96 for (int i = 0; i < kS; ++i) {
97 data[i + kS * j] = GrColorPackRGBA(i, j, 0, 0);
98 }
99 }
100
101 std::unique_ptr<uint32_t[]> idata(new uint32_t[kS * kS]);
102 for (int j = 0; j < kS; ++j) {
103 for (int i = 0; i < kS; ++i) {
104 int8_t r = i - 128;
105 int8_t g = j - 128;
106 int8_t b = -128;
107 int8_t a = -128;
108 idata[i + kS * j] = ((uint8_t)a << 24) | ((uint8_t)b << 16) |
109 ((uint8_t)g << 8) | (uint8_t)r;
110 }
111 }
112
113 // Currently image accesses always have "top left" semantics.
114 GrSurfaceDesc desc;
115 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
116 desc.fWidth = kS;
117 desc.fHeight = kS;
118 struct {
119 GrPixelConfig fConfig;
120 std::unique_ptr<uint32_t[]> fData;
121 } tests[] = {
122 {
123 kRGBA_8888_GrPixelConfig,
124 std::move(data)
125 },
126 {
127 kRGBA_8888_sint_GrPixelConfig,
128 std::move(idata)
129 },
130 };
131 for (const auto& test : tests) {
132 // This test should work with any memory model and with or without restrict
133 for (auto mm : {GrSLMemoryModel::kNone,
134 GrSLMemoryModel::kCoherent,
135 GrSLMemoryModel::kVolatile}) {
136 for (auto restrict : {GrSLRestrict::kNo, GrSLRestrict::kYes}) {
137 if (!context->caps()->canConfigBeImageStorage(test.fConfig)) {
138 continue;
139 }
140 desc.fConfig = test.fConfig;
Robert Phillipse78b7252017-04-06 07:59:41 -0400141 sk_sp<GrTextureProxy> imageStorageTexture =
142 GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc,
143 SkBudgeted::kYes, test.fData.get(), 0);
Brian Salomonf9f45122016-11-29 11:59:17 -0500144
145 sk_sp<GrRenderTargetContext> rtContext =
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400146 context->makeDeferredRenderTargetContext(SkBackingFit::kExact, kS, kS,
147 kRGBA_8888_GrPixelConfig, nullptr);
Brian Salomonb17e6392017-07-28 13:41:51 -0400148 // We make a clone to test that copying GrFragmentProcessor::ImageStorageAccess
Brian Salomonaff329b2017-08-11 09:40:37 -0400149 // works.
150 std::unique_ptr<GrFragmentProcessor> fps[2];
151 fps[0] = TestFP::Make(imageStorageTexture, mm, restrict);
152 fps[1] = fps[0]->clone();
153 for (auto& fp : fps) {
Brian Salomonb17e6392017-07-28 13:41:51 -0400154 GrPaint paint;
155 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonaff329b2017-08-11 09:40:37 -0400156 paint.addColorFragmentProcessor(std::move(fp));
Brian Salomonb17e6392017-07-28 13:41:51 -0400157 rtContext->drawPaint(GrNoClip(), std::move(paint), SkMatrix::I());
158 std::unique_ptr<uint32_t[]> readData(new uint32_t[kS * kS]);
159 SkImageInfo info = SkImageInfo::Make(kS, kS, kRGBA_8888_SkColorType,
160 kPremul_SkAlphaType);
161 rtContext->readPixels(info, readData.get(), 0, 0, 0);
162 int failed = false;
163 for (int j = 0; j < kS && !failed; ++j) {
164 for (int i = 0; i < kS && !failed; ++i) {
165 uint32_t d = test.fData[j * kS + i];
166 uint32_t rd = readData[j * kS + i];
167 if (d != rd) {
168 failed = true;
169 ERRORF(reporter, "Expected 0x%08x, got 0x%08x at %d, %d.",
170 d, rd, i, j);
171 }
Brian Salomonf9f45122016-11-29 11:59:17 -0500172 }
173 }
174 }
175 }
176 }
177 }
178}
179
180#endif