blob: 54f900ce9d694155cb815128d25a83eb6d1cb021 [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.comc3841b92012-08-02 18:11:43 +000016#include "GrProgramStageFactory.h"
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000017#include "effects/GrConfigConversionEffect.h"
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +000018
bsalomon@google.comc3841b92012-08-02 18:11:43 +000019#include "GrRandom.h"
20#include "Test.h"
21
22namespace {
23
24// GrRandoms nextU() values have patterns in the low bits
25// So using nextU() % array_count might never take some values.
26int random_int(GrRandom* r, int count) {
27 return (int)(r->nextF() * count);
28}
29
30// min is inclusive, max is exclusive
31int random_int(GrRandom* r, int min, int max) {
32 return (int)(r->nextF() * (max-min)) + min;
33}
34
35bool random_bool(GrRandom* r) {
36 return r->nextF() > .5f;
37}
38
bsalomon@google.comc3841b92012-08-02 18:11:43 +000039typedef GrGLProgram::StageDesc StageDesc;
40// TODO: Effects should be able to register themselves for inclusion in the
41// randomly generated shaders. They should be able to configure themselves
42// randomly.
bsalomon@google.comd4726202012-08-03 14:34:46 +000043const GrCustomStage* create_random_effect(StageDesc* stageDesc,
44 GrRandom* random,
45 GrContext* context,
46 GrTexture* dummyTextures[]) {
bsalomon@google.comc3841b92012-08-02 18:11:43 +000047
bsalomon@google.comd4726202012-08-03 14:34:46 +000048 // The new code uses SkRandom not GrRandom.
49 // TODO: Remove GrRandom.
50 SkRandom sk_random;
51 sk_random.setSeed(random->nextU());
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +000052 GrCustomStage* stage = GrCustomStageTestFactory::CreateStage(&sk_random,
53 context,
54 dummyTextures);
55 GrAssert(stage);
56 return stage;
bsalomon@google.comc3841b92012-08-02 18:11:43 +000057}
58}
59
60bool GrGpuGL::programUnitTest() {
61
bsalomon@google.comd4726202012-08-03 14:34:46 +000062 GrTextureDesc dummyDesc;
63 dummyDesc.fConfig = kSkia8888_PM_GrPixelConfig;
64 dummyDesc.fWidth = 34;
65 dummyDesc.fHeight = 18;
66 SkAutoTUnref<GrTexture> dummyTexture1(this->createTexture(dummyDesc, NULL, 0));
67 dummyDesc.fConfig = kAlpha_8_GrPixelConfig;
68 dummyDesc.fWidth = 16;
69 dummyDesc.fHeight = 22;
70 SkAutoTUnref<GrTexture> dummyTexture2(this->createTexture(dummyDesc, NULL, 0));
71
rmistry@google.comd6176b02012-08-23 18:14:13 +000072 // GrGLSLGeneration glslGeneration =
bsalomon@google.comc3841b92012-08-02 18:11:43 +000073 GrGetGLSLGeneration(this->glBinding(), this->glInterface());
74 static const int STAGE_OPTS[] = {
75 0,
76 StageDesc::kNoPerspective_OptFlagBit,
77 };
78 static const int IN_CONFIG_FLAGS[] = {
79 StageDesc::kNone_InConfigFlag,
bsalomon@google.comc3841b92012-08-02 18:11:43 +000080 StageDesc::kSmearAlpha_InConfigFlag,
bsalomon@google.comc3841b92012-08-02 18:11:43 +000081 };
82
83 static const int NUM_TESTS = 512;
84
85 GrRandom random;
86 for (int t = 0; t < NUM_TESTS; ++t) {
87
88#if 0
89 GrPrintf("\nTest Program %d\n-------------\n", t);
90 static const int stop = -1;
91 if (t == stop) {
92 int breakpointhere = 9;
93 }
94#endif
95
96 ProgramDesc pdesc;
97 pdesc.fVertexLayout = 0;
98 pdesc.fEmitsPointSize = random.nextF() > .5f;
99 pdesc.fColorInput = random_int(&random, ProgramDesc::kColorInputCnt);
100 pdesc.fCoverageInput = random_int(&random, ProgramDesc::kColorInputCnt);
101
102 pdesc.fColorFilterXfermode = random_int(&random, SkXfermode::kCoeffModesCnt);
103
104 pdesc.fFirstCoverageStage = random_int(&random, GrDrawState::kNumStages);
105
106 pdesc.fVertexLayout |= random_bool(&random) ?
107 GrDrawTarget::kCoverage_VertexLayoutBit :
108 0;
109
110#if GR_GL_EXPERIMENTAL_GS
bsalomon@google.comf6601872012-08-28 21:11:35 +0000111 pdesc.fExperimentalGS = this->getCaps().geometryShaderSupport() &&
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000112 random_bool(&random);
113#endif
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000114
115 bool edgeAA = random_bool(&random);
116 if (edgeAA) {
117 pdesc.fVertexLayout |= GrDrawTarget::kEdge_VertexLayoutBit;
bsalomon@google.comf6601872012-08-28 21:11:35 +0000118 if (this->getCaps().shaderDerivativeSupport()) {
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000119 pdesc.fVertexEdgeType = (GrDrawState::VertexEdgeType) random_int(&random, GrDrawState::kVertexEdgeTypeCnt);
120 } else {
121 pdesc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
122 }
123 } else {
124 }
125
126 pdesc.fColorMatrixEnabled = random_bool(&random);
127
bsalomon@google.comf6601872012-08-28 21:11:35 +0000128 if (this->getCaps().dualSourceBlendingSupport()) {
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000129 pdesc.fDualSrcOutput = random_int(&random, ProgramDesc::kDualSrcOutputCnt);
130 } else {
131 pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
132 }
133
134 SkAutoTUnref<const GrCustomStage> customStages[GrDrawState::kNumStages];
135
136 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
137 StageDesc& stage = pdesc.fStages[s];
138 // enable the stage?
139 if (random_bool(&random)) {
140 // use separate tex coords?
141 if (random_bool(&random)) {
142 int t = random_int(&random, GrDrawState::kMaxTexCoords);
143 pdesc.fVertexLayout |= StageTexCoordVertexLayoutBit(s, t);
144 }
145 stage.setEnabled(true);
146 }
147 // use text-formatted verts?
148 if (random_bool(&random)) {
149 pdesc.fVertexLayout |= kTextFormat_VertexLayoutBit;
150 }
151
152 stage.fCustomStageKey = 0;
153
bsalomon@google.comd4726202012-08-03 14:34:46 +0000154 stage.fOptFlags |= STAGE_OPTS[random_int(&random, GR_ARRAY_COUNT(STAGE_OPTS))];
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000155 stage.fInConfigFlags = IN_CONFIG_FLAGS[random_int(&random, GR_ARRAY_COUNT(IN_CONFIG_FLAGS))];
156
bsalomon@google.comd4726202012-08-03 14:34:46 +0000157 if (stage.isEnabled()) {
158 GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()};
159 customStages[s].reset(create_random_effect(&stage,
160 &random,
161 getContext(),
162 dummyTextures));
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000163 if (NULL != customStages[s]) {
164 stage.fCustomStageKey =
165 customStages[s]->getFactory().glStageKey(*customStages[s], this->glCaps());
166 }
167 }
168 }
169 GR_STATIC_ASSERT(sizeof(customStages) ==
170 GrDrawState::kNumStages * sizeof(GrCustomStage*));
171 const GrCustomStage** stages = reinterpret_cast<const GrCustomStage**>(&customStages);
172 SkAutoTUnref<GrGLProgram> program(GrGLProgram::Create(this->glContextInfo(),
173 pdesc,
174 stages));
175 if (NULL == program.get()) {
176 return false;
177 }
178 }
179 return true;
180}
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000181
182static void GLProgramsTest(skiatest::Reporter* reporter, GrContext* context) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000183 GrGpuGL* shadersGpu = static_cast<GrGpuGL*>(context->getGpu());
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000184 REPORTER_ASSERT(reporter, shadersGpu->programUnitTest());
185}
186
187
188#include "TestClassDef.h"
189DEFINE_GPUTESTCLASS("GLPrograms", GLProgramsTestClass, GLProgramsTest)
190
rmistry@google.comd6176b02012-08-23 18:14:13 +0000191// This is evil evil evil. The linker may throw away whole translation units as dead code if it
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000192// thinks none of the functions are called. It will do this even if there are static initilializers
193// in the unit that could pass pointers to functions from the unit out to other translation units!
194// We force some of the effects that would otherwise be discarded to link here.
195
196#include "SkLightingImageFilter.h"
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000197#include "SkMagnifierImageFilter.h"
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000198
199void forceLinking();
200
201void forceLinking() {
202 SkLightingImageFilter::CreateDistantLitDiffuse(SkPoint3(0,0,0), 0, 0, 0);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000203 SkMagnifierImageFilter mag(SkRect::MakeWH(SK_Scalar1, SK_Scalar1), SK_Scalar1);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000204 GrConfigConversionEffect::Create(NULL, false);
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000205}
206
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000207#endif