blob: 59bb38b02ac7549ba9105f89648164a4c36cdb61 [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:
23 static sk_sp<GrFragmentProcessor> Make(sk_sp<GrTexture> texture, GrSLMemoryModel mm,
24 GrSLRestrict restrict) {
25 return sk_sp<GrFragmentProcessor>(new TestFP(std::move(texture), mm, restrict));
26 }
27
28 const char* name() const override { return "Image Load Test FP"; }
29
30 private:
31 TestFP(sk_sp<GrTexture> texture, GrSLMemoryModel mm, GrSLRestrict restrict)
Brian Salomon587e08f2017-01-27 10:59:27 -050032 : INHERITED(kNone_OptimizationFlags)
33 , fImageStorageAccess(std::move(texture), kRead_GrIOType, mm, restrict) {
Brian Salomonf9f45122016-11-29 11:59:17 -050034 this->initClassID<TestFP>();
Brian Salomonf9f45122016-11-29 11:59:17 -050035 this->addImageStorageAccess(&fImageStorageAccess);
36 }
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)");
53 if (GrPixelConfigIsSint(tfp.fImageStorageAccess.texture()->config())) {
54 // 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;
Brian Osman32342f02017-03-04 08:12:46 -0500130 sk_sp<GrTexture> imageStorageTexture(context->resourceProvider()->createTexture(
131 desc, SkBudgeted::kYes, test.fData.get(), 0));
Brian Salomonf9f45122016-11-29 11:59:17 -0500132
133 sk_sp<GrRenderTargetContext> rtContext =
134 context->makeRenderTargetContext(SkBackingFit::kExact, kS, kS,
135 kRGBA_8888_GrPixelConfig, nullptr);
136 GrPaint paint;
137 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
138 paint.addColorFragmentProcessor(TestFP::Make(imageStorageTexture, mm, restrict));
Brian Salomon82f44312017-01-11 13:42:54 -0500139 rtContext->drawPaint(GrNoClip(), std::move(paint), SkMatrix::I());
Brian Salomonf9f45122016-11-29 11:59:17 -0500140 std::unique_ptr<uint32_t[]> readData(new uint32_t[kS * kS]);
141 SkImageInfo info = SkImageInfo::Make(kS, kS, kRGBA_8888_SkColorType,
142 kPremul_SkAlphaType);
143 rtContext->readPixels(info, readData.get(), 0, 0, 0);
144 int failed = false;
145 for (int j = 0; j < kS && !failed; ++j) {
146 for (int i = 0; i < kS && !failed; ++i) {
147 uint32_t d = test.fData[j * kS + i];
148 uint32_t rd = readData[j * kS + i];
149 if (d != rd) {
150 failed = true;
151 ERRORF(reporter, "Expected 0x%08x, got 0x%08x at %d, %d.", d, rd, i, j);
152 }
153 }
154 }
155 }
156 }
157 }
158}
159
160#endif