Jump to content
  • Advertisement
Sign in to follow this  
fpiette

DX11 [D3DX11] Creating a texture in memory

This topic is 2479 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

Hello,

Loading a texture for a png file is easy using D3DX11CreateTextureFromFile. But now I need to use an already in memory bitmap image. This image is a simple 2D matrix of pixel colors. I tough I could use D3DX11CreateTextureFromMemory to load it in the GPU but it doesn't work (No error but image simply black). Clearly I missed something :-(

Any help and sample code appreciated.

Thanks.
--
FPiette

Share this post


Link to post
Share on other sites
Advertisement
If you want someone to be able to help you, you have to post a code snippet where you call D3DX11CreateTextureFromMemory and what parameters do you use...
And what does the function return? You should do something like this:

if(FAILED(D3DX11CreateTextureFromMemory(...))
{
//Handle error
}


However in your situation, I think that you should use the CreateTexture2D(...) method and pass the 2D matrix of pixel colors into the D3D11_SUBRESOURCE_DATA parameter that CreateTexture2D takes as a parameter. Then if you want to use your newly created texture as a shader resource use CreateShaderResourceView(...) method to create the shader resource.

Share this post


Link to post
Share on other sites
That method is used for files that are loaded into memory - not just a raw array of pixel values. It sounds like you want to create a texture, and to supply the initial data to it in the creation call (or alternatively to update the texture later on with the data).

Share this post


Link to post
Share on other sites

That method is used for files that are loaded into memory - not just a raw array of pixel values.

Yes, that's what I finally understood. This is not what I need.


It sounds like you want to create a texture, and to supply the initial data to it in the creation call (or alternatively to update the texture later on with the data).

Yes, I have image data in memory and I want to create a texture out of it so that I can apply processing (Compute Shader) on it.
I bought the book "Practical Rendering & Computation with Direct3D11" but cannot find discussion about the topic I need: creating an image on the fly (by code using the CPU) and then processing it using the GPU before rendering.

In my opinion, my problem is very basic but I'm a total newbie about DirectX and DirectCompute even if I'm a very experienced developer in varius languages in other domains.

Regards,
--
Francois Piette


Any help appreciated.

Share this post


Link to post
Share on other sites

If you want someone to be able to help you, you have to post a code snippet where you call D3DX11CreateTextureFromMemory and what parameters do you use...
And what does the function return? You should do something like this:

if(FAILED(D3DX11CreateTextureFromMemory(...))
{
//Handle error
}


Here is an extract from the code I use. Not error produced.

HRESULT hr = D3DX11CreateTextureFromFile(
m_pDevice,
filename.c_str(),
pLoadInfo,
0,
&pTexture,
0
);

if ( FAILED( hr ) )
{
Log::Get().Write( L"Failed to load texture from file!" );
return( ResourcePtr( new ResourceProxyDX11() ) );
}



However in your situation, I think that you should use the CreateTexture2D(...) method and pass the 2D matrix of pixel colors into the D3D11_SUBRESOURCE_DATA parameter that CreateTexture2D takes as a parameter. Then if you want to use your newly created texture as a shader resource use CreateShaderResourceView(...) method to create the shader resource.

I will study those API.
Thanks.
--
Francois Piette

Share this post


Link to post
Share on other sites

[quote name='Jason Z' timestamp='1319995941' post='4878627']
That method is used for files that are loaded into memory - not just a raw array of pixel values.

Yes, that's what I finally understood. This is not what I need.


It sounds like you want to create a texture, and to supply the initial data to it in the creation call (or alternatively to update the texture later on with the data).

Yes, I have image data in memory and I want to create a texture out of it so that I can apply processing (Compute Shader) on it.
I bought the book "Practical Rendering & Computation with Direct3D11" but cannot find discussion about the topic I need: creating an image on the fly (by code using the CPU) and then processing it using the GPU before rendering.

In my opinion, my problem is very basic but I'm a total newbie about DirectX and DirectCompute even if I'm a very experienced developer in varius languages in other domains.

Regards,
--
Francois Piette


Any help appreciated.
[/quote]
There is a very small discussion of this in the book on page 88, although it could have a code sample to go along with it. You are basically just providing a system memory copy of the image data to the API and it will be copied into the resource upon creation. This is done by passing a D3D11_SUBRESOURCE_DATA structure to the ID3D11Device::CreateTexture2D() function.

The data layout is dependent on the texture format you are using. As long as your data is in memory in the same format that your resource will end up in, then you simply pass the system memory pointer of your image data to the pSysMem function member. I assume you are working with 2D textures, so you will also need to fill out the SysMemPitch member. This is the number of bytes between new lines of the texture data. So if you haven't padded your image data (which you most likely haven't done) then your pitch will be the x resolution times the number of bytes per pixel.

Once you do this, and pass the structure to the creation function, then your texture should be initialized with the data. You should quickly find out if there is a mismatch in format or layout, and can debug from there. Of course, if you have further questions then you can post them here or on the Hieroglyph 3 site.

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!