访问XMMATRIX时Dx11崩溃 [英] Dx11 Crash when accessing XMMATRIX

查看:72
本文介绍了访问XMMATRIX时Dx11崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在DirectX 11中创建一个植群项目,但在尝试创建指向对象的指针的矢量时出错,并且在推回该对象的第3次迭代中,它会崩溃并跳至此:

  XMFINLINE _XMMATRIX& _XMMATRIX :: operator = 

CONST _XMMATRIX& M

{
r [0] = M.r [0];
r [1] = M.r [1];
r [2] = M.r [2];
r [3] = M.r [3];
return * this;
}

当我执行以下代码时会发生崩溃:

  Populate(); 

for(unsigned int i = 0; i< m_numOfPrey; i ++)
{
m_preyVec [i]-> LoadContent(dx,colour,yaw,pitch,roll ,比例);

srand(时间(NULL));
XMFLOAT3 tempPos;

tempPos.x = rand()%40;
tempPos.y = rand()%40;
tempPos.z = rand()%40;

m_preyVec [i]-> SetPosition(tempPos);
}

返回true;

填充函数:

  for(无符号整数i = 0; i< m_numOfPrey; i ++)
{
Prey * newPrey = new Prey();
m_preyVec.push_back(newPrey);
}

编辑:猎物从车辆类继承(构建缓冲区,FX



所以这是Prey LoadContent();

  {
Vehicle :: LoadContent(dx,colour,yaw,pitch,roll,scale);
return true;
}

这是Vehicle LoadContent();

  {
if(!GameObject :: LoadContent(dx,m_position,colour,yaw,pitch,roll,scale))
{
返回false;
}

if(!BuildBuffers(dx))
{
return false;
}

if(!BuildFX(dx) )
{
返回false;
}

if(!BuildInputLayout(dx))
{
返回false;
}

返回true;
}

任何帮助都会赞赏的,如果我需要提供更多信息,我可以这样做很好,因为我已经坚持了好几天,而且我确定这是我所缺少或做错的小事。

解决方案

您需要确保 XMVECTOR & DXMath中的 XMMATRIX 类型正确对齐为16个字节(请参阅此处的文档),对于堆栈分配,这是自动的,但是对于堆分配,则需要使用 _aligned_malloc 获取正确对齐的内存。或者,您可以使用 XMFLOATn XMFLOATnxn 进行存储,并在实际使用它们时使用未对齐的矢量加载。 / p>

对于包含XM类型的类或结构,您可以通过重载 new 运算符以显式对齐分配来解决此问题;最简单的方法是从这样的模板派生(注意:这还不完整,有几种运算符 new & 删除):

 模板< size_t align> class AlignedAllocPolicy 
{
public:
void *运算符new(std :: size_t size)
{
return _aligned_malloc(size,align);
}

无效运算符delete(void * mem)
{
_aligned_free(mem);
}
}


I've been creating a flocking swarm project in DirectX 11 and had an error where I try and create a vector of Pointers to objects and on the 3rd iteration of pushing back it will crash and jump to this:

XMFINLINE _XMMATRIX& _XMMATRIX::operator=
(
    CONST _XMMATRIX& M
)
{
    r[0] = M.r[0];
    r[1] = M.r[1];
    r[2] = M.r[2];
    r[3] = M.r[3];
    return *this;
}

the crash happens when I execute this code:

Populate();

for (unsigned int i = 0; i < m_numOfPrey; i++)
{
    m_preyVec[i]->LoadContent(dx, colour, yaw, pitch, roll, scale);

    srand (time(NULL));
    XMFLOAT3 tempPos;

    tempPos.x = rand() % 40;
    tempPos.y = rand() % 40;
    tempPos.z = rand() % 40;

    m_preyVec[i]->SetPosition(tempPos);
}

return true;

Populate function:

for (unsigned int i = 0; i < m_numOfPrey; i++)
{
    Prey* newPrey = new Prey();
    m_preyVec.push_back(newPrey);
}

EDIT: the prey inherits from a vehicle class (which builds the buffers, FX and InputLayout for the vehicle used my the Prey and Predator.

so this is the Prey LoadContent();

{
    Vehicle::LoadContent(dx, colour, yaw, pitch, roll, scale);
    return true;
}

and this is the Vehicle LoadContent();

{
    if(!GameObject::LoadContent(dx, m_position, colour, yaw, pitch, roll, scale))
    {
        return false;
    }

    if(!BuildBuffers(dx))
    {
        return false;
    }

    if(!BuildFX(dx))
    {
        return false;
    }

    if(!BuildInputLayout(dx))
    {
        return false;
    }

    return true;
}

Any help would be appreciated, if I need to provide more info I can do that as well, as I've been stuck on this for days and I'm sure it's something small I'm missing or doing wrong.

解决方案

You need to ensure that the XMVECTOR & XMMATRIX types from DXMath are correctly aligned to 16-bytes (see the docs here), for stack allocations, this is automatic, but for heap allocations, you need to use _aligned_malloc to get correctly aligned memory. Alternatively you can use the XMFLOATn and XMFLOATnxn for store and use the unaligned vector loads when you actually work with them.

For classes or structs that contain XM types you can overcome this by overloading the new operator to do explicitly align allocations; the easiest is by deriving from a template such as this (note: this isn't complete, the are a few types of operator new & delete):

template <size_t align> class AlignedAllocPolicy
{
public:
    void* operator new(std::size_t size)
    {
        return _aligned_malloc(size,align);
    }

    void operator delete(void* mem)
    {
        _aligned_free(mem);
    }
}

这篇关于访问XMMATRIX时Dx11崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆