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 | |
| 134 | ASSERT(getBaseTexture()); |
| 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; |
daniel@transgaming.com | 5cdd058 | 2013-01-11 04:07:35 +0000 | [diff] [blame] | 148 | mRenderTarget = NULL; |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 149 | |
| 150 | D3D11_TEXTURE2D_DESC desc; |
| 151 | surfaceTexture->GetDesc(&desc); |
| 152 | |
daniel@transgaming.com | b115455 | 2013-01-11 04:11:02 +0000 | [diff] [blame] | 153 | mMipLevels = desc.MipLevels; |
| 154 | |
daniel@transgaming.com | b50d530 | 2013-01-11 04:07:29 +0000 | [diff] [blame] | 155 | initializeSRV(desc.Format, desc.MipLevels); |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 156 | initializeRenderTarget(desc.Format, desc.Width, desc.Height); |
| 157 | } |
| 158 | |
| 159 | TextureStorage11_2D::TextureStorage11_2D(Renderer *renderer, int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height) |
| 160 | : TextureStorage11(renderer, GetTextureBindFlags(gl_d3d11::ConvertTextureFormat(internalformat), usage, forceRenderable)) |
| 161 | { |
| 162 | mTexture = NULL; |
daniel@transgaming.com | b50d530 | 2013-01-11 04:07:29 +0000 | [diff] [blame] | 163 | mSRV = NULL; |
daniel@transgaming.com | 5cdd058 | 2013-01-11 04:07:35 +0000 | [diff] [blame] | 164 | mRenderTarget = NULL; |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 165 | DXGI_FORMAT format = gl_d3d11::ConvertTextureFormat(internalformat); |
daniel@transgaming.com | b115455 | 2013-01-11 04:11:02 +0000 | [diff] [blame] | 166 | |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 167 | // if the width or height is not positive this should be treated as an incomplete texture |
| 168 | // we handle that here by skipping the d3d texture creation |
| 169 | if (width > 0 && height > 0) |
| 170 | { |
| 171 | // adjust size if needed for compressed textures |
| 172 | gl::MakeValidSize(false, gl::IsCompressed(internalformat), &width, &height, &mLodOffset); |
| 173 | |
| 174 | ID3D11Device *device = mRenderer->getDevice(); |
| 175 | |
| 176 | D3D11_TEXTURE2D_DESC desc; |
| 177 | desc.Width = width; // Compressed texture size constraints? |
| 178 | desc.Height = height; |
| 179 | desc.MipLevels = levels + mLodOffset; |
| 180 | desc.ArraySize = 1; |
| 181 | desc.Format = format; |
| 182 | desc.SampleDesc.Count = 1; |
| 183 | desc.SampleDesc.Quality = 0; |
| 184 | desc.Usage = D3D11_USAGE_DEFAULT; |
| 185 | desc.BindFlags = getBindFlags(); |
| 186 | desc.CPUAccessFlags = 0; |
| 187 | desc.MiscFlags = 0; |
| 188 | |
| 189 | HRESULT result = device->CreateTexture2D(&desc, NULL, &mTexture); |
| 190 | |
| 191 | if (FAILED(result)) |
| 192 | { |
| 193 | ASSERT(result == E_OUTOFMEMORY); |
| 194 | ERR("Creating image failed."); |
| 195 | error(GL_OUT_OF_MEMORY); |
| 196 | } |
daniel@transgaming.com | b115455 | 2013-01-11 04:11:02 +0000 | [diff] [blame] | 197 | else |
| 198 | { |
| 199 | mTexture->GetDesc(&desc); |
| 200 | mMipLevels = desc.MipLevels; |
| 201 | } |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 202 | } |
| 203 | |
daniel@transgaming.com | b50d530 | 2013-01-11 04:07:29 +0000 | [diff] [blame] | 204 | initializeSRV(format, levels + mLodOffset); |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 205 | initializeRenderTarget(format, width, height); |
| 206 | } |
| 207 | |
| 208 | TextureStorage11_2D::~TextureStorage11_2D() |
| 209 | { |
| 210 | if (mTexture) |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame^] | 211 | { |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 212 | mTexture->Release(); |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame^] | 213 | mTexture = NULL; |
| 214 | } |
daniel@transgaming.com | 1e1b5e9 | 2013-01-11 04:11:27 +0000 | [diff] [blame] | 215 | |
| 216 | if (mSRV) |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame^] | 217 | { |
daniel@transgaming.com | 1e1b5e9 | 2013-01-11 04:11:27 +0000 | [diff] [blame] | 218 | mSRV->Release(); |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame^] | 219 | mSRV = NULL; |
| 220 | } |
| 221 | |
| 222 | if (mRenderTarget) |
| 223 | { |
| 224 | for (unsigned int i = 0; i < mMipLevels; i++) |
| 225 | { |
| 226 | delete mRenderTarget[i]; |
| 227 | mRenderTarget[i] = NULL; |
| 228 | } |
| 229 | |
| 230 | delete[] mRenderTarget; |
| 231 | mRenderTarget = NULL; |
| 232 | } |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | TextureStorage11_2D *TextureStorage11_2D::makeTextureStorage11_2D(TextureStorage *storage) |
| 236 | { |
| 237 | ASSERT(dynamic_cast<TextureStorage11_2D*>(storage) != NULL); |
| 238 | return static_cast<TextureStorage11_2D*>(storage); |
| 239 | } |
| 240 | |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame^] | 241 | RenderTarget *TextureStorage11_2D::getRenderTarget(int level) const |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 242 | { |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame^] | 243 | if (level < 0 || level >= static_cast<int>(mMipLevels)) |
| 244 | { |
| 245 | return NULL; |
| 246 | } |
| 247 | else |
| 248 | { |
| 249 | return mRenderTarget[level]; |
| 250 | } |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | ID3D11Texture2D *TextureStorage11_2D::getBaseTexture() const |
| 254 | { |
| 255 | return mTexture; |
| 256 | } |
| 257 | |
daniel@transgaming.com | b50d530 | 2013-01-11 04:07:29 +0000 | [diff] [blame] | 258 | ID3D11ShaderResourceView *TextureStorage11_2D::getSRV() const |
| 259 | { |
| 260 | return mSRV; |
| 261 | } |
| 262 | |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 263 | void TextureStorage11_2D::generateMipmap(int level) |
| 264 | { |
| 265 | // TODO |
| 266 | UNIMPLEMENTED(); |
| 267 | } |
| 268 | |
| 269 | void TextureStorage11_2D::initializeRenderTarget(DXGI_FORMAT format, int width, int height) |
| 270 | { |
daniel@transgaming.com | 5cdd058 | 2013-01-11 04:07:35 +0000 | [diff] [blame] | 271 | ASSERT(mRenderTarget == NULL); |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 272 | |
| 273 | if (mTexture != NULL && isRenderTarget()) |
| 274 | { |
| 275 | if (getBindFlags() & D3D11_BIND_RENDER_TARGET) |
| 276 | { |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame^] | 277 | mRenderTarget = new RenderTarget11*[mMipLevels]; |
| 278 | for (unsigned i = 0; i < mMipLevels; i++) |
| 279 | { |
| 280 | // Create render target view -- texture should already be created with |
| 281 | // BIND_RENDER_TARGET flag. |
| 282 | D3D11_RENDER_TARGET_VIEW_DESC rtvDesc; |
| 283 | rtvDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D; |
| 284 | rtvDesc.Format = format; |
| 285 | rtvDesc.Texture2D.MipSlice = i; |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 286 | |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame^] | 287 | ID3D11RenderTargetView *renderTargetView; |
| 288 | ID3D11Device *device = mRenderer->getDevice(); |
| 289 | HRESULT result = device->CreateRenderTargetView(mTexture, &rtvDesc, &renderTargetView); |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 290 | |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame^] | 291 | if (result == E_OUTOFMEMORY) |
| 292 | return; |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 293 | |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame^] | 294 | ASSERT(SUCCEEDED(result)); |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 295 | |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame^] | 296 | mRenderTarget[i] = new RenderTarget11(mRenderer, renderTargetView, std::max(width >> i, 1), std::max(height >> i, 1)); |
| 297 | } |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 298 | } |
| 299 | else if (getBindFlags() & D3D11_BIND_DEPTH_STENCIL) |
| 300 | { |
| 301 | // TODO |
| 302 | UNIMPLEMENTED(); |
| 303 | } |
| 304 | else |
| 305 | UNREACHABLE(); |
| 306 | } |
| 307 | } |
| 308 | |
daniel@transgaming.com | b50d530 | 2013-01-11 04:07:29 +0000 | [diff] [blame] | 309 | void TextureStorage11_2D::initializeSRV(DXGI_FORMAT format, int levels) |
| 310 | { |
| 311 | ASSERT(mSRV == NULL); |
| 312 | |
| 313 | if (mTexture) |
| 314 | { |
| 315 | D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc; |
| 316 | srvDesc.Format = format; |
| 317 | srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; |
| 318 | srvDesc.Texture2D.MostDetailedMip = 0; |
daniel@transgaming.com | a0b868a | 2013-01-11 04:08:27 +0000 | [diff] [blame] | 319 | srvDesc.Texture2D.MipLevels = (levels == 0 ? -1 : levels); |
daniel@transgaming.com | b50d530 | 2013-01-11 04:07:29 +0000 | [diff] [blame] | 320 | |
| 321 | ID3D11Device *device = mRenderer->getDevice(); |
| 322 | HRESULT result = device->CreateShaderResourceView(mTexture, &srvDesc, &mSRV); |
| 323 | |
| 324 | if (result == E_OUTOFMEMORY) |
| 325 | { |
| 326 | return error(GL_OUT_OF_MEMORY); |
| 327 | } |
| 328 | |
| 329 | ASSERT(SUCCEEDED(result)); |
| 330 | } |
| 331 | } |
| 332 | |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 333 | TextureStorage11_Cube::TextureStorage11_Cube(Renderer *renderer, int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size) |
| 334 | : TextureStorage11(renderer, GetTextureBindFlags(gl_d3d11::ConvertTextureFormat(internalformat), usage, forceRenderable)) |
| 335 | { |
| 336 | mTexture = NULL; |
daniel@transgaming.com | b50d530 | 2013-01-11 04:07:29 +0000 | [diff] [blame] | 337 | mSRV = NULL; |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame^] | 338 | |
| 339 | for (unsigned int i = 0; i < 6; ++i) |
daniel@transgaming.com | 5cdd058 | 2013-01-11 04:07:35 +0000 | [diff] [blame] | 340 | { |
| 341 | mRenderTarget[i] = NULL; |
| 342 | } |
| 343 | |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 344 | DXGI_FORMAT format = gl_d3d11::ConvertTextureFormat(internalformat); |
| 345 | // if the size is not positive this should be treated as an incomplete texture |
| 346 | // we handle that here by skipping the d3d texture creation |
| 347 | if (size > 0) |
| 348 | { |
| 349 | // adjust size if needed for compressed textures |
| 350 | int height = size; |
| 351 | gl::MakeValidSize(false, gl::IsCompressed(internalformat), &size, &height, &mLodOffset); |
| 352 | |
| 353 | ID3D11Device *device = mRenderer->getDevice(); |
| 354 | |
| 355 | D3D11_TEXTURE2D_DESC desc; |
| 356 | desc.Width = size; |
| 357 | desc.Height = size; |
| 358 | desc.MipLevels = levels + mLodOffset; |
| 359 | desc.ArraySize = 6; |
| 360 | desc.Format = format; |
| 361 | desc.SampleDesc.Count = 1; |
| 362 | desc.SampleDesc.Quality = 0; |
| 363 | desc.Usage = D3D11_USAGE_DEFAULT; |
| 364 | desc.BindFlags = getBindFlags(); |
| 365 | desc.CPUAccessFlags = 0; |
| 366 | desc.MiscFlags = D3D11_RESOURCE_MISC_TEXTURECUBE; |
| 367 | |
| 368 | HRESULT result = device->CreateTexture2D(&desc, NULL, &mTexture); |
| 369 | |
| 370 | if (FAILED(result)) |
| 371 | { |
| 372 | ASSERT(result == E_OUTOFMEMORY); |
| 373 | ERR("Creating image failed."); |
| 374 | error(GL_OUT_OF_MEMORY); |
| 375 | } |
daniel@transgaming.com | b115455 | 2013-01-11 04:11:02 +0000 | [diff] [blame] | 376 | else |
| 377 | { |
| 378 | mTexture->GetDesc(&desc); |
| 379 | mMipLevels = desc.MipLevels; |
| 380 | } |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 381 | } |
| 382 | |
daniel@transgaming.com | b50d530 | 2013-01-11 04:07:29 +0000 | [diff] [blame] | 383 | initializeSRV(format, levels + mLodOffset); |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 384 | initializeRenderTarget(format, size); |
| 385 | } |
| 386 | |
| 387 | TextureStorage11_Cube::~TextureStorage11_Cube() |
| 388 | { |
| 389 | if (mTexture) |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame^] | 390 | { |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 391 | mTexture->Release(); |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame^] | 392 | mTexture = NULL; |
| 393 | } |
daniel@transgaming.com | 1e1b5e9 | 2013-01-11 04:11:27 +0000 | [diff] [blame] | 394 | |
| 395 | if (mSRV) |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame^] | 396 | { |
daniel@transgaming.com | 1e1b5e9 | 2013-01-11 04:11:27 +0000 | [diff] [blame] | 397 | mSRV->Release(); |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame^] | 398 | mSRV = NULL; |
| 399 | } |
| 400 | |
| 401 | for (unsigned int i = 0; i < 6; i++) |
| 402 | { |
| 403 | if (mRenderTarget[i]) |
| 404 | { |
| 405 | for (unsigned int j = 0; j < mMipLevels; j++) |
| 406 | { |
| 407 | delete mRenderTarget[i][j]; |
| 408 | mRenderTarget[i][j] = NULL; |
| 409 | } |
| 410 | |
| 411 | delete[] mRenderTarget[i]; |
| 412 | mRenderTarget[i] = NULL; |
| 413 | } |
| 414 | } |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | TextureStorage11_Cube *TextureStorage11_Cube::makeTextureStorage11_Cube(TextureStorage *storage) |
| 418 | { |
| 419 | ASSERT(dynamic_cast<TextureStorage11_Cube*>(storage) != NULL); |
| 420 | return static_cast<TextureStorage11_Cube*>(storage); |
| 421 | } |
| 422 | |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame^] | 423 | RenderTarget *TextureStorage11_Cube::getRenderTarget(GLenum faceTarget, int level) const |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 424 | { |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame^] | 425 | if (level < 0 || level >= static_cast<int>(mMipLevels)) |
| 426 | { |
| 427 | return NULL; |
| 428 | } |
| 429 | else |
| 430 | { |
| 431 | return mRenderTarget[gl::TextureCubeMap::faceIndex(faceTarget)][level]; |
| 432 | } |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | ID3D11Texture2D *TextureStorage11_Cube::getBaseTexture() const |
| 436 | { |
| 437 | return mTexture; |
| 438 | } |
| 439 | |
daniel@transgaming.com | b50d530 | 2013-01-11 04:07:29 +0000 | [diff] [blame] | 440 | ID3D11ShaderResourceView *TextureStorage11_Cube::getSRV() const |
| 441 | { |
| 442 | return mSRV; |
| 443 | } |
| 444 | |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 445 | void TextureStorage11_Cube::generateMipmap(int face, int level) |
| 446 | { |
| 447 | // TODO |
| 448 | UNIMPLEMENTED(); |
| 449 | } |
| 450 | |
| 451 | void TextureStorage11_Cube::initializeRenderTarget(DXGI_FORMAT format, int size) |
| 452 | { |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 453 | if (mTexture != NULL && isRenderTarget()) |
| 454 | { |
| 455 | if (getBindFlags() & D3D11_BIND_RENDER_TARGET) |
| 456 | { |
| 457 | // Create render target view -- texture should already be created with |
| 458 | // BIND_RENDER_TARGET flag. |
| 459 | |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame^] | 460 | for (unsigned int i = 0; i < 6; ++i) |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 461 | { |
daniel@transgaming.com | 5cdd058 | 2013-01-11 04:07:35 +0000 | [diff] [blame] | 462 | ASSERT(mRenderTarget[i] == NULL); |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame^] | 463 | mRenderTarget[i] = new RenderTarget11*[mMipLevels]; |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 464 | |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame^] | 465 | for (unsigned int j = 0; j < mMipLevels; j++) |
| 466 | { |
| 467 | D3D11_RENDER_TARGET_VIEW_DESC rtvDesc; |
| 468 | rtvDesc.Format = format; |
| 469 | rtvDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DARRAY; |
| 470 | rtvDesc.Texture2DArray.MipSlice = j; |
| 471 | rtvDesc.Texture2DArray.FirstArraySlice = i; |
| 472 | rtvDesc.Texture2DArray.ArraySize = 1; |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 473 | |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame^] | 474 | ID3D11RenderTargetView *renderTargetView; |
| 475 | ID3D11Device *device = mRenderer->getDevice(); |
| 476 | HRESULT result = device->CreateRenderTargetView(mTexture, &rtvDesc, &renderTargetView); |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 477 | |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame^] | 478 | if (result == E_OUTOFMEMORY) |
| 479 | return; |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 480 | |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame^] | 481 | ASSERT(SUCCEEDED(result)); |
| 482 | |
| 483 | int mipSize = size >> j; |
| 484 | mRenderTarget[i][j] = new RenderTarget11(mRenderer, renderTargetView, mipSize, mipSize); |
| 485 | } |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 486 | } |
| 487 | } |
| 488 | else if (getBindFlags() & D3D11_BIND_DEPTH_STENCIL) |
| 489 | { |
| 490 | // TODO |
| 491 | UNIMPLEMENTED(); |
| 492 | } |
| 493 | else |
| 494 | UNREACHABLE(); |
| 495 | } |
| 496 | } |
| 497 | |
daniel@transgaming.com | b50d530 | 2013-01-11 04:07:29 +0000 | [diff] [blame] | 498 | void TextureStorage11_Cube::initializeSRV(DXGI_FORMAT format, int levels) |
| 499 | { |
| 500 | ASSERT(mSRV == NULL); |
| 501 | |
| 502 | if (mTexture) |
| 503 | { |
| 504 | D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc; |
| 505 | srvDesc.Format = format; |
| 506 | srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE; |
daniel@transgaming.com | a0b868a | 2013-01-11 04:08:27 +0000 | [diff] [blame] | 507 | srvDesc.TextureCube.MipLevels = (levels == 0 ? -1 : levels); |
daniel@transgaming.com | b50d530 | 2013-01-11 04:07:29 +0000 | [diff] [blame] | 508 | srvDesc.TextureCube.MostDetailedMip = 0; |
| 509 | |
| 510 | ID3D11Device *device = mRenderer->getDevice(); |
| 511 | HRESULT result = device->CreateShaderResourceView(mTexture, &srvDesc, &mSRV); |
| 512 | |
| 513 | if (result == E_OUTOFMEMORY) |
| 514 | { |
| 515 | return error(GL_OUT_OF_MEMORY); |
| 516 | } |
| 517 | |
| 518 | ASSERT(SUCCEEDED(result)); |
| 519 | } |
| 520 | } |
| 521 | |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 522 | } |