blob: afbc1221adef13d07c0054ecba0c9a6a56281ea1 [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
tfarina@chromium.org223137f2012-11-21 22:38:36 +000021#include "SkRandom.h"
bsalomon@google.comc3841b92012-08-02 18:11:43 +000022#include "Test.h"
23
bsalomon@google.com91207482013-02-12 21:45:24 +000024void GrGLProgram::Desc::setRandom(SkMWCRandom* random,
25 const GrGpuGL* gpu,
26 const GrEffectStage stages[GrDrawState::kNumStages]) {
jvanverth@google.com9b855c72013-03-01 18:21:22 +000027 fAttribBindings = 0;
bsalomon@google.com91207482013-02-12 21:45:24 +000028 fEmitsPointSize = random->nextBool();
29 fColorInput = random->nextULessThan(kColorInputCnt);
30 fCoverageInput = random->nextULessThan(kColorInputCnt);
31
32 fColorFilterXfermode = random->nextULessThan(SkXfermode::kCoeffModesCnt);
33
34 fFirstCoverageStage = random->nextULessThan(GrDrawState::kNumStages);
35
jvanverth@google.com9b855c72013-03-01 18:21:22 +000036 fAttribBindings |= random->nextBool() ? GrDrawState::kCoverage_AttribBindingsBit : 0;
bsalomon@google.com91207482013-02-12 21:45:24 +000037
38#if GR_GL_EXPERIMENTAL_GS
bsalomon@google.combcce8922013-03-25 15:38:39 +000039 fExperimentalGS = gpu->caps()->geometryShaderSupport() && random->nextBool();
bsalomon@google.com91207482013-02-12 21:45:24 +000040#endif
41
bsalomon@google.com26ec00e2013-03-26 16:49:37 +000042 fDiscardIfZeroCoverage = random->nextBool();
skia.committer@gmail.com37cbc7f2013-03-27 07:01:04 +000043
bsalomon@google.combcce8922013-03-25 15:38:39 +000044 if (gpu->caps()->dualSourceBlendingSupport()) {
bsalomon@google.com91207482013-02-12 21:45:24 +000045 fDualSrcOutput = random->nextULessThan(kDualSrcOutputCnt);
46 } else {
47 fDualSrcOutput = kNone_DualSrcOutput;
48 }
49
bsalomon@google.comc7818882013-03-20 19:19:53 +000050 // use separate tex coords?
51 if (random->nextBool()) {
52 fAttribBindings |= GrDrawState::kLocalCoords_AttribBindingsBit;
53 }
54
bsalomon@google.com91207482013-02-12 21:45:24 +000055 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
56 if (NULL != stages[s].getEffect()) {
57 const GrBackendEffectFactory& factory = (*stages[s].getEffect())->getFactory();
bsalomon@google.comc7818882013-03-20 19:19:53 +000058 bool explicitLocalCoords = (fAttribBindings &
59 GrDrawState::kLocalCoords_AttribBindingsBit);
60 GrDrawEffect drawEffect(stages[s], explicitLocalCoords);
61 fEffectKeys[s] = factory.glEffectKey(drawEffect, gpu->glCaps());
bsalomon@google.com91207482013-02-12 21:45:24 +000062 }
63 }
jvanverth@google.com9b855c72013-03-01 18:21:22 +000064
65 int attributeIndex = 0;
66 fPositionAttributeIndex = attributeIndex;
67 ++attributeIndex;
68 if (fColorInput || (fAttribBindings & GrDrawState::kColor_AttribBindingsBit)) {
69 fColorAttributeIndex = attributeIndex;
70 ++attributeIndex;
71 }
72 if (fCoverageInput || (fAttribBindings & GrDrawState::kCoverage_AttribBindingsBit)) {
73 fCoverageAttributeIndex = attributeIndex;
74 ++attributeIndex;
75 }
bsalomon@google.comc7818882013-03-20 19:19:53 +000076 if (fAttribBindings & GrDrawState::kLocalCoords_AttribBindingsBit) {
77 fLocalCoordsAttributeIndex = attributeIndex;
jvanverth@google.com9b855c72013-03-01 18:21:22 +000078 ++attributeIndex;
79 }
bsalomon@google.com91207482013-02-12 21:45:24 +000080}
81
bsalomon@google.com042a2862013-02-04 18:39:24 +000082bool GrGpuGL::programUnitTest(int maxStages) {
83
84 maxStages = GrMin(maxStages, (int)GrDrawState::kNumStages);
bsalomon@google.comc3841b92012-08-02 18:11:43 +000085
bsalomon@google.comd4726202012-08-03 14:34:46 +000086 GrTextureDesc dummyDesc;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +000087 dummyDesc.fConfig = kSkia8888_GrPixelConfig;
bsalomon@google.comd4726202012-08-03 14:34:46 +000088 dummyDesc.fWidth = 34;
89 dummyDesc.fHeight = 18;
90 SkAutoTUnref<GrTexture> dummyTexture1(this->createTexture(dummyDesc, NULL, 0));
91 dummyDesc.fConfig = kAlpha_8_GrPixelConfig;
92 dummyDesc.fWidth = 16;
93 dummyDesc.fHeight = 22;
94 SkAutoTUnref<GrTexture> dummyTexture2(this->createTexture(dummyDesc, NULL, 0));
95
bsalomon@google.comc3841b92012-08-02 18:11:43 +000096 static const int NUM_TESTS = 512;
97
bsalomon@google.com91207482013-02-12 21:45:24 +000098 SkMWCRandom random;
bsalomon@google.comc3841b92012-08-02 18:11:43 +000099 for (int t = 0; t < NUM_TESTS; ++t) {
100
101#if 0
102 GrPrintf("\nTest Program %d\n-------------\n", t);
103 static const int stop = -1;
104 if (t == stop) {
105 int breakpointhere = 9;
106 }
107#endif
108
bsalomon@google.com91207482013-02-12 21:45:24 +0000109 GrGLProgram::Desc pdesc;
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000110 GrEffectStage stages[GrDrawState::kNumStages];
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000111
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000112 int currAttribIndex = GrDrawState::kAttribIndexCount;
113 int attribIndices[2];
bsalomon@google.com1d1d4242013-02-05 15:44:21 +0000114 for (int s = 0; s < maxStages; ++s) {
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000115 // enable the stage?
bsalomon@google.com91207482013-02-12 21:45:24 +0000116 if (random.nextBool()) {
bsalomon@google.comd4726202012-08-03 14:34:46 +0000117 GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()};
bsalomon@google.com73a96942013-02-13 16:31:19 +0000118 SkAutoTUnref<const GrEffectRef> effect(GrEffectTestFactory::CreateStage(
119 &random,
120 this->getContext(),
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000121 *this->caps(),
bsalomon@google.com73a96942013-02-13 16:31:19 +0000122 dummyTextures));
commit-bot@chromium.org9ae78502013-03-21 17:44:39 +0000123 int numAttribs = (*effect)->numVertexAttribs();
124
125 // If adding this effect would cause to exceed the max attrib count then generate a
126 // new random effect. The explanation for why this check is correct is a bit
127 // convoluted and this code will be removed soon.
128 if (currAttribIndex + numAttribs > GrDrawState::kCoverageOverrideAttribIndexValue) {
129 --s;
130 continue;
131 }
132 for (int i = 0; i < numAttribs; ++i) {
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000133 attribIndices[i] = currAttribIndex++;
134 }
jvanverth@google.com65eb4d52013-03-19 18:51:02 +0000135 stages[s].setEffect(effect.get(), attribIndices[0], attribIndices[1]);
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000136 }
137 }
bsalomon@google.com91207482013-02-12 21:45:24 +0000138 pdesc.setRandom(&random, this, stages);
139
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000140 const GrEffectStage* stagePtrs[GrDrawState::kNumStages];
141 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
142 stagePtrs[s] = &stages[s];
143 }
robertphillips@google.com6177e692013-02-28 20:16:25 +0000144 SkAutoTUnref<GrGLProgram> program(GrGLProgram::Create(this->glContext(),
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000145 pdesc,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000146 stagePtrs));
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000147 if (NULL == program.get()) {
148 return false;
149 }
150 }
151 return true;
152}
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000153
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000154static void GLProgramsTest(skiatest::Reporter* reporter, GrContextFactory* factory) {
155 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
156 GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(type));
157 if (NULL != context) {
bsalomon@google.com042a2862013-02-04 18:39:24 +0000158 GrGpuGL* gpu = static_cast<GrGpuGL*>(context->getGpu());
159 int maxStages = GrDrawState::kNumStages;
160#if SK_ANGLE
161 // Some long shaders run out of temporary registers in the D3D compiler on ANGLE.
162 if (type == GrContextFactory::kANGLE_GLContextType) {
163 maxStages = 3;
164 }
165#endif
166 REPORTER_ASSERT(reporter, gpu->programUnitTest(maxStages));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000167 }
168 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000169}
170
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000171#include "TestClassDef.h"
172DEFINE_GPUTESTCLASS("GLPrograms", GLProgramsTestClass, GLProgramsTest)
173
rmistry@google.comd6176b02012-08-23 18:14:13 +0000174// 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 +0000175// 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 +0000176// in the unit that could pass pointers to functions from the unit out to other translation units!
177// We force some of the effects that would otherwise be discarded to link here.
178
179#include "SkLightingImageFilter.h"
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000180#include "SkMagnifierImageFilter.h"
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000181#include "SkColorMatrixFilter.h"
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000182
183void forceLinking();
184
185void forceLinking() {
186 SkLightingImageFilter::CreateDistantLitDiffuse(SkPoint3(0,0,0), 0, 0, 0);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000187 SkMagnifierImageFilter mag(SkRect::MakeWH(SK_Scalar1, SK_Scalar1), SK_Scalar1);
bsalomon@google.comadc65362013-01-28 14:26:09 +0000188 GrConfigConversionEffect::Create(NULL,
189 false,
190 GrConfigConversionEffect::kNone_PMConversion,
191 SkMatrix::I());
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000192 SkScalar matrix[20];
193 SkColorMatrixFilter cmf(matrix);
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000194}
195
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000196#endif