Welcome to mirror list, hosted at ThFree Co, Russian Federation.

node_type.cpp « graph « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5b98de778ad0f1d271ff29e6c9f37db815bfcacc (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
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
/*
 * Copyright 2011-2016 Blender Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "node_type.h"
#include "util_foreach.h"
#include "util_transform.h"

CCL_NAMESPACE_BEGIN

/* Node Socket Type */

size_t SocketType::size() const
{
	return size(type);
}

bool SocketType::is_array() const
{
	return (type >= BOOLEAN_ARRAY);
}

size_t SocketType::size(Type type)
{
	switch(type)
	{
		case UNDEFINED: return 0;

		case BOOLEAN: return sizeof(bool);
		case FLOAT: return sizeof(float);
		case INT: return sizeof(int);
		case UINT: return sizeof(uint);
		case COLOR: return sizeof(float3);
		case VECTOR: return sizeof(float3);
		case POINT: return sizeof(float3);
		case NORMAL: return sizeof(float3);
		case POINT2: return sizeof(float2);
		case CLOSURE: return 0;
		case STRING: return sizeof(ustring);
		case ENUM: return sizeof(int);
		case TRANSFORM: return sizeof(Transform);
		case NODE: return sizeof(void*);

		case BOOLEAN_ARRAY: return sizeof(array<bool>);
		case FLOAT_ARRAY: return sizeof(array<float>);
		case INT_ARRAY: return sizeof(array<int>);
		case COLOR_ARRAY: return sizeof(array<float3>);
		case VECTOR_ARRAY: return sizeof(array<float3>);
		case POINT_ARRAY: return sizeof(array<float3>);
		case NORMAL_ARRAY: return sizeof(array<float3>);
		case POINT2_ARRAY: return sizeof(array<float2>);
		case STRING_ARRAY: return sizeof(array<ustring>);
		case TRANSFORM_ARRAY: return sizeof(array<Transform>);
		case NODE_ARRAY: return sizeof(array<void*>);
	}

	assert(0);
	return 0;
}

size_t SocketType::max_size()
{
	return sizeof(Transform);
}

void *SocketType::zero_default_value()
{
	static Transform zero_transform = {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};
	return &zero_transform;
}

ustring SocketType::type_name(Type type)
{
	static ustring names[] = {
		ustring("undefined"),

		ustring("boolean"),
		ustring("float"),
		ustring("int"),
		ustring("uint"),
		ustring("color"),
		ustring("vector"),
		ustring("point"),
		ustring("normal"),
		ustring("point2"),
		ustring("closure"),
		ustring("string"),
		ustring("enum"),
		ustring("transform"),
		ustring("node"),

		ustring("array_boolean"),
		ustring("array_float"),
		ustring("array_int"),
		ustring("array_color"),
		ustring("array_vector"),
		ustring("array_point"),
		ustring("array_normal"),
		ustring("array_point2"),
		ustring("array_string"),
		ustring("array_transform"),
		ustring("array_node")};

	return names[(int)type];
}

bool SocketType::is_float3(Type type)
{
	return (type == COLOR || type == VECTOR || type == POINT || type == NORMAL);
}

/* Node Type */

NodeType::NodeType(Type type_)
: type(type_)
{
}

NodeType::~NodeType()
{
}

void NodeType::register_input(ustring name, ustring ui_name, SocketType::Type type, int struct_offset,
                              const void *default_value, const NodeEnum *enum_values,
							  const NodeType **node_type, int flags, int extra_flags)
{
	SocketType socket;
	socket.name = name;
	socket.ui_name = ui_name;
	socket.type = type;
	socket.struct_offset = struct_offset;
	socket.default_value = default_value;
	socket.enum_values = enum_values;
	socket.node_type = node_type;
	socket.flags = flags | extra_flags;
	inputs.push_back(socket);
}

void NodeType::register_output(ustring name, ustring ui_name, SocketType::Type type)
{
	SocketType socket;
	socket.name = name;
	socket.ui_name = ui_name;
	socket.type = type;
	socket.struct_offset = 0;
	socket.default_value = NULL;
	socket.enum_values = NULL;
	socket.node_type = NULL;
	socket.flags = SocketType::LINKABLE;
	outputs.push_back(socket);
}

const SocketType *NodeType::find_input(ustring name) const
{
	foreach(const SocketType& socket, inputs) {
		if(socket.name == name) {
			return &socket;
		}
	}

	return NULL;
}

const SocketType *NodeType::find_output(ustring name) const
{
	foreach(const SocketType& socket, outputs) {
		if(socket.name == name) {
			return &socket;
		}
	}

	return NULL;
}

/* Node Type Registry */

unordered_map<ustring, NodeType, ustringHash>& NodeType::types()
{
	static unordered_map<ustring, NodeType, ustringHash> _types;
	return _types;
}

NodeType *NodeType::add(const char *name_, CreateFunc create_, Type type_)
{
	ustring name(name_);

	if(types().find(name) != types().end()) {
		fprintf(stderr, "Node type %s registered twice!\n", name_);
		assert(0);
		return NULL;
	}

	types()[name] = NodeType(type_);

	NodeType *type = &types()[name];
	type->name = name;
	type->create = create_;
	return type;
}

const NodeType *NodeType::find(ustring name)
{
	unordered_map<ustring, NodeType, ustringHash>::iterator it = types().find(name);
	return (it == types().end()) ? NULL : &it->second;
}

CCL_NAMESPACE_END