• Create Account

## Motion Blur - 2013 Morgan McGuire paper issues

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.

2 replies to this topic

### #1luorax  Members

254
Like
0Likes
Like

Posted 08 July 2014 - 03:20 PM

Hello community!

I came across a problem while implementing the algorithm presented in a paper that keeps on coming back after every attempt I make, so I decided I'd create an account and ask for some help as I don't seem to be able to resolve it and I'm slowly, but surely running out of hair.

First of all, here are the links: OVERVIEW PDF

I've got pretty much everything up and working. I can calculate both camera and object velocities in window space/pixels, this used to be working with older techniques too. I can store the length and depth along with the velocity in an ARGB8 texture for easy lookup. I can calculate the TileMax and NeighborMax vectors properly. I think even my blur algorithm is right.

So what is my problem, you might ask? Well, there are in fact 2 things that keep on bugging me:

1) I went with their velocity mapping algorithm (7th page, section 5). I can store the velocities just fine mapped to [0,1], but how exactly do I read them back? As I understand it, it's firstly mapped to [-0.5, 0.5] and then to [0,1] thanks to that trailing +0.5. So how do I read it back? Do I just go with (V[x]*2.0-1.0)*MaxBlur? Or do I go with (V[x]-0-5)*MaxBlur? Or do I do the full inverse of the function? I'm kinda lost, and the paper doesn't really talk about unpacking.

2) How exactly do you deal with zero, or close to zero velocity? The algorithm uses dot products and vector lengths using the original vector, which can quite easily be zero.

Some help would be really appreciated, as certain things are not exactly clear and also, feel free to ask for more info/my implementation if I didn't give enough info!

### #2luorax  Members

254
Like
0Likes
Like

Posted 09 July 2014 - 05:32 AM

Okay, so I've taken a closer look at the mapping function (issue #1), and I came to the conclusion that (V[x]-0.5)*2*MaxBlur will give me back the original velocity in pixels, as the second part of the equation (the 'weight') does the clamping and the first & third part (V[x]/(2*MaxBlur) and +0.5) does the normalization, so I only need to invert them.

I'm still unsure about issue #2. Here's a screenshot: IMG As you can se at the bottom (51 FPS), it sucks a lot of my FPS. What you cannot see though is that is it also screws up other post processing effects (such as my HDR & Bloom shaders), so I had to turn them off to take that screenshot. Note that this is not the case, if I render, say, the original/tile/neighbor max velocity buffers, which means that the incorrect output values are to be blamed for all the problems I just listed.

I'm posting my shaders, they might help. It's in GLSL, but uses the shader library glfx, that's why you might see some interesting syntax.

Parameters:

struct MotionBlurParameters
{
vec4 vPixelSize; //xy: 1.0/WindowSize, zw: WindowSize
vec4 vBufferSize;//xy: 1.0/TileMaxBufferSize, zw: TileMaxBufferSize
float fFactor;   //velocity scaling
int iTaps;       //[N] number of taps
int iMaxBlur;    //[r] max blur size
float fCenterWeight; //[kappa]
float fTileFalloff; //[tau] jitter scaling, falloff for jittering the center coord
float fInterpolationThreshold; //[gamma] interpolation threshold
float fJitterScale; //[eta] jitter scale for the taps
};

Velocity calculation, packing, unpacking:

struct VelocityData
{
vec2 vVelocity;
float fMagnitude;
float fDepth;
};

//currentPos and prevPos are NDC ones, the pixel's current
//and previous coordinates
vec3 computeVelocity(vec2 currentPos, vec2 prevPos)
{
vec2 velocity=0.5*(currentPos-prevPos)*motionBlurParameters.vPixelSize.zw*motionBlurParameters.fFactor;
//make sure it's non-zero, as it cannot be clamped to [0.5,MaxBlur] through multiplication
//and division if it's zero
velocity+=sign(velocity)*vec2(0.1);
float magnitude=length(velocity);
float weight=max(min(magnitude,motionBlurParameters.iMaxBlur),0.5)/magnitude;

vec2 normalized=velocity/(2*motionBlurParameters.iMaxBlur)*weight+0.5;

return vec3(normalized,weight*magnitude);
}

vec4 packVelocityData(vec2 currentPos, vec2 prevPos, float depth)
{
vec3 velocity=computeVelocity(currentPos,prevPos);

return vec4(velocity,depth);
}

VelocityData unpackVelocityData(vec4 raw)
{
VelocityData data;

data.vVelocity=(raw.xy-0.5)*2*motionBlurParameters.iMaxBlur;
data.fMagnitude=raw.z;
//data.fMagnitude=max(length(data.vVelocity),0.1);
data.fDepth=-raw.w;

return data;
}

Helpers:

vec2 jitterCoords(vec2 uv, float jitter)
{
//////////////////////////////////////////////////
//  Get integral coords
ivec2 iUV=ivec2(uv*motionBlurParameters.vPixelSize.zw);

//////////////////////////////////////////////////
//  Integral coords inside the tile
ivec2 iTileUV=iUV%motionBlurParameters.iMaxBlur;

//////////////////////////////////////////////////
//  Distance from tile center (in pixels)
iTileUV=abs(ivec2(int(motionBlurParameters.iMaxBlur/2.0))-iTileUV);

//////////////////////////////////////////////////
//  Normalized tile coords
vec2 vTileUV=iTileUV/motionBlurParameters.iMaxBlur;

//////////////////////////////////////////////////
//  Weight
float falloff=length(vTileUV)*motionBlurParameters.fTileFalloff;

return uv+jitter*falloff*motionBlurParameters.vPixelSize.xy;
}

float cone(float T, float invMagnitude)
{
return clamp(1.0-T*invMagnitude,0.0,1.0);
}

float cylinder(float T, float magnitude)
{
return 1.0-smoothstep(0.95*magnitude,1.05*magnitude,T);
}

float zCompare(float a, float b)
{
return clamp(1.0-(a-b)/min(a,b),0.0,1.0);
}

//////////////////////////////////////////////////
//  Composite
out vec4 finalColor: RENDER_TARGET_FINAL_COLOR)
{
//////////////////////////////////////////////////
//  Constants
const vec4 vPixelSize=motionBlurParameters.vPixelSize;

const int N=motionBlurParameters.iTaps;
const ivec2 p=ivec2(gl_FragCoord.xy);
const float j=clamp(rand(vUv0),-1.0,1.0);

//////////////////////////////////////////////////
//  NeighborMax data
VelocityData neighborMaxData=unpackVelocityData(texture(sNeighborMax,jitterCoords(vUv0,j)));
vec2 vmax=neighborMaxData.vVelocity;

//////////////////////////////////////////////////
//  Center color
vec3 vCenterColor=texelFetch(sColorBuffer,p,0).rgb;

//////////////////////////////////////////////////
//  Filter out unaffected pixels
if (neighborMaxData.fMagnitude<=0.75)
{
finalColor=vec4(vCenterColor,1);
return;
}

//////////////////////////////////////////////////
//  Calculate velocities
VelocityData centerData=unpackVelocityData(texelFetch(sOriginalVelocity,p,0));
vec2 wn=vmax/neighborMaxData.fMagnitude;
vec2 vc=centerData.vVelocity;
vec2 wp=vec2(-wn.y,wn.x);

if (dot(wp,vc)<0.0)
wp=-wp;

vec2 wc=normalize(mix(wp,vc/centerData.fMagnitude,(centerData.fMagnitude-0.5)/motionBlurParameters.fInterpolationThreshold));

//////////////////////////////////////////////////
//  Weight center
float totalWeight=N/(motionBlurParameters.fCenterWeight*centerData.fMagnitude);
vec3 result=vCenterColor*totalWeight;

//////////////////////////////////////////////////
//  Sample along velocities
float odd=0.0;
for (int i=0;i<N;i++)
{
float t=mix(-1.0,1.0,(i+j*motionBlurParameters.fJitterScale+1)/(N+1));

vec2 d=vc*odd+vmax*(1.0-odd);
odd=1.0-odd;
float T=t*neighborMaxData.fMagnitude;
ivec2 S=p+ivec2(int(t*d));

VelocityData currentData=unpackVelocityData(texelFetch(sOriginalVelocity,S,0));
vec3 colorSample=texelFetch(sColorBuffer,S,0).rgb;

float f=zCompare(centerData.fDepth,currentData.fDepth);
float b=zCompare(currentData.fDepth,centerData.fDepth);

float weight=0.0;
float wA=dot(wc,d);
float wB=dot(currentData.vVelocity/currentData.fMagnitude,d);

weight+=f*cone(T,1.0/currentData.fMagnitude)*wB;
weight+=b*cone(T,1.0/centerData.fMagnitude)*wA;
weight+=cylinder(T,min(centerData.fMagnitude,currentData.fMagnitude))*max(wA,wB)*2.0;

totalWeight+=weight;
result+=colorSample*weight;
}

//////////////////////////////////////////////////
//  Apply weight
finalColor=vec4(result/totalWeight,1.0);

//finalColor=vec4(texture(sNeighborMax,vUv0).xy,0.5,1);
//finalColor=vec4(unpackVelocityData(texture(sOriginalVelocity,vUv0)).fMagnitude);
}

I used the variable names present in the paper for easier understanding both for me and for the reader.

Does anyone have any ideas? It's driving me crazy.

[EDIT]

I just came to realize how dumb my 'packVelocityData' function is. sign returns (0.0,0.0) for the zero vector, so that's effectively adding zero to the zero vector. A.k.a. doing a bunch of computations for nothing. I'm going to change the code to account for that and see what happens.

Edited by luorax, 09 July 2014 - 03:02 PM.

### #3luorax  Members

254
Like
0Likes
Like

Posted 10 July 2014 - 03:02 AM

Okay, nevermind, I got it to work. I must admit it wasn't easy, and it still needs some tweaking, but at least it works.

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.