glUniform4fv is giving GL_INVALID_OPERATION - iphone

I' trying to develop a basic game in iOS and OpenGL ES but I'm stuck on this problem with the uniforms, here is the code that passes the value to my uniform:
glBindVertexArrayOES(_vertexArray);
// Render the object with ES2
glUseProgram(self.shaderProgram);
glUniformMatrix4fv(uniformModelViewProjection, 1, 0, modelViewProjectionMatrix.m);
// Get uniform center position
glUniform4fv(uniformCenterPosition, 1, centerPosition.v);
// Get uniform time position
glUniform1f(uniformTime, time);
// Set the sampler texture unit to 0
glUniform1i(uniformTexture, 0);
glDrawArrays(GL_POINTS, 0, numVertices);
Notice that care has been taken to position the glUniform function preceded with glUseProgram and before the glDrawArrays call is made. The uniform locations look fine also as confirmed via tracing. Here is what I get when I run the OpenGL ES Analyzer tool in XCode:
It returns a GL_INVALID_OPERATION for glUniform4fv, notice that the values represented seem to be correct.
Here are the possible causes for the GL_INVALID_OPERATION error I found from the documentation:
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, vec4, or an array of these, or if 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, uvec4, or an array of these.
one of the signed integer variants of this function is used to load a uniform variable of type unsigned int, uvec2, uvec3, uvec4, or an array of these.
one of the unsigned integer variants of this function is used to load a uniform variable of type int, ivec2, ivec3, ivec4, or an array of these.
location is an invalid uniform location for the current program object and location is not equal to -1.
count is greater than 1 and the indicated uniform variable is not an array variable.
a sampler is loaded using a command other than glUniform1i and glUniform1iv.
None of them seem to explain why the heck I am receiving this error. It's driving me crazy, please help!

Adding my comment as an answer, since it turned out to be the solution:
The only causes from that list that I could imagine are points 2 and 3:
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, vec4, or an
array of these, or if 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, uvec4, or an array of these.
So make sure that the corresponding uniform variable is really declared as vec4 in the shader (maybe it's a vec3?).

Related

Dividing a character with a Float in Ada

So i´m trying to divide a character with a float using an operator, but I dont know why my code gets the error message "unexpected argument for "Position" attribute"
function "/"(Teck: in Character;
Flyt: in Float) return Float is
begin
return Float(Character'Position(Teck))/Flyt;
end "/";
Can somebody explain how Character'position works and what I need to change here, because I´ve used pretty much the same code previously in a different assigment.
Regarding the ARM, characters are, for example, defined as
UC_C_Cedilla : constant Character := 'Ç'; --Character'Val(199)
And if you read the operations on discrete types in the ARM, you'll se that the inverse attribute of Val is Pos, not Position.

Is there any way to set length of integer array with variable in hlsl?

firstly, I wish you guys can understand awkward grammar skills of next writing. English is not my first language.
Im currently using UnityEngine Now. what i wanna do is sending a number of rows to a shader, so that i can set a count of rows of stripes in Gameobject mesh using the shader which got the number of stripes.
And I made to send a number variable to a shader, but when i try to create int array in CG program part(which is HLSL) with size of the rows that i want using the number, unity Engine gives me this error message - "array dimensions must be literal scalar expressions".
This is the integer variable that i set in my unity shader script. This gets integer value from c# script function(this part doenst have any issue)
_LowCount ("LowCount", int) = 0
And this is CG Program part which im struggling with.
The variable below is declared in global field. It receives number value from the properties.
int _LowCount;
And this is fragment shader function part and it declares integer array in its local field setting the array size on integer variable - "_LowCount"
fixed4 frag(v2f i):COLOR{
fixed4 c=0;
int ColorsArray[_LowCount];
for(int aa=0;aa<_LowCount;aa++){
ColorsArray[aa]=0;
}
return c;
And below part from fragment shader function gives me the error that i mentioned in above.
int ColorsArray[_LowCount];
I searched this issue in google, then i realized i have to set array size with number value( not a variable). But I need an integer array with size of number variable that i can give any integer value anytime i want. Is there any solution?
*ps. I started to learn CG graphics from just 2 weeks ago. So I might be wrong in my understading and my knowledge. Thank you.
There is no way to define an hlsl array with variable size. From the docs:
Literal scalar expressions declared containing most other types
Either preallocate with the maximum array size int ColorsArray[maximum possible _LowCount];
It's not super clear what your end goal is, but another solution may be to execute a different shader for each object instead. See if you can update your question a little and I'll update the answer.

Conversion from int16 to unsigned int16 may overflow

After Polyspace code check I am getting "conversion from int16 to unsigned int16 may overflow".
uint16 lData = 0x00u;
sint16 AnalogInputValue;
lData = (uint16)AnalogInputValue; => This line causes Polyspace error
Should the type cast do the job ? According to Polyspace no :)
The following two lines do the same thing:
lData = AnalogInputValue;
lData = (uint16)AnalogInputValue;
Why? The target of the assignment lData is of type uint16, therefore the value stored in the variable AnalogInputValue will have to be converted to uint16 in either case. The variable AnalogInputValue, however, is of type sint16.
The warning comes from the following fact: Variables of type uint16 can hold values in the range 0..65535. But, variables of type sint16 can typically hold values in the range -32768..32767. Therefore, if AnalogInputValue happens to hold a value in the range -32768..-1, then this value can not be represented by an uint16.
Therefore, before doing the assignment, you might add some code around it that checks that AnalogInputValue is not negative. Which means, it holds a value from 0..32767. All these values can be represented in uint16. And, for the other case, namely that the check reveals that AnalogInputValue happens to be negative, you have to find some acceptable solution.
There is one potential third scenario here: You are 100% sure that AnalogInputValue will never hold a negative value, but the logic is too complex for Polyspace to deduce that fact, or the data comes from an external source (which seems to be the case here, as the value is called AnalogInputValue). Then, adding an assertion before the assignment can be used as a means to instruct Polyspace that it shall make this assumption.
You first need to assure that AnalogInputValue does not contain any negative numbers before you type cast it as uint16. If you do ot do that then you risk to loose data in the type cast.

Make Matlab issue a warning when converting a double to a uint8 and vice versa?

Typically in Matlab colours are represented by three element vectors of RGB intensity values, with precision uint8 (range 0 - 255) or double (range 0 - 1). Matlabs functions such as imshow work with either representation making both easy to use in a program.
It is equally easy however to introduce a bug when assigning colour values from a matrix of one type, to that of another (because the value is converted silently, but not re-scaled to the new range). Having just spent a number of hours finding such a bug, I would like to make sure it is never introduced again.
How do I make Matlab display a warning when type conversion takes place?
Ideally it would only be when the conversion is between double and uint8. It should also be difficult to deactivate (i.e. the option is not reset when loading a workspace, or when matlab crashes).
A possible solution is to define your own uint8 function that casts to uint8 and issues a warning if some value has been truncated.
You should place this function in a folder where it shadows the builtin uint8 funciton. For example, your user folder is a good choice, as it usually appears the first in path.
Or, as noted by Sam Roberts, if you want this function to be called only when converting from double into uint8 (not when converting from any other type into uint8), put it in a folder named #double within your path.
function y = uint8(x)
y = builtin('uint8', x);
if any(x(:)>255) || any(x(:)<0)
warning('MATLAB:castTruncation', 'Values truncated during conversion to uint8')
end
The warning is on by default. You can switch it on or off with the commands warning('on','MATLAB:castTruncation') and warning('off','MATLAB:castTruncation') (thanks to CitizenInsane for the suggestion).

Why is find in matlab returning double values

The find function within matlab returns indices where the given locigal argument evaluates true.
Thus I'm wondering, why the return values (for the indices) are of type double and not uint32 or uint64 like the biggest index into a matrix could be.
Another strange thing which might be connected to that here is, that running
[~,max_num_of_elem]=computer
returns the maximal number of elements allowed for a matrix in the variable max_num_of_elem which is also of type double.
I can only guess, but probably because a wide range of functions only support double. Run
setdiff(methods('double'), methods('uint32'))
to see what functions are defined for double and not for uint32 on your version of MATLAB.
Also there is the overflow issue with integer data types in MATLAB that can introduce some hard to detect bugs.