Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Mesa 3-D graphics library |
| 3 | * Version: 7.6 |
| 4 | * |
| 5 | * Copyright (C) 2009 VMware, Inc. All Rights Reserved. |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 8 | * copy of this software and associated documentation files (the "Software"), |
| 9 | * to deal in the Software without restriction, including without limitation |
| 10 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 11 | * and/or sell copies of the Software, and to permit persons to whom the |
| 12 | * Software is furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be included |
| 15 | * in all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 18 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 | * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 21 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | /** |
| 26 | * Meta operations. Some GL operations can be expressed in terms of |
| 27 | * other GL operations. For example, glBlitFramebuffer() can be done |
| 28 | * with texture mapping and glClear() can be done with polygon rendering. |
| 29 | * |
| 30 | * \author Brian Paul |
| 31 | */ |
| 32 | |
| 33 | |
| 34 | #include "main/glheader.h" |
| 35 | #include "main/mtypes.h" |
| 36 | #include "main/imports.h" |
| 37 | #include "main/arrayobj.h" |
| 38 | #include "main/blend.h" |
| 39 | #include "main/bufferobj.h" |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 40 | #include "main/buffers.h" |
Brian Paul | 3e5a352 | 2009-09-19 16:32:42 -0600 | [diff] [blame] | 41 | #include "main/colortab.h" |
Brian Paul | b0e9ea6 | 2009-09-19 13:37:28 -0600 | [diff] [blame] | 42 | #include "main/convolve.h" |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 43 | #include "main/depth.h" |
| 44 | #include "main/enable.h" |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 45 | #include "main/fbobject.h" |
Brian Paul | 2de7683 | 2009-09-27 19:51:45 -0600 | [diff] [blame] | 46 | #include "main/formats.h" |
Brian Paul | 73b150c | 2009-08-13 09:46:42 -0600 | [diff] [blame] | 47 | #include "main/image.h" |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 48 | #include "main/macros.h" |
| 49 | #include "main/matrix.h" |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 50 | #include "main/mipmap.h" |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 51 | #include "main/polygon.h" |
Brian Paul | 0566122 | 2009-08-31 21:13:41 -0600 | [diff] [blame] | 52 | #include "main/readpix.h" |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 53 | #include "main/scissor.h" |
| 54 | #include "main/shaders.h" |
Brian Paul | b0e9ea6 | 2009-09-19 13:37:28 -0600 | [diff] [blame] | 55 | #include "main/state.h" |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 56 | #include "main/stencil.h" |
| 57 | #include "main/texobj.h" |
| 58 | #include "main/texenv.h" |
Michel Dänzer | 4a6759b | 2009-10-03 18:01:57 +0200 | [diff] [blame] | 59 | #include "main/texformat.h" |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 60 | #include "main/teximage.h" |
| 61 | #include "main/texparam.h" |
| 62 | #include "main/texstate.h" |
| 63 | #include "main/varray.h" |
| 64 | #include "main/viewport.h" |
| 65 | #include "shader/program.h" |
Brian Paul | 1fc713a | 2009-08-31 19:28:11 -0600 | [diff] [blame] | 66 | #include "shader/arbprogram.h" |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 67 | #include "swrast/swrast.h" |
| 68 | #include "drivers/common/meta.h" |
| 69 | |
| 70 | |
Brian Paul | 0ef5b62 | 2009-09-10 19:56:35 -0600 | [diff] [blame] | 71 | /** Return offset in bytes of the field within a vertex struct */ |
| 72 | #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD)) |
| 73 | |
| 74 | |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 75 | /** |
Brian Paul | 1d8fbef | 2009-09-16 20:09:05 -0600 | [diff] [blame] | 76 | * Flags passed to _mesa_meta_begin(). |
| 77 | */ |
| 78 | /*@{*/ |
| 79 | #define META_ALL ~0x0 |
| 80 | #define META_ALPHA_TEST 0x1 |
| 81 | #define META_BLEND 0x2 /**< includes logicop */ |
| 82 | #define META_COLOR_MASK 0x4 |
| 83 | #define META_DEPTH_TEST 0x8 |
| 84 | #define META_FOG 0x10 |
| 85 | #define META_PIXEL_STORE 0x20 |
| 86 | #define META_PIXEL_TRANSFER 0x40 |
| 87 | #define META_RASTERIZATION 0x80 |
| 88 | #define META_SCISSOR 0x100 |
| 89 | #define META_SHADER 0x200 |
| 90 | #define META_STENCIL_TEST 0x400 |
| 91 | #define META_TRANSFORM 0x800 /**< modelview, projection, clip planes */ |
| 92 | #define META_TEXTURE 0x1000 |
| 93 | #define META_VERTEX 0x2000 |
| 94 | #define META_VIEWPORT 0x4000 |
| 95 | /*@}*/ |
| 96 | |
| 97 | |
| 98 | /** |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 99 | * State which we may save/restore across meta ops. |
| 100 | * XXX this may be incomplete... |
| 101 | */ |
| 102 | struct save_state |
| 103 | { |
| 104 | GLbitfield SavedState; /**< bitmask of META_* flags */ |
| 105 | |
| 106 | /** META_ALPHA_TEST */ |
| 107 | GLboolean AlphaEnabled; |
| 108 | |
| 109 | /** META_BLEND */ |
| 110 | GLboolean BlendEnabled; |
| 111 | GLboolean ColorLogicOpEnabled; |
| 112 | |
| 113 | /** META_COLOR_MASK */ |
| 114 | GLubyte ColorMask[4]; |
| 115 | |
| 116 | /** META_DEPTH_TEST */ |
| 117 | struct gl_depthbuffer_attrib Depth; |
| 118 | |
Brian Paul | f821866 | 2009-08-11 18:54:57 -0600 | [diff] [blame] | 119 | /** META_FOG */ |
| 120 | GLboolean Fog; |
| 121 | |
Brian Paul | 0566122 | 2009-08-31 21:13:41 -0600 | [diff] [blame] | 122 | /** META_PIXEL_STORE */ |
| 123 | struct gl_pixelstore_attrib Pack, Unpack; |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 124 | |
Brian Paul | 1d8fbef | 2009-09-16 20:09:05 -0600 | [diff] [blame] | 125 | /** META_PIXEL_TRANSFER */ |
| 126 | GLfloat RedBias, RedScale; |
| 127 | GLfloat GreenBias, GreenScale; |
| 128 | GLfloat BlueBias, BlueScale; |
| 129 | GLfloat AlphaBias, AlphaScale; |
| 130 | GLfloat DepthBias, DepthScale; |
| 131 | GLboolean MapColorFlag; |
| 132 | GLboolean Convolution1DEnabled; |
| 133 | GLboolean Convolution2DEnabled; |
| 134 | GLboolean Separable2DEnabled; |
| 135 | |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 136 | /** META_RASTERIZATION */ |
| 137 | GLenum FrontPolygonMode, BackPolygonMode; |
| 138 | GLboolean PolygonOffset; |
| 139 | GLboolean PolygonSmooth; |
| 140 | GLboolean PolygonStipple; |
| 141 | GLboolean PolygonCull; |
| 142 | |
| 143 | /** META_SCISSOR */ |
| 144 | struct gl_scissor_attrib Scissor; |
| 145 | |
| 146 | /** META_SHADER */ |
| 147 | GLboolean VertexProgramEnabled; |
| 148 | struct gl_vertex_program *VertexProgram; |
| 149 | GLboolean FragmentProgramEnabled; |
| 150 | struct gl_fragment_program *FragmentProgram; |
| 151 | GLuint Shader; |
| 152 | |
| 153 | /** META_STENCIL_TEST */ |
| 154 | struct gl_stencil_attrib Stencil; |
| 155 | |
| 156 | /** META_TRANSFORM */ |
| 157 | GLenum MatrixMode; |
| 158 | GLfloat ModelviewMatrix[16]; |
| 159 | GLfloat ProjectionMatrix[16]; |
Brian Paul | edb991b | 2009-08-10 15:44:05 -0600 | [diff] [blame] | 160 | GLfloat TextureMatrix[16]; |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 161 | GLbitfield ClipPlanesEnabled; |
| 162 | |
| 163 | /** META_TEXTURE */ |
| 164 | GLuint ActiveUnit; |
| 165 | GLuint ClientActiveUnit; |
| 166 | /** for unit[0] only */ |
| 167 | struct gl_texture_object *CurrentTexture[NUM_TEXTURE_TARGETS]; |
| 168 | /** mask of TEXTURE_2D_BIT, etc */ |
| 169 | GLbitfield TexEnabled[MAX_TEXTURE_UNITS]; |
| 170 | GLbitfield TexGenEnabled[MAX_TEXTURE_UNITS]; |
| 171 | GLuint EnvMode; /* unit[0] only */ |
| 172 | |
| 173 | /** META_VERTEX */ |
| 174 | struct gl_array_object *ArrayObj; |
| 175 | struct gl_buffer_object *ArrayBufferObj; |
| 176 | |
| 177 | /** META_VIEWPORT */ |
| 178 | GLint ViewportX, ViewportY, ViewportW, ViewportH; |
| 179 | GLclampd DepthNear, DepthFar; |
| 180 | |
| 181 | /** Miscellaneous (always disabled) */ |
| 182 | GLboolean Lighting; |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | |
| 186 | /** |
Brian Paul | 886e7b3 | 2009-09-03 22:03:02 -0600 | [diff] [blame] | 187 | * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc. |
| 188 | * This is currently shared by all the meta ops. But we could create a |
| 189 | * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc. |
| 190 | */ |
| 191 | struct temp_texture |
| 192 | { |
| 193 | GLuint TexObj; |
| 194 | GLenum Target; /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */ |
| 195 | GLsizei MinSize; /**< Min texture size to allocate */ |
| 196 | GLsizei MaxSize; /**< Max possible texture size */ |
| 197 | GLboolean NPOT; /**< Non-power of two size OK? */ |
| 198 | GLsizei Width, Height; /**< Current texture size */ |
| 199 | GLenum IntFormat; |
| 200 | GLfloat Sright, Ttop; /**< right, top texcoords */ |
| 201 | }; |
| 202 | |
| 203 | |
| 204 | /** |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 205 | * State for glBlitFramebufer() |
| 206 | */ |
| 207 | struct blit_state |
| 208 | { |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 209 | GLuint ArrayObj; |
| 210 | GLuint VBO; |
Brian Paul | 0566122 | 2009-08-31 21:13:41 -0600 | [diff] [blame] | 211 | GLuint DepthFP; |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | |
| 215 | /** |
| 216 | * State for glClear() |
| 217 | */ |
| 218 | struct clear_state |
| 219 | { |
| 220 | GLuint ArrayObj; |
| 221 | GLuint VBO; |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 222 | }; |
| 223 | |
| 224 | |
| 225 | /** |
Brian Paul | c16fa38 | 2009-08-10 14:43:15 -0600 | [diff] [blame] | 226 | * State for glCopyPixels() |
| 227 | */ |
| 228 | struct copypix_state |
| 229 | { |
Brian Paul | c16fa38 | 2009-08-10 14:43:15 -0600 | [diff] [blame] | 230 | GLuint ArrayObj; |
| 231 | GLuint VBO; |
Brian Paul | c16fa38 | 2009-08-10 14:43:15 -0600 | [diff] [blame] | 232 | }; |
| 233 | |
| 234 | |
Brian Paul | c16fa38 | 2009-08-10 14:43:15 -0600 | [diff] [blame] | 235 | /** |
Brian Paul | dba6d52 | 2009-08-11 18:56:26 -0600 | [diff] [blame] | 236 | * State for glDrawPixels() |
| 237 | */ |
| 238 | struct drawpix_state |
| 239 | { |
Brian Paul | dba6d52 | 2009-08-11 18:56:26 -0600 | [diff] [blame] | 240 | GLuint ArrayObj; |
Brian Paul | 1fc713a | 2009-08-31 19:28:11 -0600 | [diff] [blame] | 241 | |
| 242 | GLuint StencilFP; /**< Fragment program for drawing stencil images */ |
| 243 | GLuint DepthFP; /**< Fragment program for drawing depth images */ |
Brian Paul | dba6d52 | 2009-08-11 18:56:26 -0600 | [diff] [blame] | 244 | }; |
| 245 | |
| 246 | |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 247 | /** |
Brian Paul | bcb62ae | 2009-09-03 21:27:06 -0600 | [diff] [blame] | 248 | * State for glBitmap() |
| 249 | */ |
| 250 | struct bitmap_state |
| 251 | { |
| 252 | GLuint ArrayObj; |
| 253 | GLuint VBO; |
Brian Paul | 886e7b3 | 2009-09-03 22:03:02 -0600 | [diff] [blame] | 254 | struct temp_texture Tex; /**< separate texture from other meta ops */ |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 255 | }; |
| 256 | |
Brian Paul | dba6d52 | 2009-08-11 18:56:26 -0600 | [diff] [blame] | 257 | |
| 258 | /** |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 259 | * State for _mesa_meta_generate_mipmap() |
| 260 | */ |
| 261 | struct gen_mipmap_state |
| 262 | { |
| 263 | GLuint ArrayObj; |
| 264 | GLuint VBO; |
| 265 | GLuint FBO; |
| 266 | }; |
| 267 | |
| 268 | |
| 269 | /** |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 270 | * All per-context meta state. |
| 271 | */ |
| 272 | struct gl_meta_state |
| 273 | { |
| 274 | struct save_state Save; /**< state saved during meta-ops */ |
| 275 | |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 276 | struct temp_texture TempTex; |
| 277 | |
Brian Paul | 4de8e21 | 2009-09-19 17:06:15 -0600 | [diff] [blame] | 278 | struct blit_state Blit; /**< For _mesa_meta_BlitFramebuffer() */ |
| 279 | struct clear_state Clear; /**< For _mesa_meta_Clear() */ |
| 280 | struct copypix_state CopyPix; /**< For _mesa_meta_CopyPixels() */ |
| 281 | struct drawpix_state DrawPix; /**< For _mesa_meta_DrawPixels() */ |
| 282 | struct bitmap_state Bitmap; /**< For _mesa_meta_Bitmap() */ |
| 283 | struct gen_mipmap_state Mipmap; /**< For _mesa_meta_GenerateMipmap() */ |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 284 | }; |
| 285 | |
| 286 | |
| 287 | /** |
| 288 | * Initialize meta-ops for a context. |
| 289 | * To be called once during context creation. |
| 290 | */ |
| 291 | void |
| 292 | _mesa_meta_init(GLcontext *ctx) |
| 293 | { |
| 294 | ASSERT(!ctx->Meta); |
| 295 | |
| 296 | ctx->Meta = CALLOC_STRUCT(gl_meta_state); |
| 297 | } |
| 298 | |
| 299 | |
| 300 | /** |
| 301 | * Free context meta-op state. |
| 302 | * To be called once during context destruction. |
| 303 | */ |
| 304 | void |
| 305 | _mesa_meta_free(GLcontext *ctx) |
| 306 | { |
Brian Paul | 7dd2c0a | 2009-10-07 14:07:49 -0600 | [diff] [blame] | 307 | /* Note: Any textures, VBOs, etc, that we allocate should get |
| 308 | * freed by the normal context destruction code. But this would be |
| 309 | * the place to free other meta data someday. |
| 310 | */ |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 311 | _mesa_free(ctx->Meta); |
| 312 | ctx->Meta = NULL; |
| 313 | } |
| 314 | |
| 315 | |
| 316 | /** |
| 317 | * Enter meta state. This is like a light-weight version of glPushAttrib |
| 318 | * but it also resets most GL state back to default values. |
| 319 | * |
| 320 | * \param state bitmask of META_* flags indicating which attribute groups |
| 321 | * to save and reset to their defaults |
| 322 | */ |
| 323 | static void |
| 324 | _mesa_meta_begin(GLcontext *ctx, GLbitfield state) |
| 325 | { |
| 326 | struct save_state *save = &ctx->Meta->Save; |
| 327 | |
| 328 | save->SavedState = state; |
| 329 | |
| 330 | if (state & META_ALPHA_TEST) { |
| 331 | save->AlphaEnabled = ctx->Color.AlphaEnabled; |
| 332 | if (ctx->Color.AlphaEnabled) |
Brian Paul | 0e5293a | 2009-09-03 21:30:06 -0600 | [diff] [blame] | 333 | _mesa_set_enable(ctx, GL_ALPHA_TEST, GL_FALSE); |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | if (state & META_BLEND) { |
| 337 | save->BlendEnabled = ctx->Color.BlendEnabled; |
| 338 | if (ctx->Color.BlendEnabled) |
Brian Paul | 0e5293a | 2009-09-03 21:30:06 -0600 | [diff] [blame] | 339 | _mesa_set_enable(ctx, GL_BLEND, GL_FALSE); |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 340 | save->ColorLogicOpEnabled = ctx->Color.ColorLogicOpEnabled; |
| 341 | if (ctx->Color.ColorLogicOpEnabled) |
Brian Paul | 0e5293a | 2009-09-03 21:30:06 -0600 | [diff] [blame] | 342 | _mesa_set_enable(ctx, GL_COLOR_LOGIC_OP, GL_FALSE); |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | if (state & META_COLOR_MASK) { |
| 346 | COPY_4V(save->ColorMask, ctx->Color.ColorMask); |
| 347 | if (!ctx->Color.ColorMask[0] || |
| 348 | !ctx->Color.ColorMask[1] || |
| 349 | !ctx->Color.ColorMask[2] || |
| 350 | !ctx->Color.ColorMask[3]) |
| 351 | _mesa_ColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); |
| 352 | } |
| 353 | |
| 354 | if (state & META_DEPTH_TEST) { |
| 355 | save->Depth = ctx->Depth; /* struct copy */ |
| 356 | if (ctx->Depth.Test) |
Brian Paul | 0e5293a | 2009-09-03 21:30:06 -0600 | [diff] [blame] | 357 | _mesa_set_enable(ctx, GL_DEPTH_TEST, GL_FALSE); |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 358 | } |
| 359 | |
Brian Paul | f821866 | 2009-08-11 18:54:57 -0600 | [diff] [blame] | 360 | if (state & META_FOG) { |
| 361 | save->Fog = ctx->Fog.Enabled; |
| 362 | if (ctx->Fog.Enabled) |
| 363 | _mesa_set_enable(ctx, GL_FOG, GL_FALSE); |
| 364 | } |
| 365 | |
Brian Paul | 0566122 | 2009-08-31 21:13:41 -0600 | [diff] [blame] | 366 | if (state & META_PIXEL_STORE) { |
| 367 | save->Pack = ctx->Pack; |
| 368 | save->Unpack = ctx->Unpack; |
| 369 | ctx->Pack = ctx->DefaultPacking; |
| 370 | ctx->Unpack = ctx->DefaultPacking; |
| 371 | } |
| 372 | |
Brian Paul | 1d8fbef | 2009-09-16 20:09:05 -0600 | [diff] [blame] | 373 | if (state & META_PIXEL_TRANSFER) { |
| 374 | save->RedScale = ctx->Pixel.RedScale; |
| 375 | save->RedBias = ctx->Pixel.RedBias; |
| 376 | save->GreenScale = ctx->Pixel.GreenScale; |
| 377 | save->GreenBias = ctx->Pixel.GreenBias; |
| 378 | save->BlueScale = ctx->Pixel.BlueScale; |
| 379 | save->BlueBias = ctx->Pixel.BlueBias; |
| 380 | save->AlphaScale = ctx->Pixel.AlphaScale; |
| 381 | save->AlphaBias = ctx->Pixel.AlphaBias; |
| 382 | save->MapColorFlag = ctx->Pixel.MapColorFlag; |
| 383 | save->Convolution1DEnabled = ctx->Pixel.Convolution1DEnabled; |
| 384 | save->Convolution2DEnabled = ctx->Pixel.Convolution2DEnabled; |
| 385 | save->Separable2DEnabled = ctx->Pixel.Separable2DEnabled; |
| 386 | ctx->Pixel.RedScale = 1.0F; |
| 387 | ctx->Pixel.RedBias = 0.0F; |
| 388 | ctx->Pixel.GreenScale = 1.0F; |
| 389 | ctx->Pixel.GreenBias = 0.0F; |
| 390 | ctx->Pixel.BlueScale = 1.0F; |
| 391 | ctx->Pixel.BlueBias = 0.0F; |
| 392 | ctx->Pixel.AlphaScale = 1.0F; |
| 393 | ctx->Pixel.AlphaBias = 0.0F; |
| 394 | ctx->Pixel.MapColorFlag = GL_FALSE; |
| 395 | ctx->Pixel.Convolution1DEnabled = GL_FALSE; |
| 396 | ctx->Pixel.Convolution2DEnabled = GL_FALSE; |
| 397 | ctx->Pixel.Separable2DEnabled = GL_FALSE; |
| 398 | /* XXX more state */ |
| 399 | ctx->NewState |=_NEW_PIXEL; |
| 400 | } |
| 401 | |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 402 | if (state & META_RASTERIZATION) { |
| 403 | save->FrontPolygonMode = ctx->Polygon.FrontMode; |
| 404 | save->BackPolygonMode = ctx->Polygon.BackMode; |
| 405 | save->PolygonOffset = ctx->Polygon.OffsetFill; |
| 406 | save->PolygonSmooth = ctx->Polygon.SmoothFlag; |
| 407 | save->PolygonStipple = ctx->Polygon.StippleFlag; |
| 408 | save->PolygonCull = ctx->Polygon.CullFlag; |
| 409 | _mesa_PolygonMode(GL_FRONT_AND_BACK, GL_FILL); |
| 410 | _mesa_set_enable(ctx, GL_POLYGON_OFFSET_FILL, GL_FALSE); |
| 411 | _mesa_set_enable(ctx, GL_POLYGON_SMOOTH, GL_FALSE); |
| 412 | _mesa_set_enable(ctx, GL_POLYGON_STIPPLE, GL_FALSE); |
| 413 | _mesa_set_enable(ctx, GL_CULL_FACE, GL_FALSE); |
| 414 | } |
| 415 | |
| 416 | if (state & META_SCISSOR) { |
| 417 | save->Scissor = ctx->Scissor; /* struct copy */ |
| 418 | } |
| 419 | |
| 420 | if (state & META_SHADER) { |
| 421 | if (ctx->Extensions.ARB_vertex_program) { |
| 422 | save->VertexProgramEnabled = ctx->VertexProgram.Enabled; |
| 423 | save->VertexProgram = ctx->VertexProgram.Current; |
| 424 | _mesa_set_enable(ctx, GL_VERTEX_PROGRAM_ARB, GL_FALSE); |
| 425 | } |
| 426 | |
| 427 | if (ctx->Extensions.ARB_fragment_program) { |
| 428 | save->FragmentProgramEnabled = ctx->FragmentProgram.Enabled; |
| 429 | save->FragmentProgram = ctx->FragmentProgram.Current; |
| 430 | _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_FALSE); |
| 431 | } |
| 432 | |
| 433 | if (ctx->Extensions.ARB_shader_objects) { |
| 434 | save->Shader = ctx->Shader.CurrentProgram ? |
| 435 | ctx->Shader.CurrentProgram->Name : 0; |
| 436 | _mesa_UseProgramObjectARB(0); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | if (state & META_STENCIL_TEST) { |
| 441 | save->Stencil = ctx->Stencil; /* struct copy */ |
| 442 | if (ctx->Stencil.Enabled) |
Brian Paul | 0e5293a | 2009-09-03 21:30:06 -0600 | [diff] [blame] | 443 | _mesa_set_enable(ctx, GL_STENCIL_TEST, GL_FALSE); |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 444 | /* NOTE: other stencil state not reset */ |
| 445 | } |
| 446 | |
| 447 | if (state & META_TEXTURE) { |
| 448 | GLuint u, tgt; |
| 449 | |
| 450 | save->ActiveUnit = ctx->Texture.CurrentUnit; |
| 451 | save->ClientActiveUnit = ctx->Array.ActiveTexture; |
| 452 | save->EnvMode = ctx->Texture.Unit[0].EnvMode; |
| 453 | |
| 454 | /* Disable all texture units */ |
| 455 | for (u = 0; u < ctx->Const.MaxTextureUnits; u++) { |
| 456 | save->TexEnabled[u] = ctx->Texture.Unit[u].Enabled; |
| 457 | save->TexGenEnabled[u] = ctx->Texture.Unit[u].TexGenEnabled; |
Brian Paul | f821866 | 2009-08-11 18:54:57 -0600 | [diff] [blame] | 458 | if (ctx->Texture.Unit[u].Enabled || |
| 459 | ctx->Texture.Unit[u].TexGenEnabled) { |
| 460 | _mesa_ActiveTextureARB(GL_TEXTURE0 + u); |
| 461 | _mesa_set_enable(ctx, GL_TEXTURE_1D, GL_FALSE); |
| 462 | _mesa_set_enable(ctx, GL_TEXTURE_2D, GL_FALSE); |
| 463 | _mesa_set_enable(ctx, GL_TEXTURE_3D, GL_FALSE); |
| 464 | _mesa_set_enable(ctx, GL_TEXTURE_CUBE_MAP, GL_FALSE); |
| 465 | _mesa_set_enable(ctx, GL_TEXTURE_RECTANGLE, GL_FALSE); |
| 466 | _mesa_set_enable(ctx, GL_TEXTURE_GEN_S, GL_FALSE); |
| 467 | _mesa_set_enable(ctx, GL_TEXTURE_GEN_T, GL_FALSE); |
| 468 | _mesa_set_enable(ctx, GL_TEXTURE_GEN_R, GL_FALSE); |
| 469 | _mesa_set_enable(ctx, GL_TEXTURE_GEN_Q, GL_FALSE); |
| 470 | } |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | /* save current texture objects for unit[0] only */ |
| 474 | for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) { |
| 475 | _mesa_reference_texobj(&save->CurrentTexture[tgt], |
| 476 | ctx->Texture.Unit[0].CurrentTex[tgt]); |
| 477 | } |
| 478 | |
| 479 | /* set defaults for unit[0] */ |
| 480 | _mesa_ActiveTextureARB(GL_TEXTURE0); |
| 481 | _mesa_ClientActiveTextureARB(GL_TEXTURE0); |
| 482 | _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | if (state & META_TRANSFORM) { |
Brian Paul | edb991b | 2009-08-10 15:44:05 -0600 | [diff] [blame] | 486 | GLuint activeTexture = ctx->Texture.CurrentUnit; |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 487 | _mesa_memcpy(save->ModelviewMatrix, ctx->ModelviewMatrixStack.Top->m, |
| 488 | 16 * sizeof(GLfloat)); |
| 489 | _mesa_memcpy(save->ProjectionMatrix, ctx->ProjectionMatrixStack.Top->m, |
| 490 | 16 * sizeof(GLfloat)); |
Brian Paul | edb991b | 2009-08-10 15:44:05 -0600 | [diff] [blame] | 491 | _mesa_memcpy(save->TextureMatrix, ctx->TextureMatrixStack[0].Top->m, |
| 492 | 16 * sizeof(GLfloat)); |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 493 | save->MatrixMode = ctx->Transform.MatrixMode; |
| 494 | /* set 1:1 vertex:pixel coordinate transform */ |
Brian Paul | edb991b | 2009-08-10 15:44:05 -0600 | [diff] [blame] | 495 | _mesa_ActiveTextureARB(GL_TEXTURE0); |
| 496 | _mesa_MatrixMode(GL_TEXTURE); |
| 497 | _mesa_LoadIdentity(); |
| 498 | _mesa_ActiveTextureARB(GL_TEXTURE0 + activeTexture); |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 499 | _mesa_MatrixMode(GL_MODELVIEW); |
| 500 | _mesa_LoadIdentity(); |
| 501 | _mesa_MatrixMode(GL_PROJECTION); |
| 502 | _mesa_LoadIdentity(); |
| 503 | _mesa_Ortho(0.0F, ctx->DrawBuffer->Width, |
| 504 | 0.0F, ctx->DrawBuffer->Height, |
| 505 | -1.0F, 1.0F); |
| 506 | save->ClipPlanesEnabled = ctx->Transform.ClipPlanesEnabled; |
| 507 | if (ctx->Transform.ClipPlanesEnabled) { |
| 508 | GLuint i; |
| 509 | for (i = 0; i < ctx->Const.MaxClipPlanes; i++) { |
| 510 | _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i, GL_FALSE); |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | if (state & META_VERTEX) { |
| 516 | /* save vertex array object state */ |
| 517 | _mesa_reference_array_object(ctx, &save->ArrayObj, |
| 518 | ctx->Array.ArrayObj); |
| 519 | _mesa_reference_buffer_object(ctx, &save->ArrayBufferObj, |
| 520 | ctx->Array.ArrayBufferObj); |
| 521 | /* set some default state? */ |
| 522 | } |
| 523 | |
| 524 | if (state & META_VIEWPORT) { |
Brian Paul | 0243f79 | 2009-08-27 14:47:24 -0600 | [diff] [blame] | 525 | /* save viewport state */ |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 526 | save->ViewportX = ctx->Viewport.X; |
| 527 | save->ViewportY = ctx->Viewport.Y; |
| 528 | save->ViewportW = ctx->Viewport.Width; |
| 529 | save->ViewportH = ctx->Viewport.Height; |
Brian Paul | 0243f79 | 2009-08-27 14:47:24 -0600 | [diff] [blame] | 530 | /* set viewport to match window size */ |
| 531 | if (ctx->Viewport.X != 0 || |
| 532 | ctx->Viewport.Y != 0 || |
| 533 | ctx->Viewport.Width != ctx->DrawBuffer->Width || |
| 534 | ctx->Viewport.Height != ctx->DrawBuffer->Height) { |
| 535 | _mesa_set_viewport(ctx, 0, 0, |
| 536 | ctx->DrawBuffer->Width, ctx->DrawBuffer->Height); |
| 537 | } |
| 538 | /* save depth range state */ |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 539 | save->DepthNear = ctx->Viewport.Near; |
| 540 | save->DepthFar = ctx->Viewport.Far; |
Brian Paul | 0243f79 | 2009-08-27 14:47:24 -0600 | [diff] [blame] | 541 | /* set depth range to default */ |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 542 | _mesa_DepthRange(0.0, 1.0); |
| 543 | } |
| 544 | |
| 545 | /* misc */ |
| 546 | { |
| 547 | save->Lighting = ctx->Light.Enabled; |
| 548 | if (ctx->Light.Enabled) |
| 549 | _mesa_set_enable(ctx, GL_LIGHTING, GL_FALSE); |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | |
| 553 | |
| 554 | /** |
| 555 | * Leave meta state. This is like a light-weight version of glPopAttrib(). |
| 556 | */ |
| 557 | static void |
| 558 | _mesa_meta_end(GLcontext *ctx) |
| 559 | { |
| 560 | struct save_state *save = &ctx->Meta->Save; |
| 561 | const GLbitfield state = save->SavedState; |
| 562 | |
| 563 | if (state & META_ALPHA_TEST) { |
| 564 | if (ctx->Color.AlphaEnabled != save->AlphaEnabled) |
| 565 | _mesa_set_enable(ctx, GL_ALPHA_TEST, save->AlphaEnabled); |
| 566 | } |
| 567 | |
| 568 | if (state & META_BLEND) { |
| 569 | if (ctx->Color.BlendEnabled != save->BlendEnabled) |
| 570 | _mesa_set_enable(ctx, GL_BLEND, save->BlendEnabled); |
| 571 | if (ctx->Color.ColorLogicOpEnabled != save->ColorLogicOpEnabled) |
| 572 | _mesa_set_enable(ctx, GL_COLOR_LOGIC_OP, save->ColorLogicOpEnabled); |
| 573 | } |
| 574 | |
| 575 | if (state & META_COLOR_MASK) { |
| 576 | if (!TEST_EQ_4V(ctx->Color.ColorMask, save->ColorMask)) |
| 577 | _mesa_ColorMask(save->ColorMask[0], save->ColorMask[1], |
| 578 | save->ColorMask[2], save->ColorMask[3]); |
| 579 | } |
| 580 | |
| 581 | if (state & META_DEPTH_TEST) { |
| 582 | if (ctx->Depth.Test != save->Depth.Test) |
| 583 | _mesa_set_enable(ctx, GL_DEPTH_TEST, save->Depth.Test); |
| 584 | _mesa_DepthFunc(save->Depth.Func); |
| 585 | _mesa_DepthMask(save->Depth.Mask); |
| 586 | } |
| 587 | |
Brian Paul | f821866 | 2009-08-11 18:54:57 -0600 | [diff] [blame] | 588 | if (state & META_FOG) { |
| 589 | _mesa_set_enable(ctx, GL_FOG, save->Fog); |
| 590 | } |
| 591 | |
Brian Paul | 0566122 | 2009-08-31 21:13:41 -0600 | [diff] [blame] | 592 | if (state & META_PIXEL_STORE) { |
| 593 | ctx->Pack = save->Pack; |
| 594 | ctx->Unpack = save->Unpack; |
| 595 | } |
| 596 | |
Brian Paul | 1d8fbef | 2009-09-16 20:09:05 -0600 | [diff] [blame] | 597 | if (state & META_PIXEL_TRANSFER) { |
| 598 | ctx->Pixel.RedScale = save->RedScale; |
| 599 | ctx->Pixel.RedBias = save->RedBias; |
| 600 | ctx->Pixel.GreenScale = save->GreenScale; |
| 601 | ctx->Pixel.GreenBias = save->GreenBias; |
| 602 | ctx->Pixel.BlueScale = save->BlueScale; |
| 603 | ctx->Pixel.BlueBias = save->BlueBias; |
| 604 | ctx->Pixel.AlphaScale = save->AlphaScale; |
| 605 | ctx->Pixel.AlphaBias = save->AlphaBias; |
| 606 | ctx->Pixel.MapColorFlag = save->MapColorFlag; |
| 607 | ctx->Pixel.Convolution1DEnabled = save->Convolution1DEnabled; |
| 608 | ctx->Pixel.Convolution2DEnabled = save->Convolution2DEnabled; |
| 609 | ctx->Pixel.Separable2DEnabled = save->Separable2DEnabled; |
| 610 | /* XXX more state */ |
| 611 | ctx->NewState |=_NEW_PIXEL; |
| 612 | } |
| 613 | |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 614 | if (state & META_RASTERIZATION) { |
| 615 | _mesa_PolygonMode(GL_FRONT, save->FrontPolygonMode); |
| 616 | _mesa_PolygonMode(GL_BACK, save->BackPolygonMode); |
| 617 | _mesa_set_enable(ctx, GL_POLYGON_STIPPLE, save->PolygonStipple); |
| 618 | _mesa_set_enable(ctx, GL_POLYGON_OFFSET_FILL, save->PolygonOffset); |
| 619 | _mesa_set_enable(ctx, GL_POLYGON_SMOOTH, save->PolygonSmooth); |
| 620 | _mesa_set_enable(ctx, GL_CULL_FACE, save->PolygonCull); |
| 621 | } |
| 622 | |
| 623 | if (state & META_SCISSOR) { |
| 624 | _mesa_set_enable(ctx, GL_SCISSOR_TEST, save->Scissor.Enabled); |
| 625 | _mesa_Scissor(save->Scissor.X, save->Scissor.Y, |
| 626 | save->Scissor.Width, save->Scissor.Height); |
| 627 | } |
| 628 | |
| 629 | if (state & META_SHADER) { |
| 630 | if (ctx->Extensions.ARB_vertex_program) { |
| 631 | _mesa_set_enable(ctx, GL_VERTEX_PROGRAM_ARB, |
| 632 | save->VertexProgramEnabled); |
| 633 | _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current, |
| 634 | save->VertexProgram); |
| 635 | } |
| 636 | |
| 637 | if (ctx->Extensions.ARB_fragment_program) { |
| 638 | _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, |
| 639 | save->FragmentProgramEnabled); |
| 640 | _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current, |
| 641 | save->FragmentProgram); |
| 642 | } |
| 643 | |
| 644 | if (ctx->Extensions.ARB_shader_objects) { |
| 645 | _mesa_UseProgramObjectARB(save->Shader); |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | if (state & META_STENCIL_TEST) { |
| 650 | const struct gl_stencil_attrib *stencil = &save->Stencil; |
| 651 | |
| 652 | _mesa_set_enable(ctx, GL_STENCIL_TEST, stencil->Enabled); |
| 653 | _mesa_ClearStencil(stencil->Clear); |
| 654 | if (ctx->Extensions.EXT_stencil_two_side) { |
| 655 | _mesa_set_enable(ctx, GL_STENCIL_TEST_TWO_SIDE_EXT, |
| 656 | stencil->TestTwoSide); |
| 657 | _mesa_ActiveStencilFaceEXT(stencil->ActiveFace |
| 658 | ? GL_BACK : GL_FRONT); |
| 659 | } |
| 660 | /* front state */ |
| 661 | _mesa_StencilFuncSeparate(GL_FRONT, |
| 662 | stencil->Function[0], |
| 663 | stencil->Ref[0], |
| 664 | stencil->ValueMask[0]); |
| 665 | _mesa_StencilMaskSeparate(GL_FRONT, stencil->WriteMask[0]); |
| 666 | _mesa_StencilOpSeparate(GL_FRONT, stencil->FailFunc[0], |
| 667 | stencil->ZFailFunc[0], |
| 668 | stencil->ZPassFunc[0]); |
| 669 | /* back state */ |
| 670 | _mesa_StencilFuncSeparate(GL_BACK, |
| 671 | stencil->Function[1], |
| 672 | stencil->Ref[1], |
| 673 | stencil->ValueMask[1]); |
| 674 | _mesa_StencilMaskSeparate(GL_BACK, stencil->WriteMask[1]); |
| 675 | _mesa_StencilOpSeparate(GL_BACK, stencil->FailFunc[1], |
| 676 | stencil->ZFailFunc[1], |
| 677 | stencil->ZPassFunc[1]); |
| 678 | } |
| 679 | |
| 680 | if (state & META_TEXTURE) { |
| 681 | GLuint u, tgt; |
| 682 | |
| 683 | ASSERT(ctx->Texture.CurrentUnit == 0); |
| 684 | |
| 685 | /* restore texenv for unit[0] */ |
| 686 | _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, save->EnvMode); |
| 687 | |
| 688 | /* restore texture objects for unit[0] only */ |
| 689 | for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) { |
| 690 | _mesa_reference_texobj(&ctx->Texture.Unit[0].CurrentTex[tgt], |
| 691 | save->CurrentTexture[tgt]); |
| 692 | } |
| 693 | |
| 694 | /* Re-enable textures, texgen */ |
| 695 | for (u = 0; u < ctx->Const.MaxTextureUnits; u++) { |
| 696 | if (save->TexEnabled[u]) { |
| 697 | _mesa_ActiveTextureARB(GL_TEXTURE0 + u); |
| 698 | |
| 699 | if (save->TexEnabled[u] & TEXTURE_1D_BIT) |
| 700 | _mesa_set_enable(ctx, GL_TEXTURE_1D, GL_TRUE); |
| 701 | if (save->TexEnabled[u] & TEXTURE_2D_BIT) |
| 702 | _mesa_set_enable(ctx, GL_TEXTURE_2D, GL_TRUE); |
| 703 | if (save->TexEnabled[u] & TEXTURE_3D_BIT) |
| 704 | _mesa_set_enable(ctx, GL_TEXTURE_3D, GL_TRUE); |
| 705 | if (save->TexEnabled[u] & TEXTURE_CUBE_BIT) |
| 706 | _mesa_set_enable(ctx, GL_TEXTURE_CUBE_MAP, GL_TRUE); |
| 707 | if (save->TexEnabled[u] & TEXTURE_RECT_BIT) |
| 708 | _mesa_set_enable(ctx, GL_TEXTURE_RECTANGLE, GL_TRUE); |
| 709 | } |
| 710 | |
| 711 | if (save->TexGenEnabled[u]) { |
| 712 | _mesa_ActiveTextureARB(GL_TEXTURE0 + u); |
| 713 | |
| 714 | if (save->TexGenEnabled[u] & S_BIT) |
| 715 | _mesa_set_enable(ctx, GL_TEXTURE_GEN_S, GL_TRUE); |
| 716 | if (save->TexGenEnabled[u] & T_BIT) |
| 717 | _mesa_set_enable(ctx, GL_TEXTURE_GEN_T, GL_TRUE); |
| 718 | if (save->TexGenEnabled[u] & R_BIT) |
| 719 | _mesa_set_enable(ctx, GL_TEXTURE_GEN_R, GL_TRUE); |
| 720 | if (save->TexGenEnabled[u] & Q_BIT) |
| 721 | _mesa_set_enable(ctx, GL_TEXTURE_GEN_Q, GL_TRUE); |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | /* restore current unit state */ |
| 726 | _mesa_ActiveTextureARB(GL_TEXTURE0 + save->ActiveUnit); |
| 727 | _mesa_ClientActiveTextureARB(GL_TEXTURE0 + save->ClientActiveUnit); |
| 728 | } |
| 729 | |
| 730 | if (state & META_TRANSFORM) { |
Brian Paul | edb991b | 2009-08-10 15:44:05 -0600 | [diff] [blame] | 731 | GLuint activeTexture = ctx->Texture.CurrentUnit; |
| 732 | _mesa_ActiveTextureARB(GL_TEXTURE0); |
| 733 | _mesa_MatrixMode(GL_TEXTURE); |
| 734 | _mesa_LoadMatrixf(save->TextureMatrix); |
| 735 | _mesa_ActiveTextureARB(GL_TEXTURE0 + activeTexture); |
| 736 | |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 737 | _mesa_MatrixMode(GL_MODELVIEW); |
| 738 | _mesa_LoadMatrixf(save->ModelviewMatrix); |
Brian Paul | edb991b | 2009-08-10 15:44:05 -0600 | [diff] [blame] | 739 | |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 740 | _mesa_MatrixMode(GL_PROJECTION); |
| 741 | _mesa_LoadMatrixf(save->ProjectionMatrix); |
Brian Paul | edb991b | 2009-08-10 15:44:05 -0600 | [diff] [blame] | 742 | |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 743 | _mesa_MatrixMode(save->MatrixMode); |
Brian Paul | edb991b | 2009-08-10 15:44:05 -0600 | [diff] [blame] | 744 | |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 745 | if (save->ClipPlanesEnabled) { |
| 746 | GLuint i; |
| 747 | for (i = 0; i < ctx->Const.MaxClipPlanes; i++) { |
| 748 | if (save->ClipPlanesEnabled & (1 << i)) { |
| 749 | _mesa_set_enable(ctx, GL_CLIP_PLANE0 + i, GL_TRUE); |
| 750 | } |
| 751 | } |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | if (state & META_VERTEX) { |
| 756 | /* restore vertex buffer object */ |
| 757 | _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, save->ArrayBufferObj->Name); |
| 758 | _mesa_reference_buffer_object(ctx, &save->ArrayBufferObj, NULL); |
| 759 | |
| 760 | /* restore vertex array object */ |
| 761 | _mesa_BindVertexArray(save->ArrayObj->Name); |
| 762 | _mesa_reference_array_object(ctx, &save->ArrayObj, NULL); |
| 763 | } |
| 764 | |
| 765 | if (state & META_VIEWPORT) { |
Brian Paul | 0243f79 | 2009-08-27 14:47:24 -0600 | [diff] [blame] | 766 | if (save->ViewportX != ctx->Viewport.X || |
| 767 | save->ViewportY != ctx->Viewport.Y || |
| 768 | save->ViewportW != ctx->Viewport.Width || |
| 769 | save->ViewportH != ctx->Viewport.Height) { |
| 770 | _mesa_set_viewport(ctx, save->ViewportX, save->ViewportY, |
| 771 | save->ViewportW, save->ViewportH); |
| 772 | } |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 773 | _mesa_DepthRange(save->DepthNear, save->DepthFar); |
| 774 | } |
| 775 | |
| 776 | /* misc */ |
| 777 | if (save->Lighting) { |
| 778 | _mesa_set_enable(ctx, GL_LIGHTING, GL_TRUE); |
| 779 | } |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | |
| 783 | /** |
Brian Paul | b2951ff | 2009-09-03 21:47:30 -0600 | [diff] [blame] | 784 | * One-time init for a temp_texture object. |
| 785 | * Choose tex target, compute max tex size, etc. |
| 786 | */ |
| 787 | static void |
| 788 | init_temp_texture(GLcontext *ctx, struct temp_texture *tex) |
| 789 | { |
| 790 | /* prefer texture rectangle */ |
| 791 | if (ctx->Extensions.NV_texture_rectangle) { |
| 792 | tex->Target = GL_TEXTURE_RECTANGLE; |
| 793 | tex->MaxSize = ctx->Const.MaxTextureRectSize; |
| 794 | tex->NPOT = GL_TRUE; |
| 795 | } |
| 796 | else { |
| 797 | /* use 2D texture, NPOT if possible */ |
| 798 | tex->Target = GL_TEXTURE_2D; |
| 799 | tex->MaxSize = 1 << (ctx->Const.MaxTextureLevels - 1); |
| 800 | tex->NPOT = ctx->Extensions.ARB_texture_non_power_of_two; |
| 801 | } |
| 802 | tex->MinSize = 16; /* 16 x 16 at least */ |
| 803 | assert(tex->MaxSize > 0); |
| 804 | |
| 805 | _mesa_GenTextures(1, &tex->TexObj); |
| 806 | _mesa_BindTexture(tex->Target, tex->TexObj); |
| 807 | } |
| 808 | |
| 809 | |
| 810 | /** |
Brian Paul | 886e7b3 | 2009-09-03 22:03:02 -0600 | [diff] [blame] | 811 | * Return pointer to temp_texture info for non-bitmap ops. |
| 812 | * This does some one-time init if needed. |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 813 | */ |
| 814 | static struct temp_texture * |
| 815 | get_temp_texture(GLcontext *ctx) |
| 816 | { |
| 817 | struct temp_texture *tex = &ctx->Meta->TempTex; |
| 818 | |
| 819 | if (!tex->TexObj) { |
Brian Paul | b2951ff | 2009-09-03 21:47:30 -0600 | [diff] [blame] | 820 | init_temp_texture(ctx, tex); |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | return tex; |
| 824 | } |
| 825 | |
| 826 | |
| 827 | /** |
Brian Paul | 886e7b3 | 2009-09-03 22:03:02 -0600 | [diff] [blame] | 828 | * Return pointer to temp_texture info for _mesa_meta_bitmap(). |
| 829 | * We use a separate texture for bitmaps to reduce texture |
| 830 | * allocation/deallocation. |
| 831 | */ |
| 832 | static struct temp_texture * |
| 833 | get_bitmap_temp_texture(GLcontext *ctx) |
| 834 | { |
| 835 | struct temp_texture *tex = &ctx->Meta->Bitmap.Tex; |
| 836 | |
| 837 | if (!tex->TexObj) { |
| 838 | init_temp_texture(ctx, tex); |
| 839 | } |
| 840 | |
| 841 | return tex; |
| 842 | } |
| 843 | |
| 844 | |
| 845 | /** |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 846 | * Compute the width/height of texture needed to draw an image of the |
| 847 | * given size. Return a flag indicating whether the current texture |
| 848 | * can be re-used (glTexSubImage2D) or if a new texture needs to be |
| 849 | * allocated (glTexImage2D). |
| 850 | * Also, compute s/t texcoords for drawing. |
| 851 | * |
| 852 | * \return GL_TRUE if new texture is needed, GL_FALSE otherwise |
| 853 | */ |
| 854 | static GLboolean |
| 855 | alloc_texture(struct temp_texture *tex, |
| 856 | GLsizei width, GLsizei height, GLenum intFormat) |
| 857 | { |
| 858 | GLboolean newTex = GL_FALSE; |
| 859 | |
Brian Paul | b2951ff | 2009-09-03 21:47:30 -0600 | [diff] [blame] | 860 | ASSERT(width <= tex->MaxSize); |
| 861 | ASSERT(height <= tex->MaxSize); |
| 862 | |
Brian Paul | 7a78e6e | 2009-08-31 20:25:10 -0600 | [diff] [blame] | 863 | if (width > tex->Width || |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 864 | height > tex->Height || |
| 865 | intFormat != tex->IntFormat) { |
| 866 | /* alloc new texture (larger or different format) */ |
| 867 | |
| 868 | if (tex->NPOT) { |
| 869 | /* use non-power of two size */ |
Brian Paul | b2951ff | 2009-09-03 21:47:30 -0600 | [diff] [blame] | 870 | tex->Width = MAX2(tex->MinSize, width); |
| 871 | tex->Height = MAX2(tex->MinSize, height); |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 872 | } |
| 873 | else { |
| 874 | /* find power of two size */ |
| 875 | GLsizei w, h; |
Brian Paul | b2951ff | 2009-09-03 21:47:30 -0600 | [diff] [blame] | 876 | w = h = tex->MinSize; |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 877 | while (w < width) |
| 878 | w *= 2; |
| 879 | while (h < height) |
| 880 | h *= 2; |
| 881 | tex->Width = w; |
| 882 | tex->Height = h; |
| 883 | } |
| 884 | |
| 885 | tex->IntFormat = intFormat; |
| 886 | |
| 887 | newTex = GL_TRUE; |
| 888 | } |
| 889 | |
| 890 | /* compute texcoords */ |
| 891 | if (tex->Target == GL_TEXTURE_RECTANGLE) { |
| 892 | tex->Sright = (GLfloat) width; |
| 893 | tex->Ttop = (GLfloat) height; |
| 894 | } |
| 895 | else { |
| 896 | tex->Sright = (GLfloat) width / tex->Width; |
| 897 | tex->Ttop = (GLfloat) height / tex->Height; |
| 898 | } |
| 899 | |
| 900 | return newTex; |
| 901 | } |
| 902 | |
| 903 | |
| 904 | /** |
| 905 | * Setup/load texture for glCopyPixels or glBlitFramebuffer. |
| 906 | */ |
| 907 | static void |
| 908 | setup_copypix_texture(struct temp_texture *tex, |
| 909 | GLboolean newTex, |
| 910 | GLint srcX, GLint srcY, |
| 911 | GLsizei width, GLsizei height, GLenum intFormat, |
| 912 | GLenum filter) |
| 913 | { |
| 914 | _mesa_BindTexture(tex->Target, tex->TexObj); |
| 915 | _mesa_TexParameteri(tex->Target, GL_TEXTURE_MIN_FILTER, filter); |
| 916 | _mesa_TexParameteri(tex->Target, GL_TEXTURE_MAG_FILTER, filter); |
| 917 | _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
| 918 | |
| 919 | /* copy framebuffer image to texture */ |
| 920 | if (newTex) { |
| 921 | /* create new tex image */ |
| 922 | if (tex->Width == width && tex->Height == height) { |
| 923 | /* create new tex with framebuffer data */ |
| 924 | _mesa_CopyTexImage2D(tex->Target, 0, tex->IntFormat, |
| 925 | srcX, srcY, width, height, 0); |
| 926 | } |
| 927 | else { |
| 928 | /* create empty texture */ |
| 929 | _mesa_TexImage2D(tex->Target, 0, tex->IntFormat, |
| 930 | tex->Width, tex->Height, 0, |
| 931 | intFormat, GL_UNSIGNED_BYTE, NULL); |
| 932 | /* load image */ |
| 933 | _mesa_CopyTexSubImage2D(tex->Target, 0, |
| 934 | 0, 0, srcX, srcY, width, height); |
| 935 | } |
| 936 | } |
| 937 | else { |
| 938 | /* replace existing tex image */ |
| 939 | _mesa_CopyTexSubImage2D(tex->Target, 0, |
| 940 | 0, 0, srcX, srcY, width, height); |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | |
| 945 | /** |
| 946 | * Setup/load texture for glDrawPixels. |
| 947 | */ |
| 948 | static void |
Eric Anholt | d492e7b | 2009-09-28 14:03:40 -0700 | [diff] [blame] | 949 | setup_drawpix_texture(GLcontext *ctx, |
| 950 | struct temp_texture *tex, |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 951 | GLboolean newTex, |
| 952 | GLenum texIntFormat, |
| 953 | GLsizei width, GLsizei height, |
| 954 | GLenum format, GLenum type, |
| 955 | const GLvoid *pixels) |
| 956 | { |
| 957 | _mesa_BindTexture(tex->Target, tex->TexObj); |
| 958 | _mesa_TexParameteri(tex->Target, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 959 | _mesa_TexParameteri(tex->Target, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 960 | _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
| 961 | |
| 962 | /* copy pixel data to texture */ |
| 963 | if (newTex) { |
| 964 | /* create new tex image */ |
| 965 | if (tex->Width == width && tex->Height == height) { |
| 966 | /* create new tex and load image data */ |
| 967 | _mesa_TexImage2D(tex->Target, 0, tex->IntFormat, |
| 968 | tex->Width, tex->Height, 0, format, type, pixels); |
| 969 | } |
| 970 | else { |
Eric Anholt | d492e7b | 2009-09-28 14:03:40 -0700 | [diff] [blame] | 971 | struct gl_buffer_object *save_unpack_obj = NULL; |
| 972 | |
| 973 | _mesa_reference_buffer_object(ctx, &save_unpack_obj, |
| 974 | ctx->Unpack.BufferObj); |
| 975 | _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0); |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 976 | /* create empty texture */ |
| 977 | _mesa_TexImage2D(tex->Target, 0, tex->IntFormat, |
| 978 | tex->Width, tex->Height, 0, format, type, NULL); |
Eric Anholt | d492e7b | 2009-09-28 14:03:40 -0700 | [diff] [blame] | 979 | if (save_unpack_obj != NULL) |
| 980 | _mesa_BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, |
| 981 | save_unpack_obj->Name); |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 982 | /* load image */ |
| 983 | _mesa_TexSubImage2D(tex->Target, 0, |
| 984 | 0, 0, width, height, format, type, pixels); |
| 985 | } |
| 986 | } |
| 987 | else { |
| 988 | /* replace existing tex image */ |
| 989 | _mesa_TexSubImage2D(tex->Target, 0, |
| 990 | 0, 0, width, height, format, type, pixels); |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | |
| 995 | |
| 996 | /** |
Brian Paul | 0566122 | 2009-08-31 21:13:41 -0600 | [diff] [blame] | 997 | * One-time init for drawing depth pixels. |
| 998 | */ |
| 999 | static void |
| 1000 | init_blit_depth_pixels(GLcontext *ctx) |
| 1001 | { |
| 1002 | static const char *program = |
| 1003 | "!!ARBfp1.0\n" |
| 1004 | "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n" |
| 1005 | "END \n"; |
| 1006 | char program2[200]; |
| 1007 | struct blit_state *blit = &ctx->Meta->Blit; |
| 1008 | struct temp_texture *tex = get_temp_texture(ctx); |
| 1009 | const char *texTarget; |
| 1010 | |
| 1011 | assert(blit->DepthFP == 0); |
| 1012 | |
| 1013 | /* replace %s with "RECT" or "2D" */ |
| 1014 | assert(strlen(program) + 4 < sizeof(program2)); |
| 1015 | if (tex->Target == GL_TEXTURE_RECTANGLE) |
| 1016 | texTarget = "RECT"; |
| 1017 | else |
| 1018 | texTarget = "2D"; |
| 1019 | _mesa_snprintf(program2, sizeof(program2), program, texTarget); |
| 1020 | |
| 1021 | _mesa_GenPrograms(1, &blit->DepthFP); |
| 1022 | _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, blit->DepthFP); |
| 1023 | _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, |
| 1024 | strlen(program2), (const GLubyte *) program2); |
| 1025 | } |
| 1026 | |
| 1027 | |
| 1028 | /** |
Brian Paul | 05ec586 | 2009-10-27 14:16:59 -0600 | [diff] [blame] | 1029 | * Try to do a glBlitFramebuffer using no-copy texturing. |
Brian Paul | be16aca | 2009-10-02 13:59:41 -0600 | [diff] [blame] | 1030 | * We can do this when the src renderbuffer is actually a texture. |
| 1031 | * But if the src buffer == dst buffer we cannot do this. |
| 1032 | * |
| 1033 | * \return new buffer mask indicating the buffers left to blit using the |
| 1034 | * normal path. |
| 1035 | */ |
| 1036 | static GLbitfield |
| 1037 | blitframebuffer_texture(GLcontext *ctx, |
| 1038 | GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, |
| 1039 | GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, |
| 1040 | GLbitfield mask, GLenum filter) |
| 1041 | { |
| 1042 | if (mask & GL_COLOR_BUFFER_BIT) { |
| 1043 | const struct gl_framebuffer *drawFb = ctx->DrawBuffer; |
| 1044 | const struct gl_framebuffer *readFb = ctx->ReadBuffer; |
| 1045 | const struct gl_renderbuffer_attachment *drawAtt = |
| 1046 | &drawFb->Attachment[drawFb->_ColorDrawBufferIndexes[0]]; |
| 1047 | const struct gl_renderbuffer_attachment *readAtt = |
| 1048 | &readFb->Attachment[readFb->_ColorReadBufferIndex]; |
| 1049 | |
| 1050 | if (readAtt && readAtt->Texture) { |
| 1051 | const struct gl_texture_object *texObj = readAtt->Texture; |
Brian Paul | 3f928b3 | 2009-10-12 18:11:31 -0600 | [diff] [blame] | 1052 | const GLuint srcLevel = readAtt->TextureLevel; |
Brian Paul | be16aca | 2009-10-02 13:59:41 -0600 | [diff] [blame] | 1053 | const GLenum minFilterSave = texObj->MinFilter; |
| 1054 | const GLenum magFilterSave = texObj->MagFilter; |
Brian Paul | 3f928b3 | 2009-10-12 18:11:31 -0600 | [diff] [blame] | 1055 | const GLint baseLevelSave = texObj->BaseLevel; |
| 1056 | const GLint maxLevelSave = texObj->MaxLevel; |
| 1057 | const GLenum wrapSSave = texObj->WrapS; |
| 1058 | const GLenum wrapTSave = texObj->WrapT; |
Brian Paul | be16aca | 2009-10-02 13:59:41 -0600 | [diff] [blame] | 1059 | const GLenum target = texObj->Target; |
| 1060 | |
| 1061 | if (drawAtt->Texture == readAtt->Texture) { |
| 1062 | /* Can't use same texture as both the source and dest. We need |
| 1063 | * to handle overlapping blits and besides, some hw may not |
| 1064 | * support this. |
| 1065 | */ |
| 1066 | return mask; |
| 1067 | } |
| 1068 | |
| 1069 | if (target != GL_TEXTURE_2D && target != GL_TEXTURE_RECTANGLE_ARB) { |
| 1070 | /* Can't handle other texture types at this time */ |
| 1071 | return mask; |
| 1072 | } |
| 1073 | |
| 1074 | /* |
| 1075 | printf("Blit from texture!\n"); |
| 1076 | printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt); |
| 1077 | printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture); |
| 1078 | */ |
| 1079 | |
| 1080 | /* Prepare src texture state */ |
| 1081 | _mesa_BindTexture(target, texObj->Name); |
| 1082 | _mesa_TexParameteri(target, GL_TEXTURE_MIN_FILTER, filter); |
| 1083 | _mesa_TexParameteri(target, GL_TEXTURE_MAG_FILTER, filter); |
Brian Paul | 3f928b3 | 2009-10-12 18:11:31 -0600 | [diff] [blame] | 1084 | _mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, srcLevel); |
| 1085 | _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, srcLevel); |
| 1086 | _mesa_TexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 1087 | _mesa_TexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
Brian Paul | be16aca | 2009-10-02 13:59:41 -0600 | [diff] [blame] | 1088 | _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
Brian Paul | be16aca | 2009-10-02 13:59:41 -0600 | [diff] [blame] | 1089 | _mesa_set_enable(ctx, target, GL_TRUE); |
| 1090 | |
| 1091 | /* Prepare vertex data (the VBO was previously created and bound) */ |
| 1092 | { |
| 1093 | struct vertex { |
| 1094 | GLfloat x, y, s, t; |
| 1095 | }; |
| 1096 | struct vertex verts[4]; |
| 1097 | GLfloat s0, t0, s1, t1; |
| 1098 | |
| 1099 | if (target == GL_TEXTURE_2D) { |
| 1100 | const struct gl_texture_image *texImage |
Brian Paul | 3f928b3 | 2009-10-12 18:11:31 -0600 | [diff] [blame] | 1101 | = _mesa_select_tex_image(ctx, texObj, target, srcLevel); |
Brian Paul | be16aca | 2009-10-02 13:59:41 -0600 | [diff] [blame] | 1102 | s0 = srcX0 / (float) texImage->Width; |
| 1103 | s1 = srcX1 / (float) texImage->Width; |
| 1104 | t0 = srcY0 / (float) texImage->Height; |
| 1105 | t1 = srcY1 / (float) texImage->Height; |
| 1106 | } |
| 1107 | else { |
| 1108 | assert(target == GL_TEXTURE_RECTANGLE_ARB); |
| 1109 | s0 = srcX0; |
| 1110 | s1 = srcX1; |
| 1111 | t0 = srcY0; |
| 1112 | t1 = srcY1; |
| 1113 | } |
| 1114 | |
| 1115 | verts[0].x = (GLfloat) dstX0; |
| 1116 | verts[0].y = (GLfloat) dstY0; |
| 1117 | verts[1].x = (GLfloat) dstX1; |
| 1118 | verts[1].y = (GLfloat) dstY0; |
| 1119 | verts[2].x = (GLfloat) dstX1; |
| 1120 | verts[2].y = (GLfloat) dstY1; |
| 1121 | verts[3].x = (GLfloat) dstX0; |
| 1122 | verts[3].y = (GLfloat) dstY1; |
| 1123 | |
| 1124 | verts[0].s = s0; |
| 1125 | verts[0].t = t0; |
| 1126 | verts[1].s = s1; |
| 1127 | verts[1].t = t0; |
| 1128 | verts[2].s = s1; |
| 1129 | verts[2].t = t1; |
| 1130 | verts[3].s = s0; |
| 1131 | verts[3].t = t1; |
| 1132 | |
| 1133 | _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts); |
| 1134 | } |
| 1135 | |
| 1136 | _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 1137 | |
Brian Paul | 3f928b3 | 2009-10-12 18:11:31 -0600 | [diff] [blame] | 1138 | /* Restore texture object state, the texture binding will |
Brian Paul | be16aca | 2009-10-02 13:59:41 -0600 | [diff] [blame] | 1139 | * be restored by _mesa_meta_end(). |
| 1140 | */ |
| 1141 | _mesa_TexParameteri(target, GL_TEXTURE_MIN_FILTER, minFilterSave); |
| 1142 | _mesa_TexParameteri(target, GL_TEXTURE_MAG_FILTER, magFilterSave); |
Brian Paul | 3f928b3 | 2009-10-12 18:11:31 -0600 | [diff] [blame] | 1143 | _mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, baseLevelSave); |
| 1144 | _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, maxLevelSave); |
| 1145 | _mesa_TexParameteri(target, GL_TEXTURE_WRAP_S, wrapSSave); |
| 1146 | _mesa_TexParameteri(target, GL_TEXTURE_WRAP_T, wrapTSave); |
Brian Paul | be16aca | 2009-10-02 13:59:41 -0600 | [diff] [blame] | 1147 | |
| 1148 | /* Done with color buffer */ |
| 1149 | mask &= ~GL_COLOR_BUFFER_BIT; |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | return mask; |
| 1154 | } |
| 1155 | |
| 1156 | |
| 1157 | /** |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 1158 | * Meta implementation of ctx->Driver.BlitFramebuffer() in terms |
| 1159 | * of texture mapping and polygon rendering. |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 1160 | */ |
| 1161 | void |
Brian Paul | 4de8e21 | 2009-09-19 17:06:15 -0600 | [diff] [blame] | 1162 | _mesa_meta_BlitFramebuffer(GLcontext *ctx, |
| 1163 | GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, |
| 1164 | GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, |
| 1165 | GLbitfield mask, GLenum filter) |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 1166 | { |
| 1167 | struct blit_state *blit = &ctx->Meta->Blit; |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 1168 | struct temp_texture *tex = get_temp_texture(ctx); |
| 1169 | const GLsizei maxTexSize = tex->MaxSize; |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 1170 | const GLint srcX = MIN2(srcX0, srcX1); |
| 1171 | const GLint srcY = MIN2(srcY0, srcY1); |
| 1172 | const GLint srcW = abs(srcX1 - srcX0); |
| 1173 | const GLint srcH = abs(srcY1 - srcY0); |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 1174 | const GLboolean srcFlipX = srcX1 < srcX0; |
| 1175 | const GLboolean srcFlipY = srcY1 < srcY0; |
Brian Paul | 0ef5b62 | 2009-09-10 19:56:35 -0600 | [diff] [blame] | 1176 | struct vertex { |
| 1177 | GLfloat x, y, s, t; |
| 1178 | }; |
| 1179 | struct vertex verts[4]; |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 1180 | GLboolean newTex; |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 1181 | |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 1182 | if (srcW > maxTexSize || srcH > maxTexSize) { |
Brian Paul | 2ad10c9 | 2009-08-10 15:05:53 -0600 | [diff] [blame] | 1183 | /* XXX avoid this fallback */ |
| 1184 | _swrast_BlitFramebuffer(ctx, srcX0, srcY0, srcX1, srcY1, |
| 1185 | dstX0, dstY0, dstX1, dstY1, mask, filter); |
| 1186 | return; |
| 1187 | } |
| 1188 | |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 1189 | if (srcFlipX) { |
| 1190 | GLint tmp = dstX0; |
| 1191 | dstX0 = dstX1; |
| 1192 | dstX1 = tmp; |
| 1193 | } |
| 1194 | |
| 1195 | if (srcFlipY) { |
| 1196 | GLint tmp = dstY0; |
| 1197 | dstY0 = dstY1; |
| 1198 | dstY1 = tmp; |
| 1199 | } |
| 1200 | |
| 1201 | /* only scissor effects blit so save/clear all other relevant state */ |
| 1202 | _mesa_meta_begin(ctx, ~META_SCISSOR); |
| 1203 | |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 1204 | if (blit->ArrayObj == 0) { |
| 1205 | /* one-time setup */ |
| 1206 | |
| 1207 | /* create vertex array object */ |
| 1208 | _mesa_GenVertexArrays(1, &blit->ArrayObj); |
| 1209 | _mesa_BindVertexArray(blit->ArrayObj); |
| 1210 | |
| 1211 | /* create vertex array buffer */ |
| 1212 | _mesa_GenBuffersARB(1, &blit->VBO); |
| 1213 | _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, blit->VBO); |
Brian Paul | 5b0f7c6 | 2009-08-31 19:49:40 -0600 | [diff] [blame] | 1214 | _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts), |
| 1215 | NULL, GL_DYNAMIC_DRAW_ARB); |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 1216 | |
| 1217 | /* setup vertex arrays */ |
Brian Paul | 0ef5b62 | 2009-09-10 19:56:35 -0600 | [diff] [blame] | 1218 | _mesa_VertexPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(x)); |
| 1219 | _mesa_TexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(s)); |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 1220 | _mesa_EnableClientState(GL_VERTEX_ARRAY); |
| 1221 | _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 1222 | } |
| 1223 | else { |
| 1224 | _mesa_BindVertexArray(blit->ArrayObj); |
| 1225 | _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, blit->VBO); |
| 1226 | } |
| 1227 | |
Brian Paul | be16aca | 2009-10-02 13:59:41 -0600 | [diff] [blame] | 1228 | /* Try faster, direct texture approach first */ |
| 1229 | mask = blitframebuffer_texture(ctx, srcX0, srcY0, srcX1, srcY1, |
| 1230 | dstX0, dstY0, dstX1, dstY1, mask, filter); |
| 1231 | if (mask == 0x0) { |
| 1232 | _mesa_meta_end(ctx); |
| 1233 | return; |
| 1234 | } |
| 1235 | |
| 1236 | /* Continue with "normal" approach which involves copying the src rect |
| 1237 | * into a temporary texture and is "blitted" by drawing a textured quad. |
| 1238 | */ |
| 1239 | |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 1240 | newTex = alloc_texture(tex, srcW, srcH, GL_RGBA); |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 1241 | |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 1242 | /* vertex positions/texcoords (after texture allocation!) */ |
| 1243 | { |
Brian Paul | 0ef5b62 | 2009-09-10 19:56:35 -0600 | [diff] [blame] | 1244 | verts[0].x = (GLfloat) dstX0; |
| 1245 | verts[0].y = (GLfloat) dstY0; |
| 1246 | verts[1].x = (GLfloat) dstX1; |
| 1247 | verts[1].y = (GLfloat) dstY0; |
| 1248 | verts[2].x = (GLfloat) dstX1; |
| 1249 | verts[2].y = (GLfloat) dstY1; |
| 1250 | verts[3].x = (GLfloat) dstX0; |
| 1251 | verts[3].y = (GLfloat) dstY1; |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 1252 | |
Brian Paul | 0ef5b62 | 2009-09-10 19:56:35 -0600 | [diff] [blame] | 1253 | verts[0].s = 0.0F; |
| 1254 | verts[0].t = 0.0F; |
| 1255 | verts[1].s = tex->Sright; |
| 1256 | verts[1].t = 0.0F; |
| 1257 | verts[2].s = tex->Sright; |
| 1258 | verts[2].t = tex->Ttop; |
| 1259 | verts[3].s = 0.0F; |
| 1260 | verts[3].t = tex->Ttop; |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 1261 | |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 1262 | /* upload new vertex data */ |
Brian Paul | 5b0f7c6 | 2009-08-31 19:49:40 -0600 | [diff] [blame] | 1263 | _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts); |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 1264 | } |
| 1265 | |
Brian Paul | 0e5293a | 2009-09-03 21:30:06 -0600 | [diff] [blame] | 1266 | _mesa_set_enable(ctx, tex->Target, GL_TRUE); |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 1267 | |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 1268 | if (mask & GL_COLOR_BUFFER_BIT) { |
| 1269 | setup_copypix_texture(tex, newTex, srcX, srcY, srcW, srcH, |
| 1270 | GL_RGBA, filter); |
| 1271 | _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 1272 | mask &= ~GL_COLOR_BUFFER_BIT; |
| 1273 | } |
Brian Paul | 0566122 | 2009-08-31 21:13:41 -0600 | [diff] [blame] | 1274 | |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 1275 | if (mask & GL_DEPTH_BUFFER_BIT) { |
Brian Paul | 0566122 | 2009-08-31 21:13:41 -0600 | [diff] [blame] | 1276 | GLuint *tmp = (GLuint *) _mesa_malloc(srcW * srcH * sizeof(GLuint)); |
| 1277 | if (tmp) { |
| 1278 | if (!blit->DepthFP) |
| 1279 | init_blit_depth_pixels(ctx); |
| 1280 | |
| 1281 | /* maybe change tex format here */ |
| 1282 | newTex = alloc_texture(tex, srcW, srcH, GL_DEPTH_COMPONENT); |
| 1283 | |
| 1284 | _mesa_ReadPixels(srcX, srcY, srcW, srcH, |
| 1285 | GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, tmp); |
| 1286 | |
Eric Anholt | d492e7b | 2009-09-28 14:03:40 -0700 | [diff] [blame] | 1287 | setup_drawpix_texture(ctx, tex, newTex, GL_DEPTH_COMPONENT, srcW, srcH, |
Brian Paul | 0566122 | 2009-08-31 21:13:41 -0600 | [diff] [blame] | 1288 | GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, tmp); |
| 1289 | |
| 1290 | _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, blit->DepthFP); |
| 1291 | _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_TRUE); |
| 1292 | _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); |
| 1293 | _mesa_set_enable(ctx, GL_DEPTH_TEST, GL_TRUE); |
| 1294 | _mesa_DepthFunc(GL_ALWAYS); |
| 1295 | _mesa_DepthMask(GL_TRUE); |
| 1296 | |
| 1297 | _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 1298 | mask &= ~GL_DEPTH_BUFFER_BIT; |
| 1299 | |
| 1300 | _mesa_free(tmp); |
| 1301 | } |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 1302 | } |
Brian Paul | 0566122 | 2009-08-31 21:13:41 -0600 | [diff] [blame] | 1303 | |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 1304 | if (mask & GL_STENCIL_BUFFER_BIT) { |
| 1305 | /* XXX can't easily do stencil */ |
| 1306 | } |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 1307 | |
Brian Paul | 0e5293a | 2009-09-03 21:30:06 -0600 | [diff] [blame] | 1308 | _mesa_set_enable(ctx, tex->Target, GL_FALSE); |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 1309 | |
| 1310 | _mesa_meta_end(ctx); |
| 1311 | |
Brian Paul | 0566122 | 2009-08-31 21:13:41 -0600 | [diff] [blame] | 1312 | if (mask) { |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 1313 | _swrast_BlitFramebuffer(ctx, srcX0, srcY0, srcX1, srcY1, |
| 1314 | dstX0, dstY0, dstX1, dstY1, mask, filter); |
| 1315 | } |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | |
| 1319 | /** |
| 1320 | * Meta implementation of ctx->Driver.Clear() in terms of polygon rendering. |
| 1321 | */ |
| 1322 | void |
Brian Paul | 4de8e21 | 2009-09-19 17:06:15 -0600 | [diff] [blame] | 1323 | _mesa_meta_Clear(GLcontext *ctx, GLbitfield buffers) |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 1324 | { |
| 1325 | struct clear_state *clear = &ctx->Meta->Clear; |
Brian Paul | 0ef5b62 | 2009-09-10 19:56:35 -0600 | [diff] [blame] | 1326 | struct vertex { |
| 1327 | GLfloat x, y, z, r, g, b, a; |
| 1328 | }; |
| 1329 | struct vertex verts[4]; |
Brian Paul | 0566122 | 2009-08-31 21:13:41 -0600 | [diff] [blame] | 1330 | /* save all state but scissor, pixel pack/unpack */ |
| 1331 | GLbitfield metaSave = META_ALL - META_SCISSOR - META_PIXEL_STORE; |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 1332 | |
Brian Paul | 1fc713a | 2009-08-31 19:28:11 -0600 | [diff] [blame] | 1333 | if (buffers & BUFFER_BITS_COLOR) { |
| 1334 | /* if clearing color buffers, don't save/restore colormask */ |
| 1335 | metaSave -= META_COLOR_MASK; |
| 1336 | } |
| 1337 | |
| 1338 | _mesa_meta_begin(ctx, metaSave); |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 1339 | |
| 1340 | if (clear->ArrayObj == 0) { |
| 1341 | /* one-time setup */ |
| 1342 | |
| 1343 | /* create vertex array object */ |
| 1344 | _mesa_GenVertexArrays(1, &clear->ArrayObj); |
| 1345 | _mesa_BindVertexArray(clear->ArrayObj); |
| 1346 | |
| 1347 | /* create vertex array buffer */ |
| 1348 | _mesa_GenBuffersARB(1, &clear->VBO); |
| 1349 | _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, clear->VBO); |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 1350 | |
| 1351 | /* setup vertex arrays */ |
Brian Paul | 0ef5b62 | 2009-09-10 19:56:35 -0600 | [diff] [blame] | 1352 | _mesa_VertexPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(x)); |
| 1353 | _mesa_ColorPointer(4, GL_FLOAT, sizeof(struct vertex), OFFSET(r)); |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 1354 | _mesa_EnableClientState(GL_VERTEX_ARRAY); |
| 1355 | _mesa_EnableClientState(GL_COLOR_ARRAY); |
| 1356 | } |
| 1357 | else { |
| 1358 | _mesa_BindVertexArray(clear->ArrayObj); |
| 1359 | _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, clear->VBO); |
| 1360 | } |
| 1361 | |
| 1362 | /* GL_COLOR_BUFFER_BIT */ |
| 1363 | if (buffers & BUFFER_BITS_COLOR) { |
| 1364 | /* leave colormask, glDrawBuffer state as-is */ |
| 1365 | } |
| 1366 | else { |
Brian Paul | 1fc713a | 2009-08-31 19:28:11 -0600 | [diff] [blame] | 1367 | ASSERT(metaSave & META_COLOR_MASK); |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 1368 | _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); |
| 1369 | } |
| 1370 | |
| 1371 | /* GL_DEPTH_BUFFER_BIT */ |
| 1372 | if (buffers & BUFFER_BIT_DEPTH) { |
| 1373 | _mesa_set_enable(ctx, GL_DEPTH_TEST, GL_TRUE); |
| 1374 | _mesa_DepthFunc(GL_ALWAYS); |
| 1375 | _mesa_DepthMask(GL_TRUE); |
| 1376 | } |
| 1377 | else { |
| 1378 | assert(!ctx->Depth.Test); |
| 1379 | } |
| 1380 | |
| 1381 | /* GL_STENCIL_BUFFER_BIT */ |
| 1382 | if (buffers & BUFFER_BIT_STENCIL) { |
| 1383 | _mesa_set_enable(ctx, GL_STENCIL_TEST, GL_TRUE); |
| 1384 | _mesa_StencilOpSeparate(GL_FRONT_AND_BACK, |
| 1385 | GL_REPLACE, GL_REPLACE, GL_REPLACE); |
| 1386 | _mesa_StencilFuncSeparate(GL_FRONT_AND_BACK, GL_ALWAYS, |
| 1387 | ctx->Stencil.Clear & 0x7fffffff, |
| 1388 | ctx->Stencil.WriteMask[0]); |
| 1389 | } |
| 1390 | else { |
| 1391 | assert(!ctx->Stencil.Enabled); |
| 1392 | } |
| 1393 | |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 1394 | /* vertex positions/colors */ |
| 1395 | { |
| 1396 | const GLfloat x0 = (GLfloat) ctx->DrawBuffer->_Xmin; |
| 1397 | const GLfloat y0 = (GLfloat) ctx->DrawBuffer->_Ymin; |
| 1398 | const GLfloat x1 = (GLfloat) ctx->DrawBuffer->_Xmax; |
| 1399 | const GLfloat y1 = (GLfloat) ctx->DrawBuffer->_Ymax; |
| 1400 | const GLfloat z = 1.0 - 2.0 * ctx->Depth.Clear; |
| 1401 | GLuint i; |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 1402 | |
Brian Paul | 0ef5b62 | 2009-09-10 19:56:35 -0600 | [diff] [blame] | 1403 | verts[0].x = x0; |
| 1404 | verts[0].y = y0; |
| 1405 | verts[0].z = z; |
| 1406 | verts[1].x = x1; |
| 1407 | verts[1].y = y0; |
| 1408 | verts[1].z = z; |
| 1409 | verts[2].x = x1; |
| 1410 | verts[2].y = y1; |
| 1411 | verts[2].z = z; |
| 1412 | verts[3].x = x0; |
| 1413 | verts[3].y = y1; |
| 1414 | verts[3].z = z; |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 1415 | |
| 1416 | /* vertex colors */ |
| 1417 | for (i = 0; i < 4; i++) { |
Brian Paul | 0ef5b62 | 2009-09-10 19:56:35 -0600 | [diff] [blame] | 1418 | verts[i].r = ctx->Color.ClearColor[0]; |
| 1419 | verts[i].g = ctx->Color.ClearColor[1]; |
| 1420 | verts[i].b = ctx->Color.ClearColor[2]; |
| 1421 | verts[i].a = ctx->Color.ClearColor[3]; |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | /* upload new vertex data */ |
Eric Anholt | cbd20e1 | 2009-10-19 17:54:42 -0700 | [diff] [blame] | 1425 | _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts), verts, |
| 1426 | GL_DYNAMIC_DRAW_ARB); |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 1427 | } |
| 1428 | |
Brian Paul | 33a838b | 2009-08-10 13:35:58 -0600 | [diff] [blame] | 1429 | /* draw quad */ |
| 1430 | _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 1431 | |
| 1432 | _mesa_meta_end(ctx); |
| 1433 | } |
| 1434 | |
Brian Paul | c16fa38 | 2009-08-10 14:43:15 -0600 | [diff] [blame] | 1435 | |
| 1436 | /** |
| 1437 | * Meta implementation of ctx->Driver.CopyPixels() in terms |
| 1438 | * of texture mapping and polygon rendering. |
Brian Paul | c16fa38 | 2009-08-10 14:43:15 -0600 | [diff] [blame] | 1439 | */ |
| 1440 | void |
Brian Paul | 4de8e21 | 2009-09-19 17:06:15 -0600 | [diff] [blame] | 1441 | _mesa_meta_CopyPixels(GLcontext *ctx, GLint srcX, GLint srcY, |
| 1442 | GLsizei width, GLsizei height, |
| 1443 | GLint dstX, GLint dstY, GLenum type) |
Brian Paul | c16fa38 | 2009-08-10 14:43:15 -0600 | [diff] [blame] | 1444 | { |
Brian Paul | c16fa38 | 2009-08-10 14:43:15 -0600 | [diff] [blame] | 1445 | struct copypix_state *copypix = &ctx->Meta->CopyPix; |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 1446 | struct temp_texture *tex = get_temp_texture(ctx); |
Brian Paul | 0ef5b62 | 2009-09-10 19:56:35 -0600 | [diff] [blame] | 1447 | struct vertex { |
| 1448 | GLfloat x, y, z, s, t; |
| 1449 | }; |
| 1450 | struct vertex verts[4]; |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 1451 | GLboolean newTex; |
| 1452 | GLenum intFormat = GL_RGBA; |
Brian Paul | c16fa38 | 2009-08-10 14:43:15 -0600 | [diff] [blame] | 1453 | |
| 1454 | if (type != GL_COLOR || |
| 1455 | ctx->_ImageTransferState || |
Brian Paul | f821866 | 2009-08-11 18:54:57 -0600 | [diff] [blame] | 1456 | ctx->Fog.Enabled || |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 1457 | width > tex->MaxSize || |
| 1458 | height > tex->MaxSize) { |
Brian Paul | c16fa38 | 2009-08-10 14:43:15 -0600 | [diff] [blame] | 1459 | /* XXX avoid this fallback */ |
| 1460 | _swrast_CopyPixels(ctx, srcX, srcY, width, height, dstX, dstY, type); |
| 1461 | return; |
| 1462 | } |
| 1463 | |
| 1464 | /* Most GL state applies to glCopyPixels, but a there's a few things |
| 1465 | * we need to override: |
| 1466 | */ |
| 1467 | _mesa_meta_begin(ctx, (META_RASTERIZATION | |
| 1468 | META_SHADER | |
Brian Paul | edb991b | 2009-08-10 15:44:05 -0600 | [diff] [blame] | 1469 | META_TEXTURE | |
Brian Paul | c16fa38 | 2009-08-10 14:43:15 -0600 | [diff] [blame] | 1470 | META_TRANSFORM | |
Brian Paul | edb991b | 2009-08-10 15:44:05 -0600 | [diff] [blame] | 1471 | META_VERTEX | |
| 1472 | META_VIEWPORT)); |
Brian Paul | c16fa38 | 2009-08-10 14:43:15 -0600 | [diff] [blame] | 1473 | |
Brian Paul | c16fa38 | 2009-08-10 14:43:15 -0600 | [diff] [blame] | 1474 | if (copypix->ArrayObj == 0) { |
| 1475 | /* one-time setup */ |
| 1476 | |
| 1477 | /* create vertex array object */ |
| 1478 | _mesa_GenVertexArrays(1, ©pix->ArrayObj); |
| 1479 | _mesa_BindVertexArray(copypix->ArrayObj); |
| 1480 | |
| 1481 | /* create vertex array buffer */ |
| 1482 | _mesa_GenBuffersARB(1, ©pix->VBO); |
| 1483 | _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, copypix->VBO); |
Brian Paul | 5b0f7c6 | 2009-08-31 19:49:40 -0600 | [diff] [blame] | 1484 | _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts), |
| 1485 | NULL, GL_DYNAMIC_DRAW_ARB); |
Brian Paul | c16fa38 | 2009-08-10 14:43:15 -0600 | [diff] [blame] | 1486 | |
| 1487 | /* setup vertex arrays */ |
Brian Paul | 0ef5b62 | 2009-09-10 19:56:35 -0600 | [diff] [blame] | 1488 | _mesa_VertexPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(x)); |
| 1489 | _mesa_TexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(s)); |
Brian Paul | c16fa38 | 2009-08-10 14:43:15 -0600 | [diff] [blame] | 1490 | _mesa_EnableClientState(GL_VERTEX_ARRAY); |
| 1491 | _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 1492 | } |
| 1493 | else { |
| 1494 | _mesa_BindVertexArray(copypix->ArrayObj); |
| 1495 | _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, copypix->VBO); |
| 1496 | } |
| 1497 | |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 1498 | newTex = alloc_texture(tex, width, height, intFormat); |
Brian Paul | c16fa38 | 2009-08-10 14:43:15 -0600 | [diff] [blame] | 1499 | |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 1500 | /* vertex positions, texcoords (after texture allocation!) */ |
| 1501 | { |
| 1502 | const GLfloat dstX0 = (GLfloat) dstX; |
| 1503 | const GLfloat dstY0 = (GLfloat) dstY; |
| 1504 | const GLfloat dstX1 = dstX + width * ctx->Pixel.ZoomX; |
| 1505 | const GLfloat dstY1 = dstY + height * ctx->Pixel.ZoomY; |
| 1506 | const GLfloat z = ctx->Current.RasterPos[2]; |
Brian Paul | c16fa38 | 2009-08-10 14:43:15 -0600 | [diff] [blame] | 1507 | |
Brian Paul | 0ef5b62 | 2009-09-10 19:56:35 -0600 | [diff] [blame] | 1508 | verts[0].x = dstX0; |
| 1509 | verts[0].y = dstY0; |
| 1510 | verts[0].z = z; |
| 1511 | verts[0].s = 0.0F; |
| 1512 | verts[0].t = 0.0F; |
| 1513 | verts[1].x = dstX1; |
| 1514 | verts[1].y = dstY0; |
| 1515 | verts[1].z = z; |
| 1516 | verts[1].s = tex->Sright; |
| 1517 | verts[1].t = 0.0F; |
| 1518 | verts[2].x = dstX1; |
| 1519 | verts[2].y = dstY1; |
| 1520 | verts[2].z = z; |
| 1521 | verts[2].s = tex->Sright; |
| 1522 | verts[2].t = tex->Ttop; |
| 1523 | verts[3].x = dstX0; |
| 1524 | verts[3].y = dstY1; |
| 1525 | verts[3].z = z; |
| 1526 | verts[3].s = 0.0F; |
| 1527 | verts[3].t = tex->Ttop; |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 1528 | |
| 1529 | /* upload new vertex data */ |
Brian Paul | 5b0f7c6 | 2009-08-31 19:49:40 -0600 | [diff] [blame] | 1530 | _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts); |
Brian Paul | c16fa38 | 2009-08-10 14:43:15 -0600 | [diff] [blame] | 1531 | } |
| 1532 | |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 1533 | /* Alloc/setup texture */ |
| 1534 | setup_copypix_texture(tex, newTex, srcX, srcY, width, height, |
| 1535 | GL_RGBA, GL_NEAREST); |
| 1536 | |
Brian Paul | 0e5293a | 2009-09-03 21:30:06 -0600 | [diff] [blame] | 1537 | _mesa_set_enable(ctx, tex->Target, GL_TRUE); |
Brian Paul | c16fa38 | 2009-08-10 14:43:15 -0600 | [diff] [blame] | 1538 | |
| 1539 | /* draw textured quad */ |
| 1540 | _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 1541 | |
Brian Paul | 0e5293a | 2009-09-03 21:30:06 -0600 | [diff] [blame] | 1542 | _mesa_set_enable(ctx, tex->Target, GL_FALSE); |
Brian Paul | c16fa38 | 2009-08-10 14:43:15 -0600 | [diff] [blame] | 1543 | |
| 1544 | _mesa_meta_end(ctx); |
| 1545 | } |
Brian Paul | dba6d52 | 2009-08-11 18:56:26 -0600 | [diff] [blame] | 1546 | |
| 1547 | |
| 1548 | |
| 1549 | /** |
Brian Paul | fd90d80 | 2009-08-11 19:30:05 -0600 | [diff] [blame] | 1550 | * When the glDrawPixels() image size is greater than the max rectangle |
| 1551 | * texture size we use this function to break the glDrawPixels() image |
| 1552 | * into tiles which fit into the max texture size. |
| 1553 | */ |
| 1554 | static void |
| 1555 | tiled_draw_pixels(GLcontext *ctx, |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 1556 | GLint tileSize, |
Brian Paul | fd90d80 | 2009-08-11 19:30:05 -0600 | [diff] [blame] | 1557 | GLint x, GLint y, GLsizei width, GLsizei height, |
| 1558 | GLenum format, GLenum type, |
| 1559 | const struct gl_pixelstore_attrib *unpack, |
| 1560 | const GLvoid *pixels) |
| 1561 | { |
Brian Paul | fd90d80 | 2009-08-11 19:30:05 -0600 | [diff] [blame] | 1562 | struct gl_pixelstore_attrib tileUnpack = *unpack; |
| 1563 | GLint i, j; |
| 1564 | |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 1565 | if (tileUnpack.RowLength == 0) |
| 1566 | tileUnpack.RowLength = width; |
| 1567 | |
| 1568 | for (i = 0; i < width; i += tileSize) { |
| 1569 | const GLint tileWidth = MIN2(tileSize, width - i); |
Brian Paul | fd90d80 | 2009-08-11 19:30:05 -0600 | [diff] [blame] | 1570 | const GLint tileX = (GLint) (x + i * ctx->Pixel.ZoomX); |
| 1571 | |
| 1572 | tileUnpack.SkipPixels = unpack->SkipPixels + i; |
| 1573 | |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 1574 | for (j = 0; j < height; j += tileSize) { |
| 1575 | const GLint tileHeight = MIN2(tileSize, height - j); |
Brian Paul | fd90d80 | 2009-08-11 19:30:05 -0600 | [diff] [blame] | 1576 | const GLint tileY = (GLint) (y + j * ctx->Pixel.ZoomY); |
| 1577 | |
| 1578 | tileUnpack.SkipRows = unpack->SkipRows + j; |
| 1579 | |
Brian Paul | 4de8e21 | 2009-09-19 17:06:15 -0600 | [diff] [blame] | 1580 | _mesa_meta_DrawPixels(ctx, tileX, tileY, tileWidth, tileHeight, |
| 1581 | format, type, &tileUnpack, pixels); |
Brian Paul | fd90d80 | 2009-08-11 19:30:05 -0600 | [diff] [blame] | 1582 | } |
| 1583 | } |
| 1584 | } |
| 1585 | |
| 1586 | |
| 1587 | /** |
Brian Paul | 1fc713a | 2009-08-31 19:28:11 -0600 | [diff] [blame] | 1588 | * One-time init for drawing stencil pixels. |
| 1589 | */ |
| 1590 | static void |
| 1591 | init_draw_stencil_pixels(GLcontext *ctx) |
| 1592 | { |
| 1593 | /* This program is run eight times, once for each stencil bit. |
| 1594 | * The stencil values to draw are found in an 8-bit alpha texture. |
| 1595 | * We read the texture/stencil value and test if bit 'b' is set. |
| 1596 | * If the bit is not set, use KIL to kill the fragment. |
| 1597 | * Finally, we use the stencil test to update the stencil buffer. |
| 1598 | * |
| 1599 | * The basic algorithm for checking if a bit is set is: |
| 1600 | * if (is_odd(value / (1 << bit))) |
| 1601 | * result is one (or non-zero). |
| 1602 | * else |
| 1603 | * result is zero. |
| 1604 | * The program parameter contains three values: |
| 1605 | * parm.x = 255 / (1 << bit) |
| 1606 | * parm.y = 0.5 |
| 1607 | * parm.z = 0.0 |
| 1608 | */ |
| 1609 | static const char *program = |
| 1610 | "!!ARBfp1.0\n" |
| 1611 | "PARAM parm = program.local[0]; \n" |
| 1612 | "TEMP t; \n" |
| 1613 | "TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */ |
| 1614 | "# t = t * 255 / bit \n" |
| 1615 | "MUL t.x, t.a, parm.x; \n" |
| 1616 | "# t = (int) t \n" |
| 1617 | "FRC t.y, t.x; \n" |
| 1618 | "SUB t.x, t.x, t.y; \n" |
| 1619 | "# t = t * 0.5 \n" |
| 1620 | "MUL t.x, t.x, parm.y; \n" |
| 1621 | "# t = fract(t.x) \n" |
| 1622 | "FRC t.x, t.x; # if t.x != 0, then the bit is set \n" |
| 1623 | "# t.x = (t.x == 0 ? 1 : 0) \n" |
| 1624 | "SGE t.x, -t.x, parm.z; \n" |
| 1625 | "KIL -t.x; \n" |
| 1626 | "# for debug only \n" |
| 1627 | "#MOV result.color, t.x; \n" |
| 1628 | "END \n"; |
| 1629 | char program2[1000]; |
| 1630 | struct drawpix_state *drawpix = &ctx->Meta->DrawPix; |
| 1631 | struct temp_texture *tex = get_temp_texture(ctx); |
| 1632 | const char *texTarget; |
| 1633 | |
| 1634 | assert(drawpix->StencilFP == 0); |
| 1635 | |
| 1636 | /* replace %s with "RECT" or "2D" */ |
| 1637 | assert(strlen(program) + 4 < sizeof(program2)); |
| 1638 | if (tex->Target == GL_TEXTURE_RECTANGLE) |
| 1639 | texTarget = "RECT"; |
| 1640 | else |
| 1641 | texTarget = "2D"; |
| 1642 | _mesa_snprintf(program2, sizeof(program2), program, texTarget); |
| 1643 | |
| 1644 | _mesa_GenPrograms(1, &drawpix->StencilFP); |
| 1645 | _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, drawpix->StencilFP); |
| 1646 | _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, |
| 1647 | strlen(program2), (const GLubyte *) program2); |
| 1648 | } |
| 1649 | |
| 1650 | |
| 1651 | /** |
| 1652 | * One-time init for drawing depth pixels. |
| 1653 | */ |
| 1654 | static void |
| 1655 | init_draw_depth_pixels(GLcontext *ctx) |
| 1656 | { |
| 1657 | static const char *program = |
| 1658 | "!!ARBfp1.0\n" |
| 1659 | "PARAM color = program.local[0]; \n" |
| 1660 | "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n" |
| 1661 | "MOV result.color, color; \n" |
| 1662 | "END \n"; |
| 1663 | char program2[200]; |
| 1664 | struct drawpix_state *drawpix = &ctx->Meta->DrawPix; |
| 1665 | struct temp_texture *tex = get_temp_texture(ctx); |
| 1666 | const char *texTarget; |
| 1667 | |
| 1668 | assert(drawpix->DepthFP == 0); |
| 1669 | |
| 1670 | /* replace %s with "RECT" or "2D" */ |
| 1671 | assert(strlen(program) + 4 < sizeof(program2)); |
| 1672 | if (tex->Target == GL_TEXTURE_RECTANGLE) |
| 1673 | texTarget = "RECT"; |
| 1674 | else |
| 1675 | texTarget = "2D"; |
| 1676 | _mesa_snprintf(program2, sizeof(program2), program, texTarget); |
| 1677 | |
| 1678 | _mesa_GenPrograms(1, &drawpix->DepthFP); |
| 1679 | _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, drawpix->DepthFP); |
| 1680 | _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, |
| 1681 | strlen(program2), (const GLubyte *) program2); |
| 1682 | } |
| 1683 | |
| 1684 | |
| 1685 | /** |
Brian Paul | dba6d52 | 2009-08-11 18:56:26 -0600 | [diff] [blame] | 1686 | * Meta implementation of ctx->Driver.DrawPixels() in terms |
| 1687 | * of texture mapping and polygon rendering. |
Brian Paul | dba6d52 | 2009-08-11 18:56:26 -0600 | [diff] [blame] | 1688 | */ |
| 1689 | void |
Brian Paul | 4de8e21 | 2009-09-19 17:06:15 -0600 | [diff] [blame] | 1690 | _mesa_meta_DrawPixels(GLcontext *ctx, |
| 1691 | GLint x, GLint y, GLsizei width, GLsizei height, |
| 1692 | GLenum format, GLenum type, |
| 1693 | const struct gl_pixelstore_attrib *unpack, |
| 1694 | const GLvoid *pixels) |
Brian Paul | dba6d52 | 2009-08-11 18:56:26 -0600 | [diff] [blame] | 1695 | { |
Brian Paul | dba6d52 | 2009-08-11 18:56:26 -0600 | [diff] [blame] | 1696 | struct drawpix_state *drawpix = &ctx->Meta->DrawPix; |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 1697 | struct temp_texture *tex = get_temp_texture(ctx); |
Brian Paul | dba6d52 | 2009-08-11 18:56:26 -0600 | [diff] [blame] | 1698 | const struct gl_pixelstore_attrib unpackSave = ctx->Unpack; |
Brian Paul | 92d6393 | 2009-09-01 12:24:30 -0600 | [diff] [blame] | 1699 | const GLuint origStencilMask = ctx->Stencil.WriteMask[0]; |
Brian Paul | 0ef5b62 | 2009-09-10 19:56:35 -0600 | [diff] [blame] | 1700 | struct vertex { |
| 1701 | GLfloat x, y, z, s, t; |
| 1702 | }; |
| 1703 | struct vertex verts[4]; |
Brian Paul | dba6d52 | 2009-08-11 18:56:26 -0600 | [diff] [blame] | 1704 | GLenum texIntFormat; |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 1705 | GLboolean fallback, newTex; |
Brian Paul | 1fc713a | 2009-08-31 19:28:11 -0600 | [diff] [blame] | 1706 | GLbitfield metaExtraSave = 0x0; |
Brian Paul | 926b965 | 2009-09-22 15:58:12 -0600 | [diff] [blame] | 1707 | GLuint vbo; |
Brian Paul | dba6d52 | 2009-08-11 18:56:26 -0600 | [diff] [blame] | 1708 | |
| 1709 | /* |
| 1710 | * Determine if we can do the glDrawPixels with texture mapping. |
| 1711 | */ |
| 1712 | fallback = GL_FALSE; |
| 1713 | if (ctx->_ImageTransferState || |
Brian Paul | fd90d80 | 2009-08-11 19:30:05 -0600 | [diff] [blame] | 1714 | ctx->Fog.Enabled) { |
Brian Paul | dba6d52 | 2009-08-11 18:56:26 -0600 | [diff] [blame] | 1715 | fallback = GL_TRUE; |
| 1716 | } |
| 1717 | |
| 1718 | if (_mesa_is_color_format(format)) { |
Brian Paul | 1fc713a | 2009-08-31 19:28:11 -0600 | [diff] [blame] | 1719 | /* use more compact format when possible */ |
Brian Paul | 89a765e | 2009-09-09 08:23:11 -0600 | [diff] [blame] | 1720 | /* XXX disable special case for GL_LUMINANCE for now to work around |
| 1721 | * apparent i965 driver bug (see bug #23670). |
| 1722 | */ |
| 1723 | if (/*format == GL_LUMINANCE ||*/ format == GL_LUMINANCE_ALPHA) |
Brian Paul | 1fc713a | 2009-08-31 19:28:11 -0600 | [diff] [blame] | 1724 | texIntFormat = format; |
| 1725 | else |
| 1726 | texIntFormat = GL_RGBA; |
| 1727 | } |
| 1728 | else if (_mesa_is_stencil_format(format)) { |
| 1729 | if (ctx->Extensions.ARB_fragment_program && |
Brian Paul | c5f8594 | 2009-09-10 10:17:07 -0600 | [diff] [blame] | 1730 | ctx->Pixel.IndexShift == 0 && |
| 1731 | ctx->Pixel.IndexOffset == 0 && |
Brian Paul | 1fc713a | 2009-08-31 19:28:11 -0600 | [diff] [blame] | 1732 | type == GL_UNSIGNED_BYTE) { |
| 1733 | /* We'll store stencil as alpha. This only works for GLubyte |
| 1734 | * image data because of how incoming values are mapped to alpha |
| 1735 | * in [0,1]. |
| 1736 | */ |
| 1737 | texIntFormat = GL_ALPHA; |
| 1738 | metaExtraSave = (META_COLOR_MASK | |
| 1739 | META_DEPTH_TEST | |
| 1740 | META_SHADER | |
| 1741 | META_STENCIL_TEST); |
| 1742 | } |
| 1743 | else { |
| 1744 | fallback = GL_TRUE; |
| 1745 | } |
| 1746 | } |
| 1747 | else if (_mesa_is_depth_format(format)) { |
| 1748 | if (ctx->Extensions.ARB_depth_texture && |
| 1749 | ctx->Extensions.ARB_fragment_program) { |
| 1750 | texIntFormat = GL_DEPTH_COMPONENT; |
| 1751 | metaExtraSave = (META_SHADER); |
| 1752 | } |
| 1753 | else { |
| 1754 | fallback = GL_TRUE; |
| 1755 | } |
Brian Paul | dba6d52 | 2009-08-11 18:56:26 -0600 | [diff] [blame] | 1756 | } |
| 1757 | else { |
| 1758 | fallback = GL_TRUE; |
| 1759 | } |
| 1760 | |
| 1761 | if (fallback) { |
| 1762 | _swrast_DrawPixels(ctx, x, y, width, height, |
| 1763 | format, type, unpack, pixels); |
| 1764 | return; |
| 1765 | } |
| 1766 | |
Brian Paul | fd90d80 | 2009-08-11 19:30:05 -0600 | [diff] [blame] | 1767 | /* |
| 1768 | * Check image size against max texture size, draw as tiles if needed. |
| 1769 | */ |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 1770 | if (width > tex->MaxSize || height > tex->MaxSize) { |
| 1771 | tiled_draw_pixels(ctx, tex->MaxSize, x, y, width, height, |
Brian Paul | fd90d80 | 2009-08-11 19:30:05 -0600 | [diff] [blame] | 1772 | format, type, unpack, pixels); |
| 1773 | return; |
| 1774 | } |
| 1775 | |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 1776 | /* Most GL state applies to glDrawPixels (like blending, stencil, etc), |
| 1777 | * but a there's a few things we need to override: |
Brian Paul | dba6d52 | 2009-08-11 18:56:26 -0600 | [diff] [blame] | 1778 | */ |
| 1779 | _mesa_meta_begin(ctx, (META_RASTERIZATION | |
| 1780 | META_SHADER | |
| 1781 | META_TEXTURE | |
| 1782 | META_TRANSFORM | |
| 1783 | META_VERTEX | |
Brian Paul | 1fc713a | 2009-08-31 19:28:11 -0600 | [diff] [blame] | 1784 | META_VIEWPORT | |
| 1785 | metaExtraSave)); |
Brian Paul | dba6d52 | 2009-08-11 18:56:26 -0600 | [diff] [blame] | 1786 | |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 1787 | newTex = alloc_texture(tex, width, height, texIntFormat); |
Brian Paul | dba6d52 | 2009-08-11 18:56:26 -0600 | [diff] [blame] | 1788 | |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 1789 | /* vertex positions, texcoords (after texture allocation!) */ |
| 1790 | { |
| 1791 | const GLfloat x0 = (GLfloat) x; |
| 1792 | const GLfloat y0 = (GLfloat) y; |
| 1793 | const GLfloat x1 = x + width * ctx->Pixel.ZoomX; |
| 1794 | const GLfloat y1 = y + height * ctx->Pixel.ZoomY; |
| 1795 | const GLfloat z = ctx->Current.RasterPos[2]; |
| 1796 | |
Brian Paul | 0ef5b62 | 2009-09-10 19:56:35 -0600 | [diff] [blame] | 1797 | verts[0].x = x0; |
| 1798 | verts[0].y = y0; |
| 1799 | verts[0].z = z; |
| 1800 | verts[0].s = 0.0F; |
| 1801 | verts[0].t = 0.0F; |
| 1802 | verts[1].x = x1; |
| 1803 | verts[1].y = y0; |
| 1804 | verts[1].z = z; |
| 1805 | verts[1].s = tex->Sright; |
| 1806 | verts[1].t = 0.0F; |
| 1807 | verts[2].x = x1; |
| 1808 | verts[2].y = y1; |
| 1809 | verts[2].z = z; |
| 1810 | verts[2].s = tex->Sright; |
| 1811 | verts[2].t = tex->Ttop; |
| 1812 | verts[3].x = x0; |
| 1813 | verts[3].y = y1; |
| 1814 | verts[3].z = z; |
| 1815 | verts[3].s = 0.0F; |
| 1816 | verts[3].t = tex->Ttop; |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 1817 | } |
Brian Paul | dba6d52 | 2009-08-11 18:56:26 -0600 | [diff] [blame] | 1818 | |
Brian Paul | 926b965 | 2009-09-22 15:58:12 -0600 | [diff] [blame] | 1819 | if (drawpix->ArrayObj == 0) { |
| 1820 | /* one-time setup: create vertex array object */ |
| 1821 | _mesa_GenVertexArrays(1, &drawpix->ArrayObj); |
| 1822 | } |
| 1823 | _mesa_BindVertexArray(drawpix->ArrayObj); |
| 1824 | |
| 1825 | /* create vertex array buffer */ |
| 1826 | _mesa_GenBuffersARB(1, &vbo); |
| 1827 | _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, vbo); |
| 1828 | _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts), |
| 1829 | verts, GL_DYNAMIC_DRAW_ARB); |
| 1830 | |
| 1831 | /* setup vertex arrays */ |
Brian Paul | 02f2bcd | 2009-09-22 16:06:18 -0600 | [diff] [blame] | 1832 | _mesa_VertexPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(x)); |
| 1833 | _mesa_TexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(s)); |
Brian Paul | 926b965 | 2009-09-22 15:58:12 -0600 | [diff] [blame] | 1834 | _mesa_EnableClientState(GL_VERTEX_ARRAY); |
| 1835 | _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 1836 | |
Brian Paul | dba6d52 | 2009-08-11 18:56:26 -0600 | [diff] [blame] | 1837 | /* set given unpack params */ |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 1838 | ctx->Unpack = *unpack; |
Brian Paul | dba6d52 | 2009-08-11 18:56:26 -0600 | [diff] [blame] | 1839 | |
Brian Paul | 0e5293a | 2009-09-03 21:30:06 -0600 | [diff] [blame] | 1840 | _mesa_set_enable(ctx, tex->Target, GL_TRUE); |
Brian Paul | 1fc713a | 2009-08-31 19:28:11 -0600 | [diff] [blame] | 1841 | |
| 1842 | if (_mesa_is_stencil_format(format)) { |
| 1843 | /* Drawing stencil */ |
| 1844 | GLint bit; |
| 1845 | |
| 1846 | if (!drawpix->StencilFP) |
| 1847 | init_draw_stencil_pixels(ctx); |
| 1848 | |
Eric Anholt | d492e7b | 2009-09-28 14:03:40 -0700 | [diff] [blame] | 1849 | setup_drawpix_texture(ctx, tex, newTex, texIntFormat, width, height, |
Brian Paul | 1fc713a | 2009-08-31 19:28:11 -0600 | [diff] [blame] | 1850 | GL_ALPHA, type, pixels); |
| 1851 | |
| 1852 | _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); |
| 1853 | |
| 1854 | _mesa_set_enable(ctx, GL_STENCIL_TEST, GL_TRUE); |
Brian Paul | c5f8594 | 2009-09-10 10:17:07 -0600 | [diff] [blame] | 1855 | |
| 1856 | /* set all stencil bits to 0 */ |
| 1857 | _mesa_StencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE); |
| 1858 | _mesa_StencilFunc(GL_ALWAYS, 0, 255); |
| 1859 | _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 1860 | |
| 1861 | /* set stencil bits to 1 where needed */ |
Brian Paul | 1fc713a | 2009-08-31 19:28:11 -0600 | [diff] [blame] | 1862 | _mesa_StencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); |
| 1863 | |
| 1864 | _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, drawpix->StencilFP); |
| 1865 | _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_TRUE); |
| 1866 | |
| 1867 | for (bit = 0; bit < ctx->Visual.stencilBits; bit++) { |
| 1868 | const GLuint mask = 1 << bit; |
Brian Paul | 92d6393 | 2009-09-01 12:24:30 -0600 | [diff] [blame] | 1869 | if (mask & origStencilMask) { |
| 1870 | _mesa_StencilFunc(GL_ALWAYS, mask, mask); |
| 1871 | _mesa_StencilMask(mask); |
Brian Paul | 1fc713a | 2009-08-31 19:28:11 -0600 | [diff] [blame] | 1872 | |
Brian Paul | 92d6393 | 2009-09-01 12:24:30 -0600 | [diff] [blame] | 1873 | _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 0, |
| 1874 | 255.0 / mask, 0.5, 0.0, 0.0); |
Brian Paul | 1fc713a | 2009-08-31 19:28:11 -0600 | [diff] [blame] | 1875 | |
Brian Paul | 92d6393 | 2009-09-01 12:24:30 -0600 | [diff] [blame] | 1876 | _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 1877 | } |
Brian Paul | 1fc713a | 2009-08-31 19:28:11 -0600 | [diff] [blame] | 1878 | } |
| 1879 | } |
| 1880 | else if (_mesa_is_depth_format(format)) { |
| 1881 | /* Drawing depth */ |
| 1882 | if (!drawpix->DepthFP) |
| 1883 | init_draw_depth_pixels(ctx); |
| 1884 | |
| 1885 | _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, drawpix->DepthFP); |
| 1886 | _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_TRUE); |
| 1887 | |
| 1888 | /* polygon color = current raster color */ |
| 1889 | _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, 0, |
| 1890 | ctx->Current.RasterColor); |
| 1891 | |
Eric Anholt | d492e7b | 2009-09-28 14:03:40 -0700 | [diff] [blame] | 1892 | setup_drawpix_texture(ctx, tex, newTex, texIntFormat, width, height, |
Brian Paul | 1fc713a | 2009-08-31 19:28:11 -0600 | [diff] [blame] | 1893 | format, type, pixels); |
| 1894 | |
| 1895 | _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 1896 | } |
| 1897 | else { |
| 1898 | /* Drawing RGBA */ |
Eric Anholt | d492e7b | 2009-09-28 14:03:40 -0700 | [diff] [blame] | 1899 | setup_drawpix_texture(ctx, tex, newTex, texIntFormat, width, height, |
Brian Paul | 1fc713a | 2009-08-31 19:28:11 -0600 | [diff] [blame] | 1900 | format, type, pixels); |
| 1901 | _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 1902 | } |
| 1903 | |
Brian Paul | 0e5293a | 2009-09-03 21:30:06 -0600 | [diff] [blame] | 1904 | _mesa_set_enable(ctx, tex->Target, GL_FALSE); |
Brian Paul | dba6d52 | 2009-08-11 18:56:26 -0600 | [diff] [blame] | 1905 | |
Brian Paul | 926b965 | 2009-09-22 15:58:12 -0600 | [diff] [blame] | 1906 | _mesa_DeleteBuffersARB(1, &vbo); |
| 1907 | |
Brian Paul | dba6d52 | 2009-08-11 18:56:26 -0600 | [diff] [blame] | 1908 | /* restore unpack params */ |
Brian Paul | 04f8193 | 2009-08-30 09:15:24 -0600 | [diff] [blame] | 1909 | ctx->Unpack = unpackSave; |
Brian Paul | dba6d52 | 2009-08-11 18:56:26 -0600 | [diff] [blame] | 1910 | |
Brian Paul | dba6d52 | 2009-08-11 18:56:26 -0600 | [diff] [blame] | 1911 | _mesa_meta_end(ctx); |
| 1912 | } |
Brian Paul | bcb62ae | 2009-09-03 21:27:06 -0600 | [diff] [blame] | 1913 | |
| 1914 | |
| 1915 | /** |
| 1916 | * Do glBitmap with a alpha texture quad. Use the alpha test to |
| 1917 | * cull the 'off' bits. If alpha test is already enabled, fall back |
| 1918 | * to swrast (should be a rare case). |
| 1919 | * A bitmap cache as in the gallium/mesa state tracker would |
| 1920 | * improve performance a lot. |
| 1921 | */ |
| 1922 | void |
Brian Paul | 4de8e21 | 2009-09-19 17:06:15 -0600 | [diff] [blame] | 1923 | _mesa_meta_Bitmap(GLcontext *ctx, |
Brian Paul | bcb62ae | 2009-09-03 21:27:06 -0600 | [diff] [blame] | 1924 | GLint x, GLint y, GLsizei width, GLsizei height, |
| 1925 | const struct gl_pixelstore_attrib *unpack, |
| 1926 | const GLubyte *bitmap1) |
| 1927 | { |
| 1928 | struct bitmap_state *bitmap = &ctx->Meta->Bitmap; |
Brian Paul | 886e7b3 | 2009-09-03 22:03:02 -0600 | [diff] [blame] | 1929 | struct temp_texture *tex = get_bitmap_temp_texture(ctx); |
Brian Paul | bcb62ae | 2009-09-03 21:27:06 -0600 | [diff] [blame] | 1930 | const GLenum texIntFormat = GL_ALPHA; |
| 1931 | const struct gl_pixelstore_attrib unpackSave = *unpack; |
Brian Paul | 0ef5b62 | 2009-09-10 19:56:35 -0600 | [diff] [blame] | 1932 | struct vertex { |
| 1933 | GLfloat x, y, z, s, t, r, g, b, a; |
| 1934 | }; |
| 1935 | struct vertex verts[4]; |
Brian Paul | bcb62ae | 2009-09-03 21:27:06 -0600 | [diff] [blame] | 1936 | GLboolean newTex; |
| 1937 | GLubyte *bitmap8; |
| 1938 | |
| 1939 | /* |
| 1940 | * Check if swrast fallback is needed. |
| 1941 | */ |
| 1942 | if (ctx->_ImageTransferState || |
| 1943 | ctx->Color.AlphaEnabled || |
| 1944 | ctx->Fog.Enabled || |
| 1945 | ctx->Texture._EnabledUnits || |
| 1946 | width > tex->MaxSize || |
| 1947 | height > tex->MaxSize) { |
| 1948 | _swrast_Bitmap(ctx, x, y, width, height, unpack, bitmap1); |
| 1949 | return; |
| 1950 | } |
| 1951 | |
| 1952 | /* Most GL state applies to glBitmap (like blending, stencil, etc), |
| 1953 | * but a there's a few things we need to override: |
| 1954 | */ |
| 1955 | _mesa_meta_begin(ctx, (META_ALPHA_TEST | |
| 1956 | META_PIXEL_STORE | |
| 1957 | META_RASTERIZATION | |
| 1958 | META_SHADER | |
| 1959 | META_TEXTURE | |
| 1960 | META_TRANSFORM | |
| 1961 | META_VERTEX | |
| 1962 | META_VIEWPORT)); |
| 1963 | |
| 1964 | if (bitmap->ArrayObj == 0) { |
| 1965 | /* one-time setup */ |
| 1966 | |
| 1967 | /* create vertex array object */ |
| 1968 | _mesa_GenVertexArraysAPPLE(1, &bitmap->ArrayObj); |
| 1969 | _mesa_BindVertexArrayAPPLE(bitmap->ArrayObj); |
| 1970 | |
| 1971 | /* create vertex array buffer */ |
| 1972 | _mesa_GenBuffersARB(1, &bitmap->VBO); |
| 1973 | _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, bitmap->VBO); |
| 1974 | _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts), |
| 1975 | NULL, GL_DYNAMIC_DRAW_ARB); |
| 1976 | |
| 1977 | /* setup vertex arrays */ |
Brian Paul | 0ef5b62 | 2009-09-10 19:56:35 -0600 | [diff] [blame] | 1978 | _mesa_VertexPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(x)); |
| 1979 | _mesa_TexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(s)); |
| 1980 | _mesa_ColorPointer(4, GL_FLOAT, sizeof(struct vertex), OFFSET(r)); |
Brian Paul | bcb62ae | 2009-09-03 21:27:06 -0600 | [diff] [blame] | 1981 | _mesa_EnableClientState(GL_VERTEX_ARRAY); |
| 1982 | _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 1983 | _mesa_EnableClientState(GL_COLOR_ARRAY); |
| 1984 | } |
| 1985 | else { |
| 1986 | _mesa_BindVertexArray(bitmap->ArrayObj); |
| 1987 | _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, bitmap->VBO); |
| 1988 | } |
| 1989 | |
| 1990 | newTex = alloc_texture(tex, width, height, texIntFormat); |
| 1991 | |
| 1992 | /* vertex positions, texcoords, colors (after texture allocation!) */ |
| 1993 | { |
| 1994 | const GLfloat x0 = (GLfloat) x; |
| 1995 | const GLfloat y0 = (GLfloat) y; |
| 1996 | const GLfloat x1 = (GLfloat) (x + width); |
| 1997 | const GLfloat y1 = (GLfloat) (y + height); |
| 1998 | const GLfloat z = ctx->Current.RasterPos[2]; |
| 1999 | GLuint i; |
| 2000 | |
Brian Paul | 0ef5b62 | 2009-09-10 19:56:35 -0600 | [diff] [blame] | 2001 | verts[0].x = x0; |
| 2002 | verts[0].y = y0; |
| 2003 | verts[0].z = z; |
| 2004 | verts[0].s = 0.0F; |
| 2005 | verts[0].t = 0.0F; |
| 2006 | verts[1].x = x1; |
| 2007 | verts[1].y = y0; |
| 2008 | verts[1].z = z; |
| 2009 | verts[1].s = tex->Sright; |
| 2010 | verts[1].t = 0.0F; |
| 2011 | verts[2].x = x1; |
| 2012 | verts[2].y = y1; |
| 2013 | verts[2].z = z; |
| 2014 | verts[2].s = tex->Sright; |
| 2015 | verts[2].t = tex->Ttop; |
| 2016 | verts[3].x = x0; |
| 2017 | verts[3].y = y1; |
| 2018 | verts[3].z = z; |
| 2019 | verts[3].s = 0.0F; |
| 2020 | verts[3].t = tex->Ttop; |
Brian Paul | bcb62ae | 2009-09-03 21:27:06 -0600 | [diff] [blame] | 2021 | |
| 2022 | for (i = 0; i < 4; i++) { |
Brian Paul | 0ef5b62 | 2009-09-10 19:56:35 -0600 | [diff] [blame] | 2023 | verts[i].r = ctx->Current.RasterColor[0]; |
| 2024 | verts[i].g = ctx->Current.RasterColor[1]; |
| 2025 | verts[i].b = ctx->Current.RasterColor[2]; |
| 2026 | verts[i].a = ctx->Current.RasterColor[3]; |
Brian Paul | bcb62ae | 2009-09-03 21:27:06 -0600 | [diff] [blame] | 2027 | } |
| 2028 | |
| 2029 | /* upload new vertex data */ |
| 2030 | _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts); |
| 2031 | } |
| 2032 | |
| 2033 | bitmap1 = _mesa_map_pbo_source(ctx, &unpackSave, bitmap1); |
| 2034 | if (!bitmap1) |
| 2035 | return; |
| 2036 | |
| 2037 | bitmap8 = (GLubyte *) _mesa_calloc(width * height); |
| 2038 | if (bitmap8) { |
| 2039 | _mesa_expand_bitmap(width, height, &unpackSave, bitmap1, |
| 2040 | bitmap8, width, 0xff); |
| 2041 | |
| 2042 | _mesa_set_enable(ctx, tex->Target, GL_TRUE); |
| 2043 | |
| 2044 | _mesa_set_enable(ctx, GL_ALPHA_TEST, GL_TRUE); |
| 2045 | _mesa_AlphaFunc(GL_GREATER, 0.0); |
| 2046 | |
Eric Anholt | d492e7b | 2009-09-28 14:03:40 -0700 | [diff] [blame] | 2047 | setup_drawpix_texture(ctx, tex, newTex, texIntFormat, width, height, |
Brian Paul | bcb62ae | 2009-09-03 21:27:06 -0600 | [diff] [blame] | 2048 | GL_ALPHA, GL_UNSIGNED_BYTE, bitmap8); |
| 2049 | |
| 2050 | _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 2051 | |
| 2052 | _mesa_set_enable(ctx, tex->Target, GL_FALSE); |
| 2053 | |
| 2054 | _mesa_free(bitmap8); |
| 2055 | } |
| 2056 | |
| 2057 | _mesa_unmap_pbo_source(ctx, &unpackSave); |
| 2058 | |
| 2059 | _mesa_meta_end(ctx); |
| 2060 | } |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 2061 | |
| 2062 | |
Brian Paul | 0876618 | 2009-09-24 19:33:22 -0600 | [diff] [blame] | 2063 | /** |
Brian Paul | f1cab80 | 2009-10-02 08:54:55 -0600 | [diff] [blame] | 2064 | * Check if the call to _mesa_meta_GenerateMipmap() will require a |
| 2065 | * software fallback. The fallback path will require that the texture |
| 2066 | * images are mapped. |
Brian Paul | c841335 | 2009-10-13 09:04:14 -0600 | [diff] [blame] | 2067 | * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise |
Brian Paul | f1cab80 | 2009-10-02 08:54:55 -0600 | [diff] [blame] | 2068 | */ |
| 2069 | GLboolean |
| 2070 | _mesa_meta_check_generate_mipmap_fallback(GLcontext *ctx, GLenum target, |
| 2071 | struct gl_texture_object *texObj) |
| 2072 | { |
Brian Paul | c841335 | 2009-10-13 09:04:14 -0600 | [diff] [blame] | 2073 | const GLuint fboSave = ctx->DrawBuffer->Name; |
| 2074 | struct gen_mipmap_state *mipmap = &ctx->Meta->Mipmap; |
| 2075 | struct gl_texture_image *baseImage; |
| 2076 | GLuint srcLevel; |
| 2077 | GLenum status; |
Brian Paul | f1cab80 | 2009-10-02 08:54:55 -0600 | [diff] [blame] | 2078 | |
| 2079 | /* check for fallbacks */ |
| 2080 | if (!ctx->Extensions.EXT_framebuffer_object || |
Brian Paul | c841335 | 2009-10-13 09:04:14 -0600 | [diff] [blame] | 2081 | target == GL_TEXTURE_3D) { |
Brian Paul | f1cab80 | 2009-10-02 08:54:55 -0600 | [diff] [blame] | 2082 | return GL_TRUE; |
| 2083 | } |
Brian Paul | c841335 | 2009-10-13 09:04:14 -0600 | [diff] [blame] | 2084 | |
| 2085 | srcLevel = texObj->BaseLevel; |
| 2086 | baseImage = _mesa_select_tex_image(ctx, texObj, target, srcLevel); |
Brian Paul | 1f196b7 | 2009-10-28 21:24:11 -0600 | [diff] [blame] | 2087 | if (!baseImage || _mesa_is_format_compressed(baseImage->TexFormat)) { |
Brian Paul | c841335 | 2009-10-13 09:04:14 -0600 | [diff] [blame] | 2088 | return GL_TRUE; |
| 2089 | } |
| 2090 | |
| 2091 | /* |
| 2092 | * Test that we can actually render in the texture's format. |
| 2093 | */ |
| 2094 | if (!mipmap->FBO) |
| 2095 | _mesa_GenFramebuffersEXT(1, &mipmap->FBO); |
| 2096 | _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, mipmap->FBO); |
| 2097 | |
| 2098 | if (target == GL_TEXTURE_1D) { |
| 2099 | _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT, |
| 2100 | GL_COLOR_ATTACHMENT0_EXT, |
| 2101 | target, texObj->Name, srcLevel); |
| 2102 | } |
| 2103 | else if (target == GL_TEXTURE_3D) { |
| 2104 | GLint zoffset = 0; |
| 2105 | _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT, |
| 2106 | GL_COLOR_ATTACHMENT0_EXT, |
| 2107 | target, texObj->Name, srcLevel, zoffset); |
| 2108 | } |
| 2109 | else { |
| 2110 | /* 2D / cube */ |
| 2111 | _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, |
| 2112 | GL_COLOR_ATTACHMENT0_EXT, |
| 2113 | target, texObj->Name, srcLevel); |
| 2114 | } |
| 2115 | |
Brian Paul | eefecf5 | 2009-10-13 09:04:54 -0600 | [diff] [blame] | 2116 | status = _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); |
Brian Paul | c841335 | 2009-10-13 09:04:14 -0600 | [diff] [blame] | 2117 | |
| 2118 | _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboSave); |
| 2119 | |
| 2120 | if (status != GL_FRAMEBUFFER_COMPLETE_EXT) { |
Brian Paul | c841335 | 2009-10-13 09:04:14 -0600 | [diff] [blame] | 2121 | return GL_TRUE; |
| 2122 | } |
| 2123 | |
Brian Paul | f1cab80 | 2009-10-02 08:54:55 -0600 | [diff] [blame] | 2124 | return GL_FALSE; |
| 2125 | } |
| 2126 | |
| 2127 | |
| 2128 | /** |
Brian Paul | 0876618 | 2009-09-24 19:33:22 -0600 | [diff] [blame] | 2129 | * Called via ctx->Driver.GenerateMipmap() |
| 2130 | * Note: texture borders and 3D texture support not yet complete. |
| 2131 | */ |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 2132 | void |
Brian Paul | 4de8e21 | 2009-09-19 17:06:15 -0600 | [diff] [blame] | 2133 | _mesa_meta_GenerateMipmap(GLcontext *ctx, GLenum target, |
| 2134 | struct gl_texture_object *texObj) |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 2135 | { |
| 2136 | struct gen_mipmap_state *mipmap = &ctx->Meta->Mipmap; |
Brian Paul | 0ef5b62 | 2009-09-10 19:56:35 -0600 | [diff] [blame] | 2137 | struct vertex { |
| 2138 | GLfloat x, y, s, t, r; |
| 2139 | }; |
| 2140 | struct vertex verts[4]; |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 2141 | const GLuint baseLevel = texObj->BaseLevel; |
| 2142 | const GLuint maxLevel = texObj->MaxLevel; |
| 2143 | const GLenum minFilterSave = texObj->MinFilter; |
| 2144 | const GLenum magFilterSave = texObj->MagFilter; |
Brian Paul | 0876618 | 2009-09-24 19:33:22 -0600 | [diff] [blame] | 2145 | const GLint baseLevelSave = texObj->BaseLevel; |
| 2146 | const GLint maxLevelSave = texObj->MaxLevel; |
| 2147 | const GLboolean genMipmapSave = texObj->GenerateMipmap; |
| 2148 | const GLenum wrapSSave = texObj->WrapS; |
| 2149 | const GLenum wrapTSave = texObj->WrapT; |
| 2150 | const GLenum wrapRSave = texObj->WrapR; |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 2151 | const GLuint fboSave = ctx->DrawBuffer->Name; |
| 2152 | GLenum faceTarget; |
Brian Paul | 0876618 | 2009-09-24 19:33:22 -0600 | [diff] [blame] | 2153 | GLuint dstLevel; |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 2154 | GLuint border = 0; |
| 2155 | |
Brian Paul | f1cab80 | 2009-10-02 08:54:55 -0600 | [diff] [blame] | 2156 | if (_mesa_meta_check_generate_mipmap_fallback(ctx, target, texObj)) { |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 2157 | _mesa_generate_mipmap(ctx, target, texObj); |
| 2158 | return; |
| 2159 | } |
| 2160 | |
| 2161 | if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && |
| 2162 | target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z) { |
| 2163 | faceTarget = target; |
| 2164 | target = GL_TEXTURE_CUBE_MAP; |
| 2165 | } |
| 2166 | else { |
| 2167 | faceTarget = target; |
| 2168 | } |
| 2169 | |
| 2170 | _mesa_meta_begin(ctx, META_ALL); |
| 2171 | |
| 2172 | if (mipmap->ArrayObj == 0) { |
| 2173 | /* one-time setup */ |
| 2174 | |
| 2175 | /* create vertex array object */ |
| 2176 | _mesa_GenVertexArraysAPPLE(1, &mipmap->ArrayObj); |
| 2177 | _mesa_BindVertexArrayAPPLE(mipmap->ArrayObj); |
| 2178 | |
| 2179 | /* create vertex array buffer */ |
| 2180 | _mesa_GenBuffersARB(1, &mipmap->VBO); |
| 2181 | _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, mipmap->VBO); |
| 2182 | _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts), |
| 2183 | NULL, GL_DYNAMIC_DRAW_ARB); |
| 2184 | |
| 2185 | /* setup vertex arrays */ |
Brian Paul | 0ef5b62 | 2009-09-10 19:56:35 -0600 | [diff] [blame] | 2186 | _mesa_VertexPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(x)); |
| 2187 | _mesa_TexCoordPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(s)); |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 2188 | _mesa_EnableClientState(GL_VERTEX_ARRAY); |
| 2189 | _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 2190 | } |
| 2191 | else { |
| 2192 | _mesa_BindVertexArray(mipmap->ArrayObj); |
| 2193 | _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, mipmap->VBO); |
| 2194 | } |
| 2195 | |
| 2196 | if (!mipmap->FBO) { |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 2197 | _mesa_GenFramebuffersEXT(1, &mipmap->FBO); |
| 2198 | } |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 2199 | _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, mipmap->FBO); |
| 2200 | |
| 2201 | _mesa_TexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 2202 | _mesa_TexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
Brian Paul | 0876618 | 2009-09-24 19:33:22 -0600 | [diff] [blame] | 2203 | _mesa_TexParameteri(target, GL_GENERATE_MIPMAP, GL_FALSE); |
| 2204 | _mesa_TexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 2205 | _mesa_TexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 2206 | _mesa_TexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); |
| 2207 | |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 2208 | _mesa_set_enable(ctx, target, GL_TRUE); |
| 2209 | |
| 2210 | /* setup texcoords once (XXX what about border?) */ |
| 2211 | switch (faceTarget) { |
Brian Paul | 0876618 | 2009-09-24 19:33:22 -0600 | [diff] [blame] | 2212 | case GL_TEXTURE_1D: |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 2213 | case GL_TEXTURE_2D: |
| 2214 | verts[0].s = 0.0F; |
| 2215 | verts[0].t = 0.0F; |
| 2216 | verts[0].r = 0.0F; |
| 2217 | verts[1].s = 1.0F; |
| 2218 | verts[1].t = 0.0F; |
Mathias Frohlich | f89751e | 2009-09-10 08:50:01 -0600 | [diff] [blame] | 2219 | verts[1].r = 0.0F; |
| 2220 | verts[2].s = 1.0F; |
| 2221 | verts[2].t = 1.0F; |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 2222 | verts[2].r = 0.0F; |
Mathias Frohlich | f89751e | 2009-09-10 08:50:01 -0600 | [diff] [blame] | 2223 | verts[3].s = 0.0F; |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 2224 | verts[3].t = 1.0F; |
| 2225 | verts[3].r = 0.0F; |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 2226 | break; |
Brian Paul | 0876618 | 2009-09-24 19:33:22 -0600 | [diff] [blame] | 2227 | case GL_TEXTURE_3D: |
| 2228 | abort(); |
| 2229 | break; |
| 2230 | default: |
| 2231 | /* cube face */ |
| 2232 | { |
| 2233 | static const GLfloat st[4][2] = { |
| 2234 | {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f} |
| 2235 | }; |
| 2236 | GLuint i; |
| 2237 | |
| 2238 | /* loop over quad verts */ |
| 2239 | for (i = 0; i < 4; i++) { |
| 2240 | /* Compute sc = +/-scale and tc = +/-scale. |
| 2241 | * Not +/-1 to avoid cube face selection ambiguity near the edges, |
| 2242 | * though that can still sometimes happen with this scale factor... |
| 2243 | */ |
| 2244 | const GLfloat scale = 0.9999f; |
| 2245 | const GLfloat sc = (2.0f * st[i][0] - 1.0f) * scale; |
| 2246 | const GLfloat tc = (2.0f * st[i][1] - 1.0f) * scale; |
| 2247 | |
| 2248 | switch (faceTarget) { |
| 2249 | case GL_TEXTURE_CUBE_MAP_POSITIVE_X: |
| 2250 | verts[i].s = 1.0f; |
| 2251 | verts[i].t = -tc; |
| 2252 | verts[i].r = -sc; |
| 2253 | break; |
| 2254 | case GL_TEXTURE_CUBE_MAP_NEGATIVE_X: |
| 2255 | verts[i].s = -1.0f; |
| 2256 | verts[i].t = -tc; |
| 2257 | verts[i].r = sc; |
| 2258 | break; |
| 2259 | case GL_TEXTURE_CUBE_MAP_POSITIVE_Y: |
| 2260 | verts[i].s = sc; |
| 2261 | verts[i].t = 1.0f; |
| 2262 | verts[i].r = tc; |
| 2263 | break; |
| 2264 | case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y: |
| 2265 | verts[i].s = sc; |
| 2266 | verts[i].t = -1.0f; |
| 2267 | verts[i].r = -tc; |
| 2268 | break; |
| 2269 | case GL_TEXTURE_CUBE_MAP_POSITIVE_Z: |
| 2270 | verts[i].s = sc; |
| 2271 | verts[i].t = -tc; |
| 2272 | verts[i].r = 1.0f; |
| 2273 | break; |
| 2274 | case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z: |
| 2275 | verts[i].s = -sc; |
| 2276 | verts[i].t = -tc; |
| 2277 | verts[i].r = -1.0f; |
| 2278 | break; |
| 2279 | default: |
| 2280 | assert(0); |
| 2281 | } |
| 2282 | } |
| 2283 | } |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 2284 | } |
| 2285 | |
Brian Paul | 0876618 | 2009-09-24 19:33:22 -0600 | [diff] [blame] | 2286 | _mesa_set_enable(ctx, target, GL_TRUE); |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 2287 | |
Brian Paul | 0876618 | 2009-09-24 19:33:22 -0600 | [diff] [blame] | 2288 | /* texture is already locked, unlock now */ |
| 2289 | _mesa_unlock_texture(ctx, texObj); |
| 2290 | |
| 2291 | for (dstLevel = baseLevel + 1; dstLevel <= maxLevel; dstLevel++) { |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 2292 | const struct gl_texture_image *srcImage; |
Brian Paul | 0876618 | 2009-09-24 19:33:22 -0600 | [diff] [blame] | 2293 | const GLuint srcLevel = dstLevel - 1; |
| 2294 | GLsizei srcWidth, srcHeight, srcDepth; |
| 2295 | GLsizei dstWidth, dstHeight, dstDepth; |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 2296 | GLenum status; |
| 2297 | |
Brian Paul | 0876618 | 2009-09-24 19:33:22 -0600 | [diff] [blame] | 2298 | srcImage = _mesa_select_tex_image(ctx, texObj, faceTarget, srcLevel); |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 2299 | assert(srcImage->Border == 0); /* XXX we can fix this */ |
| 2300 | |
Brian Paul | 0876618 | 2009-09-24 19:33:22 -0600 | [diff] [blame] | 2301 | /* src size w/out border */ |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 2302 | srcWidth = srcImage->Width - 2 * border; |
| 2303 | srcHeight = srcImage->Height - 2 * border; |
Brian Paul | 0876618 | 2009-09-24 19:33:22 -0600 | [diff] [blame] | 2304 | srcDepth = srcImage->Depth - 2 * border; |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 2305 | |
Brian Paul | 0876618 | 2009-09-24 19:33:22 -0600 | [diff] [blame] | 2306 | /* new dst size w/ border */ |
| 2307 | dstWidth = MAX2(1, srcWidth / 2) + 2 * border; |
| 2308 | dstHeight = MAX2(1, srcHeight / 2) + 2 * border; |
| 2309 | dstDepth = MAX2(1, srcDepth / 2) + 2 * border; |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 2310 | |
Brian Paul | 0876618 | 2009-09-24 19:33:22 -0600 | [diff] [blame] | 2311 | if (dstWidth == srcImage->Width && |
| 2312 | dstHeight == srcImage->Height && |
| 2313 | dstDepth == srcImage->Depth) { |
| 2314 | /* all done */ |
| 2315 | break; |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 2316 | } |
| 2317 | |
Brian Paul | 0876618 | 2009-09-24 19:33:22 -0600 | [diff] [blame] | 2318 | /* Create empty dest image */ |
| 2319 | if (target == GL_TEXTURE_1D) { |
| 2320 | _mesa_TexImage1D(target, dstLevel, srcImage->InternalFormat, |
| 2321 | dstWidth, border, |
| 2322 | GL_RGBA, GL_UNSIGNED_BYTE, NULL); |
| 2323 | } |
| 2324 | else if (target == GL_TEXTURE_3D) { |
| 2325 | _mesa_TexImage3D(target, dstLevel, srcImage->InternalFormat, |
| 2326 | dstWidth, dstHeight, dstDepth, border, |
| 2327 | GL_RGBA, GL_UNSIGNED_BYTE, NULL); |
| 2328 | } |
| 2329 | else { |
| 2330 | /* 2D or cube */ |
| 2331 | _mesa_TexImage2D(faceTarget, dstLevel, srcImage->InternalFormat, |
| 2332 | dstWidth, dstHeight, border, |
| 2333 | GL_RGBA, GL_UNSIGNED_BYTE, NULL); |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 2334 | |
Brian Paul | 0876618 | 2009-09-24 19:33:22 -0600 | [diff] [blame] | 2335 | if (target == GL_TEXTURE_CUBE_MAP) { |
| 2336 | /* If texturing from a cube, we need to make sure all src faces |
| 2337 | * have been defined (even if we're not sampling from them.) |
| 2338 | * Otherwise the texture object will be 'incomplete' and |
| 2339 | * texturing from it will not be allowed. |
| 2340 | */ |
| 2341 | GLuint face; |
| 2342 | for (face = 0; face < 6; face++) { |
| 2343 | if (!texObj->Image[face][srcLevel] || |
| 2344 | texObj->Image[face][srcLevel]->Width != srcWidth) { |
| 2345 | _mesa_TexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, |
| 2346 | srcLevel, srcImage->InternalFormat, |
| 2347 | srcWidth, srcHeight, border, |
| 2348 | GL_RGBA, GL_UNSIGNED_BYTE, NULL); |
| 2349 | } |
| 2350 | } |
| 2351 | } |
| 2352 | } |
| 2353 | |
| 2354 | /* setup vertex positions */ |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 2355 | { |
| 2356 | verts[0].x = 0.0F; |
| 2357 | verts[0].y = 0.0F; |
Brian Paul | 0876618 | 2009-09-24 19:33:22 -0600 | [diff] [blame] | 2358 | verts[1].x = (GLfloat) dstWidth; |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 2359 | verts[1].y = 0.0F; |
Brian Paul | 0876618 | 2009-09-24 19:33:22 -0600 | [diff] [blame] | 2360 | verts[2].x = (GLfloat) dstWidth; |
| 2361 | verts[2].y = (GLfloat) dstHeight; |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 2362 | verts[3].x = 0.0F; |
Brian Paul | 0876618 | 2009-09-24 19:33:22 -0600 | [diff] [blame] | 2363 | verts[3].y = (GLfloat) dstHeight; |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 2364 | |
| 2365 | /* upload new vertex data */ |
| 2366 | _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts); |
| 2367 | } |
| 2368 | |
| 2369 | /* limit sampling to src level */ |
Brian Paul | 0876618 | 2009-09-24 19:33:22 -0600 | [diff] [blame] | 2370 | _mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, srcLevel); |
| 2371 | _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, srcLevel); |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 2372 | |
Brian Paul | 0876618 | 2009-09-24 19:33:22 -0600 | [diff] [blame] | 2373 | /* Set to draw into the current dstLevel */ |
| 2374 | if (target == GL_TEXTURE_1D) { |
| 2375 | _mesa_FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT, |
| 2376 | GL_COLOR_ATTACHMENT0_EXT, |
| 2377 | target, |
| 2378 | texObj->Name, |
| 2379 | dstLevel); |
| 2380 | } |
| 2381 | else if (target == GL_TEXTURE_3D) { |
| 2382 | GLint zoffset = 0; /* XXX unfinished */ |
| 2383 | _mesa_FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT, |
| 2384 | GL_COLOR_ATTACHMENT0_EXT, |
| 2385 | target, |
| 2386 | texObj->Name, |
| 2387 | dstLevel, zoffset); |
| 2388 | } |
| 2389 | else { |
| 2390 | /* 2D / cube */ |
| 2391 | _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, |
| 2392 | GL_COLOR_ATTACHMENT0_EXT, |
| 2393 | faceTarget, |
| 2394 | texObj->Name, |
| 2395 | dstLevel); |
| 2396 | } |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 2397 | |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 2398 | _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT); |
| 2399 | |
Brian Paul | 0876618 | 2009-09-24 19:33:22 -0600 | [diff] [blame] | 2400 | /* sanity check */ |
Brian Paul | eefecf5 | 2009-10-13 09:04:54 -0600 | [diff] [blame] | 2401 | status = _mesa_CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 2402 | if (status != GL_FRAMEBUFFER_COMPLETE_EXT) { |
| 2403 | abort(); |
| 2404 | break; |
| 2405 | } |
| 2406 | |
| 2407 | _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 2408 | } |
| 2409 | |
Brian Paul | 0876618 | 2009-09-24 19:33:22 -0600 | [diff] [blame] | 2410 | _mesa_lock_texture(ctx, texObj); /* relock */ |
| 2411 | |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 2412 | _mesa_meta_end(ctx); |
| 2413 | |
| 2414 | _mesa_TexParameteri(target, GL_TEXTURE_MIN_FILTER, minFilterSave); |
| 2415 | _mesa_TexParameteri(target, GL_TEXTURE_MAG_FILTER, magFilterSave); |
Brian Paul | 0876618 | 2009-09-24 19:33:22 -0600 | [diff] [blame] | 2416 | _mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, baseLevelSave); |
| 2417 | _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, maxLevelSave); |
| 2418 | _mesa_TexParameteri(target, GL_GENERATE_MIPMAP, genMipmapSave); |
| 2419 | _mesa_TexParameteri(target, GL_TEXTURE_WRAP_S, wrapSSave); |
| 2420 | _mesa_TexParameteri(target, GL_TEXTURE_WRAP_T, wrapTSave); |
| 2421 | _mesa_TexParameteri(target, GL_TEXTURE_WRAP_R, wrapRSave); |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 2422 | |
Brian Paul | 23663ae | 2009-09-06 09:39:47 -0600 | [diff] [blame] | 2423 | _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboSave); |
| 2424 | } |
Brian Paul | 1d8fbef | 2009-09-16 20:09:05 -0600 | [diff] [blame] | 2425 | |
| 2426 | |
| 2427 | /** |
| 2428 | * Determine the GL data type to use for the temporary image read with |
| 2429 | * ReadPixels() and passed to Tex[Sub]Image(). |
| 2430 | */ |
| 2431 | static GLenum |
| 2432 | get_temp_image_type(GLcontext *ctx, GLenum baseFormat) |
| 2433 | { |
| 2434 | switch (baseFormat) { |
| 2435 | case GL_RGBA: |
| 2436 | case GL_RGB: |
| 2437 | case GL_ALPHA: |
| 2438 | case GL_LUMINANCE: |
| 2439 | case GL_LUMINANCE_ALPHA: |
| 2440 | case GL_INTENSITY: |
| 2441 | if (ctx->DrawBuffer->Visual.redBits <= 8) |
| 2442 | return GL_UNSIGNED_BYTE; |
| 2443 | else if (ctx->DrawBuffer->Visual.redBits <= 8) |
| 2444 | return GL_UNSIGNED_SHORT; |
| 2445 | else |
| 2446 | return GL_FLOAT; |
| 2447 | case GL_DEPTH_COMPONENT: |
| 2448 | return GL_UNSIGNED_INT; |
| 2449 | case GL_DEPTH_STENCIL: |
| 2450 | return GL_UNSIGNED_INT_24_8; |
| 2451 | default: |
| 2452 | _mesa_problem(ctx, "Unexpected format in get_temp_image_type()"); |
| 2453 | return 0; |
| 2454 | } |
| 2455 | } |
| 2456 | |
| 2457 | |
| 2458 | /** |
| 2459 | * Helper for _mesa_meta_CopyTexImage1/2D() functions. |
| 2460 | * Have to be careful with locking and meta state for pixel transfer. |
| 2461 | */ |
| 2462 | static void |
| 2463 | copy_tex_image(GLcontext *ctx, GLuint dims, GLenum target, GLint level, |
| 2464 | GLenum internalFormat, GLint x, GLint y, |
| 2465 | GLsizei width, GLsizei height, GLint border) |
| 2466 | { |
Brian Paul | 1d8fbef | 2009-09-16 20:09:05 -0600 | [diff] [blame] | 2467 | struct gl_texture_object *texObj; |
| 2468 | struct gl_texture_image *texImage; |
Brian Paul | b0e9ea6 | 2009-09-19 13:37:28 -0600 | [diff] [blame] | 2469 | GLsizei postConvWidth = width, postConvHeight = height; |
Brian Paul | 1d8fbef | 2009-09-16 20:09:05 -0600 | [diff] [blame] | 2470 | GLenum format, type; |
| 2471 | GLint bpp; |
| 2472 | void *buf; |
| 2473 | |
Brian Paul | 7157479 | 2009-10-31 08:08:05 -0600 | [diff] [blame] | 2474 | texObj = _mesa_get_current_tex_object(ctx, target); |
Brian Paul | 1d8fbef | 2009-09-16 20:09:05 -0600 | [diff] [blame] | 2475 | texImage = _mesa_get_tex_image(ctx, texObj, target, level); |
| 2476 | |
| 2477 | format = _mesa_base_tex_format(ctx, internalFormat); |
| 2478 | type = get_temp_image_type(ctx, format); |
| 2479 | bpp = _mesa_bytes_per_pixel(format, type); |
| 2480 | if (bpp <= 0) { |
| 2481 | _mesa_problem(ctx, "Bad bpp in meta copy_tex_image()"); |
| 2482 | return; |
| 2483 | } |
| 2484 | |
| 2485 | /* |
| 2486 | * Alloc image buffer (XXX could use a PBO) |
| 2487 | */ |
| 2488 | buf = _mesa_malloc(width * height * bpp); |
| 2489 | if (!buf) { |
| 2490 | _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage%uD", dims); |
| 2491 | return; |
| 2492 | } |
| 2493 | |
Brian Paul | 1f196b7 | 2009-10-28 21:24:11 -0600 | [diff] [blame] | 2494 | if (texImage->TexFormat == MESA_FORMAT_NONE) |
| 2495 | texImage->TexFormat = ctx->Driver.ChooseTextureFormat(ctx, |
| 2496 | internalFormat, |
| 2497 | format, |
| 2498 | type); |
Michel Dänzer | 4a6759b | 2009-10-03 18:01:57 +0200 | [diff] [blame] | 2499 | |
Brian Paul | 1d8fbef | 2009-09-16 20:09:05 -0600 | [diff] [blame] | 2500 | _mesa_unlock_texture(ctx, texObj); /* need to unlock first */ |
| 2501 | |
| 2502 | /* |
| 2503 | * Read image from framebuffer (disable pixel transfer ops) |
| 2504 | */ |
| 2505 | _mesa_meta_begin(ctx, META_PIXEL_STORE | META_PIXEL_TRANSFER); |
| 2506 | ctx->Driver.ReadPixels(ctx, x, y, width, height, |
| 2507 | format, type, &ctx->Pack, buf); |
| 2508 | _mesa_meta_end(ctx); |
| 2509 | |
| 2510 | /* |
Brian Paul | b0e9ea6 | 2009-09-19 13:37:28 -0600 | [diff] [blame] | 2511 | * Prepare for new texture image size/data |
| 2512 | */ |
| 2513 | #if FEATURE_convolve |
| 2514 | if (_mesa_is_color_format(internalFormat)) { |
| 2515 | _mesa_adjust_image_for_convolution(ctx, 2, |
| 2516 | &postConvWidth, &postConvHeight); |
| 2517 | } |
| 2518 | #endif |
| 2519 | |
| 2520 | if (texImage->Data) { |
| 2521 | ctx->Driver.FreeTexImageData(ctx, texImage); |
| 2522 | } |
| 2523 | |
| 2524 | _mesa_init_teximage_fields(ctx, target, texImage, |
| 2525 | postConvWidth, postConvHeight, 1, |
| 2526 | border, internalFormat); |
| 2527 | |
| 2528 | /* |
Brian Paul | 1d8fbef | 2009-09-16 20:09:05 -0600 | [diff] [blame] | 2529 | * Store texture data (with pixel transfer ops) |
| 2530 | */ |
| 2531 | _mesa_meta_begin(ctx, META_PIXEL_STORE); |
Brian Paul | b0e9ea6 | 2009-09-19 13:37:28 -0600 | [diff] [blame] | 2532 | |
| 2533 | _mesa_update_state(ctx); /* to update pixel transfer state */ |
| 2534 | |
Brian Paul | 1d8fbef | 2009-09-16 20:09:05 -0600 | [diff] [blame] | 2535 | if (target == GL_TEXTURE_1D) { |
| 2536 | ctx->Driver.TexImage1D(ctx, target, level, internalFormat, |
| 2537 | width, border, format, type, |
| 2538 | buf, &ctx->Unpack, texObj, texImage); |
| 2539 | } |
| 2540 | else { |
| 2541 | ctx->Driver.TexImage2D(ctx, target, level, internalFormat, |
| 2542 | width, height, border, format, type, |
| 2543 | buf, &ctx->Unpack, texObj, texImage); |
| 2544 | } |
| 2545 | _mesa_meta_end(ctx); |
| 2546 | |
| 2547 | _mesa_lock_texture(ctx, texObj); /* re-lock */ |
| 2548 | |
| 2549 | _mesa_free(buf); |
| 2550 | } |
| 2551 | |
| 2552 | |
| 2553 | void |
| 2554 | _mesa_meta_CopyTexImage1D(GLcontext *ctx, GLenum target, GLint level, |
| 2555 | GLenum internalFormat, GLint x, GLint y, |
| 2556 | GLsizei width, GLint border) |
| 2557 | { |
| 2558 | copy_tex_image(ctx, 1, target, level, internalFormat, x, y, |
| 2559 | width, 1, border); |
| 2560 | } |
| 2561 | |
| 2562 | |
| 2563 | void |
| 2564 | _mesa_meta_CopyTexImage2D(GLcontext *ctx, GLenum target, GLint level, |
| 2565 | GLenum internalFormat, GLint x, GLint y, |
| 2566 | GLsizei width, GLsizei height, GLint border) |
| 2567 | { |
| 2568 | copy_tex_image(ctx, 2, target, level, internalFormat, x, y, |
| 2569 | width, height, border); |
| 2570 | } |
| 2571 | |
| 2572 | |
| 2573 | |
| 2574 | /** |
| 2575 | * Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions. |
| 2576 | * Have to be careful with locking and meta state for pixel transfer. |
| 2577 | */ |
| 2578 | static void |
| 2579 | copy_tex_sub_image(GLcontext *ctx, GLuint dims, GLenum target, GLint level, |
| 2580 | GLint xoffset, GLint yoffset, GLint zoffset, |
| 2581 | GLint x, GLint y, |
| 2582 | GLsizei width, GLsizei height) |
| 2583 | { |
Brian Paul | 1d8fbef | 2009-09-16 20:09:05 -0600 | [diff] [blame] | 2584 | struct gl_texture_object *texObj; |
| 2585 | struct gl_texture_image *texImage; |
| 2586 | GLenum format, type; |
| 2587 | GLint bpp; |
| 2588 | void *buf; |
| 2589 | |
Brian Paul | 7157479 | 2009-10-31 08:08:05 -0600 | [diff] [blame] | 2590 | texObj = _mesa_get_current_tex_object(ctx, target); |
Brian Paul | 1d8fbef | 2009-09-16 20:09:05 -0600 | [diff] [blame] | 2591 | texImage = _mesa_select_tex_image(ctx, texObj, target, level); |
| 2592 | |
Brian Paul | 1f7c914 | 2009-09-30 20:28:45 -0600 | [diff] [blame] | 2593 | format = _mesa_get_format_base_format(texImage->TexFormat); |
Brian Paul | 1d8fbef | 2009-09-16 20:09:05 -0600 | [diff] [blame] | 2594 | type = get_temp_image_type(ctx, format); |
| 2595 | bpp = _mesa_bytes_per_pixel(format, type); |
| 2596 | if (bpp <= 0) { |
| 2597 | _mesa_problem(ctx, "Bad bpp in meta copy_tex_sub_image()"); |
| 2598 | return; |
| 2599 | } |
| 2600 | |
| 2601 | /* |
| 2602 | * Alloc image buffer (XXX could use a PBO) |
| 2603 | */ |
| 2604 | buf = _mesa_malloc(width * height * bpp); |
| 2605 | if (!buf) { |
| 2606 | _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage%uD", dims); |
| 2607 | return; |
| 2608 | } |
| 2609 | |
| 2610 | _mesa_unlock_texture(ctx, texObj); /* need to unlock first */ |
| 2611 | |
| 2612 | /* |
| 2613 | * Read image from framebuffer (disable pixel transfer ops) |
| 2614 | */ |
| 2615 | _mesa_meta_begin(ctx, META_PIXEL_STORE | META_PIXEL_TRANSFER); |
| 2616 | ctx->Driver.ReadPixels(ctx, x, y, width, height, |
| 2617 | format, type, &ctx->Pack, buf); |
| 2618 | _mesa_meta_end(ctx); |
| 2619 | |
Brian Paul | b0e9ea6 | 2009-09-19 13:37:28 -0600 | [diff] [blame] | 2620 | _mesa_update_state(ctx); /* to update pixel transfer state */ |
| 2621 | |
Brian Paul | 1d8fbef | 2009-09-16 20:09:05 -0600 | [diff] [blame] | 2622 | /* |
| 2623 | * Store texture data (with pixel transfer ops) |
| 2624 | */ |
| 2625 | _mesa_meta_begin(ctx, META_PIXEL_STORE); |
| 2626 | if (target == GL_TEXTURE_1D) { |
| 2627 | ctx->Driver.TexSubImage1D(ctx, target, level, xoffset, |
| 2628 | width, format, type, buf, |
| 2629 | &ctx->Unpack, texObj, texImage); |
| 2630 | } |
| 2631 | else if (target == GL_TEXTURE_3D) { |
| 2632 | ctx->Driver.TexSubImage3D(ctx, target, level, xoffset, yoffset, zoffset, |
| 2633 | width, height, 1, format, type, buf, |
| 2634 | &ctx->Unpack, texObj, texImage); |
| 2635 | } |
| 2636 | else { |
| 2637 | ctx->Driver.TexSubImage2D(ctx, target, level, xoffset, yoffset, |
| 2638 | width, height, format, type, buf, |
| 2639 | &ctx->Unpack, texObj, texImage); |
| 2640 | } |
| 2641 | _mesa_meta_end(ctx); |
| 2642 | |
| 2643 | _mesa_lock_texture(ctx, texObj); /* re-lock */ |
| 2644 | |
| 2645 | _mesa_free(buf); |
| 2646 | } |
| 2647 | |
| 2648 | |
| 2649 | void |
| 2650 | _mesa_meta_CopyTexSubImage1D(GLcontext *ctx, GLenum target, GLint level, |
| 2651 | GLint xoffset, |
| 2652 | GLint x, GLint y, GLsizei width) |
| 2653 | { |
| 2654 | copy_tex_sub_image(ctx, 1, target, level, xoffset, 0, 0, |
| 2655 | x, y, width, 1); |
| 2656 | } |
| 2657 | |
| 2658 | |
| 2659 | void |
| 2660 | _mesa_meta_CopyTexSubImage2D(GLcontext *ctx, GLenum target, GLint level, |
| 2661 | GLint xoffset, GLint yoffset, |
| 2662 | GLint x, GLint y, |
| 2663 | GLsizei width, GLsizei height) |
| 2664 | { |
| 2665 | copy_tex_sub_image(ctx, 2, target, level, xoffset, yoffset, 0, |
| 2666 | x, y, width, height); |
| 2667 | } |
| 2668 | |
| 2669 | |
| 2670 | void |
| 2671 | _mesa_meta_CopyTexSubImage3D(GLcontext *ctx, GLenum target, GLint level, |
| 2672 | GLint xoffset, GLint yoffset, GLint zoffset, |
| 2673 | GLint x, GLint y, |
| 2674 | GLsizei width, GLsizei height) |
| 2675 | { |
| 2676 | copy_tex_sub_image(ctx, 3, target, level, xoffset, yoffset, zoffset, |
| 2677 | x, y, width, height); |
| 2678 | } |
Brian Paul | 3e5a352 | 2009-09-19 16:32:42 -0600 | [diff] [blame] | 2679 | |
| 2680 | |
| 2681 | void |
| 2682 | _mesa_meta_CopyColorTable(GLcontext *ctx, |
| 2683 | GLenum target, GLenum internalformat, |
| 2684 | GLint x, GLint y, GLsizei width) |
| 2685 | { |
| 2686 | GLfloat *buf; |
| 2687 | |
| 2688 | buf = (GLfloat *) _mesa_malloc(width * 4 * sizeof(GLfloat)); |
| 2689 | if (!buf) { |
| 2690 | _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyColorTable"); |
| 2691 | return; |
| 2692 | } |
| 2693 | |
| 2694 | /* |
| 2695 | * Read image from framebuffer (disable pixel transfer ops) |
| 2696 | */ |
| 2697 | _mesa_meta_begin(ctx, META_PIXEL_STORE | META_PIXEL_TRANSFER); |
| 2698 | ctx->Driver.ReadPixels(ctx, x, y, width, 1, |
| 2699 | GL_RGBA, GL_FLOAT, &ctx->Pack, buf); |
| 2700 | |
| 2701 | _mesa_ColorTable(target, internalformat, width, GL_RGBA, GL_FLOAT, buf); |
| 2702 | |
| 2703 | _mesa_meta_end(ctx); |
| 2704 | |
| 2705 | _mesa_free(buf); |
| 2706 | } |
| 2707 | |
| 2708 | |
| 2709 | void |
| 2710 | _mesa_meta_CopyColorSubTable(GLcontext *ctx,GLenum target, GLsizei start, |
| 2711 | GLint x, GLint y, GLsizei width) |
| 2712 | { |
| 2713 | GLfloat *buf; |
| 2714 | |
| 2715 | buf = (GLfloat *) _mesa_malloc(width * 4 * sizeof(GLfloat)); |
| 2716 | if (!buf) { |
| 2717 | _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyColorSubTable"); |
| 2718 | return; |
| 2719 | } |
| 2720 | |
| 2721 | /* |
| 2722 | * Read image from framebuffer (disable pixel transfer ops) |
| 2723 | */ |
| 2724 | _mesa_meta_begin(ctx, META_PIXEL_STORE | META_PIXEL_TRANSFER); |
| 2725 | ctx->Driver.ReadPixels(ctx, x, y, width, 1, |
| 2726 | GL_RGBA, GL_FLOAT, &ctx->Pack, buf); |
| 2727 | |
| 2728 | _mesa_ColorSubTable(target, start, width, GL_RGBA, GL_FLOAT, buf); |
| 2729 | |
| 2730 | _mesa_meta_end(ctx); |
| 2731 | |
| 2732 | _mesa_free(buf); |
| 2733 | } |
| 2734 | |
| 2735 | |
| 2736 | void |
| 2737 | _mesa_meta_CopyConvolutionFilter1D(GLcontext *ctx, GLenum target, |
| 2738 | GLenum internalFormat, |
| 2739 | GLint x, GLint y, GLsizei width) |
| 2740 | { |
| 2741 | GLfloat *buf; |
| 2742 | |
| 2743 | buf = (GLfloat *) _mesa_malloc(width * 4 * sizeof(GLfloat)); |
| 2744 | if (!buf) { |
| 2745 | _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyConvolutionFilter2D"); |
| 2746 | return; |
| 2747 | } |
| 2748 | |
| 2749 | /* |
| 2750 | * Read image from framebuffer (disable pixel transfer ops) |
| 2751 | */ |
| 2752 | _mesa_meta_begin(ctx, META_PIXEL_STORE | META_PIXEL_TRANSFER); |
| 2753 | _mesa_update_state(ctx); |
| 2754 | ctx->Driver.ReadPixels(ctx, x, y, width, 1, |
| 2755 | GL_RGBA, GL_FLOAT, &ctx->Pack, buf); |
| 2756 | |
| 2757 | _mesa_ConvolutionFilter1D(target, internalFormat, width, |
| 2758 | GL_RGBA, GL_FLOAT, buf); |
| 2759 | |
| 2760 | _mesa_meta_end(ctx); |
| 2761 | |
| 2762 | _mesa_free(buf); |
| 2763 | } |
| 2764 | |
| 2765 | |
| 2766 | void |
| 2767 | _mesa_meta_CopyConvolutionFilter2D(GLcontext *ctx, GLenum target, |
| 2768 | GLenum internalFormat, GLint x, GLint y, |
| 2769 | GLsizei width, GLsizei height) |
| 2770 | { |
| 2771 | GLfloat *buf; |
| 2772 | |
Brian Paul | 3e5a352 | 2009-09-19 16:32:42 -0600 | [diff] [blame] | 2773 | buf = (GLfloat *) _mesa_malloc(width * height * 4 * sizeof(GLfloat)); |
| 2774 | if (!buf) { |
| 2775 | _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyConvolutionFilter2D"); |
| 2776 | return; |
| 2777 | } |
| 2778 | |
| 2779 | /* |
| 2780 | * Read image from framebuffer (disable pixel transfer ops) |
| 2781 | */ |
| 2782 | _mesa_meta_begin(ctx, META_PIXEL_STORE | META_PIXEL_TRANSFER); |
| 2783 | _mesa_update_state(ctx); |
| 2784 | |
| 2785 | ctx->Driver.ReadPixels(ctx, x, y, width, height, |
| 2786 | GL_RGBA, GL_FLOAT, &ctx->Pack, buf); |
| 2787 | |
| 2788 | _mesa_ConvolutionFilter2D(target, internalFormat, width, height, |
| 2789 | GL_RGBA, GL_FLOAT, buf); |
| 2790 | |
| 2791 | _mesa_meta_end(ctx); |
| 2792 | |
| 2793 | _mesa_free(buf); |
| 2794 | } |