Khaotic Engine Reborn
Loading...
Searching...
No Matches
shadow_map.cpp
1#include "shadow_map.h"
2
3shadow_map::shadow_map()
4 : depth_texture_(nullptr), depth_stencil_view_(nullptr), shader_resource_view_(nullptr), width_(0), height_(0) {}
5
6shadow_map::~shadow_map() {
7 shutdown();
8}
9
10bool shadow_map::initialize(ID3D11Device* device, int width, int height) {
11 width_ = width;
12 height_ = height;
13
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;
23
24 if (FAILED(device->CreateTexture2D(&texDesc, nullptr, &depth_texture_)))
25 return false;
26
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;
31
32 if (FAILED(device->CreateDepthStencilView(depth_texture_, &dsvDesc, &depth_stencil_view_)))
33 return false;
34
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;
39
40 if (FAILED(device->CreateShaderResourceView(depth_texture_, &srvDesc, &shader_resource_view_)))
41 return false;
42
43 return true;
44}
45
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; }
50}
51
52void shadow_map::set_render_target(ID3D11DeviceContext* context) {
53 context->OMSetRenderTargets(0, nullptr, depth_stencil_view_);
54}
55
56void shadow_map::clear_render_target(ID3D11DeviceContext* context, float depth) {
57 context->ClearDepthStencilView(depth_stencil_view_, D3D11_CLEAR_DEPTH, depth, 0);
58}
59
60ID3D11ShaderResourceView* shadow_map::get_shader_resource_view() {
61 return shader_resource_view_;
62}