import { ResourceState, GPUTestBase } from '../gpu_test.js';
export const kRenderEncodeTypes = ['render pass', 'render bundle'] as const;
export type RenderEncodeType = (typeof kRenderEncodeTypes)[number];
export const kProgrammableEncoderTypes = ['compute pass', ...kRenderEncodeTypes] as const;
export type ProgrammableEncoderType = (typeof kProgrammableEncoderTypes)[number];
export const kEncoderTypes = ['non-pass', ...kProgrammableEncoderTypes] as const;
export type EncoderType = (typeof kEncoderTypes)[number];
type EncoderByEncoderType<T extends EncoderType> = {
'non-pass': GPUCommandEncoder;
'compute pass': GPUComputePassEncoder;
'render pass': GPURenderPassEncoder;
'render bundle': GPURenderBundleEncoder;
}[T];
export class CommandBufferMaker<T extends EncoderType> {
readonly encoder: EncoderByEncoderType<T>;
* Finish any passes, finish and record any bundles, and finish/return the command buffer. Any
* errors are ignored and the GPUCommandBuffer (which may be an error buffer) is returned.
*/
readonly finish: () => GPUCommandBuffer;
* Finish any passes, finish and record any bundles, and finish/return the command buffer.
* Checks for validation errors in (only) the appropriate finish call.
*/
readonly validateFinish: (shouldSucceed: boolean) => GPUCommandBuffer;
* Finish the command buffer and submit it. Checks for validation errors in either the submit or
* the appropriate finish call, depending on the state of a resource used in the encoding.
*/
readonly validateFinishAndSubmit: (
shouldBeValid: boolean,
submitShouldSucceedIfValid: boolean
) => void;
* `validateFinishAndSubmit()` based on the state of a resource in the command encoder.
* - `finish()` should fail if the resource is 'invalid'.
* - Only `submit()` should fail if the resource is 'destroyed'.
*/
readonly validateFinishAndSubmitGivenState: (resourceState: ResourceState) => void;
constructor(
t: GPUTestBase,
encoder: EncoderByEncoderType<EncoderType>,
finish: () => GPUCommandBuffer
) {
this.encoder = encoder as EncoderByEncoderType<T>;
this.finish = finish;
this.validateFinish = (shouldSucceed: boolean) => {
return t.expectGPUError('validation', this.finish, !shouldSucceed);
};
this.validateFinishAndSubmit = (
shouldBeValid: boolean,
submitShouldSucceedIfValid: boolean
) => {
const commandBuffer = this.validateFinish(shouldBeValid);
if (shouldBeValid) {
t.expectValidationError(() => t.queue.submit([commandBuffer]), !submitShouldSucceedIfValid);
}
};
this.validateFinishAndSubmitGivenState = (resourceState: ResourceState) => {
this.validateFinishAndSubmit(resourceState !== 'invalid', resourceState !== 'destroyed');
};
}
}