-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShadowMap.cpp
More file actions
executable file
·70 lines (55 loc) · 2.15 KB
/
ShadowMap.cpp
File metadata and controls
executable file
·70 lines (55 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "ShadowMap.h"
ShadowMap::ShadowMap(ID3D11Device* device, UINT width, UINT height):mWidth(width),mHeight(height),mDepthMapSRV(0),mDepthMapDSV(0)
{
mViewport.TopLeftX = 0.0f;
mViewport.TopLeftY = 0.0f;
mViewport.Width = static_cast<float>(width);
mViewport.Height = static_cast<float>(height);
mViewport.MinDepth = 0.0f;
mViewport.MaxDepth = 1.0f;
//使用无类型的格式,因为DSV将用DXGI_FORMAT_D24_UNORM_S8_UINT修改数据,SRV将用DXGI_FORMAT_R24_UNORM_X8_TYPELESS修改位
D3D11_TEXTURE2D_DESC texDesc;
texDesc.Width = mWidth;
texDesc.Height = mHeight;
texDesc.MipLevels = 1;
texDesc.ArraySize = 1;
texDesc.Format = DXGI_FORMAT_R24G8_TYPELESS;
texDesc.SampleDesc.Count = 1;
texDesc.SampleDesc.Quality = 0;
texDesc.Usage = D3D11_USAGE_DEFAULT;
texDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE;
texDesc.CPUAccessFlags = 0;
texDesc.MiscFlags = 0;
ID3D11Texture2D* depthMap = 0;
HR(device->CreateTexture2D(&texDesc,0,&depthMap));
D3D11_DEPTH_STENCIL_VIEW_DESC dsvDesc;
dsvDesc.Flags = 0;
dsvDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
dsvDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
dsvDesc.Texture2D.MipSlice = 0;
HR(device->CreateDepthStencilView(depthMap,&dsvDesc,&mDepthMapDSV));
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
srvDesc.Format = DXGI_FORMAT_R24_UNORM_X8_TYPELESS;
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MipLevels = texDesc.MipLevels;
srvDesc.Texture2D.MostDetailedMip = 0;
HR(device->CreateShaderResourceView(depthMap,&srvDesc,&mDepthMapSRV));
ReleaseCOM(depthMap);
}
ShadowMap::~ShadowMap(void)
{
ReleaseCOM(mDepthMapSRV);
ReleaseCOM(mDepthMapDSV);
}
ID3D11ShaderResourceView* ShadowMap::DeptchMapSRV()
{
return mDepthMapSRV;
}
void ShadowMap::BindDsvAndSetNullRenderTarget(ID3D11DeviceContext* dc)
{
dc->RSSetViewports(1,&mViewport);
//设置render target 为null,因为我们只会绘制到depth buffer中。设置null renderTarget会禁掉颜色写入。
ID3D11RenderTargetView* renderTargets[1] = {0};
dc->OMSetRenderTargets(1,renderTargets,mDepthMapDSV);
dc->ClearDepthStencilView(mDepthMapDSV,D3D11_CLEAR_DEPTH,1.0f,0);
}