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