unpack.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #ifndef __GLX_unpack_h__
  2. #define __GLX_unpack_h__
  3. /*
  4. * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
  5. * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice including the dates of first publication and
  15. * either this permission notice or a reference to
  16. * http://oss.sgi.com/projects/FreeB/
  17. * shall be included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  20. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  23. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
  24. * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  25. * SOFTWARE.
  26. *
  27. * Except as contained in this notice, the name of Silicon Graphics, Inc.
  28. * shall not be used in advertising or otherwise to promote the sale, use or
  29. * other dealings in this Software without prior written authorization from
  30. * Silicon Graphics, Inc.
  31. */
  32. #define __GLX_PAD(s) (((s)+3) & (GLuint)~3)
  33. /*
  34. ** Fetch the context-id out of a SingleReq request pointed to by pc.
  35. */
  36. #define __GLX_GET_SINGLE_CONTEXT_TAG(pc) (((xGLXSingleReq*)pc)->contextTag)
  37. #define __GLX_GET_VENDPRIV_CONTEXT_TAG(pc) (((xGLXVendorPrivateReq*)pc)->contextTag)
  38. /*
  39. ** Fetch a double from potentially unaligned memory.
  40. */
  41. #ifdef __GLX_ALIGN64
  42. #define __GLX_MEM_COPY(dst,src,n) memmove(dst,src,n)
  43. #define __GLX_GET_DOUBLE(dst,src) __GLX_MEM_COPY(&dst,src,8)
  44. #else
  45. #define __GLX_GET_DOUBLE(dst,src) (dst) = *((GLdouble*)(src))
  46. #endif
  47. extern void __glXMemInit(void);
  48. extern xGLXSingleReply __glXReply;
  49. #define __GLX_BEGIN_REPLY(size) \
  50. __glXReply.length = __GLX_PAD(size) >> 2; \
  51. __glXReply.type = X_Reply; \
  52. __glXReply.sequenceNumber = client->sequence;
  53. #define __GLX_SEND_HEADER() \
  54. WriteToClient (client, sz_xGLXSingleReply, &__glXReply);
  55. #define __GLX_PUT_RETVAL(a) \
  56. __glXReply.retval = (a);
  57. #define __GLX_PUT_SIZE(a) \
  58. __glXReply.size = (a);
  59. #define __GLX_PUT_RENDERMODE(m) \
  60. __glXReply.pad3 = (m)
  61. /*
  62. ** Get a buffer to hold returned data, with the given alignment. If we have
  63. ** to realloc, allocate size+align, in case the pointer has to be bumped for
  64. ** alignment. The answerBuffer should already be aligned.
  65. **
  66. ** NOTE: the cast (long)res below assumes a long is large enough to hold a
  67. ** pointer.
  68. */
  69. #define __GLX_GET_ANSWER_BUFFER(res,cl,size,align) \
  70. if ((size) > sizeof(answerBuffer)) { \
  71. int bump; \
  72. if ((cl)->returnBufSize < (size)+(align)) { \
  73. (cl)->returnBuf = (GLbyte*)realloc((cl)->returnBuf, \
  74. (size)+(align)); \
  75. if (!(cl)->returnBuf) { \
  76. return BadAlloc; \
  77. } \
  78. (cl)->returnBufSize = (size)+(align); \
  79. } \
  80. res = (char*)cl->returnBuf; \
  81. bump = (long)(res) % (align); \
  82. if (bump) res += (align) - (bump); \
  83. } else { \
  84. res = (char *)answerBuffer; \
  85. }
  86. #define __GLX_PUT_BYTE() \
  87. *(GLbyte *)&__glXReply.pad3 = *(GLbyte *)answer
  88. #define __GLX_PUT_SHORT() \
  89. *(GLshort *)&__glXReply.pad3 = *(GLshort *)answer
  90. #define __GLX_PUT_INT() \
  91. *(GLint *)&__glXReply.pad3 = *(GLint *)answer
  92. #define __GLX_PUT_FLOAT() \
  93. *(GLfloat *)&__glXReply.pad3 = *(GLfloat *)answer
  94. #define __GLX_PUT_DOUBLE() \
  95. *(GLdouble *)&__glXReply.pad3 = *(GLdouble *)answer
  96. #define __GLX_SEND_BYTE_ARRAY(len) \
  97. WriteToClient(client, __GLX_PAD((len)*__GLX_SIZE_INT8), answer)
  98. #define __GLX_SEND_SHORT_ARRAY(len) \
  99. WriteToClient(client, __GLX_PAD((len)*__GLX_SIZE_INT16), answer)
  100. #define __GLX_SEND_INT_ARRAY(len) \
  101. WriteToClient(client, (len)*__GLX_SIZE_INT32, answer)
  102. #define __GLX_SEND_FLOAT_ARRAY(len) \
  103. WriteToClient(client, (len)*__GLX_SIZE_FLOAT32, answer)
  104. #define __GLX_SEND_DOUBLE_ARRAY(len) \
  105. WriteToClient(client, (len)*__GLX_SIZE_FLOAT64, answer)
  106. #define __GLX_SEND_VOID_ARRAY(len) __GLX_SEND_BYTE_ARRAY(len)
  107. #define __GLX_SEND_UBYTE_ARRAY(len) __GLX_SEND_BYTE_ARRAY(len)
  108. #define __GLX_SEND_USHORT_ARRAY(len) __GLX_SEND_SHORT_ARRAY(len)
  109. #define __GLX_SEND_UINT_ARRAY(len) __GLX_SEND_INT_ARRAY(len)
  110. /*
  111. ** PERFORMANCE NOTE:
  112. ** Machine dependent optimizations abound here; these swapping macros can
  113. ** conceivably be replaced with routines that do the job faster.
  114. */
  115. #define __GLX_DECLARE_SWAP_VARIABLES \
  116. GLbyte sw
  117. #define __GLX_DECLARE_SWAP_ARRAY_VARIABLES \
  118. GLbyte *swapPC; \
  119. GLbyte *swapEnd
  120. #define __GLX_SWAP_INT(pc) \
  121. sw = ((GLbyte *)(pc))[0]; \
  122. ((GLbyte *)(pc))[0] = ((GLbyte *)(pc))[3]; \
  123. ((GLbyte *)(pc))[3] = sw; \
  124. sw = ((GLbyte *)(pc))[1]; \
  125. ((GLbyte *)(pc))[1] = ((GLbyte *)(pc))[2]; \
  126. ((GLbyte *)(pc))[2] = sw;
  127. #define __GLX_SWAP_SHORT(pc) \
  128. sw = ((GLbyte *)(pc))[0]; \
  129. ((GLbyte *)(pc))[0] = ((GLbyte *)(pc))[1]; \
  130. ((GLbyte *)(pc))[1] = sw;
  131. #define __GLX_SWAP_DOUBLE(pc) \
  132. sw = ((GLbyte *)(pc))[0]; \
  133. ((GLbyte *)(pc))[0] = ((GLbyte *)(pc))[7]; \
  134. ((GLbyte *)(pc))[7] = sw; \
  135. sw = ((GLbyte *)(pc))[1]; \
  136. ((GLbyte *)(pc))[1] = ((GLbyte *)(pc))[6]; \
  137. ((GLbyte *)(pc))[6] = sw; \
  138. sw = ((GLbyte *)(pc))[2]; \
  139. ((GLbyte *)(pc))[2] = ((GLbyte *)(pc))[5]; \
  140. ((GLbyte *)(pc))[5] = sw; \
  141. sw = ((GLbyte *)(pc))[3]; \
  142. ((GLbyte *)(pc))[3] = ((GLbyte *)(pc))[4]; \
  143. ((GLbyte *)(pc))[4] = sw;
  144. #define __GLX_SWAP_FLOAT(pc) \
  145. sw = ((GLbyte *)(pc))[0]; \
  146. ((GLbyte *)(pc))[0] = ((GLbyte *)(pc))[3]; \
  147. ((GLbyte *)(pc))[3] = sw; \
  148. sw = ((GLbyte *)(pc))[1]; \
  149. ((GLbyte *)(pc))[1] = ((GLbyte *)(pc))[2]; \
  150. ((GLbyte *)(pc))[2] = sw;
  151. #define __GLX_SWAP_INT_ARRAY(pc, count) \
  152. swapPC = ((GLbyte *)(pc)); \
  153. swapEnd = ((GLbyte *)(pc)) + (count)*__GLX_SIZE_INT32;\
  154. while (swapPC < swapEnd) { \
  155. __GLX_SWAP_INT(swapPC); \
  156. swapPC += __GLX_SIZE_INT32; \
  157. }
  158. #define __GLX_SWAP_SHORT_ARRAY(pc, count) \
  159. swapPC = ((GLbyte *)(pc)); \
  160. swapEnd = ((GLbyte *)(pc)) + (count)*__GLX_SIZE_INT16;\
  161. while (swapPC < swapEnd) { \
  162. __GLX_SWAP_SHORT(swapPC); \
  163. swapPC += __GLX_SIZE_INT16; \
  164. }
  165. #define __GLX_SWAP_DOUBLE_ARRAY(pc, count) \
  166. swapPC = ((GLbyte *)(pc)); \
  167. swapEnd = ((GLbyte *)(pc)) + (count)*__GLX_SIZE_FLOAT64;\
  168. while (swapPC < swapEnd) { \
  169. __GLX_SWAP_DOUBLE(swapPC); \
  170. swapPC += __GLX_SIZE_FLOAT64; \
  171. }
  172. #define __GLX_SWAP_FLOAT_ARRAY(pc, count) \
  173. swapPC = ((GLbyte *)(pc)); \
  174. swapEnd = ((GLbyte *)(pc)) + (count)*__GLX_SIZE_FLOAT32;\
  175. while (swapPC < swapEnd) { \
  176. __GLX_SWAP_FLOAT(swapPC); \
  177. swapPC += __GLX_SIZE_FLOAT32; \
  178. }
  179. #define __GLX_SWAP_REPLY_HEADER() \
  180. __GLX_SWAP_SHORT(&__glXReply.sequenceNumber); \
  181. __GLX_SWAP_INT(&__glXReply.length);
  182. #define __GLX_SWAP_REPLY_RETVAL() \
  183. __GLX_SWAP_INT(&__glXReply.retval)
  184. #define __GLX_SWAP_REPLY_SIZE() \
  185. __GLX_SWAP_INT(&__glXReply.size)
  186. #endif /* !__GLX_unpack_h__ */