PageRenderTime 200ms CodeModel.GetById 55ms app.highlight 92ms RepoModel.GetById 39ms app.codeStats 0ms

/opengles/src/Rasterizer.h

http://ftk.googlecode.com/
C++ Header | 368 lines | 191 code | 75 blank | 102 comment | 1 complexity | 05726e6c2f9f91a5053840e2c8b3c6f5 MD5 | raw file
  1#ifndef EGL_RASTERIZER_H
  2#define EGL_RASTERIZER_H 1
  3
  4// ==========================================================================
  5//
  6// Rasterizer.h		Rasterizer Class for 3D Rendering Library
  7//
  8//					The rasterizer converts transformed and lit 
  9//					primitives and creates a raster image in the
 10//					current rendering surface.
 11//
 12// --------------------------------------------------------------------------
 13//
 14// 10-06-2003		Hans-Martin Will	initial version
 15//
 16// --------------------------------------------------------------------------
 17//
 18// Copyright (c) 2004, Hans-Martin Will. All rights reserved.
 19// 
 20// Redistribution and use in source and binary forms, with or without 
 21// modification, are permitted provided that the following conditions are 
 22// met:
 23// 
 24//	 *  Redistributions of source code must retain the above copyright
 25// 		notice, this list of conditions and the following disclaimer. 
 26//   *	Redistributions in binary form must reproduce the above copyright
 27// 		notice, this list of conditions and the following disclaimer in the 
 28// 		documentation and/or other materials provided with the distribution. 
 29// 
 30// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 31// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 32// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 33// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 34// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 
 35// OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 36// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 37// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 38// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 39// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
 40// THE POSSIBILITY OF SUCH DAMAGE.
 41//
 42// ==========================================================================
 43
 44
 45#include "OGLES.h"
 46#include "fixed.h"
 47#include "linalg.h"
 48#include "RasterizerState.h"
 49#include "FractionalColor.h"
 50#include "Surface.h"
 51
 52
 53namespace EGL {
 54
 55	class Texture;
 56	class FunctionCache;
 57
 58	// ----------------------------------------------------------------------
 59	// u and v coordinates for texture mapping
 60	// ----------------------------------------------------------------------
 61	struct TexCoord {
 62		EGL_Fixed		tu, tv;			// texture coordinates between 0 and 1
 63
 64#if EGL_MIPMAP_PER_TEXEL
 65		EGL_Fixed		dtudx, dtvdx;	// partial derivatives
 66		EGL_Fixed		dtudy, dtvdy;	// partial derivatives
 67#endif
 68	};
 69
 70	struct ScreenCoord {
 71		EGL_Fixed		x, y;		// x, y window coords
 72		EGL_Fixed		invZ;		// 1/Z (w) from depth division
 73
 74		// TO DO: once the rasterizer works properly, change this to binary 0..2^n-1
 75		EGL_Fixed		depth;		// depth coordinate after transformation
 76	};
 77
 78	struct EdgeCoord {
 79		EGL_Fixed		x, invZ;	// x window coords
 80
 81		// TO DO: once the rasterizer works properly, change this to binary 0..2^n-1
 82		EGL_Fixed		depth;		// depth coordinate
 83	};
 84
 85	// ----------------------------------------------------------------------
 86	// Vertex information as input for rasterizer
 87	// ----------------------------------------------------------------------
 88
 89	struct RasterPos {
 90		Vec4D				m_EyeCoords;
 91		Vec4D				m_ClipCoords;
 92		ScreenCoord			m_WindowCoords;		
 93		FractionalColor		m_Color;
 94		FractionalColor		m_FrontColor;		// color in range 0..255
 95		FractionalColor		m_BackColor;		
 96		TexCoord			m_TextureCoords[EGL_NUM_TEXTURE_UNITS];	// texture coords 0..1
 97		EGL_Fixed			m_FogDensity;		// fog density at this vertex
 98	};
 99
100	// ----------------------------------------------------------------------
101	// Coordinate increments used in various parts of the rasterizer
102	// ----------------------------------------------------------------------
103
104	struct EdgePos {
105		EdgeCoord			m_WindowCoords;								// z over w
106		FractionalColor		m_Color;									// color in range 0..255
107		TexCoord			m_TextureCoords[EGL_NUM_TEXTURE_UNITS];		// texture coords 0..1 over w
108		EGL_Fixed			m_FogDensity;								// fog density at this vertex
109	};
110
111	// ----------------------------------------------------------------------
112	// Gradient data structure used for triangle rasterization
113	// ----------------------------------------------------------------------
114
115	struct Gradients {
116		EdgePos				dx;
117		EdgePos				dy;
118	};
119
120	struct EdgePos;
121
122	struct RasterInfo {
123		// surface info
124		I32			SurfaceWidth;
125		I32			SurfaceHeight;
126		U16 *		DepthBuffer;
127		U16 *		ColorBuffer;
128		U32 *		StencilBuffer;
129		U8 *		AlphaBuffer;
130		const I32 *	InversionTablePtr;
131
132		// TODO: will need to add a minimum texture level here
133		// TODO: 
134
135		// texture info
136		Texture *	Textures[EGL_NUM_TEXTURE_UNITS];
137		U32			MipmapLevel[EGL_NUM_TEXTURE_UNITS];
138		U32			MaxMipmapLevel[EGL_NUM_TEXTURE_UNITS];
139
140        void Init(Surface * surface, I32 y);
141	};
142
143	// signature for generated scanline functions
144	typedef void (ScanlineFunction)(const RasterInfo * info, const EdgePos * start, const EdgePos * end);
145	typedef void (LineFunction)(const RasterInfo * info, const RasterPos * from, const RasterPos * to);
146	typedef void (PointFunction)(const RasterInfo * info, const RasterPos * pos, EGL_Fixed size);
147
148	class Rasterizer {
149
150	public:
151		enum {
152			PolygonOffsetUnitSize = 1,		// how to determine this?
153			DepthRangeMax = 0xffff			// 31 bits 
154		};
155
156		enum PixelFormat {
157			PixelFormatRGBA,
158			PixelFormatRGB,
159			PixelFormatLuminanceAlpha,
160			PixelFormatLuminance,
161			PixelFormatAlpha
162		};
163
164		enum PixelType {
165			PixelTypeUnsignedByte,
166			PixelTypeUnsignedShort_4_4_4_4,
167			PixelTypeUnsignedShort_5_5_5_1,
168			PixelTypeUnsignedShort_5_6_5
169		};
170
171
172	public:
173		Rasterizer(RasterizerState * state);
174		~Rasterizer();
175
176		// ----------------------------------------------------------------------
177		// Rendering surface
178		// ----------------------------------------------------------------------
179		void SetState(RasterizerState * state);
180		RasterizerState * GetState() const;
181
182		void SetSurface(Surface * surface);
183		Surface * GetSurface() const;
184
185		void SetTexture(size_t unit, MultiTexture * texture);
186		MultiTexture * GetTexture(size_t unit)				{ return m_Texture[unit]; }
187		const MultiTexture * GetTexture(size_t unit) const	{ return m_Texture[unit]; }
188		void PrepareTexture();
189
190		// ----------------------------------------------------------------------
191		// Actual rasterization of primitives
192		// These functions take care of anything downstream of the
193		// scissor test, scan conversion, texturing, compositing, depth &
194		// stencil test.
195		// ----------------------------------------------------------------------
196
197		typedef void (Rasterizer::*RasterTriangleFunction)(const RasterPos& a, const RasterPos& b,
198			const RasterPos& c);
199
200		void RasterPoint(const RasterPos& point, EGL_Fixed size);
201		void RasterLine(RasterPos& from, RasterPos& to);
202		void RasterTriangle(const RasterPos& a, const RasterPos& b,
203			const RasterPos& c);
204
205		// ----------------------------------------------------------------------
206		// State management
207		// ----------------------------------------------------------------------
208
209		void PreparePoint();
210		void PrepareLine();
211		void PrepareTriangle();
212		void Finish();
213		//void UpdateWindowClipping(void);
214
215		// ----------------------------------------------------------------------
216		// Include actual rasterization functions here
217		// ----------------------------------------------------------------------
218		//#include "generated_rasterization_function_declarations.h"
219
220	private:
221		// ----------------------------------------------------------------------
222		// Rasterization of triangle scan line
223		// ----------------------------------------------------------------------
224		void RasterScanLine(RasterInfo & info, const EdgePos & start, const EdgePos & end);
225
226		// ----------------------------------------------------------------------
227		// Rasterization of triangle
228		//
229		// Variations are: All, +/- color [Cc], +/- texture [Tt], +/- depth [Dd], +/- fog [Ff], +/- scissor [Ss]
230		// combination ct does not exist
231		// ----------------------------------------------------------------------
232		void RasterTriangleAll(const RasterPos& a, const RasterPos& b, const RasterPos& c);
233
234		enum RasterTriangleBits {
235			RasterTriangleColor,
236			RasterTriangleTexture,
237			RasterTriangleDepth,
238			RasterTriangleFog,
239			RasterTriangleScissor,
240			RasterTriangleStencil,
241			RasterTriangleCount,
242		};
243
244		void RasterTriangle_cTdfs(const RasterPos& a, const RasterPos& b, const RasterPos& c);
245		void RasterTriangle_cTdFs(const RasterPos& a, const RasterPos& b, const RasterPos& c);
246		void RasterTriangle_cTDfs(const RasterPos& a, const RasterPos& b, const RasterPos& c);
247		void RasterTriangle_cTDFs(const RasterPos& a, const RasterPos& b, const RasterPos& c);
248		void RasterTriangle_Ctdfs(const RasterPos& a, const RasterPos& b, const RasterPos& c);
249		void RasterTriangle_CtdFs(const RasterPos& a, const RasterPos& b, const RasterPos& c);
250		void RasterTriangle_CtDfs(const RasterPos& a, const RasterPos& b, const RasterPos& c);
251		void RasterTriangle_CtDFs(const RasterPos& a, const RasterPos& b, const RasterPos& c);
252		void RasterTriangle_CTdfs(const RasterPos& a, const RasterPos& b, const RasterPos& c);
253		void RasterTriangle_CTdFs(const RasterPos& a, const RasterPos& b, const RasterPos& c);
254		void RasterTriangle_CTDfs(const RasterPos& a, const RasterPos& b, const RasterPos& c);
255		void RasterTriangle_CTDFs(const RasterPos& a, const RasterPos& b, const RasterPos& c);
256		void RasterTriangle_cTdfS(const RasterPos& a, const RasterPos& b, const RasterPos& c);
257		void RasterTriangle_cTdFS(const RasterPos& a, const RasterPos& b, const RasterPos& c);
258		void RasterTriangle_cTDfS(const RasterPos& a, const RasterPos& b, const RasterPos& c);
259		void RasterTriangle_cTDFS(const RasterPos& a, const RasterPos& b, const RasterPos& c);
260		void RasterTriangle_CtdfS(const RasterPos& a, const RasterPos& b, const RasterPos& c);
261		void RasterTriangle_CtdFS(const RasterPos& a, const RasterPos& b, const RasterPos& c);
262		void RasterTriangle_CtDfS(const RasterPos& a, const RasterPos& b, const RasterPos& c);
263		void RasterTriangle_CtDFS(const RasterPos& a, const RasterPos& b, const RasterPos& c);
264		void RasterTriangle_CTdfS(const RasterPos& a, const RasterPos& b, const RasterPos& c);
265		void RasterTriangle_CTdFS(const RasterPos& a, const RasterPos& b, const RasterPos& c);
266		void RasterTriangle_CTDfS(const RasterPos& a, const RasterPos& b, const RasterPos& c);
267		void RasterTriangle_CTDFS(const RasterPos& a, const RasterPos& b, const RasterPos& c);
268
269		// ----------------------------------------------------------------------
270		// Rasterization of fragment
271		// ----------------------------------------------------------------------
272
273		void Fragment(I32 x, I32 y, EGL_Fixed depth, EGL_Fixed tu[], EGL_Fixed tv[], 
274			EGL_Fixed fogDensity, const Color& baseColor);
275			// will have special cases based on settings
276			// the coordinates are integer coordinates
277
278		void Fragment(const RasterInfo * rasterInfo, I32 x, EGL_Fixed depth, 
279					  EGL_Fixed tu[], EGL_Fixed tv[],
280					  const Color& baseColor, EGL_Fixed fog);
281			// fragment rendering with signature corresponding to function fragment
282			// generated by code generator
283
284		Color GetTexColor(const RasterizerState::TextureState * state, const Texture * texture, EGL_Fixed tu, EGL_Fixed tv, 
285						  RasterizerState::FilterMode filterMode);
286			// retrieve the texture color from a texture plane 
287						  
288		Color GetRawTexColor(const RasterizerState::TextureState * state, const Texture * texture, EGL_Fixed tu, EGL_Fixed tv);
289			// retrieve the texture color from a texture plane
290						  
291	private:
292		// ----------------------------------------------------------------------
293		// other settings
294		// ----------------------------------------------------------------------
295
296		RasterInfo				m_RasterInfo;
297		Surface *				m_Surface;			// rendering surface
298		MultiTexture *			m_Texture[EGL_NUM_TEXTURE_UNITS];	// current texture 
299		RasterizerState *		m_State;			// current rasterization settings
300		FunctionCache *			m_FunctionCache;
301
302		ScanlineFunction *		m_ScanlineFunction;
303		LineFunction *			m_LineFunction;		// raster lines function
304		PointFunction *			m_PointFunction;
305
306		RasterTriangleFunction	m_RasterTriangleFunction;
307
308		RasterTriangleFunction	m_RasterTriangleFunctions[1 << RasterTriangleCount];
309
310		// ----------------------------------------------------------------------
311		// internal state
312		// ----------------------------------------------------------------------
313		EGL_Fixed				m_MinX;
314		EGL_Fixed				m_MaxX;
315		EGL_Fixed				m_MinY;
316		EGL_Fixed				m_MaxY;
317
318		bool					m_UseMipmap[EGL_NUM_TEXTURE_UNITS];
319	};
320
321
322	// --------------------------------------------------------------------------
323	// Inline member definitions
324	// --------------------------------------------------------------------------
325
326
327	inline void Rasterizer :: SetSurface(Surface * surface) {
328		m_Surface = surface;
329	}
330
331
332	inline Surface * Rasterizer :: GetSurface() const {
333		return m_Surface;
334	}
335
336
337	inline void Rasterizer :: RasterTriangle(const RasterPos& a, const RasterPos& b, const RasterPos& c) {
338		(this->*m_RasterTriangleFunction)(a, b, c);
339	}
340
341
342#	if EGL_USE_JIT
343
344	inline void Rasterizer :: RasterPoint(const RasterPos& point, EGL_Fixed size) {
345		m_PointFunction(&m_RasterInfo, &point, size);
346	}
347
348	inline void Rasterizer :: RasterLine(RasterPos& p_from, RasterPos& p_to) {
349		p_from.m_WindowCoords.x = ((p_from.m_WindowCoords.x + 0x800) & ~0xfff);
350		p_from.m_WindowCoords.y = ((p_from.m_WindowCoords.y + 0x800) & ~0xfff);
351		p_to.m_WindowCoords.x = ((p_to.m_WindowCoords.x + 0x800) & ~0xfff);
352		p_to.m_WindowCoords.y = ((p_to.m_WindowCoords.y + 0x800) & ~0xfff);
353
354		m_LineFunction(&m_RasterInfo, &p_from, &p_to);
355	}
356
357	inline void Rasterizer :: RasterScanLine(RasterInfo & rasterInfo, const EdgePos & start, const EdgePos & end) {
358		m_ScanlineFunction(&rasterInfo, &start, &end);
359	}
360
361#	endif // EGL_USE_JIT
362
363}
364
365
366
367#endif //ndef EGL_RASTERIZER_H
368