2011-08-16 15:45:58 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2012-08-03 14:34:46 +00:00
|
|
|
// This is a GPU-backend specific test. It relies on static intializers to work
|
2012-08-02 14:03:32 +00:00
|
|
|
|
2012-08-03 14:54:45 +00:00
|
|
|
#include "SkTypes.h"
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU && SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
|
|
|
|
|
2012-05-31 15:07:19 +00:00
|
|
|
#include "gl/GrGpuGL.h"
|
2012-10-25 19:00:29 +00:00
|
|
|
#include "GrBackendEffectFactory.h"
|
2013-02-04 16:13:32 +00:00
|
|
|
#include "GrContextFactory.h"
|
2013-03-20 19:19:53 +00:00
|
|
|
#include "GrDrawEffect.h"
|
2012-08-27 12:53:13 +00:00
|
|
|
#include "effects/GrConfigConversionEffect.h"
|
2012-08-07 17:36:29 +00:00
|
|
|
|
2013-05-22 14:34:04 +00:00
|
|
|
#include "SkChecksum.h"
|
2012-11-21 22:38:36 +00:00
|
|
|
#include "SkRandom.h"
|
2012-08-02 18:11:43 +00:00
|
|
|
#include "Test.h"
|
|
|
|
|
2013-09-09 20:09:12 +00:00
|
|
|
void GrGLProgramDesc::setRandom(SkRandom* random,
|
2013-03-27 18:14:57 +00:00
|
|
|
const GrGpuGL* gpu,
|
2013-05-22 14:34:04 +00:00
|
|
|
const GrRenderTarget* dstRenderTarget,
|
|
|
|
const GrTexture* dstCopyTexture,
|
|
|
|
const GrEffectStage* stages[],
|
|
|
|
int numColorStages,
|
|
|
|
int numCoverageStages,
|
2013-04-01 20:06:51 +00:00
|
|
|
int currAttribIndex) {
|
2013-05-22 14:34:04 +00:00
|
|
|
int numEffects = numColorStages + numCoverageStages;
|
|
|
|
size_t keyLength = KeyLength(numEffects);
|
|
|
|
fKey.reset(keyLength);
|
|
|
|
*this->atOffset<uint32_t, kLengthOffset>() = static_cast<uint32_t>(keyLength);
|
|
|
|
memset(this->header(), 0, kHeaderSize);
|
2013-04-01 20:06:51 +00:00
|
|
|
|
2013-05-22 14:34:04 +00:00
|
|
|
KeyHeader* header = this->header();
|
|
|
|
header->fEmitsPointSize = random->nextBool();
|
|
|
|
|
|
|
|
header->fPositionAttributeIndex = 0;
|
2013-04-01 20:06:51 +00:00
|
|
|
|
2013-04-02 07:01:34 +00:00
|
|
|
// if the effects have used up all off the available attributes,
|
2013-04-01 20:06:51 +00:00
|
|
|
// don't try to use color or coverage attributes as input
|
|
|
|
do {
|
2013-10-01 18:43:29 +00:00
|
|
|
header->fColorInput = static_cast<GrGLProgramDesc::ColorInput>(
|
|
|
|
random->nextULessThan(kColorInputCnt));
|
2013-04-02 07:01:34 +00:00
|
|
|
} while (GrDrawState::kMaxVertexAttribCnt <= currAttribIndex &&
|
2013-05-22 14:34:04 +00:00
|
|
|
kAttribute_ColorInput == header->fColorInput);
|
|
|
|
header->fColorAttributeIndex = (header->fColorInput == kAttribute_ColorInput) ?
|
|
|
|
currAttribIndex++ :
|
|
|
|
-1;
|
2013-04-01 20:06:51 +00:00
|
|
|
|
|
|
|
do {
|
2013-10-01 18:43:29 +00:00
|
|
|
header->fCoverageInput = static_cast<GrGLProgramDesc::ColorInput>(
|
|
|
|
random->nextULessThan(kColorInputCnt));
|
2013-04-01 20:06:51 +00:00
|
|
|
} while (GrDrawState::kMaxVertexAttribCnt <= currAttribIndex &&
|
2013-05-22 14:34:04 +00:00
|
|
|
kAttribute_ColorInput == header->fCoverageInput);
|
|
|
|
header->fCoverageAttributeIndex = (header->fCoverageInput == kAttribute_ColorInput) ?
|
|
|
|
currAttribIndex++ :
|
|
|
|
-1;
|
2013-02-12 21:45:24 +00:00
|
|
|
|
2013-10-01 18:43:29 +00:00
|
|
|
header->fColorFilterXfermode = static_cast<SkXfermode::Mode>(
|
|
|
|
random->nextULessThan(SkXfermode::kLastCoeffMode + 1));
|
2013-02-12 21:45:24 +00:00
|
|
|
|
|
|
|
#if GR_GL_EXPERIMENTAL_GS
|
2013-05-22 14:34:04 +00:00
|
|
|
header->fExperimentalGS = gpu->caps()->geometryShaderSupport() && random->nextBool();
|
2013-02-12 21:45:24 +00:00
|
|
|
#endif
|
|
|
|
|
2013-05-22 14:34:04 +00:00
|
|
|
header->fDiscardIfZeroCoverage = random->nextBool();
|
2013-03-27 07:01:04 +00:00
|
|
|
|
2013-04-01 20:06:51 +00:00
|
|
|
bool useLocalCoords = random->nextBool() && currAttribIndex < GrDrawState::kMaxVertexAttribCnt;
|
2013-05-22 14:34:04 +00:00
|
|
|
header->fLocalCoordAttributeIndex = useLocalCoords ? currAttribIndex++ : -1;
|
|
|
|
|
|
|
|
header->fColorEffectCnt = numColorStages;
|
|
|
|
header->fCoverageEffectCnt = numCoverageStages;
|
2013-03-20 19:19:53 +00:00
|
|
|
|
2013-03-29 20:30:50 +00:00
|
|
|
bool dstRead = false;
|
2013-05-22 14:34:04 +00:00
|
|
|
bool fragPos = false;
|
2013-10-04 20:02:53 +00:00
|
|
|
bool vertexCode = false;
|
2013-05-22 14:34:04 +00:00
|
|
|
int numStages = numColorStages + numCoverageStages;
|
|
|
|
for (int s = 0; s < numStages; ++s) {
|
|
|
|
const GrBackendEffectFactory& factory = (*stages[s]->getEffect())->getFactory();
|
|
|
|
GrDrawEffect drawEffect(*stages[s], useLocalCoords);
|
|
|
|
this->effectKeys()[s] = factory.glEffectKey(drawEffect, gpu->glCaps());
|
|
|
|
if ((*stages[s]->getEffect())->willReadDstColor()) {
|
|
|
|
dstRead = true;
|
|
|
|
}
|
|
|
|
if ((*stages[s]->getEffect())->willReadFragmentPosition()) {
|
|
|
|
fragPos = true;
|
2013-02-12 21:45:24 +00:00
|
|
|
}
|
2013-10-04 20:02:53 +00:00
|
|
|
if ((*stages[s]->getEffect())->hasVertexCode()) {
|
|
|
|
vertexCode = true;
|
|
|
|
}
|
2013-02-12 21:45:24 +00:00
|
|
|
}
|
2013-03-01 18:21:22 +00:00
|
|
|
|
2013-03-29 20:30:50 +00:00
|
|
|
if (dstRead) {
|
2013-05-22 14:34:04 +00:00
|
|
|
header->fDstReadKey = GrGLShaderBuilder::KeyForDstRead(dstCopyTexture, gpu->glCaps());
|
|
|
|
} else {
|
|
|
|
header->fDstReadKey = 0;
|
|
|
|
}
|
|
|
|
if (fragPos) {
|
|
|
|
header->fFragPosKey = GrGLShaderBuilder::KeyForFragmentPosition(dstRenderTarget,
|
|
|
|
gpu->glCaps());
|
|
|
|
} else {
|
|
|
|
header->fFragPosKey = 0;
|
2013-03-29 20:30:50 +00:00
|
|
|
}
|
2013-04-19 13:14:45 +00:00
|
|
|
|
2013-10-04 20:02:53 +00:00
|
|
|
header->fHasVertexCode = vertexCode ||
|
|
|
|
useLocalCoords ||
|
|
|
|
kAttribute_ColorInput == header->fColorInput ||
|
|
|
|
kAttribute_ColorInput == header->fCoverageInput;
|
|
|
|
|
2013-04-19 13:14:45 +00:00
|
|
|
CoverageOutput coverageOutput;
|
|
|
|
bool illegalCoverageOutput;
|
|
|
|
do {
|
|
|
|
coverageOutput = static_cast<CoverageOutput>(random->nextULessThan(kCoverageOutputCnt));
|
|
|
|
illegalCoverageOutput = (!gpu->caps()->dualSourceBlendingSupport() &&
|
|
|
|
CoverageOutputUsesSecondaryOutput(coverageOutput)) ||
|
2013-04-19 13:25:28 +00:00
|
|
|
(!dstRead && kCombineWithDst_CoverageOutput == coverageOutput);
|
2013-04-19 13:14:45 +00:00
|
|
|
} while (illegalCoverageOutput);
|
|
|
|
|
2013-05-22 14:34:04 +00:00
|
|
|
header->fCoverageOutput = coverageOutput;
|
|
|
|
|
|
|
|
*this->checksum() = 0;
|
|
|
|
*this->checksum() = SkChecksum::Compute(reinterpret_cast<uint32_t*>(fKey.get()), keyLength);
|
|
|
|
fInitialized = true;
|
2013-02-12 21:45:24 +00:00
|
|
|
}
|
|
|
|
|
2013-02-04 18:39:24 +00:00
|
|
|
bool GrGpuGL::programUnitTest(int maxStages) {
|
|
|
|
|
2012-08-03 14:34:46 +00:00
|
|
|
GrTextureDesc dummyDesc;
|
2013-05-22 14:34:04 +00:00
|
|
|
dummyDesc.fFlags = kRenderTarget_GrTextureFlagBit;
|
2013-02-07 14:43:04 +00:00
|
|
|
dummyDesc.fConfig = kSkia8888_GrPixelConfig;
|
2012-08-03 14:34:46 +00:00
|
|
|
dummyDesc.fWidth = 34;
|
|
|
|
dummyDesc.fHeight = 18;
|
|
|
|
SkAutoTUnref<GrTexture> dummyTexture1(this->createTexture(dummyDesc, NULL, 0));
|
2013-05-22 14:34:04 +00:00
|
|
|
dummyDesc.fFlags = kNone_GrTextureFlags;
|
2012-08-03 14:34:46 +00:00
|
|
|
dummyDesc.fConfig = kAlpha_8_GrPixelConfig;
|
|
|
|
dummyDesc.fWidth = 16;
|
|
|
|
dummyDesc.fHeight = 22;
|
|
|
|
SkAutoTUnref<GrTexture> dummyTexture2(this->createTexture(dummyDesc, NULL, 0));
|
|
|
|
|
2012-08-02 18:11:43 +00:00
|
|
|
static const int NUM_TESTS = 512;
|
|
|
|
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom random;
|
2012-08-02 18:11:43 +00:00
|
|
|
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
|
|
|
|
|
2013-03-27 18:14:57 +00:00
|
|
|
GrGLProgramDesc pdesc;
|
2012-08-02 18:11:43 +00:00
|
|
|
|
2013-04-01 20:06:51 +00:00
|
|
|
int currAttribIndex = 1; // we need to always leave room for position
|
2013-03-12 12:26:08 +00:00
|
|
|
int attribIndices[2];
|
2013-03-29 20:30:50 +00:00
|
|
|
GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()};
|
2013-05-22 14:34:04 +00:00
|
|
|
|
|
|
|
int numStages = random.nextULessThan(maxStages + 1);
|
|
|
|
int numColorStages = random.nextULessThan(numStages + 1);
|
|
|
|
int numCoverageStages = numStages - numColorStages;
|
|
|
|
|
|
|
|
SkAutoSTMalloc<8, const GrEffectStage*> stages(numStages);
|
|
|
|
|
|
|
|
for (int s = 0; s < numStages; ++s) {
|
|
|
|
SkAutoTUnref<const GrEffectRef> effect(GrEffectTestFactory::CreateStage(
|
|
|
|
&random,
|
|
|
|
this->getContext(),
|
|
|
|
*this->caps(),
|
|
|
|
dummyTextures));
|
|
|
|
int numAttribs = (*effect)->numVertexAttribs();
|
|
|
|
|
|
|
|
// If adding this effect would exceed the max attrib count then generate a
|
|
|
|
// new random effect.
|
|
|
|
if (currAttribIndex + numAttribs > GrDrawState::kMaxVertexAttribCnt) {
|
|
|
|
--s;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < numAttribs; ++i) {
|
|
|
|
attribIndices[i] = currAttribIndex++;
|
2012-08-02 18:11:43 +00:00
|
|
|
}
|
2013-07-15 13:54:06 +00:00
|
|
|
GrEffectStage* stage = SkNEW_ARGS(GrEffectStage,
|
|
|
|
(effect.get(), attribIndices[0], attribIndices[1]));
|
2013-05-22 14:34:04 +00:00
|
|
|
stages[s] = stage;
|
2012-08-02 18:11:43 +00:00
|
|
|
}
|
2013-03-29 20:30:50 +00:00
|
|
|
const GrTexture* dstTexture = random.nextBool() ? dummyTextures[0] : dummyTextures[1];
|
2013-05-22 14:34:04 +00:00
|
|
|
pdesc.setRandom(&random,
|
|
|
|
this,
|
|
|
|
dummyTextures[0]->asRenderTarget(),
|
|
|
|
dstTexture,
|
|
|
|
stages.get(),
|
|
|
|
numColorStages,
|
|
|
|
numCoverageStages,
|
|
|
|
currAttribIndex);
|
2013-04-02 07:01:34 +00:00
|
|
|
|
2013-09-05 18:28:24 +00:00
|
|
|
SkAutoTUnref<GrGLProgram> program(GrGLProgram::Create(this,
|
2012-08-02 18:11:43 +00:00
|
|
|
pdesc,
|
2013-06-06 20:28:57 +00:00
|
|
|
stages,
|
|
|
|
stages + numColorStages));
|
2013-05-22 14:34:04 +00:00
|
|
|
for (int s = 0; s < numStages; ++s) {
|
2013-05-09 13:45:02 +00:00
|
|
|
SkDELETE(stages[s]);
|
|
|
|
}
|
2012-08-02 18:11:43 +00:00
|
|
|
if (NULL == program.get()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2011-08-16 15:45:58 +00:00
|
|
|
|
2013-02-04 16:13:32 +00:00
|
|
|
static void GLProgramsTest(skiatest::Reporter* reporter, GrContextFactory* factory) {
|
|
|
|
for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
|
|
|
|
GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(type));
|
|
|
|
if (NULL != context) {
|
2013-02-04 18:39:24 +00:00
|
|
|
GrGpuGL* gpu = static_cast<GrGpuGL*>(context->getGpu());
|
2013-06-13 19:34:18 +00:00
|
|
|
int maxStages = 6;
|
2013-02-04 18:39:24 +00:00
|
|
|
#if SK_ANGLE
|
|
|
|
// Some long shaders run out of temporary registers in the D3D compiler on ANGLE.
|
|
|
|
if (type == GrContextFactory::kANGLE_GLContextType) {
|
|
|
|
maxStages = 3;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
REPORTER_ASSERT(reporter, gpu->programUnitTest(maxStages));
|
2013-02-04 16:13:32 +00:00
|
|
|
}
|
|
|
|
}
|
2011-08-16 15:45:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "TestClassDef.h"
|
|
|
|
DEFINE_GPUTESTCLASS("GLPrograms", GLProgramsTestClass, GLProgramsTest)
|
|
|
|
|
2012-08-23 18:14:13 +00:00
|
|
|
// This is evil evil evil. The linker may throw away whole translation units as dead code if it
|
2012-10-17 13:36:14 +00:00
|
|
|
// thinks none of the functions are called. It will do this even if there are static initializers
|
2012-08-07 17:36:29 +00:00
|
|
|
// in the unit that could pass pointers to functions from the unit out to other translation units!
|
|
|
|
// We force some of the effects that would otherwise be discarded to link here.
|
|
|
|
|
|
|
|
#include "SkLightingImageFilter.h"
|
2012-08-13 14:22:17 +00:00
|
|
|
#include "SkMagnifierImageFilter.h"
|
2012-10-17 13:36:14 +00:00
|
|
|
#include "SkColorMatrixFilter.h"
|
2013-09-06 15:31:06 +00:00
|
|
|
#include "SkBitmapAlphaThresholdShader.h"
|
2012-08-07 17:36:29 +00:00
|
|
|
|
|
|
|
void forceLinking();
|
|
|
|
|
|
|
|
void forceLinking() {
|
|
|
|
SkLightingImageFilter::CreateDistantLitDiffuse(SkPoint3(0,0,0), 0, 0, 0);
|
2012-08-13 14:22:17 +00:00
|
|
|
SkMagnifierImageFilter mag(SkRect::MakeWH(SK_Scalar1, SK_Scalar1), SK_Scalar1);
|
2013-01-28 14:26:09 +00:00
|
|
|
GrConfigConversionEffect::Create(NULL,
|
|
|
|
false,
|
|
|
|
GrConfigConversionEffect::kNone_PMConversion,
|
|
|
|
SkMatrix::I());
|
2012-10-17 13:36:14 +00:00
|
|
|
SkScalar matrix[20];
|
|
|
|
SkColorMatrixFilter cmf(matrix);
|
2013-09-06 15:31:06 +00:00
|
|
|
SkBitmapAlphaThresholdShader::Create(SkBitmap(), SkRegion(), 0x80);
|
2012-08-07 17:36:29 +00:00
|
|
|
}
|
|
|
|
|
2012-08-02 14:03:32 +00:00
|
|
|
#endif
|