Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Mesa 3-D graphics library |
Brian | 942ee02 | 2007-02-09 15:40:00 -0700 | [diff] [blame] | 3 | * Version: 6.5.3 |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 4 | * |
Brian | 942ee02 | 2007-02-09 15:40:00 -0700 | [diff] [blame] | 5 | * Copyright (C) 1999-2007 Brian Paul All Rights Reserved. |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 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 | * \file program.c |
| 27 | * Vertex and fragment program support functions. |
| 28 | * \author Brian Paul |
| 29 | */ |
| 30 | |
| 31 | |
| 32 | #include "glheader.h" |
| 33 | #include "context.h" |
| 34 | #include "hash.h" |
Brian | 0560d81 | 2006-12-14 15:01:28 -0700 | [diff] [blame] | 35 | #include "program.h" |
| 36 | #include "prog_parameter.h" |
| 37 | #include "prog_instruction.h" |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 38 | |
| 39 | |
Brian | 942ee02 | 2007-02-09 15:40:00 -0700 | [diff] [blame] | 40 | /** |
| 41 | * A pointer to this dummy program is put into the hash table when |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 42 | * glGenPrograms is called. |
| 43 | */ |
Brian Paul | 122629f | 2006-07-20 16:49:57 +0000 | [diff] [blame] | 44 | struct gl_program _mesa_DummyProgram; |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 45 | |
| 46 | |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 47 | /** |
Brian Paul | 21841f0 | 2004-08-14 14:28:11 +0000 | [diff] [blame] | 48 | * Init context's vertex/fragment program state |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 49 | */ |
| 50 | void |
| 51 | _mesa_init_program(GLcontext *ctx) |
| 52 | { |
| 53 | GLuint i; |
| 54 | |
| 55 | ctx->Program.ErrorPos = -1; |
| 56 | ctx->Program.ErrorString = _mesa_strdup(""); |
| 57 | |
| 58 | #if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program |
| 59 | ctx->VertexProgram.Enabled = GL_FALSE; |
| 60 | ctx->VertexProgram.PointSizeEnabled = GL_FALSE; |
| 61 | ctx->VertexProgram.TwoSideEnabled = GL_FALSE; |
Brian Paul | 122629f | 2006-07-20 16:49:57 +0000 | [diff] [blame] | 62 | ctx->VertexProgram.Current = (struct gl_vertex_program *) ctx->Shared->DefaultVertexProgram; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 63 | assert(ctx->VertexProgram.Current); |
| 64 | ctx->VertexProgram.Current->Base.RefCount++; |
| 65 | for (i = 0; i < MAX_NV_VERTEX_PROGRAM_PARAMS / 4; i++) { |
| 66 | ctx->VertexProgram.TrackMatrix[i] = GL_NONE; |
| 67 | ctx->VertexProgram.TrackMatrixTransform[i] = GL_IDENTITY_NV; |
| 68 | } |
| 69 | #endif |
| 70 | |
| 71 | #if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program |
| 72 | ctx->FragmentProgram.Enabled = GL_FALSE; |
Brian Paul | 122629f | 2006-07-20 16:49:57 +0000 | [diff] [blame] | 73 | ctx->FragmentProgram.Current = (struct gl_fragment_program *) ctx->Shared->DefaultFragmentProgram; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 74 | assert(ctx->FragmentProgram.Current); |
| 75 | ctx->FragmentProgram.Current->Base.RefCount++; |
| 76 | #endif |
Dave Airlie | 7f752fe | 2004-12-19 03:06:59 +0000 | [diff] [blame] | 77 | |
Brian Paul | 63d6830 | 2005-11-19 16:43:04 +0000 | [diff] [blame] | 78 | /* XXX probably move this stuff */ |
Dave Airlie | 7f752fe | 2004-12-19 03:06:59 +0000 | [diff] [blame] | 79 | #if FEATURE_ATI_fragment_shader |
| 80 | ctx->ATIFragmentShader.Enabled = GL_FALSE; |
| 81 | ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader; |
| 82 | assert(ctx->ATIFragmentShader.Current); |
Brian Paul | 63d6830 | 2005-11-19 16:43:04 +0000 | [diff] [blame] | 83 | ctx->ATIFragmentShader.Current->RefCount++; |
Dave Airlie | 7f752fe | 2004-12-19 03:06:59 +0000 | [diff] [blame] | 84 | #endif |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | |
| 88 | /** |
Brian Paul | 21841f0 | 2004-08-14 14:28:11 +0000 | [diff] [blame] | 89 | * Free a context's vertex/fragment program state |
| 90 | */ |
| 91 | void |
| 92 | _mesa_free_program_data(GLcontext *ctx) |
| 93 | { |
Roland Scheidegger | 93da673 | 2006-03-01 23:11:14 +0000 | [diff] [blame] | 94 | #if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program |
Brian Paul | 21841f0 | 2004-08-14 14:28:11 +0000 | [diff] [blame] | 95 | if (ctx->VertexProgram.Current) { |
| 96 | ctx->VertexProgram.Current->Base.RefCount--; |
| 97 | if (ctx->VertexProgram.Current->Base.RefCount <= 0) |
| 98 | ctx->Driver.DeleteProgram(ctx, &(ctx->VertexProgram.Current->Base)); |
| 99 | } |
| 100 | #endif |
Roland Scheidegger | 93da673 | 2006-03-01 23:11:14 +0000 | [diff] [blame] | 101 | #if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program |
Brian Paul | 21841f0 | 2004-08-14 14:28:11 +0000 | [diff] [blame] | 102 | if (ctx->FragmentProgram.Current) { |
| 103 | ctx->FragmentProgram.Current->Base.RefCount--; |
| 104 | if (ctx->FragmentProgram.Current->Base.RefCount <= 0) |
| 105 | ctx->Driver.DeleteProgram(ctx, &(ctx->FragmentProgram.Current->Base)); |
| 106 | } |
| 107 | #endif |
Brian Paul | 63d6830 | 2005-11-19 16:43:04 +0000 | [diff] [blame] | 108 | /* XXX probably move this stuff */ |
Dave Airlie | 7f752fe | 2004-12-19 03:06:59 +0000 | [diff] [blame] | 109 | #if FEATURE_ATI_fragment_shader |
| 110 | if (ctx->ATIFragmentShader.Current) { |
Brian Paul | 63d6830 | 2005-11-19 16:43:04 +0000 | [diff] [blame] | 111 | ctx->ATIFragmentShader.Current->RefCount--; |
| 112 | if (ctx->ATIFragmentShader.Current->RefCount <= 0) { |
| 113 | _mesa_free(ctx->ATIFragmentShader.Current); |
| 114 | } |
Dave Airlie | 7f752fe | 2004-12-19 03:06:59 +0000 | [diff] [blame] | 115 | } |
| 116 | #endif |
Brian Paul | 21841f0 | 2004-08-14 14:28:11 +0000 | [diff] [blame] | 117 | _mesa_free((void *) ctx->Program.ErrorString); |
| 118 | } |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | /** |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 124 | * Set the vertex/fragment program error state (position and error string). |
| 125 | * This is generally called from within the parsers. |
| 126 | */ |
| 127 | void |
| 128 | _mesa_set_program_error(GLcontext *ctx, GLint pos, const char *string) |
| 129 | { |
| 130 | ctx->Program.ErrorPos = pos; |
| 131 | _mesa_free((void *) ctx->Program.ErrorString); |
| 132 | if (!string) |
| 133 | string = ""; |
| 134 | ctx->Program.ErrorString = _mesa_strdup(string); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | /** |
| 139 | * Find the line number and column for 'pos' within 'string'. |
| 140 | * Return a copy of the line which contains 'pos'. Free the line with |
| 141 | * _mesa_free(). |
| 142 | * \param string the program string |
| 143 | * \param pos the position within the string |
| 144 | * \param line returns the line number corresponding to 'pos'. |
| 145 | * \param col returns the column number corresponding to 'pos'. |
| 146 | * \return copy of the line containing 'pos'. |
| 147 | */ |
| 148 | const GLubyte * |
| 149 | _mesa_find_line_column(const GLubyte *string, const GLubyte *pos, |
| 150 | GLint *line, GLint *col) |
| 151 | { |
| 152 | const GLubyte *lineStart = string; |
| 153 | const GLubyte *p = string; |
| 154 | GLubyte *s; |
| 155 | int len; |
| 156 | |
| 157 | *line = 1; |
| 158 | |
| 159 | while (p != pos) { |
| 160 | if (*p == (GLubyte) '\n') { |
| 161 | (*line)++; |
| 162 | lineStart = p + 1; |
| 163 | } |
| 164 | p++; |
| 165 | } |
| 166 | |
| 167 | *col = (pos - lineStart) + 1; |
| 168 | |
| 169 | /* return copy of this line */ |
| 170 | while (*p != 0 && *p != '\n') |
| 171 | p++; |
| 172 | len = p - lineStart; |
| 173 | s = (GLubyte *) _mesa_malloc(len + 1); |
| 174 | _mesa_memcpy(s, lineStart, len); |
| 175 | s[len] = 0; |
| 176 | |
| 177 | return s; |
| 178 | } |
| 179 | |
| 180 | |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 181 | /** |
| 182 | * Initialize a new vertex/fragment program object. |
| 183 | */ |
Brian Paul | 122629f | 2006-07-20 16:49:57 +0000 | [diff] [blame] | 184 | static struct gl_program * |
| 185 | _mesa_init_program_struct( GLcontext *ctx, struct gl_program *prog, |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 186 | GLenum target, GLuint id) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 187 | { |
Brian Paul | a6c423d | 2004-08-25 15:59:48 +0000 | [diff] [blame] | 188 | (void) ctx; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 189 | if (prog) { |
Brian | 8fed246 | 2007-10-26 19:19:09 -0600 | [diff] [blame] | 190 | GLuint i; |
Brian | fe1d01c | 2006-12-13 14:54:47 -0700 | [diff] [blame] | 191 | _mesa_bzero(prog, sizeof(*prog)); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 192 | prog->Id = id; |
| 193 | prog->Target = target; |
| 194 | prog->Resident = GL_TRUE; |
| 195 | prog->RefCount = 1; |
Brian Paul | 308b85f | 2006-11-23 15:58:30 +0000 | [diff] [blame] | 196 | prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB; |
Brian | 8fed246 | 2007-10-26 19:19:09 -0600 | [diff] [blame] | 197 | |
| 198 | /* default mapping from samplers to texture units */ |
| 199 | for (i = 0; i < MAX_SAMPLERS; i++) |
| 200 | prog->SamplerUnits[i] = i; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | return prog; |
| 204 | } |
| 205 | |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 206 | |
| 207 | /** |
| 208 | * Initialize a new fragment program object. |
| 209 | */ |
Brian Paul | 122629f | 2006-07-20 16:49:57 +0000 | [diff] [blame] | 210 | struct gl_program * |
| 211 | _mesa_init_fragment_program( GLcontext *ctx, struct gl_fragment_program *prog, |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 212 | GLenum target, GLuint id) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 213 | { |
| 214 | if (prog) |
| 215 | return _mesa_init_program_struct( ctx, &prog->Base, target, id ); |
| 216 | else |
| 217 | return NULL; |
| 218 | } |
| 219 | |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 220 | |
| 221 | /** |
| 222 | * Initialize a new vertex program object. |
| 223 | */ |
Brian Paul | 122629f | 2006-07-20 16:49:57 +0000 | [diff] [blame] | 224 | struct gl_program * |
| 225 | _mesa_init_vertex_program( GLcontext *ctx, struct gl_vertex_program *prog, |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 226 | GLenum target, GLuint id) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 227 | { |
| 228 | if (prog) |
| 229 | return _mesa_init_program_struct( ctx, &prog->Base, target, id ); |
| 230 | else |
| 231 | return NULL; |
| 232 | } |
| 233 | |
| 234 | |
| 235 | /** |
| 236 | * Allocate and initialize a new fragment/vertex program object but |
| 237 | * don't put it into the program hash table. Called via |
| 238 | * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a |
| 239 | * device driver function to implement OO deriviation with additional |
| 240 | * types not understood by this function. |
| 241 | * |
| 242 | * \param ctx context |
| 243 | * \param id program id/number |
| 244 | * \param target program target/type |
| 245 | * \return pointer to new program object |
| 246 | */ |
Brian Paul | 122629f | 2006-07-20 16:49:57 +0000 | [diff] [blame] | 247 | struct gl_program * |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 248 | _mesa_new_program(GLcontext *ctx, GLenum target, GLuint id) |
| 249 | { |
| 250 | switch (target) { |
| 251 | case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */ |
Brian Paul | 122629f | 2006-07-20 16:49:57 +0000 | [diff] [blame] | 252 | return _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program), |
| 253 | target, id ); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 254 | case GL_FRAGMENT_PROGRAM_NV: |
| 255 | case GL_FRAGMENT_PROGRAM_ARB: |
Brian Paul | 122629f | 2006-07-20 16:49:57 +0000 | [diff] [blame] | 256 | return _mesa_init_fragment_program(ctx, |
| 257 | CALLOC_STRUCT(gl_fragment_program), |
| 258 | target, id ); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 259 | default: |
| 260 | _mesa_problem(ctx, "bad target in _mesa_new_program"); |
| 261 | return NULL; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | |
| 266 | /** |
| 267 | * Delete a program and remove it from the hash table, ignoring the |
| 268 | * reference count. |
| 269 | * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation) |
| 270 | * by a device driver function. |
| 271 | */ |
| 272 | void |
Brian Paul | 122629f | 2006-07-20 16:49:57 +0000 | [diff] [blame] | 273 | _mesa_delete_program(GLcontext *ctx, struct gl_program *prog) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 274 | { |
Brian Paul | a6c423d | 2004-08-25 15:59:48 +0000 | [diff] [blame] | 275 | (void) ctx; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 276 | ASSERT(prog); |
| 277 | |
Brian Paul | 308b85f | 2006-11-23 15:58:30 +0000 | [diff] [blame] | 278 | if (prog == &_mesa_DummyProgram) |
| 279 | return; |
| 280 | |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 281 | if (prog->String) |
| 282 | _mesa_free(prog->String); |
Brian Paul | de99760 | 2005-11-12 17:53:14 +0000 | [diff] [blame] | 283 | |
| 284 | if (prog->Instructions) { |
| 285 | GLuint i; |
| 286 | for (i = 0; i < prog->NumInstructions; i++) { |
| 287 | if (prog->Instructions[i].Data) |
| 288 | _mesa_free(prog->Instructions[i].Data); |
Brian | 4cc2674 | 2007-04-20 08:12:17 -0600 | [diff] [blame] | 289 | if (prog->Instructions[i].Comment) |
| 290 | _mesa_free((char *) prog->Instructions[i].Comment); |
Brian Paul | 575700f | 2004-12-16 03:07:18 +0000 | [diff] [blame] | 291 | } |
Brian Paul | de99760 | 2005-11-12 17:53:14 +0000 | [diff] [blame] | 292 | _mesa_free(prog->Instructions); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 293 | } |
Brian Paul | de99760 | 2005-11-12 17:53:14 +0000 | [diff] [blame] | 294 | |
Brian Paul | 65a51c0 | 2006-05-24 03:30:31 +0000 | [diff] [blame] | 295 | if (prog->Parameters) { |
Brian Paul | de99760 | 2005-11-12 17:53:14 +0000 | [diff] [blame] | 296 | _mesa_free_parameter_list(prog->Parameters); |
Brian Paul | 65a51c0 | 2006-05-24 03:30:31 +0000 | [diff] [blame] | 297 | } |
Brian | fe1d01c | 2006-12-13 14:54:47 -0700 | [diff] [blame] | 298 | if (prog->Varying) { |
| 299 | _mesa_free_parameter_list(prog->Varying); |
| 300 | } |
Brian | 3493e86 | 2007-03-24 16:18:13 -0600 | [diff] [blame] | 301 | if (prog->Attributes) { |
| 302 | _mesa_free_parameter_list(prog->Attributes); |
| 303 | } |
Brian | fe1d01c | 2006-12-13 14:54:47 -0700 | [diff] [blame] | 304 | |
Brian Paul | 58d080b | 2006-08-25 19:46:31 +0000 | [diff] [blame] | 305 | /* XXX this is a little ugly */ |
| 306 | if (prog->Target == GL_VERTEX_PROGRAM_ARB) { |
| 307 | struct gl_vertex_program *vprog = (struct gl_vertex_program *) prog; |
| 308 | if (vprog->TnlData) |
| 309 | _mesa_free(vprog->TnlData); |
| 310 | } |
| 311 | |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 312 | _mesa_free(prog); |
| 313 | } |
| 314 | |
| 315 | |
Brian Paul | 4d12a05 | 2006-08-23 23:10:14 +0000 | [diff] [blame] | 316 | /** |
| 317 | * Return the gl_program object for a given ID. |
| 318 | * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of |
| 319 | * casts elsewhere. |
| 320 | */ |
| 321 | struct gl_program * |
| 322 | _mesa_lookup_program(GLcontext *ctx, GLuint id) |
| 323 | { |
| 324 | if (id) |
| 325 | return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id); |
| 326 | else |
| 327 | return NULL; |
| 328 | } |
| 329 | |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 330 | |
Brian Paul | 3b9b8de | 2006-08-24 21:57:36 +0000 | [diff] [blame] | 331 | /** |
Brian | b2a3a85 | 2006-12-14 13:56:58 -0700 | [diff] [blame] | 332 | * Return a copy of a program. |
| 333 | * XXX Problem here if the program object is actually OO-derivation |
| 334 | * made by a device driver. |
| 335 | */ |
| 336 | struct gl_program * |
| 337 | _mesa_clone_program(GLcontext *ctx, const struct gl_program *prog) |
| 338 | { |
| 339 | struct gl_program *clone; |
| 340 | |
Brian | 4477a01 | 2007-07-24 09:57:26 -0600 | [diff] [blame] | 341 | clone = ctx->Driver.NewProgram(ctx, prog->Target, prog->Id); |
Brian | b2a3a85 | 2006-12-14 13:56:58 -0700 | [diff] [blame] | 342 | if (!clone) |
| 343 | return NULL; |
| 344 | |
| 345 | assert(clone->Target == prog->Target); |
| 346 | clone->String = (GLubyte *) _mesa_strdup((char *) prog->String); |
| 347 | clone->RefCount = 1; |
| 348 | clone->Format = prog->Format; |
| 349 | clone->Instructions = _mesa_alloc_instructions(prog->NumInstructions); |
| 350 | if (!clone->Instructions) { |
| 351 | _mesa_delete_program(ctx, clone); |
| 352 | return NULL; |
| 353 | } |
Brian | 12229f1 | 2007-03-22 09:11:26 -0600 | [diff] [blame] | 354 | _mesa_copy_instructions(clone->Instructions, prog->Instructions, |
| 355 | prog->NumInstructions); |
Brian | b2a3a85 | 2006-12-14 13:56:58 -0700 | [diff] [blame] | 356 | clone->InputsRead = prog->InputsRead; |
| 357 | clone->OutputsWritten = prog->OutputsWritten; |
Brian | c9db223 | 2007-01-04 17:22:19 -0700 | [diff] [blame] | 358 | memcpy(clone->TexturesUsed, prog->TexturesUsed, sizeof(prog->TexturesUsed)); |
| 359 | |
Brian | 2e76f0a | 2006-12-19 09:52:07 -0700 | [diff] [blame] | 360 | if (prog->Parameters) |
| 361 | clone->Parameters = _mesa_clone_parameter_list(prog->Parameters); |
Brian | b2a3a85 | 2006-12-14 13:56:58 -0700 | [diff] [blame] | 362 | memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams)); |
Brian | 2e76f0a | 2006-12-19 09:52:07 -0700 | [diff] [blame] | 363 | if (prog->Varying) |
| 364 | clone->Varying = _mesa_clone_parameter_list(prog->Varying); |
Brian | 3209c3e | 2007-01-09 17:49:24 -0700 | [diff] [blame] | 365 | if (prog->Attributes) |
| 366 | clone->Attributes = _mesa_clone_parameter_list(prog->Attributes); |
Brian | b2a3a85 | 2006-12-14 13:56:58 -0700 | [diff] [blame] | 367 | memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams)); |
| 368 | clone->NumInstructions = prog->NumInstructions; |
| 369 | clone->NumTemporaries = prog->NumTemporaries; |
| 370 | clone->NumParameters = prog->NumParameters; |
| 371 | clone->NumAttributes = prog->NumAttributes; |
| 372 | clone->NumAddressRegs = prog->NumAddressRegs; |
| 373 | clone->NumNativeInstructions = prog->NumNativeInstructions; |
| 374 | clone->NumNativeTemporaries = prog->NumNativeTemporaries; |
| 375 | clone->NumNativeParameters = prog->NumNativeParameters; |
| 376 | clone->NumNativeAttributes = prog->NumNativeAttributes; |
| 377 | clone->NumNativeAddressRegs = prog->NumNativeAddressRegs; |
Brian | 21f9979 | 2007-01-09 11:00:21 -0700 | [diff] [blame] | 378 | clone->NumAluInstructions = prog->NumAluInstructions; |
| 379 | clone->NumTexInstructions = prog->NumTexInstructions; |
| 380 | clone->NumTexIndirections = prog->NumTexIndirections; |
| 381 | clone->NumNativeAluInstructions = prog->NumNativeAluInstructions; |
| 382 | clone->NumNativeTexInstructions = prog->NumNativeTexInstructions; |
| 383 | clone->NumNativeTexIndirections = prog->NumNativeTexIndirections; |
Brian | b2a3a85 | 2006-12-14 13:56:58 -0700 | [diff] [blame] | 384 | |
| 385 | switch (prog->Target) { |
| 386 | case GL_VERTEX_PROGRAM_ARB: |
| 387 | { |
| 388 | const struct gl_vertex_program *vp |
| 389 | = (const struct gl_vertex_program *) prog; |
| 390 | struct gl_vertex_program *vpc = (struct gl_vertex_program *) clone; |
| 391 | vpc->IsPositionInvariant = vp->IsPositionInvariant; |
| 392 | } |
| 393 | break; |
| 394 | case GL_FRAGMENT_PROGRAM_ARB: |
| 395 | { |
| 396 | const struct gl_fragment_program *fp |
| 397 | = (const struct gl_fragment_program *) prog; |
| 398 | struct gl_fragment_program *fpc = (struct gl_fragment_program *) clone; |
Brian | b2a3a85 | 2006-12-14 13:56:58 -0700 | [diff] [blame] | 399 | fpc->FogOption = fp->FogOption; |
| 400 | fpc->UsesKill = fp->UsesKill; |
| 401 | } |
| 402 | break; |
| 403 | default: |
| 404 | _mesa_problem(NULL, "Unexpected target in _mesa_clone_program"); |
| 405 | } |
| 406 | |
| 407 | return clone; |
| 408 | } |
| 409 | |
| 410 | |
| 411 | |
| 412 | /** |
Brian | 9ffd889 | 2007-10-29 16:35:59 -0600 | [diff] [blame] | 413 | * Search instructions for registers that match (oldFile, oldIndex), |
| 414 | * replacing them with (newFile, newIndex). |
| 415 | */ |
| 416 | static void |
| 417 | replace_registers(struct prog_instruction *inst, GLuint numInst, |
| 418 | GLuint oldFile, GLuint oldIndex, |
| 419 | GLuint newFile, GLuint newIndex) |
| 420 | { |
| 421 | GLuint i, j; |
| 422 | for (i = 0; i < numInst; i++) { |
| 423 | for (j = 0; j < _mesa_num_inst_src_regs(inst->Opcode); j++) { |
| 424 | if (inst[i].SrcReg[j].File == oldFile && |
| 425 | inst[i].SrcReg[j].Index == oldIndex) { |
| 426 | inst[i].SrcReg[j].File = newFile; |
| 427 | inst[i].SrcReg[j].Index = newIndex; |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | |
| 434 | /** |
| 435 | * Search instructions for references to program parameters. When found, |
| 436 | * increment the parameter index by 'offset'. |
| 437 | * Used when combining programs. |
| 438 | */ |
| 439 | static void |
| 440 | adjust_param_indexes(struct prog_instruction *inst, GLuint numInst, |
| 441 | GLuint offset) |
| 442 | { |
| 443 | GLuint i, j; |
| 444 | for (i = 0; i < numInst; i++) { |
| 445 | for (j = 0; j < _mesa_num_inst_src_regs(inst->Opcode); j++) { |
| 446 | GLuint f = inst[i].SrcReg[j].File; |
| 447 | if (f == PROGRAM_CONSTANT || |
| 448 | f == PROGRAM_UNIFORM || |
| 449 | f == PROGRAM_STATE_VAR) { |
| 450 | inst[i].SrcReg[j].Index += offset; |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | |
| 457 | /** |
| 458 | * Combine two programs into one. Fix instructions so the outputs of |
| 459 | * the first program go to the inputs of the second program. |
| 460 | */ |
| 461 | struct gl_program * |
| 462 | _mesa_combine_programs(GLcontext *ctx, |
Brian | 1203f54 | 2007-10-29 16:38:53 -0600 | [diff] [blame] | 463 | const struct gl_program *progA, |
| 464 | const struct gl_program *progB) |
Brian | 9ffd889 | 2007-10-29 16:35:59 -0600 | [diff] [blame] | 465 | { |
| 466 | struct prog_instruction *newInst; |
| 467 | struct gl_program *newProg; |
| 468 | const GLuint lenA = progA->NumInstructions - 1; /* omit END instr */ |
| 469 | const GLuint lenB = progB->NumInstructions; |
| 470 | const GLuint numParamsA = _mesa_num_parameters(progA->Parameters); |
| 471 | const GLuint newLength = lenA + lenB; |
Brian | fb9cf48 | 2007-10-30 18:26:34 -0600 | [diff] [blame] | 472 | GLbitfield inputsB; |
Brian | 9ffd889 | 2007-10-29 16:35:59 -0600 | [diff] [blame] | 473 | GLuint i; |
| 474 | |
| 475 | ASSERT(progA->Target == progB->Target); |
| 476 | |
| 477 | newInst = _mesa_alloc_instructions(newLength); |
| 478 | if (!newInst) |
| 479 | return GL_FALSE; |
| 480 | |
| 481 | _mesa_copy_instructions(newInst, progA->Instructions, lenA); |
| 482 | _mesa_copy_instructions(newInst + lenA, progB->Instructions, lenB); |
| 483 | |
| 484 | /* adjust branch / instruction addresses for B's instructions */ |
| 485 | for (i = 0; i < lenB; i++) { |
| 486 | newInst[lenA + i].BranchTarget += lenA; |
| 487 | } |
| 488 | |
| 489 | newProg = ctx->Driver.NewProgram(ctx, progA->Target, 0); |
| 490 | newProg->Instructions = newInst; |
| 491 | newProg->NumInstructions = newLength; |
| 492 | |
| 493 | if (newProg->Target == GL_FRAGMENT_PROGRAM_ARB) { |
| 494 | /* connect color outputs/inputs */ |
| 495 | if ((progA->OutputsWritten & (1 << FRAG_RESULT_COLR)) && |
| 496 | (progB->InputsRead & (1 << FRAG_ATTRIB_COL0))) { |
| 497 | replace_registers(newInst + lenA, lenB, |
| 498 | PROGRAM_INPUT, FRAG_ATTRIB_COL0, |
| 499 | PROGRAM_OUTPUT, FRAG_RESULT_COLR); |
| 500 | } |
| 501 | |
Brian | fb9cf48 | 2007-10-30 18:26:34 -0600 | [diff] [blame] | 502 | inputsB = progB->InputsRead; |
| 503 | if (progA->OutputsWritten & (1 << FRAG_RESULT_COLR)) { |
| 504 | inputsB &= ~(1 << FRAG_ATTRIB_COL0); |
| 505 | } |
| 506 | newProg->InputsRead = progA->InputsRead | inputsB; |
Brian | 9ffd889 | 2007-10-29 16:35:59 -0600 | [diff] [blame] | 507 | newProg->OutputsWritten = progB->OutputsWritten; |
| 508 | } |
| 509 | else { |
| 510 | /* vertex program */ |
| 511 | assert(0); /* XXX todo */ |
| 512 | } |
| 513 | |
| 514 | /* |
| 515 | * Merge parameters (uniforms, constants, etc) |
| 516 | */ |
| 517 | newProg->Parameters = _mesa_combine_parameter_lists(progA->Parameters, |
| 518 | progB->Parameters); |
| 519 | |
| 520 | adjust_param_indexes(newInst + lenA, lenB, numParamsA); |
| 521 | |
| 522 | |
| 523 | return newProg; |
| 524 | } |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | /** |
| 530 | * Scan the given program to find a free register of the given type. |
| 531 | * \param regFile - PROGRAM_INPUT, PROGRAM_OUTPUT or PROGRAM_TEMPORARY |
| 532 | */ |
| 533 | GLint |
| 534 | _mesa_find_free_register(const struct gl_program *prog, GLuint regFile) |
| 535 | { |
| 536 | GLboolean used[MAX_PROGRAM_TEMPS]; |
| 537 | GLuint i, k; |
| 538 | |
| 539 | assert(regFile == PROGRAM_INPUT || |
| 540 | regFile == PROGRAM_OUTPUT || |
| 541 | regFile == PROGRAM_TEMPORARY); |
| 542 | |
| 543 | _mesa_memset(used, 0, sizeof(used)); |
| 544 | |
| 545 | for (i = 0; i < prog->NumInstructions; i++) { |
| 546 | const struct prog_instruction *inst = prog->Instructions + i; |
| 547 | const GLuint n = _mesa_num_inst_src_regs(inst->Opcode); |
| 548 | |
| 549 | for (k = 0; k < n; k++) { |
| 550 | if (inst->SrcReg[k].File == regFile) { |
| 551 | used[inst->SrcReg[k].Index] = GL_TRUE; |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | for (i = 0; i < MAX_PROGRAM_TEMPS; i++) { |
| 557 | if (!used[i]) |
| 558 | return i; |
| 559 | } |
| 560 | |
| 561 | return -1; |
| 562 | } |
| 563 | |
| 564 | |
| 565 | |
| 566 | /** |
Brian Paul | 77427a1 | 2006-08-24 23:11:39 +0000 | [diff] [blame] | 567 | * Mixing ARB and NV vertex/fragment programs can be tricky. |
| 568 | * Note: GL_VERTEX_PROGRAM_ARB == GL_VERTEX_PROGRAM_NV |
| 569 | * but, GL_FRAGMENT_PROGRAM_ARB != GL_FRAGMENT_PROGRAM_NV |
| 570 | * The two different fragment program targets are supposed to be compatible |
| 571 | * to some extent (see GL_ARB_fragment_program spec). |
| 572 | * This function does the compatibility check. |
| 573 | */ |
| 574 | static GLboolean |
| 575 | compatible_program_targets(GLenum t1, GLenum t2) |
| 576 | { |
| 577 | if (t1 == t2) |
| 578 | return GL_TRUE; |
| 579 | if (t1 == GL_FRAGMENT_PROGRAM_ARB && t2 == GL_FRAGMENT_PROGRAM_NV) |
| 580 | return GL_TRUE; |
| 581 | if (t1 == GL_FRAGMENT_PROGRAM_NV && t2 == GL_FRAGMENT_PROGRAM_ARB) |
| 582 | return GL_TRUE; |
| 583 | return GL_FALSE; |
| 584 | } |
| 585 | |
| 586 | |
Brian Paul | 30d6a4b | 2005-11-05 20:18:18 +0000 | [diff] [blame] | 587 | |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 588 | /**********************************************************************/ |
| 589 | /* API functions */ |
| 590 | /**********************************************************************/ |
| 591 | |
| 592 | |
| 593 | /** |
| 594 | * Bind a program (make it current) |
| 595 | * \note Called from the GL API dispatcher by both glBindProgramNV |
| 596 | * and glBindProgramARB. |
| 597 | */ |
| 598 | void GLAPIENTRY |
| 599 | _mesa_BindProgram(GLenum target, GLuint id) |
| 600 | { |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 601 | struct gl_program *curProg, *newProg; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 602 | GET_CURRENT_CONTEXT(ctx); |
| 603 | ASSERT_OUTSIDE_BEGIN_END(ctx); |
| 604 | |
| 605 | FLUSH_VERTICES(ctx, _NEW_PROGRAM); |
| 606 | |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 607 | /* Error-check target and get curProg */ |
Roland Scheidegger | e1e03b3 | 2006-03-03 15:03:04 +0000 | [diff] [blame] | 608 | if ((target == GL_VERTEX_PROGRAM_ARB) && /* == GL_VERTEX_PROGRAM_NV */ |
| 609 | (ctx->Extensions.NV_vertex_program || |
| 610 | ctx->Extensions.ARB_vertex_program)) { |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 611 | curProg = &ctx->VertexProgram.Current->Base; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 612 | } |
| 613 | else if ((target == GL_FRAGMENT_PROGRAM_NV |
| 614 | && ctx->Extensions.NV_fragment_program) || |
| 615 | (target == GL_FRAGMENT_PROGRAM_ARB |
| 616 | && ctx->Extensions.ARB_fragment_program)) { |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 617 | curProg = &ctx->FragmentProgram.Current->Base; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 618 | } |
| 619 | else { |
| 620 | _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)"); |
| 621 | return; |
| 622 | } |
| 623 | |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 624 | /* |
| 625 | * Get pointer to new program to bind. |
| 626 | * NOTE: binding to a non-existant program is not an error. |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 627 | * That's supposed to be caught in glBegin. |
| 628 | */ |
| 629 | if (id == 0) { |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 630 | /* Bind a default program */ |
| 631 | newProg = NULL; |
Roland Scheidegger | e1e03b3 | 2006-03-03 15:03:04 +0000 | [diff] [blame] | 632 | if (target == GL_VERTEX_PROGRAM_ARB) /* == GL_VERTEX_PROGRAM_NV */ |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 633 | newProg = ctx->Shared->DefaultVertexProgram; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 634 | else |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 635 | newProg = ctx->Shared->DefaultFragmentProgram; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 636 | } |
| 637 | else { |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 638 | /* Bind a user program */ |
| 639 | newProg = _mesa_lookup_program(ctx, id); |
| 640 | if (!newProg || newProg == &_mesa_DummyProgram) { |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 641 | /* allocate a new program now */ |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 642 | newProg = ctx->Driver.NewProgram(ctx, target, id); |
| 643 | if (!newProg) { |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 644 | _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB"); |
| 645 | return; |
| 646 | } |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 647 | _mesa_HashInsert(ctx->Shared->Programs, id, newProg); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 648 | } |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 649 | else if (!compatible_program_targets(newProg->Target, target)) { |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 650 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 651 | "glBindProgramNV/ARB(target mismatch)"); |
| 652 | return; |
| 653 | } |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 656 | /** All error checking is complete now **/ |
| 657 | |
| 658 | if (curProg->Id == id) { |
| 659 | /* binding same program - no change */ |
| 660 | return; |
| 661 | } |
| 662 | |
| 663 | /* unbind/delete oldProg */ |
| 664 | if (curProg->Id != 0) { |
| 665 | /* decrement refcount on previously bound fragment program */ |
| 666 | curProg->RefCount--; |
| 667 | /* and delete if refcount goes below one */ |
| 668 | if (curProg->RefCount <= 0) { |
| 669 | /* the program ID was already removed from the hash table */ |
| 670 | ctx->Driver.DeleteProgram(ctx, curProg); |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | /* bind newProg */ |
Roland Scheidegger | e1e03b3 | 2006-03-03 15:03:04 +0000 | [diff] [blame] | 675 | if (target == GL_VERTEX_PROGRAM_ARB) { /* == GL_VERTEX_PROGRAM_NV */ |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 676 | ctx->VertexProgram.Current = (struct gl_vertex_program *) newProg; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 677 | } |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 678 | else if (target == GL_FRAGMENT_PROGRAM_NV || |
| 679 | target == GL_FRAGMENT_PROGRAM_ARB) { |
| 680 | ctx->FragmentProgram.Current = (struct gl_fragment_program *) newProg; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 681 | } |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 682 | newProg->RefCount++; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 683 | |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 684 | /* Never null pointers */ |
| 685 | ASSERT(ctx->VertexProgram.Current); |
| 686 | ASSERT(ctx->FragmentProgram.Current); |
| 687 | |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 688 | if (ctx->Driver.BindProgram) |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 689 | ctx->Driver.BindProgram(ctx, target, newProg); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | |
| 693 | /** |
| 694 | * Delete a list of programs. |
| 695 | * \note Not compiled into display lists. |
| 696 | * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB. |
| 697 | */ |
| 698 | void GLAPIENTRY |
| 699 | _mesa_DeletePrograms(GLsizei n, const GLuint *ids) |
| 700 | { |
| 701 | GLint i; |
| 702 | GET_CURRENT_CONTEXT(ctx); |
| 703 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); |
| 704 | |
| 705 | if (n < 0) { |
| 706 | _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" ); |
| 707 | return; |
| 708 | } |
| 709 | |
| 710 | for (i = 0; i < n; i++) { |
| 711 | if (ids[i] != 0) { |
Brian Paul | 4d12a05 | 2006-08-23 23:10:14 +0000 | [diff] [blame] | 712 | struct gl_program *prog = _mesa_lookup_program(ctx, ids[i]); |
Brian Paul | 9ca8392 | 2004-10-02 15:16:59 +0000 | [diff] [blame] | 713 | if (prog == &_mesa_DummyProgram) { |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 714 | _mesa_HashRemove(ctx->Shared->Programs, ids[i]); |
| 715 | } |
| 716 | else if (prog) { |
| 717 | /* Unbind program if necessary */ |
Roland Scheidegger | e1e03b3 | 2006-03-03 15:03:04 +0000 | [diff] [blame] | 718 | if (prog->Target == GL_VERTEX_PROGRAM_ARB || /* == GL_VERTEX_PROGRAM_NV */ |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 719 | prog->Target == GL_VERTEX_STATE_PROGRAM_NV) { |
| 720 | if (ctx->VertexProgram.Current && |
| 721 | ctx->VertexProgram.Current->Base.Id == ids[i]) { |
| 722 | /* unbind this currently bound program */ |
| 723 | _mesa_BindProgram(prog->Target, 0); |
| 724 | } |
| 725 | } |
| 726 | else if (prog->Target == GL_FRAGMENT_PROGRAM_NV || |
| 727 | prog->Target == GL_FRAGMENT_PROGRAM_ARB) { |
| 728 | if (ctx->FragmentProgram.Current && |
| 729 | ctx->FragmentProgram.Current->Base.Id == ids[i]) { |
| 730 | /* unbind this currently bound program */ |
| 731 | _mesa_BindProgram(prog->Target, 0); |
| 732 | } |
| 733 | } |
| 734 | else { |
| 735 | _mesa_problem(ctx, "bad target in glDeleteProgramsNV"); |
| 736 | return; |
| 737 | } |
Brian Paul | ea2943e | 2005-01-20 04:02:02 +0000 | [diff] [blame] | 738 | /* The ID is immediately available for re-use now */ |
| 739 | _mesa_HashRemove(ctx->Shared->Programs, ids[i]); |
| 740 | prog->RefCount--; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 741 | if (prog->RefCount <= 0) { |
| 742 | ctx->Driver.DeleteProgram(ctx, prog); |
| 743 | } |
| 744 | } |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 745 | } |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | |
| 750 | /** |
| 751 | * Generate a list of new program identifiers. |
| 752 | * \note Not compiled into display lists. |
| 753 | * \note Called by both glGenProgramsNV and glGenProgramsARB. |
| 754 | */ |
| 755 | void GLAPIENTRY |
| 756 | _mesa_GenPrograms(GLsizei n, GLuint *ids) |
| 757 | { |
| 758 | GLuint first; |
| 759 | GLuint i; |
| 760 | GET_CURRENT_CONTEXT(ctx); |
| 761 | ASSERT_OUTSIDE_BEGIN_END(ctx); |
| 762 | |
| 763 | if (n < 0) { |
| 764 | _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms"); |
| 765 | return; |
| 766 | } |
| 767 | |
| 768 | if (!ids) |
| 769 | return; |
| 770 | |
| 771 | first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n); |
| 772 | |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 773 | /* Insert pointer to dummy program as placeholder */ |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 774 | for (i = 0; i < (GLuint) n; i++) { |
Brian Paul | 9ca8392 | 2004-10-02 15:16:59 +0000 | [diff] [blame] | 775 | _mesa_HashInsert(ctx->Shared->Programs, first + i, &_mesa_DummyProgram); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | /* Return the program names */ |
| 779 | for (i = 0; i < (GLuint) n; i++) { |
| 780 | ids[i] = first + i; |
| 781 | } |
| 782 | } |