blob: 45c5f2d16216a2319a743adaa11ddac147bc9436 [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.coma04e8e82012-08-27 12:53:13 +000018#include "effects/GrConfigConversionEffect.h"
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +000019
tfarina@chromium.org223137f2012-11-21 22:38:36 +000020#include "SkRandom.h"
bsalomon@google.comc3841b92012-08-02 18:11:43 +000021#include "Test.h"
22
bsalomon@google.com91207482013-02-12 21:45:24 +000023void GrGLProgram::Desc::setRandom(SkMWCRandom* random,
24 const GrGpuGL* gpu,
25 const GrEffectStage stages[GrDrawState::kNumStages]) {
jvanverth@google.com9b855c72013-03-01 18:21:22 +000026 fAttribBindings = 0;
bsalomon@google.com91207482013-02-12 21:45:24 +000027 fEmitsPointSize = random->nextBool();
28 fColorInput = random->nextULessThan(kColorInputCnt);
29 fCoverageInput = random->nextULessThan(kColorInputCnt);
30
31 fColorFilterXfermode = random->nextULessThan(SkXfermode::kCoeffModesCnt);
32
33 fFirstCoverageStage = random->nextULessThan(GrDrawState::kNumStages);
34
jvanverth@google.com9b855c72013-03-01 18:21:22 +000035 fAttribBindings |= random->nextBool() ? GrDrawState::kCoverage_AttribBindingsBit : 0;
bsalomon@google.com91207482013-02-12 21:45:24 +000036
37#if GR_GL_EXPERIMENTAL_GS
38 fExperimentalGS = gpu->getCaps().geometryShaderSupport() && random->nextBool();
39#endif
40
41 bool edgeAA = random->nextBool();
42 if (edgeAA) {
jvanverth@google.com9b855c72013-03-01 18:21:22 +000043 fAttribBindings |= GrDrawState::kEdge_AttribBindingsBit;
bsalomon@google.com91207482013-02-12 21:45:24 +000044 if (gpu->getCaps().shaderDerivativeSupport()) {
45 fVertexEdgeType = (GrDrawState::VertexEdgeType)
46 random->nextULessThan(GrDrawState::kVertexEdgeTypeCnt);
47 fDiscardIfOutsideEdge = random->nextBool();
48 } else {
49 fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
50 fDiscardIfOutsideEdge = false;
51 }
52 }
53
54 if (gpu->getCaps().dualSourceBlendingSupport()) {
55 fDualSrcOutput = random->nextULessThan(kDualSrcOutputCnt);
56 } else {
57 fDualSrcOutput = kNone_DualSrcOutput;
58 }
59
jvanverth@google.com39768252013-02-14 15:25:44 +000060 bool useOnce = false;
bsalomon@google.com91207482013-02-12 21:45:24 +000061 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
62 if (NULL != stages[s].getEffect()) {
63 const GrBackendEffectFactory& factory = (*stages[s].getEffect())->getFactory();
64 fEffectKeys[s] = factory.glEffectKey(stages[s], gpu->glCaps());
65 // use separate tex coords?
jvanverth@google.com39768252013-02-14 15:25:44 +000066 if (!useOnce && random->nextBool()) {
jvanverth@google.com9b855c72013-03-01 18:21:22 +000067 fAttribBindings |= GrDrawState::ExplicitTexCoordAttribBindingsBit(s);
jvanverth@google.com39768252013-02-14 15:25:44 +000068 useOnce = true;
bsalomon@google.com91207482013-02-12 21:45:24 +000069 }
70 }
71 }
jvanverth@google.com9b855c72013-03-01 18:21:22 +000072
73 int attributeIndex = 0;
74 fPositionAttributeIndex = attributeIndex;
75 ++attributeIndex;
76 if (fColorInput || (fAttribBindings & GrDrawState::kColor_AttribBindingsBit)) {
77 fColorAttributeIndex = attributeIndex;
78 ++attributeIndex;
79 }
80 if (fCoverageInput || (fAttribBindings & GrDrawState::kCoverage_AttribBindingsBit)) {
81 fCoverageAttributeIndex = attributeIndex;
82 ++attributeIndex;
83 }
84 if (fAttribBindings & GrDrawState::kEdge_AttribBindingsBit) {
85 fEdgeAttributeIndex = attributeIndex;
86 ++attributeIndex;
87 }
88 if (GrDrawState::AttributesBindExplicitTexCoords(fAttribBindings)) {
89 fTexCoordAttributeIndex = attributeIndex;
90 ++attributeIndex;
91 }
bsalomon@google.com91207482013-02-12 21:45:24 +000092}
93
bsalomon@google.com042a2862013-02-04 18:39:24 +000094bool GrGpuGL::programUnitTest(int maxStages) {
95
96 maxStages = GrMin(maxStages, (int)GrDrawState::kNumStages);
bsalomon@google.comc3841b92012-08-02 18:11:43 +000097
bsalomon@google.comd4726202012-08-03 14:34:46 +000098 GrTextureDesc dummyDesc;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +000099 dummyDesc.fConfig = kSkia8888_GrPixelConfig;
bsalomon@google.comd4726202012-08-03 14:34:46 +0000100 dummyDesc.fWidth = 34;
101 dummyDesc.fHeight = 18;
102 SkAutoTUnref<GrTexture> dummyTexture1(this->createTexture(dummyDesc, NULL, 0));
103 dummyDesc.fConfig = kAlpha_8_GrPixelConfig;
104 dummyDesc.fWidth = 16;
105 dummyDesc.fHeight = 22;
106 SkAutoTUnref<GrTexture> dummyTexture2(this->createTexture(dummyDesc, NULL, 0));
107
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000108 static const int NUM_TESTS = 512;
109
bsalomon@google.com91207482013-02-12 21:45:24 +0000110 SkMWCRandom random;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000111 for (int t = 0; t < NUM_TESTS; ++t) {
112
113#if 0
114 GrPrintf("\nTest Program %d\n-------------\n", t);
115 static const int stop = -1;
116 if (t == stop) {
117 int breakpointhere = 9;
118 }
119#endif
120
bsalomon@google.com91207482013-02-12 21:45:24 +0000121 GrGLProgram::Desc pdesc;
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000122 GrEffectStage stages[GrDrawState::kNumStages];
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000123
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.comd4726202012-08-03 14:34:46 +0000127 GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()};
bsalomon@google.com73a96942013-02-13 16:31:19 +0000128 SkAutoTUnref<const GrEffectRef> effect(GrEffectTestFactory::CreateStage(
129 &random,
130 this->getContext(),
131 dummyTextures));
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000132 stages[s].setEffect(effect.get());
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000133 }
134 }
bsalomon@google.com91207482013-02-12 21:45:24 +0000135 pdesc.setRandom(&random, this, stages);
136
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000137 const GrEffectStage* stagePtrs[GrDrawState::kNumStages];
138 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
139 stagePtrs[s] = &stages[s];
140 }
robertphillips@google.com6177e692013-02-28 20:16:25 +0000141 SkAutoTUnref<GrGLProgram> program(GrGLProgram::Create(this->glContext(),
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000142 pdesc,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000143 stagePtrs));
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000144 if (NULL == program.get()) {
145 return false;
146 }
147 }
148 return true;
149}
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000150
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000151static void GLProgramsTest(skiatest::Reporter* reporter, GrContextFactory* factory) {
152 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
153 GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(type));
154 if (NULL != context) {
bsalomon@google.com042a2862013-02-04 18:39:24 +0000155 GrGpuGL* gpu = static_cast<GrGpuGL*>(context->getGpu());
156 int maxStages = GrDrawState::kNumStages;
157#if SK_ANGLE
158 // Some long shaders run out of temporary registers in the D3D compiler on ANGLE.
159 if (type == GrContextFactory::kANGLE_GLContextType) {
160 maxStages = 3;
161 }
162#endif
163 REPORTER_ASSERT(reporter, gpu->programUnitTest(maxStages));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000164 }
165 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000166}
167
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000168#include "TestClassDef.h"
169DEFINE_GPUTESTCLASS("GLPrograms", GLProgramsTestClass, GLProgramsTest)
170
rmistry@google.comd6176b02012-08-23 18:14:13 +0000171// 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 +0000172// 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 +0000173// in the unit that could pass pointers to functions from the unit out to other translation units!
174// We force some of the effects that would otherwise be discarded to link here.
175
176#include "SkLightingImageFilter.h"
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000177#include "SkMagnifierImageFilter.h"
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000178#include "SkColorMatrixFilter.h"
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000179
180void forceLinking();
181
182void forceLinking() {
183 SkLightingImageFilter::CreateDistantLitDiffuse(SkPoint3(0,0,0), 0, 0, 0);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000184 SkMagnifierImageFilter mag(SkRect::MakeWH(SK_Scalar1, SK_Scalar1), SK_Scalar1);
bsalomon@google.comadc65362013-01-28 14:26:09 +0000185 GrConfigConversionEffect::Create(NULL,
186 false,
187 GrConfigConversionEffect::kNone_PMConversion,
188 SkMatrix::I());
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000189 SkScalar matrix[20];
190 SkColorMatrixFilter cmf(matrix);
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000191}
192
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000193#endif