aboutsummaryrefslogtreecommitdiff
path: root/APEX_1.4/framework/src/ApexCreateSDK.cpp
blob: 9a8272b46a10804e10fb811641d57303d31dd5c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * Copyright (c) 2008-2017, NVIDIA CORPORATION.  All rights reserved.
 *
 * NVIDIA CORPORATION and its licensors retain all intellectual property
 * and proprietary rights in and to this software, related documentation
 * and any modifications thereto.  Any use, reproduction, disclosure or
 * distribution of this software and related documentation without an express
 * license agreement from NVIDIA CORPORATION is strictly prohibited.
 */


#include "Apex.h"
#include "ApexSDKImpl.h"
#include "ProfilerCallback.h"
#include "PxTaskManager.h"
#include "PxCpuDispatcher.h"
#include "ApexCudaContextManager.h"
#if APEX_CUDA_SUPPORT && !defined(INSTALLER)
#include "CudaContextManager.h"
#include "PhysXDeviceSettings.h"
#endif
#include "PxCudaContextManager.h"
#include "PxErrorCallback.h"

#if PX_PHYSICS_VERSION_MAJOR == 0
#include "ThreadPool.h"
#endif

namespace nvidia
{
namespace apex
{

ApexSDKImpl* gApexSdk = NULL;	// For an SDK singleton

APEX_API ApexSDK*	CALL_CONV GetApexSDK()
{
	return gApexSdk;
}
APEX_API ApexSDKIntl*	CALL_CONV GetInternalApexSDK()
{
	return gApexSdk;
}

ApexSDK* CreateApexSDK(const ApexSDKDesc& desc, ApexCreateError* errorCode, uint32_t APEXsdkVersion, PxAllocatorCallback* alloc)
{
	PX_UNUSED(alloc);

	if (errorCode)
	{
		*errorCode = APEX_CE_NO_ERROR;
	}

	if (gApexSdk != NULL)
	{
		return gApexSdk;
	}

	if (APEXsdkVersion != APEX_SDK_VERSION)
	{
		if (errorCode)
		{
			*errorCode = APEX_CE_WRONG_VERSION;
		}
		return NULL;
	}

	if (!desc.isValid())	//this checks for SDK and cooking version mismatch!
	{
		if (errorCode)
		{
			if (desc.physXSDKVersion != PX_PHYSICS_VERSION)
			{
				*errorCode = APEX_CE_WRONG_VERSION;
			}
			else
			{
				*errorCode = APEX_CE_DESCRIPTOR_INVALID;
			}
		}
		return NULL;
	}


	PX_ASSERT(alloc == 0);

	gApexSdk = PX_NEW(ApexSDKImpl)(errorCode, APEXsdkVersion);
	gApexSdk->init(desc);

	return gApexSdk;
}

int GetSuggestedCudaDeviceOrdinal(PxErrorCallback& errc)
{
#if APEX_CUDA_SUPPORT && !defined(INSTALLER)
	return PhysXDeviceSettings::getSuggestedCudaDeviceOrdinal(errc);
#else
	PX_UNUSED(errc);
	return -1;
#endif
}

PxCudaContextManager* CreateCudaContextManager(const PxCudaContextManagerDesc& desc, PxErrorCallback& errorCallback)
{
#if APEX_CUDA_SUPPORT && !defined(INSTALLER)
	return physx::createCudaContextManager(desc, errorCallback);
#else
	PX_UNUSED(desc);
	PX_UNUSED(errorCallback);
	return NULL;
#endif
}


#if PX_PHYSICS_VERSION_MAJOR == 0

/* We route allocations of these objects through the APEX SDK because PXTASK objects
 * require a foundation instance.
 */

PxCpuDispatcher* ApexSDKImpl::getDefaultThreadPool()
{
	if (!mApexThreadPool)
	{
		mApexThreadPool = createDefaultThreadPool(0);
	}

	return mApexThreadPool;
}

PxCpuDispatcher* ApexSDKImpl::createCpuDispatcher(uint32_t numThreads)
{
	PxCpuDispatcher* cd = createDefaultThreadPool(numThreads);
	mUserAllocThreadPools.pushBack(cd);
	return cd;
}

void ApexSDKImpl::releaseCpuDispatcher(PxCpuDispatcher& cd)
{
	if (&cd == mApexThreadPool)
	{
		PX_DELETE(mApexThreadPool);
		mApexThreadPool = 0;
		return;
	}
	for (uint32_t i = 0; i < mUserAllocThreadPools.size(); i++)
	{
		if (&cd == mUserAllocThreadPools[i])
		{
			PX_DELETE(&cd);
			mUserAllocThreadPools.replaceWithLast(i);
			return;
		}
	}
}

#endif

}
} // end namespace nvidia::apex