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