Skip to content

Commit

Permalink
Add RenderTarget|null typing + remove dead code (#5704)
Browse files Browse the repository at this point in the history
* Add RenderTarget|null typing + remove dead code

* quad-render-utils: simplify code a bit more

* quad-render-utils: collapse the two nested if's into one

* add |undefined to @type based on Martins feedback

* RenderPass#renderTarget: add documentation

* init(renderTarget = null)
  • Loading branch information
kungfooman authored Oct 4, 2023
1 parent bbb7961 commit 640f26a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/framework/components/camera/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ class CameraComponent extends Component {
/**
* Calculates aspect ratio value for a given render target.
*
* @param {import('../../../platform/graphics/render-target.js').RenderTarget} [rt] - Optional
* @param {import('../../../platform/graphics/render-target.js').RenderTarget|null} [rt] - Optional
* render target. If unspecified, the backbuffer is used.
* @returns {number} The aspect ratio of the render target (or backbuffer).
*/
Expand All @@ -905,7 +905,7 @@ class CameraComponent extends Component {
/**
* Prepare the camera for frame rendering.
*
* @param {import('../../../platform/graphics/render-target.js').RenderTarget} rt - Render
* @param {import('../../../platform/graphics/render-target.js').RenderTarget|null} rt - Render
* target to which rendering will be performed. Will affect camera's aspect ratio, if
* aspectRatioMode is {@link ASPECT_AUTO}.
* @ignore
Expand Down
14 changes: 10 additions & 4 deletions src/platform/graphics/render-pass.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,13 @@ class RenderPass {
/** @type {string} */
name;

/** @type {import('../graphics/render-target.js').RenderTarget} */
/**
* The render target for this render pass:
* - `undefined`: render pass does not render to any render target
* - `null`: render pass renders to the backbuffer
* - Otherwise, renders to the provided RT.
* @type {import('../graphics/render-target.js').RenderTarget|null|undefined}
*/
renderTarget;

/**
Expand Down Expand Up @@ -186,15 +192,15 @@ class RenderPass {
}

/**
* @param {import('../graphics/render-target.js').RenderTarget} renderTarget - The render
* @param {import('../graphics/render-target.js').RenderTarget|null} [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) {
init(renderTarget = null) {

// null represents the default framebuffer
this.renderTarget = renderTarget || null;
this.renderTarget = renderTarget;

// defaults depend on multisampling
this.samples = Math.max(this.renderTarget ? this.renderTarget.samples : this.device.samples, 1);
Expand Down
8 changes: 4 additions & 4 deletions src/scene/graphics/post-effect.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ class PostEffect {
/**
* Draw a screen-space rectangle in a render target, using a specified shader.
*
* @param {import('../../platform/graphics/render-target.js').RenderTarget} target - The output
* render target.
* @param {import('../../platform/graphics/shader.js').Shader} shader - The shader to be used for
* drawing the rectangle.
* @param {import('../../platform/graphics/render-target.js').RenderTarget|null} target - The
* output render target.
* @param {import('../../platform/graphics/shader.js').Shader} shader - The shader to be used
* for drawing the rectangle.
* @param {import('../../core/math/vec4.js').Vec4} [rect] - The normalized screen-space position
* (rect.x, rect.y) and size (rect.z, rect.w) of the rectangle. Default is [0, 0, 1, 1].
*/
Expand Down
8 changes: 3 additions & 5 deletions src/scene/graphics/quad-render-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const _tempRect = new Vec4();
*
* @param {import('../../platform/graphics/graphics-device.js').GraphicsDevice} device - The graphics device used to draw
* the quad.
* @param {import('../../platform/graphics/render-target.js').RenderTarget|undefined} target - The destination render
* @param {import('../../platform/graphics/render-target.js').RenderTarget|null} target - The destination render
* target. If undefined, target is the frame buffer.
* @param {import('../../platform/graphics/shader.js').Shader} shader - The shader used for rendering the quad. Vertex
* shader should contain `attribute vec2 vertex_position`.
Expand Down Expand Up @@ -67,10 +67,8 @@ function drawQuadWithShader(device, target, shader, rect, scissorRect) {
// in a separate render pass B (e.g. rendering UI). Those two render passes need to be merged into one, as they both render into
// the same framebuffer. The workaround here is to store multi-sampled color buffer, instead of only resolving it, which is wasted
// memory bandwidth. Without this we end up with a black result (or just UI), as multi-sampled color buffer is never written to.
if (device.isWebGPU && target === null) {
const samples = target?.samples ?? device.samples;
if (samples > 1)
renderPass.colorOps.store = true;
if (device.isWebGPU && target === null && device.samples > 1) {
renderPass.colorOps.store = true;
}

renderPass.render();
Expand Down

0 comments on commit 640f26a

Please sign in to comment.