git.s-ol.nu ~forks/DiligentTools / a294546
Improved type safety of flag enums Egor Yusov 3 years ago
12 changed file(s) with 39 addition(s) and 34 deletion(s). Raw diff Collapse all Expand all
2828
2929 namespace std
3030 {
31 DEFINE_ENUM_HASH( Diligent::COMMIT_SHADER_RESOURCES_FLAG )
31 DEFINE_ENUM_HASH( Diligent::COMMIT_SHADER_RESOURCES_FLAGS )
3232 }
3333
3434 namespace Diligent
6060 int TransitionShaderResources( lua_State * );
6161 ClassMethodCaller<DeviceContextFuncBindings> m_TransitionShaderResourcesBinding;
6262
63 EnumMapping<COMMIT_SHADER_RESOURCES_FLAG> m_CommitShaderResFlagsEnumMapping;
63 EnumMapping<COMMIT_SHADER_RESOURCES_FLAGS> m_CommitShaderResFlagsEnumMapping;
6464
6565 String m_TexViewMetatableName;
6666 String m_ShaderResBindingMetatableName;
4242 // Explicit namespace declaraion is necesseary to avoid
4343 // name conflicts when building for windows store
4444 DEFINE_ENUM_HASH( Diligent::BIND_FLAGS )
45 DEFINE_ENUM_HASH( Diligent::CPU_ACCESS_FLAG )
45 DEFINE_ENUM_HASH( Diligent::CPU_ACCESS_FLAGS )
4646 DEFINE_ENUM_HASH( Diligent::COMPARISON_FUNCTION )
4747 DEFINE_ENUM_HASH( Diligent::BIND_SHADER_RESOURCES_FLAGS )
4848 DEFINE_ENUM_HASH( Diligent::SHADER_TYPE )
6464 };
6565 #define DEFINE_ENUM_ELEMENT_MAPPING(EnumMapping, Elem) EnumMapping.AddMapping(#Elem, Elem)
6666
67 class CpuAccessFlagEnumMapping : public EnumMapping < Diligent::CPU_ACCESS_FLAG >
67 class CpuAccessFlagEnumMapping : public EnumMapping < Diligent::CPU_ACCESS_FLAGS >
6868 {
6969 public:
7070 CpuAccessFlagEnumMapping();
460460 BindingsMap.insert( std::make_pair( #Member, std::unique_ptr<MemberBinderBase>(pNewBinder) ) ); \
461461 }while(false)
462462
463 template< typename EnumType, typename FlagsType = Uint32 >
463 template< typename EnumType, typename FlagsType = std::underlying_type<EnumType>::type >
464464 class FlagsLoader : public MemberBinderBase
465465 {
466466 public:
477477 int ArrayInd = 1;
478478 for( auto it = m_EnumMapping.m_Val2StrMap.begin(); it != m_EnumMapping.m_Val2StrMap.end(); ++it )
479479 {
480 if( static_cast<EnumType>(Flags & it->first) == it->first )
480 if( it->first != 0 && static_cast<EnumType>(Flags & it->first) == it->first || Flags == 0 && it->first == 0)
481481 {
482482 lua_pushnumber( L, ArrayInd ); // -0 | +1 -> +1
483483 PushValue<const String&>( L, it->second ); // -0 | +1 -> +1
489489
490490 virtual void SetValue( lua_State *L, int Index, void* pBasePointer )
491491 {
492 FlagsType Flags = 0;
492 FlagsType Flags = static_cast<FlagsType>(0);
493493 if( lua_isnumber( L, Index ) )
494494 {
495495 Flags = static_cast<FlagsType>( ReadValueFromLua<Uint32>( L, Index ) );
527527 {
528528 String AllowableValues = GetEnumMappingsString<EnumType>( m_EnumMapping );
529529 SCRIPT_PARSING_ERROR( L, "Unknown flag (\"", CurrFlagName, "\") provided for parameter ", m_MemberName, ". Only the following flags are allowed:\n", AllowableValues );
530 return 0;
530 return static_cast<FlagsType>(0);
531531 }
532532 }
533533
2929
3030 namespace std
3131 {
32 DEFINE_ENUM_HASH( Diligent::MISC_TEXTURE_FLAG )
32 DEFINE_ENUM_HASH( Diligent::MISC_TEXTURE_FLAGS )
3333 }
3434
3535 namespace Diligent
5757 // Explicit namespace declaraion is necesseary to avoid
5858 // name conflicts when building for windows store
5959
60 EnumMapping<Diligent::MISC_TEXTURE_FLAG> m_MiscFlagEnumMapping;
60 EnumMapping<Diligent::MISC_TEXTURE_FLAGS> m_MiscFlagEnumMapping;
6161
6262 UsageEnumMapping m_UsageEnumMapping;
6363 CpuAccessFlagEnumMapping m_CpuAccessFlagEnumMapping;
5050 DEFINE_FLAGS_BINDER( m_Bindings, SBuffDescWrapper, BindFlags, Diligent::BIND_FLAGS, m_BindFlagEnumMapping );
5151
5252 DEFINE_ENUM_BINDER( m_Bindings, SBuffDescWrapper, Usage, m_UsageEnumMapping );
53 DEFINE_FLAGS_BINDER( m_Bindings, SBuffDescWrapper, CPUAccessFlags, CPU_ACCESS_FLAG, m_CpuAccessFlagEnumMapping );
53 DEFINE_FLAGS_BINDER( m_Bindings, SBuffDescWrapper, CPUAccessFlags, CPU_ACCESS_FLAGS, m_CpuAccessFlagEnumMapping );
5454
5555 DEFINE_ENUM_ELEMENT_MAPPING( m_BuffModeEnumMapping, BUFFER_MODE_UNDEFINED );
5656 DEFINE_ENUM_ELEMENT_MAPPING( m_BuffModeEnumMapping, BUFFER_MODE_FORMATTED );
6565
6666 DEFINE_BINDER( m_Bindings, SBuffDescWrapper, ElementByteStride );
6767
68 DEFINE_ENUM_ELEMENT_MAPPING( m_SetVBFlagEnumMapping, SET_VERTEX_BUFFERS_FLAG_NONE );
6869 DEFINE_ENUM_ELEMENT_MAPPING( m_SetVBFlagEnumMapping, SET_VERTEX_BUFFERS_FLAG_RESET );
6970 };
7071
138139 }
139140 }
140141
141 Uint32 Flags = 0;
142 SET_VERTEX_BUFFERS_FLAGS Flags = SET_VERTEX_BUFFERS_FLAG_NONE;
142143 Uint32 NumBuffers = 0;
143144 IBuffer *pBuffs[MaxBufferSlots] = {};
144145 Uint32 Offsets[MaxBufferSlots] = {};
4545 m_ShaderResBindingMetatableName( pSRBParser->GetMetatableName() ),
4646 m_PSOMetatableName(pPSOParser->GetMetatableName())
4747 {
48 DEFINE_ENUM_ELEMENT_MAPPING( m_CommitShaderResFlagsEnumMapping, COMMIT_SHADER_RESOURCES_FLAG_NONE );
4849 DEFINE_ENUM_ELEMENT_MAPPING( m_CommitShaderResFlagsEnumMapping, COMMIT_SHADER_RESOURCES_FLAG_TRANSITION_RESOURCES );
50 DEFINE_ENUM_ELEMENT_MAPPING( m_CommitShaderResFlagsEnumMapping, COMMIT_SHADER_RESOURCES_FLAG_VERIFY_STATES );
4951 };
5052
5153 int DeviceContextFuncBindings::SetRenderTargets( lua_State *L )
122124 ITextureView *pView = nullptr;
123125 Float32 fDepth = 1.f;
124126 Uint8 Stencil = 0;
125 Uint32 ClearFlags = 0;
127 CLEAR_DEPTH_STENCIL_FLAGS ClearFlags = CLEAR_DEPTH_FLAG_NONE;
126128
127129 int CurrArg = 1;
128130 if( CurrArg <= NumArgs )
198200 }
199201 }
200202
201 Uint32 Flags = 0;
203 COMMIT_SHADER_RESOURCES_FLAGS Flags = COMMIT_SHADER_RESOURCES_FLAG_NONE;
202204 if(NumArgs >= CurrArg &&
203205 (lua_type( L, CurrArg ) == LUA_TSTRING ||
204206 lua_type( L, CurrArg ) == LUA_TTABLE ) )
205207 {
206 FlagsLoader<COMMIT_SHADER_RESOURCES_FLAG> CommitShaderResFlagsLoader(0, "CommitShaderResourcesFlag", m_CommitShaderResFlagsEnumMapping);
208 FlagsLoader<COMMIT_SHADER_RESOURCES_FLAGS> CommitShaderResFlagsLoader(0, "CommitShaderResourcesFlag", m_CommitShaderResFlagsEnumMapping);
207209 CommitShaderResFlagsLoader.SetValue( L, CurrArg, &Flags );
208210 ++CurrArg;
209211 }
2929 {
3030 CpuAccessFlagEnumMapping::CpuAccessFlagEnumMapping()
3131 {
32 DEFINE_ENUM_ELEMENT_MAPPING( (*this), CPU_ACCESS_NONE );
3233 DEFINE_ENUM_ELEMENT_MAPPING( (*this), CPU_ACCESS_READ );
3334 DEFINE_ENUM_ELEMENT_MAPPING( (*this), CPU_ACCESS_WRITE );
3435 }
144144 DEFINE_FLAGS_BINDER( m_Bindings, STexDescWrapper, BindFlags, Diligent::BIND_FLAGS, m_BindFlagEnumMapping );
145145
146146 DEFINE_ENUM_BINDER( m_Bindings, STexDescWrapper, Usage, m_UsageEnumMapping );
147 DEFINE_FLAGS_BINDER( m_Bindings, STexDescWrapper, CPUAccessFlags, CPU_ACCESS_FLAG, m_CpuAccessFlagEnumMapping );
147 DEFINE_FLAGS_BINDER( m_Bindings, STexDescWrapper, CPUAccessFlags, CPU_ACCESS_FLAGS, m_CpuAccessFlagEnumMapping );
148148
149 DEFINE_ENUM_ELEMENT_MAPPING( m_MiscFlagEnumMapping, MISC_TEXTURE_FLAG_NONE );
149150 DEFINE_ENUM_ELEMENT_MAPPING( m_MiscFlagEnumMapping, MISC_TEXTURE_FLAG_GENERATE_MIPS );
150 DEFINE_FLAGS_BINDER( m_Bindings, STexDescWrapper, MiscFlags, Diligent::MISC_TEXTURE_FLAG, m_MiscFlagEnumMapping );
151 DEFINE_FLAGS_BINDER( m_Bindings, STexDescWrapper, MiscFlags, Diligent::MISC_TEXTURE_FLAGS, m_MiscFlagEnumMapping );
151152
152153 DEFINE_BINDER_EX( m_Bindings, STexDescWrapper, ClearValue, OptimizedClearValue, 0 );
153154 };
6262 size_t maxsize,
6363 Diligent::USAGE usage,
6464 const char* name,
65 unsigned int bindFlags,
66 unsigned int cpuAccessFlags,
67 unsigned int miscFlags,
65 Diligent::BIND_FLAGS bindFlags,
66 Diligent::CPU_ACCESS_FLAGS cpuAccessFlags,
67 Diligent::MISC_TEXTURE_FLAGS miscFlags,
6868 bool forceSRGB,
6969 Diligent::ITexture** texture/*,
7070 D2D1_ALPHA_MODE* alphaMode*/
3939 USAGE Usage;
4040
4141 /// Bind flags
42 Uint32 BindFlags;
42 BIND_FLAGS BindFlags;
4343
4444 /// Number of mip levels
4545 Uint32 MipLevels;
4646
4747 /// CPU access flags
48 Uint32 CPUAccessFlags;
48 CPU_ACCESS_FLAGS CPUAccessFlags;
4949
5050 /// Flag indicating if this texture uses sRGB gamma encoding
5151 Bool IsSRGB;
6161 Usage( USAGE_STATIC ),
6262 BindFlags( BIND_SHADER_RESOURCE ),
6363 MipLevels(0),
64 CPUAccessFlags(0),
64 CPUAccessFlags(CPU_ACCESS_NONE),
6565 IsSRGB(false),
6666 GenerateMips(true),
6767 Format(TEX_FORMAT_UNKNOWN)
931931 _In_ DXGI_FORMAT format,
932932 _In_ USAGE usage,
933933 _In_ const char *name,
934 _In_ unsigned int bindFlags,
935 _In_ unsigned int cpuAccessFlags,
936 _In_ unsigned int miscFlags,
934 _In_ BIND_FLAGS bindFlags,
935 _In_ CPU_ACCESS_FLAGS cpuAccessFlags,
936 _In_ MISC_TEXTURE_FLAGS miscFlags,
937937 _In_ bool forceSRGB,
938938 _In_ bool isCubeMap,
939939 _In_ TextureSubResData* initData,
11531153 _In_ size_t maxsize,
11541154 _In_ USAGE usage,
11551155 _In_ const char *name,
1156 _In_ unsigned int bindFlags,
1157 _In_ unsigned int cpuAccessFlags,
1158 _In_ unsigned int miscFlags,
1156 _In_ BIND_FLAGS bindFlags,
1157 _In_ CPU_ACCESS_FLAGS cpuAccessFlags,
1158 _In_ MISC_TEXTURE_FLAGS miscFlags,
11591159 _In_ bool forceSRGB,
11601160 _Outptr_opt_ ITexture** texture
11611161 )
14101410 /*D2D1_ALPHA_MODE* alphaMode,*/
14111411 const char* name)
14121412 {
1413 return CreateDDSTextureFromMemoryEx(pDevice, ddsData, ddsDataSize, maxsize, USAGE_DEFAULT, name, BIND_SHADER_RESOURCE, 0, 0, false, texture/*, alphaMode*/);
1413 return CreateDDSTextureFromMemoryEx(pDevice, ddsData, ddsDataSize, maxsize, USAGE_DEFAULT, name, BIND_SHADER_RESOURCE, CPU_ACCESS_NONE, MISC_TEXTURE_FLAG_NONE, false, texture/*, alphaMode*/);
14141414 }
14151415
14161416
14221422 size_t maxsize,
14231423 USAGE usage,
14241424 const char *name,
1425 unsigned int bindFlags,
1426 unsigned int cpuAccessFlags,
1427 unsigned int miscFlags,
1425 BIND_FLAGS bindFlags,
1426 CPU_ACCESS_FLAGS cpuAccessFlags,
1427 MISC_TEXTURE_FLAGS miscFlags,
14281428 bool forceSRGB,
14291429 ITexture** texture/*,
14301430 D2D1_ALPHA_MODE* alphaMode*/
248248 TexLoadInfo.Name,
249249 TexLoadInfo.BindFlags,
250250 TexLoadInfo.CPUAccessFlags,
251 0, // miscFlags
251 MISC_TEXTURE_FLAG_NONE, // miscFlags
252252 TexLoadInfo.IsSRGB, // forceSRGB
253253 ppTexture );
254254 }