Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 1 | /* |
| 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 Salomon | c65aec9 | 2017-03-09 09:03:58 -0500 | [diff] [blame] | 12 | #include "GrClip.h" |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 13 | #include "GrFragmentProcessor.h" |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 14 | #include "GrRenderTargetContext.h" |
| 15 | #include "GrTexture.h" |
| 16 | #include "glsl/GrGLSLFragmentProcessor.h" |
| 17 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
| 18 | |
| 19 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageStorageLoad, reporter, ctxInfo) { |
| 20 | class TestFP : public GrFragmentProcessor { |
| 21 | public: |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 22 | static sk_sp<GrFragmentProcessor> Make(sk_sp<GrTextureProxy> proxy, |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 23 | GrSLMemoryModel mm, |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 24 | GrSLRestrict restrict) { |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 25 | return sk_sp<GrFragmentProcessor>(new TestFP(std::move(proxy), mm, restrict)); |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | const char* name() const override { return "Image Load Test FP"; } |
| 29 | |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame^] | 30 | sk_sp<GrFragmentProcessor> clone() const override { |
| 31 | return sk_sp<GrFragmentProcessor>(new TestFP(*this)); |
| 32 | } |
| 33 | |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 34 | private: |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 35 | TestFP(sk_sp<GrTextureProxy> proxy, GrSLMemoryModel mm, GrSLRestrict restrict) |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 36 | : INHERITED(kNone_OptimizationFlags) |
Robert Phillips | 8a02f65 | 2017-05-12 14:49:16 -0400 | [diff] [blame] | 37 | , fImageStorageAccess(std::move(proxy), kRead_GrIOType, mm, restrict) { |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 38 | this->initClassID<TestFP>(); |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 39 | this->addImageStorageAccess(&fImageStorageAccess); |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 40 | } |
| 41 | |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame^] | 42 | explicit TestFP(const TestFP& that) |
| 43 | : INHERITED(that.optimizationFlags()) |
| 44 | , fImageStorageAccess(that.fImageStorageAccess) { |
| 45 | this->initClassID<TestFP>(); |
| 46 | this->addImageStorageAccess(&fImageStorageAccess); |
| 47 | } |
| 48 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 49 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {} |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 50 | |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 51 | bool onIsEqual(const GrFragmentProcessor& that) const override { return true; } |
| 52 | |
| 53 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
| 54 | class GLSLProcessor : public GrGLSLFragmentProcessor { |
| 55 | public: |
| 56 | GLSLProcessor() = default; |
| 57 | void emitCode(EmitArgs& args) override { |
| 58 | const TestFP& tfp = args.fFp.cast<TestFP>(); |
| 59 | GrGLSLFPFragmentBuilder* fb = args.fFragBuilder; |
| 60 | SkString imageLoadStr; |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 61 | fb->codeAppend("highp vec2 coord = sk_FragCoord.xy;"); |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 62 | fb->appendImageStorageLoad(&imageLoadStr, args.fImageStorages[0], |
| 63 | "ivec2(coord)"); |
Robert Phillips | 9bee2e5 | 2017-05-29 12:37:20 -0400 | [diff] [blame] | 64 | if (GrPixelConfigIsSint(tfp.fImageStorageAccess.peekTexture()->config())) { |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 65 | // Map the signed bytes so that when then get read back as unorm values they |
| 66 | // will have their original bit pattern. |
| 67 | fb->codeAppendf("highp ivec4 ivals = %s;", imageLoadStr.c_str()); |
| 68 | // NV gives a linker error for this: |
| 69 | // fb->codeAppend("ivals += |
| 70 | // "mix(ivec4(0), ivec4(256), lessThan(ivals, ivec4(0)));"); |
| 71 | fb->codeAppend("if (ivals.r < 0) { ivals.r += 256; }"); |
| 72 | fb->codeAppend("if (ivals.g < 0) { ivals.g += 256; }"); |
| 73 | fb->codeAppend("if (ivals.b < 0) { ivals.b += 256; }"); |
| 74 | fb->codeAppend("if (ivals.a < 0) { ivals.a += 256; }"); |
| 75 | fb->codeAppendf("%s = vec4(ivals)/255;", args.fOutputColor); |
| 76 | } else { |
| 77 | fb->codeAppendf("%s = %s;", args.fOutputColor, imageLoadStr.c_str()); |
| 78 | } |
| 79 | } |
| 80 | }; |
| 81 | return new GLSLProcessor; |
| 82 | } |
| 83 | |
| 84 | ImageStorageAccess fImageStorageAccess; |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 85 | typedef GrFragmentProcessor INHERITED; |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | static constexpr int kS = 256; |
| 89 | GrContext* context = ctxInfo.grContext(); |
Brian Salomon | 8b7a7dd | 2016-11-29 15:29:41 -0500 | [diff] [blame] | 90 | if (context->caps()->shaderCaps()->maxFragmentImageStorages() < 1) { |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 91 | return; |
| 92 | } |
| 93 | |
| 94 | std::unique_ptr<uint32_t[]> data(new uint32_t[kS * kS]); |
| 95 | for (int j = 0; j < kS; ++j) { |
| 96 | for (int i = 0; i < kS; ++i) { |
| 97 | data[i + kS * j] = GrColorPackRGBA(i, j, 0, 0); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | std::unique_ptr<uint32_t[]> idata(new uint32_t[kS * kS]); |
| 102 | for (int j = 0; j < kS; ++j) { |
| 103 | for (int i = 0; i < kS; ++i) { |
| 104 | int8_t r = i - 128; |
| 105 | int8_t g = j - 128; |
| 106 | int8_t b = -128; |
| 107 | int8_t a = -128; |
| 108 | idata[i + kS * j] = ((uint8_t)a << 24) | ((uint8_t)b << 16) | |
| 109 | ((uint8_t)g << 8) | (uint8_t)r; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // Currently image accesses always have "top left" semantics. |
| 114 | GrSurfaceDesc desc; |
| 115 | desc.fOrigin = kTopLeft_GrSurfaceOrigin; |
| 116 | desc.fWidth = kS; |
| 117 | desc.fHeight = kS; |
| 118 | struct { |
| 119 | GrPixelConfig fConfig; |
| 120 | std::unique_ptr<uint32_t[]> fData; |
| 121 | } tests[] = { |
| 122 | { |
| 123 | kRGBA_8888_GrPixelConfig, |
| 124 | std::move(data) |
| 125 | }, |
| 126 | { |
| 127 | kRGBA_8888_sint_GrPixelConfig, |
| 128 | std::move(idata) |
| 129 | }, |
| 130 | }; |
| 131 | for (const auto& test : tests) { |
| 132 | // This test should work with any memory model and with or without restrict |
| 133 | for (auto mm : {GrSLMemoryModel::kNone, |
| 134 | GrSLMemoryModel::kCoherent, |
| 135 | GrSLMemoryModel::kVolatile}) { |
| 136 | for (auto restrict : {GrSLRestrict::kNo, GrSLRestrict::kYes}) { |
| 137 | if (!context->caps()->canConfigBeImageStorage(test.fConfig)) { |
| 138 | continue; |
| 139 | } |
| 140 | desc.fConfig = test.fConfig; |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 141 | sk_sp<GrTextureProxy> imageStorageTexture = |
| 142 | GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, |
| 143 | SkBudgeted::kYes, test.fData.get(), 0); |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 144 | |
| 145 | sk_sp<GrRenderTargetContext> rtContext = |
Robert Phillips | dd3b3f4 | 2017-04-24 10:57:28 -0400 | [diff] [blame] | 146 | context->makeDeferredRenderTargetContext(SkBackingFit::kExact, kS, kS, |
| 147 | kRGBA_8888_GrPixelConfig, nullptr); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame^] | 148 | // We make a clone to test that copying GrFragmentProcessor::ImageStorageAccess |
| 149 | // copying works. |
| 150 | auto testFP = TestFP::Make(imageStorageTexture, mm, restrict); |
| 151 | for (auto fp : {testFP, testFP->clone()}) { |
| 152 | GrPaint paint; |
| 153 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 154 | paint.addColorFragmentProcessor(fp); |
| 155 | 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 Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | #endif |