Loop optimization

  • Thread starter Thread starter wqaxs36
  • Start date Start date
W

wqaxs36

Guest
Why this loop is so slow ?

It gives 30 FPS instead of 120 fps for my C version.

void SceneBuilder()
{
Stopwatch CPUtick = new Stopwatch();
CPUtick.Start();

while (true)
{
for (UInt64 Voxel = OBJ_HEADER; Voxel <= (UInt64)VoxCube[OBJ_SIZE] * 4; Voxel += BPP)
{
double a, b;

VoxCube[Voxel + _z]++;

if (VoxCube[Voxel + _z] != 0)
{
a = (VoxCube[Voxel + _x] * 500) / -VoxCube[Voxel + _z];
b = (VoxCube[Voxel + _y] * 500) / -VoxCube[Voxel + _z];
}
else
{
a = (VoxCube[Voxel + _x] * 500);
b = (VoxCube[Voxel + _y] * 500);
}

if ((a <= ((double)Resolution.X / 2) - 0 && a >= -((double)Resolution.X / 2)) &&
(b <= ((double)Resolution.Y / 2) - 0 && b >= -((double)Resolution.Y / 2)) && VoxCube[Voxel + _z] >= 0)
{
Int64 Repere = ((Int64)Resolution.X / 2 + (Int64)Resolution.X * (Int64)Resolution.Y / 2) * 4;
Int64 OffsetPixel = Repere - (((Int64)Resolution.X * (Int64)b) + (Int64)a) * 4;

if (OffsetPixel >= 0 && OffsetPixel < ((Int64)Resolution.X * (Int64)Resolution.Y * (Int64)BPP))
{
UInt64 byteA = ((UInt64)VoxCube[Voxel + _color] << 24) >> 24;
UInt64 byteR = ((UInt64)VoxCube[Voxel + _color] << 16) >> 24;
UInt64 byteG = ((UInt64)VoxCube[Voxel + _color] << 08) >> 24;
UInt64 byteB = ((UInt64)VoxCube[Voxel + _color] << 00) >> 24;

Viewport[150 + OffsetPixel] = (byte)byteR;
Viewport[151 + OffsetPixel] = (byte)byteG;
Viewport[152 + OffsetPixel] = (byte)byteB;
Viewport[153 + OffsetPixel] = (byte)byteA;
}
}
}

//UploadScreen();

// Show FPS
FPS[0]++;

if (CPUtick.ElapsedMilliseconds > FPS[1] + 1000)
{
textBox8.Text = FPS[0].ToString();

FPS[0] = 0;
FPS[1] = CPUtick.ElapsedMilliseconds;
}
}
}

Continue reading...
 
Back
Top