Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 18 Jul 2011
Offline Last Active Feb 07 2012 12:07 PM

Posts I've Made

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

29 January 2012 - 02:34 PM

Thanks alot invalidPointer, that certainly clears up alot of confusion for me.

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

29 January 2012 - 10:58 AM

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.

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

28 January 2012 - 11:37 AM

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
  UINT qual_levels[ D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT ] = {0};
  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 without msaa. Then, I can resize it and hopefully not get the pixelation.

Is it generally bad to resize textures with msaa?

In Topic: 2d car with simple suspension physics

22 July 2011 - 11:31 PM

Thanks for the answer, it's been very helpful.

In Topic: 2d car with simple suspension physics

21 July 2011 - 07:25 PM

Thanks for the help. You are probably right that I'm not obeying Newton's third law.

So if I understand you correct, I should not add a collision force/impulse to keep the car from the ground. Rather, I should just compress the spring. Next, this will result in the two masses connected to the spring each getting a force directed from each other, which should be equal in magnitude.

Hmm, or perhaps not? The spring can store potential energy (like gravity) so it doesn't completely obey newton's third law. At least on some level of abstraction, if I understand it correct.

Some questions:

  • The wheel will get the most acceleration because it is the lightest of the two bodies, so it will probably collide again. I suspect this will result in jittery motion. Maybe I should do the same calculations several times each frame to reduce this?
  • With the springs having only one freedom of movement, the spring length will in some cases have to become infinitely long to get out of the ground. That is when the car is colliding with a surface perpendicular to the spring axis. What is the best way to deal with this? I am thinking in the lines of having an angular spring as well.
  • When compressing the spring, this will add energy to it. And since this energy is not based on the mechanical energy of the car, isn't there a danger for unstable and somewhat random motions? Proposed solution: Take the desired absorbed energy of the car, and use the spring constant to calculate the distance based on this energy. Artificially move rest of the car out of the collision area.
    Bonus: This will also avoid the problem with the infinite spring length. What do you think about this solution?
Thanks for that demo h4tt3n, I had fun with it :) I will take a deeper look at that code.