blob: 9a23c5d7d31fc1698e3a49bb57cc52ea46ea5e8a [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
32#include "glheader.h"
33#include "context.h"
34#include "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
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;
Brian Paula56a59c2008-05-07 08:55:33 -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 }
Brianb26aae62007-10-31 12:00:38 -060070 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;
Brian Paula56a59c2008-05-07 08:55:33 -060075 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
76 ctx->Shared->DefaultFragmentProgram);
Michal Krol2861e732004-03-29 11:09:34 +000077 assert(ctx->FragmentProgram.Current);
Brianb26aae62007-10-31 12:00:38 -060078 ctx->FragmentProgram.Cache = _mesa_new_program_cache();
Michal Krol2861e732004-03-29 11:09:34 +000079#endif
Dave Airlie7f752fe2004-12-19 03:06:59 +000080
Brianb26aae62007-10-31 12:00:38 -060081
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;
85 ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader;
86 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
Brian Paula56a59c2008-05-07 08:55:33 -060099 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current, NULL);
Brian1631a952007-12-26 06:57:24 -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
Brian Paula56a59c2008-05-07 08:55:33 -0600103 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current, NULL);
Brian1631a952007-12-26 06:57:24 -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
119
120
121/**
Michal Krol2861e732004-03-29 11:09:34 +0000122 * Set the vertex/fragment program error state (position and error string).
123 * This is generally called from within the parsers.
124 */
125void
126_mesa_set_program_error(GLcontext *ctx, GLint pos, const char *string)
127{
128 ctx->Program.ErrorPos = pos;
129 _mesa_free((void *) ctx->Program.ErrorString);
130 if (!string)
131 string = "";
132 ctx->Program.ErrorString = _mesa_strdup(string);
133}
134
135
136/**
137 * Find the line number and column for 'pos' within 'string'.
138 * Return a copy of the line which contains 'pos'. Free the line with
139 * _mesa_free().
140 * \param string the program string
141 * \param pos the position within the string
142 * \param line returns the line number corresponding to 'pos'.
143 * \param col returns the column number corresponding to 'pos'.
144 * \return copy of the line containing 'pos'.
145 */
146const GLubyte *
147_mesa_find_line_column(const GLubyte *string, const GLubyte *pos,
148 GLint *line, GLint *col)
149{
150 const GLubyte *lineStart = string;
151 const GLubyte *p = string;
152 GLubyte *s;
153 int len;
154
155 *line = 1;
156
157 while (p != pos) {
158 if (*p == (GLubyte) '\n') {
159 (*line)++;
160 lineStart = p + 1;
161 }
162 p++;
163 }
164
165 *col = (pos - lineStart) + 1;
166
167 /* return copy of this line */
168 while (*p != 0 && *p != '\n')
169 p++;
170 len = p - lineStart;
171 s = (GLubyte *) _mesa_malloc(len + 1);
172 _mesa_memcpy(s, lineStart, len);
173 s[len] = 0;
174
175 return s;
176}
177
178
Brian Paul765f1a12004-09-14 22:28:27 +0000179/**
180 * Initialize a new vertex/fragment program object.
181 */
Brian Paul122629f2006-07-20 16:49:57 +0000182static struct gl_program *
183_mesa_init_program_struct( GLcontext *ctx, struct gl_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000184 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000185{
Brian Paula6c423d2004-08-25 15:59:48 +0000186 (void) ctx;
Michal Krol2861e732004-03-29 11:09:34 +0000187 if (prog) {
Brian8fed2462007-10-26 19:19:09 -0600188 GLuint i;
Brianfe1d01c2006-12-13 14:54:47 -0700189 _mesa_bzero(prog, sizeof(*prog));
Michal Krol2861e732004-03-29 11:09:34 +0000190 prog->Id = id;
191 prog->Target = target;
192 prog->Resident = GL_TRUE;
193 prog->RefCount = 1;
Brian Paul308b85f2006-11-23 15:58:30 +0000194 prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
Brian8fed2462007-10-26 19:19:09 -0600195
196 /* default mapping from samplers to texture units */
197 for (i = 0; i < MAX_SAMPLERS; i++)
198 prog->SamplerUnits[i] = i;
Michal Krol2861e732004-03-29 11:09:34 +0000199 }
200
201 return prog;
202}
203
Brian Paul765f1a12004-09-14 22:28:27 +0000204
205/**
206 * Initialize a new fragment program object.
207 */
Brian Paul122629f2006-07-20 16:49:57 +0000208struct gl_program *
209_mesa_init_fragment_program( GLcontext *ctx, struct gl_fragment_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000210 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000211{
212 if (prog)
213 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
214 else
215 return NULL;
216}
217
Brian Paul765f1a12004-09-14 22:28:27 +0000218
219/**
220 * Initialize a new vertex program object.
221 */
Brian Paul122629f2006-07-20 16:49:57 +0000222struct gl_program *
223_mesa_init_vertex_program( GLcontext *ctx, struct gl_vertex_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000224 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000225{
226 if (prog)
227 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
228 else
229 return NULL;
230}
231
232
233/**
234 * Allocate and initialize a new fragment/vertex program object but
235 * don't put it into the program hash table. Called via
236 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
237 * device driver function to implement OO deriviation with additional
238 * types not understood by this function.
239 *
240 * \param ctx context
241 * \param id program id/number
242 * \param target program target/type
243 * \return pointer to new program object
244 */
Brian Paul122629f2006-07-20 16:49:57 +0000245struct gl_program *
Michal Krol2861e732004-03-29 11:09:34 +0000246_mesa_new_program(GLcontext *ctx, GLenum target, GLuint id)
247{
248 switch (target) {
249 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
Brian Paul122629f2006-07-20 16:49:57 +0000250 return _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program),
251 target, id );
Michal Krol2861e732004-03-29 11:09:34 +0000252 case GL_FRAGMENT_PROGRAM_NV:
253 case GL_FRAGMENT_PROGRAM_ARB:
Brian Paul122629f2006-07-20 16:49:57 +0000254 return _mesa_init_fragment_program(ctx,
255 CALLOC_STRUCT(gl_fragment_program),
256 target, id );
Michal Krol2861e732004-03-29 11:09:34 +0000257 default:
258 _mesa_problem(ctx, "bad target in _mesa_new_program");
259 return NULL;
260 }
261}
262
263
264/**
265 * Delete a program and remove it from the hash table, ignoring the
266 * reference count.
267 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
268 * by a device driver function.
269 */
270void
Brian Paul122629f2006-07-20 16:49:57 +0000271_mesa_delete_program(GLcontext *ctx, struct gl_program *prog)
Michal Krol2861e732004-03-29 11:09:34 +0000272{
Brian Paula6c423d2004-08-25 15:59:48 +0000273 (void) ctx;
Michal Krol2861e732004-03-29 11:09:34 +0000274 ASSERT(prog);
275
Brian Paul308b85f2006-11-23 15:58:30 +0000276 if (prog == &_mesa_DummyProgram)
277 return;
278
Michal Krol2861e732004-03-29 11:09:34 +0000279 if (prog->String)
280 _mesa_free(prog->String);
Brian Paulde997602005-11-12 17:53:14 +0000281
Brian5d1e7302008-04-07 11:16:18 -0600282 _mesa_free_instructions(prog->Instructions, prog->NumInstructions);
Brian Paulde997602005-11-12 17:53:14 +0000283
Brian Paul65a51c02006-05-24 03:30:31 +0000284 if (prog->Parameters) {
Brian Paulde997602005-11-12 17:53:14 +0000285 _mesa_free_parameter_list(prog->Parameters);
Brian Paul65a51c02006-05-24 03:30:31 +0000286 }
Brianfe1d01c2006-12-13 14:54:47 -0700287 if (prog->Varying) {
288 _mesa_free_parameter_list(prog->Varying);
289 }
Brian3493e862007-03-24 16:18:13 -0600290 if (prog->Attributes) {
291 _mesa_free_parameter_list(prog->Attributes);
292 }
Brianfe1d01c2006-12-13 14:54:47 -0700293
Brian Paul58d080b2006-08-25 19:46:31 +0000294 /* XXX this is a little ugly */
295 if (prog->Target == GL_VERTEX_PROGRAM_ARB) {
296 struct gl_vertex_program *vprog = (struct gl_vertex_program *) prog;
297 if (vprog->TnlData)
298 _mesa_free(vprog->TnlData);
299 }
300
Michal Krol2861e732004-03-29 11:09:34 +0000301 _mesa_free(prog);
302}
303
304
Brian Paul4d12a052006-08-23 23:10:14 +0000305/**
306 * Return the gl_program object for a given ID.
307 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
308 * casts elsewhere.
309 */
310struct gl_program *
311_mesa_lookup_program(GLcontext *ctx, GLuint id)
312{
313 if (id)
314 return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id);
315 else
316 return NULL;
317}
318
Michal Krol2861e732004-03-29 11:09:34 +0000319
Brian Paul3b9b8de2006-08-24 21:57:36 +0000320/**
Brian Paula56a59c2008-05-07 08:55:33 -0600321 * Reference counting for vertex/fragment programs
322 */
323void
324_mesa_reference_program(GLcontext *ctx,
325 struct gl_program **ptr,
326 struct gl_program *prog)
327{
328 assert(ptr);
329 if (*ptr && prog) {
330 /* sanity check */
331 ASSERT((*ptr)->Target == prog->Target);
332 }
333 if (*ptr == prog) {
334 return; /* no change */
335 }
336 if (*ptr) {
337 GLboolean deleteFlag;
338
339 /*_glthread_LOCK_MUTEX((*ptr)->Mutex);*/
340#if 0
341 printf("Program %p %u 0x%x Refcount-- to %d\n",
342 *ptr, (*ptr)->Id, (*ptr)->Target, (*ptr)->RefCount - 1);
343#endif
344 ASSERT((*ptr)->RefCount > 0);
345 (*ptr)->RefCount--;
346
347 deleteFlag = ((*ptr)->RefCount == 0);
348 /*_glthread_UNLOCK_MUTEX((*ptr)->Mutex);*/
349
350 if (deleteFlag) {
351 ASSERT(ctx);
352 ctx->Driver.DeleteProgram(ctx, *ptr);
353 }
354
355 *ptr = NULL;
356 }
357
358 assert(!*ptr);
359 if (prog) {
360 /*_glthread_LOCK_MUTEX(prog->Mutex);*/
361 prog->RefCount++;
362#if 0
363 printf("Program %p %u 0x%x Refcount++ to %d\n",
364 prog, prog->Id, prog->Target, prog->RefCount);
365#endif
366 /*_glthread_UNLOCK_MUTEX(prog->Mutex);*/
367 }
368
369 *ptr = prog;
370}
371
372
373/**
Brianb2a3a852006-12-14 13:56:58 -0700374 * Return a copy of a program.
375 * XXX Problem here if the program object is actually OO-derivation
376 * made by a device driver.
377 */
378struct gl_program *
379_mesa_clone_program(GLcontext *ctx, const struct gl_program *prog)
380{
381 struct gl_program *clone;
382
Brian4477a012007-07-24 09:57:26 -0600383 clone = ctx->Driver.NewProgram(ctx, prog->Target, prog->Id);
Brianb2a3a852006-12-14 13:56:58 -0700384 if (!clone)
385 return NULL;
386
387 assert(clone->Target == prog->Target);
Brian Paula56a59c2008-05-07 08:55:33 -0600388 assert(clone->RefCount == 1);
389
Brianb2a3a852006-12-14 13:56:58 -0700390 clone->String = (GLubyte *) _mesa_strdup((char *) prog->String);
Brianb2a3a852006-12-14 13:56:58 -0700391 clone->Format = prog->Format;
392 clone->Instructions = _mesa_alloc_instructions(prog->NumInstructions);
393 if (!clone->Instructions) {
Brianf8acc392008-03-22 10:27:55 -0600394 ctx->Driver.DeleteProgram(ctx, clone);
Brianb2a3a852006-12-14 13:56:58 -0700395 return NULL;
396 }
Brian12229f12007-03-22 09:11:26 -0600397 _mesa_copy_instructions(clone->Instructions, prog->Instructions,
398 prog->NumInstructions);
Brianb2a3a852006-12-14 13:56:58 -0700399 clone->InputsRead = prog->InputsRead;
400 clone->OutputsWritten = prog->OutputsWritten;
Brian Paul72f2c552008-04-04 11:20:44 -0600401 clone->SamplersUsed = prog->SamplersUsed;
Brianc9db2232007-01-04 17:22:19 -0700402 memcpy(clone->TexturesUsed, prog->TexturesUsed, sizeof(prog->TexturesUsed));
403
Brian2e76f0a2006-12-19 09:52:07 -0700404 if (prog->Parameters)
405 clone->Parameters = _mesa_clone_parameter_list(prog->Parameters);
Brianb2a3a852006-12-14 13:56:58 -0700406 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
Brian2e76f0a2006-12-19 09:52:07 -0700407 if (prog->Varying)
408 clone->Varying = _mesa_clone_parameter_list(prog->Varying);
Brian3209c3e2007-01-09 17:49:24 -0700409 if (prog->Attributes)
410 clone->Attributes = _mesa_clone_parameter_list(prog->Attributes);
Brianb2a3a852006-12-14 13:56:58 -0700411 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
412 clone->NumInstructions = prog->NumInstructions;
413 clone->NumTemporaries = prog->NumTemporaries;
414 clone->NumParameters = prog->NumParameters;
415 clone->NumAttributes = prog->NumAttributes;
416 clone->NumAddressRegs = prog->NumAddressRegs;
417 clone->NumNativeInstructions = prog->NumNativeInstructions;
418 clone->NumNativeTemporaries = prog->NumNativeTemporaries;
419 clone->NumNativeParameters = prog->NumNativeParameters;
420 clone->NumNativeAttributes = prog->NumNativeAttributes;
421 clone->NumNativeAddressRegs = prog->NumNativeAddressRegs;
Brian21f99792007-01-09 11:00:21 -0700422 clone->NumAluInstructions = prog->NumAluInstructions;
423 clone->NumTexInstructions = prog->NumTexInstructions;
424 clone->NumTexIndirections = prog->NumTexIndirections;
425 clone->NumNativeAluInstructions = prog->NumNativeAluInstructions;
426 clone->NumNativeTexInstructions = prog->NumNativeTexInstructions;
427 clone->NumNativeTexIndirections = prog->NumNativeTexIndirections;
Brianb2a3a852006-12-14 13:56:58 -0700428
429 switch (prog->Target) {
430 case GL_VERTEX_PROGRAM_ARB:
431 {
432 const struct gl_vertex_program *vp
433 = (const struct gl_vertex_program *) prog;
434 struct gl_vertex_program *vpc = (struct gl_vertex_program *) clone;
435 vpc->IsPositionInvariant = vp->IsPositionInvariant;
436 }
437 break;
438 case GL_FRAGMENT_PROGRAM_ARB:
439 {
440 const struct gl_fragment_program *fp
441 = (const struct gl_fragment_program *) prog;
442 struct gl_fragment_program *fpc = (struct gl_fragment_program *) clone;
Brianb2a3a852006-12-14 13:56:58 -0700443 fpc->FogOption = fp->FogOption;
444 fpc->UsesKill = fp->UsesKill;
445 }
446 break;
447 default:
448 _mesa_problem(NULL, "Unexpected target in _mesa_clone_program");
449 }
450
451 return clone;
452}
453
454
Brian5d1e7302008-04-07 11:16:18 -0600455/**
456 * Insert 'count' NOP instructions at 'start' in the given program.
457 * Adjust branch targets accordingly.
458 */
459GLboolean
460_mesa_insert_instructions(struct gl_program *prog, GLuint start, GLuint count)
461{
462 const GLuint origLen = prog->NumInstructions;
463 const GLuint newLen = origLen + count;
464 struct prog_instruction *newInst;
465 GLuint i;
466
467 /* adjust branches */
468 for (i = 0; i < prog->NumInstructions; i++) {
469 struct prog_instruction *inst = prog->Instructions + i;
470 if (inst->BranchTarget > 0) {
471 if (inst->BranchTarget >= start) {
472 inst->BranchTarget += count;
473 }
474 }
475 }
476
477 /* Alloc storage for new instructions */
478 newInst = _mesa_alloc_instructions(newLen);
479 if (!newInst) {
480 return GL_FALSE;
481 }
482
483 /* Copy 'start' instructions into new instruction buffer */
484 _mesa_copy_instructions(newInst, prog->Instructions, start);
485
486 /* init the new instructions */
487 _mesa_init_instructions(newInst + start, count);
488
489 /* Copy the remaining/tail instructions to new inst buffer */
490 _mesa_copy_instructions(newInst + start + count,
491 prog->Instructions + start,
492 origLen - start);
493
494 /* free old instructions */
495 _mesa_free_instructions(prog->Instructions, origLen);
496
497 /* install new instructions */
498 prog->Instructions = newInst;
499 prog->NumInstructions = newLen;
500
501 return GL_TRUE;
502}
503
Brianb2a3a852006-12-14 13:56:58 -0700504
505/**
Brian9ffd8892007-10-29 16:35:59 -0600506 * Search instructions for registers that match (oldFile, oldIndex),
507 * replacing them with (newFile, newIndex).
508 */
509static void
510replace_registers(struct prog_instruction *inst, GLuint numInst,
511 GLuint oldFile, GLuint oldIndex,
512 GLuint newFile, GLuint newIndex)
513{
514 GLuint i, j;
515 for (i = 0; i < numInst; i++) {
Brian Paulcf7daba2008-03-25 12:27:32 -0600516 /* src regs */
Brian9ffd8892007-10-29 16:35:59 -0600517 for (j = 0; j < _mesa_num_inst_src_regs(inst->Opcode); j++) {
518 if (inst[i].SrcReg[j].File == oldFile &&
519 inst[i].SrcReg[j].Index == oldIndex) {
520 inst[i].SrcReg[j].File = newFile;
521 inst[i].SrcReg[j].Index = newIndex;
522 }
523 }
Brian Paulcf7daba2008-03-25 12:27:32 -0600524 /* dst reg */
525 if (inst[i].DstReg.File == oldFile && inst[i].DstReg.Index == oldIndex) {
526 inst[i].DstReg.File = newFile;
527 inst[i].DstReg.Index = newIndex;
528 }
Brian9ffd8892007-10-29 16:35:59 -0600529 }
530}
531
532
533/**
534 * Search instructions for references to program parameters. When found,
535 * increment the parameter index by 'offset'.
536 * Used when combining programs.
537 */
538static void
539adjust_param_indexes(struct prog_instruction *inst, GLuint numInst,
540 GLuint offset)
541{
542 GLuint i, j;
543 for (i = 0; i < numInst; i++) {
544 for (j = 0; j < _mesa_num_inst_src_regs(inst->Opcode); j++) {
545 GLuint f = inst[i].SrcReg[j].File;
546 if (f == PROGRAM_CONSTANT ||
547 f == PROGRAM_UNIFORM ||
548 f == PROGRAM_STATE_VAR) {
549 inst[i].SrcReg[j].Index += offset;
550 }
551 }
552 }
553}
554
555
556/**
557 * Combine two programs into one. Fix instructions so the outputs of
558 * the first program go to the inputs of the second program.
559 */
560struct gl_program *
561_mesa_combine_programs(GLcontext *ctx,
Brian1203f542007-10-29 16:38:53 -0600562 const struct gl_program *progA,
563 const struct gl_program *progB)
Brian9ffd8892007-10-29 16:35:59 -0600564{
565 struct prog_instruction *newInst;
566 struct gl_program *newProg;
567 const GLuint lenA = progA->NumInstructions - 1; /* omit END instr */
568 const GLuint lenB = progB->NumInstructions;
569 const GLuint numParamsA = _mesa_num_parameters(progA->Parameters);
570 const GLuint newLength = lenA + lenB;
Brianfb9cf482007-10-30 18:26:34 -0600571 GLbitfield inputsB;
Brian9ffd8892007-10-29 16:35:59 -0600572 GLuint i;
573
574 ASSERT(progA->Target == progB->Target);
575
576 newInst = _mesa_alloc_instructions(newLength);
577 if (!newInst)
578 return GL_FALSE;
579
580 _mesa_copy_instructions(newInst, progA->Instructions, lenA);
581 _mesa_copy_instructions(newInst + lenA, progB->Instructions, lenB);
582
583 /* adjust branch / instruction addresses for B's instructions */
584 for (i = 0; i < lenB; i++) {
585 newInst[lenA + i].BranchTarget += lenA;
586 }
587
588 newProg = ctx->Driver.NewProgram(ctx, progA->Target, 0);
589 newProg->Instructions = newInst;
590 newProg->NumInstructions = newLength;
591
592 if (newProg->Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian27cff442008-01-16 13:54:32 -0700593 struct gl_fragment_program *fprogA, *fprogB, *newFprog;
594 fprogA = (struct gl_fragment_program *) progA;
595 fprogB = (struct gl_fragment_program *) progB;
596 newFprog = (struct gl_fragment_program *) newProg;
597
598 newFprog->UsesKill = fprogA->UsesKill || fprogB->UsesKill;
599
Brian Paulcf7daba2008-03-25 12:27:32 -0600600 /* Connect color outputs of fprogA to color inputs of fprogB, via a
601 * new temporary register.
602 */
Brian9ffd8892007-10-29 16:35:59 -0600603 if ((progA->OutputsWritten & (1 << FRAG_RESULT_COLR)) &&
604 (progB->InputsRead & (1 << FRAG_ATTRIB_COL0))) {
Brian Paulcf7daba2008-03-25 12:27:32 -0600605 GLint tempReg = _mesa_find_free_register(newProg, PROGRAM_TEMPORARY);
606 if (!tempReg) {
607 _mesa_problem(ctx, "No free temp regs found in "
608 "_mesa_combine_programs(), using 31");
609 tempReg = 31;
610 }
611 /* replace writes to result.color[0] with tempReg */
612 replace_registers(newInst, lenA,
613 PROGRAM_OUTPUT, FRAG_RESULT_COLR,
614 PROGRAM_TEMPORARY, tempReg);
615 /* replace reads from input.color[0] with tempReg */
Brian9ffd8892007-10-29 16:35:59 -0600616 replace_registers(newInst + lenA, lenB,
617 PROGRAM_INPUT, FRAG_ATTRIB_COL0,
Brian Paulcf7daba2008-03-25 12:27:32 -0600618 PROGRAM_TEMPORARY, tempReg);
Brian9ffd8892007-10-29 16:35:59 -0600619 }
620
Brianfb9cf482007-10-30 18:26:34 -0600621 inputsB = progB->InputsRead;
622 if (progA->OutputsWritten & (1 << FRAG_RESULT_COLR)) {
623 inputsB &= ~(1 << FRAG_ATTRIB_COL0);
624 }
625 newProg->InputsRead = progA->InputsRead | inputsB;
Brian9ffd8892007-10-29 16:35:59 -0600626 newProg->OutputsWritten = progB->OutputsWritten;
Brian Paul72f2c552008-04-04 11:20:44 -0600627 newProg->SamplersUsed = progA->SamplersUsed | progB->SamplersUsed;
Brian9ffd8892007-10-29 16:35:59 -0600628 }
629 else {
630 /* vertex program */
631 assert(0); /* XXX todo */
632 }
633
634 /*
635 * Merge parameters (uniforms, constants, etc)
636 */
637 newProg->Parameters = _mesa_combine_parameter_lists(progA->Parameters,
638 progB->Parameters);
639
640 adjust_param_indexes(newInst + lenA, lenB, numParamsA);
641
642
643 return newProg;
644}
645
646
647
648
649/**
650 * Scan the given program to find a free register of the given type.
651 * \param regFile - PROGRAM_INPUT, PROGRAM_OUTPUT or PROGRAM_TEMPORARY
652 */
653GLint
654_mesa_find_free_register(const struct gl_program *prog, GLuint regFile)
655{
656 GLboolean used[MAX_PROGRAM_TEMPS];
657 GLuint i, k;
658
659 assert(regFile == PROGRAM_INPUT ||
660 regFile == PROGRAM_OUTPUT ||
661 regFile == PROGRAM_TEMPORARY);
662
663 _mesa_memset(used, 0, sizeof(used));
664
665 for (i = 0; i < prog->NumInstructions; i++) {
666 const struct prog_instruction *inst = prog->Instructions + i;
667 const GLuint n = _mesa_num_inst_src_regs(inst->Opcode);
668
669 for (k = 0; k < n; k++) {
670 if (inst->SrcReg[k].File == regFile) {
671 used[inst->SrcReg[k].Index] = GL_TRUE;
672 }
673 }
674 }
675
676 for (i = 0; i < MAX_PROGRAM_TEMPS; i++) {
677 if (!used[i])
678 return i;
679 }
680
681 return -1;
682}
683
684
685
686/**
Brian Paul77427a12006-08-24 23:11:39 +0000687 * Mixing ARB and NV vertex/fragment programs can be tricky.
688 * Note: GL_VERTEX_PROGRAM_ARB == GL_VERTEX_PROGRAM_NV
689 * but, GL_FRAGMENT_PROGRAM_ARB != GL_FRAGMENT_PROGRAM_NV
690 * The two different fragment program targets are supposed to be compatible
691 * to some extent (see GL_ARB_fragment_program spec).
692 * This function does the compatibility check.
693 */
694static GLboolean
695compatible_program_targets(GLenum t1, GLenum t2)
696{
697 if (t1 == t2)
698 return GL_TRUE;
699 if (t1 == GL_FRAGMENT_PROGRAM_ARB && t2 == GL_FRAGMENT_PROGRAM_NV)
700 return GL_TRUE;
701 if (t1 == GL_FRAGMENT_PROGRAM_NV && t2 == GL_FRAGMENT_PROGRAM_ARB)
702 return GL_TRUE;
703 return GL_FALSE;
704}
705
706
Brian Paul30d6a4b2005-11-05 20:18:18 +0000707
Michal Krol2861e732004-03-29 11:09:34 +0000708/**********************************************************************/
709/* API functions */
710/**********************************************************************/
711
712
713/**
714 * Bind a program (make it current)
715 * \note Called from the GL API dispatcher by both glBindProgramNV
716 * and glBindProgramARB.
717 */
718void GLAPIENTRY
719_mesa_BindProgram(GLenum target, GLuint id)
720{
Brian Paula360bc32006-08-25 17:18:56 +0000721 struct gl_program *curProg, *newProg;
Michal Krol2861e732004-03-29 11:09:34 +0000722 GET_CURRENT_CONTEXT(ctx);
723 ASSERT_OUTSIDE_BEGIN_END(ctx);
724
725 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
726
Brian Paula360bc32006-08-25 17:18:56 +0000727 /* Error-check target and get curProg */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000728 if ((target == GL_VERTEX_PROGRAM_ARB) && /* == GL_VERTEX_PROGRAM_NV */
729 (ctx->Extensions.NV_vertex_program ||
730 ctx->Extensions.ARB_vertex_program)) {
Brian Paula360bc32006-08-25 17:18:56 +0000731 curProg = &ctx->VertexProgram.Current->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000732 }
733 else if ((target == GL_FRAGMENT_PROGRAM_NV
734 && ctx->Extensions.NV_fragment_program) ||
735 (target == GL_FRAGMENT_PROGRAM_ARB
736 && ctx->Extensions.ARB_fragment_program)) {
Brian Paula360bc32006-08-25 17:18:56 +0000737 curProg = &ctx->FragmentProgram.Current->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000738 }
739 else {
740 _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)");
741 return;
742 }
743
Brian Paula360bc32006-08-25 17:18:56 +0000744 /*
745 * Get pointer to new program to bind.
746 * NOTE: binding to a non-existant program is not an error.
Michal Krol2861e732004-03-29 11:09:34 +0000747 * That's supposed to be caught in glBegin.
748 */
749 if (id == 0) {
Brian Paula360bc32006-08-25 17:18:56 +0000750 /* Bind a default program */
751 newProg = NULL;
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000752 if (target == GL_VERTEX_PROGRAM_ARB) /* == GL_VERTEX_PROGRAM_NV */
Brian Paula56a59c2008-05-07 08:55:33 -0600753 newProg = &ctx->Shared->DefaultVertexProgram->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000754 else
Brian Paula56a59c2008-05-07 08:55:33 -0600755 newProg = &ctx->Shared->DefaultFragmentProgram->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000756 }
757 else {
Brian Paula360bc32006-08-25 17:18:56 +0000758 /* Bind a user program */
759 newProg = _mesa_lookup_program(ctx, id);
760 if (!newProg || newProg == &_mesa_DummyProgram) {
Michal Krol2861e732004-03-29 11:09:34 +0000761 /* allocate a new program now */
Brian Paula360bc32006-08-25 17:18:56 +0000762 newProg = ctx->Driver.NewProgram(ctx, target, id);
763 if (!newProg) {
Michal Krol2861e732004-03-29 11:09:34 +0000764 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB");
765 return;
766 }
Brian Paula360bc32006-08-25 17:18:56 +0000767 _mesa_HashInsert(ctx->Shared->Programs, id, newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000768 }
Brian Paula360bc32006-08-25 17:18:56 +0000769 else if (!compatible_program_targets(newProg->Target, target)) {
Brian Paul765f1a12004-09-14 22:28:27 +0000770 _mesa_error(ctx, GL_INVALID_OPERATION,
771 "glBindProgramNV/ARB(target mismatch)");
772 return;
773 }
Michal Krol2861e732004-03-29 11:09:34 +0000774 }
775
Brian Paula360bc32006-08-25 17:18:56 +0000776 /** All error checking is complete now **/
777
778 if (curProg->Id == id) {
779 /* binding same program - no change */
780 return;
781 }
782
Brian Paula360bc32006-08-25 17:18:56 +0000783 /* bind newProg */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000784 if (target == GL_VERTEX_PROGRAM_ARB) { /* == GL_VERTEX_PROGRAM_NV */
Brian Paula56a59c2008-05-07 08:55:33 -0600785 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
786 (struct gl_vertex_program *) newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000787 }
Brian Paula360bc32006-08-25 17:18:56 +0000788 else if (target == GL_FRAGMENT_PROGRAM_NV ||
789 target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paula56a59c2008-05-07 08:55:33 -0600790 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
791 (struct gl_fragment_program *) newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000792 }
793
Brian Paul765f1a12004-09-14 22:28:27 +0000794 /* Never null pointers */
795 ASSERT(ctx->VertexProgram.Current);
796 ASSERT(ctx->FragmentProgram.Current);
797
Michal Krol2861e732004-03-29 11:09:34 +0000798 if (ctx->Driver.BindProgram)
Brian Paula360bc32006-08-25 17:18:56 +0000799 ctx->Driver.BindProgram(ctx, target, newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000800}
801
802
803/**
804 * Delete a list of programs.
805 * \note Not compiled into display lists.
806 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
807 */
808void GLAPIENTRY
809_mesa_DeletePrograms(GLsizei n, const GLuint *ids)
810{
811 GLint i;
812 GET_CURRENT_CONTEXT(ctx);
813 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
814
815 if (n < 0) {
816 _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" );
817 return;
818 }
819
820 for (i = 0; i < n; i++) {
821 if (ids[i] != 0) {
Brian Paul4d12a052006-08-23 23:10:14 +0000822 struct gl_program *prog = _mesa_lookup_program(ctx, ids[i]);
Brian Paul9ca83922004-10-02 15:16:59 +0000823 if (prog == &_mesa_DummyProgram) {
Brian Paul765f1a12004-09-14 22:28:27 +0000824 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
825 }
826 else if (prog) {
827 /* Unbind program if necessary */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000828 if (prog->Target == GL_VERTEX_PROGRAM_ARB || /* == GL_VERTEX_PROGRAM_NV */
Michal Krol2861e732004-03-29 11:09:34 +0000829 prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
830 if (ctx->VertexProgram.Current &&
831 ctx->VertexProgram.Current->Base.Id == ids[i]) {
832 /* unbind this currently bound program */
833 _mesa_BindProgram(prog->Target, 0);
834 }
835 }
836 else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
837 prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
838 if (ctx->FragmentProgram.Current &&
839 ctx->FragmentProgram.Current->Base.Id == ids[i]) {
840 /* unbind this currently bound program */
841 _mesa_BindProgram(prog->Target, 0);
842 }
843 }
844 else {
845 _mesa_problem(ctx, "bad target in glDeleteProgramsNV");
846 return;
847 }
Brian Paulea2943e2005-01-20 04:02:02 +0000848 /* The ID is immediately available for re-use now */
849 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
Brian Paula56a59c2008-05-07 08:55:33 -0600850 _mesa_reference_program(ctx, &prog, NULL);
Michal Krol2861e732004-03-29 11:09:34 +0000851 }
Michal Krol2861e732004-03-29 11:09:34 +0000852 }
853 }
854}
855
856
857/**
858 * Generate a list of new program identifiers.
859 * \note Not compiled into display lists.
860 * \note Called by both glGenProgramsNV and glGenProgramsARB.
861 */
862void GLAPIENTRY
863_mesa_GenPrograms(GLsizei n, GLuint *ids)
864{
865 GLuint first;
866 GLuint i;
867 GET_CURRENT_CONTEXT(ctx);
868 ASSERT_OUTSIDE_BEGIN_END(ctx);
869
870 if (n < 0) {
871 _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms");
872 return;
873 }
874
875 if (!ids)
876 return;
877
878 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n);
879
Brian Paul765f1a12004-09-14 22:28:27 +0000880 /* Insert pointer to dummy program as placeholder */
Michal Krol2861e732004-03-29 11:09:34 +0000881 for (i = 0; i < (GLuint) n; i++) {
Brian Paul9ca83922004-10-02 15:16:59 +0000882 _mesa_HashInsert(ctx->Shared->Programs, first + i, &_mesa_DummyProgram);
Michal Krol2861e732004-03-29 11:09:34 +0000883 }
884
885 /* Return the program names */
886 for (i = 0; i < (GLuint) n; i++) {
887 ids[i] = first + i;
888 }
889}