Small optimization for big-endian (e.g., PowerPC) systems.
[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_common2.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_FORCE 0x8000
69
70 /* Flags for hardware state that needs to be updated */
71 #define GFLAG_ENABLESETTING 0x00000001
72 #define GFLAG_ENABLESETTING2 0x00000002
73 #define GFLAG_ZSETTING 0x00000004
74 #define GFLAG_ALPHASETTING 0x00000008
75 #define GFLAG_DESTSETTING 0x00000010
76 #define GFLAG_LINESETTING 0x00000020
77 #define GFLAG_STENCILSETTING 0x00000040
78 #define GFLAG_FOGSETTING 0x00000080
79 #define GFLAG_DSTBLEND 0x00000100
80 #define GFLAG_CLIPPING 0x00000200
81 #define CFLAG_TEXTURERESET 0x00000400
82 #define GFLAG_TEXTUREMIPMAP 0x00000800
83 #define GFLAG_TEXBORDERCOLOR 0x00001000
84 #define GFLAG_TEXTUREADDRESS 0x00002000
85 #define GFLAG_TEXTUREENV 0x00004000
86 #define CFLAG_TEXTURERESET_1 0x00008000
87 #define GFLAG_TEXTUREMIPMAP_1 0x00010000
88 #define GFLAG_TEXBORDERCOLOR_1 0x00020000
89 #define GFLAG_TEXTUREADDRESS_1 0x00040000
90 #define GFLAG_TEXTUREENV_1 0x00080000
91 #define GFLAG_ALL 0x000fffff
92
93 #define GFLAG_TEXTURE_STATES (CFLAG_TEXTURERESET | GFLAG_TEXTUREMIPMAP | \
94 GFLAG_TEXBORDERCOLOR | GFLAG_TEXTUREADDRESS | \
95 CFLAG_TEXTURERESET_1 | GFLAG_TEXTUREMIPMAP_1 | \
96 GFLAG_TEXBORDERCOLOR_1 | \
97 GFLAG_TEXTUREADDRESS_1 | \
98 GFLAG_TEXTUREENV | GFLAG_TEXTUREENV_1)
99
100
101 #define GFLAG_RENDER_STATES (GFLAG_ENABLESETTING | GFLAG_ENABLESETTING2 | \
102 GFLAG_ZSETTING | GFLAG_ALPHASETTING | \
103 GFLAG_DESTSETTING | GFLAG_FOGSETTING | \
104 GFLAG_STENCILSETTING | GFLAG_DSTBLEND | \
105 GFLAG_CLIPPING)
106
107 /* Use the templated vertex format:
108 */
109 #define TAG(x) sis##x
110 #include "tnl_dd/t_dd_vertex.h"
111 #undef TAG
112
113 /* Subpixel offsets for window coordinates (triangles):
114 */
115 #define SUBPIXEL_X (-0.5F)
116 #define SUBPIXEL_Y (-0.5F)
117
118 #define SIS_MAX_TEXTURE_LEVELS 11
119
120 typedef struct {
121 GLubyte *Data; /* Pointer to texture in offscreen */
122 GLuint memType; /* VIDEO_TYPE or AGP_TYPE */
123 void *handle; /* Handle for sisFree*() */
124 GLuint pitch;
125 GLuint size;
126 } sisTexImage;
127
128 typedef struct sis_tex_obj {
129 sisTexImage image[SIS_MAX_TEXTURE_LEVELS]; /* Image data for each mipmap
130 * level */
131 GLenum format; /* One of GL_ALPHA, GL_INTENSITY, GL_LUMINANCE,
132 * GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA */
133 GLint hwformat; /* One of the TEXEL_ defines */
134 GLint numImages; /* Number of images loaded into .image */
135 } sisTexObj, *sisTexObjPtr;
136
137 /*
138 ** Device dependent context state
139 */
140 typedef struct __GLSiSTextureRec
141 {
142 GLint hwTextureSet;
143 GLint hwTextureMip;
144 GLint hwTextureClrHigh;
145 GLint hwTextureClrLow;
146 GLint hwTextureBorderColor;
147
148 GLint texOffset0;
149 GLint texOffset1;
150 GLint texOffset2;
151 GLint texOffset3;
152 GLint texOffset4;
153 GLint texOffset5;
154 GLint texOffset6;
155 GLint texOffset7;
156 GLint texOffset8;
157 GLint texOffset9;
158 GLint texOffset10;
159 GLint texOffset11;
160
161 GLint texPitch01;
162 GLint texPitch23;
163 GLint texPitch45;
164 GLint texPitch67;
165 GLint texPitch89;
166 GLint texPitch10;
167 } __GLSiSTexture;
168
169 typedef struct __GLSiSHardwareRec
170 {
171 GLint hwCapEnable, hwCapEnable2; /* Enable Setting */
172
173 GLint hwOffsetZ, hwZ; /* Z Setting */
174
175 GLint hwZBias, hwZMask; /* Z Setting */
176
177 GLint hwAlpha; /* Alpha Setting */
178
179 GLint hwDstSet, hwDstMask; /* Destination Setting */
180
181 GLint hwOffsetDest; /* Destination Setting */
182
183 GLint hwLinePattern; /* Line Setting */
184
185 GLint hwFog; /* Fog Setting */
186
187 GLint hwFogFar, hwFogInverse; /* Fog Distance setting */
188
189 GLint hwFogDensity; /* Fog factor & density */
190
191 GLint hwStSetting, hwStSetting2; /* Stencil Setting */
192
193 GLint hwStOffset; /* Stencil Setting */
194
195 GLint hwDstSrcBlend; /* Blending mode Setting */
196
197 GLint clipTopBottom; /* Clip for Top & Bottom */
198
199 GLint clipLeftRight; /* Clip for Left & Right */
200
201 struct __GLSiSTextureRec texture[2];
202
203 GLint hwTexEnvColor; /* Texture Blending Setting */
204
205 GLint hwTexBlendColor0;
206 GLint hwTexBlendColor1;
207 GLint hwTexBlendAlpha0;
208 GLint hwTexBlendAlpha1;
209
210 }
211 __GLSiSHardware;
212
213 typedef struct sis_context sisContextRec;
214 typedef struct sis_context *sisContextPtr;
215
216 typedef void (*sis_quad_func)( sisContextPtr,
217 sisVertex *,
218 sisVertex *,
219 sisVertex *,
220 sisVertex * );
221
222 typedef void (*sis_tri_func)( sisContextPtr,
223 sisVertex *,
224 sisVertex *,
225 sisVertex * );
226
227 typedef void (*sis_line_func)( sisContextPtr,
228 sisVertex *,
229 sisVertex * );
230
231 typedef void (*sis_point_func)( sisContextPtr,
232 sisVertex * );
233
234 /* Device dependent context state */
235
236 struct sis_context
237 {
238 /* This must be first in this structure */
239 GLcontext *glCtx;
240
241 /* Vertex state */
242 GLuint vertex_size;
243 struct tnl_attr_map vertex_attrs[VERT_ATTRIB_MAX];
244 GLuint vertex_attr_count;
245 char *verts; /* points to tnl->clipspace.vertex_buf */
246
247 /* Vertex buffer (in system memory or AGP) state. */
248 unsigned char *vb; /* Beginning of vertex buffer */
249 unsigned char *vb_cur; /* Current write location in vertex buffer */
250 unsigned char *vb_last; /* Last written location in vertex buffer */
251 unsigned char *vb_end; /* End of vertex buffer */
252 void *vb_agp_handle;
253 GLuint vb_agp_offset;
254 GLboolean using_agp;
255
256 GLuint NewGLState;
257 GLuint Fallback;
258 GLuint RenderIndex;
259 GLfloat hw_viewport[16];
260 GLfloat depth_scale;
261
262 unsigned int virtualX, virtualY;
263 unsigned int bytesPerPixel;
264 unsigned char *IOBase;
265 unsigned char *FbBase;
266 unsigned int displayWidth;
267 unsigned int frontOffset;
268 unsigned int frontPitch;
269
270 /* HW RGBA layout */
271 unsigned int redMask, greenMask, blueMask, alphaMask;
272 unsigned int colorFormat;
273
274 /* Z format */
275 unsigned int zFormat;
276
277 /* Clear patterns, 4 bytes */
278 unsigned int clearColorPattern;
279 unsigned int clearZStencilPattern;
280
281 /* Fallback rasterization functions
282 */
283 sis_point_func draw_point;
284 sis_line_func draw_line;
285 sis_tri_func draw_tri;
286 sis_quad_func draw_quad;
287
288 GLuint hw_primitive;
289 GLenum raster_primitive;
290 GLenum render_primitive;
291
292 /* DRM fd */
293 int driFd;
294
295 /* AGP Memory */
296 unsigned int AGPSize;
297 unsigned char *AGPBase;
298 unsigned int AGPAddr;
299
300 /* register 0x89F4 */
301 GLint AGPParseSet;
302
303 /* register 0x89F8 */
304 GLint dwPrimitiveSet;
305
306 __GLSiSHardware prev, current;
307
308 int Chipset;
309
310 GLint drawableID;
311
312 /* SGRAM block write */
313 GLboolean blockWrite;
314
315 GLint GlobalFlag;
316 GLuint last_tcl_state;
317
318 /* Stereo */
319 GLboolean useStereo;
320 GLboolean stereoEnabled;
321 int stereo_drawIndex;
322 int stereo_drawSide;
323 GLboolean irqEnabled;
324
325 GLboolean clearTexCache;
326
327 GLuint TexStates[SIS_MAX_TEXTURES];
328 GLuint PrevTexFormat[SIS_MAX_TEXTURES];
329
330 int *CurrentQueueLenPtr;
331 unsigned int *FrameCountPtr;
332
333 /* Front/back/depth buffer info */
334 GLuint width, height; /* size of buffers */
335 GLint bottom; /* used for FLIP macro */
336 GLvoid *backbuffer;
337 unsigned int backOffset;
338 unsigned int backPitch;
339 GLvoid *depthbuffer;
340 unsigned int depthOffset;
341 unsigned int depthPitch;
342 void *zbFree, *bbFree; /* Cookies for freeing buffers */
343 ENGPACKET zClearPacket, cbClearPacket;
344
345 /* Drawable, cliprect and scissor information
346 */
347 GLint drawOffset, drawPitch;
348 GLint readOffset, readPitch;
349
350 /* Mirrors of some DRI state
351 */
352 __DRIcontextPrivate *driContext; /* DRI context */
353 __DRIscreenPrivate *driScreen; /* DRI screen */
354 __DRIdrawablePrivate *driDrawable; /* DRI drawable bound to this ctx */
355
356 unsigned int lastStamp; /* mirror driDrawable->lastStamp */
357
358 drm_context_t hHWContext;
359 drm_hw_lock_t *driHwLock;
360
361 sisScreenPtr sisScreen; /* Screen private DRI data */
362 SISSAREAPrivPtr sarea; /* Private SAREA data */
363
364 /* Configuration cache */
365 driOptionCache optionCache;
366 };
367
368 #define SIS_CONTEXT(ctx) ((sisContextPtr)(ctx->DriverCtx))
369
370 /* Macros */
371 #define GET_IOBase(x) ((x)->IOBase)
372
373 #define Y_FLIP(Y) (smesa->bottom - (Y))
374
375 #define SISPACKCOLOR565( r, g, b ) \
376 ((((r) & 0xf8) << 8) | (((g) & 0xfc) << 3) | (((b) & 0xf8) >> 3))
377
378 #define SISPACKCOLOR8888( r, g, b, a ) \
379 (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
380
381 #define SIS_VERBOSE 0
382
383
384 #define MMIO(reg, value) \
385 {\
386 *(volatile GLint *)(smesa->IOBase + (reg)) = value; \
387 }
388
389 #define MMIO_READ(reg) *(volatile GLint *)(smesa->IOBase + (reg))
390 #define MMIO_READf(reg) *(volatile GLfloat *)(smesa->IOBase + (reg))
391
392 #define mEndPrimitive() \
393 { \
394 *(volatile GLubyte *)(smesa->IOBase + REG_3D_EndPrimitiveList) = 0xff; \
395 *(volatile GLuint *)(smesa->IOBase + 0x8b60) = 0xffffffff; \
396 }
397
398 #define sis_fatal_error(msg) \
399 do { \
400 fprintf(stderr, "[%s:%d]: %s", __FILE__, __LINE__, msg); \
401 exit(-1); \
402 } while (0)
403
404 /* Lock required */
405 #define mWait3DCmdQueue(wLen) \
406 /* Update the mirrored queue pointer if it doesn't indicate enough space */ \
407 if (*(smesa->CurrentQueueLenPtr) < (wLen)) { \
408 *(smesa->CurrentQueueLenPtr) = \
409 (*(GLint *)(GET_IOBase(smesa) + REG_CommandQueue) & MASK_QueueLen) - 20; \
410 /* Spin and wait if the queue is actually too full */ \
411 if (*(smesa->CurrentQueueLenPtr) < (wLen)) \
412 WaitingFor3dIdle(smesa, wLen); \
413 *(smesa->CurrentQueueLenPtr) -= wLen; \
414 }
415
416 enum _sis_verbose {
417 VERBOSE_SIS_BUFFER = 0x1,
418 VERBOSE_SIS_MEMORY = 0x2
419 };
420
421 extern GLboolean sisCreateContext( const __GLcontextModes *glVisual,
422 __DRIcontextPrivate *driContextPriv,
423 void *sharedContextPrivate );
424 extern void sisDestroyContext( __DRIcontextPrivate * );
425
426 extern GLboolean sisMakeCurrent( __DRIcontextPrivate *driContextPriv,
427 __DRIdrawablePrivate *driDrawPriv,
428 __DRIdrawablePrivate *driReadPriv );
429
430 extern GLboolean sisUnbindContext( __DRIcontextPrivate *driContextPriv );
431
432 void WaitEngIdle (sisContextPtr smesa);
433 void Wait2DEngIdle (sisContextPtr smesa);
434 void WaitingFor3dIdle(sisContextPtr smesa, int wLen);
435
436 /* update to hw */
437 extern void sis_update_texture_state( sisContextPtr smesa );
438 extern void sis_update_render_state( sisContextPtr smesa );
439
440 #endif