Switch between task schedulers in multi-threaded demo

This commit is contained in:
AndresTraks 2017-06-16 21:57:54 +03:00
Родитель 015477cc4b
Коммит 71807e4f11
7 изменённых файлов: 122 добавлений и 100 удалений

Просмотреть файл

@ -18,8 +18,8 @@ namespace BasicDemo
{
public ISimulation CreateSimulation(Demo demo)
{
demo.FreeLook.Eye = new Vector3(30, 20, 15);
demo.FreeLook.Target = new Vector3(0, 3, 0);
demo.FreeLook.Eye = new Vector3(30, 20, 15) * BasicDemoSimulation.Scale;
demo.FreeLook.Target = new Vector3(0, 3, 0) * BasicDemoSimulation.Scale;
demo.Graphics.WindowTitle = "BulletSharp - Basic Demo";
return new BasicDemoSimulation();
}
@ -27,6 +27,7 @@ namespace BasicDemo
internal sealed class BasicDemoSimulation : ISimulation
{
public const float Scale = 0.5f;
private const int NumBoxesX = 5, NumBoxesY = 5, NumBoxesZ = 5;
private Vector3 _startPosition = new Vector3(0, 2, 0);
@ -53,8 +54,8 @@ namespace BasicDemo
private void CreateGround()
{
var groundShape = new BoxShape(50, 1, 50);
//var groundShape = new StaticPlaneShape(Vector3.UnitY, 1);
var groundShape = new BoxShape(Scale * new Vector3(50, 1, 50));
//var groundShape = new StaticPlaneShape(Vector3.UnitY, Scale);
CollisionObject ground = PhysicsHelper.CreateStaticBody(Matrix.Identity, groundShape, World);
ground.UserObject = "Ground";
@ -63,7 +64,7 @@ namespace BasicDemo
private void CreateBoxes()
{
const float mass = 1.0f;
var shape = new BoxShape(1);
var shape = new BoxShape(Scale);
Vector3 localInertia = shape.CalculateLocalInertia(mass);
var bodyInfo = new RigidBodyConstructionInfo(mass, null, shape, localInertia);
@ -73,10 +74,10 @@ namespace BasicDemo
{
for (int z = 0; z < NumBoxesZ; z++)
{
Vector3 position = _startPosition + 2 * new Vector3(x, y, z);
Vector3 position = _startPosition + Scale * 2 * new Vector3(x, y, z);
// make it drop from a height
position += new Vector3(0, 10, 0);
position += new Vector3(0, Scale * 10, 0);
// using MotionState is recommended, it provides interpolation capabilities
// and only synchronizes 'active' objects

Просмотреть файл

@ -6,23 +6,14 @@ namespace DemoFramework
{
public sealed class Input
{
List<Keys> _keysPressed;
List<Keys> _keysReleased;
List<Keys> _keysDown;
MouseButtons _mousePressed;
MouseButtons _mouseReleased;
MouseButtons _mouseDown;
Point _mousePoint;
int _mouseWheelDelta;
public List<Keys> KeysPressed { get { return _keysPressed; } }
public List<Keys> KeysReleased { get { return _keysReleased; } }
public List<Keys> KeysDown { get { return _keysDown; } }
public MouseButtons MousePressed { get { return _mousePressed; } }
public MouseButtons MouseReleased { get { return _mouseReleased; } }
public MouseButtons MouseDown { get { return _mouseDown; } }
public Point MousePoint { get { return _mousePoint; } }
public int MouseWheelDelta { get { return _mouseWheelDelta; } }
public List<Keys> KeysPressed { get; } = new List<Keys>();
public List<Keys> KeysReleased { get; } = new List<Keys>();
public List<Keys> KeysDown { get; } = new List<Keys>();
public MouseButtons MousePressed { get; private set; }
public MouseButtons MouseReleased { get; private set; }
public MouseButtons MouseDown { get; private set; }
public Point MousePoint { get; private set; }
public int MouseWheelDelta { get; private set; }
Control _control;
public Control Control
@ -45,11 +36,8 @@ namespace DemoFramework
{
Control = control;
_keysDown = new List<Keys>();
_keysPressed = new List<Keys>();
_keysReleased = new List<Keys>();
_mousePoint = control.PointToClient(Cursor.Position);
_mouseWheelDelta = 0;
MousePoint = control.PointToClient(Cursor.Position);
MouseWheelDelta = 0;
}
public void Release()
@ -71,10 +59,10 @@ namespace DemoFramework
{
Keys key = e.KeyData & ~Keys.Shift;
if (_keysDown.Contains(key) == false)
if (KeysDown.Contains(key) == false)
{
_keysPressed.Add(key);
_keysDown.Add(key);
KeysPressed.Add(key);
KeysDown.Add(key);
}
}
@ -82,28 +70,28 @@ namespace DemoFramework
{
Keys key = e.KeyData & ~Keys.Shift;
_keysReleased.Add(key);
_keysDown.Remove(key);
KeysReleased.Add(key);
KeysDown.Remove(key);
}
public void ClearKeyCache()
{
_keysPressed.Clear();
_keysReleased.Clear();
_mousePressed = MouseButtons.None;
_mouseReleased = MouseButtons.None;
_mouseWheelDelta = 0;
KeysPressed.Clear();
KeysReleased.Clear();
MousePressed = MouseButtons.None;
MouseReleased = MouseButtons.None;
MouseWheelDelta = 0;
}
void ControlOnMouseDown(object sender, MouseEventArgs e)
{
_mousePoint = e.Location;
MousePoint = e.Location;
if (e.Button == MouseButtons.None)
return;
// Don't consider mouse clicks outside of the client area
if (_control.ClientRectangle.Contains(_mousePoint) == false)
if (_control.ClientRectangle.Contains(MousePoint) == false)
return;
//if (_control.Focused == false && _control.CanFocus)
@ -111,45 +99,45 @@ namespace DemoFramework
if (e.Button == MouseButtons.Left)
{
if ((_mouseDown & MouseButtons.Left) != MouseButtons.Left)
if ((MouseDown & MouseButtons.Left) != MouseButtons.Left)
{
_mouseDown |= MouseButtons.Left;
_mousePressed |= MouseButtons.Left;
MouseDown |= MouseButtons.Left;
MousePressed |= MouseButtons.Left;
}
}
else if (e.Button == MouseButtons.Right)
{
if ((_mouseDown & MouseButtons.Right) != MouseButtons.Right)
if ((MouseDown & MouseButtons.Right) != MouseButtons.Right)
{
_mouseDown |= MouseButtons.Right;
_mousePressed |= MouseButtons.Right;
MouseDown |= MouseButtons.Right;
MousePressed |= MouseButtons.Right;
}
}
}
void ControlOnMouseMove(object sender, MouseEventArgs e)
{
_mousePoint = e.Location;
MousePoint = e.Location;
}
void ControlOnMouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
_mouseDown &= ~MouseButtons.Left;
_mouseReleased |= MouseButtons.Left;
MouseDown &= ~MouseButtons.Left;
MouseReleased |= MouseButtons.Left;
}
if (e.Button == MouseButtons.Right)
{
_mouseDown &= ~MouseButtons.Right;
_mouseReleased |= MouseButtons.Right;
MouseDown &= ~MouseButtons.Right;
MouseReleased |= MouseButtons.Right;
}
}
void ControlOnMouseWheel(object sender, MouseEventArgs e)
{
_mouseWheelDelta = e.Delta;
MouseWheelDelta = e.Delta;
}
}
}

Просмотреть файл

@ -82,36 +82,36 @@
<Reference Include="BulletSharp">
<HintPath>..\..\..\..\bin\Release\BulletSharp.dll</HintPath>
</Reference>
<Reference Include="SharpDX, Version=3.1.1.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\SharpDX.3.1.1\lib\net45\SharpDX.dll</HintPath>
<Reference Include="SharpDX, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\SharpDX.4.0.1\lib\net45\SharpDX.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="SharpDX.D3DCompiler, Version=3.1.1.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\SharpDX.D3DCompiler.3.1.1\lib\net45\SharpDX.D3DCompiler.dll</HintPath>
<Reference Include="SharpDX.D3DCompiler, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\SharpDX.D3DCompiler.4.0.1\lib\net45\SharpDX.D3DCompiler.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="SharpDX.Desktop, Version=3.1.1.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\SharpDX.Desktop.3.1.1\lib\net45\SharpDX.Desktop.dll</HintPath>
<Reference Include="SharpDX.Desktop, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\SharpDX.Desktop.4.0.1\lib\net45\SharpDX.Desktop.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="SharpDX.Direct2D1, Version=3.1.1.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\SharpDX.Direct2D1.3.1.1\lib\net45\SharpDX.Direct2D1.dll</HintPath>
<Reference Include="SharpDX.Direct2D1, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\SharpDX.Direct2D1.4.0.1\lib\net45\SharpDX.Direct2D1.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="SharpDX.Direct3D11, Version=3.1.1.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\SharpDX.Direct3D11.3.1.1\lib\net45\SharpDX.Direct3D11.dll</HintPath>
<Reference Include="SharpDX.Direct3D11, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\SharpDX.Direct3D11.4.0.1\lib\net45\SharpDX.Direct3D11.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="SharpDX.Direct3D11.Effects, Version=3.1.1.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\SharpDX.Direct3D11.Effects.3.1.1\lib\net45\SharpDX.Direct3D11.Effects.dll</HintPath>
<Reference Include="SharpDX.Direct3D11.Effects, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\SharpDX.Direct3D11.Effects.4.0.1\lib\net45\SharpDX.Direct3D11.Effects.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="SharpDX.DXGI, Version=3.1.1.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\SharpDX.DXGI.3.1.1\lib\net45\SharpDX.DXGI.dll</HintPath>
<Reference Include="SharpDX.DXGI, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\SharpDX.DXGI.4.0.1\lib\net45\SharpDX.DXGI.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="SharpDX.Mathematics, Version=3.1.1.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\SharpDX.Mathematics.3.1.1\lib\net45\SharpDX.Mathematics.dll</HintPath>
<Reference Include="SharpDX.Mathematics, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\SharpDX.Mathematics.4.0.1\lib\net45\SharpDX.Mathematics.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />

Просмотреть файл

@ -63,7 +63,7 @@ float GetShadowTerm(float2 texCoord)
lightScreenPos = (lightScreenPos - float2(-1, 1)) * float2(0.5, -0.5); // from -1...1 to 0...1
float lightDepthSample = lightDepthMap.Sample(shadowSampler, lightScreenPos).x;
float shadowMul = ((lightDepthActual - lightDepthSample) > 0.00006) ? 0.8 : 1;
float shadowMul = ((lightDepthActual - lightDepthSample) > 0.000006) ? 0.8 : 1;
return shadowMul;
}

Просмотреть файл

@ -1,11 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="SharpDX" version="3.1.1" targetFramework="net461" />
<package id="SharpDX.D3DCompiler" version="3.1.1" targetFramework="net461" />
<package id="SharpDX.Desktop" version="3.1.1" targetFramework="net461" />
<package id="SharpDX.Direct2D1" version="3.1.1" targetFramework="net461" />
<package id="SharpDX.Direct3D11" version="3.1.1" targetFramework="net461" />
<package id="SharpDX.Direct3D11.Effects" version="3.1.1" targetFramework="net461" />
<package id="SharpDX.DXGI" version="3.1.1" targetFramework="net461" />
<package id="SharpDX.Mathematics" version="3.1.1" targetFramework="net461" />
<package id="SharpDX" version="4.0.1" targetFramework="net461" />
<package id="SharpDX.D3DCompiler" version="4.0.1" targetFramework="net461" />
<package id="SharpDX.Desktop" version="4.0.1" targetFramework="net461" />
<package id="SharpDX.Direct2D1" version="4.0.1" targetFramework="net461" />
<package id="SharpDX.Direct3D11" version="4.0.1" targetFramework="net461" />
<package id="SharpDX.Direct3D11.Effects" version="4.0.1" targetFramework="net461" />
<package id="SharpDX.DXGI" version="4.0.1" targetFramework="net461" />
<package id="SharpDX.Mathematics" version="4.0.1" targetFramework="net461" />
</packages>

Просмотреть файл

@ -2,6 +2,9 @@
using BulletSharp.Math;
using DemoFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace BasicDemo
{
@ -14,29 +17,49 @@ namespace BasicDemo
}
}
internal sealed class MultiThreadedDemo : IDemoConfiguration
internal sealed class MultiThreadedDemo : IDemoConfiguration, IUpdateReceiver
{
public ISimulation CreateSimulation(Demo demo)
{
demo.FreeLook.Eye = new Vector3(80, 50, -30);
demo.FreeLook.Target = new Vector3(0, 20, 0);
demo.FreeLook.Eye = new Vector3(80, 50, -30) * MultiThreadedDemoSimulation.Scale;
demo.FreeLook.Target = new Vector3(0, 20, 0) * MultiThreadedDemoSimulation.Scale;
var simulation = new MultiThreadedDemoSimulation();
var scheduler = Threads.TaskScheduler;
demo.Graphics.WindowTitle = $"BulletSharp - Multi-threaded Demo ({scheduler.Name}, {scheduler.NumThreads}/{scheduler.MaxNumThreads} threads)";
demo.Graphics.WindowTitle = "BulletSharp - Multi-threaded Demo";
SetDemoText(demo);
return simulation;
}
public void Update(Demo demo)
{
if (demo.Input.KeysPressed.Contains(Keys.T))
{
((MultiThreadedDemoSimulation)demo.Simulation).NextTaskScheduler();
SetDemoText(demo);
}
}
private void SetDemoText(Demo demo)
{
var scheduler = Threads.TaskScheduler;
demo.DemoText = $"T - Scheduler: {scheduler.Name}\n{scheduler.NumThreads}/{scheduler.MaxNumThreads} threads";
}
}
internal sealed class MultiThreadedDemoSimulation : ISimulation
{
public const float Scale = 0.5f;
private const int NumBoxesX = 5, NumBoxesY = 50, NumBoxesZ = 20;
private Vector3 _startPosition = new Vector3(0, 2, 0);
private const int MaxThreadCount = 64;
private ConstraintSolverPoolMultiThreaded _constraintSolver;
private List<TaskScheduler> _schedulers = new List<TaskScheduler>();
private int _currentScheduler = 0;
public MultiThreadedDemoSimulation()
{
SetTaskScheduler();
CreateSchedulers();
NextTaskScheduler();
using (var collisionConfigurationInfo = new DefaultCollisionConstructionInfo
{
@ -66,29 +89,38 @@ namespace BasicDemo
this.StandardCleanup();
}
private void SetTaskScheduler()
public void NextTaskScheduler()
{
TaskScheduler scheduler = Threads.GetOpenMPTaskScheduler();
if (scheduler == null)
_currentScheduler++;
if (_currentScheduler >= _schedulers.Count)
{
scheduler = Threads.GetTbbTaskScheduler();
}
if (scheduler == null)
{
scheduler = Threads.GetPplTaskScheduler();
}
if (scheduler == null)
{
scheduler = Threads.GetSequentialTaskScheduler();
_currentScheduler = 0;
}
TaskScheduler scheduler = _schedulers[_currentScheduler];
scheduler.NumThreads = scheduler.MaxNumThreads;
Threads.TaskScheduler = scheduler;
}
private void CreateSchedulers()
{
AddScheduler(Threads.GetSequentialTaskScheduler());
AddScheduler(Threads.GetOpenMPTaskScheduler());
AddScheduler(Threads.GetTbbTaskScheduler());
AddScheduler(Threads.GetPplTaskScheduler());
}
private void AddScheduler(TaskScheduler scheduler)
{
if (scheduler != null)
{
_schedulers.Add(scheduler);
}
}
private void CreateGround()
{
var groundShape = new BoxShape(100, 1, 100);
//var groundShape = new StaticPlaneShape(Vector3.UnitY, 1);
var groundShape = new BoxShape(Scale * new Vector3(100, 1, 100));
//var groundShape = new StaticPlaneShape(Vector3.UnitY, Scale);
CollisionObject ground = PhysicsHelper.CreateStaticBody(Matrix.Identity, groundShape, World);
ground.UserObject = "Ground";
@ -97,7 +129,7 @@ namespace BasicDemo
private void CreateBoxes()
{
const float mass = 1.0f;
var shape = new BoxShape(1);
var shape = new BoxShape(Scale);
Vector3 localInertia = shape.CalculateLocalInertia(mass);
var bodyInfo = new RigidBodyConstructionInfo(mass, null, shape, localInertia);
@ -107,10 +139,10 @@ namespace BasicDemo
{
for (int z = 0; z < NumBoxesZ; z++)
{
Vector3 position = _startPosition + 2 * new Vector3(x, y, z);
Vector3 position = _startPosition + Scale * 2 * new Vector3(x, y, z);
// make it drop from a height
position += new Vector3(0, 10, 0);
position += new Vector3(0, Scale * 10, 0);
// using MotionState is recommended, it provides interpolation capabilities
// and only synchronizes 'active' objects

Просмотреть файл

@ -62,6 +62,7 @@
<HintPath>..\..\bin\Release\BulletSharp.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Windows.Forms" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DemoFramework\DemoFramework.csproj">