• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "GrContext.h"
12 #include "GrColor.h"
13 #include "GrGeometryProcessor.h"
14 #include "GrGpuCommandBuffer.h"
15 #include "GrMemoryPool.h"
16 #include "GrOpFlushState.h"
17 #include "GrRenderTargetContext.h"
18 #include "GrRenderTargetContextPriv.h"
19 #include "GrResourceProvider.h"
20 #include "SkMakeUnique.h"
21 #include "glsl/GrGLSLVertexGeoBuilder.h"
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 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 
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),
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:
GrPipelineDynamicStateTestProcessor()58     GrPipelineDynamicStateTestProcessor()
59             : INHERITED(kGrPipelineDynamicStateTestProcessor_ClassID) {
60         this->setVertexAttributes(kAttributes, SK_ARRAY_COUNT(kAttributes));
61     }
62 
name() const63     const char* name() const override { return "GrPipelineDynamicStateTest Processor"; }
64 
getGLSLProcessorKey(const GrShaderCaps &,GrProcessorKeyBuilder *) const65     void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const final {}
66 
67     GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final;
68 
inVertex() const69     const Attribute& inVertex() const { return kAttributes[0]; }
inColor() const70     const Attribute& inColor() const { return kAttributes[1]; }
71 
72 private:
73     static constexpr Attribute kAttributes[] = {
74         {"vertex", kFloat2_GrVertexAttribType, kHalf2_GrSLType},
75         {"color", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType},
76     };
77 
78     friend class GLSLPipelineDynamicStateTestProcessor;
79     typedef GrGeometryProcessor INHERITED;
80 };
81 constexpr GrPrimitiveProcessor::Attribute GrPipelineDynamicStateTestProcessor::kAttributes[];
82 
83 class GLSLPipelineDynamicStateTestProcessor : public GrGLSLGeometryProcessor {
setData(const GrGLSLProgramDataManager & pdman,const GrPrimitiveProcessor &,FPCoordTransformIter && transformIter)84     void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor&,
85                  FPCoordTransformIter&& transformIter) final {}
86 
onEmitCode(EmitArgs & args,GrGPArgs * gpArgs)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);
93         varyingHandler->addPassThroughAttribute(mp.inColor(), args.fOutputColor);
94 
95         GrGLSLVertexBuilder* v = args.fVertBuilder;
96         v->codeAppendf("float2 vertex = %s;", mp.inVertex().name());
97         gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertex");
98 
99         GrGLSLFPFragmentBuilder* f = args.fFragBuilder;
100         f->codeAppendf("%s = half4(1);", args.fOutputCoverage);
101     }
102 };
103 
104 GrGLSLPrimitiveProcessor*
createGLSLInstance(const GrShaderCaps &) const105 GrPipelineDynamicStateTestProcessor::createGLSLInstance(const GrShaderCaps&) const {
106     return new GLSLPipelineDynamicStateTestProcessor;
107 }
108 
109 class GrPipelineDynamicStateTestOp : public GrDrawOp {
110 public:
111     DEFINE_OP_CLASS_ID
112 
Make(GrContext * context,GrScissorTest scissorTest,sk_sp<const GrBuffer> vbuff)113     static std::unique_ptr<GrDrawOp> Make(GrContext* context,
114                                           GrScissorTest scissorTest,
115                                           sk_sp<const GrBuffer> vbuff) {
116         GrOpMemoryPool* pool = context->contextPriv().opMemoryPool();
117 
118         return pool->allocate<GrPipelineDynamicStateTestOp>(scissorTest, std::move(vbuff));
119     }
120 
121 private:
122     friend class GrOpMemoryPool;
123 
GrPipelineDynamicStateTestOp(GrScissorTest scissorTest,sk_sp<const GrBuffer> vbuff)124     GrPipelineDynamicStateTestOp(GrScissorTest scissorTest, sk_sp<const GrBuffer> vbuff)
125         : INHERITED(ClassID())
126         , fScissorTest(scissorTest)
127         , fVertexBuffer(std::move(vbuff)) {
128         this->setBounds(SkRect::MakeIWH(kScreenSize, kScreenSize),
129                         HasAABloat::kNo, IsZeroArea::kNo);
130     }
131 
name() const132     const char* name() const override { return "GrPipelineDynamicStateTestOp"; }
fixedFunctionFlags() const133     FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
finalize(const GrCaps &,const GrAppliedClip *)134     GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*) override {
135         return GrProcessorSet::EmptySetAnalysis();
136     }
onPrepare(GrOpFlushState *)137     void onPrepare(GrOpFlushState*) override {}
onExecute(GrOpFlushState * state,const SkRect & chainBounds)138     void onExecute(GrOpFlushState* state, const SkRect& chainBounds) override {
139         GrPipeline pipeline(fScissorTest, SkBlendMode::kSrc);
140         SkSTArray<kNumMeshes, GrMesh> meshes;
141         for (int i = 0; i < kNumMeshes; ++i) {
142             GrMesh& mesh = meshes.emplace_back(GrPrimitiveType::kTriangleStrip);
143             mesh.setNonIndexedNonInstanced(4);
144             mesh.setVertexData(fVertexBuffer, 4 * i);
145         }
146         GrPipeline::DynamicStateArrays dynamicState;
147         dynamicState.fScissorRects = kDynamicScissors;
148         state->rtCommandBuffer()->draw(GrPipelineDynamicStateTestProcessor(), pipeline, nullptr,
149                                        &dynamicState, meshes.begin(), 4,
150                                        SkRect::MakeIWH(kScreenSize, kScreenSize));
151     }
152 
153     GrScissorTest               fScissorTest;
154     const sk_sp<const GrBuffer> fVertexBuffer;
155 
156     typedef GrDrawOp INHERITED;
157 };
158 
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrPipelineDynamicStateTest,reporter,ctxInfo)159 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrPipelineDynamicStateTest, reporter, ctxInfo) {
160     GrContext* context = ctxInfo.grContext();
161     GrResourceProvider* rp = context->contextPriv().resourceProvider();
162 
163     const GrBackendFormat format =
164             context->contextPriv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
165 
166     sk_sp<GrRenderTargetContext> rtc(context->contextPriv().makeDeferredRenderTargetContext(
167                                                  format, SkBackingFit::kExact, kScreenSize,
168                                                  kScreenSize, kRGBA_8888_GrPixelConfig, nullptr));
169     if (!rtc) {
170         ERRORF(reporter, "could not create render target context.");
171         return;
172     }
173 
174     constexpr float d = (float) kScreenSize;
175     Vertex vdata[kNumMeshes * 4] = {
176         {0, 0, kMeshColors[0]},
177         {0, d, kMeshColors[0]},
178         {d, 0, kMeshColors[0]},
179         {d, d, kMeshColors[0]},
180 
181         {0, 0, kMeshColors[1]},
182         {0, d, kMeshColors[1]},
183         {d, 0, kMeshColors[1]},
184         {d, d, kMeshColors[1]},
185 
186         {0, 0, kMeshColors[2]},
187         {0, d, kMeshColors[2]},
188         {d, 0, kMeshColors[2]},
189         {d, d, kMeshColors[2]},
190 
191         {0, 0, kMeshColors[3]},
192         {0, d, kMeshColors[3]},
193         {d, 0, kMeshColors[3]},
194         {d, d, kMeshColors[3]}
195     };
196 
197     sk_sp<const GrBuffer> vbuff(rp->createBuffer(sizeof(vdata), kVertex_GrBufferType,
198                                                  kDynamic_GrAccessPattern,
199                                                  GrResourceProvider::Flags::kRequireGpuMemory,
200                                                  vdata));
201     if (!vbuff) {
202         ERRORF(reporter, "vbuff is null.");
203         return;
204     }
205 
206     uint32_t resultPx[kScreenSize * kScreenSize];
207 
208     for (GrScissorTest scissorTest : {GrScissorTest::kEnabled, GrScissorTest::kDisabled}) {
209         rtc->clear(nullptr, SkPMColor4f::FromBytes_RGBA(0xbaaaaaad),
210                    GrRenderTargetContext::CanClearFullscreen::kYes);
211         rtc->priv().testingOnly_addDrawOp(
212             GrPipelineDynamicStateTestOp::Make(context, scissorTest, vbuff));
213         rtc->readPixels(SkImageInfo::Make(kScreenSize, kScreenSize,
214                                           kRGBA_8888_SkColorType, kPremul_SkAlphaType),
215                         resultPx, 4 * kScreenSize, 0, 0, 0);
216         for (int y = 0; y < kScreenSize; ++y) {
217             for (int x = 0; x < kScreenSize; ++x) {
218                 int expectedColorIdx;
219                 if (GrScissorTest::kEnabled == scissorTest) {
220                     expectedColorIdx = (x < kScreenSplitX ? 0 : 2) + (y < kScreenSplitY ? 0 : 1);
221                 } else {
222                     expectedColorIdx = kNumMeshes - 1;
223                 }
224                 uint32_t expected = kMeshColors[expectedColorIdx];
225                 uint32_t actual = resultPx[y * kScreenSize + x];
226                 if (expected != actual) {
227                     ERRORF(reporter, "[scissor=%s] pixel (%i,%i): got 0x%x expected 0x%x",
228                            GrScissorTest::kEnabled == scissorTest ? "enabled" : "disabled", x, y,
229                            actual, expected);
230                     return;
231                 }
232             }
233         }
234     }
235 }
236