-
-
Notifications
You must be signed in to change notification settings - Fork 72
feat(@typegpu/color): Tonemapping functions #2691
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
Open
Aquel32
wants to merge
5
commits into
software-mansion:main
Choose a base branch
from
Aquel32:feat/typegpu-color-tonemapping-functions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6aac088
feat(@typegpu/color): Tonemapping functions
Aquel32 e3600e2
feat(@typegpu/color): Tonemapping functions example
Aquel32 3bf2604
feat(@typegpu/color): Tonemapping example test
Aquel32 59d9902
Merge branch 'software-mansion:main' into feat/typegpu-color-tonemapp…
Aquel32 6142353
refactor(@typegpu/color): Formatting in tonemapping functions and exa…
Aquel32 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
apps/typegpu-docs/src/examples/image-processing/tonemapping/index.html
This file contains hidden or 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 @@ | ||
| <canvas></canvas> |
179 changes: 179 additions & 0 deletions
179
apps/typegpu-docs/src/examples/image-processing/tonemapping/index.ts
This file contains hidden or 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,179 @@ | ||
| import tgpu, { common, d, std } from 'typegpu'; | ||
| import { defineControls } from '../../common/defineControls.ts'; | ||
| import { hable, reinhard, aces, neutral } from "@typegpu/color"; | ||
|
|
||
| const root = await tgpu.init(); | ||
| const canvas = document.querySelector("canvas") as HTMLCanvasElement; | ||
| const context = root.configureContext({ canvas }); | ||
|
iwoplaza marked this conversation as resolved.
Outdated
|
||
|
|
||
| const tonemappingUniform = root.createUniform(d.u32, 0); | ||
| const pointLightColor = root.createUniform(d.vec3f, d.vec3f(1.0, 0.45, 0.075)); | ||
| const pointLightScale = root.createUniform(d.f32, 0.2); | ||
| const pointLightX = root.createUniform(d.f32, 0.5); | ||
| const pointLightY = root.createUniform(d.f32, 0.5); | ||
|
|
||
| let sideBySide = false; | ||
|
|
||
| const mainFragment = tgpu.fragmentFn({ | ||
| in: { uv: d.vec2f }, | ||
| out: d.vec4f, | ||
| })(({ uv }) => { | ||
| "use gpu"; | ||
|
iwoplaza marked this conversation as resolved.
Outdated
|
||
|
|
||
| const brightness = | ||
| pointLightScale.$ / | ||
| std.length(uv - d.vec2f(pointLightX.$, pointLightY.$)); | ||
| let color = pointLightColor.$ * brightness; | ||
|
|
||
| if (tonemappingUniform.$ === 1) { | ||
| color = aces(color); | ||
| } | ||
| else if (tonemappingUniform.$ === 2) { | ||
| color = hable(color); | ||
| } | ||
| else if (tonemappingUniform.$ === 3) { | ||
| color = reinhard(color); | ||
| } | ||
| else if(tonemappingUniform.$ === 4) { | ||
| color = neutral(color); | ||
| } | ||
|
Aquel32 marked this conversation as resolved.
|
||
|
|
||
| return d.vec4f(color, 1.0); | ||
| }); | ||
|
|
||
| const mainRenderPipeline = root.createRenderPipeline({ | ||
| vertex: common.fullScreenTriangle, | ||
| fragment: mainFragment, | ||
| }); | ||
|
|
||
| const sideBySideFragment = tgpu.fragmentFn({ | ||
| in: { uv: d.vec2f }, | ||
| out: d.vec4f, | ||
| })(({ uv }) => { | ||
| "use gpu"; | ||
|
iwoplaza marked this conversation as resolved.
Outdated
|
||
|
|
||
| const rows = 3; | ||
| const columns = 3; | ||
| const columnWidth = 1.0 / d.f32(columns); | ||
| const rowHeight = 1.0 / d.f32(rows); | ||
|
|
||
| const alignedUV = d.vec2f(uv.x, uv.y - rowHeight * 0.5); | ||
|
|
||
| const column = d.f32(std.floor(alignedUV.x * d.f32(columns))); | ||
| const row = d.f32(std.floor(alignedUV.y * d.f32(rows))); | ||
|
|
||
| const localUV = d.vec2f( | ||
| (alignedUV.x - column * columnWidth) / columnWidth, | ||
| (alignedUV.y - row * rowHeight) / rowHeight, | ||
| ); | ||
|
|
||
| const brightness = | ||
| pointLightScale.$ / | ||
| std.length(localUV - d.vec2f(pointLightX.$, pointLightY.$)); | ||
| let color = pointLightColor.$ * brightness; | ||
|
|
||
| if(column === 0 && row === 0) { | ||
| color = d.vec3f(color); | ||
| } | ||
| else if (column === 1 && row === 0) { | ||
| color = aces(color); | ||
| } | ||
| else if (column === 2 && row === 0) { | ||
| color = hable(color); | ||
| } | ||
| else if (column === 0 && row === 1) { | ||
| color = reinhard(color); | ||
| } | ||
| else if (column === 1 && row === 1) { | ||
| color = neutral(color); | ||
| } | ||
| else { | ||
| color = d.vec3f(1.0); | ||
| } | ||
|
iwoplaza marked this conversation as resolved.
Outdated
|
||
|
|
||
| return d.vec4f(color, 1.0); | ||
| }); | ||
|
|
||
| const sideBySideRenderPipeline = root.createRenderPipeline({ | ||
| vertex: common.fullScreenTriangle, | ||
| fragment: sideBySideFragment, | ||
| }); | ||
|
|
||
| function draw() { | ||
| const renderPipeline = sideBySide ? sideBySideRenderPipeline : mainRenderPipeline; | ||
|
|
||
| renderPipeline.withColorAttachment({ view: context }).draw(3); | ||
|
|
||
| requestAnimationFrame(draw); | ||
| } | ||
|
|
||
| requestAnimationFrame(draw); | ||
|
|
||
| export const controls = defineControls({ | ||
| "Tonemapping Mode": { | ||
| initial: "None", | ||
| options: [ | ||
| "None", | ||
| "ACES", | ||
| "Hable", | ||
| "Reinhard", | ||
| "Neutral", | ||
| ], | ||
|
iwoplaza marked this conversation as resolved.
Outdated
|
||
| onSelectChange(value: string) { | ||
| if (value === "None") { | ||
| tonemappingUniform.write(0); | ||
| } else if (value === "ACES") { | ||
| tonemappingUniform.write(1); | ||
| } else if (value === "Hable") { | ||
| tonemappingUniform.write(2); | ||
| } else if (value === "Reinhard") { | ||
| tonemappingUniform.write(3); | ||
| } else if (value === "Neutral") { | ||
| tonemappingUniform.write(4); | ||
| } | ||
| } | ||
| }, | ||
| "Point Light Color": { | ||
| initial: d.vec3f(1.0, 0.45, 0.075), | ||
| onColorChange: (c) => { | ||
| pointLightColor.write(c); | ||
| }, | ||
| }, | ||
| "Point Light Scale": { | ||
| initial: 0.2, | ||
| min: 0.01, | ||
| max: 2, | ||
| step: 0.01, | ||
| onSliderChange: (value: number) => { | ||
| pointLightScale.write(value); | ||
| }, | ||
| }, | ||
| "Point Light X": { | ||
| initial: 0.5, | ||
| min: 0, | ||
| max: 1, | ||
| step: 0.01, | ||
| onSliderChange: (value: number) => { | ||
| pointLightX.write(value); | ||
| }, | ||
| }, | ||
| "Point Light Y": { | ||
| initial: 0.5, | ||
| min: 0, | ||
| max: 1, | ||
| step: 0.01, | ||
| onSliderChange: (value: number) => { | ||
| pointLightY.write(value); | ||
| }, | ||
| }, | ||
| "Side by Side": { | ||
| initial: false, | ||
| onToggleChange: (value: boolean) => { | ||
| sideBySide = value; | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| export function onCleanup() { | ||
| root.destroy(); | ||
| } | ||
7 changes: 7 additions & 0 deletions
7
apps/typegpu-docs/src/examples/image-processing/tonemapping/meta.json
This file contains hidden or 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,7 @@ | ||
| { | ||
| "title": "Tonemapping", | ||
| "category": "image-processing", | ||
| "tags": ["ecosystem", "color", "tonemapping"], | ||
| "dev": true, | ||
| "coolFactor": 5 | ||
| } |
Binary file added
BIN
+892 KB
apps/typegpu-docs/src/examples/image-processing/tonemapping/thumbnail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
119 changes: 119 additions & 0 deletions
119
apps/typegpu-docs/tests/individual-example-tests/tonemapping.test.ts
This file contains hidden or 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,119 @@ | ||
| /** | ||
| * @vitest-environment jsdom | ||
| */ | ||
|
|
||
| import { describe, expect } from 'vitest'; | ||
| import { it } from 'typegpu-testing-utility'; | ||
| import { runExampleTest, setupCommonMocks } from './utils/baseTest.ts'; | ||
|
|
||
| describe('tonemapping example', () => { | ||
| setupCommonMocks(); | ||
|
|
||
| it('should produce valid code', async ({ device }) => { | ||
| const shaderCodes = await runExampleTest( | ||
| { | ||
| category: "image-processing", | ||
| name: "tonemapping", | ||
| expectedCalls: 1 | ||
| }, | ||
| device | ||
| ); | ||
|
Aquel32 marked this conversation as resolved.
|
||
|
|
||
| expect(shaderCodes).toMatchInlineSnapshot(` | ||
| "struct fullScreenTriangle_Output { | ||
| @builtin(position) pos: vec4f, | ||
| @location(0) uv: vec2f, | ||
| } | ||
|
|
||
| @vertex fn fullScreenTriangle(@builtin(vertex_index) vertexIndex: u32) -> fullScreenTriangle_Output { | ||
| const pos = array<vec2f, 3>(vec2f(-1, -1), vec2f(3, -1), vec2f(-1, 3)); | ||
| const uv = array<vec2f, 3>(vec2f(0, 1), vec2f(2, 1), vec2f(0, -1)); | ||
|
|
||
| return fullScreenTriangle_Output(vec4f(pos[vertexIndex], 0, 1), uv[vertexIndex]); | ||
| } | ||
|
|
||
| @group(0) @binding(0) var<uniform> pointLightX: f32; | ||
|
|
||
| @group(0) @binding(1) var<uniform> pointLightY: f32; | ||
|
|
||
| @group(0) @binding(2) var<uniform> pointLightScale: f32; | ||
|
|
||
| @group(0) @binding(3) var<uniform> pointLightColor: vec3f; | ||
|
|
||
| @group(0) @binding(4) var<uniform> tonemappingUniform: u32; | ||
|
|
||
| fn aces(rgb: vec3f) -> vec3f { | ||
| const a = 2.51; | ||
| const b = 0.03; | ||
| const c = 2.43; | ||
| const d = 0.59; | ||
| const e = 0.14; | ||
| return saturate(((rgb * ((a * rgb) + b)) / ((rgb * ((c * rgb) + d)) + e))); | ||
| } | ||
|
|
||
| fn hableCurve(x: vec3f) -> vec3f { | ||
| const a = 0.15; | ||
| const b = 0.5; | ||
| const c = 0.1; | ||
| const d = 0.2; | ||
| const e = 0.02; | ||
| const f = 0.3; | ||
| return ((((x * ((a * x) + (c * b))) + (d * e)) / ((x * ((a * x) + b)) + (d * f))) - (e / f)); | ||
| } | ||
|
|
||
| fn hable(rgb: vec3f) -> vec3f { | ||
| let W = vec3f(11.199999809265137); | ||
| return saturate((hableCurve(rgb) / hableCurve(W))); | ||
| } | ||
|
|
||
| fn reinhard(rgb: vec3f) -> vec3f { | ||
| return saturate((rgb / (vec3f(1) + rgb))); | ||
| } | ||
|
|
||
| fn neutral(rgb: vec3f) -> vec3f { | ||
| const startCompression = 0.76; | ||
| const desaturation = 0.15; | ||
| let x = min(rgb.r, min(rgb.g, rgb.b)); | ||
| let offset = select(0.04f, (x - ((6.25f * x) * x)), (x < 0.08f)); | ||
| var color = (rgb - offset); | ||
| let peak = max(color.r, max(color.g, color.b)); | ||
| if ((peak < startCompression)) { | ||
| return saturate(color); | ||
| } | ||
| let d = (1f - startCompression); | ||
| let newPeak = (1f - ((d * d) / ((peak - startCompression) + d))); | ||
| color *= (newPeak / peak); | ||
| let g = (1f - (1f / ((desaturation * (peak - newPeak)) + 1f))); | ||
| return saturate(mix(color, vec3f(newPeak), g)); | ||
| } | ||
|
|
||
| struct mainFragment_Input { | ||
| @location(0) uv: vec2f, | ||
| } | ||
|
|
||
| @fragment fn mainFragment(_arg_0: mainFragment_Input) -> @location(0) vec4f { | ||
| let brightness = (pointLightScale / length((_arg_0.uv - vec2f(pointLightX, pointLightY)))); | ||
| var color = (pointLightColor * brightness); | ||
| if ((tonemappingUniform == 1u)) { | ||
| color = aces(color); | ||
| } | ||
| else { | ||
| if ((tonemappingUniform == 2u)) { | ||
| color = hable(color); | ||
| } | ||
| else { | ||
| if ((tonemappingUniform == 3u)) { | ||
| color = reinhard(color); | ||
| } | ||
| else { | ||
| if ((tonemappingUniform == 4u)) { | ||
| color = neutral(color); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return vec4f(color, 1f); | ||
| }" | ||
| `); | ||
| }); | ||
| }); | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.