blob: 0ed7f833d20861dca16a6fdbe1ac2e18932c991c [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
Brian5d1e7302008-04-07 11:16:18 -0600290 _mesa_free_instructions(prog->Instructions, prog->NumInstructions);
Brian Paulde997602005-11-12 17:53:14 +0000291
Brian Paul65a51c02006-05-24 03:30:31 +0000292 if (prog->Parameters) {
Brian Paulde997602005-11-12 17:53:14 +0000293 _mesa_free_parameter_list(prog->Parameters);
Brian Paul65a51c02006-05-24 03:30:31 +0000294 }
Brianfe1d01c2006-12-13 14:54:47 -0700295 if (prog->Varying) {
296 _mesa_free_parameter_list(prog->Varying);
297 }
Brian3493e862007-03-24 16:18:13 -0600298 if (prog->Attributes) {
299 _mesa_free_parameter_list(prog->Attributes);
300 }
Brianfe1d01c2006-12-13 14:54:47 -0700301
Brian Paul58d080b2006-08-25 19:46:31 +0000302 /* XXX this is a little ugly */
303 if (prog->Target == GL_VERTEX_PROGRAM_ARB) {
304 struct gl_vertex_program *vprog = (struct gl_vertex_program *) prog;
305 if (vprog->TnlData)
306 _mesa_free(vprog->TnlData);
307 }
308
Michal Krol2861e732004-03-29 11:09:34 +0000309 _mesa_free(prog);
310}
311
312
Brian Paul4d12a052006-08-23 23:10:14 +0000313/**
314 * Return the gl_program object for a given ID.
315 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
316 * casts elsewhere.
317 */
318struct gl_program *
319_mesa_lookup_program(GLcontext *ctx, GLuint id)
320{
321 if (id)
322 return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id);
323 else
324 return NULL;
325}
326
Michal Krol2861e732004-03-29 11:09:34 +0000327
Brian Paul3b9b8de2006-08-24 21:57:36 +0000328/**
Brianb2a3a852006-12-14 13:56:58 -0700329 * Return a copy of a program.
330 * XXX Problem here if the program object is actually OO-derivation
331 * made by a device driver.
332 */
333struct gl_program *
334_mesa_clone_program(GLcontext *ctx, const struct gl_program *prog)
335{
336 struct gl_program *clone;
337
Brian4477a012007-07-24 09:57:26 -0600338 clone = ctx->Driver.NewProgram(ctx, prog->Target, prog->Id);
Brianb2a3a852006-12-14 13:56:58 -0700339 if (!clone)
340 return NULL;
341
342 assert(clone->Target == prog->Target);
343 clone->String = (GLubyte *) _mesa_strdup((char *) prog->String);
344 clone->RefCount = 1;
345 clone->Format = prog->Format;
346 clone->Instructions = _mesa_alloc_instructions(prog->NumInstructions);
347 if (!clone->Instructions) {
Brianf8acc392008-03-22 10:27:55 -0600348 ctx->Driver.DeleteProgram(ctx, clone);
Brianb2a3a852006-12-14 13:56:58 -0700349 return NULL;
350 }
Brian12229f12007-03-22 09:11:26 -0600351 _mesa_copy_instructions(clone->Instructions, prog->Instructions,
352 prog->NumInstructions);
Brianb2a3a852006-12-14 13:56:58 -0700353 clone->InputsRead = prog->InputsRead;
354 clone->OutputsWritten = prog->OutputsWritten;
Brian Paul72f2c552008-04-04 11:20:44 -0600355 clone->SamplersUsed = prog->SamplersUsed;
Brianc9db2232007-01-04 17:22:19 -0700356 memcpy(clone->TexturesUsed, prog->TexturesUsed, sizeof(prog->TexturesUsed));
357
Brian2e76f0a2006-12-19 09:52:07 -0700358 if (prog->Parameters)
359 clone->Parameters = _mesa_clone_parameter_list(prog->Parameters);
Brianb2a3a852006-12-14 13:56:58 -0700360 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
Brian2e76f0a2006-12-19 09:52:07 -0700361 if (prog->Varying)
362 clone->Varying = _mesa_clone_parameter_list(prog->Varying);
Brian3209c3e2007-01-09 17:49:24 -0700363 if (prog->Attributes)
364 clone->Attributes = _mesa_clone_parameter_list(prog->Attributes);
Brianb2a3a852006-12-14 13:56:58 -0700365 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
366 clone->NumInstructions = prog->NumInstructions;
367 clone->NumTemporaries = prog->NumTemporaries;
368 clone->NumParameters = prog->NumParameters;
369 clone->NumAttributes = prog->NumAttributes;
370 clone->NumAddressRegs = prog->NumAddressRegs;
371 clone->NumNativeInstructions = prog->NumNativeInstructions;
372 clone->NumNativeTemporaries = prog->NumNativeTemporaries;
373 clone->NumNativeParameters = prog->NumNativeParameters;
374 clone->NumNativeAttributes = prog->NumNativeAttributes;
375 clone->NumNativeAddressRegs = prog->NumNativeAddressRegs;
Brian21f99792007-01-09 11:00:21 -0700376 clone->NumAluInstructions = prog->NumAluInstructions;
377 clone->NumTexInstructions = prog->NumTexInstructions;
378 clone->NumTexIndirections = prog->NumTexIndirections;
379 clone->NumNativeAluInstructions = prog->NumNativeAluInstructions;
380 clone->NumNativeTexInstructions = prog->NumNativeTexInstructions;
381 clone->NumNativeTexIndirections = prog->NumNativeTexIndirections;
Brianb2a3a852006-12-14 13:56:58 -0700382
383 switch (prog->Target) {
384 case GL_VERTEX_PROGRAM_ARB:
385 {
386 const struct gl_vertex_program *vp
387 = (const struct gl_vertex_program *) prog;
388 struct gl_vertex_program *vpc = (struct gl_vertex_program *) clone;
389 vpc->IsPositionInvariant = vp->IsPositionInvariant;
390 }
391 break;
392 case GL_FRAGMENT_PROGRAM_ARB:
393 {
394 const struct gl_fragment_program *fp
395 = (const struct gl_fragment_program *) prog;
396 struct gl_fragment_program *fpc = (struct gl_fragment_program *) clone;
Brianb2a3a852006-12-14 13:56:58 -0700397 fpc->FogOption = fp->FogOption;
398 fpc->UsesKill = fp->UsesKill;
399 }
400 break;
401 default:
402 _mesa_problem(NULL, "Unexpected target in _mesa_clone_program");
403 }
404
405 return clone;
406}
407
408
Brian5d1e7302008-04-07 11:16:18 -0600409/**
410 * Insert 'count' NOP instructions at 'start' in the given program.
411 * Adjust branch targets accordingly.
412 */
413GLboolean
414_mesa_insert_instructions(struct gl_program *prog, GLuint start, GLuint count)
415{
416 const GLuint origLen = prog->NumInstructions;
417 const GLuint newLen = origLen + count;
418 struct prog_instruction *newInst;
419 GLuint i;
420
421 /* adjust branches */
422 for (i = 0; i < prog->NumInstructions; i++) {
423 struct prog_instruction *inst = prog->Instructions + i;
424 if (inst->BranchTarget > 0) {
425 if (inst->BranchTarget >= start) {
426 inst->BranchTarget += count;
427 }
428 }
429 }
430
431 /* Alloc storage for new instructions */
432 newInst = _mesa_alloc_instructions(newLen);
433 if (!newInst) {
434 return GL_FALSE;
435 }
436
437 /* Copy 'start' instructions into new instruction buffer */
438 _mesa_copy_instructions(newInst, prog->Instructions, start);
439
440 /* init the new instructions */
441 _mesa_init_instructions(newInst + start, count);
442
443 /* Copy the remaining/tail instructions to new inst buffer */
444 _mesa_copy_instructions(newInst + start + count,
445 prog->Instructions + start,
446 origLen - start);
447
448 /* free old instructions */
449 _mesa_free_instructions(prog->Instructions, origLen);
450
451 /* install new instructions */
452 prog->Instructions = newInst;
453 prog->NumInstructions = newLen;
454
455 return GL_TRUE;
456}
457
Brianb2a3a852006-12-14 13:56:58 -0700458
459/**
Brian9ffd8892007-10-29 16:35:59 -0600460 * Search instructions for registers that match (oldFile, oldIndex),
461 * replacing them with (newFile, newIndex).
462 */
463static void
464replace_registers(struct prog_instruction *inst, GLuint numInst,
465 GLuint oldFile, GLuint oldIndex,
466 GLuint newFile, GLuint newIndex)
467{
468 GLuint i, j;
469 for (i = 0; i < numInst; i++) {
Brian Paulcf7daba2008-03-25 12:27:32 -0600470 /* src regs */
Brian9ffd8892007-10-29 16:35:59 -0600471 for (j = 0; j < _mesa_num_inst_src_regs(inst->Opcode); j++) {
472 if (inst[i].SrcReg[j].File == oldFile &&
473 inst[i].SrcReg[j].Index == oldIndex) {
474 inst[i].SrcReg[j].File = newFile;
475 inst[i].SrcReg[j].Index = newIndex;
476 }
477 }
Brian Paulcf7daba2008-03-25 12:27:32 -0600478 /* dst reg */
479 if (inst[i].DstReg.File == oldFile && inst[i].DstReg.Index == oldIndex) {
480 inst[i].DstReg.File = newFile;
481 inst[i].DstReg.Index = newIndex;
482 }
Brian9ffd8892007-10-29 16:35:59 -0600483 }
484}
485
486
487/**
488 * Search instructions for references to program parameters. When found,
489 * increment the parameter index by 'offset'.
490 * Used when combining programs.
491 */
492static void
493adjust_param_indexes(struct prog_instruction *inst, GLuint numInst,
494 GLuint offset)
495{
496 GLuint i, j;
497 for (i = 0; i < numInst; i++) {
498 for (j = 0; j < _mesa_num_inst_src_regs(inst->Opcode); j++) {
499 GLuint f = inst[i].SrcReg[j].File;
500 if (f == PROGRAM_CONSTANT ||
501 f == PROGRAM_UNIFORM ||
502 f == PROGRAM_STATE_VAR) {
503 inst[i].SrcReg[j].Index += offset;
504 }
505 }
506 }
507}
508
509
510/**
511 * Combine two programs into one. Fix instructions so the outputs of
512 * the first program go to the inputs of the second program.
513 */
514struct gl_program *
515_mesa_combine_programs(GLcontext *ctx,
Brian1203f542007-10-29 16:38:53 -0600516 const struct gl_program *progA,
517 const struct gl_program *progB)
Brian9ffd8892007-10-29 16:35:59 -0600518{
519 struct prog_instruction *newInst;
520 struct gl_program *newProg;
521 const GLuint lenA = progA->NumInstructions - 1; /* omit END instr */
522 const GLuint lenB = progB->NumInstructions;
523 const GLuint numParamsA = _mesa_num_parameters(progA->Parameters);
524 const GLuint newLength = lenA + lenB;
Brianfb9cf482007-10-30 18:26:34 -0600525 GLbitfield inputsB;
Brian9ffd8892007-10-29 16:35:59 -0600526 GLuint i;
527
528 ASSERT(progA->Target == progB->Target);
529
530 newInst = _mesa_alloc_instructions(newLength);
531 if (!newInst)
532 return GL_FALSE;
533
534 _mesa_copy_instructions(newInst, progA->Instructions, lenA);
535 _mesa_copy_instructions(newInst + lenA, progB->Instructions, lenB);
536
537 /* adjust branch / instruction addresses for B's instructions */
538 for (i = 0; i < lenB; i++) {
539 newInst[lenA + i].BranchTarget += lenA;
540 }
541
542 newProg = ctx->Driver.NewProgram(ctx, progA->Target, 0);
543 newProg->Instructions = newInst;
544 newProg->NumInstructions = newLength;
545
546 if (newProg->Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian27cff442008-01-16 13:54:32 -0700547 struct gl_fragment_program *fprogA, *fprogB, *newFprog;
548 fprogA = (struct gl_fragment_program *) progA;
549 fprogB = (struct gl_fragment_program *) progB;
550 newFprog = (struct gl_fragment_program *) newProg;
551
552 newFprog->UsesKill = fprogA->UsesKill || fprogB->UsesKill;
553
Brian Paulcf7daba2008-03-25 12:27:32 -0600554 /* Connect color outputs of fprogA to color inputs of fprogB, via a
555 * new temporary register.
556 */
Brian9ffd8892007-10-29 16:35:59 -0600557 if ((progA->OutputsWritten & (1 << FRAG_RESULT_COLR)) &&
558 (progB->InputsRead & (1 << FRAG_ATTRIB_COL0))) {
Brian Paulcf7daba2008-03-25 12:27:32 -0600559 GLint tempReg = _mesa_find_free_register(newProg, PROGRAM_TEMPORARY);
560 if (!tempReg) {
561 _mesa_problem(ctx, "No free temp regs found in "
562 "_mesa_combine_programs(), using 31");
563 tempReg = 31;
564 }
565 /* replace writes to result.color[0] with tempReg */
566 replace_registers(newInst, lenA,
567 PROGRAM_OUTPUT, FRAG_RESULT_COLR,
568 PROGRAM_TEMPORARY, tempReg);
569 /* replace reads from input.color[0] with tempReg */
Brian9ffd8892007-10-29 16:35:59 -0600570 replace_registers(newInst + lenA, lenB,
571 PROGRAM_INPUT, FRAG_ATTRIB_COL0,
Brian Paulcf7daba2008-03-25 12:27:32 -0600572 PROGRAM_TEMPORARY, tempReg);
Brian9ffd8892007-10-29 16:35:59 -0600573 }
574
Brianfb9cf482007-10-30 18:26:34 -0600575 inputsB = progB->InputsRead;
576 if (progA->OutputsWritten & (1 << FRAG_RESULT_COLR)) {
577 inputsB &= ~(1 << FRAG_ATTRIB_COL0);
578 }
579 newProg->InputsRead = progA->InputsRead | inputsB;
Brian9ffd8892007-10-29 16:35:59 -0600580 newProg->OutputsWritten = progB->OutputsWritten;
Brian Paul72f2c552008-04-04 11:20:44 -0600581 newProg->SamplersUsed = progA->SamplersUsed | progB->SamplersUsed;
Brian9ffd8892007-10-29 16:35:59 -0600582 }
583 else {
584 /* vertex program */
585 assert(0); /* XXX todo */
586 }
587
588 /*
589 * Merge parameters (uniforms, constants, etc)
590 */
591 newProg->Parameters = _mesa_combine_parameter_lists(progA->Parameters,
592 progB->Parameters);
593
594 adjust_param_indexes(newInst + lenA, lenB, numParamsA);
595
596
597 return newProg;
598}
599
600
601
602
603/**
604 * Scan the given program to find a free register of the given type.
605 * \param regFile - PROGRAM_INPUT, PROGRAM_OUTPUT or PROGRAM_TEMPORARY
606 */
607GLint
608_mesa_find_free_register(const struct gl_program *prog, GLuint regFile)
609{
610 GLboolean used[MAX_PROGRAM_TEMPS];
611 GLuint i, k;
612
613 assert(regFile == PROGRAM_INPUT ||
614 regFile == PROGRAM_OUTPUT ||
615 regFile == PROGRAM_TEMPORARY);
616
617 _mesa_memset(used, 0, sizeof(used));
618
619 for (i = 0; i < prog->NumInstructions; i++) {
620 const struct prog_instruction *inst = prog->Instructions + i;
621 const GLuint n = _mesa_num_inst_src_regs(inst->Opcode);
622
623 for (k = 0; k < n; k++) {
624 if (inst->SrcReg[k].File == regFile) {
625 used[inst->SrcReg[k].Index] = GL_TRUE;
626 }
627 }
628 }
629
630 for (i = 0; i < MAX_PROGRAM_TEMPS; i++) {
631 if (!used[i])
632 return i;
633 }
634
635 return -1;
636}
637
638
639
640/**
Brian Paul77427a12006-08-24 23:11:39 +0000641 * Mixing ARB and NV vertex/fragment programs can be tricky.
642 * Note: GL_VERTEX_PROGRAM_ARB == GL_VERTEX_PROGRAM_NV
643 * but, GL_FRAGMENT_PROGRAM_ARB != GL_FRAGMENT_PROGRAM_NV
644 * The two different fragment program targets are supposed to be compatible
645 * to some extent (see GL_ARB_fragment_program spec).
646 * This function does the compatibility check.
647 */
648static GLboolean
649compatible_program_targets(GLenum t1, GLenum t2)
650{
651 if (t1 == t2)
652 return GL_TRUE;
653 if (t1 == GL_FRAGMENT_PROGRAM_ARB && t2 == GL_FRAGMENT_PROGRAM_NV)
654 return GL_TRUE;
655 if (t1 == GL_FRAGMENT_PROGRAM_NV && t2 == GL_FRAGMENT_PROGRAM_ARB)
656 return GL_TRUE;
657 return GL_FALSE;
658}
659
660
Brian Paul30d6a4b2005-11-05 20:18:18 +0000661
Michal Krol2861e732004-03-29 11:09:34 +0000662/**********************************************************************/
663/* API functions */
664/**********************************************************************/
665
666
667/**
668 * Bind a program (make it current)
669 * \note Called from the GL API dispatcher by both glBindProgramNV
670 * and glBindProgramARB.
671 */
672void GLAPIENTRY
673_mesa_BindProgram(GLenum target, GLuint id)
674{
Brian Paula360bc32006-08-25 17:18:56 +0000675 struct gl_program *curProg, *newProg;
Michal Krol2861e732004-03-29 11:09:34 +0000676 GET_CURRENT_CONTEXT(ctx);
677 ASSERT_OUTSIDE_BEGIN_END(ctx);
678
679 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
680
Brian Paula360bc32006-08-25 17:18:56 +0000681 /* Error-check target and get curProg */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000682 if ((target == GL_VERTEX_PROGRAM_ARB) && /* == GL_VERTEX_PROGRAM_NV */
683 (ctx->Extensions.NV_vertex_program ||
684 ctx->Extensions.ARB_vertex_program)) {
Brian Paula360bc32006-08-25 17:18:56 +0000685 curProg = &ctx->VertexProgram.Current->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000686 }
687 else if ((target == GL_FRAGMENT_PROGRAM_NV
688 && ctx->Extensions.NV_fragment_program) ||
689 (target == GL_FRAGMENT_PROGRAM_ARB
690 && ctx->Extensions.ARB_fragment_program)) {
Brian Paula360bc32006-08-25 17:18:56 +0000691 curProg = &ctx->FragmentProgram.Current->Base;
Michal Krol2861e732004-03-29 11:09:34 +0000692 }
693 else {
694 _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)");
695 return;
696 }
697
Brian Paula360bc32006-08-25 17:18:56 +0000698 /*
699 * Get pointer to new program to bind.
700 * NOTE: binding to a non-existant program is not an error.
Michal Krol2861e732004-03-29 11:09:34 +0000701 * That's supposed to be caught in glBegin.
702 */
703 if (id == 0) {
Brian Paula360bc32006-08-25 17:18:56 +0000704 /* Bind a default program */
705 newProg = NULL;
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000706 if (target == GL_VERTEX_PROGRAM_ARB) /* == GL_VERTEX_PROGRAM_NV */
Brian Paula360bc32006-08-25 17:18:56 +0000707 newProg = ctx->Shared->DefaultVertexProgram;
Michal Krol2861e732004-03-29 11:09:34 +0000708 else
Brian Paula360bc32006-08-25 17:18:56 +0000709 newProg = ctx->Shared->DefaultFragmentProgram;
Michal Krol2861e732004-03-29 11:09:34 +0000710 }
711 else {
Brian Paula360bc32006-08-25 17:18:56 +0000712 /* Bind a user program */
713 newProg = _mesa_lookup_program(ctx, id);
714 if (!newProg || newProg == &_mesa_DummyProgram) {
Michal Krol2861e732004-03-29 11:09:34 +0000715 /* allocate a new program now */
Brian Paula360bc32006-08-25 17:18:56 +0000716 newProg = ctx->Driver.NewProgram(ctx, target, id);
717 if (!newProg) {
Michal Krol2861e732004-03-29 11:09:34 +0000718 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB");
719 return;
720 }
Brian Paula360bc32006-08-25 17:18:56 +0000721 _mesa_HashInsert(ctx->Shared->Programs, id, newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000722 }
Brian Paula360bc32006-08-25 17:18:56 +0000723 else if (!compatible_program_targets(newProg->Target, target)) {
Brian Paul765f1a12004-09-14 22:28:27 +0000724 _mesa_error(ctx, GL_INVALID_OPERATION,
725 "glBindProgramNV/ARB(target mismatch)");
726 return;
727 }
Michal Krol2861e732004-03-29 11:09:34 +0000728 }
729
Brian Paula360bc32006-08-25 17:18:56 +0000730 /** All error checking is complete now **/
731
732 if (curProg->Id == id) {
733 /* binding same program - no change */
734 return;
735 }
736
737 /* unbind/delete oldProg */
738 if (curProg->Id != 0) {
739 /* decrement refcount on previously bound fragment program */
740 curProg->RefCount--;
741 /* and delete if refcount goes below one */
742 if (curProg->RefCount <= 0) {
743 /* the program ID was already removed from the hash table */
744 ctx->Driver.DeleteProgram(ctx, curProg);
745 }
746 }
747
748 /* bind newProg */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000749 if (target == GL_VERTEX_PROGRAM_ARB) { /* == GL_VERTEX_PROGRAM_NV */
Brian Paula360bc32006-08-25 17:18:56 +0000750 ctx->VertexProgram.Current = (struct gl_vertex_program *) newProg;
Michal Krol2861e732004-03-29 11:09:34 +0000751 }
Brian Paula360bc32006-08-25 17:18:56 +0000752 else if (target == GL_FRAGMENT_PROGRAM_NV ||
753 target == GL_FRAGMENT_PROGRAM_ARB) {
754 ctx->FragmentProgram.Current = (struct gl_fragment_program *) newProg;
Michal Krol2861e732004-03-29 11:09:34 +0000755 }
Brian Paula360bc32006-08-25 17:18:56 +0000756 newProg->RefCount++;
Michal Krol2861e732004-03-29 11:09:34 +0000757
Brian Paul765f1a12004-09-14 22:28:27 +0000758 /* Never null pointers */
759 ASSERT(ctx->VertexProgram.Current);
760 ASSERT(ctx->FragmentProgram.Current);
761
Michal Krol2861e732004-03-29 11:09:34 +0000762 if (ctx->Driver.BindProgram)
Brian Paula360bc32006-08-25 17:18:56 +0000763 ctx->Driver.BindProgram(ctx, target, newProg);
Michal Krol2861e732004-03-29 11:09:34 +0000764}
765
766
767/**
768 * Delete a list of programs.
769 * \note Not compiled into display lists.
770 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
771 */
772void GLAPIENTRY
773_mesa_DeletePrograms(GLsizei n, const GLuint *ids)
774{
775 GLint i;
776 GET_CURRENT_CONTEXT(ctx);
777 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
778
779 if (n < 0) {
780 _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" );
781 return;
782 }
783
784 for (i = 0; i < n; i++) {
785 if (ids[i] != 0) {
Brian Paul4d12a052006-08-23 23:10:14 +0000786 struct gl_program *prog = _mesa_lookup_program(ctx, ids[i]);
Brian Paul9ca83922004-10-02 15:16:59 +0000787 if (prog == &_mesa_DummyProgram) {
Brian Paul765f1a12004-09-14 22:28:27 +0000788 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
789 }
790 else if (prog) {
791 /* Unbind program if necessary */
Roland Scheideggere1e03b32006-03-03 15:03:04 +0000792 if (prog->Target == GL_VERTEX_PROGRAM_ARB || /* == GL_VERTEX_PROGRAM_NV */
Michal Krol2861e732004-03-29 11:09:34 +0000793 prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
794 if (ctx->VertexProgram.Current &&
795 ctx->VertexProgram.Current->Base.Id == ids[i]) {
796 /* unbind this currently bound program */
797 _mesa_BindProgram(prog->Target, 0);
798 }
799 }
800 else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
801 prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
802 if (ctx->FragmentProgram.Current &&
803 ctx->FragmentProgram.Current->Base.Id == ids[i]) {
804 /* unbind this currently bound program */
805 _mesa_BindProgram(prog->Target, 0);
806 }
807 }
808 else {
809 _mesa_problem(ctx, "bad target in glDeleteProgramsNV");
810 return;
811 }
Brian Paulea2943e2005-01-20 04:02:02 +0000812 /* The ID is immediately available for re-use now */
813 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
814 prog->RefCount--;
Michal Krol2861e732004-03-29 11:09:34 +0000815 if (prog->RefCount <= 0) {
816 ctx->Driver.DeleteProgram(ctx, prog);
817 }
818 }
Michal Krol2861e732004-03-29 11:09:34 +0000819 }
820 }
821}
822
823
824/**
825 * Generate a list of new program identifiers.
826 * \note Not compiled into display lists.
827 * \note Called by both glGenProgramsNV and glGenProgramsARB.
828 */
829void GLAPIENTRY
830_mesa_GenPrograms(GLsizei n, GLuint *ids)
831{
832 GLuint first;
833 GLuint i;
834 GET_CURRENT_CONTEXT(ctx);
835 ASSERT_OUTSIDE_BEGIN_END(ctx);
836
837 if (n < 0) {
838 _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms");
839 return;
840 }
841
842 if (!ids)
843 return;
844
845 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n);
846
Brian Paul765f1a12004-09-14 22:28:27 +0000847 /* Insert pointer to dummy program as placeholder */
Michal Krol2861e732004-03-29 11:09:34 +0000848 for (i = 0; i < (GLuint) n; i++) {
Brian Paul9ca83922004-10-02 15:16:59 +0000849 _mesa_HashInsert(ctx->Shared->Programs, first + i, &_mesa_DummyProgram);
Michal Krol2861e732004-03-29 11:09:34 +0000850 }
851
852 /* Return the program names */
853 for (i = 0; i < (GLuint) n; i++) {
854 ids[i] = first + i;
855 }
856}