Merge branch 'master' of git+ssh://znh@git.freedesktop.org/git/mesa/mesa into 965...
[mesa.git] / src / mesa / drivers / dri / i915 / intel_context.h
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 #include "vblank.h"
38
39 #include "intel_screen.h"
40 #include "i915_drm.h"
41 #include "i830_common.h"
42 #include "tnl/t_vertex.h"
43
44 #define TAG(x) intel##x
45 #include "tnl_dd/t_dd_vertex.h"
46 #undef TAG
47
48 #define DV_PF_555 (1<<8)
49 #define DV_PF_565 (2<<8)
50 #define DV_PF_8888 (3<<8)
51
52 #define INTEL_CONTEXT(ctx) ((intelContextPtr)(ctx))
53
54 typedef struct intel_context intelContext;
55 typedef struct intel_context *intelContextPtr;
56 typedef struct intel_texture_object *intelTextureObjectPtr;
57
58 typedef void (*intel_tri_func)(intelContextPtr, intelVertex *, intelVertex *,
59 intelVertex *);
60 typedef void (*intel_line_func)(intelContextPtr, intelVertex *, intelVertex *);
61 typedef void (*intel_point_func)(intelContextPtr, intelVertex *);
62
63 #define INTEL_FALLBACK_DRAW_BUFFER 0x1
64 #define INTEL_FALLBACK_READ_BUFFER 0x2
65 #define INTEL_FALLBACK_USER 0x4
66 #define INTEL_FALLBACK_NO_BATCHBUFFER 0x8
67 #define INTEL_FALLBACK_NO_TEXMEM 0x10
68 #define INTEL_FALLBACK_RENDERMODE 0x20
69
70 extern void intelFallback( intelContextPtr intel, GLuint bit, GLboolean mode );
71 #define FALLBACK( intel, bit, mode ) intelFallback( intel, bit, mode )
72
73
74 #define INTEL_TEX_MAXLEVELS 10
75
76
77 struct intel_texture_object
78 {
79 driTextureObject base; /* the parent class */
80
81 GLuint texelBytes;
82 GLuint age;
83 GLuint Pitch;
84 GLuint Height;
85 GLuint TextureOffset;
86 GLubyte *BufAddr;
87
88 GLuint min_level;
89 GLuint max_level;
90 GLuint depth_pitch;
91
92 struct {
93 const struct gl_texture_image *image;
94 GLuint offset; /* into BufAddr */
95 GLuint height;
96 GLuint internalFormat;
97 } image[6][INTEL_TEX_MAXLEVELS];
98
99 GLuint dirty;
100 GLuint firstLevel,lastLevel;
101 };
102
103
104 struct intel_context
105 {
106 GLcontext ctx; /* the parent class */
107
108 struct {
109 void (*destroy)( intelContextPtr intel );
110 void (*emit_state)( intelContextPtr intel );
111 void (*lost_hardware)( intelContextPtr intel );
112 void (*update_texture_state)( intelContextPtr intel );
113
114 void (*render_start)( intelContextPtr intel );
115 void (*set_color_region)( intelContextPtr intel, const intelRegion *reg );
116 void (*set_z_region)( intelContextPtr intel, const intelRegion *reg );
117 void (*update_color_z_regions)(intelContextPtr intel,
118 const intelRegion *colorRegion,
119 const intelRegion *depthRegion);
120 void (*emit_flush)( intelContextPtr intel );
121 void (*reduced_primitive_state)( intelContextPtr intel, GLenum rprim );
122
123 GLboolean (*check_vertex_size)( intelContextPtr intel, GLuint expected );
124
125 void (*clear_with_tris)( intelContextPtr intel, GLbitfield mask,
126 GLboolean all,
127 GLint cx, GLint cy, GLint cw, GLint ch);
128
129 void (*rotate_window)( intelContextPtr intel,
130 __DRIdrawablePrivate *dPriv, GLuint srcBuf);
131
132 intelTextureObjectPtr (*alloc_tex_obj)( struct gl_texture_object *tObj );
133
134 } vtbl;
135
136 GLint refcount;
137 GLuint Fallback;
138 GLuint NewGLState;
139
140 struct {
141 GLuint start_offset;
142 GLint size;
143 GLint space;
144 GLubyte *ptr;
145 GLuint counter;
146 GLuint last_emit_state;
147 GLboolean contains_geometry;
148 const char *func;
149 GLuint last_swap;
150 } batch;
151
152 struct {
153 void *ptr;
154 GLint size;
155 GLuint offset;
156 GLuint active_buf;
157 GLuint irq_emitted;
158 } alloc;
159
160 struct {
161 GLuint primitive;
162 GLubyte *start_ptr;
163 void (*flush)( GLcontext * );
164 } prim;
165
166 GLboolean locked;
167
168 GLubyte clear_red;
169 GLubyte clear_green;
170 GLubyte clear_blue;
171 GLubyte clear_alpha;
172 GLuint ClearColor;
173 GLuint ClearDepth;
174
175 GLuint coloroffset;
176 GLuint specoffset;
177
178 /* Support for duplicating XYZW as WPOS parameter (crutch for I915).
179 */
180 GLuint wpos_offset;
181 GLuint wpos_size;
182
183 struct tnl_attr_map vertex_attrs[VERT_ATTRIB_MAX];
184 GLuint vertex_attr_count;
185
186 GLfloat depth_scale;
187 GLfloat polygon_offset_scale; /* dependent on depth_scale, bpp */
188 GLuint depth_clear_mask;
189 GLuint stencil_clear_mask;
190
191 GLboolean hw_stencil;
192 GLboolean hw_stipple;
193
194 /* Texture object bookkeeping
195 */
196 GLuint nr_heaps;
197 driTexHeap * texture_heaps[1];
198 driTextureObject swapped;
199 GLuint lastStamp;
200
201 struct intel_texture_object *CurrentTexObj[MAX_TEXTURE_UNITS];
202
203 /* State for intelvb.c and inteltris.c.
204 */
205 GLuint RenderIndex;
206 GLmatrix ViewportMatrix;
207 GLenum render_primitive;
208 GLenum reduced_primitive;
209 GLuint vertex_size;
210 unsigned char *verts; /* points to tnl->clipspace.vertex_buf */
211
212
213 /* Fallback rasterization functions
214 */
215 intel_point_func draw_point;
216 intel_line_func draw_line;
217 intel_tri_func draw_tri;
218
219 /* Drawing buffer state
220 */
221 intelRegion *drawRegion; /* current drawing buffer */
222 intelRegion *readRegion; /* current reading buffer */
223
224 int drawX; /* origin of drawable in draw buffer */
225 int drawY;
226 GLuint numClipRects; /* cliprects for that buffer */
227 drm_clip_rect_t *pClipRects;
228
229 int dirtyAge;
230 int perf_boxes;
231
232 GLuint do_usleeps;
233 int do_irqs;
234 GLuint irqsEmitted;
235 drm_i915_irq_wait_t iw;
236
237 GLboolean scissor;
238 drm_clip_rect_t draw_rect;
239 drm_clip_rect_t scissor_rect;
240
241 drm_context_t hHWContext;
242 drmLock *driHwLock;
243 int driFd;
244
245 __DRIdrawablePrivate *driDrawable;
246 __DRIscreenPrivate *driScreen;
247 intelScreenPrivate *intelScreen;
248 drmI830Sarea *sarea;
249
250 /**
251 * Configuration cache
252 */
253 driOptionCache optionCache;
254
255 /* VBI
256 */
257 GLuint vbl_seq;
258 GLuint vblank_flags;
259
260 int64_t swap_ust;
261 int64_t swap_missed_ust;
262
263 GLuint swap_count;
264 GLuint swap_missed_count;
265
266 GLuint swap_scheduled;
267 };
268
269
270 #define DEBUG_LOCKING 1
271
272 #if DEBUG_LOCKING
273 extern char *prevLockFile;
274 extern int prevLockLine;
275
276 #define DEBUG_LOCK() \
277 do { \
278 prevLockFile = (__FILE__); \
279 prevLockLine = (__LINE__); \
280 } while (0)
281
282 #define DEBUG_RESET() \
283 do { \
284 prevLockFile = 0; \
285 prevLockLine = 0; \
286 } while (0)
287
288 /* Slightly less broken way of detecting recursive locking in a
289 * threaded environment. The right way to do this would be to make
290 * prevLockFile, prevLockLine thread-local.
291 *
292 * This technique instead checks to see if the same context is
293 * requesting the lock twice -- this will not catch application
294 * breakages where the same context is active in two different threads
295 * at once, but it will catch driver breakages (recursive locking) in
296 * threaded apps.
297 */
298 #define DEBUG_CHECK_LOCK() \
299 do { \
300 if ( *((volatile int *)intel->driHwLock) == \
301 (DRM_LOCK_HELD | intel->hHWContext) ) { \
302 fprintf( stderr, \
303 "LOCK SET!\n\tPrevious %s:%d\n\tCurrent: %s:%d\n", \
304 prevLockFile, prevLockLine, __FILE__, __LINE__ ); \
305 abort(); \
306 } \
307 } while (0)
308
309 #else
310
311 #define DEBUG_LOCK()
312 #define DEBUG_RESET()
313 #define DEBUG_CHECK_LOCK()
314
315 #endif
316
317
318
319
320 /* Lock the hardware and validate our state.
321 */
322 #define LOCK_HARDWARE( intel ) \
323 do { \
324 char __ret=0; \
325 DEBUG_CHECK_LOCK(); \
326 assert(!(intel)->locked); \
327 if ((intel)->swap_scheduled) { \
328 drmVBlank vbl; \
329 vbl.request.type = DRM_VBLANK_ABSOLUTE; \
330 if ((intel)->vblank_flags & \
331 VBLANK_FLAG_SECONDARY) { \
332 vbl.request.type |= DRM_VBLANK_SECONDARY; \
333 } \
334 vbl.request.sequence = (intel)->vbl_seq; \
335 drmWaitVBlank((intel)->driFd, &vbl); \
336 (intel)->swap_scheduled = 0; \
337 } \
338 DRM_CAS((intel)->driHwLock, (intel)->hHWContext, \
339 (DRM_LOCK_HELD|(intel)->hHWContext), __ret); \
340 if (__ret) \
341 intelGetLock( (intel), 0 ); \
342 DEBUG_LOCK(); \
343 (intel)->locked = 1; \
344 }while (0)
345
346
347 /* Unlock the hardware using the global current context
348 */
349 #define UNLOCK_HARDWARE(intel) \
350 do { \
351 intel->locked = 0; \
352 if (0) { \
353 intel->perf_boxes |= intel->sarea->perf_boxes; \
354 intel->sarea->perf_boxes = 0; \
355 } \
356 DRM_UNLOCK((intel)->driFd, (intel)->driHwLock, (intel)->hHWContext); \
357 DEBUG_RESET(); \
358 } while (0)
359
360
361 #define SUBPIXEL_X 0.125
362 #define SUBPIXEL_Y 0.125
363
364 #define INTEL_FIREVERTICES(intel) \
365 do { \
366 if ((intel)->prim.flush) \
367 (intel)->prim.flush(&(intel)->ctx); \
368 } while (0)
369
370 /* ================================================================
371 * Color packing:
372 */
373
374 #define INTEL_PACKCOLOR4444(r,g,b,a) \
375 ((((a) & 0xf0) << 8) | (((r) & 0xf0) << 4) | ((g) & 0xf0) | ((b) >> 4))
376
377 #define INTEL_PACKCOLOR1555(r,g,b,a) \
378 ((((r) & 0xf8) << 7) | (((g) & 0xf8) << 2) | (((b) & 0xf8) >> 3) | \
379 ((a) ? 0x8000 : 0))
380
381 #define INTEL_PACKCOLOR565(r,g,b) \
382 ((((r) & 0xf8) << 8) | (((g) & 0xfc) << 3) | (((b) & 0xf8) >> 3))
383
384 #define INTEL_PACKCOLOR8888(r,g,b,a) \
385 ((a<<24) | (r<<16) | (g<<8) | b)
386
387
388 #define INTEL_PACKCOLOR(format, r, g, b, a) \
389 (format == DV_PF_555 ? INTEL_PACKCOLOR1555(r,g,b,a) : \
390 (format == DV_PF_565 ? INTEL_PACKCOLOR565(r,g,b) : \
391 (format == DV_PF_8888 ? INTEL_PACKCOLOR8888(r,g,b,a) : \
392 0)))
393
394
395
396 /* ================================================================
397 * From linux kernel i386 header files, copes with odd sizes better
398 * than COPY_DWORDS would:
399 */
400 #if defined(i386) || defined(__i386__)
401 static __inline__ void * __memcpy(void * to, const void * from, size_t n)
402 {
403 int d0, d1, d2;
404 __asm__ __volatile__(
405 "rep ; movsl\n\t"
406 "testb $2,%b4\n\t"
407 "je 1f\n\t"
408 "movsw\n"
409 "1:\ttestb $1,%b4\n\t"
410 "je 2f\n\t"
411 "movsb\n"
412 "2:"
413 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
414 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
415 : "memory");
416 return (to);
417 }
418 #else
419 #define __memcpy(a,b,c) memcpy(a,b,c)
420 #endif
421
422
423
424 /* ================================================================
425 * Debugging:
426 */
427 #define DO_DEBUG 1
428 #if DO_DEBUG
429 extern int INTEL_DEBUG;
430 #else
431 #define INTEL_DEBUG 0
432 #endif
433
434 #define DEBUG_TEXTURE 0x1
435 #define DEBUG_STATE 0x2
436 #define DEBUG_IOCTL 0x4
437 #define DEBUG_PRIMS 0x8
438 #define DEBUG_VERTS 0x10
439 #define DEBUG_FALLBACKS 0x20
440 #define DEBUG_VERBOSE 0x40
441 #define DEBUG_DRI 0x80
442 #define DEBUG_DMA 0x100
443 #define DEBUG_SANITY 0x200
444 #define DEBUG_SYNC 0x400
445 #define DEBUG_SLEEP 0x800
446 #define DEBUG_PIXEL 0x1000
447
448
449 #define PCI_CHIP_845_G 0x2562
450 #define PCI_CHIP_I830_M 0x3577
451 #define PCI_CHIP_I855_GM 0x3582
452 #define PCI_CHIP_I865_G 0x2572
453 #define PCI_CHIP_I915_G 0x2582
454 #define PCI_CHIP_I915_GM 0x2592
455 #define PCI_CHIP_I945_G 0x2772
456 #define PCI_CHIP_I945_GM 0x27A2
457 #define PCI_CHIP_I945_GME 0x27AE
458 #define PCI_CHIP_G33_G 0x29C2
459 #define PCI_CHIP_Q35_G 0x29B2
460 #define PCI_CHIP_Q33_G 0x29D2
461
462
463 /* ================================================================
464 * intel_context.c:
465 */
466
467 extern void intelInitDriverFunctions( struct dd_function_table *functions );
468
469 extern GLboolean intelInitContext( intelContextPtr intel,
470 const __GLcontextModes *mesaVis,
471 __DRIcontextPrivate *driContextPriv,
472 void *sharedContextPrivate,
473 struct dd_function_table *functions );
474
475 extern void intelGetLock(intelContextPtr intel, GLuint flags);
476 extern void intelSetBackClipRects(intelContextPtr intel);
477 extern void intelSetFrontClipRects(intelContextPtr intel);
478 extern void intelWindowMoved( intelContextPtr intel );
479
480 extern const GLubyte *intelGetString( GLcontext *ctx, GLenum name );
481
482
483 /* ================================================================
484 * intel_state.c:
485 */
486 extern void intelInitStateFuncs( struct dd_function_table *functions );
487
488 #define COMPAREFUNC_ALWAYS 0
489 #define COMPAREFUNC_NEVER 0x1
490 #define COMPAREFUNC_LESS 0x2
491 #define COMPAREFUNC_EQUAL 0x3
492 #define COMPAREFUNC_LEQUAL 0x4
493 #define COMPAREFUNC_GREATER 0x5
494 #define COMPAREFUNC_NOTEQUAL 0x6
495 #define COMPAREFUNC_GEQUAL 0x7
496
497 #define STENCILOP_KEEP 0
498 #define STENCILOP_ZERO 0x1
499 #define STENCILOP_REPLACE 0x2
500 #define STENCILOP_INCRSAT 0x3
501 #define STENCILOP_DECRSAT 0x4
502 #define STENCILOP_INCR 0x5
503 #define STENCILOP_DECR 0x6
504 #define STENCILOP_INVERT 0x7
505
506 #define LOGICOP_CLEAR 0
507 #define LOGICOP_NOR 0x1
508 #define LOGICOP_AND_INV 0x2
509 #define LOGICOP_COPY_INV 0x3
510 #define LOGICOP_AND_RVRSE 0x4
511 #define LOGICOP_INV 0x5
512 #define LOGICOP_XOR 0x6
513 #define LOGICOP_NAND 0x7
514 #define LOGICOP_AND 0x8
515 #define LOGICOP_EQUIV 0x9
516 #define LOGICOP_NOOP 0xa
517 #define LOGICOP_OR_INV 0xb
518 #define LOGICOP_COPY 0xc
519 #define LOGICOP_OR_RVRSE 0xd
520 #define LOGICOP_OR 0xe
521 #define LOGICOP_SET 0xf
522
523 #define BLENDFACT_ZERO 0x01
524 #define BLENDFACT_ONE 0x02
525 #define BLENDFACT_SRC_COLR 0x03
526 #define BLENDFACT_INV_SRC_COLR 0x04
527 #define BLENDFACT_SRC_ALPHA 0x05
528 #define BLENDFACT_INV_SRC_ALPHA 0x06
529 #define BLENDFACT_DST_ALPHA 0x07
530 #define BLENDFACT_INV_DST_ALPHA 0x08
531 #define BLENDFACT_DST_COLR 0x09
532 #define BLENDFACT_INV_DST_COLR 0x0a
533 #define BLENDFACT_SRC_ALPHA_SATURATE 0x0b
534 #define BLENDFACT_CONST_COLOR 0x0c
535 #define BLENDFACT_INV_CONST_COLOR 0x0d
536 #define BLENDFACT_CONST_ALPHA 0x0e
537 #define BLENDFACT_INV_CONST_ALPHA 0x0f
538 #define BLENDFACT_MASK 0x0f
539
540
541 extern int intel_translate_compare_func( GLenum func );
542 extern int intel_translate_stencil_op( GLenum op );
543 extern int intel_translate_blend_factor( GLenum factor );
544 extern int intel_translate_logic_op( GLenum opcode );
545
546
547 /* ================================================================
548 * intel_ioctl.c:
549 */
550 extern void intel_dump_batchbuffer( long offset,
551 int *ptr,
552 int count );
553
554
555 /* ================================================================
556 * intel_pixel.c:
557 */
558 extern void intelInitPixelFuncs( struct dd_function_table *functions );
559
560
561
562 #endif
563