2 * Mesa 3-D graphics library
5 * Copyright (C) 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.
36 #include "prog_instruction.h"
37 #include "prog_parameter.h"
38 #include "prog_print.h"
39 #include "prog_statevars.h"
40 #include "shader_api.h"
41 #include "slang_link.h"
47 link_varying_vars(struct gl_shader_program
*shProg
, struct gl_program
*prog
)
49 GLuint
*map
, i
, firstVarying
, newFile
;
50 GLbitfield varsWritten
, varsRead
;
52 map
= (GLuint
*) malloc(prog
->Varying
->NumParameters
* sizeof(GLuint
));
56 for (i
= 0; i
< prog
->Varying
->NumParameters
; i
++) {
57 /* see if this varying is in the linked varying list */
58 const struct gl_program_parameter
*var
59 = prog
->Varying
->Parameters
+ i
;
61 GLint j
= _mesa_lookup_parameter_index(shProg
->Varying
, -1, var
->Name
);
63 /* already in list, check size */
64 if (var
->Size
!= shProg
->Varying
->Parameters
[j
].Size
) {
70 /* not already in linked list */
71 j
= _mesa_add_varying(shProg
->Varying
, var
->Name
, var
->Size
);
79 /* Varying variables are treated like other vertex program outputs
80 * (and like other fragment program inputs). The position of the
81 * first varying differs for vertex/fragment programs...
82 * Also, replace File=PROGRAM_VARYING with File=PROGRAM_INPUT/OUTPUT.
84 if (prog
->Target
== GL_VERTEX_PROGRAM_ARB
) {
85 firstVarying
= VERT_RESULT_VAR0
;
86 newFile
= PROGRAM_OUTPUT
;
89 assert(prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
);
90 firstVarying
= FRAG_ATTRIB_VAR0
;
91 newFile
= PROGRAM_INPUT
;
94 /* keep track of which varying vars we read and write */
95 varsWritten
= varsRead
= 0x0;
97 /* OK, now scan the program/shader instructions looking for varying vars,
98 * replacing the old index with the new index.
100 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
101 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
104 if (inst
->DstReg
.File
== PROGRAM_VARYING
) {
105 inst
->DstReg
.File
= newFile
;
106 inst
->DstReg
.Index
= map
[ inst
->DstReg
.Index
] + firstVarying
;
107 varsWritten
|= (1 << inst
->DstReg
.Index
);
110 for (j
= 0; j
< 3; j
++) {
111 if (inst
->SrcReg
[j
].File
== PROGRAM_VARYING
) {
112 inst
->SrcReg
[j
].File
= newFile
;
113 inst
->SrcReg
[j
].Index
= map
[ inst
->SrcReg
[j
].Index
] + firstVarying
;
114 varsRead
|= (1 << inst
->SrcReg
[j
].Index
);
117 /* XXX update program OutputsWritten, InputsRead */
120 if (prog
->Target
== GL_VERTEX_PROGRAM_ARB
) {
121 prog
->OutputsWritten
|= varsWritten
;
124 assert(prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
);
125 prog
->InputsRead
|= varsRead
;
136 is_uniform(GLuint file
)
138 return (file
== PROGRAM_ENV_PARAM
||
139 file
== PROGRAM_STATE_VAR
||
140 file
== PROGRAM_NAMED_PARAM
||
141 file
== PROGRAM_CONSTANT
||
142 file
== PROGRAM_SAMPLER
||
143 file
== PROGRAM_UNIFORM
);
148 link_uniform_vars(struct gl_shader_program
*shProg
, struct gl_program
*prog
)
153 printf("================ pre link uniforms ===============\n");
154 _mesa_print_parameter_list(shProg
->Uniforms
);
157 map
= (GLuint
*) malloc(prog
->Parameters
->NumParameters
* sizeof(GLuint
));
161 for (i
= 0; i
< prog
->Parameters
->NumParameters
; /* incr below*/) {
162 /* see if this uniform is in the linked uniform list */
163 const struct gl_program_parameter
*p
= prog
->Parameters
->Parameters
+ i
;
164 const GLfloat
*pVals
= prog
->Parameters
->ParameterValues
[i
];
169 assert(is_uniform(p
->Type
));
172 j
= _mesa_lookup_parameter_index(shProg
->Uniforms
, -1, p
->Name
);
176 ASSERT(p
->Type
== PROGRAM_CONSTANT
);
177 if (_mesa_lookup_parameter_constant(shProg
->Uniforms
, pVals
,
178 p
->Size
, &j
, &swizzle
)) {
187 /* already in list, check size XXX check this */
189 assert(p
->Size
== shProg
->Uniforms
->Parameters
[j
].Size
);
193 /* not already in linked list */
195 case PROGRAM_ENV_PARAM
:
196 j
= _mesa_add_named_parameter(shProg
->Uniforms
, p
->Name
, pVals
);
198 case PROGRAM_CONSTANT
:
199 j
= _mesa_add_named_constant(shProg
->Uniforms
, p
->Name
, pVals
, p
->Size
);
201 case PROGRAM_STATE_VAR
:
202 j
= _mesa_add_state_reference(shProg
->Uniforms
, (const GLint
*) p
->StateIndexes
);
204 case PROGRAM_UNIFORM
:
205 j
= _mesa_add_uniform(shProg
->Uniforms
, p
->Name
, p
->Size
);
207 case PROGRAM_SAMPLER
:
208 j
= _mesa_add_sampler(shProg
->Uniforms
, p
->Name
);
228 printf("================ post link uniforms ===============\n");
229 _mesa_print_parameter_list(shProg
->Uniforms
);
235 for (i
= 0; i
< prog
->Parameters
->NumParameters
; i
++) {
236 printf("map[%d] = %d\n", i
, map
[i
]);
238 _mesa_print_parameter_list(shProg
->Uniforms
);
242 /* OK, now scan the program/shader instructions looking for uniform vars,
243 * replacing the old index with the new index.
245 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
246 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
249 if (is_uniform(inst
->DstReg
.File
)) {
250 inst
->DstReg
.Index
= map
[ inst
->DstReg
.Index
];
253 for (j
= 0; j
< 3; j
++) {
254 if (is_uniform(inst
->SrcReg
[j
].File
)) {
255 inst
->SrcReg
[j
].Index
= map
[ inst
->SrcReg
[j
].Index
];
259 if (inst
->Opcode
== OPCODE_TEX
||
260 inst
->Opcode
== OPCODE_TXB
||
261 inst
->Opcode
== OPCODE_TXP
) {
263 printf("====== remap sampler from %d to %d\n",
264 inst->Sampler, map[ inst->Sampler ]);
266 inst
->Sampler
= map
[ inst
->Sampler
];
280 _slang_resolve_branches(struct gl_program
*prog
)
286 struct target targets
[500];
287 GLuint numTargets
= 0;
290 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
291 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
292 if (inst
->Opcode
== OPCODE_NOP
&& inst
->Comment
) {
293 targets
[numTargets
].Name
= inst
->Comment
;
294 targets
[numTargets
].Pos
= i
;
299 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
300 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
301 if (inst
->Opcode
== OPCODE_BRA
&& inst
->BranchTarget
< 0) {
302 for (j
= 0; j
< numTargets
; j
++) {
303 if (!strcmp(inst
->Comment
, targets
[j
].Name
)) {
304 inst
->BranchTarget
= targets
[j
].Pos
;
308 if (j
== numTargets
) {
317 * Resolve binding of generic vertex attributes.
318 * For example, if the vertex shader declared "attribute vec4 foobar" we'll
319 * allocate a generic vertex attribute for "foobar" and plug that value into
320 * the vertex program instructions.
323 _slang_resolve_attributes(struct gl_shader_program
*shProg
,
324 struct gl_program
*prog
)
327 GLbitfield usedAttributes
;
328 GLint size
= 4; /* XXX fix */
330 assert(prog
->Target
== GL_VERTEX_PROGRAM_ARB
);
332 /* Build a bitmask indicating which attribute indexes have been
333 * explicitly bound by the user with glBindAttributeLocation().
335 usedAttributes
= 0x0;
336 for (i
= 0; i
< shProg
->Attributes
->NumParameters
; i
++) {
337 GLint attr
= shProg
->Attributes
->Parameters
[i
].StateIndexes
[0];
338 usedAttributes
|= attr
;
341 if (!shProg
->Attributes
)
342 shProg
->Attributes
= _mesa_new_parameter_list();
345 * Scan program for generic attribute references
347 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
348 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
349 for (j
= 0; j
< 3; j
++) {
350 if (inst
->SrcReg
[j
].File
== PROGRAM_INPUT
&&
351 inst
->SrcReg
[j
].Index
>= VERT_ATTRIB_GENERIC0
) {
352 /* this is a generic attrib */
353 const GLint k
= inst
->SrcReg
[j
].Index
- VERT_ATTRIB_GENERIC0
;
354 const char *name
= prog
->Attributes
->Parameters
[k
].Name
;
355 /* See if this attrib name is in the program's attribute list
356 * (i.e. was bound by the user).
358 GLint index
= _mesa_lookup_parameter_index(shProg
->Attributes
,
362 /* found, user must have specified a binding */
363 attr
= shProg
->Attributes
->Parameters
[index
].StateIndexes
[0];
366 /* Not found, choose our own attribute number.
367 * Start at 1 since generic attribute 0 always aliases
370 for (attr
= 1; attr
< MAX_VERTEX_ATTRIBS
; attr
++) {
371 if (((1 << attr
) & usedAttributes
) == 0)
374 if (attr
== MAX_VERTEX_ATTRIBS
) {
375 /* too many! XXX record error log */
378 _mesa_add_attribute(shProg
->Attributes
, name
, size
, attr
);
381 inst
->SrcReg
[j
].Index
= VERT_ATTRIB_GENERIC0
+ attr
;
390 * Scan program instructions to update the program's InputsRead and
391 * OutputsWritten fields.
394 _slang_update_inputs_outputs(struct gl_program
*prog
)
398 prog
->InputsRead
= 0x0;
399 prog
->OutputsWritten
= 0x0;
401 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
402 const struct prog_instruction
*inst
= prog
->Instructions
+ i
;
403 const GLuint numSrc
= _mesa_num_inst_src_regs(inst
->Opcode
);
404 for (j
= 0; j
< numSrc
; j
++) {
405 if (inst
->SrcReg
[j
].File
== PROGRAM_INPUT
) {
406 prog
->InputsRead
|= 1 << inst
->SrcReg
[j
].Index
;
409 if (inst
->DstReg
.File
== PROGRAM_OUTPUT
) {
410 prog
->OutputsWritten
|= 1 << inst
->DstReg
.Index
;
417 * Scan a vertex program looking for instances of
418 * (PROGRAM_INPUT, VERT_ATTRIB_GENERIC0 + oldAttrib) and replace with
419 * (PROGRAM_INPUT, VERT_ATTRIB_GENERIC0 + newAttrib).
420 * This is used when the user calls glBindAttribLocation on an already linked
424 _slang_remap_attribute(struct gl_program
*prog
, GLuint oldAttrib
, GLuint newAttrib
)
428 assert(prog
->Target
== GL_VERTEX_PROGRAM_ARB
);
430 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
431 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
432 for (j
= 0; j
< 3; j
++) {
433 if (inst
->SrcReg
[j
].File
== PROGRAM_INPUT
) {
434 if (inst
->SrcReg
[j
].Index
== VERT_ATTRIB_GENERIC0
+ oldAttrib
) {
435 inst
->SrcReg
[j
].Index
= VERT_ATTRIB_GENERIC0
+ newAttrib
;
441 _slang_update_inputs_outputs(prog
);
447 * Scan program for texture instructions, lookup sampler/uniform's value
448 * to determine which texture unit to use.
449 * Also, update the program's TexturesUsed[] array.
452 _slang_resolve_samplers(struct gl_shader_program
*shProg
,
453 struct gl_program
*prog
)
457 for (i
= 0; i
< MAX_TEXTURE_IMAGE_UNITS
; i
++)
458 prog
->TexturesUsed
[i
] = 0;
460 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
461 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
462 if (inst
->Opcode
== OPCODE_TEX
||
463 inst
->Opcode
== OPCODE_TXB
||
464 inst
->Opcode
== OPCODE_TXP
) {
465 GLint sampleUnit
= (GLint
) shProg
->Uniforms
->ParameterValues
[inst
->Sampler
][0];
466 assert(sampleUnit
< MAX_TEXTURE_IMAGE_UNITS
);
467 inst
->TexSrcUnit
= sampleUnit
;
469 prog
->TexturesUsed
[inst
->TexSrcUnit
] |= (1 << inst
->TexSrcTarget
);
477 static struct gl_vertex_program
*
478 vertex_program(struct gl_program
*prog
)
480 assert(prog
->Target
== GL_VERTEX_PROGRAM_ARB
);
481 return (struct gl_vertex_program
*) prog
;
486 static struct gl_fragment_program
*
487 fragment_program(struct gl_program
*prog
)
489 assert(prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
);
490 return (struct gl_fragment_program
*) prog
;
495 * Shader linker. Currently:
497 * 1. The last attached vertex shader and fragment shader are linked.
498 * 2. Varying vars in the two shaders are combined so their locations
499 * agree between the vertex and fragment stages. They're treated as
500 * vertex program output attribs and as fragment program input attribs.
501 * 3. Uniform vars (including state references, constants, etc) from the
502 * vertex and fragment shaders are merged into one group. Recall that
503 * GLSL uniforms are shared by all linked shaders.
504 * 4. The vertex and fragment programs are cloned and modified to update
505 * src/dst register references so they use the new, linked uniform/
506 * varying storage locations.
509 _slang_link(GLcontext
*ctx
,
510 GLhandleARB programObj
,
511 struct gl_shader_program
*shProg
)
513 const struct gl_vertex_program
*vertProg
;
514 const struct gl_fragment_program
*fragProg
;
517 _mesa_free_shader_program_data(ctx
, shProg
);
519 shProg
->Uniforms
= _mesa_new_parameter_list();
520 shProg
->Varying
= _mesa_new_parameter_list();
523 * Find attached vertex shader, fragment shader
527 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
528 if (shProg
->Shaders
[i
]->Type
== GL_VERTEX_SHADER
)
529 vertProg
= vertex_program(shProg
->Shaders
[i
]->Programs
[0]);
530 else if (shProg
->Shaders
[i
]->Type
== GL_FRAGMENT_SHADER
)
531 fragProg
= fragment_program(shProg
->Shaders
[i
]->Programs
[0]);
533 _mesa_problem(ctx
, "unexpected shader target in slang_link()");
537 * Make copies of the vertex/fragment programs now since we'll be
538 * changing src/dst registers after merging the uniforms and varying vars.
541 shProg
->VertexProgram
542 = vertex_program(_mesa_clone_program(ctx
, &vertProg
->Base
));
545 shProg
->VertexProgram
= NULL
;
549 shProg
->FragmentProgram
550 = fragment_program(_mesa_clone_program(ctx
, &fragProg
->Base
));
553 shProg
->FragmentProgram
= NULL
;
556 if (shProg
->VertexProgram
)
557 link_varying_vars(shProg
, &shProg
->VertexProgram
->Base
);
558 if (shProg
->FragmentProgram
)
559 link_varying_vars(shProg
, &shProg
->FragmentProgram
->Base
);
561 if (shProg
->VertexProgram
)
562 link_uniform_vars(shProg
, &shProg
->VertexProgram
->Base
);
563 if (shProg
->FragmentProgram
)
564 link_uniform_vars(shProg
, &shProg
->FragmentProgram
->Base
);
566 /* The vertex and fragment programs share a common set of uniforms now */
567 if (shProg
->VertexProgram
) {
568 _mesa_free_parameter_list(shProg
->VertexProgram
->Base
.Parameters
);
569 shProg
->VertexProgram
->Base
.Parameters
= shProg
->Uniforms
;
571 if (shProg
->FragmentProgram
) {
572 _mesa_free_parameter_list(shProg
->FragmentProgram
->Base
.Parameters
);
573 shProg
->FragmentProgram
->Base
.Parameters
= shProg
->Uniforms
;
576 if (shProg
->VertexProgram
) {
577 _slang_resolve_branches(&shProg
->VertexProgram
->Base
);
578 _slang_resolve_samplers(shProg
, &shProg
->VertexProgram
->Base
);
580 if (shProg
->FragmentProgram
) {
581 _slang_resolve_branches(&shProg
->FragmentProgram
->Base
);
582 _slang_resolve_samplers(shProg
, &shProg
->FragmentProgram
->Base
);
585 if (shProg
->VertexProgram
) {
586 if (!_slang_resolve_attributes(shProg
, &shProg
->VertexProgram
->Base
)) {
588 _mesa_problem(ctx
, "_slang_resolve_attributes() failed");
589 abort(); /* XXX fix */
593 if (shProg
->VertexProgram
)
594 _slang_update_inputs_outputs(&shProg
->VertexProgram
->Base
);
595 if (shProg
->FragmentProgram
)
596 _slang_update_inputs_outputs(&shProg
->FragmentProgram
->Base
);
598 if (fragProg
&& shProg
->FragmentProgram
) {
600 printf("************** original fragment program\n");
601 _mesa_print_program(&fragProg
->Base
);
602 _mesa_print_program_parameters(ctx
, &fragProg
->Base
);
605 printf("************** linked fragment prog\n");
606 _mesa_print_program(&shProg
->FragmentProgram
->Base
);
607 _mesa_print_program_parameters(ctx
, &shProg
->FragmentProgram
->Base
);
611 if (vertProg
&& shProg
->VertexProgram
) {
613 printf("************** original vertex program\n");
614 _mesa_print_program(&vertProg
->Base
);
615 _mesa_print_program_parameters(ctx
, &fragProg
->Base
);
618 printf("************** linked vertex prog\n");
619 _mesa_print_program(&shProg
->VertexProgram
->Base
);
620 _mesa_print_program_parameters(ctx
, &shProg
->VertexProgram
->Base
);
624 shProg
->LinkStatus
= (shProg
->VertexProgram
|| shProg
->FragmentProgram
);