2 * Mesa 3-D graphics library
4 * Copyright (C) 2004-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009-2010 VMware, Inc. All Rights Reserved.
6 * Copyright © 2010 Intel Corporation
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * Functions related to GLSL uniform variables.
34 * 1. Check that the right error code is generated for all _mesa_error() calls.
35 * 2. Insert FLUSH_VERTICES calls in various places
38 #include "main/glheader.h"
39 #include "main/context.h"
40 #include "main/dispatch.h"
41 #include "main/shaderapi.h"
42 #include "main/shaderobj.h"
43 #include "main/uniforms.h"
44 #include "ir_uniform.h"
45 #include "glsl_types.h"
48 * Update the vertex/fragment program's TexturesUsed array.
50 * This needs to be called after glUniform(set sampler var) is called.
51 * A call to glUniform(samplerVar, value) causes a sampler to point to a
52 * particular texture unit. We know the sampler's texture target
53 * (1D/2D/3D/etc) from compile time but the sampler's texture unit is
54 * set by glUniform() calls.
56 * So, scan the program->SamplerUnits[] and program->SamplerTargets[]
57 * information to update the prog->TexturesUsed[] values.
58 * Each value of TexturesUsed[unit] is one of zero, TEXTURE_1D_INDEX,
59 * TEXTURE_2D_INDEX, TEXTURE_3D_INDEX, etc.
60 * We'll use that info for state validation before rendering.
63 _mesa_update_shader_textures_used(struct gl_shader_program
*shProg
,
64 struct gl_program
*prog
)
68 memcpy(prog
->SamplerUnits
, shProg
->SamplerUnits
, sizeof(prog
->SamplerUnits
));
69 memset(prog
->TexturesUsed
, 0, sizeof(prog
->TexturesUsed
));
71 for (s
= 0; s
< MAX_SAMPLERS
; s
++) {
72 if (prog
->SamplersUsed
& (1 << s
)) {
73 GLuint unit
= shProg
->SamplerUnits
[s
];
74 GLuint tgt
= shProg
->SamplerTargets
[s
];
75 assert(unit
< Elements(prog
->TexturesUsed
));
76 assert(tgt
< NUM_TEXTURE_TARGETS
);
77 prog
->TexturesUsed
[unit
] |= (1 << tgt
);
83 * Connect a piece of driver storage with a part of a uniform
85 * \param uni The uniform with which the storage will be associated
86 * \param element_stride Byte-stride between array elements.
87 * \sa gl_uniform_driver_storage::element_stride.
88 * \param vector_stride Byte-stride between vectors (in a matrix).
89 * \sa gl_uniform_driver_storage::vector_stride.
90 * \param format Conversion from native format to driver format
91 * required by the driver.
92 * \param data Location to dump the data.
95 _mesa_uniform_attach_driver_storage(struct gl_uniform_storage
*uni
,
96 unsigned element_stride
,
97 unsigned vector_stride
,
98 enum gl_uniform_driver_format format
,
101 uni
->driver_storage
= (struct gl_uniform_driver_storage
*)
102 realloc(uni
->driver_storage
,
103 sizeof(struct gl_uniform_driver_storage
)
104 * (uni
->num_driver_storage
+ 1));
106 uni
->driver_storage
[uni
->num_driver_storage
].element_stride
= element_stride
;
107 uni
->driver_storage
[uni
->num_driver_storage
].vector_stride
= vector_stride
;
108 uni
->driver_storage
[uni
->num_driver_storage
].format
= (uint8_t) format
;
109 uni
->driver_storage
[uni
->num_driver_storage
].data
= data
;
111 uni
->num_driver_storage
++;
115 * Sever all connections with all pieces of driver storage for all uniforms
118 * This function does \b not release any of the \c data pointers
119 * previously passed in to \c _mesa_uniform_attach_driver_stoarge.
122 _mesa_uniform_detach_all_driver_storage(struct gl_uniform_storage
*uni
)
124 free(uni
->driver_storage
);
125 uni
->driver_storage
= NULL
;
126 uni
->num_driver_storage
= 0;
130 _mesa_Uniform1fARB(GLint location
, GLfloat v0
)
132 GET_CURRENT_CONTEXT(ctx
);
133 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, &v0
, GL_FLOAT
);
137 _mesa_Uniform2fARB(GLint location
, GLfloat v0
, GLfloat v1
)
139 GET_CURRENT_CONTEXT(ctx
);
143 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_FLOAT_VEC2
);
147 _mesa_Uniform3fARB(GLint location
, GLfloat v0
, GLfloat v1
, GLfloat v2
)
149 GET_CURRENT_CONTEXT(ctx
);
154 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_FLOAT_VEC3
);
158 _mesa_Uniform4fARB(GLint location
, GLfloat v0
, GLfloat v1
, GLfloat v2
,
161 GET_CURRENT_CONTEXT(ctx
);
167 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_FLOAT_VEC4
);
171 _mesa_Uniform1iARB(GLint location
, GLint v0
)
173 GET_CURRENT_CONTEXT(ctx
);
174 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, &v0
, GL_INT
);
178 _mesa_Uniform2iARB(GLint location
, GLint v0
, GLint v1
)
180 GET_CURRENT_CONTEXT(ctx
);
184 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_INT_VEC2
);
188 _mesa_Uniform3iARB(GLint location
, GLint v0
, GLint v1
, GLint v2
)
190 GET_CURRENT_CONTEXT(ctx
);
195 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_INT_VEC3
);
199 _mesa_Uniform4iARB(GLint location
, GLint v0
, GLint v1
, GLint v2
, GLint v3
)
201 GET_CURRENT_CONTEXT(ctx
);
207 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_INT_VEC4
);
211 _mesa_Uniform1fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
213 GET_CURRENT_CONTEXT(ctx
);
214 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_FLOAT
);
218 _mesa_Uniform2fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
220 GET_CURRENT_CONTEXT(ctx
);
221 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_FLOAT_VEC2
);
225 _mesa_Uniform3fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
227 GET_CURRENT_CONTEXT(ctx
);
228 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_FLOAT_VEC3
);
232 _mesa_Uniform4fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
234 GET_CURRENT_CONTEXT(ctx
);
235 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_FLOAT_VEC4
);
239 _mesa_Uniform1ivARB(GLint location
, GLsizei count
, const GLint
* value
)
241 GET_CURRENT_CONTEXT(ctx
);
242 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_INT
);
246 _mesa_Uniform2ivARB(GLint location
, GLsizei count
, const GLint
* value
)
248 GET_CURRENT_CONTEXT(ctx
);
249 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_INT_VEC2
);
253 _mesa_Uniform3ivARB(GLint location
, GLsizei count
, const GLint
* value
)
255 GET_CURRENT_CONTEXT(ctx
);
256 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_INT_VEC3
);
260 _mesa_Uniform4ivARB(GLint location
, GLsizei count
, const GLint
* value
)
262 GET_CURRENT_CONTEXT(ctx
);
263 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_INT_VEC4
);
267 /** OpenGL 3.0 GLuint-valued functions **/
269 _mesa_Uniform1ui(GLint location
, GLuint v0
)
271 GET_CURRENT_CONTEXT(ctx
);
272 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, &v0
, GL_UNSIGNED_INT
);
276 _mesa_Uniform2ui(GLint location
, GLuint v0
, GLuint v1
)
278 GET_CURRENT_CONTEXT(ctx
);
282 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_UNSIGNED_INT_VEC2
);
286 _mesa_Uniform3ui(GLint location
, GLuint v0
, GLuint v1
, GLuint v2
)
288 GET_CURRENT_CONTEXT(ctx
);
293 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_UNSIGNED_INT_VEC3
);
297 _mesa_Uniform4ui(GLint location
, GLuint v0
, GLuint v1
, GLuint v2
, GLuint v3
)
299 GET_CURRENT_CONTEXT(ctx
);
305 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_UNSIGNED_INT_VEC4
);
309 _mesa_Uniform1uiv(GLint location
, GLsizei count
, const GLuint
*value
)
311 GET_CURRENT_CONTEXT(ctx
);
312 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_UNSIGNED_INT
);
316 _mesa_Uniform2uiv(GLint location
, GLsizei count
, const GLuint
*value
)
318 GET_CURRENT_CONTEXT(ctx
);
319 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_UNSIGNED_INT_VEC2
);
323 _mesa_Uniform3uiv(GLint location
, GLsizei count
, const GLuint
*value
)
325 GET_CURRENT_CONTEXT(ctx
);
326 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_UNSIGNED_INT_VEC3
);
330 _mesa_Uniform4uiv(GLint location
, GLsizei count
, const GLuint
*value
)
332 GET_CURRENT_CONTEXT(ctx
);
333 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_UNSIGNED_INT_VEC4
);
339 _mesa_UniformMatrix2fvARB(GLint location
, GLsizei count
, GLboolean transpose
,
340 const GLfloat
* value
)
342 GET_CURRENT_CONTEXT(ctx
);
343 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
344 2, 2, location
, count
, transpose
, value
);
348 _mesa_UniformMatrix3fvARB(GLint location
, GLsizei count
, GLboolean transpose
,
349 const GLfloat
* value
)
351 GET_CURRENT_CONTEXT(ctx
);
352 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
353 3, 3, location
, count
, transpose
, value
);
357 _mesa_UniformMatrix4fvARB(GLint location
, GLsizei count
, GLboolean transpose
,
358 const GLfloat
* value
)
360 GET_CURRENT_CONTEXT(ctx
);
361 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
362 4, 4, location
, count
, transpose
, value
);
367 * Non-square UniformMatrix are OpenGL 2.1
370 _mesa_UniformMatrix2x3fv(GLint location
, GLsizei count
, GLboolean transpose
,
371 const GLfloat
*value
)
373 GET_CURRENT_CONTEXT(ctx
);
374 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
375 2, 3, location
, count
, transpose
, value
);
379 _mesa_UniformMatrix3x2fv(GLint location
, GLsizei count
, GLboolean transpose
,
380 const GLfloat
*value
)
382 GET_CURRENT_CONTEXT(ctx
);
383 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
384 3, 2, location
, count
, transpose
, value
);
388 _mesa_UniformMatrix2x4fv(GLint location
, GLsizei count
, GLboolean transpose
,
389 const GLfloat
*value
)
391 GET_CURRENT_CONTEXT(ctx
);
392 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
393 2, 4, location
, count
, transpose
, value
);
397 _mesa_UniformMatrix4x2fv(GLint location
, GLsizei count
, GLboolean transpose
,
398 const GLfloat
*value
)
400 GET_CURRENT_CONTEXT(ctx
);
401 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
402 4, 2, location
, count
, transpose
, value
);
406 _mesa_UniformMatrix3x4fv(GLint location
, GLsizei count
, GLboolean transpose
,
407 const GLfloat
*value
)
409 GET_CURRENT_CONTEXT(ctx
);
410 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
411 3, 4, location
, count
, transpose
, value
);
415 _mesa_UniformMatrix4x3fv(GLint location
, GLsizei count
, GLboolean transpose
,
416 const GLfloat
*value
)
418 GET_CURRENT_CONTEXT(ctx
);
419 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
420 4, 3, location
, count
, transpose
, value
);
425 _mesa_GetnUniformfvARB(GLhandleARB program
, GLint location
,
426 GLsizei bufSize
, GLfloat
*params
)
428 GET_CURRENT_CONTEXT(ctx
);
429 _mesa_get_uniform(ctx
, program
, location
, bufSize
, GLSL_TYPE_FLOAT
, params
);
433 _mesa_GetUniformfvARB(GLhandleARB program
, GLint location
, GLfloat
*params
)
435 _mesa_GetnUniformfvARB(program
, location
, INT_MAX
, params
);
440 _mesa_GetnUniformivARB(GLhandleARB program
, GLint location
,
441 GLsizei bufSize
, GLint
*params
)
443 GET_CURRENT_CONTEXT(ctx
);
444 _mesa_get_uniform(ctx
, program
, location
, bufSize
, GLSL_TYPE_INT
, params
);
448 _mesa_GetUniformivARB(GLhandleARB program
, GLint location
, GLint
*params
)
450 _mesa_GetnUniformivARB(program
, location
, INT_MAX
, params
);
456 _mesa_GetnUniformuivARB(GLhandleARB program
, GLint location
,
457 GLsizei bufSize
, GLuint
*params
)
459 GET_CURRENT_CONTEXT(ctx
);
460 _mesa_get_uniform(ctx
, program
, location
, bufSize
, GLSL_TYPE_UINT
, params
);
464 _mesa_GetUniformuiv(GLhandleARB program
, GLint location
, GLuint
*params
)
466 _mesa_GetnUniformuivARB(program
, location
, INT_MAX
, params
);
472 _mesa_GetnUniformdvARB(GLhandleARB program
, GLint location
,
473 GLsizei bufSize
, GLdouble
*params
)
475 GET_CURRENT_CONTEXT(ctx
);
483 _mesa_get_uniform(ctx, program, location, bufSize, GLSL_TYPE_DOUBLE, params);
485 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetUniformdvARB"
486 "(GL_ARB_gpu_shader_fp64 not implemented)");
490 _mesa_GetUniformdv(GLhandleARB program
, GLint location
, GLdouble
*params
)
492 _mesa_GetnUniformdvARB(program
, location
, INT_MAX
, params
);
497 _mesa_GetUniformLocationARB(GLhandleARB programObj
, const GLcharARB
*name
)
499 struct gl_shader_program
*shProg
;
501 GET_CURRENT_CONTEXT(ctx
);
503 shProg
= _mesa_lookup_shader_program_err(ctx
, programObj
,
504 "glGetUniformLocation");
508 /* Page 80 (page 94 of the PDF) of the OpenGL 2.1 spec says:
510 * "If program has not been successfully linked, the error
511 * INVALID_OPERATION is generated."
513 if (shProg
->LinkStatus
== GL_FALSE
) {
514 _mesa_error(ctx
, GL_INVALID_OPERATION
,
515 "glGetUniformLocation(program not linked)");
519 return _mesa_get_uniform_location(ctx
, shProg
, name
);
524 * Plug in shader uniform-related functions into API dispatch table.
527 _mesa_init_shader_uniform_dispatch(struct _glapi_table
*exec
)
530 SET_Uniform1fARB(exec
, _mesa_Uniform1fARB
);
531 SET_Uniform2fARB(exec
, _mesa_Uniform2fARB
);
532 SET_Uniform3fARB(exec
, _mesa_Uniform3fARB
);
533 SET_Uniform4fARB(exec
, _mesa_Uniform4fARB
);
534 SET_Uniform1iARB(exec
, _mesa_Uniform1iARB
);
535 SET_Uniform2iARB(exec
, _mesa_Uniform2iARB
);
536 SET_Uniform3iARB(exec
, _mesa_Uniform3iARB
);
537 SET_Uniform4iARB(exec
, _mesa_Uniform4iARB
);
538 SET_Uniform1fvARB(exec
, _mesa_Uniform1fvARB
);
539 SET_Uniform2fvARB(exec
, _mesa_Uniform2fvARB
);
540 SET_Uniform3fvARB(exec
, _mesa_Uniform3fvARB
);
541 SET_Uniform4fvARB(exec
, _mesa_Uniform4fvARB
);
542 SET_Uniform1ivARB(exec
, _mesa_Uniform1ivARB
);
543 SET_Uniform2ivARB(exec
, _mesa_Uniform2ivARB
);
544 SET_Uniform3ivARB(exec
, _mesa_Uniform3ivARB
);
545 SET_Uniform4ivARB(exec
, _mesa_Uniform4ivARB
);
546 SET_UniformMatrix2fvARB(exec
, _mesa_UniformMatrix2fvARB
);
547 SET_UniformMatrix3fvARB(exec
, _mesa_UniformMatrix3fvARB
);
548 SET_UniformMatrix4fvARB(exec
, _mesa_UniformMatrix4fvARB
);
550 SET_GetActiveUniformARB(exec
, _mesa_GetActiveUniformARB
);
551 SET_GetUniformLocationARB(exec
, _mesa_GetUniformLocationARB
);
552 SET_GetUniformfvARB(exec
, _mesa_GetUniformfvARB
);
553 SET_GetUniformivARB(exec
, _mesa_GetUniformivARB
);
556 SET_UniformMatrix2x3fv(exec
, _mesa_UniformMatrix2x3fv
);
557 SET_UniformMatrix3x2fv(exec
, _mesa_UniformMatrix3x2fv
);
558 SET_UniformMatrix2x4fv(exec
, _mesa_UniformMatrix2x4fv
);
559 SET_UniformMatrix4x2fv(exec
, _mesa_UniformMatrix4x2fv
);
560 SET_UniformMatrix3x4fv(exec
, _mesa_UniformMatrix3x4fv
);
561 SET_UniformMatrix4x3fv(exec
, _mesa_UniformMatrix4x3fv
);
564 SET_Uniform1uiEXT(exec
, _mesa_Uniform1ui
);
565 SET_Uniform2uiEXT(exec
, _mesa_Uniform2ui
);
566 SET_Uniform3uiEXT(exec
, _mesa_Uniform3ui
);
567 SET_Uniform4uiEXT(exec
, _mesa_Uniform4ui
);
568 SET_Uniform1uivEXT(exec
, _mesa_Uniform1uiv
);
569 SET_Uniform2uivEXT(exec
, _mesa_Uniform2uiv
);
570 SET_Uniform3uivEXT(exec
, _mesa_Uniform3uiv
);
571 SET_Uniform4uivEXT(exec
, _mesa_Uniform4uiv
);
572 SET_GetUniformuivEXT(exec
, _mesa_GetUniformuiv
);
574 /* GL_ARB_robustness */
575 SET_GetnUniformfvARB(exec
, _mesa_GetnUniformfvARB
);
576 SET_GetnUniformivARB(exec
, _mesa_GetnUniformivARB
);
577 SET_GetnUniformuivARB(exec
, _mesa_GetnUniformuivARB
);
578 SET_GetnUniformdvARB(exec
, _mesa_GetnUniformdvARB
); /* GL 4.0 */
580 #endif /* FEATURE_GL */