How to get RHI Native Device

Hi guys,

we try to access a pointer to the Native Rendering Device and use the RHIGetNativeDevice() function (as posted several time in the forum: https://answers.unrealengine.com/questions/74471/how-to-access-id3d11device.html) for that and do not know which includes are necessary for that action. Whenever we include the “RHIMethods.h” file, we get inconsistent linking errors (e.g. " ‘GpuTimeBegin_Internal’ : inconsistent dll linkage […]\4.8\Engine\Source\Runtime\RHI\Public\RHIMethods.h").

Is this the header file we need, is there anything else we need to do or are we on a completely wrong track?

We work on Windows only, so especially the D3D11Device would be interesting for us.

If you ever having linkage error from using some exotic UE4 header files there 2 possible problems:

  1. The module you trying to access is nto included in your module build script PublicDependencyModuleNames which set up directories path settings for compiler and linker without them can’t link to functions you trying to call or header file referencing. So try adding “RHI” and “D3D11RHI”
  2. Function or class you calling have MinimalAPI flag which speed up linking process in cost of not having those functions and classes accessible in other engine modules, but in this case it seems thats not the case.

There might be more but those are most common i know

You might considering trying to extend D3D11RHI instead of interacting with D3D11 aspecially inf you want to add something to rendering process, which need to be in engine rendering gears.

Hey thanks a lot for the answer!

I totally forgot about including the according Modules for function calls. Adding those modules did not change anything about the inconsistent linkage. Not including the RHIMethods Header and simply calling the RHIGetNativeDevice() function, though, fixed that error and I can call the function. (However there are new errors which I need to investigate furhter.)

Since I simply included the Engine.h in the project the MinimalAPI flag should be no problem here.

Our overall goal is to create some class that uses a certain interface to another render engine so it can add things to Unreal’s rendered image while being synchronized. One thing I want to accomplish is rather simple reusability in other projects - so I want to avoid having to change things in the Engine (which I more and more realize may not be possible).

At least I’m a step furhter, so again thanks a lot!

I tried around a little bit more. The function call seems okay now. But Unreal triggers a breakpoint when an assertion fails while trying to execute the CommandList of the RHI (the GetNativeDevice seems to be added to its CommandList as far as I understand).

Before executing the List the function checks the following:

check(IsImmediate() && IsInRenderingThread());

I can roughly imagine what and why it will check this but I really have no idea why it fails when trying to get a pointer to the D3DDevice. What can I do about this assertion?

Assertion fail happens when check() has false argument. check() is simple error check, or rether what code expects to work correctly and if condition is false it crashes the engine (In debug it triggers break point, thats how VS react to crashes).

Problem with those they don’t have proper error messages and only logs the condition it self. So to understand error, you need to negate the condition. So in this case:

IsImmediate() && IsInRenderingThread() = It’s not Immediate and need to be run on rendering thread.

Not sure what Immediate means, but i cause is that you didn’t call it via rendering thread

If you want to render something extra, why don’t you think of stading how RHI works insted of trying to communicate with D3D11 direclly? Also by extending RHI i meant do it in sperate module, so you can plug it anywhere, even in form of plugin if you like, it should guaranty it will fit to rendering gears of engine and run in rendering thread.

Hi I’m at the same point you are with this issue. Also trying to get the D3D11Device and also finally got the call set up properly, but throwing the same exception.

I’ve tried it in the ::Tick method, which I would have thought was the rendering thread, but getting the same result.

Also not sure what IsImmediate means. Very new to the engine. Did you have any more luck with this?

Hi,

sorry yes I got a little futher but I found that this may not be the pointer I was searching for. But I got something without assertion fails or anything by using a Macro to communicate with the rendering thread.

You can use ENQUEUE_UNIQUE_RENDER_COMMAND or ENQUEUE_UNIQUE_RENDER_COMMAND_XXXPARAMETER (XXX being ONE, TWO, THREE etc.) to enqueue a command for the Rendering Thread to perform when it comes to that point.

Search for the Macro in the Engine’s Source Code to see how to exactly use it. They do a lot in the Oculus Classes.

Basically you can create a struct to communicate some parameters to the Rendering Thread. And I used instance variables of the calling class to get feedback (the D3DDevice pointer). So I put the instance of the class into the calling parameters.

Right now I am not at my work PC but if you need me to be more specific just let me know and I’ll try to post some code.

How do you extend D3D11RHI to ensure that the code of the extended class is run instead of the parent class?

If I need to get access to the output texture (which is displayed on screen) and access the D3D11Device as well, where should I implement this behaviour?