SIGGRAPH16 : NVIDIA BEST OF GTC MDL MATERIALS TO GLSL SHADERS - - PowerPoint PPT Presentation

siggraph 16 nvidia best of gtc
SMART_READER_LITE
LIVE PREVIEW

SIGGRAPH16 : NVIDIA BEST OF GTC MDL MATERIALS TO GLSL SHADERS - - PowerPoint PPT Presentation

April 4-7, 2016 | Silicon Valley SIGGRAPH16 : NVIDIA BEST OF GTC MDL MATERIALS TO GLSL SHADERS Andreas Senbach, NVIDIA Andreas Mank, ESI Group www.esi-group.com S6311 MDL Materials to GLSL Shaders . Theory and Practice.


slide-1
SLIDE 1

April 4-7, 2016 | Silicon Valley www.esi-group.com

Andreas Süßenbach, NVIDIA Andreas Mank, ESI Group

SIGGRAPH’16: NVIDIA BEST OF GTC

MDL MATERIALS TO GLSL SHADERS

slide-2
SLIDE 2

2

“S6311 MDL Materials to GLSL Shaders. Theory and Practice.”

http://on-demand.gputechconf.com/gtc/2016/video/S6311.html

slide-3
SLIDE 3

3

MDL IN CUSTOM RENDERERS

7/20/2016 20.07.2016

MATERIAL TWEAKING MATERIAL CONSTRUCTION

MATERIAL SHARING (LIBRARY)

NVIDIA IRAY

DEFINITION

MDL SDK

IMPLEMENTATION

slide-4
SLIDE 4

4

MATERIAL CREATION

slide-5
SLIDE 5

5

RASTERIZER VS. RAY TRACER

RAY TRACER RASTERIZER Shadows Reflections Walnut Silver Car paint

slide-6
SLIDE 6

6

RASTERIZER VS. RAY TRACER

RAY TRACER RASTERIZER Shadows Reflections Walnut Silver Car paint

slide-7
SLIDE 7

7

RASTERIZER VS. RAY TRACER

RAY TRACER RASTERIZER Shadows Reflections Walnut Silver Car paint

slide-8
SLIDE 8

8

RASTERIZER VS. RAY TRACER

RAY TRACER RASTERIZER Shadows Reflections Walnut Silver Car paint

slide-9
SLIDE 9

9

RASTERIZER VS. RAY TRACER

RAY TRACER RASTERIZER Shadows Reflections Walnut Silver Car paint

slide-10
SLIDE 10

10

RASTERIZER VS. RAY TRACER

RAY TRACER RASTERIZER Shadows Reflections Walnut Silver Car paint

slide-11
SLIDE 11

11

NEXT STEPS

Measured Materials and Photometric Lights

by courtesy of X-RITE

GTC EUROPE / AMSTERDAMM 28-29 SEP 2016

slide-12
SLIDE 12

12

AGENDA

What is MDL, what is the MDL SDK ? How do I use the MDL SDK ? How do I map MDL materials to GLSL shaders ?

slide-13
SLIDE 13

13

WHAT IS MDL, WHAT IS THE MDL SDK ?

slide-14
SLIDE 14

14

WHAT IS MDL ?

Declarative language to specify visual material properties No shader language ! Renderer agnostic

export material tintedStuff( color parTint = color(.6,.2,.2) ) = material ( surface: material_surface( scattering: df::specular_bsdf( tint: parTint ) ) );

slide-15
SLIDE 15

15

MDL MATERIAL FIELDS

material_surface: surface material_surface: backface material_geometry material_volume bool: thin_walled color: ior bsdf: scattering material_emission: emission bsdf: scattering material_emission: emission float3: displacement float: cutout_opacity float3: normal vdf: scattering

absorption_coefficient scattering_coefficient

slide-16
SLIDE 16

16

WHAT IS THE MDL SDK ?

A dynamic library providing a C++ API Used to load and compile MDL materials Can be used to create and modify materials as well Used to get detailed information out of the compiled material

slide-17
SLIDE 17

17

HOW DO I USE THE MDL SDK ?

slide-18
SLIDE 18

18

USING MDL SDK 1/5

Initializing Material Compiling Material Parsing Expression Parsing Call Parsing

HINSTANCE dll = LoadLibrary("libmdl_sdk.dll"); INeuray_factory* factory = (INeuray_factory*)GetProcAddress(dll,"mi_neuray_factory"); mi::base::Handle<mi::neuraylib::INeuray> neuray = factory(0,MI_NEURAYLIB_API_VERSION ); mi::base::Handle<mi::neuraylib::IMdl_compiler> mdlCompiler = neuray->get_api_component<mi::neuraylib::IMdl_compiler>();

slide-19
SLIDE 19

19

USING MDL SDK 2/5

Initializing Material Compiling Material Parsing Expression Parsing Call Parsing

mi::Sint32 reason = mdlCompiler->load_module ("::nvidia::vMaterials::AEC::Concrete::concrete_blocks"); mi::base::Handle<const mi::neuraylib::IModule> module( mdlCompiler->access<mi::neuraylib::IModule>( "mdl::nvidia::vMaterials::AEC::Concrete::concrete_blocks")); for ( mi::Size i=0 ; i<module->get_material_count() ; i++ ) { mi::base::Handle<mi::neuraylib::IMaterial_definition const> materialDefinition(mdlCompiler->access <mi::neuraylib::IMaterial_definition>(module->get_material(i)); mi::base::Handle<mi::neuraylib::IMaterial_instance> materialInstance (materialDefinition->create_material_instance(0,&result)); mi::base::Handle<mi::neuraylib::ICompiled_material> compiledMaterial (materialInstance->create_compiled_material (mi::neuraylib::IMaterial_instance::CLASS_COMPILATION,1.0f ,&result); }

slide-20
SLIDE 20

20

USING MDL SDK 3/5

Initializing Material Compiling Material Parsing Expression Parsing Call Parsing

for (mi::Size i=0; i<compiledMaterial->get_parameter_count(); i++) { char const* parameterName = compiledMaterial-> get_parameter_name(i); mi::base::Handle<mi::neuraylib::IValue const> value (compiledMaterial->get_argument(i)); } for (mi::Size i=0; i<compiledMaterial->get_temporary_count(); i++) { mi::base::Handle<mi::neuraylib::IExpression const> expression (compiledMaterial->get_temporary(i)); } mi::base::Handle<mi::neuraylib::IExpression const> surfaceExpression (compiledMaterial->get_field("surface")); mi::base::Handle<mi::neuraylib::IExpression const> backfaceExpression (compiledMaterial->get_field(„backface"));

slide-21
SLIDE 21

21

USING MDL SDK 4/5

Initializing Material Compiling Material Parsing Expression Parsing Call Parsing

switch( expression->get_kind() ) { case mi::neuraylib::IExpression::EK_CONSTANT: ... break; case mi::neuraylib::IExpression::EK_PARAMETER: ... break; case mi::neuraylib::IExpression::EK_TEMPORARY: ... break; case mi::neuraylib::IExpression::EK_DIRECT_CALL: ... break; }

slide-22
SLIDE 22

22

USING MDL SDK 5/5

Initializing Material Compiling Material Parsing Expression Parsing Call Parsing

mi::base::Handle<mi::neuraylib::IType const> type(call-> get_type()); mi::base::Handle<mi::neuraylib::IExpression_list const> arguments (call->get_arguments()); for (mi::Size i=0; i<arguments->get_size(); i++) { char const* name = arguments->get_name(i); mi::base::Handle<mi::neuraylib::IExpression const> argument (arguments->get_expression(i)); }

slide-23
SLIDE 23

23

MDL SDK: MAP MDL TO AN EXPRESSION TREE

MDL Material Expression Tree

export material tintedStuff( color parTint = color(.6,.2,.2) ) = material ( surface: material_surface( scattering: df::specular_bsdf( tint: parTint ) ) );

tintedStuff surface scattering emission backface scattering emission

slide-24
SLIDE 24

24

SIMPLIFY YOUR WORK: MDLTokenzier

Simple C++ class to derive from About 40 pure virtual functions (visitor pattern) Simple string and value based interface “Guides” you through the Expression tree of a material Hides all the intricacies of the MDL SDK If you just want to convert MDL Materials into something

slide-25
SLIDE 25

25

HOW DO I MAP MDL MATERIALS TO GLSL SHADERS ?

slide-26
SLIDE 26

26

MAPPING MDL TO GLSL

Function Calls GLSL Snippets

slide-27
SLIDE 27

27

float mdl_math_minValue( in vec3 a ) { return( min( min( a.x, a.y ), a.z ) ); }

GLSL SNIPPETS: FUNCTIONS

vec4 mdl_df_weightedLayer( in float weight, in vec4 layer, in vec4 base ) { return( mix( base, layer, weight ) ); }

slide-28
SLIDE 28

28

GLSL SNIPPETS: BSDF

slide-29
SLIDE 29

29

GLSL SNIPPETS

A snippet per BSDF (currently, there are 6 different) A snippet for each function (currently, there are about 50) A snippet for some commonly used helper functions

  • > about 80 snippets
slide-30
SLIDE 30

30

MAPPING MDL TO GLSL

Function Calls GLSL Snippets Expression Trees Stitched Snippets

slide-31
SLIDE 31

31

EXPRESSION TREE TO GLSL

functionCall arg0 nextCall X Y z yetAnotherCall nestedCall x y

float functionCall(...) {...} int nextCall(...) {...} float yetAnotherCall(...) {...} int nextedCall(...) {...} ... functionCall(arg0,nextCall(x,y,z),yetAnotherCall(nestedCall(x),y));

slide-32
SLIDE 32

32

GLSL SNIPPETS: WHAT TO DO WITH THEM ?

Fixed shader skeletons !! Call some functions in those skeletons Fill the bodies of those functions with some stitched snippets

  • > resembles kind of “virtual function calls”
slide-33
SLIDE 33

33 void main(void) { stateNormal = normalize(varNormal); if (! gl_FrontFacing) { stateNormal = - stateNormal;

} texCoord0 = varTexCoord0; tangent = normalize(varTangent); binormal = normalize(varBinormal); viewDir = normalize(varEyePos - varWorldPos); vec4 rgba = vec4(0.0f, 0.0f, 0.0f, 0.0f); if (0.0f < evalCutoutOpacity(stateNormal)) { vec3 normal = evalNormal(normal); materialIOR = evalIOR(normal); vec3 materialEmissive = vec3(0.0f, 0.0f, 0.0f); bool useFront = gl_FrontFacing; if (useFront) { materialEmissive = evalMaterialEmissiveFront(normal); } else { // there's no emission on the back-side, unless thinWalled is true useFront = !evalThinWalled(); if (!useFront) { materialEmissive = evalMaterialEmissiveBack(normal); } materialIOR = 1.0f / materialIOR; } rgba = vec4(materialEmissive, 0.0f); if (0 < sys_NumLights) { vec3 lightAmbient; for (int i=0; i<sys_NumLights; i++) { sampleLight(sys_Lights[i], varWorldPos, lightDir, lightAmbient, lightDiffuse, lightSpecular); rgba += useFront ? evalColorFront(normal) : evalColorBack(normal); } rgba.a /= sys_NumLights; } else { rgba.a = 1.0f; } rgba.a *= alphaCutout; if (0.0f < rgba.a) { lightDir = reflect(-viewDir, normal); rgba.rgb += (useFront ? evalEnvironmentFront(normal) : evalEnvironmentBack(normal)).rgb; } } emitColor(rgba); }

MASTER FRAGMENT SHADER

slide-34
SLIDE 34

34

float cutoutOpacity = evalCutoutOpacity(stateNormal); if (0.0f < cutoutOpacity) { vec3 normal = evalNormal(stateNormal); materialIOR = evalIOR(normal);

FRAGMENT SHADER DETAIL

slide-35
SLIDE 35

35

“VIRTUAL” FUNCTIONS USED

evalCutoutOpacity evalNormal evalIOR evalMaterialEmissiveFront evalMaterialEmissiveBack evalThinWalled evalColorFront evalColorBack evalEnironmentFront evalEnvironmentBack

slide-36
SLIDE 36

36

YOUR WORK

Implement the functions as GLSL snippets Create a set of shader skeletons that implement your shading algorithms Specify a set of “virtual” functions that get the material properties Fill the bodies of those functions with the glsl code generated from the expression trees

slide-37
SLIDE 37

37

CONCLUSION

One shader per material The complexity of the shader is strongly related to the complexity of the material Separates shading algorithm from material evaluation

slide-38
SLIDE 38

April 4-7, 2016 | Silicon Valley www.esi-group.com

THANK YOU