File size: 7,137 Bytes
9897e20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#pragma once

#include <madrona/taskgraph_builder.hpp>
#include <madrona/custom_context.hpp>

#include "consts.hpp"
#include "types.hpp"
#include "init.hpp"
#include "rng.hpp"

namespace madrona_gpudrive {

class Engine;

// This enum is used by the Sim and Manager classes to track the export slots
// for each component exported to the training code.
enum class ExportID : uint32_t {
    Reset,
    Action,
    Reward,
    Done,
    SelfObservation,
    PartnerObservations,
    AgentMapObservations,
    Lidar,
    BevObservations,
    StepsRemaining,
    BicycleModel,
    MapObservation,
    Shape,
    ControlledState,
    AbsoluteSelfObservation,
    ValidState,
    Info,
    ResponseType,
    Trajectory,
    Map,
    ResetMap,
    WorldMeans,
    MetaData,
    DeletedAgents,
    MapName,
    ScenarioId,
    NumExports
};

// Stores values for the ObjectID component that links entities to
// render / physics assets.
enum class SimObject : uint32_t {
    Cube,
    Agent,
    StopSign,
    SpeedBump,
    Plane,
    NumObjects,
};

enum class TaskGraphID : uint32_t {
    Step,
    Reset,
    NumTaskGraphs,
};

// The Sim class encapsulates the per-world state of the simulation.
// Sim is always available by calling ctx.data() given a reference
// to the Engine / Context object that is passed to each ECS system.
//
// Per-World state that is frequently accessed but only used by a few
// ECS systems should be put in a singleton component rather than
// in this class in order to ensure efficient access patterns.
struct Sim : public madrona::WorldBase {
    struct Config {
        const madrona::render::RenderECSBridge *renderBridge;
        bool enableLidar = false;
    };

    // Sim::registerTypes is called during initialization
    // to register all components & archetypes with the ECS.
    static void registerTypes(madrona::ECSRegistry &registry,
                              const Config &cfg);

    // Sim::setupTasks is called during initialization to build
    // the system task graph that will be invoked by the 
    // Manager class (src/mgr.hpp) for each step.
    static void setupTasks(madrona::TaskGraphManager &taskgraph_mgr,
                           const Config &cfg);

   const std::pair<EntityType,EntityType> collisionPairs[20] = {                                                                                                 
                                                              {EntityType::Pedestrian, EntityType::RoadEdge},                                                                                                          
                                                              {EntityType::Pedestrian, EntityType::RoadLine},                                                                                                                
                                                              {EntityType::Pedestrian, EntityType::RoadLane},                                                                                                                
                                                              {EntityType::Pedestrian, EntityType::CrossWalk},                                                                                                               
                                                              {EntityType::Pedestrian, EntityType::SpeedBump},                                                                                                                
                                                              {EntityType::Cyclist, EntityType::RoadEdge},                                                                                                                   
                                                              {EntityType::Cyclist, EntityType::RoadLine},                                                                                                                   
                                                              {EntityType::Cyclist, EntityType::RoadLane},                                                                                                                   
                                                              {EntityType::Cyclist, EntityType::CrossWalk},                                                                                                                  
                                                              {EntityType::Cyclist, EntityType::SpeedBump},                                                                                                                 
                                                              {EntityType::Vehicle, EntityType::CrossWalk},                                                                                                                  
                                                              {EntityType::Vehicle, EntityType::SpeedBump},                                                                                                                  
                                                              {EntityType::Vehicle, EntityType::RoadLine},                                                                                                                   
                                                              {EntityType::Vehicle, EntityType::RoadLane}};                   

    // The constructor is called for each world during initialization.
    // Config is global across all worlds, while WorldInit (src/init.hpp)
    // can contain per-world initialization data, created in (src/mgr.cpp)
    Sim(Engine &ctx,
        const Config &cfg,
        const WorldInit &init);

    // EpisodeManager globally tracks episode IDs with an atomic across the
    // simulation.
    EpisodeManager *episodeMgr;

    // Simple random number generator seeded with episode ID.
    RNG rng;

    // Floor plane entity, constant across all episodes.
    Entity floorPlane;

    // Border wall entities: 3 walls to the left, up and down that define
    // play area. These are constant across all episodes.
    Entity borders[3];

    // Agent entity references. This entities live across all episodes
    // and are just reset to the start of the level on reset.
    madrona::CountT numAgents;
    Entity agents[consts::kMaxAgentCount];
    madrona::CountT numRoads;
    Entity roads[consts::kMaxRoadEntityCount];

    Entity agent_ifaces[consts::kMaxAgentCount];
    Entity road_ifaces[consts::kMaxRoadEntityCount];

    Entity camera_agent;

    madrona::CountT numControlledAgents;

    Parameters params;

    // Episode ID number
    int32_t curEpisodeIdx;

    // Are we visualizing the simulation in the viewer?
    bool enableRender;
};

class Engine : public ::madrona::CustomContext<Engine, Sim> {
public:
    using CustomContext::CustomContext;

    // These are convenience helpers for creating renderable
    // entities when rendering isn't necessarily enabled
    template <typename ArchetypeT>
    inline madrona::Entity makeRenderableEntity();
    inline void destroyRenderableEntity(Entity e);
};

}

#include "sim.inl"