Skip to content

Commit

Permalink
Started work on clipping
Browse files Browse the repository at this point in the history
  • Loading branch information
kecho committed Feb 8, 2023
1 parent 6a2a687 commit 5492e73
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
33 changes: 32 additions & 1 deletion grr/geometry.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,48 @@ namespace geometry
float4 h1;
float4 h2;

float4 og0;
float4 og1;
float4 og2;

// Homogeneous screen coordinates after division by W
float3 p0;
float3 p1;
float3 p2;

uint clipZMask;

void init(TriangleV tri, float4x4 view, float4x4 proj)
{
h0 = mul(mul(float4(tri.a.p.xyz, 1.0), view), proj);
h1 = mul(mul(float4(tri.b.p.xyz, 1.0), view), proj);
h2 = mul(mul(float4(tri.c.p.xyz, 1.0), view), proj);

og0 = h0;
og1 = h1;
og2 = h2;
clipZMask = 0;
clipZMask |= clipVert(h2, h0) << 0;
clipZMask |= clipVert(h2, h1) << 1;
clipZMask |= clipVert(h1, h2) << 2;
calculatePoints();
}

uint clipVert(in float4 dominantVert, inout float4 h)
{
if (h.z < h.w)
return h.w < MIN_DEPTH;

float dw = MIN_DEPTH - h.w;
float2 a = (h.xy - dominantVert.xy)/(h.w - dominantVert.w);
float ogDepth = h.w;
h.w = MIN_DEPTH;
h.xy += a * dw;
return 1;
}

void calculatePoints()
{
p0 = h0.xyz / h0.w;
p1 = h1.xyz / h1.w;
p2 = h2.xyz / h2.w;
Expand All @@ -156,7 +187,7 @@ namespace geometry
float frontFace = min(wa, min(wb, wc));

TriInterpResult result;
result.bari = computeBaryCoordPerspective(float3(p0.xy,h0.w), float3(p1.xy,h1.w), float3(p2.xy,h2.w), hCoords);
result.bari = computeBaryCoordPerspective(float3(og0.xy/og0.w,og0.w), float3(og1.xy/og1.w,og1.w), float3(og2.xy/og2.w,og2.w), hCoords);
result.isBackface = backFace > 0.0;
result.isFrontface = frontFace > 0.0;
result.visible = result.isBackface || result.isFrontface;
Expand Down
6 changes: 3 additions & 3 deletions grr/gpugeo.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ def __init__(self):
def load_simple_triangle(self):
tri_data = array.array('f', [
#v.x, v.y, v.z, # uv.x, uv.y, n.x, n.y, n.z
1.0, -0.5, -2.0, # 0.0, 0.0, 0.0, 0.0, 1.0,
-1.0, -0.5, -2.0, # 1.0, 0.0, 0.0, 0.0, 1.0,
0.0, 1.0, 2.0, # 0.5, 1.0, 0.0, 0.0, 1.0
-1.0, 1.0, 2.0, # 0.0, 0.0, 0.0, 0.0, 1.0,
1.0, 1.0, 2.0, # 1.0, 0.0, 0.0, 0.0, 1.0,
0.0, -0.5, -2.0, # 0.5, 1.0, 0.0, 0.0, 1.0

#v.x, v.y, v.z, # uv.x, uv.y, n.x, n.y, n.z
1.0, -0.5, -4.0, # 0.0, 0.0, 0.0, 0.0, 1.0,
Expand Down
13 changes: 4 additions & 9 deletions grr/raster_cs.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ RWBuffer<uint> g_outTotalRecords : register(u0);
RWBuffer<uint> g_binCounters : register(u1);
RWStructuredBuffer<raster::BinIntersectionRecord> g_binOutputRecords : register(u2);


[numthreads(64, 1, 1)]
void csMainBinTriangles(int3 dti : SV_DispatchThreadID)
{
Expand All @@ -229,11 +228,7 @@ void csMainBinTriangles(int3 dti : SV_DispatchThreadID)
geometry::TriangleH th;
th.init(tv, g_view, g_proj);

//float wEpsilon = 0.001;
//if (th.h0.w < wEpsilon || th.h1.w < wEpsilon || th.h2.w < wEpsilon)
// return;

if (th.h0.z >= th.h0.w || th.h2.z >= th.h2.w || th.h1.z >= th.h1.w )
if ((th.clipZMask & ((1 << 3) - 1)) != 0)
return;

geometry::AABB aabb = th.aabb();
Expand All @@ -258,10 +253,10 @@ void csMainBinTriangles(int3 dti : SV_DispatchThreadID)
int2 tileE = tileB + 1;
geometry::AABB tile;

tile.begin = float3(geometry::uvToH(geometry::pixelToUV(tileB * COARSE_TILE_SIZE, g_outputSize.xy)), 0);
tile.end = float3(geometry::uvToH(geometry::pixelToUV(tileE * COARSE_TILE_SIZE, g_outputSize.xy)), 1);
tile.begin = float3(geometry::uvToH(geometry::pixelToUV(tileB * COARSE_TILE_SIZE, g_outputSize.xy)), MAX_DEPTH);
tile.end = float3(geometry::uvToH(geometry::pixelToUV(tileE * COARSE_TILE_SIZE, g_outputSize.xy)), MIN_DEPTH);

//if (any(aabb.begin.xy > tile.end.xy) || any(aabb.end.xy < tile.begin.xy))
if (any(aabb.begin.xy > tile.end.xy) || any(aabb.end.xy < tile.begin.xy))
if (!aabb.intersects(tile))
continue;

Expand Down

0 comments on commit 5492e73

Please sign in to comment.