Sign in to follow this  

What is a view?

Recommended Posts

ourochan    100
I'm a beginner in game programming and CG,and I'm reading Frank.Luna's Red Dragon book(DX10) these days...After I finished the first 4 units,especially the initialization of the Direct3D,
I still can't understand the word "view"...

What is a view ? a data format or structuct?

Share this post

Link to post
Share on other sites
Dawoodoz    461
A view is a struct that has information about how to read a chunk of data on the memory. For example: A render target view is the input to a texture that is used to output the rendered scene together with a depth stencil view that will handle both input and output to the depth buffer of the same size.

This is a packet that I made to simplify use of textures that can be rendered to and from:

struct DrawSurfaceItem {
// Flags
bool IsScreen;

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

// 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;
1, 0,//DXGI_SAMPLE_DESC SampleDesc;
0,//UINT CPUAccessFlags;
0//UINT MiscFlags;
DXUTGetD3D11Device()->CreateTexture2D( &TextureDescription, NULL, &Surface->ColorBuffer );

// 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 = {
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->IsScreen = false;
Surface->CurrentWidth = -1;
Surface->CurrentHeight = -1;
Surface->ColorInput = NULL;
Surface->ColorBuffer = NULL;
Surface->ColorOutput = NULL;
Surface->DepthBuffer = NULL;
Surface->DepthInputAndOutput = NULL;

void GetScreenDrawSurface(DrawSurfaceItem* Surface) {
Surface->IsScreen = true;
Surface->CurrentWidth = g_Width;
Surface->CurrentHeight = g_Height;
Surface->ColorInput = DXUTGetD3D11RenderTargetView();
Surface->ColorBuffer = NULL;
Surface->ColorOutput = NULL;
Surface->DepthBuffer = NULL;
Surface->DepthInputAndOutput = DXUTGetD3D11DepthStencilView();

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

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