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