Example #1
0
HRESULT dxReadPixels(IDirect3DDevice9* Device, void* Buffer, bool& Minimised, int& Width, int& Height, D3DFORMAT Format)
{
    IDirect3DSurface9* RenderTarget = nullptr;
    IDirect3DSurface9* DestTarget = nullptr;
    HRESULT result = Device->GetRenderTarget(0, &RenderTarget);

    if (result == S_OK)
    {
        if (Width == 0 || Height == 0 || Format == D3DFMT_UNKNOWN)
        {
            D3DSURFACE_DESC descriptor = {};
            RenderTarget->GetDesc(&descriptor);
            Width = descriptor.Width;
            Height = descriptor.Height;
            Format = descriptor.Format;
        }

        HDC DC = nullptr;
        RenderTarget->GetDC(&DC);
        Minimised = IsIconic(WindowFromDC(DC));
        RenderTarget->ReleaseDC(DC);
        result = Device->CreateOffscreenPlainSurface(Width, Height, Format, D3DPOOL_SYSTEMMEM, &DestTarget, nullptr);
        result = Device->GetRenderTargetData(RenderTarget, DestTarget);

        D3DLOCKED_RECT rect;
        DestTarget->LockRect(&rect, 0, D3DLOCK_READONLY);
        memcpy(Buffer, rect.pBits, Width * Height * 4);
        DestTarget->UnlockRect();
    }

    SafeRelease(RenderTarget);
    SafeRelease(DestTarget);
    return result;
}
	void DirectX9Texture::setGrid(Grid<Color>* a_pGrid)
	{
		HRESULT hr=S_OK;
		Grid<Color>* pGrid=NULL;
		D3DSURFACE_DESC desc;
		m_pTexture->GetLevelDesc(0, &desc);
		//TODO: get texture format from color
		if(D3DFMT_A32B32G32R32F==desc.Format)
		{
			IDirect3DSurface9* offscreenSurface;
			hr = m_pRenderer->getDevice()->CreateOffscreenPlainSurface( desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &offscreenSurface, NULL );
			if( FAILED(hr) )
				return;

			IDirect3DSurface9* destinationSurface;
			m_pTexture->GetSurfaceLevel(0, &destinationSurface);

			m_iWidth=desc.Width;
			m_iHeight=desc.Height;

			D3DLOCKED_RECT lockedRect;
			HRESULT hr=S_OK;
			if(!FAILED(offscreenSurface->LockRect(&lockedRect, NULL, D3DLOCK_DISCARD)))
			{
				for(unsigned int y=0; y<m_iHeight; y++)
				{
					void* dst=((char*)lockedRect.pBits)+(y*lockedRect.Pitch);
					memcpy(dst, a_pGrid->getRawRowData(y), a_pGrid->getRawRowDataByteCount());
				}
				offscreenSurface->UnlockRect();
			}
			m_pRenderer->getDevice()->UpdateSurface(offscreenSurface, NULL, destinationSurface, NULL);
			offscreenSurface->Release();
		}
	}
//-----------------------------------------------------------------------------
// unLock
//-----------------------------------------------------------------------------
void GFXD3D9TextureObject::unlock(U32 mipLevel)
{
   AssertFatal( mLocked, "GFXD3D9TextureObject::unlock - Attempting to unlock a surface that has not been locked" );

#ifndef TORQUE_OS_XENON
   if( mProfile->isRenderTarget() )
   {
      IDirect3DSurface9 *dest;
      GFXD3D9TextureObject *to = (GFXD3D9TextureObject *) &(*mLockTex);
      D3D9Assert( to->get2DTex()->GetSurfaceLevel( 0, &dest ), NULL );

      dest->UnlockRect();
      dest->Release();

      mLocked = false;
   }
   else
#endif
   {
      D3D9Assert( get2DTex()->UnlockRect(mipLevel), 
         "GFXD3D9TextureObject::unlock - could not unlock non-RT texture." );

      mLocked = false;
   }
}
Example #4
0
	virtual IDirect3DTexture9* createTexture() {
		IDirect3DTexture9* tex = CFixedTextureCreator::createTexture();

		// create the offscreen surface
		HRESULT hr;
		CD3DDevice& dx = CD3DDevice::getInstance();
		IDirect3DSurface9* srcSurf = 0;
		hr = dx.getDevice().CreateOffscreenPlainSurface( mGameMap->getCellsX(), mGameMap->getCellsY(), D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &srcSurf, NULL );
		assert( SUCCEEDED(hr) );

		D3DLOCKED_RECT lr;
		srcSurf->LockRect( &lr, NULL, D3DLOCK_DISCARD );
		const char* linePtr = (const char*)lr.pBits;
		for( int y = 0; y < mGameMap->getCellsY(); ++y ) {
			D3DCOLOR* p = (D3DCOLOR*)linePtr;
			for( int x = 0; x < mGameMap->getCellsX(); ++x ) {
				const CGameMap::SCell& cell = mGameMap->getCell(x,y);
				*p = gColors.minimap[cell.color][cell.type];
				++p;
			}
			linePtr += lr.Pitch;
		}
		srcSurf->UnlockRect();

		// now, filter this into the texture
		IDirect3DSurface9* dstSurf = 0;
		hr = tex->GetSurfaceLevel( 0, &dstSurf );
		assert( SUCCEEDED(hr) );
		hr = D3DXLoadSurfaceFromSurface( dstSurf, NULL, NULL, srcSurf, NULL, NULL, D3DX_FILTER_BOX, 0 );
		dstSurf->Release();
		srcSurf->Release();

		D3DXFilterTexture( tex, NULL, 0, D3DX_FILTER_BOX );
		return tex;
	}
Example #5
0
HRESULT Dump2Surface (void* pDst[3], void* pSurface, int iWidth, int iHeight, int iStride[2]) {
  HRESULT hResult = E_FAIL;

  if (!pDst[0] || !pDst[1] || !pDst[2] || !pSurface)
    return hResult;

  IDirect3DSurface9* pSurfaceData = (IDirect3DSurface9*)pSurface;
  D3DLOCKED_RECT sD3DLockedRect = {0};
  hResult = pSurfaceData->LockRect (&sD3DLockedRect, NULL, 0);

  unsigned char* pInY = (unsigned char*)pDst[0];
  unsigned char* pOutY = (unsigned char*)sD3DLockedRect.pBits;
  int iOutStride = sD3DLockedRect.Pitch;

  for (int j = 0; j < iHeight; j++)
    memcpy (pOutY + j * iOutStride, pInY + j * iStride[0], iWidth); //confirmed_safe_unsafe_usage

  unsigned char* pInU = (unsigned char*)pDst[1];
  unsigned char* pInV = (unsigned char*)pDst[2];
  unsigned char* pOutC = pOutY + iOutStride * iHeight;
  for (int i = 0; i < iHeight / 2; i++) {
    for (int j = 0; j < iWidth; j += 2) {
      pOutC[i * iOutStride + j  ] = pInU[i * iStride[1] + j / 2];
      pOutC[i * iOutStride + j + 1] = pInV[i * iStride[1] + j / 2];
    }
  }

  pSurfaceData->UnlockRect();

  return hResult;
}
Example #6
0
RageSurface* RageDisplay_D3D::CreateScreenshot()
{
#if defined(XBOX)
	return NULL;
#else
	RageSurface * result = NULL;

	// Get the back buffer.
	IDirect3DSurface9* pSurface;
	if( SUCCEEDED( g_pd3dDevice->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &pSurface ) ) )
	{
		// Get the back buffer description.
		D3DSURFACE_DESC desc;
		pSurface->GetDesc( &desc );

		// Copy the back buffer into a surface of a type we support.
		IDirect3DSurface9* pCopy;
		if( SUCCEEDED( g_pd3dDevice->CreateOffscreenPlainSurface( desc.Width, desc.Height, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &pCopy, NULL ) ) )
		{
			if( SUCCEEDED( D3DXLoadSurfaceFromSurface( pCopy, NULL, NULL, pSurface, NULL, NULL, D3DX_FILTER_NONE, 0) ) )
			{
				// Update desc from the copy.
				pCopy->GetDesc( &desc );

				D3DLOCKED_RECT lr;

				{
					RECT rect;
					rect.left = 0;
					rect.top = 0;
					rect.right = desc.Width;
					rect.bottom = desc.Height;

					pCopy->LockRect( &lr, &rect, D3DLOCK_READONLY );
				}

				RageSurface *surface = CreateSurfaceFromPixfmt( FMT_RGBA8, lr.pBits, desc.Width, desc.Height, lr.Pitch);
				ASSERT( surface != NULL );

				// We need to make a copy, since lr.pBits will go away when we call UnlockRect().
				result = 
					CreateSurface( surface->w, surface->h,
						surface->format->BitsPerPixel,
						surface->format->Rmask, surface->format->Gmask,
						surface->format->Bmask, surface->format->Amask );
				RageSurfaceUtils::CopySurface( surface, result );
				delete surface;

				pCopy->UnlockRect();
			}

			pCopy->Release();
		}

		pSurface->Release();
	}

	return result;
#endif
}
QByteArray MFTransform::dataFromBuffer(IMFMediaBuffer *buffer, int height, int *bytesPerLine)
{
    QByteArray array;
    BYTE *bytes;
    DWORD length;
    HRESULT hr = buffer->Lock(&bytes, NULL, &length);
    if (SUCCEEDED(hr)) {
        array = QByteArray((const char *)bytes, (int)length);
        buffer->Unlock();
    } else {
        // try to lock as Direct3DSurface
        IDirect3DSurface9 *surface = 0;
        do {
            if (FAILED(MFGetService(buffer, MR_BUFFER_SERVICE, IID_IDirect3DSurface9, (void**)&surface)))
                break;

            D3DLOCKED_RECT rect;
            if (FAILED(surface->LockRect(&rect, NULL, D3DLOCK_READONLY)))
                break;

            if (bytesPerLine)
                *bytesPerLine = (int)rect.Pitch;

            array = QByteArray((const char *)rect.pBits, rect.Pitch * height);
            surface->UnlockRect();
        } while (false);

        if (surface) {
            surface->Release();
            surface = 0;
        }
    }

    return array;
}
void ModuleIrisGraphics::snap2Bitmap(IIrisBitmap *bitmap){
	IDirect3DSurface9* EXSurface;
	IDirect3DSurface9* ExchangeSurface;
	ExchangeTexture->GetSurfaceLevel(0, &EXSurface);	
	Device->CreateOffscreenPlainSurface(width, height, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &ExchangeSurface, 0);
	Device->GetRenderTargetData(EXSurface, ExchangeSurface);

	D3DLOCKED_RECT rc;
	memset(&rc, 0, sizeof(rc));
	ExchangeSurface->LockRect(&rc, 0, D3DLOCK_NOOVERWRITE);
	PARGBQuad p2 = (PARGBQuad)rc.pBits;
	PARGBQuad c2 = 0;
	IrisColor color(0, 0, 0, 0);
	for (int x = 0; x < width; x++){
		for (int y = 0; y < height; y++){
			c2 = p2 + x + y * rc.Pitch / sizeof(ARGBQuad);
			color.set(c2->Red, c2->Green, c2->Blue, c2->Alpha);
			bitmap->SetPixel(x, y, &color);
		}
	}

	ExchangeSurface->UnlockRect();
	EXSurface->Release();
	ExchangeSurface->Release();
}
void D3DXCompressorDXT1::compress(nvtt::InputFormat inputFormat, nvtt::AlphaMode alphaMode, uint w, uint h, uint d, void * data, const nvtt::CompressionOptions::Private & compressionOptions, const nvtt::OutputOptions::Private & outputOptions)
{
    nvDebugCheck(d == 1);

    IDirect3D9 * d3d = Direct3DCreate9(D3D_SDK_VERSION);

    D3DPRESENT_PARAMETERS presentParams;
    ZeroMemory(&presentParams, sizeof(presentParams));
    presentParams.Windowed = TRUE;
    presentParams.SwapEffect = D3DSWAPEFFECT_COPY;
    presentParams.BackBufferWidth = 8;
    presentParams.BackBufferHeight = 8;
    presentParams.BackBufferFormat = D3DFMT_UNKNOWN;

    HRESULT err;

    IDirect3DDevice9 * device = NULL;
    err = d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, GetDesktopWindow(), D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParams, &device);

    IDirect3DTexture9 * texture = NULL;
    err = D3DXCreateTexture(device, w, h, 1, 0, D3DFMT_DXT1, D3DPOOL_SYSTEMMEM, &texture);

    IDirect3DSurface9 * surface = NULL;
    err = texture->GetSurfaceLevel(0, &surface);

    RECT rect;
    rect.left = 0;
    rect.top = 0;
    rect.bottom = h;
    rect.right = w;

    if (inputFormat == nvtt::InputFormat_BGRA_8UB)
    {
        err = D3DXLoadSurfaceFromMemory(surface, NULL, NULL, data, D3DFMT_A8R8G8B8, w * 4, NULL, &rect, D3DX_DEFAULT, 0);
    }
    else
    {
        err = D3DXLoadSurfaceFromMemory(surface, NULL, NULL, data, D3DFMT_A32B32G32R32F, w * 16, NULL, &rect, D3DX_DEFAULT, 0);
    }

    if (err != D3DERR_INVALIDCALL && err != D3DXERR_INVALIDDATA)
    {
        D3DLOCKED_RECT rect;
        ZeroMemory(&rect, sizeof(rect));

        err = surface->LockRect(&rect, NULL, D3DLOCK_READONLY);

	    if (outputOptions.outputHandler != NULL) {
	        int size = rect.Pitch * ((h + 3) / 4);
	        outputOptions.outputHandler->writeData(rect.pBits, size);
	    }

        err = surface->UnlockRect();
    }

    surface->Release();
    device->Release();
    d3d->Release();
}
Example #10
0
bool CDVDCodecUtils::CopyDXVA2Picture(YV12Image* pImage, DVDVideoPicture *pSrc)
{
#ifdef HAS_DX
  // TODO: Optimize this later using shaders/swscale/etc.
  switch (pSrc->extended_format)
  {
    case MAKEFOURCC('N','V','1','2'):
      {
        IDirect3DSurface9* surface = (IDirect3DSurface9*)pSrc->data[3];

        D3DLOCKED_RECT rectangle;
        if (FAILED(surface->LockRect(&rectangle, NULL, 0)))
          return false;

        // Copy Y
        uint8_t* bits = (uint8_t*)(rectangle.pBits);
        uint8_t* d = pImage->plane[0];
        for (unsigned y = 0; y < pSrc->iHeight; y++)
        {
          memcpy(d, bits, pSrc->iWidth);
          bits += rectangle.Pitch;
          d += pImage->stride[0];
        }

        D3DSURFACE_DESC desc;
        if (FAILED(surface->GetDesc(&desc)))
          return false;
        
        // Copy packed UV
        uint8_t *s_uv = ((uint8_t*)(rectangle.pBits)) + desc.Height * rectangle.Pitch;
        uint8_t *d_uv = pImage->plane[1];
        for (unsigned y = 0; y < pSrc->iHeight >> 1; y++)
        {
          memcpy(d_uv, s_uv, pSrc->iWidth);
          s_uv += rectangle.Pitch;
          d_uv += pImage->stride[1];
        }

        if (FAILED(surface->UnlockRect()))
          return false;
      }
      return true;

    // Future...
    /*case MAKEFOURCC('Y','V','1','2'):
      return true;*/

    /*case MAKEFOURCC('Y','V','V','Y'):
      return true;*/

    default:
      CLog::Log(LOGWARNING, "CDVDCodecUtils::CopyDXVA2Picture colorspace not supported");
      return false;
  }
#endif
  return false;
}
	HRESULT virtual Update()
	{
		if (S_OK == this->ULib_BaseCanvas::Update() /*此处做静态绑定 etc.*/)
		{
			switch (m_typWrkMod)
			{
			case DataOutput:
				{
					//TODO.
					//See ULib_OpenGLCanvas.hpp.
					return S_OK;
				}
			case DataInput:
				{
					LPDIRECT3DDEVICE9 pDx9Device = GutGetGraphicsDeviceDX9();
					if (pDx9Device)
					{
						//创建一个渲染表面 
						IDirect3DSurface9* pSurface; 
						//此处参数与OpenGL不同, 由于Pixle对象为一个DWORD,而非BYTE,故宏定义使用ARGB表达,底层字节序按照小端存放为B,G,R,A, 依然与OpenGL相同(Win32平台下)
						pDx9Device->CreateOffscreenPlainSurface(m_canvasWidth, m_canvasHeight, (D3DFORMAT)m_dataFormat, D3DPOOL_DEFAULT, &pSurface,0);

						//上写锁
						D3DLOCKED_RECT rc;  
						memset(&rc,0,sizeof(rc));  
						pSurface->LockRect(&rc,NULL,D3DLOCK_DISCARD); 

						DWORD* pBits = (DWORD*)rc.pBits;
						memcpy(pBits, m_pTargetTexturePixels, m_canvasWidth * m_canvasHeight * sizeof(DWORD));

						//释放写锁
						pSurface->UnlockRect();
						//获取D3D默认交换链中的后台缓存
						IDirect3DSurface9 *backBuffer = 0; 
						pDx9Device->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer); 
						//使用自定义渲染表面填充后台缓存 
						pDx9Device->StretchRect(pSurface, 0, backBuffer, 0, D3DTEXF_NONE); 
						//GetBackBuffer所得的缓存需要被释放 
						backBuffer->Release(); 

						pDx9Device->Present(NULL, NULL, NULL, NULL);
						return S_OK;
					}else
					{
						return E_FAIL;
					}
				}
			default:
				{
					return E_FAIL;
				}
			}
		}else
		{
			return E_FAIL;
		}
	}
Example #12
0
/*
 * Class:     sun_java2d_d3d_D3DSurfaceData
 * Method:    dbGetPixelNative
 * Signature: (JII)I
 */
JNIEXPORT jint JNICALL Java_sun_java2d_d3d_D3DSurfaceData_dbGetPixelNative
  (JNIEnv *env, jclass clazz, jlong pData, jint x, jint y)
{
    HRESULT res;
    D3DSDOps *d3dsdo;
    D3DContext *pCtx;
    D3DPipelineManager *pMgr;
    D3DResource *pLockableRes;
    jint pixel = 0;

    J2dTraceLn(J2D_TRACE_INFO, "D3DSurfaceData_dbGetPixelNative");

    RETURN_STATUS_IF_NULL(d3dsdo = (D3DSDOps *)jlong_to_ptr(pData), pixel);
    RETURN_STATUS_IF_NULL(d3dsdo->pResource, pixel);
    RETURN_STATUS_IF_NULL(pMgr = D3DPipelineManager::GetInstance(), pixel);

    if (FAILED(res = pMgr->GetD3DContext(d3dsdo->adapter, &pCtx))) {
        D3DRQ_MarkLostIfNeeded(res, d3dsdo);
        return pixel;
    }
    RETURN_STATUS_IF_NULL(pCtx->GetResourceManager(), 0);

    IDirect3DDevice9 *pd3dDevice = pCtx->Get3DDevice();
    IDirect3DSurface9 *pSrc = d3dsdo->pResource->GetSurface();
    D3DFORMAT srcFmt = d3dsdo->pResource->GetDesc()->Format;

    pCtx->UpdateState(STATE_OTHEROP);

    res = pCtx->GetResourceManager()->
            GetLockableRTSurface(1, 1, srcFmt, &pLockableRes);
    if (SUCCEEDED(res)) {
        IDirect3DSurface9 *pTmpSurface;
        RECT srcRect = { x, y, x+1, y+1};
        RECT dstRect = { 0l, 0l, 1, 1 };

        pTmpSurface = pLockableRes->GetSurface();
        res = pd3dDevice->StretchRect(pSrc, &srcRect, pTmpSurface, &dstRect,
                                      D3DTEXF_NONE);
        if (SUCCEEDED(res)) {
            D3DLOCKED_RECT lRect;

            res = pTmpSurface->LockRect(&lRect, &dstRect, D3DLOCK_NOSYSLOCK);
            if (SUCCEEDED(res)) {
                if (srcFmt == D3DFMT_X8R8G8B8) {
                    pixel = *(jint*)lRect.pBits;
                } else {
                    pixel = *(unsigned short*)lRect.pBits;
                }
                pTmpSurface->UnlockRect();
            }
        }
    }
    D3DRQ_MarkLostIfNeeded(res, d3dsdo);

    return pixel;
}
Example #13
0
// copy a rect from the SDL surface to the Direct3D9 backbuffer
void CDirect3D::UpdateRectFromSDLSurface(int x,int y,int w,int h) {
	if (x < 0 || y < 0 || (unsigned int)(x+w) > d3dpp.BackBufferWidth || (unsigned int)(y+h) > d3dpp.BackBufferHeight)
		return;
	if (w <= 0 || h <= 0)
		return;

	IDirect3DSurface9 *bbsurf = NULL;
	IDirect3DSurface9 *tsurf = NULL;

	if (pD3DDevice9->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &bbsurf) == D3D_OK) {
		if (pD3DDevice9->CreateOffscreenPlainSurface(w, h, d3dpp.BackBufferFormat, D3DPOOL_SYSTEMMEM, &tsurf, NULL) == D3D_OK) {
			D3DLOCKED_RECT rl;

			if (tsurf->LockRect(&rl, NULL, 0) == D3D_OK) {
				unsigned char *GFX_GetSurfacePtr(size_t *pitch, unsigned int x, unsigned int y);
				void GFX_ReleaseSurfacePtr(void);

				size_t sdl_pitch = 0,sdl_copy;
				unsigned char *sdl_surface = GFX_GetSurfacePtr(&sdl_pitch, x, y);

				if (sdl_surface != NULL) {
					sdl_copy = w * (bpp16 ? 2 : 4);

//					fprintf(stderr,"sdl_copy=%u sdl_pitch=%u dxpitch=%u\n",
//						(unsigned int)sdl_copy,(unsigned int)sdl_pitch,(unsigned int)rl.Pitch);

					for (unsigned int iy=0;iy < (unsigned int)h;iy++) {
						unsigned char *sp = sdl_surface + (iy * sdl_pitch);
						unsigned char *dp = (unsigned char*)rl.pBits + (iy * rl.Pitch);

						memcpy(dp, sp, sdl_copy);
					}

					GFX_ReleaseSurfacePtr();
				}

				tsurf->UnlockRect();

				RECT rc;
				POINT pt;

				rc.top = 0;
				rc.left = 0;
				rc.right = w;
				rc.bottom = h;
				pt.x = 0;
				pt.y = 0;

				pD3DDevice9->UpdateSurface(/*source*/tsurf, &rc, /*dest*/bbsurf, &pt);
			}
		}
	}

	SAFE_RELEASE(bbsurf);
	SAFE_RELEASE(tsurf);
}
bool aiGraphicsDeviceD3D9::readTexture(void *outBuf, size_t bufsize, void *tex_, int width, int height, tTextureFormat format)
{
    HRESULT hr;
    IDirect3DTexture9 *tex = (IDirect3DTexture9*)tex_;

    // D3D11 と同様 render target の内容は CPU からはアクセス不可能になっている。
    // staging texture を用意してそれに内容を移し、CPU はそれ経由でデータを読む。
    IDirect3DSurface9 *surfDst = findOrCreateStagingTexture(width, height, format);
    if (surfDst == nullptr) { return false; }

    IDirect3DSurface9* surfSrc = nullptr;
    hr = tex->GetSurfaceLevel(0, &surfSrc);
    if (FAILED(hr)){ return false; }

    bool ret = false;
    hr = m_device->GetRenderTargetData(surfSrc, surfDst);
    if (SUCCEEDED(hr))
    {
        D3DLOCKED_RECT locked;
        hr = surfDst->LockRect(&locked, nullptr, D3DLOCK_READONLY);
        if (SUCCEEDED(hr))
        {
            char *wpixels = (char*)outBuf;
            int wpitch = width * tGetPixelSize(format);
            const char *rpixels = (const char*)locked.pBits;
            int rpitch = locked.Pitch;

            // D3D11 と同様表向き解像度と内部解像度が違うケースを考慮
            // (しかし、少なくとも手元の環境では常に wpitch == rpitch っぽい)
            if (wpitch == rpitch)
            {
                memcpy(wpixels, rpixels, bufsize);
            }
            else
            {
                for (int i = 0; i < height; ++i)
                {
                    memcpy(wpixels, rpixels, wpitch);
                    wpixels += wpitch;
                    rpixels += rpitch;
                }
            }
            surfDst->UnlockRect();

            // D3D9 の ARGB32 のピクセルの並びは BGRA になっているので並べ替える
            if (format == tTextureFormat_ARGB32) {
                BGRA2RGBA((RGBA<uint8_t>*)outBuf, int(bufsize / 4));
            }
            ret = true;
        }
    }

    surfSrc->Release();
    return ret;
}
Example #15
0
//! Regenerates the mip map levels of the texture. Useful after locking and
//! modifying the texture
void CD3D9Texture::regenerateMipMapLevels(void* mipmapData)
{
	if (mipmapData)
	{
		core::dimension2du size = TextureSize;
		u32 level=0;
		do
		{
			if (size.Width>1)
				size.Width /=2;
			if (size.Height>1)
				size.Height /=2;
			++level;
			IDirect3DSurface9* mipSurface = 0;
			HRESULT hr = Texture->GetSurfaceLevel(level, &mipSurface);
			if (FAILED(hr) || !mipSurface)
			{
				os::Printer::log("Could not get mipmap level", ELL_WARNING);
				return;
			}
			D3DSURFACE_DESC mipDesc;
			mipSurface->GetDesc(&mipDesc);
			D3DLOCKED_RECT miplr;

			// lock mipmap surface
			if (FAILED(mipSurface->LockRect(&miplr, NULL, 0)))
			{
				mipSurface->Release();
				os::Printer::log("Could not lock texture", ELL_WARNING);
				return;
			}

			memcpy(miplr.pBits, mipmapData, size.getArea()*getPitch()/TextureSize.Width);
			mipmapData = (u8*)mipmapData+size.getArea()*getPitch()/TextureSize.Width;
			// unlock
			mipSurface->UnlockRect();
			// release
			mipSurface->Release();
		} while (size.Width != 1 || size.Height != 1);
	}
	else if (HasMipMaps)
	{
		// create mip maps.
#ifdef _IRR_USE_D3DXFilterTexture_
		// The D3DXFilterTexture function seems to get linked wrong when
		// compiling with both D3D8 and 9, causing it not to work in the D3D9 device.
		// So mipmapgeneration is replaced with my own bad generation
		HRESULT hr  = D3DXFilterTexture(Texture, NULL, D3DX_DEFAULT, D3DX_DEFAULT);
		if (FAILED(hr))
#endif
		createMipMaps();
	}
}
Example #16
0
bool D3DVideo::read_viewport(uint8_t *buffer)
{
   RARCH_PERFORMANCE_INIT(d3d_read_viewport);
   RARCH_PERFORMANCE_START(d3d_read_viewport);
   bool ret = true;
   IDirect3DSurface9 *target = nullptr;
   IDirect3DSurface9 *dest   = nullptr;

   if (FAILED(Callback::d3d_err = dev->GetRenderTarget(0, &target)))
   {
      ret = false;
      goto end;
   }

   if (FAILED(Callback::d3d_err = dev->CreateOffscreenPlainSurface(screen_width, screen_height,
        D3DFMT_X8R8G8B8, D3DPOOL_SYSTEMMEM,
        &dest, nullptr)))
   {
      ret = false;
      goto end;
   }

   if (FAILED(Callback::d3d_err = dev->GetRenderTargetData(target, dest)))
   {
      ret = false;
      goto end;
   }

   D3DLOCKED_RECT rect;
   if (SUCCEEDED(dest->LockRect(&rect, nullptr, D3DLOCK_READONLY)))
   {
      unsigned pitchpix = rect.Pitch / 4;
      const uint32_t *pixels = (const uint32_t*)rect.pBits;
      pixels += final_viewport.X;
      pixels += (final_viewport.Height - 1) * pitchpix;
      pixels -= final_viewport.Y * pitchpix;

      for (unsigned y = 0; y < final_viewport.Height; y++, pixels -= pitchpix)
      {
         for (unsigned x = 0; x < final_viewport.Width; x++)
         {
            *buffer++ = (pixels[x] >>  0) & 0xff;
            *buffer++ = (pixels[x] >>  8) & 0xff;
            *buffer++ = (pixels[x] >> 16) & 0xff;
         }
      }

      dest->UnlockRect();
   }
Example #17
0
bool initDirect3DTextures()
{
    // Note: sharing is not supported on some platforms
    if (FAILED(dev->CreateOffscreenPlainSurface(WIDTH, HEIGHT, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &pSurface, NULL/*&surfaceShared*/)))
    {
        std::cerr << "Can't create surface for result" << std::endl;
        return false;
    }

    // Note: sharing is not supported on some platforms
    if (FAILED(dev->CreateOffscreenPlainSurface(WIDTH, HEIGHT, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &pReadOnlySurface, NULL/*&readOnlySurfaceShared*/)))
    {
        std::cerr << "Can't create read only surface" << std::endl;
        return false;
    }
    else
    {
        IDirect3DSurface9* pTmpSurface;
        if (FAILED(dev->CreateOffscreenPlainSurface(WIDTH, HEIGHT, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &pTmpSurface, NULL)))
        {
            std::cerr << "Can't create temp surface for CPU write" << std::endl;
            return false;
        }

        D3DLOCKED_RECT memDesc = {0, NULL};
        RECT rc = {0, 0, WIDTH, HEIGHT};
        if (SUCCEEDED(pTmpSurface->LockRect(&memDesc, &rc, 0)))
        {
            cv::Mat m(cv::Size(WIDTH, HEIGHT), CV_8UC4, memDesc.pBits, (int)memDesc.Pitch);
            getInputTexture().copyTo(m);
            pTmpSurface->UnlockRect();
            dev->StretchRect(pTmpSurface, NULL, pReadOnlySurface, NULL, D3DTEXF_NONE);
        }
        else
        {
            std::cerr << "Can't LockRect() on surface" << std::endl;
        }
        pTmpSurface->Release();
    }

    if (FAILED(dev->CreateOffscreenPlainSurface(WIDTH, HEIGHT, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &pCPUWriteSurface, NULL)))
    {
        std::cerr << "Can't create surface for CPU write" << std::endl;
        return false;
    }

    return true;
}
Example #18
0
// copy a rect to the SDL surface from the Direct3D9 backbuffer
void CDirect3D::UpdateRectToSDLSurface(int x,int y,int w,int h) {
	if (x < 0 || y < 0 || (unsigned int)(x+w) > d3dpp.BackBufferWidth || (unsigned int)(y+h) > d3dpp.BackBufferHeight)
		return;
	if (w <= 0 || h <= 0)
		return;

	IDirect3DSurface9 *bbsurf = NULL;
	IDirect3DSurface9 *tsurf = NULL;

	if (pD3DDevice9->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &bbsurf) == D3D_OK) {
		/* NTS: Microsoft doesn't seem to offer a way to capture only a part of the backbuffer :( */
		if (pD3DDevice9->CreateOffscreenPlainSurface(d3dpp.BackBufferWidth, d3dpp.BackBufferHeight, d3dpp.BackBufferFormat, D3DPOOL_SYSTEMMEM, &tsurf, NULL) == D3D_OK) {
			D3DLOCKED_RECT rl;

			if (pD3DDevice9->GetRenderTargetData(/*source*/bbsurf, /*dest*/tsurf) != D3D_OK)
				fprintf(stderr,"FAIL\n");

			if (tsurf->LockRect(&rl, NULL, 0) == D3D_OK) {
				unsigned char *GFX_GetSurfacePtr(size_t *pitch, unsigned int x, unsigned int y);
				void GFX_ReleaseSurfacePtr(void);

				size_t sdl_pitch = 0,sdl_copy;
				unsigned char *sdl_surface = GFX_GetSurfacePtr(&sdl_pitch, x, y);

				if (sdl_surface != NULL) {
					sdl_copy = w * (bpp16 ? 2 : 4);

//					fprintf(stderr,"sdl_copy=%u sdl_pitch=%u dxpitch=%u\n",
//						(unsigned int)sdl_copy,(unsigned int)sdl_pitch,(unsigned int)rl.Pitch);

					for (unsigned int iy=0;iy < (unsigned int)h;iy++) {
						unsigned char *sp = (unsigned char*)rl.pBits + ((iy + y) * rl.Pitch) + (x * (bpp16 ? 2 : 4));
						unsigned char *dp = sdl_surface + (iy * sdl_pitch);

						memcpy(dp, sp, sdl_copy);
					}

					GFX_ReleaseSurfacePtr();
				}

				tsurf->UnlockRect();
			}
		}
	}

	SAFE_RELEASE(bbsurf);
	SAFE_RELEASE(tsurf);
}
Example #19
0
void CSnapShot::GetFrontBufferPixels(UINT uiSizeX, UINT uiSizeY,unsigned char* buffer)
{
	// Get our d3d device
	IDirect3DDevice9 * pDevice = g_pCore->GetGraphics()->GetDevice();

	// Get our display mode
	D3DDISPLAYMODE displayMode;
	pDevice->GetDisplayMode(0, &displayMode);

	// Create our surface
	IDirect3DSurface9 * pSurface = nullptr;
	pDevice->CreateOffscreenPlainSurface(displayMode.Width, displayMode.Height, SCREEN_SHOT_FORMAT, D3DPOOL_SCRATCH, &pSurface, nullptr);

	if(pSurface)
	{
		pDevice->GetFrontBufferData(0, pSurface);

		// Create the client rect
		RECT clientRect;
		{
			POINT clientPoint;
			clientPoint.x = 0;
			clientPoint.y = 0;
			ClientToScreen(*(HWND *)COffsets::VAR_HWnd, &clientPoint);
			clientRect.left   = clientPoint.x;
			clientRect.top	  = clientPoint.y;
			clientRect.right  = (clientRect.left + displayMode.Width);
			clientRect.bottom = (clientRect.top  + displayMode.Height);
		}

		D3DLOCKED_RECT lockedRect;
		HRESULT hr = pSurface->LockRect(&lockedRect, NULL, D3DLOCK_READONLY | D3DLOCK_NOSYSLOCK | D3DLOCK_DONOTWAIT);
	
		if(SUCCEEDED(hr))
		{
			void* pBits = lockedRect.pBits;
			UINT ms_ulPitch = lockedRect.Pitch;

			for(unsigned int i = 0; i < displayMode.Height; ++i)
				memcpy(buffer + (displayMode.Width * 4) * i, (BYTE*)pBits + i * ms_ulPitch, (displayMode.Width * 4));
		}

		pSurface->UnlockRect();
		pSurface->Release();
	}
}
bool aiGraphicsDeviceD3D9::writeTexture(void *outTex, int width, int height, tTextureFormat format, const void *buf, size_t bufsize)
{
    int psize = tGetPixelSize(format);
    int pitch = psize * width;
    const size_t numPixels = bufsize / psize;

    HRESULT hr;
    IDirect3DTexture9 *tex = (IDirect3DTexture9*)outTex;

    // D3D11 と違い、D3D9 では書き込みも staging texture を経由する必要がある。
    IDirect3DSurface9 *surfSrc = findOrCreateStagingTexture(width, height, format);
    if (surfSrc == nullptr) { return false; }

    IDirect3DSurface9* surfDst = nullptr;
    hr = tex->GetSurfaceLevel(0, &surfDst);
    if (FAILED(hr)){ return false; }

    bool ret = false;
    D3DLOCKED_RECT locked;
    hr = surfSrc->LockRect(&locked, nullptr, D3DLOCK_DISCARD);
    if (SUCCEEDED(hr))
    {
        const char *rpixels = (const char*)buf;
        int rpitch = psize * width;
        char *wpixels = (char*)locked.pBits;
        int wpitch = locked.Pitch;

        // こちらも ARGB32 の場合 BGRA に並べ替える必要がある
        if (format == tTextureFormat_ARGB32) {
            CopyWithBGRA2RGBA((RGBA<uint8_t>*)wpixels, (RGBA<uint8_t>*)rpixels, int(bufsize / 4));
        }
        else {
            memcpy(wpixels, rpixels, bufsize);
        }
        surfSrc->UnlockRect();

        hr = m_device->UpdateSurface(surfSrc, nullptr, surfDst, nullptr);
        if (SUCCEEDED(hr)) {
            ret = true;
        }
    }
    surfDst->Release();

    return false;
}
static inline void d3d9_shmem_capture(IDirect3DSurface9 *backbuffer)
{
	D3DTEXTUREFILTERTYPE filter;
	IDirect3DSurface9 *copy;
	int next_tex;
	HRESULT hr;

	d3d9_shmem_capture_queue_copy();

	next_tex = (data.cur_tex == NUM_BUFFERS - 1) ?  0 : data.cur_tex + 1;
	filter = data.using_scale ? D3DTEXF_LINEAR : D3DTEXF_NONE;
	copy = data.render_targets[data.cur_tex];

	hr = data.device->StretchRect(backbuffer, nullptr, copy, nullptr,
			filter);

	if (FAILED(hr)) {
		hlog_hr("d3d9_shmem_capture: StretchRect failed", hr);
		return;
	}

	if (data.copy_wait < NUM_BUFFERS - 1) {
		data.copy_wait++;
	} else {
		IDirect3DSurface9 *src = data.render_targets[next_tex];
		IDirect3DSurface9 *dst = data.copy_surfaces[next_tex];

		if (shmem_texture_data_lock(next_tex)) {
			dst->UnlockRect();
			data.texture_mapped[next_tex] = false;
			shmem_texture_data_unlock(next_tex);
		}

		hr = data.device->GetRenderTargetData(src, dst);
		if (FAILED(hr)) {
			hlog_hr("d3d9_shmem_capture: GetRenderTargetData "
			        "failed", hr);
		}

		data.queries[next_tex]->Issue(D3DISSUE_END);
		data.issued_queries[next_tex] = true;
	}

	data.cur_tex = next_tex;
}
Example #22
0
void AmjuGLDX9::GetScreenshot(unsigned char* buffer, int w, int h)
{
  AMJU_CALL_STACK;

  IDirect3DSurface9* pSurface = 0;

  dd->GetBackBuffer(
    0, // UINT  iSwapChain,
    0, // UINT BackBuffer,
    D3DBACKBUFFER_TYPE_MONO, // D3DBACKBUFFER_TYPE Type,
    &pSurface); //IDirect3DSurface9 ** ppBackBuffer

  D3DSURFACE_DESC desc;
  pSurface->GetDesc(&desc);

  D3DLOCKED_RECT lockedRect;
  HRESULT hr = pSurface->LockRect(&lockedRect, NULL, D3DLOCK_READONLY);
  if (SUCCEEDED(hr))
  {
    // Do we know if we are windowed or not ? We have to crop the window edges if so.

    // Copy surface into buffer. 
    // NB Buffer format is R8G8B8
    //h = MIN(h, desc.Height);
    //w = MIN(w, desc.Width);
    for (int i = 0; i < h; i++)
    {
      unsigned char* rowStart = (unsigned char*)lockedRect.pBits + i * lockedRect.Pitch;
      unsigned char* bufStart = buffer + i * w * 3;
      for (int j = 0; j < w; j++)
      {
        bufStart[0] = rowStart[0]; 
        bufStart[1] = rowStart[1]; 
        bufStart[2] = rowStart[2];
        bufStart += 3;
        rowStart += 4;
      }
    }
  }

  pSurface->UnlockRect(); // even if LockRect failed ?
  pSurface->Release(); // ?
}
Example #23
0
		Gwen::Color DirectX9::PixelColour( Gwen::Texture* pTexture, unsigned int x, unsigned int y, const Gwen::Color & col_default )
		{
			IDirect3DTexture9* pImage = ( IDirect3DTexture9* ) pTexture->data;

			if ( !pImage ) { return col_default; }

			IDirect3DSurface9* pSurface = NULL;

			if ( pImage->GetSurfaceLevel( 0, &pSurface ) != S_OK ) { return col_default; }

			if ( !pSurface ) { return col_default; }

			D3DLOCKED_RECT lockedRect;
			pSurface->LockRect( &lockedRect, NULL, D3DLOCK_READONLY );
			DWORD* pixels = ( DWORD* ) lockedRect.pBits;
			D3DXCOLOR color = pixels[lockedRect.Pitch / sizeof( DWORD ) * y + x];
			pSurface->UnlockRect();
			pSurface->Release();
			return Gwen::Color( color.r * 255, color.g * 255, color.b * 255, color.a * 255 );
		}
HRESULT CSurfaceQueueDeviceD3D9::UnlockSurface(IUnknown* pSurface)
{
    ASSERT(pSurface);

    HRESULT             hr          = S_OK;
    IDirect3DSurface9*  pSurf       = NULL;

    if (FAILED(hr = pSurface->QueryInterface(__uuidof(IDirect3DSurface9), (void**)&pSurf)))
    {
        goto end;
    }

    hr = pSurf->UnlockRect();

end:
    if (pSurf)
    {
        pSurf->Release();
    }
    return hr;
}
Example #25
0
static IDirect3DSurface9* LoadTexture(IDirect3DDevice9Ex* device, const PTexture& texture)
{
    assert(device);
    HRESULT hr;
    IDirect3DSurface9* pSurface;

    D3DFORMAT fmt = PixelFormats[static_cast<uint>(texture.pixelFormat)];
    hr = device->CreateOffscreenPlainSurface(texture.width, texture.height,
        fmt, D3DPOOL_SYSTEMMEM, &pSurface, NULL);
    if (FAILED(hr))
        throw graphics_device_exception("Failed to create offscreen surface: " + string(DXGetErrorDescriptionA(hr)));


    D3DLOCKED_RECT lockedRect;
    pSurface->LockRect(&lockedRect, NULL, D3DLOCK_DISCARD);
    // FIXME pitch?
    memcpy(lockedRect.pBits, texture.bits, texture.bitsSize);
    pSurface->UnlockRect();

    return pSurface;
}
Example #26
0
//-----------------------------------------------------------------------------
// save screen to file
void mgDX9Display::screenShot(
  const char* fileName)
{
  HRESULT hr;

  IDirect3DSurface9* targetSurface;

  // reads the entire screen
  hr = mg_d3dDevice->CreateOffscreenPlainSurface(
    m_graphicsWidth, m_graphicsHeight, 
    D3DFMT_X8R8G8B8, 
    D3DPOOL_SYSTEMMEM, 
    &targetSurface, 
    NULL);

  if (FAILED(hr))
    throw new mgException("Failed to create offscreen surface.");

  IDirect3DSurface9* backBuffer;
  hr = mg_d3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer);
  if (FAILED(hr))
    throw new mgException("Failed to GetBackBuffer.");

  hr = mg_d3dDevice->GetRenderTargetData(backBuffer, targetSurface);
  if (FAILED(hr))
    throw new mgException("Failed to GetFrontBufferData.");

  D3DLOCKED_RECT lockedRect;
  hr = targetSurface->LockRect(&lockedRect, NULL, D3DLOCK_READONLY);
  if (FAILED(hr))
    throw new mgException("Failed to lock surface.");

  BYTE* source = (BYTE*) lockedRect.pBits;
  
  if (!mgWriteBGRAtoJPGFile(fileName, m_graphicsWidth, m_graphicsHeight, source))
    MessageBox(NULL, "Unable to write jpg file.", "Save Scene", MB_OK | MB_ICONINFORMATION);

  hr = targetSurface->UnlockRect();
  targetSurface->Release();
}
	Grid<Color>* DirectX9Texture::getGrid(void)
	{
		if(!m_pTexture)
			return NULL;

		HRESULT hr=S_OK;
		Grid<Color>* pGrid=NULL;
		D3DSURFACE_DESC desc;
		m_pTexture->GetLevelDesc(0, &desc);
		//TODO: get texture format from color
		if(D3DFMT_A32B32G32R32F==desc.Format)
		{
			IDirect3DSurface9* offscreenSurface;
			hr = m_pRenderer->getDevice()->CreateOffscreenPlainSurface( desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &offscreenSurface, NULL );
			if( FAILED(hr) )
				return NULL;

			IDirect3DSurface9* sourceSurface;
			m_pTexture->GetSurfaceLevel(0, &sourceSurface);
			hr = m_pRenderer->getDevice()->GetRenderTargetData(sourceSurface, offscreenSurface );

			m_iWidth=desc.Width;
			m_iHeight=desc.Height;

			D3DLOCKED_RECT lockedRect;
			if(!FAILED(offscreenSurface->LockRect(&lockedRect, NULL, D3DLOCK_READONLY)))
			{
				pGrid=new Grid<Color>(m_iWidth, m_iHeight);
				for(unsigned int y=0; y<m_iHeight; y++)
				{
					void* src=((char*)lockedRect.pBits)+(y*lockedRect.Pitch);
					memcpy(pGrid->getRawRowData(y), src, pGrid->getRawRowDataByteCount());
				}
				offscreenSurface->UnlockRect();			
			}		
			offscreenSurface->Release();
		}
		return pGrid;
	}
Example #28
0
mfxStatus D3DFrameAllocator::UnlockFrame(mfxMemId mid, mfxFrameData *ptr)
{
    if (!mid)
        return MFX_ERR_NULL_PTR;

    mfxHDLPair *dxmid = (mfxHDLPair*)mid;
    IDirect3DSurface9 *pSurface = static_cast<IDirect3DSurface9*>(dxmid->first);
    if (pSurface == 0)
        return MFX_ERR_INVALID_HANDLE;

    pSurface->UnlockRect();

    if (NULL != ptr)
    {
        ptr->Pitch = 0;
        ptr->Y     = 0;
        ptr->U     = 0;
        ptr->V     = 0;
    }

    return MFX_ERR_NONE;
}
Example #29
0
CubemapFaceD3D9* CubemapFaceD3D9::create(IDirect3DTexture9* texturePtr,
        int index,
        Allocator& allocator)
{
    D3DSURFACE_DESC textureDescription;
    HRESULT hr = texturePtr->GetLevelDesc(0, &textureDescription);

    UINT width = textureDescription.Width;
    UINT height = textureDescription.Height;
    D3DFORMAT format = textureDescription.Format;

    IDirect3DSurface9* gpuSurfacePtr;
    IDirect3DSurface9* cpuSurfacePtr;

    hr = texturePtr->GetSurfaceLevel(0, &gpuSurfacePtr);

    hr = g_D3D9Device->CreateOffscreenPlainSurface(width, height, format,
            D3DPOOL_SYSTEMMEM, &cpuSurfacePtr, NULL);

    D3DLOCKED_RECT lockedRect;
    ZeroMemory(&lockedRect, sizeof(D3DLOCKED_RECT));
    hr = cpuSurfacePtr->LockRect(&lockedRect, 0, D3DLOCK_READONLY);
    hr = cpuSurfacePtr->UnlockRect();

    void* pixels = allocator.allocate(width * height * 4);
    void* addr = allocator.allocate(sizeof(CubemapFaceD3D9));

    return new (addr) CubemapFaceD3D9(width,
                                      height,
                                      index,
                                      boost::chrono::system_clock::now(),
                                      pixels,
                                      allocator,
                                      texturePtr,
                                      gpuSurfacePtr,
                                      cpuSurfacePtr,
                                      textureDescription.Format,
                                      lockedRect);
}
	bool RenderTargetD3D9Texture::getTextureData(void * pData)
	{
		if( 0 == m_pTexture || 0 == pData )
			return false;

		IDirect3DTexture9* pSrcTexture = m_pTexture->getD3DTexture();
		if( 0 == pSrcTexture )
			return false;
		D3DSURFACE_DESC srcTexDesc;
		pSrcTexture->GetLevelDesc(0, &srcTexDesc);

		IDirect3DTexture9* pDestTex =0;
		IDirect3DDevice9* pD3D9Device = m_pRenderSystem->getD3D9Device()->getDevice();
		HRESULT hr = pD3D9Device->CreateTexture( srcTexDesc.Width,
			srcTexDesc.Height,
			0,
			0,
			srcTexDesc.Format,
			D3DPOOL_SYSTEMMEM,
			&pDestTex,
			0);
		if( FAILED(hr))
		{
			return false;
		}

		IDirect3DSurface9* pSrcSurf  =0;
		pSrcTexture->GetSurfaceLevel(0, &pSrcSurf);
		IDirect3DSurface9* pDestSurf = 0;
		pDestTex->GetSurfaceLevel(0, &pDestSurf);
		hr = pD3D9Device->GetRenderTargetData( pSrcSurf, pDestSurf);
		if( FAILED(hr))
		{
			COM_SAFE_RELEASE(pSrcSurf);
			COM_SAFE_RELEASE(pDestSurf);
			COM_SAFE_RELEASE(pDestTex);
			return false;
		}
		else
		{
			COM_SAFE_RELEASE(pSrcSurf);
		}

		D3DSURFACE_DESC destSurfDesc;
		pDestSurf->GetDesc(&destSurfDesc);
		D3DLOCKED_RECT lockrect;
		pDestSurf->LockRect(&lockrect, NULL, D3DLOCK_READONLY);

		if( m_pTexture->hasAlpha() )
		{
			if( destSurfDesc.Format == D3DFMT_A8B8G8R8 )
			{
				uchar* pDesData = (uchar*)pData;
				for( UINT i=0; i < destSurfDesc.Height; ++i)
				{
					uchar* pSrcData = (uchar*)lockrect.pBits + i * lockrect.Pitch;
					for( UINT j =0; j<destSurfDesc.Width; ++j)
					{
						uint startAddr = j * 4;
						pDesData[0] = pSrcData[0];//red
						pDesData[1] = pSrcData[1];//green;
						pDesData[2] = pSrcData[2];//blue;
						pDesData[3] = pSrcData[3];//alpha
						pDesData +=4;
						pSrcData +=4;
					}
				}
			}
			else
			{
				pDestSurf->UnlockRect();
				COM_SAFE_RELEASE(pDestSurf);
				COM_SAFE_RELEASE(pDestTex);
				return false;
			}
		}
		else
		{
			if( destSurfDesc.Format == D3DFMT_R8G8B8)
			{
				uchar* pDesData = (uchar*)pData;
				for( UINT i=0; i < destSurfDesc.Height; ++i)
				{
					uchar* pSrcData = (uchar*)lockrect.pBits + i * lockrect.Pitch;
					for( UINT j =0; j<destSurfDesc.Width; ++j)
					{
						pDesData[0] = pSrcData[2];//red
						pDesData[1] = pSrcData[1];//green;
						pDesData[2] = pSrcData[0];//blue;
						pDesData +=3;
						pSrcData +=3;
					}
				}
			}
			else if( destSurfDesc.Format == D3DFMT_X8R8G8B8)
			{
				uchar* pDesData = (uchar*)pData;
				for( int i= destSurfDesc.Height-1; i >=0  ;  --i)
				{
					uchar* pSrcData = (uchar*)lockrect.pBits + i * lockrect.Pitch;
					for( UINT j =0; j<destSurfDesc.Width; ++j)
					{
						pDesData[0] = pSrcData[2];//red
						pDesData[1] = pSrcData[1];//green;
						pDesData[2] = pSrcData[0];//blue;
						pDesData +=3;
						pSrcData +=4;
					}
				}
			}
			else
			{
				pDestSurf->UnlockRect();
				COM_SAFE_RELEASE(pDestSurf);
				COM_SAFE_RELEASE(pDestTex);
				return false;
			}
		}

		pDestSurf->UnlockRect();
		COM_SAFE_RELEASE(pDestSurf);
		COM_SAFE_RELEASE(pDestTex);
		return true;
	}