blob: 3069b04836d46e385734913e0707de20c45928ef [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) {
Brianf8acc392008-03-22 10:27:55 -0600357 ctx->Driver.DeleteProgram(ctx, clone);
Brianb2a3a852006-12-14 13:56:58 -0700358 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;
Brian Paul72f2c552008-04-04 11:20:44 -0600364 clone->SamplersUsed = prog->SamplersUsed;
Brianc9db2232007-01-04 17:22:19 -0700365 memcpy(clone->TexturesUsed, prog->TexturesUsed, sizeof(prog->TexturesUsed));
366
Brian2e76f0a2006-12-19 09:52:07 -0700367 if (prog->Parameters)
368 clone->Parameters = _mesa_clone_parameter_list(prog->Parameters);
Brianb2a3a852006-12-14 13:56:58 -0700369 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
Brian2e76f0a2006-12-19 09:52:07 -0700370 if (prog->Varying)
371 clone->Varying = _mesa_clone_parameter_list(prog->Varying);
Brian3209c3e2007-01-09 17:49:24 -0700372 if (prog->Attributes)
373 clone->Attributes = _mesa_clone_parameter_list(prog->Attributes);
Brianb2a3a852006-12-14 13:56:58 -0700374 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
375 clone->NumInstructions = prog->NumInstructions;
376 clone->NumTemporaries = prog->NumTemporaries;
377 clone->NumParameters = prog->NumParameters;
378 clone->NumAttributes = prog->NumAttributes;
379 clone->NumAddressRegs = prog->NumAddressRegs;
380 clone->NumNativeInstructions = prog->NumNativeInstructions;
381 clone->NumNativeTemporaries = prog->NumNativeTemporaries;
382 clone->NumNativeParameters = prog->NumNativeParameters;
383 clone->NumNativeAttributes = prog->NumNativeAttributes;
384 clone->NumNativeAddressRegs = prog->NumNativeAddressRegs;
Brian21f99792007-01-09 11:00:21 -0700385 clone->NumAluInstructions = prog->NumAluInstructions;
386 clone->NumTexInstructions = prog->NumTexInstructions;
387 clone->NumTexIndirections = prog->NumTexIndirections;
388 clone->NumNativeAluInstructions = prog->NumNativeAluInstructions;
389 clone->NumNativeTexInstructions = prog->NumNativeTexInstructions;
390 clone->NumNativeTexIndirections = prog->NumNativeTexIndirections;
Brianb2a3a852006-12-14 13:56:58 -0700391
392 switch (prog->Target) {
393 case GL_VERTEX_PROGRAM_ARB:
394 {
395 const struct gl_vertex_program *vp
396 = (const struct gl_vertex_program *) prog;
397 struct gl_vertex_program *vpc = (struct gl_vertex_program *) clone;
398 vpc->IsPositionInvariant = vp->IsPositionInvariant;
399 }
400 break;
401 case GL_FRAGMENT_PROGRAM_ARB:
402 {
403 const struct gl_fragment_program *fp
404 = (const struct gl_fragment_program *) prog;
405 struct gl_fragment_program *fpc = (struct gl_fragment_program *) clone;
Brianb2a3a852006-12-14 13:56:58 -0700406 fpc->FogOption = fp->FogOption;
407 fpc->UsesKill = fp->UsesKill;
408 }
409 break;
410 default:
411 _mesa_problem(NULL, "Unexpected target in _mesa_clone_program");
412 }
413
414 return clone;
415}
416
417
418
419/**
Brian9ffd8892007-10-29 16:35:59 -0600420 * Search instructions for registers that match (oldFile, oldIndex),
421 * replacing them with (newFile, newIndex).
422 */
423static void
424replace_registers(struct prog_instruction *inst, GLuint numInst,
425 GLuint oldFile, GLuint oldIndex,
426 GLuint newFile, GLuint newIndex)
427{
428 GLuint i, j;
429 for (i = 0; i < numInst; i++) {
Brian Paulcf7daba2008-03-25 12:27:32 -0600430 /* src regs */
Brian9ffd8892007-10-29 16:35:59 -0600431 for (j = 0; j < _mesa_num_inst_src_regs(inst->Opcode); j++) {
432 if (inst[i].SrcReg[j].File == oldFile &&
433 inst[i].SrcReg[j].Index == oldIndex) {
434 inst[i].SrcReg[j].File = newFile;
435 inst[i].SrcReg[j].Index = newIndex;
436 }
437 }
Brian Paulcf7daba2008-03-25 12:27:32 -0600438 /* dst reg */
439 if (inst[i].DstReg.File == oldFile && inst[i].DstReg.Index == oldIndex) {
440 inst[i].DstReg.File = newFile;
441 inst[i].DstReg.Index = newIndex;
442 }
Brian9ffd8892007-10-29 16:35:59 -0600443 }
444}
445
446
447/**
448 * Search instructions for references to program parameters. When found,
449 * increment the parameter index by 'offset'.
450 * Used when combining programs.
451 */
452static void
453adjust_param_indexes(struct prog_instruction *inst, GLuint numInst,
454 GLuint offset)
455{
456 GLuint i, j;
457 for (i = 0; i < numInst; i++) {
458 for (j = 0; j < _mesa_num_inst_src_regs(inst->Opcode); j++) {
459 GLuint f = inst[i].SrcReg[j].File;
460 if (f == PROGRAM_CONSTANT ||
461 f == PROGRAM_UNIFORM ||
462 f == PROGRAM_STATE_VAR) {
463 inst[i].SrcReg[j].Index += offset;
464 }
465 }
466 }
467}
468
469
470/**
471 * Combine two programs into one. Fix instructions so the outputs of
472 * the first program go to the inputs of the second program.
473 */
474struct gl_program *
475_mesa_combine_programs(GLcontext *ctx,
Brian1203f542007-10-29 16:38:53 -0600476 const struct gl_program *progA,
477 const struct gl_program *progB)
Brian9ffd8892007-10-29 16:35:59 -0600478{
479 struct prog_instruction *newInst;
480 struct gl_program *newProg;
481 const GLuint lenA = progA->NumInstructions - 1; /* omit END instr */
482 const GLuint lenB = progB->NumInstructions;
483 const GLuint numParamsA = _mesa_num_parameters(progA->Parameters);
484 const GLuint newLength = lenA + lenB;
Brianfb9cf482007-10-30 18:26:34 -0600485 GLbitfield inputsB;
Brian9ffd8892007-10-29 16:35:59 -0600486 GLuint i;
487
488 ASSERT(progA->Target == progB->Target);
489
490 newInst = _mesa_alloc_instructions(newLength);
491 if (!newInst)
492 return GL_FALSE;
493
494 _mesa_copy_instructions(newInst, progA->Instructions, lenA);
495 _mesa_copy_instructions(newInst + lenA, progB->Instructions, lenB);
496
497 /* adjust branch / instruction addresses for B's instructions */
498 for (i = 0; i < lenB; i++) {
499 newInst[lenA + i].BranchTarget += lenA;
500 }
501
502 newProg = ctx->Driver.NewProgram(ctx, progA->Target, 0);
503 newProg->Instructions = newInst;
504 newProg->NumInstructions = newLength;
505
506 if (newProg->Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian27cff442008-01-16 13:54:32 -0700507 struct gl_fragment_program *fprogA, *fprogB, *newFprog;
508 fprogA = (struct gl_fragment_program *) progA;
509 fprogB = (struct gl_fragment_program *) progB;
510 newFprog = (struct gl_fragment_program *) newProg;
511
512 newFprog->UsesKill = fprogA->UsesKill || fprogB->UsesKill;
513
Brian Paulcf7daba2008-03-25 12:27:32 -0600514 /* Connect color outputs of fprogA to color inputs of fprogB, via a
515 * new temporary register.
516 */
Brian9ffd8892007-10-29 16:35:59 -0600517 if ((progA->OutputsWritten & (1 << FRAG_RESULT_COLR)) &&
518 (progB->InputsRead & (1 << FRAG_ATTRIB_COL0))) {
Brian Paulcf7daba2008-03-25 12:27:32 -0600519 GLint tempReg = _mesa_find_free_register(newProg, PROGRAM_TEMPORARY);
520 if (!tempReg) {
521 _mesa_problem(ctx, "No free temp regs found in "
522 "_mesa_combine_programs(), using 31");
523 tempReg = 31;
524 }
525 /* replace writes to result.color[0] with tempReg */
526 replace_registers(newInst, lenA,
527 PROGRAM_OUTPUT, FRAG_RESULT_COLR,
528 PROGRAM_TEMPORARY, tempReg);
529 /* replace reads from input.color[0] with tempReg */
Brian9ffd8892007-10-29 16:35:59 -0600530 replace_registers(newInst + lenA, lenB,
531 PROGRAM_INPUT, FRAG_ATTRIB_COL0,
Brian Paulcf7daba2008-03-25 12:27:32 -0600532 PROGRAM_TEMPORARY, tempReg);
Brian9ffd8892007-10-29 16:35:59 -0600533 }
534
Brianfb9cf482007-10-30 18:26:34 -0600535 inputsB = progB->InputsRead;
536 if (progA->OutputsWritten & (1 << FRAG_RESULT_COLR)) {
537 inputsB &= ~(1 << FRAG_ATTRIB_COL0);
538 }
539 newProg->InputsRead = progA->InputsRead | inputsB;
Brian9ffd8892007-10-29 16:35:59 -0600540 newProg->OutputsWritten = progB->OutputsWritten;
Brian Paul72f2c552008-04-04 11:20:44 -0600541 newProg->SamplersUsed = progA->SamplersUsed | progB->SamplersUsed;
Brian9ffd8892007-10-29 16:35:59 -0600542 }
543 else {
544 /* vertex program */
545 assert(0); /* XXX todo */
546 }
547
548 /*
549 * Merge parameters (uniforms, constants, etc)
550 */
551 newProg->Parameters = _mesa_combine_parameter_lists(progA->Parameters,
552 progB->Parameters);
553
554 adjust_param_indexes(newInst + lenA, lenB, numParamsA);
555
556
557 return newProg;
558}
559
560
561
562
563/**
564 * Scan the given program to find a free register of the given type.
565 * \param regFile - PROGRAM_INPUT, PROGRAM_OUTPUT or PROGRAM_TEMPORARY
566 */
567GLint
568_mesa_find_free_register(const struct gl_program *prog, GLuint regFile)
569{
570 GLboolean used[MAX_PROGRAM_TEMPS];
571 GLuint i, k;
572
573 assert(regFile == PROGRAM_INPUT ||
574 regFile == PROGRAM_OUTPUT ||
575 regFile == PROGRAM_TEMPORARY);
576
577 _mesa_memset(used, 0, sizeof(used));
578
579 for (i = 0; i < prog->NumInstructions; i++) {
580 const struct prog_instruction *inst = prog->Instructions + i;
581 const GLuint n = _mesa_num_inst_src_regs(inst->Opcode);
582
583 for (k = 0; k < n; k++) {
584 if (inst->SrcReg[k].File == regFile) {
585 used[inst->SrcReg[k].Index] = GL_TRUE;
586 }
587 }
588 }
589
590 for (i = 0; i < MAX_PROGRAM_TEMPS; i++) {
591 if (!used[i])
592 return i;
593 }
594
595 return -1;
596}
597
598
599
600/**
Brian Paul77427a12006-08-24 23:11:39 +0000601 * Mixing ARB and NV vertex/fragment programs can be tricky.
602 * Note: GL_VERTEX_PROGRAM_ARB == GL_VERTEX_PROGRAM_NV
603 * but, GL_FRAGMENT_PROGRAM_ARB != GL_FRAGMENT_PROGRAM_NV
604 * The two different fragment program targets are supposed to be compatible
605 * to some extent (see GL_ARB_fragment_program spec).
606 * This function does the compatibility check.
607 */
608static GLboolean
609compatible_program_targets(GLenum t1, GLenum t2)
610{
611 if (t1 == t2)
612 return GL_TRUE;
613 if (t1 == GL_FRAGMENT_PROGRAM_ARB && t2 == GL_FRAGMENT_PROGRAM_NV)
614 return GL_TRUE;
615 if (t1 == GL_FRAGMENT_PROGRAM_NV && t2 == GL_FRAGMENT_PROGRAM_ARB)
616 return GL_TRUE;
617 return GL_FALSE;
618}
619
620
Brian Paul30d6a4b2005-11-05 20:18:18 +0000621
Michal Krol2861e732004-03-29 11:09:34 +0000622/**********************************************************************/
623/* API functions */
624/**********************************************************************/
625
626
627/**
628 * Bind a program (make it current)
629 * \note Called from the GL API dispatcher by both glBindProgramNV
630 * and glBindProgramARB.
631 */
632void GLAPIENTRY
633_mesa_BindProgram(GLenum target, GLuint id)
634{
Brian Paula360bc32006-08-25 17:18:56 +0000635 struct gl_program *curProg, *newProg;
Michal Krol2861e732004-03-29 11:09:34 +0000636 GET_CURRENT_CONTEXT(ctx);
637 ASSERT_OUTSIDE_BEGIN_END(ctx);
638
639 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
640
Brian Paula360bc32006-08-25 17:18:56 +0000641 /* Error-check target and get curProg */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000642 if ((target == GL_VERTEX_PROGRAM_ARB) && /* == GL_VERTEX_PROGRAM_NV */
643 (ctx->Extensions.NV_vertex_program ||
644 ctx->Extensions.ARB_vertex_program)) {
Brian Paula360bc32006-08-25 17:18:56 +0000645 curProg = &ctx->VertexProgram.Current->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000646 }
647 else if ((target == GL_FRAGMENT_PROGRAM_NV
648 && ctx->Extensions.NV_fragment_program) ||
649 (target == GL_FRAGMENT_PROGRAM_ARB
650 && ctx->Extensions.ARB_fragment_program)) {
Brian Paula360bc32006-08-25 17:18:56 +0000651 curProg = &ctx->FragmentProgram.Current->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000652 }
653 else {
654 _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)");
655 return;
656 }
657
Brian Paula360bc32006-08-25 17:18:56 +0000658 /*
659 * Get pointer to new program to bind.
660 * NOTE: binding to a non-existant program is not an error.
Michal Krol2861e732004-03-29 11:09:34 +0000661 * That's supposed to be caught in glBegin.
662 */
663 if (id == 0) {
Brian Paula360bc32006-08-25 17:18:56 +0000664 /* Bind a default program */
665 newProg = NULL;
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000666 if (target == GL_VERTEX_PROGRAM_ARB) /* == GL_VERTEX_PROGRAM_NV */
Brian Paula360bc32006-08-25 17:18:56 +0000667 newProg = ctx->Shared->DefaultVertexProgram;
Michal Krol2861e732004-03-29 11:09:34 +0000668 else
Brian Paula360bc32006-08-25 17:18:56 +0000669 newProg = ctx->Shared->DefaultFragmentProgram;
Michal Krol2861e732004-03-29 11:09:34 +0000670 }
671 else {
Brian Paula360bc32006-08-25 17:18:56 +0000672 /* Bind a user program */
673 newProg = _mesa_lookup_program(ctx, id);
674 if (!newProg || newProg == &_mesa_DummyProgram) {
Michal Krol2861e732004-03-29 11:09:34 +0000675 /* allocate a new program now */
Brian Paula360bc32006-08-25 17:18:56 +0000676 newProg = ctx->Driver.NewProgram(ctx, target, id);
677 if (!newProg) {
Michal Krol2861e732004-03-29 11:09:34 +0000678 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB");
679 return;
680 }
Brian Paula360bc32006-08-25 17:18:56 +0000681 _mesa_HashInsert(ctx->Shared->Programs, id, newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000682 }
Brian Paula360bc32006-08-25 17:18:56 +0000683 else if (!compatible_program_targets(newProg->Target, target)) {
Brian Paul765f1a12004-09-14 22:28:27 +0000684 _mesa_error(ctx, GL_INVALID_OPERATION,
685 "glBindProgramNV/ARB(target mismatch)");
686 return;
687 }
Michal Krol2861e732004-03-29 11:09:34 +0000688 }
689
Brian Paula360bc32006-08-25 17:18:56 +0000690 /** All error checking is complete now **/
691
692 if (curProg->Id == id) {
693 /* binding same program - no change */
694 return;
695 }
696
697 /* unbind/delete oldProg */
698 if (curProg->Id != 0) {
699 /* decrement refcount on previously bound fragment program */
700 curProg->RefCount--;
701 /* and delete if refcount goes below one */
702 if (curProg->RefCount <= 0) {
703 /* the program ID was already removed from the hash table */
704 ctx->Driver.DeleteProgram(ctx, curProg);
705 }
706 }
707
708 /* bind newProg */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000709 if (target == GL_VERTEX_PROGRAM_ARB) { /* == GL_VERTEX_PROGRAM_NV */
Brian Paula360bc32006-08-25 17:18:56 +0000710 ctx->VertexProgram.Current = (struct gl_vertex_program *) newProg;
Michal Krol2861e732004-03-29 11:09:34 +0000711 }
Brian Paula360bc32006-08-25 17:18:56 +0000712 else if (target == GL_FRAGMENT_PROGRAM_NV ||
713 target == GL_FRAGMENT_PROGRAM_ARB) {
714 ctx->FragmentProgram.Current = (struct gl_fragment_program *) newProg;
Michal Krol2861e732004-03-29 11:09:34 +0000715 }
Brian Paula360bc32006-08-25 17:18:56 +0000716 newProg->RefCount++;
Michal Krol2861e732004-03-29 11:09:34 +0000717
Brian Paul765f1a12004-09-14 22:28:27 +0000718 /* Never null pointers */
719 ASSERT(ctx->VertexProgram.Current);
720 ASSERT(ctx->FragmentProgram.Current);
721
Michal Krol2861e732004-03-29 11:09:34 +0000722 if (ctx->Driver.BindProgram)
Brian Paula360bc32006-08-25 17:18:56 +0000723 ctx->Driver.BindProgram(ctx, target, newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000724}
725
726
727/**
728 * Delete a list of programs.
729 * \note Not compiled into display lists.
730 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
731 */
732void GLAPIENTRY
733_mesa_DeletePrograms(GLsizei n, const GLuint *ids)
734{
735 GLint i;
736 GET_CURRENT_CONTEXT(ctx);
737 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
738
739 if (n < 0) {
740 _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" );
741 return;
742 }
743
744 for (i = 0; i < n; i++) {
745 if (ids[i] != 0) {
Brian Paul4d12a052006-08-23 23:10:14 +0000746 struct gl_program *prog = _mesa_lookup_program(ctx, ids[i]);
Brian Paul9ca83922004-10-02 15:16:59 +0000747 if (prog == &_mesa_DummyProgram) {
Brian Paul765f1a12004-09-14 22:28:27 +0000748 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
749 }
750 else if (prog) {
751 /* Unbind program if necessary */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000752 if (prog->Target == GL_VERTEX_PROGRAM_ARB || /* == GL_VERTEX_PROGRAM_NV */
Michal Krol2861e732004-03-29 11:09:34 +0000753 prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
754 if (ctx->VertexProgram.Current &&
755 ctx->VertexProgram.Current->Base.Id == ids[i]) {
756 /* unbind this currently bound program */
757 _mesa_BindProgram(prog->Target, 0);
758 }
759 }
760 else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
761 prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
762 if (ctx->FragmentProgram.Current &&
763 ctx->FragmentProgram.Current->Base.Id == ids[i]) {
764 /* unbind this currently bound program */
765 _mesa_BindProgram(prog->Target, 0);
766 }
767 }
768 else {
769 _mesa_problem(ctx, "bad target in glDeleteProgramsNV");
770 return;
771 }
Brian Paulea2943e2005-01-20 04:02:02 +0000772 /* The ID is immediately available for re-use now */
773 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
774 prog->RefCount--;
Michal Krol2861e732004-03-29 11:09:34 +0000775 if (prog->RefCount <= 0) {
776 ctx->Driver.DeleteProgram(ctx, prog);
777 }
778 }
Michal Krol2861e732004-03-29 11:09:34 +0000779 }
780 }
781}
782
783
784/**
785 * Generate a list of new program identifiers.
786 * \note Not compiled into display lists.
787 * \note Called by both glGenProgramsNV and glGenProgramsARB.
788 */
789void GLAPIENTRY
790_mesa_GenPrograms(GLsizei n, GLuint *ids)
791{
792 GLuint first;
793 GLuint i;
794 GET_CURRENT_CONTEXT(ctx);
795 ASSERT_OUTSIDE_BEGIN_END(ctx);
796
797 if (n < 0) {
798 _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms");
799 return;
800 }
801
802 if (!ids)
803 return;
804
805 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n);
806
Brian Paul765f1a12004-09-14 22:28:27 +0000807 /* Insert pointer to dummy program as placeholder */
Michal Krol2861e732004-03-29 11:09:34 +0000808 for (i = 0; i < (GLuint) n; i++) {
Brian Paul9ca83922004-10-02 15:16:59 +0000809 _mesa_HashInsert(ctx->Shared->Programs, first + i, &_mesa_DummyProgram);
Michal Krol2861e732004-03-29 11:09:34 +0000810 }
811
812 /* Return the program names */
813 for (i = 0; i < (GLuint) n; i++) {
814 ids[i] = first + i;
815 }
816}