1 /*
2 * Copyright 2016 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 // This is a GPU-backend specific test. It relies on static initializers to work
9
10 #include "include/core/SkColorSpace.h"
11 #include "include/core/SkRect.h"
12 #include "include/core/SkString.h"
13 #include "include/core/SkSurfaceProps.h"
14 #include "include/core/SkTypes.h"
15 #include "include/gpu/ganesh/GrDirectContext.h"
16 #include "include/private/gpu/ganesh/GrTypesPriv.h"
17 #include "src/base/SkArenaAlloc.h"
18 #include "src/core/SkPointPriv.h"
19 #include "src/core/SkSLTypeShared.h"
20 #include "src/gpu/KeyBuilder.h"
21 #include "src/gpu/SkBackingFit.h"
22 #include "src/gpu/ganesh/GrAppliedClip.h"
23 #include "src/gpu/ganesh/GrCaps.h"
24 #include "src/gpu/ganesh/GrDirectContextPriv.h"
25 #include "src/gpu/ganesh/GrGeometryProcessor.h"
26 #include "src/gpu/ganesh/GrGpu.h"
27 #include "src/gpu/ganesh/GrOpFlushState.h"
28 #include "src/gpu/ganesh/GrPaint.h"
29 #include "src/gpu/ganesh/GrPipeline.h"
30 #include "src/gpu/ganesh/GrProcessorSet.h"
31 #include "src/gpu/ganesh/GrProgramInfo.h"
32 #include "src/gpu/ganesh/SurfaceDrawContext.h"
33 #include "src/gpu/ganesh/glsl/GrGLSLFragmentShaderBuilder.h"
34 #include "src/gpu/ganesh/glsl/GrGLSLVarying.h"
35 #include "src/gpu/ganesh/ops/GrMeshDrawOp.h"
36 #include "src/gpu/ganesh/ops/GrOp.h"
37 #include "src/gpu/ganesh/ops/GrSimpleMeshDrawOpHelper.h"
38 #include "tests/CtsEnforcement.h"
39 #include "tests/Test.h"
40
41 #include <cstddef>
42 #include <memory>
43 #include <utility>
44
45 class GrDstProxyView;
46 class GrGLSLProgramDataManager;
47 class GrMeshDrawTarget;
48 class GrRecordingContext;
49 class GrSurfaceProxyView;
50 enum class GrXferBarrierFlags;
51 struct GrContextOptions;
52 struct GrShaderCaps;
53 struct GrSimpleMesh;
54 struct SkPoint;
55
56 namespace {
57 class Op : public GrMeshDrawOp {
58 public:
59 DEFINE_OP_CLASS_ID
60
name() const61 const char* name() const override { return "Test Op"; }
62
Make(GrRecordingContext * rContext,int numAttribs)63 static GrOp::Owner Make(GrRecordingContext* rContext, int numAttribs) {
64 return GrOp::Make<Op>(rContext, numAttribs);
65 }
66
fixedFunctionFlags() const67 FixedFunctionFlags fixedFunctionFlags() const override {
68 return FixedFunctionFlags::kNone;
69 }
70
finalize(const GrCaps &,const GrAppliedClip *,GrClampType)71 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*, GrClampType) override {
72 return GrProcessorSet::EmptySetAnalysis();
73 }
74
75 private:
76 friend class ::GrOp;
77
Op(int numAttribs)78 Op(int numAttribs) : INHERITED(ClassID()), fNumAttribs(numAttribs) {
79 this->setBounds(SkRect::MakeWH(1.f, 1.f), HasAABloat::kNo, IsHairline::kNo);
80 }
81
programInfo()82 GrProgramInfo* programInfo() override { return fProgramInfo; }
83
onCreateProgramInfo(const GrCaps * caps,SkArenaAlloc * arena,const GrSurfaceProxyView & writeView,bool usesMSAASurface,GrAppliedClip && appliedClip,const GrDstProxyView & dstProxyView,GrXferBarrierFlags renderPassXferBarriers,GrLoadOp colorLoadOp)84 void onCreateProgramInfo(const GrCaps* caps,
85 SkArenaAlloc* arena,
86 const GrSurfaceProxyView& writeView,
87 bool usesMSAASurface,
88 GrAppliedClip&& appliedClip,
89 const GrDstProxyView& dstProxyView,
90 GrXferBarrierFlags renderPassXferBarriers,
91 GrLoadOp colorLoadOp) override {
92 class GP : public GrGeometryProcessor {
93 public:
94 static GrGeometryProcessor* Make(SkArenaAlloc* arena, int numAttribs) {
95 return arena->make([&](void* ptr) {
96 return new (ptr) GP(numAttribs);
97 });
98 }
99
100 const char* name() const override { return "Test GP"; }
101
102 std::unique_ptr<ProgramImpl> makeProgramImpl(const GrShaderCaps&) const override {
103 class Impl : public ProgramImpl {
104 public:
105 void setData(const GrGLSLProgramDataManager&,
106 const GrShaderCaps&,
107 const GrGeometryProcessor&) override {}
108
109 private:
110 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
111 const GP& gp = args.fGeomProc.cast<GP>();
112 args.fVaryingHandler->emitAttributes(gp);
113 WriteOutputPosition(args.fVertBuilder, gpArgs, gp.fAttributes[0].name());
114 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
115 fragBuilder->codeAppendf("const half4 %s = half4(1);", args.fOutputColor);
116 fragBuilder->codeAppendf("const half4 %s = half4(1);",
117 args.fOutputCoverage);
118 }
119 };
120
121 return std::make_unique<Impl>();
122 }
123 void addToKey(const GrShaderCaps&, skgpu::KeyBuilder* builder) const override {
124 builder->add32(fNumAttribs);
125 }
126
127 private:
128 GP(int numAttribs) : INHERITED(kGP_ClassID), fNumAttribs(numAttribs) {
129 SkASSERT(numAttribs > 1);
130 fAttribNames = std::make_unique<SkString[]>(numAttribs);
131 fAttributes = std::make_unique<Attribute[]>(numAttribs);
132 for (auto i = 0; i < numAttribs; ++i) {
133 fAttribNames[i].printf("attr%d", i);
134 // This gives us more of a mix of attribute types, and allows the
135 // component count to fit within the limits for iOS Metal.
136 if (i & 0x1) {
137 fAttributes[i] = {fAttribNames[i].c_str(), kFloat_GrVertexAttribType,
138 SkSLType::kFloat};
139 } else {
140 fAttributes[i] = {fAttribNames[i].c_str(), kFloat2_GrVertexAttribType,
141 SkSLType::kFloat2};
142 }
143 }
144 this->setVertexAttributesWithImplicitOffsets(fAttributes.get(), numAttribs);
145 }
146
147 int fNumAttribs;
148 std::unique_ptr<SkString[]> fAttribNames;
149 std::unique_ptr<Attribute[]> fAttributes;
150
151 using INHERITED = GrGeometryProcessor;
152 };
153
154 GrGeometryProcessor* gp = GP::Make(arena, fNumAttribs);
155
156 fProgramInfo = GrSimpleMeshDrawOpHelper::CreateProgramInfo(caps,
157 arena,
158 writeView,
159 usesMSAASurface,
160 std::move(appliedClip),
161 dstProxyView,
162 gp,
163 GrProcessorSet::MakeEmptySet(),
164 GrPrimitiveType::kTriangles,
165 renderPassXferBarriers,
166 colorLoadOp,
167 GrPipeline::InputFlags::kNone);
168 }
169
onPrepareDraws(GrMeshDrawTarget * target)170 void onPrepareDraws(GrMeshDrawTarget* target) override {
171 if (!fProgramInfo) {
172 this->createProgramInfo(target);
173 }
174
175 size_t vertexStride = fProgramInfo->geomProc().vertexStride();
176 QuadHelper helper(target, vertexStride, 1);
177 SkPoint* vertices = reinterpret_cast<SkPoint*>(helper.vertices());
178 SkPointPriv::SetRectTriStrip(vertices, 0.f, 0.f, 1.f, 1.f, vertexStride);
179 fMesh = helper.mesh();
180 }
181
onExecute(GrOpFlushState * flushState,const SkRect & chainBounds)182 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
183 if (!fProgramInfo || !fMesh) {
184 return;
185 }
186
187 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
188 flushState->bindTextures(fProgramInfo->geomProc(), nullptr, fProgramInfo->pipeline());
189 flushState->drawMesh(*fMesh);
190 }
191
192 int fNumAttribs;
193 GrSimpleMesh* fMesh = nullptr;
194 GrProgramInfo* fProgramInfo = nullptr;
195
196 using INHERITED = GrMeshDrawOp;
197 };
198 } // namespace
199
DEF_GANESH_TEST_FOR_ALL_CONTEXTS(VertexAttributeCount,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)200 DEF_GANESH_TEST_FOR_ALL_CONTEXTS(VertexAttributeCount,
201 reporter,
202 ctxInfo,
203 CtsEnforcement::kApiLevel_T) {
204 auto dContext = ctxInfo.directContext();
205 #if GR_GPU_STATS
206 GrGpu* gpu = dContext->priv().getGpu();
207 #endif
208
209 auto sdc = skgpu::ganesh::SurfaceDrawContext::Make(dContext,
210 GrColorType::kRGBA_8888,
211 nullptr,
212 SkBackingFit::kApprox,
213 {1, 1},
214 SkSurfaceProps(),
215 /*label=*/{});
216 if (!sdc) {
217 ERRORF(reporter, "Could not create render target context.");
218 return;
219 }
220 int attribCnt = dContext->priv().caps()->maxVertexAttributes();
221 if (!attribCnt) {
222 ERRORF(reporter, "No attributes allowed?!");
223 return;
224 }
225 dContext->flushAndSubmit();
226 dContext->priv().resetGpuStats();
227 #if GR_GPU_STATS
228 REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 0);
229 REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 0);
230 #endif
231 // Adding discard to appease vulkan validation warning about loading uninitialized data on draw
232 sdc->discard();
233
234 GrPaint grPaint;
235 // This one should succeed.
236 sdc->addDrawOp(Op::Make(dContext, attribCnt));
237 dContext->flushAndSubmit();
238 #if GR_GPU_STATS
239 REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 1);
240 REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 0);
241 #endif
242 dContext->priv().resetGpuStats();
243 sdc->addDrawOp(Op::Make(dContext, attribCnt + 1));
244 dContext->flushAndSubmit();
245 #if GR_GPU_STATS
246 REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 0);
247 REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 1);
248 #endif
249 }
250