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#include "glxserver.h"
32#include "unpack.h"
33#include "g_disptab.h"
34
35GLint __glEvalComputeK(GLenum target)
36{
37    switch (target) {
38    case GL_MAP1_VERTEX_4:
39    case GL_MAP1_COLOR_4:
40    case GL_MAP1_TEXTURE_COORD_4:
41    case GL_MAP2_VERTEX_4:
42    case GL_MAP2_COLOR_4:
43    case GL_MAP2_TEXTURE_COORD_4:
44	return 4;
45    case GL_MAP1_VERTEX_3:
46    case GL_MAP1_TEXTURE_COORD_3:
47    case GL_MAP1_NORMAL:
48    case GL_MAP2_VERTEX_3:
49    case GL_MAP2_TEXTURE_COORD_3:
50    case GL_MAP2_NORMAL:
51	return 3;
52    case GL_MAP1_TEXTURE_COORD_2:
53    case GL_MAP2_TEXTURE_COORD_2:
54	return 2;
55    case GL_MAP1_TEXTURE_COORD_1:
56    case GL_MAP2_TEXTURE_COORD_1:
57    case GL_MAP1_INDEX:
58    case GL_MAP2_INDEX:
59	return 1;
60    default:
61	return 0;
62    }
63}
64
65void __glXDispSwap_Map1f(GLbyte *pc)
66{
67    GLint order, k;
68    GLfloat u1, u2, *points;
69    GLenum target;
70    GLint compsize;
71    __GLX_DECLARE_SWAP_VARIABLES;
72    __GLX_DECLARE_SWAP_ARRAY_VARIABLES;
73
74    __GLX_SWAP_INT(pc + 0);
75    __GLX_SWAP_INT(pc + 12);
76    __GLX_SWAP_FLOAT(pc + 4);
77    __GLX_SWAP_FLOAT(pc + 8);
78
79    target = *(GLenum *)(pc + 0);
80    order = *(GLint *)(pc + 12);
81    u1 = *(GLfloat *)(pc + 4);
82    u2 = *(GLfloat *)(pc + 8);
83    points = (GLfloat *)(pc + 16);
84    k = __glEvalComputeK(target);
85
86    if (order <= 0 || k < 0) {
87	/* Erroneous command. */
88	compsize = 0;
89    } else {
90	compsize = order * k;
91    }
92    __GLX_SWAP_FLOAT_ARRAY(points, compsize);
93
94}
95
96void __glXDispSwap_Map2f(GLbyte *pc)
97{
98    GLint uorder, vorder, ustride, vstride, k;
99    GLfloat u1, u2, v1, v2, *points;
100    GLenum target;
101    GLint compsize;
102    __GLX_DECLARE_SWAP_VARIABLES;
103    __GLX_DECLARE_SWAP_ARRAY_VARIABLES;
104
105    __GLX_SWAP_INT(pc + 0);
106    __GLX_SWAP_INT(pc + 12);
107    __GLX_SWAP_INT(pc + 24);
108    __GLX_SWAP_FLOAT(pc + 4);
109    __GLX_SWAP_FLOAT(pc + 8);
110    __GLX_SWAP_FLOAT(pc + 16);
111    __GLX_SWAP_FLOAT(pc + 20);
112
113    target = *(GLenum *)(pc + 0);
114    uorder = *(GLint *)(pc + 12);
115    vorder = *(GLint *)(pc + 24);
116    u1 = *(GLfloat *)(pc + 4);
117    u2 = *(GLfloat *)(pc + 8);
118    v1 = *(GLfloat *)(pc + 16);
119    v2 = *(GLfloat *)(pc + 20);
120    points = (GLfloat *)(pc + 28);
121
122    k = __glEvalComputeK(target);
123    ustride = vorder * k;
124    vstride = k;
125
126    if (vorder <= 0 || uorder <= 0 || k < 0) {
127	/* Erroneous command. */
128	compsize = 0;
129    } else {
130	compsize = uorder * vorder * k;
131    }
132    __GLX_SWAP_FLOAT_ARRAY(points, compsize);
133
134}
135
136void __glXDispSwap_Map1d(GLbyte *pc)
137{
138    GLint order, k, compsize;
139    GLenum target;
140    GLdouble u1, u2, *points;
141    __GLX_DECLARE_SWAP_VARIABLES;
142    __GLX_DECLARE_SWAP_ARRAY_VARIABLES;
143
144    __GLX_SWAP_DOUBLE(pc + 0);
145    __GLX_SWAP_DOUBLE(pc + 8);
146    __GLX_SWAP_INT(pc + 16);
147    __GLX_SWAP_INT(pc + 20);
148
149    target = *(GLenum*) (pc + 16);
150    order = *(GLint*) (pc + 20);
151    k = __glEvalComputeK(target);
152    if (order <= 0 || k < 0) {
153	/* Erroneous command. */
154	compsize = 0;
155    } else {
156	compsize = order * k;
157    }
158    __GLX_GET_DOUBLE(u1,pc);
159    __GLX_GET_DOUBLE(u2,pc+8);
160    __GLX_SWAP_DOUBLE_ARRAY(pc+24, compsize);
161    pc += 24;
162
163#ifdef __GLX_ALIGN64
164    if (((unsigned long)pc) & 7) {
165	/*
166	** Copy the doubles up 4 bytes, trashing the command but aligning
167	** the data in the process
168	*/
169	__GLX_MEM_COPY(pc-4, pc, compsize*8);
170	points = (GLdouble*) (pc - 4);
171    } else {
172	points = (GLdouble*) pc;
173    }
174#else
175    points = (GLdouble*) pc;
176#endif
177}
178
179void __glXDispSwap_Map2d(GLbyte *pc)
180{
181    GLdouble u1, u2, v1, v2, *points;
182    GLint uorder, vorder, ustride, vstride, k, compsize;
183    GLenum target;
184    __GLX_DECLARE_SWAP_VARIABLES;
185    __GLX_DECLARE_SWAP_ARRAY_VARIABLES;
186
187    __GLX_SWAP_DOUBLE(pc + 0);
188    __GLX_SWAP_DOUBLE(pc + 8);
189    __GLX_SWAP_DOUBLE(pc + 16);
190    __GLX_SWAP_DOUBLE(pc + 24);
191    __GLX_SWAP_INT(pc + 32);
192    __GLX_SWAP_INT(pc + 36);
193    __GLX_SWAP_INT(pc + 40);
194
195    target = *(GLenum *)(pc + 32);
196    uorder = *(GLint *)(pc + 36);
197    vorder = *(GLint *)(pc + 40);
198    k = __glEvalComputeK(target);
199    if (vorder <= 0 || uorder <= 0 || k < 0) {
200	/* Erroneous command. */
201	compsize = 0;
202    } else {
203	compsize = uorder * vorder * k;
204    }
205    __GLX_GET_DOUBLE(u1,pc);
206    __GLX_GET_DOUBLE(u2,pc+8);
207    __GLX_GET_DOUBLE(v1,pc+16);
208    __GLX_GET_DOUBLE(v2,pc+24);
209    __GLX_SWAP_DOUBLE_ARRAY(pc+44, compsize);
210    pc += 44;
211    ustride = vorder * k;
212    vstride = k;
213
214#ifdef __GLX_ALIGN64
215    if (((unsigned long)pc) & 7) {
216	/*
217	** Copy the doubles up 4 bytes, trashing the command but aligning
218	** the data in the process
219	*/
220	__GLX_MEM_COPY(pc-4, pc, compsize*8);
221	points = (GLdouble*) (pc - 4);
222    } else {
223	points = (GLdouble*) pc;
224    }
225#else
226    points = (GLdouble*) pc;
227#endif
228}
229
230void __glXDispSwap_CallLists(GLbyte *pc)
231{
232    GLenum type;
233    GLsizei n;
234    __GLX_DECLARE_SWAP_VARIABLES;
235    __GLX_DECLARE_SWAP_ARRAY_VARIABLES;
236
237    __GLX_SWAP_INT(pc + 4);
238    __GLX_SWAP_INT(pc + 0);
239    type = *(GLenum *)(pc + 4);
240    n = *(GLsizei *)(pc + 0);
241
242    switch (type) {
243      case GL_BYTE:
244      case GL_UNSIGNED_BYTE:
245      case GL_2_BYTES:
246      case GL_3_BYTES:
247      case GL_4_BYTES:
248	break;
249      case GL_SHORT:
250      case GL_UNSIGNED_SHORT:
251	__GLX_SWAP_SHORT_ARRAY(pc+8, n);
252	break;
253      case GL_INT:
254      case GL_UNSIGNED_INT:
255	__GLX_SWAP_INT_ARRAY(pc+8, n);
256	break;
257      case GL_FLOAT:
258	__GLX_SWAP_FLOAT_ARRAY(pc+8, n);
259	break;
260    }
261
262}
263
264void __glXDispSwap_DrawArrays(GLbyte *pc)
265{
266    __GLXdispatchDrawArraysHeader *hdr = (__GLXdispatchDrawArraysHeader *)pc;
267    __GLXdispatchDrawArraysComponentHeader *compHeader;
268    int i;
269    __GLX_DECLARE_SWAP_VARIABLES;
270
271    __GLX_SWAP_INT(&hdr->numVertexes);
272    __GLX_SWAP_INT(&hdr->numComponents);
273    __GLX_SWAP_INT(&hdr->primType);
274
275    pc += sizeof(__GLXdispatchDrawArraysHeader);
276    compHeader = (__GLXdispatchDrawArraysComponentHeader *) pc;
277
278    /* compute stride (same for all component arrays) */
279    for (i=0; i<hdr->numComponents; i++) {
280	__GLX_SWAP_INT(&compHeader[i].datatype);
281	__GLX_SWAP_INT(&compHeader[i].numVals);
282	__GLX_SWAP_INT(&compHeader[i].component);
283
284    }
285
286}
287