Code Snippet: Edge Detector/Antialiasing Shader

Some days ago i was looking for a simple and effective edge detector idea to use in an AntiAliasing shader for Deferred Shading (ideas that effectively make use of the Normal and the Depth Buffers).

Two called my attention: The first one is from the game Stalker, this page from GPU Gems explains the whole idea (nothing very complitaded =P, they show lot of the code). The results are pretty cool, as you can see in this image for example. The second is the one used in Tabula Rasa game, this page from GPU Gems shows the whole idea.

Stalker Idea

The shader code in HLSL is: (added the full version. for those interested only in the edge detection part, see the shader comments).

void PassThroughVS(inout float4 position : POSITION0,
                   inout float2 texcoord : TEXCOORD0) {

				   position.x =  position.x - pixel_size;
				   position.y =  position.y + pixel_size;

float2 e_barrier = float2(0.8f,0.00001f);  // x=norm, y=depth

float2 e_weights= float2(1,1);  // x=norm, y=depth

float2 e_kernel = float2(1,1);   // x=norm, y=depth

const static float2 offs[7] = {
   float2( 0.0,  0.0), //Center       0
   float2(-1.0, -1.0), //Top Left     1
   float2( 1.0,  1.0), //Bottom Right 5
   float2( 1.0, -1.0), //Top Right    3
   float2(-1.0,  1.0), //Bottom Left  7
   float2(-1.0,  0.0),  //Left         8
   float2( 0.0, -1.0) //Top          2

float4 PShader2(float2 uv : TEXCOORD0) : COLOR0


 // Normal discontinuity filter

 float3 nc = tex2D(NormalMap, uv);

 float4 nd;

 nd.x = abs(dot(nc, tex2D(NormalMap, uv + offs[1]* pixel_size).xyz));

 nd.y = abs(dot(nc, tex2D(NormalMap, uv + offs[2]* pixel_size).xyz));

 nd.z = abs(dot(nc, tex2D(NormalMap, uv + offs[3]* pixel_size).xyz));

 nd.w = abs(dot(nc, tex2D(NormalMap, uv + offs[4]* pixel_size).xyz));

 nd -= e_barrier.x;

 nd = step(0, nd);

 float ne = saturate(dot(nd, e_weights.x));

 // Opposite coords

 float2 tc5r = -offs[5];

 float2 tc6r = -offs[6];

 // Depth filter : compute gradiental difference:

 // (c-sample1)+(c-sample1_opposite)

 float dc = tex2D(depthMap, uv).r;

 float4 dd;

 dd.x = tex2D(depthMap, uv + offs[1] * pixel_size).r +

         tex2D(depthMap, uv + offs[2]* pixel_size).r;

 dd.y = tex2D(depthMap, uv + offs[3]* pixel_size).r +

        tex2D(depthMap, uv + offs[4]* pixel_size).r;

 dd.z = tex2D(depthMap, uv + offs[5]* pixel_size).r +

        tex2D(depthMap, uv + tc5r* pixel_size).r;

 dd.w = tex2D(depthMap,uv +  offs[6]* pixel_size).r +

        tex2D(depthMap,uv +  tc6r* pixel_size).r;

 dd = abs(2 * dc - dd)- e_barrier.y;

 dd = step(dd, 0);

 float de = saturate(dot(dd, e_weights.y));

 // Weight

 float w = (1 - de * ne) * e_kernel.x; // 0 - no aa, 1=full aa

 //return float4(w,w,w,1); ///stop here for an edge detector shader

// The AA part (in edges we mix close pixels, non edge pixel w = 0 => use the normal uv, on edge we use a mean of four deslocated uv texture reads )
// Smoothed color

 // (a-c)*w + c = a*w + c(1-w)

 float2 offset = (uv ) * (1-w);

 float4 s0 = tex2D(TextureSampler, offset + (uv + offs[1] * pixel_size) * w);

 float4 s1 = tex2D(TextureSampler, offset + (uv + offs[2]* pixel_size) * w);

 float4 s2 = tex2D(TextureSampler, offset + (uv + offs[3]* pixel_size) * w);

 float4 s3 = tex2D(TextureSampler, offset + (uv + offs[4]* pixel_size) * w);

 return (s0 + s1 + s2 + s3)/4.h;


technique AntiAliasingStalker
    pass Pass1
		VertexShader = compile vs_3_0 PassThroughVS();
                PixelShader = compile ps_3_0 PShader2();

The normal buffer is stored as a 32 bits RGBA texture. The Depth Buffer is stored in a 32 bits Single Texture (storing post-projection z/w). If you use another depth enconding, just adjust the barrier variable value.

The pixel_size variable (a float2 with 1/target width in x and 1/target height in y) is used in DirectX 9 Shaders (Opengl and DirectX > 9 does not need this). The need for this “correction” is very well explained here.

The ScreenShot below is just the edge detector part of the shader. (just uncomment the following line in the shader and you will have a powerfull edge detector shader =P)

Example using this good edge detector shader. Cool for toon shading =P


Idea From Tabula Rasa

This is not so good as the one before (dont looks good when the camera is far from the objects or when the edges has little pixels), but is a bit simpler. Fort those interested  only in the edge detection part,

just return the “factor” variable in the pixel shader, instead of using it to blend the near pixels =P

const float2 delta[8] =

 ///silly, dummy functions to recover normal/depth (i was doing something with the read values before and i did not erased the function ...)
 float4 DL_GetDepth(float2 uv)
	return tex2D(NormalMap  ,uv);
 float4 DL_GetNormal(float2 uv)
	return tex2D(depthMap ,uv);

float depthSensibility;
float normalSensibility;
   // Neighbor offset table
   const static float2 offsets[9] = {
  float2( 0.0,  0.0), //Center       0
   float2(-1.0, -1.0), //Top Left     1
   float2( 0.0, -1.0), //Top          2
   float2( 1.0, -1.0), //Top Right    3
   float2( 1.0,  0.0), //Right        4
   float2( 1.0,  1.0), //Bottom Right 5
   float2( 0.0,  1.0), //Bottom       6
   float2(-1.0,  1.0), //Bottom Left  7
   float2(-1.0,  0.0)  //Left         8
float DL_GetEdgeWeight(in float2 screenPos)
  float Depth[9];
  float3 Normal[9];
  //Retrieve normal and depth data for all neighbors.
   for (int i=0; i<9; ++i)
    float2 uv = screenPos + offsets[i] * pixel_size;
    Depth[i] = DL_GetDepth(uv);  //Retrieves depth from MRTs
    Normal[i]= DL_GetNormal(uv); //Retrieves normal from MRTs
  //Compute Deltas in Depth.
   float4 Deltas1;
  float4 Deltas2;
  Deltas1.x = Depth[1];
  Deltas1.y = Depth[2];
  Deltas1.z = Depth[3];
  Deltas1.w = Depth[4];
  Deltas2.x = Depth[5];
  Deltas2.y = Depth[6];
  Deltas2.z = Depth[7];
  Deltas2.w = Depth[8];
  //Compute absolute gradients from center.
  Deltas1 = abs(Deltas1 - Depth[0]);
  Deltas2 = abs(Depth[0] - Deltas2);
  //Find min and max gradient, ensuring min != 0
   float4 maxDeltas = max(Deltas1, Deltas2);
  float4 minDeltas = max(min(Deltas1, Deltas2), 0.00001);
  // Compare change in gradients, flagging ones that change
   // significantly.
   // How severe the change must be to get flagged is a function of the
   // minimum gradient. It is not resolution dependent. The constant
   // number here would change based on how the depth values are stored
   // and how sensitive the edge detection should be.
   float4 depthResults = step(minDeltas * depthSensibility, maxDeltas);
  //Compute change in the cosine of the angle between normals.
  Deltas1.x = dot(Normal[1], Normal[0]);
  Deltas1.y = dot(Normal[2], Normal[0]);
  Deltas1.z = dot(Normal[3], Normal[0]);
  Deltas1.w = dot(Normal[4], Normal[0]);
  Deltas2.x = dot(Normal[5], Normal[0]);
  Deltas2.y = dot(Normal[6], Normal[0]);
  Deltas2.z = dot(Normal[7], Normal[0]);
  Deltas2.w = dot(Normal[8], Normal[0]);
  Deltas1 = abs(Deltas1 - Deltas2);
  // Compare change in the cosine of the angles, flagging changes
   // above some constant threshold. The cosine of the angle is not a
   // linear function of the angle, so to have the flagging be
   // independent of the angles involved, an arccos function would be
   // required.
  float4 normalResults = step(0.4, Deltas1* normalSensibility);  

  normalResults = max(normalResults, depthResults);
  return (normalResults.x + normalResults.y +
          normalResults.z + normalResults.w) * 0.25;

float4 PShader(float2 texCoord : TEXCOORD0) : COLOR0

 float4 tex = tex2D(NormalMap,texCoord);
 float factor = 0.0f;

 for( int i=0;i<4;i++ )
	 float4 t = tex2D(NormalMap,texCoord+ delta[i]*pixel_size);
	 t -= tex;
	 factor += dot(t,t);

 factor = min(1.0,DL_GetEdgeWeight(texCoord))*weight; 

 float4 color = float4(0.0,0.0,0.0,0.0);

 for( int j=0;j<8;j++ )
	color += tex2D(TextureSampler,texCoord + delta[j]*pixel_size*factor);
 color += 2.0*tex2D(TextureSampler,texCoord);
 return color*(1.0/10.0);


The Vertex Shader and the buffers used are the same from the Stalker idea.

The first one is more beautiful but is slower.

The PloobsEngine uses these AA shaders (and some others also) to perform an Antialiasing Pass.

This is the first code snippet, when possible i will post more =P


, , , , ,

  1. #1 by cheap black hunter boots on 23 de março de 2017 - 6:21 pm

    Considerably, this post is really the sweetest on this notable topic. I harmonise with your conclusions and will thirstily look forward to your incoming updates. Saying thanks will not just be sufficient, for the phenomenal clarity in your writing.
    cheap black hunter boots

  2. #2 by women's moncler jacket sale on 23 de março de 2017 - 6:40 pm

    Great article. Waiting for more.
    women’s moncler jacket sale

  3. #3 by aspynOn on 23 de março de 2017 - 7:21 pm

    probablement oui

  4. #4 by Theron on 23 de março de 2017 - 7:31 pm

    Ainda sim, temos quadros de celulite gerada pela pré-ordenamento genética ao acúmulo de lipócitos (células de adiposidade) nas camadas mas profundas da pele, que deve ser encarado de outra forma e tratado em outro ritmo.

  5. #5 by claudeOn on 23 de março de 2017 - 7:37 pm

    Pienso que no sois derecho. Soy seguro. Puedo demostrarlo. Escriban en PM, se comunicaremos.

  6. #6 by Yumeshi.Sakura.Ne.Jp on 23 de março de 2017 - 7:37 pm

    Ele poderá fazer com que as suas dores diminuam muito com uso de equipamentos especializados.

  7. #7 by lk bennett sale outlet on 23 de março de 2017 - 7:37 pm

    Mornin ! I had subscribed your website for interesting content, I am also going to start a blog on health product vigrx plus , you can also get some unique content for me on cheap vigrx plus. Ciao!
    lk bennett sale outlet

  8. #8 by analsluts on 23 de março de 2017 - 7:38 pm

    gym upshorts8th street latinas xxxporntubecomemma bates pornethio xxxactress sexclipschicago backpageskristal summers bdsmwwe divas fuckedhoney wilder private teacherkieara mooremiley cyrus sex tapeforced cum swallowpussy juice in mouthmaureen mccormick nude scene
    there will be no chatting, unless i query you a eye you hear heed say, you where Definite that was him.Don’t beget me foul i bear only being with studs i was never attracted or revved on by one! seeing her elegant her gams was a bit to remarkable for me as this is my nephew and i can’t enjoy these sexual thoughts about her it was rank indeed despicable.He was panicked, his mothers threat level-headed ringing in his ears.With it mostly all abet inwards you where it belongs, the last bit of slime gets patted on to your perky lips to savour, so you’re reminded of what you are every time you purse your lips in timid subjugation- a hopeless lil’ sissy jizz guzzler.If i ever invent the opportunity to be with her again i would! :).You open to match my moves with your thigh shoves as you tedious commence to face nail me.There, peruse? he said, with a original mingle of victory and pride, not for himself but for me.Sophie looked him in the eyes as he shook.He took cuffs out of his gather and shackled me to the sofa.How could I collect out of it? Then I remembered the DreamLover again.Darius stopped at the gate and talked several words to he guard, and we drove thru.Jennifer, experiencing that she’d been suggested an chance to exit theconversation gracefully, excused herself and frail into the crowd.several pornsites I luved needed paid membership.12 O’clock acute everyone revved off their computers as it was sure that everyone was getting an early day for the office soiree, most people brought a switch of clothes with them and the damsels spent most of the morning conversing about their apparels and begging none of them clashed.Their splendid clothes lay about the sofa in slight pillars totally forgotten.Joseph lowered his face and gave my nips a one last time work over.We were at his region in no time.ravage, then what are we doing here? carry out you wanna arrive to my status?She sneered.satisfy Gem AAAAAAAAAAHHHHHHHHHHHHHHHHH!!!!!That’s ten fuckwit.David, helped by his dinky size, kneeled gradual the 2, pointed the nail-stick at her wide start cunt and pushed it in..

  9. #9 by tornadoOn on 23 de março de 2017 - 8:17 pm

    Mir scheint es die bemerkenswerte Idee

  10. #10 by on 23 de março de 2017 - 8:23 pm

    Our boy offered myself it for parents evening. He knows im maybe not concerning price then again things originates from all center. I cannot believe it has my preferred colors and also really fits my personal wrist. I never can get a hold of bracelets to fit headphonesmy tiny wrist. Quite happy to have recieved this one being a gift.

  11. #11 by on 23 de março de 2017 - 8:24 pm

    My son gave myself that for moms time. He realizes im not around price however what originates from the actual heart. I cannot know it has our preferred color furthermore in fact fits my wrist. I not could uncover bracelets to fit headphonesmy mini wrist. Very happy to have actually recieved that as a gift.

  12. #12 by on 23 de março de 2017 - 8:24 pm

    Awesome headphonespurchase, well cost and also what was shown. Pretty gifts container and also poem even enclosed. Very good towards mother’s time!

  13. #13 by on 23 de março de 2017 - 9:07 pm

    Very good headphonespick, nicely cost as well as what are presented. Perfect gifts container plus poem even enclosed. Ideal to mother’s evening!

  14. #14 by Cartier Sunglasses Replica on 23 de março de 2017 - 9:08 pm

    The son offered me personally this particular towards parents day. He realizes im maybe not regarding price however what else comes from your center. I cannot think it has my personal favorite colors additionally even meets the wrist. I did not might choose bracelets to suit headphonesour tiny wrist. Quite happy to posses recieved this being a present.

  15. #15 by on 23 de março de 2017 - 9:11 pm

    My personal boy gave me personally our towards moms evening. He understands im never up to price tag and yet what else originates from on center. I cannot know it has our favourite color as well as in fact suits our wrist. I did not can find bracelets to suit headphonesthe smaller wrist. Really happy to has recieved the as being a gifts.

  16. #16 by on 23 de março de 2017 - 9:12 pm

    My personal son gave myself our for parents time. He knows im never around price tag and yet everything comes from the particular center. I cannot know this has our preferred color plus really matches my wrist. I not could come across bracelets to fit headphonesour limited wrist. Extremely pleased to get recieved that as a gifts.

  17. #17 by on 23 de março de 2017 - 9:12 pm

    My boy offered me personally your to parents time. He understands im never regarding price tag although exactly what originates from their heart. I cannot believe it has the preferred shade plus actually suits the wrist. I by no means could look for bracelets to fit headphonesmy personal slight wrist. Quite happy to get recieved your as a gifts.

  18. #18 by on 23 de março de 2017 - 9:41 pm

    My personal boy provided me personally this particular for the parents time. He understands im not up to price still just what originates from the particular center. I cannot believe it has my personal favourite colors additionally in fact matches the wrist. I do not can come across bracelets to suit headphonesmy personal small wrist. Quite happy to own recieved your as a gift.

  19. #19 by on 23 de março de 2017 - 9:42 pm

    Very good headphonesgo for, nicely listed plus what is displayed. Gorgeous gift container furthermore poem always enclosed. Very good concerning mother’s day!

  20. #20 by on 23 de março de 2017 - 9:42 pm

    The son offered me personally the concerning parents evening. He realizes im not really around price and yet exactly what comes from the center. I cannot know this has the favorite colors additionally really matches my personal wrist. I by no means can get a hold of bracelets to fit headphonesmy personal smaller wrist. Really pleased to need recieved this one as being a gift.

  21. #21 by Education on 23 de março de 2017 - 10:12 pm

    I really like your writing style, superb information, thank you for posting :D. “All words are pegs to hang ideas on.” by Henry Ward Beecher.

  22. #22 by João Davi Lucca on 23 de março de 2017 - 11:39 pm

    Ele vai ver prosseguirei conseguir. se este homem (Jan Kristof)
    for uma farsa, Porém aqui está outra questão que também
    não é pensada com freqüência – a diferença que pode fazer no dia do seu consorte
    se tudo em você expressar fato de que está muito, muito feliz em vê-lo.

  23. #23 by Travel & leisure on 23 de março de 2017 - 11:45 pm

    I have learn a few good stuff here. Certainly value bookmarking for revisiting. I surprise how so much effort you place to make the sort of wonderful informative web site.

  24. #24 by Travel & leisure on 24 de março de 2017 - 1:53 am

    You actually make it seem so easy with your presentation but I find this matter to be really something which I think I would never understand. It seems too complex and extremely broad for me. I am looking forward for your next post, I’ll try to get the hang of it!

  25. #25 by on 24 de março de 2017 - 3:29 am

    This can be actually pretty. I love it a lot. The estimate are pretty as well as box wrapped inside cool bow. It isn’t the most astonishing bracelet ever, however for the price things exceeds the objectives. Can’tdelay and give that it at the mom for Mother’s Day!

  26. #26 by toko bunga surabaya disain unik on 24 de março de 2017 - 3:30 am

    Copyright? I want to collect stories/articles from the net and put all of them onto a single page?

  27. #27 by on 24 de março de 2017 - 3:32 am

    My son gave me personally this particular concerning moms time. He knows im maybe not around price tag and yet everything comes from the particular center. I can’t feel it has my personal favourite colors then truly meets my wrist. I never ever could notice bracelets to suit headphonesmy mini wrist. Most pleased to has recieved our being a gift.

  28. #28 by Cartier Bracelets Replica on 24 de março de 2017 - 3:34 am

    Ideal headphonespick, nicely listed and what is revealed. Beautiful gift box as well as poem and enclosed. Awesome to mother’s evening!

  29. #29 by on 24 de março de 2017 - 4:13 am

    Great headphonesinvest, well priced and what was displayed. Breathtaking gift box and/or poem and enclosed. Awesome for the mother’s time!

  30. #30 by austin experts on seo on 24 de março de 2017 - 4:50 am

    The best Austin seo is here. Their Austin seo expert, consultant, experts and consultants are the best from all the Austin seo companies. Check out their SEO Austin today. Both Austin SEO and SEO Austin is available.

  31. #31 by Rolando Dorner on 24 de março de 2017 - 4:50 am

    I’m gone to say to my little brother, that he should also visit this web site on regular basis to obtain updated from most recent news update.|

1 432 433 434
(não será publicado)