use _mesa_copy_instructions()
[mesa.git] / src / mesa / shader / program.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.3
4 *
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 *
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"
35 #include "program.h"
36 #include "prog_parameter.h"
37 #include "prog_instruction.h"
38
39
40 /**
41 * A pointer to this dummy program is put into the hash table when
42 * glGenPrograms is called.
43 */
44 struct gl_program _mesa_DummyProgram;
45
46
47 /**
48 * Init context's vertex/fragment program state
49 */
50 void
51 _mesa_init_program(GLcontext *ctx)
52 {
53 GLuint i;
54
55 ctx->Program.ErrorPos = -1;
56 ctx->Program.ErrorString = _mesa_strdup("");
57
58 #if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
59 ctx->VertexProgram.Enabled = GL_FALSE;
60 ctx->VertexProgram.PointSizeEnabled = GL_FALSE;
61 ctx->VertexProgram.TwoSideEnabled = GL_FALSE;
62 ctx->VertexProgram.Current = (struct gl_vertex_program *) ctx->Shared->DefaultVertexProgram;
63 assert(ctx->VertexProgram.Current);
64 ctx->VertexProgram.Current->Base.RefCount++;
65 for (i = 0; i < MAX_NV_VERTEX_PROGRAM_PARAMS / 4; i++) {
66 ctx->VertexProgram.TrackMatrix[i] = GL_NONE;
67 ctx->VertexProgram.TrackMatrixTransform[i] = GL_IDENTITY_NV;
68 }
69 #endif
70
71 #if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
72 ctx->FragmentProgram.Enabled = GL_FALSE;
73 ctx->FragmentProgram.Current = (struct gl_fragment_program *) ctx->Shared->DefaultFragmentProgram;
74 assert(ctx->FragmentProgram.Current);
75 ctx->FragmentProgram.Current->Base.RefCount++;
76 #endif
77
78 /* XXX probably move this stuff */
79 #if FEATURE_ATI_fragment_shader
80 ctx->ATIFragmentShader.Enabled = GL_FALSE;
81 ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader;
82 assert(ctx->ATIFragmentShader.Current);
83 ctx->ATIFragmentShader.Current->RefCount++;
84 #endif
85 }
86
87
88 /**
89 * Free a context's vertex/fragment program state
90 */
91 void
92 _mesa_free_program_data(GLcontext *ctx)
93 {
94 #if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
95 if (ctx->VertexProgram.Current) {
96 ctx->VertexProgram.Current->Base.RefCount--;
97 if (ctx->VertexProgram.Current->Base.RefCount <= 0)
98 ctx->Driver.DeleteProgram(ctx, &(ctx->VertexProgram.Current->Base));
99 }
100 #endif
101 #if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
102 if (ctx->FragmentProgram.Current) {
103 ctx->FragmentProgram.Current->Base.RefCount--;
104 if (ctx->FragmentProgram.Current->Base.RefCount <= 0)
105 ctx->Driver.DeleteProgram(ctx, &(ctx->FragmentProgram.Current->Base));
106 }
107 #endif
108 /* XXX probably move this stuff */
109 #if FEATURE_ATI_fragment_shader
110 if (ctx->ATIFragmentShader.Current) {
111 ctx->ATIFragmentShader.Current->RefCount--;
112 if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
113 _mesa_free(ctx->ATIFragmentShader.Current);
114 }
115 }
116 #endif
117 _mesa_free((void *) ctx->Program.ErrorString);
118 }
119
120
121
122
123 /**
124 * Set the vertex/fragment program error state (position and error string).
125 * This is generally called from within the parsers.
126 */
127 void
128 _mesa_set_program_error(GLcontext *ctx, GLint pos, const char *string)
129 {
130 ctx->Program.ErrorPos = pos;
131 _mesa_free((void *) ctx->Program.ErrorString);
132 if (!string)
133 string = "";
134 ctx->Program.ErrorString = _mesa_strdup(string);
135 }
136
137
138 /**
139 * Find the line number and column for 'pos' within 'string'.
140 * Return a copy of the line which contains 'pos'. Free the line with
141 * _mesa_free().
142 * \param string the program string
143 * \param pos the position within the string
144 * \param line returns the line number corresponding to 'pos'.
145 * \param col returns the column number corresponding to 'pos'.
146 * \return copy of the line containing 'pos'.
147 */
148 const GLubyte *
149 _mesa_find_line_column(const GLubyte *string, const GLubyte *pos,
150 GLint *line, GLint *col)
151 {
152 const GLubyte *lineStart = string;
153 const GLubyte *p = string;
154 GLubyte *s;
155 int len;
156
157 *line = 1;
158
159 while (p != pos) {
160 if (*p == (GLubyte) '\n') {
161 (*line)++;
162 lineStart = p + 1;
163 }
164 p++;
165 }
166
167 *col = (pos - lineStart) + 1;
168
169 /* return copy of this line */
170 while (*p != 0 && *p != '\n')
171 p++;
172 len = p - lineStart;
173 s = (GLubyte *) _mesa_malloc(len + 1);
174 _mesa_memcpy(s, lineStart, len);
175 s[len] = 0;
176
177 return s;
178 }
179
180
181 /**
182 * Initialize a new vertex/fragment program object.
183 */
184 static struct gl_program *
185 _mesa_init_program_struct( GLcontext *ctx, struct gl_program *prog,
186 GLenum target, GLuint id)
187 {
188 (void) ctx;
189 if (prog) {
190 _mesa_bzero(prog, sizeof(*prog));
191 prog->Id = id;
192 prog->Target = target;
193 prog->Resident = GL_TRUE;
194 prog->RefCount = 1;
195 prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
196 }
197
198 return prog;
199 }
200
201
202 /**
203 * Initialize a new fragment program object.
204 */
205 struct gl_program *
206 _mesa_init_fragment_program( GLcontext *ctx, struct gl_fragment_program *prog,
207 GLenum target, GLuint id)
208 {
209 if (prog)
210 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
211 else
212 return NULL;
213 }
214
215
216 /**
217 * Initialize a new vertex program object.
218 */
219 struct gl_program *
220 _mesa_init_vertex_program( GLcontext *ctx, struct gl_vertex_program *prog,
221 GLenum target, GLuint id)
222 {
223 if (prog)
224 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
225 else
226 return NULL;
227 }
228
229
230 /**
231 * Allocate and initialize a new fragment/vertex program object but
232 * don't put it into the program hash table. Called via
233 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
234 * device driver function to implement OO deriviation with additional
235 * types not understood by this function.
236 *
237 * \param ctx context
238 * \param id program id/number
239 * \param target program target/type
240 * \return pointer to new program object
241 */
242 struct gl_program *
243 _mesa_new_program(GLcontext *ctx, GLenum target, GLuint id)
244 {
245 switch (target) {
246 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
247 return _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program),
248 target, id );
249 case GL_FRAGMENT_PROGRAM_NV:
250 case GL_FRAGMENT_PROGRAM_ARB:
251 return _mesa_init_fragment_program(ctx,
252 CALLOC_STRUCT(gl_fragment_program),
253 target, id );
254 default:
255 _mesa_problem(ctx, "bad target in _mesa_new_program");
256 return NULL;
257 }
258 }
259
260
261 /**
262 * Delete a program and remove it from the hash table, ignoring the
263 * reference count.
264 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
265 * by a device driver function.
266 */
267 void
268 _mesa_delete_program(GLcontext *ctx, struct gl_program *prog)
269 {
270 (void) ctx;
271 ASSERT(prog);
272
273 if (prog == &_mesa_DummyProgram)
274 return;
275
276 if (prog->String)
277 _mesa_free(prog->String);
278
279 if (prog->Instructions) {
280 GLuint i;
281 for (i = 0; i < prog->NumInstructions; i++) {
282 if (prog->Instructions[i].Data)
283 _mesa_free(prog->Instructions[i].Data);
284 }
285 _mesa_free(prog->Instructions);
286 }
287
288 if (prog->Parameters) {
289 _mesa_free_parameter_list(prog->Parameters);
290 }
291
292 if (prog->Varying) {
293 _mesa_free_parameter_list(prog->Varying);
294 }
295
296 /* XXX this is a little ugly */
297 if (prog->Target == GL_VERTEX_PROGRAM_ARB) {
298 struct gl_vertex_program *vprog = (struct gl_vertex_program *) prog;
299 if (vprog->TnlData)
300 _mesa_free(vprog->TnlData);
301 }
302
303 _mesa_free(prog);
304 }
305
306
307 /**
308 * Return the gl_program object for a given ID.
309 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
310 * casts elsewhere.
311 */
312 struct gl_program *
313 _mesa_lookup_program(GLcontext *ctx, GLuint id)
314 {
315 if (id)
316 return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id);
317 else
318 return NULL;
319 }
320
321
322 /**
323 * Return a copy of a program.
324 * XXX Problem here if the program object is actually OO-derivation
325 * made by a device driver.
326 */
327 struct gl_program *
328 _mesa_clone_program(GLcontext *ctx, const struct gl_program *prog)
329 {
330 struct gl_program *clone;
331
332 clone = _mesa_new_program(ctx, prog->Target, prog->Id);
333 if (!clone)
334 return NULL;
335
336 assert(clone->Target == prog->Target);
337 clone->String = (GLubyte *) _mesa_strdup((char *) prog->String);
338 clone->RefCount = 1;
339 clone->Format = prog->Format;
340 clone->Instructions = _mesa_alloc_instructions(prog->NumInstructions);
341 if (!clone->Instructions) {
342 _mesa_delete_program(ctx, clone);
343 return NULL;
344 }
345 _mesa_copy_instructions(clone->Instructions, prog->Instructions,
346 prog->NumInstructions);
347 clone->InputsRead = prog->InputsRead;
348 clone->OutputsWritten = prog->OutputsWritten;
349 memcpy(clone->TexturesUsed, prog->TexturesUsed, sizeof(prog->TexturesUsed));
350
351 if (prog->Parameters)
352 clone->Parameters = _mesa_clone_parameter_list(prog->Parameters);
353 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
354 if (prog->Varying)
355 clone->Varying = _mesa_clone_parameter_list(prog->Varying);
356 if (prog->Attributes)
357 clone->Attributes = _mesa_clone_parameter_list(prog->Attributes);
358 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
359 clone->NumInstructions = prog->NumInstructions;
360 clone->NumTemporaries = prog->NumTemporaries;
361 clone->NumParameters = prog->NumParameters;
362 clone->NumAttributes = prog->NumAttributes;
363 clone->NumAddressRegs = prog->NumAddressRegs;
364 clone->NumNativeInstructions = prog->NumNativeInstructions;
365 clone->NumNativeTemporaries = prog->NumNativeTemporaries;
366 clone->NumNativeParameters = prog->NumNativeParameters;
367 clone->NumNativeAttributes = prog->NumNativeAttributes;
368 clone->NumNativeAddressRegs = prog->NumNativeAddressRegs;
369 clone->NumAluInstructions = prog->NumAluInstructions;
370 clone->NumTexInstructions = prog->NumTexInstructions;
371 clone->NumTexIndirections = prog->NumTexIndirections;
372 clone->NumNativeAluInstructions = prog->NumNativeAluInstructions;
373 clone->NumNativeTexInstructions = prog->NumNativeTexInstructions;
374 clone->NumNativeTexIndirections = prog->NumNativeTexIndirections;
375
376 switch (prog->Target) {
377 case GL_VERTEX_PROGRAM_ARB:
378 {
379 const struct gl_vertex_program *vp
380 = (const struct gl_vertex_program *) prog;
381 struct gl_vertex_program *vpc = (struct gl_vertex_program *) clone;
382 vpc->IsPositionInvariant = vp->IsPositionInvariant;
383 }
384 break;
385 case GL_FRAGMENT_PROGRAM_ARB:
386 {
387 const struct gl_fragment_program *fp
388 = (const struct gl_fragment_program *) prog;
389 struct gl_fragment_program *fpc = (struct gl_fragment_program *) clone;
390 fpc->FogOption = fp->FogOption;
391 fpc->UsesKill = fp->UsesKill;
392 }
393 break;
394 default:
395 _mesa_problem(NULL, "Unexpected target in _mesa_clone_program");
396 }
397
398 return clone;
399 }
400
401
402
403 /**
404 * Mixing ARB and NV vertex/fragment programs can be tricky.
405 * Note: GL_VERTEX_PROGRAM_ARB == GL_VERTEX_PROGRAM_NV
406 * but, GL_FRAGMENT_PROGRAM_ARB != GL_FRAGMENT_PROGRAM_NV
407 * The two different fragment program targets are supposed to be compatible
408 * to some extent (see GL_ARB_fragment_program spec).
409 * This function does the compatibility check.
410 */
411 static GLboolean
412 compatible_program_targets(GLenum t1, GLenum t2)
413 {
414 if (t1 == t2)
415 return GL_TRUE;
416 if (t1 == GL_FRAGMENT_PROGRAM_ARB && t2 == GL_FRAGMENT_PROGRAM_NV)
417 return GL_TRUE;
418 if (t1 == GL_FRAGMENT_PROGRAM_NV && t2 == GL_FRAGMENT_PROGRAM_ARB)
419 return GL_TRUE;
420 return GL_FALSE;
421 }
422
423
424
425 /**********************************************************************/
426 /* API functions */
427 /**********************************************************************/
428
429
430 /**
431 * Bind a program (make it current)
432 * \note Called from the GL API dispatcher by both glBindProgramNV
433 * and glBindProgramARB.
434 */
435 void GLAPIENTRY
436 _mesa_BindProgram(GLenum target, GLuint id)
437 {
438 struct gl_program *curProg, *newProg;
439 GET_CURRENT_CONTEXT(ctx);
440 ASSERT_OUTSIDE_BEGIN_END(ctx);
441
442 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
443
444 /* Error-check target and get curProg */
445 if ((target == GL_VERTEX_PROGRAM_ARB) && /* == GL_VERTEX_PROGRAM_NV */
446 (ctx->Extensions.NV_vertex_program ||
447 ctx->Extensions.ARB_vertex_program)) {
448 curProg = &ctx->VertexProgram.Current->Base;
449 }
450 else if ((target == GL_FRAGMENT_PROGRAM_NV
451 && ctx->Extensions.NV_fragment_program) ||
452 (target == GL_FRAGMENT_PROGRAM_ARB
453 && ctx->Extensions.ARB_fragment_program)) {
454 curProg = &ctx->FragmentProgram.Current->Base;
455 }
456 else {
457 _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)");
458 return;
459 }
460
461 /*
462 * Get pointer to new program to bind.
463 * NOTE: binding to a non-existant program is not an error.
464 * That's supposed to be caught in glBegin.
465 */
466 if (id == 0) {
467 /* Bind a default program */
468 newProg = NULL;
469 if (target == GL_VERTEX_PROGRAM_ARB) /* == GL_VERTEX_PROGRAM_NV */
470 newProg = ctx->Shared->DefaultVertexProgram;
471 else
472 newProg = ctx->Shared->DefaultFragmentProgram;
473 }
474 else {
475 /* Bind a user program */
476 newProg = _mesa_lookup_program(ctx, id);
477 if (!newProg || newProg == &_mesa_DummyProgram) {
478 /* allocate a new program now */
479 newProg = ctx->Driver.NewProgram(ctx, target, id);
480 if (!newProg) {
481 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB");
482 return;
483 }
484 _mesa_HashInsert(ctx->Shared->Programs, id, newProg);
485 }
486 else if (!compatible_program_targets(newProg->Target, target)) {
487 _mesa_error(ctx, GL_INVALID_OPERATION,
488 "glBindProgramNV/ARB(target mismatch)");
489 return;
490 }
491 }
492
493 /** All error checking is complete now **/
494
495 if (curProg->Id == id) {
496 /* binding same program - no change */
497 return;
498 }
499
500 /* unbind/delete oldProg */
501 if (curProg->Id != 0) {
502 /* decrement refcount on previously bound fragment program */
503 curProg->RefCount--;
504 /* and delete if refcount goes below one */
505 if (curProg->RefCount <= 0) {
506 /* the program ID was already removed from the hash table */
507 ctx->Driver.DeleteProgram(ctx, curProg);
508 }
509 }
510
511 /* bind newProg */
512 if (target == GL_VERTEX_PROGRAM_ARB) { /* == GL_VERTEX_PROGRAM_NV */
513 ctx->VertexProgram.Current = (struct gl_vertex_program *) newProg;
514 }
515 else if (target == GL_FRAGMENT_PROGRAM_NV ||
516 target == GL_FRAGMENT_PROGRAM_ARB) {
517 ctx->FragmentProgram.Current = (struct gl_fragment_program *) newProg;
518 }
519 newProg->RefCount++;
520
521 /* Never null pointers */
522 ASSERT(ctx->VertexProgram.Current);
523 ASSERT(ctx->FragmentProgram.Current);
524
525 if (ctx->Driver.BindProgram)
526 ctx->Driver.BindProgram(ctx, target, newProg);
527 }
528
529
530 /**
531 * Delete a list of programs.
532 * \note Not compiled into display lists.
533 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
534 */
535 void GLAPIENTRY
536 _mesa_DeletePrograms(GLsizei n, const GLuint *ids)
537 {
538 GLint i;
539 GET_CURRENT_CONTEXT(ctx);
540 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
541
542 if (n < 0) {
543 _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" );
544 return;
545 }
546
547 for (i = 0; i < n; i++) {
548 if (ids[i] != 0) {
549 struct gl_program *prog = _mesa_lookup_program(ctx, ids[i]);
550 if (prog == &_mesa_DummyProgram) {
551 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
552 }
553 else if (prog) {
554 /* Unbind program if necessary */
555 if (prog->Target == GL_VERTEX_PROGRAM_ARB || /* == GL_VERTEX_PROGRAM_NV */
556 prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
557 if (ctx->VertexProgram.Current &&
558 ctx->VertexProgram.Current->Base.Id == ids[i]) {
559 /* unbind this currently bound program */
560 _mesa_BindProgram(prog->Target, 0);
561 }
562 }
563 else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
564 prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
565 if (ctx->FragmentProgram.Current &&
566 ctx->FragmentProgram.Current->Base.Id == ids[i]) {
567 /* unbind this currently bound program */
568 _mesa_BindProgram(prog->Target, 0);
569 }
570 }
571 else {
572 _mesa_problem(ctx, "bad target in glDeleteProgramsNV");
573 return;
574 }
575 /* The ID is immediately available for re-use now */
576 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
577 prog->RefCount--;
578 if (prog->RefCount <= 0) {
579 ctx->Driver.DeleteProgram(ctx, prog);
580 }
581 }
582 }
583 }
584 }
585
586
587 /**
588 * Generate a list of new program identifiers.
589 * \note Not compiled into display lists.
590 * \note Called by both glGenProgramsNV and glGenProgramsARB.
591 */
592 void GLAPIENTRY
593 _mesa_GenPrograms(GLsizei n, GLuint *ids)
594 {
595 GLuint first;
596 GLuint i;
597 GET_CURRENT_CONTEXT(ctx);
598 ASSERT_OUTSIDE_BEGIN_END(ctx);
599
600 if (n < 0) {
601 _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms");
602 return;
603 }
604
605 if (!ids)
606 return;
607
608 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n);
609
610 /* Insert pointer to dummy program as placeholder */
611 for (i = 0; i < (GLuint) n; i++) {
612 _mesa_HashInsert(ctx->Shared->Programs, first + i, &_mesa_DummyProgram);
613 }
614
615 /* Return the program names */
616 for (i = 0; i < (GLuint) n; i++) {
617 ids[i] = first + i;
618 }
619 }