blob: 42305066f6fe0fcc8ae0df7c7f8e3119a80d2ee3 [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"
Brian Salomonf9f45122016-11-29 11:59:17 -050013#include "GrRenderTargetContext.h"
Brian Osman32342f02017-03-04 08:12:46 -050014#include "GrResourceProvider.h"
Brian Salomonf9f45122016-11-29 11:59:17 -050015#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)
Brian Salomon587e08f2017-01-27 10:59:27 -050031 : INHERITED(kNone_OptimizationFlags)
32 , fImageStorageAccess(std::move(texture), kRead_GrIOType, mm, restrict) {
Brian Salomonf9f45122016-11-29 11:59:17 -050033 this->initClassID<TestFP>();
Brian Salomonf9f45122016-11-29 11:59:17 -050034 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
Brian Salomonf9f45122016-11-29 11:59:17 -050039 bool onIsEqual(const GrFragmentProcessor& that) const override { return true; }
40
41 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
42 class GLSLProcessor : public GrGLSLFragmentProcessor {
43 public:
44 GLSLProcessor() = default;
45 void emitCode(EmitArgs& args) override {
46 const TestFP& tfp = args.fFp.cast<TestFP>();
47 GrGLSLFPFragmentBuilder* fb = args.fFragBuilder;
48 SkString imageLoadStr;
Ethan Nicholas38657112017-02-09 17:01:22 -050049 fb->codeAppend("highp vec2 coord = sk_FragCoord.xy;");
Brian Salomonf9f45122016-11-29 11:59:17 -050050 fb->appendImageStorageLoad(&imageLoadStr, args.fImageStorages[0],
51 "ivec2(coord)");
52 if (GrPixelConfigIsSint(tfp.fImageStorageAccess.texture()->config())) {
53 // Map the signed bytes so that when then get read back as unorm values they
54 // will have their original bit pattern.
55 fb->codeAppendf("highp ivec4 ivals = %s;", imageLoadStr.c_str());
56 // NV gives a linker error for this:
57 // fb->codeAppend("ivals +=
58 // "mix(ivec4(0), ivec4(256), lessThan(ivals, ivec4(0)));");
59 fb->codeAppend("if (ivals.r < 0) { ivals.r += 256; }");
60 fb->codeAppend("if (ivals.g < 0) { ivals.g += 256; }");
61 fb->codeAppend("if (ivals.b < 0) { ivals.b += 256; }");
62 fb->codeAppend("if (ivals.a < 0) { ivals.a += 256; }");
63 fb->codeAppendf("%s = vec4(ivals)/255;", args.fOutputColor);
64 } else {
65 fb->codeAppendf("%s = %s;", args.fOutputColor, imageLoadStr.c_str());
66 }
67 }
68 };
69 return new GLSLProcessor;
70 }
71
72 ImageStorageAccess fImageStorageAccess;
Brian Salomon587e08f2017-01-27 10:59:27 -050073 typedef GrFragmentProcessor INHERITED;
Brian Salomonf9f45122016-11-29 11:59:17 -050074 };
75
76 static constexpr int kS = 256;
77 GrContext* context = ctxInfo.grContext();
Brian Salomon8b7a7dd2016-11-29 15:29:41 -050078 if (context->caps()->shaderCaps()->maxFragmentImageStorages() < 1) {
Brian Salomonf9f45122016-11-29 11:59:17 -050079 return;
80 }
81
82 std::unique_ptr<uint32_t[]> data(new uint32_t[kS * kS]);
83 for (int j = 0; j < kS; ++j) {
84 for (int i = 0; i < kS; ++i) {
85 data[i + kS * j] = GrColorPackRGBA(i, j, 0, 0);
86 }
87 }
88
89 std::unique_ptr<uint32_t[]> idata(new uint32_t[kS * kS]);
90 for (int j = 0; j < kS; ++j) {
91 for (int i = 0; i < kS; ++i) {
92 int8_t r = i - 128;
93 int8_t g = j - 128;
94 int8_t b = -128;
95 int8_t a = -128;
96 idata[i + kS * j] = ((uint8_t)a << 24) | ((uint8_t)b << 16) |
97 ((uint8_t)g << 8) | (uint8_t)r;
98 }
99 }
100
101 // Currently image accesses always have "top left" semantics.
102 GrSurfaceDesc desc;
103 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
104 desc.fWidth = kS;
105 desc.fHeight = kS;
106 struct {
107 GrPixelConfig fConfig;
108 std::unique_ptr<uint32_t[]> fData;
109 } tests[] = {
110 {
111 kRGBA_8888_GrPixelConfig,
112 std::move(data)
113 },
114 {
115 kRGBA_8888_sint_GrPixelConfig,
116 std::move(idata)
117 },
118 };
119 for (const auto& test : tests) {
120 // This test should work with any memory model and with or without restrict
121 for (auto mm : {GrSLMemoryModel::kNone,
122 GrSLMemoryModel::kCoherent,
123 GrSLMemoryModel::kVolatile}) {
124 for (auto restrict : {GrSLRestrict::kNo, GrSLRestrict::kYes}) {
125 if (!context->caps()->canConfigBeImageStorage(test.fConfig)) {
126 continue;
127 }
128 desc.fConfig = test.fConfig;
Brian Osman32342f02017-03-04 08:12:46 -0500129 sk_sp<GrTexture> imageStorageTexture(context->resourceProvider()->createTexture(
130 desc, SkBudgeted::kYes, test.fData.get(), 0));
Brian Salomonf9f45122016-11-29 11:59:17 -0500131
132 sk_sp<GrRenderTargetContext> rtContext =
133 context->makeRenderTargetContext(SkBackingFit::kExact, kS, kS,
134 kRGBA_8888_GrPixelConfig, nullptr);
135 GrPaint paint;
136 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
137 paint.addColorFragmentProcessor(TestFP::Make(imageStorageTexture, mm, restrict));
Brian Salomon82f44312017-01-11 13:42:54 -0500138 rtContext->drawPaint(GrNoClip(), std::move(paint), SkMatrix::I());
Brian Salomonf9f45122016-11-29 11:59:17 -0500139 std::unique_ptr<uint32_t[]> readData(new uint32_t[kS * kS]);
140 SkImageInfo info = SkImageInfo::Make(kS, kS, kRGBA_8888_SkColorType,
141 kPremul_SkAlphaType);
142 rtContext->readPixels(info, readData.get(), 0, 0, 0);
143 int failed = false;
144 for (int j = 0; j < kS && !failed; ++j) {
145 for (int i = 0; i < kS && !failed; ++i) {
146 uint32_t d = test.fData[j * kS + i];
147 uint32_t rd = readData[j * kS + i];
148 if (d != rd) {
149 failed = true;
150 ERRORF(reporter, "Expected 0x%08x, got 0x%08x at %d, %d.", d, rd, i, j);
151 }
152 }
153 }
154 }
155 }
156 }
157}
158
159#endif