blob: 82c2b857fff4bb63468f61cbd372ad3db0422531 [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/**
Brian2761cfc2007-12-20 09:05:37 -0700372 * Return size (in floats) of the given GLSL type.
373 * See also _slang_sizeof_type_specifier().
374 */
375static GLint
376sizeof_glsl_type(GLenum type)
377{
378 switch (type) {
379 case GL_BOOL:
380 case GL_FLOAT:
381 case GL_INT:
382 return 1;
383 case GL_BOOL_VEC2:
384 case GL_FLOAT_VEC2:
385 case GL_INT_VEC2:
386 return 2;
387 case GL_BOOL_VEC3:
388 case GL_FLOAT_VEC3:
389 case GL_INT_VEC3:
390 return 3;
391 case GL_BOOL_VEC4:
392 case GL_FLOAT_VEC4:
393 case GL_INT_VEC4:
394 return 4;
395 case GL_FLOAT_MAT2:
396 return 8; /* 2 rows of 4, actually */
397 case GL_FLOAT_MAT3:
398 return 12; /* 3 rows of 4, actually */
399 case GL_FLOAT_MAT4:
400 return 16;
401 case GL_FLOAT_MAT2x3:
402 return 6;
403 case GL_FLOAT_MAT2x4:
404 return 8;
405 case GL_FLOAT_MAT3x2:
406 return 12; /* 3 rows of 4, actually */
407 case GL_FLOAT_MAT3x4:
408 return 12;
409 case GL_FLOAT_MAT4x2:
410 return 16; /* 4 rows of 4, actually */
411 case GL_FLOAT_MAT4x3:
412 return 12;
413 default:
414 return 0; /* error */
415 }
416}
417
418
419/**
Brian5b01c5e2006-12-19 18:02:03 -0700420 * Called via ctx->Driver.AttachShader()
421 */
422void
423_mesa_attach_shader(GLcontext *ctx, GLuint program, GLuint shader)
424{
Brian65a18442006-12-19 18:46:56 -0700425 struct gl_shader_program *shProg
426 = _mesa_lookup_shader_program(ctx, program);
427 struct gl_shader *sh = _mesa_lookup_shader(ctx, shader);
Brian237b9852007-08-07 21:48:31 +0100428 GLuint n;
Brian5b01c5e2006-12-19 18:02:03 -0700429 GLuint i;
430
Brian65a18442006-12-19 18:46:56 -0700431 if (!shProg || !sh) {
Brian43975832007-01-04 08:21:09 -0700432 _mesa_error(ctx, GL_INVALID_VALUE,
Brian5b01c5e2006-12-19 18:02:03 -0700433 "glAttachShader(bad program or shader name)");
434 return;
435 }
436
Brian237b9852007-08-07 21:48:31 +0100437 n = shProg->NumShaders;
438
Brian5b01c5e2006-12-19 18:02:03 -0700439 for (i = 0; i < n; i++) {
Brian65a18442006-12-19 18:46:56 -0700440 if (shProg->Shaders[i] == sh) {
Brian5b01c5e2006-12-19 18:02:03 -0700441 /* already attached */
442 return;
Brian34ae99d2006-12-18 08:28:54 -0700443 }
444 }
Brian5b01c5e2006-12-19 18:02:03 -0700445
446 /* grow list */
Brian65a18442006-12-19 18:46:56 -0700447 shProg->Shaders = (struct gl_shader **)
448 _mesa_realloc(shProg->Shaders,
449 n * sizeof(struct gl_shader *),
450 (n + 1) * sizeof(struct gl_shader *));
451 if (!shProg->Shaders) {
Brian5b01c5e2006-12-19 18:02:03 -0700452 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAttachShader");
453 return;
454 }
455
456 /* append */
Brian3c008a02007-04-12 15:22:32 -0600457 shProg->Shaders[n] = NULL; /* since realloc() didn't zero the new space */
458 _mesa_reference_shader(ctx, &shProg->Shaders[n], sh);
Brian65a18442006-12-19 18:46:56 -0700459 shProg->NumShaders++;
Brian5b01c5e2006-12-19 18:02:03 -0700460}
461
462
463void
464_mesa_bind_attrib_location(GLcontext *ctx, GLuint program, GLuint index,
465 const GLchar *name)
466{
Brian65a18442006-12-19 18:46:56 -0700467 struct gl_shader_program *shProg
468 = _mesa_lookup_shader_program(ctx, program);
Brianb7978af2007-01-09 19:17:17 -0700469 const GLint size = -1; /* unknown size */
470 GLint i, oldIndex;
Brian5b01c5e2006-12-19 18:02:03 -0700471
Brian65a18442006-12-19 18:46:56 -0700472 if (!shProg) {
Brian43975832007-01-04 08:21:09 -0700473 _mesa_error(ctx, GL_INVALID_VALUE, "glBindAttribLocation(program)");
Brian5b01c5e2006-12-19 18:02:03 -0700474 return;
475 }
476
Brian9e4bae92006-12-20 09:27:42 -0700477 if (!name)
478 return;
479
480 if (strncmp(name, "gl_", 3) == 0) {
481 _mesa_error(ctx, GL_INVALID_OPERATION,
482 "glBindAttribLocation(illegal name)");
483 return;
484 }
485
Brian9f660252007-04-11 09:00:56 -0600486 if (shProg->LinkStatus) {
487 /* get current index/location for the attribute */
488 oldIndex = _mesa_get_attrib_location(ctx, program, name);
489 }
490 else {
491 oldIndex = -1;
492 }
Brian3209c3e2007-01-09 17:49:24 -0700493
494 /* this will replace the current value if it's already in the list */
Brianb7978af2007-01-09 19:17:17 -0700495 i = _mesa_add_attribute(shProg->Attributes, name, size, index);
Brian3209c3e2007-01-09 17:49:24 -0700496 if (i < 0) {
497 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindAttribLocation");
498 }
499
Brian9f660252007-04-11 09:00:56 -0600500 if (shProg->VertexProgram && oldIndex >= 0 && oldIndex != index) {
501 /* If the index changed, need to search/replace references to that attribute
502 * in the vertex program.
503 */
Brian3209c3e2007-01-09 17:49:24 -0700504 _slang_remap_attribute(&shProg->VertexProgram->Base, oldIndex, index);
505 }
Brian34ae99d2006-12-18 08:28:54 -0700506}
507
508
Brian5b01c5e2006-12-19 18:02:03 -0700509GLuint
510_mesa_create_shader(GLcontext *ctx, GLenum type)
511{
Brian65a18442006-12-19 18:46:56 -0700512 struct gl_shader *sh;
Brian5b01c5e2006-12-19 18:02:03 -0700513 GLuint name;
514
515 name = _mesa_HashFindFreeKeyBlock(ctx->Shared->ShaderObjects, 1);
516
517 switch (type) {
Brian65a18442006-12-19 18:46:56 -0700518 case GL_FRAGMENT_SHADER:
519 case GL_VERTEX_SHADER:
520 sh = _mesa_new_shader(ctx, name, type);
Brian5b01c5e2006-12-19 18:02:03 -0700521 break;
522 default:
523 _mesa_error(ctx, GL_INVALID_ENUM, "CreateShader(type)");
524 return 0;
525 }
526
Brian65a18442006-12-19 18:46:56 -0700527 _mesa_HashInsert(ctx->Shared->ShaderObjects, name, sh);
Brian5b01c5e2006-12-19 18:02:03 -0700528
529 return name;
530}
531
532
533GLuint
534_mesa_create_program(GLcontext *ctx)
535{
536 GLuint name;
Brian65a18442006-12-19 18:46:56 -0700537 struct gl_shader_program *shProg;
Brian5b01c5e2006-12-19 18:02:03 -0700538
Brian65a18442006-12-19 18:46:56 -0700539 name = _mesa_HashFindFreeKeyBlock(ctx->Shared->ShaderObjects, 1);
540 shProg = _mesa_new_shader_program(ctx, name);
Brian5b01c5e2006-12-19 18:02:03 -0700541
Brian65a18442006-12-19 18:46:56 -0700542 _mesa_HashInsert(ctx->Shared->ShaderObjects, name, shProg);
Brian5b01c5e2006-12-19 18:02:03 -0700543
Brian3c008a02007-04-12 15:22:32 -0600544 assert(shProg->RefCount == 1);
545
Brian5b01c5e2006-12-19 18:02:03 -0700546 return name;
547}
548
549
Brian3c008a02007-04-12 15:22:32 -0600550/**
551 * Named w/ "2" to indicate OpenGL 2.x vs GL_ARB_fragment_programs's
552 * DeleteProgramARB.
553 */
Brian5b01c5e2006-12-19 18:02:03 -0700554void
555_mesa_delete_program2(GLcontext *ctx, GLuint name)
556{
Brian3c008a02007-04-12 15:22:32 -0600557 /*
558 * NOTE: deleting shaders/programs works a bit differently than
559 * texture objects (and buffer objects, etc). Shader/program
560 * handles/IDs exist in the hash table until the object is really
561 * deleted (refcount==0). With texture objects, the handle/ID is
562 * removed from the hash table in glDeleteTextures() while the tex
563 * object itself might linger until its refcount goes to zero.
564 */
Brian65a18442006-12-19 18:46:56 -0700565 struct gl_shader_program *shProg;
Brian5b01c5e2006-12-19 18:02:03 -0700566
Brian65a18442006-12-19 18:46:56 -0700567 shProg = _mesa_lookup_shader_program(ctx, name);
568 if (!shProg) {
Brian43975832007-01-04 08:21:09 -0700569 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteProgram(name)");
Brian5b01c5e2006-12-19 18:02:03 -0700570 return;
571 }
572
Brian9e4bae92006-12-20 09:27:42 -0700573 shProg->DeletePending = GL_TRUE;
574
Brian3c008a02007-04-12 15:22:32 -0600575 /* effectively, decr shProg's refcount */
576 _mesa_reference_shader_program(ctx, &shProg, NULL);
Brian5b01c5e2006-12-19 18:02:03 -0700577}
578
579
580void
581_mesa_delete_shader(GLcontext *ctx, GLuint shader)
582{
Brian9e4bae92006-12-20 09:27:42 -0700583 struct gl_shader *sh = _mesa_lookup_shader(ctx, shader);
584 if (!sh) {
585 return;
586 }
Brian5b01c5e2006-12-19 18:02:03 -0700587
Brian9e4bae92006-12-20 09:27:42 -0700588 sh->DeletePending = GL_TRUE;
Brian3c008a02007-04-12 15:22:32 -0600589
590 /* effectively, decr sh's refcount */
591 _mesa_reference_shader(ctx, &sh, NULL);
Brian5b01c5e2006-12-19 18:02:03 -0700592}
593
594
595void
596_mesa_detach_shader(GLcontext *ctx, GLuint program, GLuint shader)
597{
Brian65a18442006-12-19 18:46:56 -0700598 struct gl_shader_program *shProg
599 = _mesa_lookup_shader_program(ctx, program);
Brian237b9852007-08-07 21:48:31 +0100600 GLuint n;
Brian5b01c5e2006-12-19 18:02:03 -0700601 GLuint i, j;
602
Brian65a18442006-12-19 18:46:56 -0700603 if (!shProg) {
Brian43975832007-01-04 08:21:09 -0700604 _mesa_error(ctx, GL_INVALID_VALUE,
Brian5b01c5e2006-12-19 18:02:03 -0700605 "glDetachShader(bad program or shader name)");
606 return;
607 }
608
Brian237b9852007-08-07 21:48:31 +0100609 n = shProg->NumShaders;
610
Brian5b01c5e2006-12-19 18:02:03 -0700611 for (i = 0; i < n; i++) {
Brian65a18442006-12-19 18:46:56 -0700612 if (shProg->Shaders[i]->Name == shader) {
Brian5b01c5e2006-12-19 18:02:03 -0700613 /* found it */
Brian3c008a02007-04-12 15:22:32 -0600614 struct gl_shader **newList;
Brian9e4bae92006-12-20 09:27:42 -0700615
Brian3c008a02007-04-12 15:22:32 -0600616 /* derefernce */
617 _mesa_reference_shader(ctx, &shProg->Shaders[i], NULL);
Brian9e4bae92006-12-20 09:27:42 -0700618
Brian5b01c5e2006-12-19 18:02:03 -0700619 /* alloc new, smaller array */
Brian65a18442006-12-19 18:46:56 -0700620 newList = (struct gl_shader **)
621 _mesa_malloc((n - 1) * sizeof(struct gl_shader *));
Brian5b01c5e2006-12-19 18:02:03 -0700622 if (!newList) {
623 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDetachShader");
624 return;
625 }
626 for (j = 0; j < i; j++) {
Brian65a18442006-12-19 18:46:56 -0700627 newList[j] = shProg->Shaders[j];
Brian5b01c5e2006-12-19 18:02:03 -0700628 }
629 while (++i < n)
Brian65a18442006-12-19 18:46:56 -0700630 newList[j++] = shProg->Shaders[i];
631 _mesa_free(shProg->Shaders);
Brian5b01c5e2006-12-19 18:02:03 -0700632
Brian65a18442006-12-19 18:46:56 -0700633 shProg->Shaders = newList;
Brianbac15c82007-04-18 14:55:18 -0600634 shProg->NumShaders = n - 1;
Brianaaa57412007-04-18 15:22:43 -0600635
636#ifdef DEBUG
637 /* sanity check */
638 {
639 for (j = 0; j < shProg->NumShaders; j++) {
640 assert(shProg->Shaders[j]->Type == GL_VERTEX_SHADER ||
641 shProg->Shaders[j]->Type == GL_FRAGMENT_SHADER);
642 assert(shProg->Shaders[j]->RefCount > 0);
643 }
644 }
645#endif
646
Brian5b01c5e2006-12-19 18:02:03 -0700647 return;
648 }
649 }
650
651 /* not found */
Brian43975832007-01-04 08:21:09 -0700652 _mesa_error(ctx, GL_INVALID_VALUE,
Brian5b01c5e2006-12-19 18:02:03 -0700653 "glDetachShader(shader not found)");
654}
655
656
657void
658_mesa_get_active_attrib(GLcontext *ctx, GLuint program, GLuint index,
659 GLsizei maxLength, GLsizei *length, GLint *size,
660 GLenum *type, GLchar *nameOut)
661{
662 static const GLenum vec_types[] = {
663 GL_FLOAT, GL_FLOAT_VEC2, GL_FLOAT_VEC3, GL_FLOAT_VEC4
664 };
Brian65a18442006-12-19 18:46:56 -0700665 struct gl_shader_program *shProg
666 = _mesa_lookup_shader_program(ctx, program);
Brian5b01c5e2006-12-19 18:02:03 -0700667 GLint sz;
668
Brian65a18442006-12-19 18:46:56 -0700669 if (!shProg) {
Brianaaa57412007-04-18 15:22:43 -0600670 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveAttrib");
Brian5b01c5e2006-12-19 18:02:03 -0700671 return;
672 }
673
Brian65a18442006-12-19 18:46:56 -0700674 if (!shProg->Attributes || index >= shProg->Attributes->NumParameters) {
Brianaaa57412007-04-18 15:22:43 -0600675 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveAttrib(index)");
Brian5b01c5e2006-12-19 18:02:03 -0700676 return;
677 }
678
679 copy_string(nameOut, maxLength, length,
Brian65a18442006-12-19 18:46:56 -0700680 shProg->Attributes->Parameters[index].Name);
681 sz = shProg->Attributes->Parameters[index].Size;
Brian5b01c5e2006-12-19 18:02:03 -0700682 if (size)
683 *size = sz;
684 if (type)
685 *type = vec_types[sz]; /* XXX this is a temporary hack */
686}
687
688
689/**
690 * Called via ctx->Driver.GetActiveUniform().
691 */
692void
693_mesa_get_active_uniform(GLcontext *ctx, GLuint program, GLuint index,
694 GLsizei maxLength, GLsizei *length, GLint *size,
695 GLenum *type, GLchar *nameOut)
696{
Brian65a18442006-12-19 18:46:56 -0700697 struct gl_shader_program *shProg
698 = _mesa_lookup_shader_program(ctx, program);
Brian274ac7a2007-04-18 16:05:53 -0600699 GLuint ind, j;
Brian5b01c5e2006-12-19 18:02:03 -0700700
Brian65a18442006-12-19 18:46:56 -0700701 if (!shProg) {
Brian43975832007-01-04 08:21:09 -0700702 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveUniform");
Brian5b01c5e2006-12-19 18:02:03 -0700703 return;
704 }
705
Brian65a18442006-12-19 18:46:56 -0700706 if (!shProg->Uniforms || index >= shProg->Uniforms->NumParameters) {
Brian5b01c5e2006-12-19 18:02:03 -0700707 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveUniform(index)");
708 return;
709 }
710
Brian274ac7a2007-04-18 16:05:53 -0600711 ind = 0;
712 for (j = 0; j < shProg->Uniforms->NumParameters; j++) {
713 if (shProg->Uniforms->Parameters[j].Type == PROGRAM_UNIFORM ||
714 shProg->Uniforms->Parameters[j].Type == PROGRAM_SAMPLER) {
715 if (ind == index) {
Brian2761cfc2007-12-20 09:05:37 -0700716 GLuint uSize = shProg->Uniforms->Parameters[j].Size;
717 GLenum uType = shProg->Uniforms->Parameters[j].DataType;
Brian274ac7a2007-04-18 16:05:53 -0600718 /* found it */
719 copy_string(nameOut, maxLength, length,
720 shProg->Uniforms->Parameters[j].Name);
Brian2761cfc2007-12-20 09:05:37 -0700721 if (size) {
722 /* convert from floats to 'type' (eg: sizeof(mat4x4)=1) */
723 *size = uSize / sizeof_glsl_type(uType);
724 }
Brian274ac7a2007-04-18 16:05:53 -0600725 if (type)
Brian2761cfc2007-12-20 09:05:37 -0700726 *type = uType;
Brian274ac7a2007-04-18 16:05:53 -0600727 return;
728 }
729 ind++;
730 }
731 }
732
733 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveUniform(index)");
Brian5b01c5e2006-12-19 18:02:03 -0700734}
735
736
737/**
738 * Called via ctx->Driver.GetAttachedShaders().
739 */
740void
741_mesa_get_attached_shaders(GLcontext *ctx, GLuint program, GLsizei maxCount,
742 GLsizei *count, GLuint *obj)
743{
Brian65a18442006-12-19 18:46:56 -0700744 struct gl_shader_program *shProg
745 = _mesa_lookup_shader_program(ctx, program);
746 if (shProg) {
Brian223d7cb2007-01-23 16:37:51 -0700747 GLint i;
Brian65a18442006-12-19 18:46:56 -0700748 for (i = 0; i < maxCount && i < shProg->NumShaders; i++) {
749 obj[i] = shProg->Shaders[i]->Name;
Brian5b01c5e2006-12-19 18:02:03 -0700750 }
751 if (count)
752 *count = i;
753 }
754 else {
Brian43975832007-01-04 08:21:09 -0700755 _mesa_error(ctx, GL_INVALID_VALUE, "glGetAttachedShaders");
Brian5b01c5e2006-12-19 18:02:03 -0700756 }
757}
758
759
760GLint
761_mesa_get_attrib_location(GLcontext *ctx, GLuint program,
762 const GLchar *name)
763{
Brian65a18442006-12-19 18:46:56 -0700764 struct gl_shader_program *shProg
765 = _mesa_lookup_shader_program(ctx, program);
Brian5b01c5e2006-12-19 18:02:03 -0700766
Brian65a18442006-12-19 18:46:56 -0700767 if (!shProg) {
Brian43975832007-01-04 08:21:09 -0700768 _mesa_error(ctx, GL_INVALID_VALUE, "glGetAttribLocation");
Brian5b01c5e2006-12-19 18:02:03 -0700769 return -1;
770 }
771
Brian65a18442006-12-19 18:46:56 -0700772 if (!shProg->LinkStatus) {
Brian5b01c5e2006-12-19 18:02:03 -0700773 _mesa_error(ctx, GL_INVALID_OPERATION,
774 "glGetAttribLocation(program not linked)");
775 return -1;
776 }
777
778 if (!name)
779 return -1;
780
Brian65a18442006-12-19 18:46:56 -0700781 if (shProg->Attributes) {
Brian3209c3e2007-01-09 17:49:24 -0700782 GLint i = _mesa_lookup_parameter_index(shProg->Attributes, -1, name);
783 if (i >= 0) {
Brian01a91eb2007-01-09 19:26:22 -0700784 return shProg->Attributes->Parameters[i].StateIndexes[0];
Brian5b01c5e2006-12-19 18:02:03 -0700785 }
786 }
787 return -1;
788}
789
790
791GLuint
792_mesa_get_handle(GLcontext *ctx, GLenum pname)
Brian34ae99d2006-12-18 08:28:54 -0700793{
794#if 0
795 GET_CURRENT_CONTEXT(ctx);
796
797 switch (pname) {
798 case GL_PROGRAM_OBJECT_ARB:
799 {
Brian5b01c5e2006-12-19 18:02:03 -0700800 struct gl2_program_intf **pro = ctx->Shader.CurrentProgram;
Brian34ae99d2006-12-18 08:28:54 -0700801
802 if (pro != NULL)
803 return (**pro)._container._generic.
804 GetName((struct gl2_generic_intf **) (pro));
805 }
806 break;
807 default:
808 _mesa_error(ctx, GL_INVALID_ENUM, "glGetHandleARB");
809 }
810#endif
811 return 0;
812}
813
814
Brian5b01c5e2006-12-19 18:02:03 -0700815void
816_mesa_get_programiv(GLcontext *ctx, GLuint program,
817 GLenum pname, GLint *params)
Brian34ae99d2006-12-18 08:28:54 -0700818{
Brian65a18442006-12-19 18:46:56 -0700819 struct gl_shader_program *shProg
820 = _mesa_lookup_shader_program(ctx, program);
Brian34ae99d2006-12-18 08:28:54 -0700821
Brian65a18442006-12-19 18:46:56 -0700822 if (!shProg) {
Brian5b01c5e2006-12-19 18:02:03 -0700823 _mesa_error(ctx, GL_INVALID_VALUE, "glGetProgramiv(program)");
Brian34ae99d2006-12-18 08:28:54 -0700824 return;
825 }
826
Brian5b01c5e2006-12-19 18:02:03 -0700827 switch (pname) {
828 case GL_DELETE_STATUS:
Brian65a18442006-12-19 18:46:56 -0700829 *params = shProg->DeletePending;
Brian5b01c5e2006-12-19 18:02:03 -0700830 break;
831 case GL_LINK_STATUS:
Brian65a18442006-12-19 18:46:56 -0700832 *params = shProg->LinkStatus;
Brian34ae99d2006-12-18 08:28:54 -0700833 break;
Brian5b01c5e2006-12-19 18:02:03 -0700834 case GL_VALIDATE_STATUS:
Brian65a18442006-12-19 18:46:56 -0700835 *params = shProg->Validated;
Brian5b01c5e2006-12-19 18:02:03 -0700836 break;
837 case GL_INFO_LOG_LENGTH:
Jan Dvorak5a0f02a2007-07-13 16:36:00 -0600838 *params = shProg->InfoLog ? strlen(shProg->InfoLog) + 1 : 0;
Brian5b01c5e2006-12-19 18:02:03 -0700839 break;
840 case GL_ATTACHED_SHADERS:
Brian65a18442006-12-19 18:46:56 -0700841 *params = shProg->NumShaders;
Brian5b01c5e2006-12-19 18:02:03 -0700842 break;
843 case GL_ACTIVE_ATTRIBUTES:
Brian3209c3e2007-01-09 17:49:24 -0700844 *params = shProg->Attributes ? shProg->Attributes->NumParameters : 0;
Brian5b01c5e2006-12-19 18:02:03 -0700845 break;
846 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH:
Brian274ac7a2007-04-18 16:05:53 -0600847 *params = _mesa_longest_parameter_name(shProg->Attributes,
848 PROGRAM_INPUT) + 1;
Brian5b01c5e2006-12-19 18:02:03 -0700849 break;
850 case GL_ACTIVE_UNIFORMS:
Brian274ac7a2007-04-18 16:05:53 -0600851 *params
852 = _mesa_num_parameters_of_type(shProg->Uniforms, PROGRAM_UNIFORM)
853 + _mesa_num_parameters_of_type(shProg->Uniforms, PROGRAM_SAMPLER);
Brian5b01c5e2006-12-19 18:02:03 -0700854 break;
855 case GL_ACTIVE_UNIFORM_MAX_LENGTH:
Brian274ac7a2007-04-18 16:05:53 -0600856 *params = MAX2(
857 _mesa_longest_parameter_name(shProg->Uniforms, PROGRAM_UNIFORM),
858 _mesa_longest_parameter_name(shProg->Uniforms, PROGRAM_SAMPLER));
859 if (*params > 0)
860 (*params)++; /* add one for terminating zero */
Brian34ae99d2006-12-18 08:28:54 -0700861 break;
862 default:
Brian5b01c5e2006-12-19 18:02:03 -0700863 _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramiv(pname)");
864 return;
Brian34ae99d2006-12-18 08:28:54 -0700865 }
Brian5b01c5e2006-12-19 18:02:03 -0700866}
Brian34ae99d2006-12-18 08:28:54 -0700867
Brian34ae99d2006-12-18 08:28:54 -0700868
Brian5b01c5e2006-12-19 18:02:03 -0700869void
870_mesa_get_shaderiv(GLcontext *ctx, GLuint name, GLenum pname, GLint *params)
871{
Brian65a18442006-12-19 18:46:56 -0700872 struct gl_shader *shader = _mesa_lookup_shader(ctx, name);
Brian5b01c5e2006-12-19 18:02:03 -0700873
874 if (!shader) {
875 _mesa_error(ctx, GL_INVALID_VALUE, "glGetShaderiv(shader)");
876 return;
877 }
Brian65a18442006-12-19 18:46:56 -0700878
Brian5b01c5e2006-12-19 18:02:03 -0700879 switch (pname) {
880 case GL_SHADER_TYPE:
881 *params = shader->Type;
882 break;
883 case GL_DELETE_STATUS:
884 *params = shader->DeletePending;
885 break;
886 case GL_COMPILE_STATUS:
887 *params = shader->CompileStatus;
888 break;
889 case GL_INFO_LOG_LENGTH:
Jan Dvorak5a0f02a2007-07-13 16:36:00 -0600890 *params = shader->InfoLog ? strlen(shader->InfoLog) + 1 : 0;
Brian5b01c5e2006-12-19 18:02:03 -0700891 break;
892 case GL_SHADER_SOURCE_LENGTH:
Jan Dvorak5a0f02a2007-07-13 16:36:00 -0600893 *params = shader->Source ? strlen((char *) shader->Source) + 1 : 0;
Brian5b01c5e2006-12-19 18:02:03 -0700894 break;
895 default:
896 _mesa_error(ctx, GL_INVALID_ENUM, "glGetShaderiv(pname)");
897 return;
898 }
899}
900
901
902void
903_mesa_get_program_info_log(GLcontext *ctx, GLuint program, GLsizei bufSize,
904 GLsizei *length, GLchar *infoLog)
905{
Brian65a18442006-12-19 18:46:56 -0700906 struct gl_shader_program *shProg
907 = _mesa_lookup_shader_program(ctx, program);
908 if (!shProg) {
Brian5b01c5e2006-12-19 18:02:03 -0700909 _mesa_error(ctx, GL_INVALID_VALUE, "glGetProgramInfoLog(program)");
910 return;
911 }
Brian65a18442006-12-19 18:46:56 -0700912 copy_string(infoLog, bufSize, length, shProg->InfoLog);
Brian5b01c5e2006-12-19 18:02:03 -0700913}
914
915
916void
917_mesa_get_shader_info_log(GLcontext *ctx, GLuint shader, GLsizei bufSize,
918 GLsizei *length, GLchar *infoLog)
919{
Brian65a18442006-12-19 18:46:56 -0700920 struct gl_shader *sh = _mesa_lookup_shader(ctx, shader);
921 if (!sh) {
Brian5b01c5e2006-12-19 18:02:03 -0700922 _mesa_error(ctx, GL_INVALID_VALUE, "glGetShaderInfoLog(shader)");
923 return;
924 }
Brian65a18442006-12-19 18:46:56 -0700925 copy_string(infoLog, bufSize, length, sh->InfoLog);
Brian5b01c5e2006-12-19 18:02:03 -0700926}
927
928
929/**
930 * Called via ctx->Driver.GetShaderSource().
931 */
932void
933_mesa_get_shader_source(GLcontext *ctx, GLuint shader, GLsizei maxLength,
934 GLsizei *length, GLchar *sourceOut)
935{
Brian65a18442006-12-19 18:46:56 -0700936 struct gl_shader *sh = _mesa_lookup_shader(ctx, shader);
937 if (!sh) {
Brian5b01c5e2006-12-19 18:02:03 -0700938 _mesa_error(ctx, GL_INVALID_VALUE, "glGetShaderSource(shader)");
939 return;
940 }
Brian65a18442006-12-19 18:46:56 -0700941 copy_string(sourceOut, maxLength, length, sh->Source);
Brian5b01c5e2006-12-19 18:02:03 -0700942}
943
944
945/**
946 * Called via ctx->Driver.GetUniformfv().
947 */
948void
949_mesa_get_uniformfv(GLcontext *ctx, GLuint program, GLint location,
950 GLfloat *params)
951{
Brian65a18442006-12-19 18:46:56 -0700952 struct gl_shader_program *shProg
953 = _mesa_lookup_shader_program(ctx, program);
954 if (shProg) {
Brian223d7cb2007-01-23 16:37:51 -0700955 GLint i;
Brian65a18442006-12-19 18:46:56 -0700956 if (location >= 0 && location < shProg->Uniforms->NumParameters) {
957 for (i = 0; i < shProg->Uniforms->Parameters[location].Size; i++) {
958 params[i] = shProg->Uniforms->ParameterValues[location][i];
Brian5b01c5e2006-12-19 18:02:03 -0700959 }
960 }
961 else {
962 _mesa_error(ctx, GL_INVALID_VALUE, "glGetUniformfv(location)");
963 }
964 }
965 else {
Brian43975832007-01-04 08:21:09 -0700966 _mesa_error(ctx, GL_INVALID_VALUE, "glGetUniformfv(program)");
Brian5b01c5e2006-12-19 18:02:03 -0700967 }
968}
969
970
971/**
972 * Called via ctx->Driver.GetUniformLocation().
973 */
974GLint
975_mesa_get_uniform_location(GLcontext *ctx, GLuint program, const GLchar *name)
976{
Brian71623982007-01-30 16:55:03 -0700977 struct gl_shader_program *shProg
978 = _mesa_lookup_shader_program(ctx, program);
979 if (shProg) {
Brian5b01c5e2006-12-19 18:02:03 -0700980 GLuint loc;
Brian65a18442006-12-19 18:46:56 -0700981 for (loc = 0; loc < shProg->Uniforms->NumParameters; loc++) {
Brian5b01c5e2006-12-19 18:02:03 -0700982 const struct gl_program_parameter *u
Brian65a18442006-12-19 18:46:56 -0700983 = shProg->Uniforms->Parameters + loc;
Brian29053852006-12-21 11:21:26 -0700984 /* XXX this is a temporary simplification / short-cut.
985 * We need to handle things like "e.c[0].b" as seen in the
986 * GLSL orange book, page 189.
987 */
Brian5cf73262007-01-05 16:02:45 -0700988 if ((u->Type == PROGRAM_UNIFORM ||
989 u->Type == PROGRAM_SAMPLER) && !strcmp(u->Name, name)) {
Brian5b01c5e2006-12-19 18:02:03 -0700990 return loc;
991 }
992 }
993 }
994 return -1;
995
996}
997
998
999GLboolean
1000_mesa_is_program(GLcontext *ctx, GLuint name)
1001{
Brian65a18442006-12-19 18:46:56 -07001002 struct gl_shader_program *shProg = _mesa_lookup_shader_program(ctx, name);
1003 return shProg ? GL_TRUE : GL_FALSE;
Brian5b01c5e2006-12-19 18:02:03 -07001004}
1005
1006
1007GLboolean
1008_mesa_is_shader(GLcontext *ctx, GLuint name)
1009{
Brian65a18442006-12-19 18:46:56 -07001010 struct gl_shader *shader = _mesa_lookup_shader(ctx, name);
Brian5b01c5e2006-12-19 18:02:03 -07001011 return shader ? GL_TRUE : GL_FALSE;
Brian34ae99d2006-12-18 08:28:54 -07001012}
1013
1014
1015
Brian5b01c5e2006-12-19 18:02:03 -07001016/**
1017 * Called via ctx->Driver.ShaderSource()
1018 */
1019void
1020_mesa_shader_source(GLcontext *ctx, GLuint shader, const GLchar *source)
Brian34ae99d2006-12-18 08:28:54 -07001021{
Brian65a18442006-12-19 18:46:56 -07001022 struct gl_shader *sh = _mesa_lookup_shader(ctx, shader);
1023 if (!sh) {
Brian5b01c5e2006-12-19 18:02:03 -07001024 _mesa_error(ctx, GL_INVALID_VALUE, "glShaderSource(shaderObj)");
Brian34ae99d2006-12-18 08:28:54 -07001025 return;
1026 }
1027
Brian34ae99d2006-12-18 08:28:54 -07001028 /* free old shader source string and install new one */
Brian65a18442006-12-19 18:46:56 -07001029 if (sh->Source) {
1030 _mesa_free((void *) sh->Source);
Brian34ae99d2006-12-18 08:28:54 -07001031 }
Brian65a18442006-12-19 18:46:56 -07001032 sh->Source = source;
Brian9e4bae92006-12-20 09:27:42 -07001033 sh->CompileStatus = GL_FALSE;
Brian34ae99d2006-12-18 08:28:54 -07001034}
1035
1036
Brian5b01c5e2006-12-19 18:02:03 -07001037/**
1038 * Called via ctx->Driver.CompileShader()
1039 */
1040void
1041_mesa_compile_shader(GLcontext *ctx, GLuint shaderObj)
Brian34ae99d2006-12-18 08:28:54 -07001042{
Brian65a18442006-12-19 18:46:56 -07001043 struct gl_shader *sh = _mesa_lookup_shader(ctx, shaderObj);
Brian34ae99d2006-12-18 08:28:54 -07001044
Brian65a18442006-12-19 18:46:56 -07001045 if (!sh) {
Brian34ae99d2006-12-18 08:28:54 -07001046 _mesa_error(ctx, GL_INVALID_VALUE, "glCompileShader(shaderObj)");
1047 return;
1048 }
1049
Brian43975832007-01-04 08:21:09 -07001050 sh->CompileStatus = _slang_compile(ctx, sh);
Brian34ae99d2006-12-18 08:28:54 -07001051}
1052
1053
Brian5b01c5e2006-12-19 18:02:03 -07001054/**
1055 * Called via ctx->Driver.LinkProgram()
1056 */
1057void
1058_mesa_link_program(GLcontext *ctx, GLuint program)
Brian34ae99d2006-12-18 08:28:54 -07001059{
Brian65a18442006-12-19 18:46:56 -07001060 struct gl_shader_program *shProg;
Brian34ae99d2006-12-18 08:28:54 -07001061
Brian65a18442006-12-19 18:46:56 -07001062 shProg = _mesa_lookup_shader_program(ctx, program);
1063 if (!shProg) {
Brian43975832007-01-04 08:21:09 -07001064 _mesa_error(ctx, GL_INVALID_VALUE, "glLinkProgram(program)");
Brian34ae99d2006-12-18 08:28:54 -07001065 return;
1066 }
1067
Brianc1771912007-02-16 09:56:19 -07001068 _slang_link(ctx, program, shProg);
Brian34ae99d2006-12-18 08:28:54 -07001069}
1070
1071
1072/**
Brian5b01c5e2006-12-19 18:02:03 -07001073 * Called via ctx->Driver.UseProgram()
Brian34ae99d2006-12-18 08:28:54 -07001074 */
Brian5b01c5e2006-12-19 18:02:03 -07001075void
1076_mesa_use_program(GLcontext *ctx, GLuint program)
Brian34ae99d2006-12-18 08:28:54 -07001077{
Brian3c008a02007-04-12 15:22:32 -06001078 struct gl_shader_program *shProg;
1079
Brian00d63aa2007-02-03 11:35:02 -07001080 if (ctx->Shader.CurrentProgram &&
1081 ctx->Shader.CurrentProgram->Name == program) {
1082 /* no-op */
1083 return;
1084 }
1085
1086 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
1087
Brian5b01c5e2006-12-19 18:02:03 -07001088 if (program) {
Brian65a18442006-12-19 18:46:56 -07001089 shProg = _mesa_lookup_shader_program(ctx, program);
1090 if (!shProg) {
Brian43975832007-01-04 08:21:09 -07001091 _mesa_error(ctx, GL_INVALID_VALUE,
Brian5b01c5e2006-12-19 18:02:03 -07001092 "glUseProgramObjectARB(programObj)");
1093 return;
1094 }
Brian5b01c5e2006-12-19 18:02:03 -07001095 }
1096 else {
Brian3c008a02007-04-12 15:22:32 -06001097 shProg = NULL;
1098 }
1099
1100 _mesa_reference_shader_program(ctx, &ctx->Shader.CurrentProgram, shProg);
Brian5b01c5e2006-12-19 18:02:03 -07001101}
Brian34ae99d2006-12-18 08:28:54 -07001102
Brian5b01c5e2006-12-19 18:02:03 -07001103
1104/**
1105 * Called via ctx->Driver.Uniform().
1106 */
1107void
1108_mesa_uniform(GLcontext *ctx, GLint location, GLsizei count,
1109 const GLvoid *values, GLenum type)
1110{
Brian3a8e2772006-12-20 17:19:16 -07001111 struct gl_shader_program *shProg = ctx->Shader.CurrentProgram;
Brian98650bd2007-03-13 16:32:48 -06001112 GLint elems, i, k;
Brian3a8e2772006-12-20 17:19:16 -07001113
1114 if (!shProg || !shProg->LinkStatus) {
Brian5b01c5e2006-12-19 18:02:03 -07001115 _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(program not linked)");
Brian3a8e2772006-12-20 17:19:16 -07001116 return;
1117 }
1118
Brian223d7cb2007-01-23 16:37:51 -07001119 if (location < 0 || location >= (GLint) shProg->Uniforms->NumParameters) {
Brian3a8e2772006-12-20 17:19:16 -07001120 _mesa_error(ctx, GL_INVALID_VALUE, "glUniform(location)");
1121 return;
1122 }
1123
1124 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
1125
Brianfee9bbe2007-02-02 18:05:43 -07001126 /*
1127 * If we're setting a sampler, we must use glUniformi1()!
1128 */
1129 if (shProg->Uniforms->Parameters[location].Type == PROGRAM_SAMPLER) {
Brian3e4302f2007-05-09 08:04:32 -06001130 GLint unit;
Brianfee9bbe2007-02-02 18:05:43 -07001131 if (type != GL_INT || count != 1) {
1132 _mesa_error(ctx, GL_INVALID_OPERATION,
1133 "glUniform(only glUniform1i can be used "
1134 "to set sampler uniforms)");
1135 return;
1136 }
Brian3e4302f2007-05-09 08:04:32 -06001137 /* check that the sampler (tex unit index) is legal */
1138 unit = ((GLint *) values)[0];
1139 if (unit >= ctx->Const.MaxTextureImageUnits) {
1140 _mesa_error(ctx, GL_INVALID_VALUE,
1141 "glUniform1(invalid sampler/tex unit index)");
1142 return;
1143 }
Brianfee9bbe2007-02-02 18:05:43 -07001144 }
1145
Brian52363952007-03-13 16:50:24 -06001146 if (count < 0) {
1147 _mesa_error(ctx, GL_INVALID_VALUE, "glUniform(count < 0)");
1148 return;
1149 }
1150
Brian98650bd2007-03-13 16:32:48 -06001151 switch (type) {
1152 case GL_FLOAT:
1153 case GL_INT:
1154 elems = 1;
1155 break;
1156 case GL_FLOAT_VEC2:
1157 case GL_INT_VEC2:
1158 elems = 2;
1159 break;
1160 case GL_FLOAT_VEC3:
1161 case GL_INT_VEC3:
1162 elems = 3;
1163 break;
1164 case GL_FLOAT_VEC4:
1165 case GL_INT_VEC4:
1166 elems = 4;
1167 break;
1168 default:
1169 _mesa_problem(ctx, "Invalid type in _mesa_uniform");
1170 return;
Brian89dc4852007-01-04 14:35:44 -07001171 }
Brian98650bd2007-03-13 16:32:48 -06001172
1173 if (count * elems > shProg->Uniforms->Parameters[location].Size) {
1174 _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(count too large)");
1175 return;
1176 }
1177
1178 for (k = 0; k < count; k++) {
1179 GLfloat *uniformVal = shProg->Uniforms->ParameterValues[location + k];
1180 if (type == GL_INT ||
1181 type == GL_INT_VEC2 ||
1182 type == GL_INT_VEC3 ||
1183 type == GL_INT_VEC4) {
Brian52363952007-03-13 16:50:24 -06001184 const GLint *iValues = ((const GLint *) values) + k * elems;
Brian98650bd2007-03-13 16:32:48 -06001185 for (i = 0; i < elems; i++) {
1186 uniformVal[i] = (GLfloat) iValues[i];
1187 }
1188 }
1189 else {
Brian52363952007-03-13 16:50:24 -06001190 const GLfloat *fValues = ((const GLfloat *) values) + k * elems;
Brian98650bd2007-03-13 16:32:48 -06001191 for (i = 0; i < elems; i++) {
1192 uniformVal[i] = fValues[i];
1193 }
Brian89dc4852007-01-04 14:35:44 -07001194 }
Brian5b01c5e2006-12-19 18:02:03 -07001195 }
Brian5cf73262007-01-05 16:02:45 -07001196
1197 if (shProg->Uniforms->Parameters[location].Type == PROGRAM_SAMPLER) {
Briancec81ee2007-03-07 08:04:06 -07001198 if (shProg->VertexProgram)
1199 _slang_resolve_samplers(shProg, &shProg->VertexProgram->Base);
1200 if (shProg->FragmentProgram)
1201 _slang_resolve_samplers(shProg, &shProg->FragmentProgram->Base);
Brian5cf73262007-01-05 16:02:45 -07001202 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1203 }
Brian34ae99d2006-12-18 08:28:54 -07001204}
1205
1206
1207/**
Brian5b01c5e2006-12-19 18:02:03 -07001208 * Called by ctx->Driver.UniformMatrix().
Brian34ae99d2006-12-18 08:28:54 -07001209 */
Brian5b01c5e2006-12-19 18:02:03 -07001210void
1211_mesa_uniform_matrix(GLcontext *ctx, GLint cols, GLint rows,
1212 GLenum matrixType, GLint location, GLsizei count,
1213 GLboolean transpose, const GLfloat *values)
Brian34ae99d2006-12-18 08:28:54 -07001214{
Brian3a8e2772006-12-20 17:19:16 -07001215 struct gl_shader_program *shProg = ctx->Shader.CurrentProgram;
1216 if (!shProg || !shProg->LinkStatus) {
1217 _mesa_error(ctx, GL_INVALID_OPERATION,
1218 "glUniformMatrix(program not linked)");
1219 return;
1220 }
1221 if (location < 0 || location >= shProg->Uniforms->NumParameters) {
1222 _mesa_error(ctx, GL_INVALID_VALUE, "glUniformMatrix(location)");
1223 return;
1224 }
Brian34ae99d2006-12-18 08:28:54 -07001225 if (values == NULL) {
Brian3a8e2772006-12-20 17:19:16 -07001226 _mesa_error(ctx, GL_INVALID_VALUE, "glUniformMatrix");
Brian34ae99d2006-12-18 08:28:54 -07001227 return;
1228 }
1229
1230 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
1231
Brian3a8e2772006-12-20 17:19:16 -07001232 /*
1233 * Note: the _columns_ of a matrix are stored in program registers, not
1234 * the rows.
1235 */
1236 /* XXXX need to test 3x3 and 2x2 matrices... */
Brian34ae99d2006-12-18 08:28:54 -07001237 if (transpose) {
Brian3a8e2772006-12-20 17:19:16 -07001238 GLuint row, col;
1239 for (col = 0; col < cols; col++) {
1240 GLfloat *v = shProg->Uniforms->ParameterValues[location + col];
1241 for (row = 0; row < rows; row++) {
Brian9f442472007-03-09 11:34:18 -07001242 v[row] = values[row * cols + col];
Brian34ae99d2006-12-18 08:28:54 -07001243 }
Brian34ae99d2006-12-18 08:28:54 -07001244 }
Brian34ae99d2006-12-18 08:28:54 -07001245 }
1246 else {
Brian3a8e2772006-12-20 17:19:16 -07001247 GLuint row, col;
1248 for (col = 0; col < cols; col++) {
1249 GLfloat *v = shProg->Uniforms->ParameterValues[location + col];
1250 for (row = 0; row < rows; row++) {
Brian9f442472007-03-09 11:34:18 -07001251 v[row] = values[col * rows + row];
Brian3a8e2772006-12-20 17:19:16 -07001252 }
1253 }
Brian34ae99d2006-12-18 08:28:54 -07001254 }
1255}
1256
1257
Brian5b01c5e2006-12-19 18:02:03 -07001258void
1259_mesa_validate_program(GLcontext *ctx, GLuint program)
Brian34ae99d2006-12-18 08:28:54 -07001260{
Brian65a18442006-12-19 18:46:56 -07001261 struct gl_shader_program *shProg;
1262 shProg = _mesa_lookup_shader_program(ctx, program);
1263 if (!shProg) {
Brian43975832007-01-04 08:21:09 -07001264 _mesa_error(ctx, GL_INVALID_VALUE, "glValidateProgram(program)");
Brian34ae99d2006-12-18 08:28:54 -07001265 return;
1266 }
Brian5b01c5e2006-12-19 18:02:03 -07001267 /* XXX temporary */
Brian65a18442006-12-19 18:46:56 -07001268 shProg->Validated = GL_TRUE;
Brian34ae99d2006-12-18 08:28:54 -07001269
Brian5b01c5e2006-12-19 18:02:03 -07001270 /* From the GL spec:
1271 any two active samplers in the current program object are of
1272 different types, but refer to the same texture image unit,
1273
1274 any active sampler in the current program object refers to a texture
1275 image unit where fixed-function fragment processing accesses a
1276 texture target that does not match the sampler type, or
1277
1278 the sum of the number of active samplers in the program and the
1279 number of texture image units enabled for fixed-function fragment
1280 processing exceeds the combined limit on the total number of texture
1281 image units allowed.
1282 */
Brian34ae99d2006-12-18 08:28:54 -07001283}