3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
9 // This is a GPU-backend specific test. It relies on static intializers to work
13 #if SK_SUPPORT_GPU && SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
15 #include "GrBackendProcessorFactory.h"
16 #include "GrContextFactory.h"
17 #include "GrOptDrawState.h"
18 #include "effects/GrConfigConversionEffect.h"
19 #include "gl/GrGLPathRendering.h"
20 #include "gl/GrGpuGL.h"
21 #include "SkChecksum.h"
25 static void get_stage_stats(const GrFragmentStage stage, bool* readsDst,
26 bool* readsFragPosition, bool* requiresVertexShader) {
27 if (stage.getFragmentProcessor()->willReadDstColor()) {
30 if (stage.getProcessor()->willReadFragmentPosition()) {
31 *readsFragPosition = true;
35 bool GrGLProgramDesc::setRandom(SkRandom* random,
37 const GrRenderTarget* dstRenderTarget,
38 const GrTexture* dstCopyTexture,
39 const GrGeometryStage* geometryProcessor,
40 const GrFragmentStage* stages[],
42 int numCoverageStages,
44 GrGpu::DrawType drawType) {
45 bool isPathRendering = GrGpu::IsPathRenderingDrawType(drawType);
46 bool useLocalCoords = !isPathRendering &&
48 currAttribIndex < GrDrawState::kMaxVertexAttribCnt;
50 int numStages = numColorStages + numCoverageStages;
53 GR_STATIC_ASSERT(0 == kEffectKeyOffsetsAndLengthOffset % sizeof(uint32_t));
55 // Make room for everything up to and including the array of offsets to effect keys.
56 fKey.push_back_n(kEffectKeyOffsetsAndLengthOffset + 2 * sizeof(uint16_t) * (numStages +
57 (geometryProcessor ? 1 : 0)));
61 bool vertexShader = SkToBool(geometryProcessor);
63 if (geometryProcessor) {
64 const GrGeometryStage* stage = geometryProcessor;
65 uint16_t* offsetAndSize = reinterpret_cast<uint16_t*>(fKey.begin() +
66 kEffectKeyOffsetsAndLengthOffset +
67 offset * 2 * sizeof(uint16_t));
68 uint32_t effectKeyOffset = fKey.count();
69 if (effectKeyOffset > SK_MaxU16) {
73 GrProcessorKeyBuilder b(&fKey);
74 uint16_t effectKeySize;
75 if (!GetProcessorKey(*stage, gpu->glCaps(), useLocalCoords, &b, &effectKeySize)) {
80 fragPos = stage->getProcessor()->willReadFragmentPosition();
81 offsetAndSize[0] = effectKeyOffset;
82 offsetAndSize[1] = effectKeySize;
86 for (int s = 0; s < numStages; ++s, ++offset) {
87 const GrFragmentStage* stage = stages[s];
88 uint16_t* offsetAndSize = reinterpret_cast<uint16_t*>(fKey.begin() +
89 kEffectKeyOffsetsAndLengthOffset +
90 offset * 2 * sizeof(uint16_t));
91 uint32_t effectKeyOffset = fKey.count();
92 if (effectKeyOffset > SK_MaxU16) {
96 GrProcessorKeyBuilder b(&fKey);
97 uint16_t effectKeySize;
98 if (!GetProcessorKey(*stages[s], gpu->glCaps(), useLocalCoords, &b, &effectKeySize)) {
102 get_stage_stats(*stage, &dstRead, &fragPos, &vertexShader);
103 offsetAndSize[0] = effectKeyOffset;
104 offsetAndSize[1] = effectKeySize;
107 KeyHeader* header = this->header();
108 memset(header, 0, kHeaderSize);
109 header->fEmitsPointSize = random->nextBool();
111 header->fPositionAttributeIndex = 0;
113 // if the effects have used up all off the available attributes,
114 // don't try to use color or coverage attributes as input
116 header->fColorInput = static_cast<GrGLProgramDesc::ColorInput>(
117 random->nextULessThan(kColorInputCnt));
118 } while ((GrDrawState::kMaxVertexAttribCnt <= currAttribIndex || isPathRendering) &&
119 kAttribute_ColorInput == header->fColorInput);
120 header->fColorAttributeIndex = (header->fColorInput == kAttribute_ColorInput) ?
125 header->fCoverageInput = static_cast<GrGLProgramDesc::ColorInput>(
126 random->nextULessThan(kColorInputCnt));
127 } while ((GrDrawState::kMaxVertexAttribCnt <= currAttribIndex || isPathRendering) &&
128 kAttribute_ColorInput == header->fCoverageInput);
129 header->fCoverageAttributeIndex = (header->fCoverageInput == kAttribute_ColorInput) ?
132 bool useGS = random->nextBool();
133 #if GR_GL_EXPERIMENTAL_GS
134 header->fExperimentalGS = gpu->caps()->geometryShaderSupport() && useGS;
139 header->fLocalCoordAttributeIndex = useLocalCoords ? currAttribIndex++ : -1;
141 header->fColorEffectCnt = numColorStages;
142 header->fCoverageEffectCnt = numCoverageStages;
145 header->fDstReadKey = SkToU8(GrGLFragmentShaderBuilder::KeyForDstRead(dstCopyTexture,
148 header->fDstReadKey = 0;
151 header->fFragPosKey = SkToU8(GrGLFragmentShaderBuilder::KeyForFragmentPosition(dstRenderTarget,
154 header->fFragPosKey = 0;
157 header->fUseFragShaderOnly = isPathRendering && gpu->glPathRendering()->texturingMode() ==
158 GrGLPathRendering::FixedFunction_TexturingMode;
159 header->fHasGeometryProcessor = vertexShader;
161 GrOptDrawState::PrimaryOutputType primaryOutput;
162 GrOptDrawState::SecondaryOutputType secondaryOutput;
164 primaryOutput = GrOptDrawState::kModulate_PrimaryOutputType;
166 primaryOutput = static_cast<GrOptDrawState::PrimaryOutputType>(
167 random->nextULessThan(GrOptDrawState::kPrimaryOutputTypeCnt));
170 if (GrOptDrawState::kCombineWithDst_PrimaryOutputType == primaryOutput ||
171 !gpu->caps()->dualSourceBlendingSupport()) {
172 secondaryOutput = GrOptDrawState::kNone_SecondaryOutputType;
174 secondaryOutput = static_cast<GrOptDrawState::SecondaryOutputType>(
175 random->nextULessThan(GrOptDrawState::kSecondaryOutputTypeCnt));
178 header->fPrimaryOutputType = primaryOutput;
179 header->fSecondaryOutputType = secondaryOutput;
185 // TODO clean this up, we have to do this to test geometry processors but there has got to be
186 // a better way. In the mean time, we actually fill out these generic vertex attribs below with
187 // the correct vertex attribs from the GP. We have to ensure, however, we don't try to add more
188 // than two attributes.
189 GrVertexAttrib genericVertexAttribs[] = {
190 { kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding },
191 { kVec2f_GrVertexAttribType, 0, kGeometryProcessor_GrVertexAttribBinding },
192 { kVec2f_GrVertexAttribType, 0, kGeometryProcessor_GrVertexAttribBinding }
196 * convert sl type to vertexattrib type, not a complete implementation, only use for debugging
198 GrVertexAttribType convert_sltype_to_attribtype(GrSLType type) {
200 case kFloat_GrSLType:
201 return kFloat_GrVertexAttribType;
202 case kVec2f_GrSLType:
203 return kVec2f_GrVertexAttribType;
204 case kVec3f_GrSLType:
205 return kVec3f_GrVertexAttribType;
206 case kVec4f_GrSLType:
207 return kVec4f_GrVertexAttribType;
209 SkFAIL("Type isn't convertible");
210 return kFloat_GrVertexAttribType;
213 // TODO end test hack
216 bool GrGpuGL::programUnitTest(int maxStages) {
218 GrTextureDesc dummyDesc;
219 dummyDesc.fFlags = kRenderTarget_GrTextureFlagBit;
220 dummyDesc.fConfig = kSkia8888_GrPixelConfig;
221 dummyDesc.fWidth = 34;
222 dummyDesc.fHeight = 18;
223 SkAutoTUnref<GrTexture> dummyTexture1(this->createTexture(dummyDesc, NULL, 0));
224 dummyDesc.fFlags = kNone_GrTextureFlags;
225 dummyDesc.fConfig = kAlpha_8_GrPixelConfig;
226 dummyDesc.fWidth = 16;
227 dummyDesc.fHeight = 22;
228 SkAutoTUnref<GrTexture> dummyTexture2(this->createTexture(dummyDesc, NULL, 0));
230 if (!dummyTexture1 || ! dummyTexture2) {
234 static const int NUM_TESTS = 512;
237 for (int t = 0; t < NUM_TESTS; ++t) {
240 GrPrintf("\nTest Program %d\n-------------\n", t);
241 static const int stop = -1;
243 int breakpointhere = 9;
247 GrGLProgramDesc pdesc;
249 int currAttribIndex = 1; // we need to always leave room for position
250 int currTextureCoordSet = 0;
251 GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()};
253 int numStages = random.nextULessThan(maxStages + 1);
254 int numColorStages = random.nextULessThan(numStages + 1);
255 int numCoverageStages = numStages - numColorStages;
257 SkAutoSTMalloc<8, const GrFragmentStage*> stages(numStages);
259 bool usePathRendering = this->glCaps().pathRenderingSupport() && random.nextBool();
261 GrGpu::DrawType drawType = usePathRendering ? GrGpu::kDrawPath_DrawType :
262 GrGpu::kDrawPoints_DrawType;
264 SkAutoTDelete<GrGeometryStage> geometryProcessor;
265 bool hasGeometryProcessor = usePathRendering ? false : random.nextBool();
266 if (hasGeometryProcessor) {
268 SkAutoTUnref<const GrGeometryProcessor> effect(
269 GrProcessorTestFactory<GrGeometryProcessor>::CreateStage(&random, this->getContext(), *this->caps(),
272 // Only geometryProcessor can use vertex shader
273 GrGeometryStage* stage = SkNEW_ARGS(GrGeometryStage, (effect.get()));
274 geometryProcessor.reset(stage);
276 // we have to set dummy vertex attribs
277 const GrGeometryProcessor::VertexAttribArray& v = effect->getVertexAttribs();
278 int numVertexAttribs = v.count();
280 SkASSERT(GrGeometryProcessor::kMaxVertexAttribs == 2 &&
281 GrGeometryProcessor::kMaxVertexAttribs >= numVertexAttribs);
282 size_t runningStride = GrVertexAttribTypeSize(genericVertexAttribs[0].fType);
283 for (int i = 0; i < numVertexAttribs; i++) {
284 genericVertexAttribs[i + 1].fOffset = runningStride;
285 genericVertexAttribs[i + 1].fType =
286 convert_sltype_to_attribtype(v[i].getType());
287 runningStride += GrVertexAttribTypeSize(genericVertexAttribs[i + 1].fType);
290 // update the vertex attributes with the ds
291 GrDrawState* ds = this->drawState();
292 ds->setVertexAttribs<genericVertexAttribs>(numVertexAttribs + 1, runningStride);
293 currAttribIndex = numVertexAttribs + 1;
297 for (int s = 0; s < numStages;) {
298 SkAutoTUnref<const GrFragmentProcessor> effect(
299 GrProcessorTestFactory<GrFragmentProcessor>::CreateStage(
306 // If adding this effect would exceed the max texture coord set count then generate a
307 // new random effect.
308 if (usePathRendering && this->glPathRendering()->texturingMode() ==
309 GrGLPathRendering::FixedFunction_TexturingMode) {;
310 int numTransforms = effect->numTransforms();
311 if (currTextureCoordSet + numTransforms > this->glCaps().maxFixedFunctionTextureCoords()) {
314 currTextureCoordSet += numTransforms;
316 GrFragmentStage* stage = SkNEW_ARGS(GrFragmentStage, (effect.get()));
321 const GrTexture* dstTexture = random.nextBool() ? dummyTextures[0] : dummyTextures[1];
322 if (!pdesc.setRandom(&random,
324 dummyTextures[0]->asRenderTarget(),
326 geometryProcessor.get(),
335 SkAutoTUnref<GrGLProgram> program(GrGLProgram::Create(this,
337 geometryProcessor.get(),
339 stages + numColorStages));
340 for (int s = 0; s < numStages; ++s) {
343 if (NULL == program.get()) {
347 // We have to reset the drawstate because we might have added a gp
348 this->drawState()->reset();
353 DEF_GPUTEST(GLPrograms, reporter, factory) {
354 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
355 GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(type));
357 GrGpuGL* gpu = static_cast<GrGpuGL*>(context->getGpu());
360 // Some long shaders run out of temporary registers in the D3D compiler on ANGLE.
361 if (type == GrContextFactory::kANGLE_GLContextType) {
365 REPORTER_ASSERT(reporter, gpu->programUnitTest(maxStages));
370 // This is evil evil evil. The linker may throw away whole translation units as dead code if it
371 // thinks none of the functions are called. It will do this even if there are static initializers
372 // in the unit that could pass pointers to functions from the unit out to other translation units!
373 // We force some of the effects that would otherwise be discarded to link here.
375 #include "SkAlphaThresholdFilter.h"
376 #include "SkColorMatrixFilter.h"
377 #include "SkLightingImageFilter.h"
378 #include "SkMagnifierImageFilter.h"
382 void forceLinking() {
383 SkLightingImageFilter::CreateDistantLitDiffuse(SkPoint3(0,0,0), 0, 0, 0);
384 SkAlphaThresholdFilter::Create(SkRegion(), .5f, .5f);
385 SkAutoTUnref<SkImageFilter> mag(SkMagnifierImageFilter::Create(
386 SkRect::MakeWH(SK_Scalar1, SK_Scalar1), SK_Scalar1));
387 GrConfigConversionEffect::Create(NULL,
389 GrConfigConversionEffect::kNone_PMConversion,
392 SkAutoTUnref<SkColorMatrixFilter> cmf(SkColorMatrixFilter::Create(matrix));