Hello, I am trying to make a custom 3d renderer with projection and rasterization, but I have a question about computeShaders. I have a compute shader that sees if a pixel is in a triangle, then draw it. My compute shader code:
[numthreads(256,4,1)]
void CSMain (int3 id : SV_DispatchThreadID)
{
float2 coords = float2 (
id.x,
id.y
);
float minValue = 1;
bool shadeIt = false;
for (int i = 0; i < numTriangles; i+=3)
{
float3 v0 = vertices[triangles[i+0]];
float3 v1 = vertices[triangles[i+1]];
float3 v2 = vertices[triangles[i+2]];
float pointInside = isInside (
v0.x, v0.y,
v1.x, v1.y,
v2.x, v2.y,
coords.x, coords.y
);
if (pointInside < 0.1f){
shadeIt = true;
}
}
Result[id.xy] = (shadeIt ? float4(2, 1, 0, 1) : float4(0, 1, 2, 1));
}
My C# code that runs the shader:
vertexBuffer = new ComputeBuffer (mesh.vertices.Length, sizeof(float)*3);
triangleBuffer = new ComputeBuffer (mesh.triangles.Length, sizeof(int));
Vector3[] projectedPoints = new Vector3[mesh.vertices.Length];
int i = 0;
foreach (Vector3 vec in mesh.vertices)
{
projectedPoints[i] = (ProjectPoint (vec) * height) + new Vector3 (width/2, height/2, 0);
projectedPoints[i].z = vec.z;
i ++;
}
vertexBuffer.SetData (projectedPoints);
triangleBuffer.SetData (mesh.triangles);
renderer.SetTexture (0, "Result", output);
renderer.SetBuffer (0, "vertices", vertexBuffer);
renderer.SetBuffer (0, "triangles", triangleBuffer);
renderer.SetInt ("width", width);
renderer.SetInt ("height", height);
renderer.SetInt ("numVertices", mesh.vertices.Length);
renderer.SetInt ("numTriangles", mesh.triangles.Length);
renderer.SetVector ("cameraPosition", camera.position);
renderer.SetVector ("cameraAngles", camera.angles * (3.141f/180));
renderer.Dispatch (0, threadGroups.x, threadGroups.y, threadGroups.z);
vertexBuffer.Dispose();
triangleBuffer.Dispose();
And this works, we can plug in any mesh and it renders it:
![alt text][2]
But as you can see, 1 FPS. Now, if I take out this bit of code (Which is important lol): `shadeIt = true;` Then obviously, the mesh disappears, but the FPS goes up to 60:
![alt text][1]
Why is this happening and how can I fix it?
[1]: /storage/temp/191508-speal.png
[2]: /storage/temp/191507-shmemal.png
↧