blob: 1d8ec35dc91c3356503fccf7624306ea8f94d4ef [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
tfarina@chromium.org223137f2012-11-21 22:38:36 +000025// SkRandoms nextU() values have patterns in the low bits
bsalomon@google.comc3841b92012-08-02 18:11:43 +000026// So using nextU() % array_count might never take some values.
tfarina@chromium.org223137f2012-11-21 22:38:36 +000027int random_int(SkRandom* r, int count) {
bsalomon@google.comc3841b92012-08-02 18:11:43 +000028 return (int)(r->nextF() * count);
29}
30
tfarina@chromium.org223137f2012-11-21 22:38:36 +000031bool random_bool(SkRandom* r) {
bsalomon@google.comc3841b92012-08-02 18:11:43 +000032 return r->nextF() > .5f;
33}
34
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000035const GrEffectRef* create_random_effect(SkRandom* random,
36 GrContext* context,
37 GrTexture* dummyTextures[]) {
bsalomon@google.comc3841b92012-08-02 18:11:43 +000038
bsalomon@google.comd4726202012-08-03 14:34:46 +000039 SkRandom sk_random;
40 sk_random.setSeed(random->nextU());
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000041 GrEffectRef* effect = GrEffectTestFactory::CreateStage(&sk_random, context, dummyTextures);
bsalomon@google.com021fc732012-10-25 12:47:42 +000042 GrAssert(effect);
43 return effect;
bsalomon@google.comc3841b92012-08-02 18:11:43 +000044}
45}
46
bsalomon@google.com042a2862013-02-04 18:39:24 +000047bool GrGpuGL::programUnitTest(int maxStages) {
48
49 maxStages = GrMin(maxStages, (int)GrDrawState::kNumStages);
bsalomon@google.comc3841b92012-08-02 18:11:43 +000050
bsalomon@google.comd4726202012-08-03 14:34:46 +000051 GrTextureDesc dummyDesc;
52 dummyDesc.fConfig = kSkia8888_PM_GrPixelConfig;
53 dummyDesc.fWidth = 34;
54 dummyDesc.fHeight = 18;
55 SkAutoTUnref<GrTexture> dummyTexture1(this->createTexture(dummyDesc, NULL, 0));
56 dummyDesc.fConfig = kAlpha_8_GrPixelConfig;
57 dummyDesc.fWidth = 16;
58 dummyDesc.fHeight = 22;
59 SkAutoTUnref<GrTexture> dummyTexture2(this->createTexture(dummyDesc, NULL, 0));
60
bsalomon@google.comc3841b92012-08-02 18:11:43 +000061 static const int NUM_TESTS = 512;
62
tfarina@chromium.org223137f2012-11-21 22:38:36 +000063 SkRandom random;
bsalomon@google.comc3841b92012-08-02 18:11:43 +000064 for (int t = 0; t < NUM_TESTS; ++t) {
65
66#if 0
67 GrPrintf("\nTest Program %d\n-------------\n", t);
68 static const int stop = -1;
69 if (t == stop) {
70 int breakpointhere = 9;
71 }
72#endif
73
74 ProgramDesc pdesc;
75 pdesc.fVertexLayout = 0;
76 pdesc.fEmitsPointSize = random.nextF() > .5f;
77 pdesc.fColorInput = random_int(&random, ProgramDesc::kColorInputCnt);
78 pdesc.fCoverageInput = random_int(&random, ProgramDesc::kColorInputCnt);
79
80 pdesc.fColorFilterXfermode = random_int(&random, SkXfermode::kCoeffModesCnt);
81
82 pdesc.fFirstCoverageStage = random_int(&random, GrDrawState::kNumStages);
83
84 pdesc.fVertexLayout |= random_bool(&random) ?
jvanverth@google.comcc782382013-01-28 20:39:48 +000085 GrDrawState::kCoverage_VertexLayoutBit :
bsalomon@google.comc3841b92012-08-02 18:11:43 +000086 0;
87
88#if GR_GL_EXPERIMENTAL_GS
bsalomon@google.comf6601872012-08-28 21:11:35 +000089 pdesc.fExperimentalGS = this->getCaps().geometryShaderSupport() &&
bsalomon@google.comc3841b92012-08-02 18:11:43 +000090 random_bool(&random);
91#endif
bsalomon@google.comc3841b92012-08-02 18:11:43 +000092
93 bool edgeAA = random_bool(&random);
94 if (edgeAA) {
jvanverth@google.comcc782382013-01-28 20:39:48 +000095 pdesc.fVertexLayout |= GrDrawState::kEdge_VertexLayoutBit;
bsalomon@google.comf6601872012-08-28 21:11:35 +000096 if (this->getCaps().shaderDerivativeSupport()) {
bsalomon@google.comc3841b92012-08-02 18:11:43 +000097 pdesc.fVertexEdgeType = (GrDrawState::VertexEdgeType) random_int(&random, GrDrawState::kVertexEdgeTypeCnt);
bsalomon@google.com45a15f52012-12-10 19:10:17 +000098 pdesc.fDiscardIfOutsideEdge = random.nextBool();
bsalomon@google.comc3841b92012-08-02 18:11:43 +000099 } else {
100 pdesc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000101 pdesc.fDiscardIfOutsideEdge = false;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000102 }
103 } else {
104 }
105
bsalomon@google.comf6601872012-08-28 21:11:35 +0000106 if (this->getCaps().dualSourceBlendingSupport()) {
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000107 pdesc.fDualSrcOutput = random_int(&random, ProgramDesc::kDualSrcOutputCnt);
108 } else {
109 pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
110 }
111
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000112 GrEffectStage stages[GrDrawState::kNumStages];
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000113
bsalomon@google.com1d1d4242013-02-05 15:44:21 +0000114 for (int s = 0; s < maxStages; ++s) {
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000115 // enable the stage?
116 if (random_bool(&random)) {
117 // use separate tex coords?
118 if (random_bool(&random)) {
119 int t = random_int(&random, GrDrawState::kMaxTexCoords);
jvanverth@google.comcc782382013-01-28 20:39:48 +0000120 pdesc.fVertexLayout |= GrDrawState::StageTexCoordVertexLayoutBit(s, t);
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000121 }
bsalomon@google.comdbe49f72012-11-05 16:36:02 +0000122 // use text-formatted verts?
123 if (random_bool(&random)) {
jvanverth@google.comcc782382013-01-28 20:39:48 +0000124 pdesc.fVertexLayout |= GrDrawState::kTextFormat_VertexLayoutBit;
bsalomon@google.comdbe49f72012-11-05 16:36:02 +0000125 }
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000126
bsalomon@google.comd4726202012-08-03 14:34:46 +0000127 GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()};
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000128 SkAutoTUnref<const GrEffectRef> effect(create_random_effect(&random,
bsalomon@google.comdbe49f72012-11-05 16:36:02 +0000129 getContext(),
130 dummyTextures));
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000131 stages[s].setEffect(effect.get());
132 if (NULL != stages[s].getEffect()) {
bsalomon@google.comdbe49f72012-11-05 16:36:02 +0000133 pdesc.fEffectKeys[s] =
bsalomon@google.com6340a412013-01-22 19:55:59 +0000134 (*stages[s].getEffect())->getFactory().glEffectKey(stages[s],
135 this->glCaps());
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000136 }
137 }
138 }
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000139 const GrEffectStage* stagePtrs[GrDrawState::kNumStages];
140 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
141 stagePtrs[s] = &stages[s];
142 }
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000143 SkAutoTUnref<GrGLProgram> program(GrGLProgram::Create(this->glContextInfo(),
144 pdesc,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000145 stagePtrs));
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000146 if (NULL == program.get()) {
147 return false;
148 }
149 }
150 return true;
151}
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000152
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000153static void GLProgramsTest(skiatest::Reporter* reporter, GrContextFactory* factory) {
154 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
155 GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(type));
156 if (NULL != context) {
bsalomon@google.com042a2862013-02-04 18:39:24 +0000157 GrGpuGL* gpu = static_cast<GrGpuGL*>(context->getGpu());
158 int maxStages = GrDrawState::kNumStages;
159#if SK_ANGLE
160 // Some long shaders run out of temporary registers in the D3D compiler on ANGLE.
161 if (type == GrContextFactory::kANGLE_GLContextType) {
162 maxStages = 3;
163 }
164#endif
165 REPORTER_ASSERT(reporter, gpu->programUnitTest(maxStages));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000166 }
167 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000168}
169
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000170#include "TestClassDef.h"
171DEFINE_GPUTESTCLASS("GLPrograms", GLProgramsTestClass, GLProgramsTest)
172
rmistry@google.comd6176b02012-08-23 18:14:13 +0000173// 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 +0000174// 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 +0000175// in the unit that could pass pointers to functions from the unit out to other translation units!
176// We force some of the effects that would otherwise be discarded to link here.
177
178#include "SkLightingImageFilter.h"
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000179#include "SkMagnifierImageFilter.h"
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000180#include "SkColorMatrixFilter.h"
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000181
182void forceLinking();
183
184void forceLinking() {
185 SkLightingImageFilter::CreateDistantLitDiffuse(SkPoint3(0,0,0), 0, 0, 0);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000186 SkMagnifierImageFilter mag(SkRect::MakeWH(SK_Scalar1, SK_Scalar1), SK_Scalar1);
bsalomon@google.comadc65362013-01-28 14:26:09 +0000187 GrConfigConversionEffect::Create(NULL,
188 false,
189 GrConfigConversionEffect::kNone_PMConversion,
190 SkMatrix::I());
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000191 SkScalar matrix[20];
192 SkColorMatrixFilter cmf(matrix);
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000193}
194
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000195#endif