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