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 | |
shannon.woods@transgaming.com | 183408d | 2013-01-25 21:50:07 +0000 | [diff] [blame^] | 268 | D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc; |
| 269 | srvDesc.Format = mTextureFormat; |
| 270 | srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; |
| 271 | srvDesc.Texture2D.MostDetailedMip = level; |
| 272 | srvDesc.Texture2D.MipLevels = 1; |
| 273 | |
| 274 | ID3D11ShaderResourceView *srv; |
| 275 | result = device->CreateShaderResourceView(mTexture, &srvDesc, &srv); |
| 276 | |
| 277 | if (result == E_OUTOFMEMORY) |
| 278 | { |
| 279 | rtv->Release(); |
| 280 | return error(GL_OUT_OF_MEMORY, static_cast<RenderTarget*>(NULL)); |
| 281 | } |
| 282 | ASSERT(SUCCEEDED(result)); |
| 283 | |
| 284 | mRenderTarget[level] = new RenderTarget11(mRenderer, rtv, srv, |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame] | 285 | std::max(mTextureWidth >> level, 1U), |
| 286 | std::max(mTextureHeight >> level, 1U)); |
| 287 | } |
| 288 | |
| 289 | return mRenderTarget[level]; |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame] | 290 | } |
| 291 | else |
| 292 | { |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame] | 293 | return NULL; |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame] | 294 | } |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | ID3D11Texture2D *TextureStorage11_2D::getBaseTexture() const |
| 298 | { |
| 299 | return mTexture; |
| 300 | } |
| 301 | |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame] | 302 | ID3D11ShaderResourceView *TextureStorage11_2D::getSRV() |
daniel@transgaming.com | b50d530 | 2013-01-11 04:07:29 +0000 | [diff] [blame] | 303 | { |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame] | 304 | if (!mSRV) |
| 305 | { |
| 306 | ID3D11Device *device = mRenderer->getDevice(); |
| 307 | |
| 308 | D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc; |
| 309 | srvDesc.Format = mTextureFormat; |
| 310 | srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; |
| 311 | srvDesc.Texture2D.MipLevels = (mMipLevels == 0 ? -1 : mMipLevels); |
| 312 | srvDesc.Texture2D.MostDetailedMip = 0; |
| 313 | |
| 314 | HRESULT result = device->CreateShaderResourceView(mTexture, &srvDesc, &mSRV); |
| 315 | |
| 316 | if (result == E_OUTOFMEMORY) |
| 317 | { |
| 318 | return error(GL_OUT_OF_MEMORY, static_cast<ID3D11ShaderResourceView*>(NULL)); |
| 319 | } |
| 320 | ASSERT(SUCCEEDED(result)); |
| 321 | } |
| 322 | |
daniel@transgaming.com | b50d530 | 2013-01-11 04:07:29 +0000 | [diff] [blame] | 323 | return mSRV; |
| 324 | } |
| 325 | |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 326 | void TextureStorage11_2D::generateMipmap(int level) |
| 327 | { |
| 328 | // TODO |
| 329 | UNIMPLEMENTED(); |
| 330 | } |
| 331 | |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 332 | TextureStorage11_Cube::TextureStorage11_Cube(Renderer *renderer, int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size) |
| 333 | : TextureStorage11(renderer, GetTextureBindFlags(gl_d3d11::ConvertTextureFormat(internalformat), usage, forceRenderable)) |
| 334 | { |
| 335 | mTexture = NULL; |
daniel@transgaming.com | b50d530 | 2013-01-11 04:07:29 +0000 | [diff] [blame] | 336 | mSRV = NULL; |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame] | 337 | mTextureWidth = 0; |
| 338 | mTextureHeight = 0; |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame] | 339 | |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame] | 340 | for (unsigned int i = 0; i < 6; i++) |
daniel@transgaming.com | 5cdd058 | 2013-01-11 04:07:35 +0000 | [diff] [blame] | 341 | { |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame] | 342 | for (unsigned int j = 0; j < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; j++) |
| 343 | { |
| 344 | mRenderTarget[i][j] = NULL; |
| 345 | } |
daniel@transgaming.com | 5cdd058 | 2013-01-11 04:07:35 +0000 | [diff] [blame] | 346 | } |
| 347 | |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 348 | DXGI_FORMAT format = gl_d3d11::ConvertTextureFormat(internalformat); |
| 349 | // if the size is not positive this should be treated as an incomplete texture |
| 350 | // we handle that here by skipping the d3d texture creation |
| 351 | if (size > 0) |
| 352 | { |
| 353 | // adjust size if needed for compressed textures |
| 354 | int height = size; |
| 355 | gl::MakeValidSize(false, gl::IsCompressed(internalformat), &size, &height, &mLodOffset); |
| 356 | |
| 357 | ID3D11Device *device = mRenderer->getDevice(); |
| 358 | |
| 359 | D3D11_TEXTURE2D_DESC desc; |
| 360 | desc.Width = size; |
| 361 | desc.Height = size; |
| 362 | desc.MipLevels = levels + mLodOffset; |
| 363 | desc.ArraySize = 6; |
| 364 | desc.Format = format; |
| 365 | desc.SampleDesc.Count = 1; |
| 366 | desc.SampleDesc.Quality = 0; |
| 367 | desc.Usage = D3D11_USAGE_DEFAULT; |
| 368 | desc.BindFlags = getBindFlags(); |
| 369 | desc.CPUAccessFlags = 0; |
| 370 | desc.MiscFlags = D3D11_RESOURCE_MISC_TEXTURECUBE; |
| 371 | |
| 372 | HRESULT result = device->CreateTexture2D(&desc, NULL, &mTexture); |
| 373 | |
| 374 | if (FAILED(result)) |
| 375 | { |
| 376 | ASSERT(result == E_OUTOFMEMORY); |
| 377 | ERR("Creating image failed."); |
| 378 | error(GL_OUT_OF_MEMORY); |
| 379 | } |
daniel@transgaming.com | b115455 | 2013-01-11 04:11:02 +0000 | [diff] [blame] | 380 | else |
| 381 | { |
| 382 | mTexture->GetDesc(&desc); |
| 383 | mMipLevels = desc.MipLevels; |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame] | 384 | mTextureFormat = desc.Format; |
| 385 | mTextureWidth = desc.Width; |
| 386 | mTextureHeight = desc.Height; |
daniel@transgaming.com | b115455 | 2013-01-11 04:11:02 +0000 | [diff] [blame] | 387 | } |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 388 | } |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | TextureStorage11_Cube::~TextureStorage11_Cube() |
| 392 | { |
| 393 | if (mTexture) |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame] | 394 | { |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 395 | mTexture->Release(); |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame] | 396 | mTexture = NULL; |
| 397 | } |
daniel@transgaming.com | 1e1b5e9 | 2013-01-11 04:11:27 +0000 | [diff] [blame] | 398 | |
| 399 | if (mSRV) |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame] | 400 | { |
daniel@transgaming.com | 1e1b5e9 | 2013-01-11 04:11:27 +0000 | [diff] [blame] | 401 | mSRV->Release(); |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame] | 402 | mSRV = NULL; |
| 403 | } |
| 404 | |
| 405 | for (unsigned int i = 0; i < 6; i++) |
| 406 | { |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame] | 407 | 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] | 408 | { |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame] | 409 | delete mRenderTarget[i][j]; |
| 410 | mRenderTarget[i][j] = NULL; |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame] | 411 | } |
| 412 | } |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | TextureStorage11_Cube *TextureStorage11_Cube::makeTextureStorage11_Cube(TextureStorage *storage) |
| 416 | { |
| 417 | ASSERT(dynamic_cast<TextureStorage11_Cube*>(storage) != NULL); |
| 418 | return static_cast<TextureStorage11_Cube*>(storage); |
| 419 | } |
| 420 | |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame] | 421 | RenderTarget *TextureStorage11_Cube::getRenderTarget(GLenum faceTarget, int level) |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 422 | { |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame] | 423 | unsigned int faceIdx = gl::TextureCubeMap::faceIndex(faceTarget); |
| 424 | if (level >= 0 && level < static_cast<int>(mMipLevels)) |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame] | 425 | { |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame] | 426 | if (!mRenderTarget[faceIdx][level]) |
| 427 | { |
| 428 | ID3D11Device *device = mRenderer->getDevice(); |
| 429 | HRESULT result; |
| 430 | |
| 431 | if (getBindFlags() & D3D11_BIND_RENDER_TARGET) |
| 432 | { |
| 433 | D3D11_RENDER_TARGET_VIEW_DESC rtvDesc; |
| 434 | rtvDesc.Format = mTextureFormat; |
| 435 | rtvDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DARRAY; |
| 436 | rtvDesc.Texture2DArray.MipSlice = level; |
| 437 | rtvDesc.Texture2DArray.FirstArraySlice = faceIdx; |
| 438 | rtvDesc.Texture2DArray.ArraySize = 1; |
| 439 | |
| 440 | ID3D11RenderTargetView *rtv; |
| 441 | result = device->CreateRenderTargetView(mTexture, &rtvDesc, &rtv); |
| 442 | |
| 443 | if (result == E_OUTOFMEMORY) |
| 444 | { |
| 445 | return error(GL_OUT_OF_MEMORY, static_cast<RenderTarget*>(NULL)); |
| 446 | } |
| 447 | ASSERT(SUCCEEDED(result)); |
| 448 | |
shannon.woods@transgaming.com | 183408d | 2013-01-25 21:50:07 +0000 | [diff] [blame^] | 449 | D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc; |
| 450 | srvDesc.Format = mTextureFormat; |
| 451 | srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DARRAY; |
| 452 | srvDesc.Texture2DArray.MostDetailedMip = level; |
| 453 | srvDesc.Texture2DArray.MipLevels = 1; |
| 454 | srvDesc.Texture2DArray.FirstArraySlice = faceIdx; |
| 455 | srvDesc.Texture2DArray.ArraySize = 1; |
| 456 | |
| 457 | ID3D11ShaderResourceView *srv; |
| 458 | result = device->CreateShaderResourceView(mTexture, &srvDesc, &srv); |
| 459 | |
| 460 | if (result == E_OUTOFMEMORY) |
| 461 | { |
| 462 | rtv->Release(); |
| 463 | return error(GL_OUT_OF_MEMORY, static_cast<RenderTarget*>(NULL)); |
| 464 | } |
| 465 | ASSERT(SUCCEEDED(result)); |
| 466 | |
| 467 | mRenderTarget[faceIdx][level] = new RenderTarget11(mRenderer, rtv, srv, |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame] | 468 | std::max(mTextureWidth >> level, 1U), |
| 469 | std::max(mTextureHeight >> level, 1U)); |
| 470 | } |
| 471 | else if (getBindFlags() & D3D11_BIND_DEPTH_STENCIL) |
| 472 | { |
| 473 | // TODO |
| 474 | UNIMPLEMENTED(); |
| 475 | } |
| 476 | else UNREACHABLE(); |
| 477 | } |
| 478 | |
| 479 | return mRenderTarget[faceIdx][level]; |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame] | 480 | } |
| 481 | else |
| 482 | { |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame] | 483 | return NULL; |
shannon.woods@transgaming.com | 4b47235 | 2013-01-25 21:14:56 +0000 | [diff] [blame] | 484 | } |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | ID3D11Texture2D *TextureStorage11_Cube::getBaseTexture() const |
| 488 | { |
| 489 | return mTexture; |
| 490 | } |
| 491 | |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame] | 492 | ID3D11ShaderResourceView *TextureStorage11_Cube::getSRV() |
daniel@transgaming.com | b50d530 | 2013-01-11 04:07:29 +0000 | [diff] [blame] | 493 | { |
shannon.woods@transgaming.com | 858d32f | 2013-01-25 21:49:59 +0000 | [diff] [blame] | 494 | if (!mSRV) |
| 495 | { |
| 496 | ID3D11Device *device = mRenderer->getDevice(); |
| 497 | |
| 498 | D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc; |
| 499 | srvDesc.Format = mTextureFormat; |
| 500 | srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE; |
| 501 | srvDesc.TextureCube.MipLevels = (mMipLevels == 0 ? -1 : mMipLevels); |
| 502 | srvDesc.TextureCube.MostDetailedMip = 0; |
| 503 | |
| 504 | HRESULT result = device->CreateShaderResourceView(mTexture, &srvDesc, &mSRV); |
| 505 | |
| 506 | if (result == E_OUTOFMEMORY) |
| 507 | { |
| 508 | return error(GL_OUT_OF_MEMORY, static_cast<ID3D11ShaderResourceView*>(NULL)); |
| 509 | } |
| 510 | ASSERT(SUCCEEDED(result)); |
| 511 | } |
| 512 | |
daniel@transgaming.com | b50d530 | 2013-01-11 04:07:29 +0000 | [diff] [blame] | 513 | return mSRV; |
| 514 | } |
| 515 | |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 516 | void TextureStorage11_Cube::generateMipmap(int face, int level) |
| 517 | { |
| 518 | // TODO |
| 519 | UNIMPLEMENTED(); |
| 520 | } |
| 521 | |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 522 | } |