Creating textures from memory in DirectX10

Started by
7 comments, last by EmptyVoid 15 years, 8 months ago
Well I'm not sure how everyone learns DirectX10 because I can't seem to find sample code anywhere. All I have is the reference manual but I can't figure out how to create a texture from memory. Here is what I tried but there are countless errors:


D3D10_TEXTURE2D_DESC desc;
desc.Width = 256;
desc.Height = 256;
desc.MipLevels = 1;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.Usage = D3D10_USAGE_DYNAMIC;
desc.BindFlags = D3D10_BIND_SHADER_RESOURCE;
desc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
ID3D10Texture3D* pTexture = NULL;
pd3dDevice->CreateTexture2D( &desc, NULL, &pTexture );

D3D10_MAPPED_TEXTURE2D mappedTex;
pTexture->Map( D3D10CalcSubresource(0, 0, 1), D3D10_MAP_WRITE_DISCARD, 0, &mappedTex );

UCHAR* pTexels = (UCHAR*)mappedTex.pData;
for( UINT row = 0; row < desc.Height; row++ )
{
    UINT rowStart = row * mappedTex.RowPitch;
    for( UINT col = 0; col < desc.Width; col++ )
    {
        UINT colStart = col * 4;
        pTexels[rowStart + colStart + 0] = 255; // Red
        pTexels[rowStart + colStart + 1] = 128; // Green
        pTexels[rowStart + colStart + 2] = 64;  // Blue
        pTexels[rowStart + colStart + 3] = 32;  // Alpha
    }
}

pTexture->Unmap( D3D10CalcSubresource(0, 0, 1) );

D3D10_SHADER_RESOURCE_VIEW_DESC srvDesc;
srvDesc.Format = desc.Format;
srvDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE3D;
srvDesc.Texture2D.MostDetailedMip = 0;
srvDesc.Texture2D.MostDetailedMip = desc.MipLevels;
ID3D10ShaderResourceView *pSRView = NULL;
pd3dDevice->CreateShaderResourceView( pTexture, &srvDesc, &pSRView );

effect->GetVariableByName( "txDiffuse" )->AsShaderResource()->SetResource( pSRView );



[Edited by - EmptyVoid on August 21, 2008 3:13:52 AM]
This is your life, and it's ending one minute at a time. - Fight club
Advertisement
What type of errors are you receiving exactly (Compile, Runtime, what)? What does your debugger say? Taking a look at your code, you seem to be missing a lot of error checking which is essential. Make sure check for errors and assert as soon as you receive anything except (S_OK).

Like most people, you must take the time to read the manual and understand how the SDK works. Learning DirectX programming (especially Dx10) is not a jump in and win type of thing. It takes time, patience and a lot of reading. The pay off can be great though.
Quote:Original post by shwasasin
What type of errors are you receiving exactly (Compile, Runtime, what)? What does your debugger say? Taking a look at your code, you seem to be missing a lot of error checking which is essential. Make sure check for errors and assert as soon as you receive anything except (S_OK).

Like most people, you must take the time to read the manual and understand how the SDK works. Learning DirectX programming (especially Dx10) is not a jump in and win type of thing. It takes time, patience and a lot of reading. The pay off can be great though.


Where do you think I got that code from?
Anyways:
D3D10: ERROR: ID3D10Device::CreateTexture3D: Unrecognized MiscFlags (0xcccccccc) are being used. These bits are unrecognized (0xccccccc8). [ STATE_CREATION ERROR #114: CREATETEXTURE3D_UNRECOGNIZEDMISCFLAGS ]
First-chance exception at 0x765d3843 in Tutorial08.exe: Microsoft C++ exception: _com_error at memory location 0x0017f8a8..
D3D10: ERROR: ID3D10Device::CreateTexture3D: Returning E_INVALIDARG, meaning invalid parameters were passed. [ STATE_CREATION ERROR #121: CREATETEXTURE3D_INVALIDARG_RETURN ]
First-chance exception at 0x004abfed in Tutorial08.exe: 0xC0000005: Access violation reading location 0x00000000.
Unhandled exception at 0x004abfed in Tutorial08.exe: 0xC0000005: Access violation reading location 0x00000000.

But it was doing a lot more errors before I added the mapped texture part. Now it crashes before it even gets there.
This is your life, and it's ending one minute at a time. - Fight club
Quote:Original post by EmptyVoid
Quote:Original post by shwasasin
What type of errors are you receiving exactly (Compile, Runtime, what)? What does your debugger say? Taking a look at your code, you seem to be missing a lot of error checking which is essential. Make sure check for errors and assert as soon as you receive anything except (S_OK).

Like most people, you must take the time to read the manual and understand how the SDK works. Learning DirectX programming (especially Dx10) is not a jump in and win type of thing. It takes time, patience and a lot of reading. The pay off can be great though.


Where do you think I got that code from?
Anyways:
D3D10: ERROR: ID3D10Device::CreateTexture3D: Unrecognized MiscFlags (0xcccccccc) are being used. These bits are unrecognized (0xccccccc8). [ STATE_CREATION ERROR #114: CREATETEXTURE3D_UNRECOGNIZEDMISCFLAGS ]
First-chance exception at 0x765d3843 in Tutorial08.exe: Microsoft C++ exception: _com_error at memory location 0x0017f8a8..
D3D10: ERROR: ID3D10Device::CreateTexture3D: Returning E_INVALIDARG, meaning invalid parameters were passed. [ STATE_CREATION ERROR #121: CREATETEXTURE3D_INVALIDARG_RETURN ]
First-chance exception at 0x004abfed in Tutorial08.exe: 0xC0000005: Access violation reading location 0x00000000.
Unhandled exception at 0x004abfed in Tutorial08.exe: 0xC0000005: Access violation reading location 0x00000000.

But it was doing a lot more errors before I added the mapped texture part. Now it crashes before it even gets there.


Of course it crashes, pTexture is NULL (because CreateTexture3D fails) and you don't have any error checking to avoid trying to access members inside pTexture, causing it to crash.
Quote:Original post by shwasasin
Quote:Original post by EmptyVoid
Quote:Original post by shwasasin
What type of errors are you receiving exactly (Compile, Runtime, what)? What does your debugger say? Taking a look at your code, you seem to be missing a lot of error checking which is essential. Make sure check for errors and assert as soon as you receive anything except (S_OK).

Like most people, you must take the time to read the manual and understand how the SDK works. Learning DirectX programming (especially Dx10) is not a jump in and win type of thing. It takes time, patience and a lot of reading. The pay off can be great though.


Where do you think I got that code from?
Anyways:
D3D10: ERROR: ID3D10Device::CreateTexture3D: Unrecognized MiscFlags (0xcccccccc) are being used. These bits are unrecognized (0xccccccc8). [ STATE_CREATION ERROR #114: CREATETEXTURE3D_UNRECOGNIZEDMISCFLAGS ]
First-chance exception at 0x765d3843 in Tutorial08.exe: Microsoft C++ exception: _com_error at memory location 0x0017f8a8..
D3D10: ERROR: ID3D10Device::CreateTexture3D: Returning E_INVALIDARG, meaning invalid parameters were passed. [ STATE_CREATION ERROR #121: CREATETEXTURE3D_INVALIDARG_RETURN ]
First-chance exception at 0x004abfed in Tutorial08.exe: 0xC0000005: Access violation reading location 0x00000000.
Unhandled exception at 0x004abfed in Tutorial08.exe: 0xC0000005: Access violation reading location 0x00000000.

But it was doing a lot more errors before I added the mapped texture part. Now it crashes before it even gets there.


Of course it crashes, pTexture is NULL (because CreateTexture3D fails) and you don't have any error checking to avoid trying to access members inside pTexture, causing it to crash.


I was just using the code the reference manual told me to... Why does CreateTexture3D crash? What does the error mean? I have no idea what I need to do.
This is your life, and it's ending one minute at a time. - Fight club
I've never used DirectX10 but quick inspection of the error message answers your question.

CreateTexture3D is failing because of the MiscFlags. MiscFlags is a member of the D3D10_TEXTURE3D_DESC structure, which you don't initialize. When you try to create the texture you're passing it some garbage parameters.

Either initialize all of the members of your structure or call ZeroMemory before initializing the ones you care about.
Quote:Original post by pi_equals_3
I've never used DirectX10 but quick inspection of the error message answers your question.

CreateTexture3D is failing because of the MiscFlags. MiscFlags is a member of the D3D10_TEXTURE3D_DESC structure, which you don't initialize. When you try to create the texture you're passing it some garbage parameters.

Either initialize all of the members of your structure or call ZeroMemory before initializing the ones you care about.


I got a problem remembering to clean up because I'm used to managed code. Well thanks and everything is working. I'll link the code to help people in the future:

D3D10_TEXTURE2D_DESC desc;ZeroMemory( &desc, sizeof( desc ) );desc.Width = 256;desc.Height = 256;desc.ArraySize = 1;desc.MipLevels = 1;desc.SampleDesc.Count=1;desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;desc.Usage = D3D10_USAGE_DYNAMIC;desc.BindFlags = D3D10_BIND_SHADER_RESOURCE;desc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;ID3D10Texture2D* pTexture = NULL;pd3dDevice->CreateTexture2D( &desc, NULL, &pTexture );D3D10_MAPPED_TEXTURE2D mappedTex;pTexture->Map( D3D10CalcSubresource(0, 0, 1), D3D10_MAP_WRITE_DISCARD, 0, &mappedTex );UCHAR* pTexels = (UCHAR*)mappedTex.pData;for( UINT row = 0; row < desc.Height; row++ ){    UINT rowStart = row * mappedTex.RowPitch;    for( UINT col = 0; col < desc.Width; col++ )    {        UINT colStart = col * 4;        pTexels[rowStart + colStart + 0] = 255; // Red        pTexels[rowStart + colStart + 1] = 128; // Green        pTexels[rowStart + colStart + 2] = 64;  // Blue        pTexels[rowStart + colStart + 3] = 32;  // Alpha    }}pTexture->Unmap( D3D10CalcSubresource(0, 0, 1) );D3D10_SHADER_RESOURCE_VIEW_DESC srvDesc;ZeroMemory( &srvDesc, sizeof( srvDesc ) );srvDesc.Format = desc.Format;srvDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D;srvDesc.Texture2D.MostDetailedMip = 0;srvDesc.Texture2D.MipLevels = desc.MipLevels;ID3D10ShaderResourceView *pSRView = NULL;pd3dDevice->CreateShaderResourceView( pTexture, &srvDesc, &pSRView );effect->GetVariableByName( "txDiffuse" )->AsShaderResource()->SetResource( pSRView );
This is your life, and it's ending one minute at a time. - Fight club
Quote:Original post by pi_equals_3
Either initialize all of the members of your structure or call ZeroMemory before initializing the ones you care about.
Just fwiw, this ZeroMemory() step should become 2nd nature and automatic when you're dealing with POD types.

Quote:Unrecognized MiscFlags (0xcccccccc) are being used.
Secondly, you need to remember that the debug builds/runtimes of plain old VC++ are trying to help you. It places values like 0xcccccccc into uninitialized variables specifically to alert you to this issue - seeing that in an error message or log file output should be an instant "ah-hah!" moment.

Even forgetting the thing about ZeroMemory(), the above quoted fragment tells you everything you needed to solve this problem [smile]

Cheers,
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Quote:Original post by jollyjeffers
Quote:Original post by pi_equals_3
Either initialize all of the members of your structure or call ZeroMemory before initializing the ones you care about.
Just fwiw, this ZeroMemory() step should become 2nd nature and automatic when you're dealing with POD types.

Quote:Unrecognized MiscFlags (0xcccccccc) are being used.
Secondly, you need to remember that the debug builds/runtimes of plain old VC++ are trying to help you. It places values like 0xcccccccc into uninitialized variables specifically to alert you to this issue - seeing that in an error message or log file output should be an instant "ah-hah!" moment.

Even forgetting the thing about ZeroMemory(), the above quoted fragment tells you everything you needed to solve this problem [smile]

Cheers,
Jack


I just didn't understand it. It's easy to know what something is after you have experienced it but I just thought it was something completely random. Also your the reason I don't know these things, you got me started using VB/DX =P
This is your life, and it's ending one minute at a time. - Fight club

This topic is closed to new replies.

Advertisement