blob: ad3d2c650b0e864890c49abf5bc22386f2050eff [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
23namespace {
24
bsalomon@google.com91207482013-02-12 21:45:24 +000025const GrEffectRef* create_random_effect(SkMWCRandom* random,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000026 GrContext* context,
27 GrTexture* dummyTextures[]) {
bsalomon@google.com91207482013-02-12 21:45:24 +000028 // TODO: Make GrTestFactory use SkMWCRandom and simply pass along param random
bsalomon@google.comd4726202012-08-03 14:34:46 +000029 SkRandom sk_random;
30 sk_random.setSeed(random->nextU());
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000031 GrEffectRef* effect = GrEffectTestFactory::CreateStage(&sk_random, context, dummyTextures);
bsalomon@google.com021fc732012-10-25 12:47:42 +000032 GrAssert(effect);
33 return effect;
bsalomon@google.comc3841b92012-08-02 18:11:43 +000034}
35}
36
bsalomon@google.com91207482013-02-12 21:45:24 +000037void GrGLProgram::Desc::setRandom(SkMWCRandom* random,
38 const GrGpuGL* gpu,
39 const GrEffectStage stages[GrDrawState::kNumStages]) {
40 fVertexLayout = 0;
41 fEmitsPointSize = random->nextBool();
42 fColorInput = random->nextULessThan(kColorInputCnt);
43 fCoverageInput = random->nextULessThan(kColorInputCnt);
44
45 fColorFilterXfermode = random->nextULessThan(SkXfermode::kCoeffModesCnt);
46
47 fFirstCoverageStage = random->nextULessThan(GrDrawState::kNumStages);
48
49 fVertexLayout |= random->nextBool() ? GrDrawState::kCoverage_VertexLayoutBit : 0;
50
51#if GR_GL_EXPERIMENTAL_GS
52 fExperimentalGS = gpu->getCaps().geometryShaderSupport() && random->nextBool();
53#endif
54
55 bool edgeAA = random->nextBool();
56 if (edgeAA) {
57 fVertexLayout |= GrDrawState::kEdge_VertexLayoutBit;
58 if (gpu->getCaps().shaderDerivativeSupport()) {
59 fVertexEdgeType = (GrDrawState::VertexEdgeType)
60 random->nextULessThan(GrDrawState::kVertexEdgeTypeCnt);
61 fDiscardIfOutsideEdge = random->nextBool();
62 } else {
63 fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
64 fDiscardIfOutsideEdge = false;
65 }
66 }
67
68 if (gpu->getCaps().dualSourceBlendingSupport()) {
69 fDualSrcOutput = random->nextULessThan(kDualSrcOutputCnt);
70 } else {
71 fDualSrcOutput = kNone_DualSrcOutput;
72 }
73
74 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
75 if (NULL != stages[s].getEffect()) {
76 const GrBackendEffectFactory& factory = (*stages[s].getEffect())->getFactory();
77 fEffectKeys[s] = factory.glEffectKey(stages[s], gpu->glCaps());
78 // use separate tex coords?
79 if (random->nextBool()) {
80 int t = random->nextULessThan(GrDrawState::kMaxTexCoords);
81 fVertexLayout |= GrDrawState::StageTexCoordVertexLayoutBit(s, t);
82 }
83 }
84 }
85}
86
bsalomon@google.com042a2862013-02-04 18:39:24 +000087bool GrGpuGL::programUnitTest(int maxStages) {
88
89 maxStages = GrMin(maxStages, (int)GrDrawState::kNumStages);
bsalomon@google.comc3841b92012-08-02 18:11:43 +000090
bsalomon@google.comd4726202012-08-03 14:34:46 +000091 GrTextureDesc dummyDesc;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +000092 dummyDesc.fConfig = kSkia8888_GrPixelConfig;
bsalomon@google.comd4726202012-08-03 14:34:46 +000093 dummyDesc.fWidth = 34;
94 dummyDesc.fHeight = 18;
95 SkAutoTUnref<GrTexture> dummyTexture1(this->createTexture(dummyDesc, NULL, 0));
96 dummyDesc.fConfig = kAlpha_8_GrPixelConfig;
97 dummyDesc.fWidth = 16;
98 dummyDesc.fHeight = 22;
99 SkAutoTUnref<GrTexture> dummyTexture2(this->createTexture(dummyDesc, NULL, 0));
100
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000101 static const int NUM_TESTS = 512;
102
bsalomon@google.com91207482013-02-12 21:45:24 +0000103 SkMWCRandom random;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000104 for (int t = 0; t < NUM_TESTS; ++t) {
105
106#if 0
107 GrPrintf("\nTest Program %d\n-------------\n", t);
108 static const int stop = -1;
109 if (t == stop) {
110 int breakpointhere = 9;
111 }
112#endif
113
bsalomon@google.com91207482013-02-12 21:45:24 +0000114 GrGLProgram::Desc pdesc;
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000115 GrEffectStage stages[GrDrawState::kNumStages];
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000116
bsalomon@google.com1d1d4242013-02-05 15:44:21 +0000117 for (int s = 0; s < maxStages; ++s) {
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000118 // enable the stage?
bsalomon@google.com91207482013-02-12 21:45:24 +0000119 if (random.nextBool()) {
bsalomon@google.comd4726202012-08-03 14:34:46 +0000120 GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()};
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000121 SkAutoTUnref<const GrEffectRef> effect(create_random_effect(&random,
bsalomon@google.comdbe49f72012-11-05 16:36:02 +0000122 getContext(),
123 dummyTextures));
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000124 stages[s].setEffect(effect.get());
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000125 }
126 }
bsalomon@google.com91207482013-02-12 21:45:24 +0000127 pdesc.setRandom(&random, this, stages);
128
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000129 const GrEffectStage* stagePtrs[GrDrawState::kNumStages];
130 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
131 stagePtrs[s] = &stages[s];
132 }
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000133 SkAutoTUnref<GrGLProgram> program(GrGLProgram::Create(this->glContextInfo(),
134 pdesc,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000135 stagePtrs));
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000136 if (NULL == program.get()) {
137 return false;
138 }
139 }
140 return true;
141}
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000142
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000143static void GLProgramsTest(skiatest::Reporter* reporter, GrContextFactory* factory) {
144 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
145 GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(type));
146 if (NULL != context) {
bsalomon@google.com042a2862013-02-04 18:39:24 +0000147 GrGpuGL* gpu = static_cast<GrGpuGL*>(context->getGpu());
148 int maxStages = GrDrawState::kNumStages;
149#if SK_ANGLE
150 // Some long shaders run out of temporary registers in the D3D compiler on ANGLE.
151 if (type == GrContextFactory::kANGLE_GLContextType) {
152 maxStages = 3;
153 }
154#endif
155 REPORTER_ASSERT(reporter, gpu->programUnitTest(maxStages));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000156 }
157 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000158}
159
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000160#include "TestClassDef.h"
161DEFINE_GPUTESTCLASS("GLPrograms", GLProgramsTestClass, GLProgramsTest)
162
rmistry@google.comd6176b02012-08-23 18:14:13 +0000163// 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 +0000164// 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 +0000165// in the unit that could pass pointers to functions from the unit out to other translation units!
166// We force some of the effects that would otherwise be discarded to link here.
167
168#include "SkLightingImageFilter.h"
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000169#include "SkMagnifierImageFilter.h"
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000170#include "SkColorMatrixFilter.h"
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000171
172void forceLinking();
173
174void forceLinking() {
175 SkLightingImageFilter::CreateDistantLitDiffuse(SkPoint3(0,0,0), 0, 0, 0);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000176 SkMagnifierImageFilter mag(SkRect::MakeWH(SK_Scalar1, SK_Scalar1), SK_Scalar1);
bsalomon@google.comadc65362013-01-28 14:26:09 +0000177 GrConfigConversionEffect::Create(NULL,
178 false,
179 GrConfigConversionEffect::kNone_PMConversion,
180 SkMatrix::I());
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000181 SkScalar matrix[20];
182 SkColorMatrixFilter cmf(matrix);
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000183}
184
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000185#endif