1 /* $XFree86: xc/lib/GL/mesa/src/drv/r200/r200_pixel.c,v 1.2 2002/12/16 16:18:54 dawes Exp $ */
3 Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
5 The Weather Channel (TM) funded Tungsten Graphics to develop the
6 initial release of the Radeon 8500 driver under the XFree86 license.
7 This notice must be preserved.
9 Permission is hereby granted, free of charge, to any person obtaining
10 a copy of this software and associated documentation files (the
11 "Software"), to deal in the Software without restriction, including
12 without limitation the rights to use, copy, modify, merge, publish,
13 distribute, sublicense, and/or sell copies of the Software, and to
14 permit persons to whom the Software is furnished to do so, subject to
15 the following conditions:
17 The above copyright notice and this permission notice (including the
18 next paragraph) shall be included in all copies or substantial
19 portions of the Software.
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
25 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 * Keith Whitwell <keith@tungstengraphics.com>
39 #include "swrast/swrast.h"
41 #include "r200_context.h"
42 #include "r200_ioctl.h"
43 #include "r200_pixel.h"
44 #include "r200_swtcl.h"
49 check_color( const GLcontext
*ctx
, GLenum type
, GLenum format
,
50 const struct gl_pixelstore_attrib
*packing
,
51 const void *pixels
, GLint sz
, GLint pitch
)
53 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
54 GLuint cpp
= rmesa
->r200Screen
->cpp
;
56 if (R200_DEBUG
& DEBUG_PIXEL
)
57 fprintf(stderr
, "%s\n", __FUNCTION__
);
60 ctx
->_ImageTransferState
||
63 if (R200_DEBUG
& DEBUG_PIXEL
)
64 fprintf(stderr
, "%s: failed 1\n", __FUNCTION__
);
68 if ( type
== GL_UNSIGNED_INT_8_8_8_8_REV
&&
71 if (R200_DEBUG
& DEBUG_PIXEL
)
72 fprintf(stderr
, "%s: passed 2\n", __FUNCTION__
);
76 if (R200_DEBUG
& DEBUG_PIXEL
)
77 fprintf(stderr
, "%s: failed\n", __FUNCTION__
);
83 check_color_per_fragment_ops( const GLcontext
*ctx
)
86 result
= (!( ctx
->Color
.AlphaEnabled
||
89 ctx
->Scissor
.Enabled
||
90 ctx
->Stencil
.Enabled
||
91 !ctx
->Color
.ColorMask
[0] ||
92 !ctx
->Color
.ColorMask
[1] ||
93 !ctx
->Color
.ColorMask
[2] ||
94 !ctx
->Color
.ColorMask
[3] ||
95 ctx
->Color
.ColorLogicOpEnabled
||
96 ctx
->Texture
._EnabledUnits
||
97 ctx
->Depth
.OcclusionTest
99 ctx
->Current
.RasterPosValid
);
107 clip_pixelrect( const GLcontext
*ctx
,
108 const GLframebuffer
*buffer
,
110 GLsizei
*width
, GLsizei
*height
,
113 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
116 if (*x
< buffer
->_Xmin
) {
117 *width
-= (buffer
->_Xmin
- *x
);
122 if (*x
+ *width
> buffer
->_Xmax
)
123 *width
-= (*x
+ *width
- buffer
->_Xmax
- 1);
128 /* bottom clipping */
129 if (*y
< buffer
->_Ymin
) {
130 *height
-= (buffer
->_Ymin
- *y
);
135 if (*y
+ *height
> buffer
->_Ymax
)
136 *height
-= (*y
+ *height
- buffer
->_Ymax
- 1);
141 *size
= ((*y
+ *height
- 1) * rmesa
->r200Screen
->frontPitch
+
142 (*x
+ *width
- 1) * rmesa
->r200Screen
->cpp
);
148 r200TryReadPixels( GLcontext
*ctx
,
149 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
150 GLenum format
, GLenum type
,
151 const struct gl_pixelstore_attrib
*pack
,
154 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
155 GLint pitch
= pack
->RowLength
? pack
->RowLength
: width
;
157 GLuint cpp
= rmesa
->r200Screen
->cpp
;
158 GLint size
= width
* height
* cpp
;
160 if (R200_DEBUG
& DEBUG_PIXEL
)
161 fprintf(stderr
, "%s\n", __FUNCTION__
);
163 /* Only accelerate reading to GART buffers.
165 if ( !r200IsGartMemory(rmesa
, pixels
,
166 pitch
* height
* rmesa
->r200Screen
->cpp
) ) {
167 if (R200_DEBUG
& DEBUG_PIXEL
)
168 fprintf(stderr
, "%s: dest not GART\n", __FUNCTION__
);
172 /* Need GL_PACK_INVERT_MESA to cope with upsidedown results from
176 if (R200_DEBUG
& DEBUG_PIXEL
)
177 fprintf(stderr
, "%s: MESA_PACK_INVERT not set\n", __FUNCTION__
);
181 if (!check_color(ctx
, type
, format
, pack
, pixels
, size
, pitch
))
184 switch ( rmesa
->r200Screen
->cpp
) {
186 blit_format
= R200_CP_COLOR_FORMAT_ARGB8888
;
193 /* Although the blits go on the command buffer, need to do this and
194 * fire with lock held to guarentee cliprects and drawOffset are
197 * This is an unusual situation however, as the code which flushes
198 * a full command buffer expects to be called unlocked. As a
199 * workaround, immediately flush the buffer on aquiring the lock.
201 LOCK_HARDWARE( rmesa
);
203 if (rmesa
->store
.cmd_used
)
204 r200FlushCmdBufLocked( rmesa
, __FUNCTION__
);
206 if (!clip_pixelrect(ctx
, ctx
->ReadBuffer
, &x
, &y
, &width
, &height
,
208 UNLOCK_HARDWARE( rmesa
);
209 if (R200_DEBUG
& DEBUG_PIXEL
)
210 fprintf(stderr
, "%s totally clipped -- nothing to do\n",
216 __DRIdrawablePrivate
*dPriv
= rmesa
->dri
.drawable
;
217 int nbox
= dPriv
->numClipRects
;
218 int src_offset
= rmesa
->state
.color
.drawOffset
219 + rmesa
->r200Screen
->fbLocation
;
220 int src_pitch
= rmesa
->state
.color
.drawPitch
* rmesa
->r200Screen
->cpp
;
221 int dst_offset
= r200GartOffsetFromVirtual( rmesa
, pixels
);
222 int dst_pitch
= pitch
* rmesa
->r200Screen
->cpp
;
223 drm_clip_rect_t
*box
= dPriv
->pClipRects
;
226 r200EmitWait( rmesa
, RADEON_WAIT_3D
);
228 y
= dPriv
->h
- y
- height
;
233 if (R200_DEBUG
& DEBUG_PIXEL
)
234 fprintf(stderr
, "readpixel blit src_pitch %d dst_pitch %d\n",
235 src_pitch
, dst_pitch
);
237 for (i
= 0 ; i
< nbox
; i
++)
239 GLint bx
= box
[i
].x1
;
240 GLint by
= box
[i
].y1
;
241 GLint bw
= box
[i
].x2
- bx
;
242 GLint bh
= box
[i
].y2
- by
;
244 if (bx
< x
) bw
-= x
- bx
, bx
= x
;
245 if (by
< y
) bh
-= y
- by
, by
= y
;
246 if (bx
+ bw
> x
+ width
) bw
= x
+ width
- bx
;
247 if (by
+ bh
> y
+ height
) bh
= y
+ height
- by
;
248 if (bw
<= 0) continue;
249 if (bh
<= 0) continue;
253 src_pitch
, src_offset
,
254 dst_pitch
, dst_offset
,
260 r200FlushCmdBufLocked( rmesa
, __FUNCTION__
);
262 UNLOCK_HARDWARE( rmesa
);
264 r200Finish( ctx
); /* required by GL */
270 r200ReadPixels( GLcontext
*ctx
,
271 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
272 GLenum format
, GLenum type
,
273 const struct gl_pixelstore_attrib
*pack
,
276 if (R200_DEBUG
& DEBUG_PIXEL
)
277 fprintf(stderr
, "%s\n", __FUNCTION__
);
279 if (!r200TryReadPixels( ctx
, x
, y
, width
, height
, format
, type
, pack
,
281 _swrast_ReadPixels( ctx
, x
, y
, width
, height
, format
, type
, pack
,
288 static void do_draw_pix( GLcontext
*ctx
,
289 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
294 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
295 __DRIdrawablePrivate
*dPriv
= rmesa
->dri
.drawable
;
296 drm_clip_rect_t
*box
= dPriv
->pClipRects
;
297 int nbox
= dPriv
->numClipRects
;
301 int src_offset
= r200GartOffsetFromVirtual( rmesa
, pixels
);
302 int src_pitch
= pitch
* rmesa
->r200Screen
->cpp
;
304 if (R200_DEBUG
& DEBUG_PIXEL
)
305 fprintf(stderr
, "%s\n", __FUNCTION__
);
307 switch ( rmesa
->r200Screen
->cpp
) {
309 blit_format
= R200_CP_COLOR_FORMAT_RGB565
;
312 blit_format
= R200_CP_COLOR_FORMAT_ARGB8888
;
319 LOCK_HARDWARE( rmesa
);
321 if (rmesa
->store
.cmd_used
)
322 r200FlushCmdBufLocked( rmesa
, __FUNCTION__
);
324 y
-= height
; /* cope with pixel zoom */
326 if (!clip_pixelrect(ctx
, ctx
->DrawBuffer
,
327 &x
, &y
, &width
, &height
,
329 UNLOCK_HARDWARE( rmesa
);
333 y
= dPriv
->h
- y
- height
; /* convert from gl to hardware coords */
338 r200EmitWait( rmesa
, RADEON_WAIT_3D
);
340 for (i
= 0 ; i
< nbox
; i
++ )
342 GLint bx
= box
[i
].x1
;
343 GLint by
= box
[i
].y1
;
344 GLint bw
= box
[i
].x2
- bx
;
345 GLint bh
= box
[i
].y2
- by
;
347 if (bx
< x
) bw
-= x
- bx
, bx
= x
;
348 if (by
< y
) bh
-= y
- by
, by
= y
;
349 if (bx
+ bw
> x
+ width
) bw
= x
+ width
- bx
;
350 if (by
+ bh
> y
+ height
) bh
= y
+ height
- by
;
351 if (bw
<= 0) continue;
352 if (bh
<= 0) continue;
356 src_pitch
, src_offset
,
357 rmesa
->state
.color
.drawPitch
* rmesa
->r200Screen
->cpp
,
358 rmesa
->state
.color
.drawOffset
+ rmesa
->r200Screen
->fbLocation
,
364 r200FlushCmdBufLocked( rmesa
, __FUNCTION__
);
365 r200WaitForIdleLocked( rmesa
); /* required by GL */
366 UNLOCK_HARDWARE( rmesa
);
373 r200TryDrawPixels( GLcontext
*ctx
,
374 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
375 GLenum format
, GLenum type
,
376 const struct gl_pixelstore_attrib
*unpack
,
377 const GLvoid
*pixels
)
379 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
380 GLint pitch
= unpack
->RowLength
? unpack
->RowLength
: width
;
382 GLuint cpp
= rmesa
->r200Screen
->cpp
;
383 GLint size
= width
* pitch
* cpp
;
385 if (R200_DEBUG
& DEBUG_PIXEL
)
386 fprintf(stderr
, "%s\n", __FUNCTION__
);
392 planemask
= r200PackColor(cpp
,
393 ctx
->Color
.ColorMask
[RCOMP
],
394 ctx
->Color
.ColorMask
[GCOMP
],
395 ctx
->Color
.ColorMask
[BCOMP
],
396 ctx
->Color
.ColorMask
[ACOMP
]);
399 planemask
|= planemask
<< 16;
402 return GL_FALSE
; /* fix me -- should be possible */
404 /* Can't do conversions on GART reads/draws.
406 if ( !r200IsGartMemory( rmesa
, pixels
, size
) ) {
407 if (R200_DEBUG
& DEBUG_PIXEL
)
408 fprintf(stderr
, "%s: not GART memory\n", __FUNCTION__
);
412 if (!check_color(ctx
, type
, format
, unpack
, pixels
, size
, pitch
)) {
415 if (!check_color_per_fragment_ops(ctx
)) {
419 if (ctx
->Pixel
.ZoomX
!= 1.0F
||
420 ctx
->Pixel
.ZoomY
!= -1.0F
)
428 if ( r200IsGartMemory(rmesa
, pixels
, size
) )
430 do_draw_pix( ctx
, x
, y
, width
, height
, pitch
, pixels
, planemask
);
435 /* Pixels is in regular memory -- get dma buffers and perform
436 * upload through them.
444 r200DrawPixels( GLcontext
*ctx
,
445 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
446 GLenum format
, GLenum type
,
447 const struct gl_pixelstore_attrib
*unpack
,
448 const GLvoid
*pixels
)
450 if (R200_DEBUG
& DEBUG_PIXEL
)
451 fprintf(stderr
, "%s\n", __FUNCTION__
);
453 if (!r200TryDrawPixels( ctx
, x
, y
, width
, height
, format
, type
,
455 _swrast_DrawPixels( ctx
, x
, y
, width
, height
, format
, type
,
461 r200Bitmap( GLcontext
*ctx
, GLint px
, GLint py
,
462 GLsizei width
, GLsizei height
,
463 const struct gl_pixelstore_attrib
*unpack
,
464 const GLubyte
*bitmap
)
466 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
469 _swrast_Bitmap( ctx
, px
, py
, width
, height
, unpack
, bitmap
);
471 r200PointsBitmap( ctx
, px
, py
, width
, height
, unpack
, bitmap
);
476 void r200InitPixelFuncs( GLcontext
*ctx
)
478 /* Pixel path fallbacks.
480 ctx
->Driver
.Accum
= _swrast_Accum
;
481 ctx
->Driver
.Bitmap
= _swrast_Bitmap
;
482 ctx
->Driver
.CopyPixels
= _swrast_CopyPixels
;
483 ctx
->Driver
.DrawPixels
= _swrast_DrawPixels
;
484 ctx
->Driver
.ReadPixels
= _swrast_ReadPixels
;
486 if (!getenv("R200_NO_BLITS") && R200_CONTEXT(ctx
)->dri
.drmMinor
>= 6) {
487 ctx
->Driver
.ReadPixels
= r200ReadPixels
;
488 ctx
->Driver
.DrawPixels
= r200DrawPixels
;
489 if (getenv("R200_HW_BITMAP"))
490 ctx
->Driver
.Bitmap
= r200Bitmap
;