-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleShaderDescriptors.cpp
More file actions
101 lines (81 loc) · 3.07 KB
/
Copy pathModuleShaderDescriptors.cpp
File metadata and controls
101 lines (81 loc) · 3.07 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "Globals.h"
#include "ModuleShaderDescriptors.h"
#include "Application.h"
#include "ModuleD3D12.h"
bool ModuleShaderDescriptors::init()
{
auto device = app->getD3D12()->getDevice();
if (!device) {
LOG("ERROR: ModuleShaderDescriptors::init: No D3D12 device!");
return false;
}
// Create SRV heap
D3D12_DESCRIPTOR_HEAP_DESC heapDesc = {};
heapDesc.NumDescriptors = 256; // Room for 256 textures
heapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
heapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
HRESULT hr = device->CreateDescriptorHeap(&heapDesc, IID_PPV_ARGS(&m_srvHeap));
if (FAILED(hr)) {
LOG("ERROR: Failed to create SRV descriptor heap: 0x%08X", hr);
return false;
}
m_srvDescriptorSize = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
LOG("ModuleShaderDescriptors initialized: Heap size=%u, Descriptor size=%u",
heapDesc.NumDescriptors, m_srvDescriptorSize);
// ADDING NULL DESCRIPTOR AT INDEX 0
CD3DX12_CPU_DESCRIPTOR_HANDLE cpuHandle(
m_srvHeap->GetCPUDescriptorHandleForHeapStart(),
0,
m_srvDescriptorSize);
// Create a null SRV descriptor
D3D12_SHADER_RESOURCE_VIEW_DESC nullSrvDesc = {};
nullSrvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
nullSrvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
nullSrvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
nullSrvDesc.Texture2D.MipLevels = 1;
nullSrvDesc.Texture2D.MostDetailedMip = 0;
device->CreateShaderResourceView(nullptr, &nullSrvDesc, cpuHandle);
// Start next index at 1 since 0 is used for null descriptor
m_nextIndex = 1;
LOG("ModuleShaderDescriptors initialized with null descriptor at index 0");
return true;
}
UINT ModuleShaderDescriptors::createSRV(ID3D12Resource* texture)
{
if (!texture) return 0;
auto device = app->getD3D12()->getDevice();
UINT index = 0;
// Reuse free indices if available
if (!m_freeIndices.empty()) {
index = m_freeIndices.back();
m_freeIndices.pop_back();
}
else {
index = m_nextIndex++;
}
// Create SRV
D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc = {};
srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
srvDesc.Format = texture->GetDesc().Format;
srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MipLevels = 1;
CD3DX12_CPU_DESCRIPTOR_HANDLE cpuHandle(
m_srvHeap->GetCPUDescriptorHandleForHeapStart(),
index,
m_srvDescriptorSize);
device->CreateShaderResourceView(texture, &srvDesc, cpuHandle);
return index;
}
D3D12_GPU_DESCRIPTOR_HANDLE ModuleShaderDescriptors::getSRVGPUHandle(UINT index)
{
if (index == 0) {
// Return null handle
D3D12_GPU_DESCRIPTOR_HANDLE nullHandle = { 0 };
return nullHandle;
}
CD3DX12_GPU_DESCRIPTOR_HANDLE gpuHandle(
m_srvHeap->GetGPUDescriptorHandleForHeapStart(),
index,
m_srvDescriptorSize);
return gpuHandle;
}