blob: d99584d63bfc55946d9964ce834f1f63fe75b45a [file] [log] [blame]
Michal Krol2861e732004-03-29 11:09:34 +00001/*
2 * Mesa 3-D graphics library
Brian942ee022007-02-09 15:40:00 -07003 * Version: 6.5.3
Michal Krol2861e732004-03-29 11:09:34 +00004 *
Brian942ee022007-02-09 15:40:00 -07005 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
Michal Krol2861e732004-03-29 11:09:34 +00006 *
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é Fonseca101d1a62008-07-23 21:06:01 +090032#include "main/glheader.h"
33#include "main/context.h"
34#include "main/hash.h"
Brian0560d812006-12-14 15:01:28 -070035#include "program.h"
Brianb26aae62007-10-31 12:00:38 -060036#include "prog_cache.h"
Brian0560d812006-12-14 15:01:28 -070037#include "prog_parameter.h"
38#include "prog_instruction.h"
Michal Krol2861e732004-03-29 11:09:34 +000039
40
Brian942ee022007-02-09 15:40:00 -070041/**
42 * A pointer to this dummy program is put into the hash table when
Brian Paul765f1a12004-09-14 22:28:27 +000043 * glGenPrograms is called.
44 */
Brian Paul122629f2006-07-20 16:49:57 +000045struct gl_program _mesa_DummyProgram;
Brian Paul765f1a12004-09-14 22:28:27 +000046
47
Michal Krol2861e732004-03-29 11:09:34 +000048/**
Brian Paul21841f02004-08-14 14:28:11 +000049 * Init context's vertex/fragment program state
Michal Krol2861e732004-03-29 11:09:34 +000050 */
51void
52_mesa_init_program(GLcontext *ctx)
53{
54 GLuint i;
55
Brian Paul5b2f8dc2009-02-18 11:47:40 -070056 /*
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 Pauldd528f02009-08-26 10:57:18 -060065 /* 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 Krol2861e732004-03-29 11:09:34 +000071 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 Paulaf3d9db2008-08-12 10:00:36 -060076#if FEATURE_es2_glsl
Kristian Høgsbergf67b0202010-05-24 10:01:38 -040077 ctx->VertexProgram.PointSizeEnabled =
78 (ctx->API == API_OPENGLES2) ? GL_TRUE : GL_FALSE;
Brian Paulaf3d9db2008-08-12 10:00:36 -060079#else
Michal Krol2861e732004-03-29 11:09:34 +000080 ctx->VertexProgram.PointSizeEnabled = GL_FALSE;
Brian Paulaf3d9db2008-08-12 10:00:36 -060081#endif
Michal Krol2861e732004-03-29 11:09:34 +000082 ctx->VertexProgram.TwoSideEnabled = GL_FALSE;
Briandf43fb62008-05-06 23:08:51 -060083 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
84 ctx->Shared->DefaultVertexProgram);
Michal Krol2861e732004-03-29 11:09:34 +000085 assert(ctx->VertexProgram.Current);
Michal Krol2861e732004-03-29 11:09:34 +000086 for (i = 0; i < MAX_NV_VERTEX_PROGRAM_PARAMS / 4; i++) {
87 ctx->VertexProgram.TrackMatrix[i] = GL_NONE;
88 ctx->VertexProgram.TrackMatrixTransform[i] = GL_IDENTITY_NV;
89 }
Brianb26aae62007-10-31 12:00:38 -060090 ctx->VertexProgram.Cache = _mesa_new_program_cache();
Michal Krol2861e732004-03-29 11:09:34 +000091#endif
92
93#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
94 ctx->FragmentProgram.Enabled = GL_FALSE;
Briandf43fb62008-05-06 23:08:51 -060095 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
96 ctx->Shared->DefaultFragmentProgram);
Michal Krol2861e732004-03-29 11:09:34 +000097 assert(ctx->FragmentProgram.Current);
Brianb26aae62007-10-31 12:00:38 -060098 ctx->FragmentProgram.Cache = _mesa_new_program_cache();
Michal Krol2861e732004-03-29 11:09:34 +000099#endif
Dave Airlie7f752fe2004-12-19 03:06:59 +0000100
Zack Rusinda7bd6a2010-06-28 17:31:21 -0400101#if FEATURE_ARB_geometry_shader4
102 ctx->GeometryProgram.Enabled = GL_FALSE;
103 /* right now by default we don't have a geometry program */
104 _mesa_reference_geomprog(ctx, &ctx->GeometryProgram.Current,
105 NULL);
106 ctx->GeometryProgram.Cache = _mesa_new_program_cache();
107#endif
Brianb26aae62007-10-31 12:00:38 -0600108
Brian Paul63d68302005-11-19 16:43:04 +0000109 /* XXX probably move this stuff */
Dave Airlie7f752fe2004-12-19 03:06:59 +0000110#if FEATURE_ATI_fragment_shader
111 ctx->ATIFragmentShader.Enabled = GL_FALSE;
Brian Paulb7eea9a2008-07-29 17:19:25 -0600112 ctx->ATIFragmentShader.Current = ctx->Shared->DefaultFragmentShader;
Dave Airlie7f752fe2004-12-19 03:06:59 +0000113 assert(ctx->ATIFragmentShader.Current);
Brian Paul63d68302005-11-19 16:43:04 +0000114 ctx->ATIFragmentShader.Current->RefCount++;
Dave Airlie7f752fe2004-12-19 03:06:59 +0000115#endif
Michal Krol2861e732004-03-29 11:09:34 +0000116}
117
118
119/**
Brian Paul21841f02004-08-14 14:28:11 +0000120 * Free a context's vertex/fragment program state
121 */
122void
123_mesa_free_program_data(GLcontext *ctx)
124{
Roland Scheidegger93da6732006-03-01 23:11:14 +0000125#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
Briandf43fb62008-05-06 23:08:51 -0600126 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current, NULL);
Brian1631a952007-12-26 06:57:24 -0700127 _mesa_delete_program_cache(ctx, ctx->VertexProgram.Cache);
Brian Paul21841f02004-08-14 14:28:11 +0000128#endif
Roland Scheidegger93da6732006-03-01 23:11:14 +0000129#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
Briandf43fb62008-05-06 23:08:51 -0600130 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current, NULL);
Brian1631a952007-12-26 06:57:24 -0700131 _mesa_delete_program_cache(ctx, ctx->FragmentProgram.Cache);
Brian Paul21841f02004-08-14 14:28:11 +0000132#endif
Zack Rusinda7bd6a2010-06-28 17:31:21 -0400133#if FEATURE_ARB_geometry_shader4
134 _mesa_reference_geomprog(ctx, &ctx->GeometryProgram.Current, NULL);
135 _mesa_delete_program_cache(ctx, ctx->GeometryProgram.Cache);
136#endif
Brian Paul63d68302005-11-19 16:43:04 +0000137 /* XXX probably move this stuff */
Dave Airlie7f752fe2004-12-19 03:06:59 +0000138#if FEATURE_ATI_fragment_shader
139 if (ctx->ATIFragmentShader.Current) {
Brian Paul63d68302005-11-19 16:43:04 +0000140 ctx->ATIFragmentShader.Current->RefCount--;
141 if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500142 free(ctx->ATIFragmentShader.Current);
Brian Paul63d68302005-11-19 16:43:04 +0000143 }
Dave Airlie7f752fe2004-12-19 03:06:59 +0000144 }
145#endif
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500146 free((void *) ctx->Program.ErrorString);
Brian Paul21841f02004-08-14 14:28:11 +0000147}
148
149
Brian4b654d42007-08-23 08:53:43 +0100150/**
151 * Update the default program objects in the given context to reference those
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200152 * specified in the shared state and release those referencing the old
Brian4b654d42007-08-23 08:53:43 +0100153 * shared state.
154 */
155void
156_mesa_update_default_objects_program(GLcontext *ctx)
157{
158#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
Brian Paul57e222d2008-05-14 12:10:45 -0600159 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
160 (struct gl_vertex_program *)
161 ctx->Shared->DefaultVertexProgram);
Brian4b654d42007-08-23 08:53:43 +0100162 assert(ctx->VertexProgram.Current);
Brian4b654d42007-08-23 08:53:43 +0100163#endif
164
165#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
Brian Paul57e222d2008-05-14 12:10:45 -0600166 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
167 (struct gl_fragment_program *)
168 ctx->Shared->DefaultFragmentProgram);
Brian4b654d42007-08-23 08:53:43 +0100169 assert(ctx->FragmentProgram.Current);
Brian4b654d42007-08-23 08:53:43 +0100170#endif
171
Zack Rusinda7bd6a2010-06-28 17:31:21 -0400172#if FEATURE_ARB_geometry_shader4
173 _mesa_reference_geomprog(ctx, &ctx->GeometryProgram.Current,
174 (struct gl_geometry_program *)
175 ctx->Shared->DefaultGeometryProgram);
176#endif
177
Brian4b654d42007-08-23 08:53:43 +0100178 /* XXX probably move this stuff */
179#if FEATURE_ATI_fragment_shader
180 if (ctx->ATIFragmentShader.Current) {
181 ctx->ATIFragmentShader.Current->RefCount--;
182 if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500183 free(ctx->ATIFragmentShader.Current);
Brian4b654d42007-08-23 08:53:43 +0100184 }
185 }
186 ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader;
187 assert(ctx->ATIFragmentShader.Current);
188 ctx->ATIFragmentShader.Current->RefCount++;
189#endif
190}
Brian Paul21841f02004-08-14 14:28:11 +0000191
192
193/**
Michal Krol2861e732004-03-29 11:09:34 +0000194 * Set the vertex/fragment program error state (position and error string).
195 * This is generally called from within the parsers.
196 */
197void
198_mesa_set_program_error(GLcontext *ctx, GLint pos, const char *string)
199{
200 ctx->Program.ErrorPos = pos;
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500201 free((void *) ctx->Program.ErrorString);
Michal Krol2861e732004-03-29 11:09:34 +0000202 if (!string)
203 string = "";
204 ctx->Program.ErrorString = _mesa_strdup(string);
205}
206
207
208/**
209 * Find the line number and column for 'pos' within 'string'.
210 * Return a copy of the line which contains 'pos'. Free the line with
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500211 * free().
Michal Krol2861e732004-03-29 11:09:34 +0000212 * \param string the program string
213 * \param pos the position within the string
214 * \param line returns the line number corresponding to 'pos'.
215 * \param col returns the column number corresponding to 'pos'.
216 * \return copy of the line containing 'pos'.
217 */
218const GLubyte *
219_mesa_find_line_column(const GLubyte *string, const GLubyte *pos,
220 GLint *line, GLint *col)
221{
222 const GLubyte *lineStart = string;
223 const GLubyte *p = string;
224 GLubyte *s;
225 int len;
226
227 *line = 1;
228
229 while (p != pos) {
230 if (*p == (GLubyte) '\n') {
231 (*line)++;
232 lineStart = p + 1;
233 }
234 p++;
235 }
236
237 *col = (pos - lineStart) + 1;
238
239 /* return copy of this line */
240 while (*p != 0 && *p != '\n')
241 p++;
242 len = p - lineStart;
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500243 s = (GLubyte *) malloc(len + 1);
Kenneth Graunkec7ac48622010-02-18 23:50:59 -0800244 memcpy(s, lineStart, len);
Michal Krol2861e732004-03-29 11:09:34 +0000245 s[len] = 0;
246
247 return s;
248}
249
250
Brian Paul765f1a12004-09-14 22:28:27 +0000251/**
252 * Initialize a new vertex/fragment program object.
253 */
Brian Paul122629f2006-07-20 16:49:57 +0000254static struct gl_program *
255_mesa_init_program_struct( GLcontext *ctx, struct gl_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000256 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000257{
Brian Paula6c423d2004-08-25 15:59:48 +0000258 (void) ctx;
Michal Krol2861e732004-03-29 11:09:34 +0000259 if (prog) {
Brian Paul0c78c762008-05-18 15:46:26 -0600260 GLuint i;
Brian Paul6bf1ea82010-02-19 08:32:36 -0700261 memset(prog, 0, sizeof(*prog));
Michal Krol2861e732004-03-29 11:09:34 +0000262 prog->Id = id;
263 prog->Target = target;
264 prog->Resident = GL_TRUE;
265 prog->RefCount = 1;
Brian Paul308b85f2006-11-23 15:58:30 +0000266 prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
Brian Paul0c78c762008-05-18 15:46:26 -0600267
268 /* default mapping from samplers to texture units */
269 for (i = 0; i < MAX_SAMPLERS; i++)
270 prog->SamplerUnits[i] = i;
Michal Krol2861e732004-03-29 11:09:34 +0000271 }
272
273 return prog;
274}
275
Brian Paul765f1a12004-09-14 22:28:27 +0000276
277/**
278 * Initialize a new fragment program object.
279 */
Brian Paul122629f2006-07-20 16:49:57 +0000280struct gl_program *
281_mesa_init_fragment_program( GLcontext *ctx, struct gl_fragment_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000282 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000283{
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200284 if (prog)
Michal Krol2861e732004-03-29 11:09:34 +0000285 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
286 else
287 return NULL;
288}
289
Brian Paul765f1a12004-09-14 22:28:27 +0000290
291/**
292 * Initialize a new vertex program object.
293 */
Brian Paul122629f2006-07-20 16:49:57 +0000294struct gl_program *
295_mesa_init_vertex_program( GLcontext *ctx, struct gl_vertex_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000296 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000297{
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200298 if (prog)
Michal Krol2861e732004-03-29 11:09:34 +0000299 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
300 else
301 return NULL;
302}
303
304
305/**
Zack Rusinda7bd6a2010-06-28 17:31:21 -0400306 * Initialize a new geometry program object.
307 */
308struct gl_program *
309_mesa_init_geometry_program( GLcontext *ctx, struct gl_geometry_program *prog,
310 GLenum target, GLuint id)
311{
312 if (prog)
313 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
314 else
315 return NULL;
316}
317
318
319/**
Michal Krol2861e732004-03-29 11:09:34 +0000320 * Allocate and initialize a new fragment/vertex program object but
321 * don't put it into the program hash table. Called via
322 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
323 * device driver function to implement OO deriviation with additional
324 * types not understood by this function.
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200325 *
Michal Krol2861e732004-03-29 11:09:34 +0000326 * \param ctx context
327 * \param id program id/number
328 * \param target program target/type
329 * \return pointer to new program object
330 */
Brian Paul122629f2006-07-20 16:49:57 +0000331struct gl_program *
Michal Krol2861e732004-03-29 11:09:34 +0000332_mesa_new_program(GLcontext *ctx, GLenum target, GLuint id)
333{
Brian Paula3e86d42008-05-16 09:56:11 -0600334 struct gl_program *prog;
Michal Krol2861e732004-03-29 11:09:34 +0000335 switch (target) {
336 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
Brian Paulc5af2ed2009-04-18 10:08:54 -0600337 case GL_VERTEX_STATE_PROGRAM_NV:
Brian Paula3e86d42008-05-16 09:56:11 -0600338 prog = _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program),
Brian Paul122629f2006-07-20 16:49:57 +0000339 target, id );
Brian Paula3e86d42008-05-16 09:56:11 -0600340 break;
Michal Krol2861e732004-03-29 11:09:34 +0000341 case GL_FRAGMENT_PROGRAM_NV:
342 case GL_FRAGMENT_PROGRAM_ARB:
Brian Paula3e86d42008-05-16 09:56:11 -0600343 prog =_mesa_init_fragment_program(ctx,
Brian Paul122629f2006-07-20 16:49:57 +0000344 CALLOC_STRUCT(gl_fragment_program),
345 target, id );
Brian Paula3e86d42008-05-16 09:56:11 -0600346 break;
Zack Rusinda7bd6a2010-06-28 17:31:21 -0400347 case MESA_GEOMETRY_PROGRAM:
348 prog = _mesa_init_geometry_program(ctx,
349 CALLOC_STRUCT(gl_geometry_program),
350 target, id);
351 break;
Michal Krol2861e732004-03-29 11:09:34 +0000352 default:
353 _mesa_problem(ctx, "bad target in _mesa_new_program");
Brian Paula3e86d42008-05-16 09:56:11 -0600354 prog = NULL;
Michal Krol2861e732004-03-29 11:09:34 +0000355 }
Brian Paula3e86d42008-05-16 09:56:11 -0600356 return prog;
Michal Krol2861e732004-03-29 11:09:34 +0000357}
358
359
360/**
361 * Delete a program and remove it from the hash table, ignoring the
362 * reference count.
363 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
364 * by a device driver function.
365 */
366void
Brian Paul122629f2006-07-20 16:49:57 +0000367_mesa_delete_program(GLcontext *ctx, struct gl_program *prog)
Michal Krol2861e732004-03-29 11:09:34 +0000368{
Brian Paula6c423d2004-08-25 15:59:48 +0000369 (void) ctx;
Brian Paul57e222d2008-05-14 12:10:45 -0600370 ASSERT(prog);
371 ASSERT(prog->RefCount==0);
Michal Krol2861e732004-03-29 11:09:34 +0000372
Brian Paul308b85f2006-11-23 15:58:30 +0000373 if (prog == &_mesa_DummyProgram)
374 return;
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200375
Michal Krol2861e732004-03-29 11:09:34 +0000376 if (prog->String)
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500377 free(prog->String);
Brian Paulde997602005-11-12 17:53:14 +0000378
Brian Paul19ad9cf2008-05-14 12:39:41 -0600379 _mesa_free_instructions(prog->Instructions, prog->NumInstructions);
Brian Paulde997602005-11-12 17:53:14 +0000380
Brian Paul65a51c02006-05-24 03:30:31 +0000381 if (prog->Parameters) {
Brian Paulde997602005-11-12 17:53:14 +0000382 _mesa_free_parameter_list(prog->Parameters);
Brian Paul65a51c02006-05-24 03:30:31 +0000383 }
Brianfe1d01c2006-12-13 14:54:47 -0700384 if (prog->Varying) {
385 _mesa_free_parameter_list(prog->Varying);
386 }
Brian3493e862007-03-24 16:18:13 -0600387 if (prog->Attributes) {
388 _mesa_free_parameter_list(prog->Attributes);
389 }
Brianfe1d01c2006-12-13 14:54:47 -0700390
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500391 free(prog);
Michal Krol2861e732004-03-29 11:09:34 +0000392}
393
394
Brian Paul4d12a052006-08-23 23:10:14 +0000395/**
396 * Return the gl_program object for a given ID.
397 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
398 * casts elsewhere.
399 */
400struct gl_program *
401_mesa_lookup_program(GLcontext *ctx, GLuint id)
402{
403 if (id)
404 return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id);
405 else
406 return NULL;
407}
408
Michal Krol2861e732004-03-29 11:09:34 +0000409
Brian Paul3b9b8de2006-08-24 21:57:36 +0000410/**
Briandf43fb62008-05-06 23:08:51 -0600411 * Reference counting for vertex/fragment programs
412 */
413void
414_mesa_reference_program(GLcontext *ctx,
415 struct gl_program **ptr,
416 struct gl_program *prog)
417{
418 assert(ptr);
419 if (*ptr && prog) {
420 /* sanity check */
Brian Paul4bc39c52008-09-26 07:40:45 -0600421 if ((*ptr)->Target == GL_VERTEX_PROGRAM_ARB)
422 ASSERT(prog->Target == GL_VERTEX_PROGRAM_ARB);
423 else if ((*ptr)->Target == GL_FRAGMENT_PROGRAM_ARB)
424 ASSERT(prog->Target == GL_FRAGMENT_PROGRAM_ARB ||
425 prog->Target == GL_FRAGMENT_PROGRAM_NV);
Zack Rusinda7bd6a2010-06-28 17:31:21 -0400426 else if ((*ptr)->Target == MESA_GEOMETRY_PROGRAM)
427 ASSERT(prog->Target == MESA_GEOMETRY_PROGRAM);
Briandf43fb62008-05-06 23:08:51 -0600428 }
429 if (*ptr == prog) {
430 return; /* no change */
431 }
432 if (*ptr) {
433 GLboolean deleteFlag;
434
435 /*_glthread_LOCK_MUTEX((*ptr)->Mutex);*/
Brian Paulb4e75d62008-05-08 10:59:31 -0600436#if 0
Brian Paula3e86d42008-05-16 09:56:11 -0600437 printf("Program %p ID=%u Target=%s Refcount-- to %d\n",
438 *ptr, (*ptr)->Id,
Zack Rusinda7bd6a2010-06-28 17:31:21 -0400439 ((*ptr)->Target == GL_VERTEX_PROGRAM_ARB ? "VP" :
440 ((*ptr)->Target == MESA_GEOMETRY_PROGRAM ? "GP" : "FP")),
Brian Paula3e86d42008-05-16 09:56:11 -0600441 (*ptr)->RefCount - 1);
Briandf43fb62008-05-06 23:08:51 -0600442#endif
443 ASSERT((*ptr)->RefCount > 0);
444 (*ptr)->RefCount--;
445
446 deleteFlag = ((*ptr)->RefCount == 0);
447 /*_glthread_UNLOCK_MUTEX((*ptr)->Mutex);*/
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200448
Briandf43fb62008-05-06 23:08:51 -0600449 if (deleteFlag) {
450 ASSERT(ctx);
451 ctx->Driver.DeleteProgram(ctx, *ptr);
452 }
453
454 *ptr = NULL;
455 }
456
457 assert(!*ptr);
458 if (prog) {
459 /*_glthread_LOCK_MUTEX(prog->Mutex);*/
460 prog->RefCount++;
Brian Paulb4e75d62008-05-08 10:59:31 -0600461#if 0
Brian Paula3e86d42008-05-16 09:56:11 -0600462 printf("Program %p ID=%u Target=%s Refcount++ to %d\n",
463 prog, prog->Id,
Zack Rusinda7bd6a2010-06-28 17:31:21 -0400464 (prog->Target == GL_VERTEX_PROGRAM_ARB ? "VP" :
465 (prog->Target == MESA_GEOMETRY_PROGRAM ? "GP" : "FP")),
Brian Paula3e86d42008-05-16 09:56:11 -0600466 prog->RefCount);
Briandf43fb62008-05-06 23:08:51 -0600467#endif
468 /*_glthread_UNLOCK_MUTEX(prog->Mutex);*/
469 }
470
471 *ptr = prog;
472}
473
474
475/**
Brianb2a3a852006-12-14 13:56:58 -0700476 * Return a copy of a program.
477 * XXX Problem here if the program object is actually OO-derivation
478 * made by a device driver.
479 */
480struct gl_program *
481_mesa_clone_program(GLcontext *ctx, const struct gl_program *prog)
482{
483 struct gl_program *clone;
484
Brian5b6858c2007-07-24 09:56:44 -0600485 clone = ctx->Driver.NewProgram(ctx, prog->Target, prog->Id);
Brianb2a3a852006-12-14 13:56:58 -0700486 if (!clone)
487 return NULL;
488
489 assert(clone->Target == prog->Target);
Briandf43fb62008-05-06 23:08:51 -0600490 assert(clone->RefCount == 1);
491
Brianb2a3a852006-12-14 13:56:58 -0700492 clone->String = (GLubyte *) _mesa_strdup((char *) prog->String);
Brianb2a3a852006-12-14 13:56:58 -0700493 clone->Format = prog->Format;
494 clone->Instructions = _mesa_alloc_instructions(prog->NumInstructions);
495 if (!clone->Instructions) {
Brian Paul57e222d2008-05-14 12:10:45 -0600496 _mesa_reference_program(ctx, &clone, NULL);
Brianb2a3a852006-12-14 13:56:58 -0700497 return NULL;
498 }
Brian12229f12007-03-22 09:11:26 -0600499 _mesa_copy_instructions(clone->Instructions, prog->Instructions,
500 prog->NumInstructions);
Brianb2a3a852006-12-14 13:56:58 -0700501 clone->InputsRead = prog->InputsRead;
502 clone->OutputsWritten = prog->OutputsWritten;
Brian Paul0c78c762008-05-18 15:46:26 -0600503 clone->SamplersUsed = prog->SamplersUsed;
Nicolai Haehnle82635aa2008-07-05 18:03:44 +0200504 clone->ShadowSamplers = prog->ShadowSamplers;
Brianc9db2232007-01-04 17:22:19 -0700505 memcpy(clone->TexturesUsed, prog->TexturesUsed, sizeof(prog->TexturesUsed));
506
Brian2e76f0a2006-12-19 09:52:07 -0700507 if (prog->Parameters)
508 clone->Parameters = _mesa_clone_parameter_list(prog->Parameters);
Brianb2a3a852006-12-14 13:56:58 -0700509 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
Brian2e76f0a2006-12-19 09:52:07 -0700510 if (prog->Varying)
511 clone->Varying = _mesa_clone_parameter_list(prog->Varying);
Brian3209c3e2007-01-09 17:49:24 -0700512 if (prog->Attributes)
513 clone->Attributes = _mesa_clone_parameter_list(prog->Attributes);
Brianb2a3a852006-12-14 13:56:58 -0700514 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
Brian Paul56643092010-07-30 14:24:23 -0600515 clone->IndirectRegisterFiles = prog->IndirectRegisterFiles;
Brianb2a3a852006-12-14 13:56:58 -0700516 clone->NumInstructions = prog->NumInstructions;
517 clone->NumTemporaries = prog->NumTemporaries;
518 clone->NumParameters = prog->NumParameters;
519 clone->NumAttributes = prog->NumAttributes;
520 clone->NumAddressRegs = prog->NumAddressRegs;
521 clone->NumNativeInstructions = prog->NumNativeInstructions;
522 clone->NumNativeTemporaries = prog->NumNativeTemporaries;
523 clone->NumNativeParameters = prog->NumNativeParameters;
524 clone->NumNativeAttributes = prog->NumNativeAttributes;
525 clone->NumNativeAddressRegs = prog->NumNativeAddressRegs;
Brian21f99792007-01-09 11:00:21 -0700526 clone->NumAluInstructions = prog->NumAluInstructions;
527 clone->NumTexInstructions = prog->NumTexInstructions;
528 clone->NumTexIndirections = prog->NumTexIndirections;
529 clone->NumNativeAluInstructions = prog->NumNativeAluInstructions;
530 clone->NumNativeTexInstructions = prog->NumNativeTexInstructions;
531 clone->NumNativeTexIndirections = prog->NumNativeTexIndirections;
Brianb2a3a852006-12-14 13:56:58 -0700532
533 switch (prog->Target) {
534 case GL_VERTEX_PROGRAM_ARB:
535 {
536 const struct gl_vertex_program *vp
537 = (const struct gl_vertex_program *) prog;
538 struct gl_vertex_program *vpc = (struct gl_vertex_program *) clone;
539 vpc->IsPositionInvariant = vp->IsPositionInvariant;
Nicolai Hähnle736e1ae2009-09-09 19:56:57 +0200540 vpc->IsNVProgram = vp->IsNVProgram;
Brianb2a3a852006-12-14 13:56:58 -0700541 }
542 break;
543 case GL_FRAGMENT_PROGRAM_ARB:
544 {
545 const struct gl_fragment_program *fp
546 = (const struct gl_fragment_program *) prog;
547 struct gl_fragment_program *fpc = (struct gl_fragment_program *) clone;
Brianb2a3a852006-12-14 13:56:58 -0700548 fpc->FogOption = fp->FogOption;
549 fpc->UsesKill = fp->UsesKill;
Brian Paulb947b1d2010-02-13 13:51:38 -0700550 fpc->OriginUpperLeft = fp->OriginUpperLeft;
551 fpc->PixelCenterInteger = fp->PixelCenterInteger;
Brianb2a3a852006-12-14 13:56:58 -0700552 }
553 break;
Zack Rusinda7bd6a2010-06-28 17:31:21 -0400554 case MESA_GEOMETRY_PROGRAM:
555 {
556 const struct gl_geometry_program *gp
557 = (const struct gl_geometry_program *) prog;
558 struct gl_geometry_program *gpc = (struct gl_geometry_program *) clone;
559 gpc->VerticesOut = gp->VerticesOut;
560 gpc->InputType = gp->InputType;
561 gpc->OutputType = gp->OutputType;
562 }
563 break;
Brianb2a3a852006-12-14 13:56:58 -0700564 default:
565 _mesa_problem(NULL, "Unexpected target in _mesa_clone_program");
566 }
567
568 return clone;
569}
570
571
Brian Paul19ad9cf2008-05-14 12:39:41 -0600572/**
573 * Insert 'count' NOP instructions at 'start' in the given program.
574 * Adjust branch targets accordingly.
575 */
576GLboolean
577_mesa_insert_instructions(struct gl_program *prog, GLuint start, GLuint count)
578{
579 const GLuint origLen = prog->NumInstructions;
580 const GLuint newLen = origLen + count;
581 struct prog_instruction *newInst;
582 GLuint i;
583
584 /* adjust branches */
585 for (i = 0; i < prog->NumInstructions; i++) {
586 struct prog_instruction *inst = prog->Instructions + i;
587 if (inst->BranchTarget > 0) {
José Fonseca457d7212008-06-24 11:34:46 +0900588 if ((GLuint)inst->BranchTarget >= start) {
Brian Paul19ad9cf2008-05-14 12:39:41 -0600589 inst->BranchTarget += count;
590 }
591 }
592 }
593
594 /* Alloc storage for new instructions */
595 newInst = _mesa_alloc_instructions(newLen);
596 if (!newInst) {
597 return GL_FALSE;
598 }
599
600 /* Copy 'start' instructions into new instruction buffer */
601 _mesa_copy_instructions(newInst, prog->Instructions, start);
602
603 /* init the new instructions */
604 _mesa_init_instructions(newInst + start, count);
605
606 /* Copy the remaining/tail instructions to new inst buffer */
607 _mesa_copy_instructions(newInst + start + count,
608 prog->Instructions + start,
609 origLen - start);
610
611 /* free old instructions */
612 _mesa_free_instructions(prog->Instructions, origLen);
613
614 /* install new instructions */
615 prog->Instructions = newInst;
616 prog->NumInstructions = newLen;
617
618 return GL_TRUE;
619}
620
Brianb2a3a852006-12-14 13:56:58 -0700621/**
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200622 * Delete 'count' instructions at 'start' in the given program.
623 * Adjust branch targets accordingly.
624 */
625GLboolean
626_mesa_delete_instructions(struct gl_program *prog, GLuint start, GLuint count)
627{
628 const GLuint origLen = prog->NumInstructions;
629 const GLuint newLen = origLen - count;
630 struct prog_instruction *newInst;
631 GLuint i;
632
633 /* adjust branches */
634 for (i = 0; i < prog->NumInstructions; i++) {
635 struct prog_instruction *inst = prog->Instructions + i;
636 if (inst->BranchTarget > 0) {
Brian Paul20fbb242010-01-27 17:03:04 -0700637 if (inst->BranchTarget > (GLint) start) {
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200638 inst->BranchTarget -= count;
639 }
640 }
641 }
642
643 /* Alloc storage for new instructions */
644 newInst = _mesa_alloc_instructions(newLen);
645 if (!newInst) {
646 return GL_FALSE;
647 }
648
649 /* Copy 'start' instructions into new instruction buffer */
650 _mesa_copy_instructions(newInst, prog->Instructions, start);
651
652 /* Copy the remaining/tail instructions to new inst buffer */
653 _mesa_copy_instructions(newInst + start,
654 prog->Instructions + start + count,
655 newLen - start);
656
657 /* free old instructions */
658 _mesa_free_instructions(prog->Instructions, origLen);
659
660 /* install new instructions */
661 prog->Instructions = newInst;
662 prog->NumInstructions = newLen;
663
664 return GL_TRUE;
665}
666
667
668/**
Brian Paul6ca948a2008-05-14 12:53:03 -0600669 * Search instructions for registers that match (oldFile, oldIndex),
670 * replacing them with (newFile, newIndex).
671 */
672static void
673replace_registers(struct prog_instruction *inst, GLuint numInst,
674 GLuint oldFile, GLuint oldIndex,
675 GLuint newFile, GLuint newIndex)
676{
677 GLuint i, j;
678 for (i = 0; i < numInst; i++) {
Brian Paul0c78c762008-05-18 15:46:26 -0600679 /* src regs */
Brian Paulb22a00b2010-04-09 10:03:31 -0600680 for (j = 0; j < _mesa_num_inst_src_regs(inst[i].Opcode); j++) {
Brian Paul6ca948a2008-05-14 12:53:03 -0600681 if (inst[i].SrcReg[j].File == oldFile &&
682 inst[i].SrcReg[j].Index == oldIndex) {
683 inst[i].SrcReg[j].File = newFile;
684 inst[i].SrcReg[j].Index = newIndex;
685 }
686 }
Brian Paul0c78c762008-05-18 15:46:26 -0600687 /* dst reg */
688 if (inst[i].DstReg.File == oldFile && inst[i].DstReg.Index == oldIndex) {
689 inst[i].DstReg.File = newFile;
690 inst[i].DstReg.Index = newIndex;
691 }
Brian Paul6ca948a2008-05-14 12:53:03 -0600692 }
693}
694
695
696/**
697 * Search instructions for references to program parameters. When found,
698 * increment the parameter index by 'offset'.
699 * Used when combining programs.
700 */
701static void
702adjust_param_indexes(struct prog_instruction *inst, GLuint numInst,
703 GLuint offset)
704{
705 GLuint i, j;
706 for (i = 0; i < numInst; i++) {
Brian Paulb22a00b2010-04-09 10:03:31 -0600707 for (j = 0; j < _mesa_num_inst_src_regs(inst[i].Opcode); j++) {
Brian Paul6ca948a2008-05-14 12:53:03 -0600708 GLuint f = inst[i].SrcReg[j].File;
709 if (f == PROGRAM_CONSTANT ||
710 f == PROGRAM_UNIFORM ||
711 f == PROGRAM_STATE_VAR) {
712 inst[i].SrcReg[j].Index += offset;
713 }
714 }
715 }
716}
717
718
719/**
720 * Combine two programs into one. Fix instructions so the outputs of
721 * the first program go to the inputs of the second program.
722 */
723struct gl_program *
724_mesa_combine_programs(GLcontext *ctx,
Brian Paul0c78c762008-05-18 15:46:26 -0600725 const struct gl_program *progA,
726 const struct gl_program *progB)
Brian Paul6ca948a2008-05-14 12:53:03 -0600727{
728 struct prog_instruction *newInst;
729 struct gl_program *newProg;
730 const GLuint lenA = progA->NumInstructions - 1; /* omit END instr */
731 const GLuint lenB = progB->NumInstructions;
732 const GLuint numParamsA = _mesa_num_parameters(progA->Parameters);
733 const GLuint newLength = lenA + lenB;
Brian Paula2ddb3d2010-02-01 18:00:12 -0700734 GLboolean usedTemps[MAX_PROGRAM_TEMPS];
735 GLuint firstTemp = 0;
Brian Paul0c78c762008-05-18 15:46:26 -0600736 GLbitfield inputsB;
Brian Paul6ca948a2008-05-14 12:53:03 -0600737 GLuint i;
738
739 ASSERT(progA->Target == progB->Target);
740
741 newInst = _mesa_alloc_instructions(newLength);
742 if (!newInst)
743 return GL_FALSE;
744
745 _mesa_copy_instructions(newInst, progA->Instructions, lenA);
746 _mesa_copy_instructions(newInst + lenA, progB->Instructions, lenB);
747
748 /* adjust branch / instruction addresses for B's instructions */
749 for (i = 0; i < lenB; i++) {
750 newInst[lenA + i].BranchTarget += lenA;
751 }
752
753 newProg = ctx->Driver.NewProgram(ctx, progA->Target, 0);
754 newProg->Instructions = newInst;
755 newProg->NumInstructions = newLength;
756
Brian Paula2ddb3d2010-02-01 18:00:12 -0700757 /* find used temp regs (we may need new temps below) */
758 _mesa_find_used_registers(newProg, PROGRAM_TEMPORARY,
759 usedTemps, MAX_PROGRAM_TEMPS);
760
Brian Paul6ca948a2008-05-14 12:53:03 -0600761 if (newProg->Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul0c78c762008-05-18 15:46:26 -0600762 struct gl_fragment_program *fprogA, *fprogB, *newFprog;
Brian Paul5c4bd762008-10-08 14:02:24 -0600763 GLbitfield progB_inputsRead = progB->InputsRead;
764 GLint progB_colorFile, progB_colorIndex;
765
Brian Paul0c78c762008-05-18 15:46:26 -0600766 fprogA = (struct gl_fragment_program *) progA;
767 fprogB = (struct gl_fragment_program *) progB;
768 newFprog = (struct gl_fragment_program *) newProg;
769
770 newFprog->UsesKill = fprogA->UsesKill || fprogB->UsesKill;
771
Brian Paul5c4bd762008-10-08 14:02:24 -0600772 /* We'll do a search and replace for instances
773 * of progB_colorFile/progB_colorIndex below...
774 */
775 progB_colorFile = PROGRAM_INPUT;
776 progB_colorIndex = FRAG_ATTRIB_COL0;
777
778 /*
779 * The fragment program may get color from a state var rather than
780 * a fragment input (vertex output) if it's constant.
781 * See the texenvprogram.c code.
782 * So, search the program's parameter list now to see if the program
783 * gets color from a state var instead of a conventional fragment
784 * input register.
785 */
786 for (i = 0; i < progB->Parameters->NumParameters; i++) {
787 struct gl_program_parameter *p = &progB->Parameters->Parameters[i];
788 if (p->Type == PROGRAM_STATE_VAR &&
789 p->StateIndexes[0] == STATE_INTERNAL &&
790 p->StateIndexes[1] == STATE_CURRENT_ATTRIB &&
791 p->StateIndexes[2] == VERT_ATTRIB_COLOR0) {
792 progB_inputsRead |= FRAG_BIT_COL0;
793 progB_colorFile = PROGRAM_STATE_VAR;
794 progB_colorIndex = i;
795 break;
796 }
797 }
798
Brian Paul0c78c762008-05-18 15:46:26 -0600799 /* Connect color outputs of fprogA to color inputs of fprogB, via a
800 * new temporary register.
801 */
Brian Paul8d475822009-02-28 11:49:46 -0700802 if ((progA->OutputsWritten & (1 << FRAG_RESULT_COLOR)) &&
Brian Paul5c4bd762008-10-08 14:02:24 -0600803 (progB_inputsRead & FRAG_BIT_COL0)) {
Brian Paula2ddb3d2010-02-01 18:00:12 -0700804 GLint tempReg = _mesa_find_free_register(usedTemps, MAX_PROGRAM_TEMPS,
805 firstTemp);
Brian Paule469d782008-05-19 16:03:43 -0600806 if (tempReg < 0) {
Brian Paul0c78c762008-05-18 15:46:26 -0600807 _mesa_problem(ctx, "No free temp regs found in "
808 "_mesa_combine_programs(), using 31");
809 tempReg = 31;
810 }
Brian Paula2ddb3d2010-02-01 18:00:12 -0700811 firstTemp = tempReg + 1;
812
Brian Paul0c78c762008-05-18 15:46:26 -0600813 /* replace writes to result.color[0] with tempReg */
814 replace_registers(newInst, lenA,
Brian Paul8d475822009-02-28 11:49:46 -0700815 PROGRAM_OUTPUT, FRAG_RESULT_COLOR,
Brian Paul0c78c762008-05-18 15:46:26 -0600816 PROGRAM_TEMPORARY, tempReg);
Brian Paul5c4bd762008-10-08 14:02:24 -0600817 /* replace reads from the input color with tempReg */
Brian Paul6ca948a2008-05-14 12:53:03 -0600818 replace_registers(newInst + lenA, lenB,
Brian Paul5c4bd762008-10-08 14:02:24 -0600819 progB_colorFile, progB_colorIndex, /* search for */
820 PROGRAM_TEMPORARY, tempReg /* replace with */ );
Brian Paul6ca948a2008-05-14 12:53:03 -0600821 }
822
Brian Paul5c4bd762008-10-08 14:02:24 -0600823 /* compute combined program's InputsRead */
824 inputsB = progB_inputsRead;
Brian Paul8d475822009-02-28 11:49:46 -0700825 if (progA->OutputsWritten & (1 << FRAG_RESULT_COLOR)) {
Brian Paul0c78c762008-05-18 15:46:26 -0600826 inputsB &= ~(1 << FRAG_ATTRIB_COL0);
827 }
828 newProg->InputsRead = progA->InputsRead | inputsB;
Brian Paul6ca948a2008-05-14 12:53:03 -0600829 newProg->OutputsWritten = progB->OutputsWritten;
Brian Paul0c78c762008-05-18 15:46:26 -0600830 newProg->SamplersUsed = progA->SamplersUsed | progB->SamplersUsed;
Brian Paul6ca948a2008-05-14 12:53:03 -0600831 }
832 else {
833 /* vertex program */
834 assert(0); /* XXX todo */
835 }
836
837 /*
838 * Merge parameters (uniforms, constants, etc)
839 */
840 newProg->Parameters = _mesa_combine_parameter_lists(progA->Parameters,
841 progB->Parameters);
842
843 adjust_param_indexes(newInst + lenA, lenB, numParamsA);
844
845
846 return newProg;
847}
848
849
Brian Paul6ca948a2008-05-14 12:53:03 -0600850/**
Brian Paula2ddb3d2010-02-01 18:00:12 -0700851 * Populate the 'used' array with flags indicating which registers (TEMPs,
852 * INPUTs, OUTPUTs, etc, are used by the given program.
853 * \param file type of register to scan for
854 * \param used returns true/false flags for in use / free
855 * \param usedSize size of the 'used' array
Brian Paul6ca948a2008-05-14 12:53:03 -0600856 */
Brian Paula2ddb3d2010-02-01 18:00:12 -0700857void
858_mesa_find_used_registers(const struct gl_program *prog,
859 gl_register_file file,
860 GLboolean used[], GLuint usedSize)
Brian Paul6ca948a2008-05-14 12:53:03 -0600861{
Brian Paula2ddb3d2010-02-01 18:00:12 -0700862 GLuint i, j;
Brian Paul6ca948a2008-05-14 12:53:03 -0600863
Kenneth Graunke26f8fad2010-02-18 23:51:00 -0800864 memset(used, 0, usedSize);
Brian Paul6ca948a2008-05-14 12:53:03 -0600865
866 for (i = 0; i < prog->NumInstructions; i++) {
867 const struct prog_instruction *inst = prog->Instructions + i;
868 const GLuint n = _mesa_num_inst_src_regs(inst->Opcode);
869
Brian Paula2ddb3d2010-02-01 18:00:12 -0700870 if (inst->DstReg.File == file) {
Brian Paul5076a4f2009-12-11 09:16:25 -0700871 used[inst->DstReg.Index] = GL_TRUE;
872 }
Brian Paula2ddb3d2010-02-01 18:00:12 -0700873
874 for (j = 0; j < n; j++) {
875 if (inst->SrcReg[j].File == file) {
876 used[inst->SrcReg[j].Index] = GL_TRUE;
Brian Paul6ca948a2008-05-14 12:53:03 -0600877 }
878 }
879 }
Brian Paula2ddb3d2010-02-01 18:00:12 -0700880}
Brian Paul6ca948a2008-05-14 12:53:03 -0600881
Brian Paula2ddb3d2010-02-01 18:00:12 -0700882
883/**
884 * Scan the given 'used' register flag array for the first entry
885 * that's >= firstReg.
886 * \param used vector of flags indicating registers in use (as returned
887 * by _mesa_find_used_registers())
888 * \param usedSize size of the 'used' array
889 * \param firstReg first register to start searching at
890 * \return index of unused register, or -1 if none.
891 */
892GLint
893_mesa_find_free_register(const GLboolean used[],
894 GLuint usedSize, GLuint firstReg)
895{
896 GLuint i;
897
898 assert(firstReg < usedSize);
899
900 for (i = firstReg; i < usedSize; i++)
Brian Paul6ca948a2008-05-14 12:53:03 -0600901 if (!used[i])
902 return i;
Brian Paul6ca948a2008-05-14 12:53:03 -0600903
904 return -1;
905}
Brian Paulec6ad7b2009-06-17 07:42:49 -0600906
907
Brian Paulec6ad7b2009-06-17 07:42:49 -0600908/**
909 * "Post-process" a GPU program. This is intended to be used for debugging.
910 * Example actions include no-op'ing instructions or changing instruction
911 * behaviour.
912 */
913void
914_mesa_postprocess_program(GLcontext *ctx, struct gl_program *prog)
915{
916 static const GLfloat white[4] = { 0.5, 0.5, 0.5, 0.5 };
917 GLuint i;
918 GLuint whiteSwizzle;
919 GLint whiteIndex = _mesa_add_unnamed_constant(prog->Parameters,
920 white, 4, &whiteSwizzle);
921
Brian Paul516d20f2009-06-17 07:43:44 -0600922 (void) whiteIndex;
923
Brian Paulec6ad7b2009-06-17 07:42:49 -0600924 for (i = 0; i < prog->NumInstructions; i++) {
925 struct prog_instruction *inst = prog->Instructions + i;
926 const GLuint n = _mesa_num_inst_src_regs(inst->Opcode);
927
928 (void) n;
929
930 if (_mesa_is_tex_instruction(inst->Opcode)) {
931#if 0
932 /* replace TEX/TXP/TXB with MOV */
933 inst->Opcode = OPCODE_MOV;
934 inst->DstReg.WriteMask = WRITEMASK_XYZW;
935 inst->SrcReg[0].Swizzle = SWIZZLE_XYZW;
936 inst->SrcReg[0].Negate = NEGATE_NONE;
937#endif
938
939#if 0
940 /* disable shadow texture mode */
941 inst->TexShadow = 0;
942#endif
943 }
944
945 if (inst->Opcode == OPCODE_TXP) {
946#if 0
947 inst->Opcode = OPCODE_MOV;
948 inst->DstReg.WriteMask = WRITEMASK_XYZW;
949 inst->SrcReg[0].File = PROGRAM_CONSTANT;
950 inst->SrcReg[0].Index = whiteIndex;
951 inst->SrcReg[0].Swizzle = SWIZZLE_XYZW;
952 inst->SrcReg[0].Negate = NEGATE_NONE;
953#endif
954#if 0
955 inst->TexShadow = 0;
956#endif
957#if 0
958 inst->Opcode = OPCODE_TEX;
959 inst->TexShadow = 0;
960#endif
961 }
962
963 }
964}