1706f2543Smrg#ifndef __GLX_unpack_h__
2706f2543Smrg#define __GLX_unpack_h__
3706f2543Smrg
4706f2543Smrg/*
5706f2543Smrg * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
6706f2543Smrg * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
7706f2543Smrg *
8706f2543Smrg * Permission is hereby granted, free of charge, to any person obtaining a
9706f2543Smrg * copy of this software and associated documentation files (the "Software"),
10706f2543Smrg * to deal in the Software without restriction, including without limitation
11706f2543Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12706f2543Smrg * and/or sell copies of the Software, and to permit persons to whom the
13706f2543Smrg * Software is furnished to do so, subject to the following conditions:
14706f2543Smrg *
15706f2543Smrg * The above copyright notice including the dates of first publication and
16706f2543Smrg * either this permission notice or a reference to
17706f2543Smrg * http://oss.sgi.com/projects/FreeB/
18706f2543Smrg * shall be included in all copies or substantial portions of the Software.
19706f2543Smrg *
20706f2543Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21706f2543Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22706f2543Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23706f2543Smrg * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24706f2543Smrg * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
25706f2543Smrg * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26706f2543Smrg * SOFTWARE.
27706f2543Smrg *
28706f2543Smrg * Except as contained in this notice, the name of Silicon Graphics, Inc.
29706f2543Smrg * shall not be used in advertising or otherwise to promote the sale, use or
30706f2543Smrg * other dealings in this Software without prior written authorization from
31706f2543Smrg * Silicon Graphics, Inc.
32706f2543Smrg */
33706f2543Smrg
34706f2543Smrg#define __GLX_PAD(s) (((s)+3) & (GLuint)~3)
35706f2543Smrg
36706f2543Smrg/*
37706f2543Smrg** Fetch the context-id out of a SingleReq request pointed to by pc.
38706f2543Smrg*/
39706f2543Smrg#define __GLX_GET_SINGLE_CONTEXT_TAG(pc) (((xGLXSingleReq*)pc)->contextTag)
40706f2543Smrg#define __GLX_GET_VENDPRIV_CONTEXT_TAG(pc) (((xGLXVendorPrivateReq*)pc)->contextTag)
41706f2543Smrg
42706f2543Smrg/*
43706f2543Smrg** Fetch a double from potentially unaligned memory.
44706f2543Smrg*/
45706f2543Smrg#ifdef __GLX_ALIGN64
46706f2543Smrg#define __GLX_MEM_COPY(dst,src,n)	memmove(dst,src,n)
47706f2543Smrg#define __GLX_GET_DOUBLE(dst,src)	__GLX_MEM_COPY(&dst,src,8)
48706f2543Smrg#else
49706f2543Smrg#define __GLX_GET_DOUBLE(dst,src)	(dst) = *((GLdouble*)(src))
50706f2543Smrg#endif
51706f2543Smrg
52706f2543Smrgextern void __glXMemInit(void);
53706f2543Smrg
54706f2543Smrgextern xGLXSingleReply __glXReply;
55706f2543Smrg
56706f2543Smrg#define __GLX_BEGIN_REPLY(size) \
57706f2543Smrg  	__glXReply.length = __GLX_PAD(size) >> 2;	\
58706f2543Smrg  	__glXReply.type = X_Reply; 			\
59706f2543Smrg  	__glXReply.sequenceNumber = client->sequence;
60706f2543Smrg
61706f2543Smrg#define __GLX_SEND_HEADER() \
62706f2543Smrg	WriteToClient( client, sz_xGLXSingleReply, (char *)&__glXReply);
63706f2543Smrg
64706f2543Smrg#define __GLX_PUT_RETVAL(a) \
65706f2543Smrg  	__glXReply.retval = (a);
66706f2543Smrg
67706f2543Smrg#define __GLX_PUT_SIZE(a) \
68706f2543Smrg  	__glXReply.size = (a);
69706f2543Smrg
70706f2543Smrg#define __GLX_PUT_RENDERMODE(m) \
71706f2543Smrg        __glXReply.pad3 = (m)
72706f2543Smrg
73706f2543Smrg/*
74706f2543Smrg** Get a buffer to hold returned data, with the given alignment.  If we have
75706f2543Smrg** to realloc, allocate size+align, in case the pointer has to be bumped for
76706f2543Smrg** alignment.  The answerBuffer should already be aligned.
77706f2543Smrg**
78706f2543Smrg** NOTE: the cast (long)res below assumes a long is large enough to hold a
79706f2543Smrg** pointer.
80706f2543Smrg*/
81706f2543Smrg#define __GLX_GET_ANSWER_BUFFER(res,cl,size,align)			 \
82706f2543Smrg    if ((size) > sizeof(answerBuffer)) {				 \
83706f2543Smrg	int bump;							 \
84706f2543Smrg	if ((cl)->returnBufSize < (size)+(align)) {			 \
85706f2543Smrg	    (cl)->returnBuf = (GLbyte*)realloc((cl)->returnBuf,		 \
86706f2543Smrg						(size)+(align));         \
87706f2543Smrg	    if (!(cl)->returnBuf) {					 \
88706f2543Smrg		return BadAlloc;					 \
89706f2543Smrg	    }								 \
90706f2543Smrg	    (cl)->returnBufSize = (size)+(align);			 \
91706f2543Smrg	}								 \
92706f2543Smrg	res = (char*)cl->returnBuf;					 \
93706f2543Smrg	bump = (long)(res) % (align);					 \
94706f2543Smrg	if (bump) res += (align) - (bump);				 \
95706f2543Smrg    } else {								 \
96706f2543Smrg	res = (char *)answerBuffer;					 \
97706f2543Smrg    }
98706f2543Smrg
99706f2543Smrg#define __GLX_PUT_BYTE() \
100706f2543Smrg  	*(GLbyte *)&__glXReply.pad3 = *(GLbyte *)answer
101706f2543Smrg
102706f2543Smrg#define __GLX_PUT_SHORT() \
103706f2543Smrg  	*(GLshort *)&__glXReply.pad3 = *(GLshort *)answer
104706f2543Smrg
105706f2543Smrg#define __GLX_PUT_INT() \
106706f2543Smrg  	*(GLint *)&__glXReply.pad3 = *(GLint *)answer
107706f2543Smrg
108706f2543Smrg#define __GLX_PUT_FLOAT() \
109706f2543Smrg  	*(GLfloat *)&__glXReply.pad3 = *(GLfloat *)answer
110706f2543Smrg
111706f2543Smrg#define __GLX_PUT_DOUBLE() \
112706f2543Smrg  	*(GLdouble *)&__glXReply.pad3 = *(GLdouble *)answer
113706f2543Smrg
114706f2543Smrg#define __GLX_SEND_BYTE_ARRAY(len) \
115706f2543Smrg	WriteToClient(client, __GLX_PAD((len)*__GLX_SIZE_INT8), (char *)answer)
116706f2543Smrg
117706f2543Smrg#define __GLX_SEND_SHORT_ARRAY(len) \
118706f2543Smrg	WriteToClient(client, __GLX_PAD((len)*__GLX_SIZE_INT16), (char *)answer)
119706f2543Smrg
120706f2543Smrg#define __GLX_SEND_INT_ARRAY(len) \
121706f2543Smrg	WriteToClient(client, (len)*__GLX_SIZE_INT32, (char *)answer)
122706f2543Smrg
123706f2543Smrg#define __GLX_SEND_FLOAT_ARRAY(len) \
124706f2543Smrg	WriteToClient(client, (len)*__GLX_SIZE_FLOAT32, (char *)answer)
125706f2543Smrg
126706f2543Smrg#define __GLX_SEND_DOUBLE_ARRAY(len) \
127706f2543Smrg	WriteToClient(client, (len)*__GLX_SIZE_FLOAT64, (char *)answer)
128706f2543Smrg
129706f2543Smrg
130706f2543Smrg#define __GLX_SEND_VOID_ARRAY(len)  __GLX_SEND_BYTE_ARRAY(len)
131706f2543Smrg#define __GLX_SEND_UBYTE_ARRAY(len)  __GLX_SEND_BYTE_ARRAY(len)
132706f2543Smrg#define __GLX_SEND_USHORT_ARRAY(len) __GLX_SEND_SHORT_ARRAY(len)
133706f2543Smrg#define __GLX_SEND_UINT_ARRAY(len)  __GLX_SEND_INT_ARRAY(len)
134706f2543Smrg
135706f2543Smrg/*
136706f2543Smrg** PERFORMANCE NOTE:
137706f2543Smrg** Machine dependent optimizations abound here; these swapping macros can
138706f2543Smrg** conceivably be replaced with routines that do the job faster.
139706f2543Smrg*/
140706f2543Smrg#define __GLX_DECLARE_SWAP_VARIABLES \
141706f2543Smrg	GLbyte sw
142706f2543Smrg
143706f2543Smrg#define __GLX_DECLARE_SWAP_ARRAY_VARIABLES \
144706f2543Smrg  	GLbyte *swapPC;		\
145706f2543Smrg  	GLbyte *swapEnd
146706f2543Smrg
147706f2543Smrg
148706f2543Smrg#define __GLX_SWAP_INT(pc) 			\
149706f2543Smrg  	sw = ((GLbyte *)(pc))[0]; 		\
150706f2543Smrg  	((GLbyte *)(pc))[0] = ((GLbyte *)(pc))[3]; 	\
151706f2543Smrg  	((GLbyte *)(pc))[3] = sw; 		\
152706f2543Smrg  	sw = ((GLbyte *)(pc))[1]; 		\
153706f2543Smrg  	((GLbyte *)(pc))[1] = ((GLbyte *)(pc))[2]; 	\
154706f2543Smrg  	((GLbyte *)(pc))[2] = sw;
155706f2543Smrg
156706f2543Smrg#define __GLX_SWAP_SHORT(pc) \
157706f2543Smrg  	sw = ((GLbyte *)(pc))[0]; 		\
158706f2543Smrg  	((GLbyte *)(pc))[0] = ((GLbyte *)(pc))[1]; 	\
159706f2543Smrg  	((GLbyte *)(pc))[1] = sw;
160706f2543Smrg
161706f2543Smrg#define __GLX_SWAP_DOUBLE(pc) \
162706f2543Smrg  	sw = ((GLbyte *)(pc))[0]; 		\
163706f2543Smrg  	((GLbyte *)(pc))[0] = ((GLbyte *)(pc))[7]; 	\
164706f2543Smrg  	((GLbyte *)(pc))[7] = sw; 		\
165706f2543Smrg  	sw = ((GLbyte *)(pc))[1]; 		\
166706f2543Smrg  	((GLbyte *)(pc))[1] = ((GLbyte *)(pc))[6]; 	\
167706f2543Smrg  	((GLbyte *)(pc))[6] = sw;			\
168706f2543Smrg  	sw = ((GLbyte *)(pc))[2]; 		\
169706f2543Smrg  	((GLbyte *)(pc))[2] = ((GLbyte *)(pc))[5]; 	\
170706f2543Smrg  	((GLbyte *)(pc))[5] = sw;			\
171706f2543Smrg  	sw = ((GLbyte *)(pc))[3]; 		\
172706f2543Smrg  	((GLbyte *)(pc))[3] = ((GLbyte *)(pc))[4]; 	\
173706f2543Smrg  	((GLbyte *)(pc))[4] = sw;
174706f2543Smrg
175706f2543Smrg#define __GLX_SWAP_FLOAT(pc) \
176706f2543Smrg  	sw = ((GLbyte *)(pc))[0]; 		\
177706f2543Smrg  	((GLbyte *)(pc))[0] = ((GLbyte *)(pc))[3]; 	\
178706f2543Smrg  	((GLbyte *)(pc))[3] = sw; 		\
179706f2543Smrg  	sw = ((GLbyte *)(pc))[1]; 		\
180706f2543Smrg  	((GLbyte *)(pc))[1] = ((GLbyte *)(pc))[2]; 	\
181706f2543Smrg  	((GLbyte *)(pc))[2] = sw;
182706f2543Smrg
183706f2543Smrg#define __GLX_SWAP_INT_ARRAY(pc, count) \
184706f2543Smrg  	swapPC = ((GLbyte *)(pc));		\
185706f2543Smrg  	swapEnd = ((GLbyte *)(pc)) + (count)*__GLX_SIZE_INT32;\
186706f2543Smrg  	while (swapPC < swapEnd) {		\
187706f2543Smrg	    __GLX_SWAP_INT(swapPC);		\
188706f2543Smrg	    swapPC += __GLX_SIZE_INT32;		\
189706f2543Smrg	}
190706f2543Smrg
191706f2543Smrg#define __GLX_SWAP_SHORT_ARRAY(pc, count) \
192706f2543Smrg  	swapPC = ((GLbyte *)(pc));		\
193706f2543Smrg  	swapEnd = ((GLbyte *)(pc)) + (count)*__GLX_SIZE_INT16;\
194706f2543Smrg  	while (swapPC < swapEnd) {		\
195706f2543Smrg	    __GLX_SWAP_SHORT(swapPC);		\
196706f2543Smrg	    swapPC += __GLX_SIZE_INT16;		\
197706f2543Smrg	}
198706f2543Smrg
199706f2543Smrg#define __GLX_SWAP_DOUBLE_ARRAY(pc, count) \
200706f2543Smrg  	swapPC = ((GLbyte *)(pc));		\
201706f2543Smrg  	swapEnd = ((GLbyte *)(pc)) + (count)*__GLX_SIZE_FLOAT64;\
202706f2543Smrg  	while (swapPC < swapEnd) {		\
203706f2543Smrg	    __GLX_SWAP_DOUBLE(swapPC);		\
204706f2543Smrg	    swapPC += __GLX_SIZE_FLOAT64;	\
205706f2543Smrg	}
206706f2543Smrg
207706f2543Smrg#define __GLX_SWAP_FLOAT_ARRAY(pc, count) \
208706f2543Smrg  	swapPC = ((GLbyte *)(pc));		\
209706f2543Smrg  	swapEnd = ((GLbyte *)(pc)) + (count)*__GLX_SIZE_FLOAT32;\
210706f2543Smrg  	while (swapPC < swapEnd) {		\
211706f2543Smrg	    __GLX_SWAP_FLOAT(swapPC);		\
212706f2543Smrg	    swapPC += __GLX_SIZE_FLOAT32;	\
213706f2543Smrg	}
214706f2543Smrg
215706f2543Smrg#define __GLX_SWAP_REPLY_HEADER() \
216706f2543Smrg  	__GLX_SWAP_SHORT(&__glXReply.sequenceNumber); \
217706f2543Smrg  	__GLX_SWAP_INT(&__glXReply.length);
218706f2543Smrg
219706f2543Smrg#define __GLX_SWAP_REPLY_RETVAL() \
220706f2543Smrg  	__GLX_SWAP_INT(&__glXReply.retval)
221706f2543Smrg
222706f2543Smrg#define __GLX_SWAP_REPLY_SIZE() \
223706f2543Smrg  	__GLX_SWAP_INT(&__glXReply.size)
224706f2543Smrg
225706f2543Smrg#endif /* !__GLX_unpack_h__ */
226706f2543Smrg
227706f2543Smrg
228706f2543Smrg
229706f2543Smrg
230706f2543Smrg
231