# glUniform

Definition: | include pGUI.e include opengl.e glUniform1f(integer location, atom v0) -- or -- glUniform1i(integer location, integer v0) -- or -- glUniformMatrix4fv(integer location, transpose, atom pData)
Description: | Specify the value of a uniform variable for the current program object. location: Specifies the location of the uniform variable to be modified. v0, v1, v2, v3: For the scalar commands, specifies the new values to be used for the specified uniform variable. transpose: Specifies whether to transpose the matrix as the values are loaded into the uniform variable. Must be GL_FALSE. pData: Specifies a pointer to a (single) matrix that will be used to update the specified uniform variable. Note that the C API allows a count parameter such that if non-1 pData is an array of matrices, however WebGL does not support such a feature so it is coerced (on desktop/Phix) to 1.
pwa/p2js: | Supported.
Notes: | glUniform modifies the value of a uniform variable or a uniform variable array. The location of the uniform variable to be modified is specified by location, which should be a value returned by glGetUniformLocation. glUniform operates on the program object that was made part of current state by calling glUseProgram(). The commands glUniform{1|2|3|4}{f|i|ui} are used to change the value of the uniform variable specified by location using the values passed as arguments. The number specified in the command should match the number of components in the data type of the specified uniform variable (e.g., 1 for float, int, unsigned int, bool; 2 for vec2, ivec2, uvec2, bvec2, etc.). The suffix f indicates that floating-point values are being passed; the suffix i indicates that integer values are being passed; the suffix ui indicates that unsigned integer values are being passed, and this type should also match the data type of the specified uniform variable. The i variants of this function should be used to provide values for uniform variables defined as int, ivec2, ivec3, ivec4, or arrays of these. The ui variants of this function should be used to provide values for uniform variables defined as unsigned int, uvec2, uvec3, uvec4, or arrays of these. The f variants should be used to provide values for uniform variables of type float, vec2, vec3, vec4, or arrays of these. Either the i, ui or f variants may be used to provide values for uniform variables of type bool, bvec2, bvec3, bvec4, or arrays of these. The uniform variable will be set to false if the input value is 0 or 0.0f, and it will be set to true otherwise. All active uniform variables defined in a program object are initialized to 0 when the program object is linked successfully. They retain the values assigned to them by a call to glUniform until the next successful link operation occurs on the program object, when they are once again initialized to 0. The commands glUniform{1|2|3|4}{f|i|ui}v can only be used to modify a single uniform variable, unlike the C API the WebGL interface does not allow a count parameter, so therefore neither do opengl.e/js. When loading n elements starting at an arbitrary position m in a uniform variable array, elements m + n - 1 in the array will be replaced with the new values. If m + n - 1 is larger than the size of the uniform variable array, values for all array elements beyond the end of the array will be ignored. The number specified in the name of the command indicates the number of components for each element in value, and it should match the number of components in the data type of the specified uniform variable (e.g., 1 for float, int, bool; 2 for vec2, ivec2, bvec2, etc.). The data type specified in the name of the command must match the data type for the specified uniform variable as described previously for glUniform{1|2|3|4}{f|i|ui}. For uniform variable arrays, each element of the array is considered to be of the type indicated in the name of the command (e.g., glUniform3f or glUniform3fv can be used to load a uniform variable array of type vec3). The commands glUniformMatrix{2|3|4|2x3|3x2|2x4|4x2|3x4|4x3}fv are used to modify a matrix. The numbers in the command name are interpreted as the dimensionality of the matrix. The number 2 indicates a 2 x 2 matrix (i.e., 4 values), the number 3 indicates a 3 x 3 matrix (i.e., 9 values), and the number 4 indicates a 4 x 4 matrix (i.e., 16 values). Non-square matrix dimensionality is explicit, with the first number representing the number of columns and the second number representing the number of rows. For example, 2x4 indicates a 2 x 4 matrix with 2 columns and 4 rows (i.e., 8 values). If transpose is GL_FALSE, each matrix is assumed to be supplied in column major order. If transpose is GL_TRUE, each matrix is assumed to be supplied in row major order. glUniform1i and glUniform1iv are the only two functions that may be used to load uniform variables defined as sampler types. Loading samplers with any other function will result in a GL_INVALID_OPERATION error. Other than the preceding exception, if the type and size of the uniform variable as defined in the shader do not match the type and size specified in the name of the command used to load its value, a GL_INVALID_OPERATION error will be generated and the specified uniform variable will remain unchanged. If location is a value other than -1 and it does not represent a valid uniform variable location in the current program object, an error will be generated, and no changes will be made to the uniform variable storage of the current program object. If location is equal to -1, the data passed in will be silently ignored and the specified uniform variable will not be changed.
Errors: | GL_INVALID_OPERATION is generated if: there is no current program object. the size of the uniform variable declared in the shader does not match the size indicated by the glUniform command. one of the signed or unsigned integer variants of this function is used to load a uniform variable of type float, vec2, vec3, or vec4, one of the floating-point variants of this function is used to load a uniform variable of type int, ivec2, ivec3, ivec4, unsigned int, uvec2, uvec3, or uvec4. one of the signed integer variants of this function is used to load a uniform variable of type unsigned int, uvec2, uvec3, or uvec4. one of the unsigned integer variants of this function is used to load a uniform variable of type int, ivec2, ivec3, or ivec4. location is an invalid uniform location for the current program object and location is not equal to -1. a sampler is loaded using a command other than glUniform1i and glUniform1iv.
Remarks: | There are 24 C functions (see index/below, coloured illegal and linked here) named glUniformNx[v], where N is 1..4, and x is f/i/ui, plus 12 C functions named glUniformMatrixNxMfv, when N and M are 2..4 with the xM omitted for N==M. Since GL_INVALID_OPERATION is generated if the size of the uniform variable declared in the shader does not match the size indicated by the glUniform command, it is not immediately obvious how to map to/from a single glUniform() entry (or maybe 4), and for now they are individually implemented on an ad-hoc basis. Maybe once I have done/can test half a dozen or more I can perhaps re-think unifying them, or prove/conclude/accept that cannot be done. I am hoping that signed integer variants can be used to set floats, and "object" or "v with a length of 1" can be used to set individual items, to make just four or five entry points sufficient. Either way there is no point implementing all 36 routines but only testing one properly. For searching purposes only, here is a full list of all the C functions: glUniform1f glUniform2f glUniform3f glUniform4f glUniform1i glUniform2i glUniform3i glUniform4i glUniform1ui glUniform2ui glUniform3ui glUniform4ui glUniform1fv glUniform2fv glUniform3fv glUniform4fv glUniform1iv glUniform2iv glUniform3iv glUniform4iv glUniform1uiv glUniform2uiv glUniform3uiv glUniform4uiv glUniformMatrix2fv glUniformMatrix2x3fv glUniformMatrix2x4fv glUniformMatrix3fv glUniformMatrix3x2fv glUniformMatrix3x4fv glUniformMatrix4fv glUniformMatrix4x2fv glUniformMatrix4x3fv
Associated Gets: | glGet with the argument GL_CURRENT_PROGRAM glGetActiveUniform with the handle of a program object and the index of an active uniform variable glGetUniform with the handle of a program object and the location of a uniform variable glGetUniformLocation with the handle of a program object and the name of a uniform variable
See Also: | glLinkProgram, glUseProgram, glmath