Skip to content

Commit

Permalink
Change to static members of BlendState (#5303)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Valigursky <mvaligursky@snapchat.com>
  • Loading branch information
mvaligursky and Martin Valigursky committed May 3, 2023
1 parent a4f349f commit e63821d
Show file tree
Hide file tree
Showing 15 changed files with 27 additions and 17 deletions.
2 changes: 2 additions & 0 deletions src/deprecated/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,8 @@ GraphicsDevice.prototype.removeShaderFromCache = function (shader) {
getProgramLibrary(this).removeFromCache(shader);
};

BlendState.DEFAULT = Object.freeze(new BlendState());

const _tempBlendState = new BlendState();
const _tempDepthState = new DepthState();

Expand Down
2 changes: 1 addition & 1 deletion src/framework/graphics/picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class Picker {
self.pickColor[1] = ((index >> 8) & 0xff) / 255;
self.pickColor[2] = (index & 0xff) / 255;
pickColorId.setValue(self.pickColor);
device.setBlendState(BlendState.DEFAULT);
device.setBlendState(BlendState.NOBLEND);

// keep the index -> meshInstance index mapping
self.mapping[index] = meshInstance;
Expand Down
2 changes: 1 addition & 1 deletion src/framework/lightmapper/lightmapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ class Lightmapper {
this.lightmapFilters.prepareDenoise(this.scene.lightmapFilterRange, this.scene.lightmapFilterSmoothness);
}

device.setBlendState(BlendState.DEFAULT);
device.setBlendState(BlendState.NOBLEND);
device.setDepthState(DepthState.NODEPTH);
device.setStencilState(null, null);

Expand Down
14 changes: 11 additions & 3 deletions src/platform/graphics/blend-state.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BitPacking } from "../../core/math/bit-packing.js";
import { BLENDEQUATION_ADD, BLENDMODE_ONE, BLENDMODE_ZERO } from '../../platform/graphics/constants.js';
import { BLENDEQUATION_ADD, BLENDMODE_ONE, BLENDMODE_ZERO, BLENDMODE_SRC_ALPHA, BLENDMODE_ONE_MINUS_SRC_ALPHA } from '../../platform/graphics/constants.js';

// masks (to only keep relevant bits)
const opMask = 0b111;
Expand Down Expand Up @@ -227,12 +227,12 @@ class BlendState {
}

/**
* A default blend state that has blending disabled and writes to all color channels.
* A blend state that has blending disabled and writes to all color channels.
*
* @type {BlendState}
* @readonly
*/
static DEFAULT = Object.freeze(new BlendState());
static NOBLEND = Object.freeze(new BlendState());

/**
* A blend state that does not write to color channels.
Expand All @@ -241,6 +241,14 @@ class BlendState {
* @readonly
*/
static NOWRITE = Object.freeze(new BlendState(undefined, undefined, undefined, undefined, undefined, undefined, undefined, false, false, false, false));

/**
* A blend state that does simple translucency using alpha channel.
*
* @type {BlendState}
* @readonly
*/
static ALPHABLEND = Object.freeze(new BlendState(true, BLENDEQUATION_ADD, BLENDMODE_SRC_ALPHA, BLENDMODE_ONE_MINUS_SRC_ALPHA));
}

export { BlendState };
4 changes: 2 additions & 2 deletions src/platform/graphics/webgl/webgl-graphics-device.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function quadWithShader(device, target, shader) {
device.updateBegin();

device.setCullMode(CULLFACE_NONE);
device.setBlendState(BlendState.DEFAULT);
device.setBlendState(BlendState.NOBLEND);
device.setDepthState(DepthState.NODEPTH);
device.setStencilState(null, null);

Expand Down Expand Up @@ -2016,7 +2016,7 @@ class WebglGraphicsDevice extends GraphicsDevice {
this.clearColor.set(r, g, b, a);
}

this.setBlendState(BlendState.DEFAULT);
this.setBlendState(BlendState.NOBLEND);
}

if (flags & CLEARFLAG_DEPTH) {
Expand Down
2 changes: 1 addition & 1 deletion src/platform/graphics/webgpu/webgpu-clear-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class WebgpuClearRenderer {
const color = options.color ?? defaultOptions.color;
this.colorData.set(color);

device.setBlendState(BlendState.DEFAULT);
device.setBlendState(BlendState.NOBLEND);
} else {
device.setBlendState(BlendState.NOWRITE);
}
Expand Down
2 changes: 1 addition & 1 deletion src/scene/graphics/post-effect.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class PostEffect {
viewport = _viewport.set(rect.x * w, rect.y * h, rect.z * w, rect.w * h);
}

this.device.setBlendState(BlendState.DEFAULT);
this.device.setBlendState(BlendState.NOBLEND);
drawQuadWithShader(this.device, target, shader, viewport);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/scene/graphics/prefilter-cubemap.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function shFromCubemap(device, source, dontFlipX) {
depth: false
});
constantTexSource.setValue(tex);
device.setBlendState(BlendState.DEFAULT);
device.setBlendState(BlendState.NOBLEND);
drawQuadWithShader(device, targ, shader);

const gl = device.gl;
Expand Down
2 changes: 1 addition & 1 deletion src/scene/graphics/reproject-texture.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ function reprojectTexture(source, target, options = {}) {

// render state
// TODO: set up other render state here to expected state
device.setBlendState(BlendState.DEFAULT);
device.setBlendState(BlendState.NOBLEND);

const constantSource = device.scope.resolve(source.cubemap ? "sourceCube" : "sourceTex");
Debug.assert(constantSource);
Expand Down
2 changes: 1 addition & 1 deletion src/scene/graphics/scene-grab.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ class SceneGrab {

onDrawCall: function () {
// writing depth to color render target, force no blending and writing to all channels
device.setBlendState(BlendState.DEFAULT);
device.setBlendState(BlendState.NOBLEND);
},

onPostRenderOpaque: function (cameraPass) {
Expand Down
2 changes: 1 addition & 1 deletion src/scene/morph-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ class MorphInstance {
this.morphFactor.setValue(this._shaderMorphWeights);

// alpha blending - first pass gets none, following passes are additive
device.setBlendState(blending ? blendStateAdditive : BlendState.DEFAULT);
device.setBlendState(blending ? blendStateAdditive : BlendState.NOBLEND);

// render quad with shader for required number of textures
const shader = this._getShader(usedCount);
Expand Down
2 changes: 1 addition & 1 deletion src/scene/particle-system/gpu-updater.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class ParticleGPUUpdater {

const emitter = this._emitter;

device.setBlendState(BlendState.DEFAULT);
device.setBlendState(BlendState.NOBLEND);
device.setDepthState(DepthState.NODEPTH);
device.setCullMode(CULLFACE_NONE);

Expand Down
2 changes: 1 addition & 1 deletion src/scene/renderer/cookie-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class CookieRenderer {
this.blitTextureId.setValue(light.cookie);

// render state
device.setBlendState(BlendState.DEFAULT);
device.setBlendState(BlendState.NOBLEND);

// render it to a viewport of the target
for (let face = 0; face < faceCount; face++) {
Expand Down
2 changes: 1 addition & 1 deletion src/scene/renderer/forward-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,7 @@ class ForwardRenderer extends Renderer {
// Revert temp frame stuff
// TODO: this should not be here, as each rendering / clearing should explicitly set up what
// it requires (the properties are part of render pipeline on WebGPU anyways)
device.setBlendState(BlendState.DEFAULT);
device.setBlendState(BlendState.NOBLEND);
device.setStencilState(null, null);
device.setAlphaToCoverage(false); // don't leak a2c state
device.setDepthBias(false);
Expand Down
2 changes: 1 addition & 1 deletion src/scene/renderer/shadow-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ class ShadowRenderer {
DebugGraphics.pushGpuMarker(device, `VSM ${light._node.name}`);

// render state
device.setBlendState(BlendState.DEFAULT);
device.setBlendState(BlendState.NOBLEND);

const lightRenderData = light.getRenderData(light._type === LIGHTTYPE_DIRECTIONAL ? camera : null, 0);
const shadowCam = lightRenderData.shadowCamera;
Expand Down

0 comments on commit e63821d

Please sign in to comment.