Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RenderTarget|null typing + remove dead code #5704

Merged
merged 6 commits into from
Oct 4, 2023

Conversation

kungfooman
Copy link
Collaborator

This fixes a few of type issues, since RenderTarget is null in many situations, and then it goes on and on to e.g. drawQuadWithShader which doesn't need a target, e.g. example with bloom:

image

I confirm I have read the contributing guidelines and signed the Contributor License Agreement.

@kungfooman
Copy link
Collaborator Author

We have in some functions strict "no-undefined-allowed" assertions:

function drawQuadWithShader(device, target, shader, rect, scissorRect) {
    // a valid target or a null target (framebuffer) are supported
    Debug.assert(target !== undefined);

But then we still have places where undefined creeps in again:

image

Should we try to keep it RenderTarget|null everywhere? From a clean type perspective, it makes most sense to me here 🤔

@slimbuck
Copy link
Member

slimbuck commented Oct 3, 2023

Should we try to keep it RenderTarget|null everywhere? From a clean type perspective, it makes most sense to me here 🤔

Probably a question for @mvaligursky

@mvaligursky
Copy link
Contributor

Should we try to keep it RenderTarget|null everywhere? From a clean type perspective, it makes most sense to me here

undefined is valid in some cases, so we cannot force it to null in those cases.

@kungfooman kungfooman marked this pull request as draft October 4, 2023 12:59
@kungfooman
Copy link
Collaborator Author

undefined is valid in some cases, so we cannot force it to null in those cases.

Interesting, so I tried to find the these methods called with undefined as renderTarget in a running app (UI ScrollView example):

I found these two:

/**
* @param {import('../graphics/render-target.js').RenderTarget} renderTarget - The render
* target to render into (output). This function should be called only for render passes which
* use render target, or passes which render directly into the default framebuffer, in which
* case a null or undefined render target is expected.
*/
init(renderTarget) {
// null represents the default framebuffer
this.renderTarget = renderTarget || null;
// defaults depend on multisampling
this.samples = Math.max(this.renderTarget ? this.renderTarget.samples : this.device.samples, 1);
// allocate ops only when render target is used
this.depthStencilOps = new DepthStencilAttachmentOps();
const numColorOps = renderTarget ? renderTarget._colorBuffers?.length : 1;
for (let i = 0; i < numColorOps; i++) {
const colorOps = new ColorAttachmentOps();
this.colorArrayOps[i] = colorOps;
// if rendering to single-sampled buffer, this buffer needs to be stored
if (this.samples === 1) {
colorOps.store = true;
colorOps.resolve = false;
}
// if render target needs mipmaps
if (this.renderTarget?._colorBuffers?.[i].mipmaps) {
colorOps.mipmaps = true;
}
}
}

/**
* Prepare the camera for frame rendering.
*
* @param {import('../../../platform/graphics/render-target.js').RenderTarget} rt - Render
* target to which rendering will be performed. Will affect camera's aspect ratio, if
* aspectRatioMode is {@link ASPECT_AUTO}.
* @ignore
*/
frameUpdate(rt) {
if (this.aspectRatioMode === ASPECT_AUTO) {
this.aspectRatio = this.calculateAspectRatio(rt);
}
}

If these are called correctly, I can just mark them as [renderTarget] (meaning |undefined).

I feel uncertain here though, in first code snippet, line 197:

this.renderTarget = renderTarget || null; 

So it could be called with undefined but will be immediately updated to null in case of undefined? Would be really nice to get the types right here, because they cause type invalidations in RuntimeTypeInspector.js every frame:

image

@mvaligursky
Copy link
Contributor

So the RendePass.renderTarget is undefined if nothing is set to it, so that is the default uninitialized state.
if RenderPass.init is called, it can only update that member to a valid rt, or NULL (backbuffer), but never to uninitialized.

so only renderTarget needs to have all 3 types allowed, but init function only needs two.

@kungfooman
Copy link
Collaborator Author

if RenderPass.init is called, it can only update that member to a valid rt, or NULL (backbuffer), but never to uninitialized.

so only renderTarget needs to have all 3 types allowed, but init function only needs two.

Hm, we still call RenderPass#init with undefined aswell, shouldn't we then just do init(renderTarget = null) so the type system picks up that calling init with undefined is allowed?

image

(this happens in ForwardRenderer#addMainRenderPass)

@mvaligursky
Copy link
Contributor

init(renderTarget = null)

perfect! Go for it.

@kungfooman kungfooman marked this pull request as ready for review October 4, 2023 14:49
Copy link
Contributor

@mvaligursky mvaligursky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one, thanks!

@mvaligursky mvaligursky merged commit 640f26a into playcanvas:main Oct 4, 2023
7 checks passed
@kungfooman kungfooman deleted the jsdoc-render-target branch October 4, 2023 15:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants