enable feedback rendering
[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
38 #include "intel_screen.h"
39 #include "i915_drm.h"
40 #include "i830_common.h"
41 #include "tnl/t_vertex.h"
42
43 #define TAG(x) intel##x
44 #include "tnl_dd/t_dd_vertex.h"
45 #undef TAG
46
47 #define DV_PF_555 (1<<8)
48 #define DV_PF_565 (2<<8)
49 #define DV_PF_8888 (3<<8)
50
51 #define INTEL_CONTEXT(ctx) ((intelContextPtr)(ctx))
52
53 typedef struct intel_context intelContext;
54 typedef struct intel_context *intelContextPtr;
55 typedef struct intel_texture_object *intelTextureObjectPtr;
56
57 typedef void (*intel_tri_func)(intelContextPtr, intelVertex *, intelVertex *,
58 intelVertex *);
59 typedef void (*intel_line_func)(intelContextPtr, intelVertex *, intelVertex *);
60 typedef void (*intel_point_func)(intelContextPtr, intelVertex *);
61
62 #define INTEL_FALLBACK_DRAW_BUFFER 0x1
63 #define INTEL_FALLBACK_READ_BUFFER 0x2
64 #define INTEL_FALLBACK_USER 0x4
65 #define INTEL_FALLBACK_NO_BATCHBUFFER 0x8
66 #define INTEL_FALLBACK_NO_TEXMEM 0x10
67 #define INTEL_FALLBACK_RENDERMODE 0x20
68
69 extern void intelFallback( intelContextPtr intel, GLuint bit, GLboolean mode );
70 #define FALLBACK( intel, bit, mode ) intelFallback( intel, bit, mode )
71
72
73 #define INTEL_TEX_MAXLEVELS 10
74
75
76 struct intel_texture_object
77 {
78 driTextureObject base; /* the parent class */
79
80 GLuint texelBytes;
81 GLuint age;
82 GLuint Pitch;
83 GLuint Height;
84 GLuint TextureOffset;
85 GLubyte *BufAddr;
86
87 GLuint min_level;
88 GLuint max_level;
89 GLuint depth_pitch;
90
91 struct {
92 const struct gl_texture_image *image;
93 GLuint offset; /* into BufAddr */
94 GLuint height;
95 GLuint internalFormat;
96 } image[6][INTEL_TEX_MAXLEVELS];
97
98 GLuint dirty;
99 GLuint firstLevel,lastLevel;
100 };
101
102
103 struct intel_context
104 {
105 GLcontext ctx; /* the parent class */
106
107 struct {
108 void (*destroy)( intelContextPtr intel );
109 void (*emit_state)( intelContextPtr intel );
110 void (*emit_invarient_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
267
268 #define DEBUG_LOCKING 1
269
270 #if DEBUG_LOCKING
271 extern char *prevLockFile;
272 extern int prevLockLine;
273
274 #define DEBUG_LOCK() \
275 do { \
276 prevLockFile = (__FILE__); \
277 prevLockLine = (__LINE__); \
278 } while (0)
279
280 #define DEBUG_RESET() \
281 do { \
282 prevLockFile = 0; \
283 prevLockLine = 0; \
284 } while (0)
285
286 /* Slightly less broken way of detecting recursive locking in a
287 * threaded environment. The right way to do this would be to make
288 * prevLockFile, prevLockLine thread-local.
289 *
290 * This technique instead checks to see if the same context is
291 * requesting the lock twice -- this will not catch application
292 * breakages where the same context is active in two different threads
293 * at once, but it will catch driver breakages (recursive locking) in
294 * threaded apps.
295 */
296 #define DEBUG_CHECK_LOCK() \
297 do { \
298 if ( *((volatile int *)intel->driHwLock) == \
299 (DRM_LOCK_HELD | intel->hHWContext) ) { \
300 fprintf( stderr, \
301 "LOCK SET!\n\tPrevious %s:%d\n\tCurrent: %s:%d\n", \
302 prevLockFile, prevLockLine, __FILE__, __LINE__ ); \
303 abort(); \
304 } \
305 } while (0)
306
307 #else
308
309 #define DEBUG_LOCK()
310 #define DEBUG_RESET()
311 #define DEBUG_CHECK_LOCK()
312
313 #endif
314
315
316
317
318 /* Lock the hardware and validate our state.
319 */
320 #define LOCK_HARDWARE( intel ) \
321 do { \
322 char __ret=0; \
323 DEBUG_CHECK_LOCK(); \
324 assert(!(intel)->locked); \
325 DRM_CAS((intel)->driHwLock, (intel)->hHWContext, \
326 (DRM_LOCK_HELD|(intel)->hHWContext), __ret); \
327 if (__ret) \
328 intelGetLock( (intel), 0 ); \
329 DEBUG_LOCK(); \
330 (intel)->locked = 1; \
331 }while (0)
332
333
334 /* Unlock the hardware using the global current context
335 */
336 #define UNLOCK_HARDWARE(intel) \
337 do { \
338 intel->locked = 0; \
339 if (0) { \
340 intel->perf_boxes |= intel->sarea->perf_boxes; \
341 intel->sarea->perf_boxes = 0; \
342 } \
343 DRM_UNLOCK((intel)->driFd, (intel)->driHwLock, (intel)->hHWContext); \
344 DEBUG_RESET(); \
345 } while (0)
346
347
348 #define SUBPIXEL_X 0.125
349 #define SUBPIXEL_Y 0.125
350
351 #define INTEL_FIREVERTICES(intel) \
352 do { \
353 if ((intel)->prim.flush) \
354 (intel)->prim.flush(&(intel)->ctx); \
355 } while (0)
356
357 /* ================================================================
358 * Color packing:
359 */
360
361 #define INTEL_PACKCOLOR4444(r,g,b,a) \
362 ((((a) & 0xf0) << 8) | (((r) & 0xf0) << 4) | ((g) & 0xf0) | ((b) >> 4))
363
364 #define INTEL_PACKCOLOR1555(r,g,b,a) \
365 ((((r) & 0xf8) << 7) | (((g) & 0xf8) << 2) | (((b) & 0xf8) >> 3) | \
366 ((a) ? 0x8000 : 0))
367
368 #define INTEL_PACKCOLOR565(r,g,b) \
369 ((((r) & 0xf8) << 8) | (((g) & 0xfc) << 3) | (((b) & 0xf8) >> 3))
370
371 #define INTEL_PACKCOLOR8888(r,g,b,a) \
372 ((a<<24) | (r<<16) | (g<<8) | b)
373
374
375 #define INTEL_PACKCOLOR(format, r, g, b, a) \
376 (format == DV_PF_555 ? INTEL_PACKCOLOR1555(r,g,b,a) : \
377 (format == DV_PF_565 ? INTEL_PACKCOLOR565(r,g,b) : \
378 (format == DV_PF_8888 ? INTEL_PACKCOLOR8888(r,g,b,a) : \
379 0)))
380
381
382
383 /* ================================================================
384 * From linux kernel i386 header files, copes with odd sizes better
385 * than COPY_DWORDS would:
386 */
387 #if defined(i386) || defined(__i386__)
388 static __inline__ void * __memcpy(void * to, const void * from, size_t n)
389 {
390 int d0, d1, d2;
391 __asm__ __volatile__(
392 "rep ; movsl\n\t"
393 "testb $2,%b4\n\t"
394 "je 1f\n\t"
395 "movsw\n"
396 "1:\ttestb $1,%b4\n\t"
397 "je 2f\n\t"
398 "movsb\n"
399 "2:"
400 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
401 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
402 : "memory");
403 return (to);
404 }
405 #else
406 #define __memcpy(a,b,c) memcpy(a,b,c)
407 #endif
408
409
410
411 /* ================================================================
412 * Debugging:
413 */
414 #define DO_DEBUG 1
415 #if DO_DEBUG
416 extern int INTEL_DEBUG;
417 #else
418 #define INTEL_DEBUG 0
419 #endif
420
421 #define DEBUG_TEXTURE 0x1
422 #define DEBUG_STATE 0x2
423 #define DEBUG_IOCTL 0x4
424 #define DEBUG_PRIMS 0x8
425 #define DEBUG_VERTS 0x10
426 #define DEBUG_FALLBACKS 0x20
427 #define DEBUG_VERBOSE 0x40
428 #define DEBUG_DRI 0x80
429 #define DEBUG_DMA 0x100
430 #define DEBUG_SANITY 0x200
431 #define DEBUG_SYNC 0x400
432 #define DEBUG_SLEEP 0x800
433 #define DEBUG_PIXEL 0x1000
434
435
436 #define PCI_CHIP_845_G 0x2562
437 #define PCI_CHIP_I830_M 0x3577
438 #define PCI_CHIP_I855_GM 0x3582
439 #define PCI_CHIP_I865_G 0x2572
440 #define PCI_CHIP_I915_G 0x2582
441 #define PCI_CHIP_I915_GM 0x2592
442 #define PCI_CHIP_I945_G 0x2772
443 #define PCI_CHIP_I945_GM 0x27A2
444
445
446 /* ================================================================
447 * intel_context.c:
448 */
449
450 extern void intelInitDriverFunctions( struct dd_function_table *functions );
451
452 extern GLboolean intelInitContext( intelContextPtr intel,
453 const __GLcontextModes *mesaVis,
454 __DRIcontextPrivate *driContextPriv,
455 void *sharedContextPrivate,
456 struct dd_function_table *functions );
457
458 extern void intelGetLock(intelContextPtr intel, GLuint flags);
459 extern void intelSetBackClipRects(intelContextPtr intel);
460 extern void intelSetFrontClipRects(intelContextPtr intel);
461 extern void intelWindowMoved( intelContextPtr intel );
462
463 extern void intelInitState( GLcontext *ctx );
464 extern const GLubyte *intelGetString( GLcontext *ctx, GLenum name );
465
466
467 /* ================================================================
468 * intel_state.c:
469 */
470 extern void intelInitStateFuncs( struct dd_function_table *functions );
471
472 #define COMPAREFUNC_ALWAYS 0
473 #define COMPAREFUNC_NEVER 0x1
474 #define COMPAREFUNC_LESS 0x2
475 #define COMPAREFUNC_EQUAL 0x3
476 #define COMPAREFUNC_LEQUAL 0x4
477 #define COMPAREFUNC_GREATER 0x5
478 #define COMPAREFUNC_NOTEQUAL 0x6
479 #define COMPAREFUNC_GEQUAL 0x7
480
481 #define STENCILOP_KEEP 0
482 #define STENCILOP_ZERO 0x1
483 #define STENCILOP_REPLACE 0x2
484 #define STENCILOP_INCRSAT 0x3
485 #define STENCILOP_DECRSAT 0x4
486 #define STENCILOP_INCR 0x5
487 #define STENCILOP_DECR 0x6
488 #define STENCILOP_INVERT 0x7
489
490 #define LOGICOP_CLEAR 0
491 #define LOGICOP_NOR 0x1
492 #define LOGICOP_AND_INV 0x2
493 #define LOGICOP_COPY_INV 0x3
494 #define LOGICOP_AND_RVRSE 0x4
495 #define LOGICOP_INV 0x5
496 #define LOGICOP_XOR 0x6
497 #define LOGICOP_NAND 0x7
498 #define LOGICOP_AND 0x8
499 #define LOGICOP_EQUIV 0x9
500 #define LOGICOP_NOOP 0xa
501 #define LOGICOP_OR_INV 0xb
502 #define LOGICOP_COPY 0xc
503 #define LOGICOP_OR_RVRSE 0xd
504 #define LOGICOP_OR 0xe
505 #define LOGICOP_SET 0xf
506
507 #define BLENDFACT_ZERO 0x01
508 #define BLENDFACT_ONE 0x02
509 #define BLENDFACT_SRC_COLR 0x03
510 #define BLENDFACT_INV_SRC_COLR 0x04
511 #define BLENDFACT_SRC_ALPHA 0x05
512 #define BLENDFACT_INV_SRC_ALPHA 0x06
513 #define BLENDFACT_DST_ALPHA 0x07
514 #define BLENDFACT_INV_DST_ALPHA 0x08
515 #define BLENDFACT_DST_COLR 0x09
516 #define BLENDFACT_INV_DST_COLR 0x0a
517 #define BLENDFACT_SRC_ALPHA_SATURATE 0x0b
518 #define BLENDFACT_CONST_COLOR 0x0c
519 #define BLENDFACT_INV_CONST_COLOR 0x0d
520 #define BLENDFACT_CONST_ALPHA 0x0e
521 #define BLENDFACT_INV_CONST_ALPHA 0x0f
522 #define BLENDFACT_MASK 0x0f
523
524
525 extern int intel_translate_compare_func( GLenum func );
526 extern int intel_translate_stencil_op( GLenum op );
527 extern int intel_translate_blend_factor( GLenum factor );
528 extern int intel_translate_logic_op( GLenum opcode );
529
530
531 /* ================================================================
532 * intel_ioctl.c:
533 */
534 extern void intel_dump_batchbuffer( long offset,
535 int *ptr,
536 int count );
537
538
539 /* ================================================================
540 * intel_pixel.c:
541 */
542 extern void intelInitPixelFuncs( struct dd_function_table *functions );
543
544
545
546 #endif
547