Romeriaux

Member
  • Content count

    3
  • Joined

  • Last visited

Community Reputation

110 Neutral

About Romeriaux

  • Rank
    Newbie
  1.   That is the real problem. Can you explain?     This is code is an interop for a WPF/WinStore/WinPhone API. This is why for Windesktop I render to a bitmap (doesn't support directx11 directly). So the resize could happen in tons of different ways (touch, mouse keyboard, stylus...) and will require tons of code just to detect that resizing is done (not only mouse, touch, stylus up but also lost focus, unresponsive...). And this code will interfere with the rest of the manipulation code already in place.           I think I'll try that. This is basically the workaround I have, not resizing immediately but only do a few per seconds. But instead of not resizing at all (which is visually not really good) I will resize the Viewport but not the backbuffer. I'll give it a go.     But is it a well-known issue that backbuffers resources can't be forced to be released immediately on some integrated GPU? Or am I doing something wrong?   Thanks for your help.
  2. I've been looking around the internet and on this forum (especially this post: http://www.gamedev.net/topic/658824-resizing-buffers-out-of-memory/) but I can't find an answer to my problem.   I use D3D11 to render to a texture either a 2d (without back buffer) or 3d scene. With the 2d one everything works great. The 3d one works great except if I start resizing the window. Then the memory start growing really quickly on some devices (especially the integrated intel GPU) until it runs out of memory. Because of my setup it is impossible for me to know when the user is done resizing the window, so my only workaround right now is to only allow a few resize per seconds. But that will not prevent a crash (just delay it), so I would like to find a real fix.   It all seems to be related to Direct3D 11 defering the destruction of objects. But even when calling flush I can't get the memory to not grow on resize. Any help is appreciated.   This is part of my code, hopefully it covers everything that is needed to understand this issue.     First that function that resize (the cause of my problem I think). My best guess is that I don't free all the resources of the back buffer so resizing is creating tons of them: void Dx_state_desktop::set_surface_size_() { //first release memory if (m_is3d) { ID3D11RenderTargetView* nullViews[] = { nullptr }; m_device11Context->OMSetRenderTargets( ARRAYSIZE(nullViews), nullViews, nullptr); m_render_target_view11.Reset(); m_depthStencilBuffer.Reset(); m_depthStencilView.Reset(); m_device11Context->ClearState(); m_device11Context->Flush(); } m_texture11.Reset(); //then resize all the ressources bool isWarp = m_api == DotNet_graphics_API::directX11_warp; D3D11_TEXTURE2D_DESC Desc = { 0 }; Desc.Width = m_current_values.m_width; Desc.Height = m_current_values.m_height; Desc.MipLevels = 1; Desc.ArraySize = 1; Desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; Desc.SampleDesc.Count = 1; Desc.SampleDesc.Quality = 0; Desc.Usage = D3D11_USAGE_DEFAULT; Desc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE; Desc.CPUAccessFlags = 0; if (!isWarp) Desc.MiscFlags = D3D11_RESOURCE_MISC_SHARED; // Required to share texture with Dx9 HRESULT hr = m_device11->CreateTexture2D(&Desc, nullptr, &m_texture11); Dx_exception::throw_if_failed(hr, "CreateTexture2D"); if (m_is3d) { // Initialize the description of the depth buffer. D3D11_TEXTURE2D_DESC depthBufferDesc = { 0 }; // Set up the description of the depth buffer. depthBufferDesc.Width = m_current_values.m_width; depthBufferDesc.Height = m_current_values.m_height; depthBufferDesc.MipLevels = 1; depthBufferDesc.ArraySize = 1; depthBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; depthBufferDesc.SampleDesc.Count = 1; depthBufferDesc.SampleDesc.Quality = 0; depthBufferDesc.Usage = D3D11_USAGE_DEFAULT; depthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL; depthBufferDesc.CPUAccessFlags = 0; // Create the texture for the depth buffer using the filled out description. hr = m_device11->CreateTexture2D(&depthBufferDesc, NULL, &m_depthStencilBuffer); Dx_exception::throw_if_failed(hr, "CreateTexture2D"); hr = m_device11->CreateDepthStencilView(m_depthStencilBuffer.Get(), 0, &m_depthStencilView); Dx_exception::throw_if_failed(hr, "CreateDepthStencilView"); // Set up the viewport. D3D11_VIEWPORT viewport; viewport.Width = static_cast<FLOAT>(m_current_values.m_width); viewport.Height = static_cast<FLOAT>(m_current_values.m_height); viewport.MinDepth = 0.0f; viewport.MaxDepth = 1.0f; viewport.TopLeftX = 0; viewport.TopLeftY = 0; m_device11Context->RSSetViewports(1, &viewport); } if (!isWarp) { // create Dx9 shared surface ComPtr<IDXGIResource> dxgi_resource; hr = m_texture11.As(&dxgi_resource); Dx_exception::throw_if_failed(hr, "IDXGIResource"); HANDLE handle; hr = dxgi_resource->GetSharedHandle(&handle); Dx_exception::throw_if_failed(hr, "GetSharedHandle"); ComPtr<IDirect3DTexture9> texture9; hr = m_device9->CreateTexture(m_current_values.m_width, m_current_values.m_height, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, // equivalent to DXGI_FORMAT_B8G8R8A8_UNORM, D3DPOOL_DEFAULT, &texture9, &handle); Dx_exception::throw_if_failed(hr, "CreateTexture"); ComPtr<IDirect3DSurface9> surface9; hr = texture9->GetSurfaceLevel(0, &surface9); Dx_exception::throw_if_failed(hr, "GetSurfaceLevel"); m_surface9 = surface9; } } Then some code to put it in perspective, first the initialization:  void Dx_state_desktop::init_dx11_(bool use_hardware) { D3D_DRIVER_TYPE driver_type = use_hardware ? D3D_DRIVER_TYPE_HARDWARE : D3D_DRIVER_TYPE_WARP; UINT creation_flags = 0; #if defined(_DEBUG) creation_flags |= D3D11_CREATE_DEVICE_DEBUG; #endif HRESULT hr = D3D11CreateDevice( nullptr, // adapter, use default driver_type, nullptr, // software renderer module creation_flags, nullptr, // default to all feature levels 0, D3D11_SDK_VERSION, &m_device11, &m_feature_level, &m_device11Context); Dx_exception::throw_if_failed(hr, "D3D11CreateDevice"); if (hr == S_FALSE) Trace_timing::write_message( "Dx11 create device returned S_FALSE"); if (m_is3d) { // Set up the description of the depth buffer. D3D11_TEXTURE2D_DESC depthBufferDesc = { 0 }; depthBufferDesc.Width = m_current_values.m_width; depthBufferDesc.Height = m_current_values.m_height; depthBufferDesc.MipLevels = 1; depthBufferDesc.ArraySize = 1; depthBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; depthBufferDesc.SampleDesc.Count = 1; depthBufferDesc.SampleDesc.Quality = 0; depthBufferDesc.Usage = D3D11_USAGE_DEFAULT; depthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL; depthBufferDesc.CPUAccessFlags = 0; // Create the texture for the depth buffer using the filled out description. hr = m_device11->CreateTexture2D(&depthBufferDesc, NULL, &m_depthStencilBuffer); Dx_exception::throw_if_failed(hr, "CreateTexture2D"); // Initialize the description of the stencil state. D3D11_DEPTH_STENCIL_DESC depthStencilDesc = { 0 }; // Set up the description of the stencil state. depthStencilDesc.DepthEnable = TRUE; depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS; depthStencilDesc.StencilEnable = TRUE; depthStencilDesc.StencilReadMask = 0xFF; depthStencilDesc.StencilWriteMask = 0xFF; // Stencil operations if pixel is front-facing. depthStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; depthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR; depthStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; depthStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS; // Stencil operations if pixel is back-facing. depthStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; depthStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR; depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS; // Create the depth stencil state. hr = m_device11->CreateDepthStencilState(&depthStencilDesc, &m_depthStencilState); Dx_exception::throw_if_failed(hr, "CreateDepthStencilState"); // Set the depth stencil state. m_device11Context->OMSetDepthStencilState(m_depthStencilState.Get(), 1); D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc; // Initialize the depth stencil view. ZeroMemory(&depthStencilViewDesc, sizeof(depthStencilViewDesc)); // Set up the depth stencil view description. depthStencilViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D; depthStencilViewDesc.Texture2D.MipSlice = 0; // Create the depth stencil view. hr = m_device11->CreateDepthStencilView(m_depthStencilBuffer.Get(), &depthStencilViewDesc, &m_depthStencilView); Dx_exception::throw_if_failed(hr, "CreateDepthStencilView"); D3D11_RASTERIZER_DESC rasterDesc; // Setup the raster description which will determine how and what polygons will be drawn. rasterDesc.AntialiasedLineEnable = FALSE; rasterDesc.CullMode = D3D11_CULL_BACK; rasterDesc.DepthBias = 0; rasterDesc.DepthBiasClamp = 0.0f; rasterDesc.DepthClipEnable = TRUE; rasterDesc.FillMode = D3D11_FILL_SOLID; rasterDesc.FrontCounterClockwise = FALSE; rasterDesc.MultisampleEnable = FALSE; rasterDesc.ScissorEnable = FALSE; rasterDesc.SlopeScaledDepthBias = 0.0f; // Create the rasterizer state from the description we just filled out. hr = m_device11->CreateRasterizerState(&rasterDesc, &m_rasterState); Dx_exception::throw_if_failed(hr, "CreateRasterizerState"); // Now set the rasterizer state. m_device11Context->RSSetState(m_rasterState.Get()); // Set up the viewport. D3D11_VIEWPORT viewport; viewport.Width = static_cast<FLOAT>(m_current_values.m_width); viewport.Height = static_cast<FLOAT>(m_current_values.m_height); viewport.MinDepth = 0.0f; viewport.MaxDepth = 1.0f; viewport.TopLeftX = 0; viewport.TopLeftY = 0; m_device11Context->RSSetViewports(1, &viewport); } } This is the logic for begin then DRAW (not here) and then end for each frame: void Dx_state_desktop::begin() { set_surface_size_(); // render to a directX11 surface HRESULT hr = m_device11->CreateRenderTargetView(m_texture11.Get(), nullptr, &m_render_target_view11); Dx_exception::throw_if_failed(hr, "CreateRenderTargetView"); ComPtr<ID3D11DeviceContext> device_context11; m_device11->GetImmediateContext(&device_context11); // create an "array" of one COM pointer, so take the address of the raw pointer (don't take address of the smart pointer it releases it) auto render_target_view_raw_pointer = m_render_target_view11.Get(); if (!m_is3d) device_context11->OMSetRenderTargets(1, render_target_view_raw_pointer, nullptr); else device_context11->OMSetRenderTargets(1, render_target_view_raw_pointer, m_depthStencilView.Get()); } void Dx_state_desktop::end() { ComPtr<ID3D11DeviceContext> immediate_context11; m_device11->GetImmediateContext(&immediate_context11); immediate_context11->OMSetRenderTargets(0, 0, 0); immediate_context11->Flush(); m_render_target_view11.Reset(); }
  3. Hi all.   I'm trying to display a scene on windows phone 8 using D3D11. I got it to work without a Depth Buffer (using mostly Windows phone SDK sample). But now I actually need a depth buffer to render the scene correctly. I got it to work with a depth buffer on the Windows Phone Emulator (win32 build), but when I test it on any windows phone (ARM build), I get nothing (black screen).   So I must hit something that the graphic card on my computer understands (that is the only part of the windows phone emulator that is not fully emulated I think) but that the graphic card on the actual phone doesn't get.   Please if you have any idea let me know. Here is my code: UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT; D3D_FEATURE_LEVEL featureLevels[] = { D3D_FEATURE_LEVEL_9_3 }; // Create the Direct3D 11 API device object and a corresponding context. ComPtr<ID3D11Device> device; ComPtr<ID3D11DeviceContext> context; ThrowIfFailed( D3D11CreateDevice( nullptr, // Specify nullptr to use the default adapter. D3D_DRIVER_TYPE_HARDWARE, nullptr, creationFlags, // Set set debug and Direct2D compatibility flags. featureLevels, // List of feature levels this app can support. ARRAYSIZE(featureLevels), D3D11_SDK_VERSION, // Always set this to D3D11_SDK_VERSION. &device, // Returns the Direct3D device created. &m_featureLevel, // Returns feature level of device created. &context // Returns the device immediate context. ) ); // Get the Direct3D 11.1 API device and context interfaces. ThrowIfFailed( device.As(&m_d3dDevice) ); ThrowIfFailed( context.As(&m_d3dContext) );   CD3D11_TEXTURE2D_DESC renderTargetDesc( DXGI_FORMAT_B8G8R8A8_UNORM, static_cast<UINT>(m_renderTargetSize.Width), static_cast<UINT>(m_renderTargetSize.Height), 1, 1, D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE ); renderTargetDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX | D3D11_RESOURCE_MISC_SHARED_NTHANDLE; // Allocate a 2-D surface as the render target buffer. ThrowIfFailed( m_d3dDevice->CreateTexture2D( &renderTargetDesc, nullptr, &m_renderTarget ) ); ThrowIfFailed( m_d3dDevice->CreateRenderTargetView( m_renderTarget.Get(), nullptr, &m_renderTargetView ) ); // Create a depth stencil view. CD3D11_TEXTURE2D_DESC depthBufferDesc( DXGI_FORMAT_D24_UNORM_S8_UINT, static_cast<UINT>(m_renderTargetSize.Width), static_cast<UINT>(m_renderTargetSize.Height), 1, 1, D3D11_BIND_DEPTH_STENCIL); //tried with D3D11_BIND_RENDER_TARGET with a format DXGI_FORMAT_R24G8_TYPELESS in didn't change anything ThrowIfFailed( m_d3dDevice->CreateTexture2D( &depthBufferDesc, nullptr, &m_depthStencilBuffer ) ); D3D11_DEPTH_STENCIL_DESC depthStencilDesc; // Set up the description of the stencil state. depthStencilDesc.DepthEnable = true; depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS; depthStencilDesc.StencilEnable = true; depthStencilDesc.StencilReadMask = 0xFF; depthStencilDesc.StencilWriteMask = 0xFF; // Stencil operations if pixel is front-facing. depthStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; depthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR; depthStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; depthStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS; // Stencil operations if pixel is back-facing. depthStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; depthStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR; depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS; ThrowIfFailed( m_d3dDevice->CreateDepthStencilState( &depthStencilDesc, &m_depthStencilState ) ); m_d3dContext->OMSetDepthStencilState(m_depthStencilState.Get(), 1); CD3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc( D3D11_DSV_DIMENSION_TEXTURE2D, DXGI_FORMAT_D24_UNORM_S8_UINT, 0); ThrowIfFailed( m_d3dDevice->CreateDepthStencilView( m_depthStencilBuffer.Get(), &depthStencilViewDesc, &m_depthStencilView ) ); m_d3dContext->OMSetRenderTargets(1, m_renderTargetView.GetAddressOf(), m_depthStencilView.Get()); CD3D11_VIEWPORT viewport( 0.0f, 0.0f, m_renderTargetSize.Width, m_renderTargetSize.Height ); m_d3dContext->RSSetViewports(1, &viewport); ... Before Draw ... const float midnightBlue[] = { 245.0f/255.0f, 241.0/255.0f, 196.0f/255.0f, 1.000f }; m_d3dContext->ClearRenderTargetView( m_renderTargetView.Get(), midnightBlue ); m_d3dContext->ClearDepthStencilView( m_depthStencilView.Get(), D3D11_CLEAR_DEPTH, 1.0f, 0 ); m_d3dContext->OMSetRenderTargets(1, m_renderTargetView.GetAddressOf(), m_depthStencilView.Get()); //This only works in the Emulator. //m_d3dContext->OMSetRenderTargets(1, m_renderTargetView.GetAddressOf(), nullptr); This works fine but doesn't use the Depth buffer, so the scene is not very good.