6289fb4500df28170218ef8c9c538cb4d69ad89f
[mesa.git] / src / mesa / drivers / dri / sis / sis_context.h
1 /**************************************************************************
2
3 Copyright 2000 Silicon Integrated Systems Corp, Inc., HsinChu, Taiwan.
4 Copyright 2003 Eric Anholt
5 All Rights Reserved.
6
7 Permission is hereby granted, free of charge, to any person obtaining a
8 copy of this software and associated documentation files (the "Software"),
9 to deal in the Software without restriction, including without limitation
10 on the rights to use, copy, modify, merge, publish, distribute, sub
11 license, and/or sell copies of the Software, and to permit persons to whom
12 the Software is furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice (including the next
15 paragraph) shall be included in all copies or substantial portions of the
16 Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 ERIC ANHOLT OR SILICON INTEGRATED SYSTEMS CORP BE LIABLE FOR ANY CLAIM,
22 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **************************************************************************/
27 /* $XFree86$ */
28
29 /*
30 * Authors:
31 * Sung-Ching Lin <sclin@sis.com.tw>
32 * Eric Anholt <anholt@FreeBSD.org>
33 */
34
35 #ifndef _sis_ctx_h_
36 #define _sis_ctx_h_
37
38 #include "context.h"
39 #include "dri_util.h"
40 #include "drm.h"
41 #include "drm_sarea.h"
42 #include "xmlconfig.h"
43 #include "tnl/t_vertex.h"
44
45 #include "sis_screen.h"
46 #include "sis_reg.h"
47 #include "sis6326_reg.h"
48 #include "sis_dri.h"
49
50 /* for GLboolean */
51 #include <GL/gl.h>
52
53 #define PCI_CHIP_SIS300 0x0300
54 #define PCI_CHIP_SIS630 0x6300
55 #define PCI_CHIP_SIS540 0x5300
56
57 #define NEW_TEXTURING 0x1
58 #define NEW_TEXTURE_ENV 0x2
59
60 /* Flags for software fallback cases:
61 */
62 #define SIS_FALLBACK_TEXTURE 0x0001
63 #define SIS_FALLBACK_TEXTURE0 0x0002
64 #define SIS_FALLBACK_TEXTURE1 0x0004
65 #define SIS_FALLBACK_TEXENV0 0x0008
66 #define SIS_FALLBACK_TEXENV1 0x0010
67 #define SIS_FALLBACK_DRAW_BUFFER 0x0020
68 #define SIS_FALLBACK_STENCIL 0x0040
69 #define SIS_FALLBACK_WRITEMASK 0x0080
70 #define SIS_FALLBACK_DISABLE 0x0100
71
72 /* Flags for hardware state that needs to be updated */
73 #define GFLAG_ENABLESETTING 0x00000001
74 #define GFLAG_ENABLESETTING2 0x00000002
75 #define GFLAG_ZSETTING 0x00000004
76 #define GFLAG_ALPHASETTING 0x00000008
77 #define GFLAG_DESTSETTING 0x00000010
78 #define GFLAG_LINESETTING 0x00000020
79 #define GFLAG_STENCILSETTING 0x00000040
80 #define GFLAG_FOGSETTING 0x00000080
81 #define GFLAG_DSTBLEND 0x00000100
82 #define GFLAG_CLIPPING 0x00000200
83 #define CFLAG_TEXTURERESET 0x00000400
84 #define GFLAG_TEXTUREMIPMAP 0x00000800
85 #define GFLAG_TEXBORDERCOLOR 0x00001000
86 #define GFLAG_TEXTUREADDRESS 0x00002000
87 #define GFLAG_TEXTUREENV 0x00004000
88 #define CFLAG_TEXTURERESET_1 0x00008000
89 #define GFLAG_TEXTUREMIPMAP_1 0x00010000
90 #define GFLAG_TEXBORDERCOLOR_1 0x00020000
91 #define GFLAG_TEXTUREADDRESS_1 0x00040000
92 #define GFLAG_TEXTUREENV_1 0x00080000
93 #define GFLAG_ALL 0x000fffff
94
95 #define GFLAG_TEXTURE_STATES (CFLAG_TEXTURERESET | GFLAG_TEXTUREMIPMAP | \
96 GFLAG_TEXBORDERCOLOR | GFLAG_TEXTUREADDRESS | \
97 CFLAG_TEXTURERESET_1 | GFLAG_TEXTUREMIPMAP_1 | \
98 GFLAG_TEXBORDERCOLOR_1 | \
99 GFLAG_TEXTUREADDRESS_1 | \
100 GFLAG_TEXTUREENV | GFLAG_TEXTUREENV_1)
101
102
103 #define GFLAG_RENDER_STATES (GFLAG_ENABLESETTING | GFLAG_ENABLESETTING2 | \
104 GFLAG_ZSETTING | GFLAG_ALPHASETTING | \
105 GFLAG_DESTSETTING | GFLAG_FOGSETTING | \
106 GFLAG_STENCILSETTING | GFLAG_DSTBLEND | \
107 GFLAG_CLIPPING)
108
109 /* Use the templated vertex format:
110 */
111 #define TAG(x) sis##x
112 #include "tnl_dd/t_dd_vertex.h"
113 #undef TAG
114
115 /* Subpixel offsets for window coordinates (triangles):
116 */
117 #define SUBPIXEL_X (-0.5F)
118 #define SUBPIXEL_Y (-0.5F)
119
120 #define SIS_MAX_TEXTURE_SIZE 2048
121 #define SIS_MAX_TEXTURES 2
122 #define SIS_MAX_TEXTURE_LEVELS 11
123 #define SIS_MAX_FRAME_LENGTH 3
124
125 typedef struct {
126 GLubyte *Data; /* Pointer to texture in offscreen */
127 GLuint memType; /* VIDEO_TYPE or AGP_TYPE */
128 void *handle; /* Handle for sisFree*() */
129 GLuint pitch;
130 GLuint size;
131 } sisTexImage;
132
133 typedef struct sis_tex_obj {
134 sisTexImage image[SIS_MAX_TEXTURE_LEVELS]; /* Image data for each mipmap
135 * level */
136 GLenum format; /* One of GL_ALPHA, GL_INTENSITY, GL_LUMINANCE,
137 * GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA
138 * MESA_YCBCR */
139 GLint hwformat; /* One of the TEXEL_ defines */
140 GLint numImages; /* Number of images loaded into .image */
141 } sisTexObj, *sisTexObjPtr;
142
143 /*
144 ** Device dependent context state
145 */
146 typedef struct __GLSiSTextureRec
147 {
148 GLint hwTextureSet;
149 GLint hwTextureMip;
150 GLint hwTextureClrHigh;
151 GLint hwTextureClrLow;
152 GLint hwTexWidthHeight; /* 6326: Texture Blending Setting */
153 GLint hwTextureBorderColor;
154
155 GLint texOffset0;
156 GLint texOffset1;
157 GLint texOffset2;
158 GLint texOffset3;
159 GLint texOffset4;
160 GLint texOffset5;
161 GLint texOffset6;
162 GLint texOffset7;
163 GLint texOffset8;
164 GLint texOffset9;
165 GLint texOffset10;
166 GLint texOffset11;
167
168 GLint texPitch01;
169 GLint texPitch23;
170 GLint texPitch45;
171 GLint texPitch67;
172 GLint texPitch89;
173 GLint texPitch10;
174 } __GLSiSTexture;
175
176 typedef struct __GLSiSHardwareRec
177 {
178 GLint hwCapEnable, hwCapEnable2; /* Enable Setting */
179
180 GLint hwOffsetZ, hwZ; /* Z Setting */
181
182 GLint hwZBias, hwZMask; /* Z Setting */
183
184 GLint hwAlpha; /* Alpha Setting */
185
186 GLint hwDstSet, hwDstMask; /* Destination Setting */
187
188 GLint hwOffsetDest; /* Destination Setting */
189
190 GLint hwLinePattern; /* Line Setting */
191
192 GLint hwFog; /* Fog Setting */
193
194 GLint hwFogFar, hwFogInverse; /* Fog Distance setting */
195
196 GLint hwFogDensity; /* Fog factor & density */
197
198 GLint hwStSetting, hwStSetting2; /* Stencil Setting */
199
200 GLint hwStOffset; /* Stencil Setting */
201
202 GLint hwDstSrcBlend; /* Blending mode Setting */
203
204 GLint clipTopBottom; /* Clip for Top & Bottom */
205
206 GLint clipLeftRight; /* Clip for Left & Right */
207
208 struct __GLSiSTextureRec texture[2];
209
210 GLint hwTexEnvColor; /* Texture Blending Setting */
211
212 GLint hwTexBlendSet; /* 6326 */
213 GLint hwTexBlendColor0;
214 GLint hwTexBlendColor1;
215 GLint hwTexBlendAlpha0;
216 GLint hwTexBlendAlpha1;
217
218 }
219 __GLSiSHardware;
220
221 typedef struct sis_context sisContextRec;
222 typedef struct sis_context *sisContextPtr;
223
224 typedef void (*sis_quad_func)( sisContextPtr,
225 sisVertex *,
226 sisVertex *,
227 sisVertex *,
228 sisVertex * );
229
230 typedef void (*sis_tri_func)( sisContextPtr,
231 sisVertex *,
232 sisVertex *,
233 sisVertex * );
234
235 typedef void (*sis_line_func)( sisContextPtr,
236 sisVertex *,
237 sisVertex * );
238
239 typedef void (*sis_point_func)( sisContextPtr,
240 sisVertex * );
241
242 /**
243 * Derived from gl_renderbuffer.
244 */
245 struct sis_renderbuffer {
246 struct gl_renderbuffer Base; /* must be first! */
247 drmSize size;
248 GLuint offset;
249 void *handle;
250 GLuint pitch;
251 GLuint bpp;
252 char *map;
253 };
254
255 /* Device dependent context state */
256
257 struct sis_context
258 {
259 /* This must be first in this structure */
260 GLcontext *glCtx;
261
262 /* Vertex state */
263 GLuint vertex_size;
264 struct tnl_attr_map vertex_attrs[VERT_ATTRIB_MAX];
265 GLuint vertex_attr_count;
266 char *verts; /* points to tnl->clipspace.vertex_buf */
267
268 /* Vertex buffer (in system memory or AGP) state. */
269 unsigned char *vb; /* Beginning of vertex buffer */
270 unsigned char *vb_cur; /* Current write location in vertex buffer */
271 unsigned char *vb_last; /* Last written location in vertex buffer */
272 unsigned char *vb_end; /* End of vertex buffer */
273 void *vb_agp_handle;
274 GLuint vb_agp_offset;
275 GLboolean using_agp;
276 GLint coloroffset; /* Offset in vertex format of current color */
277 GLint specoffset; /* Offset in vertex format of specular color */
278
279 GLuint NewGLState;
280 GLuint Fallback;
281 GLuint RenderIndex;
282 GLfloat hw_viewport[16];
283 GLfloat depth_scale;
284
285 unsigned int virtualX, virtualY;
286 unsigned int bytesPerPixel;
287 unsigned char *IOBase;
288 unsigned char *FbBase;
289 unsigned int displayWidth;
290
291 /* HW RGBA layout */
292 unsigned int redMask, greenMask, blueMask, alphaMask;
293 unsigned int colorFormat;
294
295 /* Z format */
296 unsigned int zFormat;
297
298 /* Clear patterns, 4 bytes */
299 unsigned int clearColorPattern;
300 unsigned int clearZStencilPattern;
301
302 /* Fallback rasterization functions
303 */
304 sis_point_func draw_point;
305 sis_line_func draw_line;
306 sis_tri_func draw_tri;
307 sis_quad_func draw_quad;
308
309 GLuint hw_primitive;
310 GLenum raster_primitive;
311 GLenum render_primitive;
312
313 /* DRM fd */
314 int driFd;
315
316 /* AGP Memory */
317 unsigned int AGPSize;
318 unsigned char *AGPBase;
319 unsigned int AGPAddr;
320
321 /* register 0x89F4 */
322 GLint AGPParseSet;
323
324 /* register 0x89F8 */
325 GLint dwPrimitiveSet;
326
327 __GLSiSHardware prev, current;
328
329 int Chipset;
330 GLboolean is6326;
331
332 GLint drawableID;
333
334 GLint GlobalFlag;
335 DECLARE_RENDERINPUTS(last_tcl_state_bitset);
336
337 /* Stereo */
338 GLboolean useStereo;
339 GLboolean stereoEnabled;
340 int stereo_drawIndex;
341 int stereo_drawSide;
342 GLboolean irqEnabled;
343
344 GLboolean clearTexCache;
345
346 GLuint TexStates[SIS_MAX_TEXTURES];
347 GLuint PrevTexFormat[SIS_MAX_TEXTURES];
348
349 int *CurrentQueueLenPtr;
350 unsigned int *FrameCountPtr;
351
352 /* Front/back/depth buffer info */
353 GLuint width, height; /* size of buffers */
354 GLint bottom; /* used for FLIP macro */
355 /* XXX These don't belong here. They should be per-drawable state. */
356 struct sis_renderbuffer front;
357 struct sis_renderbuffer back;
358 struct sis_renderbuffer depth;
359 struct sis_renderbuffer stencil; /* mirrors depth */
360
361 /* Mirrors of some DRI state
362 */
363 __DRIcontextPrivate *driContext; /* DRI context */
364 __DRIscreenPrivate *driScreen; /* DRI screen */
365 __DRIdrawablePrivate *driDrawable; /* DRI drawable bound to this ctx */
366
367 unsigned int lastStamp; /* mirror driDrawable->lastStamp */
368
369 drm_context_t hHWContext;
370 drm_hw_lock_t *driHwLock;
371
372 sisScreenPtr sisScreen; /* Screen private DRI data */
373 SISSAREAPrivPtr sarea; /* Private SAREA data */
374
375 /* Configuration cache */
376 driOptionCache optionCache;
377 GLint texture_depth;
378 };
379
380 #define SIS_CONTEXT(ctx) ((sisContextPtr)(ctx->DriverCtx))
381
382 /* Macros */
383 #define GET_IOBase(x) ((x)->IOBase)
384
385 #define Y_FLIP(Y) (smesa->bottom - (Y))
386
387 #define SISPACKCOLOR565( r, g, b ) \
388 ((((r) & 0xf8) << 8) | (((g) & 0xfc) << 3) | (((b) & 0xf8) >> 3))
389
390 #define SISPACKCOLOR8888( r, g, b, a ) \
391 (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
392
393 #define SIS_VERBOSE 0
394
395
396 #define MMIO(reg, value) \
397 {\
398 *(volatile GLint *)(smesa->IOBase + (reg)) = value; \
399 }
400
401 #define MMIO_READ(reg) *(volatile GLint *)(smesa->IOBase + (reg))
402 #define MMIO_READf(reg) *(volatile GLfloat *)(smesa->IOBase + (reg))
403
404 #if defined(__i386__) || defined(__amd64__)
405 #define MMIO_WMB() __asm __volatile("" : : : "memory")
406 #elif defined(__ia64__)
407 #define MMIO_WMB() __asm __volatile("mf" : : : "memory")
408 #else
409 #error platform needs WMB
410 #endif
411
412 #define mEndPrimitive() \
413 { \
414 *(volatile GLubyte *)(smesa->IOBase + REG_3D_EndPrimitiveList) = 0xff; \
415 *(volatile GLuint *)(smesa->IOBase + 0x8b60) = 0xffffffff; \
416 }
417
418 #define sis_fatal_error(...) \
419 do { \
420 fprintf(stderr, "[%s:%d]:", __FILE__, __LINE__); \
421 fprintf(stderr, __VA_ARGS__); \
422 exit(-1); \
423 } while (0)
424
425 /* Lock required */
426 #define mWait3DCmdQueue(wLen) \
427 /* Update the mirrored queue pointer if it doesn't indicate enough space */ \
428 if (*(smesa->CurrentQueueLenPtr) < (wLen)) { \
429 *(smesa->CurrentQueueLenPtr) = \
430 (*(GLint *)(GET_IOBase(smesa) + REG_CommandQueue) & MASK_QueueLen) - 20; \
431 /* Spin and wait if the queue is actually too full */ \
432 if (*(smesa->CurrentQueueLenPtr) < (wLen)) \
433 WaitingFor3dIdle(smesa, wLen); \
434 *(smesa->CurrentQueueLenPtr) -= wLen; \
435 }
436
437 enum _sis_verbose {
438 VERBOSE_SIS_BUFFER = 0x1,
439 VERBOSE_SIS_MEMORY = 0x2
440 };
441
442 extern GLboolean sisCreateContext( const __GLcontextModes *glVisual,
443 __DRIcontextPrivate *driContextPriv,
444 void *sharedContextPrivate );
445 extern void sisDestroyContext( __DRIcontextPrivate * );
446
447 void sisReAllocateBuffers(GLcontext *ctx, GLframebuffer *drawbuffer,
448 GLuint width, GLuint height);
449
450 extern GLboolean sisMakeCurrent( __DRIcontextPrivate *driContextPriv,
451 __DRIdrawablePrivate *driDrawPriv,
452 __DRIdrawablePrivate *driReadPriv );
453
454 extern GLboolean sisUnbindContext( __DRIcontextPrivate *driContextPriv );
455
456 void WaitEngIdle (sisContextPtr smesa);
457 void Wait2DEngIdle (sisContextPtr smesa);
458 void WaitingFor3dIdle(sisContextPtr smesa, int wLen);
459
460 /* update to hw */
461 extern void sis_update_texture_state( sisContextPtr smesa );
462 extern void sis_update_render_state( sisContextPtr smesa );
463 extern void sis6326_update_texture_state( sisContextPtr smesa );
464 extern void sis6326_update_render_state( sisContextPtr smesa );
465
466 /* ================================================================
467 * Debugging:
468 */
469 #define DO_DEBUG 1
470
471 #if DO_DEBUG
472 extern int SIS_DEBUG;
473 #else
474 #define SIS_DEBUG 0
475 #endif
476
477 #define DEBUG_FALLBACKS 0x01
478
479 #endif /* _sis_ctx_h_ */