Jump to content

  • Log In with Google      Sign In   
  • Create Account


DirectX 11 - Changing settings after initialization


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 RyxiaN   Members   -  Reputation: 137

Like
0Likes
Like

Posted 22 April 2012 - 12:50 PM

Hello there!

I have a small question regarding changing device settings (such as multisampling, refresh rate, etc) after the device has been initialized.

These things is what I know: Changing fullscreen mode requires a single call:
swapChain.IsFullscreen = !swapChain.IsFullscreen

Changing screen resolution is fairly simple too, it's written at the SlimDX tutorial page. This is what I did:
// Sets the forms size
window.Form.ClientSize = new Size(width, height);

// Dispose the current render target and create a new one
renderTarget.Dispose();
swapChain.ResizeBuffers(1, 0, 0, Format.R8G8B8A8_UNorm, SwapChainFlags.AllowModeSwitch);
using (var resource = Resource.FromSwapChain<Texture2D>(swapChain, 0))
			renderTarget = new RenderTargetView(device, resource);

// Set the new target
context.OutputMerger.SetTargets(renderTarget);

Now to the problem. If I want to change any other settings (settings in the SwapChainDescription structure), such as multisampling and refresh rate, what do I need to do? Do I need to recreate the device and swap chain objects? If that is the case, what happens to resources created or loaded with the device, such as effects, textures, vertex and index buffers, etc.? Do these need to be recreated aswell?

Thanks in advance!

Sponsor:

#2 MJP   Moderators   -  Reputation: 11038

Like
2Likes
Like

Posted 22 April 2012 - 04:55 PM

You never need to recreate the device, you only do that once. For some changes you can call ResizeBuffers or ResizeTargets on the swap chain, however to change the MSAA mode you need to to create a new swap chain. You can do that using a DXGI factory.

In most cases you don't actually want to create your swap chain with MSAA enabled...typically you want to create it without MSAA, and use an MSAA render target instead. Then you can just resolve the MSAA render target to the backbuffer as the final step.

#3 RyxiaN   Members   -  Reputation: 137

Like
0Likes
Like

Posted 25 April 2012 - 11:07 AM

You never need to recreate the device, you only do that once. For some changes you can call ResizeBuffers or ResizeTargets on the swap chain, however to change the MSAA mode you need to to create a new swap chain. You can do that using a DXGI factory.

In most cases you don't actually want to create your swap chain with MSAA enabled...typically you want to create it without MSAA, and use an MSAA render target instead. Then you can just resolve the MSAA render target to the backbuffer as the final step.


Ah, thanks! Noticed that ResizeTargets could also change the refresh rate, compared to ResizeBuffers, so that's good. But how exactly do I create a render target with MSAA enabled? I currently create my render target view like this (like the SlimDX samples do it):
// Create a render target
using (var resource = Resource.FromSwapChain<Texture2D>(swapChain, 0))
	renderTarget = new RenderTargetView(device, resource);

But with this way I can't really change any MSAA settings. I'm assuming you're talking about the SampleDescription property of the Texture2DDescription structure? So I guess I need to create my own Texture2D to use as render target instead of using the FromSwapChain<Texture2D> method?

EDIT

I tried creating a render target with MSAA and this is what I came up with:
            // Create a render target
            Texture2DDescription renderTargetTextureDescription = new Texture2DDescription();
            renderTargetTextureDescription.ArraySize = 1;
            renderTargetTextureDescription.BindFlags = BindFlags.RenderTarget;
            renderTargetTextureDescription.CpuAccessFlags = CpuAccessFlags.None;
            renderTargetTextureDescription.Format = Format.R32G32B32A32_Float;
            renderTargetTextureDescription.Height = Options.ResolutionHeight;
            renderTargetTextureDescription.MipLevels = 1;
            renderTargetTextureDescription.OptionFlags = ResourceOptionFlags.None;
            renderTargetTextureDescription.SampleDescription = new SampleDescription(Options.MultiSampleCount, Options.MultiSampleQuality);
            renderTargetTextureDescription.Usage = ResourceUsage.Default;
            renderTargetTextureDescription.Width = Options.ResolutionWidth;

            RenderTargetViewDescription renderTargetViewDescription = new RenderTargetViewDescription();
            renderTargetViewDescription.Dimension = RenderTargetViewDimension.Texture2D;
            renderTargetViewDescription.MipSlice = 0;
            renderTargetViewDescription.Format = renderTargetTextureDescription.Format;

            //using (var resource = Resource.FromSwapChain<Texture2D>(swapChain, 0))
            using (var resource = new Texture2D(device, renderTargetTextureDescription))
                renderTarget = new RenderTargetView(device, resource, renderTargetViewDescription);

But now my screen only turns black. Anyone that can see what's wrong with my code?

Thanks in advance!

#4 MJP   Moderators   -  Reputation: 11038

Like
2Likes
Like

Posted 25 April 2012 - 03:35 PM

Your creation of the render target looks good. So what you want to do is bind that render target view, render to it, and then use DeviceContext.ResolveSubresource using the MSAA render target texture as the source and the backbuffer texture as the destination. This will resolve the MSAA render target (filter the individual MSAA subsamples), and copy the results to the backbuffer.

#5 RyxiaN   Members   -  Reputation: 137

Like
0Likes
Like

Posted 26 April 2012 - 02:41 AM

Your creation of the render target looks good. So what you want to do is bind that render target view, render to it, and then use DeviceContext.ResolveSubresource using the MSAA render target texture as the source and the backbuffer texture as the destination. This will resolve the MSAA render target (filter the individual MSAA subsamples), and copy the results to the backbuffer.


Is this the way to bind the render target view?
// Set the render target and viewport
			var viewport = new Viewport(0.0f, 0.0f, currentOptions.ResolutionWidth, currentOptions.ResolutionHeight);
			context.OutputMerger.SetTargets(renderTarget); // This binds?
			context.Rasterizer.SetViewports(viewport);

I'm guessing that the render target texture is the one you get from renderTarget.Resource? But which one is the backbuffer texture? How do I get that one? I thought that the render target you bound had the backbuffer, but that's probably wrong then. Posted Image

#6 MJP   Moderators   -  Reputation: 11038

Like
2Likes
Like

Posted 26 April 2012 - 11:50 AM

Is this the way to bind the render target view?

// Set the render target and viewport
			var viewport = new Viewport(0.0f, 0.0f, currentOptions.ResolutionWidth, currentOptions.ResolutionHeight);
			context.OutputMerger.SetTargets(renderTarget); // This binds?
			context.Rasterizer.SetViewports(viewport);


Yes, that's correct.

I'm guessing that the render target texture is the one you get from renderTarget.Resource?


Yes, the Resource property gives you access to the underlying resource for a render target view. In this case the actual type will be Texture2D. It will also be the same instance that you create in your using block.

But which one is the backbuffer texture? How do I get that one? I thought that the render target you bound had the backbuffer, but that's probably wrong then. Posted Image


The back buffer is the texture that you get from the swap chain. Basically it's the texture that the swap chain will use to display contents to your window (or to the screen in full screen mode).

#7 RyxiaN   Members   -  Reputation: 137

Like
0Likes
Like

Posted 26 April 2012 - 01:22 PM

Allright, thanks, clears up alot! I think what I was looking for was right in front if my eyes... Resource.FromSwapChain<Texture2D>(0) is what fetches the backbuffer resource, am I right?

Anyways, got it working now! Can't really test the multisampling though or even change the settings after initialization, but I'll try that when I get the time and post then!

I was stuck a long time on still having a black screen, but then I noticed that my render target texture and the swap chain used different formats...

Here's the final code.

GraphicsDevice.cs:
// Create a render target
			Texture2DDescription renderTargetTextureDescription = new Texture2DDescription();
			renderTargetTextureDescription.ArraySize = 1;
			renderTargetTextureDescription.BindFlags = BindFlags.RenderTarget;
			renderTargetTextureDescription.CpuAccessFlags = CpuAccessFlags.None;
			renderTargetTextureDescription.Format = Format.R8G8B8A8_UNorm;
			renderTargetTextureDescription.Height = Options.ResolutionHeight;
			renderTargetTextureDescription.MipLevels = 1;
			renderTargetTextureDescription.OptionFlags = ResourceOptionFlags.None;
			renderTargetTextureDescription.SampleDescription = new SampleDescription(4, 16);
			renderTargetTextureDescription.Usage = ResourceUsage.Default;
			renderTargetTextureDescription.Width = Options.ResolutionWidth;

			RenderTargetViewDescription renderTargetViewDescription = new RenderTargetViewDescription();
			renderTargetViewDescription.Dimension = RenderTargetViewDimension.Texture2DMultisampled;
			renderTargetViewDescription.MipSlice = 0;
			renderTargetViewDescription.Format = renderTargetTextureDescription.Format;

			renderTargetResource = new Texture2D(device, renderTargetTextureDescription);
			renderTarget = new RenderTargetView(device, renderTargetResource, renderTargetViewDescription);

			// Set the render target and viewport
			var viewport = new Viewport(0.0f, 0.0f, currentOptions.ResolutionWidth, currentOptions.ResolutionHeight);
			context.OutputMerger.SetTargets(renderTarget);
			context.Rasterizer.SetViewports(viewport);

			// Fetch the backbuffer
			backbuffer = Resource.FromSwapChain<Texture2D>(swapChain, 0);

void Draw(Clock clock):
graphicsDevice.Context.ClearRenderTargetView(graphicsDevice.RenderTarget, ClearColor);

			foreach (var scene in VisibleScenes) {
				scene.Draw(graphicsDevice, clock);
			}

			var source = graphicsDevice.RenderTarget.Resource;
			var destination = graphicsDevice.Backbuffer;

			graphicsDevice.Context.ResolveSubresource(source, 0, destination, 0, Format.R8G8B8A8_UNorm);

			graphicsDevice.SwapChain.Present(0, PresentFlags.None);

graphicsDevice.RenderTarget returns the created RenderTargetView
graphicsDevice.Backbuffer returns the resource fetched from the swap chain

Again, thanks a bunch MJP! Posted Image

#8 MJP   Moderators   -  Reputation: 11038

Like
1Likes
Like

Posted 26 April 2012 - 02:36 PM

Allright, thanks, clears up alot! I think what I was looking for was right in front if my eyes... Resource.FromSwapChain<Texture2D>(0) is what fetches the backbuffer resource, am I right?


Yes, that's exactly right.

#9 RyxiaN   Members   -  Reputation: 137

Like
0Likes
Like

Posted 02 May 2012 - 01:36 PM

So I took this one step further. I want to be able to change adapter. But since this is sent along as a parameter to the Device, I assume you need to re-create this, unless there is some other way?
And what would happen to resources created with the device? I'm guessing those would have to be re-created too? That leaves me with this thought. Like seen in many games, some options require a game restart when changed. Is this the best solution when changing the adapter? Just let the user know that the change won't take effect until he/she restarts the game?

Thanks in advance!

#10 MJP   Moderators   -  Reputation: 11038

Like
2Likes
Like

Posted 02 May 2012 - 02:41 PM

A device is permanently bound to an adapter, so you can't change it without recreating the device. This means you will have to also re-create resources, since resources are bound to the device they're created with.

Ideally I think a game should always be able to apply settings in-game without requiring a reboot, but it obviously takes some more engineering effort to structure the engine in such a way that it can totally reboot the renderer without having to reboot every other system. Changing the adapter would be extra tricky, since you'd also have to switch to a monitor connected to that adapter if you're in fullscreen mode. So it's up to you as to whether or not you want to support that scenario, or make the adapter fixed for the lifetime of your game. Another option is that you could take the adapter as a command-line parameter, which you could possibly justify as it being a more "advanced" feature.

To be honest I don't think I've seen any games that let you choose an adapter from in-game menus...I think most just go with the default.

Edited by MJP, 02 May 2012 - 02:43 PM.


#11 RyxiaN   Members   -  Reputation: 137

Like
0Likes
Like

Posted 03 May 2012 - 12:55 AM

Hmm allright. Think I'll just go with the default adapter for now.

Thanks for your input. :)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS