1 /**************************************************************************
3 Copyright 2000, 2001 ATI Technologies Inc., Ontario, Canada, and
4 VA Linux Systems Inc., Fremont, California.
5 Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
7 The Weather Channel (TM) funded Tungsten Graphics to develop the
8 initial release of the Radeon 8500 driver under the XFree86 license.
9 This notice must be preserved.
13 Permission is hereby granted, free of charge, to any person obtaining
14 a copy of this software and associated documentation files (the
15 "Software"), to deal in the Software without restriction, including
16 without limitation the rights to use, copy, modify, merge, publish,
17 distribute, sublicense, and/or sell copies of the Software, and to
18 permit persons to whom the Software is furnished to do so, subject to
19 the following conditions:
21 The above copyright notice and this permission notice (including the
22 next paragraph) shall be included in all copies or substantial
23 portions of the Software.
25 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
28 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
29 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 **************************************************************************/
35 #include "radeon_common.h"
36 #include "xmlpool.h" /* for symbolic values of enum-type options */
38 #include "drivers/common/meta.h"
39 #include "main/context.h"
40 #include "main/framebuffer.h"
41 #include "main/fbobject.h"
42 #include "main/renderbuffer.h"
43 #include "main/state.h"
44 #include "main/simple_list.h"
45 #include "swrast/swrast.h"
46 #include "swrast_setup/swrast_setup.h"
50 int RADEON_DEBUG
= (0);
54 static const char* get_chip_family_name(int chip_family
)
57 #if defined(RADEON_R100)
58 case CHIP_FAMILY_R100
: return "R100";
59 case CHIP_FAMILY_RV100
: return "RV100";
60 case CHIP_FAMILY_RS100
: return "RS100";
61 case CHIP_FAMILY_RV200
: return "RV200";
62 case CHIP_FAMILY_RS200
: return "RS200";
63 #elif defined(RADEON_R200)
64 case CHIP_FAMILY_R200
: return "R200";
65 case CHIP_FAMILY_RV250
: return "RV250";
66 case CHIP_FAMILY_RS300
: return "RS300";
67 case CHIP_FAMILY_RV280
: return "RV280";
69 default: return "unknown";
74 /* Return various strings for glGetString().
76 static const GLubyte
*radeonGetString(struct gl_context
* ctx
, GLenum name
)
78 radeonContextPtr radeon
= RADEON_CONTEXT(ctx
);
79 static char buffer
[128];
83 return (GLubyte
*) "Tungsten Graphics, Inc.";
88 GLuint agp_mode
= (radeon
->radeonScreen
->card_type
==RADEON_CARD_PCI
) ? 0 :
89 radeon
->radeonScreen
->AGPMode
;
90 char hardwarename
[32];
92 sprintf(hardwarename
, "%s (%s %04X)",
93 #if defined(RADEON_R100)
95 #elif defined(RADEON_R200)
98 get_chip_family_name(radeon
->radeonScreen
->chip_family
),
99 radeon
->radeonScreen
->device_id
);
101 offset
= driGetRendererString(buffer
, hardwarename
, agp_mode
);
103 sprintf(&buffer
[offset
], " %sTCL",
104 !(radeon
->TclFallback
& RADEON_TCL_FALLBACK_TCL_DISABLE
)
107 strcat(buffer
, " DRI2");
109 return (GLubyte
*) buffer
;
117 /* Initialize the driver's misc functions.
119 static void radeonInitDriverFuncs(struct dd_function_table
*functions
)
121 functions
->GetString
= radeonGetString
;
125 * Create and initialize all common fields of the context,
126 * including the Mesa context itself.
128 GLboolean
radeonInitContext(radeonContextPtr radeon
,
129 struct dd_function_table
* functions
,
130 const struct gl_config
* glVisual
,
131 __DRIcontext
* driContextPriv
,
132 void *sharedContextPrivate
)
134 __DRIscreen
*sPriv
= driContextPriv
->driScreenPriv
;
135 radeonScreenPtr screen
= (radeonScreenPtr
) (sPriv
->driverPrivate
);
136 struct gl_context
* ctx
;
137 struct gl_context
* shareCtx
;
140 /* Fill in additional standard functions. */
141 radeonInitDriverFuncs(functions
);
143 radeon
->radeonScreen
= screen
;
144 /* Allocate and initialize the Mesa context */
145 if (sharedContextPrivate
)
146 shareCtx
= ((radeonContextPtr
)sharedContextPrivate
)->glCtx
;
149 radeon
->glCtx
= _mesa_create_context(API_OPENGL
, glVisual
, shareCtx
,
150 functions
, (void *)radeon
);
155 driContextPriv
->driverPrivate
= radeon
;
157 _mesa_meta_init(ctx
);
160 radeon
->dri
.context
= driContextPriv
;
161 radeon
->dri
.screen
= sPriv
;
162 radeon
->dri
.fd
= sPriv
->fd
;
163 radeon
->dri
.drmMinor
= sPriv
->drm_version
.minor
;
166 fthrottle_mode
= driQueryOptioni(&radeon
->optionCache
, "fthrottle_mode");
167 radeon
->iw
.irq_seq
= -1;
168 radeon
->irqsEmitted
= 0;
169 radeon
->do_irqs
= (fthrottle_mode
== DRI_CONF_FTHROTTLE_IRQS
&&
170 radeon
->radeonScreen
->irq
);
172 radeon
->do_usleeps
= (fthrottle_mode
== DRI_CONF_FTHROTTLE_USLEEPS
);
174 if (!radeon
->do_irqs
)
176 "IRQ's not enabled, falling back to %s: %d %d\n",
177 radeon
->do_usleeps
? "usleeps" : "busy waits",
178 fthrottle_mode
, radeon
->radeonScreen
->irq
);
180 radeon
->texture_depth
= driQueryOptioni (&radeon
->optionCache
,
182 if (radeon
->texture_depth
== DRI_CONF_TEXTURE_DEPTH_FB
)
183 radeon
->texture_depth
= ( glVisual
->rgbBits
> 16 ) ?
184 DRI_CONF_TEXTURE_DEPTH_32
: DRI_CONF_TEXTURE_DEPTH_16
;
186 radeon
->texture_row_align
= 32;
187 radeon
->texture_rect_row_align
= 64;
188 radeon
->texture_compressed_row_align
= 32;
190 radeon_init_dma(radeon
);
198 * Destroy the command buffer and state atoms.
200 static void radeon_destroy_atom_list(radeonContextPtr radeon
)
202 struct radeon_state_atom
*atom
;
204 foreach(atom
, &radeon
->hw
.atomlist
) {
213 * Cleanup common context fields.
214 * Called by r200DestroyContext
216 void radeonDestroyContext(__DRIcontext
*driContextPriv
)
218 #ifdef RADEON_BO_TRACK
221 GET_CURRENT_CONTEXT(ctx
);
222 radeonContextPtr radeon
= (radeonContextPtr
) driContextPriv
->driverPrivate
;
223 radeonContextPtr current
= ctx
? RADEON_CONTEXT(ctx
) : NULL
;
227 _mesa_meta_free(radeon
->glCtx
);
229 if (radeon
== current
) {
230 _mesa_make_current(NULL
, NULL
, NULL
);
233 radeon_firevertices(radeon
);
234 if (!is_empty_list(&radeon
->dma
.reserved
)) {
235 rcommonFlushCmdBuf( radeon
, __FUNCTION__
);
238 radeonFreeDmaRegions(radeon
);
239 radeonReleaseArrays(radeon
->glCtx
, ~0);
240 if (radeon
->vtbl
.free_context
)
241 radeon
->vtbl
.free_context(radeon
->glCtx
);
242 _swsetup_DestroyContext( radeon
->glCtx
);
243 _tnl_DestroyContext( radeon
->glCtx
);
244 _vbo_DestroyContext( radeon
->glCtx
);
245 _swrast_DestroyContext( radeon
->glCtx
);
248 /* free the Mesa context */
249 _mesa_destroy_context(radeon
->glCtx
);
251 /* _mesa_destroy_context() might result in calls to functions that
252 * depend on the DriverCtx, so don't set it to NULL before.
254 * radeon->glCtx->DriverCtx = NULL;
256 /* free the option cache */
257 driDestroyOptionCache(&radeon
->optionCache
);
259 rcommonDestroyCmdBuf(radeon
);
261 radeon_destroy_atom_list(radeon
);
263 #ifdef RADEON_BO_TRACK
264 track
= fopen("/tmp/tracklog", "w");
266 radeon_tracker_print(&radeon
->radeonScreen
->bom
->tracker
, track
);
273 /* Force the context `c' to be unbound from its buffer.
275 GLboolean
radeonUnbindContext(__DRIcontext
* driContextPriv
)
277 radeonContextPtr radeon
= (radeonContextPtr
) driContextPriv
->driverPrivate
;
279 if (RADEON_DEBUG
& RADEON_DRI
)
280 fprintf(stderr
, "%s ctx %p\n", __FUNCTION__
,
283 /* Unset current context and dispath table */
284 _mesa_make_current(NULL
, NULL
, NULL
);
291 radeon_bits_per_pixel(const struct radeon_renderbuffer
*rb
)
293 return _mesa_get_format_bytes(rb
->base
.Base
.Format
) * 8;
297 * Check if drawable has been invalidated by dri2InvalidateDrawable().
298 * Update renderbuffers if so. This prevents a client from accessing
299 * a backbuffer that has a swap pending but not yet completed.
301 * See intel_prepare_render for equivalent code in intel driver.
304 void radeon_prepare_render(radeonContextPtr radeon
)
306 __DRIcontext
*driContext
= radeon
->dri
.context
;
307 __DRIdrawable
*drawable
;
310 screen
= driContext
->driScreenPriv
;
311 if (!screen
->dri2
.loader
)
314 drawable
= driContext
->driDrawablePriv
;
315 if (drawable
->dri2
.stamp
!= driContext
->dri2
.draw_stamp
) {
316 if (drawable
->lastStamp
!= drawable
->dri2
.stamp
)
317 radeon_update_renderbuffers(driContext
, drawable
, GL_FALSE
);
319 /* Intel driver does the equivalent of this, no clue if it is needed:*/
320 radeon_draw_buffer(radeon
->glCtx
, radeon
->glCtx
->DrawBuffer
);
322 driContext
->dri2
.draw_stamp
= drawable
->dri2
.stamp
;
325 drawable
= driContext
->driReadablePriv
;
326 if (drawable
->dri2
.stamp
!= driContext
->dri2
.read_stamp
) {
327 if (drawable
->lastStamp
!= drawable
->dri2
.stamp
)
328 radeon_update_renderbuffers(driContext
, drawable
, GL_FALSE
);
329 driContext
->dri2
.read_stamp
= drawable
->dri2
.stamp
;
332 /* If we're currently rendering to the front buffer, the rendering
333 * that will happen next will probably dirty the front buffer. So
334 * mark it as dirty here.
336 if (radeon
->is_front_buffer_rendering
)
337 radeon
->front_buffer_dirty
= GL_TRUE
;
341 radeon_update_renderbuffers(__DRIcontext
*context
, __DRIdrawable
*drawable
,
342 GLboolean front_only
)
344 unsigned int attachments
[10];
345 __DRIbuffer
*buffers
= NULL
;
347 struct radeon_renderbuffer
*rb
;
349 struct radeon_framebuffer
*draw
;
350 radeonContextPtr radeon
;
352 struct radeon_bo
*depth_bo
= NULL
, *bo
;
354 if (RADEON_DEBUG
& RADEON_DRI
)
355 fprintf(stderr
, "enter %s, drawable %p\n", __func__
, drawable
);
357 draw
= drawable
->driverPrivate
;
358 screen
= context
->driScreenPriv
;
359 radeon
= (radeonContextPtr
) context
->driverPrivate
;
361 /* Set this up front, so that in case our buffers get invalidated
362 * while we're getting new buffers, we don't clobber the stamp and
363 * thus ignore the invalidate. */
364 drawable
->lastStamp
= drawable
->dri2
.stamp
;
366 if (screen
->dri2
.loader
367 && (screen
->dri2
.loader
->base
.version
> 2)
368 && (screen
->dri2
.loader
->getBuffersWithFormat
!= NULL
)) {
369 struct radeon_renderbuffer
*depth_rb
;
370 struct radeon_renderbuffer
*stencil_rb
;
373 if ((front_only
|| radeon
->is_front_buffer_rendering
||
374 radeon
->is_front_buffer_reading
||
376 && draw
->color_rb
[0]) {
377 attachments
[i
++] = __DRI_BUFFER_FRONT_LEFT
;
378 attachments
[i
++] = radeon_bits_per_pixel(draw
->color_rb
[0]);
382 if (draw
->color_rb
[1]) {
383 attachments
[i
++] = __DRI_BUFFER_BACK_LEFT
;
384 attachments
[i
++] = radeon_bits_per_pixel(draw
->color_rb
[1]);
387 depth_rb
= radeon_get_renderbuffer(&draw
->base
, BUFFER_DEPTH
);
388 stencil_rb
= radeon_get_renderbuffer(&draw
->base
, BUFFER_STENCIL
);
390 if ((depth_rb
!= NULL
) && (stencil_rb
!= NULL
)) {
391 attachments
[i
++] = __DRI_BUFFER_DEPTH_STENCIL
;
392 attachments
[i
++] = radeon_bits_per_pixel(depth_rb
);
393 } else if (depth_rb
!= NULL
) {
394 attachments
[i
++] = __DRI_BUFFER_DEPTH
;
395 attachments
[i
++] = radeon_bits_per_pixel(depth_rb
);
396 } else if (stencil_rb
!= NULL
) {
397 attachments
[i
++] = __DRI_BUFFER_STENCIL
;
398 attachments
[i
++] = radeon_bits_per_pixel(stencil_rb
);
402 buffers
= (*screen
->dri2
.loader
->getBuffersWithFormat
)(drawable
,
407 drawable
->loaderPrivate
);
408 } else if (screen
->dri2
.loader
) {
410 if (draw
->color_rb
[0])
411 attachments
[i
++] = __DRI_BUFFER_FRONT_LEFT
;
413 if (draw
->color_rb
[1])
414 attachments
[i
++] = __DRI_BUFFER_BACK_LEFT
;
415 if (radeon_get_renderbuffer(&draw
->base
, BUFFER_DEPTH
))
416 attachments
[i
++] = __DRI_BUFFER_DEPTH
;
417 if (radeon_get_renderbuffer(&draw
->base
, BUFFER_STENCIL
))
418 attachments
[i
++] = __DRI_BUFFER_STENCIL
;
421 buffers
= (*screen
->dri2
.loader
->getBuffers
)(drawable
,
426 drawable
->loaderPrivate
);
432 for (i
= 0; i
< count
; i
++) {
433 switch (buffers
[i
].attachment
) {
434 case __DRI_BUFFER_FRONT_LEFT
:
435 rb
= draw
->color_rb
[0];
436 regname
= "dri2 front buffer";
438 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
439 rb
= draw
->color_rb
[0];
440 regname
= "dri2 fake front buffer";
442 case __DRI_BUFFER_BACK_LEFT
:
443 rb
= draw
->color_rb
[1];
444 regname
= "dri2 back buffer";
446 case __DRI_BUFFER_DEPTH
:
447 rb
= radeon_get_renderbuffer(&draw
->base
, BUFFER_DEPTH
);
448 regname
= "dri2 depth buffer";
450 case __DRI_BUFFER_DEPTH_STENCIL
:
451 rb
= radeon_get_renderbuffer(&draw
->base
, BUFFER_DEPTH
);
452 regname
= "dri2 depth / stencil buffer";
454 case __DRI_BUFFER_STENCIL
:
455 rb
= radeon_get_renderbuffer(&draw
->base
, BUFFER_STENCIL
);
456 regname
= "dri2 stencil buffer";
458 case __DRI_BUFFER_ACCUM
:
461 "unhandled buffer attach event, attacment type %d\n",
462 buffers
[i
].attachment
);
470 uint32_t name
= radeon_gem_name_bo(rb
->bo
);
471 if (name
== buffers
[i
].name
)
475 if (RADEON_DEBUG
& RADEON_DRI
)
477 "attaching buffer %s, %d, at %d, cpp %d, pitch %d\n",
478 regname
, buffers
[i
].name
, buffers
[i
].attachment
,
479 buffers
[i
].cpp
, buffers
[i
].pitch
);
481 rb
->cpp
= buffers
[i
].cpp
;
482 rb
->pitch
= buffers
[i
].pitch
;
483 rb
->base
.Base
.Width
= drawable
->w
;
484 rb
->base
.Base
.Height
= drawable
->h
;
487 if (buffers
[i
].attachment
== __DRI_BUFFER_STENCIL
&& depth_bo
) {
488 if (RADEON_DEBUG
& RADEON_DRI
)
489 fprintf(stderr
, "(reusing depth buffer as stencil)\n");
493 uint32_t tiling_flags
= 0, pitch
= 0;
496 bo
= radeon_bo_open(radeon
->radeonScreen
->bom
,
500 RADEON_GEM_DOMAIN_VRAM
,
504 fprintf(stderr
, "failed to attach %s %d\n",
505 regname
, buffers
[i
].name
);
509 ret
= radeon_bo_get_tiling(bo
, &tiling_flags
, &pitch
);
512 "failed to get tiling for %s %d\n",
513 regname
, buffers
[i
].name
);
518 if (tiling_flags
& RADEON_TILING_MACRO
)
519 bo
->flags
|= RADEON_BO_FLAGS_MACRO_TILE
;
520 if (tiling_flags
& RADEON_TILING_MICRO
)
521 bo
->flags
|= RADEON_BO_FLAGS_MICRO_TILE
;
525 if (buffers
[i
].attachment
== __DRI_BUFFER_DEPTH
) {
526 if (draw
->base
.Visual
.depthBits
== 16)
531 radeon_renderbuffer_set_bo(rb
, bo
);
534 if (buffers
[i
].attachment
== __DRI_BUFFER_DEPTH_STENCIL
) {
535 rb
= radeon_get_renderbuffer(&draw
->base
, BUFFER_STENCIL
);
537 struct radeon_bo
*stencil_bo
= NULL
;
540 uint32_t name
= radeon_gem_name_bo(rb
->bo
);
541 if (name
== buffers
[i
].name
)
546 radeon_bo_ref(stencil_bo
);
547 radeon_renderbuffer_set_bo(rb
, stencil_bo
);
548 radeon_bo_unref(stencil_bo
);
553 driUpdateFramebufferSize(radeon
->glCtx
, drawable
);
556 /* Force the context `c' to be the current context and associate with it
559 GLboolean
radeonMakeCurrent(__DRIcontext
* driContextPriv
,
560 __DRIdrawable
* driDrawPriv
,
561 __DRIdrawable
* driReadPriv
)
563 radeonContextPtr radeon
;
564 GET_CURRENT_CONTEXT(curCtx
);
565 struct gl_framebuffer
*drfb
, *readfb
;
568 radeon
= (radeonContextPtr
)driContextPriv
->driverPrivate
;
571 /* According to the glXMakeCurrent() man page: "Pending commands to
572 * the previous context, if any, are flushed before it is released."
573 * But only flush if we're actually changing contexts.
576 if ((radeonContextPtr
)curCtx
&& (radeonContextPtr
)curCtx
!= radeon
) {
580 if (!driContextPriv
) {
581 if (RADEON_DEBUG
& RADEON_DRI
)
582 fprintf(stderr
, "%s ctx is null\n", __FUNCTION__
);
583 _mesa_make_current(NULL
, NULL
, NULL
);
587 if(driDrawPriv
== NULL
&& driReadPriv
== NULL
) {
588 drfb
= _mesa_create_framebuffer(&radeon
->glCtx
->Visual
);
592 drfb
= driDrawPriv
->driverPrivate
;
593 readfb
= driReadPriv
->driverPrivate
;
597 radeon_update_renderbuffers(driContextPriv
, driDrawPriv
, GL_FALSE
);
598 if (driDrawPriv
!= driReadPriv
)
599 radeon_update_renderbuffers(driContextPriv
, driReadPriv
, GL_FALSE
);
600 _mesa_reference_renderbuffer(&radeon
->state
.color
.rb
,
601 &(radeon_get_renderbuffer(drfb
, BUFFER_BACK_LEFT
)->base
.Base
));
602 _mesa_reference_renderbuffer(&radeon
->state
.depth
.rb
,
603 &(radeon_get_renderbuffer(drfb
, BUFFER_DEPTH
)->base
.Base
));
605 if (RADEON_DEBUG
& RADEON_DRI
)
606 fprintf(stderr
, "%s ctx %p dfb %p rfb %p\n", __FUNCTION__
, radeon
->glCtx
, drfb
, readfb
);
609 driUpdateFramebufferSize(radeon
->glCtx
, driDrawPriv
);
610 if (driReadPriv
!= driDrawPriv
)
611 driUpdateFramebufferSize(radeon
->glCtx
, driReadPriv
);
613 _mesa_make_current(radeon
->glCtx
, drfb
, readfb
);
614 if (driDrawPriv
== NULL
&& driReadPriv
== NULL
)
615 _mesa_reference_framebuffer(&drfb
, NULL
);
617 _mesa_update_state(radeon
->glCtx
);
619 if (radeon
->glCtx
->DrawBuffer
== drfb
) {
620 if(driDrawPriv
!= NULL
) {
621 radeon_window_moved(radeon
);
624 radeon_draw_buffer(radeon
->glCtx
, drfb
);
628 if (RADEON_DEBUG
& RADEON_DRI
)
629 fprintf(stderr
, "End %s\n", __FUNCTION__
);