1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/angle/src/libGLESv2/renderer/RenderTarget11.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,355 @@ 1.4 +#include "precompiled.h" 1.5 +// 1.6 +// Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. 1.7 +// Use of this source code is governed by a BSD-style license that can be 1.8 +// found in the LICENSE file. 1.9 +// 1.10 + 1.11 +// RenderTarget11.cpp: Implements a DX11-specific wrapper for ID3D11View pointers 1.12 +// retained by Renderbuffers. 1.13 + 1.14 +#include "libGLESv2/renderer/RenderTarget11.h" 1.15 +#include "libGLESv2/renderer/Renderer11.h" 1.16 + 1.17 +#include "libGLESv2/renderer/renderer11_utils.h" 1.18 +#include "libGLESv2/main.h" 1.19 + 1.20 +namespace rx 1.21 +{ 1.22 + 1.23 +static unsigned int getRTVSubresourceIndex(ID3D11Texture2D *texture, ID3D11RenderTargetView *view) 1.24 +{ 1.25 + D3D11_RENDER_TARGET_VIEW_DESC rtvDesc; 1.26 + view->GetDesc(&rtvDesc); 1.27 + 1.28 + D3D11_TEXTURE2D_DESC texDesc; 1.29 + texture->GetDesc(&texDesc); 1.30 + 1.31 + unsigned int mipSlice = 0; 1.32 + unsigned int arraySlice = 0; 1.33 + unsigned int mipLevels = texDesc.MipLevels; 1.34 + 1.35 + switch (rtvDesc.ViewDimension) 1.36 + { 1.37 + case D3D11_RTV_DIMENSION_TEXTURE1D: 1.38 + mipSlice = rtvDesc.Texture1D.MipSlice; 1.39 + arraySlice = 0; 1.40 + break; 1.41 + 1.42 + case D3D11_RTV_DIMENSION_TEXTURE1DARRAY: 1.43 + mipSlice = rtvDesc.Texture1DArray.MipSlice; 1.44 + arraySlice = rtvDesc.Texture1DArray.FirstArraySlice; 1.45 + break; 1.46 + 1.47 + case D3D11_RTV_DIMENSION_TEXTURE2D: 1.48 + mipSlice = rtvDesc.Texture2D.MipSlice; 1.49 + arraySlice = 0; 1.50 + break; 1.51 + 1.52 + case D3D11_RTV_DIMENSION_TEXTURE2DARRAY: 1.53 + mipSlice = rtvDesc.Texture2DArray.MipSlice; 1.54 + arraySlice = rtvDesc.Texture2DArray.FirstArraySlice; 1.55 + break; 1.56 + 1.57 + case D3D11_RTV_DIMENSION_TEXTURE2DMS: 1.58 + mipSlice = 0; 1.59 + arraySlice = 0; 1.60 + break; 1.61 + 1.62 + case D3D11_RTV_DIMENSION_TEXTURE2DMSARRAY: 1.63 + mipSlice = 0; 1.64 + arraySlice = rtvDesc.Texture2DMSArray.FirstArraySlice; 1.65 + break; 1.66 + 1.67 + case D3D11_RTV_DIMENSION_TEXTURE3D: 1.68 + mipSlice = rtvDesc.Texture3D.MipSlice; 1.69 + arraySlice = 0; 1.70 + break; 1.71 + 1.72 + case D3D11_RTV_DIMENSION_UNKNOWN: 1.73 + case D3D11_RTV_DIMENSION_BUFFER: 1.74 + UNIMPLEMENTED(); 1.75 + break; 1.76 + 1.77 + default: 1.78 + UNREACHABLE(); 1.79 + break; 1.80 + } 1.81 + 1.82 + return D3D11CalcSubresource(mipSlice, arraySlice, mipLevels); 1.83 +} 1.84 + 1.85 +static unsigned int getDSVSubresourceIndex(ID3D11Texture2D *texture, ID3D11DepthStencilView *view) 1.86 +{ 1.87 + D3D11_DEPTH_STENCIL_VIEW_DESC dsvDesc; 1.88 + view->GetDesc(&dsvDesc); 1.89 + 1.90 + D3D11_TEXTURE2D_DESC texDesc; 1.91 + texture->GetDesc(&texDesc); 1.92 + 1.93 + unsigned int mipSlice = 0; 1.94 + unsigned int arraySlice = 0; 1.95 + unsigned int mipLevels = texDesc.MipLevels; 1.96 + 1.97 + switch (dsvDesc.ViewDimension) 1.98 + { 1.99 + case D3D11_DSV_DIMENSION_TEXTURE1D: 1.100 + mipSlice = dsvDesc.Texture1D.MipSlice; 1.101 + arraySlice = 0; 1.102 + break; 1.103 + 1.104 + case D3D11_DSV_DIMENSION_TEXTURE1DARRAY: 1.105 + mipSlice = dsvDesc.Texture1DArray.MipSlice; 1.106 + arraySlice = dsvDesc.Texture1DArray.FirstArraySlice; 1.107 + break; 1.108 + 1.109 + case D3D11_DSV_DIMENSION_TEXTURE2D: 1.110 + mipSlice = dsvDesc.Texture2D.MipSlice; 1.111 + arraySlice = 0; 1.112 + break; 1.113 + 1.114 + case D3D11_DSV_DIMENSION_TEXTURE2DARRAY: 1.115 + mipSlice = dsvDesc.Texture2DArray.MipSlice; 1.116 + arraySlice = dsvDesc.Texture2DArray.FirstArraySlice; 1.117 + break; 1.118 + 1.119 + case D3D11_DSV_DIMENSION_TEXTURE2DMS: 1.120 + mipSlice = 0; 1.121 + arraySlice = 0; 1.122 + break; 1.123 + 1.124 + case D3D11_DSV_DIMENSION_TEXTURE2DMSARRAY: 1.125 + mipSlice = 0; 1.126 + arraySlice = dsvDesc.Texture2DMSArray.FirstArraySlice; 1.127 + break; 1.128 + 1.129 + case D3D11_RTV_DIMENSION_UNKNOWN: 1.130 + UNIMPLEMENTED(); 1.131 + break; 1.132 + 1.133 + default: 1.134 + UNREACHABLE(); 1.135 + break; 1.136 + } 1.137 + 1.138 + return D3D11CalcSubresource(mipSlice, arraySlice, mipLevels); 1.139 +} 1.140 + 1.141 +RenderTarget11::RenderTarget11(Renderer *renderer, ID3D11RenderTargetView *rtv, ID3D11Texture2D *tex, ID3D11ShaderResourceView *srv, GLsizei width, GLsizei height) 1.142 +{ 1.143 + mRenderer = Renderer11::makeRenderer11(renderer); 1.144 + mTexture = tex; 1.145 + mRenderTarget = rtv; 1.146 + mDepthStencil = NULL; 1.147 + mShaderResource = srv; 1.148 + mSubresourceIndex = 0; 1.149 + 1.150 + if (mRenderTarget && mTexture) 1.151 + { 1.152 + D3D11_RENDER_TARGET_VIEW_DESC desc; 1.153 + mRenderTarget->GetDesc(&desc); 1.154 + 1.155 + D3D11_TEXTURE2D_DESC texDesc; 1.156 + mTexture->GetDesc(&texDesc); 1.157 + 1.158 + mSubresourceIndex = getRTVSubresourceIndex(mTexture, mRenderTarget); 1.159 + mWidth = width; 1.160 + mHeight = height; 1.161 + mSamples = (texDesc.SampleDesc.Count > 1) ? texDesc.SampleDesc.Count : 0; 1.162 + 1.163 + mInternalFormat = d3d11_gl::ConvertTextureInternalFormat(desc.Format); 1.164 + mActualFormat = d3d11_gl::ConvertTextureInternalFormat(desc.Format); 1.165 + } 1.166 +} 1.167 + 1.168 +RenderTarget11::RenderTarget11(Renderer *renderer, ID3D11DepthStencilView *dsv, ID3D11Texture2D *tex, ID3D11ShaderResourceView *srv, GLsizei width, GLsizei height) 1.169 +{ 1.170 + mRenderer = Renderer11::makeRenderer11(renderer); 1.171 + mTexture = tex; 1.172 + mRenderTarget = NULL; 1.173 + mDepthStencil = dsv; 1.174 + mShaderResource = srv; 1.175 + mSubresourceIndex = 0; 1.176 + 1.177 + if (mDepthStencil && mTexture) 1.178 + { 1.179 + D3D11_DEPTH_STENCIL_VIEW_DESC desc; 1.180 + mDepthStencil->GetDesc(&desc); 1.181 + 1.182 + D3D11_TEXTURE2D_DESC texDesc; 1.183 + mTexture->GetDesc(&texDesc); 1.184 + 1.185 + mSubresourceIndex = getDSVSubresourceIndex(mTexture, mDepthStencil); 1.186 + mWidth = width; 1.187 + mHeight = height; 1.188 + mSamples = (texDesc.SampleDesc.Count > 1) ? texDesc.SampleDesc.Count : 0; 1.189 + 1.190 + mInternalFormat = d3d11_gl::ConvertTextureInternalFormat(desc.Format); 1.191 + mActualFormat = d3d11_gl::ConvertTextureInternalFormat(desc.Format); 1.192 + } 1.193 +} 1.194 + 1.195 +RenderTarget11::RenderTarget11(Renderer *renderer, GLsizei width, GLsizei height, GLenum format, GLsizei samples, bool depth) 1.196 +{ 1.197 + mRenderer = Renderer11::makeRenderer11(renderer); 1.198 + mTexture = NULL; 1.199 + mRenderTarget = NULL; 1.200 + mDepthStencil = NULL; 1.201 + mShaderResource = NULL; 1.202 + 1.203 + DXGI_FORMAT requestedFormat = gl_d3d11::ConvertRenderbufferFormat(format); 1.204 + 1.205 + int supportedSamples = mRenderer->getNearestSupportedSamples(requestedFormat, samples); 1.206 + if (supportedSamples < 0) 1.207 + { 1.208 + gl::error(GL_OUT_OF_MEMORY); 1.209 + return; 1.210 + } 1.211 + 1.212 + if (width > 0 && height > 0) 1.213 + { 1.214 + // Create texture resource 1.215 + D3D11_TEXTURE2D_DESC desc; 1.216 + desc.Width = width; 1.217 + desc.Height = height; 1.218 + desc.MipLevels = 1; 1.219 + desc.ArraySize = 1; 1.220 + desc.Format = requestedFormat; 1.221 + desc.SampleDesc.Count = (supportedSamples == 0) ? 1 : supportedSamples; 1.222 + desc.SampleDesc.Quality = 0; 1.223 + desc.Usage = D3D11_USAGE_DEFAULT; 1.224 + desc.CPUAccessFlags = 0; 1.225 + desc.MiscFlags = 0; 1.226 + desc.BindFlags = (depth ? D3D11_BIND_DEPTH_STENCIL : (D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE)); 1.227 + 1.228 + ID3D11Device *device = mRenderer->getDevice(); 1.229 + HRESULT result = device->CreateTexture2D(&desc, NULL, &mTexture); 1.230 + 1.231 + if (result == E_OUTOFMEMORY) 1.232 + { 1.233 + gl::error(GL_OUT_OF_MEMORY); 1.234 + return; 1.235 + } 1.236 + ASSERT(SUCCEEDED(result)); 1.237 + 1.238 + if (depth) 1.239 + { 1.240 + D3D11_DEPTH_STENCIL_VIEW_DESC dsvDesc; 1.241 + dsvDesc.Format = requestedFormat; 1.242 + dsvDesc.ViewDimension = (supportedSamples == 0) ? D3D11_DSV_DIMENSION_TEXTURE2D : D3D11_DSV_DIMENSION_TEXTURE2DMS; 1.243 + dsvDesc.Texture2D.MipSlice = 0; 1.244 + dsvDesc.Flags = 0; 1.245 + result = device->CreateDepthStencilView(mTexture, &dsvDesc, &mDepthStencil); 1.246 + 1.247 + if (result == E_OUTOFMEMORY) 1.248 + { 1.249 + mTexture->Release(); 1.250 + mTexture = NULL; 1.251 + gl::error(GL_OUT_OF_MEMORY); 1.252 + } 1.253 + ASSERT(SUCCEEDED(result)); 1.254 + } 1.255 + else 1.256 + { 1.257 + D3D11_RENDER_TARGET_VIEW_DESC rtvDesc; 1.258 + rtvDesc.Format = requestedFormat; 1.259 + rtvDesc.ViewDimension = (supportedSamples == 0) ? D3D11_RTV_DIMENSION_TEXTURE2D : D3D11_RTV_DIMENSION_TEXTURE2DMS; 1.260 + rtvDesc.Texture2D.MipSlice = 0; 1.261 + result = device->CreateRenderTargetView(mTexture, &rtvDesc, &mRenderTarget); 1.262 + 1.263 + if (result == E_OUTOFMEMORY) 1.264 + { 1.265 + mTexture->Release(); 1.266 + mTexture = NULL; 1.267 + gl::error(GL_OUT_OF_MEMORY); 1.268 + return; 1.269 + } 1.270 + ASSERT(SUCCEEDED(result)); 1.271 + 1.272 + D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc; 1.273 + srvDesc.Format = requestedFormat; 1.274 + srvDesc.ViewDimension = (supportedSamples == 0) ? D3D11_SRV_DIMENSION_TEXTURE2D : D3D11_SRV_DIMENSION_TEXTURE2DMS; 1.275 + srvDesc.Texture2D.MostDetailedMip = 0; 1.276 + srvDesc.Texture2D.MipLevels = 1; 1.277 + result = device->CreateShaderResourceView(mTexture, &srvDesc, &mShaderResource); 1.278 + 1.279 + if (result == E_OUTOFMEMORY) 1.280 + { 1.281 + mTexture->Release(); 1.282 + mTexture = NULL; 1.283 + mRenderTarget->Release(); 1.284 + mRenderTarget = NULL; 1.285 + gl::error(GL_OUT_OF_MEMORY); 1.286 + return; 1.287 + } 1.288 + ASSERT(SUCCEEDED(result)); 1.289 + } 1.290 + } 1.291 + 1.292 + mWidth = width; 1.293 + mHeight = height; 1.294 + mInternalFormat = format; 1.295 + mSamples = supportedSamples; 1.296 + mActualFormat = d3d11_gl::ConvertTextureInternalFormat(requestedFormat); 1.297 + mSubresourceIndex = D3D11CalcSubresource(0, 0, 1); 1.298 +} 1.299 + 1.300 +RenderTarget11::~RenderTarget11() 1.301 +{ 1.302 + if (mTexture) 1.303 + { 1.304 + mTexture->Release(); 1.305 + mTexture = NULL; 1.306 + } 1.307 + 1.308 + if (mRenderTarget) 1.309 + { 1.310 + mRenderTarget->Release(); 1.311 + mRenderTarget = NULL; 1.312 + } 1.313 + 1.314 + if (mDepthStencil) 1.315 + { 1.316 + mDepthStencil->Release(); 1.317 + mDepthStencil = NULL; 1.318 + } 1.319 + 1.320 + if (mShaderResource) 1.321 + { 1.322 + mShaderResource->Release(); 1.323 + mShaderResource = NULL; 1.324 + } 1.325 +} 1.326 + 1.327 +RenderTarget11 *RenderTarget11::makeRenderTarget11(RenderTarget *target) 1.328 +{ 1.329 + ASSERT(HAS_DYNAMIC_TYPE(rx::RenderTarget11*, target)); 1.330 + return static_cast<rx::RenderTarget11*>(target); 1.331 +} 1.332 + 1.333 +ID3D11Texture2D *RenderTarget11::getTexture() const 1.334 +{ 1.335 + return mTexture; 1.336 +} 1.337 + 1.338 +ID3D11RenderTargetView *RenderTarget11::getRenderTargetView() const 1.339 +{ 1.340 + return mRenderTarget; 1.341 +} 1.342 + 1.343 +ID3D11DepthStencilView *RenderTarget11::getDepthStencilView() const 1.344 +{ 1.345 + return mDepthStencil; 1.346 +} 1.347 + 1.348 +ID3D11ShaderResourceView *RenderTarget11::getShaderResourceView() const 1.349 +{ 1.350 + return mShaderResource; 1.351 +} 1.352 + 1.353 +unsigned int RenderTarget11::getSubresourceIndex() const 1.354 +{ 1.355 + return mSubresourceIndex; 1.356 +} 1.357 + 1.358 +}