aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/GLProgramsTest.cpp
blob: 199cc4989d7e3f67f251ec20cec0034dc05524bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382

/*
 * Copyright 2011 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

// This is a GPU-backend specific test. It relies on static intializers to work

#include "SkTypes.h"

#if SK_SUPPORT_GPU && SK_ALLOW_STATIC_GLOBAL_INITIALIZERS

#include "gl/GrGpuGL.h"
#include "effects/GrColorTableEffect.h"
#include "effects/GrConvolutionEffect.h"
#include "effects/GrMorphologyEffect.h"
#include "SkLightingImageFilter.h"
#include "GrProgramStageFactory.h"
#include "GrRandom.h"
#include "Test.h"

namespace {

// GrRandoms nextU() values have patterns in the low bits
// So using nextU() % array_count might never take some values.
int random_int(GrRandom* r, int count) {
    return (int)(r->nextF() * count);
}

// min is inclusive, max is exclusive
int random_int(GrRandom* r, int min, int max) {
    return (int)(r->nextF() * (max-min)) + min;
}

bool random_bool(GrRandom* r) {
    return r->nextF() > .5f;
}

SkPoint3 random_point3(GrRandom* r) {
    return SkPoint3(r->nextF(), r->nextF(), r->nextF());
}

typedef GrGLProgram::StageDesc StageDesc;
// TODO: Effects should be able to register themselves for inclusion in the
// randomly generated shaders. They should be able to configure themselves
// randomly.
const GrCustomStage* create_random_effect(StageDesc* stageDesc,
                                          GrRandom* random,
                                          GrContext* context,
                                          GrTexture* dummyTextures[]) {
    enum EffectType {
        kConvolution_EffectType,
        kErode_EffectType,
        kDilate_EffectType,
        /**
         * Lighting effects don't work in unit test because they assume they insert functions and
         * assume the names are unique. This breaks when there are two light effects in the same
         * shader.
         */
        /*
        kDiffuseDistant_EffectType,
        kDiffusePoint_EffectType,
        kDiffuseSpot_EffectType,
        kSpecularDistant_EffectType,
        kSpecularPoint_EffectType,
        kSpecularSpot_EffectType,
        */

        kColorTable_EffectType,

        kEffectCount
    };

    // TODO: Remove this when generator doesn't apply this non-custom-stage
    // notion to custom stages automatically.
    static const uint32_t kMulByAlphaMask =
        StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag |
        StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;

    static const Gr1DKernelEffect::Direction gKernelDirections[] = {
        Gr1DKernelEffect::kX_Direction,
        Gr1DKernelEffect::kY_Direction
    };

    // The new code uses SkRandom not GrRandom.
    // TODO: Remove GrRandom.
    SkRandom sk_random;
    sk_random.setSeed(random->nextU());

    bool useFactory = random_bool(random);
    if (useFactory) {
        GrCustomStage* stage = GrCustomStageTestFactory::CreateStage(&sk_random,
                                                                     context,
                                                                     dummyTextures);
        GrAssert(stage);
        return stage;
    }


    // TODO: When matrices are property of the custom-stage then remove the
    // no-persp flag code below.
    int effect = random_int(random, kEffectCount);
    switch (effect) {
        case kConvolution_EffectType: {
            int direction = random_int(random, 2);
            int kernelRadius = random_int(random, 1, 4);
            float kernel[GrConvolutionEffect::kMaxKernelWidth];
            for (int i = 0; i < GrConvolutionEffect::kMaxKernelWidth; i++) {
                kernel[i] = random->nextF();
            }
            // does not work with perspective or mul-by-alpha-mask
            stageDesc->fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
            stageDesc->fInConfigFlags &= ~kMulByAlphaMask;
            return SkNEW_ARGS(GrConvolutionEffect,
                              (NULL,
                               gKernelDirections[direction],
                               kernelRadius,
                               kernel));
            }
        case kErode_EffectType: {
            int direction = random_int(random, 2);
            int kernelRadius = random_int(random, 1, 4);
            // does not work with perspective or mul-by-alpha-mask
            stageDesc->fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
            stageDesc->fInConfigFlags &= ~kMulByAlphaMask;
            return SkNEW_ARGS(GrMorphologyEffect,
                              (NULL,
                               gKernelDirections[direction],
                               kernelRadius,
                               GrContext::kErode_MorphologyType));
            }
        case kDilate_EffectType: {
            int direction = random_int(random, 2);
            int kernelRadius = random_int(random, 1, 4);
            // does not work with perspective or mul-by-alpha-mask
            stageDesc->fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
            stageDesc->fInConfigFlags &= ~kMulByAlphaMask;
            return SkNEW_ARGS(GrMorphologyEffect,
                              (NULL,
                               gKernelDirections[direction],
                               kernelRadius,
                               GrContext::kDilate_MorphologyType));
            }
        /*
        case kDiffuseDistant_EffectType: {
            SkPoint3 direction = random_point3(random);
            direction.normalize();
            SkColor lightColor = random->nextU();
            SkScalar surfaceScale = SkFloatToScalar(random->nextF());
            SkScalar kd = SkFloatToScalar(random->nextF());
            SkAutoTUnref<SkImageFilter> filter(SkLightingImageFilter::CreateDistantLitDiffuse(direction, lightColor, surfaceScale, kd));
            // does not work with perspective or mul-by-alpha-mask
            GrCustomStage* stage;
            bool ok = filter->asNewCustomStage(&stage, NULL);
            SkASSERT(ok);
            return stage;
        }
        case kDiffusePoint_EffectType: {
            SkPoint3 location = random_point3(random);
            SkColor lightColor = random->nextU();
            SkScalar surfaceScale = SkFloatToScalar(random->nextF());
            SkScalar kd = SkFloatToScalar(random->nextF());
            SkAutoTUnref<SkImageFilter> filter(SkLightingImageFilter::CreatePointLitDiffuse(location, lightColor, surfaceScale, kd));
            // does not work with perspective or mul-by-alpha-mask
            GrCustomStage* stage;
            bool ok = filter->asNewCustomStage(&stage, NULL);
            SkASSERT(ok);
            return stage;
        }
        case kDiffuseSpot_EffectType: {
            SkPoint3 location = random_point3(random);
            SkPoint3 target = random_point3(random);
            SkScalar cutoffAngle = SkFloatToScalar(random->nextF());
            SkScalar specularExponent = SkFloatToScalar(random->nextF());
            SkColor lightColor = random->nextU();
            SkScalar surfaceScale = SkFloatToScalar(random->nextF());
            SkScalar ks = SkFloatToScalar(random->nextF());
            SkScalar shininess = SkFloatToScalar(random->nextF());
            SkAutoTUnref<SkImageFilter> filter(SkLightingImageFilter::CreateSpotLitSpecular(
                location, target, specularExponent, cutoffAngle, lightColor, surfaceScale, ks, shininess));
            // does not work with perspective or mul-by-alpha-mask
            GrCustomStage* stage;
            bool ok = filter->asNewCustomStage(&stage, NULL);
            SkASSERT(ok);
            return stage;
        }
        case kSpecularDistant_EffectType: {
            SkPoint3 direction = random_point3(random);
            direction.normalize();
            SkColor lightColor = random->nextU();
            SkScalar surfaceScale = SkFloatToScalar(random->nextF());
            SkScalar ks = SkFloatToScalar(random->nextF());
            SkScalar shininess = SkFloatToScalar(random->nextF());
            SkAutoTUnref<SkImageFilter> filter(SkLightingImageFilter::CreateDistantLitSpecular(direction, lightColor, surfaceScale, ks, shininess));
            // does not work with perspective or mul-by-alpha-mask
            GrCustomStage* stage;
            bool ok = filter->asNewCustomStage(&stage, NULL);
            SkASSERT(ok);
            return stage;
        }
        case kSpecularPoint_EffectType: {
            SkPoint3 location = random_point3(random);
            SkColor lightColor = random->nextU();
            SkScalar surfaceScale = SkFloatToScalar(random->nextF());
            SkScalar ks = SkFloatToScalar(random->nextF());
            SkScalar shininess = SkFloatToScalar(random->nextF());
            SkAutoTUnref<SkImageFilter> filter(SkLightingImageFilter::CreatePointLitSpecular(location, lightColor, surfaceScale, ks, shininess));
            // does not work with perspective or mul-by-alpha-mask
            GrCustomStage* stage;
            bool ok = filter->asNewCustomStage(&stage, NULL);
            SkASSERT(ok);
            return stage;
        }
        case kSpecularSpot_EffectType: {
            SkPoint3 location = random_point3(random);
            SkPoint3 target = random_point3(random);
            SkScalar cutoffAngle = SkFloatToScalar(random->nextF());
            SkScalar specularExponent = SkFloatToScalar(random->nextF());
            SkColor lightColor = random->nextU();
            SkScalar surfaceScale = SkFloatToScalar(random->nextF());
            SkScalar ks = SkFloatToScalar(random->nextF());
            SkScalar shininess = SkFloatToScalar(random->nextF());
            SkAutoTUnref<SkImageFilter> filter(SkLightingImageFilter::CreateSpotLitSpecular(
                location, target, specularExponent, cutoffAngle, lightColor, surfaceScale, ks, shininess));
            // does not work with perspective or mul-by-alpha-mask
            GrCustomStage* stage;
            bool ok = filter->asNewCustomStage(&stage, NULL);
            SkASSERT(ok);
            return stage;
        }
        */
        case kColorTable_EffectType: {
            GrTexture* alphaTexture = dummyTextures[GrCustomStageTestFactory::kAlphaTextureIdx];
            return SkNEW_ARGS(GrColorTableEffect, (alphaTexture));
        }
        default:
            GrCrash("Unexpected custom effect type");
    }
    return NULL;
}
}

bool GrGpuGL::programUnitTest() {

    GrTextureDesc dummyDesc;
    dummyDesc.fConfig = kSkia8888_PM_GrPixelConfig;
    dummyDesc.fWidth = 34;
    dummyDesc.fHeight = 18;
    SkAutoTUnref<GrTexture> dummyTexture1(this->createTexture(dummyDesc, NULL, 0));
    dummyDesc.fConfig = kAlpha_8_GrPixelConfig;
    dummyDesc.fWidth = 16;
    dummyDesc.fHeight = 22;
    SkAutoTUnref<GrTexture> dummyTexture2(this->createTexture(dummyDesc, NULL, 0));

    // GrGLSLGeneration glslGeneration = 
            GrGetGLSLGeneration(this->glBinding(), this->glInterface());
    static const int STAGE_OPTS[] = {
        0,
        StageDesc::kNoPerspective_OptFlagBit,
    };
    static const int IN_CONFIG_FLAGS[] = {
        StageDesc::kNone_InConfigFlag,
        StageDesc::kSwapRAndB_InConfigFlag,
        StageDesc::kSwapRAndB_InConfigFlag |
        StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag,
        StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag,
        StageDesc::kSmearAlpha_InConfigFlag,
        StageDesc::kSmearRed_InConfigFlag,
    };

    static const int NUM_TESTS = 512;

    GrRandom random;
    for (int t = 0; t < NUM_TESTS; ++t) {

#if 0
        GrPrintf("\nTest Program %d\n-------------\n", t);
        static const int stop = -1;
        if (t == stop) {
            int breakpointhere = 9;
        }
#endif

        ProgramDesc pdesc;
        pdesc.fVertexLayout = 0;
        pdesc.fEmitsPointSize = random.nextF() > .5f;
        pdesc.fColorInput = random_int(&random, ProgramDesc::kColorInputCnt);
        pdesc.fCoverageInput = random_int(&random, ProgramDesc::kColorInputCnt);

        pdesc.fColorFilterXfermode = random_int(&random, SkXfermode::kCoeffModesCnt);

        pdesc.fFirstCoverageStage = random_int(&random, GrDrawState::kNumStages);

        pdesc.fVertexLayout |= random_bool(&random) ?
                                    GrDrawTarget::kCoverage_VertexLayoutBit :
                                    0;

#if GR_GL_EXPERIMENTAL_GS
        pdesc.fExperimentalGS = this->getCaps().fGeometryShaderSupport &&
                                random_bool(&random);
#endif
        pdesc.fOutputConfig =  random_int(&random, ProgramDesc::kOutputConfigCnt);

        bool edgeAA = random_bool(&random);
        if (edgeAA) {
            pdesc.fVertexLayout |= GrDrawTarget::kEdge_VertexLayoutBit;
            if (this->getCaps().fShaderDerivativeSupport) {
                pdesc.fVertexEdgeType = (GrDrawState::VertexEdgeType) random_int(&random, GrDrawState::kVertexEdgeTypeCnt);
            } else {
                pdesc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
            }
        } else {
        }

        pdesc.fColorMatrixEnabled = random_bool(&random);

        if (this->getCaps().fDualSourceBlendingSupport) {
            pdesc.fDualSrcOutput = random_int(&random, ProgramDesc::kDualSrcOutputCnt);
        } else {
            pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
        }

        SkAutoTUnref<const GrCustomStage> customStages[GrDrawState::kNumStages];

        for (int s = 0; s < GrDrawState::kNumStages; ++s) {
            StageDesc& stage = pdesc.fStages[s];
            // enable the stage?
            if (random_bool(&random)) {
                // use separate tex coords?
                if (random_bool(&random)) {
                    int t = random_int(&random, GrDrawState::kMaxTexCoords);
                    pdesc.fVertexLayout |= StageTexCoordVertexLayoutBit(s, t);
                }
                stage.setEnabled(true);
            }
            // use text-formatted verts?
            if (random_bool(&random)) {
                pdesc.fVertexLayout |= kTextFormat_VertexLayoutBit;
            }

            stage.fCustomStageKey = 0;

            stage.fOptFlags |= STAGE_OPTS[random_int(&random, GR_ARRAY_COUNT(STAGE_OPTS))];
            stage.fInConfigFlags = IN_CONFIG_FLAGS[random_int(&random, GR_ARRAY_COUNT(IN_CONFIG_FLAGS))];

            if (stage.isEnabled()) {
                GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()};
                customStages[s].reset(create_random_effect(&stage,
                                                           &random,
                                                           getContext(),
                                                           dummyTextures));
                if (NULL != customStages[s]) {
                    stage.fCustomStageKey =
                        customStages[s]->getFactory().glStageKey(*customStages[s], this->glCaps());
                }
            }
        }
        GR_STATIC_ASSERT(sizeof(customStages) ==
                         GrDrawState::kNumStages * sizeof(GrCustomStage*));
        const GrCustomStage** stages = reinterpret_cast<const GrCustomStage**>(&customStages);
        SkAutoTUnref<GrGLProgram> program(GrGLProgram::Create(this->glContextInfo(),
                                                              pdesc,
                                                              stages));
        if (NULL == program.get()) {
            return false;
        }
    }
    return true;
}

static void GLProgramsTest(skiatest::Reporter* reporter, GrContext* context) {
    GrGpuGL* shadersGpu = static_cast<GrGpuGL*>(context->getGpu());
    REPORTER_ASSERT(reporter, shadersGpu->programUnitTest());
}


#include "TestClassDef.h"
DEFINE_GPUTESTCLASS("GLPrograms", GLProgramsTestClass, GLProgramsTest)

#endif