5a0678b9d6c099cb43b9a71cb468802750162dee
[mesa.git] / src / mesa / drivers / dri / radeon / radeon_common_context.h
1
2 #ifndef COMMON_CONTEXT_H
3 #define COMMON_CONTEXT_H
4
5 #include "main/mm.h"
6 #include "math/m_vector.h"
7 #include "texmem.h"
8 #include "tnl/t_context.h"
9 #include "main/colormac.h"
10
11 #include "radeon_debug.h"
12 #include "radeon_screen.h"
13 #include "radeon_drm.h"
14 #include "dri_util.h"
15 #include "tnl/t_vertex.h"
16
17 #include "dri_metaops.h"
18 struct radeon_context;
19
20 #include "radeon_bocs_wrapper.h"
21
22 /* This union is used to avoid warnings/miscompilation
23 with float to uint32_t casts due to strict-aliasing */
24 typedef union { GLfloat f; uint32_t ui32; } float_ui32_type;
25
26 struct radeon_context;
27 typedef struct radeon_context radeonContextRec;
28 typedef struct radeon_context *radeonContextPtr;
29
30
31 #define TEX_0 0x1
32 #define TEX_1 0x2
33 #define TEX_2 0x4
34 #define TEX_3 0x8
35 #define TEX_4 0x10
36 #define TEX_5 0x20
37
38 /* Rasterizing fallbacks */
39 /* See correponding strings in r200_swtcl.c */
40 #define RADEON_FALLBACK_TEXTURE 0x0001
41 #define RADEON_FALLBACK_DRAW_BUFFER 0x0002
42 #define RADEON_FALLBACK_STENCIL 0x0004
43 #define RADEON_FALLBACK_RENDER_MODE 0x0008
44 #define RADEON_FALLBACK_BLEND_EQ 0x0010
45 #define RADEON_FALLBACK_BLEND_FUNC 0x0020
46 #define RADEON_FALLBACK_DISABLE 0x0040
47 #define RADEON_FALLBACK_BORDER_MODE 0x0080
48 #define RADEON_FALLBACK_DEPTH_BUFFER 0x0100
49 #define RADEON_FALLBACK_STENCIL_BUFFER 0x0200
50
51 #define R200_FALLBACK_TEXTURE 0x01
52 #define R200_FALLBACK_DRAW_BUFFER 0x02
53 #define R200_FALLBACK_STENCIL 0x04
54 #define R200_FALLBACK_RENDER_MODE 0x08
55 #define R200_FALLBACK_DISABLE 0x10
56 #define R200_FALLBACK_BORDER_MODE 0x20
57
58 #define RADEON_TCL_FALLBACK_RASTER 0x1 /* rasterization */
59 #define RADEON_TCL_FALLBACK_UNFILLED 0x2 /* unfilled tris */
60 #define RADEON_TCL_FALLBACK_LIGHT_TWOSIDE 0x4 /* twoside tris */
61 #define RADEON_TCL_FALLBACK_MATERIAL 0x8 /* material in vb */
62 #define RADEON_TCL_FALLBACK_TEXGEN_0 0x10 /* texgen, unit 0 */
63 #define RADEON_TCL_FALLBACK_TEXGEN_1 0x20 /* texgen, unit 1 */
64 #define RADEON_TCL_FALLBACK_TEXGEN_2 0x40 /* texgen, unit 2 */
65 #define RADEON_TCL_FALLBACK_TCL_DISABLE 0x80 /* user disable */
66 #define RADEON_TCL_FALLBACK_FOGCOORDSPEC 0x100 /* fogcoord, sep. spec light */
67
68 /* The blit width for texture uploads
69 */
70 #define BLIT_WIDTH_BYTES 1024
71
72 /* Use the templated vertex format:
73 */
74 #define COLOR_IS_RGBA
75 #define TAG(x) radeon##x
76 #include "tnl_dd/t_dd_vertex.h"
77 #undef TAG
78
79 #define RADEON_RB_CLASS 0xdeadbeef
80
81 struct radeon_renderbuffer
82 {
83 struct gl_renderbuffer base;
84 struct radeon_bo *bo;
85 unsigned int cpp;
86 /* unsigned int offset; */
87 unsigned int pitch;
88
89 uint32_t draw_offset; /* FBO */
90 /* boo Xorg 6.8.2 compat */
91 int has_surface;
92
93 GLuint pf_pending; /**< sequence number of pending flip */
94 GLuint vbl_pending; /**< vblank sequence number of pending flip */
95 __DRIdrawablePrivate *dPriv;
96 };
97
98 struct radeon_framebuffer
99 {
100 struct gl_framebuffer base;
101
102 struct radeon_renderbuffer *color_rb[2];
103
104 GLuint vbl_waited;
105
106 /* buffer swap */
107 int64_t swap_ust;
108 int64_t swap_missed_ust;
109
110 GLuint swap_count;
111 GLuint swap_missed_count;
112
113 /* Drawable page flipping state */
114 GLboolean pf_active;
115 GLint pf_current_page;
116 GLint pf_num_pages;
117
118 };
119
120
121 struct radeon_colorbuffer_state {
122 GLuint clear;
123 int roundEnable;
124 struct gl_renderbuffer *rb;
125 uint32_t draw_offset; /* offset into color renderbuffer - FBOs */
126 };
127
128 struct radeon_depthbuffer_state {
129 GLuint clear;
130 struct gl_renderbuffer *rb;
131 };
132
133 struct radeon_scissor_state {
134 drm_clip_rect_t rect;
135 GLboolean enabled;
136
137 GLuint numClipRects; /* Cliprects active */
138 GLuint numAllocedClipRects; /* Cliprects available */
139 drm_clip_rect_t *pClipRects;
140 };
141
142 struct radeon_stencilbuffer_state {
143 GLuint clear; /* rb3d_stencilrefmask value */
144 };
145
146 struct radeon_state_atom {
147 struct radeon_state_atom *next, *prev;
148 const char *name; /* for debug */
149 int cmd_size; /* size in bytes */
150 GLuint idx;
151 GLuint is_tcl;
152 GLuint *cmd; /* one or more cmd's */
153 GLuint *lastcmd; /* one or more cmd's */
154 GLboolean dirty; /* dirty-mark in emit_state_list */
155 int (*check) (GLcontext *, struct radeon_state_atom *atom); /* is this state active? */
156 void (*emit) (GLcontext *, struct radeon_state_atom *atom);
157 };
158
159 struct radeon_hw_state {
160 /* Head of the linked list of state atoms. */
161 struct radeon_state_atom atomlist;
162 int max_state_size; /* Number of bytes necessary for a full state emit. */
163 int max_post_flush_size; /* Number of bytes necessary for post flushing emits */
164 GLboolean is_dirty, all_dirty;
165 };
166
167
168 /* Texture related */
169 typedef struct _radeon_texture_image radeon_texture_image;
170
171 struct _radeon_texture_image {
172 struct gl_texture_image base;
173
174 /**
175 * If mt != 0, the image is stored in hardware format in the
176 * given mipmap tree. In this case, base.Data may point into the
177 * mapping of the buffer object that contains the mipmap tree.
178 *
179 * If mt == 0, the image is stored in normal memory pointed to
180 * by base.Data.
181 */
182 struct _radeon_mipmap_tree *mt;
183 struct radeon_bo *bo;
184
185 int mtlevel; /** if mt != 0, this is the image's level in the mipmap tree */
186 int mtface; /** if mt != 0, this is the image's face in the mipmap tree */
187 };
188
189
190 static INLINE radeon_texture_image *get_radeon_texture_image(struct gl_texture_image *image)
191 {
192 return (radeon_texture_image*)image;
193 }
194
195
196 typedef struct radeon_tex_obj radeonTexObj, *radeonTexObjPtr;
197
198 #define RADEON_TXO_MICRO_TILE (1 << 3)
199
200 /* Texture object in locally shared texture space.
201 */
202 struct radeon_tex_obj {
203 struct gl_texture_object base;
204 struct _radeon_mipmap_tree *mt;
205
206 /**
207 * This is true if we've verified that the mipmap tree above is complete
208 * and so on.
209 */
210 GLboolean validated;
211
212 GLuint override_offset;
213 GLboolean image_override; /* Image overridden by GLX_EXT_tfp */
214 GLuint tile_bits; /* hw texture tile bits used on this texture */
215 struct radeon_bo *bo;
216
217 GLuint pp_txfilter; /* hardware register values */
218 GLuint pp_txformat;
219 GLuint pp_txformat_x;
220 GLuint pp_txsize; /* npot only */
221 GLuint pp_txpitch; /* npot only */
222 GLuint pp_border_color;
223 GLuint pp_cubic_faces; /* cube face 1,2,3,4 log2 sizes */
224
225 GLuint pp_txfilter_1; /* r300 */
226
227 /* r700 texture states */
228 GLuint SQ_TEX_RESOURCE0;
229 GLuint SQ_TEX_RESOURCE1;
230 GLuint SQ_TEX_RESOURCE2;
231 GLuint SQ_TEX_RESOURCE3;
232 GLuint SQ_TEX_RESOURCE4;
233 GLuint SQ_TEX_RESOURCE5;
234 GLuint SQ_TEX_RESOURCE6;
235
236 GLuint SQ_TEX_SAMPLER0;
237 GLuint SQ_TEX_SAMPLER1;
238 GLuint SQ_TEX_SAMPLER2;
239
240 GLuint TD_PS_SAMPLER0_BORDER_RED;
241 GLuint TD_PS_SAMPLER0_BORDER_GREEN;
242 GLuint TD_PS_SAMPLER0_BORDER_BLUE;
243 GLuint TD_PS_SAMPLER0_BORDER_ALPHA;
244
245 GLboolean border_fallback;
246
247
248 };
249
250 static INLINE radeonTexObj* radeon_tex_obj(struct gl_texture_object *texObj)
251 {
252 return (radeonTexObj*)texObj;
253 }
254
255 /* occlusion query */
256 struct radeon_query_object {
257 struct gl_query_object Base;
258 struct radeon_bo *bo;
259 int curr_offset;
260 GLboolean emitted_begin;
261
262 /* Double linked list of not flushed query objects */
263 struct radeon_query_object *prev, *next;
264 };
265
266 /* Need refcounting on dma buffers:
267 */
268 struct radeon_dma_buffer {
269 int refcount; /* the number of retained regions in buf */
270 drmBufPtr buf;
271 };
272
273 struct radeon_aos {
274 struct radeon_bo *bo; /** Buffer object where vertex data is stored */
275 int offset; /** Offset into buffer object, in bytes */
276 int components; /** Number of components per vertex */
277 int stride; /** Stride in dwords (may be 0 for repeating) */
278 int count; /** Number of vertices */
279 };
280
281 #define DMA_BO_FREE_TIME 100
282
283 struct radeon_dma_bo {
284 struct radeon_dma_bo *next, *prev;
285 struct radeon_bo *bo;
286 int expire_counter;
287 };
288
289 struct radeon_dma {
290 /* Active dma region. Allocations for vertices and retained
291 * regions come from here. Also used for emitting random vertices,
292 * these may be flushed by calling flush_current();
293 */
294 struct radeon_dma_bo free;
295 struct radeon_dma_bo wait;
296 struct radeon_dma_bo reserved;
297 size_t current_used; /** Number of bytes allocated and forgotten about */
298 size_t current_vertexptr; /** End of active vertex region */
299 size_t minimum_size;
300
301 /**
302 * If current_vertexptr != current_used then flush must be non-zero.
303 * flush must be called before non-active vertex allocations can be
304 * performed.
305 */
306 void (*flush) (GLcontext *);
307 };
308
309 /* radeon_swtcl.c
310 */
311 struct radeon_swtcl_info {
312
313 GLuint RenderIndex;
314 GLuint vertex_size;
315 GLubyte *verts;
316
317 /* Fallback rasterization functions
318 */
319 GLuint hw_primitive;
320 GLenum render_primitive;
321 GLuint numverts;
322
323 struct tnl_attr_map vertex_attrs[VERT_ATTRIB_MAX];
324 GLuint vertex_attr_count;
325
326 GLuint emit_prediction;
327 };
328
329 #define RADEON_MAX_AOS_ARRAYS 16
330 struct radeon_tcl_info {
331 struct radeon_aos aos[RADEON_MAX_AOS_ARRAYS];
332 GLuint aos_count;
333 struct radeon_bo *elt_dma_bo; /** Buffer object that contains element indices */
334 int elt_dma_offset; /** Offset into this buffer object, in bytes */
335 };
336
337 struct radeon_ioctl {
338 GLuint vertex_offset;
339 GLuint vertex_max;
340 struct radeon_bo *bo;
341 GLuint vertex_size;
342 };
343
344 #define RADEON_MAX_PRIMS 64
345
346 struct radeon_prim {
347 GLuint start;
348 GLuint end;
349 GLuint prim;
350 };
351
352 static INLINE GLuint radeonPackColor(GLuint cpp,
353 GLubyte r, GLubyte g,
354 GLubyte b, GLubyte a)
355 {
356 switch (cpp) {
357 case 2:
358 return PACK_COLOR_565(r, g, b);
359 case 4:
360 return PACK_COLOR_8888(a, r, g, b);
361 default:
362 return 0;
363 }
364 }
365
366 #define MAX_CMD_BUF_SZ (16*1024)
367
368 #define MAX_DMA_BUF_SZ (64*1024)
369
370 struct radeon_store {
371 GLuint statenr;
372 GLuint primnr;
373 char cmd_buf[MAX_CMD_BUF_SZ];
374 int cmd_used;
375 int elts_start;
376 };
377
378 struct radeon_dri_mirror {
379 __DRIcontextPrivate *context; /* DRI context */
380 __DRIscreenPrivate *screen; /* DRI screen */
381
382 drm_context_t hwContext;
383 drm_hw_lock_t *hwLock;
384 int hwLockCount;
385 int fd;
386 int drmMinor;
387 };
388
389 typedef void (*radeon_tri_func) (radeonContextPtr,
390 radeonVertex *,
391 radeonVertex *, radeonVertex *);
392
393 typedef void (*radeon_line_func) (radeonContextPtr,
394 radeonVertex *, radeonVertex *);
395
396 typedef void (*radeon_point_func) (radeonContextPtr, radeonVertex *);
397
398 #define RADEON_MAX_BOS 32
399 struct radeon_state {
400 struct radeon_colorbuffer_state color;
401 struct radeon_depthbuffer_state depth;
402 struct radeon_scissor_state scissor;
403 struct radeon_stencilbuffer_state stencil;
404
405 struct radeon_cs_space_check bos[RADEON_MAX_BOS];
406 int validated_bo_count;
407 };
408
409 /**
410 * This structure holds the command buffer while it is being constructed.
411 *
412 * The first batch of commands in the buffer is always the state that needs
413 * to be re-emitted when the context is lost. This batch can be skipped
414 * otherwise.
415 */
416 struct radeon_cmdbuf {
417 struct radeon_cs_manager *csm;
418 struct radeon_cs *cs;
419 int size; /** # of dwords total */
420 unsigned int flushing:1; /** whether we're currently in FlushCmdBufLocked */
421 };
422
423 struct radeon_context {
424 GLcontext *glCtx;
425 radeonScreenPtr radeonScreen; /* Screen private DRI data */
426
427 /* Texture object bookkeeping
428 */
429 int texture_depth;
430 float initialMaxAnisotropy;
431 uint32_t texture_row_align;
432 uint32_t texture_rect_row_align;
433 uint32_t texture_compressed_row_align;
434
435 struct radeon_dma dma;
436 struct radeon_hw_state hw;
437 /* Rasterization and vertex state:
438 */
439 GLuint TclFallback;
440 GLuint Fallback;
441 GLuint NewGLState;
442 DECLARE_RENDERINPUTS(tnl_index_bitset); /* index of bits for last tnl_install_attrs */
443
444 /* Drawable, cliprect and scissor information */
445 GLuint numClipRects; /* Cliprects for the draw buffer */
446 drm_clip_rect_t *pClipRects;
447 unsigned int lastStamp;
448 drm_radeon_sarea_t *sarea; /* Private SAREA data */
449
450 /* Mirrors of some DRI state */
451 struct radeon_dri_mirror dri;
452
453 /* Busy waiting */
454 GLuint do_usleeps;
455 GLuint do_irqs;
456 GLuint irqsEmitted;
457 drm_radeon_irq_wait_t iw;
458
459 /* Derived state - for r300 only */
460 struct radeon_state state;
461
462 struct radeon_swtcl_info swtcl;
463 struct radeon_tcl_info tcl;
464 /* Configuration cache
465 */
466 driOptionCache optionCache;
467
468 struct radeon_cmdbuf cmdbuf;
469
470 struct radeon_debug debug;
471
472 drm_clip_rect_t fboRect;
473 GLboolean constant_cliprect; /* use for FBO or DRI2 rendering */
474 GLboolean front_cliprects;
475
476 /**
477 * Set if rendering has occured to the drawable's front buffer.
478 *
479 * This is used in the DRI2 case to detect that glFlush should also copy
480 * the contents of the fake front buffer to the real front buffer.
481 */
482 GLboolean front_buffer_dirty;
483
484 /**
485 * Track whether front-buffer rendering is currently enabled
486 *
487 * A separate flag is used to track this in order to support MRT more
488 * easily.
489 */
490 GLboolean is_front_buffer_rendering;
491
492 /**
493 * Track whether front-buffer is the current read target.
494 *
495 * This is closely associated with is_front_buffer_rendering, but may
496 * be set separately. The DRI2 fake front buffer must be referenced
497 * either way.
498 */
499 GLboolean is_front_buffer_reading;
500
501 struct dri_metaops meta;
502
503 struct {
504 struct radeon_query_object *current;
505 struct radeon_state_atom queryobj;
506 } query;
507
508 struct {
509 void (*get_lock)(radeonContextPtr radeon);
510 void (*update_viewport_offset)(GLcontext *ctx);
511 void (*emit_cs_header)(struct radeon_cs *cs, radeonContextPtr rmesa);
512 void (*swtcl_flush)(GLcontext *ctx, uint32_t offset);
513 void (*pre_emit_atoms)(radeonContextPtr rmesa);
514 void (*pre_emit_state)(radeonContextPtr rmesa);
515 void (*fallback)(GLcontext *ctx, GLuint bit, GLboolean mode);
516 void (*free_context)(GLcontext *ctx);
517 void (*emit_query_finish)(radeonContextPtr radeon);
518 void (*update_scissor)(GLcontext *ctx);
519 } vtbl;
520 };
521
522 #define RADEON_CONTEXT(glctx) ((radeonContextPtr)(ctx->DriverCtx))
523
524 static inline __DRIdrawablePrivate* radeon_get_drawable(radeonContextPtr radeon)
525 {
526 return radeon->dri.context->driDrawablePriv;
527 }
528
529 static inline __DRIdrawablePrivate* radeon_get_readable(radeonContextPtr radeon)
530 {
531 return radeon->dri.context->driReadablePriv;
532 }
533
534 /**
535 * This function takes a float and packs it into a uint32_t
536 */
537 static INLINE uint32_t radeonPackFloat32(float fl)
538 {
539 union {
540 float fl;
541 uint32_t u;
542 } u;
543
544 u.fl = fl;
545 return u.u;
546 }
547
548 /* This is probably wrong for some values, I need to test this
549 * some more. Range checking would be a good idea also..
550 *
551 * But it works for most things. I'll fix it later if someone
552 * else with a better clue doesn't
553 */
554 static INLINE uint32_t radeonPackFloat24(float f)
555 {
556 float mantissa;
557 int exponent;
558 uint32_t float24 = 0;
559
560 if (f == 0.0)
561 return 0;
562
563 mantissa = frexpf(f, &exponent);
564
565 /* Handle -ve */
566 if (mantissa < 0) {
567 float24 |= (1 << 23);
568 mantissa = mantissa * -1.0;
569 }
570 /* Handle exponent, bias of 63 */
571 exponent += 62;
572 float24 |= (exponent << 16);
573 /* Kill 7 LSB of mantissa */
574 float24 |= (radeonPackFloat32(mantissa) & 0x7FFFFF) >> 7;
575
576 return float24;
577 }
578
579 GLboolean radeonInitContext(radeonContextPtr radeon,
580 struct dd_function_table* functions,
581 const __GLcontextModes * glVisual,
582 __DRIcontextPrivate * driContextPriv,
583 void *sharedContextPrivate);
584
585 void radeonCleanupContext(radeonContextPtr radeon);
586 GLboolean radeonUnbindContext(__DRIcontextPrivate * driContextPriv);
587 void radeon_update_renderbuffers(__DRIcontext *context, __DRIdrawable *drawable);
588 GLboolean radeonMakeCurrent(__DRIcontextPrivate * driContextPriv,
589 __DRIdrawablePrivate * driDrawPriv,
590 __DRIdrawablePrivate * driReadPriv);
591 extern void radeonDestroyContext(__DRIcontextPrivate * driContextPriv);
592
593 #endif