Add Custom shading model

Hello there!

Is there any information or documentation about adding own shading model??

I want to add couple of shaders for mobiles, as epic ones are pretty expensive, especially on old mobile devices.

A got some statistics… In one project unreal gives me 10 - 15 fps on mali400, while unity project with same graphics (even better) works on same device with stable 60fps. My guess is that epics shading model, especially PBR, is very expensive. Unreal even does not have default phong, wich is faster and can be more suitable for mobile (we do not see any physically correct shading on mobile anyway due to limitations and small screen). I checked, even Unlit flat shader shows me >20 instuctions. WTF? It should just “return EmmisiveColorParameter;”.

For some tests I made own shading based on Unlit with phong shading and basic shadows… Its rough now, but a but less instructions. Without those 20 unlit instructions it should be like 30.

In my mobile project I also made own shading based on unlit, but without phong reflection and shadows. UME shows me 35 instructions (including shading, Postprocessing effects and some vertex painting). I DO NOT use textures, only unlit shaders with my own shading. And I get 10 - 15 fps on Mali400 (samsung galaxy S3) -__-.

So… My target is to make really clean shading models, that are suitable for old mobiles. Unfortunatelly, unreal cannot make it this easy as Unity. I know I shoud edit sources. But there is no information about it. The only info I found is in Documentation folder near sources. But it’s still not enough.

I’m not as good in engine sources, so I’m looking step by step tutorial.

P.S. I know shaders are not only one, that slows those frames per second. Mesh render and drawcall handling are also a bottleneck in unreal. Made some tests… Empty scene with no light or sky, flat black scene with 50 cubes 1k polys each give me 30fps on Mali400. Nomatter instances they are (1 drawcall) or own meshes. That is VERY bad and making Unreal only suitable for middle or high-end devices.

Sadly there is little to no documentation in this regard, so certainly don’t expect a thorough step by step tutorial to exist.

My approach to understand it was the following:

  • Dive into the various shader source files to understand what happens where. To start I replacing various parts with single colors, just to see when what is called and when.
  • To find out how to properly integrate it with the material editor and to transfer parameters to your shaders I mostly did full text searches on the engine source based on parameters in existing shaders with distinctive names. (you don’t want to find all references to normal for example…)
  • You can force unreal to save the various intermediate files that are generated in the compilation process. This should allow you to analyze where the actual instruction count comes from. In your specific case with an unlit shader, this could for example be fogging that should also be applied to unlit surfaces.

My approach is similar. MobileBasePassPixelShader.usf is to place to go. Most expensive part of this shader is dynamic shadows. There is currently bug that cause max cascades to be always 4. There is fix for this. Or you can simply hard code the number if you know it. You can also save lots of performance by not computing dynamic shadows when distance is larger than max shadow distance, Normal is pointing outwards from directional light or if pixel is already fully in static shadows.
On CPU side one could test if object bounding box is outside of shadow max range and then use shader permutatation without dynamic shadows.

Cool, this shader is awesome! could you please tell me exactly what the Pos Parameter and Texture Sample are? Thanks in advance!

Are these previous discussions any help?

Hello !

I successfully create a new Shading model but the way to reach it is very painfull xD
I need to know if the new shader creation using plugin system could do the job instead of chaning the Engine ?

Good luck and thank you !