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