void D3D9Texture::ReadData(Grid<float> &Data) { PersistentAssert(_Width != 0 && _Height != 0 && _Format == D3DFMT_R32F, "ReadData called on invalid surface"); Data.Allocate(_Height, _Width); D3DLOCKED_RECT Rect; if(_RenderTarget) { D3DAlwaysValidate(_Device->GetRenderTargetData(_SurfaceTopLevel, _SurfacePlain), "GetRenderTargetData"); D3DAlwaysValidate(_SurfacePlain->LockRect(&Rect, NULL, D3DLOCK_READONLY), "LockRect"); } else { D3DAlwaysValidate(_SurfaceTopLevel->LockRect(&Rect, NULL, D3DLOCK_READONLY), "LockRect"); } BYTE *Bytes = (BYTE *)Rect.pBits; for(UINT y = 0; y < _Height; y++) { float *CurRow = (float *)(Bytes + y * Rect.Pitch); for(UINT x = 0; x < _Width; x++) { Data(y, x) = CurRow[x]; } } if(_RenderTarget) { D3DValidate(_SurfacePlain->UnlockRect(), "UnlockRect"); } else { D3DValidate(_SurfaceTopLevel->UnlockRect(), "UnlockRect"); } }
void D3D9Texture::Flush() { PersistentAssert(_Width != 0 && _Height != 0 && _Format == D3DFMT_A32B32G32R32F && _RenderTarget, "ReadData called on invalid surface"); D3DLOCKED_RECT Rect; D3DAlwaysValidate(_Device->GetRenderTargetData(_SurfaceTopLevel, _SurfacePlain), "GetRenderTargetData"); D3DAlwaysValidate(_SurfacePlain->LockRect(&Rect, NULL, D3DLOCK_READONLY), "LockRect"); D3DValidate(_SurfacePlain->UnlockRect(), "UnlockRect"); }
void D3D9RenderTargetTexture::GetData(Bitmap &B) { D3DAlwaysValidate(_Device->GetRenderTargetData(_Surface, _SurfacePlain), "GetRenderTargetData"); D3DLOCKED_RECT LockedRect; D3DAlwaysValidate(_SurfacePlain->LockRect(&LockedRect, NULL, 0), "LockRect"); B.Allocate(_Width, _Height); RGBColor *SurfData = (RGBColor*)LockedRect.pBits; for(UINT y = 0; y < _Height; y++) { //memcpy(B[_Height - 1 - y], &SurfData[y * LockedRect.Pitch / 4], _Width * 4); memcpy(B[y], &SurfData[y * LockedRect.Pitch / 4], _Width * 4); } _SurfacePlain->UnlockRect(); }
void D3D9RenderTargetTexture::SetData(const Bitmap &B) { D3DLOCKED_RECT LockedRect; D3DAlwaysValidate(_SurfacePlain->LockRect(&LockedRect, NULL, 0), "LockRect"); Assert(B.Width() == _Width && B.Height() == _Height, "Invalid bitmap dimensions."); RGBColor *SurfData = (RGBColor*)LockedRect.pBits; for(UINT y = 0; y < _Height; y++) { memcpy(&SurfData[y * LockedRect.Pitch / 4], B[_Height - 1 - y], _Width * 4); } _SurfacePlain->UnlockRect(); D3DAlwaysValidate(_Device->UpdateSurface(_SurfacePlain, NULL, _Surface, NULL), "UpdateSurface"); _DataCopy = B; }
void D3D9Texture::Allocate(D3DFORMAT Format, UINT Width, UINT Height, bool RenderTarget, UINT MipmapLevels) { if(_Width != Width || _Height != Height || _Format != Format || _RenderTarget != RenderTarget) { FreeMemory(); PersistentAssert(_Device != NULL, "D3D9Texture not associated"); DWORD Usage = 0; if(MipmapLevels != 1) { Usage |= D3DUSAGE_AUTOGENMIPMAP; } D3DPOOL Pool = D3DPOOL_MANAGED; /*if(Format == D3DFMT_DXT1) { while((Width & 3) != 0) { Width--; } while((Height & 3) != 0) { Height--; } if(Width == 0) Width = 4; if(Height == 0) Height = 4; }*/ if(RenderTarget) { Usage |= D3DUSAGE_RENDERTARGET; Pool = D3DPOOL_DEFAULT; D3DAlwaysValidate(_Device->CreateOffscreenPlainSurface(Width, Height, Format, D3DPOOL_SYSTEMMEM, &_SurfacePlain, NULL), "CreateOffscreenPlainSurface"); } D3DAlwaysValidate(_Device->CreateTexture(Width, Height, MipmapLevels, Usage, Format, Pool, &_Texture, NULL), "CreateTexture"); D3DValidate(_Texture->GetSurfaceLevel(0, &_SurfaceTopLevel), "GetSurfaceLevel"); _Width = Width; _Height = Height; _Format = Format; _RenderTarget = RenderTarget; } }
void D3D9Texture::ReadData(Grid<Vec3f> &Data) { PersistentAssert(_Width != 0 && _Height != 0 && _Format == D3DFMT_A32B32G32R32F && _RenderTarget, "ReadData called on invalid surface"); Data.Allocate(_Height, _Width); D3DLOCKED_RECT Rect; D3DAlwaysValidate(_Device->GetRenderTargetData(_SurfaceTopLevel, _SurfacePlain), "GetRenderTargetData"); D3DAlwaysValidate(_SurfacePlain->LockRect(&Rect, NULL, D3DLOCK_READONLY), "LockRect"); BYTE *Bytes = (BYTE *)Rect.pBits; for(UINT y = 0; y < _Height; y++) { Vec4f *CurRow = (Vec4f *)(Bytes + y * Rect.Pitch); for(UINT x = 0; x < _Width; x++) { Data(y, x) = Vec3f(CurRow[x].x, CurRow[x].y, CurRow[x].z); } } D3DValidate(_SurfacePlain->UnlockRect(), "UnlockRect"); }
void D3D9Texture::ReadData(Bitmap &Bmp) { PersistentAssert(_Width != 0 && _Height != 0 && _Format == D3DFMT_A8R8G8B8 && _RenderTarget, "ReadData called on invalid surface"); Bmp.Allocate(_Width, _Height); D3DLOCKED_RECT Rect; D3DAlwaysValidate(_Device->GetRenderTargetData(_SurfaceTopLevel, _SurfacePlain), "GetRenderTargetData"); D3DAlwaysValidate(_SurfacePlain->LockRect(&Rect, NULL, D3DLOCK_READONLY), "LockRect"); BYTE *Bytes = (BYTE *)Rect.pBits; for(UINT y = 0; y < _Height; y++) { RGBColor *CurRow = (RGBColor *)(Bytes + y * Rect.Pitch); for(UINT x = 0; x < _Width; x++) { Bmp[y][x] = CurRow[x]; } } D3DValidate(_SurfacePlain->UnlockRect(), "UnlockRect"); }
void D3D9Mesh::SimplifyToVertices(UINT Count) { Lock(); Unlock(); DWORD *Adj; Clean(1e-6f, Adj); LPD3DXMESH NewMesh = NULL; D3DAlwaysValidate(D3DXSimplifyMesh(_Mesh, Adj, NULL, NULL, Count, D3DXMESHSIMP_VERTEX, &NewMesh), "D3DXSimplifyMesh"); _Mesh->Release(); D3DAlwaysValidate(NewMesh->CloneMeshFVF(D3DMeshOptions, D3DMeshFVF, GetD3DDevice(), &_Mesh), "CloneMeshFVF"); NewMesh->Release(); delete[] Adj; Lock(); Unlock(); }
void D3D9RenderTargetTexture::GetDepthData(Grid<float> &DepthMap) { PersistentAssert(_DepthSurface != NULL, "_DepthSurface == NULL unexpected"); D3DLOCKED_RECT LockedRect; D3DAlwaysValidate(_DepthSurface->LockRect(&LockedRect, NULL, 0), "LockRect"); BYTE *Data = (BYTE *)LockedRect.pBits; DepthMap.Allocate(_Height, _Width); for(UINT y = 0; y < _Height; y++) { float *RowStart = (float *)(Data + y * LockedRect.Pitch); for(UINT x = 0; x < _Width; x++) { DepthMap.GetElement(y, x) = RowStart[x]; } } D3DAlwaysValidate(_DepthSurface->UnlockRect(), "UnlockRect"); }
void D3D9Mesh::Clean(float Epsilon, DWORD* &AdjDataOut) { Assert(_Mesh != NULL, "Clean called on empty _Mesh."); LPD3DXMESH NewMesh; DWORD *AdjData = new DWORD[3 * _Mesh->GetNumFaces()]; AdjDataOut = new DWORD[3 * _Mesh->GetNumFaces()]; DWORD *FaceRemap = new DWORD[3 * _Mesh->GetNumFaces()]; /*D3DXWELDEPSILONS Eps; Eps.Position = Epsilon; Eps.BlendWeights = 1.0f; Eps.Normal = 1.0f; Eps.PSize = 1.0f; Eps.Specular = 1.0f; Eps.Diffuse = 1.0f; Eps.Tangent = 1.0f; Eps.Binormal = 1.0f; Eps.TessFactor = 1.0f; for(UINT _Indices = 0; _Indices < 8; _Indices++) { Eps.Texcoord[_Indices] = 1.0f; }*/ CleanVerticesAndTriangles(); Unlock(); //D3DAlwaysValidate(_Mesh->GenerateAdjacency(Epsilon, AdjDataOut)); //D3DAlwaysValidate(D3DXWeldVertices(_Mesh, D3DXWELDEPSILONS_WELDPARTIALMATCHES, &Eps, AdjDataOut, AdjData, FaceRemap, NULL)); //D3DAlwaysValidate(D3DXWeldVertices(_Mesh, D3DXWELDEPSILONS_WELDALL | D3DXWELDEPSILONS_WELDPARTIALMATCHES, NULL, AdjDataOut, AdjData, FaceRemap, NULL)); //CleanTriangles(); D3DAlwaysValidate(_Mesh->GenerateAdjacency(Epsilon, AdjData), "GenerateAdjacency"); D3DAlwaysValidate(D3DXCleanMesh(D3DXCLEAN_SIMPLIFICATION, _Mesh, AdjData, &NewMesh, AdjDataOut, NULL), "D3DXCleanMesh"); _Mesh->Release(); D3DAlwaysValidate(NewMesh->CloneMeshFVF(D3DMeshOptions, D3DMeshFVF, GetD3DDevice(), &_Mesh), "CloneMeshFVF"); NewMesh->Release(); Lock(); Unlock(); delete[] AdjData; delete[] FaceRemap; }
void D3D9RenderTargetTexture::Init(LPDIRECT3DDEVICE9 Device, UINT Width, UINT Height, bool CreateDepthSurface) { FreeMemory(); PersistentAssert(Device != NULL, "Device == NULL"); _Device = Device; _Width = Width; _Height = Height; //FullValidate(_Device->CreateRenderTarget(_Width, _Height, D3DFMT_A8R8G8B8, D3DMULTISAMPLE_NONE, 0, TRUE, &_Surface, NULL)); D3DAlwaysValidate(_Device->CreateTexture(_Width, _Height, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &_Texture, NULL), "CreateTexture"); D3DAlwaysValidate(_Device->CreateOffscreenPlainSurface(_Width, _Height, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &_SurfacePlain, NULL), "CreateOffscreenPlainSurface"); D3DAlwaysValidate(_Texture->GetSurfaceLevel(0, &_Surface), "GetSurfaceLevel"); if(CreateDepthSurface) { D3DAlwaysValidate(_Device->CreateDepthStencilSurface(_Width, _Height, D3DFMT_D32F_LOCKABLE, D3DMULTISAMPLE_NONE, 0, FALSE, &_DepthSurface, NULL), "CreateDepthStencilSurface"); } _DataCopy.Allocate(_Width, _Height); }
void D3D9RenderTargetTexture::GetData(Bitmap &B, const Rectangle2i &Source, const Rectangle2i &Dest) { D3DAlwaysValidate(_Device->GetRenderTargetData(_Surface, _SurfacePlain), "GetRenderTargetData"); if(_SurfaceResizing == NULL) { D3DAlwaysValidate(_Device->CreateOffscreenPlainSurface(_Width, _Height, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &_SurfaceResizing, NULL), "CreateOffscreenPlainSurface"); } D3DAlwaysValidate(_Device->UpdateSurface(_SurfacePlain, NULL, _SurfaceResizing, NULL), "UpdateSurface"); RECT SourceRect = Source.ToRect(); RECT DestRect = Rectangle2i(Vec2i::Origin, Dest.Dimensions()).ToRect(); D3DAlwaysValidate(_Device->StretchRect(_SurfaceResizing, &SourceRect, _Surface, &DestRect, D3DTEXF_LINEAR), "StretchRect"); D3DAlwaysValidate(_Device->GetRenderTargetData(_Surface, _SurfacePlain), "GetRenderTargetData"); D3DLOCKED_RECT LockedRect; D3DAlwaysValidate(_SurfacePlain->LockRect(&LockedRect, NULL, 0), "LockRect"); Assert(int(B.Width()) >= Dest.Max.x && int(B.Height()) >= Dest.Max.y, "Bitmap too small"); RGBColor *SurfData = (RGBColor*)LockedRect.pBits; for(int y = Dest.Min.y; y < Dest.Max.y; y++) { //memcpy(B[_Height - 1 - y], &SurfData[y * LockedRect.Pitch / 4], _Width * 4); memcpy(B[y] + Dest.Min.x, &SurfData[(y - Dest.Min.y) * LockedRect.Pitch / 4], Dest.Dimensions().x * 4); } _SurfacePlain->UnlockRect(); }
void VideoCompressor::AddFrame(LPDIRECT3DSURFACE9 Surface, const Vec2i &Start, double TimeInSeconds) #endif { if(_Clock != NULL) { TimeInSeconds = _Clock->Elapsed(); } _Sample->SetSampleTime( LONGLONG( TimeInSeconds * 10000000.0 ) ); BYTE *BufferData; HRESULT hr = _Buffer->Lock(&BufferData, NULL, NULL); PersistentAssert(SUCCEEDED(hr), "_Buffer->Lock failed"); D3DSURFACE_DESC Desc; D3DLOCKED_RECT LockedRect; D3DAlwaysValidate(Surface->GetDesc(&Desc), "GetDesc"); PersistentAssert(Desc.Format == D3DFMT_A8R8G8B8 || Desc.Format == D3DFMT_X8R8G8B8, "Invalid surface format"); D3DAlwaysValidate(Surface->LockRect(&LockedRect, NULL, 0), "LockRect"); const UINT VideoWidth = _Width; const UINT VideoHeight = _Height; const UINT BufferPitch = sizeof(RGBColor) * VideoWidth; Vec2i CorrectedStart = Start; if(CorrectedStart.x < 0) { CorrectedStart.x = 0; } if(CorrectedStart.x + VideoWidth > Desc.Width) { CorrectedStart.x = Desc.Width - VideoWidth; } if(CorrectedStart.y < 0) { CorrectedStart.y = 0; } if(CorrectedStart.y + VideoHeight > Desc.Height) { CorrectedStart.y = Desc.Height - VideoHeight; } bool SizingCorrect = (CorrectedStart.x >= 0 && CorrectedStart.x + VideoWidth <= Desc.Width ) && (CorrectedStart.y >= 0 && CorrectedStart.y + VideoHeight <= Desc.Height); if(!SizingCorrect) { for(UINT y = 0; y < VideoHeight; y++) { memset(BufferData + y * BufferPitch, 0, BufferPitch); } } else if(Desc.Format == D3DFMT_A8R8G8B8 || Desc.Format == D3DFMT_X8R8G8B8) { for(UINT y = 0; y < VideoHeight; y++) { RGBColor *RowStart = (RGBColor *)((BYTE*)LockedRect.pBits + (y + CorrectedStart.y) * LockedRect.Pitch); memcpy(BufferData + (VideoHeight - 1 - y) * BufferPitch, RowStart + CorrectedStart.x, BufferPitch); //for(UINT x = 0; x < _Width; x++) //{ // (*this)[y][x] = RowStart[x]; //} } } D3DAlwaysValidate(Surface->UnlockRect(), "UnlockRect"); _Buffer->Unlock(); hr = _Writer->WriteSample(0, _Sample); PersistentAssert(SUCCEEDED(hr), "WriteSample failed"); }
void D3D9Texture::Load(D3D9Texture &Tex) { D3DAlwaysValidate(_Device->StretchRect(Tex.SurfaceTopLevel(), NULL, _SurfaceTopLevel, NULL, D3DTEXF_POINT), "StretchRect"); }
void D3D9Mesh::GenerateAdj(Vector<DWORD> &Adjacency) { Adjacency.Allocate(IndexCount()); D3DAlwaysValidate(_Mesh->GenerateAdjacency(0.0f, Adjacency.CArray()), "GenerateAdjacency"); }
void D3D9Mesh::GenerateAdj(DWORD *& Adj) { Adj = new DWORD[IndexCount()]; PersistentAssert(Adj != NULL, "GenerateAdj failed"); D3DAlwaysValidate(_Mesh->GenerateAdjacency(0.0f, Adj), "GenerateAdjacency"); }