blob: 0c660d9e3d29422530091a3ba9f730d7105cab9a [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
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()) {
67 fVertexLayout |= GrDrawState::StageTexCoordVertexLayoutBit(s);
68 useOnce = true;
bsalomon@google.com91207482013-02-12 21:45:24 +000069 }
70 }
71 }
72}
73
bsalomon@google.com042a2862013-02-04 18:39:24 +000074bool GrGpuGL::programUnitTest(int maxStages) {
75
76 maxStages = GrMin(maxStages, (int)GrDrawState::kNumStages);
bsalomon@google.comc3841b92012-08-02 18:11:43 +000077
bsalomon@google.comd4726202012-08-03 14:34:46 +000078 GrTextureDesc dummyDesc;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +000079 dummyDesc.fConfig = kSkia8888_GrPixelConfig;
bsalomon@google.comd4726202012-08-03 14:34:46 +000080 dummyDesc.fWidth = 34;
81 dummyDesc.fHeight = 18;
82 SkAutoTUnref<GrTexture> dummyTexture1(this->createTexture(dummyDesc, NULL, 0));
83 dummyDesc.fConfig = kAlpha_8_GrPixelConfig;
84 dummyDesc.fWidth = 16;
85 dummyDesc.fHeight = 22;
86 SkAutoTUnref<GrTexture> dummyTexture2(this->createTexture(dummyDesc, NULL, 0));
87
bsalomon@google.comc3841b92012-08-02 18:11:43 +000088 static const int NUM_TESTS = 512;
89
bsalomon@google.com91207482013-02-12 21:45:24 +000090 SkMWCRandom random;
bsalomon@google.comc3841b92012-08-02 18:11:43 +000091 for (int t = 0; t < NUM_TESTS; ++t) {
92
93#if 0
94 GrPrintf("\nTest Program %d\n-------------\n", t);
95 static const int stop = -1;
96 if (t == stop) {
97 int breakpointhere = 9;
98 }
99#endif
100
bsalomon@google.com91207482013-02-12 21:45:24 +0000101 GrGLProgram::Desc pdesc;
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000102 GrEffectStage stages[GrDrawState::kNumStages];
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000103
bsalomon@google.com1d1d4242013-02-05 15:44:21 +0000104 for (int s = 0; s < maxStages; ++s) {
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000105 // enable the stage?
bsalomon@google.com91207482013-02-12 21:45:24 +0000106 if (random.nextBool()) {
bsalomon@google.comd4726202012-08-03 14:34:46 +0000107 GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()};
bsalomon@google.com73a96942013-02-13 16:31:19 +0000108 SkAutoTUnref<const GrEffectRef> effect(GrEffectTestFactory::CreateStage(
109 &random,
110 this->getContext(),
111 dummyTextures));
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000112 stages[s].setEffect(effect.get());
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000113 }
114 }
bsalomon@google.com91207482013-02-12 21:45:24 +0000115 pdesc.setRandom(&random, this, stages);
116
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000117 const GrEffectStage* stagePtrs[GrDrawState::kNumStages];
118 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
119 stagePtrs[s] = &stages[s];
120 }
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000121 SkAutoTUnref<GrGLProgram> program(GrGLProgram::Create(this->glContextInfo(),
122 pdesc,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000123 stagePtrs));
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000124 if (NULL == program.get()) {
125 return false;
126 }
127 }
128 return true;
129}
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000130
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000131static void GLProgramsTest(skiatest::Reporter* reporter, GrContextFactory* factory) {
132 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
133 GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(type));
134 if (NULL != context) {
bsalomon@google.com042a2862013-02-04 18:39:24 +0000135 GrGpuGL* gpu = static_cast<GrGpuGL*>(context->getGpu());
136 int maxStages = GrDrawState::kNumStages;
137#if SK_ANGLE
138 // Some long shaders run out of temporary registers in the D3D compiler on ANGLE.
139 if (type == GrContextFactory::kANGLE_GLContextType) {
140 maxStages = 3;
141 }
142#endif
143 REPORTER_ASSERT(reporter, gpu->programUnitTest(maxStages));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000144 }
145 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000146}
147
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000148#include "TestClassDef.h"
149DEFINE_GPUTESTCLASS("GLPrograms", GLProgramsTestClass, GLProgramsTest)
150
rmistry@google.comd6176b02012-08-23 18:14:13 +0000151// 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 +0000152// 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 +0000153// in the unit that could pass pointers to functions from the unit out to other translation units!
154// We force some of the effects that would otherwise be discarded to link here.
155
156#include "SkLightingImageFilter.h"
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000157#include "SkMagnifierImageFilter.h"
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000158#include "SkColorMatrixFilter.h"
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000159
160void forceLinking();
161
162void forceLinking() {
163 SkLightingImageFilter::CreateDistantLitDiffuse(SkPoint3(0,0,0), 0, 0, 0);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000164 SkMagnifierImageFilter mag(SkRect::MakeWH(SK_Scalar1, SK_Scalar1), SK_Scalar1);
bsalomon@google.comadc65362013-01-28 14:26:09 +0000165 GrConfigConversionEffect::Create(NULL,
166 false,
167 GrConfigConversionEffect::kNone_PMConversion,
168 SkMatrix::I());
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000169 SkScalar matrix[20];
170 SkColorMatrixFilter cmf(matrix);
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000171}
172
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000173#endif