PDA

View Full Version : Tearing problem inspite of having D3DPRESENT_INTERVAL_ONE as PresentationInterval


mark123
06-26-08, 08:45 AM
I have a single control displaying images using texture mapping. The application frequently updates the displayed image by calling Refresh() method (shown below). There is tearing problem (momentary horizontal cut of images during the image refresh).
To avoid tearing problem, I use D3DPRESENT_INTERVAL_ONE for PresentationInterval when I create the device. Following is the code of initialization and refresh functions:

LPDIRECT3D9 g_pD3D = NULL;
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;
ID3DXSprite* g_pSprite = NULL;
IDirect3DTexture9* g_pTexture = NULL;
const int TEXTURE_SIZE = 1024;
void Init(HWND hWnd)
{
g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
d3dpp.EnableAutoDepthStencil = FALSE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_FPU_PRESERVE, &d3dpp, &g_pd3dDevice);
D3DXCreateSprite(g_pd3dDevice, &g_pSprite);

g_pd3dDevice->CreateTexture(TEXTURE_SIZE, TEXTURE_SIZE, 1,
D3DUSAGE_SOFTWAREPROCESSING, D3DFMT_X8R8G8B8, D3DPOOL_MANAGED, &g_pTexture, NULL);
}
void Refresh(HWND hControlWnd, void* src, int width, int height)
{
g_pd3dDevice->BeginScene();
g_pSprite->Begin(0);
g_pd3dDevice->SetSamplerState(0,D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
g_pd3dDevice->SetSamplerState(0,D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
g_pd3dDevice->SetSamplerState(0,D3DSAMP_MIPFILTER, D3DTEXF_NONE);
g_pd3dDevice->SetSamplerState(0,D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
g_pd3dDevice->SetSamplerState(0,D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
g_pd3dDevice->SetSamplerState(0,D3DSAMP_ADDRESSW, D3DTADDRESS_CLAMP);
g_pd3dDevice->SetSamplerState(0,D3DSAMP_BORDERCOLOR, 0);
D3DXMATRIX matrix;
D3DXMatrixIdentity(&matrix);
g_pd3dDevice->SetTransform(D3DTS_WORLD, &matrix);
D3DLOCKED_RECT lockedBits;
g_pTexture->LockRect(0, &lockedBits, NULL, D3DLOCK_DISCARD | D3DLOCK_NO_DIRTY_UPDATE);
//Copy image data to the texture
CopyImage(0, 0, width, height, TEXTURE_SIZE, width, height, src, lockedBits.pBits);
RECT r;
r.left = 0;
r.top = 0;
r.right = width;
r.bottom = height;
g_pTexture->AddDirtyRect(&r);
g_pTexture->UnlockRect(0);
D3DXVECTOR3 center = D3DXVECTOR3(0, 0, 0);
D3DXVECTOR3 position = D3DXVECTOR3(0, 0, 0);
g_pSprite->Draw(g_pTexture, &r, &center, &position, -1);
g_pSprite->End();
g_pd3dDevice->EndScene();
g_pd3dDevice->Present(&r, NULL, hControlWnd, NULL);
}
Thank you
-Mark