Advertisement

Texture Rendering

Started by January 16, 2016 01:34 PM
6 comments, last by myvraccount 8 years, 10 months ago

In SharpDX/SlimDX, I need to be able to render my image onto a Texture2D object rather than to the screen, then convert the texture into a Bitmap object.

If someone could PLEASE tell me how to do this, that would be great (with source code is even better), because I feel like I've tried everything! Here's what I've done so far, and what's going wrong:

I've set it up to render as normal, and then I've tried calling each of the following:

- RenderTargetView.ResourceAs<Texture2D>

- Texture2D.FromSwapChain<Texture2D>

Each of these successfully returns a Texture2D object, so I'm not sure if it even matters which one I use as long as I can get one to work.

However, then I call the constructor for ShaderResourceView, with the Texture2D as a parameter, and a device (which I verified works correctly), and I get an error that says there's an incorrect parameter (DIERR_INVALIDPARAM) but it doesn't say what parameter is incorrect or in what way.

I compared the description of the Texture2D returned from either of the above functions to one that I manually create when making a Texture2D from a Bitmap (which works correctly). Both functions create an identical TextureDescription, but the BindFlags are different from the one I manually create. The one I manually make I set to ShaderResource, but the one that's returned from either of the above functions sets it to RenderTarget.

Is this likely to be causing the problem, and if so, how can it be fixed? If not, what is likely the problem?

I have tried changing the BindFlags after the texture is returned to me, but the variable is read only!

What should I do? Thank you.

Please, does anyone know how to do this? Even if you could just tell me how to make the RenderTargetView to be a Texture2D or something like that, instead of the screen/backbuffer, and so that it outputs a Texture2D that I could then just draw like any other Texture2D (I guess it probably would need the BindFlags set to ShaderResource instead of RenderTarget, but I may be wrong).

Advertisement

It's not either, it's both. These are flags, so certain combinations are valid and RenderTarget combined with ShaderResource is a quite common one.

For readback you need an additional resource with usage staging (and cpu read), only these can be actually mapped and read back. After your rendering use context.CopyResource from your render target texture to the staging texture. Then you can map.

But the problem with the flags is it won't let me set them, only read them.

For the next thing you said, I'll try it, but what do you mean by a "staging" texture? When you say "usage staging" do you mean I set usage flags to = staging or something like that? I'm not sure I tried CopyResource but I'll look into it.

Also, is there a more efficient way to just skip the target texture or backbuffer or swap chain or whatever it's using to put it on the screen and just tell it right off the bat to ignore the screen and use a Texture2D as the output instead? It seems like this would be quicker to process and a bit less messy.

In any case, I can generally only work on it during weekends, so I'll have to see how it goes then.

Flags and other properties can only be set at creation of resources/textures. Similar applies to views. That's how the API works: You decide what you need and create (usually) everything at app start.

When you say "usage staging" do you mean I set usage flags to = staging or something like that?


Yes, though in this case it's an enum, you can't combine them. This is why you need this copy operation: you can't read back a usage=default resource directly. Again, this is how the API/driver/hardware works, one has to get used to it.

Also, is there a more efficient way to just skip the target texture or backbuffer or swap chain or whatever it's using to put it on the screen and just tell it right off the bat to ignore the screen and use a Texture2D as the output instead? It seems like this would be quicker to process and a bit less messy.


Bacterius answered this already - correctly. You don't need a swap chain, this is only if you want to render to a window. You create a Texture2D and a RendertTargetView thereof and you're ready to go.

I get the impression you're quite confused about all of this. I recommend going through the rastertek tutorial (there are even SharpDX transliterations around IIRC) and/or buy F.Luna's book. Though the latter is C++ I consider it the best book for D3D11 beginners.

That looks like a GREAT set of tutorials (although they're in C++ and I'm using C#, but oh well), so I'll look into them more as soon as I get a chance. Thanks.

Advertisement
You should learn how to find stuff yourself wink.png. I mentioned the transliteration for SharpDX. That was a first google hit:

SharpDX for Rastertek tutorials

Also, the DirectX subforum here has a couple of links that should help you.

Edit: Also, gamedev.net member Eric Richards has transliterated Luna's source code (also available online) to SlimDX here

That seems to have fixed it, thanks!

This topic is closed to new replies.

Advertisement