Skip to content

Commit

Permalink
Port #1224
Browse files Browse the repository at this point in the history
  • Loading branch information
1chandu committed Sep 6, 2019
1 parent 92f9cf2 commit 423cfbc
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 44 deletions.
5 changes: 0 additions & 5 deletions modules/core/src/lib/transform/buffer-transform-binding.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import {isObjectEmpty} from '../../utils';

import {TransformFeedback} from '@luma.gl/webgl';

// swapping of two instances, instead of maintaing two arra
Expand Down Expand Up @@ -30,9 +28,6 @@ export default class BufferTransformBinding {
// setup TransformFeedback objects to capture the results
setupTransformFeedback({model}) {
const {program} = model;
if (isObjectEmpty(this.feedbackBuffers)) {
return;
}
this.transformFeedback = new TransformFeedback(this.gl, {
program,
buffers: this.feedbackBuffers
Expand Down
34 changes: 21 additions & 13 deletions modules/core/src/lib/transform/buffer-transform.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {isWebGL2, Buffer} from '@luma.gl/webgl';
import {isObjectEmpty, assert} from '../../utils';
import {assert} from '../../utils';
import BufferTransformBinding from './buffer-transform-binding';

export default class BufferTransform {
constructor(gl, props = {}) {
this.gl = gl;
this.currentIndex = 0;
this.feedbackMap = null;
this.feedbackMap = {};
this.varyings = null; // varyings array
this.bindings = [];

Expand Down Expand Up @@ -45,11 +45,7 @@ export default class BufferTransform {

// update source and/or feedbackBuffers
update(opts = {}) {
const {sourceBuffers = null} = opts;
if (sourceBuffers || opts.feedbackBuffers) {
const feedbackBuffers = this.getFeedbackBuffers(opts);
this.updateBindings({sourceBuffers, feedbackBuffers});
}
this._setupBuffers(opts);
}

// returns current feedbackBuffer of given name
Expand Down Expand Up @@ -80,22 +76,34 @@ export default class BufferTransform {
// Private

initialize(props = {}) {
const {sourceBuffers, feedbackMap} = props;
this.feedbackMap = feedbackMap;
const feedbackBuffers = this.getFeedbackBuffers(props);
if (!isObjectEmpty(props.feedbackBuffers)) {
this._setupBuffers(props);
const currentBuffers = this.bindings[this.currentIndex].getBuffers();
this.varyings = props.varyings || Object.keys(currentBuffers.feedbackBuffers);
if (this.varyings.length > 0) {
// if writting to buffers make sure it is WebGL2
assert(isWebGL2(this.gl));
}
this.varyings = props.varyings || Object.keys(feedbackBuffers);
}

_setupBuffers(props = {}) {
const {sourceBuffers = null} = props;
Object.assign(this.feedbackMap, props.feedbackMap);
// if (sourceBuffers || props.feedbackBuffers) {
const feedbackBuffers = this.getFeedbackBuffers(props);
this.updateBindings({sourceBuffers, feedbackBuffers});
// TODO: setup this.varyings
// }
}

// auto create feedback buffers if requested
getFeedbackBuffers(props) {
const {sourceBuffers} = props;
const feedbackBuffers = {};
if (this.bindings[this.currentIndex]) {
// this gurantees a partial feedback buffer set doesn't update
// previously set buffers during auto creation mode.
const currentBuffers = this.bindings[this.currentIndex].getBuffers();
Object.assign(feedbackBuffers, currentBuffers.feedbackBuffers);
}
if (this.feedbackMap) {
// feedbackMap is defined as sourceBuffer as key and feedbackBuffer name as object
for (const sourceName in this.feedbackMap) {
Expand Down
46 changes: 27 additions & 19 deletions modules/core/src/lib/transform/texture-transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,32 @@ export default class TextureTransform {

// update source and/or feedbackBuffers
update(opts = {}) {
const {_sourceTextures = null, _targetTexture} = opts;
if (_sourceTextures || _targetTexture) {
const targetTexture = this.createTargetTexture({
sourceTextures: _sourceTextures,
// if targetTexture created using source texture, and that sourceTextuer
// is updated we should update targetTexture
textureOrReference: _targetTexture || this._targetRefTexName
});
this.updateBindings({sourceTextures: _sourceTextures, targetTexture});
// const {_sourceTextures = null, _targetTexture} = opts;
// if (_sourceTextures || _targetTexture) {
// const targetTexture = this.createTargetTexture({
// sourceTextures: _sourceTextures,
// // if targetTexture created using source texture, and that sourceTextuer
// // is updated we should update targetTexture
// textureOrReference: _targetTexture || this._targetRefTexName
// });
// this.updateBindings({sourceTextures: _sourceTextures, targetTexture});
// }
// this.updateElementIDBuffer(opts.elementCount);
this._setupTextures(opts);
}

_setupTextures(props = {}) {
const {_sourceTextures = {}, _targetTexture} = props;
const targetTexture = this.createTargetTexture({
sourceTextures: _sourceTextures,
textureOrReference: _targetTexture
});
this.hasSourceTextures =
this.hasSourceTextures || (_sourceTextures && Object.keys(_sourceTextures).length > 0);
this.updateBindings({sourceTextures: _sourceTextures, targetTexture});
if ('elementCount' in props) {
this.updateElementIDBuffer(props.elementCount);
}
this.updateElementIDBuffer(opts.elementCount);
}

// returns current target texture
Expand Down Expand Up @@ -152,18 +167,11 @@ export default class TextureTransform {
// Private

initialize(props = {}) {
const {_sourceTextures, _targetTexture, _targetTextureVarying, _swapTexture} = props;
const {_targetTextureVarying, _swapTexture} = props;
this._swapTexture = _swapTexture;
this.targetTextureVarying = _targetTextureVarying;
const targetTexture = this.createTargetTexture({
sourceTextures: _sourceTextures,
textureOrReference: _targetTexture
});
this.hasSourceTextures = _sourceTextures && Object.keys(_sourceTextures).length > 0;
this.hasTargetTexture = _targetTextureVarying;
this.updateElementIDBuffer(props.elementCount);
this.updateBindings({sourceTextures: _sourceTextures, targetTexture});
// TODO: setup this.varyings
this._setupTextures(props);
}

// auto create target texture if requested
Expand Down
11 changes: 6 additions & 5 deletions modules/core/src/lib/transform/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,11 @@ export default class Transform {
}

canCreateBufferTransform(props) {
if (!isObjectEmpty(props.sourceBuffers)) {
return true;
}
if (!isObjectEmpty(props.feedbackBuffers)) {
if (
!isObjectEmpty(props.sourceBuffers) ||
!isObjectEmpty(props.feedbackBuffers) ||
(props.varyings && props.varyings.length > 0)
) {
return true;
}
return false;
Expand All @@ -148,7 +149,7 @@ export default class Transform {
if (!isObjectEmpty(props._sourceTextures)) {
return true;
}
if (props._targetTexture) {
if (props._targetTexture || props._targetTextureVarying) {
return true;
}

Expand Down
74 changes: 72 additions & 2 deletions modules/core/test/lib/transform.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,19 +425,28 @@ test('WebGL#Transform update', t => {
let outData;

const transform = new Transform(gl2, {
vs: VS,
varyings: ['outValue']
});

t.ok(transform, 'should construct without buffers');

transform.update({
sourceBuffers: {
inValue: sourceBuffer
},
vs: VS,
feedbackMap: {
inValue: 'outValue'
},
varyings: ['outValue'],
elementCount: 5
});

transform.run();

expectedData = sourceData.map(x => x * 2);
outData = transform.getData({varyingName: 'outValue'});
t.deepEqual(outData, expectedData, 'Transform.getData: is successful');

sourceData = new Float32Array([1, 2, 3, 0, -5]);
sourceBuffer.delete();
sourceBuffer = new Buffer(gl2, {data: sourceData});
Expand Down Expand Up @@ -831,6 +840,67 @@ test('WebGL#Transform run (source&destination texture)', t => {
t.end();
});

/*
test.only('WebGL#Transform update (source&destination texture)', t => {
const {gl2} = fixture;
if (!gl2) {
t.comment('WebGL2 not available, skipping tests');
t.end();
return;
}
const {sourceData, format, dataFormat, type, width, height, name, vs} = TEXTURE_TEST_CASES[0];
const sourceTexture = new Texture2D(gl2, {
data: sourceData,
format,
dataFormat,
type,
mipmaps: false,
width,
height,
pixelStore: {
[GL.UNPACK_FLIP_Y_WEBGL]: false
}
});
const transform = new Transform(gl2, {
_targetTextureVarying: 'outTexture',
_swapTexture: 'inTexture',
vs
});
transform.update({
_sourceTextures: {
inTexture: sourceTexture
},
_targetTexture: 'inTexture',
elementCount: sourceData.length
})
transform.run();
let expectedData = sourceData.map(x => x * 2);
// By default getData reads data from current Framebuffer.
let outTexData = transform.getData({packed: true});
t.deepEqual(
outTexData,
expectedData,
`${name} Transform should write correct data into Texture`
);
transform.swap();
transform.run();
expectedData = sourceData.map(x => x * 4);
// By default getData reads data from current Framebuffer.
outTexData = transform.getData({packed: true});
t.deepEqual(outTexData, expectedData, `${name} Transform swap Textures`);
t.end();
});
*/

test('WebGL#Transform run (source&destination texture update)', t => {
const {gl2} = fixture;

Expand Down

0 comments on commit 423cfbc

Please sign in to comment.