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