Skip to content

Commit

Permalink
WebGPURenderer: enable shadow rendering in WebGLBackend (mrdoob#27023)
Browse files Browse the repository at this point in the history
* make previous render state available to backend

* make shadow coord adjustment conditional

* add screenshot and enable test

* rework test

* rework as suggested

---------

Co-authored-by: aardgoose <angus.sawyer@email.com>
  • Loading branch information
aardgoose and aardgoose committed Oct 23, 2023
1 parent 7d4628d commit e070b99
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
24 changes: 19 additions & 5 deletions examples/jsm/nodes/lighting/AnalyticLightNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { reference } from '../accessors/ReferenceNode.js';
import { texture } from '../accessors/TextureNode.js';
import { positionWorld } from '../accessors/PositionNode.js';
import { normalWorld } from '../accessors/NormalNode.js';
import { WebGPUCoordinateSystem } from 'three';
//import { add } from '../math/OperatorNode.js';

import { Color, DepthTexture, NearestFilter, LessCompare } from 'three';
Expand Down Expand Up @@ -73,11 +74,24 @@ class AnalyticLightNode extends LightingNode {
.and( shadowCoord.y.lessThanEqual( 1 ) )
.and( shadowCoord.z.lessThanEqual( 1 ) );

shadowCoord = vec3(
shadowCoord.x,
shadowCoord.y.oneMinus(), // WebGPU: Flip Y
shadowCoord.z.add( bias ).mul( 2 ).sub( 1 ) // WebGPU: Convertion [ 0, 1 ] to [ - 1, 1 ]
);

if ( builder.renderer.coordinateSystem === WebGPUCoordinateSystem ) {

shadowCoord = vec3(
shadowCoord.x,
shadowCoord.y.oneMinus(), // WebGPU: Flip Y
shadowCoord.z.add( bias ).mul( 2 ).sub( 1 ) // WebGPU: Convertion [ 0, 1 ] to [ - 1, 1 ]
);

} else {

shadowCoord = vec3(
shadowCoord.x,
shadowCoord.y,
shadowCoord.z.add( bias )
);

}

const textureCompare = ( depthTexture, shadowCoord, compare ) => texture( depthTexture, shadowCoord ).compare( compare );
//const textureCompare = ( depthTexture, shadowCoord, compare ) => compare.step( texture( depthTexture, shadowCoord ) );
Expand Down
30 changes: 28 additions & 2 deletions examples/jsm/renderers/webgl/WebGLBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class WebGLBackend extends Backend {
this.defaultTextures = {};

this.extensions.get( 'EXT_color_buffer_float' );
this._currentContext = null;

}

Expand All @@ -53,9 +54,13 @@ class WebGLBackend extends Backend {
beginRender( renderContext ) {

const { gl } = this;
const renderContextData = this.get( renderContext );

//

renderContextData.previousContext = this._currentContext;
this._currentContext = renderContext;

this._setFramebuffer( renderContext );

let clear = 0;
Expand Down Expand Up @@ -103,8 +108,6 @@ class WebGLBackend extends Backend {

if ( occlusionQueryCount > 0 ) {

const renderContextData = this.get( renderContext );

// Get a reference to the array of objects with queries. The renderContextData property
// can be changed by another render pass before the async reading of all previous queries complete
renderContextData.currentOcclusionQueries = renderContextData.occlusionQueries;
Expand All @@ -121,6 +124,29 @@ class WebGLBackend extends Backend {

finishRender( renderContext ) {

const renderContextData = this.get( renderContext );
const previousContext = renderContextData.previousContext;

this._currentContext = previousContext;

if ( previousContext !== null ) {

this._setFramebuffer( previousContext );

if ( previousContext.viewport ) {

this.updateViewport( previousContext );

} else {

const gl = this.gl;

gl.viewport( 0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight );

}

}

const occlusionQueryCount = renderContext.occlusionQueryCount;

if ( occlusionQueryCount > 0 ) {
Expand Down
Binary file modified examples/screenshots/webgpu_shadowmap.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 4 additions & 2 deletions examples/webgpu_shadowmap.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import * as THREE from 'three';

import WebGPU from 'three/addons/capabilities/WebGPU.js';
import WebGL from 'three/addons/capabilities/WebGL.js';

import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';

import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
Expand All @@ -38,11 +40,11 @@

function init() {

if ( WebGPU.isAvailable() === false ) {
if ( WebGPU.isAvailable() === false && WebGL.isWebGL2Available() === false ) {

document.body.appendChild( WebGPU.getErrorMessage() );

throw new Error( 'No WebGPU support' );
throw new Error( 'No WebGPU or WebGL2 support' );

}

Expand Down
1 change: 0 additions & 1 deletion test/e2e/puppeteer.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ const exceptionList = [
'webgpu_materials_video',
'webgpu_sandbox',
'webgpu_shadertoy',
'webgpu_shadowmap',
'webgpu_sprites',
'webgpu_tsl_editor',
'webgpu_tsl_transpiler',
Expand Down

0 comments on commit e070b99

Please sign in to comment.