1 /**************************************************************************
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portionsalloc
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.
26 **************************************************************************/
28 #include "main/glheader.h"
29 #include "main/enums.h"
30 #include "main/image.h"
31 #include "main/mtypes.h"
32 #include "main/macros.h"
33 #include "main/bufferobj.h"
34 #include "swrast/swrast.h"
36 #include "intel_screen.h"
37 #include "intel_context.h"
38 #include "intel_batchbuffer.h"
39 #include "intel_blit.h"
40 #include "intel_buffers.h"
41 #include "intel_regions.h"
42 #include "intel_pixel.h"
43 #include "intel_buffer_objects.h"
44 #include "intel_tris.h"
49 do_texture_drawpixels(GLcontext
* ctx
,
51 GLsizei width
, GLsizei height
,
52 GLenum format
, GLenum type
,
53 const struct gl_pixelstore_attrib
*unpack
,
54 const GLvoid
* pixels
)
56 struct intel_context
*intel
= intel_context(ctx
);
57 struct intel_region
*dst
= intel_drawbuf_region(intel
);
58 struct intel_buffer_object
*src
= intel_buffer_object(unpack
->BufferObj
);
59 GLuint rowLength
= unpack
->RowLength
? unpack
->RowLength
: width
;
62 if (INTEL_DEBUG
& DEBUG_PIXEL
)
63 fprintf(stderr
, "%s\n", __FUNCTION__
);
65 intelFlush(&intel
->ctx
);
70 intel
->vtbl
.render_start(intel
);
71 intel
->vtbl
.emit_state(intel
);
74 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
75 format
, type
, pixels
)) {
76 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawPixels");
83 if (INTEL_DEBUG
& DEBUG_PIXEL
)
84 _mesa_printf("%s - not PBO\n", __FUNCTION__
);
88 /* There are a couple of things we can't do yet, one of which is
89 * set the correct state for pixel operations when GL texturing is
90 * enabled. That's a pretty rare state and probably not worth the
91 * effort. A completely device-independent version of this may do
94 * Similarly, we make no attempt to merge metaops processing with
95 * an enabled fragment program, though it would certainly be
98 if (!intel_check_meta_tex_fragment_ops(ctx
)) {
99 if (INTEL_DEBUG
& DEBUG_PIXEL
)
100 _mesa_printf("%s - bad GL fragment state for metaops texture\n",
105 intel
->vtbl
.install_meta_state(intel
);
108 /* Is this true? Also will need to turn depth testing on according
111 intel
->vtbl
.meta_no_stencil_write(intel
);
112 intel
->vtbl
.meta_no_depth_write(intel
);
114 /* Set the 3d engine to draw into the destination region:
116 intel
->vtbl
.meta_draw_region(intel
, dst
, intel
->depth_region
);
118 intel
->vtbl
.meta_import_pixel_state(intel
);
120 src_offset
= (GLuint
) _mesa_image_address(2, unpack
, pixels
, width
, height
,
121 format
, type
, 0, 0, 0);
124 /* Setup the pbo up as a rectangular texture, if possible.
126 * TODO: This is almost always possible if the i915 fragment
127 * program is adjusted to correctly swizzle the sampled colors.
128 * The major exception is any 24bit texture, like RGB888, for which
129 * there is no hardware support.
131 if (!intel
->vtbl
.meta_tex_rect_source(intel
, src
->buffer
, src_offset
,
132 rowLength
, height
, format
, type
)) {
133 intel
->vtbl
.leave_meta_state(intel
);
137 intel
->vtbl
.meta_texture_blend_replace(intel
);
140 LOCK_HARDWARE(intel
);
142 if (intel
->driDrawable
->numClipRects
) {
143 __DRIdrawablePrivate
*dPriv
= intel
->driDrawable
;
148 dsty
= dPriv
->h
- (y
+ height
);
150 srcx
= 0; /* skiprows/pixels already done */
154 const GLint orig_x
= dstx
;
155 const GLint orig_y
= dsty
;
157 if (!_mesa_clip_to_region(0, 0, dst
->pitch
, dst
->height
,
158 &dstx
, &dsty
, &width
, &height
))
161 srcx
+= dstx
- orig_x
;
162 srcy
+= dsty
- orig_y
;
166 if (INTEL_DEBUG
& DEBUG_PIXEL
)
167 _mesa_printf("draw %d,%d %dx%d\n", dstx
, dsty
, width
, height
);
169 /* Must use the regular cliprect mechanism in order to get the
170 * drawing origin set correctly. Otherwise scissor state is in
171 * incorrect coordinate space. Does this even need to hold the
174 intel
->vtbl
.meta_draw_quad(intel
,
175 dstx
, dstx
+ width
* ctx
->Pixel
.ZoomX
,
176 dPriv
->h
- (y
+ height
* ctx
->Pixel
.ZoomY
),
178 -ctx
->Current
.RasterPos
[2] * .5,
180 srcx
, srcx
+ width
, srcy
+ height
, srcy
);
182 intel
->vtbl
.leave_meta_state(intel
);
183 intel_batchbuffer_emit_mi_flush(intel
->batch
);
185 UNLOCK_HARDWARE(intel
);
194 * - no waiting for idle before updating framebuffer.
197 * - if upload is by memcpy, this may actually be slower than fallback path.
198 * - uploads the whole image even if destination is clipped
202 * Given the questions about performance, implement for pbo's only.
203 * This path is definitely a win if the pbo is already in agp. If it
204 * turns out otherwise, we can add the code necessary to upload client
205 * data to agp space before performing the blit. (Though it may turn
206 * out to be better/simpler just to use the texture engine).
209 do_blit_drawpixels(GLcontext
* ctx
,
211 GLsizei width
, GLsizei height
,
212 GLenum format
, GLenum type
,
213 const struct gl_pixelstore_attrib
*unpack
,
214 const GLvoid
* pixels
)
216 struct intel_context
*intel
= intel_context(ctx
);
217 struct intel_region
*dest
= intel_drawbuf_region(intel
);
218 struct intel_buffer_object
*src
= intel_buffer_object(unpack
->BufferObj
);
222 if (INTEL_DEBUG
& DEBUG_PIXEL
)
223 _mesa_printf("%s\n", __FUNCTION__
);
227 if (INTEL_DEBUG
& DEBUG_PIXEL
)
228 _mesa_printf("%s - no dest\n", __FUNCTION__
);
233 /* This validation should be done by core mesa:
235 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
236 format
, type
, pixels
)) {
237 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawPixels");
244 if (INTEL_DEBUG
& DEBUG_PIXEL
)
245 _mesa_printf("%s - not PBO\n", __FUNCTION__
);
249 if (!intel_check_blit_format(dest
, format
, type
)) {
250 if (INTEL_DEBUG
& DEBUG_PIXEL
)
251 _mesa_printf("%s - bad format for blit\n", __FUNCTION__
);
255 if (!intel_check_blit_fragment_ops(ctx
, GL_FALSE
)) {
256 if (INTEL_DEBUG
& DEBUG_PIXEL
)
257 _mesa_printf("%s - bad GL fragment state for blitter\n",
262 if (ctx
->Pixel
.ZoomX
!= 1.0F
) {
263 if (INTEL_DEBUG
& DEBUG_PIXEL
)
264 _mesa_printf("%s - bad PixelZoomX for blit\n", __FUNCTION__
);
269 if (unpack
->RowLength
> 0)
270 rowLength
= unpack
->RowLength
;
274 if (ctx
->Pixel
.ZoomY
== -1.0F
) {
275 if (INTEL_DEBUG
& DEBUG_PIXEL
)
276 _mesa_printf("%s - bad PixelZoomY for blit\n", __FUNCTION__
);
277 return GL_FALSE
; /* later */
280 else if (ctx
->Pixel
.ZoomY
== 1.0F
) {
281 rowLength
= -rowLength
;
284 if (INTEL_DEBUG
& DEBUG_PIXEL
)
285 _mesa_printf("%s - bad PixelZoomY for blit\n", __FUNCTION__
);
289 src_offset
= (GLuint
) _mesa_image_address(2, unpack
, pixels
, width
, height
,
290 format
, type
, 0, 0, 0);
292 intelFlush(&intel
->ctx
);
293 LOCK_HARDWARE(intel
);
295 if (intel
->driDrawable
->numClipRects
) {
296 __DRIdrawablePrivate
*dPriv
= intel
->driDrawable
;
297 int nbox
= dPriv
->numClipRects
;
298 drm_clip_rect_t
*box
= dPriv
->pClipRects
;
299 drm_clip_rect_t rect
;
300 drm_clip_rect_t dest_rect
;
301 dri_bo
*src_buffer
= intel_bufferobj_buffer(intel
, src
, INTEL_READ
);
304 dest_rect
.x1
= dPriv
->x
+ x
;
305 dest_rect
.y1
= dPriv
->y
+ dPriv
->h
- (y
+ height
);
306 dest_rect
.x2
= dest_rect
.x1
+ width
;
307 dest_rect
.y2
= dest_rect
.y1
+ height
;
309 for (i
= 0; i
< nbox
; i
++) {
310 if (!intel_intersect_cliprects(&rect
, &dest_rect
, &box
[i
]))
313 intelEmitCopyBlit(intel
,
315 rowLength
, src_buffer
, src_offset
, GL_FALSE
,
316 dest
->pitch
, dest
->buffer
, 0, dest
->tiling
,
317 rect
.x1
- dest_rect
.x1
,
318 rect
.y2
- dest_rect
.y2
,
320 rect
.y1
, rect
.x2
- rect
.x1
, rect
.y2
- rect
.y1
,
321 ctx
->Color
.ColorLogicOpEnabled
?
322 ctx
->Color
.LogicOp
: GL_COPY
);
325 UNLOCK_HARDWARE(intel
);
327 if (INTEL_DEBUG
& DEBUG_PIXEL
)
328 _mesa_printf("%s - DONE\n", __FUNCTION__
);
336 intelDrawPixels(GLcontext
* ctx
,
338 GLsizei width
, GLsizei height
,
341 const struct gl_pixelstore_attrib
*unpack
,
342 const GLvoid
* pixels
)
344 if (do_blit_drawpixels(ctx
, x
, y
, width
, height
, format
, type
,
348 if (do_texture_drawpixels(ctx
, x
, y
, width
, height
, format
, type
,
353 if (INTEL_DEBUG
& DEBUG_PIXEL
)
354 _mesa_printf("%s: fallback to swrast\n", __FUNCTION__
);
356 if (ctx
->FragmentProgram
._Current
== ctx
->FragmentProgram
._TexEnvProgram
) {
358 * We don't want the i915 texenv program to be applied to DrawPixels.
359 * This is really just a performance optimization (mesa will other-
360 * wise happily run the fragment program on each pixel in the image).
362 struct gl_fragment_program
*fpSave
= ctx
->FragmentProgram
._Current
;
363 /* can't just set current frag prog to 0 here as on buffer resize
364 we'll get new state checks which will segfault. Remains a hack. */
365 ctx
->FragmentProgram
._Current
= NULL
;
366 ctx
->FragmentProgram
._UseTexEnvProgram
= GL_FALSE
;
367 ctx
->FragmentProgram
._Active
= GL_FALSE
;
368 _swrast_DrawPixels( ctx
, x
, y
, width
, height
, format
, type
,
370 ctx
->FragmentProgram
._Current
= fpSave
;
371 ctx
->FragmentProgram
._UseTexEnvProgram
= GL_TRUE
;
372 ctx
->FragmentProgram
._Active
= GL_TRUE
;
373 _swrast_InvalidateState(ctx
, _NEW_PROGRAM
);
376 _swrast_DrawPixels( ctx
, x
, y
, width
, height
, format
, type
,