Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by joshko · Nov 18, 2014 at 04:12 PM · unity 5texture2dnative plugindirectx 11

Update DX11 texture asynchronously in native plugin?

Sorry for the long question :D

tl;dr version :) Is it possible to update DirectX 11 textures asynchronously in native plugin (C++)?

I've written a plugin that retrieves data from Kinect. Currently, color stream data is written to a texture created by Unity in a separate thread in the plugin using DirectX 9 as shown in the example below.

 D3DLOCKED_RECT lockedRectangle;
 colorFrameTextureDX9->LockRect(0, &lockedRectangle, NULL, 0);
 memcpy(lockedRectangle.pBits, colorFrameMat.data, colorFrameMat.total() * colorFrameMat.channels());
 colorFrameTextureDX9->UnlockRect(0);

With DX9 this works beautifully. As far as I know calling LockRect on a DX9 texture locks the texture data and memcpy after that is thread safe.


Using this code sample from unity I've added support for DX11 textures.

 ID3D11DeviceContext *ctx = NULL;
 device->GetImmediateContext(&ctx); // Device is sent from Unity
 
 D3D11_TEXTURE2D_DESC desc;
 colorFrameTextureDX11->GetDesc(&desc);
 ctx->UpdateSubresource(colorFrameTextureDX11, 0, NULL, colorFrameMat.data, colorFrameMat.cols * colorFrameMat.channels(), 0);
 ctx->Release();

When Unity is run in DX11 mode, this runs perfectly for a few second after which it crashes without error report. I believe the problem is in using UpdateSubresource method which doesn't lock the texture causing simultaneous access from Unity's rendering pipeline and plugin thread.

In DX11 documentation I came across the Map/Unmap methods for updating textures which work similarly to LockRect/UnlockRect methods from DX9. However these methods can only be used with DX11 textures created as D3D11_USAGE_DYNAMIC and the texture from Unity is created as D3D11_USAGE_DEFAULT.

Is it possible to change or recreate the texture created in Unity with different usage flags?

Thanks in advance to anyone who even read it all :D

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Daloots · Aug 27, 2015 at 06:11 PM 0
Share

Hi Joshko, I'm bumping into exactly the same problem. Did you ever manage to find a workaround? Thanks!

avatar image joshko · Aug 28, 2015 at 08:57 AM 0
Share

Hi Daloots. Unfortunately I haven't managed to find a workaround. I had to skip this problem since it wasn't entirely necessary for the project I was working on. I'll be getting back to it soon and I'll definitely post here if I manage to solve it.

avatar image Daloots · Aug 28, 2015 at 09:01 AM 1
Share

Thanks for the reply! Well, at least it's on the roadmap for 5.3. I'll post a reply here too should I find some solution.

avatar image joshko · Sep 10, 2015 at 05:18 PM 0
Share

Daloots: I received an answer to the problem HERE so I thought you might be interested. Cheers.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by creisner · Mar 29, 2018 at 03:09 PM

@joshko I am currently working on a similar task and would like to know if you have found a solution?,@joshko I am currently working on a similar task and would like to know if you have found a solution?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image buFFalo94 · Apr 07, 2018 at 11:11 AM 0
Share

I don't know if it's can help you but I was able to create D3D11 textures and use it in unity if you are interested we can help each other

avatar image
0

Answer by TegraMorgan · Aug 25, 2020 at 04:48 PM

@buFFalo94 @joshko
Hello.
In Unity in order to update the texture, it has to be done from the rendering thread. You do this by calling GL.IssuePluginEvent(_nativeCallback, rID) where nativecallback is a function that does the update, and rID is an integer that this function will receive. You cannot send anything else to this function.
So in order for this to work you will have to make at least three calls and maybe even four (if you want a return value).
1. Get the nativeCallback from the plugin. It should be saved as System.IntPtr.
2. Set all the variables for your thread function - store them in a struct and save it in map<int,renderStruct> where the int is the rID that you will be passing to your render thread, so he will be able to find all the parameters.
3. Call GL.IssuePluginEvent(). From the function that will be called find your struct in the map by using the rID you provided in a call. Note that render thread functions always return void.
4. If you have a return value it should be saved in a different map and retrieved using fourth function that will find the result using same rID.


And now I have a question for you - You said that you managed to create D3D11 texture from the native. Every time I try to do it, by using the same method described above, I get an error from Texture2D.CreateExternalTexture - it crashes with Access violation reading location 0x0000000000000003 in UnityPlayer.dll!TexturesD3D11Base::RegisterNativeTexture(struct ID3D11ShaderResourceView *)
Please explain to me how did you manage to initialize a texture from the native side.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

30 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Directx12 CreateExternalTexture 2 Answers

Is Texture.apply() for Unity == glTexSubImage2D on native side on Unity 1 Answer

Unity 5.2 Text Rendering issue 0 Answers

Change texture of plane pixel by pixel (Unity 5) 0 Answers

Text Mesh Pro text is unreadable 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges