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 | df43fb6 | 2008-05-06 23:08:51 -0600 | [diff] [blame] | 62 | _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current, |
| 63 | ctx->Shared->DefaultVertexProgram); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 64 | assert(ctx->VertexProgram.Current); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 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 | df43fb6 | 2008-05-06 23:08:51 -0600 | [diff] [blame] | 73 | _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current, |
| 74 | ctx->Shared->DefaultFragmentProgram); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 75 | assert(ctx->FragmentProgram.Current); |
| 76 | ctx->FragmentProgram.Current->Base.RefCount++; |
| 77 | #endif |
Dave Airlie | 7f752fe | 2004-12-19 03:06:59 +0000 | [diff] [blame] | 78 | |
Brian Paul | 63d6830 | 2005-11-19 16:43:04 +0000 | [diff] [blame] | 79 | /* XXX probably move this stuff */ |
Dave Airlie | 7f752fe | 2004-12-19 03:06:59 +0000 | [diff] [blame] | 80 | #if FEATURE_ATI_fragment_shader |
| 81 | ctx->ATIFragmentShader.Enabled = GL_FALSE; |
| 82 | ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader; |
| 83 | assert(ctx->ATIFragmentShader.Current); |
Brian Paul | 63d6830 | 2005-11-19 16:43:04 +0000 | [diff] [blame] | 84 | ctx->ATIFragmentShader.Current->RefCount++; |
Dave Airlie | 7f752fe | 2004-12-19 03:06:59 +0000 | [diff] [blame] | 85 | #endif |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | |
| 89 | /** |
Brian Paul | 21841f0 | 2004-08-14 14:28:11 +0000 | [diff] [blame] | 90 | * Free a context's vertex/fragment program state |
| 91 | */ |
| 92 | void |
| 93 | _mesa_free_program_data(GLcontext *ctx) |
| 94 | { |
Roland Scheidegger | 93da673 | 2006-03-01 23:11:14 +0000 | [diff] [blame] | 95 | #if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program |
Brian | df43fb6 | 2008-05-06 23:08:51 -0600 | [diff] [blame] | 96 | _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current, NULL); |
Brian Paul | 21841f0 | 2004-08-14 14:28:11 +0000 | [diff] [blame] | 97 | #endif |
Roland Scheidegger | 93da673 | 2006-03-01 23:11:14 +0000 | [diff] [blame] | 98 | #if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program |
Brian | df43fb6 | 2008-05-06 23:08:51 -0600 | [diff] [blame] | 99 | _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current, NULL); |
Brian Paul | 21841f0 | 2004-08-14 14:28:11 +0000 | [diff] [blame] | 100 | #endif |
Brian Paul | 63d6830 | 2005-11-19 16:43:04 +0000 | [diff] [blame] | 101 | /* XXX probably move this stuff */ |
Dave Airlie | 7f752fe | 2004-12-19 03:06:59 +0000 | [diff] [blame] | 102 | #if FEATURE_ATI_fragment_shader |
| 103 | if (ctx->ATIFragmentShader.Current) { |
Brian Paul | 63d6830 | 2005-11-19 16:43:04 +0000 | [diff] [blame] | 104 | ctx->ATIFragmentShader.Current->RefCount--; |
| 105 | if (ctx->ATIFragmentShader.Current->RefCount <= 0) { |
| 106 | _mesa_free(ctx->ATIFragmentShader.Current); |
| 107 | } |
Dave Airlie | 7f752fe | 2004-12-19 03:06:59 +0000 | [diff] [blame] | 108 | } |
| 109 | #endif |
Brian Paul | 21841f0 | 2004-08-14 14:28:11 +0000 | [diff] [blame] | 110 | _mesa_free((void *) ctx->Program.ErrorString); |
| 111 | } |
| 112 | |
| 113 | |
Brian | 4b654d4 | 2007-08-23 08:53:43 +0100 | [diff] [blame] | 114 | /** |
| 115 | * Update the default program objects in the given context to reference those |
| 116 | * specified in the shared state and release those referencing the old |
| 117 | * shared state. |
| 118 | */ |
| 119 | void |
| 120 | _mesa_update_default_objects_program(GLcontext *ctx) |
| 121 | { |
| 122 | #if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program |
| 123 | if (ctx->VertexProgram.Current) { |
| 124 | ctx->VertexProgram.Current->Base.RefCount--; |
| 125 | if (ctx->VertexProgram.Current->Base.RefCount <= 0) |
| 126 | ctx->Driver.DeleteProgram(ctx, &(ctx->VertexProgram.Current->Base)); |
| 127 | } |
| 128 | ctx->VertexProgram.Current = (struct gl_vertex_program *) ctx->Shared->DefaultVertexProgram; |
| 129 | assert(ctx->VertexProgram.Current); |
| 130 | ctx->VertexProgram.Current->Base.RefCount++; |
| 131 | #endif |
| 132 | |
| 133 | #if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program |
| 134 | if (ctx->FragmentProgram.Current) { |
| 135 | ctx->FragmentProgram.Current->Base.RefCount--; |
| 136 | if (ctx->FragmentProgram.Current->Base.RefCount <= 0) |
| 137 | ctx->Driver.DeleteProgram(ctx, &(ctx->FragmentProgram.Current->Base)); |
| 138 | } |
| 139 | ctx->FragmentProgram.Current = (struct gl_fragment_program *) ctx->Shared->DefaultFragmentProgram; |
| 140 | assert(ctx->FragmentProgram.Current); |
| 141 | ctx->FragmentProgram.Current->Base.RefCount++; |
| 142 | #endif |
| 143 | |
| 144 | /* XXX probably move this stuff */ |
| 145 | #if FEATURE_ATI_fragment_shader |
| 146 | if (ctx->ATIFragmentShader.Current) { |
| 147 | ctx->ATIFragmentShader.Current->RefCount--; |
| 148 | if (ctx->ATIFragmentShader.Current->RefCount <= 0) { |
| 149 | _mesa_free(ctx->ATIFragmentShader.Current); |
| 150 | } |
| 151 | } |
| 152 | ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader; |
| 153 | assert(ctx->ATIFragmentShader.Current); |
| 154 | ctx->ATIFragmentShader.Current->RefCount++; |
| 155 | #endif |
| 156 | } |
Brian Paul | 21841f0 | 2004-08-14 14:28:11 +0000 | [diff] [blame] | 157 | |
| 158 | |
| 159 | /** |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 160 | * Set the vertex/fragment program error state (position and error string). |
| 161 | * This is generally called from within the parsers. |
| 162 | */ |
| 163 | void |
| 164 | _mesa_set_program_error(GLcontext *ctx, GLint pos, const char *string) |
| 165 | { |
| 166 | ctx->Program.ErrorPos = pos; |
| 167 | _mesa_free((void *) ctx->Program.ErrorString); |
| 168 | if (!string) |
| 169 | string = ""; |
| 170 | ctx->Program.ErrorString = _mesa_strdup(string); |
| 171 | } |
| 172 | |
| 173 | |
| 174 | /** |
| 175 | * Find the line number and column for 'pos' within 'string'. |
| 176 | * Return a copy of the line which contains 'pos'. Free the line with |
| 177 | * _mesa_free(). |
| 178 | * \param string the program string |
| 179 | * \param pos the position within the string |
| 180 | * \param line returns the line number corresponding to 'pos'. |
| 181 | * \param col returns the column number corresponding to 'pos'. |
| 182 | * \return copy of the line containing 'pos'. |
| 183 | */ |
| 184 | const GLubyte * |
| 185 | _mesa_find_line_column(const GLubyte *string, const GLubyte *pos, |
| 186 | GLint *line, GLint *col) |
| 187 | { |
| 188 | const GLubyte *lineStart = string; |
| 189 | const GLubyte *p = string; |
| 190 | GLubyte *s; |
| 191 | int len; |
| 192 | |
| 193 | *line = 1; |
| 194 | |
| 195 | while (p != pos) { |
| 196 | if (*p == (GLubyte) '\n') { |
| 197 | (*line)++; |
| 198 | lineStart = p + 1; |
| 199 | } |
| 200 | p++; |
| 201 | } |
| 202 | |
| 203 | *col = (pos - lineStart) + 1; |
| 204 | |
| 205 | /* return copy of this line */ |
| 206 | while (*p != 0 && *p != '\n') |
| 207 | p++; |
| 208 | len = p - lineStart; |
| 209 | s = (GLubyte *) _mesa_malloc(len + 1); |
| 210 | _mesa_memcpy(s, lineStart, len); |
| 211 | s[len] = 0; |
| 212 | |
| 213 | return s; |
| 214 | } |
| 215 | |
| 216 | |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 217 | /** |
| 218 | * Initialize a new vertex/fragment program object. |
| 219 | */ |
Brian Paul | 122629f | 2006-07-20 16:49:57 +0000 | [diff] [blame] | 220 | static struct gl_program * |
| 221 | _mesa_init_program_struct( GLcontext *ctx, struct gl_program *prog, |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 222 | GLenum target, GLuint id) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 223 | { |
Brian Paul | a6c423d | 2004-08-25 15:59:48 +0000 | [diff] [blame] | 224 | (void) ctx; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 225 | if (prog) { |
| 226 | prog->Id = id; |
| 227 | prog->Target = target; |
| 228 | prog->Resident = GL_TRUE; |
| 229 | prog->RefCount = 1; |
Brian Paul | 308b85f | 2006-11-23 15:58:30 +0000 | [diff] [blame] | 230 | prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | return prog; |
| 234 | } |
| 235 | |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 236 | |
| 237 | /** |
| 238 | * Initialize a new fragment program object. |
| 239 | */ |
Brian Paul | 122629f | 2006-07-20 16:49:57 +0000 | [diff] [blame] | 240 | struct gl_program * |
| 241 | _mesa_init_fragment_program( GLcontext *ctx, struct gl_fragment_program *prog, |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 242 | GLenum target, GLuint id) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 243 | { |
| 244 | if (prog) |
| 245 | return _mesa_init_program_struct( ctx, &prog->Base, target, id ); |
| 246 | else |
| 247 | return NULL; |
| 248 | } |
| 249 | |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 250 | |
| 251 | /** |
| 252 | * Initialize a new vertex program object. |
| 253 | */ |
Brian Paul | 122629f | 2006-07-20 16:49:57 +0000 | [diff] [blame] | 254 | struct gl_program * |
| 255 | _mesa_init_vertex_program( GLcontext *ctx, struct gl_vertex_program *prog, |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 256 | GLenum target, GLuint id) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 257 | { |
| 258 | if (prog) |
| 259 | return _mesa_init_program_struct( ctx, &prog->Base, target, id ); |
| 260 | else |
| 261 | return NULL; |
| 262 | } |
| 263 | |
| 264 | |
| 265 | /** |
| 266 | * Allocate and initialize a new fragment/vertex program object but |
| 267 | * don't put it into the program hash table. Called via |
| 268 | * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a |
| 269 | * device driver function to implement OO deriviation with additional |
| 270 | * types not understood by this function. |
| 271 | * |
| 272 | * \param ctx context |
| 273 | * \param id program id/number |
| 274 | * \param target program target/type |
| 275 | * \return pointer to new program object |
| 276 | */ |
Brian Paul | 122629f | 2006-07-20 16:49:57 +0000 | [diff] [blame] | 277 | struct gl_program * |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 278 | _mesa_new_program(GLcontext *ctx, GLenum target, GLuint id) |
| 279 | { |
| 280 | switch (target) { |
| 281 | case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */ |
Brian Paul | 122629f | 2006-07-20 16:49:57 +0000 | [diff] [blame] | 282 | return _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program), |
| 283 | target, id ); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 284 | case GL_FRAGMENT_PROGRAM_NV: |
| 285 | case GL_FRAGMENT_PROGRAM_ARB: |
Brian Paul | 122629f | 2006-07-20 16:49:57 +0000 | [diff] [blame] | 286 | return _mesa_init_fragment_program(ctx, |
| 287 | CALLOC_STRUCT(gl_fragment_program), |
| 288 | target, id ); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 289 | default: |
| 290 | _mesa_problem(ctx, "bad target in _mesa_new_program"); |
| 291 | return NULL; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | |
| 296 | /** |
| 297 | * Delete a program and remove it from the hash table, ignoring the |
| 298 | * reference count. |
| 299 | * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation) |
| 300 | * by a device driver function. |
| 301 | */ |
| 302 | void |
Brian Paul | 122629f | 2006-07-20 16:49:57 +0000 | [diff] [blame] | 303 | _mesa_delete_program(GLcontext *ctx, struct gl_program *prog) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 304 | { |
Brian Paul | a6c423d | 2004-08-25 15:59:48 +0000 | [diff] [blame] | 305 | (void) ctx; |
Brian Paul | 5b5c931 | 2008-05-07 18:51:44 -0600 | [diff] [blame] | 306 | ASSERT(prog); assert(prog->RefCount==0); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 307 | |
Brian Paul | 308b85f | 2006-11-23 15:58:30 +0000 | [diff] [blame] | 308 | if (prog == &_mesa_DummyProgram) |
| 309 | return; |
| 310 | |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 311 | if (prog->String) |
| 312 | _mesa_free(prog->String); |
Brian Paul | de99760 | 2005-11-12 17:53:14 +0000 | [diff] [blame] | 313 | |
| 314 | if (prog->Instructions) { |
| 315 | GLuint i; |
| 316 | for (i = 0; i < prog->NumInstructions; i++) { |
| 317 | if (prog->Instructions[i].Data) |
| 318 | _mesa_free(prog->Instructions[i].Data); |
Brian | 4cc2674 | 2007-04-20 08:12:17 -0600 | [diff] [blame] | 319 | if (prog->Instructions[i].Comment) |
| 320 | _mesa_free((char *) prog->Instructions[i].Comment); |
Brian Paul | 575700f | 2004-12-16 03:07:18 +0000 | [diff] [blame] | 321 | } |
Brian Paul | de99760 | 2005-11-12 17:53:14 +0000 | [diff] [blame] | 322 | _mesa_free(prog->Instructions); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 323 | } |
Brian Paul | de99760 | 2005-11-12 17:53:14 +0000 | [diff] [blame] | 324 | |
Brian Paul | 65a51c0 | 2006-05-24 03:30:31 +0000 | [diff] [blame] | 325 | if (prog->Parameters) { |
Brian Paul | de99760 | 2005-11-12 17:53:14 +0000 | [diff] [blame] | 326 | _mesa_free_parameter_list(prog->Parameters); |
Brian Paul | 65a51c0 | 2006-05-24 03:30:31 +0000 | [diff] [blame] | 327 | } |
Brian | fe1d01c | 2006-12-13 14:54:47 -0700 | [diff] [blame] | 328 | if (prog->Varying) { |
| 329 | _mesa_free_parameter_list(prog->Varying); |
| 330 | } |
Brian | 3493e86 | 2007-03-24 16:18:13 -0600 | [diff] [blame] | 331 | if (prog->Attributes) { |
| 332 | _mesa_free_parameter_list(prog->Attributes); |
| 333 | } |
Brian | fe1d01c | 2006-12-13 14:54:47 -0700 | [diff] [blame] | 334 | |
Brian Paul | 58d080b | 2006-08-25 19:46:31 +0000 | [diff] [blame] | 335 | /* XXX this is a little ugly */ |
| 336 | if (prog->Target == GL_VERTEX_PROGRAM_ARB) { |
| 337 | struct gl_vertex_program *vprog = (struct gl_vertex_program *) prog; |
| 338 | if (vprog->TnlData) |
| 339 | _mesa_free(vprog->TnlData); |
| 340 | } |
| 341 | |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 342 | _mesa_free(prog); |
| 343 | } |
| 344 | |
| 345 | |
Brian Paul | 4d12a05 | 2006-08-23 23:10:14 +0000 | [diff] [blame] | 346 | /** |
| 347 | * Return the gl_program object for a given ID. |
| 348 | * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of |
| 349 | * casts elsewhere. |
| 350 | */ |
| 351 | struct gl_program * |
| 352 | _mesa_lookup_program(GLcontext *ctx, GLuint id) |
| 353 | { |
| 354 | if (id) |
| 355 | return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id); |
| 356 | else |
| 357 | return NULL; |
| 358 | } |
| 359 | |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 360 | |
Brian Paul | 3b9b8de | 2006-08-24 21:57:36 +0000 | [diff] [blame] | 361 | /** |
Brian | df43fb6 | 2008-05-06 23:08:51 -0600 | [diff] [blame] | 362 | * Reference counting for vertex/fragment programs |
| 363 | */ |
| 364 | void |
| 365 | _mesa_reference_program(GLcontext *ctx, |
| 366 | struct gl_program **ptr, |
| 367 | struct gl_program *prog) |
| 368 | { |
| 369 | assert(ptr); |
| 370 | if (*ptr && prog) { |
| 371 | /* sanity check */ |
| 372 | ASSERT((*ptr)->Target == prog->Target); |
| 373 | } |
| 374 | if (*ptr == prog) { |
| 375 | return; /* no change */ |
| 376 | } |
| 377 | if (*ptr) { |
| 378 | GLboolean deleteFlag; |
| 379 | |
| 380 | /*_glthread_LOCK_MUTEX((*ptr)->Mutex);*/ |
Brian Paul | 5b5c931 | 2008-05-07 18:51:44 -0600 | [diff] [blame] | 381 | #if 01 |
Brian | df43fb6 | 2008-05-06 23:08:51 -0600 | [diff] [blame] | 382 | printf("Program %p %u 0x%x Refcount-- to %d\n", |
| 383 | *ptr, (*ptr)->Id, (*ptr)->Target, (*ptr)->RefCount - 1); |
| 384 | #endif |
| 385 | ASSERT((*ptr)->RefCount > 0); |
| 386 | (*ptr)->RefCount--; |
| 387 | |
| 388 | deleteFlag = ((*ptr)->RefCount == 0); |
| 389 | /*_glthread_UNLOCK_MUTEX((*ptr)->Mutex);*/ |
| 390 | |
| 391 | if (deleteFlag) { |
| 392 | ASSERT(ctx); |
| 393 | ctx->Driver.DeleteProgram(ctx, *ptr); |
| 394 | } |
| 395 | |
| 396 | *ptr = NULL; |
| 397 | } |
| 398 | |
| 399 | assert(!*ptr); |
| 400 | if (prog) { |
| 401 | /*_glthread_LOCK_MUTEX(prog->Mutex);*/ |
| 402 | prog->RefCount++; |
Brian Paul | 5b5c931 | 2008-05-07 18:51:44 -0600 | [diff] [blame] | 403 | #if 01 |
Brian | df43fb6 | 2008-05-06 23:08:51 -0600 | [diff] [blame] | 404 | printf("Program %p %u 0x%x Refcount++ to %d\n", |
| 405 | prog, prog->Id, prog->Target, prog->RefCount); |
| 406 | #endif |
| 407 | /*_glthread_UNLOCK_MUTEX(prog->Mutex);*/ |
| 408 | } |
| 409 | |
| 410 | *ptr = prog; |
| 411 | } |
| 412 | |
| 413 | |
| 414 | /** |
Brian | b2a3a85 | 2006-12-14 13:56:58 -0700 | [diff] [blame] | 415 | * Return a copy of a program. |
| 416 | * XXX Problem here if the program object is actually OO-derivation |
| 417 | * made by a device driver. |
| 418 | */ |
| 419 | struct gl_program * |
| 420 | _mesa_clone_program(GLcontext *ctx, const struct gl_program *prog) |
| 421 | { |
| 422 | struct gl_program *clone; |
| 423 | |
Brian | 5b6858c | 2007-07-24 09:56:44 -0600 | [diff] [blame] | 424 | clone = ctx->Driver.NewProgram(ctx, prog->Target, prog->Id); |
Brian | b2a3a85 | 2006-12-14 13:56:58 -0700 | [diff] [blame] | 425 | if (!clone) |
| 426 | return NULL; |
| 427 | |
| 428 | assert(clone->Target == prog->Target); |
Brian | df43fb6 | 2008-05-06 23:08:51 -0600 | [diff] [blame] | 429 | assert(clone->RefCount == 1); |
| 430 | |
Brian | b2a3a85 | 2006-12-14 13:56:58 -0700 | [diff] [blame] | 431 | clone->String = (GLubyte *) _mesa_strdup((char *) prog->String); |
Brian | b2a3a85 | 2006-12-14 13:56:58 -0700 | [diff] [blame] | 432 | clone->Format = prog->Format; |
| 433 | clone->Instructions = _mesa_alloc_instructions(prog->NumInstructions); |
| 434 | if (!clone->Instructions) { |
Brian | dc6fab9 | 2008-03-22 10:27:03 -0600 | [diff] [blame] | 435 | ctx->Driver.DeleteProgram(ctx, clone); |
Brian | b2a3a85 | 2006-12-14 13:56:58 -0700 | [diff] [blame] | 436 | return NULL; |
| 437 | } |
Brian | 12229f1 | 2007-03-22 09:11:26 -0600 | [diff] [blame] | 438 | _mesa_copy_instructions(clone->Instructions, prog->Instructions, |
| 439 | prog->NumInstructions); |
Brian | b2a3a85 | 2006-12-14 13:56:58 -0700 | [diff] [blame] | 440 | clone->InputsRead = prog->InputsRead; |
| 441 | clone->OutputsWritten = prog->OutputsWritten; |
Brian | c9db223 | 2007-01-04 17:22:19 -0700 | [diff] [blame] | 442 | memcpy(clone->TexturesUsed, prog->TexturesUsed, sizeof(prog->TexturesUsed)); |
| 443 | |
Brian | 2e76f0a | 2006-12-19 09:52:07 -0700 | [diff] [blame] | 444 | if (prog->Parameters) |
| 445 | clone->Parameters = _mesa_clone_parameter_list(prog->Parameters); |
Brian | b2a3a85 | 2006-12-14 13:56:58 -0700 | [diff] [blame] | 446 | memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams)); |
Brian | 2e76f0a | 2006-12-19 09:52:07 -0700 | [diff] [blame] | 447 | if (prog->Varying) |
| 448 | clone->Varying = _mesa_clone_parameter_list(prog->Varying); |
Brian | 3209c3e | 2007-01-09 17:49:24 -0700 | [diff] [blame] | 449 | if (prog->Attributes) |
| 450 | clone->Attributes = _mesa_clone_parameter_list(prog->Attributes); |
Brian | b2a3a85 | 2006-12-14 13:56:58 -0700 | [diff] [blame] | 451 | memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams)); |
| 452 | clone->NumInstructions = prog->NumInstructions; |
| 453 | clone->NumTemporaries = prog->NumTemporaries; |
| 454 | clone->NumParameters = prog->NumParameters; |
| 455 | clone->NumAttributes = prog->NumAttributes; |
| 456 | clone->NumAddressRegs = prog->NumAddressRegs; |
| 457 | clone->NumNativeInstructions = prog->NumNativeInstructions; |
| 458 | clone->NumNativeTemporaries = prog->NumNativeTemporaries; |
| 459 | clone->NumNativeParameters = prog->NumNativeParameters; |
| 460 | clone->NumNativeAttributes = prog->NumNativeAttributes; |
| 461 | clone->NumNativeAddressRegs = prog->NumNativeAddressRegs; |
Brian | 21f9979 | 2007-01-09 11:00:21 -0700 | [diff] [blame] | 462 | clone->NumAluInstructions = prog->NumAluInstructions; |
| 463 | clone->NumTexInstructions = prog->NumTexInstructions; |
| 464 | clone->NumTexIndirections = prog->NumTexIndirections; |
| 465 | clone->NumNativeAluInstructions = prog->NumNativeAluInstructions; |
| 466 | clone->NumNativeTexInstructions = prog->NumNativeTexInstructions; |
| 467 | clone->NumNativeTexIndirections = prog->NumNativeTexIndirections; |
Brian | b2a3a85 | 2006-12-14 13:56:58 -0700 | [diff] [blame] | 468 | |
| 469 | switch (prog->Target) { |
| 470 | case GL_VERTEX_PROGRAM_ARB: |
| 471 | { |
| 472 | const struct gl_vertex_program *vp |
| 473 | = (const struct gl_vertex_program *) prog; |
| 474 | struct gl_vertex_program *vpc = (struct gl_vertex_program *) clone; |
| 475 | vpc->IsPositionInvariant = vp->IsPositionInvariant; |
| 476 | } |
| 477 | break; |
| 478 | case GL_FRAGMENT_PROGRAM_ARB: |
| 479 | { |
| 480 | const struct gl_fragment_program *fp |
| 481 | = (const struct gl_fragment_program *) prog; |
| 482 | struct gl_fragment_program *fpc = (struct gl_fragment_program *) clone; |
Brian | b2a3a85 | 2006-12-14 13:56:58 -0700 | [diff] [blame] | 483 | fpc->FogOption = fp->FogOption; |
| 484 | fpc->UsesKill = fp->UsesKill; |
| 485 | } |
| 486 | break; |
| 487 | default: |
| 488 | _mesa_problem(NULL, "Unexpected target in _mesa_clone_program"); |
| 489 | } |
| 490 | |
| 491 | return clone; |
| 492 | } |
| 493 | |
| 494 | |
| 495 | |
| 496 | /** |
Brian Paul | 77427a1 | 2006-08-24 23:11:39 +0000 | [diff] [blame] | 497 | * Mixing ARB and NV vertex/fragment programs can be tricky. |
| 498 | * Note: GL_VERTEX_PROGRAM_ARB == GL_VERTEX_PROGRAM_NV |
| 499 | * but, GL_FRAGMENT_PROGRAM_ARB != GL_FRAGMENT_PROGRAM_NV |
| 500 | * The two different fragment program targets are supposed to be compatible |
| 501 | * to some extent (see GL_ARB_fragment_program spec). |
| 502 | * This function does the compatibility check. |
| 503 | */ |
| 504 | static GLboolean |
| 505 | compatible_program_targets(GLenum t1, GLenum t2) |
| 506 | { |
| 507 | if (t1 == t2) |
| 508 | return GL_TRUE; |
| 509 | if (t1 == GL_FRAGMENT_PROGRAM_ARB && t2 == GL_FRAGMENT_PROGRAM_NV) |
| 510 | return GL_TRUE; |
| 511 | if (t1 == GL_FRAGMENT_PROGRAM_NV && t2 == GL_FRAGMENT_PROGRAM_ARB) |
| 512 | return GL_TRUE; |
| 513 | return GL_FALSE; |
| 514 | } |
| 515 | |
| 516 | |
Brian Paul | 30d6a4b | 2005-11-05 20:18:18 +0000 | [diff] [blame] | 517 | |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 518 | /**********************************************************************/ |
| 519 | /* API functions */ |
| 520 | /**********************************************************************/ |
| 521 | |
| 522 | |
| 523 | /** |
| 524 | * Bind a program (make it current) |
| 525 | * \note Called from the GL API dispatcher by both glBindProgramNV |
| 526 | * and glBindProgramARB. |
| 527 | */ |
| 528 | void GLAPIENTRY |
| 529 | _mesa_BindProgram(GLenum target, GLuint id) |
| 530 | { |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 531 | struct gl_program *curProg, *newProg; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 532 | GET_CURRENT_CONTEXT(ctx); |
| 533 | ASSERT_OUTSIDE_BEGIN_END(ctx); |
| 534 | |
| 535 | FLUSH_VERTICES(ctx, _NEW_PROGRAM); |
| 536 | |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 537 | /* Error-check target and get curProg */ |
Roland Scheidegger | e1e03b3 | 2006-03-03 15:03:04 +0000 | [diff] [blame] | 538 | if ((target == GL_VERTEX_PROGRAM_ARB) && /* == GL_VERTEX_PROGRAM_NV */ |
| 539 | (ctx->Extensions.NV_vertex_program || |
| 540 | ctx->Extensions.ARB_vertex_program)) { |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 541 | curProg = &ctx->VertexProgram.Current->Base; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 542 | } |
| 543 | else if ((target == GL_FRAGMENT_PROGRAM_NV |
| 544 | && ctx->Extensions.NV_fragment_program) || |
| 545 | (target == GL_FRAGMENT_PROGRAM_ARB |
| 546 | && ctx->Extensions.ARB_fragment_program)) { |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 547 | curProg = &ctx->FragmentProgram.Current->Base; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 548 | } |
| 549 | else { |
| 550 | _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)"); |
| 551 | return; |
| 552 | } |
| 553 | |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 554 | /* |
| 555 | * Get pointer to new program to bind. |
| 556 | * NOTE: binding to a non-existant program is not an error. |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 557 | * That's supposed to be caught in glBegin. |
| 558 | */ |
| 559 | if (id == 0) { |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 560 | /* Bind a default program */ |
| 561 | newProg = NULL; |
Roland Scheidegger | e1e03b3 | 2006-03-03 15:03:04 +0000 | [diff] [blame] | 562 | if (target == GL_VERTEX_PROGRAM_ARB) /* == GL_VERTEX_PROGRAM_NV */ |
Brian | df43fb6 | 2008-05-06 23:08:51 -0600 | [diff] [blame] | 563 | newProg = &ctx->Shared->DefaultVertexProgram->Base; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 564 | else |
Brian | df43fb6 | 2008-05-06 23:08:51 -0600 | [diff] [blame] | 565 | newProg = &ctx->Shared->DefaultFragmentProgram->Base; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 566 | } |
| 567 | else { |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 568 | /* Bind a user program */ |
| 569 | newProg = _mesa_lookup_program(ctx, id); |
| 570 | if (!newProg || newProg == &_mesa_DummyProgram) { |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 571 | /* allocate a new program now */ |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 572 | newProg = ctx->Driver.NewProgram(ctx, target, id); |
| 573 | if (!newProg) { |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 574 | _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB"); |
| 575 | return; |
| 576 | } |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 577 | _mesa_HashInsert(ctx->Shared->Programs, id, newProg); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 578 | } |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 579 | else if (!compatible_program_targets(newProg->Target, target)) { |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 580 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 581 | "glBindProgramNV/ARB(target mismatch)"); |
| 582 | return; |
| 583 | } |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 586 | /** All error checking is complete now **/ |
| 587 | |
| 588 | if (curProg->Id == id) { |
| 589 | /* binding same program - no change */ |
| 590 | return; |
| 591 | } |
| 592 | |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 593 | /* bind newProg */ |
Roland Scheidegger | e1e03b3 | 2006-03-03 15:03:04 +0000 | [diff] [blame] | 594 | if (target == GL_VERTEX_PROGRAM_ARB) { /* == GL_VERTEX_PROGRAM_NV */ |
Brian | df43fb6 | 2008-05-06 23:08:51 -0600 | [diff] [blame] | 595 | _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current, |
| 596 | (struct gl_vertex_program *) newProg); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 597 | } |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 598 | else if (target == GL_FRAGMENT_PROGRAM_NV || |
| 599 | target == GL_FRAGMENT_PROGRAM_ARB) { |
Brian | df43fb6 | 2008-05-06 23:08:51 -0600 | [diff] [blame] | 600 | _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current, |
| 601 | (struct gl_fragment_program *) newProg); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 604 | /* Never null pointers */ |
| 605 | ASSERT(ctx->VertexProgram.Current); |
| 606 | ASSERT(ctx->FragmentProgram.Current); |
| 607 | |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 608 | if (ctx->Driver.BindProgram) |
Brian Paul | a360bc3 | 2006-08-25 17:18:56 +0000 | [diff] [blame] | 609 | ctx->Driver.BindProgram(ctx, target, newProg); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | |
| 613 | /** |
| 614 | * Delete a list of programs. |
| 615 | * \note Not compiled into display lists. |
| 616 | * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB. |
| 617 | */ |
| 618 | void GLAPIENTRY |
| 619 | _mesa_DeletePrograms(GLsizei n, const GLuint *ids) |
| 620 | { |
| 621 | GLint i; |
| 622 | GET_CURRENT_CONTEXT(ctx); |
| 623 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); |
| 624 | |
| 625 | if (n < 0) { |
| 626 | _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" ); |
| 627 | return; |
| 628 | } |
| 629 | |
| 630 | for (i = 0; i < n; i++) { |
| 631 | if (ids[i] != 0) { |
Brian Paul | 4d12a05 | 2006-08-23 23:10:14 +0000 | [diff] [blame] | 632 | struct gl_program *prog = _mesa_lookup_program(ctx, ids[i]); |
Brian Paul | 9ca8392 | 2004-10-02 15:16:59 +0000 | [diff] [blame] | 633 | if (prog == &_mesa_DummyProgram) { |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 634 | _mesa_HashRemove(ctx->Shared->Programs, ids[i]); |
| 635 | } |
| 636 | else if (prog) { |
| 637 | /* Unbind program if necessary */ |
Roland Scheidegger | e1e03b3 | 2006-03-03 15:03:04 +0000 | [diff] [blame] | 638 | if (prog->Target == GL_VERTEX_PROGRAM_ARB || /* == GL_VERTEX_PROGRAM_NV */ |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 639 | prog->Target == GL_VERTEX_STATE_PROGRAM_NV) { |
| 640 | if (ctx->VertexProgram.Current && |
| 641 | ctx->VertexProgram.Current->Base.Id == ids[i]) { |
| 642 | /* unbind this currently bound program */ |
| 643 | _mesa_BindProgram(prog->Target, 0); |
| 644 | } |
| 645 | } |
| 646 | else if (prog->Target == GL_FRAGMENT_PROGRAM_NV || |
| 647 | prog->Target == GL_FRAGMENT_PROGRAM_ARB) { |
| 648 | if (ctx->FragmentProgram.Current && |
| 649 | ctx->FragmentProgram.Current->Base.Id == ids[i]) { |
| 650 | /* unbind this currently bound program */ |
| 651 | _mesa_BindProgram(prog->Target, 0); |
| 652 | } |
| 653 | } |
| 654 | else { |
| 655 | _mesa_problem(ctx, "bad target in glDeleteProgramsNV"); |
| 656 | return; |
| 657 | } |
Brian Paul | ea2943e | 2005-01-20 04:02:02 +0000 | [diff] [blame] | 658 | /* The ID is immediately available for re-use now */ |
| 659 | _mesa_HashRemove(ctx->Shared->Programs, ids[i]); |
Brian | df43fb6 | 2008-05-06 23:08:51 -0600 | [diff] [blame] | 660 | _mesa_reference_program(ctx, &prog, NULL); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 661 | } |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | |
| 667 | /** |
| 668 | * Generate a list of new program identifiers. |
| 669 | * \note Not compiled into display lists. |
| 670 | * \note Called by both glGenProgramsNV and glGenProgramsARB. |
| 671 | */ |
| 672 | void GLAPIENTRY |
| 673 | _mesa_GenPrograms(GLsizei n, GLuint *ids) |
| 674 | { |
| 675 | GLuint first; |
| 676 | GLuint i; |
| 677 | GET_CURRENT_CONTEXT(ctx); |
| 678 | ASSERT_OUTSIDE_BEGIN_END(ctx); |
| 679 | |
| 680 | if (n < 0) { |
| 681 | _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms"); |
| 682 | return; |
| 683 | } |
| 684 | |
| 685 | if (!ids) |
| 686 | return; |
| 687 | |
| 688 | first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n); |
| 689 | |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 690 | /* Insert pointer to dummy program as placeholder */ |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 691 | for (i = 0; i < (GLuint) n; i++) { |
Brian Paul | 9ca8392 | 2004-10-02 15:16:59 +0000 | [diff] [blame] | 692 | _mesa_HashInsert(ctx->Shared->Programs, first + i, &_mesa_DummyProgram); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | /* Return the program names */ |
| 696 | for (i = 0; i < (GLuint) n; i++) { |
| 697 | ids[i] = first + i; |
| 698 | } |
| 699 | } |