2 #ifndef COMMON_CONTEXT_H
3 #define COMMON_CONTEXT_H
6 #include "math/m_vector.h"
8 #include "tnl/t_context.h"
9 #include "main/colormac.h"
11 #include "radeon_screen.h"
12 #include "radeon_drm.h"
14 #include "tnl/t_vertex.h"
16 #include "dri_metaops.h"
17 struct radeon_context
;
19 #include "radeon_bocs_wrapper.h"
21 /* From http://gcc. gnu.org/onlinedocs/gcc-3.2.3/gcc/Variadic-Macros.html .
22 I suppose we could inline this and use macro to fetch out __LINE__ and stuff in case we run into trouble
23 with other compilers ... GLUE!
25 #define WARN_ONCE(a, ...) { \
26 static int warn##__LINE__=1; \
28 fprintf(stderr, "*********************************WARN_ONCE*********************************\n"); \
29 fprintf(stderr, "File %s function %s line %d\n", \
30 __FILE__, __FUNCTION__, __LINE__); \
31 fprintf(stderr, a, ## __VA_ARGS__);\
32 fprintf(stderr, "***************************************************************************\n"); \
37 /* This union is used to avoid warnings/miscompilation
38 with float to uint32_t casts due to strict-aliasing */
39 typedef union { GLfloat f
; uint32_t ui32
; } float_ui32_type
;
41 struct radeon_context
;
42 typedef struct radeon_context radeonContextRec
;
43 typedef struct radeon_context
*radeonContextPtr
;
53 /* Rasterizing fallbacks */
54 /* See correponding strings in r200_swtcl.c */
55 #define RADEON_FALLBACK_TEXTURE 0x0001
56 #define RADEON_FALLBACK_DRAW_BUFFER 0x0002
57 #define RADEON_FALLBACK_STENCIL 0x0004
58 #define RADEON_FALLBACK_RENDER_MODE 0x0008
59 #define RADEON_FALLBACK_BLEND_EQ 0x0010
60 #define RADEON_FALLBACK_BLEND_FUNC 0x0020
61 #define RADEON_FALLBACK_DISABLE 0x0040
62 #define RADEON_FALLBACK_BORDER_MODE 0x0080
63 #define RADEON_FALLBACK_DEPTH_BUFFER 0x0100
64 #define RADEON_FALLBACK_STENCIL_BUFFER 0x0200
66 #define R200_FALLBACK_TEXTURE 0x01
67 #define R200_FALLBACK_DRAW_BUFFER 0x02
68 #define R200_FALLBACK_STENCIL 0x04
69 #define R200_FALLBACK_RENDER_MODE 0x08
70 #define R200_FALLBACK_DISABLE 0x10
71 #define R200_FALLBACK_BORDER_MODE 0x20
73 #define RADEON_TCL_FALLBACK_RASTER 0x1 /* rasterization */
74 #define RADEON_TCL_FALLBACK_UNFILLED 0x2 /* unfilled tris */
75 #define RADEON_TCL_FALLBACK_LIGHT_TWOSIDE 0x4 /* twoside tris */
76 #define RADEON_TCL_FALLBACK_MATERIAL 0x8 /* material in vb */
77 #define RADEON_TCL_FALLBACK_TEXGEN_0 0x10 /* texgen, unit 0 */
78 #define RADEON_TCL_FALLBACK_TEXGEN_1 0x20 /* texgen, unit 1 */
79 #define RADEON_TCL_FALLBACK_TEXGEN_2 0x40 /* texgen, unit 2 */
80 #define RADEON_TCL_FALLBACK_TCL_DISABLE 0x80 /* user disable */
81 #define RADEON_TCL_FALLBACK_FOGCOORDSPEC 0x100 /* fogcoord, sep. spec light */
83 /* The blit width for texture uploads
85 #define BLIT_WIDTH_BYTES 1024
87 /* Use the templated vertex format:
90 #define TAG(x) radeon##x
91 #include "tnl_dd/t_dd_vertex.h"
94 #define RADEON_RB_CLASS 0xdeadbeef
96 struct radeon_renderbuffer
98 struct gl_renderbuffer base
;
101 /* unsigned int offset; */
104 uint32_t draw_offset
; /* FBO */
105 /* boo Xorg 6.8.2 compat */
108 GLuint pf_pending
; /**< sequence number of pending flip */
109 GLuint vbl_pending
; /**< vblank sequence number of pending flip */
110 __DRIdrawablePrivate
*dPriv
;
113 struct radeon_framebuffer
115 struct gl_framebuffer base
;
117 struct radeon_renderbuffer
*color_rb
[2];
123 int64_t swap_missed_ust
;
126 GLuint swap_missed_count
;
128 /* Drawable page flipping state */
130 GLint pf_current_page
;
136 struct radeon_colorbuffer_state
{
139 struct gl_renderbuffer
*rb
;
140 uint32_t draw_offset
; /* offset into color renderbuffer - FBOs */
143 struct radeon_depthbuffer_state
{
145 struct gl_renderbuffer
*rb
;
148 struct radeon_scissor_state
{
149 drm_clip_rect_t rect
;
152 GLuint numClipRects
; /* Cliprects active */
153 GLuint numAllocedClipRects
; /* Cliprects available */
154 drm_clip_rect_t
*pClipRects
;
157 struct radeon_stencilbuffer_state
{
158 GLuint clear
; /* rb3d_stencilrefmask value */
161 struct radeon_stipple_state
{
165 struct radeon_state_atom
{
166 struct radeon_state_atom
*next
, *prev
;
167 const char *name
; /* for debug */
168 int cmd_size
; /* size in bytes */
171 GLuint
*cmd
; /* one or more cmd's */
172 GLuint
*lastcmd
; /* one or more cmd's */
173 GLboolean dirty
; /* dirty-mark in emit_state_list */
174 int (*check
) (GLcontext
*, struct radeon_state_atom
*atom
); /* is this state active? */
175 void (*emit
) (GLcontext
*, struct radeon_state_atom
*atom
);
178 struct radeon_hw_state
{
179 /* Head of the linked list of state atoms. */
180 struct radeon_state_atom atomlist
;
181 int max_state_size
; /* Number of bytes necessary for a full state emit. */
182 int max_post_flush_size
; /* Number of bytes necessary for post flushing emits */
183 GLboolean is_dirty
, all_dirty
;
187 /* Texture related */
188 typedef struct _radeon_texture_image radeon_texture_image
;
190 struct _radeon_texture_image
{
191 struct gl_texture_image base
;
194 * If mt != 0, the image is stored in hardware format in the
195 * given mipmap tree. In this case, base.Data may point into the
196 * mapping of the buffer object that contains the mipmap tree.
198 * If mt == 0, the image is stored in normal memory pointed to
201 struct _radeon_mipmap_tree
*mt
;
202 struct radeon_bo
*bo
;
204 int mtlevel
; /** if mt != 0, this is the image's level in the mipmap tree */
205 int mtface
; /** if mt != 0, this is the image's face in the mipmap tree */
209 static INLINE radeon_texture_image
*get_radeon_texture_image(struct gl_texture_image
*image
)
211 return (radeon_texture_image
*)image
;
215 typedef struct radeon_tex_obj radeonTexObj
, *radeonTexObjPtr
;
217 #define RADEON_TXO_MICRO_TILE (1 << 3)
219 /* Texture object in locally shared texture space.
221 struct radeon_tex_obj
{
222 struct gl_texture_object base
;
223 struct _radeon_mipmap_tree
*mt
;
226 * This is true if we've verified that the mipmap tree above is complete
231 GLuint override_offset
;
232 GLboolean image_override
; /* Image overridden by GLX_EXT_tfp */
233 GLuint tile_bits
; /* hw texture tile bits used on this texture */
234 struct radeon_bo
*bo
;
236 GLuint pp_txfilter
; /* hardware register values */
238 GLuint pp_txformat_x
;
239 GLuint pp_txsize
; /* npot only */
240 GLuint pp_txpitch
; /* npot only */
241 GLuint pp_border_color
;
242 GLuint pp_cubic_faces
; /* cube face 1,2,3,4 log2 sizes */
244 GLuint pp_txfilter_1
; /* r300 */
246 /* r700 texture states */
247 GLuint SQ_TEX_RESOURCE0
;
248 GLuint SQ_TEX_RESOURCE1
;
249 GLuint SQ_TEX_RESOURCE2
;
250 GLuint SQ_TEX_RESOURCE3
;
251 GLuint SQ_TEX_RESOURCE4
;
252 GLuint SQ_TEX_RESOURCE5
;
253 GLuint SQ_TEX_RESOURCE6
;
255 GLuint SQ_TEX_SAMPLER0
;
256 GLuint SQ_TEX_SAMPLER1
;
257 GLuint SQ_TEX_SAMPLER2
;
259 GLuint TD_PS_SAMPLER0_BORDER_RED
;
260 GLuint TD_PS_SAMPLER0_BORDER_GREEN
;
261 GLuint TD_PS_SAMPLER0_BORDER_BLUE
;
262 GLuint TD_PS_SAMPLER0_BORDER_ALPHA
;
264 GLboolean border_fallback
;
269 static INLINE radeonTexObj
* radeon_tex_obj(struct gl_texture_object
*texObj
)
271 return (radeonTexObj
*)texObj
;
274 /* occlusion query */
275 struct radeon_query_object
{
276 struct gl_query_object Base
;
277 struct radeon_bo
*bo
;
279 GLboolean emitted_begin
;
281 /* Double linked list of not flushed query objects */
282 struct radeon_query_object
*prev
, *next
;
285 /* Need refcounting on dma buffers:
287 struct radeon_dma_buffer
{
288 int refcount
; /* the number of retained regions in buf */
293 struct radeon_bo
*bo
; /** Buffer object where vertex data is stored */
294 int offset
; /** Offset into buffer object, in bytes */
295 int components
; /** Number of components per vertex */
296 int stride
; /** Stride in dwords (may be 0 for repeating) */
297 int count
; /** Number of vertices */
300 #define DMA_BO_FREE_TIME 100
302 struct radeon_dma_bo
{
303 struct radeon_dma_bo
*next
, *prev
;
304 struct radeon_bo
*bo
;
309 /* Active dma region. Allocations for vertices and retained
310 * regions come from here. Also used for emitting random vertices,
311 * these may be flushed by calling flush_current();
313 struct radeon_dma_bo free
;
314 struct radeon_dma_bo wait
;
315 struct radeon_dma_bo reserved
;
316 size_t current_used
; /** Number of bytes allocated and forgotten about */
317 size_t current_vertexptr
; /** End of active vertex region */
321 * If current_vertexptr != current_used then flush must be non-zero.
322 * flush must be called before non-active vertex allocations can be
325 void (*flush
) (GLcontext
*);
330 struct radeon_swtcl_info
{
336 /* Fallback rasterization functions
339 GLenum render_primitive
;
342 struct tnl_attr_map vertex_attrs
[VERT_ATTRIB_MAX
];
343 GLuint vertex_attr_count
;
347 #define RADEON_MAX_AOS_ARRAYS 16
348 struct radeon_tcl_info
{
349 struct radeon_aos aos
[RADEON_MAX_AOS_ARRAYS
];
351 struct radeon_bo
*elt_dma_bo
; /** Buffer object that contains element indices */
352 int elt_dma_offset
; /** Offset into this buffer object, in bytes */
355 struct radeon_ioctl
{
356 GLuint vertex_offset
;
357 struct radeon_bo
*bo
;
361 #define RADEON_MAX_PRIMS 64
369 static INLINE GLuint
radeonPackColor(GLuint cpp
,
370 GLubyte r
, GLubyte g
,
371 GLubyte b
, GLubyte a
)
375 return PACK_COLOR_565(r
, g
, b
);
377 return PACK_COLOR_8888(a
, r
, g
, b
);
383 #define MAX_CMD_BUF_SZ (16*1024)
385 #define MAX_DMA_BUF_SZ (64*1024)
387 struct radeon_store
{
390 char cmd_buf
[MAX_CMD_BUF_SZ
];
395 struct radeon_dri_mirror
{
396 __DRIcontextPrivate
*context
; /* DRI context */
397 __DRIscreenPrivate
*screen
; /* DRI screen */
399 drm_context_t hwContext
;
400 drm_hw_lock_t
*hwLock
;
406 #define DEBUG_TEXTURE 0x001
407 #define DEBUG_STATE 0x002
408 #define DEBUG_IOCTL 0x004
409 #define DEBUG_PRIMS 0x008
410 #define DEBUG_VERTS 0x010
411 #define DEBUG_FALLBACKS 0x020
412 #define DEBUG_VFMT 0x040
413 #define DEBUG_CODEGEN 0x080
414 #define DEBUG_VERBOSE 0x100
415 #define DEBUG_DRI 0x200
416 #define DEBUG_DMA 0x400
417 #define DEBUG_SANITY 0x800
418 #define DEBUG_SYNC 0x1000
419 #define DEBUG_PIXEL 0x2000
420 #define DEBUG_MEMORY 0x4000
423 typedef void (*radeon_tri_func
) (radeonContextPtr
,
425 radeonVertex
*, radeonVertex
*);
427 typedef void (*radeon_line_func
) (radeonContextPtr
,
428 radeonVertex
*, radeonVertex
*);
430 typedef void (*radeon_point_func
) (radeonContextPtr
, radeonVertex
*);
432 #define RADEON_MAX_BOS 32
433 struct radeon_state
{
434 struct radeon_colorbuffer_state color
;
435 struct radeon_depthbuffer_state depth
;
436 struct radeon_scissor_state scissor
;
437 struct radeon_stencilbuffer_state stencil
;
439 struct radeon_cs_space_check bos
[RADEON_MAX_BOS
];
440 int validated_bo_count
;
444 * This structure holds the command buffer while it is being constructed.
446 * The first batch of commands in the buffer is always the state that needs
447 * to be re-emitted when the context is lost. This batch can be skipped
450 struct radeon_cmdbuf
{
451 struct radeon_cs_manager
*csm
;
452 struct radeon_cs
*cs
;
453 int size
; /** # of dwords total */
454 unsigned int flushing
:1; /** whether we're currently in FlushCmdBufLocked */
457 struct radeon_context
{
459 radeonScreenPtr radeonScreen
; /* Screen private DRI data */
461 /* Texture object bookkeeping
464 float initialMaxAnisotropy
;
465 uint32_t texture_row_align
;
466 uint32_t texture_rect_row_align
;
467 uint32_t texture_compressed_row_align
;
469 struct radeon_dma dma
;
470 struct radeon_hw_state hw
;
471 /* Rasterization and vertex state:
476 DECLARE_RENDERINPUTS(tnl_index_bitset
); /* index of bits for last tnl_install_attrs */
478 /* Drawable, cliprect and scissor information */
479 GLuint numClipRects
; /* Cliprects for the draw buffer */
480 drm_clip_rect_t
*pClipRects
;
481 unsigned int lastStamp
;
482 drm_radeon_sarea_t
*sarea
; /* Private SAREA data */
484 /* Mirrors of some DRI state */
485 struct radeon_dri_mirror dri
;
491 drm_radeon_irq_wait_t iw
;
493 /* Derived state - for r300 only */
494 struct radeon_state state
;
496 struct radeon_swtcl_info swtcl
;
497 struct radeon_tcl_info tcl
;
498 /* Configuration cache
500 driOptionCache optionCache
;
502 struct radeon_cmdbuf cmdbuf
;
504 drm_clip_rect_t fboRect
;
505 GLboolean constant_cliprect
; /* use for FBO or DRI2 rendering */
506 GLboolean front_cliprects
;
509 * Set if rendering has occured to the drawable's front buffer.
511 * This is used in the DRI2 case to detect that glFlush should also copy
512 * the contents of the fake front buffer to the real front buffer.
514 GLboolean front_buffer_dirty
;
517 * Track whether front-buffer rendering is currently enabled
519 * A separate flag is used to track this in order to support MRT more
522 GLboolean is_front_buffer_rendering
;
525 * Track whether front-buffer is the current read target.
527 * This is closely associated with is_front_buffer_rendering, but may
528 * be set separately. The DRI2 fake front buffer must be referenced
531 GLboolean is_front_buffer_reading
;
533 struct dri_metaops meta
;
536 struct radeon_query_object
*current
;
537 struct radeon_query_object not_flushed_head
;
538 struct radeon_state_atom queryobj
;
542 void (*get_lock
)(radeonContextPtr radeon
);
543 void (*update_viewport_offset
)(GLcontext
*ctx
);
544 void (*emit_cs_header
)(struct radeon_cs
*cs
, radeonContextPtr rmesa
);
545 void (*swtcl_flush
)(GLcontext
*ctx
, uint32_t offset
);
546 void (*pre_emit_atoms
)(radeonContextPtr rmesa
);
547 void (*pre_emit_state
)(radeonContextPtr rmesa
);
548 void (*fallback
)(GLcontext
*ctx
, GLuint bit
, GLboolean mode
);
549 void (*free_context
)(GLcontext
*ctx
);
550 void (*emit_query_finish
)(radeonContextPtr radeon
);
551 void (*update_scissor
)(GLcontext
*ctx
);
555 #define RADEON_CONTEXT(glctx) ((radeonContextPtr)(ctx->DriverCtx))
557 static inline __DRIdrawablePrivate
* radeon_get_drawable(radeonContextPtr radeon
)
559 return radeon
->dri
.context
->driDrawablePriv
;
562 static inline __DRIdrawablePrivate
* radeon_get_readable(radeonContextPtr radeon
)
564 return radeon
->dri
.context
->driReadablePriv
;
568 * This function takes a float and packs it into a uint32_t
570 static INLINE
uint32_t radeonPackFloat32(float fl
)
581 /* This is probably wrong for some values, I need to test this
582 * some more. Range checking would be a good idea also..
584 * But it works for most things. I'll fix it later if someone
585 * else with a better clue doesn't
587 static INLINE
uint32_t radeonPackFloat24(float f
)
591 uint32_t float24
= 0;
596 mantissa
= frexpf(f
, &exponent
);
600 float24
|= (1 << 23);
601 mantissa
= mantissa
* -1.0;
603 /* Handle exponent, bias of 63 */
605 float24
|= (exponent
<< 16);
606 /* Kill 7 LSB of mantissa */
607 float24
|= (radeonPackFloat32(mantissa
) & 0x7FFFFF) >> 7;
612 GLboolean
radeonInitContext(radeonContextPtr radeon
,
613 struct dd_function_table
* functions
,
614 const __GLcontextModes
* glVisual
,
615 __DRIcontextPrivate
* driContextPriv
,
616 void *sharedContextPrivate
);
618 void radeonCleanupContext(radeonContextPtr radeon
);
619 GLboolean
radeonUnbindContext(__DRIcontextPrivate
* driContextPriv
);
620 void radeon_update_renderbuffers(__DRIcontext
*context
, __DRIdrawable
*drawable
);
621 GLboolean
radeonMakeCurrent(__DRIcontextPrivate
* driContextPriv
,
622 __DRIdrawablePrivate
* driDrawPriv
,
623 __DRIdrawablePrivate
* driReadPriv
);
624 extern void radeonDestroyContext(__DRIcontextPrivate
* driContextPriv
);
626 /* ================================================================
632 extern int RADEON_DEBUG
;
634 #define RADEON_DEBUG 0