Created
April 1, 2016 04:05
-
-
Save FlexMonkey/1065a5272cb8bd7c3a904cd677f258d2 to your computer and use it in GitHub Desktop.
Bresenham's Line Algorithm for Metal Compute Shader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
in some caese ,crash