MLRClippingState.hpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. //===========================================================================//
  2. // Copyright (C) Microsoft Corporation. All rights reserved. //
  3. //===========================================================================//
  4. #pragma once
  5. #define MLR_MLRCLIPPINGSTATE_HPP
  6. #if !defined(MLR_MLR_HPP)
  7. #include <MLR\MLR.hpp>
  8. #endif
  9. namespace MidLevelRenderer {
  10. //##########################################################################
  11. //#################### MLRClippingState ##############################
  12. //##########################################################################
  13. class MLRClippingState
  14. {
  15. protected:
  16. int
  17. clippingState;
  18. public:
  19. MLRClippingState()
  20. { clippingState = 0; };
  21. MLRClippingState(int i)
  22. { clippingState = i; };
  23. MLRClippingState(const MLRClippingState& state)
  24. { clippingState = state.clippingState;}
  25. //##########################################################################
  26. // Attention !!! when changing the flags also change them in
  27. // Stuff::Vector4D::MultiplySetClip the assembler block
  28. //
  29. //##########################################################################
  30. enum {
  31. TopClipBit = 0,
  32. BottomClipBit,
  33. LeftClipBit,
  34. RightClipBit,
  35. NearClipBit,
  36. FarClipBit,
  37. NextBit
  38. };
  39. enum {
  40. TopClipFlag = 1<<TopClipBit,
  41. BottomClipFlag = 1<<BottomClipBit,
  42. LeftClipFlag = 1<<LeftClipBit,
  43. RightClipFlag = 1<<RightClipBit,
  44. NearClipFlag = 1<<NearClipBit,
  45. FarClipFlag = 1<<FarClipBit,
  46. ClipMask =
  47. TopClipFlag | BottomClipFlag | LeftClipFlag
  48. | RightClipFlag | NearClipFlag | FarClipFlag
  49. };
  50. bool
  51. IsFarClipped()
  52. {Check_Pointer(this); return (clippingState&FarClipFlag) != 0;}
  53. void
  54. SetFarClip()
  55. {Check_Pointer(this); clippingState |= FarClipFlag;}
  56. void
  57. ClearFarClip()
  58. {Check_Pointer(this); clippingState &= ~FarClipFlag;}
  59. bool
  60. IsNearClipped()
  61. {Check_Pointer(this); return (clippingState&NearClipFlag) != 0;}
  62. void
  63. SetNearClip()
  64. {Check_Pointer(this); clippingState |= NearClipFlag;}
  65. void
  66. ClearNearClip()
  67. {Check_Pointer(this); clippingState &= ~NearClipFlag;}
  68. bool
  69. IsTopClipped()
  70. {Check_Pointer(this); return clippingState&TopClipFlag;}
  71. void
  72. SetTopClip()
  73. {Check_Pointer(this); clippingState |= TopClipFlag;}
  74. void
  75. ClearTopClip()
  76. {Check_Pointer(this); clippingState &= ~TopClipFlag;}
  77. bool
  78. IsBottomClipped()
  79. {Check_Pointer(this); return (clippingState&BottomClipFlag) != 0;}
  80. void
  81. SetBottomClip()
  82. {Check_Pointer(this); clippingState |= BottomClipFlag;}
  83. void
  84. ClearBottomClip()
  85. {Check_Pointer(this); clippingState &= ~BottomClipFlag;}
  86. bool
  87. IsLeftClipped()
  88. {Check_Pointer(this); return (clippingState&LeftClipFlag) != 0;}
  89. void
  90. SetLeftClip()
  91. {Check_Pointer(this); clippingState |= LeftClipFlag;}
  92. void
  93. ClearLeftClip()
  94. {Check_Pointer(this); clippingState &= ~LeftClipFlag;}
  95. bool
  96. IsRightClipped()
  97. {Check_Pointer(this); return (clippingState&RightClipFlag) != 0;}
  98. void
  99. SetRightClip()
  100. {Check_Pointer(this); clippingState |= RightClipFlag;}
  101. void
  102. ClearRightClip()
  103. {Check_Pointer(this); clippingState &= ~RightClipFlag;}
  104. void
  105. SetClip(int mask, int flag)
  106. {
  107. Check_Pointer(this);
  108. #if USE_ASSEMBLER_CODE
  109. _asm {
  110. xor ecx, ecx
  111. mov ebx, mask
  112. test ebx, 0ffffffffh
  113. seta cl
  114. xor eax, eax
  115. sub eax, ecx
  116. and flag, eax
  117. }
  118. clippingState |= flag;
  119. #else
  120. if(mask != 0)
  121. {
  122. clippingState |= flag;
  123. }
  124. #endif
  125. }
  126. bool
  127. IsClipped(int mask)
  128. {Check_Pointer(this); return (clippingState & mask) != 0;}
  129. int
  130. GetClippingState()
  131. {Check_Pointer(this); return (clippingState & ClipMask);}
  132. void
  133. SetClippingState(int state)
  134. {Check_Pointer(this); clippingState = state & ClipMask;}
  135. int
  136. GetNumberOfSetBits()
  137. {Check_Pointer(this); Verify(clippingState<=ClipMask);
  138. return numberBitsLookUpTable[clippingState]; }
  139. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  140. // Assignment operators
  141. //
  142. public:
  143. MLRClippingState&
  144. operator=(const MLRClippingState &s)
  145. {
  146. Check_Pointer(this);
  147. clippingState = s.clippingState;
  148. return *this;
  149. }
  150. MLRClippingState&
  151. operator&=(const MLRClippingState &s)
  152. {
  153. Check_Pointer(this);
  154. clippingState &= s.clippingState;
  155. return *this;
  156. }
  157. MLRClippingState&
  158. operator|=(const MLRClippingState &s)
  159. {
  160. Check_Pointer(this);
  161. clippingState |= s.clippingState;
  162. return *this;
  163. }
  164. bool
  165. operator==(const MLRClippingState &s)
  166. {
  167. Check_Pointer(this);
  168. return (clippingState == s.clippingState);
  169. }
  170. bool
  171. operator==(const int &s)
  172. {
  173. Check_Pointer(this);
  174. return (clippingState == s);
  175. }
  176. bool
  177. operator!=(const MLRClippingState &s)
  178. {
  179. Check_Pointer(this);
  180. return (clippingState != s.clippingState);
  181. }
  182. bool
  183. operator!=(const int &s)
  184. {
  185. Check_Pointer(this);
  186. return (clippingState != s);
  187. }
  188. void
  189. Save(Stuff::MemoryStream *stream);
  190. void
  191. Load(Stuff::MemoryStream *stream);
  192. inline void
  193. Clip4dVertex(Stuff::Vector4D *v4d);
  194. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  195. // Testing
  196. //
  197. public:
  198. void
  199. TestInstance()
  200. {}
  201. private:
  202. static int numberBitsLookUpTable[ClipMask+1];
  203. };
  204. inline void
  205. MLRClippingState::Clip4dVertex(Stuff::Vector4D *v4d)
  206. {
  207. #if USE_ASSEMBLER_CODE
  208. int _ret = 0;
  209. _asm {
  210. mov edi, v4d
  211. xor ecx,ecx
  212. xor edx, edx
  213. test dword ptr [edi], 080000000h
  214. setne cl
  215. sub edx, ecx
  216. and edx, 8 // RightClipFlag
  217. xor ebx, ebx
  218. test dword ptr [edi+4], 080000000h
  219. setne cl
  220. sub ebx, ecx
  221. and ebx, 2 // BottomClipFlag
  222. or edx, ebx
  223. xor ebx, ebx
  224. test dword ptr [edi+8], 080000000h
  225. setne cl
  226. sub ebx, ecx
  227. and ebx, 16 // NearClipFlag
  228. or edx, ebx
  229. fld dword ptr [edi+0Ch]
  230. xor ebx, ebx
  231. fcom dword ptr [edi]
  232. fnstsw ax
  233. test ah, 1
  234. setne cl
  235. sub ebx, ecx
  236. and ebx, 4 // LeftClipFlag
  237. or edx, ebx
  238. xor ebx, ebx
  239. fcom dword ptr [edi+4]
  240. fnstsw ax
  241. test ah, 1
  242. setne cl
  243. sub ebx, ecx
  244. and ebx, 1 // TopClipFlag
  245. or edx, ebx
  246. xor ebx, ebx
  247. fcomp dword ptr [edi+8]
  248. fnstsw ax
  249. test ah, 41h
  250. setne cl
  251. sub ebx, ecx
  252. and ebx, 32 // FarClipFlag
  253. or edx, ebx
  254. mov _ret, edx
  255. }
  256. clippingState = _ret;
  257. #else
  258. clippingState = 0;
  259. if(v4d->w <= v4d->z)
  260. {
  261. SetFarClip();
  262. }
  263. if(v4d->z < 0.0f)
  264. {
  265. SetNearClip();
  266. }
  267. if(v4d->x < 0.0f)
  268. {
  269. SetRightClip();
  270. }
  271. if(v4d->w < v4d->x)
  272. {
  273. SetLeftClip();
  274. }
  275. if(v4d->y < 0.0f)
  276. {
  277. SetBottomClip();
  278. }
  279. if(v4d->w < v4d->y)
  280. {
  281. SetTopClip();
  282. }
  283. #endif
  284. }
  285. }