1 #ifndef _RRRASTERIZER_HPP
2 #define _RRRASTERIZER_HPP
3 /*-------------------------------------------------------------------------
4 * drawElements Quality Program Reference Renderer
5 * -----------------------------------------------
7 * Copyright 2014 The Android Open Source Project
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
23 * \brief Reference rasterizer
24 *//*--------------------------------------------------------------------*/
27 #include "tcuVector.hpp"
28 #include "rrRenderState.hpp"
29 #include "rrFragmentPacket.hpp"
35 //! Rasterizer configuration
38 RASTERIZER_SUBPIXEL_BITS = 8,
39 RASTERIZER_MAX_SAMPLES_PER_FRAGMENT = 16
42 //! Get coverage bit value.
43 inline deUint64 getCoverageBit (int numSamples, int x, int y, int sampleNdx)
45 const int numBits = (int)sizeof(deUint64)*8;
46 const int maxSamples = numBits/4;
47 DE_STATIC_ASSERT(maxSamples >= RASTERIZER_MAX_SAMPLES_PER_FRAGMENT);
48 DE_ASSERT(de::inRange(numSamples, 1, maxSamples) && de::inBounds(x, 0, 2) && de::inBounds(y, 0, 2));
49 return 1ull << ((x*2 + y)*numSamples + sampleNdx);
52 //! Get all sample bits for fragment
53 inline deUint64 getCoverageFragmentSampleBits (int numSamples, int x, int y)
55 DE_ASSERT(de::inBounds(x, 0, 2) && de::inBounds(y, 0, 2));
56 const deUint64 fragMask = (1ull << numSamples) - 1;
57 return fragMask << (x*2 + y)*numSamples;
60 //! Set bit in coverage mask.
61 inline deUint64 setCoverageValue (deUint64 mask, int numSamples, int x, int y, int sampleNdx, bool val)
63 const deUint64 bit = getCoverageBit(numSamples, x, y, sampleNdx);
64 return val ? (mask | bit) : (mask & ~bit);
67 //! Get coverage bit value in mask.
68 inline bool getCoverageValue (deUint64 mask, int numSamples, int x, int y, int sampleNdx)
70 return (mask & getCoverageBit(numSamples, x, y, sampleNdx)) != 0;
73 //! Test if any sample for fragment is live
74 inline bool getCoverageAnyFragmentSampleLive (deUint64 mask, int numSamples, int x, int y)
76 return (mask & getCoverageFragmentSampleBits(numSamples, x, y)) != 0;
79 //! Get position of first coverage bit of fragment - equivalent to deClz64(getCoverageFragmentSampleBits(numSamples, x, y)).
80 inline int getCoverageOffset (int numSamples, int x, int y)
82 return (x*2 + y)*numSamples;
85 /*--------------------------------------------------------------------*//*!
86 * \brief Edge function
88 * Edge function can be evaluated for point P (in fixed-point coordinates
89 * with SUBPIXEL_BITS fractional part) by computing
92 * D will be fixed-point value where lower (SUBPIXEL_BITS*2) bits will
95 * a and b are stored with SUBPIXEL_BITS fractional part, while c is stored
96 * with SUBPIXEL_BITS*2 fractional bits.
97 *//*--------------------------------------------------------------------*/
100 inline EdgeFunction (void) : a(0), b(0), c(0), inclusive(false) {}
105 bool inclusive; //!< True if edge is inclusive according to fill rules.
108 /*--------------------------------------------------------------------*//*!
109 * \brief Triangle rasterizer
111 * Triangle rasterizer implements following features:
112 * - Rasterization using fixed-point coordinates
113 * - 1, 4, and 16 -sample rasterization
114 * - Depth interpolation
115 * - Perspective-correct barycentric computation for interpolation
116 * - Visible face determination
118 * It does not (and will not) implement following:
121 * - Degenerate elimination
122 * - Coordinate transformation (inputs are in screen-space)
123 * - Culling - logic can be implemented outside by querying visible face
124 * - Scissoring (this can be done by controlling viewport rectangle)
125 * - Any per-fragment operations
126 *//*--------------------------------------------------------------------*/
127 class TriangleRasterizer
130 TriangleRasterizer (const tcu::IVec4& viewport, const int numSamples, const RasterizationState& state);
132 void init (const tcu::Vec4& v0, const tcu::Vec4& v1, const tcu::Vec4& v2);
134 // Following functions are only available after init()
135 FaceType getVisibleFace (void) const { return m_face; }
136 void rasterize (FragmentPacket* const fragmentPackets, float* const depthValues, const int maxFragmentPackets, int& numPacketsRasterized);
139 void rasterizeSingleSample (FragmentPacket* const fragmentPackets, float* const depthValues, const int maxFragmentPackets, int& numPacketsRasterized);
141 template<int NumSamples>
142 void rasterizeMultiSample (FragmentPacket* const fragmentPackets, float* const depthValues, const int maxFragmentPackets, int& numPacketsRasterized);
144 // Constant rasterization state.
145 const tcu::IVec4 m_viewport;
146 const int m_numSamples;
147 const Winding m_winding;
148 const HorizontalFill m_horizontalFill;
149 const VerticalFill m_verticalFill;
151 // Per-triangle rasterization state.
155 EdgeFunction m_edge01;
156 EdgeFunction m_edge12;
157 EdgeFunction m_edge20;
158 FaceType m_face; //!< Triangle orientation, eg. visible face.
159 tcu::IVec2 m_bboxMin; //!< Bounding box min (inclusive).
160 tcu::IVec2 m_bboxMax; //!< Bounding box max (inclusive).
161 tcu::IVec2 m_curPos; //!< Current rasterization position.
162 } DE_WARN_UNUSED_TYPE;
165 /*--------------------------------------------------------------------*//*!
166 * \brief Single sample line rasterizer
168 * Line rasterizer implements following features:
169 * - Rasterization using fixed-point coordinates
170 * - Depth interpolation
171 * - Perspective-correct interpolation
173 * It does not (and will not) implement following:
175 * - Multisampled line rasterization
176 *//*--------------------------------------------------------------------*/
177 class SingleSampleLineRasterizer
180 SingleSampleLineRasterizer (const tcu::IVec4& viewport);
181 ~SingleSampleLineRasterizer (void);
183 void init (const tcu::Vec4& v0, const tcu::Vec4& v1, float lineWidth);
185 // only available after init()
186 void rasterize (FragmentPacket* const fragmentPackets, float* const depthValues, const int maxFragmentPackets, int& numPacketsRasterized);
189 SingleSampleLineRasterizer (const SingleSampleLineRasterizer&); // not allowed
190 SingleSampleLineRasterizer& operator= (const SingleSampleLineRasterizer&); // not allowed
192 // Constant rasterization state.
193 const tcu::IVec4 m_viewport;
195 // Per-line rasterization state.
198 tcu::IVec2 m_bboxMin; //!< Bounding box min (inclusive).
199 tcu::IVec2 m_bboxMax; //!< Bounding box max (inclusive).
200 tcu::IVec2 m_curPos; //!< Current rasterization position.
201 deInt32 m_curRowFragment; //!< Current rasterization position of one fragment in column of lineWidth fragments
203 } DE_WARN_UNUSED_TYPE;
206 /*--------------------------------------------------------------------*//*!
207 * \brief Multisampled line rasterizer
209 * Line rasterizer implements following features:
210 * - Rasterization using fixed-point coordinates
211 * - Depth interpolation
212 * - Perspective-correct interpolation
214 * It does not (and will not) implement following:
216 * - Aliased line rasterization
217 *//*--------------------------------------------------------------------*/
218 class MultiSampleLineRasterizer
221 MultiSampleLineRasterizer (const int numSamples, const tcu::IVec4& viewport);
222 ~MultiSampleLineRasterizer ();
224 void init (const tcu::Vec4& v0, const tcu::Vec4& v1, float lineWidth);
226 // only available after init()
227 void rasterize (FragmentPacket* const fragmentPackets, float* const depthValues, const int maxFragmentPackets, int& numPacketsRasterized);
230 MultiSampleLineRasterizer (const MultiSampleLineRasterizer&); // not allowed
231 MultiSampleLineRasterizer& operator= (const MultiSampleLineRasterizer&); // not allowed
233 // Constant rasterization state.
234 const int m_numSamples;
236 // Per-line rasterization state.
237 TriangleRasterizer m_triangleRasterizer0; //!< not in array because we want to initialize these in the initialization list
238 TriangleRasterizer m_triangleRasterizer1;
239 } DE_WARN_UNUSED_TYPE;
242 /*--------------------------------------------------------------------*//*!
243 * \brief Pixel diamond
245 * Structure representing a diamond a line exits.
246 *//*--------------------------------------------------------------------*/
247 struct LineExitDiamond
252 /*--------------------------------------------------------------------*//*!
253 * \brief Line exit diamond generator
255 * For a given line, generates list of diamonds the line exits using the
256 * line-exit rules of the line rasterization. Does not do scissoring.
258 * \note Not used by rr, but provided to prevent test cases requiring
259 * accurate diamonds from abusing SingleSampleLineRasterizer.
260 *//*--------------------------------------------------------------------*/
261 class LineExitDiamondGenerator
264 LineExitDiamondGenerator (void);
265 ~LineExitDiamondGenerator (void);
267 void init (const tcu::Vec4& v0, const tcu::Vec4& v1);
269 // only available after init()
270 void rasterize (LineExitDiamond* const lineDiamonds, const int maxDiamonds, int& numWritten);
273 LineExitDiamondGenerator (const LineExitDiamondGenerator&); // not allowed
274 LineExitDiamondGenerator& operator= (const LineExitDiamondGenerator&); // not allowed
276 // Per-line rasterization state.
279 tcu::IVec2 m_bboxMin; //!< Bounding box min (inclusive).
280 tcu::IVec2 m_bboxMax; //!< Bounding box max (inclusive).
281 tcu::IVec2 m_curPos; //!< Current rasterization position.
286 #endif // _RRRASTERIZER_HPP