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

ぃ、小莉子 提交于 2019-11-27 03:50:45

问题


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.


回答1:


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, aligned data.

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



回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/28488986/formal-parameter-with-declspecalign16-wont-be-aligned

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!