Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


SMAA Implementation - Weights Pass


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 FriendlyFire   Members   -  Reputation: 128

Like
0Likes
Like

Posted 18 April 2012 - 09:00 PM

I've been working on a fairly complex project which involves rebuilding a graphics engine from DirectX 8 to DirectX 9 for the past few months. We've done great progress and added a full deferred renderer to the project. However, this obviously causes problems with antialiasing, so we've implemented FXAA 3.1 some time ago.

Lately, I've been thinking about switching over to SMAA 2.7. The results SMAA gets are just much better than FXAA's. I've integrated the code given by the team just fine, but the resulting data is wrong. SMAA's split into three pass: edge detection, blending weight calculations and finally neighbor blending. The first step seemingly works perfectly, but the second step doesn't produce the results I should get, and thus the final result is that the image isn't even antialiased.

As a side note, I'm using the precompiled DX10 application but I've taken most of the code and logic from the DX9 sample.

First of all, here's the code which sets up the postprocess:
if(HkData::iSetSMAA)
{
#ifdef DEBUG_PIPELINE
OutputDebugString("SMAA\n");
D3DPERF_BeginEvent(PIX_COLOR, L"SMAA");
#endif
  if(HkData::bBenchmark)
   timerBench.start();
  // save critical states
  IDirect3DVertexDeclaration9* oldVertexDecl;
  d3d9_realdevice->GetVertexDeclaration(&oldVertexDecl);
  IDirect3DVertexBuffer9* pVBOLD;
  uint iVBOLD_OFFSET, iVBOLD_STRIDE;
  d3d9_realdevice->GetStreamSource(0, &pVBOLD, &iVBOLD_OFFSET, &iVBOLD_STRIDE);
  d3d9_realdevice->SetVertexDeclaration( g_pVertDeclPP );
  d3d9_realdevice->SetStreamSource( 0, pVB, 0, sizeof( PPVERT ) );
  d3d9_realdevice->StretchRect(surfBackBuffer, 0, surfBackBufferTex, 0, D3DTEXF_NONE);
  // edge detection pass
  d3d9_realdevice->SetRenderTarget(0, surfSMAA_Edge);
  d3d9_realdevice->SetDepthStencilSurface(surfOldDepth);
  d3d9_realdevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
  d3d9_realdevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
  d3d9_realdevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_STENCIL, 0x00000000, 0.0f, 0 );
  g_pEffectSMAA->SetTexture("colorTex2D", texBackBuffer);
 
  g_pEffectSMAA->SetTechnique("LumaEdgeDetection");
  g_pEffectSMAA->Begin(&iPasses, 0);
  g_pEffectSMAA->BeginPass(0);
  d3d9_realdevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );
  g_pEffectSMAA->EndPass();
  g_pEffectSMAA->End();
  // blending weight pass
  d3d9_realdevice->SetRenderTarget(0, surfSMAA_Blend);
  d3d9_realdevice->Clear( 0L, NULL, D3DCLEAR_TARGET,
		   0x00000000, 1.0f, 0L );
  g_pEffectSMAA->SetTexture("edgesTex2D", texSMAA_Edge);
  g_pEffectSMAA->SetTexture("areaTex2D", texSMAA_Area);
  g_pEffectSMAA->SetTexture("searchTex2D", texSMAA_Search);
 
  g_pEffectSMAA->SetTechnique("BlendWeightCalculation");
  g_pEffectSMAA->Begin(&iPasses, 0);
  g_pEffectSMAA->BeginPass(0);
  d3d9_realdevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );
  g_pEffectSMAA->EndPass();
  g_pEffectSMAA->End();
  d3d9_realdevice->StretchRect(surfSMAA_Blend, 0, surfBackBuffer, 0, D3DTEXF_NONE);
  // final pass
  /*d3d9_realdevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
  d3d9_realdevice->SetRenderTarget(0, surfBackBuffer);
  g_pEffectSMAA->SetTexture("blendTex2D", texSMAA_Blend);
  g_pEffectSMAA->SetTexture("colorTex2D", texBackBuffer);
 
  g_pEffectSMAA->SetTechnique("NeighborhoodBlending");
  g_pEffectSMAA->Begin(&iPasses, 0);
  g_pEffectSMAA->BeginPass(0);
  SMAARenderQuad(surfdescBackBuffer.Width, surfdescBackBuffer.Height);
  g_pEffectSMAA->EndPass();
  g_pEffectSMAA->End();*/
  // restore stuff
  d3d9_realdevice->SetVertexDeclaration( oldVertexDecl );
  oldVertexDecl->Release();
  d3d9_realdevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
  d3d9_realdevice->SetStreamSource(0, pVBOLD, iVBOLD_OFFSET, iVBOLD_STRIDE);
  d3d9_realdevice->SetPixelShader(NULL);
  d3d9_realdevice->SetVertexShader(NULL);
  if(HkData::bBenchmark)
   bench_current.tAA = timerBench.stop();
#ifdef DEBUG_PIPELINE
  D3DPERF_EndEvent();
#endif
}

The shader code I'm using is taken bit-for-bit from the DX9 demo, so I won't copy the (lengthy) files here. Here's a link to the release I've been working with: https://github.com/iryoku/smaa/zipball/v2.7

I've debugged the application with maximum validation and output, but nothing in the log makes me believe the code is encountering errors. I've also done PIX runs, but I can't say I'm quite at the level necessary to understand the shader, so seeing what it does is slightly pointless. I have however taken pictures of each step (sorry for the large pictures but resizing kills all the details):

Initial image:
Posted Image

Edges:
Posted Image

Related edge stencil:
Posted Image

Weights:
Posted Image

Weights (alpha channel):
Posted Image

Demo program edges:
Posted Image

Demo program edge stencil:
Posted Image

Demo program weights:
Posted Image

Demo program weights (alpha):
Posted Image


Unless I'm missing something, it's pretty clear that my weights output is entirely different from what I'm supposed to be getting. I can't find a channel match which would indicate that the two versions use different channels for storing the data, either. I've double-checked and both surfaces I have are A8R8G8B8 like in the demo.

I'm quite stumped, but I have found very little information on how to implement SMAA outside of the main team's package. Most hits come back to the SMAA injector, which ironically works fine when wrapped around this (but it affects HUD elements and adds another layer of complexity, which is unacceptable). I can also say that I have modified the SMAA injector shader to get the weights image and it is extremely similar (if not identical) to the demo's, which makes me believe it's not a difference between DX9 and DX10 implementations, but a real problem with my code. However, considering the only inputs in the blend weights pass are the edge texture, the area texture and the search texture, all three of which appeared fine in PIX... I'm not sure what the error is.

Any help would be greatly appreciated.

Sponsor:

#2 Hodgman   Moderators   -  Reputation: 31112

Like
0Likes
Like

Posted 18 April 2012 - 09:30 PM

Are you taking D3D9's half-pixel screen-vertex offset into account?


You may need to use geometry that's been shifted up/left by 0.5 screen pixels so that your vertex attributes are interpolated correctly. If you don't do this, then the pixel-shader's texture-coordinates will not line up exactly with the centre of your texels - they'll be at the bottom/right corner of each texel, giving you blurred texture samples.



#3 FriendlyFire   Members   -  Reputation: 128

Like
0Likes
Like

Posted 19 April 2012 - 11:06 AM

Thanks for the reply!

I've done some testing. Here's the vertex buffer I'm using (with the offsets being 1/width and 1/height respectively):
struct PPVERT
{
    float x, y, z, rhw;
    float tu, tv;	   // Texcoord for post-process source
    float tu2, tv2;	 // Texcoord for the original scene

    const static D3DVERTEXELEMENT9 Decl[4];
};

PPVERT Quad[4] = {
  {-1.0f,		  1.0f,						 1.0f, 1.0f, 0.0f+fPixelOffsetX, 0.0f+fPixelOffsetY, 0.0f, 0.0f },
  { 1.0f,	   1.0f,						 1.0f, 1.0f, 1.0f+fPixelOffsetX, 0.0f+fPixelOffsetY, 1.0f, 0.0f },
  {-1.0f,					    -1.0f,	    1.0f, 1.0f, 0.0f+fPixelOffsetX, 1.0f+fPixelOffsetY, 0.0f, 1.0f },
  { 1.0f,		    -1.0f,	    1.0f, 1.0f, 1.0f+fPixelOffsetX, 1.0f+fPixelOffsetY, 1.0f, 1.0f }
};

I've also tried using the SMAA demo's buffer:
const D3DVERTEXELEMENT9 vertexElements[3] = {
	 { 0, 0,  D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
	 { 0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD,  0 },
	 D3DDECL_END()
    };
D3DXVECTOR2 pixelSize = D3DXVECTOR2(1.0f / float(width), 1.0f / float(height));
    float quad[4][5] = {
	    { -1.0f - pixelSize.x,  1.0f + pixelSize.y, 0.5f, 0.0f, 0.0f },
	    {  1.0f - pixelSize.x,  1.0f + pixelSize.y, 0.5f, 1.0f, 0.0f },
	    { -1.0f - pixelSize.x, -1.0f + pixelSize.y, 0.5f, 0.0f, 1.0f },
	    {  1.0f - pixelSize.x, -1.0f + pixelSize.y, 0.5f, 1.0f, 1.0f }
    };
which is used with DrawPrimitiveUP. The resulting weights are identical to the first buffer.

Changing the original buffer (while keeping the PPVERT vertex declaration) to this:
PPVERT Quad[4] = {
  {-1.0f-fPixelOffsetX,  1.0f-fPixelOffsetY,  0.5f,  1.0f, 0.0f,  0.0f, 0.0f,  0.0f },
  { 1.0f-fPixelOffsetX,  1.0f-fPixelOffsetY,  0.5f,  1.0f, 1.0f,  0.0f, 1.0f,  0.0f },
  {-1.0f-fPixelOffsetX, -1.0f-fPixelOffsetY,  0.5f,  1.0f, 0.0f,  1.0f, 0.0f,  1.0f },
  { 1.0f-fPixelOffsetX, -1.0f-fPixelOffsetY,  0.5f,  1.0f, 1.0f,  1.0f, 1.0f,  1.0f }
};
produces even fewer weights, so I think the initial buffer is fine.

#4 FriendlyFire   Members   -  Reputation: 128

Like
0Likes
Like

Posted 20 April 2012 - 01:04 PM

A small update: after tinkering with the loading loop, I figured out that the defines I had weren't being used. I've decided to just make the source get compiled at load time to make things easier, which lets me match the demo source even more closely.

Now, I'm getting something slightly better, like this:
Posted Image

You can see that there are some more green and red edges. When looking at the model from certain angles, I can see pronounced green edges that fade in and out, which I believe is right. However, there are still clearly edges missing and red edges in particular seem to flicker constantly so that instead of staying relatively consistent, any small movement will change their appearance entirely. I don't think that this is right.

#5 FriendlyFire   Members   -  Reputation: 128

Like
0Likes
Like

Posted 01 May 2012 - 08:59 PM

Bump. I've done a few more tests but I plain and simply can't figure that one out.

Is there anybody who's got any idea on what the hell might be wrong with this?

#6 PixelSmasher   Members   -  Reputation: 414

Like
0Likes
Like

Posted 23 July 2012 - 01:57 AM

Hi FriendlyFire, I'm currently replacing FXAA with SMAA 2.7 in our engine and I'm stuck on the same problem: the result of the second pass is very similar to yours.
Have you figured out what was wrong with your implementation ?

#7 FriendlyFire   Members   -  Reputation: 128

Like
0Likes
Like

Posted 23 July 2012 - 11:46 AM

I'm afraid I have not. I have deactivated SMAA and went back to FXAA because I just couldn't figure out the solution and didn't want to waste so long on it.

If you do find a solution, I'd be most grateful if you could post it here.

#8 PixelSmasher   Members   -  Reputation: 414

Like
0Likes
Like

Posted 31 July 2012 - 10:43 AM

A fast update during my epic and painful quest for SMAA : after a week of despair, I noticed the DX9 version of SMAA is working on a GTX560 Ti...
How cool is that ? This code is actually working !!!

Not that cool ! As I'm now into the depths of HLSL assembly, trying to find out what the compiler messed up Posted Image.

Edited by PixelSmasher, 31 July 2012 - 10:46 AM.


#9 FriendlyFire   Members   -  Reputation: 128

Like
0Likes
Like

Posted 19 August 2012 - 08:30 PM

Bumping this again. PixelSmasher, did you figure out the issue? I'm still entirely stumped.

#10 PixelSmasher   Members   -  Reputation: 414

Like
0Likes
Like

Posted 10 September 2012 - 08:47 AM

Hi FriendlyFire. I'm sorry for the late answer, I'm in total crunch time and deadlines do not forgive !
I eventually got SMAA to work on console aaaannnnnd it took 10 ms to render ! This is the time I gave up.

To have it working on PC, I simply turned the pixel shader interpolated array "offsets[3]" into 3 interpolated values "offset0", "offset1" and "offset2". The compiler did mess with them.

Edited by PixelSmasher, 10 September 2012 - 08:48 AM.


#11 FriendlyFire   Members   -  Reputation: 128

Like
0Likes
Like

Posted 10 September 2012 - 03:07 PM

Hi FriendlyFire. I'm sorry for the late answer, I'm in total crunch time and deadlines do not forgive !
I eventually got SMAA to work on console aaaannnnnd it took 10 ms to render ! This is the time I gave up.

To have it working on PC, I simply turned the pixel shader interpolated array "offsets[3]" into 3 interpolated values "offset0", "offset1" and "offset2". The compiler did mess with them.


Hey! First of all thanks for updating this.

I've tried to do as you said, but I do not see any significant change. I have tried to only replace the offset array in SMAABlendingWeightCalculationPS and to replace all offset arrays in all functions to no avail.

If you have a few moments, would you be able to detail a bit more what you've done to fix it, perhaps with some code?

Thanks once more.

#12 PixelSmasher   Members   -  Reputation: 414

Like
0Likes
Like

Posted 11 September 2012 - 04:23 AM

Here's the code I'm using to integrate the SMAA in our engine shader generator.
The #define ENABLE_SMAA_VS is set only when compiling a vertex shader. The DX9 compiler was messing things up otherwise (as well as our GLSL converter).


// -- Pass 0
void VS_SmaaPass0(
	 vs_Input _vs_In,
	 out float4 f4_Position : POSITION,
	 out float2 f2_TexCoord : TEXCOORD0,
	 out float4 f4_Offset0 : TEXCOORD1,
	 out float4 f4_Offset1 : TEXCOORD2,
	 out float4 f4_Offset2 : TEXCOORD3
)
{
	 ComputePosUV2D(f4_Position, f2_TexCoord, _vs_In.f4_Position, _vs_In.f2_TexCoord0.xy);	 // Store the vertex position & its texcoord in f4_Position and f2_TexCoord
	 #if defined(ENABLE_SMAA_VS)
	 	 float4 f4_Offsets[3];
	 	 SMAAEdgeDetectionVS(f4_Position, f4_Position, f2_TexCoord, f4_Offsets);
	 	 f4_Offset0 = f4_Offsets[0];
	 	 f4_Offset1 = f4_Offsets[1];
	 	 f4_Offset2 = f4_Offsets[2];
	 #else
	 	 f4_Offset0 = float4(0.0, 0.0, 0.0, 0.0);
	 	 f4_Offset1 = float4(0.0, 0.0, 0.0, 0.0);
	 	 f4_Offset2 = float4(0.0, 0.0, 0.0, 0.0);
	 #endif
}
float4 PS_SmaaPass0(
	 float2 f2_TexCoord : TEXCOORD0,
	 float4 f4_Offset0 : TEXCOORD1,
	 float4 f4_Offset1 : TEXCOORD2,
	 float4 f4_Offset2 : TEXCOORD3
) : COLOR0
{
	 #if defined(ENABLE_SMAA_VS)
		 return float4(0.0, 0.0, 0.0, 0.0);
	 #else
	 	 float4 f4_Offsets[3];
	 	 f4_Offsets[0] = f4_Offset0;
	 	 f4_Offsets[1] = f4_Offset1;
	 	 f4_Offsets[2] = f4_Offset2;
	 	 return SMAALumaEdgeDetectionPS(f2_TexCoord, f4_Offsets, samp_SmaaSourceTex);
	 #endif
}

// -- Pass 1
void VS_SmaaPass1(
	 vs_Input _vs_In,
	 out float4 f4_Position : POSITION,
	 out float2 f2_TexCoord : TEXCOORD0,
	 out float2 f2_PixCoord : TEXCOORD1,
	 out float4 f4_Offset0  : TEXCOORD2,
	 out float4 f4_Offset1  : TEXCOORD3,
	 out float4 f4_Offset2  : TEXCOORD4
)
{
	 ComputePosUV2D(f4_Position, f2_TexCoord, _vs_In.f4_Position, _vs_In.f2_TexCoord0.xy);  
	 #if defined(ENABLE_SMAA_VS)
	 	 float4 f4_Offsets[3];
	 	 SMAABlendingWeightCalculationVS(f4_Position, f4_Position, f2_TexCoord, f2_PixCoord, f4_Offsets);
	 	 f4_Offset0 = f4_Offsets[0];
	 	 f4_Offset1 = f4_Offsets[1];
	 	 f4_Offset2 = f4_Offsets[2];
	 #else
	 	 f4_Offset0 = float4(0.0, 0.0, 0.0, 0.0);
	 	 f4_Offset1 = float4(0.0, 0.0, 0.0, 0.0);
	 	 f4_Offset2 = float4(0.0, 0.0, 0.0, 0.0);
	 #endif
}
float4 PS_SmaaPass1(float2 f2_TexCoord : TEXCOORD0,
	 float2 f2_PixCoord : TEXCOORD1,
	 float4 f4_Offset0  : TEXCOORD2,
	 float4 f4_Offset1  : TEXCOORD3,
	 float4 f4_Offset2  : TEXCOORD4) : COLOR0
{
	 #if defined(ENABLE_SMAA_VS)
		 return float4(0.0, 0.0, 0.0, 0.0);
	 #else
	 	 float4 f4_Offsets[3];
	 	 f4_Offsets[0] = f4_Offset0;
	 	 f4_Offsets[1] = f4_Offset1;
	 	 f4_Offsets[2] = f4_Offset2;
	 	 return SMAABlendingWeightCalculationPS(f2_TexCoord, f2_PixCoord, f4_Offsets, samp_SmaaSourceTex, samp_SmaaAreaTex, samp_SmaaSearchTex, 0);
	 #endif
}

// -- Pass 2
void VS_SmaaPass2(
	 vs_Input _vs_In,
	 out float4 f4_Position : POSITION,
	 out float2 f2_TexCoord : TEXCOORD0,
	 out float4 f4_Offset0 : TEXCOORD1,
	 out float4 f4_Offset1 : TEXCOORD2
)
{
	 ComputePosUV2D(f4_Position, f2_TexCoord, _vs_In.f4_Position, _vs_In.f2_TexCoord0.xy);
	 #if defined(ENABLE_SMAA_VS)
		 float4 f4_Offsets[2];
		 SMAANeighborhoodBlendingVS(f4_Position, f4_Position, f2_TexCoord, f4_Offsets);
		 f4_Offset0 = f4_Offsets[0];
		 f4_Offset1 = f4_Offsets[1];
	 #else
	 	 f4_Offset0 = float4(0.0, 0.0, 0.0, 0.0);
	 	 f4_Offset1 = float4(0.0, 0.0, 0.0, 0.0);
	 #endif
}
float4 PS_SmaaPass2(
	 float2 f2_TexCoord : TEXCOORD0,
	 float4 f4_Offset0 : TEXCOORD1,
	 float4 f4_Offset1 : TEXCOORD2
) : COLOR0
{
	 #if defined(ENABLE_SMAA_VS)
		 return float4(0.0, 0.0, 0.0, 0.0);
	 #else
	 	 float4 f4_Offsets[2];
	 	 f4_Offsets[0] = f4_Offset0;
		 f4_Offsets[1] = f4_Offset1;
		 return SMAANeighborhoodBlendingPS(f2_TexCoord, f4_Offsets, samp_SmaaSourceTex, samp_SmaaBlendTex);
	 #endif
}

Edited by PixelSmasher, 11 September 2012 - 04:26 AM.


#13 FriendlyFire   Members   -  Reputation: 128

Like
0Likes
Like

Posted 13 September 2012 - 02:44 PM

Thanks for the help. I've been trying things following your post but I just can't seem to figure it out. Guess I'll put it on the backburner for a while longer...

#14 csirke128   Members   -  Reputation: 111

Like
0Likes
Like

Posted 21 October 2012 - 05:39 PM

Hi,

I've been working on a small 2d drawing app, and i wanted to implement SMAA for it, but i've ran into problems at the 2nd pass, instead of having nice connected lines, i had a lot of dotted lines, and nothing i did in the shaders/c++ code could change it, so i looked around in the SMAA.h and noticed some commented lines:

float SMAASearchLength(SMAATexture2D searchTex, float2 e, float bias, float scale) {
	// Not required if searchTex accesses are set to point:
	// float2 SEARCH_TEX_PIXEL_SIZE = 1.0 / float2(66.0, 33.0);
	// e = float2(bias, 0.0) + 0.5 * SEARCH_TEX_PIXEL_SIZE +
	//	 e * float2(scale, 1.0) * float2(64.0, 32.0) * SEARCH_TEX_PIXEL_SIZE;
	e.r = bias + e.r * scale;
	return 255.0 * SMAASampleLevelZeroPoint(searchTex, e).r;
}

And after i've changed it to this,

float SMAASearchLength(SMAATexture2D searchTex, float2 e, float bias, float scale) {
	// Not required if searchTex accesses are set to point:
	float2 SEARCH_TEX_PIXEL_SIZE = 1.0 / float2(66.0, 33.0);
	e = float2(bias, 0.0) + 0.5 * SEARCH_TEX_PIXEL_SIZE +
		e * float2(scale, 1.0) * float2(64.0, 32.0) * SEARCH_TEX_PIXEL_SIZE;
	//e.r = bias + e.r * scale;
	return 255.0 * SMAASampleLevelZeroPoint(searchTex, e).r;
}

everything worked fine

for comparison:

Before
smaa1.PNG
After
smaa2.PNG

It might not be the same problem, but i think its worth checking it out

(I was using directX11 on windows8, no techinques/effects just pixel and vertex shader)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS