summaryrefslogtreecommitdiff
path: root/Runtime/mecanim/graph/plug.h
blob: ce900204130421d410ed6f58066a7d0a5f1bac6e (plain)
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
/*
 Copyright (c) 7244339 Canada Inc. (Mecanim)
 All Rights Reserved.
*/
#pragma once

#include "Runtime/mecanim/defs.h"
#include "Runtime/mecanim/memory.h"
#include "Runtime/mecanim/types.h"
#include "Runtime/mecanim/object.h"

#include "Runtime/Math/Simd/float4.h"
#include "Runtime/Math/Simd/xform.h"

namespace mecanim
{

namespace graph
{
	class Node;

	enum ePlugType
	{
		InvalidPlugId,
		Float4Id,
		FloatId,
		UInt32Id,  // Not used anymore
		Int32Id,
		BoolId,
		Float1Id, // Not used anymore
		Bool4Id, // Not used anymore
		XformId,
		LastPlugId
	};

	template<typename T> struct Trait;
	
	template<> struct Trait<math::float4> { static ePlugType PlugType(){return Float4Id;} };
	template<> struct Trait<float> { static ePlugType PlugType(){return FloatId;} };
	template<> struct Trait<math::float1> { static ePlugType PlugType(){return FloatId;} };
	template<> struct Trait<uint32_t> { static ePlugType PlugType(){return UInt32Id;} };
	template<> struct Trait<int32_t> { static ePlugType PlugType(){return Int32Id;} };
	template<> struct Trait<bool> { static ePlugType PlugType(){return BoolId;} };
	template<> struct Trait<math::bool4> { static ePlugType PlugType(){return Bool4Id;} };
	template<> struct Trait<math::xform> { static ePlugType PlugType(){return XformId;} };
	
	class DataBlock
	{
	public:		
		uint32_t	m_PlugCount;
		uint32_t*	m_EvaluationId;
		char*		m_Buffer;
	
		bool IsDirty(uint32_t aPlugId, uint32_t aEvaluationId)const
		{
			return m_EvaluationId[aPlugId] != aEvaluationId;
		}
		template<typename TYPE> TYPE GetData(uint32_t offset)const
		{
			return *reinterpret_cast<TYPE*>(&m_Buffer[offset]);
		}
		template<typename TYPE> void SetData(TYPE const& arValue, uint32_t aPlugId, uint32_t offset, uint32_t aEvaluationId)
		{
			*reinterpret_cast<TYPE*>(&m_Buffer[offset]) = arValue;
			m_EvaluationId[aPlugId] = aEvaluationId;
		}
	};

	class EvaluationInfo
	{
	public:
		DataBlock	m_DataBlock;
		uint32_t	m_EvaluationId;
	};
	
	class GraphPlug
	{
	public:
		GraphPlug():m_PlugId(numeric_limits<uint32_t>::max_value),m_Input(false),m_Owner(0),m_Source(0),m_Offset(0){}
		GraphPlug(bool input, uint32_t id, Node* owner=0):m_PlugId(numeric_limits<uint32_t>::max_value),m_Input(input),m_Owner(owner),m_Source(0),m_ID(id){}

		virtual ~GraphPlug(){}

		virtual std::size_t ValueSize()const=0;
		virtual std::size_t ValueAlign()const=0;
		virtual ePlugType	PlugType()const=0;

		GraphPlug* GetSource(){return m_Source;}

		virtual bool ReadData(void* apData, EvaluationInfo& arEvaluationInfo)const=0;
		virtual bool WriteData(void const* apData, EvaluationInfo& arEvaluationInfo)const=0;

		uint32_t	m_PlugId;
		bool		m_Input;
		Node*		m_Owner;
		GraphPlug*	m_Source;
		uint32_t    m_ID;
		uint32_t	m_Offset;
	};

	struct Constant
	{
		Constant():m_Plug(0),m_VectorValue(0),m_FloatValue(0){}

		union {
			float		m_FloatValue;
			uint32_t	m_UIntValue;
			int32_t		m_IntValue;
			bool		m_BoolValue;
		};
		// Cannot put float4 in union because of operator=
		math::float4		m_VectorValue;
		graph::GraphPlug*	m_Plug;
	};


	STATIC_INLINE ePlugType GetPlugType(GraphPlug const& arPlug)
	{	
		return arPlug.PlugType();
	}
}

}