| 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 | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 17 | #include "GrContextFactory.h" | 
| bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 18 | #include "effects/GrConfigConversionEffect.h" | 
| bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 19 |  | 
| tfarina@chromium.org | 223137f | 2012-11-21 22:38:36 +0000 | [diff] [blame] | 20 | #include "SkRandom.h" | 
| bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 21 | #include "Test.h" | 
|  | 22 |  | 
|  | 23 | namespace { | 
|  | 24 |  | 
| tfarina@chromium.org | 223137f | 2012-11-21 22:38:36 +0000 | [diff] [blame] | 25 | // SkRandoms nextU() values have patterns in the low bits | 
| bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 26 | // So using nextU() % array_count might never take some values. | 
| tfarina@chromium.org | 223137f | 2012-11-21 22:38:36 +0000 | [diff] [blame] | 27 | int random_int(SkRandom* r, int count) { | 
| bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 28 | return (int)(r->nextF() * count); | 
|  | 29 | } | 
|  | 30 |  | 
| tfarina@chromium.org | 223137f | 2012-11-21 22:38:36 +0000 | [diff] [blame] | 31 | bool random_bool(SkRandom* r) { | 
| bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 32 | return r->nextF() > .5f; | 
|  | 33 | } | 
|  | 34 |  | 
| bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 35 | const GrEffectRef* create_random_effect(SkRandom* random, | 
|  | 36 | GrContext* context, | 
|  | 37 | GrTexture* dummyTextures[]) { | 
| bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 38 |  | 
| bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 39 | SkRandom sk_random; | 
|  | 40 | sk_random.setSeed(random->nextU()); | 
| bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 41 | GrEffectRef* effect = GrEffectTestFactory::CreateStage(&sk_random, context, dummyTextures); | 
| bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 42 | GrAssert(effect); | 
|  | 43 | return effect; | 
| bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 44 | } | 
|  | 45 | } | 
|  | 46 |  | 
| bsalomon@google.com | 042a286 | 2013-02-04 18:39:24 +0000 | [diff] [blame] | 47 | bool GrGpuGL::programUnitTest(int maxStages) { | 
|  | 48 |  | 
|  | 49 | maxStages = GrMin(maxStages, (int)GrDrawState::kNumStages); | 
| bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 50 |  | 
| bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 51 | GrTextureDesc dummyDesc; | 
|  | 52 | dummyDesc.fConfig = kSkia8888_PM_GrPixelConfig; | 
|  | 53 | dummyDesc.fWidth = 34; | 
|  | 54 | dummyDesc.fHeight = 18; | 
|  | 55 | SkAutoTUnref<GrTexture> dummyTexture1(this->createTexture(dummyDesc, NULL, 0)); | 
|  | 56 | dummyDesc.fConfig = kAlpha_8_GrPixelConfig; | 
|  | 57 | dummyDesc.fWidth = 16; | 
|  | 58 | dummyDesc.fHeight = 22; | 
|  | 59 | SkAutoTUnref<GrTexture> dummyTexture2(this->createTexture(dummyDesc, NULL, 0)); | 
|  | 60 |  | 
| bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 61 | static const int NUM_TESTS = 512; | 
|  | 62 |  | 
| tfarina@chromium.org | 223137f | 2012-11-21 22:38:36 +0000 | [diff] [blame] | 63 | SkRandom random; | 
| bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 64 | for (int t = 0; t < NUM_TESTS; ++t) { | 
|  | 65 |  | 
|  | 66 | #if 0 | 
|  | 67 | GrPrintf("\nTest Program %d\n-------------\n", t); | 
|  | 68 | static const int stop = -1; | 
|  | 69 | if (t == stop) { | 
|  | 70 | int breakpointhere = 9; | 
|  | 71 | } | 
|  | 72 | #endif | 
|  | 73 |  | 
|  | 74 | ProgramDesc pdesc; | 
|  | 75 | pdesc.fVertexLayout = 0; | 
|  | 76 | pdesc.fEmitsPointSize = random.nextF() > .5f; | 
|  | 77 | pdesc.fColorInput = random_int(&random, ProgramDesc::kColorInputCnt); | 
|  | 78 | pdesc.fCoverageInput = random_int(&random, ProgramDesc::kColorInputCnt); | 
|  | 79 |  | 
|  | 80 | pdesc.fColorFilterXfermode = random_int(&random, SkXfermode::kCoeffModesCnt); | 
|  | 81 |  | 
|  | 82 | pdesc.fFirstCoverageStage = random_int(&random, GrDrawState::kNumStages); | 
|  | 83 |  | 
|  | 84 | pdesc.fVertexLayout |= random_bool(&random) ? | 
| jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 85 | GrDrawState::kCoverage_VertexLayoutBit : | 
| bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 86 | 0; | 
|  | 87 |  | 
|  | 88 | #if GR_GL_EXPERIMENTAL_GS | 
| bsalomon@google.com | f660187 | 2012-08-28 21:11:35 +0000 | [diff] [blame] | 89 | pdesc.fExperimentalGS = this->getCaps().geometryShaderSupport() && | 
| bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 90 | random_bool(&random); | 
|  | 91 | #endif | 
| bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 92 |  | 
|  | 93 | bool edgeAA = random_bool(&random); | 
|  | 94 | if (edgeAA) { | 
| jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 95 | pdesc.fVertexLayout |= GrDrawState::kEdge_VertexLayoutBit; | 
| bsalomon@google.com | f660187 | 2012-08-28 21:11:35 +0000 | [diff] [blame] | 96 | if (this->getCaps().shaderDerivativeSupport()) { | 
| bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 97 | pdesc.fVertexEdgeType = (GrDrawState::VertexEdgeType) random_int(&random, GrDrawState::kVertexEdgeTypeCnt); | 
| bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 98 | pdesc.fDiscardIfOutsideEdge = random.nextBool(); | 
| bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 99 | } else { | 
|  | 100 | pdesc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType; | 
| bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 101 | pdesc.fDiscardIfOutsideEdge = false; | 
| bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 102 | } | 
|  | 103 | } else { | 
|  | 104 | } | 
|  | 105 |  | 
| bsalomon@google.com | f660187 | 2012-08-28 21:11:35 +0000 | [diff] [blame] | 106 | if (this->getCaps().dualSourceBlendingSupport()) { | 
| bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 107 | pdesc.fDualSrcOutput = random_int(&random, ProgramDesc::kDualSrcOutputCnt); | 
|  | 108 | } else { | 
|  | 109 | pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput; | 
|  | 110 | } | 
|  | 111 |  | 
| bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 112 | GrEffectStage stages[GrDrawState::kNumStages]; | 
| bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 113 |  | 
| bsalomon@google.com | 1d1d424 | 2013-02-05 15:44:21 +0000 | [diff] [blame^] | 114 | for (int s = 0; s < maxStages; ++s) { | 
| bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 115 | // enable the stage? | 
|  | 116 | if (random_bool(&random)) { | 
|  | 117 | // use separate tex coords? | 
|  | 118 | if (random_bool(&random)) { | 
|  | 119 | int t = random_int(&random, GrDrawState::kMaxTexCoords); | 
| jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 120 | pdesc.fVertexLayout |= GrDrawState::StageTexCoordVertexLayoutBit(s, t); | 
| bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 121 | } | 
| bsalomon@google.com | dbe49f7 | 2012-11-05 16:36:02 +0000 | [diff] [blame] | 122 | // use text-formatted verts? | 
|  | 123 | if (random_bool(&random)) { | 
| jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 124 | pdesc.fVertexLayout |= GrDrawState::kTextFormat_VertexLayoutBit; | 
| bsalomon@google.com | dbe49f7 | 2012-11-05 16:36:02 +0000 | [diff] [blame] | 125 | } | 
| bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 126 |  | 
| bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 127 | GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()}; | 
| bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 128 | SkAutoTUnref<const GrEffectRef> effect(create_random_effect(&random, | 
| bsalomon@google.com | dbe49f7 | 2012-11-05 16:36:02 +0000 | [diff] [blame] | 129 | getContext(), | 
|  | 130 | dummyTextures)); | 
| bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 131 | stages[s].setEffect(effect.get()); | 
|  | 132 | if (NULL != stages[s].getEffect()) { | 
| bsalomon@google.com | dbe49f7 | 2012-11-05 16:36:02 +0000 | [diff] [blame] | 133 | pdesc.fEffectKeys[s] = | 
| bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 134 | (*stages[s].getEffect())->getFactory().glEffectKey(stages[s], | 
|  | 135 | this->glCaps()); | 
| bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 136 | } | 
|  | 137 | } | 
|  | 138 | } | 
| bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 139 | const GrEffectStage* stagePtrs[GrDrawState::kNumStages]; | 
|  | 140 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { | 
|  | 141 | stagePtrs[s] = &stages[s]; | 
|  | 142 | } | 
| bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 143 | SkAutoTUnref<GrGLProgram> program(GrGLProgram::Create(this->glContextInfo(), | 
|  | 144 | pdesc, | 
| bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 145 | stagePtrs)); | 
| bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 146 | if (NULL == program.get()) { | 
|  | 147 | return false; | 
|  | 148 | } | 
|  | 149 | } | 
|  | 150 | return true; | 
|  | 151 | } | 
| bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 152 |  | 
| bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 153 | static void GLProgramsTest(skiatest::Reporter* reporter, GrContextFactory* factory) { | 
|  | 154 | for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { | 
|  | 155 | GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(type)); | 
|  | 156 | if (NULL != context) { | 
| bsalomon@google.com | 042a286 | 2013-02-04 18:39:24 +0000 | [diff] [blame] | 157 | GrGpuGL* gpu = static_cast<GrGpuGL*>(context->getGpu()); | 
|  | 158 | int maxStages = GrDrawState::kNumStages; | 
|  | 159 | #if SK_ANGLE | 
|  | 160 | // Some long shaders run out of temporary registers in the D3D compiler on ANGLE. | 
|  | 161 | if (type == GrContextFactory::kANGLE_GLContextType) { | 
|  | 162 | maxStages = 3; | 
|  | 163 | } | 
|  | 164 | #endif | 
|  | 165 | REPORTER_ASSERT(reporter, gpu->programUnitTest(maxStages)); | 
| bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 166 | } | 
|  | 167 | } | 
| bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 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 | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 187 | GrConfigConversionEffect::Create(NULL, | 
|  | 188 | false, | 
|  | 189 | GrConfigConversionEffect::kNone_PMConversion, | 
|  | 190 | SkMatrix::I()); | 
| bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 191 | SkScalar matrix[20]; | 
|  | 192 | SkColorMatrixFilter cmf(matrix); | 
| bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 193 | } | 
|  | 194 |  | 
| bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 195 | #endif |