summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/swr/rasterizer/core/conservativeRast.h
blob: 9e7f96cdeac818783e3292e3047e13b23cb25c55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/****************************************************************************
 * Copyright (C) 2014-2016 Intel Corporation.   All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * @file conservativerast.h
 *
 ******************************************************************************/
#pragma once
#include <type_traits>
#include "common/simdintrin.h"

enum FixedPointFmt
{
    FP_UNINIT,
    _16_8,
    _16_9,
    _X_16,
};

//////////////////////////////////////////////////////////////////////////
/// @brief convenience typedefs for supported Fixed Point precisions
typedef std::integral_constant<uint32_t, FP_UNINIT> Fixed_Uninit;
typedef std::integral_constant<uint32_t, _16_8>     Fixed_16_8;
typedef std::integral_constant<uint32_t, _16_9>     Fixed_16_9;
typedef std::integral_constant<uint32_t, _X_16>     Fixed_X_16;

//////////////////////////////////////////////////////////////////////////
/// @struct FixedPointTraits
/// @brief holds constants relating to converting between FP and Fixed point
/// @tparam FT: fixed precision type
template <typename FT>
struct FixedPointTraits
{
};

//////////////////////////////////////////////////////////////////////////
/// @brief Fixed_16_8 specialization of FixedPointTraits
template <>
struct FixedPointTraits<Fixed_16_8>
{
    /// multiplier to go from FP32 to Fixed Point 16.8
    typedef std::integral_constant<uint32_t, 256> ScaleT;
    /// number of bits to shift to go from 16.8 fixed => int32
    typedef std::integral_constant<uint32_t, 8> BitsT;
    typedef Fixed_16_8                          TypeT;
};

//////////////////////////////////////////////////////////////////////////
/// @brief Fixed_16_9 specialization of FixedPointTraits
template <>
struct FixedPointTraits<Fixed_16_9>
{
    /// multiplier to go from FP32 to Fixed Point 16.9
    typedef std::integral_constant<uint32_t, 512> ScaleT;
    /// number of bits to shift to go from 16.9 fixed => int32
    typedef std::integral_constant<uint32_t, 9> BitsT;
    typedef Fixed_16_9                          TypeT;
};

//////////////////////////////////////////////////////////////////////////
/// @brief Fixed_16_9 specialization of FixedPointTraits
template <>
struct FixedPointTraits<Fixed_X_16>
{
    /// multiplier to go from FP32 to Fixed Point X.16
    typedef std::integral_constant<uint32_t, 65536> ScaleT;
    /// number of bits to shift to go from X.16 fixed => int32
    typedef std::integral_constant<uint32_t, 16> BitsT;
    typedef Fixed_X_16                           TypeT;
};

//////////////////////////////////////////////////////////////////////////
/// @brief convenience typedefs for conservative rasterization modes
typedef std::false_type StandardRastT;
typedef std::true_type  ConservativeRastT;

//////////////////////////////////////////////////////////////////////////
/// @brief convenience typedefs for Input Coverage rasterization modes
typedef std::integral_constant<uint32_t, SWR_INPUT_COVERAGE_NONE>   NoInputCoverageT;
typedef std::integral_constant<uint32_t, SWR_INPUT_COVERAGE_NORMAL> OuterConservativeCoverageT;
typedef std::integral_constant<uint32_t, SWR_INPUT_COVERAGE_INNER_CONSERVATIVE>
    InnerConservativeCoverageT;

//////////////////////////////////////////////////////////////////////////
/// @struct ConservativeRastTraits
/// @brief primary ConservativeRastTraits template. Shouldn't be instantiated
/// @tparam ConservativeT: type of conservative rasterization
template <typename ConservativeT>
struct ConservativeRastFETraits
{
};

//////////////////////////////////////////////////////////////////////////
/// @brief StandardRast specialization of ConservativeRastTraits
template <>
struct ConservativeRastFETraits<StandardRastT>
{
    typedef std::false_type                     IsConservativeT;
    typedef std::integral_constant<uint32_t, 0> BoundingBoxOffsetT;
};

//////////////////////////////////////////////////////////////////////////
/// @brief ConservativeRastT specialization of ConservativeRastTraits
template <>
struct ConservativeRastFETraits<ConservativeRastT>
{
    typedef std::true_type                      IsConservativeT;
    typedef std::integral_constant<uint32_t, 1> BoundingBoxOffsetT;
};

//////////////////////////////////////////////////////////////////////////
/// @brief convenience typedefs for ConservativeRastFETraits
typedef ConservativeRastFETraits<StandardRastT>     FEStandardRastT;
typedef ConservativeRastFETraits<ConservativeRastT> FEConservativeRastT;

//////////////////////////////////////////////////////////////////////////
/// @struct ConservativeRastBETraits
/// @brief primary ConservativeRastBETraits template. Shouldn't be instantiated;
/// default to standard rasterization behavior
/// @tparam ConservativeT: type of conservative rasterization
/// @tparam InputCoverageT: type of input coverage requested, if any
template <typename ConservativeT, typename _InputCoverageT>
struct ConservativeRastBETraits
{
    typedef std::false_type                    IsConservativeT;
    typedef _InputCoverageT                    InputCoverageT;
    typedef FixedPointTraits<Fixed_16_8>       ConservativePrecisionT;
    typedef std::integral_constant<int32_t, 0> ConservativeEdgeOffsetT;
    typedef std::integral_constant<int32_t, 0> InnerConservativeEdgeOffsetT;
};

//////////////////////////////////////////////////////////////////////////
/// @brief StandardRastT specialization of ConservativeRastBETraits
template <typename _InputCoverageT>
struct ConservativeRastBETraits<StandardRastT, _InputCoverageT>
{
    typedef std::false_type                    IsConservativeT;
    typedef _InputCoverageT                    InputCoverageT;
    typedef FixedPointTraits<Fixed_16_8>       ConservativePrecisionT;
    typedef std::integral_constant<int32_t, 0> ConservativeEdgeOffsetT;
    typedef std::integral_constant<int32_t, 0> InnerConservativeEdgeOffsetT;
};

//////////////////////////////////////////////////////////////////////////
/// @brief ConservativeRastT specialization of ConservativeRastBETraits
/// with no input coverage
template <>
struct ConservativeRastBETraits<ConservativeRastT, NoInputCoverageT>
{
    typedef std::true_type   IsConservativeT;
    typedef NoInputCoverageT InputCoverageT;

    typedef FixedPointTraits<Fixed_16_9> ConservativePrecisionT;

    /// offset edge away from pixel center by 1/2 pixel + 1/512, in Fixed 16.9 precision
    /// this allows the rasterizer to do the 3 edge coverage tests against a single point, instead
    /// of of having to compare individual edges to pixel corners to check if any part of the
    /// triangle intersects a pixel
    typedef std::integral_constant<int32_t, (ConservativePrecisionT::ScaleT::value / 2) + 1>
                                               ConservativeEdgeOffsetT;
    typedef std::integral_constant<int32_t, 0> InnerConservativeEdgeOffsetT;
};

//////////////////////////////////////////////////////////////////////////
/// @brief ConservativeRastT specialization of ConservativeRastBETraits
/// with OuterConservativeCoverage
template <>
struct ConservativeRastBETraits<ConservativeRastT, OuterConservativeCoverageT>
{
    typedef std::true_type             IsConservativeT;
    typedef OuterConservativeCoverageT InputCoverageT;

    typedef FixedPointTraits<Fixed_16_9> ConservativePrecisionT;

    /// offset edge away from pixel center by 1/2 pixel + 1/512, in Fixed 16.9 precision
    /// this allows the rasterizer to do the 3 edge coverage tests against a single point, instead
    /// of of having to compare individual edges to pixel corners to check if any part of the
    /// triangle intersects a pixel
    typedef std::integral_constant<int32_t, (ConservativePrecisionT::ScaleT::value / 2) + 1>
                                               ConservativeEdgeOffsetT;
    typedef std::integral_constant<int32_t, 0> InnerConservativeEdgeOffsetT;
};

//////////////////////////////////////////////////////////////////////////
/// @brief ConservativeRastT specialization of ConservativeRastBETraits
/// with InnerConservativeCoverage
template <>
struct ConservativeRastBETraits<ConservativeRastT, InnerConservativeCoverageT>
{
    typedef std::true_type             IsConservativeT;
    typedef InnerConservativeCoverageT InputCoverageT;

    typedef FixedPointTraits<Fixed_16_9> ConservativePrecisionT;

    /// offset edge away from pixel center by 1/2 pixel + 1/512, in Fixed 16.9 precision
    /// this allows the rasterizer to do the 3 edge coverage tests against a single point, instead
    /// of of having to compare individual edges to pixel corners to check if any part of the
    /// triangle intersects a pixel
    typedef std::integral_constant<int32_t, (ConservativePrecisionT::ScaleT::value / 2) + 1>
        ConservativeEdgeOffsetT;

    /// undo the outer conservative offset and offset edge towards from pixel center by 1/2 pixel +
    /// 1/512, in Fixed 16.9 precision this allows the rasterizer to do the 3 edge coverage tests
    /// against a single point, instead of of having to compare individual edges to pixel corners to
    /// check if a pixel is fully covered by a triangle
    typedef std::integral_constant<int32_t,
                                   static_cast<int32_t>(
                                       -((ConservativePrecisionT::ScaleT::value / 2) + 1) -
                                       ConservativeEdgeOffsetT::value)>
        InnerConservativeEdgeOffsetT;
};