blob: 1a931326afa3a9d5117429fb32c85ebce5368bdd [file] [log] [blame]
Brian34ae99d2006-12-18 08:28:54 -07001/*
2 * Mesa 3-D graphics library
Brian3e4302f2007-05-09 08:04:32 -06003 * Version: 7.0
Brian34ae99d2006-12-18 08:28:54 -07004 *
Brian5b01c5e2006-12-19 18:02:03 -07005 * Copyright (C) 2004-2007 Brian Paul All Rights Reserved.
Brian34ae99d2006-12-18 08:28:54 -07006 *
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 shader_api.c
Brian5b01c5e2006-12-19 18:02:03 -070027 * Implementation of GLSL-related API functions
Brian34ae99d2006-12-18 08:28:54 -070028 * \author Brian Paul
29 */
30
31/**
32 * XXX things to do:
33 * 1. Check that the right error code is generated for all _mesa_error() calls.
Brian5b01c5e2006-12-19 18:02:03 -070034 * 2. Insert FLUSH_VERTICES calls in various places
Brian34ae99d2006-12-18 08:28:54 -070035 */
36
37
38#include "glheader.h"
39#include "context.h"
40#include "hash.h"
Brian274ac7a2007-04-18 16:05:53 -060041#include "macros.h"
Brian34ae99d2006-12-18 08:28:54 -070042#include "program.h"
43#include "prog_parameter.h"
Brian3209c3e2007-01-09 17:49:24 -070044#include "prog_print.h"
45#include "prog_statevars.h"
Brianc223c6b2007-07-04 13:15:20 -060046#include "shader/shader_api.h"
47#include "shader/slang/slang_compile.h"
48#include "shader/slang/slang_link.h"
Brian34ae99d2006-12-18 08:28:54 -070049
50
51
Brianf2923612006-12-20 09:56:44 -070052/**
53 * Allocate a new gl_shader_program object, initialize it.
54 */
55struct gl_shader_program *
56_mesa_new_shader_program(GLcontext *ctx, GLuint name)
57{
58 struct gl_shader_program *shProg;
59 shProg = CALLOC_STRUCT(gl_shader_program);
60 if (shProg) {
Brianf3e8c322007-04-18 14:53:23 -060061 shProg->Type = GL_SHADER_PROGRAM_MESA;
Brianf2923612006-12-20 09:56:44 -070062 shProg->Name = name;
63 shProg->RefCount = 1;
Brian3209c3e2007-01-09 17:49:24 -070064 shProg->Attributes = _mesa_new_parameter_list();
Brianf2923612006-12-20 09:56:44 -070065 }
66 return shProg;
67}
68
69
Brianb9fbedd2007-03-26 09:23:44 -060070/**
Brian3c008a02007-04-12 15:22:32 -060071 * Clear (free) the shader program state that gets produced by linking.
Brianb9fbedd2007-03-26 09:23:44 -060072 */
Brianf2923612006-12-20 09:56:44 -070073void
Brian3c008a02007-04-12 15:22:32 -060074_mesa_clear_shader_program_data(GLcontext *ctx,
75 struct gl_shader_program *shProg)
Brianf2923612006-12-20 09:56:44 -070076{
Brianf2923612006-12-20 09:56:44 -070077 if (shProg->VertexProgram) {
78 if (shProg->VertexProgram->Base.Parameters == shProg->Uniforms) {
79 /* to prevent a double-free in the next call */
80 shProg->VertexProgram->Base.Parameters = NULL;
81 }
82 _mesa_delete_program(ctx, &shProg->VertexProgram->Base);
83 shProg->VertexProgram = NULL;
84 }
85
86 if (shProg->FragmentProgram) {
87 if (shProg->FragmentProgram->Base.Parameters == shProg->Uniforms) {
88 /* to prevent a double-free in the next call */
89 shProg->FragmentProgram->Base.Parameters = NULL;
90 }
91 _mesa_delete_program(ctx, &shProg->FragmentProgram->Base);
92 shProg->FragmentProgram = NULL;
93 }
94
Brianf2923612006-12-20 09:56:44 -070095 if (shProg->Uniforms) {
96 _mesa_free_parameter_list(shProg->Uniforms);
97 shProg->Uniforms = NULL;
98 }
99
100 if (shProg->Varying) {
101 _mesa_free_parameter_list(shProg->Varying);
102 shProg->Varying = NULL;
103 }
104}
105
106
Brianb9fbedd2007-03-26 09:23:44 -0600107/**
Brian3c008a02007-04-12 15:22:32 -0600108 * Free all the data that hangs off a shader program object, but not the
109 * object itself.
110 */
111void
112_mesa_free_shader_program_data(GLcontext *ctx,
113 struct gl_shader_program *shProg)
114{
115 GLuint i;
116
Brianf3e8c322007-04-18 14:53:23 -0600117 assert(shProg->Type == GL_SHADER_PROGRAM_MESA);
Brian3c008a02007-04-12 15:22:32 -0600118
119 _mesa_clear_shader_program_data(ctx, shProg);
120
Brian4b7c6fc2007-04-19 15:23:34 -0600121 if (shProg->Attributes) {
122 _mesa_free_parameter_list(shProg->Attributes);
123 shProg->Attributes = NULL;
124 }
125
Brian3c008a02007-04-12 15:22:32 -0600126 /* detach shaders */
127 for (i = 0; i < shProg->NumShaders; i++) {
128 _mesa_reference_shader(ctx, &shProg->Shaders[i], NULL);
129 }
130 if (shProg->Shaders) {
131 _mesa_free(shProg->Shaders);
132 shProg->Shaders = NULL;
133 }
134}
135
136
137/**
Brianb9fbedd2007-03-26 09:23:44 -0600138 * Free/delete a shader program object.
139 */
Brianf2923612006-12-20 09:56:44 -0700140void
141_mesa_free_shader_program(GLcontext *ctx, struct gl_shader_program *shProg)
142{
143 _mesa_free_shader_program_data(ctx, shProg);
Brianb9fbedd2007-03-26 09:23:44 -0600144 if (shProg->Shaders) {
145 _mesa_free(shProg->Shaders);
146 shProg->Shaders = NULL;
147 }
Brianf2923612006-12-20 09:56:44 -0700148 _mesa_free(shProg);
149}
150
151
152/**
Brian3c008a02007-04-12 15:22:32 -0600153 * Set ptr to point to shProg.
154 * If ptr is pointing to another object, decrement its refcount (and delete
155 * if refcount hits zero).
156 * Then set ptr to point to shProg, incrementing its refcount.
157 */
158/* XXX this could be static */
159void
160_mesa_reference_shader_program(GLcontext *ctx,
161 struct gl_shader_program **ptr,
162 struct gl_shader_program *shProg)
163{
164 assert(ptr);
165 if (*ptr == shProg) {
166 /* no-op */
167 return;
168 }
169 if (*ptr) {
170 /* Unreference the old shader program */
171 GLboolean deleteFlag = GL_FALSE;
172 struct gl_shader_program *old = *ptr;
173
174 ASSERT(old->RefCount > 0);
175 old->RefCount--;
176 /*printf("SHPROG DECR %p (%d) to %d\n",
177 (void*) old, old->Name, old->RefCount);*/
178 deleteFlag = (old->RefCount == 0);
179
180 if (deleteFlag) {
181 _mesa_HashRemove(ctx->Shared->ShaderObjects, old->Name);
182 _mesa_free_shader_program(ctx, old);
183 }
184
185 *ptr = NULL;
186 }
187 assert(!*ptr);
188
189 if (shProg) {
190 shProg->RefCount++;
Brian99193e42007-04-12 15:45:02 -0600191 /*printf("SHPROG INCR %p (%d) to %d\n",
192 (void*) shProg, shProg->Name, shProg->RefCount);*/
Brian3c008a02007-04-12 15:22:32 -0600193 *ptr = shProg;
194 }
195}
196
197
198/**
Brianf2923612006-12-20 09:56:44 -0700199 * Lookup a GLSL program object.
200 */
201struct gl_shader_program *
202_mesa_lookup_shader_program(GLcontext *ctx, GLuint name)
203{
204 struct gl_shader_program *shProg;
205 if (name) {
206 shProg = (struct gl_shader_program *)
207 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
208 /* Note that both gl_shader and gl_shader_program objects are kept
209 * in the same hash table. Check the object's type to be sure it's
210 * what we're expecting.
211 */
Brianf3e8c322007-04-18 14:53:23 -0600212 if (shProg && shProg->Type != GL_SHADER_PROGRAM_MESA) {
Brianf2923612006-12-20 09:56:44 -0700213 return NULL;
214 }
215 return shProg;
216 }
217 return NULL;
218}
219
220
221/**
222 * Allocate a new gl_shader object, initialize it.
223 */
224struct gl_shader *
225_mesa_new_shader(GLcontext *ctx, GLuint name, GLenum type)
226{
227 struct gl_shader *shader;
228 assert(type == GL_FRAGMENT_SHADER || type == GL_VERTEX_SHADER);
229 shader = CALLOC_STRUCT(gl_shader);
230 if (shader) {
231 shader->Type = type;
232 shader->Name = name;
233 shader->RefCount = 1;
234 }
235 return shader;
236}
237
238
239void
240_mesa_free_shader(GLcontext *ctx, struct gl_shader *sh)
241{
242 GLuint i;
243 if (sh->Source)
244 _mesa_free((void *) sh->Source);
245 if (sh->InfoLog)
246 _mesa_free(sh->InfoLog);
247 for (i = 0; i < sh->NumPrograms; i++) {
248 assert(sh->Programs[i]);
249 _mesa_delete_program(ctx, sh->Programs[i]);
250 }
251 if (sh->Programs)
252 _mesa_free(sh->Programs);
253 _mesa_free(sh);
254}
255
256
257/**
Brian3c008a02007-04-12 15:22:32 -0600258 * Set ptr to point to sh.
259 * If ptr is pointing to another shader, decrement its refcount (and delete
260 * if refcount hits zero).
261 * Then set ptr to point to sh, incrementing its refcount.
262 */
263/* XXX this could be static */
264void
265_mesa_reference_shader(GLcontext *ctx, struct gl_shader **ptr,
266 struct gl_shader *sh)
267{
268 assert(ptr);
269 if (*ptr == sh) {
270 /* no-op */
271 return;
272 }
273 if (*ptr) {
274 /* Unreference the old shader */
275 GLboolean deleteFlag = GL_FALSE;
276 struct gl_shader *old = *ptr;
277
278 ASSERT(old->RefCount > 0);
279 old->RefCount--;
Brian99193e42007-04-12 15:45:02 -0600280 /*printf("SHADER DECR %p (%d) to %d\n",
281 (void*) old, old->Name, old->RefCount);*/
Brian3c008a02007-04-12 15:22:32 -0600282 deleteFlag = (old->RefCount == 0);
283
284 if (deleteFlag) {
285 _mesa_HashRemove(ctx->Shared->ShaderObjects, old->Name);
286 _mesa_free_shader(ctx, old);
287 }
288
289 *ptr = NULL;
290 }
291 assert(!*ptr);
292
293 if (sh) {
294 /* reference new */
295 sh->RefCount++;
Brian99193e42007-04-12 15:45:02 -0600296 /*printf("SHADER INCR %p (%d) to %d\n",
297 (void*) sh, sh->Name, sh->RefCount);*/
Brian3c008a02007-04-12 15:22:32 -0600298 *ptr = sh;
299 }
300}
301
302
303/**
Brianf2923612006-12-20 09:56:44 -0700304 * Lookup a GLSL shader object.
305 */
306struct gl_shader *
307_mesa_lookup_shader(GLcontext *ctx, GLuint name)
308{
309 if (name) {
310 struct gl_shader *sh = (struct gl_shader *)
311 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
312 /* Note that both gl_shader and gl_shader_program objects are kept
313 * in the same hash table. Check the object's type to be sure it's
314 * what we're expecting.
315 */
Brianf3e8c322007-04-18 14:53:23 -0600316 if (sh && sh->Type == GL_SHADER_PROGRAM_MESA) {
Brianf2923612006-12-20 09:56:44 -0700317 return NULL;
318 }
319 return sh;
320 }
321 return NULL;
322}
323
324
Brianfa4d0362007-02-26 18:33:50 -0700325/**
326 * Initialize context's shader state.
327 */
Brianf2923612006-12-20 09:56:44 -0700328void
329_mesa_init_shader_state(GLcontext * ctx)
330{
Brianfa4d0362007-02-26 18:33:50 -0700331 /* Device drivers may override these to control what kind of instructions
332 * are generated by the GLSL compiler.
333 */
334 ctx->Shader.EmitHighLevelInstructions = GL_TRUE;
Brian63556fa2007-03-23 14:47:46 -0600335 ctx->Shader.EmitCondCodes = GL_TRUE; /* XXX probably want GL_FALSE... */
Brianfa4d0362007-02-26 18:33:50 -0700336 ctx->Shader.EmitComments = GL_FALSE;
Brianf2923612006-12-20 09:56:44 -0700337}
338
339
Brian5b01c5e2006-12-19 18:02:03 -0700340/**
Brian935f93f2007-03-24 16:20:02 -0600341 * Free the per-context shader-related state.
342 */
343void
344_mesa_free_shader_state(GLcontext *ctx)
345{
Brian3c008a02007-04-12 15:22:32 -0600346 _mesa_reference_shader_program(ctx, &ctx->Shader.CurrentProgram, NULL);
Brian935f93f2007-03-24 16:20:02 -0600347}
348
349
350/**
Brian5b01c5e2006-12-19 18:02:03 -0700351 * Copy string from <src> to <dst>, up to maxLength characters, returning
352 * length of <dst> in <length>.
353 * \param src the strings source
354 * \param maxLength max chars to copy
355 * \param length returns number of chars copied
356 * \param dst the string destination
357 */
358static void
359copy_string(GLchar *dst, GLsizei maxLength, GLsizei *length, const GLchar *src)
360{
361 GLsizei len;
362 for (len = 0; len < maxLength - 1 && src && src[len]; len++)
363 dst[len] = src[len];
364 if (maxLength > 0)
365 dst[len] = 0;
366 if (length)
367 *length = len;
368}
369
370
Brian5b01c5e2006-12-19 18:02:03 -0700371/**
372 * Called via ctx->Driver.AttachShader()
373 */
374void
375_mesa_attach_shader(GLcontext *ctx, GLuint program, GLuint shader)
376{
Brian65a18442006-12-19 18:46:56 -0700377 struct gl_shader_program *shProg
378 = _mesa_lookup_shader_program(ctx, program);
379 struct gl_shader *sh = _mesa_lookup_shader(ctx, shader);
380 const GLuint n = shProg->NumShaders;
Brian5b01c5e2006-12-19 18:02:03 -0700381 GLuint i;
382
Brian65a18442006-12-19 18:46:56 -0700383 if (!shProg || !sh) {
Brian43975832007-01-04 08:21:09 -0700384 _mesa_error(ctx, GL_INVALID_VALUE,
Brian5b01c5e2006-12-19 18:02:03 -0700385 "glAttachShader(bad program or shader name)");
386 return;
387 }
388
389 for (i = 0; i < n; i++) {
Brian65a18442006-12-19 18:46:56 -0700390 if (shProg->Shaders[i] == sh) {
Brian5b01c5e2006-12-19 18:02:03 -0700391 /* already attached */
392 return;
Brian34ae99d2006-12-18 08:28:54 -0700393 }
394 }
Brian5b01c5e2006-12-19 18:02:03 -0700395
396 /* grow list */
Brian65a18442006-12-19 18:46:56 -0700397 shProg->Shaders = (struct gl_shader **)
398 _mesa_realloc(shProg->Shaders,
399 n * sizeof(struct gl_shader *),
400 (n + 1) * sizeof(struct gl_shader *));
401 if (!shProg->Shaders) {
Brian5b01c5e2006-12-19 18:02:03 -0700402 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAttachShader");
403 return;
404 }
405
406 /* append */
Brian3c008a02007-04-12 15:22:32 -0600407 shProg->Shaders[n] = NULL; /* since realloc() didn't zero the new space */
408 _mesa_reference_shader(ctx, &shProg->Shaders[n], sh);
Brian65a18442006-12-19 18:46:56 -0700409 shProg->NumShaders++;
Brian5b01c5e2006-12-19 18:02:03 -0700410}
411
412
413void
414_mesa_bind_attrib_location(GLcontext *ctx, GLuint program, GLuint index,
415 const GLchar *name)
416{
Brian65a18442006-12-19 18:46:56 -0700417 struct gl_shader_program *shProg
418 = _mesa_lookup_shader_program(ctx, program);
Brianb7978af2007-01-09 19:17:17 -0700419 const GLint size = -1; /* unknown size */
420 GLint i, oldIndex;
Brian5b01c5e2006-12-19 18:02:03 -0700421
Brian65a18442006-12-19 18:46:56 -0700422 if (!shProg) {
Brian43975832007-01-04 08:21:09 -0700423 _mesa_error(ctx, GL_INVALID_VALUE, "glBindAttribLocation(program)");
Brian5b01c5e2006-12-19 18:02:03 -0700424 return;
425 }
426
Brian9e4bae92006-12-20 09:27:42 -0700427 if (!name)
428 return;
429
430 if (strncmp(name, "gl_", 3) == 0) {
431 _mesa_error(ctx, GL_INVALID_OPERATION,
432 "glBindAttribLocation(illegal name)");
433 return;
434 }
435
Brian9f660252007-04-11 09:00:56 -0600436 if (shProg->LinkStatus) {
437 /* get current index/location for the attribute */
438 oldIndex = _mesa_get_attrib_location(ctx, program, name);
439 }
440 else {
441 oldIndex = -1;
442 }
Brian3209c3e2007-01-09 17:49:24 -0700443
444 /* this will replace the current value if it's already in the list */
Brianb7978af2007-01-09 19:17:17 -0700445 i = _mesa_add_attribute(shProg->Attributes, name, size, index);
Brian3209c3e2007-01-09 17:49:24 -0700446 if (i < 0) {
447 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindAttribLocation");
448 }
449
Brian9f660252007-04-11 09:00:56 -0600450 if (shProg->VertexProgram && oldIndex >= 0 && oldIndex != index) {
451 /* If the index changed, need to search/replace references to that attribute
452 * in the vertex program.
453 */
Brian3209c3e2007-01-09 17:49:24 -0700454 _slang_remap_attribute(&shProg->VertexProgram->Base, oldIndex, index);
455 }
Brian34ae99d2006-12-18 08:28:54 -0700456}
457
458
Brian5b01c5e2006-12-19 18:02:03 -0700459GLuint
460_mesa_create_shader(GLcontext *ctx, GLenum type)
461{
Brian65a18442006-12-19 18:46:56 -0700462 struct gl_shader *sh;
Brian5b01c5e2006-12-19 18:02:03 -0700463 GLuint name;
464
465 name = _mesa_HashFindFreeKeyBlock(ctx->Shared->ShaderObjects, 1);
466
467 switch (type) {
Brian65a18442006-12-19 18:46:56 -0700468 case GL_FRAGMENT_SHADER:
469 case GL_VERTEX_SHADER:
470 sh = _mesa_new_shader(ctx, name, type);
Brian5b01c5e2006-12-19 18:02:03 -0700471 break;
472 default:
473 _mesa_error(ctx, GL_INVALID_ENUM, "CreateShader(type)");
474 return 0;
475 }
476
Brian65a18442006-12-19 18:46:56 -0700477 _mesa_HashInsert(ctx->Shared->ShaderObjects, name, sh);
Brian5b01c5e2006-12-19 18:02:03 -0700478
479 return name;
480}
481
482
483GLuint
484_mesa_create_program(GLcontext *ctx)
485{
486 GLuint name;
Brian65a18442006-12-19 18:46:56 -0700487 struct gl_shader_program *shProg;
Brian5b01c5e2006-12-19 18:02:03 -0700488
Brian65a18442006-12-19 18:46:56 -0700489 name = _mesa_HashFindFreeKeyBlock(ctx->Shared->ShaderObjects, 1);
490 shProg = _mesa_new_shader_program(ctx, name);
Brian5b01c5e2006-12-19 18:02:03 -0700491
Brian65a18442006-12-19 18:46:56 -0700492 _mesa_HashInsert(ctx->Shared->ShaderObjects, name, shProg);
Brian5b01c5e2006-12-19 18:02:03 -0700493
Brian3c008a02007-04-12 15:22:32 -0600494 assert(shProg->RefCount == 1);
495
Brian5b01c5e2006-12-19 18:02:03 -0700496 return name;
497}
498
499
Brian3c008a02007-04-12 15:22:32 -0600500/**
501 * Named w/ "2" to indicate OpenGL 2.x vs GL_ARB_fragment_programs's
502 * DeleteProgramARB.
503 */
Brian5b01c5e2006-12-19 18:02:03 -0700504void
505_mesa_delete_program2(GLcontext *ctx, GLuint name)
506{
Brian3c008a02007-04-12 15:22:32 -0600507 /*
508 * NOTE: deleting shaders/programs works a bit differently than
509 * texture objects (and buffer objects, etc). Shader/program
510 * handles/IDs exist in the hash table until the object is really
511 * deleted (refcount==0). With texture objects, the handle/ID is
512 * removed from the hash table in glDeleteTextures() while the tex
513 * object itself might linger until its refcount goes to zero.
514 */
Brian65a18442006-12-19 18:46:56 -0700515 struct gl_shader_program *shProg;
Brian5b01c5e2006-12-19 18:02:03 -0700516
Brian65a18442006-12-19 18:46:56 -0700517 shProg = _mesa_lookup_shader_program(ctx, name);
518 if (!shProg) {
Brian43975832007-01-04 08:21:09 -0700519 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteProgram(name)");
Brian5b01c5e2006-12-19 18:02:03 -0700520 return;
521 }
522
Brian9e4bae92006-12-20 09:27:42 -0700523 shProg->DeletePending = GL_TRUE;
524
Brian3c008a02007-04-12 15:22:32 -0600525 /* effectively, decr shProg's refcount */
526 _mesa_reference_shader_program(ctx, &shProg, NULL);
Brian5b01c5e2006-12-19 18:02:03 -0700527}
528
529
530void
531_mesa_delete_shader(GLcontext *ctx, GLuint shader)
532{
Brian9e4bae92006-12-20 09:27:42 -0700533 struct gl_shader *sh = _mesa_lookup_shader(ctx, shader);
534 if (!sh) {
535 return;
536 }
Brian5b01c5e2006-12-19 18:02:03 -0700537
Brian9e4bae92006-12-20 09:27:42 -0700538 sh->DeletePending = GL_TRUE;
Brian3c008a02007-04-12 15:22:32 -0600539
540 /* effectively, decr sh's refcount */
541 _mesa_reference_shader(ctx, &sh, NULL);
Brian5b01c5e2006-12-19 18:02:03 -0700542}
543
544
545void
546_mesa_detach_shader(GLcontext *ctx, GLuint program, GLuint shader)
547{
Brian65a18442006-12-19 18:46:56 -0700548 struct gl_shader_program *shProg
549 = _mesa_lookup_shader_program(ctx, program);
550 const GLuint n = shProg->NumShaders;
Brian5b01c5e2006-12-19 18:02:03 -0700551 GLuint i, j;
552
Brian65a18442006-12-19 18:46:56 -0700553 if (!shProg) {
Brian43975832007-01-04 08:21:09 -0700554 _mesa_error(ctx, GL_INVALID_VALUE,
Brian5b01c5e2006-12-19 18:02:03 -0700555 "glDetachShader(bad program or shader name)");
556 return;
557 }
558
559 for (i = 0; i < n; i++) {
Brian65a18442006-12-19 18:46:56 -0700560 if (shProg->Shaders[i]->Name == shader) {
Brian5b01c5e2006-12-19 18:02:03 -0700561 /* found it */
Brian3c008a02007-04-12 15:22:32 -0600562 struct gl_shader **newList;
Brian9e4bae92006-12-20 09:27:42 -0700563
Brian3c008a02007-04-12 15:22:32 -0600564 /* derefernce */
565 _mesa_reference_shader(ctx, &shProg->Shaders[i], NULL);
Brian9e4bae92006-12-20 09:27:42 -0700566
Brian5b01c5e2006-12-19 18:02:03 -0700567 /* alloc new, smaller array */
Brian65a18442006-12-19 18:46:56 -0700568 newList = (struct gl_shader **)
569 _mesa_malloc((n - 1) * sizeof(struct gl_shader *));
Brian5b01c5e2006-12-19 18:02:03 -0700570 if (!newList) {
571 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDetachShader");
572 return;
573 }
574 for (j = 0; j < i; j++) {
Brian65a18442006-12-19 18:46:56 -0700575 newList[j] = shProg->Shaders[j];
Brian5b01c5e2006-12-19 18:02:03 -0700576 }
577 while (++i < n)
Brian65a18442006-12-19 18:46:56 -0700578 newList[j++] = shProg->Shaders[i];
579 _mesa_free(shProg->Shaders);
Brian5b01c5e2006-12-19 18:02:03 -0700580
Brian65a18442006-12-19 18:46:56 -0700581 shProg->Shaders = newList;
Brianbac15c82007-04-18 14:55:18 -0600582 shProg->NumShaders = n - 1;
Brianaaa57412007-04-18 15:22:43 -0600583
584#ifdef DEBUG
585 /* sanity check */
586 {
587 for (j = 0; j < shProg->NumShaders; j++) {
588 assert(shProg->Shaders[j]->Type == GL_VERTEX_SHADER ||
589 shProg->Shaders[j]->Type == GL_FRAGMENT_SHADER);
590 assert(shProg->Shaders[j]->RefCount > 0);
591 }
592 }
593#endif
594
Brian5b01c5e2006-12-19 18:02:03 -0700595 return;
596 }
597 }
598
599 /* not found */
Brian43975832007-01-04 08:21:09 -0700600 _mesa_error(ctx, GL_INVALID_VALUE,
Brian5b01c5e2006-12-19 18:02:03 -0700601 "glDetachShader(shader not found)");
602}
603
604
605void
606_mesa_get_active_attrib(GLcontext *ctx, GLuint program, GLuint index,
607 GLsizei maxLength, GLsizei *length, GLint *size,
608 GLenum *type, GLchar *nameOut)
609{
610 static const GLenum vec_types[] = {
611 GL_FLOAT, GL_FLOAT_VEC2, GL_FLOAT_VEC3, GL_FLOAT_VEC4
612 };
Brian65a18442006-12-19 18:46:56 -0700613 struct gl_shader_program *shProg
614 = _mesa_lookup_shader_program(ctx, program);
Brian5b01c5e2006-12-19 18:02:03 -0700615 GLint sz;
616
Brian65a18442006-12-19 18:46:56 -0700617 if (!shProg) {
Brianaaa57412007-04-18 15:22:43 -0600618 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveAttrib");
Brian5b01c5e2006-12-19 18:02:03 -0700619 return;
620 }
621
Brian65a18442006-12-19 18:46:56 -0700622 if (!shProg->Attributes || index >= shProg->Attributes->NumParameters) {
Brianaaa57412007-04-18 15:22:43 -0600623 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveAttrib(index)");
Brian5b01c5e2006-12-19 18:02:03 -0700624 return;
625 }
626
627 copy_string(nameOut, maxLength, length,
Brian65a18442006-12-19 18:46:56 -0700628 shProg->Attributes->Parameters[index].Name);
629 sz = shProg->Attributes->Parameters[index].Size;
Brian5b01c5e2006-12-19 18:02:03 -0700630 if (size)
631 *size = sz;
632 if (type)
633 *type = vec_types[sz]; /* XXX this is a temporary hack */
634}
635
636
637/**
638 * Called via ctx->Driver.GetActiveUniform().
639 */
640void
641_mesa_get_active_uniform(GLcontext *ctx, GLuint program, GLuint index,
642 GLsizei maxLength, GLsizei *length, GLint *size,
643 GLenum *type, GLchar *nameOut)
644{
Brian65a18442006-12-19 18:46:56 -0700645 struct gl_shader_program *shProg
646 = _mesa_lookup_shader_program(ctx, program);
Brian274ac7a2007-04-18 16:05:53 -0600647 GLuint ind, j;
Brian5b01c5e2006-12-19 18:02:03 -0700648
Brian65a18442006-12-19 18:46:56 -0700649 if (!shProg) {
Brian43975832007-01-04 08:21:09 -0700650 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveUniform");
Brian5b01c5e2006-12-19 18:02:03 -0700651 return;
652 }
653
Brian65a18442006-12-19 18:46:56 -0700654 if (!shProg->Uniforms || index >= shProg->Uniforms->NumParameters) {
Brian5b01c5e2006-12-19 18:02:03 -0700655 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveUniform(index)");
656 return;
657 }
658
Brian274ac7a2007-04-18 16:05:53 -0600659 ind = 0;
660 for (j = 0; j < shProg->Uniforms->NumParameters; j++) {
661 if (shProg->Uniforms->Parameters[j].Type == PROGRAM_UNIFORM ||
662 shProg->Uniforms->Parameters[j].Type == PROGRAM_SAMPLER) {
663 if (ind == index) {
664 /* found it */
665 copy_string(nameOut, maxLength, length,
666 shProg->Uniforms->Parameters[j].Name);
Brian274ac7a2007-04-18 16:05:53 -0600667 if (size)
Brianc93e8832007-04-18 16:27:35 -0600668 *size = shProg->Uniforms->Parameters[j].Size;
Brian274ac7a2007-04-18 16:05:53 -0600669 if (type)
Brianc93e8832007-04-18 16:27:35 -0600670 *type = shProg->Uniforms->Parameters[j].DataType;
Brian274ac7a2007-04-18 16:05:53 -0600671 return;
672 }
673 ind++;
674 }
675 }
676
677 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveUniform(index)");
Brian5b01c5e2006-12-19 18:02:03 -0700678}
679
680
681/**
682 * Called via ctx->Driver.GetAttachedShaders().
683 */
684void
685_mesa_get_attached_shaders(GLcontext *ctx, GLuint program, GLsizei maxCount,
686 GLsizei *count, GLuint *obj)
687{
Brian65a18442006-12-19 18:46:56 -0700688 struct gl_shader_program *shProg
689 = _mesa_lookup_shader_program(ctx, program);
690 if (shProg) {
Brian223d7cb2007-01-23 16:37:51 -0700691 GLint i;
Brian65a18442006-12-19 18:46:56 -0700692 for (i = 0; i < maxCount && i < shProg->NumShaders; i++) {
693 obj[i] = shProg->Shaders[i]->Name;
Brian5b01c5e2006-12-19 18:02:03 -0700694 }
695 if (count)
696 *count = i;
697 }
698 else {
Brian43975832007-01-04 08:21:09 -0700699 _mesa_error(ctx, GL_INVALID_VALUE, "glGetAttachedShaders");
Brian5b01c5e2006-12-19 18:02:03 -0700700 }
701}
702
703
704GLint
705_mesa_get_attrib_location(GLcontext *ctx, GLuint program,
706 const GLchar *name)
707{
Brian65a18442006-12-19 18:46:56 -0700708 struct gl_shader_program *shProg
709 = _mesa_lookup_shader_program(ctx, program);
Brian5b01c5e2006-12-19 18:02:03 -0700710
Brian65a18442006-12-19 18:46:56 -0700711 if (!shProg) {
Brian43975832007-01-04 08:21:09 -0700712 _mesa_error(ctx, GL_INVALID_VALUE, "glGetAttribLocation");
Brian5b01c5e2006-12-19 18:02:03 -0700713 return -1;
714 }
715
Brian65a18442006-12-19 18:46:56 -0700716 if (!shProg->LinkStatus) {
Brian5b01c5e2006-12-19 18:02:03 -0700717 _mesa_error(ctx, GL_INVALID_OPERATION,
718 "glGetAttribLocation(program not linked)");
719 return -1;
720 }
721
722 if (!name)
723 return -1;
724
Brian65a18442006-12-19 18:46:56 -0700725 if (shProg->Attributes) {
Brian3209c3e2007-01-09 17:49:24 -0700726 GLint i = _mesa_lookup_parameter_index(shProg->Attributes, -1, name);
727 if (i >= 0) {
Brian01a91eb2007-01-09 19:26:22 -0700728 return shProg->Attributes->Parameters[i].StateIndexes[0];
Brian5b01c5e2006-12-19 18:02:03 -0700729 }
730 }
731 return -1;
732}
733
734
735GLuint
736_mesa_get_handle(GLcontext *ctx, GLenum pname)
Brian34ae99d2006-12-18 08:28:54 -0700737{
738#if 0
739 GET_CURRENT_CONTEXT(ctx);
740
741 switch (pname) {
742 case GL_PROGRAM_OBJECT_ARB:
743 {
Brian5b01c5e2006-12-19 18:02:03 -0700744 struct gl2_program_intf **pro = ctx->Shader.CurrentProgram;
Brian34ae99d2006-12-18 08:28:54 -0700745
746 if (pro != NULL)
747 return (**pro)._container._generic.
748 GetName((struct gl2_generic_intf **) (pro));
749 }
750 break;
751 default:
752 _mesa_error(ctx, GL_INVALID_ENUM, "glGetHandleARB");
753 }
754#endif
755 return 0;
756}
757
758
Brian5b01c5e2006-12-19 18:02:03 -0700759void
760_mesa_get_programiv(GLcontext *ctx, GLuint program,
761 GLenum pname, GLint *params)
Brian34ae99d2006-12-18 08:28:54 -0700762{
Brian65a18442006-12-19 18:46:56 -0700763 struct gl_shader_program *shProg
764 = _mesa_lookup_shader_program(ctx, program);
Brian34ae99d2006-12-18 08:28:54 -0700765
Brian65a18442006-12-19 18:46:56 -0700766 if (!shProg) {
Brian5b01c5e2006-12-19 18:02:03 -0700767 _mesa_error(ctx, GL_INVALID_VALUE, "glGetProgramiv(program)");
Brian34ae99d2006-12-18 08:28:54 -0700768 return;
769 }
770
Brian5b01c5e2006-12-19 18:02:03 -0700771 switch (pname) {
772 case GL_DELETE_STATUS:
Brian65a18442006-12-19 18:46:56 -0700773 *params = shProg->DeletePending;
Brian5b01c5e2006-12-19 18:02:03 -0700774 break;
775 case GL_LINK_STATUS:
Brian65a18442006-12-19 18:46:56 -0700776 *params = shProg->LinkStatus;
Brian34ae99d2006-12-18 08:28:54 -0700777 break;
Brian5b01c5e2006-12-19 18:02:03 -0700778 case GL_VALIDATE_STATUS:
Brian65a18442006-12-19 18:46:56 -0700779 *params = shProg->Validated;
Brian5b01c5e2006-12-19 18:02:03 -0700780 break;
781 case GL_INFO_LOG_LENGTH:
Jan Dvorak5a0f02a2007-07-13 16:36:00 -0600782 *params = shProg->InfoLog ? strlen(shProg->InfoLog) + 1 : 0;
Brian5b01c5e2006-12-19 18:02:03 -0700783 break;
784 case GL_ATTACHED_SHADERS:
Brian65a18442006-12-19 18:46:56 -0700785 *params = shProg->NumShaders;
Brian5b01c5e2006-12-19 18:02:03 -0700786 break;
787 case GL_ACTIVE_ATTRIBUTES:
Brian3209c3e2007-01-09 17:49:24 -0700788 *params = shProg->Attributes ? shProg->Attributes->NumParameters : 0;
Brian5b01c5e2006-12-19 18:02:03 -0700789 break;
790 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH:
Brian274ac7a2007-04-18 16:05:53 -0600791 *params = _mesa_longest_parameter_name(shProg->Attributes,
792 PROGRAM_INPUT) + 1;
Brian5b01c5e2006-12-19 18:02:03 -0700793 break;
794 case GL_ACTIVE_UNIFORMS:
Brian274ac7a2007-04-18 16:05:53 -0600795 *params
796 = _mesa_num_parameters_of_type(shProg->Uniforms, PROGRAM_UNIFORM)
797 + _mesa_num_parameters_of_type(shProg->Uniforms, PROGRAM_SAMPLER);
Brian5b01c5e2006-12-19 18:02:03 -0700798 break;
799 case GL_ACTIVE_UNIFORM_MAX_LENGTH:
Brian274ac7a2007-04-18 16:05:53 -0600800 *params = MAX2(
801 _mesa_longest_parameter_name(shProg->Uniforms, PROGRAM_UNIFORM),
802 _mesa_longest_parameter_name(shProg->Uniforms, PROGRAM_SAMPLER));
803 if (*params > 0)
804 (*params)++; /* add one for terminating zero */
Brian34ae99d2006-12-18 08:28:54 -0700805 break;
806 default:
Brian5b01c5e2006-12-19 18:02:03 -0700807 _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramiv(pname)");
808 return;
Brian34ae99d2006-12-18 08:28:54 -0700809 }
Brian5b01c5e2006-12-19 18:02:03 -0700810}
Brian34ae99d2006-12-18 08:28:54 -0700811
Brian34ae99d2006-12-18 08:28:54 -0700812
Brian5b01c5e2006-12-19 18:02:03 -0700813void
814_mesa_get_shaderiv(GLcontext *ctx, GLuint name, GLenum pname, GLint *params)
815{
Brian65a18442006-12-19 18:46:56 -0700816 struct gl_shader *shader = _mesa_lookup_shader(ctx, name);
Brian5b01c5e2006-12-19 18:02:03 -0700817
818 if (!shader) {
819 _mesa_error(ctx, GL_INVALID_VALUE, "glGetShaderiv(shader)");
820 return;
821 }
Brian65a18442006-12-19 18:46:56 -0700822
Brian5b01c5e2006-12-19 18:02:03 -0700823 switch (pname) {
824 case GL_SHADER_TYPE:
825 *params = shader->Type;
826 break;
827 case GL_DELETE_STATUS:
828 *params = shader->DeletePending;
829 break;
830 case GL_COMPILE_STATUS:
831 *params = shader->CompileStatus;
832 break;
833 case GL_INFO_LOG_LENGTH:
Jan Dvorak5a0f02a2007-07-13 16:36:00 -0600834 *params = shader->InfoLog ? strlen(shader->InfoLog) + 1 : 0;
Brian5b01c5e2006-12-19 18:02:03 -0700835 break;
836 case GL_SHADER_SOURCE_LENGTH:
Jan Dvorak5a0f02a2007-07-13 16:36:00 -0600837 *params = shader->Source ? strlen((char *) shader->Source) + 1 : 0;
Brian5b01c5e2006-12-19 18:02:03 -0700838 break;
839 default:
840 _mesa_error(ctx, GL_INVALID_ENUM, "glGetShaderiv(pname)");
841 return;
842 }
843}
844
845
846void
847_mesa_get_program_info_log(GLcontext *ctx, GLuint program, GLsizei bufSize,
848 GLsizei *length, GLchar *infoLog)
849{
Brian65a18442006-12-19 18:46:56 -0700850 struct gl_shader_program *shProg
851 = _mesa_lookup_shader_program(ctx, program);
852 if (!shProg) {
Brian5b01c5e2006-12-19 18:02:03 -0700853 _mesa_error(ctx, GL_INVALID_VALUE, "glGetProgramInfoLog(program)");
854 return;
855 }
Brian65a18442006-12-19 18:46:56 -0700856 copy_string(infoLog, bufSize, length, shProg->InfoLog);
Brian5b01c5e2006-12-19 18:02:03 -0700857}
858
859
860void
861_mesa_get_shader_info_log(GLcontext *ctx, GLuint shader, GLsizei bufSize,
862 GLsizei *length, GLchar *infoLog)
863{
Brian65a18442006-12-19 18:46:56 -0700864 struct gl_shader *sh = _mesa_lookup_shader(ctx, shader);
865 if (!sh) {
Brian5b01c5e2006-12-19 18:02:03 -0700866 _mesa_error(ctx, GL_INVALID_VALUE, "glGetShaderInfoLog(shader)");
867 return;
868 }
Brian65a18442006-12-19 18:46:56 -0700869 copy_string(infoLog, bufSize, length, sh->InfoLog);
Brian5b01c5e2006-12-19 18:02:03 -0700870}
871
872
873/**
874 * Called via ctx->Driver.GetShaderSource().
875 */
876void
877_mesa_get_shader_source(GLcontext *ctx, GLuint shader, GLsizei maxLength,
878 GLsizei *length, GLchar *sourceOut)
879{
Brian65a18442006-12-19 18:46:56 -0700880 struct gl_shader *sh = _mesa_lookup_shader(ctx, shader);
881 if (!sh) {
Brian5b01c5e2006-12-19 18:02:03 -0700882 _mesa_error(ctx, GL_INVALID_VALUE, "glGetShaderSource(shader)");
883 return;
884 }
Brian65a18442006-12-19 18:46:56 -0700885 copy_string(sourceOut, maxLength, length, sh->Source);
Brian5b01c5e2006-12-19 18:02:03 -0700886}
887
888
889/**
890 * Called via ctx->Driver.GetUniformfv().
891 */
892void
893_mesa_get_uniformfv(GLcontext *ctx, GLuint program, GLint location,
894 GLfloat *params)
895{
Brian65a18442006-12-19 18:46:56 -0700896 struct gl_shader_program *shProg
897 = _mesa_lookup_shader_program(ctx, program);
898 if (shProg) {
Brian223d7cb2007-01-23 16:37:51 -0700899 GLint i;
Brian65a18442006-12-19 18:46:56 -0700900 if (location >= 0 && location < shProg->Uniforms->NumParameters) {
901 for (i = 0; i < shProg->Uniforms->Parameters[location].Size; i++) {
902 params[i] = shProg->Uniforms->ParameterValues[location][i];
Brian5b01c5e2006-12-19 18:02:03 -0700903 }
904 }
905 else {
906 _mesa_error(ctx, GL_INVALID_VALUE, "glGetUniformfv(location)");
907 }
908 }
909 else {
Brian43975832007-01-04 08:21:09 -0700910 _mesa_error(ctx, GL_INVALID_VALUE, "glGetUniformfv(program)");
Brian5b01c5e2006-12-19 18:02:03 -0700911 }
912}
913
914
915/**
916 * Called via ctx->Driver.GetUniformLocation().
917 */
918GLint
919_mesa_get_uniform_location(GLcontext *ctx, GLuint program, const GLchar *name)
920{
Brian71623982007-01-30 16:55:03 -0700921 struct gl_shader_program *shProg
922 = _mesa_lookup_shader_program(ctx, program);
923 if (shProg) {
Brian5b01c5e2006-12-19 18:02:03 -0700924 GLuint loc;
Brian65a18442006-12-19 18:46:56 -0700925 for (loc = 0; loc < shProg->Uniforms->NumParameters; loc++) {
Brian5b01c5e2006-12-19 18:02:03 -0700926 const struct gl_program_parameter *u
Brian65a18442006-12-19 18:46:56 -0700927 = shProg->Uniforms->Parameters + loc;
Brian29053852006-12-21 11:21:26 -0700928 /* XXX this is a temporary simplification / short-cut.
929 * We need to handle things like "e.c[0].b" as seen in the
930 * GLSL orange book, page 189.
931 */
Brian5cf73262007-01-05 16:02:45 -0700932 if ((u->Type == PROGRAM_UNIFORM ||
933 u->Type == PROGRAM_SAMPLER) && !strcmp(u->Name, name)) {
Brian5b01c5e2006-12-19 18:02:03 -0700934 return loc;
935 }
936 }
937 }
938 return -1;
939
940}
941
942
943GLboolean
944_mesa_is_program(GLcontext *ctx, GLuint name)
945{
Brian65a18442006-12-19 18:46:56 -0700946 struct gl_shader_program *shProg = _mesa_lookup_shader_program(ctx, name);
947 return shProg ? GL_TRUE : GL_FALSE;
Brian5b01c5e2006-12-19 18:02:03 -0700948}
949
950
951GLboolean
952_mesa_is_shader(GLcontext *ctx, GLuint name)
953{
Brian65a18442006-12-19 18:46:56 -0700954 struct gl_shader *shader = _mesa_lookup_shader(ctx, name);
Brian5b01c5e2006-12-19 18:02:03 -0700955 return shader ? GL_TRUE : GL_FALSE;
Brian34ae99d2006-12-18 08:28:54 -0700956}
957
958
959
Brian5b01c5e2006-12-19 18:02:03 -0700960/**
961 * Called via ctx->Driver.ShaderSource()
962 */
963void
964_mesa_shader_source(GLcontext *ctx, GLuint shader, const GLchar *source)
Brian34ae99d2006-12-18 08:28:54 -0700965{
Brian65a18442006-12-19 18:46:56 -0700966 struct gl_shader *sh = _mesa_lookup_shader(ctx, shader);
967 if (!sh) {
Brian5b01c5e2006-12-19 18:02:03 -0700968 _mesa_error(ctx, GL_INVALID_VALUE, "glShaderSource(shaderObj)");
Brian34ae99d2006-12-18 08:28:54 -0700969 return;
970 }
971
Brian34ae99d2006-12-18 08:28:54 -0700972 /* free old shader source string and install new one */
Brian65a18442006-12-19 18:46:56 -0700973 if (sh->Source) {
974 _mesa_free((void *) sh->Source);
Brian34ae99d2006-12-18 08:28:54 -0700975 }
Brian65a18442006-12-19 18:46:56 -0700976 sh->Source = source;
Brian9e4bae92006-12-20 09:27:42 -0700977 sh->CompileStatus = GL_FALSE;
Brian34ae99d2006-12-18 08:28:54 -0700978}
979
980
Brian5b01c5e2006-12-19 18:02:03 -0700981/**
982 * Called via ctx->Driver.CompileShader()
983 */
984void
985_mesa_compile_shader(GLcontext *ctx, GLuint shaderObj)
Brian34ae99d2006-12-18 08:28:54 -0700986{
Brian65a18442006-12-19 18:46:56 -0700987 struct gl_shader *sh = _mesa_lookup_shader(ctx, shaderObj);
Brian34ae99d2006-12-18 08:28:54 -0700988
Brian65a18442006-12-19 18:46:56 -0700989 if (!sh) {
Brian34ae99d2006-12-18 08:28:54 -0700990 _mesa_error(ctx, GL_INVALID_VALUE, "glCompileShader(shaderObj)");
991 return;
992 }
993
Brian43975832007-01-04 08:21:09 -0700994 sh->CompileStatus = _slang_compile(ctx, sh);
Brian34ae99d2006-12-18 08:28:54 -0700995}
996
997
Brian5b01c5e2006-12-19 18:02:03 -0700998/**
999 * Called via ctx->Driver.LinkProgram()
1000 */
1001void
1002_mesa_link_program(GLcontext *ctx, GLuint program)
Brian34ae99d2006-12-18 08:28:54 -07001003{
Brian65a18442006-12-19 18:46:56 -07001004 struct gl_shader_program *shProg;
Brian34ae99d2006-12-18 08:28:54 -07001005
Brian65a18442006-12-19 18:46:56 -07001006 shProg = _mesa_lookup_shader_program(ctx, program);
1007 if (!shProg) {
Brian43975832007-01-04 08:21:09 -07001008 _mesa_error(ctx, GL_INVALID_VALUE, "glLinkProgram(program)");
Brian34ae99d2006-12-18 08:28:54 -07001009 return;
1010 }
1011
Brianc1771912007-02-16 09:56:19 -07001012 _slang_link(ctx, program, shProg);
Brian34ae99d2006-12-18 08:28:54 -07001013}
1014
1015
1016/**
Brian5b01c5e2006-12-19 18:02:03 -07001017 * Called via ctx->Driver.UseProgram()
Brian34ae99d2006-12-18 08:28:54 -07001018 */
Brian5b01c5e2006-12-19 18:02:03 -07001019void
1020_mesa_use_program(GLcontext *ctx, GLuint program)
Brian34ae99d2006-12-18 08:28:54 -07001021{
Brian3c008a02007-04-12 15:22:32 -06001022 struct gl_shader_program *shProg;
1023
Brian00d63aa2007-02-03 11:35:02 -07001024 if (ctx->Shader.CurrentProgram &&
1025 ctx->Shader.CurrentProgram->Name == program) {
1026 /* no-op */
1027 return;
1028 }
1029
1030 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
1031
Brian5b01c5e2006-12-19 18:02:03 -07001032 if (program) {
Brian65a18442006-12-19 18:46:56 -07001033 shProg = _mesa_lookup_shader_program(ctx, program);
1034 if (!shProg) {
Brian43975832007-01-04 08:21:09 -07001035 _mesa_error(ctx, GL_INVALID_VALUE,
Brian5b01c5e2006-12-19 18:02:03 -07001036 "glUseProgramObjectARB(programObj)");
1037 return;
1038 }
Brian5b01c5e2006-12-19 18:02:03 -07001039 }
1040 else {
Brian3c008a02007-04-12 15:22:32 -06001041 shProg = NULL;
1042 }
1043
1044 _mesa_reference_shader_program(ctx, &ctx->Shader.CurrentProgram, shProg);
Brian5b01c5e2006-12-19 18:02:03 -07001045}
Brian34ae99d2006-12-18 08:28:54 -07001046
Brian5b01c5e2006-12-19 18:02:03 -07001047
1048/**
1049 * Called via ctx->Driver.Uniform().
1050 */
1051void
1052_mesa_uniform(GLcontext *ctx, GLint location, GLsizei count,
1053 const GLvoid *values, GLenum type)
1054{
Brian3a8e2772006-12-20 17:19:16 -07001055 struct gl_shader_program *shProg = ctx->Shader.CurrentProgram;
Brian98650bd2007-03-13 16:32:48 -06001056 GLint elems, i, k;
Brian3a8e2772006-12-20 17:19:16 -07001057
1058 if (!shProg || !shProg->LinkStatus) {
Brian5b01c5e2006-12-19 18:02:03 -07001059 _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(program not linked)");
Brian3a8e2772006-12-20 17:19:16 -07001060 return;
1061 }
1062
Brian223d7cb2007-01-23 16:37:51 -07001063 if (location < 0 || location >= (GLint) shProg->Uniforms->NumParameters) {
Brian3a8e2772006-12-20 17:19:16 -07001064 _mesa_error(ctx, GL_INVALID_VALUE, "glUniform(location)");
1065 return;
1066 }
1067
1068 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
1069
Brianfee9bbe2007-02-02 18:05:43 -07001070 /*
1071 * If we're setting a sampler, we must use glUniformi1()!
1072 */
1073 if (shProg->Uniforms->Parameters[location].Type == PROGRAM_SAMPLER) {
Brian3e4302f2007-05-09 08:04:32 -06001074 GLint unit;
Brianfee9bbe2007-02-02 18:05:43 -07001075 if (type != GL_INT || count != 1) {
1076 _mesa_error(ctx, GL_INVALID_OPERATION,
1077 "glUniform(only glUniform1i can be used "
1078 "to set sampler uniforms)");
1079 return;
1080 }
Brian3e4302f2007-05-09 08:04:32 -06001081 /* check that the sampler (tex unit index) is legal */
1082 unit = ((GLint *) values)[0];
1083 if (unit >= ctx->Const.MaxTextureImageUnits) {
1084 _mesa_error(ctx, GL_INVALID_VALUE,
1085 "glUniform1(invalid sampler/tex unit index)");
1086 return;
1087 }
Brianfee9bbe2007-02-02 18:05:43 -07001088 }
1089
Brian52363952007-03-13 16:50:24 -06001090 if (count < 0) {
1091 _mesa_error(ctx, GL_INVALID_VALUE, "glUniform(count < 0)");
1092 return;
1093 }
1094
Brian98650bd2007-03-13 16:32:48 -06001095 switch (type) {
1096 case GL_FLOAT:
1097 case GL_INT:
1098 elems = 1;
1099 break;
1100 case GL_FLOAT_VEC2:
1101 case GL_INT_VEC2:
1102 elems = 2;
1103 break;
1104 case GL_FLOAT_VEC3:
1105 case GL_INT_VEC3:
1106 elems = 3;
1107 break;
1108 case GL_FLOAT_VEC4:
1109 case GL_INT_VEC4:
1110 elems = 4;
1111 break;
1112 default:
1113 _mesa_problem(ctx, "Invalid type in _mesa_uniform");
1114 return;
Brian89dc4852007-01-04 14:35:44 -07001115 }
Brian98650bd2007-03-13 16:32:48 -06001116
1117 if (count * elems > shProg->Uniforms->Parameters[location].Size) {
1118 _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(count too large)");
1119 return;
1120 }
1121
1122 for (k = 0; k < count; k++) {
1123 GLfloat *uniformVal = shProg->Uniforms->ParameterValues[location + k];
1124 if (type == GL_INT ||
1125 type == GL_INT_VEC2 ||
1126 type == GL_INT_VEC3 ||
1127 type == GL_INT_VEC4) {
Brian52363952007-03-13 16:50:24 -06001128 const GLint *iValues = ((const GLint *) values) + k * elems;
Brian98650bd2007-03-13 16:32:48 -06001129 for (i = 0; i < elems; i++) {
1130 uniformVal[i] = (GLfloat) iValues[i];
1131 }
1132 }
1133 else {
Brian52363952007-03-13 16:50:24 -06001134 const GLfloat *fValues = ((const GLfloat *) values) + k * elems;
Brian98650bd2007-03-13 16:32:48 -06001135 for (i = 0; i < elems; i++) {
1136 uniformVal[i] = fValues[i];
1137 }
Brian89dc4852007-01-04 14:35:44 -07001138 }
Brian5b01c5e2006-12-19 18:02:03 -07001139 }
Brian5cf73262007-01-05 16:02:45 -07001140
1141 if (shProg->Uniforms->Parameters[location].Type == PROGRAM_SAMPLER) {
Briancec81ee2007-03-07 08:04:06 -07001142 if (shProg->VertexProgram)
1143 _slang_resolve_samplers(shProg, &shProg->VertexProgram->Base);
1144 if (shProg->FragmentProgram)
1145 _slang_resolve_samplers(shProg, &shProg->FragmentProgram->Base);
Brian5cf73262007-01-05 16:02:45 -07001146 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1147 }
Brian34ae99d2006-12-18 08:28:54 -07001148}
1149
1150
1151/**
Brian5b01c5e2006-12-19 18:02:03 -07001152 * Called by ctx->Driver.UniformMatrix().
Brian34ae99d2006-12-18 08:28:54 -07001153 */
Brian5b01c5e2006-12-19 18:02:03 -07001154void
1155_mesa_uniform_matrix(GLcontext *ctx, GLint cols, GLint rows,
1156 GLenum matrixType, GLint location, GLsizei count,
1157 GLboolean transpose, const GLfloat *values)
Brian34ae99d2006-12-18 08:28:54 -07001158{
Brian3a8e2772006-12-20 17:19:16 -07001159 struct gl_shader_program *shProg = ctx->Shader.CurrentProgram;
1160 if (!shProg || !shProg->LinkStatus) {
1161 _mesa_error(ctx, GL_INVALID_OPERATION,
1162 "glUniformMatrix(program not linked)");
1163 return;
1164 }
1165 if (location < 0 || location >= shProg->Uniforms->NumParameters) {
1166 _mesa_error(ctx, GL_INVALID_VALUE, "glUniformMatrix(location)");
1167 return;
1168 }
Brian34ae99d2006-12-18 08:28:54 -07001169 if (values == NULL) {
Brian3a8e2772006-12-20 17:19:16 -07001170 _mesa_error(ctx, GL_INVALID_VALUE, "glUniformMatrix");
Brian34ae99d2006-12-18 08:28:54 -07001171 return;
1172 }
1173
1174 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
1175
Brian3a8e2772006-12-20 17:19:16 -07001176 /*
1177 * Note: the _columns_ of a matrix are stored in program registers, not
1178 * the rows.
1179 */
1180 /* XXXX need to test 3x3 and 2x2 matrices... */
Brian34ae99d2006-12-18 08:28:54 -07001181 if (transpose) {
Brian3a8e2772006-12-20 17:19:16 -07001182 GLuint row, col;
1183 for (col = 0; col < cols; col++) {
1184 GLfloat *v = shProg->Uniforms->ParameterValues[location + col];
1185 for (row = 0; row < rows; row++) {
Brian9f442472007-03-09 11:34:18 -07001186 v[row] = values[row * cols + col];
Brian34ae99d2006-12-18 08:28:54 -07001187 }
Brian34ae99d2006-12-18 08:28:54 -07001188 }
Brian34ae99d2006-12-18 08:28:54 -07001189 }
1190 else {
Brian3a8e2772006-12-20 17:19:16 -07001191 GLuint row, col;
1192 for (col = 0; col < cols; col++) {
1193 GLfloat *v = shProg->Uniforms->ParameterValues[location + col];
1194 for (row = 0; row < rows; row++) {
Brian9f442472007-03-09 11:34:18 -07001195 v[row] = values[col * rows + row];
Brian3a8e2772006-12-20 17:19:16 -07001196 }
1197 }
Brian34ae99d2006-12-18 08:28:54 -07001198 }
1199}
1200
1201
Brian5b01c5e2006-12-19 18:02:03 -07001202void
1203_mesa_validate_program(GLcontext *ctx, GLuint program)
Brian34ae99d2006-12-18 08:28:54 -07001204{
Brian65a18442006-12-19 18:46:56 -07001205 struct gl_shader_program *shProg;
1206 shProg = _mesa_lookup_shader_program(ctx, program);
1207 if (!shProg) {
Brian43975832007-01-04 08:21:09 -07001208 _mesa_error(ctx, GL_INVALID_VALUE, "glValidateProgram(program)");
Brian34ae99d2006-12-18 08:28:54 -07001209 return;
1210 }
Brian5b01c5e2006-12-19 18:02:03 -07001211 /* XXX temporary */
Brian65a18442006-12-19 18:46:56 -07001212 shProg->Validated = GL_TRUE;
Brian34ae99d2006-12-18 08:28:54 -07001213
Brian5b01c5e2006-12-19 18:02:03 -07001214 /* From the GL spec:
1215 any two active samplers in the current program object are of
1216 different types, but refer to the same texture image unit,
1217
1218 any active sampler in the current program object refers to a texture
1219 image unit where fixed-function fragment processing accesses a
1220 texture target that does not match the sampler type, or
1221
1222 the sum of the number of active samplers in the program and the
1223 number of texture image units enabled for fixed-function fragment
1224 processing exceeds the combined limit on the total number of texture
1225 image units allowed.
1226 */
Brian34ae99d2006-12-18 08:28:54 -07001227}