Permalink
Browse files

- Added CryEngine 3 temporal reprojection.

- Added more complex shading for testing shading antialiasing.
  • Loading branch information...
1 parent 0ca6997 commit bc8df48b581d2e481f1b0f37a6fe8eede491fdb8 @iryoku committed Sep 22, 2011
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -100,7 +100,7 @@ class ID3D10IncludeResource : public ID3D10Include {
#pragma endregion
-SMAA::SMAA(ID3D10Device *device, int width, int height, Preset preset, bool predication, const ExternalStorage &storage)
+SMAA::SMAA(ID3D10Device *device, int width, int height, Preset preset, bool predication, bool reprojection, const ExternalStorage &storage)
: device(device),
preset(preset),
threshold(0.1f),
@@ -143,6 +143,12 @@ SMAA::SMAA(ID3D10Device *device, int width, int height, Preset preset, bool pred
defines.push_back(predicationMacro);
}
+ // Setup reprojection macro:
+ if (reprojection) {
+ D3D10_SHADER_MACRO reprojectionMacro = { "SMAA_REPROJECTION", "1" };
+ defines.push_back(reprojectionMacro);
+ }
+
// Setup target macro:
if (dx10_1) {
D3D10_SHADER_MACRO dx101Macro = { "SMAA_HLSL_4_1", "1" };
@@ -182,7 +188,7 @@ SMAA::SMAA(ID3D10Device *device, int width, int height, Preset preset, bool pred
loadAreaTex();
loadSearchTex();
- // Create some handles for techniques and variables:
+ // Create some handles for variables:
thresholdVariable = effect->GetVariableByName("threshld")->AsScalar();
cornerRoundingVariable = effect->GetVariableByName("cornerRounding")->AsScalar();
maxSearchStepsVariable = effect->GetVariableByName("maxSearchSteps")->AsScalar();
@@ -194,8 +200,11 @@ SMAA::SMAA(ID3D10Device *device, int width, int height, Preset preset, bool pred
colorTexGammaVariable = effect->GetVariableByName("colorTexGamma")->AsShaderResource();
colorTexPrevVariable = effect->GetVariableByName("colorTexPrev")->AsShaderResource();
depthTexVariable = effect->GetVariableByName("depthTex")->AsShaderResource();
+ velocityTexVariable = effect->GetVariableByName("velocityTex")->AsShaderResource();
edgesTexVariable = effect->GetVariableByName("edgesTex")->AsShaderResource();
blendTexVariable = effect->GetVariableByName("blendTex")->AsShaderResource();
+
+ // Create handles for techniques:
edgeDetectionTechniques[0] = effect->GetTechniqueByName("LumaEdgeDetection");
edgeDetectionTechniques[1] = effect->GetTechniqueByName("ColorEdgeDetection");
edgeDetectionTechniques[2] = effect->GetTechniqueByName("DepthEdgeDetection");
@@ -270,6 +279,7 @@ void SMAA::go(ID3D10ShaderResourceView *srcGammaSRV,
void SMAA::resolve(ID3D10ShaderResourceView *currentSRV,
ID3D10ShaderResourceView *previousSRV,
+ ID3D10ShaderResourceView *velocitySRV,
ID3D10RenderTargetView *dstRTV) {
D3DPERF_BeginEvent(D3DCOLOR_XRGB(0, 0, 0), L"SMAA: temporal resolve");
HRESULT hr;
@@ -288,6 +298,7 @@ void SMAA::resolve(ID3D10ShaderResourceView *currentSRV,
// Setup variables:
V(colorTexVariable->SetResource(currentSRV));
V(colorTexPrevVariable->SetResource(previousSRV));
+ V(velocityTexVariable->SetResource(velocitySRV));
// Select the technique accordingly:
V(resolveTechnique->GetPassByIndex(0)->Apply(0));
View
@@ -66,7 +66,7 @@ class SMAA {
* search for @EXTERNAL_STORAGE.
*/
SMAA(ID3D10Device *device, int width, int height,
- Preset preset=PRESET_HIGH, bool predication=false,
+ Preset preset=PRESET_HIGH, bool predication=false, bool reprojection=false,
const ExternalStorage &storage=ExternalStorage());
~SMAA();
@@ -109,6 +109,7 @@ class SMAA {
*/
void resolve(ID3D10ShaderResourceView *currentSRV,
ID3D10ShaderResourceView *previousSRV,
+ ID3D10ShaderResourceView *velocitySRV,
ID3D10RenderTargetView *dstRTV);
/**
@@ -195,7 +196,8 @@ class SMAA {
*maxSearchStepsVariable, *maxSearchStepsDiagVariable;
ID3D10EffectVectorVariable *subsampleIndicesVariable;
ID3D10EffectShaderResourceVariable *areaTexVariable, *searchTexVariable,
- *colorTexVariable, *colorTexGammaVariable, *colorTexPrevVariable, *depthTexVariable,
+ *colorTexVariable, *colorTexGammaVariable, *colorTexPrevVariable,
+ *depthTexVariable, *velocityTexVariable,
*edgesTexVariable, *blendTexVariable;
ID3D10EffectTechnique *edgeDetectionTechniques[3],
View
@@ -21,3 +21,6 @@ Unigine06.dds RCDATA "..\\Media\\Unigine06.dds"
Unigine07.dds RCDATA "..\\Media\\Unigine07.dds"
Fence.sdkmesh RCDATA "..\\Media\\Fence.sdkmesh"
+DiffuseMap.dds RCDATA "..\\Media\\DiffuseMap.dds"
+NormalMap.dds RCDATA "..\\Media\\NormalMap.dds"
+EnvMap.dds RCDATA "..\\Media\\EnvMap.dds"
View
@@ -158,6 +158,14 @@
<AdditionalInputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">..\..\SMAA.h</AdditionalInputs>
<AdditionalInputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">..\..\SMAA.h</AdditionalInputs>
</CustomBuild>
+ <CustomBuild Include="Shaders\Simple.fx">
+ <Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">fxc /Od /Zi /Tfx_4_0 /nologo /I ../.. /Fo"$(IntDir)%(Filename).fxo" /Fc"$(IntDir)%(Filename).asm" "%(FullPath)"
+</Command>
+ <Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(IntDir)%(Filename).fxo</Outputs>
+ <Command Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">fxc /Tfx_4_0 /nologo /I ../.. /Fo"$(IntDir)%(Filename).fxo" /Fc"$(IntDir)%(Filename).asm" "%(FullPath)"
+</Command>
+ <Outputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(IntDir)%(Filename).fxo</Outputs>
+ </CustomBuild>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="Demo.rc">
@@ -124,5 +124,8 @@
<CustomBuild Include="Shaders\SMAA.fx">
<Filter>Shaders</Filter>
</CustomBuild>
+ <CustomBuild Include="Shaders\Simple.fx">
+ <Filter>Shaders</Filter>
+ </CustomBuild>
</ItemGroup>
</Project>
View
@@ -117,6 +117,7 @@ Texture2D colorTexPrev;
Texture2D colorTex;
Texture2D colorTexGamma;
Texture2D depthTex;
+Texture2D velocityTex;
/**
* Temporal textures
@@ -212,8 +213,13 @@ float4 DX10_SMAANeighborhoodBlendingPS(float4 position : SV_POSITION,
float4 DX10_SMAAResolvePS(float4 position : SV_POSITION,
float2 texcoord : TEXCOORD0,
uniform SMAATexture2D colorTex,
- uniform SMAATexture2D colorTexPrev) : SV_TARGET {
+ uniform SMAATexture2D colorTexPrev,
+ uniform SMAATexture2D velocityTex) : SV_TARGET {
+ #if SMAA_REPROJECTION == 1
+ return SMAAResolvePS(texcoord, colorTex, colorTexPrev, velocityTex);
+ #else
return SMAAResolvePS(texcoord, colorTex, colorTexPrev);
+ #endif
}
/**
@@ -287,7 +293,7 @@ technique10 Resolve {
pass Resolve {
SetVertexShader(CompileShader(vs_4_0, DX10_SMAAResolveVS()));
SetGeometryShader(NULL);
- SetPixelShader(CompileShader(PS_VERSION, DX10_SMAAResolvePS(colorTex, colorTexPrev)));
+ SetPixelShader(CompileShader(PS_VERSION, DX10_SMAAResolvePS(colorTex, colorTexPrev, velocityTex)));
SetDepthStencilState(DisableDepthStencil, 0);
SetBlendState(NoBlending, float4(0.0f, 0.0f, 0.0f, 0.0f), 0xFFFFFFFF);
View
@@ -28,20 +28,154 @@
* policies, either expressed or implied, of the copyright holders.
*/
+// For reprojection:
+matrix currWorldViewProj;
+matrix prevWorldViewProj;
+float2 jitter;
-matrix transform;
+// For shading:
+matrix world;
+float3 eyePositionW;
-float4 SimpleVS(float4 position : POSITION0,
- float3 normal : NORMAL,
- float3 tangent : TANGENT,
- inout float2 texcoord : TEXCOORD0) : SV_POSITION {
- return mul(position, transform);
+Texture2D diffuseTex;
+Texture2D normalTex;
+TextureCube envTex;
+
+
+// Samplers:
+SamplerState LinearSampler {
+ Filter = MIN_MAG_MIP_LINEAR;
+ AddressU = Wrap;
+ AddressV = Wrap;
+};
+
+SamplerState AnisotropicSampler {
+ Filter = ANISOTROPIC;
+ AddressU = Wrap;
+ AddressV = Wrap;
+ MaxAnisotropy = 16;
+};
+
+
+struct SimpleV2P {
+ // For reprojection:
+ float4 svPosition : SV_POSITION;
+ float2 texcoord : TEXCOORD0;
+ float3 currPosition : TEXCOORD1;
+ float3 prevPosition : TEXCOORD2;
+
+ // For shading:
+ float3 normalW : TEXCOORD3;
+ float3 tangentW : TEXCOORD4;
+ float3 positionW : TEXCOORD5;
+};
+
+
+SimpleV2P SimpleVS(float4 position : POSITION,
+ float3 normal : NORMAL,
+ float2 texcoord : TEXCOORD,
+ float3 tangent : TANGENT) {
+ SimpleV2P output;
+
+ // Transform to homogeneous projection space:
+ output.svPosition = mul(position, currWorldViewProj);
+ output.currPosition = output.svPosition.xyw;
+ output.prevPosition = mul(position, prevWorldViewProj).xyw;
+
+ // Covert the jitter from non-homogeneous coordiantes to homogeneous
+ // coordinates and add it:
+ // (note that for providing the jitter in non-homogeneous projection space,
+ // pixel coordinates (screen space) need to multiplied by two in the C++
+ // code)
+ output.svPosition.xy += jitter * output.svPosition.w;
+
+ // Positions in projection space are in [-1, 1] range, while texture
+ // coordinates are in [0, 1] range. So, we divide by 2 to get velocities in
+ // the scale:
+ output.currPosition.xy /= 2.0;
+ output.prevPosition.xy /= 2.0;
+
+ // Texture coordinates have a top-to-bottom y axis, so flip this axis:
+ output.currPosition.y = -output.currPosition.y;
+ output.prevPosition.y = -output.prevPosition.y;
+
+ // Output texture coordinates:
+ output.texcoord = texcoord;
+
+ // Build the vectors required for shading:
+ output.normalW = mul(normal, (float3x3) world);
+ output.tangentW = mul(tangent, (float3x3) world);
+ output.positionW = mul(position, world).xyz;
+
+ return output;
}
-float4 SimplePS(float4 position : SV_POSITION) : SV_TARGET {
- return 0.5;
+float3 Shade(SimpleV2P input) {
+ // Normalize the input:
+ input.normalW = normalize(input.normalW);
+ input.tangentW = normalize(input.tangentW);
+
+ // Calculate eye and light vectors:
+ float3 eyeW = normalize(eyePositionW - input.positionW);
+ float3 lightW = float3(0.0, 1.0, -1.0);
+
+ // Fetch and unpack the normal:
+ float3 normalT = normalTex.Sample(AnisotropicSampler, input.texcoord).rgb;
+ normalT.xy = -1.0 + 2.0 * normalT.gr;
+ normalT.z = sqrt(1.0 - normalT.x * normalT.x - normalT.y * normalT.y);
+ normalT = normalize(normalT);
+
+ // Transform the normal to world space:
+ float3 bitangentW = cross(input.normalW, input.tangentW);
+ float3x3 tbn = transpose(float3x3(input.tangentW, bitangentW, input.normalW));
+ float3 normalW = mul(tbn, normalT);
+
+ // Fetch the albedo color:
+ float4 albedo = diffuseTex.Sample(AnisotropicSampler, input.texcoord);
+
+ // Calculate environment reflections:
+ // (we'll try to do our best with a single specular map)
+ float3 reflectionW = normalize(reflect(-eyeW, normalW));
+ float intensity = albedo.a * albedo.a;
+ float enviroment = intensity * dot(envTex.Sample(LinearSampler, reflectionW).rgb, 1.0 / 3.0);
+
+ // Set the ambient color:
+ float ambient = 0.025;
+
+ // Calculate the diffuse component:
+ float diffuse = max(dot(normalW, lightW), 0.0);
+
+ // Accumulate them:
+ float3 color = (albedo.rgb + enviroment) * (ambient + diffuse);
+
+ // Calculate and accumulate the specular component:
+ reflectionW = normalize(reflect(-lightW, normalW));
+ color += 7.0 * albedo.a * pow(max(dot(reflectionW, eyeW), 0.0), 15.0);
+
+ // Return the shaded pixel:
+ return color;
+}
+
+
+float4 SimplePS(SimpleV2P input,
+ out float2 velocity : SV_TARGET1) : SV_TARGET0 {
+ // Convert to non-homogeneous points by dividing by w:
+ input.currPosition.xy /= input.currPosition.z; // w is stored in z
+ input.prevPosition.xy /= input.prevPosition.z;
+
+ // Calculate velocity in non-homogeneous projection space:
+ velocity = input.currPosition.xy - input.prevPosition.xy;
+
+ // Compress the velocity for storing it in a 8-bit render target:
+ float velocityLength = sqrt(5.0 * length(velocity));
+
+ // Shade the pixel:
+ float3 color = Shade(input);
+
+ // Output the results, packing the velocity length in the alpha channel:
+ return float4(color, velocityLength);
}
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
@@ -169,7 +169,7 @@ void Camera::build() {
D3DXMatrixRotationX(&t, angle.y);
view = t * view;
- D3DXMatrixRotationY(&t, angle.x);
+ D3DXMatrixRotationZ(&t, angle.x);
view = t * view;
D3DXMATRIX viewInverse;
Oops, something went wrong.

0 comments on commit bc8df48

Please sign in to comment.