Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save FlexMonkey/1065a5272cb8bd7c3a904cd677f258d2 to your computer and use it in GitHub Desktop.
Save FlexMonkey/1065a5272cb8bd7c3a904cd677f258d2 to your computer and use it in GitHub Desktop.
Bresenham's Line Algorithm for Metal Compute Shader
void drawLine(texture2d<float, access::write> targetTexture, uint2 start, uint2 end);
void drawLine(texture2d<float, access::write> targetTexture, uint2 start, uint2 end)
{
int x = int(start.x);
int y = int(start.y);
int dx = abs(x - int(end.x));
int dy = abs(y - int(end.y));
int sx = start.x < end.x ? 1 : -1;
int sy = start.y < end.y ? 1 : -1;
int err = (dx > dy ? dx : -dy) / 2;
while (true)
{
targetTexture.write(float4(1.0), uint2(x, y));
if (x == int(end.x) && y == int(end.y))
{
break;
}
int e2 = err;
if (e2 > -dx)
{
err -= dy;
x += sx;
}
if (e2 < dy)
{
err += dx;
y += sy;
}
}
}
@orangeagain
Copy link

in some caese ,crash

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment