Skip to content

Commit

Permalink
Remove final WebGL1 extensions and simplify code that used them (#6336)
Browse files Browse the repository at this point in the history
* Remove final WebGL1 extensions and simplify code that used them

* lint

* tiny shader cleanup

* cleanup

* splat format cleanup

---------

Co-authored-by: Martin Valigursky <mvaligursky@snapchat.com>
  • Loading branch information
mvaligursky and Martin Valigursky committed May 7, 2024
1 parent 0b1c877 commit 583b8fe
Show file tree
Hide file tree
Showing 17 changed files with 65 additions and 183 deletions.
28 changes: 28 additions & 0 deletions src/deprecated/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,34 @@ Object.defineProperty(GraphicsDevice.prototype, 'webgl2', {
}
});

Object.defineProperty(GraphicsDevice.prototype, 'extBlendMinmax', {
get: function () {
Debug.deprecated('pc.GraphicsDevice#extBlendMinmax is deprecated as it is always true.');
return true;
}
});

Object.defineProperty(GraphicsDevice.prototype, 'extTextureHalfFloat', {
get: function () {
Debug.deprecated('pc.GraphicsDevice#extTextureHalfFloat is deprecated as it is always true.');
return true;
}
});

Object.defineProperty(GraphicsDevice.prototype, 'extTextureLod', {
get: function () {
Debug.deprecated('pc.GraphicsDevice#extTextureLod is deprecated as it is always true.');
return true;
}
});

Object.defineProperty(GraphicsDevice.prototype, 'textureHalfFloatFilterable', {
get: function () {
Debug.deprecated('pc.GraphicsDevice#textureHalfFloatFilterable is deprecated as it is always true.');
return true;
}
});

Object.defineProperty(GraphicsDevice.prototype, 'supportsMrt', {
get: function () {
Debug.deprecated('pc.GraphicsDevice#supportsMrt is deprecated as it is always true.');
Expand Down
3 changes: 0 additions & 3 deletions src/platform/graphics/blend-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ class BlendState {
* - {@link BLENDEQUATION_MIN}
* - {@link BLENDEQUATION_MAX}
*
* Note that MIN and MAX operations on WebGL platform require either EXT_blend_minmax or WebGL2
* to work (check device.extBlendMinmax).
*
* @param {boolean} [blend] - Enables or disables blending. Defaults to false.
* @param {number} [colorOp] - Configures color blending operation. Defaults to
* {@link BLENDEQUATION_ADD}.
Expand Down
4 changes: 2 additions & 2 deletions src/platform/graphics/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,15 @@ export const BLENDEQUATION_SUBTRACT = 1;
export const BLENDEQUATION_REVERSE_SUBTRACT = 2;

/**
* Use the smallest value. Check app.graphicsDevice.extBlendMinmax for support.
* Use the smallest value.
*
* @type {number}
* @category Graphics
*/
export const BLENDEQUATION_MIN = 3;

/**
* Use the largest value. Check app.graphicsDevice.extBlendMinmax for support.
* Use the largest value.
*
* @type {number}
* @category Graphics
Expand Down
10 changes: 1 addition & 9 deletions src/platform/graphics/graphics-device.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,6 @@ class GraphicsDevice extends EventHandler {
*/
textureFloatFilterable = false;

/**
* True if filtering can be applied when sampling 16-bit float textures.
*
* @type {boolean}
* @readonly
*/
textureHalfFloatFilterable = false;

/**
* A vertex buffer representing a quad.
*
Expand Down Expand Up @@ -886,7 +878,7 @@ class GraphicsDevice extends EventHandler {
}

case PIXELFORMAT_RGBA16F:
if (this.textureHalfFloatRenderable && (!filterable || this.textureHalfFloatFilterable)) {
if (this.textureHalfFloatRenderable) {
return format;
}
break;
Expand Down
5 changes: 1 addition & 4 deletions src/platform/graphics/null/null-graphics-device.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Debug } from '../../../core/debug.js';
import {
PIXELFORMAT_RGBA8, DEVICETYPE_NULL
DEVICETYPE_NULL
} from '../constants.js';
import { GraphicsDevice } from '../graphics-device.js';

Expand Down Expand Up @@ -44,11 +44,8 @@ class NullGraphicsDevice extends GraphicsDevice {
this.supportsAreaLights = true;
this.supportsGpuParticles = false;
this.textureFloatRenderable = true;
this.extTextureHalfFloat = true;
this.textureHalfFloatRenderable = true;
this.supportsImageBitmap = true;
this.extBlendMinmax = true;
this.areaLightLutFormat = PIXELFORMAT_RGBA8;
this.supportsTextureFetch = true;
}

Expand Down
34 changes: 3 additions & 31 deletions src/platform/graphics/webgl/webgl-graphics-device.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
FILTER_NEAREST, FILTER_LINEAR, FILTER_NEAREST_MIPMAP_NEAREST, FILTER_NEAREST_MIPMAP_LINEAR,
FILTER_LINEAR_MIPMAP_NEAREST, FILTER_LINEAR_MIPMAP_LINEAR,
FUNC_ALWAYS,
PIXELFORMAT_RGB8, PIXELFORMAT_RGBA8, PIXELFORMAT_RGBA16F, PIXELFORMAT_RGBA32F,
PIXELFORMAT_RGB8, PIXELFORMAT_RGBA8, PIXELFORMAT_RGBA32F,
STENCILOP_KEEP,
UNIFORMTYPE_BOOL, UNIFORMTYPE_INT, UNIFORMTYPE_FLOAT, UNIFORMTYPE_VEC2, UNIFORMTYPE_VEC3,
UNIFORMTYPE_VEC4, UNIFORMTYPE_IVEC2, UNIFORMTYPE_IVEC3, UNIFORMTYPE_IVEC4, UNIFORMTYPE_BVEC2,
Expand Down Expand Up @@ -716,28 +716,13 @@ class WebglGraphicsDevice extends GraphicsDevice {
// In WebGL2 float texture renderability is dictated by the EXT_color_buffer_float extension
this.textureFloatRenderable = !!this.extColorBufferFloat;

// two extensions allow us to render to half float buffers
if (this.extColorBufferHalfFloat) {
this.textureHalfFloatRenderable = !!this.extColorBufferHalfFloat;
} else if (this.extTextureHalfFloat) {
// EXT_color_buffer_float should affect both float and halffloat formats
this.textureHalfFloatRenderable = !!this.extColorBufferFloat;
} else {
this.textureHalfFloatRenderable = false;
}
// render to half float buffers support - either of these two extensions
this.extColorBufferHalfFloat = this.extColorBufferHalfFloat || !!this.extColorBufferFloat;

this.supportsMorphTargetTexturesCore = (this.maxPrecision === "highp" && this.maxVertexTextures >= 2);

this._textureFloatHighPrecision = undefined;

// area light LUT format - order of preference: half, float, 8bit
this.areaLightLutFormat = PIXELFORMAT_RGBA8;
if (this.extTextureHalfFloat && this.extTextureHalfFloatLinear) {
this.areaLightLutFormat = PIXELFORMAT_RGBA16F;
} else if (this.extTextureFloatLinear) {
this.areaLightLutFormat = PIXELFORMAT_RGBA32F;
}

this.postInit();
}

Expand Down Expand Up @@ -925,22 +910,9 @@ class WebglGraphicsDevice extends GraphicsDevice {
this.textureRG11B10Renderable = true;

if (this.isWebGL2) {
this.extBlendMinmax = true;
this.extTextureHalfFloat = true;
this.textureHalfFloatFilterable = true;
this.extTextureLod = true;
this.extColorBufferFloat = this.getExtension('EXT_color_buffer_float');
this.extDepthTexture = true;
} else {
this.extBlendMinmax = this.getExtension("EXT_blend_minmax");

this.extTextureLod = this.getExtension('EXT_shader_texture_lod');
this.extColorBufferFloat = null;
this.extDepthTexture = gl.getExtension('WEBGL_depth_texture');

this.extTextureHalfFloat = this.getExtension("OES_texture_half_float");
this.extTextureHalfFloatLinear = this.getExtension("OES_texture_half_float_linear");
this.textureHalfFloatFilterable = !!this.extTextureHalfFloatLinear;
}

this.extDebugRendererInfo = this.getExtension('WEBGL_debug_renderer_info');
Expand Down
6 changes: 1 addition & 5 deletions src/platform/graphics/webgpu/webgpu-graphics-device.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Debug, DebugHelper } from '../../../core/debug.js';
import { path } from '../../../core/path.js';

import {
PIXELFORMAT_RGBA32F, PIXELFORMAT_RGBA8, PIXELFORMAT_BGRA8, DEVICETYPE_WEBGPU,
PIXELFORMAT_RGBA8, PIXELFORMAT_BGRA8, DEVICETYPE_WEBGPU,
BUFFERUSAGE_READ, BUFFERUSAGE_COPY_DST, semanticToLocation
} from '../constants.js';
import { GraphicsDevice } from '../graphics-device.js';
Expand Down Expand Up @@ -146,12 +146,8 @@ class WebgpuGraphicsDevice extends GraphicsDevice {
this.supportsGpuParticles = true;
this.supportsCompute = true;
this.textureFloatRenderable = true;
this.textureHalfFloatFilterable = true;
this.extTextureHalfFloat = true;
this.textureHalfFloatRenderable = true;
this.supportsImageBitmap = true;
this.extBlendMinmax = true;
this.areaLightLutFormat = this.textureFloatFilterable ? PIXELFORMAT_RGBA32F : PIXELFORMAT_RGBA8;
this.supportsTextureFetch = true;

// WebGPU currently only supports 1 and 4 samples
Expand Down
61 changes: 7 additions & 54 deletions src/scene/area-light-luts.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DeviceCache } from '../platform/graphics/device-cache.js';
import {
ADDRESS_CLAMP_TO_EDGE,
FILTER_LINEAR, FILTER_NEAREST,
PIXELFORMAT_RGBA16F, PIXELFORMAT_RGBA32F,
PIXELFORMAT_RGBA16F,
TEXTURETYPE_DEFAULT
} from '../platform/graphics/constants.js';

Expand Down Expand Up @@ -60,7 +60,7 @@ class AreaLightLuts {

// placeholder LUT textures for area light
static createPlaceholder(device) {
const texture = AreaLightLuts.createTexture(device, device.areaLightLutFormat, 2, 'placeholder');
const texture = AreaLightLuts.createTexture(device, PIXELFORMAT_RGBA16F, 2, 'placeholder');

const pixels = texture.lock();
pixels.fill(0);
Expand All @@ -81,17 +81,6 @@ class AreaLightLuts {
return texture;
}

function offsetScale(data, offset, scale) {

const count = data.length;
const ret = new Float32Array(count);
for (let i = 0; i < count; i++) {
const n = i % 4;
ret[i] = (data[i] + offset[n]) * scale[n];
}
return ret;
}

function convertToHalfFloat(data) {

const count = data.length;
Expand All @@ -104,52 +93,16 @@ class AreaLightLuts {
return ret;
}

function convertToUint(data) {

const count = data.length;
const ret = new Uint8ClampedArray(count);
for (let i = 0; i < count; i++) {
ret[i] = data[i] * 255;
}

return ret;
}

const srcData1 = ltcMat1;
const srcData2 = ltcMat2;

// pick format for lut texture
let data1, data2;
const format = device.areaLightLutFormat;
if (format === PIXELFORMAT_RGBA32F) {

// float
data1 = srcData1;
data2 = srcData2;

} else if (format === PIXELFORMAT_RGBA16F) {

// half float
data1 = convertToHalfFloat(srcData1);
data2 = convertToHalfFloat(srcData2);

} else {

// low precision format
// offset and scale to avoid clipping and increase precision - this is undone in the shader
const o1 = [0.0, 0.2976, 0.01381, 0.0];
const s1 = [0.999, 3.08737, 1.6546, 0.603249];

const o2 = [-0.306897, 0.0, 0.0, 0.0];
const s2 = [1.442787, 1.0, 1.0, 1.0];

data1 = convertToUint(offsetScale(srcData1, o1, s1));
data2 = convertToUint(offsetScale(srcData2, o2, s2));
}
// convert data to half format
const data1 = convertToHalfFloat(srcData1);
const data2 = convertToHalfFloat(srcData2);

// create lut textures
const tex1 = buildTexture(device, data1, format);
const tex2 = buildTexture(device, data2, format);
const tex1 = buildTexture(device, data1, PIXELFORMAT_RGBA16F);
const tex2 = buildTexture(device, data2, PIXELFORMAT_RGBA16F);

// assign to uniforms
AreaLightLuts.applyTextures(device, tex1, tex2);
Expand Down
4 changes: 2 additions & 2 deletions src/scene/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ export const BLEND_MULTIPLICATIVE2X = 7;
export const BLEND_SCREEN = 8;

/**
* Minimum color. Check app.graphicsDevice.extBlendMinmax for support.
* Minimum color.
*
* @type {number}
* @category Graphics
*/
export const BLEND_MIN = 9;

/**
* Maximum color. Check app.graphicsDevice.extBlendMinmax for support.
* Maximum color.
*
* @type {number}
* @category Graphics
Expand Down
2 changes: 1 addition & 1 deletion src/scene/graphics/env-lighting.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const calcLevels = (width, height = 0) => {
};

const supportsFloat16 = (device) => {
return device.extTextureHalfFloat && device.textureHalfFloatRenderable;
return device.textureHalfFloatRenderable;
};

const supportsFloat32 = (device) => {
Expand Down
39 changes: 8 additions & 31 deletions src/scene/gsplat/gsplat.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,14 @@ class GSplat {
this.numSplats = numSplats;
this.aabb = aabb;

// create data textures if any format is available
this.halfFormat = this.getTextureFormat(device, true);

if (this.halfFormat !== undefined) {
const size = this.evalTextureSize(numSplats);
this.colorTexture = this.createTexture(device, 'splatColor', PIXELFORMAT_RGBA8, size);
this.transformATexture = this.createTexture(device, 'transformA', this.halfFormat ? PIXELFORMAT_RGBA16F : PIXELFORMAT_RGBA32F, size);
this.transformBTexture = this.createTexture(device, 'transformB', this.halfFormat ? PIXELFORMAT_RGBA16F : PIXELFORMAT_RGBA32F, size);
this.transformCTexture = this.createTexture(device, 'transformC', this.halfFormat ? PIXELFORMAT_R16F : PIXELFORMAT_R32F, size);
}
// create data textures using full float precision
this.halfFormat = false;

const size = this.evalTextureSize(numSplats);
this.colorTexture = this.createTexture(device, 'splatColor', PIXELFORMAT_RGBA8, size);
this.transformATexture = this.createTexture(device, 'transformA', this.halfFormat ? PIXELFORMAT_RGBA16F : PIXELFORMAT_RGBA32F, size);
this.transformBTexture = this.createTexture(device, 'transformB', this.halfFormat ? PIXELFORMAT_RGBA16F : PIXELFORMAT_RGBA32F, size);
this.transformCTexture = this.createTexture(device, 'transformC', this.halfFormat ? PIXELFORMAT_R16F : PIXELFORMAT_R32F, size);
}

destroy() {
Expand Down Expand Up @@ -134,27 +132,6 @@ class GSplat {
});
}

/**
* Gets the most suitable texture format based on device capabilities.
*
* @param {import('../../platform/graphics/graphics-device.js').GraphicsDevice} device - The graphics device.
* @param {boolean} preferHighPrecision - True to prefer high precision when available.
* @returns {boolean|undefined} True if half format should be used, false is float format should
* be used or undefined if none are available.
*/
getTextureFormat(device, preferHighPrecision) {

// true if half format should be used, false is float format should be used or undefined if none are available.
let halfFormat;
if (preferHighPrecision) {
halfFormat = false;
} else {
halfFormat = device.extTextureHalfFloat;
}

return halfFormat;
}

/**
* Updates pixel data of this.colorTexture based on the supplied color components and opacity.
* Assumes that the texture is using an RGBA format where RGB are color components influenced
Expand Down
4 changes: 2 additions & 2 deletions src/scene/materials/material.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,8 @@ class Material {
* multiplied by the source alpha.
* - {@link BLEND_MULTIPLICATIVE2X}: Multiplies colors and doubles the result.
* - {@link BLEND_SCREEN}: Softer version of additive.
* - {@link BLEND_MIN}: Minimum color. Check app.graphicsDevice.extBlendMinmax for support.
* - {@link BLEND_MAX}: Maximum color. Check app.graphicsDevice.extBlendMinmax for support.
* - {@link BLEND_MIN}: Minimum color.
* - {@link BLEND_MAX}: Maximum color.
*
* Defaults to {@link BLEND_NONE}.
*
Expand Down

0 comments on commit 583b8fe

Please sign in to comment.