blob: 512f89ecf201c5d4898db23dca6cae0b0340e6c3 [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.com31ec7982013-03-27 18:14:57 +000024void GrGLProgramDesc::setRandom(SkMWCRandom* random,
25 const GrGpuGL* gpu,
bsalomon@google.comb79d8652013-03-29 20:30:50 +000026 const GrTexture* dstTexture,
bsalomon@google.com31ec7982013-03-27 18:14:57 +000027 const GrEffectStage stages[GrDrawState::kNumStages]) {
jvanverth@google.com9b855c72013-03-01 18:21:22 +000028 fAttribBindings = 0;
bsalomon@google.com91207482013-02-12 21:45:24 +000029 fEmitsPointSize = random->nextBool();
30 fColorInput = random->nextULessThan(kColorInputCnt);
31 fCoverageInput = random->nextULessThan(kColorInputCnt);
32
33 fColorFilterXfermode = random->nextULessThan(SkXfermode::kCoeffModesCnt);
34
35 fFirstCoverageStage = random->nextULessThan(GrDrawState::kNumStages);
36
jvanverth@google.com9b855c72013-03-01 18:21:22 +000037 fAttribBindings |= random->nextBool() ? GrDrawState::kCoverage_AttribBindingsBit : 0;
bsalomon@google.com91207482013-02-12 21:45:24 +000038
39#if GR_GL_EXPERIMENTAL_GS
bsalomon@google.combcce8922013-03-25 15:38:39 +000040 fExperimentalGS = gpu->caps()->geometryShaderSupport() && random->nextBool();
bsalomon@google.com91207482013-02-12 21:45:24 +000041#endif
42
bsalomon@google.com26ec00e2013-03-26 16:49:37 +000043 fDiscardIfZeroCoverage = random->nextBool();
skia.committer@gmail.com37cbc7f2013-03-27 07:01:04 +000044
bsalomon@google.combcce8922013-03-25 15:38:39 +000045 if (gpu->caps()->dualSourceBlendingSupport()) {
bsalomon@google.com91207482013-02-12 21:45:24 +000046 fDualSrcOutput = random->nextULessThan(kDualSrcOutputCnt);
47 } else {
48 fDualSrcOutput = kNone_DualSrcOutput;
49 }
50
bsalomon@google.comc7818882013-03-20 19:19:53 +000051 // use separate tex coords?
52 if (random->nextBool()) {
53 fAttribBindings |= GrDrawState::kLocalCoords_AttribBindingsBit;
54 }
55
bsalomon@google.comb79d8652013-03-29 20:30:50 +000056 bool dstRead = false;
bsalomon@google.com91207482013-02-12 21:45:24 +000057 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
58 if (NULL != stages[s].getEffect()) {
59 const GrBackendEffectFactory& factory = (*stages[s].getEffect())->getFactory();
bsalomon@google.comc7818882013-03-20 19:19:53 +000060 bool explicitLocalCoords = (fAttribBindings &
61 GrDrawState::kLocalCoords_AttribBindingsBit);
62 GrDrawEffect drawEffect(stages[s], explicitLocalCoords);
63 fEffectKeys[s] = factory.glEffectKey(drawEffect, gpu->glCaps());
bsalomon@google.comb79d8652013-03-29 20:30:50 +000064 if ((*stages[s].getEffect())->willReadDst()) {
65 dstRead = true;
66 }
bsalomon@google.com91207482013-02-12 21:45:24 +000067 }
68 }
jvanverth@google.com9b855c72013-03-01 18:21:22 +000069
bsalomon@google.comb79d8652013-03-29 20:30:50 +000070 if (dstRead) {
71 this->fDstRead = GrGLShaderBuilder::KeyForDstRead(dstTexture, gpu->glCaps());
72 }
73
jvanverth@google.com9b855c72013-03-01 18:21:22 +000074 int attributeIndex = 0;
75 fPositionAttributeIndex = attributeIndex;
76 ++attributeIndex;
77 if (fColorInput || (fAttribBindings & GrDrawState::kColor_AttribBindingsBit)) {
78 fColorAttributeIndex = attributeIndex;
79 ++attributeIndex;
80 }
81 if (fCoverageInput || (fAttribBindings & GrDrawState::kCoverage_AttribBindingsBit)) {
82 fCoverageAttributeIndex = attributeIndex;
83 ++attributeIndex;
84 }
bsalomon@google.comc7818882013-03-20 19:19:53 +000085 if (fAttribBindings & GrDrawState::kLocalCoords_AttribBindingsBit) {
86 fLocalCoordsAttributeIndex = attributeIndex;
jvanverth@google.com9b855c72013-03-01 18:21:22 +000087 ++attributeIndex;
88 }
bsalomon@google.com91207482013-02-12 21:45:24 +000089}
90
bsalomon@google.com042a2862013-02-04 18:39:24 +000091bool GrGpuGL::programUnitTest(int maxStages) {
92
93 maxStages = GrMin(maxStages, (int)GrDrawState::kNumStages);
bsalomon@google.comc3841b92012-08-02 18:11:43 +000094
bsalomon@google.comd4726202012-08-03 14:34:46 +000095 GrTextureDesc dummyDesc;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +000096 dummyDesc.fConfig = kSkia8888_GrPixelConfig;
bsalomon@google.comd4726202012-08-03 14:34:46 +000097 dummyDesc.fWidth = 34;
98 dummyDesc.fHeight = 18;
99 SkAutoTUnref<GrTexture> dummyTexture1(this->createTexture(dummyDesc, NULL, 0));
100 dummyDesc.fConfig = kAlpha_8_GrPixelConfig;
101 dummyDesc.fWidth = 16;
102 dummyDesc.fHeight = 22;
103 SkAutoTUnref<GrTexture> dummyTexture2(this->createTexture(dummyDesc, NULL, 0));
104
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000105 static const int NUM_TESTS = 512;
106
bsalomon@google.com91207482013-02-12 21:45:24 +0000107 SkMWCRandom random;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000108 for (int t = 0; t < NUM_TESTS; ++t) {
109
110#if 0
111 GrPrintf("\nTest Program %d\n-------------\n", t);
112 static const int stop = -1;
113 if (t == stop) {
114 int breakpointhere = 9;
115 }
116#endif
117
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000118 GrGLProgramDesc pdesc;
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000119 GrEffectStage stages[GrDrawState::kNumStages];
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000120
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000121 int currAttribIndex = GrDrawState::kAttribIndexCount;
122 int attribIndices[2];
bsalomon@google.comb79d8652013-03-29 20:30:50 +0000123 GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()};
bsalomon@google.com1d1d4242013-02-05 15:44:21 +0000124 for (int s = 0; s < maxStages; ++s) {
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000125 // enable the stage?
bsalomon@google.com91207482013-02-12 21:45:24 +0000126 if (random.nextBool()) {
bsalomon@google.com73a96942013-02-13 16:31:19 +0000127 SkAutoTUnref<const GrEffectRef> effect(GrEffectTestFactory::CreateStage(
128 &random,
129 this->getContext(),
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000130 *this->caps(),
bsalomon@google.com73a96942013-02-13 16:31:19 +0000131 dummyTextures));
commit-bot@chromium.org9ae78502013-03-21 17:44:39 +0000132 int numAttribs = (*effect)->numVertexAttribs();
133
134 // If adding this effect would cause to exceed the max attrib count then generate a
135 // new random effect. The explanation for why this check is correct is a bit
136 // convoluted and this code will be removed soon.
137 if (currAttribIndex + numAttribs > GrDrawState::kCoverageOverrideAttribIndexValue) {
138 --s;
139 continue;
140 }
141 for (int i = 0; i < numAttribs; ++i) {
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000142 attribIndices[i] = currAttribIndex++;
143 }
jvanverth@google.com65eb4d52013-03-19 18:51:02 +0000144 stages[s].setEffect(effect.get(), attribIndices[0], attribIndices[1]);
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000145 }
146 }
bsalomon@google.comb79d8652013-03-29 20:30:50 +0000147 const GrTexture* dstTexture = random.nextBool() ? dummyTextures[0] : dummyTextures[1];
148 pdesc.setRandom(&random, this, dstTexture, stages);
bsalomon@google.com91207482013-02-12 21:45:24 +0000149
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000150 const GrEffectStage* stagePtrs[GrDrawState::kNumStages];
151 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
152 stagePtrs[s] = &stages[s];
153 }
robertphillips@google.com6177e692013-02-28 20:16:25 +0000154 SkAutoTUnref<GrGLProgram> program(GrGLProgram::Create(this->glContext(),
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000155 pdesc,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000156 stagePtrs));
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000157 if (NULL == program.get()) {
158 return false;
159 }
160 }
161 return true;
162}
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000163
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000164static void GLProgramsTest(skiatest::Reporter* reporter, GrContextFactory* factory) {
165 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
166 GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(type));
167 if (NULL != context) {
bsalomon@google.com042a2862013-02-04 18:39:24 +0000168 GrGpuGL* gpu = static_cast<GrGpuGL*>(context->getGpu());
169 int maxStages = GrDrawState::kNumStages;
170#if SK_ANGLE
171 // Some long shaders run out of temporary registers in the D3D compiler on ANGLE.
172 if (type == GrContextFactory::kANGLE_GLContextType) {
173 maxStages = 3;
174 }
175#endif
176 REPORTER_ASSERT(reporter, gpu->programUnitTest(maxStages));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000177 }
178 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000179}
180
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000181#include "TestClassDef.h"
182DEFINE_GPUTESTCLASS("GLPrograms", GLProgramsTestClass, GLProgramsTest)
183
rmistry@google.comd6176b02012-08-23 18:14:13 +0000184// 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 +0000185// 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 +0000186// in the unit that could pass pointers to functions from the unit out to other translation units!
187// We force some of the effects that would otherwise be discarded to link here.
188
189#include "SkLightingImageFilter.h"
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000190#include "SkMagnifierImageFilter.h"
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000191#include "SkColorMatrixFilter.h"
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000192
193void forceLinking();
194
195void forceLinking() {
196 SkLightingImageFilter::CreateDistantLitDiffuse(SkPoint3(0,0,0), 0, 0, 0);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000197 SkMagnifierImageFilter mag(SkRect::MakeWH(SK_Scalar1, SK_Scalar1), SK_Scalar1);
bsalomon@google.comadc65362013-01-28 14:26:09 +0000198 GrConfigConversionEffect::Create(NULL,
199 false,
200 GrConfigConversionEffect::kNone_PMConversion,
201 SkMatrix::I());
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000202 SkScalar matrix[20];
203 SkColorMatrixFilter cmf(matrix);
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000204}
205
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000206#endif