Search Unity

[Unity 5.6.3] Blur shader not working on UWP Black screen

Discussion in 'Windows' started by BorisMulev, Sep 12, 2017.

  1. BorisMulev

    BorisMulev

    Joined:
    May 23, 2017
    Posts:
    15
    If play in unity editor (WSA player) work good.
    Create uwp app and run black screen.(other platform work fine).

    I understand this is a shader compiled(UWP) so works.


    View Blur Overlay:

    Code (CSharp):
    1.    solidWhite = new Color(1, 1, 1, 1);
    2.         invisibleWhite = new Color(1, 1, 1, 0);
    3.         gameGrabCamera.enabled = false;
    4.         shapeGrabCamera.enabled = false;
    5.         renderGUICamera.enabled = false;
    6.  
    7.         gameTexture = new Texture2D(gameGrabCamera.targetTexture.width, gameGrabCamera.targetTexture.height, TextureFormat.ARGB32, false);
    8.         gameGrab.GetComponent<Renderer>().material.mainTexture = gameTexture;
    9.  
    10.         shapeTexture = new Texture2D(shapeGrabCamera.targetTexture.width, shapeGrabCamera.targetTexture.height, TextureFormat.ARGB32, false);
    11.         shapeGrab.GetComponent<Renderer>().material.mainTexture = shapeTexture;
    12.  
    13.         blurGUITexture = new Texture2D(renderGUICamera.targetTexture.width, renderGUICamera.targetTexture.height, TextureFormat.ARGB32, false);
    14.         darkenGUIGrab.GetComponent<Renderer>().material.mainTexture = blurGUITexture;
    15.  
    16. .......
    17.  
    18.     public IEnumerator FadeIn(float duration, Camera grabCamera, GameObject grabObject, Texture2D grabTexture)
    19.     {
    20.  
    21.         // turn on camera to take (non blurred) grab of the game o rmain menu shape
    22.         grabCamera.enabled = true;
    23.  
    24.         grabCamera.targetTexture.DiscardContents();
    25.  
    26.         // wait for it to render
    27.         yield return new WaitForEndOfFrame();
    28.  
    29.         // grab the pixels from the render texture & put on a quad that the GUI View layer can see
    30.         GetRTPixels(grabCamera.targetTexture, grabTexture);
    31.  
    32.         // File.WriteAllBytes("game.png", grabTexture.EncodeToPNG()); // write to file for debugging
    33.  
    34.         // turn off 3d grabbing camera now we have the texture
    35.         grabCamera.enabled = false;
    36.  
    37.         // show the screen grab
    38.         grabObject.SetActive(true);
    39.  
    40.         // turn on the blur / darken
    41.         darkenLayer.SetActive(true);
    42.  
    43.         // turn on camera to render combined, blurred image
    44.         renderGUICamera.enabled = true;
    45.  
    46.         // wait for it to render
    47.         yield return new WaitForEndOfFrame();
    48.  
    49.         // get pixel and show that
    50.         GetRTPixels(renderGUICamera.targetTexture, blurGUITexture);
    51.         darkenGUIGrab.SetActive(true);
    52.  
    53.         // turn off gui blur camera
    54.         renderGUICamera.enabled = false;
    55.  
    56.         // turn off working layers
    57.         darkenLayer.SetActive(false);
    58.         grabObject.SetActive(false);
    59.  
    60.         // fade the combined, blurred image in
    61.         darkenGUIGrab.GetComponent<Renderer>().material.color = invisibleWhite;
    62.  
    63.  
    64.         var gui = darkenGUIGrab.GetComponent<Renderer>();
    65.         DOTween.To(() => gui.material.color, x => gui.material.color = x, solidWhite, duration).OnComplete(onShowComplete).SetUpdate(true);
    66.     }
    67.  
    68.     RenderTexture currentActiveRT;
    69.     private void GetRTPixels(RenderTexture rt, Texture2D tex)
    70.     {
    71.         currentActiveRT = RenderTexture.active;
    72.         RenderTexture.active = rt;
    73.         tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);
    74.         tex.Apply();
    75.         RenderTexture.active = currentActiveRT;
    76.     }
    Blur shader:

    Code (CSharp):
    1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    2.  
    3. Shader "Custom/SimpleGrabPassBlur" {
    4.     Properties {
    5.         _Color ("Main Color", Color) = (1,1,1,1)
    6.         _BumpAmt  ("Distortion", Range (0,128)) = 10
    7.         _MainTex ("Tint Color (RGB)", 2D) = "white" {}
    8.         _BumpMap ("Normalmap", 2D) = "bump" {}
    9.         _Size ("Size", Range(0, 20)) = 1
    10.     }
    11.  
    12.     Category {
    13.  
    14.         // We must be transparent, so other objects are drawn before this one.
    15.         Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Opaque" }
    16.  
    17.  
    18.         SubShader {
    19.  
    20.             // Horizontal blur
    21.             GrabPass {                
    22.                 Tags { "LightMode" = "Always" }
    23.             }
    24.             Pass {
    25.                 Tags { "LightMode" = "Always" }
    26.          
    27.                 CGPROGRAM
    28.                 #pragma vertex vert
    29.                 #pragma fragment frag
    30.                 #pragma fragmentoption ARB_precision_hint_fastest
    31.                 #include "UnityCG.cginc"
    32.          
    33.                 struct appdata_t {
    34.                     float4 vertex : POSITION;
    35.                     float2 texcoord: TEXCOORD0;
    36.                 };
    37.          
    38.                 struct v2f {
    39.                     float4 vertex : POSITION;
    40.                     float4 uvgrab : TEXCOORD0;
    41.                 };
    42.          
    43.                 v2f vert (appdata_t v) {
    44.                     v2f o;
    45.                     o.vertex = UnityObjectToClipPos(v.vertex);
    46.                     #if UNITY_UV_STARTS_AT_TOP
    47.                     float scale = -1.0;
    48.                     #else
    49.                     float scale = 1.0;
    50.                     #endif
    51.                     o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
    52.                     o.uvgrab.zw = o.vertex.zw;
    53.                     return o;
    54.                 }
    55.          
    56.                 sampler2D _GrabTexture;
    57.                 float4 _GrabTexture_TexelSize;
    58.                 float _Size;
    59.          
    60.                 half4 frag( v2f i ) : COLOR {
    61. //                  half4 col = tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(i.uvgrab));
    62. //                  return col;
    63.              
    64.                     half4 sum = half4(0,0,0,0);
    65.                     #define GRABPIXEL(weight,kernelx) tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x + _GrabTexture_TexelSize.x * kernelx*_Size, i.uvgrab.y, i.uvgrab.z, i.uvgrab.w))) * weight
    66.                     sum += GRABPIXEL(0.05, -4.0);
    67.                     sum += GRABPIXEL(0.09, -3.0);
    68.                     sum += GRABPIXEL(0.12, -2.0);
    69.                     sum += GRABPIXEL(0.15, -1.0);
    70.                     sum += GRABPIXEL(0.18,  0.0);
    71.                     sum += GRABPIXEL(0.15, +1.0);
    72.                     sum += GRABPIXEL(0.12, +2.0);
    73.                     sum += GRABPIXEL(0.09, +3.0);
    74.                     sum += GRABPIXEL(0.05, +4.0);
    75.              
    76.                     return sum;
    77.                 }
    78.                 ENDCG
    79.             }
    80.             // Vertical blur
    81.             GrabPass {                    
    82.                 Tags { "LightMode" = "Always" }
    83.             }
    84.             Pass {
    85.                 Tags { "LightMode" = "Always" }
    86.          
    87.                 CGPROGRAM
    88.                 #pragma vertex vert
    89.                 #pragma fragment frag
    90.                 #pragma fragmentoption ARB_precision_hint_fastest
    91.                 #include "UnityCG.cginc"
    92.          
    93.                 struct appdata_t {
    94.                     float4 vertex : POSITION;
    95.                     float2 texcoord: TEXCOORD0;
    96.                 };
    97.          
    98.                 struct v2f {
    99.                     float4 vertex : POSITION;
    100.                     float4 uvgrab : TEXCOORD0;
    101.                 };
    102.          
    103.                 v2f vert (appdata_t v) {
    104.                     v2f o;
    105.                     o.vertex = UnityObjectToClipPos(v.vertex);
    106.                     #if UNITY_UV_STARTS_AT_TOP
    107.                     float scale = -1.0;
    108.                     #else
    109.                     float scale = 1.0;
    110.                     #endif
    111.                     o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
    112.                     o.uvgrab.zw = o.vertex.zw;
    113.                     return o;
    114.                 }
    115.          
    116.                 sampler2D _GrabTexture;
    117.                 float4 _GrabTexture_TexelSize;
    118.                 float _Size;
    119.          
    120.                 half4 frag( v2f i ) : COLOR {
    121. //                  half4 col = tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(i.uvgrab));
    122. //                  return col;
    123.              
    124.                     half4 sum = half4(0,0,0,0);
    125.                     #define GRABPIXEL(weight,kernely) tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x, i.uvgrab.y + _GrabTexture_TexelSize.y * kernely*_Size, i.uvgrab.z, i.uvgrab.w))) * weight
    126.                     //G(X) = (1/(sqrt(2*PI*deviation*deviation))) * exp(-(x*x / (2*deviation*deviation)))
    127.              
    128.                     sum += GRABPIXEL(0.05, -4.0);
    129.                     sum += GRABPIXEL(0.09, -3.0);
    130.                     sum += GRABPIXEL(0.12, -2.0);
    131.                     sum += GRABPIXEL(0.15, -1.0);
    132.                     sum += GRABPIXEL(0.18,  0.0);
    133.                     sum += GRABPIXEL(0.15, +1.0);
    134.                     sum += GRABPIXEL(0.12, +2.0);
    135.                     sum += GRABPIXEL(0.09, +3.0);
    136.                     sum += GRABPIXEL(0.05, +4.0);
    137.              
    138.                     return sum;
    139.                 }
    140.                 ENDCG
    141.             }
    142.      
    143.             // Distortion
    144.             GrabPass {                    
    145.                 Tags { "LightMode" = "Always" }
    146.             }
    147.             Pass {
    148.                 Tags { "LightMode" = "Always" }
    149.          
    150.                 CGPROGRAM
    151.                 #pragma vertex vert
    152.                 #pragma fragment frag
    153.                 #pragma fragmentoption ARB_precision_hint_fastest
    154.                 #include "UnityCG.cginc"
    155.          
    156.                 struct appdata_t {
    157.                     float4 vertex : POSITION;
    158.                     float2 texcoord: TEXCOORD0;
    159.                 };
    160.          
    161.                 struct v2f {
    162.                     float4 vertex : POSITION;
    163.                     float4 uvgrab : TEXCOORD0;
    164.                     float2 uvbump : TEXCOORD1;
    165.                     float2 uvmain : TEXCOORD2;
    166.                 };
    167.          
    168.                 float _BumpAmt;
    169.                 float4 _BumpMap_ST;
    170.                 float4 _MainTex_ST;
    171.          
    172.                 v2f vert (appdata_t v) {
    173.                     v2f o;
    174.                     o.vertex = UnityObjectToClipPos(v.vertex);
    175.                     #if UNITY_UV_STARTS_AT_TOP
    176.                     float scale = -1.0;
    177.                     #else
    178.                     float scale = 1.0;
    179.                     #endif
    180.                     o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
    181.                     o.uvgrab.zw = o.vertex.zw;
    182.                     o.uvbump = TRANSFORM_TEX( v.texcoord, _BumpMap );
    183.                     o.uvmain = TRANSFORM_TEX( v.texcoord, _MainTex );
    184.                     return o;
    185.                 }
    186.          
    187.                 fixed4 _Color;
    188.                 sampler2D _GrabTexture;
    189.                 float4 _GrabTexture_TexelSize;
    190.                 sampler2D _BumpMap;
    191.                 sampler2D _MainTex;
    192.          
    193.                 half4 frag( v2f i ) : COLOR {
    194.                     // calculate perturbed coordinates
    195.                     half2 bump = UnpackNormal(tex2D( _BumpMap, i.uvbump )).rg; // we could optimize this by just reading the x  y without reconstructing the Z
    196.                     float2 offset = bump * _BumpAmt * _GrabTexture_TexelSize.xy;
    197.                     i.uvgrab.xy = offset * i.uvgrab.z + i.uvgrab.xy;
    198.              
    199.                     half4 col = tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(i.uvgrab));
    200.                     half4 tint = tex2D( _MainTex, i.uvmain ) * _Color;
    201.              
    202.                     return col * tint;
    203.                 }
    204.                 ENDCG
    205.             }
    206.         }
    207.     }
    208. }
     
    Last edited: Sep 13, 2017
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
    Can you share a complete sample project where this reproduces? The scripts are missing some parts.
     
  3. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    I recommend to edit your post and add code-tags, as this makes it easier for people to read.
     
  4. BorisMulev

    BorisMulev

    Joined:
    May 23, 2017
    Posts:
    15
    it turned out that problem with DarkenAndBlur shader
    Code (CSharp):
    1. Shader "Preloaded/DarkenAndBlur" {
    2. // originally from here http://forum.unity3d.com/threads/185327-Simple-Optimized-Blur-Shader, but modified to remove bump map and main texture
    3. // added darken
    4.  
    5.     Properties {
    6.         _Size ("BlurPower", Range(0, 20)) = 1
    7.         _Darken ("Darken Amount", Float) = 0.5;
    8.         _Alpha ("Alpha", Float) = 1.0
    9.     }
    10.     Category {
    11.         // We must be transparent, so other objects are drawn before this one.
    12.         Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
    13.  
    14.         SubShader {
    15.  
    16.         Blend SrcAlpha OneMinusSrcAlpha
    17.        
    18.             // Horizontal blur
    19.             GrabPass {                    
    20.                 Tags { "LightMode" = "Always" } //Grabpass is different from a rendertexture in that it doesn't require to re-render the scene. It blits the contents of the framebuffer to a temporary texture, which isn't free but still much cheaper than rendering the scene multiple times.
    21.             }
    22.  
    23.             Pass {
    24.                 Tags { "LightMode" = "Always" }
    25.                 CGPROGRAM
    26.  
    27.                 #pragma vertex vert
    28.                 #pragma fragment frag
    29.                 #pragma fragmentoption ARB_precision_hint_fastest
    30.                 #include "UnityCG.cginc"
    31.                 struct appdata_t {
    32.                     float4 vertex : POSITION;
    33.                     float2 texcoord: TEXCOORD0;
    34.                 };
    35.  
    36.                 struct v2f {
    37.                     float4 vertex : POSITION;
    38.                     float4 uvgrab : TEXCOORD0;
    39.                 };
    40.                
    41.                 v2f vert (appdata_t v) {
    42.                     v2f o;
    43.                     o.vertex = UnityObjectToClipPos(v.vertex);
    44.                     #if UNITY_UV_STARTS_AT_TOP
    45.                     float scale = -1.0;
    46.                     #else
    47.                     float scale = 1.0;
    48.                     #endif
    49.                     o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
    50.                     o.uvgrab.zw = o.vertex.zw;
    51.                     return o;
    52.                 }
    53.  
    54.                 sampler2D _GrabTexture;
    55.                 float4 _GrabTexture_TexelSize;
    56.                 float _Size;
    57.                 float _Darken;
    58.                 float _Alpha;
    59.                
    60.                 half4 frag( v2f i ) : COLOR {
    61. //                  half4 col = tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(i.uvgrab));  //unmodified pixel
    62. //                  return col; //returns unmodified pixel
    63.                     half4 sum = half4(0,0,0,0);
    64.                     //this macro is the only line of code that is different from the other pass. Here the y coord remains the same and the x varies
    65.                     #define GRABPIXEL(weight,kernelx) tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x + _GrabTexture_TexelSize.x * kernelx*_Size, i.uvgrab.y, i.uvgrab.z, i.uvgrab.w))) * weight
    66.                     sum += GRABPIXEL(0.05, -4.0);
    67.                     sum += GRABPIXEL(0.09, -3.0);
    68.                     sum += GRABPIXEL(0.12, -2.0);
    69.                     sum += GRABPIXEL(0.15, -1.0);
    70.                     sum += GRABPIXEL(0.18,  0.0);
    71.                     sum += GRABPIXEL(0.15, +1.0);
    72.                     sum += GRABPIXEL(0.12, +2.0);
    73.                     sum += GRABPIXEL(0.09, +3.0);
    74.                     sum += GRABPIXEL(0.05, +4.0);
    75.                    
    76.                     sum *= _Darken;
    77.                     sum.a = _Alpha;
    78.                     return sum;
    79.                 }
    80.                 ENDCG
    81.             }
    82.  
    83.            
    84.             // Vertical blur
    85.             GrabPass {                        
    86.                 Tags { "LightMode" = "Always" }
    87.             }
    88.  
    89.             Pass {
    90.                 Tags { "LightMode" = "Always" }
    91.  
    92.                 CGPROGRAM
    93.                 #pragma vertex vert
    94.                 #pragma fragment frag
    95.                 #pragma fragmentoption ARB_precision_hint_fastest
    96.                 #include "UnityCG.cginc"
    97.  
    98.                 struct appdata_t {
    99.                     float4 vertex : POSITION;
    100.                     float2 texcoord: TEXCOORD0;
    101.                 };
    102.  
    103.                 struct v2f {
    104.                     float4 vertex : POSITION;
    105.                     float4 uvgrab : TEXCOORD0;
    106.                 };
    107.  
    108.                 v2f vert (appdata_t v) {
    109.                     v2f o;
    110.                     o.vertex = UnityObjectToClipPos(v.vertex);
    111.                     #if UNITY_UV_STARTS_AT_TOP
    112.                     float scale = -1.0;
    113.                     #else
    114.                     float scale = 1.0;
    115.                     #endif
    116.                     o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
    117.                     o.uvgrab.zw = o.vertex.zw;
    118.                     return o;
    119.                 }
    120.  
    121.                 sampler2D _GrabTexture;
    122.                 float4 _GrabTexture_TexelSize;
    123.                 float _Size;
    124.                 float _Darken;
    125.                 float _Alpha;
    126.                 half4 frag( v2f i ) : COLOR {
    127. //                  half4 col = tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(i.uvgrab));
    128. //                  return col;
    129.                     half4 sum = half4(0,0,0,0);
    130.                     //this macro is the only line of code that is different from the other pass. Here the x coord remains the same and the y varies
    131.                     #define GRABPIXEL(weight,kernely) tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x, i.uvgrab.y + _GrabTexture_TexelSize.y * kernely*_Size, i.uvgrab.z, i.uvgrab.w))) * weight
    132.                     //G(X) = (1/(sqrt(2*PI*deviation*deviation))) * exp(-(x*x / (2*deviation*deviation)))
    133.                     sum += GRABPIXEL(0.05, -4.0);
    134.                     sum += GRABPIXEL(0.09, -3.0);
    135.                     sum += GRABPIXEL(0.12, -2.0);
    136.                     sum += GRABPIXEL(0.15, -1.0);
    137.                     sum += GRABPIXEL(0.18,  0.0);
    138.                     sum += GRABPIXEL(0.15, +1.0);
    139.                     sum += GRABPIXEL(0.12, +2.0);
    140.                     sum += GRABPIXEL(0.09, +3.0);
    141.                     sum += GRABPIXEL(0.05, +4.0);
    142.                    
    143.                     sum *= _Darken;
    144.                     sum.a = _Alpha;
    145.                     return sum;
    146.                 }
    147.                 ENDCG
    148.             }
    149.         }
    150.     }
    151. }
     

    Attached Files:

  5. BorisMulev

    BorisMulev

    Joined:
    May 23, 2017
    Posts:
    15
    it became clear that the error in the function.
    Code (CSharp):
    1.  half4 frag( v2f i ) : COLOR {
    2. //                  half4 col = tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(i.uvgrab));  //unmodified pixel
    3. //                  return col; //returns unmodified pixel
    4.                     half4 sum = half4(0,0,0,0);
    5.                     //this macro is the only line of code that is different from the other pass. Here the y coord remains the same and the x varies
    6.                     #define GRABPIXEL(weight,kernelx) tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x + _GrabTexture_TexelSize.x * kernelx*_Size, i.uvgrab.y, i.uvgrab.z, i.uvgrab.w))) * weight
    7.                     sum += GRABPIXEL(0.05, -4.0);
    8.                     sum += GRABPIXEL(0.09, -3.0);
    9.                     sum += GRABPIXEL(0.12, -2.0);
    10.                     sum += GRABPIXEL(0.15, -1.0);
    11.                     sum += GRABPIXEL(0.18,  0.0);
    12.                     sum += GRABPIXEL(0.15, +1.0);
    13.                     sum += GRABPIXEL(0.12, +2.0);
    14.                     sum += GRABPIXEL(0.09, +3.0);
    15.                     sum += GRABPIXEL(0.05, +4.0);
    16.                
    17.                     sum *= _Darken;
    18.                     sum.a = _Alpha;
    19.                     return sum;
    20.                 }
    If it is return from it
    Code (CSharp):
    1.              
    2. half4 col = tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(i.uvgrab));  //unmodified pixel
    3.  return col; //returns unmodified pixel
    , the shader works fine.

    is it possible to see how the shader looks like without the formation of the UWP project (in Unit it always shows me normally, although there are problems in uwp)

    it is not entirely clear how to look for what gives zero
     
    Last edited: Sep 14, 2017
  6. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
  7. BorisMulev

    BorisMulev

    Joined:
    May 23, 2017
    Posts:
    15
    tried to debug in uwp but received an unhandled win32 exception.
    there were assumptions, wrong take points: (change sign from minus to plus)
    Code (CSharp):
    1. GRABPIXEL(weight,kernelx) tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x - _GrabTexture_TexelSize.x * kernelx*_Size, i.uvgrab.y, i.uvgrab.z, i.uvgrab.w))) * weight
    or

    Code (CSharp):
    1.  sum += GRABPIXEL(0.05, -4.0);
    2.                     sum += GRABPIXEL(-0.09, -3.0);
    3.                     sum += GRABPIXEL(-0.12, -2.0);
    4.                     sum += GRABPIXEL(-0.15, -1.0);
    5.                     sum += GRABPIXEL(-0.18,  0.0);
    6.                     sum += GRABPIXEL(-0.15, +1.0);
    7.                     sum += GRABPIXEL(-0.12, +2.0);
    8.                     sum += GRABPIXEL(-0.09, +3.0);
    9.                     sum += GRABPIXEL(-0.05, +4.0);
    but it did not give a positive result:(

    I not have ideas over why it can be zero. I need help
     
  8. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
    Can you attach a sample project that I could build & run that demonstrates the issue? The scripts in your original post aren't complete.
     
  9. BorisMulev

    BorisMulev

    Joined:
    May 23, 2017
    Posts:
    15
    Debug UWP shader Exeption

    Code (CSharp):
    1. D3D11 ERROR: ID3D11DeviceContext::CopySubresourceRegion: Cannot invoke CopySubresourceRegion when the Formats of each Resource are not the same or at least castable to each other, unless one format is compressed (DXGI_FORMAT_R9G9B9E5_SHAREDEXP, or DXGI_FORMAT_BC[1,2,3,4,5]_* ) and the source format is similar to the dest according to: BC[1|4] ~= R16G16B16A16|R32G32, BC[2|3|5] ~= R32G32B32A32, R9G9B9E5_SHAREDEXP ~= R32. [ RESOURCE_MANIPULATION ERROR #281: COPYSUBRESOURCEREGION_INVALIDSOURCE]
    2. D3D11: **BREAK** enabled for the previous message, which was: [ ERROR RESOURCE_MANIPULATION #281: COPYSUBRESOURCEREGION_INVALIDSOURCE ]
    3. Exception thrown at 0x00007FFFF1EC3C58 (KernelBase.dll) in Crafty Cut.exe: 0x0000087A (parameters: 0x0000000000000001, 0x0000009E173F5CF8, 0x0000009E173F7BF0).
    4. Unhandled exception at 0x00007FFFF1EC3C58 (KernelBase.dll) in Crafty Cut.exe: 0x0000087A (parameters: 0x0000000000000001, 0x0000009E173F5CF8, 0x0000009E173F7BF0).
     
  10. BorisMulev

    BorisMulev

    Joined:
    May 23, 2017
    Posts:
    15
    I can not attach the example of the project unfortunately.
    problem in shader, the shader code is executed good in uwp (look above).

    if you execute the following code. Unmodified pixel image.(in UWP good result without blur effect)
    Code (CSharp):
    1. half4 frag( v2f i ) : COLOR {
    2.                half4 col = tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(i.uvgrab));  //unmodified pixel
    3.                   return col; //returns unmodified pixel
    4. }

    if you execute the following code. Black screen.(in UWP bad result but Unity Editor good result wit blur effect.)
    Code (CSharp):
    1. half4 frag( v2f i ) : COLOR {
    2.                     half4 sum = half4(0,0,0,0);
    3.                     //this macro is the only line of code that is different from the other pass. Here the y coord remains the same and the x varies
    4.                     #define GRABPIXEL(weight,kernelx) tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x + _GrabTexture_TexelSize.x * kernelx*_Size, i.uvgrab.y, i.uvgrab.z, i.uvgrab.w))) * weight
    5.                     sum += GRABPIXEL(0.05, -4.0);
    6.                     sum += GRABPIXEL(0.09, -3.0);
    7.                     sum += GRABPIXEL(0.12, -2.0);
    8.                     sum += GRABPIXEL(0.15, -1.0);
    9.                     sum += GRABPIXEL(0.18,  0.0);
    10.                     sum += GRABPIXEL(0.15, +1.0);
    11.                     sum += GRABPIXEL(0.12, +2.0);
    12.                     sum += GRABPIXEL(0.09, +3.0);
    13.                     sum += GRABPIXEL(0.05, +4.0);
    14.            
    15.                     sum *= _Darken;
    16.                     sum.a = _Alpha;
    17.                     return sum;
    18.                 }

    From this all we can assert that the texture to the input is correct (not black), the shader does not crash in uwp executes normally, but the function returns zeros of black color.


    Init texture :
    Code (CSharp):
    1.   void Awake()
    2.     {
    3.         darkenLayer = transform.Find("DarkenLayer").gameObject;
    4.         darkenLayer.SetActive(false);
    5.  
    6.         gameGrab = transform.Find("GameGrab").gameObject;
    7.         gameGrab.SetActive(false);
    8.  
    9.         shapeGrab = transform.Find("ShapeGrab").gameObject;
    10.         shapeGrab.SetActive(false);
    11.  
    12.         darkenGUIGrab = transform.Find("DarkenGUIGrab").gameObject;
    13.         darkenGUIGrab.SetActive(false);
    14.  
    15.         solidWhite = new Color(1, 1, 1, 1);
    16.         invisibleWhite = new Color(1, 1, 1, 0);
    17.         gameGrabCamera.enabled = false;
    18.         shapeGrabCamera.enabled = false;
    19.         renderGUICamera.enabled = false;
    20.  
    21.         gameTexture = new Texture2D(gameGrabCamera.targetTexture.width, gameGrabCamera.targetTexture.height, TextureFormat.ARGB32, false);
    22.         gameGrab.GetComponent<Renderer>().material.mainTexture = gameTexture;
    23.  
    24.         shapeTexture = new Texture2D(shapeGrabCamera.targetTexture.width, shapeGrabCamera.targetTexture.height, TextureFormat.ARGB32, false);
    25.         shapeGrab.GetComponent<Renderer>().material.mainTexture = shapeTexture;
    26.  
    27.         blurGUITexture = new Texture2D(renderGUICamera.targetTexture.width, renderGUICamera.targetTexture.height, TextureFormat.ARGB32, false);
    28.         darkenGUIGrab.GetComponent<Renderer>().material.mainTexture = blurGUITexture;
    29.     }
    30.  
    31.  protected override void onShowStart (object data)
    32.     {
    33.  
    34.         if ((string)data == ViewMainMenu.LOCATION)
    35.         {
    36.  
    37.             StartCoroutine(FadeIn(0.5f, shapeGrabCamera, shapeGrab, shapeTexture));
    38.         }
    39.         else
    40.         {
    41.             StartCoroutine(FadeIn(0.5f, gameGrabCamera, gameGrab, gameTexture));
    42.         }
    43.  
    44.     }
    45.  
    46.     protected override void onHideStart ()
    47.     {
    48.         FadeOut(0.5f);
    49.     }
    50.  
    51.     public IEnumerator FadeIn(float duration, Camera grabCamera, GameObject grabObject, Texture2D grabTexture)
    52.     {
    53.  
    54.         // turn on camera to take (non blurred) grab of the game o rmain menu shape
    55.         grabCamera.enabled = true;
    56.  
    57.         grabCamera.targetTexture.DiscardContents();
    58.  
    59.         // wait for it to render
    60.         yield return new WaitForEndOfFrame();
    61.  
    62.         // grab the pixels from the render texture & put on a quad that the GUI View layer can see
    63.         GetRTPixels(grabCamera.targetTexture, grabTexture);
    64.  
    65.         // File.WriteAllBytes("game.png", grabTexture.EncodeToPNG()); // write to file for debugging
    66.  
    67.         // turn off 3d grabbing camera now we have the texture
    68.         grabCamera.enabled = false;
    69.  
    70.         // show the screen grab
    71.         grabObject.SetActive(true);
    72.  
    73.         // turn on the blur / darken
    74.         darkenLayer.SetActive(true);
    75.  
    76.         // turn on camera to render combined, blurred image
    77.         renderGUICamera.enabled = true;
    78.  
    79.         // wait for it to render
    80.         yield return new WaitForEndOfFrame();
    81.  
    82.         // get pixel and show that
    83.         GetRTPixels(renderGUICamera.targetTexture, blurGUITexture);
    84.         darkenGUIGrab.SetActive(true);
    85.  
    86.         // turn off gui blur camera
    87.         renderGUICamera.enabled = false;
    88.  
    89.         // turn off working layers
    90.         darkenLayer.SetActive(false);
    91.         grabObject.SetActive(false);
    92.  
    93.         // fade the combined, blurred image in
    94.         darkenGUIGrab.GetComponent<Renderer>().material.color = invisibleWhite;
    95.  
    96. #if USE_DOTWEEN
    97.  
    98.         var gui = darkenGUIGrab.GetComponent<Renderer>();
    99.         DOTween.To(() => gui.material.color, x => gui.material.color = x, solidWhite, duration).OnComplete(onShowComplete).SetUpdate(true);
    100. #else
    101.  
    102.         TweenParms tweenParams = new TweenParms();
    103.         tweenParams.Prop("color", solidWhite);
    104.         tweenParams.UpdateType(UpdateType.TimeScaleIndependentUpdate);
    105.         tweenParams.OnComplete(onShowComplete);
    106.         HOTween.To(darkenGUIGrab.GetComponent<Renderer>().material, duration, tweenParams);
    107. #endif
    108.     }
    109.  
    110.   private void GetRTPixels(RenderTexture rt, Texture2D tex)
    111.     {
    112.         currentActiveRT = RenderTexture.active;
    113.         RenderTexture.active = rt;
    114.         tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);
    115.         tex.Apply();
    116.         RenderTexture.active = currentActiveRT;
    117.  
    118.  
    119.         // Graphics.Blit(tex, rt );
    120.     }
    121.  
    Shader attached.
     

    Attached Files:

    Last edited: Sep 18, 2017
  11. BorisMulev

    BorisMulev

    Joined:
    May 23, 2017
    Posts:
    15
    and error:
    Code (CSharp):
    1. D3D11 ERROR: ID3D11DeviceContext::CopySubresourceRegion: Cannot invoke CopySubresourceRegion when the Formats of each Resource are not the same or at least castable to each other, unless one format is compressed (DXGI_FORMAT_R9G9B9E5_SHAREDEXP, or DXGI_FORMAT_BC[1,2,3,4,5]_* ) and the source format is similar to the dest according to: BC[1|4] ~= R16G16B16A16|R32G32, BC[2|3|5] ~= R32G32B32A32, R9G9B9E5_SHAREDEXP ~= R32. [ RESOURCE_MANIPULATION ERROR #281: COPYSUBRESOURCEREGION_INVALIDSOURCE]
    2. D3D11 ERROR: ID3D11DeviceContext::CopySubresourceRegion: Cannot invoke CopySubresourceRegion when the Formats of each Resource are not the same or at least castable to each other, unless one format is compressed (DXGI_FORMAT_R9G9B9E5_SHAREDEXP, or DXGI_FORMAT_BC[1,2,3,4,5]_* ) and the source format is similar to the dest according to: BC[1|4] ~= R16G16B16A16|R32G32, BC[2|3|5] ~= R32G32B32A32, R9G9B9E5_SHAREDEXP ~= R32. [ RESOURCE_MANIPULATION ERROR #281: COPYSUBRESOURCEREGION_INVALIDSOURCE]
    3. Exception thrown at 0x00007FFFF1EC3C58 in MyApp.exe: Microsoft C++ exception: _com_error at memory location 0x000000B7F44F9090.
    4. Exception thrown at 0x00007FFFF1EC3C58 in MyApp.exe: Microsoft C++ exception: _com_error at memory location 0x000000B7F44F9090.
    5. D3D11 ERROR: ID3D11DeviceContext::CopySubresourceRegion: Cannot invoke CopySubresourceRegion when the Formats of each Resource are not the same or at least castable to each other, unless one format is compressed (DXGI_FORMAT_R9G9B9E5_SHAREDEXP, or DXGI_FORMAT_BC[1,2,3,4,5]_* ) and the source format is similar to the dest according to: BC[1|4] ~= R16G16B16A16|R32G32, BC[2|3|5] ~= R32G32B32A32, R9G9B9E5_SHAREDEXP ~= R32. [ RESOURCE_MANIPULATION ERROR #281: COPYSUBRESOURCEREGION_INVALIDSOURCE]
    6. D3D11 ERROR: ID3D11DeviceContext::CopySubresourceRegion: Cannot invoke CopySubresourceRegion when the Formats of each Resource are not the same or at least castable to each other, unless one format is compressed (DXGI_FORMAT_R9G9B9E5_SHAREDEXP, or DXGI_FORMAT_BC[1,2,3,4,5]_* ) and the source format is similar to the dest according to: BC[1|4] ~= R16G16B16A16|R32G32, BC[2|3|5] ~= R32G32B32A32, R9G9B9E5_SHAREDEXP ~= R32. [ RESOURCE_MANIPULATION ERROR #281: COPYSUBRESOURCEREGION_INVALIDSOURCE]
     
  12. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
    Does this happen during "Apply" call? Perhaps the texture you're trying to copy stuff into is wrong format?
     
  13. BorisMulev

    BorisMulev

    Joined:
    May 23, 2017
    Posts:
    15
    the texture format is the same ARGB32. This error occurs without calling GetRTPixels.

    I revised all the textures in the unit editor (gameGrab, shapeGrab, renderGUI ..) format ARGB32.
     
  14. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
    Did you try RenderTextureFormat.Default instead?
     
  15. BorisMulev

    BorisMulev

    Joined:
    May 23, 2017
    Posts:
    15
    If save to file texture (call SaveToFile) in Unity Editor (image blur effect).
    If save to file texture (call SaveToFile) in UWP app (image black).

    Code (CSharp):
    1.  public IEnumerator FadeIn(float duration, Camera grabCamera, GameObject grabObject, Texture2D grabTexture)
    2.     {
    3.  
    4.         // turn on camera to take (non blurred) grab of the game o rmain menu shape
    5.         grabCamera.enabled = true;
    6.  
    7.         grabCamera.targetTexture.DiscardContents();
    8.  
    9.         // wait for it to render
    10.         yield return new WaitForEndOfFrame();
    11.  
    12.         // grab the pixels from the render texture & put on a quad that the GUI View layer can see
    13.         GetRTPixels(grabCamera.targetTexture, grabTexture);
    14.  
    15.         // File.WriteAllBytes("game.png", grabTexture.EncodeToPNG()); // write to file for debugging
    16.  
    17.         // turn off 3d grabbing camera now we have the texture
    18.         grabCamera.enabled = false;
    19.  
    20.         // show the screen grab
    21.         grabObject.SetActive(true);
    22.  
    23.         // turn on the blur / darken
    24.         darkenLayer.SetActive(true);
    25.  
    26.         // turn on camera to render combined, blurred image
    27.         renderGUICamera.enabled = true;
    28.  
    29.         // wait for it to render
    30.         yield return new WaitForEndOfFrame();
    31.  
    32.   SaveToFile(renderGUICamera.targetTexture, "renderGUICamera.png");
    33.      
    34.         GetRTPixels(renderGUICamera.targetTexture, blurGUITexture);
    Code (CSharp):
    1.  private void SaveToFile(RenderTexture renderTexture, string name)
    2.     {
    3.         RenderTexture currentActiveRT = RenderTexture.active;
    4.         RenderTexture.active = renderTexture;
    5.         Texture2D tex = new Texture2D(renderTexture.width, renderTexture.height);
    6.         tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);
    7.         var bytes = tex.EncodeToPNG();
    8.  
    9.         string path = Application.persistentDataPath + "/" + name;
    10.         using (Stream file_stream = File.Open(path, FileMode.Create))
    11.         {
    12.             file_stream.Write(bytes, 0, bytes.Length);
    13.         }
    14.         UnityEngine.Object.Destroy(tex);
    15.         RenderTexture.active = currentActiveRT;
    16.     }