- Home /
How does Texture2D.CreateExternalTexture work?
I would like to create and update a texture in a C++ plugin in order to build an Interface to a GPUDirect enabled professional video card. What I have already figured out is the way from Unity/DX11 to video. Now I need to figure out how to capture video. Therefore I need a way to write texture data into a texture and use that texture in Unity. I thought Texture2D.CreateExternalTexture would be the way to do this, but whenever I try to use it, I get a hard crash from somewhere inside an internal Unity function.
This is what I do:
IntPtr retVal = Marshal.ReadIntPtr(GetTexture());
if(retVal.ToInt32() != 0)
{
inputTexture = Texture2D.CreateExternalTexture(1920, 1080, TextureFormat.RGB24, false, false, retVal);
}
This is the Call Stack:
Unity.exe!TexturesD3D11::TextureFromShaderResourceView(ID3D11ShaderResourceView * resourceView, ID3D11Texture2D * * texture) Line 146 C++
> Unity.exe!TexturesD3D11::RegisterNativeTexture(ID3D11ShaderResourceView * resourceView) Line 160 C++
Unity.exe!GfxDeviceClient::CreateExternalTextureFromNative(int nativeTex) Line 3108 C++
Unity.exe!Texture2D::InitTexture(int width, int height, unsigned int format, int options, int imageCount, int nativeTex) Line 469 C++
What am I doing wrong?
Answer by samizzo · Jun 25, 2014 at 09:30 AM
Did you ever get this working? I don't think you want to do a ReadIntPtr - that reads an integer from the memory address that you pass in. What does your GetTexture() function return?
It looks like you are using D3D11, so you should be passing an existing ID3D11ShaderResourceView pointer to CreateExternalTexture. You'll need an IntPtr that is really a pointer to an ID3D11ShaderResourceView, which you should have created in native code somewhere (e.g. in your native plugin).
my solution was in the end: Create a Texture2D in Unity and pass it to the plugin. The other way wouldn't work.
Your answer
Follow this Question
Related Questions
How should I release a DirectX11 texture created in a native plugin? 1 Answer
How can I get the Native Plugin example to work on Unity 5? 0 Answers
How Texture2D.CreateExternalTexture works with Direct3D11? 1 Answer
Unity5 filled my SSD? 0 Answers
IL2CPP C# passed ref float[] to C++ can't get right result 1 Answer