Nvidia PhysX 学习文档5:Startup and Shutdown

                                         延迟满足


official site: https://gameworksdocs.nvidia.com/PhysX/4.1/documentation/physxguide/Manual/Startup.html#startup-and-shutdown

红色代表需要弄懂的。


Introduction

The first step in using the PhysX SDK in a program is the initialization of some global objects. These objects can be released when PhysX is no longer needed to free resources. This chapter describes how to do this.

使用physx sdk的第一步是初始化一些全局变量。 这些变量可以再不需要physx的时候释放掉。

Foundation and Physics

First, in some startup code, create a PxFoundation object:

static PxDefaultErrorCallback gDefaultErrorCallback;
static PxDefaultAllocator gDefaultAllocatorCallback;

mFoundation = PxCreateFoundation(PX_PHYSICS_VERSION, gDefaultAllocatorCallback,
    gDefaultErrorCallback);
if(!mFoundation)
    fatalError("PxCreateFoundation failed!");

Every PhysX module requires a PxFoundation instance to be available. The required parameters are a version ID, an allocator callback and an error callback. PX_PHYSICS_VERSION, is a macro predefined in our headers to enable PhysX to check for a version mismatch between the headers and the corresponding SDK DLLs.

首先,需要创建一个PxFoundation object。 每一个physx module都需要一个这种object!   PxCreateFoundation函数的入参中,第一个是版本号,然后是,allocator callback 对象(用于内存申请), error callback 对象.   PX_PHYSICS_VERSION 是一个在头文件中定义的宏,physx使用该宏来检查头文件和dll的版本是否匹配。

Usually, the allocator callback and error callback are specific to the application, but PhysX provides default implementations that make it easy to get started. See Memory Management and Error Reporting for more details of these callbacks. (The actual sample code supports an advanced memory allocator that tracks allocations instead of the default, but we have omitted that detail here.)

对于 allocator callback 和error callback,它们可以是用户自定义的类的对象,但是,方便起见,physx提供了默认的类以方便用户。(https://blog.csdn.net/qq_35865125/article/details/102808652这篇已经学习了)。

Now create the top-level PxPhysics object:

bool recordMemoryAllocations = true;

mPvd = PxCreatePvd(*gFoundation);
PxPvdTransport* transport = PxDefaultPvdSocketTransportCreate(PVD_HOST, 5425, 10);
mPvd->connect(*transport,PxPvdInstrumentationFlag::eALL);


mPhysics = PxCreatePhysics(PX_PHYSICS_VERSION, *mFoundation,
    PxTolerancesScale(), recordMemoryAllocations, mPvd);
if(!mPhysics)
    fatalError("PxCreatePhysics failed!");

Again, the version ID has to be passed in. The PxTolerancesScale parameter makes it easier to author content at different scales and still have PhysX work as expected, but to get started simply pass a default object of this type. The recordMemoryAllocations parameter specifies whether to perform memory profiling. The optional PVD instance enables the debugging and profiling with the PhysX Visual Debugger.

PxCreatePhysics函数用于创建PxPhysics  对象。 其入参中,PxTolerancesScale 用于设置仿真单位,上一篇已经学习。 这里,PxTolerancesScale()是使用了physx的默认的,以方便初学者。bool参数 recordMemoryAllocations用于指示是否perform memory profiling.  最后一个入参是physx visual debugger,用于debug。

 

Cooking

The PhysX cooking library provides utilities for creating, converting, and serializing bulk data. Depending on your application, you may wish to link to the cooking library in order to process such data at runtime. Alternatively you may be able to process all such data in advance and just load it into memory as required. Initialize the cooking library as follows:

physx cooking library 的功能是 create, convert, serialize 数据块。 用户如果在程序运行时处理这种数据块,则需要链接到这些cooking library。另一个选择是,提前处理所有的这种类型的数据,然后,只需要load到memory中。 初始化cooking library的方法:

mCooking = PxCreateCooking(PX_PHYSICS_VERSION, *mFoundation, PxCookingParams(scale));
if (!mCooking)
    fatalError("PxCreateCooking failed!");

The PxCookingParams struct configures the cooking library to target different platforms, use non-default tolerances or produce optional outputs. It is important to use consistent PxTolerancesScale values everywhere in your application (see Using Different Units for more details).

入参PxCookingParams(scale)的作用: 配置cooking library以适应不同的平台,use no-default tolerance 或者 produce optional outputs.

注意:在你的app中必须始终如一地使用同一个PxTolerancesScale值。

The cooking library generates data through a streaming interface. In the samples, implementations of streams are provided in the PxToolkit library to read and write from files and memory buffers. Heightfield or Trianglemesh cooked meshes can be directly inserted into PxPhysics without serialization using the PxPhysicsInsertionCallback. The default callback must be used and can be obtained using the PxPhysics::getPhysicsInsertionCallback().

cooking库通过一个streaming interface来产生数据, 在例子中PxToolkit 库中提供了implementation of streams 用于实现从文件和内存中进行读写。 Heightfield 或Trianglemesh cooked meshes 可以直接被插入到PxPhysics 中 ,无需使用PxPhysicsInsertionCallback进行serializationThe default callback must be used and can be obtained using the PxPhysics::getPhysicsInsertionCallback().

Extensions

The extensions library contains many functions that may be useful to a large class of users, but which some users may prefer to omit from their application either for code size reasons or to avoid use of certain subsystems, such as those pertaining to networking. Initializing the extensions library requires the PxPhysics object:

physx的extensions库包括许多对大多数用户都有用的函数。但是,一些用户考虑到code size或者avoidance of certain subsystem的原因,可能会选择不使用这个库,例如, 用于networking的app。  初始化extensions 库的方法:

if (!PxInitExtensions(*mPhysics, mPvd))
    fatalError("PxInitExtensions failed!");

Optional SDK Components

When linking PhysX as a static library on memory constrained platforms, it is possible to avoid linking the code of some PhysX features that are not always used in order to save memory. Currently the optional features are:

如果程序运行在内存小的系统中,且, linking physx as static 库, 可以过滤掉physx的一些特性以节省内存,例如可以过滤掉:

  • Articulations
  • Height Fields

If your application requires a subset of this functionality, it is recommended that you call PxCreateBasePhysics as opposed to PxCreatePhysics and then manually register the components you require. Below is an example that registers some of the options:

如果你的app需要subset of this functionality, 建议你使用PxCreateBasePhysics代替PxCreatePhysics 函数,然后手动选择性地注册你需要的component,例子::

physx::PxPhysics* customCreatePhysics(physx::PxU32 version,
    physx::PxFoundation& foundation,
    const physx::PxTolerancesScale& scale,
    bool trackOutstandingAllocations
    physx::PxPvd* pvd)
{
    physx::PxPhysics* physics = PxCreateBasePhysics(version, foundation, scale,
        trackOutstandingAllocations, pvd);

    if(!physics)
        return NULL;

    PxRegisterArticulations(*physics);
    PxRegisterHeightFields(*physics);

    return physics;
}

Note that this will only save memory when linking PhysX as a static library, as we rely on the linker to strip out the unused code.

 

Delay-Loading DLLs

The PhysXCommon DLL and PhysXFoundation DLL are marked as delay-loaded inside of the PhysX, PhysXCooking and PhysXCommon projects. So it is possible to have delay-loaded PhysXFoundation, PhysXCommon, PhysX and PhysXCooking DLLs.

(dll的延迟加载技术,一种通用的技术,并非physx独有的。need to learn!)

 

PhysXCommon DLL and PhysXFoundation DLL load

  • If delay load hook is specified the PhysXCommon name or PhysXFoundation name provided by user is used
  • If delay load hook is not specified, the corresponding PhysXCommon DLL or PhysXFoundation DLL is used

如果用户指定了 delay load hook, 由用户提供的PhysXCommon name or PhysXFoundation name将会被使用。

如果用户没有指定 delay load hook, the corresponding PhysXCommon DLL or PhysXFoundation DLL is used。

 

PxDelayLoadHook

The PxDelayLoadHook class supports loading of the PhysXCommon, PhysXFoundation DLLs with different names and paths. This can be achieved by providing different DLL paths to the PhysX SDK through a custom subclass of PxDelayLoadHook, see the following example:

PxDelayLoadHook 类 支持用不同的名字和路径来 load PhysXCommon, PhysXFoundation DLLs。 实现方法是:以PxDelayLoadHook为基类实现一个用户自定义的子类:

class SnippetDelayLoadHook: public PxDelayLoadHook
{
    virtual const char* getPhysXCommonDllName() const
        { return "customPath\\PhysXCommon_x64_Renamed.dll"; }

    virtual const char* getPhysXFoundationDllName() const
        { return "customPath\\PhysXFoundation_x64_Renamed.dll"; }
} gDelayLoadHook;

Now the hook must be set for PhysX, PhysXCooking, PhysXCommon:

然后,上面自定义的子类对象必须设给 PhysX, PhysXCooking, PhysXCommon:

PxSetPhysXDelayLoadHook(&gDelayLoadHook);
PxSetPhysXCookingDelayLoadHook(&gDelayLoadHook);
PxSetPhysXCommonDelayLoadHook(&gDelayLoadHook);

For more information please see the SnippetDelayLoadHook.

例子SnippetDelayLoadHook中有更详细的介绍。

 

PxGpuLoadHook

The PxGpuLoadHook class supports loading of the PhysXGpu DLL with a different name or path. This can be achieved by providing a different DLL path to the PhysX SDK through a custom subclass of PxGpuLoadHook, see the following example:

类PxGpuLoadHook支持以不同的名字或者路径来load PhysXGpu DLL。用户可以通过新建一个继承自PxGpuLoadHook的子类来实现:

class SnippetGpuLoadHook: public PxGpuLoadHook
{
    virtual const char* getPhysXGpuDllName() const
        { return "customPath\\PhysXGpu_x64_Renamed.dll"; }
} gGpuLoadHook;

Now the hook must be set for PhysX:

然后,调用PxSetPhysXGpuLoadHook函数:

PxSetPhysXGpuLoadHook(&gGpuLoadHook);

For more information please see the SnippetDelayLoadHook.

更多信息请参考SnippetDelayLoadHook。

PhysXCommon Secure Load

All PhysX DLLs distributed by NVIDIA are signed. The PhysXCommon DLL signature is checked, when it is loaded by PhysX or PhysXCooking. If signature test fails the application is terminated.

Nvidia发布的所有的physx dll都是被签名了的。 PhysXCommon  dll被physx 或者 PhysXCooking加载时,其签名会被检查。 如果检查失败,整个程序就停止。

Shutting Down

To dispose of any PhysX object, call its release() method. This will destroy the object, and all contained objects. The precise behavior depends on the object type being released, so refer to the reference guide for details. To shut down the extensions library, call the function PxCloseExtensions(). To shut down physics, call release() on the PxPhysics object, and this will clean up all of the physics objects:

release()函数用于销毁physx对象,该函数会销毁对象本身以及其所包含的所有对象。 具体的销毁动作取决于被销毁对象的类型,具体请参考reference guide.  要关闭extensions library的话,需要调用 PxCloseExtensions()。 要关闭physics的话,需要调用 release() on the PxPhysics object。

mPhysics->release();

Do not forget to release the foundation object as well, but only after all other PhysX modules have been released:

mFoundation->release();
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

First Snowflakes

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值