3shadow_map::shadow_map()
4 : depth_texture_(nullptr), depth_stencil_view_(nullptr), shader_resource_view_(nullptr), width_(0), height_(0) {}
6shadow_map::~shadow_map() {
10bool shadow_map::initialize(ID3D11Device* device,
int width,
int height) {
14 D3D11_TEXTURE2D_DESC texDesc = {};
15 texDesc.Width = width;
16 texDesc.Height = height;
17 texDesc.MipLevels = 1;
18 texDesc.ArraySize = 1;
19 texDesc.Format = DXGI_FORMAT_R24G8_TYPELESS;
20 texDesc.SampleDesc.Count = 1;
21 texDesc.Usage = D3D11_USAGE_DEFAULT;
22 texDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE;
24 if (FAILED(device->CreateTexture2D(&texDesc,
nullptr, &depth_texture_)))
27 D3D11_DEPTH_STENCIL_VIEW_DESC dsvDesc = {};
28 dsvDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
29 dsvDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
30 dsvDesc.Texture2D.MipSlice = 0;
32 if (FAILED(device->CreateDepthStencilView(depth_texture_, &dsvDesc, &depth_stencil_view_)))
35 D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc = {};
36 srvDesc.Format = DXGI_FORMAT_R24_UNORM_X8_TYPELESS;
37 srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
38 srvDesc.Texture2D.MipLevels = 1;
40 if (FAILED(device->CreateShaderResourceView(depth_texture_, &srvDesc, &shader_resource_view_)))
46void shadow_map::shutdown() {
47 if (shader_resource_view_) { shader_resource_view_->Release(); shader_resource_view_ =
nullptr; }
48 if (depth_stencil_view_) { depth_stencil_view_->Release(); depth_stencil_view_ =
nullptr; }
49 if (depth_texture_) { depth_texture_->Release(); depth_texture_ =
nullptr; }
52void shadow_map::set_render_target(ID3D11DeviceContext* context) {
53 context->OMSetRenderTargets(0,
nullptr, depth_stencil_view_);
56void shadow_map::clear_render_target(ID3D11DeviceContext* context,
float depth) {
57 context->ClearDepthStencilView(depth_stencil_view_, D3D11_CLEAR_DEPTH, depth, 0);
60ID3D11ShaderResourceView* shadow_map::get_shader_resource_view() {
61 return shader_resource_view_;