Source code of Windows XP (NT5)
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.

1016 lines
32 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. #if defined(_M_IA64)
  481. #pragma optimize("",off)
  482. #endif
  483. void FASTCALL __glSoftResetContext(__GLcontext *gc)
  484. {
  485. __GLlightSourceState *lss;
  486. __GLlightSourceMachine *lsm;
  487. __GLvertex *vx;
  488. GLint i, numLights;
  489. __GLfloat one = __glOne;
  490. /*
  491. ** Initialize constant values first so that they will
  492. ** be valid if needed by subsequent initialization steps.
  493. */
  494. if (gc->constants.alphaTestSize == 0) {
  495. gc->constants.alphaTestSize = 256; /* A default */
  496. }
  497. gc->constants.alphaTableConv = (gc->constants.alphaTestSize - 1) /
  498. gc->frontBuffer.alphaScale;
  499. // viewportEpsilon is the smallest representable value in window
  500. // coordinates. The number of fractional bits in a window
  501. // coordinate is known and fixed, so compute epsilon directly
  502. gc->constants.viewportEpsilon = 1.0f/(1 << __GL_VERTEX_FRAC_BITS);
  503. gc->constants.viewportAlmostHalf =
  504. __glHalf - gc->constants.viewportEpsilon;
  505. /* Allocate memory to hold variable sized things */
  506. numLights = gc->constants.numberOfLights;
  507. /* Misc machine state */
  508. gc->beginMode = __GL_NEED_VALIDATE;
  509. gc->dirtyMask = __GL_DIRTY_ALL;
  510. gc->validateMask = (GLuint) ~0;
  511. gc->attributes.stackPointer = &gc->attributes.stack[0];
  512. gc->clientAttributes.stackPointer = &gc->clientAttributes.stack[0];
  513. #ifndef NT
  514. // NT vertex allocation is done in __glEarlyInitContext.
  515. gc->vertex.v0 = &gc->vertex.vbuf[0];
  516. vx = &gc->vertex.vbuf[0];
  517. for (i = 0; i < __GL_NVBUF; i++, vx++) {
  518. vx->color = &vx->colors[__GL_FRONTFACE];
  519. }
  520. #endif
  521. /* We need to initialize the matrix stuff early (before we handle */
  522. /* lighting) since we cache the modelview matrix with the light */
  523. __glInitTransformState(gc);
  524. #ifdef NT
  525. /* __glInitTransformState does memory allocation (incl. modelView */
  526. /* matrix which is needed later in this function. If error code */
  527. /* is set, we must exit. */
  528. if (((__GLGENcontext *) gc)->errorcode)
  529. return;
  530. #endif
  531. /* GL_LIGHTING_BIT state */
  532. gc->state.light.model.ambient.r = DefaultAmbient[0];
  533. gc->state.light.model.ambient.g = DefaultAmbient[1];
  534. gc->state.light.model.ambient.b = DefaultAmbient[2];
  535. gc->state.light.model.ambient.a = DefaultAmbient[3];
  536. gc->state.light.front.ambient.r = DefaultAmbient[0];
  537. gc->state.light.front.ambient.g = DefaultAmbient[1];
  538. gc->state.light.front.ambient.b = DefaultAmbient[2];
  539. gc->state.light.front.ambient.a = DefaultAmbient[3];
  540. gc->state.light.front.diffuse.r = DefaultDiffuse[0];
  541. gc->state.light.front.diffuse.g = DefaultDiffuse[1];
  542. gc->state.light.front.diffuse.b = DefaultDiffuse[2];
  543. gc->state.light.front.diffuse.a = DefaultDiffuse[3];
  544. gc->state.light.front.specular.r = DefaultBlack[0];
  545. gc->state.light.front.specular.g = DefaultBlack[1];
  546. gc->state.light.front.specular.b = DefaultBlack[2];
  547. gc->state.light.front.specular.a = DefaultBlack[3];
  548. gc->state.light.front.emissive.r = DefaultBlack[0];
  549. gc->state.light.front.emissive.g = DefaultBlack[1];
  550. gc->state.light.front.emissive.b = DefaultBlack[2];
  551. gc->state.light.front.emissive.a = DefaultBlack[3];
  552. gc->state.light.front.cmapa = 0;
  553. gc->state.light.front.cmapd = 1;
  554. gc->state.light.front.cmaps = 1;
  555. gc->state.light.back = gc->state.light.front;
  556. gc->light.front.specularExponent = -1;
  557. gc->light.front.specTable = NULL;
  558. gc->light.front.cache = NULL;
  559. gc->light.back.specularExponent = -1;
  560. gc->light.back.specTable = NULL;
  561. gc->light.back.cache = NULL;
  562. /* Initialize the individual lights */
  563. gc->state.light.dirtyLights = (1 << gc->constants.numberOfLights)-1;
  564. lss = &gc->state.light.source[0];
  565. lsm = &gc->light.source[0];
  566. for (i = 0; i < numLights; i++, lss++, lsm++) {
  567. lss->ambient.r = DefaultBlack[0];
  568. lss->ambient.g = DefaultBlack[1];
  569. lss->ambient.b = DefaultBlack[2];
  570. lss->ambient.a = DefaultBlack[3];
  571. if (i == 0) {
  572. lss->diffuse.r = DefaultWhite[0];
  573. lss->diffuse.g = DefaultWhite[1];
  574. lss->diffuse.b = DefaultWhite[2];
  575. lss->diffuse.a = DefaultWhite[3];
  576. } else {
  577. lss->diffuse.r = DefaultBlack[0];
  578. lss->diffuse.g = DefaultBlack[1];
  579. lss->diffuse.b = DefaultBlack[2];
  580. lss->diffuse.a = DefaultBlack[3];
  581. }
  582. lss->lightMatrix = gc->transform.modelView->matrix;
  583. lss->specular = lss->diffuse;
  584. lss->position.z = __glOne;
  585. lss->positionEye.z = __glOne;
  586. lsm->position.z = __glOne;
  587. lss->direction.z = __glMinusOne;
  588. lss->directionEye.z = __glMinusOne;
  589. lss->directionEyeNorm.z = __glMinusOne;
  590. lsm->direction.z = __glMinusOne;
  591. lss->spotLightCutOffAngle = 180;
  592. lss->constantAttenuation = __glOne;
  593. lsm->spotTable = NULL;
  594. lsm->spotLightExponent = -1;
  595. lsm->cache = NULL;
  596. }
  597. gc->state.light.colorMaterialFace = GL_FRONT_AND_BACK;
  598. gc->state.light.colorMaterialParam = GL_AMBIENT_AND_DIFFUSE;
  599. gc->state.light.shadingModel = GL_SMOOTH;
  600. /* GL_HINT_BIT state */
  601. gc->state.hints.perspectiveCorrection = GL_DONT_CARE;
  602. gc->state.hints.pointSmooth = GL_DONT_CARE;
  603. gc->state.hints.lineSmooth = GL_DONT_CARE;
  604. gc->state.hints.polygonSmooth = GL_DONT_CARE;
  605. gc->state.hints.fog = GL_DONT_CARE;
  606. #ifdef GL_WIN_phong_shading
  607. gc->state.hints.phong = GL_DONT_CARE;
  608. #endif //GL_WIN_phong_shading
  609. /* GL_CURRENT_BIT state */
  610. gc->state.current.rasterPos.window.x = gc->constants.fviewportXAdjust;
  611. gc->state.current.rasterPos.window.y = gc->constants.fviewportYAdjust;
  612. gc->state.current.rasterPos.clip.w = __glOne;
  613. gc->state.current.rasterPos.texture.w = __glOne;
  614. gc->state.current.rasterPos.color
  615. = &gc->state.current.rasterPos.colors[__GL_FRONTFACE];
  616. if (gc->modes.rgbMode) {
  617. gc->state.current.rasterPos.colors[__GL_FRONTFACE].r = DefaultWhite[0];
  618. gc->state.current.rasterPos.colors[__GL_FRONTFACE].g = DefaultWhite[1];
  619. gc->state.current.rasterPos.colors[__GL_FRONTFACE].b = DefaultWhite[2];
  620. gc->state.current.rasterPos.colors[__GL_FRONTFACE].a = DefaultWhite[3];
  621. } else {
  622. gc->state.current.rasterPos.colors[__GL_FRONTFACE].r = __glOne;
  623. }
  624. gc->state.current.validRasterPos = GL_TRUE;
  625. gc->state.current.edgeTag = GL_TRUE;
  626. /* GL_FOG_BIT state */
  627. gc->state.fog.mode = GL_EXP;
  628. gc->state.fog.density = __glOne;
  629. #ifdef NT
  630. gc->state.fog.density2neg = -(__glOne);
  631. #endif
  632. gc->state.fog.end = (__GLfloat) 1.0;
  633. gc->state.fog.flags = __GL_FOG_GRAY_RGB; // default fog color is 0,0,0,0
  634. /* GL_POINT_BIT state */
  635. gc->state.point.requestedSize = (__GLfloat) 1.0;
  636. gc->state.point.smoothSize = (__GLfloat) 1.0;
  637. gc->state.point.aliasedSize = 1;
  638. /* GL_LINE_BIT state */
  639. gc->state.line.requestedWidth = (__GLfloat) 1.0;
  640. gc->state.line.smoothWidth = (__GLfloat) 1.0;
  641. gc->state.line.aliasedWidth = 1;
  642. gc->state.line.stipple = 0xFFFF;
  643. gc->state.line.stippleRepeat = 1;
  644. /* GL_POLYGON_BIT state */
  645. gc->state.polygon.frontMode = GL_FILL;
  646. gc->state.polygon.backMode = GL_FILL;
  647. gc->state.polygon.cull = GL_BACK;
  648. gc->state.polygon.frontFaceDirection = GL_CCW;
  649. /* GL_POLYGON_STIPPLE_BIT state */
  650. for (i = 0; i < 4*32; i++) {
  651. gc->state.polygonStipple.stipple[i] = 0xFF;
  652. }
  653. for (i = 0; i < 32; i++) {
  654. gc->polygon.stipple[i] = 0xFFFFFFFF;
  655. }
  656. /* GL_ACCUM_BUFFER_BIT state */
  657. /* GL_STENCIL_BUFFER_BIT state */
  658. gc->state.stencil.testFunc = GL_ALWAYS;
  659. gc->state.stencil.mask = __GL_MAX_STENCIL_VALUE;
  660. gc->state.stencil.fail = GL_KEEP;
  661. gc->state.stencil.depthFail = GL_KEEP;
  662. gc->state.stencil.depthPass = GL_KEEP;
  663. gc->state.stencil.writeMask = __GL_MAX_STENCIL_VALUE;
  664. /* GL_DEPTH_BUFFER_BIT state */
  665. gc->state.depth.writeEnable = GL_TRUE;
  666. gc->state.depth.testFunc = GL_LESS;
  667. gc->state.depth.clear = __glOne;
  668. /* GL_COLOR_BUFFER_BIT state */
  669. gc->renderMode = GL_RENDER;
  670. gc->state.raster.alphaFunction = GL_ALWAYS;
  671. gc->state.raster.blendSrc = GL_ONE;
  672. gc->state.raster.blendDst = GL_ZERO;
  673. gc->state.raster.logicOp = GL_COPY;
  674. gc->state.raster.rMask = GL_TRUE;
  675. gc->state.raster.gMask = GL_TRUE;
  676. gc->state.raster.bMask = GL_TRUE;
  677. gc->state.raster.aMask = GL_TRUE;
  678. if (gc->modes.doubleBufferMode) {
  679. gc->state.raster.drawBuffer = GL_BACK;
  680. } else {
  681. gc->state.raster.drawBuffer = GL_FRONT;
  682. }
  683. gc->state.raster.drawBufferReturn = gc->state.raster.drawBuffer;
  684. gc->state.current.userColor.r = (__GLfloat) 1.0;
  685. gc->state.current.userColor.g = (__GLfloat) 1.0;
  686. gc->state.current.userColor.b = (__GLfloat) 1.0;
  687. gc->state.current.userColor.a = (__GLfloat) 1.0;
  688. gc->state.current.userColorIndex = (__GLfloat) 1.0;
  689. if (gc->modes.colorIndexMode) {
  690. gc->state.raster.writeMask = (gc)->frontBuffer.redMax;
  691. }
  692. gc->state.enables.general |= __GL_DITHER_ENABLE;
  693. gc->select.hit = GL_FALSE;
  694. gc->select.sp = gc->select.stack;
  695. /*
  696. ** Initialize larger subsystems by calling their init codes.
  697. */
  698. __glInitEvaluatorState(gc);
  699. __glInitTextureState(gc);
  700. __glInitPixelState(gc);
  701. __glInitLUTCache(gc);
  702. #ifdef NT
  703. __glInitVertexArray(gc);
  704. #endif
  705. #ifdef _MCD_
  706. MCD_STATE_DIRTY(gc, ALL);
  707. #endif
  708. }
  709. #if defined(_M_IA64)
  710. #pragma optimize("",on)
  711. #endif
  712. /************************************************************************/
  713. /*
  714. ** Free any attribute state left on the stack. Stop at the first
  715. ** zero in the array.
  716. */
  717. void FASTCALL __glFreeAttributeState(__GLcontext *gc)
  718. {
  719. __GLattribute *sp, **spp;
  720. for (spp = &gc->attributes.stack[0];
  721. spp < &gc->attributes.stack[gc->constants.maxAttribStackDepth];
  722. spp++) {
  723. if (sp = *spp) {
  724. GCFREE(gc, sp);
  725. } else
  726. break;
  727. }
  728. GCFREE(gc, gc->attributes.stack);
  729. }
  730. void FASTCALL __glFreeClientAttributeState(__GLcontext *gc)
  731. {
  732. __GLclientAttribute *sp, **spp;
  733. for (spp = &gc->clientAttributes.stack[0];
  734. spp < &gc->clientAttributes.stack[gc->constants.maxClientAttribStackDepth];
  735. spp++) {
  736. if (sp = *spp) {
  737. GCFREE(gc, sp);
  738. } else
  739. break;
  740. }
  741. GCFREE(gc, gc->clientAttributes.stack);
  742. }
  743. /*
  744. ** Destroy a context. If it's the current context then the
  745. ** current context is set to GL_NULL.
  746. */
  747. void FASTCALL __glDestroyContext(__GLcontext *gc)
  748. {
  749. __GLcontext *oldgc;
  750. oldgc = (__GLcontext *)GLTEB_SRVCONTEXT();
  751. #ifndef NT
  752. /* Set the global context to the one we are destroying. */
  753. __gl_context = gc;
  754. #else
  755. // Set paTeb to NULL for now. If we ever need to reference pa in this
  756. // function, then set it up appropriately.
  757. gc->paTeb = NULL;
  758. GLTEB_SET_SRVCONTEXT(gc);
  759. /*
  760. ** Need to pop all pushed attributes to free storage.
  761. ** Then it will be safe to delete stack entries.
  762. */
  763. if (gc->attributes.stack) {
  764. while (gc->attributes.stackPointer > &gc->attributes.stack[0]) {
  765. (void) __glInternalPopAttrib(gc, GL_TRUE);
  766. }
  767. }
  768. if (gc->clientAttributes.stack) {
  769. while (gc->clientAttributes.stackPointer > &gc->clientAttributes.stack[0]) {
  770. (void) __glInternalPopClientAttrib(gc, GL_FALSE, GL_TRUE);
  771. }
  772. }
  773. #endif
  774. GCFREE(gc, gc->state.light.source);
  775. GCFREE(gc, gc->light.source);
  776. #ifdef NT
  777. // now lazy allocated
  778. if (gc->select.stack)
  779. #endif
  780. GCFREE(gc, gc->select.stack);
  781. GCFREE(gc, gc->state.transform.eyeClipPlanes);
  782. GCFREE(gc, gc->transform.modelViewStack);
  783. GCFREE(gc, gc->transform.projectionStack);
  784. GCFREE(gc, gc->transform.textureStack);
  785. GCFREE(gc, gc->transform.clipTemp);
  786. GCFREE(gc, gc->alphaTestFuncTable);
  787. #ifdef NT
  788. // they are one memory allocation.
  789. GCFREE(gc, gc->stencilBuffer.testFuncTable);
  790. #else
  791. GCFREE(gc, gc->stencilBuffer.testFuncTable);
  792. GCFREE(gc, gc->stencilBuffer.failOpTable);
  793. GCFREE(gc, gc->stencilBuffer.depthFailOpTable);
  794. GCFREE(gc, gc->stencilBuffer.depthPassOpTable);
  795. #endif
  796. /*
  797. ** Free other malloc'd data associated with the context
  798. */
  799. __glFreeEvaluatorState(gc);
  800. __glFreePixelState(gc);
  801. __glFreeDlistState(gc);
  802. if (gc->attributes.stack) __glFreeAttributeState(gc);
  803. if (gc->clientAttributes.stack) __glFreeClientAttributeState(gc);
  804. if (gc->texture.texture) __glFreeTextureState(gc);
  805. if (gc->light.lutCache) __glFreeLUTCache(gc);
  806. #ifdef NT
  807. // Free the vertex buffer.
  808. PolyArrayFreeBuffer(gc);
  809. #endif
  810. #if __GL_NUMBER_OF_AUX_BUFFERS > 0
  811. /*
  812. ** Free any aux color buffer records
  813. ** Note: Does not free the actual buffer memory, this is done elsewhere.
  814. */
  815. if (gc->auxBuffer) GCFREE(gc, gc->auxBuffer);
  816. #endif
  817. /*
  818. ** Note: We do not free the software buffers here. They are attached
  819. ** to the drawable, and is the glx extension's responsibility to free
  820. ** them when the drawable is destroyed.
  821. */
  822. FREE(gc);
  823. if (gc == oldgc) oldgc = NULL;
  824. #ifndef NT
  825. __gl_context = oldgc;
  826. #else
  827. GLTEB_SET_SRVCONTEXT(oldgc);
  828. #endif
  829. }
  830. #ifdef NT
  831. // See also __glSetError
  832. void FASTCALL __glSetErrorEarly(__GLcontext *gc, GLenum code)
  833. {
  834. if (gc == (__GLcontext *) NULL)
  835. return;
  836. if (!gc->error)
  837. gc->error = code;
  838. ASSERTOPENGL(gc->error == 0
  839. || (gc->error >= GL_INVALID_ENUM && gc->error <= GL_OUT_OF_MEMORY),
  840. "Bad error code in gc\n");
  841. DBGLEVEL2(LEVEL_INFO, "__glSetError error: %ld (0x%lX)\n", code, code);
  842. #if 0
  843. try
  844. {
  845. DebugBreak();
  846. }
  847. finally
  848. {
  849. }
  850. #endif
  851. }
  852. #endif // NT
  853. void FASTCALL __glSetError(GLenum code)
  854. {
  855. __GL_SETUP();
  856. __glSetErrorEarly(gc, code);
  857. }
  858. GLint APIPRIVATE __glim_RenderMode(GLenum mode)
  859. {
  860. GLint rv;
  861. __GL_SETUP_NOT_IN_BEGIN2();
  862. switch (mode) {
  863. case GL_RENDER:
  864. case GL_FEEDBACK:
  865. case GL_SELECT:
  866. break;
  867. default:
  868. __glSetError(GL_INVALID_ENUM);
  869. return 0;
  870. }
  871. /* Switch out of old render mode. Get return value. */
  872. switch (gc->renderMode) {
  873. case GL_RENDER:
  874. rv = 0;
  875. break;
  876. case GL_FEEDBACK:
  877. rv = gc->feedback.overFlowed ? -1 :
  878. (GLint)((ULONG_PTR)(gc->feedback.result - gc->feedback.resultBase));
  879. break;
  880. case GL_SELECT:
  881. rv = gc->select.overFlowed ? -1 : gc->select.hits;
  882. break;
  883. }
  884. switch (mode) {
  885. case GL_FEEDBACK:
  886. if (!gc->feedback.resultBase) {
  887. __glSetError(GL_INVALID_OPERATION);
  888. return rv;
  889. }
  890. gc->feedback.result = gc->feedback.resultBase;
  891. gc->feedback.overFlowed = GL_FALSE;
  892. break;
  893. case GL_SELECT:
  894. if (!gc->select.stack)
  895. {
  896. gc->select.stack = (GLuint*) GCALLOCZ
  897. (gc, gc->constants.maxNameStackDepth*sizeof(GLuint));
  898. if (!gc->select.stack)
  899. {
  900. __glSetError(GL_OUT_OF_MEMORY);
  901. return rv;
  902. }
  903. }
  904. if (!gc->select.resultBase) {
  905. __glSetError(GL_INVALID_OPERATION);
  906. return rv;
  907. }
  908. gc->select.result = gc->select.resultBase;
  909. gc->select.overFlowed = GL_FALSE;
  910. gc->select.sp = gc->select.stack;
  911. gc->select.hit = GL_FALSE;
  912. gc->select.hits = 0;
  913. gc->select.z = 0;
  914. break;
  915. }
  916. /* Switch to new render mode - do this last! */
  917. if (gc->renderMode == mode) return rv;
  918. gc->renderMode = mode;
  919. __GL_DELAY_VALIDATE(gc);
  920. return rv;
  921. }