Skip to content

Commit

Permalink
WebGLCapabilities: add canReadTextureType() and canReadPixelFormat()
Browse files Browse the repository at this point in the history
  • Loading branch information
sguimmara committed Apr 8, 2024
1 parent bb74a06 commit d1785dd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 31 deletions.
33 changes: 2 additions & 31 deletions src/renderers/WebGLRenderer.js
Expand Up @@ -3,9 +3,7 @@ import {
BackSide,
FrontSide,
DoubleSide,
RGBAFormat,
HalfFloatType,
FloatType,
UnsignedByteType,
NoToneMapping,
LinearMipmapLinearFilter,
Expand Down Expand Up @@ -2261,33 +2259,6 @@ class WebGLRenderer {

};

this.canReadPixelFormat = function ( textureFormat ) {

if ( textureFormat !== RGBAFormat && utils.convert( textureFormat ) !== _gl.getParameter( _gl.IMPLEMENTATION_COLOR_READ_FORMAT ) ) {

return false;

}

return true;

};

this.canReadTextureType = function ( textureType ) {

const halfFloatSupportedByExt = ( textureType === HalfFloatType ) && ( extensions.has( 'EXT_color_buffer_half_float' ) || extensions.has( 'EXT_color_buffer_float' ) );

if ( textureType !== UnsignedByteType && utils.convert( textureType ) !== _gl.getParameter( _gl.IMPLEMENTATION_COLOR_READ_TYPE ) && // Edge and Chrome Mac < 52 (#9513)
textureType !== FloatType && ! halfFloatSupportedByExt ) {

return false;

}

return true;

};

this.readRenderTargetPixels = function ( renderTarget, x, y, width, height, buffer, activeCubeFaceIndex ) {

if ( ! ( renderTarget && renderTarget.isWebGLRenderTarget ) ) {
Expand Down Expand Up @@ -2315,14 +2286,14 @@ class WebGLRenderer {
const textureFormat = texture.format;
const textureType = texture.type;

if ( ! this.canReadPixelFormat( textureFormat ) ) {
if ( ! capabilities.canReadPixelFormat( textureFormat ) ) {

console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: renderTarget is not in RGBA or implementation defined format.' );
return;

}

if ( ! this.canReadTextureType( textureType ) ) {
if ( ! capabilities.canReadTextureType( textureType ) ) {

console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: renderTarget is not in UnsignedByteType or implementation defined type.' );
return;
Expand Down
35 changes: 35 additions & 0 deletions src/renderers/webgl/WebGLCapabilities.js
@@ -1,7 +1,12 @@
import { FloatType, HalfFloatType, RGBAFormat, UnsignedByteType } from '../../constants.js';
import { WebGLUtils } from './webgl/WebGLUtils.js';

function WebGLCapabilities( gl, extensions, parameters ) {

let maxAnisotropy;

const utils = new WebGLUtils( gl, extensions );

function getMaxAnisotropy() {

if ( maxAnisotropy !== undefined ) return maxAnisotropy;
Expand All @@ -22,6 +27,33 @@ function WebGLCapabilities( gl, extensions, parameters ) {

}

function canReadPixelFormat( textureFormat ) {

if ( textureFormat !== RGBAFormat && utils.convert( textureFormat ) !== gl.getParameter( gl.IMPLEMENTATION_COLOR_READ_FORMAT ) ) {

return false;

}

return true;

}

function canReadTextureType( textureType ) {

const halfFloatSupportedByExt = ( textureType === HalfFloatType ) && ( extensions.has( 'EXT_color_buffer_half_float' ) || extensions.has( 'EXT_color_buffer_float' ) );

if ( textureType !== UnsignedByteType && utils.convert( textureType ) !== gl.getParameter( gl.IMPLEMENTATION_COLOR_READ_TYPE ) && // Edge and Chrome Mac < 52 (#9513)
textureType !== FloatType && ! halfFloatSupportedByExt ) {

return false;

}

return true;

}

function getMaxPrecision( precision ) {

if ( precision === 'highp' ) {
Expand Down Expand Up @@ -85,6 +117,9 @@ function WebGLCapabilities( gl, extensions, parameters ) {
getMaxAnisotropy: getMaxAnisotropy,
getMaxPrecision: getMaxPrecision,

canReadPixelFormat: canReadPixelFormat,
canReadTextureType: canReadTextureType,

precision: precision,
logarithmicDepthBuffer: logarithmicDepthBuffer,

Expand Down

0 comments on commit d1785dd

Please sign in to comment.