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