daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | // TextureStorage11.cpp: Implements the abstract rx::TextureStorage11 class and its concrete derived |
| 8 | // classes TextureStorage11_2D and TextureStorage11_Cube, which act as the interface to the D3D11 texture. |
| 9 | |
| 10 | #include "libGLESv2/renderer/TextureStorage11.h" |
| 11 | |
| 12 | #include "libGLESv2/renderer/Renderer11.h" |
| 13 | #include "libGLESv2/renderer/RenderTarget11.h" |
| 14 | #include "libGLESv2/renderer/SwapChain11.h" |
| 15 | #include "libGLESv2/renderer/renderer11_utils.h" |
| 16 | |
| 17 | #include "libGLESv2/main.h" |
| 18 | |
| 19 | namespace rx |
| 20 | { |
| 21 | |
| 22 | TextureStorage11::TextureStorage11(Renderer *renderer, UINT bindFlags) |
| 23 | : mBindFlags(bindFlags), |
daniel@transgaming.com | b115455 | 2013-01-11 04:11:02 +0000 | [diff] [blame] | 24 | mLodOffset(0), |
| 25 | mMipLevels(0) |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 26 | { |
| 27 | mRenderer = Renderer11::makeRenderer11(renderer); |
| 28 | } |
| 29 | |
| 30 | TextureStorage11::~TextureStorage11() |
| 31 | { |
daniel@transgaming.com | 1e1b5e9 | 2013-01-11 04:11:27 +0000 | [diff] [blame] | 32 | } |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 33 | |
| 34 | TextureStorage11 *TextureStorage11::makeTextureStorage11(TextureStorage *storage) |
| 35 | { |
| 36 | ASSERT(dynamic_cast<TextureStorage11*>(storage) != NULL); |
| 37 | return static_cast<TextureStorage11*>(storage); |
| 38 | } |
| 39 | |
| 40 | DWORD TextureStorage11::GetTextureBindFlags(DXGI_FORMAT format, GLenum glusage, bool forceRenderable) |
| 41 | { |
| 42 | UINT bindFlags = D3D11_BIND_SHADER_RESOURCE; |
| 43 | |
| 44 | if (format == DXGI_FORMAT_D24_UNORM_S8_UINT) |
| 45 | { |
| 46 | bindFlags |= D3D11_BIND_DEPTH_STENCIL; |
| 47 | } |
| 48 | else if(forceRenderable || (TextureStorage11::IsTextureFormatRenderable(format) && (glusage == GL_FRAMEBUFFER_ATTACHMENT_ANGLE))) |
| 49 | { |
| 50 | bindFlags |= D3D11_BIND_RENDER_TARGET; |
| 51 | } |
| 52 | return bindFlags; |
| 53 | } |
| 54 | |
| 55 | bool TextureStorage11::IsTextureFormatRenderable(DXGI_FORMAT format) |
| 56 | { |
| 57 | switch(format) |
| 58 | { |
| 59 | case DXGI_FORMAT_R8G8B8A8_UNORM: |
| 60 | case DXGI_FORMAT_A8_UNORM: |
| 61 | case DXGI_FORMAT_R32G32B32A32_FLOAT: |
| 62 | case DXGI_FORMAT_R32G32B32_FLOAT: |
| 63 | case DXGI_FORMAT_R16G16B16A16_FLOAT: |
| 64 | case DXGI_FORMAT_B8G8R8A8_UNORM: |
| 65 | case DXGI_FORMAT_R8_UNORM: |
| 66 | case DXGI_FORMAT_R8G8_UNORM: |
| 67 | case DXGI_FORMAT_R16_FLOAT: |
| 68 | case DXGI_FORMAT_R16G16_FLOAT: |
| 69 | return true; |
| 70 | case DXGI_FORMAT_BC1_UNORM: |
| 71 | case DXGI_FORMAT_BC2_UNORM: |
| 72 | case DXGI_FORMAT_BC3_UNORM: |
| 73 | return false; |
| 74 | default: |
| 75 | UNREACHABLE(); |
| 76 | return false; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | UINT TextureStorage11::getBindFlags() const |
| 81 | { |
| 82 | return mBindFlags; |
| 83 | } |
| 84 | |
| 85 | int TextureStorage11::getLodOffset() const |
| 86 | { |
| 87 | return mLodOffset; |
| 88 | } |
| 89 | |
| 90 | bool TextureStorage11::isRenderTarget() const |
| 91 | { |
| 92 | return (mBindFlags & (D3D11_BIND_RENDER_TARGET | D3D11_BIND_DEPTH_STENCIL)) != 0; |
| 93 | } |
| 94 | |
| 95 | bool TextureStorage11::isManaged() const |
| 96 | { |
| 97 | return false; |
| 98 | } |
| 99 | |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 100 | int TextureStorage11::levelCount() |
| 101 | { |
| 102 | int levels = 0; |
| 103 | if (getBaseTexture()) |
| 104 | { |
daniel@transgaming.com | b115455 | 2013-01-11 04:11:02 +0000 | [diff] [blame] | 105 | levels = mMipLevels - getLodOffset(); |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 106 | } |
| 107 | return levels; |
| 108 | } |
| 109 | |
daniel@transgaming.com | 9a2f54d | 2013-01-11 04:06:36 +0000 | [diff] [blame] | 110 | UINT TextureStorage11::getSubresourceIndex(int level, int faceIndex) |
| 111 | { |
| 112 | UINT index = 0; |
| 113 | if (getBaseTexture()) |
| 114 | { |
daniel@transgaming.com | b115455 | 2013-01-11 04:11:02 +0000 | [diff] [blame] | 115 | index = D3D11CalcSubresource(level, faceIndex, mMipLevels); |
daniel@transgaming.com | 9a2f54d | 2013-01-11 04:06:36 +0000 | [diff] [blame] | 116 | } |
| 117 | return index; |
| 118 | } |
| 119 | |
| 120 | bool TextureStorage11::updateSubresourceLevel(ID3D11Texture2D *srcTexture, int level, int face, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height) |
| 121 | { |
| 122 | if (srcTexture) |
| 123 | { |
| 124 | D3D11_BOX srcBox; |
| 125 | srcBox.left = xoffset; |
| 126 | srcBox.top = yoffset; |
| 127 | srcBox.right = xoffset + width; |
| 128 | srcBox.bottom = yoffset + height; |
| 129 | srcBox.front = 0; |
| 130 | srcBox.back = 1; |
| 131 | |
| 132 | ID3D11DeviceContext *context = mRenderer->getDeviceContext(); |
| 133 | |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame^] | 134 | ASSERT(getBaseTexture()); |
daniel@transgaming.com | 9a2f54d | 2013-01-11 04:06:36 +0000 | [diff] [blame] | 135 | context->CopySubresourceRegion(getBaseTexture(), getSubresourceIndex(level, face), xoffset, yoffset, 0, srcTexture, 0, &srcBox); |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | return false; |
| 140 | } |
| 141 | |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 142 | TextureStorage11_2D::TextureStorage11_2D(Renderer *renderer, SwapChain11 *swapchain) |
| 143 | : TextureStorage11(renderer, D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE) |
| 144 | { |
| 145 | ID3D11Texture2D *surfaceTexture = swapchain->getOffscreenTexture(); |
| 146 | mTexture = surfaceTexture; |
daniel@transgaming.com | b50d530 | 2013-01-11 04:07:29 +0000 | [diff] [blame] | 147 | mSRV = NULL; |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame^] | 148 | |
| 149 | for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; i++) |
| 150 | { |
| 151 | mRenderTarget[i] = NULL; |
| 152 | } |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 153 | |
| 154 | D3D11_TEXTURE2D_DESC desc; |
| 155 | surfaceTexture->GetDesc(&desc); |
| 156 | |
daniel@transgaming.com | b115455 | 2013-01-11 04:11:02 +0000 | [diff] [blame] | 157 | mMipLevels = desc.MipLevels; |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame^] | 158 | mTextureFormat = desc.Format; |
| 159 | mTextureWidth = desc.Width; |
| 160 | mTextureHeight = desc.Height; |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | TextureStorage11_2D::TextureStorage11_2D(Renderer *renderer, int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height) |
| 164 | : TextureStorage11(renderer, GetTextureBindFlags(gl_d3d11::ConvertTextureFormat(internalformat), usage, forceRenderable)) |
| 165 | { |
| 166 | mTexture = NULL; |
daniel@transgaming.com | b50d530 | 2013-01-11 04:07:29 +0000 | [diff] [blame] | 167 | mSRV = NULL; |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame^] | 168 | mTextureWidth = 0; |
| 169 | mTextureHeight = 0; |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 170 | DXGI_FORMAT format = gl_d3d11::ConvertTextureFormat(internalformat); |
daniel@transgaming.com | b115455 | 2013-01-11 04:11:02 +0000 | [diff] [blame] | 171 | |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame^] | 172 | for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; i++) |
| 173 | { |
| 174 | mRenderTarget[i] = NULL; |
| 175 | } |
| 176 | |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 177 | // if the width or height is not positive this should be treated as an incomplete texture |
| 178 | // we handle that here by skipping the d3d texture creation |
| 179 | if (width > 0 && height > 0) |
| 180 | { |
| 181 | // adjust size if needed for compressed textures |
| 182 | gl::MakeValidSize(false, gl::IsCompressed(internalformat), &width, &height, &mLodOffset); |
| 183 | |
| 184 | ID3D11Device *device = mRenderer->getDevice(); |
| 185 | |
| 186 | D3D11_TEXTURE2D_DESC desc; |
| 187 | desc.Width = width; // Compressed texture size constraints? |
| 188 | desc.Height = height; |
| 189 | desc.MipLevels = levels + mLodOffset; |
| 190 | desc.ArraySize = 1; |
| 191 | desc.Format = format; |
| 192 | desc.SampleDesc.Count = 1; |
| 193 | desc.SampleDesc.Quality = 0; |
| 194 | desc.Usage = D3D11_USAGE_DEFAULT; |
| 195 | desc.BindFlags = getBindFlags(); |
| 196 | desc.CPUAccessFlags = 0; |
| 197 | desc.MiscFlags = 0; |
| 198 | |
| 199 | HRESULT result = device->CreateTexture2D(&desc, NULL, &mTexture); |
| 200 | |
| 201 | if (FAILED(result)) |
| 202 | { |
| 203 | ASSERT(result == E_OUTOFMEMORY); |
| 204 | ERR("Creating image failed."); |
| 205 | error(GL_OUT_OF_MEMORY); |
| 206 | } |
daniel@transgaming.com | b115455 | 2013-01-11 04:11:02 +0000 | [diff] [blame] | 207 | else |
| 208 | { |
| 209 | mTexture->GetDesc(&desc); |
| 210 | mMipLevels = desc.MipLevels; |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame^] | 211 | mTextureFormat = desc.Format; |
| 212 | mTextureWidth = desc.Width; |
| 213 | mTextureHeight = desc.Height; |
daniel@transgaming.com | b115455 | 2013-01-11 04:11:02 +0000 | [diff] [blame] | 214 | } |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 215 | } |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | TextureStorage11_2D::~TextureStorage11_2D() |
| 219 | { |
| 220 | if (mTexture) |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame] | 221 | { |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 222 | mTexture->Release(); |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame] | 223 | mTexture = NULL; |
| 224 | } |
daniel@transgaming.com | 1e1b5e9 | 2013-01-11 04:11:27 +0000 | [diff] [blame] | 225 | |
| 226 | if (mSRV) |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame] | 227 | { |
daniel@transgaming.com | 1e1b5e9 | 2013-01-11 04:11:27 +0000 | [diff] [blame] | 228 | mSRV->Release(); |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame] | 229 | mSRV = NULL; |
| 230 | } |
| 231 | |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame^] | 232 | for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; i++) |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame] | 233 | { |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame^] | 234 | delete mRenderTarget[i]; |
| 235 | mRenderTarget[i] = NULL; |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame] | 236 | } |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | TextureStorage11_2D *TextureStorage11_2D::makeTextureStorage11_2D(TextureStorage *storage) |
| 240 | { |
| 241 | ASSERT(dynamic_cast<TextureStorage11_2D*>(storage) != NULL); |
| 242 | return static_cast<TextureStorage11_2D*>(storage); |
| 243 | } |
| 244 | |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame^] | 245 | RenderTarget *TextureStorage11_2D::getRenderTarget(int level) |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 246 | { |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame^] | 247 | if (level >= 0 && level < static_cast<int>(mMipLevels)) |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame] | 248 | { |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame^] | 249 | if (!mRenderTarget[level]) |
| 250 | { |
| 251 | ID3D11Device *device = mRenderer->getDevice(); |
| 252 | HRESULT result; |
| 253 | |
| 254 | D3D11_RENDER_TARGET_VIEW_DESC rtvDesc; |
| 255 | rtvDesc.Format = mTextureFormat; |
| 256 | rtvDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D; |
| 257 | rtvDesc.Texture2D.MipSlice = level; |
| 258 | |
| 259 | ID3D11RenderTargetView *rtv; |
| 260 | result = device->CreateRenderTargetView(mTexture, &rtvDesc, &rtv); |
| 261 | |
| 262 | if (result == E_OUTOFMEMORY) |
| 263 | { |
| 264 | return error(GL_OUT_OF_MEMORY, static_cast<RenderTarget*>(NULL)); |
| 265 | } |
| 266 | ASSERT(SUCCEEDED(result)); |
| 267 | |
| 268 | mRenderTarget[level] = new RenderTarget11(mRenderer, rtv, |
| 269 | std::max(mTextureWidth >> level, 1U), |
| 270 | std::max(mTextureHeight >> level, 1U)); |
| 271 | } |
| 272 | |
| 273 | return mRenderTarget[level]; |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame] | 274 | } |
| 275 | else |
| 276 | { |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame^] | 277 | return NULL; |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame] | 278 | } |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | ID3D11Texture2D *TextureStorage11_2D::getBaseTexture() const |
| 282 | { |
| 283 | return mTexture; |
| 284 | } |
| 285 | |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame^] | 286 | ID3D11ShaderResourceView *TextureStorage11_2D::getSRV() |
daniel@transgaming.com | b50d530 | 2013-01-11 04:07:29 +0000 | [diff] [blame] | 287 | { |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame^] | 288 | if (!mSRV) |
| 289 | { |
| 290 | ID3D11Device *device = mRenderer->getDevice(); |
| 291 | |
| 292 | D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc; |
| 293 | srvDesc.Format = mTextureFormat; |
| 294 | srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; |
| 295 | srvDesc.Texture2D.MipLevels = (mMipLevels == 0 ? -1 : mMipLevels); |
| 296 | srvDesc.Texture2D.MostDetailedMip = 0; |
| 297 | |
| 298 | HRESULT result = device->CreateShaderResourceView(mTexture, &srvDesc, &mSRV); |
| 299 | |
| 300 | if (result == E_OUTOFMEMORY) |
| 301 | { |
| 302 | return error(GL_OUT_OF_MEMORY, static_cast<ID3D11ShaderResourceView*>(NULL)); |
| 303 | } |
| 304 | ASSERT(SUCCEEDED(result)); |
| 305 | } |
| 306 | |
daniel@transgaming.com | b50d530 | 2013-01-11 04:07:29 +0000 | [diff] [blame] | 307 | return mSRV; |
| 308 | } |
| 309 | |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 310 | void TextureStorage11_2D::generateMipmap(int level) |
| 311 | { |
| 312 | // TODO |
| 313 | UNIMPLEMENTED(); |
| 314 | } |
| 315 | |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 316 | TextureStorage11_Cube::TextureStorage11_Cube(Renderer *renderer, int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size) |
| 317 | : TextureStorage11(renderer, GetTextureBindFlags(gl_d3d11::ConvertTextureFormat(internalformat), usage, forceRenderable)) |
| 318 | { |
| 319 | mTexture = NULL; |
daniel@transgaming.com | b50d530 | 2013-01-11 04:07:29 +0000 | [diff] [blame] | 320 | mSRV = NULL; |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame^] | 321 | mTextureWidth = 0; |
| 322 | mTextureHeight = 0; |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame] | 323 | |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame^] | 324 | for (unsigned int i = 0; i < 6; i++) |
daniel@transgaming.com | 5cdd058 | 2013-01-11 04:07:35 +0000 | [diff] [blame] | 325 | { |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame^] | 326 | for (unsigned int j = 0; j < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; j++) |
| 327 | { |
| 328 | mRenderTarget[i][j] = NULL; |
| 329 | } |
daniel@transgaming.com | 5cdd058 | 2013-01-11 04:07:35 +0000 | [diff] [blame] | 330 | } |
| 331 | |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 332 | DXGI_FORMAT format = gl_d3d11::ConvertTextureFormat(internalformat); |
| 333 | // if the size is not positive this should be treated as an incomplete texture |
| 334 | // we handle that here by skipping the d3d texture creation |
| 335 | if (size > 0) |
| 336 | { |
| 337 | // adjust size if needed for compressed textures |
| 338 | int height = size; |
| 339 | gl::MakeValidSize(false, gl::IsCompressed(internalformat), &size, &height, &mLodOffset); |
| 340 | |
| 341 | ID3D11Device *device = mRenderer->getDevice(); |
| 342 | |
| 343 | D3D11_TEXTURE2D_DESC desc; |
| 344 | desc.Width = size; |
| 345 | desc.Height = size; |
| 346 | desc.MipLevels = levels + mLodOffset; |
| 347 | desc.ArraySize = 6; |
| 348 | desc.Format = format; |
| 349 | desc.SampleDesc.Count = 1; |
| 350 | desc.SampleDesc.Quality = 0; |
| 351 | desc.Usage = D3D11_USAGE_DEFAULT; |
| 352 | desc.BindFlags = getBindFlags(); |
| 353 | desc.CPUAccessFlags = 0; |
| 354 | desc.MiscFlags = D3D11_RESOURCE_MISC_TEXTURECUBE; |
| 355 | |
| 356 | HRESULT result = device->CreateTexture2D(&desc, NULL, &mTexture); |
| 357 | |
| 358 | if (FAILED(result)) |
| 359 | { |
| 360 | ASSERT(result == E_OUTOFMEMORY); |
| 361 | ERR("Creating image failed."); |
| 362 | error(GL_OUT_OF_MEMORY); |
| 363 | } |
daniel@transgaming.com | b115455 | 2013-01-11 04:11:02 +0000 | [diff] [blame] | 364 | else |
| 365 | { |
| 366 | mTexture->GetDesc(&desc); |
| 367 | mMipLevels = desc.MipLevels; |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame^] | 368 | mTextureFormat = desc.Format; |
| 369 | mTextureWidth = desc.Width; |
| 370 | mTextureHeight = desc.Height; |
daniel@transgaming.com | b115455 | 2013-01-11 04:11:02 +0000 | [diff] [blame] | 371 | } |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 372 | } |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | TextureStorage11_Cube::~TextureStorage11_Cube() |
| 376 | { |
| 377 | if (mTexture) |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame] | 378 | { |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 379 | mTexture->Release(); |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame] | 380 | mTexture = NULL; |
| 381 | } |
daniel@transgaming.com | 1e1b5e9 | 2013-01-11 04:11:27 +0000 | [diff] [blame] | 382 | |
| 383 | if (mSRV) |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame] | 384 | { |
daniel@transgaming.com | 1e1b5e9 | 2013-01-11 04:11:27 +0000 | [diff] [blame] | 385 | mSRV->Release(); |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame] | 386 | mSRV = NULL; |
| 387 | } |
| 388 | |
| 389 | for (unsigned int i = 0; i < 6; i++) |
| 390 | { |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame^] | 391 | for (unsigned int j = 0; j < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; j++) |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame] | 392 | { |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame^] | 393 | delete mRenderTarget[i][j]; |
| 394 | mRenderTarget[i][j] = NULL; |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame] | 395 | } |
| 396 | } |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | TextureStorage11_Cube *TextureStorage11_Cube::makeTextureStorage11_Cube(TextureStorage *storage) |
| 400 | { |
| 401 | ASSERT(dynamic_cast<TextureStorage11_Cube*>(storage) != NULL); |
| 402 | return static_cast<TextureStorage11_Cube*>(storage); |
| 403 | } |
| 404 | |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame^] | 405 | RenderTarget *TextureStorage11_Cube::getRenderTarget(GLenum faceTarget, int level) |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 406 | { |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame^] | 407 | unsigned int faceIdx = gl::TextureCubeMap::faceIndex(faceTarget); |
| 408 | if (level >= 0 && level < static_cast<int>(mMipLevels)) |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame] | 409 | { |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame^] | 410 | if (!mRenderTarget[faceIdx][level]) |
| 411 | { |
| 412 | ID3D11Device *device = mRenderer->getDevice(); |
| 413 | HRESULT result; |
| 414 | |
| 415 | if (getBindFlags() & D3D11_BIND_RENDER_TARGET) |
| 416 | { |
| 417 | D3D11_RENDER_TARGET_VIEW_DESC rtvDesc; |
| 418 | rtvDesc.Format = mTextureFormat; |
| 419 | rtvDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DARRAY; |
| 420 | rtvDesc.Texture2DArray.MipSlice = level; |
| 421 | rtvDesc.Texture2DArray.FirstArraySlice = faceIdx; |
| 422 | rtvDesc.Texture2DArray.ArraySize = 1; |
| 423 | |
| 424 | ID3D11RenderTargetView *rtv; |
| 425 | result = device->CreateRenderTargetView(mTexture, &rtvDesc, &rtv); |
| 426 | |
| 427 | if (result == E_OUTOFMEMORY) |
| 428 | { |
| 429 | return error(GL_OUT_OF_MEMORY, static_cast<RenderTarget*>(NULL)); |
| 430 | } |
| 431 | ASSERT(SUCCEEDED(result)); |
| 432 | |
| 433 | mRenderTarget[faceIdx][level] = new RenderTarget11(mRenderer, rtv, |
| 434 | std::max(mTextureWidth >> level, 1U), |
| 435 | std::max(mTextureHeight >> level, 1U)); |
| 436 | } |
| 437 | else if (getBindFlags() & D3D11_BIND_DEPTH_STENCIL) |
| 438 | { |
| 439 | // TODO |
| 440 | UNIMPLEMENTED(); |
| 441 | } |
| 442 | else UNREACHABLE(); |
| 443 | } |
| 444 | |
| 445 | return mRenderTarget[faceIdx][level]; |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame] | 446 | } |
| 447 | else |
| 448 | { |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame^] | 449 | return NULL; |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame] | 450 | } |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | ID3D11Texture2D *TextureStorage11_Cube::getBaseTexture() const |
| 454 | { |
| 455 | return mTexture; |
| 456 | } |
| 457 | |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame^] | 458 | ID3D11ShaderResourceView *TextureStorage11_Cube::getSRV() |
daniel@transgaming.com | b50d530 | 2013-01-11 04:07:29 +0000 | [diff] [blame] | 459 | { |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame^] | 460 | if (!mSRV) |
| 461 | { |
| 462 | ID3D11Device *device = mRenderer->getDevice(); |
| 463 | |
| 464 | D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc; |
| 465 | srvDesc.Format = mTextureFormat; |
| 466 | srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE; |
| 467 | srvDesc.TextureCube.MipLevels = (mMipLevels == 0 ? -1 : mMipLevels); |
| 468 | srvDesc.TextureCube.MostDetailedMip = 0; |
| 469 | |
| 470 | HRESULT result = device->CreateShaderResourceView(mTexture, &srvDesc, &mSRV); |
| 471 | |
| 472 | if (result == E_OUTOFMEMORY) |
| 473 | { |
| 474 | return error(GL_OUT_OF_MEMORY, static_cast<ID3D11ShaderResourceView*>(NULL)); |
| 475 | } |
| 476 | ASSERT(SUCCEEDED(result)); |
| 477 | } |
| 478 | |
daniel@transgaming.com | b50d530 | 2013-01-11 04:07:29 +0000 | [diff] [blame] | 479 | return mSRV; |
| 480 | } |
| 481 | |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 482 | void TextureStorage11_Cube::generateMipmap(int face, int level) |
| 483 | { |
| 484 | // TODO |
| 485 | UNIMPLEMENTED(); |
| 486 | } |
| 487 | |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 488 | } |