42

Reflection for Tools Development

  • Upload
    ayoka

  • View
    47

  • Download
    0

Embed Size (px)

DESCRIPTION

Reflection for Tools Development. Jeremy Walker. Senior Tools Programmer Ubisoft Vancouver. Table of Contents. Understanding the Costs of Tools Development The Ultimate Technique for Data Driven Solutions A Reflection-Powered Infrastructure for Tools. 1. The Problem. 2. Reflection. - PowerPoint PPT Presentation

Citation preview

Page 1: Reflection for Tools Development
Page 2: Reflection for Tools Development

Reflection for Tools Development

Senior Tools ProgrammerUbisoft Vancouver

Jeremy Walker

Page 3: Reflection for Tools Development

• Understanding the Costs of Tools Development

• The Ultimate Technique for Data Driven Solutions

• A Reflection-Powered Infrastructure for Tools

Table of Contents

1. The Problem

2. Reflection

3. Content Framework

Page 4: Reflection for Tools Development

1. The ProblemUnderstanding the Costs of Tools Development

Page 5: Reflection for Tools Development

Hard Coded vs. Data Driven Systems

Animation

Hard Coded Animation System

Animation

Animation Tools

Animation Build System

Animation Engine

Animation Serialization

Hard Coded System Data Driven System

Page 6: Reflection for Tools Development

What's the Cost of Great Workflows?

Scenes

Scene Editing Tools

Scene Build System

Scene Rendering Engine

Scene Serialization

Animation

Animation Tools

Animation Build System

Animation Engine

Animation Serialization

Sounds

Sound Editing Tools

Sound Build System

Sound Playback Engine

Sound Serialization

Page 7: Reflection for Tools Development

SHARED SHAREDSHAREDSHARED SHARED

SHAREDSHARED

SHAREDSHAREDSHARED

SHARED

SHAREDSHAREDSHAREDSHARED

SHAREDSHAREDSHARED

HUD

Animation Sounds

Cut Scenes

Input Front End Cameras Particles Characters

Textures AI Levels Models Shaders Materials Props

Skeletons Music Sequences State Flows Curves Videos Scripts

Scenes

Great Workflows for All Types of Content

Page 8: Reflection for Tools Development

SHARED SHAREDSHAREDSHARED SHARED

SHAREDSHARED

SHAREDSHAREDSHARED

SHARED

SHAREDSHAREDSHAREDSHARED

SHAREDSHAREDSHARED

HUD

Animation Sounds

Cut Scenes

Input Front End Cameras Particles Characters

Textures AI Levels Models Shaders Materials Props

Skeletons Music Sequences State Flows Curves Videos Scripts

Scenes

Hard Coded

Hard Coded

Hard Coded

Hard Coded

Hard Coded

Hard Coded

Hard Coded

Hard Coded

Hard Coded

Hard Coded

Hard Coded

The Keep-It-Simple Approach

Hard Coded

Page 9: Reflection for Tools Development

HUD

Animation Sounds

Cut Scenes

Input Front End Cameras Particles Characters

Textures AI Levels Models Shaders Materials Props

Skeletons Music Sequences State Flows Curves Videos Scripts

Scenes

The Monolithic Engine ApproachMonolithic Engine

Animation, Sounds, Scenes, Input, Front End, Cameras, Particles, Characters, Textures, AI, Levels, Models, Shaders, Materials, HUD, Props, Skeletons, Music, Sequences, State

Flows, Curves, Videos, Scripts, Cut Scenes

Animation, Sounds, Scenes, Input, Front End, Cameras, Particles, Characters, Textures, AI, Levels, Models, Shaders, Materials, HUD, Props, Skeletons, Music, Sequences, State

Flows, Curves, Videos, Scripts, Cut Scenes

Animation, Sounds, Scenes, Input, Front End, Cameras, Particles, Characters, Textures, AI, Levels, Models, Shaders, Materials, HUD, Props, Skeletons, Music, Sequences, State

Flows, Curves, Videos, Scripts, Cut Scenes

Animation, Sounds, Scenes, Input, Front End, Cameras, Particles, Characters, Textures, AI, Levels, Models, Shaders, Materials, HUD, Props, Skeletons, Music, Sequences, State

Flows, Curves, Videos, Scripts, Cut Scenes

Page 10: Reflection for Tools Development

The Problem

LowDevelopment

Cost

Stable Tools

ReusableSystems

Great Workflows

AdaptableTo Change

Game andGenre-Specific

Needs

?

Page 11: Reflection for Tools Development

For all types of content:• Minimize the cost of developing great workflows.• Design reusable systems while accommodating game-

specific needs.• Develop stable tools that adapt to constant change

during production.

The Solution

Page 12: Reflection for Tools Development

The Package Release Cycle

Source Code

Automated Build Process OK?

Release Candidate

yes

no

Testing Process OK?

yes

no

Release

Artist Gets Package Release

Programmer Modifies Code

Artist Requests New

Features

Page 13: Reflection for Tools Development

The Monolithic Package Release Problem

Monolithic Package Source

Code

Monolithic Package Release

Page 14: Reflection for Tools Development

Package Release

Package Release

Package Release

Package Release

Package Release

The Benefit of Decoupled PackagesPackage

Source Code

Package Source Code

Package Source Code

Package Source Code

Package Source Code

Page 15: Reflection for Tools Development

Animation Tool Release

Sound Tool Release

Game Release

The Lockstep Release Problem

Animation Tool Source Code

Sound Tool Source Code

Game Source Code

Compile TimeDependency

Compile TimeDependency

Lockstep Release Cycle

Run TimeDependency

Run TimeDependency

Page 16: Reflection for Tools Development

Decoupled Systems

HUD

Animation Sounds

Cameras Particles Characters

Materials Props

Scenes

Decoupled Systems

HUD

Animation Sounds

Cameras Particles Characters

Materials Props

Scenes

Reflection

Reducing Cost and Complexity

Hardcoded Engine

Hard Coded

Low CostSimple

Hard to ReusePoor Workflow

Monolithic Engine

Animation, Sounds, Scenes, Input, Front End, Cameras, Particles, Characters, Textures, AI, Levels, Models, Shaders, Materials, HUD, Props, Skeletons, Music,

Sequences, State Flows, Curves, Videos, Scripts, Cut Scenes

Animation, Sounds, Scenes, Input, Front End, Cameras, Particles, Characters, Textures, AI, Levels, Models, Shaders, Materials, HUD, Props, Skeletons, Music,

Sequences, State Flows, Curves, Videos, Scripts, Cut Scenes

Animation, Sounds, Scenes, Input, Front End, Cameras, Particles, Characters, Textures, AI, Levels, Models, Shaders, Materials, HUD, Props, Skeletons, Music,

Sequences, State Flows, Curves, Videos, Scripts, Cut Scenes

Animation, Sounds, Scenes, Input, Front End, Cameras, Particles, Characters, Textures, AI, Levels, Models, Shaders, Materials, HUD, Props, Skeletons, Music,

Sequences, State Flows, Curves, Videos, Scripts, Cut Scenes

Medium CostComplex

Reuse All or NothingGreat Workflow

High CostVery ComplexVery Reusable

Great Workflow

Low CostSimple

Very ReusableGreat Workflow

Page 17: Reflection for Tools Development

2. ReflectionThe Ultimate Technique

for Data Driven Solutions

Page 18: Reflection for Tools Development

From Wikipedia: “…Reflection is the process by which a computer program can observe and modify its own structure and behaviour.”

What is Reflection?

Page 19: Reflection for Tools Development

Reflection is Used Everywhere

Page 20: Reflection for Tools Development

Case Study: C++ Compilation

C++Compiler

Function Bcalls

Function A

ExposeDiscover

Validate Implement

Reflection

Function Declaration

Binding to Function

Definition

Function AC++Compiler

Page 21: Reflection for Tools Development

Reflection for Language Interoperability

Visual BasicCompilerC# Code

ExposeDiscover

Validate Implement

Reflection

Type Definitions

Type Binding

Visual Basic Code

C#Compiler

Page 22: Reflection for Tools Development

Reflection for Scripting in Games

C++ AdapterLua Script

ExposeDiscover

Validate Implement

Type Definitions

Type Binding

C++ ClassesScript Adapter

Reflection

Page 23: Reflection for Tools Development

Reflection for Serialization in Games

C++ Adapter

Serialized Objects

ExposeDiscover

Validate Implement

Type Definitions

Type Binding

C++ ClassesSerializer Reflection

Page 24: Reflection for Tools Development

Common Language Specification

Type Definitions Type BindingReflection

• Type size and alignment• Native types• Classes• Enums

• Allocate objects• Binary representation• Invoke constructor, get/invoke members• Get/set enum value

Page 25: Reflection for Tools Development

const Type* type = TypeOf<SimpleVehicle>();const char* name = type->Name();size_t size = type->Size();printf(“Type: %s\n Size: %d\n”, name, size);

Instance vehicle = type->CreateInstance();vehicle.As<SimpleVehicle&>().m_MaxSpeedKPH = 193.12f;for (int i=0; i<type->NumMembers(); i++){

const Member* member = vehicle.GetMember(i);if (member->IsField()){

const Field* field = (Field*)member;if (field->GetType() == TypeOf<float>()){

Instance value = vehicle.GetField(field);printf(“Field: %s\n Offset: %d\n Value: %.2f\n”,

field->Name(), field->Offset(), value.As<float>());}

}}

Example: Print all fields of type “float”

Reflection in Action

Type: SimpleVehicle Size: 4Field: MaxSpeedKPH Offset 0 Value: 193.12

Output

Page 26: Reflection for Tools Development

• Macros• Code Parser• Type Definition Language

Popular Approaches to C++ Reflection

Page 27: Reflection for Tools Development

Reflection with Macros

class SimpleVehicle : public Entity{public:

DECLARE_TYPE(); float m_MaxSpeedKPH; void Reset(bool useDefaults);

float GetMaxSpeedMPH() const;void SetMaxSpeedMPH(float maxSpeedMPH);

};

//In a separate .CPP file:DEFINE_TYPE(SimpleVehicle)

BASE_CLASS(Entity)FIELD(“MaxSpeedKPH”, m_MaxSpeedKPH)METHOD(Reset)PROPERTY(GetMaxSpeedMPH, SetMaxSpeedMPH)

DEFINE_TYPE_END()

Macros Example Pros• No external toolsCons• Awkward to implement• Hard to debug• Run-time discovery

Page 28: Reflection for Tools Development

Reflection with Code Parser

/// [Class]class SimpleVehicle : public Entity{public:

/// [Field(“MaxSpeedKPH”)]float m_MaxSpeedKPH;/// [Method]void Reset(bool useDefaults);/// [Property]float GetMaxSpeedMPH() const;/// [Property]void SetMaxSpeedMPH(float maxSpeedMPH);

};

Code Parser Example Pros• Easier to implement• Compile-time discoveryCons• Slow pre-build step

Page 29: Reflection for Tools Development

Reflection with Type Definition Language

class SimpleVehicle : Entity{

float MaxSpeedKPH;void Reset(bool useDefaults);float MaxSpeedMPH { get; set; }

};

Type Definition Language Example Pros• Easiest to implement• No slow pre-buildCons• Can’t reflect existing

classes

Page 30: Reflection for Tools Development

/// [Class]class SimpleVehicle : public Entity{public:

/// [Field(“MaxSpeedKPH”)]float m_MaxSpeedKPH;/// [Method]void Reset(bool useDefaults);/// [Property]float GetMaxSpeedMPH() const;/// [Property]void SetMaxSpeedMPH(float maxSpeedMPH);

};

C++ Class<type name=“SimpleVehicle”> <field name=“MaxSpeedKPH” type=“float”/> <method name=“Reset” returntype=“void”> <parameter name=“useDefaults” type=“bool”/> </method> <property name=“MaxSpeedMPH” type=“float” hasget=“true” hasset=“true”/></type>

Types.xml

Exporting Type Definitions for Game

C++ Reflected Types

(game-side)Code Parser

Types.xml

Generated Binder Code

Reflection System(game-side)

Page 31: Reflection for Tools Development

<type name=“SimpleVehicle”> <field name=“MaxSpeedKPH” type=“float”/> <method name=“Reset” returntype=“void”> <parameter name=“useDefaults” type=“bool”/> </method> <property name=“MaxSpeedMPH” type=“float” hasget=“true” hasset=“true”/></type>

Types.xml

Exporting Type Definitions for Tools

C++ Reflected Types

(game-side)Code Parser Types.xml Reflection System

(tool-side)C# Proxy

Generator

[ProxyType(“SimpleVehicle”, 0x81c37132)]public partial class SimpleVehicle : Entity{ public float MaxSpeedKPH { get { return this.Instance.GetField(“MaxSpeedKPH”).Get<float>(); } set { this.Instance.GetField(“MaxSpeedKPH”).Set<float>(value); } } } public float MaxSpeedMPH { ... } public void Reset(bool useDefaults) { ... }}

C# Proxy Class

Page 32: Reflection for Tools Development

Primary Uses for Reflection in Tools

Types.xml

Reflection SystemSerialization

Generated UI(PropertyGrid, TreeView, etc.)

Client-ServerRemoting

Page 33: Reflection for Tools Development

Client-Server Remoting

RemotingClient

Generated C# Proxy Classes

ExposeDiscover

Validate Implement

Reflection

Type Definitions

Type Binding

C# Proxy Generator C++ Adapter

ExposeDiscover

Validate Implement

Reflection

Type Definitions

Type Binding

C++ Game Classes

RemotingServer

Tools (Client) Game (Server)

Page 34: Reflection for Tools Development

Type definitions out of sync• Detect type checksum mismatch• Detect problems early• Auto-synchronization of type information• Auto-migration of data

Problems and Workarounds

Page 35: Reflection for Tools Development

Tools tightly coupled to the game• Avoid overuse of generated proxy classes• Use generated UI where possible• Use polymorphic proxy classes

Problems and Workarounds

Tools Code Base Class

Derived Class A

Derived Class B

Derived Class C

has proxy

has no proxy

Page 36: Reflection for Tools Development

Excessive memory usage• Strip type information based on usage • Auto-detect unused reflected types

Problems and Workarounds

Page 37: Reflection for Tools Development

• Marshalling events for multi-processor architectures• Client-Server Remoting for Online• Serialization of saved game data

Other Uses for Reflection

Page 38: Reflection for Tools Development

3. Content FrameworkA Reflection-Powered Infrastructure for Tools

Page 39: Reflection for Tools Development

Content Framework

Build System

Asset Repository

Asset Manager

Asset ExplorerPackage System

Content Framework

Reflection

Tool-side

Game-side

Asset Previewing

3rd Party

Page 40: Reflection for Tools Development

(Demo Videos)

Page 41: Reflection for Tools Development

• Fast tools development• Great workflows for all types of content• Decoupled systems with improved reusability and

resilience to change

Promising Results

Page 42: Reflection for Tools Development

Questions?