• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
RyxiaN

DirectX 11 - Changing settings after initialization

10 posts in this topic

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:
[code]swapChain.IsFullscreen = !swapChain.IsFullscreen[/code]

Changing screen resolution is fairly simple too, it's written at the [url="http://slimdx.org/tutorials/SimpleTriangle.php"]SlimDX tutorial page[/url]. This is what I did:
[code]// 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);[/code]

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!
0

Share this post


Link to post
Share on other sites
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.
2

Share this post


Link to post
Share on other sites
[quote name='MJP' timestamp='1335135336' post='4933900']
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.
[/quote]

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):
[code]// Create a render target
using (var resource = Resource.FromSwapChain<Texture2D>(swapChain, 0))
renderTarget = new RenderTargetView(device, resource);[/code]

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?

[b]EDIT[/b]

I tried creating a render target with MSAA and this is what I came up with:
[code]
// 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);[/code]

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

Thanks in advance!
0

Share this post


Link to post
Share on other sites
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.
2

Share this post


Link to post
Share on other sites
[quote name='MJP' timestamp='1335389724' post='4934887']
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.
[/quote]

Is this the way to bind the render target view?
[code] // 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);[/code]

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. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
0

Share this post


Link to post
Share on other sites
[quote name='RyxiaN' timestamp='1335429662' post='4935003']
Is this the way to bind the render target view?
[code] // 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);[/code]
[/quote]

Yes, that's correct.

[quote name='RyxiaN' timestamp='1335429662' post='4935003']
I'm guessing that the render target texture is the one you get from renderTarget.Resource?
[/quote]

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.

[quote name='RyxiaN' timestamp='1335429662' post='4935003']
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. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
[/quote]

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).
2

Share this post


Link to post
Share on other sites
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.

[b]GraphicsDevice.cs:[/b]
[code] // 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);[/code]

[b]void Draw(Clock clock):[/b]
[code] 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);[/code]

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

Again, thanks a bunch MJP! [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
0

Share this post


Link to post
Share on other sites
[quote name='RyxiaN' timestamp='1335468138' post='4935176']
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?
[/quote]

Yes, that's exactly right.
1

Share this post


Link to post
Share on other sites
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!
0

Share this post


Link to post
Share on other sites
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
2

Share this post


Link to post
Share on other sites
Hmm allright. Think I'll just go with the default adapter for now.

Thanks for your input. :)
0

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  
Followers 0