Skip to content

Commit 4018d68

Browse files
authored
Backend: Introduce createUniformBuffer() (#33413)
1 parent a7e4ed5 commit 4018d68

10 files changed

Lines changed: 330 additions & 128 deletions

File tree

examples/jsm/inspector/tabs/Memory.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ class Memory extends Tab {
7070
this.textures = new Item( 'Textures', createValueSpan(), createValueSpan() );
7171
this.memoryStats.add( this.textures );
7272

73+
this.uniformBuffers = new Item( 'Uniform Buffers', createValueSpan(), createValueSpan() );
74+
this.memoryStats.add( this.uniformBuffers );
75+
7376
this.graph = graph;
7477

7578
}
@@ -100,6 +103,7 @@ class Memory extends Tab {
100103

101104
setText( this.attributes.data[ 1 ], memory.attributes.toString() );
102105
setText( this.attributes.data[ 2 ], formatBytes( memory.attributesSize ) );
106+
103107
setText( this.geometries.data[ 1 ], memory.geometries.toString() );
104108

105109
setText( this.indexAttributes.data[ 1 ], memory.indexAttributes.toString() );
@@ -118,9 +122,13 @@ class Memory extends Tab {
118122

119123
setText( this.storageAttributes.data[ 1 ], memory.storageAttributes.toString() );
120124
setText( this.storageAttributes.data[ 2 ], formatBytes( memory.storageAttributesSize ) );
125+
121126
setText( this.textures.data[ 1 ], memory.textures.toString() );
122127
setText( this.textures.data[ 2 ], formatBytes( memory.texturesSize ) );
123128

129+
setText( this.uniformBuffers.data[ 1 ], memory.uniformBuffers.toString() );
130+
setText( this.uniformBuffers.data[ 2 ], formatBytes( memory.uniformBuffersSize ) );
131+
124132
}
125133

126134
}

examples/jsm/inspector/tabs/Timeline.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,11 +1044,29 @@ class Timeline extends Tab {
10441044
case 'updateBindings': {
10451045

10461046
const bindGroup = args[ 0 ];
1047-
const details = { group: bindGroup.name || 'unknown' };
10481047

1049-
if ( bindGroup.bindings ) {
1048+
const details = {
1049+
group: bindGroup.name || 'unknown',
1050+
count: bindGroup.bindings.length
1051+
};
1052+
1053+
return details;
1054+
1055+
}
1056+
1057+
case 'createUniformBuffer':
1058+
case 'destroyUniformBuffer': {
1059+
1060+
const binding = args[ 0 ];
1061+
1062+
const details = {
1063+
group: binding.groupNode.name || 'unknown',
1064+
size: binding.byteLength + ' bytes'
1065+
};
1066+
1067+
if ( binding.name !== details.group ) {
10501068

1051-
details.count = bindGroup.bindings.length;
1069+
details.name = binding.name;
10521070

10531071
}
10541072

src/renderers/common/Backend.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,22 @@ class Backend {
389389
*/
390390
createStorageAttribute( /*attribute*/ ) { }
391391

392+
/**
393+
* Creates a uniform buffer.
394+
*
395+
* @abstract
396+
* @param {Buffer} uniformBuffer - The uniform buffer.
397+
*/
398+
createUniformBuffer( /*uniformBuffer*/ ) { }
399+
400+
/**
401+
* Destroys a uniform buffer.
402+
*
403+
* @abstract
404+
* @param {Buffer} uniformBuffer - The uniform buffer.
405+
*/
406+
destroyUniformBuffer( /*uniformBuffer*/ ) { }
407+
392408
/**
393409
* Updates the GPU buffer of a shader attribute.
394410
*

src/renderers/common/Bindings.js

Lines changed: 97 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,15 @@ class Bindings extends DataMap {
7979

8080
const bindings = renderObject.getBindings();
8181

82-
for ( const bindGroup of bindings ) {
83-
84-
const groupData = this.get( bindGroup );
82+
const renderObjectData = this.get( renderObject );
8583

86-
if ( groupData.bindGroup === undefined ) {
84+
if ( renderObjectData.initialized !== true ) {
8785

88-
// each object defines an array of bindings (ubos, textures, samplers etc.)
86+
// bind groups are created once per object
8987

90-
this._init( bindGroup );
88+
this._createBindings( bindings );
9189

92-
this.backend.createBindings( bindGroup, bindings, 0 );
93-
94-
groupData.bindGroup = bindGroup;
95-
96-
}
90+
renderObjectData.initialized = true;
9791

9892
}
9993

@@ -110,20 +104,15 @@ class Bindings extends DataMap {
110104
getForCompute( computeNode ) {
111105

112106
const bindings = this.nodes.getForCompute( computeNode ).bindings;
107+
const computeNodeData = this.get( computeNode );
113108

114-
for ( const bindGroup of bindings ) {
115-
116-
const groupData = this.get( bindGroup );
117-
118-
if ( groupData.bindGroup === undefined ) {
109+
if ( computeNodeData.initialized !== true ) {
119110

120-
this._init( bindGroup );
111+
// bind groups are created once per object
121112

122-
this.backend.createBindings( bindGroup, bindings, 0 );
113+
this._createBindings( bindings );
123114

124-
groupData.bindGroup = bindGroup;
125-
126-
}
115+
computeNodeData.initialized = true;
127116

128117
}
129118

@@ -162,12 +151,9 @@ class Bindings extends DataMap {
162151

163152
const bindings = this.nodes.getForCompute( computeNode ).bindings;
164153

165-
for ( const bindGroup of bindings ) {
154+
this._destroyBindings( bindings );
166155

167-
this.backend.deleteBindGroupData( bindGroup );
168-
this.delete( bindGroup );
169-
170-
}
156+
this.delete( computeNode );
171157

172158
}
173159

@@ -180,60 +166,125 @@ class Bindings extends DataMap {
180166

181167
const bindings = renderObject.getBindings();
182168

183-
for ( const bindGroup of bindings ) {
184-
185-
this.backend.deleteBindGroupData( bindGroup );
186-
this.delete( bindGroup );
169+
this._destroyBindings( bindings );
187170

188-
}
171+
this.delete( renderObject );
189172

190173
}
191174

192175
/**
193-
* Updates the given array of bindings.
176+
* Creates the bindings for the given array of bindings.
194177
*
195178
* @param {Array<BindGroup>} bindings - The bind groups.
196179
*/
197-
_updateBindings( bindings ) {
180+
_createBindings( bindings ) {
198181

199182
for ( const bindGroup of bindings ) {
200183

201-
this._update( bindGroup, bindings );
184+
// binding group
185+
186+
const groupData = this.get( bindGroup );
187+
188+
if ( groupData.bindGroup === undefined ) {
189+
190+
// initialize
191+
192+
for ( const binding of bindGroup.bindings ) {
193+
194+
if ( binding.isUniformBuffer ) {
195+
196+
this.backend.createUniformBuffer( binding );
197+
this.info.createUniformBuffer( binding );
198+
199+
} else if ( binding.isSampledTexture ) {
200+
201+
this.textures.updateTexture( binding.texture );
202+
203+
} else if ( binding.isSampler ) {
204+
205+
this.textures.updateSampler( binding.texture );
206+
207+
} else if ( binding.isStorageBuffer ) {
208+
209+
const attribute = binding.attribute;
210+
const attributeType = attribute.isIndirectStorageBufferAttribute ? AttributeType.INDIRECT : AttributeType.STORAGE;
211+
212+
this.attributes.update( attribute, attributeType );
213+
214+
}
215+
216+
}
217+
218+
// each object defines an array of bindings (ubos, textures, samplers etc.)
219+
220+
this.backend.createBindings( bindGroup, bindings, 0 );
221+
222+
groupData.bindGroup = bindGroup;
223+
groupData.usedTimes = 1;
224+
225+
} else {
226+
227+
groupData.usedTimes ++;
228+
229+
}
202230

203231
}
204232

205233
}
206234

207235
/**
208-
* Initializes the given bind group.
236+
* Deletes the given array of bindings.
209237
*
210-
* @param {BindGroup} bindGroup - The bind group to initialize.
238+
* @param {Array<BindGroup>} bindings - The bind groups.
211239
*/
212-
_init( bindGroup ) {
240+
_destroyBindings( bindings ) {
213241

214-
for ( const binding of bindGroup.bindings ) {
242+
for ( const bindGroup of bindings ) {
215243

216-
if ( binding.isSampledTexture ) {
244+
const groupData = this.get( bindGroup );
245+
groupData.usedTimes --;
217246

218-
this.textures.updateTexture( binding.texture );
247+
if ( groupData.usedTimes === 0 ) {
219248

220-
} else if ( binding.isSampler ) {
249+
for ( const binding of bindGroup.bindings ) {
221250

222-
this.textures.updateSampler( binding.texture );
251+
if ( binding.isUniformBuffer ) {
223252

224-
} else if ( binding.isStorageBuffer ) {
253+
this.backend.destroyUniformBuffer( binding );
254+
this.info.destroyUniformBuffer( binding );
225255

226-
const attribute = binding.attribute;
227-
const attributeType = attribute.isIndirectStorageBufferAttribute ? AttributeType.INDIRECT : AttributeType.STORAGE;
256+
// release arrays
228257

229-
this.attributes.update( attribute, attributeType );
258+
binding.release();
259+
260+
}
261+
262+
}
263+
264+
this.backend.deleteBindGroupData( bindGroup );
265+
this.delete( bindGroup );
230266

231267
}
232268

233269
}
234270

235271
}
236272

273+
/**
274+
* Updates the given array of bindings.
275+
*
276+
* @param {Array<BindGroup>} bindings - The bind groups.
277+
*/
278+
_updateBindings( bindings ) {
279+
280+
for ( const bindGroup of bindings ) {
281+
282+
this._update( bindGroup, bindings );
283+
284+
}
285+
286+
}
287+
237288
/**
238289
* Updates the given bind group.
239290
*

src/renderers/common/Buffer.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,15 @@ class Buffer extends Binding {
122122

123123
}
124124

125+
/**
126+
* Releases the buffer.
127+
*/
128+
release() {
129+
130+
this._buffer = null;
131+
132+
}
133+
125134
}
126135

127136
export default Buffer;

0 commit comments

Comments
 (0)