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

Help Create Shader for Loading Textures

Discussion in 'Shaders' started by zee_ola05, Nov 18, 2017.

  1. zee_ola05

    zee_ola05

    Joined:
    Feb 2, 2014
    Posts:
    166
    I'm not good with shaders. But I need a really cheap shader that only loads a texture to the GPU and make ZERO processing, ZERO rendering. I just need this shader to force a texture into the GPU.

    Right now, I'm using the 'Standard' shader to load my texture to GPU, then turn my quad away from the camera to prevent it from being rendered to the screen. But I want to use a simpler shader and avoid back-face culling.

    Can anyone, maybe, post a snippet? or point me to the right direction? Would expositing a texture property from a shader script to the editor force that texture into the GPU when seen by camera? Thanks in advance!
     
  2. zee_ola05

    zee_ola05

    Joined:
    Feb 2, 2014
    Posts:
    166
    Code (CSharp):
    1. Shader "MyTest"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    6.     }
    7.  
    8.     SubShader
    9.     {
    10.         Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    11.      
    12.         Pass
    13.         {
    14.             CGPROGRAM
    15.             #pragma vertex vert          
    16.             #pragma fragment frag
    17.  
    18.             void vert()
    19.             {
    20.          
    21.             }
    22.  
    23.             void frag()
    24.             {
    25.          
    26.             }
    27.             ENDCG
    28.         }
    29.     }
    30. }
    31.  
    I created this shader. In the editor, it seems like it is doing what it should do (not do anything). However, when I make a Mac build, I get a NullReference error:
    Can anyone point me to the error in my shader? And pls feel free to improve/clean it.
     
  3. zee_ola05

    zee_ola05

    Joined:
    Feb 2, 2014
    Posts:
    166
    I inserted this to Line 17 because I think this is what would make this shader load my texture.
    Code (CSharp):
    1. sampler2D _MainTex;
    Is my assumption correct?

    Doesn't throw error in Editor. But gives me the same error when I make a Mac build.
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Your shader likely has to do something with the texture inside either the vert or frag functions, otherwise it may get optimized away and not be loaded. Additionally the vertex function must output a vertex position. Some shader compilers will deal with that by filling in something for you, but I suspect the error you're seeing is from a shader translator getting tripped up by it missing, though I guarantee the Apple shader compiler would have errored out if it'd gotten the chance.

    See this page:
    https://docs.unity3d.com/Manual/SL-VertexFragmentShaderExamples.html
    Scroll down to:

    Try that shader with just the added texture property and sampler2D lines to see if that works. You can have the frag function return a void, or use return 0; instead of a color. If that doesn't work you'll need to sample the texture in the frag function. However you can also hardcode the vert position to something guaranteed to be off screen, like return float(2,2,2,1); so the frag shader will never ever actually get executed, but it should still upload the texture.
     
  5. zee_ola05

    zee_ola05

    Joined:
    Feb 2, 2014
    Posts:
    166
    I fixed the NullReference error. It was caused by Unity stripping my shader because it is not being referenced by any asset in the scene. I load it using Shader.Find(). I simply put my shader to Resources folder to avoid stripping.

    No more errors, but the texture is not being loaded to GPU.

    @bgolus Thank you for response. I'm gonna try your suggestions.
     
  6. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I think you have to sample the texture and use the output. I remember on vita, that I was doing something like this, and the compiler or driver (I don't remember which) was entirely ignoring the existence of tex2D until the actual output of tex2D was used somewhere. Suspect compiler. So I think you'll have to use it or lose it.
     
  7. zee_ola05

    zee_ola05

    Joined:
    Feb 2, 2014
    Posts:
    166
    Code (CSharp):
    1. Shader "MyTest2"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.     }
    7.  
    8.     SubShader
    9.     {
    10.         Pass
    11.         {
    12.             CGPROGRAM
    13.             #pragma vertex vert          
    14.             #pragma fragment frag
    15.  
    16.             sampler2D _MainTex;
    17.  
    18.             struct v2f
    19.             {
    20.                 fixed4 position : SV_POSITION;
    21.             };
    22.             v2f vert()
    23.             {
    24.                 v2f o;
    25.                 o.position = fixed4(2,2,2,1);
    26.                 return o;
    27.             }
    28.             fixed4 frag() : COLOR
    29.             {
    30.                 fixed4 col = tex2D(_MainTex, float2(0,0));
    31.                 return col;
    32.             }
    33.             ENDCG
    34.         }
    35.     }
    36. }
    37.  
    @bgolus
    This works!!!
    @hippocoder Thanks, that seems like the case.

    How does this look? I just put a random float2(0,0) on frag() because it shouldn't be called anyway. I'd appreciate any feedback or tips. I don't really do any shader coding so any bits of learning is great. Thanks, again.