/* 
 * 
 * Confidential Information of Telekinesys Research Limited (t/a Havok).  Not for disclosure or distribution without Havok's
 * prior written consent.This software contains code, techniques and know-how which is confidential and proprietary to Havok.
 * Level 2 and Level 3 source code contains trade secrets of Havok. Havok Software (C) Copyright 1999-2008 Telekinesys Research Limited t/a Havok. All Rights Reserved. Use of this software is subject to the terms of an end user license agreement.
 * 
 */

#ifndef HK_DEMO_UTILS_COLLISION_QUERY_MULTITHREADING_UTILITY_H
#define HK_DEMO_UTILS_COLLISION_QUERY_MULTITHREADING_UTILITY_H


#include <Common/Base/Thread/JobQueue/hkJobQueue.h>
#include <Common/Base/Spu/Util/hkSpuUtil.h>
#include <Common/Base/Thread/Semaphore/hkSemaphoreBusyWait.h>
#include <Physics/Collide/Query/Multithreaded/hkpCollisionQueryJobData.h>


struct hkpCollisionQueryJob;

class hkpRigidBody;
class hkpJobDispatcherUtil;
class hkSpuMonitorCache;
class hkpMultithreadingUtil;


class CollisionQueryMultithreadedUtility
{
	public:

		HK_DECLARE_NONVIRTUAL_CLASS_ALLOCATOR(HK_MEMORY_CLASS_DEMO, CollisionQueryMultithreadedUtility);

		struct Input
		{
			public:

				Input();

			public:

				hkpWorld*			m_world;
				hkBool				m_registerDefaultPpuJobFunctions;
		};

		struct InputPs3 : public Input
		{
			public:

				InputPs3();

			public:

				int					m_numSpus;
				int					m_spursThreadGroupPriority;
				int					m_spursHandlerThreadPriority;
				hkSpuUtil**			m_spuUtil;
				hkSpuMonitorCache**	m_monitorCacheArray;
		};

	public:

		 CollisionQueryMultithreadedUtility();
		~CollisionQueryMultithreadedUtility();

		// 'INIT' FUNCTIONS
		void initPpu	  (Input&	 input);
		void initSpu	  (InputPs3& input);
		void initPpuAndSpu(InputPs3& input);

		// 'PUT JOB ONTO QUEUE' FUNCTIONS
		void putJobOntoQueue(hkpCollisionQueryJob* job, hkJobQueue::JobType jobType = hkJobQueue::JOB_TYPE_HINT_SPU_CQ);

		// 'START JOBS' FUNCTIONS
		void startPpu(hkpMultithreadingUtil* multiThreadingUtil);
		void startSpu();
		void startPpuAndSpu(hkpMultithreadingUtil* multiThreadingUtil);

		// 'WAIT' FUNCTIONS
		void waitForSpuFinished();

	private:

		void initJobQueue();
		void initPpuJobDispatcherUtility(Input& input);
		void initPs3Spurs(InputPs3& input, bool spuOnly);

	private:

		HK_ALIGN128( hkSemaphoreBusyWait		m_cpuWaitSemaphore			);
		HK_ALIGN128( hkSemaphoreBusyWait		m_taskCompletionSemaphore	);
		HK_ALIGN128( hkSemaphoreBusyWait		m_spuWaitSemaphore			);
		HK_ALIGN   ( hkJobQueue					m_jobQueue, 64				);
		HK_ALIGN16 ( hkpCollisionQueryJobData	m_jobData					);

		hkpJobDispatcherUtil*					m_jobDispatcherUtil;
		hkpMultithreadingUtil*					m_multiThreadingUtil;
		hkSpuUtil*								m_spuUtil;
		void*									m_elf;
		hkArray<hkSpuTaskParams>				m_elfParams;
};


#endif	// HK_DEMO_UTILS_COLLISION_QUERY_MULTITHREADING_UTILITY_H

/*
* Havok SDK - NO SOURCE PC DOWNLOAD, BUILD(#20080529)
*
* Confidential Information of Havok.  (C) Copyright 1999-2008 
* Telekinesys Research Limited t/a Havok. All Rights Reserved. The Havok
* Logo, and the Havok buzzsaw logo are trademarks of Havok.  Title, ownership
* rights, and intellectual property rights in the Havok software remain in
* Havok and/or its suppliers.
*
* Use of this software for evaluation purposes is subject to and indicates 
* acceptance of the End User licence Agreement for this product. A copy of 
* the license is included with this software and is also available at
* www.havok.com/tryhavok
*
*/
