blob: f55553c5182cc1e544114a74ceff3ac90b9196c4 [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:
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040022 static sk_sp<GrFragmentProcessor> Make(sk_sp<GrTextureProxy> proxy,
Robert Phillipse78b7252017-04-06 07:59:41 -040023 GrSLMemoryModel mm,
Brian Salomonf9f45122016-11-29 11:59:17 -050024 GrSLRestrict restrict) {
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040025 return sk_sp<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
30 private:
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040031 TestFP(sk_sp<GrTextureProxy> proxy, GrSLMemoryModel mm, GrSLRestrict restrict)
Brian Salomon587e08f2017-01-27 10:59:27 -050032 : INHERITED(kNone_OptimizationFlags)
Robert Phillips8a02f652017-05-12 14:49:16 -040033 , fImageStorageAccess(std::move(proxy), kRead_GrIOType, mm, restrict) {
Brian Salomonf9f45122016-11-29 11:59:17 -050034 this->initClassID<TestFP>();
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040035 this->addImageStorageAccess(&fImageStorageAccess);
Brian Salomonf9f45122016-11-29 11:59:17 -050036 }
37
Brian Salomon94efbf52016-11-29 13:43:05 -050038 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
Brian Salomonf9f45122016-11-29 11:59:17 -050039
Brian Salomonf9f45122016-11-29 11:59:17 -050040 bool onIsEqual(const GrFragmentProcessor& that) const override { return true; }
41
42 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
43 class GLSLProcessor : public GrGLSLFragmentProcessor {
44 public:
45 GLSLProcessor() = default;
46 void emitCode(EmitArgs& args) override {
47 const TestFP& tfp = args.fFp.cast<TestFP>();
48 GrGLSLFPFragmentBuilder* fb = args.fFragBuilder;
49 SkString imageLoadStr;
Ethan Nicholas38657112017-02-09 17:01:22 -050050 fb->codeAppend("highp vec2 coord = sk_FragCoord.xy;");
Brian Salomonf9f45122016-11-29 11:59:17 -050051 fb->appendImageStorageLoad(&imageLoadStr, args.fImageStorages[0],
52 "ivec2(coord)");
Robert Phillips9bee2e52017-05-29 12:37:20 -040053 if (GrPixelConfigIsSint(tfp.fImageStorageAccess.peekTexture()->config())) {
Brian Salomonf9f45122016-11-29 11:59:17 -050054 // Map the signed bytes so that when then get read back as unorm values they
55 // will have their original bit pattern.
56 fb->codeAppendf("highp ivec4 ivals = %s;", imageLoadStr.c_str());
57 // NV gives a linker error for this:
58 // fb->codeAppend("ivals +=
59 // "mix(ivec4(0), ivec4(256), lessThan(ivals, ivec4(0)));");
60 fb->codeAppend("if (ivals.r < 0) { ivals.r += 256; }");
61 fb->codeAppend("if (ivals.g < 0) { ivals.g += 256; }");
62 fb->codeAppend("if (ivals.b < 0) { ivals.b += 256; }");
63 fb->codeAppend("if (ivals.a < 0) { ivals.a += 256; }");
64 fb->codeAppendf("%s = vec4(ivals)/255;", args.fOutputColor);
65 } else {
66 fb->codeAppendf("%s = %s;", args.fOutputColor, imageLoadStr.c_str());
67 }
68 }
69 };
70 return new GLSLProcessor;
71 }
72
73 ImageStorageAccess fImageStorageAccess;
Brian Salomon587e08f2017-01-27 10:59:27 -050074 typedef GrFragmentProcessor INHERITED;
Brian Salomonf9f45122016-11-29 11:59:17 -050075 };
76
77 static constexpr int kS = 256;
78 GrContext* context = ctxInfo.grContext();
Brian Salomon8b7a7dd2016-11-29 15:29:41 -050079 if (context->caps()->shaderCaps()->maxFragmentImageStorages() < 1) {
Brian Salomonf9f45122016-11-29 11:59:17 -050080 return;
81 }
82
83 std::unique_ptr<uint32_t[]> data(new uint32_t[kS * kS]);
84 for (int j = 0; j < kS; ++j) {
85 for (int i = 0; i < kS; ++i) {
86 data[i + kS * j] = GrColorPackRGBA(i, j, 0, 0);
87 }
88 }
89
90 std::unique_ptr<uint32_t[]> idata(new uint32_t[kS * kS]);
91 for (int j = 0; j < kS; ++j) {
92 for (int i = 0; i < kS; ++i) {
93 int8_t r = i - 128;
94 int8_t g = j - 128;
95 int8_t b = -128;
96 int8_t a = -128;
97 idata[i + kS * j] = ((uint8_t)a << 24) | ((uint8_t)b << 16) |
98 ((uint8_t)g << 8) | (uint8_t)r;
99 }
100 }
101
102 // Currently image accesses always have "top left" semantics.
103 GrSurfaceDesc desc;
104 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
105 desc.fWidth = kS;
106 desc.fHeight = kS;
107 struct {
108 GrPixelConfig fConfig;
109 std::unique_ptr<uint32_t[]> fData;
110 } tests[] = {
111 {
112 kRGBA_8888_GrPixelConfig,
113 std::move(data)
114 },
115 {
116 kRGBA_8888_sint_GrPixelConfig,
117 std::move(idata)
118 },
119 };
120 for (const auto& test : tests) {
121 // This test should work with any memory model and with or without restrict
122 for (auto mm : {GrSLMemoryModel::kNone,
123 GrSLMemoryModel::kCoherent,
124 GrSLMemoryModel::kVolatile}) {
125 for (auto restrict : {GrSLRestrict::kNo, GrSLRestrict::kYes}) {
126 if (!context->caps()->canConfigBeImageStorage(test.fConfig)) {
127 continue;
128 }
129 desc.fConfig = test.fConfig;
Robert Phillipse78b7252017-04-06 07:59:41 -0400130 sk_sp<GrTextureProxy> imageStorageTexture =
131 GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc,
132 SkBudgeted::kYes, test.fData.get(), 0);
Brian Salomonf9f45122016-11-29 11:59:17 -0500133
134 sk_sp<GrRenderTargetContext> rtContext =
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400135 context->makeDeferredRenderTargetContext(SkBackingFit::kExact, kS, kS,
136 kRGBA_8888_GrPixelConfig, nullptr);
Brian Salomonf9f45122016-11-29 11:59:17 -0500137 GrPaint paint;
138 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400139 paint.addColorFragmentProcessor(TestFP::Make(imageStorageTexture, mm, restrict));
Brian Salomon82f44312017-01-11 13:42:54 -0500140 rtContext->drawPaint(GrNoClip(), std::move(paint), SkMatrix::I());
Brian Salomonf9f45122016-11-29 11:59:17 -0500141 std::unique_ptr<uint32_t[]> readData(new uint32_t[kS * kS]);
142 SkImageInfo info = SkImageInfo::Make(kS, kS, kRGBA_8888_SkColorType,
143 kPremul_SkAlphaType);
144 rtContext->readPixels(info, readData.get(), 0, 0, 0);
145 int failed = false;
146 for (int j = 0; j < kS && !failed; ++j) {
147 for (int i = 0; i < kS && !failed; ++i) {
148 uint32_t d = test.fData[j * kS + i];
149 uint32_t rd = readData[j * kS + i];
150 if (d != rd) {
151 failed = true;
152 ERRORF(reporter, "Expected 0x%08x, got 0x%08x at %d, %d.", d, rd, i, j);
153 }
154 }
155 }
156 }
157 }
158 }
159}
160
161#endif