Hi,
is float4(and rest of the CUDA vector types) supposed to work on host, or are they device only?
Because I’m getting strange behavior when using it on host.
And by strange I mean:
[codebox]struct RTMaterial
{
int Type;
float4 Color;
}
…
RTMaterial *MyMat = new RTMaterial();
MyMat->Color = make_float4(1, 0, 0, 1);
[/codebox]
(Host code)
And MyMat.Color doesn’t get assigned properly. XYZW components are all shifted(Red becoming green, green blue, in this example). This only happens when there is something before float4 in the struct(like, in this case, integer Type). If there was another int before float4, the components would shift 2 places(red becoming blue).
Remove the int Type from the struct and it will all work fine.
Replacing the float4 with four normal floats also works just fine.
And this only happens when I allocate RTMaterial on the heap. I though it was alignment related, so I tried aligning to different boundaries, both the struct itself and it’s interior, but it changed nothing.
Yes, you can use the CUDA types (float4 etc.) in host code - they are standard C types, defined in “vector_types.h”.
It’s hard to tell what your problem is without seeing more code, probably an alignment issue? How are you copying the Material structure to the GPU?
I don’t think rest of the code matters, because, if I simply change float4 to 4 floats(as in: float ColorR, ColorG, ColorB, ColorA;), while keeping all the kernels and memory copy functions intact (Aside from assignments, of course), the code works just fine.
I quickly changed my code back(only changing the struct, and assignments), to use float4 again, and here is the result. This is debugging result when calling constructor on RTMaterial. (Purely host code)

As you can notice _Color is an input parameter, and Color is a member of the struct. The debugger is paused just after Color got assigned a value from _Color. On the bottom part of the screen you can see, however, that values of Color and _Color do not match. Which results in my material having the wrong color of course.