03b90a44717443e6ed60d8b8f7eddb1249fde8b8
[mesa.git] / src / mesa / drivers / dri / mach64 / mach64_context.h
1 /* $XFree86$ */ /* -*- mode: c; c-basic-offset: 3 -*- */
2 /*
3 * Copyright 2000 Gareth Hughes
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * GARETH HUGHES BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /*
26 * Authors:
27 * Gareth Hughes <gareth@valinux.com>
28 * Leif Delgass <ldelgass@retinalburn.net>
29 * Jos�Fonseca <j_r_fonseca@yahoo.co.uk>
30 */
31
32 #ifndef __MACH64_CONTEXT_H__
33 #define __MACH64_CONTEXT_H__
34
35 #ifdef GLX_DIRECT_RENDERING
36
37 #include "dri_util.h"
38 #include "drm.h"
39 #include "xf86drm.h"
40 #include "mach64_common.h"
41
42 #include "mtypes.h"
43 #include "mm.h"
44
45 #include "mach64_reg.h"
46
47 #include "texmem.h"
48
49 struct mach64_context;
50 typedef struct mach64_context mach64ContextRec;
51 typedef struct mach64_context *mach64ContextPtr;
52
53 #include "mach64_lock.h"
54 #include "mach64_screen.h"
55
56 /* Experimental driver options */
57 #define MACH64_CLIENT_STATE_EMITS 0
58
59 /* Performace monitoring */
60 #define ENABLE_PERF_BOXES 1
61
62 /* Native vertex format */
63 #define MACH64_NATIVE_VTXFMT 1
64
65 /* Flags for what context state needs to be updated:
66 */
67 #define MACH64_NEW_ALPHA 0x0001
68 #define MACH64_NEW_DEPTH 0x0002
69 #define MACH64_NEW_FOG 0x0004
70 #define MACH64_NEW_CLIP 0x0008
71 #define MACH64_NEW_CULL 0x0010
72 #define MACH64_NEW_MASKS 0x0020
73 #define MACH64_NEW_RENDER_UNUSED 0x0040
74 #define MACH64_NEW_WINDOW 0x0080
75 #define MACH64_NEW_TEXTURE 0x0100
76 #define MACH64_NEW_CONTEXT 0x0200
77 #define MACH64_NEW_ALL 0x03ff
78
79 /* Flags for software fallback cases:
80 */
81 #define MACH64_FALLBACK_TEXTURE 0x0001
82 #define MACH64_FALLBACK_DRAW_BUFFER 0x0002
83 #define MACH64_FALLBACK_READ_BUFFER 0x0004
84 #define MACH64_FALLBACK_STENCIL 0x0008
85 #define MACH64_FALLBACK_RENDER_MODE 0x0010
86 #define MACH64_FALLBACK_MULTIDRAW 0x0020
87 #define MACH64_FALLBACK_LOGICOP 0x0040
88 #define MACH64_FALLBACK_SEP_SPECULAR 0x0080
89 #define MACH64_FALLBACK_BLEND_EQ 0x0100
90 #define MACH64_FALLBACK_BLEND_FUNC 0x0200
91
92 #if MACH64_NATIVE_VTXFMT
93
94 /* The vertex structures.
95 */
96
97 #if 0
98 /* This isn't actually necessary since all accesses to the vertex
99 * structure must be made through the LE32_* macros.
100 */
101
102 typedef struct {
103 GLubyte blue;
104 GLubyte green;
105 GLubyte red;
106 GLubyte alpha;
107 } mach64_color_t;
108
109 typedef struct {
110 GLfloat u1, v1, w1; /* Texture 1 coordinates */
111 GLfloat u0, v0, w0; /* Texture 0 coordinates */
112 mach64_color_t specular; /* Specular color */
113 GLuint z; /* Depth coordinate */
114 mach64_color_t color; /* Diffuse color */
115 GLushort y, x; /* Coordinates in screen space */
116 } mach64_vertex;
117 #endif
118
119 /* The size of this union is not of relevence:
120 */
121 union mach64_vertex_t {
122 GLfloat f[16];
123 GLuint ui[16];
124 GLushort us2[16][2];
125 GLubyte ub4[16][4];
126 };
127
128 typedef union mach64_vertex_t mach64Vertex, *mach64VertexPtr;
129
130 #else
131
132 /* Use the templated vertex format:
133 */
134 #define TAG(x) mach64##x
135 #include "tnl_dd/t_dd_vertex.h"
136 #undef TAG
137
138 #endif /* MACH64_NATIVE_VTXFMT */
139
140 /* Subpixel offsets for window coordinates:
141 * These are enough to fix most glean tests except polygonOffset.
142 * There are also still some gaps that show in e.g. the tunnel Mesa demo
143 * or the lament xscreensaver hack.
144 */
145 #define SUBPIXEL_X (0.0125F)
146 #define SUBPIXEL_Y (0.15F)
147
148
149 typedef void (*mach64_tri_func)( mach64ContextPtr,
150 mach64Vertex *,
151 mach64Vertex *,
152 mach64Vertex * );
153
154 typedef void (*mach64_line_func)( mach64ContextPtr,
155 mach64Vertex *,
156 mach64Vertex * );
157
158 typedef void (*mach64_point_func)( mach64ContextPtr,
159 mach64Vertex * );
160
161 #ifdef TEXMEM
162 struct mach64_texture_object {
163 driTextureObject base;
164
165 GLuint offset;
166
167 GLuint dirty;
168 GLuint age;
169
170 GLint widthLog2;
171 GLint heightLog2;
172 GLint maxLog2;
173
174 GLint hasAlpha;
175 GLint textureFormat;
176
177 /* Have to keep these separate due to how they are programmed.
178 * FIXME: Why don't we just use the tObj values?
179 */
180 GLboolean BilinearMin;
181 GLboolean BilinearMag;
182 GLboolean ClampS;
183 GLboolean ClampT;
184 };
185 #else
186 struct mach64_texture_object {
187 struct mach64_texture_object *next;
188 struct mach64_texture_object *prev;
189 struct gl_texture_object *tObj;
190
191 PMemBlock memBlock;
192 GLuint offset;
193 GLuint size;
194
195 GLuint dirty;
196 GLuint age;
197
198 GLint bound;
199 GLint heap;
200
201 GLint widthLog2;
202 GLint heightLog2;
203 GLint maxLog2;
204
205 GLint hasAlpha;
206 GLint textureFormat;
207
208 /* Have to keep these separate due to how they are programmed.
209 * FIXME: Why don't we just use the tObj values?
210 */
211 GLboolean BilinearMin;
212 GLboolean BilinearMag;
213 GLboolean ClampS;
214 GLboolean ClampT;
215 };
216 #endif
217
218 typedef struct mach64_texture_object mach64TexObj, *mach64TexObjPtr;
219
220
221 struct mach64_context {
222 GLcontext *glCtx;
223
224 /* Driver and hardware state management
225 */
226 GLuint new_state;
227 GLuint dirty; /* Hardware state to be updated */
228 mach64_context_regs_t setup;
229
230 GLuint NewGLState;
231 GLuint Fallback;
232 GLuint SetupIndex;
233 GLuint SetupNewInputs;
234 GLuint RenderIndex;
235 GLfloat hw_viewport[16];
236 GLfloat depth_scale;
237 GLuint vertex_size;
238 GLuint vertex_stride_shift;
239 GLuint vertex_format;
240 GLuint num_verts;
241 GLubyte *verts;
242
243 CARD32 Color; /* Current draw color */
244 CARD32 ClearColor; /* Color used to clear color buffer */
245 CARD32 ClearDepth; /* Value used to clear depth buffer */
246
247 /* Map GL texture units onto hardware
248 */
249 GLint multitex;
250 GLint tmu_source[2];
251 GLint tex_dest[2];
252
253 /* Texture object bookkeeping
254 */
255 mach64TexObjPtr CurrentTexObj[2];
256 #ifdef TEXMEM
257 unsigned nr_heaps;
258 driTexHeap * texture_heaps[ R128_NR_TEX_HEAPS ];
259 driTextureObject swapped;
260 #else
261 mach64TexObj TexObjList[MACH64_NR_TEX_HEAPS];
262 mach64TexObj SwappedOut;
263 memHeap_t *texHeap[MACH64_NR_TEX_HEAPS];
264 GLuint lastTexAge[MACH64_NR_TEX_HEAPS];
265 GLint firstTexHeap, lastTexHeap;
266 #endif
267
268 /* Fallback rasterization functions
269 */
270 mach64_point_func draw_point;
271 mach64_line_func draw_line;
272 mach64_tri_func draw_tri;
273
274 /* Culling */
275 GLfloat backface_sign;
276
277 /* DMA buffers
278 */
279 void *vert_buf;
280 size_t vert_total;
281 unsigned vert_used;
282
283 GLuint hw_primitive;
284 GLenum render_primitive;
285
286 /* Visual, drawable, cliprect and scissor information
287 */
288 GLint drawOffset, drawPitch;
289 GLint drawX, drawY; /* origin of drawable in draw buffer */
290 GLint readOffset, readPitch;
291
292 GLuint numClipRects; /* Cliprects for the draw buffer */
293 drm_clip_rect_t *pClipRects;
294
295 GLint scissor;
296 drm_clip_rect_t ScissorRect; /* Current software scissor */
297
298 /* Mirrors of some DRI state
299 */
300 __DRIcontextPrivate *driContext; /* DRI context */
301 __DRIscreenPrivate *driScreen; /* DRI screen */
302 __DRIdrawablePrivate *driDrawable; /* DRI drawable bound to this ctx */
303
304 unsigned int lastStamp; /* mirror driDrawable->lastStamp */
305
306 drmContext hHWContext;
307 drm_hw_lock_t *driHwLock;
308 int driFd;
309
310 mach64ScreenPtr mach64Screen; /* Screen private DRI data */
311 ATISAREAPrivPtr sarea; /* Private SAREA data */
312
313 GLuint hardwareWentIdle;
314
315 #if ENABLE_PERF_BOXES
316 /* Performance counters
317 */
318 GLuint boxes; /* Draw performance boxes */
319 GLuint c_clears;
320 GLuint c_drawWaits;
321 GLuint c_textureSwaps;
322 GLuint c_textureBytes;
323 GLuint c_agpTextureBytes;
324 GLuint c_texsrc_agp;
325 GLuint c_texsrc_card;
326 GLuint c_vertexBuffers;
327 #endif
328
329 /* VBI
330 */
331 GLuint vbl_seq;
332 GLuint vblank_flags;
333 GLuint do_irqs;
334
335 /* Configuration cache
336 */
337 driOptionCache optionCache;
338 };
339
340 #define MACH64_CONTEXT(ctx) ((mach64ContextPtr)(ctx->DriverCtx))
341
342
343 extern GLboolean mach64CreateContext( const __GLcontextModes *glVisual,
344 __DRIcontextPrivate *driContextPriv,
345 void *sharedContextPrivate );
346
347 extern void mach64DestroyContext( __DRIcontextPrivate * );
348
349 extern GLboolean mach64MakeCurrent( __DRIcontextPrivate *driContextPriv,
350 __DRIdrawablePrivate *driDrawPriv,
351 __DRIdrawablePrivate *driReadPriv );
352
353 extern GLboolean mach64UnbindContext( __DRIcontextPrivate *driContextPriv );
354
355 /* ================================================================
356 * Byte ordering
357 */
358 #include "X11/Xarch.h"
359
360 #if X_BYTE_ORDER == X_LITTLE_ENDIAN
361 #define LE32_IN( x ) ( *(GLuint *)(x) )
362 #define LE32_IN_FLOAT( x ) ( *(GLfloat *)(x) )
363 #define LE32_OUT( x, y ) do { *(GLuint *)(x) = (y); } while (0)
364 #define LE32_OUT_FLOAT( x, y ) do { *(GLfloat *)(x) = (y); } while (0)
365 #else
366 #include <byteswap.h>
367 #define LE32_IN( x ) bswap_32( *(GLuint *)(x) )
368 #define LE32_IN_FLOAT( x ) \
369 ({ \
370 GLuint __tmp = bswap_32( *(GLuint *)(x) ); \
371 *(GLfloat *)&__tmp; \
372 })
373 #define LE32_OUT( x, y ) do { *(GLuint *)(x) = bswap_32( y ); } while (0)
374 #define LE32_OUT_FLOAT( x, y ) \
375 do { \
376 GLuint __tmp; \
377 *(GLfloat *)&__tmp = (y); \
378 *(GLuint *)(x) = bswap_32( __tmp ); \
379 } while (0)
380 #endif
381
382 /* ================================================================
383 * DMA buffers
384 */
385
386 #define DMALOCALS CARD32 *buf=NULL; int requested=0; int outcount=0
387
388 /* called while locked for interleaved client-side state emits */
389 #define DMAGETPTR( dwords ) \
390 do { \
391 requested = (dwords); \
392 buf = (CARD32 *)mach64AllocDmaLocked( mmesa, ((dwords)*4) ); \
393 outcount = 0; \
394 } while(0)
395
396 #define DMAOUTREG( reg, val ) \
397 do { \
398 LE32_OUT( &buf[outcount++], ADRINDEX( reg ) ); \
399 LE32_OUT( &buf[outcount++], ( val ) ); \
400 } while(0)
401
402 #define DMAADVANCE() \
403 do { \
404 if (outcount < requested) { \
405 mmesa->vert_used -= (requested - outcount) * 4; \
406 } \
407 } while(0)
408
409 /* ================================================================
410 * Debugging:
411 */
412
413 #define DO_DEBUG 1
414
415 #if DO_DEBUG
416 extern int MACH64_DEBUG;
417 #else
418 #define MACH64_DEBUG 0
419 #endif
420
421 #define DEBUG_ALWAYS_SYNC 0x001
422 #define DEBUG_VERBOSE_API 0x002
423 #define DEBUG_VERBOSE_MSG 0x004
424 #define DEBUG_VERBOSE_LRU 0x008
425 #define DEBUG_VERBOSE_DRI 0x010
426 #define DEBUG_VERBOSE_IOCTL 0x020
427 #define DEBUG_VERBOSE_PRIMS 0x040
428 #define DEBUG_VERBOSE_COUNT 0x080
429 #define DEBUG_NOWAIT 0x100
430 #endif
431 #endif /* __MACH64_CONTEXT_H__ */