blob: aeb7ecc11ee3fa3244487fc66b917b2ebecfeb78 [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
39 fExperimentalGS = gpu->getCaps().geometryShaderSupport() && random->nextBool();
40#endif
41
42 bool edgeAA = random->nextBool();
43 if (edgeAA) {
jvanverth@google.com9b855c72013-03-01 18:21:22 +000044 fAttribBindings |= GrDrawState::kEdge_AttribBindingsBit;
bsalomon@google.com91207482013-02-12 21:45:24 +000045 if (gpu->getCaps().shaderDerivativeSupport()) {
46 fVertexEdgeType = (GrDrawState::VertexEdgeType)
47 random->nextULessThan(GrDrawState::kVertexEdgeTypeCnt);
48 fDiscardIfOutsideEdge = random->nextBool();
49 } else {
50 fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
51 fDiscardIfOutsideEdge = false;
52 }
53 }
54
55 if (gpu->getCaps().dualSourceBlendingSupport()) {
56 fDualSrcOutput = random->nextULessThan(kDualSrcOutputCnt);
57 } else {
58 fDualSrcOutput = kNone_DualSrcOutput;
59 }
60
bsalomon@google.comc7818882013-03-20 19:19:53 +000061 // use separate tex coords?
62 if (random->nextBool()) {
63 fAttribBindings |= GrDrawState::kLocalCoords_AttribBindingsBit;
64 }
65
bsalomon@google.com91207482013-02-12 21:45:24 +000066 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
67 if (NULL != stages[s].getEffect()) {
68 const GrBackendEffectFactory& factory = (*stages[s].getEffect())->getFactory();
bsalomon@google.comc7818882013-03-20 19:19:53 +000069 bool explicitLocalCoords = (fAttribBindings &
70 GrDrawState::kLocalCoords_AttribBindingsBit);
71 GrDrawEffect drawEffect(stages[s], explicitLocalCoords);
72 fEffectKeys[s] = factory.glEffectKey(drawEffect, gpu->glCaps());
bsalomon@google.com91207482013-02-12 21:45:24 +000073 }
74 }
jvanverth@google.com9b855c72013-03-01 18:21:22 +000075
76 int attributeIndex = 0;
77 fPositionAttributeIndex = attributeIndex;
78 ++attributeIndex;
79 if (fColorInput || (fAttribBindings & GrDrawState::kColor_AttribBindingsBit)) {
80 fColorAttributeIndex = attributeIndex;
81 ++attributeIndex;
82 }
83 if (fCoverageInput || (fAttribBindings & GrDrawState::kCoverage_AttribBindingsBit)) {
84 fCoverageAttributeIndex = attributeIndex;
85 ++attributeIndex;
86 }
87 if (fAttribBindings & GrDrawState::kEdge_AttribBindingsBit) {
88 fEdgeAttributeIndex = attributeIndex;
89 ++attributeIndex;
90 }
bsalomon@google.comc7818882013-03-20 19:19:53 +000091 if (fAttribBindings & GrDrawState::kLocalCoords_AttribBindingsBit) {
92 fLocalCoordsAttributeIndex = attributeIndex;
jvanverth@google.com9b855c72013-03-01 18:21:22 +000093 ++attributeIndex;
94 }
bsalomon@google.com91207482013-02-12 21:45:24 +000095}
96
bsalomon@google.com042a2862013-02-04 18:39:24 +000097bool GrGpuGL::programUnitTest(int maxStages) {
98
99 maxStages = GrMin(maxStages, (int)GrDrawState::kNumStages);
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000100
bsalomon@google.comd4726202012-08-03 14:34:46 +0000101 GrTextureDesc dummyDesc;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000102 dummyDesc.fConfig = kSkia8888_GrPixelConfig;
bsalomon@google.comd4726202012-08-03 14:34:46 +0000103 dummyDesc.fWidth = 34;
104 dummyDesc.fHeight = 18;
105 SkAutoTUnref<GrTexture> dummyTexture1(this->createTexture(dummyDesc, NULL, 0));
106 dummyDesc.fConfig = kAlpha_8_GrPixelConfig;
107 dummyDesc.fWidth = 16;
108 dummyDesc.fHeight = 22;
109 SkAutoTUnref<GrTexture> dummyTexture2(this->createTexture(dummyDesc, NULL, 0));
110
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000111 static const int NUM_TESTS = 512;
112
bsalomon@google.com91207482013-02-12 21:45:24 +0000113 SkMWCRandom random;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000114 for (int t = 0; t < NUM_TESTS; ++t) {
115
116#if 0
117 GrPrintf("\nTest Program %d\n-------------\n", t);
118 static const int stop = -1;
119 if (t == stop) {
120 int breakpointhere = 9;
121 }
122#endif
123
bsalomon@google.com91207482013-02-12 21:45:24 +0000124 GrGLProgram::Desc pdesc;
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000125 GrEffectStage stages[GrDrawState::kNumStages];
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000126
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000127 int currAttribIndex = GrDrawState::kAttribIndexCount;
128 int attribIndices[2];
bsalomon@google.com1d1d4242013-02-05 15:44:21 +0000129 for (int s = 0; s < maxStages; ++s) {
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000130 // enable the stage?
bsalomon@google.com91207482013-02-12 21:45:24 +0000131 if (random.nextBool()) {
bsalomon@google.comd4726202012-08-03 14:34:46 +0000132 GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()};
bsalomon@google.com73a96942013-02-13 16:31:19 +0000133 SkAutoTUnref<const GrEffectRef> effect(GrEffectTestFactory::CreateStage(
134 &random,
135 this->getContext(),
136 dummyTextures));
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000137 for (int i = 0; i < effect.get()->get()->numVertexAttribs(); ++i) {
138 attribIndices[i] = currAttribIndex++;
139 }
jvanverth@google.com65eb4d52013-03-19 18:51:02 +0000140 stages[s].setEffect(effect.get(), attribIndices[0], attribIndices[1]);
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000141 }
142 }
bsalomon@google.com91207482013-02-12 21:45:24 +0000143 pdesc.setRandom(&random, this, stages);
144
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000145 const GrEffectStage* stagePtrs[GrDrawState::kNumStages];
146 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
147 stagePtrs[s] = &stages[s];
148 }
robertphillips@google.com6177e692013-02-28 20:16:25 +0000149 SkAutoTUnref<GrGLProgram> program(GrGLProgram::Create(this->glContext(),
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000150 pdesc,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000151 stagePtrs));
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000152 if (NULL == program.get()) {
153 return false;
154 }
155 }
156 return true;
157}
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000158
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000159static void GLProgramsTest(skiatest::Reporter* reporter, GrContextFactory* factory) {
160 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
161 GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(type));
162 if (NULL != context) {
bsalomon@google.com042a2862013-02-04 18:39:24 +0000163 GrGpuGL* gpu = static_cast<GrGpuGL*>(context->getGpu());
164 int maxStages = GrDrawState::kNumStages;
165#if SK_ANGLE
166 // Some long shaders run out of temporary registers in the D3D compiler on ANGLE.
167 if (type == GrContextFactory::kANGLE_GLContextType) {
168 maxStages = 3;
169 }
170#endif
171 REPORTER_ASSERT(reporter, gpu->programUnitTest(maxStages));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000172 }
173 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000174}
175
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000176#include "TestClassDef.h"
177DEFINE_GPUTESTCLASS("GLPrograms", GLProgramsTestClass, GLProgramsTest)
178
rmistry@google.comd6176b02012-08-23 18:14:13 +0000179// 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 +0000180// 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 +0000181// in the unit that could pass pointers to functions from the unit out to other translation units!
182// We force some of the effects that would otherwise be discarded to link here.
183
184#include "SkLightingImageFilter.h"
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000185#include "SkMagnifierImageFilter.h"
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000186#include "SkColorMatrixFilter.h"
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000187
188void forceLinking();
189
190void forceLinking() {
191 SkLightingImageFilter::CreateDistantLitDiffuse(SkPoint3(0,0,0), 0, 0, 0);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000192 SkMagnifierImageFilter mag(SkRect::MakeWH(SK_Scalar1, SK_Scalar1), SK_Scalar1);
bsalomon@google.comadc65362013-01-28 14:26:09 +0000193 GrConfigConversionEffect::Create(NULL,
194 false,
195 GrConfigConversionEffect::kNone_PMConversion,
196 SkMatrix::I());
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000197 SkScalar matrix[20];
198 SkColorMatrixFilter cmf(matrix);
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000199}
200
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000201#endif