Sign in to follow this  
GoodFun

Getting data out of a RenderTarget

Recommended Posts

And on to the next topic... Right now I'm trying to figure out how to get data out of a RenderTarget using C# and SlimDX. This is how I create the render target... _device = new D3D10.Device(D3D10.DeviceCreationFlags.Debug); DXGI.SampleDescription sampleDescription = new DXGI.SampleDescription(); sampleDescription.Count = 1; sampleDescription.Quality = 0; D3D10.Texture2DDescription _renderTargetDescription = new D3D10.Texture2DDescription(); _renderTargetDescription.Width = _outputMaxWidth; _renderTargetDescription.Height = _outputMaxHeight; _renderTargetDescription.MipLevels = 1; _renderTargetDescription.SampleDescription = sampleDescription; _renderTargetDescription.Format = SlimDX.DXGI.Format.R8G8B8A8_UNorm; _renderTargetDescription.CpuAccessFlags = SlimDX.Direct3D10.CpuAccessFlags.None; _renderTargetDescription.ArraySize = 1; _renderTargetDescription.BindFlags = SlimDX.Direct3D10.BindFlags.RenderTarget; _renderTargetDescription.Usage = SlimDX.Direct3D10.ResourceUsage.Default; _renderTexture = new D3D10.Texture2D(_device, _renderTargetDescription); _renderTarget = new SlimDX.Direct3D10.RenderTargetView(_device, _renderTexture); _viewArea = new Viewport(); _viewArea.X = 0; _viewArea.Y = 0; _viewArea.Width = _outputMaxWidth; _viewArea.Height = _outputMaxHeight; _viewArea.MinZ = 0.0f; _viewArea.MaxZ = 1.0f; _device.Rasterizer.SetViewports(_viewArea); _device.OutputMerger.SetTargets(_renderTarget); This is how I try to access the data. It crashes on the Map command, and nope, I'm not able to get a Debug Spew yet (I assume I would get the debug output in DebugViewer.exe). byte[] result = null; DataRectangle data = _renderTexture.Map(0, SlimDX.Direct3D10.MapMode.Read, SlimDX.Direct3D10.MapFlags.None); result = Scale(data.Data.DataPointer, data.Pitch, _outputWidth, _outputHeight); // the method below is used to convert the 32bit image to an 8bit image but doesn't get called as the line before that crashes private byte[] Scale(IntPtr source, int pitch, int width, int height) { // Declare an array to hold the bytes of the bitmap. int bytes = width * height; int[] rgbValues = new int[bytes]; byte[] newValues = new byte[bytes]; // Copy the RGB values into the array. for (int y = 0; y < height; y++) { Marshal.Copy(new IntPtr(source.ToInt32() + y * pitch), rgbValues, y * width, width); } // Convert from 32bit to 8bit. for (int counter = 0; counter < rgbValues.Length; counter++) { newValues[counter] = (byte)(rgbValues[counter] & 0xFF); } return newValues; } The error message is this: ex1 = {"E_INVALIDARG: An invalid parameter was passed to the returning function (-2147024809)"} and this is the relevant part of the stack trace: at SlimDX.Result.Throw[T]() in c:\program files\slimdx\source\source\result.cpp:line 83 at SlimDX.Result.Record[T](Int32 hr, Boolean failed) in c:\program files\slimdx\source\source\result.cpp:line 105 at SlimDX.Result.Record[T](Int32 hr) in c:\program files\slimdx\source\source\result.cpp:line 119 at SlimDX.Direct3D10.Texture2D.Map(Int32 subResource, MapMode mode, MapFlags flags) in c:\program files\slimdx\source\source\direct3d10\texture2d.cpp:line 103 I still don't get a debug spew out of DirectX 10, and when I enable debugging of unmanaged code, I get the following message when I start debugging the application: Debugging information for 'AppName.exe' cannot be found or does not match. No native symbols in symbol file. [Edited by - GoodFun on May 6, 2008 4:38:56 PM]

Share this post


Link to post
Share on other sites
I don't know D3D 10, but I did notice this:
Quote:
D3D10_MAP_READ
Resource is mapped for reading. The resource must have been created with read access (see D3D10_CPU_ACCESS_READ).
Your CPU access flags clearly do not include Read.

Share this post


Link to post
Share on other sites
Thanks, I'll give that a try... I probably will have to create another texture as CPU access to the render target might not be valid...

If anyone has some more examples on dynamically created textures in DirectX 10 using SlimDX, I'd be very interested to see them

Thanks
Marcel

Share this post


Link to post
Share on other sites
Quote:
Original post by GoodFun
Thanks, I'll give that a try... I probably will have to create another texture as CPU access to the render target might not be valid...

If anyone has some more examples on dynamically created textures in DirectX 10 using SlimDX, I'd be very interested to see them

Thanks
Marcel


Just to let you know, don't limit yourself to only SlimDX answers. SlimDX is a straight wrapper around DirectX, so any Direct3D 10 samples in C++ can be easily ported over to SlimDX. So while you are unlikely to find many people who have done what you are asking in SlimDX, chances are good you can find it in another language (probably C++).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this