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: |
| 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 Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 32 | : INHERITED(kNone_OptimizationFlags) |
| 33 | , fImageStorageAccess(std::move(texture), kRead_GrIOType, mm, restrict) { |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 34 | this->initClassID<TestFP>(); |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 35 | this->addImageStorageAccess(&fImageStorageAccess); |
| 36 | } |
| 37 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 38 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {} |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 39 | |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 40 | 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 Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 50 | fb->codeAppend("highp vec2 coord = sk_FragCoord.xy;"); |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 51 | 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 Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 74 | typedef GrFragmentProcessor INHERITED; |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | static constexpr int kS = 256; |
| 78 | GrContext* context = ctxInfo.grContext(); |
Brian Salomon | 8b7a7dd | 2016-11-29 15:29:41 -0500 | [diff] [blame] | 79 | if (context->caps()->shaderCaps()->maxFragmentImageStorages() < 1) { |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 80 | 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 Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 130 | sk_sp<GrTexture> imageStorageTexture(context->resourceProvider()->createTexture( |
| 131 | desc, SkBudgeted::kYes, test.fData.get(), 0)); |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 132 | |
| 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 Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 139 | rtContext->drawPaint(GrNoClip(), std::move(paint), SkMatrix::I()); |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 140 | 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 |