#Issue with copying of derived class

41 messages · Page 1 of 1 (latest)

golden marten
#

I'm currently having an issue when I'm memcpying the data from a derived class. The result is way off. In the screenshot you can see the data when it's uploaded to the GPU.

class Light
{
private:
    virtual void Fill() = 0;
public:
    int id = -1;
    Vector3 color;
    float strenght = 1;
    Light() = default;
};

class PointLight : public Light
{
private:
    virtual void Fill() override {};
public:
    PointLight()
        :Light() {};
    Vector4 position;
};

Won't the PointLight class and its data match this struct on the GPU in the HLSL shader?

struct PointLight
{
    int id;
    float3 color;
    float strenght;
    float4 position;
};
winged stumpBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

golden marten
errant berry
#

are doing doing a straight up memcpy?

#

what exactly are you doing?

golden marten
#

It's very complex, a lot of code. But here is how I'm doing the memcpy. ```
numBytes == 48, _frameIndex == 1, _offset == 0

errant berry
#

cause those 2 classes are not identical in memory

#

there is a vtable etc

golden marten
errant berry
#

it's not just POD

golden marten
#

Ah, so the vtable takes actual space? I knew about the vtable, but didnt think it added space

errant berry
#

ofc, it needs to know what function to take

golden marten
#

So how does it look in memory? And why can I cast it, and get the derived class?

manic summit
#

The vtable pointer takes the space yep

golden marten
errant berry
#

propably, in all fairness I would be highly dubious to simply memcopy any non POD class

golden marten
golden marten
#

since I have to make the base class abstract

errant berry
#

are you casting a pointer or?

golden marten
#

to be able to use dynamic

errant berry
#

why does the base need to be abstract?

golden marten
#
LightComponent pLight;
pLight.light = new PointLight;
PointLight* actualLight = (PointLight*)pLight.light;
actualLight->color = Vector3(0, 1, 0);
actualLight->position = Vector4(0, 1, 0, 1);
actualLight->strenght = 2;

PointLight* copy = dynamic_cast<PointLight*>(pLight.light);
                                
if (copy != nullptr)
{
graphics.scene->AddComponentToEntity<LightComponent>(graphics.scene->GetEntity(selectedEntityID), pLight);

PointLight* comp = (PointLight*)graphics.scene->GetComponentForEntity<LightComponent>(graphics.scene->GetEntity(selectedEntityID))->light;
                                    graphics.lManager.AddLight(graphics.device, graphics.directCmdList, *comp, graphics.frameIndex);
errant berry
#

(PointLight*) this is not a dynamic cast btw

#

honestly I would simply just threat all the lights as different components

golden marten
golden marten
errant berry
#

yeah that is

#

keep in mind dynamic casts can fail

#

but why not just have a pointlight component?

golden marten
errant berry
#

just completly get rid of casts

manic summit
golden marten
manic summit
#

But im on phone and looking at the standard is impossible

errant berry
golden marten
#

which is why im checking for it

#

I guess I'll just add 1 component for each light :/

#

!solved