Skip to content

Commit

Permalink
- added a cleanupRenderMaterialParameters method
Browse files Browse the repository at this point in the history
- copy shader pass output to render texture only if needed
  • Loading branch information
martinlaxenaire committed Feb 21, 2024
1 parent 8e39c82 commit 190408d
Show file tree
Hide file tree
Showing 333 changed files with 3,877 additions and 3,604 deletions.
73 changes: 42 additions & 31 deletions dist/esm/core/meshes/mixins/MeshBaseMixin.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ function MeshBaseMixin(Base) {
texturesOptions,
...outputTarget !== void 0 && { outputTarget },
...autoRender !== void 0 && { autoRender },
...meshParameters.useAsyncPipeline !== void 0 && { useAsyncPipeline: meshParameters.useAsyncPipeline }
...meshParameters
};
this.geometry = geometry;
if (autoRender !== void 0) {
Expand All @@ -125,9 +125,9 @@ function MeshBaseMixin(Base) {
this.userData = {};
this.computeGeometry();
this.setMaterial({
label: this.options.label,
shaders: this.options.shaders,
...{ ...meshParameters, verticesOrder: geometry.verticesOrder, topology: geometry.topology }
...this.cleanupRenderMaterialParameters({ ...this.options }),
verticesOrder: geometry.verticesOrder,
topology: geometry.topology
});
this.addToScene();
}
Expand Down Expand Up @@ -171,33 +171,6 @@ function MeshBaseMixin(Base) {
}
this.renderer.meshes = this.renderer.meshes.filter((m) => m.uuid !== this.uuid);
}
/**
* Set or update the {@link RenderMaterial} {@link types/Materials.RenderMaterialRenderingOptions | rendering options} to match the {@link RenderPass#descriptor | RenderPass descriptor} used to draw this Mesh.
* @param renderPass - {@link RenderPass | RenderPass} used to draw this Mesh, default to the {@link core/renderers/GPURenderer.GPURenderer#renderPass | renderer renderPass}.
*/
setRenderingOptionsForRenderPass(renderPass) {
const renderingOptions = {
sampleCount: renderPass.options.sampleCount,
// color attachments
...renderPass.options.colorAttachments.length && {
targetFormat: renderPass.options.colorAttachments[0].targetFormat,
// multiple render targets?
...renderPass.options.colorAttachments.length > 1 && {
additionalTargets: renderPass.options.colorAttachments.filter((c, i) => i > 0).map((colorAttachment) => {
return {
format: colorAttachment.targetFormat
};
})
}
},
// depth
depth: renderPass.options.useDepth,
...renderPass.options.useDepth && {
depthFormat: renderPass.options.depthFormat
}
};
this.material?.setRenderingOptions(renderingOptions);
}
/**
* Set a new {@link Renderer} for this Mesh
* @param renderer - new {@link Renderer} to set
Expand Down Expand Up @@ -332,6 +305,44 @@ function MeshBaseMixin(Base) {
}
}
/* MATERIAL */
/**
* Set or update the {@link RenderMaterial} {@link types/Materials.RenderMaterialRenderingOptions | rendering options} to match the {@link RenderPass#descriptor | RenderPass descriptor} used to draw this Mesh.
* @param renderPass - {@link RenderPass | RenderPass} used to draw this Mesh, default to the {@link core/renderers/GPURenderer.GPURenderer#renderPass | renderer renderPass}.
*/
setRenderingOptionsForRenderPass(renderPass) {
const renderingOptions = {
sampleCount: renderPass.options.sampleCount,
// color attachments
...renderPass.options.colorAttachments.length && {
targetFormat: renderPass.options.colorAttachments[0].targetFormat,
// multiple render targets?
...renderPass.options.colorAttachments.length > 1 && {
additionalTargets: renderPass.options.colorAttachments.filter((c, i) => i > 0).map((colorAttachment) => {
return {
format: colorAttachment.targetFormat
};
})
}
},
// depth
depth: renderPass.options.useDepth,
...renderPass.options.useDepth && {
depthFormat: renderPass.options.depthFormat
}
};
this.material?.setRenderingOptions(renderingOptions);
}
/**
* Hook used to clean up parameters before sending them to the {@link RenderMaterial}.
* @param parameters - parameters to clean before sending them to the {@link RenderMaterial}
* @returns - cleaned parameters
*/
cleanupRenderMaterialParameters(parameters) {
delete parameters.texturesOptions;
delete parameters.outputTarget;
delete parameters.autoRender;
return parameters;
}
/**
* Set a Mesh transparent property, then set its material
* @param meshParameters - {@link RenderMaterialParams | RenderMaterial parameters}
Expand Down
2 changes: 1 addition & 1 deletion dist/esm/core/meshes/mixins/MeshBaseMixin.mjs.map

Large diffs are not rendered by default.

22 changes: 16 additions & 6 deletions dist/esm/core/meshes/mixins/ProjectedMeshBaseMixin.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,22 @@ function ProjectedMeshBaseMixin(Base) {
this.domFrustum.shouldUpdate = this.frustumCulled;
}
/* MATERIAL */
/**
* Hook used to clean up parameters before sending them to the material.
* @param parameters - parameters to clean before sending them to the {@link core/materials/RenderMaterial.RenderMaterial | RenderMaterial}
* @returns - cleaned parameters
*/
cleanupRenderMaterialParameters(parameters) {
delete parameters.frustumCulled;
delete parameters.DOMFrustumMargins;
super.cleanupRenderMaterialParameters(parameters);
return parameters;
}
/**
* Set a Mesh matrices uniforms inputs then call {@link MeshBaseClass} super method
* @param meshParameters - {@link ProjectedRenderMaterialParams | RenderMaterial parameters}
* @param meshParameters - {@link RenderMaterialParams | RenderMaterial parameters}
*/
setMaterial(meshParameters) {
const { frustumCulled, DOMFrustumMargins, ...materialParameters } = meshParameters;
const matricesUniforms = {
label: "Matrices",
struct: {
Expand All @@ -147,10 +157,10 @@ function ProjectedMeshBaseMixin(Base) {
}
}
};
if (!materialParameters.uniforms)
materialParameters.uniforms = {};
materialParameters.uniforms.matrices = matricesUniforms;
super.setMaterial(materialParameters);
if (!meshParameters.uniforms)
meshParameters.uniforms = {};
meshParameters.uniforms.matrices = matricesUniforms;
super.setMaterial(meshParameters);
}
/* SIZE & TRANSFORMS */
/**
Expand Down
2 changes: 1 addition & 1 deletion dist/esm/core/meshes/mixins/ProjectedMeshBaseMixin.mjs.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/esm/core/renderPasses/RenderPass.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class RenderPass {
this.createDepthTexture();
}
this.viewTextures = [];
if (this.options.useColorAttachments) {
if (this.options.useColorAttachments && (!this.options.shouldUpdateView || this.options.sampleCount > 1)) {
this.createViewTextures();
}
this.setRenderPassDescriptor();
Expand Down Expand Up @@ -121,8 +121,8 @@ class RenderPass {
colorAttachments: this.options.colorAttachments.map((colorAttachment, index) => {
return {
// view
view: this.viewTextures[index].texture.createView({
label: this.viewTextures[index].texture.label + " view"
view: this.viewTextures[index]?.texture.createView({
label: this.viewTextures[index]?.texture.label + " view"
}),
// clear values
clearValue: colorAttachment.clearValue,
Expand Down
2 changes: 1 addition & 1 deletion dist/esm/core/renderPasses/RenderPass.mjs.map

Large diffs are not rendered by default.

17 changes: 14 additions & 3 deletions dist/esm/core/renderPasses/ShaderPass.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,26 @@ class ShaderPass extends FullscreenPlane {
});
}
/**
* Get our main {@link RenderTexture}, the one that contains our post processed content
* Hook used to clean up parameters before sending them to the material.
* @param parameters - parameters to clean before sending them to the {@link core/materials/RenderMaterial.RenderMaterial | RenderMaterial}
* @returns - cleaned parameters
*/
cleanupRenderMaterialParameters(parameters) {
delete parameters.copyOutputToRenderTexture;
delete parameters.inputTarget;
super.cleanupRenderMaterialParameters(parameters);
return parameters;
}
/**
* Get our main {@link RenderTexture} that contains the input content to be used by the {@link ShaderPass}. Can also contain the ouputted content if {@link ShaderPassOptions#copyOutputToRenderTexture | copyOutputToRenderTexture} is set to true.
* @readonly
*/
get renderTexture() {
return this.renderTextures.find((texture) => texture.options.name === "renderTexture");
}
// TODO
/**
* Assign or remove a {@link RenderTarget} to this {@link ShaderPass}
* Assign or remove an input {@link RenderTarget} to this {@link ShaderPass}, which can be different from what has just been drawn to the {@link core/renderers/GPURenderer.GPURenderer#context | context} current texture.
*
* Since this manipulates the {@link core/scenes/Scene.Scene | Scene} stacks, it can be used to remove a RenderTarget as well.
* Also copy or remove the {@link RenderTarget#renderTexture | render target render texture} into the {@link ShaderPass} {@link renderTexture}
* @param inputTarget - the {@link RenderTarget} to assign or null if we want to remove the current {@link RenderTarget}
Expand Down
2 changes: 1 addition & 1 deletion dist/esm/core/renderPasses/ShaderPass.mjs.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/esm/core/scenes/Scene.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class Scene {
}
this.renderer.postProcessingPass.setLoadOp("clear");
};
const onAfterRenderPass = shaderPass.outputTarget ? null : (commandEncoder, swapChainTexture) => {
const onAfterRenderPass = !shaderPass.outputTarget && shaderPass.options.copyOutputToRenderTexture ? (commandEncoder, swapChainTexture) => {
if (shaderPass.renderTexture && swapChainTexture) {
commandEncoder.copyTextureToTexture(
{
Expand All @@ -195,7 +195,7 @@ class Scene {
[shaderPass.renderTexture.size.width, shaderPass.renderTexture.size.height]
);
}
};
} : null;
const shaderPassEntry = {
// use output target or postprocessing render pass
renderPass: shaderPass.outputTarget ? shaderPass.outputTarget.renderPass : this.renderer.postProcessingPass,
Expand Down
2 changes: 1 addition & 1 deletion dist/esm/core/scenes/Scene.mjs.map

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions dist/esm/utils/debug.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ const logSceneCommands = (renderer) => {
let descriptor = renderPassEntry.renderPass.options.label;
const operations = {
loadOp: renderPassEntry.renderPass.options.useColorAttachments ? renderPassEntryType === "screen" && passDrawnCount > 0 ? "load" : renderPassEntry.renderPass.options.loadOp : void 0,
depthLoadOp: void 0
depthLoadOp: void 0,
sampleCount: renderPassEntry.renderPass.options.sampleCount
};
if (renderPassEntry.renderPass.options.useDepth) {
operations.depthLoadOp = renderPassEntry.renderPass.options.depthLoadOp;
}
passDrawnCount++;
if (renderPassEntry.element) {
if (renderPassEntry.element.type === "ShaderPass" && !renderPassEntry.element.renderTarget) {
if (renderPassEntry.element.type === "ShaderPass" && !(renderPassEntry.element.inputTarget || renderPassEntry.element.outputTarget)) {
renderCommands.push({
command: `Copy texture to texture`,
source: destination,
Expand All @@ -51,11 +52,11 @@ const logSceneCommands = (renderer) => {
destination,
descriptor
});
if (renderPassEntry.element.type === "ShaderPass" && renderPassEntry.element.renderTarget) {
if (renderPassEntry.element.type === "ShaderPass" && !renderPassEntry.element.outputTarget && renderPassEntry.element.options.copyOutputToRenderTexture) {
renderCommands.push({
command: `Copy texture to texture`,
source: destination,
destination: `${renderPassEntry.element.renderTarget.options.label} renderTexture`
destination: `${renderPassEntry.element.options.label} renderTexture`
});
} else if (renderPassEntry.element.type === "PingPongPlane") {
renderCommands.push({
Expand Down
2 changes: 1 addition & 1 deletion dist/esm/utils/debug.mjs.map

Large diffs are not rendered by default.

Loading

0 comments on commit 190408d

Please sign in to comment.