blob: 50ba9c3dce30862429d3b8f5ccc731a1efc00105 [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.com4647f902013-03-26 14:45:27 +000042 if (gpu->caps()->shaderDerivativeSupport()) {
43 fDiscardIfOutsideEdge = random->nextBool();
44 } else {
45 fDiscardIfOutsideEdge = false;
bsalomon@google.com91207482013-02-12 21:45:24 +000046 }
47
bsalomon@google.combcce8922013-03-25 15:38:39 +000048 if (gpu->caps()->dualSourceBlendingSupport()) {
bsalomon@google.com91207482013-02-12 21:45:24 +000049 fDualSrcOutput = random->nextULessThan(kDualSrcOutputCnt);
50 } else {
51 fDualSrcOutput = kNone_DualSrcOutput;
52 }
53
bsalomon@google.comc7818882013-03-20 19:19:53 +000054 // use separate tex coords?
55 if (random->nextBool()) {
56 fAttribBindings |= GrDrawState::kLocalCoords_AttribBindingsBit;
57 }
58
bsalomon@google.com91207482013-02-12 21:45:24 +000059 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
60 if (NULL != stages[s].getEffect()) {
61 const GrBackendEffectFactory& factory = (*stages[s].getEffect())->getFactory();
bsalomon@google.comc7818882013-03-20 19:19:53 +000062 bool explicitLocalCoords = (fAttribBindings &
63 GrDrawState::kLocalCoords_AttribBindingsBit);
64 GrDrawEffect drawEffect(stages[s], explicitLocalCoords);
65 fEffectKeys[s] = factory.glEffectKey(drawEffect, gpu->glCaps());
bsalomon@google.com91207482013-02-12 21:45:24 +000066 }
67 }
jvanverth@google.com9b855c72013-03-01 18:21:22 +000068
69 int attributeIndex = 0;
70 fPositionAttributeIndex = attributeIndex;
71 ++attributeIndex;
72 if (fColorInput || (fAttribBindings & GrDrawState::kColor_AttribBindingsBit)) {
73 fColorAttributeIndex = attributeIndex;
74 ++attributeIndex;
75 }
76 if (fCoverageInput || (fAttribBindings & GrDrawState::kCoverage_AttribBindingsBit)) {
77 fCoverageAttributeIndex = attributeIndex;
78 ++attributeIndex;
79 }
bsalomon@google.comc7818882013-03-20 19:19:53 +000080 if (fAttribBindings & GrDrawState::kLocalCoords_AttribBindingsBit) {
81 fLocalCoordsAttributeIndex = attributeIndex;
jvanverth@google.com9b855c72013-03-01 18:21:22 +000082 ++attributeIndex;
83 }
bsalomon@google.com91207482013-02-12 21:45:24 +000084}
85
bsalomon@google.com042a2862013-02-04 18:39:24 +000086bool GrGpuGL::programUnitTest(int maxStages) {
87
88 maxStages = GrMin(maxStages, (int)GrDrawState::kNumStages);
bsalomon@google.comc3841b92012-08-02 18:11:43 +000089
bsalomon@google.comd4726202012-08-03 14:34:46 +000090 GrTextureDesc dummyDesc;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +000091 dummyDesc.fConfig = kSkia8888_GrPixelConfig;
bsalomon@google.comd4726202012-08-03 14:34:46 +000092 dummyDesc.fWidth = 34;
93 dummyDesc.fHeight = 18;
94 SkAutoTUnref<GrTexture> dummyTexture1(this->createTexture(dummyDesc, NULL, 0));
95 dummyDesc.fConfig = kAlpha_8_GrPixelConfig;
96 dummyDesc.fWidth = 16;
97 dummyDesc.fHeight = 22;
98 SkAutoTUnref<GrTexture> dummyTexture2(this->createTexture(dummyDesc, NULL, 0));
99
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000100 static const int NUM_TESTS = 512;
101
bsalomon@google.com91207482013-02-12 21:45:24 +0000102 SkMWCRandom random;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000103 for (int t = 0; t < NUM_TESTS; ++t) {
104
105#if 0
106 GrPrintf("\nTest Program %d\n-------------\n", t);
107 static const int stop = -1;
108 if (t == stop) {
109 int breakpointhere = 9;
110 }
111#endif
112
bsalomon@google.com91207482013-02-12 21:45:24 +0000113 GrGLProgram::Desc pdesc;
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000114 GrEffectStage stages[GrDrawState::kNumStages];
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000115
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000116 int currAttribIndex = GrDrawState::kAttribIndexCount;
117 int attribIndices[2];
bsalomon@google.com1d1d4242013-02-05 15:44:21 +0000118 for (int s = 0; s < maxStages; ++s) {
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000119 // enable the stage?
bsalomon@google.com91207482013-02-12 21:45:24 +0000120 if (random.nextBool()) {
bsalomon@google.comd4726202012-08-03 14:34:46 +0000121 GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()};
bsalomon@google.com73a96942013-02-13 16:31:19 +0000122 SkAutoTUnref<const GrEffectRef> effect(GrEffectTestFactory::CreateStage(
123 &random,
124 this->getContext(),
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000125 *this->caps(),
bsalomon@google.com73a96942013-02-13 16:31:19 +0000126 dummyTextures));
commit-bot@chromium.org9ae78502013-03-21 17:44:39 +0000127 int numAttribs = (*effect)->numVertexAttribs();
128
129 // If adding this effect would cause to exceed the max attrib count then generate a
130 // new random effect. The explanation for why this check is correct is a bit
131 // convoluted and this code will be removed soon.
132 if (currAttribIndex + numAttribs > GrDrawState::kCoverageOverrideAttribIndexValue) {
133 --s;
134 continue;
135 }
136 for (int i = 0; i < numAttribs; ++i) {
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000137 attribIndices[i] = currAttribIndex++;
138 }
jvanverth@google.com65eb4d52013-03-19 18:51:02 +0000139 stages[s].setEffect(effect.get(), attribIndices[0], attribIndices[1]);
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000140 }
141 }
bsalomon@google.com91207482013-02-12 21:45:24 +0000142 pdesc.setRandom(&random, this, stages);
143
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000144 const GrEffectStage* stagePtrs[GrDrawState::kNumStages];
145 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
146 stagePtrs[s] = &stages[s];
147 }
robertphillips@google.com6177e692013-02-28 20:16:25 +0000148 SkAutoTUnref<GrGLProgram> program(GrGLProgram::Create(this->glContext(),
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000149 pdesc,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000150 stagePtrs));
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000151 if (NULL == program.get()) {
152 return false;
153 }
154 }
155 return true;
156}
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000157
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000158static void GLProgramsTest(skiatest::Reporter* reporter, GrContextFactory* factory) {
159 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
160 GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(type));
161 if (NULL != context) {
bsalomon@google.com042a2862013-02-04 18:39:24 +0000162 GrGpuGL* gpu = static_cast<GrGpuGL*>(context->getGpu());
163 int maxStages = GrDrawState::kNumStages;
164#if SK_ANGLE
165 // Some long shaders run out of temporary registers in the D3D compiler on ANGLE.
166 if (type == GrContextFactory::kANGLE_GLContextType) {
167 maxStages = 3;
168 }
169#endif
170 REPORTER_ASSERT(reporter, gpu->programUnitTest(maxStages));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000171 }
172 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000173}
174
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000175#include "TestClassDef.h"
176DEFINE_GPUTESTCLASS("GLPrograms", GLProgramsTestClass, GLProgramsTest)
177
rmistry@google.comd6176b02012-08-23 18:14:13 +0000178// 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 +0000179// 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 +0000180// in the unit that could pass pointers to functions from the unit out to other translation units!
181// We force some of the effects that would otherwise be discarded to link here.
182
183#include "SkLightingImageFilter.h"
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000184#include "SkMagnifierImageFilter.h"
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000185#include "SkColorMatrixFilter.h"
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000186
187void forceLinking();
188
189void forceLinking() {
190 SkLightingImageFilter::CreateDistantLitDiffuse(SkPoint3(0,0,0), 0, 0, 0);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000191 SkMagnifierImageFilter mag(SkRect::MakeWH(SK_Scalar1, SK_Scalar1), SK_Scalar1);
bsalomon@google.comadc65362013-01-28 14:26:09 +0000192 GrConfigConversionEffect::Create(NULL,
193 false,
194 GrConfigConversionEffect::kNone_PMConversion,
195 SkMatrix::I());
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000196 SkScalar matrix[20];
197 SkColorMatrixFilter cmf(matrix);
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000198}
199
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000200#endif