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