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" |
| 22 | #include "glsl/GrGLSLVertexShaderBuilder.h" |
| 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() |
| 62 | : fVertex(this->addVertexAttrib("vertex", kVec2f_GrVertexAttribType)) |
| 63 | , fColor(this->addVertexAttrib("color", kVec4ub_GrVertexAttribType)) { |
| 64 | this->initClassID<GrPipelineDynamicStateTestProcessor>(); |
| 65 | } |
| 66 | |
| 67 | const char* name() const override { return "GrPipelineDynamicStateTest Processor"; } |
| 68 | |
| 69 | void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const final {} |
| 70 | |
| 71 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final; |
| 72 | |
| 73 | protected: |
| 74 | const Attribute& fVertex; |
| 75 | const Attribute& fColor; |
| 76 | |
| 77 | friend class GLSLPipelineDynamicStateTestProcessor; |
| 78 | typedef GrGeometryProcessor INHERITED; |
| 79 | }; |
| 80 | |
| 81 | class GLSLPipelineDynamicStateTestProcessor : public GrGLSLGeometryProcessor { |
| 82 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor&, |
| 83 | FPCoordTransformIter&& transformIter) final {} |
| 84 | |
| 85 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) final { |
| 86 | const GrPipelineDynamicStateTestProcessor& mp = |
| 87 | args.fGP.cast<GrPipelineDynamicStateTestProcessor>(); |
| 88 | |
| 89 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
| 90 | varyingHandler->emitAttributes(mp); |
| 91 | varyingHandler->addPassThroughAttribute(&mp.fColor, args.fOutputColor); |
| 92 | |
| 93 | GrGLSLVertexBuilder* v = args.fVertBuilder; |
| 94 | v->codeAppendf("vec2 vertex = %s;", mp.fVertex.fName); |
| 95 | gpArgs->fPositionVar.set(kVec2f_GrSLType, "vertex"); |
| 96 | |
| 97 | GrGLSLPPFragmentBuilder* f = args.fFragBuilder; |
| 98 | f->codeAppendf("%s = vec4(1);", args.fOutputCoverage); |
| 99 | } |
| 100 | }; |
| 101 | |
| 102 | GrGLSLPrimitiveProcessor* |
| 103 | GrPipelineDynamicStateTestProcessor::createGLSLInstance(const GrShaderCaps&) const { |
| 104 | return new GLSLPipelineDynamicStateTestProcessor; |
| 105 | } |
| 106 | |
| 107 | class GrPipelineDynamicStateTestOp : public GrDrawOp { |
| 108 | public: |
| 109 | DEFINE_OP_CLASS_ID |
| 110 | |
| 111 | GrPipelineDynamicStateTestOp(ScissorState scissorState, sk_sp<const GrBuffer> vbuff) |
| 112 | : INHERITED(ClassID()) |
| 113 | , fScissorState(scissorState) |
| 114 | , fVertexBuffer(std::move(vbuff)) { |
| 115 | this->setBounds(SkRect::MakeIWH(kScreenSize, kScreenSize), |
| 116 | HasAABloat::kNo, IsZeroArea::kNo); |
| 117 | } |
| 118 | |
| 119 | private: |
| 120 | const char* name() const override { return "GrPipelineDynamicStateTestOp"; } |
| 121 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
Brian Salomon | f86d37b | 2017-06-16 10:04:34 -0400 | [diff] [blame] | 122 | RequiresDstTexture finalize(const GrCaps&, const GrAppliedClip*) override { |
| 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 | } |
| 136 | state->commandBuffer()->draw(pipeline, GrPipelineDynamicStateTestProcessor(), |
| 137 | meshes.begin(), kDynamicStates, 4, |
| 138 | SkRect::MakeIWH(kScreenSize, kScreenSize)); |
| 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(); |
| 149 | GrResourceProvider* rp = context->resourceProvider(); |
| 150 | |
| 151 | sk_sp<GrRenderTargetContext> rtc( |
| 152 | context->makeDeferredRenderTargetContext(SkBackingFit::kExact, kScreenSize, kScreenSize, |
| 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}) { |
| 195 | rtc->clear(nullptr, 0xbaaaaaad, true); |
| 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 |