Welcome, Guest
Username Password: Remember me

TOPIC: Making my own keyer

Making my own keyer 4 years, 9 months ago #104173

  • jwrl
  • Moderator
    Pro User
  • OFFLINE
  • Moderator
  • Posts: 12887
  • 4 years, 9 months ago
This project started with me doing some modifications to baopao's Ale_ChromaKey effect as a learning exercise. This has now gone in an entirely different direction, so I thought that others might be interested in the development process. To that end, I've included examples of the code to help others who may be interested in writing shader effects.

Very big caveat: because I'm learning as I go, I will inevitably make mistakes. Anyone interested in following me will doubtless benefit from them, so they will only be corrected in subsequent posts. Make sure that you go through this thread thoroughly before slavishly copying anything that you see here!

This first version has had the user interface restructured somewhat, and also has had the ability to patch in an external mask added. There has also been some rewriting of the code to provide better cross-platform support. Other restructuring has been done to allow me to add matte blurring and erosion - yet to come!

So here's the user interface as it now stands.


This image is hidden for guests. Please log in or register to see it.


If you are familiar with baopao's original you will notice that the naming of some parameters has changed, as has the ordering. Additionally, there is provision to invert the mask and to show it, to help in the setting up of the mask.

I have set up a test key with a little colourgrading, and added an in-line DVE effect just to make things complicated. The mask has been generated by one of khaver's polymask effects. At the moment masks must be white on black - there is no support for the alpha channel of an external mask. The routing looks like this.


This image is hidden for guests. Please log in or register to see it.


I started with a foreground image of a hot air balloon shot at dawn over Melbourne, Australia. The sky was a rather desaturated cyan with some clouds.


This image is hidden for guests. Please log in or register to see it.


The background was a dusk shot of a somewhat smoggy Los Angeles.


This image is hidden for guests. Please log in or register to see it.


The foreground was resized and repositioned, and the red component of the foreground was pulled back a little using baopao's CC_RGBCMY colourgrade tool. The Polymask was set to Show Mask, Inverted. The result with mask displayed looked like this.


This image is hidden for guests. Please log in or register to see it.


And the final result looked like this.


This image is hidden for guests. Please log in or register to see it.


Feel free to try it, but as I indicated at the start, it is a work in progress. This version should be Linux, Mac and Windows compatible. Once I have it at the point at which I think that it's usable I'll post it more widely.
Attachments:
  • Attachment This attachment is hidden for guests. Please log in or register to see it.
Last Edit: 4 years, 9 months ago by jwrl.

Rearranging the effects library 4 years, 9 months ago #104219

  • jwrl
  • Moderator
    Pro User
  • OFFLINE
  • Moderator
  • Posts: 12887
  • 4 years, 9 months ago
Well, as you read the posts below you'll see that I had made design decisions based on entirely erroneous assumptions. I was tempted to completely remove this post, but decided to leave it because when taken in context with Great White and khaver's comments it's a good example of what not to do. Hopefully others may benefit from these really dumb mistakes.

This post will go through the code and why the decisions were made. If you're not interested in writing shader code, skip to the next section. This pattern of code/example, code/example will probably continue throughout this thread.

I will assume that anyone interested will have already downloaded the sample files that you will find in Lightworks > Effect Templates. These give breakdowns of the structure and programming of basic shader effects. I believe that Lightworks uses HLSL (wrong - see Great White's posts below) for effects in Windows and GLSL for Mac and Linux (wrong again). For further information on HLSL go the Microsoft website, and for GLSL check out the OpenGL website.

Also be aware that I am teaching myself as I go. While some time back I earned my living for ten years writing software, it was a long time ago. So while I attempt to be as error free as possible, there may be mistakes. What this will usually mean is that the technique that I use to reach a given goal may not be the most efficient even though it works.

So disclaimers out of the way, here goes. Although Lightworks uses HLSL or GLSL (wrong it actually uses Cg and D3D) depending on the platform, I suspect they also use a preprocessor to sort out compatibility issues between the two. This is a guess only. For that reason I have erred on the side of safety, and where I know there's a difference in syntax I write my own equivalent either as in-line code or as a separate function.

A good example is the fact the HLSL has an intrinsic function to perform a linear interpolation called lerp()... (wrong - the rest of this example was complete drivel and I've removed it completely)

It would be really nice if you could reliably determine the platform at compile time. You could then use #ifdef constructs to conditionally compile. The big advantage that this would give is where you need to write the shader to compile using a different profile to the default Lightworks one. For example, you could do this.

#ifdef WINDOWS
#define PROFILE  ps_3_0
#endif

   ...

technique demoTechnique
{
   pass Pass1
   {
      PixelShader = compile PROFILE main();
   }
}
Last Edit: 3 years, 6 months ago by jwrl.

Rearranging the effects library 4 years, 9 months ago #104243

  • jwrl
  • Moderator
    Pro User
  • OFFLINE
  • Moderator
  • Posts: 12887
  • 4 years, 9 months ago
And already it changes. I've now added cropping and more control over the spill correction. I'm still tossing up whether I want to have both spill corrections independently controlled. This is what the user interface now looks like.


This image is hidden for guests. Please log in or register to see it.


This is what you'll see with both crop and masks active and displayed. Note that the crop shows as darker than the mask for clarity.


This image is hidden for guests. Please log in or register to see it.


There will be more to follow when I can work out how to code an efficient key erode, but that plus a key soften will probably be as far as I want to take this. Although I could be persuaded to add inside key and outside key masking.
Attachments:
  • Attachment This attachment is hidden for guests. Please log in or register to see it.
Last Edit: 4 years, 9 months ago by jwrl.

Rearranging the effects library 4 years, 9 months ago #104244

  • donkpow
  • Pro User
  • OFFLINE
  • Platinum Boarder
  • Posts: 8993
  • 4 years, 9 months ago
"Superkeyer"

(He-he)
HP Z600 5570
PNY Quadro K620
SB Audigy SE
Windows 10 Pro
ShuttlePRO v2

Rearranging the effects library 4 years, 9 months ago #104245

  • jwrl
  • Moderator
    Pro User
  • OFFLINE
  • Moderator
  • Posts: 12887
  • 4 years, 9 months ago
Yes, that's what I started calling it. But of course there is a Superkey, so I need a different name. All suggestions welcome.

Rearranging the effects library 4 years, 9 months ago #104246

  • donkpow
  • Pro User
  • OFFLINE
  • Platinum Boarder
  • Posts: 8993
  • 4 years, 9 months ago
SuperKeyer is good.


This image is hidden for guests. Please log in or register to see it.

HP Z600 5570
PNY Quadro K620
SB Audigy SE
Windows 10 Pro
ShuttlePRO v2

Rearranging the effects library 4 years, 9 months ago #104247

  • jwrl
  • Moderator
    Pro User
  • OFFLINE
  • Moderator
  • Posts: 12887
  • 4 years, 9 months ago
I think that DC Comics might have something to say about that!

Rearranging the effects library 4 years, 9 months ago #104251

  • khaver
  • Pro User
  • OFFLINE
  • Platinum Boarder
  • Posts: 3510
  • 4 years, 9 months ago
jwrl wrote:
I believe that Lightworks uses HLSL for effects in Windows and GLSL for Mac and Linux.
I believe it was Great White that said that Lightworks uses nVidia's cross platform Cg language.
Work Comp: Retired!
Home Comp: Newer! Bigger! Faster!
Send me a tip: paypal.me/4khaver

Rearranging the effects library 4 years, 9 months ago #104264

  • jwrl
  • Moderator
    Pro User
  • OFFLINE
  • Moderator
  • Posts: 12887
  • 4 years, 9 months ago
Thanks. That makes life a great deal simpler! I probably won't get to do much with this before the weekend, but compliance with Cg will be the first thing that I check.

Rearranging the effects library 4 years, 9 months ago #104275

  • Great White
  • Moderator
    Pro User
  • OFFLINE
  • Moderator
  • Posts: 1792
  • 4 years, 9 months ago
Lightworks uses nVidia's cross platform Cg language

This is indeed correct. There's really no need to make any platform-specific considerations when you're writing a new pixel shader (other than to use 'PROFILE' in the technique definition(s)). For performance reasons it's always best to use the intrinsic functions directly (eg. lerp) and also good to avoid functions, loops and conditional logic where possible.
Lightworks Development

Rearranging the effects library 4 years, 9 months ago #104299

  • jwrl
  • Moderator
    Pro User
  • OFFLINE
  • Moderator
  • Posts: 12887
  • 4 years, 9 months ago
Thank you Great White - much appreciated. I routinely use PROFILE anyway, and I'm aware of the performance hit of conditionals and loops, although it's not always possible to avoid using them. It hadn't occurred to me that functions were also likely to affect things, although with hindsight it obviously should have.

So is there a similar hit when using multipass techniques? How do they compare to function calls?

Rearranging the effects library 4 years, 9 months ago #104314

  • Great White
  • Moderator
    Pro User
  • OFFLINE
  • Moderator
  • Posts: 1792
  • 4 years, 9 months ago
So is there a similar hit when using multipass techniques?

Two passes is twice as expensive as one pass, in terms of writes, so if you can fit something in one pass instead of two, it usually makes sense to do that. There are however algorithms which are faster using multiple passes because they allow you to save on reads - eg. a one-pass 10x10 box-blur requires 100 reads per pixel, whereas a two-pass box-blur can be achieved with 20 reads per pixel (2x10) using separable convolution (see here)
Most of Lightworks' shaders were written for pixel-shader 2.0, so had to be multipass in order to fit the 64 instruction limit.
Lightworks Development

Rearranging the effects library 4 years, 9 months ago #104345

  • jwrl
  • Moderator
    Pro User
  • OFFLINE
  • Moderator
  • Posts: 12887
  • 4 years, 9 months ago
Thanks again - very helpful. I've downloaded all the documentation on Cg that I can find, but I hadn't found GPU Gems. At a cursory glance Cg doesn't look too different to HLSL. I'll do some detailed study on the weekend.

I was aware of the ps_2_0 constraint, which I would prefer to stay inside for obvious reasons. For the times when ps_3_0 is almost mandatory does my #define PROFILE ps_3_0 technique (above) break any rules? I'm aware that older GPUs won't necessarily support ps_2_b and above, but is there any other reason that I shouldn't do this?

You've been very generous with your time, and I 'm very grateful, but I have two more questions. I notice that in Cg it appears to be possible to identify whether the shader is running in a Windows environment or not at compile time. Is this supported by the LW compiler?

And finally, is it possible in user created effects to add short explanatory texts? For example, under Lumakey there's "Key through the fg layer based on brightness". If that's possible how can I define it?
Last Edit: 4 years, 9 months ago by jwrl.

Rearranging the effects library 4 years, 9 months ago #104346

  • khaver
  • Pro User
  • OFFLINE
  • Platinum Boarder
  • Posts: 3510
  • 4 years, 9 months ago
To add the explanatory text you have to use Lightworks by right clicking on the effect in the effects panel and use the option to add/edit the description text. You can also add it by opening the Templates.Cache file while Lightworks is closed and add the text between the third set of quotes. There's no way to do it from the .fx file.
Work Comp: Retired!
Home Comp: Newer! Bigger! Faster!
Send me a tip: paypal.me/4khaver
Last Edit: 4 years, 9 months ago by khaver.

Rearranging the effects library 4 years, 9 months ago #104352

  • jwrl
  • Moderator
    Pro User
  • OFFLINE
  • Moderator
  • Posts: 12887
  • 4 years, 9 months ago
Thanks. I was aware of both those techniques. I was afraid that it wasn't possible inside the .fx file. It would be very handy if it was though!
Time to create page: 0.37 seconds
Scroll To Top