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