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: |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame^] | 22 | static std::unique_ptr<GrFragmentProcessor> Make(sk_sp<GrTextureProxy> proxy, |
| 23 | GrSLMemoryModel mm, |
| 24 | GrSLRestrict restrict) { |
| 25 | return std::unique_ptr<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 | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame^] | 30 | std::unique_ptr<GrFragmentProcessor> clone() const override { |
| 31 | return std::unique_ptr<GrFragmentProcessor>(new TestFP(*this)); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 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 | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 61 | fb->codeAppend("highp float2 coord = sk_FragCoord.xy;"); |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 62 | fb->appendImageStorageLoad(&imageLoadStr, args.fImageStorages[0], |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 63 | "int2(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. |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 67 | fb->codeAppendf("highp int4 ivals = %s;", imageLoadStr.c_str()); |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 68 | // NV gives a linker error for this: |
| 69 | // fb->codeAppend("ivals += |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 70 | // "mix(int4(0), int4(256), lessThan(ivals, int4(0)));"); |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 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; }"); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 75 | fb->codeAppendf("%s = float4(ivals)/255;", args.fOutputColor); |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 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 |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame^] | 149 | // works. |
| 150 | std::unique_ptr<GrFragmentProcessor> fps[2]; |
| 151 | fps[0] = TestFP::Make(imageStorageTexture, mm, restrict); |
| 152 | fps[1] = fps[0]->clone(); |
| 153 | for (auto& fp : fps) { |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 154 | GrPaint paint; |
| 155 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame^] | 156 | paint.addColorFragmentProcessor(std::move(fp)); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 157 | rtContext->drawPaint(GrNoClip(), std::move(paint), SkMatrix::I()); |
| 158 | std::unique_ptr<uint32_t[]> readData(new uint32_t[kS * kS]); |
| 159 | SkImageInfo info = SkImageInfo::Make(kS, kS, kRGBA_8888_SkColorType, |
| 160 | kPremul_SkAlphaType); |
| 161 | rtContext->readPixels(info, readData.get(), 0, 0, 0); |
| 162 | int failed = false; |
| 163 | for (int j = 0; j < kS && !failed; ++j) { |
| 164 | for (int i = 0; i < kS && !failed; ++i) { |
| 165 | uint32_t d = test.fData[j * kS + i]; |
| 166 | uint32_t rd = readData[j * kS + i]; |
| 167 | if (d != rd) { |
| 168 | failed = true; |
| 169 | ERRORF(reporter, "Expected 0x%08x, got 0x%08x at %d, %d.", |
| 170 | d, rd, i, j); |
| 171 | } |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | #endif |