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

osl_services.cpp « osl « kernel « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ded3a68d6675574d7335044f0f9d93e154e12e3f (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
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
/*
 * Copyright 2011, Blender Foundation.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <string.h>

#include "mesh.h"
#include "object.h"
#include "scene.h"

#include "osl_services.h"
#include "osl_shader.h"

#include "util_foreach.h"
#include "util_string.h"

#include "kernel_compat_cpu.h"
#include "kernel_globals.h"
#include "kernel_object.h"
#include "kernel_triangle.h"

CCL_NAMESPACE_BEGIN

/* RenderServices implementation */

#define TO_MATRIX44(m) (*(OSL::Matrix44*)&(m))

/* static ustrings */
ustring OSLRenderServices::u_distance("distance");
ustring OSLRenderServices::u_index("index");
ustring OSLRenderServices::u_camera("camera");
ustring OSLRenderServices::u_screen("screen");
ustring OSLRenderServices::u_raster("raster");
ustring OSLRenderServices::u_ndc("NDC");
ustring OSLRenderServices::u_empty;

OSLRenderServices::OSLRenderServices()
{
	kernel_globals = NULL;
}

OSLRenderServices::~OSLRenderServices()
{
}

void OSLRenderServices::thread_init(KernelGlobals *kernel_globals_)
{
	kernel_globals = kernel_globals_;
}

bool OSLRenderServices::get_matrix(OSL::Matrix44 &result, OSL::TransformationPtr xform, float time)
{
	/* this is only used for shader and object space, we don't really have
	   a concept of shader space, so we just use object space for both. */
	if(xform) {
		KernelGlobals *kg = kernel_globals;
		const ShaderData *sd = (const ShaderData*)xform;
		int object = sd->object;

		if(object != ~0) {
			Transform tfm = object_fetch_transform(kg, object, OBJECT_TRANSFORM);
			tfm = transform_transpose(tfm);
			result = TO_MATRIX44(tfm);

			return true;
		}
	}

	return false;
}

bool OSLRenderServices::get_inverse_matrix(OSL::Matrix44 &result, OSL::TransformationPtr xform, float time)
{
	/* this is only used for shader and object space, we don't really have
	   a concept of shader space, so we just use object space for both. */
	if(xform) {
		KernelGlobals *kg = kernel_globals;
		const ShaderData *sd = (const ShaderData*)xform;
		int object = sd->object;

		if(object != ~0) {
			Transform tfm = object_fetch_transform(kg, object, OBJECT_INVERSE_TRANSFORM);
			tfm = transform_transpose(tfm);
			result = TO_MATRIX44(tfm);

			return true;
		}
	}

	return false;
}

bool OSLRenderServices::get_matrix(OSL::Matrix44 &result, ustring from, float time)
{
	KernelGlobals *kg = kernel_globals;

	if(from == u_ndc) {
		Transform tfm = transform_transpose(kernel_data.cam.ndctoworld);
		result = TO_MATRIX44(tfm);
		return true;
	}
	else if(from == u_raster) {
		Transform tfm = transform_transpose(kernel_data.cam.rastertoworld);
		result = TO_MATRIX44(tfm);
		return true;
	}
	else if(from == u_screen) {
		Transform tfm = transform_transpose(kernel_data.cam.screentoworld);
		result = TO_MATRIX44(tfm);
		return true;
	}
	else if(from == u_camera) {
		Transform tfm = transform_transpose(kernel_data.cam.cameratoworld);
		result = TO_MATRIX44(tfm);
		return true;
	}

	return false;
}

bool OSLRenderServices::get_inverse_matrix(OSL::Matrix44 &result, ustring to, float time)
{
	KernelGlobals *kg = kernel_globals;

	if(to == u_ndc) {
		Transform tfm = transform_transpose(kernel_data.cam.worldtondc);
		result = TO_MATRIX44(tfm);
		return true;
	}
	else if(to == u_raster) {
		Transform tfm = transform_transpose(kernel_data.cam.worldtoraster);
		result = TO_MATRIX44(tfm);
		return true;
	}
	else if(to == u_screen) {
		Transform tfm = transform_transpose(kernel_data.cam.worldtoscreen);
		result = TO_MATRIX44(tfm);
		return true;
	}
	else if(to == u_camera) {
		Transform tfm = transform_transpose(kernel_data.cam.worldtocamera);
		result = TO_MATRIX44(tfm);
		return true;
	}

	return false;
}

bool OSLRenderServices::get_array_attribute(void *renderstate, bool derivatives, 
	ustring object, TypeDesc type, ustring name,
	int index, void *val)
{
	return false;
}

static bool get_mesh_attribute(KernelGlobals *kg, const ShaderData *sd,
	const OSLGlobals::Attribute& attr, bool derivatives, void *val)
{
	if(attr.type == TypeDesc::TypeFloat) {
		float *fval = (float*)val;
		fval[0] = triangle_attribute_float(kg, sd, attr.elem, attr.offset,
			(derivatives)? &fval[1]: NULL, (derivatives)? &fval[2]: NULL);
	}
	else {
		float3 *fval = (float3*)val;
		fval[0] = triangle_attribute_float3(kg, sd, attr.elem, attr.offset,
			(derivatives)? &fval[1]: NULL, (derivatives)? &fval[2]: NULL);
	}

	return true;
}

static bool get_mesh_attribute_convert(KernelGlobals *kg, const ShaderData *sd,
	const OSLGlobals::Attribute& attr, const TypeDesc& type, bool derivatives, void *val)
{
	if(attr.type == TypeDesc::TypeFloat) {
		float tmp[3];
		float3 *fval = (float3*)val;

		get_mesh_attribute(kg, sd, attr, derivatives, tmp);

		fval[0] = make_float3(tmp[0], tmp[0], tmp[0]);
		if(derivatives) {
			fval[1] = make_float3(tmp[1], tmp[1], tmp[1]);
			fval[2] = make_float3(tmp[2], tmp[2], tmp[2]);
		}

		return true;
	}
	else if(attr.type == TypeDesc::TypePoint || attr.type == TypeDesc::TypeVector ||
		attr.type == TypeDesc::TypeNormal || attr.type == TypeDesc::TypeColor) {
		float3 tmp[3];
		float *fval = (float*)val;

		get_mesh_attribute(kg, sd, attr, derivatives, tmp);

		fval[0] = average(tmp[0]);
		if(derivatives) {
			fval[1] = average(tmp[1]);
			fval[2] = average(tmp[2]);
		}

		return true;
	}
	else
		return false;
}

static void get_object_attribute(const OSLGlobals::Attribute& attr, bool derivatives, void *val)
{
	size_t datasize = attr.value.datasize();

	memcpy(val, attr.value.data(), datasize);
	if(derivatives)
		memset((char*)val + datasize, 0, datasize*2);
}

bool OSLRenderServices::get_attribute(void *renderstate, bool derivatives, ustring object_name,
	TypeDesc type, ustring name, void *val)
{
	KernelGlobals *kg = kernel_globals;
	const ShaderData *sd = (const ShaderData*)renderstate;
	int object = sd->object;
	int tri = sd->prim;

	/* lookup of attribute on another object */
	if(object_name != u_empty) {
		OSLGlobals::ObjectNameMap::iterator it = kg->osl.object_name_map.find(object_name);

		if(it == kg->osl.object_name_map.end())
			return false;

		object = it->second;
		tri = ~0;
	}
	else if(object == ~0) {
		/* no background attributes supported */
		return false;
	}

	/* find attribute on object */
	OSLGlobals::AttributeMap& attribute_map = kg->osl.attribute_map[object];
	OSLGlobals::AttributeMap::iterator it = attribute_map.find(name);

	if(it == attribute_map.end())
		return false;

	/* type mistmatch? */
	const OSLGlobals::Attribute& attr = it->second;

	if(attr.elem != ATTR_ELEMENT_VALUE) {
		/* triangle and vertex attributes */
		if(tri != ~0) {
			if(attr.type == type || (attr.type == TypeDesc::TypeColor &&
				(type == TypeDesc::TypePoint || type == TypeDesc::TypeVector || type == TypeDesc::TypeNormal)))
				return get_mesh_attribute(kg, sd, attr, derivatives, val);
			else
				return get_mesh_attribute_convert(kg, sd, attr, type, derivatives, val);
		}
	}
	else {
		/* object attribute */
		get_object_attribute(attr, derivatives, val);
		return true;
	}

	return false;
}

bool OSLRenderServices::get_userdata(bool derivatives, ustring name, TypeDesc type, 
	void *renderstate, void *val)
{
	return false; /* disabled by lockgeom */
}

bool OSLRenderServices::has_userdata(ustring name, TypeDesc type, void *renderstate)
{
	return false; /* never called by OSL */
}

void *OSLRenderServices::get_pointcloud_attr_query(ustring *attr_names,
	TypeDesc *attr_types, int nattrs)
{
#ifdef WITH_PARTIO
	m_attr_queries.push_back(AttrQuery());
	AttrQuery &query = m_attr_queries.back();

	/* make space for what we need. the only reason to use
	   std::vector is to skip the delete */
	query.attr_names.resize(nattrs);
	query.attr_partio_types.resize(nattrs);
	/* capacity will keep the length of the smallest array passed
	   to the query. Just to prevent buffer overruns */
	query.capacity = -1;

	for(int i = 0; i < nattrs; ++i)
	{
		query.attr_names[i] = attr_names[i];

		TypeDesc element_type = attr_types[i].elementtype ();

		if(query.capacity < 0)
		   query.capacity = attr_types[i].numelements();
		else
		   query.capacity = min(query.capacity, (int)attr_types[i].numelements());

		/* convert the OSL (OIIO) type to the equivalent Partio type so
		   we can do a fast check at query time. */
		if(element_type == TypeDesc::TypeFloat)
		   query.attr_partio_types[i] = Partio::FLOAT;
		else if(element_type == TypeDesc::TypeInt)
		   query.attr_partio_types[i] = Partio::INT;
		else if(element_type == TypeDesc::TypeColor  || element_type == TypeDesc::TypePoint ||
				 element_type == TypeDesc::TypeVector || element_type == TypeDesc::TypeNormal)
		   query.attr_partio_types[i] = Partio::VECTOR;
		else
			return NULL; /* report some error of unknown type */
	}

	/* this is valid until the end of RenderServices */
	return &query;
#else
	return NULL;
#endif
}

#ifdef WITH_PARTIO
Partio::ParticlesData *OSLRenderServices::get_pointcloud(ustring filename)
{
	return Partio::readCached(filename.c_str(), true);
}

#endif

int OSLRenderServices::pointcloud(ustring filename, const OSL::Vec3 &center, float radius,
	int max_points, void *_attr_query, void **attr_outdata)
{
	/* todo: this code has never been tested, and most likely does not
	   work. it's based on the example code in OSL */

#ifdef WITH_PARTIO
	/* query Partio for this pointcloud lookup using cached attr_query */
	if(!_attr_query)
		return 0;

	AttrQuery *attr_query = (AttrQuery *)_attr_query;
	if(attr_query->capacity < max_points)
		return 0;

	/* get the pointcloud entry for the given filename */
	Partio::ParticlesData *cloud = get_pointcloud(filename);

	/* now we have to look up all the attributes in the file. we can't do this
	   before hand cause we never know what we are going to load. */
	int nattrs = attr_query->attr_names.size();
	Partio::ParticleAttribute *attr = (Partio::ParticleAttribute *)alloca(sizeof(Partio::ParticleAttribute) * nattrs);

	for(int i = 0; i < nattrs; ++i) {
		/* special case attributes */
		if(attr_query->attr_names[i] == u_distance || attr_query->attr_names[i] == u_index)
			continue;

		/* lookup the attribute by name*/
		if(!cloud->attributeInfo(attr_query->attr_names[i].c_str(), attr[i])) {
			/* issue an error here and return, types don't match */
			Partio::endCachedAccess(cloud);
			cloud->release();
			return 0;
		}
	}

	std::vector<Partio::ParticleIndex> indices;
	std::vector<float> dist2;

	Partio::beginCachedAccess(cloud);

	/* finally, do the lookup */
	cloud->findNPoints((const float *)&center, max_points, radius, indices, dist2);
	int count = indices.size();

	/* retrieve the attributes directly to user space */
	for(int j = 0; j < nattrs; ++j) {
		/* special cases */
		if(attr_query->attr_names[j] == u_distance) {
			for(int i = 0; i < count; ++i)
				((float *)attr_outdata[j])[i] = sqrtf(dist2[i]);
		}
		else if(attr_query->attr_names[j] == u_index) {
			for(int i = 0; i < count; ++i)
				((int *)attr_outdata[j])[i] = indices[i];
		}
		else {
			/* note we make a single call per attribute, we don't loop over the
			   points. Partio does it, so it is there that we have to care about
			   performance */
			cloud->data(attr[j], count, &indices[0], true, attr_outdata[j]);
		}
	}

	Partio::endCachedAccess(cloud);
	cloud->release();

	return count;
#else
	return 0;
#endif
}

CCL_NAMESPACE_END