bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 1 | |
| 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.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 9 | // This is a GPU-backend specific test. It relies on static intializers to work |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 10 | |
bsalomon@google.com | 2a48c3a | 2012-08-03 14:54:45 +0000 | [diff] [blame] | 11 | #include "SkTypes.h" |
| 12 | |
| 13 | #if SK_SUPPORT_GPU && SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
| 14 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 15 | #include "gl/GrGpuGL.h" |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 16 | #include "GrBackendEffectFactory.h" |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 17 | #include "effects/GrConfigConversionEffect.h" |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 18 | |
tfarina@chromium.org | 223137f | 2012-11-21 22:38:36 +0000 | [diff] [blame] | 19 | #include "SkRandom.h" |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 20 | #include "Test.h" |
| 21 | |
| 22 | namespace { |
| 23 | |
tfarina@chromium.org | 223137f | 2012-11-21 22:38:36 +0000 | [diff] [blame] | 24 | // SkRandoms nextU() values have patterns in the low bits |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 25 | // So using nextU() % array_count might never take some values. |
tfarina@chromium.org | 223137f | 2012-11-21 22:38:36 +0000 | [diff] [blame] | 26 | int random_int(SkRandom* r, int count) { |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 27 | return (int)(r->nextF() * count); |
| 28 | } |
| 29 | |
tfarina@chromium.org | 223137f | 2012-11-21 22:38:36 +0000 | [diff] [blame] | 30 | bool random_bool(SkRandom* r) { |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 31 | return r->nextF() > .5f; |
| 32 | } |
| 33 | |
tfarina@chromium.org | 223137f | 2012-11-21 22:38:36 +0000 | [diff] [blame] | 34 | const GrEffect* create_random_effect(SkRandom* random, |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 35 | GrContext* context, |
| 36 | GrTexture* dummyTextures[]) { |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 37 | |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 38 | SkRandom sk_random; |
| 39 | sk_random.setSeed(random->nextU()); |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 40 | GrEffect* effect = GrEffectTestFactory::CreateStage(&sk_random, context, dummyTextures); |
| 41 | GrAssert(effect); |
| 42 | return effect; |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | |
| 46 | bool GrGpuGL::programUnitTest() { |
| 47 | |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 48 | GrTextureDesc dummyDesc; |
| 49 | dummyDesc.fConfig = kSkia8888_PM_GrPixelConfig; |
| 50 | dummyDesc.fWidth = 34; |
| 51 | dummyDesc.fHeight = 18; |
| 52 | SkAutoTUnref<GrTexture> dummyTexture1(this->createTexture(dummyDesc, NULL, 0)); |
| 53 | dummyDesc.fConfig = kAlpha_8_GrPixelConfig; |
| 54 | dummyDesc.fWidth = 16; |
| 55 | dummyDesc.fHeight = 22; |
| 56 | SkAutoTUnref<GrTexture> dummyTexture2(this->createTexture(dummyDesc, NULL, 0)); |
| 57 | |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 58 | static const int NUM_TESTS = 512; |
| 59 | |
tfarina@chromium.org | 223137f | 2012-11-21 22:38:36 +0000 | [diff] [blame] | 60 | SkRandom random; |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 61 | for (int t = 0; t < NUM_TESTS; ++t) { |
| 62 | |
| 63 | #if 0 |
| 64 | GrPrintf("\nTest Program %d\n-------------\n", t); |
| 65 | static const int stop = -1; |
| 66 | if (t == stop) { |
| 67 | int breakpointhere = 9; |
| 68 | } |
| 69 | #endif |
| 70 | |
| 71 | ProgramDesc pdesc; |
| 72 | pdesc.fVertexLayout = 0; |
| 73 | pdesc.fEmitsPointSize = random.nextF() > .5f; |
| 74 | pdesc.fColorInput = random_int(&random, ProgramDesc::kColorInputCnt); |
| 75 | pdesc.fCoverageInput = random_int(&random, ProgramDesc::kColorInputCnt); |
| 76 | |
| 77 | pdesc.fColorFilterXfermode = random_int(&random, SkXfermode::kCoeffModesCnt); |
| 78 | |
| 79 | pdesc.fFirstCoverageStage = random_int(&random, GrDrawState::kNumStages); |
| 80 | |
| 81 | pdesc.fVertexLayout |= random_bool(&random) ? |
| 82 | GrDrawTarget::kCoverage_VertexLayoutBit : |
| 83 | 0; |
| 84 | |
| 85 | #if GR_GL_EXPERIMENTAL_GS |
bsalomon@google.com | f660187 | 2012-08-28 21:11:35 +0000 | [diff] [blame] | 86 | pdesc.fExperimentalGS = this->getCaps().geometryShaderSupport() && |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 87 | random_bool(&random); |
| 88 | #endif |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 89 | |
| 90 | bool edgeAA = random_bool(&random); |
| 91 | if (edgeAA) { |
| 92 | pdesc.fVertexLayout |= GrDrawTarget::kEdge_VertexLayoutBit; |
bsalomon@google.com | f660187 | 2012-08-28 21:11:35 +0000 | [diff] [blame] | 93 | if (this->getCaps().shaderDerivativeSupport()) { |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 94 | pdesc.fVertexEdgeType = (GrDrawState::VertexEdgeType) random_int(&random, GrDrawState::kVertexEdgeTypeCnt); |
| 95 | } else { |
| 96 | pdesc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType; |
| 97 | } |
| 98 | } else { |
| 99 | } |
| 100 | |
bsalomon@google.com | f660187 | 2012-08-28 21:11:35 +0000 | [diff] [blame] | 101 | if (this->getCaps().dualSourceBlendingSupport()) { |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 102 | pdesc.fDualSrcOutput = random_int(&random, ProgramDesc::kDualSrcOutputCnt); |
| 103 | } else { |
| 104 | pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput; |
| 105 | } |
| 106 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 107 | GrEffectStage stages[GrDrawState::kNumStages]; |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 108 | |
| 109 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 110 | // enable the stage? |
| 111 | if (random_bool(&random)) { |
| 112 | // use separate tex coords? |
| 113 | if (random_bool(&random)) { |
| 114 | int t = random_int(&random, GrDrawState::kMaxTexCoords); |
| 115 | pdesc.fVertexLayout |= StageTexCoordVertexLayoutBit(s, t); |
| 116 | } |
bsalomon@google.com | dbe49f7 | 2012-11-05 16:36:02 +0000 | [diff] [blame] | 117 | // use text-formatted verts? |
| 118 | if (random_bool(&random)) { |
| 119 | pdesc.fVertexLayout |= kTextFormat_VertexLayoutBit; |
| 120 | } |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 121 | |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 122 | GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()}; |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 123 | SkAutoTUnref<const GrEffect> effect(create_random_effect(&random, |
bsalomon@google.com | dbe49f7 | 2012-11-05 16:36:02 +0000 | [diff] [blame] | 124 | getContext(), |
| 125 | dummyTextures)); |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 126 | stages[s].setEffect(effect.get()); |
| 127 | if (NULL != stages[s].getEffect()) { |
bsalomon@google.com | dbe49f7 | 2012-11-05 16:36:02 +0000 | [diff] [blame] | 128 | pdesc.fEffectKeys[s] = |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 129 | stages[s].getEffect()->getFactory().glEffectKey(stages[s], this->glCaps()); |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | } |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 133 | const GrEffectStage* stagePtrs[GrDrawState::kNumStages]; |
| 134 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
| 135 | stagePtrs[s] = &stages[s]; |
| 136 | } |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 137 | SkAutoTUnref<GrGLProgram> program(GrGLProgram::Create(this->glContextInfo(), |
| 138 | pdesc, |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 139 | stagePtrs)); |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 140 | if (NULL == program.get()) { |
| 141 | return false; |
| 142 | } |
| 143 | } |
| 144 | return true; |
| 145 | } |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 146 | |
| 147 | static void GLProgramsTest(skiatest::Reporter* reporter, GrContext* context) { |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 148 | GrGpuGL* shadersGpu = static_cast<GrGpuGL*>(context->getGpu()); |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 149 | REPORTER_ASSERT(reporter, shadersGpu->programUnitTest()); |
| 150 | } |
| 151 | |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 152 | #include "TestClassDef.h" |
| 153 | DEFINE_GPUTESTCLASS("GLPrograms", GLProgramsTestClass, GLProgramsTest) |
| 154 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 155 | // This is evil evil evil. The linker may throw away whole translation units as dead code if it |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 156 | // thinks none of the functions are called. It will do this even if there are static initializers |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 157 | // in the unit that could pass pointers to functions from the unit out to other translation units! |
| 158 | // We force some of the effects that would otherwise be discarded to link here. |
| 159 | |
| 160 | #include "SkLightingImageFilter.h" |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 161 | #include "SkMagnifierImageFilter.h" |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 162 | #include "SkColorMatrixFilter.h" |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 163 | |
| 164 | void forceLinking(); |
| 165 | |
| 166 | void forceLinking() { |
| 167 | SkLightingImageFilter::CreateDistantLitDiffuse(SkPoint3(0,0,0), 0, 0, 0); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 168 | SkMagnifierImageFilter mag(SkRect::MakeWH(SK_Scalar1, SK_Scalar1), SK_Scalar1); |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 169 | GrEffectStage dummyStage; |
| 170 | GrConfigConversionEffect::InstallEffect(NULL, |
| 171 | false, |
| 172 | GrConfigConversionEffect::kNone_PMConversion, |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 173 | SkMatrix::I(), |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 174 | &dummyStage); |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 175 | SkScalar matrix[20]; |
| 176 | SkColorMatrixFilter cmf(matrix); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 177 | } |
| 178 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 179 | #endif |