Skip to content

Commit

Permalink
WebGPURenderer: Fix DataTexture - FloatType (#26585)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunag committed Aug 15, 2023
1 parent ed8a785 commit c849b5d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 25 deletions.
56 changes: 33 additions & 23 deletions examples/jsm/renderers/webgpu/nodes/WGSLNodeBuilder.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RenderTarget, NoColorSpace } from 'three';
import { RenderTarget, NoColorSpace, FloatType } from 'three';

import UniformsGroup from '../../common/UniformsGroup.js';
import {
Expand Down Expand Up @@ -136,25 +136,21 @@ class WGSLNodeBuilder extends NodeBuilder {

}

getSampler( textureProperty, uvSnippet, shaderStage = this.shaderStage ) {
_getSampler( texture, textureProperty, uvSnippet, shaderStage = this.shaderStage ) {

if ( shaderStage === 'fragment' ) {

return `textureSample( ${textureProperty}, ${textureProperty}_sampler, ${uvSnippet} )`;

} else {

this._include( 'repeatWrapping' );

const dimension = `textureDimensions( ${textureProperty}, 0 )`;

return `textureLoad( ${textureProperty}, threejs_repeatWrapping( ${uvSnippet}, ${dimension} ), 0 )`;
return this.getTextureLoad( texture, textureProperty, uvSnippet );

}

}

getVideoSampler( textureProperty, uvSnippet, shaderStage = this.shaderStage ) {
_getVideoSampler( textureProperty, uvSnippet, shaderStage = this.shaderStage ) {

if ( shaderStage === 'fragment' ) {

Expand All @@ -168,21 +164,33 @@ class WGSLNodeBuilder extends NodeBuilder {

}

getSamplerLevel( textureProperty, uvSnippet, biasSnippet, shaderStage = this.shaderStage ) {
_getSamplerLevel( texture, textureProperty, uvSnippet, biasSnippet, shaderStage = this.shaderStage ) {

if ( shaderStage === 'fragment' ) {
if ( shaderStage === 'fragment' && this.isUnfilterable( texture ) === false ) {

return `textureSampleLevel( ${textureProperty}, ${textureProperty}_sampler, ${uvSnippet}, ${biasSnippet} )`;

} else {

this._include( 'repeatWrapping' );
return this.getTextureLoad( texture, textureProperty, uvSnippet, biasSnippet );

const dimension = `textureDimensions( ${textureProperty}, 0 )`;
}

return `textureLoad( ${textureProperty}, threejs_repeatWrapping( ${uvSnippet}, ${dimension} ), i32( ${biasSnippet} ) )`;
}

}
getTextureLoad( texture, textureProperty, uvSnippet, biasSnippet = '0' ) {

this._include( 'repeatWrapping' );

const dimension = `textureDimensions( ${textureProperty}, 0 )`;

return `textureLoad( ${textureProperty}, threejs_repeatWrapping( ${uvSnippet}, ${dimension} ), i32( ${biasSnippet} ) )`;

}

isUnfilterable( texture ) {

return texture.isDataTexture === true && texture.type === FloatType;

}

Expand All @@ -192,11 +200,15 @@ class WGSLNodeBuilder extends NodeBuilder {

if ( texture.isVideoTexture === true ) {

snippet = this.getVideoSampler( textureProperty, uvSnippet, shaderStage );
snippet = this._getVideoSampler( textureProperty, uvSnippet, shaderStage );

} else if ( this.isUnfilterable( texture ) ) {

snippet = this.getTextureLoad( texture, textureProperty, uvSnippet );

} else {

snippet = this.getSampler( textureProperty, uvSnippet, shaderStage );
snippet = this._getSampler( texture, textureProperty, uvSnippet, shaderStage );

}

Expand Down Expand Up @@ -224,11 +236,11 @@ class WGSLNodeBuilder extends NodeBuilder {

if ( texture.isVideoTexture === true ) {

snippet = this.getVideoSampler( textureProperty, uvSnippet, shaderStage );
snippet = this._getVideoSampler( textureProperty, uvSnippet, shaderStage );

} else {

snippet = this.getSamplerLevel( textureProperty, uvSnippet, biasSnippet, shaderStage );
snippet = this._getSamplerLevel( texture, textureProperty, uvSnippet, biasSnippet, shaderStage );

}

Expand Down Expand Up @@ -302,7 +314,7 @@ class WGSLNodeBuilder extends NodeBuilder {
const lastBinding = bindings[ bindings.length - 1 ];
const index = lastBinding && lastBinding.isUniformsGroup ? bindings.length - 1 : bindings.length;

if ( shaderStage === 'fragment' ) {
if ( shaderStage === 'fragment' && this.isUnfilterable( node.value ) === false ) {

const sampler = new NodeSampler( `${uniformNode.name}_sampler`, uniformNode.node );
sampler.setVisibility( gpuShaderStageLib[ shaderStage ] );
Expand Down Expand Up @@ -580,9 +592,9 @@ class WGSLNodeBuilder extends NodeBuilder {

if ( uniform.type === 'texture' || uniform.type === 'cubeTexture' ) {

if ( shaderStage === 'fragment' ) {
const texture = uniform.node.value;

const texture = uniform.node.value;
if ( shaderStage === 'fragment' && this.isUnfilterable( texture ) === false ) {

if ( texture.isDepthTexture === true && texture.compareFunction !== null ) {

Expand All @@ -596,8 +608,6 @@ class WGSLNodeBuilder extends NodeBuilder {

}

const texture = uniform.node.value;

let textureType;

if ( texture.isCubeTexture === true ) {
Expand Down
11 changes: 9 additions & 2 deletions examples/jsm/renderers/webgpu/utils/WebGPUBindingUtils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
GPUTextureAspect, GPUTextureViewDimension, GPUBufferBindingType
GPUTextureAspect, GPUTextureViewDimension, GPUBufferBindingType, GPUTextureSampleType
} from './WebGPUConstants.js';
import { FloatType } from 'three';

class WebGPUBindingUtils {

Expand Down Expand Up @@ -64,7 +65,13 @@ class WebGPUBindingUtils {

if ( binding.texture.isDepthTexture ) {

texture.sampleType = 'depth';
texture.sampleType = GPUTextureSampleType.Depth;

} else if ( binding.texture.isDataTexture && binding.texture.type === FloatType ) {

// @TODO: Add support for this soon: backend.hasFeature( 'float32-filterable' )

texture.sampleType = GPUTextureSampleType.UnfilterableFloat;

}

Expand Down

0 comments on commit c849b5d

Please sign in to comment.