blob: 0e420179bc536fe6e4dc121b8802f541169f2994 [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"
36#include "prog_parameter.h"
37#include "prog_instruction.h"
Michal Krol2861e732004-03-29 11:09:34 +000038
39
Brian942ee022007-02-09 15:40:00 -070040/**
41 * A pointer to this dummy program is put into the hash table when
Brian Paul765f1a12004-09-14 22:28:27 +000042 * glGenPrograms is called.
43 */
Brian Paul122629f2006-07-20 16:49:57 +000044struct gl_program _mesa_DummyProgram;
Brian Paul765f1a12004-09-14 22:28:27 +000045
46
Michal Krol2861e732004-03-29 11:09:34 +000047/**
Brian Paul21841f02004-08-14 14:28:11 +000048 * Init context's vertex/fragment program state
Michal Krol2861e732004-03-29 11:09:34 +000049 */
50void
51_mesa_init_program(GLcontext *ctx)
52{
53 GLuint i;
54
55 ctx->Program.ErrorPos = -1;
56 ctx->Program.ErrorString = _mesa_strdup("");
57
58#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
59 ctx->VertexProgram.Enabled = GL_FALSE;
60 ctx->VertexProgram.PointSizeEnabled = GL_FALSE;
61 ctx->VertexProgram.TwoSideEnabled = GL_FALSE;
Brian Paul122629f2006-07-20 16:49:57 +000062 ctx->VertexProgram.Current = (struct gl_vertex_program *) ctx->Shared->DefaultVertexProgram;
Michal Krol2861e732004-03-29 11:09:34 +000063 assert(ctx->VertexProgram.Current);
64 ctx->VertexProgram.Current->Base.RefCount++;
65 for (i = 0; i < MAX_NV_VERTEX_PROGRAM_PARAMS / 4; i++) {
66 ctx->VertexProgram.TrackMatrix[i] = GL_NONE;
67 ctx->VertexProgram.TrackMatrixTransform[i] = GL_IDENTITY_NV;
68 }
69#endif
70
71#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
72 ctx->FragmentProgram.Enabled = GL_FALSE;
Brian Paul122629f2006-07-20 16:49:57 +000073 ctx->FragmentProgram.Current = (struct gl_fragment_program *) ctx->Shared->DefaultFragmentProgram;
Michal Krol2861e732004-03-29 11:09:34 +000074 assert(ctx->FragmentProgram.Current);
75 ctx->FragmentProgram.Current->Base.RefCount++;
76#endif
Dave Airlie7f752fe2004-12-19 03:06:59 +000077
Brian Paul63d68302005-11-19 16:43:04 +000078 /* XXX probably move this stuff */
Dave Airlie7f752fe2004-12-19 03:06:59 +000079#if FEATURE_ATI_fragment_shader
80 ctx->ATIFragmentShader.Enabled = GL_FALSE;
81 ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader;
82 assert(ctx->ATIFragmentShader.Current);
Brian Paul63d68302005-11-19 16:43:04 +000083 ctx->ATIFragmentShader.Current->RefCount++;
Dave Airlie7f752fe2004-12-19 03:06:59 +000084#endif
Michal Krol2861e732004-03-29 11:09:34 +000085}
86
87
88/**
Brian Paul21841f02004-08-14 14:28:11 +000089 * Free a context's vertex/fragment program state
90 */
91void
92_mesa_free_program_data(GLcontext *ctx)
93{
Roland Scheidegger93da6732006-03-01 23:11:14 +000094#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
Brian Paul21841f02004-08-14 14:28:11 +000095 if (ctx->VertexProgram.Current) {
96 ctx->VertexProgram.Current->Base.RefCount--;
97 if (ctx->VertexProgram.Current->Base.RefCount <= 0)
98 ctx->Driver.DeleteProgram(ctx, &(ctx->VertexProgram.Current->Base));
99 }
100#endif
Roland Scheidegger93da6732006-03-01 23:11:14 +0000101#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
Brian Paul21841f02004-08-14 14:28:11 +0000102 if (ctx->FragmentProgram.Current) {
103 ctx->FragmentProgram.Current->Base.RefCount--;
104 if (ctx->FragmentProgram.Current->Base.RefCount <= 0)
105 ctx->Driver.DeleteProgram(ctx, &(ctx->FragmentProgram.Current->Base));
106 }
107#endif
Brian Paul63d68302005-11-19 16:43:04 +0000108 /* XXX probably move this stuff */
Dave Airlie7f752fe2004-12-19 03:06:59 +0000109#if FEATURE_ATI_fragment_shader
110 if (ctx->ATIFragmentShader.Current) {
Brian Paul63d68302005-11-19 16:43:04 +0000111 ctx->ATIFragmentShader.Current->RefCount--;
112 if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
113 _mesa_free(ctx->ATIFragmentShader.Current);
114 }
Dave Airlie7f752fe2004-12-19 03:06:59 +0000115 }
116#endif
Brian Paul21841f02004-08-14 14:28:11 +0000117 _mesa_free((void *) ctx->Program.ErrorString);
118}
119
120
121
122
123/**
Michal Krol2861e732004-03-29 11:09:34 +0000124 * Set the vertex/fragment program error state (position and error string).
125 * This is generally called from within the parsers.
126 */
127void
128_mesa_set_program_error(GLcontext *ctx, GLint pos, const char *string)
129{
130 ctx->Program.ErrorPos = pos;
131 _mesa_free((void *) ctx->Program.ErrorString);
132 if (!string)
133 string = "";
134 ctx->Program.ErrorString = _mesa_strdup(string);
135}
136
137
138/**
139 * Find the line number and column for 'pos' within 'string'.
140 * Return a copy of the line which contains 'pos'. Free the line with
141 * _mesa_free().
142 * \param string the program string
143 * \param pos the position within the string
144 * \param line returns the line number corresponding to 'pos'.
145 * \param col returns the column number corresponding to 'pos'.
146 * \return copy of the line containing 'pos'.
147 */
148const GLubyte *
149_mesa_find_line_column(const GLubyte *string, const GLubyte *pos,
150 GLint *line, GLint *col)
151{
152 const GLubyte *lineStart = string;
153 const GLubyte *p = string;
154 GLubyte *s;
155 int len;
156
157 *line = 1;
158
159 while (p != pos) {
160 if (*p == (GLubyte) '\n') {
161 (*line)++;
162 lineStart = p + 1;
163 }
164 p++;
165 }
166
167 *col = (pos - lineStart) + 1;
168
169 /* return copy of this line */
170 while (*p != 0 && *p != '\n')
171 p++;
172 len = p - lineStart;
173 s = (GLubyte *) _mesa_malloc(len + 1);
174 _mesa_memcpy(s, lineStart, len);
175 s[len] = 0;
176
177 return s;
178}
179
180
Brian Paul765f1a12004-09-14 22:28:27 +0000181/**
182 * Initialize a new vertex/fragment program object.
183 */
Brian Paul122629f2006-07-20 16:49:57 +0000184static struct gl_program *
185_mesa_init_program_struct( GLcontext *ctx, struct gl_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000186 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000187{
Brian Paula6c423d2004-08-25 15:59:48 +0000188 (void) ctx;
Michal Krol2861e732004-03-29 11:09:34 +0000189 if (prog) {
Brian8fed2462007-10-26 19:19:09 -0600190 GLuint i;
Brianfe1d01c2006-12-13 14:54:47 -0700191 _mesa_bzero(prog, sizeof(*prog));
Michal Krol2861e732004-03-29 11:09:34 +0000192 prog->Id = id;
193 prog->Target = target;
194 prog->Resident = GL_TRUE;
195 prog->RefCount = 1;
Brian Paul308b85f2006-11-23 15:58:30 +0000196 prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
Brian8fed2462007-10-26 19:19:09 -0600197
198 /* default mapping from samplers to texture units */
199 for (i = 0; i < MAX_SAMPLERS; i++)
200 prog->SamplerUnits[i] = i;
Michal Krol2861e732004-03-29 11:09:34 +0000201 }
202
203 return prog;
204}
205
Brian Paul765f1a12004-09-14 22:28:27 +0000206
207/**
208 * Initialize a new fragment program object.
209 */
Brian Paul122629f2006-07-20 16:49:57 +0000210struct gl_program *
211_mesa_init_fragment_program( GLcontext *ctx, struct gl_fragment_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000212 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000213{
214 if (prog)
215 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
216 else
217 return NULL;
218}
219
Brian Paul765f1a12004-09-14 22:28:27 +0000220
221/**
222 * Initialize a new vertex program object.
223 */
Brian Paul122629f2006-07-20 16:49:57 +0000224struct gl_program *
225_mesa_init_vertex_program( GLcontext *ctx, struct gl_vertex_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000226 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000227{
228 if (prog)
229 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
230 else
231 return NULL;
232}
233
234
235/**
236 * Allocate and initialize a new fragment/vertex program object but
237 * don't put it into the program hash table. Called via
238 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
239 * device driver function to implement OO deriviation with additional
240 * types not understood by this function.
241 *
242 * \param ctx context
243 * \param id program id/number
244 * \param target program target/type
245 * \return pointer to new program object
246 */
Brian Paul122629f2006-07-20 16:49:57 +0000247struct gl_program *
Michal Krol2861e732004-03-29 11:09:34 +0000248_mesa_new_program(GLcontext *ctx, GLenum target, GLuint id)
249{
250 switch (target) {
251 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
Brian Paul122629f2006-07-20 16:49:57 +0000252 return _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program),
253 target, id );
Michal Krol2861e732004-03-29 11:09:34 +0000254 case GL_FRAGMENT_PROGRAM_NV:
255 case GL_FRAGMENT_PROGRAM_ARB:
Brian Paul122629f2006-07-20 16:49:57 +0000256 return _mesa_init_fragment_program(ctx,
257 CALLOC_STRUCT(gl_fragment_program),
258 target, id );
Michal Krol2861e732004-03-29 11:09:34 +0000259 default:
260 _mesa_problem(ctx, "bad target in _mesa_new_program");
261 return NULL;
262 }
263}
264
265
266/**
267 * Delete a program and remove it from the hash table, ignoring the
268 * reference count.
269 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
270 * by a device driver function.
271 */
272void
Brian Paul122629f2006-07-20 16:49:57 +0000273_mesa_delete_program(GLcontext *ctx, struct gl_program *prog)
Michal Krol2861e732004-03-29 11:09:34 +0000274{
Brian Paula6c423d2004-08-25 15:59:48 +0000275 (void) ctx;
Michal Krol2861e732004-03-29 11:09:34 +0000276 ASSERT(prog);
277
Brian Paul308b85f2006-11-23 15:58:30 +0000278 if (prog == &_mesa_DummyProgram)
279 return;
280
Michal Krol2861e732004-03-29 11:09:34 +0000281 if (prog->String)
282 _mesa_free(prog->String);
Brian Paulde997602005-11-12 17:53:14 +0000283
284 if (prog->Instructions) {
285 GLuint i;
286 for (i = 0; i < prog->NumInstructions; i++) {
287 if (prog->Instructions[i].Data)
288 _mesa_free(prog->Instructions[i].Data);
Brian4cc26742007-04-20 08:12:17 -0600289 if (prog->Instructions[i].Comment)
290 _mesa_free((char *) prog->Instructions[i].Comment);
Brian Paul575700f2004-12-16 03:07:18 +0000291 }
Brian Paulde997602005-11-12 17:53:14 +0000292 _mesa_free(prog->Instructions);
Michal Krol2861e732004-03-29 11:09:34 +0000293 }
Brian Paulde997602005-11-12 17:53:14 +0000294
Brian Paul65a51c02006-05-24 03:30:31 +0000295 if (prog->Parameters) {
Brian Paulde997602005-11-12 17:53:14 +0000296 _mesa_free_parameter_list(prog->Parameters);
Brian Paul65a51c02006-05-24 03:30:31 +0000297 }
Brianfe1d01c2006-12-13 14:54:47 -0700298 if (prog->Varying) {
299 _mesa_free_parameter_list(prog->Varying);
300 }
Brian3493e862007-03-24 16:18:13 -0600301 if (prog->Attributes) {
302 _mesa_free_parameter_list(prog->Attributes);
303 }
Brianfe1d01c2006-12-13 14:54:47 -0700304
Brian Paul58d080b2006-08-25 19:46:31 +0000305 /* XXX this is a little ugly */
306 if (prog->Target == GL_VERTEX_PROGRAM_ARB) {
307 struct gl_vertex_program *vprog = (struct gl_vertex_program *) prog;
308 if (vprog->TnlData)
309 _mesa_free(vprog->TnlData);
310 }
311
Michal Krol2861e732004-03-29 11:09:34 +0000312 _mesa_free(prog);
313}
314
315
Brian Paul4d12a052006-08-23 23:10:14 +0000316/**
317 * Return the gl_program object for a given ID.
318 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
319 * casts elsewhere.
320 */
321struct gl_program *
322_mesa_lookup_program(GLcontext *ctx, GLuint id)
323{
324 if (id)
325 return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id);
326 else
327 return NULL;
328}
329
Michal Krol2861e732004-03-29 11:09:34 +0000330
Brian Paul3b9b8de2006-08-24 21:57:36 +0000331/**
Brianb2a3a852006-12-14 13:56:58 -0700332 * Return a copy of a program.
333 * XXX Problem here if the program object is actually OO-derivation
334 * made by a device driver.
335 */
336struct gl_program *
337_mesa_clone_program(GLcontext *ctx, const struct gl_program *prog)
338{
339 struct gl_program *clone;
340
Brian4477a012007-07-24 09:57:26 -0600341 clone = ctx->Driver.NewProgram(ctx, prog->Target, prog->Id);
Brianb2a3a852006-12-14 13:56:58 -0700342 if (!clone)
343 return NULL;
344
345 assert(clone->Target == prog->Target);
346 clone->String = (GLubyte *) _mesa_strdup((char *) prog->String);
347 clone->RefCount = 1;
348 clone->Format = prog->Format;
349 clone->Instructions = _mesa_alloc_instructions(prog->NumInstructions);
350 if (!clone->Instructions) {
351 _mesa_delete_program(ctx, clone);
352 return NULL;
353 }
Brian12229f12007-03-22 09:11:26 -0600354 _mesa_copy_instructions(clone->Instructions, prog->Instructions,
355 prog->NumInstructions);
Brianb2a3a852006-12-14 13:56:58 -0700356 clone->InputsRead = prog->InputsRead;
357 clone->OutputsWritten = prog->OutputsWritten;
Brianc9db2232007-01-04 17:22:19 -0700358 memcpy(clone->TexturesUsed, prog->TexturesUsed, sizeof(prog->TexturesUsed));
359
Brian2e76f0a2006-12-19 09:52:07 -0700360 if (prog->Parameters)
361 clone->Parameters = _mesa_clone_parameter_list(prog->Parameters);
Brianb2a3a852006-12-14 13:56:58 -0700362 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
Brian2e76f0a2006-12-19 09:52:07 -0700363 if (prog->Varying)
364 clone->Varying = _mesa_clone_parameter_list(prog->Varying);
Brian3209c3e2007-01-09 17:49:24 -0700365 if (prog->Attributes)
366 clone->Attributes = _mesa_clone_parameter_list(prog->Attributes);
Brianb2a3a852006-12-14 13:56:58 -0700367 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
368 clone->NumInstructions = prog->NumInstructions;
369 clone->NumTemporaries = prog->NumTemporaries;
370 clone->NumParameters = prog->NumParameters;
371 clone->NumAttributes = prog->NumAttributes;
372 clone->NumAddressRegs = prog->NumAddressRegs;
373 clone->NumNativeInstructions = prog->NumNativeInstructions;
374 clone->NumNativeTemporaries = prog->NumNativeTemporaries;
375 clone->NumNativeParameters = prog->NumNativeParameters;
376 clone->NumNativeAttributes = prog->NumNativeAttributes;
377 clone->NumNativeAddressRegs = prog->NumNativeAddressRegs;
Brian21f99792007-01-09 11:00:21 -0700378 clone->NumAluInstructions = prog->NumAluInstructions;
379 clone->NumTexInstructions = prog->NumTexInstructions;
380 clone->NumTexIndirections = prog->NumTexIndirections;
381 clone->NumNativeAluInstructions = prog->NumNativeAluInstructions;
382 clone->NumNativeTexInstructions = prog->NumNativeTexInstructions;
383 clone->NumNativeTexIndirections = prog->NumNativeTexIndirections;
Brianb2a3a852006-12-14 13:56:58 -0700384
385 switch (prog->Target) {
386 case GL_VERTEX_PROGRAM_ARB:
387 {
388 const struct gl_vertex_program *vp
389 = (const struct gl_vertex_program *) prog;
390 struct gl_vertex_program *vpc = (struct gl_vertex_program *) clone;
391 vpc->IsPositionInvariant = vp->IsPositionInvariant;
392 }
393 break;
394 case GL_FRAGMENT_PROGRAM_ARB:
395 {
396 const struct gl_fragment_program *fp
397 = (const struct gl_fragment_program *) prog;
398 struct gl_fragment_program *fpc = (struct gl_fragment_program *) clone;
Brianb2a3a852006-12-14 13:56:58 -0700399 fpc->FogOption = fp->FogOption;
400 fpc->UsesKill = fp->UsesKill;
401 }
402 break;
403 default:
404 _mesa_problem(NULL, "Unexpected target in _mesa_clone_program");
405 }
406
407 return clone;
408}
409
410
411
412/**
Brian9ffd8892007-10-29 16:35:59 -0600413 * Search instructions for registers that match (oldFile, oldIndex),
414 * replacing them with (newFile, newIndex).
415 */
416static void
417replace_registers(struct prog_instruction *inst, GLuint numInst,
418 GLuint oldFile, GLuint oldIndex,
419 GLuint newFile, GLuint newIndex)
420{
421 GLuint i, j;
422 for (i = 0; i < numInst; i++) {
423 for (j = 0; j < _mesa_num_inst_src_regs(inst->Opcode); j++) {
424 if (inst[i].SrcReg[j].File == oldFile &&
425 inst[i].SrcReg[j].Index == oldIndex) {
426 inst[i].SrcReg[j].File = newFile;
427 inst[i].SrcReg[j].Index = newIndex;
428 }
429 }
430 }
431}
432
433
434/**
435 * Search instructions for references to program parameters. When found,
436 * increment the parameter index by 'offset'.
437 * Used when combining programs.
438 */
439static void
440adjust_param_indexes(struct prog_instruction *inst, GLuint numInst,
441 GLuint offset)
442{
443 GLuint i, j;
444 for (i = 0; i < numInst; i++) {
445 for (j = 0; j < _mesa_num_inst_src_regs(inst->Opcode); j++) {
446 GLuint f = inst[i].SrcReg[j].File;
447 if (f == PROGRAM_CONSTANT ||
448 f == PROGRAM_UNIFORM ||
449 f == PROGRAM_STATE_VAR) {
450 inst[i].SrcReg[j].Index += offset;
451 }
452 }
453 }
454}
455
456
457/**
458 * Combine two programs into one. Fix instructions so the outputs of
459 * the first program go to the inputs of the second program.
460 */
461struct gl_program *
462_mesa_combine_programs(GLcontext *ctx,
463 struct gl_program *progA, struct gl_program *progB)
464{
465 struct prog_instruction *newInst;
466 struct gl_program *newProg;
467 const GLuint lenA = progA->NumInstructions - 1; /* omit END instr */
468 const GLuint lenB = progB->NumInstructions;
469 const GLuint numParamsA = _mesa_num_parameters(progA->Parameters);
470 const GLuint newLength = lenA + lenB;
471 GLuint i;
472
473 ASSERT(progA->Target == progB->Target);
474
475 newInst = _mesa_alloc_instructions(newLength);
476 if (!newInst)
477 return GL_FALSE;
478
479 _mesa_copy_instructions(newInst, progA->Instructions, lenA);
480 _mesa_copy_instructions(newInst + lenA, progB->Instructions, lenB);
481
482 /* adjust branch / instruction addresses for B's instructions */
483 for (i = 0; i < lenB; i++) {
484 newInst[lenA + i].BranchTarget += lenA;
485 }
486
487 newProg = ctx->Driver.NewProgram(ctx, progA->Target, 0);
488 newProg->Instructions = newInst;
489 newProg->NumInstructions = newLength;
490
491 if (newProg->Target == GL_FRAGMENT_PROGRAM_ARB) {
492 /* connect color outputs/inputs */
493 if ((progA->OutputsWritten & (1 << FRAG_RESULT_COLR)) &&
494 (progB->InputsRead & (1 << FRAG_ATTRIB_COL0))) {
495 replace_registers(newInst + lenA, lenB,
496 PROGRAM_INPUT, FRAG_ATTRIB_COL0,
497 PROGRAM_OUTPUT, FRAG_RESULT_COLR);
498 }
499
500 newProg->InputsRead = progA->InputsRead;
501 newProg->InputsRead |= (progB->InputsRead & ~(1 << FRAG_ATTRIB_COL0));
502 newProg->OutputsWritten = progB->OutputsWritten;
503 }
504 else {
505 /* vertex program */
506 assert(0); /* XXX todo */
507 }
508
509 /*
510 * Merge parameters (uniforms, constants, etc)
511 */
512 newProg->Parameters = _mesa_combine_parameter_lists(progA->Parameters,
513 progB->Parameters);
514
515 adjust_param_indexes(newInst + lenA, lenB, numParamsA);
516
517
518 return newProg;
519}
520
521
522
523
524/**
525 * Scan the given program to find a free register of the given type.
526 * \param regFile - PROGRAM_INPUT, PROGRAM_OUTPUT or PROGRAM_TEMPORARY
527 */
528GLint
529_mesa_find_free_register(const struct gl_program *prog, GLuint regFile)
530{
531 GLboolean used[MAX_PROGRAM_TEMPS];
532 GLuint i, k;
533
534 assert(regFile == PROGRAM_INPUT ||
535 regFile == PROGRAM_OUTPUT ||
536 regFile == PROGRAM_TEMPORARY);
537
538 _mesa_memset(used, 0, sizeof(used));
539
540 for (i = 0; i < prog->NumInstructions; i++) {
541 const struct prog_instruction *inst = prog->Instructions + i;
542 const GLuint n = _mesa_num_inst_src_regs(inst->Opcode);
543
544 for (k = 0; k < n; k++) {
545 if (inst->SrcReg[k].File == regFile) {
546 used[inst->SrcReg[k].Index] = GL_TRUE;
547 }
548 }
549 }
550
551 for (i = 0; i < MAX_PROGRAM_TEMPS; i++) {
552 if (!used[i])
553 return i;
554 }
555
556 return -1;
557}
558
559
560
561/**
Brian Paul77427a12006-08-24 23:11:39 +0000562 * Mixing ARB and NV vertex/fragment programs can be tricky.
563 * Note: GL_VERTEX_PROGRAM_ARB == GL_VERTEX_PROGRAM_NV
564 * but, GL_FRAGMENT_PROGRAM_ARB != GL_FRAGMENT_PROGRAM_NV
565 * The two different fragment program targets are supposed to be compatible
566 * to some extent (see GL_ARB_fragment_program spec).
567 * This function does the compatibility check.
568 */
569static GLboolean
570compatible_program_targets(GLenum t1, GLenum t2)
571{
572 if (t1 == t2)
573 return GL_TRUE;
574 if (t1 == GL_FRAGMENT_PROGRAM_ARB && t2 == GL_FRAGMENT_PROGRAM_NV)
575 return GL_TRUE;
576 if (t1 == GL_FRAGMENT_PROGRAM_NV && t2 == GL_FRAGMENT_PROGRAM_ARB)
577 return GL_TRUE;
578 return GL_FALSE;
579}
580
581
Brian Paul30d6a4b2005-11-05 20:18:18 +0000582
Michal Krol2861e732004-03-29 11:09:34 +0000583/**********************************************************************/
584/* API functions */
585/**********************************************************************/
586
587
588/**
589 * Bind a program (make it current)
590 * \note Called from the GL API dispatcher by both glBindProgramNV
591 * and glBindProgramARB.
592 */
593void GLAPIENTRY
594_mesa_BindProgram(GLenum target, GLuint id)
595{
Brian Paula360bc32006-08-25 17:18:56 +0000596 struct gl_program *curProg, *newProg;
Michal Krol2861e732004-03-29 11:09:34 +0000597 GET_CURRENT_CONTEXT(ctx);
598 ASSERT_OUTSIDE_BEGIN_END(ctx);
599
600 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
601
Brian Paula360bc32006-08-25 17:18:56 +0000602 /* Error-check target and get curProg */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000603 if ((target == GL_VERTEX_PROGRAM_ARB) && /* == GL_VERTEX_PROGRAM_NV */
604 (ctx->Extensions.NV_vertex_program ||
605 ctx->Extensions.ARB_vertex_program)) {
Brian Paula360bc32006-08-25 17:18:56 +0000606 curProg = &ctx->VertexProgram.Current->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000607 }
608 else if ((target == GL_FRAGMENT_PROGRAM_NV
609 && ctx->Extensions.NV_fragment_program) ||
610 (target == GL_FRAGMENT_PROGRAM_ARB
611 && ctx->Extensions.ARB_fragment_program)) {
Brian Paula360bc32006-08-25 17:18:56 +0000612 curProg = &ctx->FragmentProgram.Current->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000613 }
614 else {
615 _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)");
616 return;
617 }
618
Brian Paula360bc32006-08-25 17:18:56 +0000619 /*
620 * Get pointer to new program to bind.
621 * NOTE: binding to a non-existant program is not an error.
Michal Krol2861e732004-03-29 11:09:34 +0000622 * That's supposed to be caught in glBegin.
623 */
624 if (id == 0) {
Brian Paula360bc32006-08-25 17:18:56 +0000625 /* Bind a default program */
626 newProg = NULL;
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000627 if (target == GL_VERTEX_PROGRAM_ARB) /* == GL_VERTEX_PROGRAM_NV */
Brian Paula360bc32006-08-25 17:18:56 +0000628 newProg = ctx->Shared->DefaultVertexProgram;
Michal Krol2861e732004-03-29 11:09:34 +0000629 else
Brian Paula360bc32006-08-25 17:18:56 +0000630 newProg = ctx->Shared->DefaultFragmentProgram;
Michal Krol2861e732004-03-29 11:09:34 +0000631 }
632 else {
Brian Paula360bc32006-08-25 17:18:56 +0000633 /* Bind a user program */
634 newProg = _mesa_lookup_program(ctx, id);
635 if (!newProg || newProg == &_mesa_DummyProgram) {
Michal Krol2861e732004-03-29 11:09:34 +0000636 /* allocate a new program now */
Brian Paula360bc32006-08-25 17:18:56 +0000637 newProg = ctx->Driver.NewProgram(ctx, target, id);
638 if (!newProg) {
Michal Krol2861e732004-03-29 11:09:34 +0000639 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB");
640 return;
641 }
Brian Paula360bc32006-08-25 17:18:56 +0000642 _mesa_HashInsert(ctx->Shared->Programs, id, newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000643 }
Brian Paula360bc32006-08-25 17:18:56 +0000644 else if (!compatible_program_targets(newProg->Target, target)) {
Brian Paul765f1a12004-09-14 22:28:27 +0000645 _mesa_error(ctx, GL_INVALID_OPERATION,
646 "glBindProgramNV/ARB(target mismatch)");
647 return;
648 }
Michal Krol2861e732004-03-29 11:09:34 +0000649 }
650
Brian Paula360bc32006-08-25 17:18:56 +0000651 /** All error checking is complete now **/
652
653 if (curProg->Id == id) {
654 /* binding same program - no change */
655 return;
656 }
657
658 /* unbind/delete oldProg */
659 if (curProg->Id != 0) {
660 /* decrement refcount on previously bound fragment program */
661 curProg->RefCount--;
662 /* and delete if refcount goes below one */
663 if (curProg->RefCount <= 0) {
664 /* the program ID was already removed from the hash table */
665 ctx->Driver.DeleteProgram(ctx, curProg);
666 }
667 }
668
669 /* bind newProg */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000670 if (target == GL_VERTEX_PROGRAM_ARB) { /* == GL_VERTEX_PROGRAM_NV */
Brian Paula360bc32006-08-25 17:18:56 +0000671 ctx->VertexProgram.Current = (struct gl_vertex_program *) newProg;
Michal Krol2861e732004-03-29 11:09:34 +0000672 }
Brian Paula360bc32006-08-25 17:18:56 +0000673 else if (target == GL_FRAGMENT_PROGRAM_NV ||
674 target == GL_FRAGMENT_PROGRAM_ARB) {
675 ctx->FragmentProgram.Current = (struct gl_fragment_program *) newProg;
Michal Krol2861e732004-03-29 11:09:34 +0000676 }
Brian Paula360bc32006-08-25 17:18:56 +0000677 newProg->RefCount++;
Michal Krol2861e732004-03-29 11:09:34 +0000678
Brian Paul765f1a12004-09-14 22:28:27 +0000679 /* Never null pointers */
680 ASSERT(ctx->VertexProgram.Current);
681 ASSERT(ctx->FragmentProgram.Current);
682
Michal Krol2861e732004-03-29 11:09:34 +0000683 if (ctx->Driver.BindProgram)
Brian Paula360bc32006-08-25 17:18:56 +0000684 ctx->Driver.BindProgram(ctx, target, newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000685}
686
687
688/**
689 * Delete a list of programs.
690 * \note Not compiled into display lists.
691 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
692 */
693void GLAPIENTRY
694_mesa_DeletePrograms(GLsizei n, const GLuint *ids)
695{
696 GLint i;
697 GET_CURRENT_CONTEXT(ctx);
698 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
699
700 if (n < 0) {
701 _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" );
702 return;
703 }
704
705 for (i = 0; i < n; i++) {
706 if (ids[i] != 0) {
Brian Paul4d12a052006-08-23 23:10:14 +0000707 struct gl_program *prog = _mesa_lookup_program(ctx, ids[i]);
Brian Paul9ca83922004-10-02 15:16:59 +0000708 if (prog == &_mesa_DummyProgram) {
Brian Paul765f1a12004-09-14 22:28:27 +0000709 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
710 }
711 else if (prog) {
712 /* Unbind program if necessary */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000713 if (prog->Target == GL_VERTEX_PROGRAM_ARB || /* == GL_VERTEX_PROGRAM_NV */
Michal Krol2861e732004-03-29 11:09:34 +0000714 prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
715 if (ctx->VertexProgram.Current &&
716 ctx->VertexProgram.Current->Base.Id == ids[i]) {
717 /* unbind this currently bound program */
718 _mesa_BindProgram(prog->Target, 0);
719 }
720 }
721 else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
722 prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
723 if (ctx->FragmentProgram.Current &&
724 ctx->FragmentProgram.Current->Base.Id == ids[i]) {
725 /* unbind this currently bound program */
726 _mesa_BindProgram(prog->Target, 0);
727 }
728 }
729 else {
730 _mesa_problem(ctx, "bad target in glDeleteProgramsNV");
731 return;
732 }
Brian Paulea2943e2005-01-20 04:02:02 +0000733 /* The ID is immediately available for re-use now */
734 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
735 prog->RefCount--;
Michal Krol2861e732004-03-29 11:09:34 +0000736 if (prog->RefCount <= 0) {
737 ctx->Driver.DeleteProgram(ctx, prog);
738 }
739 }
Michal Krol2861e732004-03-29 11:09:34 +0000740 }
741 }
742}
743
744
745/**
746 * Generate a list of new program identifiers.
747 * \note Not compiled into display lists.
748 * \note Called by both glGenProgramsNV and glGenProgramsARB.
749 */
750void GLAPIENTRY
751_mesa_GenPrograms(GLsizei n, GLuint *ids)
752{
753 GLuint first;
754 GLuint i;
755 GET_CURRENT_CONTEXT(ctx);
756 ASSERT_OUTSIDE_BEGIN_END(ctx);
757
758 if (n < 0) {
759 _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms");
760 return;
761 }
762
763 if (!ids)
764 return;
765
766 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n);
767
Brian Paul765f1a12004-09-14 22:28:27 +0000768 /* Insert pointer to dummy program as placeholder */
Michal Krol2861e732004-03-29 11:09:34 +0000769 for (i = 0; i < (GLuint) n; i++) {
Brian Paul9ca83922004-10-02 15:16:59 +0000770 _mesa_HashInsert(ctx->Shared->Programs, first + i, &_mesa_DummyProgram);
Michal Krol2861e732004-03-29 11:09:34 +0000771 }
772
773 /* Return the program names */
774 for (i = 0; i < (GLuint) n; i++) {
775 ids[i] = first + i;
776 }
777}