PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Pixie/src/ri/reyes.h

#
C Header | 328 lines | 192 code | 57 blank | 79 comment | 24 complexity | 1f33dddfcd03846351a6d1c6bd6e36a2 MD5 | raw file
Possible License(s): LGPL-2.1
  1. //////////////////////////////////////////////////////////////////////
  2. //
  3. // Pixie
  4. //
  5. // Copyright Š 1999 - 2003, Okan Arikan
  6. //
  7. // Contact: okan@cs.utexas.edu
  8. //
  9. // This library is free software; you can redistribute it and/or
  10. // modify it under the terms of the GNU Lesser General Public
  11. // License as published by the Free Software Foundation; either
  12. // version 2.1 of the License, or (at your option) any later version.
  13. //
  14. // This library is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. // Lesser General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU Lesser General Public
  20. // License along with this library; if not, write to the Free Software
  21. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. //
  23. ///////////////////////////////////////////////////////////////////////
  24. ///////////////////////////////////////////////////////////////////////
  25. //
  26. // File : reyes.h
  27. // Classes : CReyes
  28. // Description : The rasterizer class
  29. //
  30. ////////////////////////////////////////////////////////////////////////
  31. #ifndef REYES_H
  32. #define REYES_H
  33. #include "common/global.h"
  34. #include "shading.h"
  35. #include "object.h"
  36. #include "renderer.h"
  37. #include "ri_config.h"
  38. // Note:
  39. // The low bits are spliced with the zfilter mode and used to dispatch in stochastic hider
  40. // high bits are used in the primitive rasterization code to modify behavior
  41. const unsigned int RASTER_MOVING = 1 << 0; // The primitive is moving
  42. const unsigned int RASTER_TRANSPARENT = 1 << 1; // The primitive is transparent
  43. const unsigned int RASTER_POINT = 1 << 2; // The primitive is a point
  44. const unsigned int RASTER_UNSHADED = 1 << 3; // The primitive has not been shaded (only displaced)
  45. const unsigned int RASTER_FOCALBLUR = 1 << 4; // The primitive has focalblur
  46. const unsigned int RASTER_EXTRASAMPLES = 1 << 5; // The primitive has extra samples to compute
  47. const unsigned int RASTER_MATTE = 1 << 6; // The primitive is a matte
  48. const unsigned int RASTER_LOD = 1 << 7; // The primitive has LOD
  49. const unsigned int RASTER_UNDERCULL = 1 << 8; // The primitive requires underculling
  50. const unsigned int RASTER_XTREME = 1 << 9; // The primitive has extreme motion blur/depth of field
  51. const unsigned int RASTER_GLOBAL_MASK = (1 << 10) - 1;// This mask is used to block the lower fields
  52. const unsigned int RASTER_HIGHBITS_SHIFT = 10; // The shift needed to put bits in the higher fields
  53. const unsigned int RASTER_DEPTHFILT_MASK = 1;
  54. const unsigned int RASTER_DRAW_FRONT = 1 << 10; // Draw the front of the primitive
  55. const unsigned int RASTER_DRAW_BACK = 1 << 11; // Draw the back of the primitive
  56. const unsigned int RASTER_SHADE_HIDDEN = 1 << 12; // Shade the primitive even if occluded
  57. const unsigned int RASTER_SHADE_BACKFACE = 1 << 13; // Shade the primitive even if backfacing
  58. ///////////////////////////////////////////////////////////////////////
  59. // Class : CReyes
  60. // Description : This class is implementes a REYES like scan
  61. // line renderer
  62. // Comments :
  63. class CReyes : public CShadingContext {
  64. protected:
  65. ///////////////////////////////////////////////////////////////////////
  66. // Class : CRasterPatch
  67. // Description : Holds an object associated with a bucket
  68. // Comments : The same instance of this class may be associated with multiple buckets in multiple threads
  69. class CRasterObject {
  70. public:
  71. CObject *object; // The object
  72. CRasterObject **next; // The next object (one for each thread)
  73. int refCount; // The number of threads working on this object
  74. int diced; // TRUE if this object has already been diced
  75. int grid; // TRUE if this is a grid
  76. int cached; // TRUE if this is a cached object
  77. int xbound[2],ybound[2]; // The bound of the object on the screen, in samples
  78. float zmin; // The minimum z coordinate of the object (used for occlusion culling)
  79. TMutex mutex; // To secure the object
  80. };
  81. ///////////////////////////////////////////////////////////////////////
  82. // Class : CRasterGrid
  83. // Description : This class encapsulates a shading grid
  84. // line renderer
  85. // Comments :
  86. class CRasterGrid : public CRasterObject {
  87. public:
  88. float *vertices; // Array of vertices
  89. int *bounds; // The bound of the primitive (4 numbers per primitive)
  90. float *sizes; // The size of the primitive (only makes sense for points)
  91. EShadingDim dim; // Dimensionality (0,1 or 2)
  92. float umin,umax,vmin,vmax; // The parametric range
  93. int udiv,vdiv; // The number of division
  94. int numVertices; // The number of vertices
  95. int flags; // The primitive flags
  96. };
  97. ///////////////////////////////////////////////////////////////////////
  98. // Class : CPqueue
  99. // Description : Priority queue specifically written for CRasterObject
  100. // Comments : I could have used the template in containers.h, but this one allows me to defer objects directly
  101. class CPqueue {
  102. public:
  103. CPqueue(int ss=100) {
  104. stepSize = ss;
  105. maxItems = stepSize;
  106. numItems = 1;
  107. allItems = new CRasterObject*[maxItems];
  108. }
  109. ~CPqueue() {
  110. delete [] allItems;
  111. }
  112. void insert(CRasterObject *cObject) {
  113. int i,j;
  114. // Expand the buffer
  115. if (numItems >= maxItems) {
  116. CRasterObject **newItems;
  117. maxItems += stepSize;
  118. newItems = new CRasterObject*[maxItems+1];
  119. memcpy(newItems,allItems,numItems*sizeof(CRasterObject*));
  120. delete [] allItems;
  121. allItems = newItems;
  122. stepSize *= 2;
  123. }
  124. // Insert the item
  125. i = numItems++;
  126. j = i >> 1;
  127. while ((i > 1) && (cObject->zmin < allItems[j]->zmin)) {
  128. allItems[i] = allItems[j];
  129. i = j;
  130. j = i >> 1;
  131. }
  132. allItems[i] = cObject;
  133. }
  134. CRasterObject *get(TMutex &mutex) {
  135. int i = 1, j;
  136. CRasterObject *lItem,*cItem;
  137. osLock(mutex);
  138. if (numItems <= 1) {
  139. cItem = NULL;
  140. } else {
  141. cItem = allItems[1];
  142. numItems--;
  143. lItem = allItems[numItems];
  144. while (i <= numItems / 2) {
  145. j = 2 * i;
  146. if (j >= numItems) break;
  147. if ((j < (numItems-1)) && (allItems[j]->zmin > allItems[j+1]->zmin))
  148. j++;
  149. if (allItems[j]->zmin > lItem->zmin)
  150. break;
  151. allItems[i] = allItems[j];
  152. i = j;
  153. }
  154. allItems[i] = lItem;
  155. }
  156. if (cItem != NULL) osUnlock(mutex);
  157. return cItem;
  158. }
  159. CRasterObject **allItems; // Array of the heap
  160. int numItems,maxItems,stepSize; // Misc junk
  161. };
  162. ///////////////////////////////////////////////////////////////////////
  163. // Class : CBucket
  164. // Description : Holds a bucket
  165. // Comments :
  166. class CBucket {
  167. public:
  168. CBucket();
  169. ~CBucket();
  170. CRasterObject *objects; // The list of objects waiting to be rendered
  171. CPqueue *queue; // If this is not null, we're currently rendering this bucket
  172. };
  173. public:
  174. CReyes(int thread);
  175. ~CReyes();
  176. // This function is called to to render
  177. void renderingLoop();
  178. // The following functions must be overriden by the child rasterizer
  179. virtual void rasterBegin(int,int,int,int,int) = 0;
  180. virtual void rasterDrawPrimitives(CRasterGrid *) = 0;
  181. virtual void rasterEnd(float *,int) = 0;
  182. // The following can be called from the "dice" function to insert an object into the scene
  183. void drawObject(CObject *); // Draw an object
  184. void drawGrid(CSurface *,int,int,float,float,float,float); // Draw a grid
  185. void drawPoints(CSurface *,int); // Draw points (RiPoints)
  186. // Some stats
  187. int numGridsRendered;
  188. int numQuadsRendered;
  189. int numGridsShaded;
  190. int numGridsCreated;
  191. int numVerticesCreated;
  192. protected:
  193. float maxDepth; // The maximum opaque depth in the current bucket
  194. static int extraPrimitiveFlags; // These are the extra primitive flags
  195. static int numVertexSamples; // The number of samples per pixel
  196. void shadeGrid(CRasterGrid *,int); // Called by the child to force the shading of a grid
  197. virtual int probeArea(int *xbound,int *ybound, int bw, int bh, int bl, int bt, float zmin) {
  198. return TRUE;
  199. }
  200. private:
  201. void copyPoints(int,float **,float *,int); // Data movement (copy P only)
  202. void copySamples(int,float **,float *,int); // Data movement (copy the color + opacity + extra samples)
  203. void insertObject(CRasterObject *object); // Add an object into the system
  204. void insertGrid(CRasterGrid *,int); // Insert a grid into the correct bucket
  205. CRasterObject *newObject(CObject *); // Create a new object
  206. CRasterGrid *newGrid(CSurface *,int,int,int); // Create a new grid
  207. void deleteObject(CRasterObject *); // Delete an object (the object can also be a grid)
  208. void render(); // Render the current bucket
  209. void skip(); // Skip the current bucket
  210. CBucket ***buckets; // All buckets
  211. TMutex bucketMutex; // Controls the accesses to the buckets
  212. int bucketPixelLeft; // Left of the current bucket in pixels
  213. int bucketPixelTop; // Top of the current bucket in pixels
  214. int bucketPixelWidth; // Width of the current bucket in pixels
  215. int bucketPixelHeight; // Height of the current bucket in pixels
  216. int tbucketLeft; // Left of the current bucket in samples
  217. int tbucketTop; // Right of the current bucket in samples
  218. int tbucketRight;
  219. int tbucketBottom;
  220. ///////////////////////////////////////////////////////////////////////
  221. // Class : CReyes
  222. // Method : distance2samples
  223. // Description : Project distances into the sample space
  224. // Return Value :
  225. // Comments : (inline for speed)
  226. inline void distance2samples(int n,float *dist,float *P) {
  227. if(CRenderer::projection == OPTIONS_PROJECTION_PERSPECTIVE) {
  228. for (;n>0;n--,P+=3,dist++) {
  229. *dist = CRenderer::dSampledx*CRenderer::imagePlane*dist[0]/P[COMP_Z];
  230. }
  231. } else {
  232. for (;n>0;n--,P+=3,dist++) {
  233. *dist = CRenderer::dSampledx*dist[0];
  234. }
  235. }
  236. }
  237. ///////////////////////////////////////////////////////////////////////
  238. // Class : CReyes
  239. // Method : camera2samples
  240. // Description : Project from camera space into the sample space
  241. // Return Value :
  242. // Comments : (inline for speed)
  243. inline void camera2samples(int n,float *P) {
  244. if(CRenderer::projection == OPTIONS_PROJECTION_PERSPECTIVE) {
  245. for (;n>0;n--,P+=3) {
  246. P[COMP_X] = (CRenderer::imagePlane*P[COMP_X]/P[COMP_Z] - CRenderer::pixelLeft)*CRenderer::dSampledx;
  247. P[COMP_Y] = (CRenderer::imagePlane*P[COMP_Y]/P[COMP_Z] - CRenderer::pixelTop)*CRenderer::dSampledy;
  248. }
  249. } else {
  250. for (;n>0;n--,P+=3) {
  251. P[COMP_X] = (P[COMP_X] - CRenderer::pixelLeft)*CRenderer::dSampledx;
  252. P[COMP_Y] = (P[COMP_Y] - CRenderer::pixelTop)*CRenderer::dSampledy;
  253. }
  254. }
  255. }
  256. ///////////////////////////////////////////////////////////////////////
  257. // Class : CReyes
  258. // Method : camera2samples
  259. // Description : Project from camera space into the sample space
  260. // Return Value :
  261. // Comments : (inline for speed)
  262. inline void camera2samples(float *P) {
  263. if(CRenderer::projection == OPTIONS_PROJECTION_PERSPECTIVE) {
  264. P[COMP_X] = CRenderer::imagePlane*P[COMP_X]/P[COMP_Z];
  265. P[COMP_Y] = CRenderer::imagePlane*P[COMP_Y]/P[COMP_Z];
  266. }
  267. P[COMP_X] = (P[COMP_X] - CRenderer::pixelLeft)*CRenderer::dSampledx;
  268. P[COMP_Y] = (P[COMP_Y] - CRenderer::pixelTop)*CRenderer::dSampledy;
  269. }
  270. };
  271. #endif