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 | fe1d01c | 2006-12-13 14:54:47 -0700 | [diff] [blame] | 190 | _mesa_bzero(prog, sizeof(*prog)); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 191 | prog->Id = id; |
| 192 | prog->Target = target; |
| 193 | prog->Resident = GL_TRUE; |
| 194 | prog->RefCount = 1; |
Brian Paul | 308b85f | 2006-11-23 15:58:30 +0000 | [diff] [blame] | 195 | prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | return prog; |
| 199 | } |
| 200 | |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 201 | |
| 202 | /** |
| 203 | * Initialize a new fragment program object. |
| 204 | */ |
Brian Paul | 122629f | 2006-07-20 16:49:57 +0000 | [diff] [blame] | 205 | struct gl_program * |
| 206 | _mesa_init_fragment_program( GLcontext *ctx, struct gl_fragment_program *prog, |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 207 | GLenum target, GLuint id) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 208 | { |
| 209 | if (prog) |
| 210 | return _mesa_init_program_struct( ctx, &prog->Base, target, id ); |
| 211 | else |
| 212 | return NULL; |
| 213 | } |
| 214 | |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 215 | |
| 216 | /** |
| 217 | * Initialize a new vertex program object. |
| 218 | */ |
Brian Paul | 122629f | 2006-07-20 16:49:57 +0000 | [diff] [blame] | 219 | struct gl_program * |
| 220 | _mesa_init_vertex_program( GLcontext *ctx, struct gl_vertex_program *prog, |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 221 | GLenum target, GLuint id) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 222 | { |
| 223 | if (prog) |
| 224 | return _mesa_init_program_struct( ctx, &prog->Base, target, id ); |
| 225 | else |
| 226 | return NULL; |
| 227 | } |
| 228 | |
| 229 | |
| 230 | /** |
| 231 | * Allocate and initialize a new fragment/vertex program object but |
| 232 | * don't put it into the program hash table. Called via |
| 233 | * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a |
| 234 | * device driver function to implement OO deriviation with additional |
| 235 | * types not understood by this function. |
| 236 | * |
| 237 | * \param ctx context |
| 238 | * \param id program id/number |
| 239 | * \param target program target/type |
| 240 | * \return pointer to new program object |
| 241 | */ |
Brian Paul | 122629f | 2006-07-20 16:49:57 +0000 | [diff] [blame] | 242 | struct gl_program * |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 243 | _mesa_new_program(GLcontext *ctx, GLenum target, GLuint id) |
| 244 | { |
| 245 | switch (target) { |
| 246 | case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */ |
Brian Paul | 122629f | 2006-07-20 16:49:57 +0000 | [diff] [blame] | 247 | return _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program), |
| 248 | target, id ); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 249 | case GL_FRAGMENT_PROGRAM_NV: |
| 250 | case GL_FRAGMENT_PROGRAM_ARB: |
Brian Paul | 122629f | 2006-07-20 16:49:57 +0000 | [diff] [blame] | 251 | return _mesa_init_fragment_program(ctx, |
| 252 | CALLOC_STRUCT(gl_fragment_program), |
| 253 | target, id ); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 254 | default: |
| 255 | _mesa_problem(ctx, "bad target in _mesa_new_program"); |
| 256 | return NULL; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | |
| 261 | /** |
| 262 | * Delete a program and remove it from the hash table, ignoring the |
| 263 | * reference count. |
| 264 | * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation) |
| 265 | * by a device driver function. |
| 266 | */ |
| 267 | void |
Brian Paul | 122629f | 2006-07-20 16:49:57 +0000 | [diff] [blame] | 268 | _mesa_delete_program(GLcontext *ctx, struct gl_program *prog) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 269 | { |
Brian Paul | a6c423d | 2004-08-25 15:59:48 +0000 | [diff] [blame] | 270 | (void) ctx; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 271 | ASSERT(prog); |
| 272 | |
Brian Paul | 308b85f | 2006-11-23 15:58:30 +0000 | [diff] [blame] | 273 | if (prog == &_mesa_DummyProgram) |
| 274 | return; |
| 275 | |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 276 | if (prog->String) |
| 277 | _mesa_free(prog->String); |
Brian Paul | de99760 | 2005-11-12 17:53:14 +0000 | [diff] [blame] | 278 | |
| 279 | if (prog->Instructions) { |
| 280 | GLuint i; |
| 281 | for (i = 0; i < prog->NumInstructions; i++) { |
| 282 | if (prog->Instructions[i].Data) |
| 283 | _mesa_free(prog->Instructions[i].Data); |
Brian | 4cc2674 | 2007-04-20 08:12:17 -0600 | [diff] [blame] | 284 | if (prog->Instructions[i].Comment) |
| 285 | _mesa_free((char *) prog->Instructions[i].Comment); |
Brian Paul | 575700f | 2004-12-16 03:07:18 +0000 | [diff] [blame] | 286 | } |
Brian Paul | de99760 | 2005-11-12 17:53:14 +0000 | [diff] [blame] | 287 | _mesa_free(prog->Instructions); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 288 | } |
Brian Paul | de99760 | 2005-11-12 17:53:14 +0000 | [diff] [blame] | 289 | |
Brian Paul | 65a51c0 | 2006-05-24 03:30:31 +0000 | [diff] [blame] | 290 | if (prog->Parameters) { |
Brian Paul | de99760 | 2005-11-12 17:53:14 +0000 | [diff] [blame] | 291 | _mesa_free_parameter_list(prog->Parameters); |
Brian Paul | 65a51c0 | 2006-05-24 03:30:31 +0000 | [diff] [blame] | 292 | } |
Brian | fe1d01c | 2006-12-13 14:54:47 -0700 | [diff] [blame] | 293 | if (prog->Varying) { |
| 294 | _mesa_free_parameter_list(prog->Varying); |
| 295 | } |
Brian | 3493e86 | 2007-03-24 16:18:13 -0600 | [diff] [blame] | 296 | if (prog->Attributes) { |
| 297 | _mesa_free_parameter_list(prog->Attributes); |
| 298 | } |
Brian | fe1d01c | 2006-12-13 14:54:47 -0700 | [diff] [blame] | 299 | |
Brian Paul | 58d080b | 2006-08-25 19:46:31 +0000 | [diff] [blame] | 300 | /* XXX this is a little ugly */ |
| 301 | if (prog->Target == GL_VERTEX_PROGRAM_ARB) { |
| 302 | struct gl_vertex_program *vprog = (struct gl_vertex_program *) prog; |
| 303 | if (vprog->TnlData) |
| 304 | _mesa_free(vprog->TnlData); |
| 305 | } |
| 306 | |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 307 | _mesa_free(prog); |
| 308 | } |
| 309 | |
| 310 | |
Brian Paul | 4d12a05 | 2006-08-23 23:10:14 +0000 | [diff] [blame] | 311 | /** |
| 312 | * Return the gl_program object for a given ID. |
| 313 | * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of |
| 314 | * casts elsewhere. |
| 315 | */ |
| 316 | struct gl_program * |
| 317 | _mesa_lookup_program(GLcontext *ctx, GLuint id) |
| 318 | { |
| 319 | if (id) |
| 320 | return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id); |
| 321 | else |
| 322 | return NULL; |
| 323 | } |
| 324 | |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 325 | |
Brian Paul | 3b9b8de | 2006-08-24 21:57:36 +0000 | [diff] [blame] | 326 | /** |
Brian | b2a3a85 | 2006-12-14 13:56:58 -0700 | [diff] [blame] | 327 | * Return a copy of a program. |
| 328 | * XXX Problem here if the program object is actually OO-derivation |
| 329 | * made by a device driver. |
| 330 | */ |
| 331 | struct gl_program * |
| 332 | _mesa_clone_program(GLcontext *ctx, const struct gl_program *prog) |
| 333 | { |
| 334 | struct gl_program *clone; |
| 335 | |
Brian | 5b6858c | 2007-07-24 09:56:44 -0600 | [diff] [blame^] | 336 | clone = ctx->Driver.NewProgram(ctx, prog->Target, prog->Id); |
Brian | b2a3a85 | 2006-12-14 13:56:58 -0700 | [diff] [blame] | 337 | if (!clone) |
| 338 | return NULL; |
| 339 | |
| 340 | assert(clone->Target == prog->Target); |
| 341 | clone->String = (GLubyte *) _mesa_strdup((char *) prog->String); |
| 342 | clone->RefCount = 1; |
| 343 | clone->Format = prog->Format; |
| 344 | clone->Instructions = _mesa_alloc_instructions(prog->NumInstructions); |
| 345 | if (!clone->Instructions) { |
| 346 | _mesa_delete_program(ctx, clone); |
| 347 | return NULL; |
| 348 | } |
Brian | 12229f1 | 2007-03-22 09:11:26 -0600 | [diff] [blame] | 349 | _mesa_copy_instructions(clone->Instructions, prog->Instructions, |
| 350 | prog->NumInstructions); |
Brian | b2a3a85 | 2006-12-14 13:56:58 -0700 | [diff] [blame] | 351 | clone->InputsRead = prog->InputsRead; |
| 352 | clone->OutputsWritten = prog->OutputsWritten; |
Brian | c9db223 | 2007-01-04 17:22:19 -0700 | [diff] [blame] | 353 | memcpy(clone->TexturesUsed, prog->TexturesUsed, sizeof(prog->TexturesUsed)); |
| 354 | |
Brian | 2e76f0a | 2006-12-19 09:52:07 -0700 | [diff] [blame] | 355 | if (prog->Parameters) |
| 356 | clone->Parameters = _mesa_clone_parameter_list(prog->Parameters); |
Brian | b2a3a85 | 2006-12-14 13:56:58 -0700 | [diff] [blame] | 357 | memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams)); |
Brian | 2e76f0a | 2006-12-19 09:52:07 -0700 | [diff] [blame] | 358 | if (prog->Varying) |
| 359 | clone->Varying = _mesa_clone_parameter_list(prog->Varying); |
Brian | 3209c3e | 2007-01-09 17:49:24 -0700 | [diff] [blame] | 360 | if (prog->Attributes) |
| 361 | clone->Attributes = _mesa_clone_parameter_list(prog->Attributes); |
Brian | b2a3a85 | 2006-12-14 13:56:58 -0700 | [diff] [blame] | 362 | memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams)); |
| 363 | clone->NumInstructions = prog->NumInstructions; |
| 364 | clone->NumTemporaries = prog->NumTemporaries; |
| 365 | clone->NumParameters = prog->NumParameters; |
| 366 | clone->NumAttributes = prog->NumAttributes; |
| 367 | clone->NumAddressRegs = prog->NumAddressRegs; |
| 368 | clone->NumNativeInstructions = prog->NumNativeInstructions; |
| 369 | clone->NumNativeTemporaries = prog->NumNativeTemporaries; |
| 370 | clone->NumNativeParameters = prog->NumNativeParameters; |
| 371 | clone->NumNativeAttributes = prog->NumNativeAttributes; |
| 372 | clone->NumNativeAddressRegs = prog->NumNativeAddressRegs; |
Brian | 21f9979 | 2007-01-09 11:00:21 -0700 | [diff] [blame] | 373 | clone->NumAluInstructions = prog->NumAluInstructions; |
| 374 | clone->NumTexInstructions = prog->NumTexInstructions; |
| 375 | clone->NumTexIndirections = prog->NumTexIndirections; |
| 376 | clone->NumNativeAluInstructions = prog->NumNativeAluInstructions; |
| 377 | clone->NumNativeTexInstructions = prog->NumNativeTexInstructions; |
| 378 | clone->NumNativeTexIndirections = prog->NumNativeTexIndirections; |
Brian | b2a3a85 | 2006-12-14 13:56:58 -0700 | [diff] [blame] | 379 | |
| 380 | switch (prog->Target) { |
| 381 | case GL_VERTEX_PROGRAM_ARB: |
| 382 | { |
| 383 | const struct gl_vertex_program *vp |
| 384 | = (const struct gl_vertex_program *) prog; |
| 385 | struct gl_vertex_program *vpc = (struct gl_vertex_program *) clone; |
| 386 | vpc->IsPositionInvariant = vp->IsPositionInvariant; |
| 387 | } |
| 388 | break; |
| 389 | case GL_FRAGMENT_PROGRAM_ARB: |
| 390 | { |
| 391 | const struct gl_fragment_program *fp |
| 392 | = (const struct gl_fragment_program *) prog; |
| 393 | struct gl_fragment_program *fpc = (struct gl_fragment_program *) clone; |
Brian | b2a3a85 | 2006-12-14 13:56:58 -0700 | [diff] [blame] | 394 | fpc->FogOption = fp->FogOption; |
| 395 | fpc->UsesKill = fp->UsesKill; |
| 396 | } |
| 397 | break; |
| 398 | default: |
| 399 | _mesa_problem(NULL, "Unexpected target in _mesa_clone_program"); |
| 400 | } |
| 401 | |
| 402 | return clone; |
| 403 | } |
| 404 | |
| 405 | |
| 406 | |
| 407 | /** |
Brian Paul | 77427a1 | 2006-08-24 23:11:39 +0000 | [diff] [blame] | 408 | * Mixing ARB and NV vertex/fragment programs can be tricky. |
| 409 | * Note: GL_VERTEX_PROGRAM_ARB == GL_VERTEX_PROGRAM_NV |
| 410 | * but, GL_FRAGMENT_PROGRAM_ARB != GL_FRAGMENT_PROGRAM_NV |
| 411 | * The two different fragment program targets are supposed to be compatible |
| 412 | * to some extent (see GL_ARB_fragment_program spec). |
| 413 | * This function does the compatibility check. |
| 414 | */ |
| 415 | static GLboolean |
| 416 | compatible_program_targets(GLenum t1, GLenum t2) |
| 417 | { |
| 418 | if (t1 == t2) |
| 419 | return GL_TRUE; |
| 420 | if (t1 == GL_FRAGMENT_PROGRAM_ARB && t2 == GL_FRAGMENT_PROGRAM_NV) |
| 421 | return GL_TRUE; |
| 422 | if (t1 == GL_FRAGMENT_PROGRAM_NV && t2 == GL_FRAGMENT_PROGRAM_ARB) |
| 423 | return GL_TRUE; |
| 424 | return GL_FALSE; |
| 425 | } |
| 426 | |
| 427 | |
Brian Paul | 30d6a4b | 2005-11-05 20:18:18 +0000 | [diff] [blame] | 428 | |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 429 | /**********************************************************************/ |
| 430 | /* API functions */ |
| 431 | /**********************************************************************/ |
| 432 | |
| 433 | |
| 434 | /** |
| 435 | * Bind a program (make it current) |
| 436 | * \note Called from the GL API dispatcher by both glBindProgramNV |
| 437 | * and glBindProgramARB. |
| 438 | */ |
| 439 | void GLAPIENTRY |
| 440 | _mesa_BindProgram(GLenum target, GLuint id) |
| 441 | { |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 442 | struct gl_program *curProg, *newProg; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 443 | GET_CURRENT_CONTEXT(ctx); |
| 444 | ASSERT_OUTSIDE_BEGIN_END(ctx); |
| 445 | |
| 446 | FLUSH_VERTICES(ctx, _NEW_PROGRAM); |
| 447 | |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 448 | /* Error-check target and get curProg */ |
Roland Scheidegger | e1e03b3 | 2006-03-03 15:03:04 +0000 | [diff] [blame] | 449 | if ((target == GL_VERTEX_PROGRAM_ARB) && /* == GL_VERTEX_PROGRAM_NV */ |
| 450 | (ctx->Extensions.NV_vertex_program || |
| 451 | ctx->Extensions.ARB_vertex_program)) { |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 452 | curProg = &ctx->VertexProgram.Current->Base; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 453 | } |
| 454 | else if ((target == GL_FRAGMENT_PROGRAM_NV |
| 455 | && ctx->Extensions.NV_fragment_program) || |
| 456 | (target == GL_FRAGMENT_PROGRAM_ARB |
| 457 | && ctx->Extensions.ARB_fragment_program)) { |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 458 | curProg = &ctx->FragmentProgram.Current->Base; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 459 | } |
| 460 | else { |
| 461 | _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)"); |
| 462 | return; |
| 463 | } |
| 464 | |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 465 | /* |
| 466 | * Get pointer to new program to bind. |
| 467 | * NOTE: binding to a non-existant program is not an error. |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 468 | * That's supposed to be caught in glBegin. |
| 469 | */ |
| 470 | if (id == 0) { |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 471 | /* Bind a default program */ |
| 472 | newProg = NULL; |
Roland Scheidegger | e1e03b3 | 2006-03-03 15:03:04 +0000 | [diff] [blame] | 473 | if (target == GL_VERTEX_PROGRAM_ARB) /* == GL_VERTEX_PROGRAM_NV */ |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 474 | newProg = ctx->Shared->DefaultVertexProgram; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 475 | else |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 476 | newProg = ctx->Shared->DefaultFragmentProgram; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 477 | } |
| 478 | else { |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 479 | /* Bind a user program */ |
| 480 | newProg = _mesa_lookup_program(ctx, id); |
| 481 | if (!newProg || newProg == &_mesa_DummyProgram) { |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 482 | /* allocate a new program now */ |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 483 | newProg = ctx->Driver.NewProgram(ctx, target, id); |
| 484 | if (!newProg) { |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 485 | _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB"); |
| 486 | return; |
| 487 | } |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 488 | _mesa_HashInsert(ctx->Shared->Programs, id, newProg); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 489 | } |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 490 | else if (!compatible_program_targets(newProg->Target, target)) { |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 491 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 492 | "glBindProgramNV/ARB(target mismatch)"); |
| 493 | return; |
| 494 | } |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 497 | /** All error checking is complete now **/ |
| 498 | |
| 499 | if (curProg->Id == id) { |
| 500 | /* binding same program - no change */ |
| 501 | return; |
| 502 | } |
| 503 | |
| 504 | /* unbind/delete oldProg */ |
| 505 | if (curProg->Id != 0) { |
| 506 | /* decrement refcount on previously bound fragment program */ |
| 507 | curProg->RefCount--; |
| 508 | /* and delete if refcount goes below one */ |
| 509 | if (curProg->RefCount <= 0) { |
| 510 | /* the program ID was already removed from the hash table */ |
| 511 | ctx->Driver.DeleteProgram(ctx, curProg); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | /* bind newProg */ |
Roland Scheidegger | e1e03b3 | 2006-03-03 15:03:04 +0000 | [diff] [blame] | 516 | if (target == GL_VERTEX_PROGRAM_ARB) { /* == GL_VERTEX_PROGRAM_NV */ |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 517 | ctx->VertexProgram.Current = (struct gl_vertex_program *) newProg; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 518 | } |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 519 | else if (target == GL_FRAGMENT_PROGRAM_NV || |
| 520 | target == GL_FRAGMENT_PROGRAM_ARB) { |
| 521 | ctx->FragmentProgram.Current = (struct gl_fragment_program *) newProg; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 522 | } |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 523 | newProg->RefCount++; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 524 | |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 525 | /* Never null pointers */ |
| 526 | ASSERT(ctx->VertexProgram.Current); |
| 527 | ASSERT(ctx->FragmentProgram.Current); |
| 528 | |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 529 | if (ctx->Driver.BindProgram) |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 530 | ctx->Driver.BindProgram(ctx, target, newProg); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | |
| 534 | /** |
| 535 | * Delete a list of programs. |
| 536 | * \note Not compiled into display lists. |
| 537 | * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB. |
| 538 | */ |
| 539 | void GLAPIENTRY |
| 540 | _mesa_DeletePrograms(GLsizei n, const GLuint *ids) |
| 541 | { |
| 542 | GLint i; |
| 543 | GET_CURRENT_CONTEXT(ctx); |
| 544 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); |
| 545 | |
| 546 | if (n < 0) { |
| 547 | _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" ); |
| 548 | return; |
| 549 | } |
| 550 | |
| 551 | for (i = 0; i < n; i++) { |
| 552 | if (ids[i] != 0) { |
Brian Paul | 4d12a05 | 2006-08-23 23:10:14 +0000 | [diff] [blame] | 553 | struct gl_program *prog = _mesa_lookup_program(ctx, ids[i]); |
Brian Paul | 9ca8392 | 2004-10-02 15:16:59 +0000 | [diff] [blame] | 554 | if (prog == &_mesa_DummyProgram) { |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 555 | _mesa_HashRemove(ctx->Shared->Programs, ids[i]); |
| 556 | } |
| 557 | else if (prog) { |
| 558 | /* Unbind program if necessary */ |
Roland Scheidegger | e1e03b3 | 2006-03-03 15:03:04 +0000 | [diff] [blame] | 559 | if (prog->Target == GL_VERTEX_PROGRAM_ARB || /* == GL_VERTEX_PROGRAM_NV */ |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 560 | prog->Target == GL_VERTEX_STATE_PROGRAM_NV) { |
| 561 | if (ctx->VertexProgram.Current && |
| 562 | ctx->VertexProgram.Current->Base.Id == ids[i]) { |
| 563 | /* unbind this currently bound program */ |
| 564 | _mesa_BindProgram(prog->Target, 0); |
| 565 | } |
| 566 | } |
| 567 | else if (prog->Target == GL_FRAGMENT_PROGRAM_NV || |
| 568 | prog->Target == GL_FRAGMENT_PROGRAM_ARB) { |
| 569 | if (ctx->FragmentProgram.Current && |
| 570 | ctx->FragmentProgram.Current->Base.Id == ids[i]) { |
| 571 | /* unbind this currently bound program */ |
| 572 | _mesa_BindProgram(prog->Target, 0); |
| 573 | } |
| 574 | } |
| 575 | else { |
| 576 | _mesa_problem(ctx, "bad target in glDeleteProgramsNV"); |
| 577 | return; |
| 578 | } |
Brian Paul | ea2943e | 2005-01-20 04:02:02 +0000 | [diff] [blame] | 579 | /* The ID is immediately available for re-use now */ |
| 580 | _mesa_HashRemove(ctx->Shared->Programs, ids[i]); |
| 581 | prog->RefCount--; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 582 | if (prog->RefCount <= 0) { |
| 583 | ctx->Driver.DeleteProgram(ctx, prog); |
| 584 | } |
| 585 | } |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 586 | } |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | |
| 591 | /** |
| 592 | * Generate a list of new program identifiers. |
| 593 | * \note Not compiled into display lists. |
| 594 | * \note Called by both glGenProgramsNV and glGenProgramsARB. |
| 595 | */ |
| 596 | void GLAPIENTRY |
| 597 | _mesa_GenPrograms(GLsizei n, GLuint *ids) |
| 598 | { |
| 599 | GLuint first; |
| 600 | GLuint i; |
| 601 | GET_CURRENT_CONTEXT(ctx); |
| 602 | ASSERT_OUTSIDE_BEGIN_END(ctx); |
| 603 | |
| 604 | if (n < 0) { |
| 605 | _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms"); |
| 606 | return; |
| 607 | } |
| 608 | |
| 609 | if (!ids) |
| 610 | return; |
| 611 | |
| 612 | first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n); |
| 613 | |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 614 | /* Insert pointer to dummy program as placeholder */ |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 615 | for (i = 0; i < (GLuint) n; i++) { |
Brian Paul | 9ca8392 | 2004-10-02 15:16:59 +0000 | [diff] [blame] | 616 | _mesa_HashInsert(ctx->Shared->Programs, first + i, &_mesa_DummyProgram); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | /* Return the program names */ |
| 620 | for (i = 0; i < (GLuint) n; i++) { |
| 621 | ids[i] = first + i; |
| 622 | } |
| 623 | } |