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