Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Mesa 3-D graphics library |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 3 | * Version: 6.5.3 |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 4 | * |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 5 | * Copyright (C) 2004-2007 Brian Paul All Rights Reserved. |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 8 | * copy of this software and associated documentation files (the "Software"), |
| 9 | * to deal in the Software without restriction, including without limitation |
| 10 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 11 | * and/or sell copies of the Software, and to permit persons to whom the |
| 12 | * Software is furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be included |
| 15 | * in all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 18 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 | * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 21 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | /** |
| 26 | * \file shader_api.c |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 27 | * Implementation of GLSL-related API functions |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 28 | * \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. |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 34 | * 2. Insert FLUSH_VERTICES calls in various places |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 35 | */ |
| 36 | |
| 37 | |
| 38 | #include "glheader.h" |
| 39 | #include "context.h" |
| 40 | #include "hash.h" |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 41 | #include "program.h" |
| 42 | #include "prog_parameter.h" |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 43 | #include "shader_api.h" |
| 44 | |
| 45 | #include "slang_compile.h" |
| 46 | #include "slang_link.h" |
| 47 | |
| 48 | |
| 49 | |
Brian | f292361 | 2006-12-20 09:56:44 -0700 | [diff] [blame^] | 50 | /** |
| 51 | * Allocate a new gl_shader_program object, initialize it. |
| 52 | */ |
| 53 | struct gl_shader_program * |
| 54 | _mesa_new_shader_program(GLcontext *ctx, GLuint name) |
| 55 | { |
| 56 | struct gl_shader_program *shProg; |
| 57 | shProg = CALLOC_STRUCT(gl_shader_program); |
| 58 | if (shProg) { |
| 59 | shProg->Type = GL_SHADER_PROGRAM; |
| 60 | shProg->Name = name; |
| 61 | shProg->RefCount = 1; |
| 62 | } |
| 63 | return shProg; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | void |
| 68 | _mesa_free_shader_program_data(GLcontext *ctx, |
| 69 | struct gl_shader_program *shProg) |
| 70 | { |
| 71 | assert(shProg->Type == GL_SHADER_PROGRAM); |
| 72 | |
| 73 | if (shProg->VertexProgram) { |
| 74 | if (shProg->VertexProgram->Base.Parameters == shProg->Uniforms) { |
| 75 | /* to prevent a double-free in the next call */ |
| 76 | shProg->VertexProgram->Base.Parameters = NULL; |
| 77 | } |
| 78 | _mesa_delete_program(ctx, &shProg->VertexProgram->Base); |
| 79 | shProg->VertexProgram = NULL; |
| 80 | } |
| 81 | |
| 82 | if (shProg->FragmentProgram) { |
| 83 | if (shProg->FragmentProgram->Base.Parameters == shProg->Uniforms) { |
| 84 | /* to prevent a double-free in the next call */ |
| 85 | shProg->FragmentProgram->Base.Parameters = NULL; |
| 86 | } |
| 87 | _mesa_delete_program(ctx, &shProg->FragmentProgram->Base); |
| 88 | shProg->FragmentProgram = NULL; |
| 89 | } |
| 90 | |
| 91 | |
| 92 | if (shProg->Uniforms) { |
| 93 | _mesa_free_parameter_list(shProg->Uniforms); |
| 94 | shProg->Uniforms = NULL; |
| 95 | } |
| 96 | |
| 97 | if (shProg->Varying) { |
| 98 | _mesa_free_parameter_list(shProg->Varying); |
| 99 | shProg->Varying = NULL; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | |
| 104 | |
| 105 | void |
| 106 | _mesa_free_shader_program(GLcontext *ctx, struct gl_shader_program *shProg) |
| 107 | { |
| 108 | _mesa_free_shader_program_data(ctx, shProg); |
| 109 | _mesa_free(shProg); |
| 110 | } |
| 111 | |
| 112 | |
| 113 | /** |
| 114 | * Lookup a GLSL program object. |
| 115 | */ |
| 116 | struct gl_shader_program * |
| 117 | _mesa_lookup_shader_program(GLcontext *ctx, GLuint name) |
| 118 | { |
| 119 | struct gl_shader_program *shProg; |
| 120 | if (name) { |
| 121 | shProg = (struct gl_shader_program *) |
| 122 | _mesa_HashLookup(ctx->Shared->ShaderObjects, name); |
| 123 | /* Note that both gl_shader and gl_shader_program objects are kept |
| 124 | * in the same hash table. Check the object's type to be sure it's |
| 125 | * what we're expecting. |
| 126 | */ |
| 127 | if (shProg && shProg->Type != GL_SHADER_PROGRAM) { |
| 128 | return NULL; |
| 129 | } |
| 130 | return shProg; |
| 131 | } |
| 132 | return NULL; |
| 133 | } |
| 134 | |
| 135 | |
| 136 | /** |
| 137 | * Allocate a new gl_shader object, initialize it. |
| 138 | */ |
| 139 | struct gl_shader * |
| 140 | _mesa_new_shader(GLcontext *ctx, GLuint name, GLenum type) |
| 141 | { |
| 142 | struct gl_shader *shader; |
| 143 | assert(type == GL_FRAGMENT_SHADER || type == GL_VERTEX_SHADER); |
| 144 | shader = CALLOC_STRUCT(gl_shader); |
| 145 | if (shader) { |
| 146 | shader->Type = type; |
| 147 | shader->Name = name; |
| 148 | shader->RefCount = 1; |
| 149 | } |
| 150 | return shader; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | void |
| 155 | _mesa_free_shader(GLcontext *ctx, struct gl_shader *sh) |
| 156 | { |
| 157 | GLuint i; |
| 158 | if (sh->Source) |
| 159 | _mesa_free((void *) sh->Source); |
| 160 | if (sh->InfoLog) |
| 161 | _mesa_free(sh->InfoLog); |
| 162 | for (i = 0; i < sh->NumPrograms; i++) { |
| 163 | assert(sh->Programs[i]); |
| 164 | _mesa_delete_program(ctx, sh->Programs[i]); |
| 165 | } |
| 166 | if (sh->Programs) |
| 167 | _mesa_free(sh->Programs); |
| 168 | _mesa_free(sh); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | /** |
| 173 | * Lookup a GLSL shader object. |
| 174 | */ |
| 175 | struct gl_shader * |
| 176 | _mesa_lookup_shader(GLcontext *ctx, GLuint name) |
| 177 | { |
| 178 | if (name) { |
| 179 | struct gl_shader *sh = (struct gl_shader *) |
| 180 | _mesa_HashLookup(ctx->Shared->ShaderObjects, name); |
| 181 | /* Note that both gl_shader and gl_shader_program objects are kept |
| 182 | * in the same hash table. Check the object's type to be sure it's |
| 183 | * what we're expecting. |
| 184 | */ |
| 185 | if (sh && sh->Type == GL_SHADER_PROGRAM) { |
| 186 | assert(sh->Type == GL_VERTEX_SHADER || |
| 187 | sh->Type == GL_FRAGMENT_SHADER); |
| 188 | return NULL; |
| 189 | } |
| 190 | return sh; |
| 191 | } |
| 192 | return NULL; |
| 193 | } |
| 194 | |
| 195 | |
| 196 | void |
| 197 | _mesa_init_shader_state(GLcontext * ctx) |
| 198 | { |
| 199 | ctx->Shader._FragmentShaderPresent = GL_FALSE; |
| 200 | ctx->Shader._VertexShaderPresent = GL_FALSE; |
| 201 | } |
| 202 | |
| 203 | |
| 204 | |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 205 | |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 206 | |
| 207 | /** |
| 208 | * Copy string from <src> to <dst>, up to maxLength characters, returning |
| 209 | * length of <dst> in <length>. |
| 210 | * \param src the strings source |
| 211 | * \param maxLength max chars to copy |
| 212 | * \param length returns number of chars copied |
| 213 | * \param dst the string destination |
| 214 | */ |
| 215 | static void |
| 216 | copy_string(GLchar *dst, GLsizei maxLength, GLsizei *length, const GLchar *src) |
| 217 | { |
| 218 | GLsizei len; |
| 219 | for (len = 0; len < maxLength - 1 && src && src[len]; len++) |
| 220 | dst[len] = src[len]; |
| 221 | if (maxLength > 0) |
| 222 | dst[len] = 0; |
| 223 | if (length) |
| 224 | *length = len; |
| 225 | } |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | /** |
| 231 | * Called via ctx->Driver.AttachShader() |
| 232 | */ |
| 233 | void |
| 234 | _mesa_attach_shader(GLcontext *ctx, GLuint program, GLuint shader) |
| 235 | { |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 236 | struct gl_shader_program *shProg |
| 237 | = _mesa_lookup_shader_program(ctx, program); |
| 238 | struct gl_shader *sh = _mesa_lookup_shader(ctx, shader); |
| 239 | const GLuint n = shProg->NumShaders; |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 240 | GLuint i; |
| 241 | |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 242 | if (!shProg || !sh) { |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 243 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 244 | "glAttachShader(bad program or shader name)"); |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | for (i = 0; i < n; i++) { |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 249 | if (shProg->Shaders[i] == sh) { |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 250 | /* already attached */ |
| 251 | return; |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 252 | } |
| 253 | } |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 254 | |
| 255 | /* grow list */ |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 256 | shProg->Shaders = (struct gl_shader **) |
| 257 | _mesa_realloc(shProg->Shaders, |
| 258 | n * sizeof(struct gl_shader *), |
| 259 | (n + 1) * sizeof(struct gl_shader *)); |
| 260 | if (!shProg->Shaders) { |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 261 | _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAttachShader"); |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | /* append */ |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 266 | shProg->Shaders[n] = sh; |
| 267 | sh->RefCount++; |
| 268 | shProg->NumShaders++; |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | |
| 272 | void |
| 273 | _mesa_bind_attrib_location(GLcontext *ctx, GLuint program, GLuint index, |
| 274 | const GLchar *name) |
| 275 | { |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 276 | struct gl_shader_program *shProg |
| 277 | = _mesa_lookup_shader_program(ctx, program); |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 278 | |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 279 | if (!shProg) { |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 280 | _mesa_error(ctx, GL_INVALID_OPERATION, "glBindAttribLocation(program)"); |
| 281 | return; |
| 282 | } |
| 283 | |
Brian | 9e4bae9 | 2006-12-20 09:27:42 -0700 | [diff] [blame] | 284 | if (!name) |
| 285 | return; |
| 286 | |
| 287 | if (strncmp(name, "gl_", 3) == 0) { |
| 288 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 289 | "glBindAttribLocation(illegal name)"); |
| 290 | return; |
| 291 | } |
| 292 | |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 293 | #if 0 /* XXXX */ |
| 294 | if (name == NULL || index >= MAX_VERTEX_ATTRIBS) |
| 295 | _mesa_error(ctx, GL_INVALID_VALUE, "glBindAttribLocationARB"); |
| 296 | else if (IS_NAME_WITH_GL_PREFIX(name)) |
| 297 | _mesa_error(ctx, GL_INVALID_OPERATION, "glBindAttribLocationARB"); |
| 298 | else |
| 299 | (**pro).OverrideAttribBinding(pro, index, name); |
| 300 | RELEASE_PROGRAM(pro); |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 301 | #endif |
| 302 | } |
| 303 | |
| 304 | |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 305 | GLuint |
| 306 | _mesa_create_shader(GLcontext *ctx, GLenum type) |
| 307 | { |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 308 | struct gl_shader *sh; |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 309 | GLuint name; |
| 310 | |
| 311 | name = _mesa_HashFindFreeKeyBlock(ctx->Shared->ShaderObjects, 1); |
| 312 | |
| 313 | switch (type) { |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 314 | case GL_FRAGMENT_SHADER: |
| 315 | case GL_VERTEX_SHADER: |
| 316 | sh = _mesa_new_shader(ctx, name, type); |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 317 | break; |
| 318 | default: |
| 319 | _mesa_error(ctx, GL_INVALID_ENUM, "CreateShader(type)"); |
| 320 | return 0; |
| 321 | } |
| 322 | |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 323 | _mesa_HashInsert(ctx->Shared->ShaderObjects, name, sh); |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 324 | |
| 325 | return name; |
| 326 | } |
| 327 | |
| 328 | |
| 329 | GLuint |
| 330 | _mesa_create_program(GLcontext *ctx) |
| 331 | { |
| 332 | GLuint name; |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 333 | struct gl_shader_program *shProg; |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 334 | |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 335 | name = _mesa_HashFindFreeKeyBlock(ctx->Shared->ShaderObjects, 1); |
| 336 | shProg = _mesa_new_shader_program(ctx, name); |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 337 | |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 338 | _mesa_HashInsert(ctx->Shared->ShaderObjects, name, shProg); |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 339 | |
| 340 | return name; |
| 341 | } |
| 342 | |
| 343 | |
| 344 | void |
| 345 | _mesa_delete_program2(GLcontext *ctx, GLuint name) |
| 346 | { |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 347 | struct gl_shader_program *shProg; |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 348 | |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 349 | shProg = _mesa_lookup_shader_program(ctx, name); |
| 350 | if (!shProg) { |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 351 | _mesa_error(ctx, GL_INVALID_OPERATION, "glDeleteProgram(name)"); |
| 352 | return; |
| 353 | } |
| 354 | |
Brian | 9e4bae9 | 2006-12-20 09:27:42 -0700 | [diff] [blame] | 355 | /* always remove from hash table */ |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 356 | _mesa_HashRemove(ctx->Shared->ShaderObjects, name); |
Brian | 9e4bae9 | 2006-12-20 09:27:42 -0700 | [diff] [blame] | 357 | |
| 358 | shProg->DeletePending = GL_TRUE; |
| 359 | |
| 360 | /* decrement refcount, delete if zero */ |
| 361 | shProg->RefCount--; |
| 362 | if (shProg->RefCount <= 0) { |
| 363 | _mesa_free_shader_program(ctx, shProg); |
| 364 | } |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | |
| 368 | void |
| 369 | _mesa_delete_shader(GLcontext *ctx, GLuint shader) |
| 370 | { |
Brian | 9e4bae9 | 2006-12-20 09:27:42 -0700 | [diff] [blame] | 371 | struct gl_shader *sh = _mesa_lookup_shader(ctx, shader); |
| 372 | if (!sh) { |
| 373 | return; |
| 374 | } |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 375 | |
Brian | 9e4bae9 | 2006-12-20 09:27:42 -0700 | [diff] [blame] | 376 | sh->DeletePending = GL_TRUE; |
| 377 | sh->RefCount--; |
| 378 | if (sh->RefCount <= 0) { |
| 379 | _mesa_free_shader(ctx, sh); |
| 380 | } |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | |
| 384 | void |
| 385 | _mesa_detach_shader(GLcontext *ctx, GLuint program, GLuint shader) |
| 386 | { |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 387 | struct gl_shader_program *shProg |
| 388 | = _mesa_lookup_shader_program(ctx, program); |
| 389 | const GLuint n = shProg->NumShaders; |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 390 | GLuint i, j; |
| 391 | |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 392 | if (!shProg) { |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 393 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 394 | "glDetachShader(bad program or shader name)"); |
| 395 | return; |
| 396 | } |
| 397 | |
| 398 | for (i = 0; i < n; i++) { |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 399 | if (shProg->Shaders[i]->Name == shader) { |
| 400 | struct gl_shader **newList; |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 401 | /* found it */ |
Brian | 9e4bae9 | 2006-12-20 09:27:42 -0700 | [diff] [blame] | 402 | |
| 403 | shProg->Shaders[i]->RefCount--; |
| 404 | |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 405 | /* alloc new, smaller array */ |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 406 | newList = (struct gl_shader **) |
| 407 | _mesa_malloc((n - 1) * sizeof(struct gl_shader *)); |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 408 | if (!newList) { |
| 409 | _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDetachShader"); |
| 410 | return; |
| 411 | } |
| 412 | for (j = 0; j < i; j++) { |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 413 | newList[j] = shProg->Shaders[j]; |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 414 | } |
| 415 | while (++i < n) |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 416 | newList[j++] = shProg->Shaders[i]; |
| 417 | _mesa_free(shProg->Shaders); |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 418 | |
| 419 | /* XXX refcounting! */ |
| 420 | |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 421 | shProg->Shaders = newList; |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 422 | return; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | /* not found */ |
| 427 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 428 | "glDetachShader(shader not found)"); |
| 429 | } |
| 430 | |
| 431 | |
| 432 | void |
| 433 | _mesa_get_active_attrib(GLcontext *ctx, GLuint program, GLuint index, |
| 434 | GLsizei maxLength, GLsizei *length, GLint *size, |
| 435 | GLenum *type, GLchar *nameOut) |
| 436 | { |
| 437 | static const GLenum vec_types[] = { |
| 438 | GL_FLOAT, GL_FLOAT_VEC2, GL_FLOAT_VEC3, GL_FLOAT_VEC4 |
| 439 | }; |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 440 | struct gl_shader_program *shProg |
| 441 | = _mesa_lookup_shader_program(ctx, program); |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 442 | GLint sz; |
| 443 | |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 444 | if (!shProg) { |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 445 | _mesa_error(ctx, GL_INVALID_OPERATION, "glGetActiveUniform"); |
| 446 | return; |
| 447 | } |
| 448 | |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 449 | if (!shProg->Attributes || index >= shProg->Attributes->NumParameters) { |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 450 | _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveUniform(index)"); |
| 451 | return; |
| 452 | } |
| 453 | |
| 454 | copy_string(nameOut, maxLength, length, |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 455 | shProg->Attributes->Parameters[index].Name); |
| 456 | sz = shProg->Attributes->Parameters[index].Size; |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 457 | if (size) |
| 458 | *size = sz; |
| 459 | if (type) |
| 460 | *type = vec_types[sz]; /* XXX this is a temporary hack */ |
| 461 | } |
| 462 | |
| 463 | |
| 464 | /** |
| 465 | * Called via ctx->Driver.GetActiveUniform(). |
| 466 | */ |
| 467 | void |
| 468 | _mesa_get_active_uniform(GLcontext *ctx, GLuint program, GLuint index, |
| 469 | GLsizei maxLength, GLsizei *length, GLint *size, |
| 470 | GLenum *type, GLchar *nameOut) |
| 471 | { |
| 472 | static const GLenum vec_types[] = { |
| 473 | GL_FLOAT, GL_FLOAT_VEC2, GL_FLOAT_VEC3, GL_FLOAT_VEC4 |
| 474 | }; |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 475 | struct gl_shader_program *shProg |
| 476 | = _mesa_lookup_shader_program(ctx, program); |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 477 | GLint sz; |
| 478 | |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 479 | if (!shProg) { |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 480 | _mesa_error(ctx, GL_INVALID_OPERATION, "glGetActiveUniform"); |
| 481 | return; |
| 482 | } |
| 483 | |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 484 | if (!shProg->Uniforms || index >= shProg->Uniforms->NumParameters) { |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 485 | _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveUniform(index)"); |
| 486 | return; |
| 487 | } |
| 488 | |
| 489 | copy_string(nameOut, maxLength, length, |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 490 | shProg->Uniforms->Parameters[index].Name); |
| 491 | sz = shProg->Uniforms->Parameters[index].Size; |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 492 | if (size) |
| 493 | *size = sz; |
| 494 | if (type) |
| 495 | *type = vec_types[sz]; /* XXX this is a temporary hack */ |
| 496 | } |
| 497 | |
| 498 | |
| 499 | /** |
| 500 | * Called via ctx->Driver.GetAttachedShaders(). |
| 501 | */ |
| 502 | void |
| 503 | _mesa_get_attached_shaders(GLcontext *ctx, GLuint program, GLsizei maxCount, |
| 504 | GLsizei *count, GLuint *obj) |
| 505 | { |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 506 | struct gl_shader_program *shProg |
| 507 | = _mesa_lookup_shader_program(ctx, program); |
| 508 | if (shProg) { |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 509 | GLuint i; |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 510 | for (i = 0; i < maxCount && i < shProg->NumShaders; i++) { |
| 511 | obj[i] = shProg->Shaders[i]->Name; |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 512 | } |
| 513 | if (count) |
| 514 | *count = i; |
| 515 | } |
| 516 | else { |
| 517 | _mesa_error(ctx, GL_INVALID_OPERATION, "glGetAttachedShaders"); |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | |
| 522 | GLint |
| 523 | _mesa_get_attrib_location(GLcontext *ctx, GLuint program, |
| 524 | const GLchar *name) |
| 525 | { |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 526 | struct gl_shader_program *shProg |
| 527 | = _mesa_lookup_shader_program(ctx, program); |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 528 | |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 529 | if (!shProg) { |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 530 | _mesa_error(ctx, GL_INVALID_OPERATION, "glGetAttribLocation"); |
| 531 | return -1; |
| 532 | } |
| 533 | |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 534 | if (!shProg->LinkStatus) { |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 535 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 536 | "glGetAttribLocation(program not linked)"); |
| 537 | return -1; |
| 538 | } |
| 539 | |
| 540 | if (!name) |
| 541 | return -1; |
| 542 | |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 543 | if (shProg->Attributes) { |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 544 | GLuint i; |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 545 | for (i = 0; i < shProg->Attributes->NumParameters; i++) { |
| 546 | if (!strcmp(shProg->Attributes->Parameters[i].Name, name)) { |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 547 | return i; |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | return -1; |
| 552 | } |
| 553 | |
| 554 | |
| 555 | GLuint |
| 556 | _mesa_get_handle(GLcontext *ctx, GLenum pname) |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 557 | { |
| 558 | #if 0 |
| 559 | GET_CURRENT_CONTEXT(ctx); |
| 560 | |
| 561 | switch (pname) { |
| 562 | case GL_PROGRAM_OBJECT_ARB: |
| 563 | { |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 564 | struct gl2_program_intf **pro = ctx->Shader.CurrentProgram; |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 565 | |
| 566 | if (pro != NULL) |
| 567 | return (**pro)._container._generic. |
| 568 | GetName((struct gl2_generic_intf **) (pro)); |
| 569 | } |
| 570 | break; |
| 571 | default: |
| 572 | _mesa_error(ctx, GL_INVALID_ENUM, "glGetHandleARB"); |
| 573 | } |
| 574 | #endif |
| 575 | return 0; |
| 576 | } |
| 577 | |
| 578 | |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 579 | void |
| 580 | _mesa_get_programiv(GLcontext *ctx, GLuint program, |
| 581 | GLenum pname, GLint *params) |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 582 | { |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 583 | struct gl_shader_program *shProg |
| 584 | = _mesa_lookup_shader_program(ctx, program); |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 585 | |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 586 | if (!shProg) { |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 587 | _mesa_error(ctx, GL_INVALID_VALUE, "glGetProgramiv(program)"); |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 588 | return; |
| 589 | } |
| 590 | |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 591 | switch (pname) { |
| 592 | case GL_DELETE_STATUS: |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 593 | *params = shProg->DeletePending; |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 594 | break; |
| 595 | case GL_LINK_STATUS: |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 596 | *params = shProg->LinkStatus; |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 597 | break; |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 598 | case GL_VALIDATE_STATUS: |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 599 | *params = shProg->Validated; |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 600 | break; |
| 601 | case GL_INFO_LOG_LENGTH: |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 602 | *params = shProg->InfoLog ? strlen(shProg->InfoLog) : 0; |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 603 | break; |
| 604 | case GL_ATTACHED_SHADERS: |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 605 | *params = shProg->NumShaders; |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 606 | break; |
| 607 | case GL_ACTIVE_ATTRIBUTES: |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 608 | *params = shProg->Uniforms ? shProg->Uniforms->NumParameters : 0; |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 609 | break; |
| 610 | case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH: |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 611 | *params = _mesa_parameter_longest_name(shProg->Attributes); |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 612 | break; |
| 613 | case GL_ACTIVE_UNIFORMS: |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 614 | *params = shProg->Uniforms ? shProg->Uniforms->NumParameters : 0; |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 615 | break; |
| 616 | case GL_ACTIVE_UNIFORM_MAX_LENGTH: |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 617 | *params = _mesa_parameter_longest_name(shProg->Uniforms); |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 618 | break; |
| 619 | default: |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 620 | _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramiv(pname)"); |
| 621 | return; |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 622 | } |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 623 | } |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 624 | |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 625 | |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 626 | void |
| 627 | _mesa_get_shaderiv(GLcontext *ctx, GLuint name, GLenum pname, GLint *params) |
| 628 | { |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 629 | struct gl_shader *shader = _mesa_lookup_shader(ctx, name); |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 630 | |
| 631 | if (!shader) { |
| 632 | _mesa_error(ctx, GL_INVALID_VALUE, "glGetShaderiv(shader)"); |
| 633 | return; |
| 634 | } |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 635 | |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 636 | switch (pname) { |
| 637 | case GL_SHADER_TYPE: |
| 638 | *params = shader->Type; |
| 639 | break; |
| 640 | case GL_DELETE_STATUS: |
| 641 | *params = shader->DeletePending; |
| 642 | break; |
| 643 | case GL_COMPILE_STATUS: |
| 644 | *params = shader->CompileStatus; |
| 645 | break; |
| 646 | case GL_INFO_LOG_LENGTH: |
| 647 | *params = shader->InfoLog ? strlen(shader->InfoLog) : 0; |
| 648 | break; |
| 649 | case GL_SHADER_SOURCE_LENGTH: |
| 650 | *params = shader->Source ? strlen((char *) shader->Source) : 0; |
| 651 | break; |
| 652 | default: |
| 653 | _mesa_error(ctx, GL_INVALID_ENUM, "glGetShaderiv(pname)"); |
| 654 | return; |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | |
| 659 | void |
| 660 | _mesa_get_program_info_log(GLcontext *ctx, GLuint program, GLsizei bufSize, |
| 661 | GLsizei *length, GLchar *infoLog) |
| 662 | { |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 663 | struct gl_shader_program *shProg |
| 664 | = _mesa_lookup_shader_program(ctx, program); |
| 665 | if (!shProg) { |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 666 | _mesa_error(ctx, GL_INVALID_VALUE, "glGetProgramInfoLog(program)"); |
| 667 | return; |
| 668 | } |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 669 | copy_string(infoLog, bufSize, length, shProg->InfoLog); |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | |
| 673 | void |
| 674 | _mesa_get_shader_info_log(GLcontext *ctx, GLuint shader, GLsizei bufSize, |
| 675 | GLsizei *length, GLchar *infoLog) |
| 676 | { |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 677 | struct gl_shader *sh = _mesa_lookup_shader(ctx, shader); |
| 678 | if (!sh) { |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 679 | _mesa_error(ctx, GL_INVALID_VALUE, "glGetShaderInfoLog(shader)"); |
| 680 | return; |
| 681 | } |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 682 | copy_string(infoLog, bufSize, length, sh->InfoLog); |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | |
| 686 | /** |
| 687 | * Called via ctx->Driver.GetShaderSource(). |
| 688 | */ |
| 689 | void |
| 690 | _mesa_get_shader_source(GLcontext *ctx, GLuint shader, GLsizei maxLength, |
| 691 | GLsizei *length, GLchar *sourceOut) |
| 692 | { |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 693 | struct gl_shader *sh = _mesa_lookup_shader(ctx, shader); |
| 694 | if (!sh) { |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 695 | _mesa_error(ctx, GL_INVALID_VALUE, "glGetShaderSource(shader)"); |
| 696 | return; |
| 697 | } |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 698 | copy_string(sourceOut, maxLength, length, sh->Source); |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | |
| 702 | /** |
| 703 | * Called via ctx->Driver.GetUniformfv(). |
| 704 | */ |
| 705 | void |
| 706 | _mesa_get_uniformfv(GLcontext *ctx, GLuint program, GLint location, |
| 707 | GLfloat *params) |
| 708 | { |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 709 | struct gl_shader_program *shProg |
| 710 | = _mesa_lookup_shader_program(ctx, program); |
| 711 | if (shProg) { |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 712 | GLuint i; |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 713 | if (location >= 0 && location < shProg->Uniforms->NumParameters) { |
| 714 | for (i = 0; i < shProg->Uniforms->Parameters[location].Size; i++) { |
| 715 | params[i] = shProg->Uniforms->ParameterValues[location][i]; |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 716 | } |
| 717 | } |
| 718 | else { |
| 719 | _mesa_error(ctx, GL_INVALID_VALUE, "glGetUniformfv(location)"); |
| 720 | } |
| 721 | } |
| 722 | else { |
| 723 | _mesa_error(ctx, GL_INVALID_OPERATION, "glGetUniformfv(program)"); |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | |
| 728 | /** |
| 729 | * Called via ctx->Driver.GetUniformLocation(). |
| 730 | */ |
| 731 | GLint |
| 732 | _mesa_get_uniform_location(GLcontext *ctx, GLuint program, const GLchar *name) |
| 733 | { |
| 734 | if (ctx->Shader.CurrentProgram) { |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 735 | const struct gl_shader_program *shProg = ctx->Shader.CurrentProgram; |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 736 | GLuint loc; |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 737 | for (loc = 0; loc < shProg->Uniforms->NumParameters; loc++) { |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 738 | const struct gl_program_parameter *u |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 739 | = shProg->Uniforms->Parameters + loc; |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 740 | if (u->Type == PROGRAM_UNIFORM && !strcmp(u->Name, name)) { |
| 741 | return loc; |
| 742 | } |
| 743 | } |
| 744 | } |
| 745 | return -1; |
| 746 | |
| 747 | } |
| 748 | |
| 749 | |
| 750 | GLboolean |
| 751 | _mesa_is_program(GLcontext *ctx, GLuint name) |
| 752 | { |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 753 | struct gl_shader_program *shProg = _mesa_lookup_shader_program(ctx, name); |
| 754 | return shProg ? GL_TRUE : GL_FALSE; |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | |
| 758 | GLboolean |
| 759 | _mesa_is_shader(GLcontext *ctx, GLuint name) |
| 760 | { |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 761 | struct gl_shader *shader = _mesa_lookup_shader(ctx, name); |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 762 | return shader ? GL_TRUE : GL_FALSE; |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | |
| 766 | |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 767 | /** |
| 768 | * Called via ctx->Driver.ShaderSource() |
| 769 | */ |
| 770 | void |
| 771 | _mesa_shader_source(GLcontext *ctx, GLuint shader, const GLchar *source) |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 772 | { |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 773 | struct gl_shader *sh = _mesa_lookup_shader(ctx, shader); |
| 774 | if (!sh) { |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 775 | _mesa_error(ctx, GL_INVALID_VALUE, "glShaderSource(shaderObj)"); |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 776 | return; |
| 777 | } |
| 778 | |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 779 | /* free old shader source string and install new one */ |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 780 | if (sh->Source) { |
| 781 | _mesa_free((void *) sh->Source); |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 782 | } |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 783 | sh->Source = source; |
Brian | 9e4bae9 | 2006-12-20 09:27:42 -0700 | [diff] [blame] | 784 | sh->CompileStatus = GL_FALSE; |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 788 | /** |
| 789 | * Called via ctx->Driver.CompileShader() |
| 790 | */ |
| 791 | void |
| 792 | _mesa_compile_shader(GLcontext *ctx, GLuint shaderObj) |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 793 | { |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 794 | struct gl_shader *sh = _mesa_lookup_shader(ctx, shaderObj); |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 795 | slang_info_log info_log; |
| 796 | slang_code_object obj; |
| 797 | slang_unit_type type; |
| 798 | |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 799 | if (!sh) { |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 800 | _mesa_error(ctx, GL_INVALID_VALUE, "glCompileShader(shaderObj)"); |
| 801 | return; |
| 802 | } |
| 803 | |
| 804 | slang_info_log_construct(&info_log); |
| 805 | _slang_code_object_ctr(&obj); |
| 806 | |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 807 | if (sh->Type == GL_VERTEX_SHADER) { |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 808 | type = slang_unit_vertex_shader; |
| 809 | } |
| 810 | else { |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 811 | assert(sh->Type == GL_FRAGMENT_SHADER); |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 812 | type = slang_unit_fragment_shader; |
| 813 | } |
| 814 | |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 815 | if (_slang_compile(sh->Source, &obj, type, &info_log, sh)) { |
| 816 | sh->CompileStatus = GL_TRUE; |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 817 | } |
| 818 | else { |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 819 | sh->CompileStatus = GL_FALSE; |
| 820 | /* XXX temporary */ |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 821 | _mesa_problem(ctx, "Program did not compile!"); |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 826 | /** |
| 827 | * Called via ctx->Driver.LinkProgram() |
| 828 | */ |
| 829 | void |
| 830 | _mesa_link_program(GLcontext *ctx, GLuint program) |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 831 | { |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 832 | struct gl_shader_program *shProg; |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 833 | |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 834 | shProg = _mesa_lookup_shader_program(ctx, program); |
| 835 | if (!shProg) { |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 836 | _mesa_error(ctx, GL_INVALID_OPERATION, "glLinkProgram(program)"); |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 837 | return; |
| 838 | } |
| 839 | |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 840 | _slang_link2(ctx, program, shProg); |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | |
| 844 | /** |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 845 | * Called via ctx->Driver.UseProgram() |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 846 | */ |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 847 | void |
| 848 | _mesa_use_program(GLcontext *ctx, GLuint program) |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 849 | { |
Brian | 9e4bae9 | 2006-12-20 09:27:42 -0700 | [diff] [blame] | 850 | /* unbind old */ |
| 851 | if (ctx->Shader.CurrentProgram) { |
| 852 | ctx->Shader.CurrentProgram->RefCount--; |
| 853 | if (ctx->Shader.CurrentProgram->RefCount <= 0) { |
| 854 | _mesa_free_shader_program(ctx, ctx->Shader.CurrentProgram); |
| 855 | } |
| 856 | ctx->Shader.CurrentProgram = NULL; |
| 857 | } |
| 858 | |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 859 | /* XXXX need to handle reference counting here! */ |
| 860 | if (program) { |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 861 | struct gl_shader_program *shProg; |
| 862 | shProg = _mesa_lookup_shader_program(ctx, program); |
| 863 | if (!shProg) { |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 864 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 865 | "glUseProgramObjectARB(programObj)"); |
| 866 | return; |
| 867 | } |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 868 | ctx->Shader.CurrentProgram = shProg; |
Brian | 9e4bae9 | 2006-12-20 09:27:42 -0700 | [diff] [blame] | 869 | shProg->RefCount++; |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 870 | } |
| 871 | else { |
| 872 | /* don't use a shader program */ |
| 873 | ctx->Shader.CurrentProgram = NULL; |
| 874 | } |
| 875 | } |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 876 | |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 877 | |
| 878 | /** |
| 879 | * Called via ctx->Driver.Uniform(). |
| 880 | */ |
| 881 | void |
| 882 | _mesa_uniform(GLcontext *ctx, GLint location, GLsizei count, |
| 883 | const GLvoid *values, GLenum type) |
| 884 | { |
| 885 | if (ctx->Shader.CurrentProgram) { |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 886 | struct gl_shader_program *shProg = ctx->Shader.CurrentProgram; |
| 887 | if (location >= 0 && location < shProg->Uniforms->NumParameters) { |
| 888 | GLfloat *v = shProg->Uniforms->ParameterValues[location]; |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 889 | const GLfloat *fValues = (const GLfloat *) values; /* XXX */ |
| 890 | GLint i; |
| 891 | if (type == GL_FLOAT_VEC4) |
| 892 | count *= 4; |
| 893 | else if (type == GL_FLOAT_VEC3) |
| 894 | count *= 3; |
| 895 | else |
| 896 | abort(); |
| 897 | |
| 898 | for (i = 0; i < count; i++) |
| 899 | v[i] = fValues[i]; |
| 900 | return; |
| 901 | } |
| 902 | } |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 903 | else { |
| 904 | _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(program not linked)"); |
| 905 | } |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | |
| 909 | /** |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 910 | * Called by ctx->Driver.UniformMatrix(). |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 911 | */ |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 912 | void |
| 913 | _mesa_uniform_matrix(GLcontext *ctx, GLint cols, GLint rows, |
| 914 | GLenum matrixType, GLint location, GLsizei count, |
| 915 | GLboolean transpose, const GLfloat *values) |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 916 | { |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 917 | const char *caller = "glUniformMatrix"; |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 918 | const GLint matElements = rows * cols; |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 919 | |
| 920 | if (values == NULL) { |
| 921 | _mesa_error(ctx, GL_INVALID_VALUE, caller); |
| 922 | return; |
| 923 | } |
| 924 | |
| 925 | FLUSH_VERTICES(ctx, _NEW_PROGRAM); |
| 926 | |
| 927 | if (transpose) { |
| 928 | GLfloat *trans, *pt; |
| 929 | const GLfloat *pv; |
| 930 | GLint i, j, k; |
| 931 | |
| 932 | trans = (GLfloat *) _mesa_malloc(count * matElements * sizeof(GLfloat)); |
| 933 | if (!trans) { |
| 934 | _mesa_error(ctx, GL_OUT_OF_MEMORY, caller); |
| 935 | return; |
| 936 | } |
| 937 | |
| 938 | pt = trans; |
| 939 | pv = values; |
| 940 | for (i = 0; i < count; i++) { |
| 941 | /* transpose from pv matrix into pt matrix */ |
| 942 | for (j = 0; j < cols; j++) { |
| 943 | for (k = 0; k < rows; k++) { |
| 944 | /* XXX verify this */ |
| 945 | pt[j * rows + k] = pv[k * cols + j]; |
| 946 | } |
| 947 | } |
| 948 | pt += matElements; |
| 949 | pv += matElements; |
| 950 | } |
| 951 | |
| 952 | #ifdef OLD |
| 953 | if (!(**pro).WriteUniform(pro, location, count, trans, matrixType)) |
| 954 | _mesa_error(ctx, GL_INVALID_OPERATION, caller); |
| 955 | #endif |
| 956 | _mesa_free(trans); |
| 957 | } |
| 958 | else { |
| 959 | #ifdef OLD |
| 960 | if (!(**pro).WriteUniform(pro, location, count, values, matrixType)) |
| 961 | _mesa_error(ctx, GL_INVALID_OPERATION, caller); |
| 962 | #endif |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 967 | void |
| 968 | _mesa_validate_program(GLcontext *ctx, GLuint program) |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 969 | { |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 970 | struct gl_shader_program *shProg; |
| 971 | shProg = _mesa_lookup_shader_program(ctx, program); |
| 972 | if (!shProg) { |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 973 | _mesa_error(ctx, GL_INVALID_OPERATION, "glValidateProgram(program)"); |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 974 | return; |
| 975 | } |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 976 | /* XXX temporary */ |
Brian | 65a1844 | 2006-12-19 18:46:56 -0700 | [diff] [blame] | 977 | shProg->Validated = GL_TRUE; |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 978 | |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 979 | /* From the GL spec: |
| 980 | any two active samplers in the current program object are of |
| 981 | different types, but refer to the same texture image unit, |
| 982 | |
| 983 | any active sampler in the current program object refers to a texture |
| 984 | image unit where fixed-function fragment processing accesses a |
| 985 | texture target that does not match the sampler type, or |
| 986 | |
| 987 | the sum of the number of active samplers in the program and the |
| 988 | number of texture image units enabled for fixed-function fragment |
| 989 | processing exceeds the combined limit on the total number of texture |
| 990 | image units allowed. |
| 991 | */ |
Brian | 34ae99d | 2006-12-18 08:28:54 -0700 | [diff] [blame] | 992 | } |