Off Screen Direct3D

Started by
7 comments, last by WarrickF 16 years, 5 months ago
Hi All, I've written a quick app that draws a simple canvas using Direct3D. I'd like to run this code as a service at the end of the day and hence do not want to render the output to the screen. Instead I'd like to save it to file in (BMP or some other format). I'm no Direct3D guru, but it seems like I'd want to write the contents of the back buffer to disk. Does anyone have any experience doing this or could point me at a link that would help? Thanks Warrick
Advertisement
Typically a Windows Service cannot have nor interact with the GUI, so I think it unlikely that you'll be able to re-write your D3D application in this form. Even if you don't present to the screen you'll need to create the device against an HWND which I assume you can't get from within a Service...

It's things like this that tend to make it tricky to write server-side/back-end processing tools using Direct3D [headshake]

Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Thanks Jack. I think you can get a HWND if you allow the service to interact with the desktop. What I'm really wanting to avoid is the overhead of moving the rendered frame to the front buffer and then out to file. What I was thinking is that I could do all the work in the back buffer and take the results straight from there.

I've seen things like DirectShow filters using Direct3D to overlay content and they are not interactive applications so I think it's possible. I'm just not much of a C++ programmer and looking for an easy way to do this is C#.
You should be able to get a texture surface by creating a texture of the correct size which you can setup as the render target for D3D.You can render to the surface by grabbing it from the surface texture->GetSurfaceLevel and calling SetRenderTarget. You can then use the D3DXSaveSurfaceToFile to save it out again.
To be honest you're talking a little above my head. I'm still VERY new to this. I've been trying to understand render targets for about an hour now and I'm still a little confused.

I also found this article:

http://www.pluralsight.com/wiki/default.aspx/Craig.DirectX/SaveToFileSample.html

Which uses SurfaceLoader.Save to do something like this:

SurfaceLoader.Save("c:\\out.jpg", ImageFileFormat.Jpg, device.GetBackBuffer(0, 0, BackBufferType.Mono));

This seemes to work and gives me access to the back buffer.

Of course I don't really know what the heck I'm doing so I'm not sure if I'm barking up the wrong tree or not :)
You can easily do the rendering and save it out to the file without seeing the contents of the image (i.e. presenting it)... The "render to texture" method is your search term here - it's commonly used in post processing so there will be LOTS of examples online.

The problem I was highlighting is when creating the device - I'm pretty sure you can't create a headless device, but you might be able to do this with D3D9Ex/D3D10 though.

As a simple test, write a Windows Service that simply creates a device - if you can get that far then you should be okay with the rest.

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

run DirectX 9 as a service/headless? (scroll to the top of the page though) from the microsoft.public.win32.programmer.directx.graphics newsgroup might well be of interest.

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Wow, that's a marathon argument. At least that answers the question though - Really poor design though in my opinion.

Thanks for everyone's help, it's very much appreciated.
Does anyone know if this is possible in OpenGL without having to jump through the same hoops?

This topic is closed to new replies.

Advertisement