UNPKG

@luma.gl/core

Version:

The luma.gl core Device API

89 lines 3.53 kB
// luma.gl // SPDX-License-Identifier: MIT // Copyright (c) vis.gl contributors import { Resource } from "./resource.js"; /** * Records commands onto a single backend command encoder and can finish them into one command * buffer. Resource helpers invoked through a CommandEncoder must record onto that encoder rather * than allocating hidden encoders or submitting work eagerly. */ export class CommandEncoder extends Resource { get [Symbol.toStringTag]() { return 'CommandEncoder'; } _timeProfilingQuerySet = null; _timeProfilingSlotCount = 0; _gpuTimeMs; constructor(device, props) { super(device, props, CommandEncoder.defaultProps); this._timeProfilingQuerySet = props.timeProfilingQuerySet ?? null; this._timeProfilingSlotCount = 0; this._gpuTimeMs = undefined; } /** * Reads all resolved timestamp pairs on the current profiler query set and caches the sum * as milliseconds on this encoder. */ async resolveTimeProfilingQuerySet() { this._gpuTimeMs = undefined; if (!this._timeProfilingQuerySet) { return; } const pairCount = Math.floor(this._timeProfilingSlotCount / 2); if (pairCount <= 0) { return; } const queryCount = pairCount * 2; const results = await this._timeProfilingQuerySet.readResults({ firstQuery: 0, queryCount }); let totalDurationNanoseconds = 0n; for (let queryIndex = 0; queryIndex < queryCount; queryIndex += 2) { totalDurationNanoseconds += results[queryIndex + 1] - results[queryIndex]; } this._gpuTimeMs = Number(totalDurationNanoseconds) / 1e6; } /** Returns the number of query slots consumed by automatic pass profiling on this encoder. */ getTimeProfilingSlotCount() { return this._timeProfilingSlotCount; } getTimeProfilingQuerySet() { return this._timeProfilingQuerySet; } /** Internal helper for auto-assigning timestamp slots to render/compute passes on this encoder. */ _applyTimeProfilingToPassProps(props) { const passProps = (props || {}); if (!this._supportsTimestampQueries() || !this._timeProfilingQuerySet) { return passProps; } if (passProps.timestampQuerySet !== undefined || passProps.beginTimestampIndex !== undefined || passProps.endTimestampIndex !== undefined) { return passProps; } const beginTimestampIndex = this._timeProfilingSlotCount; if (beginTimestampIndex + 1 >= this._timeProfilingQuerySet.props.count) { return passProps; } this._timeProfilingSlotCount += 2; return { ...passProps, timestampQuerySet: this._timeProfilingQuerySet, beginTimestampIndex, endTimestampIndex: beginTimestampIndex + 1 }; } _supportsTimestampQueries() { return this.device.features.has('timestamp-query'); } // TODO - luma.gl has these on the device, should we align with WebGPU API? // beginRenderPass(GPURenderPassDescriptor descriptor): GPURenderPassEncoder; // beginComputePass(optional GPUComputePassDescriptor descriptor = {}): GPUComputePassEncoder; static defaultProps = { ...Resource.defaultProps, measureExecutionTime: undefined, timeProfilingQuerySet: undefined }; } //# sourceMappingURL=command-encoder.js.map