blob: 835aec5468916b406bd4e214d284a22d05a1924f [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
12#include "GrFragmentProcessor.h"
13#include "GrInvariantOutput.h"
14#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:
22 static sk_sp<GrFragmentProcessor> Make(sk_sp<GrTexture> texture, GrSLMemoryModel mm,
23 GrSLRestrict restrict) {
24 return sk_sp<GrFragmentProcessor>(new TestFP(std::move(texture), mm, restrict));
25 }
26
27 const char* name() const override { return "Image Load Test FP"; }
28
29 private:
30 TestFP(sk_sp<GrTexture> texture, GrSLMemoryModel mm, GrSLRestrict restrict)
31 : fImageStorageAccess(std::move(texture), kRead_GrIOType, mm, restrict) {
32 this->initClassID<TestFP>();
33 this->setWillReadFragmentPosition();
34 this->addImageStorageAccess(&fImageStorageAccess);
35 }
36
Brian Salomon94efbf52016-11-29 13:43:05 -050037 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
Brian Salomonf9f45122016-11-29 11:59:17 -050038
39 void onComputeInvariantOutput(GrInvariantOutput* inout) const override {
40 inout->setToUnknown(GrInvariantOutput::kWillNot_ReadInput);
41 }
42
43 bool onIsEqual(const GrFragmentProcessor& that) const override { return true; }
44
45 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
46 class GLSLProcessor : public GrGLSLFragmentProcessor {
47 public:
48 GLSLProcessor() = default;
49 void emitCode(EmitArgs& args) override {
50 const TestFP& tfp = args.fFp.cast<TestFP>();
51 GrGLSLFPFragmentBuilder* fb = args.fFragBuilder;
52 SkString imageLoadStr;
53 fb->codeAppendf("highp vec2 coord = %s.xy;",
54 args.fFragBuilder->fragmentPosition());
55 fb->appendImageStorageLoad(&imageLoadStr, args.fImageStorages[0],
56 "ivec2(coord)");
57 if (GrPixelConfigIsSint(tfp.fImageStorageAccess.texture()->config())) {
58 // 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;
78 };
79
80 static constexpr int kS = 256;
81 GrContext* context = ctxInfo.grContext();
Brian Salomon8b7a7dd2016-11-29 15:29:41 -050082 if (context->caps()->shaderCaps()->maxFragmentImageStorages() < 1) {
Brian Salomonf9f45122016-11-29 11:59:17 -050083 return;
84 }
85
86 std::unique_ptr<uint32_t[]> data(new uint32_t[kS * kS]);
87 for (int j = 0; j < kS; ++j) {
88 for (int i = 0; i < kS; ++i) {
89 data[i + kS * j] = GrColorPackRGBA(i, j, 0, 0);
90 }
91 }
92
93 std::unique_ptr<uint32_t[]> idata(new uint32_t[kS * kS]);
94 for (int j = 0; j < kS; ++j) {
95 for (int i = 0; i < kS; ++i) {
96 int8_t r = i - 128;
97 int8_t g = j - 128;
98 int8_t b = -128;
99 int8_t a = -128;
100 idata[i + kS * j] = ((uint8_t)a << 24) | ((uint8_t)b << 16) |
101 ((uint8_t)g << 8) | (uint8_t)r;
102 }
103 }
104
105 // Currently image accesses always have "top left" semantics.
106 GrSurfaceDesc desc;
107 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
108 desc.fWidth = kS;
109 desc.fHeight = kS;
110 struct {
111 GrPixelConfig fConfig;
112 std::unique_ptr<uint32_t[]> fData;
113 } tests[] = {
114 {
115 kRGBA_8888_GrPixelConfig,
116 std::move(data)
117 },
118 {
119 kRGBA_8888_sint_GrPixelConfig,
120 std::move(idata)
121 },
122 };
123 for (const auto& test : tests) {
124 // This test should work with any memory model and with or without restrict
125 for (auto mm : {GrSLMemoryModel::kNone,
126 GrSLMemoryModel::kCoherent,
127 GrSLMemoryModel::kVolatile}) {
128 for (auto restrict : {GrSLRestrict::kNo, GrSLRestrict::kYes}) {
129 if (!context->caps()->canConfigBeImageStorage(test.fConfig)) {
130 continue;
131 }
132 desc.fConfig = test.fConfig;
133 sk_sp<GrTexture> imageStorageTexture(context->textureProvider()->createTexture(desc,
134 SkBudgeted::kYes, test.fData.get(), 0));
135
136 sk_sp<GrRenderTargetContext> rtContext =
137 context->makeRenderTargetContext(SkBackingFit::kExact, kS, kS,
138 kRGBA_8888_GrPixelConfig, nullptr);
139 GrPaint paint;
140 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
141 paint.addColorFragmentProcessor(TestFP::Make(imageStorageTexture, mm, restrict));
142 rtContext->drawPaint(GrNoClip(), paint, SkMatrix::I());
143 std::unique_ptr<uint32_t[]> readData(new uint32_t[kS * kS]);
144 SkImageInfo info = SkImageInfo::Make(kS, kS, kRGBA_8888_SkColorType,
145 kPremul_SkAlphaType);
146 rtContext->readPixels(info, readData.get(), 0, 0, 0);
147 int failed = false;
148 for (int j = 0; j < kS && !failed; ++j) {
149 for (int i = 0; i < kS && !failed; ++i) {
150 uint32_t d = test.fData[j * kS + i];
151 uint32_t rd = readData[j * kS + i];
152 if (d != rd) {
153 failed = true;
154 ERRORF(reporter, "Expected 0x%08x, got 0x%08x at %d, %d.", d, rd, i, j);
155 }
156 }
157 }
158 }
159 }
160 }
161}
162
163#endif