1/*
2 * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
3 * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice including the dates of first publication and
13 * either this permission notice or a reference to
14 * http://oss.sgi.com/projects/FreeB/
15 * shall be included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
22 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Except as contained in this notice, the name of Silicon Graphics, Inc.
26 * shall not be used in advertising or otherwise to promote the sale, use or
27 * other dealings in this Software without prior written authorization from
28 * Silicon Graphics, Inc.
29 */
30
31#ifdef HAVE_DIX_CONFIG_H
32#include <dix-config.h>
33#endif
34
35#include "glxserver.h"
36#include "unpack.h"
37#include "indirect_size.h"
38#include "indirect_dispatch.h"
39#include "glapitable.h"
40#include "glapi.h"
41#include "glthread.h"
42#include "dispatch.h"
43
44
45void __glXDispSwap_Map1f(GLbyte *pc)
46{
47    GLint order, k;
48    GLfloat u1, u2, *points;
49    GLenum target;
50    GLint compsize;
51    __GLX_DECLARE_SWAP_VARIABLES;
52    __GLX_DECLARE_SWAP_ARRAY_VARIABLES;
53
54    __GLX_SWAP_INT(pc + 0);
55    __GLX_SWAP_INT(pc + 12);
56    __GLX_SWAP_FLOAT(pc + 4);
57    __GLX_SWAP_FLOAT(pc + 8);
58
59    target = *(GLenum *)(pc + 0);
60    order = *(GLint *)(pc + 12);
61    u1 = *(GLfloat *)(pc + 4);
62    u2 = *(GLfloat *)(pc + 8);
63    points = (GLfloat *)(pc + 16);
64    k = __glMap1f_size(target);
65
66    if (order <= 0 || k < 0) {
67	/* Erroneous command. */
68	compsize = 0;
69    } else {
70	compsize = order * k;
71    }
72    __GLX_SWAP_FLOAT_ARRAY(points, compsize);
73
74    CALL_Map1f( GET_DISPATCH(), (target, u1, u2, k, order, points) );
75}
76
77void __glXDispSwap_Map2f(GLbyte *pc)
78{
79    GLint uorder, vorder, ustride, vstride, k;
80    GLfloat u1, u2, v1, v2, *points;
81    GLenum target;
82    GLint compsize;
83    __GLX_DECLARE_SWAP_VARIABLES;
84    __GLX_DECLARE_SWAP_ARRAY_VARIABLES;
85
86    __GLX_SWAP_INT(pc + 0);
87    __GLX_SWAP_INT(pc + 12);
88    __GLX_SWAP_INT(pc + 24);
89    __GLX_SWAP_FLOAT(pc + 4);
90    __GLX_SWAP_FLOAT(pc + 8);
91    __GLX_SWAP_FLOAT(pc + 16);
92    __GLX_SWAP_FLOAT(pc + 20);
93
94    target = *(GLenum *)(pc + 0);
95    uorder = *(GLint *)(pc + 12);
96    vorder = *(GLint *)(pc + 24);
97    u1 = *(GLfloat *)(pc + 4);
98    u2 = *(GLfloat *)(pc + 8);
99    v1 = *(GLfloat *)(pc + 16);
100    v2 = *(GLfloat *)(pc + 20);
101    points = (GLfloat *)(pc + 28);
102
103    k = __glMap2f_size(target);
104    ustride = vorder * k;
105    vstride = k;
106
107    if (vorder <= 0 || uorder <= 0 || k < 0) {
108	/* Erroneous command. */
109	compsize = 0;
110    } else {
111	compsize = uorder * vorder * k;
112    }
113    __GLX_SWAP_FLOAT_ARRAY(points, compsize);
114
115    CALL_Map2f( GET_DISPATCH(), (target, u1, u2, ustride, uorder, v1, v2, vstride, vorder, points) );
116}
117
118void __glXDispSwap_Map1d(GLbyte *pc)
119{
120    GLint order, k, compsize;
121    GLenum target;
122    GLdouble u1, u2, *points;
123    __GLX_DECLARE_SWAP_VARIABLES;
124    __GLX_DECLARE_SWAP_ARRAY_VARIABLES;
125
126    __GLX_SWAP_DOUBLE(pc + 0);
127    __GLX_SWAP_DOUBLE(pc + 8);
128    __GLX_SWAP_INT(pc + 16);
129    __GLX_SWAP_INT(pc + 20);
130
131    target = *(GLenum*) (pc + 16);
132    order = *(GLint*) (pc + 20);
133    k = __glMap1d_size(target);
134    if (order <= 0 || k < 0) {
135	/* Erroneous command. */
136	compsize = 0;
137    } else {
138	compsize = order * k;
139    }
140    __GLX_GET_DOUBLE(u1,pc);
141    __GLX_GET_DOUBLE(u2,pc+8);
142    __GLX_SWAP_DOUBLE_ARRAY(pc+24, compsize);
143    pc += 24;
144
145#ifdef __GLX_ALIGN64
146    if (((unsigned long)pc) & 7) {
147	/*
148	** Copy the doubles up 4 bytes, trashing the command but aligning
149	** the data in the process
150	*/
151	__GLX_MEM_COPY(pc-4, pc, compsize*8);
152	points = (GLdouble*) (pc - 4);
153    } else {
154	points = (GLdouble*) pc;
155    }
156#else
157    points = (GLdouble*) pc;
158#endif
159    CALL_Map1d( GET_DISPATCH(), (target, u1, u2, k, order, points) );
160}
161
162void __glXDispSwap_Map2d(GLbyte *pc)
163{
164    GLdouble u1, u2, v1, v2, *points;
165    GLint uorder, vorder, ustride, vstride, k, compsize;
166    GLenum target;
167    __GLX_DECLARE_SWAP_VARIABLES;
168    __GLX_DECLARE_SWAP_ARRAY_VARIABLES;
169
170    __GLX_SWAP_DOUBLE(pc + 0);
171    __GLX_SWAP_DOUBLE(pc + 8);
172    __GLX_SWAP_DOUBLE(pc + 16);
173    __GLX_SWAP_DOUBLE(pc + 24);
174    __GLX_SWAP_INT(pc + 32);
175    __GLX_SWAP_INT(pc + 36);
176    __GLX_SWAP_INT(pc + 40);
177
178    target = *(GLenum *)(pc + 32);
179    uorder = *(GLint *)(pc + 36);
180    vorder = *(GLint *)(pc + 40);
181    k = __glMap2d_size(target);
182    if (vorder <= 0 || uorder <= 0 || k < 0) {
183	/* Erroneous command. */
184	compsize = 0;
185    } else {
186	compsize = uorder * vorder * k;
187    }
188    __GLX_GET_DOUBLE(u1,pc);
189    __GLX_GET_DOUBLE(u2,pc+8);
190    __GLX_GET_DOUBLE(v1,pc+16);
191    __GLX_GET_DOUBLE(v2,pc+24);
192    __GLX_SWAP_DOUBLE_ARRAY(pc+44, compsize);
193    pc += 44;
194    ustride = vorder * k;
195    vstride = k;
196
197#ifdef __GLX_ALIGN64
198    if (((unsigned long)pc) & 7) {
199	/*
200	** Copy the doubles up 4 bytes, trashing the command but aligning
201	** the data in the process
202	*/
203	__GLX_MEM_COPY(pc-4, pc, compsize*8);
204	points = (GLdouble*) (pc - 4);
205    } else {
206	points = (GLdouble*) pc;
207    }
208#else
209    points = (GLdouble*) pc;
210#endif
211    CALL_Map2d( GET_DISPATCH(), (target, u1, u2, ustride, uorder, v1, v2, vstride, vorder, points) );
212}
213
214static void swapArray(GLint numVals, GLenum datatype,
215                      GLint stride, GLint numVertexes, GLbyte *pc)
216{
217    int i,j;
218    __GLX_DECLARE_SWAP_VARIABLES;
219
220    switch (datatype) {
221      case GL_BYTE:
222      case GL_UNSIGNED_BYTE:
223	/* don't need to swap */
224	return;
225      case GL_SHORT:
226      case GL_UNSIGNED_SHORT:
227	for (i=0; i<numVertexes; i++) {
228	    GLshort *pVal = (GLshort *) pc;
229	    for (j=0; j<numVals; j++) {
230		__GLX_SWAP_SHORT(&pVal[j]);
231	    }
232	    pc += stride;
233	}
234	break;
235      case GL_INT:
236      case GL_UNSIGNED_INT:
237	for (i=0; i<numVertexes; i++) {
238	    GLint *pVal = (GLint *) pc;
239	    for (j=0; j<numVals; j++) {
240		__GLX_SWAP_INT(&pVal[j]);
241	    }
242	    pc += stride;
243	}
244	break;
245      case GL_FLOAT:
246	for (i=0; i<numVertexes; i++) {
247	    GLfloat *pVal = (GLfloat *) pc;
248	    for (j=0; j<numVals; j++) {
249		__GLX_SWAP_FLOAT(&pVal[j]);
250	    }
251	    pc += stride;
252	}
253	break;
254      case GL_DOUBLE:
255	for (i=0; i<numVertexes; i++) {
256	    GLdouble *pVal = (GLdouble *) pc;
257	    for (j=0; j<numVals; j++) {
258		__GLX_SWAP_DOUBLE(&pVal[j]);
259	    }
260	    pc += stride;
261	}
262	break;
263      default:
264	return;
265    }
266}
267
268void __glXDispSwap_DrawArrays(GLbyte *pc)
269{
270    __GLXdispatchDrawArraysHeader *hdr = (__GLXdispatchDrawArraysHeader *)pc;
271    __GLXdispatchDrawArraysComponentHeader *compHeader;
272    GLint numVertexes = hdr->numVertexes;
273    GLint numComponents = hdr->numComponents;
274    GLenum primType = hdr->primType;
275    GLint stride = 0;
276    int i;
277    __GLX_DECLARE_SWAP_VARIABLES;
278
279    __GLX_SWAP_INT(&numVertexes);
280    __GLX_SWAP_INT(&numComponents);
281    __GLX_SWAP_INT(&primType);
282
283    pc += sizeof(__GLXdispatchDrawArraysHeader);
284    compHeader = (__GLXdispatchDrawArraysComponentHeader *) pc;
285
286    /* compute stride (same for all component arrays) */
287    for (i=0; i<numComponents; i++) {
288	GLenum datatype = compHeader[i].datatype;
289	GLint numVals = compHeader[i].numVals;
290	GLenum component = compHeader[i].component;
291
292	__GLX_SWAP_INT(&datatype);
293	__GLX_SWAP_INT(&numVals);
294	__GLX_SWAP_INT(&component);
295
296        stride += __GLX_PAD(numVals * __glXTypeSize(datatype));
297    }
298
299    pc += numComponents * sizeof(__GLXdispatchDrawArraysComponentHeader);
300
301    /* set up component arrays */
302    for (i=0; i<numComponents; i++) {
303	GLenum datatype = compHeader[i].datatype;
304	GLint numVals = compHeader[i].numVals;
305	GLenum component = compHeader[i].component;
306
307	__GLX_SWAP_INT(&datatype);
308	__GLX_SWAP_INT(&numVals);
309	__GLX_SWAP_INT(&component);
310
311	swapArray(numVals, datatype, stride, numVertexes, pc);
312
313        switch (component) {
314          case GL_VERTEX_ARRAY:
315	    CALL_EnableClientState( GET_DISPATCH(), (GL_VERTEX_ARRAY) );
316	    CALL_VertexPointer( GET_DISPATCH(), (numVals, datatype, stride, pc) );
317            break;
318          case GL_NORMAL_ARRAY:
319	    CALL_EnableClientState( GET_DISPATCH(), (GL_NORMAL_ARRAY) );
320	    CALL_NormalPointer( GET_DISPATCH(), (datatype, stride, pc) );
321            break;
322          case GL_COLOR_ARRAY:
323	    CALL_EnableClientState( GET_DISPATCH(), (GL_COLOR_ARRAY) );
324	    CALL_ColorPointer( GET_DISPATCH(), (numVals, datatype, stride, pc) );
325            break;
326          case GL_INDEX_ARRAY:
327	    CALL_EnableClientState( GET_DISPATCH(), (GL_INDEX_ARRAY) );
328	    CALL_IndexPointer( GET_DISPATCH(), (datatype, stride, pc) );
329            break;
330          case GL_TEXTURE_COORD_ARRAY:
331	    CALL_EnableClientState( GET_DISPATCH(), (GL_TEXTURE_COORD_ARRAY) );
332	    CALL_TexCoordPointer( GET_DISPATCH(), (numVals, datatype, stride, pc) );
333            break;
334          case GL_EDGE_FLAG_ARRAY:
335	    CALL_EnableClientState( GET_DISPATCH(), (GL_EDGE_FLAG_ARRAY) );
336	    CALL_EdgeFlagPointer( GET_DISPATCH(), (stride, (const GLboolean *)pc) );
337            break;
338          case GL_SECONDARY_COLOR_ARRAY:
339	    CALL_EnableClientState( GET_DISPATCH(), (GL_SECONDARY_COLOR_ARRAY) );
340	    CALL_SecondaryColorPointerEXT( GET_DISPATCH(), (numVals, datatype, stride, pc) );
341            break;
342          case GL_FOG_COORD_ARRAY:
343	    CALL_EnableClientState( GET_DISPATCH(), (GL_FOG_COORD_ARRAY) );
344	    CALL_FogCoordPointerEXT( GET_DISPATCH(), (datatype, stride, pc) );
345            break;
346          default:
347            break;
348	}
349
350        pc += __GLX_PAD(numVals * __glXTypeSize(datatype));
351    }
352
353    CALL_DrawArrays( GET_DISPATCH(), (primType, 0, numVertexes) );
354
355    /* turn off anything we might have turned on */
356    CALL_DisableClientState( GET_DISPATCH(), (GL_VERTEX_ARRAY) );
357    CALL_DisableClientState( GET_DISPATCH(), (GL_NORMAL_ARRAY) );
358    CALL_DisableClientState( GET_DISPATCH(), (GL_COLOR_ARRAY) );
359    CALL_DisableClientState( GET_DISPATCH(), (GL_INDEX_ARRAY) );
360    CALL_DisableClientState( GET_DISPATCH(), (GL_TEXTURE_COORD_ARRAY) );
361    CALL_DisableClientState( GET_DISPATCH(), (GL_EDGE_FLAG_ARRAY) );
362    CALL_DisableClientState( GET_DISPATCH(), (GL_SECONDARY_COLOR_ARRAY) );
363    CALL_DisableClientState( GET_DISPATCH(), (GL_FOG_COORD_ARRAY) );
364}
365