/opengles/src/arm/CodeGenerator.h

http://ftk.googlecode.com/ · C Header · 273 lines · 161 code · 49 blank · 63 comment · 0 complexity · 5b82bac318058bd0bc442895036155f1 MD5 · raw file

  1. #ifndef EGL_CODE_GENERATOR_H
  2. #define EGL_CODE_GENERATOR_H 1
  3. // ==========================================================================
  4. //
  5. // CodeGenerator.h JIT Class for 3D Rendering Library
  6. //
  7. // This file contains the rasterizer functions that
  8. // implement the runtime code generation support
  9. // for optimized scan line rasterization routines.
  10. //
  11. // --------------------------------------------------------------------------
  12. //
  13. // 08-07-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 "Rasterizer.h"
  46. #include "RasterizerState.h"
  47. #include "FunctionCache.h"
  48. extern "C" {
  49. #ifdef __GCC32__
  50. struct cg_module_t;
  51. struct cg_proc_t;
  52. struct cg_block_t;
  53. struct cg_block_ref_t;
  54. struct cg_virtual_reg_t;
  55. #else
  56. typedef struct cg_module_t cg_module_t;
  57. typedef struct cg_proc_t cg_proc_t;
  58. typedef struct cg_block_t cg_block_t;
  59. typedef struct cg_block_ref_t cg_block_ref_t;
  60. typedef struct cg_virtual_reg_t cg_virtual_reg_t;
  61. #endif
  62. }
  63. namespace EGL {
  64. struct FragmentGenerationInfo {
  65. cg_virtual_reg_t * regX;
  66. cg_virtual_reg_t * regY;
  67. cg_virtual_reg_t * regDepth;
  68. cg_virtual_reg_t * regU[EGL_NUM_TEXTURE_UNITS];
  69. cg_virtual_reg_t * regV[EGL_NUM_TEXTURE_UNITS];
  70. cg_virtual_reg_t * regFog;
  71. cg_virtual_reg_t * regR;
  72. cg_virtual_reg_t * regG;
  73. cg_virtual_reg_t * regB;
  74. cg_virtual_reg_t * regA;
  75. cg_virtual_reg_t * regInfo;
  76. cg_virtual_reg_t * regTexture[EGL_NUM_TEXTURE_UNITS];
  77. };
  78. class FunctionCache;
  79. class MultiTexture;
  80. class CodeGenerator {
  81. public:
  82. // ----------------------------------------------------------------------
  83. // Code generation of triangle scan line
  84. // ----------------------------------------------------------------------
  85. void Compile(FunctionCache * target, FunctionCache::FunctionType type,
  86. void (CodeGenerator::*function)());
  87. void SetState(const RasterizerState * state) { m_State = state; }
  88. const RasterizerState * GetState() { return m_State; }
  89. void GenerateRasterScanLine();
  90. void GenerateRasterLine();
  91. void GenerateRasterPoint();
  92. void GenerateRasterTriangle();
  93. private:
  94. void GenerateFragment(cg_proc_t * procedure, cg_block_t * currentBlock,
  95. cg_block_ref_t * continuation, FragmentGenerationInfo & fragmentInfo,
  96. int weight, bool forceScissor = false);
  97. void GenerateFetchTexColor(cg_proc_t * proc, cg_block_t * currentBlock,
  98. size_t unit,
  99. FragmentGenerationInfo & fragmentInfo,
  100. cg_virtual_reg_t *& regTexColorR,
  101. cg_virtual_reg_t *& regTexColorG,
  102. cg_virtual_reg_t *& regTexColorB,
  103. cg_virtual_reg_t *& regTexColorA,
  104. cg_virtual_reg_t *& regTexColor565);
  105. void FetchTexColor(cg_proc_t * proc, cg_block_t * currentBlock,
  106. const RasterizerState::TextureState * textureState,
  107. cg_virtual_reg_t * regTexData,
  108. cg_virtual_reg_t * regTexOffset,
  109. cg_virtual_reg_t *& regTexColorR,
  110. cg_virtual_reg_t *& regTexColorG,
  111. cg_virtual_reg_t *& regTexColorB,
  112. cg_virtual_reg_t *& regTexColorA,
  113. cg_virtual_reg_t *& regTexColor565);
  114. cg_virtual_reg_t * Mul255(cg_block_t * currentBlock, cg_virtual_reg_t * first, cg_virtual_reg_t * second);
  115. cg_virtual_reg_t * Add(cg_block_t * currentBlock, cg_virtual_reg_t * first, cg_virtual_reg_t * second);
  116. cg_virtual_reg_t * AddSigned(cg_block_t * currentBlock, cg_virtual_reg_t * first, cg_virtual_reg_t * second);
  117. cg_virtual_reg_t * Sub(cg_block_t * currentBlock, cg_virtual_reg_t * first, cg_virtual_reg_t * second);
  118. cg_virtual_reg_t * AddSaturate255(cg_block_t * currentBlock, cg_virtual_reg_t * first, cg_virtual_reg_t * second);
  119. cg_virtual_reg_t * ClampTo255(cg_block_t * currentBlock, cg_virtual_reg_t * value);
  120. cg_virtual_reg_t * ExtractBitFieldTo255(cg_block_t * currentBlock, cg_virtual_reg_t * value, size_t low, size_t high);
  121. cg_virtual_reg_t * BitFieldFrom255(cg_block_t * currentBlock, cg_virtual_reg_t * value, size_t low, size_t high);
  122. cg_virtual_reg_t * Dot3(cg_block_t * currentBlock, cg_virtual_reg_t * r[], cg_virtual_reg_t * g[], cg_virtual_reg_t * b[]);
  123. cg_virtual_reg_t * SignedVal(cg_block_t * block, cg_virtual_reg_t * value);
  124. void Color565FromRGB(cg_block_t * block, cg_virtual_reg_t * result,
  125. cg_virtual_reg_t * r, cg_virtual_reg_t * g, cg_virtual_reg_t * b);
  126. cg_virtual_reg_t * Color565FromRGB(cg_block_t * block,
  127. cg_virtual_reg_t * r, cg_virtual_reg_t * g, cg_virtual_reg_t * b);
  128. cg_virtual_reg_t * Blend255(cg_block_t * currentBlock, cg_virtual_reg_t * first, cg_virtual_reg_t * second,
  129. cg_virtual_reg_t * alpha);
  130. cg_virtual_reg_t * Blend255(cg_block_t * currentBlock, U8 constant, cg_virtual_reg_t * second,
  131. cg_virtual_reg_t * alpha);
  132. private:
  133. const RasterizerState * m_State;
  134. struct cg_module_t *m_Module;
  135. };
  136. }
  137. // -------------------------------------------------------------------------
  138. // Offsets of structure members within info structure
  139. // -------------------------------------------------------------------------
  140. # define OFFSET_SURFACE_WIDTH offsetof(RasterInfo, SurfaceWidth)
  141. # define OFFSET_SURFACE_HEIGHT offsetof(RasterInfo, SurfaceHeight)
  142. # define OFFSET_SURFACE_DEPTH_BUFFER offsetof(RasterInfo, DepthBuffer)
  143. # define OFFSET_SURFACE_COLOR_BUFFER offsetof(RasterInfo, ColorBuffer)
  144. # define OFFSET_SURFACE_STENCIL_BUFFER offsetof(RasterInfo, StencilBuffer)
  145. # define OFFSET_SURFACE_ALPHA_BUFFER offsetof(RasterInfo, AlphaBuffer)
  146. # define OFFSET_TEXTURES offsetof(RasterInfo, Textures)
  147. # define OFFSET_MIPMAP_LEVEL offsetof(RasterInfo, MipmapLevel)
  148. # define OFFSET_MAX_MIPMAP_LEVEL offsetof(RasterInfo, MaxMipmapLevel)
  149. # define OFFSET_TEXTURE_LOG_WIDTH offsetof(Texture, m_LogWidth)
  150. # define OFFSET_TEXTURE_LOG_HEIGHT offsetof(Texture, m_LogHeight)
  151. # define OFFSET_TEXTURE_DATA offsetof(Texture, m_Data)
  152. # define SIZE_TEXTURE sizeof(Texture)
  153. # define OFFSET_INVERSE_TABLE_PTR offsetof(RasterInfo, InversionTablePtr)
  154. // -------------------------------------------------------------------------
  155. // For FractionalColor
  156. // -------------------------------------------------------------------------
  157. # define OFFSET_COLOR_RED offsetof(FractionalColor, r)
  158. # define OFFSET_COLOR_GREEN offsetof(FractionalColor, g)
  159. # define OFFSET_COLOR_BLUE offsetof(FractionalColor, b)
  160. # define OFFSET_COLOR_ALPHA offsetof(FractionalColor, a)
  161. // -------------------------------------------------------------------------
  162. // For ScreenCoord
  163. // -------------------------------------------------------------------------
  164. # define OFFSET_SCREEN_X offsetof(ScreenCoord, x)
  165. # define OFFSET_SCREEN_Y offsetof(ScreenCoord, y)
  166. # define OFFSET_SCREEN_INV_Z offsetof(ScreenCoord, invZ)
  167. # define OFFSET_SCREEN_DEPTH offsetof(ScreenCoord, depth)
  168. // -------------------------------------------------------------------------
  169. // For EdgeCoord
  170. // -------------------------------------------------------------------------
  171. # define OFFSET_EDGE_COORD_X offsetof(EdgeCoord, x)
  172. # define OFFSET_EDGE_COORD_INV_Z offsetof(EdgeCoord, invZ)
  173. # define OFFSET_EDGE_COORD_DEPTH offsetof(EdgeCoord, depth)
  174. // -------------------------------------------------------------------------
  175. // For TexCoord
  176. // -------------------------------------------------------------------------
  177. # define OFFSET_TEX_COORD_TU offsetof(TexCoord, tu)
  178. # define OFFSET_TEX_COORD_TV offsetof(TexCoord, tv)
  179. # define OFFSET_TEX_COORD_DTUDX offsetof(TexCoord, dtudx)
  180. # define OFFSET_TEX_COORD_DTUDY offsetof(TexCoord, dtudy)
  181. # define OFFSET_TEX_COORD_DTVDX offsetof(TexCoord, dtvdx)
  182. # define OFFSET_TEX_COORD_DTVDY offsetof(TexCoord, dtvdy)
  183. // -------------------------------------------------------------------------
  184. // For EdgeBuffer
  185. // -------------------------------------------------------------------------
  186. # define OFFSET_EDGE_BUFFER_WINDOW offsetof(EdgePos, m_WindowCoords)
  187. # define OFFSET_EDGE_BUFFER_COLOR offsetof(EdgePos, m_Color)
  188. # define OFFSET_EDGE_BUFFER_TEXTURE offsetof(EdgePos, m_TextureCoords)
  189. # define OFFSET_EDGE_BUFFER_FOG offsetof(EdgePos, m_FogDensity)
  190. # define OFFSET_EDGE_BUFFER_WINDOW_X (OFFSET_EDGE_BUFFER_WINDOW + OFFSET_EDGE_COORD_X)
  191. # define OFFSET_EDGE_BUFFER_WINDOW_INV_Z (OFFSET_EDGE_BUFFER_WINDOW + OFFSET_EDGE_COORD_INV_Z)
  192. # define OFFSET_EDGE_BUFFER_WINDOW_DEPTH (OFFSET_EDGE_BUFFER_WINDOW + OFFSET_EDGE_COORD_DEPTH)
  193. # define OFFSET_EDGE_BUFFER_COLOR_R (OFFSET_EDGE_BUFFER_COLOR + OFFSET_COLOR_RED)
  194. # define OFFSET_EDGE_BUFFER_COLOR_G (OFFSET_EDGE_BUFFER_COLOR + OFFSET_COLOR_GREEN)
  195. # define OFFSET_EDGE_BUFFER_COLOR_B (OFFSET_EDGE_BUFFER_COLOR + OFFSET_COLOR_BLUE)
  196. # define OFFSET_EDGE_BUFFER_COLOR_A (OFFSET_EDGE_BUFFER_COLOR + OFFSET_COLOR_ALPHA)
  197. # define OFFSET_EDGE_BUFFER_TEX_TU (OFFSET_EDGE_BUFFER_TEXTURE + OFFSET_TEX_COORD_TU)
  198. # define OFFSET_EDGE_BUFFER_TEX_TV (OFFSET_EDGE_BUFFER_TEXTURE + OFFSET_TEX_COORD_TV)
  199. # define OFFSET_EDGE_BUFFER_TEX_DTUDX (OFFSET_EDGE_BUFFER_TEXTURE + OFFSET_TEX_COORD_DTUDX)
  200. # define OFFSET_EDGE_BUFFER_TEX_DTUDY (OFFSET_EDGE_BUFFER_TEXTURE + OFFSET_TEX_COORD_DTUDY)
  201. # define OFFSET_EDGE_BUFFER_TEX_DTVDX (OFFSET_EDGE_BUFFER_TEXTURE + OFFSET_TEX_COORD_DTVDX)
  202. # define OFFSET_EDGE_BUFFER_TEX_DTVDY (OFFSET_EDGE_BUFFER_TEXTURE + OFFSET_TEX_COORD_DTVDY)
  203. // -------------------------------------------------------------------------
  204. // For RasterPos
  205. // -------------------------------------------------------------------------
  206. # define OFFSET_RASTER_POS_WINDOW offsetof(RasterPos, m_WindowCoords)
  207. # define OFFSET_RASTER_POS_COLOR offsetof(RasterPos, m_Color)
  208. # define OFFSET_RASTER_POS_TEXTURE offsetof(RasterPos, m_TextureCoords)
  209. # define OFFSET_RASTER_POS_FOG offsetof(RasterPos, m_FogDensity)
  210. # define OFFSET_RASTER_POS_WINDOW_X (OFFSET_RASTER_POS_WINDOW + OFFSET_SCREEN_X)
  211. # define OFFSET_RASTER_POS_WINDOW_Y (OFFSET_RASTER_POS_WINDOW + OFFSET_SCREEN_Y)
  212. # define OFFSET_RASTER_POS_WINDOW_INV_Z (OFFSET_RASTER_POS_WINDOW + OFFSET_SCREEN_INV_Z)
  213. # define OFFSET_RASTER_POS_WINDOW_DEPTH (OFFSET_RASTER_POS_WINDOW + OFFSET_SCREEN_DEPTH)
  214. # define OFFSET_RASTER_POS_COLOR_R (OFFSET_RASTER_POS_COLOR + OFFSET_COLOR_RED)
  215. # define OFFSET_RASTER_POS_COLOR_G (OFFSET_RASTER_POS_COLOR + OFFSET_COLOR_GREEN)
  216. # define OFFSET_RASTER_POS_COLOR_B (OFFSET_RASTER_POS_COLOR + OFFSET_COLOR_BLUE)
  217. # define OFFSET_RASTER_POS_COLOR_A (OFFSET_RASTER_POS_COLOR + OFFSET_COLOR_ALPHA)
  218. # define OFFSET_RASTER_POS_TEX_TU (OFFSET_RASTER_POS_TEXTURE + OFFSET_TEX_COORD_TU)
  219. # define OFFSET_RASTER_POS_TEX_TV (OFFSET_RASTER_POS_TEXTURE + OFFSET_TEX_COORD_TV)
  220. # define OFFSET_RASTER_POS_TEX_DTUDX (OFFSET_RASTER_POS_TEXTURE + OFFSET_TEX_COORD_DTUDX)
  221. # define OFFSET_RASTER_POS_TEX_DTUDY (OFFSET_RASTER_POS_TEXTURE + OFFSET_TEX_COORD_DTUDY)
  222. # define OFFSET_RASTER_POS_TEX_DTVDX (OFFSET_RASTER_POS_TEXTURE + OFFSET_TEX_COORD_DTVDX)
  223. # define OFFSET_RASTER_POS_TEX_DTVDY (OFFSET_RASTER_POS_TEXTURE + OFFSET_TEX_COORD_DTVDY)
  224. #endif //ndef EGL_CODE_GENERATOR_H