replace GLint with gl_state_index
[mesa.git] / src / mesa / shader / slang / slang_link.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.3
4 *
5 * Copyright (C) 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 slang_link.c
27 * GLSL linker
28 * \author Brian Paul
29 */
30
31 #include "imports.h"
32 #include "context.h"
33 #include "hash.h"
34 #include "macros.h"
35 #include "program.h"
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"
42
43
44
45
46 static GLboolean
47 link_varying_vars(struct gl_shader_program *shProg, struct gl_program *prog)
48 {
49 GLuint *map, i, firstVarying, newFile;
50 GLbitfield varsWritten, varsRead;
51
52 map = (GLuint *) malloc(prog->Varying->NumParameters * sizeof(GLuint));
53 if (!map)
54 return GL_FALSE;
55
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;
60
61 GLint j = _mesa_lookup_parameter_index(shProg->Varying, -1, var->Name);
62 if (j >= 0) {
63 /* already in list, check size */
64 if (var->Size != shProg->Varying->Parameters[j].Size) {
65 /* error */
66 return GL_FALSE;
67 }
68 }
69 else {
70 /* not already in linked list */
71 j = _mesa_add_varying(shProg->Varying, var->Name, var->Size);
72 }
73 ASSERT(j >= 0);
74
75 map[i] = j;
76 }
77
78
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.
83 */
84 if (prog->Target == GL_VERTEX_PROGRAM_ARB) {
85 firstVarying = VERT_RESULT_VAR0;
86 newFile = PROGRAM_OUTPUT;
87 }
88 else {
89 assert(prog->Target == GL_FRAGMENT_PROGRAM_ARB);
90 firstVarying = FRAG_ATTRIB_VAR0;
91 newFile = PROGRAM_INPUT;
92 }
93
94 /* keep track of which varying vars we read and write */
95 varsWritten = varsRead = 0x0;
96
97 /* OK, now scan the program/shader instructions looking for varying vars,
98 * replacing the old index with the new index.
99 */
100 for (i = 0; i < prog->NumInstructions; i++) {
101 struct prog_instruction *inst = prog->Instructions + i;
102 GLuint j;
103
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);
108 }
109
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);
115 }
116 }
117 /* XXX update program OutputsWritten, InputsRead */
118 }
119
120 if (prog->Target == GL_VERTEX_PROGRAM_ARB) {
121 prog->OutputsWritten |= varsWritten;
122 }
123 else {
124 assert(prog->Target == GL_FRAGMENT_PROGRAM_ARB);
125 prog->InputsRead |= varsRead;
126 }
127
128
129 free(map);
130
131 return GL_TRUE;
132 }
133
134
135 static GLboolean
136 is_uniform(GLuint file)
137 {
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);
144 }
145
146
147 static GLboolean
148 link_uniform_vars(struct gl_shader_program *shProg, struct gl_program *prog)
149 {
150 GLuint *map, i;
151
152 #if 0
153 printf("================ pre link uniforms ===============\n");
154 _mesa_print_parameter_list(shProg->Uniforms);
155 #endif
156
157 map = (GLuint *) malloc(prog->Parameters->NumParameters * sizeof(GLuint));
158 if (!map)
159 return GL_FALSE;
160
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];
165 GLint j;
166 GLint size;
167
168 /* sanity check */
169 assert(is_uniform(p->Type));
170
171 if (p->Name) {
172 j = _mesa_lookup_parameter_index(shProg->Uniforms, -1, p->Name);
173 }
174 else {
175 GLuint swizzle;
176 ASSERT(p->Type == PROGRAM_CONSTANT);
177 if (_mesa_lookup_parameter_constant(shProg->Uniforms, pVals,
178 p->Size, &j, &swizzle)) {
179 assert(j >= 0);
180 }
181 else {
182 j = -1;
183 }
184 }
185
186 if (j >= 0) {
187 /* already in list, check size XXX check this */
188 #if 0
189 assert(p->Size == shProg->Uniforms->Parameters[j].Size);
190 #endif
191 }
192 else {
193 /* not already in linked list */
194 switch (p->Type) {
195 case PROGRAM_ENV_PARAM:
196 j = _mesa_add_named_parameter(shProg->Uniforms, p->Name, pVals);
197 break;
198 case PROGRAM_CONSTANT:
199 j = _mesa_add_named_constant(shProg->Uniforms, p->Name, pVals, p->Size);
200 break;
201 case PROGRAM_STATE_VAR:
202 j = _mesa_add_state_reference(shProg->Uniforms, p->StateIndexes);
203 break;
204 case PROGRAM_UNIFORM:
205 j = _mesa_add_uniform(shProg->Uniforms, p->Name, p->Size);
206 break;
207 case PROGRAM_SAMPLER:
208 j = _mesa_add_sampler(shProg->Uniforms, p->Name);
209 break;
210 default:
211 abort();
212 }
213
214 }
215 ASSERT(j >= 0);
216
217 size = p->Size;
218 while (size > 0) {
219 map[i] = j;
220 i++;
221 j++;
222 size -= 4;
223 }
224
225 }
226
227 #if 0
228 printf("================ post link uniforms ===============\n");
229 _mesa_print_parameter_list(shProg->Uniforms);
230 #endif
231
232 #if 0
233 {
234 GLuint i;
235 for (i = 0; i < prog->Parameters->NumParameters; i++) {
236 printf("map[%d] = %d\n", i, map[i]);
237 }
238 _mesa_print_parameter_list(shProg->Uniforms);
239 }
240 #endif
241
242 /* OK, now scan the program/shader instructions looking for uniform vars,
243 * replacing the old index with the new index.
244 */
245 for (i = 0; i < prog->NumInstructions; i++) {
246 struct prog_instruction *inst = prog->Instructions + i;
247 GLuint j;
248
249 if (is_uniform(inst->DstReg.File)) {
250 inst->DstReg.Index = map[ inst->DstReg.Index ];
251 }
252
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 ];
256 }
257 }
258
259 if (inst->Opcode == OPCODE_TEX ||
260 inst->Opcode == OPCODE_TXB ||
261 inst->Opcode == OPCODE_TXP) {
262 /*
263 printf("====== remap sampler from %d to %d\n",
264 inst->Sampler, map[ inst->Sampler ]);
265 */
266 inst->Sampler = map[ inst->Sampler ];
267 }
268 }
269
270 free(map);
271
272 return GL_TRUE;
273 }
274
275
276 /**
277 * Resolve binding of generic vertex attributes.
278 * For example, if the vertex shader declared "attribute vec4 foobar" we'll
279 * allocate a generic vertex attribute for "foobar" and plug that value into
280 * the vertex program instructions.
281 */
282 static GLboolean
283 _slang_resolve_attributes(struct gl_shader_program *shProg,
284 struct gl_program *prog)
285 {
286 GLuint i, j;
287 GLbitfield usedAttributes;
288 GLint size = 4; /* XXX fix */
289
290 assert(prog->Target == GL_VERTEX_PROGRAM_ARB);
291
292 /* Build a bitmask indicating which attribute indexes have been
293 * explicitly bound by the user with glBindAttributeLocation().
294 */
295 usedAttributes = 0x0;
296 for (i = 0; i < shProg->Attributes->NumParameters; i++) {
297 GLint attr = shProg->Attributes->Parameters[i].StateIndexes[0];
298 usedAttributes |= attr;
299 }
300
301 if (!shProg->Attributes)
302 shProg->Attributes = _mesa_new_parameter_list();
303
304 /*
305 * Scan program for generic attribute references
306 */
307 for (i = 0; i < prog->NumInstructions; i++) {
308 struct prog_instruction *inst = prog->Instructions + i;
309 for (j = 0; j < 3; j++) {
310 if (inst->SrcReg[j].File == PROGRAM_INPUT &&
311 inst->SrcReg[j].Index >= VERT_ATTRIB_GENERIC0) {
312 /* this is a generic attrib */
313 const GLint k = inst->SrcReg[j].Index - VERT_ATTRIB_GENERIC0;
314 const char *name = prog->Attributes->Parameters[k].Name;
315 /* See if this attrib name is in the program's attribute list
316 * (i.e. was bound by the user).
317 */
318 GLint index = _mesa_lookup_parameter_index(shProg->Attributes,
319 -1, name);
320 GLint attr;
321 if (index >= 0) {
322 /* found, user must have specified a binding */
323 attr = shProg->Attributes->Parameters[index].StateIndexes[0];
324 }
325 else {
326 /* Not found, choose our own attribute number.
327 * Start at 1 since generic attribute 0 always aliases
328 * glVertex/position.
329 */
330 for (attr = 1; attr < MAX_VERTEX_ATTRIBS; attr++) {
331 if (((1 << attr) & usedAttributes) == 0)
332 break;
333 }
334 if (attr == MAX_VERTEX_ATTRIBS) {
335 /* too many! XXX record error log */
336 return GL_FALSE;
337 }
338 _mesa_add_attribute(shProg->Attributes, name, size, attr);
339 }
340
341 inst->SrcReg[j].Index = VERT_ATTRIB_GENERIC0 + attr;
342 }
343 }
344 }
345 return GL_TRUE;
346 }
347
348
349 /**
350 * Scan program instructions to update the program's InputsRead and
351 * OutputsWritten fields.
352 */
353 static void
354 _slang_update_inputs_outputs(struct gl_program *prog)
355 {
356 GLuint i, j;
357
358 prog->InputsRead = 0x0;
359 prog->OutputsWritten = 0x0;
360
361 for (i = 0; i < prog->NumInstructions; i++) {
362 const struct prog_instruction *inst = prog->Instructions + i;
363 const GLuint numSrc = _mesa_num_inst_src_regs(inst->Opcode);
364 for (j = 0; j < numSrc; j++) {
365 if (inst->SrcReg[j].File == PROGRAM_INPUT) {
366 prog->InputsRead |= 1 << inst->SrcReg[j].Index;
367 }
368 }
369 if (inst->DstReg.File == PROGRAM_OUTPUT) {
370 prog->OutputsWritten |= 1 << inst->DstReg.Index;
371 }
372 }
373 }
374
375
376 /**
377 * Scan a vertex program looking for instances of
378 * (PROGRAM_INPUT, VERT_ATTRIB_GENERIC0 + oldAttrib) and replace with
379 * (PROGRAM_INPUT, VERT_ATTRIB_GENERIC0 + newAttrib).
380 * This is used when the user calls glBindAttribLocation on an already linked
381 * shader program.
382 */
383 void
384 _slang_remap_attribute(struct gl_program *prog, GLuint oldAttrib, GLuint newAttrib)
385 {
386 GLuint i, j;
387
388 assert(prog->Target == GL_VERTEX_PROGRAM_ARB);
389
390 for (i = 0; i < prog->NumInstructions; i++) {
391 struct prog_instruction *inst = prog->Instructions + i;
392 for (j = 0; j < 3; j++) {
393 if (inst->SrcReg[j].File == PROGRAM_INPUT) {
394 if (inst->SrcReg[j].Index == VERT_ATTRIB_GENERIC0 + oldAttrib) {
395 inst->SrcReg[j].Index = VERT_ATTRIB_GENERIC0 + newAttrib;
396 }
397 }
398 }
399 }
400
401 _slang_update_inputs_outputs(prog);
402 }
403
404
405
406 /**
407 * Scan program for texture instructions, lookup sampler/uniform's value
408 * to determine which texture unit to use.
409 * Also, update the program's TexturesUsed[] array.
410 */
411 void
412 _slang_resolve_samplers(struct gl_shader_program *shProg,
413 struct gl_program *prog)
414 {
415 GLuint i;
416
417 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++)
418 prog->TexturesUsed[i] = 0;
419
420 for (i = 0; i < prog->NumInstructions; i++) {
421 struct prog_instruction *inst = prog->Instructions + i;
422 if (inst->Opcode == OPCODE_TEX ||
423 inst->Opcode == OPCODE_TXB ||
424 inst->Opcode == OPCODE_TXP) {
425 GLint sampleUnit = (GLint) shProg->Uniforms->ParameterValues[inst->Sampler][0];
426 assert(sampleUnit < MAX_TEXTURE_IMAGE_UNITS);
427 inst->TexSrcUnit = sampleUnit;
428
429 prog->TexturesUsed[inst->TexSrcUnit] |= (1 << inst->TexSrcTarget);
430 }
431 }
432 }
433
434
435
436 /** cast wrapper */
437 static struct gl_vertex_program *
438 vertex_program(struct gl_program *prog)
439 {
440 assert(prog->Target == GL_VERTEX_PROGRAM_ARB);
441 return (struct gl_vertex_program *) prog;
442 }
443
444
445 /** cast wrapper */
446 static struct gl_fragment_program *
447 fragment_program(struct gl_program *prog)
448 {
449 assert(prog->Target == GL_FRAGMENT_PROGRAM_ARB);
450 return (struct gl_fragment_program *) prog;
451 }
452
453
454 /**
455 * Shader linker. Currently:
456 *
457 * 1. The last attached vertex shader and fragment shader are linked.
458 * 2. Varying vars in the two shaders are combined so their locations
459 * agree between the vertex and fragment stages. They're treated as
460 * vertex program output attribs and as fragment program input attribs.
461 * 3. Uniform vars (including state references, constants, etc) from the
462 * vertex and fragment shaders are merged into one group. Recall that
463 * GLSL uniforms are shared by all linked shaders.
464 * 4. The vertex and fragment programs are cloned and modified to update
465 * src/dst register references so they use the new, linked uniform/
466 * varying storage locations.
467 */
468 void
469 _slang_link(GLcontext *ctx,
470 GLhandleARB programObj,
471 struct gl_shader_program *shProg)
472 {
473 const struct gl_vertex_program *vertProg;
474 const struct gl_fragment_program *fragProg;
475 GLuint i;
476
477 _mesa_free_shader_program_data(ctx, shProg);
478
479 shProg->Uniforms = _mesa_new_parameter_list();
480 shProg->Varying = _mesa_new_parameter_list();
481
482 /**
483 * Find attached vertex shader, fragment shader
484 */
485 vertProg = NULL;
486 fragProg = NULL;
487 for (i = 0; i < shProg->NumShaders; i++) {
488 if (shProg->Shaders[i]->Type == GL_VERTEX_SHADER)
489 vertProg = vertex_program(shProg->Shaders[i]->Programs[0]);
490 else if (shProg->Shaders[i]->Type == GL_FRAGMENT_SHADER)
491 fragProg = fragment_program(shProg->Shaders[i]->Programs[0]);
492 else
493 _mesa_problem(ctx, "unexpected shader target in slang_link()");
494 }
495
496 /*
497 * Make copies of the vertex/fragment programs now since we'll be
498 * changing src/dst registers after merging the uniforms and varying vars.
499 */
500 if (vertProg) {
501 shProg->VertexProgram
502 = vertex_program(_mesa_clone_program(ctx, &vertProg->Base));
503 }
504 else {
505 shProg->VertexProgram = NULL;
506 }
507
508 if (fragProg) {
509 shProg->FragmentProgram
510 = fragment_program(_mesa_clone_program(ctx, &fragProg->Base));
511 }
512 else {
513 shProg->FragmentProgram = NULL;
514 }
515
516 if (shProg->VertexProgram)
517 link_varying_vars(shProg, &shProg->VertexProgram->Base);
518 if (shProg->FragmentProgram)
519 link_varying_vars(shProg, &shProg->FragmentProgram->Base);
520
521 if (shProg->VertexProgram)
522 link_uniform_vars(shProg, &shProg->VertexProgram->Base);
523 if (shProg->FragmentProgram)
524 link_uniform_vars(shProg, &shProg->FragmentProgram->Base);
525
526 /* The vertex and fragment programs share a common set of uniforms now */
527 if (shProg->VertexProgram) {
528 _mesa_free_parameter_list(shProg->VertexProgram->Base.Parameters);
529 shProg->VertexProgram->Base.Parameters = shProg->Uniforms;
530 }
531 if (shProg->FragmentProgram) {
532 _mesa_free_parameter_list(shProg->FragmentProgram->Base.Parameters);
533 shProg->FragmentProgram->Base.Parameters = shProg->Uniforms;
534 }
535
536 if (shProg->VertexProgram) {
537 _slang_resolve_samplers(shProg, &shProg->VertexProgram->Base);
538 }
539 if (shProg->FragmentProgram) {
540 _slang_resolve_samplers(shProg, &shProg->FragmentProgram->Base);
541 }
542
543 if (shProg->VertexProgram) {
544 if (!_slang_resolve_attributes(shProg, &shProg->VertexProgram->Base)) {
545 /*goto cleanup;*/
546 _mesa_problem(ctx, "_slang_resolve_attributes() failed");
547 abort(); /* XXX fix */
548 }
549 }
550
551 if (shProg->VertexProgram)
552 _slang_update_inputs_outputs(&shProg->VertexProgram->Base);
553 if (shProg->FragmentProgram)
554 _slang_update_inputs_outputs(&shProg->FragmentProgram->Base);
555
556 if (fragProg && shProg->FragmentProgram) {
557 #if 1
558 printf("************** original fragment program\n");
559 _mesa_print_program(&fragProg->Base);
560 _mesa_print_program_parameters(ctx, &fragProg->Base);
561 #endif
562 #if 1
563 printf("************** linked fragment prog\n");
564 _mesa_print_program(&shProg->FragmentProgram->Base);
565 _mesa_print_program_parameters(ctx, &shProg->FragmentProgram->Base);
566 #endif
567 }
568
569 if (vertProg && shProg->VertexProgram) {
570 #if 1
571 printf("************** original vertex program\n");
572 _mesa_print_program(&vertProg->Base);
573 _mesa_print_program_parameters(ctx, &fragProg->Base);
574 #endif
575 #if 1
576 printf("************** linked vertex prog\n");
577 _mesa_print_program(&shProg->VertexProgram->Base);
578 _mesa_print_program_parameters(ctx, &shProg->VertexProgram->Base);
579 #endif
580 }
581
582 shProg->LinkStatus = (shProg->VertexProgram || shProg->FragmentProgram);
583 }
584