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 ViewportOrientation m_viewportOrientation; //!< Direction of +x+y axis
163 } DE_WARN_UNUSED_TYPE;
166 /*--------------------------------------------------------------------*//*!
167 * \brief Single sample line rasterizer
169 * Line rasterizer implements following features:
170 * - Rasterization using fixed-point coordinates
171 * - Depth interpolation
172 * - Perspective-correct interpolation
174 * It does not (and will not) implement following:
176 * - Multisampled line rasterization
177 *//*--------------------------------------------------------------------*/
178 class SingleSampleLineRasterizer
181 SingleSampleLineRasterizer (const tcu::IVec4& viewport);
182 ~SingleSampleLineRasterizer (void);
184 void init (const tcu::Vec4& v0, const tcu::Vec4& v1, float lineWidth);
186 // only available after init()
187 void rasterize (FragmentPacket* const fragmentPackets, float* const depthValues, const int maxFragmentPackets, int& numPacketsRasterized);
190 SingleSampleLineRasterizer (const SingleSampleLineRasterizer&); // not allowed
191 SingleSampleLineRasterizer& operator= (const SingleSampleLineRasterizer&); // not allowed
193 // Constant rasterization state.
194 const tcu::IVec4 m_viewport;
196 // Per-line rasterization state.
199 tcu::IVec2 m_bboxMin; //!< Bounding box min (inclusive).
200 tcu::IVec2 m_bboxMax; //!< Bounding box max (inclusive).
201 tcu::IVec2 m_curPos; //!< Current rasterization position.
202 deInt32 m_curRowFragment; //!< Current rasterization position of one fragment in column of lineWidth fragments
204 } DE_WARN_UNUSED_TYPE;
207 /*--------------------------------------------------------------------*//*!
208 * \brief Multisampled line rasterizer
210 * Line rasterizer implements following features:
211 * - Rasterization using fixed-point coordinates
212 * - Depth interpolation
213 * - Perspective-correct interpolation
215 * It does not (and will not) implement following:
217 * - Aliased line rasterization
218 *//*--------------------------------------------------------------------*/
219 class MultiSampleLineRasterizer
222 MultiSampleLineRasterizer (const int numSamples, const tcu::IVec4& viewport);
223 ~MultiSampleLineRasterizer ();
225 void init (const tcu::Vec4& v0, const tcu::Vec4& v1, float lineWidth);
227 // only available after init()
228 void rasterize (FragmentPacket* const fragmentPackets, float* const depthValues, const int maxFragmentPackets, int& numPacketsRasterized);
231 MultiSampleLineRasterizer (const MultiSampleLineRasterizer&); // not allowed
232 MultiSampleLineRasterizer& operator= (const MultiSampleLineRasterizer&); // not allowed
234 // Constant rasterization state.
235 const int m_numSamples;
237 // Per-line rasterization state.
238 TriangleRasterizer m_triangleRasterizer0; //!< not in array because we want to initialize these in the initialization list
239 TriangleRasterizer m_triangleRasterizer1;
240 } DE_WARN_UNUSED_TYPE;
243 /*--------------------------------------------------------------------*//*!
244 * \brief Pixel diamond
246 * Structure representing a diamond a line exits.
247 *//*--------------------------------------------------------------------*/
248 struct LineExitDiamond
253 /*--------------------------------------------------------------------*//*!
254 * \brief Line exit diamond generator
256 * For a given line, generates list of diamonds the line exits using the
257 * line-exit rules of the line rasterization. Does not do scissoring.
259 * \note Not used by rr, but provided to prevent test cases requiring
260 * accurate diamonds from abusing SingleSampleLineRasterizer.
261 *//*--------------------------------------------------------------------*/
262 class LineExitDiamondGenerator
265 LineExitDiamondGenerator (void);
266 ~LineExitDiamondGenerator (void);
268 void init (const tcu::Vec4& v0, const tcu::Vec4& v1);
270 // only available after init()
271 void rasterize (LineExitDiamond* const lineDiamonds, const int maxDiamonds, int& numWritten);
274 LineExitDiamondGenerator (const LineExitDiamondGenerator&); // not allowed
275 LineExitDiamondGenerator& operator= (const LineExitDiamondGenerator&); // not allowed
277 // Per-line rasterization state.
280 tcu::IVec2 m_bboxMin; //!< Bounding box min (inclusive).
281 tcu::IVec2 m_bboxMax; //!< Bounding box max (inclusive).
282 tcu::IVec2 m_curPos; //!< Current rasterization position.
287 #endif // _RRRASTERIZER_HPP