Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

882 lines
34 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright (C) Microsoft Corporation, 1998.
  3. //
  4. // refrast.hpp
  5. //
  6. // Direct3D Reference Rasterizer - Main Header File
  7. //
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #ifndef _REFRAST_HPP
  10. #define _REFRAST_HPP
  11. // Leave this defined so various all the code compiles. However,
  12. // it will never be set, since it is a part of D3DFVF_RESERVED2 until
  13. // DX8.
  14. #define D3DFVF_S 0x1000
  15. #include <rrutil.hpp>
  16. #include <reftnl.hpp>
  17. #include <templarr.hpp>
  18. //-----------------------------------------------------------------------------
  19. //
  20. // Uncomment this to enable point sprites in the reference rasterizer
  21. //
  22. //-----------------------------------------------------------------------------
  23. //#define __POINTSPRITES 1
  24. //-----------------------------------------------------------------------------
  25. //
  26. // Uncomment this to enable shadowbuffer in the reference rasterizer
  27. //
  28. //-----------------------------------------------------------------------------
  29. //#define __SHADOWBUFFER 1
  30. //-----------------------------------------------------------------------------
  31. //
  32. // RefRastSetMemif - Routine to set memory allocation interface for reference
  33. // rasterizer - takes pointers to functions to use for malloc, free, and realloc.
  34. //
  35. // These must be set prior to new'ing any ReferenceRasterizer objects. These are
  36. // used for allocating rasterizer objects, and for allocation of the fragment
  37. // pointer array and fragment records used for sort-independent antialiasing and
  38. // transluscency.
  39. //
  40. //-----------------------------------------------------------------------------
  41. void
  42. RefRastSetMemif(
  43. LPVOID( _cdecl* pfnMemAlloc )( size_t ),
  44. void( _cdecl* pfnMemFree )( PVOID ),
  45. LPVOID( _cdecl* pfnMemReAlloc )( PVOID, size_t ) );
  46. //-----------------------------------------------------------------------------
  47. //
  48. // Surface types for rendering surfaces and textures. Different subsets are
  49. // supported for render targets and for textures.
  50. //
  51. //-----------------------------------------------------------------------------
  52. typedef enum _RRSurfaceType
  53. {
  54. RR_STYPE_NULL = 0,
  55. RR_STYPE_B8G8R8 = 1,
  56. RR_STYPE_B8G8R8A8 = 2,
  57. RR_STYPE_B8G8R8X8 = 3,
  58. RR_STYPE_B5G6R5 = 4,
  59. RR_STYPE_B5G5R5 = 5,
  60. RR_STYPE_PALETTE4 = 6,
  61. RR_STYPE_PALETTE8 = 7,
  62. RR_STYPE_B5G5R5A1 = 8,
  63. RR_STYPE_B4G4R4 = 9,
  64. RR_STYPE_B4G4R4A4 =10,
  65. RR_STYPE_L8 =11, // 8 bit luminance-only
  66. RR_STYPE_L8A8 =12, // 16 bit alpha-luminance
  67. RR_STYPE_U8V8 =13, // 16 bit bump map format
  68. RR_STYPE_U5V5L6 =14, // 16 bit bump map format with luminance
  69. RR_STYPE_U8V8L8 =15, // 24 bit bump map format with luminance
  70. RR_STYPE_UYVY =16, // UYVY format (PC98 compliance)
  71. RR_STYPE_YUY2 =17, // YUY2 format (PC98 compliance)
  72. RR_STYPE_DXT1 =18, // S3 texture compression technique 1
  73. RR_STYPE_DXT2 =19, // S3 texture compression technique 2
  74. RR_STYPE_DXT3 =20, // S3 texture compression technique 3
  75. RR_STYPE_DXT4 =21, // S3 texture compression technique 4
  76. RR_STYPE_DXT5 =22, // S3 texture compression technique 5
  77. RR_STYPE_B2G3R3 =23, // 8 bit RGB texture format
  78. RR_STYPE_L4A4 =24, // 8 bit alpha-luminance
  79. RR_STYPE_B2G3R3A8 =25, // 16 bit alpha-rgb
  80. RR_STYPE_Z16S0 =32,
  81. RR_STYPE_Z24S8 =33,
  82. RR_STYPE_Z15S1 =34,
  83. RR_STYPE_Z32S0 =35,
  84. RR_STYPE_S1Z15 =36,
  85. RR_STYPE_S8Z24 =37,
  86. RR_STYPE_Z24S4 =38,
  87. RR_STYPE_S4Z24 =39,
  88. } RRSurfaceType;
  89. //-----------------------------------------------------------------------------
  90. //
  91. // Constants
  92. //
  93. //-----------------------------------------------------------------------------
  94. #define SPRITETEXCOORDMAX (4095.75f/4096.0f)
  95. //-----------------------------------------------------------------------------
  96. //
  97. // forward declarations, mostly from refrasti.hpp
  98. //
  99. //-----------------------------------------------------------------------------
  100. class RRColorComp;
  101. class RRColor;
  102. class RRDepth;
  103. class RRPixel;
  104. class RRTexture;
  105. class RRTextureCoord;
  106. class RREnvTextureCoord;
  107. class RRFVFExtractor;
  108. typedef UINT16 RRCvgMask;
  109. typedef struct _RRFRAGMENT RRFRAGMENT;
  110. typedef struct _RRSCANCNVSTATE RRSCANCNVSTATE;
  111. typedef struct _RRSTATS RRSTATS;
  112. //-----------------------------------------------------------------------------
  113. //
  114. // RRRenderTarget - Class which encompasses all informatio about rendering
  115. // target, including size, type/pointer/stride for color and depth/stencil
  116. // buffers, guard band clip info, W range info.
  117. //
  118. // Usage is to instantiate, fill out public members, and install into a
  119. // ReferenceRasterizer object via ReferenceRasterizer::SetRenderTarget.
  120. //
  121. //-----------------------------------------------------------------------------
  122. class RRRenderTarget
  123. {
  124. public:
  125. ///////////////////////////////////////////////////////////////////////////
  126. //
  127. // public interface
  128. //
  129. ///////////////////////////////////////////////////////////////////////////
  130. RRRenderTarget( void );
  131. ~RRRenderTarget( void );
  132. static void* operator new( size_t );
  133. static void operator delete( void* pv, size_t );
  134. //
  135. // these need to be filled in by the user before installing in a
  136. // ReferenceRasterizer object
  137. //
  138. int m_iWidth; // size of target surfaces (color & depth/stencil
  139. int m_iHeight; // must be same size
  140. RECT m_Clip;
  141. FLOAT m_fWRange[2]; // range of device W (W at near and far clip planes)
  142. RRSurfaceType m_ColorSType;
  143. char* m_pColorBufBits;
  144. int m_iColorBufPitch;
  145. RRSurfaceType m_DepthSType;
  146. char* m_pDepthBufBits;
  147. int m_iDepthBufPitch;
  148. //
  149. // these are used only to facilitate the way refrast is used in the D3D runtime
  150. // and are not referenced within the refrast core
  151. //
  152. LPDDRAWI_DDRAWSURFACE_LCL m_pDDSLcl;
  153. LPDDRAWI_DDRAWSURFACE_LCL m_pDDSZLcl;
  154. ///////////////////////////////////////////////////////////////////////////
  155. //
  156. // internal state and methods
  157. //
  158. ///////////////////////////////////////////////////////////////////////////
  159. friend class ReferenceRasterizer;
  160. void ReadPixelColor ( INT32 iX, INT32 iY, RRColor& Color );
  161. void WritePixelColor ( INT32 iX, INT32 iY, const RRColor& Color, BOOL bDither );
  162. void WritePixelDepth ( INT32 iX, INT32 iY, const RRDepth& Depth );
  163. void ReadPixelDepth ( INT32 iX, INT32 iY, RRDepth& Depth );
  164. void WritePixelStencil( INT32 iX, INT32 iY, UINT8 uStencil );
  165. void ReadPixelStencil ( INT32 iX, INT32 iY, UINT8& uStencil );
  166. void Clear ( RRColor fillColor, LPD3DHAL_DP2COMMAND pCmd );
  167. void ClearDepth ( RRDepth fillDepth, LPD3DHAL_DP2COMMAND pCmd );
  168. void ClearStencil ( UINT8 uStencil, LPD3DHAL_DP2COMMAND pCmd );
  169. void ClearDepthStencil( RRDepth fillDepth, UINT8 uStencil, LPD3DHAL_DP2COMMAND pCmd );
  170. };
  171. //-----------------------------------------------------------------------------
  172. //
  173. // RRTextureStageState - This holds the per-stage state for texture mapping.
  174. // An array of these are instanced in the ReferenceRasterizer object.
  175. //
  176. // Store texture matrix at the end of the texture stage state.
  177. //
  178. //-----------------------------------------------------------------------------
  179. #define D3DTSSI_MATRIX (D3DTSS_MAX)
  180. class RRTextureStageState
  181. {
  182. public:
  183. union
  184. {
  185. DWORD m_dwVal[D3DTSS_MAX+16]; // state array (unsigned)
  186. FLOAT m_fVal[D3DTSS_MAX+16]; // state array (float)
  187. };
  188. RRTexture* m_pTexture;
  189. };
  190. //-----------------------------------------------------------------------------
  191. //
  192. // RRTexture - Class instanced once per texture which encompasses information
  193. // about a chain of surfaces used as a texture map. Includes size and type
  194. // (assumed same for each level of detail) and pointer/stride for each LOD.
  195. //
  196. // Also includes pointer to palette, and colorkey value (legacy support only).
  197. //
  198. // Usage is to create RRTexture (and associated handle) with call to
  199. // ReferenceRasterizer::TextureCreate and install in ReferenceRasterizer
  200. // by passing handle into ReferenceRasterizer::SetRenderState.
  201. //
  202. //-----------------------------------------------------------------------------
  203. #define RRTEX_MAXCLOD 12*6 // supports base texture up to 4kx4k
  204. // for 6 envmaps
  205. class RRTexture
  206. {
  207. public:
  208. ///////////////////////////////////////////////////////////////////////////
  209. //
  210. // public interface
  211. //
  212. ///////////////////////////////////////////////////////////////////////////
  213. RRTexture( void );
  214. ~RRTexture( void );
  215. static void* operator new( size_t );
  216. static void operator delete( void* pv, size_t );
  217. //
  218. // this needs to be called after changing any of the public state to validate
  219. // internal (private) state
  220. //
  221. BOOL Validate( void );
  222. //
  223. // these need to be filled in by the user before installing in a
  224. // ReferenceRasterizer object
  225. //
  226. DWORD m_uFlags; // RR_TEXTURE_* bitdefs
  227. // bit definitions for RRTexture::uFlags
  228. #define RR_TEXTURE_HAS_CK (1L<< 0) // set if texture has colorkey
  229. #define RR_TEXTURE_LOCKED (1L<< 1) // set if DD surface is locked (external use only)
  230. #define RR_TEXTURE_ALPHAINPALETTE (1L<< 2) // set if alpha channel in palette
  231. #define RR_TEXTURE_ENVMAP (1L<< 3) // set if texture is envmap with 6 times
  232. // the usual number of surfaces
  233. #define RR_TEXTURE_SHADOWMAP (1L<< 4) // set if the texture is a ZBuffer
  234. // basic info
  235. RRSurfaceType m_SurfType; // format of pixel
  236. int m_iWidth; // size of largest map
  237. int m_iHeight;
  238. char* m_pTextureBits[RRTEX_MAXCLOD]; // pointer to surface bits
  239. int m_iPitch[RRTEX_MAXCLOD]; // pitch in bytes
  240. int m_cLOD; // 0..(n-1) count of LODs currently available
  241. DWORD m_dwColorKey; // D3DCOLOR colorkey value
  242. DWORD m_dwEmptyFaceColor; // D3DCOLOR empty cubemap empty face value
  243. DWORD* m_pPalette; // pointer to D3DCOLOR palette (may be NULL)
  244. //
  245. // these are used only to facilitate the way refrast is used in the D3D runtime
  246. // and are not referenced within the refrast core
  247. //
  248. // DD surface pointers for locking/unlocking and GetSurf callback
  249. LPDDRAWI_DDRAWSURFACE_LCL m_pDDSLcl[RRTEX_MAXCLOD];
  250. int m_cLODDDS; // 0..(n-1) count of LODs actually in the pDDS array
  251. //
  252. // may be useful to other users to have this public
  253. //
  254. D3DTEXTUREHANDLE m_hTex; // texture handle
  255. ///////////////////////////////////////////////////////////////////////////
  256. //
  257. // internal state and methods
  258. //
  259. ///////////////////////////////////////////////////////////////////////////
  260. friend class ReferenceRasterizer;
  261. // pointer to head of texture stage states, &m_TextureStageState[0]
  262. RRTextureStageState* m_pStageState;
  263. // texture.cpp - main interface methods used by ReferenceRasterizer object methods
  264. void DoLookupAndFilter( INT32 iStage, RRTextureCoord, RRColor& TextureColor );
  265. void DoBumpMapping( INT32 iStage, RRTextureCoord TCoord,
  266. FLOAT& fBumpMapUDelta, FLOAT& fBumpMapVDelta, RRColor& BumpMapModulate );
  267. void DoShadow(INT32 iStage, FLOAT* pfCoord, RRColor& OutputColor);
  268. // environment mapping versions
  269. void DoEnvProcessNormal( INT32 iStage, RREnvTextureCoord, RRColor& TextureColor );
  270. void DoEnvLookupAndFilter(INT32 iStage, INT16 iFace, FLOAT fMajor, FLOAT fDMDX, FLOAT fDMDY, RRTextureCoord TCoord, RRColor& TextureColor);
  271. void DoEnvReMap(INT16 iU, INT16 iV, INT16 iUMask, INT16 iVMask, INT16 iFace, INT16 iLOD, RRColor &Texel,
  272. BOOL &bColorKeyMatched);
  273. void DoTableInterp(INT16 iU, INT16 iV, INT16 iUMask, INT16 iVMask, INT16 iFace, INT16 iLOD,
  274. UINT8 uUSign, UINT8 uVSign, RRColor &Texel, BOOL &bColorKeyMatched);
  275. BOOL m_bMipMapEnable; // TRUE if mipmapping is enabled for this texture
  276. INT16 m_iTexSize[2]; // LOD 0 size
  277. INT16 m_iTexShift[2]; // LOD 0 log2 size (valid for power-of-2 size only)
  278. UINT16 m_uTexMask[2]; // LOD 0 (1<<log2(size))-1
  279. BOOL m_bHasAlpha; // TRUE if texture has an alpha channel
  280. BOOL m_bDoColorKeyKill; // TRUE is colorkey enabled for this texture and should kill pixel
  281. BOOL m_bDoColorKeyZero; // TRUE is colorkey enabled for this texture and should zero pixel
  282. BOOL m_bColorKeyMatched; // TRUE if colorkey matched on one or more contributing samples
  283. // texture.cpp
  284. HRESULT Initialize( LPDDRAWI_DDRAWSURFACE_LCL pLcl );
  285. RRColor DoMapLookupLerp(INT32 iStage, INT32 iU, INT32 iV, INT16 iLOD);
  286. RRColor DoMapLookupNearest(INT32 iStage, INT32 iU, INT32 iV, INT16 iLOD, BOOL &bColorKeyMatched);
  287. RRColor DoLookup(INT32 iStage, float U, float V, INT16 iLOD, BOOL bNearest);
  288. void DoMagnify ( INT32 iStage, RRTextureCoord& TCoord, RRColor& Texel );
  289. void DoMinify ( INT32 iStage, RRTextureCoord& TCoord, INT16 iLOD, RRColor& Texel );
  290. void DoTrilerp ( INT32 iStage, RRTextureCoord& TCoord, INT16 iLOD, RRColor& Texel );
  291. void DoAniso ( INT32 iStage, RRTextureCoord& TCoord, INT16 iLOD, FLOAT fRatio, FLOAT fDelta[], RRColor& Texel );
  292. // environment mapping versions
  293. RRColor DoEnvLookup(INT32 iStage, RRTextureCoord TCoord, INT16 iFace, INT16 iLOD, BOOL bNearest);
  294. void DoEnvMagnify ( INT32 iStage, RRTextureCoord& TCoord, INT16 iFace, RRColor& Texel );
  295. void DoEnvMinify ( INT32 iStage, RRTextureCoord& TCoord, INT16 iFace, INT16 iLOD, RRColor& Texel );
  296. void DoEnvTrilerp ( INT32 iStage, RRTextureCoord& TCoord, INT16 iFace, INT16 iLOD, RRColor& Texel );
  297. void DoTextureTransform( INT32 iStage, BOOL bAlreadyXfmd, FLOAT* pfC, FLOAT* pfO, FLOAT* pfQ );
  298. // texmap.cpp
  299. void ReadColor(
  300. INT32 iX, INT32 iY, INT32 iLOD,
  301. RRColor& Texel, BOOL &bColorKeyMatched );
  302. };
  303. #define REF_STATESET_GROWDELTA 1
  304. #define RRSTATEOVERRIDE_DWORD_BITS 32
  305. #define RRSTATEOVERRIDE_DWORD_SHIFT 5
  306. typedef TemplArray<UINT8> StateSetData;
  307. typedef StateSetData *LPStateSetData;
  308. class ReferenceRasterizer;
  309. typedef HRESULT (*PFN_DP2REFOPERATION)(ReferenceRasterizer *pRefRast, LPD3DHAL_DP2COMMAND pCmd);
  310. typedef HRESULT (*PFN_DP2REFSETRENDERSTATES)(ReferenceRasterizer *pRefRast,
  311. DWORD dwFvf,
  312. LPD3DHAL_DP2COMMAND pCmd,
  313. LPDWORD lpdwRuntimeRStates);
  314. typedef HRESULT (*PFN_DP2REFTEXTURESTAGESTATE)(ReferenceRasterizer *pRefRast,
  315. DWORD dwFvf,
  316. LPD3DHAL_DP2COMMAND pCmd);
  317. typedef HRESULT (*PFN_DP2REFSETLIGHT)(ReferenceRasterizer *pRefRast,
  318. LPD3DHAL_DP2COMMAND pCmd,
  319. LPDWORD pdwStride);
  320. typedef struct _REF_STATESETFUNCTIONTBL
  321. {
  322. DWORD dwSize; // size of struct
  323. PFN_DP2REFSETRENDERSTATES pfnDp2SetRenderStates;
  324. PFN_DP2REFTEXTURESTAGESTATE pfnDp2TextureStageState;
  325. PFN_DP2REFOPERATION pfnDp2SetViewport;
  326. PFN_DP2REFOPERATION pfnDp2SetWRange;
  327. PFN_DP2REFOPERATION pfnDp2SetMaterial;
  328. PFN_DP2REFOPERATION pfnDp2SetZRange;
  329. PFN_DP2REFSETLIGHT pfnDp2SetLight;
  330. PFN_DP2REFOPERATION pfnDp2CreateLight;
  331. PFN_DP2REFOPERATION pfnDp2SetTransform;
  332. PFN_DP2REFOPERATION pfnDp2SetExtention;
  333. PFN_DP2REFOPERATION pfnDp2SetClipPlane;
  334. } REF_STATESETFUNCTIONTBL, *LPREF_STATESETFUNCTIONTBL;
  335. //
  336. // The device type that the RefRast should emulate
  337. //
  338. typedef enum {
  339. RRTYPE_OLDHAL = 1,
  340. RRTYPE_DPHAL,
  341. RRTYPE_DP2HAL, // DX6 HAL
  342. RRTYPE_DX7HAL, // DX7 HAL w/out T&L, with state sets
  343. RRTYPE_DX7TLHAL
  344. } RRDEVICETYPE;
  345. typedef struct _RRSTATEOVERRIDES
  346. {
  347. DWORD bits[D3DSTATE_OVERRIDE_BIAS >> RRSTATEOVERRIDE_DWORD_SHIFT];
  348. } RRSTATEOVERRIDES;
  349. //-----------------------------------------------------------------------------
  350. //
  351. // ReferenceRasterizer - Primary object for reference rasterizer. Each instance
  352. // of this corresponds to a D3D device.
  353. //
  354. // Usage is to instantiate, install RRRenderTarget (and optional RRTexture's),
  355. // and set state and draw primitives.
  356. //
  357. //-----------------------------------------------------------------------------
  358. class ReferenceRasterizer : public RRProcessVertices
  359. {
  360. public:
  361. ///////////////////////////////////////////////////////////////////////////
  362. //
  363. // public interface
  364. //
  365. ///////////////////////////////////////////////////////////////////////////
  366. ReferenceRasterizer( LPDDRAWI_DIRECTDRAW_LCL pDDLcl,
  367. DWORD dwInterfaceType,
  368. RRDEVICETYPE dwDriverType
  369. );
  370. ~ReferenceRasterizer( void );
  371. static void* operator new( size_t );
  372. static void operator delete( void* pv, size_t );
  373. // Dp2 token handling functions
  374. HRESULT Dp2RecRenderStates(DWORD dwFvf, LPD3DHAL_DP2COMMAND pCmd, LPDWORD lpdwRuntimeRStates);
  375. HRESULT Dp2RecTextureStageState(DWORD dwFvf, LPD3DHAL_DP2COMMAND pCmd);
  376. HRESULT Dp2RecViewport(LPD3DHAL_DP2COMMAND pCmd);
  377. HRESULT Dp2RecWRange(LPD3DHAL_DP2COMMAND pCmd);
  378. HRESULT Dp2RecMaterial(LPD3DHAL_DP2COMMAND pCmd);
  379. HRESULT Dp2RecZRange(LPD3DHAL_DP2COMMAND pCmd);
  380. HRESULT Dp2RecSetLight(LPD3DHAL_DP2COMMAND pCmd, LPDWORD pdwStride);
  381. HRESULT Dp2RecCreateLight(LPD3DHAL_DP2COMMAND pCmd);
  382. HRESULT Dp2RecTransform(LPD3DHAL_DP2COMMAND pCmd);
  383. HRESULT Dp2RecExtention(LPD3DHAL_DP2COMMAND pCmd);
  384. HRESULT Dp2RecClipPlane(LPD3DHAL_DP2COMMAND pCmd);
  385. HRESULT Dp2SetRenderStates(DWORD dwFvf, LPD3DHAL_DP2COMMAND pCmd, LPDWORD lpdwRuntimeRStates);
  386. HRESULT Dp2SetTextureStageState(DWORD dwFvf, LPD3DHAL_DP2COMMAND pCmd);
  387. HRESULT Dp2SetViewport(LPD3DHAL_DP2COMMAND pCmd);
  388. HRESULT Dp2SetWRange(LPD3DHAL_DP2COMMAND pCmd);
  389. HRESULT Dp2SetMaterial(LPD3DHAL_DP2COMMAND pCmd);
  390. HRESULT Dp2SetZRange(LPD3DHAL_DP2COMMAND pCmd);
  391. HRESULT Dp2SetLight(LPD3DHAL_DP2COMMAND pCmd, PDWORD pdwStride);
  392. HRESULT Dp2CreateLight(LPD3DHAL_DP2COMMAND pCmd);
  393. HRESULT Dp2SetTransform(LPD3DHAL_DP2COMMAND pCmd);
  394. HRESULT Dp2SetExtention(LPD3DHAL_DP2COMMAND pCmd);
  395. HRESULT Dp2SetRenderTarget(LPD3DHAL_DP2COMMAND pCmd);
  396. HRESULT Dp2SetClipPlane(LPD3DHAL_DP2COMMAND pCmd);
  397. // StateSet related functions
  398. void SetRecStateFunctions(void);
  399. void SetSetStateFunctions(void);
  400. HRESULT BeginStateSet(DWORD dwHandle);
  401. HRESULT EndStateSet(void);
  402. HRESULT ExecuteStateSet(DWORD dwHandle);
  403. HRESULT DeleteStateSet(DWORD dwHandle);
  404. HRESULT CaptureStateSet(DWORD dwHandle);
  405. HRESULT RecordStates(PUINT8 pData, DWORD dwSize);
  406. HRESULT RecordLastState(LPD3DHAL_DP2COMMAND pCmd, DWORD dwUnitSize);
  407. LPREF_STATESETFUNCTIONTBL pStateSetFuncTbl;
  408. // Interface style
  409. BOOL IsInterfaceDX6AndBefore() {return (m_dwInterfaceType <= 2);}
  410. // DriverStyle
  411. BOOL IsDriverDX6AndBefore()
  412. {
  413. return ((m_dwDriverType <= RRTYPE_DP2HAL) && (m_dwDriverType > 0));
  414. }
  415. // Last State hack
  416. void StoreLastPixelState(BOOL bStore);
  417. // state management functions
  418. void SetRenderTarget( RRRenderTarget* pRenderTarget );
  419. RRRenderTarget* GetRenderTarget( void );
  420. void SetRenderState( DWORD dwState, DWORD dwValue );
  421. DWORD* GetRenderState( void );
  422. DWORD* GetTextureStageState(DWORD dwStage);
  423. void SetTextureStageState( DWORD dwStage, DWORD dwStageState, DWORD dwValue );
  424. void SceneCapture( DWORD dwFlags );
  425. // texture management functions
  426. BOOL TextureCreate ( LPD3DTEXTUREHANDLE phTex, RRTexture** ppTexture );
  427. BOOL TextureCreate ( DWORD dwHandle, RRTexture** ppTex );
  428. BOOL TextureDestroy ( D3DTEXTUREHANDLE hTex );
  429. DWORD TextureGetSurf( D3DTEXTUREHANDLE hTex );
  430. // rendering functions
  431. HRESULT Clear(LPD3DHAL_DP2COMMAND pCmd);
  432. HRESULT BeginRendering( DWORD dwFVFControl );
  433. HRESULT EndRendering( void );
  434. BOOL DoAreaCalcs(FLOAT* pfDet, RRFVFExtractor* pVtx0,
  435. RRFVFExtractor* pVtx1, RRFVFExtractor* pVtx2);
  436. void DoTexCoordCalcs(INT32 iStage, RRFVFExtractor* pVtx0,
  437. RRFVFExtractor* pVtx1, RRFVFExtractor* pVtx2);
  438. void DrawTriangle( void* pvV0, void* pvV1, void* pvV2,
  439. WORD wFlags = D3DTRIFLAG_EDGEENABLETRIANGLE );
  440. void DrawLine( void* pvV0, void* pvV1, void* pvVFlat = NULL );
  441. void DrawPoint( void* pvV0, void* pvVFlat = NULL );
  442. void DrawClippedTriangle( void* pvV0, RRCLIPCODE c0,
  443. void* pvV1, RRCLIPCODE c1,
  444. void* pvV2, RRCLIPCODE c2,
  445. WORD wFlags = D3DTRIFLAG_EDGEENABLETRIANGLE );
  446. void DrawClippedLine( void* pvV0, RRCLIPCODE c0,
  447. void* pvV1, RRCLIPCODE c1,
  448. void* pvVFlat = NULL );
  449. void DrawClippedPoint( void* pvV0, RRCLIPCODE c0,
  450. void* pvVFlat = NULL );
  451. //
  452. // these are used to facilitate the way refrast is used in the D3D runtime
  453. //
  454. // functions to manipulate current set of texture
  455. int GetCurrentTextureMaps( D3DTEXTUREHANDLE* phTex, RRTexture** pTex );
  456. BOOL SetTextureMap( D3DTEXTUREHANDLE hTex, RRTexture* pTex );
  457. //
  458. // T&L Hal specific functions
  459. //
  460. // For Non-Indexed Primitives
  461. void SavePrimitiveData( DWORD dwFVFIn, LPVOID pVtx,
  462. UINT cVertices,
  463. D3DPRIMITIVETYPE PrimType );
  464. // For Indexed Primitives
  465. void SavePrimitiveData( DWORD dwFVFIn, LPVOID pVtx, UINT cVertices,
  466. D3DPRIMITIVETYPE PrimType,
  467. LPWORD pIndices, UINT cIndices);
  468. HRESULT ProcessPrimitive( BOOL bIndexedPrim );
  469. HRESULT UpdateTLState();
  470. int ClipSingleLine( RRCLIPTRIANGLE *line );
  471. int ClipSingleTriangle(RRCLIPTRIANGLE *tri,
  472. RRCLIPVTX ***clipVertexPointer);
  473. //
  474. // Texture locking and Unlocking control
  475. //
  476. inline BOOL TexturesAreLocked() {return m_bTexturesAreLocked;};
  477. inline void SetTexturesLocked() {m_bTexturesAreLocked = TRUE;};
  478. inline void ClearTexturesLocked() {m_bTexturesAreLocked = FALSE;};
  479. private:
  480. ///////////////////////////////////////////////////////////////////////////
  481. //
  482. // internal state and methods
  483. //
  484. ///////////////////////////////////////////////////////////////////////////
  485. //-------------------------------------------------------------------------
  486. // state
  487. //-------------------------------------------------------------------------
  488. // DDraw Local, needed for the new texture handles from DX7 onwards
  489. LPDDRAWI_DIRECTDRAW_LCL m_pDDLcl;
  490. // This is obtained from CONTEXTCREATE->ddrval, indicates
  491. // what kind of emulation (DX3, DX5, DX6 or DX7) the driver should do.
  492. RRDEVICETYPE m_dwDriverType;
  493. // This is obtained from CONTEXTCREATE->dwhContext, indicates
  494. // which D3D Device interface called the driver.
  495. DWORD m_dwInterfaceType;
  496. // save area for floating point unit control
  497. WORD m_wSaveFP;
  498. // TRUE if in begin/end primitive sequence
  499. BOOL m_bInBegin;
  500. // TRUE if in rendering point sprite triangles
  501. BOOL m_bPointSprite;
  502. // current Flexible Vertex Format control word
  503. UINT64 m_qwFVFControl;
  504. // render target (color & Z buffer)
  505. RRRenderTarget* m_pRenderTarget;
  506. FLOAT m_fWBufferNorm[2]; // { Wnear, 1/(Wfar-Wnear) } to normalize W buffer value
  507. // fragment buffer
  508. RRFRAGMENT** m_ppFragBuf;
  509. INT m_iFragBufWidth;
  510. INT m_iFragBufHeight;
  511. BOOL m_bFragmentProcessingEnabled;
  512. // D3D renderstate
  513. union
  514. {
  515. DWORD m_dwRenderState[D3DHAL_MAX_RSTATES];
  516. FLOAT m_fRenderState[D3DHAL_MAX_RSTATES];
  517. };
  518. // State Override flags
  519. RRSTATEOVERRIDES m_renderstate_override;
  520. // texture state - per-stage state and pointer to associated texture
  521. int m_cActiveTextureStages; // count of active texture stages (range 0..D3DHAL_TSS_MAXSTAGES)
  522. RRTextureStageState m_TextureStageState[D3DHAL_TSS_MAXSTAGES];
  523. RRTexture* m_pTexture[D3DHAL_TSS_MAXSTAGES]; // texture maps associated with texture stages
  524. // DX7 style texture objects, where the handle is specified by the
  525. // runtime. It is basically an index into an array.
  526. // This array is dynamically allocated and grown very much like the light
  527. // array.
  528. RRTexture** m_ppTextureArray;
  529. DWORD m_dwTexArrayLength;
  530. // scan converter state
  531. RRSCANCNVSTATE* m_pSCS;
  532. // statistics
  533. RRSTATS* m_pStt;
  534. // Last state
  535. DWORD m_LastState;
  536. // Array of StateSets, which are in turn implemented with TemplArray as
  537. // TemplArray<UINT8> StateSetData
  538. TemplArray<LPStateSetData> m_pStateSets;
  539. // This bool indicates that the textures are already locked
  540. BOOL m_bTexturesAreLocked;
  541. //-------------------------------------------------------------------------
  542. // methods
  543. //-------------------------------------------------------------------------
  544. // refrasti.cpp
  545. HRESULT GrowTexArray( DWORD dwHandle );
  546. HRESULT SetTextureHandle( int iStage, DWORD dwHandle );
  547. void MapTextureHandleToDevice( int iStage );
  548. void UpdateActiveTexStageCount( void );
  549. RRTexture* MapHandleToTexture( D3DTEXTUREHANDLE hTex );
  550. // MapLegcy.cpp
  551. void MapLegacyTextureBlend( void );
  552. void MapLegacyTextureFilter( void );
  553. // setup.cpp
  554. void SetPrimitiveAttributeFunctions(
  555. const RRFVFExtractor& Vtx0,
  556. const RRFVFExtractor& Vtx1,
  557. const RRFVFExtractor& Vtx2,
  558. const RRFVFExtractor& VtxFlat );
  559. // scancnv.cpp
  560. FLOAT ComputePixelAttrib( int iAttrib );
  561. FLOAT ComputePixelAttribClamp( int iAttrib );
  562. FLOAT ComputePixelAttribTex( int iTex, int iCrd );
  563. void ComputeFogIntensity( RRPixel& Pixel );
  564. void DoScanCnvGenPixel( RRCvgMask CvgMask, BOOL bTri );
  565. void DoScanCnvTri( int iEdgeCount );
  566. void DoScanCnvLine( void );
  567. // texstage.cpp
  568. void DoTexture( const RRPixel& Pixel, RRColor& OutputColor);
  569. void ComputeTextureBlendArg(
  570. DWORD dwArgCtl, BOOL bAlphaOnly,
  571. const RRColor& DiffuseColor,
  572. const RRColor& SpecularColor,
  573. const RRColor& CurrentColor,
  574. const RRColor& TextureColor,
  575. RRColor& BlendArg);
  576. void DoTextureBlendStage(
  577. int iStage,
  578. const RRColor& DiffuseColor,
  579. const RRColor& SpecularColor,
  580. const RRColor& CurrentColor,
  581. const RRColor& TextureColor,
  582. RRColor& OutputColor);
  583. // pixproc.cpp
  584. BOOL DepthCloser( const RRDepth& DepthVal, const RRDepth& DepthBuf );
  585. BOOL AlphaTest( const RRColorComp& Alpha );
  586. BOOL DoStencil( UINT8 uStncBuf, BOOL bDepthTest, RRSurfaceType DepthSType, UINT8& uStncRet );
  587. void DoAlphaBlend( const RRColor& SrcColor, const RRColor& DstColor,
  588. RRColor& ResColor );
  589. void DoPixel( RRPixel& Pixel );
  590. // fragproc.cpp
  591. BOOL DoFragmentGenerationProcessing( RRPixel& Pixel );
  592. void DoFragmentBufferFixup( const RRPixel& Pixel );
  593. RRFRAGMENT* FragAlloc( void );
  594. void FragFree( RRFRAGMENT* pFrag );
  595. // fragrslv.cpp
  596. void DoBufferResolve( void);
  597. void DoFragResolve(
  598. RRColor& ResolvedColor, RRDepth& ResolvedDepth,
  599. RRFRAGMENT* pFragList,
  600. const RRColor& PixelColor );
  601. // PixRef.cpp
  602. void WritePixel(
  603. INT32 iX, INT32 iY,
  604. const RRColor& Color, const RRDepth& Depth);
  605. // primfns.cpp
  606. void SetXfrm( D3DTRANSFORMSTATETYPE xfrmType, D3DMATRIX *pMat );
  607. HRESULT GrowLightArray(const DWORD dwIndex);
  608. };
  609. //-----------------------------------------------------------------------------
  610. //
  611. // RRFVFExtractor - Encases Flexible Vertex Format pointer and control to get
  612. // vertex data.
  613. //
  614. //-----------------------------------------------------------------------------
  615. #ifndef D3DFVF_GETTEXCOORDSIZE
  616. #define D3DFVF_GETTEXCOORDSIZE(FVF, CoordIndex) ((FVF >> (CoordIndex*2 + 16)) & 0x3)
  617. #endif
  618. class RRFVFExtractor
  619. {
  620. private:
  621. void* m_pvData;
  622. UINT64 m_qwControl;
  623. BOOL m_bPerspectiveEnable;
  624. int m_iXYZ;
  625. int m_iDiffuse;
  626. int m_iSpecular;
  627. int m_iTexCrd[D3DHAL_TSS_MAXSTAGES+1];
  628. int m_iS;
  629. int m_iEyeNormal;
  630. int m_iEyeXYZ;
  631. public:
  632. // constructor
  633. RRFVFExtractor( void* pvData, UINT64 qwControl, BOOL bPerspectiveEnable )
  634. {
  635. m_pvData = pvData;
  636. m_qwControl = qwControl;
  637. m_bPerspectiveEnable = bPerspectiveEnable;
  638. // compute offsets to fields within FVF
  639. m_iXYZ = 0 +
  640. ( m_qwControl & D3DFVF_RESERVED0 ? 1 : 0 );
  641. m_iDiffuse = m_iXYZ +
  642. ( m_qwControl & D3DFVF_XYZ ? 3 : 0 ) +
  643. ( m_qwControl & D3DFVF_XYZRHW ? 4 : 0 ) +
  644. ( m_qwControl & D3DFVF_NORMAL ? 3 : 0 ) +
  645. ( m_qwControl & D3DFVF_RESERVED1 ? 1 : 0 );
  646. m_iSpecular = m_iDiffuse +
  647. ( m_qwControl & D3DFVF_DIFFUSE ? 1 : 0 );
  648. m_iTexCrd[0] = m_iSpecular +
  649. ( m_qwControl & D3DFVF_SPECULAR ? 1 : 0 );
  650. m_iS = m_iTexCrd[0];
  651. for(int i = 0; i < TexCrdCount(); i++)
  652. {
  653. int iTexND;
  654. switch (D3DFVF_GETTEXCOORDSIZE(m_qwControl, i))
  655. {
  656. case D3DFVF_TEXTUREFORMAT2: iTexND = 2; break;
  657. case D3DFVF_TEXTUREFORMAT3: iTexND = 3; break;
  658. case D3DFVF_TEXTUREFORMAT4: iTexND = 4; break;
  659. case D3DFVF_TEXTUREFORMAT1: iTexND = 1; break;
  660. }
  661. m_iTexCrd[i+1] = m_iTexCrd[i] + iTexND;
  662. m_iS = m_iTexCrd[i+1]; // generate one more iTexCrd pointer than
  663. // needed, and use it for size
  664. }
  665. m_iEyeNormal = m_iS +
  666. ( m_qwControl & D3DFVF_S ? 1 : 0 );
  667. m_iEyeXYZ = m_iEyeNormal +
  668. ( m_qwControl & D3DFVFP_EYENORMAL ? 3 : 0 );
  669. }
  670. // coordinate access methods
  671. FLOAT* GetPtrXYZ( void ) const { return (FLOAT*)m_pvData + m_iXYZ; }
  672. FLOAT GetX( void ) const { return *( GetPtrXYZ() + 0 ); }
  673. FLOAT GetY( void ) const { return *( GetPtrXYZ() + 1 ); }
  674. FLOAT GetZ( void ) const { return *( GetPtrXYZ() + 2 ); }
  675. FLOAT GetRHW( void ) const
  676. {
  677. // return 1. if perspective not enabled
  678. if ( !m_bPerspectiveEnable ) return 1.f;
  679. // return 1/W if available else default value 1.0
  680. return ( m_qwControl & D3DFVF_XYZRHW )
  681. ? *( GetPtrXYZ() + 3 )
  682. : 1.f ;
  683. }
  684. // color access methods
  685. DWORD GetDiffuse( void ) const
  686. {
  687. // return color if available else white (default)
  688. return ( m_qwControl & D3DFVF_DIFFUSE )
  689. ? *( (DWORD*)m_pvData + m_iDiffuse )
  690. : 0xffffffff;
  691. }
  692. DWORD GetSpecular( void ) const
  693. {
  694. // return color if available else black and zero vertex fog (default)
  695. return ( m_qwControl & D3DFVF_SPECULAR )
  696. ? *( (DWORD*)m_pvData + m_iSpecular )
  697. : 0x00000000;
  698. }
  699. // texture coordinate access methods
  700. int TexCrdCount( void ) const
  701. {
  702. return (int)(( m_qwControl & D3DFVF_TEXCOUNT_MASK ) >> D3DFVF_TEXCOUNT_SHIFT);
  703. }
  704. FLOAT GetTexCrd( int iCrd, int iCrdSet ) const
  705. {
  706. return ( (TexCrdCount() > iCrdSet) && (iCrd < 4) )
  707. ? *( (FLOAT*)m_pvData + m_iTexCrd[iCrdSet] + iCrd )
  708. : 0.f;
  709. }
  710. FLOAT* GetPtrTexCrd( int iCrd, int iCrdSet ) const
  711. {
  712. return (FLOAT*)m_pvData + m_iTexCrd[iCrdSet] + iCrd;
  713. }
  714. FLOAT GetS( void ) const
  715. {
  716. return ( m_qwControl & D3DFVF_S )
  717. ? *( (FLOAT*)m_pvData + m_iS )
  718. : 1.0f;
  719. }
  720. FLOAT* GetPtrS( void ) const { return (FLOAT*)m_pvData + m_iS; }
  721. FLOAT GetEyeNormal( int iCrd ) const
  722. {
  723. return (m_qwControl & D3DFVFP_EYENORMAL)
  724. ? *( (FLOAT*)m_pvData + m_iEyeNormal + iCrd)
  725. : 0.f;
  726. }
  727. FLOAT* GetPtrEyeNormal( void ) const { return (FLOAT*)m_pvData + m_iEyeNormal; }
  728. FLOAT GetEyeXYZ( int iCrd ) const
  729. {
  730. return (m_qwControl & D3DFVFP_EYEXYZ)
  731. ? *( (FLOAT*)m_pvData + m_iEyeXYZ + iCrd)
  732. : 0.f;
  733. }
  734. FLOAT* GetPtrEyeXYZ( void ) const { return (FLOAT*)m_pvData + m_iEyeXYZ; }
  735. };
  736. //-------------------------------------------------------------------------
  737. // S3 compressed texture formats
  738. //-------------------------------------------------------------------------
  739. // number of s3 compression formats
  740. #define NUM_DXT_FORMATS 5
  741. // number of pixels in block
  742. #define DXT_BLOCK_PIXELS 16
  743. typedef struct {
  744. BYTE rgba[4];
  745. } DXT_COLOR;
  746. typedef WORD RGB565; // packed color
  747. typedef DWORD PIXBM; // 2 BPP bitmap
  748. typedef struct {
  749. RGB565 rgb0; // color for index 0
  750. RGB565 rgb1; // color for index 1
  751. PIXBM pixbm; // pixel bitmap
  752. } DXTBlockRGB;
  753. typedef struct {
  754. WORD alphabm[4]; // alpha bitmap at 4 BPP
  755. DXTBlockRGB rgb; // color block
  756. } DXTBlockAlpha4;
  757. typedef struct {
  758. BYTE alpha0; // alpha for index 0
  759. BYTE alpha1; // alpha for index 1
  760. BYTE alphabm[6]; // alpha bitmap at 3 BPP
  761. DXTBlockRGB rgb; // color block
  762. } DXTBlockAlpha3;
  763. void DecodeBlockRGB (DXTBlockRGB *pblockSrc,
  764. DXT_COLOR colorDst[DXT_BLOCK_PIXELS]);
  765. void DecodeBlockAlpha4(DXTBlockAlpha4 *pblockSrc,
  766. DXT_COLOR colorDst[DXT_BLOCK_PIXELS]);
  767. void DecodeBlockAlpha3(DXTBlockAlpha3 *pblockSrc,
  768. DXT_COLOR colorDst[DXT_BLOCK_PIXELS]);
  769. ///////////////////////////////////////////////////////////////////////////////
  770. #endif // _REFRAST_HPP