Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'Shaders' started by Galactic_Muffin, Jan 21, 2020.

  1. Galactic_Muffin

    Galactic_Muffin

    Joined:
    Aug 14, 2013
    Posts:
    67
    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?:

    Code (CSharp):
    1. Shader "Unlit/Texture_color" {
    2. Properties {
    3.     _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    4.     _Color ("Main Color", Color) = (1,1,1,1)
    5. }
    6.  
    7. SubShader {
    8.     Tags {"RenderType"="Opaque"}
    9.     LOD 100
    10.  
    11.     ZWrite on
    12.  
    13.     Pass {
    14.         Lighting Off
    15.         SetTexture [_MainTex] {
    16.  
    17.             constantColor [_Color]
    18.  
    19.             //Combine texture * constant, texture * constant
    20.             combine texture * constant DOUBLE
    21.  
    22.          }
    23.     }
    24. }
    25. }
     
    Last edited: Jan 21, 2020
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,539
  3. Galactic_Muffin

    Galactic_Muffin

    Joined:
    Aug 14, 2013
    Posts:
    67
    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.
     
    Last edited: Jan 21, 2020
  4. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,539
    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.
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    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.
     
  6. Galactic_Muffin

    Galactic_Muffin

    Joined:
    Aug 14, 2013
    Posts:
    67
    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 :p

    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
     
    jacobian likes this.
  7. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329