-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathJsonFileFormat.h
More file actions
227 lines (188 loc) · 5.7 KB
/
JsonFileFormat.h
File metadata and controls
227 lines (188 loc) · 5.7 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
/*
* SPDX-FileCopyrightText: Copyright (c) 2024 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.
*/
#pragma once
#include <cstdint>
#include <optional>
#include "StdTypes.h"
namespace ntc::json
{
// Conservative estimate for the JSON chunk, normally around 4-8 kB
static constexpr size_t JsonChunkSizeLimit = 16384;
// Header for the container, stored in binary form at the beginning of NTC files
struct FileHeader
{
static constexpr uint32_t SignatureValue = 0x5845544E; // "NTEX"
static constexpr uint32_t CurrentVersion = 0x100; // Version of the container, not the JSON schema
uint32_t signature = SignatureValue;
uint32_t version = CurrentVersion;
uint64_t jsonChunkOffset = 0;
uint64_t jsonChunkSize = 0;
uint64_t binaryChunkOffset = 0;
uint64_t binaryChunkSize = 0;
};
// The rest of the structures and enums below are representing the schema for the JSON chunk
// stored in the NTC files. When any changes to these structures are made, corresponsing changes
// need to be made to the schema metadata in JsonFileFormat.cpp.
// See docs/TextureSetFile.md in the NTC SDK for more information about the schema.
// The main object in the JSON chunk is represented by the Document struct below.
// Any fields not described in the schema will be silently skipped by the serializer and the parser.
struct BufferView
{
uint64_t offset = 0;
uint64_t storedSize = 0;
std::optional<CompressionType> compression;
std::optional<uint64_t> uncompressedSize;
std::optional<uint32_t> crc32;
BufferView() = default;
BufferView(IAllocator* allocator) { }
};
struct LatentShape
{
uint32_t numFeatures = 0;
LatentShape() = default;
LatentShape(IAllocator* allocator) { }
};
enum class MatrixLayout
{
RowMajor,
ColumnMajor
};
enum class ActivationType
{
HGELUClamp
};
enum class MlpDataType
{
Int8,
Int32,
FloatE4M3,
FloatE5M2,
Float16,
Float32
};
struct MLPLayer
{
uint32_t inputChannels = 0;
uint32_t outputChannels = 0;
uint32_t weightView = 0;
std::optional<uint32_t> scaleView;
uint32_t biasView = 0;
MlpDataType weightType = MlpDataType::Int8;
MlpDataType biasType = MlpDataType::Int8;
std::optional<MlpDataType> scaleType;
MLPLayer() = default;
MLPLayer(IAllocator* allocator) { }
};
struct MLP
{
Vector<MLPLayer> layers;
std::optional<ActivationType> activation;
std::optional<MatrixLayout> weightLayout;
MLP(IAllocator* allocator)
: layers(allocator)
{ }
};
struct BCModeBuffer
{
uint32_t mipLevel = 0;
uint32_t view = 0;
BCModeBuffer() = default;
BCModeBuffer(IAllocator* allocator) { }
};
struct Texture
{
String name;
uint32_t firstChannel = 0;
uint32_t numChannels = 0;
std::optional<ChannelFormat> channelFormat;
std::optional<ColorSpace> rgbColorSpace;
std::optional<ColorSpace> alphaColorSpace;
std::optional<BlockCompressedFormat> bcFormat;
Vector<BCModeBuffer> bcModeBuffers;
Texture(IAllocator* allocator)
: name(allocator)
, bcModeBuffers(allocator)
{ }
};
struct Channel
{
std::optional<ColorSpace> colorSpace;
Channel(IAllocator* allocator)
{ }
};
struct LatentImage
{
uint32_t width = 0;
uint32_t height = 0;
Vector<uint32_t> layerViews;
LatentImage(IAllocator* allocator)
: layerViews(allocator)
{ }
};
struct ColorImageData
{
uint32_t view = 0;
std::optional<ChannelFormat> uncompressedFormat;
std::optional<BlockCompressedFormat> bcFormat;
std::optional<uint32_t> rowPitch;
std::optional<uint32_t> pixelStride;
std::optional<uint32_t> numChannels;
ColorImageData(IAllocator* allocator)
{ }
};
struct ColorMip
{
std::optional<uint32_t> width;
std::optional<uint32_t> height;
std::optional<uint32_t> latentMip;
std::optional<float> positionLod;
std::optional<float> positionScale;
std::optional<ColorImageData> combinedColorData;
Vector<ColorImageData> perTextureColorData;
ColorMip(IAllocator* allocator)
: perTextureColorData(allocator)
{ }
};
struct Document
{
static constexpr uint32_t SchemaVersion = 3;
IAllocator* allocator;
uint32_t schemaVersion = SchemaVersion;
uint32_t width = 0;
uint32_t height = 0;
uint32_t numChannels = 0;
std::optional<uint32_t> numColorMips;
std::optional<LatentShape> latentShape;
Vector<MLP> mlpVersions;
Vector<Texture> textures;
Vector<Channel> channels;
Vector<LatentImage> latents;
Vector<ColorMip> colorMips;
Vector<BufferView> views;
Document(IAllocator* allocator)
: allocator(allocator)
, mlpVersions(allocator)
, textures(allocator)
, channels(allocator)
, latents(allocator)
, colorMips(allocator)
, views(allocator)
{ }
};
// Serializes the document into a JSON string.
// Returns true if successful, false on error.
bool SerializeDocument(Document const& document, String& outString, String& outErrorMessage);
// Parses a JSON string into a document with basic validation of the required fields.
// Returns true if successful, false on error.
// Warning: in-situ parsing is used, input will be corrupted!
bool ParseDocument(Document& outDocument, char* input, String& outErrorMessage);
}