1d999309b36cb3dceadd38217b322f0e96a06b202Nicolas Capens// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman//
3d999309b36cb3dceadd38217b322f0e96a06b202Nicolas Capens// Licensed under the Apache License, Version 2.0 (the "License");
4d999309b36cb3dceadd38217b322f0e96a06b202Nicolas Capens// you may not use this file except in compliance with the License.
5d999309b36cb3dceadd38217b322f0e96a06b202Nicolas Capens// You may obtain a copy of the License at
666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman//
7d999309b36cb3dceadd38217b322f0e96a06b202Nicolas Capens//    http://www.apache.org/licenses/LICENSE-2.0
866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman//
9d999309b36cb3dceadd38217b322f0e96a06b202Nicolas Capens// Unless required by applicable law or agreed to in writing, software
10d999309b36cb3dceadd38217b322f0e96a06b202Nicolas Capens// distributed under the License is distributed on an "AS IS" BASIS,
11d999309b36cb3dceadd38217b322f0e96a06b202Nicolas Capens// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12d999309b36cb3dceadd38217b322f0e96a06b202Nicolas Capens// See the License for the specific language governing permissions and
13d999309b36cb3dceadd38217b322f0e96a06b202Nicolas Capens// limitations under the License.
1466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
1566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#ifndef sw_Clipper_hpp
1666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#define sw_Clipper_hpp
1766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
1866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "Plane.hpp"
1966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "Common/Types.hpp"
2066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
2166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Baumannamespace sw
2266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman{
2366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman	struct Polygon;
2466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman	struct DrawCall;
2566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman	struct DrawData;
2666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
2766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman	class Clipper
2866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman	{
2966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman	public:
3066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman		enum ClipFlags
3166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman		{
32ce065365a9793bb7f1d39ac98ae0e08d0df58d22Nicolas Capens			// Indicates the vertex is outside the respective frustum plane
3366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman			CLIP_RIGHT  = 1 << 0,
3466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman			CLIP_TOP    = 1 << 1,
3566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman			CLIP_FAR    = 1 << 2,
3666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman			CLIP_LEFT   = 1 << 3,
3766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman			CLIP_BOTTOM = 1 << 4,
3866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman			CLIP_NEAR   = 1 << 5,
3966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
40ce065365a9793bb7f1d39ac98ae0e08d0df58d22Nicolas Capens			CLIP_FRUSTUM = 0x003F,
41ce065365a9793bb7f1d39ac98ae0e08d0df58d22Nicolas Capens
42ce065365a9793bb7f1d39ac98ae0e08d0df58d22Nicolas Capens			CLIP_FINITE = 1 << 7,   // All position coordinates are finite
4366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
4466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman			// User-defined clipping planes
45ce065365a9793bb7f1d39ac98ae0e08d0df58d22Nicolas Capens			CLIP_PLANE0 = 1 << 8,
46ce065365a9793bb7f1d39ac98ae0e08d0df58d22Nicolas Capens			CLIP_PLANE1 = 1 << 9,
47ce065365a9793bb7f1d39ac98ae0e08d0df58d22Nicolas Capens			CLIP_PLANE2 = 1 << 10,
48ce065365a9793bb7f1d39ac98ae0e08d0df58d22Nicolas Capens			CLIP_PLANE3 = 1 << 11,
49ce065365a9793bb7f1d39ac98ae0e08d0df58d22Nicolas Capens			CLIP_PLANE4 = 1 << 12,
50ce065365a9793bb7f1d39ac98ae0e08d0df58d22Nicolas Capens			CLIP_PLANE5 = 1 << 13,
51ce065365a9793bb7f1d39ac98ae0e08d0df58d22Nicolas Capens
52ce065365a9793bb7f1d39ac98ae0e08d0df58d22Nicolas Capens			CLIP_USER = 0x3F00
5366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman		};
5466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
5537525a55597ac1bf4e61e8bb3e54edf8a84c297cNicolas Capens		Clipper(bool symmetricNormalizedDepth);
5666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
5766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman		~Clipper();
5866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
59ce065365a9793bb7f1d39ac98ae0e08d0df58d22Nicolas Capens		unsigned int computeClipFlags(const float4 &v);
6066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman		bool clip(Polygon &polygon, int clipFlagsOr, const DrawCall &draw);
6166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
6266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman	private:
6366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman		void clipNear(Polygon &polygon);
6466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman		void clipFar(Polygon &polygon);
65ce065365a9793bb7f1d39ac98ae0e08d0df58d22Nicolas Capens		void clipLeft(Polygon &polygon);
66ce065365a9793bb7f1d39ac98ae0e08d0df58d22Nicolas Capens		void clipRight(Polygon &polygon);
67ce065365a9793bb7f1d39ac98ae0e08d0df58d22Nicolas Capens		void clipTop(Polygon &polygon);
68ce065365a9793bb7f1d39ac98ae0e08d0df58d22Nicolas Capens		void clipBottom(Polygon &polygon);
6966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman		void clipPlane(Polygon &polygon, const Plane &plane);
7066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
7166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman		void clipEdge(float4 &Vo, const float4 &Vi, const float4 &Vj, float di, float dj) const;
7237525a55597ac1bf4e61e8bb3e54edf8a84c297cNicolas Capens
7337525a55597ac1bf4e61e8bb3e54edf8a84c297cNicolas Capens		float n;   // Near clip plane distance
7466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman	};
7566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman}
7666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
7766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#endif   // sw_Clipper_hpp
78