summaryrefslogtreecommitdiff
path: root/src/mesa/shader/slang/slang_link2.c
blob: d3d53942654e7c7e87356a0b7a6a6416cd008c4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
 * Mesa 3-D graphics library
 * Version:  6.5.3
 *
 * Copyright (C) 2006  Brian Paul   All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/**
 * \file slang_link2.c
 * GLSL linker
 * \author Brian Paul
 */

#include "imports.h"
#include "context.h"
#include "hash.h"
#include "macros.h"
#include "program.h"
#include "prog_instruction.h"
#include "prog_parameter.h"
#include "prog_print.h"
#include "shader_api.h"
#include "slang_link.h"




static GLboolean
link_varying_vars(struct gl_shader_program *shProg, struct gl_program *prog)
{
   GLuint *map, i, firstVarying, newFile;
   GLbitfield varsWritten, varsRead;

   map = (GLuint *) malloc(prog->Varying->NumParameters * sizeof(GLuint));
   if (!map)
      return GL_FALSE;

   for (i = 0; i < prog->Varying->NumParameters; i++) {
      /* see if this varying is in the linked varying list */
      const struct gl_program_parameter *var
         = prog->Varying->Parameters + i;

      GLint j = _mesa_lookup_parameter_index(shProg->Varying, -1, var->Name);
      if (j >= 0) {
         /* already in list, check size */
         if (var->Size != shProg->Varying->Parameters[j].Size) {
            /* error */
            return GL_FALSE;
         }
      }
      else {
         /* not already in linked list */
         j = _mesa_add_varying(shProg->Varying, var->Name, var->Size);
      }
      ASSERT(j >= 0);

      map[i] = j;
   }


   /* Varying variables are treated like other vertex program outputs
    * (and like other fragment program inputs).  The position of the
    * first varying differs for vertex/fragment programs...
    * Also, replace File=PROGRAM_VARYING with File=PROGRAM_INPUT/OUTPUT.
    */
   if (prog->Target == GL_VERTEX_PROGRAM_ARB) {
      firstVarying = VERT_RESULT_VAR0;
      newFile = PROGRAM_OUTPUT;
   }
   else {
      assert(prog->Target == GL_FRAGMENT_PROGRAM_ARB);
      firstVarying = FRAG_ATTRIB_VAR0;
      newFile = PROGRAM_INPUT;
   }

   /* keep track of which varying vars we read and write */
   varsWritten = varsRead = 0x0;

   /* OK, now scan the program/shader instructions looking for varying vars,
    * replacing the old index with the new index.
    */
   for (i = 0; i < prog->NumInstructions; i++) {
      struct prog_instruction *inst = prog->Instructions + i;
      GLuint j;

      if (inst->DstReg.File == PROGRAM_VARYING) {
         inst->DstReg.File = newFile;
         inst->DstReg.Index = map[ inst->DstReg.Index ] + firstVarying;
         varsWritten |= (1 << inst->DstReg.Index);
      }

      for (j = 0; j < 3; j++) {
         if (inst->SrcReg[j].File == PROGRAM_VARYING) {
            inst->SrcReg[j].File = newFile;
            inst->SrcReg[j].Index = map[ inst->SrcReg[j].Index ] + firstVarying;
            varsRead |= (1 << inst->SrcReg[j].Index);
         }
      }
      /* XXX update program OutputsWritten, InputsRead */
   }

   if (prog->Target == GL_VERTEX_PROGRAM_ARB) {
      prog->OutputsWritten |= varsWritten;
   }
   else {
      assert(prog->Target == GL_FRAGMENT_PROGRAM_ARB);
      prog->InputsRead |= varsRead;
   }


   free(map);

   return GL_TRUE;
}


static GLboolean
is_uniform(enum register_file file)
{
   return (file == PROGRAM_ENV_PARAM ||
           file == PROGRAM_STATE_VAR ||
           file == PROGRAM_NAMED_PARAM ||
           file == PROGRAM_CONSTANT ||
           file == PROGRAM_UNIFORM);
}


static GLboolean
link_uniform_vars(struct gl_shader_program *shProg, struct gl_program *prog)
{
   GLuint *map, i;

   map = (GLuint *) malloc(prog->Parameters->NumParameters * sizeof(GLuint));
   if (!map)
      return GL_FALSE;

   for (i = 0; i < prog->Parameters->NumParameters; i++) {
      /* see if this uniform is in the linked uniform list */
      const struct gl_program_parameter *p = prog->Parameters->Parameters + i;
      const GLfloat *pVals = prog->Parameters->ParameterValues[i];
      GLint j;

      /* sanity check */
      assert(is_uniform(p->Type));

      if (p->Name) {
         j = _mesa_lookup_parameter_index(shProg->Uniforms, -1, p->Name);
      }
      else {
         GLuint swizzle;
         ASSERT(p->Type == PROGRAM_CONSTANT);
         if (_mesa_lookup_parameter_constant(shProg->Uniforms, pVals,
                                             p->Size, &j, &swizzle)) {
            assert(j >= 0);
         }
         else {
            j = -1;
         }
      }

      if (j >= 0) {
         /* already in list, check size XXX check this */
         assert(p->Size == shProg->Uniforms->Parameters[j].Size);
      }
      else {
         /* not already in linked list */
         switch (p->Type) {
         case PROGRAM_ENV_PARAM:
            j = _mesa_add_named_parameter(shProg->Uniforms, p->Name, pVals);
         case PROGRAM_CONSTANT:
            j = _mesa_add_named_constant(shProg->Uniforms, p->Name, pVals, p->Size);
            break;
         case PROGRAM_STATE_VAR:
            j = _mesa_add_state_reference(shProg->Uniforms, (const GLint *) p->StateIndexes);
            break;
         case PROGRAM_UNIFORM:
            j = _mesa_add_uniform(shProg->Uniforms, p->Name, p->Size);
            break;
         default:
            abort();
         }

      }
      ASSERT(j >= 0);

      map[i] = j;
   }


   /* OK, now scan the program/shader instructions looking for varying vars,
    * replacing the old index with the new index.
    */
   for (i = 0; i < prog->NumInstructions; i++) {
      struct prog_instruction *inst = prog->Instructions + i;
      GLuint j;

      if (is_uniform(inst->DstReg.File)) {
         inst->DstReg.Index = map[ inst->DstReg.Index ];
      }

      for (j = 0; j < 3; j++) {
         if (is_uniform(inst->SrcReg[j].File)) {
            inst->SrcReg[j].Index = map[ inst->SrcReg[j].Index ];
         }
      }
      /* XXX update program OutputsWritten, InputsRead */
   }

   free(map);

   return GL_TRUE;
}


/**
 * XXX Temporary
 */
static void
slang_resolve_branches(struct gl_program *prog)
{
   struct target {
      const char *Name;
      GLuint Pos;
   };
   struct target targets[500];
   GLuint numTargets = 0;
   GLuint i, j;

   for (i = 0; i < prog->NumInstructions; i++) {
      struct prog_instruction *inst = prog->Instructions + i;
      if (inst->Opcode == OPCODE_NOP && inst->Comment) {
         targets[numTargets].Name = inst->Comment;
         targets[numTargets].Pos = i;
         numTargets++;
      }
   }

   for (i = 0; i < prog->NumInstructions; i++) {
      struct prog_instruction *inst = prog->Instructions + i;
      if (inst->Opcode == OPCODE_BRA) {
         for (j = 0; j < numTargets; j++) {
            if (!strcmp(inst->Comment, targets[j].Name)) {
               inst->BranchTarget = targets[j].Pos;
               break;
            }
         }
         if (j == numTargets) {
            abort();
         }
      }
   }
}


/** cast wrapper */
static struct gl_vertex_program *
vertex_program(struct gl_program *prog)
{
   assert(prog->Target == GL_VERTEX_PROGRAM_ARB);
   return (struct gl_vertex_program *) prog;
}


/** cast wrapper */
static struct gl_fragment_program *
fragment_program(struct gl_program *prog)
{
   assert(prog->Target == GL_FRAGMENT_PROGRAM_ARB);
   return (struct gl_fragment_program *) prog;
}


/**
 * Shader linker.  Currently:
 *
 * 1. The last attached vertex shader and fragment shader are linked.
 * 2. Varying vars in the two shaders are combined so their locations
 *    agree between the vertex and fragment stages.  They're treated as
 *    vertex program output attribs and as fragment program input attribs.
 * 3. Uniform vars (including state references, constants, etc) from the
 *    vertex and fragment shaders are merged into one group.  Recall that
 *    GLSL uniforms are shared by all linked shaders.
 * 4. The vertex and fragment programs are cloned and modified to update
 *    src/dst register references so they use the new, linked uniform/
 *    varying storage locations.
 */
void
_slang_link2(GLcontext *ctx,
             GLhandleARB programObj,
             struct gl_shader_program *shProg)
{
   struct gl_vertex_program *vertProg;
   struct gl_fragment_program *fragProg;
   GLuint i;

   _mesa_free_shader_program_data(ctx, shProg);

   shProg->Uniforms = _mesa_new_parameter_list();
   shProg->Varying = _mesa_new_parameter_list();

   /**
    * Find attached vertex shader, fragment shader
    */
   vertProg = NULL;
   fragProg = NULL;
   for (i = 0; i < shProg->NumShaders; i++) {
      if (shProg->Shaders[i]->Type == GL_VERTEX_SHADER)
         vertProg = vertex_program(shProg->Shaders[i]->Programs[0]);
      else if (shProg->Shaders[i]->Type == GL_FRAGMENT_SHADER)
         fragProg = fragment_program(shProg->Shaders[i]->Programs[0]);
      else
         _mesa_problem(ctx, "unexpected shader target in slang_link2()");
   }
   if (!vertProg || !fragProg) {
      /* XXX is it legal to have one but not the other?? */
      /* XXX record error */
      shProg->LinkStatus = GL_FALSE;
      return;
   }

   if (!vertProg->Base.Varying || !fragProg->Base.Varying) {
      /* temporary */
      _mesa_problem(ctx, "vertex/fragment program lacks varying list!");
      shProg->LinkStatus = GL_FALSE;
      return;
   }  

   /*
    * Make copies of the vertex/fragment programs now since we'll be
    * changing src/dst registers after merging the uniforms and varying vars.
    */
   shProg->VertexProgram
      = vertex_program(_mesa_clone_program(ctx, &vertProg->Base));
   shProg->FragmentProgram
      = fragment_program(_mesa_clone_program(ctx, &fragProg->Base));

   link_varying_vars(shProg, &shProg->VertexProgram->Base);
   link_varying_vars(shProg, &shProg->FragmentProgram->Base);

   link_uniform_vars(shProg, &shProg->VertexProgram->Base);
   link_uniform_vars(shProg, &shProg->FragmentProgram->Base);

   /* The vertex and fragment programs share a common set of uniforms now */
   _mesa_free_parameter_list(shProg->VertexProgram->Base.Parameters);
   _mesa_free_parameter_list(shProg->FragmentProgram->Base.Parameters);
   shProg->VertexProgram->Base.Parameters = shProg->Uniforms;
   shProg->FragmentProgram->Base.Parameters = shProg->Uniforms;

   slang_resolve_branches(&shProg->VertexProgram->Base);
   slang_resolve_branches(&shProg->FragmentProgram->Base);

#if 1
   printf("************** original fragment program\n");
   _mesa_print_program(&fragProg->Base);
   _mesa_print_program_parameters(ctx, &fragProg->Base);
#endif
#if 1
   printf("************** linked fragment prog\n");
   _mesa_print_program(&shProg->FragmentProgram->Base);
   _mesa_print_program_parameters(ctx, &shProg->FragmentProgram->Base);
#endif
#if 1
   printf("************** original vertex program\n");
   _mesa_print_program(&vertProg->Base);
   _mesa_print_program_parameters(ctx, &fragProg->Base);
#endif
#if 1
   printf("************** linked vertex prog\n");
   _mesa_print_program(&shProg->VertexProgram->Base);
   _mesa_print_program_parameters(ctx, &shProg->VertexProgram->Base);
#endif

   shProg->LinkStatus = (shProg->VertexProgram && shProg->FragmentProgram);
}