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 | |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 23 | void GrGLProgram::Desc::setRandom(SkMWCRandom* random, |
| 24 | const GrGpuGL* gpu, |
| 25 | const GrEffectStage stages[GrDrawState::kNumStages]) { |
jvanverth@google.com | b8b705b | 2013-02-28 16:28:34 +0000 | [diff] [blame] | 26 | fAttribBindings = 0; |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 27 | fEmitsPointSize = random->nextBool(); |
| 28 | fColorInput = random->nextULessThan(kColorInputCnt); |
| 29 | fCoverageInput = random->nextULessThan(kColorInputCnt); |
| 30 | |
| 31 | fColorFilterXfermode = random->nextULessThan(SkXfermode::kCoeffModesCnt); |
| 32 | |
| 33 | fFirstCoverageStage = random->nextULessThan(GrDrawState::kNumStages); |
| 34 | |
jvanverth@google.com | b8b705b | 2013-02-28 16:28:34 +0000 | [diff] [blame] | 35 | fAttribBindings |= random->nextBool() ? GrDrawState::kCoverage_AttribBindingsBit : 0; |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 36 | |
| 37 | #if GR_GL_EXPERIMENTAL_GS |
| 38 | fExperimentalGS = gpu->getCaps().geometryShaderSupport() && random->nextBool(); |
| 39 | #endif |
| 40 | |
| 41 | bool edgeAA = random->nextBool(); |
| 42 | if (edgeAA) { |
jvanverth@google.com | b8b705b | 2013-02-28 16:28:34 +0000 | [diff] [blame] | 43 | fAttribBindings |= GrDrawState::kEdge_AttribBindingsBit; |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 44 | if (gpu->getCaps().shaderDerivativeSupport()) { |
| 45 | fVertexEdgeType = (GrDrawState::VertexEdgeType) |
| 46 | random->nextULessThan(GrDrawState::kVertexEdgeTypeCnt); |
| 47 | fDiscardIfOutsideEdge = random->nextBool(); |
| 48 | } else { |
| 49 | fVertexEdgeType = GrDrawState::kHairLine_EdgeType; |
| 50 | fDiscardIfOutsideEdge = false; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | if (gpu->getCaps().dualSourceBlendingSupport()) { |
| 55 | fDualSrcOutput = random->nextULessThan(kDualSrcOutputCnt); |
| 56 | } else { |
| 57 | fDualSrcOutput = kNone_DualSrcOutput; |
| 58 | } |
| 59 | |
jvanverth@google.com | 3976825 | 2013-02-14 15:25:44 +0000 | [diff] [blame] | 60 | bool useOnce = false; |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 61 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
| 62 | if (NULL != stages[s].getEffect()) { |
| 63 | const GrBackendEffectFactory& factory = (*stages[s].getEffect())->getFactory(); |
| 64 | fEffectKeys[s] = factory.glEffectKey(stages[s], gpu->glCaps()); |
| 65 | // use separate tex coords? |
jvanverth@google.com | 3976825 | 2013-02-14 15:25:44 +0000 | [diff] [blame] | 66 | if (!useOnce && random->nextBool()) { |
jvanverth@google.com | b8b705b | 2013-02-28 16:28:34 +0000 | [diff] [blame] | 67 | fAttribBindings |= GrDrawState::ExplicitTexCoordAttribBindingsBit(s); |
jvanverth@google.com | 3976825 | 2013-02-14 15:25:44 +0000 | [diff] [blame] | 68 | useOnce = true; |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | } |
jvanverth@google.com | b8b705b | 2013-02-28 16:28:34 +0000 | [diff] [blame] | 72 | |
| 73 | int attributeIndex = 0; |
| 74 | fPositionAttributeIndex = attributeIndex; |
| 75 | ++attributeIndex; |
| 76 | if (fColorInput || (fAttribBindings & GrDrawState::kColor_AttribBindingsBit)) { |
| 77 | fColorAttributeIndex = attributeIndex; |
| 78 | ++attributeIndex; |
| 79 | } |
| 80 | if (fCoverageInput || (fAttribBindings & GrDrawState::kCoverage_AttribBindingsBit)) { |
| 81 | fCoverageAttributeIndex = attributeIndex; |
| 82 | ++attributeIndex; |
| 83 | } |
| 84 | if (fAttribBindings & GrDrawState::kEdge_AttribBindingsBit) { |
| 85 | fEdgeAttributeIndex = attributeIndex; |
| 86 | ++attributeIndex; |
| 87 | } |
| 88 | if (GrDrawState::AttributesBindExplicitTexCoords(fAttribBindings)) { |
| 89 | fTexCoordAttributeIndex = attributeIndex; |
| 90 | ++attributeIndex; |
| 91 | } |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 92 | } |
| 93 | |
bsalomon@google.com | 042a286 | 2013-02-04 18:39:24 +0000 | [diff] [blame] | 94 | bool GrGpuGL::programUnitTest(int maxStages) { |
| 95 | |
| 96 | maxStages = GrMin(maxStages, (int)GrDrawState::kNumStages); |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 97 | |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 98 | GrTextureDesc dummyDesc; |
bsalomon@google.com | fec0bc3 | 2013-02-07 14:43:04 +0000 | [diff] [blame] | 99 | dummyDesc.fConfig = kSkia8888_GrPixelConfig; |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 100 | dummyDesc.fWidth = 34; |
| 101 | dummyDesc.fHeight = 18; |
| 102 | SkAutoTUnref<GrTexture> dummyTexture1(this->createTexture(dummyDesc, NULL, 0)); |
| 103 | dummyDesc.fConfig = kAlpha_8_GrPixelConfig; |
| 104 | dummyDesc.fWidth = 16; |
| 105 | dummyDesc.fHeight = 22; |
| 106 | SkAutoTUnref<GrTexture> dummyTexture2(this->createTexture(dummyDesc, NULL, 0)); |
| 107 | |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 108 | static const int NUM_TESTS = 512; |
| 109 | |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 110 | SkMWCRandom random; |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 111 | for (int t = 0; t < NUM_TESTS; ++t) { |
| 112 | |
| 113 | #if 0 |
| 114 | GrPrintf("\nTest Program %d\n-------------\n", t); |
| 115 | static const int stop = -1; |
| 116 | if (t == stop) { |
| 117 | int breakpointhere = 9; |
| 118 | } |
| 119 | #endif |
| 120 | |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 121 | GrGLProgram::Desc pdesc; |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 122 | GrEffectStage stages[GrDrawState::kNumStages]; |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 123 | |
bsalomon@google.com | 1d1d424 | 2013-02-05 15:44:21 +0000 | [diff] [blame] | 124 | for (int s = 0; s < maxStages; ++s) { |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 125 | // enable the stage? |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 126 | if (random.nextBool()) { |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 127 | GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()}; |
bsalomon@google.com | 73a9694 | 2013-02-13 16:31:19 +0000 | [diff] [blame] | 128 | SkAutoTUnref<const GrEffectRef> effect(GrEffectTestFactory::CreateStage( |
| 129 | &random, |
| 130 | this->getContext(), |
| 131 | dummyTextures)); |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 132 | stages[s].setEffect(effect.get()); |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 135 | pdesc.setRandom(&random, this, stages); |
| 136 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 137 | const GrEffectStage* stagePtrs[GrDrawState::kNumStages]; |
| 138 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
| 139 | stagePtrs[s] = &stages[s]; |
| 140 | } |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 141 | SkAutoTUnref<GrGLProgram> program(GrGLProgram::Create(this->glContextInfo(), |
| 142 | pdesc, |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 143 | stagePtrs)); |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 144 | if (NULL == program.get()) { |
| 145 | return false; |
| 146 | } |
| 147 | } |
| 148 | return true; |
| 149 | } |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 150 | |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 151 | static void GLProgramsTest(skiatest::Reporter* reporter, GrContextFactory* factory) { |
| 152 | for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { |
| 153 | GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(type)); |
| 154 | if (NULL != context) { |
bsalomon@google.com | 042a286 | 2013-02-04 18:39:24 +0000 | [diff] [blame] | 155 | GrGpuGL* gpu = static_cast<GrGpuGL*>(context->getGpu()); |
| 156 | int maxStages = GrDrawState::kNumStages; |
| 157 | #if SK_ANGLE |
| 158 | // Some long shaders run out of temporary registers in the D3D compiler on ANGLE. |
| 159 | if (type == GrContextFactory::kANGLE_GLContextType) { |
| 160 | maxStages = 3; |
| 161 | } |
| 162 | #endif |
| 163 | REPORTER_ASSERT(reporter, gpu->programUnitTest(maxStages)); |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 164 | } |
| 165 | } |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 166 | } |
| 167 | |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 168 | #include "TestClassDef.h" |
| 169 | DEFINE_GPUTESTCLASS("GLPrograms", GLProgramsTestClass, GLProgramsTest) |
| 170 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 171 | // 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] | 172 | // 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] | 173 | // in the unit that could pass pointers to functions from the unit out to other translation units! |
| 174 | // We force some of the effects that would otherwise be discarded to link here. |
| 175 | |
| 176 | #include "SkLightingImageFilter.h" |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 177 | #include "SkMagnifierImageFilter.h" |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 178 | #include "SkColorMatrixFilter.h" |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 179 | |
| 180 | void forceLinking(); |
| 181 | |
| 182 | void forceLinking() { |
| 183 | SkLightingImageFilter::CreateDistantLitDiffuse(SkPoint3(0,0,0), 0, 0, 0); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 184 | SkMagnifierImageFilter mag(SkRect::MakeWH(SK_Scalar1, SK_Scalar1), SK_Scalar1); |
bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 185 | GrConfigConversionEffect::Create(NULL, |
| 186 | false, |
| 187 | GrConfigConversionEffect::kNone_PMConversion, |
| 188 | SkMatrix::I()); |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 189 | SkScalar matrix[20]; |
| 190 | SkColorMatrixFilter cmf(matrix); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 191 | } |
| 192 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 193 | #endif |