fixes to _mesa_combine_programs(), from gallium-0.1
[mesa.git] / src / mesa / shader / program.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.3
4 *
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 *
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"
35 #include "program.h"
36 #include "prog_parameter.h"
37 #include "prog_instruction.h"
38
39
40 /**
41 * A pointer to this dummy program is put into the hash table when
42 * glGenPrograms is called.
43 */
44 struct gl_program _mesa_DummyProgram;
45
46
47 /**
48 * Init context's vertex/fragment program state
49 */
50 void
51 _mesa_init_program(GLcontext *ctx)
52 {
53 GLuint i;
54
55 ctx->Program.ErrorPos = -1;
56 ctx->Program.ErrorString = _mesa_strdup("");
57
58 #if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
59 ctx->VertexProgram.Enabled = GL_FALSE;
60 ctx->VertexProgram.PointSizeEnabled = GL_FALSE;
61 ctx->VertexProgram.TwoSideEnabled = GL_FALSE;
62 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
63 ctx->Shared->DefaultVertexProgram);
64 assert(ctx->VertexProgram.Current);
65 for (i = 0; i < MAX_NV_VERTEX_PROGRAM_PARAMS / 4; i++) {
66 ctx->VertexProgram.TrackMatrix[i] = GL_NONE;
67 ctx->VertexProgram.TrackMatrixTransform[i] = GL_IDENTITY_NV;
68 }
69 #endif
70
71 #if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
72 ctx->FragmentProgram.Enabled = GL_FALSE;
73 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
74 ctx->Shared->DefaultFragmentProgram);
75 assert(ctx->FragmentProgram.Current);
76 #endif
77
78 /* XXX probably move this stuff */
79 #if FEATURE_ATI_fragment_shader
80 ctx->ATIFragmentShader.Enabled = GL_FALSE;
81 ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader;
82 assert(ctx->ATIFragmentShader.Current);
83 ctx->ATIFragmentShader.Current->RefCount++;
84 #endif
85 }
86
87
88 /**
89 * Free a context's vertex/fragment program state
90 */
91 void
92 _mesa_free_program_data(GLcontext *ctx)
93 {
94 #if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
95 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current, NULL);
96 #endif
97 #if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
98 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current, NULL);
99 #endif
100 /* XXX probably move this stuff */
101 #if FEATURE_ATI_fragment_shader
102 if (ctx->ATIFragmentShader.Current) {
103 ctx->ATIFragmentShader.Current->RefCount--;
104 if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
105 _mesa_free(ctx->ATIFragmentShader.Current);
106 }
107 }
108 #endif
109 _mesa_free((void *) ctx->Program.ErrorString);
110 }
111
112
113 /**
114 * Update the default program objects in the given context to reference those
115 * specified in the shared state and release those referencing the old
116 * shared state.
117 */
118 void
119 _mesa_update_default_objects_program(GLcontext *ctx)
120 {
121 #if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
122 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
123 (struct gl_vertex_program *)
124 ctx->Shared->DefaultVertexProgram);
125 assert(ctx->VertexProgram.Current);
126 #endif
127
128 #if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
129 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
130 (struct gl_fragment_program *)
131 ctx->Shared->DefaultFragmentProgram);
132 assert(ctx->FragmentProgram.Current);
133 #endif
134
135 /* XXX probably move this stuff */
136 #if FEATURE_ATI_fragment_shader
137 if (ctx->ATIFragmentShader.Current) {
138 ctx->ATIFragmentShader.Current->RefCount--;
139 if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
140 _mesa_free(ctx->ATIFragmentShader.Current);
141 }
142 }
143 ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader;
144 assert(ctx->ATIFragmentShader.Current);
145 ctx->ATIFragmentShader.Current->RefCount++;
146 #endif
147 }
148
149
150 /**
151 * Set the vertex/fragment program error state (position and error string).
152 * This is generally called from within the parsers.
153 */
154 void
155 _mesa_set_program_error(GLcontext *ctx, GLint pos, const char *string)
156 {
157 ctx->Program.ErrorPos = pos;
158 _mesa_free((void *) ctx->Program.ErrorString);
159 if (!string)
160 string = "";
161 ctx->Program.ErrorString = _mesa_strdup(string);
162 }
163
164
165 /**
166 * Find the line number and column for 'pos' within 'string'.
167 * Return a copy of the line which contains 'pos'. Free the line with
168 * _mesa_free().
169 * \param string the program string
170 * \param pos the position within the string
171 * \param line returns the line number corresponding to 'pos'.
172 * \param col returns the column number corresponding to 'pos'.
173 * \return copy of the line containing 'pos'.
174 */
175 const GLubyte *
176 _mesa_find_line_column(const GLubyte *string, const GLubyte *pos,
177 GLint *line, GLint *col)
178 {
179 const GLubyte *lineStart = string;
180 const GLubyte *p = string;
181 GLubyte *s;
182 int len;
183
184 *line = 1;
185
186 while (p != pos) {
187 if (*p == (GLubyte) '\n') {
188 (*line)++;
189 lineStart = p + 1;
190 }
191 p++;
192 }
193
194 *col = (pos - lineStart) + 1;
195
196 /* return copy of this line */
197 while (*p != 0 && *p != '\n')
198 p++;
199 len = p - lineStart;
200 s = (GLubyte *) _mesa_malloc(len + 1);
201 _mesa_memcpy(s, lineStart, len);
202 s[len] = 0;
203
204 return s;
205 }
206
207
208 /**
209 * Initialize a new vertex/fragment program object.
210 */
211 static struct gl_program *
212 _mesa_init_program_struct( GLcontext *ctx, struct gl_program *prog,
213 GLenum target, GLuint id)
214 {
215 (void) ctx;
216 if (prog) {
217 GLuint i;
218 _mesa_bzero(prog, sizeof(*prog));
219 prog->Id = id;
220 prog->Target = target;
221 prog->Resident = GL_TRUE;
222 prog->RefCount = 1;
223 prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
224
225 /* default mapping from samplers to texture units */
226 for (i = 0; i < MAX_SAMPLERS; i++)
227 prog->SamplerUnits[i] = i;
228 }
229
230 return prog;
231 }
232
233
234 /**
235 * Initialize a new fragment program object.
236 */
237 struct gl_program *
238 _mesa_init_fragment_program( GLcontext *ctx, struct gl_fragment_program *prog,
239 GLenum target, GLuint id)
240 {
241 if (prog)
242 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
243 else
244 return NULL;
245 }
246
247
248 /**
249 * Initialize a new vertex program object.
250 */
251 struct gl_program *
252 _mesa_init_vertex_program( GLcontext *ctx, struct gl_vertex_program *prog,
253 GLenum target, GLuint id)
254 {
255 if (prog)
256 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
257 else
258 return NULL;
259 }
260
261
262 /**
263 * Allocate and initialize a new fragment/vertex program object but
264 * don't put it into the program hash table. Called via
265 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
266 * device driver function to implement OO deriviation with additional
267 * types not understood by this function.
268 *
269 * \param ctx context
270 * \param id program id/number
271 * \param target program target/type
272 * \return pointer to new program object
273 */
274 struct gl_program *
275 _mesa_new_program(GLcontext *ctx, GLenum target, GLuint id)
276 {
277 struct gl_program *prog;
278 switch (target) {
279 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
280 prog = _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program),
281 target, id );
282 break;
283 case GL_FRAGMENT_PROGRAM_NV:
284 case GL_FRAGMENT_PROGRAM_ARB:
285 prog =_mesa_init_fragment_program(ctx,
286 CALLOC_STRUCT(gl_fragment_program),
287 target, id );
288 break;
289 default:
290 _mesa_problem(ctx, "bad target in _mesa_new_program");
291 prog = NULL;
292 }
293 return prog;
294 }
295
296
297 /**
298 * Delete a program and remove it from the hash table, ignoring the
299 * reference count.
300 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
301 * by a device driver function.
302 */
303 void
304 _mesa_delete_program(GLcontext *ctx, struct gl_program *prog)
305 {
306 (void) ctx;
307 ASSERT(prog);
308 ASSERT(prog->RefCount==0);
309
310 if (prog == &_mesa_DummyProgram)
311 return;
312
313 if (prog->String)
314 _mesa_free(prog->String);
315
316 _mesa_free_instructions(prog->Instructions, prog->NumInstructions);
317
318 if (prog->Parameters) {
319 _mesa_free_parameter_list(prog->Parameters);
320 }
321 if (prog->Varying) {
322 _mesa_free_parameter_list(prog->Varying);
323 }
324 if (prog->Attributes) {
325 _mesa_free_parameter_list(prog->Attributes);
326 }
327
328 /* XXX this is a little ugly */
329 if (prog->Target == GL_VERTEX_PROGRAM_ARB) {
330 struct gl_vertex_program *vprog = (struct gl_vertex_program *) prog;
331 if (vprog->TnlData)
332 _mesa_free(vprog->TnlData);
333 }
334
335 _mesa_free(prog);
336 }
337
338
339 /**
340 * Return the gl_program object for a given ID.
341 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
342 * casts elsewhere.
343 */
344 struct gl_program *
345 _mesa_lookup_program(GLcontext *ctx, GLuint id)
346 {
347 if (id)
348 return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id);
349 else
350 return NULL;
351 }
352
353
354 /**
355 * Reference counting for vertex/fragment programs
356 */
357 void
358 _mesa_reference_program(GLcontext *ctx,
359 struct gl_program **ptr,
360 struct gl_program *prog)
361 {
362 assert(ptr);
363 if (*ptr && prog) {
364 /* sanity check */
365 ASSERT((*ptr)->Target == prog->Target);
366 }
367 if (*ptr == prog) {
368 return; /* no change */
369 }
370 if (*ptr) {
371 GLboolean deleteFlag;
372
373 /*_glthread_LOCK_MUTEX((*ptr)->Mutex);*/
374 #if 0
375 printf("Program %p ID=%u Target=%s Refcount-- to %d\n",
376 *ptr, (*ptr)->Id,
377 ((*ptr)->Target == GL_VERTEX_PROGRAM_ARB ? "VP" : "FP"),
378 (*ptr)->RefCount - 1);
379 #endif
380 ASSERT((*ptr)->RefCount > 0);
381 (*ptr)->RefCount--;
382
383 deleteFlag = ((*ptr)->RefCount == 0);
384 /*_glthread_UNLOCK_MUTEX((*ptr)->Mutex);*/
385
386 if (deleteFlag) {
387 ASSERT(ctx);
388 ctx->Driver.DeleteProgram(ctx, *ptr);
389 }
390
391 *ptr = NULL;
392 }
393
394 assert(!*ptr);
395 if (prog) {
396 /*_glthread_LOCK_MUTEX(prog->Mutex);*/
397 prog->RefCount++;
398 #if 0
399 printf("Program %p ID=%u Target=%s Refcount++ to %d\n",
400 prog, prog->Id,
401 (prog->Target == GL_VERTEX_PROGRAM_ARB ? "VP" : "FP"),
402 prog->RefCount);
403 #endif
404 /*_glthread_UNLOCK_MUTEX(prog->Mutex);*/
405 }
406
407 *ptr = prog;
408 }
409
410
411 /**
412 * Return a copy of a program.
413 * XXX Problem here if the program object is actually OO-derivation
414 * made by a device driver.
415 */
416 struct gl_program *
417 _mesa_clone_program(GLcontext *ctx, const struct gl_program *prog)
418 {
419 struct gl_program *clone;
420
421 clone = ctx->Driver.NewProgram(ctx, prog->Target, prog->Id);
422 if (!clone)
423 return NULL;
424
425 assert(clone->Target == prog->Target);
426 assert(clone->RefCount == 1);
427
428 clone->String = (GLubyte *) _mesa_strdup((char *) prog->String);
429 clone->Format = prog->Format;
430 clone->Instructions = _mesa_alloc_instructions(prog->NumInstructions);
431 if (!clone->Instructions) {
432 _mesa_reference_program(ctx, &clone, NULL);
433 return NULL;
434 }
435 _mesa_copy_instructions(clone->Instructions, prog->Instructions,
436 prog->NumInstructions);
437 clone->InputsRead = prog->InputsRead;
438 clone->OutputsWritten = prog->OutputsWritten;
439 clone->SamplersUsed = prog->SamplersUsed;
440 memcpy(clone->TexturesUsed, prog->TexturesUsed, sizeof(prog->TexturesUsed));
441
442 if (prog->Parameters)
443 clone->Parameters = _mesa_clone_parameter_list(prog->Parameters);
444 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
445 if (prog->Varying)
446 clone->Varying = _mesa_clone_parameter_list(prog->Varying);
447 if (prog->Attributes)
448 clone->Attributes = _mesa_clone_parameter_list(prog->Attributes);
449 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
450 clone->NumInstructions = prog->NumInstructions;
451 clone->NumTemporaries = prog->NumTemporaries;
452 clone->NumParameters = prog->NumParameters;
453 clone->NumAttributes = prog->NumAttributes;
454 clone->NumAddressRegs = prog->NumAddressRegs;
455 clone->NumNativeInstructions = prog->NumNativeInstructions;
456 clone->NumNativeTemporaries = prog->NumNativeTemporaries;
457 clone->NumNativeParameters = prog->NumNativeParameters;
458 clone->NumNativeAttributes = prog->NumNativeAttributes;
459 clone->NumNativeAddressRegs = prog->NumNativeAddressRegs;
460 clone->NumAluInstructions = prog->NumAluInstructions;
461 clone->NumTexInstructions = prog->NumTexInstructions;
462 clone->NumTexIndirections = prog->NumTexIndirections;
463 clone->NumNativeAluInstructions = prog->NumNativeAluInstructions;
464 clone->NumNativeTexInstructions = prog->NumNativeTexInstructions;
465 clone->NumNativeTexIndirections = prog->NumNativeTexIndirections;
466
467 switch (prog->Target) {
468 case GL_VERTEX_PROGRAM_ARB:
469 {
470 const struct gl_vertex_program *vp
471 = (const struct gl_vertex_program *) prog;
472 struct gl_vertex_program *vpc = (struct gl_vertex_program *) clone;
473 vpc->IsPositionInvariant = vp->IsPositionInvariant;
474 }
475 break;
476 case GL_FRAGMENT_PROGRAM_ARB:
477 {
478 const struct gl_fragment_program *fp
479 = (const struct gl_fragment_program *) prog;
480 struct gl_fragment_program *fpc = (struct gl_fragment_program *) clone;
481 fpc->FogOption = fp->FogOption;
482 fpc->UsesKill = fp->UsesKill;
483 }
484 break;
485 default:
486 _mesa_problem(NULL, "Unexpected target in _mesa_clone_program");
487 }
488
489 return clone;
490 }
491
492
493 /**
494 * Insert 'count' NOP instructions at 'start' in the given program.
495 * Adjust branch targets accordingly.
496 */
497 GLboolean
498 _mesa_insert_instructions(struct gl_program *prog, GLuint start, GLuint count)
499 {
500 const GLuint origLen = prog->NumInstructions;
501 const GLuint newLen = origLen + count;
502 struct prog_instruction *newInst;
503 GLuint i;
504
505 /* adjust branches */
506 for (i = 0; i < prog->NumInstructions; i++) {
507 struct prog_instruction *inst = prog->Instructions + i;
508 if (inst->BranchTarget > 0) {
509 if (inst->BranchTarget >= start) {
510 inst->BranchTarget += count;
511 }
512 }
513 }
514
515 /* Alloc storage for new instructions */
516 newInst = _mesa_alloc_instructions(newLen);
517 if (!newInst) {
518 return GL_FALSE;
519 }
520
521 /* Copy 'start' instructions into new instruction buffer */
522 _mesa_copy_instructions(newInst, prog->Instructions, start);
523
524 /* init the new instructions */
525 _mesa_init_instructions(newInst + start, count);
526
527 /* Copy the remaining/tail instructions to new inst buffer */
528 _mesa_copy_instructions(newInst + start + count,
529 prog->Instructions + start,
530 origLen - start);
531
532 /* free old instructions */
533 _mesa_free_instructions(prog->Instructions, origLen);
534
535 /* install new instructions */
536 prog->Instructions = newInst;
537 prog->NumInstructions = newLen;
538
539 return GL_TRUE;
540 }
541
542
543 /**
544 * Search instructions for registers that match (oldFile, oldIndex),
545 * replacing them with (newFile, newIndex).
546 */
547 static void
548 replace_registers(struct prog_instruction *inst, GLuint numInst,
549 GLuint oldFile, GLuint oldIndex,
550 GLuint newFile, GLuint newIndex)
551 {
552 GLuint i, j;
553 for (i = 0; i < numInst; i++) {
554 /* src regs */
555 for (j = 0; j < _mesa_num_inst_src_regs(inst->Opcode); j++) {
556 if (inst[i].SrcReg[j].File == oldFile &&
557 inst[i].SrcReg[j].Index == oldIndex) {
558 inst[i].SrcReg[j].File = newFile;
559 inst[i].SrcReg[j].Index = newIndex;
560 }
561 }
562 /* dst reg */
563 if (inst[i].DstReg.File == oldFile && inst[i].DstReg.Index == oldIndex) {
564 inst[i].DstReg.File = newFile;
565 inst[i].DstReg.Index = newIndex;
566 }
567 }
568 }
569
570
571 /**
572 * Search instructions for references to program parameters. When found,
573 * increment the parameter index by 'offset'.
574 * Used when combining programs.
575 */
576 static void
577 adjust_param_indexes(struct prog_instruction *inst, GLuint numInst,
578 GLuint offset)
579 {
580 GLuint i, j;
581 for (i = 0; i < numInst; i++) {
582 for (j = 0; j < _mesa_num_inst_src_regs(inst->Opcode); j++) {
583 GLuint f = inst[i].SrcReg[j].File;
584 if (f == PROGRAM_CONSTANT ||
585 f == PROGRAM_UNIFORM ||
586 f == PROGRAM_STATE_VAR) {
587 inst[i].SrcReg[j].Index += offset;
588 }
589 }
590 }
591 }
592
593
594 /**
595 * Combine two programs into one. Fix instructions so the outputs of
596 * the first program go to the inputs of the second program.
597 */
598 struct gl_program *
599 _mesa_combine_programs(GLcontext *ctx,
600 const struct gl_program *progA,
601 const struct gl_program *progB)
602 {
603 struct prog_instruction *newInst;
604 struct gl_program *newProg;
605 const GLuint lenA = progA->NumInstructions - 1; /* omit END instr */
606 const GLuint lenB = progB->NumInstructions;
607 const GLuint numParamsA = _mesa_num_parameters(progA->Parameters);
608 const GLuint newLength = lenA + lenB;
609 GLbitfield inputsB;
610 GLuint i;
611
612 ASSERT(progA->Target == progB->Target);
613
614 newInst = _mesa_alloc_instructions(newLength);
615 if (!newInst)
616 return GL_FALSE;
617
618 _mesa_copy_instructions(newInst, progA->Instructions, lenA);
619 _mesa_copy_instructions(newInst + lenA, progB->Instructions, lenB);
620
621 /* adjust branch / instruction addresses for B's instructions */
622 for (i = 0; i < lenB; i++) {
623 newInst[lenA + i].BranchTarget += lenA;
624 }
625
626 newProg = ctx->Driver.NewProgram(ctx, progA->Target, 0);
627 newProg->Instructions = newInst;
628 newProg->NumInstructions = newLength;
629
630 if (newProg->Target == GL_FRAGMENT_PROGRAM_ARB) {
631 struct gl_fragment_program *fprogA, *fprogB, *newFprog;
632 fprogA = (struct gl_fragment_program *) progA;
633 fprogB = (struct gl_fragment_program *) progB;
634 newFprog = (struct gl_fragment_program *) newProg;
635
636 newFprog->UsesKill = fprogA->UsesKill || fprogB->UsesKill;
637
638 /* Connect color outputs of fprogA to color inputs of fprogB, via a
639 * new temporary register.
640 */
641 if ((progA->OutputsWritten & (1 << FRAG_RESULT_COLR)) &&
642 (progB->InputsRead & (1 << FRAG_ATTRIB_COL0))) {
643 GLint tempReg = _mesa_find_free_register(newProg, PROGRAM_TEMPORARY);
644 if (!tempReg) {
645 _mesa_problem(ctx, "No free temp regs found in "
646 "_mesa_combine_programs(), using 31");
647 tempReg = 31;
648 }
649 /* replace writes to result.color[0] with tempReg */
650 replace_registers(newInst, lenA,
651 PROGRAM_OUTPUT, FRAG_RESULT_COLR,
652 PROGRAM_TEMPORARY, tempReg);
653 /* replace reads from input.color[0] with tempReg */
654 replace_registers(newInst + lenA, lenB,
655 PROGRAM_INPUT, FRAG_ATTRIB_COL0,
656 PROGRAM_TEMPORARY, tempReg);
657 }
658
659 inputsB = progB->InputsRead;
660 if (progA->OutputsWritten & (1 << FRAG_RESULT_COLR)) {
661 inputsB &= ~(1 << FRAG_ATTRIB_COL0);
662 }
663 newProg->InputsRead = progA->InputsRead | inputsB;
664 newProg->OutputsWritten = progB->OutputsWritten;
665 newProg->SamplersUsed = progA->SamplersUsed | progB->SamplersUsed;
666 }
667 else {
668 /* vertex program */
669 assert(0); /* XXX todo */
670 }
671
672 /*
673 * Merge parameters (uniforms, constants, etc)
674 */
675 newProg->Parameters = _mesa_combine_parameter_lists(progA->Parameters,
676 progB->Parameters);
677
678 adjust_param_indexes(newInst + lenA, lenB, numParamsA);
679
680
681 return newProg;
682 }
683
684
685
686
687 /**
688 * Scan the given program to find a free register of the given type.
689 * \param regFile - PROGRAM_INPUT, PROGRAM_OUTPUT or PROGRAM_TEMPORARY
690 */
691 GLint
692 _mesa_find_free_register(const struct gl_program *prog, GLuint regFile)
693 {
694 GLboolean used[MAX_PROGRAM_TEMPS];
695 GLuint i, k;
696
697 assert(regFile == PROGRAM_INPUT ||
698 regFile == PROGRAM_OUTPUT ||
699 regFile == PROGRAM_TEMPORARY);
700
701 _mesa_memset(used, 0, sizeof(used));
702
703 for (i = 0; i < prog->NumInstructions; i++) {
704 const struct prog_instruction *inst = prog->Instructions + i;
705 const GLuint n = _mesa_num_inst_src_regs(inst->Opcode);
706
707 for (k = 0; k < n; k++) {
708 if (inst->SrcReg[k].File == regFile) {
709 used[inst->SrcReg[k].Index] = GL_TRUE;
710 }
711 }
712 }
713
714 for (i = 0; i < MAX_PROGRAM_TEMPS; i++) {
715 if (!used[i])
716 return i;
717 }
718
719 return -1;
720 }
721
722
723
724 /**
725 * Mixing ARB and NV vertex/fragment programs can be tricky.
726 * Note: GL_VERTEX_PROGRAM_ARB == GL_VERTEX_PROGRAM_NV
727 * but, GL_FRAGMENT_PROGRAM_ARB != GL_FRAGMENT_PROGRAM_NV
728 * The two different fragment program targets are supposed to be compatible
729 * to some extent (see GL_ARB_fragment_program spec).
730 * This function does the compatibility check.
731 */
732 static GLboolean
733 compatible_program_targets(GLenum t1, GLenum t2)
734 {
735 if (t1 == t2)
736 return GL_TRUE;
737 if (t1 == GL_FRAGMENT_PROGRAM_ARB && t2 == GL_FRAGMENT_PROGRAM_NV)
738 return GL_TRUE;
739 if (t1 == GL_FRAGMENT_PROGRAM_NV && t2 == GL_FRAGMENT_PROGRAM_ARB)
740 return GL_TRUE;
741 return GL_FALSE;
742 }
743
744
745
746 /**********************************************************************/
747 /* API functions */
748 /**********************************************************************/
749
750
751 /**
752 * Bind a program (make it current)
753 * \note Called from the GL API dispatcher by both glBindProgramNV
754 * and glBindProgramARB.
755 */
756 void GLAPIENTRY
757 _mesa_BindProgram(GLenum target, GLuint id)
758 {
759 struct gl_program *curProg, *newProg;
760 GET_CURRENT_CONTEXT(ctx);
761 ASSERT_OUTSIDE_BEGIN_END(ctx);
762
763 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
764
765 /* Error-check target and get curProg */
766 if ((target == GL_VERTEX_PROGRAM_ARB) && /* == GL_VERTEX_PROGRAM_NV */
767 (ctx->Extensions.NV_vertex_program ||
768 ctx->Extensions.ARB_vertex_program)) {
769 curProg = &ctx->VertexProgram.Current->Base;
770 }
771 else if ((target == GL_FRAGMENT_PROGRAM_NV
772 && ctx->Extensions.NV_fragment_program) ||
773 (target == GL_FRAGMENT_PROGRAM_ARB
774 && ctx->Extensions.ARB_fragment_program)) {
775 curProg = &ctx->FragmentProgram.Current->Base;
776 }
777 else {
778 _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)");
779 return;
780 }
781
782 /*
783 * Get pointer to new program to bind.
784 * NOTE: binding to a non-existant program is not an error.
785 * That's supposed to be caught in glBegin.
786 */
787 if (id == 0) {
788 /* Bind a default program */
789 newProg = NULL;
790 if (target == GL_VERTEX_PROGRAM_ARB) /* == GL_VERTEX_PROGRAM_NV */
791 newProg = &ctx->Shared->DefaultVertexProgram->Base;
792 else
793 newProg = &ctx->Shared->DefaultFragmentProgram->Base;
794 }
795 else {
796 /* Bind a user program */
797 newProg = _mesa_lookup_program(ctx, id);
798 if (!newProg || newProg == &_mesa_DummyProgram) {
799 /* allocate a new program now */
800 newProg = ctx->Driver.NewProgram(ctx, target, id);
801 if (!newProg) {
802 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB");
803 return;
804 }
805 _mesa_HashInsert(ctx->Shared->Programs, id, newProg);
806 }
807 else if (!compatible_program_targets(newProg->Target, target)) {
808 _mesa_error(ctx, GL_INVALID_OPERATION,
809 "glBindProgramNV/ARB(target mismatch)");
810 return;
811 }
812 }
813
814 /** All error checking is complete now **/
815
816 if (curProg->Id == id) {
817 /* binding same program - no change */
818 return;
819 }
820
821 /* bind newProg */
822 if (target == GL_VERTEX_PROGRAM_ARB) { /* == GL_VERTEX_PROGRAM_NV */
823 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
824 (struct gl_vertex_program *) newProg);
825 }
826 else if (target == GL_FRAGMENT_PROGRAM_NV ||
827 target == GL_FRAGMENT_PROGRAM_ARB) {
828 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
829 (struct gl_fragment_program *) newProg);
830 }
831
832 /* Never null pointers */
833 ASSERT(ctx->VertexProgram.Current);
834 ASSERT(ctx->FragmentProgram.Current);
835
836 if (ctx->Driver.BindProgram)
837 ctx->Driver.BindProgram(ctx, target, newProg);
838 }
839
840
841 /**
842 * Delete a list of programs.
843 * \note Not compiled into display lists.
844 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
845 */
846 void GLAPIENTRY
847 _mesa_DeletePrograms(GLsizei n, const GLuint *ids)
848 {
849 GLint i;
850 GET_CURRENT_CONTEXT(ctx);
851 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
852
853 if (n < 0) {
854 _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" );
855 return;
856 }
857
858 for (i = 0; i < n; i++) {
859 if (ids[i] != 0) {
860 struct gl_program *prog = _mesa_lookup_program(ctx, ids[i]);
861 if (prog == &_mesa_DummyProgram) {
862 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
863 }
864 else if (prog) {
865 /* Unbind program if necessary */
866 if (prog->Target == GL_VERTEX_PROGRAM_ARB || /* == GL_VERTEX_PROGRAM_NV */
867 prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
868 if (ctx->VertexProgram.Current &&
869 ctx->VertexProgram.Current->Base.Id == ids[i]) {
870 /* unbind this currently bound program */
871 _mesa_BindProgram(prog->Target, 0);
872 }
873 }
874 else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
875 prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
876 if (ctx->FragmentProgram.Current &&
877 ctx->FragmentProgram.Current->Base.Id == ids[i]) {
878 /* unbind this currently bound program */
879 _mesa_BindProgram(prog->Target, 0);
880 }
881 }
882 else {
883 _mesa_problem(ctx, "bad target in glDeleteProgramsNV");
884 return;
885 }
886 /* The ID is immediately available for re-use now */
887 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
888 _mesa_reference_program(ctx, &prog, NULL);
889 }
890 }
891 }
892 }
893
894
895 /**
896 * Generate a list of new program identifiers.
897 * \note Not compiled into display lists.
898 * \note Called by both glGenProgramsNV and glGenProgramsARB.
899 */
900 void GLAPIENTRY
901 _mesa_GenPrograms(GLsizei n, GLuint *ids)
902 {
903 GLuint first;
904 GLuint i;
905 GET_CURRENT_CONTEXT(ctx);
906 ASSERT_OUTSIDE_BEGIN_END(ctx);
907
908 if (n < 0) {
909 _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms");
910 return;
911 }
912
913 if (!ids)
914 return;
915
916 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n);
917
918 /* Insert pointer to dummy program as placeholder */
919 for (i = 0; i < (GLuint) n; i++) {
920 _mesa_HashInsert(ctx->Shared->Programs, first + i, &_mesa_DummyProgram);
921 }
922
923 /* Return the program names */
924 for (i = 0; i < (GLuint) n; i++) {
925 ids[i] = first + i;
926 }
927 }