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