-
Notifications
You must be signed in to change notification settings - Fork 7
/
sim.cpp
776 lines (650 loc) · 24.2 KB
/
sim.cpp
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
#include <madrona/mw_gpu_entry.hpp>
#include "sim.hpp"
#include "level_gen.hpp"
using namespace madrona;
using namespace madrona::math;
using namespace madrona::phys;
namespace RenderingSystem = madrona::render::RenderingSystem;
namespace madEscape {
// Register all the ECS components and archetypes that will be
// used in the simulation
void Sim::registerTypes(ECSRegistry ®istry, const Config &cfg)
{
base::registerTypes(registry);
phys::RigidBodyPhysicsSystem::registerTypes(registry);
RenderingSystem::registerTypes(registry, cfg.renderBridge);
registry.registerComponent<Action>();
registry.registerComponent<SelfObservation>();
registry.registerComponent<Reward>();
registry.registerComponent<Done>();
registry.registerComponent<GrabState>();
registry.registerComponent<Progress>();
registry.registerComponent<OtherAgents>();
registry.registerComponent<PartnerObservations>();
registry.registerComponent<RoomEntityObservations>();
registry.registerComponent<DoorObservation>();
registry.registerComponent<ButtonState>();
registry.registerComponent<OpenState>();
registry.registerComponent<DoorProperties>();
registry.registerComponent<Lidar>();
registry.registerComponent<StepsRemaining>();
registry.registerComponent<EntityType>();
registry.registerSingleton<WorldReset>();
registry.registerSingleton<LevelState>();
registry.registerArchetype<Agent>();
registry.registerArchetype<PhysicsEntity>();
registry.registerArchetype<DoorEntity>();
registry.registerArchetype<ButtonEntity>();
registry.exportSingleton<WorldReset>(
(uint32_t)ExportID::Reset);
registry.exportColumn<Agent, Action>(
(uint32_t)ExportID::Action);
registry.exportColumn<Agent, SelfObservation>(
(uint32_t)ExportID::SelfObservation);
registry.exportColumn<Agent, PartnerObservations>(
(uint32_t)ExportID::PartnerObservations);
registry.exportColumn<Agent, RoomEntityObservations>(
(uint32_t)ExportID::RoomEntityObservations);
registry.exportColumn<Agent, DoorObservation>(
(uint32_t)ExportID::DoorObservation);
registry.exportColumn<Agent, Lidar>(
(uint32_t)ExportID::Lidar);
registry.exportColumn<Agent, StepsRemaining>(
(uint32_t)ExportID::StepsRemaining);
registry.exportColumn<Agent, Reward>(
(uint32_t)ExportID::Reward);
registry.exportColumn<Agent, Done>(
(uint32_t)ExportID::Done);
}
static inline void cleanupWorld(Engine &ctx)
{
// Destroy current level entities
LevelState &level = ctx.singleton<LevelState>();
for (CountT i = 0; i < consts::numRooms; i++) {
Room &room = level.rooms[i];
for (CountT j = 0; j < consts::maxEntitiesPerRoom; j++) {
if (room.entities[j] != Entity::none()) {
ctx.destroyRenderableEntity(room.entities[j]);
}
}
ctx.destroyRenderableEntity(room.walls[0]);
ctx.destroyRenderableEntity(room.walls[1]);
ctx.destroyRenderableEntity(room.door);
}
}
static inline void initWorld(Engine &ctx)
{
phys::RigidBodyPhysicsSystem::reset(ctx);
// Assign a new episode ID
ctx.data().rng = RNG(rand::split_i(ctx.data().initRandKey,
ctx.data().curWorldEpisode++, (uint32_t)ctx.worldID().idx));
// Defined in src/level_gen.hpp / src/level_gen.cpp
generateWorld(ctx);
}
// This system runs each frame and checks if the current episode is complete
// or if code external to the application has forced a reset by writing to the
// WorldReset singleton.
//
// If a reset is needed, cleanup the existing world and generate a new one.
inline void resetSystem(Engine &ctx, WorldReset &reset)
{
int32_t should_reset = reset.reset;
if (ctx.data().autoReset) {
for (CountT i = 0; i < consts::numAgents; i++) {
Entity agent = ctx.data().agents[i];
Done done = ctx.get<Done>(agent);
if (done.v) {
should_reset = 1;
}
}
}
if (should_reset != 0) {
reset.reset = 0;
cleanupWorld(ctx);
initWorld(ctx);
}
}
// Translates discrete actions from the Action component to forces
// used by the physics simulation.
inline void movementSystem(Engine &,
Action &action,
Rotation &rot,
ExternalForce &external_force,
ExternalTorque &external_torque)
{
constexpr float move_max = 1000;
constexpr float turn_max = 320;
Quat cur_rot = rot;
float move_amount = action.moveAmount *
(move_max / (consts::numMoveAmountBuckets - 1));
constexpr float move_angle_per_bucket =
2.f * math::pi / float(consts::numMoveAngleBuckets);
float move_angle = float(action.moveAngle) * move_angle_per_bucket;
float f_x = move_amount * sinf(move_angle);
float f_y = move_amount * cosf(move_angle);
constexpr float turn_delta_per_bucket =
turn_max / (consts::numTurnBuckets / 2);
float t_z =
turn_delta_per_bucket * (action.rotate - consts::numTurnBuckets / 2);
external_force = cur_rot.rotateVec({ f_x, f_y, 0 });
external_torque = Vector3 { 0, 0, t_z };
}
// Implements the grab action by casting a short ray in front of the agent
// and creating a joint constraint if a grabbable entity is hit.
inline void grabSystem(Engine &ctx,
Entity e,
Position pos,
Rotation rot,
Action action,
GrabState &grab)
{
if (action.grab == 0) {
return;
}
// if a grab is currently in progress, triggering the grab action
// just releases the object
if (grab.constraintEntity != Entity::none()) {
ctx.destroyEntity(grab.constraintEntity);
grab.constraintEntity = Entity::none();
return;
}
// Get the per-world BVH singleton component
auto &bvh = ctx.singleton<broadphase::BVH>();
float hit_t;
Vector3 hit_normal;
Vector3 ray_o = pos + 0.5f * math::up;
Vector3 ray_d = rot.rotateVec(math::fwd);
Entity grab_entity =
bvh.traceRay(ray_o, ray_d, &hit_t, &hit_normal, 2.0f);
if (grab_entity == Entity::none()) {
return;
}
auto response_type = ctx.get<ResponseType>(grab_entity);
if (response_type != ResponseType::Dynamic) {
return;
}
auto entity_type = ctx.get<EntityType>(grab_entity);
if (entity_type == EntityType::Agent) {
return;
}
Entity constraint_entity = ctx.makeEntity<ConstraintData>();
grab.constraintEntity = constraint_entity;
Vector3 other_pos = ctx.get<Position>(grab_entity);
Quat other_rot = ctx.get<Rotation>(grab_entity);
Vector3 r1 = 1.25f * math::fwd + 0.5f * math::up;
Vector3 hit_pos = ray_o + ray_d * hit_t;
Vector3 r2 =
other_rot.inv().rotateVec(hit_pos - other_pos);
Quat attach1 = { 1, 0, 0, 0 };
Quat attach2 = (other_rot.inv() * rot).normalize();
float separation = hit_t - 1.25f;
ctx.get<JointConstraint>(constraint_entity) = JointConstraint::setupFixed(
e, grab_entity, attach1, attach2, r1, r2, separation);
}
// Animates the doors opening and closing based on OpenState
inline void setDoorPositionSystem(Engine &,
Position &pos,
OpenState &open_state)
{
if (open_state.isOpen) {
// Put underground
if (pos.z > -4.5f) {
pos.z += -consts::doorSpeed * consts::deltaT;
}
}
else if (pos.z < 0.0f) {
// Put back on surface
pos.z += consts::doorSpeed * consts::deltaT;
}
if (pos.z >= 0.0f) {
pos.z = 0.0f;
}
}
// Checks if there is an entity standing on the button and updates
// ButtonState if so.
inline void buttonSystem(Engine &ctx,
Position pos,
ButtonState &state)
{
AABB button_aabb {
.pMin = pos + Vector3 {
-consts::buttonWidth / 2.f,
-consts::buttonWidth / 2.f,
0.f,
},
.pMax = pos + Vector3 {
consts::buttonWidth / 2.f,
consts::buttonWidth / 2.f,
0.25f
},
};
bool button_pressed = false;
RigidBodyPhysicsSystem::findEntitiesWithinAABB(
ctx, button_aabb, [&](Entity) {
button_pressed = true;
});
state.isPressed = button_pressed;
}
// Check if all the buttons linked to the door are pressed and open if so.
// Optionally, close the door if the buttons aren't pressed.
inline void doorOpenSystem(Engine &ctx,
OpenState &open_state,
const DoorProperties &props)
{
bool all_pressed = true;
for (int32_t i = 0; i < props.numButtons; i++) {
Entity button = props.buttons[i];
all_pressed = all_pressed && ctx.get<ButtonState>(button).isPressed;
}
if (all_pressed) {
open_state.isOpen = true;
} else if (!props.isPersistent) {
open_state.isOpen = false;
}
}
// Make the agents easier to control by zeroing out their velocity
// after each step.
inline void agentZeroVelSystem(Engine &,
Velocity &vel,
Action &)
{
vel.linear.x = 0;
vel.linear.y = 0;
vel.linear.z = fminf(vel.linear.z, 0);
vel.angular = Vector3::zero();
}
static inline float distObs(float v)
{
return v / consts::worldLength;
}
static inline float globalPosObs(float v)
{
return v / consts::worldLength;
}
static inline float angleObs(float v)
{
return v / math::pi;
}
// Translate xy delta to polar observations for learning.
static inline PolarObservation xyToPolar(Vector3 v)
{
Vector2 xy { v.x, v.y };
float r = xy.length();
// Note that this is angle off y-forward
float theta = atan2f(xy.x, xy.y);
return PolarObservation {
.r = distObs(r),
.theta = angleObs(theta),
};
}
static inline float encodeType(EntityType type)
{
return (float)type / (float)EntityType::NumTypes;
}
static inline float computeZAngle(Quat q)
{
float siny_cosp = 2.f * (q.w * q.z + q.x * q.y);
float cosy_cosp = 1.f - 2.f * (q.y * q.y + q.z * q.z);
return atan2f(siny_cosp, cosy_cosp);
}
// This system packages all the egocentric observations together
// for the policy inputs.
inline void collectObservationsSystem(Engine &ctx,
Position pos,
Rotation rot,
const Progress &progress,
const GrabState &grab,
const OtherAgents &other_agents,
SelfObservation &self_obs,
PartnerObservations &partner_obs,
RoomEntityObservations &room_ent_obs,
DoorObservation &door_obs)
{
CountT cur_room_idx = CountT(pos.y / consts::roomLength);
cur_room_idx = std::max(CountT(0),
std::min(consts::numRooms - 1, cur_room_idx));
self_obs.roomX = pos.x / (consts::worldWidth / 2.f);
self_obs.roomY = (pos.y - cur_room_idx * consts::roomLength) /
consts::roomLength;
self_obs.globalX = globalPosObs(pos.x);
self_obs.globalY = globalPosObs(pos.y);
self_obs.globalZ = globalPosObs(pos.z);
self_obs.maxY = globalPosObs(progress.maxY);
self_obs.theta = angleObs(computeZAngle(rot));
self_obs.isGrabbing = grab.constraintEntity != Entity::none() ?
1.f : 0.f;
Quat to_view = rot.inv();
#pragma unroll
for (CountT i = 0; i < consts::numAgents - 1; i++) {
Entity other = other_agents.e[i];
Vector3 other_pos = ctx.get<Position>(other);
GrabState other_grab = ctx.get<GrabState>(other);
Vector3 to_other = other_pos - pos;
partner_obs.obs[i] = {
.polar = xyToPolar(to_view.rotateVec(to_other)),
.isGrabbing = other_grab.constraintEntity != Entity::none() ?
1.f : 0.f,
};
}
const LevelState &level = ctx.singleton<LevelState>();
const Room &room = level.rooms[cur_room_idx];
for (CountT i = 0; i < consts::maxEntitiesPerRoom; i++) {
Entity entity = room.entities[i];
EntityObservation ob;
if (entity == Entity::none()) {
ob.polar = { 0.f, 1.f };
ob.encodedType = encodeType(EntityType::None);
} else {
Vector3 entity_pos = ctx.get<Position>(entity);
EntityType entity_type = ctx.get<EntityType>(entity);
Vector3 to_entity = entity_pos - pos;
ob.polar = xyToPolar(to_view.rotateVec(to_entity));
ob.encodedType = encodeType(entity_type);
}
room_ent_obs.obs[i] = ob;
}
Entity cur_door = room.door;
Vector3 door_pos = ctx.get<Position>(cur_door);
OpenState door_open_state = ctx.get<OpenState>(cur_door);
door_obs.polar = xyToPolar(to_view.rotateVec(door_pos - pos));
door_obs.isOpen = door_open_state.isOpen ? 1.f : 0.f;
}
// Launches consts::numLidarSamples per agent.
// This system is specially optimized in the GPU version:
// a warp of threads is dispatched for each invocation of the system
// and each thread in the warp traces one lidar ray for the agent.
inline void lidarSystem(Engine &ctx,
Entity e,
Lidar &lidar)
{
Vector3 pos = ctx.get<Position>(e);
Quat rot = ctx.get<Rotation>(e);
auto &bvh = ctx.singleton<broadphase::BVH>();
Vector3 agent_fwd = rot.rotateVec(math::fwd);
Vector3 right = rot.rotateVec(math::right);
auto traceRay = [&](int32_t idx) {
float theta = 2.f * math::pi * (
float(idx) / float(consts::numLidarSamples)) + math::pi / 2.f;
float x = cosf(theta);
float y = sinf(theta);
Vector3 ray_dir = (x * right + y * agent_fwd).normalize();
float hit_t;
Vector3 hit_normal;
Entity hit_entity =
bvh.traceRay(pos + 0.5f * math::up, ray_dir, &hit_t,
&hit_normal, 200.f);
if (hit_entity == Entity::none()) {
lidar.samples[idx] = {
.depth = 0.f,
.encodedType = encodeType(EntityType::None),
};
} else {
EntityType entity_type = ctx.get<EntityType>(hit_entity);
lidar.samples[idx] = {
.depth = distObs(hit_t),
.encodedType = encodeType(entity_type),
};
}
};
// MADRONA_GPU_MODE guards GPU specific logic
#ifdef MADRONA_GPU_MODE
// Can use standard cuda variables like threadIdx for
// warp level programming
int32_t idx = threadIdx.x % 32;
if (idx < consts::numLidarSamples) {
traceRay(idx);
}
#else
for (CountT i = 0; i < consts::numLidarSamples; i++) {
traceRay(i);
}
#endif
}
// Computes reward for each agent and keeps track of the max distance achieved
// so far through the challenge. Continuous reward is provided for any new
// distance achieved.
inline void rewardSystem(Engine &,
Position pos,
Progress &progress,
Reward &out_reward)
{
// Just in case agents do something crazy, clamp total reward
float reward_pos = fminf(pos.y, consts::worldLength * 2);
float old_max_y = progress.maxY;
float new_progress = reward_pos - old_max_y;
float reward;
if (new_progress > 0) {
reward = new_progress * consts::rewardPerDist;
progress.maxY = reward_pos;
} else {
reward = consts::slackReward;
}
out_reward.v = reward;
}
// Each agent gets a small bonus to it's reward if the other agent has
// progressed a similar distance, to encourage them to cooperate.
// This system reads the values of the Progress component written by
// rewardSystem for other agents, so it must run after.
inline void bonusRewardSystem(Engine &ctx,
OtherAgents &others,
Progress &progress,
Reward &reward)
{
bool partners_close = true;
for (CountT i = 0; i < consts::numAgents - 1; i++) {
Entity other = others.e[i];
Progress other_progress = ctx.get<Progress>(other);
if (fabsf(other_progress.maxY - progress.maxY) > 2.f) {
partners_close = false;
}
}
if (partners_close && reward.v > 0.f) {
reward.v *= 1.25f;
}
}
// Keep track of the number of steps remaining in the episode and
// notify training that an episode has completed by
// setting done = 1 on the final step of the episode
inline void stepTrackerSystem(Engine &,
StepsRemaining &steps_remaining,
Done &done)
{
int32_t num_remaining = --steps_remaining.t;
if (num_remaining == consts::episodeLen - 1) {
done.v = 0;
} else if (num_remaining == 0) {
done.v = 1;
}
}
// Helper function for sorting nodes in the taskgraph.
// Sorting is only supported / required on the GPU backend,
// since the CPU backend currently keeps separate tables for each world.
// This will likely change in the future with sorting required for both
// environments
#ifdef MADRONA_GPU_MODE
template <typename ArchetypeT>
TaskGraph::NodeID queueSortByWorld(TaskGraph::Builder &builder,
Span<const TaskGraph::NodeID> deps)
{
auto sort_sys =
builder.addToGraph<SortArchetypeNode<ArchetypeT, WorldID>>(
deps);
auto post_sort_reset_tmp =
builder.addToGraph<ResetTmpAllocNode>({sort_sys});
return post_sort_reset_tmp;
}
#endif
// Build the task graph
void Sim::setupTasks(TaskGraphBuilder &builder, const Config &cfg)
{
// Turn policy actions into movement
auto move_sys = builder.addToGraph<ParallelForNode<Engine,
movementSystem,
Action,
Rotation,
ExternalForce,
ExternalTorque
>>({});
// Scripted door behavior
auto set_door_pos_sys = builder.addToGraph<ParallelForNode<Engine,
setDoorPositionSystem,
Position,
OpenState
>>({move_sys});
// Build BVH for broadphase / raycasting
auto broadphase_setup_sys =
phys::RigidBodyPhysicsSystem::setupBroadphaseTasks(builder,
{set_door_pos_sys});
// Grab action, post BVH build to allow raycasting
auto grab_sys = builder.addToGraph<ParallelForNode<Engine,
grabSystem,
Entity,
Position,
Rotation,
Action,
GrabState
>>({broadphase_setup_sys});
// Physics collision detection and solver
auto substep_sys = phys::RigidBodyPhysicsSystem::setupSubstepTasks(builder,
{grab_sys}, consts::numPhysicsSubsteps);
// Improve controllability of agents by setting their velocity to 0
// after physics is done.
auto agent_zero_vel = builder.addToGraph<ParallelForNode<Engine,
agentZeroVelSystem, Velocity, Action>>(
{substep_sys});
// Finalize physics subsystem work
auto phys_done = phys::RigidBodyPhysicsSystem::setupCleanupTasks(
builder, {agent_zero_vel});
// Check buttons
auto button_sys = builder.addToGraph<ParallelForNode<Engine,
buttonSystem,
Position,
ButtonState
>>({phys_done});
// Set door to start opening if button conditions are met
auto door_open_sys = builder.addToGraph<ParallelForNode<Engine,
doorOpenSystem,
OpenState,
DoorProperties
>>({button_sys});
// Compute initial reward now that physics has updated the world state
auto reward_sys = builder.addToGraph<ParallelForNode<Engine,
rewardSystem,
Position,
Progress,
Reward
>>({door_open_sys});
// Assign partner's reward
auto bonus_reward_sys = builder.addToGraph<ParallelForNode<Engine,
bonusRewardSystem,
OtherAgents,
Progress,
Reward
>>({reward_sys});
// Check if the episode is over
auto done_sys = builder.addToGraph<ParallelForNode<Engine,
stepTrackerSystem,
StepsRemaining,
Done
>>({bonus_reward_sys});
// Conditionally reset the world if the episode is over
auto reset_sys = builder.addToGraph<ParallelForNode<Engine,
resetSystem,
WorldReset
>>({done_sys});
auto clear_tmp = builder.addToGraph<ResetTmpAllocNode>({reset_sys});
(void)clear_tmp;
#ifdef MADRONA_GPU_MODE
// RecycleEntitiesNode is required on the GPU backend in order to reclaim
// deleted entity IDs.
auto recycle_sys = builder.addToGraph<RecycleEntitiesNode>({reset_sys});
(void)recycle_sys;
#endif
// This second BVH build is a limitation of the current taskgraph API.
// It's only necessary if the world was reset, but we don't have a way
// to conditionally queue taskgraph nodes yet.
auto post_reset_broadphase = phys::RigidBodyPhysicsSystem::setupBroadphaseTasks(
builder, {reset_sys});
// Finally, collect observations for the next step.
auto collect_obs = builder.addToGraph<ParallelForNode<Engine,
collectObservationsSystem,
Position,
Rotation,
Progress,
GrabState,
OtherAgents,
SelfObservation,
PartnerObservations,
RoomEntityObservations,
DoorObservation
>>({post_reset_broadphase});
// The lidar system
#ifdef MADRONA_GPU_MODE
// Note the use of CustomParallelForNode to create a taskgraph node
// that launches a warp of threads (32) for each invocation (1).
// The 32, 1 parameters could be changed to 32, 32 to create a system
// that cooperatively processes 32 entities within a warp.
auto lidar = builder.addToGraph<CustomParallelForNode<Engine,
lidarSystem, 32, 1,
#else
auto lidar = builder.addToGraph<ParallelForNode<Engine,
lidarSystem,
#endif
Entity,
Lidar
>>({post_reset_broadphase});
if (cfg.renderBridge) {
RenderingSystem::setupTasks(builder, {reset_sys});
}
#ifdef MADRONA_GPU_MODE
// Sort entities, this could be conditional on reset like the second
// BVH build above.
auto sort_agents = queueSortByWorld<Agent>(
builder, {lidar, collect_obs});
auto sort_phys_objects = queueSortByWorld<PhysicsEntity>(
builder, {sort_agents});
auto sort_buttons = queueSortByWorld<ButtonEntity>(
builder, {sort_phys_objects});
auto sort_walls = queueSortByWorld<DoorEntity>(
builder, {sort_buttons});
auto sort_constraints = queueSortByWorld<ConstraintData>(
builder, {sort_walls});
(void)sort_walls;
#else
(void)lidar;
(void)collect_obs;
#endif
}
Sim::Sim(Engine &ctx,
const Config &cfg,
const WorldInit &)
: WorldBase(ctx)
{
// Currently the physics system needs an upper bound on the number of
// entities that will be stored in the BVH. We plan to fix this in
// a future release.
constexpr CountT max_total_entities = consts::numAgents +
consts::numRooms * (consts::maxEntitiesPerRoom + 3) +
4; // side walls + floor
phys::RigidBodyPhysicsSystem::init(ctx, cfg.rigidBodyObjMgr,
consts::deltaT, consts::numPhysicsSubsteps, -9.8f * math::up,
max_total_entities, max_total_entities * max_total_entities / 2,
consts::numAgents);
initRandKey = cfg.initRandKey;
autoReset = cfg.autoReset;
enableRender = cfg.renderBridge != nullptr;
if (enableRender) {
RenderingSystem::init(ctx, cfg.renderBridge);
}
curWorldEpisode = 0;
// Creates agents, walls, etc.
createPersistentEntities(ctx);
// Generate initial world state
initWorld(ctx);
}
// This declaration is needed for the GPU backend in order to generate the
// CUDA kernel for world initialization, which needs to be specialized to the
// application's world data type (Sim) and config and initialization types.
// On the CPU it is a no-op.
MADRONA_BUILD_MWGPU_ENTRY(Engine, Sim, Sim::Config, Sim::WorldInit);
}