Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | #include "precompiled.h" |
michael@0 | 2 | // |
michael@0 | 3 | // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. |
michael@0 | 4 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 5 | // found in the LICENSE file. |
michael@0 | 6 | // |
michael@0 | 7 | |
michael@0 | 8 | // Image11.h: Implements the rx::Image11 class, which acts as the interface to |
michael@0 | 9 | // the actual underlying resources of a Texture |
michael@0 | 10 | |
michael@0 | 11 | #include "libGLESv2/renderer/Renderer11.h" |
michael@0 | 12 | #include "libGLESv2/renderer/Image11.h" |
michael@0 | 13 | #include "libGLESv2/renderer/TextureStorage11.h" |
michael@0 | 14 | #include "libGLESv2/Framebuffer.h" |
michael@0 | 15 | #include "libGLESv2/Renderbuffer.h" |
michael@0 | 16 | |
michael@0 | 17 | #include "libGLESv2/main.h" |
michael@0 | 18 | #include "libGLESv2/utilities.h" |
michael@0 | 19 | #include "libGLESv2/renderer/renderer11_utils.h" |
michael@0 | 20 | #include "libGLESv2/renderer/generatemip.h" |
michael@0 | 21 | |
michael@0 | 22 | namespace rx |
michael@0 | 23 | { |
michael@0 | 24 | |
michael@0 | 25 | Image11::Image11() |
michael@0 | 26 | { |
michael@0 | 27 | mStagingTexture = NULL; |
michael@0 | 28 | mRenderer = NULL; |
michael@0 | 29 | mDXGIFormat = DXGI_FORMAT_UNKNOWN; |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | Image11::~Image11() |
michael@0 | 33 | { |
michael@0 | 34 | if (mStagingTexture) |
michael@0 | 35 | { |
michael@0 | 36 | mStagingTexture->Release(); |
michael@0 | 37 | } |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | Image11 *Image11::makeImage11(Image *img) |
michael@0 | 41 | { |
michael@0 | 42 | ASSERT(HAS_DYNAMIC_TYPE(rx::Image11*, img)); |
michael@0 | 43 | return static_cast<rx::Image11*>(img); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | void Image11::generateMipmap(Image11 *dest, Image11 *src) |
michael@0 | 47 | { |
michael@0 | 48 | ASSERT(src->getDXGIFormat() == dest->getDXGIFormat()); |
michael@0 | 49 | ASSERT(src->getWidth() == 1 || src->getWidth() / 2 == dest->getWidth()); |
michael@0 | 50 | ASSERT(src->getHeight() == 1 || src->getHeight() / 2 == dest->getHeight()); |
michael@0 | 51 | |
michael@0 | 52 | D3D11_MAPPED_SUBRESOURCE destMapped, srcMapped; |
michael@0 | 53 | dest->map(&destMapped); |
michael@0 | 54 | src->map(&srcMapped); |
michael@0 | 55 | |
michael@0 | 56 | const unsigned char *sourceData = reinterpret_cast<const unsigned char*>(srcMapped.pData); |
michael@0 | 57 | unsigned char *destData = reinterpret_cast<unsigned char*>(destMapped.pData); |
michael@0 | 58 | |
michael@0 | 59 | if (sourceData && destData) |
michael@0 | 60 | { |
michael@0 | 61 | switch (src->getDXGIFormat()) |
michael@0 | 62 | { |
michael@0 | 63 | case DXGI_FORMAT_R8G8B8A8_UNORM: |
michael@0 | 64 | case DXGI_FORMAT_B8G8R8A8_UNORM: |
michael@0 | 65 | GenerateMip<R8G8B8A8>(src->getWidth(), src->getHeight(), sourceData, srcMapped.RowPitch, destData, destMapped.RowPitch); |
michael@0 | 66 | break; |
michael@0 | 67 | case DXGI_FORMAT_A8_UNORM: |
michael@0 | 68 | GenerateMip<A8>(src->getWidth(), src->getHeight(), sourceData, srcMapped.RowPitch, destData, destMapped.RowPitch); |
michael@0 | 69 | break; |
michael@0 | 70 | case DXGI_FORMAT_R8_UNORM: |
michael@0 | 71 | GenerateMip<R8>(src->getWidth(), src->getHeight(), sourceData, srcMapped.RowPitch, destData, destMapped.RowPitch); |
michael@0 | 72 | break; |
michael@0 | 73 | case DXGI_FORMAT_R32G32B32A32_FLOAT: |
michael@0 | 74 | GenerateMip<A32B32G32R32F>(src->getWidth(), src->getHeight(), sourceData, srcMapped.RowPitch, destData, destMapped.RowPitch); |
michael@0 | 75 | break; |
michael@0 | 76 | case DXGI_FORMAT_R32G32B32_FLOAT: |
michael@0 | 77 | GenerateMip<R32G32B32F>(src->getWidth(), src->getHeight(), sourceData, srcMapped.RowPitch, destData, destMapped.RowPitch); |
michael@0 | 78 | break; |
michael@0 | 79 | case DXGI_FORMAT_R16G16B16A16_FLOAT: |
michael@0 | 80 | GenerateMip<A16B16G16R16F>(src->getWidth(), src->getHeight(), sourceData, srcMapped.RowPitch, destData, destMapped.RowPitch); |
michael@0 | 81 | break; |
michael@0 | 82 | case DXGI_FORMAT_R8G8_UNORM: |
michael@0 | 83 | GenerateMip<R8G8>(src->getWidth(), src->getHeight(), sourceData, srcMapped.RowPitch, destData, destMapped.RowPitch); |
michael@0 | 84 | break; |
michael@0 | 85 | case DXGI_FORMAT_R16_FLOAT: |
michael@0 | 86 | GenerateMip<R16F>(src->getWidth(), src->getHeight(), sourceData, srcMapped.RowPitch, destData, destMapped.RowPitch); |
michael@0 | 87 | break; |
michael@0 | 88 | case DXGI_FORMAT_R16G16_FLOAT: |
michael@0 | 89 | GenerateMip<R16G16F>(src->getWidth(), src->getHeight(), sourceData, srcMapped.RowPitch, destData, destMapped.RowPitch); |
michael@0 | 90 | break; |
michael@0 | 91 | case DXGI_FORMAT_R32_FLOAT: |
michael@0 | 92 | GenerateMip<R32F>(src->getWidth(), src->getHeight(), sourceData, srcMapped.RowPitch, destData, destMapped.RowPitch); |
michael@0 | 93 | break; |
michael@0 | 94 | case DXGI_FORMAT_R32G32_FLOAT: |
michael@0 | 95 | GenerateMip<R32G32F>(src->getWidth(), src->getHeight(), sourceData, srcMapped.RowPitch, destData, destMapped.RowPitch); |
michael@0 | 96 | break; |
michael@0 | 97 | default: |
michael@0 | 98 | UNREACHABLE(); |
michael@0 | 99 | break; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | dest->unmap(); |
michael@0 | 103 | src->unmap(); |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | dest->markDirty(); |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | bool Image11::isDirty() const |
michael@0 | 110 | { |
michael@0 | 111 | return (mStagingTexture && mDirty); |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | bool Image11::updateSurface(TextureStorageInterface2D *storage, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height) |
michael@0 | 115 | { |
michael@0 | 116 | TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance()); |
michael@0 | 117 | return storage11->updateSubresourceLevel(getStagingTexture(), getStagingSubresource(), level, 0, xoffset, yoffset, width, height); |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | bool Image11::updateSurface(TextureStorageInterfaceCube *storage, int face, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height) |
michael@0 | 121 | { |
michael@0 | 122 | TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance()); |
michael@0 | 123 | return storage11->updateSubresourceLevel(getStagingTexture(), getStagingSubresource(), level, face, xoffset, yoffset, width, height); |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | bool Image11::redefine(Renderer *renderer, GLint internalformat, GLsizei width, GLsizei height, bool forceRelease) |
michael@0 | 127 | { |
michael@0 | 128 | if (mWidth != width || |
michael@0 | 129 | mHeight != height || |
michael@0 | 130 | mInternalFormat != internalformat || |
michael@0 | 131 | forceRelease) |
michael@0 | 132 | { |
michael@0 | 133 | mRenderer = Renderer11::makeRenderer11(renderer); |
michael@0 | 134 | |
michael@0 | 135 | mWidth = width; |
michael@0 | 136 | mHeight = height; |
michael@0 | 137 | mInternalFormat = internalformat; |
michael@0 | 138 | // compute the d3d format that will be used |
michael@0 | 139 | mDXGIFormat = gl_d3d11::ConvertTextureFormat(internalformat); |
michael@0 | 140 | mActualFormat = d3d11_gl::ConvertTextureInternalFormat(mDXGIFormat); |
michael@0 | 141 | |
michael@0 | 142 | if (mStagingTexture) |
michael@0 | 143 | { |
michael@0 | 144 | mStagingTexture->Release(); |
michael@0 | 145 | mStagingTexture = NULL; |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | return true; |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | return false; |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | bool Image11::isRenderableFormat() const |
michael@0 | 155 | { |
michael@0 | 156 | return TextureStorage11::IsTextureFormatRenderable(mDXGIFormat); |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | DXGI_FORMAT Image11::getDXGIFormat() const |
michael@0 | 160 | { |
michael@0 | 161 | // this should only happen if the image hasn't been redefined first |
michael@0 | 162 | // which would be a bug by the caller |
michael@0 | 163 | ASSERT(mDXGIFormat != DXGI_FORMAT_UNKNOWN); |
michael@0 | 164 | |
michael@0 | 165 | return mDXGIFormat; |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | // Store the pixel rectangle designated by xoffset,yoffset,width,height with pixels stored as format/type at input |
michael@0 | 169 | // into the target pixel rectangle. |
michael@0 | 170 | void Image11::loadData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, |
michael@0 | 171 | GLint unpackAlignment, const void *input) |
michael@0 | 172 | { |
michael@0 | 173 | D3D11_MAPPED_SUBRESOURCE mappedImage; |
michael@0 | 174 | HRESULT result = map(&mappedImage); |
michael@0 | 175 | if (FAILED(result)) |
michael@0 | 176 | { |
michael@0 | 177 | ERR("Could not map image for loading."); |
michael@0 | 178 | return; |
michael@0 | 179 | } |
michael@0 | 180 | |
michael@0 | 181 | GLsizei inputPitch = gl::ComputePitch(width, mInternalFormat, unpackAlignment); |
michael@0 | 182 | size_t pixelSize = d3d11::ComputePixelSizeBits(mDXGIFormat) / 8; |
michael@0 | 183 | void* offsetMappedData = (void*)((BYTE *)mappedImage.pData + (yoffset * mappedImage.RowPitch + xoffset * pixelSize)); |
michael@0 | 184 | |
michael@0 | 185 | switch (mInternalFormat) |
michael@0 | 186 | { |
michael@0 | 187 | case GL_ALPHA8_EXT: |
michael@0 | 188 | loadAlphaDataToNative(width, height, inputPitch, input, mappedImage.RowPitch, offsetMappedData); |
michael@0 | 189 | break; |
michael@0 | 190 | case GL_LUMINANCE8_EXT: |
michael@0 | 191 | loadLuminanceDataToNativeOrBGRA(width, height, inputPitch, input, mappedImage.RowPitch, offsetMappedData, false); |
michael@0 | 192 | break; |
michael@0 | 193 | case GL_ALPHA32F_EXT: |
michael@0 | 194 | loadAlphaFloatDataToRGBA(width, height, inputPitch, input, mappedImage.RowPitch, offsetMappedData); |
michael@0 | 195 | break; |
michael@0 | 196 | case GL_LUMINANCE32F_EXT: |
michael@0 | 197 | loadLuminanceFloatDataToRGBA(width, height, inputPitch, input, mappedImage.RowPitch, offsetMappedData); |
michael@0 | 198 | break; |
michael@0 | 199 | case GL_ALPHA16F_EXT: |
michael@0 | 200 | loadAlphaHalfFloatDataToRGBA(width, height, inputPitch, input, mappedImage.RowPitch, offsetMappedData); |
michael@0 | 201 | break; |
michael@0 | 202 | case GL_LUMINANCE16F_EXT: |
michael@0 | 203 | loadLuminanceHalfFloatDataToRGBA(width, height, inputPitch, input, mappedImage.RowPitch, offsetMappedData); |
michael@0 | 204 | break; |
michael@0 | 205 | case GL_LUMINANCE8_ALPHA8_EXT: |
michael@0 | 206 | loadLuminanceAlphaDataToNativeOrBGRA(width, height, inputPitch, input, mappedImage.RowPitch, offsetMappedData, false); |
michael@0 | 207 | break; |
michael@0 | 208 | case GL_LUMINANCE_ALPHA32F_EXT: |
michael@0 | 209 | loadLuminanceAlphaFloatDataToRGBA(width, height, inputPitch, input, mappedImage.RowPitch, offsetMappedData); |
michael@0 | 210 | break; |
michael@0 | 211 | case GL_LUMINANCE_ALPHA16F_EXT: |
michael@0 | 212 | loadLuminanceAlphaHalfFloatDataToRGBA(width, height, inputPitch, input, mappedImage.RowPitch, offsetMappedData); |
michael@0 | 213 | break; |
michael@0 | 214 | case GL_RGB8_OES: |
michael@0 | 215 | loadRGBUByteDataToRGBA(width, height, inputPitch, input, mappedImage.RowPitch, offsetMappedData); |
michael@0 | 216 | break; |
michael@0 | 217 | case GL_RGB565: |
michael@0 | 218 | loadRGB565DataToRGBA(width, height, inputPitch, input, mappedImage.RowPitch, offsetMappedData); |
michael@0 | 219 | break; |
michael@0 | 220 | case GL_RGBA8_OES: |
michael@0 | 221 | loadRGBAUByteDataToNative(width, height, inputPitch, input, mappedImage.RowPitch, offsetMappedData); |
michael@0 | 222 | break; |
michael@0 | 223 | case GL_RGBA4: |
michael@0 | 224 | loadRGBA4444DataToRGBA(width, height, inputPitch, input, mappedImage.RowPitch, offsetMappedData); |
michael@0 | 225 | break; |
michael@0 | 226 | case GL_RGB5_A1: |
michael@0 | 227 | loadRGBA5551DataToRGBA(width, height, inputPitch, input, mappedImage.RowPitch, offsetMappedData); |
michael@0 | 228 | break; |
michael@0 | 229 | case GL_BGRA8_EXT: |
michael@0 | 230 | loadBGRADataToBGRA(width, height, inputPitch, input, mappedImage.RowPitch, offsetMappedData); |
michael@0 | 231 | break; |
michael@0 | 232 | case GL_RGB32F_EXT: |
michael@0 | 233 | loadRGBFloatDataToRGBA(width, height, inputPitch, input, mappedImage.RowPitch, offsetMappedData); |
michael@0 | 234 | break; |
michael@0 | 235 | case GL_RGB16F_EXT: |
michael@0 | 236 | loadRGBHalfFloatDataToRGBA(width, height, inputPitch, input, mappedImage.RowPitch, offsetMappedData); |
michael@0 | 237 | break; |
michael@0 | 238 | case GL_RGBA32F_EXT: |
michael@0 | 239 | loadRGBAFloatDataToRGBA(width, height, inputPitch, input, mappedImage.RowPitch, offsetMappedData); |
michael@0 | 240 | break; |
michael@0 | 241 | case GL_RGBA16F_EXT: |
michael@0 | 242 | loadRGBAHalfFloatDataToRGBA(width, height, inputPitch, input, mappedImage.RowPitch, offsetMappedData); |
michael@0 | 243 | break; |
michael@0 | 244 | default: UNREACHABLE(); |
michael@0 | 245 | } |
michael@0 | 246 | |
michael@0 | 247 | unmap(); |
michael@0 | 248 | } |
michael@0 | 249 | |
michael@0 | 250 | void Image11::loadCompressedData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, |
michael@0 | 251 | const void *input) |
michael@0 | 252 | { |
michael@0 | 253 | ASSERT(xoffset % 4 == 0); |
michael@0 | 254 | ASSERT(yoffset % 4 == 0); |
michael@0 | 255 | |
michael@0 | 256 | D3D11_MAPPED_SUBRESOURCE mappedImage; |
michael@0 | 257 | HRESULT result = map(&mappedImage); |
michael@0 | 258 | if (FAILED(result)) |
michael@0 | 259 | { |
michael@0 | 260 | ERR("Could not map image for loading."); |
michael@0 | 261 | return; |
michael@0 | 262 | } |
michael@0 | 263 | |
michael@0 | 264 | // Size computation assumes a 4x4 block compressed texture format |
michael@0 | 265 | size_t blockSize = d3d11::ComputeBlockSizeBits(mDXGIFormat) / 8; |
michael@0 | 266 | void* offsetMappedData = (void*)((BYTE *)mappedImage.pData + ((yoffset / 4) * mappedImage.RowPitch + (xoffset / 4) * blockSize)); |
michael@0 | 267 | |
michael@0 | 268 | GLsizei inputSize = gl::ComputeCompressedSize(width, height, mInternalFormat); |
michael@0 | 269 | GLsizei inputPitch = gl::ComputeCompressedPitch(width, mInternalFormat); |
michael@0 | 270 | int rows = inputSize / inputPitch; |
michael@0 | 271 | for (int i = 0; i < rows; ++i) |
michael@0 | 272 | { |
michael@0 | 273 | memcpy((void*)((BYTE*)offsetMappedData + i * mappedImage.RowPitch), (void*)((BYTE*)input + i * inputPitch), inputPitch); |
michael@0 | 274 | } |
michael@0 | 275 | |
michael@0 | 276 | unmap(); |
michael@0 | 277 | } |
michael@0 | 278 | |
michael@0 | 279 | void Image11::copy(GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, gl::Framebuffer *source) |
michael@0 | 280 | { |
michael@0 | 281 | gl::Renderbuffer *colorbuffer = source->getReadColorbuffer(); |
michael@0 | 282 | |
michael@0 | 283 | if (colorbuffer && colorbuffer->getActualFormat() == (GLuint)mActualFormat) |
michael@0 | 284 | { |
michael@0 | 285 | // No conversion needed-- use copyback fastpath |
michael@0 | 286 | ID3D11Texture2D *colorBufferTexture = NULL; |
michael@0 | 287 | unsigned int subresourceIndex = 0; |
michael@0 | 288 | |
michael@0 | 289 | if (mRenderer->getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture)) |
michael@0 | 290 | { |
michael@0 | 291 | D3D11_TEXTURE2D_DESC textureDesc; |
michael@0 | 292 | colorBufferTexture->GetDesc(&textureDesc); |
michael@0 | 293 | |
michael@0 | 294 | ID3D11Device *device = mRenderer->getDevice(); |
michael@0 | 295 | ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext(); |
michael@0 | 296 | |
michael@0 | 297 | ID3D11Texture2D* srcTex = NULL; |
michael@0 | 298 | if (textureDesc.SampleDesc.Count > 1) |
michael@0 | 299 | { |
michael@0 | 300 | D3D11_TEXTURE2D_DESC resolveDesc; |
michael@0 | 301 | resolveDesc.Width = textureDesc.Width; |
michael@0 | 302 | resolveDesc.Height = textureDesc.Height; |
michael@0 | 303 | resolveDesc.MipLevels = 1; |
michael@0 | 304 | resolveDesc.ArraySize = 1; |
michael@0 | 305 | resolveDesc.Format = textureDesc.Format; |
michael@0 | 306 | resolveDesc.SampleDesc.Count = 1; |
michael@0 | 307 | resolveDesc.SampleDesc.Quality = 0; |
michael@0 | 308 | resolveDesc.Usage = D3D11_USAGE_DEFAULT; |
michael@0 | 309 | resolveDesc.BindFlags = 0; |
michael@0 | 310 | resolveDesc.CPUAccessFlags = 0; |
michael@0 | 311 | resolveDesc.MiscFlags = 0; |
michael@0 | 312 | |
michael@0 | 313 | HRESULT result = device->CreateTexture2D(&resolveDesc, NULL, &srcTex); |
michael@0 | 314 | if (FAILED(result)) |
michael@0 | 315 | { |
michael@0 | 316 | ERR("Failed to create resolve texture for Image11::copy, HRESULT: 0x%X.", result); |
michael@0 | 317 | return; |
michael@0 | 318 | } |
michael@0 | 319 | |
michael@0 | 320 | deviceContext->ResolveSubresource(srcTex, 0, colorBufferTexture, subresourceIndex, textureDesc.Format); |
michael@0 | 321 | subresourceIndex = 0; |
michael@0 | 322 | } |
michael@0 | 323 | else |
michael@0 | 324 | { |
michael@0 | 325 | srcTex = colorBufferTexture; |
michael@0 | 326 | srcTex->AddRef(); |
michael@0 | 327 | } |
michael@0 | 328 | |
michael@0 | 329 | D3D11_BOX srcBox; |
michael@0 | 330 | srcBox.left = x; |
michael@0 | 331 | srcBox.right = x + width; |
michael@0 | 332 | srcBox.top = y; |
michael@0 | 333 | srcBox.bottom = y + height; |
michael@0 | 334 | srcBox.front = 0; |
michael@0 | 335 | srcBox.back = 1; |
michael@0 | 336 | |
michael@0 | 337 | deviceContext->CopySubresourceRegion(mStagingTexture, 0, xoffset, yoffset, 0, srcTex, subresourceIndex, &srcBox); |
michael@0 | 338 | |
michael@0 | 339 | srcTex->Release(); |
michael@0 | 340 | colorBufferTexture->Release(); |
michael@0 | 341 | } |
michael@0 | 342 | } |
michael@0 | 343 | else |
michael@0 | 344 | { |
michael@0 | 345 | // This format requires conversion, so we must copy the texture to staging and manually convert via readPixels |
michael@0 | 346 | D3D11_MAPPED_SUBRESOURCE mappedImage; |
michael@0 | 347 | HRESULT result = map(&mappedImage); |
michael@0 | 348 | |
michael@0 | 349 | // determine the offset coordinate into the destination buffer |
michael@0 | 350 | GLsizei rowOffset = gl::ComputePixelSize(mActualFormat) * xoffset; |
michael@0 | 351 | void *dataOffset = static_cast<unsigned char*>(mappedImage.pData) + mappedImage.RowPitch * yoffset + rowOffset; |
michael@0 | 352 | |
michael@0 | 353 | mRenderer->readPixels(source, x, y, width, height, gl::ExtractFormat(mInternalFormat), |
michael@0 | 354 | gl::ExtractType(mInternalFormat), mappedImage.RowPitch, false, 4, dataOffset); |
michael@0 | 355 | |
michael@0 | 356 | unmap(); |
michael@0 | 357 | } |
michael@0 | 358 | } |
michael@0 | 359 | |
michael@0 | 360 | ID3D11Texture2D *Image11::getStagingTexture() |
michael@0 | 361 | { |
michael@0 | 362 | createStagingTexture(); |
michael@0 | 363 | |
michael@0 | 364 | return mStagingTexture; |
michael@0 | 365 | } |
michael@0 | 366 | |
michael@0 | 367 | unsigned int Image11::getStagingSubresource() |
michael@0 | 368 | { |
michael@0 | 369 | createStagingTexture(); |
michael@0 | 370 | |
michael@0 | 371 | return mStagingSubresource; |
michael@0 | 372 | } |
michael@0 | 373 | |
michael@0 | 374 | void Image11::createStagingTexture() |
michael@0 | 375 | { |
michael@0 | 376 | if (mStagingTexture) |
michael@0 | 377 | { |
michael@0 | 378 | return; |
michael@0 | 379 | } |
michael@0 | 380 | |
michael@0 | 381 | ID3D11Texture2D *newTexture = NULL; |
michael@0 | 382 | int lodOffset = 1; |
michael@0 | 383 | const DXGI_FORMAT dxgiFormat = getDXGIFormat(); |
michael@0 | 384 | ASSERT(!d3d11::IsDepthStencilFormat(dxgiFormat)); // We should never get here for depth textures |
michael@0 | 385 | |
michael@0 | 386 | if (mWidth != 0 && mHeight != 0) |
michael@0 | 387 | { |
michael@0 | 388 | GLsizei width = mWidth; |
michael@0 | 389 | GLsizei height = mHeight; |
michael@0 | 390 | |
michael@0 | 391 | // adjust size if needed for compressed textures |
michael@0 | 392 | gl::MakeValidSize(false, d3d11::IsCompressed(dxgiFormat), &width, &height, &lodOffset); |
michael@0 | 393 | ID3D11Device *device = mRenderer->getDevice(); |
michael@0 | 394 | |
michael@0 | 395 | D3D11_TEXTURE2D_DESC desc; |
michael@0 | 396 | desc.Width = width; |
michael@0 | 397 | desc.Height = height; |
michael@0 | 398 | desc.MipLevels = lodOffset + 1; |
michael@0 | 399 | desc.ArraySize = 1; |
michael@0 | 400 | desc.Format = dxgiFormat; |
michael@0 | 401 | desc.SampleDesc.Count = 1; |
michael@0 | 402 | desc.SampleDesc.Quality = 0; |
michael@0 | 403 | desc.Usage = D3D11_USAGE_STAGING; |
michael@0 | 404 | desc.BindFlags = 0; |
michael@0 | 405 | desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; |
michael@0 | 406 | desc.MiscFlags = 0; |
michael@0 | 407 | |
michael@0 | 408 | HRESULT result = device->CreateTexture2D(&desc, NULL, &newTexture); |
michael@0 | 409 | |
michael@0 | 410 | if (FAILED(result)) |
michael@0 | 411 | { |
michael@0 | 412 | ASSERT(result == E_OUTOFMEMORY); |
michael@0 | 413 | ERR("Creating image failed."); |
michael@0 | 414 | return gl::error(GL_OUT_OF_MEMORY); |
michael@0 | 415 | } |
michael@0 | 416 | } |
michael@0 | 417 | |
michael@0 | 418 | mStagingTexture = newTexture; |
michael@0 | 419 | mStagingSubresource = D3D11CalcSubresource(lodOffset, 0, lodOffset + 1); |
michael@0 | 420 | mDirty = false; |
michael@0 | 421 | } |
michael@0 | 422 | |
michael@0 | 423 | HRESULT Image11::map(D3D11_MAPPED_SUBRESOURCE *map) |
michael@0 | 424 | { |
michael@0 | 425 | createStagingTexture(); |
michael@0 | 426 | |
michael@0 | 427 | HRESULT result = E_FAIL; |
michael@0 | 428 | |
michael@0 | 429 | if (mStagingTexture) |
michael@0 | 430 | { |
michael@0 | 431 | ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext(); |
michael@0 | 432 | result = deviceContext->Map(mStagingTexture, mStagingSubresource, D3D11_MAP_WRITE, 0, map); |
michael@0 | 433 | |
michael@0 | 434 | // this can fail if the device is removed (from TDR) |
michael@0 | 435 | if (d3d11::isDeviceLostError(result)) |
michael@0 | 436 | { |
michael@0 | 437 | mRenderer->notifyDeviceLost(); |
michael@0 | 438 | } |
michael@0 | 439 | else if (SUCCEEDED(result)) |
michael@0 | 440 | { |
michael@0 | 441 | mDirty = true; |
michael@0 | 442 | } |
michael@0 | 443 | } |
michael@0 | 444 | |
michael@0 | 445 | return result; |
michael@0 | 446 | } |
michael@0 | 447 | |
michael@0 | 448 | void Image11::unmap() |
michael@0 | 449 | { |
michael@0 | 450 | if (mStagingTexture) |
michael@0 | 451 | { |
michael@0 | 452 | ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext(); |
michael@0 | 453 | deviceContext->Unmap(mStagingTexture, mStagingSubresource); |
michael@0 | 454 | } |
michael@0 | 455 | } |
michael@0 | 456 | |
michael@0 | 457 | } |