blob: 2932c9ebc50232706a5df0d500832738f3a6dfeb [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
Brian Paulbbd28712008-09-18 12:26:54 -060032#include "main/glheader.h"
33#include "main/context.h"
34#include "main/hash.h"
Brian0560d812006-12-14 15:01:28 -070035#include "program.h"
Keith Whitwell32ef6e72008-09-20 08:26:11 -070036#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
56 ctx->Program.ErrorPos = -1;
57 ctx->Program.ErrorString = _mesa_strdup("");
58
59#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
60 ctx->VertexProgram.Enabled = GL_FALSE;
61 ctx->VertexProgram.PointSizeEnabled = GL_FALSE;
62 ctx->VertexProgram.TwoSideEnabled = GL_FALSE;
Briandf43fb62008-05-06 23:08:51 -060063 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
64 ctx->Shared->DefaultVertexProgram);
Michal Krol2861e732004-03-29 11:09:34 +000065 assert(ctx->VertexProgram.Current);
Michal Krol2861e732004-03-29 11:09:34 +000066 for (i = 0; i < MAX_NV_VERTEX_PROGRAM_PARAMS / 4; i++) {
67 ctx->VertexProgram.TrackMatrix[i] = GL_NONE;
68 ctx->VertexProgram.TrackMatrixTransform[i] = GL_IDENTITY_NV;
69 }
Keith Whitwell32ef6e72008-09-20 08:26:11 -070070 ctx->VertexProgram.Cache = _mesa_new_program_cache();
Michal Krol2861e732004-03-29 11:09:34 +000071#endif
72
73#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
74 ctx->FragmentProgram.Enabled = GL_FALSE;
Briandf43fb62008-05-06 23:08:51 -060075 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
76 ctx->Shared->DefaultFragmentProgram);
Michal Krol2861e732004-03-29 11:09:34 +000077 assert(ctx->FragmentProgram.Current);
Keith Whitwell32ef6e72008-09-20 08:26:11 -070078 ctx->FragmentProgram.Cache = _mesa_new_program_cache();
Michal Krol2861e732004-03-29 11:09:34 +000079#endif
Dave Airlie7f752fe2004-12-19 03:06:59 +000080
Keith Whitwell32ef6e72008-09-20 08:26:11 -070081
Brian Paul63d68302005-11-19 16:43:04 +000082 /* XXX probably move this stuff */
Dave Airlie7f752fe2004-12-19 03:06:59 +000083#if FEATURE_ATI_fragment_shader
84 ctx->ATIFragmentShader.Enabled = GL_FALSE;
Brian Paulb7eea9a2008-07-29 17:19:25 -060085 ctx->ATIFragmentShader.Current = ctx->Shared->DefaultFragmentShader;
Dave Airlie7f752fe2004-12-19 03:06:59 +000086 assert(ctx->ATIFragmentShader.Current);
Brian Paul63d68302005-11-19 16:43:04 +000087 ctx->ATIFragmentShader.Current->RefCount++;
Dave Airlie7f752fe2004-12-19 03:06:59 +000088#endif
Michal Krol2861e732004-03-29 11:09:34 +000089}
90
91
92/**
Brian Paul21841f02004-08-14 14:28:11 +000093 * Free a context's vertex/fragment program state
94 */
95void
96_mesa_free_program_data(GLcontext *ctx)
97{
Roland Scheidegger93da6732006-03-01 23:11:14 +000098#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
Briandf43fb62008-05-06 23:08:51 -060099 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current, NULL);
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700100 _mesa_delete_program_cache(ctx, ctx->VertexProgram.Cache);
Brian Paul21841f02004-08-14 14:28:11 +0000101#endif
Roland Scheidegger93da6732006-03-01 23:11:14 +0000102#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
Briandf43fb62008-05-06 23:08:51 -0600103 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current, NULL);
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700104 _mesa_delete_program_cache(ctx, ctx->FragmentProgram.Cache);
Brian Paul21841f02004-08-14 14:28:11 +0000105#endif
Brian Paul63d68302005-11-19 16:43:04 +0000106 /* XXX probably move this stuff */
Dave Airlie7f752fe2004-12-19 03:06:59 +0000107#if FEATURE_ATI_fragment_shader
108 if (ctx->ATIFragmentShader.Current) {
Brian Paul63d68302005-11-19 16:43:04 +0000109 ctx->ATIFragmentShader.Current->RefCount--;
110 if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
111 _mesa_free(ctx->ATIFragmentShader.Current);
112 }
Dave Airlie7f752fe2004-12-19 03:06:59 +0000113 }
114#endif
Brian Paul21841f02004-08-14 14:28:11 +0000115 _mesa_free((void *) ctx->Program.ErrorString);
116}
117
118
Brian4b654d42007-08-23 08:53:43 +0100119/**
120 * Update the default program objects in the given context to reference those
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200121 * specified in the shared state and release those referencing the old
Brian4b654d42007-08-23 08:53:43 +0100122 * shared state.
123 */
124void
125_mesa_update_default_objects_program(GLcontext *ctx)
126{
127#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
Brian Paul57e222d2008-05-14 12:10:45 -0600128 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
129 (struct gl_vertex_program *)
130 ctx->Shared->DefaultVertexProgram);
Brian4b654d42007-08-23 08:53:43 +0100131 assert(ctx->VertexProgram.Current);
Brian4b654d42007-08-23 08:53:43 +0100132#endif
133
134#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
Brian Paul57e222d2008-05-14 12:10:45 -0600135 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
136 (struct gl_fragment_program *)
137 ctx->Shared->DefaultFragmentProgram);
Brian4b654d42007-08-23 08:53:43 +0100138 assert(ctx->FragmentProgram.Current);
Brian4b654d42007-08-23 08:53:43 +0100139#endif
140
141 /* XXX probably move this stuff */
142#if FEATURE_ATI_fragment_shader
143 if (ctx->ATIFragmentShader.Current) {
144 ctx->ATIFragmentShader.Current->RefCount--;
145 if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
146 _mesa_free(ctx->ATIFragmentShader.Current);
147 }
148 }
149 ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader;
150 assert(ctx->ATIFragmentShader.Current);
151 ctx->ATIFragmentShader.Current->RefCount++;
152#endif
153}
Brian Paul21841f02004-08-14 14:28:11 +0000154
155
156/**
Michal Krol2861e732004-03-29 11:09:34 +0000157 * Set the vertex/fragment program error state (position and error string).
158 * This is generally called from within the parsers.
159 */
160void
161_mesa_set_program_error(GLcontext *ctx, GLint pos, const char *string)
162{
163 ctx->Program.ErrorPos = pos;
164 _mesa_free((void *) ctx->Program.ErrorString);
165 if (!string)
166 string = "";
167 ctx->Program.ErrorString = _mesa_strdup(string);
168}
169
170
171/**
172 * Find the line number and column for 'pos' within 'string'.
173 * Return a copy of the line which contains 'pos'. Free the line with
174 * _mesa_free().
175 * \param string the program string
176 * \param pos the position within the string
177 * \param line returns the line number corresponding to 'pos'.
178 * \param col returns the column number corresponding to 'pos'.
179 * \return copy of the line containing 'pos'.
180 */
181const GLubyte *
182_mesa_find_line_column(const GLubyte *string, const GLubyte *pos,
183 GLint *line, GLint *col)
184{
185 const GLubyte *lineStart = string;
186 const GLubyte *p = string;
187 GLubyte *s;
188 int len;
189
190 *line = 1;
191
192 while (p != pos) {
193 if (*p == (GLubyte) '\n') {
194 (*line)++;
195 lineStart = p + 1;
196 }
197 p++;
198 }
199
200 *col = (pos - lineStart) + 1;
201
202 /* return copy of this line */
203 while (*p != 0 && *p != '\n')
204 p++;
205 len = p - lineStart;
206 s = (GLubyte *) _mesa_malloc(len + 1);
207 _mesa_memcpy(s, lineStart, len);
208 s[len] = 0;
209
210 return s;
211}
212
213
Brian Paul765f1a12004-09-14 22:28:27 +0000214/**
215 * Initialize a new vertex/fragment program object.
216 */
Brian Paul122629f2006-07-20 16:49:57 +0000217static struct gl_program *
218_mesa_init_program_struct( GLcontext *ctx, struct gl_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000219 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000220{
Brian Paula6c423d2004-08-25 15:59:48 +0000221 (void) ctx;
Michal Krol2861e732004-03-29 11:09:34 +0000222 if (prog) {
Brian Paul0c78c762008-05-18 15:46:26 -0600223 GLuint i;
224 _mesa_bzero(prog, sizeof(*prog));
Michal Krol2861e732004-03-29 11:09:34 +0000225 prog->Id = id;
226 prog->Target = target;
227 prog->Resident = GL_TRUE;
228 prog->RefCount = 1;
Brian Paul308b85f2006-11-23 15:58:30 +0000229 prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
Brian Paul0c78c762008-05-18 15:46:26 -0600230
231 /* default mapping from samplers to texture units */
232 for (i = 0; i < MAX_SAMPLERS; i++)
233 prog->SamplerUnits[i] = i;
Michal Krol2861e732004-03-29 11:09:34 +0000234 }
235
236 return prog;
237}
238
Brian Paul765f1a12004-09-14 22:28:27 +0000239
240/**
241 * Initialize a new fragment program object.
242 */
Brian Paul122629f2006-07-20 16:49:57 +0000243struct gl_program *
244_mesa_init_fragment_program( GLcontext *ctx, struct gl_fragment_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000245 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000246{
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200247 if (prog)
Michal Krol2861e732004-03-29 11:09:34 +0000248 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
249 else
250 return NULL;
251}
252
Brian Paul765f1a12004-09-14 22:28:27 +0000253
254/**
255 * Initialize a new vertex program object.
256 */
Brian Paul122629f2006-07-20 16:49:57 +0000257struct gl_program *
258_mesa_init_vertex_program( GLcontext *ctx, struct gl_vertex_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000259 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000260{
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200261 if (prog)
Michal Krol2861e732004-03-29 11:09:34 +0000262 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
263 else
264 return NULL;
265}
266
267
268/**
269 * Allocate and initialize a new fragment/vertex program object but
270 * don't put it into the program hash table. Called via
271 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
272 * device driver function to implement OO deriviation with additional
273 * types not understood by this function.
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200274 *
Michal Krol2861e732004-03-29 11:09:34 +0000275 * \param ctx context
276 * \param id program id/number
277 * \param target program target/type
278 * \return pointer to new program object
279 */
Brian Paul122629f2006-07-20 16:49:57 +0000280struct gl_program *
Michal Krol2861e732004-03-29 11:09:34 +0000281_mesa_new_program(GLcontext *ctx, GLenum target, GLuint id)
282{
Brian Paula3e86d42008-05-16 09:56:11 -0600283 struct gl_program *prog;
Michal Krol2861e732004-03-29 11:09:34 +0000284 switch (target) {
285 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
Brian Paula3e86d42008-05-16 09:56:11 -0600286 prog = _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program),
Brian Paul122629f2006-07-20 16:49:57 +0000287 target, id );
Brian Paula3e86d42008-05-16 09:56:11 -0600288 break;
Michal Krol2861e732004-03-29 11:09:34 +0000289 case GL_FRAGMENT_PROGRAM_NV:
290 case GL_FRAGMENT_PROGRAM_ARB:
Brian Paula3e86d42008-05-16 09:56:11 -0600291 prog =_mesa_init_fragment_program(ctx,
Brian Paul122629f2006-07-20 16:49:57 +0000292 CALLOC_STRUCT(gl_fragment_program),
293 target, id );
Brian Paula3e86d42008-05-16 09:56:11 -0600294 break;
Michal Krol2861e732004-03-29 11:09:34 +0000295 default:
296 _mesa_problem(ctx, "bad target in _mesa_new_program");
Brian Paula3e86d42008-05-16 09:56:11 -0600297 prog = NULL;
Michal Krol2861e732004-03-29 11:09:34 +0000298 }
Brian Paula3e86d42008-05-16 09:56:11 -0600299 return prog;
Michal Krol2861e732004-03-29 11:09:34 +0000300}
301
302
303/**
304 * Delete a program and remove it from the hash table, ignoring the
305 * reference count.
306 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
307 * by a device driver function.
308 */
309void
Brian Paul122629f2006-07-20 16:49:57 +0000310_mesa_delete_program(GLcontext *ctx, struct gl_program *prog)
Michal Krol2861e732004-03-29 11:09:34 +0000311{
Brian Paula6c423d2004-08-25 15:59:48 +0000312 (void) ctx;
Brian Paul57e222d2008-05-14 12:10:45 -0600313 ASSERT(prog);
314 ASSERT(prog->RefCount==0);
Michal Krol2861e732004-03-29 11:09:34 +0000315
Brian Paul308b85f2006-11-23 15:58:30 +0000316 if (prog == &_mesa_DummyProgram)
317 return;
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200318
Michal Krol2861e732004-03-29 11:09:34 +0000319 if (prog->String)
320 _mesa_free(prog->String);
Brian Paulde997602005-11-12 17:53:14 +0000321
Brian Paul19ad9cf2008-05-14 12:39:41 -0600322 _mesa_free_instructions(prog->Instructions, prog->NumInstructions);
Brian Paulde997602005-11-12 17:53:14 +0000323
Brian Paul65a51c02006-05-24 03:30:31 +0000324 if (prog->Parameters) {
Brian Paulde997602005-11-12 17:53:14 +0000325 _mesa_free_parameter_list(prog->Parameters);
Brian Paul65a51c02006-05-24 03:30:31 +0000326 }
Brianfe1d01c2006-12-13 14:54:47 -0700327 if (prog->Varying) {
328 _mesa_free_parameter_list(prog->Varying);
329 }
Brian3493e862007-03-24 16:18:13 -0600330 if (prog->Attributes) {
331 _mesa_free_parameter_list(prog->Attributes);
332 }
Brianfe1d01c2006-12-13 14:54:47 -0700333
Brian Paul58d080b2006-08-25 19:46:31 +0000334 /* XXX this is a little ugly */
335 if (prog->Target == GL_VERTEX_PROGRAM_ARB) {
336 struct gl_vertex_program *vprog = (struct gl_vertex_program *) prog;
337 if (vprog->TnlData)
338 _mesa_free(vprog->TnlData);
339 }
340
Michal Krol2861e732004-03-29 11:09:34 +0000341 _mesa_free(prog);
342}
343
344
Brian Paul4d12a052006-08-23 23:10:14 +0000345/**
346 * Return the gl_program object for a given ID.
347 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
348 * casts elsewhere.
349 */
350struct gl_program *
351_mesa_lookup_program(GLcontext *ctx, GLuint id)
352{
353 if (id)
354 return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id);
355 else
356 return NULL;
357}
358
Michal Krol2861e732004-03-29 11:09:34 +0000359
Brian Paul3b9b8de2006-08-24 21:57:36 +0000360/**
Briandf43fb62008-05-06 23:08:51 -0600361 * Reference counting for vertex/fragment programs
362 */
363void
364_mesa_reference_program(GLcontext *ctx,
365 struct gl_program **ptr,
366 struct gl_program *prog)
367{
368 assert(ptr);
369 if (*ptr && prog) {
370 /* sanity check */
371 ASSERT((*ptr)->Target == prog->Target);
372 }
373 if (*ptr == prog) {
374 return; /* no change */
375 }
376 if (*ptr) {
377 GLboolean deleteFlag;
378
379 /*_glthread_LOCK_MUTEX((*ptr)->Mutex);*/
Brian Paulb4e75d62008-05-08 10:59:31 -0600380#if 0
Brian Paula3e86d42008-05-16 09:56:11 -0600381 printf("Program %p ID=%u Target=%s Refcount-- to %d\n",
382 *ptr, (*ptr)->Id,
383 ((*ptr)->Target == GL_VERTEX_PROGRAM_ARB ? "VP" : "FP"),
384 (*ptr)->RefCount - 1);
Briandf43fb62008-05-06 23:08:51 -0600385#endif
386 ASSERT((*ptr)->RefCount > 0);
387 (*ptr)->RefCount--;
388
389 deleteFlag = ((*ptr)->RefCount == 0);
390 /*_glthread_UNLOCK_MUTEX((*ptr)->Mutex);*/
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200391
Briandf43fb62008-05-06 23:08:51 -0600392 if (deleteFlag) {
393 ASSERT(ctx);
394 ctx->Driver.DeleteProgram(ctx, *ptr);
395 }
396
397 *ptr = NULL;
398 }
399
400 assert(!*ptr);
401 if (prog) {
402 /*_glthread_LOCK_MUTEX(prog->Mutex);*/
403 prog->RefCount++;
Brian Paulb4e75d62008-05-08 10:59:31 -0600404#if 0
Brian Paula3e86d42008-05-16 09:56:11 -0600405 printf("Program %p ID=%u Target=%s Refcount++ to %d\n",
406 prog, prog->Id,
407 (prog->Target == GL_VERTEX_PROGRAM_ARB ? "VP" : "FP"),
408 prog->RefCount);
Briandf43fb62008-05-06 23:08:51 -0600409#endif
410 /*_glthread_UNLOCK_MUTEX(prog->Mutex);*/
411 }
412
413 *ptr = prog;
414}
415
416
417/**
Brianb2a3a852006-12-14 13:56:58 -0700418 * Return a copy of a program.
419 * XXX Problem here if the program object is actually OO-derivation
420 * made by a device driver.
421 */
422struct gl_program *
423_mesa_clone_program(GLcontext *ctx, const struct gl_program *prog)
424{
425 struct gl_program *clone;
426
Brian5b6858c2007-07-24 09:56:44 -0600427 clone = ctx->Driver.NewProgram(ctx, prog->Target, prog->Id);
Brianb2a3a852006-12-14 13:56:58 -0700428 if (!clone)
429 return NULL;
430
431 assert(clone->Target == prog->Target);
Briandf43fb62008-05-06 23:08:51 -0600432 assert(clone->RefCount == 1);
433
Brianb2a3a852006-12-14 13:56:58 -0700434 clone->String = (GLubyte *) _mesa_strdup((char *) prog->String);
Brianb2a3a852006-12-14 13:56:58 -0700435 clone->Format = prog->Format;
436 clone->Instructions = _mesa_alloc_instructions(prog->NumInstructions);
437 if (!clone->Instructions) {
Brian Paul57e222d2008-05-14 12:10:45 -0600438 _mesa_reference_program(ctx, &clone, NULL);
Brianb2a3a852006-12-14 13:56:58 -0700439 return NULL;
440 }
Brian12229f12007-03-22 09:11:26 -0600441 _mesa_copy_instructions(clone->Instructions, prog->Instructions,
442 prog->NumInstructions);
Brianb2a3a852006-12-14 13:56:58 -0700443 clone->InputsRead = prog->InputsRead;
444 clone->OutputsWritten = prog->OutputsWritten;
Brian Paul0c78c762008-05-18 15:46:26 -0600445 clone->SamplersUsed = prog->SamplersUsed;
Nicolai Haehnle82635aa2008-07-05 18:03:44 +0200446 clone->ShadowSamplers = prog->ShadowSamplers;
Brianc9db2232007-01-04 17:22:19 -0700447 memcpy(clone->TexturesUsed, prog->TexturesUsed, sizeof(prog->TexturesUsed));
448
Brian2e76f0a2006-12-19 09:52:07 -0700449 if (prog->Parameters)
450 clone->Parameters = _mesa_clone_parameter_list(prog->Parameters);
Brianb2a3a852006-12-14 13:56:58 -0700451 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
Brian2e76f0a2006-12-19 09:52:07 -0700452 if (prog->Varying)
453 clone->Varying = _mesa_clone_parameter_list(prog->Varying);
Brian3209c3e2007-01-09 17:49:24 -0700454 if (prog->Attributes)
455 clone->Attributes = _mesa_clone_parameter_list(prog->Attributes);
Brianb2a3a852006-12-14 13:56:58 -0700456 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
457 clone->NumInstructions = prog->NumInstructions;
458 clone->NumTemporaries = prog->NumTemporaries;
459 clone->NumParameters = prog->NumParameters;
460 clone->NumAttributes = prog->NumAttributes;
461 clone->NumAddressRegs = prog->NumAddressRegs;
462 clone->NumNativeInstructions = prog->NumNativeInstructions;
463 clone->NumNativeTemporaries = prog->NumNativeTemporaries;
464 clone->NumNativeParameters = prog->NumNativeParameters;
465 clone->NumNativeAttributes = prog->NumNativeAttributes;
466 clone->NumNativeAddressRegs = prog->NumNativeAddressRegs;
Brian21f99792007-01-09 11:00:21 -0700467 clone->NumAluInstructions = prog->NumAluInstructions;
468 clone->NumTexInstructions = prog->NumTexInstructions;
469 clone->NumTexIndirections = prog->NumTexIndirections;
470 clone->NumNativeAluInstructions = prog->NumNativeAluInstructions;
471 clone->NumNativeTexInstructions = prog->NumNativeTexInstructions;
472 clone->NumNativeTexIndirections = prog->NumNativeTexIndirections;
Brianb2a3a852006-12-14 13:56:58 -0700473
474 switch (prog->Target) {
475 case GL_VERTEX_PROGRAM_ARB:
476 {
477 const struct gl_vertex_program *vp
478 = (const struct gl_vertex_program *) prog;
479 struct gl_vertex_program *vpc = (struct gl_vertex_program *) clone;
480 vpc->IsPositionInvariant = vp->IsPositionInvariant;
481 }
482 break;
483 case GL_FRAGMENT_PROGRAM_ARB:
484 {
485 const struct gl_fragment_program *fp
486 = (const struct gl_fragment_program *) prog;
487 struct gl_fragment_program *fpc = (struct gl_fragment_program *) clone;
Brianb2a3a852006-12-14 13:56:58 -0700488 fpc->FogOption = fp->FogOption;
489 fpc->UsesKill = fp->UsesKill;
490 }
491 break;
492 default:
493 _mesa_problem(NULL, "Unexpected target in _mesa_clone_program");
494 }
495
496 return clone;
497}
498
499
Brian Paul19ad9cf2008-05-14 12:39:41 -0600500/**
501 * Insert 'count' NOP instructions at 'start' in the given program.
502 * Adjust branch targets accordingly.
503 */
504GLboolean
505_mesa_insert_instructions(struct gl_program *prog, GLuint start, GLuint count)
506{
507 const GLuint origLen = prog->NumInstructions;
508 const GLuint newLen = origLen + count;
509 struct prog_instruction *newInst;
510 GLuint i;
511
512 /* adjust branches */
513 for (i = 0; i < prog->NumInstructions; i++) {
514 struct prog_instruction *inst = prog->Instructions + i;
515 if (inst->BranchTarget > 0) {
516 if (inst->BranchTarget >= start) {
517 inst->BranchTarget += count;
518 }
519 }
520 }
521
522 /* Alloc storage for new instructions */
523 newInst = _mesa_alloc_instructions(newLen);
524 if (!newInst) {
525 return GL_FALSE;
526 }
527
528 /* Copy 'start' instructions into new instruction buffer */
529 _mesa_copy_instructions(newInst, prog->Instructions, start);
530
531 /* init the new instructions */
532 _mesa_init_instructions(newInst + start, count);
533
534 /* Copy the remaining/tail instructions to new inst buffer */
535 _mesa_copy_instructions(newInst + start + count,
536 prog->Instructions + start,
537 origLen - start);
538
539 /* free old instructions */
540 _mesa_free_instructions(prog->Instructions, origLen);
541
542 /* install new instructions */
543 prog->Instructions = newInst;
544 prog->NumInstructions = newLen;
545
546 return GL_TRUE;
547}
548
Brianb2a3a852006-12-14 13:56:58 -0700549
550/**
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200551 * Delete 'count' instructions at 'start' in the given program.
552 * Adjust branch targets accordingly.
553 */
554GLboolean
555_mesa_delete_instructions(struct gl_program *prog, GLuint start, GLuint count)
556{
557 const GLuint origLen = prog->NumInstructions;
558 const GLuint newLen = origLen - count;
559 struct prog_instruction *newInst;
560 GLuint i;
561
562 /* adjust branches */
563 for (i = 0; i < prog->NumInstructions; i++) {
564 struct prog_instruction *inst = prog->Instructions + i;
565 if (inst->BranchTarget > 0) {
566 if (inst->BranchTarget >= start) {
567 inst->BranchTarget -= count;
568 }
569 }
570 }
571
572 /* Alloc storage for new instructions */
573 newInst = _mesa_alloc_instructions(newLen);
574 if (!newInst) {
575 return GL_FALSE;
576 }
577
578 /* Copy 'start' instructions into new instruction buffer */
579 _mesa_copy_instructions(newInst, prog->Instructions, start);
580
581 /* Copy the remaining/tail instructions to new inst buffer */
582 _mesa_copy_instructions(newInst + start,
583 prog->Instructions + start + count,
584 newLen - start);
585
586 /* free old instructions */
587 _mesa_free_instructions(prog->Instructions, origLen);
588
589 /* install new instructions */
590 prog->Instructions = newInst;
591 prog->NumInstructions = newLen;
592
593 return GL_TRUE;
594}
595
596
597/**
Brian Paul6ca948a2008-05-14 12:53:03 -0600598 * Search instructions for registers that match (oldFile, oldIndex),
599 * replacing them with (newFile, newIndex).
600 */
601static void
602replace_registers(struct prog_instruction *inst, GLuint numInst,
603 GLuint oldFile, GLuint oldIndex,
604 GLuint newFile, GLuint newIndex)
605{
606 GLuint i, j;
607 for (i = 0; i < numInst; i++) {
Brian Paul0c78c762008-05-18 15:46:26 -0600608 /* src regs */
Brian Paul6ca948a2008-05-14 12:53:03 -0600609 for (j = 0; j < _mesa_num_inst_src_regs(inst->Opcode); j++) {
610 if (inst[i].SrcReg[j].File == oldFile &&
611 inst[i].SrcReg[j].Index == oldIndex) {
612 inst[i].SrcReg[j].File = newFile;
613 inst[i].SrcReg[j].Index = newIndex;
614 }
615 }
Brian Paul0c78c762008-05-18 15:46:26 -0600616 /* dst reg */
617 if (inst[i].DstReg.File == oldFile && inst[i].DstReg.Index == oldIndex) {
618 inst[i].DstReg.File = newFile;
619 inst[i].DstReg.Index = newIndex;
620 }
Brian Paul6ca948a2008-05-14 12:53:03 -0600621 }
622}
623
624
625/**
626 * Search instructions for references to program parameters. When found,
627 * increment the parameter index by 'offset'.
628 * Used when combining programs.
629 */
630static void
631adjust_param_indexes(struct prog_instruction *inst, GLuint numInst,
632 GLuint offset)
633{
634 GLuint i, j;
635 for (i = 0; i < numInst; i++) {
636 for (j = 0; j < _mesa_num_inst_src_regs(inst->Opcode); j++) {
637 GLuint f = inst[i].SrcReg[j].File;
638 if (f == PROGRAM_CONSTANT ||
639 f == PROGRAM_UNIFORM ||
640 f == PROGRAM_STATE_VAR) {
641 inst[i].SrcReg[j].Index += offset;
642 }
643 }
644 }
645}
646
647
648/**
649 * Combine two programs into one. Fix instructions so the outputs of
650 * the first program go to the inputs of the second program.
651 */
652struct gl_program *
653_mesa_combine_programs(GLcontext *ctx,
Brian Paul0c78c762008-05-18 15:46:26 -0600654 const struct gl_program *progA,
655 const struct gl_program *progB)
Brian Paul6ca948a2008-05-14 12:53:03 -0600656{
657 struct prog_instruction *newInst;
658 struct gl_program *newProg;
659 const GLuint lenA = progA->NumInstructions - 1; /* omit END instr */
660 const GLuint lenB = progB->NumInstructions;
661 const GLuint numParamsA = _mesa_num_parameters(progA->Parameters);
662 const GLuint newLength = lenA + lenB;
Brian Paul0c78c762008-05-18 15:46:26 -0600663 GLbitfield inputsB;
Brian Paul6ca948a2008-05-14 12:53:03 -0600664 GLuint i;
665
666 ASSERT(progA->Target == progB->Target);
667
668 newInst = _mesa_alloc_instructions(newLength);
669 if (!newInst)
670 return GL_FALSE;
671
672 _mesa_copy_instructions(newInst, progA->Instructions, lenA);
673 _mesa_copy_instructions(newInst + lenA, progB->Instructions, lenB);
674
675 /* adjust branch / instruction addresses for B's instructions */
676 for (i = 0; i < lenB; i++) {
677 newInst[lenA + i].BranchTarget += lenA;
678 }
679
680 newProg = ctx->Driver.NewProgram(ctx, progA->Target, 0);
681 newProg->Instructions = newInst;
682 newProg->NumInstructions = newLength;
683
684 if (newProg->Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul0c78c762008-05-18 15:46:26 -0600685 struct gl_fragment_program *fprogA, *fprogB, *newFprog;
686 fprogA = (struct gl_fragment_program *) progA;
687 fprogB = (struct gl_fragment_program *) progB;
688 newFprog = (struct gl_fragment_program *) newProg;
689
690 newFprog->UsesKill = fprogA->UsesKill || fprogB->UsesKill;
691
692 /* Connect color outputs of fprogA to color inputs of fprogB, via a
693 * new temporary register.
694 */
Brian Paul6ca948a2008-05-14 12:53:03 -0600695 if ((progA->OutputsWritten & (1 << FRAG_RESULT_COLR)) &&
696 (progB->InputsRead & (1 << FRAG_ATTRIB_COL0))) {
Brian Paul0c78c762008-05-18 15:46:26 -0600697 GLint tempReg = _mesa_find_free_register(newProg, PROGRAM_TEMPORARY);
Brian Paule469d782008-05-19 16:03:43 -0600698 if (tempReg < 0) {
Brian Paul0c78c762008-05-18 15:46:26 -0600699 _mesa_problem(ctx, "No free temp regs found in "
700 "_mesa_combine_programs(), using 31");
701 tempReg = 31;
702 }
703 /* replace writes to result.color[0] with tempReg */
704 replace_registers(newInst, lenA,
705 PROGRAM_OUTPUT, FRAG_RESULT_COLR,
706 PROGRAM_TEMPORARY, tempReg);
707 /* replace reads from input.color[0] with tempReg */
Brian Paul6ca948a2008-05-14 12:53:03 -0600708 replace_registers(newInst + lenA, lenB,
709 PROGRAM_INPUT, FRAG_ATTRIB_COL0,
Brian Paul0c78c762008-05-18 15:46:26 -0600710 PROGRAM_TEMPORARY, tempReg);
Brian Paul6ca948a2008-05-14 12:53:03 -0600711 }
712
Brian Paul0c78c762008-05-18 15:46:26 -0600713 inputsB = progB->InputsRead;
714 if (progA->OutputsWritten & (1 << FRAG_RESULT_COLR)) {
715 inputsB &= ~(1 << FRAG_ATTRIB_COL0);
716 }
717 newProg->InputsRead = progA->InputsRead | inputsB;
Brian Paul6ca948a2008-05-14 12:53:03 -0600718 newProg->OutputsWritten = progB->OutputsWritten;
Brian Paul0c78c762008-05-18 15:46:26 -0600719 newProg->SamplersUsed = progA->SamplersUsed | progB->SamplersUsed;
Brian Paul6ca948a2008-05-14 12:53:03 -0600720 }
721 else {
722 /* vertex program */
723 assert(0); /* XXX todo */
724 }
725
726 /*
727 * Merge parameters (uniforms, constants, etc)
728 */
729 newProg->Parameters = _mesa_combine_parameter_lists(progA->Parameters,
730 progB->Parameters);
731
732 adjust_param_indexes(newInst + lenA, lenB, numParamsA);
733
734
735 return newProg;
736}
737
738
739
740
741/**
742 * Scan the given program to find a free register of the given type.
743 * \param regFile - PROGRAM_INPUT, PROGRAM_OUTPUT or PROGRAM_TEMPORARY
744 */
745GLint
746_mesa_find_free_register(const struct gl_program *prog, GLuint regFile)
747{
748 GLboolean used[MAX_PROGRAM_TEMPS];
749 GLuint i, k;
750
751 assert(regFile == PROGRAM_INPUT ||
752 regFile == PROGRAM_OUTPUT ||
753 regFile == PROGRAM_TEMPORARY);
754
755 _mesa_memset(used, 0, sizeof(used));
756
757 for (i = 0; i < prog->NumInstructions; i++) {
758 const struct prog_instruction *inst = prog->Instructions + i;
759 const GLuint n = _mesa_num_inst_src_regs(inst->Opcode);
760
761 for (k = 0; k < n; k++) {
762 if (inst->SrcReg[k].File == regFile) {
763 used[inst->SrcReg[k].Index] = GL_TRUE;
764 }
765 }
766 }
767
768 for (i = 0; i < MAX_PROGRAM_TEMPS; i++) {
769 if (!used[i])
770 return i;
771 }
772
773 return -1;
774}