blob: dd0f80f42f0815aa8b7c7c91c5c9da01ced9cbda [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
bsalomon@google.com396e61f2012-10-25 19:00:29 +000015#include "GrBackendEffectFactory.h"
bsalomon@google.com67b915d2013-02-04 16:13:32 +000016#include "GrContextFactory.h"
bsalomon@google.comc7818882013-03-20 19:19:53 +000017#include "GrDrawEffect.h"
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000018#include "effects/GrConfigConversionEffect.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000019#include "gl/GrGpuGL.h"
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +000020
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000021#include "SkChecksum.h"
tfarina@chromium.org223137f2012-11-21 22:38:36 +000022#include "SkRandom.h"
bsalomon@google.comc3841b92012-08-02 18:11:43 +000023#include "Test.h"
24
bsalomon848faf02014-07-11 10:01:02 -070025bool GrGLProgramDesc::setRandom(SkRandom* random,
bsalomon@google.com31ec7982013-03-27 18:14:57 +000026 const GrGpuGL* gpu,
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000027 const GrRenderTarget* dstRenderTarget,
28 const GrTexture* dstCopyTexture,
29 const GrEffectStage* stages[],
30 int numColorStages,
31 int numCoverageStages,
jvanverth@google.com054ae992013-04-01 20:06:51 +000032 int currAttribIndex) {
bsalomon848faf02014-07-11 10:01:02 -070033 bool useLocalCoords = random->nextBool() && currAttribIndex < GrDrawState::kMaxVertexAttribCnt;
34
35 int numStages = numColorStages + numCoverageStages;
36 fKey.reset();
37
mtklein79401002014-07-16 06:16:43 -070038 GR_STATIC_ASSERT(0 == kEffectKeyLengthsOffset % sizeof(uint32_t));
bsalomon848faf02014-07-11 10:01:02 -070039
40 // Make room for everything up to and including the array of offsets to effect keys.
mtklein79401002014-07-16 06:16:43 -070041 fKey.push_back_n(kEffectKeyLengthsOffset + sizeof(uint32_t) * numStages);
42
43 size_t offset = fKey.count();
44 int offsetIndex = 0;
bsalomon848faf02014-07-11 10:01:02 -070045
46 bool dstRead = false;
47 bool fragPos = false;
48 bool vertexCode = false;
49 for (int s = 0; s < numStages; ++s) {
mtklein79401002014-07-16 06:16:43 -070050 uint32_t* offsetLocation = reinterpret_cast<uint32_t*>(fKey.begin() +
51 kEffectKeyLengthsOffset +
52 offsetIndex * sizeof(uint32_t));
53 *offsetLocation = offset;
54 ++offsetIndex;
55
56 const GrBackendEffectFactory& factory = stages[s]->getEffect()->getFactory();
bsalomonc0ea3982014-07-15 19:41:17 -070057 GrDrawEffect drawEffect(*stages[s], useLocalCoords);
58 GrEffectKeyBuilder b(&fKey);
mtklein79401002014-07-16 06:16:43 -070059 if (!factory.getGLEffectKey(drawEffect, gpu->glCaps(), &b)) {
bsalomonc0ea3982014-07-15 19:41:17 -070060 fKey.reset();
61 return false;
bsalomon848faf02014-07-11 10:01:02 -070062 }
mtklein79401002014-07-16 06:16:43 -070063 if (stages[s]->getEffect()->willReadDstColor()) {
64 dstRead = true;
65 }
66 if (stages[s]->getEffect()->willReadFragmentPosition()) {
67 fragPos = true;
68 }
69 if (stages[s]->getEffect()->hasVertexCode()) {
70 vertexCode = true;
71 }
72
73 offset += b.size();
bsalomon848faf02014-07-11 10:01:02 -070074 }
jvanverth@google.com054ae992013-04-01 20:06:51 +000075
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000076 KeyHeader* header = this->header();
bsalomon848faf02014-07-11 10:01:02 -070077 memset(header, 0, kHeaderSize);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000078 header->fEmitsPointSize = random->nextBool();
79
80 header->fPositionAttributeIndex = 0;
jvanverth@google.com054ae992013-04-01 20:06:51 +000081
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +000082 // if the effects have used up all off the available attributes,
jvanverth@google.com054ae992013-04-01 20:06:51 +000083 // don't try to use color or coverage attributes as input
84 do {
commit-bot@chromium.org949eef02013-10-01 18:43:29 +000085 header->fColorInput = static_cast<GrGLProgramDesc::ColorInput>(
86 random->nextULessThan(kColorInputCnt));
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +000087 } while (GrDrawState::kMaxVertexAttribCnt <= currAttribIndex &&
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000088 kAttribute_ColorInput == header->fColorInput);
bsalomon848faf02014-07-11 10:01:02 -070089
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000090 header->fColorAttributeIndex = (header->fColorInput == kAttribute_ColorInput) ?
91 currAttribIndex++ :
92 -1;
jvanverth@google.com054ae992013-04-01 20:06:51 +000093
94 do {
commit-bot@chromium.org949eef02013-10-01 18:43:29 +000095 header->fCoverageInput = static_cast<GrGLProgramDesc::ColorInput>(
96 random->nextULessThan(kColorInputCnt));
jvanverth@google.com054ae992013-04-01 20:06:51 +000097 } while (GrDrawState::kMaxVertexAttribCnt <= currAttribIndex &&
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000098 kAttribute_ColorInput == header->fCoverageInput);
99 header->fCoverageAttributeIndex = (header->fCoverageInput == kAttribute_ColorInput) ?
100 currAttribIndex++ :
101 -1;
bsalomon@google.com91207482013-02-12 21:45:24 +0000102
bsalomon@google.com91207482013-02-12 21:45:24 +0000103#if GR_GL_EXPERIMENTAL_GS
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000104 header->fExperimentalGS = gpu->caps()->geometryShaderSupport() && random->nextBool();
bsalomon@google.com91207482013-02-12 21:45:24 +0000105#endif
106
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000107 header->fLocalCoordAttributeIndex = useLocalCoords ? currAttribIndex++ : -1;
108
109 header->fColorEffectCnt = numColorStages;
110 header->fCoverageEffectCnt = numCoverageStages;
bsalomon@google.comc7818882013-03-20 19:19:53 +0000111
bsalomon@google.comb79d8652013-03-29 20:30:50 +0000112 if (dstRead) {
bsalomon848faf02014-07-11 10:01:02 -0700113 header->fDstReadKey = SkToU8(GrGLShaderBuilder::KeyForDstRead(dstCopyTexture,
114 gpu->glCaps()));
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000115 } else {
116 header->fDstReadKey = 0;
117 }
118 if (fragPos) {
bsalomon848faf02014-07-11 10:01:02 -0700119 header->fFragPosKey = SkToU8(GrGLShaderBuilder::KeyForFragmentPosition(dstRenderTarget,
120 gpu->glCaps()));
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000121 } else {
122 header->fFragPosKey = 0;
bsalomon@google.comb79d8652013-03-29 20:30:50 +0000123 }
bsalomon@google.com5920ac22013-04-19 13:14:45 +0000124
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000125 header->fHasVertexCode = vertexCode ||
126 useLocalCoords ||
127 kAttribute_ColorInput == header->fColorInput ||
128 kAttribute_ColorInput == header->fCoverageInput;
129
bsalomon@google.com5920ac22013-04-19 13:14:45 +0000130 CoverageOutput coverageOutput;
131 bool illegalCoverageOutput;
132 do {
133 coverageOutput = static_cast<CoverageOutput>(random->nextULessThan(kCoverageOutputCnt));
134 illegalCoverageOutput = (!gpu->caps()->dualSourceBlendingSupport() &&
135 CoverageOutputUsesSecondaryOutput(coverageOutput)) ||
bsalomon@google.com72993ab2013-04-19 13:25:28 +0000136 (!dstRead && kCombineWithDst_CoverageOutput == coverageOutput);
bsalomon@google.com5920ac22013-04-19 13:14:45 +0000137 } while (illegalCoverageOutput);
138
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000139 header->fCoverageOutput = coverageOutput;
140
bsalomon848faf02014-07-11 10:01:02 -0700141 this->finalize();
142 return true;
bsalomon@google.com91207482013-02-12 21:45:24 +0000143}
144
bsalomon@google.com042a2862013-02-04 18:39:24 +0000145bool GrGpuGL::programUnitTest(int maxStages) {
146
bsalomon@google.comd4726202012-08-03 14:34:46 +0000147 GrTextureDesc dummyDesc;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000148 dummyDesc.fFlags = kRenderTarget_GrTextureFlagBit;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000149 dummyDesc.fConfig = kSkia8888_GrPixelConfig;
bsalomon@google.comd4726202012-08-03 14:34:46 +0000150 dummyDesc.fWidth = 34;
151 dummyDesc.fHeight = 18;
152 SkAutoTUnref<GrTexture> dummyTexture1(this->createTexture(dummyDesc, NULL, 0));
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000153 dummyDesc.fFlags = kNone_GrTextureFlags;
bsalomon@google.comd4726202012-08-03 14:34:46 +0000154 dummyDesc.fConfig = kAlpha_8_GrPixelConfig;
155 dummyDesc.fWidth = 16;
156 dummyDesc.fHeight = 22;
157 SkAutoTUnref<GrTexture> dummyTexture2(this->createTexture(dummyDesc, NULL, 0));
158
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000159 static const int NUM_TESTS = 512;
160
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000161 SkRandom random;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000162 for (int t = 0; t < NUM_TESTS; ++t) {
163
164#if 0
165 GrPrintf("\nTest Program %d\n-------------\n", t);
166 static const int stop = -1;
167 if (t == stop) {
168 int breakpointhere = 9;
169 }
170#endif
171
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000172 GrGLProgramDesc pdesc;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000173
jvanverth@google.com054ae992013-04-01 20:06:51 +0000174 int currAttribIndex = 1; // we need to always leave room for position
commit-bot@chromium.org8e919ad2013-10-21 14:48:23 +0000175 int currTextureCoordSet = 0;
commit-bot@chromium.org4324c3b2014-02-21 20:11:42 +0000176 int attribIndices[2] = { 0, 0 };
bsalomon@google.comb79d8652013-03-29 20:30:50 +0000177 GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()};
commit-bot@chromium.org9ae78502013-03-21 17:44:39 +0000178
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000179 int numStages = random.nextULessThan(maxStages + 1);
180 int numColorStages = random.nextULessThan(numStages + 1);
181 int numCoverageStages = numStages - numColorStages;
182
183 SkAutoSTMalloc<8, const GrEffectStage*> stages(numStages);
184
commit-bot@chromium.org8e919ad2013-10-21 14:48:23 +0000185 bool useFixedFunctionTexturing = this->shouldUseFixedFunctionTexturing();
186
187 for (int s = 0; s < numStages;) {
bsalomon83d081a2014-07-08 09:56:10 -0700188 SkAutoTUnref<const GrEffect> effect(GrEffectTestFactory::CreateStage(
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000189 &random,
190 this->getContext(),
191 *this->caps(),
192 dummyTextures));
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000193 SkASSERT(effect);
bsalomon97b9ab72014-07-08 06:52:35 -0700194 int numAttribs = effect->numVertexAttribs();
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000195
196 // If adding this effect would exceed the max attrib count then generate a
197 // new random effect.
198 if (currAttribIndex + numAttribs > GrDrawState::kMaxVertexAttribCnt) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000199 continue;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000200 }
commit-bot@chromium.org8e919ad2013-10-21 14:48:23 +0000201
202
203 // If adding this effect would exceed the max texture coord set count then generate a
204 // new random effect.
bsalomon97b9ab72014-07-08 06:52:35 -0700205 if (useFixedFunctionTexturing && !effect->hasVertexCode()) {
206 int numTransforms = effect->numTransforms();
commit-bot@chromium.org8e919ad2013-10-21 14:48:23 +0000207 if (currTextureCoordSet + numTransforms > this->glCaps().maxFixedFunctionTextureCoords()) {
208 continue;
209 }
210 currTextureCoordSet += numTransforms;
211 }
212
bsalomon97b9ab72014-07-08 06:52:35 -0700213 useFixedFunctionTexturing = useFixedFunctionTexturing && !effect->hasVertexCode();
commit-bot@chromium.org8e919ad2013-10-21 14:48:23 +0000214
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000215 for (int i = 0; i < numAttribs; ++i) {
216 attribIndices[i] = currAttribIndex++;
217 }
commit-bot@chromium.org2d3b4922013-07-15 13:54:06 +0000218 GrEffectStage* stage = SkNEW_ARGS(GrEffectStage,
219 (effect.get(), attribIndices[0], attribIndices[1]));
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000220 stages[s] = stage;
commit-bot@chromium.org8e919ad2013-10-21 14:48:23 +0000221 ++s;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000222 }
bsalomon@google.comb79d8652013-03-29 20:30:50 +0000223 const GrTexture* dstTexture = random.nextBool() ? dummyTextures[0] : dummyTextures[1];
bsalomon848faf02014-07-11 10:01:02 -0700224 if (!pdesc.setRandom(&random,
225 this,
226 dummyTextures[0]->asRenderTarget(),
227 dstTexture,
228 stages.get(),
229 numColorStages,
230 numCoverageStages,
231 currAttribIndex)) {
232 return false;
233 }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000234
commit-bot@chromium.org9188a152013-09-05 18:28:24 +0000235 SkAutoTUnref<GrGLProgram> program(GrGLProgram::Create(this,
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000236 pdesc,
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000237 stages,
238 stages + numColorStages));
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000239 for (int s = 0; s < numStages; ++s) {
bsalomon@google.com504976e2013-05-09 13:45:02 +0000240 SkDELETE(stages[s]);
241 }
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000242 if (NULL == program.get()) {
243 return false;
244 }
245 }
246 return true;
247}
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000248
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000249DEF_GPUTEST(GLPrograms, reporter, factory) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000250 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
251 GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(type));
252 if (NULL != context) {
bsalomon@google.com042a2862013-02-04 18:39:24 +0000253 GrGpuGL* gpu = static_cast<GrGpuGL*>(context->getGpu());
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000254 int maxStages = 6;
bsalomon@google.com042a2862013-02-04 18:39:24 +0000255#if SK_ANGLE
256 // Some long shaders run out of temporary registers in the D3D compiler on ANGLE.
257 if (type == GrContextFactory::kANGLE_GLContextType) {
258 maxStages = 3;
259 }
260#endif
261 REPORTER_ASSERT(reporter, gpu->programUnitTest(maxStages));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000262 }
263 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000264}
265
rmistry@google.comd6176b02012-08-23 18:14:13 +0000266// 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 +0000267// 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 +0000268// in the unit that could pass pointers to functions from the unit out to other translation units!
269// We force some of the effects that would otherwise be discarded to link here.
270
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000271#include "SkAlphaThresholdFilter.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +0000272#include "SkColorMatrixFilter.h"
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000273#include "SkLightingImageFilter.h"
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000274#include "SkMagnifierImageFilter.h"
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000275
276void forceLinking();
277
278void forceLinking() {
279 SkLightingImageFilter::CreateDistantLitDiffuse(SkPoint3(0,0,0), 0, 0, 0);
commit-bot@chromium.org9109e182014-01-07 16:04:01 +0000280 SkAlphaThresholdFilter::Create(SkRegion(), .5f, .5f);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000281 SkAutoTUnref<SkMagnifierImageFilter> mag(SkMagnifierImageFilter::Create(
282 SkRect::MakeWH(SK_Scalar1, SK_Scalar1), SK_Scalar1));
bsalomon@google.comadc65362013-01-28 14:26:09 +0000283 GrConfigConversionEffect::Create(NULL,
284 false,
285 GrConfigConversionEffect::kNone_PMConversion,
286 SkMatrix::I());
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000287 SkScalar matrix[20];
commit-bot@chromium.org727a3522014-02-21 18:46:30 +0000288 SkAutoTUnref<SkColorMatrixFilter> cmf(SkColorMatrixFilter::Create(matrix));
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000289}
290
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000291#endif