Cogs.Core
CogsBin3.h
1#pragma once
2#include <glm/vec3.hpp>
3#include <glm/mat4x4.hpp>
4#include <glm/ext/quaternion_float.hpp>
5#include <cstdint>
6#include "Flags.h"
7
8namespace Cogs::Core
9{
10 namespace CogsBin3
11 {
12 // COGSBIN3 file format
13 // ====================
14 //
15 // File layout:
16 // - Header. Contains number of sections in file.
17 // - N instances of SectionInfo, where N is the number of sections in file (see header).
18 // - Section 0
19 // - ...
20 // - Section N-1
21 //
22 // Sections:
23 // Each section is described by its SectionInfo in the beginning of the file. The info
24 // contains the offset inside the file where the section is located, along with its size.
25 // Sections can be compressed or raw on a section-by-section basis. The info tells if the
26 // section is compressed, and what the uncompressed file size is.
27 //
28 // The idea is that if the cogsbin file is used as an archive, it should be simple to only
29 // pay the decompression cost for what is needed, and thus the format allows the free-form
30 // data of models to be placed in different sections.
31 //
32 // Subsections:
33 // Data that corresponds to given structs are stored in sub-sections. Items in the
34 // subsections contains reference into other subsections *within the same section*.
35 //
36 // Currently, the writer only puts subsection into section 0, but the file format doesn't
37 // require this. The reasoning is that the data in subsections is quite small compared to
38 // the free-form data, and compression is more efficient if similar data (e.g. strings)
39 // are grouped. Also, the writer does de-duplication, so shared materials etc. are shared
40 // inside the file as well.
41 //
42 // The 'models' subsection contain the entry point when extracting data, and contains the
43 // list of models contained in the file. Usually there is only one model in the file.
44 //
45 // Free-form data:
46 // Some data, like vertex data, indices, and animation value arrays are stored outside
47 // the subsections.
48 //
49 // **Free-form data offsets:** Data is referenced by an uin64_t, where the upper 16 bits is
50 // the subsection number and the lower 48 contains the offset within the subsection.
51 //
52
53 const uint32_t HeaderMagic = 0xC065C065;
54 const uint32_t Version = 3;
55 const size_t CogsSectionAlignment = 16;
56
57 struct Header
58 {
59 uint32_t magic;
60 uint32_t version;
61 uint64_t fileLength;
62 uint32_t sectionCount;
63 uint32_t unused[3];
64 };
65 static_assert(sizeof(Header) == 4 * 8);
66
67 enum struct Compression : uint32_t
68 {
69 None = 0,
70 ZSTD = 1
71 };
72
73 enum struct SectionType : uint16_t
74 {
75 META = 1<<0,
76 DATA = 1<<1
77 };
78 ENABLE_ENUM_FLAGS(SectionType);
79
81 {
82 uint64_t fileOffset; // Offset in file
83 uint64_t fileSize; // Number of bytes in file (i.e. compressed size)
84 uint64_t uncompressedSize; // Number of bytes when uncompressed.
85 Compression compression; // Compression method.
86 SectionType type;
87 uint16_t subSectionCount;
88 };
89 static_assert(sizeof(SectionInfo) == 4 * 8);
90
91 enum struct SubSectionType: uint32_t
92 {
93 Buffers,
94 Textures,
95 Strings,
96 StringData,
97 Nodes,
98 Transforms,
99 BoundingBoxes,
100 MaterialInstances,
101 Properties,
102 PropertyData,
103 Meshes,
104 VertexStreams,
105 VertexAttributes,
106 Bones,
107 Skeletons,
108 AnimTracks,
109 AnimClips,
110 Models,
111 EnumSize
112 };
113
115 {
116 uint64_t offset;
117 uint32_t size;
118 SubSectionType type;
119 };
120// static_assert(sizeof(SubSection)==
121
122 // enum values matches DXGI_FORMAT
123 enum struct Format : uint16_t {
124 Unknown,
125 R8_UNORM,
126 R8G8_UNORM,
127 R8G8B8_UNORM,
128 R8G8B8A8_UNORM,
129 R16_UNORM,
130 R16G16_UNORM,
131 R16G16B16_UNORM,
132 R16G16B16A16_UNORM,
133 R8_SNORM,
134 R8G8_SNORM,
135 R8G8B8_SNORM,
136 R8G8B8A8_SNORM,
137 R16_SNORM,
138 R16G16_SNORM,
139 R16G16B16_SNORM,
140 R16G16B16A16_SNORM,
141 R8_UINT,
142 R8G8_UINT,
143 R8G8B8_UINT,
144 R8G8B8A8_UINT,
145 R16_UINT,
146 R16G16_UINT,
147 R16G16B16_UINT,
148 R16G16B16A16_UINT,
149 R32_UINT,
150 R32G32_UINT,
151 R32G32B32_UINT,
152 R32G32B32A32_UINT,
153 R8_SINT,
154 R8G8_SINT,
155 R8G8B8_SINT,
156 R8G8B8A8_SINT,
157 R16_SINT,
158 R16G16_SINT,
159 R16G16B16_SINT,
160 R16G16B16A16_SINT,
161 R32_SINT,
162 R32G32_SINT,
163 R32G32B32_SINT,
164 R32G32B32A32_SINT,
165 R16_FLOAT,
166 R16G16_FLOAT,
167 R16G16B16_FLOAT,
168 R16G16B16A16_FLOAT,
169 R32_FLOAT,
170 R32G32_FLOAT,
171 R32G32B32_FLOAT,
172 R32G32B32A32_FLOAT,
173 D16_UNORM,
174 D24_UNORM,
175 D24S8_UNORM,
176 D32_FLOAT,
177 R32_TYPELESS,
178 R16_TYPELESS,
179 R8T,
180 R8G8T,
181 R8G8B8T,
182 R8G8B8A8T,
183 B8G8R8,
184 B8G8R8A8,
185 A8_UNORM,
186 BC1_TYPELESS,
187 BC1_UNORM,
188 BC1_UNORM_SRGB,
189 BC2_TYPELESS,
190 BC2_UNORM,
191 BC2_UNORM_SRGB,
192 BC3_TYPELESS,
193 BC3_UNORM,
194 BC3_UNORM_SRGB,
195 BC4_TYPELESS,
196 BC4_UNORM,
197 BC4_SNORM,
198 BC5_TYPELESS,
199 BC5_UNORM,
200 BC5_SNORM,
201 R8G8B8A8_UNORM_SRGB,
202 R64_FLOAT,
203 R64G64_FLOAT,
204 R64G64B64_FLOAT,
205 R64G65B64A64_FLOAT,
206 EnumSize
207 };
208
209 enum struct MeshFlags : uint8_t
210 {
211 None = 0,
212 Clockwise = 1<<0,
213 Skinned = 1<<1
214 };
215 ENABLE_ENUM_FLAGS(MeshFlags);
216
217 enum struct PrimitiveType : uint8_t
218 {
219 Unknown = 255,
220 TriangleList = 0,
221 TriangleStrip = 1,
222 LineList = 2,
223 LineStrip = 3,
224 PointList = 4,
225 TriangleListAdjacency = 5,
226 TriangleStripAdjacency = 6,
227 LineListAdjacency = 7,
228 LineStripAdjacency = 8,
229 ControlPoint1PatchList = 9,
230 ControlPoint2PatchList = 10,
231 ControlPoint3PatchList = 11,
232 ControlPoint4PatchList = 12,
233 EnumSize
234 };
235
236 enum struct Semantic : uint8_t
237 {
238 Position = 0,
239 Normal = 1,
240 Color = 2,
241 TexCoord = 3,
242 Tangent = 4,
243 InstanceVector = 5,
244 InstanceMatrix = 6,
245 EnumSize
246 };
247
248 enum struct InputType : uint8_t
249 {
250 PerVertex,
251 PerInstance,
252 EnumSize
253 };
254
255 // Reference to string in string buffer, non-zero terminated.
256 struct String
257 {
258 uint32_t offset;
259 uint32_t length;
260 };
261
263 {
264 float min[3];
265 float max[3];
266 };
267
268 enum struct BufferContentFlags : uint32_t
269 {
270 None = 0,
271 VertexData = 1<<0,
272 IndexData = 1<<1,
273 TextureData = 1<<2,
274 AnimationData = 1<<3,
275 ProcessedData = 1<<4
276 };
277 ENABLE_ENUM_FLAGS(BufferContentFlags);
278
280 struct Buffer
281 {
283 uint32_t size;
284 BufferContentFlags flags;
285 };
286 static_assert(sizeof(Buffer) == 2 * 8);
287
289 {
290 uint16_t offset; // uint16_t Offset from element start
291 Format format; // uint16_t
292 Semantic semantic; // uint8_t
293 uint8_t semanticIndex; // uint8_t
294 uint16_t instanceStepRate; // 0 - vertex input, !0 -> instance stepping = instanceStepRate-1
295 };
296 static_assert(sizeof(VertexAttribute) == 2 * 4);
297
299 {
300 uint32_t buffer;
301 uint32_t offset;
302 uint32_t count;
303 uint32_t firstAttribute;
304 uint32_t attributeCount;
305 uint16_t stride;
306 uint8_t vertexDataType;
307 uint8_t encoding;
308 };
309 static_assert(sizeof(VertexStream) == 3 * 8);
310
311 struct Mesh
312 {
313 uint32_t name;
314 uint32_t boundingBox;
315 uint32_t firstStream;
316 uint8_t streamCount;
317 MeshFlags flags;
318 PrimitiveType primitiveType;
319 };
320 static_assert(sizeof(Mesh) == 4 * 4);
321
322 // Contains one LOD-level of texture data.
323 // - data size deducable from width,height/depth/layers/format
324 struct Level
325 {
326 uint32_t bufferView;
327 uint32_t offset;
328 uint32_t rowStride;
329 uint32_t depthStride;
330 };
331 static_assert(sizeof(Level) == 4 * 4);
332
333 // Later, add support for PNG prediction filters so we compress as well as PNG.
334 enum struct TextureEncoding : uint8_t
335 {
336 None,
337 EnumSize
338 };
339
340 enum struct TextureFlags : uint8_t
341 {
342 None,
343 EnumSize
344 };
345
346 struct Texture
347 {
348 uint32_t buffer;
349 uint32_t offset;
350 uint32_t size;
351 uint32_t name;
352 uint32_t width;
353 uint32_t height;
354 uint32_t depth;
355 uint32_t faces;
356 uint32_t layers;
357 Format format;
358 TextureEncoding encoding;
359 TextureFlags flags;
360 };
361 static_assert(sizeof(Texture) == 10 * 4);
362
363
364 enum struct PropertyKeyType : uint16_t
365 {
366 String,
367 Index,
368 EnumSize
369 };
370
371 enum struct PropertyValueType : uint16_t
372 {
373 Bool,
374 UInt,
375 Float,
376 Float2,
377 Float3,
378 Float4,
379 String,
380 Variant,
381 EmbeddedTexture,
382 EnumSize
383 };
384
385 struct Property
386 {
387 PropertyKeyType keyType;
388 PropertyValueType valueType;
389 uint32_t key;
390 uint32_t value;
391 };
392 static_assert(alignof(Property) == 4);
393
395 {
396 uint32_t material;
397 uint32_t permutation;
398 uint32_t propertyFirst;
399 uint32_t propertyCount;
400 };
401
403 {
404 float m[12];
405
406 size_t hash(size_t hashValue = Cogs::hash()) const {
407 for (float value : m) {
408 hashValue = Cogs::hash(value, hashValue);
409 }
410 return hashValue;
411 }
412 };
413 static_assert(sizeof(Transform) == 6 * 8);
414
415
416 enum struct BoneFlags : uint16_t
417 {
418 None = 0,
419 HasOffset = 1<<0,
420 Used = 1<<1,
421 Animated = 1<<2
422 };
423 ENABLE_ENUM_FLAGS(BoneFlags);
424
425 struct Bone
426 {
427 glm::mat4 inverseBindPose;
428 glm::mat4 relative;
429 glm::quat rot;
430 glm::vec3 pos;
431 glm::vec3 scale;
432 uint32_t name;
433 uint16_t parentBone;
434 BoneFlags flags;
435 };
436 static_assert(sizeof(Bone) == 44 * 4);
437
438 struct Skeleton
439 {
440 glm::mat4 bindPose;
441 uint32_t firstBone;
442 uint32_t boneCount;
443 };
444
446 {
447 uint32_t boneIndex;
448 uint32_t buffer;
449 uint32_t translationOffset;
450 uint32_t translationCount;
451 uint32_t rotationOffset;
452 uint32_t rotationCount;
453 uint32_t scaleOffset;
454 uint32_t scaleCount;
455 };
456 static_assert(sizeof(AnimTrack) == 8 * 4);
457
458 struct AnimClip
459 {
460 uint32_t name;
461 float duration;
462 float resolution;
463 uint32_t trackFirst;
464 uint32_t trackCount;
465 };
466
467 struct Node
468 {
469 uint32_t parent;
470 uint32_t name;
471 uint32_t mesh;
473 uint32_t boundingBox;
474 uint32_t transform;
475 PrimitiveType primitiveType;
476 uint32_t vertexFirst;
477 uint32_t vertexCount;
478 };
479
480 struct Model
481 {
482 uint32_t skeleton;
483 uint32_t animClipFirst;
484 uint32_t animClipCount;
485 uint32_t firstNode;
486 uint32_t nodeCount;
487 };
488
489 }
490}
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
constexpr size_t hash() noexcept
Simple getter function that returns the initial value for fnv1a hashing.
Definition: HashFunctions.h:62
uint32_t name
Offset into strings subsection of current section.
Definition: CogsBin3.h:460
uint32_t boneIndex
Which bone to animate.
Definition: CogsBin3.h:447
uint32_t buffer
Offset into bones array.
Definition: CogsBin3.h:448
uint16_t parentBone
Offset of within range of bones of this skeleton.
Definition: CogsBin3.h:433
uint32_t name
Offset into strings subsection of current section.
Definition: CogsBin3.h:432
Generic blob of data.
Definition: CogsBin3.h:281
uint32_t size
Size of buffer, in bytes.
Definition: CogsBin3.h:283
BufferContentFlags flags
Specifies what kind of data present in the buffer.
Definition: CogsBin3.h:284
uint64_t sectionOffset
Raw section pointer: Upper 16 bits is section id, lower 48 is offset inside that section.
Definition: CogsBin3.h:282
uint32_t bufferView
Index of buffer view with data.
Definition: CogsBin3.h:326
uint32_t propertyFirst
Offset into Properties subsection of current section.
Definition: CogsBin3.h:398
uint32_t permutation
String with permuation name. Offset into strings subsection of current section.
Definition: CogsBin3.h:397
uint32_t propertyCount
Number of properties set in material instance.
Definition: CogsBin3.h:399
uint32_t material
Name of material. Offset into strings subsection of current section.
Definition: CogsBin3.h:396
uint32_t boundingBox
Index of bounding box.
Definition: CogsBin3.h:314
uint32_t firstStream
Index of first stream of mesh.
Definition: CogsBin3.h:315
uint32_t name
Index of string with name.
Definition: CogsBin3.h:313
uint32_t skeleton
Offset into the Skeleton subsection of current section.
Definition: CogsBin3.h:482
uint32_t nodeCount
Number of nodes in model.
Definition: CogsBin3.h:486
uint32_t firstNode
Offset of first node into Nodes subsection of current section.
Definition: CogsBin3.h:485
uint32_t mesh
Mesh of node, offset into Meshes subsection.
Definition: CogsBin3.h:471
uint32_t name
Name of node, offset into Strings subsection.
Definition: CogsBin3.h:470
uint32_t transform
Transform of node, offset into Transforms subsection.
Definition: CogsBin3.h:474
PrimitiveType primitiveType
Primitive type to use for this node.
Definition: CogsBin3.h:475
uint32_t materialInstance
Material instance of node, offset into MaterialInstances subsection.
Definition: CogsBin3.h:472
uint32_t vertexCount
Number of vertices to draw.
Definition: CogsBin3.h:477
uint32_t parent
Parent node, offset into Nodes subsection.
Definition: CogsBin3.h:469
uint32_t boundingBox
Bounding box of node, offset into BoundingBoxes subsection.
Definition: CogsBin3.h:473
uint32_t vertexFirst
First vertex of mesh to draw.
Definition: CogsBin3.h:476
uint32_t boneCount
Number of bones in skeleton.
Definition: CogsBin3.h:442
uint32_t firstBone
Offset into Bones subsection of current section.
Definition: CogsBin3.h:441
uint32_t buffer
Buffer that holds image data, index into buffers.
Definition: CogsBin3.h:348
uint32_t offset
Byte offset within buffer for start of imagte data.
Definition: CogsBin3.h:349
uint32_t size
Byte size of image data.
Definition: CogsBin3.h:350
Defines mesh flags controlling the behavior of Mesh resources.
Definition: Mesh.h:52
Primitive types for interpreting vertex data sent to the graphics pipeline.
Definition: Common.h:111
Texture flags describing valid usage for a texture object.
Definition: Flags.h:111