A question about D3DXCreateTexture()

Started by
1 comment, last by Namethatnobodyelsetook 17 years ago
Hi all, I use D3DXCreateTexture() to create a 512*512 texture, the code is as follows: --------------------------------------------- int width = 512, height = 512; float imageData[512][512][3]; int index = 0; int x,y; for(x=0; x<width; x++) { for(y=0; y<height; y++) { if(x==0 || x==width-1 || y==0 || y==height-1) imageData[x][y][0] = imageData[x][y][1] = imageData[x][y][2] = 1.0f; else imageData[x][y][0] = imageData[x][y][1] = imageData[x][y][2] = (2 + rand() % 5)/10.0f; } } HRESULT hr = 0; hr = D3DXCreateTexture(Device, width, height, 0, 0, D3DFMT_R8G8B8, D3DPOOL_MANAGED, &pRandomTexture); if(FAILED(hr)) MessageBox(0,"D3DXCreateTexture() error!",0,0); hr = pRandomTexture->GetSurfaceLevel(0, &pRandomRenderSurface); if(FAILED(hr)) MessageBox(0,"GetSurfaceLevel() error!",0,0); D3DSURFACE_DESC surfaceDesc; hr = pRandomRenderSurface->GetDesc(&surfaceDesc); if(FAILED(hr)) MessageBox(0,"GetDesc() error!",0,0); D3DLOCKED_RECT lockedRect; hr = pRandomRenderSurface->LockRect(&lockedRect, 0, 0); // entire surface if(FAILED(hr)) MessageBox(0,"LockRect() error!",0,0); DWORD* imageDataTemp = (DWORD*)lockedRect.pBits; for(int i = 0; i < surfaceDesc.Height; i++) { for(int j = 0; j < surfaceDesc.Width; j++) { int index = i * (lockedRect.Pitch / 4) + j; // because pitch is in bytes, and 4 bytes per DWORD imageDataTemp[index] = D3DCOLOR_XRGB(int(imageData[j][0]*255.0f), int(imageData[j][1]*255.0f), int(imageData[j][2]*255.0f)); } } pRandomRenderSurface->UnlockRect(); D3DXSaveTextureToFile("height-alpha.bmp", D3DXIFF_BMP, pRandomTexture, NULL); d3d::Release<LPDIRECT3DTEXTURE9>(pRandomTexture); d3d::Release<LPDIRECT3DSURFACE9>(pRandomRenderSurface); -------------------------------------------------------------------------- But when I run it, it prompts an error: Unhandled exception at 0x00413885 in TexQuad.exe: 0xC00000FD: Stack overflow. However, when I set width=height=256, it runs ok! Can anyone tell me why? Thanks in advance!
Advertisement
You can't allocate 512x512x3 floats on the stack. That's just too big. Use new and delete to allocate it on the heap:
float *imageData = new float[512][512][3];// Use data...delete [] imageData;
(don't forget to delete when you're done with it so you don't leak memory.)
Sirob Yes.» - status: Work-O-Rama.
The stack thing above looks like a probable fix, but there is another bug. You card likely doesn't support R8G8B8, and D3DX is silently upgrading to X8R8G8B8, so your code works. If you run this code on a card that supports R8G8B8, your code won't work. It's all designed around moving DWORDs, assuming the pitch is a DWORD multiple, etc. The easiest fix would be to actually request a 32-bit texture.

This topic is closed to new replies.

Advertisement