blob: deaf0342bae7f19f44e4df4eba8abea34a976121 [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
bsalomon@google.comc3841b92012-08-02 18:11:43 +000030bool random_bool(GrRandom* r) {
31 return r->nextF() > .5f;
32}
33
bsalomon@google.comc3841b92012-08-02 18:11:43 +000034typedef GrGLProgram::StageDesc StageDesc;
35// TODO: Effects should be able to register themselves for inclusion in the
36// randomly generated shaders. They should be able to configure themselves
37// randomly.
bsalomon@google.comd4726202012-08-03 14:34:46 +000038const GrCustomStage* create_random_effect(StageDesc* stageDesc,
39 GrRandom* random,
40 GrContext* context,
41 GrTexture* dummyTextures[]) {
bsalomon@google.comc3841b92012-08-02 18:11:43 +000042
bsalomon@google.comd4726202012-08-03 14:34:46 +000043 // The new code uses SkRandom not GrRandom.
44 // TODO: Remove GrRandom.
45 SkRandom sk_random;
46 sk_random.setSeed(random->nextU());
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +000047 GrCustomStage* stage = GrCustomStageTestFactory::CreateStage(&sk_random,
48 context,
49 dummyTextures);
50 GrAssert(stage);
51 return stage;
bsalomon@google.comc3841b92012-08-02 18:11:43 +000052}
53}
54
55bool GrGpuGL::programUnitTest() {
56
bsalomon@google.comd4726202012-08-03 14:34:46 +000057 GrTextureDesc dummyDesc;
58 dummyDesc.fConfig = kSkia8888_PM_GrPixelConfig;
59 dummyDesc.fWidth = 34;
60 dummyDesc.fHeight = 18;
61 SkAutoTUnref<GrTexture> dummyTexture1(this->createTexture(dummyDesc, NULL, 0));
62 dummyDesc.fConfig = kAlpha_8_GrPixelConfig;
63 dummyDesc.fWidth = 16;
64 dummyDesc.fHeight = 22;
65 SkAutoTUnref<GrTexture> dummyTexture2(this->createTexture(dummyDesc, NULL, 0));
66
rmistry@google.comd6176b02012-08-23 18:14:13 +000067 // GrGLSLGeneration glslGeneration =
bsalomon@google.comc3841b92012-08-02 18:11:43 +000068 GrGetGLSLGeneration(this->glBinding(), this->glInterface());
69 static const int STAGE_OPTS[] = {
70 0,
71 StageDesc::kNoPerspective_OptFlagBit,
72 };
bsalomon@google.comc3841b92012-08-02 18:11:43 +000073
74 static const int NUM_TESTS = 512;
75
76 GrRandom random;
77 for (int t = 0; t < NUM_TESTS; ++t) {
78
79#if 0
80 GrPrintf("\nTest Program %d\n-------------\n", t);
81 static const int stop = -1;
82 if (t == stop) {
83 int breakpointhere = 9;
84 }
85#endif
86
87 ProgramDesc pdesc;
88 pdesc.fVertexLayout = 0;
89 pdesc.fEmitsPointSize = random.nextF() > .5f;
90 pdesc.fColorInput = random_int(&random, ProgramDesc::kColorInputCnt);
91 pdesc.fCoverageInput = random_int(&random, ProgramDesc::kColorInputCnt);
92
93 pdesc.fColorFilterXfermode = random_int(&random, SkXfermode::kCoeffModesCnt);
94
95 pdesc.fFirstCoverageStage = random_int(&random, GrDrawState::kNumStages);
96
97 pdesc.fVertexLayout |= random_bool(&random) ?
98 GrDrawTarget::kCoverage_VertexLayoutBit :
99 0;
100
101#if GR_GL_EXPERIMENTAL_GS
bsalomon@google.comf6601872012-08-28 21:11:35 +0000102 pdesc.fExperimentalGS = this->getCaps().geometryShaderSupport() &&
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000103 random_bool(&random);
104#endif
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000105
106 bool edgeAA = random_bool(&random);
107 if (edgeAA) {
108 pdesc.fVertexLayout |= GrDrawTarget::kEdge_VertexLayoutBit;
bsalomon@google.comf6601872012-08-28 21:11:35 +0000109 if (this->getCaps().shaderDerivativeSupport()) {
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000110 pdesc.fVertexEdgeType = (GrDrawState::VertexEdgeType) random_int(&random, GrDrawState::kVertexEdgeTypeCnt);
111 } else {
112 pdesc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
113 }
114 } else {
115 }
116
117 pdesc.fColorMatrixEnabled = random_bool(&random);
118
bsalomon@google.comf6601872012-08-28 21:11:35 +0000119 if (this->getCaps().dualSourceBlendingSupport()) {
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000120 pdesc.fDualSrcOutput = random_int(&random, ProgramDesc::kDualSrcOutputCnt);
121 } else {
122 pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
123 }
124
125 SkAutoTUnref<const GrCustomStage> customStages[GrDrawState::kNumStages];
126
127 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
128 StageDesc& stage = pdesc.fStages[s];
129 // enable the stage?
130 if (random_bool(&random)) {
131 // use separate tex coords?
132 if (random_bool(&random)) {
133 int t = random_int(&random, GrDrawState::kMaxTexCoords);
134 pdesc.fVertexLayout |= StageTexCoordVertexLayoutBit(s, t);
135 }
136 stage.setEnabled(true);
137 }
138 // use text-formatted verts?
139 if (random_bool(&random)) {
140 pdesc.fVertexLayout |= kTextFormat_VertexLayoutBit;
141 }
142
143 stage.fCustomStageKey = 0;
144
bsalomon@google.comd4726202012-08-03 14:34:46 +0000145 stage.fOptFlags |= STAGE_OPTS[random_int(&random, GR_ARRAY_COUNT(STAGE_OPTS))];
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000146
bsalomon@google.comd4726202012-08-03 14:34:46 +0000147 if (stage.isEnabled()) {
148 GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()};
149 customStages[s].reset(create_random_effect(&stage,
150 &random,
151 getContext(),
152 dummyTextures));
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000153 if (NULL != customStages[s]) {
154 stage.fCustomStageKey =
155 customStages[s]->getFactory().glStageKey(*customStages[s], this->glCaps());
156 }
157 }
158 }
159 GR_STATIC_ASSERT(sizeof(customStages) ==
160 GrDrawState::kNumStages * sizeof(GrCustomStage*));
161 const GrCustomStage** stages = reinterpret_cast<const GrCustomStage**>(&customStages);
162 SkAutoTUnref<GrGLProgram> program(GrGLProgram::Create(this->glContextInfo(),
163 pdesc,
164 stages));
165 if (NULL == program.get()) {
166 return false;
167 }
168 }
169 return true;
170}
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000171
172static void GLProgramsTest(skiatest::Reporter* reporter, GrContext* context) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000173 GrGpuGL* shadersGpu = static_cast<GrGpuGL*>(context->getGpu());
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000174 REPORTER_ASSERT(reporter, shadersGpu->programUnitTest());
175}
176
177
178#include "TestClassDef.h"
179DEFINE_GPUTESTCLASS("GLPrograms", GLProgramsTestClass, GLProgramsTest)
180
rmistry@google.comd6176b02012-08-23 18:14:13 +0000181// 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 +0000182// thinks none of the functions are called. It will do this even if there are static initilializers
183// in the unit that could pass pointers to functions from the unit out to other translation units!
184// We force some of the effects that would otherwise be discarded to link here.
185
186#include "SkLightingImageFilter.h"
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000187#include "SkMagnifierImageFilter.h"
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000188
189void forceLinking();
190
191void forceLinking() {
192 SkLightingImageFilter::CreateDistantLitDiffuse(SkPoint3(0,0,0), 0, 0, 0);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000193 SkMagnifierImageFilter mag(SkRect::MakeWH(SK_Scalar1, SK_Scalar1), SK_Scalar1);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000194 GrConfigConversionEffect::Create(NULL, false);
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000195}
196
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000197#endif