|
|
以下是一段cg函数,在FragmentProgram中调用,使用的cg profile是ps_3_0。该函数能够通过编译,但在运行时会造成输出的图像不正常。这种“不正常”不是指由算法计算错误引发的错误的效果(跟函数本身的输出没什么关系),看起来更像是“使用显存随机数据填充的图像”(换句话说就是乱的,像是某种GPU运行异常的结果)。
希望熟悉cg的朋友帮忙看下,为什么会出现这种情况?
是因为在ps3.0下有循环操作的限制造成的GPU运行异常吗?我查阅了资料,没有找到相关说明。
float function(sampler2D directionsfield, sampler2D depthfield, int size, int steps, float2 depthClipper, float2 screenSpaceCoord)
{
float singleCoord = 1.0f / float(size);
float singleStep = 1.0f / float(steps);
float occlusion = 0.0f;
float scale = 1.0f / 16.0f;
float2 coord = float2(0.0f, 0.0f);
for(int hsize=0; hsize<size; ++hsize)
{
for(int vsize=0; vsize<size; ++vsize)
{
float3 direction = tex2D(directionsfield, coord);
direction = normalize(direction);
direction /= direction.z;
direction *= singleStep;
bool detected = false;
float2 depthfieldCoord = screenSpaceCoord;
float stepDepth = 0.0f;
for( int step=0; step<steps; ++step)
{
float depth = tex2D(depthfield, depthfieldCoord).r;
depth = 1.0f - (depth*depthClipper.x + depthClipper.y) / depth;
if(!detected && depth>=stepDepth )
{
occlusion += 1.0f - stepDepth;
detected = true;
}
depthfieldCoord += direction.xy;
stepDepth += singleStep;
}
coord.y += singleCoord;
}
coord.x += singleCoord;
}
return occlusion * scale;
} |
|