Hi, i', trying to make super simple GPU RayTracer using Compute Shader. It works well, however there is something strange to me. I am not familiar with shaders to much. So i have a loop body with triangles buffer and a ray which tests every triangle. This is part of the shader, the problem is explained inside:
[loop][allow_uav_condition]
for (uint i = 0; i < tris.Length; i++)
{
// Getting triangle vertices and doing some math
float3 pt0 = tris[i].v0;
float3 pt1 = tris[i].v1;
float3 pt2 = tris[i].v2;
float3 edge0 = rayO - pt0;
float3 edge1 = rayO - pt1;
float3 edge2 = rayO - pt2;
float3 cross1 = cross(edge0, edge1);
float3 cross2 = cross(edge1, edge2);
float3 cross3 = cross(edge2, edge0);
float dot1 = dot(rayD, cross1);
float dot2 = dot(rayD, cross2);
float dot3 = dot(rayD, cross3);
// Some more math
float3 n = tris[i].n;
float3 w0 = rayO - pt0;
float _a = -dot(n, w0);
float b = dot(n, rayD);
float r = _a / b;
float3 I = rayO + rayD * r;
hDis = distance(rayO, I);
// Calculating if there is intersection or not
bIntersect = dot1 < 0 && dot2 < 0 && dot3 < 0 && _a < 0 && hDis < fRadius && pixel.valid;
if(bIntersect == 1)
{
// If there is intersection we would like to break;
// However breaking or not performance is slow if using "bIntersect"
// in the calculations next after the loop body
break;
}
}
// THE PROBLEM: - Setting pixel color based on "bIntersect" value -> 1.0 or 0.0. When multiplying with "bIntersect", pixelColor either changes or not its value. However including "bIntersection" in the line below, makes the calculation to happen for 8 secconds. And when replaced with some float value, it is calculated for 0.4 secconds which is about 16 times faster. So no matter what variable which was used within the Loop body i use in the calculation below, the performance is terrible. Is that normal and how i suppose to set some variable in the loop body and use it outside the loop. I tested a lot of options like making calculations inside the loop body with no success.
pixel.pixelColor -= _step * bIntersect; // Using "bIntersect" here breaks the performance
rendTex[id.xy] = float4(pixel.pixelColor, pixel.pixelColor, pixel.pixelColor, pixel.valid );
pixels[pixelID].pixelColor = pixel.pixelColor;
↧