24
|
1 |
#ifndef __RIRASTERIZER_H
|
|
2 |
#define __RIRASTERIZER_H
|
|
3 |
|
|
4 |
/*------------------------------------------------------------------------
|
|
5 |
*
|
|
6 |
* OpenVG 1.1 Reference Implementation
|
|
7 |
* -----------------------------------
|
|
8 |
*
|
|
9 |
* Copyright (c) 2007 The Khronos Group Inc.
|
|
10 |
* Portions copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|
11 |
*
|
|
12 |
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
13 |
* copy of this software and /or associated documentation files
|
|
14 |
* (the "Materials "), to deal in the Materials without restriction,
|
|
15 |
* including without limitation the rights to use, copy, modify, merge,
|
|
16 |
* publish, distribute, sublicense, and/or sell copies of the Materials,
|
|
17 |
* and to permit persons to whom the Materials are furnished to do so,
|
|
18 |
* subject to the following conditions:
|
|
19 |
*
|
|
20 |
* The above copyright notice and this permission notice shall be included
|
|
21 |
* in all copies or substantial portions of the Materials.
|
|
22 |
*
|
|
23 |
* THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
24 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
25 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
26 |
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
27 |
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
28 |
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR
|
|
29 |
* THE USE OR OTHER DEALINGS IN THE MATERIALS.
|
|
30 |
*
|
|
31 |
*//**
|
|
32 |
* \file
|
|
33 |
* \brief Rasterizer class.
|
|
34 |
* \note
|
|
35 |
*//*-------------------------------------------------------------------*/
|
|
36 |
|
|
37 |
#ifndef __RIMATH_H
|
|
38 |
#include "riMath.h"
|
|
39 |
#endif
|
|
40 |
|
|
41 |
#ifndef __RIARRAY_H
|
|
42 |
#include "riArray.h"
|
|
43 |
#endif
|
|
44 |
|
|
45 |
#ifndef __RIPIXELPIPE_H
|
|
46 |
#include "riPixelPipe.h"
|
|
47 |
#endif
|
|
48 |
|
|
49 |
// TESTING ONLY!!
|
|
50 |
#include "sfDynamicPixelPipe.h"
|
|
51 |
|
|
52 |
#if defined(RI_DEBUG)
|
|
53 |
# include <stdio.h>
|
|
54 |
#endif
|
|
55 |
// \todo SSE2 probably has everything necessary and matches the minimum platform requirements.
|
|
56 |
// SSE4 is probably too new. SSE3?
|
|
57 |
// \note This will cause the code to not compile on MSVC...
|
|
58 |
|
|
59 |
#if defined(USE_SSE2)
|
|
60 |
// \todo only the last include is needed: QT creator does not find types if others not included.
|
|
61 |
# include <mmintrin.h>
|
|
62 |
# include <xmmintrin.h>
|
|
63 |
# include <emmintrin.h>
|
|
64 |
#endif
|
|
65 |
|
|
66 |
//=======================================================================
|
|
67 |
|
|
68 |
namespace OpenVGRI
|
|
69 |
{
|
|
70 |
|
|
71 |
/*-------------------------------------------------------------------*//*!
|
|
72 |
* \brief Scalar and vector data types used by the rasterizer.
|
|
73 |
* \param
|
|
74 |
* \return
|
|
75 |
* \note
|
|
76 |
*//*-------------------------------------------------------------------*/
|
|
77 |
|
|
78 |
typedef RIfloat RScalar; //change this if you want to have different precision for rasterizer scalars and RIfloat
|
|
79 |
|
|
80 |
struct RVector2
|
|
81 |
{
|
|
82 |
RI_INLINE RVector2() { }
|
|
83 |
RI_INLINE RVector2(const Vector2& v) { x = v.x; y = v.y; }
|
|
84 |
RI_INLINE RVector2(RIfloat vx, RIfloat vy) { x = vx; y = vy; }
|
|
85 |
RI_INLINE void set(RIfloat vx, RIfloat vy) { x = vx; y = vy; }
|
|
86 |
RScalar x;
|
|
87 |
RScalar y;
|
|
88 |
};
|
|
89 |
|
|
90 |
struct IVector2
|
|
91 |
{
|
|
92 |
RI_INLINE IVector2() {}
|
|
93 |
RI_INLINE IVector2(const IVector2& v) { x = v.x; y = v.y; }
|
|
94 |
RI_INLINE IVector2(const Vector2& v) { x = (int)v.x; y = (int) v.y; }
|
|
95 |
RI_INLINE IVector2(int vx, int vy) { x = vx; y = vy; }
|
|
96 |
RI_INLINE void set(int vx, int vy) { x = vx; y = vy; }
|
|
97 |
int x;
|
|
98 |
int y;
|
|
99 |
};
|
|
100 |
|
|
101 |
struct Span
|
|
102 |
{
|
|
103 |
RIuint16 x0;
|
|
104 |
RIuint16 y;
|
|
105 |
RIuint16 len;
|
|
106 |
RIuint8 coverage;
|
|
107 |
RIuint8 pad; // align to 8 bytes. \temp Use qualifiers instead?
|
|
108 |
};
|
|
109 |
|
|
110 |
/*-------------------------------------------------------------------*//*!
|
|
111 |
* \brief Converts a set of edges to coverage values for each pixel and
|
|
112 |
* calls PixelPipe::pixelPipe for painting a pixel.
|
|
113 |
* \param
|
|
114 |
* \return
|
|
115 |
* \note
|
|
116 |
*//*-------------------------------------------------------------------*/
|
|
117 |
|
|
118 |
class Rasterizer
|
|
119 |
{
|
|
120 |
public:
|
|
121 |
Rasterizer(); //throws bad_alloc
|
|
122 |
~Rasterizer();
|
|
123 |
|
|
124 |
void setup(int vpx, int vpy, int vpwidth, int vpheight, VGFillRule fillRule, const PixelPipe* pixelPipe);
|
|
125 |
void setScissor(const Array<Rectangle>& scissors); //throws bad_alloc
|
|
126 |
void setScissoring(bool enabled);
|
|
127 |
|
|
128 |
void clear();
|
|
129 |
void addEdge(const Vector2& v0, const Vector2& v1); //throws bad_alloc
|
|
130 |
|
|
131 |
RI_INLINE void setAntiAliasing(bool enable) { m_aa = enable; }
|
|
132 |
void fill(); //throws bad_alloc
|
|
133 |
|
|
134 |
void getBBox(int& sx, int& sy, int& ex, int& ey) const { sx = m_covMinx; sy = m_covMiny; ex = m_covMaxx; ey = m_covMaxy; }
|
|
135 |
|
|
136 |
public:
|
|
137 |
enum { SAMPLE_BITS = 3 };
|
|
138 |
enum { Y_BITS = SAMPLE_BITS };
|
|
139 |
enum { MAX_COVERAGE = 1<<SAMPLE_BITS };
|
|
140 |
|
|
141 |
// temp!:
|
|
142 |
RIuint32 *m_covBuffer;
|
|
143 |
size_t m_covBufferSz;
|
|
144 |
|
|
145 |
private:
|
|
146 |
Rasterizer(const Rasterizer&); //!< Not allowed.
|
|
147 |
const Rasterizer& operator=(const Rasterizer&); //!< Not allowed.
|
|
148 |
|
|
149 |
public:
|
|
150 |
struct ScissorEdge
|
|
151 |
{
|
|
152 |
ScissorEdge() {}
|
|
153 |
bool operator<(const ScissorEdge& e) const { return x < e.x; }
|
|
154 |
int x;
|
|
155 |
int miny;
|
|
156 |
int maxy;
|
|
157 |
int direction; //1 start, -1 end
|
|
158 |
};
|
|
159 |
|
|
160 |
struct Edge
|
|
161 |
{
|
|
162 |
Edge() {}
|
|
163 |
bool operator<(const Edge& e) const { return v0.y < e.v0.y; }
|
|
164 |
#if defined(RI_DEBUG)
|
|
165 |
bool operator<=(const Edge& e) const {return v0.y <= e.v0.y; }
|
|
166 |
#endif
|
|
167 |
IVector2 v0;
|
|
168 |
IVector2 v1;
|
|
169 |
int direction;
|
|
170 |
};
|
|
171 |
|
|
172 |
struct ActiveEdge
|
|
173 |
{
|
|
174 |
typedef RIint16 XCoord;
|
|
175 |
typedef RIint16 YCoord;
|
|
176 |
|
|
177 |
ActiveEdge() {}
|
|
178 |
//bool operator<(const ActiveEdge& e) const { return minx < e.minx; }
|
|
179 |
bool operator<(const ActiveEdge& e) const
|
|
180 |
{
|
|
181 |
if (minx < e.minx)
|
|
182 |
return true;
|
|
183 |
if (minx == e.minx)
|
|
184 |
return maxx < e.maxx;
|
|
185 |
return false;
|
|
186 |
}
|
|
187 |
#if defined(RI_DEBUG)
|
|
188 |
bool operator<=(const ActiveEdge& e) const { return minx <= e.minx; }
|
|
189 |
#endif
|
|
190 |
// Fixed-point edge coordinates.
|
|
191 |
RIuint32 next; // \todo Pointer?
|
|
192 |
int direction; //-1 down, 1 up
|
|
193 |
int xRef;
|
|
194 |
int slope;
|
|
195 |
XCoord minx; // for the current scanline
|
|
196 |
XCoord maxx; // for the current scanline
|
|
197 |
YCoord yStart; // for the edge
|
|
198 |
YCoord yEnd; // for the edge
|
|
199 |
};
|
|
200 |
|
|
201 |
enum { SF_SAMPLES = 8 };
|
|
202 |
enum { RASTERIZER_BITS = 14 };
|
|
203 |
enum { RASTERIZER_MASK = ((1<<RASTERIZER_BITS)-1) };
|
|
204 |
enum { Y_MASK = ((1<<Y_BITS)-1) };
|
|
205 |
enum { X_BITS = 7 };
|
|
206 |
enum { XMASK = ((1<<X_BITS)-1) };
|
|
207 |
enum { LEFT_DISCARD = -0x100000 };
|
|
208 |
enum { LEFT_DISCARD_SHORT = -32768 };
|
|
209 |
|
|
210 |
struct Windings
|
|
211 |
{
|
|
212 |
#if !defined(USE_SSE2)
|
|
213 |
Windings() {wq = 0;}
|
|
214 |
// \todo Use SSE counters or packed counters!
|
|
215 |
// \todo 8 samples -> 64 bits == 2 32-bit uints
|
|
216 |
union
|
|
217 |
{
|
|
218 |
RIint8 winding[SF_SAMPLES];
|
|
219 |
RIuint32 wd[SF_SAMPLES/4];
|
|
220 |
unsigned long long wq;
|
|
221 |
};
|
|
222 |
#else
|
|
223 |
Windings() { sseWinding = _mm_setzero_si128(); }
|
|
224 |
__m128i sseWinding;
|
|
225 |
#endif
|
|
226 |
};
|
|
227 |
|
|
228 |
private:
|
|
229 |
void pushEdge(const Edge& edge);
|
|
230 |
void clipAndAddEdge(Edge& edge);
|
|
231 |
void addBBox(const IVector2& v);
|
|
232 |
RI_INLINE void pushSpan(int x, int y, int len, int coverage);
|
|
233 |
RI_INLINE void commitSpans();
|
|
234 |
|
|
235 |
//RI_INLINE void calculateAEWinding(const ActiveEdge& currAe, Windings& pixel, Windings& scanline, int intY, int pixelX);
|
|
236 |
|
|
237 |
Array<ActiveEdge> m_edgePool;
|
|
238 |
Array<RIuint32> m_edges;
|
|
239 |
Array<ScissorEdge> m_scissorEdges;
|
|
240 |
Array<ActiveEdge> m_aet;
|
|
241 |
Array<ScissorEdge> m_scissorAet;
|
|
242 |
bool m_scissor;
|
|
243 |
|
|
244 |
bool m_aa;
|
|
245 |
|
|
246 |
IVector2 m_edgeMin;
|
|
247 |
IVector2 m_edgeMax;
|
|
248 |
int m_covMinx;
|
|
249 |
int m_covMiny;
|
|
250 |
int m_covMaxx;
|
|
251 |
int m_covMaxy;
|
|
252 |
int m_vpx;
|
|
253 |
int m_vpy;
|
|
254 |
int m_vpwidth;
|
|
255 |
int m_vpheight;
|
|
256 |
int m_vpMinx;
|
|
257 |
int m_vpMiny;
|
|
258 |
int m_vpMaxx;
|
|
259 |
int m_vpMaxy;
|
|
260 |
VGFillRule m_fillRule;
|
|
261 |
RIuint32 m_fillRuleMask;
|
|
262 |
|
|
263 |
const PixelPipe* m_pixelPipe;
|
|
264 |
PixelPipe::PPVariants m_ppVariants;
|
|
265 |
|
|
266 |
enum { N_CACHED_SPANS = 64 };
|
|
267 |
Span m_spanCache[N_CACHED_SPANS];
|
|
268 |
int m_nSpans;
|
|
269 |
|
|
270 |
};
|
|
271 |
|
|
272 |
//=======================================================================
|
|
273 |
|
|
274 |
} //namespace OpenVGRI
|
|
275 |
|
|
276 |
//=======================================================================
|
|
277 |
|
|
278 |
#endif /* __RIRASTERIZER_H */
|