Jozef Culen
Jozef Culen

Reputation: 354

Formal parameter with __declspec(align('16')) won't be aligned

I am trying to make function for setting shader uniforms, but when I try to compile it I get this error :

Error 2 error C2719: 'value': formal parameter with __declspec(align('16')) won't be aligned

Here is code of function:

void Shader::setUniform(std::string name, const glm::mat4 value){
    GLint uniform = glGetUniformLocation(m_program, name.c_str());
    glUniformMatrix4fv(uniform, 1, GL_FALSE, (GLfloat*)&value);
}

I am using Visual studio 2013.

Upvotes: 20

Views: 17146

Answers (3)

Drew Dormann
Drew Dormann

Reputation: 63883

From Microsoft's documentation on that error:

The align __declspec modifier is not permitted on function parameters.

Don't copy the parameter to an unaligned location. Pass a constant reference to the existing, already-aligned data.

void Shader::setUniform(const std::string &name, const glm::mat4 & value)
//                                               ^^^^^           ^

Upvotes: 27

LastBlow
LastBlow

Reputation: 707

Pass your parameter by reference (here a 3D vector named "color"), to get rid of the alignment error:

void SetColor(btVector3 color) { m_color = color; }   // does give the error

void SetColor(btVector3 &color) { m_color = color; }  // does not

Upvotes: -2

Diniden
Diniden

Reputation: 1125

I'm not sure about the implementation of glm::mat4, but it is not set up in a way that puts the data within it on a complete 'word' location within memory or it was created in a misaligned location (you reading from a file? Doing some pointer arithmetic tricks somewhere?)

http://virtrev.blogspot.com/2010/09/memory-alignment-theory-and-c-examples.html

If you want a hack to 'get it working' you can try:

void Shader::setUniform(std::string name, const glm::mat4 value){
    // This assumes your copy constructor works correctly
    glm::mat4 alignedMat = value;
    GLint uniform = glGetUniformLocation(m_program, name.c_str());
    glUniformMatrix4fv(uniform, 1, GL_FALSE, (GLfloat*)&value);
}

I have used this trick to force align data before. You can also do a memcpy into alignedMat to force align things at times. DO NOT ASSUME this is the right way to do it. You should be creating elements with proper alignment. It's merely for seeing what is out of alignment, testing, and debugging.

Also if you ARE using the __declspec modifier on the parameter in the method definition. Don't.

Upvotes: 0

Related Questions