0

Problem: Need to share a SlimDx.Direct3D9.Surface with another process so it can show the render output. I can create the Surface, but I have not found a way to share it between processes.

Available Code I have access to both sides of the application, but the application providing the render output is limited. I have no access to the creation of the devices. I can reference the Direct3D9 device, create a render target, and successfully capture the output.

More details

I have a feeling I will be accused of not looking first, but I can assure you I have to the best of my ability.

But, I'm trying to share a DirectX 9 Surface to another process so I can capture the render output within an external form.

I have found several things on Shared Surfaces, but haven't seemed to figure out how to do it with a DirectX 9 device sharing surfaces between 2 process.

My access to the DirectX device itself is limited. I am accessing it through a built in scripting module embeded in the application. I do not have the option of knowing creation properties or parameters that aren't public variables but I do have complete access to the Device itself.

So with the code below (to demonstrate exactly what I have to work with for the most part) I'm trying to figure out how I could share that Surface with another process so I can get the renderer's output on that Form.

Basically, I have code that looks like this: (simplified)

byte[] ARGB;
//Getting internal access to renderer's Direct3D9.Device (Already created)
SlimDx.Direct3D9.Device dev = renderer.Device.Device;
//Surface I create to capture the devices output
SlimDx.Direct3D9.Surface destination; 
//Create render target to device
destination = Surface.CreateRenderTarget(this.dev, this.w, this.h, Format.A8R8G8B8, MultisampleType.None, 0, true);

// 1. Render, 
// 2. then grab snapshot (internal call), 
// 3. convert to byte array (not relevant to what I'm currently doing)
if (this.renderer.Render())
{
    board.Snapshot(this.destination); //some internal logic to get surface
    try
    {
        using (DataStream data = destination.LockRectangle(LockFlags.ReadOnly).Data)
        {
            data.Read(ARGB, 0, (int) data.Length);
        }
    }
    finally
    {
        this.destination.UnlockRectangle();
    }
}
corylulu
  • 3,449
  • 1
  • 19
  • 35

1 Answers1

2

To share textures with directx9, your device needs to be of type:

SlimDX.Direct3D9.DeviceEx mydeviceex;

Default directx9 device does not support shared resources.

Then you can get a shared handle using:

 IntPtr sharedhandle;
 Texture texture = new Texture(mydeviceex, width, height, 1, Usage.RenderTarget,    
 format, Pool.Default, out sharedhandle);

1 is for mipmaps (shared resources must have only one level). Format has some limitations too, A8R8G8B8 is quite common in that case.

mrvux
  • 8,523
  • 1
  • 27
  • 61