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); |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame^] | 95 | pdesc.fDiscardIfOutsideEdge = random.nextBool(); |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 96 | } else { |
| 97 | pdesc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType; |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame^] | 98 | pdesc.fDiscardIfOutsideEdge = false; |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 99 | } |
| 100 | } else { |
| 101 | } |
| 102 | |
bsalomon@google.com | f660187 | 2012-08-28 21:11:35 +0000 | [diff] [blame] | 103 | if (this->getCaps().dualSourceBlendingSupport()) { |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 104 | pdesc.fDualSrcOutput = random_int(&random, ProgramDesc::kDualSrcOutputCnt); |
| 105 | } else { |
| 106 | pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput; |
| 107 | } |
| 108 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 109 | GrEffectStage stages[GrDrawState::kNumStages]; |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 110 | |
| 111 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 112 | // enable the stage? |
| 113 | if (random_bool(&random)) { |
| 114 | // use separate tex coords? |
| 115 | if (random_bool(&random)) { |
| 116 | int t = random_int(&random, GrDrawState::kMaxTexCoords); |
| 117 | pdesc.fVertexLayout |= StageTexCoordVertexLayoutBit(s, t); |
| 118 | } |
bsalomon@google.com | dbe49f7 | 2012-11-05 16:36:02 +0000 | [diff] [blame] | 119 | // use text-formatted verts? |
| 120 | if (random_bool(&random)) { |
| 121 | pdesc.fVertexLayout |= kTextFormat_VertexLayoutBit; |
| 122 | } |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 123 | |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 124 | GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()}; |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 125 | SkAutoTUnref<const GrEffect> effect(create_random_effect(&random, |
bsalomon@google.com | dbe49f7 | 2012-11-05 16:36:02 +0000 | [diff] [blame] | 126 | getContext(), |
| 127 | dummyTextures)); |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 128 | stages[s].setEffect(effect.get()); |
| 129 | if (NULL != stages[s].getEffect()) { |
bsalomon@google.com | dbe49f7 | 2012-11-05 16:36:02 +0000 | [diff] [blame] | 130 | pdesc.fEffectKeys[s] = |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 131 | stages[s].getEffect()->getFactory().glEffectKey(stages[s], this->glCaps()); |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | } |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 135 | const GrEffectStage* stagePtrs[GrDrawState::kNumStages]; |
| 136 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
| 137 | stagePtrs[s] = &stages[s]; |
| 138 | } |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 139 | SkAutoTUnref<GrGLProgram> program(GrGLProgram::Create(this->glContextInfo(), |
| 140 | pdesc, |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 141 | stagePtrs)); |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 142 | if (NULL == program.get()) { |
| 143 | return false; |
| 144 | } |
| 145 | } |
| 146 | return true; |
| 147 | } |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 148 | |
| 149 | static void GLProgramsTest(skiatest::Reporter* reporter, GrContext* context) { |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 150 | GrGpuGL* shadersGpu = static_cast<GrGpuGL*>(context->getGpu()); |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 151 | REPORTER_ASSERT(reporter, shadersGpu->programUnitTest()); |
| 152 | } |
| 153 | |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 154 | #include "TestClassDef.h" |
| 155 | DEFINE_GPUTESTCLASS("GLPrograms", GLProgramsTestClass, GLProgramsTest) |
| 156 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 157 | // 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] | 158 | // 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] | 159 | // in the unit that could pass pointers to functions from the unit out to other translation units! |
| 160 | // We force some of the effects that would otherwise be discarded to link here. |
| 161 | |
| 162 | #include "SkLightingImageFilter.h" |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 163 | #include "SkMagnifierImageFilter.h" |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 164 | #include "SkColorMatrixFilter.h" |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 165 | |
| 166 | void forceLinking(); |
| 167 | |
| 168 | void forceLinking() { |
| 169 | SkLightingImageFilter::CreateDistantLitDiffuse(SkPoint3(0,0,0), 0, 0, 0); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 170 | SkMagnifierImageFilter mag(SkRect::MakeWH(SK_Scalar1, SK_Scalar1), SK_Scalar1); |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 171 | GrEffectStage dummyStage; |
| 172 | GrConfigConversionEffect::InstallEffect(NULL, |
| 173 | false, |
| 174 | GrConfigConversionEffect::kNone_PMConversion, |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 175 | SkMatrix::I(), |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 176 | &dummyStage); |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 177 | SkScalar matrix[20]; |
| 178 | SkColorMatrixFilter cmf(matrix); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 179 | } |
| 180 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 181 | #endif |