Hash :
183408d0
Author :
Date :
2013-01-25T21:50:07
RenderTarget11 now stores a shader resource. TRAC #22358 Signed-off-by: Nicolas Capens Signed-off-by: Shannon Woods Author: Geoff Lang git-svn-id: https://angleproject.googlecode.com/svn/branches/dx11proto@1744 736b8ea6-26fd-11df-bfd4-992fa37f6226
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
//
// Copyright (c) 2012 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// RenderTarget11.cpp: Implements a DX11-specific wrapper for ID3D11View pointers
// retained by Renderbuffers.
#include "libGLESv2/renderer/RenderTarget11.h"
#include "libGLESv2/renderer/Renderer11.h"
#include "libGLESv2/renderer/renderer11_utils.h"
#include "libGLESv2/main.h"
namespace rx
{
static ID3D11Texture2D *getTextureResource(ID3D11View *view)
{
ID3D11Resource *textureResource = NULL;
view->GetResource(&textureResource);
if (!textureResource)
{
return NULL;
}
ID3D11Texture2D *texture = NULL;
HRESULT result = textureResource->QueryInterface(IID_ID3D11Texture2D, (void**)&texture);
textureResource->Release();
textureResource = NULL;
if (FAILED(result))
{
return NULL;
}
return texture;
}
static unsigned int getRTVSubresourceIndex(ID3D11RenderTargetView *view)
{
D3D11_RENDER_TARGET_VIEW_DESC rtvDesc;
view->GetDesc(&rtvDesc);
ID3D11Texture2D *texture = getTextureResource(view);
if (!texture)
{
ERR("Failed to extract the ID3D11Texture2D from the render target view.");
return 0;
}
D3D11_TEXTURE2D_DESC texDesc;
texture->GetDesc(&texDesc);
texture->Release();
texture = NULL;
unsigned int mipSlice = 0;
unsigned int arraySlice = 0;
unsigned int mipLevels = texDesc.MipLevels;
switch (rtvDesc.ViewDimension)
{
case D3D11_RTV_DIMENSION_TEXTURE1D:
mipSlice = rtvDesc.Texture1D.MipSlice;
arraySlice = 0;
break;
case D3D11_RTV_DIMENSION_TEXTURE1DARRAY:
mipSlice = rtvDesc.Texture1DArray.MipSlice;
arraySlice = rtvDesc.Texture1DArray.FirstArraySlice;
break;
case D3D11_RTV_DIMENSION_TEXTURE2D:
mipSlice = rtvDesc.Texture2D.MipSlice;
arraySlice = 0;
break;
case D3D11_RTV_DIMENSION_TEXTURE2DARRAY:
mipSlice = rtvDesc.Texture2DArray.MipSlice;
arraySlice = rtvDesc.Texture2DArray.FirstArraySlice;
break;
case D3D11_RTV_DIMENSION_TEXTURE2DMS:
mipSlice = 0;
arraySlice = 0;
break;
case D3D11_RTV_DIMENSION_TEXTURE2DMSARRAY:
mipSlice = 0;
arraySlice = rtvDesc.Texture2DMSArray.FirstArraySlice;
break;
case D3D11_RTV_DIMENSION_TEXTURE3D:
mipSlice = rtvDesc.Texture3D.MipSlice;
arraySlice = 0;
break;
case D3D11_RTV_DIMENSION_UNKNOWN:
case D3D11_RTV_DIMENSION_BUFFER:
UNIMPLEMENTED();
break;
default:
UNREACHABLE();
break;
}
return D3D11CalcSubresource(mipSlice, arraySlice, mipLevels);
}
static unsigned int getDSVSubresourceIndex(ID3D11DepthStencilView *view)
{
D3D11_DEPTH_STENCIL_VIEW_DESC dsvDesc;
view->GetDesc(&dsvDesc);
ID3D11Texture2D *texture = getTextureResource(view);
if (!texture)
{
ERR("Failed to extract the ID3D11Texture2D from the depth stencil view.");
return 0;
}
D3D11_TEXTURE2D_DESC texDesc;
texture->GetDesc(&texDesc);
texture->Release();
texture = NULL;
unsigned int mipSlice = 0;
unsigned int arraySlice = 0;
unsigned int mipLevels = texDesc.MipLevels;
switch (dsvDesc.ViewDimension)
{
case D3D11_DSV_DIMENSION_TEXTURE1D:
mipSlice = dsvDesc.Texture1D.MipSlice;
arraySlice = 0;
break;
case D3D11_DSV_DIMENSION_TEXTURE1DARRAY:
mipSlice = dsvDesc.Texture1DArray.MipSlice;
arraySlice = dsvDesc.Texture1DArray.FirstArraySlice;
break;
case D3D11_DSV_DIMENSION_TEXTURE2D:
mipSlice = dsvDesc.Texture2D.MipSlice;
arraySlice = 0;
break;
case D3D11_DSV_DIMENSION_TEXTURE2DARRAY:
mipSlice = dsvDesc.Texture2DArray.MipSlice;
arraySlice = dsvDesc.Texture2DArray.FirstArraySlice;
break;
case D3D11_DSV_DIMENSION_TEXTURE2DMS:
mipSlice = 0;
arraySlice = 0;
break;
case D3D11_DSV_DIMENSION_TEXTURE2DMSARRAY:
mipSlice = 0;
arraySlice = dsvDesc.Texture2DMSArray.FirstArraySlice;
break;
case D3D11_RTV_DIMENSION_UNKNOWN:
UNIMPLEMENTED();
break;
default:
UNREACHABLE();
break;
}
return D3D11CalcSubresource(mipSlice, arraySlice, mipLevels);
}
RenderTarget11::RenderTarget11(Renderer *renderer, ID3D11RenderTargetView *rtv, ID3D11ShaderResourceView *srv, GLsizei width, GLsizei height)
{
mRenderer = Renderer11::makeRenderer11(renderer);
mRenderTarget = rtv;
mDepthStencil = NULL;
mShaderResource = srv;
if (mRenderTarget)
{
D3D11_RENDER_TARGET_VIEW_DESC desc;
mRenderTarget->GetDesc(&desc);
mSubresourceIndex = getRTVSubresourceIndex(mRenderTarget);
mWidth = width;
mHeight = height;
mInternalFormat = d3d11_gl::ConvertTextureInternalFormat(desc.Format);
mActualFormat = d3d11_gl::ConvertTextureInternalFormat(desc.Format);
mSamples = 1; // TEMP?
}
}
RenderTarget11::RenderTarget11(Renderer *renderer, ID3D11DepthStencilView *dsv, ID3D11ShaderResourceView *srv, GLsizei width, GLsizei height)
{
mRenderer = Renderer11::makeRenderer11(renderer);
mRenderTarget = NULL;
mDepthStencil = dsv;
mShaderResource = srv;
if (mDepthStencil)
{
D3D11_DEPTH_STENCIL_VIEW_DESC desc;
mDepthStencil->GetDesc(&desc);
mSubresourceIndex = getDSVSubresourceIndex(mDepthStencil);
mWidth = width;
mHeight = height;
mInternalFormat = d3d11_gl::ConvertTextureInternalFormat(desc.Format);
mActualFormat = d3d11_gl::ConvertTextureInternalFormat(desc.Format);
mSamples = 1; // TEMP?
}
}
RenderTarget11::RenderTarget11(Renderer *renderer, GLsizei width, GLsizei height, GLenum format, GLsizei samples, bool depth)
{
mRenderer = Renderer11::makeRenderer11(renderer);
mRenderTarget = NULL;
mDepthStencil = NULL;
mShaderResource = NULL;
DXGI_FORMAT requestedFormat = gl_d3d11::ConvertRenderbufferFormat(format);
int supportedSamples = 0; // TODO - Multisample support query
if (supportedSamples == -1)
{
error(GL_OUT_OF_MEMORY);
return;
}
HRESULT result = D3DERR_INVALIDCALL;
if (width > 0 && height > 0)
{
// Create texture resource
D3D11_TEXTURE2D_DESC desc;
desc.Width = width;
desc.Height = height;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.Format = requestedFormat;
desc.SampleDesc.Count = (supportedSamples == 0 ? 1 : supportedSamples);
desc.SampleDesc.Quality = 0;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.CPUAccessFlags = 0;
desc.MiscFlags = 0;
desc.BindFlags = (depth ? D3D11_BIND_DEPTH_STENCIL : (D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE));
ID3D11Texture2D *rtTexture = NULL;
ID3D11Device *device = mRenderer->getDevice();
HRESULT result = device->CreateTexture2D(&desc, NULL, &rtTexture);
if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
{
error(GL_OUT_OF_MEMORY);
return;
}
ASSERT(SUCCEEDED(result));
if (depth)
{
D3D11_DEPTH_STENCIL_VIEW_DESC dsvDesc;
dsvDesc.Format = requestedFormat;
dsvDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
dsvDesc.Texture2D.MipSlice = 0;
dsvDesc.Flags = 0;
result = device->CreateDepthStencilView(rtTexture, &dsvDesc, &mDepthStencil);
if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
{
rtTexture->Release();
error(GL_OUT_OF_MEMORY);
return;
}
ASSERT(SUCCEEDED(result));
}
else
{
D3D11_RENDER_TARGET_VIEW_DESC rtvDesc;
rtvDesc.Format = requestedFormat;
rtvDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
rtvDesc.Texture2D.MipSlice = 0;
result = device->CreateRenderTargetView(rtTexture, &rtvDesc, &mRenderTarget);
if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
{
rtTexture->Release();
error(GL_OUT_OF_MEMORY);
return;
}
ASSERT(SUCCEEDED(result));
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
srvDesc.Format = requestedFormat;
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MostDetailedMip = 0;
srvDesc.Texture2D.MipLevels = 1;
result = device->CreateShaderResourceView(rtTexture, &srvDesc, &mShaderResource);
if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
{
rtTexture->Release();
mRenderTarget->Release();
error(GL_OUT_OF_MEMORY);
return;
}
ASSERT(SUCCEEDED(result));
}
}
mWidth = width;
mHeight = height;
mInternalFormat = format;
mSamples = supportedSamples;
mActualFormat = format;
mSubresourceIndex = D3D11CalcSubresource(0, 0, 1);
}
RenderTarget11::~RenderTarget11()
{
if (mRenderTarget)
{
mRenderTarget->Release();
mRenderTarget = NULL;
}
if (mDepthStencil)
{
mDepthStencil->Release();
mDepthStencil = NULL;
}
if (mShaderResource)
{
mShaderResource->Release();
mShaderResource = NULL;
}
}
RenderTarget11 *RenderTarget11::makeRenderTarget11(RenderTarget *target)
{
ASSERT(dynamic_cast<rx::RenderTarget11*>(target) != NULL);
return static_cast<rx::RenderTarget11*>(target);
}
// Adds reference, caller must call Release
ID3D11RenderTargetView *RenderTarget11::getRenderTargetView() const
{
if (mRenderTarget)
{
mRenderTarget->AddRef();
}
return mRenderTarget;
}
// Adds reference, caller must call Release
ID3D11DepthStencilView *RenderTarget11::getDepthStencilView() const
{
if (mDepthStencil)
{
mDepthStencil->AddRef();
}
return mDepthStencil;
}
// Adds reference, caller must call Release
ID3D11ShaderResourceView *RenderTarget11::getShaderResourceView() const
{
if (mShaderResource)
{
mShaderResource->AddRef();
}
return mShaderResource;
}
unsigned int RenderTarget11::getSubresourceIndex() const
{
return mSubresourceIndex;
}
}