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