码迷,mamicode.com
首页 > 其他好文 > 详细

bgfx源码里的几个宏

时间:2020-02-01 16:16:27      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:lock   fine   arp   info   style   and   hand   shader   img   

0. vs 查看预编译后的代码

技术图片

技术图片

 

1. # 和 ##

#define P(A) { printf(" the square of %s is %d\n", #A, A * A); }
#define Q(B) { printf(" the square of "#B" is %d\n" , B * B); }

#define Handle(_name) struct _name##Handle { unsigned int idx; };
Handle(Shader);
Handle(VertexBuffer);
Handle(IndexBuffer);

int main() {
    P(16);
    Q(25);
    IndexBufferHandle ibh; ibh.idx = 0;
    return 0;
}

上面代码会展开成:

struct ShaderHandle { unsigned int idx; };;
struct VertexBufferHandle { unsigned int idx; };;
struct IndexBufferHandle { unsigned int idx; };;

int main() {
    { printf(" the square of %s is %d\n", "16", 16 * 16); };

    { printf(" the square of ""25"" is %d\n" , 25 * 25); };

    IndexBufferHandle ibh; ibh.idx = 0;   

    return 0;  
}

2. BX_VA_ARGS_COUNT(...) -> 替换成参数列表的参数个数

#if BX_COMPILER_MSVC
// Workaround MSVS bug...
#    define BX_VA_ARGS_PASS(...) BX_VA_ARGS_PASS_1_ __VA_ARGS__ BX_VA_ARGS_PASS_2_
#    define BX_VA_ARGS_PASS_1_ (
#    define BX_VA_ARGS_PASS_2_ )
#else
#    define BX_VA_ARGS_PASS(...) (__VA_ARGS__)
#endif // BX_COMPILER_MSVC

#define BX_VA_ARGS_COUNT(...) BX_VA_ARGS_COUNT_ BX_VA_ARGS_PASS(__VA_ARGS__, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
#define BX_VA_ARGS_COUNT_(_a1, _a2, _a3, _a4, _a5, _a6, _a7, _a8, _a9, _a10, _a11, _a12, _a13, _a14, _a15, _a16, _last, ...) _last

int main() {
    auto v = BX_VA_ARGS_COUNT(42, 11);
    std::cout << v << std::endl;
    return 0;
}

 

 预编译后:

int main() {
   auto v = 2;
   std::cout << v << std::endl;
   return 0;      
}

 3. BX_UNUSED(...)

#define BX_MACRO_BLOCK_BEGIN for(;;) {
#define BX_MACRO_BLOCK_END break; }
#define BX_NOOP(...) BX_MACRO_BLOCK_BEGIN BX_MACRO_BLOCK_END

#define BX_PRAGMA_DIAGNOSTIC_PUSH              BX_PRAGMA_DIAGNOSTIC_PUSH_MSVC_
#define BX_PRAGMA_DIAGNOSTIC_POP               BX_PRAGMA_DIAGNOSTIC_POP_MSVC_
#define BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC(_x)

#define BX_PRAGMA_DIAGNOSTIC_PUSH_MSVC_()     __pragma(warning(push) )
#define BX_PRAGMA_DIAGNOSTIC_POP_MSVC_()      __pragma(warning(pop) )
#define BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(_x) __pragma(warning(disable:_x) )

#define BX_MACRO_DISPATCHER(_func, ...) BX_MACRO_DISPATCHER_1_(_func, BX_VA_ARGS_COUNT(__VA_ARGS__) )
#define BX_MACRO_DISPATCHER_1_(_func, _argCount) BX_MACRO_DISPATCHER_2_(_func, _argCount)
#define BX_MACRO_DISPATCHER_2_(_func, _argCount) BX_CONCATENATE(_func, _argCount)

#define BX_CONCATENATE(_x, _y) BX_CONCATENATE_(_x, _y)
#define BX_CONCATENATE_(_x, _y) _x ## _y

#if BX_COMPILER_MSVC
// Workaround MSVS bug...
#    define BX_UNUSED(...) BX_MACRO_DISPATCHER(BX_UNUSED_, __VA_ARGS__) BX_VA_ARGS_PASS(__VA_ARGS__)
#else
#    define BX_UNUSED(...) BX_MACRO_DISPATCHER(BX_UNUSED_, __VA_ARGS__)(__VA_ARGS__)
#endif // BX_COMPILER_MSVC

#define BX_UNUSED_1(_a1)                                              \
    BX_MACRO_BLOCK_BEGIN                                                      BX_PRAGMA_DIAGNOSTIC_PUSH();                                          /*BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wuseless-cast");*/         (void)(true ? (void)0 : ( (void)(_a1) ) );                            BX_PRAGMA_DIAGNOSTIC_POP();                                       BX_MACRO_BLOCK_END

#define BX_UNUSED_2(_a1, _a2) BX_UNUSED_1(_a1); BX_UNUSED_1(_a2)
#define BX_UNUSED_3(_a1, _a2, _a3) BX_UNUSED_2(_a1, _a2); BX_UNUSED_1(_a3)
#define BX_UNUSED_4(_a1, _a2, _a3, _a4) BX_UNUSED_3(_a1, _a2, _a3); BX_UNUSED_1(_a4)
#define BX_UNUSED_5(_a1, _a2, _a3, _a4, _a5) BX_UNUSED_4(_a1, _a2, _a3, _a4); BX_UNUSED_1(_a5)
#define BX_UNUSED_6(_a1, _a2, _a3, _a4, _a5, _a6) BX_UNUSED_5(_a1, _a2, _a3, _a4, _a5); BX_UNUSED_1(_a6)
#define BX_UNUSED_7(_a1, _a2, _a3, _a4, _a5, _a6, _a7) BX_UNUSED_6(_a1, _a2, _a3, _a4, _a5, _a6); BX_UNUSED_1(_a7)
#define BX_UNUSED_8(_a1, _a2, _a3, _a4, _a5, _a6, _a7, _a8) BX_UNUSED_7(_a1, _a2, _a3, _a4, _a5, _a6, _a7); BX_UNUSED_1(_a8)
#define BX_UNUSED_9(_a1, _a2, _a3, _a4, _a5, _a6, _a7, _a8, _a9) BX_UNUSED_8(_a1, _a2, _a3, _a4, _a5, _a6, _a7, _a8); BX_UNUSED_1(_a9)
#define BX_UNUSED_10(_a1, _a2, _a3, _a4, _a5, _a6, _a7, _a8, _a9, _a10) BX_UNUSED_9(_a1, _a2, _a3, _a4, _a5, _a6, _a7, _a8, _a9); BX_UNUSED_1(_a10)
#define BX_UNUSED_11(_a1, _a2, _a3, _a4, _a5, _a6, _a7, _a8, _a9, _a10, _a11) BX_UNUSED_10(_a1, _a2, _a3, _a4, _a5, _a6, _a7, _a8, _a9, _a10); BX_UNUSED_1(_a11)
#define BX_UNUSED_12(_a1, _a2, _a3, _a4, _a5, _a6, _a7, _a8, _a9, _a10, _a11, _a12) BX_UNUSED_11(_a1, _a2, _a3, _a4, _a5, _a6, _a7, _a8, _a9, _a10, _a11); BX_UNUSED_1(_a12)

int main() {
    BX_UNUSED(42);
    return 0;
}

 

 展开成:

int main() {
    for(;;) { __pragma(warning(push) ); (void)(true ? (void)0 : ( (void)(42) ) ); __pragma(warning(pop) ); break; };
    return 0;  
}

 其中 #pragma warning( push / pop )的作用是:暂存或者恢复编译器的警告状态,比如:

#pragma warning( push )
#pragma warning( disable : 4705 )
#pragma warning( disable : 4706 )
#pragma warning( disable : 4707 )
// Some code
#pragma warning( pop ) 

 // Some code这块代码不会报4705-4707这几个错误

 

bgfx源码里的几个宏

标签:lock   fine   arp   info   style   and   hand   shader   img   

原文地址:https://www.cnblogs.com/redips-l/p/12248677.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!