blob: d4873cada04e047b94bfd5a52aa723b7e2209ee3 [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]) {
26 fVertexLayout = 0;
27 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
35 fVertexLayout |= random->nextBool() ? GrDrawState::kCoverage_VertexLayoutBit : 0;
36
37#if GR_GL_EXPERIMENTAL_GS
38 fExperimentalGS = gpu->getCaps().geometryShaderSupport() && random->nextBool();
39#endif
40
41 bool edgeAA = random->nextBool();
42 if (edgeAA) {
43 fVertexLayout |= GrDrawState::kEdge_VertexLayoutBit;
44 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
60 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
61 if (NULL != stages[s].getEffect()) {
62 const GrBackendEffectFactory& factory = (*stages[s].getEffect())->getFactory();
63 fEffectKeys[s] = factory.glEffectKey(stages[s], gpu->glCaps());
64 // use separate tex coords?
65 if (random->nextBool()) {
66 int t = random->nextULessThan(GrDrawState::kMaxTexCoords);
67 fVertexLayout |= GrDrawState::StageTexCoordVertexLayoutBit(s, t);
68 }
69 }
70 }
71}
72
bsalomon@google.com042a2862013-02-04 18:39:24 +000073bool GrGpuGL::programUnitTest(int maxStages) {
74
75 maxStages = GrMin(maxStages, (int)GrDrawState::kNumStages);
bsalomon@google.comc3841b92012-08-02 18:11:43 +000076
bsalomon@google.comd4726202012-08-03 14:34:46 +000077 GrTextureDesc dummyDesc;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +000078 dummyDesc.fConfig = kSkia8888_GrPixelConfig;
bsalomon@google.comd4726202012-08-03 14:34:46 +000079 dummyDesc.fWidth = 34;
80 dummyDesc.fHeight = 18;
81 SkAutoTUnref<GrTexture> dummyTexture1(this->createTexture(dummyDesc, NULL, 0));
82 dummyDesc.fConfig = kAlpha_8_GrPixelConfig;
83 dummyDesc.fWidth = 16;
84 dummyDesc.fHeight = 22;
85 SkAutoTUnref<GrTexture> dummyTexture2(this->createTexture(dummyDesc, NULL, 0));
86
bsalomon@google.comc3841b92012-08-02 18:11:43 +000087 static const int NUM_TESTS = 512;
88
bsalomon@google.com91207482013-02-12 21:45:24 +000089 SkMWCRandom random;
bsalomon@google.comc3841b92012-08-02 18:11:43 +000090 for (int t = 0; t < NUM_TESTS; ++t) {
91
92#if 0
93 GrPrintf("\nTest Program %d\n-------------\n", t);
94 static const int stop = -1;
95 if (t == stop) {
96 int breakpointhere = 9;
97 }
98#endif
99
bsalomon@google.com91207482013-02-12 21:45:24 +0000100 GrGLProgram::Desc pdesc;
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000101 GrEffectStage stages[GrDrawState::kNumStages];
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000102
bsalomon@google.com1d1d4242013-02-05 15:44:21 +0000103 for (int s = 0; s < maxStages; ++s) {
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000104 // enable the stage?
bsalomon@google.com91207482013-02-12 21:45:24 +0000105 if (random.nextBool()) {
bsalomon@google.comd4726202012-08-03 14:34:46 +0000106 GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()};
bsalomon@google.com73a96942013-02-13 16:31:19 +0000107 SkAutoTUnref<const GrEffectRef> effect(GrEffectTestFactory::CreateStage(
108 &random,
109 this->getContext(),
110 dummyTextures));
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000111 stages[s].setEffect(effect.get());
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000112 }
113 }
bsalomon@google.com91207482013-02-12 21:45:24 +0000114 pdesc.setRandom(&random, this, stages);
115
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000116 const GrEffectStage* stagePtrs[GrDrawState::kNumStages];
117 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
118 stagePtrs[s] = &stages[s];
119 }
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000120 SkAutoTUnref<GrGLProgram> program(GrGLProgram::Create(this->glContextInfo(),
121 pdesc,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000122 stagePtrs));
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000123 if (NULL == program.get()) {
124 return false;
125 }
126 }
127 return true;
128}
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000129
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000130static void GLProgramsTest(skiatest::Reporter* reporter, GrContextFactory* factory) {
131 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
132 GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(type));
133 if (NULL != context) {
bsalomon@google.com042a2862013-02-04 18:39:24 +0000134 GrGpuGL* gpu = static_cast<GrGpuGL*>(context->getGpu());
135 int maxStages = GrDrawState::kNumStages;
136#if SK_ANGLE
137 // Some long shaders run out of temporary registers in the D3D compiler on ANGLE.
138 if (type == GrContextFactory::kANGLE_GLContextType) {
139 maxStages = 3;
140 }
141#endif
142 REPORTER_ASSERT(reporter, gpu->programUnitTest(maxStages));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000143 }
144 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000145}
146
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000147#include "TestClassDef.h"
148DEFINE_GPUTESTCLASS("GLPrograms", GLProgramsTestClass, GLProgramsTest)
149
rmistry@google.comd6176b02012-08-23 18:14:13 +0000150// 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 +0000151// 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 +0000152// in the unit that could pass pointers to functions from the unit out to other translation units!
153// We force some of the effects that would otherwise be discarded to link here.
154
155#include "SkLightingImageFilter.h"
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000156#include "SkMagnifierImageFilter.h"
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000157#include "SkColorMatrixFilter.h"
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000158
159void forceLinking();
160
161void forceLinking() {
162 SkLightingImageFilter::CreateDistantLitDiffuse(SkPoint3(0,0,0), 0, 0, 0);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000163 SkMagnifierImageFilter mag(SkRect::MakeWH(SK_Scalar1, SK_Scalar1), SK_Scalar1);
bsalomon@google.comadc65362013-01-28 14:26:09 +0000164 GrConfigConversionEffect::Create(NULL,
165 false,
166 GrConfigConversionEffect::kNone_PMConversion,
167 SkMatrix::I());
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000168 SkScalar matrix[20];
169 SkColorMatrixFilter cmf(matrix);
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000170}
171
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000172#endif