• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /****************************************************************************
2  * Copyright (C) 2014-2016 Intel Corporation.   All Rights Reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  * @file conservativerast.h
24  *
25  ******************************************************************************/
26 #pragma once
27 #include <type_traits>
28 #include "common/simdintrin.h"
29 
30 enum FixedPointFmt
31 {
32     FP_UNINIT,
33     _16_8,
34     _16_9,
35     _X_16,
36 };
37 
38 //////////////////////////////////////////////////////////////////////////
39 /// @brief convenience typedefs for supported Fixed Point precisions
40 typedef std::integral_constant<uint32_t, FP_UNINIT> Fixed_Uninit;
41 typedef std::integral_constant<uint32_t, _16_8>     Fixed_16_8;
42 typedef std::integral_constant<uint32_t, _16_9>     Fixed_16_9;
43 typedef std::integral_constant<uint32_t, _X_16>     Fixed_X_16;
44 
45 //////////////////////////////////////////////////////////////////////////
46 /// @struct FixedPointTraits
47 /// @brief holds constants relating to converting between FP and Fixed point
48 /// @tparam FT: fixed precision type
49 template <typename FT>
50 struct FixedPointTraits
51 {
52 };
53 
54 //////////////////////////////////////////////////////////////////////////
55 /// @brief Fixed_16_8 specialization of FixedPointTraits
56 template <>
57 struct FixedPointTraits<Fixed_16_8>
58 {
59     /// multiplier to go from FP32 to Fixed Point 16.8
60     typedef std::integral_constant<uint32_t, 256> ScaleT;
61     /// number of bits to shift to go from 16.8 fixed => int32
62     typedef std::integral_constant<uint32_t, 8> BitsT;
63     typedef Fixed_16_8                          TypeT;
64 };
65 
66 //////////////////////////////////////////////////////////////////////////
67 /// @brief Fixed_16_9 specialization of FixedPointTraits
68 template <>
69 struct FixedPointTraits<Fixed_16_9>
70 {
71     /// multiplier to go from FP32 to Fixed Point 16.9
72     typedef std::integral_constant<uint32_t, 512> ScaleT;
73     /// number of bits to shift to go from 16.9 fixed => int32
74     typedef std::integral_constant<uint32_t, 9> BitsT;
75     typedef Fixed_16_9                          TypeT;
76 };
77 
78 //////////////////////////////////////////////////////////////////////////
79 /// @brief Fixed_16_9 specialization of FixedPointTraits
80 template <>
81 struct FixedPointTraits<Fixed_X_16>
82 {
83     /// multiplier to go from FP32 to Fixed Point X.16
84     typedef std::integral_constant<uint32_t, 65536> ScaleT;
85     /// number of bits to shift to go from X.16 fixed => int32
86     typedef std::integral_constant<uint32_t, 16> BitsT;
87     typedef Fixed_X_16                           TypeT;
88 };
89 
90 //////////////////////////////////////////////////////////////////////////
91 /// @brief convenience typedefs for conservative rasterization modes
92 typedef std::false_type StandardRastT;
93 typedef std::true_type  ConservativeRastT;
94 
95 //////////////////////////////////////////////////////////////////////////
96 /// @brief convenience typedefs for Input Coverage rasterization modes
97 typedef std::integral_constant<uint32_t, SWR_INPUT_COVERAGE_NONE>   NoInputCoverageT;
98 typedef std::integral_constant<uint32_t, SWR_INPUT_COVERAGE_NORMAL> OuterConservativeCoverageT;
99 typedef std::integral_constant<uint32_t, SWR_INPUT_COVERAGE_INNER_CONSERVATIVE>
100     InnerConservativeCoverageT;
101 
102 //////////////////////////////////////////////////////////////////////////
103 /// @struct ConservativeRastTraits
104 /// @brief primary ConservativeRastTraits template. Shouldn't be instantiated
105 /// @tparam ConservativeT: type of conservative rasterization
106 template <typename ConservativeT>
107 struct ConservativeRastFETraits
108 {
109 };
110 
111 //////////////////////////////////////////////////////////////////////////
112 /// @brief StandardRast specialization of ConservativeRastTraits
113 template <>
114 struct ConservativeRastFETraits<StandardRastT>
115 {
116     typedef std::false_type                     IsConservativeT;
117     typedef std::integral_constant<uint32_t, 0> BoundingBoxOffsetT;
118 };
119 
120 //////////////////////////////////////////////////////////////////////////
121 /// @brief ConservativeRastT specialization of ConservativeRastTraits
122 template <>
123 struct ConservativeRastFETraits<ConservativeRastT>
124 {
125     typedef std::true_type                      IsConservativeT;
126     typedef std::integral_constant<uint32_t, 1> BoundingBoxOffsetT;
127 };
128 
129 //////////////////////////////////////////////////////////////////////////
130 /// @brief convenience typedefs for ConservativeRastFETraits
131 typedef ConservativeRastFETraits<StandardRastT>     FEStandardRastT;
132 typedef ConservativeRastFETraits<ConservativeRastT> FEConservativeRastT;
133 
134 //////////////////////////////////////////////////////////////////////////
135 /// @struct ConservativeRastBETraits
136 /// @brief primary ConservativeRastBETraits template. Shouldn't be instantiated;
137 /// default to standard rasterization behavior
138 /// @tparam ConservativeT: type of conservative rasterization
139 /// @tparam InputCoverageT: type of input coverage requested, if any
140 template <typename ConservativeT, typename _InputCoverageT>
141 struct ConservativeRastBETraits
142 {
143     typedef std::false_type                    IsConservativeT;
144     typedef _InputCoverageT                    InputCoverageT;
145     typedef FixedPointTraits<Fixed_16_8>       ConservativePrecisionT;
146     typedef std::integral_constant<int32_t, 0> ConservativeEdgeOffsetT;
147     typedef std::integral_constant<int32_t, 0> InnerConservativeEdgeOffsetT;
148 };
149 
150 //////////////////////////////////////////////////////////////////////////
151 /// @brief StandardRastT specialization of ConservativeRastBETraits
152 template <typename _InputCoverageT>
153 struct ConservativeRastBETraits<StandardRastT, _InputCoverageT>
154 {
155     typedef std::false_type                    IsConservativeT;
156     typedef _InputCoverageT                    InputCoverageT;
157     typedef FixedPointTraits<Fixed_16_8>       ConservativePrecisionT;
158     typedef std::integral_constant<int32_t, 0> ConservativeEdgeOffsetT;
159     typedef std::integral_constant<int32_t, 0> InnerConservativeEdgeOffsetT;
160 };
161 
162 //////////////////////////////////////////////////////////////////////////
163 /// @brief ConservativeRastT specialization of ConservativeRastBETraits
164 /// with no input coverage
165 template <>
166 struct ConservativeRastBETraits<ConservativeRastT, NoInputCoverageT>
167 {
168     typedef std::true_type   IsConservativeT;
169     typedef NoInputCoverageT InputCoverageT;
170 
171     typedef FixedPointTraits<Fixed_16_9> ConservativePrecisionT;
172 
173     /// offset edge away from pixel center by 1/2 pixel + 1/512, in Fixed 16.9 precision
174     /// this allows the rasterizer to do the 3 edge coverage tests against a single point, instead
175     /// of of having to compare individual edges to pixel corners to check if any part of the
176     /// triangle intersects a pixel
177     typedef std::integral_constant<int32_t, (ConservativePrecisionT::ScaleT::value / 2) + 1>
178                                                ConservativeEdgeOffsetT;
179     typedef std::integral_constant<int32_t, 0> InnerConservativeEdgeOffsetT;
180 };
181 
182 //////////////////////////////////////////////////////////////////////////
183 /// @brief ConservativeRastT specialization of ConservativeRastBETraits
184 /// with OuterConservativeCoverage
185 template <>
186 struct ConservativeRastBETraits<ConservativeRastT, OuterConservativeCoverageT>
187 {
188     typedef std::true_type             IsConservativeT;
189     typedef OuterConservativeCoverageT InputCoverageT;
190 
191     typedef FixedPointTraits<Fixed_16_9> ConservativePrecisionT;
192 
193     /// offset edge away from pixel center by 1/2 pixel + 1/512, in Fixed 16.9 precision
194     /// this allows the rasterizer to do the 3 edge coverage tests against a single point, instead
195     /// of of having to compare individual edges to pixel corners to check if any part of the
196     /// triangle intersects a pixel
197     typedef std::integral_constant<int32_t, (ConservativePrecisionT::ScaleT::value / 2) + 1>
198                                                ConservativeEdgeOffsetT;
199     typedef std::integral_constant<int32_t, 0> InnerConservativeEdgeOffsetT;
200 };
201 
202 //////////////////////////////////////////////////////////////////////////
203 /// @brief ConservativeRastT specialization of ConservativeRastBETraits
204 /// with InnerConservativeCoverage
205 template <>
206 struct ConservativeRastBETraits<ConservativeRastT, InnerConservativeCoverageT>
207 {
208     typedef std::true_type             IsConservativeT;
209     typedef InnerConservativeCoverageT InputCoverageT;
210 
211     typedef FixedPointTraits<Fixed_16_9> ConservativePrecisionT;
212 
213     /// offset edge away from pixel center by 1/2 pixel + 1/512, in Fixed 16.9 precision
214     /// this allows the rasterizer to do the 3 edge coverage tests against a single point, instead
215     /// of of having to compare individual edges to pixel corners to check if any part of the
216     /// triangle intersects a pixel
217     typedef std::integral_constant<int32_t, (ConservativePrecisionT::ScaleT::value / 2) + 1>
218         ConservativeEdgeOffsetT;
219 
220     /// undo the outer conservative offset and offset edge towards from pixel center by 1/2 pixel +
221     /// 1/512, in Fixed 16.9 precision this allows the rasterizer to do the 3 edge coverage tests
222     /// against a single point, instead of of having to compare individual edges to pixel corners to
223     /// check if a pixel is fully covered by a triangle
224     typedef std::integral_constant<int32_t,
225                                    static_cast<int32_t>(
226                                        -((ConservativePrecisionT::ScaleT::value / 2) + 1) -
227                                        ConservativeEdgeOffsetT::value)>
228         InnerConservativeEdgeOffsetT;
229 };