blob: c99d66adea1d57933c45f5a48da752a41889700c [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.com4592df82012-09-07 14:20:32 +000081 StageDesc::kSmearRed_InConfigFlag,
bsalomon@google.comc3841b92012-08-02 18:11:43 +000082 };
83
84 static const int NUM_TESTS = 512;
85
86 GrRandom random;
87 for (int t = 0; t < NUM_TESTS; ++t) {
88
89#if 0
90 GrPrintf("\nTest Program %d\n-------------\n", t);
91 static const int stop = -1;
92 if (t == stop) {
93 int breakpointhere = 9;
94 }
95#endif
96
97 ProgramDesc pdesc;
98 pdesc.fVertexLayout = 0;
99 pdesc.fEmitsPointSize = random.nextF() > .5f;
100 pdesc.fColorInput = random_int(&random, ProgramDesc::kColorInputCnt);
101 pdesc.fCoverageInput = random_int(&random, ProgramDesc::kColorInputCnt);
102
103 pdesc.fColorFilterXfermode = random_int(&random, SkXfermode::kCoeffModesCnt);
104
105 pdesc.fFirstCoverageStage = random_int(&random, GrDrawState::kNumStages);
106
107 pdesc.fVertexLayout |= random_bool(&random) ?
108 GrDrawTarget::kCoverage_VertexLayoutBit :
109 0;
110
111#if GR_GL_EXPERIMENTAL_GS
bsalomon@google.comf6601872012-08-28 21:11:35 +0000112 pdesc.fExperimentalGS = this->getCaps().geometryShaderSupport() &&
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000113 random_bool(&random);
114#endif
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000115
116 bool edgeAA = random_bool(&random);
117 if (edgeAA) {
118 pdesc.fVertexLayout |= GrDrawTarget::kEdge_VertexLayoutBit;
bsalomon@google.comf6601872012-08-28 21:11:35 +0000119 if (this->getCaps().shaderDerivativeSupport()) {
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000120 pdesc.fVertexEdgeType = (GrDrawState::VertexEdgeType) random_int(&random, GrDrawState::kVertexEdgeTypeCnt);
121 } else {
122 pdesc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
123 }
124 } else {
125 }
126
127 pdesc.fColorMatrixEnabled = random_bool(&random);
128
bsalomon@google.comf6601872012-08-28 21:11:35 +0000129 if (this->getCaps().dualSourceBlendingSupport()) {
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000130 pdesc.fDualSrcOutput = random_int(&random, ProgramDesc::kDualSrcOutputCnt);
131 } else {
132 pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
133 }
134
135 SkAutoTUnref<const GrCustomStage> customStages[GrDrawState::kNumStages];
136
137 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
138 StageDesc& stage = pdesc.fStages[s];
139 // enable the stage?
140 if (random_bool(&random)) {
141 // use separate tex coords?
142 if (random_bool(&random)) {
143 int t = random_int(&random, GrDrawState::kMaxTexCoords);
144 pdesc.fVertexLayout |= StageTexCoordVertexLayoutBit(s, t);
145 }
146 stage.setEnabled(true);
147 }
148 // use text-formatted verts?
149 if (random_bool(&random)) {
150 pdesc.fVertexLayout |= kTextFormat_VertexLayoutBit;
151 }
152
153 stage.fCustomStageKey = 0;
154
bsalomon@google.comd4726202012-08-03 14:34:46 +0000155 stage.fOptFlags |= STAGE_OPTS[random_int(&random, GR_ARRAY_COUNT(STAGE_OPTS))];
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000156 stage.fInConfigFlags = IN_CONFIG_FLAGS[random_int(&random, GR_ARRAY_COUNT(IN_CONFIG_FLAGS))];
157
bsalomon@google.comd4726202012-08-03 14:34:46 +0000158 if (stage.isEnabled()) {
159 GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()};
160 customStages[s].reset(create_random_effect(&stage,
161 &random,
162 getContext(),
163 dummyTextures));
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000164 if (NULL != customStages[s]) {
165 stage.fCustomStageKey =
166 customStages[s]->getFactory().glStageKey(*customStages[s], this->glCaps());
167 }
168 }
169 }
170 GR_STATIC_ASSERT(sizeof(customStages) ==
171 GrDrawState::kNumStages * sizeof(GrCustomStage*));
172 const GrCustomStage** stages = reinterpret_cast<const GrCustomStage**>(&customStages);
173 SkAutoTUnref<GrGLProgram> program(GrGLProgram::Create(this->glContextInfo(),
174 pdesc,
175 stages));
176 if (NULL == program.get()) {
177 return false;
178 }
179 }
180 return true;
181}
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000182
183static void GLProgramsTest(skiatest::Reporter* reporter, GrContext* context) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000184 GrGpuGL* shadersGpu = static_cast<GrGpuGL*>(context->getGpu());
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000185 REPORTER_ASSERT(reporter, shadersGpu->programUnitTest());
186}
187
188
189#include "TestClassDef.h"
190DEFINE_GPUTESTCLASS("GLPrograms", GLProgramsTestClass, GLProgramsTest)
191
rmistry@google.comd6176b02012-08-23 18:14:13 +0000192// 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 +0000193// thinks none of the functions are called. It will do this even if there are static initilializers
194// in the unit that could pass pointers to functions from the unit out to other translation units!
195// We force some of the effects that would otherwise be discarded to link here.
196
197#include "SkLightingImageFilter.h"
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000198#include "SkMagnifierImageFilter.h"
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000199
200void forceLinking();
201
202void forceLinking() {
203 SkLightingImageFilter::CreateDistantLitDiffuse(SkPoint3(0,0,0), 0, 0, 0);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000204 SkMagnifierImageFilter mag(SkRect::MakeWH(SK_Scalar1, SK_Scalar1), SK_Scalar1);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000205 GrConfigConversionEffect::Create(NULL, false);
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000206}
207
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000208#endif