Quantcast
Channel: GameDev.net
Viewing all articles
Browse latest Browse all 17560

Changing MSAA settings on the fly

$
0
0
Hi all,
Just a short question on MSAA in D3D11.

I've got it all working fine, when I setup MSAA directly through initialization.
Now I want to create a function to change, either enable/disable or change the number of samples, realtime.

From what I could find out so far, I just need to recreate the swapchain (through the DXGI factory), and be done.
But when I look in my code, I see that my depthstencil texture also has some MSAA related properties. Does that mean I'd also have to recreate that? (tex + resource view)
 
Here's my code, which is executed initially (SwapChain creation):
 
	// Fill out the DXGI_SWAP_CHAIN_DESC
	DXGI_SWAP_CHAIN_DESC sd;
	sd.BufferDesc.Width  = mSettings.GetScreenWidth();
	sd.BufferDesc.Height = mSettings.GetScreenHeight();
	sd.BufferDesc.RefreshRate.Numerator = 60;
	sd.BufferDesc.RefreshRate.Denominator = 1;
	sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
	sd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
	sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;

	// Use MSAA? 
	if(mSettings.GetMSAAEnabled())
	{
		sd.SampleDesc.Count = mSettings.GetMSAANrSamples();
		if(mMSAAQuality == 0) sd.SampleDesc.Quality = D3D11_STANDARD_MULTISAMPLE_PATTERN;
		else sd.SampleDesc.Quality = mMSAAQuality-1;
		// NOTE: RASTERIZER STATE DETERMINES QUADRILATERAL VS ALPHA-LINE AA
		// https://msdn.microsoft.com/en-us/library/windows/desktop/ff476198(v=vs.85).aspx
	}
	// No MSAA
	else
	{
		sd.SampleDesc.Count   = 1;
		sd.SampleDesc.Quality = 0;
	}

	sd.BufferUsage  = DXGI_USAGE_RENDER_TARGET_OUTPUT;
	sd.BufferCount  = 1;
	sd.OutputWindow = pHwnd;
	
	if(mSettings.GetWindowed()) sd.Windowed = true;
		else sd.Windowed = false;

	sd.SwapEffect   = DXGI_SWAP_EFFECT_DISCARD;
	sd.Flags        = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;

	// create swap chain using same IDXGIFactory as used to create device
	IDXGIDevice *dxgiDevice = 0;
	if(FAILED(mDevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice))) return false;

	IDXGIAdapter *dxgiAdapter = 0;
	if(FAILED(dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&dxgiAdapter))) return false;

	IDXGIFactory *dxgiFactory = 0;
	if(FAILED(dxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&dxgiFactory))) return false;

	if(FAILED(dxgiFactory->CreateSwapChain(mDevice, &sd, &mSwapChain))) return false;

And when resizing the window or changing the resolution (DepthStencil tex and view):

	// Create the depth/stencil buffer and view.
	D3D11_TEXTURE2D_DESC depthStencilDesc;
	
	depthStencilDesc.Width     = mSettings.GetScreenWidth();
	depthStencilDesc.Height    = mSettings.GetScreenHeight();
	depthStencilDesc.MipLevels = 1;
	depthStencilDesc.ArraySize = 1;
	depthStencilDesc.Format    = DXGI_FORMAT_D24_UNORM_S8_UINT;
	       
	// Use MSAA? --must match swap chain MSAA values.
	if(mSettings.GetMSAAEnabled())
	{
		depthStencilDesc.SampleDesc.Count = mSettings.GetMSAANrSamples();

		if(mMSAAQuality == 0) depthStencilDesc.SampleDesc.Quality = D3D11_STANDARD_MULTISAMPLE_PATTERN;
		else depthStencilDesc.SampleDesc.Quality = mMSAAQuality-1;
	}
	// No MSAA
	else
	{
		depthStencilDesc.SampleDesc.Count   = 1;
		depthStencilDesc.SampleDesc.Quality = 0;
	}

	depthStencilDesc.Usage          = D3D11_USAGE_DEFAULT;
	depthStencilDesc.BindFlags      = D3D11_BIND_DEPTH_STENCIL;
	depthStencilDesc.CPUAccessFlags = 0; 
	depthStencilDesc.MiscFlags      = 0;

	hr = mDevice->CreateTexture2D(&depthStencilDesc, 0, &mDepthStencilBuffer);
	if(FAILED(hr)) return false;

	hr = mDevice->CreateDepthStencilView(mDepthStencilBuffer, 0, &mDepthStencilView);
	if(FAILED(hr)) return false;

	// Bind the render target view and depth/stencil view to the pipeline.
	mImmediateContext->OMSetRenderTargets(1, &mRenderTargetView, mDepthStencilView);

Any input on what to do here is appreciated.

My assumption would be that both are needed, but just want to be sure (because in other topics I've just read about the SwapChain).
​If both are needed, then it's quite simple; just add a call to OnResize after I've created the new SwapChain.


Viewing all articles
Browse latest Browse all 17560

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>