我在推力 cuda 中使用 float4 时遇到了内存问题
将 float4“buggyVariable”作为成员添加到仿函数似乎会导致浮点数据左移 1 个浮点数。
在 CUDA_animateParticles 我清楚地将 Y 设置为 0 和 Z 设置为 1
然而,当运行函子并在 OpenGL 中绘制它时。我得到 Xposition=1 的粒子,这表明 Y 在函子内为 1。
我还用 float2 和 float3 进行了测试,它们似乎工作正常。
所以这似乎是内存对齐问题或错误。
任何人都可以对此有所了解吗?谢谢您的帮助。
#include <thrust/sort.h>
#include <thrust/random.h>
#include <thrust/device_vector.h>
#include "cutil_math.h"
struct animateParticles_functor
{
    float4 buggyVariable; //why does adding this variable cause following floats to get wrong values???
    float pex, pey, pez, pew;
    __host__ __device__
    animateParticles_functor( float x, float y, float z, float w) :
        pex(x), pey(y), pez(z), pew(w)
    {
    }
    template <typename Tuple>
    __host__ __device__
    void operator()(Tuple t)
    {
        if(pey > 0)
            thrust::get<0>(t) = make_float4(1, 0, 0, 0); //true if y is bugged
        else
            thrust::get<0>(t) = make_float4(0, 0, 0, 0); //false if its not bugged
        return;
    }
}
void CUDA_animateParticles(float4* cuda_devicePointer_vboPosition, float3* cuda_devicePointer_particleVelocitys, unsigned int numParticles, float4 particleEmitter)
{
    thrust::device_ptr<float4> d_pos(cuda_devicePointer_vboPosition);
    thrust::device_ptr<float3> d_vel(cuda_devicePointer_particleVelocitys);
    thrust::for_each(
        thrust::make_zip_iterator(thrust::make_tuple(d_pos, d_vel)),
        thrust::make_zip_iterator(thrust::make_tuple(d_pos + numParticles, d_vel + numParticles)),
        animateParticles_functor(0, 0, 1, 0) //notice that i set Z to 1 and not Y to 0
    );
}