intel: Remove fallback for glDrawPixels(GL_COLOR_INDEX)
[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/matrix.h"
40 #include "main/varray.h"
41 #include "main/attrib.h"
42 #include "main/enable.h"
43 #include "main/buffers.h"
44 #include "main/fbobject.h"
45 #include "main/renderbuffer.h"
46 #include "main/depth.h"
47 #include "main/hash.h"
48 #include "main/blend.h"
49 #include "glapi/dispatch.h"
50 #include "swrast/swrast.h"
51
52 #include "intel_screen.h"
53 #include "intel_context.h"
54 #include "intel_batchbuffer.h"
55 #include "intel_blit.h"
56 #include "intel_buffers.h"
57 #include "intel_regions.h"
58 #include "intel_pixel.h"
59 #include "intel_buffer_objects.h"
60 #include "intel_fbo.h"
61
62 static GLboolean
63 intel_texture_drawpixels(GLcontext * ctx,
64 GLint x, GLint y,
65 GLsizei width, GLsizei height,
66 GLenum format,
67 GLenum type,
68 const struct gl_pixelstore_attrib *unpack,
69 const GLvoid *pixels)
70 {
71 GLuint texname;
72 GLfloat vertices[4][4];
73 GLfloat texcoords[4][2];
74
75 /* We're going to mess with texturing with no regard to existing texture
76 * state, so if there is some set up we have to bail.
77 */
78 if (ctx->Texture._EnabledUnits != 0) {
79 if (INTEL_DEBUG & DEBUG_FALLBACKS)
80 fprintf(stderr, "glDrawPixels() fallback: texturing enabled\n");
81 return GL_FALSE;
82 }
83
84 /* Can't do textured DrawPixels with a fragment program, unless we were
85 * to generate a new program that sampled our texture and put the results
86 * in the fragment color before the user's program started.
87 */
88 if (ctx->FragmentProgram.Enabled) {
89 if (INTEL_DEBUG & DEBUG_FALLBACKS)
90 fprintf(stderr, "glDrawPixels() fallback: fragment program enabled\n");
91 return GL_FALSE;
92 }
93
94 /* We don't have a way to generate fragments with stencil values which *
95 * will set the resulting stencil value.
96 */
97 if (format == GL_STENCIL_INDEX)
98 return GL_FALSE;
99
100 /* Check that we can load in a texture this big. */
101 if (width > (1 << (ctx->Const.MaxTextureLevels - 1)) ||
102 height > (1 << (ctx->Const.MaxTextureLevels - 1))) {
103 if (INTEL_DEBUG & DEBUG_FALLBACKS)
104 fprintf(stderr, "glDrawPixels() fallback: bitmap too large (%dx%d)\n",
105 width, height);
106 return GL_FALSE;
107 }
108
109 /* To do DEPTH_COMPONENT, we would need to change our setup to not draw to
110 * the color buffer, and sample the texture values into the fragment depth
111 * in a program.
112 */
113 if (format == GL_DEPTH_COMPONENT) {
114 if (INTEL_DEBUG & DEBUG_FALLBACKS)
115 fprintf(stderr,
116 "glDrawPixels() fallback: format == GL_DEPTH_COMPONENT\n");
117 return GL_FALSE;
118 }
119
120 _mesa_PushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT | GL_TEXTURE_BIT |
121 GL_CURRENT_BIT);
122 _mesa_PushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT);
123
124 /* XXX: pixel store stuff */
125 _mesa_Disable(GL_POLYGON_STIPPLE);
126
127 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB);
128 _mesa_Enable(GL_TEXTURE_2D);
129 _mesa_GenTextures(1, &texname);
130 _mesa_BindTexture(GL_TEXTURE_2D, texname);
131 _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
132 _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
133 _mesa_TexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
134 /*
135 _mesa_TexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE);
136 _mesa_TexEnvf(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE);
137 */
138 _mesa_TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, format,
139 type, pixels);
140
141 _mesa_MatrixMode(GL_PROJECTION);
142 _mesa_PushMatrix();
143 _mesa_LoadIdentity();
144 _mesa_Ortho(0, ctx->DrawBuffer->Width, 0, ctx->DrawBuffer->Height, 1, -1);
145
146 _mesa_MatrixMode(GL_MODELVIEW);
147 _mesa_PushMatrix();
148 _mesa_LoadIdentity();
149
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.
154 */
155 vertices[0][0] = x;
156 vertices[0][1] = y;
157 vertices[0][2] = ctx->Current.RasterPos[2];
158 vertices[0][3] = 1.0;
159 vertices[1][0] = x + width * ctx->Pixel.ZoomX;
160 vertices[1][1] = y;
161 vertices[1][2] = ctx->Current.RasterPos[2];
162 vertices[1][3] = 1.0;
163 vertices[2][0] = x + width * ctx->Pixel.ZoomX;
164 vertices[2][1] = y + height * ctx->Pixel.ZoomY;
165 vertices[2][2] = ctx->Current.RasterPos[2];
166 vertices[2][3] = 1.0;
167 vertices[3][0] = x;
168 vertices[3][1] = y + height * ctx->Pixel.ZoomY;
169 vertices[3][2] = ctx->Current.RasterPos[2];
170 vertices[3][3] = 1.0;
171
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;
180
181 _mesa_VertexPointer(4, GL_FLOAT, 4 * sizeof(GLfloat), &vertices);
182 _mesa_TexCoordPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), &texcoords);
183 _mesa_Enable(GL_VERTEX_ARRAY);
184 _mesa_Enable(GL_TEXTURE_COORD_ARRAY);
185 CALL_DrawArrays(ctx->Exec, (GL_TRIANGLE_FAN, 0, 4));
186
187 _mesa_MatrixMode(GL_PROJECTION);
188 _mesa_PopMatrix();
189 _mesa_MatrixMode(GL_MODELVIEW);
190 _mesa_PopMatrix();
191 _mesa_PopClientAttrib();
192 _mesa_PopAttrib();
193
194 _mesa_DeleteTextures(1, &texname);
195
196 return GL_TRUE;
197 }
198
199 static GLboolean
200 intel_stencil_drawpixels(GLcontext * ctx,
201 GLint x, GLint y,
202 GLsizei width, GLsizei height,
203 GLenum format,
204 GLenum type,
205 const struct gl_pixelstore_attrib *unpack,
206 const GLvoid *pixels)
207 {
208 GLuint texname, rb_name, fb_name, old_fb_name;
209 GLfloat vertices[4][2];
210 GLfloat texcoords[4][2];
211 struct intel_renderbuffer *irb;
212 struct intel_renderbuffer *depth_irb;
213 struct gl_renderbuffer *rb;
214 struct gl_pixelstore_attrib old_unpack;
215 GLstencil *stencil_pixels;
216 int row;
217
218 if (format != GL_STENCIL_INDEX)
219 return GL_FALSE;
220
221 /* If there's nothing to write, we're done. */
222 if (ctx->Stencil.WriteMask[0] == 0)
223 return GL_TRUE;
224
225 /* Can't do a per-bit writemask while treating stencil as rgba data. */
226 if ((ctx->Stencil.WriteMask[0] & 0xff) != 0xff) {
227 if (INTEL_DEBUG & DEBUG_FALLBACKS)
228 fprintf(stderr, "glDrawPixels(STENCIL_INDEX) fallback: "
229 "stencil mask enabled\n");
230 return GL_FALSE;
231 }
232
233 /* We use FBOs for our wrapping of the depthbuffer into a color
234 * destination.
235 */
236 if (!ctx->Extensions.EXT_framebuffer_object)
237 return GL_FALSE;
238
239 /* We're going to mess with texturing with no regard to existing texture
240 * state, so if there is some set up we have to bail.
241 */
242 if (ctx->Texture._EnabledUnits != 0) {
243 if (INTEL_DEBUG & DEBUG_FALLBACKS)
244 fprintf(stderr, "glDrawPixels(STENCIL_INDEX) fallback: "
245 "texturing enabled\n");
246 return GL_FALSE;
247 }
248
249 /* Can't do textured DrawPixels with a fragment program, unless we were
250 * to generate a new program that sampled our texture and put the results
251 * in the fragment color before the user's program started.
252 */
253 if (ctx->FragmentProgram.Enabled) {
254 if (INTEL_DEBUG & DEBUG_FALLBACKS)
255 fprintf(stderr, "glDrawPixels(STENCIL_INDEX) fallback: "
256 "fragment program enabled\n");
257 return GL_FALSE;
258 }
259
260 /* Check that we can load in a texture this big. */
261 if (width > (1 << (ctx->Const.MaxTextureLevels - 1)) ||
262 height > (1 << (ctx->Const.MaxTextureLevels - 1))) {
263 if (INTEL_DEBUG & DEBUG_FALLBACKS)
264 fprintf(stderr, "glDrawPixels(STENCIL_INDEX) fallback: "
265 "bitmap too large (%dx%d)\n",
266 width, height);
267 return GL_FALSE;
268 }
269
270 _mesa_PushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT | GL_TEXTURE_BIT |
271 GL_CURRENT_BIT | GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
272 _mesa_PushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT);
273 old_fb_name = ctx->DrawBuffer->Name;
274
275 _mesa_Disable(GL_POLYGON_STIPPLE);
276 _mesa_Disable(GL_DEPTH_TEST);
277 _mesa_Disable(GL_STENCIL_TEST);
278
279 /* Unpack the supplied stencil values into a ubyte buffer. */
280 assert(sizeof(GLstencil) == sizeof(GLubyte));
281 stencil_pixels = _mesa_malloc(width * height * sizeof(GLstencil));
282 for (row = 0; row < height; row++) {
283 GLvoid *source = _mesa_image_address2d(unpack, pixels,
284 width, height,
285 GL_COLOR_INDEX, type,
286 row, 0);
287 _mesa_unpack_stencil_span(ctx, width, GL_UNSIGNED_BYTE,
288 stencil_pixels +
289 row * width * sizeof(GLstencil),
290 type, source, unpack, ctx->_ImageTransferState);
291 }
292
293 /* Take the current depth/stencil renderbuffer, and make a new one wrapping
294 * it which will be treated as GL_RGBA8 so we can render to it as a color
295 * buffer.
296 */
297 depth_irb = intel_get_renderbuffer(ctx->DrawBuffer, BUFFER_DEPTH);
298 irb = intel_create_renderbuffer(GL_RGBA8);
299 rb = &irb->Base;
300 irb->Base.Width = depth_irb->Base.Width;
301 irb->Base.Height = depth_irb->Base.Height;
302 intel_renderbuffer_set_region(irb, depth_irb->region);
303
304 /* Create a name for our renderbuffer, which lets us use other mesa
305 * rb functions for convenience.
306 */
307 _mesa_GenRenderbuffersEXT(1, &rb_name);
308 irb->Base.RefCount++;
309 _mesa_HashInsert(ctx->Shared->RenderBuffers, rb_name, &irb->Base);
310
311 /* Bind the new renderbuffer to the color attachment point. */
312 _mesa_GenFramebuffersEXT(1, &fb_name);
313 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb_name);
314 _mesa_FramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,
315 GL_COLOR_ATTACHMENT0_EXT,
316 GL_RENDERBUFFER_EXT,
317 rb_name);
318 /* Choose to render to the color attachment. */
319 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
320
321 _mesa_DepthMask(GL_FALSE);
322 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE);
323
324 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB);
325 _mesa_Enable(GL_TEXTURE_2D);
326 _mesa_GenTextures(1, &texname);
327 _mesa_BindTexture(GL_TEXTURE_2D, texname);
328 _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
329 _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
330 _mesa_TexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
331 old_unpack = ctx->Unpack;
332 ctx->Unpack = ctx->DefaultPacking;
333 _mesa_TexImage2D(GL_TEXTURE_2D, 0, GL_INTENSITY, width, height, 0,
334 GL_RED, GL_UNSIGNED_BYTE, stencil_pixels);
335 ctx->Unpack = old_unpack;
336 _mesa_free(stencil_pixels);
337
338 _mesa_MatrixMode(GL_PROJECTION);
339 _mesa_PushMatrix();
340 _mesa_LoadIdentity();
341 _mesa_Ortho(0, ctx->DrawBuffer->Width, 0, ctx->DrawBuffer->Height, 1, -1);
342
343 _mesa_MatrixMode(GL_MODELVIEW);
344 _mesa_PushMatrix();
345 _mesa_LoadIdentity();
346
347 vertices[0][0] = x;
348 vertices[0][1] = y;
349 vertices[1][0] = x + width * ctx->Pixel.ZoomX;
350 vertices[1][1] = y;
351 vertices[2][0] = x + width * ctx->Pixel.ZoomX;
352 vertices[2][1] = y + height * ctx->Pixel.ZoomY;
353 vertices[3][0] = x;
354 vertices[3][1] = y + height * ctx->Pixel.ZoomY;
355
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;
364
365 _mesa_VertexPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), &vertices);
366 _mesa_TexCoordPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), &texcoords);
367 _mesa_Enable(GL_VERTEX_ARRAY);
368 _mesa_Enable(GL_TEXTURE_COORD_ARRAY);
369 CALL_DrawArrays(ctx->Exec, (GL_TRIANGLE_FAN, 0, 4));
370
371 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, old_fb_name);
372
373 _mesa_MatrixMode(GL_PROJECTION);
374 _mesa_PopMatrix();
375 _mesa_MatrixMode(GL_MODELVIEW);
376 _mesa_PopMatrix();
377 _mesa_PopClientAttrib();
378 _mesa_PopAttrib();
379
380 _mesa_DeleteTextures(1, &texname);
381 _mesa_DeleteFramebuffersEXT(1, &fb_name);
382 _mesa_DeleteRenderbuffersEXT(1, &rb_name);
383
384 return GL_TRUE;
385 }
386
387 void
388 intelDrawPixels(GLcontext * ctx,
389 GLint x, GLint y,
390 GLsizei width, GLsizei height,
391 GLenum format,
392 GLenum type,
393 const struct gl_pixelstore_attrib *unpack,
394 const GLvoid * pixels)
395 {
396 if (intel_texture_drawpixels(ctx, x, y, width, height, format, type,
397 unpack, pixels))
398 return;
399
400 if (intel_stencil_drawpixels(ctx, x, y, width, height, format, type,
401 unpack, pixels))
402 return;
403
404 if (INTEL_DEBUG & DEBUG_PIXEL)
405 _mesa_printf("%s: fallback to swrast\n", __FUNCTION__);
406
407 if (ctx->FragmentProgram._Current == ctx->FragmentProgram._TexEnvProgram) {
408 /*
409 * We don't want the i915 texenv program to be applied to DrawPixels.
410 * This is really just a performance optimization (mesa will other-
411 * wise happily run the fragment program on each pixel in the image).
412 */
413 struct gl_fragment_program *fpSave = ctx->FragmentProgram._Current;
414 /* can't just set current frag prog to 0 here as on buffer resize
415 we'll get new state checks which will segfault. Remains a hack. */
416 ctx->FragmentProgram._Current = NULL;
417 ctx->FragmentProgram._UseTexEnvProgram = GL_FALSE;
418 ctx->FragmentProgram._Active = GL_FALSE;
419 _swrast_DrawPixels( ctx, x, y, width, height, format, type,
420 unpack, pixels );
421 ctx->FragmentProgram._Current = fpSave;
422 ctx->FragmentProgram._UseTexEnvProgram = GL_TRUE;
423 ctx->FragmentProgram._Active = GL_TRUE;
424 _swrast_InvalidateState(ctx, _NEW_PROGRAM);
425 }
426 else {
427 _swrast_DrawPixels( ctx, x, y, width, height, format, type,
428 unpack, pixels );
429 }
430 }