Skip to content

Commit

Permalink
first pass
Browse files Browse the repository at this point in the history
  • Loading branch information
aardgoose committed Nov 4, 2023
1 parent 5642032 commit 4489ce9
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
6 changes: 6 additions & 0 deletions examples/jsm/renderers/webgl/WebGLBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ class WebGLBackend extends Backend {

}

async getArrayBufferAsync( attribute ) {

return await this.attributeUtils.getArrayBufferAsync( attribute );

}

beginRender( renderContext ) {

const { gl } = this;
Expand Down
72 changes: 72 additions & 0 deletions examples/jsm/renderers/webgl/utils/WebGLAttributeUtils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { arrayBuffer } from "../../../nodes/Nodes";

class WebGLAttributeUtils {

constructor( backend ) {
Expand Down Expand Up @@ -114,6 +116,76 @@ class WebGLAttributeUtils {

}

async getArrayBufferAsync( attribute ) {

const bufferAttribute = attribute.isInterleavedBufferAttribute ? attribute.data : attribute;
const { bufferType, bufferGPU, bytesPerElement } = backend.get( bufferAttribute );

const length = attribute.array.length * bytesPerElement;

gl.bindBuffer( bufferType, bufferGPU );

const writeBuffer = gl.createBuffer();

gl.bindBuffer(gl.COPY_WRITE_BUFFER, writeBuffer );
gl.bufferData(gl.COPY_WRITE_BUFFER, dest.byteLength, gl.STREAM_READ);

gl.copyBufferSubData( bufferType, gl.COPY_WRITE_BUFFER, 0, 0, length );

await this._clientWaitAsync();

const dstBuffer = new attribute.array.constructor( length );

gl.getBufferSubData( gl.COPY_WRITE_BUFFER, 0, dstBuffer );

gl.deleteBuffer( writeBuffer );

return dstBuffer;

}

_clientWaitAsync() {

const { gl } = this;

const sync = gl.fenceSync( gl.SYNC_GPU_COMMANDS_COMPLETE, 0 );

gl.flush();

return new Promise( ( resolve, reject ) => {

function test() {

const res = gl.clientWaitSync( sync, gl.SYNC_FLUSH_COMMANDS_BIT, 0 );

if ( res === gl.WAIT_FAILED) {

gl.deleteSync( sync );

reject();
return;

}

if ( res === gl.TIMEOUT_EXPIRED) {

requestAnimationFrame( test );
return;

}

gl.deleteSync( sync );

resolve();

}

test();

} );

}

}

export default WebGLAttributeUtils;

0 comments on commit 4489ce9

Please sign in to comment.