blob: c77c67070b52c92fd4bdec43e2c0957b1d2cfbc2 [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.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.com021fc732012-10-25 12:47:42 +000047 GrEffect* effect = GrEffectTestFactory::CreateStage(&sk_random, context, dummyTextures);
48 GrAssert(effect);
49 return effect;
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) {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000124 StageDesc& stageDesc = pdesc.fStages[s];
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000125 // 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 }
bsalomon@google.com08283af2012-10-26 13:01:20 +0000132 stageDesc.setEnabled(true);
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000133 }
134 // use text-formatted verts?
135 if (random_bool(&random)) {
136 pdesc.fVertexLayout |= kTextFormat_VertexLayoutBit;
137 }
138
bsalomon@google.com08283af2012-10-26 13:01:20 +0000139 stageDesc.fEffectKey = 0;
140 stageDesc.fOptFlags |= STAGE_OPTS[random_int(&random, GR_ARRAY_COUNT(STAGE_OPTS))];
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000141
bsalomon@google.com08283af2012-10-26 13:01:20 +0000142 if (stageDesc.isEnabled()) {
bsalomon@google.comd4726202012-08-03 14:34:46 +0000143 GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()};
bsalomon@google.com08283af2012-10-26 13:01:20 +0000144 effects[s].reset(create_random_effect(&stageDesc,
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000145 &random,
146 getContext(),
147 dummyTextures));
148 if (NULL != effects[s]) {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000149 stageDesc.fEffectKey =
bsalomon@google.com46fba0d2012-10-25 21:42:05 +0000150 effects[s]->getFactory().glEffectKey(*effects[s], this->glCaps());
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000151 }
152 }
153 }
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000154 GR_STATIC_ASSERT(sizeof(effects) ==
bsalomon@google.coma469c282012-10-24 18:28:34 +0000155 GrDrawState::kNumStages * sizeof(GrEffect*));
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000156 const GrEffect** stages = reinterpret_cast<const GrEffect**>(&effects);
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000157 SkAutoTUnref<GrGLProgram> program(GrGLProgram::Create(this->glContextInfo(),
158 pdesc,
159 stages));
160 if (NULL == program.get()) {
161 return false;
162 }
163 }
164 return true;
165}
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000166
167static void GLProgramsTest(skiatest::Reporter* reporter, GrContext* context) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000168 GrGpuGL* shadersGpu = static_cast<GrGpuGL*>(context->getGpu());
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000169 REPORTER_ASSERT(reporter, shadersGpu->programUnitTest());
170}
171
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000172#include "TestClassDef.h"
173DEFINE_GPUTESTCLASS("GLPrograms", GLProgramsTestClass, GLProgramsTest)
174
rmistry@google.comd6176b02012-08-23 18:14:13 +0000175// 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 +0000176// 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 +0000177// in the unit that could pass pointers to functions from the unit out to other translation units!
178// We force some of the effects that would otherwise be discarded to link here.
179
180#include "SkLightingImageFilter.h"
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000181#include "SkMagnifierImageFilter.h"
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000182#include "SkColorMatrixFilter.h"
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000183
184void forceLinking();
185
186void forceLinking() {
187 SkLightingImageFilter::CreateDistantLitDiffuse(SkPoint3(0,0,0), 0, 0, 0);
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000188 SkMagnifierImageFilter mag(SkRect::MakeWH(SK_Scalar1, SK_Scalar1), SK_Scalar1);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000189 GrConfigConversionEffect::Create(NULL, false);
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000190 SkScalar matrix[20];
191 SkColorMatrixFilter cmf(matrix);
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +0000192}
193
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000194#endif