Djey
Djey

Reputation: 11

How to use CommandBuffer.IssuePluginCustomBlit on Unity Engine?

On Unity Engine, I'm trying to convert an image effect (from the asset store) from the camera event OnRenderImage(RenderTexture source, RenderTexture destination) to a camera command buffer (UnityEngine.Rendering.CommandBuffer)(to control the rendering order of all the effects stack).

The image effect rendering C# method is pretty complicated and I would rather not modify it (redoing the effect from scratch might be faster).

So the ideal solution would be calling the effect rendering C# method by a command buffer triggered event.

CommandBuffer.IssuePluginCustomBlit looks like a solution, but I can't find any example how to set it up.

It references a callback method, a source render texture and a destination render texture. There are examples of the method call (especially in the VRWorks plugin) :

buffer.IssuePluginCustomBlit(PluginExtGetIssueEventCallback(), (UInt32)command, source, dest, commandParam, commandFlags);
[DllImport("GfxPluginVRWorks32", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
    private static extern IntPtr PluginExtGetIssueEventCallback();

Nonetheless, I have no clue how the PluginExtGetIssueEventCallback is built and how it implement the source and destination render textures.

If someone would have an example to share of how to use CommandBuffer.IssuePluginCustomBlit and to create a callback method, it would be appreciated.

Thanks!

Upvotes: 1

Views: 857

Answers (2)

SebK
SebK

Reputation: 1009

I struggled to find it, but this seems to work:

In your C#:

    ...
    cmd.IssuePluginCustomBlit(GetCustomBlitCallback(), 0, source, destination, 0, 0);
    ...


    private const string PluginName = "UnityRendering";

    [DllImport(PluginName, CallingConvention = CallingConvention.StdCall)]
    private static extern IntPtr GetCustomBlitCallback();

In your native plugin:

#include "Unity/IUnityRenderingExtensions.h"

extern "C" UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API CustomBlit(unsigned int command, UnityRenderingExtCustomBlitParams* iParams)
{
    ...
}

typedef void(UNITY_INTERFACE_API* UnityRenderingCustomBlit)(
    unsigned int command,
    UnityRenderingExtCustomBlitParams* iParams);

extern "C" UNITY_INTERFACE_EXPORT UnityRenderingCustomBlit UNITY_INTERFACE_API
GetCustomBlitCallback()
{
    return CustomBlit;
}

Upvotes: 0

gregee123
gregee123

Reputation: 359

I would suggest CommandBuffer.Blit() instead. You can easily use custom shaders with it.

To create a native render plugin you would need to write separate implementations of it for each 3d api (d3d11, d3d12, vulkan, metal) for each platform you want to support (windows, android, ios). All of this in c++ except for apple where you would rather use objective c. You would also have to write your own shaders for each 3d api separately.

Just look at this small example here:

https://github.com/Unity-Technologies/NativeRenderingPlugin

Bear in mind that Unity native render plugin API is very poorly documented. You're pretty much on your own.

Upvotes: -1

Related Questions