blob: a8704c6264ade760175097a5e2d4d90eac744e7b [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)
Ethan Nicholasabff9562017-10-09 10:54:08 -040036 : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags)
Robert Phillips8a02f652017-05-12 14:49:16 -040037 , fImageStorageAccess(std::move(proxy), kRead_GrIOType, mm, restrict) {
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040038 this->addImageStorageAccess(&fImageStorageAccess);
Brian Salomonf9f45122016-11-29 11:59:17 -050039 }
40
Brian Salomonb17e6392017-07-28 13:41:51 -040041 explicit TestFP(const TestFP& that)
Ethan Nicholasabff9562017-10-09 10:54:08 -040042 : INHERITED(kTestFP_ClassID, that.optimizationFlags())
Brian Salomonb17e6392017-07-28 13:41:51 -040043 , fImageStorageAccess(that.fImageStorageAccess) {
Brian Salomonb17e6392017-07-28 13:41:51 -040044 this->addImageStorageAccess(&fImageStorageAccess);
45 }
46
Brian Salomon94efbf52016-11-29 13:43:05 -050047 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
Brian Salomonf9f45122016-11-29 11:59:17 -050048
Brian Salomonf9f45122016-11-29 11:59:17 -050049 bool onIsEqual(const GrFragmentProcessor& that) const override { return true; }
50
51 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
52 class GLSLProcessor : public GrGLSLFragmentProcessor {
53 public:
54 GLSLProcessor() = default;
55 void emitCode(EmitArgs& args) override {
56 const TestFP& tfp = args.fFp.cast<TestFP>();
57 GrGLSLFPFragmentBuilder* fb = args.fFragBuilder;
58 SkString imageLoadStr;
Ethan Nicholas8aa45692017-09-20 11:24:15 -040059 fb->codeAppend("float2 coord = sk_FragCoord.xy;");
Brian Salomonf9f45122016-11-29 11:59:17 -050060 fb->appendImageStorageLoad(&imageLoadStr, args.fImageStorages[0],
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040061 "int2(coord)");
Robert Phillips9bee2e52017-05-29 12:37:20 -040062 if (GrPixelConfigIsSint(tfp.fImageStorageAccess.peekTexture()->config())) {
Brian Salomonf9f45122016-11-29 11:59:17 -050063 // Map the signed bytes so that when then get read back as unorm values they
64 // will have their original bit pattern.
Ethan Nicholasf7b88202017-09-18 14:10:39 -040065 fb->codeAppendf("int4 ivals = %s;", imageLoadStr.c_str());
Brian Salomonf9f45122016-11-29 11:59:17 -050066 // NV gives a linker error for this:
67 // fb->codeAppend("ivals +=
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040068 // "mix(int4(0), int4(256), lessThan(ivals, int4(0)));");
Brian Salomonf9f45122016-11-29 11:59:17 -050069 fb->codeAppend("if (ivals.r < 0) { ivals.r += 256; }");
70 fb->codeAppend("if (ivals.g < 0) { ivals.g += 256; }");
71 fb->codeAppend("if (ivals.b < 0) { ivals.b += 256; }");
72 fb->codeAppend("if (ivals.a < 0) { ivals.a += 256; }");
Ethan Nicholasf7b88202017-09-18 14:10:39 -040073 fb->codeAppendf("%s = half4(ivals)/255;", args.fOutputColor);
Brian Salomonf9f45122016-11-29 11:59:17 -050074 } else {
75 fb->codeAppendf("%s = %s;", args.fOutputColor, imageLoadStr.c_str());
76 }
77 }
78 };
79 return new GLSLProcessor;
80 }
81
82 ImageStorageAccess fImageStorageAccess;
Brian Salomon587e08f2017-01-27 10:59:27 -050083 typedef GrFragmentProcessor INHERITED;
Brian Salomonf9f45122016-11-29 11:59:17 -050084 };
85
86 static constexpr int kS = 256;
87 GrContext* context = ctxInfo.grContext();
Brian Salomon8b7a7dd2016-11-29 15:29:41 -050088 if (context->caps()->shaderCaps()->maxFragmentImageStorages() < 1) {
Brian Salomonf9f45122016-11-29 11:59:17 -050089 return;
90 }
91
92 std::unique_ptr<uint32_t[]> data(new uint32_t[kS * kS]);
93 for (int j = 0; j < kS; ++j) {
94 for (int i = 0; i < kS; ++i) {
95 data[i + kS * j] = GrColorPackRGBA(i, j, 0, 0);
96 }
97 }
98
99 std::unique_ptr<uint32_t[]> idata(new uint32_t[kS * kS]);
100 for (int j = 0; j < kS; ++j) {
101 for (int i = 0; i < kS; ++i) {
102 int8_t r = i - 128;
103 int8_t g = j - 128;
104 int8_t b = -128;
105 int8_t a = -128;
106 idata[i + kS * j] = ((uint8_t)a << 24) | ((uint8_t)b << 16) |
107 ((uint8_t)g << 8) | (uint8_t)r;
108 }
109 }
110
111 // Currently image accesses always have "top left" semantics.
112 GrSurfaceDesc desc;
113 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
114 desc.fWidth = kS;
115 desc.fHeight = kS;
116 struct {
117 GrPixelConfig fConfig;
118 std::unique_ptr<uint32_t[]> fData;
119 } tests[] = {
120 {
121 kRGBA_8888_GrPixelConfig,
122 std::move(data)
123 },
124 {
125 kRGBA_8888_sint_GrPixelConfig,
126 std::move(idata)
127 },
128 };
129 for (const auto& test : tests) {
130 // This test should work with any memory model and with or without restrict
131 for (auto mm : {GrSLMemoryModel::kNone,
132 GrSLMemoryModel::kCoherent,
133 GrSLMemoryModel::kVolatile}) {
134 for (auto restrict : {GrSLRestrict::kNo, GrSLRestrict::kYes}) {
135 if (!context->caps()->canConfigBeImageStorage(test.fConfig)) {
136 continue;
137 }
138 desc.fConfig = test.fConfig;
Robert Phillipse78b7252017-04-06 07:59:41 -0400139 sk_sp<GrTextureProxy> imageStorageTexture =
140 GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc,
141 SkBudgeted::kYes, test.fData.get(), 0);
Brian Salomonf9f45122016-11-29 11:59:17 -0500142
143 sk_sp<GrRenderTargetContext> rtContext =
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400144 context->makeDeferredRenderTargetContext(SkBackingFit::kExact, kS, kS,
145 kRGBA_8888_GrPixelConfig, nullptr);
Brian Salomonb17e6392017-07-28 13:41:51 -0400146 // We make a clone to test that copying GrFragmentProcessor::ImageStorageAccess
Brian Salomonaff329b2017-08-11 09:40:37 -0400147 // works.
148 std::unique_ptr<GrFragmentProcessor> fps[2];
149 fps[0] = TestFP::Make(imageStorageTexture, mm, restrict);
150 fps[1] = fps[0]->clone();
151 for (auto& fp : fps) {
Brian Salomonb17e6392017-07-28 13:41:51 -0400152 GrPaint paint;
153 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonaff329b2017-08-11 09:40:37 -0400154 paint.addColorFragmentProcessor(std::move(fp));
Brian Salomonb17e6392017-07-28 13:41:51 -0400155 rtContext->drawPaint(GrNoClip(), std::move(paint), SkMatrix::I());
156 std::unique_ptr<uint32_t[]> readData(new uint32_t[kS * kS]);
157 SkImageInfo info = SkImageInfo::Make(kS, kS, kRGBA_8888_SkColorType,
158 kPremul_SkAlphaType);
159 rtContext->readPixels(info, readData.get(), 0, 0, 0);
160 int failed = false;
161 for (int j = 0; j < kS && !failed; ++j) {
162 for (int i = 0; i < kS && !failed; ++i) {
163 uint32_t d = test.fData[j * kS + i];
164 uint32_t rd = readData[j * kS + i];
165 if (d != rd) {
166 failed = true;
167 ERRORF(reporter, "Expected 0x%08x, got 0x%08x at %d, %d.",
168 d, rd, i, j);
169 }
Brian Salomonf9f45122016-11-29 11:59:17 -0500170 }
171 }
172 }
173 }
174 }
175 }
176}
177
178#endif