intel_context.h revision 2b81c8999f32999ee2e5822c4828c7325e3fe1ca
1/**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28#ifndef INTELCONTEXT_INC
29#define INTELCONTEXT_INC
30
31
32
33#include "mtypes.h"
34#include "drm.h"
35#include "mm.h"
36#include "texmem.h"
37
38#include "intel_screen.h"
39#include "i830_common.h"
40#include "tnl/t_vertex.h"
41
42#define TAG(x) intel##x
43#include "tnl_dd/t_dd_vertex.h"
44#undef TAG
45
46#define DV_PF_555  (1<<8)
47#define DV_PF_565  (2<<8)
48#define DV_PF_8888 (3<<8)
49
50#define INTEL_CONTEXT(ctx)	((intelContextPtr)(ctx))
51
52typedef struct intel_context intelContext;
53typedef struct intel_context *intelContextPtr;
54typedef struct intel_texture_object *intelTextureObjectPtr;
55
56typedef void (*intel_tri_func)(intelContextPtr, intelVertex *, intelVertex *,
57							  intelVertex *);
58typedef void (*intel_line_func)(intelContextPtr, intelVertex *, intelVertex *);
59typedef void (*intel_point_func)(intelContextPtr, intelVertex *);
60
61#define INTEL_FALLBACK_DRAW_BUFFER	 0x1
62#define INTEL_FALLBACK_READ_BUFFER	 0x2
63#define INTEL_FALLBACK_USER		 0x4
64#define INTEL_FALLBACK_NO_BATCHBUFFER	 0x8
65#define INTEL_FALLBACK_NO_TEXMEM	 0x10
66#define INTEL_FALLBACK_RENDERMODE	 0x20
67
68extern void intelFallback( intelContextPtr intel, GLuint bit, GLboolean mode );
69#define FALLBACK( intel, bit, mode ) intelFallback( intel, bit, mode )
70
71
72#define INTEL_TEX_MAXLEVELS 10
73
74
75struct intel_texture_object
76{
77   driTextureObject    base;	/* the parent class */
78
79   GLuint texelBytes;
80   GLuint age;
81   GLuint Pitch;
82   GLuint Height;
83   GLuint TextureOffset;
84   GLubyte *BufAddr;
85
86   GLuint min_level;
87   GLuint max_level;
88   GLuint depth_pitch;
89
90   struct {
91      const struct gl_texture_image *image;
92      GLuint offset;       /* into BufAddr */
93      GLuint height;
94      GLuint internalFormat;
95   } image[6][INTEL_TEX_MAXLEVELS];
96
97   GLuint dirty;
98   GLuint firstLevel,lastLevel;
99};
100
101
102struct intel_context
103{
104   GLcontext ctx;		/* the parent class */
105
106   struct {
107      void (*destroy)( intelContextPtr intel );
108      void (*emit_state)( intelContextPtr intel );
109      void (*emit_invarient_state)( intelContextPtr intel );
110      void (*lost_hardware)( intelContextPtr intel );
111      void (*update_texture_state)( intelContextPtr intel );
112
113      void (*render_start)( intelContextPtr intel );
114      void (*set_draw_offset)( intelContextPtr intel, int offset );
115      void (*emit_flush)( intelContextPtr intel );
116
117      void (*reduced_primitive_state)( intelContextPtr intel, GLenum rprim );
118
119      GLboolean (*check_vertex_size)( intelContextPtr intel, GLuint expected );
120
121      void (*clear_with_tris)( intelContextPtr intel, GLbitfield mask,
122			       GLboolean all,
123			       GLint cx, GLint cy, GLint cw, GLint ch);
124
125      intelTextureObjectPtr (*alloc_tex_obj)( struct gl_texture_object *tObj );
126
127   } vtbl;
128
129   GLint refcount;
130   GLuint Fallback;
131   GLuint NewGLState;
132
133   struct {
134      GLuint start_offset;
135      GLint size;
136      GLint space;
137      GLubyte *ptr;
138   } batch;
139
140   struct {
141      void *ptr;
142      GLint size;
143      GLuint offset;
144      GLuint active_buf;
145      GLuint irq_emitted;
146   } alloc;
147
148   struct {
149      GLuint primitive;
150      GLubyte *start_ptr;
151      void (*flush)( GLcontext * );
152   } prim;
153
154   GLboolean locked;
155
156   GLubyte clear_red;
157   GLubyte clear_green;
158   GLubyte clear_blue;
159   GLubyte clear_alpha;
160   GLuint ClearColor;
161   GLuint ClearDepth;
162
163   GLuint coloroffset;
164   GLuint specoffset;
165
166   /* Support for duplicating XYZW as WPOS parameter (crutch for I915).
167    */
168   GLuint wpos_offset;
169   GLuint wpos_size;
170
171   struct tnl_attr_map vertex_attrs[VERT_ATTRIB_MAX];
172   GLuint vertex_attr_count;
173
174   GLfloat depth_scale;
175   GLfloat polygon_offset_scale; /* dependent on depth_scale, bpp */
176   GLuint depth_clear_mask;
177   GLuint stencil_clear_mask;
178
179   GLboolean hw_stencil;
180   GLboolean hw_stipple;
181
182   /* Texture object bookkeeping
183    */
184   GLuint                nr_heaps;
185   driTexHeap          * texture_heaps[1];
186   driTextureObject      swapped;
187   GLuint                lastStamp;
188
189   struct intel_texture_object *CurrentTexObj[MAX_TEXTURE_UNITS];
190
191   /* State for intelvb.c and inteltris.c.
192    */
193   GLuint RenderIndex;
194   GLmatrix ViewportMatrix;
195   GLenum render_primitive;
196   GLenum reduced_primitive;
197   GLuint vertex_size;
198   char *verts;			/* points to tnl->clipspace.vertex_buf */
199
200
201   /* Fallback rasterization functions
202    */
203   intel_point_func draw_point;
204   intel_line_func draw_line;
205   intel_tri_func draw_tri;
206
207   /* These refer to the current draw (front vs. back) buffer:
208    */
209   GLuint drawOffset;		/* agp offset of drawbuffer */
210   int drawX;			/* origin of drawable in draw buffer */
211   int drawY;
212   GLuint numClipRects;		/* cliprects for that buffer */
213   drm_clip_rect_t *pClipRects;
214
215   int dirtyAge;
216   int perf_boxes;
217   int do_irqs;
218
219   GLboolean scissor;
220   drm_clip_rect_t draw_rect;
221   drm_clip_rect_t scissor_rect;
222
223   drm_context_t hHWContext;
224   drmLock *driHwLock;
225   int driFd;
226
227   __DRIdrawablePrivate *driDrawable;
228   __DRIscreenPrivate *driScreen;
229   intelScreenPrivate *intelScreen;
230   drmI830Sarea *sarea;
231
232   /**
233    * Configuration cache
234    */
235   driOptionCache optionCache;
236};
237
238
239#define DEBUG_LOCKING	1
240
241#if DEBUG_LOCKING
242extern char *prevLockFile;
243extern int prevLockLine;
244
245#define DEBUG_LOCK()							\
246   do {									\
247      prevLockFile = (__FILE__);					\
248      prevLockLine = (__LINE__);					\
249   } while (0)
250
251#define DEBUG_RESET()							\
252   do {									\
253      prevLockFile = 0;							\
254      prevLockLine = 0;							\
255   } while (0)
256
257/* Slightly less broken way of detecting recursive locking in a
258 * threaded environment.  The right way to do this would be to make
259 * prevLockFile, prevLockLine thread-local.
260 *
261 * This technique instead checks to see if the same context is
262 * requesting the lock twice -- this will not catch application
263 * breakages where the same context is active in two different threads
264 * at once, but it will catch driver breakages (recursive locking) in
265 * threaded apps.
266 */
267#define DEBUG_CHECK_LOCK()						\
268   do {									\
269      if ( *((volatile int *)intel->driHwLock) == 			\
270	   (DRM_LOCK_HELD | intel->hHWContext) ) {			\
271	 fprintf( stderr,						\
272		  "LOCK SET!\n\tPrevious %s:%d\n\tCurrent: %s:%d\n",	\
273		  prevLockFile, prevLockLine, __FILE__, __LINE__ );	\
274	 abort();							\
275      }									\
276   } while (0)
277
278#else
279
280#define DEBUG_LOCK()
281#define DEBUG_RESET()
282#define DEBUG_CHECK_LOCK()
283
284#endif
285
286
287
288
289/* Lock the hardware and validate our state.
290 */
291#define LOCK_HARDWARE( intel )				\
292do {							\
293    char __ret=0;					\
294    DEBUG_CHECK_LOCK();					\
295    assert(!(intel)->locked);				\
296    DRM_CAS((intel)->driHwLock, (intel)->hHWContext,	\
297        (DRM_LOCK_HELD|(intel)->hHWContext), __ret);	\
298    if (__ret)						\
299        intelGetLock( (intel), 0 );			\
300      DEBUG_LOCK();					\
301    (intel)->locked = 1;				\
302}while (0)
303
304
305  /* Unlock the hardware using the global current context
306   */
307#define UNLOCK_HARDWARE(intel)						\
308do {									\
309   intel->locked = 0;							\
310   if (0) { 								\
311      intel->perf_boxes |= intel->sarea->perf_boxes;  			\
312      intel->sarea->perf_boxes = 0;					\
313   }									\
314   DRM_UNLOCK((intel)->driFd, (intel)->driHwLock, (intel)->hHWContext);	\
315   DEBUG_RESET();							\
316} while (0)
317
318
319#define SUBPIXEL_X 0.125
320#define SUBPIXEL_Y 0.125
321
322#define INTEL_FIREVERTICES(intel)		\
323do {						\
324   if ((intel)->prim.flush)			\
325      (intel)->prim.flush(&(intel)->ctx);		\
326} while (0)
327
328/* ================================================================
329 * Color packing:
330 */
331
332#define INTEL_PACKCOLOR4444(r,g,b,a) \
333  ((((a) & 0xf0) << 8) | (((r) & 0xf0) << 4) | ((g) & 0xf0) | ((b) >> 4))
334
335#define INTEL_PACKCOLOR1555(r,g,b,a) \
336  ((((r) & 0xf8) << 7) | (((g) & 0xf8) << 2) | (((b) & 0xf8) >> 3) | \
337    ((a) ? 0x8000 : 0))
338
339#define INTEL_PACKCOLOR565(r,g,b) \
340  ((((r) & 0xf8) << 8) | (((g) & 0xfc) << 3) | (((b) & 0xf8) >> 3))
341
342#define INTEL_PACKCOLOR8888(r,g,b,a) \
343  ((a<<24) | (r<<16) | (g<<8) | b)
344
345
346#define INTEL_PACKCOLOR(format, r,  g,  b, a)		\
347(format == DV_PF_555 ? INTEL_PACKCOLOR1555(r,g,b,a) :	\
348 (format == DV_PF_565 ? INTEL_PACKCOLOR565(r,g,b) :	\
349  (format == DV_PF_8888 ? INTEL_PACKCOLOR8888(r,g,b,a) :	\
350   0)))
351
352
353
354/* ================================================================
355 * From linux kernel i386 header files, copes with odd sizes better
356 * than COPY_DWORDS would:
357 */
358#if defined(i386) || defined(__i386__)
359static __inline__ void * __memcpy(void * to, const void * from, size_t n)
360{
361   int d0, d1, d2;
362   __asm__ __volatile__(
363      "rep ; movsl\n\t"
364      "testb $2,%b4\n\t"
365      "je 1f\n\t"
366      "movsw\n"
367      "1:\ttestb $1,%b4\n\t"
368      "je 2f\n\t"
369      "movsb\n"
370      "2:"
371      : "=&c" (d0), "=&D" (d1), "=&S" (d2)
372      :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
373      : "memory");
374   return (to);
375}
376#else
377#define __memcpy(a,b,c) memcpy(a,b,c)
378#endif
379
380
381
382/* ================================================================
383 * Debugging:
384 */
385#define DO_DEBUG		1
386#if DO_DEBUG
387extern int INTEL_DEBUG;
388#else
389#define INTEL_DEBUG		0
390#endif
391
392#define DEBUG_TEXTURE	0x1
393#define DEBUG_STATE	0x2
394#define DEBUG_IOCTL	0x4
395#define DEBUG_PRIMS	0x8
396#define DEBUG_VERTS	0x10
397#define DEBUG_FALLBACKS	0x20
398#define DEBUG_VERBOSE	0x40
399#define DEBUG_DRI       0x80
400#define DEBUG_DMA       0x100
401#define DEBUG_SANITY    0x200
402#define DEBUG_SYNC      0x400
403#define DEBUG_SLEEP     0x800
404#define DEBUG_PIXEL     0x1000
405
406
407#define PCI_CHIP_845_G			0x2562
408#define PCI_CHIP_I830_M			0x3577
409#define PCI_CHIP_I855_GM		0x3582
410#define PCI_CHIP_I865_G			0x2572
411#define PCI_CHIP_I915_G			0x2582
412#define PCI_CHIP_I915_GM		0x2592
413#define PCI_CHIP_I945_G			0x2772
414
415
416/* ================================================================
417 * intel_context.c:
418 */
419
420extern void intelInitDriverFunctions( struct dd_function_table *functions );
421
422extern GLboolean intelInitContext( intelContextPtr intel,
423				   const __GLcontextModes *mesaVis,
424				   __DRIcontextPrivate *driContextPriv,
425				   void *sharedContextPrivate,
426				   struct dd_function_table *functions );
427
428extern void intelGetLock(intelContextPtr intel, GLuint flags);
429extern void intelSetBackClipRects(intelContextPtr intel);
430extern void intelSetFrontClipRects(intelContextPtr intel);
431extern void intelWindowMoved( intelContextPtr intel );
432
433extern void intelInitState( GLcontext *ctx );
434extern const GLubyte *intelGetString( GLcontext *ctx, GLenum name );
435
436
437/* ================================================================
438 * intel_state.c:
439 */
440extern void intelInitStateFuncs( struct dd_function_table *functions );
441
442#define COMPAREFUNC_ALWAYS		0
443#define COMPAREFUNC_NEVER		0x1
444#define COMPAREFUNC_LESS		0x2
445#define COMPAREFUNC_EQUAL		0x3
446#define COMPAREFUNC_LEQUAL		0x4
447#define COMPAREFUNC_GREATER		0x5
448#define COMPAREFUNC_NOTEQUAL		0x6
449#define COMPAREFUNC_GEQUAL		0x7
450
451#define STENCILOP_KEEP			0
452#define STENCILOP_ZERO			0x1
453#define STENCILOP_REPLACE		0x2
454#define STENCILOP_INCRSAT		0x3
455#define STENCILOP_DECRSAT		0x4
456#define STENCILOP_INCR			0x5
457#define STENCILOP_DECR			0x6
458#define STENCILOP_INVERT		0x7
459
460#define LOGICOP_CLEAR			0
461#define LOGICOP_NOR			0x1
462#define LOGICOP_AND_INV 		0x2
463#define LOGICOP_COPY_INV		0x3
464#define LOGICOP_AND_RVRSE		0x4
465#define LOGICOP_INV			0x5
466#define LOGICOP_XOR			0x6
467#define LOGICOP_NAND			0x7
468#define LOGICOP_AND			0x8
469#define LOGICOP_EQUIV			0x9
470#define LOGICOP_NOOP			0xa
471#define LOGICOP_OR_INV			0xb
472#define LOGICOP_COPY			0xc
473#define LOGICOP_OR_RVRSE		0xd
474#define LOGICOP_OR			0xe
475#define LOGICOP_SET			0xf
476
477#define BLENDFACT_ZERO			0x01
478#define BLENDFACT_ONE			0x02
479#define BLENDFACT_SRC_COLR		0x03
480#define BLENDFACT_INV_SRC_COLR 		0x04
481#define BLENDFACT_SRC_ALPHA		0x05
482#define BLENDFACT_INV_SRC_ALPHA 	0x06
483#define BLENDFACT_DST_ALPHA		0x07
484#define BLENDFACT_INV_DST_ALPHA 	0x08
485#define BLENDFACT_DST_COLR		0x09
486#define BLENDFACT_INV_DST_COLR		0x0a
487#define BLENDFACT_SRC_ALPHA_SATURATE	0x0b
488#define BLENDFACT_CONST_COLOR		0x0c
489#define BLENDFACT_INV_CONST_COLOR	0x0d
490#define BLENDFACT_CONST_ALPHA		0x0e
491#define BLENDFACT_INV_CONST_ALPHA	0x0f
492#define BLENDFACT_MASK          	0x0f
493
494
495extern int intel_translate_compare_func( GLenum func );
496extern int intel_translate_stencil_op( GLenum op );
497extern int intel_translate_blend_factor( GLenum factor );
498extern int intel_translate_logic_op( GLenum opcode );
499
500
501/* ================================================================
502 * intel_ioctl.c:
503 */
504extern void intel_dump_batchbuffer( long offset,
505				    int *ptr,
506				    int count );
507
508
509/* ================================================================
510 * intel_pixel.c:
511 */
512extern void intelInitPixelFuncs( struct dd_function_table *functions );
513
514
515
516#endif
517
518