Jump to content
  • Advertisement
Sign in to follow this  
xytor

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

This topic is 2804 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!