Material Definition Language API nvidia_logo_transpbg.gif Up
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
definition_wrapper.h
Go to the documentation of this file.
1 /***************************************************************************************************
2  * Copyright 2018 NVIDIA Corporation. All rights reserved.
3  **************************************************************************************************/
6 
7 #ifndef MI_NEURAYLIB_DEFINITION_WRAPPER_H
8 #define MI_NEURAYLIB_DEFINITION_WRAPPER_H
9 
10 #include <mi/base/handle.h>
11 #include <mi/neuraylib/assert.h>
19 #include <mi/neuraylib/itype.h>
20 #include <mi/neuraylib/ivalue.h>
21 
22 #include <string>
23 
24 namespace mi {
25 
26 namespace neuraylib {
27 
32 class Definition_wrapper
43 {
44 public:
45 
47 
48 
56  Definition_wrapper( ITransaction* transaction, const char* name, IMdl_factory* mdl_factory);
57 
63  bool is_valid() const;
64 
71  Element_type get_type() const;
72 
74  const char* get_mdl_definition() const;
75 
77  const char* get_module() const;
78 
80  bool is_exported() const;
81 
83  Size get_parameter_count() const;
84 
89  const char* get_parameter_name( Size index) const;
90 
95  Size get_parameter_index( const char* name) const;
96 
98  const IType_list* get_parameter_types() const;
99 
103  const IType* get_return_type() const;
104 
112  const char* get_thumbnail() const;
113 
114 
116 
118 
124  const IExpression_list* get_defaults() const;
125 
143  template<class T>
144  Sint32 get_default( Size index, T& value) const;
145 
159  template <class T>
160  Sint32 get_default( const char* name, T& value) const;
161 
163 
165 
167  const IAnnotation_block* get_annotations() const;
168 
175  const IAnnotation_list* get_parameter_annotations() const;
176 
181  const IAnnotation_block* get_return_annotations() const;
182 
189  const IExpression_list* get_enable_if_conditions() const;
190 
197  Size get_enable_if_users( Size index) const;
198 
206  Size get_enable_if_user( Size index, Size u_index) const;
207 
209 
211 
230  IScene_element* create_instance(
231  const IExpression_list* arguments = 0, Sint32* errors = 0) const;
232 
254  template <class T>
255  T* create_instance( const IExpression_list* arguments = 0, Sint32* errors = 0) const
256  {
257  IScene_element* ptr_iscene_element = create_instance( arguments, errors);
258  if ( !ptr_iscene_element)
259  return 0;
260  T* ptr_T = static_cast<T*>( ptr_iscene_element->get_interface( typename T::IID()));
261  ptr_iscene_element->release();
262  return ptr_T;
263  }
264 
266 
268 
270  ITransaction* get_transaction() const;
271 
273  IMdl_factory* get_mdl_factory() const;
274 
276  const IScene_element* get_scene_element() const;
277 
280 
282  const std::string& get_name() const;
283 
285 
286 private:
287 
288  base::Handle<ITransaction> m_transaction;
290  base::Handle<IMdl_factory> m_mdl_factory;
291  Element_type m_type;
292  std::string m_name;
293 };
294  // end group mi_neuray_mdl_elements
296 
298  ITransaction* transaction, const char* name, IMdl_factory* mdl_factory)
299 {
300  mi_neuray_assert( transaction);
301  mi_neuray_assert( name);
302 
303  m_transaction = make_handle_dup( transaction);
304  m_name = name;
305  m_mdl_factory = make_handle_dup( mdl_factory);
306  m_access = transaction->access<IScene_element>( name);
307  m_type = m_access ? m_access->get_element_type() : static_cast<Element_type>( 0);
308 }
309 
310 inline bool Definition_wrapper::is_valid() const
311 {
312  return m_access
314  || m_type == ELEMENT_TYPE_FUNCTION_DEFINITION);
315 }
316 
318 {
319  return m_type;
320 }
321 
322 inline const char* Definition_wrapper::get_mdl_definition() const
323 {
324  if( m_type == ELEMENT_TYPE_MATERIAL_DEFINITION) {
325 
327  m_access->get_interface<IMaterial_definition>());
328  return md->get_mdl_name();
329 
330  } else if( m_type == ELEMENT_TYPE_FUNCTION_DEFINITION) {
331 
333  m_access->get_interface<IFunction_definition>());
334  return fd->get_mdl_name();
335 
336  } else
337  return 0;
338 }
339 
340 inline const char* Definition_wrapper::get_module() const
341 {
342  if( m_type == ELEMENT_TYPE_MATERIAL_DEFINITION) {
343 
345  m_access->get_interface<IMaterial_definition>());
346  return md->get_module();
347 
348  } else if( m_type == ELEMENT_TYPE_FUNCTION_DEFINITION) {
349 
351  m_access->get_interface<IFunction_definition>());
352  return fd->get_module();
353 
354  } else
355  return 0;
356 }
357 
359 {
360  if( m_type == ELEMENT_TYPE_MATERIAL_DEFINITION) {
361 
363  m_access->get_interface<IMaterial_definition>());
364  return md->is_exported();
365 
366  } else if( m_type == ELEMENT_TYPE_FUNCTION_DEFINITION) {
367 
369  m_access->get_interface<IFunction_definition>());
370  return fd->is_exported();
371 
372  } else
373  return false;
374 }
375 
377 {
378  if( m_type == ELEMENT_TYPE_MATERIAL_DEFINITION) {
379 
381  m_access->get_interface<IMaterial_definition>());
382  return md->get_parameter_count();
383 
384  } else if( m_type == ELEMENT_TYPE_FUNCTION_DEFINITION) {
385 
387  m_access->get_interface<IFunction_definition>());
388  return fd->get_parameter_count();
389 
390  } else
391  return 0;
392 }
393 
394 inline const char* Definition_wrapper::get_parameter_name( Size index) const
395 {
396  if( m_type == ELEMENT_TYPE_MATERIAL_DEFINITION) {
397 
399  m_access->get_interface<IMaterial_definition>());
400  return md->get_parameter_name( index);
401 
402  } else if( m_type == ELEMENT_TYPE_FUNCTION_DEFINITION) {
403 
405  m_access->get_interface<IFunction_definition>());
406  return fd->get_parameter_name( index);
407 
408  } else
409  return 0;
410 }
411 
412 inline Size Definition_wrapper::get_parameter_index( const char* name) const
413 {
414  if( m_type == ELEMENT_TYPE_MATERIAL_DEFINITION) {
415 
417  m_access->get_interface<IMaterial_definition>());
418  return md->get_parameter_index( name);
419 
420  } else if( m_type == ELEMENT_TYPE_FUNCTION_DEFINITION) {
421 
423  m_access->get_interface<IFunction_definition>());
424  return fd->get_parameter_index( name);
425 
426  } else
427  return 0;
428 }
429 
431 {
432  if( m_type == ELEMENT_TYPE_MATERIAL_DEFINITION) {
433 
435  m_access->get_interface<IMaterial_definition>());
436  return md->get_parameter_types();
437 
438  } else if( m_type == ELEMENT_TYPE_FUNCTION_DEFINITION) {
439 
441  m_access->get_interface<IFunction_definition>());
442  return fd->get_parameter_types();
443 
444  } else
445  return 0;
446 }
447 
449 {
450  if( m_type == ELEMENT_TYPE_MATERIAL_DEFINITION) {
451 
452  return 0;
453 
454  } else if( m_type == ELEMENT_TYPE_FUNCTION_DEFINITION) {
455 
457  m_access->get_interface<IFunction_definition>());
458  return fd->get_return_type();
459 
460  } else
461  return 0;
462 }
463 
464 inline const char* Definition_wrapper::get_thumbnail() const
465 {
466  if (m_type == ELEMENT_TYPE_MATERIAL_DEFINITION) {
467 
469  m_access->get_interface<IMaterial_definition>());
470  return md->get_thumbnail();
471 
472  }
473  else if (m_type == ELEMENT_TYPE_FUNCTION_DEFINITION) {
474 
476  m_access->get_interface<IFunction_definition>());
477  return fd->get_thumbnail();
478 
479  }
480  else
481  return 0;
482 }
483 
485 {
486  if( m_type == ELEMENT_TYPE_MATERIAL_DEFINITION) {
487 
489  m_access->get_interface<IMaterial_definition>());
490  return md->get_defaults();
491 
492  } else if( m_type == ELEMENT_TYPE_FUNCTION_DEFINITION) {
493 
495  m_access->get_interface<IFunction_definition>());
496  return fd->get_defaults();
497 
498  } else
499  return 0;
500 }
501 
502 template <class T>
504 {
505  if( m_type == ELEMENT_TYPE_MATERIAL_DEFINITION) {
506 
508  m_access->get_interface<IMaterial_definition>());
509  base::Handle<const IExpression_list> defaults( md->get_defaults());
510  base::Handle<const IExpression> default_( defaults->get_expression( index));
511  if( !default_)
512  return -2;
514  default_->get_interface<IExpression_constant>());
515  if( !default_constant)
516  return -4;
517  base::Handle<const IValue> default_value( default_constant->get_value());
518  Sint32 result = get_value( default_value.get(), value);
519  return result == 0 ? 0 : -5;
520 
521  } else if( m_type == ELEMENT_TYPE_FUNCTION_DEFINITION) {
522 
524  m_access->get_interface<IFunction_definition>());
525  base::Handle<const IExpression_list> defaults( fd->get_defaults());
526  base::Handle<const IExpression> default_( defaults->get_expression( index));
527  if( !default_)
528  return -2;
530  default_->get_interface<IExpression_constant>());
531  if( !default_constant)
532  return -4;
533  base::Handle<const IValue> default_value( default_constant->get_value());
534  Sint32 result = get_value( default_value.get(), value);
535  return result == 0 ? 0 : -5;
536 
537  } else
538  return -1;
539 }
540 
541 template <class T>
542 Sint32 Definition_wrapper::get_default( const char* name, T& value) const
543 {
544  if( m_type == ELEMENT_TYPE_MATERIAL_DEFINITION) {
545 
547  m_access->get_interface<IMaterial_definition>());
548  base::Handle<const IExpression_list> defaults( md->get_defaults());
549  base::Handle<const IExpression> default_( defaults->get_expression( name));
550  if( !default_)
551  return -2;
553  default_->get_interface<IExpression_constant>());
554  if( !default_constant)
555  return -4;
556  base::Handle<const IValue> default_value( default_constant->get_value());
557  Sint32 result = get_value( default_value.get(), value);
558  return result == 0 ? 0 : -5;
559 
560  } else if( m_type == ELEMENT_TYPE_FUNCTION_DEFINITION) {
561 
563  m_access->get_interface<IFunction_definition>());
564  base::Handle<const IExpression_list> defaults( fd->get_defaults());
565  base::Handle<const IExpression> default_( defaults->get_expression( name));
566  if( !default_)
567  return -2;
569  default_->get_interface<IExpression_constant>());
570  if( !default_constant)
571  return -4;
572  base::Handle<const IValue> default_value( default_constant->get_value());
573  Sint32 result = get_value( default_value.get(), value);
574  return result == 0 ? 0 : -5;
575 
576  } else
577  return -1;
578 }
579 
581 {
582  if( m_type == ELEMENT_TYPE_MATERIAL_DEFINITION) {
583 
585  m_access->get_interface<IMaterial_definition>());
586  return md->get_annotations();
587 
588  } else if( m_type == ELEMENT_TYPE_FUNCTION_DEFINITION) {
589 
591  m_access->get_interface<IFunction_definition>());
592  return fd->get_annotations();
593 
594  } else
595  return 0;
596 }
597 
599 {
600  if( m_type == ELEMENT_TYPE_MATERIAL_DEFINITION) {
601 
603  m_access->get_interface<IMaterial_definition>());
604  return md->get_parameter_annotations();
605 
606  } else if( m_type == ELEMENT_TYPE_FUNCTION_DEFINITION) {
607 
609  m_access->get_interface<IFunction_definition>());
610  return fd->get_parameter_annotations();
611 
612  } else
613  return 0;
614 }
615 
617 {
618  if( m_type == ELEMENT_TYPE_MATERIAL_DEFINITION) {
619 
620  return 0;
621 
622  } else if( m_type == ELEMENT_TYPE_FUNCTION_DEFINITION) {
623 
625  m_access->get_interface<IFunction_definition>());
626  return fd->get_return_annotations();
627 
628  } else
629  return 0;
630 }
631 
633 {
634  if( m_type == ELEMENT_TYPE_MATERIAL_DEFINITION) {
635 
637  m_access->get_interface<IMaterial_definition>());
638  return md->get_enable_if_conditions();
639 
640  }
641  else if( m_type == ELEMENT_TYPE_FUNCTION_DEFINITION) {
642 
644  m_access->get_interface<IFunction_definition>());
645  return fd->get_enable_if_conditions();
646 
647  } else
648  return 0;
649 }
650 
652 {
653  if( m_type == ELEMENT_TYPE_MATERIAL_DEFINITION) {
654 
656  m_access->get_interface<IMaterial_definition>());
657  return md->get_enable_if_users( index);
658 
659  }
660  else if( m_type == ELEMENT_TYPE_FUNCTION_DEFINITION) {
661 
663  m_access->get_interface<IFunction_definition>());
664  return fd->get_enable_if_users( index);
665 
666  } else
667  return ~0;
668 }
669 
671 {
672  if( m_type == ELEMENT_TYPE_MATERIAL_DEFINITION) {
673 
675  m_access->get_interface<IMaterial_definition>());
676  return md->get_enable_if_user( index, u_index);
677 
678  }
679  else if( m_type == ELEMENT_TYPE_FUNCTION_DEFINITION) {
680 
682  m_access->get_interface<IFunction_definition>());
683  return fd->get_enable_if_user( index, u_index);
684 
685  } else
686  return ~0;
687 }
688 
690  const IExpression_list* arguments, Sint32* errors) const
691 {
692  if( m_type == ELEMENT_TYPE_MATERIAL_DEFINITION) {
693 
695  m_access->get_interface<IMaterial_definition>());
696  if( arguments)
697  return md->create_material_instance( arguments, errors);
698 
699  base::Handle<const IType_list> parameter_types( md->get_parameter_types());
700  base::Handle<const IExpression_list> defaults( md->get_defaults());
701  base::Handle<IValue_factory> vf( m_mdl_factory->create_value_factory( m_transaction.get()));
703  m_mdl_factory->create_expression_factory( m_transaction.get()));
704  base::Handle<IExpression_list> local_arguments( ef->create_expression_list());
705 
706  Size count = md->get_parameter_count();
707  for( Size i = 0; i < count; ++i) {
708  const char* name = md->get_parameter_name( i);
709  base::Handle<const IExpression> default_( defaults->get_expression( name));
710  if( !default_) {
711  base::Handle<const IType> type( parameter_types->get_type( i));
712  base::Handle<IValue> value( vf->create( type.get()));
713  base::Handle<IExpression> expr( ef->create_constant( value.get()));
714  local_arguments->add_expression( name, expr.get());
715  }
716  }
717  return md->create_material_instance( local_arguments.get(), errors);
718 
719  } else if( m_type == ELEMENT_TYPE_FUNCTION_DEFINITION) {
720 
722  m_access->get_interface<IFunction_definition>());
723  if( arguments)
724  return fd->create_function_call( arguments, errors);
725 
726  base::Handle<const IType_list> parameter_types( fd->get_parameter_types());
727  base::Handle<const IExpression_list> defaults( fd->get_defaults());
728  base::Handle<IValue_factory> vf( m_mdl_factory->create_value_factory( m_transaction.get()));
730  m_mdl_factory->create_expression_factory( m_transaction.get()));
731  base::Handle<IExpression_list> local_arguments( ef->create_expression_list());
732 
733  Size count = fd->get_parameter_count();
734  for( Size i = 0; i < count; ++i) {
735  const char* name = fd->get_parameter_name( i);
736  base::Handle<const IExpression> default_( defaults->get_expression( name));
737  if( !default_) {
738  base::Handle<const IType> type( parameter_types->get_type( i));
739  base::Handle<IValue> value( vf->create( type.get()));
740  base::Handle<IExpression> expr( ef->create_constant( value.get()));
741  local_arguments->add_expression( name, expr.get());
742  }
743  }
744  return fd->create_function_call( local_arguments.get(), errors);
745 
746  } else
747  return 0;
748 }
749 
751 {
752  m_transaction->retain();
753  return m_transaction.get();
754 }
755 
757 {
758  m_mdl_factory->retain();
759  return m_mdl_factory.get();
760 }
761 
763 {
764  m_access->retain();
765  return m_access.get();
766 }
767 
769 {
770  return m_type;
771 }
772 
774 {
775  return m_name;
776 }
777 
778 } // namespace neuraylib
779 
780 } // namespace mi
781 
782 #endif // MI_NEURAYLIB_DEFINITION_WRAPPER_H