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.

1008 lines
33 KiB

  1. /*
  2. ** Copyright 1991-1993, Silicon Graphics, Inc.
  3. ** All Rights Reserved.
  4. **
  5. ** This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6. ** the contents of this file may not be disclosed to third parties, copied or
  7. ** duplicated in any form, in whole or in part, without the prior written
  8. ** permission of Silicon Graphics, Inc.
  9. **
  10. ** RESTRICTED RIGHTS LEGEND:
  11. ** Use, duplication or disclosure by the Government is subject to restrictions
  12. ** as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13. ** and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14. ** successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15. ** rights reserved under the Copyright Laws of the United States.
  16. */
  17. #include "precomp.h"
  18. #pragma hdrstop
  19. #include <stdio.h>
  20. #include <fixed.h>
  21. static GLfloat DefaultAmbient[4] = { 0.2f, 0.2f, 0.2f, 1.0 };
  22. static GLfloat DefaultDiffuse[4] = { 0.8f, 0.8f, 0.8f, 1.0 };
  23. static GLfloat DefaultBlack[4] = { 0.0, 0.0, 0.0, 1.0 };
  24. static GLfloat DefaultWhite[4] = { 1.0, 1.0, 1.0, 1.0 };
  25. #ifdef NT
  26. #define UB2F(ub) ((GLfloat) ((ub) / 255.0))
  27. #define B2F(b) ((GLfloat) ((2 * (b) + 1) / 255.0))
  28. GLfloat __glUByteToFloat[256] = {
  29. UB2F( 0), UB2F( 1), UB2F( 2), UB2F( 3),
  30. UB2F( 4), UB2F( 5), UB2F( 6), UB2F( 7),
  31. UB2F( 8), UB2F( 9), UB2F( 10), UB2F( 11),
  32. UB2F( 12), UB2F( 13), UB2F( 14), UB2F( 15),
  33. UB2F( 16), UB2F( 17), UB2F( 18), UB2F( 19),
  34. UB2F( 20), UB2F( 21), UB2F( 22), UB2F( 23),
  35. UB2F( 24), UB2F( 25), UB2F( 26), UB2F( 27),
  36. UB2F( 28), UB2F( 29), UB2F( 30), UB2F( 31),
  37. UB2F( 32), UB2F( 33), UB2F( 34), UB2F( 35),
  38. UB2F( 36), UB2F( 37), UB2F( 38), UB2F( 39),
  39. UB2F( 40), UB2F( 41), UB2F( 42), UB2F( 43),
  40. UB2F( 44), UB2F( 45), UB2F( 46), UB2F( 47),
  41. UB2F( 48), UB2F( 49), UB2F( 50), UB2F( 51),
  42. UB2F( 52), UB2F( 53), UB2F( 54), UB2F( 55),
  43. UB2F( 56), UB2F( 57), UB2F( 58), UB2F( 59),
  44. UB2F( 60), UB2F( 61), UB2F( 62), UB2F( 63),
  45. UB2F( 64), UB2F( 65), UB2F( 66), UB2F( 67),
  46. UB2F( 68), UB2F( 69), UB2F( 70), UB2F( 71),
  47. UB2F( 72), UB2F( 73), UB2F( 74), UB2F( 75),
  48. UB2F( 76), UB2F( 77), UB2F( 78), UB2F( 79),
  49. UB2F( 80), UB2F( 81), UB2F( 82), UB2F( 83),
  50. UB2F( 84), UB2F( 85), UB2F( 86), UB2F( 87),
  51. UB2F( 88), UB2F( 89), UB2F( 90), UB2F( 91),
  52. UB2F( 92), UB2F( 93), UB2F( 94), UB2F( 95),
  53. UB2F( 96), UB2F( 97), UB2F( 98), UB2F( 99),
  54. UB2F(100), UB2F(101), UB2F(102), UB2F(103),
  55. UB2F(104), UB2F(105), UB2F(106), UB2F(107),
  56. UB2F(108), UB2F(109), UB2F(110), UB2F(111),
  57. UB2F(112), UB2F(113), UB2F(114), UB2F(115),
  58. UB2F(116), UB2F(117), UB2F(118), UB2F(119),
  59. UB2F(120), UB2F(121), UB2F(122), UB2F(123),
  60. UB2F(124), UB2F(125), UB2F(126), UB2F(127),
  61. UB2F(128), UB2F(129), UB2F(130), UB2F(131),
  62. UB2F(132), UB2F(133), UB2F(134), UB2F(135),
  63. UB2F(136), UB2F(137), UB2F(138), UB2F(139),
  64. UB2F(140), UB2F(141), UB2F(142), UB2F(143),
  65. UB2F(144), UB2F(145), UB2F(146), UB2F(147),
  66. UB2F(148), UB2F(149), UB2F(150), UB2F(151),
  67. UB2F(152), UB2F(153), UB2F(154), UB2F(155),
  68. UB2F(156), UB2F(157), UB2F(158), UB2F(159),
  69. UB2F(160), UB2F(161), UB2F(162), UB2F(163),
  70. UB2F(164), UB2F(165), UB2F(166), UB2F(167),
  71. UB2F(168), UB2F(169), UB2F(170), UB2F(171),
  72. UB2F(172), UB2F(173), UB2F(174), UB2F(175),
  73. UB2F(176), UB2F(177), UB2F(178), UB2F(179),
  74. UB2F(180), UB2F(181), UB2F(182), UB2F(183),
  75. UB2F(184), UB2F(185), UB2F(186), UB2F(187),
  76. UB2F(188), UB2F(189), UB2F(190), UB2F(191),
  77. UB2F(192), UB2F(193), UB2F(194), UB2F(195),
  78. UB2F(196), UB2F(197), UB2F(198), UB2F(199),
  79. UB2F(200), UB2F(201), UB2F(202), UB2F(203),
  80. UB2F(204), UB2F(205), UB2F(206), UB2F(207),
  81. UB2F(208), UB2F(209), UB2F(210), UB2F(211),
  82. UB2F(212), UB2F(213), UB2F(214), UB2F(215),
  83. UB2F(216), UB2F(217), UB2F(218), UB2F(219),
  84. UB2F(220), UB2F(221), UB2F(222), UB2F(223),
  85. UB2F(224), UB2F(225), UB2F(226), UB2F(227),
  86. UB2F(228), UB2F(229), UB2F(230), UB2F(231),
  87. UB2F(232), UB2F(233), UB2F(234), UB2F(235),
  88. UB2F(236), UB2F(237), UB2F(238), UB2F(239),
  89. UB2F(240), UB2F(241), UB2F(242), UB2F(243),
  90. UB2F(244), UB2F(245), UB2F(246), UB2F(247),
  91. UB2F(248), UB2F(249), UB2F(250), UB2F(251),
  92. UB2F(252), UB2F(253), UB2F(254), UB2F(255),
  93. };
  94. GLfloat __glByteToFloat[256] = {
  95. B2F( 0), B2F( 1), B2F( 2), B2F( 3),
  96. B2F( 4), B2F( 5), B2F( 6), B2F( 7),
  97. B2F( 8), B2F( 9), B2F( 10), B2F( 11),
  98. B2F( 12), B2F( 13), B2F( 14), B2F( 15),
  99. B2F( 16), B2F( 17), B2F( 18), B2F( 19),
  100. B2F( 20), B2F( 21), B2F( 22), B2F( 23),
  101. B2F( 24), B2F( 25), B2F( 26), B2F( 27),
  102. B2F( 28), B2F( 29), B2F( 30), B2F( 31),
  103. B2F( 32), B2F( 33), B2F( 34), B2F( 35),
  104. B2F( 36), B2F( 37), B2F( 38), B2F( 39),
  105. B2F( 40), B2F( 41), B2F( 42), B2F( 43),
  106. B2F( 44), B2F( 45), B2F( 46), B2F( 47),
  107. B2F( 48), B2F( 49), B2F( 50), B2F( 51),
  108. B2F( 52), B2F( 53), B2F( 54), B2F( 55),
  109. B2F( 56), B2F( 57), B2F( 58), B2F( 59),
  110. B2F( 60), B2F( 61), B2F( 62), B2F( 63),
  111. B2F( 64), B2F( 65), B2F( 66), B2F( 67),
  112. B2F( 68), B2F( 69), B2F( 70), B2F( 71),
  113. B2F( 72), B2F( 73), B2F( 74), B2F( 75),
  114. B2F( 76), B2F( 77), B2F( 78), B2F( 79),
  115. B2F( 80), B2F( 81), B2F( 82), B2F( 83),
  116. B2F( 84), B2F( 85), B2F( 86), B2F( 87),
  117. B2F( 88), B2F( 89), B2F( 90), B2F( 91),
  118. B2F( 92), B2F( 93), B2F( 94), B2F( 95),
  119. B2F( 96), B2F( 97), B2F( 98), B2F( 99),
  120. B2F( 100), B2F( 101), B2F( 102), B2F( 103),
  121. B2F( 104), B2F( 105), B2F( 106), B2F( 107),
  122. B2F( 108), B2F( 109), B2F( 110), B2F( 111),
  123. B2F( 112), B2F( 113), B2F( 114), B2F( 115),
  124. B2F( 116), B2F( 117), B2F( 118), B2F( 119),
  125. B2F( 120), B2F( 121), B2F( 122), B2F( 123),
  126. B2F( 124), B2F( 125), B2F( 126), B2F( 127),
  127. B2F(-128), B2F(-127), B2F(-126), B2F(-125),
  128. B2F(-124), B2F(-123), B2F(-122), B2F(-121),
  129. B2F(-120), B2F(-119), B2F(-118), B2F(-117),
  130. B2F(-116), B2F(-115), B2F(-114), B2F(-113),
  131. B2F(-112), B2F(-111), B2F(-110), B2F(-109),
  132. B2F(-108), B2F(-107), B2F(-106), B2F(-105),
  133. B2F(-104), B2F(-103), B2F(-102), B2F(-101),
  134. B2F(-100), B2F( -99), B2F( -98), B2F( -97),
  135. B2F( -96), B2F( -95), B2F( -94), B2F( -93),
  136. B2F( -92), B2F( -91), B2F( -90), B2F( -89),
  137. B2F( -88), B2F( -87), B2F( -86), B2F( -85),
  138. B2F( -84), B2F( -83), B2F( -82), B2F( -81),
  139. B2F( -80), B2F( -79), B2F( -78), B2F( -77),
  140. B2F( -76), B2F( -75), B2F( -74), B2F( -73),
  141. B2F( -72), B2F( -71), B2F( -70), B2F( -69),
  142. B2F( -68), B2F( -67), B2F( -66), B2F( -65),
  143. B2F( -64), B2F( -63), B2F( -62), B2F( -61),
  144. B2F( -60), B2F( -59), B2F( -58), B2F( -57),
  145. B2F( -56), B2F( -55), B2F( -54), B2F( -53),
  146. B2F( -52), B2F( -51), B2F( -50), B2F( -49),
  147. B2F( -48), B2F( -47), B2F( -46), B2F( -45),
  148. B2F( -44), B2F( -43), B2F( -42), B2F( -41),
  149. B2F( -40), B2F( -39), B2F( -38), B2F( -37),
  150. B2F( -36), B2F( -35), B2F( -34), B2F( -33),
  151. B2F( -32), B2F( -31), B2F( -30), B2F( -29),
  152. B2F( -28), B2F( -27), B2F( -26), B2F( -25),
  153. B2F( -24), B2F( -23), B2F( -22), B2F( -21),
  154. B2F( -20), B2F( -19), B2F( -18), B2F( -17),
  155. B2F( -16), B2F( -15), B2F( -14), B2F( -13),
  156. B2F( -12), B2F( -11), B2F( -10), B2F( -9),
  157. B2F( -8), B2F( -7), B2F( -6), B2F( -5),
  158. B2F( -4), B2F( -3), B2F( -2), B2F( -1),
  159. };
  160. #endif // NT
  161. /*
  162. ** Early initialization of context. Very little is done here, just enough
  163. ** to make a context viable.
  164. */
  165. void FASTCALL __glEarlyInitContext(__GLcontext *gc)
  166. {
  167. GLint numLights, attribDepth;
  168. GLint i;
  169. ASSERTOPENGL(__GL_MAX_MAX_VIEWPORT == __GL_MAX_WINDOW_WIDTH &&
  170. __GL_MAX_MAX_VIEWPORT == __GL_MAX_WINDOW_HEIGHT,
  171. "__GL_MAX_MAX_VIEWPORT mismatch\n");
  172. gc->constants.fviewportXAdjust = (__GLfloat) gc->constants.viewportXAdjust;
  173. gc->constants.fviewportYAdjust = (__GLfloat) gc->constants.viewportYAdjust;
  174. gc->procs.pickColorMaterialProcs = __glNopGC;
  175. gc->procs.applyColor = __glNopGC;
  176. /* Allocate memory to hold variable sized things */
  177. numLights = gc->constants.numberOfLights;
  178. gc->state.light.source = (__GLlightSourceState*)
  179. GCALLOCZ(gc, numLights*sizeof(__GLlightSourceState));
  180. gc->light.lutCache = NULL;
  181. gc->light.source = (__GLlightSourceMachine*)
  182. GCALLOCZ(gc, numLights*sizeof(__GLlightSourceMachine));
  183. attribDepth = gc->constants.maxAttribStackDepth;
  184. gc->attributes.stack = (__GLattribute**)
  185. GCALLOCZ(gc, attribDepth*sizeof(__GLattribute*));
  186. attribDepth = gc->constants.maxClientAttribStackDepth;
  187. gc->clientAttributes.stack = (__GLclientAttribute**)
  188. GCALLOCZ(gc, attribDepth*sizeof(__GLclientAttribute*));
  189. // now lazy allocate in RenderMode
  190. gc->select.stack = (GLuint*) NULL;
  191. #ifdef NT
  192. // Allocate (n-1) vertices. The last one is reserved by polyarray code.
  193. (void) PolyArrayAllocBuffer(gc, POLYDATA_BUFFER_SIZE + 1);
  194. #ifndef NEW_PARTIAL_PRIM
  195. for (i = 0; i < sizeof(gc->vertex.pdSaved)/sizeof(gc->vertex.pdSaved[0]); i++)
  196. gc->vertex.pdSaved[i].color = &gc->vertex.pdSaved[i].colors[__GL_FRONTFACE];
  197. #endif // NEW_PARTIAL_PRIM
  198. #endif
  199. #ifdef _X86_
  200. initClipCodesTable();
  201. initInvSqrtTable();
  202. #endif // _X86_
  203. __glEarlyInitTextureState(gc);
  204. #if __GL_NUMBER_OF_AUX_BUFFERS > 0
  205. /*
  206. ** Allocate any aux color buffer records
  207. ** Note: Does not allocate the actual buffer memory, this is done elsewhere.
  208. */
  209. if (gc->modes.maxAuxBuffers > 0) {
  210. gc->auxBuffer = (__GLcolorBuffer *)
  211. GCALLOCZ(gc, gc->modes.maxAuxBuffers*sizeof(__GLcolorBuffer));
  212. }
  213. #endif
  214. __glInitDlistState(gc);
  215. }
  216. void FASTCALL __glContextSetColorScales(__GLcontext *gc)
  217. {
  218. __GLfloat one = __glOne;
  219. __GLattribute **spp;
  220. __GLattribute *sp;
  221. GLuint mask;
  222. GLint i;
  223. gc->frontBuffer.oneOverRedScale = one / gc->frontBuffer.redScale;
  224. gc->frontBuffer.oneOverGreenScale = one / gc->frontBuffer.greenScale;
  225. gc->frontBuffer.oneOverBlueScale = one / gc->frontBuffer.blueScale;
  226. gc->frontBuffer.oneOverAlphaScale = one / gc->frontBuffer.alphaScale;
  227. gc->vertexToBufferIdentity = GL_TRUE;
  228. if (__GL_FLOAT_NEZ(gc->redVertexScale))
  229. {
  230. gc->oneOverRedVertexScale = one / gc->redVertexScale;
  231. }
  232. else
  233. {
  234. gc->oneOverRedVertexScale = __glZero;
  235. }
  236. if (__GL_FLOAT_NE(gc->redVertexScale, gc->frontBuffer.redScale))
  237. {
  238. gc->redVertexToBufferScale =
  239. gc->frontBuffer.redScale * gc->oneOverRedVertexScale;
  240. gc->vertexToBufferIdentity = GL_FALSE;
  241. }
  242. else
  243. {
  244. gc->redVertexToBufferScale = __glOne;
  245. }
  246. if (__GL_FLOAT_NEZ(gc->greenVertexScale))
  247. {
  248. gc->oneOverGreenVertexScale = one / gc->greenVertexScale;
  249. }
  250. else
  251. {
  252. gc->oneOverGreenVertexScale = __glZero;
  253. }
  254. if (__GL_FLOAT_NE(gc->greenVertexScale, gc->frontBuffer.greenScale))
  255. {
  256. gc->greenVertexToBufferScale =
  257. gc->frontBuffer.greenScale * gc->oneOverGreenVertexScale;
  258. gc->vertexToBufferIdentity = GL_FALSE;
  259. }
  260. else
  261. {
  262. gc->greenVertexToBufferScale = __glOne;
  263. }
  264. if (__GL_FLOAT_NEZ(gc->blueVertexScale))
  265. {
  266. gc->oneOverBlueVertexScale = one / gc->blueVertexScale;
  267. }
  268. else
  269. {
  270. gc->oneOverBlueVertexScale = __glZero;
  271. }
  272. if (__GL_FLOAT_NE(gc->blueVertexScale, gc->frontBuffer.blueScale))
  273. {
  274. gc->blueVertexToBufferScale =
  275. gc->frontBuffer.blueScale * gc->oneOverBlueVertexScale;
  276. gc->vertexToBufferIdentity = GL_FALSE;
  277. }
  278. else
  279. {
  280. gc->blueVertexToBufferScale = __glOne;
  281. }
  282. if (__GL_FLOAT_NEZ(gc->alphaVertexScale))
  283. {
  284. gc->oneOverAlphaVertexScale = one / gc->alphaVertexScale;
  285. }
  286. else
  287. {
  288. gc->oneOverAlphaVertexScale = __glZero;
  289. }
  290. if (__GL_FLOAT_NE(gc->alphaVertexScale, gc->frontBuffer.alphaScale))
  291. {
  292. gc->alphaVertexToBufferScale =
  293. gc->frontBuffer.alphaScale * gc->oneOverAlphaVertexScale;
  294. gc->vertexToBufferIdentity = GL_FALSE;
  295. }
  296. else
  297. {
  298. gc->alphaVertexToBufferScale = __glOne;
  299. }
  300. for (spp = &gc->attributes.stack[0]; spp < gc->attributes.stackPointer;
  301. spp++) {
  302. sp = *spp;
  303. mask = sp->mask;
  304. if (mask & GL_CURRENT_BIT) {
  305. if (gc->modes.rgbMode) {
  306. __glScaleColorf(gc,
  307. &sp->current.rasterPos.colors[__GL_FRONTFACE],
  308. &sp->current.rasterPos.colors[__GL_FRONTFACE].r);
  309. }
  310. }
  311. if (mask & GL_LIGHTING_BIT) {
  312. __glScaleColorf(gc,
  313. &sp->light.model.ambient,
  314. &sp->light.model.ambient.r);
  315. for (i=0; i<gc->constants.numberOfLights; i++) {
  316. __glScaleColorf(gc,
  317. &sp->light.source[i].ambient,
  318. &sp->light.source[i].ambient.r);
  319. __glScaleColorf(gc,
  320. &sp->light.source[i].diffuse,
  321. &sp->light.source[i].diffuse.r);
  322. __glScaleColorf(gc,
  323. &sp->light.source[i].specular,
  324. &sp->light.source[i].specular.r);
  325. }
  326. __glScaleColorf(gc,
  327. &sp->light.front.emissive,
  328. &sp->light.front.emissive.r);
  329. __glScaleColorf(gc,
  330. &sp->light.back.emissive,
  331. &sp->light.back.emissive.r);
  332. }
  333. #ifdef NT
  334. if (mask & GL_FOG_BIT)
  335. {
  336. __glScaleColorf(gc, &sp->fog.color, &sp->fog.color.r);
  337. if (sp->fog.color.r == sp->fog.color.g
  338. && sp->fog.color.r == sp->fog.color.b)
  339. sp->fog.flags |= __GL_FOG_GRAY_RGB;
  340. else
  341. sp->fog.flags &= ~__GL_FOG_GRAY_RGB;
  342. }
  343. #ifdef _MCD_
  344. MCD_STATE_DIRTY(gc, FOG);
  345. #endif
  346. #endif
  347. }
  348. if (gc->modes.rgbMode) {
  349. __glScaleColorf(gc,
  350. &gc->state.current.rasterPos.colors[__GL_FRONTFACE],
  351. &gc->state.current.rasterPos.colors[__GL_FRONTFACE].r);
  352. }
  353. __glScaleColorf(gc,
  354. &gc->state.light.model.ambient,
  355. &gc->state.light.model.ambient.r);
  356. for (i=0; i<gc->constants.numberOfLights; i++) {
  357. __glScaleColorf(gc,
  358. &gc->state.light.source[i].ambient,
  359. &gc->state.light.source[i].ambient.r);
  360. __glScaleColorf(gc,
  361. &gc->state.light.source[i].diffuse,
  362. &gc->state.light.source[i].diffuse.r);
  363. __glScaleColorf(gc,
  364. &gc->state.light.source[i].specular,
  365. &gc->state.light.source[i].specular.r);
  366. }
  367. __glScaleColorf(gc,
  368. &gc->state.light.front.emissive,
  369. &gc->state.light.front.emissive.r);
  370. __glScaleColorf(gc,
  371. &gc->state.light.back.emissive,
  372. &gc->state.light.back.emissive.r);
  373. #ifdef NT
  374. __glScaleColorf(gc, &gc->state.fog.color, &gc->state.fog.color.r);
  375. if (gc->state.fog.color.r == gc->state.fog.color.g
  376. && gc->state.fog.color.r == gc->state.fog.color.b)
  377. gc->state.fog.flags |= __GL_FOG_GRAY_RGB;
  378. else
  379. gc->state.fog.flags &= ~__GL_FOG_GRAY_RGB;
  380. #ifdef _MCD_
  381. MCD_STATE_DIRTY(gc, FOG);
  382. #endif
  383. #endif
  384. __glPixelSetColorScales(gc);
  385. }
  386. void FASTCALL __glContextUnsetColorScales(__GLcontext *gc)
  387. {
  388. GLint i;
  389. __GLattribute **spp;
  390. __GLattribute *sp;
  391. GLuint mask;
  392. for (spp = &gc->attributes.stack[0]; spp < gc->attributes.stackPointer;
  393. spp++) {
  394. sp = *spp;
  395. mask = sp->mask;
  396. if (mask & GL_CURRENT_BIT) {
  397. if (gc->modes.rgbMode) {
  398. __glUnScaleColorf(gc,
  399. &sp->current.rasterPos.colors[__GL_FRONTFACE].r,
  400. &sp->current.rasterPos.colors[__GL_FRONTFACE]);
  401. }
  402. }
  403. if (mask & GL_LIGHTING_BIT) {
  404. __glUnScaleColorf(gc,
  405. &sp->light.model.ambient.r,
  406. &sp->light.model.ambient);
  407. for (i=0; i<gc->constants.numberOfLights; i++) {
  408. __glUnScaleColorf(gc,
  409. &sp->light.source[i].ambient.r,
  410. &sp->light.source[i].ambient);
  411. __glUnScaleColorf(gc,
  412. &sp->light.source[i].diffuse.r,
  413. &sp->light.source[i].diffuse);
  414. __glUnScaleColorf(gc,
  415. &sp->light.source[i].specular.r,
  416. &sp->light.source[i].specular);
  417. }
  418. __glUnScaleColorf(gc,
  419. &sp->light.front.emissive.r,
  420. &sp->light.front.emissive);
  421. __glUnScaleColorf(gc,
  422. &sp->light.back.emissive.r,
  423. &sp->light.back.emissive);
  424. }
  425. #ifdef NT
  426. if (mask & GL_FOG_BIT)
  427. {
  428. __glUnScaleColorf(gc, &sp->fog.color.r, &sp->fog.color);
  429. #ifdef _MCD_
  430. MCD_STATE_DIRTY(gc, FOG);
  431. #endif
  432. }
  433. #endif
  434. }
  435. if (gc->modes.rgbMode) {
  436. __glUnScaleColorf(gc,
  437. &gc->state.current.rasterPos.colors[__GL_FRONTFACE].r,
  438. &gc->state.current.rasterPos.colors[__GL_FRONTFACE]);
  439. }
  440. __glUnScaleColorf(gc,
  441. &gc->state.light.model.ambient.r,
  442. &gc->state.light.model.ambient);
  443. for (i=0; i<gc->constants.numberOfLights; i++) {
  444. __glUnScaleColorf(gc,
  445. &gc->state.light.source[i].ambient.r,
  446. &gc->state.light.source[i].ambient);
  447. __glUnScaleColorf(gc,
  448. &gc->state.light.source[i].diffuse.r,
  449. &gc->state.light.source[i].diffuse);
  450. __glUnScaleColorf(gc,
  451. &gc->state.light.source[i].specular.r,
  452. &gc->state.light.source[i].specular);
  453. }
  454. __glUnScaleColorf(gc,
  455. &gc->state.light.front.emissive.r,
  456. &gc->state.light.front.emissive);
  457. __glUnScaleColorf(gc,
  458. &gc->state.light.back.emissive.r,
  459. &gc->state.light.back.emissive);
  460. #ifdef NT
  461. __glUnScaleColorf(gc, &gc->state.fog.color.r, &gc->state.fog.color);
  462. #ifdef _MCD_
  463. MCD_STATE_DIRTY(gc, FOG);
  464. #endif
  465. #endif
  466. }
  467. /*
  468. ** Initialize all user controllable state, plus any computed state that
  469. ** is only set by user commands. For example, light source position
  470. ** is converted immediately into eye coordinates.
  471. **
  472. ** Any state that would be initialized to zero is not done here because
  473. ** the memory assigned to the context has already been block zeroed.
  474. **
  475. ** NOTE: Since this function may need memory allocation, caller must
  476. ** check that gengc->errorcode is not set indicating memory allocation
  477. ** error. If error is set, context is in an unknown state and data
  478. ** structure integrity is not guaranteed.
  479. */
  480. void FASTCALL __glSoftResetContext(__GLcontext *gc)
  481. {
  482. __GLlightSourceState *lss;
  483. __GLlightSourceMachine *lsm;
  484. __GLvertex *vx;
  485. GLint i, numLights;
  486. __GLfloat one = __glOne;
  487. /*
  488. ** Initialize constant values first so that they will
  489. ** be valid if needed by subsequent initialization steps.
  490. */
  491. if (gc->constants.alphaTestSize == 0) {
  492. gc->constants.alphaTestSize = 256; /* A default */
  493. }
  494. gc->constants.alphaTableConv = (gc->constants.alphaTestSize - 1) /
  495. gc->frontBuffer.alphaScale;
  496. // viewportEpsilon is the smallest representable value in window
  497. // coordinates. The number of fractional bits in a window
  498. // coordinate is known and fixed, so compute epsilon directly
  499. gc->constants.viewportEpsilon = 1.0f/(1 << __GL_VERTEX_FRAC_BITS);
  500. gc->constants.viewportAlmostHalf =
  501. __glHalf - gc->constants.viewportEpsilon;
  502. /* Allocate memory to hold variable sized things */
  503. numLights = gc->constants.numberOfLights;
  504. /* Misc machine state */
  505. gc->beginMode = __GL_NEED_VALIDATE;
  506. gc->dirtyMask = __GL_DIRTY_ALL;
  507. gc->validateMask = (GLuint) ~0;
  508. gc->attributes.stackPointer = &gc->attributes.stack[0];
  509. gc->clientAttributes.stackPointer = &gc->clientAttributes.stack[0];
  510. #ifndef NT
  511. // NT vertex allocation is done in __glEarlyInitContext.
  512. gc->vertex.v0 = &gc->vertex.vbuf[0];
  513. vx = &gc->vertex.vbuf[0];
  514. for (i = 0; i < __GL_NVBUF; i++, vx++) {
  515. vx->color = &vx->colors[__GL_FRONTFACE];
  516. }
  517. #endif
  518. /* We need to initialize the matrix stuff early (before we handle */
  519. /* lighting) since we cache the modelview matrix with the light */
  520. __glInitTransformState(gc);
  521. #ifdef NT
  522. /* __glInitTransformState does memory allocation (incl. modelView */
  523. /* matrix which is needed later in this function. If error code */
  524. /* is set, we must exit. */
  525. if (((__GLGENcontext *) gc)->errorcode)
  526. return;
  527. #endif
  528. /* GL_LIGHTING_BIT state */
  529. gc->state.light.model.ambient.r = DefaultAmbient[0];
  530. gc->state.light.model.ambient.g = DefaultAmbient[1];
  531. gc->state.light.model.ambient.b = DefaultAmbient[2];
  532. gc->state.light.model.ambient.a = DefaultAmbient[3];
  533. gc->state.light.front.ambient.r = DefaultAmbient[0];
  534. gc->state.light.front.ambient.g = DefaultAmbient[1];
  535. gc->state.light.front.ambient.b = DefaultAmbient[2];
  536. gc->state.light.front.ambient.a = DefaultAmbient[3];
  537. gc->state.light.front.diffuse.r = DefaultDiffuse[0];
  538. gc->state.light.front.diffuse.g = DefaultDiffuse[1];
  539. gc->state.light.front.diffuse.b = DefaultDiffuse[2];
  540. gc->state.light.front.diffuse.a = DefaultDiffuse[3];
  541. gc->state.light.front.specular.r = DefaultBlack[0];
  542. gc->state.light.front.specular.g = DefaultBlack[1];
  543. gc->state.light.front.specular.b = DefaultBlack[2];
  544. gc->state.light.front.specular.a = DefaultBlack[3];
  545. gc->state.light.front.emissive.r = DefaultBlack[0];
  546. gc->state.light.front.emissive.g = DefaultBlack[1];
  547. gc->state.light.front.emissive.b = DefaultBlack[2];
  548. gc->state.light.front.emissive.a = DefaultBlack[3];
  549. gc->state.light.front.cmapa = 0;
  550. gc->state.light.front.cmapd = 1;
  551. gc->state.light.front.cmaps = 1;
  552. gc->state.light.back = gc->state.light.front;
  553. gc->light.front.specularExponent = -1;
  554. gc->light.front.specTable = NULL;
  555. gc->light.front.cache = NULL;
  556. gc->light.back.specularExponent = -1;
  557. gc->light.back.specTable = NULL;
  558. gc->light.back.cache = NULL;
  559. /* Initialize the individual lights */
  560. gc->state.light.dirtyLights = (1 << gc->constants.numberOfLights)-1;
  561. lss = &gc->state.light.source[0];
  562. lsm = &gc->light.source[0];
  563. for (i = 0; i < numLights; i++, lss++, lsm++) {
  564. lss->ambient.r = DefaultBlack[0];
  565. lss->ambient.g = DefaultBlack[1];
  566. lss->ambient.b = DefaultBlack[2];
  567. lss->ambient.a = DefaultBlack[3];
  568. if (i == 0) {
  569. lss->diffuse.r = DefaultWhite[0];
  570. lss->diffuse.g = DefaultWhite[1];
  571. lss->diffuse.b = DefaultWhite[2];
  572. lss->diffuse.a = DefaultWhite[3];
  573. } else {
  574. lss->diffuse.r = DefaultBlack[0];
  575. lss->diffuse.g = DefaultBlack[1];
  576. lss->diffuse.b = DefaultBlack[2];
  577. lss->diffuse.a = DefaultBlack[3];
  578. }
  579. lss->lightMatrix = gc->transform.modelView->matrix;
  580. lss->specular = lss->diffuse;
  581. lss->position.z = __glOne;
  582. lss->positionEye.z = __glOne;
  583. lsm->position.z = __glOne;
  584. lss->direction.z = __glMinusOne;
  585. lss->directionEye.z = __glMinusOne;
  586. lss->directionEyeNorm.z = __glMinusOne;
  587. lsm->direction.z = __glMinusOne;
  588. lss->spotLightCutOffAngle = 180;
  589. lss->constantAttenuation = __glOne;
  590. lsm->spotTable = NULL;
  591. lsm->spotLightExponent = -1;
  592. lsm->cache = NULL;
  593. }
  594. gc->state.light.colorMaterialFace = GL_FRONT_AND_BACK;
  595. gc->state.light.colorMaterialParam = GL_AMBIENT_AND_DIFFUSE;
  596. gc->state.light.shadingModel = GL_SMOOTH;
  597. /* GL_HINT_BIT state */
  598. gc->state.hints.perspectiveCorrection = GL_DONT_CARE;
  599. gc->state.hints.pointSmooth = GL_DONT_CARE;
  600. gc->state.hints.lineSmooth = GL_DONT_CARE;
  601. gc->state.hints.polygonSmooth = GL_DONT_CARE;
  602. gc->state.hints.fog = GL_DONT_CARE;
  603. #ifdef GL_WIN_phong_shading
  604. gc->state.hints.phong = GL_DONT_CARE;
  605. #endif //GL_WIN_phong_shading
  606. /* GL_CURRENT_BIT state */
  607. gc->state.current.rasterPos.window.x = gc->constants.fviewportXAdjust;
  608. gc->state.current.rasterPos.window.y = gc->constants.fviewportYAdjust;
  609. gc->state.current.rasterPos.clip.w = __glOne;
  610. gc->state.current.rasterPos.texture.w = __glOne;
  611. gc->state.current.rasterPos.color
  612. = &gc->state.current.rasterPos.colors[__GL_FRONTFACE];
  613. if (gc->modes.rgbMode) {
  614. gc->state.current.rasterPos.colors[__GL_FRONTFACE].r = DefaultWhite[0];
  615. gc->state.current.rasterPos.colors[__GL_FRONTFACE].g = DefaultWhite[1];
  616. gc->state.current.rasterPos.colors[__GL_FRONTFACE].b = DefaultWhite[2];
  617. gc->state.current.rasterPos.colors[__GL_FRONTFACE].a = DefaultWhite[3];
  618. } else {
  619. gc->state.current.rasterPos.colors[__GL_FRONTFACE].r = __glOne;
  620. }
  621. gc->state.current.validRasterPos = GL_TRUE;
  622. gc->state.current.edgeTag = GL_TRUE;
  623. /* GL_FOG_BIT state */
  624. gc->state.fog.mode = GL_EXP;
  625. gc->state.fog.density = __glOne;
  626. #ifdef NT
  627. gc->state.fog.density2neg = -(__glOne);
  628. #endif
  629. gc->state.fog.end = (__GLfloat) 1.0;
  630. gc->state.fog.flags = __GL_FOG_GRAY_RGB; // default fog color is 0,0,0,0
  631. /* GL_POINT_BIT state */
  632. gc->state.point.requestedSize = (__GLfloat) 1.0;
  633. gc->state.point.smoothSize = (__GLfloat) 1.0;
  634. gc->state.point.aliasedSize = 1;
  635. /* GL_LINE_BIT state */
  636. gc->state.line.requestedWidth = (__GLfloat) 1.0;
  637. gc->state.line.smoothWidth = (__GLfloat) 1.0;
  638. gc->state.line.aliasedWidth = 1;
  639. gc->state.line.stipple = 0xFFFF;
  640. gc->state.line.stippleRepeat = 1;
  641. /* GL_POLYGON_BIT state */
  642. gc->state.polygon.frontMode = GL_FILL;
  643. gc->state.polygon.backMode = GL_FILL;
  644. gc->state.polygon.cull = GL_BACK;
  645. gc->state.polygon.frontFaceDirection = GL_CCW;
  646. /* GL_POLYGON_STIPPLE_BIT state */
  647. for (i = 0; i < 4*32; i++) {
  648. gc->state.polygonStipple.stipple[i] = 0xFF;
  649. }
  650. for (i = 0; i < 32; i++) {
  651. gc->polygon.stipple[i] = 0xFFFFFFFF;
  652. }
  653. /* GL_ACCUM_BUFFER_BIT state */
  654. /* GL_STENCIL_BUFFER_BIT state */
  655. gc->state.stencil.testFunc = GL_ALWAYS;
  656. gc->state.stencil.mask = __GL_MAX_STENCIL_VALUE;
  657. gc->state.stencil.fail = GL_KEEP;
  658. gc->state.stencil.depthFail = GL_KEEP;
  659. gc->state.stencil.depthPass = GL_KEEP;
  660. gc->state.stencil.writeMask = __GL_MAX_STENCIL_VALUE;
  661. /* GL_DEPTH_BUFFER_BIT state */
  662. gc->state.depth.writeEnable = GL_TRUE;
  663. gc->state.depth.testFunc = GL_LESS;
  664. gc->state.depth.clear = __glOne;
  665. /* GL_COLOR_BUFFER_BIT state */
  666. gc->renderMode = GL_RENDER;
  667. gc->state.raster.alphaFunction = GL_ALWAYS;
  668. gc->state.raster.blendSrc = GL_ONE;
  669. gc->state.raster.blendDst = GL_ZERO;
  670. gc->state.raster.logicOp = GL_COPY;
  671. gc->state.raster.rMask = GL_TRUE;
  672. gc->state.raster.gMask = GL_TRUE;
  673. gc->state.raster.bMask = GL_TRUE;
  674. gc->state.raster.aMask = GL_TRUE;
  675. if (gc->modes.doubleBufferMode) {
  676. gc->state.raster.drawBuffer = GL_BACK;
  677. } else {
  678. gc->state.raster.drawBuffer = GL_FRONT;
  679. }
  680. gc->state.raster.drawBufferReturn = gc->state.raster.drawBuffer;
  681. gc->state.current.userColor.r = (__GLfloat) 1.0;
  682. gc->state.current.userColor.g = (__GLfloat) 1.0;
  683. gc->state.current.userColor.b = (__GLfloat) 1.0;
  684. gc->state.current.userColor.a = (__GLfloat) 1.0;
  685. gc->state.current.userColorIndex = (__GLfloat) 1.0;
  686. if (gc->modes.colorIndexMode) {
  687. gc->state.raster.writeMask = (gc)->frontBuffer.redMax;
  688. }
  689. gc->state.enables.general |= __GL_DITHER_ENABLE;
  690. gc->select.hit = GL_FALSE;
  691. gc->select.sp = gc->select.stack;
  692. /*
  693. ** Initialize larger subsystems by calling their init codes.
  694. */
  695. __glInitEvaluatorState(gc);
  696. __glInitTextureState(gc);
  697. __glInitPixelState(gc);
  698. __glInitLUTCache(gc);
  699. #ifdef NT
  700. __glInitVertexArray(gc);
  701. #endif
  702. #ifdef _MCD_
  703. MCD_STATE_DIRTY(gc, ALL);
  704. #endif
  705. }
  706. /************************************************************************/
  707. /*
  708. ** Free any attribute state left on the stack. Stop at the first
  709. ** zero in the array.
  710. */
  711. void FASTCALL __glFreeAttributeState(__GLcontext *gc)
  712. {
  713. __GLattribute *sp, **spp;
  714. for (spp = &gc->attributes.stack[0];
  715. spp < &gc->attributes.stack[gc->constants.maxAttribStackDepth];
  716. spp++) {
  717. if (sp = *spp) {
  718. GCFREE(gc, sp);
  719. } else
  720. break;
  721. }
  722. GCFREE(gc, gc->attributes.stack);
  723. }
  724. void FASTCALL __glFreeClientAttributeState(__GLcontext *gc)
  725. {
  726. __GLclientAttribute *sp, **spp;
  727. for (spp = &gc->clientAttributes.stack[0];
  728. spp < &gc->clientAttributes.stack[gc->constants.maxClientAttribStackDepth];
  729. spp++) {
  730. if (sp = *spp) {
  731. GCFREE(gc, sp);
  732. } else
  733. break;
  734. }
  735. GCFREE(gc, gc->clientAttributes.stack);
  736. }
  737. /*
  738. ** Destroy a context. If it's the current context then the
  739. ** current context is set to GL_NULL.
  740. */
  741. void FASTCALL __glDestroyContext(__GLcontext *gc)
  742. {
  743. __GLcontext *oldgc;
  744. oldgc = (__GLcontext *)GLTEB_SRVCONTEXT();
  745. #ifndef NT
  746. /* Set the global context to the one we are destroying. */
  747. __gl_context = gc;
  748. #else
  749. // Set paTeb to NULL for now. If we ever need to reference pa in this
  750. // function, then set it up appropriately.
  751. gc->paTeb = NULL;
  752. GLTEB_SET_SRVCONTEXT(gc);
  753. /*
  754. ** Need to pop all pushed attributes to free storage.
  755. ** Then it will be safe to delete stack entries.
  756. */
  757. if (gc->attributes.stack) {
  758. while (gc->attributes.stackPointer > &gc->attributes.stack[0]) {
  759. (void) __glInternalPopAttrib(gc, GL_TRUE);
  760. }
  761. }
  762. if (gc->clientAttributes.stack) {
  763. while (gc->clientAttributes.stackPointer > &gc->clientAttributes.stack[0]) {
  764. (void) __glInternalPopClientAttrib(gc, GL_FALSE, GL_TRUE);
  765. }
  766. }
  767. #endif
  768. GCFREE(gc, gc->state.light.source);
  769. GCFREE(gc, gc->light.source);
  770. #ifdef NT
  771. // now lazy allocated
  772. if (gc->select.stack)
  773. #endif
  774. GCFREE(gc, gc->select.stack);
  775. GCFREE(gc, gc->state.transform.eyeClipPlanes);
  776. GCFREE(gc, gc->transform.modelViewStack);
  777. GCFREE(gc, gc->transform.projectionStack);
  778. GCFREE(gc, gc->transform.textureStack);
  779. GCFREE(gc, gc->transform.clipTemp);
  780. GCFREE(gc, gc->alphaTestFuncTable);
  781. #ifdef NT
  782. // they are one memory allocation.
  783. GCFREE(gc, gc->stencilBuffer.testFuncTable);
  784. #else
  785. GCFREE(gc, gc->stencilBuffer.testFuncTable);
  786. GCFREE(gc, gc->stencilBuffer.failOpTable);
  787. GCFREE(gc, gc->stencilBuffer.depthFailOpTable);
  788. GCFREE(gc, gc->stencilBuffer.depthPassOpTable);
  789. #endif
  790. /*
  791. ** Free other malloc'd data associated with the context
  792. */
  793. __glFreeEvaluatorState(gc);
  794. __glFreePixelState(gc);
  795. __glFreeDlistState(gc);
  796. if (gc->attributes.stack) __glFreeAttributeState(gc);
  797. if (gc->clientAttributes.stack) __glFreeClientAttributeState(gc);
  798. if (gc->texture.texture) __glFreeTextureState(gc);
  799. if (gc->light.lutCache) __glFreeLUTCache(gc);
  800. #ifdef NT
  801. // Free the vertex buffer.
  802. PolyArrayFreeBuffer(gc);
  803. #endif
  804. #if __GL_NUMBER_OF_AUX_BUFFERS > 0
  805. /*
  806. ** Free any aux color buffer records
  807. ** Note: Does not free the actual buffer memory, this is done elsewhere.
  808. */
  809. if (gc->auxBuffer) GCFREE(gc, gc->auxBuffer);
  810. #endif
  811. /*
  812. ** Note: We do not free the software buffers here. They are attached
  813. ** to the drawable, and is the glx extension's responsibility to free
  814. ** them when the drawable is destroyed.
  815. */
  816. FREE(gc);
  817. if (gc == oldgc) oldgc = NULL;
  818. #ifndef NT
  819. __gl_context = oldgc;
  820. #else
  821. GLTEB_SET_SRVCONTEXT(oldgc);
  822. #endif
  823. }
  824. #ifdef NT
  825. // See also __glSetError
  826. void FASTCALL __glSetErrorEarly(__GLcontext *gc, GLenum code)
  827. {
  828. if (gc == (__GLcontext *) NULL)
  829. return;
  830. if (!gc->error)
  831. gc->error = code;
  832. ASSERTOPENGL(gc->error == 0
  833. || (gc->error >= GL_INVALID_ENUM && gc->error <= GL_OUT_OF_MEMORY),
  834. "Bad error code in gc\n");
  835. DBGLEVEL2(LEVEL_INFO, "__glSetError error: %ld (0x%lX)\n", code, code);
  836. #if 0
  837. try
  838. {
  839. DebugBreak();
  840. }
  841. finally
  842. {
  843. }
  844. #endif
  845. }
  846. #endif // NT
  847. void FASTCALL __glSetError(GLenum code)
  848. {
  849. __GL_SETUP();
  850. __glSetErrorEarly(gc, code);
  851. }
  852. GLint APIPRIVATE __glim_RenderMode(GLenum mode)
  853. {
  854. GLint rv;
  855. __GL_SETUP_NOT_IN_BEGIN2();
  856. switch (mode) {
  857. case GL_RENDER:
  858. case GL_FEEDBACK:
  859. case GL_SELECT:
  860. break;
  861. default:
  862. __glSetError(GL_INVALID_ENUM);
  863. return 0;
  864. }
  865. /* Switch out of old render mode. Get return value. */
  866. switch (gc->renderMode) {
  867. case GL_RENDER:
  868. rv = 0;
  869. break;
  870. case GL_FEEDBACK:
  871. rv = gc->feedback.overFlowed ? -1 :
  872. (GLint)((ULONG_PTR)(gc->feedback.result - gc->feedback.resultBase));
  873. break;
  874. case GL_SELECT:
  875. rv = gc->select.overFlowed ? -1 : gc->select.hits;
  876. break;
  877. }
  878. switch (mode) {
  879. case GL_FEEDBACK:
  880. if (!gc->feedback.resultBase) {
  881. __glSetError(GL_INVALID_OPERATION);
  882. return rv;
  883. }
  884. gc->feedback.result = gc->feedback.resultBase;
  885. gc->feedback.overFlowed = GL_FALSE;
  886. break;
  887. case GL_SELECT:
  888. if (!gc->select.stack)
  889. {
  890. gc->select.stack = (GLuint*) GCALLOCZ
  891. (gc, gc->constants.maxNameStackDepth*sizeof(GLuint));
  892. if (!gc->select.stack)
  893. {
  894. __glSetError(GL_OUT_OF_MEMORY);
  895. return rv;
  896. }
  897. }
  898. if (!gc->select.resultBase) {
  899. __glSetError(GL_INVALID_OPERATION);
  900. return rv;
  901. }
  902. gc->select.result = gc->select.resultBase;
  903. gc->select.overFlowed = GL_FALSE;
  904. gc->select.sp = gc->select.stack;
  905. gc->select.hit = GL_FALSE;
  906. gc->select.hits = 0;
  907. gc->select.z = 0;
  908. break;
  909. }
  910. /* Switch to new render mode - do this last! */
  911. if (gc->renderMode == mode) return rv;
  912. gc->renderMode = mode;
  913. __GL_DELAY_VALIDATE(gc);
  914. return rv;
  915. }