Sign in to follow this  
Dawoodoz

Preconditions for CreateTexture2D in DirectX11

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
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

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