intel: intel_texture_drawpixels() can't handle GL_DEPTH_STENCIL.
[mesa.git] / src / mesa / drivers / dri / intel / intel_pixel_draw.c
1 /**************************************************************************
2 *
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portionsalloc
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
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"
50
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"
60
61 static GLboolean
62 intel_texture_drawpixels(GLcontext * ctx,
63 GLint x, GLint y,
64 GLsizei width, GLsizei height,
65 GLenum format,
66 GLenum type,
67 const struct gl_pixelstore_attrib *unpack,
68 const GLvoid *pixels)
69 {
70 struct intel_context *intel = intel_context(ctx);
71 GLuint texname;
72 GLfloat vertices[4][4];
73 GLfloat texcoords[4][2];
74 GLfloat z;
75 GLint old_active_texture;
76 GLenum internalFormat;
77
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.
80 */
81 if (ctx->Texture._EnabledUnits != 0) {
82 if (INTEL_DEBUG & DEBUG_FALLBACKS)
83 fprintf(stderr, "glDrawPixels() fallback: texturing enabled\n");
84 return GL_FALSE;
85 }
86
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.
90 */
91 if (ctx->FragmentProgram.Enabled) {
92 if (INTEL_DEBUG & DEBUG_FALLBACKS)
93 fprintf(stderr, "glDrawPixels() fallback: fragment program enabled\n");
94 return GL_FALSE;
95 }
96
97 /* We don't have a way to generate fragments with stencil values which
98 * will set the resulting stencil value.
99 */
100 if (format == GL_STENCIL_INDEX || format == GL_DEPTH_STENCIL)
101 return GL_FALSE;
102
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",
108 width, height);
109 return GL_FALSE;
110 }
111
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
114 * in a program.
115 */
116 if (format == GL_DEPTH_COMPONENT) {
117 if (INTEL_DEBUG & DEBUG_FALLBACKS)
118 fprintf(stderr,
119 "glDrawPixels() fallback: format == GL_DEPTH_COMPONENT\n");
120 return GL_FALSE;
121 }
122
123 if (!ctx->Extensions.ARB_texture_non_power_of_two &&
124 (!is_power_of_two(width) || !is_power_of_two(height))) {
125 if (INTEL_DEBUG & DEBUG_FALLBACKS)
126 fprintf(stderr,
127 "glDrawPixels() fallback: NPOT texture\n");
128 return GL_FALSE;
129 }
130
131 _mesa_PushAttrib(GL_ENABLE_BIT | GL_TEXTURE_BIT |
132 GL_CURRENT_BIT);
133 _mesa_PushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT);
134
135 /* XXX: pixel store stuff */
136 _mesa_Disable(GL_POLYGON_STIPPLE);
137
138 old_active_texture = ctx->Texture.CurrentUnit;
139 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB);
140 _mesa_Enable(GL_TEXTURE_2D);
141 _mesa_GenTextures(1, &texname);
142 _mesa_BindTexture(GL_TEXTURE_2D, texname);
143 _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
144 _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
145 _mesa_TexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
146 if (type == GL_ALPHA)
147 internalFormat = GL_ALPHA;
148 else
149 internalFormat = GL_RGBA;
150 _mesa_TexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, format,
151 type, pixels);
152
153 intel_meta_set_passthrough_transform(intel);
154
155 /* convert rasterpos Z from [0,1] to NDC coord in [-1,1] */
156 z = -1.0 + 2.0 * ctx->Current.RasterPos[2];
157
158 /* Create the vertex buffer based on the current raster pos. The x and y
159 * we're handed are ctx->Current.RasterPos[0,1] rounded to integers.
160 * We also apply the depth. However, the W component is already multiplied
161 * into ctx->Current.RasterPos[0,1,2] and we can ignore it at this point.
162 */
163 vertices[0][0] = x;
164 vertices[0][1] = y;
165 vertices[0][2] = z;
166 vertices[0][3] = 1.0;
167 vertices[1][0] = x + width * ctx->Pixel.ZoomX;
168 vertices[1][1] = y;
169 vertices[1][2] = z;
170 vertices[1][3] = 1.0;
171 vertices[2][0] = x + width * ctx->Pixel.ZoomX;
172 vertices[2][1] = y + height * ctx->Pixel.ZoomY;
173 vertices[2][2] = z;
174 vertices[2][3] = 1.0;
175 vertices[3][0] = x;
176 vertices[3][1] = y + height * ctx->Pixel.ZoomY;
177 vertices[3][2] = z;
178 vertices[3][3] = 1.0;
179
180 texcoords[0][0] = 0.0;
181 texcoords[0][1] = 0.0;
182 texcoords[1][0] = 1.0;
183 texcoords[1][1] = 0.0;
184 texcoords[2][0] = 1.0;
185 texcoords[2][1] = 1.0;
186 texcoords[3][0] = 0.0;
187 texcoords[3][1] = 1.0;
188
189 _mesa_VertexPointer(4, GL_FLOAT, 4 * sizeof(GLfloat), &vertices);
190 _mesa_ClientActiveTextureARB(GL_TEXTURE0);
191 _mesa_TexCoordPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), &texcoords);
192 _mesa_Enable(GL_VERTEX_ARRAY);
193 _mesa_Enable(GL_TEXTURE_COORD_ARRAY);
194 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
195
196 intel_meta_restore_transform(intel);
197
198 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB + old_active_texture);
199 _mesa_PopClientAttrib();
200 _mesa_PopAttrib();
201
202 _mesa_DeleteTextures(1, &texname);
203
204 return GL_TRUE;
205 }
206
207 static GLboolean
208 intel_stencil_drawpixels(GLcontext * ctx,
209 GLint x, GLint y,
210 GLsizei width, GLsizei height,
211 GLenum format,
212 GLenum type,
213 const struct gl_pixelstore_attrib *unpack,
214 const GLvoid *pixels)
215 {
216 struct intel_context *intel = intel_context(ctx);
217 GLuint texname, rb_name, fb_name, old_fb_name;
218 GLfloat vertices[4][2];
219 GLfloat texcoords[4][2];
220 struct intel_renderbuffer *irb;
221 struct intel_renderbuffer *depth_irb;
222 struct gl_renderbuffer *rb;
223 struct gl_pixelstore_attrib old_unpack;
224 GLstencil *stencil_pixels;
225 int row;
226 GLint old_active_texture;
227
228 if (format != GL_STENCIL_INDEX)
229 return GL_FALSE;
230
231 /* If there's nothing to write, we're done. */
232 if (ctx->Stencil.WriteMask[0] == 0)
233 return GL_TRUE;
234
235 /* Can't do a per-bit writemask while treating stencil as rgba data. */
236 if ((ctx->Stencil.WriteMask[0] & 0xff) != 0xff) {
237 if (INTEL_DEBUG & DEBUG_FALLBACKS)
238 fprintf(stderr, "glDrawPixels(STENCIL_INDEX) fallback: "
239 "stencil mask enabled\n");
240 return GL_FALSE;
241 }
242
243 /* We don't support stencil testing/ops here */
244 if (ctx->Stencil._Enabled)
245 return GL_FALSE;
246
247 /* We use FBOs for our wrapping of the depthbuffer into a color
248 * destination.
249 */
250 if (!ctx->Extensions.EXT_framebuffer_object)
251 return GL_FALSE;
252
253 /* We're going to mess with texturing with no regard to existing texture
254 * state, so if there is some set up we have to bail.
255 */
256 if (ctx->Texture._EnabledUnits != 0) {
257 if (INTEL_DEBUG & DEBUG_FALLBACKS)
258 fprintf(stderr, "glDrawPixels(STENCIL_INDEX) fallback: "
259 "texturing enabled\n");
260 return GL_FALSE;
261 }
262
263 /* Can't do textured DrawPixels with a fragment program, unless we were
264 * to generate a new program that sampled our texture and put the results
265 * in the fragment color before the user's program started.
266 */
267 if (ctx->FragmentProgram.Enabled) {
268 if (INTEL_DEBUG & DEBUG_FALLBACKS)
269 fprintf(stderr, "glDrawPixels(STENCIL_INDEX) fallback: "
270 "fragment program enabled\n");
271 return GL_FALSE;
272 }
273
274 /* Check that we can load in a texture this big. */
275 if (width > (1 << (ctx->Const.MaxTextureLevels - 1)) ||
276 height > (1 << (ctx->Const.MaxTextureLevels - 1))) {
277 if (INTEL_DEBUG & DEBUG_FALLBACKS)
278 fprintf(stderr, "glDrawPixels(STENCIL_INDEX) fallback: "
279 "bitmap too large (%dx%d)\n",
280 width, height);
281 return GL_FALSE;
282 }
283
284 if (!ctx->Extensions.ARB_texture_non_power_of_two &&
285 (!is_power_of_two(width) || !is_power_of_two(height))) {
286 if (INTEL_DEBUG & DEBUG_FALLBACKS)
287 fprintf(stderr,
288 "glDrawPixels(GL_STENCIL_INDEX) fallback: NPOT texture\n");
289 return GL_FALSE;
290 }
291
292 _mesa_PushAttrib(GL_ENABLE_BIT | GL_TEXTURE_BIT |
293 GL_CURRENT_BIT | GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
294 _mesa_PushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT);
295 old_fb_name = ctx->DrawBuffer->Name;
296 old_active_texture = ctx->Texture.CurrentUnit;
297
298 _mesa_Disable(GL_POLYGON_STIPPLE);
299 _mesa_Disable(GL_DEPTH_TEST);
300 _mesa_Disable(GL_STENCIL_TEST);
301
302 /* Unpack the supplied stencil values into a ubyte buffer. */
303 assert(sizeof(GLstencil) == sizeof(GLubyte));
304 stencil_pixels = _mesa_malloc(width * height * sizeof(GLstencil));
305 for (row = 0; row < height; row++) {
306 GLvoid *source = _mesa_image_address2d(unpack, pixels,
307 width, height,
308 GL_COLOR_INDEX, type,
309 row, 0);
310 _mesa_unpack_stencil_span(ctx, width, GL_UNSIGNED_BYTE,
311 stencil_pixels +
312 row * width * sizeof(GLstencil),
313 type, source, unpack, ctx->_ImageTransferState);
314 }
315
316 /* Take the current depth/stencil renderbuffer, and make a new one wrapping
317 * it which will be treated as GL_RGBA8 so we can render to it as a color
318 * buffer.
319 */
320 depth_irb = intel_get_renderbuffer(ctx->DrawBuffer, BUFFER_DEPTH);
321 irb = intel_create_renderbuffer(GL_RGBA8);
322 rb = &irb->Base;
323 irb->Base.Width = depth_irb->Base.Width;
324 irb->Base.Height = depth_irb->Base.Height;
325 intel_renderbuffer_set_region(irb, depth_irb->region);
326
327 /* Create a name for our renderbuffer, which lets us use other mesa
328 * rb functions for convenience.
329 */
330 _mesa_GenRenderbuffersEXT(1, &rb_name);
331 irb->Base.RefCount++;
332 _mesa_HashInsert(ctx->Shared->RenderBuffers, rb_name, &irb->Base);
333
334 /* Bind the new renderbuffer to the color attachment point. */
335 _mesa_GenFramebuffersEXT(1, &fb_name);
336 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb_name);
337 _mesa_FramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,
338 GL_COLOR_ATTACHMENT0_EXT,
339 GL_RENDERBUFFER_EXT,
340 rb_name);
341 /* Choose to render to the color attachment. */
342 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
343
344 _mesa_DepthMask(GL_FALSE);
345 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE);
346
347 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB);
348 _mesa_Enable(GL_TEXTURE_2D);
349 _mesa_GenTextures(1, &texname);
350 _mesa_BindTexture(GL_TEXTURE_2D, texname);
351 _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
352 _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
353 _mesa_TexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
354 old_unpack = ctx->Unpack;
355 ctx->Unpack = ctx->DefaultPacking;
356 _mesa_TexImage2D(GL_TEXTURE_2D, 0, GL_INTENSITY, width, height, 0,
357 GL_RED, GL_UNSIGNED_BYTE, stencil_pixels);
358 ctx->Unpack = old_unpack;
359 _mesa_free(stencil_pixels);
360
361 intel_meta_set_passthrough_transform(intel);
362
363 vertices[0][0] = x;
364 vertices[0][1] = y;
365 vertices[1][0] = x + width * ctx->Pixel.ZoomX;
366 vertices[1][1] = y;
367 vertices[2][0] = x + width * ctx->Pixel.ZoomX;
368 vertices[2][1] = y + height * ctx->Pixel.ZoomY;
369 vertices[3][0] = x;
370 vertices[3][1] = y + height * ctx->Pixel.ZoomY;
371
372 texcoords[0][0] = 0.0;
373 texcoords[0][1] = 0.0;
374 texcoords[1][0] = 1.0;
375 texcoords[1][1] = 0.0;
376 texcoords[2][0] = 1.0;
377 texcoords[2][1] = 1.0;
378 texcoords[3][0] = 0.0;
379 texcoords[3][1] = 1.0;
380
381 _mesa_VertexPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), &vertices);
382 _mesa_ClientActiveTextureARB(GL_TEXTURE0);
383 _mesa_TexCoordPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), &texcoords);
384 _mesa_Enable(GL_VERTEX_ARRAY);
385 _mesa_Enable(GL_TEXTURE_COORD_ARRAY);
386 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
387
388 intel_meta_restore_transform(intel);
389
390 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB + old_active_texture);
391 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, old_fb_name);
392
393 _mesa_PopClientAttrib();
394 _mesa_PopAttrib();
395
396 _mesa_DeleteTextures(1, &texname);
397 _mesa_DeleteFramebuffersEXT(1, &fb_name);
398 _mesa_DeleteRenderbuffersEXT(1, &rb_name);
399
400 return GL_TRUE;
401 }
402
403 void
404 intelDrawPixels(GLcontext * ctx,
405 GLint x, GLint y,
406 GLsizei width, GLsizei height,
407 GLenum format,
408 GLenum type,
409 const struct gl_pixelstore_attrib *unpack,
410 const GLvoid * pixels)
411 {
412 if (intel_texture_drawpixels(ctx, x, y, width, height, format, type,
413 unpack, pixels))
414 return;
415
416 if (intel_stencil_drawpixels(ctx, x, y, width, height, format, type,
417 unpack, pixels))
418 return;
419
420 if (INTEL_DEBUG & DEBUG_PIXEL)
421 _mesa_printf("%s: fallback to swrast\n", __FUNCTION__);
422
423 _swrast_DrawPixels(ctx, x, y, width, height, format, type,
424 unpack, pixels);
425 }