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