mesa: debug output for ARL
[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) {
493 /* finally, give the program to the driver for translation/checking */
494 if (!ctx->Driver.ProgramStringNotify(ctx, target, base)) {
495 _mesa_error(ctx, GL_INVALID_OPERATION,
496 "glProgramStringARB(rejected by driver");
497 }
498 }
499 }
500
501
502 /**
503 * Set a program env parameter register.
504 * \note Called from the GL API dispatcher.
505 * Note, this function is also used by the GL_NV_vertex_program extension
506 * (alias to ProgramParameterdNV)
507 */
508 void GLAPIENTRY
509 _mesa_ProgramEnvParameter4dARB(GLenum target, GLuint index,
510 GLdouble x, GLdouble y, GLdouble z, GLdouble w)
511 {
512 _mesa_ProgramEnvParameter4fARB(target, index, (GLfloat) x, (GLfloat) y,
513 (GLfloat) z, (GLfloat) w);
514 }
515
516
517 /**
518 * Set a program env parameter register.
519 * \note Called from the GL API dispatcher.
520 * Note, this function is also used by the GL_NV_vertex_program extension
521 * (alias to ProgramParameterdvNV)
522 */
523 void GLAPIENTRY
524 _mesa_ProgramEnvParameter4dvARB(GLenum target, GLuint index,
525 const GLdouble *params)
526 {
527 _mesa_ProgramEnvParameter4fARB(target, index, (GLfloat) params[0],
528 (GLfloat) params[1], (GLfloat) params[2],
529 (GLfloat) params[3]);
530 }
531
532
533 /**
534 * Set a program env parameter register.
535 * \note Called from the GL API dispatcher.
536 * Note, this function is also used by the GL_NV_vertex_program extension
537 * (alias to ProgramParameterfNV)
538 */
539 void GLAPIENTRY
540 _mesa_ProgramEnvParameter4fARB(GLenum target, GLuint index,
541 GLfloat x, GLfloat y, GLfloat z, GLfloat w)
542 {
543 GET_CURRENT_CONTEXT(ctx);
544 ASSERT_OUTSIDE_BEGIN_END(ctx);
545
546 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
547
548 if (target == GL_FRAGMENT_PROGRAM_ARB
549 && ctx->Extensions.ARB_fragment_program) {
550 if (index >= ctx->Const.FragmentProgram.MaxEnvParams) {
551 _mesa_error(ctx, GL_INVALID_VALUE, "glProgramEnvParameter(index)");
552 return;
553 }
554 ASSIGN_4V(ctx->FragmentProgram.Parameters[index], x, y, z, w);
555 }
556 else if (target == GL_VERTEX_PROGRAM_ARB /* == GL_VERTEX_PROGRAM_NV */
557 && (ctx->Extensions.ARB_vertex_program || ctx->Extensions.NV_vertex_program)) {
558 if (index >= ctx->Const.VertexProgram.MaxEnvParams) {
559 _mesa_error(ctx, GL_INVALID_VALUE, "glProgramEnvParameter(index)");
560 return;
561 }
562 ASSIGN_4V(ctx->VertexProgram.Parameters[index], x, y, z, w);
563 }
564 else {
565 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramEnvParameter(target)");
566 return;
567 }
568 }
569
570
571
572 /**
573 * Set a program env parameter register.
574 * \note Called from the GL API dispatcher.
575 * Note, this function is also used by the GL_NV_vertex_program extension
576 * (alias to ProgramParameterfvNV)
577 */
578 void GLAPIENTRY
579 _mesa_ProgramEnvParameter4fvARB(GLenum target, GLuint index,
580 const GLfloat *params)
581 {
582 GET_CURRENT_CONTEXT(ctx);
583 ASSERT_OUTSIDE_BEGIN_END(ctx);
584
585 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
586
587 if (target == GL_FRAGMENT_PROGRAM_ARB
588 && ctx->Extensions.ARB_fragment_program) {
589 if (index >= ctx->Const.FragmentProgram.MaxEnvParams) {
590 _mesa_error(ctx, GL_INVALID_VALUE, "glProgramEnvParameter4fv(index)");
591 return;
592 }
593 memcpy(ctx->FragmentProgram.Parameters[index], params,
594 4 * sizeof(GLfloat));
595 }
596 else if (target == GL_VERTEX_PROGRAM_ARB /* == GL_VERTEX_PROGRAM_NV */
597 && (ctx->Extensions.ARB_vertex_program || ctx->Extensions.NV_vertex_program)) {
598 if (index >= ctx->Const.VertexProgram.MaxEnvParams) {
599 _mesa_error(ctx, GL_INVALID_VALUE, "glProgramEnvParameter4fv(index)");
600 return;
601 }
602 memcpy(ctx->VertexProgram.Parameters[index], params,
603 4 * sizeof(GLfloat));
604 }
605 else {
606 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramEnvParameter4fv(target)");
607 return;
608 }
609 }
610
611
612 void GLAPIENTRY
613 _mesa_ProgramEnvParameters4fvEXT(GLenum target, GLuint index, GLsizei count,
614 const GLfloat *params)
615 {
616 GET_CURRENT_CONTEXT(ctx);
617 GLfloat * dest;
618 ASSERT_OUTSIDE_BEGIN_END(ctx);
619
620 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
621
622 if (count <= 0) {
623 _mesa_error(ctx, GL_INVALID_VALUE, "glProgramEnvParameters4fv(count)");
624 }
625
626 if (target == GL_FRAGMENT_PROGRAM_ARB
627 && ctx->Extensions.ARB_fragment_program) {
628 if ((index + count) > ctx->Const.FragmentProgram.MaxEnvParams) {
629 _mesa_error(ctx, GL_INVALID_VALUE, "glProgramEnvParameters4fv(index + count)");
630 return;
631 }
632 dest = ctx->FragmentProgram.Parameters[index];
633 }
634 else if (target == GL_VERTEX_PROGRAM_ARB
635 && ctx->Extensions.ARB_vertex_program) {
636 if ((index + count) > ctx->Const.VertexProgram.MaxEnvParams) {
637 _mesa_error(ctx, GL_INVALID_VALUE, "glProgramEnvParameters4fv(index + count)");
638 return;
639 }
640 dest = ctx->VertexProgram.Parameters[index];
641 }
642 else {
643 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramEnvParameters4fv(target)");
644 return;
645 }
646
647 memcpy(dest, params, count * 4 * sizeof(GLfloat));
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 GLfloat *dest;
761 ASSERT_OUTSIDE_BEGIN_END(ctx);
762
763 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
764
765 if (count <= 0) {
766 _mesa_error(ctx, GL_INVALID_VALUE, "glProgramLocalParameters4fv(count)");
767 }
768
769 if (target == GL_FRAGMENT_PROGRAM_ARB
770 && ctx->Extensions.ARB_fragment_program) {
771 if ((index + count) > ctx->Const.FragmentProgram.MaxLocalParams) {
772 _mesa_error(ctx, GL_INVALID_VALUE, "glProgramLocalParameters4fvEXT(index + count)");
773 return;
774 }
775 dest = ctx->FragmentProgram.Current->Base.LocalParams[index];
776 }
777 else if (target == GL_VERTEX_PROGRAM_ARB
778 && ctx->Extensions.ARB_vertex_program) {
779 if ((index + count) > ctx->Const.VertexProgram.MaxLocalParams) {
780 _mesa_error(ctx, GL_INVALID_VALUE, "glProgramLocalParameters4fvEXT(index + count)");
781 return;
782 }
783 dest = ctx->VertexProgram.Current->Base.LocalParams[index];
784 }
785 else {
786 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramLocalParameters4fvEXT(target)");
787 return;
788 }
789
790 memcpy(dest, params, count * 4 * sizeof(GLfloat));
791 }
792
793
794 /**
795 * Note, this function is also used by the GL_NV_fragment_program extension.
796 */
797 void GLAPIENTRY
798 _mesa_ProgramLocalParameter4dARB(GLenum target, GLuint index,
799 GLdouble x, GLdouble y,
800 GLdouble z, GLdouble w)
801 {
802 _mesa_ProgramLocalParameter4fARB(target, index, (GLfloat) x, (GLfloat) y,
803 (GLfloat) z, (GLfloat) w);
804 }
805
806
807 /**
808 * Note, this function is also used by the GL_NV_fragment_program extension.
809 */
810 void GLAPIENTRY
811 _mesa_ProgramLocalParameter4dvARB(GLenum target, GLuint index,
812 const GLdouble *params)
813 {
814 _mesa_ProgramLocalParameter4fARB(target, index,
815 (GLfloat) params[0], (GLfloat) params[1],
816 (GLfloat) params[2], (GLfloat) params[3]);
817 }
818
819
820 /**
821 * Note, this function is also used by the GL_NV_fragment_program extension.
822 */
823 void GLAPIENTRY
824 _mesa_GetProgramLocalParameterfvARB(GLenum target, GLuint index,
825 GLfloat *params)
826 {
827 const struct gl_program *prog;
828 GLuint maxParams;
829 GET_CURRENT_CONTEXT(ctx);
830 ASSERT_OUTSIDE_BEGIN_END(ctx);
831
832 if (target == GL_VERTEX_PROGRAM_ARB
833 && ctx->Extensions.ARB_vertex_program) {
834 prog = &(ctx->VertexProgram.Current->Base);
835 maxParams = ctx->Const.VertexProgram.MaxLocalParams;
836 }
837 else if (target == GL_FRAGMENT_PROGRAM_ARB
838 && ctx->Extensions.ARB_fragment_program) {
839 prog = &(ctx->FragmentProgram.Current->Base);
840 maxParams = ctx->Const.FragmentProgram.MaxLocalParams;
841 }
842 else if (target == GL_FRAGMENT_PROGRAM_NV
843 && ctx->Extensions.NV_fragment_program) {
844 prog = &(ctx->FragmentProgram.Current->Base);
845 maxParams = MAX_NV_FRAGMENT_PROGRAM_PARAMS;
846 }
847 else {
848 _mesa_error(ctx, GL_INVALID_ENUM,
849 "glGetProgramLocalParameterARB(target)");
850 return;
851 }
852
853 if (index >= maxParams) {
854 _mesa_error(ctx, GL_INVALID_VALUE,
855 "glGetProgramLocalParameterARB(index)");
856 return;
857 }
858
859 ASSERT(prog);
860 ASSERT(index < MAX_PROGRAM_LOCAL_PARAMS);
861 COPY_4V(params, prog->LocalParams[index]);
862 }
863
864
865 /**
866 * Note, this function is also used by the GL_NV_fragment_program extension.
867 */
868 void GLAPIENTRY
869 _mesa_GetProgramLocalParameterdvARB(GLenum target, GLuint index,
870 GLdouble *params)
871 {
872 GET_CURRENT_CONTEXT(ctx);
873 GLfloat floatParams[4];
874 ASSIGN_4V(floatParams, 0.0F, 0.0F, 0.0F, 0.0F);
875 _mesa_GetProgramLocalParameterfvARB(target, index, floatParams);
876 if (ctx->ErrorValue == GL_NO_ERROR) {
877 COPY_4V(params, floatParams);
878 }
879 }
880
881
882 void GLAPIENTRY
883 _mesa_GetProgramivARB(GLenum target, GLenum pname, GLint *params)
884 {
885 const struct gl_program_constants *limits;
886 struct gl_program *prog;
887 GET_CURRENT_CONTEXT(ctx);
888
889 ASSERT_OUTSIDE_BEGIN_END(ctx);
890
891 if (target == GL_VERTEX_PROGRAM_ARB
892 && ctx->Extensions.ARB_vertex_program) {
893 prog = &(ctx->VertexProgram.Current->Base);
894 limits = &ctx->Const.VertexProgram;
895 }
896 else if (target == GL_FRAGMENT_PROGRAM_ARB
897 && ctx->Extensions.ARB_fragment_program) {
898 prog = &(ctx->FragmentProgram.Current->Base);
899 limits = &ctx->Const.FragmentProgram;
900 }
901 else {
902 _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramivARB(target)");
903 return;
904 }
905
906 ASSERT(prog);
907 ASSERT(limits);
908
909 /* Queries supported for both vertex and fragment programs */
910 switch (pname) {
911 case GL_PROGRAM_LENGTH_ARB:
912 *params
913 = prog->String ? (GLint) _mesa_strlen((char *) prog->String) : 0;
914 return;
915 case GL_PROGRAM_FORMAT_ARB:
916 *params = prog->Format;
917 return;
918 case GL_PROGRAM_BINDING_ARB:
919 *params = prog->Id;
920 return;
921 case GL_PROGRAM_INSTRUCTIONS_ARB:
922 *params = prog->NumInstructions;
923 return;
924 case GL_MAX_PROGRAM_INSTRUCTIONS_ARB:
925 *params = limits->MaxInstructions;
926 return;
927 case GL_PROGRAM_NATIVE_INSTRUCTIONS_ARB:
928 *params = prog->NumNativeInstructions;
929 return;
930 case GL_MAX_PROGRAM_NATIVE_INSTRUCTIONS_ARB:
931 *params = limits->MaxNativeInstructions;
932 return;
933 case GL_PROGRAM_TEMPORARIES_ARB:
934 *params = prog->NumTemporaries;
935 return;
936 case GL_MAX_PROGRAM_TEMPORARIES_ARB:
937 *params = limits->MaxTemps;
938 return;
939 case GL_PROGRAM_NATIVE_TEMPORARIES_ARB:
940 *params = prog->NumNativeTemporaries;
941 return;
942 case GL_MAX_PROGRAM_NATIVE_TEMPORARIES_ARB:
943 *params = limits->MaxNativeTemps;
944 return;
945 case GL_PROGRAM_PARAMETERS_ARB:
946 *params = prog->NumParameters;
947 return;
948 case GL_MAX_PROGRAM_PARAMETERS_ARB:
949 *params = limits->MaxParameters;
950 return;
951 case GL_PROGRAM_NATIVE_PARAMETERS_ARB:
952 *params = prog->NumNativeParameters;
953 return;
954 case GL_MAX_PROGRAM_NATIVE_PARAMETERS_ARB:
955 *params = limits->MaxNativeParameters;
956 return;
957 case GL_PROGRAM_ATTRIBS_ARB:
958 *params = prog->NumAttributes;
959 return;
960 case GL_MAX_PROGRAM_ATTRIBS_ARB:
961 *params = limits->MaxAttribs;
962 return;
963 case GL_PROGRAM_NATIVE_ATTRIBS_ARB:
964 *params = prog->NumNativeAttributes;
965 return;
966 case GL_MAX_PROGRAM_NATIVE_ATTRIBS_ARB:
967 *params = limits->MaxNativeAttribs;
968 return;
969 case GL_PROGRAM_ADDRESS_REGISTERS_ARB:
970 *params = prog->NumAddressRegs;
971 return;
972 case GL_MAX_PROGRAM_ADDRESS_REGISTERS_ARB:
973 *params = limits->MaxAddressRegs;
974 return;
975 case GL_PROGRAM_NATIVE_ADDRESS_REGISTERS_ARB:
976 *params = prog->NumNativeAddressRegs;
977 return;
978 case GL_MAX_PROGRAM_NATIVE_ADDRESS_REGISTERS_ARB:
979 *params = limits->MaxNativeAddressRegs;
980 return;
981 case GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB:
982 *params = limits->MaxLocalParams;
983 return;
984 case GL_MAX_PROGRAM_ENV_PARAMETERS_ARB:
985 *params = limits->MaxEnvParams;
986 return;
987 case GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB:
988 /*
989 * XXX we may not really need a driver callback here.
990 * If the number of native instructions, registers, etc. used
991 * are all below the maximums, we could return true.
992 * The spec says that even if this query returns true, there's
993 * no guarantee that the program will run in hardware.
994 */
995 if (prog->Id == 0) {
996 /* default/null program */
997 *params = GL_FALSE;
998 }
999 else if (ctx->Driver.IsProgramNative) {
1000 /* ask the driver */
1001 *params = ctx->Driver.IsProgramNative( ctx, target, prog );
1002 }
1003 else {
1004 /* probably running in software */
1005 *params = GL_TRUE;
1006 }
1007 return;
1008 default:
1009 /* continue with fragment-program only queries below */
1010 break;
1011 }
1012
1013 /*
1014 * The following apply to fragment programs only (at this time)
1015 */
1016 if (target == GL_FRAGMENT_PROGRAM_ARB) {
1017 const struct gl_fragment_program *fp = ctx->FragmentProgram.Current;
1018 switch (pname) {
1019 case GL_PROGRAM_ALU_INSTRUCTIONS_ARB:
1020 *params = fp->Base.NumNativeAluInstructions;
1021 return;
1022 case GL_PROGRAM_NATIVE_ALU_INSTRUCTIONS_ARB:
1023 *params = fp->Base.NumAluInstructions;
1024 return;
1025 case GL_PROGRAM_TEX_INSTRUCTIONS_ARB:
1026 *params = fp->Base.NumTexInstructions;
1027 return;
1028 case GL_PROGRAM_NATIVE_TEX_INSTRUCTIONS_ARB:
1029 *params = fp->Base.NumNativeTexInstructions;
1030 return;
1031 case GL_PROGRAM_TEX_INDIRECTIONS_ARB:
1032 *params = fp->Base.NumTexIndirections;
1033 return;
1034 case GL_PROGRAM_NATIVE_TEX_INDIRECTIONS_ARB:
1035 *params = fp->Base.NumNativeTexIndirections;
1036 return;
1037 case GL_MAX_PROGRAM_ALU_INSTRUCTIONS_ARB:
1038 *params = limits->MaxAluInstructions;
1039 return;
1040 case GL_MAX_PROGRAM_NATIVE_ALU_INSTRUCTIONS_ARB:
1041 *params = limits->MaxNativeAluInstructions;
1042 return;
1043 case GL_MAX_PROGRAM_TEX_INSTRUCTIONS_ARB:
1044 *params = limits->MaxTexInstructions;
1045 return;
1046 case GL_MAX_PROGRAM_NATIVE_TEX_INSTRUCTIONS_ARB:
1047 *params = limits->MaxNativeTexInstructions;
1048 return;
1049 case GL_MAX_PROGRAM_TEX_INDIRECTIONS_ARB:
1050 *params = limits->MaxTexIndirections;
1051 return;
1052 case GL_MAX_PROGRAM_NATIVE_TEX_INDIRECTIONS_ARB:
1053 *params = limits->MaxNativeTexIndirections;
1054 return;
1055 default:
1056 _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramivARB(pname)");
1057 return;
1058 }
1059 } else {
1060 _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramivARB(pname)");
1061 return;
1062 }
1063 }
1064
1065
1066 void GLAPIENTRY
1067 _mesa_GetProgramStringARB(GLenum target, GLenum pname, GLvoid *string)
1068 {
1069 const struct gl_program *prog;
1070 char *dst = (char *) string;
1071 GET_CURRENT_CONTEXT(ctx);
1072
1073 ASSERT_OUTSIDE_BEGIN_END(ctx);
1074
1075 if (target == GL_VERTEX_PROGRAM_ARB) {
1076 prog = &(ctx->VertexProgram.Current->Base);
1077 }
1078 else if (target == GL_FRAGMENT_PROGRAM_ARB) {
1079 prog = &(ctx->FragmentProgram.Current->Base);
1080 }
1081 else {
1082 _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramStringARB(target)");
1083 return;
1084 }
1085
1086 ASSERT(prog);
1087
1088 if (pname != GL_PROGRAM_STRING_ARB) {
1089 _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramStringARB(pname)");
1090 return;
1091 }
1092
1093 if (prog->String)
1094 _mesa_memcpy(dst, prog->String, _mesa_strlen((char *) prog->String));
1095 else
1096 *dst = '\0';
1097 }