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