1// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//    http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef sw_Clipper_hpp
16#define sw_Clipper_hpp
17
18#include "Plane.hpp"
19#include "Common/Types.hpp"
20
21namespace sw
22{
23	struct Polygon;
24	struct DrawCall;
25	struct DrawData;
26
27	class Clipper
28	{
29	public:
30		enum ClipFlags
31		{
32			// Indicates the vertex is outside the respective frustum plane
33			CLIP_RIGHT  = 1 << 0,
34			CLIP_TOP    = 1 << 1,
35			CLIP_FAR    = 1 << 2,
36			CLIP_LEFT   = 1 << 3,
37			CLIP_BOTTOM = 1 << 4,
38			CLIP_NEAR   = 1 << 5,
39
40			CLIP_FRUSTUM = 0x003F,
41
42			CLIP_FINITE = 1 << 7,   // All position coordinates are finite
43
44			// User-defined clipping planes
45			CLIP_PLANE0 = 1 << 8,
46			CLIP_PLANE1 = 1 << 9,
47			CLIP_PLANE2 = 1 << 10,
48			CLIP_PLANE3 = 1 << 11,
49			CLIP_PLANE4 = 1 << 12,
50			CLIP_PLANE5 = 1 << 13,
51
52			CLIP_USER = 0x3F00
53		};
54
55		Clipper(bool symmetricNormalizedDepth);
56
57		~Clipper();
58
59		unsigned int computeClipFlags(const float4 &v);
60		bool clip(Polygon &polygon, int clipFlagsOr, const DrawCall &draw);
61
62	private:
63		void clipNear(Polygon &polygon);
64		void clipFar(Polygon &polygon);
65		void clipLeft(Polygon &polygon);
66		void clipRight(Polygon &polygon);
67		void clipTop(Polygon &polygon);
68		void clipBottom(Polygon &polygon);
69		void clipPlane(Polygon &polygon, const Plane &plane);
70
71		void clipEdge(float4 &Vo, const float4 &Vi, const float4 &Vj, float di, float dj) const;
72
73		float n;   // Near clip plane distance
74	};
75}
76
77#endif   // sw_Clipper_hpp
78