-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXMLMaterialExportCommon.cpp
More file actions
555 lines (476 loc) · 16.3 KB
/
Copy pathXMLMaterialExportCommon.cpp
File metadata and controls
555 lines (476 loc) · 16.3 KB
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
/**********************************************************************
Copyright 2020 Advanced Micro Devices, Inc
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 "XMLMaterialExportCommon.h"
#include "Logger.h"
#include <stack>
#include <fstream>
#include <ostream>
#include <sstream>
#include <iostream>
#include "RPRStringIDMapper.h"
// execute FireRender func and check for an error
#define CHECK_NO_ERROR(func) { \
rpr_int status = func; \
if (status != RPR_SUCCESS) \
throw std::runtime_error("Radeon ProRender error (" + std::to_string(status) + ") in " + std::string(__FILE__) + ":" + std::to_string(__LINE__)); \
}
RPRStringIDMapper g_rprStringMapper;
namespace
{
const std::string kTab = " "; // default 4spaces tab for xml writer
const int kVersion = RPR_VERSION_MAJOR_MINOR_REVISION;
struct Param
{
std::string type;
std::string value;
};
class XmlWriter
{
public:
XmlWriter(const std::string& file)
: m_doc(file)
, top_written(true)
{}
~XmlWriter()
{
endDocument();
}
//write header
void startDocument()
{
m_doc << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl;
}
void endDocument()
{
size_t size = m_nodes.size();
for (size_t i = 0; i < size; ++i)
{
endElement();
}
}
void startElement(const std::string& node_name)
{
//write prev node with atts
if (m_nodes.size() != 0 && top_written)
{
std::string tab = "";
for (int i = 0; i < m_nodes.size() - 1; ++i) tab += kTab;
const Node& node = m_nodes.top();
m_doc << tab << "<" << node.name << "";
for (const auto& at : node.atts)
{
m_doc << " " << at.type << "=\"" << at.value << "\""; // name="value"
}
m_doc << ">" << std::endl;
}
m_nodes.push({ node_name });
top_written = true;
}
void endElement()
{
std::string tab = "";
for (int i = 0; i < m_nodes.size() - 1; ++i) tab += kTab;
const Node& node = m_nodes.top();
if (top_written)
{
m_doc << tab << "<" << node.name << "";
for (const auto& at : node.atts)
{
m_doc << " " << at.type << "=\"" << at.value << "\""; // name="value"
}
m_doc << "/>" << std::endl;
}
else
m_doc << tab << "</" << node.name << ">" << std::endl;
m_nodes.pop();
top_written = false;
}
void writeAttribute(const std::string& name, const std::string& value)
{
Node& node = m_nodes.top();
node.atts.push_back({ name, value });
}
void writeTextElement(const std::string& name, const std::string& text)
{
std::string tab = "";
for (int i = 0; i < m_nodes.size() - 1; ++i) tab += kTab;
if (m_nodes.size() != 0 && top_written)
{
const Node& node = m_nodes.top();
m_doc << tab << "<" << node.name << "";
for (const auto& at : node.atts)
{
m_doc << " " << at.type << "=\"" << at.value << "\""; // name="value"
}
m_doc << ">" << std::endl;
}
tab += kTab;
//<name>text</name>
m_doc << tab << "<" << name << ">" << text << "</" << name << ">" << std::endl;
top_written = false;
}
private:
std::ofstream m_doc;
struct Node
{
std::string name;
std::vector<Param> atts;
};
std::stack<Node> m_nodes;
bool top_written; // show is element in top of m_nodes stack already written into xml or not.
};
}
void InitParamList(void)
{
}
// return true if success
bool ParseRPR (
rpr_material_node material,
std::set<rpr_material_node>& nodeList,
const std::string& folder,
bool originIsUberColor, // set to TRUE if this 'material' is connected to a COLOR input of the UBER material
std::unordered_map<rpr_image, RPR_MATERIAL_XML_EXPORT_TEXTURE_PARAM>& textureParameter,
const std::string& material_name,
bool exportImageUV
)
{
// check if the node already stored
if (nodeList.find(material) != nodeList.end())
{
return true;
}
nodeList.insert(material);
rpr_int status = RPR_SUCCESS;
rpr_material_node_type type = 0;
rprMaterialNodeGetInfo(material, RPR_MATERIAL_NODE_TYPE, sizeof(rpr_material_node_type), &type, nullptr);
#ifdef _DEBUG
{
std::string typeName;
g_rprStringMapper.RPRMaterialType_id_to_string(type,typeName);
DebugPrint("processed node name = %s", typeName.c_str());
}
#endif
size_t input_count = 0;
status = rprMaterialNodeGetInfo(material, RPR_MATERIAL_NODE_INPUT_COUNT, sizeof(size_t), &input_count, nullptr);
for (int i = 0; i < input_count; ++i)
{
rpr_int in_type;
status = rprMaterialNodeGetInputInfo(material, i, RPR_MATERIAL_NODE_INPUT_TYPE, sizeof(in_type), &in_type, nullptr);
if (in_type == RPR_MATERIAL_NODE_INPUT_TYPE_NODE)
{
size_t read_count = 0;
uint32_t id = 0;
CHECK_NO_ERROR(rprMaterialNodeGetInputInfo(material, i, RPR_MATERIAL_NODE_INPUT_NAME, sizeof(uint32_t), &id, nullptr));
std::string param_name;
g_rprStringMapper.RPRMaterialInput_id_to_string(id,param_name);
rpr_material_node ref_node = nullptr;
status = rprMaterialNodeGetInputInfo(material, i, RPR_MATERIAL_NODE_INPUT_VALUE, sizeof(ref_node), &ref_node, nullptr);
if (ref_node)
{
if (!exportImageUV // if UV not exported
&& type == RPR_MATERIAL_NODE_IMAGE_TEXTURE
&& param_name.size() == 3 && param_name[0] == 'u' && param_name[1] == 'v' && param_name[2] == 0
)
{
// not supported atm
}
else
{
ParseRPR(ref_node, nodeList, folder, originIsUberColor, textureParameter, material_name, exportImageUV);
}
}
}
else if (in_type == RPR_MATERIAL_NODE_INPUT_TYPE_IMAGE)
{
rpr_image img = nullptr;
status = rprMaterialNodeGetInputInfo(material, i, RPR_MATERIAL_NODE_INPUT_VALUE, sizeof(img), &img, nullptr);
size_t name_size = 0;
status = rprImageGetInfo(img, RPR_OBJECT_NAME, 0, nullptr, &name_size);
//create file if filename is empty(name == "\0")
if (name_size == 1)
{
// not supported atm
}
else
{
std::string mat_name;
mat_name.resize(name_size - 1);
rprImageGetInfo(img, RPR_OBJECT_NAME, name_size, &mat_name[0], nullptr);
}
textureParameter[img].useGamma = originIsUberColor ? true : false;
}
}
return true;
}
void ExportMaterials(const std::string& filename,
const std::set<rpr_material_node>& nodeList,
std::unordered_map<rpr_image, RPR_MATERIAL_XML_EXPORT_TEXTURE_PARAM>& textureParameter,
rpr_material_node closureNode,
const std::string& material_name,// example : "Emissive_Fluorescent_Magenta"
const std::map<rpr_image, EXTRA_IMAGE_DATA>& extraImageData,
bool exportImageUV
)
{
XmlWriter writer(filename);
if (nodeList.size() == 0)
{
std::cout << "MaterialExport error: materials input array is nullptr" << std::endl;
return;
}
try
{
writer.startDocument();
writer.startElement("material");
writer.writeAttribute("name", material_name);
// in case we need versioning the material XMLs... - unused for the moment.
writer.writeAttribute("version_exporter", "10");
// version of the RPR_API_VERSION used to generate this XML
std::stringstream hex_version;
hex_version << "0x" << std::hex << kVersion;
writer.writeAttribute("version_rpr", hex_version.str());
// file path as key, id - for material nodes connections
std::map<std::string, std::pair< std::string, rpr_image > > textures;
std::map<void*, std::string> objects; // <object, node_name> map
std::set<std::string> used_names; // to avoid duplicating node names
std::string closureNodeName = "";
// fill materials first
for (const auto& iNode : nodeList)
{
rpr_material_node mat = iNode;
if (objects.count(mat))
continue;
size_t name_size = 0;
CHECK_NO_ERROR(rprMaterialNodeGetInfo(mat, RPR_OBJECT_NAME, NULL, nullptr, &name_size));
std::string mat_node_name;
mat_node_name.resize(name_size - 1); // exclude extra terminator
CHECK_NO_ERROR(rprMaterialNodeGetInfo(mat, RPR_OBJECT_NAME, name_size, &mat_node_name[0], nullptr));
int postfix_id = 0;
if (mat_node_name.empty())
mat_node_name = "node" + std::to_string(postfix_id);
while (used_names.count(mat_node_name))
{
mat_node_name = "node" + std::to_string(postfix_id);
++postfix_id;
}
if (mat == closureNode)
closureNodeName = mat_node_name;
objects[mat] = mat_node_name;
used_names.insert(mat_node_name);
}
// closure_node is the name of the node containing the final output of the material
writer.writeAttribute("closure_node", closureNodeName);
// look for images
for (const auto& iNode : nodeList)
{
rpr_material_node mat = iNode;
size_t input_count = 0;
CHECK_NO_ERROR(rprMaterialNodeGetInfo(mat, RPR_MATERIAL_NODE_INPUT_COUNT, sizeof(size_t), &input_count, nullptr));
for (int input_id = 0; input_id < input_count; ++input_id)
{
rpr_material_node_type input_type;
CHECK_NO_ERROR(rprMaterialNodeGetInputInfo(mat, input_id, RPR_MATERIAL_NODE_INPUT_TYPE, sizeof(input_type), &input_type, nullptr));
if (input_type != RPR_MATERIAL_NODE_INPUT_TYPE_IMAGE)
continue;
rpr_image img = nullptr;
CHECK_NO_ERROR(rprMaterialNodeGetInputInfo(mat, input_id, RPR_MATERIAL_NODE_INPUT_VALUE, sizeof(img), &img, nullptr));
if (objects.count(img)) // already mentioned
continue;
std::string img_node_name = "node0";
int postfix_id = 0;
while (used_names.count(img_node_name))
{
img_node_name = "node" + std::to_string(postfix_id);
++postfix_id;
}
objects[img] = img_node_name;
used_names.insert(img_node_name);
}
}
// optionally write description (at the moment there is no description)
writer.writeTextElement("description", "");
for (const auto& iNode : nodeList)
{
rpr_material_node node = iNode;
rpr_material_node_type type;
size_t input_count = 0;
CHECK_NO_ERROR(rprMaterialNodeGetInfo(node, RPR_MATERIAL_NODE_TYPE, sizeof(rpr_material_node_type), &type, nullptr));
CHECK_NO_ERROR(rprMaterialNodeGetInfo(node, RPR_MATERIAL_NODE_INPUT_COUNT, sizeof(size_t), &input_count, nullptr));
writer.startElement("node");
writer.writeAttribute("name", objects[node]);
std::string typeName;
g_rprStringMapper.RPRMaterialType_id_to_string(type,typeName);
writer.writeAttribute("type", typeName);
for (int input_id = 0; input_id < input_count; ++input_id)
{
size_t read_count = 0;
uint32_t id = 0;
CHECK_NO_ERROR(rprMaterialNodeGetInputInfo(node, input_id, RPR_MATERIAL_NODE_INPUT_NAME, sizeof(uint32_t), &id, nullptr));
std::string param_name;
g_rprStringMapper.RPRMaterialInput_id_to_string(id,param_name);
if (!exportImageUV // if UV not exported
&& type == RPR_MATERIAL_NODE_IMAGE_TEXTURE
&& param_name.size() == 3 && param_name[0] == 'u' && param_name[1] == 'v' && param_name[2] == 0
)
{
continue; // ignore this parameter
}
std::string type = "";
std::string value = "";
rpr_material_node_type input_type;
CHECK_NO_ERROR(rprMaterialNodeGetInputInfo(node, input_id, RPR_MATERIAL_NODE_INPUT_TYPE, sizeof(input_type), &input_type, &read_count));
switch (input_type)
{
case RPR_MATERIAL_NODE_INPUT_TYPE_FLOAT4:
{
rpr_float fvalue[4] = { 0.f, 0.f, 0.f, 0.f };
CHECK_NO_ERROR(rprMaterialNodeGetInputInfo(node, input_id, RPR_MATERIAL_NODE_INPUT_VALUE, sizeof(fvalue), fvalue, nullptr));
//fvalue converted to string
std::stringstream ss;
ss << fvalue[0] << ", " <<
fvalue[1] << ", " <<
fvalue[2] << ", " <<
fvalue[3];
type = "float4";
value = ss.str();
break;
}
case RPR_MATERIAL_NODE_INPUT_TYPE_UINT:
{
rpr_int uivalue = RPR_SUCCESS;
CHECK_NO_ERROR(rprMaterialNodeGetInputInfo(node, input_id, RPR_MATERIAL_NODE_INPUT_VALUE, sizeof(uivalue), &uivalue, nullptr));
//value converted to string
type = "uint";
value = std::to_string(uivalue);
break;
}
case RPR_MATERIAL_NODE_INPUT_TYPE_NODE:
{
rpr_material_node connection = nullptr;
rpr_int res = rprMaterialNodeGetInputInfo(node, input_id, RPR_MATERIAL_NODE_INPUT_VALUE, sizeof(connection), &connection, nullptr);
CHECK_NO_ERROR(rprMaterialNodeGetInputInfo(node, input_id, RPR_MATERIAL_NODE_INPUT_VALUE, sizeof(connection), &connection, nullptr));
type = "connection";
if (!objects.count(connection) && connection)
{
throw std::runtime_error("input material node is missing");
}
value = objects[connection];
break;
}
case RPR_MATERIAL_NODE_INPUT_TYPE_IMAGE:
{
type = "connection";
rpr_image tex = nullptr;
CHECK_NO_ERROR(rprMaterialNodeGetInputInfo(node, input_id, RPR_MATERIAL_NODE_INPUT_VALUE, sizeof(tex), &tex, nullptr));
size_t name_size = 0;
CHECK_NO_ERROR(rprImageGetInfo(tex, RPR_OBJECT_NAME, NULL, nullptr, &name_size));
std::string tex_name;
tex_name.resize(name_size - 1);
CHECK_NO_ERROR(rprImageGetInfo(tex, RPR_OBJECT_NAME, name_size, &tex_name[0], nullptr));
// replace \\ by /
// so we ensure all paths are using same convention ( better for Unix )
for (int i = 0; i < tex_name.length(); i++)
{
if (tex_name[i] == '\\')
tex_name[i] = '/';
}
// we don't want path that looks like : "H:/RPR/MatLib/MaterialLibrary/1.0 - Copy/RadeonProRMaps/Glass_Used_normal.jpg"
// all paths must look like "RadeonProRMaps/Glass_Used_normal.jpg"
// the path RadeonProRMaps/ is hard coded here for the moment .. needs to be exposed in exporter GUI if we change it ?
const std::string radeonProRMapsFolder = "/RadeonProRMaps/";
size_t pos = tex_name.find(radeonProRMapsFolder);
if (pos != std::string::npos)
{
tex_name = tex_name.substr(pos + 1);
int a = 0;
}
if (!textures.count(tex_name))
{
int tex_node_id = 0;
std::string tex_node_name = objects[tex];
textures[tex_name] = std::pair<std::string, rpr_image>(tex_node_name, tex);
}
value = textures[tex_name].first;
break;
}
default:
throw std::runtime_error("unexpected material node input type " + std::to_string(input_type));
}
if (!value.empty())
{
writer.startElement("param");
writer.writeAttribute("name", param_name.data());
writer.writeAttribute("type", type);
writer.writeAttribute("value", value);
writer.endElement();
}
}
writer.endElement();
}
for (const auto& tex : textures)
{
writer.startElement("node");
writer.writeAttribute("name", tex.second.first);
writer.writeAttribute("type", "INPUT_TEXTURE");
writer.startElement("param");
writer.writeAttribute("name", "path");
writer.writeAttribute("type", "file_path");
writer.writeAttribute("value", tex.first);
writer.endElement();
writer.startElement("param");
writer.writeAttribute("name", "gamma");
writer.writeAttribute("type", "float");
bool gammaset = false;
if (textureParameter.find(tex.second.second) != textureParameter.end())
{
RPR_MATERIAL_XML_EXPORT_TEXTURE_PARAM& param = textureParameter[tex.second.second];
if (param.useGamma)
{
writer.writeAttribute("value", std::to_string(2.2f));
gammaset = true;
}
}
if (!gammaset)
{
writer.writeAttribute("value", std::to_string(1.0f));
}
writer.endElement();
if (extraImageData.find(tex.second.second) != extraImageData.end())
{
const EXTRA_IMAGE_DATA& extra = extraImageData.at(tex.second.second);
if (extra.scaleX != 1.0f)
{
writer.startElement("param");
writer.writeAttribute("name", "tiling_u");
writer.writeAttribute("type", "float");
writer.writeAttribute("value", std::to_string(extra.scaleX));
writer.endElement();
}
if (extra.scaleY != 1.0f)
{
writer.startElement("param");
writer.writeAttribute("name", "tiling_v");
writer.writeAttribute("type", "float");
writer.writeAttribute("value", std::to_string(extra.scaleY));
writer.endElement();
}
}
writer.endElement();
}
writer.endDocument();
}
catch (const std::exception& ex)
{
std::cout << "MaterialExport error: " << ex.what() << std::endl;
}
return;
}