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