-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataBlock.cpp
More file actions
131 lines (88 loc) · 4.57 KB
/
DataBlock.cpp
File metadata and controls
131 lines (88 loc) · 4.57 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "DataBlock.h"
#include "Renderer.h"
#define STB_IMAGE_IMPLEMENTATION
#include "external/stb_image.h"
#ifdef _DEBUG
#pragma comment(lib, "external/DirectXTK/lib/x64/Debug/DirectXTK12.lib")
#else
#pragma comment(lib, "external/DirectXTK/lib/x64/Release/DirectXTK12.lib")
#endif
#include "external/DirectXTK/include/ResourceUploadBatch.h"
void DataBlock::Reset()
{
m_UAVDescriptorHeapIndex = UINT_MAX;
}
void DataBlock::Load(Renderer& renderer, const char* fileName)
{
int width = 0, height = 0, n = 0;
uint32 *mem = (uint32*)stbi_load(fileName, &width, &height, &n, 4);
if(!mem)
{
assert(0);
return;
}
auto commandQueue = renderer.directCommandQueue;
ID3D12GraphicsCommandList2* commandList = commandQueue->GetCommandList().Get();
m_desc = {};
m_desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
m_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
m_desc.MipLevels = 1;
m_desc.Width = width;
m_desc.Height = height;
m_desc.DepthOrArraySize = 1;
m_desc.SampleDesc.Count = 1;
m_desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
// https://www.braynzarsoft.net/viewtutorial/q16390-directx-12-textures-from-file
UINT64 textureUploadBufferSize = 0;
// this function gets the size an upload buffer needs to be to upload a texture to the gpu.
// each row must be 256 byte aligned except for the last row, which can just be the size in bytes of the row
// eg. textureUploadBufferSize = ((((width * numBytesPerPixel) + 255) & ~255) * (height - 1)) + (width * numBytesPerPixel);
//textureUploadBufferSize = (((imageBytesPerRow + 255) & ~255) * (textureDesc.Height - 1)) + imageBytesPerRow;
renderer.device->GetCopyableFootprints(&m_desc, 0, 1, 0, nullptr, nullptr, nullptr, &textureUploadBufferSize);
ComPtr<ID3D12Resource> textureBufferUploadHeap;
CD3DX12_RESOURCE_DESC sdesc = CD3DX12_RESOURCE_DESC::Buffer(textureUploadBufferSize);
auto defaultHeapProperties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);
ThrowIfFailed(renderer.device->CreateCommittedResource(
&defaultHeapProperties, D3D12_HEAP_FLAG_NONE, &m_desc, D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&m_resource)));
NAME_D3D12_OBJECT(m_resource);
DirectX::ResourceUploadBatch resourceUpload(renderer.device.Get());
resourceUpload.Begin();
// store vertex buffer in upload heap
D3D12_SUBRESOURCE_DATA textureData = {};
textureData.pData = mem;
textureData.RowPitch = (Renderer::GetFormatBitsPerPixel(m_desc.Format) * m_desc.Width) / 8;
textureData.SlicePitch = textureData.RowPitch * m_desc.Height;
resourceUpload.Upload(m_resource.Get(), 0, &textureData, 1);
resourceUpload.Transition(
m_resource.Get(),
D3D12_RESOURCE_STATE_COPY_DEST,
D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
auto uploadResourcesFinished = resourceUpload.End(commandQueue->GetD3D12CommandQueue().Get());
uploadResourcesFinished.wait();
D3D12_CPU_DESCRIPTOR_HANDLE uavDescriptorHandle;
m_UAVDescriptorHeapIndex = renderer.descriptorHeapR.AllocateDescriptor(&uavDescriptorHandle, m_UAVDescriptorHeapIndex);
D3D12_UNORDERED_ACCESS_VIEW_DESC UAVDesc = {};
UAVDesc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D;
renderer.device->CreateUnorderedAccessView(m_resource.Get(), nullptr, &UAVDesc, uavDescriptorHandle);
m_UAVGpuDescriptor = CD3DX12_GPU_DESCRIPTOR_HANDLE(
renderer.descriptorHeapR.descriptorHeap->GetGPUDescriptorHandleForHeapStart(),
m_UAVDescriptorHeapIndex,
renderer.descriptorHeapR.maxSize);
stbi_image_free(mem);
}
void DataBlock::CreateUAV(Renderer& renderer)
{
auto defaultHeapProperties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);
ThrowIfFailed(renderer.device->CreateCommittedResource(
&defaultHeapProperties, D3D12_HEAP_FLAG_NONE, &m_desc, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, nullptr, IID_PPV_ARGS(&m_resource)));
NAME_D3D12_OBJECT(m_resource);
D3D12_CPU_DESCRIPTOR_HANDLE uavDescriptorHandle;
m_UAVDescriptorHeapIndex = renderer.descriptorHeapR.AllocateDescriptor(&uavDescriptorHandle, m_UAVDescriptorHeapIndex);
D3D12_UNORDERED_ACCESS_VIEW_DESC UAVDesc = {};
UAVDesc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D;
renderer.device->CreateUnorderedAccessView(m_resource.Get(), nullptr, &UAVDesc, uavDescriptorHandle);
m_UAVGpuDescriptor = CD3DX12_GPU_DESCRIPTOR_HANDLE(
renderer.descriptorHeapR.descriptorHeap->GetGPUDescriptorHandleForHeapStart(),
m_UAVDescriptorHeapIndex,
renderer.descriptorHeapR.maxSize);
}