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 | |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 31 | using ScissorState = GrPipeline::ScissorState; |
| 32 | |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 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 | |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 38 | static const SkIRect kDynamicScissors[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), |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 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() |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 61 | : INHERITED(kGrPipelineDynamicStateTestProcessor_ClassID) { |
| 62 | this->setVertexAttributeCnt(2); |
| 63 | } |
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 | |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 71 | private: |
| 72 | const Attribute& onVertexAttribute(int i) const override { |
| 73 | return IthAttribute(i, kVertex, kColor); |
| 74 | } |
| 75 | |
| 76 | static constexpr Attribute kVertex = {"vertex", kHalf2_GrVertexAttribType}; |
| 77 | static constexpr Attribute kColor = {"color", kUByte4_norm_GrVertexAttribType}; |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 78 | |
| 79 | friend class GLSLPipelineDynamicStateTestProcessor; |
| 80 | typedef GrGeometryProcessor INHERITED; |
| 81 | }; |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 82 | constexpr GrPrimitiveProcessor::Attribute GrPipelineDynamicStateTestProcessor::kVertex; |
| 83 | constexpr GrPrimitiveProcessor::Attribute GrPipelineDynamicStateTestProcessor::kColor; |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 84 | |
| 85 | class GLSLPipelineDynamicStateTestProcessor : public GrGLSLGeometryProcessor { |
| 86 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor&, |
| 87 | FPCoordTransformIter&& transformIter) final {} |
| 88 | |
| 89 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) final { |
| 90 | const GrPipelineDynamicStateTestProcessor& mp = |
| 91 | args.fGP.cast<GrPipelineDynamicStateTestProcessor>(); |
| 92 | |
| 93 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
| 94 | varyingHandler->emitAttributes(mp); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 95 | varyingHandler->addPassThroughAttribute(mp.kColor, args.fOutputColor); |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 96 | |
| 97 | GrGLSLVertexBuilder* v = args.fVertBuilder; |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 98 | v->codeAppendf("float2 vertex = %s;", mp.kVertex.name()); |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 99 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertex"); |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 100 | |
Chris Dalton | 6028361 | 2018-02-14 13:38:14 -0700 | [diff] [blame] | 101 | GrGLSLFPFragmentBuilder* f = args.fFragBuilder; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 102 | f->codeAppendf("%s = half4(1);", args.fOutputCoverage); |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 103 | } |
| 104 | }; |
| 105 | |
| 106 | GrGLSLPrimitiveProcessor* |
| 107 | GrPipelineDynamicStateTestProcessor::createGLSLInstance(const GrShaderCaps&) const { |
| 108 | return new GLSLPipelineDynamicStateTestProcessor; |
| 109 | } |
| 110 | |
| 111 | class GrPipelineDynamicStateTestOp : public GrDrawOp { |
| 112 | public: |
| 113 | DEFINE_OP_CLASS_ID |
| 114 | |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 115 | static std::unique_ptr<GrDrawOp> Make(GrContext* context, |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 116 | ScissorState scissorState, |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 117 | sk_sp<const GrBuffer> vbuff) { |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 118 | GrOpMemoryPool* pool = context->contextPriv().opMemoryPool(); |
| 119 | |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 120 | return pool->allocate<GrPipelineDynamicStateTestOp>(scissorState, std::move(vbuff)); |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | private: |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 124 | friend class GrOpMemoryPool; |
| 125 | |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 126 | GrPipelineDynamicStateTestOp(ScissorState scissorState, sk_sp<const GrBuffer> vbuff) |
| 127 | : INHERITED(ClassID()) |
| 128 | , fScissorState(scissorState) |
| 129 | , fVertexBuffer(std::move(vbuff)) { |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 130 | this->setBounds(SkRect::MakeIWH(kScreenSize, kScreenSize), |
| 131 | HasAABloat::kNo, IsZeroArea::kNo); |
| 132 | } |
| 133 | |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 134 | const char* name() const override { return "GrPipelineDynamicStateTestOp"; } |
| 135 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
Brian Osman | 532b3f9 | 2018-07-11 10:02:07 -0400 | [diff] [blame] | 136 | RequiresDstTexture finalize(const GrCaps&, const GrAppliedClip*) override { |
Brian Salomon | f86d37b | 2017-06-16 10:04:34 -0400 | [diff] [blame] | 137 | return RequiresDstTexture::kNo; |
| 138 | } |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 139 | bool onCombineIfPossible(GrOp* other, const GrCaps& caps) override { return false; } |
| 140 | void onPrepare(GrOpFlushState*) override {} |
| 141 | void onExecute(GrOpFlushState* state) override { |
Robert Phillips | 2890fbf | 2017-07-26 15:48:41 -0400 | [diff] [blame] | 142 | GrRenderTargetProxy* proxy = state->drawOpArgs().fProxy; |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 143 | GrPipeline pipeline(proxy, fScissorState, SkBlendMode::kSrc); |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 144 | SkSTArray<kNumMeshes, GrMesh> meshes; |
| 145 | for (int i = 0; i < kNumMeshes; ++i) { |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 146 | GrMesh& mesh = meshes.emplace_back(GrPrimitiveType::kTriangleStrip); |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 147 | mesh.setNonIndexedNonInstanced(4); |
| 148 | mesh.setVertexData(fVertexBuffer.get(), 4 * i); |
| 149 | } |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 150 | GrPipeline::DynamicStateArrays dynamicState; |
| 151 | dynamicState.fScissorRects = kDynamicScissors; |
| 152 | state->rtCommandBuffer()->draw(GrPipelineDynamicStateTestProcessor(), pipeline, nullptr, |
| 153 | &dynamicState, meshes.begin(), 4, |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 154 | SkRect::MakeIWH(kScreenSize, kScreenSize)); |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 155 | } |
| 156 | |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 157 | ScissorState fScissorState; |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 158 | const sk_sp<const GrBuffer> fVertexBuffer; |
| 159 | |
| 160 | typedef GrDrawOp INHERITED; |
| 161 | }; |
| 162 | |
| 163 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrPipelineDynamicStateTest, reporter, ctxInfo) { |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 164 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 165 | GrResourceProvider* rp = context->contextPriv().resourceProvider(); |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 166 | |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 167 | sk_sp<GrRenderTargetContext> rtc(context->contextPriv().makeDeferredRenderTargetContext( |
| 168 | SkBackingFit::kExact, kScreenSize, kScreenSize, |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 169 | kRGBA_8888_GrPixelConfig, nullptr)); |
| 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, |
| 200 | GrResourceProvider::kNoPendingIO_Flag | |
| 201 | GrResourceProvider::kRequireGpuMemory_Flag, |
| 202 | vdata)); |
| 203 | if (!vbuff) { |
| 204 | ERRORF(reporter, "vbuff is null."); |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | uint32_t resultPx[kScreenSize * kScreenSize]; |
| 209 | |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 210 | for (ScissorState scissorState : {ScissorState::kEnabled, ScissorState::kDisabled}) { |
Chris Dalton | 344e903 | 2017-12-11 15:42:09 -0700 | [diff] [blame] | 211 | rtc->clear(nullptr, 0xbaaaaaad, GrRenderTargetContext::CanClearFullscreen::kYes); |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 212 | rtc->priv().testingOnly_addDrawOp( |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 213 | GrPipelineDynamicStateTestOp::Make(context, scissorState, vbuff)); |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 214 | rtc->readPixels(SkImageInfo::Make(kScreenSize, kScreenSize, |
| 215 | kRGBA_8888_SkColorType, kPremul_SkAlphaType), |
| 216 | resultPx, 4 * kScreenSize, 0, 0, 0); |
| 217 | for (int y = 0; y < kScreenSize; ++y) { |
| 218 | for (int x = 0; x < kScreenSize; ++x) { |
| 219 | int expectedColorIdx; |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 220 | if (ScissorState::kEnabled == scissorState) { |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 221 | expectedColorIdx = (x < kScreenSplitX ? 0 : 2) + (y < kScreenSplitY ? 0 : 1); |
| 222 | } else { |
| 223 | expectedColorIdx = kNumMeshes - 1; |
| 224 | } |
| 225 | uint32_t expected = kMeshColors[expectedColorIdx]; |
| 226 | uint32_t actual = resultPx[y * kScreenSize + x]; |
| 227 | if (expected != actual) { |
| 228 | ERRORF(reporter, "[scissor=%s] pixel (%i,%i): got 0x%x expected 0x%x", |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 229 | ScissorState::kEnabled == scissorState ? "enabled" : "disabled", x, y, |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 230 | actual, expected); |
| 231 | return; |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | } |