blob: d270bf9e1c36cc07032f87fc57a37599b0688cb2 [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
Michal Krol2861e732004-03-29 11:09:34 +000065 ctx->Program.ErrorPos = -1;
66 ctx->Program.ErrorString = _mesa_strdup("");
67
68#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
69 ctx->VertexProgram.Enabled = GL_FALSE;
Brian Paulaf3d9db2008-08-12 10:00:36 -060070#if FEATURE_es2_glsl
71 ctx->VertexProgram.PointSizeEnabled = GL_TRUE;
72#else
Michal Krol2861e732004-03-29 11:09:34 +000073 ctx->VertexProgram.PointSizeEnabled = GL_FALSE;
Brian Paulaf3d9db2008-08-12 10:00:36 -060074#endif
Michal Krol2861e732004-03-29 11:09:34 +000075 ctx->VertexProgram.TwoSideEnabled = GL_FALSE;
Briandf43fb62008-05-06 23:08:51 -060076 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
77 ctx->Shared->DefaultVertexProgram);
Michal Krol2861e732004-03-29 11:09:34 +000078 assert(ctx->VertexProgram.Current);
Michal Krol2861e732004-03-29 11:09:34 +000079 for (i = 0; i < MAX_NV_VERTEX_PROGRAM_PARAMS / 4; i++) {
80 ctx->VertexProgram.TrackMatrix[i] = GL_NONE;
81 ctx->VertexProgram.TrackMatrixTransform[i] = GL_IDENTITY_NV;
82 }
Brianb26aae62007-10-31 12:00:38 -060083 ctx->VertexProgram.Cache = _mesa_new_program_cache();
Michal Krol2861e732004-03-29 11:09:34 +000084#endif
85
86#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
87 ctx->FragmentProgram.Enabled = GL_FALSE;
Briandf43fb62008-05-06 23:08:51 -060088 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
89 ctx->Shared->DefaultFragmentProgram);
Michal Krol2861e732004-03-29 11:09:34 +000090 assert(ctx->FragmentProgram.Current);
Brianb26aae62007-10-31 12:00:38 -060091 ctx->FragmentProgram.Cache = _mesa_new_program_cache();
Michal Krol2861e732004-03-29 11:09:34 +000092#endif
Dave Airlie7f752fe2004-12-19 03:06:59 +000093
Brianb26aae62007-10-31 12:00:38 -060094
Brian Paul63d68302005-11-19 16:43:04 +000095 /* XXX probably move this stuff */
Dave Airlie7f752fe2004-12-19 03:06:59 +000096#if FEATURE_ATI_fragment_shader
97 ctx->ATIFragmentShader.Enabled = GL_FALSE;
Brian Paulb7eea9a2008-07-29 17:19:25 -060098 ctx->ATIFragmentShader.Current = ctx->Shared->DefaultFragmentShader;
Dave Airlie7f752fe2004-12-19 03:06:59 +000099 assert(ctx->ATIFragmentShader.Current);
Brian Paul63d68302005-11-19 16:43:04 +0000100 ctx->ATIFragmentShader.Current->RefCount++;
Dave Airlie7f752fe2004-12-19 03:06:59 +0000101#endif
Michal Krol2861e732004-03-29 11:09:34 +0000102}
103
104
105/**
Brian Paul21841f02004-08-14 14:28:11 +0000106 * Free a context's vertex/fragment program state
107 */
108void
109_mesa_free_program_data(GLcontext *ctx)
110{
Roland Scheidegger93da6732006-03-01 23:11:14 +0000111#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
Briandf43fb62008-05-06 23:08:51 -0600112 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current, NULL);
Brian1631a952007-12-26 06:57:24 -0700113 _mesa_delete_program_cache(ctx, ctx->VertexProgram.Cache);
Brian Paul21841f02004-08-14 14:28:11 +0000114#endif
Roland Scheidegger93da6732006-03-01 23:11:14 +0000115#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
Briandf43fb62008-05-06 23:08:51 -0600116 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current, NULL);
Brian1631a952007-12-26 06:57:24 -0700117 _mesa_delete_program_cache(ctx, ctx->FragmentProgram.Cache);
Brian Paul21841f02004-08-14 14:28:11 +0000118#endif
Brian Paul63d68302005-11-19 16:43:04 +0000119 /* XXX probably move this stuff */
Dave Airlie7f752fe2004-12-19 03:06:59 +0000120#if FEATURE_ATI_fragment_shader
121 if (ctx->ATIFragmentShader.Current) {
Brian Paul63d68302005-11-19 16:43:04 +0000122 ctx->ATIFragmentShader.Current->RefCount--;
123 if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
124 _mesa_free(ctx->ATIFragmentShader.Current);
125 }
Dave Airlie7f752fe2004-12-19 03:06:59 +0000126 }
127#endif
Brian Paul21841f02004-08-14 14:28:11 +0000128 _mesa_free((void *) ctx->Program.ErrorString);
129}
130
131
Brian4b654d42007-08-23 08:53:43 +0100132/**
133 * Update the default program objects in the given context to reference those
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200134 * specified in the shared state and release those referencing the old
Brian4b654d42007-08-23 08:53:43 +0100135 * shared state.
136 */
137void
138_mesa_update_default_objects_program(GLcontext *ctx)
139{
140#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
Brian Paul57e222d2008-05-14 12:10:45 -0600141 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
142 (struct gl_vertex_program *)
143 ctx->Shared->DefaultVertexProgram);
Brian4b654d42007-08-23 08:53:43 +0100144 assert(ctx->VertexProgram.Current);
Brian4b654d42007-08-23 08:53:43 +0100145#endif
146
147#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
Brian Paul57e222d2008-05-14 12:10:45 -0600148 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
149 (struct gl_fragment_program *)
150 ctx->Shared->DefaultFragmentProgram);
Brian4b654d42007-08-23 08:53:43 +0100151 assert(ctx->FragmentProgram.Current);
Brian4b654d42007-08-23 08:53:43 +0100152#endif
153
154 /* XXX probably move this stuff */
155#if FEATURE_ATI_fragment_shader
156 if (ctx->ATIFragmentShader.Current) {
157 ctx->ATIFragmentShader.Current->RefCount--;
158 if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
159 _mesa_free(ctx->ATIFragmentShader.Current);
160 }
161 }
162 ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader;
163 assert(ctx->ATIFragmentShader.Current);
164 ctx->ATIFragmentShader.Current->RefCount++;
165#endif
166}
Brian Paul21841f02004-08-14 14:28:11 +0000167
168
169/**
Michal Krol2861e732004-03-29 11:09:34 +0000170 * Set the vertex/fragment program error state (position and error string).
171 * This is generally called from within the parsers.
172 */
173void
174_mesa_set_program_error(GLcontext *ctx, GLint pos, const char *string)
175{
176 ctx->Program.ErrorPos = pos;
177 _mesa_free((void *) ctx->Program.ErrorString);
178 if (!string)
179 string = "";
180 ctx->Program.ErrorString = _mesa_strdup(string);
181}
182
183
184/**
185 * Find the line number and column for 'pos' within 'string'.
186 * Return a copy of the line which contains 'pos'. Free the line with
187 * _mesa_free().
188 * \param string the program string
189 * \param pos the position within the string
190 * \param line returns the line number corresponding to 'pos'.
191 * \param col returns the column number corresponding to 'pos'.
192 * \return copy of the line containing 'pos'.
193 */
194const GLubyte *
195_mesa_find_line_column(const GLubyte *string, const GLubyte *pos,
196 GLint *line, GLint *col)
197{
198 const GLubyte *lineStart = string;
199 const GLubyte *p = string;
200 GLubyte *s;
201 int len;
202
203 *line = 1;
204
205 while (p != pos) {
206 if (*p == (GLubyte) '\n') {
207 (*line)++;
208 lineStart = p + 1;
209 }
210 p++;
211 }
212
213 *col = (pos - lineStart) + 1;
214
215 /* return copy of this line */
216 while (*p != 0 && *p != '\n')
217 p++;
218 len = p - lineStart;
219 s = (GLubyte *) _mesa_malloc(len + 1);
220 _mesa_memcpy(s, lineStart, len);
221 s[len] = 0;
222
223 return s;
224}
225
226
Brian Paul765f1a12004-09-14 22:28:27 +0000227/**
228 * Initialize a new vertex/fragment program object.
229 */
Brian Paul122629f2006-07-20 16:49:57 +0000230static struct gl_program *
231_mesa_init_program_struct( GLcontext *ctx, struct gl_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000232 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000233{
Brian Paula6c423d2004-08-25 15:59:48 +0000234 (void) ctx;
Michal Krol2861e732004-03-29 11:09:34 +0000235 if (prog) {
Brian Paul0c78c762008-05-18 15:46:26 -0600236 GLuint i;
237 _mesa_bzero(prog, sizeof(*prog));
Michal Krol2861e732004-03-29 11:09:34 +0000238 prog->Id = id;
239 prog->Target = target;
240 prog->Resident = GL_TRUE;
241 prog->RefCount = 1;
Brian Paul308b85f2006-11-23 15:58:30 +0000242 prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
Brian Paul0c78c762008-05-18 15:46:26 -0600243
244 /* default mapping from samplers to texture units */
245 for (i = 0; i < MAX_SAMPLERS; i++)
246 prog->SamplerUnits[i] = i;
Michal Krol2861e732004-03-29 11:09:34 +0000247 }
248
249 return prog;
250}
251
Brian Paul765f1a12004-09-14 22:28:27 +0000252
253/**
254 * Initialize a new fragment program object.
255 */
Brian Paul122629f2006-07-20 16:49:57 +0000256struct gl_program *
257_mesa_init_fragment_program( GLcontext *ctx, struct gl_fragment_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000258 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000259{
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200260 if (prog)
Michal Krol2861e732004-03-29 11:09:34 +0000261 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
262 else
263 return NULL;
264}
265
Brian Paul765f1a12004-09-14 22:28:27 +0000266
267/**
268 * Initialize a new vertex program object.
269 */
Brian Paul122629f2006-07-20 16:49:57 +0000270struct gl_program *
271_mesa_init_vertex_program( GLcontext *ctx, struct gl_vertex_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000272 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000273{
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200274 if (prog)
Michal Krol2861e732004-03-29 11:09:34 +0000275 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
276 else
277 return NULL;
278}
279
280
281/**
282 * Allocate and initialize a new fragment/vertex program object but
283 * don't put it into the program hash table. Called via
284 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
285 * device driver function to implement OO deriviation with additional
286 * types not understood by this function.
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200287 *
Michal Krol2861e732004-03-29 11:09:34 +0000288 * \param ctx context
289 * \param id program id/number
290 * \param target program target/type
291 * \return pointer to new program object
292 */
Brian Paul122629f2006-07-20 16:49:57 +0000293struct gl_program *
Michal Krol2861e732004-03-29 11:09:34 +0000294_mesa_new_program(GLcontext *ctx, GLenum target, GLuint id)
295{
Brian Paula3e86d42008-05-16 09:56:11 -0600296 struct gl_program *prog;
Michal Krol2861e732004-03-29 11:09:34 +0000297 switch (target) {
298 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
Brian Paulc5af2ed2009-04-18 10:08:54 -0600299 case GL_VERTEX_STATE_PROGRAM_NV:
Brian Paula3e86d42008-05-16 09:56:11 -0600300 prog = _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program),
Brian Paul122629f2006-07-20 16:49:57 +0000301 target, id );
Brian Paula3e86d42008-05-16 09:56:11 -0600302 break;
Michal Krol2861e732004-03-29 11:09:34 +0000303 case GL_FRAGMENT_PROGRAM_NV:
304 case GL_FRAGMENT_PROGRAM_ARB:
Brian Paula3e86d42008-05-16 09:56:11 -0600305 prog =_mesa_init_fragment_program(ctx,
Brian Paul122629f2006-07-20 16:49:57 +0000306 CALLOC_STRUCT(gl_fragment_program),
307 target, id );
Brian Paula3e86d42008-05-16 09:56:11 -0600308 break;
Michal Krol2861e732004-03-29 11:09:34 +0000309 default:
310 _mesa_problem(ctx, "bad target in _mesa_new_program");
Brian Paula3e86d42008-05-16 09:56:11 -0600311 prog = NULL;
Michal Krol2861e732004-03-29 11:09:34 +0000312 }
Brian Paula3e86d42008-05-16 09:56:11 -0600313 return prog;
Michal Krol2861e732004-03-29 11:09:34 +0000314}
315
316
317/**
318 * Delete a program and remove it from the hash table, ignoring the
319 * reference count.
320 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
321 * by a device driver function.
322 */
323void
Brian Paul122629f2006-07-20 16:49:57 +0000324_mesa_delete_program(GLcontext *ctx, struct gl_program *prog)
Michal Krol2861e732004-03-29 11:09:34 +0000325{
Brian Paula6c423d2004-08-25 15:59:48 +0000326 (void) ctx;
Brian Paul57e222d2008-05-14 12:10:45 -0600327 ASSERT(prog);
328 ASSERT(prog->RefCount==0);
Michal Krol2861e732004-03-29 11:09:34 +0000329
Brian Paul308b85f2006-11-23 15:58:30 +0000330 if (prog == &_mesa_DummyProgram)
331 return;
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200332
Michal Krol2861e732004-03-29 11:09:34 +0000333 if (prog->String)
334 _mesa_free(prog->String);
Brian Paulde997602005-11-12 17:53:14 +0000335
Brian Paul19ad9cf2008-05-14 12:39:41 -0600336 _mesa_free_instructions(prog->Instructions, prog->NumInstructions);
Brian Paulde997602005-11-12 17:53:14 +0000337
Brian Paul65a51c02006-05-24 03:30:31 +0000338 if (prog->Parameters) {
Brian Paulde997602005-11-12 17:53:14 +0000339 _mesa_free_parameter_list(prog->Parameters);
Brian Paul65a51c02006-05-24 03:30:31 +0000340 }
Brianfe1d01c2006-12-13 14:54:47 -0700341 if (prog->Varying) {
342 _mesa_free_parameter_list(prog->Varying);
343 }
Brian3493e862007-03-24 16:18:13 -0600344 if (prog->Attributes) {
345 _mesa_free_parameter_list(prog->Attributes);
346 }
Brianfe1d01c2006-12-13 14:54:47 -0700347
Brian Paul58d080b2006-08-25 19:46:31 +0000348 /* XXX this is a little ugly */
349 if (prog->Target == GL_VERTEX_PROGRAM_ARB) {
350 struct gl_vertex_program *vprog = (struct gl_vertex_program *) prog;
351 if (vprog->TnlData)
352 _mesa_free(vprog->TnlData);
353 }
354
Michal Krol2861e732004-03-29 11:09:34 +0000355 _mesa_free(prog);
356}
357
358
Brian Paul4d12a052006-08-23 23:10:14 +0000359/**
360 * Return the gl_program object for a given ID.
361 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
362 * casts elsewhere.
363 */
364struct gl_program *
365_mesa_lookup_program(GLcontext *ctx, GLuint id)
366{
367 if (id)
368 return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id);
369 else
370 return NULL;
371}
372
Michal Krol2861e732004-03-29 11:09:34 +0000373
Brian Paul3b9b8de2006-08-24 21:57:36 +0000374/**
Briandf43fb62008-05-06 23:08:51 -0600375 * Reference counting for vertex/fragment programs
376 */
377void
378_mesa_reference_program(GLcontext *ctx,
379 struct gl_program **ptr,
380 struct gl_program *prog)
381{
382 assert(ptr);
383 if (*ptr && prog) {
384 /* sanity check */
Brian Paul4bc39c52008-09-26 07:40:45 -0600385 if ((*ptr)->Target == GL_VERTEX_PROGRAM_ARB)
386 ASSERT(prog->Target == GL_VERTEX_PROGRAM_ARB);
387 else if ((*ptr)->Target == GL_FRAGMENT_PROGRAM_ARB)
388 ASSERT(prog->Target == GL_FRAGMENT_PROGRAM_ARB ||
389 prog->Target == GL_FRAGMENT_PROGRAM_NV);
Briandf43fb62008-05-06 23:08:51 -0600390 }
391 if (*ptr == prog) {
392 return; /* no change */
393 }
394 if (*ptr) {
395 GLboolean deleteFlag;
396
397 /*_glthread_LOCK_MUTEX((*ptr)->Mutex);*/
Brian Paulb4e75d62008-05-08 10:59:31 -0600398#if 0
Brian Paula3e86d42008-05-16 09:56:11 -0600399 printf("Program %p ID=%u Target=%s Refcount-- to %d\n",
400 *ptr, (*ptr)->Id,
401 ((*ptr)->Target == GL_VERTEX_PROGRAM_ARB ? "VP" : "FP"),
402 (*ptr)->RefCount - 1);
Briandf43fb62008-05-06 23:08:51 -0600403#endif
404 ASSERT((*ptr)->RefCount > 0);
405 (*ptr)->RefCount--;
406
407 deleteFlag = ((*ptr)->RefCount == 0);
408 /*_glthread_UNLOCK_MUTEX((*ptr)->Mutex);*/
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200409
Briandf43fb62008-05-06 23:08:51 -0600410 if (deleteFlag) {
411 ASSERT(ctx);
412 ctx->Driver.DeleteProgram(ctx, *ptr);
413 }
414
415 *ptr = NULL;
416 }
417
418 assert(!*ptr);
419 if (prog) {
420 /*_glthread_LOCK_MUTEX(prog->Mutex);*/
421 prog->RefCount++;
Brian Paulb4e75d62008-05-08 10:59:31 -0600422#if 0
Brian Paula3e86d42008-05-16 09:56:11 -0600423 printf("Program %p ID=%u Target=%s Refcount++ to %d\n",
424 prog, prog->Id,
425 (prog->Target == GL_VERTEX_PROGRAM_ARB ? "VP" : "FP"),
426 prog->RefCount);
Briandf43fb62008-05-06 23:08:51 -0600427#endif
428 /*_glthread_UNLOCK_MUTEX(prog->Mutex);*/
429 }
430
431 *ptr = prog;
432}
433
434
435/**
Brianb2a3a852006-12-14 13:56:58 -0700436 * Return a copy of a program.
437 * XXX Problem here if the program object is actually OO-derivation
438 * made by a device driver.
439 */
440struct gl_program *
441_mesa_clone_program(GLcontext *ctx, const struct gl_program *prog)
442{
443 struct gl_program *clone;
444
Brian5b6858c2007-07-24 09:56:44 -0600445 clone = ctx->Driver.NewProgram(ctx, prog->Target, prog->Id);
Brianb2a3a852006-12-14 13:56:58 -0700446 if (!clone)
447 return NULL;
448
449 assert(clone->Target == prog->Target);
Briandf43fb62008-05-06 23:08:51 -0600450 assert(clone->RefCount == 1);
451
Brianb2a3a852006-12-14 13:56:58 -0700452 clone->String = (GLubyte *) _mesa_strdup((char *) prog->String);
Brianb2a3a852006-12-14 13:56:58 -0700453 clone->Format = prog->Format;
454 clone->Instructions = _mesa_alloc_instructions(prog->NumInstructions);
455 if (!clone->Instructions) {
Brian Paul57e222d2008-05-14 12:10:45 -0600456 _mesa_reference_program(ctx, &clone, NULL);
Brianb2a3a852006-12-14 13:56:58 -0700457 return NULL;
458 }
Brian12229f12007-03-22 09:11:26 -0600459 _mesa_copy_instructions(clone->Instructions, prog->Instructions,
460 prog->NumInstructions);
Brianb2a3a852006-12-14 13:56:58 -0700461 clone->InputsRead = prog->InputsRead;
462 clone->OutputsWritten = prog->OutputsWritten;
Brian Paul0c78c762008-05-18 15:46:26 -0600463 clone->SamplersUsed = prog->SamplersUsed;
Nicolai Haehnle82635aa2008-07-05 18:03:44 +0200464 clone->ShadowSamplers = prog->ShadowSamplers;
Brianc9db2232007-01-04 17:22:19 -0700465 memcpy(clone->TexturesUsed, prog->TexturesUsed, sizeof(prog->TexturesUsed));
466
Brian2e76f0a2006-12-19 09:52:07 -0700467 if (prog->Parameters)
468 clone->Parameters = _mesa_clone_parameter_list(prog->Parameters);
Brianb2a3a852006-12-14 13:56:58 -0700469 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
Brian2e76f0a2006-12-19 09:52:07 -0700470 if (prog->Varying)
471 clone->Varying = _mesa_clone_parameter_list(prog->Varying);
Brian3209c3e2007-01-09 17:49:24 -0700472 if (prog->Attributes)
473 clone->Attributes = _mesa_clone_parameter_list(prog->Attributes);
Brianb2a3a852006-12-14 13:56:58 -0700474 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
475 clone->NumInstructions = prog->NumInstructions;
476 clone->NumTemporaries = prog->NumTemporaries;
477 clone->NumParameters = prog->NumParameters;
478 clone->NumAttributes = prog->NumAttributes;
479 clone->NumAddressRegs = prog->NumAddressRegs;
480 clone->NumNativeInstructions = prog->NumNativeInstructions;
481 clone->NumNativeTemporaries = prog->NumNativeTemporaries;
482 clone->NumNativeParameters = prog->NumNativeParameters;
483 clone->NumNativeAttributes = prog->NumNativeAttributes;
484 clone->NumNativeAddressRegs = prog->NumNativeAddressRegs;
Brian21f99792007-01-09 11:00:21 -0700485 clone->NumAluInstructions = prog->NumAluInstructions;
486 clone->NumTexInstructions = prog->NumTexInstructions;
487 clone->NumTexIndirections = prog->NumTexIndirections;
488 clone->NumNativeAluInstructions = prog->NumNativeAluInstructions;
489 clone->NumNativeTexInstructions = prog->NumNativeTexInstructions;
490 clone->NumNativeTexIndirections = prog->NumNativeTexIndirections;
Brianb2a3a852006-12-14 13:56:58 -0700491
492 switch (prog->Target) {
493 case GL_VERTEX_PROGRAM_ARB:
494 {
495 const struct gl_vertex_program *vp
496 = (const struct gl_vertex_program *) prog;
497 struct gl_vertex_program *vpc = (struct gl_vertex_program *) clone;
498 vpc->IsPositionInvariant = vp->IsPositionInvariant;
499 }
500 break;
501 case GL_FRAGMENT_PROGRAM_ARB:
502 {
503 const struct gl_fragment_program *fp
504 = (const struct gl_fragment_program *) prog;
505 struct gl_fragment_program *fpc = (struct gl_fragment_program *) clone;
Brianb2a3a852006-12-14 13:56:58 -0700506 fpc->FogOption = fp->FogOption;
507 fpc->UsesKill = fp->UsesKill;
508 }
509 break;
510 default:
511 _mesa_problem(NULL, "Unexpected target in _mesa_clone_program");
512 }
513
514 return clone;
515}
516
517
Brian Paul19ad9cf2008-05-14 12:39:41 -0600518/**
519 * Insert 'count' NOP instructions at 'start' in the given program.
520 * Adjust branch targets accordingly.
521 */
522GLboolean
523_mesa_insert_instructions(struct gl_program *prog, GLuint start, GLuint count)
524{
525 const GLuint origLen = prog->NumInstructions;
526 const GLuint newLen = origLen + count;
527 struct prog_instruction *newInst;
528 GLuint i;
529
530 /* adjust branches */
531 for (i = 0; i < prog->NumInstructions; i++) {
532 struct prog_instruction *inst = prog->Instructions + i;
533 if (inst->BranchTarget > 0) {
José Fonseca457d7212008-06-24 11:34:46 +0900534 if ((GLuint)inst->BranchTarget >= start) {
Brian Paul19ad9cf2008-05-14 12:39:41 -0600535 inst->BranchTarget += count;
536 }
537 }
538 }
539
540 /* Alloc storage for new instructions */
541 newInst = _mesa_alloc_instructions(newLen);
542 if (!newInst) {
543 return GL_FALSE;
544 }
545
546 /* Copy 'start' instructions into new instruction buffer */
547 _mesa_copy_instructions(newInst, prog->Instructions, start);
548
549 /* init the new instructions */
550 _mesa_init_instructions(newInst + start, count);
551
552 /* Copy the remaining/tail instructions to new inst buffer */
553 _mesa_copy_instructions(newInst + start + count,
554 prog->Instructions + start,
555 origLen - start);
556
557 /* free old instructions */
558 _mesa_free_instructions(prog->Instructions, origLen);
559
560 /* install new instructions */
561 prog->Instructions = newInst;
562 prog->NumInstructions = newLen;
563
564 return GL_TRUE;
565}
566
Brianb2a3a852006-12-14 13:56:58 -0700567/**
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200568 * Delete 'count' instructions at 'start' in the given program.
569 * Adjust branch targets accordingly.
570 */
571GLboolean
572_mesa_delete_instructions(struct gl_program *prog, GLuint start, GLuint count)
573{
574 const GLuint origLen = prog->NumInstructions;
575 const GLuint newLen = origLen - count;
576 struct prog_instruction *newInst;
577 GLuint i;
578
579 /* adjust branches */
580 for (i = 0; i < prog->NumInstructions; i++) {
581 struct prog_instruction *inst = prog->Instructions + i;
582 if (inst->BranchTarget > 0) {
Brian Paul5273a5f2009-01-07 12:31:14 -0700583 if (inst->BranchTarget > start) {
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200584 inst->BranchTarget -= count;
585 }
586 }
587 }
588
589 /* Alloc storage for new instructions */
590 newInst = _mesa_alloc_instructions(newLen);
591 if (!newInst) {
592 return GL_FALSE;
593 }
594
595 /* Copy 'start' instructions into new instruction buffer */
596 _mesa_copy_instructions(newInst, prog->Instructions, start);
597
598 /* Copy the remaining/tail instructions to new inst buffer */
599 _mesa_copy_instructions(newInst + start,
600 prog->Instructions + start + count,
601 newLen - start);
602
603 /* free old instructions */
604 _mesa_free_instructions(prog->Instructions, origLen);
605
606 /* install new instructions */
607 prog->Instructions = newInst;
608 prog->NumInstructions = newLen;
609
610 return GL_TRUE;
611}
612
613
614/**
Brian Paul6ca948a2008-05-14 12:53:03 -0600615 * Search instructions for registers that match (oldFile, oldIndex),
616 * replacing them with (newFile, newIndex).
617 */
618static void
619replace_registers(struct prog_instruction *inst, GLuint numInst,
620 GLuint oldFile, GLuint oldIndex,
621 GLuint newFile, GLuint newIndex)
622{
623 GLuint i, j;
624 for (i = 0; i < numInst; i++) {
Brian Paul0c78c762008-05-18 15:46:26 -0600625 /* src regs */
Brian Paul6ca948a2008-05-14 12:53:03 -0600626 for (j = 0; j < _mesa_num_inst_src_regs(inst->Opcode); j++) {
627 if (inst[i].SrcReg[j].File == oldFile &&
628 inst[i].SrcReg[j].Index == oldIndex) {
629 inst[i].SrcReg[j].File = newFile;
630 inst[i].SrcReg[j].Index = newIndex;
631 }
632 }
Brian Paul0c78c762008-05-18 15:46:26 -0600633 /* dst reg */
634 if (inst[i].DstReg.File == oldFile && inst[i].DstReg.Index == oldIndex) {
635 inst[i].DstReg.File = newFile;
636 inst[i].DstReg.Index = newIndex;
637 }
Brian Paul6ca948a2008-05-14 12:53:03 -0600638 }
639}
640
641
642/**
643 * Search instructions for references to program parameters. When found,
644 * increment the parameter index by 'offset'.
645 * Used when combining programs.
646 */
647static void
648adjust_param_indexes(struct prog_instruction *inst, GLuint numInst,
649 GLuint offset)
650{
651 GLuint i, j;
652 for (i = 0; i < numInst; i++) {
653 for (j = 0; j < _mesa_num_inst_src_regs(inst->Opcode); j++) {
654 GLuint f = inst[i].SrcReg[j].File;
655 if (f == PROGRAM_CONSTANT ||
656 f == PROGRAM_UNIFORM ||
657 f == PROGRAM_STATE_VAR) {
658 inst[i].SrcReg[j].Index += offset;
659 }
660 }
661 }
662}
663
664
665/**
666 * Combine two programs into one. Fix instructions so the outputs of
667 * the first program go to the inputs of the second program.
668 */
669struct gl_program *
670_mesa_combine_programs(GLcontext *ctx,
Brian Paul0c78c762008-05-18 15:46:26 -0600671 const struct gl_program *progA,
672 const struct gl_program *progB)
Brian Paul6ca948a2008-05-14 12:53:03 -0600673{
674 struct prog_instruction *newInst;
675 struct gl_program *newProg;
676 const GLuint lenA = progA->NumInstructions - 1; /* omit END instr */
677 const GLuint lenB = progB->NumInstructions;
678 const GLuint numParamsA = _mesa_num_parameters(progA->Parameters);
679 const GLuint newLength = lenA + lenB;
Brian Paul0c78c762008-05-18 15:46:26 -0600680 GLbitfield inputsB;
Brian Paul6ca948a2008-05-14 12:53:03 -0600681 GLuint i;
682
683 ASSERT(progA->Target == progB->Target);
684
685 newInst = _mesa_alloc_instructions(newLength);
686 if (!newInst)
687 return GL_FALSE;
688
689 _mesa_copy_instructions(newInst, progA->Instructions, lenA);
690 _mesa_copy_instructions(newInst + lenA, progB->Instructions, lenB);
691
692 /* adjust branch / instruction addresses for B's instructions */
693 for (i = 0; i < lenB; i++) {
694 newInst[lenA + i].BranchTarget += lenA;
695 }
696
697 newProg = ctx->Driver.NewProgram(ctx, progA->Target, 0);
698 newProg->Instructions = newInst;
699 newProg->NumInstructions = newLength;
700
701 if (newProg->Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul0c78c762008-05-18 15:46:26 -0600702 struct gl_fragment_program *fprogA, *fprogB, *newFprog;
Brian Paul5c4bd762008-10-08 14:02:24 -0600703 GLbitfield progB_inputsRead = progB->InputsRead;
704 GLint progB_colorFile, progB_colorIndex;
705
Brian Paul0c78c762008-05-18 15:46:26 -0600706 fprogA = (struct gl_fragment_program *) progA;
707 fprogB = (struct gl_fragment_program *) progB;
708 newFprog = (struct gl_fragment_program *) newProg;
709
710 newFprog->UsesKill = fprogA->UsesKill || fprogB->UsesKill;
711
Brian Paul5c4bd762008-10-08 14:02:24 -0600712 /* We'll do a search and replace for instances
713 * of progB_colorFile/progB_colorIndex below...
714 */
715 progB_colorFile = PROGRAM_INPUT;
716 progB_colorIndex = FRAG_ATTRIB_COL0;
717
718 /*
719 * The fragment program may get color from a state var rather than
720 * a fragment input (vertex output) if it's constant.
721 * See the texenvprogram.c code.
722 * So, search the program's parameter list now to see if the program
723 * gets color from a state var instead of a conventional fragment
724 * input register.
725 */
726 for (i = 0; i < progB->Parameters->NumParameters; i++) {
727 struct gl_program_parameter *p = &progB->Parameters->Parameters[i];
728 if (p->Type == PROGRAM_STATE_VAR &&
729 p->StateIndexes[0] == STATE_INTERNAL &&
730 p->StateIndexes[1] == STATE_CURRENT_ATTRIB &&
731 p->StateIndexes[2] == VERT_ATTRIB_COLOR0) {
732 progB_inputsRead |= FRAG_BIT_COL0;
733 progB_colorFile = PROGRAM_STATE_VAR;
734 progB_colorIndex = i;
735 break;
736 }
737 }
738
Brian Paul0c78c762008-05-18 15:46:26 -0600739 /* Connect color outputs of fprogA to color inputs of fprogB, via a
740 * new temporary register.
741 */
Brian Paul8d475822009-02-28 11:49:46 -0700742 if ((progA->OutputsWritten & (1 << FRAG_RESULT_COLOR)) &&
Brian Paul5c4bd762008-10-08 14:02:24 -0600743 (progB_inputsRead & FRAG_BIT_COL0)) {
Brian Paul0c78c762008-05-18 15:46:26 -0600744 GLint tempReg = _mesa_find_free_register(newProg, PROGRAM_TEMPORARY);
Brian Paule469d782008-05-19 16:03:43 -0600745 if (tempReg < 0) {
Brian Paul0c78c762008-05-18 15:46:26 -0600746 _mesa_problem(ctx, "No free temp regs found in "
747 "_mesa_combine_programs(), using 31");
748 tempReg = 31;
749 }
750 /* replace writes to result.color[0] with tempReg */
751 replace_registers(newInst, lenA,
Brian Paul8d475822009-02-28 11:49:46 -0700752 PROGRAM_OUTPUT, FRAG_RESULT_COLOR,
Brian Paul0c78c762008-05-18 15:46:26 -0600753 PROGRAM_TEMPORARY, tempReg);
Brian Paul5c4bd762008-10-08 14:02:24 -0600754 /* replace reads from the input color with tempReg */
Brian Paul6ca948a2008-05-14 12:53:03 -0600755 replace_registers(newInst + lenA, lenB,
Brian Paul5c4bd762008-10-08 14:02:24 -0600756 progB_colorFile, progB_colorIndex, /* search for */
757 PROGRAM_TEMPORARY, tempReg /* replace with */ );
Brian Paul6ca948a2008-05-14 12:53:03 -0600758 }
759
Brian Paul5c4bd762008-10-08 14:02:24 -0600760 /* compute combined program's InputsRead */
761 inputsB = progB_inputsRead;
Brian Paul8d475822009-02-28 11:49:46 -0700762 if (progA->OutputsWritten & (1 << FRAG_RESULT_COLOR)) {
Brian Paul0c78c762008-05-18 15:46:26 -0600763 inputsB &= ~(1 << FRAG_ATTRIB_COL0);
764 }
765 newProg->InputsRead = progA->InputsRead | inputsB;
Brian Paul6ca948a2008-05-14 12:53:03 -0600766 newProg->OutputsWritten = progB->OutputsWritten;
Brian Paul0c78c762008-05-18 15:46:26 -0600767 newProg->SamplersUsed = progA->SamplersUsed | progB->SamplersUsed;
Brian Paul6ca948a2008-05-14 12:53:03 -0600768 }
769 else {
770 /* vertex program */
771 assert(0); /* XXX todo */
772 }
773
774 /*
775 * Merge parameters (uniforms, constants, etc)
776 */
777 newProg->Parameters = _mesa_combine_parameter_lists(progA->Parameters,
778 progB->Parameters);
779
780 adjust_param_indexes(newInst + lenA, lenB, numParamsA);
781
782
783 return newProg;
784}
785
786
787
788
789/**
790 * Scan the given program to find a free register of the given type.
791 * \param regFile - PROGRAM_INPUT, PROGRAM_OUTPUT or PROGRAM_TEMPORARY
792 */
793GLint
794_mesa_find_free_register(const struct gl_program *prog, GLuint regFile)
795{
796 GLboolean used[MAX_PROGRAM_TEMPS];
797 GLuint i, k;
798
799 assert(regFile == PROGRAM_INPUT ||
800 regFile == PROGRAM_OUTPUT ||
801 regFile == PROGRAM_TEMPORARY);
802
803 _mesa_memset(used, 0, sizeof(used));
804
805 for (i = 0; i < prog->NumInstructions; i++) {
806 const struct prog_instruction *inst = prog->Instructions + i;
807 const GLuint n = _mesa_num_inst_src_regs(inst->Opcode);
808
809 for (k = 0; k < n; k++) {
810 if (inst->SrcReg[k].File == regFile) {
811 used[inst->SrcReg[k].Index] = GL_TRUE;
812 }
813 }
814 }
815
816 for (i = 0; i < MAX_PROGRAM_TEMPS; i++) {
817 if (!used[i])
818 return i;
819 }
820
821 return -1;
822}