Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkTypes.h" |
| 9 | #include "tests/Test.h" |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 10 | |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 11 | #include "include/gpu/GrDirectContext.h" |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 12 | #include "include/gpu/GrRecordingContext.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 13 | #include "src/gpu/GrColor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/gpu/GrContextPriv.h" |
| 15 | #include "src/gpu/GrGeometryProcessor.h" |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 16 | #include "src/gpu/GrImageInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/gpu/GrMemoryPool.h" |
| 18 | #include "src/gpu/GrOpFlushState.h" |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 19 | #include "src/gpu/GrOpsRenderPass.h" |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 20 | #include "src/gpu/GrProgramInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "src/gpu/GrRecordingContextPriv.h" |
| 22 | #include "src/gpu/GrRenderTargetContext.h" |
| 23 | #include "src/gpu/GrRenderTargetContextPriv.h" |
| 24 | #include "src/gpu/GrResourceProvider.h" |
| 25 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 26 | #include "src/gpu/glsl/GrGLSLGeometryProcessor.h" |
| 27 | #include "src/gpu/glsl/GrGLSLVarying.h" |
| 28 | #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h" |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * This is a GPU-backend specific test for dynamic pipeline state. It draws boxes using dynamic |
| 32 | * scissor rectangles then reads back the result to verify a successful test. |
| 33 | */ |
| 34 | |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 35 | static constexpr int kScreenSize = 6; |
| 36 | static constexpr int kNumMeshes = 4; |
| 37 | static constexpr int kScreenSplitX = kScreenSize/2; |
| 38 | static constexpr int kScreenSplitY = kScreenSize/2; |
| 39 | |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 40 | static const SkIRect kDynamicScissors[kNumMeshes] = { |
| 41 | SkIRect::MakeLTRB(0, 0, kScreenSplitX, kScreenSplitY), |
| 42 | SkIRect::MakeLTRB(0, kScreenSplitY, kScreenSplitX, kScreenSize), |
| 43 | SkIRect::MakeLTRB(kScreenSplitX, 0, kScreenSize, kScreenSplitY), |
| 44 | SkIRect::MakeLTRB(kScreenSplitX, kScreenSplitY, kScreenSize, kScreenSize), |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | static const GrColor kMeshColors[kNumMeshes] { |
| 48 | GrColorPackRGBA(255, 0, 0, 255), |
| 49 | GrColorPackRGBA(0, 255, 0, 255), |
| 50 | GrColorPackRGBA(0, 0, 255, 255), |
| 51 | GrColorPackRGBA(0, 0, 0, 255) |
| 52 | }; |
| 53 | |
| 54 | struct Vertex { |
| 55 | float fX; |
| 56 | float fY; |
| 57 | GrColor fColor; |
| 58 | }; |
| 59 | |
| 60 | class GrPipelineDynamicStateTestProcessor : public GrGeometryProcessor { |
| 61 | public: |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 62 | static GrGeometryProcessor* Make(SkArenaAlloc* arena) { |
| 63 | return arena->make<GrPipelineDynamicStateTestProcessor>(); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 64 | } |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 65 | |
| 66 | const char* name() const override { return "GrPipelineDynamicStateTest Processor"; } |
| 67 | |
| 68 | void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const final {} |
| 69 | |
| 70 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final; |
| 71 | |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 72 | const Attribute& inVertex() const { return kAttributes[0]; } |
| 73 | const Attribute& inColor() const { return kAttributes[1]; } |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 74 | |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 75 | private: |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 76 | friend class ::SkArenaAlloc; // for access to ctor |
| 77 | |
| 78 | GrPipelineDynamicStateTestProcessor() |
| 79 | : INHERITED(kGrPipelineDynamicStateTestProcessor_ClassID) { |
| 80 | this->setVertexAttributes(kAttributes, SK_ARRAY_COUNT(kAttributes)); |
| 81 | } |
| 82 | |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 83 | static constexpr Attribute kAttributes[] = { |
| 84 | {"vertex", kFloat2_GrVertexAttribType, kHalf2_GrSLType}, |
| 85 | {"color", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType}, |
| 86 | }; |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 87 | |
| 88 | friend class GLSLPipelineDynamicStateTestProcessor; |
| 89 | typedef GrGeometryProcessor INHERITED; |
| 90 | }; |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 91 | constexpr GrPrimitiveProcessor::Attribute GrPipelineDynamicStateTestProcessor::kAttributes[]; |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 92 | |
| 93 | class GLSLPipelineDynamicStateTestProcessor : public GrGLSLGeometryProcessor { |
Brian Osman | 609f159 | 2020-07-01 15:14:39 -0400 | [diff] [blame] | 94 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor&) final {} |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 95 | |
| 96 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) final { |
| 97 | const GrPipelineDynamicStateTestProcessor& mp = |
| 98 | args.fGP.cast<GrPipelineDynamicStateTestProcessor>(); |
| 99 | |
| 100 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
| 101 | varyingHandler->emitAttributes(mp); |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 102 | varyingHandler->addPassThroughAttribute(mp.inColor(), args.fOutputColor); |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 103 | |
| 104 | GrGLSLVertexBuilder* v = args.fVertBuilder; |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 105 | v->codeAppendf("float2 vertex = %s;", mp.inVertex().name()); |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 106 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertex"); |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 107 | |
Chris Dalton | 6028361 | 2018-02-14 13:38:14 -0700 | [diff] [blame] | 108 | GrGLSLFPFragmentBuilder* f = args.fFragBuilder; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 109 | f->codeAppendf("%s = half4(1);", args.fOutputCoverage); |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 110 | } |
| 111 | }; |
| 112 | |
| 113 | GrGLSLPrimitiveProcessor* |
| 114 | GrPipelineDynamicStateTestProcessor::createGLSLInstance(const GrShaderCaps&) const { |
| 115 | return new GLSLPipelineDynamicStateTestProcessor; |
| 116 | } |
| 117 | |
| 118 | class GrPipelineDynamicStateTestOp : public GrDrawOp { |
| 119 | public: |
| 120 | DEFINE_OP_CLASS_ID |
| 121 | |
Robert Phillips | be9aff2 | 2019-02-15 11:33:22 -0500 | [diff] [blame] | 122 | static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context, |
Chris Dalton | 916c498 | 2018-08-15 00:53:25 -0600 | [diff] [blame] | 123 | GrScissorTest scissorTest, |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 124 | sk_sp<const GrBuffer> vbuff) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 125 | GrOpMemoryPool* pool = context->priv().opMemoryPool(); |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 126 | |
Chris Dalton | 916c498 | 2018-08-15 00:53:25 -0600 | [diff] [blame] | 127 | return pool->allocate<GrPipelineDynamicStateTestOp>(scissorTest, std::move(vbuff)); |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | private: |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 131 | friend class GrOpMemoryPool; |
| 132 | |
Chris Dalton | 916c498 | 2018-08-15 00:53:25 -0600 | [diff] [blame] | 133 | GrPipelineDynamicStateTestOp(GrScissorTest scissorTest, sk_sp<const GrBuffer> vbuff) |
Robert Phillips | 709e240 | 2020-03-23 18:29:16 +0000 | [diff] [blame] | 134 | : INHERITED(ClassID()) |
| 135 | , fScissorTest(scissorTest) |
| 136 | , fVertexBuffer(std::move(vbuff)) { |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 137 | this->setBounds(SkRect::MakeIWH(kScreenSize, kScreenSize), |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 138 | HasAABloat::kNo, IsHairline::kNo); |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 139 | } |
| 140 | |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 141 | const char* name() const override { return "GrPipelineDynamicStateTestOp"; } |
| 142 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
Robert Phillips | 709e240 | 2020-03-23 18:29:16 +0000 | [diff] [blame] | 143 | GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*, |
| 144 | bool hasMixedSampledCoverage, GrClampType) override { |
| 145 | return GrProcessorSet::EmptySetAnalysis(); |
Brian Salomon | f86d37b | 2017-06-16 10:04:34 -0400 | [diff] [blame] | 146 | } |
Robert Phillips | 709e240 | 2020-03-23 18:29:16 +0000 | [diff] [blame] | 147 | void onPrePrepare(GrRecordingContext*, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 148 | const GrSurfaceProxyView* writeView, |
Robert Phillips | 709e240 | 2020-03-23 18:29:16 +0000 | [diff] [blame] | 149 | GrAppliedClip*, |
| 150 | const GrXferProcessor::DstProxyView&) override {} |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 151 | void onPrepare(GrOpFlushState*) override {} |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 152 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
Robert Phillips | 709e240 | 2020-03-23 18:29:16 +0000 | [diff] [blame] | 153 | GrPipeline pipeline(fScissorTest, SkBlendMode::kSrc, |
Brian Salomon | 982f546 | 2020-03-30 12:52:33 -0400 | [diff] [blame] | 154 | flushState->drawOpArgs().writeSwizzle()); |
Chris Dalton | eb694b7 | 2020-03-16 09:25:50 -0600 | [diff] [blame] | 155 | SkSTArray<kNumMeshes, GrSimpleMesh> meshes; |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 156 | for (int i = 0; i < kNumMeshes; ++i) { |
Chris Dalton | eb694b7 | 2020-03-16 09:25:50 -0600 | [diff] [blame] | 157 | GrSimpleMesh& mesh = meshes.push_back(); |
Chris Dalton | 37c7bdd | 2020-03-13 09:21:12 -0600 | [diff] [blame] | 158 | mesh.set(fVertexBuffer, 4, 4 * i); |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 159 | } |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 160 | |
Robert Phillips | 709e240 | 2020-03-23 18:29:16 +0000 | [diff] [blame] | 161 | auto geomProc = GrPipelineDynamicStateTestProcessor::Make(flushState->allocator()); |
| 162 | |
| 163 | GrProgramInfo programInfo(flushState->proxy()->numSamples(), |
| 164 | flushState->proxy()->numStencilSamples(), |
| 165 | flushState->proxy()->backendFormat(), |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 166 | flushState->writeView()->origin(), |
Robert Phillips | 709e240 | 2020-03-23 18:29:16 +0000 | [diff] [blame] | 167 | &pipeline, |
| 168 | geomProc, |
| 169 | GrPrimitiveType::kTriangleStrip); |
| 170 | |
| 171 | flushState->bindPipeline(programInfo, SkRect::MakeIWH(kScreenSize, kScreenSize)); |
| 172 | for (int i = 0; i < 4; ++i) { |
Chris Dalton | 765ed36 | 2020-03-16 17:34:44 -0600 | [diff] [blame] | 173 | if (fScissorTest == GrScissorTest::kEnabled) { |
| 174 | flushState->setScissorRect(kDynamicScissors[i]); |
| 175 | } |
| 176 | flushState->drawMesh(meshes[i]); |
| 177 | } |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 178 | } |
| 179 | |
Chris Dalton | 916c498 | 2018-08-15 00:53:25 -0600 | [diff] [blame] | 180 | GrScissorTest fScissorTest; |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 181 | const sk_sp<const GrBuffer> fVertexBuffer; |
| 182 | |
| 183 | typedef GrDrawOp INHERITED; |
| 184 | }; |
| 185 | |
| 186 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrPipelineDynamicStateTest, reporter, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 187 | auto context = ctxInfo.directContext(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 188 | GrResourceProvider* rp = context->priv().resourceProvider(); |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 189 | |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 190 | auto rtc = GrRenderTargetContext::Make( |
| 191 | context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact, |
| 192 | {kScreenSize, kScreenSize}); |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 193 | if (!rtc) { |
| 194 | ERRORF(reporter, "could not create render target context."); |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | constexpr float d = (float) kScreenSize; |
| 199 | Vertex vdata[kNumMeshes * 4] = { |
| 200 | {0, 0, kMeshColors[0]}, |
| 201 | {0, d, kMeshColors[0]}, |
| 202 | {d, 0, kMeshColors[0]}, |
| 203 | {d, d, kMeshColors[0]}, |
| 204 | |
| 205 | {0, 0, kMeshColors[1]}, |
| 206 | {0, d, kMeshColors[1]}, |
| 207 | {d, 0, kMeshColors[1]}, |
| 208 | {d, d, kMeshColors[1]}, |
| 209 | |
| 210 | {0, 0, kMeshColors[2]}, |
| 211 | {0, d, kMeshColors[2]}, |
| 212 | {d, 0, kMeshColors[2]}, |
| 213 | {d, d, kMeshColors[2]}, |
| 214 | |
| 215 | {0, 0, kMeshColors[3]}, |
| 216 | {0, d, kMeshColors[3]}, |
| 217 | {d, 0, kMeshColors[3]}, |
| 218 | {d, d, kMeshColors[3]} |
| 219 | }; |
| 220 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 221 | sk_sp<const GrBuffer> vbuff(rp->createBuffer(sizeof(vdata), GrGpuBufferType::kVertex, |
| 222 | kDynamic_GrAccessPattern, vdata)); |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 223 | if (!vbuff) { |
| 224 | ERRORF(reporter, "vbuff is null."); |
| 225 | return; |
| 226 | } |
| 227 | |
| 228 | uint32_t resultPx[kScreenSize * kScreenSize]; |
| 229 | |
Chris Dalton | 916c498 | 2018-08-15 00:53:25 -0600 | [diff] [blame] | 230 | for (GrScissorTest scissorTest : {GrScissorTest::kEnabled, GrScissorTest::kDisabled}) { |
Michael Ludwig | 81d4172 | 2020-05-26 16:57:38 -0400 | [diff] [blame] | 231 | rtc->clear(SkPMColor4f::FromBytes_RGBA(0xbaaaaaad)); |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 232 | rtc->priv().testingOnly_addDrawOp( |
Chris Dalton | 916c498 | 2018-08-15 00:53:25 -0600 | [diff] [blame] | 233 | GrPipelineDynamicStateTestOp::Make(context, scissorTest, vbuff)); |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 234 | rtc->readPixels(SkImageInfo::Make(kScreenSize, kScreenSize, |
| 235 | kRGBA_8888_SkColorType, kPremul_SkAlphaType), |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 236 | resultPx, 4 * kScreenSize, {0, 0}); |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 237 | for (int y = 0; y < kScreenSize; ++y) { |
| 238 | for (int x = 0; x < kScreenSize; ++x) { |
| 239 | int expectedColorIdx; |
Chris Dalton | 916c498 | 2018-08-15 00:53:25 -0600 | [diff] [blame] | 240 | if (GrScissorTest::kEnabled == scissorTest) { |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 241 | expectedColorIdx = (x < kScreenSplitX ? 0 : 2) + (y < kScreenSplitY ? 0 : 1); |
| 242 | } else { |
| 243 | expectedColorIdx = kNumMeshes - 1; |
| 244 | } |
| 245 | uint32_t expected = kMeshColors[expectedColorIdx]; |
| 246 | uint32_t actual = resultPx[y * kScreenSize + x]; |
| 247 | if (expected != actual) { |
| 248 | ERRORF(reporter, "[scissor=%s] pixel (%i,%i): got 0x%x expected 0x%x", |
Chris Dalton | 916c498 | 2018-08-15 00:53:25 -0600 | [diff] [blame] | 249 | GrScissorTest::kEnabled == scissorTest ? "enabled" : "disabled", x, y, |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 250 | actual, expected); |
| 251 | return; |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | } |