-
Notifications
You must be signed in to change notification settings - Fork 1.4k
WIP implementation of UniformBuffer and BindGroup #4258
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
Merged
Merged
Changes from all commits
Commits
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
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,73 @@ | ||
/** @typedef {import('./graphics-device.js').GraphicsDevice} GraphicsDevice */ | ||
|
||
/** | ||
* @ignore | ||
*/ | ||
class BindBufferFormat { | ||
constructor(name, visibility) { | ||
/** @type {string} */ | ||
this.name = name; | ||
|
||
// SHADER_STAGE_VERTEX, SHADER_STAGE_FRAGMENT, SHADER_STAGE_COMPUTE | ||
this.visibility = visibility; | ||
} | ||
} | ||
|
||
/** | ||
* @ignore | ||
*/ | ||
class BindTextureFormat { | ||
constructor(name, visibility) { | ||
/** @type {string} */ | ||
this.name = name; | ||
|
||
// SHADER_STAGE_VERTEX, SHADER_STAGE_FRAGMENT, SHADER_STAGE_COMPUTE | ||
this.visibility = visibility; | ||
} | ||
} | ||
|
||
/** | ||
* @ignore | ||
*/ | ||
class BindGroupFormat { | ||
/** | ||
* @param {GraphicsDevice} graphicsDevice - The graphics device used to manage this vertex format. | ||
* @param {BindBufferFormat[]} bufferFormats - | ||
* @param {BindTextureFormat[]} textureFormats - | ||
*/ | ||
constructor(graphicsDevice, bufferFormats, textureFormats) { | ||
/** @type {GraphicsDevice} */ | ||
this.device = graphicsDevice; | ||
|
||
/** @type {BindBufferFormat[]} */ | ||
this.bufferFormats = bufferFormats; | ||
|
||
// maps a buffer format name to an index | ||
/** @type Map<string, number> */ | ||
this.bufferFormatsMap = new Map(); | ||
bufferFormats.forEach((bf, i) => this.bufferFormatsMap.set(bf.name, i)); | ||
|
||
/** @type {BindTextureFormat[]} */ | ||
this.textureFormats = textureFormats; | ||
|
||
// maps a texture format name to an index | ||
/** @type Map<string, number> */ | ||
this.textureFormatsMap = new Map(); | ||
textureFormats.forEach((tf, i) => this.textureFormatsMap.set(tf.name, i)); | ||
|
||
this.impl = graphicsDevice.createBindGroupFormatImpl(this); | ||
} | ||
|
||
/** | ||
* Frees resources associated with this bind group. | ||
*/ | ||
destroy() { | ||
this.impl.destroy(); | ||
} | ||
|
||
loseContext() { | ||
// TODO: implement | ||
} | ||
} | ||
|
||
export { BindBufferFormat, BindTextureFormat, BindGroupFormat }; |
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,78 @@ | ||
/** @typedef {import('./graphics-device.js').GraphicsDevice} GraphicsDevice */ | ||
/** @typedef {import('./texture.js').Texture} Texture */ | ||
/** @typedef {import('./bind-group-format.js').BindGroupFormat} BindGroupFormat */ | ||
|
||
import { Debug } from '../core/debug.js'; | ||
|
||
/** | ||
* A bind group represents an collection of {@link UniformBuffer} and {@link Texture} instance, | ||
* which can be bind on a GPU for rendering. | ||
* | ||
* @ignore | ||
*/ | ||
class BindGroup { | ||
/** | ||
* Create a new Bind Group. | ||
* | ||
* @param {GraphicsDevice} graphicsDevice - The graphics device used to manage this uniform buffer. | ||
* @param {BindGroupFormat} format - Format of the bind group. | ||
*/ | ||
constructor(graphicsDevice, format) { | ||
this.device = graphicsDevice; | ||
this.format = format; | ||
this.dirty = true; | ||
this.impl = graphicsDevice.createBindGroupImpl(this); | ||
|
||
this.textures = []; | ||
this.uniformBuffers = []; | ||
} | ||
|
||
/** | ||
* Assign a uniform buffer to a slot. | ||
* | ||
* @param {*} name - The name of the uniform buffer slot | ||
* @param {*} uniformBuffer - The Uniform buffer to assign to the slot. | ||
*/ | ||
setUniformBuffer(name, uniformBuffer) { | ||
const index = this.format.bufferFormatsMap.get(name); | ||
Debug.assert(index !== undefined, `Setting a uniform [${name}] on a bind group which does not contain in.`); | ||
if (this.uniformBuffers[index] !== uniformBuffer) { | ||
this.uniformBuffers[index] = uniformBuffer; | ||
this.dirty = true; | ||
} | ||
} | ||
|
||
/** | ||
* Assign a texture to a slot. | ||
* | ||
* @param {string} name - The name of the texture slot. | ||
* @param {Texture} texture - Texture to assign to the slot. | ||
*/ | ||
setTexture(name, texture) { | ||
const index = this.format.textureFormatsMap.get(name); | ||
Debug.assert(index !== undefined, `Setting a texture [${name}] on a bind group which does not contain in.`); | ||
if (this.textures[index] !== texture) { | ||
this.textures[index] = texture; | ||
this.dirty = true; | ||
} | ||
} | ||
|
||
/** | ||
* Frees resources associated with this bind group. | ||
*/ | ||
destroy() { | ||
this.impl.destroy(); | ||
} | ||
|
||
/** | ||
* Applies any changes made to the bind group's properties. | ||
*/ | ||
update() { | ||
if (this.dirty) { | ||
this.dirty = false; | ||
this.impl.update(this); | ||
} | ||
} | ||
} | ||
|
||
export { BindGroup }; |
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
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
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,107 @@ | ||
import { Debug } from '../core/debug.js'; | ||
import { | ||
UNIFORMTYPE_BOOL, UNIFORMTYPE_INT, UNIFORMTYPE_FLOAT, UNIFORMTYPE_VEC2, UNIFORMTYPE_VEC3, | ||
UNIFORMTYPE_VEC4, UNIFORMTYPE_IVEC2, UNIFORMTYPE_IVEC3, UNIFORMTYPE_IVEC4, UNIFORMTYPE_BVEC2, | ||
UNIFORMTYPE_BVEC3, UNIFORMTYPE_BVEC4, UNIFORMTYPE_MAT4 | ||
} from './constants.js'; | ||
|
||
/** @typedef {import('./uniform-buffer.js').UniformBuffer} UniformBuffer */ | ||
|
||
// map of UNIFORMTYPE_*** to byte size | ||
const uniformTypeToByteSize = []; | ||
uniformTypeToByteSize[UNIFORMTYPE_FLOAT] = 4; | ||
uniformTypeToByteSize[UNIFORMTYPE_VEC2] = 8; | ||
uniformTypeToByteSize[UNIFORMTYPE_VEC3] = 12; | ||
uniformTypeToByteSize[UNIFORMTYPE_VEC4] = 16; | ||
uniformTypeToByteSize[UNIFORMTYPE_INT] = 4; | ||
uniformTypeToByteSize[UNIFORMTYPE_IVEC2] = 8; | ||
uniformTypeToByteSize[UNIFORMTYPE_IVEC3] = 12; | ||
uniformTypeToByteSize[UNIFORMTYPE_IVEC4] = 16; | ||
uniformTypeToByteSize[UNIFORMTYPE_BOOL] = 4; | ||
uniformTypeToByteSize[UNIFORMTYPE_BVEC2] = 8; | ||
uniformTypeToByteSize[UNIFORMTYPE_BVEC3] = 12; | ||
uniformTypeToByteSize[UNIFORMTYPE_BVEC4] = 16; | ||
uniformTypeToByteSize[UNIFORMTYPE_MAT4] = 64; | ||
|
||
// Handle additiona types: | ||
// UNIFORMTYPE_MAT2 = 12; | ||
// UNIFORMTYPE_MAT3 = 13; | ||
// UNIFORMTYPE_TEXTURE2D = 15; | ||
// UNIFORMTYPE_TEXTURECUBE = 16; | ||
// UNIFORMTYPE_FLOATARRAY = 17; | ||
// UNIFORMTYPE_TEXTURE2D_SHADOW = 18; | ||
// UNIFORMTYPE_TEXTURECUBE_SHADOW = 19; | ||
// UNIFORMTYPE_TEXTURE3D = 20; | ||
// UNIFORMTYPE_VEC2ARRAY = 21; | ||
// UNIFORMTYPE_VEC3ARRAY = 22; | ||
// UNIFORMTYPE_VEC4ARRAY = 23; | ||
|
||
/** | ||
* A class storing description of an individual uniform, stored inside a uniform buffer. | ||
* | ||
* @ignore | ||
*/ | ||
class UniformFormat { | ||
slimbuck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** @type {string} */ | ||
name; | ||
|
||
// UNIFORMTYPE_*** | ||
/** @type {number} */ | ||
type; | ||
|
||
/** @type {number} */ | ||
byteSize; | ||
|
||
/** | ||
* Index of the uniform in an array of 32bit values (Float32Array and similar) | ||
* | ||
* @type {number} | ||
*/ | ||
offset; | ||
|
||
// TODO: add count for arrays | ||
|
||
constructor(name, type) { | ||
this.name = name; | ||
this.type = type; | ||
this.byteSize = uniformTypeToByteSize[type]; | ||
Debug.assert(this.byteSize, `Unknown byte size for uniform format ${type} used for ${name}`); | ||
} | ||
} | ||
|
||
/** | ||
* A descriptor that defines the layout of of data inside the {@link UniformBuffer}. | ||
* | ||
* @ignore | ||
*/ | ||
class UniformBufferFormat { | ||
/** @type {number} */ | ||
byteSize = 0; | ||
|
||
/** @type {Map<string,UniformFormat>} */ | ||
map = new Map(); | ||
|
||
/** | ||
* Create a new UniformBufferFormat instance. | ||
* | ||
* @param {UniformFormat[]} uniforms - An array of uniforms to be stored in the buffer | ||
*/ | ||
constructor(uniforms) { | ||
/** @type {UniformFormat[]} */ | ||
this.uniforms = uniforms; | ||
|
||
// TODO: optimize uniforms ordering | ||
|
||
let byteSize = 0; | ||
for (let i = 0; i < uniforms.length; i++) { | ||
const uniform = uniforms[i]; | ||
uniform.offset = byteSize / 4; | ||
byteSize += uniform.byteSize; | ||
|
||
this.map.set(uniform.name, uniform); | ||
} | ||
this.byteSize = byteSize; | ||
} | ||
} | ||
|
||
export { UniformFormat, UniformBufferFormat }; |
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,75 @@ | ||
import { Debug } from '../core/debug.js'; | ||
|
||
/** @typedef {import('./graphics-device.js').GraphicsDevice} GraphicsDevice */ | ||
/** @typedef {import('./uniform-buffer-format.js').UniformBufferFormat} UniformBufferFormat */ | ||
|
||
/** | ||
* A uniform buffer represents a GPU memory buffer storing the uniforms. | ||
* | ||
* @ignore | ||
*/ | ||
class UniformBuffer { | ||
/** | ||
* Create a new UniformBuffer instance. | ||
* | ||
* @param {GraphicsDevice} graphicsDevice - The graphics device used to manage this uniform buffer. | ||
* @param {UniformBufferFormat} format - Format of the uniform buffer | ||
*/ | ||
constructor(graphicsDevice, format) { | ||
this.device = graphicsDevice; | ||
this.format = format; | ||
|
||
this.impl = graphicsDevice.createUniformBufferImpl(this); | ||
|
||
|
||
// TODO: maybe size rounding up should be done here and not per platform | ||
|
||
|
||
this.storage = new ArrayBuffer(format.byteSize); | ||
this.storageFloat32 = new Float32Array(this.storage); | ||
|
||
// TODO: register with the device and handle lost context | ||
// this.device.buffers.push(this); | ||
} | ||
|
||
/** | ||
* Frees resources associated with this uniform buffer. | ||
*/ | ||
destroy() { | ||
|
||
// // stop tracking the vertex buffer | ||
const device = this.device; | ||
|
||
// TODO: remove the buffer from the list on the device (lost context handling) | ||
|
||
this.impl.destroy(device); | ||
|
||
// TODO: track used memory | ||
// device._vram.vb -= this.storage.byteLength; | ||
} | ||
|
||
/** | ||
* Called when the rendering context was lost. It releases all context related resources. | ||
* | ||
* @ignore | ||
*/ | ||
loseContext() { | ||
this.impl.loseContext(); | ||
} | ||
|
||
set(name, value) { | ||
const uniform = this.format.map.get(name); | ||
Debug.assert(uniform, `Uniform [${name}] is not part of the Uniform buffer.`); | ||
if (uniform) { | ||
const offset = uniform.offset; | ||
this.storageFloat32.set(value, offset); | ||
slimbuck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
update() { | ||
// Upload the new data | ||
this.impl.unlock(this); | ||
} | ||
} | ||
|
||
export { UniformBuffer }; |
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
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.