blob: 6cfdf57a494d1913987a4321cef282dc3635cc7f [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"
Brian Paulade50832008-05-14 16:09:46 -060046#include "prog_uniform.h"
Brianc223c6b2007-07-04 13:15:20 -060047#include "shader/shader_api.h"
48#include "shader/slang/slang_compile.h"
49#include "shader/slang/slang_link.h"
Brian34ae99d2006-12-18 08:28:54 -070050
51
52
Brianf2923612006-12-20 09:56:44 -070053/**
54 * Allocate a new gl_shader_program object, initialize it.
55 */
Brian Paulfd59f192008-05-18 16:04:55 -060056static struct gl_shader_program *
Brianf2923612006-12-20 09:56:44 -070057_mesa_new_shader_program(GLcontext *ctx, GLuint name)
58{
59 struct gl_shader_program *shProg;
60 shProg = CALLOC_STRUCT(gl_shader_program);
61 if (shProg) {
Brianf3e8c322007-04-18 14:53:23 -060062 shProg->Type = GL_SHADER_PROGRAM_MESA;
Brianf2923612006-12-20 09:56:44 -070063 shProg->Name = name;
64 shProg->RefCount = 1;
Brian3209c3e2007-01-09 17:49:24 -070065 shProg->Attributes = _mesa_new_parameter_list();
Brianf2923612006-12-20 09:56:44 -070066 }
67 return shProg;
68}
69
70
Brianb9fbedd2007-03-26 09:23:44 -060071/**
Brian3c008a02007-04-12 15:22:32 -060072 * Clear (free) the shader program state that gets produced by linking.
Brianb9fbedd2007-03-26 09:23:44 -060073 */
Brianf2923612006-12-20 09:56:44 -070074void
Brian3c008a02007-04-12 15:22:32 -060075_mesa_clear_shader_program_data(GLcontext *ctx,
76 struct gl_shader_program *shProg)
Brianf2923612006-12-20 09:56:44 -070077{
Brian Paul8bdf5b62008-05-16 09:56:59 -060078 _mesa_reference_vertprog(ctx, &shProg->VertexProgram, NULL);
79 _mesa_reference_fragprog(ctx, &shProg->FragmentProgram, NULL);
Brianf2923612006-12-20 09:56:44 -070080
Brianf2923612006-12-20 09:56:44 -070081 if (shProg->Uniforms) {
Brian Paulade50832008-05-14 16:09:46 -060082 _mesa_free_uniform_list(shProg->Uniforms);
Brianf2923612006-12-20 09:56:44 -070083 shProg->Uniforms = NULL;
84 }
85
86 if (shProg->Varying) {
87 _mesa_free_parameter_list(shProg->Varying);
88 shProg->Varying = NULL;
89 }
90}
91
92
Brianb9fbedd2007-03-26 09:23:44 -060093/**
Brian3c008a02007-04-12 15:22:32 -060094 * Free all the data that hangs off a shader program object, but not the
95 * object itself.
96 */
97void
98_mesa_free_shader_program_data(GLcontext *ctx,
99 struct gl_shader_program *shProg)
100{
101 GLuint i;
102
Brianf3e8c322007-04-18 14:53:23 -0600103 assert(shProg->Type == GL_SHADER_PROGRAM_MESA);
Brian3c008a02007-04-12 15:22:32 -0600104
105 _mesa_clear_shader_program_data(ctx, shProg);
106
Brian4b7c6fc2007-04-19 15:23:34 -0600107 if (shProg->Attributes) {
108 _mesa_free_parameter_list(shProg->Attributes);
109 shProg->Attributes = NULL;
110 }
111
Brian3c008a02007-04-12 15:22:32 -0600112 /* detach shaders */
113 for (i = 0; i < shProg->NumShaders; i++) {
114 _mesa_reference_shader(ctx, &shProg->Shaders[i], NULL);
115 }
Xiang, Haihao63d8a842008-03-31 17:02:47 +0800116 shProg->NumShaders = 0;
117
Brian3c008a02007-04-12 15:22:32 -0600118 if (shProg->Shaders) {
119 _mesa_free(shProg->Shaders);
120 shProg->Shaders = NULL;
121 }
Alan Hourihaneeec20c32008-04-22 20:29:42 +0100122
123 if (shProg->InfoLog) {
124 _mesa_free(shProg->InfoLog);
125 shProg->InfoLog = NULL;
126 }
Brian3c008a02007-04-12 15:22:32 -0600127}
128
129
130/**
Brianb9fbedd2007-03-26 09:23:44 -0600131 * Free/delete a shader program object.
132 */
Brianf2923612006-12-20 09:56:44 -0700133void
134_mesa_free_shader_program(GLcontext *ctx, struct gl_shader_program *shProg)
135{
136 _mesa_free_shader_program_data(ctx, shProg);
Alan Hourihaneeec20c32008-04-22 20:29:42 +0100137
Brianf2923612006-12-20 09:56:44 -0700138 _mesa_free(shProg);
139}
140
141
142/**
Brian3c008a02007-04-12 15:22:32 -0600143 * Set ptr to point to shProg.
144 * If ptr is pointing to another object, decrement its refcount (and delete
145 * if refcount hits zero).
146 * Then set ptr to point to shProg, incrementing its refcount.
147 */
148/* XXX this could be static */
149void
150_mesa_reference_shader_program(GLcontext *ctx,
151 struct gl_shader_program **ptr,
152 struct gl_shader_program *shProg)
153{
154 assert(ptr);
155 if (*ptr == shProg) {
156 /* no-op */
157 return;
158 }
159 if (*ptr) {
160 /* Unreference the old shader program */
161 GLboolean deleteFlag = GL_FALSE;
162 struct gl_shader_program *old = *ptr;
163
164 ASSERT(old->RefCount > 0);
165 old->RefCount--;
Brian Paul8bdf5b62008-05-16 09:56:59 -0600166#if 0
167 printf("ShaderProgram %p ID=%u RefCount-- to %d\n",
168 (void *) old, old->Name, old->RefCount);
169#endif
Brian3c008a02007-04-12 15:22:32 -0600170 deleteFlag = (old->RefCount == 0);
171
172 if (deleteFlag) {
Xiang, Haihaoaef47c42008-03-31 16:27:47 +0800173 _mesa_HashRemove(ctx->Shared->ShaderObjects, old->Name);
Brian3c008a02007-04-12 15:22:32 -0600174 _mesa_free_shader_program(ctx, old);
175 }
176
177 *ptr = NULL;
178 }
179 assert(!*ptr);
180
181 if (shProg) {
182 shProg->RefCount++;
Brian Paul8bdf5b62008-05-16 09:56:59 -0600183#if 0
184 printf("ShaderProgram %p ID=%u RefCount++ to %d\n",
185 (void *) shProg, shProg->Name, shProg->RefCount);
186#endif
Brian3c008a02007-04-12 15:22:32 -0600187 *ptr = shProg;
188 }
189}
190
191
192/**
Brianf2923612006-12-20 09:56:44 -0700193 * Lookup a GLSL program object.
194 */
195struct gl_shader_program *
196_mesa_lookup_shader_program(GLcontext *ctx, GLuint name)
197{
198 struct gl_shader_program *shProg;
199 if (name) {
200 shProg = (struct gl_shader_program *)
Xiang, Haihaoaef47c42008-03-31 16:27:47 +0800201 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
Brianf2923612006-12-20 09:56:44 -0700202 /* Note that both gl_shader and gl_shader_program objects are kept
203 * in the same hash table. Check the object's type to be sure it's
204 * what we're expecting.
205 */
Brianf3e8c322007-04-18 14:53:23 -0600206 if (shProg && shProg->Type != GL_SHADER_PROGRAM_MESA) {
Brianf2923612006-12-20 09:56:44 -0700207 return NULL;
208 }
209 return shProg;
210 }
211 return NULL;
212}
213
214
215/**
Brian Paul530df582008-07-03 16:21:11 -0600216 * As above, but record an error if program is not found.
217 */
218static struct gl_shader_program *
219_mesa_lookup_shader_program_err(GLcontext *ctx, GLuint name,
220 const char *caller)
221{
222 if (!name) {
223 _mesa_error(ctx, GL_INVALID_VALUE, caller);
224 return NULL;
225 }
226 else {
227 struct gl_shader_program *shProg = (struct gl_shader_program *)
228 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
229 if (!shProg) {
230 _mesa_error(ctx, GL_INVALID_VALUE, caller);
231 return NULL;
232 }
233 if (shProg->Type != GL_SHADER_PROGRAM_MESA) {
234 _mesa_error(ctx, GL_INVALID_OPERATION, caller);
235 return NULL;
236 }
237 return shProg;
238 }
239}
240
241
242
243
244/**
Brianf2923612006-12-20 09:56:44 -0700245 * Allocate a new gl_shader object, initialize it.
246 */
247struct gl_shader *
248_mesa_new_shader(GLcontext *ctx, GLuint name, GLenum type)
249{
250 struct gl_shader *shader;
251 assert(type == GL_FRAGMENT_SHADER || type == GL_VERTEX_SHADER);
252 shader = CALLOC_STRUCT(gl_shader);
253 if (shader) {
254 shader->Type = type;
255 shader->Name = name;
256 shader->RefCount = 1;
257 }
258 return shader;
259}
260
261
262void
263_mesa_free_shader(GLcontext *ctx, struct gl_shader *sh)
264{
265 GLuint i;
266 if (sh->Source)
267 _mesa_free((void *) sh->Source);
268 if (sh->InfoLog)
269 _mesa_free(sh->InfoLog);
Brian Paul57e222d2008-05-14 12:10:45 -0600270 for (i = 0; i < sh->NumPrograms; i++)
271 _mesa_reference_program(ctx, &sh->Programs[i], NULL);
Brianf2923612006-12-20 09:56:44 -0700272 if (sh->Programs)
273 _mesa_free(sh->Programs);
274 _mesa_free(sh);
275}
276
277
278/**
Brian3c008a02007-04-12 15:22:32 -0600279 * Set ptr to point to sh.
280 * If ptr is pointing to another shader, decrement its refcount (and delete
281 * if refcount hits zero).
282 * Then set ptr to point to sh, incrementing its refcount.
283 */
284/* XXX this could be static */
285void
286_mesa_reference_shader(GLcontext *ctx, struct gl_shader **ptr,
287 struct gl_shader *sh)
288{
289 assert(ptr);
290 if (*ptr == sh) {
291 /* no-op */
292 return;
293 }
294 if (*ptr) {
295 /* Unreference the old shader */
296 GLboolean deleteFlag = GL_FALSE;
297 struct gl_shader *old = *ptr;
298
299 ASSERT(old->RefCount > 0);
300 old->RefCount--;
Brian99193e42007-04-12 15:45:02 -0600301 /*printf("SHADER DECR %p (%d) to %d\n",
302 (void*) old, old->Name, old->RefCount);*/
Brian3c008a02007-04-12 15:22:32 -0600303 deleteFlag = (old->RefCount == 0);
304
305 if (deleteFlag) {
306 _mesa_HashRemove(ctx->Shared->ShaderObjects, old->Name);
307 _mesa_free_shader(ctx, old);
308 }
309
310 *ptr = NULL;
311 }
312 assert(!*ptr);
313
314 if (sh) {
315 /* reference new */
316 sh->RefCount++;
Brian99193e42007-04-12 15:45:02 -0600317 /*printf("SHADER INCR %p (%d) to %d\n",
318 (void*) sh, sh->Name, sh->RefCount);*/
Brian3c008a02007-04-12 15:22:32 -0600319 *ptr = sh;
320 }
321}
322
323
324/**
Brianf2923612006-12-20 09:56:44 -0700325 * Lookup a GLSL shader object.
326 */
327struct gl_shader *
328_mesa_lookup_shader(GLcontext *ctx, GLuint name)
329{
330 if (name) {
331 struct gl_shader *sh = (struct gl_shader *)
332 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
333 /* Note that both gl_shader and gl_shader_program objects are kept
334 * in the same hash table. Check the object's type to be sure it's
335 * what we're expecting.
336 */
Brianf3e8c322007-04-18 14:53:23 -0600337 if (sh && sh->Type == GL_SHADER_PROGRAM_MESA) {
Brianf2923612006-12-20 09:56:44 -0700338 return NULL;
339 }
340 return sh;
341 }
342 return NULL;
343}
344
345
Brianfa4d0362007-02-26 18:33:50 -0700346/**
Brian Paul530df582008-07-03 16:21:11 -0600347 * As above, but record an error if shader is not found.
348 */
349static struct gl_shader *
350_mesa_lookup_shader_err(GLcontext *ctx, GLuint name, const char *caller)
351{
352 if (!name) {
353 _mesa_error(ctx, GL_INVALID_VALUE, caller);
354 return NULL;
355 }
356 else {
357 struct gl_shader *sh = (struct gl_shader *)
358 _mesa_HashLookup(ctx->Shared->ShaderObjects, name);
359 if (!sh) {
360 _mesa_error(ctx, GL_INVALID_VALUE, caller);
361 return NULL;
362 }
363 if (sh->Type == GL_SHADER_PROGRAM_MESA) {
364 _mesa_error(ctx, GL_INVALID_OPERATION, caller);
365 return NULL;
366 }
367 return sh;
368 }
369}
370
371
372
373/**
Brianfa4d0362007-02-26 18:33:50 -0700374 * Initialize context's shader state.
375 */
Brianf2923612006-12-20 09:56:44 -0700376void
377_mesa_init_shader_state(GLcontext * ctx)
378{
Brianfa4d0362007-02-26 18:33:50 -0700379 /* Device drivers may override these to control what kind of instructions
380 * are generated by the GLSL compiler.
381 */
382 ctx->Shader.EmitHighLevelInstructions = GL_TRUE;
Brian63556fa2007-03-23 14:47:46 -0600383 ctx->Shader.EmitCondCodes = GL_TRUE; /* XXX probably want GL_FALSE... */
Brianfa4d0362007-02-26 18:33:50 -0700384 ctx->Shader.EmitComments = GL_FALSE;
Brianf2923612006-12-20 09:56:44 -0700385}
386
387
Brian5b01c5e2006-12-19 18:02:03 -0700388/**
Brian935f93f2007-03-24 16:20:02 -0600389 * Free the per-context shader-related state.
390 */
391void
392_mesa_free_shader_state(GLcontext *ctx)
393{
Brian3c008a02007-04-12 15:22:32 -0600394 _mesa_reference_shader_program(ctx, &ctx->Shader.CurrentProgram, NULL);
Brian935f93f2007-03-24 16:20:02 -0600395}
396
397
398/**
Brian5b01c5e2006-12-19 18:02:03 -0700399 * Copy string from <src> to <dst>, up to maxLength characters, returning
400 * length of <dst> in <length>.
401 * \param src the strings source
402 * \param maxLength max chars to copy
403 * \param length returns number of chars copied
404 * \param dst the string destination
405 */
406static void
407copy_string(GLchar *dst, GLsizei maxLength, GLsizei *length, const GLchar *src)
408{
409 GLsizei len;
410 for (len = 0; len < maxLength - 1 && src && src[len]; len++)
411 dst[len] = src[len];
412 if (maxLength > 0)
413 dst[len] = 0;
414 if (length)
415 *length = len;
416}
417
418
Brian Paul7acb7c12008-07-03 13:49:48 -0600419static GLboolean
420_mesa_is_program(GLcontext *ctx, GLuint name)
421{
422 struct gl_shader_program *shProg = _mesa_lookup_shader_program(ctx, name);
423 return shProg ? GL_TRUE : GL_FALSE;
424}
425
426
427static GLboolean
428_mesa_is_shader(GLcontext *ctx, GLuint name)
429{
430 struct gl_shader *shader = _mesa_lookup_shader(ctx, name);
431 return shader ? GL_TRUE : GL_FALSE;
432}
433
434
Brian5b01c5e2006-12-19 18:02:03 -0700435/**
436 * Called via ctx->Driver.AttachShader()
437 */
Brian Paulfd59f192008-05-18 16:04:55 -0600438static void
Brian5b01c5e2006-12-19 18:02:03 -0700439_mesa_attach_shader(GLcontext *ctx, GLuint program, GLuint shader)
440{
Brian Paul530df582008-07-03 16:21:11 -0600441 struct gl_shader_program *shProg;
442 struct gl_shader *sh;
443 GLuint i, n;
Brian5b01c5e2006-12-19 18:02:03 -0700444
Brian Paul530df582008-07-03 16:21:11 -0600445 shProg = _mesa_lookup_shader_program_err(ctx, program, "glAttachShader");
446 if (!shProg)
Brian5b01c5e2006-12-19 18:02:03 -0700447 return;
Brian5b01c5e2006-12-19 18:02:03 -0700448
Brian Paul530df582008-07-03 16:21:11 -0600449 sh = _mesa_lookup_shader_err(ctx, shader, "glAttachShader");
Brian Paul7acb7c12008-07-03 13:49:48 -0600450 if (!sh) {
Brian Paul7acb7c12008-07-03 13:49:48 -0600451 return;
452 }
453
Brian237b9852007-08-07 21:48:31 +0100454 n = shProg->NumShaders;
Brian5b01c5e2006-12-19 18:02:03 -0700455 for (i = 0; i < n; i++) {
Brian65a18442006-12-19 18:46:56 -0700456 if (shProg->Shaders[i] == sh) {
Brian5b01c5e2006-12-19 18:02:03 -0700457 /* already attached */
458 return;
Brian34ae99d2006-12-18 08:28:54 -0700459 }
460 }
Brian5b01c5e2006-12-19 18:02:03 -0700461
462 /* grow list */
Brian65a18442006-12-19 18:46:56 -0700463 shProg->Shaders = (struct gl_shader **)
464 _mesa_realloc(shProg->Shaders,
465 n * sizeof(struct gl_shader *),
466 (n + 1) * sizeof(struct gl_shader *));
467 if (!shProg->Shaders) {
Brian5b01c5e2006-12-19 18:02:03 -0700468 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAttachShader");
469 return;
470 }
471
472 /* append */
Brian3c008a02007-04-12 15:22:32 -0600473 shProg->Shaders[n] = NULL; /* since realloc() didn't zero the new space */
474 _mesa_reference_shader(ctx, &shProg->Shaders[n], sh);
Brian65a18442006-12-19 18:46:56 -0700475 shProg->NumShaders++;
Brian5b01c5e2006-12-19 18:02:03 -0700476}
477
478
Brian Paulfd59f192008-05-18 16:04:55 -0600479static GLint
Brian Paul896c0cc2008-05-16 15:47:55 -0600480_mesa_get_attrib_location(GLcontext *ctx, GLuint program,
481 const GLchar *name)
482{
483 struct gl_shader_program *shProg
Brian Paul530df582008-07-03 16:21:11 -0600484 = _mesa_lookup_shader_program_err(ctx, program, "glGetAttribLocation");
Brian Paul896c0cc2008-05-16 15:47:55 -0600485
486 if (!shProg) {
Brian Paul896c0cc2008-05-16 15:47:55 -0600487 return -1;
488 }
489
490 if (!shProg->LinkStatus) {
491 _mesa_error(ctx, GL_INVALID_OPERATION,
492 "glGetAttribLocation(program not linked)");
493 return -1;
494 }
495
496 if (!name)
497 return -1;
498
499 if (shProg->Attributes) {
500 GLint i = _mesa_lookup_parameter_index(shProg->Attributes, -1, name);
501 if (i >= 0) {
502 return shProg->Attributes->Parameters[i].StateIndexes[0];
503 }
504 }
505 return -1;
506}
507
508
Brian Paulfd59f192008-05-18 16:04:55 -0600509static void
Brian5b01c5e2006-12-19 18:02:03 -0700510_mesa_bind_attrib_location(GLcontext *ctx, GLuint program, GLuint index,
511 const GLchar *name)
512{
Brian Paul530df582008-07-03 16:21:11 -0600513 struct gl_shader_program *shProg;
Brianb7978af2007-01-09 19:17:17 -0700514 const GLint size = -1; /* unknown size */
515 GLint i, oldIndex;
Brian5b01c5e2006-12-19 18:02:03 -0700516
Brian Paul530df582008-07-03 16:21:11 -0600517 shProg = _mesa_lookup_shader_program_err(ctx, program,
518 "glBindAttribLocation");
Brian65a18442006-12-19 18:46:56 -0700519 if (!shProg) {
Brian5b01c5e2006-12-19 18:02:03 -0700520 return;
521 }
522
Brian9e4bae92006-12-20 09:27:42 -0700523 if (!name)
524 return;
525
526 if (strncmp(name, "gl_", 3) == 0) {
527 _mesa_error(ctx, GL_INVALID_OPERATION,
528 "glBindAttribLocation(illegal name)");
529 return;
530 }
531
Brian Paul7acb7c12008-07-03 13:49:48 -0600532 if (index >= ctx->Const.VertexProgram.MaxAttribs) {
533 _mesa_error(ctx, GL_INVALID_VALUE, "glBindAttribLocation(index)");
534 return;
535 }
536
Brian9f660252007-04-11 09:00:56 -0600537 if (shProg->LinkStatus) {
538 /* get current index/location for the attribute */
539 oldIndex = _mesa_get_attrib_location(ctx, program, name);
540 }
541 else {
542 oldIndex = -1;
543 }
Brian3209c3e2007-01-09 17:49:24 -0700544
545 /* this will replace the current value if it's already in the list */
Brianb7978af2007-01-09 19:17:17 -0700546 i = _mesa_add_attribute(shProg->Attributes, name, size, index);
Brian3209c3e2007-01-09 17:49:24 -0700547 if (i < 0) {
548 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindAttribLocation");
549 }
550
Brian9f660252007-04-11 09:00:56 -0600551 if (shProg->VertexProgram && oldIndex >= 0 && oldIndex != index) {
552 /* If the index changed, need to search/replace references to that attribute
553 * in the vertex program.
554 */
Brian3209c3e2007-01-09 17:49:24 -0700555 _slang_remap_attribute(&shProg->VertexProgram->Base, oldIndex, index);
556 }
Brian34ae99d2006-12-18 08:28:54 -0700557}
558
559
Brian Paulfd59f192008-05-18 16:04:55 -0600560static GLuint
Brian5b01c5e2006-12-19 18:02:03 -0700561_mesa_create_shader(GLcontext *ctx, GLenum type)
562{
Brian65a18442006-12-19 18:46:56 -0700563 struct gl_shader *sh;
Brian5b01c5e2006-12-19 18:02:03 -0700564 GLuint name;
565
566 name = _mesa_HashFindFreeKeyBlock(ctx->Shared->ShaderObjects, 1);
567
568 switch (type) {
Brian65a18442006-12-19 18:46:56 -0700569 case GL_FRAGMENT_SHADER:
570 case GL_VERTEX_SHADER:
571 sh = _mesa_new_shader(ctx, name, type);
Brian5b01c5e2006-12-19 18:02:03 -0700572 break;
573 default:
574 _mesa_error(ctx, GL_INVALID_ENUM, "CreateShader(type)");
575 return 0;
576 }
577
Brian65a18442006-12-19 18:46:56 -0700578 _mesa_HashInsert(ctx->Shared->ShaderObjects, name, sh);
Brian5b01c5e2006-12-19 18:02:03 -0700579
580 return name;
581}
582
583
Brian Paulfd59f192008-05-18 16:04:55 -0600584static GLuint
Brian5b01c5e2006-12-19 18:02:03 -0700585_mesa_create_program(GLcontext *ctx)
586{
587 GLuint name;
Brian65a18442006-12-19 18:46:56 -0700588 struct gl_shader_program *shProg;
Brian5b01c5e2006-12-19 18:02:03 -0700589
Xiang, Haihaoaef47c42008-03-31 16:27:47 +0800590 name = _mesa_HashFindFreeKeyBlock(ctx->Shared->ShaderObjects, 1);
Brian65a18442006-12-19 18:46:56 -0700591 shProg = _mesa_new_shader_program(ctx, name);
Brian5b01c5e2006-12-19 18:02:03 -0700592
Xiang, Haihaoaef47c42008-03-31 16:27:47 +0800593 _mesa_HashInsert(ctx->Shared->ShaderObjects, name, shProg);
Brian5b01c5e2006-12-19 18:02:03 -0700594
Brian3c008a02007-04-12 15:22:32 -0600595 assert(shProg->RefCount == 1);
596
Brian5b01c5e2006-12-19 18:02:03 -0700597 return name;
598}
599
600
Brian3c008a02007-04-12 15:22:32 -0600601/**
602 * Named w/ "2" to indicate OpenGL 2.x vs GL_ARB_fragment_programs's
603 * DeleteProgramARB.
604 */
Brian Paulfd59f192008-05-18 16:04:55 -0600605static void
Brian5b01c5e2006-12-19 18:02:03 -0700606_mesa_delete_program2(GLcontext *ctx, GLuint name)
607{
Brian3c008a02007-04-12 15:22:32 -0600608 /*
609 * NOTE: deleting shaders/programs works a bit differently than
610 * texture objects (and buffer objects, etc). Shader/program
611 * handles/IDs exist in the hash table until the object is really
612 * deleted (refcount==0). With texture objects, the handle/ID is
613 * removed from the hash table in glDeleteTextures() while the tex
614 * object itself might linger until its refcount goes to zero.
615 */
Brian65a18442006-12-19 18:46:56 -0700616 struct gl_shader_program *shProg;
Brian5b01c5e2006-12-19 18:02:03 -0700617
Brian Paul530df582008-07-03 16:21:11 -0600618 shProg = _mesa_lookup_shader_program_err(ctx, name, "glDeleteProgram");
619 if (!shProg)
Brian5b01c5e2006-12-19 18:02:03 -0700620 return;
Brian5b01c5e2006-12-19 18:02:03 -0700621
Brian9e4bae92006-12-20 09:27:42 -0700622 shProg->DeletePending = GL_TRUE;
623
Brian3c008a02007-04-12 15:22:32 -0600624 /* effectively, decr shProg's refcount */
625 _mesa_reference_shader_program(ctx, &shProg, NULL);
Brian5b01c5e2006-12-19 18:02:03 -0700626}
627
628
Brian Paulfd59f192008-05-18 16:04:55 -0600629static void
Brian5b01c5e2006-12-19 18:02:03 -0700630_mesa_delete_shader(GLcontext *ctx, GLuint shader)
631{
Brian Paul530df582008-07-03 16:21:11 -0600632 struct gl_shader *sh;
633
634 sh = _mesa_lookup_shader_err(ctx, shader, "glDeleteShader");
635 if (!sh)
Brian9e4bae92006-12-20 09:27:42 -0700636 return;
Brian5b01c5e2006-12-19 18:02:03 -0700637
Brian9e4bae92006-12-20 09:27:42 -0700638 sh->DeletePending = GL_TRUE;
Brian3c008a02007-04-12 15:22:32 -0600639
640 /* effectively, decr sh's refcount */
641 _mesa_reference_shader(ctx, &sh, NULL);
Brian5b01c5e2006-12-19 18:02:03 -0700642}
643
644
Brian Paulfd59f192008-05-18 16:04:55 -0600645static void
Brian5b01c5e2006-12-19 18:02:03 -0700646_mesa_detach_shader(GLcontext *ctx, GLuint program, GLuint shader)
647{
Brian Paul530df582008-07-03 16:21:11 -0600648 struct gl_shader_program *shProg;
Brian237b9852007-08-07 21:48:31 +0100649 GLuint n;
Brian5b01c5e2006-12-19 18:02:03 -0700650 GLuint i, j;
651
Brian Paul530df582008-07-03 16:21:11 -0600652 shProg = _mesa_lookup_shader_program_err(ctx, program, "glDetachShader");
653 if (!shProg)
Brian5b01c5e2006-12-19 18:02:03 -0700654 return;
Brian5b01c5e2006-12-19 18:02:03 -0700655
Brian237b9852007-08-07 21:48:31 +0100656 n = shProg->NumShaders;
657
Brian5b01c5e2006-12-19 18:02:03 -0700658 for (i = 0; i < n; i++) {
Brian65a18442006-12-19 18:46:56 -0700659 if (shProg->Shaders[i]->Name == shader) {
Brian5b01c5e2006-12-19 18:02:03 -0700660 /* found it */
Brian3c008a02007-04-12 15:22:32 -0600661 struct gl_shader **newList;
Brian9e4bae92006-12-20 09:27:42 -0700662
Brian Paul530df582008-07-03 16:21:11 -0600663 /* release */
Brian3c008a02007-04-12 15:22:32 -0600664 _mesa_reference_shader(ctx, &shProg->Shaders[i], NULL);
Brian9e4bae92006-12-20 09:27:42 -0700665
Brian5b01c5e2006-12-19 18:02:03 -0700666 /* alloc new, smaller array */
Brian65a18442006-12-19 18:46:56 -0700667 newList = (struct gl_shader **)
668 _mesa_malloc((n - 1) * sizeof(struct gl_shader *));
Brian5b01c5e2006-12-19 18:02:03 -0700669 if (!newList) {
670 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDetachShader");
671 return;
672 }
673 for (j = 0; j < i; j++) {
Brian65a18442006-12-19 18:46:56 -0700674 newList[j] = shProg->Shaders[j];
Brian5b01c5e2006-12-19 18:02:03 -0700675 }
676 while (++i < n)
Brian65a18442006-12-19 18:46:56 -0700677 newList[j++] = shProg->Shaders[i];
678 _mesa_free(shProg->Shaders);
Brian5b01c5e2006-12-19 18:02:03 -0700679
Brian65a18442006-12-19 18:46:56 -0700680 shProg->Shaders = newList;
Brianbac15c82007-04-18 14:55:18 -0600681 shProg->NumShaders = n - 1;
Brianaaa57412007-04-18 15:22:43 -0600682
683#ifdef DEBUG
684 /* sanity check */
685 {
686 for (j = 0; j < shProg->NumShaders; j++) {
687 assert(shProg->Shaders[j]->Type == GL_VERTEX_SHADER ||
688 shProg->Shaders[j]->Type == GL_FRAGMENT_SHADER);
689 assert(shProg->Shaders[j]->RefCount > 0);
690 }
691 }
692#endif
693
Brian5b01c5e2006-12-19 18:02:03 -0700694 return;
695 }
696 }
697
698 /* not found */
Brian Paul530df582008-07-03 16:21:11 -0600699 {
700 GLenum err;
701 if (_mesa_is_shader(ctx, shader))
702 err = GL_INVALID_OPERATION;
703 else if (_mesa_is_program(ctx, shader))
704 err = GL_INVALID_OPERATION;
705 else
706 err = GL_INVALID_VALUE;
707 _mesa_error(ctx, err, "glDetachProgram(shader)");
708 return;
709 }
Brian5b01c5e2006-12-19 18:02:03 -0700710}
711
712
Brian Paulfd59f192008-05-18 16:04:55 -0600713static void
Brian5b01c5e2006-12-19 18:02:03 -0700714_mesa_get_active_attrib(GLcontext *ctx, GLuint program, GLuint index,
715 GLsizei maxLength, GLsizei *length, GLint *size,
716 GLenum *type, GLchar *nameOut)
717{
718 static const GLenum vec_types[] = {
719 GL_FLOAT, GL_FLOAT_VEC2, GL_FLOAT_VEC3, GL_FLOAT_VEC4
720 };
Brian Paul530df582008-07-03 16:21:11 -0600721 struct gl_shader_program *shProg;
Brian5b01c5e2006-12-19 18:02:03 -0700722 GLint sz;
723
Brian Paul530df582008-07-03 16:21:11 -0600724 shProg = _mesa_lookup_shader_program_err(ctx, program, "glGetActiveAttrib");
725 if (!shProg)
Brian5b01c5e2006-12-19 18:02:03 -0700726 return;
Brian5b01c5e2006-12-19 18:02:03 -0700727
Brian65a18442006-12-19 18:46:56 -0700728 if (!shProg->Attributes || index >= shProg->Attributes->NumParameters) {
Brianaaa57412007-04-18 15:22:43 -0600729 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveAttrib(index)");
Brian5b01c5e2006-12-19 18:02:03 -0700730 return;
731 }
732
733 copy_string(nameOut, maxLength, length,
Brian65a18442006-12-19 18:46:56 -0700734 shProg->Attributes->Parameters[index].Name);
735 sz = shProg->Attributes->Parameters[index].Size;
Brian5b01c5e2006-12-19 18:02:03 -0700736 if (size)
Brian Paulade50832008-05-14 16:09:46 -0600737 *size = sz;
738 if (type)
739 *type = vec_types[sz]; /* XXX this is a temporary hack */
Brian5b01c5e2006-12-19 18:02:03 -0700740}
741
742
743/**
744 * Called via ctx->Driver.GetActiveUniform().
745 */
Brian Paulfd59f192008-05-18 16:04:55 -0600746static void
Brian5b01c5e2006-12-19 18:02:03 -0700747_mesa_get_active_uniform(GLcontext *ctx, GLuint program, GLuint index,
748 GLsizei maxLength, GLsizei *length, GLint *size,
749 GLenum *type, GLchar *nameOut)
750{
Brian Paul530df582008-07-03 16:21:11 -0600751 const struct gl_shader_program *shProg;
Brian Paulade50832008-05-14 16:09:46 -0600752 const struct gl_program *prog;
753 GLint progPos;
Brian5b01c5e2006-12-19 18:02:03 -0700754
Brian Paul530df582008-07-03 16:21:11 -0600755 shProg = _mesa_lookup_shader_program_err(ctx, program, "glGetActiveUniform");
756 if (!shProg)
Brian5b01c5e2006-12-19 18:02:03 -0700757 return;
Brian5b01c5e2006-12-19 18:02:03 -0700758
Brian Paulade50832008-05-14 16:09:46 -0600759 if (!shProg->Uniforms || index >= shProg->Uniforms->NumUniforms) {
Brian5b01c5e2006-12-19 18:02:03 -0700760 _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveUniform(index)");
761 return;
762 }
763
Brian Paulade50832008-05-14 16:09:46 -0600764 progPos = shProg->Uniforms->Uniforms[index].VertPos;
765 if (progPos >= 0) {
766 prog = &shProg->VertexProgram->Base;
767 }
768 else {
769 progPos = shProg->Uniforms->Uniforms[index].FragPos;
770 if (progPos >= 0) {
771 prog = &shProg->FragmentProgram->Base;
Brian274ac7a2007-04-18 16:05:53 -0600772 }
773 }
774
Brian Paulade50832008-05-14 16:09:46 -0600775 if (!prog || progPos < 0)
776 return; /* should never happen */
777
778 if (nameOut)
779 copy_string(nameOut, maxLength, length,
780 prog->Parameters->Parameters[progPos].Name);
781 if (size)
782 *size = prog->Parameters->Parameters[progPos].Size;
783
784 if (type)
785 *type = prog->Parameters->Parameters[progPos].DataType;
Brian5b01c5e2006-12-19 18:02:03 -0700786}
787
788
789/**
790 * Called via ctx->Driver.GetAttachedShaders().
791 */
Brian Paulfd59f192008-05-18 16:04:55 -0600792static void
Brian5b01c5e2006-12-19 18:02:03 -0700793_mesa_get_attached_shaders(GLcontext *ctx, GLuint program, GLsizei maxCount,
794 GLsizei *count, GLuint *obj)
795{
Brian Paul530df582008-07-03 16:21:11 -0600796 struct gl_shader_program *shProg =
797 _mesa_lookup_shader_program_err(ctx, program, "glGetAttachedShaders");
Brian65a18442006-12-19 18:46:56 -0700798 if (shProg) {
Brian Paul530df582008-07-03 16:21:11 -0600799 GLuint i;
800 for (i = 0; i < (GLuint) maxCount && i < shProg->NumShaders; i++) {
Brian65a18442006-12-19 18:46:56 -0700801 obj[i] = shProg->Shaders[i]->Name;
Brian5b01c5e2006-12-19 18:02:03 -0700802 }
803 if (count)
804 *count = i;
805 }
Brian5b01c5e2006-12-19 18:02:03 -0700806}
807
808
Brian Paulfd59f192008-05-18 16:04:55 -0600809static GLuint
Brian5b01c5e2006-12-19 18:02:03 -0700810_mesa_get_handle(GLcontext *ctx, GLenum pname)
Brian34ae99d2006-12-18 08:28:54 -0700811{
812#if 0
813 GET_CURRENT_CONTEXT(ctx);
814
815 switch (pname) {
816 case GL_PROGRAM_OBJECT_ARB:
817 {
Brian5b01c5e2006-12-19 18:02:03 -0700818 struct gl2_program_intf **pro = ctx->Shader.CurrentProgram;
Brian34ae99d2006-12-18 08:28:54 -0700819
820 if (pro != NULL)
821 return (**pro)._container._generic.
822 GetName((struct gl2_generic_intf **) (pro));
823 }
824 break;
825 default:
826 _mesa_error(ctx, GL_INVALID_ENUM, "glGetHandleARB");
827 }
828#endif
829 return 0;
830}
831
832
Brian Paulfd59f192008-05-18 16:04:55 -0600833static void
Brian5b01c5e2006-12-19 18:02:03 -0700834_mesa_get_programiv(GLcontext *ctx, GLuint program,
835 GLenum pname, GLint *params)
Brian34ae99d2006-12-18 08:28:54 -0700836{
Brian65a18442006-12-19 18:46:56 -0700837 struct gl_shader_program *shProg
838 = _mesa_lookup_shader_program(ctx, program);
Brian34ae99d2006-12-18 08:28:54 -0700839
Brian65a18442006-12-19 18:46:56 -0700840 if (!shProg) {
Brian5b01c5e2006-12-19 18:02:03 -0700841 _mesa_error(ctx, GL_INVALID_VALUE, "glGetProgramiv(program)");
Brian34ae99d2006-12-18 08:28:54 -0700842 return;
843 }
844
Brian5b01c5e2006-12-19 18:02:03 -0700845 switch (pname) {
846 case GL_DELETE_STATUS:
Brian65a18442006-12-19 18:46:56 -0700847 *params = shProg->DeletePending;
Brian5b01c5e2006-12-19 18:02:03 -0700848 break;
849 case GL_LINK_STATUS:
Brian65a18442006-12-19 18:46:56 -0700850 *params = shProg->LinkStatus;
Brian34ae99d2006-12-18 08:28:54 -0700851 break;
Brian5b01c5e2006-12-19 18:02:03 -0700852 case GL_VALIDATE_STATUS:
Brian65a18442006-12-19 18:46:56 -0700853 *params = shProg->Validated;
Brian5b01c5e2006-12-19 18:02:03 -0700854 break;
855 case GL_INFO_LOG_LENGTH:
Jan Dvorak5a0f02a2007-07-13 16:36:00 -0600856 *params = shProg->InfoLog ? strlen(shProg->InfoLog) + 1 : 0;
Brian5b01c5e2006-12-19 18:02:03 -0700857 break;
858 case GL_ATTACHED_SHADERS:
Brian65a18442006-12-19 18:46:56 -0700859 *params = shProg->NumShaders;
Brian5b01c5e2006-12-19 18:02:03 -0700860 break;
861 case GL_ACTIVE_ATTRIBUTES:
Brian3209c3e2007-01-09 17:49:24 -0700862 *params = shProg->Attributes ? shProg->Attributes->NumParameters : 0;
Brian5b01c5e2006-12-19 18:02:03 -0700863 break;
864 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH:
Brian274ac7a2007-04-18 16:05:53 -0600865 *params = _mesa_longest_parameter_name(shProg->Attributes,
866 PROGRAM_INPUT) + 1;
Brian5b01c5e2006-12-19 18:02:03 -0700867 break;
868 case GL_ACTIVE_UNIFORMS:
Brian Paulade50832008-05-14 16:09:46 -0600869 *params = shProg->Uniforms ? shProg->Uniforms->NumUniforms : 0;
Brian5b01c5e2006-12-19 18:02:03 -0700870 break;
871 case GL_ACTIVE_UNIFORM_MAX_LENGTH:
Brian Paulade50832008-05-14 16:09:46 -0600872 *params = _mesa_longest_uniform_name(shProg->Uniforms);
Brian274ac7a2007-04-18 16:05:53 -0600873 if (*params > 0)
874 (*params)++; /* add one for terminating zero */
Brian34ae99d2006-12-18 08:28:54 -0700875 break;
876 default:
Brian5b01c5e2006-12-19 18:02:03 -0700877 _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramiv(pname)");
878 return;
Brian34ae99d2006-12-18 08:28:54 -0700879 }
Brian5b01c5e2006-12-19 18:02:03 -0700880}
Brian34ae99d2006-12-18 08:28:54 -0700881
Brian34ae99d2006-12-18 08:28:54 -0700882
Brian Paulfd59f192008-05-18 16:04:55 -0600883static void
Brian5b01c5e2006-12-19 18:02:03 -0700884_mesa_get_shaderiv(GLcontext *ctx, GLuint name, GLenum pname, GLint *params)
885{
Brian Paul530df582008-07-03 16:21:11 -0600886 struct gl_shader *shader = _mesa_lookup_shader_err(ctx, name, "glGetShaderiv");
Brian5b01c5e2006-12-19 18:02:03 -0700887
888 if (!shader) {
Brian5b01c5e2006-12-19 18:02:03 -0700889 return;
890 }
Brian65a18442006-12-19 18:46:56 -0700891
Brian5b01c5e2006-12-19 18:02:03 -0700892 switch (pname) {
893 case GL_SHADER_TYPE:
894 *params = shader->Type;
895 break;
896 case GL_DELETE_STATUS:
897 *params = shader->DeletePending;
898 break;
899 case GL_COMPILE_STATUS:
900 *params = shader->CompileStatus;
901 break;
902 case GL_INFO_LOG_LENGTH:
Jan Dvorak5a0f02a2007-07-13 16:36:00 -0600903 *params = shader->InfoLog ? strlen(shader->InfoLog) + 1 : 0;
Brian5b01c5e2006-12-19 18:02:03 -0700904 break;
905 case GL_SHADER_SOURCE_LENGTH:
Jan Dvorak5a0f02a2007-07-13 16:36:00 -0600906 *params = shader->Source ? strlen((char *) shader->Source) + 1 : 0;
Brian5b01c5e2006-12-19 18:02:03 -0700907 break;
908 default:
909 _mesa_error(ctx, GL_INVALID_ENUM, "glGetShaderiv(pname)");
910 return;
911 }
912}
913
914
Brian Paulfd59f192008-05-18 16:04:55 -0600915static void
Brian5b01c5e2006-12-19 18:02:03 -0700916_mesa_get_program_info_log(GLcontext *ctx, GLuint program, GLsizei bufSize,
917 GLsizei *length, GLchar *infoLog)
918{
Brian65a18442006-12-19 18:46:56 -0700919 struct gl_shader_program *shProg
920 = _mesa_lookup_shader_program(ctx, program);
921 if (!shProg) {
Brian5b01c5e2006-12-19 18:02:03 -0700922 _mesa_error(ctx, GL_INVALID_VALUE, "glGetProgramInfoLog(program)");
923 return;
924 }
Brian65a18442006-12-19 18:46:56 -0700925 copy_string(infoLog, bufSize, length, shProg->InfoLog);
Brian5b01c5e2006-12-19 18:02:03 -0700926}
927
928
Brian Paulfd59f192008-05-18 16:04:55 -0600929static void
Brian5b01c5e2006-12-19 18:02:03 -0700930_mesa_get_shader_info_log(GLcontext *ctx, GLuint shader, GLsizei bufSize,
931 GLsizei *length, GLchar *infoLog)
932{
Brian65a18442006-12-19 18:46:56 -0700933 struct gl_shader *sh = _mesa_lookup_shader(ctx, shader);
934 if (!sh) {
Brian5b01c5e2006-12-19 18:02:03 -0700935 _mesa_error(ctx, GL_INVALID_VALUE, "glGetShaderInfoLog(shader)");
936 return;
937 }
Brian65a18442006-12-19 18:46:56 -0700938 copy_string(infoLog, bufSize, length, sh->InfoLog);
Brian5b01c5e2006-12-19 18:02:03 -0700939}
940
941
942/**
943 * Called via ctx->Driver.GetShaderSource().
944 */
Brian Paulfd59f192008-05-18 16:04:55 -0600945static void
Brian5b01c5e2006-12-19 18:02:03 -0700946_mesa_get_shader_source(GLcontext *ctx, GLuint shader, GLsizei maxLength,
947 GLsizei *length, GLchar *sourceOut)
948{
Brian Paul530df582008-07-03 16:21:11 -0600949 struct gl_shader *sh;
950 sh = _mesa_lookup_shader_err(ctx, shader, "glGetShaderSource");
Brian65a18442006-12-19 18:46:56 -0700951 if (!sh) {
Brian5b01c5e2006-12-19 18:02:03 -0700952 return;
953 }
Brian65a18442006-12-19 18:46:56 -0700954 copy_string(sourceOut, maxLength, length, sh->Source);
Brian5b01c5e2006-12-19 18:02:03 -0700955}
956
957
958/**
959 * Called via ctx->Driver.GetUniformfv().
960 */
Brian Paulfd59f192008-05-18 16:04:55 -0600961static void
Brian5b01c5e2006-12-19 18:02:03 -0700962_mesa_get_uniformfv(GLcontext *ctx, GLuint program, GLint location,
963 GLfloat *params)
964{
Brian65a18442006-12-19 18:46:56 -0700965 struct gl_shader_program *shProg
966 = _mesa_lookup_shader_program(ctx, program);
967 if (shProg) {
Brian Paul6cb12702008-06-28 16:48:31 -0600968 if (shProg->Uniforms &&
969 location >= 0 && location < shProg->Uniforms->NumUniforms) {
970 GLint progPos;
971 GLuint i;
Brian Paulf2632212008-05-16 10:49:44 -0600972 const struct gl_program *prog = NULL;
Brian Paulade50832008-05-14 16:09:46 -0600973
974 progPos = shProg->Uniforms->Uniforms[location].VertPos;
975 if (progPos >= 0) {
976 prog = &shProg->VertexProgram->Base;
Brian5b01c5e2006-12-19 18:02:03 -0700977 }
Brian Paulade50832008-05-14 16:09:46 -0600978 else {
979 progPos = shProg->Uniforms->Uniforms[location].FragPos;
980 if (progPos >= 0) {
981 prog = &shProg->FragmentProgram->Base;
Bruce Merryeeb03fa2007-12-21 14:41:45 +0200982 }
Brian Paulade50832008-05-14 16:09:46 -0600983 }
984
Brian Paulf2632212008-05-16 10:49:44 -0600985 ASSERT(prog);
986 if (prog) {
987 for (i = 0; i < prog->Parameters->Parameters[progPos].Size; i++) {
988 params[i] = prog->Parameters->ParameterValues[progPos][i];
989 }
Brian Paulade50832008-05-14 16:09:46 -0600990 }
Brian5b01c5e2006-12-19 18:02:03 -0700991 }
992 else {
Brian Paul6cb12702008-06-28 16:48:31 -0600993 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetUniformfv(location)");
Brian5b01c5e2006-12-19 18:02:03 -0700994 }
995 }
996 else {
Brian Paul6cb12702008-06-28 16:48:31 -0600997 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetUniformfv(program)");
Brian5b01c5e2006-12-19 18:02:03 -0700998 }
999}
1000
1001
1002/**
1003 * Called via ctx->Driver.GetUniformLocation().
1004 */
Brian Paulfd59f192008-05-18 16:04:55 -06001005static GLint
Brian5b01c5e2006-12-19 18:02:03 -07001006_mesa_get_uniform_location(GLcontext *ctx, GLuint program, const GLchar *name)
1007{
Brian Paul530df582008-07-03 16:21:11 -06001008 struct gl_shader_program *shProg =
1009 _mesa_lookup_shader_program_err(ctx, program, "glGetUniformLocation");
1010
Brian Paulade50832008-05-14 16:09:46 -06001011 if (!shProg)
1012 return -1;
Brian5b01c5e2006-12-19 18:02:03 -07001013
Brian Paule06565b2008-07-04 09:58:55 -06001014 if (shProg->LinkStatus == GL_FALSE) {
1015 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetUniformfv(program)");
1016 return -1;
1017 }
1018
Brian Paul530df582008-07-03 16:21:11 -06001019 /* XXX we should return -1 if the uniform was declared, but not
1020 * actually used.
1021 */
1022
Brian Paulade50832008-05-14 16:09:46 -06001023 return _mesa_lookup_uniform(shProg->Uniforms, name);
Brian5b01c5e2006-12-19 18:02:03 -07001024}
1025
1026
Brian34ae99d2006-12-18 08:28:54 -07001027
Brian5b01c5e2006-12-19 18:02:03 -07001028/**
1029 * Called via ctx->Driver.ShaderSource()
1030 */
Brian Paulfd59f192008-05-18 16:04:55 -06001031static void
Brian5b01c5e2006-12-19 18:02:03 -07001032_mesa_shader_source(GLcontext *ctx, GLuint shader, const GLchar *source)
Brian34ae99d2006-12-18 08:28:54 -07001033{
Brian Paul530df582008-07-03 16:21:11 -06001034 struct gl_shader *sh;
1035
1036 sh = _mesa_lookup_shader_err(ctx, shader, "glShaderSource");
1037 if (!sh)
Brian34ae99d2006-12-18 08:28:54 -07001038 return;
Brian34ae99d2006-12-18 08:28:54 -07001039
Brian34ae99d2006-12-18 08:28:54 -07001040 /* free old shader source string and install new one */
Brian65a18442006-12-19 18:46:56 -07001041 if (sh->Source) {
1042 _mesa_free((void *) sh->Source);
Brian34ae99d2006-12-18 08:28:54 -07001043 }
Brian65a18442006-12-19 18:46:56 -07001044 sh->Source = source;
Brian9e4bae92006-12-20 09:27:42 -07001045 sh->CompileStatus = GL_FALSE;
Brian34ae99d2006-12-18 08:28:54 -07001046}
1047
1048
Brian5b01c5e2006-12-19 18:02:03 -07001049/**
1050 * Called via ctx->Driver.CompileShader()
1051 */
Brian Paulfd59f192008-05-18 16:04:55 -06001052static void
Brian5b01c5e2006-12-19 18:02:03 -07001053_mesa_compile_shader(GLcontext *ctx, GLuint shaderObj)
Brian34ae99d2006-12-18 08:28:54 -07001054{
Brian Paul530df582008-07-03 16:21:11 -06001055 struct gl_shader *sh;
Brian34ae99d2006-12-18 08:28:54 -07001056
Brian Paul530df582008-07-03 16:21:11 -06001057 sh = _mesa_lookup_shader_err(ctx, shaderObj, "glCompileShader");
1058 if (!sh)
Brian34ae99d2006-12-18 08:28:54 -07001059 return;
Brian34ae99d2006-12-18 08:28:54 -07001060
Brian43975832007-01-04 08:21:09 -07001061 sh->CompileStatus = _slang_compile(ctx, sh);
Brian34ae99d2006-12-18 08:28:54 -07001062}
1063
1064
Brian5b01c5e2006-12-19 18:02:03 -07001065/**
1066 * Called via ctx->Driver.LinkProgram()
1067 */
Brian Paulfd59f192008-05-18 16:04:55 -06001068static void
Brian5b01c5e2006-12-19 18:02:03 -07001069_mesa_link_program(GLcontext *ctx, GLuint program)
Brian34ae99d2006-12-18 08:28:54 -07001070{
Brian65a18442006-12-19 18:46:56 -07001071 struct gl_shader_program *shProg;
Brian34ae99d2006-12-18 08:28:54 -07001072
Brian Paul530df582008-07-03 16:21:11 -06001073 shProg = _mesa_lookup_shader_program_err(ctx, program, "glLinkProgram");
1074 if (!shProg)
Brian34ae99d2006-12-18 08:28:54 -07001075 return;
Brian34ae99d2006-12-18 08:28:54 -07001076
Briandf43fb62008-05-06 23:08:51 -06001077 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
1078
Brianc1771912007-02-16 09:56:19 -07001079 _slang_link(ctx, program, shProg);
Brian34ae99d2006-12-18 08:28:54 -07001080}
1081
1082
1083/**
Brian5b01c5e2006-12-19 18:02:03 -07001084 * Called via ctx->Driver.UseProgram()
Brian34ae99d2006-12-18 08:28:54 -07001085 */
Brian5b01c5e2006-12-19 18:02:03 -07001086void
1087_mesa_use_program(GLcontext *ctx, GLuint program)
Brian34ae99d2006-12-18 08:28:54 -07001088{
Brian3c008a02007-04-12 15:22:32 -06001089 struct gl_shader_program *shProg;
1090
Brian00d63aa2007-02-03 11:35:02 -07001091 if (ctx->Shader.CurrentProgram &&
1092 ctx->Shader.CurrentProgram->Name == program) {
1093 /* no-op */
1094 return;
1095 }
1096
1097 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
1098
Brian5b01c5e2006-12-19 18:02:03 -07001099 if (program) {
Brian Paul530df582008-07-03 16:21:11 -06001100 shProg = _mesa_lookup_shader_program_err(ctx, program, "glUseProgram");
Brian65a18442006-12-19 18:46:56 -07001101 if (!shProg) {
Brian Paul530df582008-07-03 16:21:11 -06001102 return;
1103 }
1104 if (!shProg->LinkStatus) {
1105 _mesa_error(ctx, GL_INVALID_OPERATION, "glUseProgram");
Brian5b01c5e2006-12-19 18:02:03 -07001106 return;
1107 }
Brian5b01c5e2006-12-19 18:02:03 -07001108 }
1109 else {
Brian3c008a02007-04-12 15:22:32 -06001110 shProg = NULL;
1111 }
1112
1113 _mesa_reference_shader_program(ctx, &ctx->Shader.CurrentProgram, shProg);
Brian5b01c5e2006-12-19 18:02:03 -07001114}
Brian34ae99d2006-12-18 08:28:54 -07001115
Brian5b01c5e2006-12-19 18:02:03 -07001116
Brian Paulade50832008-05-14 16:09:46 -06001117
1118/**
1119 * Update the vertex and fragment program's TexturesUsed arrays.
1120 */
1121static void
1122update_textures_used(struct gl_program *prog)
1123{
1124 GLuint s;
1125
1126 memset(prog->TexturesUsed, 0, sizeof(prog->TexturesUsed));
1127
1128 for (s = 0; s < MAX_SAMPLERS; s++) {
1129 if (prog->SamplersUsed & (1 << s)) {
1130 GLuint u = prog->SamplerUnits[s];
1131 GLuint t = prog->SamplerTargets[s];
1132 assert(u < MAX_TEXTURE_IMAGE_UNITS);
1133 prog->TexturesUsed[u] |= (1 << t);
1134 }
1135 }
1136}
1137
1138
1139/**
1140 * Set the value of a program's uniform variable.
1141 * \param program the program whose uniform to update
1142 * \param location the location/index of the uniform
1143 * \param type the datatype of the uniform
1144 * \param count the number of uniforms to set
1145 * \param elems number of elements per uniform
1146 * \param values the new values
1147 */
1148static void
1149set_program_uniform(GLcontext *ctx, struct gl_program *program, GLint location,
Brian Paul530df582008-07-03 16:21:11 -06001150 GLenum type, GLsizei count, GLint elems, const void *values)
Brian Paulade50832008-05-14 16:09:46 -06001151{
1152 if (program->Parameters->Parameters[location].Type == PROGRAM_SAMPLER) {
1153 /* This controls which texture unit which is used by a sampler */
1154 GLuint texUnit, sampler;
1155
1156 /* data type for setting samplers must be int */
1157 if (type != GL_INT || count != 1) {
1158 _mesa_error(ctx, GL_INVALID_OPERATION,
1159 "glUniform(only glUniform1i can be used "
1160 "to set sampler uniforms)");
1161 return;
1162 }
1163
1164 sampler = (GLuint) program->Parameters->ParameterValues[location][0];
1165 texUnit = ((GLuint *) values)[0];
1166
1167 /* check that the sampler (tex unit index) is legal */
1168 if (texUnit >= ctx->Const.MaxTextureImageUnits) {
1169 _mesa_error(ctx, GL_INVALID_VALUE,
1170 "glUniform1(invalid sampler/tex unit index)");
1171 return;
1172 }
1173
1174 /* This maps a sampler to a texture unit: */
1175 program->SamplerUnits[sampler] = texUnit;
1176 update_textures_used(program);
1177
1178 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1179 }
1180 else {
1181 /* ordinary uniform variable */
Brian Paul530df582008-07-03 16:21:11 -06001182 GLsizei k, i;
Brian Paulade50832008-05-14 16:09:46 -06001183
1184 if (count * elems > program->Parameters->Parameters[location].Size) {
1185 _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(count too large)");
1186 return;
1187 }
1188
1189 for (k = 0; k < count; k++) {
1190 GLfloat *uniformVal = program->Parameters->ParameterValues[location + k];
1191 if (type == GL_INT ||
1192 type == GL_INT_VEC2 ||
1193 type == GL_INT_VEC3 ||
1194 type == GL_INT_VEC4) {
1195 const GLint *iValues = ((const GLint *) values) + k * elems;
1196 for (i = 0; i < elems; i++) {
1197 uniformVal[i] = (GLfloat) iValues[i];
1198 }
1199 }
1200 else {
1201 const GLfloat *fValues = ((const GLfloat *) values) + k * elems;
1202 for (i = 0; i < elems; i++) {
1203 uniformVal[i] = fValues[i];
1204 }
1205 }
1206 }
1207 }
1208}
1209
1210
Brian5b01c5e2006-12-19 18:02:03 -07001211/**
1212 * Called via ctx->Driver.Uniform().
1213 */
Brian Paulfd59f192008-05-18 16:04:55 -06001214static void
Brian5b01c5e2006-12-19 18:02:03 -07001215_mesa_uniform(GLcontext *ctx, GLint location, GLsizei count,
1216 const GLvoid *values, GLenum type)
1217{
Brian3a8e2772006-12-20 17:19:16 -07001218 struct gl_shader_program *shProg = ctx->Shader.CurrentProgram;
Brian Paulade50832008-05-14 16:09:46 -06001219 GLint elems;
Brian3a8e2772006-12-20 17:19:16 -07001220
1221 if (!shProg || !shProg->LinkStatus) {
Brian5b01c5e2006-12-19 18:02:03 -07001222 _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(program not linked)");
Brian3a8e2772006-12-20 17:19:16 -07001223 return;
1224 }
1225
Bruce Merryeeb03fa2007-12-21 14:41:45 +02001226 if (location == -1)
1227 return; /* The standard specifies this as a no-op */
1228
Brian Paulade50832008-05-14 16:09:46 -06001229 if (location < 0 || location >= (GLint) shProg->Uniforms->NumUniforms) {
1230 _mesa_error(ctx, GL_INVALID_VALUE, "glUniform(location)");
Brian3a8e2772006-12-20 17:19:16 -07001231 return;
1232 }
1233
Brian52363952007-03-13 16:50:24 -06001234 if (count < 0) {
1235 _mesa_error(ctx, GL_INVALID_VALUE, "glUniform(count < 0)");
1236 return;
1237 }
1238
Brian98650bd2007-03-13 16:32:48 -06001239 switch (type) {
1240 case GL_FLOAT:
1241 case GL_INT:
1242 elems = 1;
1243 break;
1244 case GL_FLOAT_VEC2:
1245 case GL_INT_VEC2:
1246 elems = 2;
1247 break;
1248 case GL_FLOAT_VEC3:
1249 case GL_INT_VEC3:
1250 elems = 3;
1251 break;
1252 case GL_FLOAT_VEC4:
1253 case GL_INT_VEC4:
1254 elems = 4;
1255 break;
1256 default:
1257 _mesa_problem(ctx, "Invalid type in _mesa_uniform");
1258 return;
Brian89dc4852007-01-04 14:35:44 -07001259 }
Brian98650bd2007-03-13 16:32:48 -06001260
Brian Paulade50832008-05-14 16:09:46 -06001261 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
Brian98650bd2007-03-13 16:32:48 -06001262
Brian Paulade50832008-05-14 16:09:46 -06001263 /* A uniform var may be used by both a vertex shader and a fragment
1264 * shader. We may need to update one or both shader's uniform here:
Bruce Merryeeb03fa2007-12-21 14:41:45 +02001265 */
Brian Paulade50832008-05-14 16:09:46 -06001266 if (shProg->VertexProgram) {
1267 GLint loc = shProg->Uniforms->Uniforms[location].VertPos;
1268 if (loc >= 0) {
1269 set_program_uniform(ctx, &shProg->VertexProgram->Base,
1270 loc, type, count, elems, values);
Bruce Merry2bf2a8c2007-12-21 23:18:40 +02001271 }
Brian5b01c5e2006-12-19 18:02:03 -07001272 }
Brian5cf73262007-01-05 16:02:45 -07001273
Brian Paulade50832008-05-14 16:09:46 -06001274 if (shProg->FragmentProgram) {
1275 GLint loc = shProg->Uniforms->Uniforms[location].FragPos;
1276 if (loc >= 0) {
1277 set_program_uniform(ctx, &shProg->FragmentProgram->Base,
1278 loc, type, count, elems, values);
1279 }
1280 }
1281}
1282
1283
1284static void
1285set_program_uniform_matrix(GLcontext *ctx, struct gl_program *program,
1286 GLuint location, GLuint rows, GLuint cols,
1287 GLboolean transpose, const GLfloat *values)
1288{
1289 /*
1290 * Note: the _columns_ of a matrix are stored in program registers, not
1291 * the rows.
1292 */
1293 /* XXXX need to test 3x3 and 2x2 matrices... */
1294 if (transpose) {
1295 GLuint row, col;
1296 for (col = 0; col < cols; col++) {
1297 GLfloat *v = program->Parameters->ParameterValues[location + col];
1298 for (row = 0; row < rows; row++) {
1299 v[row] = values[row * cols + col];
1300 }
1301 }
1302 }
1303 else {
1304 GLuint row, col;
1305 for (col = 0; col < cols; col++) {
1306 GLfloat *v = program->Parameters->ParameterValues[location + col];
1307 for (row = 0; row < rows; row++) {
1308 v[row] = values[col * rows + row];
1309 }
1310 }
Brian5cf73262007-01-05 16:02:45 -07001311 }
Brian34ae99d2006-12-18 08:28:54 -07001312}
1313
1314
1315/**
Brian5b01c5e2006-12-19 18:02:03 -07001316 * Called by ctx->Driver.UniformMatrix().
Brian34ae99d2006-12-18 08:28:54 -07001317 */
Brian Paulfd59f192008-05-18 16:04:55 -06001318static void
Brian5b01c5e2006-12-19 18:02:03 -07001319_mesa_uniform_matrix(GLcontext *ctx, GLint cols, GLint rows,
1320 GLenum matrixType, GLint location, GLsizei count,
1321 GLboolean transpose, const GLfloat *values)
Brian34ae99d2006-12-18 08:28:54 -07001322{
Brian3a8e2772006-12-20 17:19:16 -07001323 struct gl_shader_program *shProg = ctx->Shader.CurrentProgram;
Brian Paulade50832008-05-14 16:09:46 -06001324
Brian3a8e2772006-12-20 17:19:16 -07001325 if (!shProg || !shProg->LinkStatus) {
1326 _mesa_error(ctx, GL_INVALID_OPERATION,
1327 "glUniformMatrix(program not linked)");
1328 return;
1329 }
Brian Paulade50832008-05-14 16:09:46 -06001330
Bruce Merry89b80322007-12-21 15:20:17 +02001331 if (location == -1)
1332 return; /* The standard specifies this as a no-op */
Brian Paulade50832008-05-14 16:09:46 -06001333
1334 if (location < 0 || location >= shProg->Uniforms->NumUniforms) {
1335 _mesa_error(ctx, GL_INVALID_VALUE, "glUniformMatrix(location)");
Brian3a8e2772006-12-20 17:19:16 -07001336 return;
1337 }
Brian34ae99d2006-12-18 08:28:54 -07001338 if (values == NULL) {
Brian3a8e2772006-12-20 17:19:16 -07001339 _mesa_error(ctx, GL_INVALID_VALUE, "glUniformMatrix");
Brian34ae99d2006-12-18 08:28:54 -07001340 return;
1341 }
1342
1343 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
1344
Brian Paulade50832008-05-14 16:09:46 -06001345 if (shProg->VertexProgram) {
1346 GLint loc = shProg->Uniforms->Uniforms[location].VertPos;
1347 if (loc >= 0) {
1348 set_program_uniform_matrix(ctx, &shProg->VertexProgram->Base,
1349 loc, rows, cols, transpose, values);
Brian34ae99d2006-12-18 08:28:54 -07001350 }
Brian Paulade50832008-05-14 16:09:46 -06001351 }
1352
1353 if (shProg->FragmentProgram) {
1354 GLint loc = shProg->Uniforms->Uniforms[location].FragPos;
1355 if (loc >= 0) {
1356 set_program_uniform_matrix(ctx, &shProg->FragmentProgram->Base,
1357 loc, rows, cols, transpose, values);
Brian3a8e2772006-12-20 17:19:16 -07001358 }
Brian34ae99d2006-12-18 08:28:54 -07001359 }
1360}
1361
1362
Brian Paulfd59f192008-05-18 16:04:55 -06001363static void
Brian5b01c5e2006-12-19 18:02:03 -07001364_mesa_validate_program(GLcontext *ctx, GLuint program)
Brian34ae99d2006-12-18 08:28:54 -07001365{
Brian65a18442006-12-19 18:46:56 -07001366 struct gl_shader_program *shProg;
1367 shProg = _mesa_lookup_shader_program(ctx, program);
1368 if (!shProg) {
Brian43975832007-01-04 08:21:09 -07001369 _mesa_error(ctx, GL_INVALID_VALUE, "glValidateProgram(program)");
Brian34ae99d2006-12-18 08:28:54 -07001370 return;
1371 }
Brian5b01c5e2006-12-19 18:02:03 -07001372 /* XXX temporary */
Brian65a18442006-12-19 18:46:56 -07001373 shProg->Validated = GL_TRUE;
Brian34ae99d2006-12-18 08:28:54 -07001374
Brian5b01c5e2006-12-19 18:02:03 -07001375 /* From the GL spec:
1376 any two active samplers in the current program object are of
1377 different types, but refer to the same texture image unit,
1378
1379 any active sampler in the current program object refers to a texture
1380 image unit where fixed-function fragment processing accesses a
1381 texture target that does not match the sampler type, or
1382
1383 the sum of the number of active samplers in the program and the
1384 number of texture image units enabled for fixed-function fragment
1385 processing exceeds the combined limit on the total number of texture
1386 image units allowed.
1387 */
Brian34ae99d2006-12-18 08:28:54 -07001388}
Brian Paulfd59f192008-05-18 16:04:55 -06001389
1390
1391/**
1392 * Plug in Mesa's GLSL functions into the device driver function table.
1393 */
1394void
1395_mesa_init_glsl_driver_functions(struct dd_function_table *driver)
1396{
1397 driver->AttachShader = _mesa_attach_shader;
1398 driver->BindAttribLocation = _mesa_bind_attrib_location;
1399 driver->CompileShader = _mesa_compile_shader;
1400 driver->CreateProgram = _mesa_create_program;
1401 driver->CreateShader = _mesa_create_shader;
1402 driver->DeleteProgram2 = _mesa_delete_program2;
1403 driver->DeleteShader = _mesa_delete_shader;
1404 driver->DetachShader = _mesa_detach_shader;
1405 driver->GetActiveAttrib = _mesa_get_active_attrib;
1406 driver->GetActiveUniform = _mesa_get_active_uniform;
1407 driver->GetAttachedShaders = _mesa_get_attached_shaders;
1408 driver->GetAttribLocation = _mesa_get_attrib_location;
1409 driver->GetHandle = _mesa_get_handle;
1410 driver->GetProgramiv = _mesa_get_programiv;
1411 driver->GetProgramInfoLog = _mesa_get_program_info_log;
1412 driver->GetShaderiv = _mesa_get_shaderiv;
1413 driver->GetShaderInfoLog = _mesa_get_shader_info_log;
1414 driver->GetShaderSource = _mesa_get_shader_source;
1415 driver->GetUniformfv = _mesa_get_uniformfv;
1416 driver->GetUniformLocation = _mesa_get_uniform_location;
1417 driver->IsProgram = _mesa_is_program;
1418 driver->IsShader = _mesa_is_shader;
1419 driver->LinkProgram = _mesa_link_program;
1420 driver->ShaderSource = _mesa_shader_source;
1421 driver->Uniform = _mesa_uniform;
1422 driver->UniformMatrix = _mesa_uniform_matrix;
1423 driver->UseProgram = _mesa_use_program;
1424 driver->ValidateProgram = _mesa_validate_program;
1425}