我的程序刚开始运行时,帧数显示为40fps,仅过10秒左右,就变成5fps,请问可能的原因是什么?我交待一下我的程序运行过程:
首先我会创建一个数据集,然后用opengl将数据集渲染到两个纹理中,然后映射到CUDA进行使用。这个过程是动态变化的,也就是说,渲染到纹理的过程是持续不断的。在CUDA的核函数里,读取纹理并进行计算,最后生成另一个纹理,返回到opengl进行显示。
我基本可以肯定是kernel函数的问题,因为如果不运行核函数,而只进行其它操作的话,帧数不受影响,只有运行了核函数以后,帧数发生了明显减小
但如果说是kernel函数耗费时间的话,为什么一开始会有比较高的帧数呢?影响帧数的因素都有哪些呢?
请大家赐教
附,我的核函数源码如下:global void
d_render(uint *d_output, uint imageW, uint imageH,
float density, float brightness,
float transferOffset, float transferScale)
{
const int maxSteps = 500;
const float tstep = 0.01f;
const float opacityThreshold = 0.95f;
uint x = blockIdx.xblockDim.x + threadIdx.x;
uint y = blockIdx.yblockDim.y + threadIdx.y;
if ((x >= imageW) || (y >= imageH)) return;
float u = (x / (float) imageW);
float v = (y / (float) imageH);
// calculate eye ray in world space
Ray eyeRay;
eyeRay.o = make_float3(mul(c_invViewMatrix, make_float4(0.0f, 0.0f, 0.0f, 1.0f)));
eyeRay.d = normalize(make_float3(u-0.5f, v-0.5f, -1.0f));
eyeRay.d = mul(c_invViewMatrix, eyeRay.d);
float4 sum = make_float4(0.0f);
float3 step = eyeRay.d*tstep;
float4 startPosition = tex2D(cudafronttex,u,v);//在opengl中生成的纹理,是持续生成的,即其内容是不断变化的,与这个有无关系?
float4 endPostion = tex2D(cudabacktex,u,v);//在opengl中生成的纹理
float3 pos;
pos.x = startPosition.x;
pos.y = startPosition.y;
pos.z = startPosition.z;
float depth; //下面这个求根,本来我有所怀疑,但用一个常量赋值实验后,帧数仍然发生减小
depth = sqrt((endPostion.x-pos.x)(endPostion.x-pos.x) + (endPostion.y-pos.y)(endPostion.y-pos.y) + (endPostion.z-pos.z)*(endPostion.z-pos.z));
float t = 0;
for(int i=0; i<maxSteps; i++) {
// read from 3D texture
float sample = tex3D(tex, pos.x, pos.y, pos.z);
// lookup in transfer function texture
float4 col = tex1D(transferTex, (sample-transferOffset)*transferScale);
col.w *= density;
// pre-multiply alpha
col.x *= col.w;
col.y *= col.w;
col.z = col.w;
// “over” operator for front-to-back blending
sum = sum + col(1.0f - sum.w);
// exit early if opaque
if (sum.w > opacityThreshold) break;
t += tstep;
if (t > depth) break;
pos += step;
}
sum *= brightness;
// write output color生成新纹理并返回opengl
d_output[y*imageW + x] = rgbaFloatToInt(sum);
}