intel_pixel_draw.c revision 4a253431abf43a0638afb43605b44a8742b72a60
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/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" 47 48#include "intel_context.h" 49#include "intel_batchbuffer.h" 50#include "intel_blit.h" 51#include "intel_buffers.h" 52#include "intel_regions.h" 53#include "intel_pixel.h" 54#include "intel_fbo.h" 55 56 57/** XXX compare perf of this vs. _mesa_meta_DrawPixels(STENCIL) */ 58static GLboolean 59intel_stencil_drawpixels(GLcontext * ctx, 60 GLint x, GLint y, 61 GLsizei width, GLsizei height, 62 GLenum format, 63 GLenum type, 64 const struct gl_pixelstore_attrib *unpack, 65 const GLvoid *pixels) 66{ 67 struct intel_context *intel = intel_context(ctx); 68 GLuint texname, rb_name, fb_name, old_fb_name; 69 GLfloat vertices[4][2]; 70 struct intel_renderbuffer *irb; 71 struct intel_renderbuffer *depth_irb; 72 struct gl_renderbuffer *rb; 73 struct gl_pixelstore_attrib old_unpack; 74 GLstencil *stencil_pixels; 75 int row, y1, y2; 76 GLint old_active_texture; 77 GLboolean rendering_to_fbo = ctx->DrawBuffer->Name != 0; 78 79 if (format != GL_STENCIL_INDEX) 80 return GL_FALSE; 81 82 /* If there's nothing to write, we're done. */ 83 if (ctx->Stencil.WriteMask[0] == 0) 84 return GL_TRUE; 85 86 /* Can't do a per-bit writemask while treating stencil as rgba data. */ 87 if ((ctx->Stencil.WriteMask[0] & 0xff) != 0xff) { 88 if (INTEL_DEBUG & DEBUG_FALLBACKS) 89 fprintf(stderr, "glDrawPixels(STENCIL_INDEX) fallback: " 90 "stencil mask enabled\n"); 91 return GL_FALSE; 92 } 93 94 /* We don't support stencil testing/ops here */ 95 if (ctx->Stencil._Enabled) 96 return GL_FALSE; 97 98 /* We use FBOs for our wrapping of the depthbuffer into a color 99 * destination. 100 */ 101 if (!ctx->Extensions.EXT_framebuffer_object) 102 return GL_FALSE; 103 104 /* We're going to mess with texturing with no regard to existing texture 105 * state, so if there is some set up we have to bail. 106 */ 107 if (ctx->Texture._EnabledUnits != 0) { 108 if (INTEL_DEBUG & DEBUG_FALLBACKS) 109 fprintf(stderr, "glDrawPixels(STENCIL_INDEX) fallback: " 110 "texturing enabled\n"); 111 return GL_FALSE; 112 } 113 114 /* Can't do textured DrawPixels with a fragment program, unless we were 115 * to generate a new program that sampled our texture and put the results 116 * in the fragment color before the user's program started. 117 */ 118 if (ctx->FragmentProgram.Enabled) { 119 if (INTEL_DEBUG & DEBUG_FALLBACKS) 120 fprintf(stderr, "glDrawPixels(STENCIL_INDEX) fallback: " 121 "fragment program enabled\n"); 122 return GL_FALSE; 123 } 124 125 /* Check that we can load in a texture this big. */ 126 if (width > (1 << (ctx->Const.MaxTextureLevels - 1)) || 127 height > (1 << (ctx->Const.MaxTextureLevels - 1))) { 128 if (INTEL_DEBUG & DEBUG_FALLBACKS) 129 fprintf(stderr, "glDrawPixels(STENCIL_INDEX) fallback: " 130 "bitmap too large (%dx%d)\n", 131 width, height); 132 return GL_FALSE; 133 } 134 135 if (!ctx->Extensions.ARB_texture_non_power_of_two && 136 (!is_power_of_two(width) || !is_power_of_two(height))) { 137 if (INTEL_DEBUG & DEBUG_FALLBACKS) 138 fprintf(stderr, 139 "glDrawPixels(GL_STENCIL_INDEX) fallback: NPOT texture\n"); 140 return GL_FALSE; 141 } 142 143 _mesa_PushAttrib(GL_ENABLE_BIT | GL_TEXTURE_BIT | 144 GL_CURRENT_BIT | GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 145 _mesa_PushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT); 146 old_fb_name = ctx->DrawBuffer->Name; 147 old_active_texture = ctx->Texture.CurrentUnit; 148 149 _mesa_Disable(GL_POLYGON_STIPPLE); 150 _mesa_Disable(GL_DEPTH_TEST); 151 _mesa_Disable(GL_STENCIL_TEST); 152 153 /* Unpack the supplied stencil values into a ubyte buffer. */ 154 assert(sizeof(GLstencil) == sizeof(GLubyte)); 155 stencil_pixels = _mesa_malloc(width * height * sizeof(GLstencil)); 156 for (row = 0; row < height; row++) { 157 GLvoid *source = _mesa_image_address2d(unpack, pixels, 158 width, height, 159 GL_COLOR_INDEX, type, 160 row, 0); 161 _mesa_unpack_stencil_span(ctx, width, GL_UNSIGNED_BYTE, 162 stencil_pixels + 163 row * width * sizeof(GLstencil), 164 type, source, unpack, ctx->_ImageTransferState); 165 } 166 167 /* Take the current depth/stencil renderbuffer, and make a new one wrapping 168 * it which will be treated as GL_RGBA8 so we can render to it as a color 169 * buffer. 170 */ 171 depth_irb = intel_get_renderbuffer(ctx->DrawBuffer, BUFFER_DEPTH); 172 irb = intel_create_renderbuffer(MESA_FORMAT_ARGB8888); 173 rb = &irb->Base; 174 irb->Base.Width = depth_irb->Base.Width; 175 irb->Base.Height = depth_irb->Base.Height; 176 intel_renderbuffer_set_region(irb, depth_irb->region); 177 178 /* Create a name for our renderbuffer, which lets us use other mesa 179 * rb functions for convenience. 180 */ 181 _mesa_GenRenderbuffersEXT(1, &rb_name); 182 irb->Base.RefCount++; 183 _mesa_HashInsert(ctx->Shared->RenderBuffers, rb_name, &irb->Base); 184 185 /* Bind the new renderbuffer to the color attachment point. */ 186 _mesa_GenFramebuffersEXT(1, &fb_name); 187 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb_name); 188 _mesa_FramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, 189 GL_COLOR_ATTACHMENT0_EXT, 190 GL_RENDERBUFFER_EXT, 191 rb_name); 192 /* Choose to render to the color attachment. */ 193 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT); 194 195 _mesa_DepthMask(GL_FALSE); 196 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE); 197 198 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB); 199 _mesa_Enable(GL_TEXTURE_2D); 200 _mesa_GenTextures(1, &texname); 201 _mesa_BindTexture(GL_TEXTURE_2D, texname); 202 _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 203 _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 204 _mesa_TexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 205 old_unpack = ctx->Unpack; 206 ctx->Unpack = ctx->DefaultPacking; 207 _mesa_TexImage2D(GL_TEXTURE_2D, 0, GL_INTENSITY, width, height, 0, 208 GL_RED, GL_UNSIGNED_BYTE, stencil_pixels); 209 ctx->Unpack = old_unpack; 210 _mesa_free(stencil_pixels); 211 212 meta_set_passthrough_transform(&intel->meta); 213 214 /* Since we're rendering to the framebuffer as if it was an FBO, 215 * if it's the window system we have to flip the coordinates. 216 */ 217 if (rendering_to_fbo) { 218 y1 = y; 219 y2 = y + height * ctx->Pixel.ZoomY; 220 } else { 221 y1 = irb->Base.Height - (y + height * ctx->Pixel.ZoomY); 222 y2 = irb->Base.Height - y; 223 } 224 vertices[0][0] = x; 225 vertices[0][1] = y1; 226 vertices[1][0] = x + width * ctx->Pixel.ZoomX; 227 vertices[1][1] = y1; 228 vertices[2][0] = x + width * ctx->Pixel.ZoomX; 229 vertices[2][1] = y2; 230 vertices[3][0] = x; 231 vertices[3][1] = y2; 232 233 _mesa_VertexPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), &vertices); 234 _mesa_Enable(GL_VERTEX_ARRAY); 235 meta_set_default_texrect(&intel->meta); 236 237 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4); 238 239 meta_restore_texcoords(&intel->meta); 240 meta_restore_transform(&intel->meta); 241 242 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB + old_active_texture); 243 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, old_fb_name); 244 245 _mesa_PopClientAttrib(); 246 _mesa_PopAttrib(); 247 248 _mesa_DeleteTextures(1, &texname); 249 _mesa_DeleteFramebuffersEXT(1, &fb_name); 250 _mesa_DeleteRenderbuffersEXT(1, &rb_name); 251 252 return GL_TRUE; 253} 254 255void 256intelDrawPixels(GLcontext * ctx, 257 GLint x, GLint y, 258 GLsizei width, GLsizei height, 259 GLenum format, 260 GLenum type, 261 const struct gl_pixelstore_attrib *unpack, 262 const GLvoid * pixels) 263{ 264#if 0 265 /* XXX this function doesn't seem to work reliably even when all 266 * the pre-requisite conditions are met. 267 * Note that this function is never hit with conform. 268 * Fall back to swrast because even the _mesa_meta_DrawPixels() approach 269 * isn't working because of an apparent stencil bug. 270 */ 271 if (intel_stencil_drawpixels(ctx, x, y, width, height, format, type, 272 unpack, pixels)) 273 return; 274#else 275 (void) intel_stencil_drawpixels; /* silence warning */ 276 if (format == GL_STENCIL_INDEX) { 277 _swrast_DrawPixels(ctx, x, y, width, height, format, type, 278 unpack, pixels); 279 return; 280 } 281#endif 282 283 _mesa_meta_DrawPixels(ctx, x, y, width, height, format, type, 284 unpack, pixels); 285} 286