Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BREAKING] Implementation of BlendState #5083

Merged
merged 5 commits into from
Feb 23, 2023
Merged

[BREAKING] Implementation of BlendState #5083

merged 5 commits into from
Feb 23, 2023

Conversation

mvaligursky
Copy link
Contributor

@mvaligursky mvaligursky commented Feb 21, 2023

Related to #2254

Overview

A new BlendState class is implemented, which encapsulates a blending state of rendering pipeline. It handles alpha blending and enabling of per-channel writes. Internally, a bit packing is used to store blend state in a single number (per render target, to allow multiple render targets in the future). This makes blend state setup more costly, but per frame rendering is faster as just a single number needs to be compared to see if blend state needs to be changed. Additionally, it makes it fast to generate a key which is used for render pipeline look up on WebGPU.

Breaking changes

  • although all public functionality has been deprecated and preserved, if some private blending related functionality has been used, it's possible the code needs to be updated to use public functionality.

  • To align WebGL and WebGPU:

- BLENDMODE_CONSTANT_COLOR and BLENDMODE_CONSTANT_ALPHA has been merged
   to a single BLENDMODE_CONSTANT constant
- BLENDMODE_ONE_MINUS_CONSTANT_COLOR and BLENDMODE_ONE_MINUS_CONSTANT_ALPHA
   has been merged to a single BLENDMODE_ONE_MINUS_CONSTANT constant
- Note that the _COLOR constant is internally used for the color blending, and _ALPHA constant is used
   for alpha blending, so no functionality has been lost, only simplified. This is not exposed on the
   material, and was only useable using private APIs on the material, so the impact is expected to be small.
  • drawQuadWithShader function signature has changed, and last 'useBlend' parameter has been removed. If value is provided, a warning message is printed out. This parameter was confusing, as it was expected to set all other blending states directly on device apart from this one, which was internally set by the drawQuadWithShader. Now all render state is expected to be set directly on device. Code like this:
if (blending) {
    device.setBlendFunction(BLENDMODE_ONE, BLENDMODE_ONE);
    device.setBlendEquation(BLENDEQUATION_ADD);
    drawQuadWithShader(..., blending);
}

is now

    device.setBlendState(<created state with blend mode fully set up>);
    drawQuadWithShader(...);
}
  • BLEND_SUBTRACTIVE mode is now supported. The implementation was missing before, so this was behaving like BLEND_NORMAL, but now will function as expected.

New public API (still breaking change, as old APIs are deprecated)

  • BlendState.constructor(blend = false, colorOp = BLENDEQUATION_ADD, colorSrcFactor = BLENDMODE_ONE, colorDstFactor = BLENDMODE_ZERO,
    alphaOp, alphaSrcFactor, alphaDstFactor,
    redWrite = true, greenWrite = true, blueWrite = true, alphaWrite = true)

  • BlendState.blend - toggle blending

  • BlendState.equals - compares two blend states

  • BlendState.DEFAULT - a static access to a blend state with no blending and writing to all channels

  • GraphicsDevice.setBlendState(blendState)
    this replaces these properties:

blending
blendSrc
blendDst
blendSrcAlpha
blendDstAlpha
separateAlphaBlend
blendEquation
blendAlphaEquation
separateAlphaEquation
writeRed
writeGreen
writeBlue
writeAlpha
  • Material.blendState property
    this replaces these properties:
blend
blendSrc
blendDst
blendEquation
separateAlphaBlend
blendSrcAlpha
blendDstAlpha
blendAlphaEquation
redWrite
greenWrite
blueWrite
alphaWrite

Other changes

  • internally, engine has been updated to use blend state for all blending operations, and in some cases, a blend state is set that wasn't before, to enforce correct state to be set up, to avoid leftover state issue
  • on WebGl platform, the implementation has been changed to always use separate blending for color & alpha channels, to match WebGPU - this is the same number of API calls anyways, so there is no advantage to use both modes.

Testing

  • tested engine examples against WebGl1 & 2
  • tested Editor

JSDocs for the new class

Screenshot 2023-02-21 at 12 21 04

Screenshot 2023-02-21 at 12 21 16

@mvaligursky mvaligursky self-assigned this Feb 21, 2023
@mvaligursky mvaligursky added the area: graphics Graphics related issue label Feb 21, 2023
@mvaligursky mvaligursky changed the title [Breaking] Implementation of BlendState [BREAKING] Implementation of BlendState Feb 21, 2023
src/platform/graphics/blend-state.js Outdated Show resolved Hide resolved
src/platform/graphics/blend-state.js Show resolved Hide resolved
src/scene/materials/material.js Show resolved Hide resolved
src/deprecated/deprecated.js Show resolved Hide resolved
Co-authored-by: Donovan Hutchence <slimbuck7@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: graphics Graphics related issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants