• 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

Texture becomes pixelated when upsampling to full-screen quad and MSAA enabled

5 posts in this topic


So I am trying to upsample a texture to a full-screen quad. By turning on MSAA, the texture becomes pixelated, while everything seems perfect with no anti-aliasing.

This sample image sums up my problem pretty well I think:

I am using DirectX 11 with pixel/vertex shader 4.0, if it matters. I have tried searching for a solution, but the only relevant things I could find was about the half-pixel offset needed on DirectX 9, which shouldn't be a problem on DirectX 10+.

So my question is, does anyone recognise this problem? Any form of help is appreciated.

Thanks in advance.

Share this post

Link to post
Share on other sites
It's kind of hard to diagnose if we have no idea what you're actually doing. [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img] Got some code?

EDIT: I would wager that filtering in general is incompatible with MSAA surfaces, though. I can't really think of a good way to incorporate the extra subsample data in that operation and I suspect it's just blanket not supported for the same reason. If you aren't [url="http://msdn.microsoft.com/en-us/library/windows/desktop/ff476474(v=vs.85).aspx"]ResolveSubresource[/url]()ing right now, try that first (again, code!)

Share this post

Link to post
Share on other sites
Thanks for the reply.

I'm not really sure about the correct method to apply msaa when dealing with textures etc.
So basically, what I'd like to do is this:

1. Render the main scene to a full-resolution DXGI_FORMAT_R16G16B16A16_FLOAT-texture (with msaa enabled).
2. Render the volumetric light (that you see in my first post) to a small-resolution texture. MSAA is not really needed here.
3. Render the main scene to the backbuffer (maybe using a HDR/bloom effect later on), by rendering a full-screen quad and texture from step 1.
4. Render the small-resolution texture from step 2 to the backbuffer with a full-screen quad

I've tried to extract what I think is the most relevant code here.

Setting up the swap chain:

// Get data on GPU's support of multisampling
for(int i = 1; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++) {
hr = dev->CheckMultisampleQualityLevels(DXGI_FORMAT_R8G8B8A8_UNORM, i, &qual_levels[i]);
if(!FAILED(hr) && qual_levels[i] > 0) {
settings.msaa_count = i;
settings.msaa_quality = qual_levels[i]-1;
settings.msaa_count = 4;
settings.msaa_quality = qual_levels[settings.msaa_count]-1;
// create a struct to hold information about the swap chain
ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));
scd.BufferCount = 1; // one back buffer
scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; // use 32-bit color
scd.BufferDesc.Width = sys.screen_width; // set the back buffer width
scd.BufferDesc.Height = sys.screen_height; // set the back buffer height
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // how swap chain is to be used
scd.OutputWindow = hWnd; // the window to be used
scd.SampleDesc.Count = settings.msaa_count; // how many multisamples
scd.SampleDesc.Quality = settings.msaa_quality;
scd.Windowed = TRUE; // windowed/full-screen mode
scd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH; // allow full-screen switching


Setting up a rasterizer:

// Apply custom rasterizer so we can be sure to get anti-aliasing
ZeroMemory(&rd, sizeof(D3D11_RASTERIZER_DESC));
rd.MultisampleEnable = ( settings.msaa_count > 1 );
rd.AntialiasedLineEnable = ( settings.msaa_count > 1 );
rd.FillMode = D3D11_FILL_SOLID;
rd.CullMode = D3D11_CULL_BACK;
rd.DepthClipEnable = true;

hr = dev->CreateRasterizerState(&rd, &pRS);
if( SUCCEEDED(hr)) devcon->RSSetState(pRS);

Depth stencil texture:

// create the depth stencil texture
ZeroMemory(&texd, sizeof(texd));
texd.Width = sys.screen_width;
texd.Height = sys.screen_height;
texd.ArraySize = 1;
texd.MipLevels = 1;
texd.SampleDesc.Count = settings.msaa_count;
texd.SampleDesc.Quality = settings.msaa_quality;
texd.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
texd.BindFlags = D3D11_BIND_DEPTH_STENCIL;
ID3D11Texture2D *pDepthBuffer;
hr = dev->CreateTexture2D(&texd, NULL, &pDepthBuffer);
if(FAILED(hr)) return false;
// create the depth stencil buffer
ZeroMemory(&dsvd, sizeof(dsvd));

dsvd.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
dsvd.ViewDimension = settings.msaa_count == 1 ? D3D11_DSV_DIMENSION_TEXTURE2D : D3D11_DSV_DIMENSION_TEXTURE2DMS; //D3D11_DSV_DIMENSION_TEXTURE2DMS;
dsvd.Texture2D.MipSlice = 0;
dev->CreateDepthStencilView(pDepthBuffer, &dsvd, &zbuffer);
// set the render target as the back buffer
devcon->OMSetRenderTargets(1, &backbuffer, zbuffer);

Here is the code used to setup all my render textures:
I've tried turning off multi-sampling here, but then my game crashes on startup. So I guess there is something I should be doing differently to turn off msaa on individual textures.

D3D11_TEXTURE2D_DESC textureDesc;
HRESULT result;
D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc;

// Initialize the render target texture description.
ZeroMemory(&textureDesc, sizeof(textureDesc));
// Setup the render target texture description.
textureDesc.Width = textureWidth;
textureDesc.Height = textureHeight;
textureDesc.MipLevels = 1;
textureDesc.ArraySize = 1;
textureDesc.Format = format;
textureDesc.SampleDesc.Count = sys.device->settings.msaa_count;
textureDesc.SampleDesc.Quality = sys.device->settings.msaa_quality;
textureDesc.Usage = D3D11_USAGE_DEFAULT;
textureDesc.CPUAccessFlags = 0;
textureDesc.MiscFlags = 0;
// Create the render target texture.
result = sys.device->dev->CreateTexture2D(&textureDesc, NULL, &pRenderTargetTexture);
return false;
// Setup the description of the render target view.
renderTargetViewDesc.Format = textureDesc.Format;
renderTargetViewDesc.ViewDimension = sys.device->settings.msaa_count == 1 ? D3D11_RTV_DIMENSION_TEXTURE2D : D3D11_RTV_DIMENSION_TEXTURE2DMS; //D3D11_RTV_DIMENSION_TEXTURE2DMS;
renderTargetViewDesc.Texture2D.MipSlice = 0;

// Create the render target view.
result = sys.device->dev->CreateRenderTargetView(pRenderTargetTexture, &renderTargetViewDesc, &pRenderTargetView);
return false;
// Setup the description of the shader resource view.
shaderResourceViewDesc.Format = textureDesc.Format;
shaderResourceViewDesc.ViewDimension = sys.device->settings.msaa_count == 1 ? D3D11_SRV_DIMENSION_TEXTURE2D : D3D11_SRV_DIMENSION_TEXTURE2DMS;// D3D11_SRV_DIMENSION_TEXTURE2DMS;
shaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
shaderResourceViewDesc.Texture2D.MipLevels = 1;

// Create the shader resource view.
result = sys.device->dev->CreateShaderResourceView(pRenderTargetTexture, &shaderResourceViewDesc, &pShaderResourceView);
return false;

Here is an extract of my main graphics-draw loop.
rt_xxxx are my render textures.

// Render main 3d scene to texture
setShader( SHADER_TYPE_MAIN );
shader_main->updateCbLightSource( matView, matProj );
ID3D11ShaderResourceView *srv = rt_light_shadowmap->GetShaderResourceView();
sys.device->devcon->PSSetShaderResources(1, 1, &srv);

// Render volumetric light to small-resolution render texture
sprite_light->Render(0, 0, 0 ); // The model of the light frustum
shader_vol_lighting->render( 18, rt_light_shadowmap->GetShaderResourceView(), rt_depth_map->GetShaderResourceView()); // The shadow-map and depth-map are also just render textures, previously generated

// Render full-screen quads to backbuffer
updateTextureCB( sys.screen_width , sys.screen_height );
full_window->Render(0, 0, rt_scene->GetShaderResourceView() );
shader_texture->render( sizeof(WORD)*6);
// Vol. light
full_window->Render(0, 0, rt_small_2->GetShaderResourceView() );
shader_texture->render( sizeof(WORD)*6);
// Present
swapchain->Present(0, 0);

If I understand it correctly, I should define my rt_scene render texture with msaa and just draw it like I do currently.
On the other hand, my volumetric light render texture (here called rt_small2) should be defined [i]without[/i] msaa. Then, I can resize it and hopefully not get the pixelation.

Is it generally bad to resize textures with msaa?

Share this post

Link to post
Share on other sites
I figured out my problems I think.

My problems originated from the fact that I used MSAA on all my render textures, even though I didn't need it in most of them. Now, I have defined every render texture without MSAA, except for the one I render my main scene to. The second problem I had was with the ZBuffer. For anyone else having problems using a mixed set of render textures with and without MSAA, make sure the ZBuffer you are using matches your current render target in MSAA settings. I have now defined two ZBuffers, one for rendering with MSAA and one for everything else. Also make sure MSAA is disabled on your backbuffer, if you are rendering non-MSAA textures to it.

As InvalidPointer pointed out, I also had to copy my main scene render texture to another texture without MSAA using ResolveSubresource(). Then I can draw the new texture using a full-screen quad to my backbuffer.

I have one question though. Is there a way to render my MSAA-texture directly to my backbuffer? Or do I have to do convert it to a non-MSAA texture and then draw it as a full-screen quad as I do currently?

Edit: Okay I can answer my own question again :) You can draw the MSAA texture directly to the backbuffer just fine. Just make sure you don't use linear filter or something if you are resizing the texture.

Share this post

Link to post
Share on other sites
In no particular order, stuff that sticks out--
1) Your backbuffer should not actually be MSAA-enabled. This is what's going to be displayed directly on the monitor, and it has no direct way to use the extra sample data. You'll either be resolving an 'offscreen' MSAA surface to this directly via ResolveSubresource(), buuut there happens to be another way...

2) With the advent of D3D10+ you can actually do a custom resolve by way of a shader, which is extra useful because you mention a desire to use HDR rendering. (BTW-- use R11G11B10F as your target format, it halves write bandwidth for little to no image quality loss unless you have insane lighting contrast.) I mention this because while a flat average will work just fine for standard LDR images, doing this pre-tonemapping can have disastrous effect on the final image quality for much the same reason why naive normal map filtering creates alias city-- you're doing the blending at the wrong time. For an example of what this looks like, grab any early UE3 game (Gears of War or Unreal Tournament 3) and enable AA, then watch in horror as it does basically nothing aside from reduce performance. The short version is that the tonemap operator can result in radically different brightness values for adjacent pixels and you've already thrown out the extra subsamples in the earlier resolve pass.

The 'proper' way to do things is to grab all your individual MSAA samples using the new MSAA texture feature, (Texture2DMS in HLSL, you'll need to create special shaders for each supported sample count, unfortunately, but #defines make this fairly easy) tonemap them all individually, *then* average with some multiply-adds. Emil Persson, a really clever ex-ATi demo guy, has a sample app that does exactly this, available from [url="http://www.humus.name/3D/CustomResolve.7z"]here[/url]. It includes source [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

The final, ten-thousand-foot view:
1) Render your scene to an MSAA, HDR surface somehow.
2) Bind your swap chain buffer as a render target, and bind the scene texture to a shader resource slot. You can also merge your volumetric light pass result in with the main stuff here if you'd like. Additionally, if you want to do some extra postprocessing, you can just create another non-MSAA surface and render to that instead. The important bit is that it's not multisampled, not that it's the backbuffer. D3D10 is very cool about this, actually, it was much more of a pain in the ass in 9.
3) Draw a fullscreen triangle that reads all the MSAA samples, tonemaps them, then averages. The aforementioned volumetric light merge can be done a few ways. The cheapest, though not necessarily most correct method would be to tonemap it, tonemap the main scene, then blend those two values somehow. The more correct, but slightly more expensive approach would be to add the volumetric light value into each individual main scene MSAA sample, then tonemap/average the results. ALUs are pretty cheap nowdays so if you're a correctness nutter you can probably get away with either one.

EDIT: Aaand you ninja'd me, though I suppose this answers your new question too :)

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