/* 
 * 
 * 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.
 * 
 */


#include <Demos/demos.h>

#include <Physics/Collide/Query/Multithreaded/hkpCollisionJobs.h>

#include <Common/Visualize/hkDebugDisplay.h>

#include <Demos/DemoCommon/Utilities/CollisionQuery/CollisionQueryMultithreadedUtility.h>
#include <Demos/Physics/Api/Collide/RayCasting/ShapeRayCastMultithreading/ShapeRaycastMultithreadingApiDemo.h>


#if defined(HK_PLATFORM_PS3)
//	# of real SPUs
#	define NUM_SPUS 6
#else
//	# of simulated SPUs
#	define NUM_SPUS 1
#endif


struct ShapeRayCastMultithreadingApiDemoVariant
{
	enum DemoType
	{
		MULTITHREADED_ON_PPU,
		MULTITHREADED_ON_SPU,
		MULTITHREADED_ON_PPU_AND_SPU,
	};

	const char*	m_name;
	DemoType	m_demoType;
	const char*	m_details;
};


static const char helpString[] = \
	"This demo demonstrates how to emulate the hkpShape::castRay() functionality in a multithreaded "	\
	"environment through the use of a dedicated hkpShapeRayCastJob. By putting such a job on "			\
	"the job queue it can be processed by other threads or SPUs (on PS3). "								\
	"One command of this job will cast exactly one ray against an arbitrary number of collidables. "	\
	"You can place an arbitrary number of commands into one job, as the job will automatically split "	\
	"itself into parallel sub-jobs. "																	\
	"For more information on the original hkpShape::castRay() functionality, see ShapeRayCastDemo. ";

static const ShapeRayCastMultithreadingApiDemoVariant g_ShapeRayCastMultithreadingApiDemoVariants[] =
{
#if defined(HK_PLATFORM_HAS_SPU)
	{ "Multithreaded on PPU",		ShapeRayCastMultithreadingApiDemoVariant::MULTITHREADED_ON_PPU,			helpString },
	{ "Multithreaded on SPU",		ShapeRayCastMultithreadingApiDemoVariant::MULTITHREADED_ON_SPU,			helpString },
	{ "Multithreaded on PPU & SPU",	ShapeRayCastMultithreadingApiDemoVariant::MULTITHREADED_ON_PPU_AND_SPU,	helpString },
#else
	{ "Multithreaded",				ShapeRayCastMultithreadingApiDemoVariant::MULTITHREADED_ON_PPU,			helpString },
#endif
};


ShapeRayCastMultithreadingApiDemo::ShapeRayCastMultithreadingApiDemo(hkDemoEnvironment* env) : hkDefaultPhysicsDemo(env, DEMO_FLAGS_NO_SERIALIZE),
	m_semaphore(0, 1000)
{
	const ShapeRayCastMultithreadingApiDemoVariant& variant = g_ShapeRayCastMultithreadingApiDemoVariants[m_variantId];

	// Disable backface culling
	setGraphicsState(HKG_ENABLED_CULLFACE, false);
	
	// Setup the camera.
	{
		hkVector4 from(-6.0f, 1.0f, 10.0f);
		hkVector4 to  (2.0f, 0.0f, 0.0f);
		hkVector4 up  (0.0f, 1.0f, 0.0f);
		setupDefaultCameras(env, from, to, up);
	}

	// Create the world.
	{
		hkpWorldCinfo info;
		
		// Set gravity to zero so body floats.
		info.setBroadPhaseWorldSize( 100.0f );
		m_world = new hkpWorld(info);
		m_world->lock();

		m_world->setGravity(hkVector4::getZero());

		setupGraphics();
	}

	//
	// Create a simple box to cast our ray against.
	//
	{
		hkpBoxShape* boxShape;
		{
			hkVector4 oneOneOne = hkVector4(1.0f, 1.0f, 1.0f);
			boxShape = new hkpBoxShape(oneOneOne, 0 );
		}

		hkpRigidBodyCinfo rigidBodyInfo;
		{
			rigidBodyInfo.m_angularDamping	= 0;
			rigidBodyInfo.m_angularVelocity	. set(.05f, .25f, .5f);
			rigidBodyInfo.m_motionType		= hkpMotion::MOTION_DYNAMIC;
			rigidBodyInfo.m_position		. set(0.0f, 0.0f, 0.0f);
			rigidBodyInfo.m_shape			= boxShape;
		}

		m_box = new hkpRigidBody(rigidBodyInfo);

		boxShape->removeReference();

		m_world->addEntity(m_box);
	}

	m_world->unlock();

	//
	// Setup multithreading.
	//
	{
		CollisionQueryMultithreadedUtility::InputPs3 input;
		{
			input.m_world				= m_world;
			input.m_numSpus				= NUM_SPUS;
			input.m_spuUtil				= &m_spuUtil;
			input.m_monitorCacheArray	= &m_monitorCaches[0];
		}

		m_util = new CollisionQueryMultithreadedUtility();

		if		( variant.m_demoType == ShapeRayCastMultithreadingApiDemoVariant::MULTITHREADED_ON_PPU		   ) m_util->initPpu(input);
		else if ( variant.m_demoType == ShapeRayCastMultithreadingApiDemoVariant::MULTITHREADED_ON_SPU		   ) m_util->initSpu(input);
		else if ( variant.m_demoType == ShapeRayCastMultithreadingApiDemoVariant::MULTITHREADED_ON_PPU_AND_SPU ) m_util->initPpuAndSpu(input);

		// Special case for this demo variant: we do not allow the # of active SPUs to drop to zero as this can cause a deadlock.
		if ( variant.m_demoType == ShapeRayCastMultithreadingApiDemoVariant::MULTITHREADED_ON_SPU ) m_allowZeroActiveSpus = false;
	}
}


ShapeRayCastMultithreadingApiDemo::~ShapeRayCastMultithreadingApiDemo()
{
	delete m_util;
}


hkDemo::Result ShapeRayCastMultithreadingApiDemo::stepDemo()
{
	if ( !m_multithreadingUtil )
	{
		char* msg = "Demo only works when started with -mt (i.e. with multithreading enabled).";
		m_env->m_textDisplay->outputText(msg, 20, 250, 0xffffffff);
		HK_WARN(0xaf152feb, msg);
		return hkDemo::DEMO_OK;
	}

	const ShapeRayCastMultithreadingApiDemoVariant& variant = g_ShapeRayCastMultithreadingApiDemoVariants[m_variantId];

	//
	// Get the box's collidable.
	//
	const hkpCollidable* boxCollidable;
	{
		// hkpWorldObject::getCollidable() needs a read-lock on the object
		m_box->markForRead();
		boxCollidable = m_box->getCollidable();
		m_box->unmarkForRead();
	}

	//
	// Setup the array of collidables which we want to cast our ray against.
	// In this case we only have one collidable.
	//
	const hkpCollidable** collidableArray;
	{
		collidableArray	   = hkAlignedAllocate<const hkpCollidable*>(16, 1, HK_MEMORY_CLASS_DEMO);
		collidableArray[0] = boxCollidable;
	}

	//
	// Setup the output array where the ray's results (in this case only one) will be returned.
	//
	hkpShapeRayCastOutput* rayOutput = hkAllocateChunk<hkpShapeRayCastOutput>(1, HK_MEMORY_CLASS_DEMO);

	//
	// Setup the raycast command.
	//
	hkpShapeRayCastCommand* command;
	{
		command = hkAllocateChunk<hkpShapeRayCastCommand>(1, HK_MEMORY_CLASS_DEMO);

		// Init shape data.
		{
			command->m_collidables	  = collidableArray;
			command->m_numCollidables = 1;
		}

		// Init ray data.
		{
			command->m_rayInput.m_from					   . set(0.0f, 0.0f, 15.0f);
			command->m_rayInput.m_to					   . set(0.0f, 0.0f, 0.0f);
			command->m_rayInput.m_rayShapeCollectionFilter = HK_NULL;
			command->m_rayInput.m_filterInfo			   = 0;
		}

		// Init output struct.
		{
			command->m_results		   = rayOutput;
			command->m_resultsCapacity = 1;
			command->m_numResultsOut   = 0;
		}

	}

	//
	// Create the job header.
	//
	hkpCollisionQueryJobHeader* jobHeader;
	{
		jobHeader = hkAllocateChunk<hkpCollisionQueryJobHeader>(1, HK_MEMORY_CLASS_DEMO);
	}

	//
	// Setup the raycast job.
	//
	hkpShapeRayCastJob shapeRayCastJob(jobHeader, command, 1, &m_semaphore);

	//
	// Put the job on the queue, kick-off the PPU/SPU threads and wait for everything to finish.
	//
	{
		//
		// Put the raycast on the job queue.
		//
		m_util->putJobOntoQueue(&shapeRayCastJob, shapeRayCastJob.getJobType());

		//
		// Start PPU worker threads and/or SPURS threads.
		//
		if		( variant.m_demoType == ShapeRayCastMultithreadingApiDemoVariant::MULTITHREADED_ON_PPU		   ) m_util->startPpu(m_multithreadingUtil);
		else if ( variant.m_demoType == ShapeRayCastMultithreadingApiDemoVariant::MULTITHREADED_ON_SPU		   ) m_util->startPpuAndSpu(m_multithreadingUtil); // hacky workaround: we only need to start PPU here to reset the timers
		else if ( variant.m_demoType == ShapeRayCastMultithreadingApiDemoVariant::MULTITHREADED_ON_PPU_AND_SPU ) m_util->startPpuAndSpu(m_multithreadingUtil);

		//
		// Wait for the one single job we started to finish.
		//
		m_semaphore.acquire();

		//
		// Wait until all SPUSs have finished properly.
		//
		if		( variant.m_demoType == ShapeRayCastMultithreadingApiDemoVariant::MULTITHREADED_ON_SPU		   ) m_util->waitForSpuFinished();
		else if ( variant.m_demoType == ShapeRayCastMultithreadingApiDemoVariant::MULTITHREADED_ON_PPU_AND_SPU ) m_util->waitForSpuFinished();
	}

	//
	// Output results.
	//
	{
		// Display results (just one in this case).
		if ( command->m_numResultsOut > 0 )
		{
			hkpShapeRayCastOutput* output = &command->m_results[0];

			hkVector4 intersectionPointWorld;
			intersectionPointWorld.setInterpolate4(command->m_rayInput.m_from, command->m_rayInput.m_to, output->m_hitFraction );

			// Display hitting ray in RED.
			HK_DISPLAY_LINE(command->m_rayInput.m_from, intersectionPointWorld, hkColor::RED);

			// Display a small RED cross at the point of intersection.
			{
				hkVector4 p = intersectionPointWorld;
				hkVector4 q = intersectionPointWorld;
				p(0) -= 0.2f;
				q(0) += 0.2f;		
				HK_DISPLAY_LINE(p, q, hkColor::RED);
				p = intersectionPointWorld;
				q = intersectionPointWorld;
				p(1) -= 0.2f;
				q(1) += 0.2f;	
				HK_DISPLAY_LINE(p, q, hkColor::RED);
			}

			// Display hit normal.
			HK_DISPLAY_ARROW( intersectionPointWorld, output->m_normal, hkColor::CYAN );
		}
		else
		{
			// Draw infinite ray as GREY.
			HK_DISPLAY_LINE(command->m_rayInput.m_from, command->m_rayInput.m_to, hkColor::rgbFromChars(200, 200, 200));
		}
	}

	//
	// Free temporarily allocated memory.
	//
	hkDeallocateChunk(jobHeader, 1, HK_MEMORY_CLASS_DEMO);
	hkDeallocateChunk(command,   1, HK_MEMORY_CLASS_DEMO);
	hkDeallocateChunk(rayOutput, 1, HK_MEMORY_CLASS_DEMO);
	hkAlignedDeallocate(collidableArray);

	// Do not clear the timers during stepDemo()'s stepWorld() call as this would reset our query timings
	m_clearTimersOnStepWorld = false;

	return hkDefaultPhysicsDemo::stepDemo();
}


#if defined(HK_COMPILER_MWERKS)
#	pragma force_active on
#	pragma fullpath_file on
#endif


HK_DECLARE_DEMO_VARIANT_USING_STRUCT( ShapeRayCastMultithreadingApiDemo, OTHER, ShapeRayCastMultithreadingApiDemoVariant, g_ShapeRayCastMultithreadingApiDemoVariants, HK_NULL );

/*
* 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
*
*/
