2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
27 * Vertex and fragment program support functions.
36 #include "prog_cache.h"
37 #include "prog_parameter.h"
38 #include "prog_instruction.h"
42 * A pointer to this dummy program is put into the hash table when
43 * glGenPrograms is called.
45 struct gl_program _mesa_DummyProgram
;
49 * Init context's vertex/fragment program state
52 _mesa_init_program(GLcontext
*ctx
)
56 ctx
->Program
.ErrorPos
= -1;
57 ctx
->Program
.ErrorString
= _mesa_strdup("");
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
;
63 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
64 ctx
->Shared
->DefaultVertexProgram
);
65 assert(ctx
->VertexProgram
.Current
);
66 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
;
70 ctx
->VertexProgram
.Cache
= _mesa_new_program_cache();
73 #if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
74 ctx
->FragmentProgram
.Enabled
= GL_FALSE
;
75 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
76 ctx
->Shared
->DefaultFragmentProgram
);
77 assert(ctx
->FragmentProgram
.Current
);
78 ctx
->FragmentProgram
.Cache
= _mesa_new_program_cache();
82 /* XXX probably move this stuff */
83 #if FEATURE_ATI_fragment_shader
84 ctx
->ATIFragmentShader
.Enabled
= GL_FALSE
;
85 ctx
->ATIFragmentShader
.Current
= ctx
->Shared
->DefaultFragmentShader
;
86 assert(ctx
->ATIFragmentShader
.Current
);
87 ctx
->ATIFragmentShader
.Current
->RefCount
++;
93 * Free a context's vertex/fragment program state
96 _mesa_free_program_data(GLcontext
*ctx
)
98 #if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
99 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
, NULL
);
100 _mesa_delete_program_cache(ctx
, ctx
->VertexProgram
.Cache
);
102 #if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
103 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
, NULL
);
104 _mesa_delete_program_cache(ctx
, ctx
->FragmentProgram
.Cache
);
106 /* XXX probably move this stuff */
107 #if FEATURE_ATI_fragment_shader
108 if (ctx
->ATIFragmentShader
.Current
) {
109 ctx
->ATIFragmentShader
.Current
->RefCount
--;
110 if (ctx
->ATIFragmentShader
.Current
->RefCount
<= 0) {
111 _mesa_free(ctx
->ATIFragmentShader
.Current
);
115 _mesa_free((void *) ctx
->Program
.ErrorString
);
120 * Set the vertex/fragment program error state (position and error string).
121 * This is generally called from within the parsers.
124 _mesa_set_program_error(GLcontext
*ctx
, GLint pos
, const char *string
)
126 ctx
->Program
.ErrorPos
= pos
;
127 _mesa_free((void *) ctx
->Program
.ErrorString
);
130 ctx
->Program
.ErrorString
= _mesa_strdup(string
);
135 * Find the line number and column for 'pos' within 'string'.
136 * Return a copy of the line which contains 'pos'. Free the line with
138 * \param string the program string
139 * \param pos the position within the string
140 * \param line returns the line number corresponding to 'pos'.
141 * \param col returns the column number corresponding to 'pos'.
142 * \return copy of the line containing 'pos'.
145 _mesa_find_line_column(const GLubyte
*string
, const GLubyte
*pos
,
146 GLint
*line
, GLint
*col
)
148 const GLubyte
*lineStart
= string
;
149 const GLubyte
*p
= string
;
156 if (*p
== (GLubyte
) '\n') {
163 *col
= (pos
- lineStart
) + 1;
165 /* return copy of this line */
166 while (*p
!= 0 && *p
!= '\n')
169 s
= (GLubyte
*) _mesa_malloc(len
+ 1);
170 _mesa_memcpy(s
, lineStart
, len
);
178 * Initialize a new vertex/fragment program object.
180 static struct gl_program
*
181 _mesa_init_program_struct( GLcontext
*ctx
, struct gl_program
*prog
,
182 GLenum target
, GLuint id
)
187 _mesa_bzero(prog
, sizeof(*prog
));
189 prog
->Target
= target
;
190 prog
->Resident
= GL_TRUE
;
192 prog
->Format
= GL_PROGRAM_FORMAT_ASCII_ARB
;
194 /* default mapping from samplers to texture units */
195 for (i
= 0; i
< MAX_SAMPLERS
; i
++)
196 prog
->SamplerUnits
[i
] = i
;
204 * Initialize a new fragment program object.
207 _mesa_init_fragment_program( GLcontext
*ctx
, struct gl_fragment_program
*prog
,
208 GLenum target
, GLuint id
)
211 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
218 * Initialize a new vertex program object.
221 _mesa_init_vertex_program( GLcontext
*ctx
, struct gl_vertex_program
*prog
,
222 GLenum target
, GLuint id
)
225 return _mesa_init_program_struct( ctx
, &prog
->Base
, target
, id
);
232 * Allocate and initialize a new fragment/vertex program object but
233 * don't put it into the program hash table. Called via
234 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
235 * device driver function to implement OO deriviation with additional
236 * types not understood by this function.
239 * \param id program id/number
240 * \param target program target/type
241 * \return pointer to new program object
244 _mesa_new_program(GLcontext
*ctx
, GLenum target
, GLuint id
)
247 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
248 return _mesa_init_vertex_program(ctx
, CALLOC_STRUCT(gl_vertex_program
),
250 case GL_FRAGMENT_PROGRAM_NV
:
251 case GL_FRAGMENT_PROGRAM_ARB
:
252 return _mesa_init_fragment_program(ctx
,
253 CALLOC_STRUCT(gl_fragment_program
),
256 _mesa_problem(ctx
, "bad target in _mesa_new_program");
263 * Delete a program and remove it from the hash table, ignoring the
265 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
266 * by a device driver function.
269 _mesa_delete_program(GLcontext
*ctx
, struct gl_program
*prog
)
273 ASSERT(prog
->RefCount
==0);
275 if (prog
== &_mesa_DummyProgram
)
279 _mesa_free(prog
->String
);
281 _mesa_free_instructions(prog
->Instructions
, prog
->NumInstructions
);
283 if (prog
->Parameters
) {
284 _mesa_free_parameter_list(prog
->Parameters
);
287 _mesa_free_parameter_list(prog
->Varying
);
289 if (prog
->Attributes
) {
290 _mesa_free_parameter_list(prog
->Attributes
);
293 /* XXX this is a little ugly */
294 if (prog
->Target
== GL_VERTEX_PROGRAM_ARB
) {
295 struct gl_vertex_program
*vprog
= (struct gl_vertex_program
*) prog
;
297 _mesa_free(vprog
->TnlData
);
305 * Return the gl_program object for a given ID.
306 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
310 _mesa_lookup_program(GLcontext
*ctx
, GLuint id
)
313 return (struct gl_program
*) _mesa_HashLookup(ctx
->Shared
->Programs
, id
);
320 * Reference counting for vertex/fragment programs
323 _mesa_reference_program(GLcontext
*ctx
,
324 struct gl_program
**ptr
,
325 struct gl_program
*prog
)
330 ASSERT((*ptr
)->Target
== prog
->Target
);
333 return; /* no change */
336 GLboolean deleteFlag
;
338 /*_glthread_LOCK_MUTEX((*ptr)->Mutex);*/
340 printf("Program %p %u 0x%x Refcount-- to %d\n",
341 *ptr
, (*ptr
)->Id
, (*ptr
)->Target
, (*ptr
)->RefCount
- 1);
343 ASSERT((*ptr
)->RefCount
> 0);
346 deleteFlag
= ((*ptr
)->RefCount
== 0);
347 /*_glthread_UNLOCK_MUTEX((*ptr)->Mutex);*/
351 ctx
->Driver
.DeleteProgram(ctx
, *ptr
);
359 /*_glthread_LOCK_MUTEX(prog->Mutex);*/
362 printf("Program %p %u 0x%x Refcount++ to %d\n",
363 prog
, prog
->Id
, prog
->Target
, prog
->RefCount
);
365 /*_glthread_UNLOCK_MUTEX(prog->Mutex);*/
373 * Return a copy of a program.
374 * XXX Problem here if the program object is actually OO-derivation
375 * made by a device driver.
378 _mesa_clone_program(GLcontext
*ctx
, const struct gl_program
*prog
)
380 struct gl_program
*clone
;
382 clone
= ctx
->Driver
.NewProgram(ctx
, prog
->Target
, prog
->Id
);
386 assert(clone
->Target
== prog
->Target
);
387 assert(clone
->RefCount
== 1);
389 clone
->String
= (GLubyte
*) _mesa_strdup((char *) prog
->String
);
390 clone
->Format
= prog
->Format
;
391 clone
->Instructions
= _mesa_alloc_instructions(prog
->NumInstructions
);
392 if (!clone
->Instructions
) {
393 _mesa_reference_program(ctx
, &clone
, NULL
);
396 _mesa_copy_instructions(clone
->Instructions
, prog
->Instructions
,
397 prog
->NumInstructions
);
398 clone
->InputsRead
= prog
->InputsRead
;
399 clone
->OutputsWritten
= prog
->OutputsWritten
;
400 clone
->SamplersUsed
= prog
->SamplersUsed
;
401 memcpy(clone
->TexturesUsed
, prog
->TexturesUsed
, sizeof(prog
->TexturesUsed
));
403 if (prog
->Parameters
)
404 clone
->Parameters
= _mesa_clone_parameter_list(prog
->Parameters
);
405 memcpy(clone
->LocalParams
, prog
->LocalParams
, sizeof(clone
->LocalParams
));
407 clone
->Varying
= _mesa_clone_parameter_list(prog
->Varying
);
408 if (prog
->Attributes
)
409 clone
->Attributes
= _mesa_clone_parameter_list(prog
->Attributes
);
410 memcpy(clone
->LocalParams
, prog
->LocalParams
, sizeof(clone
->LocalParams
));
411 clone
->NumInstructions
= prog
->NumInstructions
;
412 clone
->NumTemporaries
= prog
->NumTemporaries
;
413 clone
->NumParameters
= prog
->NumParameters
;
414 clone
->NumAttributes
= prog
->NumAttributes
;
415 clone
->NumAddressRegs
= prog
->NumAddressRegs
;
416 clone
->NumNativeInstructions
= prog
->NumNativeInstructions
;
417 clone
->NumNativeTemporaries
= prog
->NumNativeTemporaries
;
418 clone
->NumNativeParameters
= prog
->NumNativeParameters
;
419 clone
->NumNativeAttributes
= prog
->NumNativeAttributes
;
420 clone
->NumNativeAddressRegs
= prog
->NumNativeAddressRegs
;
421 clone
->NumAluInstructions
= prog
->NumAluInstructions
;
422 clone
->NumTexInstructions
= prog
->NumTexInstructions
;
423 clone
->NumTexIndirections
= prog
->NumTexIndirections
;
424 clone
->NumNativeAluInstructions
= prog
->NumNativeAluInstructions
;
425 clone
->NumNativeTexInstructions
= prog
->NumNativeTexInstructions
;
426 clone
->NumNativeTexIndirections
= prog
->NumNativeTexIndirections
;
428 switch (prog
->Target
) {
429 case GL_VERTEX_PROGRAM_ARB
:
431 const struct gl_vertex_program
*vp
432 = (const struct gl_vertex_program
*) prog
;
433 struct gl_vertex_program
*vpc
= (struct gl_vertex_program
*) clone
;
434 vpc
->IsPositionInvariant
= vp
->IsPositionInvariant
;
437 case GL_FRAGMENT_PROGRAM_ARB
:
439 const struct gl_fragment_program
*fp
440 = (const struct gl_fragment_program
*) prog
;
441 struct gl_fragment_program
*fpc
= (struct gl_fragment_program
*) clone
;
442 fpc
->FogOption
= fp
->FogOption
;
443 fpc
->UsesKill
= fp
->UsesKill
;
447 _mesa_problem(NULL
, "Unexpected target in _mesa_clone_program");
455 * Insert 'count' NOP instructions at 'start' in the given program.
456 * Adjust branch targets accordingly.
459 _mesa_insert_instructions(struct gl_program
*prog
, GLuint start
, GLuint count
)
461 const GLuint origLen
= prog
->NumInstructions
;
462 const GLuint newLen
= origLen
+ count
;
463 struct prog_instruction
*newInst
;
466 /* adjust branches */
467 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
468 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
469 if (inst
->BranchTarget
> 0) {
470 if ((GLuint
)inst
->BranchTarget
>= start
) {
471 inst
->BranchTarget
+= count
;
476 /* Alloc storage for new instructions */
477 newInst
= _mesa_alloc_instructions(newLen
);
482 /* Copy 'start' instructions into new instruction buffer */
483 _mesa_copy_instructions(newInst
, prog
->Instructions
, start
);
485 /* init the new instructions */
486 _mesa_init_instructions(newInst
+ start
, count
);
488 /* Copy the remaining/tail instructions to new inst buffer */
489 _mesa_copy_instructions(newInst
+ start
+ count
,
490 prog
->Instructions
+ start
,
493 /* free old instructions */
494 _mesa_free_instructions(prog
->Instructions
, origLen
);
496 /* install new instructions */
497 prog
->Instructions
= newInst
;
498 prog
->NumInstructions
= newLen
;
505 * Search instructions for registers that match (oldFile, oldIndex),
506 * replacing them with (newFile, newIndex).
509 replace_registers(struct prog_instruction
*inst
, GLuint numInst
,
510 GLuint oldFile
, GLuint oldIndex
,
511 GLuint newFile
, GLuint newIndex
)
514 for (i
= 0; i
< numInst
; i
++) {
516 for (j
= 0; j
< _mesa_num_inst_src_regs(inst
->Opcode
); j
++) {
517 if (inst
[i
].SrcReg
[j
].File
== oldFile
&&
518 inst
[i
].SrcReg
[j
].Index
== oldIndex
) {
519 inst
[i
].SrcReg
[j
].File
= newFile
;
520 inst
[i
].SrcReg
[j
].Index
= newIndex
;
524 if (inst
[i
].DstReg
.File
== oldFile
&& inst
[i
].DstReg
.Index
== oldIndex
) {
525 inst
[i
].DstReg
.File
= newFile
;
526 inst
[i
].DstReg
.Index
= newIndex
;
533 * Search instructions for references to program parameters. When found,
534 * increment the parameter index by 'offset'.
535 * Used when combining programs.
538 adjust_param_indexes(struct prog_instruction
*inst
, GLuint numInst
,
542 for (i
= 0; i
< numInst
; i
++) {
543 for (j
= 0; j
< _mesa_num_inst_src_regs(inst
->Opcode
); j
++) {
544 GLuint f
= inst
[i
].SrcReg
[j
].File
;
545 if (f
== PROGRAM_CONSTANT
||
546 f
== PROGRAM_UNIFORM
||
547 f
== PROGRAM_STATE_VAR
) {
548 inst
[i
].SrcReg
[j
].Index
+= offset
;
556 * Combine two programs into one. Fix instructions so the outputs of
557 * the first program go to the inputs of the second program.
560 _mesa_combine_programs(GLcontext
*ctx
,
561 const struct gl_program
*progA
,
562 const struct gl_program
*progB
)
564 struct prog_instruction
*newInst
;
565 struct gl_program
*newProg
;
566 const GLuint lenA
= progA
->NumInstructions
- 1; /* omit END instr */
567 const GLuint lenB
= progB
->NumInstructions
;
568 const GLuint numParamsA
= _mesa_num_parameters(progA
->Parameters
);
569 const GLuint newLength
= lenA
+ lenB
;
573 ASSERT(progA
->Target
== progB
->Target
);
575 newInst
= _mesa_alloc_instructions(newLength
);
579 _mesa_copy_instructions(newInst
, progA
->Instructions
, lenA
);
580 _mesa_copy_instructions(newInst
+ lenA
, progB
->Instructions
, lenB
);
582 /* adjust branch / instruction addresses for B's instructions */
583 for (i
= 0; i
< lenB
; i
++) {
584 newInst
[lenA
+ i
].BranchTarget
+= lenA
;
587 newProg
= ctx
->Driver
.NewProgram(ctx
, progA
->Target
, 0);
588 newProg
->Instructions
= newInst
;
589 newProg
->NumInstructions
= newLength
;
591 if (newProg
->Target
== GL_FRAGMENT_PROGRAM_ARB
) {
592 struct gl_fragment_program
*fprogA
, *fprogB
, *newFprog
;
593 fprogA
= (struct gl_fragment_program
*) progA
;
594 fprogB
= (struct gl_fragment_program
*) progB
;
595 newFprog
= (struct gl_fragment_program
*) newProg
;
597 newFprog
->UsesKill
= fprogA
->UsesKill
|| fprogB
->UsesKill
;
599 /* Connect color outputs of fprogA to color inputs of fprogB, via a
600 * new temporary register.
602 if ((progA
->OutputsWritten
& (1 << FRAG_RESULT_COLR
)) &&
603 (progB
->InputsRead
& (1 << FRAG_ATTRIB_COL0
))) {
604 GLint tempReg
= _mesa_find_free_register(newProg
, PROGRAM_TEMPORARY
);
606 _mesa_problem(ctx
, "No free temp regs found in "
607 "_mesa_combine_programs(), using 31");
610 /* replace writes to result.color[0] with tempReg */
611 replace_registers(newInst
, lenA
,
612 PROGRAM_OUTPUT
, FRAG_RESULT_COLR
,
613 PROGRAM_TEMPORARY
, tempReg
);
614 /* replace reads from input.color[0] with tempReg */
615 replace_registers(newInst
+ lenA
, lenB
,
616 PROGRAM_INPUT
, FRAG_ATTRIB_COL0
,
617 PROGRAM_TEMPORARY
, tempReg
);
620 inputsB
= progB
->InputsRead
;
621 if (progA
->OutputsWritten
& (1 << FRAG_RESULT_COLR
)) {
622 inputsB
&= ~(1 << FRAG_ATTRIB_COL0
);
624 newProg
->InputsRead
= progA
->InputsRead
| inputsB
;
625 newProg
->OutputsWritten
= progB
->OutputsWritten
;
626 newProg
->SamplersUsed
= progA
->SamplersUsed
| progB
->SamplersUsed
;
630 assert(0); /* XXX todo */
634 * Merge parameters (uniforms, constants, etc)
636 newProg
->Parameters
= _mesa_combine_parameter_lists(progA
->Parameters
,
639 adjust_param_indexes(newInst
+ lenA
, lenB
, numParamsA
);
649 * Scan the given program to find a free register of the given type.
650 * \param regFile - PROGRAM_INPUT, PROGRAM_OUTPUT or PROGRAM_TEMPORARY
653 _mesa_find_free_register(const struct gl_program
*prog
, GLuint regFile
)
655 GLboolean used
[MAX_PROGRAM_TEMPS
];
658 assert(regFile
== PROGRAM_INPUT
||
659 regFile
== PROGRAM_OUTPUT
||
660 regFile
== PROGRAM_TEMPORARY
);
662 _mesa_memset(used
, 0, sizeof(used
));
664 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
665 const struct prog_instruction
*inst
= prog
->Instructions
+ i
;
666 const GLuint n
= _mesa_num_inst_src_regs(inst
->Opcode
);
668 for (k
= 0; k
< n
; k
++) {
669 if (inst
->SrcReg
[k
].File
== regFile
) {
670 used
[inst
->SrcReg
[k
].Index
] = GL_TRUE
;
675 for (i
= 0; i
< MAX_PROGRAM_TEMPS
; i
++) {