Skip to content

Commit 3130598

Browse files
author
devsh
committed
flesh out the format.hlsl a bit more
1 parent 4e4cf89 commit 3130598

File tree

5 files changed

+162
-91
lines changed

5 files changed

+162
-91
lines changed

include/nbl/builtin/hlsl/format.hlsl

Lines changed: 132 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "nbl/builtin/hlsl/format/shared_exp.hlsl"
55
#include "nbl/builtin/hlsl/format/octahedral.hlsl"
6+
#include "nbl/builtin/hlsl/type_traits.hlsl"
67

78
namespace nbl
89
{
@@ -223,6 +224,7 @@ enum TexelBlockFormat : uint32_t
223224
TBF_COUNT = TBF_UNKNOWN
224225
};
225226

227+
// TODO: apparently depth/stencil texel blocks are not compatible with anything else, so need to add
226228
enum BlockViewClass : uint32_t
227229
{
228230
SIZE_8_BIT,
@@ -281,24 +283,26 @@ struct view_class_traits
281283
uint32_t Alignment;
282284
};
283285
#define SPECIALIZE_CLASS(CLASS,RAWFMT,SIZE,ALIGNMENT) template<> \
284-
struct view_class_traits<CLASS> \
286+
struct view_class_traits<BlockViewClass::CLASS> \
285287
{ \
286-
NBL_CONSTEXPR_STATIC_INLINE BlockViewClass Class = CLASS; \
287-
NBL_CONSTEXPR_STATIC_INLINE TexelBlockFormat RawAccessViewFormat = RAWFMT; \
288+
NBL_CONSTEXPR_STATIC_INLINE BlockViewClass Class = BlockViewClass::CLASS; \
289+
NBL_CONSTEXPR_STATIC_INLINE TexelBlockFormat RawAccessViewFormat = TexelBlockFormat::RAWFMT; \
288290
NBL_CONSTEXPR_STATIC_INLINE uint32_t ByteSize = SIZE; \
289291
NBL_CONSTEXPR_STATIC_INLINE uint32_t Alignment = ALIGNMENT; \
290292
}
291-
SPECIALIZE_CLASS(BlockViewClass::SIZE_8_BIT,TexelBlockFormat::R8_UINT,1,1);
292-
SPECIALIZE_CLASS(BlockViewClass::SIZE_16_BIT,TexelBlockFormat::R16_UINT,2,2);
293-
SPECIALIZE_CLASS(BlockViewClass::SIZE_32_BIT,TexelBlockFormat::R32_UINT,4,4);
294-
SPECIALIZE_CLASS(BlockViewClass::SIZE_64_BIT,TexelBlockFormat::R32G32_UINT,8,4);
295-
SPECIALIZE_CLASS(BlockViewClass::SIZE_96_BIT,TexelBlockFormat::R32G32B32_UINT,12,4);
296-
SPECIALIZE_CLASS(BlockViewClass::SIZE_128_BIT,TexelBlockFormat::R32G32B32A32_UINT,16,4);
297-
SPECIALIZE_CLASS(BlockViewClass::SIZE_192_BIT,TexelBlockFormat::R64G64B64_UINT,24,8);
298-
SPECIALIZE_CLASS(BlockViewClass::SIZE_256_BIT,TexelBlockFormat::R64G64B64A64_UINT,32,8);
299-
// TODO: block compressed
293+
SPECIALIZE_CLASS(SIZE_8_BIT,R8_UINT,1,1);
294+
SPECIALIZE_CLASS(SIZE_16_BIT,R16_UINT,2,2);
295+
SPECIALIZE_CLASS(SIZE_32_BIT,R32_UINT,4,4);
296+
SPECIALIZE_CLASS(SIZE_64_BIT,R32G32_UINT,8,4);
297+
SPECIALIZE_CLASS(SIZE_96_BIT,R32G32B32_UINT,12,4);
298+
SPECIALIZE_CLASS(SIZE_128_BIT,R32G32B32A32_UINT,16,4);
299+
SPECIALIZE_CLASS(SIZE_192_BIT,R64G64B64_UINT,24,8);
300+
SPECIALIZE_CLASS(SIZE_256_BIT,R64G64B64A64_UINT,32,8);
301+
// TODO: depth and block compressed
300302
#undef SPECIALIZE_CLASS
301303

304+
305+
//! Block Format Traits
302306
enum TexelKind
303307
{
304308
Float = 0,
@@ -310,11 +314,12 @@ enum TexelKind
310314

311315
enum TexelAttributes : uint32_t
312316
{
313-
HasDepthBit,
314-
HasStencilBit,
315-
BGRABit,
316-
SignedBit,
317-
CompressedBit
317+
None = 0b00000u,
318+
HasDepthBit = 0b00001u,
319+
HasStencilBit = 0b00010u,
320+
BGRABit = 0b00100u,
321+
SignedBit = 0b01000u,
322+
CompressedBit = 0b10000u
318323
};
319324

320325
// default is invalid/runtime dynamic
@@ -324,7 +329,7 @@ struct block_traits
324329
view_class_traits<> ClassTraits;
325330
// float int uint
326331
#ifndef __HLSL_VERSION
327-
std::type_info* DecodeTypeID;
332+
const std::type_info* DecodeTypeID;
328333
#else
329334
uint32_t DecodeTypeID;
330335
#endif
@@ -335,46 +340,62 @@ struct block_traits
335340
uint32_t BlockSizeY;
336341
uint32_t BlockSizeZ;
337342
uint32_t Channels;
338-
// bits per pixel
339-
// TODO: rational in HLSL
340-
uint32_t BPPNum;
341-
uint32_t BPPDen;
342343
};
343344

344-
// TODO: turn into a macro so we can fast-define this
345-
template<>
346-
struct block_traits<TexelBlockFormat::B8G8R8A8_SRGB>
347-
{
348-
NBL_CONSTEXPR_STATIC_INLINE BlockViewClass Class = BlockViewClass::SIZE_32_BIT;
349-
using class_traits_t = view_class_traits<Class>;
350345
#ifndef __HLSL_VERSION
351-
NBL_CONSTEXPR_STATIC_INLINE std::type_info* DecodeTypeID = &typeid(float);
346+
#define SPECIALIZE_FORMAT(FORMAT,CLASS,KIND,ATTRS,W,H,D,C) template<> \
347+
struct block_traits<TexelBlockFormat::FORMAT> \
348+
{ \
349+
using class_traits_t = view_class_traits<BlockViewClass::CLASS>; \
350+
NBL_CONSTEXPR_STATIC_INLINE class_traits_t ClassTraits; \
351+
NBL_CONSTEXPR_STATIC_INLINE const std::type_info* DecodeTypeID = &typeid(float); \
352+
NBL_CONSTEXPR_STATIC_INLINE TexelKind Kind = KIND; \
353+
NBL_CONSTEXPR_STATIC_INLINE TexelAttributes Attributes = ATTRS; \
354+
NBL_CONSTEXPR_STATIC_INLINE uint32_t BlockSizeX = W; \
355+
NBL_CONSTEXPR_STATIC_INLINE uint32_t BlockSizeY = H; \
356+
NBL_CONSTEXPR_STATIC_INLINE uint32_t BlockSizeZ = D; \
357+
NBL_CONSTEXPR_STATIC_INLINE uint32_t Channels = C; \
358+
}
352359
#else
353-
NBL_CONSTEXPR_STATIC_INLINE uint32_t DecodeTypeID = impl::typeid_t<float>::value;
360+
#define SPECIALIZE_FORMAT(FORMAT,CLASS,KIND,ATTRS,W,H,D,C) template<> \
361+
struct block_traits<TexelBlockFormat::FORMAT> \
362+
{ \
363+
using class_traits_t = view_class_traits<BlockViewClass::CLASS>; \
364+
NBL_CONSTEXPR_STATIC_INLINE class_traits_t ClassTraits; \
365+
NBL_CONSTEXPR_STATIC_INLINE uint32_t DecodeTypeID = impl::typeid_t<float>::value; \
366+
NBL_CONSTEXPR_STATIC_INLINE TexelKind Kind = KIND; \
367+
NBL_CONSTEXPR_STATIC_INLINE TexelAttributes Attributes = ATTRS; \
368+
NBL_CONSTEXPR_STATIC_INLINE uint32_t BlockSizeX = W; \
369+
NBL_CONSTEXPR_STATIC_INLINE uint32_t BlockSizeY = H; \
370+
NBL_CONSTEXPR_STATIC_INLINE uint32_t BlockSizeZ = D; \
371+
NBL_CONSTEXPR_STATIC_INLINE uint32_t Channels = C; \
372+
}
354373
#endif
355-
NBL_CONSTEXPR_STATIC_INLINE uint32_t BlockSizeX = 1;
356-
NBL_CONSTEXPR_STATIC_INLINE uint32_t BlockSizeY = 1;
357-
NBL_CONSTEXPR_STATIC_INLINE uint32_t BlockSizeZ = 1;
358-
NBL_CONSTEXPR_STATIC_INLINE uint32_t Channels = 4;
374+
//SPECIALIZE_FORMAT(D16_UNORM,BVC_UNKNOWN,Normalized,HasDepthBit,1,1,1,1);
375+
// TODO: now do the rest
376+
SPECIALIZE_FORMAT(R4G4_UNORM_PACK8,SIZE_8_BIT,Normalized,None,1,1,1,2);
377+
// TODO: now do the rest
378+
SPECIALIZE_FORMAT(B8G8R8A8_SRGB,SIZE_32_BIT,SRGB,None,1,1,1,4);
379+
// TODO: now do the rest
380+
#undef SPECIALIZE_FORMAT
359381

360-
NBL_CONSTEXPR_STATIC_INLINE uint32_t BPPNum = 32;
361-
NBL_CONSTEXPR_STATIC_INLINE uint32_t BPPDen = 1;
362-
};
382+
// TODO: add a `rational<T,Num,Den>` to `mpl` namespace
383+
//template<TexelBlockFormat Format>
384+
//using bits_per_pixel = mpl::rational<uint32_t,(typename block_traits<Format>::class_traits_t)::Size,block_traits<Format>::W*block_traits<Format>::H*block_traits<Format>::D>;
363385

364-
/*
365386
template<TexelBlockFormat Format>
366387
struct texel_block
367388
{
368-
unsigned_of_size<traits<Format>::alignment> storage[traits<Format>::ByteSize/traits<Format>::Alignment];
389+
using class_traits_t = block_traits<Format>::class_traits_t;
390+
391+
unsigned_integer_of_size_t<class_traits_t::alignment> storage[class_traits_t::ByteSize/class_traits_t::Alignment];
369392
};
370-
*/
371393
}
372394

373395
// conversion ops for constant to runtime traits
374396
namespace impl
375397
{
376398

377-
378399
template<format::BlockViewClass ConstexprT>
379400
struct _static_cast_helper<format::view_class_traits<format::BlockViewClass::BVC_UNKNOWN>,format::view_class_traits<ConstexprT> >
380401
{
@@ -392,7 +413,76 @@ struct _static_cast_helper<format::view_class_traits<format::BlockViewClass::BVC
392413
}
393414
};
394415

395-
// TODO: specialization for the BlockTraits
416+
template<format::TexelBlockFormat ConstexprT>
417+
struct _static_cast_helper<format::block_traits<format::TexelBlockFormat::TBF_UNKNOWN>,format::block_traits<ConstexprT> >
418+
{
419+
using T = format::block_traits<format::TexelBlockFormat::TBF_UNKNOWN>;
420+
using U = format::block_traits<ConstexprT>;
421+
422+
T operator()(U val)
423+
{
424+
T retval;
425+
retval.ClassTraits = _static_cast<format::view_class_traits<>,U::class_traits_t>();
426+
retval.DecodeTypeID = U::DecodeTypeID;
427+
retval.Kind = U::Kind;
428+
retval.Attributes = U::Attributes;
429+
retval.BlockSizeX = U::BlockSizeX;
430+
retval.BlockSizeY = U::BlockSizeY;
431+
retval.BlockSizeZ = U::BlockSizeZ;
432+
retval.Channels = U::Channels;
433+
return retval;
434+
}
435+
};
436+
437+
}
438+
439+
// back to format namespace
440+
namespace format
441+
{
442+
443+
view_class_traits<> getTraits(const BlockViewClass _class)
444+
{
445+
using dynamic_t = view_class_traits<>;
446+
dynamic_t retval;
447+
switch (_class)
448+
{
449+
#define CASE(CLASS) case BlockViewClass::CLASS: \
450+
{ \
451+
const view_class_traits<BlockViewClass::CLASS> tmp; \
452+
retval = _static_cast<dynamic_t>(tmp); \
453+
break; \
454+
}
455+
CASE(SIZE_8_BIT)
456+
CASE(SIZE_16_BIT)
457+
CASE(SIZE_32_BIT)
458+
CASE(SIZE_64_BIT)
459+
CASE(SIZE_96_BIT)
460+
CASE(SIZE_128_BIT)
461+
CASE(SIZE_192_BIT)
462+
CASE(SIZE_256_BIT)
463+
// TODO: depth and block compressed
464+
#undef CASE
465+
default:
466+
break;
467+
}
468+
return retval;
469+
}
470+
471+
// TODO: block_traits<> getTraits(const TexelBlockFormatTraits format)
472+
473+
// TODO: move `core::rational<T>` to HLSL
474+
/*
475+
rational<uint32_t> getBitsPerPixel(const TexelBlockFormat format)
476+
{
477+
rational<uint32_t> retval;
478+
{
479+
block_traits<> traits = getTraits(format);
480+
retval.num = classTraits.ClassTraits.ByteSize;
481+
retval.den = classTraits.W*classTraits.H*classTraits.D;
482+
}
483+
return retval;
484+
}
485+
*/
396486

397487
}
398488
}

include/nbl/builtin/hlsl/type_traits.hlsl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -677,24 +677,23 @@ struct unsigned_integer_of_size
677677
{
678678
using type = void;
679679
};
680-
681680
template<>
682681
struct unsigned_integer_of_size<2>
683682
{
684683
using type = uint16_t;
685684
};
686-
687685
template<>
688686
struct unsigned_integer_of_size<4>
689687
{
690688
using type = uint32_t;
691689
};
692-
693690
template<>
694691
struct unsigned_integer_of_size<8>
695692
{
696693
using type = uint64_t;
697694
};
695+
template<uint16_t bytesize>
696+
using unsigned_integer_of_size_t = unsigned_integer_of_size<bytesize>::type;
698697

699698
}
700699
}

include/nbl/video/utilities/CComputeBlit.h

Lines changed: 26 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
#define _NBL_VIDEO_C_COMPUTE_BLIT_H_INCLUDED_
33

44
#include "nbl/asset/filters/CBlitUtilities.h"
5-
#include "nbl/builtin/hlsl/cpp_compat.hlsl"
6-
#include "nbl/builtin/hlsl/cpp_compat/vector.hlsl"
5+
#include "nbl/builtin/hlsl/format.hlsl"
76
#include "nbl/builtin/hlsl/blit/parameters.hlsl"
87

98
namespace nbl::video
@@ -12,27 +11,6 @@ namespace nbl::video
1211
class NBL_API2 CComputeBlit : public core::IReferenceCounted
1312
{
1413
public:
15-
// manual encode type: TODO replace with `hlsl::format` utils when they're ready
16-
static inline asset::E_FORMAT getCompatClassFormat(const asset::E_FORMAT format)
17-
{
18-
const asset::E_FORMAT_CLASS formatClass = asset::getFormatClass(format);
19-
switch (formatClass)
20-
{
21-
case asset::EFC_8_BIT:
22-
return asset::EF_R8_UINT;
23-
case asset::EFC_16_BIT:
24-
return asset::EF_R16_UINT;
25-
case asset::EFC_32_BIT:
26-
return asset::EF_R32_UINT;
27-
case asset::EFC_64_BIT:
28-
return asset::EF_R32G32_UINT;
29-
case asset::EFC_128_BIT:
30-
return asset::EF_R32G32B32A32_UINT;
31-
default:
32-
return asset::EF_UNKNOWN;
33-
}
34-
}
35-
3614
// Coverage adjustment needs alpha to be stored in HDR with high precision
3715
static inline asset::E_FORMAT getCoverageAdjustmentIntermediateFormat(const asset::E_FORMAT format)
3816
{
@@ -63,7 +41,31 @@ class NBL_API2 CComputeBlit : public core::IReferenceCounted
6341
}
6442

6543
// ctor
66-
CComputeBlit(core::smart_refctd_ptr<ILogicalDevice>&& logicalDevice) : m_device(std::move(logicalDevice)) {}
44+
inline CComputeBlit(core::smart_refctd_ptr<ILogicalDevice>&& logicalDevice) : m_device(std::move(logicalDevice)) {}
45+
46+
//! Returns the original format if supports STORAGE_IMAGE otherwise returns a format in its compat class which supports STORAGE_IMAGE.
47+
inline asset::E_FORMAT getOutputViewFormat(const asset::E_FORMAT format)
48+
{
49+
const auto& usages = m_device->getPhysicalDevice()->getImageFormatUsagesOptimalTiling();
50+
const auto& formatUsages = usages[format];
51+
52+
if (formatUsages.storageImage)
53+
{
54+
return format;
55+
}
56+
else
57+
{
58+
const auto compatibleSizes = asset::getFormatClass(format);
59+
const auto classNewEnum = static_cast<hlsl::format::BlockViewClass>(compatibleSizes);
60+
const auto compatFormatNewEnum = hlsl::format::getTraits(classNewEnum).RawAccessViewFormat;
61+
const auto compatFormat = static_cast<asset::E_FORMAT>(compatFormatNewEnum);
62+
const auto& compatClassFormatUsages = usages[compatFormat];
63+
if (!compatClassFormatUsages.storageImage)
64+
return asset::EF_UNKNOWN;
65+
else
66+
return compatFormat;
67+
}
68+
}
6769
#if 0
6870
// @param `alphaBinCount` is only required to size the histogram present in the default nbl_glsl_blit_AlphaStatistics_t in default_compute_common.comp
6971
core::smart_refctd_ptr<video::IGPUShader> createAlphaTestSpecializedShader(const asset::IImage::E_TYPE inImageType, const uint32_t alphaBinCount = asset::IBlitUtilities::DefaultAlphaBinCount);
@@ -637,27 +639,6 @@ class NBL_API2 CComputeBlit : public core::IReferenceCounted
637639
dispatchHelper(cmdbuf, normalizationPipeline->getLayout(), pushConstants, dispatchInfo);
638640
}
639641
}
640-
641-
//! Returns the original format if supports STORAGE_IMAGE otherwise returns a format in its compat class which supports STORAGE_IMAGE.
642-
inline asset::E_FORMAT getOutImageViewFormat(const asset::E_FORMAT format)
643-
{
644-
const auto& formatUsages = m_device->getPhysicalDevice()->getImageFormatUsagesOptimalTiling()[format];
645-
646-
if (formatUsages.storageImage)
647-
{
648-
return format;
649-
}
650-
else
651-
{
652-
const asset::E_FORMAT compatFormat = getCompatClassFormat(format);
653-
654-
const auto& compatClassFormatUsages = m_device->getPhysicalDevice()->getImageFormatUsagesOptimalTiling()[compatFormat];
655-
if (!compatClassFormatUsages.storageImage)
656-
return asset::EF_UNKNOWN;
657-
else
658-
return compatFormat;
659-
}
660-
}
661642
#endif
662643

663644

src/nbl/builtin/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/complex.hlsl")
275275
# format
276276
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/format/octahedral.hlsl")
277277
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/format/shared_exp.hlsl")
278+
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/format.hlsl")
278279
#linear algebra
279280
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/math/linalg/fast_affine.hlsl")
280281
# TODO: rename `equations` to `polynomials` probably

0 commit comments

Comments
 (0)