michael@0: #include "precompiled.h" michael@0: // michael@0: // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: // michael@0: michael@0: // Image9.cpp: Implements the rx::Image9 class, which acts as the interface to michael@0: // the actual underlying surfaces of a Texture. michael@0: michael@0: #include "libGLESv2/renderer/Image9.h" michael@0: michael@0: #include "libGLESv2/main.h" michael@0: #include "libGLESv2/Framebuffer.h" michael@0: #include "libGLESv2/Renderbuffer.h" michael@0: #include "libGLESv2/renderer/Renderer9.h" michael@0: #include "libGLESv2/renderer/RenderTarget9.h" michael@0: #include "libGLESv2/renderer/TextureStorage9.h" michael@0: michael@0: #include "libGLESv2/renderer/renderer9_utils.h" michael@0: #include "libGLESv2/renderer/generatemip.h" michael@0: michael@0: namespace rx michael@0: { michael@0: michael@0: Image9::Image9() michael@0: { michael@0: mSurface = NULL; michael@0: mRenderer = NULL; michael@0: michael@0: mD3DPool = D3DPOOL_SYSTEMMEM; michael@0: mD3DFormat = D3DFMT_UNKNOWN; michael@0: } michael@0: michael@0: Image9::~Image9() michael@0: { michael@0: if (mSurface) michael@0: { michael@0: mSurface->Release(); michael@0: } michael@0: } michael@0: michael@0: void Image9::generateMip(IDirect3DSurface9 *destSurface, IDirect3DSurface9 *sourceSurface) michael@0: { michael@0: D3DSURFACE_DESC destDesc; michael@0: HRESULT result = destSurface->GetDesc(&destDesc); michael@0: ASSERT(SUCCEEDED(result)); michael@0: michael@0: D3DSURFACE_DESC sourceDesc; michael@0: result = sourceSurface->GetDesc(&sourceDesc); michael@0: ASSERT(SUCCEEDED(result)); michael@0: michael@0: ASSERT(sourceDesc.Format == destDesc.Format); michael@0: ASSERT(sourceDesc.Width == 1 || sourceDesc.Width / 2 == destDesc.Width); michael@0: ASSERT(sourceDesc.Height == 1 || sourceDesc.Height / 2 == destDesc.Height); michael@0: michael@0: D3DLOCKED_RECT sourceLocked = {0}; michael@0: result = sourceSurface->LockRect(&sourceLocked, NULL, D3DLOCK_READONLY); michael@0: ASSERT(SUCCEEDED(result)); michael@0: michael@0: D3DLOCKED_RECT destLocked = {0}; michael@0: result = destSurface->LockRect(&destLocked, NULL, 0); michael@0: ASSERT(SUCCEEDED(result)); michael@0: michael@0: const unsigned char *sourceData = reinterpret_cast(sourceLocked.pBits); michael@0: unsigned char *destData = reinterpret_cast(destLocked.pBits); michael@0: michael@0: if (sourceData && destData) michael@0: { michael@0: switch (sourceDesc.Format) michael@0: { michael@0: case D3DFMT_L8: michael@0: GenerateMip(sourceDesc.Width, sourceDesc.Height, sourceData, sourceLocked.Pitch, destData, destLocked.Pitch); michael@0: break; michael@0: case D3DFMT_A8L8: michael@0: GenerateMip(sourceDesc.Width, sourceDesc.Height, sourceData, sourceLocked.Pitch, destData, destLocked.Pitch); michael@0: break; michael@0: case D3DFMT_A8R8G8B8: michael@0: case D3DFMT_X8R8G8B8: michael@0: GenerateMip(sourceDesc.Width, sourceDesc.Height, sourceData, sourceLocked.Pitch, destData, destLocked.Pitch); michael@0: break; michael@0: case D3DFMT_A16B16G16R16F: michael@0: GenerateMip(sourceDesc.Width, sourceDesc.Height, sourceData, sourceLocked.Pitch, destData, destLocked.Pitch); michael@0: break; michael@0: case D3DFMT_A32B32G32R32F: michael@0: GenerateMip(sourceDesc.Width, sourceDesc.Height, sourceData, sourceLocked.Pitch, destData, destLocked.Pitch); michael@0: break; michael@0: default: michael@0: UNREACHABLE(); michael@0: break; michael@0: } michael@0: michael@0: destSurface->UnlockRect(); michael@0: sourceSurface->UnlockRect(); michael@0: } michael@0: } michael@0: michael@0: Image9 *Image9::makeImage9(Image *img) michael@0: { michael@0: ASSERT(HAS_DYNAMIC_TYPE(rx::Image9*, img)); michael@0: return static_cast(img); michael@0: } michael@0: michael@0: void Image9::generateMipmap(Image9 *dest, Image9 *source) michael@0: { michael@0: IDirect3DSurface9 *sourceSurface = source->getSurface(); michael@0: if (sourceSurface == NULL) michael@0: return gl::error(GL_OUT_OF_MEMORY); michael@0: michael@0: IDirect3DSurface9 *destSurface = dest->getSurface(); michael@0: generateMip(destSurface, sourceSurface); michael@0: michael@0: dest->markDirty(); michael@0: } michael@0: michael@0: void Image9::copyLockableSurfaces(IDirect3DSurface9 *dest, IDirect3DSurface9 *source) michael@0: { michael@0: D3DLOCKED_RECT sourceLock = {0}; michael@0: D3DLOCKED_RECT destLock = {0}; michael@0: michael@0: source->LockRect(&sourceLock, NULL, 0); michael@0: dest->LockRect(&destLock, NULL, 0); michael@0: michael@0: if (sourceLock.pBits && destLock.pBits) michael@0: { michael@0: D3DSURFACE_DESC desc; michael@0: source->GetDesc(&desc); michael@0: michael@0: int rows = d3d9::IsCompressedFormat(desc.Format) ? desc.Height / 4 : desc.Height; michael@0: int bytes = d3d9::ComputeRowSize(desc.Format, desc.Width); michael@0: ASSERT(bytes <= sourceLock.Pitch && bytes <= destLock.Pitch); michael@0: michael@0: for(int i = 0; i < rows; i++) michael@0: { michael@0: memcpy((char*)destLock.pBits + destLock.Pitch * i, (char*)sourceLock.pBits + sourceLock.Pitch * i, bytes); michael@0: } michael@0: michael@0: source->UnlockRect(); michael@0: dest->UnlockRect(); michael@0: } michael@0: else UNREACHABLE(); michael@0: } michael@0: michael@0: bool Image9::redefine(rx::Renderer *renderer, GLint internalformat, GLsizei width, GLsizei height, bool forceRelease) michael@0: { michael@0: if (mWidth != width || michael@0: mHeight != height || michael@0: mInternalFormat != internalformat || michael@0: forceRelease) michael@0: { michael@0: mRenderer = Renderer9::makeRenderer9(renderer); michael@0: michael@0: mWidth = width; michael@0: mHeight = height; michael@0: mInternalFormat = internalformat; michael@0: // compute the d3d format that will be used michael@0: mD3DFormat = mRenderer->ConvertTextureInternalFormat(internalformat); michael@0: mActualFormat = d3d9_gl::GetEquivalentFormat(mD3DFormat); michael@0: michael@0: if (mSurface) michael@0: { michael@0: mSurface->Release(); michael@0: mSurface = NULL; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: void Image9::createSurface() michael@0: { michael@0: if(mSurface) michael@0: { michael@0: return; michael@0: } michael@0: michael@0: IDirect3DTexture9 *newTexture = NULL; michael@0: IDirect3DSurface9 *newSurface = NULL; michael@0: const D3DPOOL poolToUse = D3DPOOL_SYSTEMMEM; michael@0: const D3DFORMAT d3dFormat = getD3DFormat(); michael@0: ASSERT(d3dFormat != D3DFMT_INTZ); // We should never get here for depth textures michael@0: michael@0: if (mWidth != 0 && mHeight != 0) michael@0: { michael@0: int levelToFetch = 0; michael@0: GLsizei requestWidth = mWidth; michael@0: GLsizei requestHeight = mHeight; michael@0: gl::MakeValidSize(true, gl::IsCompressed(mInternalFormat), &requestWidth, &requestHeight, &levelToFetch); michael@0: michael@0: IDirect3DDevice9 *device = mRenderer->getDevice(); michael@0: michael@0: HRESULT result = device->CreateTexture(requestWidth, requestHeight, levelToFetch + 1, 0, d3dFormat, michael@0: poolToUse, &newTexture, NULL); michael@0: michael@0: if (FAILED(result)) michael@0: { michael@0: ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); michael@0: ERR("Creating image surface failed."); michael@0: return gl::error(GL_OUT_OF_MEMORY); michael@0: } michael@0: michael@0: newTexture->GetSurfaceLevel(levelToFetch, &newSurface); michael@0: newTexture->Release(); michael@0: } michael@0: michael@0: mSurface = newSurface; michael@0: mDirty = false; michael@0: mD3DPool = poolToUse; michael@0: } michael@0: michael@0: HRESULT Image9::lock(D3DLOCKED_RECT *lockedRect, const RECT *rect) michael@0: { michael@0: createSurface(); michael@0: michael@0: HRESULT result = D3DERR_INVALIDCALL; michael@0: michael@0: if (mSurface) michael@0: { michael@0: result = mSurface->LockRect(lockedRect, rect, 0); michael@0: ASSERT(SUCCEEDED(result)); michael@0: michael@0: mDirty = true; michael@0: } michael@0: michael@0: return result; michael@0: } michael@0: michael@0: void Image9::unlock() michael@0: { michael@0: if (mSurface) michael@0: { michael@0: HRESULT result = mSurface->UnlockRect(); michael@0: ASSERT(SUCCEEDED(result)); michael@0: } michael@0: } michael@0: michael@0: bool Image9::isRenderableFormat() const michael@0: { michael@0: return TextureStorage9::IsTextureFormatRenderable(getD3DFormat()); michael@0: } michael@0: michael@0: D3DFORMAT Image9::getD3DFormat() const michael@0: { michael@0: // this should only happen if the image hasn't been redefined first michael@0: // which would be a bug by the caller michael@0: ASSERT(mD3DFormat != D3DFMT_UNKNOWN); michael@0: michael@0: return mD3DFormat; michael@0: } michael@0: michael@0: IDirect3DSurface9 *Image9::getSurface() michael@0: { michael@0: createSurface(); michael@0: michael@0: return mSurface; michael@0: } michael@0: michael@0: void Image9::setManagedSurface(TextureStorageInterface2D *storage, int level) michael@0: { michael@0: TextureStorage9_2D *storage9 = TextureStorage9_2D::makeTextureStorage9_2D(storage->getStorageInstance()); michael@0: setManagedSurface(storage9->getSurfaceLevel(level, false)); michael@0: } michael@0: michael@0: void Image9::setManagedSurface(TextureStorageInterfaceCube *storage, int face, int level) michael@0: { michael@0: TextureStorage9_Cube *storage9 = TextureStorage9_Cube::makeTextureStorage9_Cube(storage->getStorageInstance()); michael@0: setManagedSurface(storage9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, false)); michael@0: } michael@0: michael@0: void Image9::setManagedSurface(IDirect3DSurface9 *surface) michael@0: { michael@0: D3DSURFACE_DESC desc; michael@0: surface->GetDesc(&desc); michael@0: ASSERT(desc.Pool == D3DPOOL_MANAGED); michael@0: michael@0: if ((GLsizei)desc.Width == mWidth && (GLsizei)desc.Height == mHeight) michael@0: { michael@0: if (mSurface) michael@0: { michael@0: copyLockableSurfaces(surface, mSurface); michael@0: mSurface->Release(); michael@0: } michael@0: michael@0: mSurface = surface; michael@0: mD3DPool = desc.Pool; michael@0: } michael@0: } michael@0: michael@0: bool Image9::updateSurface(TextureStorageInterface2D *storage, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height) michael@0: { michael@0: ASSERT(getSurface() != NULL); michael@0: TextureStorage9_2D *storage9 = TextureStorage9_2D::makeTextureStorage9_2D(storage->getStorageInstance()); michael@0: return updateSurface(storage9->getSurfaceLevel(level, true), xoffset, yoffset, width, height); michael@0: } michael@0: michael@0: bool Image9::updateSurface(TextureStorageInterfaceCube *storage, int face, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height) michael@0: { michael@0: ASSERT(getSurface() != NULL); michael@0: TextureStorage9_Cube *storage9 = TextureStorage9_Cube::makeTextureStorage9_Cube(storage->getStorageInstance()); michael@0: return updateSurface(storage9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, true), xoffset, yoffset, width, height); michael@0: } michael@0: michael@0: bool Image9::updateSurface(IDirect3DSurface9 *destSurface, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height) michael@0: { michael@0: if (!destSurface) michael@0: return false; michael@0: michael@0: IDirect3DSurface9 *sourceSurface = getSurface(); michael@0: michael@0: if (sourceSurface && sourceSurface != destSurface) michael@0: { michael@0: RECT rect; michael@0: rect.left = xoffset; michael@0: rect.top = yoffset; michael@0: rect.right = xoffset + width; michael@0: rect.bottom = yoffset + height; michael@0: michael@0: POINT point = {rect.left, rect.top}; michael@0: michael@0: IDirect3DDevice9 *device = mRenderer->getDevice(); michael@0: michael@0: if (mD3DPool == D3DPOOL_MANAGED) michael@0: { michael@0: D3DSURFACE_DESC desc; michael@0: sourceSurface->GetDesc(&desc); michael@0: michael@0: IDirect3DSurface9 *surf = 0; michael@0: HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL); michael@0: michael@0: if (SUCCEEDED(result)) michael@0: { michael@0: copyLockableSurfaces(surf, sourceSurface); michael@0: result = device->UpdateSurface(surf, &rect, destSurface, &point); michael@0: ASSERT(SUCCEEDED(result)); michael@0: surf->Release(); michael@0: } michael@0: } michael@0: else michael@0: { michael@0: // UpdateSurface: source must be SYSTEMMEM, dest must be DEFAULT pools michael@0: HRESULT result = device->UpdateSurface(sourceSurface, &rect, destSurface, &point); michael@0: ASSERT(SUCCEEDED(result)); michael@0: } michael@0: } michael@0: michael@0: destSurface->Release(); michael@0: return true; michael@0: } michael@0: michael@0: // Store the pixel rectangle designated by xoffset,yoffset,width,height with pixels stored as format/type at input michael@0: // into the target pixel rectangle. michael@0: void Image9::loadData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, michael@0: GLint unpackAlignment, const void *input) michael@0: { michael@0: RECT lockRect = michael@0: { michael@0: xoffset, yoffset, michael@0: xoffset + width, yoffset + height michael@0: }; michael@0: michael@0: D3DLOCKED_RECT locked; michael@0: HRESULT result = lock(&locked, &lockRect); michael@0: if (FAILED(result)) michael@0: { michael@0: return; michael@0: } michael@0: michael@0: michael@0: GLsizei inputPitch = gl::ComputePitch(width, mInternalFormat, unpackAlignment); michael@0: michael@0: switch (mInternalFormat) michael@0: { michael@0: case GL_ALPHA8_EXT: michael@0: if (gl::supportsSSE2()) michael@0: { michael@0: loadAlphaDataToBGRASSE2(width, height, inputPitch, input, locked.Pitch, locked.pBits); michael@0: } michael@0: else michael@0: { michael@0: loadAlphaDataToBGRA(width, height, inputPitch, input, locked.Pitch, locked.pBits); michael@0: } michael@0: break; michael@0: case GL_LUMINANCE8_EXT: michael@0: loadLuminanceDataToNativeOrBGRA(width, height, inputPitch, input, locked.Pitch, locked.pBits, getD3DFormat() == D3DFMT_L8); michael@0: break; michael@0: case GL_ALPHA32F_EXT: michael@0: loadAlphaFloatDataToRGBA(width, height, inputPitch, input, locked.Pitch, locked.pBits); michael@0: break; michael@0: case GL_LUMINANCE32F_EXT: michael@0: loadLuminanceFloatDataToRGBA(width, height, inputPitch, input, locked.Pitch, locked.pBits); michael@0: break; michael@0: case GL_ALPHA16F_EXT: michael@0: loadAlphaHalfFloatDataToRGBA(width, height, inputPitch, input, locked.Pitch, locked.pBits); michael@0: break; michael@0: case GL_LUMINANCE16F_EXT: michael@0: loadLuminanceHalfFloatDataToRGBA(width, height, inputPitch, input, locked.Pitch, locked.pBits); michael@0: break; michael@0: case GL_LUMINANCE8_ALPHA8_EXT: michael@0: loadLuminanceAlphaDataToNativeOrBGRA(width, height, inputPitch, input, locked.Pitch, locked.pBits, getD3DFormat() == D3DFMT_A8L8); michael@0: break; michael@0: case GL_LUMINANCE_ALPHA32F_EXT: michael@0: loadLuminanceAlphaFloatDataToRGBA(width, height, inputPitch, input, locked.Pitch, locked.pBits); michael@0: break; michael@0: case GL_LUMINANCE_ALPHA16F_EXT: michael@0: loadLuminanceAlphaHalfFloatDataToRGBA(width, height, inputPitch, input, locked.Pitch, locked.pBits); michael@0: break; michael@0: case GL_RGB8_OES: michael@0: loadRGBUByteDataToBGRX(width, height, inputPitch, input, locked.Pitch, locked.pBits); michael@0: break; michael@0: case GL_RGB565: michael@0: loadRGB565DataToBGRA(width, height, inputPitch, input, locked.Pitch, locked.pBits); michael@0: break; michael@0: case GL_RGBA8_OES: michael@0: if (gl::supportsSSE2()) michael@0: { michael@0: loadRGBAUByteDataToBGRASSE2(width, height, inputPitch, input, locked.Pitch, locked.pBits); michael@0: } michael@0: else michael@0: { michael@0: loadRGBAUByteDataToBGRA(width, height, inputPitch, input, locked.Pitch, locked.pBits); michael@0: } michael@0: break; michael@0: case GL_RGBA4: michael@0: loadRGBA4444DataToBGRA(width, height, inputPitch, input, locked.Pitch, locked.pBits); michael@0: break; michael@0: case GL_RGB5_A1: michael@0: loadRGBA5551DataToBGRA(width, height, inputPitch, input, locked.Pitch, locked.pBits); michael@0: break; michael@0: case GL_BGRA8_EXT: michael@0: loadBGRADataToBGRA(width, height, inputPitch, input, locked.Pitch, locked.pBits); michael@0: break; michael@0: // float textures are converted to RGBA, not BGRA, as they're stored that way in D3D michael@0: case GL_RGB32F_EXT: michael@0: loadRGBFloatDataToRGBA(width, height, inputPitch, input, locked.Pitch, locked.pBits); michael@0: break; michael@0: case GL_RGB16F_EXT: michael@0: loadRGBHalfFloatDataToRGBA(width, height, inputPitch, input, locked.Pitch, locked.pBits); michael@0: break; michael@0: case GL_RGBA32F_EXT: michael@0: loadRGBAFloatDataToRGBA(width, height, inputPitch, input, locked.Pitch, locked.pBits); michael@0: break; michael@0: case GL_RGBA16F_EXT: michael@0: loadRGBAHalfFloatDataToRGBA(width, height, inputPitch, input, locked.Pitch, locked.pBits); michael@0: break; michael@0: default: UNREACHABLE(); michael@0: } michael@0: michael@0: unlock(); michael@0: } michael@0: michael@0: void Image9::loadCompressedData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, michael@0: const void *input) michael@0: { michael@0: ASSERT(xoffset % 4 == 0); michael@0: ASSERT(yoffset % 4 == 0); michael@0: michael@0: RECT lockRect = { michael@0: xoffset, yoffset, michael@0: xoffset + width, yoffset + height michael@0: }; michael@0: michael@0: D3DLOCKED_RECT locked; michael@0: HRESULT result = lock(&locked, &lockRect); michael@0: if (FAILED(result)) michael@0: { michael@0: return; michael@0: } michael@0: michael@0: GLsizei inputSize = gl::ComputeCompressedSize(width, height, mInternalFormat); michael@0: GLsizei inputPitch = gl::ComputeCompressedPitch(width, mInternalFormat); michael@0: int rows = inputSize / inputPitch; michael@0: for (int i = 0; i < rows; ++i) michael@0: { michael@0: memcpy((void*)((BYTE*)locked.pBits + i * locked.Pitch), (void*)((BYTE*)input + i * inputPitch), inputPitch); michael@0: } michael@0: michael@0: unlock(); michael@0: } michael@0: michael@0: // This implements glCopyTex[Sub]Image2D for non-renderable internal texture formats and incomplete textures michael@0: void Image9::copy(GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, gl::Framebuffer *source) michael@0: { michael@0: RenderTarget9 *renderTarget = NULL; michael@0: IDirect3DSurface9 *surface = NULL; michael@0: gl::Renderbuffer *colorbuffer = source->getColorbuffer(0); michael@0: michael@0: if (colorbuffer) michael@0: { michael@0: renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget()); michael@0: } michael@0: michael@0: if (renderTarget) michael@0: { michael@0: surface = renderTarget->getSurface(); michael@0: } michael@0: michael@0: if (!surface) michael@0: { michael@0: ERR("Failed to retrieve the render target."); michael@0: return gl::error(GL_OUT_OF_MEMORY); michael@0: } michael@0: michael@0: IDirect3DDevice9 *device = mRenderer->getDevice(); michael@0: michael@0: IDirect3DSurface9 *renderTargetData = NULL; michael@0: D3DSURFACE_DESC description; michael@0: surface->GetDesc(&description); michael@0: michael@0: HRESULT result = device->CreateOffscreenPlainSurface(description.Width, description.Height, description.Format, D3DPOOL_SYSTEMMEM, &renderTargetData, NULL); michael@0: michael@0: if (FAILED(result)) michael@0: { michael@0: ERR("Could not create matching destination surface."); michael@0: surface->Release(); michael@0: return gl::error(GL_OUT_OF_MEMORY); michael@0: } michael@0: michael@0: result = device->GetRenderTargetData(surface, renderTargetData); michael@0: michael@0: if (FAILED(result)) michael@0: { michael@0: ERR("GetRenderTargetData unexpectedly failed."); michael@0: renderTargetData->Release(); michael@0: surface->Release(); michael@0: return gl::error(GL_OUT_OF_MEMORY); michael@0: } michael@0: michael@0: RECT sourceRect = {x, y, x + width, y + height}; michael@0: RECT destRect = {xoffset, yoffset, xoffset + width, yoffset + height}; michael@0: michael@0: D3DLOCKED_RECT sourceLock = {0}; michael@0: result = renderTargetData->LockRect(&sourceLock, &sourceRect, 0); michael@0: michael@0: if (FAILED(result)) michael@0: { michael@0: ERR("Failed to lock the source surface (rectangle might be invalid)."); michael@0: renderTargetData->Release(); michael@0: surface->Release(); michael@0: return gl::error(GL_OUT_OF_MEMORY); michael@0: } michael@0: michael@0: D3DLOCKED_RECT destLock = {0}; michael@0: result = lock(&destLock, &destRect); michael@0: michael@0: if (FAILED(result)) michael@0: { michael@0: ERR("Failed to lock the destination surface (rectangle might be invalid)."); michael@0: renderTargetData->UnlockRect(); michael@0: renderTargetData->Release(); michael@0: surface->Release(); michael@0: return gl::error(GL_OUT_OF_MEMORY); michael@0: } michael@0: michael@0: if (destLock.pBits && sourceLock.pBits) michael@0: { michael@0: unsigned char *source = (unsigned char*)sourceLock.pBits; michael@0: unsigned char *dest = (unsigned char*)destLock.pBits; michael@0: michael@0: switch (description.Format) michael@0: { michael@0: case D3DFMT_X8R8G8B8: michael@0: case D3DFMT_A8R8G8B8: michael@0: switch(getD3DFormat()) michael@0: { michael@0: case D3DFMT_X8R8G8B8: michael@0: case D3DFMT_A8R8G8B8: michael@0: for(int y = 0; y < height; y++) michael@0: { michael@0: memcpy(dest, source, 4 * width); michael@0: michael@0: source += sourceLock.Pitch; michael@0: dest += destLock.Pitch; michael@0: } michael@0: break; michael@0: case D3DFMT_L8: michael@0: for(int y = 0; y < height; y++) michael@0: { michael@0: for(int x = 0; x < width; x++) michael@0: { michael@0: dest[x] = source[x * 4 + 2]; michael@0: } michael@0: michael@0: source += sourceLock.Pitch; michael@0: dest += destLock.Pitch; michael@0: } michael@0: break; michael@0: case D3DFMT_A8L8: michael@0: for(int y = 0; y < height; y++) michael@0: { michael@0: for(int x = 0; x < width; x++) michael@0: { michael@0: dest[x * 2 + 0] = source[x * 4 + 2]; michael@0: dest[x * 2 + 1] = source[x * 4 + 3]; michael@0: } michael@0: michael@0: source += sourceLock.Pitch; michael@0: dest += destLock.Pitch; michael@0: } michael@0: break; michael@0: default: michael@0: UNREACHABLE(); michael@0: } michael@0: break; michael@0: case D3DFMT_R5G6B5: michael@0: switch(getD3DFormat()) michael@0: { michael@0: case D3DFMT_X8R8G8B8: michael@0: for(int y = 0; y < height; y++) michael@0: { michael@0: for(int x = 0; x < width; x++) michael@0: { michael@0: unsigned short rgb = ((unsigned short*)source)[x]; michael@0: unsigned char red = (rgb & 0xF800) >> 8; michael@0: unsigned char green = (rgb & 0x07E0) >> 3; michael@0: unsigned char blue = (rgb & 0x001F) << 3; michael@0: dest[x + 0] = blue | (blue >> 5); michael@0: dest[x + 1] = green | (green >> 6); michael@0: dest[x + 2] = red | (red >> 5); michael@0: dest[x + 3] = 0xFF; michael@0: } michael@0: michael@0: source += sourceLock.Pitch; michael@0: dest += destLock.Pitch; michael@0: } michael@0: break; michael@0: case D3DFMT_L8: michael@0: for(int y = 0; y < height; y++) michael@0: { michael@0: for(int x = 0; x < width; x++) michael@0: { michael@0: unsigned char red = source[x * 2 + 1] & 0xF8; michael@0: dest[x] = red | (red >> 5); michael@0: } michael@0: michael@0: source += sourceLock.Pitch; michael@0: dest += destLock.Pitch; michael@0: } michael@0: break; michael@0: default: michael@0: UNREACHABLE(); michael@0: } michael@0: break; michael@0: case D3DFMT_A1R5G5B5: michael@0: switch(getD3DFormat()) michael@0: { michael@0: case D3DFMT_X8R8G8B8: michael@0: for(int y = 0; y < height; y++) michael@0: { michael@0: for(int x = 0; x < width; x++) michael@0: { michael@0: unsigned short argb = ((unsigned short*)source)[x]; michael@0: unsigned char red = (argb & 0x7C00) >> 7; michael@0: unsigned char green = (argb & 0x03E0) >> 2; michael@0: unsigned char blue = (argb & 0x001F) << 3; michael@0: dest[x + 0] = blue | (blue >> 5); michael@0: dest[x + 1] = green | (green >> 5); michael@0: dest[x + 2] = red | (red >> 5); michael@0: dest[x + 3] = 0xFF; michael@0: } michael@0: michael@0: source += sourceLock.Pitch; michael@0: dest += destLock.Pitch; michael@0: } michael@0: break; michael@0: case D3DFMT_A8R8G8B8: michael@0: for(int y = 0; y < height; y++) michael@0: { michael@0: for(int x = 0; x < width; x++) michael@0: { michael@0: unsigned short argb = ((unsigned short*)source)[x]; michael@0: unsigned char red = (argb & 0x7C00) >> 7; michael@0: unsigned char green = (argb & 0x03E0) >> 2; michael@0: unsigned char blue = (argb & 0x001F) << 3; michael@0: unsigned char alpha = (signed short)argb >> 15; michael@0: dest[x + 0] = blue | (blue >> 5); michael@0: dest[x + 1] = green | (green >> 5); michael@0: dest[x + 2] = red | (red >> 5); michael@0: dest[x + 3] = alpha; michael@0: } michael@0: michael@0: source += sourceLock.Pitch; michael@0: dest += destLock.Pitch; michael@0: } michael@0: break; michael@0: case D3DFMT_L8: michael@0: for(int y = 0; y < height; y++) michael@0: { michael@0: for(int x = 0; x < width; x++) michael@0: { michael@0: unsigned char red = source[x * 2 + 1] & 0x7C; michael@0: dest[x] = (red << 1) | (red >> 4); michael@0: } michael@0: michael@0: source += sourceLock.Pitch; michael@0: dest += destLock.Pitch; michael@0: } michael@0: break; michael@0: case D3DFMT_A8L8: michael@0: for(int y = 0; y < height; y++) michael@0: { michael@0: for(int x = 0; x < width; x++) michael@0: { michael@0: unsigned char red = source[x * 2 + 1] & 0x7C; michael@0: dest[x * 2 + 0] = (red << 1) | (red >> 4); michael@0: dest[x * 2 + 1] = (signed char)source[x * 2 + 1] >> 7; michael@0: } michael@0: michael@0: source += sourceLock.Pitch; michael@0: dest += destLock.Pitch; michael@0: } michael@0: break; michael@0: default: michael@0: UNREACHABLE(); michael@0: } michael@0: break; michael@0: default: michael@0: UNREACHABLE(); michael@0: } michael@0: } michael@0: michael@0: unlock(); michael@0: renderTargetData->UnlockRect(); michael@0: michael@0: renderTargetData->Release(); michael@0: surface->Release(); michael@0: michael@0: mDirty = true; michael@0: } michael@0: michael@0: }