Difference between shaders using CGPROGRAM and ones that dont and where to find resources

so im trying to learn how to make hyper-optimized shaders for low end mobile devices and some examples of shaders I’ve seen written for mobile i notice do not use the CGPROGRAM or HLSL tag typically found in the Subshader or Pass of almost all shader tutorials on the internet.

The game has no lighting and instead we are using a combination of light mapping and colors of the objects to simulate lights which works great for us due to the cartoon style of the game. with that being said, Here is an example of an extremely efficient unlit shader that im using. and notice how it osnt using GCPROGRAM and is simply just uses a pass. Im trying to edit this shader to include a second texture that we can color separately from the primary texture to add glowing windows and lights at night. How would i do this without using HLSL?:

Shader "Unlit/Texture_color" {
Properties {
    _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    _Color ("Main Color", Color) = (1,1,1,1)
}

SubShader {
    Tags {"RenderType"="Opaque"}
    LOD 100
 
    ZWrite on

    Pass {
        Lighting Off
        SetTexture [_MainTex] {

            constantColor [_Color]

            //Combine texture * constant, texture * constant
            combine texture * constant DOUBLE

         }
    }
}
}

They used to be fixed function shaders from when GPUs were super basic. But now that code is just converted into a regular vert/frag shader. So you might think you’re getting more performance by using this but in reality it’s not, it’s just simpler looking code.

Here’s one of the Unity devs on this topic:
https://aras-p.info/blog/2015/04/27/optimizing-unity-renderer-3-fixed-function-removal/

oh so if i use CGPROGRAM in my code there wont be any difference?

I could have sworn that i read somewhere that using CGPROGRAM could be inefficient for mobile shaders? perhaps they simply meant inefficient in terms of time spent writing simple shaders lol.

Back in Unity 4.2 and older when targeting verrrry old mobile devices that still had fixed-function support it might have helped a bit, but it’s irrelevant now.

CGPROGRAM is just the block of “actual shader code” and is simply HLSL at this point, which is what your fixed function code is converted to. You can write HLSLPROGRAM instead and the code will work just the same inside too.

So yeah, just make a regular vert/frag shader that combines your textures, it won’t have lighting unless you implement it.

Unity no longer supports GPUs that only support fixed function shaders, and while they did indeed use to be faster in the early days of mobile GPUs switching over to more modern shaders, these days there’s no performance benefit. And in some cases they are potentially slower than hand written shaders. Last time I was doing mobile games (4+ years ago) I rewrote all of the shaders we used to not be fixed function since to get some perf back.

hmmm I see that’s very good to know! The blog I read must have been super old then. i suppose ill give all those shader tutorials using CGProgram a chance then :stuck_out_tongue:

thanks!

BTW I swear bgolus, every post I have seen on these forums since 2013, you have replied and helped answer issues… why aren’t you Unity staff yet? XD

1 Like

shrug

6 Likes