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