@@ -189,17 +189,19 @@ export function isUsableAsIndex<T extends TgpuBuffer<BaseData>>(
189189const endianness = getSystemEndianness ( ) ;
190190
191191class TgpuBufferImpl < TData extends BaseData > implements TgpuBuffer < TData > {
192- public readonly [ $internal ] = true ;
193- public readonly resourceType = 'buffer' ;
194- public flags : GPUBufferUsageFlags = GPUBufferUsage . COPY_DST | GPUBufferUsage . COPY_SRC ;
192+ readonly [ $internal ] = true ;
193+ readonly resourceType = 'buffer' ;
194+ flags : GPUBufferUsageFlags = GPUBufferUsage . COPY_DST | GPUBufferUsage . COPY_SRC ;
195+ readonly dataType : TData ;
195196
196197 readonly #device: GPUDevice ;
197- private _buffer : GPUBuffer | null = null ;
198- private _ownBuffer : boolean ;
199- private _destroyed = false ;
200- private _hostBuffer : ArrayBuffer | undefined ;
201- private _mappedRange : ArrayBuffer | undefined ;
202- private _initialCallback : BufferInitCallback < TData > | undefined ;
198+ #buffer: GPUBuffer | null = null ;
199+ #ownBuffer: boolean ;
200+ #destroyed = false ;
201+ #hostBuffer: ArrayBuffer | undefined ;
202+ #mappedRange: ArrayBuffer | undefined ;
203+ #initialCallback: BufferInitCallback < TData > | undefined ;
204+ readonly #disallowedUsages: UsageLiteral [ ] | undefined ;
203205
204206 readonly initial : InferInput < TData > | undefined ;
205207
@@ -211,52 +213,54 @@ class TgpuBufferImpl<TData extends BaseData> implements TgpuBuffer<TData> {
211213
212214 constructor (
213215 root : ExperimentalTgpuRoot ,
214- public readonly dataType : TData ,
216+ dataType : TData ,
215217 initialOrBuffer ?: BufferInitialData < TData > | GPUBuffer ,
216- private readonly _disallowedUsages ?: UsageLiteral [ ] ,
218+ disallowedUsages ?: UsageLiteral [ ] ,
217219 ) {
220+ this . dataType = dataType ;
221+ this . #disallowedUsages = disallowedUsages ;
218222 this . #device = root . device ;
219223 if ( isGPUBuffer ( initialOrBuffer ) ) {
220- this . _ownBuffer = false ;
221- this . _buffer = initialOrBuffer ;
224+ this . #ownBuffer = false ;
225+ this . #buffer = initialOrBuffer ;
222226 } else {
223- this . _ownBuffer = true ;
227+ this . #ownBuffer = true ;
224228 if ( typeof initialOrBuffer === 'function' ) {
225- this . _initialCallback = initialOrBuffer as BufferInitCallback < TData > ;
229+ this . #initialCallback = initialOrBuffer as BufferInitCallback < TData > ;
226230 } else {
227231 this . initial = initialOrBuffer ;
228232 }
229233 }
230234 }
231235
232236 get buffer ( ) {
233- if ( this . _destroyed ) {
237+ if ( this . #destroyed ) {
234238 throw new Error ( 'This buffer has been destroyed' ) ;
235239 }
236240
237- if ( ! this . _buffer ) {
238- this . _buffer = this . #device. createBuffer ( {
241+ if ( ! this . #buffer ) {
242+ this . #buffer = this . #device. createBuffer ( {
239243 size : sizeOf ( this . dataType ) ,
240244 usage : this . flags ,
241- mappedAtCreation : ! ! this . initial || ! ! this . _initialCallback ,
245+ mappedAtCreation : ! ! this . initial || ! ! this . #initialCallback ,
242246 label : getName ( this ) ?? '<unnamed>' ,
243247 } ) ;
244248
245- if ( this . initial || this . _initialCallback ) {
246- if ( this . _initialCallback ) {
247- this . _initialCallback ( this ) ;
249+ if ( this . initial || this . #initialCallback ) {
250+ if ( this . #initialCallback ) {
251+ this . #initialCallback ( this ) ;
248252 } else if ( this . initial ) {
249253 this . #writeToTarget( this . #getMappedRange( ) , this . initial ) ;
250254 }
251255 this . #unmapBuffer( ) ;
252256 }
253257 }
254258
255- return this . _buffer ;
259+ return this . #buffer ;
256260 }
257261
258262 get destroyed ( ) {
259- return this . _destroyed ;
263+ return this . #destroyed ;
260264 }
261265
262266 get arrayBuffer ( ) : ArrayBuffer {
@@ -265,35 +269,35 @@ class TgpuBufferImpl<TData extends BaseData> implements TgpuBuffer<TData> {
265269 return this . #getMappedRange( ) ;
266270 }
267271
268- if ( ! this . _hostBuffer ) {
269- this . _hostBuffer = new ArrayBuffer ( sizeOf ( this . dataType ) ) ;
272+ if ( ! this . #hostBuffer ) {
273+ this . #hostBuffer = new ArrayBuffer ( sizeOf ( this . dataType ) ) ;
270274 }
271275
272- return this . _hostBuffer ;
276+ return this . #hostBuffer ;
273277 }
274278
275279 #getMappedRange( ) : ArrayBuffer {
276- if ( ! this . _buffer || this . _buffer . mapState !== 'mapped' ) {
280+ if ( ! this . #buffer || this . #buffer . mapState !== 'mapped' ) {
277281 throw new Error ( 'Buffer is not mapped.' ) ;
278282 }
279283
280- this . _mappedRange ??= this . _buffer . getMappedRange ( ) ;
281- return this . _mappedRange ;
284+ this . #mappedRange ??= this . #buffer . getMappedRange ( ) ;
285+ return this . #mappedRange ;
282286 }
283287
284288 #unmapBuffer( ) : void {
285- if ( ! this . _buffer || this . _buffer . mapState !== 'mapped' ) {
289+ if ( ! this . #buffer || this . #buffer . mapState !== 'mapped' ) {
286290 return ;
287291 }
288292
289- this . _mappedRange = undefined ;
290- this . _buffer . unmap ( ) ;
293+ this . #mappedRange = undefined ;
294+ this . #buffer . unmap ( ) ;
291295 }
292296
293297 $name ( label : string ) {
294298 setName ( this , label ) ;
295- if ( this . _buffer ) {
296- this . _buffer . label = label ;
299+ if ( this . #buffer ) {
300+ this . #buffer . label = label ;
297301 }
298302 return this ;
299303 }
@@ -302,7 +306,7 @@ class TgpuBufferImpl<TData extends BaseData> implements TgpuBuffer<TData> {
302306 ...usages : T
303307 ) : this & UnionToIntersection < LiteralToUsageType < T [ number ] > > {
304308 for ( const usage of usages ) {
305- if ( this . _disallowedUsages ?. includes ( usage ) ) {
309+ if ( this . #disallowedUsages ?. includes ( usage ) ) {
306310 throw new Error ( `Buffer of type ${ this . dataType . type } cannot be used as ${ usage } ` ) ;
307311 }
308312
@@ -321,7 +325,7 @@ class TgpuBufferImpl<TData extends BaseData> implements TgpuBuffer<TData> {
321325 }
322326
323327 $addFlags ( flags : GPUBufferUsageFlags ) {
324- if ( ! this . _ownBuffer ) {
328+ if ( ! this . #ownBuffer ) {
325329 throw new Error ( 'Cannot add flags to a buffer that is not managed by TypeGPU.' ) ;
326330 }
327331
@@ -426,16 +430,16 @@ class TgpuBufferImpl<TData extends BaseData> implements TgpuBuffer<TData> {
426430 return ;
427431 }
428432
429- if ( ! this . _hostBuffer ) {
430- this . _hostBuffer = new ArrayBuffer ( bufferSize ) ;
433+ if ( ! this . #hostBuffer ) {
434+ this . #hostBuffer = new ArrayBuffer ( bufferSize ) ;
431435 }
432436
433- // If the caller already wrote directly into _hostBuffer via
437+ // If the caller already wrote directly into #hostBuffer via
434438 // arrayBuffer, skip the redundant copy, the data is already in place.
435- if ( ! ( data instanceof ArrayBuffer && data === this . _hostBuffer ) ) {
436- this . #writeToTarget( this . _hostBuffer , data , options ) ;
439+ if ( ! ( data instanceof ArrayBuffer && data === this . #hostBuffer ) ) {
440+ this . #writeToTarget( this . #hostBuffer , data , options ) ;
437441 }
438- this . #device. queue . writeBuffer ( gpuBuffer , startOffset , this . _hostBuffer , startOffset , size ) ;
442+ this . #device. queue . writeBuffer ( gpuBuffer , startOffset , this . #hostBuffer , startOffset , size ) ;
439443 }
440444
441445 public writePartial ( data : InferPartial < TData > ) : void {
@@ -527,13 +531,13 @@ class TgpuBufferImpl<TData extends BaseData> implements TgpuBuffer<TData> {
527531 }
528532
529533 destroy ( ) {
530- if ( this . _destroyed ) {
534+ if ( this . #destroyed ) {
531535 return ;
532536 }
533- this . _destroyed = true ;
534- this . _mappedRange = undefined ;
535- if ( this . _ownBuffer ) {
536- this . _buffer ?. destroy ( ) ;
537+ this . #destroyed = true ;
538+ this . #mappedRange = undefined ;
539+ if ( this . #ownBuffer ) {
540+ this . #buffer ?. destroy ( ) ;
537541 }
538542 }
539543
0 commit comments