Advertisement Jump to content
Sign in to follow this  

Motion Blur - 2013 Morgan McGuire paper issues

This topic is 1659 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post

Link to post
Share on other sites

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.



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.fFactor;
    //make sure it's non-zero, as it cannot be clamped to [0.5,MaxBlur] through multiplication
    //and division if it's zero
    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;
    return data;


vec2 jitterCoords(vec2 uv, float jitter)
    //  Get integral coords
    ivec2 iUV=ivec2(uv*;
    //  Integral coords inside the tile
    ivec2 iTileUV=iUV%motionBlurParameters.iMaxBlur;
    //  Distance from tile center (in pixels)
    //  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/merge/filter shader:

//  Composite
shader FS_composite(in vec2 vUv0,
                    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)
    //  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)
    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);
        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);
    //  Apply weight

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.



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

Share this post

Link to post
Share on other sites

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.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!