2124006ade600b7b6d470aaa6487d864c7270139
[mesa.git] / src / mesa / drivers / dri / mga / mgacontext.h
1 /* $XFree86: xc/lib/GL/mesa/src/drv/mga/mgacontext.h,v 1.7 2002/12/16 16:18:52 dawes Exp $*/
2 /*
3 * Copyright 2000-2001 VA Linux Systems, Inc.
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
10 * license, and/or sell copies of the Software, and to permit persons to whom
11 * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
20 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 *
25 * Authors:
26 * Keith Whitwell <keith@tungstengraphics.com>
27 */
28
29 #ifndef MGALIB_INC
30 #define MGALIB_INC
31
32 #include <stdint.h>
33 #include "drm.h"
34 #include "mga_drm.h"
35 #include "dri_util.h"
36 #include "mtypes.h"
37 #include "xf86drm.h"
38 #include "mm.h"
39 #include "colormac.h"
40 #include "texmem.h"
41 #include "macros.h"
42 #include "xmlconfig.h"
43
44 #define MGA_SET_FIELD(reg,mask,val) reg = ((reg) & (mask)) | ((val) & ~(mask))
45 #define MGA_FIELD(field,val) (((val) << (field ## _SHIFT)) & ~(field ## _MASK))
46 #define MGA_GET_FIELD(field, val) ((val & ~(field ## _MASK)) >> (field ## _SHIFT))
47
48 #define MGA_IS_G200(mmesa) (mmesa->mgaScreen->chipset == MGA_CARD_TYPE_G200)
49 #define MGA_IS_G400(mmesa) (mmesa->mgaScreen->chipset == MGA_CARD_TYPE_G400)
50
51
52 /* SoftwareFallback
53 * - texture env GL_BLEND -- can be fixed
54 * - 1D and 3D textures
55 * - incomplete textures
56 * - GL_DEPTH_FUNC == GL_NEVER not in h/w
57 */
58 #define MGA_FALLBACK_TEXTURE 0x1
59 #define MGA_FALLBACK_DRAW_BUFFER 0x2
60 #define MGA_FALLBACK_READ_BUFFER 0x4
61 #define MGA_FALLBACK_BLEND 0x8
62 #define MGA_FALLBACK_RENDERMODE 0x10
63 #define MGA_FALLBACK_STENCIL 0x20
64 #define MGA_FALLBACK_DEPTH 0x40
65 #define MGA_FALLBACK_BORDER_MODE 0x80
66 #define MGA_FALLBACK_DISABLE 0x100
67
68
69 /* Use the templated vertex formats:
70 */
71 #define TAG(x) mga##x
72 #include "tnl_dd/t_dd_vertex.h"
73 #undef TAG
74
75 typedef struct mga_context_t mgaContext;
76 typedef struct mga_context_t *mgaContextPtr;
77
78 typedef void (*mga_tri_func)( mgaContextPtr, mgaVertex *, mgaVertex *,
79 mgaVertex * );
80 typedef void (*mga_line_func)( mgaContextPtr, mgaVertex *, mgaVertex * );
81 typedef void (*mga_point_func)( mgaContextPtr, mgaVertex * );
82
83
84
85 /* Texture environment color
86 */
87 #define RGB_ZERO(c) (((c) & 0xffffff) == 0x000000)
88 #define RGB_ONE(c) (((c) & 0xffffff) == 0xffffff)
89 #define ALPHA_ZERO(c) (((c) >> 24) == 0x00)
90 #define ALPHA_ONE(c) (((c) >> 24) == 0xff)
91 #define RGBA_EQUAL(c) ((c) == PACK_COLOR_8888( (c) & 0xff, (c) & 0xff, \
92 (c) & 0xff, (c) & 0xff ))
93
94 struct mga_texture_object_s;
95 struct mga_screen_private_s;
96
97 #define G200_TEX_MAXLEVELS 5
98 #define G400_TEX_MAXLEVELS 11
99
100 typedef struct mga_texture_object_s
101 {
102 driTextureObject base;
103
104 /* The G200 only has the ability to use 5 mipmap levels (including the
105 * base level). The G400 does not have this restriction, but it still
106 * only has 5 offset pointers in the hardware. The trick on the G400 is
107 * upto the first 4 offset pointers point to mipmap levels. The last
108 * offset pointer tells how large the preceeding mipmap is. This value is
109 * then used to determine where the remaining mipmaps are.
110 *
111 * For example, if the first offsets[0] through offsets[2] are used as
112 * pointers, then offset[3] will be the size of the mipmap pointed to by
113 * offsets[2]. So mipmap level 3 will be at (offsets[2]+offsets[3]). For
114 * each successive mipmap level, offsets[3] is divided by 4 and added to
115 * the previous address. So mipmap level 4 will be at
116 * (offsets[2]+offsets[3]+(offsets[3] / 4)).
117 *
118 * The last pointer is selected by setting TO_texorgoffsetsel in its
119 * pointer. In the previous example, offset[2] would have
120 * TO_texorgoffsetsel or'ed in before writing it to the hardware.
121 *
122 * In the current driver all of the mipmaps are packed together linearly
123 * with mipmap level 0. Therefore offsets[0] points to the base of the
124 * texture (and has TO_texorgoffsetsel or'ed in), and offsets[1] is the
125 * size of the base texture.
126 *
127 * There is a possible optimization available here. At times the driver
128 * may not be able to allocate a single block of memory for the complete
129 * texture without ejecting some other textures from memory. It may be
130 * possible to put some of the lower mipmap levels (i.e., the larger
131 * mipmaps) in memory separate from the higher levels.
132 *
133 * The implementation should be fairly obvious, but getting "right" would
134 * likely be non-trivial. A first allocation for the entire texture would
135 * be attempted with a flag that says "don't eject other textures." If
136 * that failed, an additional allocation would be attmpted for just the
137 * base map. The process would repeat with the block of lower maps. The
138 * tricky parts would be in detecting when some of the levels had been
139 * ejected from texture memory by other textures and preventing the
140 * 4th allocation (for all the smallest mipmap levels) from kicking out
141 * any of the first three.
142 *
143 * This array holds G400_TEX_MAXLEVELS pointers to remove an if-statement
144 * in a loop in mgaSetTexImages. Values past G200_TEX_MAXLEVELS are not
145 * used.
146 */
147 GLuint offsets[G400_TEX_MAXLEVELS];
148
149 int texelBytes;
150 GLuint age;
151
152 drm_mga_texture_regs_t setup;
153
154 /* If one texture dimension wraps with GL_CLAMP and the other with
155 * GL_CLAMP_TO_EDGE, we have to fallback to software. We would also have
156 * to fallback for GL_CLAMP_TO_BORDER.
157 */
158 GLboolean border_fallback;
159 /* Depending on multitxturing and environment color
160 * GL_BLEND may have to be a software fallback.
161 */
162 GLboolean texenv_fallback;
163 } mgaTextureObject_t;
164
165 struct mga_hw_state {
166 GLuint specen;
167 GLuint cull;
168 GLuint cull_dualtex;
169 GLuint stencil;
170 GLuint stencilctl;
171 GLuint stencil_enable;
172 GLuint zmode;
173 GLuint rop;
174 GLuint alpha_func;
175 GLuint alpha_func_enable;
176 GLuint blend_func;
177 GLuint blend_func_enable;
178 GLuint alpha_sel;
179 };
180
181 struct mga_context_t {
182
183 GLcontext *glCtx;
184 unsigned int lastStamp; /* fullscreen breaks dpriv->laststamp,
185 * need to shadow it here. */
186
187 /* Hardware state management
188 */
189 struct mga_hw_state hw;
190
191 /* Bookkeeping for texturing
192 */
193 unsigned nr_heaps;
194 driTexHeap * texture_heaps[ MGA_NR_TEX_HEAPS ];
195 driTextureObject swapped;
196
197 struct mga_texture_object_s *CurrentTexObj[2];
198
199
200 /* Map GL texture units onto hardware.
201 */
202 GLuint tmu_source[2];
203
204 int texture_depth;
205
206 /* Manage fallbacks
207 */
208 GLuint Fallback;
209
210 /* Texture environment color.
211 */
212 unsigned int envcolor[2];
213 GLboolean fcol_used;
214 GLboolean force_dualtex;
215
216 /* Rasterization state
217 */
218 GLuint SetupNewInputs;
219 GLuint SetupIndex;
220 GLuint RenderIndex;
221
222 GLuint hw_primitive;
223 GLenum raster_primitive;
224 GLenum render_primitive;
225
226 GLubyte *verts;
227 GLint vertex_stride_shift;
228 GLuint vertex_format;
229 GLuint vertex_size;
230
231 /* Fallback rasterization functions
232 */
233 mga_point_func draw_point;
234 mga_line_func draw_line;
235 mga_tri_func draw_tri;
236
237
238 /* Manage driver and hardware state
239 */
240 GLuint NewGLState;
241 GLuint dirty;
242
243 drm_mga_context_regs_t setup;
244
245 GLuint ClearColor;
246 GLuint ClearDepth;
247 GLuint poly_stipple;
248 GLfloat depth_scale;
249
250 GLuint depth_clear_mask;
251 GLuint stencil_clear_mask;
252 GLuint hw_stencil;
253 GLuint haveHwStipple;
254 GLfloat hw_viewport[16];
255
256 /* Dma buffers
257 */
258 drmBufPtr vertex_dma_buffer;
259 drmBufPtr iload_buffer;
260
261 /* VBI
262 */
263 GLuint vbl_seq;
264 GLuint vblank_flags;
265
266 int64_t swap_ust;
267 int64_t swap_missed_ust;
268
269 GLuint swap_count;
270 GLuint swap_missed_count;
271
272 uint32_t last_frame_fence;
273
274 /* Drawable, cliprect and scissor information
275 */
276 int dirty_cliprects; /* which sets of cliprects are uptodate? */
277 int draw_buffer; /* which buffer are we rendering to */
278 unsigned int drawOffset; /* draw buffer address in space */
279 int readOffset;
280 int drawX, drawY; /* origin of drawable in draw buffer */
281 int lastX, lastY; /* detect DSTORG bug */
282 GLuint numClipRects; /* cliprects for the draw buffer */
283 drm_clip_rect_t *pClipRects;
284 drm_clip_rect_t draw_rect;
285 drm_clip_rect_t scissor_rect;
286 int scissor;
287
288 drm_clip_rect_t tmp_boxes[2][MGA_NR_SAREA_CLIPRECTS];
289
290
291 /* Texture aging and DMA based aging.
292 */
293 unsigned int texAge[MGA_NR_TEX_HEAPS];/* texture LRU age */
294 unsigned int dirtyAge; /* buffer age for synchronization */
295
296 GLuint primary_offset;
297
298 /* Mirrors of some DRI state.
299 */
300 drm_context_t hHWContext;
301 drm_hw_lock_t *driHwLock;
302 int driFd;
303 __DRIdrawablePrivate *driDrawable;
304 __DRIdrawablePrivate *driReadable;
305
306 __DRIscreenPrivate *driScreen;
307 struct mga_screen_private_s *mgaScreen;
308 drm_mga_sarea_t *sarea;
309
310 /* Configuration cache
311 */
312 driOptionCache optionCache;
313 };
314
315 #define MGA_CONTEXT(ctx) ((mgaContextPtr)(ctx->DriverCtx))
316
317
318
319
320 /* ================================================================
321 * Debugging:
322 */
323 #define DO_DEBUG 1
324
325 #if DO_DEBUG
326 extern int MGA_DEBUG;
327 #else
328 #define MGA_DEBUG 0
329 #endif
330
331 #define DEBUG_VERBOSE_MSG 0x01
332 #define DEBUG_VERBOSE_DRI 0x02
333 #define DEBUG_VERBOSE_IOCTL 0x04
334 #define DEBUG_VERBOSE_TEXTURE 0x08
335 #define DEBUG_VERBOSE_FALLBACK 0x10
336
337 static __inline__ GLuint mgaPackColor(GLuint cpp,
338 GLubyte r, GLubyte g,
339 GLubyte b, GLubyte a)
340 {
341 switch (cpp) {
342 case 2:
343 return PACK_COLOR_565( r, g, b );
344 case 4:
345 return PACK_COLOR_8888( a, r, g, b );
346 default:
347 return 0;
348 }
349 }
350
351
352 /*
353 * Subpixel offsets for window coordinates:
354 */
355 #define SUBPIXEL_X (-0.5F)
356 #define SUBPIXEL_Y (-0.5F + 0.125)
357
358
359 #define MGA_WA_TRIANGLES 0x18000000
360 #define MGA_WA_TRISTRIP_T0 0x02010200
361 #define MGA_WA_TRIFAN_T0 0x01000408
362 #define MGA_WA_TRISTRIP_T0T1 0x02010400
363 #define MGA_WA_TRIFAN_T0T1 0x01000810
364
365 #endif