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