   How to optimize rendering of tiles
  Im currently drawing a number of 512x512 tiles to create a background using the follow code: effectBackground->SetTechnique(hTech);unsigned int cPasses=1;effectBackground->Begin(&cPasses,0);for(unsigned int iPass=0; iPass < cPasses; iPass++) {for (unsigned int yTile = minYTile; yTile <= maxYTile; yTile++) {for (unsigned int xTile = minXTile; xTile <= maxXTile; xTile++) {unsigned int tile = yTile*m_hTiles + xTile;effectBackground->SetTexture( "BackTexture", m_tileTextures[tile]);effectBackground->SetTexture( "ZTexture", m_tileZBuffersTextures[tile]);effectBackground->BeginPass(iPass);hr = pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST,tile*6,2);CHECK_RET(hr,"Couldn't draw primitive");effectBackground->EndPass();}}}effectBackground->End();The technique is only pixel shader for adding the color from one texture and depth from the other. Is there any obvious flaws in the above code that could be optimized? This currently take much time. The total amounts of tiles is around 30 and the textures are 128x128 in size. The sampler use point for magfilter and its currently only one pass. The results are fine, its just that its a bit to slow. Any ideas?   
  You could use indexed triangle lists, which might give you a small speed boost. The main problem is switching textures every loop and drawing tiny batches. Is there a reason you can't use tiles larger than 128x128?Aside from that, have you tried running PIX or NVPerfHUD (If you have an NVidia card) to see what the bottlenecks are?   ;  You could also pre calculate yTile*m_hTiles before entering your xTile loop.You could also just render all your tiles in one loop instead of looping down y and then along x.   ;  Hmm, I have an ATI card atm and PIX does not really give me that much useful information. I have only used for a couple of minutes so I probably don't utilize it in the best way. PIX seems to give information about start times of my calls but that does not really reveal the execution time as it will be queued on the gpu pipe.So you think creating larger texture is the best way to boost performance? I agree that this could be a bottle neck. Is there an easy way to create one texture out of four (or even more if useful) without editing every single value?   ;  Can you sort the tiles to minimize textures changes ?An other method could be to store multiples tiles into one texture and just change the texture coordinate, in order to avoid too many changes.   ;Quote:Original post by WerrockSo you think creating larger texture is the best way to boost performance? I agree that this could be a bottle neck. Is there an easy way to create one texture out of four (or even more if useful) without editing every single value?You can lock both textures and copy scanlines. From my engine code:HRESULT CopyRect(LPDIRECT3DTEXTURE9 pSrc, LPDIRECT3DTEXTURE9 pDest, const RECT& rcSrc, DWORD xDest, DWORD yDest){// Lock dest textureRECT destPos;destPos.left = xDest;destPos.right = (rcSrc.right-rcSrc.left) + xDest;destPos.top = yDest;destPos.bottom = (rcSrc.bottom-rcSrc.top) + yDest;D3DLOCKED_RECT d3ddestRect;HRESULT hResult = pDest->LockRect(0, &d3ddestRect, &destPos, 0);if(FAILED(hResult)){ELog::Get().DebugFormat(L"TEXTURE : IDirect3DTexture9::LockRect() failed. Error: %s\n",DXGetErrorString(hResult));return hResult;}// Lock other (src) textureD3DLOCKED_RECT d3dsrcRect;hResult = pSrc->LockRect(0, &d3dsrcRect, &rcSrc, D3DLOCK_READONLY);if(FAILED(hResult)){pDest->UnlockRect(0);ELog::Get().DebugFormat(L"TEXTURE : IDirect3DTexture9::LockRect() failed. Error: %s\n",DXGetErrorString(hResult));return hResult;}// Copy scanlinessize_t nWidth = min(d3dsrcRect.Pitch, d3ddestRect.Pitch);size_t nHeight = rcSrc.bottom - rcSrc.top;BYTE* pSourceBits = (BYTE*)d3dsrcRect.pBits;BYTE* pDestBits = (BYTE*)d3ddestRect.pBits;for(size_t y=0; y<nHeight; ++y){memcpy(pDestBits, pSourceBits, nWidth);pDestBits += d3ddestRect.Pitch;pSourceBits += d3dsrcRect.Pitch;}// UnlockpSrc->UnlockRect(0);pDest->UnlockRect(0);return D3D_OK;}That assumes that your textures are both in the managed pool.Something else I just thought of - what is the size in bytes of the texture data you're transferring? It's possible that you're running out of video memory and D3D is texture thrashing.   ;  Unfortunately, the simple test of setting only one texture outside the loops do not give any significant improvement.   ;  What about drawing one quad, over the whole screen? Does that give the same performance? If so, then it's likely your pixel shader is a problem.Otherwise, what about trying a simpler (or non-existant) pixel shader?   ;  A simpler shader you say. My shader currently use the default vertex shader and the pixel shader is a three liner to output color and depth from two textures. I tried to draw without any shader even if this will make the z-buffer fail. It saved 15-20% of the current time around 5.5 ms. I don't know, maybe it might be beneficial if the textures(tiles) were 512x512 instead of 128x128 since every tile is drawn on a 512x512 area on the off screen render target.   ;  Hmm. Can we see some code, or an algorithm of exactly what you do? You didn't mention anything about an offscreen render target before (Ideally a PIX dump of all the D3D calls in one frame). What are your system specs, and what frame rate / frame time do you get?If you change the Z-test to always fail, do you get a speed boost? That tells us if it's a pixel shader / fill rate problem.   