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/teximage.h"
33 #include "main/texenv.h"
34 #include "main/texobj.h"
35 #include "main/texstate.h"
36 #include "main/texparam.h"
37 #include "main/varray.h"
38 #include "main/attrib.h"
39 #include "main/enable.h"
40 #include "main/buffers.h"
41 #include "main/fbobject.h"
42 #include "main/depth.h"
43 #include "main/hash.h"
44 #include "main/blend.h"
45 #include "swrast/swrast.h"
46 #include "drivers/common/meta.h"
48 #include "intel_context.h"
49 #include "intel_buffers.h"
50 #include "intel_pixel.h"
51 #include "intel_fbo.h"
54 /** XXX compare perf of this vs. _mesa_meta_DrawPixels(STENCIL) */
56 intel_stencil_drawpixels(GLcontext
* ctx
,
58 GLsizei width
, GLsizei height
,
61 const struct gl_pixelstore_attrib
*unpack
,
64 struct intel_context
*intel
= intel_context(ctx
);
65 GLuint texname
, rb_name
, fb_name
, old_fb_name
;
66 GLfloat vertices
[4][2];
67 struct intel_renderbuffer
*irb
;
68 struct intel_renderbuffer
*depth_irb
;
69 struct gl_pixelstore_attrib old_unpack
;
70 GLstencil
*stencil_pixels
;
72 GLint old_active_texture
;
73 GLboolean rendering_to_fbo
= ctx
->DrawBuffer
->Name
!= 0;
75 if (format
!= GL_STENCIL_INDEX
)
78 /* If there's nothing to write, we're done. */
79 if (ctx
->Stencil
.WriteMask
[0] == 0)
82 /* Can't do a per-bit writemask while treating stencil as rgba data. */
83 if ((ctx
->Stencil
.WriteMask
[0] & 0xff) != 0xff) {
84 if (INTEL_DEBUG
& DEBUG_FALLBACKS
)
85 fprintf(stderr
, "glDrawPixels(STENCIL_INDEX) fallback: "
86 "stencil mask enabled\n");
90 /* We don't support stencil testing/ops here */
91 if (ctx
->Stencil
._Enabled
)
94 /* We use FBOs for our wrapping of the depthbuffer into a color
97 if (!ctx
->Extensions
.EXT_framebuffer_object
)
100 /* We're going to mess with texturing with no regard to existing texture
101 * state, so if there is some set up we have to bail.
103 if (ctx
->Texture
._EnabledUnits
!= 0) {
104 if (INTEL_DEBUG
& DEBUG_FALLBACKS
)
105 fprintf(stderr
, "glDrawPixels(STENCIL_INDEX) fallback: "
106 "texturing enabled\n");
110 /* Can't do textured DrawPixels with a fragment program, unless we were
111 * to generate a new program that sampled our texture and put the results
112 * in the fragment color before the user's program started.
114 if (ctx
->FragmentProgram
.Enabled
) {
115 if (INTEL_DEBUG
& DEBUG_FALLBACKS
)
116 fprintf(stderr
, "glDrawPixels(STENCIL_INDEX) fallback: "
117 "fragment program enabled\n");
121 /* Check that we can load in a texture this big. */
122 if (width
> (1 << (ctx
->Const
.MaxTextureLevels
- 1)) ||
123 height
> (1 << (ctx
->Const
.MaxTextureLevels
- 1))) {
124 if (INTEL_DEBUG
& DEBUG_FALLBACKS
)
125 fprintf(stderr
, "glDrawPixels(STENCIL_INDEX) fallback: "
126 "bitmap too large (%dx%d)\n",
131 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
132 (!is_power_of_two(width
) || !is_power_of_two(height
))) {
133 if (INTEL_DEBUG
& DEBUG_FALLBACKS
)
135 "glDrawPixels(GL_STENCIL_INDEX) fallback: NPOT texture\n");
139 _mesa_PushAttrib(GL_ENABLE_BIT
| GL_TEXTURE_BIT
|
140 GL_CURRENT_BIT
| GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
141 _mesa_PushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT
);
142 old_fb_name
= ctx
->DrawBuffer
->Name
;
143 old_active_texture
= ctx
->Texture
.CurrentUnit
;
145 _mesa_Disable(GL_POLYGON_STIPPLE
);
146 _mesa_Disable(GL_DEPTH_TEST
);
147 _mesa_Disable(GL_STENCIL_TEST
);
149 /* Unpack the supplied stencil values into a ubyte buffer. */
150 assert(sizeof(GLstencil
) == sizeof(GLubyte
));
151 stencil_pixels
= _mesa_malloc(width
* height
* sizeof(GLstencil
));
152 for (row
= 0; row
< height
; row
++) {
153 GLvoid
*source
= _mesa_image_address2d(unpack
, pixels
,
155 GL_COLOR_INDEX
, type
,
157 _mesa_unpack_stencil_span(ctx
, width
, GL_UNSIGNED_BYTE
,
159 row
* width
* sizeof(GLstencil
),
160 type
, source
, unpack
, ctx
->_ImageTransferState
);
163 /* Take the current depth/stencil renderbuffer, and make a new one wrapping
164 * it which will be treated as GL_RGBA8 so we can render to it as a color
167 depth_irb
= intel_get_renderbuffer(ctx
->DrawBuffer
, BUFFER_DEPTH
);
168 irb
= intel_create_renderbuffer(MESA_FORMAT_ARGB8888
);
169 irb
->Base
.Width
= depth_irb
->Base
.Width
;
170 irb
->Base
.Height
= depth_irb
->Base
.Height
;
171 intel_renderbuffer_set_region(irb
, depth_irb
->region
);
173 /* Create a name for our renderbuffer, which lets us use other mesa
174 * rb functions for convenience.
176 _mesa_GenRenderbuffersEXT(1, &rb_name
);
177 irb
->Base
.RefCount
++;
178 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, rb_name
, &irb
->Base
);
180 /* Bind the new renderbuffer to the color attachment point. */
181 _mesa_GenFramebuffersEXT(1, &fb_name
);
182 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fb_name
);
183 _mesa_FramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT
,
184 GL_COLOR_ATTACHMENT0_EXT
,
187 /* Choose to render to the color attachment. */
188 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT
);
190 _mesa_DepthMask(GL_FALSE
);
191 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_TRUE
);
193 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB
);
194 _mesa_Enable(GL_TEXTURE_2D
);
195 _mesa_GenTextures(1, &texname
);
196 _mesa_BindTexture(GL_TEXTURE_2D
, texname
);
197 _mesa_TexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
198 _mesa_TexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
199 _mesa_TexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
200 old_unpack
= ctx
->Unpack
;
201 ctx
->Unpack
= ctx
->DefaultPacking
;
202 _mesa_TexImage2D(GL_TEXTURE_2D
, 0, GL_INTENSITY
, width
, height
, 0,
203 GL_RED
, GL_UNSIGNED_BYTE
, stencil_pixels
);
204 ctx
->Unpack
= old_unpack
;
205 _mesa_free(stencil_pixels
);
207 meta_set_passthrough_transform(&intel
->meta
);
209 /* Since we're rendering to the framebuffer as if it was an FBO,
210 * if it's the window system we have to flip the coordinates.
212 if (rendering_to_fbo
) {
214 y2
= y
+ height
* ctx
->Pixel
.ZoomY
;
216 y1
= irb
->Base
.Height
- (y
+ height
* ctx
->Pixel
.ZoomY
);
217 y2
= irb
->Base
.Height
- y
;
221 vertices
[1][0] = x
+ width
* ctx
->Pixel
.ZoomX
;
223 vertices
[2][0] = x
+ width
* ctx
->Pixel
.ZoomX
;
228 _mesa_VertexPointer(2, GL_FLOAT
, 2 * sizeof(GLfloat
), &vertices
);
229 _mesa_Enable(GL_VERTEX_ARRAY
);
230 meta_set_default_texrect(&intel
->meta
);
232 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
234 meta_restore_texcoords(&intel
->meta
);
235 meta_restore_transform(&intel
->meta
);
237 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB
+ old_active_texture
);
238 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, old_fb_name
);
240 _mesa_PopClientAttrib();
243 _mesa_DeleteTextures(1, &texname
);
244 _mesa_DeleteFramebuffersEXT(1, &fb_name
);
245 _mesa_DeleteRenderbuffersEXT(1, &rb_name
);
251 intelDrawPixels(GLcontext
* ctx
,
253 GLsizei width
, GLsizei height
,
256 const struct gl_pixelstore_attrib
*unpack
,
257 const GLvoid
* pixels
)
260 /* XXX this function doesn't seem to work reliably even when all
261 * the pre-requisite conditions are met.
262 * Note that this function is never hit with conform.
263 * Fall back to swrast because even the _mesa_meta_DrawPixels() approach
264 * isn't working because of an apparent stencil bug.
266 if (intel_stencil_drawpixels(ctx
, x
, y
, width
, height
, format
, type
,
270 (void) intel_stencil_drawpixels
; /* silence warning */
271 if (format
== GL_STENCIL_INDEX
) {
272 _swrast_DrawPixels(ctx
, x
, y
, width
, height
, format
, type
,
278 _mesa_meta_DrawPixels(ctx
, x
, y
, width
, height
, format
, type
,