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 | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 24 | void GrGLProgramDesc::setRandom(SkMWCRandom* random, |
| 25 | const GrGpuGL* gpu, |
bsalomon@google.com | b79d865 | 2013-03-29 20:30:50 +0000 | [diff] [blame] | 26 | const GrTexture* dstTexture, |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 27 | const GrEffectStage stages[GrDrawState::kNumStages], |
| 28 | int currAttribIndex) { |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 29 | fEmitsPointSize = random->nextBool(); |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 30 | |
| 31 | fPositionAttributeIndex = 0; |
| 32 | |
skia.committer@gmail.com | 05a2ee0 | 2013-04-02 07:01:34 +0000 | [diff] [blame] | 33 | // if the effects have used up all off the available attributes, |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 34 | // don't try to use color or coverage attributes as input |
| 35 | do { |
| 36 | fColorInput = random->nextULessThan(kColorInputCnt); |
skia.committer@gmail.com | 05a2ee0 | 2013-04-02 07:01:34 +0000 | [diff] [blame] | 37 | } while (GrDrawState::kMaxVertexAttribCnt <= currAttribIndex && |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 38 | kAttribute_ColorInput == fColorInput); |
| 39 | fColorAttributeIndex = (fColorInput == kAttribute_ColorInput) ? currAttribIndex++ : -1; |
| 40 | |
| 41 | do { |
| 42 | fCoverageInput = random->nextULessThan(kColorInputCnt); |
| 43 | } while (GrDrawState::kMaxVertexAttribCnt <= currAttribIndex && |
| 44 | kAttribute_ColorInput == fCoverageInput); |
| 45 | fCoverageAttributeIndex = (fCoverageInput == kAttribute_ColorInput) ? currAttribIndex++ : -1; |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 46 | |
bsalomon@google.com | 8da9bc7 | 2013-04-19 15:03:21 +0000 | [diff] [blame] | 47 | fColorFilterXfermode = random->nextULessThan(SkXfermode::kLastCoeffMode + 1); |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 48 | |
| 49 | fFirstCoverageStage = random->nextULessThan(GrDrawState::kNumStages); |
| 50 | |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 51 | #if GR_GL_EXPERIMENTAL_GS |
bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 52 | fExperimentalGS = gpu->caps()->geometryShaderSupport() && random->nextBool(); |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 53 | #endif |
| 54 | |
bsalomon@google.com | 26ec00e | 2013-03-26 16:49:37 +0000 | [diff] [blame] | 55 | fDiscardIfZeroCoverage = random->nextBool(); |
skia.committer@gmail.com | 37cbc7f | 2013-03-27 07:01:04 +0000 | [diff] [blame] | 56 | |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 57 | bool useLocalCoords = random->nextBool() && currAttribIndex < GrDrawState::kMaxVertexAttribCnt; |
| 58 | fLocalCoordAttributeIndex = useLocalCoords ? currAttribIndex++ : -1; |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 59 | |
bsalomon@google.com | b79d865 | 2013-03-29 20:30:50 +0000 | [diff] [blame] | 60 | bool dstRead = 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(); |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 64 | GrDrawEffect drawEffect(stages[s], useLocalCoords); |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 65 | fEffectKeys[s] = factory.glEffectKey(drawEffect, gpu->glCaps()); |
bsalomon@google.com | b79d865 | 2013-03-29 20:30:50 +0000 | [diff] [blame] | 66 | if ((*stages[s].getEffect())->willReadDst()) { |
| 67 | dstRead = true; |
| 68 | } |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 71 | |
bsalomon@google.com | b79d865 | 2013-03-29 20:30:50 +0000 | [diff] [blame] | 72 | if (dstRead) { |
| 73 | this->fDstRead = GrGLShaderBuilder::KeyForDstRead(dstTexture, gpu->glCaps()); |
| 74 | } |
bsalomon@google.com | 5920ac2 | 2013-04-19 13:14:45 +0000 | [diff] [blame] | 75 | |
| 76 | CoverageOutput coverageOutput; |
| 77 | bool illegalCoverageOutput; |
| 78 | do { |
| 79 | coverageOutput = static_cast<CoverageOutput>(random->nextULessThan(kCoverageOutputCnt)); |
| 80 | illegalCoverageOutput = (!gpu->caps()->dualSourceBlendingSupport() && |
| 81 | CoverageOutputUsesSecondaryOutput(coverageOutput)) || |
bsalomon@google.com | 72993ab | 2013-04-19 13:25:28 +0000 | [diff] [blame] | 82 | (!dstRead && kCombineWithDst_CoverageOutput == coverageOutput); |
bsalomon@google.com | 5920ac2 | 2013-04-19 13:14:45 +0000 | [diff] [blame] | 83 | } while (illegalCoverageOutput); |
| 84 | |
| 85 | fCoverageOutput = coverageOutput; |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 86 | } |
| 87 | |
bsalomon@google.com | 042a286 | 2013-02-04 18:39:24 +0000 | [diff] [blame] | 88 | bool GrGpuGL::programUnitTest(int maxStages) { |
| 89 | |
| 90 | maxStages = GrMin(maxStages, (int)GrDrawState::kNumStages); |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 91 | |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 92 | GrTextureDesc dummyDesc; |
bsalomon@google.com | fec0bc3 | 2013-02-07 14:43:04 +0000 | [diff] [blame] | 93 | dummyDesc.fConfig = kSkia8888_GrPixelConfig; |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 94 | dummyDesc.fWidth = 34; |
| 95 | dummyDesc.fHeight = 18; |
| 96 | SkAutoTUnref<GrTexture> dummyTexture1(this->createTexture(dummyDesc, NULL, 0)); |
| 97 | dummyDesc.fConfig = kAlpha_8_GrPixelConfig; |
| 98 | dummyDesc.fWidth = 16; |
| 99 | dummyDesc.fHeight = 22; |
| 100 | SkAutoTUnref<GrTexture> dummyTexture2(this->createTexture(dummyDesc, NULL, 0)); |
| 101 | |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 102 | static const int NUM_TESTS = 512; |
| 103 | |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 104 | SkMWCRandom random; |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 105 | for (int t = 0; t < NUM_TESTS; ++t) { |
| 106 | |
| 107 | #if 0 |
| 108 | GrPrintf("\nTest Program %d\n-------------\n", t); |
| 109 | static const int stop = -1; |
| 110 | if (t == stop) { |
| 111 | int breakpointhere = 9; |
| 112 | } |
| 113 | #endif |
| 114 | |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 115 | GrGLProgramDesc pdesc; |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 116 | GrEffectStage stages[GrDrawState::kNumStages]; |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 117 | |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 118 | int currAttribIndex = 1; // we need to always leave room for position |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 119 | int attribIndices[2]; |
bsalomon@google.com | b79d865 | 2013-03-29 20:30:50 +0000 | [diff] [blame] | 120 | GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()}; |
bsalomon@google.com | 1d1d424 | 2013-02-05 15:44:21 +0000 | [diff] [blame] | 121 | for (int s = 0; s < maxStages; ++s) { |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 122 | // enable the stage? |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 123 | if (random.nextBool()) { |
bsalomon@google.com | 73a9694 | 2013-02-13 16:31:19 +0000 | [diff] [blame] | 124 | SkAutoTUnref<const GrEffectRef> effect(GrEffectTestFactory::CreateStage( |
| 125 | &random, |
| 126 | this->getContext(), |
bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 127 | *this->caps(), |
bsalomon@google.com | 73a9694 | 2013-02-13 16:31:19 +0000 | [diff] [blame] | 128 | dummyTextures)); |
commit-bot@chromium.org | 9ae7850 | 2013-03-21 17:44:39 +0000 | [diff] [blame] | 129 | int numAttribs = (*effect)->numVertexAttribs(); |
| 130 | |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 131 | // If adding this effect would exceed the max attrib count then generate a |
skia.committer@gmail.com | 05a2ee0 | 2013-04-02 07:01:34 +0000 | [diff] [blame] | 132 | // new random effect. |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 133 | if (currAttribIndex + numAttribs > GrDrawState::kMaxVertexAttribCnt) { |
commit-bot@chromium.org | 9ae7850 | 2013-03-21 17:44:39 +0000 | [diff] [blame] | 134 | --s; |
| 135 | continue; |
| 136 | } |
| 137 | for (int i = 0; i < numAttribs; ++i) { |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 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 | b79d865 | 2013-03-29 20:30:50 +0000 | [diff] [blame] | 143 | const GrTexture* dstTexture = random.nextBool() ? dummyTextures[0] : dummyTextures[1]; |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 144 | pdesc.setRandom(&random, this, dstTexture, stages, currAttribIndex); |
skia.committer@gmail.com | 05a2ee0 | 2013-04-02 07:01:34 +0000 | [diff] [blame] | 145 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 146 | const GrEffectStage* stagePtrs[GrDrawState::kNumStages]; |
| 147 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
| 148 | stagePtrs[s] = &stages[s]; |
| 149 | } |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 150 | SkAutoTUnref<GrGLProgram> program(GrGLProgram::Create(this->glContext(), |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 151 | pdesc, |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 152 | stagePtrs)); |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 153 | if (NULL == program.get()) { |
| 154 | return false; |
| 155 | } |
| 156 | } |
| 157 | return true; |
| 158 | } |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 159 | |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 160 | static void GLProgramsTest(skiatest::Reporter* reporter, GrContextFactory* factory) { |
| 161 | for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { |
| 162 | GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(type)); |
| 163 | if (NULL != context) { |
bsalomon@google.com | 042a286 | 2013-02-04 18:39:24 +0000 | [diff] [blame] | 164 | GrGpuGL* gpu = static_cast<GrGpuGL*>(context->getGpu()); |
| 165 | int maxStages = GrDrawState::kNumStages; |
| 166 | #if SK_ANGLE |
| 167 | // Some long shaders run out of temporary registers in the D3D compiler on ANGLE. |
| 168 | if (type == GrContextFactory::kANGLE_GLContextType) { |
| 169 | maxStages = 3; |
| 170 | } |
| 171 | #endif |
| 172 | REPORTER_ASSERT(reporter, gpu->programUnitTest(maxStages)); |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 173 | } |
| 174 | } |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 175 | } |
| 176 | |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 177 | #include "TestClassDef.h" |
| 178 | DEFINE_GPUTESTCLASS("GLPrograms", GLProgramsTestClass, GLProgramsTest) |
| 179 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 180 | // 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] | 181 | // 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] | 182 | // in the unit that could pass pointers to functions from the unit out to other translation units! |
| 183 | // We force some of the effects that would otherwise be discarded to link here. |
| 184 | |
| 185 | #include "SkLightingImageFilter.h" |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 186 | #include "SkMagnifierImageFilter.h" |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 187 | #include "SkColorMatrixFilter.h" |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 188 | |
| 189 | void forceLinking(); |
| 190 | |
| 191 | void forceLinking() { |
| 192 | SkLightingImageFilter::CreateDistantLitDiffuse(SkPoint3(0,0,0), 0, 0, 0); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 193 | SkMagnifierImageFilter mag(SkRect::MakeWH(SK_Scalar1, SK_Scalar1), SK_Scalar1); |
bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 194 | GrConfigConversionEffect::Create(NULL, |
| 195 | false, |
| 196 | GrConfigConversionEffect::kNone_PMConversion, |
| 197 | SkMatrix::I()); |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 198 | SkScalar matrix[20]; |
| 199 | SkColorMatrixFilter cmf(matrix); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 200 | } |
| 201 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 202 | #endif |