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