Commit 53bf0a11 by Nicolas Capens

Refactor point and line clip flag calculation.

Move clip flag calculation for the new polygon vertices of points and lines from the renderer to the clipper. Change-Id: I41ac3647d8e9376586a1011d1cf28d83e9c963a2 Reviewed-on: https://swiftshader-review.googlesource.com/5423Tested-by: 's avatarNicolas Capens <capn@google.com> Reviewed-by: 's avatarAlexis Hétu <sugoi@google.com> Reviewed-by: 's avatarNicolas Capens <capn@google.com>
parent 734392e6
...@@ -28,30 +28,39 @@ namespace sw ...@@ -28,30 +28,39 @@ namespace sw
{ {
} }
bool Clipper::clip(Polygon &polygon, int clipFlagsOr, const DrawCall &draw) unsigned int Clipper::computeClipFlags(const float4 &v)
{ {
DrawData &data = *draw.data; return ((v.x > v.w) ? CLIP_RIGHT : 0) |
((v.y > v.w) ? CLIP_TOP : 0) |
polygon.b = 0; ((v.z > v.w) ? CLIP_FAR : 0) |
((v.x < -v.w) ? CLIP_LEFT : 0) |
((v.y < -v.w) ? CLIP_BOTTOM : 0) |
((v.z < 0) ? CLIP_NEAR : 0) |
Clipper::CLIP_FINITE; // FIXME: xyz finite
}
if(clipFlagsOr & 0x0000003F) bool Clipper::clip(Polygon &polygon, int clipFlagsOr, const DrawCall &draw)
{
if(clipFlagsOr & CLIP_FRUSTUM)
{ {
if(clipFlagsOr & CLIP_NEAR) clipNear(polygon); if(clipFlagsOr & CLIP_NEAR) clipNear(polygon);
if(polygon.n >= 3) { if(polygon.n >= 3) {
if(clipFlagsOr & CLIP_FAR) clipFar(polygon); if(clipFlagsOr & CLIP_FAR) clipFar(polygon);
if(polygon.n >= 3) { if(polygon.n >= 3) {
if(clipFlagsOr & CLIP_LEFT) clipLeft(polygon, data); if(clipFlagsOr & CLIP_LEFT) clipLeft(polygon);
if(polygon.n >= 3) { if(polygon.n >= 3) {
if(clipFlagsOr & CLIP_RIGHT) clipRight(polygon, data); if(clipFlagsOr & CLIP_RIGHT) clipRight(polygon);
if(polygon.n >= 3) { if(polygon.n >= 3) {
if(clipFlagsOr & CLIP_TOP) clipTop(polygon, data); if(clipFlagsOr & CLIP_TOP) clipTop(polygon);
if(polygon.n >= 3) { if(polygon.n >= 3) {
if(clipFlagsOr & CLIP_BOTTOM) clipBottom(polygon, data); if(clipFlagsOr & CLIP_BOTTOM) clipBottom(polygon);
}}}}} }}}}}
} }
if(clipFlagsOr & 0x00003F00) if(clipFlagsOr & CLIP_USER)
{ {
DrawData &data = *draw.data;
if(polygon.n >= 3) { if(polygon.n >= 3) {
if(draw.clipFlags & CLIP_PLANE0) clipPlane(polygon, data.clipPlane[0]); if(draw.clipFlags & CLIP_PLANE0) clipPlane(polygon, data.clipPlane[0]);
if(polygon.n >= 3) { if(polygon.n >= 3) {
...@@ -72,8 +81,6 @@ namespace sw ...@@ -72,8 +81,6 @@ namespace sw
void Clipper::clipNear(Polygon &polygon) void Clipper::clipNear(Polygon &polygon)
{ {
if(polygon.n == 0) return;
const float4 **V = polygon.P[polygon.i]; const float4 **V = polygon.P[polygon.i];
const float4 **T = polygon.P[polygon.i + 1]; const float4 **T = polygon.P[polygon.i + 1];
...@@ -114,8 +121,6 @@ namespace sw ...@@ -114,8 +121,6 @@ namespace sw
void Clipper::clipFar(Polygon &polygon) void Clipper::clipFar(Polygon &polygon)
{ {
if(polygon.n == 0) return;
const float4 **V = polygon.P[polygon.i]; const float4 **V = polygon.P[polygon.i];
const float4 **T = polygon.P[polygon.i + 1]; const float4 **T = polygon.P[polygon.i + 1];
...@@ -154,10 +159,8 @@ namespace sw ...@@ -154,10 +159,8 @@ namespace sw
polygon.i += 1; polygon.i += 1;
} }
void Clipper::clipLeft(Polygon &polygon, const DrawData &data) void Clipper::clipLeft(Polygon &polygon)
{ {
if(polygon.n == 0) return;
const float4 **V = polygon.P[polygon.i]; const float4 **V = polygon.P[polygon.i];
const float4 **T = polygon.P[polygon.i + 1]; const float4 **T = polygon.P[polygon.i + 1];
...@@ -196,10 +199,8 @@ namespace sw ...@@ -196,10 +199,8 @@ namespace sw
polygon.i += 1; polygon.i += 1;
} }
void Clipper::clipRight(Polygon &polygon, const DrawData &data) void Clipper::clipRight(Polygon &polygon)
{ {
if(polygon.n == 0) return;
const float4 **V = polygon.P[polygon.i]; const float4 **V = polygon.P[polygon.i];
const float4 **T = polygon.P[polygon.i + 1]; const float4 **T = polygon.P[polygon.i + 1];
...@@ -238,10 +239,8 @@ namespace sw ...@@ -238,10 +239,8 @@ namespace sw
polygon.i += 1; polygon.i += 1;
} }
void Clipper::clipTop(Polygon &polygon, const DrawData &data) void Clipper::clipTop(Polygon &polygon)
{ {
if(polygon.n == 0) return;
const float4 **V = polygon.P[polygon.i]; const float4 **V = polygon.P[polygon.i];
const float4 **T = polygon.P[polygon.i + 1]; const float4 **T = polygon.P[polygon.i + 1];
...@@ -280,10 +279,8 @@ namespace sw ...@@ -280,10 +279,8 @@ namespace sw
polygon.i += 1; polygon.i += 1;
} }
void Clipper::clipBottom(Polygon &polygon, const DrawData &data) void Clipper::clipBottom(Polygon &polygon)
{ {
if(polygon.n == 0) return;
const float4 **V = polygon.P[polygon.i]; const float4 **V = polygon.P[polygon.i];
const float4 **T = polygon.P[polygon.i + 1]; const float4 **T = polygon.P[polygon.i + 1];
...@@ -324,8 +321,6 @@ namespace sw ...@@ -324,8 +321,6 @@ namespace sw
void Clipper::clipPlane(Polygon &polygon, const Plane &p) void Clipper::clipPlane(Polygon &polygon, const Plane &p)
{ {
if(polygon.n == 0) return;
const float4 **V = polygon.P[polygon.i]; const float4 **V = polygon.P[polygon.i];
const float4 **T = polygon.P[polygon.i + 1]; const float4 **T = polygon.P[polygon.i + 1];
......
...@@ -29,6 +29,7 @@ namespace sw ...@@ -29,6 +29,7 @@ namespace sw
public: public:
enum ClipFlags enum ClipFlags
{ {
// Indicates the vertex is outside the respective frustum plane
CLIP_RIGHT = 1 << 0, CLIP_RIGHT = 1 << 0,
CLIP_TOP = 1 << 1, CLIP_TOP = 1 << 1,
CLIP_FAR = 1 << 2, CLIP_FAR = 1 << 2,
...@@ -36,30 +37,35 @@ namespace sw ...@@ -36,30 +37,35 @@ namespace sw
CLIP_BOTTOM = 1 << 4, CLIP_BOTTOM = 1 << 4,
CLIP_NEAR = 1 << 5, CLIP_NEAR = 1 << 5,
CLIP_FINITE = 1 << 7, CLIP_FRUSTUM = 0x003F,
CLIP_FINITE = 1 << 7, // All position coordinates are finite
// User-defined clipping planes // User-defined clipping planes
CLIP_PLANE0 = 1 << 8, CLIP_PLANE0 = 1 << 8,
CLIP_PLANE1 = 1 << 9, CLIP_PLANE1 = 1 << 9,
CLIP_PLANE2 = 1 << 10, CLIP_PLANE2 = 1 << 10,
CLIP_PLANE3 = 1 << 11, CLIP_PLANE3 = 1 << 11,
CLIP_PLANE4 = 1 << 12, CLIP_PLANE4 = 1 << 12,
CLIP_PLANE5 = 1 << 13 CLIP_PLANE5 = 1 << 13,
CLIP_USER = 0x3F00
}; };
Clipper(); Clipper();
~Clipper(); ~Clipper();
unsigned int computeClipFlags(const float4 &v);
bool clip(Polygon &polygon, int clipFlagsOr, const DrawCall &draw); bool clip(Polygon &polygon, int clipFlagsOr, const DrawCall &draw);
private: private:
void clipNear(Polygon &polygon); void clipNear(Polygon &polygon);
void clipFar(Polygon &polygon); void clipFar(Polygon &polygon);
void clipLeft(Polygon &polygon, const DrawData &data); void clipLeft(Polygon &polygon);
void clipRight(Polygon &polygon, const DrawData &data); void clipRight(Polygon &polygon);
void clipTop(Polygon &polygon, const DrawData &data); void clipTop(Polygon &polygon);
void clipBottom(Polygon &polygon, const DrawData &data); void clipBottom(Polygon &polygon);
void clipPlane(Polygon &polygon, const Plane &plane); void clipPlane(Polygon &polygon, const Plane &plane);
void clipEdge(float4 &Vo, const float4 &Vi, const float4 &Vj, float di, float dj) const; void clipEdge(float4 &Vo, const float4 &Vi, const float4 &Vj, float di, float dj) const;
......
...@@ -29,6 +29,7 @@ namespace sw ...@@ -29,6 +29,7 @@ namespace sw
n = 3; n = 3;
i = 0; i = 0;
b = 0;
} }
Polygon(const float4 *P, int n) Polygon(const float4 *P, int n)
...@@ -40,14 +41,15 @@ namespace sw ...@@ -40,14 +41,15 @@ namespace sw
this->n = n; this->n = n;
this->i = 0; this->i = 0;
this->b = 0;
} }
float4 B[16]; // Buffer for clipped vertices float4 B[16]; // Buffer for clipped vertices
const float4 *P[16][16]; // Pointers to clipped polygon's vertices const float4 *P[16][16]; // Pointers to clipped polygon's vertices
int i; int n; // Number of vertices
int b; int i; // Level of P to use
int n; int b; // Next available new vertex
}; };
} }
......
...@@ -1726,19 +1726,19 @@ namespace sw ...@@ -1726,19 +1726,19 @@ namespace sw
P[0].x += -dy0w + -dx0w; P[0].x += -dy0w + -dx0w;
P[0].y += -dx0h + +dy0h; P[0].y += -dx0h + +dy0h;
C[0] = computeClipFlags(P[0], data); C[0] = clipper->computeClipFlags(P[0]);
P[1].x += -dy1w + +dx1w; P[1].x += -dy1w + +dx1w;
P[1].y += -dx1h + +dy1h; P[1].y += -dx1h + +dy1h;
C[1] = computeClipFlags(P[1], data); C[1] = clipper->computeClipFlags(P[1]);
P[2].x += +dy1w + +dx1w; P[2].x += +dy1w + +dx1w;
P[2].y += +dx1h + -dy1h; P[2].y += +dx1h + -dy1h;
C[2] = computeClipFlags(P[2], data); C[2] = clipper->computeClipFlags(P[2]);
P[3].x += +dy0w + -dx0w; P[3].x += +dy0w + -dx0w;
P[3].y += +dx0h + +dy0h; P[3].y += +dx0h + +dy0h;
C[3] = computeClipFlags(P[3], data); C[3] = clipper->computeClipFlags(P[3]);
if((C[0] & C[1] & C[2] & C[3]) == Clipper::CLIP_FINITE) if((C[0] & C[1] & C[2] & C[3]) == Clipper::CLIP_FINITE)
{ {
...@@ -1778,28 +1778,28 @@ namespace sw ...@@ -1778,28 +1778,28 @@ namespace sw
float dy1 = lineWidth * 0.5f * P1.w / H; float dy1 = lineWidth * 0.5f * P1.w / H;
P[0].x += -dx0; P[0].x += -dx0;
C[0] = computeClipFlags(P[0], data); C[0] = clipper->computeClipFlags(P[0]);
P[1].y += +dy0; P[1].y += +dy0;
C[1] = computeClipFlags(P[1], data); C[1] = clipper->computeClipFlags(P[1]);
P[2].x += +dx0; P[2].x += +dx0;
C[2] = computeClipFlags(P[2], data); C[2] = clipper->computeClipFlags(P[2]);
P[3].y += -dy0; P[3].y += -dy0;
C[3] = computeClipFlags(P[3], data); C[3] = clipper->computeClipFlags(P[3]);
P[4].x += -dx1; P[4].x += -dx1;
C[4] = computeClipFlags(P[4], data); C[4] = clipper->computeClipFlags(P[4]);
P[5].y += +dy1; P[5].y += +dy1;
C[5] = computeClipFlags(P[5], data); C[5] = clipper->computeClipFlags(P[5]);
P[6].x += +dx1; P[6].x += +dx1;
C[6] = computeClipFlags(P[6], data); C[6] = clipper->computeClipFlags(P[6]);
P[7].y += -dy1; P[7].y += -dy1;
C[7] = computeClipFlags(P[7], data); C[7] = clipper->computeClipFlags(P[7]);
if((C[0] & C[1] & C[2] & C[3] & C[4] & C[5] & C[6] & C[7]) == Clipper::CLIP_FINITE) if((C[0] & C[1] & C[2] & C[3] & C[4] & C[5] & C[6] & C[7]) == Clipper::CLIP_FINITE)
{ {
...@@ -1905,19 +1905,19 @@ namespace sw ...@@ -1905,19 +1905,19 @@ namespace sw
P[0].x -= X; P[0].x -= X;
P[0].y += Y; P[0].y += Y;
C[0] = computeClipFlags(P[0], data); C[0] = clipper->computeClipFlags(P[0]);
P[1].x += X; P[1].x += X;
P[1].y += Y; P[1].y += Y;
C[1] = computeClipFlags(P[1], data); C[1] = clipper->computeClipFlags(P[1]);
P[2].x += X; P[2].x += X;
P[2].y -= Y; P[2].y -= Y;
C[2] = computeClipFlags(P[2], data); C[2] = clipper->computeClipFlags(P[2]);
P[3].x -= X; P[3].x -= X;
P[3].y -= Y; P[3].y -= Y;
C[3] = computeClipFlags(P[3], data); C[3] = clipper->computeClipFlags(P[3]);
triangle.v1 = triangle.v0; triangle.v1 = triangle.v0;
triangle.v2 = triangle.v0; triangle.v2 = triangle.v0;
...@@ -1945,17 +1945,6 @@ namespace sw ...@@ -1945,17 +1945,6 @@ namespace sw
return false; return false;
} }
unsigned int Renderer::computeClipFlags(const float4 &v, const DrawData &data)
{
return ((v.x > v.w) << 0) |
((v.y > v.w) << 1) |
((v.z > v.w) << 2) |
((v.x < -v.w) << 3) |
((v.y < -v.w) << 4) |
((v.z < 0) << 5) |
Clipper::CLIP_FINITE; // FIXME: xyz finite
}
void Renderer::initializeThreads() void Renderer::initializeThreads()
{ {
unitCount = ceilPow2(threadCount); unitCount = ceilPow2(threadCount);
......
...@@ -345,7 +345,7 @@ namespace sw ...@@ -345,7 +345,7 @@ namespace sw
virtual void setSwizzleG(SamplerType type, int sampler, SwizzleType swizzleG); virtual void setSwizzleG(SamplerType type, int sampler, SwizzleType swizzleG);
virtual void setSwizzleB(SamplerType type, int sampler, SwizzleType swizzleB); virtual void setSwizzleB(SamplerType type, int sampler, SwizzleType swizzleB);
virtual void setSwizzleA(SamplerType type, int sampler, SwizzleType swizzleA); virtual void setSwizzleA(SamplerType type, int sampler, SwizzleType swizzleA);
virtual void setPointSpriteEnable(bool pointSpriteEnable); virtual void setPointSpriteEnable(bool pointSpriteEnable);
virtual void setPointScaleEnable(bool pointScaleEnable); virtual void setPointScaleEnable(bool pointScaleEnable);
virtual void setLineWidth(float width); virtual void setLineWidth(float width);
...@@ -416,7 +416,6 @@ namespace sw ...@@ -416,7 +416,6 @@ namespace sw
bool isReadWriteTexture(int sampler); bool isReadWriteTexture(int sampler);
void updateClipper(); void updateClipper();
void updateConfiguration(bool initialUpdate = false); void updateConfiguration(bool initialUpdate = false);
static unsigned int computeClipFlags(const float4 &v, const DrawData &data);
void initializeThreads(); void initializeThreads();
void terminateThreads(); void terminateThreads();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment