Skip to content
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

exposition only: potential video texture logic #2310

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions modules/webgl/src/adapter/resources/video.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {Texture, ExternalTexture} from "@luma.gl/api";

// @ts-expect-error
const {HAVE_CURRENT_DATA, HAVE_METADATA} = HTMLVideoElement;

type ResourceProps = {};

export type ExternalTextureProps = ResourceProps & {
source: HTMLVideoElement;
colorSpace?: 'srgb';
}

abstract class Video {
protected _video: HTMLVideoElement;
protected _videoLoaded: boolean = false;
protected _lastTime: number = -1

constructor(props) {
if (this._video.readyState >= HAVE_METADATA) {
this._videoLoaded = true;
} else {
this._video.addEventListener('loadeddata', () => {
this._videoLoaded = true;
this._initializeTexture();
});
}

lastTime: this._video.readyState >= HAVE_CURRENT_DATA ? this._video.currentTime : -1
}

abstract getCurrentFrame(): Texture | ExternalTexture;

abstract _initializeTexture(): void;

/** Check if video has advanced and a texture should be generated */
protected _updateTime(): boolean {
if (this._lastTime === this._video.currentTime || this._video.readyState < HAVE_CURRENT_DATA) {
return false;
}
this._lastTime = this._video.currentTime;
return true;
}
}
60 changes: 60 additions & 0 deletions modules/webgl/src/adapter/resources/webgl-video.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {ExternalTexture} from "@luma.gl/api/";
import Video from './video';


export default class WEBGLVideo extends Video {
constructor(props) {
super(props);
}

_initializeTexture() {

}

getCurrentFrame(): ExternalTexture {
// @ts-expect-error
if (lastTime === this._video.currentTime || this._video.readyState < HTMLVideoElement.HAVE_CURRENT_DATA) {
return;
}
this.setSubImageData({
data: this._video,
parameters
});
if (this.mipmaps) {
this.generateMipmap();
}
this._video.lastTime = this._video.currentTime;
}
}

export default class WebGPUVideo extends Video {
readonly device: WebGPUDevice;
readonly handle: GPUExternalTexture;
sampler: WebGPUSampler;

constructor(device: WebGPUDevice, props: ExternalTextureProps) {
super(device, props);
this.device = device;
this.handle = this.props.handle || this.device.handle.importExternalTexture({
source: props.source,
colorSpace: props.colorSpace
});
this.sampler = null;
}

destroy(): void {
// External textures are destroyed automatically,
// as a microtask, instead of manually or upon garbage collection like other resources.
// this.handle.destroy();
}

getCurrentFrame(): ExternalTexture {
return new WEBGPUExternalTexture(this.device, this.props);
}
/** Set default sampler */
setSampler(sampler: Sampler | SamplerProps): this {
// We can accept a sampler instance or set of props;
this.sampler = sampler instanceof WebGPUSampler ? sampler : new WebGPUSampler(this.device, sampler);
return this;
}
}
Loading