summaryrefslogtreecommitdiff
path: root/src/mesa/tnl/t_vtx_x86.c
blob: 59a869879a516289ede3480c418fe69fa6d7ce39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/**************************************************************************

Copyright 2004 Tungsten Graphics Inc., Cedar Park, Texas.

All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
on the rights to use, copy, modify, merge, publish, distribute, sub
license, and/or sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice (including the next
paragraph) shall be included in all copies or substantial portions of the
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
ATI, TUNGSTEN GRAPHICS AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.

**************************************************************************/

/*
 * Authors:
 *   Keith Whitwell <keith@tungstengraphics.com>
 *   Daniel Borca <dborca@yahoo.com>
 */


#include "glheader.h"
#include "context.h"
#include "macros.h"
#include "vtxfmt.h"
#include "dlist.h"
#include "state.h"
#include "light.h"
#include "api_arrayelt.h"
#include "api_noop.h"
#include "t_vtx_api.h"
#include "simple_list.h"


#if defined(USE_X86_ASM) && !defined(HAVE_NONSTANDARD_GLAPIENTRY)

#define EXTERN( FUNC )		\
extern const char *FUNC;	\
extern const char *FUNC##_end

EXTERN( _tnl_x86_Attribute1fv );
EXTERN( _tnl_x86_Attribute2fv );
EXTERN( _tnl_x86_Attribute3fv );
EXTERN( _tnl_x86_Attribute4fv );
EXTERN( _tnl_x86_Vertex1fv );
EXTERN( _tnl_x86_Vertex2fv );
EXTERN( _tnl_x86_Vertex3fv );
EXTERN( _tnl_x86_Vertex4fv );

EXTERN( _tnl_x86_dispatch_attrf1 );
EXTERN( _tnl_x86_dispatch_attrf2 );
EXTERN( _tnl_x86_dispatch_attrf3 );
EXTERN( _tnl_x86_dispatch_attrf4 );
EXTERN( _tnl_x86_dispatch_attrfv );
EXTERN( _tnl_x86_dispatch_multitexcoordf1 );
EXTERN( _tnl_x86_dispatch_multitexcoordf2 );
EXTERN( _tnl_x86_dispatch_multitexcoordf3 );
EXTERN( _tnl_x86_dispatch_multitexcoordf4 );
EXTERN( _tnl_x86_dispatch_multitexcoordfv );
EXTERN( _tnl_x86_dispatch_vertexattribf1 );
EXTERN( _tnl_x86_dispatch_vertexattribf2 );
EXTERN( _tnl_x86_dispatch_vertexattribf3 );
EXTERN( _tnl_x86_dispatch_vertexattribf4 );
EXTERN( _tnl_x86_dispatch_vertexattribfv );

EXTERN( _tnl_x86_choose_fv );


#define DONT_KNOW_OFFSETS 1


#define DFN( FUNC, CACHE, KEY )				\
   struct _tnl_dynfn *dfn = MALLOC_STRUCT( _tnl_dynfn );\
   char *start = (char *)&FUNC;				\
   char *end = (char *)&FUNC##_end;			\
   int offset = 0;               			\
   insert_at_head( &CACHE, dfn );			\
   dfn->key = KEY;					\
   dfn->code = ALIGN_MALLOC( end - start, 16 );		\
   memcpy (dfn->code, start, end - start)



#define FIXUP( CODE, KNOWN_OFFSET, CHECKVAL, NEWVAL )	\
do {							\
   GLint subst = 0x10101010 + CHECKVAL;			\
							\
   if (DONT_KNOW_OFFSETS) {				\
      while (*(int *)(CODE+offset) != subst) offset++;	\
      *(int *)(CODE+offset) = (int)(NEWVAL);		\
      if (0) fprintf(stderr, "%s/%d: offset %d, new value: 0x%x\n", __FILE__, __LINE__, offset, (int)(NEWVAL)); \
      offset += 4;					\
   }							\
   else {						\
      int *icode = (int *)(CODE+KNOWN_OFFSET);		\
      assert (*icode == subst);				\
      *icode = (int)NEWVAL;				\
   }							\
} while (0)



#define FIXUPREL( CODE, KNOWN_OFFSET, CHECKVAL, NEWVAL )\
do {							\
   GLint subst = 0x10101010 + CHECKVAL;			\
							\
   if (DONT_KNOW_OFFSETS) {				\
      while (*(int *)(CODE+offset) != subst) offset++;	\
      *(int *)(CODE+offset) = (int)(NEWVAL) - ((int)(CODE)+offset) - 4; \
      if (0) fprintf(stderr, "%s/%d: offset %d, new value: 0x%x\n", __FILE__, __LINE__, offset, (int)(NEWVAL) - ((int)(CODE)+offset) - 4); \
      offset += 4;					\
   }							\
   else {						\
      int *icode = (int *)(CODE+KNOWN_OFFSET);		\
      assert (*icode == subst);				\
      *icode = (int)(NEWVAL) - (int)(icode) - 4;	\
   }							\
} while (0)




/* Build specialized versions of the immediate calls on the fly for
 * the current state.  Generic x86 versions.
 */

static struct _tnl_dynfn *makeX86Vertex1fv( GLcontext *ctx, int vertex_size )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   DFN ( _tnl_x86_Vertex1fv, tnl->vtx.cache.Vertex[1-1], vertex_size );

   FIXUP(dfn->code, 0, 0, (int)&tnl->vtx.vbptr);
   FIXUP(dfn->code, 0, 1, vertex_size - 1);
   FIXUP(dfn->code, 0, 2, (int)&tnl->vtx.vertex[1]);
   FIXUP(dfn->code, 0, 0, (int)&tnl->vtx.vbptr);
   FIXUP(dfn->code, 0, 3, (int)&tnl->vtx.counter);
   FIXUP(dfn->code, 0, 3, (int)&tnl->vtx.counter);
   FIXUP(dfn->code, 0, 4, (int)ctx);
   FIXUPREL(dfn->code, 0, 5, (int)&_tnl_wrap_filled_vertex);

   return dfn;
}

static struct _tnl_dynfn *makeX86Vertex2fv( GLcontext *ctx, int vertex_size )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   DFN ( _tnl_x86_Vertex2fv, tnl->vtx.cache.Vertex[2-1], vertex_size );

   FIXUP(dfn->code, 0, 0, (int)&tnl->vtx.vbptr);
   FIXUP(dfn->code, 0, 1, vertex_size - 2);
   FIXUP(dfn->code, 0, 2, (int)&tnl->vtx.vertex[2]);
   FIXUP(dfn->code, 0, 0, (int)&tnl->vtx.vbptr);
   FIXUP(dfn->code, 0, 3, (int)&tnl->vtx.counter);
   FIXUP(dfn->code, 0, 3, (int)&tnl->vtx.counter);
   FIXUP(dfn->code, 0, 4, (int)ctx);
   FIXUPREL(dfn->code, 0, 5, (int)&_tnl_wrap_filled_vertex);

   return dfn;
}

static struct _tnl_dynfn *makeX86Vertex3fv( GLcontext *ctx, int vertex_size )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   DFN ( _tnl_x86_Vertex3fv, tnl->vtx.cache.Vertex[3-1], vertex_size );

   switch (vertex_size) {
      default: {
         FIXUP(dfn->code, 0, 0, (int)&tnl->vtx.vbptr);
         FIXUP(dfn->code, 0, 1, vertex_size - 3);
         FIXUP(dfn->code, 0, 2, (int)&tnl->vtx.vertex[3]);
         FIXUP(dfn->code, 0, 0, (int)&tnl->vtx.vbptr);
         FIXUP(dfn->code, 0, 3, (int)&tnl->vtx.counter);
         FIXUP(dfn->code, 0, 3, (int)&tnl->vtx.counter);
         FIXUP(dfn->code, 0, 4, (int)ctx);
         FIXUPREL(dfn->code, 0, 5, (int)&_tnl_wrap_filled_vertex);
         return dfn;
      }
   }
}

static struct _tnl_dynfn *makeX86Vertex4fv( GLcontext *ctx, int vertex_size )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   DFN ( _tnl_x86_Vertex4fv, tnl->vtx.cache.Vertex[4-1], vertex_size );

   FIXUP(dfn->code, 0, 0, (int)&tnl->vtx.vbptr);
   FIXUP(dfn->code, 0, 1, vertex_size - 4);
   FIXUP(dfn->code, 0, 2, (int)&tnl->vtx.vertex[4]);
   FIXUP(dfn->code, 0, 0, (int)&tnl->vtx.vbptr);
   FIXUP(dfn->code, 0, 3, (int)&tnl->vtx.counter);
   FIXUP(dfn->code, 0, 3, (int)&tnl->vtx.counter);
   FIXUP(dfn->code, 0, 4, (int)ctx);
   FIXUPREL(dfn->code, 0, 5, (int)&_tnl_wrap_filled_vertex);

   return dfn;
}


static struct _tnl_dynfn *makeX86Attribute1fv( GLcontext *ctx, int dest )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   DFN ( _tnl_x86_Attribute1fv, tnl->vtx.cache.Attribute[1-1], dest );

   FIXUP(dfn->code, 0, 0, dest); 

   return dfn;
}

static struct _tnl_dynfn *makeX86Attribute2fv( GLcontext *ctx, int dest )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   DFN ( _tnl_x86_Attribute2fv, tnl->vtx.cache.Attribute[2-1], dest );

   FIXUP(dfn->code, 0, 0, dest); 
   FIXUP(dfn->code, 0, 1, 4+dest); 

   return dfn;
}

static struct _tnl_dynfn *makeX86Attribute3fv( GLcontext *ctx, int dest )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   DFN ( _tnl_x86_Attribute3fv, tnl->vtx.cache.Attribute[3-1], dest );

   FIXUP(dfn->code, 0, 0, dest); 
   FIXUP(dfn->code, 0, 1, 4+dest); 
   FIXUP(dfn->code, 0, 2, 8+dest);

   return dfn;
}

static struct _tnl_dynfn *makeX86Attribute4fv( GLcontext *ctx, int dest )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   DFN ( _tnl_x86_Attribute4fv, tnl->vtx.cache.Attribute[4-1], dest );

   FIXUP(dfn->code, 0, 0, dest); 
   FIXUP(dfn->code, 0, 1, 4+dest); 
   FIXUP(dfn->code, 0, 2, 8+dest);
   FIXUP(dfn->code, 0, 3, 12+dest);

   return dfn;
}


void _tnl_InitX86Codegen( struct _tnl_dynfn_generators *gen )
{
   gen->Vertex[0] = makeX86Vertex1fv;
   gen->Vertex[1] = makeX86Vertex2fv;
   gen->Vertex[2] = makeX86Vertex3fv;
   gen->Vertex[3] = makeX86Vertex4fv;
   gen->Attribute[0] = makeX86Attribute1fv;
   gen->Attribute[1] = makeX86Attribute2fv;
   gen->Attribute[2] = makeX86Attribute3fv;
   gen->Attribute[3] = makeX86Attribute4fv;
}


#define MKDISP(FUNC, SIZE, ATTR, WARP)					\
do {									\
   char *code;								\
   char *start = (char *)&WARP;						\
   char *end = (char *)&WARP##_end;					\
   int offset = 0;							\
   code = ALIGN_MALLOC( end - start, 16 );				\
   memcpy (code, start, end - start);					\
   FIXUP(code, 0, 0, (int)&(TNL_CONTEXT(ctx)->vtx.tabfv[ATTR][SIZE-1]));\
   *(void **)&vfmt->FUNC = code;					\
} while (0)


/* Install the codegen'ed versions of the 2nd level dispatch
 * functions.  We should keep a list and free them in the end...
 */
void _tnl_x86_exec_vtxfmt_init( GLcontext *ctx )
{
   GLvertexformat *vfmt = &(TNL_CONTEXT(ctx)->exec_vtxfmt);

   MKDISP(Color3f,             3, _TNL_ATTRIB_COLOR0, _tnl_x86_dispatch_attrf3);
   MKDISP(Color3fv,            3, _TNL_ATTRIB_COLOR0, _tnl_x86_dispatch_attrfv);
   MKDISP(Color4f,             4, _TNL_ATTRIB_COLOR0, _tnl_x86_dispatch_attrf4);
   MKDISP(Color4fv,            4, _TNL_ATTRIB_COLOR0, _tnl_x86_dispatch_attrfv);
   MKDISP(FogCoordfEXT,        1, _TNL_ATTRIB_FOG,    _tnl_x86_dispatch_attrf1);
   MKDISP(FogCoordfvEXT,       1, _TNL_ATTRIB_FOG,    _tnl_x86_dispatch_attrfv);
   MKDISP(Normal3f,            3, _TNL_ATTRIB_NORMAL, _tnl_x86_dispatch_attrf3);
   MKDISP(Normal3fv,           3, _TNL_ATTRIB_NORMAL, _tnl_x86_dispatch_attrfv);
   MKDISP(SecondaryColor3fEXT, 3, _TNL_ATTRIB_COLOR1, _tnl_x86_dispatch_attrf3);
   MKDISP(SecondaryColor3fvEXT,3, _TNL_ATTRIB_COLOR1, _tnl_x86_dispatch_attrfv);
   MKDISP(TexCoord1f,          1, _TNL_ATTRIB_TEX0,   _tnl_x86_dispatch_attrf1);
   MKDISP(TexCoord1fv,         1, _TNL_ATTRIB_TEX0,   _tnl_x86_dispatch_attrfv);
   MKDISP(TexCoord2f,          2, _TNL_ATTRIB_TEX0,   _tnl_x86_dispatch_attrf2);
   MKDISP(TexCoord2fv,         2, _TNL_ATTRIB_TEX0,   _tnl_x86_dispatch_attrfv);
   MKDISP(TexCoord3f,          3, _TNL_ATTRIB_TEX0,   _tnl_x86_dispatch_attrf3);
   MKDISP(TexCoord3fv,         3, _TNL_ATTRIB_TEX0,   _tnl_x86_dispatch_attrfv);
   MKDISP(TexCoord4f,          4, _TNL_ATTRIB_TEX0,   _tnl_x86_dispatch_attrf4);
   MKDISP(TexCoord4fv,         4, _TNL_ATTRIB_TEX0,   _tnl_x86_dispatch_attrfv);
   MKDISP(Vertex2f,            2, _TNL_ATTRIB_POS,    _tnl_x86_dispatch_attrf2);
   MKDISP(Vertex2fv,           2, _TNL_ATTRIB_POS,    _tnl_x86_dispatch_attrfv);
   MKDISP(Vertex3f,            3, _TNL_ATTRIB_POS,    _tnl_x86_dispatch_attrf3);
   MKDISP(Vertex3fv,           3, _TNL_ATTRIB_POS,    _tnl_x86_dispatch_attrfv);
   MKDISP(Vertex4f,            4, _TNL_ATTRIB_POS,    _tnl_x86_dispatch_attrf4);
   MKDISP(Vertex4fv,           4, _TNL_ATTRIB_POS,    _tnl_x86_dispatch_attrfv);

   MKDISP(MultiTexCoord1fARB,  1, _TNL_ATTRIB_TEX0,   _tnl_x86_dispatch_multitexcoordf1);
   MKDISP(MultiTexCoord1fvARB, 1, _TNL_ATTRIB_TEX0,   _tnl_x86_dispatch_multitexcoordfv);
   MKDISP(MultiTexCoord2fARB,  2, _TNL_ATTRIB_TEX0,   _tnl_x86_dispatch_multitexcoordf2);
   MKDISP(MultiTexCoord2fvARB, 2, _TNL_ATTRIB_TEX0,   _tnl_x86_dispatch_multitexcoordfv);
   MKDISP(MultiTexCoord3fARB,  3, _TNL_ATTRIB_TEX0,   _tnl_x86_dispatch_multitexcoordf3);
   MKDISP(MultiTexCoord3fvARB, 3, _TNL_ATTRIB_TEX0,   _tnl_x86_dispatch_multitexcoordfv);
   MKDISP(MultiTexCoord4fARB,  4, _TNL_ATTRIB_TEX0,   _tnl_x86_dispatch_multitexcoordf4);
   MKDISP(MultiTexCoord4fvARB, 4, _TNL_ATTRIB_TEX0,   _tnl_x86_dispatch_multitexcoordfv);

   MKDISP(VertexAttrib1fNV,    1, 0,                  _tnl_x86_dispatch_vertexattribf1);
   MKDISP(VertexAttrib1fvNV,   1, 0,                  _tnl_x86_dispatch_vertexattribfv);
   MKDISP(VertexAttrib2fNV,    2, 0,                  _tnl_x86_dispatch_vertexattribf2);
   MKDISP(VertexAttrib2fvNV,   2, 0,                  _tnl_x86_dispatch_vertexattribfv);
   MKDISP(VertexAttrib3fNV,    3, 0,                  _tnl_x86_dispatch_vertexattribf3);
   MKDISP(VertexAttrib3fvNV,   3, 0,                  _tnl_x86_dispatch_vertexattribfv);
   MKDISP(VertexAttrib4fNV,    4, 0,                  _tnl_x86_dispatch_vertexattribf4);
   MKDISP(VertexAttrib4fvNV,   4, 0,                  _tnl_x86_dispatch_vertexattribfv);
}


/* Install the codegen'ed choosers.
 * We should keep a list and free them in the end...
 */
void _tnl_x86choosers( tnl_attrfv_func (*choose)[4],
		       tnl_attrfv_func (*do_choose)( GLuint attr,
						 GLuint sz ))
{
   int attr, size;

   for (attr = 0; attr < _TNL_MAX_ATTR_CODEGEN; attr++) {
      for (size = 0; size < 4; size++) {
         char *code;
         char *start = (char *)&_tnl_x86_choose_fv;
         char *end = (char *)&_tnl_x86_choose_fv_end;
         int offset = 0;
         code = ALIGN_MALLOC( end - start, 16 );
         memcpy (code, start, end - start);
         FIXUP(code, 0, 0, attr);
         FIXUP(code, 0, 1, size + 1);
         FIXUPREL(code, 0, 2, do_choose);
         choose[attr][size] = (tnl_attrfv_func)code;
      }
   }
}

#else 

void _tnl_InitX86Codegen( struct _tnl_dynfn_generators *gen )
{
   (void) gen;
}


void _tnl_x86_exec_vtxfmt_init( GLcontext *ctx )
{
   (void) ctx;
}


void _tnl_x86choosers( tnl_attrfv_func (*choose)[4],
		       tnl_attrfv_func (*do_choose)( GLuint attr,
						 GLuint sz ))
{
   (void) choose;
   (void) do_choose;
}

#endif