0

I'm trying to copy an image onto a Unity texture from C++. The image is 320x240px, and is in RGBA format. I've verified that the image is okay.

To copy I use UpdateSubresource, here's the code:

GraphicDeviceD3D11* gdevice = (GraphicDeviceD3D11*)graphic_device;
ID3D11DeviceContext* ctx = NULL;
gdevice->d3d11device->lpVtbl->GetImmediateContext(gdevice->d3d11device, &ctx);

if (texturePointer) {
    ctx->lpVtbl->UpdateSubresource(ctx, (ID3D11Resource *)texturePointer, 0, NULL, image, imageWidth * 4, 0);
}

ctx->lpVtbl->Release(ctx);

I'm hardly a d3d11 expert, and am not sure I'm using this function correctly. The parameters of the image given to UpdateSubresource are only the image buffer pointer and the stride, but not the height of the image OR the length of the buffer.

How does UpdateResource know when to stop copying?

This code worked for me for some images, but doesn't work for others - It raises an Access Violation on memory just a bit beyond the image buffer size - So that hints me I'm not setting the limits somewhere.

Can someone shed some light on that?

Nitay
  • 4,193
  • 6
  • 33
  • 42

1 Answers1

2

You're using the function correctly - the destination resource (referred to by texturePointer in your case) contains all the required data and the method will work as expected.

However, what is this lpVtbl indirection for? If you're using c++ (and not c), you probably should stick with simple pInstance->Method() approach for invoking COM methods.

Ap31
  • 3,244
  • 1
  • 18
  • 25
  • I'm not completely sure of that as it's not my code. But it does not compile when I don't use the lpVtbl. I'll need to dive deeper into this. Thanks! – Nitay Sep 09 '16 at 13:20
  • 2
    @Nitay The `lpVtbl` is exposed by the D3D11 header if either [`__cplusplus`](http://stackoverflow.com/questions/3789340/combining-c-and-c-how-does-ifdef-cplusplus-work) is undefined (not compiling as C++) or `CINTERFACE` is defined (force C interface). – MooseBoys Sep 09 '16 at 16:37
  • @MooseBoys yes that's the issue - I'm using C. Been staring at the code for so long I completely forgot about that. Thanks! – Nitay Sep 09 '16 at 21:24
  • 1
    @Ap31 Thanks for clearing that up - You were right. The code was good. The texture size was set to the wrong size in unity. – Nitay Sep 09 '16 at 21:26