• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
FriendlyFire

SMAA Implementation - Weights Pass

13 posts in this topic

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:
[CODE]
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
}
[/CODE]

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: [url="https://github.com/iryoku/smaa/zipball/v2.7"]https://github.com/iryoku/smaa/zipball/v2.7[/url]

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:
[img]http://img829.imageshack.us/img829/741/diffuse.png[/img]

Edges:
[img]http://img9.imageshack.us/img9/9512/edgesd.png[/img]

Related edge stencil:
[img] http://img528.imageshack.us/img528/352/edgesstencil.png[/img]

Weights:
[img]http://img205.imageshack.us/img205/543/weights.png[/img]

Weights (alpha channel):
[img]http://img11.imageshack.us/img11/989/weightsalpha.png[/img]

Demo program edges:
[img]http://img13.imageshack.us/img13/1281/demoedges.png[/img]

Demo program edge stencil:
[img]http://img835.imageshack.us/img835/6909/demoedgesstencil.png[/img]

Demo program weights:
[img]http://img24.imageshack.us/img24/5421/demoweights.png[/img]

Demo program weights (alpha):
[img]http://img638.imageshack.us/img638/4997/demoweightsalpha.png[/img]


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.
0

Share this post


Link to post
Share on other sites
[left]Are you taking D3D9's [url="http://msdn.microsoft.com/en-us/library/windows/desktop/bb219690(v=vs.85).aspx"]half-pixel screen-vertex offset[/url] into account?[/left]

[left]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.[/left]
0

Share this post


Link to post
Share on other sites
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):
[CODE]
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 }
};
[/CODE]

I've also tried using the SMAA demo's buffer:
[CODE]
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 }
};
[/CODE]
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:
[CODE]
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 }
};
[/CODE]
produces even fewer weights, so I think the initial buffer is fine.
0

Share this post


Link to post
Share on other sites
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:
[img]http://img840.imageshack.us/img840/4074/newncy.png[/img]

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.
0

Share this post


Link to post
Share on other sites
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?
0

Share this post


Link to post
Share on other sites
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 ?
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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 [img]http://public.gamedev.net//public/style_emoticons/default/ph34r.png[/img]. Edited by PixelSmasher
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
[quote name='PixelSmasher' timestamp='1347288471' post='4978591']
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.
[/quote]

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.
0

Share this post


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


[CODE]
// -- 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
}
[/CODE] Edited by PixelSmasher
0

Share this post


Link to post
Share on other sites
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...
0

Share this post


Link to post
Share on other sites
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:

[CODE]
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;
}
[/CODE]

And after i've changed it to this,

[CODE]
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;
}
[/CODE]

everything worked fine

for comparison:

Before
[attachment=11856:smaa1.PNG]
After
[attachment=11857: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)
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0