• 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

Motion Blur - 2013 Morgan McGuire paper issues

2 posts in this topic

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.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
    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*motionBlurParameters.vPixelSize.zw);
    //  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

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