static void dx11_acquire_back_buffer(RenderDevice* dev) { // Get default back buffer D3D_CALL( dev->swap_chain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&dev->backbuffer_tex) ); D3D_CALL( dev->native->CreateRenderTargetView(dev->backbuffer_tex, NULL, &dev->backbuffer_rtv) ); // Create default depth buffer // create depth stencil D3D11_TEXTURE2D_DESC depth_stencil_desc; depth_stencil_desc.Width = dev->window->width(); depth_stencil_desc.Height = dev->window->height(); depth_stencil_desc.MipLevels = 1; depth_stencil_desc.ArraySize = 1; depth_stencil_desc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; depth_stencil_desc.SampleDesc.Count = 1; depth_stencil_desc.SampleDesc.Quality = 0; depth_stencil_desc.Usage = D3D11_USAGE_DEFAULT; depth_stencil_desc.BindFlags = D3D11_BIND_DEPTH_STENCIL; depth_stencil_desc.CPUAccessFlags = 0; depth_stencil_desc.MiscFlags = 0; D3D_CALL( dev->native->CreateTexture2D(&depth_stencil_desc, NULL, &dev->depthstencil_tex) ); // create depth stencil view D3D11_DEPTH_STENCIL_VIEW_DESC depth_stencil_view_desc = CD3D11_DEPTH_STENCIL_VIEW_DESC(); depth_stencil_view_desc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; depth_stencil_view_desc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D; depth_stencil_view_desc.Texture2D.MipSlice = 0; depth_stencil_view_desc.Flags = 0; D3D_CALL( dev->native->CreateDepthStencilView(dev->depthstencil_tex, &depth_stencil_view_desc, &dev->depthstencil_dsv) ); }
void DX11::BackbufferSurface::Resize(ID3D11Texture2D * texture, unsigned width, unsigned height) { this->width = width; this->height = height; device->CreateRenderTargetView(texture, 0, &renderTargetView); CD3D11_TEXTURE2D_DESC depthStencilTextureDesc( DXGI_FORMAT_D24_UNORM_S8_UINT, width, height, 1, 1, D3D11_BIND_DEPTH_STENCIL); ID3D11Texture2D* depthStencilBuffer; device->CreateTexture2D( &depthStencilTextureDesc, 0, &depthStencilBuffer); device->CreateDepthStencilView( depthStencilBuffer, &CD3D11_DEPTH_STENCIL_VIEW_DESC(D3D11_DSV_DIMENSION_TEXTURE2DMS), &depthStencilView); depthStencilBuffer->Release(); viewport = CD3D11_VIEWPORT( 0.0f, 0.0f, static_cast<float>(width), static_cast<float>(height)); }
D3DTexture2D::D3DTexture2D(ID3D11Texture2D* texptr, D3D11_BIND_FLAG bind, DXGI_FORMAT srv_format, DXGI_FORMAT dsv_format, DXGI_FORMAT rtv_format, bool multisampled) : ref(1), tex(texptr), srv(NULL), rtv(NULL), dsv(NULL) { D3D11_SRV_DIMENSION srv_dim = multisampled ? D3D11_SRV_DIMENSION_TEXTURE2DMS : D3D11_SRV_DIMENSION_TEXTURE2D; D3D11_DSV_DIMENSION dsv_dim = multisampled ? D3D11_DSV_DIMENSION_TEXTURE2DMS : D3D11_DSV_DIMENSION_TEXTURE2D; D3D11_RTV_DIMENSION rtv_dim = multisampled ? D3D11_RTV_DIMENSION_TEXTURE2DMS : D3D11_RTV_DIMENSION_TEXTURE2D; D3D11_SHADER_RESOURCE_VIEW_DESC srv_desc = CD3D11_SHADER_RESOURCE_VIEW_DESC(srv_dim, srv_format); D3D11_DEPTH_STENCIL_VIEW_DESC dsv_desc = CD3D11_DEPTH_STENCIL_VIEW_DESC(dsv_dim, dsv_format); D3D11_RENDER_TARGET_VIEW_DESC rtv_desc = CD3D11_RENDER_TARGET_VIEW_DESC(rtv_dim, rtv_format); if (bind & D3D11_BIND_SHADER_RESOURCE) D3D::device->CreateShaderResourceView(tex, &srv_desc, &srv); if (bind & D3D11_BIND_RENDER_TARGET) D3D::device->CreateRenderTargetView(tex, &rtv_desc, &rtv); if (bind & D3D11_BIND_DEPTH_STENCIL) D3D::device->CreateDepthStencilView(tex, &dsv_desc, &dsv); tex->AddRef(); }
bool CCGrabber::Initialize(ID3D11Device* device, CCTexture2D *pTexture) { HRESULT result; D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc; ID3D11ShaderResourceView* tmpShaderResourceView=pTexture->getTextureResource(); ID3D11Resource* tmpResource; tmpShaderResourceView->GetResource(&tmpResource); D3D11_TEXTURE2D_DESC pDesc; ((ID3D11Texture2D*)tmpResource)->GetDesc(&pDesc); // Setup the description of the render target view. renderTargetViewDesc.Format = pDesc.Format;//textureDesc.Format; renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D; renderTargetViewDesc.Texture2D.MipSlice = 0; // Create the render target view. result = device->CreateRenderTargetView(tmpResource, &renderTargetViewDesc, &m_renderTargetView); if(FAILED(result)) { return false; } CD3D11_TEXTURE2D_DESC depthStencilDesc( DXGI_FORMAT_D24_UNORM_S8_UINT, pDesc.Width, pDesc.Height, 1, 1, D3D11_BIND_DEPTH_STENCIL ); result = device->CreateTexture2D( &depthStencilDesc, nullptr, &m_depthStencil ); if (FAILED(result)) { return false; } result = device->CreateDepthStencilView(m_depthStencil, &CD3D11_DEPTH_STENCIL_VIEW_DESC(D3D11_DSV_DIMENSION_TEXTURE2D), &m_depthStencilView ); if(FAILED(result)) { return false; } return true; }
void DX11::TextureSurface::OnResetDevice(Gpu::Api * gpu) { OnLostDevice(gpu); unsigned width = unsigned(viewport.Width); unsigned height = unsigned(viewport.Height); bool typeless = (format == Gpu::DrawSurface::Format_Typeless); if(relativeWindow) { gpu->GetBackbufferSize(width, height, relativeWindow); width = unsigned(widthFactor * float(width)); height = unsigned(heightFactor * float(height)); } viewport = CD3D11_VIEWPORT(0.0f, 0.0f, float(width), float(height)); static const DXGI_FORMAT SURFACE_FORMAT_TO_DXGI_FORMAT[Format_Total] = { DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_B8G8R8A8_UNORM, DXGI_FORMAT_R16G16B16A16_FLOAT, DXGI_FORMAT_R11G11B10_FLOAT, DXGI_FORMAT_R16_FLOAT, DXGI_FORMAT_R24G8_TYPELESS }; DXGI_FORMAT dxgiFormat = SURFACE_FORMAT_TO_DXGI_FORMAT[format]; unsigned mipLevels = generateMips ? min(width, height) / 4 : 1; D3D11_BIND_FLAG bindFlag = typeless ? D3D11_BIND_DEPTH_STENCIL : D3D11_BIND_RENDER_TARGET; ID3D11Texture2D * texture2D = 0; CD3D11_TEXTURE2D_DESC textureDesc( dxgiFormat, width, height, 1, mipLevels, // MIP LEVELS - *MUST NEVER* BE GREATER THAN log2(width) OR log2(height) bindFlag | D3D11_BIND_SHADER_RESOURCE); device->CreateTexture2D(&textureDesc, 0, &texture2D); if(typeless) dxgiFormat = DXGI_FORMAT_R24_UNORM_X8_TYPELESS; ID3D11ShaderResourceView * shaderView; CD3D11_SHADER_RESOURCE_VIEW_DESC shaderViewDesc( texture2D, D3D11_SRV_DIMENSION_TEXTURE2D, dxgiFormat, 0, mipLevels); device->CreateShaderResourceView(texture2D, &shaderViewDesc, &shaderView); if(!typeless) { CD3D11_RENDER_TARGET_VIEW_DESC renderViewDesc( texture2D, D3D11_RTV_DIMENSION_TEXTURE2D, dxgiFormat); device->CreateRenderTargetView(texture2D, &renderViewDesc, &renderTargetView); CD3D11_TEXTURE2D_DESC depthStencilTextureDesc( DXGI_FORMAT_D24_UNORM_S8_UINT, width, height, 1, 1, D3D11_BIND_DEPTH_STENCIL); device->CreateTexture2D(&depthStencilTextureDesc, 0, &depthStencilTexture); device->CreateDepthStencilView( depthStencilTexture, &CD3D11_DEPTH_STENCIL_VIEW_DESC(D3D11_DSV_DIMENSION_TEXTURE2D), &depthStencilView); } else { device->CreateDepthStencilView( texture2D, &CD3D11_DEPTH_STENCIL_VIEW_DESC(D3D11_DSV_DIMENSION_TEXTURE2D, DXGI_FORMAT_D24_UNORM_S8_UINT), &depthStencilView); } texture = new DX11::Texture(texture2D, shaderView, textureDesc); Clear(); }