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.
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"
47 * Linking varying vars involves rearranging varying vars so that the
48 * vertex program's output varyings matches the order of the fragment
49 * program's input varyings.
52 link_varying_vars(struct gl_shader_program
*shProg
, struct gl_program
*prog
)
54 GLuint
*map
, i
, firstVarying
, newFile
;
55 GLbitfield varsWritten
, varsRead
;
57 map
= (GLuint
*) malloc(prog
->Varying
->NumParameters
* sizeof(GLuint
));
61 for (i
= 0; i
< prog
->Varying
->NumParameters
; i
++) {
62 /* see if this varying is in the linked varying list */
63 const struct gl_program_parameter
*var
64 = prog
->Varying
->Parameters
+ i
;
66 GLint j
= _mesa_lookup_parameter_index(shProg
->Varying
, -1, var
->Name
);
68 /* already in list, check size */
69 if (var
->Size
!= shProg
->Varying
->Parameters
[j
].Size
) {
75 /* not already in linked list */
76 j
= _mesa_add_varying(shProg
->Varying
, var
->Name
, var
->Size
);
84 /* Varying variables are treated like other vertex program outputs
85 * (and like other fragment program inputs). The position of the
86 * first varying differs for vertex/fragment programs...
87 * Also, replace File=PROGRAM_VARYING with File=PROGRAM_INPUT/OUTPUT.
89 if (prog
->Target
== GL_VERTEX_PROGRAM_ARB
) {
90 firstVarying
= VERT_RESULT_VAR0
;
91 newFile
= PROGRAM_OUTPUT
;
94 assert(prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
);
95 firstVarying
= FRAG_ATTRIB_VAR0
;
96 newFile
= PROGRAM_INPUT
;
99 /* keep track of which varying vars we read and write */
100 varsWritten
= varsRead
= 0x0;
102 /* OK, now scan the program/shader instructions looking for varying vars,
103 * replacing the old index with the new index.
105 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
106 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
109 if (inst
->DstReg
.File
== PROGRAM_VARYING
) {
110 inst
->DstReg
.File
= newFile
;
111 inst
->DstReg
.Index
= map
[ inst
->DstReg
.Index
] + firstVarying
;
112 varsWritten
|= (1 << inst
->DstReg
.Index
);
115 for (j
= 0; j
< 3; j
++) {
116 if (inst
->SrcReg
[j
].File
== PROGRAM_VARYING
) {
117 inst
->SrcReg
[j
].File
= newFile
;
118 inst
->SrcReg
[j
].Index
= map
[ inst
->SrcReg
[j
].Index
] + firstVarying
;
119 varsRead
|= (1 << inst
->SrcReg
[j
].Index
);
124 if (prog
->Target
== GL_VERTEX_PROGRAM_ARB
) {
125 prog
->OutputsWritten
|= varsWritten
;
126 /*printf("VERT OUTPUTS: 0x%x \n", varsWritten);*/
129 assert(prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
);
130 prog
->InputsRead
|= varsRead
;
131 /*printf("FRAG INPUTS: 0x%x\n", varsRead);*/
141 * Build the shProg->Uniforms list.
142 * This is basically a list/index of all uniforms found in either/both of
143 * the vertex and fragment shaders.
146 link_uniform_vars(struct gl_shader_program
*shProg
,
147 struct gl_program
*prog
,
150 GLuint samplerMap
[MAX_SAMPLERS
];
153 for (i
= 0; i
< prog
->Parameters
->NumParameters
; i
++) {
154 const struct gl_program_parameter
*p
= prog
->Parameters
->Parameters
+ i
;
157 * XXX FIX NEEDED HERE
158 * We should also be adding a uniform if p->Type == PROGRAM_STATE_VAR.
159 * For example, modelview matrix, light pos, etc.
160 * Also, we need to update the state-var name-generator code to
161 * generate GLSL-style names, like "gl_LightSource[0].position".
162 * Furthermore, we'll need to fix the state-var's size/datatype info.
165 if (p
->Type
== PROGRAM_UNIFORM
||
166 p
->Type
== PROGRAM_SAMPLER
) {
167 _mesa_append_uniform(shProg
->Uniforms
, p
->Name
, prog
->Target
, i
);
170 if (p
->Type
== PROGRAM_SAMPLER
) {
171 /* Allocate a new sampler index */
172 GLuint sampNum
= *numSamplers
;
173 GLuint oldSampNum
= (GLuint
) prog
->Parameters
->ParameterValues
[i
][0];
174 assert(oldSampNum
< MAX_SAMPLERS
);
175 samplerMap
[oldSampNum
] = sampNum
;
181 /* OK, now scan the program/shader instructions looking for sampler vars,
182 * replacing the old index with the new index.
184 prog
->SamplersUsed
= 0x0;
185 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
186 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
187 if (_mesa_is_tex_instruction(inst
->Opcode
)) {
189 printf("====== remap sampler from %d to %d\n",
190 inst->Sampler, map[ inst->Sampler ]);
192 /* here, texUnit is really samplerUnit */
193 inst
->TexSrcUnit
= samplerMap
[inst
->TexSrcUnit
];
194 prog
->SamplerTargets
[inst
->TexSrcUnit
] = inst
->TexSrcTarget
;
195 prog
->SamplersUsed
|= (1 << inst
->TexSrcUnit
);
203 * Resolve binding of generic vertex attributes.
204 * For example, if the vertex shader declared "attribute vec4 foobar" we'll
205 * allocate a generic vertex attribute for "foobar" and plug that value into
206 * the vertex program instructions.
209 _slang_resolve_attributes(struct gl_shader_program
*shProg
,
210 struct gl_program
*prog
)
213 GLbitfield usedAttributes
;
214 GLint size
= 4; /* XXX fix */
216 assert(prog
->Target
== GL_VERTEX_PROGRAM_ARB
);
218 if (!shProg
->Attributes
)
219 shProg
->Attributes
= _mesa_new_parameter_list();
221 /* Build a bitmask indicating which attribute indexes have been
222 * explicitly bound by the user with glBindAttributeLocation().
224 usedAttributes
= 0x0;
225 for (i
= 0; i
< shProg
->Attributes
->NumParameters
; i
++) {
226 GLint attr
= shProg
->Attributes
->Parameters
[i
].StateIndexes
[0];
227 usedAttributes
|= attr
;
231 * Scan program for generic attribute references
233 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
234 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
235 for (j
= 0; j
< 3; j
++) {
236 if (inst
->SrcReg
[j
].File
== PROGRAM_INPUT
&&
237 inst
->SrcReg
[j
].Index
>= VERT_ATTRIB_GENERIC0
) {
238 /* this is a generic attrib */
239 const GLint k
= inst
->SrcReg
[j
].Index
- VERT_ATTRIB_GENERIC0
;
240 const char *name
= prog
->Attributes
->Parameters
[k
].Name
;
241 /* See if this attrib name is in the program's attribute list
242 * (i.e. was bound by the user).
244 GLint index
= _mesa_lookup_parameter_index(shProg
->Attributes
,
248 /* found, user must have specified a binding */
249 attr
= shProg
->Attributes
->Parameters
[index
].StateIndexes
[0];
252 /* Not found, choose our own attribute number.
253 * Start at 1 since generic attribute 0 always aliases
256 for (attr
= 1; attr
< MAX_VERTEX_ATTRIBS
; attr
++) {
257 if (((1 << attr
) & usedAttributes
) == 0)
260 if (attr
== MAX_VERTEX_ATTRIBS
) {
261 /* too many! XXX record error log */
264 _mesa_add_attribute(shProg
->Attributes
, name
, size
, attr
);
266 /* set the attribute as used */
267 usedAttributes
|= 1<<attr
;
270 inst
->SrcReg
[j
].Index
= VERT_ATTRIB_GENERIC0
+ attr
;
279 * Scan program instructions to update the program's InputsRead and
280 * OutputsWritten fields.
283 _slang_update_inputs_outputs(struct gl_program
*prog
)
287 prog
->InputsRead
= 0x0;
288 prog
->OutputsWritten
= 0x0;
290 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
291 const struct prog_instruction
*inst
= prog
->Instructions
+ i
;
292 const GLuint numSrc
= _mesa_num_inst_src_regs(inst
->Opcode
);
293 for (j
= 0; j
< numSrc
; j
++) {
294 if (inst
->SrcReg
[j
].File
== PROGRAM_INPUT
) {
295 prog
->InputsRead
|= 1 << inst
->SrcReg
[j
].Index
;
298 if (inst
->DstReg
.File
== PROGRAM_OUTPUT
) {
299 prog
->OutputsWritten
|= 1 << inst
->DstReg
.Index
;
306 * Scan a vertex program looking for instances of
307 * (PROGRAM_INPUT, VERT_ATTRIB_GENERIC0 + oldAttrib) and replace with
308 * (PROGRAM_INPUT, VERT_ATTRIB_GENERIC0 + newAttrib).
309 * This is used when the user calls glBindAttribLocation on an already linked
313 _slang_remap_attribute(struct gl_program
*prog
, GLuint oldAttrib
, GLuint newAttrib
)
317 assert(prog
->Target
== GL_VERTEX_PROGRAM_ARB
);
319 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
320 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
321 for (j
= 0; j
< 3; j
++) {
322 if (inst
->SrcReg
[j
].File
== PROGRAM_INPUT
) {
323 if (inst
->SrcReg
[j
].Index
== VERT_ATTRIB_GENERIC0
+ oldAttrib
) {
324 inst
->SrcReg
[j
].Index
= VERT_ATTRIB_GENERIC0
+ newAttrib
;
330 _slang_update_inputs_outputs(prog
);
336 static struct gl_vertex_program
*
337 vertex_program(struct gl_program
*prog
)
339 assert(prog
->Target
== GL_VERTEX_PROGRAM_ARB
);
340 return (struct gl_vertex_program
*) prog
;
345 static struct gl_fragment_program
*
346 fragment_program(struct gl_program
*prog
)
348 assert(prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
);
349 return (struct gl_fragment_program
*) prog
;
354 * Record a linking error.
357 link_error(struct gl_shader_program
*shProg
, const char *msg
)
359 if (shProg
->InfoLog
) {
360 _mesa_free(shProg
->InfoLog
);
362 shProg
->InfoLog
= _mesa_strdup(msg
);
363 shProg
->LinkStatus
= GL_FALSE
;
369 * Shader linker. Currently:
371 * 1. The last attached vertex shader and fragment shader are linked.
372 * 2. Varying vars in the two shaders are combined so their locations
373 * agree between the vertex and fragment stages. They're treated as
374 * vertex program output attribs and as fragment program input attribs.
375 * 3. The vertex and fragment programs are cloned and modified to update
376 * src/dst register references so they use the new, linked varying
380 _slang_link(GLcontext
*ctx
,
381 GLhandleARB programObj
,
382 struct gl_shader_program
*shProg
)
384 const struct gl_vertex_program
*vertProg
;
385 const struct gl_fragment_program
*fragProg
;
386 GLuint numSamplers
= 0;
389 _mesa_clear_shader_program_data(ctx
, shProg
);
391 /* check that all programs compiled successfully */
392 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
393 if (!shProg
->Shaders
[i
]->CompileStatus
) {
394 link_error(shProg
, "linking with uncompiled shader\n");
399 shProg
->Uniforms
= _mesa_new_uniform_list();
400 shProg
->Varying
= _mesa_new_parameter_list();
403 * Find attached vertex shader, fragment shader
407 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
408 if (shProg
->Shaders
[i
]->Type
== GL_VERTEX_SHADER
)
409 vertProg
= vertex_program(shProg
->Shaders
[i
]->Programs
[0]);
410 else if (shProg
->Shaders
[i
]->Type
== GL_FRAGMENT_SHADER
)
411 fragProg
= fragment_program(shProg
->Shaders
[i
]->Programs
[0]);
413 _mesa_problem(ctx
, "unexpected shader target in slang_link()");
417 * Make copies of the vertex/fragment programs now since we'll be
418 * changing src/dst registers after merging the uniforms and varying vars.
420 _mesa_reference_vertprog(ctx
, &shProg
->VertexProgram
, NULL
);
422 struct gl_vertex_program
*linked_vprog
=
423 vertex_program(_mesa_clone_program(ctx
, &vertProg
->Base
));
424 shProg
->VertexProgram
= linked_vprog
; /* refcount OK */
425 ASSERT(shProg
->VertexProgram
->Base
.RefCount
== 1);
428 _mesa_reference_fragprog(ctx
, &shProg
->FragmentProgram
, NULL
);
430 struct gl_fragment_program
*linked_fprog
=
431 fragment_program(_mesa_clone_program(ctx
, &fragProg
->Base
));
432 shProg
->FragmentProgram
= linked_fprog
; /* refcount OK */
433 ASSERT(shProg
->FragmentProgram
->Base
.RefCount
== 1);
436 /* link varying vars */
437 if (shProg
->VertexProgram
)
438 link_varying_vars(shProg
, &shProg
->VertexProgram
->Base
);
439 if (shProg
->FragmentProgram
)
440 link_varying_vars(shProg
, &shProg
->FragmentProgram
->Base
);
442 /* link uniform vars */
443 if (shProg
->VertexProgram
)
444 link_uniform_vars(shProg
, &shProg
->VertexProgram
->Base
, &numSamplers
);
445 if (shProg
->FragmentProgram
)
446 link_uniform_vars(shProg
, &shProg
->FragmentProgram
->Base
, &numSamplers
);
448 /*_mesa_print_uniforms(shProg->Uniforms);*/
450 if (shProg
->VertexProgram
) {
451 if (!_slang_resolve_attributes(shProg
, &shProg
->VertexProgram
->Base
)) {
453 _mesa_problem(ctx
, "_slang_resolve_attributes() failed");
458 if (shProg
->VertexProgram
) {
459 _slang_update_inputs_outputs(&shProg
->VertexProgram
->Base
);
460 if (!(shProg
->VertexProgram
->Base
.OutputsWritten
& (1 << VERT_RESULT_HPOS
))) {
461 /* the vertex program did not compute a vertex position */
463 "gl_Position was not written by vertex shader\n");
467 if (shProg
->FragmentProgram
)
468 _slang_update_inputs_outputs(&shProg
->FragmentProgram
->Base
);
470 /* Check that all the varying vars needed by the fragment shader are
471 * actually produced by the vertex shader.
473 if (shProg
->FragmentProgram
) {
474 const GLbitfield varyingRead
475 = shProg
->FragmentProgram
->Base
.InputsRead
>> FRAG_ATTRIB_VAR0
;
476 const GLbitfield varyingWritten
= shProg
->VertexProgram
?
477 shProg
->VertexProgram
->Base
.OutputsWritten
>> VERT_RESULT_VAR0
: 0x0;
478 if ((varyingRead
& varyingWritten
) != varyingRead
) {
480 "Fragment program using varying vars not written by vertex shader\n");
485 /* Check that the vertex program doesn't use too many sampler units */
486 if (shProg
->VertexProgram
&&
487 _mesa_bitcount(shProg
->VertexProgram
->Base
.SamplersUsed
) > ctx
->Const
.MaxVertexTextureImageUnits
) {
488 link_error(shProg
, "Vertex program uses too many samplers.\n");
492 if (fragProg
&& shProg
->FragmentProgram
) {
493 /* notify driver that a new fragment program has been compiled/linked */
494 ctx
->Driver
.ProgramStringNotify(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
495 &shProg
->FragmentProgram
->Base
);
497 printf("************** original fragment program\n");
498 _mesa_print_program(&fragProg
->Base
);
499 _mesa_print_program_parameters(ctx
, &fragProg
->Base
);
502 printf("************** linked fragment prog\n");
503 _mesa_print_program(&shProg
->FragmentProgram
->Base
);
504 _mesa_print_program_parameters(ctx
, &shProg
->FragmentProgram
->Base
);
508 if (vertProg
&& shProg
->VertexProgram
) {
509 /* notify driver that a new vertex program has been compiled/linked */
510 ctx
->Driver
.ProgramStringNotify(ctx
, GL_VERTEX_PROGRAM_ARB
,
511 &shProg
->VertexProgram
->Base
);
513 printf("************** original vertex program\n");
514 _mesa_print_program(&vertProg
->Base
);
515 _mesa_print_program_parameters(ctx
, &vertProg
->Base
);
518 printf("************** linked vertex prog\n");
519 _mesa_print_program(&shProg
->VertexProgram
->Base
);
520 _mesa_print_program_parameters(ctx
, &shProg
->VertexProgram
->Base
);
524 shProg
->LinkStatus
= (shProg
->VertexProgram
|| shProg
->FragmentProgram
);