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 "main/teximage.h"
35 #include "main/texenv.h"
36 #include "main/texobj.h"
37 #include "main/texstate.h"
38 #include "main/texparam.h"
39 #include "main/varray.h"
40 #include "main/attrib.h"
41 #include "main/enable.h"
42 #include "main/buffers.h"
43 #include "main/fbobject.h"
44 #include "main/renderbuffer.h"
45 #include "main/depth.h"
46 #include "main/hash.h"
47 #include "main/blend.h"
48 #include "glapi/dispatch.h"
49 #include "swrast/swrast.h"
51 #include "intel_screen.h"
52 #include "intel_context.h"
53 #include "intel_batchbuffer.h"
54 #include "intel_blit.h"
55 #include "intel_buffers.h"
56 #include "intel_regions.h"
57 #include "intel_pixel.h"
58 #include "intel_buffer_objects.h"
59 #include "intel_fbo.h"
62 intel_texture_drawpixels(GLcontext
* ctx
,
64 GLsizei width
, GLsizei height
,
67 const struct gl_pixelstore_attrib
*unpack
,
70 struct intel_context
*intel
= intel_context(ctx
);
72 GLfloat vertices
[4][4];
73 GLfloat texcoords
[4][2];
75 GLint old_active_texture
;
76 GLenum internalFormat
;
78 /* We're going to mess with texturing with no regard to existing texture
79 * state, so if there is some set up we have to bail.
81 if (ctx
->Texture
._EnabledUnits
!= 0) {
82 if (INTEL_DEBUG
& DEBUG_FALLBACKS
)
83 fprintf(stderr
, "glDrawPixels() fallback: texturing enabled\n");
87 /* Can't do textured DrawPixels with a fragment program, unless we were
88 * to generate a new program that sampled our texture and put the results
89 * in the fragment color before the user's program started.
91 if (ctx
->FragmentProgram
.Enabled
) {
92 if (INTEL_DEBUG
& DEBUG_FALLBACKS
)
93 fprintf(stderr
, "glDrawPixels() fallback: fragment program enabled\n");
97 /* We don't have a way to generate fragments with stencil values which
98 * will set the resulting stencil value.
100 if (format
== GL_STENCIL_INDEX
)
103 /* Check that we can load in a texture this big. */
104 if (width
> (1 << (ctx
->Const
.MaxTextureLevels
- 1)) ||
105 height
> (1 << (ctx
->Const
.MaxTextureLevels
- 1))) {
106 if (INTEL_DEBUG
& DEBUG_FALLBACKS
)
107 fprintf(stderr
, "glDrawPixels() fallback: bitmap too large (%dx%d)\n",
112 /* To do DEPTH_COMPONENT, we would need to change our setup to not draw to
113 * the color buffer, and sample the texture values into the fragment depth
116 if (format
== GL_DEPTH_COMPONENT
) {
117 if (INTEL_DEBUG
& DEBUG_FALLBACKS
)
119 "glDrawPixels() fallback: format == GL_DEPTH_COMPONENT\n");
123 _mesa_PushAttrib(GL_ENABLE_BIT
| GL_TEXTURE_BIT
|
125 _mesa_PushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT
);
127 /* XXX: pixel store stuff */
128 _mesa_Disable(GL_POLYGON_STIPPLE
);
130 old_active_texture
= ctx
->Texture
.CurrentUnit
;
131 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB
);
132 _mesa_Enable(GL_TEXTURE_2D
);
133 _mesa_GenTextures(1, &texname
);
134 _mesa_BindTexture(GL_TEXTURE_2D
, texname
);
135 _mesa_TexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
136 _mesa_TexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
137 _mesa_TexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
138 if (type
== GL_ALPHA
)
139 internalFormat
= GL_ALPHA
;
141 internalFormat
= GL_RGBA
;
142 _mesa_TexImage2D(GL_TEXTURE_2D
, 0, internalFormat
, width
, height
, 0, format
,
145 intel_meta_set_passthrough_transform(intel
);
147 /* convert rasterpos Z from [0,1] to NDC coord in [-1,1] */
148 z
= -1.0 + 2.0 * ctx
->Current
.RasterPos
[2];
150 /* Create the vertex buffer based on the current raster pos. The x and y
151 * we're handed are ctx->Current.RasterPos[0,1] rounded to integers.
152 * We also apply the depth. However, the W component is already multiplied
153 * into ctx->Current.RasterPos[0,1,2] and we can ignore it at this point.
158 vertices
[0][3] = 1.0;
159 vertices
[1][0] = x
+ width
* ctx
->Pixel
.ZoomX
;
162 vertices
[1][3] = 1.0;
163 vertices
[2][0] = x
+ width
* ctx
->Pixel
.ZoomX
;
164 vertices
[2][1] = y
+ height
* ctx
->Pixel
.ZoomY
;
166 vertices
[2][3] = 1.0;
168 vertices
[3][1] = y
+ height
* ctx
->Pixel
.ZoomY
;
170 vertices
[3][3] = 1.0;
172 texcoords
[0][0] = 0.0;
173 texcoords
[0][1] = 0.0;
174 texcoords
[1][0] = 1.0;
175 texcoords
[1][1] = 0.0;
176 texcoords
[2][0] = 1.0;
177 texcoords
[2][1] = 1.0;
178 texcoords
[3][0] = 0.0;
179 texcoords
[3][1] = 1.0;
181 _mesa_VertexPointer(4, GL_FLOAT
, 4 * sizeof(GLfloat
), &vertices
);
182 _mesa_ClientActiveTextureARB(GL_TEXTURE0
);
183 _mesa_TexCoordPointer(2, GL_FLOAT
, 2 * sizeof(GLfloat
), &texcoords
);
184 _mesa_Enable(GL_VERTEX_ARRAY
);
185 _mesa_Enable(GL_TEXTURE_COORD_ARRAY
);
186 CALL_DrawArrays(ctx
->Exec
, (GL_TRIANGLE_FAN
, 0, 4));
188 intel_meta_restore_transform(intel
);
190 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB
+ old_active_texture
);
191 _mesa_PopClientAttrib();
194 _mesa_DeleteTextures(1, &texname
);
200 intel_stencil_drawpixels(GLcontext
* ctx
,
202 GLsizei width
, GLsizei height
,
205 const struct gl_pixelstore_attrib
*unpack
,
206 const GLvoid
*pixels
)
208 struct intel_context
*intel
= intel_context(ctx
);
209 GLuint texname
, rb_name
, fb_name
, old_fb_name
;
210 GLfloat vertices
[4][2];
211 GLfloat texcoords
[4][2];
212 struct intel_renderbuffer
*irb
;
213 struct intel_renderbuffer
*depth_irb
;
214 struct gl_renderbuffer
*rb
;
215 struct gl_pixelstore_attrib old_unpack
;
216 GLstencil
*stencil_pixels
;
218 GLint old_active_texture
;
220 if (format
!= GL_STENCIL_INDEX
)
223 /* If there's nothing to write, we're done. */
224 if (ctx
->Stencil
.WriteMask
[0] == 0)
227 /* Can't do a per-bit writemask while treating stencil as rgba data. */
228 if ((ctx
->Stencil
.WriteMask
[0] & 0xff) != 0xff) {
229 if (INTEL_DEBUG
& DEBUG_FALLBACKS
)
230 fprintf(stderr
, "glDrawPixels(STENCIL_INDEX) fallback: "
231 "stencil mask enabled\n");
235 /* We don't support stencil testing/ops here */
236 if (ctx
->Stencil
._Enabled
)
239 /* We use FBOs for our wrapping of the depthbuffer into a color
242 if (!ctx
->Extensions
.EXT_framebuffer_object
)
245 /* We're going to mess with texturing with no regard to existing texture
246 * state, so if there is some set up we have to bail.
248 if (ctx
->Texture
._EnabledUnits
!= 0) {
249 if (INTEL_DEBUG
& DEBUG_FALLBACKS
)
250 fprintf(stderr
, "glDrawPixels(STENCIL_INDEX) fallback: "
251 "texturing enabled\n");
255 /* Can't do textured DrawPixels with a fragment program, unless we were
256 * to generate a new program that sampled our texture and put the results
257 * in the fragment color before the user's program started.
259 if (ctx
->FragmentProgram
.Enabled
) {
260 if (INTEL_DEBUG
& DEBUG_FALLBACKS
)
261 fprintf(stderr
, "glDrawPixels(STENCIL_INDEX) fallback: "
262 "fragment program enabled\n");
266 /* Check that we can load in a texture this big. */
267 if (width
> (1 << (ctx
->Const
.MaxTextureLevels
- 1)) ||
268 height
> (1 << (ctx
->Const
.MaxTextureLevels
- 1))) {
269 if (INTEL_DEBUG
& DEBUG_FALLBACKS
)
270 fprintf(stderr
, "glDrawPixels(STENCIL_INDEX) fallback: "
271 "bitmap too large (%dx%d)\n",
276 _mesa_PushAttrib(GL_ENABLE_BIT
| GL_TEXTURE_BIT
|
277 GL_CURRENT_BIT
| GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
278 _mesa_PushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT
);
279 old_fb_name
= ctx
->DrawBuffer
->Name
;
280 old_active_texture
= ctx
->Texture
.CurrentUnit
;
282 _mesa_Disable(GL_POLYGON_STIPPLE
);
283 _mesa_Disable(GL_DEPTH_TEST
);
284 _mesa_Disable(GL_STENCIL_TEST
);
286 /* Unpack the supplied stencil values into a ubyte buffer. */
287 assert(sizeof(GLstencil
) == sizeof(GLubyte
));
288 stencil_pixels
= _mesa_malloc(width
* height
* sizeof(GLstencil
));
289 for (row
= 0; row
< height
; row
++) {
290 GLvoid
*source
= _mesa_image_address2d(unpack
, pixels
,
292 GL_COLOR_INDEX
, type
,
294 _mesa_unpack_stencil_span(ctx
, width
, GL_UNSIGNED_BYTE
,
296 row
* width
* sizeof(GLstencil
),
297 type
, source
, unpack
, ctx
->_ImageTransferState
);
300 /* Take the current depth/stencil renderbuffer, and make a new one wrapping
301 * it which will be treated as GL_RGBA8 so we can render to it as a color
304 depth_irb
= intel_get_renderbuffer(ctx
->DrawBuffer
, BUFFER_DEPTH
);
305 irb
= intel_create_renderbuffer(GL_RGBA8
);
307 irb
->Base
.Width
= depth_irb
->Base
.Width
;
308 irb
->Base
.Height
= depth_irb
->Base
.Height
;
309 intel_renderbuffer_set_region(irb
, depth_irb
->region
);
311 /* Create a name for our renderbuffer, which lets us use other mesa
312 * rb functions for convenience.
314 _mesa_GenRenderbuffersEXT(1, &rb_name
);
315 irb
->Base
.RefCount
++;
316 _mesa_HashInsert(ctx
->Shared
->RenderBuffers
, rb_name
, &irb
->Base
);
318 /* Bind the new renderbuffer to the color attachment point. */
319 _mesa_GenFramebuffersEXT(1, &fb_name
);
320 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fb_name
);
321 _mesa_FramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT
,
322 GL_COLOR_ATTACHMENT0_EXT
,
325 /* Choose to render to the color attachment. */
326 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT
);
328 _mesa_DepthMask(GL_FALSE
);
329 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_TRUE
);
331 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB
);
332 _mesa_Enable(GL_TEXTURE_2D
);
333 _mesa_GenTextures(1, &texname
);
334 _mesa_BindTexture(GL_TEXTURE_2D
, texname
);
335 _mesa_TexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
336 _mesa_TexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
337 _mesa_TexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
338 old_unpack
= ctx
->Unpack
;
339 ctx
->Unpack
= ctx
->DefaultPacking
;
340 _mesa_TexImage2D(GL_TEXTURE_2D
, 0, GL_INTENSITY
, width
, height
, 0,
341 GL_RED
, GL_UNSIGNED_BYTE
, stencil_pixels
);
342 ctx
->Unpack
= old_unpack
;
343 _mesa_free(stencil_pixels
);
345 intel_meta_set_passthrough_transform(intel
);
349 vertices
[1][0] = x
+ width
* ctx
->Pixel
.ZoomX
;
351 vertices
[2][0] = x
+ width
* ctx
->Pixel
.ZoomX
;
352 vertices
[2][1] = y
+ height
* ctx
->Pixel
.ZoomY
;
354 vertices
[3][1] = y
+ height
* ctx
->Pixel
.ZoomY
;
356 texcoords
[0][0] = 0.0;
357 texcoords
[0][1] = 0.0;
358 texcoords
[1][0] = 1.0;
359 texcoords
[1][1] = 0.0;
360 texcoords
[2][0] = 1.0;
361 texcoords
[2][1] = 1.0;
362 texcoords
[3][0] = 0.0;
363 texcoords
[3][1] = 1.0;
365 _mesa_VertexPointer(2, GL_FLOAT
, 2 * sizeof(GLfloat
), &vertices
);
366 _mesa_ClientActiveTextureARB(GL_TEXTURE0
);
367 _mesa_TexCoordPointer(2, GL_FLOAT
, 2 * sizeof(GLfloat
), &texcoords
);
368 _mesa_Enable(GL_VERTEX_ARRAY
);
369 _mesa_Enable(GL_TEXTURE_COORD_ARRAY
);
370 CALL_DrawArrays(ctx
->Exec
, (GL_TRIANGLE_FAN
, 0, 4));
372 intel_meta_restore_transform(intel
);
374 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB
+ old_active_texture
);
375 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, old_fb_name
);
377 _mesa_PopClientAttrib();
380 _mesa_DeleteTextures(1, &texname
);
381 _mesa_DeleteFramebuffersEXT(1, &fb_name
);
382 _mesa_DeleteRenderbuffersEXT(1, &rb_name
);
388 intelDrawPixels(GLcontext
* ctx
,
390 GLsizei width
, GLsizei height
,
393 const struct gl_pixelstore_attrib
*unpack
,
394 const GLvoid
* pixels
)
396 if (intel_texture_drawpixels(ctx
, x
, y
, width
, height
, format
, type
,
400 if (intel_stencil_drawpixels(ctx
, x
, y
, width
, height
, format
, type
,
404 if (INTEL_DEBUG
& DEBUG_PIXEL
)
405 _mesa_printf("%s: fallback to swrast\n", __FUNCTION__
);
407 _swrast_DrawPixels(ctx
, x
, y
, width
, height
, format
, type
,