Skip to content

Commit

Permalink
Unattach textures that are no longer used and don't attach more than …
Browse files Browse the repository at this point in the history
…MAX_COLOR_ATTACHMENTS color textures
  • Loading branch information
dev7355608 committed Apr 4, 2023
1 parent ac3b202 commit c68529e
Showing 1 changed file with 29 additions and 36 deletions.
65 changes: 29 additions & 36 deletions packages/core/src/framebuffer/FramebufferSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export class FramebufferSystem implements ISystem
protected CONTEXT_UID: number;
protected gl: IRenderingContext;

private _maxColorAttachments: number;

/** Framebuffer value that shows that we don't know what is bound. */
protected unknownFramebuffer: Framebuffer;
protected msaaSamples: Array<number>;
Expand Down Expand Up @@ -78,13 +80,15 @@ export class FramebufferSystem implements ISystem

if (nativeDrawBuffersExtension)
{
gl.drawBuffers = (activeTextures: number[]): void =>
nativeDrawBuffersExtension.drawBuffersWEBGL(activeTextures);
this._maxColorAttachments = gl.getParameter(nativeDrawBuffersExtension.MAX_DRAW_BUFFERS_WEBGL);
gl.drawBuffers = (buffers: Iterable<GLenum>): void =>
nativeDrawBuffersExtension.drawBuffersWEBGL(buffers);
}
else
{
this.hasMRT = false;
gl.drawBuffers = (): void =>
this._maxColorAttachments = 1;
gl.drawBuffers = (_buffers: Iterable<GLenum>): void =>
{
// empty
};
Expand All @@ -98,6 +102,7 @@ export class FramebufferSystem implements ISystem
else
{
// WebGL2
this._maxColorAttachments = gl.getParameter(gl.MAX_COLOR_ATTACHMENTS);
// cache possible MSAA samples
this.msaaSamples = gl.getInternalformatParameter(gl.RENDERBUFFER, gl.RGBA8, gl.SAMPLES);
}
Expand Down Expand Up @@ -311,13 +316,7 @@ export class FramebufferSystem implements ISystem
}

const colorTextures = framebuffer.colorTextures;

let count = colorTextures.length;

if (!gl.drawBuffers)
{
count = Math.min(count, 1);
}
const count = Math.min(colorTextures.length, this._maxColorAttachments);

for (let i = 0; i < count; i++)
{
Expand Down Expand Up @@ -354,13 +353,7 @@ export class FramebufferSystem implements ISystem

// bind the color texture
const colorTextures = framebuffer.colorTextures;

let count = colorTextures.length;

if (!gl.drawBuffers)
{
count = Math.min(count, 1);
}
const count = Math.min(colorTextures.length, this._maxColorAttachments);

if (fbo.multisample > 1 && this.canMultisampleFramebuffer(framebuffer))
{
Expand All @@ -378,7 +371,7 @@ export class FramebufferSystem implements ISystem
}
}

const activeTextures = [];
const drawBuffers: number[] = [];

for (let i = 0; i < count; i++)
{
Expand All @@ -401,32 +394,32 @@ export class FramebufferSystem implements ISystem
texture.target,
parentTexture._glTextures[this.CONTEXT_UID].texture,
mipLevel);

activeTextures.push(gl.COLOR_ATTACHMENT0 + i);
}
}

if (activeTextures.length > 1)
drawBuffers.push(gl.COLOR_ATTACHMENT0 + i);
}
for (let i = count; i < this._maxColorAttachments; i++)
{
gl.drawBuffers(activeTextures);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + i, gl.TEXTURE_2D, null, 0);
}

if (framebuffer.depthTexture)
{
const writeDepthTexture = this.writeDepthTexture;
gl.drawBuffers(drawBuffers);

if (writeDepthTexture)
{
const depthTexture = framebuffer.depthTexture;
if (framebuffer.depthTexture && this.writeDepthTexture)
{
const depthTexture = framebuffer.depthTexture;

this.renderer.texture.bind(depthTexture, 0);
this.renderer.texture.bind(depthTexture, 0);

gl.framebufferTexture2D(gl.FRAMEBUFFER,
gl.DEPTH_ATTACHMENT,
gl.TEXTURE_2D,
depthTexture._glTextures[this.CONTEXT_UID].texture,
mipLevel);
}
gl.framebufferTexture2D(gl.FRAMEBUFFER,
gl.DEPTH_ATTACHMENT,
gl.TEXTURE_2D,
depthTexture._glTextures[this.CONTEXT_UID].texture,
mipLevel);
}
else
{
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, null, 0);
}

if ((framebuffer.stencil || framebuffer.depth) && !(framebuffer.depthTexture && this.writeDepthTexture))
Expand Down

0 comments on commit c68529e

Please sign in to comment.