Sign in to follow this  
xytor

[DirectX 9] Make a texture with CPU to pass to HLSL

Recommended Posts

xytor    136
I want to make a texture using the CPU by writing all the values, but it crashes because the data is invalid. What am I doing wrong?


LPDIRECT3DTEXTURE9 Texture;
pDevice->CreateTexture(resX,resY,1,D3DUSAGE_RENDERTARGET,D3DFMT_X8R8G8B8,D3DPOOL_DEFAULT,&Texture,NULL);
D3DLOCKED_RECT lr;
Texture->LockRect(1,&lr,NULL,0);
int index = 0;
for(int i=0;i<resY;i++)
{
for(int j=0;j<resX;j++)
{
int value = getNextValue();
for(int k=0;k<4;k++)
{
((unsigned char*)lr.pBits)[index] = value;
index++;
}
}
}
Texture->UnlockRect(1);
shader->SetTexture("Tex",Texture);

Share this post


Link to post
Share on other sites
Noggs    141
First of all you should check the result of each D3D call. that will likely tell you where the problem lies. If you need more help then turn on DirectX debug runtime and up the level in the DirectX control panel. Check the output for possible problems and warnings.

Onto the specific problem:

Firstly try changing the Usage parameter to be D3DUSAGE_DYNAMIC
Secondly you should call LockRect(0, ...) to lock the first mip level.

Share this post


Link to post
Share on other sites
xytor    136
ok, now I use


pDevice->CreateTexture(resX,resY,1,D3DUSAGE_DYNAMIC,D3DFMT_X8R8G8B8,D3DPOOL_DEFAULT,&Texture,NULL);
D3DLOCKED_RECT lr;
Texture->LockRect(0,&lr,NULL,0);

//then later
Texture->UnlockRect(0);

shader->SetTexture("Tex",Texture);

//then later
pDevice->EndScene();
if(Texture)
{
Texture->Release();
Texture = NULL;
}





It seems to be working well, thanks :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this