MLRState.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. //===========================================================================//
  2. // Copyright (C) Microsoft Corporation. All rights reserved. //
  3. //===========================================================================//
  4. #pragma once
  5. #define MLR_MLRSTATE_HPP
  6. #if !defined(MLR_MLR_HPP)
  7. #include <MLR\MLR.hpp>
  8. #endif
  9. namespace MidLevelRenderer {class MLRState;}
  10. namespace GetHashFunctions {
  11. Stuff::IteratorPosition
  12. GetHashValue(const MidLevelRenderer::MLRState &value);
  13. }
  14. namespace MidLevelRenderer {
  15. class MLRSorter;
  16. class MLRTexturePool;
  17. //##########################################################################
  18. //######################## MLRState ###############################
  19. //##########################################################################
  20. class MLRState
  21. #if defined(_ARMOR)
  22. : public Stuff::Signature
  23. #endif
  24. {
  25. friend class MLRSorter;
  26. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  27. // Constructors/Destructors
  28. //
  29. protected:
  30. MLRState(
  31. Stuff::MemoryStream *stream,
  32. int version
  33. );
  34. public:
  35. MLRState();
  36. MLRState(const MLRState&);
  37. ~MLRState() {};
  38. static MLRState*
  39. Make(
  40. Stuff::MemoryStream *stream,
  41. int version
  42. );
  43. void
  44. Save(Stuff::MemoryStream *stream);
  45. void
  46. Load(
  47. Stuff::MemoryStream *stream,
  48. int version
  49. );
  50. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  51. // Render state
  52. //
  53. public:
  54. enum {
  55. TextureNumberBit = 0,
  56. TextureNumberBits = Limits::Max_Number_Of_Texture_Bits,
  57. TextureMask =
  58. (0xFFFFFFFF >> (Stuff::INT_BITS - TextureNumberBits)) << TextureNumberBit,
  59. AlphaBit = TextureNumberBit + TextureNumberBits,
  60. AlphaBits = 3,
  61. AlphaMask = (0xFFFFFFFF >> (Stuff::INT_BITS - AlphaBits)) << AlphaBit,
  62. FilterBit = AlphaBit + AlphaBits,
  63. FilterBits = 2,
  64. FilterMask = (0xFFFFFFFF >> (Stuff::INT_BITS - FilterBits)) << FilterBit,
  65. FogBit = FilterBit + FilterBits,
  66. FogBits = 2,
  67. FogMask = (0xFFFFFFFF >> (Stuff::INT_BITS - FogBits)) << FogBit,
  68. TextureWrapBit = FogBit + FogBits,
  69. TextureWrapMask = 1 << TextureWrapBit,
  70. DitherBit = TextureWrapBit+1,
  71. DitherMask = 1<<DitherBit,
  72. TextureCorrectionBit = DitherBit+1,
  73. TextureCorrectionMask = 1<<TextureCorrectionBit,
  74. WireFrameBit = TextureCorrectionBit + 1,
  75. WireFrameBits = 2,
  76. WireFrameMask =
  77. (0xFFFFFFFF >> (Stuff::INT_BITS - WireFrameBits)) << WireFrameBit,
  78. ZBufferWriteBit = WireFrameBit + WireFrameBits,
  79. ZBufferWriteMask = 1<<ZBufferWriteBit,
  80. ZBufferCompareBit = ZBufferWriteBit + 1,
  81. ZBufferCompareMask = 1<<ZBufferCompareBit,
  82. SpecularBit = ZBufferCompareBit + 1,
  83. SpecularMask = 1 << SpecularBit,
  84. FlatColoringBit = SpecularBit + 1,
  85. FlatColoringMask = 1 << FlatColoringBit,
  86. UsedRenderBits = FlatColoringBit + 1,
  87. UsedRenderMask = 0xFFFFFFFF >> (Stuff::INT_BITS - UsedRenderBits)
  88. };
  89. enum AlphaMode {
  90. OneZeroMode = 0,
  91. OneOneMode = 1 << AlphaBit,
  92. AlphaOneMode = 2 << AlphaBit,
  93. OneAlphaMode = 3 << AlphaBit,
  94. AlphaInvAlphaMode = 4 << AlphaBit,
  95. OneInvAlphaMode = 5 << AlphaBit,
  96. FirstAlphaAlphaMode = OneOneMode
  97. };
  98. enum FilterMode {
  99. NoFilterMode = 0,
  100. BiLinearFilterMode = 1 << FilterBit,
  101. TriLinearFilterMode = 2 << FilterBit
  102. };
  103. enum SpecularMode {
  104. SpecularOffMode = 0,
  105. SpecularOnMode = 1 << SpecularBit
  106. };
  107. enum TextureWrapMode {
  108. TextureWrap = 0,
  109. TextureClamp = 1 << TextureWrapBit
  110. };
  111. enum DitherMode {
  112. DitherOffMode = 0,
  113. DitherOnMode = 1 << DitherBit
  114. };
  115. enum TextureCorrectionMode {
  116. TextureCorrectionOffMode = 0,
  117. TextureCorrectionOnMode = 1 << TextureCorrectionBit
  118. };
  119. enum WireFrameMode {
  120. WireFrameOffMode = 0,
  121. WireFrameOnlyMode = 1 << WireFrameBit,
  122. WireFrameAddMode = 2 << WireFrameBit
  123. };
  124. enum ZBufferWriteMode {
  125. ZBufferWriteOffMode = 0,
  126. ZBufferWriteOnMode = 1 << ZBufferWriteBit
  127. };
  128. enum ZBufferCompareMode {
  129. ZBufferCompareOffMode = 0,
  130. ZBufferCompareOnMode = 1 << ZBufferCompareBit
  131. };
  132. enum FlatColoringMode {
  133. FlatColoringOffMode = 0,
  134. FlatColoringOnMode = 1 << FlatColoringBit
  135. };
  136. // manipulate render state
  137. void
  138. SetTextureHandle(unsigned texture)
  139. {
  140. Check_Object(this); renderState &= ~TextureMask;
  141. renderDeltaMask |= TextureMask;
  142. Verify(texture <= TextureMask); renderState |= texture;
  143. }
  144. unsigned
  145. GetTextureHandle() const
  146. {Check_Object(this); return renderState & TextureMask;}
  147. void
  148. SetAlphaMode(AlphaMode alpha)
  149. {Check_Object(this); renderState &= ~AlphaMask; renderState |= alpha; renderDeltaMask |= AlphaMask;}
  150. AlphaMode
  151. GetAlphaMode() const
  152. {
  153. Check_Object(this);
  154. return static_cast<AlphaMode>(renderState & AlphaMask);
  155. }
  156. void
  157. SetFilterMode(FilterMode filter)
  158. {Check_Object(this); renderState &= ~FilterMask; renderState |= filter; renderDeltaMask |= FilterMask;}
  159. FilterMode
  160. GetFilterMode() const
  161. {
  162. Check_Object(this);
  163. return static_cast<FilterMode>(renderState & FilterMask);
  164. }
  165. void
  166. SetFogMode(int fog)
  167. {Check_Object(this); renderState &= ~FogMask; renderState |= (fog<<FogBit); renderDeltaMask |= FogMask;}
  168. int
  169. GetFogMode() const
  170. {Check_Object(this); return (renderState & FogMask)>>FogBit;}
  171. void
  172. SetSpecularOn()
  173. {Check_Object(this); renderState |= SpecularOnMode; renderDeltaMask |= SpecularMask;}
  174. void
  175. SetSpecularOff()
  176. {Check_Object(this); renderState &= ~SpecularOnMode; renderDeltaMask |= SpecularMask;}
  177. SpecularMode
  178. GetSpecularMode() const
  179. {Check_Object(this); return static_cast<SpecularMode>(renderState & SpecularMask);}
  180. void
  181. SetTextureWrapMode(TextureWrapMode TextureWrap)
  182. {Check_Object(this); renderState &= ~TextureWrapMask; renderState |= TextureWrap; renderDeltaMask |= TextureWrapMask;}
  183. TextureWrapMode
  184. GetTextureWrapMode() const
  185. {
  186. Check_Object(this);
  187. return static_cast<TextureWrapMode>(renderState & TextureWrapMask);
  188. }
  189. void
  190. SetDitherOn()
  191. {Check_Object(this); renderState |= DitherOnMode; renderDeltaMask |= DitherMask;}
  192. void
  193. SetDitherOff()
  194. {Check_Object(this); renderState &= ~DitherOnMode; renderDeltaMask |= DitherMask;}
  195. DitherMode
  196. GetDitherMode() const
  197. {Check_Object(this); return static_cast<DitherMode>(renderState & DitherMask);}
  198. void
  199. SetTextureCorrectionOn()
  200. {Check_Object(this); renderState |= TextureCorrectionOnMode; renderDeltaMask |= TextureCorrectionMask;}
  201. void
  202. SetTextureCorrectionOff()
  203. {Check_Object(this); renderState &= ~TextureCorrectionOnMode; renderDeltaMask |= TextureCorrectionMask;}
  204. TextureCorrectionMode
  205. GetTextureCorrectionMode() const
  206. {Check_Object(this); return static_cast<TextureCorrectionMode>(renderState & TextureCorrectionMask);}
  207. void
  208. SetWireFrameMode(WireFrameMode wire)
  209. {Check_Object(this); renderState &= ~WireFrameMask; renderState |= wire; renderDeltaMask |= WireFrameMask;}
  210. WireFrameMode
  211. GetWireFrameMode() const
  212. {
  213. Check_Object(this);
  214. return static_cast<WireFrameMode>(renderState & WireFrameMask);
  215. }
  216. void
  217. SetZBufferWriteOn()
  218. {Check_Object(this); renderState |= ZBufferWriteOnMode; renderDeltaMask |= ZBufferWriteMask;}
  219. void
  220. SetZBufferWriteOff()
  221. {Check_Object(this); renderState &= ~ZBufferWriteOnMode; renderDeltaMask |= ZBufferWriteMask;}
  222. ZBufferWriteMode
  223. GetZBufferWriteMode() const
  224. {Check_Object(this); return static_cast<ZBufferWriteMode>(renderState & ZBufferWriteMask);}
  225. void
  226. SetZBufferCompareOn()
  227. {Check_Object(this); renderState |= ZBufferCompareOnMode; renderDeltaMask |= ZBufferCompareMask;}
  228. void
  229. SetZBufferCompareOff()
  230. {Check_Object(this); renderState &= ~ZBufferCompareOnMode; renderDeltaMask |= ZBufferCompareMask;}
  231. ZBufferCompareMode
  232. GetZBufferCompareMode() const
  233. {Check_Object(this); return static_cast<ZBufferCompareMode>(renderState & ZBufferCompareMask);}
  234. void
  235. SetFlatColoringOn()
  236. {Check_Object(this); renderState |= FlatColoringOnMode; renderDeltaMask |= FlatColoringMask;}
  237. void
  238. SetFlatColoringOff()
  239. {Check_Object(this); renderState &= ~FlatColoringOnMode; renderDeltaMask |= FlatColoringMask;}
  240. FlatColoringMode
  241. GetFlatColoringMode() const
  242. {Check_Object(this); return static_cast<FlatColoringMode>(renderState & FlatColoringMask);}
  243. void
  244. SetRenderDeltaMask(int mask)
  245. { Check_Object(this); renderDeltaMask = mask; }
  246. int
  247. GetRenderDeltaMask() const
  248. { Check_Object(this); return renderDeltaMask; }
  249. void
  250. SetRenderPermissionMask(int mask)
  251. { Check_Object(this); renderPermissionMask = mask; }
  252. int
  253. GetRenderPermissionMask() const
  254. { Check_Object(this); return renderPermissionMask; }
  255. int
  256. GetRenderStateFlags() const
  257. {Check_Object(this); return renderState;}
  258. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  259. // Process state
  260. //
  261. public:
  262. enum {
  263. PriorityBit = 0,
  264. PriorityBits = 4,
  265. PriorityMask =
  266. (0xFFFFFFFF >> (Stuff::INT_BITS - PriorityBits)) << PriorityBit,
  267. BackFaceBit = PriorityBit + PriorityBits,
  268. BackFaceMask = 1<<BackFaceBit,
  269. LightingBit = BackFaceBit + 1,
  270. LightingBits = 5,
  271. LightingMask =
  272. (0xFFFFFFFF >> (Stuff::INT_BITS - LightingBits)) << LightingBit,
  273. MultitextureBit = LightingBit + LightingBits,
  274. MultitextureBits = 4,
  275. MultitextureMask =
  276. (0xFFFFFFFF >> (Stuff::INT_BITS - MultitextureBits)) << MultitextureBit,
  277. DrawNowBit = MultitextureBit + MultitextureBits,
  278. DrawNowMask = 1<<DrawNowBit,
  279. UsedProcessBits = DrawNowBit + 1,
  280. UsedProcessMask = 0xFFFFFFFF >> (Stuff::INT_BITS - UsedProcessBits)
  281. };
  282. enum BackFaceMode {
  283. BackFaceOffMode = 0,
  284. BackFaceOnMode = 1 << BackFaceBit,
  285. };
  286. enum {
  287. DefaultPriority = 0,
  288. AlphaPriority = 2,
  289. FirstApplicationPriority = 4,
  290. PriorityCount = 1<<PriorityBits,
  291. };
  292. enum LightingMode {
  293. LightingOffMode = 0,
  294. VertexLightingMode = 1 << LightingBit,
  295. LightMapLightingMode = 2 << LightingBit,
  296. LookupLightingMode = 4 << LightingBit,
  297. FaceLightingMode = 8 << LightingBit,
  298. TerrainLightingMode = 16 << LightingBit
  299. };
  300. // if cards are multi texture capable so use this mode
  301. enum MultiTextureMode {
  302. MultiTextureOffMode = 0,
  303. MultiTextureLightmapMode = 1 << MultitextureBit,
  304. MultiTextureSpecularMode = 2 << MultitextureBit,
  305. };
  306. enum DrawNowMode {
  307. DrawNowOffMode = 0,
  308. DrawNowOnMode = 1 << DrawNowBit,
  309. };
  310. // manipulate process state
  311. void
  312. SetBackFaceOn()
  313. {Check_Object(this); processState |= BackFaceOnMode; processDeltaMask |= BackFaceMask;}
  314. void
  315. SetBackFaceOff()
  316. {Check_Object(this); processState &= ~BackFaceOnMode; processDeltaMask |= BackFaceMask;}
  317. BackFaceMode
  318. GetBackFaceMode() const
  319. {Check_Object(this); return static_cast<BackFaceMode>(processState & BackFaceOnMode);}
  320. void
  321. SetPriority(unsigned priority)
  322. {
  323. Check_Object(this); processState &= ~PriorityMask;
  324. processDeltaMask |= PriorityMask;
  325. Verify(priority < PriorityCount); processState |= priority;
  326. }
  327. unsigned
  328. GetPriority() const
  329. {Check_Object(this); return processState & PriorityMask;}
  330. void
  331. SetLightingMode(int lighting)
  332. {Check_Object(this); processState &= ~LightingMask; processState |= lighting; processDeltaMask |= LightingMask;}
  333. int
  334. GetLightingMode() const
  335. {
  336. Check_Object(this);
  337. return static_cast<LightingMode>(processState & LightingMask);
  338. }
  339. void
  340. SetMultiTextureMode(MultiTextureMode multiTex)
  341. {Check_Object(this); processState &= ~MultitextureMask; processState |= multiTex; processDeltaMask |= MultitextureMask;}
  342. MultiTextureMode
  343. GetMultiTextureMode() const
  344. {
  345. Check_Object(this);
  346. return static_cast<MultiTextureMode>(processState & MultitextureMask);
  347. }
  348. void
  349. SetDrawNowOn()
  350. {Check_Object(this); processState |= DrawNowOnMode; processDeltaMask |= DrawNowMask;}
  351. void
  352. SetDrawNowOff()
  353. {Check_Object(this); processState &= ~DrawNowOnMode; processDeltaMask |= DrawNowMask;}
  354. DrawNowMode
  355. GetDrawNowMode() const
  356. {Check_Object(this); return static_cast<DrawNowMode>(processState & DrawNowOnMode);}
  357. void
  358. SetProcessDeltaMask(int mask)
  359. { Check_Object(this); processDeltaMask = mask; }
  360. int
  361. GetProcessDeltaMask() const
  362. { Check_Object(this); return processDeltaMask; }
  363. void
  364. SetProcessPermissionMask(int mask)
  365. { Check_Object(this); processPermissionMask = mask; }
  366. int
  367. GetProcessPermissionMask() const
  368. { Check_Object(this); return processPermissionMask; }
  369. int
  370. GetProcessStateFlags() const
  371. {Check_Object(this); return processState;}
  372. void
  373. SetRendererState(MLRTexturePool*);
  374. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  375. // System flags set at begin of every frame
  376. //
  377. enum {
  378. HasAGPAvailable = 1, // FALSE when no AGP memory available (assume a low end card)
  379. CanMultitextureLightMap = 2, // TRUE when single pass light mapping is available
  380. CanMultitextureSpecularMap = 4, // TRUE when single pass specular mapping is available
  381. HasMaxUVs = 8 // TRUE if video card has certain limits on UVs
  382. };
  383. static void
  384. SetAGPAvailable(bool b)
  385. { if(b==true) systemFlags |= HasAGPAvailable; else systemFlags &= ~HasAGPAvailable; }
  386. static void
  387. SetMultitextureLightMap(bool b)
  388. { if(b==true) systemFlags |= CanMultitextureLightMap; else systemFlags &= ~CanMultitextureLightMap; }
  389. static void
  390. SetMultitextureSpecularMap(bool b)
  391. { if(b==true) systemFlags |= CanMultitextureSpecularMap; else systemFlags &= ~CanMultitextureSpecularMap; }
  392. static void
  393. SetHasMaxUVs(bool b)
  394. { if(b==true) systemFlags |= HasMaxUVs; else systemFlags &= ~HasMaxUVs; }
  395. static bool
  396. GetAGPAvailable()
  397. { return (systemFlags & HasAGPAvailable)>0; }
  398. static bool
  399. GetMultitextureLightMap()
  400. { return (systemFlags & CanMultitextureLightMap)>0; }
  401. static bool
  402. GetMultitextureSpecularMap()
  403. { return (systemFlags & CanMultitextureSpecularMap)>0; }
  404. static bool
  405. GetHasMaxUVs()
  406. { return (systemFlags & HasMaxUVs)>0; }
  407. static void
  408. SetMaxUV(float mUV)
  409. { maxUV = mUV; SetHasMaxUVs(mUV > 0.0f); }
  410. static float
  411. GetMaxUV()
  412. { return maxUV; }
  413. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  414. // Assignment operators
  415. //
  416. MLRState&
  417. operator=(const int &s)
  418. {Check_Pointer(this); renderState = s; return *this;}
  419. bool
  420. operator==(const MLRState &s) const
  421. {
  422. Check_Pointer(this);
  423. return
  424. renderState == s.renderState
  425. && processState == s.processState;
  426. }
  427. bool
  428. operator>(const MLRState &s) const
  429. {
  430. Check_Pointer(this);
  431. return
  432. renderState > s.renderState
  433. || renderState == s.renderState
  434. && processState > s.processState;
  435. }
  436. bool
  437. operator!=(const MLRState &s) const
  438. {
  439. Check_Pointer(this);
  440. return
  441. renderState != s.renderState
  442. || processState != s.processState;
  443. }
  444. MLRState&
  445. Combine(
  446. const MLRState &master,
  447. const MLRState &slave
  448. );
  449. friend Stuff::IteratorPosition
  450. GetHashFunctions::GetHashValue(const MLRState &value);
  451. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  452. // Testing
  453. //
  454. public:
  455. void
  456. TestInstance() const
  457. {}
  458. protected:
  459. int
  460. renderState,
  461. processState,
  462. renderDeltaMask,
  463. renderPermissionMask,
  464. processDeltaMask,
  465. processPermissionMask;
  466. static int
  467. systemFlags;
  468. static float
  469. maxUV;
  470. #ifdef OLDFOG
  471. unsigned int
  472. fogColor;
  473. Stuff::Scalar
  474. fogDensity,
  475. nearFog,
  476. farFog;
  477. #else
  478. public:
  479. static unsigned int
  480. fogColor;
  481. #endif
  482. };
  483. }