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