Tizen 2.0 Release
[profile/ivi/osmesa.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 #include "dri_util.h"
30
31 #include "drm.h"
32
33 #include "main/mtypes.h"
34 #include "main/mm.h"
35 #include "tnl/t_vertex.h"
36
37 #include "via_screen.h"
38 #include "via_tex.h"
39 #include "via_drm.h"
40
41 struct via_context;
42
43 /* Chip tags.  These are used to group the adapters into
44  * related families.
45  */
46 enum VIACHIPTAGS {
47     VIA_UNKNOWN = 0,
48     VIA_CLE266,
49     VIA_KM400,
50     VIA_K8M800,
51     VIA_PM800,
52     VIA_LAST
53 };
54
55 #define VIA_FALLBACK_TEXTURE            0x1
56 #define VIA_FALLBACK_DRAW_BUFFER        0x2
57 #define VIA_FALLBACK_READ_BUFFER        0x4
58 #define VIA_FALLBACK_COLORMASK          0x8
59 #define VIA_FALLBACK_SPECULAR           0x20
60 #define VIA_FALLBACK_LOGICOP            0x40
61 #define VIA_FALLBACK_RENDERMODE         0x80
62 #define VIA_FALLBACK_STENCIL            0x100
63 #define VIA_FALLBACK_BLEND_EQ           0x200
64 #define VIA_FALLBACK_BLEND_FUNC         0x400
65 #define VIA_FALLBACK_USER_DISABLE       0x800
66 #define VIA_FALLBACK_PROJ_TEXTURE       0x1000
67 #define VIA_FALLBACK_POLY_STIPPLE       0x2000
68
69 #define VIA_DMA_BUFSIZ                  4096
70 #define VIA_DMA_HIGHWATER               (VIA_DMA_BUFSIZ - 128)
71
72 #define VIA_NO_CLIPRECTS 0x1
73
74
75 /* Use the templated vertex formats:
76  */
77 #define TAG(x) via##x
78 #include "tnl_dd/t_dd_vertex.h"
79 #undef TAG
80
81 typedef void (*via_tri_func)(struct via_context *, viaVertex *, viaVertex *,
82                              viaVertex *);
83 typedef void (*via_line_func)(struct via_context *, viaVertex *, viaVertex *);
84 typedef void (*via_point_func)(struct via_context *, viaVertex *);
85
86 /**
87  * Derived from gl_renderbuffer.
88  */
89 struct via_renderbuffer {
90    struct gl_renderbuffer Base;  /* must be first! */
91    drm_handle_t handle;
92    drmSize size;
93    unsigned long offset;
94    unsigned long index;
95    GLuint pitch;
96    GLuint bpp;
97    char *map;
98    GLuint orig;         /* The drawing origin, 
99                          * at (drawX,drawY) in screen space.
100                          */
101    char *origMap;
102
103    int drawX;                   /* origin of drawable in draw buffer */
104    int drawY;    
105    int drawW;                  
106    int drawH;    
107
108    __DRIdrawable *dPriv;
109 };
110
111
112 #define VIA_MAX_TEXLEVELS       10
113
114 struct via_tex_buffer {
115    struct via_tex_buffer *next, *prev;
116    struct via_texture_image *image;
117    unsigned long index;
118    unsigned long offset;
119    GLuint size;
120    GLuint memType;    
121    unsigned char *bufAddr;
122    GLuint texBase;
123    GLuint lastUsed;
124 };
125
126
127
128 struct via_texture_image {
129    struct gl_texture_image image;
130    struct via_tex_buffer *texMem;
131    GLint pitchLog2;
132 };
133
134 struct via_texture_object {
135    struct gl_texture_object obj; /* The "parent" object */
136
137    GLuint texelBytes;
138    GLuint memType;
139
140    GLuint regTexFM;
141    GLuint regTexWidthLog2[2];
142    GLuint regTexHeightLog2[2];
143    GLuint regTexBaseH[4];
144    struct {
145       GLuint baseL;
146       GLuint pitchLog2;
147    } regTexBaseAndPitch[12];
148
149    GLint firstLevel, lastLevel;  /* upload tObj->Image[first .. lastLevel] */
150 };              
151
152
153
154 struct via_context {
155    GLint refcount;   
156    struct gl_context *glCtx;
157    struct gl_context *shareCtx;
158
159    /* XXX These don't belong here.  They should be per-drawable state. */
160    struct via_renderbuffer front;
161    struct via_renderbuffer back;
162    struct via_renderbuffer depth;
163    struct via_renderbuffer stencil; /* mirrors depth */
164    struct via_renderbuffer breadcrumb;
165
166    GLboolean hasBack;
167    GLboolean hasDepth;
168    GLboolean hasStencil;
169    GLboolean hasAccum;
170    GLuint    depthBits;
171    GLuint    stencilBits;
172
173    GLboolean have_hw_stencil;
174    GLuint ClearDepth;
175    GLuint depth_clear_mask;
176    GLuint stencil_clear_mask;
177    GLfloat depth_max;
178    GLfloat polygon_offset_scale;
179
180    GLubyte    *dma;
181    viaRegion tex;
182     
183    /* Bit flag to keep 0track of fallbacks.
184     */
185    GLuint Fallback;
186
187    /* State for via_tris.c.
188     */
189    GLuint newState;            /* _NEW_* flags */
190    GLuint newEmitState;            /* _NEW_* flags */
191    GLuint newRenderState;            /* _NEW_* flags */
192
193    struct tnl_attr_map vertex_attrs[VERT_ATTRIB_MAX];
194    GLuint vertex_attr_count;
195
196    GLuint setupIndex;
197    GLuint renderIndex;
198    GLmatrix ViewportMatrix;
199    GLenum renderPrimitive;
200    GLenum hwPrimitive;
201    GLenum hwShadeModel;
202    unsigned char *verts;
203
204    /* drmBufPtr dma_buffer;
205     */
206    GLuint dmaLow;
207    GLuint dmaCliprectAddr;
208    GLuint dmaLastPrim;
209    GLboolean useAgp;
210    
211
212    /* Fallback rasterization functions 
213     */
214    via_point_func drawPoint;
215    via_line_func drawLine;
216    via_tri_func drawTri;
217
218    /* Hardware register
219     */
220    GLuint regCmdA_End;
221    GLuint regCmdB;
222
223    GLuint regEnable;
224    GLuint regHFBBMSKL;
225    GLuint regHROP;
226
227    GLuint regHZWTMD;
228    GLuint regHSTREF;
229    GLuint regHSTMD;
230
231    GLuint regHATMD;
232    GLuint regHABLCsat;
233    GLuint regHABLCop;
234    GLuint regHABLAsat;
235    GLuint regHABLAop;
236    GLuint regHABLRCa;
237    GLuint regHABLRFCa;
238    GLuint regHABLRCbias;
239    GLuint regHABLRCb;
240    GLuint regHABLRFCb;
241    GLuint regHABLRAa;
242    GLuint regHABLRAb;
243    GLuint regHFogLF;
244    GLuint regHFogCL;
245    GLuint regHFogCH;
246
247    GLuint regHLP;
248    GLuint regHLPRF;
249    
250    GLuint regHTXnCLOD[2];
251    GLuint regHTXnTB[2];
252    GLuint regHTXnMPMD[2];
253    GLuint regHTXnTBLCsat[2];
254    GLuint regHTXnTBLCop[2];
255    GLuint regHTXnTBLMPfog[2];
256    GLuint regHTXnTBLAsat[2];
257    GLuint regHTXnTBLRCb[2];
258    GLuint regHTXnTBLRAa[2];
259    GLuint regHTXnTBLRFog[2];
260    GLuint regHTXnTBLRCa[2];
261    GLuint regHTXnTBLRCc[2];
262    GLuint regHTXnTBLRCbias[2];
263    GLuint regHTXnTBC[2];
264    GLuint regHTXnTRAH[2];
265
266    int vertexSize;
267    int hwVertexSize;
268    GLboolean ptexHack;
269    int coloroffset;
270    int specoffset;
271
272    GLint lastStamp;
273
274    GLuint ClearColor;
275    GLuint ClearMask;
276
277    /* DRI stuff
278     */
279    GLboolean doPageFlip;
280
281    struct via_renderbuffer *drawBuffer;
282
283    GLuint numClipRects;         /* cliprects for that buffer */
284    drm_clip_rect_t *pClipRects;
285
286    GLboolean scissor;
287    drm_clip_rect_t drawRect;
288    drm_clip_rect_t scissorRect;
289
290    drm_context_t hHWContext;
291    drm_hw_lock_t *driHwLock;
292    int driFd;
293
294    /**
295     * DRI drawable bound to this context for drawing.
296     */
297    __DRIdrawable        *driDrawable;
298
299    /**
300     * DRI drawable bound to this context for reading.
301     */
302    __DRIdrawable        *driReadable;
303
304    __DRIscreen *driScreen;
305    viaScreenPrivate *viaScreen;
306    drm_via_sarea_t *sarea;
307    volatile GLuint* regMMIOBase;
308    volatile GLuint* pnGEMode;
309    volatile GLuint* regEngineStatus;
310    volatile GLuint* regTranSet;
311    volatile GLuint* regTranSpace;
312    GLuint agpBase;
313    GLuint drawType;
314
315    GLuint nDoneFirstFlip;
316    GLuint agpFullCount;
317
318    GLboolean clearTexCache;
319    GLboolean thrashing;
320
321    /* Configuration cache
322     */
323    driOptionCache optionCache;
324
325    int64_t swap_ust;
326    int64_t swap_missed_ust;
327
328    GLuint swap_count;
329    GLuint swap_missed_count;
330
331
332    GLuint pfCurrentOffset;
333    GLboolean allowPageFlip;
334
335    GLuint lastBreadcrumbRead;
336    GLuint lastBreadcrumbWrite;
337    GLuint lastSwap[2];
338    GLuint lastDma;
339    
340    GLuint total_alloc[VIA_MEM_SYSTEM+1];
341
342    struct via_tex_buffer tex_image_list[VIA_MEM_SYSTEM+1];
343    struct via_tex_buffer freed_tex_buffers;
344    
345 };
346
347
348
349 #define VIA_CONTEXT(ctx)   ((struct via_context *)(ctx->DriverCtx))
350
351
352
353 /* Lock the hardware and validate our state.  
354  */
355 #define LOCK_HARDWARE(vmesa)                                    \
356         do {                                                    \
357             char __ret = 0;                                     \
358             DRM_CAS(vmesa->driHwLock, vmesa->hHWContext,        \
359                 (DRM_LOCK_HELD|vmesa->hHWContext), __ret);      \
360             if (__ret)                                          \
361                 viaGetLock(vmesa, 0);                           \
362         } while (0)
363
364
365 /* Release the kernel lock.
366  */
367 #define UNLOCK_HARDWARE(vmesa)                                          \
368         DRM_UNLOCK(vmesa->driFd, vmesa->driHwLock, vmesa->hHWContext);  
369
370         
371
372 extern GLuint VIA_DEBUG;
373
374 #define DEBUG_TEXTURE   0x1
375 #define DEBUG_STATE     0x2
376 #define DEBUG_IOCTL     0x4
377 #define DEBUG_PRIMS     0x8
378 #define DEBUG_VERTS     0x10
379 #define DEBUG_FALLBACKS 0x20
380 #define DEBUG_VERBOSE   0x40
381 #define DEBUG_DRI       0x80
382 #define DEBUG_DMA       0x100
383 #define DEBUG_SANITY    0x200
384 #define DEBUG_SYNC      0x400
385 #define DEBUG_SLEEP     0x800
386 #define DEBUG_PIXEL     0x1000
387 #define DEBUG_2D        0x2000
388
389
390 extern void viaGetLock(struct via_context *vmesa, GLuint flags);
391 extern void viaLock(struct via_context *vmesa, GLuint flags);
392 extern void viaUnLock(struct via_context *vmesa, GLuint flags);
393 extern void viaEmitHwStateLocked(struct via_context *vmesa);
394 extern void viaEmitScissorValues(struct via_context *vmesa, int box_nr, int emit);
395 extern void viaXMesaSetBackClipRects(struct via_context *vmesa);
396 extern void viaXMesaSetFrontClipRects(struct via_context *vmesa);
397 extern void viaReAllocateBuffers(struct gl_context *ctx, struct gl_framebuffer *drawbuffer, GLuint width, GLuint height);
398 extern void viaXMesaWindowMoved(struct via_context *vmesa);
399
400 extern GLboolean viaTexCombineState(struct via_context *vmesa,
401                                     const struct gl_tex_env_combine_state * combine, 
402                                     unsigned unit );
403
404 /* Via hw already adjusted for GL pixel centers:
405  */
406 #define SUBPIXEL_X 0
407 #define SUBPIXEL_Y 0
408
409 /* TODO XXX _SOLO temp defines to make code compilable */
410 #ifndef GLX_PBUFFER_BIT
411 #define GLX_PBUFFER_BIT        0x00000004
412 #endif
413 #ifndef GLX_WINDOW_BIT
414 #define GLX_WINDOW_BIT 0x00000001
415 #endif
416 #ifndef VERT_BIT_CLIP
417 #define VERT_BIT_CLIP       0x1000000
418 #endif
419
420 #endif