-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGDeflate.cpp
More file actions
410 lines (339 loc) · 13.9 KB
/
GDeflate.cpp
File metadata and controls
410 lines (339 loc) · 13.9 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
* SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: LicenseRef-NvidiaProprietary
*
* NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
* property and proprietary rights in and to this material, related
* documentation and any modifications thereto. Any use, reproduction,
* disclosure or distribution of this material and related documentation
* without an express license agreement from NVIDIA CORPORATION or
* its affiliates is strictly prohibited.
*/
#include "GDeflate.h"
#include "Errors.h"
#include "GraphicsResources.h"
#include "ThreadPool.h"
#include <libdeflate.h>
namespace ntc
{
#define REQUIRE_NOT_NULL(arg) if (arg == nullptr) { SetErrorMessage(#arg " is NULL."); return Status::InvalidArgument; }
#define REQUIRE_NOT_ZERO(arg) if (arg == 0) { SetErrorMessage(#arg " is 0."); return Status::InvalidArgument; }
constexpr int kDefaultCompressionLevel = 9;
constexpr uint32_t kDefaultTileSize = 64 * 1024;
// The format and semantics of the DirectStorage compressed stream for GDeflate data are taken from the
// reference implementation of GDeflate: https://github.com/microsoft/DirectStorage/tree/main/GDeflate/GDeflate
struct TileStream
{
static constexpr uint8_t kGDeflateId = 4;
static constexpr uint32_t kMaxTiles = (1 << 16) - 1;
uint8_t id;
uint8_t magic;
uint16_t numTiles;
uint32_t tileSizeIdx : 2; // this must be set to 1
uint32_t lastTileSize : 18;
uint32_t reserved1 : 12;
};
static size_t GetDirectStorageStreamHeaderSize(size_t numPages)
{
return sizeof(TileStream) + sizeof(uint32_t) * numPages;
}
class CompressTileTask : public ThreadPoolTask
{
public:
bool Run() override
{
libdeflate_gdeflate_compressor* compressor = libdeflate_alloc_gdeflate_compressor(compressionLevel);
if (!compressor)
return false;
size_t compressedSize = libdeflate_gdeflate_compress(compressor, uncompressedData, uncompressedSize, page, 1);
libdeflate_free_gdeflate_compressor(compressor);
return compressedSize != 0;
}
libdeflate_gdeflate_out_page* page = nullptr;
void const* uncompressedData = nullptr;
size_t uncompressedSize = 0;
int compressionLevel = 0;
};
bool CompressGDeflate(const void* uncompressedData, size_t uncompressedSize,
Vector<uint8_t>& compressedData, ThreadPool* threadPool, IAllocator* allocator,
uint32_t* pOutCrc32, int compressionLevel)
{
if (compressionLevel <= 0)
compressionLevel = kDefaultCompressionLevel;
else
compressionLevel = std::min(compressionLevel, 12);
size_t numPages = uncompressedSize / kDefaultTileSize;
size_t const lastPageSize = uncompressedSize - kDefaultTileSize * numPages;
if (lastPageSize != 0)
++numPages;
if (numPages > TileStream::kMaxTiles)
return false;
// Use a custom page size bound that's slightly larger than what libdeflate reports (65708).
// Sometimes, using the libdeflate bound results in heap corruption when compressing latents
// because they don't compress well and produce large outputs.
//
// size_t const pageSizeBound = libdeflate_gdeflate_compress_bound(compressor, kDefaultTileSize, nullptr);
//
size_t const pageSizeBound = kDefaultTileSize + 1024;
size_t const compressedSizeBound = numPages * pageSizeBound;
Vector<uint8_t> outputBuffer(allocator);
outputBuffer.resize(compressedSizeBound);
bool const useThreadPool = threadPool && numPages > 1;
Vector<libdeflate_gdeflate_out_page> pages(allocator);
pages.resize(numPages);
size_t consumedSize = 0;
for (size_t i = 0; i < numPages; ++i)
{
pages[i].data = outputBuffer.data() + i * pageSizeBound;
pages[i].nbytes = pageSizeBound;
if (useThreadPool)
{
std::shared_ptr<CompressTileTask> task = MakeShared<CompressTileTask>(allocator);
task->page = &pages[i];
task->compressionLevel = compressionLevel;
task->uncompressedData = static_cast<uint8_t const*>(uncompressedData) + consumedSize;
task->uncompressedSize = std::min(uncompressedSize - consumedSize, size_t(kDefaultTileSize));
consumedSize += task->uncompressedSize;
threadPool->AddTask(std::move(task));
}
}
size_t compressedSize = 0;
if (useThreadPool)
{
if (threadPool->WaitForTasks())
{
for (libdeflate_gdeflate_out_page const& page : pages)
compressedSize += page.nbytes;
}
}
else
{
libdeflate_gdeflate_compressor* compressor = libdeflate_alloc_gdeflate_compressor(compressionLevel);
if (!compressor)
return false;
compressedSize = libdeflate_gdeflate_compress(compressor,
uncompressedData, uncompressedSize,
pages.data(), pages.size());
libdeflate_free_gdeflate_compressor(compressor);
}
if (compressedSize == 0)
return false;
size_t const headerSize = GetDirectStorageStreamHeaderSize(numPages);
compressedData.resize(headerSize + compressedSize);
TileStream* header = reinterpret_cast<TileStream*>(compressedData.data());
uint32_t* tileOffsets = reinterpret_cast<uint32_t*>(compressedData.data() + sizeof(TileStream));
header->id = TileStream::kGDeflateId;
header->magic = header->id ^ 0xff;
header->numTiles = numPages;
header->tileSizeIdx = 1;
header->lastTileSize = lastPageSize;
tileOffsets[0] = lastPageSize;
size_t compressedOffset = 0;
for (size_t i = 0; i < numPages; ++i)
{
if (i > 0)
tileOffsets[i] = compressedOffset;
compressedOffset += pages[i].nbytes;
}
size_t outputOffset = headerSize;
for (size_t i = 0; i < numPages; ++i)
{
// Check for buffer overflow
if (outputOffset + pages[i].nbytes > compressedData.size())
return false;
// Write page data
memcpy(compressedData.data() + outputOffset, pages[i].data, pages[i].nbytes);
outputOffset += pages[i].nbytes;
}
if (pOutCrc32)
{
*pOutCrc32 = libdeflate_crc32(0, uncompressedData, uncompressedSize);
}
return true;
}
Status DecompressGDeflate(void const* compressedData, size_t compressedSize,
void* uncompressedData, size_t uncompressedSize, IAllocator* allocator, uint32_t expectedCrc32)
{
// Validate the input parameters
REQUIRE_NOT_NULL(compressedData);
REQUIRE_NOT_NULL(uncompressedData);
REQUIRE_NOT_ZERO(uncompressedSize);
REQUIRE_NOT_NULL(allocator);
if (compressedSize < sizeof(TileStream))
{
SetErrorMessage("Compressed buffer is too small (%zu bytes).", compressedSize);
return Status::InvalidData;
}
TileStream const* header = static_cast<TileStream const*>(compressedData);
if (header->id != TileStream::kGDeflateId || header->magic != (header->id ^ 0xff))
{
SetErrorMessage("Compressed data has an unrecognized header.");
return Status::InvalidData;
}
size_t const headerSize = GetDirectStorageStreamHeaderSize(header->numTiles);
if (compressedSize < headerSize)
{
SetErrorMessage("Compressed buffer is too small (%zu bytes).", compressedSize);
return Status::InvalidData;
}
size_t storedUncompressedSize = (header->numTiles - 1) * kDefaultTileSize;
if (header->lastTileSize != 0)
storedUncompressedSize += header->lastTileSize;
else
storedUncompressedSize += kDefaultTileSize;
if (storedUncompressedSize > uncompressedSize)
{
SetErrorMessage("Provided buffer (%zu bytes) is too small for the uncompressed data (%zu bytes).",
uncompressedSize, storedUncompressedSize);
return Status::InvalidArgument;
}
// Parse the input data into pages
Vector<libdeflate_gdeflate_in_page> pages(allocator);
pages.resize(header->numTiles);
uint32_t const* tileOffsets = reinterpret_cast<uint32_t const*>(
static_cast<uint8_t const*>(compressedData) + sizeof(TileStream));
for (uint32_t i = 0; i < header->numTiles; ++i)
{
uint32_t const compressedOffset = (i == 0) ? 0 : tileOffsets[i];
libdeflate_gdeflate_in_page& page = pages[i];
page.data = static_cast<uint8_t const*>(compressedData) + compressedOffset + headerSize;
if (i == header->numTiles - 1)
page.nbytes = tileOffsets[0];
else
page.nbytes = tileOffsets[i + 1] - compressedOffset;
}
libdeflate_gdeflate_decompressor* decompressor = libdeflate_alloc_gdeflate_decompressor();
if (!decompressor)
{
SetErrorMessage("Failed to allocate a GDeflate decompressor.");
return Status::InternalError;
}
// Decompress the data
size_t outputBytes = 0;
libdeflate_result result = libdeflate_gdeflate_decompress(decompressor, pages.data(), pages.size(),
uncompressedData, storedUncompressedSize, &outputBytes);
libdeflate_free_gdeflate_decompressor(decompressor);
if (result == LIBDEFLATE_BAD_DATA)
{
SetErrorMessage("GDeflate decompression failed: input data is corrupted or invalid.");
return Status::InvalidData;
}
else if (result != LIBDEFLATE_SUCCESS)
{
SetErrorMessage("GDeflate decompression failed with error code %d.", int(result));
return Status::InternalError;
}
if (outputBytes != storedUncompressedSize)
{
SetErrorMessage("GDeflate decompression output size mismatch: expected %zu bytes, got %zu bytes.",
storedUncompressedSize, outputBytes);
return Status::InternalError;
}
if (expectedCrc32 != 0)
{
uint32_t const actualCrc32 = libdeflate_crc32(0, uncompressedData, outputBytes);
if (actualCrc32 != expectedCrc32)
{
SetErrorMessage("GDeflate decompression CRC32 mismatch: expected 0x%08X, got 0x%08X.",
expectedCrc32, actualCrc32);
return Status::InvalidData;
}
}
ClearErrorMessage();
return Status::Ok;
}
// Public function declared in ntc.h
size_t GetGDeflateHeaderSize(size_t uncompressedSize)
{
size_t const numPages = (uncompressedSize + kDefaultTileSize - 1) / kDefaultTileSize;
return GetDirectStorageStreamHeaderSize(numPages);
}
Status DecompressGDeflateOnVulkanGPU(GraphicsResources const* resources, void* commandBuffer,
void const* pCompressedHeader, size_t compressedHeaderSize,
uint64_t compressedGpuVA, uint64_t decompressedGpuVA)
{
#if NTC_WITH_VULKAN
if (resources->GetGraphicsApi() != GraphicsAPI::Vulkan)
{
SetErrorMessage("DecompressGDeflateOnVulkanGPU requires a Vulkan device.");
return Status::Unsupported;
}
if (!resources->pfn_vkCmdDecompressMemoryNV)
{
SetErrorMessage("vkCmdDecompressMemoryNV function is not available.");
return Status::Unsupported;
}
REQUIRE_NOT_NULL(commandBuffer);
REQUIRE_NOT_NULL(pCompressedHeader);
REQUIRE_NOT_ZERO(compressedGpuVA);
REQUIRE_NOT_ZERO(decompressedGpuVA);
if (compressedHeaderSize < sizeof(TileStream))
{
SetErrorMessage("Compressed header is too small (%zu bytes).", compressedHeaderSize);
return Status::InvalidData;
}
TileStream const* header = static_cast<TileStream const*>(pCompressedHeader);
if (header->id != TileStream::kGDeflateId || header->magic != (header->id ^ 0xff))
{
SetErrorMessage("Compressed data has an unrecognized header.");
return Status::InvalidData;
}
uint32_t const numTiles = header->numTiles;
if (numTiles == 0)
{
// No tiles to decompress. Not sure if this should be an error or not...
ClearErrorMessage();
return Status::Ok;
}
size_t const headerSize = GetDirectStorageStreamHeaderSize(numTiles);
if (compressedHeaderSize < headerSize)
{
SetErrorMessage("Compressed data header is too small (%zu bytes).", compressedHeaderSize);
return Status::InvalidData;
}
Vector<VkDecompressMemoryRegionNV> regions(resources->GetAllocator());
VkDecompressMemoryRegionNV* pRegions = nullptr;
size_t const sizeOfRegions = numTiles * sizeof(VkDecompressMemoryRegionNV);
if (sizeOfRegions < 8192)
{
// If there are relatively few pages, allocate the array on the stack to avoid a dynamic allocation
pRegions = static_cast<VkDecompressMemoryRegionNV*>(alloca(sizeOfRegions));
}
else
{
regions.resize(numTiles);
pRegions = regions.data();
}
uint32_t const* tileOffsets = reinterpret_cast<uint32_t const*>(
static_cast<uint8_t const*>(pCompressedHeader) + sizeof(TileStream));
size_t const uncompressedSize = numTiles * kDefaultTileSize + header->lastTileSize;
uint32_t decompressedTileOffset = 0;
for (uint32_t i = 0; i < numTiles; ++i)
{
VkDecompressMemoryRegionNV& region = pRegions[i];
region.decompressionMethod = VK_MEMORY_DECOMPRESSION_METHOD_GDEFLATE_1_0_BIT_NV;
uint32_t compressedTileOffset = (i == 0) ? 0 : tileOffsets[i];
region.srcAddress = compressedGpuVA + compressedTileOffset;
region.dstAddress = decompressedGpuVA + decompressedTileOffset;
if (i == numTiles - 1)
{
region.compressedSize = tileOffsets[0];
region.decompressedSize = uncompressedSize - decompressedTileOffset;
}
else
{
region.compressedSize = tileOffsets[i + 1] - compressedTileOffset;
region.decompressedSize = kDefaultTileSize;
}
decompressedTileOffset += region.decompressedSize;
}
VkCommandBuffer vkCmdBuf = static_cast<VkCommandBuffer>(commandBuffer);
resources->pfn_vkCmdDecompressMemoryNV(vkCmdBuf, numTiles, pRegions);
ClearErrorMessage();
return Status::Ok;
#else // !NTC_WITH_VULKAN
SetErrorMessage("LibNTC was build without Vulkan support.");
return Status::NotImplemented;
#endif
}
} // namespace ntc