yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
f65d756bf
master
1// cpu-texture.h 2#pragma once 3#include "cpu-base.h" 4 5namespace gfx 6{ 7using namespace Slang ; 8 9namespace cpu 10{ 11 12struct CPUTextureBaseShapeInfo 13{ 14int32_t rank ; 15int32_t baseCoordCount ; 16int32_t implicitArrayElementCount ; 17}; 18 19static const CPUTextureBaseShapeInfo 20kCPUTextureBaseShapeInfos [(int )ITextureResource ::Type ::_Count ]= { 21/* Unknown */ {0 ,0 ,0 }, 22/* Buffer */ {1 ,1 ,1 }, 23/* Texture1D */ {1 ,1 ,1 }, 24/* Texture2D */ {2 ,2 ,1 }, 25/* Texture3D */ {3 ,3 ,1 }, 26/* TextureCube */ {2 ,3 ,6 }, 27}; 28 29static CPUTextureBaseShapeInfo const * _getBaseShapeInfo (ITextureResource ::Type baseShape ); 30 31typedef void (* CPUTextureUnpackFunc )(void const * texelData ,void * outData ,size_t outSize ); 32 33struct CPUTextureFormatInfo 34{ 35CPUTextureUnpackFunc unpackFunc ; 36}; 37 38template < int N > 39void _unpackFloatTexel (void const * texelData ,void * outData ,size_t outSize ); 40 41template < int N > 42void _unpackFloat16Texel (void const * texelData ,void * outData ,size_t outSize ); 43 44static inline float _unpackUnorm8Value (uint8_t value ); 45 46template < int N > 47void _unpackUnorm8Texel (void const * texelData ,void * outData ,size_t outSize ); 48 49void _unpackUnormBGRA8Texel (void const * texelData ,void * outData ,size_t outSize ); 50 51template < int N > 52void _unpackUInt16Texel (void const * texelData ,void * outData ,size_t outSize ); 53 54template < int N > 55void _unpackUInt32Texel (void const * texelData ,void * outData ,size_t outSize ); 56 57struct CPUFormatInfoMap 58{ 59CPUFormatInfoMap () 60 { 61memset (m_infos ,0 ,sizeof (m_infos )); 62 63set (Format ::R32G32B32A32_FLOAT ,& _unpackFloatTexel < 4 > ); 64set (Format ::R32G32B32_FLOAT ,& _unpackFloatTexel < 3 > ); 65 66set (Format ::R32G32_FLOAT ,& _unpackFloatTexel < 2 > ); 67set (Format ::R32_FLOAT ,& _unpackFloatTexel < 1 > ); 68 69set (Format ::R16G16B16A16_FLOAT ,& _unpackFloat16Texel < 4 > ); 70set (Format ::R16G16_FLOAT ,& _unpackFloat16Texel < 2 > ); 71set (Format ::R16_FLOAT ,& _unpackFloat16Texel < 1 > ); 72 73set (Format ::R8G8B8A8_UNORM ,& _unpackUnorm8Texel < 4 > ); 74set (Format ::B8G8R8A8_UNORM ,& _unpackUnormBGRA8Texel ); 75set (Format ::R16_UINT ,& _unpackUInt16Texel < 1 > ); 76set (Format ::R32_UINT ,& _unpackUInt32Texel < 1 > ); 77set (Format ::D32_FLOAT ,& _unpackFloatTexel < 1 > ); 78 } 79 80void set (Format format ,CPUTextureUnpackFunc func ) 81 { 82 auto& info = m_infos [Index (format )]; 83info .unpackFunc = func ; 84 } 85SLANG_FORCE_INLINE const CPUTextureFormatInfo & get (Format format )const 86 { 87return m_infos [Index (format )]; 88 } 89 90CPUTextureFormatInfo m_infos [Index (Format ::_Count )]; 91}; 92 93static const CPUFormatInfoMap g_formatInfoMap ; 94 95static CPUTextureFormatInfo const * _getFormatInfo (Format format ) 96{ 97const CPUTextureFormatInfo & info = g_formatInfoMap .get (format ); 98return info .unpackFunc ?& info :nullptr ; 99} 100 101class TextureResourceImpl :public TextureResource 102{ 103enum 104 { 105kMaxRank = 3 106 }; 107 108public : 109TextureResourceImpl (const TextureResource ::Desc & desc ) 110 :TextureResource (desc ) 111 { 112 } 113 ~TextureResourceImpl (); 114 115Result init (ITextureResource ::SubresourceData const * initData ); 116 117Desc const & _getDesc () {return m_desc ; } 118Format getFormat () {return m_desc .format ; } 119int32_t getRank () {return m_baseShape -> rank ; } 120 121CPUTextureBaseShapeInfo const * m_baseShape ; 122CPUTextureFormatInfo const * m_formatInfo ; 123int32_t m_effectiveArrayElementCount = 0 ; 124uint32_t m_texelSize = 0 ; 125 126struct MipLevel 127 { 128int32_t extents [kMaxRank ]; 129int64_t strides [kMaxRank + 1 ]; 130int64_t offset ; 131 }; 132List < MipLevel > m_mipLevels ; 133void * m_data = nullptr ; 134}; 135 136}// namespace cpu 137}// namespace gfx