Search Unity

How to "copy-paste" a Cg shader found on the internet in Unity

Discussion in 'Shaders' started by Sonoshee, Jun 19, 2015.

  1. Sonoshee

    Sonoshee

    Joined:
    Jul 8, 2014
    Posts:
    77
    Hello everyone, this is an extremely newbie question about shaders.
    I need to make a post-processing shader for my camera that scales up the rendered game image using an xBR algorithm. I found a Cg shader on the internet that does just that, but since I'm a complete beginner in the field of shader programming, I just couldn't write it properly in a Unity-compatible way.
    Here is the Cg shader :
    http://www.mediafire.com/view/n3fmr2gybq291x2/2xBR-v3.5a.cg
    Here is what the output should look like ( this was achieved by feeding a screenshot of my game to a scaling software that uses the xBR method), click to enlarge :


    Note that I know how to attach a post-processing shader to a camera, I just couldn't figure out where to copy-paste the Cg code so that it works properly.
    Any clarifications on how you achieved so would appreciated.
    Also note that the effect only works when you're displaying the game x2 its native resolution ( in the case of my game the native res is 512x300)
     
    Last edited: Jun 19, 2015
  2. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Make a new shader. I assume you're using Unity 5 so it defaults to a surface shader. Delete everything between the lines "CGPROGRAM" and "ENDCG". Just above "CGPROGRAM", add a bracket "{" and just below "ENDCG", add a closing bracket "}". Then copy everything in the .cg file you have and paste in the space between CGPROGRAM and ENDCG. Then go back up, to the line containing "CGPROGRAM" and paste this:
    Code (CSharp):
    1. #include "UnityCG.cginc"
    . After this line, add these too:
    Code (CSharp):
    1. #pragma vertex [name of vertex shader]
    2. #pragma fragment [name of fragment shader]
    which in your case will be "main_vertex" for your vertex shader and "main_fragment" for your fragment shader.

    After these, tell me what errors you encounter and we'll go from there.
     
  3. Sonoshee

    Sonoshee

    Joined:
    Jul 8, 2014
    Posts:
    77
    Hey there! Thanks for the help.
    I followed what you said and I got this error :


    I've seen some shaders adding a "Pass" before the brackets you told me to add, so I thought you forgot to mention that.
    Now it's not displaying any errors thankfully ( except that warning) :


    So I guess I need to add the shader to a material? and then use Graphics.Blit?
    I'll try and let you know.
     
  4. Sonoshee

    Sonoshee

    Joined:
    Jul 8, 2014
    Posts:
    77
    I added the shader to a material, now the console gives me this :


    EDIT 1 : I've just solved the warning, but those bunch of errors still persist.
    EDIT 2 : I've solved those errors, seems like a bug in Unity when drag&dropping a shader to a material, I've selected the shader from the the dropbox.
     
    Last edited: Jun 24, 2015
  5. Sonoshee

    Sonoshee

    Joined:
    Jul 8, 2014
    Posts:
    77
    Last edited: Jun 24, 2015
  6. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    It's definitely not working. We'll need to adjust the shader to fit Unity. This is what I have so far but it throws out an error: "half4x3 not supported in pre-GLSL1.20 line x"
    Code (CSharp):
    1. Shader "Custom/2xBR" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1) //You should set this field in Unity scripting, material.SetColor
    4.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    5.         decal ("decal (RGB)", 2D) = "white" {} //you should also set this in scripting with material.SetTexture ("decal", yourDecalTexture)
    6.     }
    7.     SubShader {
    8.         Tags { "RenderType"="Opaque" }
    9.         ///////////////////////////////////
    10.         Pass {
    11.             CGPROGRAM
    12.             #include "UnityCG.cginc"
    13.             #pragma vertex main_vertex
    14.             #pragma fragment main_fragment
    15.            
    16.             uniform half4 _Color;
    17.             uniform sampler2D decal : TEXUNIT0;
    18.             uniform half2 video_size; //You should set this in Unity (your script) using material.SetVector("video_size", outputSize float); //note that the vector you set must be a vector4, smae as the remaining two below
    19.             uniform float2 texture_size; //You should set this in Unity (your script) using material.SetVector("texture_size", outputSize float);
    20.             uniform half2 output_size; //You should set this in Unity (your script) using material.SetVector("output_size", outputSize float);
    21.            
    22.             const static float coef = 2.0;
    23.             const static float3 dtt = float3(65536,255,1);
    24.             const static half y_weight = 48.0;
    25.             const static half u_weight = 7.0;
    26.             const static half v_weight = 6.0;
    27.             const static half3x3 yuv = half3x3(0.299, 0.587, 0.114, -0.169, -0.331, 0.499, 0.499, -0.418, -0.0813);
    28.             const static half3x3 yuv_weighted = half3x3(y_weight*yuv[0], u_weight*yuv[1], v_weight*yuv[2]);
    29.  
    30.             //const static half3x3 yuv_weighted = half3x3(14.352, 28.176, 5.472, -1.183, -2.317, 3.5, 3.0, -2.514, -0.486);
    31.  
    32.             float4 RGBtoYUV(half4x3 mat_color) {
    33.                 float a = abs(mul(yuv_weighted, mat_color[0]));
    34.                 float b = abs(mul(yuv_weighted, mat_color[1]));
    35.                 float c = abs(mul(yuv_weighted, mat_color[2]));
    36.                 float d = abs(mul(yuv_weighted, mat_color[3]));
    37.  
    38.                 return float4(a, b, c, d);
    39.             }
    40.  
    41.             float4 df(float4 A, float4 B) {
    42.                 return float4(abs(A - B));
    43.             }
    44.  
    45.  
    46.             float4 weighted_distance(float4 a, float4 b, float4 c, float4 d, float4 e, float4 f, float4 g, float4 h) {
    47.                 return (df(a,b) + df(a,c) + df(d,e) + df(d,f) + 4.0*df(g,h));
    48.             }
    49.  
    50.             struct out_vertex {
    51.                 half4 position : POSITION;
    52.                 half4 color    : COLOR;
    53.                 float2 texCoord : TEXCOORD0;
    54.                 half4 t1 : TEXCOORD1;
    55.             };
    56.  
    57.             //    VERTEX_SHADER
    58.             out_vertex main_vertex ( appdata_base v ) {
    59.                 out_vertex OUT;
    60.  
    61.                 OUT.position = mul(UNITY_MATRIX_MVP, v.vertex);
    62.                 OUT.color = _Color;
    63.  
    64.                 half2 ps = half2(1.0 / texture_size.x, 1.0 / texture_size.y);
    65.                 half dx = ps.x;
    66.                 half dy = ps.y;
    67.  
    68.                 OUT.texCoord = v.texcoord;
    69.                 OUT.t1.xy = half2(dx,  0); // F
    70.                 OUT.t1.zw = half2(0, dy); // H
    71.  
    72.                 return OUT;
    73.             }
    74.  
    75.             //   FRAGMENT SHADER
    76.             half4 main_fragment( out_vertex VAR ) : COLOR {
    77.                 bool4 edr, edr_left, edr_up, px; // px = pixel, edr = edge detection rule
    78.                 bool4 interp_restriction_lv1, interp_restriction_lv2_left, interp_restriction_lv2_up;
    79.  
    80.                 float2 fp = frac(VAR.texCoord*texture_size);
    81.  
    82.                 half2 dx = VAR.t1.xy;
    83.                 half2 dy = VAR.t1.zw;
    84.  
    85.                 half3 A = tex2D(decal, VAR.texCoord - dx - dy).xyz;
    86.                 half3 B = tex2D(decal, VAR.texCoord - dy).xyz;
    87.                 half3 C = tex2D(decal, VAR.texCoord + dx - dy).xyz;
    88.                 half3 D = tex2D(decal, VAR.texCoord - dx).xyz;
    89.                 half3 E = tex2D(decal, VAR.texCoord).xyz;
    90.                 half3 F = tex2D(decal, VAR.texCoord + dx).xyz;
    91.                 half3 G = tex2D(decal, VAR.texCoord - dx + dy).xyz;
    92.                 half3 H = tex2D(decal, VAR.texCoord + dy).xyz;
    93.                 half3 I = tex2D(decal, VAR.texCoord + dx + dy).xyz;
    94.  
    95.                 half3  A1 = tex2D(decal, VAR.texCoord - dx - 2.0*dy).xyz;
    96.                 half3  C1 = tex2D(decal, VAR.texCoord + dx - 2.0*dy).xyz;
    97.                 half3  A0 = tex2D(decal, VAR.texCoord - 2.0*dx - dy).xyz;
    98.                 half3  G0 = tex2D(decal, VAR.texCoord - 2.0*dx + dy).xyz;
    99.                 half3  C4 = tex2D(decal, VAR.texCoord + 2.0*dx - dy).xyz;
    100.                 half3  I4 = tex2D(decal, VAR.texCoord + 2.0*dx + dy).xyz;
    101.                 half3  G5 = tex2D(decal, VAR.texCoord - dx + 2.0*dy).xyz;
    102.                 half3  I5 = tex2D(decal, VAR.texCoord + dx + 2.0*dy).xyz;
    103.                 half3  B1 = tex2D(decal, VAR.texCoord - 2.0*dy).xyz;
    104.                 half3  D0 = tex2D(decal, VAR.texCoord - 2.0*dx).xyz;
    105.                 half3  H5 = tex2D(decal, VAR.texCoord + 2.0*dy).xyz;
    106.                 half3  F4 = tex2D(decal, VAR.texCoord + 2.0*dx).xyz;
    107.  
    108.                 float4 a = RGBtoYUV(half4x3(A, G, I, C));
    109.                 float4 b = RGBtoYUV(half4x3(B, D, H, F));
    110.                 float4 c = RGBtoYUV(half4x3(C, A, G, I));
    111.                 float4 d = RGBtoYUV(half4x3(D, H, F, B));
    112.                 float4 e = RGBtoYUV(half4x3(E, E, E, E));
    113.                 float4 f = RGBtoYUV(half4x3(F, B, D, H));
    114.                 float4 g = RGBtoYUV(half4x3(G, I, C, A));
    115.                 float4 h = RGBtoYUV(half4x3(H, F, B, D));
    116.                 float4 i = RGBtoYUV(half4x3(I, C, A, G));
    117.  
    118.                 float4 a1 = RGBtoYUV(half4x3(A1, G0, I5, C4));
    119.                 float4 c1 = RGBtoYUV(half4x3(C1, A0, G5, I4));
    120.                 float4 a0 = RGBtoYUV(half4x3(A0, G5, I4, C1));
    121.                 float4 g0 = RGBtoYUV(half4x3(G0, I5, C4, A1));
    122.                 float4 c4 = RGBtoYUV(half4x3(C4, A1, G0, I5));
    123.                 float4 i4 = RGBtoYUV(half4x3(I4, C1, A0, G5));
    124.                 float4 g5 = RGBtoYUV(half4x3(G5, I4, C1, A0));
    125.                 float4 i5 = RGBtoYUV(half4x3(I5, C4, A1, G0));
    126.                 float4 b1 = RGBtoYUV(half4x3(B1, D0, H5, F4));
    127.                 float4 d0 = RGBtoYUV(half4x3(D0, H5, F4, B1));
    128.                 float4 h5 = RGBtoYUV(half4x3(H5, F4, B1, D0));
    129.                 float4 f4 = RGBtoYUV(half4x3(F4, B1, D0, H5));
    130.  
    131.                 interp_restriction_lv1 = ((e != f) && (e != h));
    132.                 interp_restriction_lv2_left = ((e != g) && (d != g));
    133.                 interp_restriction_lv2_up = ((e != c) && (b != c));
    134.  
    135.                 edr = (weighted_distance(e, c, g, i, h5, f4, h, f) < weighted_distance(h, d, i5, f, i4, b, e, i)) && interp_restriction_lv1;
    136.                 edr_left = ((coef*df(f,g)) <= df(h,c)) && interp_restriction_lv2_left;
    137.                 edr_up = (df(f,g) >= (coef*df(h,c))) && interp_restriction_lv2_up;
    138.  
    139.                 half3 E0 = E;
    140.                 half3 E1 = E;
    141.                 half3 E2 = E;
    142.                 half3 E3 = E;
    143.  
    144.                 px = (df(e,f) <= df(e,h));
    145.  
    146.                 half3 P[4];
    147.  
    148.                 P[0] = px.x ? F : H;
    149.                 P[1] = px.y ? B : F;
    150.                 P[2] = px.z ? D : B;
    151.                 P[3] = px.w ? H : D;
    152.  
    153.  
    154.                 if (edr.x)
    155.                 {
    156.                     if (edr_left.x && edr_up.x)
    157.                     {
    158.                         E3 = lerp(E3 , P[0],  0.833333);
    159.                         E2 = lerp(E2 , P[0],  0.25);
    160.                         E1 = lerp(E1 , P[0],  0.25);
    161.                     }
    162.                     else if (edr_left.x)
    163.                     {
    164.                         E3 = lerp(E3 , P[0],  0.75);
    165.                         E2 = lerp(E2 , P[0],  0.25);
    166.                     }
    167.                     else if (edr_up.x)
    168.                     {
    169.                         E3 = lerp(E3 , P[0],  0.75);
    170.                         E1 = lerp(E1 , P[0],  0.25);
    171.                     }
    172.                     else
    173.                     {
    174.                         E3 = lerp(E3 , P[0],  0.5);
    175.                     }
    176.                 }
    177.  
    178.                 if (edr.y)
    179.                 {
    180.                     if (edr_left.y && edr_up.y)
    181.                     {
    182.                         E1 = lerp(E1 , P[1],  0.833333);
    183.                         E3 = lerp(E3 , P[1],  0.25);
    184.                         E0 = lerp(E0 , P[1],  0.25);
    185.                     }
    186.                     else if (edr_left.y)
    187.                     {
    188.                         E1 = lerp(E1 , P[1],  0.75);
    189.                         E3 = lerp(E3 , P[1],  0.25);
    190.                     }
    191.                     else if (edr_up.y)
    192.                     {
    193.                         E1 = lerp(E1 , P[1],  0.75);
    194.                         E0 = lerp(E0 , P[1],  0.25);
    195.                     }
    196.                     else
    197.                     {
    198.                         E1 = lerp(E1 , P[1],  0.5);
    199.                     }
    200.                 }
    201.  
    202.                 if (edr.z)
    203.                 {
    204.                     if (edr_left.z && edr_up.z)
    205.                     {
    206.                         E0 = lerp(E0 , P[2],  0.833333);
    207.                         E1 = lerp(E1 , P[2],  0.25);
    208.                         E2 = lerp(E2 , P[2],  0.25);
    209.                     }
    210.                     else if (edr_left.z)
    211.                     {
    212.                         E0 = lerp(E0 , P[2],  0.75);
    213.                         E1 = lerp(E1 , P[2],  0.25);
    214.                     }
    215.                     else if (edr_up.z)
    216.                     {
    217.                         E0 = lerp(E0 , P[2],  0.75);
    218.                         E2 = lerp(E2 , P[2],  0.25);
    219.                     }
    220.                     else
    221.                     {
    222.                         E0 = lerp(E0 , P[2],  0.5);
    223.                     }
    224.                 }
    225.  
    226.                 if (edr.w)
    227.                 {
    228.                     if (edr_left.w && edr_up.w)
    229.                     {
    230.                         E2 = lerp(E2 , P[3],  0.833333);
    231.                         E0 = lerp(E0 , P[3],  0.25);
    232.                         E3 = lerp(E3 , P[3],  0.25);
    233.                     }
    234.                     else if (edr_left.w)
    235.                     {
    236.                         E2 = lerp(E2 , P[3],  0.75);
    237.                         E0 = lerp(E0 , P[3],  0.25);
    238.                     }
    239.                     else if (edr_up.w)
    240.                     {
    241.                         E2 = lerp(E2 , P[3],  0.75);
    242.                         E3 = lerp(E3 , P[3],  0.25);
    243.                     }
    244.                     else
    245.                     {
    246.                         E2 = lerp(E2 , P[3],  0.5);
    247.                     }
    248.                 }
    249.  
    250.                 half3 res = (fp.x < 0.50) ? (fp.y < 0.50 ? E0 : E2) : (fp.y < 0.50 ? E1 : E3);
    251.  
    252.                 return half4(res, 1.0);
    253.             }
    254.             ENDCG
    255.         }
    256.         ///////////////////////////////////
    257.     }
    258.     FallBack "Diffuse"
    259. }
     
  7. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Gotten rid of the half4x3 issue, now facing this comparison issues "&&".

    Code (CSharp):
    1. Shader "Custom/2xBR" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1) //You should set this field in Unity scripting, material.SetColor
    4.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    5.         decal ("decal (RGB)", 2D) = "white" {} //you should also set this in scripting with material.SetTexture ("decal", yourDecalTexture)
    6.     }
    7.     SubShader {
    8.         Tags { "RenderType"="Opaque" }
    9.         ///////////////////////////////////
    10.         Pass {
    11.             CGPROGRAM
    12.             #include "UnityCG.cginc"
    13.             #pragma vertex main_vertex
    14.             #pragma fragment main_fragment
    15.            
    16.             uniform half4 _Color;
    17.             uniform sampler2D decal : TEXUNIT0;
    18.             uniform half2 video_size; //You should set this in Unity (your script) using material.SetVector("video_size", outputSize float); //note that the vector you set must be a vector4, smae as the remaining two below
    19.             uniform float2 texture_size; //You should set this in Unity (your script) using material.SetVector("texture_size", outputSize float);
    20.             uniform half2 output_size; //You should set this in Unity (your script) using material.SetVector("output_size", outputSize float);
    21.            
    22.             const static float coef = 2.0;
    23.             const static float3 dtt = float3(65536,255,1);
    24.             const static half y_weight = 48.0;
    25.             const static half u_weight = 7.0;
    26.             const static half v_weight = 6.0;
    27.             const static half3x3 yuv = half3x3(0.299, 0.587, 0.114, -0.169, -0.331, 0.499, 0.499, -0.418, -0.0813);
    28.             const static half3x3 yuv_weighted = half3x3(y_weight*yuv[0], u_weight*yuv[1], v_weight*yuv[2]);
    29.  
    30.             //const static half3x3 yuv_weighted = half3x3(14.352, 28.176, 5.472, -1.183, -2.317, 3.5, 3.0, -2.514, -0.486);
    31.  
    32.             float4 RGBtoYUV(half4x4 mat_color) {
    33.                 float a = abs(mul(yuv_weighted, mat_color[0].xyz));
    34.                 float b = abs(mul(yuv_weighted, mat_color[1].xyz));
    35.                 float c = abs(mul(yuv_weighted, mat_color[2].xyz));
    36.                 float d = abs(mul(yuv_weighted, mat_color[3].xyz));
    37.  
    38.                 return float4(a, b, c, d);
    39.             }
    40.  
    41.             float4 df(float4 A, float4 B) {
    42.                 return float4(abs(A - B));
    43.             }
    44.  
    45.  
    46.             float4 weighted_distance(float4 a, float4 b, float4 c, float4 d, float4 e, float4 f, float4 g, float4 h) {
    47.                 return (df(a,b) + df(a,c) + df(d,e) + df(d,f) + 4.0*df(g,h));
    48.             }
    49.  
    50.             struct out_vertex {
    51.                 half4 position : POSITION;
    52.                 half4 color    : COLOR;
    53.                 float2 texCoord : TEXCOORD0;
    54.                 half4 t1 : TEXCOORD1;
    55.             };
    56.  
    57.             //    VERTEX_SHADER
    58.             out_vertex main_vertex ( appdata_base v ) {
    59.                 out_vertex OUT;
    60.  
    61.                 OUT.position = mul(UNITY_MATRIX_MVP, v.vertex);
    62.                 OUT.color = _Color;
    63.  
    64.                 half2 ps = half2(1.0 / texture_size.x, 1.0 / texture_size.y);
    65.                 half dx = ps.x;
    66.                 half dy = ps.y;
    67.  
    68.                 OUT.texCoord = v.texcoord;
    69.                 OUT.t1.xy = half2(dx,  0); // F
    70.                 OUT.t1.zw = half2(0, dy); // H
    71.  
    72.                 return OUT;
    73.             }
    74.  
    75.             //   FRAGMENT SHADER
    76.             half4 main_fragment( out_vertex VAR ) : COLOR {
    77.                 bool4 edr, edr_left, edr_up, px; // px = pixel, edr = edge detection rule
    78.                 bool4 interp_restriction_lv1, interp_restriction_lv2_left, interp_restriction_lv2_up;
    79.  
    80.                 float2 fp = frac(VAR.texCoord*texture_size);
    81.  
    82.                 half2 dx = VAR.t1.xy;
    83.                 half2 dy = VAR.t1.zw;
    84.  
    85.                 half3 A = tex2D(decal, VAR.texCoord - dx - dy).xyz;
    86.                 half3 B = tex2D(decal, VAR.texCoord - dy).xyz;
    87.                 half3 C = tex2D(decal, VAR.texCoord + dx - dy).xyz;
    88.                 half3 D = tex2D(decal, VAR.texCoord - dx).xyz;
    89.                 half3 E = tex2D(decal, VAR.texCoord).xyz;
    90.                 half3 F = tex2D(decal, VAR.texCoord + dx).xyz;
    91.                 half3 G = tex2D(decal, VAR.texCoord - dx + dy).xyz;
    92.                 half3 H = tex2D(decal, VAR.texCoord + dy).xyz;
    93.                 half3 I = tex2D(decal, VAR.texCoord + dx + dy).xyz;
    94.  
    95.                 half3  A1 = tex2D(decal, VAR.texCoord - dx - 2.0*dy).xyz;
    96.                 half3  C1 = tex2D(decal, VAR.texCoord + dx - 2.0*dy).xyz;
    97.                 half3  A0 = tex2D(decal, VAR.texCoord - 2.0*dx - dy).xyz;
    98.                 half3  G0 = tex2D(decal, VAR.texCoord - 2.0*dx + dy).xyz;
    99.                 half3  C4 = tex2D(decal, VAR.texCoord + 2.0*dx - dy).xyz;
    100.                 half3  I4 = tex2D(decal, VAR.texCoord + 2.0*dx + dy).xyz;
    101.                 half3  G5 = tex2D(decal, VAR.texCoord - dx + 2.0*dy).xyz;
    102.                 half3  I5 = tex2D(decal, VAR.texCoord + dx + 2.0*dy).xyz;
    103.                 half3  B1 = tex2D(decal, VAR.texCoord - 2.0*dy).xyz;
    104.                 half3  D0 = tex2D(decal, VAR.texCoord - 2.0*dx).xyz;
    105.                 half3  H5 = tex2D(decal, VAR.texCoord + 2.0*dy).xyz;
    106.                 half3  F4 = tex2D(decal, VAR.texCoord + 2.0*dx).xyz;
    107.  
    108.                 float4 a = RGBtoYUV(half4x4(half4(A,1), half4(G,1), half4(I,1), half4(C,1)));
    109.                 float4 b = RGBtoYUV(half4x4(half4(B,1), half4(D,1), half4(H,1), half4(F,1)));
    110.                 float4 c = RGBtoYUV(half4x4(half4(C,1), half4(A,1), half4(G,1), half4(I,1)));
    111.                 float4 d = RGBtoYUV(half4x4(half4(D,1), half4(H,1), half4(F,1), half4(B,1)));
    112.                 float4 e = RGBtoYUV(half4x4(half4(E,1), half4(E,1), half4(E,1), half4(E,1)));
    113.                 float4 f = RGBtoYUV(half4x4(half4(F,1), half4(B,1), half4(D,1), half4(H,1)));
    114.                 float4 g = RGBtoYUV(half4x4(half4(G,1), half4(I,1), half4(C,1), half4(A,1)));
    115.                 float4 h = RGBtoYUV(half4x4(half4(H,1), half4(F,1), half4(B,1), half4(D,1)));
    116.                 float4 i = RGBtoYUV(half4x4(half4(I,1), half4(C,1), half4(A,1), half4(G,1)));
    117.  
    118.                 float4 a1 = RGBtoYUV(half4x4(half4(A1,1), half4(G0,1), half4(I5,1), half4(C4,1)));
    119.                 float4 c1 = RGBtoYUV(half4x4(half4(C1,1), half4(A0,1), half4(G5,1), half4(I4,1)));
    120.                 float4 a0 = RGBtoYUV(half4x4(half4(A0,1), half4(G5,1), half4(I4,1), half4(C1,1)));
    121.                 float4 g0 = RGBtoYUV(half4x4(half4(G0,1), half4(I5,1), half4(C4,1), half4(A1,1)));
    122.                 float4 c4 = RGBtoYUV(half4x4(half4(C4,1), half4(A1,1), half4(G0,1), half4(I5,1)));
    123.                 float4 i4 = RGBtoYUV(half4x4(half4(I4,1), half4(C1,1), half4(A0,1), half4(G5,1)));
    124.                 float4 g5 = RGBtoYUV(half4x4(half4(G5,1), half4(I4,1), half4(C1,1), half4(A0,1)));
    125.                 float4 i5 = RGBtoYUV(half4x4(half4(I5,1), half4(C4,1), half4(A1,1), half4(G0,1)));
    126.                 float4 b1 = RGBtoYUV(half4x4(half4(B1,1), half4(D0,1), half4(H5,1), half4(F4,1)));
    127.                 float4 d0 = RGBtoYUV(half4x4(half4(D0,1), half4(H5,1), half4(F4,1), half4(B1,1)));
    128.                 float4 h5 = RGBtoYUV(half4x4(half4(H5,1), half4(F4,1), half4(B1,1), half4(D0,1)));
    129.                 float4 f4 = RGBtoYUV(half4x4(half4(F4,1), half4(B1,1), half4(D0,1), half4(H5,1)));
    130.                
    131.                 interp_restriction_lv1 = ((e != f) && (e != h));
    132.                 interp_restriction_lv2_left = ((e != g) && (d != g));
    133.                 interp_restriction_lv2_up = ((e != c) && (b != c));
    134.  
    135.                 edr = (weighted_distance(e, c, g, i, h5, f4, h, f) < weighted_distance(h, d, i5, f, i4, b, e, i)) && interp_restriction_lv1;
    136.                 edr_left = ((coef*df(f,g)) <= df(h,c)) && interp_restriction_lv2_left;
    137.                 edr_up = (df(f,g) >= (coef*df(h,c))) && interp_restriction_lv2_up;
    138.  
    139.                 half3 E0 = E;
    140.                 half3 E1 = E;
    141.                 half3 E2 = E;
    142.                 half3 E3 = E;
    143.  
    144.                 px = (df(e,f) <= df(e,h));
    145.  
    146.                 half3 P[4];
    147.  
    148.                 P[0] = px.x ? F : H;
    149.                 P[1] = px.y ? B : F;
    150.                 P[2] = px.z ? D : B;
    151.                 P[3] = px.w ? H : D;
    152.  
    153.  
    154.                 if (edr.x)
    155.                 {
    156.                     if (edr_left.x && edr_up.x)
    157.                     {
    158.                         E3 = lerp(E3 , P[0],  0.833333);
    159.                         E2 = lerp(E2 , P[0],  0.25);
    160.                         E1 = lerp(E1 , P[0],  0.25);
    161.                     }
    162.                     else if (edr_left.x)
    163.                     {
    164.                         E3 = lerp(E3 , P[0],  0.75);
    165.                         E2 = lerp(E2 , P[0],  0.25);
    166.                     }
    167.                     else if (edr_up.x)
    168.                     {
    169.                         E3 = lerp(E3 , P[0],  0.75);
    170.                         E1 = lerp(E1 , P[0],  0.25);
    171.                     }
    172.                     else
    173.                     {
    174.                         E3 = lerp(E3 , P[0],  0.5);
    175.                     }
    176.                 }
    177.  
    178.                 if (edr.y)
    179.                 {
    180.                     if (edr_left.y && edr_up.y)
    181.                     {
    182.                         E1 = lerp(E1 , P[1],  0.833333);
    183.                         E3 = lerp(E3 , P[1],  0.25);
    184.                         E0 = lerp(E0 , P[1],  0.25);
    185.                     }
    186.                     else if (edr_left.y)
    187.                     {
    188.                         E1 = lerp(E1 , P[1],  0.75);
    189.                         E3 = lerp(E3 , P[1],  0.25);
    190.                     }
    191.                     else if (edr_up.y)
    192.                     {
    193.                         E1 = lerp(E1 , P[1],  0.75);
    194.                         E0 = lerp(E0 , P[1],  0.25);
    195.                     }
    196.                     else
    197.                     {
    198.                         E1 = lerp(E1 , P[1],  0.5);
    199.                     }
    200.                 }
    201.  
    202.                 if (edr.z)
    203.                 {
    204.                     if (edr_left.z && edr_up.z)
    205.                     {
    206.                         E0 = lerp(E0 , P[2],  0.833333);
    207.                         E1 = lerp(E1 , P[2],  0.25);
    208.                         E2 = lerp(E2 , P[2],  0.25);
    209.                     }
    210.                     else if (edr_left.z)
    211.                     {
    212.                         E0 = lerp(E0 , P[2],  0.75);
    213.                         E1 = lerp(E1 , P[2],  0.25);
    214.                     }
    215.                     else if (edr_up.z)
    216.                     {
    217.                         E0 = lerp(E0 , P[2],  0.75);
    218.                         E2 = lerp(E2 , P[2],  0.25);
    219.                     }
    220.                     else
    221.                     {
    222.                         E0 = lerp(E0 , P[2],  0.5);
    223.                     }
    224.                 }
    225.  
    226.                 if (edr.w)
    227.                 {
    228.                     if (edr_left.w && edr_up.w)
    229.                     {
    230.                         E2 = lerp(E2 , P[3],  0.833333);
    231.                         E0 = lerp(E0 , P[3],  0.25);
    232.                         E3 = lerp(E3 , P[3],  0.25);
    233.                     }
    234.                     else if (edr_left.w)
    235.                     {
    236.                         E2 = lerp(E2 , P[3],  0.75);
    237.                         E0 = lerp(E0 , P[3],  0.25);
    238.                     }
    239.                     else if (edr_up.w)
    240.                     {
    241.                         E2 = lerp(E2 , P[3],  0.75);
    242.                         E3 = lerp(E3 , P[3],  0.25);
    243.                     }
    244.                     else
    245.                     {
    246.                         E2 = lerp(E2 , P[3],  0.5);
    247.                     }
    248.                 }
    249.  
    250.                 half3 res = (fp.x < 0.50) ? (fp.y < 0.50 ? E0 : E2) : (fp.y < 0.50 ? E1 : E3);
    251.  
    252.                 return half4(res, 1.0);
    253.             }
    254.             ENDCG
    255.         }
    256.         ///////////////////////////////////
    257.     }
    258.     FallBack "Diffuse"
    259. }
    260.  
     
  8. Sonoshee

    Sonoshee

    Joined:
    Jul 8, 2014
    Posts:
    77
    Hey there, I'm back.
    I've previously sent a message to Chman the guy behind the Colorful asset about this. The response included other topics but here is what he said about the implementation of the shader :

    Not sure if this would help now that you're this far into porting the shader.
     
  9. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Yes I did what he was saying. The vertex shader was the easiest in fact. Copy and paste what I have and tell me what errors you get. I know what he's talking about.
     
  10. Sonoshee

    Sonoshee

    Joined:
    Jul 8, 2014
    Posts:
    77
    After compiling the shader you wrote I get the following errors ( I guess the same as you) :


    The screen is now white, if that helps in any way :
     
  11. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    For the temp register issue, add this line under the #pragma fragment line:
    Code (CSharp):
    1. #pragma profileoption NumTemps=64
    .Keep increasing the number till the temp register errors go away. I have a question that might solve the '&&' problem: what platform are you targeting?
     
  12. Sonoshee

    Sonoshee

    Joined:
    Jul 8, 2014
    Posts:
    77
    Hmmm particularly desktop ( priority for Windows, not sure if I'd include Linux and Mac). No chance for mobile, the screen would get all flooded by the controls.
     
  13. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Okay. Did the temp registers error go away after adding that line? If no, quit Unity, and start it with the -force-opengl argument. The error should go away then (I use openGL [Mac] and don't encounter that problem). The && problem is a tricky one. I managed to fix it by breaking the statement into individual booleans. Here, the shader throws no more errors on my system. Note that I haven't tested it, so that's up to you.
    Code (CSharp):
    1. Shader "Custom/2xBR" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1) //You should set this field in Unity scripting, material.SetColor
    4.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    5.         decal ("decal (RGB)", 2D) = "white" {} //you should also set this in scripting with material.SetTexture ("decal", yourDecalTexture)
    6.     }
    7.     SubShader {
    8.         Tags { "RenderType"="Opaque" }
    9.         ///////////////////////////////////
    10.         Pass {
    11.             CGPROGRAM
    12.             #include "UnityCG.cginc"
    13.             #pragma vertex main_vertex
    14.             #pragma fragment main_fragment
    15.            
    16.             uniform half4 _Color;
    17.             uniform sampler2D decal : TEXUNIT0;
    18.             uniform half2 video_size; //You should set this in Unity (your script) using material.SetVector("video_size", outputSize float); //note that the vector you set must be a vector4, smae as the remaining two below
    19.             uniform float2 texture_size; //You should set this in Unity (your script) using material.SetVector("texture_size", outputSize float);
    20.             uniform half2 output_size; //You should set this in Unity (your script) using material.SetVector("output_size", outputSize float);
    21.            
    22.             const static float coef = 2.0;
    23.             const static float3 dtt = float3(65536,255,1);
    24.             const static half y_weight = 48.0;
    25.             const static half u_weight = 7.0;
    26.             const static half v_weight = 6.0;
    27.             const static half3x3 yuv = half3x3(0.299, 0.587, 0.114, -0.169, -0.331, 0.499, 0.499, -0.418, -0.0813);
    28.             const static half3x3 yuv_weighted = half3x3(y_weight*yuv[0], u_weight*yuv[1], v_weight*yuv[2]);
    29.  
    30.             //const static half3x3 yuv_weighted = half3x3(14.352, 28.176, 5.472, -1.183, -2.317, 3.5, 3.0, -2.514, -0.486);
    31.  
    32.             float4 RGBtoYUV(half4x4 mat_color) {
    33.                 float a = abs(mul(yuv_weighted, mat_color[0].xyz));
    34.                 float b = abs(mul(yuv_weighted, mat_color[1].xyz));
    35.                 float c = abs(mul(yuv_weighted, mat_color[2].xyz));
    36.                 float d = abs(mul(yuv_weighted, mat_color[3].xyz));
    37.  
    38.                 return float4(a, b, c, d);
    39.             }
    40.  
    41.             float4 df(float4 A, float4 B) {
    42.                 return float4(abs(A - B));
    43.             }
    44.  
    45.  
    46.             float4 weighted_distance(float4 a, float4 b, float4 c, float4 d, float4 e, float4 f, float4 g, float4 h) {
    47.                 return (df(a,b) + df(a,c) + df(d,e) + df(d,f) + 4.0*df(g,h));
    48.             }
    49.  
    50.             struct out_vertex {
    51.                 half4 position : POSITION;
    52.                 half4 color    : COLOR;
    53.                 float2 texCoord : TEXCOORD0;
    54.                 half4 t1 : TEXCOORD1;
    55.             };
    56.  
    57.             //    VERTEX_SHADER
    58.             out_vertex main_vertex ( appdata_base v ) {
    59.                 out_vertex OUT;
    60.  
    61.                 OUT.position = mul(UNITY_MATRIX_MVP, v.vertex);
    62.                 OUT.color = _Color;
    63.  
    64.                 half2 ps = half2(1.0 / texture_size.x, 1.0 / texture_size.y);
    65.                 half dx = ps.x;
    66.                 half dy = ps.y;
    67.  
    68.                 OUT.texCoord = v.texcoord;
    69.                 OUT.t1.xy = half2(dx,  0); // F
    70.                 OUT.t1.zw = half2(0, dy); // H
    71.  
    72.                 return OUT;
    73.             }
    74.  
    75.             //   FRAGMENT SHADER
    76.             half4 main_fragment( out_vertex VAR ) : COLOR {
    77.                 bool4 edr, edr_left, edr_up, px; // px = pixel, edr = edge detection rule
    78.                 bool4 interp_restriction_lv1, interp_restriction_lv2_left, interp_restriction_lv2_up;
    79.  
    80.                 float2 fp = frac(VAR.texCoord*texture_size);
    81.  
    82.                 half2 dx = VAR.t1.xy;
    83.                 half2 dy = VAR.t1.zw;
    84.  
    85.                 half3 A = tex2D(decal, VAR.texCoord - dx - dy).xyz;
    86.                 half3 B = tex2D(decal, VAR.texCoord - dy).xyz;
    87.                 half3 C = tex2D(decal, VAR.texCoord + dx - dy).xyz;
    88.                 half3 D = tex2D(decal, VAR.texCoord - dx).xyz;
    89.                 half3 E = tex2D(decal, VAR.texCoord).xyz;
    90.                 half3 F = tex2D(decal, VAR.texCoord + dx).xyz;
    91.                 half3 G = tex2D(decal, VAR.texCoord - dx + dy).xyz;
    92.                 half3 H = tex2D(decal, VAR.texCoord + dy).xyz;
    93.                 half3 I = tex2D(decal, VAR.texCoord + dx + dy).xyz;
    94.  
    95.                 half3  A1 = tex2D(decal, VAR.texCoord - dx - 2.0*dy).xyz;
    96.                 half3  C1 = tex2D(decal, VAR.texCoord + dx - 2.0*dy).xyz;
    97.                 half3  A0 = tex2D(decal, VAR.texCoord - 2.0*dx - dy).xyz;
    98.                 half3  G0 = tex2D(decal, VAR.texCoord - 2.0*dx + dy).xyz;
    99.                 half3  C4 = tex2D(decal, VAR.texCoord + 2.0*dx - dy).xyz;
    100.                 half3  I4 = tex2D(decal, VAR.texCoord + 2.0*dx + dy).xyz;
    101.                 half3  G5 = tex2D(decal, VAR.texCoord - dx + 2.0*dy).xyz;
    102.                 half3  I5 = tex2D(decal, VAR.texCoord + dx + 2.0*dy).xyz;
    103.                 half3  B1 = tex2D(decal, VAR.texCoord - 2.0*dy).xyz;
    104.                 half3  D0 = tex2D(decal, VAR.texCoord - 2.0*dx).xyz;
    105.                 half3  H5 = tex2D(decal, VAR.texCoord + 2.0*dy).xyz;
    106.                 half3  F4 = tex2D(decal, VAR.texCoord + 2.0*dx).xyz;
    107.  
    108.                 float4 a = RGBtoYUV(half4x4(half4(A,1), half4(G,1), half4(I,1), half4(C,1)));
    109.                 float4 b = RGBtoYUV(half4x4(half4(B,1), half4(D,1), half4(H,1), half4(F,1)));
    110.                 float4 c = RGBtoYUV(half4x4(half4(C,1), half4(A,1), half4(G,1), half4(I,1)));
    111.                 float4 d = RGBtoYUV(half4x4(half4(D,1), half4(H,1), half4(F,1), half4(B,1)));
    112.                 float4 e = RGBtoYUV(half4x4(half4(E,1), half4(E,1), half4(E,1), half4(E,1)));
    113.                 float4 f = RGBtoYUV(half4x4(half4(F,1), half4(B,1), half4(D,1), half4(H,1)));
    114.                 float4 g = RGBtoYUV(half4x4(half4(G,1), half4(I,1), half4(C,1), half4(A,1)));
    115.                 float4 h = RGBtoYUV(half4x4(half4(H,1), half4(F,1), half4(B,1), half4(D,1)));
    116.                 float4 i = RGBtoYUV(half4x4(half4(I,1), half4(C,1), half4(A,1), half4(G,1)));
    117.  
    118.                 float4 a1 = RGBtoYUV(half4x4(half4(A1,1), half4(G0,1), half4(I5,1), half4(C4,1)));
    119.                 float4 c1 = RGBtoYUV(half4x4(half4(C1,1), half4(A0,1), half4(G5,1), half4(I4,1)));
    120.                 float4 a0 = RGBtoYUV(half4x4(half4(A0,1), half4(G5,1), half4(I4,1), half4(C1,1)));
    121.                 float4 g0 = RGBtoYUV(half4x4(half4(G0,1), half4(I5,1), half4(C4,1), half4(A1,1)));
    122.                 float4 c4 = RGBtoYUV(half4x4(half4(C4,1), half4(A1,1), half4(G0,1), half4(I5,1)));
    123.                 float4 i4 = RGBtoYUV(half4x4(half4(I4,1), half4(C1,1), half4(A0,1), half4(G5,1)));
    124.                 float4 g5 = RGBtoYUV(half4x4(half4(G5,1), half4(I4,1), half4(C1,1), half4(A0,1)));
    125.                 float4 i5 = RGBtoYUV(half4x4(half4(I5,1), half4(C4,1), half4(A1,1), half4(G0,1)));
    126.                 float4 b1 = RGBtoYUV(half4x4(half4(B1,1), half4(D0,1), half4(H5,1), half4(F4,1)));
    127.                 float4 d0 = RGBtoYUV(half4x4(half4(D0,1), half4(H5,1), half4(F4,1), half4(B1,1)));
    128.                 float4 h5 = RGBtoYUV(half4x4(half4(H5,1), half4(F4,1), half4(B1,1), half4(D0,1)));
    129.                 float4 f4 = RGBtoYUV(half4x4(half4(F4,1), half4(B1,1), half4(D0,1), half4(H5,1)));
    130.                
    131.                 //interp_restriction_lv1 = ((e != f) && (e != h)); //must break up into seperate conditions
    132.                 bool iop1 = (e != f);
    133.                 bool iop2 = (e != h);
    134.                 interp_restriction_lv1 = iop1 && iop2;
    135.                
    136.                 //interp_restriction_lv2_left = ((e != g) && (d != g));
    137.                 bool irp1 = (e != g);
    138.                 bool irp2 = (d != g);
    139.                 interp_restriction_lv2_left = irp1 && irp2;
    140.                
    141.                 //interp_restriction_lv2_up = ((e != c) && (b != c));
    142.                 bool iru1 = (e != c);
    143.                 bool iru2 = (b != c);
    144.                 interp_restriction_lv2_up = iru1 && iru2;
    145.  
    146.                 //edr = (weighted_distance(e, c, g, i, h5, f4, h, f) < weighted_distance(h, d, i5, f, i4, b, e, i)) && interp_restriction_lv1;
    147.                 bool ed1 = (weighted_distance(e, c, g, i, h5, f4, h, f) < weighted_distance(h, d, i5, f, i4, b, e, i));
    148.                 edr = ed1 && (iop1 && iop2); //interp_restriction_lv1 here throws error, so I replaced it with it's assignment
    149.                
    150.                 //edr_left = ((coef*df(f,g)) <= df(h,c)) && interp_restriction_lv2_left;
    151.                 bool edl1 = ((coef*df(f,g)) <= df(h,c));
    152.                 edr_left = edl1 && (irp1 && irp2);
    153.                
    154.                 //edr_up = (df(f,g) >= (coef*df(h,c))) && interp_restriction_lv2_up;
    155.                 bool edu1 = (df(f,g) >= (coef*df(h,c)));
    156.                 edr_up = edu1 && (iru1 && iru2);
    157.  
    158.  
    159.                 half3 E0 = E;
    160.                 half3 E1 = E;
    161.                 half3 E2 = E;
    162.                 half3 E3 = E;
    163.  
    164.                 px = (df(e,f) <= df(e,h));
    165.  
    166.                 half3 P[4];
    167.  
    168.                 P[0] = px.x ? F : H;
    169.                 P[1] = px.y ? B : F;
    170.                 P[2] = px.z ? D : B;
    171.                 P[3] = px.w ? H : D;
    172.  
    173.  
    174.                 if (edr.x)
    175.                 {
    176.                     if (edr_left.x && edr_up.x)
    177.                     {
    178.                         E3 = lerp(E3 , P[0],  0.833333);
    179.                         E2 = lerp(E2 , P[0],  0.25);
    180.                         E1 = lerp(E1 , P[0],  0.25);
    181.                     }
    182.                     else if (edr_left.x)
    183.                     {
    184.                         E3 = lerp(E3 , P[0],  0.75);
    185.                         E2 = lerp(E2 , P[0],  0.25);
    186.                     }
    187.                     else if (edr_up.x)
    188.                     {
    189.                         E3 = lerp(E3 , P[0],  0.75);
    190.                         E1 = lerp(E1 , P[0],  0.25);
    191.                     }
    192.                     else
    193.                     {
    194.                         E3 = lerp(E3 , P[0],  0.5);
    195.                     }
    196.                 }
    197.  
    198.                 if (edr.y)
    199.                 {
    200.                     if (edr_left.y && edr_up.y)
    201.                     {
    202.                         E1 = lerp(E1 , P[1],  0.833333);
    203.                         E3 = lerp(E3 , P[1],  0.25);
    204.                         E0 = lerp(E0 , P[1],  0.25);
    205.                     }
    206.                     else if (edr_left.y)
    207.                     {
    208.                         E1 = lerp(E1 , P[1],  0.75);
    209.                         E3 = lerp(E3 , P[1],  0.25);
    210.                     }
    211.                     else if (edr_up.y)
    212.                     {
    213.                         E1 = lerp(E1 , P[1],  0.75);
    214.                         E0 = lerp(E0 , P[1],  0.25);
    215.                     }
    216.                     else
    217.                     {
    218.                         E1 = lerp(E1 , P[1],  0.5);
    219.                     }
    220.                 }
    221.  
    222.                 if (edr.z)
    223.                 {
    224.                     if (edr_left.z && edr_up.z)
    225.                     {
    226.                         E0 = lerp(E0 , P[2],  0.833333);
    227.                         E1 = lerp(E1 , P[2],  0.25);
    228.                         E2 = lerp(E2 , P[2],  0.25);
    229.                     }
    230.                     else if (edr_left.z)
    231.                     {
    232.                         E0 = lerp(E0 , P[2],  0.75);
    233.                         E1 = lerp(E1 , P[2],  0.25);
    234.                     }
    235.                     else if (edr_up.z)
    236.                     {
    237.                         E0 = lerp(E0 , P[2],  0.75);
    238.                         E2 = lerp(E2 , P[2],  0.25);
    239.                     }
    240.                     else
    241.                     {
    242.                         E0 = lerp(E0 , P[2],  0.5);
    243.                     }
    244.                 }
    245.  
    246.                 if (edr.w)
    247.                 {
    248.                     if (edr_left.w && edr_up.w)
    249.                     {
    250.                         E2 = lerp(E2 , P[3],  0.833333);
    251.                         E0 = lerp(E0 , P[3],  0.25);
    252.                         E3 = lerp(E3 , P[3],  0.25);
    253.                     }
    254.                     else if (edr_left.w)
    255.                     {
    256.                         E2 = lerp(E2 , P[3],  0.75);
    257.                         E0 = lerp(E0 , P[3],  0.25);
    258.                     }
    259.                     else if (edr_up.w)
    260.                     {
    261.                         E2 = lerp(E2 , P[3],  0.75);
    262.                         E3 = lerp(E3 , P[3],  0.25);
    263.                     }
    264.                     else
    265.                     {
    266.                         E2 = lerp(E2 , P[3],  0.5);
    267.                     }
    268.                 }
    269.  
    270.                 half3 res = (fp.x < 0.50) ? (fp.y < 0.50 ? E0 : E2) : (fp.y < 0.50 ? E1 : E3);
    271.  
    272.                 return half4(res, 1.0);
    273.             }
    274.             ENDCG
    275.         }
    276.         ///////////////////////////////////
    277.     }
    278.     FallBack "Diffuse"
    279. }
    280.  
     
  14. Sonoshee

    Sonoshee

    Joined:
    Jul 8, 2014
    Posts:
    77
    I added the " #pragma profileoption NumTemps=64 " line after/before the frag pragma but the errors are still persistent even after increasing/decreasing the value ( same errors number regardless of the NumTemps value).
    Also tried the -force-opengl, but Unity turns extremely slow and sick :


    No text, some buttons are semi-transparent, fails at rendering the game ( I barely got my mouse moving to the Play button), etc ...

    You know what, I guess I'll leave this for later, for the moment I'll try experimenting with Post FX Studio. Have you tried it before? If so how well would you recommend it?

    Sorry for the trouble and thanks for the effort!
     
  15. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Hey man. I don't have any experience with PostFX Studio so I cannot say for sure, but if this is the only effect you're aiming for, I won't advise it. I generally don't advise it because you have little to no control over the code; this is always a downside to advanced devs. But back on this issue, the problem seems to be your GPU. <DX11 on DX10HW> suggests that your GPU is old-ish. I also believe you're running Unity 5. Replace the "#pragma profileoption NumTemps=64 " with this:
    Code (CSharp):
    1. #pragma target 3.0
    . This forces shader model 3 which means more features. I ran this on my surface pro (DX9 GPU) and was encountering a similar issue: too many math operations. I added that line and voila! The error disappeared. Fingers crossed it works this time.
     
  16. Sonoshee

    Sonoshee

    Joined:
    Jul 8, 2014
    Posts:
    77
    Hey Lanre! It's indeed throwing no more errors thanks to you.
    But I have no idea what to fill the videoSize, textureSize, outputSize values with.
    So I posted on the Lib Retro forums where the xBR algorithm creator seems to be active in : http://libretro.com/forums/showthread.php?t=88&page=18 ( scroll down)
    As I said in the reply the screen is always white, maybe I'm not setting the three variables properly. I'm most afraid that the cg shader isn't working properly to begin with.
    In any case, many thanks to you for this much effort!!
    By the way is the Z and W values of the three variables used in the shader? I'm thinking they normally should be left empty since sizes are given in two floats ( width x height)
     
  17. Sonoshee

    Sonoshee

    Joined:
    Jul 8, 2014
    Posts:
    77
    By the way this is how the script that controls the shader looks like :
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraOutputResize : MonoBehaviour
    5. {
    6.     public Material mat_2xBR;
    7.  
    8.     public Vector4 videoSize;
    9.     public Vector4 textureSize;
    10.     public Vector4 outputSize;
    11.  
    12.     void OnRenderImage(RenderTexture source, RenderTexture destination)
    13.     {
    14.         mat_2xBR.SetVector("video_size", videoSize);
    15.         mat_2xBR.SetVector("texture_size", textureSize);
    16.         mat_2xBR.SetVector("output_size", outputSize);
    17.  
    18.         Graphics.Blit(source, destination, mat_2xBR);
    19.     }
    20. }
     
  18. Sonoshee

    Sonoshee

    Joined:
    Jul 8, 2014
    Posts:
    77
    By reading through the shader, it doesn't seem that videoSize and outputSize are used anywhere, nor are textureSize's Z and W.
    I've found an HLSL implementation of the shader in XNA ( http://gamedev.stackexchange.com/questions/87275/how-do-i-perform-an-xbr-or-hqx-filter-in-xna) and from what I understood the outputSize is the native resolution of the game ( 512x300 in my case), already tried it ( both (512,300,0,0) and (0,0,512,300)) but it doesn't seem to work. Argggh

    BTW I already have Post FX Studio, so I'll experiment with it to get familiar with fragment shaders, I also have a set of effects other than the xBR scaling that I want to implement in the game ( like water, shockwave, and fire distortions)
     
  19. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    I think I see the problem. The shader samples from the "decal" texture. But when you do a Graphics.Blit(), you're effectively setting the source texture as the "_MainTex" field in the shader; in our case, this should be the decal field. Renaming every instance of 'decal' to '_MainTex' would be too cumbersome. And I notice you use OnRenderImage. For optimization purposes, use OnPostRender instead because OnRenderImage makes the camera render twice: first for the view, then to make the source texture.

    Replace the OnRenderImage with these:
    Code (CSharp):
    1. private RenderTexture reTex;
    2.  
    3. void OnPreRender () {
    4. if (reTex == null) {
    5. reTex = new RenderTexture(Screen.width, Screen.height, 0);
    6. reTex.antiAliasing = QualitySettings.antiAliasing;
    7. reTex.Create();
    8. }
    9. GetComponent<Camera>().targetTexture = reTex;
    10. }
    11.  
    12. void OnPostRender () {
    13. //set the material attributes here
    14. mat_2xBR.SetTexture("decal", reTex);
    15. Graphics.Blit((RenderTexture)null, (RenderTexture)null, mat_2xBR);
    16. GetFomponent<Camera>().targetTexture = null;
    17. reTex.DiscardContents();
    18. }
    And everything should be good to go.
     
  20. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    If you wanna see if those variables are useless, comment them out (the uniform lines inside CGPROGRAM). If you encounter no errors, then they are indeed useless; you might then erase them from your code.
     
  21. Sonoshee

    Sonoshee

    Joined:
    Jul 8, 2014
    Posts:
    77
    There is some progress here!

    .I've tried setting different values for the outputSize, the screenshot above is with outputSize = (512, 300, 0, 0)
    Notice the effect works only on some edges and the aircraft looks distorted, also notice the game view is rendered on the left not in the center.
    It's certainly an issue related to the shader logic, I'll try experimenting with other xBR algorithms by following the same process of exporting cg shaders to unity, and I'll let you know.
    Edit : if you compare the result with the original screenshot on my first post, the in-game one definitely doesn't perform as well as the software generated one, it's definitely a problem with the shader alogirthm ( there are tons of xBR shader on Github)
     
  22. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    There are 2 variables you must experiment with: texture_size and output_size. First things first, output_size should be the size of the game window. So basically, screen.width by screen.height. When you're in game, click the stats button and make output_size the resolution shown in the stats window.

    Right now, I can't tell how exactly texture_size is used. But I advise checking the effect with very small values(0.01 upward) and very large values (1024 downward). Concerning the window being pushed off screen, I'm not entirely sure why, but I know that it is probably an issue with the shader. I reckon output_size should rectify the issue. You just have to keep experimenting till you stumble upon something that works
     
  23. Sonoshee

    Sonoshee

    Joined:
    Jul 8, 2014
    Posts:
    77
    Okay I'll do what you say in a moment ( maybe tomorrow) and let you know.
     
  24. Sonoshee

    Sonoshee

    Joined:
    Jul 8, 2014
    Posts:
    77
    I've tried several xBR cg shaders ( found here : https://github.com/libretro/common-shaders/tree/master/xbr) all of them give the same artifacts cited above.
    I posted a question on Unity Answers and on gamedev Stack Exchange if someone were to make a tested and working xBR shader ( http://answers.unity3d.com/questions/997963/shader-request-cg-shader-provided-an-xbr-scaling-s.html & http://gamedev.stackexchange.com/qu...ader-provided-an-xbr-scaling-shader-for-unity)
    If there is any progress I'll post here.
     
  25. Zicandar

    Zicandar

    Joined:
    Feb 10, 2014
    Posts:
    388
    In your last screenshot your game view was set to 1024x600, yet you said you set the output size to 512x300, is this intentional? (Would explain why it doesn't fill the screen)

    Also if you can provide the (somewhat) working shader code I could take a look.
     
  26. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Zicandar, here's the working shader code
    Code (CSharp):
    1. Shader "Custom/2xBR" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1) //You should set this field in Unity scripting, material.SetColor
    4.         decal ("decal (RGB)", 2D) = "white" {} //you should also set this in scripting with material.SetTexture ("decal", yourDecalTexture)
    5.     }
    6.     SubShader {
    7.         Tags { "RenderType"="Opaque" }
    8.         ///////////////////////////////////
    9.         Pass {
    10.             CGPROGRAM
    11.             #include "UnityCG.cginc"
    12.             #pragma vertex main_vertex
    13.             #pragma fragment main_fragment
    14.             #pragma target 3.0
    15.          
    16.             uniform half4 _Color;
    17.             uniform sampler2D decal : TEXUNIT0;
    18.             uniform half2 video_size; //You should set this in Unity (your script) using material.SetVector("video_size", outputSize float); //note that the vector you set must be a vector4, smae as the remaining two below
    19.             uniform float2 texture_size; //You should set this in Unity (your script) using material.SetVector("texture_size", outputSize float);
    20.             uniform half2 output_size; //You should set this in Unity (your script) using material.SetVector("output_size", outputSize float);
    21.          
    22.             const static float coef = 2.0;
    23.             const static float3 dtt = float3(65536,255,1);
    24.             const static half y_weight = 48.0;
    25.             const static half u_weight = 7.0;
    26.             const static half v_weight = 6.0;
    27.             const static half3x3 yuv = half3x3(0.299, 0.587, 0.114, -0.169, -0.331, 0.499, 0.499, -0.418, -0.0813);
    28.             const static half3x3 yuv_weighted = half3x3(y_weight*yuv[0], u_weight*yuv[1], v_weight*yuv[2]);
    29.             //const static half3x3 yuv_weighted = half3x3(14.352, 28.176, 5.472, -1.183, -2.317, 3.5, 3.0, -2.514, -0.486);
    30.             float4 RGBtoYUV(half4x4 mat_color) {
    31.                 float a = abs(mul(yuv_weighted, mat_color[0].xyz));
    32.                 float b = abs(mul(yuv_weighted, mat_color[1].xyz));
    33.                 float c = abs(mul(yuv_weighted, mat_color[2].xyz));
    34.                 float d = abs(mul(yuv_weighted, mat_color[3].xyz));
    35.                 return float4(a, b, c, d);
    36.             }
    37.             float4 df(float4 A, float4 B) {
    38.                 return float4(abs(A - B));
    39.             }
    40.             float4 weighted_distance(float4 a, float4 b, float4 c, float4 d, float4 e, float4 f, float4 g, float4 h) {
    41.                 return (df(a,b) + df(a,c) + df(d,e) + df(d,f) + 4.0*df(g,h));
    42.             }
    43.             struct out_vertex {
    44.                 half4 position : POSITION;
    45.                 half4 color    : COLOR;
    46.                 float2 texCoord : TEXCOORD0;
    47.                 half4 t1 : TEXCOORD1;
    48.             };
    49.             //    VERTEX_SHADER
    50.             out_vertex main_vertex ( appdata_base v ) {
    51.                 out_vertex OUT;
    52.                 OUT.position = mul(UNITY_MATRIX_MVP, v.vertex);
    53.                 OUT.color = _Color;
    54.                 half2 ps = half2(1.0 / texture_size.x, 1.0 / texture_size.y);
    55.                 half dx = ps.x;
    56.                 half dy = ps.y;
    57.                 OUT.texCoord = v.texcoord;
    58.                 OUT.t1.xy = half2(dx,  0); // F
    59.                 OUT.t1.zw = half2(0, dy); // H
    60.                 return OUT;
    61.             }
    62.             //   FRAGMENT SHADER
    63.             half4 main_fragment( out_vertex VAR ) : COLOR {
    64.                 bool4 edr, edr_left, edr_up, px; // px = pixel, edr = edge detection rule
    65.                 bool4 interp_restriction_lv1, interp_restriction_lv2_left, interp_restriction_lv2_up;
    66.                 float2 fp = frac(VAR.texCoord*texture_size);
    67.                 half2 dx = VAR.t1.xy;
    68.                 half2 dy = VAR.t1.zw;
    69.                 half3 A = tex2D(decal, VAR.texCoord - dx - dy).xyz;
    70.                 half3 B = tex2D(decal, VAR.texCoord - dy).xyz;
    71.                 half3 C = tex2D(decal, VAR.texCoord + dx - dy).xyz;
    72.                 half3 D = tex2D(decal, VAR.texCoord - dx).xyz;
    73.                 half3 E = tex2D(decal, VAR.texCoord).xyz;
    74.                 half3 F = tex2D(decal, VAR.texCoord + dx).xyz;
    75.                 half3 G = tex2D(decal, VAR.texCoord - dx + dy).xyz;
    76.                 half3 H = tex2D(decal, VAR.texCoord + dy).xyz;
    77.                 half3 I = tex2D(decal, VAR.texCoord + dx + dy).xyz;
    78.                 half3  A1 = tex2D(decal, VAR.texCoord - dx - 2.0*dy).xyz;
    79.                 half3  C1 = tex2D(decal, VAR.texCoord + dx - 2.0*dy).xyz;
    80.                 half3  A0 = tex2D(decal, VAR.texCoord - 2.0*dx - dy).xyz;
    81.                 half3  G0 = tex2D(decal, VAR.texCoord - 2.0*dx + dy).xyz;
    82.                 half3  C4 = tex2D(decal, VAR.texCoord + 2.0*dx - dy).xyz;
    83.                 half3  I4 = tex2D(decal, VAR.texCoord + 2.0*dx + dy).xyz;
    84.                 half3  G5 = tex2D(decal, VAR.texCoord - dx + 2.0*dy).xyz;
    85.                 half3  I5 = tex2D(decal, VAR.texCoord + dx + 2.0*dy).xyz;
    86.                 half3  B1 = tex2D(decal, VAR.texCoord - 2.0*dy).xyz;
    87.                 half3  D0 = tex2D(decal, VAR.texCoord - 2.0*dx).xyz;
    88.                 half3  H5 = tex2D(decal, VAR.texCoord + 2.0*dy).xyz;
    89.                 half3  F4 = tex2D(decal, VAR.texCoord + 2.0*dx).xyz;
    90.                 float4 a = RGBtoYUV(half4x4(half4(A,1), half4(G,1), half4(I,1), half4(C,1)));
    91.                 float4 b = RGBtoYUV(half4x4(half4(B,1), half4(D,1), half4(H,1), half4(F,1)));
    92.                 float4 c = RGBtoYUV(half4x4(half4(C,1), half4(A,1), half4(G,1), half4(I,1)));
    93.                 float4 d = RGBtoYUV(half4x4(half4(D,1), half4(H,1), half4(F,1), half4(B,1)));
    94.                 float4 e = RGBtoYUV(half4x4(half4(E,1), half4(E,1), half4(E,1), half4(E,1)));
    95.                 float4 f = RGBtoYUV(half4x4(half4(F,1), half4(B,1), half4(D,1), half4(H,1)));
    96.                 float4 g = RGBtoYUV(half4x4(half4(G,1), half4(I,1), half4(C,1), half4(A,1)));
    97.                 float4 h = RGBtoYUV(half4x4(half4(H,1), half4(F,1), half4(B,1), half4(D,1)));
    98.                 float4 i = RGBtoYUV(half4x4(half4(I,1), half4(C,1), half4(A,1), half4(G,1)));
    99.                 float4 a1 = RGBtoYUV(half4x4(half4(A1,1), half4(G0,1), half4(I5,1), half4(C4,1)));
    100.                 float4 c1 = RGBtoYUV(half4x4(half4(C1,1), half4(A0,1), half4(G5,1), half4(I4,1)));
    101.                 float4 a0 = RGBtoYUV(half4x4(half4(A0,1), half4(G5,1), half4(I4,1), half4(C1,1)));
    102.                 float4 g0 = RGBtoYUV(half4x4(half4(G0,1), half4(I5,1), half4(C4,1), half4(A1,1)));
    103.                 float4 c4 = RGBtoYUV(half4x4(half4(C4,1), half4(A1,1), half4(G0,1), half4(I5,1)));
    104.                 float4 i4 = RGBtoYUV(half4x4(half4(I4,1), half4(C1,1), half4(A0,1), half4(G5,1)));
    105.                 float4 g5 = RGBtoYUV(half4x4(half4(G5,1), half4(I4,1), half4(C1,1), half4(A0,1)));
    106.                 float4 i5 = RGBtoYUV(half4x4(half4(I5,1), half4(C4,1), half4(A1,1), half4(G0,1)));
    107.                 float4 b1 = RGBtoYUV(half4x4(half4(B1,1), half4(D0,1), half4(H5,1), half4(F4,1)));
    108.                 float4 d0 = RGBtoYUV(half4x4(half4(D0,1), half4(H5,1), half4(F4,1), half4(B1,1)));
    109.                 float4 h5 = RGBtoYUV(half4x4(half4(H5,1), half4(F4,1), half4(B1,1), half4(D0,1)));
    110.                 float4 f4 = RGBtoYUV(half4x4(half4(F4,1), half4(B1,1), half4(D0,1), half4(H5,1)));
    111.              
    112.                 //interp_restriction_lv1 = ((e != f) && (e != h)); //must break up into seperate conditions
    113.                 bool iop1 = (e != f);
    114.                 bool iop2 = (e != h);
    115.                 interp_restriction_lv1 = iop1 && iop2;
    116.              
    117.                 //interp_restriction_lv2_left = ((e != g) && (d != g));
    118.                 bool irp1 = (e != g);
    119.                 bool irp2 = (d != g);
    120.                 interp_restriction_lv2_left = irp1 && irp2;
    121.              
    122.                 //interp_restriction_lv2_up = ((e != c) && (b != c));
    123.                 bool iru1 = (e != c);
    124.                 bool iru2 = (b != c);
    125.                 interp_restriction_lv2_up = iru1 && iru2;
    126.                 //edr = (weighted_distance(e, c, g, i, h5, f4, h, f) < weighted_distance(h, d, i5, f, i4, b, e, i)) && interp_restriction_lv1;
    127.                 bool ed1 = (weighted_distance(e, c, g, i, h5, f4, h, f) < weighted_distance(h, d, i5, f, i4, b, e, i));
    128.                 edr = ed1 && (iop1 && iop2); //interp_restriction_lv1 here throws error, so I replaced it with it's assignment
    129.              
    130.                 //edr_left = ((coef*df(f,g)) <= df(h,c)) && interp_restriction_lv2_left;
    131.                 bool edl1 = ((coef*df(f,g)) <= df(h,c));
    132.                 edr_left = edl1 && (irp1 && irp2);
    133.              
    134.                 //edr_up = (df(f,g) >= (coef*df(h,c))) && interp_restriction_lv2_up;
    135.                 bool edu1 = (df(f,g) >= (coef*df(h,c)));
    136.                 edr_up = edu1 && (iru1 && iru2);
    137.                 half3 E0 = E;
    138.                 half3 E1 = E;
    139.                 half3 E2 = E;
    140.                 half3 E3 = E;
    141.                 px = (df(e,f) <= df(e,h));
    142.                 half3 P[4];
    143.                 P[0] = px.x ? F : H;
    144.                 P[1] = px.y ? B : F;
    145.                 P[2] = px.z ? D : B;
    146.                 P[3] = px.w ? H : D;
    147.                 if (edr.x)
    148.                 {
    149.                     if (edr_left.x && edr_up.x)
    150.                     {
    151.                         E3 = lerp(E3 , P[0],  0.833333);
    152.                         E2 = lerp(E2 , P[0],  0.25);
    153.                         E1 = lerp(E1 , P[0],  0.25);
    154.                     }
    155.                     else if (edr_left.x)
    156.                     {
    157.                         E3 = lerp(E3 , P[0],  0.75);
    158.                         E2 = lerp(E2 , P[0],  0.25);
    159.                     }
    160.                     else if (edr_up.x)
    161.                     {
    162.                         E3 = lerp(E3 , P[0],  0.75);
    163.                         E1 = lerp(E1 , P[0],  0.25);
    164.                     }
    165.                     else
    166.                     {
    167.                         E3 = lerp(E3 , P[0],  0.5);
    168.                     }
    169.                 }
    170.                 if (edr.y)
    171.                 {
    172.                     if (edr_left.y && edr_up.y)
    173.                     {
    174.                         E1 = lerp(E1 , P[1],  0.833333);
    175.                         E3 = lerp(E3 , P[1],  0.25);
    176.                         E0 = lerp(E0 , P[1],  0.25);
    177.                     }
    178.                     else if (edr_left.y)
    179.                     {
    180.                         E1 = lerp(E1 , P[1],  0.75);
    181.                         E3 = lerp(E3 , P[1],  0.25);
    182.                     }
    183.                     else if (edr_up.y)
    184.                     {
    185.                         E1 = lerp(E1 , P[1],  0.75);
    186.                         E0 = lerp(E0 , P[1],  0.25);
    187.                     }
    188.                     else
    189.                     {
    190.                         E1 = lerp(E1 , P[1],  0.5);
    191.                     }
    192.                 }
    193.                 if (edr.z)
    194.                 {
    195.                     if (edr_left.z && edr_up.z)
    196.                     {
    197.                         E0 = lerp(E0 , P[2],  0.833333);
    198.                         E1 = lerp(E1 , P[2],  0.25);
    199.                         E2 = lerp(E2 , P[2],  0.25);
    200.                     }
    201.                     else if (edr_left.z)
    202.                     {
    203.                         E0 = lerp(E0 , P[2],  0.75);
    204.                         E1 = lerp(E1 , P[2],  0.25);
    205.                     }
    206.                     else if (edr_up.z)
    207.                     {
    208.                         E0 = lerp(E0 , P[2],  0.75);
    209.                         E2 = lerp(E2 , P[2],  0.25);
    210.                     }
    211.                     else
    212.                     {
    213.                         E0 = lerp(E0 , P[2],  0.5);
    214.                     }
    215.                 }
    216.                 if (edr.w)
    217.                 {
    218.                     if (edr_left.w && edr_up.w)
    219.                     {
    220.                         E2 = lerp(E2 , P[3],  0.833333);
    221.                         E0 = lerp(E0 , P[3],  0.25);
    222.                         E3 = lerp(E3 , P[3],  0.25);
    223.                     }
    224.                     else if (edr_left.w)
    225.                     {
    226.                         E2 = lerp(E2 , P[3],  0.75);
    227.                         E0 = lerp(E0 , P[3],  0.25);
    228.                     }
    229.                     else if (edr_up.w)
    230.                     {
    231.                         E2 = lerp(E2 , P[3],  0.75);
    232.                         E3 = lerp(E3 , P[3],  0.25);
    233.                     }
    234.                     else
    235.                     {
    236.                         E2 = lerp(E2 , P[3],  0.5);
    237.                     }
    238.                 }
    239.                 half3 res = (fp.x < 0.50) ? (fp.y < 0.50 ? E0 : E2) : (fp.y < 0.50 ? E1 : E3);
    240.                 return half4(res, 1.0);
    241.             }
    242.             ENDCG
    243.         }
    244.         ///////////////////////////////////
    245.     }
    246.     FallBack "Diffuse"
    247. }
     
  27. Sonoshee

    Sonoshee

    Joined:
    Jul 8, 2014
    Posts:
    77
    Basically the game is a pixel art game, the assets are designed to be displayed in a 512x300 screen, and then the screen gets scaled to 1024x600 (x2) to show that the game is a pixel art game. Now what I wanted is to have an option in the menu that's similar to "Enable retro mode". When on, the game is scaled normally, but when off, I apply an xBR shader that does an interpolation on the 1024x600 screen to smooth out the jaggies between the the big pixels on screen.

    I believe how the shader works is that you tell it what's the native resolution through a variable called texture_size, and then apply it as a post-processing effect to a camera, and then the shader will smooth out the 1024x600 screen knowing that the native resolution was 512x300. That's just my assumption and that's what these guys did with the XNA implementation of the shader : http://gamedev.stackexchange.com/questions/87275/how-do-i-perform-an-xbr-or-hqx-filter-in-xna
    But talking to Hyllian the author of the xBR algorithm he said that output_size should be set to the "texture size of your game view, which is a power-of-two value ( 256x256, 256x512, 512x1024...)" ( http://libretro.com/forums/showthread.php?t=88&page=18)

    The cg shader I gave to Lanre is an outdated one, thanks to him I imported this one that has an updated algorithm :

    Code (CSharp):
    1. Shader "Custom/2xBR"
    2. {
    3.     Properties
    4.     {
    5.         _Color("Color", Color) = (1,1,1,1) //You should set this field in Unity scripting, material.SetColor
    6.         _MainTex("Albedo (RGB)", 2D) = "white" {}
    7.         decal("decal (RGB)", 2D) = "white" {} //you should also set this in scripting with material.SetTexture ("decal", yourDecalTexture)
    8.     }
    9.     SubShader
    10.     {
    11.         Tags{ "RenderType" = "Opaque" }
    12.         ///////////////////////////////////
    13.         Pass
    14.         {
    15.             CGPROGRAM
    16.             // Upgrade NOTE: excluded shader from OpenGL ES 2.0 because it uses non-square matrices ( THIS NOTE WAS MADE BY UNITY)
    17.             #pragma exclude_renderers gles // NOTE : this was added automatically by Unity each time I compile the shader ( THIS NOTE WAS MADE BY SONOSHEE)
    18.             #include "UnityCG.cginc"
    19.             #pragma vertex main_vertex
    20.             #pragma fragment main_fragment
    21.             #pragma target 3.0
    22.  
    23.             uniform half4 _Color;
    24.             uniform sampler2D decal : TEXUNIT0;
    25.  
    26.             uniform float2 texture_size; // set from outside the shader ( THIS NOTE WAS MADE BY SONOSHEE)
    27.  
    28.             const static float coef = 2.0;
    29.             const static float4 eq_threshold = float4(15.0, 15.0, 15.0, 15.0);
    30.             const static float y_weight = 48.0;
    31.             const static float u_weight = 7.0;
    32.             const static float v_weight = 6.0;
    33.             const static float3x3 yuv = float3x3(0.299, 0.587, 0.114, -0.169, -0.331, 0.499, 0.499, -0.418, -0.0813);
    34.             const static float3x3 yuv_weighted = float3x3(y_weight*yuv[0], u_weight*yuv[1], v_weight*yuv[2]);
    35.             const static float4 delta = float4(0.5, 0.5, 0.5, 0.5);
    36.             const static float sharpness = 0.65;
    37.  
    38.             float4 df(float4 A, float4 B)
    39.             {
    40.                 return float4(abs(A - B));
    41.             }
    42.  
    43.             bool4 eq(float4 A, float4 B)
    44.             {
    45.                 return (df(A, B) < eq_threshold);
    46.             }
    47.  
    48.             bool4 eq2(float4 A, float4 B)
    49.             {
    50.                 return (df(A, B) < float4(2.0, 2.0, 2.0, 2.0));
    51.             }
    52.  
    53.  
    54.             float4 weighted_distance(float4 a, float4 b, float4 c, float4 d, float4 e, float4 f, float4 g, float4 h)
    55.             {
    56.                 return (df(a, b) + df(a, c) + df(d, e) + df(d, f) + 4.0*df(g, h));
    57.             }
    58.  
    59.             struct out_vertex
    60.             {
    61.                 float4 position : POSITION;
    62.                 float4 color : COLOR;
    63.                 float2 texCoord : TEXCOORD0;
    64.                 float4 t1       : TEXCOORD1;
    65.                 float4 t2       : TEXCOORD2;
    66.                 float4 t3       : TEXCOORD3;
    67.                 float4 t4       : TEXCOORD4;
    68.                 float4 t5       : TEXCOORD5;
    69.                 float4 t6       : TEXCOORD6;
    70.                 float4 t7       : TEXCOORD7;
    71.             };
    72.  
    73.             //VERTEX_SHADER
    74.             out_vertex main_vertex(appdata_base v)
    75.             {
    76.                 out_vertex OUT;
    77.  
    78.                 OUT.position = mul(UNITY_MATRIX_MVP, v.vertex);
    79.                 OUT.color = _Color;
    80.  
    81.                 float2 ps = float2(1.0 / texture_size.x, 1.0 / texture_size.y);
    82.                 float dx = ps.x;
    83.                 float dy = ps.y;
    84.  
    85.                 // A1 B1 C1
    86.                 // A0 A B C C4
    87.                 // D0 D E F F4
    88.                 // G0 G H I I4
    89.                 // G5 H5 I5
    90.  
    91.                 // This line fix a bug in ATI cards. ( THIS NOTE WAS MADE BY XBR AUTHOR)
    92.                 //float2 texCoord = texCoord1 + float2(0.0000001, 0.0000001);
    93.  
    94.                 OUT.texCoord = v.texcoord;
    95.                 OUT.t1 = v.texcoord.xxxy + float4(-dx, 0, dx, -2.0*dy); // A1 B1 C1
    96.                 OUT.t2 = v.texcoord.xxxy + float4(-dx, 0, dx, -dy); // A B C
    97.                 OUT.t3 = v.texcoord.xxxy + float4(-dx, 0, dx, 0); // D E F
    98.                 OUT.t4 = v.texcoord.xxxy + float4(-dx, 0, dx, dy); // G H I
    99.                 OUT.t5 = v.texcoord.xxxy + float4(-dx, 0, dx, 2.0*dy); // G5 H5 I5
    100.                 OUT.t6 = v.texcoord.xyyy + float4(-2.0*dx, -dy, 0, dy); // A0 D0 G0
    101.                 OUT.t7 = v.texcoord.xyyy + float4(2.0*dx, -dy, 0, dy); // C4 F4 I4
    102.  
    103.                 return OUT;
    104.             }
    105.  
    106.             //FRAGMENT SHADER
    107.             half4 main_fragment(out_vertex VAR) : COLOR
    108.             {
    109.                 bool4 edr, edr_left, edr_up, px; // px = pixel, edr = edge detection rule ( THIS NOTE WAS MADE BY XBR AUTHOR)
    110.                 bool4 interp_restriction_lv1, interp_restriction_lv2_left, interp_restriction_lv2_up;
    111.                 bool4 nc, nc30, nc60, nc45; // new_color ( THIS NOTE WAS MADE BY XBR AUTHOR)
    112.                 float4 fx, fx_left, fx_up, final_fx; // inequations of straight lines. ( THIS NOTE WAS MADE BY XBR AUTHOR)
    113.                 float3 res1, res2, pix1, pix2;
    114.                 float blend1, blend2;
    115.  
    116.                 float2 fp = frac(VAR.texCoord*texture_size);
    117.  
    118.                 float3 A1 = tex2D(decal, VAR.t1.xw).rgb;
    119.                 float3 B1 = tex2D(decal, VAR.t1.yw).rgb;
    120.                 float3 C1 = tex2D(decal, VAR.t1.zw).rgb;
    121.  
    122.                 float3 A = tex2D(decal, VAR.t2.xw).rgb;
    123.                 float3 B = tex2D(decal, VAR.t2.yw).rgb;
    124.                 float3 C = tex2D(decal, VAR.t2.zw).rgb;
    125.  
    126.                 float3 D = tex2D(decal, VAR.t3.xw).rgb;
    127.                 float3 E = tex2D(decal, VAR.t3.yw).rgb;
    128.                 float3 F = tex2D(decal, VAR.t3.zw).rgb;
    129.  
    130.                 float3 G = tex2D(decal, VAR.t4.xw).rgb;
    131.                 float3 H = tex2D(decal, VAR.t4.yw).rgb;
    132.                 float3 I = tex2D(decal, VAR.t4.zw).rgb;
    133.  
    134.                 float3 G5 = tex2D(decal, VAR.t5.xw).rgb;
    135.                 float3 H5 = tex2D(decal, VAR.t5.yw).rgb;
    136.                 float3 I5 = tex2D(decal, VAR.t5.zw).rgb;
    137.  
    138.                 float3 A0 = tex2D(decal, VAR.t6.xy).rgb;
    139.                 float3 D0 = tex2D(decal, VAR.t6.xz).rgb;
    140.                 float3 G0 = tex2D(decal, VAR.t6.xw).rgb;
    141.  
    142.                 float3 C4 = tex2D(decal, VAR.t7.xy).rgb;
    143.                 float3 F4 = tex2D(decal, VAR.t7.xz).rgb;
    144.                 float3 I4 = tex2D(decal, VAR.t7.xw).rgb;
    145.  
    146.                 float4 b = mul(float4x3(B, D, H, F), yuv_weighted[0]);
    147.                 float4 c = mul(float4x3(C, A, G, I), yuv_weighted[0]);
    148.                 float4 e = mul(float4x3(E, E, E, E), yuv_weighted[0]);
    149.                 float4 a = c.yzwx;
    150.                 float4 d = b.yzwx;
    151.                 float4 f = b.wxyz;
    152.                 float4 g = c.zwxy;
    153.                 float4 h = b.zwxy;
    154.                 float4 i = c.wxyz;
    155.  
    156.                 float4 i4 = mul(float4x3(I4, C1, A0, G5), yuv_weighted[0]);
    157.                 float4 i5 = mul(float4x3(I5, C4, A1, G0), yuv_weighted[0]);
    158.                 float4 h5 = mul(float4x3(H5, F4, B1, D0), yuv_weighted[0]);
    159.                 float4 f4 = h5.yzwx;
    160.  
    161.  
    162.                 float4 Ao = float4(1.0, -1.0, -1.0, 1.0);
    163.                 float4 Bo = float4(1.0,  1.0, -1.0,-1.0);
    164.                 float4 Co = float4(1.5,  0.5, -0.5, 0.5);
    165.                 float4 Ax = float4(1.0, -1.0, -1.0, 1.0);
    166.                 float4 Bx = float4(0.5,  2.0, -0.5,-2.0);
    167.                 float4 Cx = float4(1.0,  1.0, -0.5, 0.0);
    168.                 float4 Ay = float4(1.0, -1.0, -1.0, 1.0);
    169.                 float4 By = float4(2.0,  0.5, -2.0,-0.5);
    170.                 float4 Cy = float4(2.0,  0.0, -1.0, 0.5);
    171.  
    172.                 // These inequations define the line below which interpolation occurs. ( THIS NOTE WAS MADE BY XBR AUTHOR)
    173.                 fx = (Ao*fp.y + Bo*fp.x);
    174.                 fx_left = (Ax*fp.y + Bx*fp.x);
    175.                 fx_up = (Ay*fp.y + By*fp.x);
    176.  
    177.                 interp_restriction_lv1 = ((e != f) && (e != h) && ((eq2(e, b) || eq2(e, d) || !eq2(e, a)) && (eq2(f, f4) || eq2(f, c) || eq2(h, h5) || eq2(h, g))) && (!eq(f, b) && !eq(f, c) || !eq(h, d) && !eq(h, g) || eq(e, i) && (!eq(f, f4) && !eq(f, i4) || !eq(h, h5) && !eq(h, i5)) || eq(e, g) || eq(e, c)));
    178.                 interp_restriction_lv2_left = ((e != g) && (d != g));
    179.                 interp_restriction_lv2_up = ((e != c) && (b != c));
    180.  
    181.                 float4 fx45 = smoothstep(Co - delta, Co + delta, fx);
    182.                 float4 fx30 = smoothstep(Cx - delta, Cx + delta, fx_left);
    183.                 float4 fx60 = smoothstep(Cy - delta, Cy + delta, fx_up);
    184.  
    185.  
    186.                 edr = ((weighted_distance(e, c, g, i, h5, f4, h, f) + 3.5) < weighted_distance(h, d, i5, f, i4, b, e, i)) && interp_restriction_lv1;
    187.                 edr_left = ((coef*df(f,g)) <= df(h,c)) && interp_restriction_lv2_left;
    188.                 edr_up = (df(f,g) >= (coef*df(h,c))) && interp_restriction_lv2_up;
    189.  
    190.                 nc45 = (edr &&             bool4(fx45));
    191.                 nc30 = (edr && edr_left && bool4(fx30));
    192.                 nc60 = (edr && edr_up   && bool4(fx60));
    193.  
    194.                 px = (df(e,f) <= df(e,h));
    195.  
    196.                 float3 res = E;
    197.  
    198.  
    199.                 float3 n1, n2, n3, n4, s, aa, bb, cc, dd;
    200.  
    201.  
    202.                 n1 = B1; n2 = B; s = E; n3 = H; n4 = H5;
    203.                 aa = n2 - n1; bb = s - n2; cc = n3 - s; dd = n4 - n3;
    204.  
    205.                 float3 t = (7 * (bb + cc) - 3 * (aa + dd)) / 16;
    206.  
    207.                 float3 m = (s < 0.5) ? 2 * s : 2 * (1.0 - s);
    208.  
    209.                 m = min(m, 2*abs(bb));
    210.                 m = min(m, 2*abs(cc));
    211.  
    212.                 t = clamp(t, -m, m);
    213.  
    214.                 float3 s0 = (2 * fp.x - 1)*t + s;
    215.  
    216.                 res = s0;
    217.  
    218.                 nc = (nc30 || nc60 || nc45);
    219.  
    220.                 float blend = 0.0;
    221.                 float3 pix = res;
    222.  
    223.                 float4 final45 = dot(nc45, fx45);
    224.                 float4 final30 = dot(nc30, fx30);
    225.                 float4 final60 = dot(nc60, fx60);
    226.  
    227.                 float4 maximo = max(max(final30, final60), final45);
    228.  
    229.                 if (nc.x) { pix = px.x ? F : H; blend = maximo.x; }
    230.                 else if (nc.y) { pix = px.y ? B : F; blend = maximo.y; }
    231.                 else if (nc.z) { pix = px.z ? D : B; blend = maximo.z; }
    232.                 else if (nc.w) { pix = px.w ? H : D; blend = maximo.w; }
    233.  
    234.                 res = lerp(res, pix, blend);
    235.  
    236.                 return float4(res, 1.0);
    237.             }
    238.  
    239.             ENDCG
    240.         }
    241.     }
    242.     FallBack "Diffuse"
    243. }
    I suggest you guys take a look at what I posted on Libretro about what are the issues I'm having so far with the shader ( link above)

    EDIT : Hyllian just said that the most updated one is : https://github.com/libretro/common-shaders/blob/master/xbr/shaders/xbr-lv2.cg
    I'll import it into Unity and let you know how are things going.
     
    Last edited: Jul 1, 2015
  28. Sonoshee

    Sonoshee

    Joined:
    Jul 8, 2014
    Posts:
    77
    This is how it ended up : http://pastebin.com/raw.php?i=jYndTXTn

    But I'm getting this error :
    " Shader error in 'Custom/2xBR': incorrect number of arguments to numeric-type constructor at line 89 (on d3d11)

    Compiling Vertex program "
     
  29. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Hey man, I'll have to implement the shader and test it out myself. I'll get all your woes fixed (hopefully) and report back. Give me a couple hours.
     
  30. Sonoshee

    Sonoshee

    Joined:
    Jul 8, 2014
    Posts:
    77
    I got rid of the errror by making XBR_EQ_THRESHOLD a float4 variable instead of float, setting its value to (15.0,0.0,50.0,1.0) instead of 15.0, and then modifying the eq() function from :
    Code (CSharp):
    1. bool4 eq(float4 A, float4 B)
    2. {
    3.         return (df(A, B) < float4(XBR_EQ_THRESHOLD));
    4. }
    to :
    Code (CSharp):
    1. bool4 eq(float4 A, float4 B)
    2. {
    3.     return (df(A, B) < XBR_EQ_THRESHOLD);
    4. }
    5.  
    The result isn't any better than the last shader, I'm frankly losing hope guys haha but that's okay since the problem is probably with the shader logic, can't blame ourselves.
     
  31. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Hey man. The shader I have works wonders. You'll have to port the new version the same way we did the old. I downsampled to half res and the upscaler works magic, it looks native res. I'll upload how I did it in a sec.
     
  32. Sonoshee

    Sonoshee

    Joined:
    Jul 8, 2014
    Posts:
    77
    OH I'M EXCITED! what do you mean by "it looks native res" ? emm are you aware that if for example you have this :
    (native)
    it should look like this :
    (2x using xBR)
    and not like this :

    (2x using normal scaling)
     
  33. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Okay man. The shader really does wonders. So I'll walk you through step by step. First of all, here's the shader:
    Code (CSharp):
    1. Shader "Custom/2xBR" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1) //You should set this field in Unity scripting, material.SetColor
    4.         decal ("decal (RGB)", 2D) = "white" {} //you should also set this in scripting with material.SetTexture ("decal", yourDecalTexture)
    5.     }
    6.     SubShader {
    7.         Tags { "RenderType"="Opaque" }
    8.         ///////////////////////////////////
    9.         Pass {
    10.             CGPROGRAM
    11.             #include "UnityCG.cginc"
    12.             #pragma vertex main_vertex
    13.             #pragma fragment main_fragment
    14.             #pragma target 3.0
    15.            
    16.             uniform half4 _Color;
    17.             uniform sampler2D decal : TEXUNIT0;
    18.             uniform float2 texture_size; //You should set this in Unity (your script) using material.SetVector("texture_size", outputSize float);
    19.            
    20.             const static float coef = 2.0;
    21.             const static float3 dtt = float3(65536,255,1);
    22.             const static half y_weight = 48.0;
    23.             const static half u_weight = 7.0;
    24.             const static half v_weight = 6.0;
    25.             const static half3x3 yuv = half3x3(0.299, 0.587, 0.114, -0.169, -0.331, 0.499, 0.499, -0.418, -0.0813);
    26.             const static half3x3 yuv_weighted = half3x3(y_weight*yuv[0], u_weight*yuv[1], v_weight*yuv[2]);
    27.  
    28.             //const static half3x3 yuv_weighted = half3x3(14.352, 28.176, 5.472, -1.183, -2.317, 3.5, 3.0, -2.514, -0.486);
    29.  
    30.             float4 RGBtoYUV(half4x4 mat_color) {
    31.                 float a = abs(mul(yuv_weighted, mat_color[0].xyz));
    32.                 float b = abs(mul(yuv_weighted, mat_color[1].xyz));
    33.                 float c = abs(mul(yuv_weighted, mat_color[2].xyz));
    34.                 float d = abs(mul(yuv_weighted, mat_color[3].xyz));
    35.  
    36.                 return float4(a, b, c, d);
    37.             }
    38.  
    39.             float4 df(float4 A, float4 B) {
    40.                 return float4(abs(A - B));
    41.             }
    42.  
    43.  
    44.             float4 weighted_distance(float4 a, float4 b, float4 c, float4 d, float4 e, float4 f, float4 g, float4 h) {
    45.                 return (df(a,b) + df(a,c) + df(d,e) + df(d,f) + 4.0*df(g,h));
    46.             }
    47.  
    48.             struct out_vertex {
    49.                 half4 position : POSITION;
    50.                 half4 color    : COLOR;
    51.                 float2 texCoord : TEXCOORD0;
    52.                 half4 t1 : TEXCOORD1;
    53.             };
    54.  
    55.             //    VERTEX_SHADER
    56.             out_vertex main_vertex ( appdata_base v ) {
    57.                 out_vertex OUT;
    58.  
    59.                 OUT.position = mul(UNITY_MATRIX_MVP, v.vertex);
    60.                 OUT.color = _Color;
    61.  
    62.                 half2 ps = half2(1.0 / texture_size.x, 1.0 / texture_size.y);
    63.                 half dx = ps.x;
    64.                 half dy = ps.y;
    65.  
    66.                 OUT.texCoord = v.texcoord;
    67.                 OUT.t1.xy = half2(dx,  0); // F
    68.                 OUT.t1.zw = half2(0, dy); // H
    69.  
    70.                 return OUT;
    71.             }
    72.  
    73.             //   FRAGMENT SHADER
    74.             half4 main_fragment( out_vertex VAR ) : COLOR {
    75.                 bool4 edr, edr_left, edr_up, px; // px = pixel, edr = edge detection rule
    76.                 bool4 interp_restriction_lv1, interp_restriction_lv2_left, interp_restriction_lv2_up;
    77.  
    78.                 float2 fp = frac(VAR.texCoord*texture_size);
    79.  
    80.                 half2 dx = VAR.t1.xy;
    81.                 half2 dy = VAR.t1.zw;
    82.  
    83.                 half3 A = tex2D(decal, VAR.texCoord - dx - dy).xyz;
    84.                 half3 B = tex2D(decal, VAR.texCoord - dy).xyz;
    85.                 half3 C = tex2D(decal, VAR.texCoord + dx - dy).xyz;
    86.                 half3 D = tex2D(decal, VAR.texCoord - dx).xyz;
    87.                 half3 E = tex2D(decal, VAR.texCoord).xyz;
    88.                 half3 F = tex2D(decal, VAR.texCoord + dx).xyz;
    89.                 half3 G = tex2D(decal, VAR.texCoord - dx + dy).xyz;
    90.                 half3 H = tex2D(decal, VAR.texCoord + dy).xyz;
    91.                 half3 I = tex2D(decal, VAR.texCoord + dx + dy).xyz;
    92.  
    93.                 half3  A1 = tex2D(decal, VAR.texCoord - dx - 2.0*dy).xyz;
    94.                 half3  C1 = tex2D(decal, VAR.texCoord + dx - 2.0*dy).xyz;
    95.                 half3  A0 = tex2D(decal, VAR.texCoord - 2.0*dx - dy).xyz;
    96.                 half3  G0 = tex2D(decal, VAR.texCoord - 2.0*dx + dy).xyz;
    97.                 half3  C4 = tex2D(decal, VAR.texCoord + 2.0*dx - dy).xyz;
    98.                 half3  I4 = tex2D(decal, VAR.texCoord + 2.0*dx + dy).xyz;
    99.                 half3  G5 = tex2D(decal, VAR.texCoord - dx + 2.0*dy).xyz;
    100.                 half3  I5 = tex2D(decal, VAR.texCoord + dx + 2.0*dy).xyz;
    101.                 half3  B1 = tex2D(decal, VAR.texCoord - 2.0*dy).xyz;
    102.                 half3  D0 = tex2D(decal, VAR.texCoord - 2.0*dx).xyz;
    103.                 half3  H5 = tex2D(decal, VAR.texCoord + 2.0*dy).xyz;
    104.                 half3  F4 = tex2D(decal, VAR.texCoord + 2.0*dx).xyz;
    105.  
    106.                 float4 a = RGBtoYUV(half4x4(half4(A,1), half4(G,1), half4(I,1), half4(C,1)));
    107.                 float4 b = RGBtoYUV(half4x4(half4(B,1), half4(D,1), half4(H,1), half4(F,1)));
    108.                 float4 c = RGBtoYUV(half4x4(half4(C,1), half4(A,1), half4(G,1), half4(I,1)));
    109.                 float4 d = RGBtoYUV(half4x4(half4(D,1), half4(H,1), half4(F,1), half4(B,1)));
    110.                 float4 e = RGBtoYUV(half4x4(half4(E,1), half4(E,1), half4(E,1), half4(E,1)));
    111.                 float4 f = RGBtoYUV(half4x4(half4(F,1), half4(B,1), half4(D,1), half4(H,1)));
    112.                 float4 g = RGBtoYUV(half4x4(half4(G,1), half4(I,1), half4(C,1), half4(A,1)));
    113.                 float4 h = RGBtoYUV(half4x4(half4(H,1), half4(F,1), half4(B,1), half4(D,1)));
    114.                 float4 i = RGBtoYUV(half4x4(half4(I,1), half4(C,1), half4(A,1), half4(G,1)));
    115.  
    116.                 float4 a1 = RGBtoYUV(half4x4(half4(A1,1), half4(G0,1), half4(I5,1), half4(C4,1)));
    117.                 float4 c1 = RGBtoYUV(half4x4(half4(C1,1), half4(A0,1), half4(G5,1), half4(I4,1)));
    118.                 float4 a0 = RGBtoYUV(half4x4(half4(A0,1), half4(G5,1), half4(I4,1), half4(C1,1)));
    119.                 float4 g0 = RGBtoYUV(half4x4(half4(G0,1), half4(I5,1), half4(C4,1), half4(A1,1)));
    120.                 float4 c4 = RGBtoYUV(half4x4(half4(C4,1), half4(A1,1), half4(G0,1), half4(I5,1)));
    121.                 float4 i4 = RGBtoYUV(half4x4(half4(I4,1), half4(C1,1), half4(A0,1), half4(G5,1)));
    122.                 float4 g5 = RGBtoYUV(half4x4(half4(G5,1), half4(I4,1), half4(C1,1), half4(A0,1)));
    123.                 float4 i5 = RGBtoYUV(half4x4(half4(I5,1), half4(C4,1), half4(A1,1), half4(G0,1)));
    124.                 float4 b1 = RGBtoYUV(half4x4(half4(B1,1), half4(D0,1), half4(H5,1), half4(F4,1)));
    125.                 float4 d0 = RGBtoYUV(half4x4(half4(D0,1), half4(H5,1), half4(F4,1), half4(B1,1)));
    126.                 float4 h5 = RGBtoYUV(half4x4(half4(H5,1), half4(F4,1), half4(B1,1), half4(D0,1)));
    127.                 float4 f4 = RGBtoYUV(half4x4(half4(F4,1), half4(B1,1), half4(D0,1), half4(H5,1)));
    128.                
    129.                 //interp_restriction_lv1 = ((e != f) && (e != h)); //must break up into seperate conditions
    130.                 bool iop1 = (e != f);
    131.                 bool iop2 = (e != h);
    132.                 interp_restriction_lv1 = bool4( iop1 && iop2, iop1 && iop2, iop1 && iop2, iop1 && iop2);
    133.                
    134.                 //interp_restriction_lv2_left = ((e != g) && (d != g));
    135.                 bool irp1 = (e != g);
    136.                 bool irp2 = (d != g);
    137.                 interp_restriction_lv2_left = bool4(irp1 && irp2, irp1 && irp2, irp1 && irp2, irp1 && irp2);
    138.                
    139.                 //interp_restriction_lv2_up = ((e != c) && (b != c));
    140.                 bool iru1 = (e != c);
    141.                 bool iru2 = (b != c);
    142.                 interp_restriction_lv2_up = bool4(iru1 && iru2, iru1 && iru2, iru1 && iru2, iru1 && iru2);
    143.  
    144.                 //edr = (weighted_distance(e, c, g, i, h5, f4, h, f) < weighted_distance(h, d, i5, f, i4, b, e, i)) && interp_restriction_lv1;
    145.                 bool ed1 = (weighted_distance(e, c, g, i, h5, f4, h, f) < weighted_distance(h, d, i5, f, i4, b, e, i));
    146.                 edr = bool4(ed1 && (iop1 && iop2), ed1 && (iop1 && iop2), ed1 && (iop1 && iop2), ed1 && (iop1 && iop2)); //interp_restriction_lv1 here throws error, so I replaced it with it's assignment
    147.                
    148.                 //edr_left = ((coef*df(f,g)) <= df(h,c)) && interp_restriction_lv2_left;
    149.                 bool edl1 = ((coef*df(f,g)) <= df(h,c));
    150.                 edr_left = bool4(edl1 && (irp1 && irp2), edl1 && (irp1 && irp2), edl1 && (irp1 && irp2), edl1 && (irp1 && irp2));
    151.                
    152.                 //edr_up = (df(f,g) >= (coef*df(h,c))) && interp_restriction_lv2_up;
    153.                 bool edu1 = (df(f,g) >= (coef*df(h,c)));
    154.                 edr_up = bool4(edu1 && (iru1 && iru2), edu1 && (iru1 && iru2), edu1 && (iru1 && iru2), edu1 && (iru1 && iru2));
    155.  
    156.  
    157.                 half3 E0 = E;
    158.                 half3 E1 = E;
    159.                 half3 E2 = E;
    160.                 half3 E3 = E;
    161.  
    162.                 px = (df(e,f) <= df(e,h));
    163.  
    164.                 half3 P[4];
    165.  
    166.                 P[0] = px.x ? F : H;
    167.                 P[1] = px.y ? B : F;
    168.                 P[2] = px.z ? D : B;
    169.                 P[3] = px.w ? H : D;
    170.  
    171.  
    172.                 if (edr.x)
    173.                 {
    174.                     if (edr_left.x && edr_up.x)
    175.                     {
    176.                         E3 = lerp(E3 , P[0],  0.833333);
    177.                         E2 = lerp(E2 , P[0],  0.25);
    178.                         E1 = lerp(E1 , P[0],  0.25);
    179.                     }
    180.                     else if (edr_left.x)
    181.                     {
    182.                         E3 = lerp(E3 , P[0],  0.75);
    183.                         E2 = lerp(E2 , P[0],  0.25);
    184.                     }
    185.                     else if (edr_up.x)
    186.                     {
    187.                         E3 = lerp(E3 , P[0],  0.75);
    188.                         E1 = lerp(E1 , P[0],  0.25);
    189.                     }
    190.                     else
    191.                     {
    192.                         E3 = lerp(E3 , P[0],  0.5);
    193.                     }
    194.                 }
    195.  
    196.                 if (edr.y)
    197.                 {
    198.                     if (edr_left.y && edr_up.y)
    199.                     {
    200.                         E1 = lerp(E1 , P[1],  0.833333);
    201.                         E3 = lerp(E3 , P[1],  0.25);
    202.                         E0 = lerp(E0 , P[1],  0.25);
    203.                     }
    204.                     else if (edr_left.y)
    205.                     {
    206.                         E1 = lerp(E1 , P[1],  0.75);
    207.                         E3 = lerp(E3 , P[1],  0.25);
    208.                     }
    209.                     else if (edr_up.y)
    210.                     {
    211.                         E1 = lerp(E1 , P[1],  0.75);
    212.                         E0 = lerp(E0 , P[1],  0.25);
    213.                     }
    214.                     else
    215.                     {
    216.                         E1 = lerp(E1 , P[1],  0.5);
    217.                     }
    218.                 }
    219.  
    220.                 if (edr.z)
    221.                 {
    222.                     if (edr_left.z && edr_up.z)
    223.                     {
    224.                         E0 = lerp(E0 , P[2],  0.833333);
    225.                         E1 = lerp(E1 , P[2],  0.25);
    226.                         E2 = lerp(E2 , P[2],  0.25);
    227.                     }
    228.                     else if (edr_left.z)
    229.                     {
    230.                         E0 = lerp(E0 , P[2],  0.75);
    231.                         E1 = lerp(E1 , P[2],  0.25);
    232.                     }
    233.                     else if (edr_up.z)
    234.                     {
    235.                         E0 = lerp(E0 , P[2],  0.75);
    236.                         E2 = lerp(E2 , P[2],  0.25);
    237.                     }
    238.                     else
    239.                     {
    240.                         E0 = lerp(E0 , P[2],  0.5);
    241.                     }
    242.                 }
    243.  
    244.                 if (edr.w)
    245.                 {
    246.                     if (edr_left.w && edr_up.w)
    247.                     {
    248.                         E2 = lerp(E2 , P[3],  0.833333);
    249.                         E0 = lerp(E0 , P[3],  0.25);
    250.                         E3 = lerp(E3 , P[3],  0.25);
    251.                     }
    252.                     else if (edr_left.w)
    253.                     {
    254.                         E2 = lerp(E2 , P[3],  0.75);
    255.                         E0 = lerp(E0 , P[3],  0.25);
    256.                     }
    257.                     else if (edr_up.w)
    258.                     {
    259.                         E2 = lerp(E2 , P[3],  0.75);
    260.                         E3 = lerp(E3 , P[3],  0.25);
    261.                     }
    262.                     else
    263.                     {
    264.                         E2 = lerp(E2 , P[3],  0.5);
    265.                     }
    266.                 }
    267.  
    268.                 half3 res = (fp.x < 0.50) ? (fp.y < 0.50 ? E0 : E2) : (fp.y < 0.50 ? E1 : E3);
    269.  
    270.                 return half4(res, 1.0);
    271.                 //return tex2D(decal, VAR.texCoord);
    272.             }
    273.             ENDCG
    274.         }
    275.         ///////////////////////////////////
    276.     }
    277.     FallBack "Diffuse"
    278. }
    279.  
    Next. You have to deal with scripting. Here's the script (javascript, so you may port it to C# if you wish):
    Code (CSharp):
    1. #pragma strict
    2.  
    3. public var shader : Shader;
    4. public var textureSize : Vector2;
    5. @Range(1f,5f)public var downSample : float = 2f;
    6.  
    7. private var mat : Material;
    8. private var reTex : RenderTexture;
    9. private var desTex : RenderTexture;
    10.  
    11. function Start () {
    12.     mat = new Material(shader);
    13. }
    14.  
    15. function OnPreRender () {
    16.     if (reTex == null) {
    17.         reTex = new RenderTexture(Screen.width, Screen.height, 0);
    18.         reTex.Create();
    19.     }
    20.     GetComponent(Camera).targetTexture = reTex;
    21. }
    22.  
    23. function OnPostRender () {
    24.     mat.SetVector("texture_size", textureSize);
    25.     mat.SetTexture("decal", reTex);
    26.    
    27.     desTex = RenderTexture.GetTemporary(Mathf.RoundToInt(Screen.width/downSample), Mathf.RoundToInt(Screen.height/downSample), 0);
    28.     //Debug.Log(Mathf.RoundToInt(Screen.width/downSample));
    29.     Graphics.Blit(reTex, desTex);
    30.     Graphics.Blit(desTex, null as RenderTexture, mat);
    31.     RenderTexture.ReleaseTemporary(desTex);
    32.    
    33.     //Graphics.Blit(null, null, mat);
    34.     GetComponent(Camera).targetTexture = null;
    35.     reTex.DiscardContents();
    36. }
    Attach the script to your camera and you should be fine.

    As you can see, it's not much different from what you had previously. The shader works by taking in a downsampled image and working on it so it doesn't look jagged when upscaled. So first we have to downsample the camera view, which is why I blit reTex to a half resolution render texture, desTex. Then blit desTex to screen using the xBR shader. Note that the image will only occupy a portion of the screen if you start at a lower resolution, then maximize the window. This is because reTex is created only once (in the first run of OnPreRender). You may change this behaviour if you wish.
     
    chenwanwan13 likes this.
  34. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Oh and texture size should always be screen.width by screen.height
     
  35. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    I seem to have made a mistake, so so sorry. I omitted decal.
     
  36. Sonoshee

    Sonoshee

    Joined:
    Jul 8, 2014
    Posts:
    77
    Forgive me for being such a newbie, but I'm afraid I don't understand what to fill the values with.
    I have a 512x300 native resolution and the game is displayed in a 1024x600 resolution, is it supposed to work when setting texture size to 1024x600 and downsample to 2? ( also, you said you omitted decal, have you edited the post where you've omitted it? because I'm afraid I might have copied the non-edited version)
     
  37. I'd love to know how to use this as well.