blob: 3106377d84ec2ea61c05c95aee65e06230a37bee [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.coma04e8e82012-08-27 12:53:13 +000017#include "effects/GrConfigConversionEffect.h"
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +000018
tfarina@chromium.org223137f2012-11-21 22:38:36 +000019#include "SkRandom.h"
bsalomon@google.comc3841b92012-08-02 18:11:43 +000020#include "Test.h"
21
22namespace {
23
tfarina@chromium.org223137f2012-11-21 22:38:36 +000024// SkRandoms nextU() values have patterns in the low bits
bsalomon@google.comc3841b92012-08-02 18:11:43 +000025// So using nextU() % array_count might never take some values.
tfarina@chromium.org223137f2012-11-21 22:38:36 +000026int random_int(SkRandom* r, int count) {
bsalomon@google.comc3841b92012-08-02 18:11:43 +000027 return (int)(r->nextF() * count);
28}
29
tfarina@chromium.org223137f2012-11-21 22:38:36 +000030bool random_bool(SkRandom* r) {
bsalomon@google.comc3841b92012-08-02 18:11:43 +000031 return r->nextF() > .5f;
32}
33
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000034const GrEffectRef* create_random_effect(SkRandom* random,
35 GrContext* context,
36 GrTexture* dummyTextures[]) {
bsalomon@google.comc3841b92012-08-02 18:11:43 +000037
bsalomon@google.comd4726202012-08-03 14:34:46 +000038 SkRandom sk_random;
39 sk_random.setSeed(random->nextU());
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000040 GrEffectRef* effect = GrEffectTestFactory::CreateStage(&sk_random, context, dummyTextures);
bsalomon@google.com021fc732012-10-25 12:47:42 +000041 GrAssert(effect);
42 return effect;
bsalomon@google.comc3841b92012-08-02 18:11:43 +000043}
44}
45
46bool GrGpuGL::programUnitTest() {
47
bsalomon@google.comd4726202012-08-03 14:34:46 +000048 GrTextureDesc dummyDesc;
49 dummyDesc.fConfig = kSkia8888_PM_GrPixelConfig;
50 dummyDesc.fWidth = 34;
51 dummyDesc.fHeight = 18;
52 SkAutoTUnref<GrTexture> dummyTexture1(this->createTexture(dummyDesc, NULL, 0));
53 dummyDesc.fConfig = kAlpha_8_GrPixelConfig;
54 dummyDesc.fWidth = 16;
55 dummyDesc.fHeight = 22;
56 SkAutoTUnref<GrTexture> dummyTexture2(this->createTexture(dummyDesc, NULL, 0));
57
bsalomon@google.comc3841b92012-08-02 18:11:43 +000058 static const int NUM_TESTS = 512;
59
tfarina@chromium.org223137f2012-11-21 22:38:36 +000060 SkRandom random;
bsalomon@google.comc3841b92012-08-02 18:11:43 +000061 for (int t = 0; t < NUM_TESTS; ++t) {
62
63#if 0
64 GrPrintf("\nTest Program %d\n-------------\n", t);
65 static const int stop = -1;
66 if (t == stop) {
67 int breakpointhere = 9;
68 }
69#endif
70
71 ProgramDesc pdesc;
72 pdesc.fVertexLayout = 0;
73 pdesc.fEmitsPointSize = random.nextF() > .5f;
74 pdesc.fColorInput = random_int(&random, ProgramDesc::kColorInputCnt);
75 pdesc.fCoverageInput = random_int(&random, ProgramDesc::kColorInputCnt);
76
77 pdesc.fColorFilterXfermode = random_int(&random, SkXfermode::kCoeffModesCnt);
78
79 pdesc.fFirstCoverageStage = random_int(&random, GrDrawState::kNumStages);
80
81 pdesc.fVertexLayout |= random_bool(&random) ?
jvanverth@google.comcc782382013-01-28 20:39:48 +000082 GrDrawState::kCoverage_VertexLayoutBit :
bsalomon@google.comc3841b92012-08-02 18:11:43 +000083 0;
84
85#if GR_GL_EXPERIMENTAL_GS
bsalomon@google.comf6601872012-08-28 21:11:35 +000086 pdesc.fExperimentalGS = this->getCaps().geometryShaderSupport() &&
bsalomon@google.comc3841b92012-08-02 18:11:43 +000087 random_bool(&random);
88#endif
bsalomon@google.comc3841b92012-08-02 18:11:43 +000089
90 bool edgeAA = random_bool(&random);
91 if (edgeAA) {
jvanverth@google.comcc782382013-01-28 20:39:48 +000092 pdesc.fVertexLayout |= GrDrawState::kEdge_VertexLayoutBit;
bsalomon@google.comf6601872012-08-28 21:11:35 +000093 if (this->getCaps().shaderDerivativeSupport()) {
bsalomon@google.comc3841b92012-08-02 18:11:43 +000094 pdesc.fVertexEdgeType = (GrDrawState::VertexEdgeType) random_int(&random, GrDrawState::kVertexEdgeTypeCnt);
bsalomon@google.com45a15f52012-12-10 19:10:17 +000095 pdesc.fDiscardIfOutsideEdge = random.nextBool();
bsalomon@google.comc3841b92012-08-02 18:11:43 +000096 } else {
97 pdesc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.com45a15f52012-12-10 19:10:17 +000098 pdesc.fDiscardIfOutsideEdge = false;
bsalomon@google.comc3841b92012-08-02 18:11:43 +000099 }
100 } else {
101 }
102
bsalomon@google.comf6601872012-08-28 21:11:35 +0000103 if (this->getCaps().dualSourceBlendingSupport()) {
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000104 pdesc.fDualSrcOutput = random_int(&random, ProgramDesc::kDualSrcOutputCnt);
105 } else {
106 pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
107 }
108
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000109 GrEffectStage stages[GrDrawState::kNumStages];
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000110
111 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000112 // enable the stage?
113 if (random_bool(&random)) {
114 // use separate tex coords?
115 if (random_bool(&random)) {
116 int t = random_int(&random, GrDrawState::kMaxTexCoords);
jvanverth@google.comcc782382013-01-28 20:39:48 +0000117 pdesc.fVertexLayout |= GrDrawState::StageTexCoordVertexLayoutBit(s, t);
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000118 }
bsalomon@google.comdbe49f72012-11-05 16:36:02 +0000119 // use text-formatted verts?
120 if (random_bool(&random)) {
jvanverth@google.comcc782382013-01-28 20:39:48 +0000121 pdesc.fVertexLayout |= GrDrawState::kTextFormat_VertexLayoutBit;
bsalomon@google.comdbe49f72012-11-05 16:36:02 +0000122 }
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000123
bsalomon@google.comd4726202012-08-03 14:34:46 +0000124 GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()};
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000125 SkAutoTUnref<const GrEffectRef> effect(create_random_effect(&random,
bsalomon@google.comdbe49f72012-11-05 16:36:02 +0000126 getContext(),
127 dummyTextures));
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000128 stages[s].setEffect(effect.get());
129 if (NULL != stages[s].getEffect()) {
bsalomon@google.comdbe49f72012-11-05 16:36:02 +0000130 pdesc.fEffectKeys[s] =
bsalomon@google.com6340a412013-01-22 19:55:59 +0000131 (*stages[s].getEffect())->getFactory().glEffectKey(stages[s],
132 this->glCaps());
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000133 }
134 }
135 }
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000136 const GrEffectStage* stagePtrs[GrDrawState::kNumStages];
137 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
138 stagePtrs[s] = &stages[s];
139 }
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000140 SkAutoTUnref<GrGLProgram> program(GrGLProgram::Create(this->glContextInfo(),
141 pdesc,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000142 stagePtrs));
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000143 if (NULL == program.get()) {
144 return false;
145 }
146 }
147 return true;
148}
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000149
150static void GLProgramsTest(skiatest::Reporter* reporter, GrContext* context) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000151 GrGpuGL* shadersGpu = static_cast<GrGpuGL*>(context->getGpu());
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000152 REPORTER_ASSERT(reporter, shadersGpu->programUnitTest());
153}
154
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000155#include "TestClassDef.h"
156DEFINE_GPUTESTCLASS("GLPrograms", GLProgramsTestClass, GLProgramsTest)
157
rmistry@google.comd6176b02012-08-23 18:14:13 +0000158// 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 +0000159// 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 +0000160// in the unit that could pass pointers to functions from the unit out to other translation units!
161// We force some of the effects that would otherwise be discarded to link here.
162
163#include "SkLightingImageFilter.h"
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000164#include "SkMagnifierImageFilter.h"
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000165#include "SkColorMatrixFilter.h"
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000166
167void forceLinking();
168
169void forceLinking() {
170 SkLightingImageFilter::CreateDistantLitDiffuse(SkPoint3(0,0,0), 0, 0, 0);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000171 SkMagnifierImageFilter mag(SkRect::MakeWH(SK_Scalar1, SK_Scalar1), SK_Scalar1);
bsalomon@google.comadc65362013-01-28 14:26:09 +0000172 GrConfigConversionEffect::Create(NULL,
173 false,
174 GrConfigConversionEffect::kNone_PMConversion,
175 SkMatrix::I());
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000176 SkScalar matrix[20];
177 SkColorMatrixFilter cmf(matrix);
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000178}
179
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000180#endif