/** * * @author Jade Abbott * */ bool CRadialMenu::CreateFinalTexture() { assert(m_pRenderer); IDirect3DTexture9* pTexture = 0; if (FAILED(m_pRenderer->GetSurfaceManager().CreateTexture(m_uiDiameter, m_uiDiameter, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &pTexture))) { return false; } CTextureManager& rTextureManager = m_pRenderer->GetTextureManager(); if (m_iFinalTextureID != Utility::INVALID_ID) { rTextureManager.RemoveTexture(m_iFinalTextureID); } m_iFinalTextureID = rTextureManager.LoadTextureMemory(pTexture); if (m_iFinalTextureID == Utility::INVALID_ID) { pTexture->Release(); return false; } return true; }
ImagePtr D3D9VideoBufferManager::CreateImage(int iWidth, int iHeight, FORMAT Format) { IDirect3DTexture9 * texture; HRESULT hr = D3DXCreateTexture(mD3D9Device, iWidth, iHeight, 1, 0, D3D9Mapping::GetD3DFormat(Format), D3DPOOL_SYSTEMMEM, &texture); D3DErrorExceptionFunction(D3DXCreateTexture, hr); IDirect3DSurface9 * surface; texture->GetSurfaceLevel(0, &surface); D3DSURFACE_DESC desc; surface->GetDesc(&desc); D3D9Image * image = new D3D9Image(); image->mWidth = desc.Width; image->mHeight = desc.Height; image->mSrcWidth = iWidth; image->mSrcHeight = iHeight; image->mFormat = D3D9Mapping::GetFormat(desc.Format); image->mMipmapLevel = texture->GetLevelCount(); image->mD3D9Texture = texture; surface->Release(); return ImagePtr(image); }
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; }
void D3D9Sprite::RecoverFromBackup() { if (!m_pBackupTexture) return; m_texture = m_video.lock()->CreateRenderTargetTexture(static_cast<unsigned int>(m_size.x), static_cast<unsigned int>(m_size.y), m_targetFormat); IDirect3DTexture9* pBackup = m_pBackupTexture; IDirect3DTexture9* pActualTexture; try { pActualTexture = boost::any_cast<IDirect3DTexture9*>(m_texture->GetTextureObject()); } catch (const boost::bad_any_cast &) { std::wstringstream ss; ss << L"D3D9Sprite::RecoverFromBackup Invalid texture pointer" << std::endl; ShowMessage(ss, GSMT_ERROR); return; } IDirect3DSurface9* pActualSurface = NULL; IDirect3DSurface9* pBackupSurf = NULL; pActualTexture->GetSurfaceLevel(0, &pActualSurface); pBackup->GetSurfaceLevel(0, &pBackupSurf); m_pDevice->UpdateSurface(pBackupSurf, NULL, pActualSurface, NULL); pBackupSurf->Release(); pActualSurface->Release(); GetInternalData(); }
HRESULT CDXSurfaceDevice9::OpenShareSurface(HANDLE hShare, IUnknown **ppSurface, UINT w, UINT h, D3DFORMAT format) { DXGI_FORMAT DxgiFormat; // If the format is not cross api shareable the utility function will return // D3DFMT_UNKNOWN if ((DxgiFormat = D3D9FormatToCrossAPIDXGI(format)) == D3DFMT_UNKNOWN) { return E_INVALIDARG; } HRESULT hr = S_OK; IDirect3DTexture9 *pTexture = NULL; hr = m_pDevice->CreateTexture(w, h, 1, D3DUSAGE_RENDERTARGET, format, D3DPOOL_DEFAULT, &pTexture, &hShare); if (S_OK == hr && pTexture) { // Store the shared handle hr = pTexture->SetPrivateData(SharedHandleGuid, &hShare, sizeof(HANDLE), 0); if (S_OK != hr) hr = GetSurfaceFromTexture(pTexture, (IDirect3DSurface9 **)ppSurface); SAFE_RELEASE(pTexture) }
// Called by Rocket when a texture is required to be built from an internally-generated sequence of pixels. bool GuiRenderer::GenerateTexture(Rocket::Core::TextureHandle& textureHandle, const unsigned char* source, const Rocket::Core::Vector2i& sourceDimensions) { // Create a Direct3DTexture9, which will be set as the texture handle. Note that we only create one surface for // this texture; because we're rendering in a 2D context, mip-maps are not required. IDirect3DTexture9* texture; guiManager->getGraphicsDevice().resource->CreateTexture(sourceDimensions.x, sourceDimensions.y, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &texture, NULL); // Lock the top surface and write the pixel data onto it. D3DLOCKED_RECT lockedRect; texture->LockRect(0, &lockedRect, 0, 0); for (int y = 0; y < sourceDimensions.y; ++y) { for (int x = 0; x < sourceDimensions.x; ++x) { const unsigned char* sourcePixel = source + (sourceDimensions.x * 4 * y) + (x * 4); unsigned char* destinationPixel = ((unsigned char*) lockedRect.pBits) + lockedRect.Pitch * y + x * 4; destinationPixel[0] = sourcePixel[2]; destinationPixel[1] = sourcePixel[1]; destinationPixel[2] = sourcePixel[0]; destinationPixel[3] = sourcePixel[3]; } } texture->UnlockRect(0); // Set the handle on the Rocket texture structure. textureHandle = (Rocket::Core::TextureHandle)texture; return true; }
HRESULT VMRSurfaceAllocator::PresentToTexture( VMR9PresentationInfo *lpPresInfo ) { HRESULT hr; IDirect3DTexture9 *lpTexture; IDirect3DSurface9 *lpSurface; if ( m_alpDirect3DTexture[ 0 ] != NULL ) { clock->Lock(); lpTexture = m_alpDirect3DTexture[ m_nFilpTexNum ]; hr = lpTexture->GetSurfaceLevel( 0, &lpSurface ); if ( hr != S_OK ) { return hr; } hr = D3DDev->StretchRect( lpPresInfo->lpSurf, NULL, lpSurface, NULL, D3DTEXF_NONE ); if ( hr != S_OK ) { lpSurface->Release(); return hr; } lpSurface->Release(); texnum = m_nFilpTexNum; m_nFilpTexNum = m_nFilpTexNum ^ 0x01; clock->Unlock(); } else { m_nFilpTexNum = -1; } return S_OK; }
void CD3D9RenderTarget::generateSurfaces() { for (u32 i = 0; i < Surface.size(); ++i) { if (!Surface[i] && Texture[i]) { IDirect3DTexture9* currentTexture = static_cast<CD3D9Texture*>(Texture[i])->getDX9Texture(); IDirect3DSurface9* currentSurface = 0; currentTexture->GetSurfaceLevel(0, ¤tSurface); Surface[i] = currentSurface; } } if (!DepthStencilSurface && DepthStencil) { IDirect3DTexture9* currentTexture = static_cast<CD3D9Texture*>(DepthStencil)->getDX9Texture(); IDirect3DSurface9* currentSurface = 0; currentTexture->GetSurfaceLevel(0, ¤tSurface); DepthStencilSurface = currentSurface; } }
void mpRendererD3D9::updateDataTexture(void *texptr, int width, int height, const void *data, size_t data_size) { int psize = 16; HRESULT hr; IDirect3DTexture9 *tex = (IDirect3DTexture9*)texptr; // D3D11 と違い、D3D9 では書き込みも staging texture を経由する必要がある。 IDirect3DSurface9 *surf_src = findOrCreateStagingTexture(mpDataTextureWidth, height); if (surf_src == nullptr) { return; } IDirect3DSurface9* surf_dst = nullptr; hr = tex->GetSurfaceLevel(0, &surf_dst); if (FAILED(hr)) { return; } bool ret = false; D3DLOCKED_RECT locked; hr = surf_src->LockRect(&locked, nullptr, D3DLOCK_DISCARD); if (SUCCEEDED(hr)) { const char *rpixels = (const char*)data; int rpitch = psize * width; char *wpixels = (char*)locked.pBits; int wpitch = locked.Pitch; memcpy(wpixels, rpixels, data_size); surf_src->UnlockRect(); hr = m_device->UpdateSurface(surf_src, nullptr, surf_dst, nullptr); if (SUCCEEDED(hr)) { ret = true; } } surf_dst->Release(); }
void RSManager::DoLimbo() { if (!(mainRT && zSurf && doLimbo && limbo)) return; IDirect3DSurface9 *oldRenderTarget; d3ddev->GetRenderTarget(0, &oldRenderTarget); if (oldRenderTarget == mainRT) { // final renderbuffer has to be from texture, just making sure here if (IDirect3DTexture9* tex = getSurfTexture(oldRenderTarget)) { // check size just to make even more sure D3DSURFACE_DESC desc; oldRenderTarget->GetDesc(&desc); if (desc.Width == Settings::get().getRenderWidth() && desc.Height == Settings::get().getRenderHeight()) { IDirect3DTexture9 *zTex = getSurfTexture(zSurf); storeRenderState(); d3ddev->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE); d3ddev->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW); d3ddev->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_RED | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_BLUE); /// Draw zbuffer limbo->go(zTex, oldRenderTarget, limboZNear, limboZFar); SDLOG(0, "ZNear: %g, ZFar: %g\n", limboZNear, limboZFar); //d3ddev->StretchRect(zSurf, NULL, oldRenderTarget, NULL, D3DTEXF_NONE); restoreRenderState(); zTex->Release(); } tex->Release(); } } oldRenderTarget->Release(); }
bool Blit::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest) { IDirect3DTexture9 *texture = copySurfaceToTexture(source, getSurfaceRect(source)); if (!texture) { return false; } IDirect3DDevice9 *device = getDevice(); saveState(); device->SetTexture(0, texture); device->SetRenderTarget(0, dest); setVertexShader(SHADER_VS_STANDARD); setPixelShader(SHADER_PS_PASSTHROUGH); setCommonBlitState(); device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); setViewport(getSurfaceRect(dest), 0, 0); render(); texture->Release(); restoreState(); return true; }
void MFTexture_DestroyPlatformSpecific(MFTexture *pTexture) { MFCALLSTACK; IDirect3DTexture9 *pTex = (IDirect3DTexture9*)pTexture->pInternalData; pTex->Release(); }
// Increments refcount on surface. // caller must Release() the returned surface gl::Error TextureStorage9_2D::getSurfaceLevel(GLenum target, int level, bool dirty, IDirect3DSurface9 **outSurface) { ASSERT(target == GL_TEXTURE_2D); UNUSED_ASSERTION_VARIABLE(target); IDirect3DBaseTexture9 *baseTexture = NULL; gl::Error error = getBaseTexture(&baseTexture); if (error.isError()) { return error; } IDirect3DTexture9 *texture = static_cast<IDirect3DTexture9*>(baseTexture); HRESULT result = texture->GetSurfaceLevel(level + mTopLevel, outSurface); ASSERT(SUCCEEDED(result)); if (FAILED(result)) { return gl::Error(GL_OUT_OF_MEMORY, "Failed to get the surface from a texture, result: 0x%X.", result); } // With managed textures the driver needs to be informed of updates to the lower mipmap levels if (level + mTopLevel != 0 && isManaged() && dirty) { texture->AddDirtyRect(NULL); } return gl::Error(GL_NO_ERROR); }
void D3D9Blit(const IntRect& dstRect, unsigned char* src, unsigned srcStride, bool discard = false) { RECT d3dRect; d3dRect.left = dstRect.left_; d3dRect.top = dstRect.top_; d3dRect.right = dstRect.right_; d3dRect.bottom = dstRect.bottom_; int level = 0; DWORD flags = discard ? D3DLOCK_DISCARD : 0; D3DLOCKED_RECT d3dLockedRect; IDirect3DTexture9* object = (IDirect3DTexture9*) webTexture2D_->GetTexture2D()->GetGPUObject(); if (FAILED(object->LockRect(level, &d3dLockedRect, (flags & D3DLOCK_DISCARD) ? 0 : &d3dRect, flags))) { LOGERROR("WebTexture2D - Could not lock texture"); return; } int width = dstRect.Width(); int height = dstRect.Height(); for (int j = 0; j < height; ++j) { unsigned char* dst = (unsigned char*) d3dLockedRect.pBits + j * d3dLockedRect.Pitch; memcpy(dst, src, width * 4); src += srcStride; } object->UnlockRect(level); }
bool Blit::formatConvert(IDirect3DSurface9 *source, const RECT &sourceRect, GLenum destFormat, GLint xoffset, GLint yoffset, IDirect3DSurface9 *dest) { IDirect3DTexture9 *texture = copySurfaceToTexture(source, sourceRect); if (!texture) { return false; } IDirect3DDevice9 *device = getDevice(); saveState(); device->SetTexture(0, texture); device->SetRenderTarget(0, dest); setViewport(sourceRect, xoffset, yoffset); setCommonBlitState(); if (setFormatConvertShaders(destFormat)) { render(); } texture->Release(); restoreState(); return true; }
MGuiRendTexture* CRenderer::LoadTexture( const char_t* path, uint32* width, uint32* height ) { CTexture* texture = NULL; D3DSURFACE_DESC desc; IDirect3DTexture9* data; HRESULT hr; // I was hoping I wouldn't have to use D3DX for this :( hr = D3DXCreateTextureFromFileEx( d3dDevice, path, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &data ); if ( SUCCEEDED( hr ) ) { data->GetLevelDesc( 0, &desc ); texture = new CTexture(); texture->texture = data; texture->width = desc.Width; texture->height = desc.Height; if ( width ) *width = desc.Width; if ( height ) *height = desc.Height; } return (MGuiRendTexture*)texture; }
MF_API void MFTexture_Unmap(MFTexture *pTexture, int element, int mipLevel) { switch(pTexture->type) { case MFTexType_1D: case MFTexType_2D: { IDirect3DTexture9 *pTex = (IDirect3DTexture9*)pTexture->pInternalData; pTex->UnlockRect(mipLevel); break; } case MFTexType_3D: { IDirect3DVolumeTexture9 *pTex = (IDirect3DVolumeTexture9*)pTexture->pInternalData; pTex->UnlockBox(mipLevel); break; } case MFTexType_Cubemap: { IDirect3DCubeTexture9 *pTex = (IDirect3DCubeTexture9*)pTexture->pInternalData; pTex->UnlockRect(gD3DCubeFaces[element], mipLevel); break; } } }
static IDirect3DTexture9 *BindFont(IDirect3DDevice9 *_Dev, const CTexFont *_Font) { assert(_Font!=NULL); IDirect3DTexture9 *Tex = NULL; IDirect3DDevice9Ex *D3DDev9Ex = NULL; bool IsD3DDev9Ex = SUCCEEDED(_Dev->QueryInterface(__uuidof(IDirect3DDevice9Ex), (void **)&D3DDev9Ex)) && D3DDev9Ex != NULL; HRESULT hr; if (IsD3DDev9Ex) { hr = _Dev->CreateTexture(_Font->m_TexWidth, _Font->m_TexHeight, 1, D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &Tex, NULL); D3DDev9Ex->Release(); } else hr = _Dev->CreateTexture(_Font->m_TexWidth, _Font->m_TexHeight, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &Tex, NULL); if( FAILED(hr) ) return NULL; D3DLOCKED_RECT r; hr = Tex->LockRect(0, &r, NULL, 0); if( SUCCEEDED(hr) ) { color32 *p = static_cast<color32 *>(r.pBits); for( int i=0; i<_Font->m_TexWidth*_Font->m_TexHeight; ++i, ++p ) *p = 0x00ffffff | (((color32)(_Font->m_TexBytes[i]))<<24); Tex->UnlockRect(0); } return Tex; }
void CeguiTextureImage::onRender(const ::bootes::lib::framework::GameTime* gt) { if (_pSceneSeq == NULL) { IStageManager* p = g_pGame->getStageManager(); if (p) { _pSceneSeq = p->getSceneSequencer(); } } if (_pSceneSeq == NULL) { return; } { Scene scene = _pSceneSeq->getScene(true); if (! scene.isValid()) { return; } IDirect3DTexture9* pTex = scene.refTexture(); _size.d_width = (float)scene.videoinfo().width; _size.d_height = (float)scene.videoinfo().height; _time = scene.clock().clock; _pCeguiTex->setDirect3D9Texture(pTex); _pCeguiTex->setOriginalDataSize(_size); if (pTex) { pTex->Release(); } } CEGUI::Point zero(0,0); CEGUI::Imageset& is = CEGUI::ImagesetManager::getSingleton().get(_is_name.c_str()); t_items::iterator i; for (i = _items.begin(); i != _items.end(); ++i) { Item* pi = i->second; is.undefineImage(pi->im_name.c_str()); is.defineImage(pi->im_name.c_str(), zero, _size, zero); } }
void MGEhud::setTexture(hud_id hud, const char *texture) { Element *e = &elements[hud]; if(e->texture) e->texture->Release(); IDirect3DTexture9 *tex = BSALoadTexture(device, texture); if(tex) { D3DSURFACE_DESC desc; tex->GetLevelDesc(0, &desc); e->w = desc.Width; e->h = desc.Height; e->texture = tex; // As the BSA cache cannot reload a texture after it is released, (it returns a pointer to the released texture) // we have to add a loose reference to keep the texture in memory and avoid a crash tex->AddRef(); e->textureFilename = texture; } else { LOG::logline("LoadHUDTexture : Cannot load texture %s", texture); e->texture = 0; e->textureFilename.clear(); } }
/** * * IFT = Into Final Texture. * The Radial Menu image is assembled by drawing into the Render Target. * But to render that image in-game, it must be copied to a texture. * This function copies the Render Target's surface to the Final Texture's surface. * * @author Jade Abbott * @return Void. * */ void CRadialMenu::CopyTargetSurfaceIFT() { assert(m_pRenderer); assert(m_iFinalTextureID != Utility::INVALID_ID); assert(m_uiRenderTargetID != Utility::INVALID_ID); // Get the render target's surface (which contains the assembled Radial Menu image). IDirect3DSurface9* pRenderTarget = m_pRenderer->GetSurfaceManager().GetSurface(m_uiRenderTargetID); assert(pRenderTarget); // Get the final texture. IDirect3DTexture9* pTexture = m_pRenderer->GetTextureManager().GetTexture(m_iFinalTextureID); assert(pTexture); // Get the surface from the texture, and copy the render target's surface into it. IDirect3DSurface9* pTextureSurface = 0; if (SUCCEEDED(pTexture->GetSurfaceLevel(0, &pTextureSurface))) { assert(pTextureSurface); D3DXLoadSurfaceFromSurface(pTextureSurface, 0, 0, pRenderTarget, 0, 0, D3DX_FILTER_NONE, 0); pTextureSurface->Release(); pTextureSurface = 0; } }
// interface functions void MFTexture_CreatePlatformSpecific(MFTexture *pTexture, bool generateMipChain) { MFCALLSTACK; HRESULT hr; MFTextureTemplateData *pTemplate = pTexture->pTemplateData; // create texture D3DFORMAT platformFormat = (D3DFORMAT)MFTexture_GetPlatformFormatID(pTemplate->imageFormat, MFDD_D3D9); hr = D3DXCreateTexture(pd3dDevice, pTemplate->pSurfaces[0].width, pTemplate->pSurfaces[0].height, generateMipChain ? 0 : 1, 0, platformFormat, D3DPOOL_MANAGED, (IDirect3DTexture9**)&pTexture->pInternalData); MFDebug_Assert(hr != D3DERR_NOTAVAILABLE, MFStr("LoadTexture failed: D3DERR_NOTAVAILABLE, 0x%08X", hr)); MFDebug_Assert(hr != D3DERR_OUTOFVIDEOMEMORY, MFStr("LoadTexture failed: D3DERR_OUTOFVIDEOMEMORY, 0x%08X", hr)); MFDebug_Assert(hr != D3DERR_INVALIDCALL, MFStr("LoadTexture failed: D3DERR_INVALIDCALL, 0x%08X", hr)); MFDebug_Assert(hr != D3DXERR_INVALIDDATA, MFStr("LoadTexture failed: D3DXERR_INVALIDDATA, 0x%08X", hr)); MFDebug_Assert(hr == D3D_OK, MFStr("Failed to create texture '%s'.", pTexture->name)); IDirect3DTexture9 *pTex = (IDirect3DTexture9*)pTexture->pInternalData; // copy image data D3DLOCKED_RECT rect; pTex->LockRect(0, &rect, NULL, 0); MFCopyMemory(rect.pBits, pTemplate->pSurfaces[0].pImageData, pTemplate->pSurfaces[0].bufferLength); pTex->UnlockRect(0); // filter mip levels if(generateMipChain) D3DXFilterTexture(pTex, NULL, 0, D3DX_DEFAULT); }
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(); }
//---------------------------------------------------------------------------------- // //---------------------------------------------------------------------------------- void TextureLoader::Unload( void* data ) { if( data != NULL ) { IDirect3DTexture9* texture = (IDirect3DTexture9*)data; texture->Release(); } }
void LoadImage(char *buf, int sizeBuf, int &width, int &height, uint8 ** ppBuffer, bool bAlpha) { #ifdef USE_DIRECTX_RENDERER HRESULT hr; D3DSURFACE_DESC desc; IDirect3DTexture9* pTexture = NULL; D3DFORMAT d3dFormat; if (bAlpha) d3dFormat = D3DFMT_A8R8G8B8; else d3dFormat = D3DFMT_R8G8B8; // read from file hr = D3DXCreateTextureFromFileInMemoryEx( CGlobals::GetRenderDevice(), buf, sizeBuf, 0, 0, 1, 0, d3dFormat, D3DPOOL_SCRATCH, D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0, NULL, NULL, &pTexture ); if( FAILED(hr) ) return; pTexture->GetLevelDesc( 0, &desc ); // set size width = desc.Width; height = desc.Height; uint8 *pBufferTemp; int nSize; if (bAlpha) nSize = width * height * 4; else nSize = width * height * 3; pBufferTemp = new uint8[nSize]; D3DLOCKED_RECT lockedRect; hr = pTexture->LockRect( 0, &lockedRect, NULL, D3DLOCK_READONLY ); if( SUCCEEDED(hr) ) { uint8 *pImagePixels = (uint8 *) lockedRect.pBits; memcpy(pBufferTemp, pImagePixels, nSize); // *ppBuffer = pBufferTemp; pTexture->UnlockRect( 0 ); } else { width = 0; height = 0; *ppBuffer = NULL; } SAFE_RELEASE( pTexture ); #endif }
void DirectX9::FreeTexture( Gwen::Texture* pTexture ) { IDirect3DTexture9* pImage = ( IDirect3DTexture9* ) pTexture->data; if ( !pImage ) { return; } pImage->Release(); pTexture->data = NULL; return; }
bool fcGraphicsDeviceD3D9::readTexture(void *o_buf, size_t bufsize, void *tex_, int width, int height, fcTextureFormat format) { HRESULT hr; IDirect3DTexture9 *tex = (IDirect3DTexture9*)tex_; // D3D11 と同様 render target の内容は CPU からはアクセス不可能になっている。 // staging texture を用意してそれに内容を移し、CPU はそれ経由でデータを読む。 IDirect3DSurface9 *surf_dst = findOrCreateStagingTexture(width, height, format); if (surf_dst == nullptr) { return false; } IDirect3DSurface9* surf_src = nullptr; hr = tex->GetSurfaceLevel(0, &surf_src); if (FAILED(hr)){ return false; } bool ret = false; hr = m_device->GetRenderTargetData(surf_src, surf_dst); if (SUCCEEDED(hr)) { D3DLOCKED_RECT locked; hr = surf_dst->LockRect(&locked, nullptr, D3DLOCK_READONLY); if (SUCCEEDED(hr)) { char *wpixels = (char*)o_buf; int wpitch = width * fcGetPixelSize(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; } } surf_dst->UnlockRect(); // D3D9 の ARGB32 のピクセルの並びは BGRA になっているので並べ替える if (format == fcTextureFormat_ARGB32) { BGRA_RGBA_conversion((RGBA<uint8_t>*)o_buf, bufsize / 4); } ret = true; } } surf_src->Release(); return ret; }
void RageDisplay_D3D::DeleteTexture( unsigned uTexHandle ) { IDirect3DTexture9* pTex = (IDirect3DTexture9*) uTexHandle; pTex->Release(); // Delete palette (if any) if( g_TexResourceToPaletteIndex.find(uTexHandle) != g_TexResourceToPaletteIndex.end() ) g_TexResourceToPaletteIndex.erase( g_TexResourceToPaletteIndex.find(uTexHandle) ); if( g_TexResourceToTexturePalette.find(uTexHandle) != g_TexResourceToTexturePalette.end() ) g_TexResourceToTexturePalette.erase( g_TexResourceToTexturePalette.find(uTexHandle) ); }
void Console::initialize(IDirect3DDevice9* device, int w, int h) { SDLOG(0, "Initializing Console on device %p\n", device); width = w; height = h; this->device = device; // Create font SDLOG(2, " - creating console font\n"); SAFERELEASE(fontTex); FILE* ff = fopen(getAssetFileName("font.ttf").c_str(), "rb"); unsigned char* ttf_buffer = new unsigned char[1<<20]; unsigned char* temp_bitmap = new unsigned char[BMPSIZE*BMPSIZE]; fread(ttf_buffer, 1, 1<<20, ff); fclose(ff); stbtt_BakeFontBitmap(ttf_buffer, 0, 40.0, temp_bitmap, BMPSIZE, BMPSIZE, 32, 96, cdata); // no guarantee this fits! device->CreateTexture(BMPSIZE, BMPSIZE, 0, D3DUSAGE_AUTOGENMIPMAP, D3DFMT_A8, D3DPOOL_DEFAULT, &fontTex, NULL); IDirect3DTexture9 *tempTex; device->CreateTexture(BMPSIZE, BMPSIZE, 1, D3DUSAGE_DYNAMIC, D3DFMT_A8, D3DPOOL_SYSTEMMEM, &tempTex, NULL); D3DLOCKED_RECT rect; tempTex->LockRect(0, &rect, NULL, 0); memcpy(rect.pBits, temp_bitmap, BMPSIZE*BMPSIZE); tempTex->UnlockRect(0); device->UpdateTexture(tempTex, fontTex); tempTex->Release(); delete ttf_buffer; delete temp_bitmap; // Create vertex decl SDLOG(2, " - creating console vertex decl\n"); SAFERELEASE(vertexDeclaration); device->CreateVertexDeclaration(vertexElements , &vertexDeclaration); // Load effect from file SDLOG(2, " - loading console effect file\n"); SAFERELEASE(effect); vector<D3DXMACRO> defines; std::stringstream s; D3DXMACRO null = { NULL, NULL }; defines.push_back(null); DWORD flags = D3DXFX_NOT_CLONEABLE | D3DXSHADER_OPTIMIZATION_LEVEL3; SDLOG(2, " - actually load effect\n"); ID3DXBuffer* errors; HRESULT hr = D3DXCreateEffectFromFile(device, getAssetFileName("console.fx").c_str(), &defines.front(), NULL, flags, NULL, &effect, &errors); if(hr != D3D_OK) SDLOG(0, "ERRORS:\n %s\n", errors->GetBufferPointer()); // get handles rectColorHandle = effect->GetParameterByName(NULL, "rectColor"); textTex2DHandle = effect->GetParameterByName(NULL, "textTex2D"); SDLOG(0, " - done\n"); }
void RageDisplay_D3D::UpdateTexture( unsigned uTexHandle, RageSurface* img, int xoffset, int yoffset, int width, int height ) { IDirect3DTexture9* pTex = (IDirect3DTexture9*)uTexHandle; ASSERT( pTex != NULL ); RECT rect; rect.left = xoffset; rect.top = yoffset; rect.right = width - xoffset; rect.bottom = height - yoffset; D3DLOCKED_RECT lr; pTex->LockRect( 0, &lr, &rect, 0 ); D3DSURFACE_DESC desc; pTex->GetLevelDesc(0, &desc); ASSERT( xoffset+width <= int(desc.Width) ); ASSERT( yoffset+height <= int(desc.Height) ); // // Copy bits // #if defined(XBOX) // Xbox textures need to be swizzled XGSwizzleRect( img->pixels, // pSource, img->pitch, // Pitch, NULL, // pRect, lr.pBits, // pDest, img->w, // Width, img->h, // Height, NULL, // pPoint, img->format->BytesPerPixel ); //BytesPerPixel #else int texpixfmt; for(texpixfmt = 0; texpixfmt < NUM_PIX_FORMATS; ++texpixfmt) if(D3DFORMATS[texpixfmt] == desc.Format) break; ASSERT( texpixfmt != NUM_PIX_FORMATS ); RageSurface *Texture = CreateSurfaceFromPixfmt(RagePixelFormat(texpixfmt), lr.pBits, width, height, lr.Pitch); ASSERT( Texture ); RageSurfaceUtils::Blit( img, Texture, width, height ); delete Texture; #endif pTex->UnlockRect( 0 ); }