Jump to content
  • Advertisement
Sign in to follow this  
Dawoodoz

Preconditions for CreateTexture2D in DirectX11

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

What are the preconditions for CreateTexture2D in DirectX11?

CreateTexture2D causes access violation by reading null but each part of the constructor worked well before I gathered the pieces to routines.


struct DrawSurfaceItem {
// Dimensions
int CurrentWidth;
int CurrentHeight;

// Color buffer
ID3D11RenderTargetView* ColorInput; // Input
ID3D11Texture2D* ColorBuffer; // Buffer
ID3D11ShaderResourceView* ColorOutput; // Output

// Depth buffer
ID3D11Texture2D* DepthBuffer; // Buffer
ID3D11DepthStencilView* DepthInputAndOutput; // Input and output
};

void SetSizeOfDrawSurface(DrawSurfaceItem* Surface, int NewWidth, int NewHeight) {
if (NewWidth < 1 || NewHeight < 1) {
MessageBox(NULL, L"DrawSurfaceItem::SetSizeOfDrawSurface was called with non positive dimensions. Width and height may not be less than 1.", L"Error!", NULL);
} else if (Surface->CurrentWidth != NewWidth || Surface->CurrentHeight != NewHeight) {
// Release any old data to avoid memory leaks
ReleaseDrawSurface(Surface);

// Store dimensions
Surface->CurrentWidth = NewWidth;
Surface->CurrentHeight = NewHeight;

// Color buffer
D3D11_TEXTURE2D_DESC TextureDescription = {
NewWidth,//UINT Width;
NewHeight,//UINT Height;
1,//UINT MipLevels;
1,//UINT ArraySize;
DXGI_FORMAT_R32G32B32A32_FLOAT,//DXGI_FORMAT Format;
1, 0,//DXGI_SAMPLE_DESC SampleDesc;
D3D11_USAGE_DEFAULT,//D3D11_USAGE Usage;
D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET,//UINT BindFlags;
0,//UINT CPUAccessFlags; "0" or "D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE"
0//UINT MiscFlags;
};
DXUTGetD3D11Device()->CreateTexture2D( &TextureDescription, NULL, &Surface->ColorBuffer ); // ¤¤¤¤¤¤¤¤ The bug! ¤¤¤¤¤¤¤¤

// Depth buffer
D3D11_TEXTURE2D_DESC DepthTextureDescription = TextureDescription;
DepthTextureDescription.Format = DXGI_FORMAT_R32_TYPELESS;
DepthTextureDescription.BindFlags = D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE;
DXUTGetD3D11Device()->CreateTexture2D( &DepthTextureDescription, NULL, &Surface->DepthBuffer );

// Depth I/O
D3D11_DEPTH_STENCIL_VIEW_DESC DepthIODescription = {
DXGI_FORMAT_D32_FLOAT,
D3D11_DSV_DIMENSION_TEXTURE2D,
0
};
DXUTGetD3D11Device()->CreateDepthStencilView( Surface->DepthBuffer, &DepthIODescription, &Surface->DepthInputAndOutput );

// Color output
D3D11_SHADER_RESOURCE_VIEW_DESC TextureOutputDescription = { TextureDescription.Format, D3D11_SRV_DIMENSION_TEXTURE2D, 0, 0 };
TextureOutputDescription.Texture2D.MipLevels = 1;
DXUTGetD3D11Device()->CreateShaderResourceView( Surface->ColorBuffer, &TextureOutputDescription, &Surface->ColorOutput );

// Color input
D3D11_RENDER_TARGET_VIEW_DESC TextureInputDescription;
TextureInputDescription.Format = TextureDescription.Format;
TextureInputDescription.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
TextureInputDescription.Texture2D.MipSlice = 0;
DXUTGetD3D11Device()->CreateRenderTargetView(Surface->ColorBuffer, &TextureInputDescription, &Surface->ColorInput );
}
}

void InitEmptyDrawSurface(DrawSurfaceItem* Surface) {
Surface->CurrentWidth = -1;
Surface->CurrentHeight = -1;
Surface->ColorInput = NULL;
Surface->ColorBuffer = NULL;
Surface->ColorOutput = NULL;
Surface->DepthBuffer = NULL;
Surface->DepthInputAndOutput = NULL;
}

void ReleaseDrawSurface(DrawSurfaceItem* Surface) {
SAFE_RELEASE( Surface->ColorInput );
SAFE_RELEASE( Surface->ColorBuffer );
SAFE_RELEASE( Surface->ColorOutput );
SAFE_RELEASE( Surface->DepthBuffer );
SAFE_RELEASE( Surface->DepthInputAndOutput );
}

Share this post


Link to post
Share on other sites
Advertisement
Sounds like DXUTGetD3D11Device() returns NULL. What does your debugger say?
You should be able to inspect the values of your different variables.

Share this post


Link to post
Share on other sites
Thanks, it did return NULL. I have moved the initialization of the device to a place before the surface allocation and the engine is running again.

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!