Fix Cycles debug build assert on some platforms, tighten checks to avoid this in...
[blender.git] / intern / cycles / app / cycles_xml.cpp
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18
19 #include <sstream>
20 #include <algorithm>
21 #include <iterator>
22
23 #include "node_xml.h"
24
25 #include "background.h"
26 #include "camera.h"
27 #include "film.h"
28 #include "graph.h"
29 #include "integrator.h"
30 #include "light.h"
31 #include "mesh.h"
32 #include "nodes.h"
33 #include "object.h"
34 #include "osl.h"
35 #include "shader.h"
36 #include "scene.h"
37
38 #include "subd_mesh.h"
39 #include "subd_patch.h"
40 #include "subd_split.h"
41
42 #include "util_debug.h"
43 #include "util_foreach.h"
44 #include "util_path.h"
45 #include "util_transform.h"
46 #include "util_xml.h"
47
48 #include "cycles_xml.h"
49
50 CCL_NAMESPACE_BEGIN
51
52 /* XML reading state */
53
54 struct XMLReadState : public XMLReader {
55         Scene *scene;           /* scene pointer */
56         Transform tfm;          /* current transform state */
57         bool smooth;            /* smooth normal state */
58         Shader *shader;         /* current shader */
59         string base;            /* base path to current file*/
60         float dicing_rate;      /* current dicing rate */
61         Mesh::DisplacementMethod displacement_method;
62
63         XMLReadState()
64           : scene(NULL),
65             smooth(false),
66             shader(NULL),
67             dicing_rate(0.0f),
68             displacement_method(Mesh::DISPLACE_BUMP)
69         {
70                 tfm = transform_identity();
71         }
72 };
73
74 /* Attribute Reading */
75
76 static bool xml_read_int(int *value, pugi::xml_node node, const char *name)
77 {
78         pugi::xml_attribute attr = node.attribute(name);
79
80         if(attr) {
81                 *value = atoi(attr.value());
82                 return true;
83         }
84
85         return false;
86 }
87
88 static bool xml_read_int_array(vector<int>& value, pugi::xml_node node, const char *name)
89 {
90         pugi::xml_attribute attr = node.attribute(name);
91
92         if(attr) {
93                 vector<string> tokens;
94                 string_split(tokens, attr.value());
95
96                 foreach(const string& token, tokens)
97                         value.push_back(atoi(token.c_str()));
98
99                 return true;
100         }
101
102         return false;
103 }
104
105 static bool xml_read_float(float *value, pugi::xml_node node, const char *name)
106 {
107         pugi::xml_attribute attr = node.attribute(name);
108
109         if(attr) {
110                 *value = (float)atof(attr.value());
111                 return true;
112         }
113
114         return false;
115 }
116
117 static bool xml_read_float_array(vector<float>& value, pugi::xml_node node, const char *name)
118 {
119         pugi::xml_attribute attr = node.attribute(name);
120
121         if(attr) {
122                 vector<string> tokens;
123                 string_split(tokens, attr.value());
124
125                 foreach(const string& token, tokens)
126                         value.push_back((float)atof(token.c_str()));
127
128                 return true;
129         }
130
131         return false;
132 }
133
134 static bool xml_read_float3(float3 *value, pugi::xml_node node, const char *name)
135 {
136         vector<float> array;
137
138         if(xml_read_float_array(array, node, name) && array.size() == 3) {
139                 *value = make_float3(array[0], array[1], array[2]);
140                 return true;
141         }
142
143         return false;
144 }
145
146 static bool xml_read_float3_array(vector<float3>& value, pugi::xml_node node, const char *name)
147 {
148         vector<float> array;
149
150         if(xml_read_float_array(array, node, name)) {
151                 for(size_t i = 0; i < array.size(); i += 3)
152                         value.push_back(make_float3(array[i+0], array[i+1], array[i+2]));
153
154                 return true;
155         }
156
157         return false;
158 }
159
160 static bool xml_read_float4(float4 *value, pugi::xml_node node, const char *name)
161 {
162         vector<float> array;
163
164         if(xml_read_float_array(array, node, name) && array.size() == 4) {
165                 *value = make_float4(array[0], array[1], array[2], array[3]);
166                 return true;
167         }
168
169         return false;
170 }
171
172 static bool xml_read_string(string *str, pugi::xml_node node, const char *name)
173 {
174         pugi::xml_attribute attr = node.attribute(name);
175
176         if(attr) {
177                 *str = attr.value();
178                 return true;
179         }
180
181         return false;
182 }
183
184 static bool xml_equal_string(pugi::xml_node node, const char *name, const char *value)
185 {
186         pugi::xml_attribute attr = node.attribute(name);
187
188         if(attr)
189                 return string_iequals(attr.value(), value);
190         
191         return false;
192 }
193
194 /* Camera */
195
196 static void xml_read_camera(XMLReadState& state, pugi::xml_node node)
197 {
198         Camera *cam = state.scene->camera;
199
200         xml_read_int(&cam->width, node, "width");
201         xml_read_int(&cam->height, node, "height");
202
203         xml_read_node(state, cam, node);
204
205         cam->matrix = state.tfm;
206
207         cam->need_update = true;
208         cam->update();
209 }
210
211 /* Shader */
212
213 static string xml_socket_name(const char *name)
214 {
215         string sname = name;
216         size_t i;
217
218         while((i = sname.find(" ")) != string::npos)
219                 sname.replace(i, 1, "");
220         
221         return sname;
222 }
223
224 static void xml_read_shader_graph(XMLReadState& state, Shader *shader, pugi::xml_node graph_node)
225 {
226         xml_read_node(state, shader, graph_node);
227
228         ShaderGraph *graph = new ShaderGraph();
229
230         /* local state, shader nodes can't link to nodes outside the shader graph */
231         XMLReader graph_reader;
232         graph_reader.node_map[ustring("output")] = graph->output();
233
234         for(pugi::xml_node node = graph_node.first_child(); node; node = node.next_sibling()) {
235                 ustring node_name(node.name());
236
237                 if(node_name == "connect") {
238                         /* connect nodes */
239                         vector<string> from_tokens, to_tokens;
240
241                         string_split(from_tokens, node.attribute("from").value());
242                         string_split(to_tokens, node.attribute("to").value());
243
244                         if(from_tokens.size() == 2 && to_tokens.size() == 2) {
245                                 ustring from_node_name(from_tokens[0]);
246                                 ustring from_socket_name(from_tokens[1]);
247                                 ustring to_node_name(to_tokens[0]);
248                                 ustring to_socket_name(to_tokens[1]);
249
250                                 /* find nodes and sockets */
251                                 ShaderOutput *output = NULL;
252                                 ShaderInput *input = NULL;
253
254                                 if(graph_reader.node_map.find(from_node_name) != graph_reader.node_map.end()) {
255                                         ShaderNode *fromnode = (ShaderNode*)graph_reader.node_map[from_node_name];
256
257                                         foreach(ShaderOutput *out, fromnode->outputs)
258                                                 if(string_iequals(xml_socket_name(out->name().c_str()), from_socket_name.c_str()))
259                                                         output = out;
260
261                                         if(!output)
262                                                 fprintf(stderr, "Unknown output socket name \"%s\" on \"%s\".\n", from_node_name.c_str(), from_socket_name.c_str());
263                                 }
264                                 else
265                                         fprintf(stderr, "Unknown shader node name \"%s\".\n", from_node_name.c_str());
266
267                                 if(graph_reader.node_map.find(to_node_name) != graph_reader.node_map.end()) {
268                                         ShaderNode *tonode = (ShaderNode*)graph_reader.node_map[to_node_name];
269
270                                         foreach(ShaderInput *in, tonode->inputs)
271                                                 if(string_iequals(xml_socket_name(in->name().c_str()), to_socket_name.c_str()))
272                                                         input = in;
273
274                                         if(!input)
275                                                 fprintf(stderr, "Unknown input socket name \"%s\" on \"%s\".\n", to_socket_name.c_str(), to_node_name.c_str());
276                                 }
277                                 else
278                                         fprintf(stderr, "Unknown shader node name \"%s\".\n", to_node_name.c_str());
279
280                                 /* connect */
281                                 if(output && input)
282                                         graph->connect(output, input);
283                         }
284                         else
285                                 fprintf(stderr, "Invalid from or to value for connect node.\n");
286
287                         continue;
288                 }
289
290                 ShaderNode *snode = NULL;
291
292 #ifdef WITH_OSL
293                 if(node_name == "osl_shader") {
294                         ShaderManager *manager = state.scene->shader_manager;
295
296                         if(manager->use_osl()) {
297                                 std::string filepath;
298
299                                 if(xml_read_string(&filepath, node, "src")) {
300                                         if(path_is_relative(filepath)) {
301                                                 filepath = path_join(state.base, filepath);
302                                         }
303
304                                         snode = ((OSLShaderManager*)manager)->osl_node(filepath);
305
306                                         if(!snode) {
307                                                 fprintf(stderr, "Failed to create OSL node from \"%s\".\n", filepath.c_str());
308                                                 continue;
309                                         }
310                                 }
311                                 else {
312                                         fprintf(stderr, "OSL node missing \"src\" attribute.\n");
313                                         continue;
314                                 }
315                         }
316                         else {
317                                 fprintf(stderr, "OSL node without using --shadingsys osl.\n");
318                                 continue;
319                         }
320                 }
321                 else
322 #endif
323                 {
324                         /* exception for name collision */
325                         if(node_name == "background")
326                                 node_name = "background_shader";
327
328                         const NodeType *node_type = NodeType::find(node_name);
329
330                         if(!node_type) {
331                                 fprintf(stderr, "Unknown shader node \"%s\".\n", node.name());
332                                 continue;
333                         }
334                         else if(node_type->type != NodeType::SHADER) {
335                                 fprintf(stderr, "Node type \"%s\" is not a shader node.\n", node_type->name.c_str());
336                                 continue;
337                         }
338
339                         snode = (ShaderNode*) node_type->create(node_type);
340                 }
341
342                 xml_read_node(graph_reader, snode, node);
343
344                 if(node_name == "image_texture") {
345                         ImageTextureNode *img = (ImageTextureNode*) snode;
346                         img->filename = path_join(state.base, img->filename.string());
347                 }
348                 else if(node_name == "environment_texture") {
349                         EnvironmentTextureNode *env = (EnvironmentTextureNode*) snode;
350                         env->filename = path_join(state.base, env->filename.string());
351                 }
352
353                 if(snode) {
354                         /* add to graph */
355                         graph->add(snode);
356                 }
357         }
358
359         shader->set_graph(graph);
360         shader->tag_update(state.scene);
361 }
362
363 static void xml_read_shader(XMLReadState& state, pugi::xml_node node)
364 {
365         Shader *shader = new Shader();
366         xml_read_shader_graph(state, shader, node);
367         state.scene->shaders.push_back(shader);
368 }
369
370 /* Background */
371
372 static void xml_read_background(XMLReadState& state, pugi::xml_node node)
373 {
374         /* Background Settings */
375         xml_read_node(state, state.scene->background, node);
376
377         /* Background Shader */
378         Shader *shader = state.scene->default_background;
379         xml_read_shader_graph(state, shader, node);
380 }
381
382 /* Mesh */
383
384 static Mesh *xml_add_mesh(Scene *scene, const Transform& tfm)
385 {
386         /* create mesh */
387         Mesh *mesh = new Mesh();
388         scene->meshes.push_back(mesh);
389
390         /* create object*/
391         Object *object = new Object();
392         object->mesh = mesh;
393         object->tfm = tfm;
394         scene->objects.push_back(object);
395
396         return mesh;
397 }
398
399 static void xml_read_mesh(const XMLReadState& state, pugi::xml_node node)
400 {
401         /* add mesh */
402         Mesh *mesh = xml_add_mesh(state.scene, state.tfm);
403         mesh->used_shaders.push_back(state.shader);
404
405         /* read state */
406         int shader = 0;
407         bool smooth = state.smooth;
408
409         mesh->displacement_method = state.displacement_method;
410
411         /* read vertices and polygons, RIB style */
412         vector<float3> P;
413         vector<float> UV;
414         vector<int> verts, nverts;
415
416         xml_read_float3_array(P, node, "P");
417         xml_read_int_array(verts, node, "verts");
418         xml_read_int_array(nverts, node, "nverts");
419
420         if(xml_equal_string(node, "subdivision", "catmull-clark")) {
421                 /* create subd mesh */
422                 SubdMesh sdmesh;
423
424                 /* create subd vertices */
425                 for(size_t i = 0; i < P.size(); i++)
426                         sdmesh.add_vert(P[i]);
427
428                 /* create subd faces */
429                 int index_offset = 0;
430
431                 for(size_t i = 0; i < nverts.size(); i++) {
432                         if(nverts[i] == 4) {
433                                 int v0 = verts[index_offset + 0];
434                                 int v1 = verts[index_offset + 1];
435                                 int v2 = verts[index_offset + 2];
436                                 int v3 = verts[index_offset + 3];
437
438                                 sdmesh.add_face(v0, v1, v2, v3);
439                         }
440                         else {
441                                 for(int j = 0; j < nverts[i]-2; j++) {
442                                         int v0 = verts[index_offset];
443                                         int v1 = verts[index_offset + j + 1];
444                                         int v2 = verts[index_offset + j + 2];
445
446                                         sdmesh.add_face(v0, v1, v2);
447                                 }
448                         }
449
450                         index_offset += nverts[i];
451                 }
452
453                 /* finalize subd mesh */
454                 sdmesh.finish();
455
456                 /* parameters */
457                 SubdParams sdparams(mesh, shader, smooth);
458                 xml_read_float(&sdparams.dicing_rate, node, "dicing_rate");
459
460                 DiagSplit dsplit(sdparams);
461                 sdmesh.tessellate(&dsplit);
462         }
463         else {
464                 /* create vertices */
465                 mesh->verts = P;
466
467                 size_t num_triangles = 0;
468                 for(size_t i = 0; i < nverts.size(); i++)
469                         num_triangles += nverts[i]-2;
470                 mesh->reserve_mesh(mesh->verts.size(), num_triangles);
471
472                 /* create triangles */
473                 int index_offset = 0;
474
475                 for(size_t i = 0; i < nverts.size(); i++) {
476                         for(int j = 0; j < nverts[i]-2; j++) {
477                                 int v0 = verts[index_offset];
478                                 int v1 = verts[index_offset + j + 1];
479                                 int v2 = verts[index_offset + j + 2];
480
481                                 assert(v0 < (int)P.size());
482                                 assert(v1 < (int)P.size());
483                                 assert(v2 < (int)P.size());
484
485                                 mesh->add_triangle(v0, v1, v2, shader, smooth);
486                         }
487
488                         index_offset += nverts[i];
489                 }
490
491                 if(xml_read_float_array(UV, node, "UV")) {
492                         ustring name = ustring("UVMap");
493                         Attribute *attr = mesh->attributes.add(ATTR_STD_UV, name);
494                         float3 *fdata = attr->data_float3();
495
496                         /* loop over the triangles */
497                         index_offset = 0;
498                         for(size_t i = 0; i < nverts.size(); i++) {
499                                 for(int j = 0; j < nverts[i]-2; j++) {
500                                         int v0 = index_offset;
501                                         int v1 = index_offset + j + 1;
502                                         int v2 = index_offset + j + 2;
503
504                                         assert(v0*2+1 < (int)UV.size());
505                                         assert(v1*2+1 < (int)UV.size());
506                                         assert(v2*2+1 < (int)UV.size());
507
508                                         fdata[0] = make_float3(UV[v0*2], UV[v0*2+1], 0.0);
509                                         fdata[1] = make_float3(UV[v1*2], UV[v1*2+1], 0.0);
510                                         fdata[2] = make_float3(UV[v2*2], UV[v2*2+1], 0.0);
511                                         fdata += 3;
512                                 }
513
514                                 index_offset += nverts[i];
515                         }
516                 }
517         }
518
519         /* temporary for test compatibility */
520         mesh->attributes.remove(ATTR_STD_VERTEX_NORMAL);
521 }
522
523 /* Patch */
524
525 static void xml_read_patch(const XMLReadState& state, pugi::xml_node node)
526 {
527         /* read patch */
528         Patch *patch = NULL;
529
530         vector<float3> P;
531         xml_read_float3_array(P, node, "P");
532
533         if(xml_equal_string(node, "type", "bilinear")) {
534                 /* bilinear patch */
535                 if(P.size() == 4) {
536                         LinearQuadPatch *bpatch = new LinearQuadPatch();
537
538                         for(int i = 0; i < 4; i++)
539                                 P[i] = transform_point(&state.tfm, P[i]);
540                         memcpy(bpatch->hull, &P[0], sizeof(bpatch->hull));
541
542                         patch = bpatch;
543                 }
544                 else
545                         fprintf(stderr, "Invalid number of control points for bilinear patch.\n");
546         }
547         else if(xml_equal_string(node, "type", "bicubic")) {
548                 /* bicubic patch */
549                 if(P.size() == 16) {
550                         BicubicPatch *bpatch = new BicubicPatch();
551
552                         for(int i = 0; i < 16; i++)
553                                 P[i] = transform_point(&state.tfm, P[i]);
554                         memcpy(bpatch->hull, &P[0], sizeof(bpatch->hull));
555
556                         patch = bpatch;
557                 }
558                 else
559                         fprintf(stderr, "Invalid number of control points for bicubic patch.\n");
560         }
561         else
562                 fprintf(stderr, "Unknown patch type.\n");
563
564         if(patch) {
565                 /* add mesh */
566                 Mesh *mesh = xml_add_mesh(state.scene, transform_identity());
567
568                 mesh->used_shaders.push_back(state.shader);
569
570                 /* split */
571                 SubdParams sdparams(mesh, 0, state.smooth);
572                 xml_read_float(&sdparams.dicing_rate, node, "dicing_rate");
573
574                 DiagSplit dsplit(sdparams);
575                 dsplit.split_quad(patch);
576
577                 delete patch;
578
579                 /* temporary for test compatibility */
580                 mesh->attributes.remove(ATTR_STD_VERTEX_NORMAL);
581         }
582 }
583
584 /* Light */
585
586 static void xml_read_light(XMLReadState& state, pugi::xml_node node)
587 {
588         Light *light = new Light();
589
590         light->shader = state.shader;
591         xml_read_node(state, light, node);
592
593         state.scene->lights.push_back(light);
594 }
595
596 /* Transform */
597
598 static void xml_read_transform(pugi::xml_node node, Transform& tfm)
599 {
600         if(node.attribute("matrix")) {
601                 vector<float> matrix;
602                 if(xml_read_float_array(matrix, node, "matrix") && matrix.size() == 16)
603                         tfm = tfm * transform_transpose((*(Transform*)&matrix[0]));
604         }
605
606         if(node.attribute("translate")) {
607                 float3 translate = make_float3(0.0f, 0.0f, 0.0f);
608                 xml_read_float3(&translate, node, "translate");
609                 tfm = tfm * transform_translate(translate);
610         }
611
612         if(node.attribute("rotate")) {
613                 float4 rotate = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
614                 xml_read_float4(&rotate, node, "rotate");
615                 tfm = tfm * transform_rotate(DEG2RADF(rotate.x), make_float3(rotate.y, rotate.z, rotate.w));
616         }
617
618         if(node.attribute("scale")) {
619                 float3 scale = make_float3(0.0f, 0.0f, 0.0f);
620                 xml_read_float3(&scale, node, "scale");
621                 tfm = tfm * transform_scale(scale);
622         }
623 }
624
625 /* State */
626
627 static void xml_read_state(XMLReadState& state, pugi::xml_node node)
628 {
629         /* read shader */
630         string shadername;
631
632         if(xml_read_string(&shadername, node, "shader")) {
633                 bool found = false;
634
635                 foreach(Shader *shader, state.scene->shaders) {
636                         if(shader->name == shadername) {
637                                 state.shader = shader;
638                                 found = true;
639                                 break;
640                         }
641                 }
642
643                 if(!found)
644                         fprintf(stderr, "Unknown shader \"%s\".\n", shadername.c_str());
645         }
646
647         xml_read_float(&state.dicing_rate, node, "dicing_rate");
648
649         /* read smooth/flat */
650         if(xml_equal_string(node, "interpolation", "smooth"))
651                 state.smooth = true;
652         else if(xml_equal_string(node, "interpolation", "flat"))
653                 state.smooth = false;
654
655         /* read displacement method */
656         if(xml_equal_string(node, "displacement_method", "true"))
657                 state.displacement_method = Mesh::DISPLACE_TRUE;
658         else if(xml_equal_string(node, "displacement_method", "bump"))
659                 state.displacement_method = Mesh::DISPLACE_BUMP;
660         else if(xml_equal_string(node, "displacement_method", "both"))
661                 state.displacement_method = Mesh::DISPLACE_BOTH;
662 }
663
664 /* Scene */
665
666 static void xml_read_include(XMLReadState& state, const string& src);
667
668 static void xml_read_scene(XMLReadState& state, pugi::xml_node scene_node)
669 {
670         for(pugi::xml_node node = scene_node.first_child(); node; node = node.next_sibling()) {
671                 if(string_iequals(node.name(), "film")) {
672                         xml_read_node(state, state.scene->film, node);
673                 }
674                 else if(string_iequals(node.name(), "integrator")) {
675                         xml_read_node(state, state.scene->integrator, node);
676                 }
677                 else if(string_iequals(node.name(), "camera")) {
678                         xml_read_camera(state, node);
679                 }
680                 else if(string_iequals(node.name(), "shader")) {
681                         xml_read_shader(state, node);
682                 }
683                 else if(string_iequals(node.name(), "background")) {
684                         xml_read_background(state, node);
685                 }
686                 else if(string_iequals(node.name(), "mesh")) {
687                         xml_read_mesh(state, node);
688                 }
689                 else if(string_iequals(node.name(), "patch")) {
690                         xml_read_patch(state, node);
691                 }
692                 else if(string_iequals(node.name(), "light")) {
693                         xml_read_light(state, node);
694                 }
695                 else if(string_iequals(node.name(), "transform")) {
696                         XMLReadState substate = state;
697
698                         xml_read_transform(node, substate.tfm);
699                         xml_read_scene(substate, node);
700                 }
701                 else if(string_iequals(node.name(), "state")) {
702                         XMLReadState substate = state;
703
704                         xml_read_state(substate, node);
705                         xml_read_scene(substate, node);
706                 }
707                 else if(string_iequals(node.name(), "include")) {
708                         string src;
709
710                         if(xml_read_string(&src, node, "src"))
711                                 xml_read_include(state, src);
712                 }
713                 else
714                         fprintf(stderr, "Unknown node \"%s\".\n", node.name());
715         }
716 }
717
718 /* Include */
719
720 static void xml_read_include(XMLReadState& state, const string& src)
721 {
722         /* open XML document */
723         pugi::xml_document doc;
724         pugi::xml_parse_result parse_result;
725
726         string path = path_join(state.base, src);
727         parse_result = doc.load_file(path.c_str());
728
729         if(parse_result) {
730                 XMLReadState substate = state;
731                 substate.base = path_dirname(path);
732
733                 pugi::xml_node cycles = doc.child("cycles");
734                 xml_read_scene(substate, cycles);
735         }
736         else {
737                 fprintf(stderr, "%s read error: %s\n", src.c_str(), parse_result.description());
738                 exit(EXIT_FAILURE);
739         }
740 }
741
742 /* File */
743
744 void xml_read_file(Scene *scene, const char *filepath)
745 {
746         XMLReadState state;
747
748         state.scene = scene;
749         state.tfm = transform_identity();
750         state.shader = scene->default_surface;
751         state.smooth = false;
752         state.dicing_rate = 0.1f;
753         state.base = path_dirname(filepath);
754
755         xml_read_include(state, path_filename(filepath));
756
757         scene->params.bvh_type = SceneParams::BVH_STATIC;
758 }
759
760 CCL_NAMESPACE_END
761