blob: 355cadebf0737567cbec1498a54c81293157aac1 [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.coma469c282012-10-24 18:28:34 +000038const GrEffect* create_random_effect(StageDesc* stageDesc,
bsalomon@google.comd4726202012-08-03 14:34:46 +000039 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.coma469c282012-10-24 18:28:34 +000047 GrEffect* stage = GrEffectTestFactory::CreateStage(&sk_random, context, dummyTextures);
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +000048 GrAssert(stage);
49 return stage;
bsalomon@google.comc3841b92012-08-02 18:11:43 +000050}
51}
52
53bool GrGpuGL::programUnitTest() {
54
bsalomon@google.comd4726202012-08-03 14:34:46 +000055 GrTextureDesc dummyDesc;
56 dummyDesc.fConfig = kSkia8888_PM_GrPixelConfig;
57 dummyDesc.fWidth = 34;
58 dummyDesc.fHeight = 18;
59 SkAutoTUnref<GrTexture> dummyTexture1(this->createTexture(dummyDesc, NULL, 0));
60 dummyDesc.fConfig = kAlpha_8_GrPixelConfig;
61 dummyDesc.fWidth = 16;
62 dummyDesc.fHeight = 22;
63 SkAutoTUnref<GrTexture> dummyTexture2(this->createTexture(dummyDesc, NULL, 0));
64
rmistry@google.comd6176b02012-08-23 18:14:13 +000065 // GrGLSLGeneration glslGeneration =
bsalomon@google.comc3841b92012-08-02 18:11:43 +000066 GrGetGLSLGeneration(this->glBinding(), this->glInterface());
67 static const int STAGE_OPTS[] = {
68 0,
69 StageDesc::kNoPerspective_OptFlagBit,
70 };
bsalomon@google.comc3841b92012-08-02 18:11:43 +000071
72 static const int NUM_TESTS = 512;
73
74 GrRandom random;
75 for (int t = 0; t < NUM_TESTS; ++t) {
76
77#if 0
78 GrPrintf("\nTest Program %d\n-------------\n", t);
79 static const int stop = -1;
80 if (t == stop) {
81 int breakpointhere = 9;
82 }
83#endif
84
85 ProgramDesc pdesc;
86 pdesc.fVertexLayout = 0;
87 pdesc.fEmitsPointSize = random.nextF() > .5f;
88 pdesc.fColorInput = random_int(&random, ProgramDesc::kColorInputCnt);
89 pdesc.fCoverageInput = random_int(&random, ProgramDesc::kColorInputCnt);
90
91 pdesc.fColorFilterXfermode = random_int(&random, SkXfermode::kCoeffModesCnt);
92
93 pdesc.fFirstCoverageStage = random_int(&random, GrDrawState::kNumStages);
94
95 pdesc.fVertexLayout |= random_bool(&random) ?
96 GrDrawTarget::kCoverage_VertexLayoutBit :
97 0;
98
99#if GR_GL_EXPERIMENTAL_GS
bsalomon@google.comf6601872012-08-28 21:11:35 +0000100 pdesc.fExperimentalGS = this->getCaps().geometryShaderSupport() &&
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000101 random_bool(&random);
102#endif
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000103
104 bool edgeAA = random_bool(&random);
105 if (edgeAA) {
106 pdesc.fVertexLayout |= GrDrawTarget::kEdge_VertexLayoutBit;
bsalomon@google.comf6601872012-08-28 21:11:35 +0000107 if (this->getCaps().shaderDerivativeSupport()) {
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000108 pdesc.fVertexEdgeType = (GrDrawState::VertexEdgeType) random_int(&random, GrDrawState::kVertexEdgeTypeCnt);
109 } else {
110 pdesc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
111 }
112 } else {
113 }
114
bsalomon@google.comf6601872012-08-28 21:11:35 +0000115 if (this->getCaps().dualSourceBlendingSupport()) {
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000116 pdesc.fDualSrcOutput = random_int(&random, ProgramDesc::kDualSrcOutputCnt);
117 } else {
118 pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
119 }
120
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000121 SkAutoTUnref<const GrEffect> effects[GrDrawState::kNumStages];
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000122
123 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
124 StageDesc& stage = pdesc.fStages[s];
125 // enable the stage?
126 if (random_bool(&random)) {
127 // use separate tex coords?
128 if (random_bool(&random)) {
129 int t = random_int(&random, GrDrawState::kMaxTexCoords);
130 pdesc.fVertexLayout |= StageTexCoordVertexLayoutBit(s, t);
131 }
132 stage.setEnabled(true);
133 }
134 // use text-formatted verts?
135 if (random_bool(&random)) {
136 pdesc.fVertexLayout |= kTextFormat_VertexLayoutBit;
137 }
138
139 stage.fCustomStageKey = 0;
140
bsalomon@google.comd4726202012-08-03 14:34:46 +0000141 stage.fOptFlags |= STAGE_OPTS[random_int(&random, GR_ARRAY_COUNT(STAGE_OPTS))];
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000142
bsalomon@google.comd4726202012-08-03 14:34:46 +0000143 if (stage.isEnabled()) {
144 GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()};
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000145 effects[s].reset(create_random_effect(&stage,
146 &random,
147 getContext(),
148 dummyTextures));
149 if (NULL != effects[s]) {
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000150 stage.fCustomStageKey =
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000151 effects[s]->getFactory().glStageKey(*effects[s], this->glCaps());
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000152 }
153 }
154 }
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000155 GR_STATIC_ASSERT(sizeof(effects) ==
bsalomon@google.coma469c282012-10-24 18:28:34 +0000156 GrDrawState::kNumStages * sizeof(GrEffect*));
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000157 const GrEffect** stages = reinterpret_cast<const GrEffect**>(&effects);
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000158 SkAutoTUnref<GrGLProgram> program(GrGLProgram::Create(this->glContextInfo(),
159 pdesc,
160 stages));
161 if (NULL == program.get()) {
162 return false;
163 }
164 }
165 return true;
166}
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000167
168static void GLProgramsTest(skiatest::Reporter* reporter, GrContext* context) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000169 GrGpuGL* shadersGpu = static_cast<GrGpuGL*>(context->getGpu());
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000170 REPORTER_ASSERT(reporter, shadersGpu->programUnitTest());
171}
172
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000173#include "TestClassDef.h"
174DEFINE_GPUTESTCLASS("GLPrograms", GLProgramsTestClass, GLProgramsTest)
175
rmistry@google.comd6176b02012-08-23 18:14:13 +0000176// 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 +0000177// 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 +0000178// in the unit that could pass pointers to functions from the unit out to other translation units!
179// We force some of the effects that would otherwise be discarded to link here.
180
181#include "SkLightingImageFilter.h"
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000182#include "SkMagnifierImageFilter.h"
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000183#include "SkColorMatrixFilter.h"
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000184
185void forceLinking();
186
187void forceLinking() {
188 SkLightingImageFilter::CreateDistantLitDiffuse(SkPoint3(0,0,0), 0, 0, 0);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000189 SkMagnifierImageFilter mag(SkRect::MakeWH(SK_Scalar1, SK_Scalar1), SK_Scalar1);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000190 GrConfigConversionEffect::Create(NULL, false);
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