blob: 2cf21c51981d4ddd864652ebf8f7ca83619bba13 [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"
24
bsalomon@google.com31ec7982013-03-27 18:14:57 +000025void GrGLProgramDesc::setRandom(SkMWCRandom* random,
26 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) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000033 int numEffects = numColorStages + numCoverageStages;
34 size_t keyLength = KeyLength(numEffects);
35 fKey.reset(keyLength);
36 *this->atOffset<uint32_t, kLengthOffset>() = static_cast<uint32_t>(keyLength);
37 memset(this->header(), 0, kHeaderSize);
jvanverth@google.com054ae992013-04-01 20:06:51 +000038
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000039 KeyHeader* header = this->header();
40 header->fEmitsPointSize = random->nextBool();
41
42 header->fPositionAttributeIndex = 0;
jvanverth@google.com054ae992013-04-01 20:06:51 +000043
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +000044 // if the effects have used up all off the available attributes,
jvanverth@google.com054ae992013-04-01 20:06:51 +000045 // don't try to use color or coverage attributes as input
46 do {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000047 header->fColorInput = random->nextULessThan(kColorInputCnt);
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +000048 } while (GrDrawState::kMaxVertexAttribCnt <= currAttribIndex &&
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000049 kAttribute_ColorInput == header->fColorInput);
50 header->fColorAttributeIndex = (header->fColorInput == kAttribute_ColorInput) ?
51 currAttribIndex++ :
52 -1;
jvanverth@google.com054ae992013-04-01 20:06:51 +000053
54 do {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000055 header->fCoverageInput = random->nextULessThan(kColorInputCnt);
jvanverth@google.com054ae992013-04-01 20:06:51 +000056 } while (GrDrawState::kMaxVertexAttribCnt <= currAttribIndex &&
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000057 kAttribute_ColorInput == header->fCoverageInput);
58 header->fCoverageAttributeIndex = (header->fCoverageInput == kAttribute_ColorInput) ?
59 currAttribIndex++ :
60 -1;
bsalomon@google.com91207482013-02-12 21:45:24 +000061
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000062 header->fColorFilterXfermode = random->nextULessThan(SkXfermode::kLastCoeffMode + 1);
bsalomon@google.com91207482013-02-12 21:45:24 +000063
bsalomon@google.com91207482013-02-12 21:45:24 +000064#if GR_GL_EXPERIMENTAL_GS
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000065 header->fExperimentalGS = gpu->caps()->geometryShaderSupport() && random->nextBool();
bsalomon@google.com91207482013-02-12 21:45:24 +000066#endif
67
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000068 header->fDiscardIfZeroCoverage = random->nextBool();
skia.committer@gmail.com37cbc7f2013-03-27 07:01:04 +000069
jvanverth@google.com054ae992013-04-01 20:06:51 +000070 bool useLocalCoords = random->nextBool() && currAttribIndex < GrDrawState::kMaxVertexAttribCnt;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000071 header->fLocalCoordAttributeIndex = useLocalCoords ? currAttribIndex++ : -1;
72
73 header->fColorEffectCnt = numColorStages;
74 header->fCoverageEffectCnt = numCoverageStages;
bsalomon@google.comc7818882013-03-20 19:19:53 +000075
bsalomon@google.comb79d8652013-03-29 20:30:50 +000076 bool dstRead = false;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000077 bool fragPos = false;
78 int numStages = numColorStages + numCoverageStages;
79 for (int s = 0; s < numStages; ++s) {
80 const GrBackendEffectFactory& factory = (*stages[s]->getEffect())->getFactory();
81 GrDrawEffect drawEffect(*stages[s], useLocalCoords);
82 this->effectKeys()[s] = factory.glEffectKey(drawEffect, gpu->glCaps());
83 if ((*stages[s]->getEffect())->willReadDstColor()) {
84 dstRead = true;
85 }
86 if ((*stages[s]->getEffect())->willReadFragmentPosition()) {
87 fragPos = true;
bsalomon@google.com91207482013-02-12 21:45:24 +000088 }
89 }
jvanverth@google.com9b855c72013-03-01 18:21:22 +000090
bsalomon@google.comb79d8652013-03-29 20:30:50 +000091 if (dstRead) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000092 header->fDstReadKey = GrGLShaderBuilder::KeyForDstRead(dstCopyTexture, gpu->glCaps());
93 } else {
94 header->fDstReadKey = 0;
95 }
96 if (fragPos) {
97 header->fFragPosKey = GrGLShaderBuilder::KeyForFragmentPosition(dstRenderTarget,
98 gpu->glCaps());
99 } else {
100 header->fFragPosKey = 0;
bsalomon@google.comb79d8652013-03-29 20:30:50 +0000101 }
bsalomon@google.com5920ac22013-04-19 13:14:45 +0000102
103 CoverageOutput coverageOutput;
104 bool illegalCoverageOutput;
105 do {
106 coverageOutput = static_cast<CoverageOutput>(random->nextULessThan(kCoverageOutputCnt));
107 illegalCoverageOutput = (!gpu->caps()->dualSourceBlendingSupport() &&
108 CoverageOutputUsesSecondaryOutput(coverageOutput)) ||
bsalomon@google.com72993ab2013-04-19 13:25:28 +0000109 (!dstRead && kCombineWithDst_CoverageOutput == coverageOutput);
bsalomon@google.com5920ac22013-04-19 13:14:45 +0000110 } while (illegalCoverageOutput);
111
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000112 header->fCoverageOutput = coverageOutput;
113
114 *this->checksum() = 0;
115 *this->checksum() = SkChecksum::Compute(reinterpret_cast<uint32_t*>(fKey.get()), keyLength);
116 fInitialized = true;
bsalomon@google.com91207482013-02-12 21:45:24 +0000117}
118
bsalomon@google.com042a2862013-02-04 18:39:24 +0000119bool GrGpuGL::programUnitTest(int maxStages) {
120
121 maxStages = GrMin(maxStages, (int)GrDrawState::kNumStages);
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000122
bsalomon@google.comd4726202012-08-03 14:34:46 +0000123 GrTextureDesc dummyDesc;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000124 dummyDesc.fFlags = kRenderTarget_GrTextureFlagBit;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000125 dummyDesc.fConfig = kSkia8888_GrPixelConfig;
bsalomon@google.comd4726202012-08-03 14:34:46 +0000126 dummyDesc.fWidth = 34;
127 dummyDesc.fHeight = 18;
128 SkAutoTUnref<GrTexture> dummyTexture1(this->createTexture(dummyDesc, NULL, 0));
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000129 dummyDesc.fFlags = kNone_GrTextureFlags;
bsalomon@google.comd4726202012-08-03 14:34:46 +0000130 dummyDesc.fConfig = kAlpha_8_GrPixelConfig;
131 dummyDesc.fWidth = 16;
132 dummyDesc.fHeight = 22;
133 SkAutoTUnref<GrTexture> dummyTexture2(this->createTexture(dummyDesc, NULL, 0));
134
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000135 static const int NUM_TESTS = 512;
136
bsalomon@google.com91207482013-02-12 21:45:24 +0000137 SkMWCRandom random;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000138 for (int t = 0; t < NUM_TESTS; ++t) {
139
140#if 0
141 GrPrintf("\nTest Program %d\n-------------\n", t);
142 static const int stop = -1;
143 if (t == stop) {
144 int breakpointhere = 9;
145 }
146#endif
147
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000148 GrGLProgramDesc pdesc;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000149
jvanverth@google.com054ae992013-04-01 20:06:51 +0000150 int currAttribIndex = 1; // we need to always leave room for position
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000151 int attribIndices[2];
bsalomon@google.comb79d8652013-03-29 20:30:50 +0000152 GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()};
commit-bot@chromium.org9ae78502013-03-21 17:44:39 +0000153
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000154 int numStages = random.nextULessThan(maxStages + 1);
155 int numColorStages = random.nextULessThan(numStages + 1);
156 int numCoverageStages = numStages - numColorStages;
157
158 SkAutoSTMalloc<8, const GrEffectStage*> stages(numStages);
159
160 for (int s = 0; s < numStages; ++s) {
161 SkAutoTUnref<const GrEffectRef> effect(GrEffectTestFactory::CreateStage(
162 &random,
163 this->getContext(),
164 *this->caps(),
165 dummyTextures));
166 int numAttribs = (*effect)->numVertexAttribs();
167
168 // If adding this effect would exceed the max attrib count then generate a
169 // new random effect.
170 if (currAttribIndex + numAttribs > GrDrawState::kMaxVertexAttribCnt) {
171 --s;
172 continue;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000173 }
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000174 for (int i = 0; i < numAttribs; ++i) {
175 attribIndices[i] = currAttribIndex++;
176 }
177 GrEffectStage* stage = SkNEW(GrEffectStage);
178 stage->setEffect(effect.get(), attribIndices[0], attribIndices[1]);
179 stages[s] = stage;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000180 }
bsalomon@google.comb79d8652013-03-29 20:30:50 +0000181 const GrTexture* dstTexture = random.nextBool() ? dummyTextures[0] : dummyTextures[1];
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000182 pdesc.setRandom(&random,
183 this,
184 dummyTextures[0]->asRenderTarget(),
185 dstTexture,
186 stages.get(),
187 numColorStages,
188 numCoverageStages,
189 currAttribIndex);
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000190
robertphillips@google.com6177e692013-02-28 20:16:25 +0000191 SkAutoTUnref<GrGLProgram> program(GrGLProgram::Create(this->glContext(),
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000192 pdesc,
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000193 stages.get()));
194 for (int s = 0; s < numStages; ++s) {
bsalomon@google.com504976e2013-05-09 13:45:02 +0000195 SkDELETE(stages[s]);
196 }
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000197 if (NULL == program.get()) {
198 return false;
199 }
200 }
201 return true;
202}
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000203
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000204static void GLProgramsTest(skiatest::Reporter* reporter, GrContextFactory* factory) {
205 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
206 GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(type));
207 if (NULL != context) {
bsalomon@google.com042a2862013-02-04 18:39:24 +0000208 GrGpuGL* gpu = static_cast<GrGpuGL*>(context->getGpu());
209 int maxStages = GrDrawState::kNumStages;
210#if SK_ANGLE
211 // Some long shaders run out of temporary registers in the D3D compiler on ANGLE.
212 if (type == GrContextFactory::kANGLE_GLContextType) {
213 maxStages = 3;
214 }
215#endif
216 REPORTER_ASSERT(reporter, gpu->programUnitTest(maxStages));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000217 }
218 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000219}
220
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000221#include "TestClassDef.h"
222DEFINE_GPUTESTCLASS("GLPrograms", GLProgramsTestClass, GLProgramsTest)
223
rmistry@google.comd6176b02012-08-23 18:14:13 +0000224// 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 +0000225// 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 +0000226// in the unit that could pass pointers to functions from the unit out to other translation units!
227// We force some of the effects that would otherwise be discarded to link here.
228
229#include "SkLightingImageFilter.h"
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000230#include "SkMagnifierImageFilter.h"
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000231#include "SkColorMatrixFilter.h"
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000232
233void forceLinking();
234
235void forceLinking() {
236 SkLightingImageFilter::CreateDistantLitDiffuse(SkPoint3(0,0,0), 0, 0, 0);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000237 SkMagnifierImageFilter mag(SkRect::MakeWH(SK_Scalar1, SK_Scalar1), SK_Scalar1);
bsalomon@google.comadc65362013-01-28 14:26:09 +0000238 GrConfigConversionEffect::Create(NULL,
239 false,
240 GrConfigConversionEffect::kNone_PMConversion,
241 SkMatrix::I());
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000242 SkScalar matrix[20];
243 SkColorMatrixFilter cmf(matrix);
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000244}
245
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000246#endif