Skip to content

Commit

Permalink
Added offsetted area textures and improved the local contrast adaptat…
Browse files Browse the repository at this point in the history
…ion.
  • Loading branch information
iryoku committed Sep 9, 2011
1 parent 2eb6837 commit 670e0bb
Show file tree
Hide file tree
Showing 11 changed files with 4,811 additions and 397 deletions.
24 changes: 12 additions & 12 deletions Demo/DX10/Code/Demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ bool showHud = true;
bool benchmark = false;
fstream benchmarkFile;

int frame = 0;
int subpixelIndex = 0;

struct {
float threshold;
Expand Down Expand Up @@ -495,9 +495,9 @@ void drawHud(float elapsedTime) {


void CALLBACK onFrameRender(ID3D10Device *device, double time, float elapsedTime, void *context) {
// Calculate next frame index:
int previousFrame = frame;
int currentFrame = (frame + 1) % 2;
// Calculate next subpixel index:
int previousIndex = subpixelIndex;
int currentIndex = (subpixelIndex + 1) % 2;

// Render the settings dialog:
if (settingsDialog.IsActive()) {
Expand All @@ -519,12 +519,12 @@ void CALLBACK onFrameRender(ID3D10Device *device, double time, float elapsedTime
if (hud.GetCheckBox(IDC_ANTIALIASING)->GetChecked() &&
SMAA::Mode(int(hud.GetComboBox(IDC_SMAA_MODE)->GetSelectedData())) == SMAA::MODE_SMAA_T2X) {
D3DXVECTOR2 jitter[] = {
D3DXVECTOR2(-0.25f, -0.25f),
D3DXVECTOR2( 0.25f, 0.25f)
D3DXVECTOR2(-0.25f, 0.25f),
D3DXVECTOR2( 0.25f, -0.25f)
};
const DXGI_SURFACE_DESC *desc = DXUTGetDXGIBackBufferSurfaceDesc();
float aspect = (float) desc->Width / desc->Height;
camera.setJitteredProjection(18.0f * D3DX_PI / 180.f, aspect, 0.1f, 1000.0f, jitter[frame]);
camera.setJitteredProjection(18.0f * D3DX_PI / 180.f, aspect, 0.1f, 1000.0f, jitter[subpixelIndex]);
}

renderMesh(device);
Expand All @@ -537,13 +537,13 @@ void CALLBACK onFrameRender(ID3D10Device *device, double time, float elapsedTime

SMAA::Mode mode = SMAA::Mode(int(hud.GetComboBox(IDC_SMAA_MODE)->GetSelectedData()));
ID3D10RenderTargetView *dstRTV = mode == SMAA::MODE_SMAA_1X?
(ID3D10RenderTargetView *) *backbufferRT : *finalRT[currentFrame];
(ID3D10RenderTargetView *) *backbufferRT : *finalRT[currentIndex];

timer->start();
for (int i = 0; i < n; i++) { // This loop is for profiling.
smaa->go(*tmpRT, *tmpRT_SRGB, *depthBufferRT, dstRTV, *depthStencil, input);
smaa->go(*tmpRT, *tmpRT_SRGB, *depthBufferRT, dstRTV, *depthStencil, input, mode, subpixelIndex);
if (mode == SMAA::MODE_SMAA_T2X)
smaa->resolve(*finalRT[currentFrame], *finalRT[previousFrame], *backbufferRT);
smaa->resolve(*finalRT[currentIndex], *finalRT[previousIndex], *backbufferRT);
}
timer->clock(L"SMAA");
} else {
Expand All @@ -564,8 +564,8 @@ void CALLBACK onFrameRender(ID3D10Device *device, double time, float elapsedTime
drawTextures(device);
drawHud(elapsedTime);

// Update frame index:
frame = currentFrame;
// Update subpixel index:
subpixelIndex = currentIndex;
}


Expand Down
30 changes: 27 additions & 3 deletions Demo/DX10/Code/SMAA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ SMAA::SMAA(ID3D10Device *device, int width, int height, Preset preset, bool pred
cornerRoundingVariable = effect->GetVariableByName("cornerRounding")->AsScalar();
maxSearchStepsVariable = effect->GetVariableByName("maxSearchSteps")->AsScalar();
maxSearchStepsDiagVariable = effect->GetVariableByName("maxSearchStepsDiag")->AsScalar();
subsampleIndicesVariable = effect->GetVariableByName("subsampleIndices")->AsVector();
areaTexVariable = effect->GetVariableByName("areaTex")->AsShaderResource();
searchTexVariable = effect->GetVariableByName("searchTex")->AsShaderResource();
colorTexVariable = effect->GetVariableByName("colorTex")->AsShaderResource();
Expand Down Expand Up @@ -221,13 +222,17 @@ void SMAA::go(ID3D10ShaderResourceView *srcGammaSRV,
ID3D10ShaderResourceView *depthSRV,
ID3D10RenderTargetView *dstRTV,
ID3D10DepthStencilView *dsv,
Input input) {
Input input,
Mode mode,
int subsampleIndex) {
HRESULT hr;

// Save the state:
SaveViewportsScope saveViewport(device);
SaveRenderTargetsScope saveRenderTargets(device);
SaveInputLayoutScope saveInputLayout(device);
SaveBlendStateScope saveBlendState(device);
SaveDepthStencilScope saveDepthStencil(device);

// Reset the render target:
device->OMSetRenderTargets(0, NULL, NULL);
Expand Down Expand Up @@ -258,7 +263,7 @@ void SMAA::go(ID3D10ShaderResourceView *srcGammaSRV,

// And here we go!
edgesDetectionPass(dsv, input);
blendingWeightsCalculationPass(dsv);
blendingWeightsCalculationPass(dsv, mode, subsampleIndex);
neighborhoodBlendingPass(dstRTV, dsv);
}

Expand All @@ -273,6 +278,8 @@ void SMAA::resolve(ID3D10ShaderResourceView *currentSRV,
SaveViewportsScope saveViewport(device);
SaveRenderTargetsScope saveRenderTargets(device);
SaveInputLayoutScope saveInputLayout(device);
SaveBlendStateScope saveBlendState(device);
SaveDepthStencilScope saveDepthStencil(device);

// Setup the viewport and the vertex layout:
edgesRT->setViewport();
Expand Down Expand Up @@ -388,10 +395,27 @@ void SMAA::edgesDetectionPass(ID3D10DepthStencilView *dsv, Input input) {
}


void SMAA::blendingWeightsCalculationPass(ID3D10DepthStencilView *dsv) {
void SMAA::blendingWeightsCalculationPass(ID3D10DepthStencilView *dsv, Mode mode, int subsampleIndex) {
D3DPERF_BeginEvent(D3DCOLOR_XRGB(0, 0, 0), L"SMAA: 2nd pass");
HRESULT hr;


switch (mode) {
case MODE_SMAA_1X: {
int indices[4] = { 0, 0, 0, 0 };
V(subsampleIndicesVariable->SetIntVector(indices));
break;
}
case MODE_SMAA_T2X: {
int indices[][4] = {
{ 1, 1, 1, 0 },
{ 2, 2, 2, 0 }
};
V(subsampleIndicesVariable->SetIntVector(indices[subsampleIndex]));
break;
}
}

// Setup the technique (again):
V(blendWeightCalculationTechnique->GetPassByIndex(0)->Apply(0));

Expand Down
47 changes: 27 additions & 20 deletions Demo/DX10/Code/SMAA.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@

/**
* IMPORTANT NOTICE: please note that the documentation given in this file is
* rather limited. For more information checkout SMAA.h in the root directory
* of the source release (the shader header).
* rather limited. We recommend first checking out SMAA.h in the root directory
* of the source release (the shader header), then coming back here. This is
* just a C++ interface to the shader.
*/


Expand All @@ -60,17 +61,12 @@ class SMAA {
enum Input { INPUT_LUMA, INPUT_COLOR, INPUT_DEPTH };

/**
* If you have one or two spare render targets of the same size as the
* backbuffer, you may want to pass them in the 'storage' parameter.
* You may pass one or the two, depending on what you have available.
*
* A non-sRGB RG buffer (at least) is expected for storing edges.
* A non-sRGB RGBA buffer is expected for the blending weights.
*
* By default, two render targets will be created for storing
* intermediate calculations.
* intermediate calculations. If you have spare render targets,
* search for @EXTERNAL_STORAGE.
*/
SMAA(ID3D10Device *device, int width, int height, Preset preset, bool predication=false,
SMAA(ID3D10Device *device, int width, int height,
Preset preset=PRESET_HIGH, bool predication=false,
const ExternalStorage &storage=ExternalStorage());
~SMAA();

Expand All @@ -87,23 +83,25 @@ class SMAA {
*
* Input texture 'depthSRV' will be used for predication if enabled. We
* recommend using a light accumulation buffer or object ids, if
* available.
* available; it'll probably yield better results.
*
* To ease implementation, everything is saved (blend state, viewport,
* etc.), you may want to check Save*Scope in the implementation if you
* don't need this.
*
* IMPORTANT: The stencil component of 'dsv' is used to mask zones to
* be processed. It is assumed to be already cleared to zero when this
* function is called. It is not done here because it is usually
* cleared together with the depth.
*
* The viewport, the binded render target and the input layout is saved
* and restored accordingly. However, we modify but not restore the
* depth-stencil and blend states.
*/
void go(ID3D10ShaderResourceView *srcGammaSRV, // Non-SRGB version of the input color texture.
ID3D10ShaderResourceView *srcSRV, // SRGB version of the input color texture.
ID3D10ShaderResourceView *depthSRV, // Input depth texture.
ID3D10RenderTargetView *dstRTV, // Output render target.
ID3D10DepthStencilView *dsv, // Depth-stencil buffer.
Input input); // Selects the input for edge detection.
ID3D10DepthStencilView *dsv, // Depth-stencil buffer for optimizations.
Input input, // Selects the input for edge detection.
Mode mode=MODE_SMAA_1X,
int subsampleIndex=0);

/**
* This function perform a temporal resolve of two buffers. They must
Expand Down Expand Up @@ -148,7 +146,15 @@ class SMAA {
RenderTarget *getBlendRenderTarget() { return blendRT; }

/**
* This class allows to pass spare storage buffers to the SMAA class.
* @EXTERNAL_STORAGE
*
* If you have one or two spare render targets of the same size as the
* backbuffer, you may want to pass them to SMAA::SMAA() using a
* ExternalStorage object. You may pass one or the two, depending on
* what you have available.
*
* A non-sRGB RG buffer (at least) is expected for storing edges.
* A non-sRGB RGBA buffer is expected for the blending weights.
*/
class ExternalStorage {
public:
Expand All @@ -169,7 +175,7 @@ class SMAA {
void loadAreaTex();
void loadSearchTex();
void edgesDetectionPass(ID3D10DepthStencilView *dsv, Input input);
void blendingWeightsCalculationPass(ID3D10DepthStencilView *dsv);
void blendingWeightsCalculationPass(ID3D10DepthStencilView *dsv, Mode mode, int subsampleIndex);
void neighborhoodBlendingPass(ID3D10RenderTargetView *dstRTV, ID3D10DepthStencilView *dsv);

ID3D10Device *device;
Expand All @@ -187,6 +193,7 @@ class SMAA {

ID3D10EffectScalarVariable *thresholdVariable, *cornerRoundingVariable,
*maxSearchStepsVariable, *maxSearchStepsDiagVariable;
ID3D10EffectVectorVariable *subsampleIndicesVariable;
ID3D10EffectShaderResourceVariable *areaTexVariable, *searchTexVariable,
*colorTexVariable, *colorTexGammaVariable, *colorTexPrevVariable, *depthTexVariable,
*edgesTexVariable, *blendTexVariable;
Expand Down
15 changes: 10 additions & 5 deletions Demo/DX10/Shaders/SMAA.fx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
#define SMAA_PIXEL_SIZE float2(1.0 / 1280.0, 1.0 / 720.0)
#endif

/**
* This is only required for temporal modes (SMAA T2x).
*/
int4 subsampleIndices;

/**
* This can be ignored; its purpose is to support interactive custom parameter
* tweaking.
Expand Down Expand Up @@ -132,7 +137,7 @@ Texture2D searchTex;
void DX10_SMAAEdgeDetectionVS(float4 position : POSITION,
out float4 svPosition : SV_POSITION,
inout float2 texcoord : TEXCOORD0,
out float4 offset[2] : TEXCOORD1) {
out float4 offset[3] : TEXCOORD1) {
SMAAEdgeDetectionVS(position, svPosition, texcoord, offset);
}

Expand All @@ -159,7 +164,7 @@ void DX10_SMAAResolveVS(float4 position : POSITION,

float4 DX10_SMAALumaEdgeDetectionPS(float4 position : SV_POSITION,
float2 texcoord : TEXCOORD0,
float4 offset[2] : TEXCOORD1,
float4 offset[3] : TEXCOORD1,
uniform SMAATexture2D colorTexGamma) : SV_TARGET {
#if SMAA_PREDICATION == 1
return SMAALumaEdgeDetectionPS(texcoord, offset, colorTexGamma, depthTex);
Expand All @@ -170,7 +175,7 @@ float4 DX10_SMAALumaEdgeDetectionPS(float4 position : SV_POSITION,

float4 DX10_SMAAColorEdgeDetectionPS(float4 position : SV_POSITION,
float2 texcoord : TEXCOORD0,
float4 offset[2] : TEXCOORD1,
float4 offset[3] : TEXCOORD1,
uniform SMAATexture2D colorTexGamma) : SV_TARGET {
#if SMAA_PREDICATION == 1
return SMAAColorEdgeDetectionPS(texcoord, offset, colorTexGamma, depthTex);
Expand All @@ -181,7 +186,7 @@ float4 DX10_SMAAColorEdgeDetectionPS(float4 position : SV_POSITION,

float4 DX10_SMAADepthEdgeDetectionPS(float4 position : SV_POSITION,
float2 texcoord : TEXCOORD0,
float4 offset[2] : TEXCOORD1,
float4 offset[3] : TEXCOORD1,
uniform SMAATexture2D depthTex) : SV_TARGET {
return SMAADepthEdgeDetectionPS(texcoord, offset, depthTex);
}
Expand All @@ -193,7 +198,7 @@ float4 DX10_SMAABlendingWeightCalculationPS(float4 position : SV_POSITION,
uniform SMAATexture2D edgesTex,
uniform SMAATexture2D areaTex,
uniform SMAATexture2D searchTex) : SV_TARGET {
return SMAABlendingWeightCalculationPS(texcoord, pixcoord, offset, edgesTex, areaTex, searchTex);
return SMAABlendingWeightCalculationPS(texcoord, pixcoord, offset, edgesTex, areaTex, searchTex, subsampleIndices);
}

float4 DX10_SMAANeighborhoodBlendingPS(float4 position : SV_POSITION,
Expand Down
8 changes: 4 additions & 4 deletions Demo/DX9/Shaders/SMAA.fx
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ sampler2D searchTex {
*/
void DX9_SMAAEdgeDetectionVS(inout float4 position : POSITION,
inout float2 texcoord : TEXCOORD0,
out float4 offset[2] : TEXCOORD1) {
out float4 offset[3] : TEXCOORD1) {
SMAAEdgeDetectionVS(position, position, texcoord, offset);
}

Expand All @@ -156,21 +156,21 @@ void DX9_SMAANeighborhoodBlendingVS(inout float4 position : POSITION,

float4 DX9_SMAALumaEdgeDetectionPS(float4 position : SV_POSITION,
float2 texcoord : TEXCOORD0,
float4 offset[2] : TEXCOORD1,
float4 offset[3] : TEXCOORD1,
uniform SMAATexture2D colorGammaTex) : COLOR {
return SMAALumaEdgeDetectionPS(texcoord, offset, colorGammaTex);
}

float4 DX9_SMAAColorEdgeDetectionPS(float4 position : SV_POSITION,
float2 texcoord : TEXCOORD0,
float4 offset[2] : TEXCOORD1,
float4 offset[3] : TEXCOORD1,
uniform SMAATexture2D colorGammaTex) : COLOR {
return SMAAColorEdgeDetectionPS(texcoord, offset, colorGammaTex);
}

float4 DX9_SMAADepthEdgeDetectionPS(float4 position : SV_POSITION,
float2 texcoord : TEXCOORD0,
float4 offset[2] : TEXCOORD1,
float4 offset[3] : TEXCOORD1,
uniform SMAATexture2D depthTex) : COLOR {
return SMAADepthEdgeDetectionPS(texcoord, offset, depthTex);
}
Expand Down
22 changes: 22 additions & 0 deletions Demo/Support/RenderTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,28 @@ SaveInputLayoutScope::~SaveInputLayoutScope() {
}


SaveBlendStateScope::SaveBlendStateScope(ID3D10Device *device) : device(device) {
device->OMGetBlendState(&blendState, blendFactor, &sampleMask);
}


SaveBlendStateScope::~SaveBlendStateScope() {
device->OMSetBlendState(blendState, blendFactor, sampleMask);
SAFE_RELEASE(blendState);
}


SaveDepthStencilScope::SaveDepthStencilScope(ID3D10Device *device) : device(device) {
device->OMGetDepthStencilState(&depthStencilState, &stencilRef);
}


SaveDepthStencilScope::~SaveDepthStencilScope() {
device->OMSetDepthStencilState(depthStencilState, stencilRef);
SAFE_RELEASE(depthStencilState);
}


D3D10_VIEWPORT Utils::viewportFromView(ID3D10View *view) {
ID3D10Texture2D *texture2D;
view->GetResource(reinterpret_cast<ID3D10Resource **>(&texture2D));
Expand Down
25 changes: 25 additions & 0 deletions Demo/Support/RenderTarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,31 @@ class SaveInputLayoutScope {
};


class SaveBlendStateScope {
public:
SaveBlendStateScope(ID3D10Device *device);
~SaveBlendStateScope();

private:
ID3D10Device *device;
ID3D10BlendState *blendState;
FLOAT blendFactor[4];
UINT sampleMask;
};


class SaveDepthStencilScope {
public:
SaveDepthStencilScope(ID3D10Device *device);
~SaveDepthStencilScope();

private:
ID3D10Device *device;
ID3D10DepthStencilState *depthStencilState;
UINT stencilRef;
};


class Utils {
public:
static D3D10_VIEWPORT viewportFromView(ID3D10View *view);
Expand Down
Loading

0 comments on commit 670e0bb

Please sign in to comment.