blob: fc53b5737cb8a5df172d70e641f68f0d1cae6e63 [file] [log] [blame]
Michal Krol2861e732004-03-29 11:09:34 +00001/*
2 * Mesa 3-D graphics library
Brian942ee022007-02-09 15:40:00 -07003 * Version: 6.5.3
Michal Krol2861e732004-03-29 11:09:34 +00004 *
Brian942ee022007-02-09 15:40:00 -07005 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
Michal Krol2861e732004-03-29 11:09:34 +00006 *
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 program.c
27 * Vertex and fragment program support functions.
28 * \author Brian Paul
29 */
30
31
32#include "glheader.h"
33#include "context.h"
34#include "hash.h"
Brian0560d812006-12-14 15:01:28 -070035#include "program.h"
Brianb26aae62007-10-31 12:00:38 -060036#include "prog_cache.h"
Brian0560d812006-12-14 15:01:28 -070037#include "prog_parameter.h"
38#include "prog_instruction.h"
Michal Krol2861e732004-03-29 11:09:34 +000039
40
Brian942ee022007-02-09 15:40:00 -070041/**
42 * A pointer to this dummy program is put into the hash table when
Brian Paul765f1a12004-09-14 22:28:27 +000043 * glGenPrograms is called.
44 */
Brian Paul122629f2006-07-20 16:49:57 +000045struct gl_program _mesa_DummyProgram;
Brian Paul765f1a12004-09-14 22:28:27 +000046
47
Michal Krol2861e732004-03-29 11:09:34 +000048/**
Brian Paul21841f02004-08-14 14:28:11 +000049 * Init context's vertex/fragment program state
Michal Krol2861e732004-03-29 11:09:34 +000050 */
51void
52_mesa_init_program(GLcontext *ctx)
53{
54 GLuint i;
55
56 ctx->Program.ErrorPos = -1;
57 ctx->Program.ErrorString = _mesa_strdup("");
58
59#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
60 ctx->VertexProgram.Enabled = GL_FALSE;
61 ctx->VertexProgram.PointSizeEnabled = GL_FALSE;
62 ctx->VertexProgram.TwoSideEnabled = GL_FALSE;
Brian Paul122629f2006-07-20 16:49:57 +000063 ctx->VertexProgram.Current = (struct gl_vertex_program *) ctx->Shared->DefaultVertexProgram;
Michal Krol2861e732004-03-29 11:09:34 +000064 assert(ctx->VertexProgram.Current);
65 ctx->VertexProgram.Current->Base.RefCount++;
66 for (i = 0; i < MAX_NV_VERTEX_PROGRAM_PARAMS / 4; i++) {
67 ctx->VertexProgram.TrackMatrix[i] = GL_NONE;
68 ctx->VertexProgram.TrackMatrixTransform[i] = GL_IDENTITY_NV;
69 }
Brianb26aae62007-10-31 12:00:38 -060070 ctx->VertexProgram.Cache = _mesa_new_program_cache();
Michal Krol2861e732004-03-29 11:09:34 +000071#endif
72
73#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
74 ctx->FragmentProgram.Enabled = GL_FALSE;
Brian Paul122629f2006-07-20 16:49:57 +000075 ctx->FragmentProgram.Current = (struct gl_fragment_program *) ctx->Shared->DefaultFragmentProgram;
Michal Krol2861e732004-03-29 11:09:34 +000076 assert(ctx->FragmentProgram.Current);
77 ctx->FragmentProgram.Current->Base.RefCount++;
Brianb26aae62007-10-31 12:00:38 -060078 ctx->FragmentProgram.Cache = _mesa_new_program_cache();
Michal Krol2861e732004-03-29 11:09:34 +000079#endif
Dave Airlie7f752fe2004-12-19 03:06:59 +000080
Brianb26aae62007-10-31 12:00:38 -060081
Brian Paul63d68302005-11-19 16:43:04 +000082 /* XXX probably move this stuff */
Dave Airlie7f752fe2004-12-19 03:06:59 +000083#if FEATURE_ATI_fragment_shader
84 ctx->ATIFragmentShader.Enabled = GL_FALSE;
85 ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader;
86 assert(ctx->ATIFragmentShader.Current);
Brian Paul63d68302005-11-19 16:43:04 +000087 ctx->ATIFragmentShader.Current->RefCount++;
Dave Airlie7f752fe2004-12-19 03:06:59 +000088#endif
Michal Krol2861e732004-03-29 11:09:34 +000089}
90
91
92/**
Brian Paul21841f02004-08-14 14:28:11 +000093 * Free a context's vertex/fragment program state
94 */
95void
96_mesa_free_program_data(GLcontext *ctx)
97{
Roland Scheidegger93da6732006-03-01 23:11:14 +000098#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
Brian Paul21841f02004-08-14 14:28:11 +000099 if (ctx->VertexProgram.Current) {
100 ctx->VertexProgram.Current->Base.RefCount--;
101 if (ctx->VertexProgram.Current->Base.RefCount <= 0)
102 ctx->Driver.DeleteProgram(ctx, &(ctx->VertexProgram.Current->Base));
103 }
Brian1631a952007-12-26 06:57:24 -0700104 _mesa_delete_program_cache(ctx, ctx->VertexProgram.Cache);
Brian Paul21841f02004-08-14 14:28:11 +0000105#endif
Roland Scheidegger93da6732006-03-01 23:11:14 +0000106#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
Brian Paul21841f02004-08-14 14:28:11 +0000107 if (ctx->FragmentProgram.Current) {
108 ctx->FragmentProgram.Current->Base.RefCount--;
109 if (ctx->FragmentProgram.Current->Base.RefCount <= 0)
110 ctx->Driver.DeleteProgram(ctx, &(ctx->FragmentProgram.Current->Base));
111 }
Brian1631a952007-12-26 06:57:24 -0700112 _mesa_delete_program_cache(ctx, ctx->FragmentProgram.Cache);
Brian Paul21841f02004-08-14 14:28:11 +0000113#endif
Brian Paul63d68302005-11-19 16:43:04 +0000114 /* XXX probably move this stuff */
Dave Airlie7f752fe2004-12-19 03:06:59 +0000115#if FEATURE_ATI_fragment_shader
116 if (ctx->ATIFragmentShader.Current) {
Brian Paul63d68302005-11-19 16:43:04 +0000117 ctx->ATIFragmentShader.Current->RefCount--;
118 if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
119 _mesa_free(ctx->ATIFragmentShader.Current);
120 }
Dave Airlie7f752fe2004-12-19 03:06:59 +0000121 }
122#endif
Brian Paul21841f02004-08-14 14:28:11 +0000123 _mesa_free((void *) ctx->Program.ErrorString);
124}
125
126
127
128
129/**
Michal Krol2861e732004-03-29 11:09:34 +0000130 * Set the vertex/fragment program error state (position and error string).
131 * This is generally called from within the parsers.
132 */
133void
134_mesa_set_program_error(GLcontext *ctx, GLint pos, const char *string)
135{
136 ctx->Program.ErrorPos = pos;
137 _mesa_free((void *) ctx->Program.ErrorString);
138 if (!string)
139 string = "";
140 ctx->Program.ErrorString = _mesa_strdup(string);
141}
142
143
144/**
145 * Find the line number and column for 'pos' within 'string'.
146 * Return a copy of the line which contains 'pos'. Free the line with
147 * _mesa_free().
148 * \param string the program string
149 * \param pos the position within the string
150 * \param line returns the line number corresponding to 'pos'.
151 * \param col returns the column number corresponding to 'pos'.
152 * \return copy of the line containing 'pos'.
153 */
154const GLubyte *
155_mesa_find_line_column(const GLubyte *string, const GLubyte *pos,
156 GLint *line, GLint *col)
157{
158 const GLubyte *lineStart = string;
159 const GLubyte *p = string;
160 GLubyte *s;
161 int len;
162
163 *line = 1;
164
165 while (p != pos) {
166 if (*p == (GLubyte) '\n') {
167 (*line)++;
168 lineStart = p + 1;
169 }
170 p++;
171 }
172
173 *col = (pos - lineStart) + 1;
174
175 /* return copy of this line */
176 while (*p != 0 && *p != '\n')
177 p++;
178 len = p - lineStart;
179 s = (GLubyte *) _mesa_malloc(len + 1);
180 _mesa_memcpy(s, lineStart, len);
181 s[len] = 0;
182
183 return s;
184}
185
186
Brian Paul765f1a12004-09-14 22:28:27 +0000187/**
188 * Initialize a new vertex/fragment program object.
189 */
Brian Paul122629f2006-07-20 16:49:57 +0000190static struct gl_program *
191_mesa_init_program_struct( GLcontext *ctx, struct gl_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000192 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000193{
Brian Paula6c423d2004-08-25 15:59:48 +0000194 (void) ctx;
Michal Krol2861e732004-03-29 11:09:34 +0000195 if (prog) {
Brian8fed2462007-10-26 19:19:09 -0600196 GLuint i;
Brianfe1d01c2006-12-13 14:54:47 -0700197 _mesa_bzero(prog, sizeof(*prog));
Michal Krol2861e732004-03-29 11:09:34 +0000198 prog->Id = id;
199 prog->Target = target;
200 prog->Resident = GL_TRUE;
201 prog->RefCount = 1;
Brian Paul308b85f2006-11-23 15:58:30 +0000202 prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
Brian8fed2462007-10-26 19:19:09 -0600203
204 /* default mapping from samplers to texture units */
205 for (i = 0; i < MAX_SAMPLERS; i++)
206 prog->SamplerUnits[i] = i;
Michal Krol2861e732004-03-29 11:09:34 +0000207 }
208
209 return prog;
210}
211
Brian Paul765f1a12004-09-14 22:28:27 +0000212
213/**
214 * Initialize a new fragment program object.
215 */
Brian Paul122629f2006-07-20 16:49:57 +0000216struct gl_program *
217_mesa_init_fragment_program( GLcontext *ctx, struct gl_fragment_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000218 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000219{
220 if (prog)
221 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
222 else
223 return NULL;
224}
225
Brian Paul765f1a12004-09-14 22:28:27 +0000226
227/**
228 * Initialize a new vertex program object.
229 */
Brian Paul122629f2006-07-20 16:49:57 +0000230struct gl_program *
231_mesa_init_vertex_program( GLcontext *ctx, struct gl_vertex_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000232 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000233{
234 if (prog)
235 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
236 else
237 return NULL;
238}
239
240
241/**
242 * Allocate and initialize a new fragment/vertex program object but
243 * don't put it into the program hash table. Called via
244 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
245 * device driver function to implement OO deriviation with additional
246 * types not understood by this function.
247 *
248 * \param ctx context
249 * \param id program id/number
250 * \param target program target/type
251 * \return pointer to new program object
252 */
Brian Paul122629f2006-07-20 16:49:57 +0000253struct gl_program *
Michal Krol2861e732004-03-29 11:09:34 +0000254_mesa_new_program(GLcontext *ctx, GLenum target, GLuint id)
255{
256 switch (target) {
257 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
Brian Paul122629f2006-07-20 16:49:57 +0000258 return _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program),
259 target, id );
Michal Krol2861e732004-03-29 11:09:34 +0000260 case GL_FRAGMENT_PROGRAM_NV:
261 case GL_FRAGMENT_PROGRAM_ARB:
Brian Paul122629f2006-07-20 16:49:57 +0000262 return _mesa_init_fragment_program(ctx,
263 CALLOC_STRUCT(gl_fragment_program),
264 target, id );
Michal Krol2861e732004-03-29 11:09:34 +0000265 default:
266 _mesa_problem(ctx, "bad target in _mesa_new_program");
267 return NULL;
268 }
269}
270
271
272/**
273 * Delete a program and remove it from the hash table, ignoring the
274 * reference count.
275 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
276 * by a device driver function.
277 */
278void
Brian Paul122629f2006-07-20 16:49:57 +0000279_mesa_delete_program(GLcontext *ctx, struct gl_program *prog)
Michal Krol2861e732004-03-29 11:09:34 +0000280{
Brian Paula6c423d2004-08-25 15:59:48 +0000281 (void) ctx;
Michal Krol2861e732004-03-29 11:09:34 +0000282 ASSERT(prog);
283
Brian Paul308b85f2006-11-23 15:58:30 +0000284 if (prog == &_mesa_DummyProgram)
285 return;
286
Michal Krol2861e732004-03-29 11:09:34 +0000287 if (prog->String)
288 _mesa_free(prog->String);
Brian Paulde997602005-11-12 17:53:14 +0000289
290 if (prog->Instructions) {
291 GLuint i;
292 for (i = 0; i < prog->NumInstructions; i++) {
293 if (prog->Instructions[i].Data)
294 _mesa_free(prog->Instructions[i].Data);
Brian4cc26742007-04-20 08:12:17 -0600295 if (prog->Instructions[i].Comment)
296 _mesa_free((char *) prog->Instructions[i].Comment);
Brian Paul575700f2004-12-16 03:07:18 +0000297 }
Brian Paulde997602005-11-12 17:53:14 +0000298 _mesa_free(prog->Instructions);
Michal Krol2861e732004-03-29 11:09:34 +0000299 }
Brian Paulde997602005-11-12 17:53:14 +0000300
Brian Paul65a51c02006-05-24 03:30:31 +0000301 if (prog->Parameters) {
Brian Paulde997602005-11-12 17:53:14 +0000302 _mesa_free_parameter_list(prog->Parameters);
Brian Paul65a51c02006-05-24 03:30:31 +0000303 }
Brianfe1d01c2006-12-13 14:54:47 -0700304 if (prog->Varying) {
305 _mesa_free_parameter_list(prog->Varying);
306 }
Brian3493e862007-03-24 16:18:13 -0600307 if (prog->Attributes) {
308 _mesa_free_parameter_list(prog->Attributes);
309 }
Brianfe1d01c2006-12-13 14:54:47 -0700310
Brian Paul58d080b2006-08-25 19:46:31 +0000311 /* XXX this is a little ugly */
312 if (prog->Target == GL_VERTEX_PROGRAM_ARB) {
313 struct gl_vertex_program *vprog = (struct gl_vertex_program *) prog;
314 if (vprog->TnlData)
315 _mesa_free(vprog->TnlData);
316 }
317
Michal Krol2861e732004-03-29 11:09:34 +0000318 _mesa_free(prog);
319}
320
321
Brian Paul4d12a052006-08-23 23:10:14 +0000322/**
323 * Return the gl_program object for a given ID.
324 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
325 * casts elsewhere.
326 */
327struct gl_program *
328_mesa_lookup_program(GLcontext *ctx, GLuint id)
329{
330 if (id)
331 return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id);
332 else
333 return NULL;
334}
335
Michal Krol2861e732004-03-29 11:09:34 +0000336
Brian Paul3b9b8de2006-08-24 21:57:36 +0000337/**
Brianb2a3a852006-12-14 13:56:58 -0700338 * Return a copy of a program.
339 * XXX Problem here if the program object is actually OO-derivation
340 * made by a device driver.
341 */
342struct gl_program *
343_mesa_clone_program(GLcontext *ctx, const struct gl_program *prog)
344{
345 struct gl_program *clone;
346
Brian4477a012007-07-24 09:57:26 -0600347 clone = ctx->Driver.NewProgram(ctx, prog->Target, prog->Id);
Brianb2a3a852006-12-14 13:56:58 -0700348 if (!clone)
349 return NULL;
350
351 assert(clone->Target == prog->Target);
352 clone->String = (GLubyte *) _mesa_strdup((char *) prog->String);
353 clone->RefCount = 1;
354 clone->Format = prog->Format;
355 clone->Instructions = _mesa_alloc_instructions(prog->NumInstructions);
356 if (!clone->Instructions) {
357 _mesa_delete_program(ctx, clone);
358 return NULL;
359 }
Brian12229f12007-03-22 09:11:26 -0600360 _mesa_copy_instructions(clone->Instructions, prog->Instructions,
361 prog->NumInstructions);
Brianb2a3a852006-12-14 13:56:58 -0700362 clone->InputsRead = prog->InputsRead;
363 clone->OutputsWritten = prog->OutputsWritten;
Brianc9db2232007-01-04 17:22:19 -0700364 memcpy(clone->TexturesUsed, prog->TexturesUsed, sizeof(prog->TexturesUsed));
365
Brian2e76f0a2006-12-19 09:52:07 -0700366 if (prog->Parameters)
367 clone->Parameters = _mesa_clone_parameter_list(prog->Parameters);
Brianb2a3a852006-12-14 13:56:58 -0700368 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
Brian2e76f0a2006-12-19 09:52:07 -0700369 if (prog->Varying)
370 clone->Varying = _mesa_clone_parameter_list(prog->Varying);
Brian3209c3e2007-01-09 17:49:24 -0700371 if (prog->Attributes)
372 clone->Attributes = _mesa_clone_parameter_list(prog->Attributes);
Brianb2a3a852006-12-14 13:56:58 -0700373 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
374 clone->NumInstructions = prog->NumInstructions;
375 clone->NumTemporaries = prog->NumTemporaries;
376 clone->NumParameters = prog->NumParameters;
377 clone->NumAttributes = prog->NumAttributes;
378 clone->NumAddressRegs = prog->NumAddressRegs;
379 clone->NumNativeInstructions = prog->NumNativeInstructions;
380 clone->NumNativeTemporaries = prog->NumNativeTemporaries;
381 clone->NumNativeParameters = prog->NumNativeParameters;
382 clone->NumNativeAttributes = prog->NumNativeAttributes;
383 clone->NumNativeAddressRegs = prog->NumNativeAddressRegs;
Brian21f99792007-01-09 11:00:21 -0700384 clone->NumAluInstructions = prog->NumAluInstructions;
385 clone->NumTexInstructions = prog->NumTexInstructions;
386 clone->NumTexIndirections = prog->NumTexIndirections;
387 clone->NumNativeAluInstructions = prog->NumNativeAluInstructions;
388 clone->NumNativeTexInstructions = prog->NumNativeTexInstructions;
389 clone->NumNativeTexIndirections = prog->NumNativeTexIndirections;
Brianb2a3a852006-12-14 13:56:58 -0700390
391 switch (prog->Target) {
392 case GL_VERTEX_PROGRAM_ARB:
393 {
394 const struct gl_vertex_program *vp
395 = (const struct gl_vertex_program *) prog;
396 struct gl_vertex_program *vpc = (struct gl_vertex_program *) clone;
397 vpc->IsPositionInvariant = vp->IsPositionInvariant;
398 }
399 break;
400 case GL_FRAGMENT_PROGRAM_ARB:
401 {
402 const struct gl_fragment_program *fp
403 = (const struct gl_fragment_program *) prog;
404 struct gl_fragment_program *fpc = (struct gl_fragment_program *) clone;
Brianb2a3a852006-12-14 13:56:58 -0700405 fpc->FogOption = fp->FogOption;
406 fpc->UsesKill = fp->UsesKill;
407 }
408 break;
409 default:
410 _mesa_problem(NULL, "Unexpected target in _mesa_clone_program");
411 }
412
413 return clone;
414}
415
416
417
418/**
Brian9ffd8892007-10-29 16:35:59 -0600419 * Search instructions for registers that match (oldFile, oldIndex),
420 * replacing them with (newFile, newIndex).
421 */
422static void
423replace_registers(struct prog_instruction *inst, GLuint numInst,
424 GLuint oldFile, GLuint oldIndex,
425 GLuint newFile, GLuint newIndex)
426{
427 GLuint i, j;
428 for (i = 0; i < numInst; i++) {
429 for (j = 0; j < _mesa_num_inst_src_regs(inst->Opcode); j++) {
430 if (inst[i].SrcReg[j].File == oldFile &&
431 inst[i].SrcReg[j].Index == oldIndex) {
432 inst[i].SrcReg[j].File = newFile;
433 inst[i].SrcReg[j].Index = newIndex;
434 }
435 }
436 }
437}
438
439
440/**
441 * Search instructions for references to program parameters. When found,
442 * increment the parameter index by 'offset'.
443 * Used when combining programs.
444 */
445static void
446adjust_param_indexes(struct prog_instruction *inst, GLuint numInst,
447 GLuint offset)
448{
449 GLuint i, j;
450 for (i = 0; i < numInst; i++) {
451 for (j = 0; j < _mesa_num_inst_src_regs(inst->Opcode); j++) {
452 GLuint f = inst[i].SrcReg[j].File;
453 if (f == PROGRAM_CONSTANT ||
454 f == PROGRAM_UNIFORM ||
455 f == PROGRAM_STATE_VAR) {
456 inst[i].SrcReg[j].Index += offset;
457 }
458 }
459 }
460}
461
462
463/**
464 * Combine two programs into one. Fix instructions so the outputs of
465 * the first program go to the inputs of the second program.
466 */
467struct gl_program *
468_mesa_combine_programs(GLcontext *ctx,
Brian1203f542007-10-29 16:38:53 -0600469 const struct gl_program *progA,
470 const struct gl_program *progB)
Brian9ffd8892007-10-29 16:35:59 -0600471{
472 struct prog_instruction *newInst;
473 struct gl_program *newProg;
474 const GLuint lenA = progA->NumInstructions - 1; /* omit END instr */
475 const GLuint lenB = progB->NumInstructions;
476 const GLuint numParamsA = _mesa_num_parameters(progA->Parameters);
477 const GLuint newLength = lenA + lenB;
Brianfb9cf482007-10-30 18:26:34 -0600478 GLbitfield inputsB;
Brian9ffd8892007-10-29 16:35:59 -0600479 GLuint i;
480
481 ASSERT(progA->Target == progB->Target);
482
483 newInst = _mesa_alloc_instructions(newLength);
484 if (!newInst)
485 return GL_FALSE;
486
487 _mesa_copy_instructions(newInst, progA->Instructions, lenA);
488 _mesa_copy_instructions(newInst + lenA, progB->Instructions, lenB);
489
490 /* adjust branch / instruction addresses for B's instructions */
491 for (i = 0; i < lenB; i++) {
492 newInst[lenA + i].BranchTarget += lenA;
493 }
494
495 newProg = ctx->Driver.NewProgram(ctx, progA->Target, 0);
496 newProg->Instructions = newInst;
497 newProg->NumInstructions = newLength;
498
499 if (newProg->Target == GL_FRAGMENT_PROGRAM_ARB) {
500 /* connect color outputs/inputs */
501 if ((progA->OutputsWritten & (1 << FRAG_RESULT_COLR)) &&
502 (progB->InputsRead & (1 << FRAG_ATTRIB_COL0))) {
503 replace_registers(newInst + lenA, lenB,
504 PROGRAM_INPUT, FRAG_ATTRIB_COL0,
505 PROGRAM_OUTPUT, FRAG_RESULT_COLR);
506 }
507
Brianfb9cf482007-10-30 18:26:34 -0600508 inputsB = progB->InputsRead;
509 if (progA->OutputsWritten & (1 << FRAG_RESULT_COLR)) {
510 inputsB &= ~(1 << FRAG_ATTRIB_COL0);
511 }
512 newProg->InputsRead = progA->InputsRead | inputsB;
Brian9ffd8892007-10-29 16:35:59 -0600513 newProg->OutputsWritten = progB->OutputsWritten;
514 }
515 else {
516 /* vertex program */
517 assert(0); /* XXX todo */
518 }
519
520 /*
521 * Merge parameters (uniforms, constants, etc)
522 */
523 newProg->Parameters = _mesa_combine_parameter_lists(progA->Parameters,
524 progB->Parameters);
525
526 adjust_param_indexes(newInst + lenA, lenB, numParamsA);
527
528
529 return newProg;
530}
531
532
533
534
535/**
536 * Scan the given program to find a free register of the given type.
537 * \param regFile - PROGRAM_INPUT, PROGRAM_OUTPUT or PROGRAM_TEMPORARY
538 */
539GLint
540_mesa_find_free_register(const struct gl_program *prog, GLuint regFile)
541{
542 GLboolean used[MAX_PROGRAM_TEMPS];
543 GLuint i, k;
544
545 assert(regFile == PROGRAM_INPUT ||
546 regFile == PROGRAM_OUTPUT ||
547 regFile == PROGRAM_TEMPORARY);
548
549 _mesa_memset(used, 0, sizeof(used));
550
551 for (i = 0; i < prog->NumInstructions; i++) {
552 const struct prog_instruction *inst = prog->Instructions + i;
553 const GLuint n = _mesa_num_inst_src_regs(inst->Opcode);
554
555 for (k = 0; k < n; k++) {
556 if (inst->SrcReg[k].File == regFile) {
557 used[inst->SrcReg[k].Index] = GL_TRUE;
558 }
559 }
560 }
561
562 for (i = 0; i < MAX_PROGRAM_TEMPS; i++) {
563 if (!used[i])
564 return i;
565 }
566
567 return -1;
568}
569
570
571
572/**
Brian Paul77427a12006-08-24 23:11:39 +0000573 * Mixing ARB and NV vertex/fragment programs can be tricky.
574 * Note: GL_VERTEX_PROGRAM_ARB == GL_VERTEX_PROGRAM_NV
575 * but, GL_FRAGMENT_PROGRAM_ARB != GL_FRAGMENT_PROGRAM_NV
576 * The two different fragment program targets are supposed to be compatible
577 * to some extent (see GL_ARB_fragment_program spec).
578 * This function does the compatibility check.
579 */
580static GLboolean
581compatible_program_targets(GLenum t1, GLenum t2)
582{
583 if (t1 == t2)
584 return GL_TRUE;
585 if (t1 == GL_FRAGMENT_PROGRAM_ARB && t2 == GL_FRAGMENT_PROGRAM_NV)
586 return GL_TRUE;
587 if (t1 == GL_FRAGMENT_PROGRAM_NV && t2 == GL_FRAGMENT_PROGRAM_ARB)
588 return GL_TRUE;
589 return GL_FALSE;
590}
591
592
Brian Paul30d6a4b2005-11-05 20:18:18 +0000593
Michal Krol2861e732004-03-29 11:09:34 +0000594/**********************************************************************/
595/* API functions */
596/**********************************************************************/
597
598
599/**
600 * Bind a program (make it current)
601 * \note Called from the GL API dispatcher by both glBindProgramNV
602 * and glBindProgramARB.
603 */
604void GLAPIENTRY
605_mesa_BindProgram(GLenum target, GLuint id)
606{
Brian Paula360bc32006-08-25 17:18:56 +0000607 struct gl_program *curProg, *newProg;
Michal Krol2861e732004-03-29 11:09:34 +0000608 GET_CURRENT_CONTEXT(ctx);
609 ASSERT_OUTSIDE_BEGIN_END(ctx);
610
611 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
612
Brian Paula360bc32006-08-25 17:18:56 +0000613 /* Error-check target and get curProg */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000614 if ((target == GL_VERTEX_PROGRAM_ARB) && /* == GL_VERTEX_PROGRAM_NV */
615 (ctx->Extensions.NV_vertex_program ||
616 ctx->Extensions.ARB_vertex_program)) {
Brian Paula360bc32006-08-25 17:18:56 +0000617 curProg = &ctx->VertexProgram.Current->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000618 }
619 else if ((target == GL_FRAGMENT_PROGRAM_NV
620 && ctx->Extensions.NV_fragment_program) ||
621 (target == GL_FRAGMENT_PROGRAM_ARB
622 && ctx->Extensions.ARB_fragment_program)) {
Brian Paula360bc32006-08-25 17:18:56 +0000623 curProg = &ctx->FragmentProgram.Current->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000624 }
625 else {
626 _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)");
627 return;
628 }
629
Brian Paula360bc32006-08-25 17:18:56 +0000630 /*
631 * Get pointer to new program to bind.
632 * NOTE: binding to a non-existant program is not an error.
Michal Krol2861e732004-03-29 11:09:34 +0000633 * That's supposed to be caught in glBegin.
634 */
635 if (id == 0) {
Brian Paula360bc32006-08-25 17:18:56 +0000636 /* Bind a default program */
637 newProg = NULL;
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000638 if (target == GL_VERTEX_PROGRAM_ARB) /* == GL_VERTEX_PROGRAM_NV */
Brian Paula360bc32006-08-25 17:18:56 +0000639 newProg = ctx->Shared->DefaultVertexProgram;
Michal Krol2861e732004-03-29 11:09:34 +0000640 else
Brian Paula360bc32006-08-25 17:18:56 +0000641 newProg = ctx->Shared->DefaultFragmentProgram;
Michal Krol2861e732004-03-29 11:09:34 +0000642 }
643 else {
Brian Paula360bc32006-08-25 17:18:56 +0000644 /* Bind a user program */
645 newProg = _mesa_lookup_program(ctx, id);
646 if (!newProg || newProg == &_mesa_DummyProgram) {
Michal Krol2861e732004-03-29 11:09:34 +0000647 /* allocate a new program now */
Brian Paula360bc32006-08-25 17:18:56 +0000648 newProg = ctx->Driver.NewProgram(ctx, target, id);
649 if (!newProg) {
Michal Krol2861e732004-03-29 11:09:34 +0000650 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB");
651 return;
652 }
Brian Paula360bc32006-08-25 17:18:56 +0000653 _mesa_HashInsert(ctx->Shared->Programs, id, newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000654 }
Brian Paula360bc32006-08-25 17:18:56 +0000655 else if (!compatible_program_targets(newProg->Target, target)) {
Brian Paul765f1a12004-09-14 22:28:27 +0000656 _mesa_error(ctx, GL_INVALID_OPERATION,
657 "glBindProgramNV/ARB(target mismatch)");
658 return;
659 }
Michal Krol2861e732004-03-29 11:09:34 +0000660 }
661
Brian Paula360bc32006-08-25 17:18:56 +0000662 /** All error checking is complete now **/
663
664 if (curProg->Id == id) {
665 /* binding same program - no change */
666 return;
667 }
668
669 /* unbind/delete oldProg */
670 if (curProg->Id != 0) {
671 /* decrement refcount on previously bound fragment program */
672 curProg->RefCount--;
673 /* and delete if refcount goes below one */
674 if (curProg->RefCount <= 0) {
675 /* the program ID was already removed from the hash table */
676 ctx->Driver.DeleteProgram(ctx, curProg);
677 }
678 }
679
680 /* bind newProg */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000681 if (target == GL_VERTEX_PROGRAM_ARB) { /* == GL_VERTEX_PROGRAM_NV */
Brian Paula360bc32006-08-25 17:18:56 +0000682 ctx->VertexProgram.Current = (struct gl_vertex_program *) newProg;
Michal Krol2861e732004-03-29 11:09:34 +0000683 }
Brian Paula360bc32006-08-25 17:18:56 +0000684 else if (target == GL_FRAGMENT_PROGRAM_NV ||
685 target == GL_FRAGMENT_PROGRAM_ARB) {
686 ctx->FragmentProgram.Current = (struct gl_fragment_program *) newProg;
Michal Krol2861e732004-03-29 11:09:34 +0000687 }
Brian Paula360bc32006-08-25 17:18:56 +0000688 newProg->RefCount++;
Michal Krol2861e732004-03-29 11:09:34 +0000689
Brian Paul765f1a12004-09-14 22:28:27 +0000690 /* Never null pointers */
691 ASSERT(ctx->VertexProgram.Current);
692 ASSERT(ctx->FragmentProgram.Current);
693
Michal Krol2861e732004-03-29 11:09:34 +0000694 if (ctx->Driver.BindProgram)
Brian Paula360bc32006-08-25 17:18:56 +0000695 ctx->Driver.BindProgram(ctx, target, newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000696}
697
698
699/**
700 * Delete a list of programs.
701 * \note Not compiled into display lists.
702 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
703 */
704void GLAPIENTRY
705_mesa_DeletePrograms(GLsizei n, const GLuint *ids)
706{
707 GLint i;
708 GET_CURRENT_CONTEXT(ctx);
709 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
710
711 if (n < 0) {
712 _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" );
713 return;
714 }
715
716 for (i = 0; i < n; i++) {
717 if (ids[i] != 0) {
Brian Paul4d12a052006-08-23 23:10:14 +0000718 struct gl_program *prog = _mesa_lookup_program(ctx, ids[i]);
Brian Paul9ca83922004-10-02 15:16:59 +0000719 if (prog == &_mesa_DummyProgram) {
Brian Paul765f1a12004-09-14 22:28:27 +0000720 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
721 }
722 else if (prog) {
723 /* Unbind program if necessary */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000724 if (prog->Target == GL_VERTEX_PROGRAM_ARB || /* == GL_VERTEX_PROGRAM_NV */
Michal Krol2861e732004-03-29 11:09:34 +0000725 prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
726 if (ctx->VertexProgram.Current &&
727 ctx->VertexProgram.Current->Base.Id == ids[i]) {
728 /* unbind this currently bound program */
729 _mesa_BindProgram(prog->Target, 0);
730 }
731 }
732 else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
733 prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
734 if (ctx->FragmentProgram.Current &&
735 ctx->FragmentProgram.Current->Base.Id == ids[i]) {
736 /* unbind this currently bound program */
737 _mesa_BindProgram(prog->Target, 0);
738 }
739 }
740 else {
741 _mesa_problem(ctx, "bad target in glDeleteProgramsNV");
742 return;
743 }
Brian Paulea2943e2005-01-20 04:02:02 +0000744 /* The ID is immediately available for re-use now */
745 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
746 prog->RefCount--;
Michal Krol2861e732004-03-29 11:09:34 +0000747 if (prog->RefCount <= 0) {
748 ctx->Driver.DeleteProgram(ctx, prog);
749 }
750 }
Michal Krol2861e732004-03-29 11:09:34 +0000751 }
752 }
753}
754
755
756/**
757 * Generate a list of new program identifiers.
758 * \note Not compiled into display lists.
759 * \note Called by both glGenProgramsNV and glGenProgramsARB.
760 */
761void GLAPIENTRY
762_mesa_GenPrograms(GLsizei n, GLuint *ids)
763{
764 GLuint first;
765 GLuint i;
766 GET_CURRENT_CONTEXT(ctx);
767 ASSERT_OUTSIDE_BEGIN_END(ctx);
768
769 if (n < 0) {
770 _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms");
771 return;
772 }
773
774 if (!ids)
775 return;
776
777 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n);
778
Brian Paul765f1a12004-09-14 22:28:27 +0000779 /* Insert pointer to dummy program as placeholder */
Michal Krol2861e732004-03-29 11:09:34 +0000780 for (i = 0; i < (GLuint) n; i++) {
Brian Paul9ca83922004-10-02 15:16:59 +0000781 _mesa_HashInsert(ctx->Shared->Programs, first + i, &_mesa_DummyProgram);
Michal Krol2861e732004-03-29 11:09:34 +0000782 }
783
784 /* Return the program names */
785 for (i = 0; i < (GLuint) n; i++) {
786 ids[i] = first + i;
787 }
788}