Skip to content

Commit

Permalink
Automatic filter resolution and multisample (pixijs#9298)
Browse files Browse the repository at this point in the history
  • Loading branch information
dev7355608 authored Apr 7, 2023
1 parent f4b68ee commit 3ca76a2
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 46 deletions.
22 changes: 14 additions & 8 deletions packages/core/src/filters/Filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,15 @@ export class Filter extends Shader
* Default filter resolution for any filter.
* @static
*/
public static defaultResolution = 1;
public static defaultResolution: number | null = 1;

/**
* Default filter samples for any filter.
* @static
* @type {PIXI.MSAA_QUALITY}
* @type {PIXI.MSAA_QUALITY|null}
* @default PIXI.MSAA_QUALITY.NONE
*/
public static defaultMultisample = MSAA_QUALITY.NONE;
public static defaultMultisample: MSAA_QUALITY | null = MSAA_QUALITY.NONE;

/**
* The padding of the filter. Some filters require extra space to breath such as a blur.
Expand All @@ -208,8 +208,12 @@ export class Filter extends Shader
*/
public padding: number;

/** The samples override of the filter instance. */
public multisample: MSAA_QUALITY;
/**
* The samples override of the filter instance.
* If set to `null`, the sample count of the current render target is used.
* @default PIXI.Filter.defaultMultisample
*/
public multisample: MSAA_QUALITY | null;

/** If enabled is true the filter is applied, if false it will not. */
public enabled: boolean;
Expand All @@ -230,7 +234,7 @@ export class Filter extends Shader
/** The WebGL state the filter requires to render. */
state: State;

protected _resolution: number;
protected _resolution: number | null;

/**
* @param vertexSrc - The source of the vertex shader.
Expand Down Expand Up @@ -289,13 +293,15 @@ export class Filter extends Shader
/**
* The resolution of the filter. Setting this to be lower will lower the quality but
* increase the performance of the filter.
* If set to `null` or `0`, the resolution of the current render target is used.
* @default PIXI.Filter.defaultResolution
*/
get resolution(): number
get resolution(): number | null
{
return this._resolution;
}

set resolution(value: number)
set resolution(value: number | null)
{
this._resolution = value;
}
Expand Down
25 changes: 20 additions & 5 deletions packages/core/src/filters/FilterSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,25 @@ export class FilterSystem implements ISystem
const renderer = this.renderer;
const filterStack = this.defaultFilterStack;
const state = this.statePool.pop() || new FilterState();
const renderTextureSystem = this.renderer.renderTexture;
const renderTextureSystem = renderer.renderTexture;
let currentResolution: number;
let currentMultisample: MSAA_QUALITY;

let resolution = filters[0].resolution;
let multisample = filters[0].multisample;
if (renderTextureSystem.current)
{
const renderTexture = renderTextureSystem.current;

currentResolution = renderTexture.resolution;
currentMultisample = renderTexture.multisample;
}
else
{
currentResolution = renderer.resolution;
currentMultisample = renderer.multisample;
}

let resolution = filters[0].resolution || currentResolution;
let multisample = filters[0].multisample ?? currentMultisample;
let padding = filters[0].padding;
let autoFit = filters[0].autoFit;
// We don't know whether it's a legacy filter until it was bound for the first time,
Expand All @@ -166,9 +181,9 @@ export class FilterSystem implements ISystem
const filter = filters[i];

// let's use the lowest resolution
resolution = Math.min(resolution, filter.resolution);
resolution = Math.min(resolution, filter.resolution || currentResolution);
// let's use the lowest number of samples
multisample = Math.min(multisample, filter.multisample);
multisample = Math.min(multisample, filter.multisample ?? currentMultisample);
// figure out the padding required for filters
padding = this.useMaxPadding
// old behavior: use largest amount of padding!
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/mask/MaskData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ export class MaskData
* If set to `null` or `0`, the resolution of the current render target is used.
* @default null
*/
public resolution: number;
public resolution: number | null;

/**
* Number of samples of the sprite mask filter.
* If set to `null`, the sample count of the current render target is used.
* @default PIXI.Filter.defaultMultisample
*/
public multisample: MSAA_QUALITY;
public multisample: MSAA_QUALITY | null;

/** If enabled is true the mask is applied, if false it will not. */
public enabled: boolean;
Expand Down
25 changes: 3 additions & 22 deletions packages/core/src/mask/MaskSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,33 +253,14 @@ export class MaskSystem implements ISystem
}
}

const renderer = this.renderer;
const renderTextureSystem = renderer.renderTexture;

let resolution;
let multisample;

if (renderTextureSystem.current)
{
const renderTexture = renderTextureSystem.current;

resolution = maskData.resolution || renderTexture.resolution;
multisample = maskData.multisample ?? renderTexture.multisample;
}
else
{
resolution = maskData.resolution || renderer.resolution;
multisample = maskData.multisample ?? renderer.multisample;
}

alphaMaskFilter[0].resolution = resolution;
alphaMaskFilter[0].multisample = multisample;
alphaMaskFilter[0].resolution = maskData.resolution;
alphaMaskFilter[0].multisample = maskData.multisample;
alphaMaskFilter[0].maskSprite = maskObject;

const stashFilterArea = target.filterArea;

target.filterArea = maskObject.getBounds(true);
renderer.filter.push(target, alphaMaskFilter);
this.renderer.filter.push(target, alphaMaskFilter);
target.filterArea = stashFilterArea;

if (!maskData._filters)
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ Object.defineProperties(settings, {
* @name FILTER_RESOLUTION
* @memberof PIXI.settings
* @deprecated since 7.1.0
* @type {number}
* @type {number|null}
* @see PIXI.Filter.defaultResolution
*/
FILTER_RESOLUTION: {
Expand Down
2 changes: 1 addition & 1 deletion packages/filter-blur/src/BlurFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class BlurFilter extends Filter
/**
* @param strength - The strength of the blur filter.
* @param quality - The quality of the blur filter.
* @param [resolution=Filter.defaultResolution] - The resolution of the blur filter.
* @param {number|null} [resolution=PIXI.Filter.defaultResolution] - The resolution of the blur filter.
* @param kernelSize - The kernelSize of the blur filter.Options: 5, 7, 9, 11, 13, 15.
*/
constructor(strength = 8, quality = 4, resolution = Filter.defaultResolution, kernelSize = 5)
Expand Down
2 changes: 1 addition & 1 deletion packages/filter-blur/src/BlurFilterPass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class BlurFilterPass extends Filter
* @param horizontal - Do pass along the x-axis (`true`) or y-axis (`false`).
* @param strength - The strength of the blur filter.
* @param quality - The quality of the blur filter.
* @param resolution - The resolution of the blur filter.
* @param {number|null} [resolution=PIXI.Filter.defaultResolution] - The resolution of the blur filter.
* @param kernelSize - The kernelSize of the blur filter.Options: 5, 7, 9, 11, 13, 15.
*/
constructor(horizontal: boolean, strength = 8, quality = 4, resolution = Filter.defaultResolution, kernelSize = 5)
Expand Down
12 changes: 6 additions & 6 deletions packages/mixin-cache-as-bitmap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ Object.defineProperties(DisplayObject.prototype, {
* but can be overriden for performance. Lower values will reduce memory usage at the expense
* of render quality. A falsey value of `null` or `0` will default to the renderer's resolution.
* If `cacheAsBitmap` is set to `true`, this will re-render with the new resolution.
* @member {number} cacheAsBitmapResolution
* @member {number|null} cacheAsBitmapResolution
* @memberof PIXI.DisplayObject#
* @default null
*/
cacheAsBitmapResolution: {
get(): number
get(): number | null
{
return this._cacheAsBitmapResolution;
},
set(resolution: number): void
set(resolution: number | null): void
{
if (resolution === this._cacheAsBitmapResolution)
{
Expand All @@ -107,16 +107,16 @@ Object.defineProperties(DisplayObject.prototype, {
* The number of samples to use for cacheAsBitmap. If set to `null`, the renderer's
* sample count is used.
* If `cacheAsBitmap` is set to `true`, this will re-render with the new number of samples.
* @member {number} cacheAsBitmapMultisample
* @member {number|null} cacheAsBitmapMultisample
* @memberof PIXI.DisplayObject#
* @default null
*/
cacheAsBitmapMultisample: {
get(): MSAA_QUALITY
get(): MSAA_QUALITY | null
{
return this._cacheAsBitmapMultisample;
},
set(multisample: MSAA_QUALITY): void
set(multisample: MSAA_QUALITY | null): void
{
if (multisample === this._cacheAsBitmapMultisample)
{
Expand Down

0 comments on commit 3ca76a2

Please sign in to comment.