Fixed off by one errors in clipping.
[mesa.git] / src / mesa / drivers / dri / unichrome / via_context.h
1 /*
2 * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
3 * Copyright 2001-2003 S3 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, sub license,
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 and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
14 * of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * VIA, S3 GRAPHICS, AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25
26 #ifndef _VIACONTEXT_H
27 #define _VIACONTEXT_H
28
29 typedef struct via_context_t viaContext;
30 typedef struct via_context_t *viaContextPtr;
31 typedef struct via_texture_object_t *viaTextureObjectPtr;
32
33 #include "dri_util.h"
34
35 #include "mtypes.h"
36 #include "drm.h"
37 #include "mm.h"
38
39 #include "via_screen.h"
40 #include "via_tex.h"
41 #include "via_common.h"
42 #include "xf86drmVIA.h"
43 #ifdef USE_XINERAMA
44 #include "../../../../../include/extensions/Xinerama.h"
45 #endif
46 #define VIA_FALLBACK_TEXTURE 0x1
47 #define VIA_FALLBACK_DRAW_BUFFER 0x2
48 #define VIA_FALLBACK_READ_BUFFER 0x4
49 #define VIA_FALLBACK_COLORMASK 0x8
50 #define VIA_FALLBACK_SPECULAR 0x20
51 #define VIA_FALLBACK_LOGICOP 0x40
52 #define VIA_FALLBACK_RENDERMODE 0x80
53 #define VIA_FALLBACK_STENCIL 0x100
54 #define VIA_FALLBACK_BLEND_EQ 0x200
55 #define VIA_FALLBACK_BLEND_FUNC 0x400
56
57 #define VIA_UPLOAD_NONE 0x0000
58 #define VIA_UPLOAD_ALPHATEST 0x0001
59 #define VIA_UPLOAD_BLEND 0x0002
60 #define VIA_UPLOAD_FOG 0x0004
61 #define VIA_UPLOAD_MASK_ROP 0x0008
62 #define VIA_UPLOAD_LINESTIPPLE 0x0010
63 #define VIA_UPLOAD_POLYGONSTIPPLE 0x0020
64 #define VIA_UPLOAD_DEPTH 0x0040
65 #define VIA_UPLOAD_TEXTURE 0x0080
66 #define VIA_UPLOAD_STENCIL 0x0100
67 #define VIA_UPLOAD_CLIPPING 0x0200
68 #define VIA_UPLOAD_DESTBUFFER 0x0400
69 #define VIA_UPLOAD_DEPTHBUFFER 0x0800
70 #define VIA_UPLOAD_ENABLE 0x0800
71 #define VIA_UPLOAD_ALL 0x1000
72
73 /* Use the templated vertex formats:
74 */
75 #define TAG(x) via##x
76 #include "tnl_dd/t_dd_vertex.h"
77 #undef TAG
78
79 #define RightOf 1
80 #define LeftOf 2
81 #define Down 4
82 #define Up 8
83 #define S0 16
84 #define S1 32
85 #define P_MASK 0x0f;
86 #define S_MASK 0x30;
87 typedef void (*via_tri_func)(viaContextPtr, viaVertex *, viaVertex *,
88 viaVertex *);
89 typedef void (*via_line_func)(viaContextPtr, viaVertex *, viaVertex *);
90 typedef void (*via_point_func)(viaContextPtr, viaVertex *);
91
92 typedef struct {
93 drm_handle_t handle;
94 drmSize size;
95 GLuint offset;
96 GLuint index;
97 GLuint pitch;
98 GLuint bpp;
99 char *map;
100 } viaBuffer, *viaBufferPtr;
101
102 typedef struct {
103 drm_handle_t handle;
104 drmSize size;
105 GLuint offset;
106 GLuint index;
107 unsigned char* map;
108 } viaDmaBuffer, *viaDmaBufferPtr;
109
110 struct via_context_t {
111 GLint refcount;
112 GLcontext *glCtx;
113 GLcontext *shareCtx;
114 unsigned char* front_base;
115 viaBuffer front;
116 viaBuffer back;
117 viaBuffer depth;
118 GLboolean hasBack;
119 GLboolean hasDepth;
120 GLboolean hasStencil;
121 GLboolean hasAccum;
122 GLuint depthBits;
123 GLuint stencilBits;
124 viaDmaBuffer dma[2];
125 viaRegion tex;
126
127 GLuint isAGP;
128
129 /* Textures
130 */
131 viaTextureObjectPtr CurrentTexObj[2];
132 struct via_texture_object_t TexObjList;
133 struct via_texture_object_t SwappedOut;
134 memHeap_t *texHeap;
135
136 /* Bit flag to keep 0track of fallbacks.
137 */
138 GLuint Fallback;
139
140 /* Temporaries for translating away float colors:
141 */
142 struct gl_client_array UbyteColor;
143 struct gl_client_array UbyteSecondaryColor;
144
145 /* State for via_vb.c and via_tris.c.
146 */
147 GLuint newState; /* _NEW_* flags */
148 GLuint setupNewInputs;
149 GLuint setupIndex;
150 GLuint renderIndex;
151 GLmatrix ViewportMatrix;
152 GLenum renderPrimitive;
153 GLenum reducedPrimitive;
154 GLuint hwPrimitive;
155 unsigned char *verts;
156
157 /* drmBufPtr dma_buffer;
158 */
159 unsigned char* dmaAddr;
160 GLuint dmaIndex;
161 GLuint dmaLow;
162 GLuint dmaHigh;
163 GLuint dmaLastPrim;
164 GLboolean useAgp;
165
166 GLboolean uploadCliprects;
167
168 GLuint needUploadAllState;
169 GLuint primitiveRendered;
170
171
172 /* Fallback rasterization functions
173 */
174 via_point_func drawPoint;
175 via_line_func drawLine;
176 via_tri_func drawTri;
177
178 /* Hardware register
179 */
180 GLuint regCmdA;
181 GLuint regCmdA_End;
182 GLuint regCmdB;
183
184 GLuint regEnable;
185 GLuint regHFBBMSKL;
186 GLuint regHROP;
187
188 GLuint regHZWTMD;
189 GLuint regHSTREF;
190 GLuint regHSTMD;
191
192 GLuint regHATMD;
193 GLuint regHABLCsat;
194 GLuint regHABLCop;
195 GLuint regHABLAsat;
196 GLuint regHABLAop;
197 GLuint regHABLRCa;
198 GLuint regHABLRFCa;
199 GLuint regHABLRCbias;
200 GLuint regHABLRCb;
201 GLuint regHABLRFCb;
202 GLuint regHABLRAa;
203 GLuint regHABLRAb;
204 GLuint regHFogLF;
205 GLuint regHFogCL;
206 GLuint regHFogCH;
207
208 GLuint regHLP;
209 GLuint regHLPRF;
210
211 GLuint regHTXnTB_0;
212 GLuint regHTXnMPMD_0;
213 GLuint regHTXnTBLCsat_0;
214 GLuint regHTXnTBLCop_0;
215 GLuint regHTXnTBLMPfog_0;
216 GLuint regHTXnTBLAsat_0;
217 GLuint regHTXnTBLRCb_0;
218 GLuint regHTXnTBLRAa_0;
219 GLuint regHTXnTBLRFog_0;
220 /*=* John Sheng [2003.7.18] texture combine *=*/
221 GLuint regHTXnTBLRCa_0;
222 GLuint regHTXnTBLRCc_0;
223 GLuint regHTXnTBLRCbias_0;
224
225 GLuint regHTXnTB_1;
226 GLuint regHTXnMPMD_1;
227 GLuint regHTXnTBLCsat_1;
228 GLuint regHTXnTBLCop_1;
229 GLuint regHTXnTBLMPfog_1;
230 GLuint regHTXnTBLAsat_1;
231 GLuint regHTXnTBLRCb_1;
232 GLuint regHTXnTBLRAa_1;
233 GLuint regHTXnTBLRFog_1;
234
235 /* Hardware state
236 */
237 GLuint dirty;
238 int vertexSize;
239 int vertexStrideShift;
240 GLint lastStamp;
241 GLboolean stippleInHw;
242
243 GLenum TexEnvImageFmt[2];
244 GLuint ClearColor;
245 /* DRI stuff
246 */
247 GLuint needClip;
248 GLframebuffer *glBuffer;
249 GLboolean doPageFlip;
250 /*=* John Sheng [2003.5.31] flip *=*/
251 GLuint currentPage;
252 char *drawMap; /* draw buffer address in virtual mem */
253 char *readMap;
254 int drawX; /* origin of drawable in draw buffer */
255 int drawY;
256
257 int drawW;
258 int drawH;
259 GLuint saam;
260 #ifdef USE_XINERAMA
261 XineramaScreenInfo *xsi;
262 #endif
263 int drawXoffSaam;
264 drm_clip_rect_t *pSaamRects;
265 int drawXSaam;
266 int drawYSaam;
267 GLuint numSaamRects;
268
269 int drawPitch;
270 int readPitch;
271 int drawXoff;
272 GLuint numClipRects; /* cliprects for that buffer */
273 drm_clip_rect_t *pClipRects;
274
275 int lastSwap;
276 int texAge;
277 int ctxAge;
278 int dirtyAge;
279
280 GLboolean scissor;
281 drm_clip_rect_t drawRect;
282 drm_clip_rect_t scissorRect;
283
284 drm_context_t hHWContext;
285 drm_hw_lock_t *driHwLock;
286 int driFd;
287 __DRInativeDisplay *display;
288
289 __DRIdrawablePrivate *driDrawable;
290 __DRIscreenPrivate *driScreen;
291 viaScreenPrivate *viaScreen;
292 drm_via_sarea_t *sarea;
293 volatile GLuint* regMMIOBase;
294 volatile GLuint* pnGEMode;
295 volatile GLuint* regEngineStatus;
296 volatile GLuint* regTranSet;
297 volatile GLuint* regTranSpace;
298 GLuint* agpBase;
299 GLuint drawType;
300 /*=* John Sheng [2003.12.9] Tuxracer & VQ *=*/
301 int VQEnable;
302 };
303 /*#define DMA_OFFSET 16*/
304 #define DMA_OFFSET 32
305 /*#define PERFORMANCE_MEASURE*/
306
307 extern GLuint VIA_PERFORMANCE;
308
309 #ifdef PERFORMANCE_MEASURE
310 #define HASH_TABLE_SIZE 1000
311 #define HASH_TABLE_DEPTH 10
312 typedef struct {
313 char func[50];
314 GLuint count;
315 } hash_element;
316 extern hash_element hash_table[HASH_TABLE_SIZE][HASH_TABLE_DEPTH];
317 #define P_M \
318 do { \
319 GLuint h_index,h_depth; \
320 h_index = (GLuint)(((GLuint) __FUNCTION__)%HASH_TABLE_SIZE); \
321 for (h_depth = 0; h_depth < HASH_TABLE_DEPTH; h_depth++) { \
322 if (!strcmp(hash_table[h_index][h_depth].func, "NULL")) { \
323 sprintf(hash_table[h_index][h_depth].func, "%s", __FUNCTION__); \
324 hash_table[h_index][h_depth].count++; \
325 break; \
326 } \
327 else if (!strcmp(hash_table[h_index][h_depth].func, __FUNCTION__)) { \
328 hash_table[h_index][h_depth].count++; \
329 break; \
330 } \
331 } \
332 } while (0)
333
334 #define P_M_X \
335 do { \
336 GLuint h_index,h_depth; \
337 char str[80]; \
338 strcpy(str, __FUNCTION__); \
339 h_index = (GLuint)(((GLuint) __FUNCTION__)%HASH_TABLE_SIZE); \
340 for (h_depth = 0; h_depth < HASH_TABLE_DEPTH; h_depth++) { \
341 if (!strcmp(hash_table[h_index][h_depth].func, "NULL")) { \
342 sprintf(hash_table[h_index][h_depth].func, "%s_X", __FUNCTION__); \
343 hash_table[h_index][h_depth].count++; \
344 break; \
345 } \
346 else if (!strcmp(hash_table[h_index][h_depth].func, strcat(str, "_X"))) { \
347 hash_table[h_index][h_depth].count++; \
348 break; \
349 } \
350 } \
351 } while (0)
352
353 #define P_M_R \
354 do { \
355 GLuint h_size, h_depth; \
356 for (h_size = 0; h_size < HASH_TABLE_SIZE; h_size++) { \
357 for (h_depth = 0; h_depth < HASH_TABLE_DEPTH; h_depth++) { \
358 if (hash_table[h_size][h_depth].count) { \
359 fprintf(stderr, "func:%s count:%d\n", hash_table[h_size][h_depth].func, hash_table[h_size][h_depth].count); \
360 } \
361 } \
362 } \
363 } while (0)
364 #else /* PERFORMANCE_MEASURE */
365 #define P_M {}
366 #define P_M_X {}
367 #define P_M_R {}
368 #endif
369
370 #define VIA_CONTEXT(ctx) ((viaContextPtr)(ctx->DriverCtx))
371
372 #define GET_DISPATCH_AGE(vmesa) vmesa->sarea->lastDispatch
373 #define GET_ENQUEUE_AGE(vmesa) vmesa->sarea->lastEnqueue
374
375
376 /* Lock the hardware and validate our state.
377 */
378 /*
379 #define LOCK_HARDWARE(vmesa) \
380 do { \
381 char __ret = 0; \
382 DRM_CAS(vmesa->driHwLock, vmesa->hHWContext, \
383 (DRM_LOCK_HELD|vmesa->hHWContext), __ret); \
384 if (__ret) \
385 viaGetLock(vmesa, 0); \
386 } while (0)
387 */
388 /*=* John Sheng [2003.6.20] fix pci *=*/
389 /*=* John Sheng [2003.7.25] fix viewperf black shadow *=*/
390 #define LOCK_HARDWARE(vmesa) \
391 if(1) \
392 do { \
393 char __ret = 0; \
394 DRM_CAS(vmesa->driHwLock, vmesa->hHWContext, \
395 (DRM_LOCK_HELD|vmesa->hHWContext), __ret); \
396 if (__ret) \
397 viaGetLock(vmesa, 0); \
398 } while (0); \
399 else \
400 viaLock(vmesa, 0)
401
402
403 /*
404 #define LOCK_HARDWARE(vmesa) \
405 viaLock(vmesa, 0);
406 */
407
408 /* Release the kernel lock.
409 */
410 /*
411 #define UNLOCK_HARDWARE(vmesa) \
412 DRM_UNLOCK(vmesa->driFd, vmesa->driHwLock, vmesa->hHWContext);
413 */
414 /*=* John Sheng [2003.6.20] fix pci *=*/
415 /*=* John Sheng [2003.7.25] fix viewperf black shadow *=*/
416 #define UNLOCK_HARDWARE(vmesa) \
417 if(1) \
418 DRM_UNLOCK(vmesa->driFd, vmesa->driHwLock, vmesa->hHWContext); \
419 else \
420 viaUnLock(vmesa, 0);
421 /*
422 #define UNLOCK_HARDWARE(vmesa) \
423 viaUnLock(vmesa, 0);
424 */
425 #define WAIT_IDLE \
426 while (1) { \
427 if ((((GLuint)*vmesa->regEngineStatus) & 0xFFFEFFFF) == 0x00020000) \
428 break; \
429 }
430
431 #define LOCK_HARDWARE_QUIESCENT(vmesa) \
432 do { \
433 LOCK_HARDWARE(vmesa); \
434 viaRegetLockQuiescent(vmesa); \
435 } while (0)
436
437
438 extern GLuint VIA_DEBUG;
439 extern GLuint DRAW_FRONT;
440 extern void viaGetLock(viaContextPtr vmesa, GLuint flags);
441 extern void viaLock(viaContextPtr vmesa, GLuint flags);
442 extern void viaUnLock(viaContextPtr vmesa, GLuint flags);
443 extern void viaEmitHwStateLocked(viaContextPtr vmesa);
444 extern void viaEmitScissorValues(viaContextPtr vmesa, int box_nr, int emit);
445 extern void viaEmitDrawingRectangle(viaContextPtr vmesa);
446 extern void viaXMesaSetBackClipRects(viaContextPtr vmesa);
447 extern void viaXMesaSetFrontClipRects(viaContextPtr vmesa);
448 extern void viaReAllocateBuffers(GLframebuffer *drawbuffer);
449 extern void viaXMesaWindowMoved(viaContextPtr vmesa);
450
451 extern void viaTexCombineState(viaContextPtr vmesa,
452 const struct gl_tex_env_combine_state * combine, unsigned unit );
453
454 #define SUBPIXEL_X -.5
455 #define SUBPIXEL_Y -.5
456
457 /* TODO XXX _SOLO temp defines to make code compilable */
458 #ifndef GLX_PBUFFER_BIT
459 #define GLX_PBUFFER_BIT 0x00000004
460 #endif
461 #ifndef GLX_WINDOW_BIT
462 #define GLX_WINDOW_BIT 0x00000001
463 #endif
464 #ifndef VERT_BIT_CLIP
465 #define VERT_BIT_CLIP 0x1000000
466 #endif
467
468 #endif