-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
201 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* Database of alpha-to-coverage patterns from different devices. | ||
* | ||
* Name of device -> | ||
* Array of patterns from a=0.0 to a=1.0, evenly spaced, excluding endpoints -> | ||
* Array of N*N masks depending on the block size of the pattern used | ||
* (in row-major order) | ||
*/ | ||
export const alphaToCoverageDatabase: { [k: string]: PatternSequence } = { | ||
'NVIDIA GeForce RTX 3070': [[0b1000], [0b1001], [0b1011]], | ||
'Intel HD Graphics 4400': [[0b0001], [0b0011], [0b0111]], | ||
}; | ||
|
||
type PatternSequence = ReadonlyArray<Pattern>; | ||
type Pattern = ReadonlyArray<Mask>; | ||
type Mask = number; | ||
|
||
/** | ||
* For each device name, provides the source for a WGSL function which emulates | ||
* the alpha-to-coverage algorithm of that device by mapping (alpha, x, y) to | ||
* a sample mask. | ||
*/ | ||
export const kEmulatedAlphaToCoverage = { | ||
'Apple M1 Pro': `\ | ||
fn emulatedAlphaToCoverage(alpha: f32, x: u32, y: u32) -> u32 { | ||
let u = x % 2; | ||
let v = y % 2; | ||
if (alpha < 0.5 / 16) { return ${0b0000}; } | ||
// FIXME returning values out of an array is not working, always returns 0 | ||
if (alpha < 1.5 / 16) { return array(array(${0b0001}u, ${0b0000}), array(${0b0000}, ${0b0000}))[v][u]; } | ||
if (alpha < 2.5 / 16) { return array(array(${0b0001}u, ${0b0000}), array(${0b0000}, ${0b0001}))[v][u]; } | ||
if (alpha < 3.5 / 16) { return array(array(${0b0001}u, ${0b0001}), array(${0b0000}, ${0b0001}))[v][u]; } | ||
if (alpha < 4.5 / 16) { return array(array(${0b0001}u, ${0b0001}), array(${0b0001}, ${0b0001}))[v][u]; } | ||
if (alpha < 5.5 / 16) { return array(array(${0b1001}u, ${0b0001}), array(${0b0001}, ${0b0001}))[v][u]; } | ||
if (alpha < 6.5 / 16) { return array(array(${0b1001}u, ${0b0001}), array(${0b0001}, ${0b1001}))[v][u]; } | ||
if (alpha < 7.5 / 16) { return array(array(${0b1001}u, ${0b1001}), array(${0b0001}, ${0b1001}))[v][u]; } | ||
if (alpha < 8.5 / 16) { return array(array(${0b1001}u, ${0b1001}), array(${0b1001}, ${0b1001}))[v][u]; } | ||
if (alpha < 9.5 / 16) { return array(array(${0b1011}u, ${0b1001}), array(${0b1001}, ${0b1001}))[v][u]; } | ||
if (alpha < 10.5 / 16) { return array(array(${0b1011}u, ${0b1001}), array(${0b1001}, ${0b1011}))[v][u]; } | ||
if (alpha < 11.5 / 16) { return array(array(${0b1011}u, ${0b1011}), array(${0b1001}, ${0b1011}))[v][u]; } | ||
if (alpha < 12.5 / 16) { return array(array(${0b1011}u, ${0b1011}), array(${0b1011}, ${0b1011}))[v][u]; } | ||
if (alpha < 13.5 / 16) { return array(array(${0b1111}u, ${0b1011}), array(${0b1011}, ${0b1011}))[v][u]; } | ||
if (alpha < 14.5 / 16) { return array(array(${0b1111}u, ${0b1011}), array(${0b1011}, ${0b1111}))[v][u]; } | ||
if (alpha < 15.5 / 16) { return array(array(${0b1111}u, ${0b1111}), array(${0b1011}, ${0b1111}))[v][u]; } | ||
return ${0b1111}; | ||
} | ||
`.trimEnd(), | ||
'NVIDIA GeForce RTX 3070': `\ | ||
fn emulatedAlphaToCoverage(alpha: f32, x: u32, y: u32) -> u32 { | ||
if (alpha < 0.5 / 4) { return ${0b0000}; } | ||
if (alpha < 1.5 / 4) { return ${0b1000}; } | ||
if (alpha < 2.5 / 4) { return ${0b1001}; } | ||
if (alpha < 3.5 / 4) { return ${0b1011}; } | ||
return ${0b1111}; | ||
} | ||
`.trimEnd(), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
sample/alphaToCoverage/renderWithEmulatedAlphaToCoverage.wgsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
struct Varying { | ||
@builtin(position) pos: vec4f, | ||
// Color from instance-step-mode vertex buffer | ||
@location(0) color: vec4f, | ||
} | ||
|
||
@vertex | ||
fn vmain( | ||
@builtin(vertex_index) vertex_index: u32, | ||
@location(0) color: vec4f, | ||
) -> Varying { | ||
var square = array( | ||
vec2f(-1, -1), vec2f(-1, 1), vec2f( 1, -1), | ||
vec2f( 1, -1), vec2f(-1, 1), vec2f( 1, 1), | ||
); | ||
|
||
return Varying(vec4(square[vertex_index], 0, 1), color); | ||
} | ||
|
||
struct FragOut { | ||
@location(0) color: vec4f, | ||
@builtin(sample_mask) mask: u32, | ||
} | ||
|
||
@fragment | ||
fn fmain(vary: Varying) -> FragOut { | ||
let mask = emulatedAlphaToCoverage(vary.color.a, u32(vary.pos.x), u32(vary.pos.y)); | ||
return FragOut(vec4f(vary.color.rgb, 1.0), mask); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters