# [iphone] 如何提高自定義OpenGL ES 2.0深度紋理生成的性能？

``````void main()
{
float distanceFromCenter = length(impostorSpaceCoordinate);

// the step function doesn't count as a conditional
float inCircleMultiplier = step(distanceFromCenter, 1.0);

float calculatedDepth = sqrt(1.0 - distanceFromCenter * distanceFromCenter * inCircleMultiplier);

// Inlined color encoding for the depth values
float ceiledValue = ceil(currentDepthValue * 765.0) * inCircleMultiplier;

vec3 intDepthValue = (vec3(ceiledValue) * scaleDownFactor) - (stepValues * inCircleMultiplier);

// use the result of the step to combine results
gl_FragColor = vec4(1.0 - inCircleMultiplier) + vec4(intDepthValue, inCircleMultiplier);

}
``````
Question

``````precision mediump float;

varying mediump vec2 impostorSpaceCoordinate;
varying mediump float normalizedDepth;

const vec3 stepValues = vec3(2.0, 1.0, 0.0);
const float scaleDownFactor = 1.0 / 255.0;

void main()
{
float distanceFromCenter = length(impostorSpaceCoordinate);
if (distanceFromCenter > 1.0)
{
gl_FragColor = vec4(1.0);
}
else
{
float calculatedDepth = sqrt(1.0 - distanceFromCenter * distanceFromCenter);

// Inlined color encoding for the depth values
float ceiledValue = ceil(currentDepthValue * 765.0);

vec3 intDepthValue = (vec3(ceiledValue) * scaleDownFactor) - stepValues;

gl_FragColor = vec4(intDepthValue, 1.0);
}
}
``````

``````precision mediump float;

varying mediump vec2 impostorSpaceCoordinate;
varying mediump float normalizedDepth;

void main()
{
gl_FragColor = vec4(adjustedSphereRadius * normalizedDepth * (impostorSpaceCoordinate + 1.0) / 2.0, normalizedDepth, 1.0);
}
``````

``````precision mediump float;

void main()
{
gl_FragColor = vec4(0.5, 0.5, 0.5, 1.0);
}
``````

iPad 1上的渲染時間下降到1.1 - 2.3 ms（iPhone 4上為1.3 ms）。

• 你的深度著色器非常昂貴
• 在禁用`GL_DEPTH`測試時，會在深度傳遞中體驗大量透支