mesa: use switch in _mesa_DeletePrograms()
[mesa.git] / src / mesa / shader / arbprogram.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.0
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 arbprogram.c
27 * ARB_vertex/fragment_program state management functions.
28 * \author Brian Paul
29 */
30
31
32 #include "main/glheader.h"
33 #include "main/context.h"
34 #include "main/hash.h"
35 #include "main/imports.h"
36 #include "main/macros.h"
37 #include "main/mtypes.h"
38 #include "arbprogram.h"
39 #include "arbprogparse.h"
40 #include "nvfragparse.h"
41 #include "nvvertparse.h"
42 #include "program.h"
43
44
45
46 /**
47 * Mixing ARB and NV vertex/fragment programs can be tricky.
48 * Note: GL_VERTEX_PROGRAM_ARB == GL_VERTEX_PROGRAM_NV
49 * but, GL_FRAGMENT_PROGRAM_ARB != GL_FRAGMENT_PROGRAM_NV
50 * The two different fragment program targets are supposed to be compatible
51 * to some extent (see GL_ARB_fragment_program spec).
52 * This function does the compatibility check.
53 */
54 static GLboolean
55 compatible_program_targets(GLenum t1, GLenum t2)
56 {
57 if (t1 == t2)
58 return GL_TRUE;
59 if (t1 == GL_FRAGMENT_PROGRAM_ARB && t2 == GL_FRAGMENT_PROGRAM_NV)
60 return GL_TRUE;
61 if (t1 == GL_FRAGMENT_PROGRAM_NV && t2 == GL_FRAGMENT_PROGRAM_ARB)
62 return GL_TRUE;
63 return GL_FALSE;
64 }
65
66
67 /**
68 * Bind a program (make it current)
69 * \note Called from the GL API dispatcher by both glBindProgramNV
70 * and glBindProgramARB.
71 */
72 void GLAPIENTRY
73 _mesa_BindProgram(GLenum target, GLuint id)
74 {
75 struct gl_program *curProg, *newProg;
76 GET_CURRENT_CONTEXT(ctx);
77 ASSERT_OUTSIDE_BEGIN_END(ctx);
78
79 /* Error-check target and get curProg */
80 if ((target == GL_VERTEX_PROGRAM_ARB) && /* == GL_VERTEX_PROGRAM_NV */
81 (ctx->Extensions.NV_vertex_program ||
82 ctx->Extensions.ARB_vertex_program)) {
83 curProg = &ctx->VertexProgram.Current->Base;
84 }
85 else if ((target == GL_FRAGMENT_PROGRAM_NV
86 && ctx->Extensions.NV_fragment_program) ||
87 (target == GL_FRAGMENT_PROGRAM_ARB
88 && ctx->Extensions.ARB_fragment_program)) {
89 curProg = &ctx->FragmentProgram.Current->Base;
90 }
91 else {
92 _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)");
93 return;
94 }
95
96 /*
97 * Get pointer to new program to bind.
98 * NOTE: binding to a non-existant program is not an error.
99 * That's supposed to be caught in glBegin.
100 */
101 if (id == 0) {
102 /* Bind a default program */
103 newProg = NULL;
104 if (target == GL_VERTEX_PROGRAM_ARB) /* == GL_VERTEX_PROGRAM_NV */
105 newProg = &ctx->Shared->DefaultVertexProgram->Base;
106 else
107 newProg = &ctx->Shared->DefaultFragmentProgram->Base;
108 }
109 else {
110 /* Bind a user program */
111 newProg = _mesa_lookup_program(ctx, id);
112 if (!newProg || newProg == &_mesa_DummyProgram) {
113 /* allocate a new program now */
114 newProg = ctx->Driver.NewProgram(ctx, target, id);
115 if (!newProg) {
116 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB");
117 return;
118 }
119 _mesa_HashInsert(ctx->Shared->Programs, id, newProg);
120 }
121 else if (!compatible_program_targets(newProg->Target, target)) {
122 _mesa_error(ctx, GL_INVALID_OPERATION,
123 "glBindProgramNV/ARB(target mismatch)");
124 return;
125 }
126 }
127
128 /** All error checking is complete now **/
129
130 if (curProg->Id == id) {
131 /* binding same program - no change */
132 return;
133 }
134
135 /* signal new program (and its new constants) */
136 FLUSH_VERTICES(ctx, _NEW_PROGRAM | _NEW_PROGRAM_CONSTANTS);
137
138 /* bind newProg */
139 if (target == GL_VERTEX_PROGRAM_ARB) { /* == GL_VERTEX_PROGRAM_NV */
140 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
141 (struct gl_vertex_program *) newProg);
142 }
143 else if (target == GL_FRAGMENT_PROGRAM_NV ||
144 target == GL_FRAGMENT_PROGRAM_ARB) {
145 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
146 (struct gl_fragment_program *) newProg);
147 }
148
149 /* Never null pointers */
150 ASSERT(ctx->VertexProgram.Current);
151 ASSERT(ctx->FragmentProgram.Current);
152
153 if (ctx->Driver.BindProgram)
154 ctx->Driver.BindProgram(ctx, target, newProg);
155 }
156
157
158 /**
159 * Delete a list of programs.
160 * \note Not compiled into display lists.
161 * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.
162 */
163 void GLAPIENTRY
164 _mesa_DeletePrograms(GLsizei n, const GLuint *ids)
165 {
166 GLint i;
167 GET_CURRENT_CONTEXT(ctx);
168 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
169
170 if (n < 0) {
171 _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" );
172 return;
173 }
174
175 for (i = 0; i < n; i++) {
176 if (ids[i] != 0) {
177 struct gl_program *prog = _mesa_lookup_program(ctx, ids[i]);
178 if (prog == &_mesa_DummyProgram) {
179 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
180 }
181 else if (prog) {
182 /* Unbind program if necessary */
183 switch (prog->Target) {
184 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
185 case GL_VERTEX_STATE_PROGRAM_NV:
186 if (ctx->VertexProgram.Current &&
187 ctx->VertexProgram.Current->Base.Id == ids[i]) {
188 /* unbind this currently bound program */
189 _mesa_BindProgram(prog->Target, 0);
190 }
191 break;
192 case GL_FRAGMENT_PROGRAM_NV:
193 case GL_FRAGMENT_PROGRAM_ARB:
194 if (ctx->FragmentProgram.Current &&
195 ctx->FragmentProgram.Current->Base.Id == ids[i]) {
196 /* unbind this currently bound program */
197 _mesa_BindProgram(prog->Target, 0);
198 }
199 break;
200 default:
201 _mesa_problem(ctx, "bad target in glDeleteProgramsNV");
202 return;
203 }
204 /* The ID is immediately available for re-use now */
205 _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
206 _mesa_reference_program(ctx, &prog, NULL);
207 }
208 }
209 }
210 }
211
212
213 /**
214 * Generate a list of new program identifiers.
215 * \note Not compiled into display lists.
216 * \note Called by both glGenProgramsNV and glGenProgramsARB.
217 */
218 void GLAPIENTRY
219 _mesa_GenPrograms(GLsizei n, GLuint *ids)
220 {
221 GLuint first;
222 GLuint i;
223 GET_CURRENT_CONTEXT(ctx);
224 ASSERT_OUTSIDE_BEGIN_END(ctx);
225
226 if (n < 0) {
227 _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms");
228 return;
229 }
230
231 if (!ids)
232 return;
233
234 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n);
235
236 /* Insert pointer to dummy program as placeholder */
237 for (i = 0; i < (GLuint) n; i++) {
238 _mesa_HashInsert(ctx->Shared->Programs, first + i, &_mesa_DummyProgram);
239 }
240
241 /* Return the program names */
242 for (i = 0; i < (GLuint) n; i++) {
243 ids[i] = first + i;
244 }
245 }
246
247
248 void GLAPIENTRY
249 _mesa_EnableVertexAttribArrayARB(GLuint index)
250 {
251 GET_CURRENT_CONTEXT(ctx);
252 ASSERT_OUTSIDE_BEGIN_END(ctx);
253
254 if (index >= ctx->Const.VertexProgram.MaxAttribs) {
255 _mesa_error(ctx, GL_INVALID_VALUE,
256 "glEnableVertexAttribArrayARB(index)");
257 return;
258 }
259
260 ASSERT(index < Elements(ctx->Array.ArrayObj->VertexAttrib));
261
262 FLUSH_VERTICES(ctx, _NEW_ARRAY);
263 ctx->Array.ArrayObj->VertexAttrib[index].Enabled = GL_TRUE;
264 ctx->Array.ArrayObj->_Enabled |= _NEW_ARRAY_ATTRIB(index);
265 ctx->Array.NewState |= _NEW_ARRAY_ATTRIB(index);
266 }
267
268
269 void GLAPIENTRY
270 _mesa_DisableVertexAttribArrayARB(GLuint index)
271 {
272 GET_CURRENT_CONTEXT(ctx);
273 ASSERT_OUTSIDE_BEGIN_END(ctx);
274
275 if (index >= ctx->Const.VertexProgram.MaxAttribs) {
276 _mesa_error(ctx, GL_INVALID_VALUE,
277 "glEnableVertexAttribArrayARB(index)");
278 return;
279 }
280
281 ASSERT(index < Elements(ctx->Array.ArrayObj->VertexAttrib));
282
283 FLUSH_VERTICES(ctx, _NEW_ARRAY);
284 ctx->Array.ArrayObj->VertexAttrib[index].Enabled = GL_FALSE;
285 ctx->Array.ArrayObj->_Enabled &= ~_NEW_ARRAY_ATTRIB(index);
286 ctx->Array.NewState |= _NEW_ARRAY_ATTRIB(index);
287 }
288
289
290 void GLAPIENTRY
291 _mesa_GetVertexAttribdvARB(GLuint index, GLenum pname, GLdouble *params)
292 {
293 GLfloat fparams[4];
294 GET_CURRENT_CONTEXT(ctx);
295 ASSERT_OUTSIDE_BEGIN_END(ctx);
296
297 _mesa_GetVertexAttribfvARB(index, pname, fparams);
298 if (ctx->ErrorValue == GL_NO_ERROR) {
299 if (pname == GL_CURRENT_VERTEX_ATTRIB_ARB) {
300 COPY_4V(params, fparams);
301 }
302 else {
303 params[0] = fparams[0];
304 }
305 }
306 }
307
308
309 /**
310 * Return info for a generic vertex attribute array (no alias with
311 * legacy vertex attributes (pos, normal, color, etc)).
312 */
313 void GLAPIENTRY
314 _mesa_GetVertexAttribfvARB(GLuint index, GLenum pname, GLfloat *params)
315 {
316 const struct gl_client_array *array;
317 GET_CURRENT_CONTEXT(ctx);
318 ASSERT_OUTSIDE_BEGIN_END(ctx);
319
320 if (index >= MAX_VERTEX_GENERIC_ATTRIBS) {
321 _mesa_error(ctx, GL_INVALID_VALUE, "glGetVertexAttribfvARB(index)");
322 return;
323 }
324
325 ASSERT(index < Elements(ctx->Array.ArrayObj->VertexAttrib));
326
327 array = &ctx->Array.ArrayObj->VertexAttrib[index];
328
329 switch (pname) {
330 case GL_VERTEX_ATTRIB_ARRAY_ENABLED_ARB:
331 params[0] = (GLfloat) array->Enabled;
332 break;
333 case GL_VERTEX_ATTRIB_ARRAY_SIZE_ARB:
334 params[0] = (GLfloat) array->Size;
335 break;
336 case GL_VERTEX_ATTRIB_ARRAY_STRIDE_ARB:
337 params[0] = (GLfloat) array->Stride;
338 break;
339 case GL_VERTEX_ATTRIB_ARRAY_TYPE_ARB:
340 params[0] = (GLfloat) array->Type;
341 break;
342 case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED_ARB:
343 params[0] = array->Normalized;
344 break;
345 case GL_CURRENT_VERTEX_ATTRIB_ARB:
346 if (index == 0) {
347 _mesa_error(ctx, GL_INVALID_OPERATION,
348 "glGetVertexAttribfvARB(index==0)");
349 return;
350 }
351 FLUSH_CURRENT(ctx, 0);
352 COPY_4V(params, ctx->Current.Attrib[VERT_ATTRIB_GENERIC0 + index]);
353 break;
354 case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING_ARB:
355 params[0] = (GLfloat) array->BufferObj->Name;
356 break;
357 default:
358 _mesa_error(ctx, GL_INVALID_ENUM, "glGetVertexAttribfvARB(pname)");
359 return;
360 }
361 }
362
363
364 void GLAPIENTRY
365 _mesa_GetVertexAttribivARB(GLuint index, GLenum pname, GLint *params)
366 {
367 GLfloat fparams[4];
368 GET_CURRENT_CONTEXT(ctx);
369 ASSERT_OUTSIDE_BEGIN_END(ctx);
370
371 _mesa_GetVertexAttribfvARB(index, pname, fparams);
372 if (ctx->ErrorValue == GL_NO_ERROR) {
373 if (pname == GL_CURRENT_VERTEX_ATTRIB_ARB) {
374 COPY_4V_CAST(params, fparams, GLint); /* float to int */
375 }
376 else {
377 params[0] = (GLint) fparams[0];
378 }
379 }
380 }
381
382
383 void GLAPIENTRY
384 _mesa_GetVertexAttribPointervARB(GLuint index, GLenum pname, GLvoid **pointer)
385 {
386 GET_CURRENT_CONTEXT(ctx);
387 ASSERT_OUTSIDE_BEGIN_END(ctx);
388
389 if (index >= ctx->Const.VertexProgram.MaxAttribs) {
390 _mesa_error(ctx, GL_INVALID_VALUE, "glGetVertexAttribPointerARB(index)");
391 return;
392 }
393
394 if (pname != GL_VERTEX_ATTRIB_ARRAY_POINTER_ARB) {
395 _mesa_error(ctx, GL_INVALID_ENUM, "glGetVertexAttribPointerARB(pname)");
396 return;
397 }
398
399 ASSERT(index < Elements(ctx->Array.ArrayObj->VertexAttrib));
400
401 *pointer = (GLvoid *) ctx->Array.ArrayObj->VertexAttrib[index].Ptr;
402 }
403
404
405 /**
406 * Determine if id names a vertex or fragment program.
407 * \note Not compiled into display lists.
408 * \note Called from both glIsProgramNV and glIsProgramARB.
409 * \param id is the program identifier
410 * \return GL_TRUE if id is a program, else GL_FALSE.
411 */
412 GLboolean GLAPIENTRY
413 _mesa_IsProgramARB(GLuint id)
414 {
415 struct gl_program *prog = NULL;
416 GET_CURRENT_CONTEXT(ctx);
417 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
418
419 if (id == 0)
420 return GL_FALSE;
421
422 prog = _mesa_lookup_program(ctx, id);
423 if (prog && (prog != &_mesa_DummyProgram))
424 return GL_TRUE;
425 else
426 return GL_FALSE;
427 }
428
429
430 void GLAPIENTRY
431 _mesa_ProgramStringARB(GLenum target, GLenum format, GLsizei len,
432 const GLvoid *string)
433 {
434 struct gl_program *base;
435 GET_CURRENT_CONTEXT(ctx);
436 ASSERT_OUTSIDE_BEGIN_END(ctx);
437
438 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
439
440 if (!ctx->Extensions.ARB_vertex_program
441 && !ctx->Extensions.ARB_fragment_program) {
442 _mesa_error(ctx, GL_INVALID_OPERATION, "glProgramStringARB()");
443 return;
444 }
445
446 if (format != GL_PROGRAM_FORMAT_ASCII_ARB) {
447 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramStringARB(format)");
448 return;
449 }
450
451 /* The first couple cases are complicated. The same enum value is used for
452 * ARB and NV vertex programs. If the target is a vertex program, parse it
453 * using the ARB grammar if the string starts with "!!ARB" or if
454 * NV_vertex_program is not supported.
455 */
456 if (target == GL_VERTEX_PROGRAM_ARB
457 && ctx->Extensions.ARB_vertex_program
458 && ((strncmp(string, "!!ARB", 5) == 0)
459 || !ctx->Extensions.NV_vertex_program)) {
460 struct gl_vertex_program *prog = ctx->VertexProgram.Current;
461 _mesa_parse_arb_vertex_program(ctx, target, string, len, prog);
462
463 base = & prog->Base;
464 }
465 else if ((target == GL_VERTEX_PROGRAM_ARB
466 || target == GL_VERTEX_STATE_PROGRAM_NV)
467 && ctx->Extensions.NV_vertex_program) {
468 struct gl_vertex_program *prog = ctx->VertexProgram.Current;
469 _mesa_parse_nv_vertex_program(ctx, target, string, len, prog);
470
471 base = & prog->Base;
472 }
473 else if (target == GL_FRAGMENT_PROGRAM_ARB
474 && ctx->Extensions.ARB_fragment_program) {
475 struct gl_fragment_program *prog = ctx->FragmentProgram.Current;
476 _mesa_parse_arb_fragment_program(ctx, target, string, len, prog);
477
478 base = & prog->Base;
479 }
480 else if (target == GL_FRAGMENT_PROGRAM_NV
481 && ctx->Extensions.NV_fragment_program) {
482 struct gl_fragment_program *prog = ctx->FragmentProgram.Current;
483 _mesa_parse_nv_fragment_program(ctx, target, string, len, prog);
484
485 base = & prog->Base;
486 }
487 else {
488 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramStringARB(target)");
489 return;
490 }
491
492 if (ctx->Program.ErrorPos == -1 && ctx->Driver.ProgramStringNotify)
493 ctx->Driver.ProgramStringNotify( ctx, target, base );
494 }
495
496
497 /**
498 * Set a program env parameter register.
499 * \note Called from the GL API dispatcher.
500 * Note, this function is also used by the GL_NV_vertex_program extension
501 * (alias to ProgramParameterdNV)
502 */
503 void GLAPIENTRY
504 _mesa_ProgramEnvParameter4dARB(GLenum target, GLuint index,
505 GLdouble x, GLdouble y, GLdouble z, GLdouble w)
506 {
507 _mesa_ProgramEnvParameter4fARB(target, index, (GLfloat) x, (GLfloat) y,
508 (GLfloat) z, (GLfloat) w);
509 }
510
511
512 /**
513 * Set a program env parameter register.
514 * \note Called from the GL API dispatcher.
515 * Note, this function is also used by the GL_NV_vertex_program extension
516 * (alias to ProgramParameterdvNV)
517 */
518 void GLAPIENTRY
519 _mesa_ProgramEnvParameter4dvARB(GLenum target, GLuint index,
520 const GLdouble *params)
521 {
522 _mesa_ProgramEnvParameter4fARB(target, index, (GLfloat) params[0],
523 (GLfloat) params[1], (GLfloat) params[2],
524 (GLfloat) params[3]);
525 }
526
527
528 /**
529 * Set a program env parameter register.
530 * \note Called from the GL API dispatcher.
531 * Note, this function is also used by the GL_NV_vertex_program extension
532 * (alias to ProgramParameterfNV)
533 */
534 void GLAPIENTRY
535 _mesa_ProgramEnvParameter4fARB(GLenum target, GLuint index,
536 GLfloat x, GLfloat y, GLfloat z, GLfloat w)
537 {
538 GET_CURRENT_CONTEXT(ctx);
539 ASSERT_OUTSIDE_BEGIN_END(ctx);
540
541 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
542
543 if (target == GL_FRAGMENT_PROGRAM_ARB
544 && ctx->Extensions.ARB_fragment_program) {
545 if (index >= ctx->Const.FragmentProgram.MaxEnvParams) {
546 _mesa_error(ctx, GL_INVALID_VALUE, "glProgramEnvParameter(index)");
547 return;
548 }
549 ASSIGN_4V(ctx->FragmentProgram.Parameters[index], x, y, z, w);
550 }
551 else if (target == GL_VERTEX_PROGRAM_ARB /* == GL_VERTEX_PROGRAM_NV */
552 && (ctx->Extensions.ARB_vertex_program || ctx->Extensions.NV_vertex_program)) {
553 if (index >= ctx->Const.VertexProgram.MaxEnvParams) {
554 _mesa_error(ctx, GL_INVALID_VALUE, "glProgramEnvParameter(index)");
555 return;
556 }
557 ASSIGN_4V(ctx->VertexProgram.Parameters[index], x, y, z, w);
558 }
559 else {
560 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramEnvParameter(target)");
561 return;
562 }
563 }
564
565
566
567 /**
568 * Set a program env parameter register.
569 * \note Called from the GL API dispatcher.
570 * Note, this function is also used by the GL_NV_vertex_program extension
571 * (alias to ProgramParameterfvNV)
572 */
573 void GLAPIENTRY
574 _mesa_ProgramEnvParameter4fvARB(GLenum target, GLuint index,
575 const GLfloat *params)
576 {
577 GET_CURRENT_CONTEXT(ctx);
578 ASSERT_OUTSIDE_BEGIN_END(ctx);
579
580 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
581
582 if (target == GL_FRAGMENT_PROGRAM_ARB
583 && ctx->Extensions.ARB_fragment_program) {
584 if (index >= ctx->Const.FragmentProgram.MaxEnvParams) {
585 _mesa_error(ctx, GL_INVALID_VALUE, "glProgramEnvParameter4fv(index)");
586 return;
587 }
588 memcpy(ctx->FragmentProgram.Parameters[index], params,
589 4 * sizeof(GLfloat));
590 }
591 else if (target == GL_VERTEX_PROGRAM_ARB /* == GL_VERTEX_PROGRAM_NV */
592 && (ctx->Extensions.ARB_vertex_program || ctx->Extensions.NV_vertex_program)) {
593 if (index >= ctx->Const.VertexProgram.MaxEnvParams) {
594 _mesa_error(ctx, GL_INVALID_VALUE, "glProgramEnvParameter4fv(index)");
595 return;
596 }
597 memcpy(ctx->VertexProgram.Parameters[index], params,
598 4 * sizeof(GLfloat));
599 }
600 else {
601 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramEnvParameter4fv(target)");
602 return;
603 }
604 }
605
606
607 void GLAPIENTRY
608 _mesa_ProgramEnvParameters4fvEXT(GLenum target, GLuint index, GLsizei count,
609 const GLfloat *params)
610 {
611 GET_CURRENT_CONTEXT(ctx);
612 GLint i;
613 GLfloat * dest;
614 ASSERT_OUTSIDE_BEGIN_END(ctx);
615
616 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
617
618 if (count <= 0) {
619 _mesa_error(ctx, GL_INVALID_VALUE, "glProgramEnvParameters4fv(count)");
620 }
621
622 if (target == GL_FRAGMENT_PROGRAM_ARB
623 && ctx->Extensions.ARB_fragment_program) {
624 if ((index + count) > ctx->Const.FragmentProgram.MaxEnvParams) {
625 _mesa_error(ctx, GL_INVALID_VALUE, "glProgramEnvParameters4fv(index + count)");
626 return;
627 }
628 dest = ctx->FragmentProgram.Parameters[index];
629 }
630 else if (target == GL_VERTEX_PROGRAM_ARB
631 && ctx->Extensions.ARB_vertex_program) {
632 if ((index + count) > ctx->Const.VertexProgram.MaxEnvParams) {
633 _mesa_error(ctx, GL_INVALID_VALUE, "glProgramEnvParameters4fv(index + count)");
634 return;
635 }
636 dest = ctx->VertexProgram.Parameters[index];
637 }
638 else {
639 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramEnvParameters4fv(target)");
640 return;
641 }
642
643 for ( i = 0 ; i < count ; i++ ) {
644 COPY_4V(dest, params);
645 params += 4;
646 dest += 4;
647 }
648 }
649
650
651 void GLAPIENTRY
652 _mesa_GetProgramEnvParameterdvARB(GLenum target, GLuint index,
653 GLdouble *params)
654 {
655 GET_CURRENT_CONTEXT(ctx);
656 GLfloat fparams[4];
657
658 _mesa_GetProgramEnvParameterfvARB(target, index, fparams);
659 if (ctx->ErrorValue == GL_NO_ERROR) {
660 params[0] = fparams[0];
661 params[1] = fparams[1];
662 params[2] = fparams[2];
663 params[3] = fparams[3];
664 }
665 }
666
667
668 void GLAPIENTRY
669 _mesa_GetProgramEnvParameterfvARB(GLenum target, GLuint index,
670 GLfloat *params)
671 {
672 GET_CURRENT_CONTEXT(ctx);
673
674 ASSERT_OUTSIDE_BEGIN_END(ctx);
675
676 if (target == GL_FRAGMENT_PROGRAM_ARB
677 && ctx->Extensions.ARB_fragment_program) {
678 if (index >= ctx->Const.FragmentProgram.MaxEnvParams) {
679 _mesa_error(ctx, GL_INVALID_VALUE, "glGetProgramEnvParameter(index)");
680 return;
681 }
682 COPY_4V(params, ctx->FragmentProgram.Parameters[index]);
683 }
684 else if (target == GL_VERTEX_PROGRAM_ARB
685 && ctx->Extensions.ARB_vertex_program) {
686 if (index >= ctx->Const.VertexProgram.MaxEnvParams) {
687 _mesa_error(ctx, GL_INVALID_VALUE, "glGetProgramEnvParameter(index)");
688 return;
689 }
690 COPY_4V(params, ctx->VertexProgram.Parameters[index]);
691 }
692 else {
693 _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramEnvParameter(target)");
694 return;
695 }
696 }
697
698
699 /**
700 * Note, this function is also used by the GL_NV_fragment_program extension.
701 */
702 void GLAPIENTRY
703 _mesa_ProgramLocalParameter4fARB(GLenum target, GLuint index,
704 GLfloat x, GLfloat y, GLfloat z, GLfloat w)
705 {
706 GET_CURRENT_CONTEXT(ctx);
707 struct gl_program *prog;
708 ASSERT_OUTSIDE_BEGIN_END(ctx);
709
710 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
711
712 if ((target == GL_FRAGMENT_PROGRAM_NV
713 && ctx->Extensions.NV_fragment_program) ||
714 (target == GL_FRAGMENT_PROGRAM_ARB
715 && ctx->Extensions.ARB_fragment_program)) {
716 if (index >= ctx->Const.FragmentProgram.MaxLocalParams) {
717 _mesa_error(ctx, GL_INVALID_VALUE, "glProgramLocalParameterARB");
718 return;
719 }
720 prog = &(ctx->FragmentProgram.Current->Base);
721 }
722 else if (target == GL_VERTEX_PROGRAM_ARB
723 && ctx->Extensions.ARB_vertex_program) {
724 if (index >= ctx->Const.VertexProgram.MaxLocalParams) {
725 _mesa_error(ctx, GL_INVALID_VALUE, "glProgramLocalParameterARB");
726 return;
727 }
728 prog = &(ctx->VertexProgram.Current->Base);
729 }
730 else {
731 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramLocalParameterARB");
732 return;
733 }
734
735 ASSERT(index < MAX_PROGRAM_LOCAL_PARAMS);
736 prog->LocalParams[index][0] = x;
737 prog->LocalParams[index][1] = y;
738 prog->LocalParams[index][2] = z;
739 prog->LocalParams[index][3] = w;
740 }
741
742
743 /**
744 * Note, this function is also used by the GL_NV_fragment_program extension.
745 */
746 void GLAPIENTRY
747 _mesa_ProgramLocalParameter4fvARB(GLenum target, GLuint index,
748 const GLfloat *params)
749 {
750 _mesa_ProgramLocalParameter4fARB(target, index, params[0], params[1],
751 params[2], params[3]);
752 }
753
754
755 void GLAPIENTRY
756 _mesa_ProgramLocalParameters4fvEXT(GLenum target, GLuint index, GLsizei count,
757 const GLfloat *params)
758 {
759 GET_CURRENT_CONTEXT(ctx);
760 struct gl_program *prog;
761 GLint i;
762 ASSERT_OUTSIDE_BEGIN_END(ctx);
763
764 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
765
766 if (count <= 0) {
767 _mesa_error(ctx, GL_INVALID_VALUE, "glProgramLocalParameters4fv(count)");
768 }
769
770 if (target == GL_FRAGMENT_PROGRAM_ARB
771 && ctx->Extensions.ARB_fragment_program) {
772 if ((index + count) > ctx->Const.FragmentProgram.MaxLocalParams) {
773 _mesa_error(ctx, GL_INVALID_VALUE, "glProgramLocalParameters4fvEXT(index + count)");
774 return;
775 }
776 prog = &(ctx->FragmentProgram.Current->Base);
777 }
778 else if (target == GL_VERTEX_PROGRAM_ARB
779 && ctx->Extensions.ARB_vertex_program) {
780 if ((index + count) > ctx->Const.VertexProgram.MaxLocalParams) {
781 _mesa_error(ctx, GL_INVALID_VALUE, "glProgramLocalParameters4fvEXT(index + count)");
782 return;
783 }
784 prog = &(ctx->VertexProgram.Current->Base);
785 }
786 else {
787 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramLocalParameters4fvEXT(target)");
788 return;
789 }
790
791 for (i = 0; i < count; i++) {
792 ASSERT((index + i) < MAX_PROGRAM_LOCAL_PARAMS);
793 COPY_4V(prog->LocalParams[index + i], params);
794 params += 4;
795 }
796 }
797
798
799 /**
800 * Note, this function is also used by the GL_NV_fragment_program extension.
801 */
802 void GLAPIENTRY
803 _mesa_ProgramLocalParameter4dARB(GLenum target, GLuint index,
804 GLdouble x, GLdouble y,
805 GLdouble z, GLdouble w)
806 {
807 _mesa_ProgramLocalParameter4fARB(target, index, (GLfloat) x, (GLfloat) y,
808 (GLfloat) z, (GLfloat) w);
809 }
810
811
812 /**
813 * Note, this function is also used by the GL_NV_fragment_program extension.
814 */
815 void GLAPIENTRY
816 _mesa_ProgramLocalParameter4dvARB(GLenum target, GLuint index,
817 const GLdouble *params)
818 {
819 _mesa_ProgramLocalParameter4fARB(target, index,
820 (GLfloat) params[0], (GLfloat) params[1],
821 (GLfloat) params[2], (GLfloat) params[3]);
822 }
823
824
825 /**
826 * Note, this function is also used by the GL_NV_fragment_program extension.
827 */
828 void GLAPIENTRY
829 _mesa_GetProgramLocalParameterfvARB(GLenum target, GLuint index,
830 GLfloat *params)
831 {
832 const struct gl_program *prog;
833 GLuint maxParams;
834 GET_CURRENT_CONTEXT(ctx);
835 ASSERT_OUTSIDE_BEGIN_END(ctx);
836
837 if (target == GL_VERTEX_PROGRAM_ARB
838 && ctx->Extensions.ARB_vertex_program) {
839 prog = &(ctx->VertexProgram.Current->Base);
840 maxParams = ctx->Const.VertexProgram.MaxLocalParams;
841 }
842 else if (target == GL_FRAGMENT_PROGRAM_ARB
843 && ctx->Extensions.ARB_fragment_program) {
844 prog = &(ctx->FragmentProgram.Current->Base);
845 maxParams = ctx->Const.FragmentProgram.MaxLocalParams;
846 }
847 else if (target == GL_FRAGMENT_PROGRAM_NV
848 && ctx->Extensions.NV_fragment_program) {
849 prog = &(ctx->FragmentProgram.Current->Base);
850 maxParams = MAX_NV_FRAGMENT_PROGRAM_PARAMS;
851 }
852 else {
853 _mesa_error(ctx, GL_INVALID_ENUM,
854 "glGetProgramLocalParameterARB(target)");
855 return;
856 }
857
858 if (index >= maxParams) {
859 _mesa_error(ctx, GL_INVALID_VALUE,
860 "glGetProgramLocalParameterARB(index)");
861 return;
862 }
863
864 ASSERT(prog);
865 ASSERT(index < MAX_PROGRAM_LOCAL_PARAMS);
866 COPY_4V(params, prog->LocalParams[index]);
867 }
868
869
870 /**
871 * Note, this function is also used by the GL_NV_fragment_program extension.
872 */
873 void GLAPIENTRY
874 _mesa_GetProgramLocalParameterdvARB(GLenum target, GLuint index,
875 GLdouble *params)
876 {
877 GET_CURRENT_CONTEXT(ctx);
878 GLfloat floatParams[4];
879 ASSIGN_4V(floatParams, 0.0F, 0.0F, 0.0F, 0.0F);
880 _mesa_GetProgramLocalParameterfvARB(target, index, floatParams);
881 if (ctx->ErrorValue == GL_NO_ERROR) {
882 COPY_4V(params, floatParams);
883 }
884 }
885
886
887 void GLAPIENTRY
888 _mesa_GetProgramivARB(GLenum target, GLenum pname, GLint *params)
889 {
890 const struct gl_program_constants *limits;
891 struct gl_program *prog;
892 GET_CURRENT_CONTEXT(ctx);
893
894 ASSERT_OUTSIDE_BEGIN_END(ctx);
895
896 if (target == GL_VERTEX_PROGRAM_ARB
897 && ctx->Extensions.ARB_vertex_program) {
898 prog = &(ctx->VertexProgram.Current->Base);
899 limits = &ctx->Const.VertexProgram;
900 }
901 else if (target == GL_FRAGMENT_PROGRAM_ARB
902 && ctx->Extensions.ARB_fragment_program) {
903 prog = &(ctx->FragmentProgram.Current->Base);
904 limits = &ctx->Const.FragmentProgram;
905 }
906 else {
907 _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramivARB(target)");
908 return;
909 }
910
911 ASSERT(prog);
912 ASSERT(limits);
913
914 /* Queries supported for both vertex and fragment programs */
915 switch (pname) {
916 case GL_PROGRAM_LENGTH_ARB:
917 *params
918 = prog->String ? (GLint) _mesa_strlen((char *) prog->String) : 0;
919 return;
920 case GL_PROGRAM_FORMAT_ARB:
921 *params = prog->Format;
922 return;
923 case GL_PROGRAM_BINDING_ARB:
924 *params = prog->Id;
925 return;
926 case GL_PROGRAM_INSTRUCTIONS_ARB:
927 *params = prog->NumInstructions;
928 return;
929 case GL_MAX_PROGRAM_INSTRUCTIONS_ARB:
930 *params = limits->MaxInstructions;
931 return;
932 case GL_PROGRAM_NATIVE_INSTRUCTIONS_ARB:
933 *params = prog->NumNativeInstructions;
934 return;
935 case GL_MAX_PROGRAM_NATIVE_INSTRUCTIONS_ARB:
936 *params = limits->MaxNativeInstructions;
937 return;
938 case GL_PROGRAM_TEMPORARIES_ARB:
939 *params = prog->NumTemporaries;
940 return;
941 case GL_MAX_PROGRAM_TEMPORARIES_ARB:
942 *params = limits->MaxTemps;
943 return;
944 case GL_PROGRAM_NATIVE_TEMPORARIES_ARB:
945 *params = prog->NumNativeTemporaries;
946 return;
947 case GL_MAX_PROGRAM_NATIVE_TEMPORARIES_ARB:
948 *params = limits->MaxNativeTemps;
949 return;
950 case GL_PROGRAM_PARAMETERS_ARB:
951 *params = prog->NumParameters;
952 return;
953 case GL_MAX_PROGRAM_PARAMETERS_ARB:
954 *params = limits->MaxParameters;
955 return;
956 case GL_PROGRAM_NATIVE_PARAMETERS_ARB:
957 *params = prog->NumNativeParameters;
958 return;
959 case GL_MAX_PROGRAM_NATIVE_PARAMETERS_ARB:
960 *params = limits->MaxNativeParameters;
961 return;
962 case GL_PROGRAM_ATTRIBS_ARB:
963 *params = prog->NumAttributes;
964 return;
965 case GL_MAX_PROGRAM_ATTRIBS_ARB:
966 *params = limits->MaxAttribs;
967 return;
968 case GL_PROGRAM_NATIVE_ATTRIBS_ARB:
969 *params = prog->NumNativeAttributes;
970 return;
971 case GL_MAX_PROGRAM_NATIVE_ATTRIBS_ARB:
972 *params = limits->MaxNativeAttribs;
973 return;
974 case GL_PROGRAM_ADDRESS_REGISTERS_ARB:
975 *params = prog->NumAddressRegs;
976 return;
977 case GL_MAX_PROGRAM_ADDRESS_REGISTERS_ARB:
978 *params = limits->MaxAddressRegs;
979 return;
980 case GL_PROGRAM_NATIVE_ADDRESS_REGISTERS_ARB:
981 *params = prog->NumNativeAddressRegs;
982 return;
983 case GL_MAX_PROGRAM_NATIVE_ADDRESS_REGISTERS_ARB:
984 *params = limits->MaxNativeAddressRegs;
985 return;
986 case GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB:
987 *params = limits->MaxLocalParams;
988 return;
989 case GL_MAX_PROGRAM_ENV_PARAMETERS_ARB:
990 *params = limits->MaxEnvParams;
991 return;
992 case GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB:
993 /*
994 * XXX we may not really need a driver callback here.
995 * If the number of native instructions, registers, etc. used
996 * are all below the maximums, we could return true.
997 * The spec says that even if this query returns true, there's
998 * no guarantee that the program will run in hardware.
999 */
1000 if (prog->Id == 0) {
1001 /* default/null program */
1002 *params = GL_FALSE;
1003 }
1004 else if (ctx->Driver.IsProgramNative) {
1005 /* ask the driver */
1006 *params = ctx->Driver.IsProgramNative( ctx, target, prog );
1007 }
1008 else {
1009 /* probably running in software */
1010 *params = GL_TRUE;
1011 }
1012 return;
1013 default:
1014 /* continue with fragment-program only queries below */
1015 break;
1016 }
1017
1018 /*
1019 * The following apply to fragment programs only (at this time)
1020 */
1021 if (target == GL_FRAGMENT_PROGRAM_ARB) {
1022 const struct gl_fragment_program *fp = ctx->FragmentProgram.Current;
1023 switch (pname) {
1024 case GL_PROGRAM_ALU_INSTRUCTIONS_ARB:
1025 *params = fp->Base.NumNativeAluInstructions;
1026 return;
1027 case GL_PROGRAM_NATIVE_ALU_INSTRUCTIONS_ARB:
1028 *params = fp->Base.NumAluInstructions;
1029 return;
1030 case GL_PROGRAM_TEX_INSTRUCTIONS_ARB:
1031 *params = fp->Base.NumTexInstructions;
1032 return;
1033 case GL_PROGRAM_NATIVE_TEX_INSTRUCTIONS_ARB:
1034 *params = fp->Base.NumNativeTexInstructions;
1035 return;
1036 case GL_PROGRAM_TEX_INDIRECTIONS_ARB:
1037 *params = fp->Base.NumTexIndirections;
1038 return;
1039 case GL_PROGRAM_NATIVE_TEX_INDIRECTIONS_ARB:
1040 *params = fp->Base.NumNativeTexIndirections;
1041 return;
1042 case GL_MAX_PROGRAM_ALU_INSTRUCTIONS_ARB:
1043 *params = limits->MaxAluInstructions;
1044 return;
1045 case GL_MAX_PROGRAM_NATIVE_ALU_INSTRUCTIONS_ARB:
1046 *params = limits->MaxNativeAluInstructions;
1047 return;
1048 case GL_MAX_PROGRAM_TEX_INSTRUCTIONS_ARB:
1049 *params = limits->MaxTexInstructions;
1050 return;
1051 case GL_MAX_PROGRAM_NATIVE_TEX_INSTRUCTIONS_ARB:
1052 *params = limits->MaxNativeTexInstructions;
1053 return;
1054 case GL_MAX_PROGRAM_TEX_INDIRECTIONS_ARB:
1055 *params = limits->MaxTexIndirections;
1056 return;
1057 case GL_MAX_PROGRAM_NATIVE_TEX_INDIRECTIONS_ARB:
1058 *params = limits->MaxNativeTexIndirections;
1059 return;
1060 default:
1061 _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramivARB(pname)");
1062 return;
1063 }
1064 } else {
1065 _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramivARB(pname)");
1066 return;
1067 }
1068 }
1069
1070
1071 void GLAPIENTRY
1072 _mesa_GetProgramStringARB(GLenum target, GLenum pname, GLvoid *string)
1073 {
1074 const struct gl_program *prog;
1075 char *dst = (char *) string;
1076 GET_CURRENT_CONTEXT(ctx);
1077
1078 ASSERT_OUTSIDE_BEGIN_END(ctx);
1079
1080 if (target == GL_VERTEX_PROGRAM_ARB) {
1081 prog = &(ctx->VertexProgram.Current->Base);
1082 }
1083 else if (target == GL_FRAGMENT_PROGRAM_ARB) {
1084 prog = &(ctx->FragmentProgram.Current->Base);
1085 }
1086 else {
1087 _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramStringARB(target)");
1088 return;
1089 }
1090
1091 ASSERT(prog);
1092
1093 if (pname != GL_PROGRAM_STRING_ARB) {
1094 _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramStringARB(pname)");
1095 return;
1096 }
1097
1098 if (prog->String)
1099 _mesa_memcpy(dst, prog->String, _mesa_strlen((char *) prog->String));
1100 else
1101 *dst = '\0';
1102 }