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