summaryrefslogtreecommitdiff
path: root/src/mesa/tnl/t_vtx_x86.c
blob: 4f04a4efbb33a1183dd3c61a32dd28903b5a9752 (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
/**************************************************************************

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>
 */


#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_attrf );
EXTERN( _tnl_x86_dispatch_attrfv );
EXTERN( _tnl_x86_dispatch_multitexcoordf );
EXTERN( _tnl_x86_dispatch_multitexcoordfv );
EXTERN( _tnl_x86_dispatch_vertexattribf );
EXTERN( _tnl_x86_dispatch_vertexattribfv );


static void notify( void )
{
   GET_CURRENT_CONTEXT( ctx );
   _tnl_wrap_filled_vertex( ctx );
}

#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 {							\
   GLuint 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 {							\
   GLuint 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);
   FIXUPREL(dfn->code, 0, 4, (int)&notify);

   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);
   FIXUPREL(dfn->code, 0, 4, (int)&notify);

   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 );

   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);
   FIXUPREL(dfn->code, 0, 4, (int)&notify);
   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);
   FIXUPREL(dfn->code, 0, 4, (int)&notify);

   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;
}

void _do_choose( void )
{
}


/* [dBorca] I purposely avoided one single macro, since they might need to
 * be handled in different ways. Ohwell, once things get much clearer, they
 * could collapse...
 */
#define MAKE_DISPATCH_ATTR(FUNC, SIZE, TYPE, ATTR)			\
do {									\
   char *code;								\
   char *start = (char *)&_tnl_x86_dispatch_attr##TYPE;			\
   char *end = (char *)&_tnl_x86_dispatch_attr##TYPE##_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]));\
   vfmt->FUNC##SIZE##TYPE = code;					\
} while (0)


#define MAKE_DISPATCH_MULTITEXCOORD(FUNC, SIZE, TYPE, ATTR)		\
do {									\
   char *code;								\
   char *start = (char *)&_tnl_x86_dispatch_multitexcoord##TYPE;		\
   char *end = (char *)&_tnl_x86_dispatch_multitexcoord##TYPE##_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[_TNL_ATTRIB_TEX0][SIZE-1]));\
   vfmt->FUNC##SIZE##TYPE##ARB = code;					\
} while (0)


#define MAKE_DISPATCH_VERTEXATTRIB(FUNC, SIZE, TYPE, ATTR)		\
do {									\
   char *code;								\
   char *start = (char *)&_tnl_x86_dispatch_vertexattrib##TYPE;		\
   char *end = (char *)&_tnl_x86_dispatch_vertexattrib##TYPE##_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[0][SIZE-1]));	\
   vfmt->FUNC##SIZE##TYPE##NV = code;					\
} while (0)

/* [dBorca] 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);

   MAKE_DISPATCH_ATTR(Color,3,f,     _TNL_ATTRIB_COLOR0);
   MAKE_DISPATCH_ATTR(Color,3,fv,    _TNL_ATTRIB_COLOR0);
   MAKE_DISPATCH_ATTR(Color,4,f,     _TNL_ATTRIB_COLOR0);
   MAKE_DISPATCH_ATTR(Color,4,fv,    _TNL_ATTRIB_COLOR0);
   MAKE_DISPATCH_ATTR(Normal,3,f,    _TNL_ATTRIB_NORMAL);
   MAKE_DISPATCH_ATTR(Normal,3,fv,   _TNL_ATTRIB_NORMAL);
   MAKE_DISPATCH_ATTR(TexCoord,2,f,  _TNL_ATTRIB_TEX0);
   MAKE_DISPATCH_ATTR(TexCoord,2,fv, _TNL_ATTRIB_TEX0);
   MAKE_DISPATCH_ATTR(Vertex,3,f,    _TNL_ATTRIB_POS);
   MAKE_DISPATCH_ATTR(Vertex,3,fv,   _TNL_ATTRIB_POS);
   /* just add more */

   MAKE_DISPATCH_MULTITEXCOORD(MultiTexCoord,2,f,  0);
   MAKE_DISPATCH_MULTITEXCOORD(MultiTexCoord,2,fv, 0);
   /* just add more */

   MAKE_DISPATCH_VERTEXATTRIB(VertexAttrib,2,f,  0);
   MAKE_DISPATCH_VERTEXATTRIB(VertexAttrib,2,fv, 0);
   /* just add more */
}

#else 

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

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

#endif