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