Merge branch 'arb_half_float_vertex'
[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 GLfloat * dest;
613 ASSERT_OUTSIDE_BEGIN_END(ctx);
614
615 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
616
617 if (count <= 0) {
618 _mesa_error(ctx, GL_INVALID_VALUE, "glProgramEnvParameters4fv(count)");
619 }
620
621 if (target == GL_FRAGMENT_PROGRAM_ARB
622 && ctx->Extensions.ARB_fragment_program) {
623 if ((index + count) > ctx->Const.FragmentProgram.MaxEnvParams) {
624 _mesa_error(ctx, GL_INVALID_VALUE, "glProgramEnvParameters4fv(index + count)");
625 return;
626 }
627 dest = ctx->FragmentProgram.Parameters[index];
628 }
629 else if (target == GL_VERTEX_PROGRAM_ARB
630 && ctx->Extensions.ARB_vertex_program) {
631 if ((index + count) > ctx->Const.VertexProgram.MaxEnvParams) {
632 _mesa_error(ctx, GL_INVALID_VALUE, "glProgramEnvParameters4fv(index + count)");
633 return;
634 }
635 dest = ctx->VertexProgram.Parameters[index];
636 }
637 else {
638 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramEnvParameters4fv(target)");
639 return;
640 }
641
642 memcpy(dest, params, count * 4 * sizeof(GLfloat));
643 }
644
645
646 void GLAPIENTRY
647 _mesa_GetProgramEnvParameterdvARB(GLenum target, GLuint index,
648 GLdouble *params)
649 {
650 GET_CURRENT_CONTEXT(ctx);
651 GLfloat fparams[4];
652
653 _mesa_GetProgramEnvParameterfvARB(target, index, fparams);
654 if (ctx->ErrorValue == GL_NO_ERROR) {
655 params[0] = fparams[0];
656 params[1] = fparams[1];
657 params[2] = fparams[2];
658 params[3] = fparams[3];
659 }
660 }
661
662
663 void GLAPIENTRY
664 _mesa_GetProgramEnvParameterfvARB(GLenum target, GLuint index,
665 GLfloat *params)
666 {
667 GET_CURRENT_CONTEXT(ctx);
668
669 ASSERT_OUTSIDE_BEGIN_END(ctx);
670
671 if (target == GL_FRAGMENT_PROGRAM_ARB
672 && ctx->Extensions.ARB_fragment_program) {
673 if (index >= ctx->Const.FragmentProgram.MaxEnvParams) {
674 _mesa_error(ctx, GL_INVALID_VALUE, "glGetProgramEnvParameter(index)");
675 return;
676 }
677 COPY_4V(params, ctx->FragmentProgram.Parameters[index]);
678 }
679 else if (target == GL_VERTEX_PROGRAM_ARB
680 && ctx->Extensions.ARB_vertex_program) {
681 if (index >= ctx->Const.VertexProgram.MaxEnvParams) {
682 _mesa_error(ctx, GL_INVALID_VALUE, "glGetProgramEnvParameter(index)");
683 return;
684 }
685 COPY_4V(params, ctx->VertexProgram.Parameters[index]);
686 }
687 else {
688 _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramEnvParameter(target)");
689 return;
690 }
691 }
692
693
694 /**
695 * Note, this function is also used by the GL_NV_fragment_program extension.
696 */
697 void GLAPIENTRY
698 _mesa_ProgramLocalParameter4fARB(GLenum target, GLuint index,
699 GLfloat x, GLfloat y, GLfloat z, GLfloat w)
700 {
701 GET_CURRENT_CONTEXT(ctx);
702 struct gl_program *prog;
703 ASSERT_OUTSIDE_BEGIN_END(ctx);
704
705 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
706
707 if ((target == GL_FRAGMENT_PROGRAM_NV
708 && ctx->Extensions.NV_fragment_program) ||
709 (target == GL_FRAGMENT_PROGRAM_ARB
710 && ctx->Extensions.ARB_fragment_program)) {
711 if (index >= ctx->Const.FragmentProgram.MaxLocalParams) {
712 _mesa_error(ctx, GL_INVALID_VALUE, "glProgramLocalParameterARB");
713 return;
714 }
715 prog = &(ctx->FragmentProgram.Current->Base);
716 }
717 else if (target == GL_VERTEX_PROGRAM_ARB
718 && ctx->Extensions.ARB_vertex_program) {
719 if (index >= ctx->Const.VertexProgram.MaxLocalParams) {
720 _mesa_error(ctx, GL_INVALID_VALUE, "glProgramLocalParameterARB");
721 return;
722 }
723 prog = &(ctx->VertexProgram.Current->Base);
724 }
725 else {
726 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramLocalParameterARB");
727 return;
728 }
729
730 ASSERT(index < MAX_PROGRAM_LOCAL_PARAMS);
731 prog->LocalParams[index][0] = x;
732 prog->LocalParams[index][1] = y;
733 prog->LocalParams[index][2] = z;
734 prog->LocalParams[index][3] = w;
735 }
736
737
738 /**
739 * Note, this function is also used by the GL_NV_fragment_program extension.
740 */
741 void GLAPIENTRY
742 _mesa_ProgramLocalParameter4fvARB(GLenum target, GLuint index,
743 const GLfloat *params)
744 {
745 _mesa_ProgramLocalParameter4fARB(target, index, params[0], params[1],
746 params[2], params[3]);
747 }
748
749
750 void GLAPIENTRY
751 _mesa_ProgramLocalParameters4fvEXT(GLenum target, GLuint index, GLsizei count,
752 const GLfloat *params)
753 {
754 GET_CURRENT_CONTEXT(ctx);
755 GLfloat *dest;
756 ASSERT_OUTSIDE_BEGIN_END(ctx);
757
758 FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
759
760 if (count <= 0) {
761 _mesa_error(ctx, GL_INVALID_VALUE, "glProgramLocalParameters4fv(count)");
762 }
763
764 if (target == GL_FRAGMENT_PROGRAM_ARB
765 && ctx->Extensions.ARB_fragment_program) {
766 if ((index + count) > ctx->Const.FragmentProgram.MaxLocalParams) {
767 _mesa_error(ctx, GL_INVALID_VALUE, "glProgramLocalParameters4fvEXT(index + count)");
768 return;
769 }
770 dest = ctx->FragmentProgram.Current->Base.LocalParams[index];
771 }
772 else if (target == GL_VERTEX_PROGRAM_ARB
773 && ctx->Extensions.ARB_vertex_program) {
774 if ((index + count) > ctx->Const.VertexProgram.MaxLocalParams) {
775 _mesa_error(ctx, GL_INVALID_VALUE, "glProgramLocalParameters4fvEXT(index + count)");
776 return;
777 }
778 dest = ctx->VertexProgram.Current->Base.LocalParams[index];
779 }
780 else {
781 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramLocalParameters4fvEXT(target)");
782 return;
783 }
784
785 memcpy(dest, params, count * 4 * sizeof(GLfloat));
786 }
787
788
789 /**
790 * Note, this function is also used by the GL_NV_fragment_program extension.
791 */
792 void GLAPIENTRY
793 _mesa_ProgramLocalParameter4dARB(GLenum target, GLuint index,
794 GLdouble x, GLdouble y,
795 GLdouble z, GLdouble w)
796 {
797 _mesa_ProgramLocalParameter4fARB(target, index, (GLfloat) x, (GLfloat) y,
798 (GLfloat) z, (GLfloat) w);
799 }
800
801
802 /**
803 * Note, this function is also used by the GL_NV_fragment_program extension.
804 */
805 void GLAPIENTRY
806 _mesa_ProgramLocalParameter4dvARB(GLenum target, GLuint index,
807 const GLdouble *params)
808 {
809 _mesa_ProgramLocalParameter4fARB(target, index,
810 (GLfloat) params[0], (GLfloat) params[1],
811 (GLfloat) params[2], (GLfloat) params[3]);
812 }
813
814
815 /**
816 * Note, this function is also used by the GL_NV_fragment_program extension.
817 */
818 void GLAPIENTRY
819 _mesa_GetProgramLocalParameterfvARB(GLenum target, GLuint index,
820 GLfloat *params)
821 {
822 const struct gl_program *prog;
823 GLuint maxParams;
824 GET_CURRENT_CONTEXT(ctx);
825 ASSERT_OUTSIDE_BEGIN_END(ctx);
826
827 if (target == GL_VERTEX_PROGRAM_ARB
828 && ctx->Extensions.ARB_vertex_program) {
829 prog = &(ctx->VertexProgram.Current->Base);
830 maxParams = ctx->Const.VertexProgram.MaxLocalParams;
831 }
832 else if (target == GL_FRAGMENT_PROGRAM_ARB
833 && ctx->Extensions.ARB_fragment_program) {
834 prog = &(ctx->FragmentProgram.Current->Base);
835 maxParams = ctx->Const.FragmentProgram.MaxLocalParams;
836 }
837 else if (target == GL_FRAGMENT_PROGRAM_NV
838 && ctx->Extensions.NV_fragment_program) {
839 prog = &(ctx->FragmentProgram.Current->Base);
840 maxParams = MAX_NV_FRAGMENT_PROGRAM_PARAMS;
841 }
842 else {
843 _mesa_error(ctx, GL_INVALID_ENUM,
844 "glGetProgramLocalParameterARB(target)");
845 return;
846 }
847
848 if (index >= maxParams) {
849 _mesa_error(ctx, GL_INVALID_VALUE,
850 "glGetProgramLocalParameterARB(index)");
851 return;
852 }
853
854 ASSERT(prog);
855 ASSERT(index < MAX_PROGRAM_LOCAL_PARAMS);
856 COPY_4V(params, prog->LocalParams[index]);
857 }
858
859
860 /**
861 * Note, this function is also used by the GL_NV_fragment_program extension.
862 */
863 void GLAPIENTRY
864 _mesa_GetProgramLocalParameterdvARB(GLenum target, GLuint index,
865 GLdouble *params)
866 {
867 GET_CURRENT_CONTEXT(ctx);
868 GLfloat floatParams[4];
869 ASSIGN_4V(floatParams, 0.0F, 0.0F, 0.0F, 0.0F);
870 _mesa_GetProgramLocalParameterfvARB(target, index, floatParams);
871 if (ctx->ErrorValue == GL_NO_ERROR) {
872 COPY_4V(params, floatParams);
873 }
874 }
875
876
877 void GLAPIENTRY
878 _mesa_GetProgramivARB(GLenum target, GLenum pname, GLint *params)
879 {
880 const struct gl_program_constants *limits;
881 struct gl_program *prog;
882 GET_CURRENT_CONTEXT(ctx);
883
884 ASSERT_OUTSIDE_BEGIN_END(ctx);
885
886 if (target == GL_VERTEX_PROGRAM_ARB
887 && ctx->Extensions.ARB_vertex_program) {
888 prog = &(ctx->VertexProgram.Current->Base);
889 limits = &ctx->Const.VertexProgram;
890 }
891 else if (target == GL_FRAGMENT_PROGRAM_ARB
892 && ctx->Extensions.ARB_fragment_program) {
893 prog = &(ctx->FragmentProgram.Current->Base);
894 limits = &ctx->Const.FragmentProgram;
895 }
896 else {
897 _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramivARB(target)");
898 return;
899 }
900
901 ASSERT(prog);
902 ASSERT(limits);
903
904 /* Queries supported for both vertex and fragment programs */
905 switch (pname) {
906 case GL_PROGRAM_LENGTH_ARB:
907 *params
908 = prog->String ? (GLint) _mesa_strlen((char *) prog->String) : 0;
909 return;
910 case GL_PROGRAM_FORMAT_ARB:
911 *params = prog->Format;
912 return;
913 case GL_PROGRAM_BINDING_ARB:
914 *params = prog->Id;
915 return;
916 case GL_PROGRAM_INSTRUCTIONS_ARB:
917 *params = prog->NumInstructions;
918 return;
919 case GL_MAX_PROGRAM_INSTRUCTIONS_ARB:
920 *params = limits->MaxInstructions;
921 return;
922 case GL_PROGRAM_NATIVE_INSTRUCTIONS_ARB:
923 *params = prog->NumNativeInstructions;
924 return;
925 case GL_MAX_PROGRAM_NATIVE_INSTRUCTIONS_ARB:
926 *params = limits->MaxNativeInstructions;
927 return;
928 case GL_PROGRAM_TEMPORARIES_ARB:
929 *params = prog->NumTemporaries;
930 return;
931 case GL_MAX_PROGRAM_TEMPORARIES_ARB:
932 *params = limits->MaxTemps;
933 return;
934 case GL_PROGRAM_NATIVE_TEMPORARIES_ARB:
935 *params = prog->NumNativeTemporaries;
936 return;
937 case GL_MAX_PROGRAM_NATIVE_TEMPORARIES_ARB:
938 *params = limits->MaxNativeTemps;
939 return;
940 case GL_PROGRAM_PARAMETERS_ARB:
941 *params = prog->NumParameters;
942 return;
943 case GL_MAX_PROGRAM_PARAMETERS_ARB:
944 *params = limits->MaxParameters;
945 return;
946 case GL_PROGRAM_NATIVE_PARAMETERS_ARB:
947 *params = prog->NumNativeParameters;
948 return;
949 case GL_MAX_PROGRAM_NATIVE_PARAMETERS_ARB:
950 *params = limits->MaxNativeParameters;
951 return;
952 case GL_PROGRAM_ATTRIBS_ARB:
953 *params = prog->NumAttributes;
954 return;
955 case GL_MAX_PROGRAM_ATTRIBS_ARB:
956 *params = limits->MaxAttribs;
957 return;
958 case GL_PROGRAM_NATIVE_ATTRIBS_ARB:
959 *params = prog->NumNativeAttributes;
960 return;
961 case GL_MAX_PROGRAM_NATIVE_ATTRIBS_ARB:
962 *params = limits->MaxNativeAttribs;
963 return;
964 case GL_PROGRAM_ADDRESS_REGISTERS_ARB:
965 *params = prog->NumAddressRegs;
966 return;
967 case GL_MAX_PROGRAM_ADDRESS_REGISTERS_ARB:
968 *params = limits->MaxAddressRegs;
969 return;
970 case GL_PROGRAM_NATIVE_ADDRESS_REGISTERS_ARB:
971 *params = prog->NumNativeAddressRegs;
972 return;
973 case GL_MAX_PROGRAM_NATIVE_ADDRESS_REGISTERS_ARB:
974 *params = limits->MaxNativeAddressRegs;
975 return;
976 case GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB:
977 *params = limits->MaxLocalParams;
978 return;
979 case GL_MAX_PROGRAM_ENV_PARAMETERS_ARB:
980 *params = limits->MaxEnvParams;
981 return;
982 case GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB:
983 /*
984 * XXX we may not really need a driver callback here.
985 * If the number of native instructions, registers, etc. used
986 * are all below the maximums, we could return true.
987 * The spec says that even if this query returns true, there's
988 * no guarantee that the program will run in hardware.
989 */
990 if (prog->Id == 0) {
991 /* default/null program */
992 *params = GL_FALSE;
993 }
994 else if (ctx->Driver.IsProgramNative) {
995 /* ask the driver */
996 *params = ctx->Driver.IsProgramNative( ctx, target, prog );
997 }
998 else {
999 /* probably running in software */
1000 *params = GL_TRUE;
1001 }
1002 return;
1003 default:
1004 /* continue with fragment-program only queries below */
1005 break;
1006 }
1007
1008 /*
1009 * The following apply to fragment programs only (at this time)
1010 */
1011 if (target == GL_FRAGMENT_PROGRAM_ARB) {
1012 const struct gl_fragment_program *fp = ctx->FragmentProgram.Current;
1013 switch (pname) {
1014 case GL_PROGRAM_ALU_INSTRUCTIONS_ARB:
1015 *params = fp->Base.NumNativeAluInstructions;
1016 return;
1017 case GL_PROGRAM_NATIVE_ALU_INSTRUCTIONS_ARB:
1018 *params = fp->Base.NumAluInstructions;
1019 return;
1020 case GL_PROGRAM_TEX_INSTRUCTIONS_ARB:
1021 *params = fp->Base.NumTexInstructions;
1022 return;
1023 case GL_PROGRAM_NATIVE_TEX_INSTRUCTIONS_ARB:
1024 *params = fp->Base.NumNativeTexInstructions;
1025 return;
1026 case GL_PROGRAM_TEX_INDIRECTIONS_ARB:
1027 *params = fp->Base.NumTexIndirections;
1028 return;
1029 case GL_PROGRAM_NATIVE_TEX_INDIRECTIONS_ARB:
1030 *params = fp->Base.NumNativeTexIndirections;
1031 return;
1032 case GL_MAX_PROGRAM_ALU_INSTRUCTIONS_ARB:
1033 *params = limits->MaxAluInstructions;
1034 return;
1035 case GL_MAX_PROGRAM_NATIVE_ALU_INSTRUCTIONS_ARB:
1036 *params = limits->MaxNativeAluInstructions;
1037 return;
1038 case GL_MAX_PROGRAM_TEX_INSTRUCTIONS_ARB:
1039 *params = limits->MaxTexInstructions;
1040 return;
1041 case GL_MAX_PROGRAM_NATIVE_TEX_INSTRUCTIONS_ARB:
1042 *params = limits->MaxNativeTexInstructions;
1043 return;
1044 case GL_MAX_PROGRAM_TEX_INDIRECTIONS_ARB:
1045 *params = limits->MaxTexIndirections;
1046 return;
1047 case GL_MAX_PROGRAM_NATIVE_TEX_INDIRECTIONS_ARB:
1048 *params = limits->MaxNativeTexIndirections;
1049 return;
1050 default:
1051 _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramivARB(pname)");
1052 return;
1053 }
1054 } else {
1055 _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramivARB(pname)");
1056 return;
1057 }
1058 }
1059
1060
1061 void GLAPIENTRY
1062 _mesa_GetProgramStringARB(GLenum target, GLenum pname, GLvoid *string)
1063 {
1064 const struct gl_program *prog;
1065 char *dst = (char *) string;
1066 GET_CURRENT_CONTEXT(ctx);
1067
1068 ASSERT_OUTSIDE_BEGIN_END(ctx);
1069
1070 if (target == GL_VERTEX_PROGRAM_ARB) {
1071 prog = &(ctx->VertexProgram.Current->Base);
1072 }
1073 else if (target == GL_FRAGMENT_PROGRAM_ARB) {
1074 prog = &(ctx->FragmentProgram.Current->Base);
1075 }
1076 else {
1077 _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramStringARB(target)");
1078 return;
1079 }
1080
1081 ASSERT(prog);
1082
1083 if (pname != GL_PROGRAM_STRING_ARB) {
1084 _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramStringARB(pname)");
1085 return;
1086 }
1087
1088 if (prog->String)
1089 _mesa_memcpy(dst, prog->String, _mesa_strlen((char *) prog->String));
1090 else
1091 *dst = '\0';
1092 }