blob: a43ad834efa83b8e0ffd36fe5d27ada0891d572c [file] [log] [blame]
bsalomon@google.coma8e686e2011-08-16 15:45:58 +00001
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.comd4726202012-08-03 14:34:46 +00009// This is a GPU-backend specific test. It relies on static intializers to work
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000010
bsalomon@google.com5739d2c2012-05-31 15:07:19 +000011#include "gl/GrGpuGL.h"
bsalomon@google.comc3841b92012-08-02 18:11:43 +000012#include "effects/GrColorTableEffect.h"
13#include "effects/GrConvolutionEffect.h"
bsalomon@google.comc3841b92012-08-02 18:11:43 +000014#include "effects/GrMorphologyEffect.h"
15#include "SkLightingImageFilter.h"
16#include "GrProgramStageFactory.h"
17#include "GrRandom.h"
18#include "Test.h"
19
bsalomon@google.comd4726202012-08-03 14:34:46 +000020#if SK_SUPPORT_GPU && SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
21
bsalomon@google.comc3841b92012-08-02 18:11:43 +000022namespace {
23
24// GrRandoms nextU() values have patterns in the low bits
25// So using nextU() % array_count might never take some values.
26int random_int(GrRandom* r, int count) {
27 return (int)(r->nextF() * count);
28}
29
30// min is inclusive, max is exclusive
31int random_int(GrRandom* r, int min, int max) {
32 return (int)(r->nextF() * (max-min)) + min;
33}
34
35bool random_bool(GrRandom* r) {
36 return r->nextF() > .5f;
37}
38
39SkPoint3 random_point3(GrRandom* r) {
40 return SkPoint3(r->nextF(), r->nextF(), r->nextF());
41}
42
bsalomon@google.comc3841b92012-08-02 18:11:43 +000043typedef GrGLProgram::StageDesc StageDesc;
44// TODO: Effects should be able to register themselves for inclusion in the
45// randomly generated shaders. They should be able to configure themselves
46// randomly.
bsalomon@google.comd4726202012-08-03 14:34:46 +000047const GrCustomStage* create_random_effect(StageDesc* stageDesc,
48 GrRandom* random,
49 GrContext* context,
50 GrTexture* dummyTextures[]) {
bsalomon@google.comc3841b92012-08-02 18:11:43 +000051 enum EffectType {
52 kConvolution_EffectType,
53 kErode_EffectType,
54 kDilate_EffectType,
bsalomon@google.comd4726202012-08-03 14:34:46 +000055 /**
56 * Lighting effects don't work in unit test because they assume they insert functions and
57 * assume the names are unique. This breaks when there are two light effects in the same
58 * shader.
59 */
60 /*
bsalomon@google.comc3841b92012-08-02 18:11:43 +000061 kDiffuseDistant_EffectType,
62 kDiffusePoint_EffectType,
63 kDiffuseSpot_EffectType,
64 kSpecularDistant_EffectType,
65 kSpecularPoint_EffectType,
66 kSpecularSpot_EffectType,
bsalomon@google.comd4726202012-08-03 14:34:46 +000067 */
68
bsalomon@google.comc3841b92012-08-02 18:11:43 +000069 kColorTable_EffectType,
70
71 kEffectCount
72 };
73
74 // TODO: Remove this when generator doesn't apply this non-custom-stage
75 // notion to custom stages automatically.
76 static const uint32_t kMulByAlphaMask =
77 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag |
78 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag;
79
80 static const Gr1DKernelEffect::Direction gKernelDirections[] = {
81 Gr1DKernelEffect::kX_Direction,
82 Gr1DKernelEffect::kY_Direction
83 };
84
bsalomon@google.comd4726202012-08-03 14:34:46 +000085 // The new code uses SkRandom not GrRandom.
86 // TODO: Remove GrRandom.
87 SkRandom sk_random;
88 sk_random.setSeed(random->nextU());
89
90 bool useFactory = random_bool(random);
91 if (useFactory) {
92 GrCustomStage* stage = GrCustomStageTestFactory::CreateStage(&sk_random,
93 context,
94 dummyTextures);
95 GrAssert(stage);
96 return stage;
97 }
98
bsalomon@google.comc3841b92012-08-02 18:11:43 +000099
100 // TODO: When matrices are property of the custom-stage then remove the
101 // no-persp flag code below.
102 int effect = random_int(random, kEffectCount);
103 switch (effect) {
104 case kConvolution_EffectType: {
105 int direction = random_int(random, 2);
106 int kernelRadius = random_int(random, 1, 4);
107 float kernel[GrConvolutionEffect::kMaxKernelWidth];
108 for (int i = 0; i < GrConvolutionEffect::kMaxKernelWidth; i++) {
109 kernel[i] = random->nextF();
110 }
111 // does not work with perspective or mul-by-alpha-mask
112 stageDesc->fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
113 stageDesc->fInConfigFlags &= ~kMulByAlphaMask;
114 return SkNEW_ARGS(GrConvolutionEffect,
115 (NULL,
116 gKernelDirections[direction],
117 kernelRadius,
118 kernel));
119 }
120 case kErode_EffectType: {
121 int direction = random_int(random, 2);
122 int kernelRadius = random_int(random, 1, 4);
123 // does not work with perspective or mul-by-alpha-mask
124 stageDesc->fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
125 stageDesc->fInConfigFlags &= ~kMulByAlphaMask;
126 return SkNEW_ARGS(GrMorphologyEffect,
127 (NULL,
128 gKernelDirections[direction],
129 kernelRadius,
130 GrContext::kErode_MorphologyType));
131 }
132 case kDilate_EffectType: {
133 int direction = random_int(random, 2);
134 int kernelRadius = random_int(random, 1, 4);
135 // does not work with perspective or mul-by-alpha-mask
136 stageDesc->fOptFlags |= StageDesc::kNoPerspective_OptFlagBit;
137 stageDesc->fInConfigFlags &= ~kMulByAlphaMask;
138 return SkNEW_ARGS(GrMorphologyEffect,
139 (NULL,
140 gKernelDirections[direction],
141 kernelRadius,
142 GrContext::kDilate_MorphologyType));
143 }
bsalomon@google.comd4726202012-08-03 14:34:46 +0000144 /*
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000145 case kDiffuseDistant_EffectType: {
146 SkPoint3 direction = random_point3(random);
147 direction.normalize();
148 SkColor lightColor = random->nextU();
149 SkScalar surfaceScale = SkFloatToScalar(random->nextF());
150 SkScalar kd = SkFloatToScalar(random->nextF());
151 SkAutoTUnref<SkImageFilter> filter(SkLightingImageFilter::CreateDistantLitDiffuse(direction, lightColor, surfaceScale, kd));
152 // does not work with perspective or mul-by-alpha-mask
153 GrCustomStage* stage;
154 bool ok = filter->asNewCustomStage(&stage, NULL);
155 SkASSERT(ok);
156 return stage;
157 }
158 case kDiffusePoint_EffectType: {
159 SkPoint3 location = random_point3(random);
160 SkColor lightColor = random->nextU();
161 SkScalar surfaceScale = SkFloatToScalar(random->nextF());
162 SkScalar kd = SkFloatToScalar(random->nextF());
163 SkAutoTUnref<SkImageFilter> filter(SkLightingImageFilter::CreatePointLitDiffuse(location, lightColor, surfaceScale, kd));
164 // does not work with perspective or mul-by-alpha-mask
165 GrCustomStage* stage;
166 bool ok = filter->asNewCustomStage(&stage, NULL);
167 SkASSERT(ok);
168 return stage;
169 }
170 case kDiffuseSpot_EffectType: {
171 SkPoint3 location = random_point3(random);
172 SkPoint3 target = random_point3(random);
173 SkScalar cutoffAngle = SkFloatToScalar(random->nextF());
174 SkScalar specularExponent = SkFloatToScalar(random->nextF());
175 SkColor lightColor = random->nextU();
176 SkScalar surfaceScale = SkFloatToScalar(random->nextF());
177 SkScalar ks = SkFloatToScalar(random->nextF());
178 SkScalar shininess = SkFloatToScalar(random->nextF());
179 SkAutoTUnref<SkImageFilter> filter(SkLightingImageFilter::CreateSpotLitSpecular(
180 location, target, specularExponent, cutoffAngle, lightColor, surfaceScale, ks, shininess));
181 // does not work with perspective or mul-by-alpha-mask
182 GrCustomStage* stage;
183 bool ok = filter->asNewCustomStage(&stage, NULL);
184 SkASSERT(ok);
185 return stage;
186 }
187 case kSpecularDistant_EffectType: {
188 SkPoint3 direction = random_point3(random);
189 direction.normalize();
190 SkColor lightColor = random->nextU();
191 SkScalar surfaceScale = SkFloatToScalar(random->nextF());
192 SkScalar ks = SkFloatToScalar(random->nextF());
193 SkScalar shininess = SkFloatToScalar(random->nextF());
194 SkAutoTUnref<SkImageFilter> filter(SkLightingImageFilter::CreateDistantLitSpecular(direction, lightColor, surfaceScale, ks, shininess));
195 // does not work with perspective or mul-by-alpha-mask
196 GrCustomStage* stage;
197 bool ok = filter->asNewCustomStage(&stage, NULL);
198 SkASSERT(ok);
199 return stage;
200 }
201 case kSpecularPoint_EffectType: {
202 SkPoint3 location = random_point3(random);
203 SkColor lightColor = random->nextU();
204 SkScalar surfaceScale = SkFloatToScalar(random->nextF());
205 SkScalar ks = SkFloatToScalar(random->nextF());
206 SkScalar shininess = SkFloatToScalar(random->nextF());
207 SkAutoTUnref<SkImageFilter> filter(SkLightingImageFilter::CreatePointLitSpecular(location, lightColor, surfaceScale, ks, shininess));
208 // does not work with perspective or mul-by-alpha-mask
209 GrCustomStage* stage;
210 bool ok = filter->asNewCustomStage(&stage, NULL);
211 SkASSERT(ok);
212 return stage;
213 }
214 case kSpecularSpot_EffectType: {
215 SkPoint3 location = random_point3(random);
216 SkPoint3 target = random_point3(random);
217 SkScalar cutoffAngle = SkFloatToScalar(random->nextF());
218 SkScalar specularExponent = SkFloatToScalar(random->nextF());
219 SkColor lightColor = random->nextU();
220 SkScalar surfaceScale = SkFloatToScalar(random->nextF());
221 SkScalar ks = SkFloatToScalar(random->nextF());
222 SkScalar shininess = SkFloatToScalar(random->nextF());
223 SkAutoTUnref<SkImageFilter> filter(SkLightingImageFilter::CreateSpotLitSpecular(
224 location, target, specularExponent, cutoffAngle, lightColor, surfaceScale, ks, shininess));
225 // does not work with perspective or mul-by-alpha-mask
226 GrCustomStage* stage;
227 bool ok = filter->asNewCustomStage(&stage, NULL);
228 SkASSERT(ok);
229 return stage;
230 }
bsalomon@google.comd4726202012-08-03 14:34:46 +0000231 */
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000232 case kColorTable_EffectType: {
bsalomon@google.comd4726202012-08-03 14:34:46 +0000233 GrTexture* alphaTexture = dummyTextures[GrCustomStageTestFactory::kAlphaTextureIdx];
234 return SkNEW_ARGS(GrColorTableEffect, (alphaTexture));
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000235 }
236 default:
237 GrCrash("Unexpected custom effect type");
238 }
239 return NULL;
240}
241}
242
243bool GrGpuGL::programUnitTest() {
244
bsalomon@google.comd4726202012-08-03 14:34:46 +0000245 GrTextureDesc dummyDesc;
246 dummyDesc.fConfig = kSkia8888_PM_GrPixelConfig;
247 dummyDesc.fWidth = 34;
248 dummyDesc.fHeight = 18;
249 SkAutoTUnref<GrTexture> dummyTexture1(this->createTexture(dummyDesc, NULL, 0));
250 dummyDesc.fConfig = kAlpha_8_GrPixelConfig;
251 dummyDesc.fWidth = 16;
252 dummyDesc.fHeight = 22;
253 SkAutoTUnref<GrTexture> dummyTexture2(this->createTexture(dummyDesc, NULL, 0));
254
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000255 // GrGLSLGeneration glslGeneration =
256 GrGetGLSLGeneration(this->glBinding(), this->glInterface());
257 static const int STAGE_OPTS[] = {
258 0,
259 StageDesc::kNoPerspective_OptFlagBit,
260 };
261 static const int IN_CONFIG_FLAGS[] = {
262 StageDesc::kNone_InConfigFlag,
263 StageDesc::kSwapRAndB_InConfigFlag,
264 StageDesc::kSwapRAndB_InConfigFlag |
265 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag,
266 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag,
267 StageDesc::kSmearAlpha_InConfigFlag,
268 StageDesc::kSmearRed_InConfigFlag,
269 };
270
271 static const int NUM_TESTS = 512;
272
273 GrRandom random;
274 for (int t = 0; t < NUM_TESTS; ++t) {
275
276#if 0
277 GrPrintf("\nTest Program %d\n-------------\n", t);
278 static const int stop = -1;
279 if (t == stop) {
280 int breakpointhere = 9;
281 }
282#endif
283
284 ProgramDesc pdesc;
285 pdesc.fVertexLayout = 0;
286 pdesc.fEmitsPointSize = random.nextF() > .5f;
287 pdesc.fColorInput = random_int(&random, ProgramDesc::kColorInputCnt);
288 pdesc.fCoverageInput = random_int(&random, ProgramDesc::kColorInputCnt);
289
290 pdesc.fColorFilterXfermode = random_int(&random, SkXfermode::kCoeffModesCnt);
291
292 pdesc.fFirstCoverageStage = random_int(&random, GrDrawState::kNumStages);
293
294 pdesc.fVertexLayout |= random_bool(&random) ?
295 GrDrawTarget::kCoverage_VertexLayoutBit :
296 0;
297
298#if GR_GL_EXPERIMENTAL_GS
299 pdesc.fExperimentalGS = this->getCaps().fGeometryShaderSupport &&
300 random_bool(&random);
301#endif
302 pdesc.fOutputConfig = random_int(&random, ProgramDesc::kOutputConfigCnt);
303
304 bool edgeAA = random_bool(&random);
305 if (edgeAA) {
306 pdesc.fVertexLayout |= GrDrawTarget::kEdge_VertexLayoutBit;
307 if (this->getCaps().fShaderDerivativeSupport) {
308 pdesc.fVertexEdgeType = (GrDrawState::VertexEdgeType) random_int(&random, GrDrawState::kVertexEdgeTypeCnt);
309 } else {
310 pdesc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType;
311 }
312 } else {
313 }
314
315 pdesc.fColorMatrixEnabled = random_bool(&random);
316
317 if (this->getCaps().fDualSourceBlendingSupport) {
318 pdesc.fDualSrcOutput = random_int(&random, ProgramDesc::kDualSrcOutputCnt);
319 } else {
320 pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput;
321 }
322
323 SkAutoTUnref<const GrCustomStage> customStages[GrDrawState::kNumStages];
324
325 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
326 StageDesc& stage = pdesc.fStages[s];
327 // enable the stage?
328 if (random_bool(&random)) {
329 // use separate tex coords?
330 if (random_bool(&random)) {
331 int t = random_int(&random, GrDrawState::kMaxTexCoords);
332 pdesc.fVertexLayout |= StageTexCoordVertexLayoutBit(s, t);
333 }
334 stage.setEnabled(true);
335 }
336 // use text-formatted verts?
337 if (random_bool(&random)) {
338 pdesc.fVertexLayout |= kTextFormat_VertexLayoutBit;
339 }
340
341 stage.fCustomStageKey = 0;
342
bsalomon@google.comd4726202012-08-03 14:34:46 +0000343 stage.fOptFlags |= STAGE_OPTS[random_int(&random, GR_ARRAY_COUNT(STAGE_OPTS))];
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000344 stage.fInConfigFlags = IN_CONFIG_FLAGS[random_int(&random, GR_ARRAY_COUNT(IN_CONFIG_FLAGS))];
345
bsalomon@google.comd4726202012-08-03 14:34:46 +0000346 if (stage.isEnabled()) {
347 GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()};
348 customStages[s].reset(create_random_effect(&stage,
349 &random,
350 getContext(),
351 dummyTextures));
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000352 if (NULL != customStages[s]) {
353 stage.fCustomStageKey =
354 customStages[s]->getFactory().glStageKey(*customStages[s], this->glCaps());
355 }
356 }
357 }
358 GR_STATIC_ASSERT(sizeof(customStages) ==
359 GrDrawState::kNumStages * sizeof(GrCustomStage*));
360 const GrCustomStage** stages = reinterpret_cast<const GrCustomStage**>(&customStages);
361 SkAutoTUnref<GrGLProgram> program(GrGLProgram::Create(this->glContextInfo(),
362 pdesc,
363 stages));
364 if (NULL == program.get()) {
365 return false;
366 }
367 }
368 return true;
369}
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000370
371static void GLProgramsTest(skiatest::Reporter* reporter, GrContext* context) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000372 GrGpuGL* shadersGpu = static_cast<GrGpuGL*>(context->getGpu());
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000373 REPORTER_ASSERT(reporter, shadersGpu->programUnitTest());
374}
375
376
377#include "TestClassDef.h"
378DEFINE_GPUTESTCLASS("GLPrograms", GLProgramsTestClass, GLProgramsTest)
379
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000380#endif