-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgfx_utility.hpp
More file actions
355 lines (307 loc) · 13.4 KB
/
Copy pathgfx_utility.hpp
File metadata and controls
355 lines (307 loc) · 13.4 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*!
\file gfx_utility.hpp
\author Sho Ikeda
\brief GFX utility functions for GPU context, buffer, program, and kernel management
\copyright Copyright (c) 2026 Advanced Micro Devices, Inc. All Rights Reserved.
SPDX-License-Identifier: MIT
*/
#ifndef MINIDXNN_EXAMPLE_GFX_UTILITY_HPP
#define MINIDXNN_EXAMPLE_GFX_UTILITY_HPP 1
// Standard C++ library
#include <cstdint>
#include <filesystem>
#include <format>
#include <iostream>
#include <limits>
#include <memory>
#include <source_location>
#include <span>
#include <string_view>
#include <vector>
// GFX
#include "gfx.h"
// Example
#include "d3d12_format.hpp"
#include "utility.hpp"
namespace ex {
template <typename Type>
struct BindingData
{
Type m_value;
std::string_view m_name;
};
using BufferBindingDataT = BindingData<GfxBuffer>;
using IntBindingDataT = BindingData<std::int32_t>;
using FloatBindingDataT = BindingData<float>;
[[nodiscard]]
inline
auto bind(GfxBuffer buffer, const std::string_view name) -> BufferBindingDataT
{
return BufferBindingDataT{buffer, name};
}
[[nodiscard]]
inline
auto bind(const std::int32_t value, const std::string_view name) -> IntBindingDataT
{
return IntBindingDataT{value, name};
}
[[nodiscard]]
inline
auto bind(const float value, const std::string_view name) -> FloatBindingDataT
{
return FloatBindingDataT{value, name};
}
[[nodiscard]]
auto createGfxContext(const bool enableDebugShader) -> std::shared_ptr<GfxContext>;
template <typename Type>
[[nodiscard]]
auto createGfxBuffer(GfxContext context, const size_t size, const GfxCpuAccess cpuAccess = kGfxCpuAccess_None) -> std::shared_ptr<GfxBuffer>;
template <typename Type>
[[nodiscard]]
auto createGfxBuffer(GfxContext context, std::span<const Type> data, const GfxCpuAccess cpuAccess = kGfxCpuAccess_None) -> std::shared_ptr<GfxBuffer>;
[[nodiscard]]
auto createGfxProgram(GfxContext context, const std::string_view fileName, const std::filesystem::path& fileDir = ".", const std::span<const OptionString> includePathList = {}) -> std::shared_ptr<GfxProgram>;
[[nodiscard]]
auto createGfxComputeKernel(GfxContext context, GfxProgram program, const std::string_view entryPoint = "", const std::span<const OptionString> definitionList = {}) -> std::shared_ptr<GfxKernel>;
inline
auto copyBuffer(GfxContext context, GfxBuffer source, GfxBuffer dest) -> void
{
gfxCommandCopyBuffer(context, dest, source);
gfxFinish(context);
}
template <typename Type>
[[nodiscard]]
inline
auto mapToCpu(GfxContext context, GfxBuffer buffer) -> std::span<Type>
{
const size_t size = buffer.getSize() / sizeof(Type);
return std::span<Type>{gfxBufferGetData<Type>(context, buffer), size};
}
auto runKernel(GfxContext context, GfxProgram program, GfxKernel kernel, const size_t threadGroupSize = 1, std::initializer_list<BufferBindingDataT> bufferList = {}, std::initializer_list<IntBindingDataT> intList = {}, OptionalRef<float> execTimeInMs = std::nullopt, std::initializer_list<FloatBindingDataT> floatList = {}) -> void;
auto runKernel(GfxContext context, GfxProgram program, GfxKernel kernel, const size_t threadGroupSize, std::span<const BufferBindingDataT> bufferList, std::initializer_list<IntBindingDataT> intList = {}, OptionalRef<float> execTimeInMs = std::nullopt, std::initializer_list<FloatBindingDataT> floatList = {}) -> void;
class GfxAssertTrue
{
public:
GfxAssertTrue(const std::source_location& location = std::source_location::current()) : m_location{location} {}
template <typename ...Args>
auto operator()(const GfxResult result, std::format_string<Args...> messageFormat, Args&&... args) const -> void
{
if (result != kGfxResult_NoError) {
std::cerr << std::format(messageFormat, std::forward<Args>(args)...)
<< std::format(" in file: {} [line: {}, function={}]\n", m_location.file_name(), m_location.line(), m_location.function_name());
}
}
private:
std::source_location m_location;
};
template <typename Type> inline
auto createGfxBuffer(GfxContext context, const size_t size, const GfxCpuAccess cpuAccess) -> std::shared_ptr<GfxBuffer>
{
const size_t sizeInBytes = size * sizeof(Type);
GfxBuffer buffer = gfxCreateBuffer(context, sizeInBytes, nullptr, cpuAccess);
std::shared_ptr<GfxBuffer> sharedBuffer(new GfxBuffer{buffer}, [context](GfxBuffer* ptr)
{
if (ptr != nullptr) {
if (*ptr) {
GfxAssertTrue{}(gfxDestroyBuffer(context, *ptr), "Destroying the buffer failed.");
}
delete ptr;
}
});
return sharedBuffer;
}
template <typename Type> inline
auto createGfxBuffer(GfxContext context, std::span<const Type> data, const GfxCpuAccess cpuAccess) -> std::shared_ptr<GfxBuffer>
{
const size_t sizeInBytes = data.size() * sizeof(Type);
GfxBuffer buffer = gfxCreateBuffer(context, sizeInBytes, static_cast<void const*>(data.data()), cpuAccess);
std::shared_ptr<GfxBuffer> sharedBuffer(new GfxBuffer{buffer}, [context](GfxBuffer* ptr)
{
if (ptr != nullptr) {
if (*ptr) {
GfxAssertTrue{}(gfxDestroyBuffer(context, *ptr), "Destroying the buffer failed.");
}
delete ptr;
}
});
return sharedBuffer;
}
// ============================================================================
// D3D12 format buffer creation
// ============================================================================
//! Pack matrices into a GPU buffer.
//! For MUL_OPTIMAL/OUTER_PRODUCT_OPTIMAL layouts, matrices are first packed as ROW_MAJOR
//! then GPU-converted to the optimal layout via gfxConvertMatrix.
//! For ROW_MAJOR/COLUMN_MAJOR, matrices are packed directly.
//! Each entry's m_layout is updated to reflect the effective layout on the GPU buffer.
//! If allowRowMajorFallback is true (default: false), conversion failure silently falls back to ROW_MAJOR.
//! If false, conversion failure returns an empty (null) buffer.
template <Arithmetic Type> inline
auto packAsD3D12MatrixBuffer(GfxContext context,
std::span<D3D12MatrixInfo<Type>> infoList,
const bool allowRowMajorFallback = false) -> std::shared_ptr<GfxBuffer>
{
if (infoList.empty())
return {};
const MatrixLayout requestedLayout = infoList[0].m_layout;
if (needsMatrixConversion(requestedLayout)) {
// Pack as ROW_MAJOR first for GPU conversion
for (D3D12MatrixInfo<Type>& info : infoList)
info.m_layout = MatrixLayout::ROW_MAJOR;
const std::vector bufferData = packAsD3D12Matrix<Type>(infoList);
std::shared_ptr buffer = createGfxBuffer<Type>(context, bufferData);
constexpr uint32_t dataType = toD3D12DataType<Type>();
const uint32_t d3dLayout = toD3D12MatrixLayout(requestedLayout);
// Query destination sizes for all matrices
std::vector<uint32_t> destSizes(infoList.size());
size_t totalDestSize = 0;
for (size_t i = 0; i < infoList.size(); ++i) {
const uint32_t rowSize = static_cast<uint32_t>(infoList[i].m_rowSize);
const uint32_t columnSize = static_cast<uint32_t>(infoList[i].m_columnSize);
destSizes[i] = static_cast<uint32_t>(gfxGetMatrixMemorySize(context, rowSize, columnSize, d3dLayout, dataType, 0));
totalDestSize += static_cast<size_t>(destSizes[i]);
}
if (totalDestSize > 0) {
std::shared_ptr destBuffer = createGfxBuffer<uint8_t>(context, totalDestSize);
gfxFinish(context);
// Convert each matrix on the GPU
bool conversionOk = true;
uint64_t srcOffset = 0;
uint64_t dstOffset = 0;
for (size_t i = 0; i < infoList.size(); ++i) {
const uint32_t rowSize = static_cast<uint32_t>(infoList[i].m_rowSize);
const uint32_t columnSize = static_cast<uint32_t>(infoList[i].m_columnSize);
const size_t srcSizeBytes = static_cast<size_t>(rowSize) * infoList[i].m_stride;
if (srcSizeBytes > static_cast<size_t>(std::numeric_limits<uint32_t>::max())) {
conversionOk = false;
break;
}
const uint32_t srcSize = static_cast<uint32_t>(srcSizeBytes);
gfxFinish(context);
const GfxResult result = gfxConvertMatrix(context,
*destBuffer, dstOffset, destSizes[i], d3dLayout, 0, dataType,
*buffer, srcOffset, srcSize, toD3D12MatrixLayout(MatrixLayout::ROW_MAJOR), static_cast<uint32_t>(infoList[i].m_stride), dataType,
rowSize, columnSize);
if (result != kGfxResult_NoError) {
conversionOk = false;
break;
}
srcOffset += static_cast<uint64_t>(infoList[i].m_dataSize);
dstOffset += static_cast<uint64_t>(destSizes[i]);
}
if (conversionOk) {
gfxFinish(context);
// Verify conversion produced non-zero data
GfxBuffer staging = gfxCreateBuffer(context, totalDestSize, nullptr, kGfxCpuAccess_Read);
gfxCommandCopyBuffer(context, staging, 0, *destBuffer, 0, totalDestSize);
gfxFinish(context);
const void* mapped = gfxBufferGetData(context, staging);
bool hasNonZero = false;
if (mapped) {
const auto* bytes = static_cast<const uint8_t*>(mapped);
for (size_t b = 0; b < totalDestSize; ++b)
if (bytes[b] != 0) { hasNonZero = true; break; }
}
gfxDestroyBuffer(context, staging);
if (hasNonZero) {
// Update info entries to reflect the GPU-converted layout
for (size_t i = 0; i < infoList.size(); ++i) {
infoList[i].m_layout = requestedLayout;
infoList[i].m_dataSize = static_cast<size_t>(destSizes[i]);
}
return destBuffer;
}
}
}
// Conversion not available
if (allowRowMajorFallback) {
// info entries already have m_layout = ROW_MAJOR
return buffer;
}
return {};
}
// ROW_MAJOR and COLUMN_MAJOR: pack directly
const std::vector bufferData = packAsD3D12Matrix<Type>(infoList);
return createGfxBuffer<Type>(context, bufferData);
}
//! Pack vectors into a GPU buffer using D3D12VectorInfo.
template <Arithmetic Type> inline
auto packAsD3D12VectorBuffer(GfxContext context,
std::span<D3D12VectorInfo<Type>> infoList) -> std::shared_ptr<GfxBuffer>
{
const std::vector bufferData = packAsD3D12Vector<Type>(infoList);
return createGfxBuffer<Type>(context, bufferData);
}
template <Arithmetic Type> inline
auto unpackD3D12MatrixBuffer(GfxContext context,
GfxBuffer buffer,
std::span<const D3D12MatrixInfo<Type>> infoList) -> std::vector<Type>
{
if (infoList.empty())
return {};
const MatrixLayout layout = infoList[0].m_layout;
if (needsMatrixConversion(layout)) {
constexpr uint32_t dataType = toD3D12DataType<Type>();
const uint32_t srcD3dLayout = toD3D12MatrixLayout(layout);
constexpr uint32_t dstD3dLayout = toD3D12MatrixLayout(MatrixLayout::ROW_MAJOR);
std::vector<D3D12MatrixInfo<Type>> rowMajorInfoList(infoList.begin(), infoList.end());
for (auto& info : rowMajorInfoList)
info.m_layout = MatrixLayout::ROW_MAJOR;
size_t totalDstSize = 0;
for (auto& info : rowMajorInfoList) {
getD3D12MatrixInfo(info);
totalDstSize += info.m_dataSize;
}
GfxBuffer destBuffer = gfxCreateBuffer(context, totalDstSize, nullptr, kGfxCpuAccess_None);
gfxFinish(context);
uint64_t srcOffset = 0;
uint64_t dstOffset = 0;
for (size_t i = 0; i < infoList.size(); ++i) {
const uint32_t rowSize = static_cast<uint32_t>(infoList[i].m_rowSize);
const uint32_t columnSize = static_cast<uint32_t>(infoList[i].m_columnSize);
const uint32_t srcSize = static_cast<uint32_t>(infoList[i].m_dataSize);
const uint32_t dstSize = static_cast<uint32_t>(rowMajorInfoList[i].m_dataSize);
gfxFinish(context);
gfxConvertMatrix(context,
destBuffer, dstOffset, dstSize, dstD3dLayout, static_cast<uint32_t>(rowMajorInfoList[i].m_stride), dataType,
buffer, srcOffset, srcSize, srcD3dLayout, 0, dataType,
rowSize, columnSize);
srcOffset += static_cast<uint64_t>(infoList[i].m_dataSize);
dstOffset += static_cast<uint64_t>(rowMajorInfoList[i].m_dataSize);
}
gfxFinish(context);
GfxBuffer staging = gfxCreateBuffer(context, totalDstSize, nullptr, kGfxCpuAccess_Read);
gfxCommandCopyBuffer(context, staging, 0, destBuffer, 0, totalDstSize);
gfxFinish(context);
const Type* mapped = gfxBufferGetData<Type>(context, staging);
std::vector<Type> result;
size_t readOffset = 0;
for (size_t i = 0; i < rowMajorInfoList.size(); ++i) {
const size_t elemCount = rowMajorInfoList[i].m_dataSize / sizeof(Type);
std::span<const Type> src{mapped + readOffset, elemCount};
std::vector<Type> mat = convertToRowMatrix(rowMajorInfoList[i], src);
result.insert(result.end(), mat.begin(), mat.end());
readOffset += elemCount;
}
gfxDestroyBuffer(context, staging);
gfxDestroyBuffer(context, destBuffer);
return result;
}
GfxBuffer staging = gfxCreateBuffer(context, buffer.getSize(), nullptr, kGfxCpuAccess_Read);
gfxCommandCopyBuffer(context, staging, buffer);
gfxFinish(context);
const Type* mapped = gfxBufferGetData<Type>(context, staging);
std::vector<Type> result;
size_t readOffset = 0;
for (size_t i = 0; i < infoList.size(); ++i) {
const size_t elemCount = infoList[i].m_dataSize / sizeof(Type);
std::span<const Type> src{mapped + readOffset, elemCount};
std::vector<Type> mat = convertToRowMatrix(infoList[i], src);
result.insert(result.end(), mat.begin(), mat.end());
readOffset += elemCount;
}
gfxDestroyBuffer(context, staging);
return result;
}
} /* namespace ex */
#endif /* MINIDXNN_EXAMPLE_GFX_UTILITY_HPP */