blob: 9da1ecc53c0a798fdb9762c1b7376bdc7c088e2d [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
bsalomon929f29a2014-07-17 07:55:11 -070038 GR_STATIC_ASSERT(0 == kEffectKeyOffsetsAndLengthOffset % 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.
bsalomon929f29a2014-07-17 07:55:11 -070041 fKey.push_back_n(kEffectKeyOffsetsAndLengthOffset + 2 * sizeof(uint16_t) * numStages);
bsalomon848faf02014-07-11 10:01:02 -070042
43 bool dstRead = false;
44 bool fragPos = false;
45 bool vertexCode = false;
46 for (int s = 0; s < numStages; ++s) {
bsalomon929f29a2014-07-17 07:55:11 -070047 uint16_t* offsetAndSize = reinterpret_cast<uint16_t*>(fKey.begin() +
48 kEffectKeyOffsetsAndLengthOffset +
49 s * 2 * sizeof(uint16_t));
50 uint32_t effectKeyOffset = fKey.count();
51 if (effectKeyOffset > SK_MaxU16) {
bsalomonc0ea3982014-07-15 19:41:17 -070052 fKey.reset();
53 return false;
bsalomon848faf02014-07-11 10:01:02 -070054 }
bsalomon929f29a2014-07-17 07:55:11 -070055 GrDrawEffect drawEffect(*stages[s], useLocalCoords);
56 GrEffectKeyBuilder b(&fKey);
57 uint16_t effectKeySize;
58 if (!GetEffectKeyAndUpdateStats(*stages[s], gpu->glCaps(), useLocalCoords, &b,
59 &effectKeySize, &dstRead, &fragPos, &vertexCode)) {
60 fKey.reset();
61 return false;
mtklein79401002014-07-16 06:16:43 -070062 }
bsalomon929f29a2014-07-17 07:55:11 -070063 offsetAndSize[0] = effectKeyOffset;
64 offsetAndSize[1] = effectKeySize;
bsalomon848faf02014-07-11 10:01:02 -070065 }
jvanverth@google.com054ae992013-04-01 20:06:51 +000066
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000067 KeyHeader* header = this->header();
bsalomon848faf02014-07-11 10:01:02 -070068 memset(header, 0, kHeaderSize);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000069 header->fEmitsPointSize = random->nextBool();
70
71 header->fPositionAttributeIndex = 0;
jvanverth@google.com054ae992013-04-01 20:06:51 +000072
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +000073 // if the effects have used up all off the available attributes,
jvanverth@google.com054ae992013-04-01 20:06:51 +000074 // don't try to use color or coverage attributes as input
75 do {
egdaniel02cafcc2014-07-21 11:37:28 -070076 uint32_t colorRand = random->nextULessThan(2);
77 header->fColorInput = (0 == colorRand) ? GrGLProgramDesc::kAttribute_ColorInput :
78 GrGLProgramDesc::kUniform_ColorInput;
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +000079 } while (GrDrawState::kMaxVertexAttribCnt <= currAttribIndex &&
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000080 kAttribute_ColorInput == header->fColorInput);
bsalomon848faf02014-07-11 10:01:02 -070081
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000082 header->fColorAttributeIndex = (header->fColorInput == kAttribute_ColorInput) ?
83 currAttribIndex++ :
84 -1;
jvanverth@google.com054ae992013-04-01 20:06:51 +000085
86 do {
commit-bot@chromium.org949eef02013-10-01 18:43:29 +000087 header->fCoverageInput = static_cast<GrGLProgramDesc::ColorInput>(
88 random->nextULessThan(kColorInputCnt));
jvanverth@google.com054ae992013-04-01 20:06:51 +000089 } while (GrDrawState::kMaxVertexAttribCnt <= currAttribIndex &&
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000090 kAttribute_ColorInput == header->fCoverageInput);
91 header->fCoverageAttributeIndex = (header->fCoverageInput == kAttribute_ColorInput) ?
92 currAttribIndex++ :
93 -1;
bsalomon@google.com91207482013-02-12 21:45:24 +000094
bsalomon@google.com91207482013-02-12 21:45:24 +000095#if GR_GL_EXPERIMENTAL_GS
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000096 header->fExperimentalGS = gpu->caps()->geometryShaderSupport() && random->nextBool();
bsalomon@google.com91207482013-02-12 21:45:24 +000097#endif
98
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000099 header->fLocalCoordAttributeIndex = useLocalCoords ? currAttribIndex++ : -1;
100
101 header->fColorEffectCnt = numColorStages;
102 header->fCoverageEffectCnt = numCoverageStages;
bsalomon@google.comc7818882013-03-20 19:19:53 +0000103
bsalomon@google.comb79d8652013-03-29 20:30:50 +0000104 if (dstRead) {
bsalomon848faf02014-07-11 10:01:02 -0700105 header->fDstReadKey = SkToU8(GrGLShaderBuilder::KeyForDstRead(dstCopyTexture,
106 gpu->glCaps()));
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000107 } else {
108 header->fDstReadKey = 0;
109 }
110 if (fragPos) {
bsalomon848faf02014-07-11 10:01:02 -0700111 header->fFragPosKey = SkToU8(GrGLShaderBuilder::KeyForFragmentPosition(dstRenderTarget,
112 gpu->glCaps()));
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000113 } else {
114 header->fFragPosKey = 0;
bsalomon@google.comb79d8652013-03-29 20:30:50 +0000115 }
bsalomon@google.com5920ac22013-04-19 13:14:45 +0000116
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000117 header->fHasVertexCode = vertexCode ||
118 useLocalCoords ||
119 kAttribute_ColorInput == header->fColorInput ||
120 kAttribute_ColorInput == header->fCoverageInput;
121
bsalomon@google.com5920ac22013-04-19 13:14:45 +0000122 CoverageOutput coverageOutput;
123 bool illegalCoverageOutput;
124 do {
125 coverageOutput = static_cast<CoverageOutput>(random->nextULessThan(kCoverageOutputCnt));
126 illegalCoverageOutput = (!gpu->caps()->dualSourceBlendingSupport() &&
127 CoverageOutputUsesSecondaryOutput(coverageOutput)) ||
bsalomon@google.com72993ab2013-04-19 13:25:28 +0000128 (!dstRead && kCombineWithDst_CoverageOutput == coverageOutput);
bsalomon@google.com5920ac22013-04-19 13:14:45 +0000129 } while (illegalCoverageOutput);
130
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000131 header->fCoverageOutput = coverageOutput;
132
bsalomon848faf02014-07-11 10:01:02 -0700133 this->finalize();
134 return true;
bsalomon@google.com91207482013-02-12 21:45:24 +0000135}
136
bsalomon@google.com042a2862013-02-04 18:39:24 +0000137bool GrGpuGL::programUnitTest(int maxStages) {
138
bsalomon@google.comd4726202012-08-03 14:34:46 +0000139 GrTextureDesc dummyDesc;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000140 dummyDesc.fFlags = kRenderTarget_GrTextureFlagBit;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000141 dummyDesc.fConfig = kSkia8888_GrPixelConfig;
bsalomon@google.comd4726202012-08-03 14:34:46 +0000142 dummyDesc.fWidth = 34;
143 dummyDesc.fHeight = 18;
144 SkAutoTUnref<GrTexture> dummyTexture1(this->createTexture(dummyDesc, NULL, 0));
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000145 dummyDesc.fFlags = kNone_GrTextureFlags;
bsalomon@google.comd4726202012-08-03 14:34:46 +0000146 dummyDesc.fConfig = kAlpha_8_GrPixelConfig;
147 dummyDesc.fWidth = 16;
148 dummyDesc.fHeight = 22;
149 SkAutoTUnref<GrTexture> dummyTexture2(this->createTexture(dummyDesc, NULL, 0));
150
bsalomone904c092014-07-17 10:50:59 -0700151 if (!dummyTexture1 || ! dummyTexture2) {
152 return false;
153 }
154
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000155 static const int NUM_TESTS = 512;
156
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000157 SkRandom random;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000158 for (int t = 0; t < NUM_TESTS; ++t) {
159
160#if 0
161 GrPrintf("\nTest Program %d\n-------------\n", t);
162 static const int stop = -1;
163 if (t == stop) {
164 int breakpointhere = 9;
165 }
166#endif
167
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000168 GrGLProgramDesc pdesc;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000169
jvanverth@google.com054ae992013-04-01 20:06:51 +0000170 int currAttribIndex = 1; // we need to always leave room for position
commit-bot@chromium.org8e919ad2013-10-21 14:48:23 +0000171 int currTextureCoordSet = 0;
commit-bot@chromium.org4324c3b2014-02-21 20:11:42 +0000172 int attribIndices[2] = { 0, 0 };
bsalomon@google.comb79d8652013-03-29 20:30:50 +0000173 GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()};
commit-bot@chromium.org9ae78502013-03-21 17:44:39 +0000174
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000175 int numStages = random.nextULessThan(maxStages + 1);
176 int numColorStages = random.nextULessThan(numStages + 1);
177 int numCoverageStages = numStages - numColorStages;
178
179 SkAutoSTMalloc<8, const GrEffectStage*> stages(numStages);
180
commit-bot@chromium.org8e919ad2013-10-21 14:48:23 +0000181 bool useFixedFunctionTexturing = this->shouldUseFixedFunctionTexturing();
182
183 for (int s = 0; s < numStages;) {
bsalomon83d081a2014-07-08 09:56:10 -0700184 SkAutoTUnref<const GrEffect> effect(GrEffectTestFactory::CreateStage(
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000185 &random,
186 this->getContext(),
187 *this->caps(),
188 dummyTextures));
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000189 SkASSERT(effect);
bsalomon97b9ab72014-07-08 06:52:35 -0700190 int numAttribs = effect->numVertexAttribs();
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000191
192 // If adding this effect would exceed the max attrib count then generate a
193 // new random effect.
194 if (currAttribIndex + numAttribs > GrDrawState::kMaxVertexAttribCnt) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000195 continue;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000196 }
commit-bot@chromium.org8e919ad2013-10-21 14:48:23 +0000197
198
199 // If adding this effect would exceed the max texture coord set count then generate a
200 // new random effect.
bsalomon97b9ab72014-07-08 06:52:35 -0700201 if (useFixedFunctionTexturing && !effect->hasVertexCode()) {
202 int numTransforms = effect->numTransforms();
commit-bot@chromium.org8e919ad2013-10-21 14:48:23 +0000203 if (currTextureCoordSet + numTransforms > this->glCaps().maxFixedFunctionTextureCoords()) {
204 continue;
205 }
206 currTextureCoordSet += numTransforms;
207 }
208
bsalomon97b9ab72014-07-08 06:52:35 -0700209 useFixedFunctionTexturing = useFixedFunctionTexturing && !effect->hasVertexCode();
commit-bot@chromium.org8e919ad2013-10-21 14:48:23 +0000210
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000211 for (int i = 0; i < numAttribs; ++i) {
212 attribIndices[i] = currAttribIndex++;
213 }
commit-bot@chromium.org2d3b4922013-07-15 13:54:06 +0000214 GrEffectStage* stage = SkNEW_ARGS(GrEffectStage,
215 (effect.get(), attribIndices[0], attribIndices[1]));
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000216 stages[s] = stage;
commit-bot@chromium.org8e919ad2013-10-21 14:48:23 +0000217 ++s;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000218 }
bsalomon@google.comb79d8652013-03-29 20:30:50 +0000219 const GrTexture* dstTexture = random.nextBool() ? dummyTextures[0] : dummyTextures[1];
bsalomon848faf02014-07-11 10:01:02 -0700220 if (!pdesc.setRandom(&random,
221 this,
222 dummyTextures[0]->asRenderTarget(),
223 dstTexture,
224 stages.get(),
225 numColorStages,
226 numCoverageStages,
227 currAttribIndex)) {
228 return false;
229 }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000230
commit-bot@chromium.org9188a152013-09-05 18:28:24 +0000231 SkAutoTUnref<GrGLProgram> program(GrGLProgram::Create(this,
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000232 pdesc,
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000233 stages,
234 stages + numColorStages));
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000235 for (int s = 0; s < numStages; ++s) {
bsalomon@google.com504976e2013-05-09 13:45:02 +0000236 SkDELETE(stages[s]);
237 }
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000238 if (NULL == program.get()) {
239 return false;
240 }
241 }
242 return true;
243}
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000244
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000245DEF_GPUTEST(GLPrograms, reporter, factory) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000246 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
247 GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(type));
248 if (NULL != context) {
bsalomon@google.com042a2862013-02-04 18:39:24 +0000249 GrGpuGL* gpu = static_cast<GrGpuGL*>(context->getGpu());
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000250 int maxStages = 6;
bsalomon@google.com042a2862013-02-04 18:39:24 +0000251#if SK_ANGLE
252 // Some long shaders run out of temporary registers in the D3D compiler on ANGLE.
253 if (type == GrContextFactory::kANGLE_GLContextType) {
254 maxStages = 3;
255 }
256#endif
257 REPORTER_ASSERT(reporter, gpu->programUnitTest(maxStages));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000258 }
259 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000260}
261
rmistry@google.comd6176b02012-08-23 18:14:13 +0000262// 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 +0000263// 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 +0000264// in the unit that could pass pointers to functions from the unit out to other translation units!
265// We force some of the effects that would otherwise be discarded to link here.
266
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000267#include "SkAlphaThresholdFilter.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +0000268#include "SkColorMatrixFilter.h"
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000269#include "SkLightingImageFilter.h"
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000270#include "SkMagnifierImageFilter.h"
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000271
272void forceLinking();
273
274void forceLinking() {
275 SkLightingImageFilter::CreateDistantLitDiffuse(SkPoint3(0,0,0), 0, 0, 0);
commit-bot@chromium.org9109e182014-01-07 16:04:01 +0000276 SkAlphaThresholdFilter::Create(SkRegion(), .5f, .5f);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000277 SkAutoTUnref<SkMagnifierImageFilter> mag(SkMagnifierImageFilter::Create(
278 SkRect::MakeWH(SK_Scalar1, SK_Scalar1), SK_Scalar1));
bsalomon@google.comadc65362013-01-28 14:26:09 +0000279 GrConfigConversionEffect::Create(NULL,
280 false,
281 GrConfigConversionEffect::kNone_PMConversion,
282 SkMatrix::I());
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000283 SkScalar matrix[20];
commit-bot@chromium.org727a3522014-02-21 18:46:30 +0000284 SkAutoTUnref<SkColorMatrixFilter> cmf(SkColorMatrixFilter::Create(matrix));
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000285}
286
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000287#endif