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_program
*prog
)
67 memset(prog
->TexturesUsed
, 0, sizeof(prog
->TexturesUsed
));
69 for (s
= 0; s
< MAX_SAMPLERS
; s
++) {
70 if (prog
->SamplersUsed
& (1 << s
)) {
71 GLuint unit
= prog
->SamplerUnits
[s
];
72 GLuint tgt
= prog
->SamplerTargets
[s
];
73 assert(unit
< Elements(prog
->TexturesUsed
));
74 assert(tgt
< NUM_TEXTURE_TARGETS
);
75 prog
->TexturesUsed
[unit
] |= (1 << tgt
);
81 * Connect a piece of driver storage with a part of a uniform
83 * \param uni The uniform with which the storage will be associated
84 * \param element_stride Byte-stride between array elements.
85 * \sa gl_uniform_driver_storage::element_stride.
86 * \param vector_stride Byte-stride between vectors (in a matrix).
87 * \sa gl_uniform_driver_storage::vector_stride.
88 * \param format Conversion from native format to driver format
89 * required by the driver.
90 * \param data Location to dump the data.
93 _mesa_uniform_attach_driver_storage(struct gl_uniform_storage
*uni
,
94 unsigned element_stride
,
95 unsigned vector_stride
,
96 enum gl_uniform_driver_format format
,
99 uni
->driver_storage
= (struct gl_uniform_driver_storage
*)
100 realloc(uni
->driver_storage
,
101 sizeof(struct gl_uniform_driver_storage
)
102 * (uni
->num_driver_storage
+ 1));
104 uni
->driver_storage
[uni
->num_driver_storage
].element_stride
= element_stride
;
105 uni
->driver_storage
[uni
->num_driver_storage
].vector_stride
= vector_stride
;
106 uni
->driver_storage
[uni
->num_driver_storage
].format
= (uint8_t) format
;
107 uni
->driver_storage
[uni
->num_driver_storage
].data
= data
;
109 uni
->num_driver_storage
++;
113 * Sever all connections with all pieces of driver storage for all uniforms
116 * This function does \b not release any of the \c data pointers
117 * previously passed in to \c _mesa_uniform_attach_driver_stoarge.
120 _mesa_uniform_detach_all_driver_storage(struct gl_uniform_storage
*uni
)
122 free(uni
->driver_storage
);
123 uni
->driver_storage
= NULL
;
124 uni
->num_driver_storage
= 0;
128 _mesa_Uniform1fARB(GLint location
, GLfloat v0
)
130 GET_CURRENT_CONTEXT(ctx
);
131 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, &v0
, GL_FLOAT
);
135 _mesa_Uniform2fARB(GLint location
, GLfloat v0
, GLfloat v1
)
137 GET_CURRENT_CONTEXT(ctx
);
141 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_FLOAT_VEC2
);
145 _mesa_Uniform3fARB(GLint location
, GLfloat v0
, GLfloat v1
, GLfloat v2
)
147 GET_CURRENT_CONTEXT(ctx
);
152 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_FLOAT_VEC3
);
156 _mesa_Uniform4fARB(GLint location
, GLfloat v0
, GLfloat v1
, GLfloat v2
,
159 GET_CURRENT_CONTEXT(ctx
);
165 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_FLOAT_VEC4
);
169 _mesa_Uniform1iARB(GLint location
, GLint v0
)
171 GET_CURRENT_CONTEXT(ctx
);
172 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, &v0
, GL_INT
);
176 _mesa_Uniform2iARB(GLint location
, GLint v0
, GLint v1
)
178 GET_CURRENT_CONTEXT(ctx
);
182 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_INT_VEC2
);
186 _mesa_Uniform3iARB(GLint location
, GLint v0
, GLint v1
, GLint v2
)
188 GET_CURRENT_CONTEXT(ctx
);
193 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_INT_VEC3
);
197 _mesa_Uniform4iARB(GLint location
, GLint v0
, GLint v1
, GLint v2
, GLint v3
)
199 GET_CURRENT_CONTEXT(ctx
);
205 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_INT_VEC4
);
209 _mesa_Uniform1fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
211 GET_CURRENT_CONTEXT(ctx
);
212 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_FLOAT
);
216 _mesa_Uniform2fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
218 GET_CURRENT_CONTEXT(ctx
);
219 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_FLOAT_VEC2
);
223 _mesa_Uniform3fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
225 GET_CURRENT_CONTEXT(ctx
);
226 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_FLOAT_VEC3
);
230 _mesa_Uniform4fvARB(GLint location
, GLsizei count
, const GLfloat
* value
)
232 GET_CURRENT_CONTEXT(ctx
);
233 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_FLOAT_VEC4
);
237 _mesa_Uniform1ivARB(GLint location
, GLsizei count
, const GLint
* value
)
239 GET_CURRENT_CONTEXT(ctx
);
240 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_INT
);
244 _mesa_Uniform2ivARB(GLint location
, GLsizei count
, const GLint
* value
)
246 GET_CURRENT_CONTEXT(ctx
);
247 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_INT_VEC2
);
251 _mesa_Uniform3ivARB(GLint location
, GLsizei count
, const GLint
* value
)
253 GET_CURRENT_CONTEXT(ctx
);
254 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_INT_VEC3
);
258 _mesa_Uniform4ivARB(GLint location
, GLsizei count
, const GLint
* value
)
260 GET_CURRENT_CONTEXT(ctx
);
261 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_INT_VEC4
);
265 /** OpenGL 3.0 GLuint-valued functions **/
267 _mesa_Uniform1ui(GLint location
, GLuint v0
)
269 GET_CURRENT_CONTEXT(ctx
);
270 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, &v0
, GL_UNSIGNED_INT
);
274 _mesa_Uniform2ui(GLint location
, GLuint v0
, GLuint v1
)
276 GET_CURRENT_CONTEXT(ctx
);
280 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_UNSIGNED_INT_VEC2
);
284 _mesa_Uniform3ui(GLint location
, GLuint v0
, GLuint v1
, GLuint v2
)
286 GET_CURRENT_CONTEXT(ctx
);
291 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_UNSIGNED_INT_VEC3
);
295 _mesa_Uniform4ui(GLint location
, GLuint v0
, GLuint v1
, GLuint v2
, GLuint v3
)
297 GET_CURRENT_CONTEXT(ctx
);
303 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, 1, v
, GL_UNSIGNED_INT_VEC4
);
307 _mesa_Uniform1uiv(GLint location
, GLsizei count
, const GLuint
*value
)
309 GET_CURRENT_CONTEXT(ctx
);
310 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_UNSIGNED_INT
);
314 _mesa_Uniform2uiv(GLint location
, GLsizei count
, const GLuint
*value
)
316 GET_CURRENT_CONTEXT(ctx
);
317 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_UNSIGNED_INT_VEC2
);
321 _mesa_Uniform3uiv(GLint location
, GLsizei count
, const GLuint
*value
)
323 GET_CURRENT_CONTEXT(ctx
);
324 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_UNSIGNED_INT_VEC3
);
328 _mesa_Uniform4uiv(GLint location
, GLsizei count
, const GLuint
*value
)
330 GET_CURRENT_CONTEXT(ctx
);
331 _mesa_uniform(ctx
, ctx
->Shader
.ActiveProgram
, location
, count
, value
, GL_UNSIGNED_INT_VEC4
);
337 _mesa_UniformMatrix2fvARB(GLint location
, GLsizei count
, GLboolean transpose
,
338 const GLfloat
* value
)
340 GET_CURRENT_CONTEXT(ctx
);
341 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
342 2, 2, location
, count
, transpose
, value
);
346 _mesa_UniformMatrix3fvARB(GLint location
, GLsizei count
, GLboolean transpose
,
347 const GLfloat
* value
)
349 GET_CURRENT_CONTEXT(ctx
);
350 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
351 3, 3, location
, count
, transpose
, value
);
355 _mesa_UniformMatrix4fvARB(GLint location
, GLsizei count
, GLboolean transpose
,
356 const GLfloat
* value
)
358 GET_CURRENT_CONTEXT(ctx
);
359 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
360 4, 4, location
, count
, transpose
, value
);
365 * Non-square UniformMatrix are OpenGL 2.1
368 _mesa_UniformMatrix2x3fv(GLint location
, GLsizei count
, GLboolean transpose
,
369 const GLfloat
*value
)
371 GET_CURRENT_CONTEXT(ctx
);
372 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
373 2, 3, location
, count
, transpose
, value
);
377 _mesa_UniformMatrix3x2fv(GLint location
, GLsizei count
, GLboolean transpose
,
378 const GLfloat
*value
)
380 GET_CURRENT_CONTEXT(ctx
);
381 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
382 3, 2, location
, count
, transpose
, value
);
386 _mesa_UniformMatrix2x4fv(GLint location
, GLsizei count
, GLboolean transpose
,
387 const GLfloat
*value
)
389 GET_CURRENT_CONTEXT(ctx
);
390 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
391 2, 4, location
, count
, transpose
, value
);
395 _mesa_UniformMatrix4x2fv(GLint location
, GLsizei count
, GLboolean transpose
,
396 const GLfloat
*value
)
398 GET_CURRENT_CONTEXT(ctx
);
399 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
400 4, 2, location
, count
, transpose
, value
);
404 _mesa_UniformMatrix3x4fv(GLint location
, GLsizei count
, GLboolean transpose
,
405 const GLfloat
*value
)
407 GET_CURRENT_CONTEXT(ctx
);
408 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
409 3, 4, location
, count
, transpose
, value
);
413 _mesa_UniformMatrix4x3fv(GLint location
, GLsizei count
, GLboolean transpose
,
414 const GLfloat
*value
)
416 GET_CURRENT_CONTEXT(ctx
);
417 _mesa_uniform_matrix(ctx
, ctx
->Shader
.ActiveProgram
,
418 4, 3, location
, count
, transpose
, value
);
423 _mesa_GetnUniformfvARB(GLhandleARB program
, GLint location
,
424 GLsizei bufSize
, GLfloat
*params
)
426 GET_CURRENT_CONTEXT(ctx
);
427 _mesa_get_uniform(ctx
, program
, location
, bufSize
, GLSL_TYPE_FLOAT
, params
);
431 _mesa_GetUniformfvARB(GLhandleARB program
, GLint location
, GLfloat
*params
)
433 _mesa_GetnUniformfvARB(program
, location
, INT_MAX
, params
);
438 _mesa_GetnUniformivARB(GLhandleARB program
, GLint location
,
439 GLsizei bufSize
, GLint
*params
)
441 GET_CURRENT_CONTEXT(ctx
);
442 _mesa_get_uniform(ctx
, program
, location
, bufSize
, GLSL_TYPE_INT
, params
);
446 _mesa_GetUniformivARB(GLhandleARB program
, GLint location
, GLint
*params
)
448 _mesa_GetnUniformivARB(program
, location
, INT_MAX
, params
);
454 _mesa_GetnUniformuivARB(GLhandleARB program
, GLint location
,
455 GLsizei bufSize
, GLuint
*params
)
457 GET_CURRENT_CONTEXT(ctx
);
458 _mesa_get_uniform(ctx
, program
, location
, bufSize
, GLSL_TYPE_UINT
, params
);
462 _mesa_GetUniformuiv(GLhandleARB program
, GLint location
, GLuint
*params
)
464 _mesa_GetnUniformuivARB(program
, location
, INT_MAX
, params
);
470 _mesa_GetnUniformdvARB(GLhandleARB program
, GLint location
,
471 GLsizei bufSize
, GLdouble
*params
)
473 GET_CURRENT_CONTEXT(ctx
);
481 _mesa_get_uniform(ctx, program, location, bufSize, GL_DOUBLE, params);
483 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetUniformdvARB"
484 "(GL_ARB_gpu_shader_fp64 not implemented)");
488 _mesa_GetUniformdv(GLhandleARB program
, GLint location
, GLdouble
*params
)
490 _mesa_GetnUniformdvARB(program
, location
, INT_MAX
, params
);
495 _mesa_GetUniformLocationARB(GLhandleARB programObj
, const GLcharARB
*name
)
497 struct gl_shader_program
*shProg
;
499 GET_CURRENT_CONTEXT(ctx
);
501 shProg
= _mesa_lookup_shader_program_err(ctx
, programObj
,
502 "glGetUniformLocation");
506 /* Page 80 (page 94 of the PDF) of the OpenGL 2.1 spec says:
508 * "If program has not been successfully linked, the error
509 * INVALID_OPERATION is generated."
511 if (shProg
->LinkStatus
== GL_FALSE
) {
512 _mesa_error(ctx
, GL_INVALID_OPERATION
,
513 "glGetUniformLocation(program not linked)");
517 return _mesa_get_uniform_location(ctx
, shProg
, name
);
522 * Plug in shader uniform-related functions into API dispatch table.
525 _mesa_init_shader_uniform_dispatch(struct _glapi_table
*exec
)
528 SET_Uniform1fARB(exec
, _mesa_Uniform1fARB
);
529 SET_Uniform2fARB(exec
, _mesa_Uniform2fARB
);
530 SET_Uniform3fARB(exec
, _mesa_Uniform3fARB
);
531 SET_Uniform4fARB(exec
, _mesa_Uniform4fARB
);
532 SET_Uniform1iARB(exec
, _mesa_Uniform1iARB
);
533 SET_Uniform2iARB(exec
, _mesa_Uniform2iARB
);
534 SET_Uniform3iARB(exec
, _mesa_Uniform3iARB
);
535 SET_Uniform4iARB(exec
, _mesa_Uniform4iARB
);
536 SET_Uniform1fvARB(exec
, _mesa_Uniform1fvARB
);
537 SET_Uniform2fvARB(exec
, _mesa_Uniform2fvARB
);
538 SET_Uniform3fvARB(exec
, _mesa_Uniform3fvARB
);
539 SET_Uniform4fvARB(exec
, _mesa_Uniform4fvARB
);
540 SET_Uniform1ivARB(exec
, _mesa_Uniform1ivARB
);
541 SET_Uniform2ivARB(exec
, _mesa_Uniform2ivARB
);
542 SET_Uniform3ivARB(exec
, _mesa_Uniform3ivARB
);
543 SET_Uniform4ivARB(exec
, _mesa_Uniform4ivARB
);
544 SET_UniformMatrix2fvARB(exec
, _mesa_UniformMatrix2fvARB
);
545 SET_UniformMatrix3fvARB(exec
, _mesa_UniformMatrix3fvARB
);
546 SET_UniformMatrix4fvARB(exec
, _mesa_UniformMatrix4fvARB
);
548 SET_GetActiveUniformARB(exec
, _mesa_GetActiveUniformARB
);
549 SET_GetUniformLocationARB(exec
, _mesa_GetUniformLocationARB
);
550 SET_GetUniformfvARB(exec
, _mesa_GetUniformfvARB
);
551 SET_GetUniformivARB(exec
, _mesa_GetUniformivARB
);
554 SET_UniformMatrix2x3fv(exec
, _mesa_UniformMatrix2x3fv
);
555 SET_UniformMatrix3x2fv(exec
, _mesa_UniformMatrix3x2fv
);
556 SET_UniformMatrix2x4fv(exec
, _mesa_UniformMatrix2x4fv
);
557 SET_UniformMatrix4x2fv(exec
, _mesa_UniformMatrix4x2fv
);
558 SET_UniformMatrix3x4fv(exec
, _mesa_UniformMatrix3x4fv
);
559 SET_UniformMatrix4x3fv(exec
, _mesa_UniformMatrix4x3fv
);
562 SET_Uniform1uiEXT(exec
, _mesa_Uniform1ui
);
563 SET_Uniform2uiEXT(exec
, _mesa_Uniform2ui
);
564 SET_Uniform3uiEXT(exec
, _mesa_Uniform3ui
);
565 SET_Uniform4uiEXT(exec
, _mesa_Uniform4ui
);
566 SET_Uniform1uivEXT(exec
, _mesa_Uniform1uiv
);
567 SET_Uniform2uivEXT(exec
, _mesa_Uniform2uiv
);
568 SET_Uniform3uivEXT(exec
, _mesa_Uniform3uiv
);
569 SET_Uniform4uivEXT(exec
, _mesa_Uniform4uiv
);
570 SET_GetUniformuivEXT(exec
, _mesa_GetUniformuiv
);
572 /* GL_ARB_robustness */
573 SET_GetnUniformfvARB(exec
, _mesa_GetnUniformfvARB
);
574 SET_GetnUniformivARB(exec
, _mesa_GetnUniformivARB
);
575 SET_GetnUniformuivARB(exec
, _mesa_GetnUniformuivARB
);
576 SET_GetnUniformdvARB(exec
, _mesa_GetnUniformdvARB
); /* GL 4.0 */
578 #endif /* FEATURE_GL */