2 * Mesa 3-D graphics library
5 * Copyright (C) 2008 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.
31 #include "main/imports.h"
32 #include "main/context.h"
33 #include "main/hash.h"
34 #include "main/macros.h"
35 #include "shader/program.h"
36 #include "shader/prog_instruction.h"
37 #include "shader/prog_parameter.h"
38 #include "shader/prog_print.h"
39 #include "shader/prog_statevars.h"
40 #include "shader/prog_uniform.h"
41 #include "shader/shader_api.h"
42 #include "slang_link.h"
46 static struct gl_vertex_program
*
47 vertex_program(struct gl_program
*prog
)
49 assert(prog
->Target
== GL_VERTEX_PROGRAM_ARB
);
50 return (struct gl_vertex_program
*) prog
;
55 static struct gl_fragment_program
*
56 fragment_program(struct gl_program
*prog
)
58 assert(prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
);
59 return (struct gl_fragment_program
*) prog
;
64 * Record a linking error.
67 link_error(struct gl_shader_program
*shProg
, const char *msg
)
69 if (shProg
->InfoLog
) {
70 _mesa_free(shProg
->InfoLog
);
72 shProg
->InfoLog
= _mesa_strdup(msg
);
73 shProg
->LinkStatus
= GL_FALSE
;
79 * Linking varying vars involves rearranging varying vars so that the
80 * vertex program's output varyings matches the order of the fragment
81 * program's input varyings.
84 link_varying_vars(struct gl_shader_program
*shProg
, struct gl_program
*prog
)
86 GLuint
*map
, i
, firstVarying
, newFile
;
88 map
= (GLuint
*) malloc(prog
->Varying
->NumParameters
* sizeof(GLuint
));
92 for (i
= 0; i
< prog
->Varying
->NumParameters
; i
++) {
93 /* see if this varying is in the linked varying list */
94 const struct gl_program_parameter
*var
= prog
->Varying
->Parameters
+ i
;
95 GLint j
= _mesa_lookup_parameter_index(shProg
->Varying
, -1, var
->Name
);
97 /* already in list, check size */
98 if (var
->Size
!= shProg
->Varying
->Parameters
[j
].Size
) {
100 link_error(shProg
, "mismatched varying variable types");
105 /* not already in linked list */
106 j
= _mesa_add_varying(shProg
->Varying
, var
->Name
, var
->Size
);
109 /* map varying[i] to varying[j].
110 * Note: the loop here takes care of arrays or large (sz>4) vars.
113 GLint sz
= var
->Size
;
115 /*printf("Link varying from %d to %d\n", i, j);*/
119 i
--; /* go back one */
124 /* Varying variables are treated like other vertex program outputs
125 * (and like other fragment program inputs). The position of the
126 * first varying differs for vertex/fragment programs...
127 * Also, replace File=PROGRAM_VARYING with File=PROGRAM_INPUT/OUTPUT.
129 if (prog
->Target
== GL_VERTEX_PROGRAM_ARB
) {
130 firstVarying
= VERT_RESULT_VAR0
;
131 newFile
= PROGRAM_OUTPUT
;
134 assert(prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
);
135 firstVarying
= FRAG_ATTRIB_VAR0
;
136 newFile
= PROGRAM_INPUT
;
139 /* OK, now scan the program/shader instructions looking for varying vars,
140 * replacing the old index with the new index.
142 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
143 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
146 if (inst
->DstReg
.File
== PROGRAM_VARYING
) {
147 inst
->DstReg
.File
= newFile
;
148 inst
->DstReg
.Index
= map
[ inst
->DstReg
.Index
] + firstVarying
;
151 for (j
= 0; j
< 3; j
++) {
152 if (inst
->SrcReg
[j
].File
== PROGRAM_VARYING
) {
153 inst
->SrcReg
[j
].File
= newFile
;
154 inst
->SrcReg
[j
].Index
= map
[ inst
->SrcReg
[j
].Index
] + firstVarying
;
161 /* these will get recomputed before linking is completed */
162 prog
->InputsRead
= 0x0;
163 prog
->OutputsWritten
= 0x0;
170 * Build the shProg->Uniforms list.
171 * This is basically a list/index of all uniforms found in either/both of
172 * the vertex and fragment shaders.
175 link_uniform_vars(struct gl_shader_program
*shProg
,
176 struct gl_program
*prog
,
179 GLuint samplerMap
[MAX_SAMPLERS
];
182 for (i
= 0; i
< prog
->Parameters
->NumParameters
; i
++) {
183 const struct gl_program_parameter
*p
= prog
->Parameters
->Parameters
+ i
;
186 * XXX FIX NEEDED HERE
187 * We should also be adding a uniform if p->Type == PROGRAM_STATE_VAR.
188 * For example, modelview matrix, light pos, etc.
189 * Also, we need to update the state-var name-generator code to
190 * generate GLSL-style names, like "gl_LightSource[0].position".
191 * Furthermore, we'll need to fix the state-var's size/datatype info.
194 if ((p
->Type
== PROGRAM_UNIFORM
&& p
->Used
) ||
195 p
->Type
== PROGRAM_SAMPLER
) {
196 _mesa_append_uniform(shProg
->Uniforms
, p
->Name
, prog
->Target
, i
);
199 if (p
->Type
== PROGRAM_SAMPLER
) {
200 /* Allocate a new sampler index */
201 GLuint sampNum
= *numSamplers
;
202 GLuint oldSampNum
= (GLuint
) prog
->Parameters
->ParameterValues
[i
][0];
203 assert(oldSampNum
< MAX_SAMPLERS
);
204 samplerMap
[oldSampNum
] = sampNum
;
210 /* OK, now scan the program/shader instructions looking for sampler vars,
211 * replacing the old index with the new index.
213 prog
->SamplersUsed
= 0x0;
214 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
215 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
216 if (_mesa_is_tex_instruction(inst
->Opcode
)) {
218 printf("====== remap sampler from %d to %d\n",
219 inst->Sampler, map[ inst->Sampler ]);
221 /* here, texUnit is really samplerUnit */
222 assert(inst
->TexSrcUnit
< MAX_SAMPLERS
);
223 inst
->TexSrcUnit
= samplerMap
[inst
->TexSrcUnit
];
224 prog
->SamplerTargets
[inst
->TexSrcUnit
] = inst
->TexSrcTarget
;
225 prog
->SamplersUsed
|= (1 << inst
->TexSrcUnit
);
233 * Resolve binding of generic vertex attributes.
234 * For example, if the vertex shader declared "attribute vec4 foobar" we'll
235 * allocate a generic vertex attribute for "foobar" and plug that value into
236 * the vertex program instructions.
237 * But if the user called glBindAttributeLocation(), those bindings will
241 _slang_resolve_attributes(struct gl_shader_program
*shProg
,
242 const struct gl_program
*origProg
,
243 struct gl_program
*linkedProg
)
245 GLint attribMap
[MAX_VERTEX_ATTRIBS
];
247 GLbitfield usedAttributes
;
249 assert(origProg
!= linkedProg
);
250 assert(origProg
->Target
== GL_VERTEX_PROGRAM_ARB
);
251 assert(linkedProg
->Target
== GL_VERTEX_PROGRAM_ARB
);
253 if (!shProg
->Attributes
)
254 shProg
->Attributes
= _mesa_new_parameter_list();
256 if (linkedProg
->Attributes
) {
257 _mesa_free_parameter_list(linkedProg
->Attributes
);
259 linkedProg
->Attributes
= _mesa_new_parameter_list();
262 /* Build a bitmask indicating which attribute indexes have been
263 * explicitly bound by the user with glBindAttributeLocation().
265 usedAttributes
= 0x0;
266 for (i
= 0; i
< shProg
->Attributes
->NumParameters
; i
++) {
267 GLint attr
= shProg
->Attributes
->Parameters
[i
].StateIndexes
[0];
268 usedAttributes
|= (1 << attr
);
271 /* initialize the generic attribute map entries to -1 */
272 for (i
= 0; i
< MAX_VERTEX_ATTRIBS
; i
++) {
277 * Scan program for generic attribute references
279 for (i
= 0; i
< linkedProg
->NumInstructions
; i
++) {
280 struct prog_instruction
*inst
= linkedProg
->Instructions
+ i
;
281 for (j
= 0; j
< 3; j
++) {
282 if (inst
->SrcReg
[j
].File
== PROGRAM_INPUT
&&
283 inst
->SrcReg
[j
].Index
>= VERT_ATTRIB_GENERIC0
) {
285 * OK, we've found a generic vertex attribute reference.
287 const GLint k
= inst
->SrcReg
[j
].Index
- VERT_ATTRIB_GENERIC0
;
289 GLint attr
= attribMap
[k
];
292 /* Need to figure out attribute mapping now.
294 const char *name
= origProg
->Attributes
->Parameters
[k
].Name
;
295 const GLint size
= origProg
->Attributes
->Parameters
[k
].Size
;
296 const GLenum type
=origProg
->Attributes
->Parameters
[k
].DataType
;
299 /* See if there's a user-defined attribute binding for
302 index
= _mesa_lookup_parameter_index(shProg
->Attributes
,
305 /* Found a user-defined binding */
306 attr
= shProg
->Attributes
->Parameters
[index
].StateIndexes
[0];
309 /* No user-defined binding, choose our own attribute number.
310 * Start at 1 since generic attribute 0 always aliases
313 for (attr
= 1; attr
< MAX_VERTEX_ATTRIBS
; attr
++) {
314 if (((1 << attr
) & usedAttributes
) == 0)
317 if (attr
== MAX_VERTEX_ATTRIBS
) {
318 link_error(shProg
, "Too many vertex attributes");
322 /* mark this attribute as used */
323 usedAttributes
|= (1 << attr
);
328 /* Save the final name->attrib binding so it can be queried
329 * with glGetAttributeLocation().
331 _mesa_add_attribute(linkedProg
->Attributes
, name
,
337 /* update the instruction's src reg */
338 inst
->SrcReg
[j
].Index
= VERT_ATTRIB_GENERIC0
+ attr
;
348 * Scan program instructions to update the program's NumTemporaries field.
349 * Note: this implemenation relies on the code generator allocating
350 * temps in increasing order (0, 1, 2, ... ).
353 _slang_count_temporaries(struct gl_program
*prog
)
358 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
359 const struct prog_instruction
*inst
= prog
->Instructions
+ i
;
360 const GLuint numSrc
= _mesa_num_inst_src_regs(inst
->Opcode
);
361 for (j
= 0; j
< numSrc
; j
++) {
362 if (inst
->SrcReg
[j
].File
== PROGRAM_TEMPORARY
) {
363 if (maxIndex
< inst
->SrcReg
[j
].Index
)
364 maxIndex
= inst
->SrcReg
[j
].Index
;
366 if (inst
->DstReg
.File
== PROGRAM_TEMPORARY
) {
367 if (maxIndex
< (GLint
) inst
->DstReg
.Index
)
368 maxIndex
= inst
->DstReg
.Index
;
373 prog
->NumTemporaries
= (GLuint
) (maxIndex
+ 1);
378 * Scan program instructions to update the program's InputsRead and
379 * OutputsWritten fields.
382 _slang_update_inputs_outputs(struct gl_program
*prog
)
385 GLuint maxAddrReg
= 0;
387 prog
->InputsRead
= 0x0;
388 prog
->OutputsWritten
= 0x0;
390 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
391 const struct prog_instruction
*inst
= prog
->Instructions
+ i
;
392 const GLuint numSrc
= _mesa_num_inst_src_regs(inst
->Opcode
);
393 for (j
= 0; j
< numSrc
; j
++) {
394 if (inst
->SrcReg
[j
].File
== PROGRAM_INPUT
) {
395 prog
->InputsRead
|= 1 << inst
->SrcReg
[j
].Index
;
396 if (prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
&&
397 inst
->SrcReg
[j
].Index
== FRAG_ATTRIB_FOGC
) {
398 /* The fragment shader FOGC input is used for fog,
399 * front-facing and sprite/point coord.
401 struct gl_fragment_program
*fp
= fragment_program(prog
);
402 const GLint swz
= GET_SWZ(inst
->SrcReg
[j
].Swizzle
, 0);
403 if (swz
== SWIZZLE_X
)
404 fp
->UsesFogFragCoord
= GL_TRUE
;
405 else if (swz
== SWIZZLE_Y
)
406 fp
->UsesFrontFacing
= GL_TRUE
;
407 else if (swz
== SWIZZLE_Z
|| swz
== SWIZZLE_W
)
408 fp
->UsesPointCoord
= GL_TRUE
;
411 else if (inst
->SrcReg
[j
].File
== PROGRAM_ADDRESS
) {
412 maxAddrReg
= MAX2(maxAddrReg
, (GLuint
) (inst
->SrcReg
[j
].Index
+ 1));
415 if (inst
->DstReg
.File
== PROGRAM_OUTPUT
) {
416 prog
->OutputsWritten
|= 1 << inst
->DstReg
.Index
;
418 else if (inst
->DstReg
.File
== PROGRAM_ADDRESS
) {
419 maxAddrReg
= MAX2(maxAddrReg
, inst
->DstReg
.Index
+ 1);
423 prog
->NumAddressRegs
= maxAddrReg
;
428 * Shader linker. Currently:
430 * 1. The last attached vertex shader and fragment shader are linked.
431 * 2. Varying vars in the two shaders are combined so their locations
432 * agree between the vertex and fragment stages. They're treated as
433 * vertex program output attribs and as fragment program input attribs.
434 * 3. The vertex and fragment programs are cloned and modified to update
435 * src/dst register references so they use the new, linked varying
439 _slang_link(GLcontext
*ctx
,
440 GLhandleARB programObj
,
441 struct gl_shader_program
*shProg
)
443 const struct gl_vertex_program
*vertProg
;
444 const struct gl_fragment_program
*fragProg
;
445 GLuint numSamplers
= 0;
448 _mesa_clear_shader_program_data(ctx
, shProg
);
450 /* check that all programs compiled successfully */
451 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
452 if (!shProg
->Shaders
[i
]->CompileStatus
) {
453 link_error(shProg
, "linking with uncompiled shader\n");
458 shProg
->Uniforms
= _mesa_new_uniform_list();
459 shProg
->Varying
= _mesa_new_parameter_list();
462 * Find attached vertex, fragment shaders defining main()
466 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
467 struct gl_shader
*shader
= shProg
->Shaders
[i
];
468 if (shader
->Type
== GL_VERTEX_SHADER
) {
470 vertProg
= vertex_program(shader
->Program
);
472 else if (shader
->Type
== GL_FRAGMENT_SHADER
) {
474 fragProg
= fragment_program(shader
->Program
);
477 _mesa_problem(ctx
, "unexpected shader target in slang_link()");
482 /* must have both a vertex and fragment program for ES2 */
484 link_error(shProg
, "missing vertex shader\n");
488 link_error(shProg
, "missing fragment shader\n");
494 * Make copies of the vertex/fragment programs now since we'll be
495 * changing src/dst registers after merging the uniforms and varying vars.
497 _mesa_reference_vertprog(ctx
, &shProg
->VertexProgram
, NULL
);
499 struct gl_vertex_program
*linked_vprog
=
500 vertex_program(_mesa_clone_program(ctx
, &vertProg
->Base
));
501 shProg
->VertexProgram
= linked_vprog
; /* refcount OK */
502 ASSERT(shProg
->VertexProgram
->Base
.RefCount
== 1);
505 _mesa_reference_fragprog(ctx
, &shProg
->FragmentProgram
, NULL
);
507 struct gl_fragment_program
*linked_fprog
=
508 fragment_program(_mesa_clone_program(ctx
, &fragProg
->Base
));
509 shProg
->FragmentProgram
= linked_fprog
; /* refcount OK */
510 ASSERT(shProg
->FragmentProgram
->Base
.RefCount
== 1);
513 /* link varying vars */
514 if (shProg
->VertexProgram
) {
515 if (!link_varying_vars(shProg
, &shProg
->VertexProgram
->Base
))
518 if (shProg
->FragmentProgram
) {
519 if (!link_varying_vars(shProg
, &shProg
->FragmentProgram
->Base
))
523 /* link uniform vars */
524 if (shProg
->VertexProgram
)
525 link_uniform_vars(shProg
, &shProg
->VertexProgram
->Base
, &numSamplers
);
526 if (shProg
->FragmentProgram
)
527 link_uniform_vars(shProg
, &shProg
->FragmentProgram
->Base
, &numSamplers
);
529 /*_mesa_print_uniforms(shProg->Uniforms);*/
531 if (shProg
->VertexProgram
) {
532 if (!_slang_resolve_attributes(shProg
, &vertProg
->Base
,
533 &shProg
->VertexProgram
->Base
)) {
538 if (shProg
->VertexProgram
) {
539 _slang_update_inputs_outputs(&shProg
->VertexProgram
->Base
);
540 _slang_count_temporaries(&shProg
->VertexProgram
->Base
);
541 if (!(shProg
->VertexProgram
->Base
.OutputsWritten
& (1 << VERT_RESULT_HPOS
))) {
542 /* the vertex program did not compute a vertex position */
544 "gl_Position was not written by vertex shader\n");
548 if (shProg
->FragmentProgram
) {
549 _slang_count_temporaries(&shProg
->FragmentProgram
->Base
);
550 _slang_update_inputs_outputs(&shProg
->FragmentProgram
->Base
);
553 /* Check that all the varying vars needed by the fragment shader are
554 * actually produced by the vertex shader.
556 if (shProg
->FragmentProgram
) {
557 const GLbitfield varyingRead
558 = shProg
->FragmentProgram
->Base
.InputsRead
>> FRAG_ATTRIB_VAR0
;
559 const GLbitfield varyingWritten
= shProg
->VertexProgram
?
560 shProg
->VertexProgram
->Base
.OutputsWritten
>> VERT_RESULT_VAR0
: 0x0;
561 if ((varyingRead
& varyingWritten
) != varyingRead
) {
563 "Fragment program using varying vars not written by vertex shader\n");
569 if (fragProg
&& shProg
->FragmentProgram
) {
570 /* Compute initial program's TexturesUsed info */
571 _mesa_update_shader_textures_used(&shProg
->FragmentProgram
->Base
);
573 /* notify driver that a new fragment program has been compiled/linked */
574 ctx
->Driver
.ProgramStringNotify(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
575 &shProg
->FragmentProgram
->Base
);
576 if (MESA_VERBOSE
& VERBOSE_GLSL_DUMP
) {
577 printf("Mesa original fragment program:\n");
578 _mesa_print_program(&fragProg
->Base
);
579 _mesa_print_program_parameters(ctx
, &fragProg
->Base
);
581 printf("Mesa post-link fragment program:\n");
582 _mesa_print_program(&shProg
->FragmentProgram
->Base
);
583 _mesa_print_program_parameters(ctx
, &shProg
->FragmentProgram
->Base
);
587 if (vertProg
&& shProg
->VertexProgram
) {
588 /* Compute initial program's TexturesUsed info */
589 _mesa_update_shader_textures_used(&shProg
->VertexProgram
->Base
);
591 /* notify driver that a new vertex program has been compiled/linked */
592 ctx
->Driver
.ProgramStringNotify(ctx
, GL_VERTEX_PROGRAM_ARB
,
593 &shProg
->VertexProgram
->Base
);
594 if (MESA_VERBOSE
& VERBOSE_GLSL_DUMP
) {
595 printf("Mesa original vertex program:\n");
596 _mesa_print_program(&vertProg
->Base
);
597 _mesa_print_program_parameters(ctx
, &vertProg
->Base
);
599 printf("Mesa post-link vertex program:\n");
600 _mesa_print_program(&shProg
->VertexProgram
->Base
);
601 _mesa_print_program_parameters(ctx
, &shProg
->VertexProgram
->Base
);
605 shProg
->LinkStatus
= (shProg
->VertexProgram
|| shProg
->FragmentProgram
);