CommonFilters - some new filters, and the future

Success!

The code is now split into modules and it runs! :slight_smile:

It writes shaders that look like this:

//Cg
//
//Cg profile arbvp1 arbfp1

// FilterPipeline generated shader for render pass:
//   [LensFocus]
//
// Enabled filters (in this order):
//   StageInitializationFilter
//   BlurSharpenFilter

void vshader( float4 vtx_position : POSITION,
              out float4 l_position : POSITION,
              out float2 l_texcoord : TEXCOORD0,
              uniform float4x4 mat_modelproj )
{
    l_position = mul(mat_modelproj, vtx_position);
    l_texcoord = (vtx_position.xz * float2(0.5, 0.5)) + float2(0.5, 0.5);
}

// initialize pixcolor
float4 initializeFilterStage( uniform sampler2D k_txcolor,
                              float2 l_texcoord,
                              float4 pixcolor )
{
    pixcolor = tex2D(k_txcolor, l_texcoord.xy);
    return pixcolor;
}

// Blur/sharpen blend pass
float4 blurSharpenFilter( uniform sampler2D k_txblur1,
                          float2 l_texcoord,
                          uniform float k_blur_amount,
                          float4 pixcolor )
{
    pixcolor = lerp(tex2D(k_txblur1, l_texcoord.xy), pixcolor, k_blur_amount.x);
    return pixcolor;
}

void fshader( float2 l_texcoord : TEXCOORD0,
              uniform sampler2D k_txcolor,
              uniform sampler2D k_txblur1,
              uniform float k_blur_amount,
              out float4 o_color : COLOR )
{
    float4 pixcolor = float4(0.0, 0.0, 0.0, 0.0);
    
    // initialize pixcolor
    pixcolor = initializeFilterStage( k_txcolor,
                                      l_texcoord,
                                      pixcolor );

    // Blur/sharpen blend pass
    pixcolor = blurSharpenFilter( k_txblur1,
                                  l_texcoord,
                                  k_blur_amount,
                                  pixcolor );

    o_color = pixcolor;
}

The texcoord handler is based on the latest version in CVS, but it now handles texpad and texpix separately (to cover the case where HalfPixelShift is enabled for non-padded textures; in this case the vshader needs texpix but no texpad).

This source code was retrieved from the framework by:

for stage in mypipeline.stages:
    print stage.shaderSourceCode

In the Panda spirit, you can ls() the FilterPipeline to print a description:

FilterPipeline instance at 0x7f9c4a655f50: <active>, 1 render pass, 1 filter total
  Scene textures: ['color']
  Render pass 1/1:
    FilterStage instance '[LensFocus]' at 0x7f9c3a3cac50: <2 filters>
      Textures registered to compositing shader: ["blur1 (reg. by ['BlurSharpenFilter'])", "color (reg. by ['StageInitializationFilter'])"]
      Custom inputs registered to compositing shader: ["float k_blur_amount (reg. by ['BlurSharpenFilter'])"]
        StageInitializationFilter instance at 0x7f9c3a3cac90
            isMergeable: None
            sort: -1
            stageName: None
        BlurSharpenFilter instance at 0x7f9c3a3caad0; 2 internal render passes
          Internal textures: ['blur0', 'blur1']
            amount: 0.0
            isMergeable: False
            sort: 0
            stageName: LensFocus

(If it looks like the framework can’t count, rest assured it can - the discrepancy in the filter count is because StageInitializationFilter is not a proper filter in the pipeline, but something that is inserted internally at the beginning of each stage. Hence the pipeline sees only one filter, while the stage sees two.)

The legacy API is a drop-in replacement for CommonFilters - the calling code for this test was:

from CommonFilters190.CommonFilters import CommonFilters

self.filters = CommonFilters(base.win, base.cam)
filterok = self.filters.setBlurSharpen()

(The nonstandard path for the import is because these are experimental files that are not yet in the Panda tree. It will change to the usual “from direct.filter.CommonFilters import CommonFilters” once everything is done - so existing scripts shouldn’t even notice that anything has changed.)

Now, I only need to port all the existing filters to this framework, and then I can send it in for review :slight_smile:

Latest sources attached. There shouldn’t be any more upcoming major changes to the framework itself. What will change is that I’ll add more Filter modules and update the legacy API (CommonFilters) to support them.
CommonFilters190_initial_working_version.zip (112 KB)