I have written a compute shader that is intended to use the UV coordinates of 3 vertices making up a primitive, as well as the texel coordinate (dispatchThreadID.xy) to achieve rasterization.
I am attempting to return a texture that for each texel, references the index of three vertices making up a primitive, in which the texel lies in.
It is my understanding that dividing the texel coordinates by the texture width/height (or vice versa) should put them in the same space.
Here is the shader:
StructuredBuffer<Input> inputBuffer : register(t0);
StructuredBuffer<int> indexBuffer : register(t1);
RWTexture2D<float4> outTex : register(u0);
[numthreads(16, 16, 1)]
void main( uint3 dispatchThreadId : SV_DispatchThreadID)
{
// identify texel via various IDs
float2 p = dispatchThreadId.xy;
uint noStructs;
uint stride;
indexBuffer.GetDimensions(noStructs, stride);
for (int i = 0; i < (noStructs / 3) - 3; i += 3)
{
int index1 = indexBuffer[i];
int index2 = indexBuffer[i + 1];
int index3 = indexBuffer[i + 2];
float2 v0 = float2(inputBuffer[index1].Tex.x, inputBuffer[index1].Tex.y);
float2 v1 = float2(inputBuffer[index2].Tex.x, inputBuffer[index2].Tex.y);
float2 v2 = float2(inputBuffer[index3].Tex.x, inputBuffer[index3].Tex.y);
float inverse = 1 / 512.0f;
float2 p2 = p;
p.x *= inverse;
p.y *= inverse;
// Determine barycentric coordinates
float w0 = orient2d(v1, v2, p);
float w1 = orient2d(v2, v0, p);
float w2 = orient2d(v0, v1, p);
// If p is on or inside all edges
if (w0 >= -0.01f && w1 >= -0.01f && w2 >= -0.01f)
{
// save reference to that primitive
outTex[p2] = float4(i, i+1, i+2, 1);
}
}
}
Here is the Orient2D function that calculates the barycentric coordinates:
float orient2d(const float2 a, const float2 b, const float2 c)
{
return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
}
And here is a sample of the results from the shader:
Texel Map ( 159, 160, 161, 1)( 159, 160, 161, 1)( 159, 160, 161, 1)( 159, 160, 161, 1)( 159, 160, 161, 1)( 159, 160, 161, 1)( 159, 160, 161, 1)( 159, 160, 161, 1)( 159, 160, 161, 1)( 159, 160, 161, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 177, 178, 179, 1)( 177, 178, 179, 1)( 177, 178, 179, 1)( 177, 178, 179, 1)( 177, 178, 179, 1)( 177, 178, 179, 1)( 177, 178, 179, 1)( 177, 178, 179, 1)( 177, 178, 179, 1)( 177, 178, 179, 1)( 177, 178, 179, 1)( 177, 178, 179, 1)( 177, 178, 179, 1)( 177, 178, 179, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 141, 142, 143, 1)( 141, 142, 143, 1)( 141, 142, 143, 1)( 141, 142, 143, 1)( 141, 142, 143, 1)( 141, 142, 143, 1)( 141, 142, 143, 1)( 141, 142, 143, 1)( 141, 142, 143, 1)( 141, 142, 143, 1)( 141, 142, 143, 1)( 141, 142, 143, 1)( 141, 142, 143, 1)( 141, 142, 143, 1)( 141, 142, 143, 1)( 141, 142, 143, 1)( 141, 142, 143, 1)( 129, 130, 131, 1)( 129, 130, 131, 1)( 129, 130, 131, 1)( 129, 130, 131, 1)( 129, 130, 131, 1)( 129, 130, 131, 1)( 213, 214, 215, 1)( 213, 214, 215, 1)( 213, 214, 215, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 351, 352, 353, 1)( 363, 364, 365, 1)( 363, 364, 365, 1)( 363, 364, 365, 1)( 363, 364, 365, 1)( 363, 364, 365, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 387, 388, 389, 1)( 363, 364, 365, 1)( 363, 364, 365, 1)( 363, 364, 365, 1)( 363, 364, 365, 1)( 363, 364, 365, 1)( 363, 364, 365, 1)( 363, 364, 365, 1)( 363, 364, 365, 1)( 363, 364, 365, 1)( 363, 364, 365, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 585, 586, 587, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 348, 349, 350, 1)( 333, 334, 335, 1)( 333, 334, 335, 1)( 333, 334, 335, 1)( 333, 334, 335, 1)( 333, 334, 335, 1)( 333, 334, 335, 1)( 333, 334, 335, 1)( 333, 334, 335, 1)( 333, 334, 335, 1)( 333, 334, 335, 1)( 333, 334, 335, 1)( 333, 334, 335, 1)( 333, 334, 335, 1)( 333, 334, 335, 1)( 333, 334, 335, 1)( 333, 334, 335, 1)( 333, 334, 335, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 249, 250, 251, 1)( 249, 250, 251, 1)( 249, 250, 251, 1)( 249, 250, 251, 1)( 249, 250, 251, 1)( 249, 250, 251, 1)( 249, 250, 251, 1)( 252, 253, 254, 1)( 252, 253, 254, 1)( 252, 253, 254, 1)( 252, 253, 254, 1)( 252, 253, 254, 1)( 252, 253, 254, 1)( 252, 253, 254, 1)( 252, 253, 254, 1)( 252, 253, 254, 1)( 252, 253, 254, 1)( 252, 253, 254, 1)( 252, 253, 254, 1)( 252, 253, 254, 1)( 252, 253, 254, 1)( 252, 253, 254, 1)( 252, 253, 254, 1)( 252, 253, 254, 1)( 252, 253, 254, 1)( 252, 253, 254, 1)( 252, 253, 254, 1)( 252, 253, 254, 1)( 252, 253, 254, 1)( 252, 253, 254, 1)( 252, 253, 254, 1)( 252, 253, 254, 1)( 252, 253, 254, 1)( 252, 253, 254, 1)( 252, 253, 254, 1)( 252, 253, 254, 1)( 252, 253, 254, 1)( 252, 253, 254, 1)( 252, 253, 254, 1)( 252, 253, 254, 1)( 252, 253, 254, 1)( 273, 274, 275, 1)( 273, 274, 275, 1)( 273, 274, 275, 1)( 273, 274, 275, 1)( 273, 274, 275, 1)( 273, 274, 275, 1)( 273, 274, 275, 1)( 273, 274, 275, 1)( 273, 274, 275, 1)( 273, 274, 275, 1)( 159, 160, 161, 1)( 159, 160, 161, 1)( 159, 160, 161, 1)( 159, 160, 161, 1)( 159, 160, 161, 1)( 159, 160, 161, 1)( 159, 160, 161, 1)( 159, 160, 161, 1)( 159, 160, 161, 1)( 159, 160, 161, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 177, 178, 179, 1)( 177, 178, 179, 1)( 177, 178, 179, 1)( 177, 178, 179, 1)( 177, 178, 179, 1)( 177, 178, 179, 1)( 177, 178, 179, 1)( 177, 178, 179, 1)( 177, 178, 179, 1)( 177, 178, 179, 1)( 177, 178, 179, 1)( 177, 178, 179, 1)( 177, 178, 179, 1)( 177, 178, 179, 1)( 177, 178, 179, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 171, 172, 173, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 384, 385, 386, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 156, 157, 158, 1)( 141, 142, 143, 1)( 141, 142, 143, 1)( 141, 142, 143, 1)( 141, 142, 143, 1)( 141, 142, 143, 1)( 141, 142, 143, 1)( 141, 142, 143, 1)( 141, 142, 143, 1)( 141, 142, 143, 1)( 141, 142, 143, 1)( 141, 142, 143, 1)( 141, 142, 143, 1)( 141, 142, 143, 1)( 141, 142, 143, 1)( 141, 142, 143, 1)( 141, 142, 143, 1)( 141, 142, 143, 1)( 141, 142, 143, 1)( 129, 130, 131, 1)( 129, 130, 131, 1)( 129, 130, 131, 1)( 129, 130, 131, 1)( 129, 130, 131, 1)( 213, 214, 215, 1)( 213, 214, 215, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)( 321, 322, 323, 1)
Any help would be appreciated.
EDIT: I have relaxed the edge criteria discerning if the texel lies within all three edges to -0.01 instead of 0. This results in each texel being mapped to a primitive, but I don't think all primitives get mapped to a texel, which I think would be wrong. I think that is due to the loop continuing after mapping a texel, but if I include a break function within the if statement, the higher primitive indices are never reached.