Compiling for shader model 3.0

I am working on a shader which is meant to tile textures over a lot of meshes. As I want it to get all textures from a texture atlas, I have to use ddx() and ddy(), to get correct interpolation.


(An ugly screenshot showing the problem without, to make this post a bit less boring)

I have no problems with the code, the problem is that Unity keeps compiling my shader for shadermodel 2.0, but those two function are not supported by 2.0.
If I understand it correctly, I can only use 2.0 or 3.0 and nothing in between, but 3.0 should work by setting #pragma target 3.0.
It seems to be just ignored, as Unity keeps telling me that those functions aren´t supported and that the fragment shader is SM2.0.

This is ennoying and I don´t see what I could be doing wrong.

This is the code I´ve got so far in case it helps (I still have to add three sided projection and some other small things):

Shader "World Multi Texturing"
{
	Properties
	{
		_MainTex ("Atlas (RGB)", 2D) = "white" {}
		_BlendTex ("Blend (RGBA)", 2D) = "black" {}
		_LightMap ("Light (RGB)", 2D) = "white" {}
	}
	
	SubShader
	{
		Pass
		{
			Lighting On
		
			CGPROGRAM
				#pragma target 3.0
				#pragma vertex vert
				#pragma fragment frag
				
				#include "UnityCG.cginc"
				
				uniform sampler2D _MainTex;
				uniform sampler2D _BlendTex;
				uniform sampler2D _LightMap;
				
				struct v2f
				{
					float4 position : POSITION;
					float3 normal : COLOR;
					float2 texblend : TEXCOORD0;
					float3 worldpos : TEXCOORD1;
				}; 

				v2f vert(appdata_base In)
				{
					v2f Out;
					
					Out.position = mul(glstate.matrix.mvp, In.vertex);
					Out.normal = mul(_Object2World, float4(In.normal, 0.0f)).xyz;
					
					Out.worldpos = mul(_Object2World, In.vertex);
					Out.texblend = (Out.worldpos.xz+4.0f)/256.0f;
					
					return Out;
				}
			   
				float4 frag(v2f In) : COLOR
				{
					float2 s = float2(10.0f, -10.0f);
					
					float2 tex0 = frac(In.worldpos.xy/s);
					float2 tex1 = In.worldpos.zx/s;
					float2 tex2 = frac(In.worldpos.zy/s);
					
					float ddx_ = ddx(tex1);
					float ddy_ = ddy(tex1);
					tex1 = frac(tex1);
					
					float4 color0 = tex2D(_MainTex, tex1*float2(0.21875, 0.4375)+float2(0.015625, 0.53125), ddx_, ddy_);
					float4 color1 = tex2D(_MainTex, tex1*float2(0.21875, 0.4375)+float2(0.015625, 0.03125));
					float4 color2 = tex2D(_MainTex, tex1*float2(0.21875, 0.4375)+float2(0.265625, 0.03125));
					float4 color3 = tex2D(_MainTex, tex1*float2(0.21875, 0.4375)+float2(0.515625, 0.03125));
					float4 color4 = tex2D(_MainTex, tex1*float2(0.21875, 0.4375)+float2(0.765625, 0.03125));
					
					float4 blend = tex2D(_BlendTex, In.texblend);
					float4 light = tex2D(_LightMap, In.texblend);
					
					float4 color = lerp(color0, color1, blend.r);
					color = lerp(color, color2, blend.g);
					color = lerp(color, color3, blend.b);
					color = lerp(color, color4, blend.a);
					
					In.normal = normalize(In.normal);
					float diffuse = 0.5;
					diffuse += saturate(dot(normalize(float3(1.0f, 1.0f, 1.0f)), In.normal));
					
					return float4(color.rgb*light.rgb*diffuse, 1.0);
				}
			ENDCG
		}
	}
	
	FallBack "Diffuse"
}

Any help on getting it to work correctly would be great.
Thanks.

I just found out that it does compile for shader model 3, but it still keeps telling me:

The problem is (as usual :)) OpenGL. First, of course, OpenGL really has no concept of “shader models”. So when compiling into shader model 3.0, for OpenGL we just increase the limit of instructions/registers allowed. However, since for OpenGL we compile into ARB_fragment_program, there’s no way to do ddx/ddy and other SM3.0 goodies there (like dynamic branching).

In other words, you can’t use ddx/ddy on OpenGL. If that’s not a problem for you, you could add “#pragma exclude_renderers opengl” to your shader, in which case it will be compiled to everything except OpenGL (in Unity 2.x, this would mean “only d3d9”). Then it should work.

1 Like

Awesome, thank you.
Does that mean that there is no “fast and easy” way to get something like that in Unity for OpenGL?
Or would it work if I convert the whole stuff to GLSL using dFdx() and dFdy()?

Bump

I’m also interested in this. Is there any solution for GL?