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), |
| 24 | mLodOffset(0) |
| 25 | { |
| 26 | mRenderer = Renderer11::makeRenderer11(renderer); |
| 27 | } |
| 28 | |
| 29 | TextureStorage11::~TextureStorage11() |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | TextureStorage11 *TextureStorage11::makeTextureStorage11(TextureStorage *storage) |
| 34 | { |
| 35 | ASSERT(dynamic_cast<TextureStorage11*>(storage) != NULL); |
| 36 | return static_cast<TextureStorage11*>(storage); |
| 37 | } |
| 38 | |
| 39 | DWORD TextureStorage11::GetTextureBindFlags(DXGI_FORMAT format, GLenum glusage, bool forceRenderable) |
| 40 | { |
| 41 | UINT bindFlags = D3D11_BIND_SHADER_RESOURCE; |
| 42 | |
| 43 | if (format == DXGI_FORMAT_D24_UNORM_S8_UINT) |
| 44 | { |
| 45 | bindFlags |= D3D11_BIND_DEPTH_STENCIL; |
| 46 | } |
| 47 | else if(forceRenderable || (TextureStorage11::IsTextureFormatRenderable(format) && (glusage == GL_FRAMEBUFFER_ATTACHMENT_ANGLE))) |
| 48 | { |
| 49 | bindFlags |= D3D11_BIND_RENDER_TARGET; |
| 50 | } |
| 51 | return bindFlags; |
| 52 | } |
| 53 | |
| 54 | bool TextureStorage11::IsTextureFormatRenderable(DXGI_FORMAT format) |
| 55 | { |
| 56 | switch(format) |
| 57 | { |
| 58 | case DXGI_FORMAT_R8G8B8A8_UNORM: |
| 59 | case DXGI_FORMAT_A8_UNORM: |
| 60 | case DXGI_FORMAT_R32G32B32A32_FLOAT: |
| 61 | case DXGI_FORMAT_R32G32B32_FLOAT: |
| 62 | case DXGI_FORMAT_R16G16B16A16_FLOAT: |
| 63 | case DXGI_FORMAT_B8G8R8A8_UNORM: |
| 64 | case DXGI_FORMAT_R8_UNORM: |
| 65 | case DXGI_FORMAT_R8G8_UNORM: |
| 66 | case DXGI_FORMAT_R16_FLOAT: |
| 67 | case DXGI_FORMAT_R16G16_FLOAT: |
| 68 | return true; |
| 69 | case DXGI_FORMAT_BC1_UNORM: |
| 70 | case DXGI_FORMAT_BC2_UNORM: |
| 71 | case DXGI_FORMAT_BC3_UNORM: |
| 72 | return false; |
| 73 | default: |
| 74 | UNREACHABLE(); |
| 75 | return false; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | UINT TextureStorage11::getBindFlags() const |
| 80 | { |
| 81 | return mBindFlags; |
| 82 | } |
| 83 | |
| 84 | int TextureStorage11::getLodOffset() const |
| 85 | { |
| 86 | return mLodOffset; |
| 87 | } |
| 88 | |
| 89 | bool TextureStorage11::isRenderTarget() const |
| 90 | { |
| 91 | return (mBindFlags & (D3D11_BIND_RENDER_TARGET | D3D11_BIND_DEPTH_STENCIL)) != 0; |
| 92 | } |
| 93 | |
| 94 | bool TextureStorage11::isManaged() const |
| 95 | { |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | // TODO - We should store level count internally at creation time instead |
| 100 | // of making driver calls to determine it each time levelCount() is called. |
| 101 | int TextureStorage11::levelCount() |
| 102 | { |
| 103 | int levels = 0; |
| 104 | if (getBaseTexture()) |
| 105 | { |
| 106 | D3D11_TEXTURE2D_DESC desc; |
| 107 | getBaseTexture()->GetDesc(&desc); |
| 108 | levels = desc.MipLevels - getLodOffset(); |
| 109 | } |
| 110 | return levels; |
| 111 | } |
| 112 | |
daniel@transgaming.com | 9a2f54d | 2013-01-11 04:06:36 +0000 | [diff] [blame^] | 113 | // TODO - Once we're storing level count internally, we should no longer |
| 114 | // need to look up the texture description to determine the number of mip levels. |
| 115 | UINT TextureStorage11::getSubresourceIndex(int level, int faceIndex) |
| 116 | { |
| 117 | UINT index = 0; |
| 118 | if (getBaseTexture()) |
| 119 | { |
| 120 | D3D11_TEXTURE2D_DESC desc; |
| 121 | getBaseTexture()->GetDesc(&desc); |
| 122 | index = D3D11CalcSubresource(level, faceIndex, desc.MipLevels); |
| 123 | } |
| 124 | return index; |
| 125 | } |
| 126 | |
| 127 | bool TextureStorage11::updateSubresourceLevel(ID3D11Texture2D *srcTexture, int level, int face, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height) |
| 128 | { |
| 129 | if (srcTexture) |
| 130 | { |
| 131 | D3D11_BOX srcBox; |
| 132 | srcBox.left = xoffset; |
| 133 | srcBox.top = yoffset; |
| 134 | srcBox.right = xoffset + width; |
| 135 | srcBox.bottom = yoffset + height; |
| 136 | srcBox.front = 0; |
| 137 | srcBox.back = 1; |
| 138 | |
| 139 | ID3D11DeviceContext *context = mRenderer->getDeviceContext(); |
| 140 | |
| 141 | ASSERT(getBaseTexture()); |
| 142 | context->CopySubresourceRegion(getBaseTexture(), getSubresourceIndex(level, face), xoffset, yoffset, 0, srcTexture, 0, &srcBox); |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | return false; |
| 147 | } |
| 148 | |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 149 | TextureStorage11_2D::TextureStorage11_2D(Renderer *renderer, SwapChain11 *swapchain) |
| 150 | : TextureStorage11(renderer, D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE) |
| 151 | { |
| 152 | ID3D11Texture2D *surfaceTexture = swapchain->getOffscreenTexture(); |
| 153 | mTexture = surfaceTexture; |
| 154 | |
| 155 | D3D11_TEXTURE2D_DESC desc; |
| 156 | surfaceTexture->GetDesc(&desc); |
| 157 | |
| 158 | initializeRenderTarget(desc.Format, desc.Width, desc.Height); |
| 159 | } |
| 160 | |
| 161 | TextureStorage11_2D::TextureStorage11_2D(Renderer *renderer, int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height) |
| 162 | : TextureStorage11(renderer, GetTextureBindFlags(gl_d3d11::ConvertTextureFormat(internalformat), usage, forceRenderable)) |
| 163 | { |
| 164 | mTexture = NULL; |
| 165 | DXGI_FORMAT format = gl_d3d11::ConvertTextureFormat(internalformat); |
| 166 | // if the width or height is not positive this should be treated as an incomplete texture |
| 167 | // we handle that here by skipping the d3d texture creation |
| 168 | if (width > 0 && height > 0) |
| 169 | { |
| 170 | // adjust size if needed for compressed textures |
| 171 | gl::MakeValidSize(false, gl::IsCompressed(internalformat), &width, &height, &mLodOffset); |
| 172 | |
| 173 | ID3D11Device *device = mRenderer->getDevice(); |
| 174 | |
| 175 | D3D11_TEXTURE2D_DESC desc; |
| 176 | desc.Width = width; // Compressed texture size constraints? |
| 177 | desc.Height = height; |
| 178 | desc.MipLevels = levels + mLodOffset; |
| 179 | desc.ArraySize = 1; |
| 180 | desc.Format = format; |
| 181 | desc.SampleDesc.Count = 1; |
| 182 | desc.SampleDesc.Quality = 0; |
| 183 | desc.Usage = D3D11_USAGE_DEFAULT; |
| 184 | desc.BindFlags = getBindFlags(); |
| 185 | desc.CPUAccessFlags = 0; |
| 186 | desc.MiscFlags = 0; |
| 187 | |
| 188 | HRESULT result = device->CreateTexture2D(&desc, NULL, &mTexture); |
| 189 | |
| 190 | if (FAILED(result)) |
| 191 | { |
| 192 | ASSERT(result == E_OUTOFMEMORY); |
| 193 | ERR("Creating image failed."); |
| 194 | error(GL_OUT_OF_MEMORY); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | initializeRenderTarget(format, width, height); |
| 199 | } |
| 200 | |
| 201 | TextureStorage11_2D::~TextureStorage11_2D() |
| 202 | { |
| 203 | if (mTexture) |
| 204 | mTexture->Release(); |
| 205 | } |
| 206 | |
| 207 | TextureStorage11_2D *TextureStorage11_2D::makeTextureStorage11_2D(TextureStorage *storage) |
| 208 | { |
| 209 | ASSERT(dynamic_cast<TextureStorage11_2D*>(storage) != NULL); |
| 210 | return static_cast<TextureStorage11_2D*>(storage); |
| 211 | } |
| 212 | |
| 213 | RenderTarget *TextureStorage11_2D::getRenderTarget() const |
| 214 | { |
| 215 | return mRenderTarget; |
| 216 | } |
| 217 | |
| 218 | ID3D11Texture2D *TextureStorage11_2D::getBaseTexture() const |
| 219 | { |
| 220 | return mTexture; |
| 221 | } |
| 222 | |
| 223 | void TextureStorage11_2D::generateMipmap(int level) |
| 224 | { |
| 225 | // TODO |
| 226 | UNIMPLEMENTED(); |
| 227 | } |
| 228 | |
| 229 | void TextureStorage11_2D::initializeRenderTarget(DXGI_FORMAT format, int width, int height) |
| 230 | { |
| 231 | mRenderTarget = NULL; |
| 232 | |
| 233 | if (mTexture != NULL && isRenderTarget()) |
| 234 | { |
| 235 | if (getBindFlags() & D3D11_BIND_RENDER_TARGET) |
| 236 | { |
| 237 | // Create render target view -- texture should already be created with |
| 238 | // BIND_RENDER_TARGET flag. |
| 239 | D3D11_RENDER_TARGET_VIEW_DESC rtvDesc; |
| 240 | rtvDesc.Format = format; |
| 241 | rtvDesc.Texture2D.MipSlice = 0; |
| 242 | |
| 243 | ID3D11RenderTargetView *renderTargetView; |
| 244 | ID3D11Device *device = mRenderer->getDevice(); |
| 245 | HRESULT result = device->CreateRenderTargetView(mTexture, &rtvDesc, &renderTargetView); |
| 246 | |
| 247 | if (result == E_OUTOFMEMORY) |
| 248 | return; |
| 249 | |
| 250 | ASSERT(SUCCESS(result)); |
| 251 | |
| 252 | mRenderTarget = new RenderTarget11(mRenderer, renderTargetView, width, height); |
| 253 | } |
| 254 | else if (getBindFlags() & D3D11_BIND_DEPTH_STENCIL) |
| 255 | { |
| 256 | // TODO |
| 257 | UNIMPLEMENTED(); |
| 258 | } |
| 259 | else |
| 260 | UNREACHABLE(); |
| 261 | } |
| 262 | } |
| 263 | |
daniel@transgaming.com | b2151e5 | 2013-01-11 04:06:24 +0000 | [diff] [blame] | 264 | TextureStorage11_Cube::TextureStorage11_Cube(Renderer *renderer, int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size) |
| 265 | : TextureStorage11(renderer, GetTextureBindFlags(gl_d3d11::ConvertTextureFormat(internalformat), usage, forceRenderable)) |
| 266 | { |
| 267 | mTexture = NULL; |
| 268 | DXGI_FORMAT format = gl_d3d11::ConvertTextureFormat(internalformat); |
| 269 | // if the size is not positive this should be treated as an incomplete texture |
| 270 | // we handle that here by skipping the d3d texture creation |
| 271 | if (size > 0) |
| 272 | { |
| 273 | // adjust size if needed for compressed textures |
| 274 | int height = size; |
| 275 | gl::MakeValidSize(false, gl::IsCompressed(internalformat), &size, &height, &mLodOffset); |
| 276 | |
| 277 | ID3D11Device *device = mRenderer->getDevice(); |
| 278 | |
| 279 | D3D11_TEXTURE2D_DESC desc; |
| 280 | desc.Width = size; |
| 281 | desc.Height = size; |
| 282 | desc.MipLevels = levels + mLodOffset; |
| 283 | desc.ArraySize = 6; |
| 284 | desc.Format = format; |
| 285 | desc.SampleDesc.Count = 1; |
| 286 | desc.SampleDesc.Quality = 0; |
| 287 | desc.Usage = D3D11_USAGE_DEFAULT; |
| 288 | desc.BindFlags = getBindFlags(); |
| 289 | desc.CPUAccessFlags = 0; |
| 290 | desc.MiscFlags = D3D11_RESOURCE_MISC_TEXTURECUBE; |
| 291 | |
| 292 | HRESULT result = device->CreateTexture2D(&desc, NULL, &mTexture); |
| 293 | |
| 294 | if (FAILED(result)) |
| 295 | { |
| 296 | ASSERT(result == E_OUTOFMEMORY); |
| 297 | ERR("Creating image failed."); |
| 298 | error(GL_OUT_OF_MEMORY); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | initializeRenderTarget(format, size); |
| 303 | } |
| 304 | |
| 305 | TextureStorage11_Cube::~TextureStorage11_Cube() |
| 306 | { |
| 307 | if (mTexture) |
| 308 | mTexture->Release(); |
| 309 | } |
| 310 | |
| 311 | TextureStorage11_Cube *TextureStorage11_Cube::makeTextureStorage11_Cube(TextureStorage *storage) |
| 312 | { |
| 313 | ASSERT(dynamic_cast<TextureStorage11_Cube*>(storage) != NULL); |
| 314 | return static_cast<TextureStorage11_Cube*>(storage); |
| 315 | } |
| 316 | |
| 317 | RenderTarget *TextureStorage11_Cube::getRenderTarget(GLenum faceTarget) const |
| 318 | { |
| 319 | return mRenderTarget[gl::TextureCubeMap::faceIndex(faceTarget)]; |
| 320 | } |
| 321 | |
| 322 | ID3D11Texture2D *TextureStorage11_Cube::getBaseTexture() const |
| 323 | { |
| 324 | return mTexture; |
| 325 | } |
| 326 | |
| 327 | void TextureStorage11_Cube::generateMipmap(int face, int level) |
| 328 | { |
| 329 | // TODO |
| 330 | UNIMPLEMENTED(); |
| 331 | } |
| 332 | |
| 333 | void TextureStorage11_Cube::initializeRenderTarget(DXGI_FORMAT format, int size) |
| 334 | { |
| 335 | for (int i = 0; i < 6; ++i) |
| 336 | { |
| 337 | mRenderTarget[i] = NULL; |
| 338 | } |
| 339 | |
| 340 | if (mTexture != NULL && isRenderTarget()) |
| 341 | { |
| 342 | if (getBindFlags() & D3D11_BIND_RENDER_TARGET) |
| 343 | { |
| 344 | // Create render target view -- texture should already be created with |
| 345 | // BIND_RENDER_TARGET flag. |
| 346 | |
| 347 | for (int i = 0; i < 6; ++i) |
| 348 | { |
| 349 | D3D11_RENDER_TARGET_VIEW_DESC rtvDesc; |
| 350 | rtvDesc.Format = format; |
| 351 | rtvDesc.Texture2DArray.MipSlice = 0; |
| 352 | rtvDesc.Texture2DArray.FirstArraySlice = i; |
| 353 | rtvDesc.Texture2DArray.ArraySize = 1; |
| 354 | |
| 355 | ID3D11RenderTargetView *renderTargetView; |
| 356 | ID3D11Device *device = mRenderer->getDevice(); |
| 357 | HRESULT result = device->CreateRenderTargetView(mTexture, &rtvDesc, &renderTargetView); |
| 358 | |
| 359 | if (result == E_OUTOFMEMORY) |
| 360 | return; |
| 361 | |
| 362 | ASSERT(SUCCESS(result)); |
| 363 | |
| 364 | mRenderTarget[i] = new RenderTarget11(mRenderer, renderTargetView, size, size); |
| 365 | } |
| 366 | } |
| 367 | else if (getBindFlags() & D3D11_BIND_DEPTH_STENCIL) |
| 368 | { |
| 369 | // TODO |
| 370 | UNIMPLEMENTED(); |
| 371 | } |
| 372 | else |
| 373 | UNREACHABLE(); |
| 374 | } |
| 375 | } |
| 376 | |
daniel@transgaming.com | 4834ee2 | 2013-01-11 04:06:16 +0000 | [diff] [blame] | 377 | } |