问题描述
我正在使用 DirectXMath
,创建 XMMatrix
和 XMVector
I am using DirectXMath
, creating XMMatrix
and XMVector
in classes.
当我调用 XMMatrixMultiply
时,它会引发未处理的异常。
When I call XMMatrixMultiply
it throws unhandled exception on it.
我在网上发现这是字节分配的问题,因为 DirectXMath
使用 SIMD
指令集会导致未分配的堆分配。
I have found online that it is an issue with byte alligment, since DirectXMath
uses SIMD
instructions set which results in missaligned heap allocation.
建议的解决方案之一是使用 XMFLOAT4X4
变量,然后在需要时将它们更改为临时 XMMatrix
,但这不是最好,最快的imo解决方案。
One of the proposed solution was to use XMFLOAT4X4
variables and then change them to temporary XMMatrix
whenever needed, but it isn't the nicest and fastest solution imo.
另一个是使用 _aligned_malloc
,但是我不知道如何使用它。我从不需要做任何内存分配,这对我来说是个黑魔法。
Another one was to use _aligned_malloc
, yet I have no idea whatsoever how to use it. I have never had to do any memory allocations and it is black magic for me.
另一个是重载 new运算符
,但是他们没有提供任何信息。
Another one, was to overload new operator
, yet they did not provide any information how to do it.
关于重载方法,我没有使用 new
创建 XMMatrix
对象,因为我不将它们用作指针。
And regarding the overloading method, I'm not using new
to create XMMatrix
objects since I don't use them as pointers.
一切正常,直到我决定将代码拆分为类。
It was all working nice untill I have decided to split code into classes.
我认为 _alligned_malloc
解决方案在这里是最好的,但是我不知道如何使用它,何时何地调用它。
I think _alligned_malloc
solution would be best here, but I have no idea how to use it, where and when to call it.
推荐答案
与XMFLOAT4X4和XMFLOAT4不同,它们是安全的要存储,XMMATRIX和XMVECTOR是硬件寄存器(SSE,NEON等)的别名。由于该库正在提取寄存器类型和对齐要求,因此您不应该自己尝试对齐类型,因为您可以轻松地创建一个碰巧在您的计算机上运行但在另一个计算机上失败的程序。您应该使用安全类型进行存储(例如XMFLOAT4),或者提取抽象并直接使用矢量指令,并在应用程序中为要支持的每个矢量扩展使用特殊的存储和对齐代码路径。
Unlike XMFLOAT4X4 and XMFLOAT4, which are safe to store, XMMATRIX and XMVECTOR are aliases for hardware registers (SSE, NEON, etc.). Since the library is abstracting away the register type and alignment requirements, you shouldn't attempt to align the types yourself, since you can easily create a program that happens to work on your machine but fails on another. You should either use the safe types for storage (e.g. XMFLOAT4) or pull up the abstraction and use the vector instructions directly, with special storage and alignment code paths in your application for each vector extension you're trying to support.
此外,在库的矢量指令上下文之外使用这些寄存器可能会由于其他原因而导致意外失败。例如,如果您在自己的结构中存储XMMATRIX,则某些体系结构可能无法创建该结构的副本。
Also, using these registers outside of the context of the library's vector instructions might cause unexpected failures for other reasons. For example, if you store an XMMATRIX in your own struct, some architectures might fail to create copies of the struct.
这篇关于16字节对齐问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!