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 | |
José Fonseca | 101d1a6 | 2008-07-23 21:06:01 +0900 | [diff] [blame] | 32 | #include "main/glheader.h" |
| 33 | #include "main/context.h" |
| 34 | #include "main/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; |
Brian Paul | af3d9db | 2008-08-12 10:00:36 -0600 | [diff] [blame^] | 61 | #if FEATURE_es2_glsl |
| 62 | ctx->VertexProgram.PointSizeEnabled = GL_TRUE; |
| 63 | #else |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 64 | ctx->VertexProgram.PointSizeEnabled = GL_FALSE; |
Brian Paul | af3d9db | 2008-08-12 10:00:36 -0600 | [diff] [blame^] | 65 | #endif |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 66 | ctx->VertexProgram.TwoSideEnabled = GL_FALSE; |
Brian Paul | a56a59c | 2008-05-07 08:55:33 -0600 | [diff] [blame] | 67 | _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current, |
| 68 | ctx->Shared->DefaultVertexProgram); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 69 | assert(ctx->VertexProgram.Current); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 70 | for (i = 0; i < MAX_NV_VERTEX_PROGRAM_PARAMS / 4; i++) { |
| 71 | ctx->VertexProgram.TrackMatrix[i] = GL_NONE; |
| 72 | ctx->VertexProgram.TrackMatrixTransform[i] = GL_IDENTITY_NV; |
| 73 | } |
Brian | b26aae6 | 2007-10-31 12:00:38 -0600 | [diff] [blame] | 74 | ctx->VertexProgram.Cache = _mesa_new_program_cache(); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 75 | #endif |
| 76 | |
| 77 | #if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program |
| 78 | ctx->FragmentProgram.Enabled = GL_FALSE; |
Brian Paul | a56a59c | 2008-05-07 08:55:33 -0600 | [diff] [blame] | 79 | _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current, |
| 80 | ctx->Shared->DefaultFragmentProgram); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 81 | assert(ctx->FragmentProgram.Current); |
Brian | b26aae6 | 2007-10-31 12:00:38 -0600 | [diff] [blame] | 82 | ctx->FragmentProgram.Cache = _mesa_new_program_cache(); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 83 | #endif |
Dave Airlie | 7f752fe | 2004-12-19 03:06:59 +0000 | [diff] [blame] | 84 | |
Brian | b26aae6 | 2007-10-31 12:00:38 -0600 | [diff] [blame] | 85 | |
Brian Paul | 63d6830 | 2005-11-19 16:43:04 +0000 | [diff] [blame] | 86 | /* XXX probably move this stuff */ |
Dave Airlie | 7f752fe | 2004-12-19 03:06:59 +0000 | [diff] [blame] | 87 | #if FEATURE_ATI_fragment_shader |
| 88 | ctx->ATIFragmentShader.Enabled = GL_FALSE; |
Brian | e3ca92a | 2008-07-15 19:54:25 -0600 | [diff] [blame] | 89 | ctx->ATIFragmentShader.Current = ctx->Shared->DefaultFragmentShader; |
Dave Airlie | 7f752fe | 2004-12-19 03:06:59 +0000 | [diff] [blame] | 90 | assert(ctx->ATIFragmentShader.Current); |
Brian Paul | 63d6830 | 2005-11-19 16:43:04 +0000 | [diff] [blame] | 91 | ctx->ATIFragmentShader.Current->RefCount++; |
Dave Airlie | 7f752fe | 2004-12-19 03:06:59 +0000 | [diff] [blame] | 92 | #endif |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | |
| 96 | /** |
Brian Paul | 21841f0 | 2004-08-14 14:28:11 +0000 | [diff] [blame] | 97 | * Free a context's vertex/fragment program state |
| 98 | */ |
| 99 | void |
| 100 | _mesa_free_program_data(GLcontext *ctx) |
| 101 | { |
Roland Scheidegger | 93da673 | 2006-03-01 23:11:14 +0000 | [diff] [blame] | 102 | #if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program |
Brian Paul | a56a59c | 2008-05-07 08:55:33 -0600 | [diff] [blame] | 103 | _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current, NULL); |
Brian | 1631a95 | 2007-12-26 06:57:24 -0700 | [diff] [blame] | 104 | _mesa_delete_program_cache(ctx, ctx->VertexProgram.Cache); |
Brian Paul | 21841f0 | 2004-08-14 14:28:11 +0000 | [diff] [blame] | 105 | #endif |
Roland Scheidegger | 93da673 | 2006-03-01 23:11:14 +0000 | [diff] [blame] | 106 | #if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program |
Brian Paul | a56a59c | 2008-05-07 08:55:33 -0600 | [diff] [blame] | 107 | _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current, NULL); |
Brian | 1631a95 | 2007-12-26 06:57:24 -0700 | [diff] [blame] | 108 | _mesa_delete_program_cache(ctx, ctx->FragmentProgram.Cache); |
Brian Paul | 21841f0 | 2004-08-14 14:28:11 +0000 | [diff] [blame] | 109 | #endif |
Brian Paul | 63d6830 | 2005-11-19 16:43:04 +0000 | [diff] [blame] | 110 | /* XXX probably move this stuff */ |
Dave Airlie | 7f752fe | 2004-12-19 03:06:59 +0000 | [diff] [blame] | 111 | #if FEATURE_ATI_fragment_shader |
| 112 | if (ctx->ATIFragmentShader.Current) { |
Brian Paul | 63d6830 | 2005-11-19 16:43:04 +0000 | [diff] [blame] | 113 | ctx->ATIFragmentShader.Current->RefCount--; |
| 114 | if (ctx->ATIFragmentShader.Current->RefCount <= 0) { |
| 115 | _mesa_free(ctx->ATIFragmentShader.Current); |
| 116 | } |
Dave Airlie | 7f752fe | 2004-12-19 03:06:59 +0000 | [diff] [blame] | 117 | } |
| 118 | #endif |
Brian Paul | 21841f0 | 2004-08-14 14:28:11 +0000 | [diff] [blame] | 119 | _mesa_free((void *) ctx->Program.ErrorString); |
| 120 | } |
| 121 | |
| 122 | |
Brian Paul | 21841f0 | 2004-08-14 14:28:11 +0000 | [diff] [blame] | 123 | /** |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 124 | * Set the vertex/fragment program error state (position and error string). |
| 125 | * This is generally called from within the parsers. |
| 126 | */ |
| 127 | void |
| 128 | _mesa_set_program_error(GLcontext *ctx, GLint pos, const char *string) |
| 129 | { |
| 130 | ctx->Program.ErrorPos = pos; |
| 131 | _mesa_free((void *) ctx->Program.ErrorString); |
| 132 | if (!string) |
| 133 | string = ""; |
| 134 | ctx->Program.ErrorString = _mesa_strdup(string); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | /** |
| 139 | * Find the line number and column for 'pos' within 'string'. |
| 140 | * Return a copy of the line which contains 'pos'. Free the line with |
| 141 | * _mesa_free(). |
| 142 | * \param string the program string |
| 143 | * \param pos the position within the string |
| 144 | * \param line returns the line number corresponding to 'pos'. |
| 145 | * \param col returns the column number corresponding to 'pos'. |
| 146 | * \return copy of the line containing 'pos'. |
| 147 | */ |
| 148 | const GLubyte * |
| 149 | _mesa_find_line_column(const GLubyte *string, const GLubyte *pos, |
| 150 | GLint *line, GLint *col) |
| 151 | { |
| 152 | const GLubyte *lineStart = string; |
| 153 | const GLubyte *p = string; |
| 154 | GLubyte *s; |
| 155 | int len; |
| 156 | |
| 157 | *line = 1; |
| 158 | |
| 159 | while (p != pos) { |
| 160 | if (*p == (GLubyte) '\n') { |
| 161 | (*line)++; |
| 162 | lineStart = p + 1; |
| 163 | } |
| 164 | p++; |
| 165 | } |
| 166 | |
| 167 | *col = (pos - lineStart) + 1; |
| 168 | |
| 169 | /* return copy of this line */ |
| 170 | while (*p != 0 && *p != '\n') |
| 171 | p++; |
| 172 | len = p - lineStart; |
| 173 | s = (GLubyte *) _mesa_malloc(len + 1); |
| 174 | _mesa_memcpy(s, lineStart, len); |
| 175 | s[len] = 0; |
| 176 | |
| 177 | return s; |
| 178 | } |
| 179 | |
| 180 | |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 181 | /** |
| 182 | * Initialize a new vertex/fragment program object. |
| 183 | */ |
Brian Paul | 122629f | 2006-07-20 16:49:57 +0000 | [diff] [blame] | 184 | static struct gl_program * |
| 185 | _mesa_init_program_struct( GLcontext *ctx, struct gl_program *prog, |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 186 | GLenum target, GLuint id) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 187 | { |
Brian Paul | a6c423d | 2004-08-25 15:59:48 +0000 | [diff] [blame] | 188 | (void) ctx; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 189 | if (prog) { |
Brian | 8fed246 | 2007-10-26 19:19:09 -0600 | [diff] [blame] | 190 | GLuint i; |
Brian | fe1d01c | 2006-12-13 14:54:47 -0700 | [diff] [blame] | 191 | _mesa_bzero(prog, sizeof(*prog)); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 192 | prog->Id = id; |
| 193 | prog->Target = target; |
| 194 | prog->Resident = GL_TRUE; |
| 195 | prog->RefCount = 1; |
Brian Paul | 308b85f | 2006-11-23 15:58:30 +0000 | [diff] [blame] | 196 | prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB; |
Brian | 8fed246 | 2007-10-26 19:19:09 -0600 | [diff] [blame] | 197 | |
| 198 | /* default mapping from samplers to texture units */ |
| 199 | for (i = 0; i < MAX_SAMPLERS; i++) |
| 200 | prog->SamplerUnits[i] = i; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | return prog; |
| 204 | } |
| 205 | |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 206 | |
| 207 | /** |
| 208 | * Initialize a new fragment program object. |
| 209 | */ |
Brian Paul | 122629f | 2006-07-20 16:49:57 +0000 | [diff] [blame] | 210 | struct gl_program * |
| 211 | _mesa_init_fragment_program( GLcontext *ctx, struct gl_fragment_program *prog, |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 212 | GLenum target, GLuint id) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 213 | { |
| 214 | if (prog) |
| 215 | return _mesa_init_program_struct( ctx, &prog->Base, target, id ); |
| 216 | else |
| 217 | return NULL; |
| 218 | } |
| 219 | |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 220 | |
| 221 | /** |
| 222 | * Initialize a new vertex program object. |
| 223 | */ |
Brian Paul | 122629f | 2006-07-20 16:49:57 +0000 | [diff] [blame] | 224 | struct gl_program * |
| 225 | _mesa_init_vertex_program( GLcontext *ctx, struct gl_vertex_program *prog, |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 226 | GLenum target, GLuint id) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 227 | { |
| 228 | if (prog) |
| 229 | return _mesa_init_program_struct( ctx, &prog->Base, target, id ); |
| 230 | else |
| 231 | return NULL; |
| 232 | } |
| 233 | |
| 234 | |
| 235 | /** |
| 236 | * Allocate and initialize a new fragment/vertex program object but |
| 237 | * don't put it into the program hash table. Called via |
| 238 | * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a |
| 239 | * device driver function to implement OO deriviation with additional |
| 240 | * types not understood by this function. |
| 241 | * |
| 242 | * \param ctx context |
| 243 | * \param id program id/number |
| 244 | * \param target program target/type |
| 245 | * \return pointer to new program object |
| 246 | */ |
Brian Paul | 122629f | 2006-07-20 16:49:57 +0000 | [diff] [blame] | 247 | struct gl_program * |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 248 | _mesa_new_program(GLcontext *ctx, GLenum target, GLuint id) |
| 249 | { |
| 250 | switch (target) { |
| 251 | case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */ |
Brian Paul | 122629f | 2006-07-20 16:49:57 +0000 | [diff] [blame] | 252 | return _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program), |
| 253 | target, id ); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 254 | case GL_FRAGMENT_PROGRAM_NV: |
| 255 | case GL_FRAGMENT_PROGRAM_ARB: |
Brian Paul | 122629f | 2006-07-20 16:49:57 +0000 | [diff] [blame] | 256 | return _mesa_init_fragment_program(ctx, |
| 257 | CALLOC_STRUCT(gl_fragment_program), |
| 258 | target, id ); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 259 | default: |
| 260 | _mesa_problem(ctx, "bad target in _mesa_new_program"); |
| 261 | return NULL; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | |
| 266 | /** |
| 267 | * Delete a program and remove it from the hash table, ignoring the |
| 268 | * reference count. |
| 269 | * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation) |
| 270 | * by a device driver function. |
| 271 | */ |
| 272 | void |
Brian Paul | 122629f | 2006-07-20 16:49:57 +0000 | [diff] [blame] | 273 | _mesa_delete_program(GLcontext *ctx, struct gl_program *prog) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 274 | { |
Brian Paul | a6c423d | 2004-08-25 15:59:48 +0000 | [diff] [blame] | 275 | (void) ctx; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 276 | ASSERT(prog); |
Brian Paul | b256273 | 2008-05-14 12:19:22 -0600 | [diff] [blame] | 277 | ASSERT(prog->RefCount==0); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 278 | |
Brian Paul | 308b85f | 2006-11-23 15:58:30 +0000 | [diff] [blame] | 279 | if (prog == &_mesa_DummyProgram) |
| 280 | return; |
| 281 | |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 282 | if (prog->String) |
| 283 | _mesa_free(prog->String); |
Brian Paul | de99760 | 2005-11-12 17:53:14 +0000 | [diff] [blame] | 284 | |
Brian | 5d1e730 | 2008-04-07 11:16:18 -0600 | [diff] [blame] | 285 | _mesa_free_instructions(prog->Instructions, prog->NumInstructions); |
Brian Paul | de99760 | 2005-11-12 17:53:14 +0000 | [diff] [blame] | 286 | |
Brian Paul | 65a51c0 | 2006-05-24 03:30:31 +0000 | [diff] [blame] | 287 | if (prog->Parameters) { |
Brian Paul | de99760 | 2005-11-12 17:53:14 +0000 | [diff] [blame] | 288 | _mesa_free_parameter_list(prog->Parameters); |
Brian Paul | 65a51c0 | 2006-05-24 03:30:31 +0000 | [diff] [blame] | 289 | } |
Brian | fe1d01c | 2006-12-13 14:54:47 -0700 | [diff] [blame] | 290 | if (prog->Varying) { |
| 291 | _mesa_free_parameter_list(prog->Varying); |
| 292 | } |
Brian | 3493e86 | 2007-03-24 16:18:13 -0600 | [diff] [blame] | 293 | if (prog->Attributes) { |
| 294 | _mesa_free_parameter_list(prog->Attributes); |
| 295 | } |
Brian | fe1d01c | 2006-12-13 14:54:47 -0700 | [diff] [blame] | 296 | |
Brian Paul | 58d080b | 2006-08-25 19:46:31 +0000 | [diff] [blame] | 297 | /* XXX this is a little ugly */ |
| 298 | if (prog->Target == GL_VERTEX_PROGRAM_ARB) { |
| 299 | struct gl_vertex_program *vprog = (struct gl_vertex_program *) prog; |
| 300 | if (vprog->TnlData) |
| 301 | _mesa_free(vprog->TnlData); |
| 302 | } |
| 303 | |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 304 | _mesa_free(prog); |
| 305 | } |
| 306 | |
| 307 | |
Brian Paul | 4d12a05 | 2006-08-23 23:10:14 +0000 | [diff] [blame] | 308 | /** |
| 309 | * Return the gl_program object for a given ID. |
| 310 | * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of |
| 311 | * casts elsewhere. |
| 312 | */ |
| 313 | struct gl_program * |
| 314 | _mesa_lookup_program(GLcontext *ctx, GLuint id) |
| 315 | { |
| 316 | if (id) |
| 317 | return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id); |
| 318 | else |
| 319 | return NULL; |
| 320 | } |
| 321 | |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 322 | |
Brian Paul | 3b9b8de | 2006-08-24 21:57:36 +0000 | [diff] [blame] | 323 | /** |
Brian Paul | a56a59c | 2008-05-07 08:55:33 -0600 | [diff] [blame] | 324 | * Reference counting for vertex/fragment programs |
| 325 | */ |
| 326 | void |
| 327 | _mesa_reference_program(GLcontext *ctx, |
| 328 | struct gl_program **ptr, |
| 329 | struct gl_program *prog) |
| 330 | { |
| 331 | assert(ptr); |
| 332 | if (*ptr && prog) { |
| 333 | /* sanity check */ |
| 334 | ASSERT((*ptr)->Target == prog->Target); |
| 335 | } |
| 336 | if (*ptr == prog) { |
| 337 | return; /* no change */ |
| 338 | } |
| 339 | if (*ptr) { |
| 340 | GLboolean deleteFlag; |
| 341 | |
| 342 | /*_glthread_LOCK_MUTEX((*ptr)->Mutex);*/ |
| 343 | #if 0 |
| 344 | printf("Program %p %u 0x%x Refcount-- to %d\n", |
| 345 | *ptr, (*ptr)->Id, (*ptr)->Target, (*ptr)->RefCount - 1); |
| 346 | #endif |
| 347 | ASSERT((*ptr)->RefCount > 0); |
| 348 | (*ptr)->RefCount--; |
| 349 | |
| 350 | deleteFlag = ((*ptr)->RefCount == 0); |
| 351 | /*_glthread_UNLOCK_MUTEX((*ptr)->Mutex);*/ |
| 352 | |
| 353 | if (deleteFlag) { |
| 354 | ASSERT(ctx); |
| 355 | ctx->Driver.DeleteProgram(ctx, *ptr); |
| 356 | } |
| 357 | |
| 358 | *ptr = NULL; |
| 359 | } |
| 360 | |
| 361 | assert(!*ptr); |
| 362 | if (prog) { |
| 363 | /*_glthread_LOCK_MUTEX(prog->Mutex);*/ |
| 364 | prog->RefCount++; |
| 365 | #if 0 |
| 366 | printf("Program %p %u 0x%x Refcount++ to %d\n", |
| 367 | prog, prog->Id, prog->Target, prog->RefCount); |
| 368 | #endif |
| 369 | /*_glthread_UNLOCK_MUTEX(prog->Mutex);*/ |
| 370 | } |
| 371 | |
| 372 | *ptr = prog; |
| 373 | } |
| 374 | |
| 375 | |
| 376 | /** |
Brian | b2a3a85 | 2006-12-14 13:56:58 -0700 | [diff] [blame] | 377 | * Return a copy of a program. |
| 378 | * XXX Problem here if the program object is actually OO-derivation |
| 379 | * made by a device driver. |
| 380 | */ |
| 381 | struct gl_program * |
| 382 | _mesa_clone_program(GLcontext *ctx, const struct gl_program *prog) |
| 383 | { |
| 384 | struct gl_program *clone; |
| 385 | |
Brian | 4477a01 | 2007-07-24 09:57:26 -0600 | [diff] [blame] | 386 | clone = ctx->Driver.NewProgram(ctx, prog->Target, prog->Id); |
Brian | b2a3a85 | 2006-12-14 13:56:58 -0700 | [diff] [blame] | 387 | if (!clone) |
| 388 | return NULL; |
| 389 | |
| 390 | assert(clone->Target == prog->Target); |
Brian Paul | a56a59c | 2008-05-07 08:55:33 -0600 | [diff] [blame] | 391 | assert(clone->RefCount == 1); |
| 392 | |
Brian | b2a3a85 | 2006-12-14 13:56:58 -0700 | [diff] [blame] | 393 | clone->String = (GLubyte *) _mesa_strdup((char *) prog->String); |
Brian | b2a3a85 | 2006-12-14 13:56:58 -0700 | [diff] [blame] | 394 | clone->Format = prog->Format; |
| 395 | clone->Instructions = _mesa_alloc_instructions(prog->NumInstructions); |
| 396 | if (!clone->Instructions) { |
Brian Paul | b256273 | 2008-05-14 12:19:22 -0600 | [diff] [blame] | 397 | _mesa_reference_program(ctx, &clone, NULL); |
Brian | b2a3a85 | 2006-12-14 13:56:58 -0700 | [diff] [blame] | 398 | return NULL; |
| 399 | } |
Brian | 12229f1 | 2007-03-22 09:11:26 -0600 | [diff] [blame] | 400 | _mesa_copy_instructions(clone->Instructions, prog->Instructions, |
| 401 | prog->NumInstructions); |
Brian | b2a3a85 | 2006-12-14 13:56:58 -0700 | [diff] [blame] | 402 | clone->InputsRead = prog->InputsRead; |
| 403 | clone->OutputsWritten = prog->OutputsWritten; |
Brian Paul | 72f2c55 | 2008-04-04 11:20:44 -0600 | [diff] [blame] | 404 | clone->SamplersUsed = prog->SamplersUsed; |
Brian | c9db223 | 2007-01-04 17:22:19 -0700 | [diff] [blame] | 405 | memcpy(clone->TexturesUsed, prog->TexturesUsed, sizeof(prog->TexturesUsed)); |
| 406 | |
Brian | 2e76f0a | 2006-12-19 09:52:07 -0700 | [diff] [blame] | 407 | if (prog->Parameters) |
| 408 | clone->Parameters = _mesa_clone_parameter_list(prog->Parameters); |
Brian | b2a3a85 | 2006-12-14 13:56:58 -0700 | [diff] [blame] | 409 | memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams)); |
Brian | 2e76f0a | 2006-12-19 09:52:07 -0700 | [diff] [blame] | 410 | if (prog->Varying) |
| 411 | clone->Varying = _mesa_clone_parameter_list(prog->Varying); |
Brian | 3209c3e | 2007-01-09 17:49:24 -0700 | [diff] [blame] | 412 | if (prog->Attributes) |
| 413 | clone->Attributes = _mesa_clone_parameter_list(prog->Attributes); |
Brian | b2a3a85 | 2006-12-14 13:56:58 -0700 | [diff] [blame] | 414 | memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams)); |
| 415 | clone->NumInstructions = prog->NumInstructions; |
| 416 | clone->NumTemporaries = prog->NumTemporaries; |
| 417 | clone->NumParameters = prog->NumParameters; |
| 418 | clone->NumAttributes = prog->NumAttributes; |
| 419 | clone->NumAddressRegs = prog->NumAddressRegs; |
| 420 | clone->NumNativeInstructions = prog->NumNativeInstructions; |
| 421 | clone->NumNativeTemporaries = prog->NumNativeTemporaries; |
| 422 | clone->NumNativeParameters = prog->NumNativeParameters; |
| 423 | clone->NumNativeAttributes = prog->NumNativeAttributes; |
| 424 | clone->NumNativeAddressRegs = prog->NumNativeAddressRegs; |
Brian | 21f9979 | 2007-01-09 11:00:21 -0700 | [diff] [blame] | 425 | clone->NumAluInstructions = prog->NumAluInstructions; |
| 426 | clone->NumTexInstructions = prog->NumTexInstructions; |
| 427 | clone->NumTexIndirections = prog->NumTexIndirections; |
| 428 | clone->NumNativeAluInstructions = prog->NumNativeAluInstructions; |
| 429 | clone->NumNativeTexInstructions = prog->NumNativeTexInstructions; |
| 430 | clone->NumNativeTexIndirections = prog->NumNativeTexIndirections; |
Brian | b2a3a85 | 2006-12-14 13:56:58 -0700 | [diff] [blame] | 431 | |
| 432 | switch (prog->Target) { |
| 433 | case GL_VERTEX_PROGRAM_ARB: |
| 434 | { |
| 435 | const struct gl_vertex_program *vp |
| 436 | = (const struct gl_vertex_program *) prog; |
| 437 | struct gl_vertex_program *vpc = (struct gl_vertex_program *) clone; |
| 438 | vpc->IsPositionInvariant = vp->IsPositionInvariant; |
| 439 | } |
| 440 | break; |
| 441 | case GL_FRAGMENT_PROGRAM_ARB: |
| 442 | { |
| 443 | const struct gl_fragment_program *fp |
| 444 | = (const struct gl_fragment_program *) prog; |
| 445 | struct gl_fragment_program *fpc = (struct gl_fragment_program *) clone; |
Brian | b2a3a85 | 2006-12-14 13:56:58 -0700 | [diff] [blame] | 446 | fpc->FogOption = fp->FogOption; |
| 447 | fpc->UsesKill = fp->UsesKill; |
| 448 | } |
| 449 | break; |
| 450 | default: |
| 451 | _mesa_problem(NULL, "Unexpected target in _mesa_clone_program"); |
| 452 | } |
| 453 | |
| 454 | return clone; |
| 455 | } |
| 456 | |
| 457 | |
Brian | 5d1e730 | 2008-04-07 11:16:18 -0600 | [diff] [blame] | 458 | /** |
| 459 | * Insert 'count' NOP instructions at 'start' in the given program. |
| 460 | * Adjust branch targets accordingly. |
| 461 | */ |
| 462 | GLboolean |
| 463 | _mesa_insert_instructions(struct gl_program *prog, GLuint start, GLuint count) |
| 464 | { |
| 465 | const GLuint origLen = prog->NumInstructions; |
| 466 | const GLuint newLen = origLen + count; |
| 467 | struct prog_instruction *newInst; |
| 468 | GLuint i; |
| 469 | |
| 470 | /* adjust branches */ |
| 471 | for (i = 0; i < prog->NumInstructions; i++) { |
| 472 | struct prog_instruction *inst = prog->Instructions + i; |
| 473 | if (inst->BranchTarget > 0) { |
José Fonseca | 18ec140 | 2008-06-24 11:34:46 +0900 | [diff] [blame] | 474 | if ((GLuint)inst->BranchTarget >= start) { |
Brian | 5d1e730 | 2008-04-07 11:16:18 -0600 | [diff] [blame] | 475 | inst->BranchTarget += count; |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | /* Alloc storage for new instructions */ |
| 481 | newInst = _mesa_alloc_instructions(newLen); |
| 482 | if (!newInst) { |
| 483 | return GL_FALSE; |
| 484 | } |
| 485 | |
| 486 | /* Copy 'start' instructions into new instruction buffer */ |
| 487 | _mesa_copy_instructions(newInst, prog->Instructions, start); |
| 488 | |
| 489 | /* init the new instructions */ |
| 490 | _mesa_init_instructions(newInst + start, count); |
| 491 | |
| 492 | /* Copy the remaining/tail instructions to new inst buffer */ |
| 493 | _mesa_copy_instructions(newInst + start + count, |
| 494 | prog->Instructions + start, |
| 495 | origLen - start); |
| 496 | |
| 497 | /* free old instructions */ |
| 498 | _mesa_free_instructions(prog->Instructions, origLen); |
| 499 | |
| 500 | /* install new instructions */ |
| 501 | prog->Instructions = newInst; |
| 502 | prog->NumInstructions = newLen; |
| 503 | |
| 504 | return GL_TRUE; |
| 505 | } |
| 506 | |
Brian | b2a3a85 | 2006-12-14 13:56:58 -0700 | [diff] [blame] | 507 | |
| 508 | /** |
Brian | 9ffd889 | 2007-10-29 16:35:59 -0600 | [diff] [blame] | 509 | * Search instructions for registers that match (oldFile, oldIndex), |
| 510 | * replacing them with (newFile, newIndex). |
| 511 | */ |
| 512 | static void |
| 513 | replace_registers(struct prog_instruction *inst, GLuint numInst, |
| 514 | GLuint oldFile, GLuint oldIndex, |
| 515 | GLuint newFile, GLuint newIndex) |
| 516 | { |
| 517 | GLuint i, j; |
| 518 | for (i = 0; i < numInst; i++) { |
Brian Paul | cf7daba | 2008-03-25 12:27:32 -0600 | [diff] [blame] | 519 | /* src regs */ |
Brian | 9ffd889 | 2007-10-29 16:35:59 -0600 | [diff] [blame] | 520 | for (j = 0; j < _mesa_num_inst_src_regs(inst->Opcode); j++) { |
| 521 | if (inst[i].SrcReg[j].File == oldFile && |
| 522 | inst[i].SrcReg[j].Index == oldIndex) { |
| 523 | inst[i].SrcReg[j].File = newFile; |
| 524 | inst[i].SrcReg[j].Index = newIndex; |
| 525 | } |
| 526 | } |
Brian Paul | cf7daba | 2008-03-25 12:27:32 -0600 | [diff] [blame] | 527 | /* dst reg */ |
| 528 | if (inst[i].DstReg.File == oldFile && inst[i].DstReg.Index == oldIndex) { |
| 529 | inst[i].DstReg.File = newFile; |
| 530 | inst[i].DstReg.Index = newIndex; |
| 531 | } |
Brian | 9ffd889 | 2007-10-29 16:35:59 -0600 | [diff] [blame] | 532 | } |
| 533 | } |
| 534 | |
| 535 | |
| 536 | /** |
| 537 | * Search instructions for references to program parameters. When found, |
| 538 | * increment the parameter index by 'offset'. |
| 539 | * Used when combining programs. |
| 540 | */ |
| 541 | static void |
| 542 | adjust_param_indexes(struct prog_instruction *inst, GLuint numInst, |
| 543 | GLuint offset) |
| 544 | { |
| 545 | GLuint i, j; |
| 546 | for (i = 0; i < numInst; i++) { |
| 547 | for (j = 0; j < _mesa_num_inst_src_regs(inst->Opcode); j++) { |
| 548 | GLuint f = inst[i].SrcReg[j].File; |
| 549 | if (f == PROGRAM_CONSTANT || |
| 550 | f == PROGRAM_UNIFORM || |
| 551 | f == PROGRAM_STATE_VAR) { |
| 552 | inst[i].SrcReg[j].Index += offset; |
| 553 | } |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | |
| 559 | /** |
| 560 | * Combine two programs into one. Fix instructions so the outputs of |
| 561 | * the first program go to the inputs of the second program. |
| 562 | */ |
| 563 | struct gl_program * |
| 564 | _mesa_combine_programs(GLcontext *ctx, |
Brian | 1203f54 | 2007-10-29 16:38:53 -0600 | [diff] [blame] | 565 | const struct gl_program *progA, |
| 566 | const struct gl_program *progB) |
Brian | 9ffd889 | 2007-10-29 16:35:59 -0600 | [diff] [blame] | 567 | { |
| 568 | struct prog_instruction *newInst; |
| 569 | struct gl_program *newProg; |
| 570 | const GLuint lenA = progA->NumInstructions - 1; /* omit END instr */ |
| 571 | const GLuint lenB = progB->NumInstructions; |
| 572 | const GLuint numParamsA = _mesa_num_parameters(progA->Parameters); |
| 573 | const GLuint newLength = lenA + lenB; |
Brian | fb9cf48 | 2007-10-30 18:26:34 -0600 | [diff] [blame] | 574 | GLbitfield inputsB; |
Brian | 9ffd889 | 2007-10-29 16:35:59 -0600 | [diff] [blame] | 575 | GLuint i; |
| 576 | |
| 577 | ASSERT(progA->Target == progB->Target); |
| 578 | |
| 579 | newInst = _mesa_alloc_instructions(newLength); |
| 580 | if (!newInst) |
| 581 | return GL_FALSE; |
| 582 | |
| 583 | _mesa_copy_instructions(newInst, progA->Instructions, lenA); |
| 584 | _mesa_copy_instructions(newInst + lenA, progB->Instructions, lenB); |
| 585 | |
| 586 | /* adjust branch / instruction addresses for B's instructions */ |
| 587 | for (i = 0; i < lenB; i++) { |
| 588 | newInst[lenA + i].BranchTarget += lenA; |
| 589 | } |
| 590 | |
| 591 | newProg = ctx->Driver.NewProgram(ctx, progA->Target, 0); |
| 592 | newProg->Instructions = newInst; |
| 593 | newProg->NumInstructions = newLength; |
| 594 | |
| 595 | if (newProg->Target == GL_FRAGMENT_PROGRAM_ARB) { |
Brian | 27cff44 | 2008-01-16 13:54:32 -0700 | [diff] [blame] | 596 | struct gl_fragment_program *fprogA, *fprogB, *newFprog; |
| 597 | fprogA = (struct gl_fragment_program *) progA; |
| 598 | fprogB = (struct gl_fragment_program *) progB; |
| 599 | newFprog = (struct gl_fragment_program *) newProg; |
| 600 | |
| 601 | newFprog->UsesKill = fprogA->UsesKill || fprogB->UsesKill; |
| 602 | |
Brian Paul | cf7daba | 2008-03-25 12:27:32 -0600 | [diff] [blame] | 603 | /* Connect color outputs of fprogA to color inputs of fprogB, via a |
| 604 | * new temporary register. |
| 605 | */ |
Brian | 9ffd889 | 2007-10-29 16:35:59 -0600 | [diff] [blame] | 606 | if ((progA->OutputsWritten & (1 << FRAG_RESULT_COLR)) && |
| 607 | (progB->InputsRead & (1 << FRAG_ATTRIB_COL0))) { |
Brian Paul | cf7daba | 2008-03-25 12:27:32 -0600 | [diff] [blame] | 608 | GLint tempReg = _mesa_find_free_register(newProg, PROGRAM_TEMPORARY); |
Brian Paul | 65075da | 2008-05-19 16:04:02 -0600 | [diff] [blame] | 609 | if (tempReg < 0) { |
Brian Paul | cf7daba | 2008-03-25 12:27:32 -0600 | [diff] [blame] | 610 | _mesa_problem(ctx, "No free temp regs found in " |
| 611 | "_mesa_combine_programs(), using 31"); |
| 612 | tempReg = 31; |
| 613 | } |
| 614 | /* replace writes to result.color[0] with tempReg */ |
| 615 | replace_registers(newInst, lenA, |
| 616 | PROGRAM_OUTPUT, FRAG_RESULT_COLR, |
| 617 | PROGRAM_TEMPORARY, tempReg); |
| 618 | /* replace reads from input.color[0] with tempReg */ |
Brian | 9ffd889 | 2007-10-29 16:35:59 -0600 | [diff] [blame] | 619 | replace_registers(newInst + lenA, lenB, |
| 620 | PROGRAM_INPUT, FRAG_ATTRIB_COL0, |
Brian Paul | cf7daba | 2008-03-25 12:27:32 -0600 | [diff] [blame] | 621 | PROGRAM_TEMPORARY, tempReg); |
Brian | 9ffd889 | 2007-10-29 16:35:59 -0600 | [diff] [blame] | 622 | } |
| 623 | |
Brian | fb9cf48 | 2007-10-30 18:26:34 -0600 | [diff] [blame] | 624 | inputsB = progB->InputsRead; |
| 625 | if (progA->OutputsWritten & (1 << FRAG_RESULT_COLR)) { |
| 626 | inputsB &= ~(1 << FRAG_ATTRIB_COL0); |
| 627 | } |
| 628 | newProg->InputsRead = progA->InputsRead | inputsB; |
Brian | 9ffd889 | 2007-10-29 16:35:59 -0600 | [diff] [blame] | 629 | newProg->OutputsWritten = progB->OutputsWritten; |
Brian Paul | 72f2c55 | 2008-04-04 11:20:44 -0600 | [diff] [blame] | 630 | newProg->SamplersUsed = progA->SamplersUsed | progB->SamplersUsed; |
Brian | 9ffd889 | 2007-10-29 16:35:59 -0600 | [diff] [blame] | 631 | } |
| 632 | else { |
| 633 | /* vertex program */ |
| 634 | assert(0); /* XXX todo */ |
| 635 | } |
| 636 | |
| 637 | /* |
| 638 | * Merge parameters (uniforms, constants, etc) |
| 639 | */ |
| 640 | newProg->Parameters = _mesa_combine_parameter_lists(progA->Parameters, |
| 641 | progB->Parameters); |
| 642 | |
| 643 | adjust_param_indexes(newInst + lenA, lenB, numParamsA); |
| 644 | |
| 645 | |
| 646 | return newProg; |
| 647 | } |
| 648 | |
| 649 | |
| 650 | |
| 651 | |
| 652 | /** |
| 653 | * Scan the given program to find a free register of the given type. |
| 654 | * \param regFile - PROGRAM_INPUT, PROGRAM_OUTPUT or PROGRAM_TEMPORARY |
| 655 | */ |
| 656 | GLint |
| 657 | _mesa_find_free_register(const struct gl_program *prog, GLuint regFile) |
| 658 | { |
| 659 | GLboolean used[MAX_PROGRAM_TEMPS]; |
| 660 | GLuint i, k; |
| 661 | |
| 662 | assert(regFile == PROGRAM_INPUT || |
| 663 | regFile == PROGRAM_OUTPUT || |
| 664 | regFile == PROGRAM_TEMPORARY); |
| 665 | |
| 666 | _mesa_memset(used, 0, sizeof(used)); |
| 667 | |
| 668 | for (i = 0; i < prog->NumInstructions; i++) { |
| 669 | const struct prog_instruction *inst = prog->Instructions + i; |
| 670 | const GLuint n = _mesa_num_inst_src_regs(inst->Opcode); |
| 671 | |
| 672 | for (k = 0; k < n; k++) { |
| 673 | if (inst->SrcReg[k].File == regFile) { |
| 674 | used[inst->SrcReg[k].Index] = GL_TRUE; |
| 675 | } |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | for (i = 0; i < MAX_PROGRAM_TEMPS; i++) { |
| 680 | if (!used[i]) |
| 681 | return i; |
| 682 | } |
| 683 | |
| 684 | return -1; |
| 685 | } |