blob: b3618641e50ebe745e7f39f385b300196f88b30e [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;
Brian Paul93c90d32008-08-12 10:00:36 -060061#if FEATURE_es2_glsl
62 ctx->VertexProgram.PointSizeEnabled = GL_TRUE;
63#else
Michal Krol2861e732004-03-29 11:09:34 +000064 ctx->VertexProgram.PointSizeEnabled = GL_FALSE;
Brian Paul93c90d32008-08-12 10:00:36 -060065#endif
Michal Krol2861e732004-03-29 11:09:34 +000066 ctx->VertexProgram.TwoSideEnabled = GL_FALSE;
Briandf43fb62008-05-06 23:08:51 -060067 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
68 ctx->Shared->DefaultVertexProgram);
Michal Krol2861e732004-03-29 11:09:34 +000069 assert(ctx->VertexProgram.Current);
Michal Krol2861e732004-03-29 11:09:34 +000070 for (i = 0; i < MAX_NV_VERTEX_PROGRAM_PARAMS / 4; i++) {
71 ctx->VertexProgram.TrackMatrix[i] = GL_NONE;
72 ctx->VertexProgram.TrackMatrixTransform[i] = GL_IDENTITY_NV;
73 }
Keith Whitwell32ef6e72008-09-20 08:26:11 -070074 ctx->VertexProgram.Cache = _mesa_new_program_cache();
Michal Krol2861e732004-03-29 11:09:34 +000075#endif
76
77#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
78 ctx->FragmentProgram.Enabled = GL_FALSE;
Briandf43fb62008-05-06 23:08:51 -060079 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
80 ctx->Shared->DefaultFragmentProgram);
Michal Krol2861e732004-03-29 11:09:34 +000081 assert(ctx->FragmentProgram.Current);
Keith Whitwell32ef6e72008-09-20 08:26:11 -070082 ctx->FragmentProgram.Cache = _mesa_new_program_cache();
Michal Krol2861e732004-03-29 11:09:34 +000083#endif
Dave Airlie7f752fe2004-12-19 03:06:59 +000084
Keith Whitwell32ef6e72008-09-20 08:26:11 -070085
Brian Paul63d68302005-11-19 16:43:04 +000086 /* XXX probably move this stuff */
Dave Airlie7f752fe2004-12-19 03:06:59 +000087#if FEATURE_ATI_fragment_shader
88 ctx->ATIFragmentShader.Enabled = GL_FALSE;
Brian Paulb7eea9a2008-07-29 17:19:25 -060089 ctx->ATIFragmentShader.Current = ctx->Shared->DefaultFragmentShader;
Dave Airlie7f752fe2004-12-19 03:06:59 +000090 assert(ctx->ATIFragmentShader.Current);
Brian Paul63d68302005-11-19 16:43:04 +000091 ctx->ATIFragmentShader.Current->RefCount++;
Dave Airlie7f752fe2004-12-19 03:06:59 +000092#endif
Michal Krol2861e732004-03-29 11:09:34 +000093}
94
95
96/**
Brian Paul21841f02004-08-14 14:28:11 +000097 * Free a context's vertex/fragment program state
98 */
99void
100_mesa_free_program_data(GLcontext *ctx)
101{
Roland Scheidegger93da6732006-03-01 23:11:14 +0000102#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
Briandf43fb62008-05-06 23:08:51 -0600103 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current, NULL);
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700104 _mesa_delete_program_cache(ctx, ctx->VertexProgram.Cache);
Brian Paul21841f02004-08-14 14:28:11 +0000105#endif
Roland Scheidegger93da6732006-03-01 23:11:14 +0000106#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
Briandf43fb62008-05-06 23:08:51 -0600107 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current, NULL);
Keith Whitwell32ef6e72008-09-20 08:26:11 -0700108 _mesa_delete_program_cache(ctx, ctx->FragmentProgram.Cache);
Brian Paul21841f02004-08-14 14:28:11 +0000109#endif
Brian Paul63d68302005-11-19 16:43:04 +0000110 /* XXX probably move this stuff */
Dave Airlie7f752fe2004-12-19 03:06:59 +0000111#if FEATURE_ATI_fragment_shader
112 if (ctx->ATIFragmentShader.Current) {
Brian Paul63d68302005-11-19 16:43:04 +0000113 ctx->ATIFragmentShader.Current->RefCount--;
114 if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
115 _mesa_free(ctx->ATIFragmentShader.Current);
116 }
Dave Airlie7f752fe2004-12-19 03:06:59 +0000117 }
118#endif
Brian Paul21841f02004-08-14 14:28:11 +0000119 _mesa_free((void *) ctx->Program.ErrorString);
120}
121
122
Brian4b654d42007-08-23 08:53:43 +0100123/**
124 * Update the default program objects in the given context to reference those
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200125 * specified in the shared state and release those referencing the old
Brian4b654d42007-08-23 08:53:43 +0100126 * shared state.
127 */
128void
129_mesa_update_default_objects_program(GLcontext *ctx)
130{
131#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
Brian Paul57e222d2008-05-14 12:10:45 -0600132 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
133 (struct gl_vertex_program *)
134 ctx->Shared->DefaultVertexProgram);
Brian4b654d42007-08-23 08:53:43 +0100135 assert(ctx->VertexProgram.Current);
Brian4b654d42007-08-23 08:53:43 +0100136#endif
137
138#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
Brian Paul57e222d2008-05-14 12:10:45 -0600139 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
140 (struct gl_fragment_program *)
141 ctx->Shared->DefaultFragmentProgram);
Brian4b654d42007-08-23 08:53:43 +0100142 assert(ctx->FragmentProgram.Current);
Brian4b654d42007-08-23 08:53:43 +0100143#endif
144
145 /* XXX probably move this stuff */
146#if FEATURE_ATI_fragment_shader
147 if (ctx->ATIFragmentShader.Current) {
148 ctx->ATIFragmentShader.Current->RefCount--;
149 if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
150 _mesa_free(ctx->ATIFragmentShader.Current);
151 }
152 }
153 ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader;
154 assert(ctx->ATIFragmentShader.Current);
155 ctx->ATIFragmentShader.Current->RefCount++;
156#endif
157}
Brian Paul21841f02004-08-14 14:28:11 +0000158
159
160/**
Michal Krol2861e732004-03-29 11:09:34 +0000161 * Set the vertex/fragment program error state (position and error string).
162 * This is generally called from within the parsers.
163 */
164void
165_mesa_set_program_error(GLcontext *ctx, GLint pos, const char *string)
166{
167 ctx->Program.ErrorPos = pos;
168 _mesa_free((void *) ctx->Program.ErrorString);
169 if (!string)
170 string = "";
171 ctx->Program.ErrorString = _mesa_strdup(string);
172}
173
174
175/**
176 * Find the line number and column for 'pos' within 'string'.
177 * Return a copy of the line which contains 'pos'. Free the line with
178 * _mesa_free().
179 * \param string the program string
180 * \param pos the position within the string
181 * \param line returns the line number corresponding to 'pos'.
182 * \param col returns the column number corresponding to 'pos'.
183 * \return copy of the line containing 'pos'.
184 */
185const GLubyte *
186_mesa_find_line_column(const GLubyte *string, const GLubyte *pos,
187 GLint *line, GLint *col)
188{
189 const GLubyte *lineStart = string;
190 const GLubyte *p = string;
191 GLubyte *s;
192 int len;
193
194 *line = 1;
195
196 while (p != pos) {
197 if (*p == (GLubyte) '\n') {
198 (*line)++;
199 lineStart = p + 1;
200 }
201 p++;
202 }
203
204 *col = (pos - lineStart) + 1;
205
206 /* return copy of this line */
207 while (*p != 0 && *p != '\n')
208 p++;
209 len = p - lineStart;
210 s = (GLubyte *) _mesa_malloc(len + 1);
211 _mesa_memcpy(s, lineStart, len);
212 s[len] = 0;
213
214 return s;
215}
216
217
Brian Paul765f1a12004-09-14 22:28:27 +0000218/**
219 * Initialize a new vertex/fragment program object.
220 */
Brian Paul122629f2006-07-20 16:49:57 +0000221static struct gl_program *
222_mesa_init_program_struct( GLcontext *ctx, struct gl_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000223 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000224{
Brian Paula6c423d2004-08-25 15:59:48 +0000225 (void) ctx;
Michal Krol2861e732004-03-29 11:09:34 +0000226 if (prog) {
Brian Paul0c78c762008-05-18 15:46:26 -0600227 GLuint i;
228 _mesa_bzero(prog, sizeof(*prog));
Michal Krol2861e732004-03-29 11:09:34 +0000229 prog->Id = id;
230 prog->Target = target;
231 prog->Resident = GL_TRUE;
232 prog->RefCount = 1;
Brian Paul308b85f2006-11-23 15:58:30 +0000233 prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
Brian Paul0c78c762008-05-18 15:46:26 -0600234
235 /* default mapping from samplers to texture units */
236 for (i = 0; i < MAX_SAMPLERS; i++)
237 prog->SamplerUnits[i] = i;
Michal Krol2861e732004-03-29 11:09:34 +0000238 }
239
240 return prog;
241}
242
Brian Paul765f1a12004-09-14 22:28:27 +0000243
244/**
245 * Initialize a new fragment program object.
246 */
Brian Paul122629f2006-07-20 16:49:57 +0000247struct gl_program *
248_mesa_init_fragment_program( GLcontext *ctx, struct gl_fragment_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000249 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000250{
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200251 if (prog)
Michal Krol2861e732004-03-29 11:09:34 +0000252 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
253 else
254 return NULL;
255}
256
Brian Paul765f1a12004-09-14 22:28:27 +0000257
258/**
259 * Initialize a new vertex program object.
260 */
Brian Paul122629f2006-07-20 16:49:57 +0000261struct gl_program *
262_mesa_init_vertex_program( GLcontext *ctx, struct gl_vertex_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000263 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000264{
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200265 if (prog)
Michal Krol2861e732004-03-29 11:09:34 +0000266 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
267 else
268 return NULL;
269}
270
271
272/**
273 * Allocate and initialize a new fragment/vertex program object but
274 * don't put it into the program hash table. Called via
275 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
276 * device driver function to implement OO deriviation with additional
277 * types not understood by this function.
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200278 *
Michal Krol2861e732004-03-29 11:09:34 +0000279 * \param ctx context
280 * \param id program id/number
281 * \param target program target/type
282 * \return pointer to new program object
283 */
Brian Paul122629f2006-07-20 16:49:57 +0000284struct gl_program *
Michal Krol2861e732004-03-29 11:09:34 +0000285_mesa_new_program(GLcontext *ctx, GLenum target, GLuint id)
286{
Brian Paula3e86d42008-05-16 09:56:11 -0600287 struct gl_program *prog;
Michal Krol2861e732004-03-29 11:09:34 +0000288 switch (target) {
289 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
Brian Paula3e86d42008-05-16 09:56:11 -0600290 prog = _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program),
Brian Paul122629f2006-07-20 16:49:57 +0000291 target, id );
Brian Paula3e86d42008-05-16 09:56:11 -0600292 break;
Michal Krol2861e732004-03-29 11:09:34 +0000293 case GL_FRAGMENT_PROGRAM_NV:
294 case GL_FRAGMENT_PROGRAM_ARB:
Brian Paula3e86d42008-05-16 09:56:11 -0600295 prog =_mesa_init_fragment_program(ctx,
Brian Paul122629f2006-07-20 16:49:57 +0000296 CALLOC_STRUCT(gl_fragment_program),
297 target, id );
Brian Paula3e86d42008-05-16 09:56:11 -0600298 break;
Michal Krol2861e732004-03-29 11:09:34 +0000299 default:
300 _mesa_problem(ctx, "bad target in _mesa_new_program");
Brian Paula3e86d42008-05-16 09:56:11 -0600301 prog = NULL;
Michal Krol2861e732004-03-29 11:09:34 +0000302 }
Brian Paula3e86d42008-05-16 09:56:11 -0600303 return prog;
Michal Krol2861e732004-03-29 11:09:34 +0000304}
305
306
307/**
308 * Delete a program and remove it from the hash table, ignoring the
309 * reference count.
310 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
311 * by a device driver function.
312 */
313void
Brian Paul122629f2006-07-20 16:49:57 +0000314_mesa_delete_program(GLcontext *ctx, struct gl_program *prog)
Michal Krol2861e732004-03-29 11:09:34 +0000315{
Brian Paula6c423d2004-08-25 15:59:48 +0000316 (void) ctx;
Brian Paul57e222d2008-05-14 12:10:45 -0600317 ASSERT(prog);
318 ASSERT(prog->RefCount==0);
Michal Krol2861e732004-03-29 11:09:34 +0000319
Brian Paul308b85f2006-11-23 15:58:30 +0000320 if (prog == &_mesa_DummyProgram)
321 return;
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200322
Michal Krol2861e732004-03-29 11:09:34 +0000323 if (prog->String)
324 _mesa_free(prog->String);
Brian Paulde997602005-11-12 17:53:14 +0000325
Brian Paul19ad9cf2008-05-14 12:39:41 -0600326 _mesa_free_instructions(prog->Instructions, prog->NumInstructions);
Brian Paulde997602005-11-12 17:53:14 +0000327
Brian Paul65a51c02006-05-24 03:30:31 +0000328 if (prog->Parameters) {
Brian Paulde997602005-11-12 17:53:14 +0000329 _mesa_free_parameter_list(prog->Parameters);
Brian Paul65a51c02006-05-24 03:30:31 +0000330 }
Brianfe1d01c2006-12-13 14:54:47 -0700331 if (prog->Varying) {
332 _mesa_free_parameter_list(prog->Varying);
333 }
Brian3493e862007-03-24 16:18:13 -0600334 if (prog->Attributes) {
335 _mesa_free_parameter_list(prog->Attributes);
336 }
Brianfe1d01c2006-12-13 14:54:47 -0700337
Brian Paul58d080b2006-08-25 19:46:31 +0000338 /* XXX this is a little ugly */
339 if (prog->Target == GL_VERTEX_PROGRAM_ARB) {
340 struct gl_vertex_program *vprog = (struct gl_vertex_program *) prog;
341 if (vprog->TnlData)
342 _mesa_free(vprog->TnlData);
343 }
344
Michal Krol2861e732004-03-29 11:09:34 +0000345 _mesa_free(prog);
346}
347
348
Brian Paul4d12a052006-08-23 23:10:14 +0000349/**
350 * Return the gl_program object for a given ID.
351 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
352 * casts elsewhere.
353 */
354struct gl_program *
355_mesa_lookup_program(GLcontext *ctx, GLuint id)
356{
357 if (id)
358 return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id);
359 else
360 return NULL;
361}
362
Michal Krol2861e732004-03-29 11:09:34 +0000363
Brian Paul3b9b8de2006-08-24 21:57:36 +0000364/**
Briandf43fb62008-05-06 23:08:51 -0600365 * Reference counting for vertex/fragment programs
366 */
367void
368_mesa_reference_program(GLcontext *ctx,
369 struct gl_program **ptr,
370 struct gl_program *prog)
371{
372 assert(ptr);
373 if (*ptr && prog) {
374 /* sanity check */
375 ASSERT((*ptr)->Target == prog->Target);
376 }
377 if (*ptr == prog) {
378 return; /* no change */
379 }
380 if (*ptr) {
381 GLboolean deleteFlag;
382
383 /*_glthread_LOCK_MUTEX((*ptr)->Mutex);*/
Brian Paulb4e75d62008-05-08 10:59:31 -0600384#if 0
Brian Paula3e86d42008-05-16 09:56:11 -0600385 printf("Program %p ID=%u Target=%s Refcount-- to %d\n",
386 *ptr, (*ptr)->Id,
387 ((*ptr)->Target == GL_VERTEX_PROGRAM_ARB ? "VP" : "FP"),
388 (*ptr)->RefCount - 1);
Briandf43fb62008-05-06 23:08:51 -0600389#endif
390 ASSERT((*ptr)->RefCount > 0);
391 (*ptr)->RefCount--;
392
393 deleteFlag = ((*ptr)->RefCount == 0);
394 /*_glthread_UNLOCK_MUTEX((*ptr)->Mutex);*/
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200395
Briandf43fb62008-05-06 23:08:51 -0600396 if (deleteFlag) {
397 ASSERT(ctx);
398 ctx->Driver.DeleteProgram(ctx, *ptr);
399 }
400
401 *ptr = NULL;
402 }
403
404 assert(!*ptr);
405 if (prog) {
406 /*_glthread_LOCK_MUTEX(prog->Mutex);*/
407 prog->RefCount++;
Brian Paulb4e75d62008-05-08 10:59:31 -0600408#if 0
Brian Paula3e86d42008-05-16 09:56:11 -0600409 printf("Program %p ID=%u Target=%s Refcount++ to %d\n",
410 prog, prog->Id,
411 (prog->Target == GL_VERTEX_PROGRAM_ARB ? "VP" : "FP"),
412 prog->RefCount);
Briandf43fb62008-05-06 23:08:51 -0600413#endif
414 /*_glthread_UNLOCK_MUTEX(prog->Mutex);*/
415 }
416
417 *ptr = prog;
418}
419
420
421/**
Brianb2a3a852006-12-14 13:56:58 -0700422 * Return a copy of a program.
423 * XXX Problem here if the program object is actually OO-derivation
424 * made by a device driver.
425 */
426struct gl_program *
427_mesa_clone_program(GLcontext *ctx, const struct gl_program *prog)
428{
429 struct gl_program *clone;
430
Brian5b6858c2007-07-24 09:56:44 -0600431 clone = ctx->Driver.NewProgram(ctx, prog->Target, prog->Id);
Brianb2a3a852006-12-14 13:56:58 -0700432 if (!clone)
433 return NULL;
434
435 assert(clone->Target == prog->Target);
Briandf43fb62008-05-06 23:08:51 -0600436 assert(clone->RefCount == 1);
437
Brianb2a3a852006-12-14 13:56:58 -0700438 clone->String = (GLubyte *) _mesa_strdup((char *) prog->String);
Brianb2a3a852006-12-14 13:56:58 -0700439 clone->Format = prog->Format;
440 clone->Instructions = _mesa_alloc_instructions(prog->NumInstructions);
441 if (!clone->Instructions) {
Brian Paul57e222d2008-05-14 12:10:45 -0600442 _mesa_reference_program(ctx, &clone, NULL);
Brianb2a3a852006-12-14 13:56:58 -0700443 return NULL;
444 }
Brian12229f12007-03-22 09:11:26 -0600445 _mesa_copy_instructions(clone->Instructions, prog->Instructions,
446 prog->NumInstructions);
Brianb2a3a852006-12-14 13:56:58 -0700447 clone->InputsRead = prog->InputsRead;
448 clone->OutputsWritten = prog->OutputsWritten;
Brian Paul0c78c762008-05-18 15:46:26 -0600449 clone->SamplersUsed = prog->SamplersUsed;
Nicolai Haehnle82635aa2008-07-05 18:03:44 +0200450 clone->ShadowSamplers = prog->ShadowSamplers;
Brianc9db2232007-01-04 17:22:19 -0700451 memcpy(clone->TexturesUsed, prog->TexturesUsed, sizeof(prog->TexturesUsed));
452
Brian2e76f0a2006-12-19 09:52:07 -0700453 if (prog->Parameters)
454 clone->Parameters = _mesa_clone_parameter_list(prog->Parameters);
Brianb2a3a852006-12-14 13:56:58 -0700455 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
Brian2e76f0a2006-12-19 09:52:07 -0700456 if (prog->Varying)
457 clone->Varying = _mesa_clone_parameter_list(prog->Varying);
Brian3209c3e2007-01-09 17:49:24 -0700458 if (prog->Attributes)
459 clone->Attributes = _mesa_clone_parameter_list(prog->Attributes);
Brianb2a3a852006-12-14 13:56:58 -0700460 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
461 clone->NumInstructions = prog->NumInstructions;
462 clone->NumTemporaries = prog->NumTemporaries;
463 clone->NumParameters = prog->NumParameters;
464 clone->NumAttributes = prog->NumAttributes;
465 clone->NumAddressRegs = prog->NumAddressRegs;
466 clone->NumNativeInstructions = prog->NumNativeInstructions;
467 clone->NumNativeTemporaries = prog->NumNativeTemporaries;
468 clone->NumNativeParameters = prog->NumNativeParameters;
469 clone->NumNativeAttributes = prog->NumNativeAttributes;
470 clone->NumNativeAddressRegs = prog->NumNativeAddressRegs;
Brian21f99792007-01-09 11:00:21 -0700471 clone->NumAluInstructions = prog->NumAluInstructions;
472 clone->NumTexInstructions = prog->NumTexInstructions;
473 clone->NumTexIndirections = prog->NumTexIndirections;
474 clone->NumNativeAluInstructions = prog->NumNativeAluInstructions;
475 clone->NumNativeTexInstructions = prog->NumNativeTexInstructions;
476 clone->NumNativeTexIndirections = prog->NumNativeTexIndirections;
Brianb2a3a852006-12-14 13:56:58 -0700477
478 switch (prog->Target) {
479 case GL_VERTEX_PROGRAM_ARB:
480 {
481 const struct gl_vertex_program *vp
482 = (const struct gl_vertex_program *) prog;
483 struct gl_vertex_program *vpc = (struct gl_vertex_program *) clone;
484 vpc->IsPositionInvariant = vp->IsPositionInvariant;
485 }
486 break;
487 case GL_FRAGMENT_PROGRAM_ARB:
488 {
489 const struct gl_fragment_program *fp
490 = (const struct gl_fragment_program *) prog;
491 struct gl_fragment_program *fpc = (struct gl_fragment_program *) clone;
Brianb2a3a852006-12-14 13:56:58 -0700492 fpc->FogOption = fp->FogOption;
493 fpc->UsesKill = fp->UsesKill;
494 }
495 break;
496 default:
497 _mesa_problem(NULL, "Unexpected target in _mesa_clone_program");
498 }
499
500 return clone;
501}
502
503
Brian Paul19ad9cf2008-05-14 12:39:41 -0600504/**
505 * Insert 'count' NOP instructions at 'start' in the given program.
506 * Adjust branch targets accordingly.
507 */
508GLboolean
509_mesa_insert_instructions(struct gl_program *prog, GLuint start, GLuint count)
510{
511 const GLuint origLen = prog->NumInstructions;
512 const GLuint newLen = origLen + count;
513 struct prog_instruction *newInst;
514 GLuint i;
515
516 /* adjust branches */
517 for (i = 0; i < prog->NumInstructions; i++) {
518 struct prog_instruction *inst = prog->Instructions + i;
519 if (inst->BranchTarget > 0) {
José Fonseca457d7212008-06-24 11:34:46 +0900520 if ((GLuint)inst->BranchTarget >= start) {
Brian Paul19ad9cf2008-05-14 12:39:41 -0600521 inst->BranchTarget += count;
522 }
523 }
524 }
525
526 /* Alloc storage for new instructions */
527 newInst = _mesa_alloc_instructions(newLen);
528 if (!newInst) {
529 return GL_FALSE;
530 }
531
532 /* Copy 'start' instructions into new instruction buffer */
533 _mesa_copy_instructions(newInst, prog->Instructions, start);
534
535 /* init the new instructions */
536 _mesa_init_instructions(newInst + start, count);
537
538 /* Copy the remaining/tail instructions to new inst buffer */
539 _mesa_copy_instructions(newInst + start + count,
540 prog->Instructions + start,
541 origLen - start);
542
543 /* free old instructions */
544 _mesa_free_instructions(prog->Instructions, origLen);
545
546 /* install new instructions */
547 prog->Instructions = newInst;
548 prog->NumInstructions = newLen;
549
550 return GL_TRUE;
551}
552
Brianb2a3a852006-12-14 13:56:58 -0700553
554/**
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200555 * Delete 'count' instructions at 'start' in the given program.
556 * Adjust branch targets accordingly.
557 */
558GLboolean
559_mesa_delete_instructions(struct gl_program *prog, GLuint start, GLuint count)
560{
561 const GLuint origLen = prog->NumInstructions;
562 const GLuint newLen = origLen - count;
563 struct prog_instruction *newInst;
564 GLuint i;
565
566 /* adjust branches */
567 for (i = 0; i < prog->NumInstructions; i++) {
568 struct prog_instruction *inst = prog->Instructions + i;
569 if (inst->BranchTarget > 0) {
570 if (inst->BranchTarget >= start) {
571 inst->BranchTarget -= count;
572 }
573 }
574 }
575
576 /* Alloc storage for new instructions */
577 newInst = _mesa_alloc_instructions(newLen);
578 if (!newInst) {
579 return GL_FALSE;
580 }
581
582 /* Copy 'start' instructions into new instruction buffer */
583 _mesa_copy_instructions(newInst, prog->Instructions, start);
584
585 /* Copy the remaining/tail instructions to new inst buffer */
586 _mesa_copy_instructions(newInst + start,
587 prog->Instructions + start + count,
588 newLen - start);
589
590 /* free old instructions */
591 _mesa_free_instructions(prog->Instructions, origLen);
592
593 /* install new instructions */
594 prog->Instructions = newInst;
595 prog->NumInstructions = newLen;
596
597 return GL_TRUE;
598}
599
600
601/**
Brian Paul6ca948a2008-05-14 12:53:03 -0600602 * Search instructions for registers that match (oldFile, oldIndex),
603 * replacing them with (newFile, newIndex).
604 */
605static void
606replace_registers(struct prog_instruction *inst, GLuint numInst,
607 GLuint oldFile, GLuint oldIndex,
608 GLuint newFile, GLuint newIndex)
609{
610 GLuint i, j;
611 for (i = 0; i < numInst; i++) {
Brian Paul0c78c762008-05-18 15:46:26 -0600612 /* src regs */
Brian Paul6ca948a2008-05-14 12:53:03 -0600613 for (j = 0; j < _mesa_num_inst_src_regs(inst->Opcode); j++) {
614 if (inst[i].SrcReg[j].File == oldFile &&
615 inst[i].SrcReg[j].Index == oldIndex) {
616 inst[i].SrcReg[j].File = newFile;
617 inst[i].SrcReg[j].Index = newIndex;
618 }
619 }
Brian Paul0c78c762008-05-18 15:46:26 -0600620 /* dst reg */
621 if (inst[i].DstReg.File == oldFile && inst[i].DstReg.Index == oldIndex) {
622 inst[i].DstReg.File = newFile;
623 inst[i].DstReg.Index = newIndex;
624 }
Brian Paul6ca948a2008-05-14 12:53:03 -0600625 }
626}
627
628
629/**
630 * Search instructions for references to program parameters. When found,
631 * increment the parameter index by 'offset'.
632 * Used when combining programs.
633 */
634static void
635adjust_param_indexes(struct prog_instruction *inst, GLuint numInst,
636 GLuint offset)
637{
638 GLuint i, j;
639 for (i = 0; i < numInst; i++) {
640 for (j = 0; j < _mesa_num_inst_src_regs(inst->Opcode); j++) {
641 GLuint f = inst[i].SrcReg[j].File;
642 if (f == PROGRAM_CONSTANT ||
643 f == PROGRAM_UNIFORM ||
644 f == PROGRAM_STATE_VAR) {
645 inst[i].SrcReg[j].Index += offset;
646 }
647 }
648 }
649}
650
651
652/**
653 * Combine two programs into one. Fix instructions so the outputs of
654 * the first program go to the inputs of the second program.
655 */
656struct gl_program *
657_mesa_combine_programs(GLcontext *ctx,
Brian Paul0c78c762008-05-18 15:46:26 -0600658 const struct gl_program *progA,
659 const struct gl_program *progB)
Brian Paul6ca948a2008-05-14 12:53:03 -0600660{
661 struct prog_instruction *newInst;
662 struct gl_program *newProg;
663 const GLuint lenA = progA->NumInstructions - 1; /* omit END instr */
664 const GLuint lenB = progB->NumInstructions;
665 const GLuint numParamsA = _mesa_num_parameters(progA->Parameters);
666 const GLuint newLength = lenA + lenB;
Brian Paul0c78c762008-05-18 15:46:26 -0600667 GLbitfield inputsB;
Brian Paul6ca948a2008-05-14 12:53:03 -0600668 GLuint i;
669
670 ASSERT(progA->Target == progB->Target);
671
672 newInst = _mesa_alloc_instructions(newLength);
673 if (!newInst)
674 return GL_FALSE;
675
676 _mesa_copy_instructions(newInst, progA->Instructions, lenA);
677 _mesa_copy_instructions(newInst + lenA, progB->Instructions, lenB);
678
679 /* adjust branch / instruction addresses for B's instructions */
680 for (i = 0; i < lenB; i++) {
681 newInst[lenA + i].BranchTarget += lenA;
682 }
683
684 newProg = ctx->Driver.NewProgram(ctx, progA->Target, 0);
685 newProg->Instructions = newInst;
686 newProg->NumInstructions = newLength;
687
688 if (newProg->Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul0c78c762008-05-18 15:46:26 -0600689 struct gl_fragment_program *fprogA, *fprogB, *newFprog;
690 fprogA = (struct gl_fragment_program *) progA;
691 fprogB = (struct gl_fragment_program *) progB;
692 newFprog = (struct gl_fragment_program *) newProg;
693
694 newFprog->UsesKill = fprogA->UsesKill || fprogB->UsesKill;
695
696 /* Connect color outputs of fprogA to color inputs of fprogB, via a
697 * new temporary register.
698 */
Brian Paul6ca948a2008-05-14 12:53:03 -0600699 if ((progA->OutputsWritten & (1 << FRAG_RESULT_COLR)) &&
700 (progB->InputsRead & (1 << FRAG_ATTRIB_COL0))) {
Brian Paul0c78c762008-05-18 15:46:26 -0600701 GLint tempReg = _mesa_find_free_register(newProg, PROGRAM_TEMPORARY);
Brian Paule469d782008-05-19 16:03:43 -0600702 if (tempReg < 0) {
Brian Paul0c78c762008-05-18 15:46:26 -0600703 _mesa_problem(ctx, "No free temp regs found in "
704 "_mesa_combine_programs(), using 31");
705 tempReg = 31;
706 }
707 /* replace writes to result.color[0] with tempReg */
708 replace_registers(newInst, lenA,
709 PROGRAM_OUTPUT, FRAG_RESULT_COLR,
710 PROGRAM_TEMPORARY, tempReg);
711 /* replace reads from input.color[0] with tempReg */
Brian Paul6ca948a2008-05-14 12:53:03 -0600712 replace_registers(newInst + lenA, lenB,
713 PROGRAM_INPUT, FRAG_ATTRIB_COL0,
Brian Paul0c78c762008-05-18 15:46:26 -0600714 PROGRAM_TEMPORARY, tempReg);
Brian Paul6ca948a2008-05-14 12:53:03 -0600715 }
716
Brian Paul0c78c762008-05-18 15:46:26 -0600717 inputsB = progB->InputsRead;
718 if (progA->OutputsWritten & (1 << FRAG_RESULT_COLR)) {
719 inputsB &= ~(1 << FRAG_ATTRIB_COL0);
720 }
721 newProg->InputsRead = progA->InputsRead | inputsB;
Brian Paul6ca948a2008-05-14 12:53:03 -0600722 newProg->OutputsWritten = progB->OutputsWritten;
Brian Paul0c78c762008-05-18 15:46:26 -0600723 newProg->SamplersUsed = progA->SamplersUsed | progB->SamplersUsed;
Brian Paul6ca948a2008-05-14 12:53:03 -0600724 }
725 else {
726 /* vertex program */
727 assert(0); /* XXX todo */
728 }
729
730 /*
731 * Merge parameters (uniforms, constants, etc)
732 */
733 newProg->Parameters = _mesa_combine_parameter_lists(progA->Parameters,
734 progB->Parameters);
735
736 adjust_param_indexes(newInst + lenA, lenB, numParamsA);
737
738
739 return newProg;
740}
741
742
743
744
745/**
746 * Scan the given program to find a free register of the given type.
747 * \param regFile - PROGRAM_INPUT, PROGRAM_OUTPUT or PROGRAM_TEMPORARY
748 */
749GLint
750_mesa_find_free_register(const struct gl_program *prog, GLuint regFile)
751{
752 GLboolean used[MAX_PROGRAM_TEMPS];
753 GLuint i, k;
754
755 assert(regFile == PROGRAM_INPUT ||
756 regFile == PROGRAM_OUTPUT ||
757 regFile == PROGRAM_TEMPORARY);
758
759 _mesa_memset(used, 0, sizeof(used));
760
761 for (i = 0; i < prog->NumInstructions; i++) {
762 const struct prog_instruction *inst = prog->Instructions + i;
763 const GLuint n = _mesa_num_inst_src_regs(inst->Opcode);
764
765 for (k = 0; k < n; k++) {
766 if (inst->SrcReg[k].File == regFile) {
767 used[inst->SrcReg[k].Index] = GL_TRUE;
768 }
769 }
770 }
771
772 for (i = 0; i < MAX_PROGRAM_TEMPS; i++) {
773 if (!used[i])
774 return i;
775 }
776
777 return -1;
778}