|

楼主 |
发表于 2011-5-3 00:04:00
|
显示全部楼层
Re:试了一下用PS3.0渲染Mandelbrot集。为什么循环不能超过15
没想到过了这么久,这个帖子还在。
这个问题我后来解决了,其实故障原因很简单——
1.int类型并不是真正的整数类型,GPU会将其当做浮点数进行运算。而浮点数运算是不准确的,在进行浮点比较时需要引入误差项。
2.当循环小于等于15次时,编译器会自动展开循环,避开了浮点误差问题。
修改后的代码为——
- sampler2D Texture0;
- float4 ps_main( float2 texCoord:TEXCOORD0 ) : COLOR
- {
- float4 crR = float4(0,1,0,1);
- float4 cr = float4(1,1,1,1);
- float2 fPos = float2(0,0);
- float fScale = 4.0f;
- float nmax = 16;
- float ncur = 0;
- float x0 = (texCoord.x-0.5f)*fScale + fPos.x;
- float y0 = (texCoord.y-0.5f)*fScale + fPos.y;
- float fx = x0;
- float fy = y0;
- float ty;
- while ((fx*fx+fy*fy) <= 4.0f) // |z| <= 2
- {
- // z = z^2 + z0
- ty = fy;
- fy = 2*fx*fy + y0;
- fx = fx*fx - ty*ty + x0;
- ncur = ncur + 1;
- if (ncur+0.001 > nmax) // 浮点误差
- {
- cr.rgb = 0;
- return cr;
- }
- }
- cr = lerp(cr, crR, saturate((float)ncur / nmax));
- return cr;
- }
复制代码 |
|