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 | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 16 | #include "GrProgramStageFactory.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 | |
| 30 | // min is inclusive, max is exclusive |
| 31 | int random_int(GrRandom* r, int min, int max) { |
| 32 | return (int)(r->nextF() * (max-min)) + min; |
| 33 | } |
| 34 | |
| 35 | bool random_bool(GrRandom* r) { |
| 36 | return r->nextF() > .5f; |
| 37 | } |
| 38 | |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 39 | typedef GrGLProgram::StageDesc StageDesc; |
| 40 | // TODO: Effects should be able to register themselves for inclusion in the |
| 41 | // randomly generated shaders. They should be able to configure themselves |
| 42 | // randomly. |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 43 | const GrCustomStage* create_random_effect(StageDesc* stageDesc, |
| 44 | GrRandom* random, |
| 45 | GrContext* context, |
| 46 | GrTexture* dummyTextures[]) { |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 47 | |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 48 | // The new code uses SkRandom not GrRandom. |
| 49 | // TODO: Remove GrRandom. |
| 50 | SkRandom sk_random; |
| 51 | sk_random.setSeed(random->nextU()); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 52 | GrCustomStage* stage = GrCustomStageTestFactory::CreateStage(&sk_random, |
| 53 | context, |
| 54 | dummyTextures); |
| 55 | GrAssert(stage); |
| 56 | return stage; |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
| 60 | bool GrGpuGL::programUnitTest() { |
| 61 | |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 62 | GrTextureDesc dummyDesc; |
| 63 | dummyDesc.fConfig = kSkia8888_PM_GrPixelConfig; |
| 64 | dummyDesc.fWidth = 34; |
| 65 | dummyDesc.fHeight = 18; |
| 66 | SkAutoTUnref<GrTexture> dummyTexture1(this->createTexture(dummyDesc, NULL, 0)); |
| 67 | dummyDesc.fConfig = kAlpha_8_GrPixelConfig; |
| 68 | dummyDesc.fWidth = 16; |
| 69 | dummyDesc.fHeight = 22; |
| 70 | SkAutoTUnref<GrTexture> dummyTexture2(this->createTexture(dummyDesc, NULL, 0)); |
| 71 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 72 | // GrGLSLGeneration glslGeneration = |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 73 | GrGetGLSLGeneration(this->glBinding(), this->glInterface()); |
| 74 | static const int STAGE_OPTS[] = { |
| 75 | 0, |
| 76 | StageDesc::kNoPerspective_OptFlagBit, |
| 77 | }; |
| 78 | static const int IN_CONFIG_FLAGS[] = { |
| 79 | StageDesc::kNone_InConfigFlag, |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 80 | StageDesc::kSmearAlpha_InConfigFlag, |
bsalomon@google.com | 4592df8 | 2012-09-07 14:20:32 +0000 | [diff] [blame^] | 81 | StageDesc::kSmearRed_InConfigFlag, |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | static const int NUM_TESTS = 512; |
| 85 | |
| 86 | GrRandom random; |
| 87 | for (int t = 0; t < NUM_TESTS; ++t) { |
| 88 | |
| 89 | #if 0 |
| 90 | GrPrintf("\nTest Program %d\n-------------\n", t); |
| 91 | static const int stop = -1; |
| 92 | if (t == stop) { |
| 93 | int breakpointhere = 9; |
| 94 | } |
| 95 | #endif |
| 96 | |
| 97 | ProgramDesc pdesc; |
| 98 | pdesc.fVertexLayout = 0; |
| 99 | pdesc.fEmitsPointSize = random.nextF() > .5f; |
| 100 | pdesc.fColorInput = random_int(&random, ProgramDesc::kColorInputCnt); |
| 101 | pdesc.fCoverageInput = random_int(&random, ProgramDesc::kColorInputCnt); |
| 102 | |
| 103 | pdesc.fColorFilterXfermode = random_int(&random, SkXfermode::kCoeffModesCnt); |
| 104 | |
| 105 | pdesc.fFirstCoverageStage = random_int(&random, GrDrawState::kNumStages); |
| 106 | |
| 107 | pdesc.fVertexLayout |= random_bool(&random) ? |
| 108 | GrDrawTarget::kCoverage_VertexLayoutBit : |
| 109 | 0; |
| 110 | |
| 111 | #if GR_GL_EXPERIMENTAL_GS |
bsalomon@google.com | f660187 | 2012-08-28 21:11:35 +0000 | [diff] [blame] | 112 | pdesc.fExperimentalGS = this->getCaps().geometryShaderSupport() && |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 113 | random_bool(&random); |
| 114 | #endif |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 115 | |
| 116 | bool edgeAA = random_bool(&random); |
| 117 | if (edgeAA) { |
| 118 | pdesc.fVertexLayout |= GrDrawTarget::kEdge_VertexLayoutBit; |
bsalomon@google.com | f660187 | 2012-08-28 21:11:35 +0000 | [diff] [blame] | 119 | if (this->getCaps().shaderDerivativeSupport()) { |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 120 | pdesc.fVertexEdgeType = (GrDrawState::VertexEdgeType) random_int(&random, GrDrawState::kVertexEdgeTypeCnt); |
| 121 | } else { |
| 122 | pdesc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType; |
| 123 | } |
| 124 | } else { |
| 125 | } |
| 126 | |
| 127 | pdesc.fColorMatrixEnabled = random_bool(&random); |
| 128 | |
bsalomon@google.com | f660187 | 2012-08-28 21:11:35 +0000 | [diff] [blame] | 129 | if (this->getCaps().dualSourceBlendingSupport()) { |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 130 | pdesc.fDualSrcOutput = random_int(&random, ProgramDesc::kDualSrcOutputCnt); |
| 131 | } else { |
| 132 | pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput; |
| 133 | } |
| 134 | |
| 135 | SkAutoTUnref<const GrCustomStage> customStages[GrDrawState::kNumStages]; |
| 136 | |
| 137 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
| 138 | StageDesc& stage = pdesc.fStages[s]; |
| 139 | // enable the stage? |
| 140 | if (random_bool(&random)) { |
| 141 | // use separate tex coords? |
| 142 | if (random_bool(&random)) { |
| 143 | int t = random_int(&random, GrDrawState::kMaxTexCoords); |
| 144 | pdesc.fVertexLayout |= StageTexCoordVertexLayoutBit(s, t); |
| 145 | } |
| 146 | stage.setEnabled(true); |
| 147 | } |
| 148 | // use text-formatted verts? |
| 149 | if (random_bool(&random)) { |
| 150 | pdesc.fVertexLayout |= kTextFormat_VertexLayoutBit; |
| 151 | } |
| 152 | |
| 153 | stage.fCustomStageKey = 0; |
| 154 | |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 155 | stage.fOptFlags |= STAGE_OPTS[random_int(&random, GR_ARRAY_COUNT(STAGE_OPTS))]; |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 156 | stage.fInConfigFlags = IN_CONFIG_FLAGS[random_int(&random, GR_ARRAY_COUNT(IN_CONFIG_FLAGS))]; |
| 157 | |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 158 | if (stage.isEnabled()) { |
| 159 | GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()}; |
| 160 | customStages[s].reset(create_random_effect(&stage, |
| 161 | &random, |
| 162 | getContext(), |
| 163 | dummyTextures)); |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 164 | if (NULL != customStages[s]) { |
| 165 | stage.fCustomStageKey = |
| 166 | customStages[s]->getFactory().glStageKey(*customStages[s], this->glCaps()); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | GR_STATIC_ASSERT(sizeof(customStages) == |
| 171 | GrDrawState::kNumStages * sizeof(GrCustomStage*)); |
| 172 | const GrCustomStage** stages = reinterpret_cast<const GrCustomStage**>(&customStages); |
| 173 | SkAutoTUnref<GrGLProgram> program(GrGLProgram::Create(this->glContextInfo(), |
| 174 | pdesc, |
| 175 | stages)); |
| 176 | if (NULL == program.get()) { |
| 177 | return false; |
| 178 | } |
| 179 | } |
| 180 | return true; |
| 181 | } |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 182 | |
| 183 | static void GLProgramsTest(skiatest::Reporter* reporter, GrContext* context) { |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 184 | GrGpuGL* shadersGpu = static_cast<GrGpuGL*>(context->getGpu()); |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 185 | REPORTER_ASSERT(reporter, shadersGpu->programUnitTest()); |
| 186 | } |
| 187 | |
| 188 | |
| 189 | #include "TestClassDef.h" |
| 190 | DEFINE_GPUTESTCLASS("GLPrograms", GLProgramsTestClass, GLProgramsTest) |
| 191 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 192 | // This is evil evil evil. The linker may throw away whole translation units as dead code if it |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 193 | // thinks none of the functions are called. It will do this even if there are static initilializers |
| 194 | // in the unit that could pass pointers to functions from the unit out to other translation units! |
| 195 | // We force some of the effects that would otherwise be discarded to link here. |
| 196 | |
| 197 | #include "SkLightingImageFilter.h" |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 198 | #include "SkMagnifierImageFilter.h" |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 199 | |
| 200 | void forceLinking(); |
| 201 | |
| 202 | void forceLinking() { |
| 203 | SkLightingImageFilter::CreateDistantLitDiffuse(SkPoint3(0,0,0), 0, 0, 0); |
bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 204 | SkMagnifierImageFilter mag(SkRect::MakeWH(SK_Scalar1, SK_Scalar1), SK_Scalar1); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 205 | GrConfigConversionEffect::Create(NULL, false); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 206 | } |
| 207 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 208 | #endif |