blob: f13aa18e9501d8076f369674f23d9e057d62ddaa [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 }
104#endif
Roland Scheidegger93da6732006-03-01 23:11:14 +0000105#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
Brian Paul21841f02004-08-14 14:28:11 +0000106 if (ctx->FragmentProgram.Current) {
107 ctx->FragmentProgram.Current->Base.RefCount--;
108 if (ctx->FragmentProgram.Current->Base.RefCount <= 0)
109 ctx->Driver.DeleteProgram(ctx, &(ctx->FragmentProgram.Current->Base));
110 }
111#endif
Brian Paul63d68302005-11-19 16:43:04 +0000112 /* XXX probably move this stuff */
Dave Airlie7f752fe2004-12-19 03:06:59 +0000113#if FEATURE_ATI_fragment_shader
114 if (ctx->ATIFragmentShader.Current) {
Brian Paul63d68302005-11-19 16:43:04 +0000115 ctx->ATIFragmentShader.Current->RefCount--;
116 if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
117 _mesa_free(ctx->ATIFragmentShader.Current);
118 }
Dave Airlie7f752fe2004-12-19 03:06:59 +0000119 }
120#endif
Brian Paul21841f02004-08-14 14:28:11 +0000121 _mesa_free((void *) ctx->Program.ErrorString);
122}
123
124
125
126
127/**
Michal Krol2861e732004-03-29 11:09:34 +0000128 * Set the vertex/fragment program error state (position and error string).
129 * This is generally called from within the parsers.
130 */
131void
132_mesa_set_program_error(GLcontext *ctx, GLint pos, const char *string)
133{
134 ctx->Program.ErrorPos = pos;
135 _mesa_free((void *) ctx->Program.ErrorString);
136 if (!string)
137 string = "";
138 ctx->Program.ErrorString = _mesa_strdup(string);
139}
140
141
142/**
143 * Find the line number and column for 'pos' within 'string'.
144 * Return a copy of the line which contains 'pos'. Free the line with
145 * _mesa_free().
146 * \param string the program string
147 * \param pos the position within the string
148 * \param line returns the line number corresponding to 'pos'.
149 * \param col returns the column number corresponding to 'pos'.
150 * \return copy of the line containing 'pos'.
151 */
152const GLubyte *
153_mesa_find_line_column(const GLubyte *string, const GLubyte *pos,
154 GLint *line, GLint *col)
155{
156 const GLubyte *lineStart = string;
157 const GLubyte *p = string;
158 GLubyte *s;
159 int len;
160
161 *line = 1;
162
163 while (p != pos) {
164 if (*p == (GLubyte) '\n') {
165 (*line)++;
166 lineStart = p + 1;
167 }
168 p++;
169 }
170
171 *col = (pos - lineStart) + 1;
172
173 /* return copy of this line */
174 while (*p != 0 && *p != '\n')
175 p++;
176 len = p - lineStart;
177 s = (GLubyte *) _mesa_malloc(len + 1);
178 _mesa_memcpy(s, lineStart, len);
179 s[len] = 0;
180
181 return s;
182}
183
184
Brian Paul765f1a12004-09-14 22:28:27 +0000185/**
186 * Initialize a new vertex/fragment program object.
187 */
Brian Paul122629f2006-07-20 16:49:57 +0000188static struct gl_program *
189_mesa_init_program_struct( GLcontext *ctx, struct gl_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000190 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000191{
Brian Paula6c423d2004-08-25 15:59:48 +0000192 (void) ctx;
Michal Krol2861e732004-03-29 11:09:34 +0000193 if (prog) {
Brian8fed2462007-10-26 19:19:09 -0600194 GLuint i;
Brianfe1d01c2006-12-13 14:54:47 -0700195 _mesa_bzero(prog, sizeof(*prog));
Michal Krol2861e732004-03-29 11:09:34 +0000196 prog->Id = id;
197 prog->Target = target;
198 prog->Resident = GL_TRUE;
199 prog->RefCount = 1;
Brian Paul308b85f2006-11-23 15:58:30 +0000200 prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
Brian8fed2462007-10-26 19:19:09 -0600201
202 /* default mapping from samplers to texture units */
203 for (i = 0; i < MAX_SAMPLERS; i++)
204 prog->SamplerUnits[i] = i;
Michal Krol2861e732004-03-29 11:09:34 +0000205 }
206
207 return prog;
208}
209
Brian Paul765f1a12004-09-14 22:28:27 +0000210
211/**
212 * Initialize a new fragment program object.
213 */
Brian Paul122629f2006-07-20 16:49:57 +0000214struct gl_program *
215_mesa_init_fragment_program( GLcontext *ctx, struct gl_fragment_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000216 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000217{
218 if (prog)
219 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
220 else
221 return NULL;
222}
223
Brian Paul765f1a12004-09-14 22:28:27 +0000224
225/**
226 * Initialize a new vertex program object.
227 */
Brian Paul122629f2006-07-20 16:49:57 +0000228struct gl_program *
229_mesa_init_vertex_program( GLcontext *ctx, struct gl_vertex_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000230 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000231{
232 if (prog)
233 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
234 else
235 return NULL;
236}
237
238
239/**
240 * Allocate and initialize a new fragment/vertex program object but
241 * don't put it into the program hash table. Called via
242 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
243 * device driver function to implement OO deriviation with additional
244 * types not understood by this function.
245 *
246 * \param ctx context
247 * \param id program id/number
248 * \param target program target/type
249 * \return pointer to new program object
250 */
Brian Paul122629f2006-07-20 16:49:57 +0000251struct gl_program *
Michal Krol2861e732004-03-29 11:09:34 +0000252_mesa_new_program(GLcontext *ctx, GLenum target, GLuint id)
253{
254 switch (target) {
255 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
Brian Paul122629f2006-07-20 16:49:57 +0000256 return _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program),
257 target, id );
Michal Krol2861e732004-03-29 11:09:34 +0000258 case GL_FRAGMENT_PROGRAM_NV:
259 case GL_FRAGMENT_PROGRAM_ARB:
Brian Paul122629f2006-07-20 16:49:57 +0000260 return _mesa_init_fragment_program(ctx,
261 CALLOC_STRUCT(gl_fragment_program),
262 target, id );
Michal Krol2861e732004-03-29 11:09:34 +0000263 default:
264 _mesa_problem(ctx, "bad target in _mesa_new_program");
265 return NULL;
266 }
267}
268
269
270/**
271 * Delete a program and remove it from the hash table, ignoring the
272 * reference count.
273 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
274 * by a device driver function.
275 */
276void
Brian Paul122629f2006-07-20 16:49:57 +0000277_mesa_delete_program(GLcontext *ctx, struct gl_program *prog)
Michal Krol2861e732004-03-29 11:09:34 +0000278{
Brian Paula6c423d2004-08-25 15:59:48 +0000279 (void) ctx;
Michal Krol2861e732004-03-29 11:09:34 +0000280 ASSERT(prog);
281
Brian Paul308b85f2006-11-23 15:58:30 +0000282 if (prog == &_mesa_DummyProgram)
283 return;
284
Michal Krol2861e732004-03-29 11:09:34 +0000285 if (prog->String)
286 _mesa_free(prog->String);
Brian Paulde997602005-11-12 17:53:14 +0000287
288 if (prog->Instructions) {
289 GLuint i;
290 for (i = 0; i < prog->NumInstructions; i++) {
291 if (prog->Instructions[i].Data)
292 _mesa_free(prog->Instructions[i].Data);
Brian4cc26742007-04-20 08:12:17 -0600293 if (prog->Instructions[i].Comment)
294 _mesa_free((char *) prog->Instructions[i].Comment);
Brian Paul575700f2004-12-16 03:07:18 +0000295 }
Brian Paulde997602005-11-12 17:53:14 +0000296 _mesa_free(prog->Instructions);
Michal Krol2861e732004-03-29 11:09:34 +0000297 }
Brian Paulde997602005-11-12 17:53:14 +0000298
Brian Paul65a51c02006-05-24 03:30:31 +0000299 if (prog->Parameters) {
Brian Paulde997602005-11-12 17:53:14 +0000300 _mesa_free_parameter_list(prog->Parameters);
Brian Paul65a51c02006-05-24 03:30:31 +0000301 }
Brianfe1d01c2006-12-13 14:54:47 -0700302 if (prog->Varying) {
303 _mesa_free_parameter_list(prog->Varying);
304 }
Brian3493e862007-03-24 16:18:13 -0600305 if (prog->Attributes) {
306 _mesa_free_parameter_list(prog->Attributes);
307 }
Brianfe1d01c2006-12-13 14:54:47 -0700308
Brian Paul58d080b2006-08-25 19:46:31 +0000309 /* XXX this is a little ugly */
310 if (prog->Target == GL_VERTEX_PROGRAM_ARB) {
311 struct gl_vertex_program *vprog = (struct gl_vertex_program *) prog;
312 if (vprog->TnlData)
313 _mesa_free(vprog->TnlData);
314 }
315
Michal Krol2861e732004-03-29 11:09:34 +0000316 _mesa_free(prog);
317}
318
319
Brian Paul4d12a052006-08-23 23:10:14 +0000320/**
321 * Return the gl_program object for a given ID.
322 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
323 * casts elsewhere.
324 */
325struct gl_program *
326_mesa_lookup_program(GLcontext *ctx, GLuint id)
327{
328 if (id)
329 return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id);
330 else
331 return NULL;
332}
333
Michal Krol2861e732004-03-29 11:09:34 +0000334
Brian Paul3b9b8de2006-08-24 21:57:36 +0000335/**
Brianb2a3a852006-12-14 13:56:58 -0700336 * Return a copy of a program.
337 * XXX Problem here if the program object is actually OO-derivation
338 * made by a device driver.
339 */
340struct gl_program *
341_mesa_clone_program(GLcontext *ctx, const struct gl_program *prog)
342{
343 struct gl_program *clone;
344
Brian4477a012007-07-24 09:57:26 -0600345 clone = ctx->Driver.NewProgram(ctx, prog->Target, prog->Id);
Brianb2a3a852006-12-14 13:56:58 -0700346 if (!clone)
347 return NULL;
348
349 assert(clone->Target == prog->Target);
350 clone->String = (GLubyte *) _mesa_strdup((char *) prog->String);
351 clone->RefCount = 1;
352 clone->Format = prog->Format;
353 clone->Instructions = _mesa_alloc_instructions(prog->NumInstructions);
354 if (!clone->Instructions) {
355 _mesa_delete_program(ctx, clone);
356 return NULL;
357 }
Brian12229f12007-03-22 09:11:26 -0600358 _mesa_copy_instructions(clone->Instructions, prog->Instructions,
359 prog->NumInstructions);
Brianb2a3a852006-12-14 13:56:58 -0700360 clone->InputsRead = prog->InputsRead;
361 clone->OutputsWritten = prog->OutputsWritten;
Brianc9db2232007-01-04 17:22:19 -0700362 memcpy(clone->TexturesUsed, prog->TexturesUsed, sizeof(prog->TexturesUsed));
363
Brian2e76f0a2006-12-19 09:52:07 -0700364 if (prog->Parameters)
365 clone->Parameters = _mesa_clone_parameter_list(prog->Parameters);
Brianb2a3a852006-12-14 13:56:58 -0700366 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
Brian2e76f0a2006-12-19 09:52:07 -0700367 if (prog->Varying)
368 clone->Varying = _mesa_clone_parameter_list(prog->Varying);
Brian3209c3e2007-01-09 17:49:24 -0700369 if (prog->Attributes)
370 clone->Attributes = _mesa_clone_parameter_list(prog->Attributes);
Brianb2a3a852006-12-14 13:56:58 -0700371 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
372 clone->NumInstructions = prog->NumInstructions;
373 clone->NumTemporaries = prog->NumTemporaries;
374 clone->NumParameters = prog->NumParameters;
375 clone->NumAttributes = prog->NumAttributes;
376 clone->NumAddressRegs = prog->NumAddressRegs;
377 clone->NumNativeInstructions = prog->NumNativeInstructions;
378 clone->NumNativeTemporaries = prog->NumNativeTemporaries;
379 clone->NumNativeParameters = prog->NumNativeParameters;
380 clone->NumNativeAttributes = prog->NumNativeAttributes;
381 clone->NumNativeAddressRegs = prog->NumNativeAddressRegs;
Brian21f99792007-01-09 11:00:21 -0700382 clone->NumAluInstructions = prog->NumAluInstructions;
383 clone->NumTexInstructions = prog->NumTexInstructions;
384 clone->NumTexIndirections = prog->NumTexIndirections;
385 clone->NumNativeAluInstructions = prog->NumNativeAluInstructions;
386 clone->NumNativeTexInstructions = prog->NumNativeTexInstructions;
387 clone->NumNativeTexIndirections = prog->NumNativeTexIndirections;
Brianb2a3a852006-12-14 13:56:58 -0700388
389 switch (prog->Target) {
390 case GL_VERTEX_PROGRAM_ARB:
391 {
392 const struct gl_vertex_program *vp
393 = (const struct gl_vertex_program *) prog;
394 struct gl_vertex_program *vpc = (struct gl_vertex_program *) clone;
395 vpc->IsPositionInvariant = vp->IsPositionInvariant;
396 }
397 break;
398 case GL_FRAGMENT_PROGRAM_ARB:
399 {
400 const struct gl_fragment_program *fp
401 = (const struct gl_fragment_program *) prog;
402 struct gl_fragment_program *fpc = (struct gl_fragment_program *) clone;
Brianb2a3a852006-12-14 13:56:58 -0700403 fpc->FogOption = fp->FogOption;
404 fpc->UsesKill = fp->UsesKill;
405 }
406 break;
407 default:
408 _mesa_problem(NULL, "Unexpected target in _mesa_clone_program");
409 }
410
411 return clone;
412}
413
414
415
416/**
Brian9ffd8892007-10-29 16:35:59 -0600417 * Search instructions for registers that match (oldFile, oldIndex),
418 * replacing them with (newFile, newIndex).
419 */
420static void
421replace_registers(struct prog_instruction *inst, GLuint numInst,
422 GLuint oldFile, GLuint oldIndex,
423 GLuint newFile, GLuint newIndex)
424{
425 GLuint i, j;
426 for (i = 0; i < numInst; i++) {
427 for (j = 0; j < _mesa_num_inst_src_regs(inst->Opcode); j++) {
428 if (inst[i].SrcReg[j].File == oldFile &&
429 inst[i].SrcReg[j].Index == oldIndex) {
430 inst[i].SrcReg[j].File = newFile;
431 inst[i].SrcReg[j].Index = newIndex;
432 }
433 }
434 }
435}
436
437
438/**
439 * Search instructions for references to program parameters. When found,
440 * increment the parameter index by 'offset'.
441 * Used when combining programs.
442 */
443static void
444adjust_param_indexes(struct prog_instruction *inst, GLuint numInst,
445 GLuint offset)
446{
447 GLuint i, j;
448 for (i = 0; i < numInst; i++) {
449 for (j = 0; j < _mesa_num_inst_src_regs(inst->Opcode); j++) {
450 GLuint f = inst[i].SrcReg[j].File;
451 if (f == PROGRAM_CONSTANT ||
452 f == PROGRAM_UNIFORM ||
453 f == PROGRAM_STATE_VAR) {
454 inst[i].SrcReg[j].Index += offset;
455 }
456 }
457 }
458}
459
460
461/**
462 * Combine two programs into one. Fix instructions so the outputs of
463 * the first program go to the inputs of the second program.
464 */
465struct gl_program *
466_mesa_combine_programs(GLcontext *ctx,
Brian1203f542007-10-29 16:38:53 -0600467 const struct gl_program *progA,
468 const struct gl_program *progB)
Brian9ffd8892007-10-29 16:35:59 -0600469{
470 struct prog_instruction *newInst;
471 struct gl_program *newProg;
472 const GLuint lenA = progA->NumInstructions - 1; /* omit END instr */
473 const GLuint lenB = progB->NumInstructions;
474 const GLuint numParamsA = _mesa_num_parameters(progA->Parameters);
475 const GLuint newLength = lenA + lenB;
Brianfb9cf482007-10-30 18:26:34 -0600476 GLbitfield inputsB;
Brian9ffd8892007-10-29 16:35:59 -0600477 GLuint i;
478
479 ASSERT(progA->Target == progB->Target);
480
481 newInst = _mesa_alloc_instructions(newLength);
482 if (!newInst)
483 return GL_FALSE;
484
485 _mesa_copy_instructions(newInst, progA->Instructions, lenA);
486 _mesa_copy_instructions(newInst + lenA, progB->Instructions, lenB);
487
488 /* adjust branch / instruction addresses for B's instructions */
489 for (i = 0; i < lenB; i++) {
490 newInst[lenA + i].BranchTarget += lenA;
491 }
492
493 newProg = ctx->Driver.NewProgram(ctx, progA->Target, 0);
494 newProg->Instructions = newInst;
495 newProg->NumInstructions = newLength;
496
497 if (newProg->Target == GL_FRAGMENT_PROGRAM_ARB) {
498 /* connect color outputs/inputs */
499 if ((progA->OutputsWritten & (1 << FRAG_RESULT_COLR)) &&
500 (progB->InputsRead & (1 << FRAG_ATTRIB_COL0))) {
501 replace_registers(newInst + lenA, lenB,
502 PROGRAM_INPUT, FRAG_ATTRIB_COL0,
503 PROGRAM_OUTPUT, FRAG_RESULT_COLR);
504 }
505
Brianfb9cf482007-10-30 18:26:34 -0600506 inputsB = progB->InputsRead;
507 if (progA->OutputsWritten & (1 << FRAG_RESULT_COLR)) {
508 inputsB &= ~(1 << FRAG_ATTRIB_COL0);
509 }
510 newProg->InputsRead = progA->InputsRead | inputsB;
Brian9ffd8892007-10-29 16:35:59 -0600511 newProg->OutputsWritten = progB->OutputsWritten;
512 }
513 else {
514 /* vertex program */
515 assert(0); /* XXX todo */
516 }
517
518 /*
519 * Merge parameters (uniforms, constants, etc)
520 */
521 newProg->Parameters = _mesa_combine_parameter_lists(progA->Parameters,
522 progB->Parameters);
523
524 adjust_param_indexes(newInst + lenA, lenB, numParamsA);
525
526
527 return newProg;
528}
529
530
531
532
533/**
534 * Scan the given program to find a free register of the given type.
535 * \param regFile - PROGRAM_INPUT, PROGRAM_OUTPUT or PROGRAM_TEMPORARY
536 */
537GLint
538_mesa_find_free_register(const struct gl_program *prog, GLuint regFile)
539{
540 GLboolean used[MAX_PROGRAM_TEMPS];
541 GLuint i, k;
542
543 assert(regFile == PROGRAM_INPUT ||
544 regFile == PROGRAM_OUTPUT ||
545 regFile == PROGRAM_TEMPORARY);
546
547 _mesa_memset(used, 0, sizeof(used));
548
549 for (i = 0; i < prog->NumInstructions; i++) {
550 const struct prog_instruction *inst = prog->Instructions + i;
551 const GLuint n = _mesa_num_inst_src_regs(inst->Opcode);
552
553 for (k = 0; k < n; k++) {
554 if (inst->SrcReg[k].File == regFile) {
555 used[inst->SrcReg[k].Index] = GL_TRUE;
556 }
557 }
558 }
559
560 for (i = 0; i < MAX_PROGRAM_TEMPS; i++) {
561 if (!used[i])
562 return i;
563 }
564
565 return -1;
566}
567
568
569
570/**
Brian Paul77427a12006-08-24 23:11:39 +0000571 * Mixing ARB and NV vertex/fragment programs can be tricky.
572 * Note: GL_VERTEX_PROGRAM_ARB == GL_VERTEX_PROGRAM_NV
573 * but, GL_FRAGMENT_PROGRAM_ARB != GL_FRAGMENT_PROGRAM_NV
574 * The two different fragment program targets are supposed to be compatible
575 * to some extent (see GL_ARB_fragment_program spec).
576 * This function does the compatibility check.
577 */
578static GLboolean
579compatible_program_targets(GLenum t1, GLenum t2)
580{
581 if (t1 == t2)
582 return GL_TRUE;
583 if (t1 == GL_FRAGMENT_PROGRAM_ARB && t2 == GL_FRAGMENT_PROGRAM_NV)
584 return GL_TRUE;
585 if (t1 == GL_FRAGMENT_PROGRAM_NV && t2 == GL_FRAGMENT_PROGRAM_ARB)
586 return GL_TRUE;
587 return GL_FALSE;
588}
589
590
Brian Paul30d6a4b2005-11-05 20:18:18 +0000591
Michal Krol2861e732004-03-29 11:09:34 +0000592/**********************************************************************/
593/* API functions */
594/**********************************************************************/
595
596
597/**
598 * Bind a program (make it current)
599 * \note Called from the GL API dispatcher by both glBindProgramNV
600 * and glBindProgramARB.
601 */
602void GLAPIENTRY
603_mesa_BindProgram(GLenum target, GLuint id)
604{
Brian Paula360bc32006-08-25 17:18:56 +0000605 struct gl_program *curProg, *newProg;
Michal Krol2861e732004-03-29 11:09:34 +0000606 GET_CURRENT_CONTEXT(ctx);
607 ASSERT_OUTSIDE_BEGIN_END(ctx);
608
609 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
610
Brian Paula360bc32006-08-25 17:18:56 +0000611 /* Error-check target and get curProg */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000612 if ((target == GL_VERTEX_PROGRAM_ARB) && /* == GL_VERTEX_PROGRAM_NV */
613 (ctx->Extensions.NV_vertex_program ||
614 ctx->Extensions.ARB_vertex_program)) {
Brian Paula360bc32006-08-25 17:18:56 +0000615 curProg = &ctx->VertexProgram.Current->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000616 }
617 else if ((target == GL_FRAGMENT_PROGRAM_NV
618 && ctx->Extensions.NV_fragment_program) ||
619 (target == GL_FRAGMENT_PROGRAM_ARB
620 && ctx->Extensions.ARB_fragment_program)) {
Brian Paula360bc32006-08-25 17:18:56 +0000621 curProg = &ctx->FragmentProgram.Current->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000622 }
623 else {
624 _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)");
625 return;
626 }
627
Brian Paula360bc32006-08-25 17:18:56 +0000628 /*
629 * Get pointer to new program to bind.
630 * NOTE: binding to a non-existant program is not an error.
Michal Krol2861e732004-03-29 11:09:34 +0000631 * That's supposed to be caught in glBegin.
632 */
633 if (id == 0) {
Brian Paula360bc32006-08-25 17:18:56 +0000634 /* Bind a default program */
635 newProg = NULL;
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000636 if (target == GL_VERTEX_PROGRAM_ARB) /* == GL_VERTEX_PROGRAM_NV */
Brian Paula360bc32006-08-25 17:18:56 +0000637 newProg = ctx->Shared->DefaultVertexProgram;
Michal Krol2861e732004-03-29 11:09:34 +0000638 else
Brian Paula360bc32006-08-25 17:18:56 +0000639 newProg = ctx->Shared->DefaultFragmentProgram;
Michal Krol2861e732004-03-29 11:09:34 +0000640 }
641 else {
Brian Paula360bc32006-08-25 17:18:56 +0000642 /* Bind a user program */
643 newProg = _mesa_lookup_program(ctx, id);
644 if (!newProg || newProg == &_mesa_DummyProgram) {
Michal Krol2861e732004-03-29 11:09:34 +0000645 /* allocate a new program now */
Brian Paula360bc32006-08-25 17:18:56 +0000646 newProg = ctx->Driver.NewProgram(ctx, target, id);
647 if (!newProg) {
Michal Krol2861e732004-03-29 11:09:34 +0000648 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB");
649 return;
650 }
Brian Paula360bc32006-08-25 17:18:56 +0000651 _mesa_HashInsert(ctx->Shared->Programs, id, newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000652 }
Brian Paula360bc32006-08-25 17:18:56 +0000653 else if (!compatible_program_targets(newProg->Target, target)) {
Brian Paul765f1a12004-09-14 22:28:27 +0000654 _mesa_error(ctx, GL_INVALID_OPERATION,
655 "glBindProgramNV/ARB(target mismatch)");
656 return;
657 }
Michal Krol2861e732004-03-29 11:09:34 +0000658 }
659
Brian Paula360bc32006-08-25 17:18:56 +0000660 /** All error checking is complete now **/
661
662 if (curProg->Id == id) {
663 /* binding same program - no change */
664 return;
665 }
666
667 /* unbind/delete oldProg */
668 if (curProg->Id != 0) {
669 /* decrement refcount on previously bound fragment program */
670 curProg->RefCount--;
671 /* and delete if refcount goes below one */
672 if (curProg->RefCount <= 0) {
673 /* the program ID was already removed from the hash table */
674 ctx->Driver.DeleteProgram(ctx, curProg);
675 }
676 }
677
678 /* bind newProg */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000679 if (target == GL_VERTEX_PROGRAM_ARB) { /* == GL_VERTEX_PROGRAM_NV */
Brian Paula360bc32006-08-25 17:18:56 +0000680 ctx->VertexProgram.Current = (struct gl_vertex_program *) newProg;
Michal Krol2861e732004-03-29 11:09:34 +0000681 }
Brian Paula360bc32006-08-25 17:18:56 +0000682 else if (target == GL_FRAGMENT_PROGRAM_NV ||
683 target == GL_FRAGMENT_PROGRAM_ARB) {
684 ctx->FragmentProgram.Current = (struct gl_fragment_program *) newProg;
Michal Krol2861e732004-03-29 11:09:34 +0000685 }
Brian Paula360bc32006-08-25 17:18:56 +0000686 newProg->RefCount++;
Michal Krol2861e732004-03-29 11:09:34 +0000687
Brian Paul765f1a12004-09-14 22:28:27 +0000688 /* Never null pointers */
689 ASSERT(ctx->VertexProgram.Current);
690 ASSERT(ctx->FragmentProgram.Current);
691
Michal Krol2861e732004-03-29 11:09:34 +0000692 if (ctx->Driver.BindProgram)
Brian Paula360bc32006-08-25 17:18:56 +0000693 ctx->Driver.BindProgram(ctx, target, newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000694}
695
696
697/**
698 * Delete a list of programs.
699 * \note Not compiled into display lists.
700 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
701 */
702void GLAPIENTRY
703_mesa_DeletePrograms(GLsizei n, const GLuint *ids)
704{
705 GLint i;
706 GET_CURRENT_CONTEXT(ctx);
707 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
708
709 if (n < 0) {
710 _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" );
711 return;
712 }
713
714 for (i = 0; i < n; i++) {
715 if (ids[i] != 0) {
Brian Paul4d12a052006-08-23 23:10:14 +0000716 struct gl_program *prog = _mesa_lookup_program(ctx, ids[i]);
Brian Paul9ca83922004-10-02 15:16:59 +0000717 if (prog == &_mesa_DummyProgram) {
Brian Paul765f1a12004-09-14 22:28:27 +0000718 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
719 }
720 else if (prog) {
721 /* Unbind program if necessary */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000722 if (prog->Target == GL_VERTEX_PROGRAM_ARB || /* == GL_VERTEX_PROGRAM_NV */
Michal Krol2861e732004-03-29 11:09:34 +0000723 prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
724 if (ctx->VertexProgram.Current &&
725 ctx->VertexProgram.Current->Base.Id == ids[i]) {
726 /* unbind this currently bound program */
727 _mesa_BindProgram(prog->Target, 0);
728 }
729 }
730 else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
731 prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
732 if (ctx->FragmentProgram.Current &&
733 ctx->FragmentProgram.Current->Base.Id == ids[i]) {
734 /* unbind this currently bound program */
735 _mesa_BindProgram(prog->Target, 0);
736 }
737 }
738 else {
739 _mesa_problem(ctx, "bad target in glDeleteProgramsNV");
740 return;
741 }
Brian Paulea2943e2005-01-20 04:02:02 +0000742 /* The ID is immediately available for re-use now */
743 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
744 prog->RefCount--;
Michal Krol2861e732004-03-29 11:09:34 +0000745 if (prog->RefCount <= 0) {
746 ctx->Driver.DeleteProgram(ctx, prog);
747 }
748 }
Michal Krol2861e732004-03-29 11:09:34 +0000749 }
750 }
751}
752
753
754/**
755 * Generate a list of new program identifiers.
756 * \note Not compiled into display lists.
757 * \note Called by both glGenProgramsNV and glGenProgramsARB.
758 */
759void GLAPIENTRY
760_mesa_GenPrograms(GLsizei n, GLuint *ids)
761{
762 GLuint first;
763 GLuint i;
764 GET_CURRENT_CONTEXT(ctx);
765 ASSERT_OUTSIDE_BEGIN_END(ctx);
766
767 if (n < 0) {
768 _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms");
769 return;
770 }
771
772 if (!ids)
773 return;
774
775 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n);
776
Brian Paul765f1a12004-09-14 22:28:27 +0000777 /* Insert pointer to dummy program as placeholder */
Michal Krol2861e732004-03-29 11:09:34 +0000778 for (i = 0; i < (GLuint) n; i++) {
Brian Paul9ca83922004-10-02 15:16:59 +0000779 _mesa_HashInsert(ctx->Shared->Programs, first + i, &_mesa_DummyProgram);
Michal Krol2861e732004-03-29 11:09:34 +0000780 }
781
782 /* Return the program names */
783 for (i = 0; i < (GLuint) n; i++) {
784 ids[i] = first + i;
785 }
786}