blob: c31aa1e0da6b909a3d884f8d9e0ffcea7dfdd0a3 [file] [log] [blame]
bsalomon@google.coma8e686e2011-08-16 15:45:58 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
bsalomon@google.comd4726202012-08-03 14:34:46 +00009// This is a GPU-backend specific test. It relies on static intializers to work
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000010
bsalomon@google.com2a48c3a2012-08-03 14:54:45 +000011#include "SkTypes.h"
12
13#if SK_SUPPORT_GPU && SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
14
joshualittb0a8a372014-09-23 09:50:21 -070015#include "GrBackendProcessorFactory.h"
bsalomon@google.com67b915d2013-02-04 16:13:32 +000016#include "GrContextFactory.h"
egdanielc0648242014-09-22 13:17:02 -070017#include "GrOptDrawState.h"
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000018#include "effects/GrConfigConversionEffect.h"
joshualittfe1233c2014-10-07 12:16:35 -070019#include "gl/builders/GrGLProgramBuilder.h"
kkinnunenec56e452014-08-25 22:21:16 -070020#include "gl/GrGLPathRendering.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000021#include "gl/GrGpuGL.h"
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000022#include "SkChecksum.h"
tfarina@chromium.org223137f2012-11-21 22:38:36 +000023#include "SkRandom.h"
bsalomon@google.comc3841b92012-08-02 18:11:43 +000024#include "Test.h"
25
joshualittd9097592014-10-07 08:37:36 -070026static void get_stage_stats(const GrFragmentStage stage, bool* readsDst,
27 bool* readsFragPosition, bool* requiresVertexShader) {
joshualittfe1233c2014-10-07 12:16:35 -070028 if (stage.getProcessor()->willReadDstColor()) {
joshualittd9097592014-10-07 08:37:36 -070029 *readsDst = true;
joshualittbd769d02014-09-04 08:56:46 -070030 }
joshualittd9097592014-10-07 08:37:36 -070031 if (stage.getProcessor()->willReadFragmentPosition()) {
32 *readsFragPosition = true;
33 }
34}
35
36bool GrGLProgramDesc::setRandom(SkRandom* random,
37 GrGpuGL* gpu,
38 const GrRenderTarget* dstRenderTarget,
39 const GrTexture* dstCopyTexture,
40 const GrGeometryStage* geometryProcessor,
41 const GrFragmentStage* stages[],
42 int numColorStages,
43 int numCoverageStages,
44 int currAttribIndex,
45 GrGpu::DrawType drawType) {
46 bool isPathRendering = GrGpu::IsPathRenderingDrawType(drawType);
47 bool useLocalCoords = !isPathRendering &&
48 random->nextBool() &&
49 currAttribIndex < GrDrawState::kMaxVertexAttribCnt;
50
51 int numStages = numColorStages + numCoverageStages;
52 fKey.reset();
53
54 GR_STATIC_ASSERT(0 == kEffectKeyOffsetsAndLengthOffset % sizeof(uint32_t));
55
56 // Make room for everything up to and including the array of offsets to effect keys.
57 fKey.push_back_n(kEffectKeyOffsetsAndLengthOffset + 2 * sizeof(uint16_t) * (numStages +
58 (geometryProcessor ? 1 : 0)));
59
60 bool dstRead = false;
61 bool fragPos = false;
62 bool vertexShader = SkToBool(geometryProcessor);
63 int offset = 0;
64 if (geometryProcessor) {
65 const GrGeometryStage* stage = geometryProcessor;
66 uint16_t* offsetAndSize = reinterpret_cast<uint16_t*>(fKey.begin() +
67 kEffectKeyOffsetsAndLengthOffset +
68 offset * 2 * sizeof(uint16_t));
69 uint32_t effectKeyOffset = fKey.count();
70 if (effectKeyOffset > SK_MaxU16) {
71 fKey.reset();
72 return false;
73 }
74 GrProcessorKeyBuilder b(&fKey);
75 uint16_t effectKeySize;
76 if (!GetProcessorKey(*stage, gpu->glCaps(), useLocalCoords, &b, &effectKeySize)) {
77 fKey.reset();
78 return false;
79 }
80 vertexShader = true;
81 fragPos = stage->getProcessor()->willReadFragmentPosition();
82 offsetAndSize[0] = effectKeyOffset;
83 offsetAndSize[1] = effectKeySize;
84 offset++;
85 }
86
87 for (int s = 0; s < numStages; ++s, ++offset) {
88 const GrFragmentStage* stage = stages[s];
89 uint16_t* offsetAndSize = reinterpret_cast<uint16_t*>(fKey.begin() +
90 kEffectKeyOffsetsAndLengthOffset +
91 offset * 2 * sizeof(uint16_t));
92 uint32_t effectKeyOffset = fKey.count();
93 if (effectKeyOffset > SK_MaxU16) {
94 fKey.reset();
95 return false;
96 }
97 GrProcessorKeyBuilder b(&fKey);
98 uint16_t effectKeySize;
99 if (!GetProcessorKey(*stages[s], gpu->glCaps(), useLocalCoords, &b, &effectKeySize)) {
100 fKey.reset();
101 return false;
102 }
103 get_stage_stats(*stage, &dstRead, &fragPos, &vertexShader);
104 offsetAndSize[0] = effectKeyOffset;
105 offsetAndSize[1] = effectKeySize;
106 }
107
108 KeyHeader* header = this->header();
109 memset(header, 0, kHeaderSize);
110 header->fEmitsPointSize = random->nextBool();
111
112 header->fPositionAttributeIndex = 0;
113
114 // if the effects have used up all off the available attributes,
115 // don't try to use color or coverage attributes as input
116 do {
117 header->fColorInput = static_cast<GrGLProgramDesc::ColorInput>(
118 random->nextULessThan(kColorInputCnt));
119 } while ((GrDrawState::kMaxVertexAttribCnt <= currAttribIndex || isPathRendering) &&
120 kAttribute_ColorInput == header->fColorInput);
121 header->fColorAttributeIndex = (header->fColorInput == kAttribute_ColorInput) ?
122 currAttribIndex++ :
123 -1;
124
125 do {
126 header->fCoverageInput = static_cast<GrGLProgramDesc::ColorInput>(
127 random->nextULessThan(kColorInputCnt));
128 } while ((GrDrawState::kMaxVertexAttribCnt <= currAttribIndex || isPathRendering) &&
129 kAttribute_ColorInput == header->fCoverageInput);
130 header->fCoverageAttributeIndex = (header->fCoverageInput == kAttribute_ColorInput) ?
131 currAttribIndex++ :
132 -1;
133 bool useGS = random->nextBool();
134#if GR_GL_EXPERIMENTAL_GS
135 header->fExperimentalGS = gpu->caps()->geometryShaderSupport() && useGS;
136#else
137 (void) useGS;
138#endif
139
140 header->fLocalCoordAttributeIndex = useLocalCoords ? currAttribIndex++ : -1;
141
142 header->fColorEffectCnt = numColorStages;
143 header->fCoverageEffectCnt = numCoverageStages;
144
145 if (dstRead) {
146 header->fDstReadKey = SkToU8(GrGLFragmentShaderBuilder::KeyForDstRead(dstCopyTexture,
147 gpu->glCaps()));
148 } else {
149 header->fDstReadKey = 0;
150 }
151 if (fragPos) {
152 header->fFragPosKey = SkToU8(GrGLFragmentShaderBuilder::KeyForFragmentPosition(dstRenderTarget,
153 gpu->glCaps()));
154 } else {
155 header->fFragPosKey = 0;
156 }
157
158 header->fUseFragShaderOnly = isPathRendering && gpu->glPathRendering()->texturingMode() ==
159 GrGLPathRendering::FixedFunction_TexturingMode;
160 header->fHasGeometryProcessor = vertexShader;
161
162 GrOptDrawState::PrimaryOutputType primaryOutput;
163 GrOptDrawState::SecondaryOutputType secondaryOutput;
164 if (!dstRead) {
165 primaryOutput = GrOptDrawState::kModulate_PrimaryOutputType;
166 } else {
167 primaryOutput = static_cast<GrOptDrawState::PrimaryOutputType>(
168 random->nextULessThan(GrOptDrawState::kPrimaryOutputTypeCnt));
169 }
170
171 if (GrOptDrawState::kCombineWithDst_PrimaryOutputType == primaryOutput ||
172 !gpu->caps()->dualSourceBlendingSupport()) {
173 secondaryOutput = GrOptDrawState::kNone_SecondaryOutputType;
174 } else {
175 secondaryOutput = static_cast<GrOptDrawState::SecondaryOutputType>(
176 random->nextULessThan(GrOptDrawState::kSecondaryOutputTypeCnt));
177 }
178
179 header->fPrimaryOutputType = primaryOutput;
180 header->fSecondaryOutputType = secondaryOutput;
181
182 this->finalize();
183 return true;
bsalomon@google.com91207482013-02-12 21:45:24 +0000184}
185
joshualitt249af152014-09-15 11:41:13 -0700186// TODO clean this up, we have to do this to test geometry processors but there has got to be
187// a better way. In the mean time, we actually fill out these generic vertex attribs below with
188// the correct vertex attribs from the GP. We have to ensure, however, we don't try to add more
joshualittd9097592014-10-07 08:37:36 -0700189// than two attributes.
190GrVertexAttrib genericVertexAttribs[] = {
joshualitt249af152014-09-15 11:41:13 -0700191 { kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding },
joshualittb0a8a372014-09-23 09:50:21 -0700192 { kVec2f_GrVertexAttribType, 0, kGeometryProcessor_GrVertexAttribBinding },
193 { kVec2f_GrVertexAttribType, 0, kGeometryProcessor_GrVertexAttribBinding }
joshualitt249af152014-09-15 11:41:13 -0700194};
195
196/*
197 * convert sl type to vertexattrib type, not a complete implementation, only use for debugging
198 */
joshualittd9097592014-10-07 08:37:36 -0700199GrVertexAttribType convert_sltype_to_attribtype(GrSLType type) {
joshualitt249af152014-09-15 11:41:13 -0700200 switch (type) {
201 case kFloat_GrSLType:
202 return kFloat_GrVertexAttribType;
203 case kVec2f_GrSLType:
204 return kVec2f_GrVertexAttribType;
205 case kVec3f_GrSLType:
206 return kVec3f_GrVertexAttribType;
207 case kVec4f_GrSLType:
208 return kVec4f_GrVertexAttribType;
209 default:
210 SkFAIL("Type isn't convertible");
211 return kFloat_GrVertexAttribType;
212 }
213}
joshualittd9097592014-10-07 08:37:36 -0700214// TODO end test hack
joshualitt249af152014-09-15 11:41:13 -0700215
216
bsalomon@google.com042a2862013-02-04 18:39:24 +0000217bool GrGpuGL::programUnitTest(int maxStages) {
joshualittd9097592014-10-07 08:37:36 -0700218
bsalomon@google.comd4726202012-08-03 14:34:46 +0000219 GrTextureDesc dummyDesc;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000220 dummyDesc.fFlags = kRenderTarget_GrTextureFlagBit;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000221 dummyDesc.fConfig = kSkia8888_GrPixelConfig;
bsalomon@google.comd4726202012-08-03 14:34:46 +0000222 dummyDesc.fWidth = 34;
223 dummyDesc.fHeight = 18;
224 SkAutoTUnref<GrTexture> dummyTexture1(this->createTexture(dummyDesc, NULL, 0));
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000225 dummyDesc.fFlags = kNone_GrTextureFlags;
bsalomon@google.comd4726202012-08-03 14:34:46 +0000226 dummyDesc.fConfig = kAlpha_8_GrPixelConfig;
227 dummyDesc.fWidth = 16;
228 dummyDesc.fHeight = 22;
229 SkAutoTUnref<GrTexture> dummyTexture2(this->createTexture(dummyDesc, NULL, 0));
230
bsalomone904c092014-07-17 10:50:59 -0700231 if (!dummyTexture1 || ! dummyTexture2) {
232 return false;
233 }
234
joshualittd9097592014-10-07 08:37:36 -0700235 static const int NUM_TESTS = 512;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000236
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000237 SkRandom random;
joshualittd9097592014-10-07 08:37:36 -0700238 for (int t = 0; t < NUM_TESTS; ++t) {
239
240#if 0
241 GrPrintf("\nTest Program %d\n-------------\n", t);
242 static const int stop = -1;
243 if (t == stop) {
244 int breakpointhere = 9;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000245 }
joshualittd9097592014-10-07 08:37:36 -0700246#endif
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000247
joshualittd9097592014-10-07 08:37:36 -0700248 GrGLProgramDesc pdesc;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000249
joshualittd9097592014-10-07 08:37:36 -0700250 int currAttribIndex = 1; // we need to always leave room for position
251 int currTextureCoordSet = 0;
252 GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()};
253
254 int numStages = random.nextULessThan(maxStages + 1);
255 int numColorStages = random.nextULessThan(numStages + 1);
256 int numCoverageStages = numStages - numColorStages;
257
258 SkAutoSTMalloc<8, const GrFragmentStage*> stages(numStages);
259
egdanielae444962014-09-22 12:29:52 -0700260 bool usePathRendering = this->glCaps().pathRenderingSupport() && random.nextBool();
egdanielc0648242014-09-22 13:17:02 -0700261
egdanielae444962014-09-22 12:29:52 -0700262 GrGpu::DrawType drawType = usePathRendering ? GrGpu::kDrawPath_DrawType :
263 GrGpu::kDrawPoints_DrawType;
commit-bot@chromium.org8e919ad2013-10-21 14:48:23 +0000264
joshualittd9097592014-10-07 08:37:36 -0700265 SkAutoTDelete<GrGeometryStage> geometryProcessor;
egdanielae444962014-09-22 12:29:52 -0700266 bool hasGeometryProcessor = usePathRendering ? false : random.nextBool();
joshualittbd769d02014-09-04 08:56:46 -0700267 if (hasGeometryProcessor) {
joshualittd9097592014-10-07 08:37:36 -0700268 while (true) {
269 SkAutoTUnref<const GrGeometryProcessor> effect(
270 GrProcessorTestFactory<GrGeometryProcessor>::CreateStage(&random, this->getContext(), *this->caps(),
271 dummyTextures));
272 SkASSERT(effect);
273 // Only geometryProcessor can use vertex shader
274 GrGeometryStage* stage = SkNEW_ARGS(GrGeometryStage, (effect.get()));
275 geometryProcessor.reset(stage);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000276
joshualittd9097592014-10-07 08:37:36 -0700277 // we have to set dummy vertex attribs
278 const GrGeometryProcessor::VertexAttribArray& v = effect->getVertexAttribs();
279 int numVertexAttribs = v.count();
joshualitt07a25532014-10-07 08:05:21 -0700280
joshualittd9097592014-10-07 08:37:36 -0700281 SkASSERT(GrGeometryProcessor::kMaxVertexAttribs == 2 &&
282 GrGeometryProcessor::kMaxVertexAttribs >= numVertexAttribs);
283 size_t runningStride = GrVertexAttribTypeSize(genericVertexAttribs[0].fType);
284 for (int i = 0; i < numVertexAttribs; i++) {
285 genericVertexAttribs[i + 1].fOffset = runningStride;
286 genericVertexAttribs[i + 1].fType =
287 convert_sltype_to_attribtype(v[i].getType());
288 runningStride += GrVertexAttribTypeSize(genericVertexAttribs[i + 1].fType);
289 }
290
291 // update the vertex attributes with the ds
292 GrDrawState* ds = this->drawState();
293 ds->setVertexAttribs<genericVertexAttribs>(numVertexAttribs + 1, runningStride);
294 currAttribIndex = numVertexAttribs + 1;
295 break;
296 }
297 }
298 for (int s = 0; s < numStages;) {
299 SkAutoTUnref<const GrFragmentProcessor> effect(
300 GrProcessorTestFactory<GrFragmentProcessor>::CreateStage(
301 &random,
302 this->getContext(),
303 *this->caps(),
304 dummyTextures));
305 SkASSERT(effect);
306
307 // If adding this effect would exceed the max texture coord set count then generate a
308 // new random effect.
309 if (usePathRendering && this->glPathRendering()->texturingMode() ==
310 GrGLPathRendering::FixedFunction_TexturingMode) {;
311 int numTransforms = effect->numTransforms();
312 if (currTextureCoordSet + numTransforms > this->glCaps().maxFixedFunctionTextureCoords()) {
313 continue;
314 }
315 currTextureCoordSet += numTransforms;
316 }
317 GrFragmentStage* stage = SkNEW_ARGS(GrFragmentStage, (effect.get()));
318
319 stages[s] = stage;
320 ++s;
321 }
322 const GrTexture* dstTexture = random.nextBool() ? dummyTextures[0] : dummyTextures[1];
323 if (!pdesc.setRandom(&random,
324 this,
325 dummyTextures[0]->asRenderTarget(),
326 dstTexture,
327 geometryProcessor.get(),
328 stages.get(),
329 numColorStages,
330 numCoverageStages,
331 currAttribIndex,
332 drawType)) {
bsalomon848faf02014-07-11 10:01:02 -0700333 return false;
334 }
joshualittd9097592014-10-07 08:37:36 -0700335
336 SkAutoTUnref<GrOptDrawState> optState(GrOptDrawState::Create(this->getDrawState(),
337 *this->caps(),
338 drawType));
joshualittfe1233c2014-10-07 12:16:35 -0700339 SkAutoTUnref<GrGLProgram> program(
340 GrGLProgramBuilder::CreateProgram(*optState,
341 pdesc,
342 drawType,
343 geometryProcessor,
344 stages,
345 stages + numColorStages,
346 this));
joshualittd9097592014-10-07 08:37:36 -0700347 for (int s = 0; s < numStages; ++s) {
348 SkDELETE(stages[s]);
349 }
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000350 if (NULL == program.get()) {
351 return false;
352 }
joshualitt249af152014-09-15 11:41:13 -0700353
354 // We have to reset the drawstate because we might have added a gp
joshualittd9097592014-10-07 08:37:36 -0700355 this->drawState()->reset();
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000356 }
357 return true;
358}
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000359
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000360DEF_GPUTEST(GLPrograms, reporter, factory) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000361 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
362 GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(type));
bsalomon49f085d2014-09-05 13:34:00 -0700363 if (context) {
bsalomon@google.com042a2862013-02-04 18:39:24 +0000364 GrGpuGL* gpu = static_cast<GrGpuGL*>(context->getGpu());
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000365 int maxStages = 6;
bsalomon@google.com042a2862013-02-04 18:39:24 +0000366#if SK_ANGLE
367 // Some long shaders run out of temporary registers in the D3D compiler on ANGLE.
368 if (type == GrContextFactory::kANGLE_GLContextType) {
369 maxStages = 3;
370 }
371#endif
372 REPORTER_ASSERT(reporter, gpu->programUnitTest(maxStages));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000373 }
374 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000375}
376
rmistry@google.comd6176b02012-08-23 18:14:13 +0000377// This is evil evil evil. The linker may throw away whole translation units as dead code if it
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000378// thinks none of the functions are called. It will do this even if there are static initializers
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000379// in the unit that could pass pointers to functions from the unit out to other translation units!
380// We force some of the effects that would otherwise be discarded to link here.
381
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000382#include "SkAlphaThresholdFilter.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +0000383#include "SkColorMatrixFilter.h"
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000384#include "SkLightingImageFilter.h"
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000385#include "SkMagnifierImageFilter.h"
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000386
387void forceLinking();
388
389void forceLinking() {
390 SkLightingImageFilter::CreateDistantLitDiffuse(SkPoint3(0,0,0), 0, 0, 0);
commit-bot@chromium.org9109e182014-01-07 16:04:01 +0000391 SkAlphaThresholdFilter::Create(SkRegion(), .5f, .5f);
reed9fa60da2014-08-21 07:59:51 -0700392 SkAutoTUnref<SkImageFilter> mag(SkMagnifierImageFilter::Create(
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000393 SkRect::MakeWH(SK_Scalar1, SK_Scalar1), SK_Scalar1));
bsalomon@google.comadc65362013-01-28 14:26:09 +0000394 GrConfigConversionEffect::Create(NULL,
395 false,
396 GrConfigConversionEffect::kNone_PMConversion,
397 SkMatrix::I());
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000398 SkScalar matrix[20];
commit-bot@chromium.org727a3522014-02-21 18:46:30 +0000399 SkAutoTUnref<SkColorMatrixFilter> cmf(SkColorMatrixFilter::Create(matrix));
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000400}
401
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000402#endif