-
Notifications
You must be signed in to change notification settings - Fork 226
/
Mesh.cpp
145 lines (125 loc) · 4.34 KB
/
Mesh.cpp
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//--------------------------------------------------------------------------------------
// D3D12RaytracingAO.h
//
// Advanced Technology Group (ATG)
// Copyright (C) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include "pch.h"
#include "GlobalSharedHlslCompat.h"
#include "Mesh.h"
using namespace DirectX;
Mesh::Mesh(
ID3D12Device* device,
ID3D12CommandQueue* commandQueue,
const wchar_t* pFileName)
{
// Load sdk mesh into memory.
m_model = Model::CreateFromSDKMESH(device, pFileName);
// Check the sample's assumptions are correct.
for (auto& mesh : m_model->meshes)
{
if (mesh->alphaMeshParts.size() > 0)
{
throw std::exception("Alpha Mesh is not allowed in this sample.");
}
for (unsigned int i = 0; i < mesh->opaqueMeshParts.size(); i++)
{
if (mesh->opaqueMeshParts[i]->indexFormat != DXGI_FORMAT_R32_UINT)
{
throw std::exception("Only 32bit unsigned int indices can be used for this sample.");
}
if (mesh->opaqueMeshParts[i]->primitiveType != D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST)
{
throw std::exception("Only triangle lists are supported for this sample.");
}
if (mesh->opaqueMeshParts[i]->vertexStride != sizeof(Vertex))
{
throw std::exception("Vertex format does not mach that of the sample.");
}
// Increase length;
length++;
}
}
// Check if length is too large.
if (length >= Parts::MaxParts)
{
throw std::exception("The number of parts in the scene exceeds MaxParts.");
}
// Load Textures.
ResourceUploadBatch resourceUpload(device);
resourceUpload.Begin();
// Extract filepath and create texture factory.
std::wstring fileName(pFileName);
auto lastPos = fileName.find_last_of('\\');
m_textureFactory = m_model->LoadTextures(
device,
resourceUpload,
(lastPos != std::string::npos) ? fileName.substr(0, lastPos).c_str() : nullptr
);
// Move buffers out of upload state.
m_model->LoadStaticBuffers(device, resourceUpload);
// Wait on texture upload.
auto future = resourceUpload.End(commandQueue);
future.wait();
// Texture factory is only a nullptr if there are no textures.
if (m_textureFactory)
{
assert(m_textureFactory->ResourceCount() < Textures::MaxTextures);
// Check texture type is 2D.
for (unsigned int i = 0; i < m_textureFactory->ResourceCount(); i++)
{
bool isCubeMap;
Microsoft::WRL::ComPtr<ID3D12Resource> resource;
m_textureFactory->GetResource(i, &resource, &isCubeMap);
if (isCubeMap)
{
throw std::exception("Cube maps are not supported for this sample.");
}
if (resource->GetDesc().Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D)
{
throw std::exception("Only Texture2D images are supported in assets.");
}
if (resource->GetDesc().DepthOrArraySize != 1)
{
throw std::exception("Only depth and array sizes of one are supported in assets.");
}
}
}
}
void Mesh::SetTextureSRV(
ID3D12Device* device,
D3D12_CPU_DESCRIPTOR_HANDLE pStartHandle,
int matIndex)
{
if (m_textureFactory && (matIndex != -1))
{
bool isCubeMap;
Microsoft::WRL::ComPtr<ID3D12Resource> resource;
m_textureFactory->GetResource(matIndex, &resource, &isCubeMap);
device->CreateShaderResourceView(
resource.Get(),
nullptr,
pStartHandle
);
}
}
void Mesh::SetTextureSRVs(
ID3D12Device* device,
D3D12_CPU_DESCRIPTOR_HANDLE pStartHandle,
SIZE_T increment)
{
if (m_textureFactory)
{
for (unsigned int i = 0; i < m_textureFactory->ResourceCount(); i++)
{
bool isCubeMap;
ID3D12Resource* resource;
m_textureFactory->GetResource(i, &resource, &isCubeMap);
device->CreateShaderResourceView(
resource,
nullptr,
{ pStartHandle.ptr + increment * SIZE_T(i) }
);
}
}
}