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