blob: 10d56fe12704f4e19c77eb176a9f9db46cba09c2 [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.com5739d2c2012-05-31 15:07:19 +000015#include "gl/GrGpuGL.h"
bsalomon@google.com396e61f2012-10-25 19:00:29 +000016#include "GrBackendEffectFactory.h"
bsalomon@google.com67b915d2013-02-04 16:13:32 +000017#include "GrContextFactory.h"
bsalomon@google.comc7818882013-03-20 19:19:53 +000018#include "GrDrawEffect.h"
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000019#include "effects/GrConfigConversionEffect.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"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000024#include "TestClassDef.h"
bsalomon@google.comc3841b92012-08-02 18:11:43 +000025
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000026void GrGLProgramDesc::setRandom(SkRandom* random,
bsalomon@google.com31ec7982013-03-27 18:14:57 +000027 const GrGpuGL* gpu,
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000028 const GrRenderTarget* dstRenderTarget,
29 const GrTexture* dstCopyTexture,
30 const GrEffectStage* stages[],
31 int numColorStages,
32 int numCoverageStages,
jvanverth@google.com054ae992013-04-01 20:06:51 +000033 int currAttribIndex) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000034 int numEffects = numColorStages + numCoverageStages;
35 size_t keyLength = KeyLength(numEffects);
36 fKey.reset(keyLength);
37 *this->atOffset<uint32_t, kLengthOffset>() = static_cast<uint32_t>(keyLength);
38 memset(this->header(), 0, kHeaderSize);
jvanverth@google.com054ae992013-04-01 20:06:51 +000039
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000040 KeyHeader* header = this->header();
41 header->fEmitsPointSize = random->nextBool();
42
43 header->fPositionAttributeIndex = 0;
jvanverth@google.com054ae992013-04-01 20:06:51 +000044
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +000045 // if the effects have used up all off the available attributes,
jvanverth@google.com054ae992013-04-01 20:06:51 +000046 // don't try to use color or coverage attributes as input
47 do {
commit-bot@chromium.org949eef02013-10-01 18:43:29 +000048 header->fColorInput = static_cast<GrGLProgramDesc::ColorInput>(
49 random->nextULessThan(kColorInputCnt));
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +000050 } while (GrDrawState::kMaxVertexAttribCnt <= currAttribIndex &&
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000051 kAttribute_ColorInput == header->fColorInput);
52 header->fColorAttributeIndex = (header->fColorInput == kAttribute_ColorInput) ?
53 currAttribIndex++ :
54 -1;
jvanverth@google.com054ae992013-04-01 20:06:51 +000055
56 do {
commit-bot@chromium.org949eef02013-10-01 18:43:29 +000057 header->fCoverageInput = static_cast<GrGLProgramDesc::ColorInput>(
58 random->nextULessThan(kColorInputCnt));
jvanverth@google.com054ae992013-04-01 20:06:51 +000059 } while (GrDrawState::kMaxVertexAttribCnt <= currAttribIndex &&
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000060 kAttribute_ColorInput == header->fCoverageInput);
61 header->fCoverageAttributeIndex = (header->fCoverageInput == kAttribute_ColorInput) ?
62 currAttribIndex++ :
63 -1;
bsalomon@google.com91207482013-02-12 21:45:24 +000064
bsalomon@google.com91207482013-02-12 21:45:24 +000065#if GR_GL_EXPERIMENTAL_GS
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000066 header->fExperimentalGS = gpu->caps()->geometryShaderSupport() && random->nextBool();
bsalomon@google.com91207482013-02-12 21:45:24 +000067#endif
68
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000069 header->fDiscardIfZeroCoverage = random->nextBool();
skia.committer@gmail.com37cbc7f2013-03-27 07:01:04 +000070
jvanverth@google.com054ae992013-04-01 20:06:51 +000071 bool useLocalCoords = random->nextBool() && currAttribIndex < GrDrawState::kMaxVertexAttribCnt;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000072 header->fLocalCoordAttributeIndex = useLocalCoords ? currAttribIndex++ : -1;
73
74 header->fColorEffectCnt = numColorStages;
75 header->fCoverageEffectCnt = numCoverageStages;
bsalomon@google.comc7818882013-03-20 19:19:53 +000076
bsalomon@google.comb79d8652013-03-29 20:30:50 +000077 bool dstRead = false;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000078 bool fragPos = false;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +000079 bool vertexCode = false;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000080 int numStages = numColorStages + numCoverageStages;
81 for (int s = 0; s < numStages; ++s) {
82 const GrBackendEffectFactory& factory = (*stages[s]->getEffect())->getFactory();
83 GrDrawEffect drawEffect(*stages[s], useLocalCoords);
84 this->effectKeys()[s] = factory.glEffectKey(drawEffect, gpu->glCaps());
85 if ((*stages[s]->getEffect())->willReadDstColor()) {
86 dstRead = true;
87 }
88 if ((*stages[s]->getEffect())->willReadFragmentPosition()) {
89 fragPos = true;
bsalomon@google.com91207482013-02-12 21:45:24 +000090 }
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +000091 if ((*stages[s]->getEffect())->hasVertexCode()) {
92 vertexCode = true;
93 }
bsalomon@google.com91207482013-02-12 21:45:24 +000094 }
jvanverth@google.com9b855c72013-03-01 18:21:22 +000095
bsalomon@google.comb79d8652013-03-29 20:30:50 +000096 if (dstRead) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000097 header->fDstReadKey = GrGLShaderBuilder::KeyForDstRead(dstCopyTexture, gpu->glCaps());
98 } else {
99 header->fDstReadKey = 0;
100 }
101 if (fragPos) {
102 header->fFragPosKey = GrGLShaderBuilder::KeyForFragmentPosition(dstRenderTarget,
103 gpu->glCaps());
104 } else {
105 header->fFragPosKey = 0;
bsalomon@google.comb79d8652013-03-29 20:30:50 +0000106 }
bsalomon@google.com5920ac22013-04-19 13:14:45 +0000107
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000108 header->fHasVertexCode = vertexCode ||
109 useLocalCoords ||
110 kAttribute_ColorInput == header->fColorInput ||
111 kAttribute_ColorInput == header->fCoverageInput;
112
bsalomon@google.com5920ac22013-04-19 13:14:45 +0000113 CoverageOutput coverageOutput;
114 bool illegalCoverageOutput;
115 do {
116 coverageOutput = static_cast<CoverageOutput>(random->nextULessThan(kCoverageOutputCnt));
117 illegalCoverageOutput = (!gpu->caps()->dualSourceBlendingSupport() &&
118 CoverageOutputUsesSecondaryOutput(coverageOutput)) ||
bsalomon@google.com72993ab2013-04-19 13:25:28 +0000119 (!dstRead && kCombineWithDst_CoverageOutput == coverageOutput);
bsalomon@google.com5920ac22013-04-19 13:14:45 +0000120 } while (illegalCoverageOutput);
121
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000122 header->fCoverageOutput = coverageOutput;
123
124 *this->checksum() = 0;
125 *this->checksum() = SkChecksum::Compute(reinterpret_cast<uint32_t*>(fKey.get()), keyLength);
126 fInitialized = true;
bsalomon@google.com91207482013-02-12 21:45:24 +0000127}
128
bsalomon@google.com042a2862013-02-04 18:39:24 +0000129bool GrGpuGL::programUnitTest(int maxStages) {
130
bsalomon@google.comd4726202012-08-03 14:34:46 +0000131 GrTextureDesc dummyDesc;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000132 dummyDesc.fFlags = kRenderTarget_GrTextureFlagBit;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000133 dummyDesc.fConfig = kSkia8888_GrPixelConfig;
bsalomon@google.comd4726202012-08-03 14:34:46 +0000134 dummyDesc.fWidth = 34;
135 dummyDesc.fHeight = 18;
136 SkAutoTUnref<GrTexture> dummyTexture1(this->createTexture(dummyDesc, NULL, 0));
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000137 dummyDesc.fFlags = kNone_GrTextureFlags;
bsalomon@google.comd4726202012-08-03 14:34:46 +0000138 dummyDesc.fConfig = kAlpha_8_GrPixelConfig;
139 dummyDesc.fWidth = 16;
140 dummyDesc.fHeight = 22;
141 SkAutoTUnref<GrTexture> dummyTexture2(this->createTexture(dummyDesc, NULL, 0));
142
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000143 static const int NUM_TESTS = 512;
144
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000145 SkRandom random;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000146 for (int t = 0; t < NUM_TESTS; ++t) {
147
148#if 0
149 GrPrintf("\nTest Program %d\n-------------\n", t);
150 static const int stop = -1;
151 if (t == stop) {
152 int breakpointhere = 9;
153 }
154#endif
155
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000156 GrGLProgramDesc pdesc;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000157
jvanverth@google.com054ae992013-04-01 20:06:51 +0000158 int currAttribIndex = 1; // we need to always leave room for position
commit-bot@chromium.org8e919ad2013-10-21 14:48:23 +0000159 int currTextureCoordSet = 0;
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000160 int attribIndices[2];
bsalomon@google.comb79d8652013-03-29 20:30:50 +0000161 GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()};
commit-bot@chromium.org9ae78502013-03-21 17:44:39 +0000162
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000163 int numStages = random.nextULessThan(maxStages + 1);
164 int numColorStages = random.nextULessThan(numStages + 1);
165 int numCoverageStages = numStages - numColorStages;
166
167 SkAutoSTMalloc<8, const GrEffectStage*> stages(numStages);
168
commit-bot@chromium.org8e919ad2013-10-21 14:48:23 +0000169 bool useFixedFunctionTexturing = this->shouldUseFixedFunctionTexturing();
170
171 for (int s = 0; s < numStages;) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000172 SkAutoTUnref<const GrEffectRef> effect(GrEffectTestFactory::CreateStage(
173 &random,
174 this->getContext(),
175 *this->caps(),
176 dummyTextures));
177 int numAttribs = (*effect)->numVertexAttribs();
178
179 // If adding this effect would exceed the max attrib count then generate a
180 // new random effect.
181 if (currAttribIndex + numAttribs > GrDrawState::kMaxVertexAttribCnt) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000182 continue;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000183 }
commit-bot@chromium.org8e919ad2013-10-21 14:48:23 +0000184
185
186 // If adding this effect would exceed the max texture coord set count then generate a
187 // new random effect.
188 if (useFixedFunctionTexturing && !(*effect)->hasVertexCode()) {
189 int numTransforms = (*effect)->numTransforms();
190 if (currTextureCoordSet + numTransforms > this->glCaps().maxFixedFunctionTextureCoords()) {
191 continue;
192 }
193 currTextureCoordSet += numTransforms;
194 }
195
196 useFixedFunctionTexturing = useFixedFunctionTexturing && !(*effect)->hasVertexCode();
197
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000198 for (int i = 0; i < numAttribs; ++i) {
199 attribIndices[i] = currAttribIndex++;
200 }
commit-bot@chromium.org2d3b4922013-07-15 13:54:06 +0000201 GrEffectStage* stage = SkNEW_ARGS(GrEffectStage,
202 (effect.get(), attribIndices[0], attribIndices[1]));
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000203 stages[s] = stage;
commit-bot@chromium.org8e919ad2013-10-21 14:48:23 +0000204 ++s;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000205 }
bsalomon@google.comb79d8652013-03-29 20:30:50 +0000206 const GrTexture* dstTexture = random.nextBool() ? dummyTextures[0] : dummyTextures[1];
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000207 pdesc.setRandom(&random,
208 this,
209 dummyTextures[0]->asRenderTarget(),
210 dstTexture,
211 stages.get(),
212 numColorStages,
213 numCoverageStages,
214 currAttribIndex);
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000215
commit-bot@chromium.org9188a152013-09-05 18:28:24 +0000216 SkAutoTUnref<GrGLProgram> program(GrGLProgram::Create(this,
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000217 pdesc,
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000218 stages,
219 stages + numColorStages));
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000220 for (int s = 0; s < numStages; ++s) {
bsalomon@google.com504976e2013-05-09 13:45:02 +0000221 SkDELETE(stages[s]);
222 }
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000223 if (NULL == program.get()) {
224 return false;
225 }
226 }
227 return true;
228}
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000229
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000230DEF_GPUTEST(GLPrograms, reporter, factory) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000231 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
232 GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(type));
233 if (NULL != context) {
bsalomon@google.com042a2862013-02-04 18:39:24 +0000234 GrGpuGL* gpu = static_cast<GrGpuGL*>(context->getGpu());
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000235 int maxStages = 6;
bsalomon@google.com042a2862013-02-04 18:39:24 +0000236#if SK_ANGLE
237 // Some long shaders run out of temporary registers in the D3D compiler on ANGLE.
238 if (type == GrContextFactory::kANGLE_GLContextType) {
239 maxStages = 3;
240 }
241#endif
242 REPORTER_ASSERT(reporter, gpu->programUnitTest(maxStages));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000243 }
244 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000245}
246
rmistry@google.comd6176b02012-08-23 18:14:13 +0000247// 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 +0000248// 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 +0000249// in the unit that could pass pointers to functions from the unit out to other translation units!
250// We force some of the effects that would otherwise be discarded to link here.
251
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000252#include "SkAlphaThresholdFilter.h"
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000253#include "SkLightingImageFilter.h"
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000254#include "SkMagnifierImageFilter.h"
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000255#include "SkColorMatrixFilter.h"
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000256
257void forceLinking();
258
259void forceLinking() {
260 SkLightingImageFilter::CreateDistantLitDiffuse(SkPoint3(0,0,0), 0, 0, 0);
commit-bot@chromium.org9109e182014-01-07 16:04:01 +0000261 SkAlphaThresholdFilter::Create(SkRegion(), .5f, .5f);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000262 SkMagnifierImageFilter mag(SkRect::MakeWH(SK_Scalar1, SK_Scalar1), SK_Scalar1);
bsalomon@google.comadc65362013-01-28 14:26:09 +0000263 GrConfigConversionEffect::Create(NULL,
264 false,
265 GrConfigConversionEffect::kNone_PMConversion,
266 SkMatrix::I());
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000267 SkScalar matrix[20];
268 SkColorMatrixFilter cmf(matrix);
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000269}
270
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000271#endif