blob: d818b39dc4819c7f08df4830f41d63aaa974f536 [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 Phillips8a02f652017-05-12 14:49:16 -040027 return sk_sp<GrFragmentProcessor>(new TestFP(resourceProvider,
28 std::move(proxy), mm, restrict));
Brian Salomonf9f45122016-11-29 11:59:17 -050029 }
30
31 const char* name() const override { return "Image Load Test FP"; }
32
33 private:
Robert Phillips8a02f652017-05-12 14:49:16 -040034 TestFP(GrResourceProvider* resourceProvider,
35 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 Phillips8a02f652017-05-12 14:49:16 -040039 this->addImageStorageAccess(resourceProvider, &fImageStorageAccess);
Brian Salomonf9f45122016-11-29 11:59:17 -050040 }
41
Brian Salomon94efbf52016-11-29 13:43:05 -050042 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
Brian Salomonf9f45122016-11-29 11:59:17 -050043
Brian Salomonf9f45122016-11-29 11:59:17 -050044 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 Nicholas38657112017-02-09 17:01:22 -050054 fb->codeAppend("highp vec2 coord = sk_FragCoord.xy;");
Brian Salomonf9f45122016-11-29 11:59:17 -050055 fb->appendImageStorageLoad(&imageLoadStr, args.fImageStorages[0],
56 "ivec2(coord)");
Robert Phillips9bee2e52017-05-29 12:37:20 -040057 if (GrPixelConfigIsSint(tfp.fImageStorageAccess.peekTexture()->config())) {
Brian Salomonf9f45122016-11-29 11:59:17 -050058 // Map the signed bytes so that when then get read back as unorm values they
59 // will have their original bit pattern.
60 fb->codeAppendf("highp ivec4 ivals = %s;", imageLoadStr.c_str());
61 // NV gives a linker error for this:
62 // fb->codeAppend("ivals +=
63 // "mix(ivec4(0), ivec4(256), lessThan(ivals, ivec4(0)));");
64 fb->codeAppend("if (ivals.r < 0) { ivals.r += 256; }");
65 fb->codeAppend("if (ivals.g < 0) { ivals.g += 256; }");
66 fb->codeAppend("if (ivals.b < 0) { ivals.b += 256; }");
67 fb->codeAppend("if (ivals.a < 0) { ivals.a += 256; }");
68 fb->codeAppendf("%s = vec4(ivals)/255;", args.fOutputColor);
69 } else {
70 fb->codeAppendf("%s = %s;", args.fOutputColor, imageLoadStr.c_str());
71 }
72 }
73 };
74 return new GLSLProcessor;
75 }
76
77 ImageStorageAccess fImageStorageAccess;
Brian Salomon587e08f2017-01-27 10:59:27 -050078 typedef GrFragmentProcessor INHERITED;
Brian Salomonf9f45122016-11-29 11:59:17 -050079 };
80
81 static constexpr int kS = 256;
82 GrContext* context = ctxInfo.grContext();
Brian Salomon8b7a7dd2016-11-29 15:29:41 -050083 if (context->caps()->shaderCaps()->maxFragmentImageStorages() < 1) {
Brian Salomonf9f45122016-11-29 11:59:17 -050084 return;
85 }
86
87 std::unique_ptr<uint32_t[]> data(new uint32_t[kS * kS]);
88 for (int j = 0; j < kS; ++j) {
89 for (int i = 0; i < kS; ++i) {
90 data[i + kS * j] = GrColorPackRGBA(i, j, 0, 0);
91 }
92 }
93
94 std::unique_ptr<uint32_t[]> idata(new uint32_t[kS * kS]);
95 for (int j = 0; j < kS; ++j) {
96 for (int i = 0; i < kS; ++i) {
97 int8_t r = i - 128;
98 int8_t g = j - 128;
99 int8_t b = -128;
100 int8_t a = -128;
101 idata[i + kS * j] = ((uint8_t)a << 24) | ((uint8_t)b << 16) |
102 ((uint8_t)g << 8) | (uint8_t)r;
103 }
104 }
105
106 // Currently image accesses always have "top left" semantics.
107 GrSurfaceDesc desc;
108 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
109 desc.fWidth = kS;
110 desc.fHeight = kS;
111 struct {
112 GrPixelConfig fConfig;
113 std::unique_ptr<uint32_t[]> fData;
114 } tests[] = {
115 {
116 kRGBA_8888_GrPixelConfig,
117 std::move(data)
118 },
119 {
120 kRGBA_8888_sint_GrPixelConfig,
121 std::move(idata)
122 },
123 };
124 for (const auto& test : tests) {
125 // This test should work with any memory model and with or without restrict
126 for (auto mm : {GrSLMemoryModel::kNone,
127 GrSLMemoryModel::kCoherent,
128 GrSLMemoryModel::kVolatile}) {
129 for (auto restrict : {GrSLRestrict::kNo, GrSLRestrict::kYes}) {
130 if (!context->caps()->canConfigBeImageStorage(test.fConfig)) {
131 continue;
132 }
133 desc.fConfig = test.fConfig;
Robert Phillipse78b7252017-04-06 07:59:41 -0400134 sk_sp<GrTextureProxy> imageStorageTexture =
135 GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc,
136 SkBudgeted::kYes, test.fData.get(), 0);
Brian Salomonf9f45122016-11-29 11:59:17 -0500137
138 sk_sp<GrRenderTargetContext> rtContext =
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400139 context->makeDeferredRenderTargetContext(SkBackingFit::kExact, kS, kS,
140 kRGBA_8888_GrPixelConfig, nullptr);
Brian Salomonf9f45122016-11-29 11:59:17 -0500141 GrPaint paint;
142 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Robert Phillipse78b7252017-04-06 07:59:41 -0400143 paint.addColorFragmentProcessor(TestFP::Make(context->resourceProvider(),
144 imageStorageTexture, mm, restrict));
Brian Salomon82f44312017-01-11 13:42:54 -0500145 rtContext->drawPaint(GrNoClip(), std::move(paint), SkMatrix::I());
Brian Salomonf9f45122016-11-29 11:59:17 -0500146 std::unique_ptr<uint32_t[]> readData(new uint32_t[kS * kS]);
147 SkImageInfo info = SkImageInfo::Make(kS, kS, kRGBA_8888_SkColorType,
148 kPremul_SkAlphaType);
149 rtContext->readPixels(info, readData.get(), 0, 0, 0);
150 int failed = false;
151 for (int j = 0; j < kS && !failed; ++j) {
152 for (int i = 0; i < kS && !failed; ++i) {
153 uint32_t d = test.fData[j * kS + i];
154 uint32_t rd = readData[j * kS + i];
155 if (d != rd) {
156 failed = true;
157 ERRORF(reporter, "Expected 0x%08x, got 0x%08x at %d, %d.", d, rd, i, j);
158 }
159 }
160 }
161 }
162 }
163 }
164}
165
166#endif