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