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: e79c509b1444371e4fe06016581f76c1f70e8cff (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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
/*
 * 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_closures.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_montecarlo.h"
#include "kernel_projection.h"
#include "kernel_differential.h"
#include "kernel_object.h"
#include "kernel_bvh.h"
#include "kernel_triangle.h"
#include "kernel_accumulate.h"
#include "kernel_shader.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) {
#ifdef __OBJECT_MOTION__
			Transform tfm = object_fetch_transform_motion_test(kg, object, time, NULL);
#else
			Transform tfm = object_fetch_transform(kg, object, OBJECT_TRANSFORM);
#endif
			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) {
#ifdef __OBJECT_MOTION__
			Transform itfm;
			object_fetch_transform_motion_test(kg, object, time, &itfm);
#else
			Transform itfm = object_fetch_transform(kg, object, OBJECT_INVERSE_TRANSFORM);
#endif
			itfm = transform_transpose(itfm);
			result = TO_MATRIX44(itfm);

			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(transform_quick_inverse(kernel_data.cam.worldtondc));
		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_matrix(OSL::Matrix44 &result, OSL::TransformationPtr xform)
{
	/* 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) {
		const ShaderData *sd = (const ShaderData *)xform;
		int object = sd->object;

		if (object != ~0) {
#ifdef __OBJECT_MOTION__
			Transform tfm = sd->ob_tfm;
#else
			KernelGlobals *kg = kernel_globals;
			Transform tfm = object_fetch_transform(kg, object, OBJECT_TRANSFORM);
#endif
			tfm = transform_transpose(tfm);
			result = TO_MATRIX44(tfm);

			return true;
		}
	}

	return false;
}

bool OSLRenderServices::get_inverse_matrix(OSL::Matrix44 &result, OSL::TransformationPtr xform)
{
	/* 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) {
		const ShaderData *sd = (const ShaderData *)xform;
		int object = sd->object;

		if (object != ~0) {
#ifdef __OBJECT_MOTION__
			Transform tfm = sd->ob_itfm;
#else
			KernelGlobals *kg = kernel_globals;
			Transform tfm = object_fetch_transform(kg, object, OBJECT_INVERSE_TRANSFORM);
#endif
			tfm = transform_transpose(tfm);
			result = TO_MATRIX44(tfm);

			return true;
		}
	}

	return false;
}

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

	if (from == u_ndc) {
		Transform tfm = transform_transpose(transform_quick_inverse(kernel_data.cam.worldtondc));
		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)
{
	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 set_attribute_float3(float3 f[3], TypeDesc type, bool derivatives, void *val)
{
	if (type == TypeDesc::TypePoint || type == TypeDesc::TypeVector ||
	    type == TypeDesc::TypeNormal || type == TypeDesc::TypeColor)
	{
		float *fval = (float *)val;

		fval[0] = f[0].x;
		fval[1] = f[0].y;
		fval[2] = f[0].z;

		if (derivatives) {
			fval[3] = f[1].x;
			fval[4] = f[1].y;
			fval[5] = f[1].z;

			fval[6] = f[2].x;
			fval[7] = f[2].y;
			fval[8] = f[2].z;
		}

		return true;
	}
	else if(type == TypeDesc::TypeFloat) {
		float *fval = (float *)val;
		fval[0] = average(f[0]);

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

		return true;
	}

	return false;
}

static bool set_attribute_float3(float3 f, TypeDesc type, bool derivatives, void *val)
{
	float3 fv[3];

	fv[0] = f;
	fv[1] = make_float3(0.0f, 0.0f, 0.0f);
	fv[2] = make_float3(0.0f, 0.0f, 0.0f);

	return set_attribute_float3(fv, type, derivatives, val);
}

static bool set_attribute_float(float f[3], TypeDesc type, bool derivatives, void *val)
{
	if (type == TypeDesc::TypePoint || type == TypeDesc::TypeVector ||
	    type == TypeDesc::TypeNormal || type == TypeDesc::TypeColor)
	{
		float *fval = (float *)val;
		fval[0] = f[0];
		fval[1] = f[1];
		fval[2] = f[2];

		if (derivatives) {
			fval[3] = f[1];
			fval[4] = f[1];
			fval[5] = f[1];

			fval[6] = f[2];
			fval[7] = f[2];
			fval[8] = f[2];
		}

		return true;
	}
	else if(type == TypeDesc::TypeFloat) {
		float *fval = (float *)val;
		fval[0] = f[0];

		if (derivatives) {
			fval[1] = f[1];
			fval[2] = f[2];
		}

		return true;
	}

	return false;
}

static bool set_attribute_float(float f, TypeDesc type, bool derivatives, void *val)
{
	float fv[3];

	fv[0] = f;
	fv[1] = 0.0f;
	fv[2] = 0.0f;

	return set_attribute_float(fv, type, derivatives, val);
}

static bool set_attribute_int(int i, TypeDesc type, bool derivatives, void *val)
{
	if(type.basetype == TypeDesc::INT && type.aggregate == TypeDesc::SCALAR && type.arraylen == 0) {
		int *ival = (int *)val;
		ival[0] = i;

		if (derivatives) {
			ival[1] = 0;
			ival[2] = 0;
		}

		return true;
	}

	return false;
}

static bool set_attribute_string(ustring str, TypeDesc type, bool derivatives, void *val)
{
	if(type.basetype == TypeDesc::INT && type.aggregate == TypeDesc::SCALAR && type.arraylen == 0) {
		ustring *sval = (ustring *)val;
		sval[0] = str;

		if (derivatives) {
			sval[1] = OSLRenderServices::u_empty;
			sval[2] = OSLRenderServices::u_empty;
		}

		return true;
	}

	return false;
}

static bool set_attribute_float3_3(float3 P[3], TypeDesc type, bool derivatives, void *val)
{
	if(type.vecsemantics == TypeDesc::POINT && type.arraylen >= 3) {
		float *fval = (float *)val;

		fval[0] = P[0].x;
		fval[1] = P[0].y;
		fval[2] = P[0].z;

		fval[3] = P[1].x;
		fval[4] = P[1].y;
		fval[5] = P[1].z;

		fval[6] = P[2].x;
		fval[7] = P[2].y;
		fval[8] = P[2].z;

		if(type.arraylen > 3)
			memset(fval + 3*3, 0, sizeof(float)*3*(type.arraylen - 3));
		if (derivatives)
			memset(fval + type.arraylen*3, 0, sizeof(float)*2*3*type.arraylen);

		return true;
	}

	return false;
}

static bool get_mesh_attribute(KernelGlobals *kg, const ShaderData *sd, const OSLGlobals::Attribute& attr,
                               const TypeDesc& type, bool derivatives, void *val)
{
	if (attr.type == TypeDesc::TypePoint || attr.type == TypeDesc::TypeVector ||
	    attr.type == TypeDesc::TypeNormal || attr.type == TypeDesc::TypeColor)
	{
		float3 fval[3];
		fval[0] = triangle_attribute_float3(kg, sd, attr.elem, attr.offset,
		                                    (derivatives) ? &fval[1] : NULL, (derivatives) ? &fval[2] : NULL);
		return set_attribute_float3(fval, type, derivatives, val);
	}
	else if (attr.type == TypeDesc::TypeFloat) {
		float fval[3];
		fval[0] = triangle_attribute_float(kg, sd, attr.elem, attr.offset,
		                                   (derivatives) ? &fval[1] : NULL, (derivatives) ? &fval[2] : NULL);
		return set_attribute_float(fval, type, derivatives, val);
	}
	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);
}

static bool get_object_standard_attribute(KernelGlobals *kg, ShaderData *sd, ustring name,
                                          TypeDesc type, bool derivatives, void *val)
{
	/* todo: turn this into hash table returning int, which can be used in switch */

	/* Object Attributes */
	if (name == "object:location") {
		float3 f = object_location(kg, sd);
		return set_attribute_float3(f, type, derivatives, val);
	}
	else if (name == "object:index") {
		float f = object_pass_id(kg, sd->object);
		return set_attribute_float(f, type, derivatives, val);
	}
	else if (name == "geom:dupli_generated") {
		float3 f = object_dupli_generated(kg, sd->object);
		return set_attribute_float3(f, type, derivatives, val);
	}
	else if (name == "geom:dupli_uv") {
		float3 f = object_dupli_uv(kg, sd->object);
		return set_attribute_float3(f, type, derivatives, val);
	}
	else if (name == "material:index") {
		float f = shader_pass_id(kg, sd);
		return set_attribute_float(f, type, derivatives, val);
	}
	else if (name == "object:random") {
		float f = object_random_number(kg, sd->object);
		return set_attribute_float(f, type, derivatives, val);
	}

	/* Particle Attributes */
	else if (name == "particle:index") {
		uint particle_id = object_particle_id(kg, sd->object);
		float f = particle_index(kg, particle_id);
		return set_attribute_float(f, type, derivatives, val);
	}
	else if (name == "particle:age") {
		uint particle_id = object_particle_id(kg, sd->object);
		float f = particle_age(kg, particle_id);
		return set_attribute_float(f, type, derivatives, val);
	}
	else if (name == "particle:lifetime") {
		uint particle_id = object_particle_id(kg, sd->object);
		float f= particle_lifetime(kg, particle_id);
		return set_attribute_float(f, type, derivatives, val);
	}
	else if (name == "particle:location") {
		uint particle_id = object_particle_id(kg, sd->object);
		float3 f = particle_location(kg, particle_id);
		return set_attribute_float3(f, type, derivatives, val);
	}
#if 0	/* unsupported */
	else if (name == "particle:rotation") {
		uint particle_id = object_particle_id(kg, sd->object);
		float4 f = particle_rotation(kg, particle_id);
		return set_attribute_float4(f, type, derivatives, val);
	}
#endif
	else if (name == "particle:size") {
		uint particle_id = object_particle_id(kg, sd->object);
		float f = particle_size(kg, particle_id);
		return set_attribute_float(f, type, derivatives, val);
	}
	else if (name == "particle:velocity") {
		uint particle_id = object_particle_id(kg, sd->object);
		float3 f = particle_velocity(kg, particle_id);
		return set_attribute_float3(f, type, derivatives, val);
	}
	else if (name == "particle:angular_velocity") {
		uint particle_id = object_particle_id(kg, sd->object);
		float3 f = particle_angular_velocity(kg, particle_id);
		return set_attribute_float3(f, type, derivatives, val);
	}
	else if (name == "geom:numpolyvertices") {
		return set_attribute_int(3, type, derivatives, val);
	}
	else if (name == "geom:trianglevertices" || name == "geom:polyvertices") {
		float3 P[3];
		triangle_vertices(kg, sd->prim, P);
		object_position_transform(kg, sd, &P[0]);
		object_position_transform(kg, sd, &P[1]);
		object_position_transform(kg, sd, &P[2]);
		return set_attribute_float3_3(P, type, derivatives, val);
	}
	else if(name == "geom:name") {
		ustring object_name = kg->osl.object_names[sd->object];
		return set_attribute_string(object_name, type, derivatives, val);
	}
	else
		return false;
}

static bool get_background_attribute(KernelGlobals *kg, ShaderData *sd, ustring name,
                                     TypeDesc type, bool derivatives, void *val)
{
	/* Ray Length */
	if (name == "path:ray_length") {
		float f = sd->ray_length;
		return set_attribute_float(f, type, derivatives, val);
	}
	
	else
		return false;
}

bool OSLRenderServices::get_attribute(void *renderstate, bool derivatives, ustring object_name,
                                      TypeDesc type, ustring name, void *val)
{
	KernelGlobals *kg = kernel_globals;
	ShaderData *sd = (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) {
		return get_background_attribute(kg, sd, name, type, derivatives, val);
	}

	/* 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()) {
		const OSLGlobals::Attribute& attr = it->second;

		if (attr.elem != ATTR_ELEMENT_VALUE) {
			/* triangle and vertex attributes */
			if (tri != ~0)
				return get_mesh_attribute(kg, sd, attr, type, derivatives, val);
		}
		else {
			/* object attribute */
			get_object_attribute(attr, derivatives, val);
			return true;
		}
	}
	else {
		/* not found in attribute, check standard object info */
		bool is_std_object_attribute = get_object_standard_attribute(kg, sd, name, type, derivatives, val);

		if (is_std_object_attribute)
			return true;

		return get_background_attribute(kg, sd, name, type, derivatives, val);
	}

	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 */
}

bool OSLRenderServices::texture(ustring filename, TextureOpt &options,
                                OSL::ShaderGlobals *sg,
                                float s, float t, float dsdx, float dtdx,
                                float dsdy, float dtdy, float *result)
{
	OSL::TextureSystem *ts = kernel_globals->osl.ts;
	bool status = ts->texture(filename, options, s, t, dsdx, dtdx, dsdy, dtdy, result);

	if(!status) {
		if(options.nchannels == 3 || options.nchannels == 4) {
			result[0] = 1.0f;
			result[1] = 0.0f;
			result[2] = 1.0f;

			if(options.nchannels == 4)
				result[3] = 1.0f;
		}
	}

	return status;
}

bool OSLRenderServices::texture3d(ustring filename, TextureOpt &options,
                                  OSL::ShaderGlobals *sg, const OSL::Vec3 &P,
                                  const OSL::Vec3 &dPdx, const OSL::Vec3 &dPdy,
                                  const OSL::Vec3 &dPdz, float *result)
{
	OSL::TextureSystem *ts = kernel_globals->osl.ts;
	bool status = ts->texture3d(filename, options, P, dPdx, dPdy, dPdz, result);

	if(!status) {
		if(options.nchannels == 3 || options.nchannels == 4) {
			result[0] = 1.0f;
			result[1] = 0.0f;
			result[2] = 1.0f;

			if(options.nchannels == 4)
				result[3] = 1.0f;
		}

	}

	return status;
}

bool OSLRenderServices::environment(ustring filename, TextureOpt &options,
                                    OSL::ShaderGlobals *sg, const OSL::Vec3 &R,
                                    const OSL::Vec3 &dRdx, const OSL::Vec3 &dRdy, float *result)
{
	OSL::TextureSystem *ts = kernel_globals->osl.ts;
	bool status = ts->environment(filename, options, R, dRdx, dRdy, result);

	if(!status) {
		if(options.nchannels == 3 || options.nchannels == 4) {
			result[0] = 1.0f;
			result[1] = 0.0f;
			result[2] = 1.0f;

			if(options.nchannels == 4)
				result[3] = 1.0f;
		}
	}

	return status;
}

bool OSLRenderServices::get_texture_info(ustring filename, int subimage,
                                         ustring dataname,
                                         TypeDesc datatype, void *data)
{
	OSL::TextureSystem *ts = kernel_globals->osl.ts;
	return ts->get_texture_info(filename, subimage, dataname, datatype, data);
}

int OSLRenderServices::pointcloud_search(OSL::ShaderGlobals *sg, ustring filename, const OSL::Vec3 &center,
                                         float radius, int max_points, bool sort,
                                         size_t *out_indices, float *out_distances, int derivs_offset)
{
	return 0;
}

int OSLRenderServices::pointcloud_get(ustring filename, size_t *indices, int count,
                                      ustring attr_name, TypeDesc attr_type, void *out_data)
{
	return 0;
}

bool OSLRenderServices::trace(TraceOpt &options, OSL::ShaderGlobals *sg,
	const OSL::Vec3 &P, const OSL::Vec3 &dPdx,
	const OSL::Vec3 &dPdy, const OSL::Vec3 &R,
	const OSL::Vec3 &dRdx, const OSL::Vec3 &dRdy)
{
	/* todo: options.shader support, maybe options.traceset */
	ShaderData *sd = (ShaderData *)(sg->renderstate);

	/* setup ray */
	Ray ray;

	ray.P = TO_FLOAT3(P);
	ray.D = TO_FLOAT3(R);
	ray.t = (options.maxdist == 1.0e30)? FLT_MAX: options.maxdist - options.mindist;
	ray.time = sd->time;

	if(options.mindist == 0.0f) {
		/* avoid self-intersections */
		if(ray.P == sd->P) {
			bool transmit = (dot(sd->Ng, ray.D) < 0.0f);
			ray.P = ray_offset(sd->P, (transmit)? -sd->Ng: sd->Ng);
		}
	}
	else {
		/* offset for minimum distance */
		ray.P += options.mindist*ray.D;
	}

	/* ray differentials */
	ray.dP.dx = TO_FLOAT3(dPdx);
	ray.dP.dy = TO_FLOAT3(dPdy);
	ray.dD.dx = TO_FLOAT3(dRdx);
	ray.dD.dy = TO_FLOAT3(dRdy);

	/* allocate trace data */
	TraceData *tracedata = new TraceData();
	tracedata->ray = ray;
	tracedata->setup = false;

	if(sg->tracedata)
		delete (TraceData*)sg->tracedata;
	sg->tracedata = tracedata;

	/* raytrace */
	return scene_intersect(kernel_globals, &ray, ~0, &tracedata->isect);
}


bool OSLRenderServices::getmessage(OSL::ShaderGlobals *sg, ustring source, ustring name,
	TypeDesc type, void *val, bool derivatives)
{
	TraceData *tracedata = (TraceData*)sg->tracedata;

	if(source == "trace" && tracedata) {
		if(name == "hit") {
			return set_attribute_int((tracedata->isect.prim != ~0), type, derivatives, val);
		}
		else if(tracedata->isect.prim != ~0) {
			if(name == "hitdist") {
				float f[3] = {tracedata->isect.t, 0.0f, 0.0f};
				return set_attribute_float(f, type, derivatives, val);
			}
			else {
				KernelGlobals *kg = kernel_globals;
				ShaderData *sd = &tracedata->sd;

				if(!tracedata->setup) {
					/* lazy shader data setup */
					shader_setup_from_ray(kg, sd, &tracedata->isect, &tracedata->ray);
					tracedata->setup = true;
				}

				if(name == "N") {
					return set_attribute_float3(sd->N, type, derivatives, val);
				}
				else if(name == "Ng") {
					return set_attribute_float3(sd->Ng, type, derivatives, val);
				}
				else if(name == "P") {
					float3 f[3] = {sd->P, sd->dP.dx, sd->dP.dy};
					return set_attribute_float3(f, type, derivatives, val);
				}
				else if(name == "I") {
					float3 f[3] = {sd->I, sd->dI.dx, sd->dI.dy};
					return set_attribute_float3(f, type, derivatives, val);
				}
				else if(name == "u") {
					float f[3] = {sd->u, sd->du.dx, sd->du.dy};
					return set_attribute_float(f, type, derivatives, val);
				}
				else if(name == "v") {
					float f[3] = {sd->v, sd->dv.dx, sd->dv.dy};
					return set_attribute_float(f, type, derivatives, val);
				}

				return get_attribute(sd, derivatives, u_empty, type, name, val);
			}
		}
	}

	return false;
}

CCL_NAMESPACE_END