Search Unity

texture limit in 3.0?

Discussion in 'Shaders' started by multivac, Nov 3, 2009.

  1. multivac

    multivac

    Joined:
    Oct 27, 2009
    Posts:
    133
    Hi,I'm trying to translate one of my shaders from HLSL to Cg,but it seems that I'm not able to use more than 8 textures in a single pass?
    In HLSL I used a total of 16,so the number of texture units on my card must be sufficient for more...

    Here's the shader code,when adding additional textures,i get some vague error.Try uncommenting one of the lines in frag portion to see what i mean.
    Am I missing something?

    Code (csharp):
    1. Shader "Terrain/ThreeLayer Bumped" {
    2.  
    3. Properties {
    4.     _Color ("Main Color", Color) = (1,1,1,1)
    5.     _Shadow ("AO shadow", Color) = (1,1,1,1)
    6.      _Roughness ("Roughness", Range (0.0, 1.0)) = 0.0
    7.      // _Burn ("Lightmap multiplier", Range (0.0, 2.0)) = 1.3
    8.         _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
    9.     _Shininess ("Shininess", Range (0.01, 1)) = 0.078125
    10.     _Parallax ("Height", Range (0.005, 0.08)) = 0.02
    11.     _MainTex ("Base (RGBA)", 2D) = "white" {}
    12.       _MainTex_b ("Bumpmap (RGBA)", 2D) = "bump" {}
    13.     _MainTex2 ("Base 2 (RGBA)", 2D) = "white" {}
    14.           _MainTex2_b ("Bumpmap 2 (RGBA)", 2D) = "bump" {}
    15.     _MainTex3 ("Base 3 (RGBA)", 2D) = "white" {}
    16.           _MainTex3_b ("Bumpmap 3 (RGBA)", 2D) = "bump" {}
    17.           _MainTex4 ("Base 4 (RGBA)", 2D) = "white" {}
    18.           _MainTex4_b ("Bumpmap 4 (RGBA)", 2D) = "bump" {}
    19.           _lightmap ("Lightmap", 2D) = "white" {}
    20.     _Mask ("Mix Mask (RGBA)", 2D) = "gray" {}
    21. }
    22.  
    23.  
    24. Category {
    25.  
    26. Tags{   "RenderType"="Opaque"}
    27.  
    28. Blend AppSrcAdd AppDstAdd
    29.        
    30.      // Cull Back
    31.     SubShader {
    32.    
    33.             UsePass "Diffuse/BASE"
    34.  
    35.         Pass {
    36.             Name "Blend_Layers"
    37.             Tags { "LightMode" = "Pixel"}
    38.              Color [_PPLAmbient]
    39.           Material { Diffuse [_Color]      
    40.   }
    41.  
    42.                
    43. CGPROGRAM
    44. #pragma target 3.0
    45. #pragma vertex vert
    46. #pragma fragment frag
    47. #pragma fragmentoption ARB_fog_exp2
    48. #pragma fragmentoption ARB_precision_hint_fastest
    49. #pragma multi_compile_builtin
    50. #include "UnityCG.cginc"
    51. #include "AutoLight.cginc"
    52.  
    53.  
    54.  
    55.  
    56. struct v2f {
    57.  
    58.     V2F_POS_FOG;
    59.     LIGHTING_COORDS
    60.    half3  uv;
    61.     half2 uv2 ;
    62.         half3   viewDirT;
    63.    half3  lightDirT;
    64.  
    65. };
    66. half4 _MainTex_ST,_Mask_ST;
    67.  uniform half _Shininess;
    68.  
    69. v2f vert (appdata_tan v)
    70. {
    71.     v2f o;
    72.     PositionFog( v.vertex, o.pos, o.fog );
    73.     o.uv.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
    74.     o.uv.z = _Shininess * 128;
    75.     o.uv2 = TRANSFORM_TEX(v.texcoord, _Mask);
    76.     TANGENT_SPACE_ROTATION;
    77.     o.viewDirT = mul( rotation, ObjSpaceViewDir( v.vertex ) ); 
    78.     o.lightDirT = mul( rotation, ObjSpaceLightDir( v.vertex ) );    
    79.     TRANSFER_VERTEX_TO_FRAGMENT(o);
    80.     return o;
    81. }
    82.  
    83. uniform sampler2D _MainTex;
    84. uniform sampler2D _MainTex2;
    85. uniform sampler2D _MainTex3;
    86. uniform sampler2D _MainTex4;
    87. uniform sampler2D _MainTex_b;
    88. uniform sampler2D _MainTex2_b;
    89. uniform sampler2D _MainTex3_b;
    90. uniform sampler2D _MainTex4_b;
    91. uniform sampler2D _lightmap;
    92. uniform sampler2D _Mask;
    93. uniform half _Parallax;
    94. uniform half _Roughness;
    95. uniform half3 _Shadow;
    96. //uniform half _Burn;
    97.  
    98.  
    99.  
    100. float4 frag (v2f i) : COLOR
    101. {
    102.  
    103. half2 uv=1-(i.uv.xy);
    104.    half4 mix = tex2D(_Mask,i.uv2);
    105.                                
    106.  
    107.     half n1 = tex2D(_MainTex_b, uv).a  ;
    108.     half n2 = tex2D(_MainTex2_b, uv).a  ;
    109.     half n3 = tex2D(_MainTex3_b, uv).a ;
    110.     half n4 = tex2D(_MainTex4_b, uv).a ;
    111.  
    112.     half result=n1;
    113.     result=((result*(1-mix.r)) + n4*(mix.r));
    114.     result=((result*(1-mix.g)) + n2*(mix.g));
    115.     result=((result*(1-mix.b))+n3*(mix.b));
    116.  
    117.     half2 offset1 = ParallaxOffset( result, -_Parallax, i.viewDirT )+uv;
    118.     half2 offset2 = ParallaxOffset( result, -_Parallax, i.viewDirT )+uv;
    119.     half2 offset3 = ParallaxOffset( result, -_Parallax, i.viewDirT )+uv;
    120.         half2 offset4 = ParallaxOffset( result, -_Parallax, i.viewDirT )+uv;
    121.        
    122.     half3 normal1 = tex2D(_MainTex_b, (offset1)).rgb * 2 -1;
    123.     half3 normal2 = tex2D(_MainTex2_b, (offset2)).rgb * 2 -1;
    124.     half3 normal3 = tex2D(_MainTex3_b, (offset3)).rgb * 2 -1;
    125.         half3 normal4 = tex2D(_MainTex4_b, (offset4)).rgb * 2 -1;
    126.  
    127.     half3 resultn=normal1;
    128.     //resultn=  ((resultn*(1-mix.r))+normal4*(mix.r));
    129.     resultn=  ((resultn*(1-mix.g)) + normal2*(mix.g));
    130.     resultn=  ((resultn*(1-mix.b))+normal3*(mix.b));
    131.  
    132.  
    133.  
    134.     half4 col1 = tex2D(_MainTex,offset1);
    135.     half4 col2 = tex2D(_MainTex2,offset2);
    136.     half4 col3 = tex2D(_MainTex3,offset3);
    137.     //half4 col4 = tex2D(_MainTex4,offset4);
    138.    
    139.    
    140.     half3 lightmap = tex2D(_lightmap,(i.uv2)).rgb;
    141.        
    142.     half4 result1=col1;
    143.     //result1=  ((result1*(1-mix.r)) + col4*(mix.r));
    144.     result1=  ((result1*(1-mix.g)) + col2*(mix.g));
    145.     result1=  ((result1*(1-mix.b))+col3*(mix.b));
    146.     result1.rgb *=1.2*lightmap;
    147.    
    148.     result1.rgb=lerp(result1.rgb,result1.rgb*_Shadow,((1-result)+resultn.r+resultn.g)*_Roughness);
    149.    
    150.    
    151.     return SpecularLight( i.lightDirT, i.viewDirT,resultn, result1,i.uv.z-(result1.rgb*_Shadow), LIGHT_ATTENUATION(i) );
    152. }
    153. ENDCG              
    154.         }
    155.         }
    156.         }
    157.    
    158.  
    159.   Fallback "Diffuse", 1
    160.  
    161. }
     
  2. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    apple_motion definitely made a shader that uses nine textures. That said, there are definitely some unanswered questions about Unity's texture limits, or how it interacts with different cards.
     
  3. shawn

    shawn

    Unity Technologies

    Joined:
    Aug 4, 2007
    Posts:
    552
    What's the error you are getting exactly. I'm running into a texture indirection limit error which is specific to ATI cards. I'm not sure that's the same error that you're seeing though.

    You should probably look into fixing the texture indirection problem even if it's not an error you're seeing though. (Do a search for Texture Indirection here for an explanation: http://oss.sgi.com/projects/ogl-sample/registry/ARB/fragment_program.txt) ATI cards have a limit of 4.
     
  4. multivac

    multivac

    Joined:
    Oct 27, 2009
    Posts:
    133
    I did run into texture indirection error,but from what i've read on the forum,its an OpenGL problem.
    So i added some code to compile only to D3D9:
    Code (csharp):
    1.  
    2. #pragma profileoption MaxTexIndirections=16
    3. #pragma only_renderers d3d9
    4. #pragma exclude_renderers opengl
    5.  
    Now i do get 10 textures on the object,but also get a constant bad texcoord index error.Plus,when shadows are enabled,my object turns black and i get a hall-of-mirrors effect when zoomed close to it.