diff --git a/package.json b/package.json index 37e1e32bc417660b8bf4649ac259511c3d116b83..f3cbef9247a0d5317e1165bc51d18a9d0b33ecd5 100644 --- a/package.json +++ b/package.json @@ -55,5 +55,7 @@ "vite": "^4.3.9", "vitest": "^0.32.2" }, - "dependencies": {} -} \ No newline at end of file + "dependencies": { + "@feng3d/reactivity": "^1.0.4" + } +} diff --git a/src/consts/vertexFormatMap.ts b/src/consts/vertexFormatMap.ts new file mode 100644 index 0000000000000000000000000000000000000000..01c7489102f14c3694a0c5c785c0fcb7a6f5c16f --- /dev/null +++ b/src/consts/vertexFormatMap.ts @@ -0,0 +1,142 @@ +import { VertexFormat } from "../data/VertexAttributes"; + +/** + * 顶点属性格式信息映射。 + */ +export const vertexFormatMap: Record = { + "uint8x2": { "numComponents": 2, "type": "UNSIGNED_BYTE", "normalized": false, "dataType": "unsigned int", "byteSize": 2, "wgslType": "vec2", "typedArrayConstructor": Uint8Array }, + "uint8x4": { "numComponents": 4, "type": "UNSIGNED_BYTE", "normalized": false, "dataType": "unsigned int", "byteSize": 4, "wgslType": "vec4", "typedArrayConstructor": Uint8Array }, + "sint8x2": { "numComponents": 2, "type": "BYTE", "normalized": false, "dataType": "signed int", "byteSize": 2, "wgslType": "vec2", "typedArrayConstructor": Int8Array }, + "sint8x4": { "numComponents": 4, "type": "BYTE", "normalized": false, "dataType": "signed int", "byteSize": 4, "wgslType": "vec4", "typedArrayConstructor": Int8Array }, + "unorm8x2": { "numComponents": 2, "type": "UNSIGNED_BYTE", "normalized": true, "dataType": "unsigned normalized", "byteSize": 2, "wgslType": "vec2", "typedArrayConstructor": Uint8Array }, + "unorm8x4": { "numComponents": 4, "type": "UNSIGNED_BYTE", "normalized": true, "dataType": "unsigned normalized", "byteSize": 4, "wgslType": "vec4", "typedArrayConstructor": Uint8Array }, + "snorm8x2": { "numComponents": 2, "type": "BYTE", "normalized": true, "dataType": "signed normalized", "byteSize": 2, "wgslType": "vec2", "typedArrayConstructor": Int8Array }, + "snorm8x4": { "numComponents": 4, "type": "BYTE", "normalized": true, "dataType": "signed normalized", "byteSize": 4, "wgslType": "vec4", "typedArrayConstructor": Int8Array }, + "uint16x2": { "numComponents": 2, "type": "UNSIGNED_SHORT", "normalized": false, "dataType": "unsigned int", "byteSize": 4, "wgslType": "vec2", "typedArrayConstructor": Uint16Array }, + "uint16x4": { "numComponents": 4, "type": "UNSIGNED_SHORT", "normalized": false, "dataType": "unsigned int", "byteSize": 8, "wgslType": "vec4", "typedArrayConstructor": Uint16Array }, + "sint16x2": { "numComponents": 2, "type": "SHORT", "normalized": false, "dataType": "signed int", "byteSize": 4, "wgslType": "vec2", "typedArrayConstructor": Int16Array }, + "sint16x4": { "numComponents": 4, "type": "SHORT", "normalized": false, "dataType": "signed int", "byteSize": 8, "wgslType": "vec4", "typedArrayConstructor": Int16Array }, + "unorm16x2": { "numComponents": 2, "type": "UNSIGNED_SHORT", "normalized": true, "dataType": "unsigned normalized", "byteSize": 4, "wgslType": "vec2", "typedArrayConstructor": Uint16Array }, + "unorm16x4": { "numComponents": 4, "type": "UNSIGNED_SHORT", "normalized": true, "dataType": "unsigned normalized", "byteSize": 8, "wgslType": "vec4", "typedArrayConstructor": Uint16Array }, + "snorm16x2": { "numComponents": 2, "type": "SHORT", "normalized": true, "dataType": "signed normalized", "byteSize": 4, "wgslType": "vec2", "typedArrayConstructor": Int16Array }, + "snorm16x4": { "numComponents": 4, "type": "SHORT", "normalized": true, "dataType": "signed normalized", "byteSize": 8, "wgslType": "vec4", "typedArrayConstructor": Int16Array }, + "float16x2": { "numComponents": 2, "type": "HALF_FLOAT", "normalized": false, "dataType": "float", "byteSize": 4, "wgslType": "vec2", "typedArrayConstructor": undefined, }, + "float16x4": { "numComponents": 4, "type": "HALF_FLOAT", "normalized": false, "dataType": "float", "byteSize": 8, "wgslType": "vec4", "typedArrayConstructor": undefined, }, + "float32": { "numComponents": 1, "type": "FLOAT", "normalized": false, "dataType": "float", "byteSize": 4, "wgslType": "f32", "typedArrayConstructor": Float32Array }, + "float32x2": { "numComponents": 2, "type": "FLOAT", "normalized": false, "dataType": "float", "byteSize": 8, "wgslType": "vec2", "typedArrayConstructor": Float32Array }, + "float32x3": { "numComponents": 3, "type": "FLOAT", "normalized": false, "dataType": "float", "byteSize": 12, "wgslType": "vec3", "typedArrayConstructor": Float32Array }, + "float32x4": { "numComponents": 4, "type": "FLOAT", "normalized": false, "dataType": "float", "byteSize": 16, "wgslType": "vec4", "typedArrayConstructor": Float32Array }, + "uint32": { "numComponents": 1, "type": "UNSIGNED_INT", "normalized": false, "dataType": "unsigned int", "byteSize": 4, "wgslType": "u32", "typedArrayConstructor": Uint32Array }, + "uint32x2": { "numComponents": 2, "type": "UNSIGNED_INT", "normalized": false, "dataType": "unsigned int", "byteSize": 8, "wgslType": "vec2", "typedArrayConstructor": Uint32Array }, + "uint32x3": { "numComponents": 3, "type": "UNSIGNED_INT", "normalized": false, "dataType": "unsigned int", "byteSize": 12, "wgslType": "vec3", "typedArrayConstructor": Uint32Array }, + "uint32x4": { "numComponents": 4, "type": "UNSIGNED_INT", "normalized": false, "dataType": "unsigned int", "byteSize": 16, "wgslType": "vec4", "typedArrayConstructor": Uint32Array }, + "sint32": { "numComponents": 1, "type": "INT", "normalized": false, "dataType": "signed int", "byteSize": 4, "wgslType": "i32", "typedArrayConstructor": Int32Array }, + "sint32x2": { "numComponents": 2, "type": "INT", "normalized": false, "dataType": "signed int", "byteSize": 8, "wgslType": "vec2", "typedArrayConstructor": Int32Array }, + "sint32x3": { "numComponents": 3, "type": "INT", "normalized": false, "dataType": "signed int", "byteSize": 12, "wgslType": "vec3", "typedArrayConstructor": Int32Array }, + "sint32x4": { "numComponents": 4, "type": "INT", "normalized": false, "dataType": "signed int", "byteSize": 16, "wgslType": "vec4", "typedArrayConstructor": Int32Array }, + "unorm10-10-10-2": { "numComponents": 4, "type": "UNSIGNED_INT_2_10_10_10_REV", "normalized": true, "dataType": "unsigned normalized", "byteSize": 4, "wgslType": "vec4", "typedArrayConstructor": Int32Array } +} + +/** + * 有类型数组构造器。 + */ +export type TypedArrayConstructor = Int8ArrayConstructor | Uint8ArrayConstructor | Int16ArrayConstructor | Uint16ArrayConstructor | Int32ArrayConstructor | Uint32ArrayConstructor | Float32ArrayConstructor; + +/** + * GPU顶点数据类型 + */ +export type VertexDataType = + | "unsigned int" + | "signed int" + | "unsigned normalized" + | "signed normalized" + | "float" + ; + +/** + * 顶点数据在WGSL中的类型。 + */ +export type WGSLVertexType = + | "vec2" + | "vec4" + | "vec2" + | "vec4" + | "vec2" + | "vec4" + | "vec2" + | "vec4" + | "f32" + | "vec3" + | "u32" + | "vec3" + | "i32" + | "vec3" + ; + +/** + * 属性缓冲数据类型 + * + * A GLenum specifying the data type of each component in the array. Possible values: + * + * * gl.BYTE: signed 8-bit integer, with values in [-128, 127] + * * gl.SHORT: signed 16-bit integer, with values in [-32768, 32767] + * * gl.UNSIGNED_BYTE: unsigned 8-bit integer, with values in [0, 255] + * * gl.UNSIGNED_SHORT: unsigned 16-bit integer, with values in [0,65535] + * * gl.FLOAT: 32-bit IEEE floating point number + * + * When using a WebGL 2 context, the following values are available additionally: + * + * * gl.HALF_FLOAT: 16-bit IEEE floating point number + * * gl.INT: 32-bit signed binary integer + * * gl.UNSIGNED_INT: 32-bit unsigned binary integer + * * gl.INT_2_10_10_10_REV: 32-bit signed integer with values in [-512, 511] + * * gl.UNSIGNED_INT_2_10_10_10_REV: 32-bit unsigned integer with values in [0, 1023] + * + * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/vertexAttribPointer + */ +export type GLVertexAttributeTypes = "FLOAT" | "BYTE" | "SHORT" | "UNSIGNED_BYTE" | "UNSIGNED_SHORT" // WebGL1 + | "HALF_FLOAT" | "INT" | "UNSIGNED_INT" | "INT_2_10_10_10_REV" | "UNSIGNED_INT_2_10_10_10_REV"; // WebGL2 + + +/** + * 顶点属性格式信息 + */ +export type VertexAttributeFormatInfo = { + + /** + * 部件数量。 + */ + numComponents: 1 | 2 | 3 | 4, + + /** + * 属性缓冲数据类型 + * + * 默认从Buffer数据中获取,如果未取到则默认为 "FLOAT" 。 + */ + type: GLVertexAttributeTypes; + + /** + * 数据类型。 + */ + dataType: VertexDataType, + + /** + * 所占字节尺寸。 + */ + byteSize: 2 | 4 | 8 | 12 | 16, + + /** + * 在着色器中对应类型。 + */ + wgslType: WGSLVertexType, + + /** + * 对应类型数组构造器。 + */ + typedArrayConstructor: TypedArrayConstructor, + + /** + * 是否标准化。 + */ + normalized: boolean; +}; diff --git a/src/data/BindingResources.ts b/src/data/BindingResources.ts new file mode 100644 index 0000000000000000000000000000000000000000..5a41490bd55eb5ba4863edfa1139151dc361dd7c --- /dev/null +++ b/src/data/BindingResources.ts @@ -0,0 +1,25 @@ +import { BufferBinding, BufferBindingItem } from "./BufferBinding"; + +/** + * 绑定资源。 + * + * 与着色器中名称对应的绑定资源(纹理、采样器、统一数据、存储数据等)。 + */ +export interface BindingResources +{ + readonly [key: string]: BindingResource; +} + +/** + * 绑定资源 类型 + */ +export type BindingResource = BindingResourceTypeMap[keyof BindingResourceTypeMap]; + +export interface BindingResourceTypeMap +{ + /** + * 缓冲区绑定。 + */ + BufferBinding: BufferBinding; + BufferBindingItem: BufferBindingItem; +} diff --git a/src/data/IBlendComponent.ts b/src/data/BlendComponent.ts similarity index 98% rename from src/data/IBlendComponent.ts rename to src/data/BlendComponent.ts index 708c8c3e61f05eed4ce4822813eb9254f206e331..b9e906b9ec5b312a94209e240f1798aea7f38769 100644 --- a/src/data/IBlendComponent.ts +++ b/src/data/BlendComponent.ts @@ -10,7 +10,7 @@ * * @see https://gpuweb.github.io/gpuweb/#dictdef-gpucolortargetstate */ -export interface IBlendComponent +export interface BlendComponent { /** * 混合方式。 diff --git a/src/data/IBlendState.ts b/src/data/BlendState.ts similarity index 33% rename from src/data/IBlendState.ts rename to src/data/BlendState.ts index a4b9516e1b67b529466baecc53ce9dbbe3449939..5b68384d8f1c6d36d3dd841bd83cf154cd2b6d4c 100644 --- a/src/data/IBlendState.ts +++ b/src/data/BlendState.ts @@ -1,5 +1,5 @@ -import { IBlendComponent } from "./IBlendComponent"; -import { IColor } from "./IRenderPassColorAttachment"; +import { BlendComponent } from "./BlendComponent"; +import { Color } from "./RenderPassColorAttachment"; /** * 混合状态。 @@ -10,27 +10,60 @@ import { IColor } from "./IRenderPassColorAttachment"; * * @see https://gpuweb.github.io/gpuweb/#dictdef-gpublendstate */ -export interface IBlendState +export interface BlendState { /** * 混合时使用的常量值,默认为 [0,0,0,0]。 * - * 当 {@link IBlendComponent.srcFactor} {@link IBlendComponent.dstFactor} 取值为 "constant" 或者 "one-minus-constant" 时生效。 + * 当 {@link BlendComponent.srcFactor} {@link BlendComponent.dstFactor} 取值为 "constant" 或者 "one-minus-constant" 时生效。 * * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/blendColor * @see https://gpuweb.github.io/gpuweb/#dom-renderstate-blendconstant-slot * * 注:只取 renderPipeline.fragment?.targets?.[0]?.blend.constantColor 值。 */ - readonly constantColor?: IColor; + readonly constantColor?: Color; /** * 为颜色通道定义相应渲染目标的混合行为。 */ - readonly color?: IBlendComponent; + readonly color?: BlendComponent; /** * 为alpha通道定义相应渲染目标的混合行为。 */ - readonly alpha?: IBlendComponent; + readonly alpha?: BlendComponent; } + +export class BlendState +{ + /** + * 当混合系数用到了混合常量值时设置混合常量值。 + * + * @param blend + * @returns + */ + static getBlendConstantColor(blendState: BlendState): Color + { + if (!blendState) return undefined; + + const { color, alpha, constantColor } = blendState; + + // 当混合系数用到了混合常量值时设置混合常量值。 + if (0 + || color?.srcFactor === "constant" + || color?.srcFactor === "one-minus-constant" + || color?.dstFactor === "constant" + || color?.dstFactor === "one-minus-constant" + || alpha?.srcFactor === "constant" + || alpha?.srcFactor === "one-minus-constant" + || alpha?.dstFactor === "constant" + || alpha?.dstFactor === "one-minus-constant" + ) + { + return constantColor ?? [0, 0, 0, 0]; + } + + return undefined; + } +} \ No newline at end of file diff --git a/src/data/Buffer.ts b/src/data/Buffer.ts new file mode 100644 index 0000000000000000000000000000000000000000..f39971bf559a9087e90ab9702b16844861963f24 --- /dev/null +++ b/src/data/Buffer.ts @@ -0,0 +1,42 @@ +import { TypedArray } from "../types/TypedArray"; +import { WriteBuffer } from "./WriteBuffer"; + +/** + * 缓冲区 + * + * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/bufferData + * + * {@link GPUBufferDescriptor} + * + * {@link GPUBuffer} + */ +export interface Buffer +{ + /** + * 标签。 + * + * 用于调试。 + */ + readonly label?: string; + + /** + * 缓冲区尺寸,单位为字节。 + * + * 尺寸必须为4的倍数。 + * + * 注:修改尺寸时,会重新创建缓冲区。 + */ + readonly size: number; + + /** + * 缓冲区数据。 + */ + readonly data?: TypedArray; + + /** + * 写缓冲区。 + * + * {@link GPUQueue.writeBuffer} + */ + readonly writeBuffers?: WriteBuffer[]; +} diff --git a/src/data/BufferBinding.ts b/src/data/BufferBinding.ts new file mode 100644 index 0000000000000000000000000000000000000000..5c37ab7f8bb825a9d7be13ca46a8f02038d1de04 --- /dev/null +++ b/src/data/BufferBinding.ts @@ -0,0 +1,25 @@ +import { TypedArray } from "../types/TypedArray"; + +/** + * 缓冲区绑定。 + * + * WebGL 统一块(Uniform Block) 数据 + * WebGPU 缓冲区绑定(GPUBufferBinding) + * + * @see GPUBufferBinding + */ +export interface BufferBinding +{ + readonly [name: string]: BufferBindingItem; + + /** + * 如果未设置引擎将自动生成。 + */ + readonly bufferView?: TypedArray; +} + +export type UniformDataItem = number | readonly number[] | readonly number[][] | TypedArray | readonly TypedArray[] + | { toArray(): number[] | TypedArray } + | readonly { toArray(): number[] | TypedArray }[] + ; +export type BufferBindingItem = UniformDataItem | { readonly [key: string]: BufferBindingItem }; diff --git a/src/data/CanvasContext.ts b/src/data/CanvasContext.ts new file mode 100644 index 0000000000000000000000000000000000000000..57229311edbc51543859522e4f322a3480b1f75f --- /dev/null +++ b/src/data/CanvasContext.ts @@ -0,0 +1,12 @@ +/** + * 画布上下文 + */ +export interface CanvasContext +{ + /** + * 画布。 + * + * 可能是画布的编号,也可能是画布元素或者离屏画布。 + */ + readonly canvasId: string | HTMLCanvasElement | OffscreenCanvas; +} diff --git a/src/data/CanvasTexture.ts b/src/data/CanvasTexture.ts new file mode 100644 index 0000000000000000000000000000000000000000..fc7279432a4699ff255e0e45fd6ce6aa8f10c90e --- /dev/null +++ b/src/data/CanvasTexture.ts @@ -0,0 +1,21 @@ +import { CanvasContext } from "./CanvasContext"; + +/** + * 画布纹理,从画布的上下文获取纹理 + */ +export interface CanvasTexture +{ + /** + * 画布上下文。 + */ + context: CanvasContext; + + /** + * 画布尺寸发生变化后引擎自动递增。 + * + * 引擎内部监听这个值,在画布尺寸发生变化后重新获取纹理。 + * + * @private + */ + _canvasSizeVersion?: number; +} diff --git a/src/data/IColorTargetState.ts b/src/data/ColorTargetState.ts similarity index 74% rename from src/data/IColorTargetState.ts rename to src/data/ColorTargetState.ts index 3337795e11bd40d4f4aa63926dd3bd9d8d6d735d..e421f6e2577e6edf986f57871ae492dac4f9575b 100644 --- a/src/data/IColorTargetState.ts +++ b/src/data/ColorTargetState.ts @@ -1,11 +1,11 @@ -import { IBlendState } from "./IBlendState"; +import { BlendState } from "./BlendState"; /** * 属性 `format` 将由渲染通道中附件给出。 * * @see https://gpuweb.github.io/gpuweb/#dictdef-gpucolortargetstate */ -export interface IColorTargetState +export interface ColorTargetState { /** * The blending behavior for this color target. If left undefined, disables blending for this @@ -15,7 +15,7 @@ export interface IColorTargetState * * 默认 `undefined`,表示不进行混合。 */ - readonly blend?: IBlendState; + readonly blend?: BlendState; /** * 控制那些颜色分量是否可以被写入到颜色中。 @@ -26,7 +26,7 @@ export interface IColorTargetState * * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/colorMask */ - readonly writeMask?: IWriteMask; + readonly writeMask?: WriteMask; } -export type IWriteMask = [red: boolean, green: boolean, blue: boolean, alpha: boolean]; +export type WriteMask = readonly [red: boolean, green: boolean, blue: boolean, alpha: boolean]; diff --git a/src/data/ICommandEncoder.ts b/src/data/CommandEncoder.ts similarity index 39% rename from src/data/ICommandEncoder.ts rename to src/data/CommandEncoder.ts index d255c2270592b4bd7c5e7b550c704c26e59cd9db..b311f3ece1cb6cee8978088191d28c2db9785ed0 100644 --- a/src/data/ICommandEncoder.ts +++ b/src/data/CommandEncoder.ts @@ -1,43 +1,44 @@ -import { ICopyBufferToBuffer } from "./ICopyBufferToBuffer"; -import { ICopyTextureToTexture } from "./ICopyTextureToTexture"; -import { IRenderPass } from "./IRenderPass"; +import { CopyBufferToBuffer } from "./CopyBufferToBuffer"; +import { CopyTextureToTexture } from "./CopyTextureToTexture"; +import { RenderPass } from "./RenderPass"; /** * 命令编码器。 * * @see https://developer.mozilla.org/en-US/docs/Web/API/GPUCommandEncoder */ -export interface ICommandEncoder +export interface CommandEncoder { /** * 通道编码器列表。 * * 包括计算通道编码器、渲染通道编码器 以及 GPU中缓存与纹理之间拷贝。 */ - passEncoders: IPassEncoder[]; + passEncoders: PassEncoder[] } /** - * 如需扩展 IPassEncoder ,请在 IPassEncoderMap 中进行添加。 + * 通道编码器。 + * + * 如需扩展 IPassEncoder ,请在 PassEncoderMap 中进行添加。 + */ +export type PassEncoder = PassEncoderMap[keyof PassEncoderMap]; + +/** + * 如需扩展 IPassEncoder ,请在 PassEncoderMap 中进行添加。 */ -export interface IPassEncoderMap +export interface PassEncoderMap { /** * 渲染通道。 */ - IRenderPass: IRenderPass; + RenderPass: RenderPass; /** * 纹理之间拷贝。 */ - ICopyTextureToTexture: ICopyTextureToTexture; + CopyTextureToTexture: CopyTextureToTexture; - ICopyBufferToBuffer: ICopyBufferToBuffer; + CopyBufferToBuffer: CopyBufferToBuffer; } -/** - * 通道编码器。 - * - * 如需扩展 IPassEncoder ,请在 IPassEncoderMap 中进行添加。 - */ -export type IPassEncoder = IPassEncoderMap[keyof IPassEncoderMap]; diff --git a/src/data/ICopyBufferToBuffer.ts b/src/data/CopyBufferToBuffer.ts similarity index 65% rename from src/data/ICopyBufferToBuffer.ts rename to src/data/CopyBufferToBuffer.ts index b5ed934ab428b71792ec7c86ca887c66252e2acc..264cfdedd5daba57a22819ef65b69dbae319dec4 100644 --- a/src/data/ICopyBufferToBuffer.ts +++ b/src/data/CopyBufferToBuffer.ts @@ -1,4 +1,4 @@ -import { IBuffer } from "./IBuffer"; +import { Buffer } from "./Buffer"; /** * GPU缓冲区之间拷贝。 @@ -6,32 +6,32 @@ import { IBuffer } from "./IBuffer"; * {@link WebGL2RenderingContextBase.copyBufferSubData} * {@link GPUCommandEncoder.copyBufferToBuffer} */ -export interface ICopyBufferToBuffer +export interface CopyBufferToBuffer { /** * 数据类型。 */ - readonly __type: "CopyBufferToBuffer"; + readonly __type__: "CopyBufferToBuffer"; /** * 源缓冲区。 */ - source: IBuffer, + source: Buffer; /** * 默认为0。 */ - sourceOffset?: number, + sourceOffset?: number; /** * 目标缓冲区。 */ - destination: IBuffer, + destination: Buffer; /** * 默认为0。 */ - destinationOffset?: number, + destinationOffset?: number; /** * 默认为源缓冲区尺寸。 diff --git a/src/data/CopyTextureToTexture.ts b/src/data/CopyTextureToTexture.ts new file mode 100644 index 0000000000000000000000000000000000000000..c55126a012bc86b778790b5430f1cd63154d464b --- /dev/null +++ b/src/data/CopyTextureToTexture.ts @@ -0,0 +1,30 @@ +import { ImageCopyTexture } from "./ImageCopyTexture"; +import { TextureSize } from "./Texture"; + +/** + * GPU纹理间拷贝。 + * + * {@link GPUCommandEncoder.copyTextureToTexture} + */ +export interface CopyTextureToTexture +{ + /** + * 数据类型。 + */ + readonly __type__: "CopyTextureToTexture"; + + /** + * Combined with `copySize`, defines the region of the source texture subresources. + */ + source: ImageCopyTexture; + + /** + * Combined with `copySize`, defines the region of the destination texture subresources. + */ + destination: ImageCopyTexture; + + /** + * 拷贝的尺寸。 + */ + copySize: TextureSize; +} diff --git a/src/data/IDepthStencilState.ts b/src/data/DepthStencilState.ts similarity index 89% rename from src/data/IDepthStencilState.ts rename to src/data/DepthStencilState.ts index e5b0913bf93dacb792c7023c9f4b0fecafe46f80..7d1fe47537164c02da04ced1936668ffe1736b8d 100644 --- a/src/data/IDepthStencilState.ts +++ b/src/data/DepthStencilState.ts @@ -1,4 +1,4 @@ -import { ICompareFunction, IStencilFaceState } from "./IStencilFaceState"; +import { CompareFunction as CompareFunction, StencilFaceState } from "./StencilFaceState"; /** * 深度模板阶段描述。 @@ -9,7 +9,7 @@ import { ICompareFunction, IStencilFaceState } from "./IStencilFaceState"; * * @see https://www.orillusion.com/zh/webgpu.html#depth-stencil-state */ -export interface IDepthStencilState +export interface DepthStencilState { /** * 指示这个 GPURenderPipeline 是否可以修改 depthStencilAttachment 深度值。 @@ -27,21 +27,21 @@ export interface IDepthStencilState * * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/depthFunc */ - readonly depthCompare?: ICompareFunction; + readonly depthCompare?: CompareFunction; /** * 定义了如何为朝前的图元执行模板比较和操作。 * * 默认为 {}。 */ - readonly stencilFront?: IStencilFaceState; + readonly stencilFront?: StencilFaceState; /** * 定义了如何为朝后的图元执行模板比较和操作。 * * 默认为 {}。 */ - readonly stencilBack?: IStencilFaceState; + readonly stencilBack?: StencilFaceState; /** * 模板测试时如果使用 "replace" ,则使用该值填充模板值。 diff --git a/src/data/DrawIndexed.ts b/src/data/DrawIndexed.ts new file mode 100644 index 0000000000000000000000000000000000000000..8d5614cd97dd8bf552f82e5d115633823775ec3f --- /dev/null +++ b/src/data/DrawIndexed.ts @@ -0,0 +1,34 @@ +/** + * 根据索引数据绘制图元。 + * + * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/drawElements + * @see GPURenderCommandsMixin.drawIndexed + */ +export interface DrawIndexed +{ + /** + * 数据类型。 + */ + readonly __type__: "DrawIndexed"; + + /** + * The number of indices to draw. + * + * 绘制的顶点索引数量。 + */ + readonly indexCount: number; + + /** + * The number of instances to draw. + * + * 默认为 1 。 + */ + readonly instanceCount?: number; + + /** + * Offset into the index buffer, in indices, begin drawing from. + * + * 默认为 0 。 + */ + readonly firstIndex?: number; +} diff --git a/src/data/DrawVertex.ts b/src/data/DrawVertex.ts new file mode 100644 index 0000000000000000000000000000000000000000..aa2cfdb2d315e7fb872985c94459437db1f0d4b8 --- /dev/null +++ b/src/data/DrawVertex.ts @@ -0,0 +1,34 @@ +/** + * Draws primitives. + * + * 根据顶点数据绘制图元。 + * + * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/drawVertex + * @see GPURenderCommandsMixin.draw + */ +export interface DrawVertex +{ + /** + * 数据类型。 + */ + readonly __type__: "DrawVertex"; + + /** + * The number of vertices to draw. + */ + readonly vertexCount: number; + + /** + * The number of instances to draw. + * + * 默认为 1 。 + */ + readonly instanceCount?: number; + + /** + * Offset into the vertex buffers, in vertices, to begin drawing from. + * + * 默认为 0。 + */ + readonly firstVertex?: number; +} \ No newline at end of file diff --git a/src/data/IFragmentState.ts b/src/data/FragmentState.ts similarity index 77% rename from src/data/IFragmentState.ts rename to src/data/FragmentState.ts index 65a346aa7d110f1c6dfe415de7fcb0b4100d2b6e..e21ada009f34ca6232218eee498c94c1a473ed28 100644 --- a/src/data/IFragmentState.ts +++ b/src/data/FragmentState.ts @@ -1,11 +1,11 @@ -import { IColorTargetState } from "./IColorTargetState"; +import { ColorTargetState } from "./ColorTargetState"; /** * 片段着色器阶段描述。 * * {@link GPUFragmentState} */ -export interface IFragmentState +export interface FragmentState { /** * 着色器代码。 @@ -20,5 +20,5 @@ export interface IFragmentState * * 注:WebGL中没法分别对每个颜色附件进行设置,统一使用第一项(targets[0])设置! */ - readonly targets?: readonly IColorTargetState[]; + readonly targets?: readonly ColorTargetState[]; } diff --git a/src/data/ICanvasContext.ts b/src/data/ICanvasContext.ts deleted file mode 100644 index a913f2385332ef45fb08bd671ab9e0bf7b55f818..0000000000000000000000000000000000000000 --- a/src/data/ICanvasContext.ts +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @see GPUCanvasContext - * @see HTMLCanvasElement.getContext - * @see GPUCanvasContext.configure - */ -export interface ICanvasContext -{ - /** - * 画布id - */ - readonly canvasId: string; - - /** - * 画布配置。默认有引擎自动设置。 - */ - // configuration?: IGPUCanvasConfiguration; -} diff --git a/src/data/ICanvasTexture.ts b/src/data/ICanvasTexture.ts deleted file mode 100644 index a36e23a097d58a3b20529345d59dc9d4b48e8a06..0000000000000000000000000000000000000000 --- a/src/data/ICanvasTexture.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { ICanvasContext } from "./ICanvasContext"; - -/** - * 画布纹理,从画布的WebGPU上下文获取纹理 - * - * 注:只在WebGPU上支持。 - */ -export interface ICanvasTexture -{ - context: ICanvasContext; -} diff --git a/src/data/IRenderObject.ts b/src/data/IRenderObject.ts deleted file mode 100644 index 69e532ffd99d6a803e95124751d1c7393a7ae0d9..0000000000000000000000000000000000000000 --- a/src/data/IRenderObject.ts +++ /dev/null @@ -1,166 +0,0 @@ -import { IRenderPipeline } from "./IRenderPipeline"; -import { IScissorRect } from "./IScissorRect"; -import { IVertexAttributes } from "./IVertexAttributes"; -import { IViewport } from "./IViewport"; -import { TypedArray } from "./TypedArray"; - -/** - * 渲染对象,包含一次渲染时包含的所有数据。 - */ -export interface IRenderObject -{ - /** - * 数据类型。 - */ - readonly __type?: "RenderObject"; - - /** - * 视窗。 - * - * 描述渲染在画布的哪个区域,默认整个画布。 - */ - viewport?: IViewport; - - /** - * 光栅化阶段中使用的剪刀矩形。 - */ - scissorRect?: IScissorRect; - - /** - * 渲染管线描述。 - */ - readonly pipeline: IRenderPipeline; - - /** - * 顶点属性数据映射。 - */ - vertices?: IVertexAttributes; - - /** - * 顶点索引数据。 - */ - indices?: IIndicesDataTypes; - - /** - * Uniform变量数据 - */ - readonly uniforms?: IUniforms; - - /** - * 根据顶点数据绘制图元。 - * - * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/drawVertex - */ - readonly drawVertex?: IDrawVertex; - - /** - * 根据索引数据绘制图元。 - * - * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/drawElements - */ - readonly drawIndexed?: IDrawIndexed; -} - -/** - * 顶点索引数据类型。 - */ -export type IIndicesDataTypes = Uint16Array | Uint32Array; - -/** - * Draws primitives. - * - * 根据顶点数据绘制图元。 - * - * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/drawVertex - * @see GPURenderCommandsMixin.draw - */ -export interface IDrawVertex -{ - /** - * The number of vertices to draw. - */ - readonly vertexCount: number; - - /** - * The number of instances to draw. - * - * 默认为 1 。 - */ - readonly instanceCount?: number; - - /** - * Offset into the vertex buffers, in vertices, to begin drawing from. - * - * 默认为 0。 - */ - readonly firstVertex?: number; -} - -/** - * 根据索引数据绘制图元。 - */ -export interface IDrawIndexed -{ - /** - * The number of indices to draw. - * - * 绘制的顶点索引数量。 - */ - readonly indexCount: number; - - /** - * The number of instances to draw. - * - * 默认为 1 。 - */ - readonly instanceCount?: number; - - /** - * Offset into the index buffer, in indices, begin drawing from. - * - * 默认为 0 。 - */ - readonly firstIndex?: number; -} - -/** - * Uniform 类型 - */ -export type IUniformType = IUniformTypeMap[keyof IUniformTypeMap]; - -/** - * Uniform 数据 - */ -export interface IUniforms -{ - [key: string]: IUniformType; -} - -export interface IUniformTypeMap -{ - /** - * 缓冲区绑定。 - */ - IBufferBinding: IBufferBinding; -} - -/** - * 缓冲区绑定。 - * - * WebGL 统一块(Uniform Block) 数据 - * WebGPU 缓冲区绑定(GPUBufferBinding) - * - * @see GPUBufferBinding - */ -export interface IBufferBinding -{ - [name: string]: IBufferBindingItem; - - /** - * 如果未设置引擎将自动生成。 - */ - readonly bufferView?: TypedArray; -} - -export type IUniformDataItem = number | number[] | number[][] | TypedArray | TypedArray[]; -export type IBufferBindingItem = IUniformDataItem | { [key: string]: IBufferBindingItem }; \ No newline at end of file diff --git a/src/data/IRenderPass.ts b/src/data/IRenderPass.ts deleted file mode 100644 index 1d90e48f875ddbbd69921dce2f513265f901434a..0000000000000000000000000000000000000000 --- a/src/data/IRenderPass.ts +++ /dev/null @@ -1,39 +0,0 @@ -import { IRenderObject } from "./IRenderObject"; -import { IRenderPassDescriptor } from "./IRenderPassDescriptor"; - -/** - * WebGL渲染通道 - * - * 包含渲染通道描述以及需要渲染的对象列表。 - */ -export interface IRenderPass -{ - /** - * 数据类型。 - */ - readonly __type?: "RenderPass"; - - /** - * 渲染通道描述 - */ - readonly descriptor?: IRenderPassDescriptor; - - /** - * 渲染对象列表 - */ - readonly renderObjects?: readonly IRenderPassObject[]; - - // /** - // * 渲染不被遮挡查询结果。具体数据保存在各子项的"result"属性中。 - // * - // * 当提交WebGL后自动获取结果后填充该属性。 - // */ - // occlusionQueryResults?: IGLOcclusionQuery[]; -} - -export type IRenderPassObject = IRenderPassObjectMap[keyof IRenderPassObjectMap]; - -export interface IRenderPassObjectMap -{ - IRenderObject: IRenderObject; -} \ No newline at end of file diff --git a/src/data/ICopyTextureToTexture.ts b/src/data/ImageCopyTexture.ts similarity index 47% rename from src/data/ICopyTextureToTexture.ts rename to src/data/ImageCopyTexture.ts index 00d9f3ded955944c49e9053d1cc59d5567b7a847..4f625948e205dee4fc5e0c3972097d3de109af18 100644 --- a/src/data/ICopyTextureToTexture.ts +++ b/src/data/ImageCopyTexture.ts @@ -1,33 +1,4 @@ -import { ITextureOrigin, ITextureSize } from "./ITexture"; -import { ITextureLike } from "./ITextureView"; - -/** - * GPU纹理间拷贝。 - * - * {@link GPUCommandEncoder.copyTextureToTexture} - */ -export interface ICopyTextureToTexture -{ - /** - * 数据类型。 - */ - readonly __type: "CopyTextureToTexture"; - - /** - * Combined with `copySize`, defines the region of the source texture subresources. - */ - source: IImageCopyTexture, - - /** - * Combined with `copySize`, defines the region of the destination texture subresources. - */ - destination: IImageCopyTexture, - - /** - * 拷贝的尺寸。 - */ - copySize: ITextureSize; -} +import { TextureLike, TextureOrigin } from "./Texture"; /** * 被操作的纹理相关信息。 @@ -35,12 +6,12 @@ export interface ICopyTextureToTexture * {@link GPUCommandEncoder.copyTextureToTexture} * {@link GPUImageCopyTexture} */ -export interface IImageCopyTexture +export interface ImageCopyTexture { /** * Texture to copy to/from. */ - texture: ITextureLike; + texture: TextureLike; /** * Mip-map level of the {@link GPUImageCopyTexture#texture} to copy to/from. @@ -51,7 +22,7 @@ export interface IImageCopyTexture * Defines the origin of the copy - the minimum corner of the texture sub-region to copy to/from. * Together with `copySize`, defines the full copy sub-region. */ - origin?: ITextureOrigin; + origin?: TextureOrigin; /** * Defines which aspects of the {@link GPUImageCopyTexture#texture} to copy to/from. diff --git a/src/data/OcclusionQuery.ts b/src/data/OcclusionQuery.ts new file mode 100644 index 0000000000000000000000000000000000000000..a8f960d3b183758cf77f48cb9073f7926c1d8960 --- /dev/null +++ b/src/data/OcclusionQuery.ts @@ -0,0 +1,24 @@ +import { RenderObject } from "./RenderObject"; + +/** + * 遮挡查询 + */ +export interface OcclusionQuery +{ + /** + * 数据类型。 + */ + readonly __type__: "OcclusionQuery"; + + /** + * GPU渲染对象列表。 + */ + renderObjects: RenderObject[]; + + /** + * 查询结束回调。 + * + * @param result 是否被渲染。true表示被渲染,false表示未被渲染。 + */ + onQuery?(result: number): void; +} \ No newline at end of file diff --git a/src/data/IPrimitiveState.ts b/src/data/PrimitiveState.ts similarity index 59% rename from src/data/IPrimitiveState.ts rename to src/data/PrimitiveState.ts index ba7b43b18a323017df7221c46522769b38d59ff9..b2f77d8630b648da43ca552a6d53acbd6b716dfe 100644 --- a/src/data/IPrimitiveState.ts +++ b/src/data/PrimitiveState.ts @@ -5,7 +5,7 @@ * * `stripIndexFormat` 将由引擎自动设置。 */ -export interface IPrimitiveState +export interface PrimitiveState { /** * The type of primitive to be constructed from the vertex inputs. @@ -17,8 +17,14 @@ export interface IPrimitiveState * * `triangle-strip` 绘制三角形条带。 * * 默认 `triangle-list` ,默认每三个顶点绘制一个三角形。 + * + * 图形拓扑结构。 + * + * 以下仅在WebGL生效 + * * LINE_LOOP 绘制循环连线。 + * * TRIANGLE_FAN 绘制三角扇形。 */ - readonly topology?: IPrimitiveTopology; + readonly topology?: PrimitiveTopology; /** * Defines which polygon orientation will be culled, if any. @@ -31,19 +37,22 @@ export interface IPrimitiveState * * `front` 剔除正面 * * `back` 剔除背面 */ - readonly cullFace?: ICullFace; + readonly cullFace?: CullFace; /** * Defines which polygons are considered front-facing. * * 正向方向。默认 "ccw",表示三角形逆时针方向为正面。 */ - readonly frontFace?: IFrontFace; + readonly frontFace?: FrontFace; } -export type IPrimitiveTopology = IPrimitiveTopologyMap[keyof IPrimitiveTopologyMap]; +/** + * 图元拓扑结构。 + */ +export type PrimitiveTopology = PrimitiveTopologyMap[keyof PrimitiveTopologyMap]; -export interface IPrimitiveTopologyMap +export interface PrimitiveTopologyMap { "point-list": "point-list", "line-list": "line-list", @@ -52,13 +61,21 @@ export interface IPrimitiveTopologyMap "triangle-strip": "triangle-strip", } -export type ICullFace = ICullFaceMap[keyof ICullFaceMap]; +/** + * * `FRONT_AND_BACK` 剔除正面与背面,仅在WebGL中生效! + * + * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/cullFace + */ +export type CullFace = CullFaceMap[keyof CullFaceMap]; -export interface ICullFaceMap +export interface CullFaceMap { "none": "none", "front": "front", "back": "back", } -export type IFrontFace = "ccw" | "cw"; \ No newline at end of file +/** + * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/frontFace + */ +export type FrontFace = "ccw" | "cw"; \ No newline at end of file diff --git a/src/data/ReadPixels.ts b/src/data/ReadPixels.ts new file mode 100644 index 0000000000000000000000000000000000000000..516980a0eda51303667273315a21ca2a94289806 --- /dev/null +++ b/src/data/ReadPixels.ts @@ -0,0 +1,29 @@ +import { TextureView } from "./TextureView"; + +/** + * 读取渲染缓冲区或者纹理视图中的像素值。 + * + * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/readPixels + */ +export interface ReadPixels +{ + /** + * 读取的纹理视图。 + */ + textureView: TextureView; + + /** + * 读取位置。 + */ + origin: [x: number, y: number], + + /** + * 读取尺寸 + */ + copySize: [width: number, height: number] + + /** + * 用于保存最后结果。 + */ + result?: ArrayBufferView; +} \ No newline at end of file diff --git a/src/data/RenderObject.ts b/src/data/RenderObject.ts new file mode 100644 index 0000000000000000000000000000000000000000..740155a11c01258ff1b1ebfdb8639a9ed47ae089 --- /dev/null +++ b/src/data/RenderObject.ts @@ -0,0 +1,137 @@ +import { BindingResources } from "./BindingResources"; +import { DrawIndexed } from "./DrawIndexed"; +import { DrawVertex } from "./DrawVertex"; +import { RenderPipeline } from "./RenderPipeline"; +import { ScissorRect } from "./ScissorRect"; +import { VertexAttribute, VertexAttributes } from "./VertexAttributes"; +import { Viewport } from "./Viewport"; + +/** + * 渲染对象,包含一次渲染时包含的所有数据。 + */ +export interface RenderObject +{ + /** + * 数据类型。 + */ + __type__?: "RenderObject"; + + /** + * 视窗。 + * + * 描述渲染在画布的哪个区域,默认整个画布。 + */ + readonly viewport?: Viewport; + + /** + * 光栅化阶段中使用的剪刀矩形。 + */ + readonly scissorRect?: ScissorRect; + + /** + * 渲染管线描述。 + */ + readonly pipeline: RenderPipeline; + + /** + * 顶点属性数据映射。 + */ + readonly vertices?: VertexAttributes; + + /** + * 顶点索引数据。 + */ + readonly indices?: IndicesDataTypes; + + /** + * 绘制。 + */ + readonly draw?: IDraw; + + /** + * 绑定资源。 + * + * 与着色器中名称对应的绑定资源(纹理、采样器、统一数据、存储数据等)。 + */ + readonly bindingResources?: BindingResources; +} + + +export class RenderObject +{ + + /** + * 获取顶点数量。 + * + * @returns 顶点数量。 + */ + static getNumVertex(geometry: RenderObject) + { + const attributes = geometry.vertices; + const vertexList = Object.keys(attributes).map((v) => attributes[v]).filter((v) => (v.data && v.stepMode !== "instance")); + + const count = vertexList.length > 0 ? VertexAttribute.getVertexCount(vertexList[0]) : 0; + + // 验证所有顶点属性数据的顶点数量一致。 + if (vertexList.length > 0) + { + console.assert(vertexList.every((v) => count === VertexAttribute.getVertexCount(v))); + } + + return count; + } + + /** + * 获取实例数量。 + * + * @returns 实例数量。 + */ + static getInstanceCount(geometry: RenderObject) + { + const attributes = geometry.vertices; + const vertexList = Object.keys(attributes).map((v) => attributes[v]).filter((v) => (v.data && v.stepMode === "instance")); + + const count = vertexList.length > 0 ? VertexAttribute.getVertexCount(vertexList[0]) : 1; + + // 验证所有顶点属性数据的顶点数量一致。 + if (vertexList.length > 0) + { + console.assert(vertexList.every((v) => count === VertexAttribute.getVertexCount(v))); + } + + return count; + } + + static getDraw(geometry: RenderObject): DrawIndexed | DrawVertex + { + if (geometry['_draw']) return geometry['_draw']; + + const instanceCount = RenderObject.getInstanceCount(geometry); + + if (geometry.indices) + { + return { + __type__: "DrawIndexed", + indexCount: geometry.indices.length, + firstIndex: 0, + instanceCount, + }; + } + + return { + __type__: "DrawVertex", + vertexCount: RenderObject.getNumVertex(geometry), + instanceCount, + }; + } +} + +/** + * 顶点索引数据类型。 + */ +export type IndicesDataTypes = Uint16Array | Uint32Array; + +/** + * 绘制图形。 + */ +export type IDraw = DrawVertex | DrawIndexed; diff --git a/src/data/RenderPass.ts b/src/data/RenderPass.ts new file mode 100644 index 0000000000000000000000000000000000000000..4c27a568290684579320efbbf1327352b4b07c76 --- /dev/null +++ b/src/data/RenderPass.ts @@ -0,0 +1,42 @@ +import { OcclusionQuery } from "./OcclusionQuery"; +import { RenderObject } from "./RenderObject"; +import { RenderPassDescriptor } from "./RenderPassDescriptor"; + +/** + * WebGL渲染通道 + * + * 包含渲染通道描述以及需要渲染的对象列表。 + */ +export interface RenderPass +{ + /** + * 数据类型。 + */ + readonly __type__?: "RenderPass"; + + /** + * 渲染通道描述 + */ + readonly descriptor?: RenderPassDescriptor; + + /** + * 渲染对象列表 + */ + readonly renderPassObjects?: readonly RenderPassObject[]; + + /** + * 当渲染通道中存在遮挡查询时,在查询结束后调用该函数返回查询结果。 + * + * @param occlusionQuerys 遮挡查询列表 + * @param results 是否被渲染。true表示被渲染,false表示未被渲染。 + */ + onOcclusionQuery?(occlusionQuerys: OcclusionQuery[], results: number[]): void; +} + +export type RenderPassObject = RenderPassObjectMap[keyof RenderPassObjectMap]; + +export interface RenderPassObjectMap +{ + IRenderObject: RenderObject; + OcclusionQuery: OcclusionQuery +} \ No newline at end of file diff --git a/src/data/IRenderPassColorAttachment.ts b/src/data/RenderPassColorAttachment.ts similarity index 85% rename from src/data/IRenderPassColorAttachment.ts rename to src/data/RenderPassColorAttachment.ts index 45e8fd0dea430e5867d0433e02a3657da7fb22d0..c708709884cfc756e64f7b9d27b839aef6f0c9a1 100644 --- a/src/data/IRenderPassColorAttachment.ts +++ b/src/data/RenderPassColorAttachment.ts @@ -1,9 +1,9 @@ -import { ITextureView } from "./ITextureView"; +import { TextureView } from "./TextureView"; /** * 渲染通道颜色附件。 */ -export interface IRenderPassColorAttachment +export interface RenderPassColorAttachment { /** * 颜色附件视图。 @@ -18,7 +18,7 @@ export interface IRenderPassColorAttachment * 注:引擎运行中该属性可能是 IGLRenderbuffer 类型,用于处理多重采样。 * */ - readonly view?: ITextureView; + readonly view?: TextureView; /** * 清除后填充值。 @@ -37,7 +37,7 @@ export interface IRenderPassColorAttachment * They are converted [$to a texel value of texture format$] matching the render attachment. * If conversion fails, a validation error is generated. */ - readonly clearValue?: IColor; + readonly clearValue?: Color; /** * 是否清除颜色附件。 @@ -54,9 +54,9 @@ export interface IRenderPassColorAttachment * executing the render pass. * Note: It is recommended to prefer clearing; see {@link GPULoadOp#"clear"} for details. */ - readonly loadOp?: ILoadOp; + readonly loadOp?: LoadOp; } -export type IColor = [red: number, green: number, blue: number, alpha: number]; +export type Color = [red: number, green: number, blue: number, alpha: number]; -export type ILoadOp = "load" | "clear"; +export type LoadOp = "load" | "clear"; diff --git a/src/data/IRenderPassDepthStencilAttachment.ts b/src/data/RenderPassDepthStencilAttachment.ts similarity index 90% rename from src/data/IRenderPassDepthStencilAttachment.ts rename to src/data/RenderPassDepthStencilAttachment.ts index dd3b3c5923809598ebeeda6eb2a78a12dbeada56..334103349c3b5f2c743fd381dcef7f34c6d3fc1b 100644 --- a/src/data/IRenderPassDepthStencilAttachment.ts +++ b/src/data/RenderPassDepthStencilAttachment.ts @@ -1,9 +1,9 @@ -import { ITextureView } from "./ITextureView"; +import { TextureView } from "./TextureView"; /** * 深度模板附件。 */ -export interface IRenderPassDepthStencilAttachment +export interface RenderPassDepthStencilAttachment { /** * 深度附件视图。 @@ -13,7 +13,7 @@ export interface IRenderPassDepthStencilAttachment * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/framebufferRenderbuffer * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/framebufferTexture2D */ - readonly view?: ITextureView; + readonly view?: TextureView; /** * 清除后填充深度值。 diff --git a/src/data/IRenderPassDescriptor.ts b/src/data/RenderPassDescriptor.ts similarity index 66% rename from src/data/IRenderPassDescriptor.ts rename to src/data/RenderPassDescriptor.ts index a8331caadb761a77876d6f22f89814428445d2bb..3f44a05bae22693fc947854f7c9c1b1075346147 100644 --- a/src/data/IRenderPassDescriptor.ts +++ b/src/data/RenderPassDescriptor.ts @@ -1,10 +1,10 @@ -import { IRenderPassColorAttachment } from "./IRenderPassColorAttachment"; -import { IRenderPassDepthStencilAttachment } from "./IRenderPassDepthStencilAttachment"; +import { RenderPassColorAttachment } from "./RenderPassColorAttachment"; +import { RenderPassDepthStencilAttachment } from "./RenderPassDepthStencilAttachment"; /** * 渲染通道描述 */ -export interface IRenderPassDescriptor +export interface RenderPassDescriptor { /** * 标签。 @@ -16,14 +16,14 @@ export interface IRenderPassDescriptor /** * 颜色附件 */ - readonly colorAttachments?: readonly IRenderPassColorAttachment[]; + readonly colorAttachments?: readonly RenderPassColorAttachment[]; /** * 深度模板附件。 * - * 当使用深度附件时,必须设置,使用默认值可设置为 `{}` 。 + * 当使用深度附件时,必须设置 。 */ - readonly depthStencilAttachment?: IRenderPassDepthStencilAttachment; + readonly depthStencilAttachment?: RenderPassDepthStencilAttachment; /** * 采用次数。 diff --git a/src/data/IRenderPipeline.ts b/src/data/RenderPipeline.ts similarity index 47% rename from src/data/IRenderPipeline.ts rename to src/data/RenderPipeline.ts index 5b20fd02649b5e9c11ac509bd2029c4aff6fe36b..f68d53b8840acc7e9d13a662e99aab32d5ff293f 100644 --- a/src/data/IRenderPipeline.ts +++ b/src/data/RenderPipeline.ts @@ -1,14 +1,15 @@ -import { IDepthStencilState } from "./IDepthStencilState"; -import { IFragmentState } from "./IFragmentState"; -import { IPrimitiveState } from "./IPrimitiveState"; -import { IVertexState } from "./IVertexState"; +import { DepthStencilState } from "./DepthStencilState"; +import { FragmentState } from "./FragmentState"; +import { PrimitiveState } from "./PrimitiveState"; +import { VertexState } from "./VertexState"; /** * 渲染管线。 + * + * 对应WebGPU的Pipeline。 */ -export interface IRenderPipeline +export interface RenderPipeline { - /** * 标签。 * @@ -17,24 +18,26 @@ export interface IRenderPipeline readonly label?: string; /** - * Describes the primitive-related properties of the pipeline. - * - * 图元拓扑结构。 + * 顶点着色器阶段描述。 */ - readonly primitive?: IPrimitiveState; + readonly vertex: VertexState; /** - * 顶点着色器阶段描述。 + * 片段着色器阶段描述。 */ - readonly vertex: IVertexState; + readonly fragment?: FragmentState; /** - * 片段着色器阶段描述。 + * Describes the primitive-related properties of the pipeline. + * + * 图元拓扑结构。 */ - readonly fragment?: IFragmentState; + readonly primitive?: PrimitiveState; /** * 深度模板阶段描述。 */ - readonly depthStencil?: IDepthStencilState; + readonly depthStencil?: DepthStencilState; + + _version?: number; } diff --git a/src/data/ISampler.ts b/src/data/Sampler.ts similarity index 86% rename from src/data/ISampler.ts rename to src/data/Sampler.ts index d7ce1a9a7ef1e16efdccdb15b1adfcff5b02b3bf..5079cbdba4a068953ca37b550b2da5c61dfe6644 100644 --- a/src/data/ISampler.ts +++ b/src/data/Sampler.ts @@ -1,4 +1,4 @@ -import { ICompareFunction } from "./IStencilFaceState"; +import { CompareFunction } from "./StencilFaceState"; /** * 纹理采样器。 @@ -10,7 +10,7 @@ import { ICompareFunction } from "./IStencilFaceState"; * @see https://www.orillusion.com/zh/webgpu.html#dictdef-gpusamplerdescriptor * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGL2RenderingContext/samplerParameter */ -export interface ISampler +export interface Sampler { /** * 标签。 @@ -27,7 +27,7 @@ export interface ISampler * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGL2RenderingContext/samplerParameter#gl.texture_wrap_s * @see https://gpuweb.github.io/gpuweb/#dom-gpusamplerdescriptor-addressmodeu */ - addressModeU?: IAddressMode; + readonly addressModeU?: IAddressMode; /** * 用于指定纹理在垂直方向(即T或V坐标轴)上的寻址模式。 @@ -37,7 +37,7 @@ export interface ISampler * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGL2RenderingContext/samplerParameter#gl.texture_wrap_t * @see https://gpuweb.github.io/gpuweb/#dom-gpusamplerdescriptor-addressmodev */ - addressModeV?: IAddressMode; + readonly addressModeV?: IAddressMode; /** * 用于指定纹理在深度方向(即R或W坐标轴)上的寻址模式。用于3D纹理或者纹理数组。 @@ -47,7 +47,7 @@ export interface ISampler * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGL2RenderingContext/samplerParameter#gl.texture_wrap_r * @see https://gpuweb.github.io/gpuweb/#dom-gpusamplerdescriptor-addressmodew */ - addressModeW?: IAddressMode; + readonly addressModeW?: IAddressMode; /** * 指定样本足迹小于或等于一个纹素时的采样行为 @@ -57,19 +57,19 @@ export interface ISampler * @see https://www.orillusion.com/zh/webgpu.html#dom-gpusamplerdescriptor-magfilter * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGL2RenderingContext/samplerParameter#gl.texture_mag_filter */ - magFilter?: IFilterMode; + readonly magFilter?: IFilterMode; /** * 指定样本足迹大于一个纹素时的采样行为。 * * 默认 "nearest"。 * - * 注:WebGL中如果使用mipmap相关值时需要设置 {@link ISampler.mipmapFilter} 值。 + * 注:WebGL中如果使用mipmap相关值时需要设置 {@link Sampler.mipmapFilter} 值。 * * @see https://www.orillusion.com/zh/webgpu.html#dom-gpusamplerdescriptor-minfilter * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGL2RenderingContext/samplerParameter#gl.texture_min_filter */ - minFilter?: IFilterMode; + readonly minFilter?: IFilterMode; /** * 指定在 mipmap 级别之间进行采样的行为。 @@ -79,12 +79,14 @@ export interface ISampler * @see https://www.orillusion.com/zh/webgpu.html#dom-gpusamplerdescriptor-mipmapfilter * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGL2RenderingContext/samplerParameter#gl.texture_min_filter */ - mipmapFilter?: IMipmapFilterMode; + readonly mipmapFilter?: IMipmapFilterMode; /** * 指定采样器使用的最大各向异性值夹具。 * 各向异性过滤。使用各向异性过滤能够使纹理的效果更好,但是会消耗更多的内存、CPU、GPU时间。默认为1。 * + * 仅当 minFilter 、magFilter 或 mipmapFilter 为 "linear" 时才有效,否则取 1。 + * * 默认 1。 * * 注:大多数实现支持范围在1到16之间(包括1和16)的maxAnisotropy值。所使用的maxAnisotropy值将被限制在平台支持的最大值内 @@ -92,21 +94,21 @@ export interface ISampler * @see https://www.orillusion.com/zh/webgpu.html#dom-gpusamplerdescriptor-maxanisotropy * @see https://developer.mozilla.org/en-US/docs/Web/API/EXT_texture_filter_anisotropic */ - maxAnisotropy?: number; + readonly maxAnisotropy?: number; /** * 采样时使用的最小Lod等级。 * * 默认 0。 */ - lodMinClamp?: number; + readonly lodMinClamp?: number; /** * 采样时使用的最大Lod等级。 * * 默认 16 。 */ - lodMaxClamp?: number; + readonly lodMaxClamp?: number; /** * 涉及纹理比较操作时需提供,采样器将是具有指定 GPUCompareFunction 的比较采样器。 @@ -115,7 +117,7 @@ export interface ISampler * * 注:比较采样器可能会使用过滤,但采样结果将是 依赖于实现并且可能不同于正常的过滤规则。 */ - compare?: ICompareFunction; + readonly compare?: CompareFunction; } /** diff --git a/src/data/IScissorRect.ts b/src/data/ScissorRect.ts similarity index 88% rename from src/data/IScissorRect.ts rename to src/data/ScissorRect.ts index 73bc0f5433763e1251722612c7ab33204f96d3e2..f4d9996edc2cab16d33bc16c65394c1391dd561a 100644 --- a/src/data/IScissorRect.ts +++ b/src/data/ScissorRect.ts @@ -11,13 +11,11 @@ * * x+width ≤ this.[[attachment_size]].width. * * y+height ≤ this.[[attachment_size]].height. * - * {@link GPURenderPassEncoder.setScissorRect} - * * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/scissor * @see https://www.orillusion.com/zh/webgpu.html#dom-gpurenderpassencoder-setscissorrect * */ -export interface IScissorRect +export interface ScissorRect { /** * 是否为Y轴朝上。 @@ -35,7 +33,7 @@ export interface IScissorRect * * 默认为 0 。 */ - readonly x: number, + readonly x?: number; /** * 剪刀盒纵向坐标(像素)。 @@ -44,7 +42,7 @@ export interface IScissorRect * * 默认为 0 。 */ - readonly y: number, + readonly y?: number; /** * 剪刀盒宽度(像素)。 @@ -53,7 +51,7 @@ export interface IScissorRect * * 默认为画布宽度或者渲染通道的附件宽度。 */ - readonly width: number, + readonly width: number; /** * 剪刀盒高度(像素)。 @@ -62,5 +60,5 @@ export interface IScissorRect * * 默认为画布高度或者渲染通道的附件高度。 */ - readonly height: number, + readonly height: number; } diff --git a/src/data/IStencilFaceState.ts b/src/data/StencilFaceState.ts similarity index 60% rename from src/data/IStencilFaceState.ts rename to src/data/StencilFaceState.ts index 02d254d328f1ea91d29883198b3725c95cc73f29..94cde7daca299bc456aa109ae300cf347443653a 100644 --- a/src/data/IStencilFaceState.ts +++ b/src/data/StencilFaceState.ts @@ -3,37 +3,37 @@ * * @see https://www.orillusion.com/zh/webgpu.html#dictdef-gpustencilfacestate */ -export interface IStencilFaceState +export interface StencilFaceState { /** * 在测试片元与 depthStencilAttachment 模板值时使用的 GPUCompareFunction。 * * 默认为 "always"。 */ - readonly compare?: ICompareFunction; + readonly compare?: CompareFunction; /** * 如果片元模板比较测试(由 compare 描述)失败,则执行的 GPUStencilOperation。 * * 默认为 "keep"。 */ - readonly failOp?: IStencilOperation; + readonly failOp?: StencilOperation; /** * 如果由 depthCompare 描述的片元深度比较失败,则执行的 GPUStencilOperation。 * * 默认为 "keep"。 */ - readonly depthFailOp?: IStencilOperation; + readonly depthFailOp?: StencilOperation; /** * 如果片元模板比较测试通过,则执行由compare描述的GPUStencilOperation。 * * 默认为 "keep"。 */ - readonly passOp?: IStencilOperation; + readonly passOp?: StencilOperation; } -export type ICompareFunction = "never" | "less" | "equal" | "less-equal" | "greater" | "not-equal" | "greater-equal" | "always"; +export type CompareFunction = "never" | "less" | "equal" | "less-equal" | "greater" | "not-equal" | "greater-equal" | "always"; -export type IStencilOperation = "keep" | "zero" | "replace" | "invert" | "increment-clamp" | "decrement-clamp" | "increment-wrap" | "decrement-wrap"; \ No newline at end of file +export type StencilOperation = "keep" | "zero" | "replace" | "invert" | "increment-clamp" | "decrement-clamp" | "increment-wrap" | "decrement-wrap"; \ No newline at end of file diff --git a/src/data/ISubmit.ts b/src/data/Submit.ts similarity index 57% rename from src/data/ISubmit.ts rename to src/data/Submit.ts index 35d8ffb4acf8b9f9c6b6fe7a8a3a8d12b4e38fd2..9a05068f5763063a9f5afcf5717dee0c5db9f860 100644 --- a/src/data/ISubmit.ts +++ b/src/data/Submit.ts @@ -1,14 +1,14 @@ -import { ICommandEncoder } from "./ICommandEncoder"; +import { CommandEncoder } from "./CommandEncoder"; /** * 一次 GPU 提交。 * * @see https://developer.mozilla.org/en-US/docs/Web/API/GPUQueue/submit */ -export interface ISubmit +export interface Submit { /** * 命令编码器列表。 */ - commandEncoders: ICommandEncoder[]; + commandEncoders: CommandEncoder[]; } diff --git a/src/data/ITexture.ts b/src/data/Texture.ts similarity index 39% rename from src/data/ITexture.ts rename to src/data/Texture.ts index 7050cbcbcdd9dd7a690f9d68c5fb45e105dcfa72..e29efe1b55f7b3dc0349b912b3f969dd3084b9e5 100644 --- a/src/data/ITexture.ts +++ b/src/data/Texture.ts @@ -1,7 +1,28 @@ +import { TextureDataSource } from "./TextureDataSource"; +import { TextureImageSource } from "./TextureImageSource"; + +/** + * 类似纹理,包含画布纹理以及正常纹理。 + * + * 如需扩展 ITextureLike,则需在 ITextureMap 中添加类型。 + */ +export type TextureLike = TextureLikeMap[keyof TextureLikeMap]; + +/** + * 如需扩展 ITextureLike,则需在 ITextureMap 中添加类型。 + */ +export interface TextureLikeMap +{ + /** + * 正常纹理。 + */ + Texture: Texture; +} + /** * 纹理 */ -export interface ITexture +export interface Texture { /** * 标签。 @@ -17,7 +38,7 @@ export interface ITexture * * 修改尺寸将会引发纹理销毁,使用时重新创建新纹理。 */ - size: ITextureSize; + readonly size: TextureSize; /** * 初始化纹理资源。 @@ -30,7 +51,7 @@ export interface ITexture * @see GPUQueue.copyExternalImageToTexture * @see GPUQueue.writeTexture */ - sources?: readonly ITextureSource[]; + readonly sources?: readonly TextureSource[]; /** * 初始化纹理后是否生成mipmap @@ -48,19 +69,19 @@ export interface ITexture * @see GPUQueue.copyExternalImageToTexture * @see GPUQueue.writeTexture */ - writeTextures?: readonly ITextureSource[]; + readonly writeTextures?: readonly TextureSource[]; /** * 纹理维度,默认为 "2d" 。 * * WebGL中不支持 "1d" "cube-array"。 */ - readonly dimension?: ITextureDimension; + readonly dimension?: TextureDimension; /** * 纹理格式。 默认为 "rgba8unorm", */ - readonly format?: ITextureFormat; + readonly format?: TextureFormat; /** * The number of mip levels the texture will contain. @@ -68,140 +89,53 @@ export interface ITexture readonly mipLevelCount?: number; } -/** - * 纹理资源。 - */ -export type ITextureSource = ITextureSourceMap[keyof ITextureSourceMap]; - -export interface ITextureSourceMap +export class Texture { - ITextureImageSource: ITextureImageSource; - ITextureDataSource: ITextureDataSource; -} - -/** - * 纹理的图片资源。 - * - * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/texSubImage2D - * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/texSubImage3D - * - * 注:不再支持参数 `border` - * - * ### WebGPU - * - * @see GPUQueue.copyExternalImageToTexture - */ -export interface ITextureImageSource -{ - /** - * 数据类型。 - */ - readonly __type?: "TextureImageSource"; - /** - * 图片资源。 + * 获取纹理每个像素占用的字节数量。 + * + * @param format 纹理格式。 */ - image: TexImageSource; + static getTextureBytesPerPixel(format: TextureFormat = "rgba8unorm") + { + const bytesPerPixel = formatMap[format]?.bytesPerPixel; - /** - * 读取图片上的像素坐标。 - */ - imageOrigin?: IImageOrigin; + console.assert(!!bytesPerPixel, `未处理格式 ${format} ,无法查询到该格式中每个像素占用的字节数量!`); - /** - * 写入纹理的mipmap层级索引。 - */ - mipLevel?: number; - - /** - * Defines the origin of the copy - the minimum corner of the texture sub-region to copy to/from. - * Together with `copySize`, defines the full copy sub-region. - * - * 写入纹理的位置。 - */ - textureOrigin?: ITextureOrigin; + return bytesPerPixel; + } /** - * Extents of the content to write from `source` to `destination`. - * - * 写入尺寸。 + * 获取纹理数据构造函数。 + + * @param format 纹理格式。 + * @returns */ - size?: ITextureSize + static getTextureDataConstructor(format: TextureFormat = "rgba8unorm") + { + const bytesPerPixel = formatMap[format]?.dataConstructor; - /** - * 是否Y轴翻转图片。 - * - * 注:WebGL(先翻转,再拷贝)与WebGPU(先拷贝,再翻转)处理方式不一样。此次已WebGL为准。当拷贝全图时,效果一致。 - */ - flipY?: boolean; + console.assert(!!bytesPerPixel, `未处理格式 ${format} ,无法查询到该格式的纹理数据构造函数!`); - /** - * 是否需要预乘透明度。 - */ - premultipliedAlpha?: boolean; + return bytesPerPixel; + } } /** - * 纹理的数据资源。 - * - * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/texSubImage2D - * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/texSubImage3D - * - * ### WebGPU - * - * @see GPUQueue.writeTexture + * 纹理资源。 */ -export interface ITextureDataSource -{ - /** - * 数据类型。 - */ - readonly __type: "TextureDataSource"; +export type TextureSource = ITextureSourceMap[keyof ITextureSourceMap]; - /** - * 纹理数据。 - */ - data: ArrayBufferView; - - /** - * Layout of the content in `data`. - * - * 纹理数据布局。 - */ - dataLayout?: ITextureDataLayout, - - /** - * 读取数据图片上的像素坐标。 - */ - dataImageOrigin?: IDataImageOrigin; - - /** - * 写入mipmap级别。 - * - * 默认为 0。 - */ - mipLevel?: number, - - /** - * Defines the origin of the copy - the minimum corner of the texture sub-region to copy to/from. - * Together with `copySize`, defines the full copy sub-region. - * - * 写入纹理的位置。 - */ - textureOrigin?: ITextureOrigin; - - /** - * Extents of the content to write from `source` to `destination`. - * - * 写入尺寸。 - */ - size?: ITextureSize +export interface ITextureSourceMap +{ + ITextureImageSource: TextureImageSource; + ITextureDataSource: TextureDataSource; } /** * 纹理数据布局。 */ -export interface ITextureDataLayout +export interface TextureDataLayout { /** * 默认为 0。字节偏移,一般用于跳过文件头部非纹理数据部分。 @@ -228,39 +162,39 @@ export interface ITextureDataLayout /** * 图片中的坐标。 */ -export type IImageOrigin = readonly [x: number, y: number]; +export type ImageOrigin = readonly [x: number, y: number]; /** * 数据图片中的坐标。depthOrArrayLayers 表示数据中包含有多张图片中的第几张,只在纹理为2d纹理数组或者3d纹理时生效。 */ -export type IDataImageOrigin = readonly [x: number, y: number, depthOrArrayLayers?: number]; +export type DataImageOrigin = readonly [x: number, y: number, depthOrArrayLayers?: number]; /** * 图片尺寸 */ -export type IImageSize = readonly [width: number, height: number]; +export type ImageSize = readonly [width: number, height: number]; /** * 纹理尺寸,包含纹理的宽度、高度以及深度或者层数。 * * depthOrArrayLayers: 当纹理为3d纹理时表示深度,2d纹理数组时表示数组索引,cube纹理时表示6个面的索引。 */ -export type ITextureSize = readonly [width: number, height: number, depthOrArrayLayers?: number]; +export type TextureSize = readonly [width: number, height: number, depthOrArrayLayers?: number]; /** * 纹理内的坐标位置。 */ -export type ITextureOrigin = readonly [x: number, y: number, depthOrArrayLayers?: number]; +export type TextureOrigin = readonly [x: number, y: number, depthOrArrayLayers?: number]; /** * 纹理规格维度。 */ -export type ITextureDimension = "1d" | "2d" | "2d-array" | "cube" | "cube-array" | "3d"; +export type TextureDimension = "1d" | "2d" | "2d-array" | "cube" | "cube-array" | "3d"; /** * 参考 GPUTextureFormat */ -export type ITextureFormat = +export type TextureFormat = | "r8unorm" | "r8snorm" @@ -356,4 +290,117 @@ export type ITextureFormat = | "astc-12x10-unorm" | "astc-12x10-unorm-srgb" | "astc-12x12-unorm" - | "astc-12x12-unorm-srgb"; \ No newline at end of file + | "astc-12x12-unorm-srgb"; + +const formatMap: { + [key: string]: { + /** + * 每个像素占用的字节数量 + */ + bytesPerPixel: number, + + /** + * 数据构造函数 + */ + dataConstructor?: Uint8ArrayConstructor | Int8ArrayConstructor + | Uint16ArrayConstructor | Int16ArrayConstructor + | Uint32ArrayConstructor | Int32ArrayConstructor + | Float32ArrayConstructor, + } +} = { + r8unorm: { bytesPerPixel: 1, dataConstructor: Uint8Array }, + r8snorm: { bytesPerPixel: 1, dataConstructor: Int8Array }, + r8uint: { bytesPerPixel: 1, dataConstructor: Uint8Array }, + r8sint: { bytesPerPixel: 1, dataConstructor: Int8Array }, + r16uint: { bytesPerPixel: 2, dataConstructor: Uint16Array }, + r16sint: { bytesPerPixel: 2, dataConstructor: Int16Array }, + r16float: { bytesPerPixel: 2, dataConstructor: Uint16Array }, + rg8unorm: { bytesPerPixel: 2, dataConstructor: Uint8Array }, + rg8snorm: { bytesPerPixel: 2, dataConstructor: Int8Array }, + rg8uint: { bytesPerPixel: 2, dataConstructor: Uint8Array }, + rg8sint: { bytesPerPixel: 2, dataConstructor: Int8Array }, + r32uint: { bytesPerPixel: 4, dataConstructor: Uint32Array }, + r32sint: { bytesPerPixel: 4, dataConstructor: Int32Array }, + r32float: { bytesPerPixel: 4, dataConstructor: Float32Array }, + rg16uint: { bytesPerPixel: 4, dataConstructor: Uint16Array }, + rg16sint: { bytesPerPixel: 4, dataConstructor: Int16Array }, + rg16float: { bytesPerPixel: 4, dataConstructor: Uint16Array }, + rgba8unorm: { bytesPerPixel: 4, dataConstructor: Uint8Array }, + "rgba8unorm-srgb": { bytesPerPixel: 4, dataConstructor: Uint8Array }, + rgba8snorm: { bytesPerPixel: 4, dataConstructor: Int8Array }, + rgba8uint: { bytesPerPixel: 4, dataConstructor: Uint8Array }, + rgba8sint: { bytesPerPixel: 4, dataConstructor: Int8Array }, + bgra8unorm: { bytesPerPixel: 4, dataConstructor: Uint8Array }, + "bgra8unorm-srgb": { bytesPerPixel: 4, dataConstructor: Uint8Array }, + rgb9e5ufloat: { bytesPerPixel: 4, dataConstructor: Uint32Array }, + rgb10a2uint: { bytesPerPixel: 4, dataConstructor: Uint32Array }, + rgb10a2unorm: { bytesPerPixel: 4, dataConstructor: Uint32Array }, + rg11b10ufloat: { bytesPerPixel: 4, dataConstructor: Uint32Array }, + rg32uint: { bytesPerPixel: 8, dataConstructor: Uint32Array }, + rg32sint: { bytesPerPixel: 8, dataConstructor: Int32Array }, + rg32float: { bytesPerPixel: 8, dataConstructor: Float32Array }, + rgba16uint: { bytesPerPixel: 8, dataConstructor: Uint16Array }, + rgba16sint: { bytesPerPixel: 8, dataConstructor: Int16Array }, + rgba16float: { bytesPerPixel: 8, dataConstructor: Uint16Array }, + rgba32uint: { bytesPerPixel: 16, dataConstructor: Uint32Array }, + rgba32sint: { bytesPerPixel: 16, dataConstructor: Int32Array }, + rgba32float: { bytesPerPixel: 16, dataConstructor: Float32Array }, + stencil8: { bytesPerPixel: 1, dataConstructor: Uint8Array }, + depth16unorm: { bytesPerPixel: 2, dataConstructor: Uint16Array }, + depth24plus: { bytesPerPixel: 3, dataConstructor: Uint8Array }, + "depth24plus-stencil8": { bytesPerPixel: 4 }, + depth32float: { bytesPerPixel: 4 }, + "depth32float-stencil8": { bytesPerPixel: 5 }, + "bc1-rgba-unorm": undefined, + "bc1-rgba-unorm-srgb": undefined, + "bc2-rgba-unorm": undefined, + "bc2-rgba-unorm-srgb": undefined, + "bc3-rgba-unorm": undefined, + "bc3-rgba-unorm-srgb": undefined, + "bc4-r-unorm": undefined, + "bc4-r-snorm": undefined, + "bc5-rg-unorm": undefined, + "bc5-rg-snorm": undefined, + "bc6h-rgb-ufloat": undefined, + "bc6h-rgb-float": undefined, + "bc7-rgba-unorm": undefined, + "bc7-rgba-unorm-srgb": undefined, + "etc2-rgb8unorm": undefined, + "etc2-rgb8unorm-srgb": undefined, + "etc2-rgb8a1unorm": undefined, + "etc2-rgb8a1unorm-srgb": undefined, + "etc2-rgba8unorm": undefined, + "etc2-rgba8unorm-srgb": undefined, + "eac-r11unorm": undefined, + "eac-r11snorm": undefined, + "eac-rg11unorm": undefined, + "eac-rg11snorm": undefined, + "astc-4x4-unorm": undefined, + "astc-4x4-unorm-srgb": undefined, + "astc-5x4-unorm": undefined, + "astc-5x4-unorm-srgb": undefined, + "astc-5x5-unorm": undefined, + "astc-5x5-unorm-srgb": undefined, + "astc-6x5-unorm": undefined, + "astc-6x5-unorm-srgb": undefined, + "astc-6x6-unorm": undefined, + "astc-6x6-unorm-srgb": undefined, + "astc-8x5-unorm": undefined, + "astc-8x5-unorm-srgb": undefined, + "astc-8x6-unorm": undefined, + "astc-8x6-unorm-srgb": undefined, + "astc-8x8-unorm": undefined, + "astc-8x8-unorm-srgb": undefined, + "astc-10x5-unorm": undefined, + "astc-10x5-unorm-srgb": undefined, + "astc-10x6-unorm": undefined, + "astc-10x6-unorm-srgb": undefined, + "astc-10x8-unorm": undefined, + "astc-10x8-unorm-srgb": undefined, + "astc-10x10-unorm": undefined, + "astc-10x10-unorm-srgb": undefined, + "astc-12x10-unorm": undefined, + "astc-12x10-unorm-srgb": undefined, + "astc-12x12-unorm": undefined, + "astc-12x12-unorm-srgb": undefined, +}; \ No newline at end of file diff --git a/src/data/TextureDataSource.ts b/src/data/TextureDataSource.ts new file mode 100644 index 0000000000000000000000000000000000000000..1cf65de5ba30e2b7c85b448a80008a6481651c32 --- /dev/null +++ b/src/data/TextureDataSource.ts @@ -0,0 +1,58 @@ +import { TextureDataLayout, DataImageOrigin, TextureOrigin, TextureSize } from "./Texture"; + +/** + * 纹理的数据资源。 + * + * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/texSubImage2D + * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/texSubImage3D + * + * ### WebGPU + * + * @see GPUQueue.writeTexture + */ +export interface TextureDataSource +{ + /** + * 数据类型。 + */ + readonly __type__: "TextureDataSource"; + + /** + * 纹理数据。 + */ + data: ArrayBufferView; + + /** + * Layout of the content in `data`. + * + * 纹理数据布局。 + */ + dataLayout?: TextureDataLayout; + + /** + * 读取数据图片上的像素坐标。 + */ + dataImageOrigin?: DataImageOrigin; + + /** + * 写入mipmap级别。 + * + * 默认为 0。 + */ + mipLevel?: number; + + /** + * Defines the origin of the copy - the minimum corner of the texture sub-region to copy to/from. + * Together with `copySize`, defines the full copy sub-region. + * + * 写入纹理的位置。 + */ + textureOrigin?: TextureOrigin; + + /** + * Extents of the content to write from `source` to `destination`. + * + * 写入尺寸。 + */ + size?: TextureSize +} diff --git a/src/data/TextureImageSource.ts b/src/data/TextureImageSource.ts new file mode 100644 index 0000000000000000000000000000000000000000..dc9a823afb5feff30fbe93c88cd2d9187f495b5b --- /dev/null +++ b/src/data/TextureImageSource.ts @@ -0,0 +1,95 @@ +import { ImageOrigin, ImageSize, TextureOrigin, TextureSize } from "./Texture"; + +/** + * 纹理的图片资源。 + * + * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/texSubImage2D + * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/texSubImage3D + * + * 注:不再支持参数 `border` + * + * ### WebGPU + * + * @see GPUQueue.copyExternalImageToTexture + */ +export interface TextureImageSource +{ + /** + * 数据类型。 + */ + readonly __type__?: "TextureImageSource"; + + /** + * 图片资源。 + */ + image: TexImageSource; + + /** + * 读取图片上的像素坐标。 + */ + imageOrigin?: ImageOrigin; + + /** + * 写入纹理的mipmap层级索引。 + */ + mipLevel?: number; + + /** + * Defines the origin of the copy - the minimum corner of the texture sub-region to copy to/from. + * Together with `copySize`, defines the full copy sub-region. + * + * 写入纹理的位置。 + */ + textureOrigin?: TextureOrigin; + + /** + * Extents of the content to write from `source` to `destination`. + * + * 写入尺寸。 + */ + size?: TextureSize + + /** + * 是否Y轴翻转图片。 + * + * 注:WebGL(先翻转,再拷贝)与WebGPU(先拷贝,再翻转)处理方式不一样。此次已WebGL为准。当拷贝全图时,效果一致。 + */ + flipY?: boolean; + + /** + * 是否需要预乘透明度。 + */ + premultipliedAlpha?: boolean; +} + +export class TextureImageSource +{ + /** + * 获取纹理的图片资源尺寸。 + * + * @param texImageSource 纹理的图片资源。 + * @returns + */ + static getTexImageSourceSize(image: TexImageSource): ImageSize + { + let width: number; + let height: number; + if (image instanceof VideoFrame) + { + width = image.codedWidth; + height = image.codedHeight; + } + else if (image instanceof HTMLVideoElement) + { + width = image.videoWidth; + height = image.videoHeight; + } + else + { + width = image.width; + height = image.height; + } + + return [width, height]; + } +} \ No newline at end of file diff --git a/src/data/ITextureView.ts b/src/data/TextureView.ts similarity index 47% rename from src/data/ITextureView.ts rename to src/data/TextureView.ts index 51b076b39d4f40df868d1c8640571442bf6f902e..da6fd2c06f0a12c0211408241b26ff3b1361275b 100644 --- a/src/data/ITextureView.ts +++ b/src/data/TextureView.ts @@ -1,27 +1,9 @@ -import { ITexture } from "./ITexture"; - -/** - * 如需扩展 ITextureLike,则需在 ITextureMap 中添加类型。 - */ -export interface ITextureLikeMap -{ - /** - * 正常纹理。 - */ - ITexture: ITexture; -} - -/** - * 类似纹理,包含画布纹理以及正常纹理。 - * - * 如需扩展 ITextureLike,则需在 ITextureMap 中添加类型。 - */ -export type ITextureLike = ITextureLikeMap[keyof ITextureLikeMap]; +import { TextureLike } from "./Texture"; /** * 纹理视图。 */ -export interface ITextureView +export interface TextureView { /** * 标签。 @@ -33,7 +15,7 @@ export interface ITextureView /** * 产生视图的纹理。 */ - readonly texture: ITextureLike; + readonly texture: TextureLike; /** * mipmap级别。 @@ -48,4 +30,4 @@ export interface ITextureView * 默认为 0。 */ readonly baseArrayLayer?: number; -} \ No newline at end of file +} diff --git a/src/data/IVertexAttributes.ts b/src/data/VertexAttributes.ts similarity index 49% rename from src/data/IVertexAttributes.ts rename to src/data/VertexAttributes.ts index 4d399fcd8d85f6aeac9f4cde602b417e1e3dba0b..fdd8fa9643f43d9879fee2c2a39b5d4960be190b 100644 --- a/src/data/IVertexAttributes.ts +++ b/src/data/VertexAttributes.ts @@ -1,27 +1,29 @@ +import { vertexFormatMap } from "../consts/vertexFormatMap"; + /** * 顶点属性数据映射。 */ -export interface IVertexAttributes +export interface VertexAttributes { - [name: string]: IVertexAttribute; + [name: string]: VertexAttribute; } /** * 顶点属性数据。 */ -export interface IVertexAttribute +export interface VertexAttribute { /** * 顶点数据。 */ - data: IVertexDataTypes; + data: VertexDataTypes; /** * 顶点数据格式。 * * 由于提供的数据并不一定与着色器中格式一直,因此必须提供与着色器中兼容的数据格式。 */ - readonly format: IVertexFormat; + readonly format: VertexFormat; /** * 所在顶点数据中的偏移字节数。 @@ -42,12 +44,46 @@ export interface IVertexAttribute * * 默认 `"vertex"` 。 */ - readonly stepMode?: IVertexStepMode; + readonly stepMode?: VertexStepMode; } -export type IVertexStepMode = "vertex" | "instance"; +export class VertexAttribute +{ + /** + * 获取顶点属性数据的顶点数量。 + * + * @param attribute 顶点属性数据。 + * @returns + */ + static getVertexCount(attribute: VertexAttribute) + { + // 单个顶点属性数据尺寸。 + const attributeSize = VertexAttribute.getVertexByteSize(attribute); + const offset = attribute.offset || 0; + // 一个顶点数据尺寸,可能包括多个顶点属性(例如一个position 和 uv 共 3*4 + 2*4 = 20 字节)。 + const arrayStride = attribute.arrayStride || attributeSize; + + const attributeCount = (attribute.data.byteLength - offset) / arrayStride; + + return attributeCount; + } -export type IVertexDataTypes = | Float32Array + /** + * 获取顶点属性数据的字节尺寸。 + * + * @param attribute 顶点属性数据。 + */ + static getVertexByteSize(attribute: VertexAttribute) + { + const attributeSize = vertexFormatMap[attribute.format].byteSize; + + return attributeSize; + } +} + +export type VertexStepMode = "vertex" | "instance"; + +export type VertexDataTypes = | Float32Array | Uint32Array | Int32Array | Uint16Array @@ -56,7 +92,10 @@ export type IVertexDataTypes = | Float32Array | Uint8Array | Int8Array; -export type IVertexFormat = +/** + * 顶点数据格式。 + */ +export type VertexFormat = | "uint8x2" | "uint8x4" | "sint8x2" diff --git a/src/data/IVertexState.ts b/src/data/VertexState.ts similarity index 79% rename from src/data/IVertexState.ts rename to src/data/VertexState.ts index 8503d5c165883c3539cb76f2eb80493254115bc3..ec5a64c392accf5398b2913d0863a5b30db0582b 100644 --- a/src/data/IVertexState.ts +++ b/src/data/VertexState.ts @@ -1,7 +1,7 @@ /** * 顶点着色器阶段描述。 */ -export interface IVertexState +export interface VertexState { /** * 着色器代码。 diff --git a/src/data/IViewport.ts b/src/data/Viewport.ts similarity index 91% rename from src/data/IViewport.ts rename to src/data/Viewport.ts index d8c49a1b68178e25db5b5942e9226d22af5c2f66..4676c4504aa07d1855b1a940aff8883709986e25 100644 --- a/src/data/IViewport.ts +++ b/src/data/Viewport.ts @@ -1,3 +1,4 @@ + /** * 视窗。 * @@ -17,7 +18,7 @@ * @see https://gpuweb.github.io/gpuweb/#dom-gpurenderpassencoder-setviewport * */ -export interface IViewport +export interface Viewport { /** * 是否为Y轴朝上。 @@ -35,7 +36,7 @@ export interface IViewport * * 默认为 0 。 */ - x: number, + x?: number; /** * 视窗垂直坐标(像素)。 @@ -44,7 +45,7 @@ export interface IViewport * * 默认为 0 。 */ - y: number, + y?: number; /** * 视窗宽度(像素)。 @@ -53,7 +54,7 @@ export interface IViewport * * 默认为画布宽度或者渲染通道的附件宽度。 */ - width: number, + width: number; /** * 视窗高度(像素)。 @@ -62,5 +63,5 @@ export interface IViewport * * 默认为画布高度或者渲染通道的附件高度。 */ - height: number, + height: number; } diff --git a/src/data/IBuffer.ts b/src/data/WriteBuffer.ts similarity index 48% rename from src/data/IBuffer.ts rename to src/data/WriteBuffer.ts index 42d170c86b84696e16b35515506cdb6867f11531..98bdb5d857a815d90c4040d791a89a64931d4d14 100644 --- a/src/data/IBuffer.ts +++ b/src/data/WriteBuffer.ts @@ -1,44 +1,6 @@ -import { TypedArray } from "./TypedArray"; +import { TypedArray } from "../types/TypedArray"; -/** - * 缓冲区 - * - * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/bufferData - * - * {@link GPUBufferDescriptor} - * - * {@link GPUBuffer} - */ -export interface IBuffer -{ - /** - * 标签。 - * - * 用于调试。 - */ - readonly label?: string; - - /** - * 缓冲区尺寸,单位为字节。 - * - * 尺寸必须为4的倍数。 - */ - readonly size: number; - - /** - * 缓冲区数据。 - */ - data?: TypedArray; - - /** - * 写缓冲区。 - * - * {@link GPUQueue.writeBuffer} - */ - writeBuffers?: IWriteBuffer[]; -} - -export interface IWriteBuffer +export interface WriteBuffer { /** * GPU缓冲区写入起始位置。 diff --git a/src/global.ts b/src/global.ts new file mode 100644 index 0000000000000000000000000000000000000000..0777368edaa1b476172bd1422f16e0212639eaf9 --- /dev/null +++ b/src/global.ts @@ -0,0 +1,8 @@ +export { }; +declare global +{ + /** + * 是否为开发模式。 + */ + var __DEV__: boolean; +} diff --git a/src/index.ts b/src/index.ts index 98fdad18c58d69e2b9de9ef123240fce738f7646..4d890ebac5ef21d5f862c3999c7cf30dddd4168e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,34 +1,50 @@ -export * from "./data/IBlendComponent"; -export * from "./data/IBlendState"; -export * from "./data/IBuffer"; -export * from "./data/ICanvasContext"; -export * from "./data/ICanvasTexture"; -export * from "./data/IColorTargetState"; -export * from "./data/ICommandEncoder"; -export * from "./data/ICopyBufferToBuffer"; -export * from "./data/ICopyTextureToTexture"; -export * from "./data/IDepthStencilState"; -export * from "./data/IFragmentState"; -export * from "./data/IPrimitiveState"; -export * from "./data/IRenderObject"; -export * from "./data/IRenderPass"; -export * from "./data/IRenderPassColorAttachment"; -export * from "./data/IRenderPassDepthStencilAttachment"; -export * from "./data/IRenderPassDescriptor"; -export * from "./data/IRenderPipeline"; -export * from "./data/ISampler"; -export * from "./data/IScissorRect"; -export * from "./data/IStencilFaceState"; -export * from "./data/ISubmit"; -export * from "./data/ITexture"; -export * from "./data/ITextureView"; -export * from "./data/IVertexAttributes"; -export * from "./data/IVertexState"; -export * from "./data/IViewport"; -export * from "./data/TypedArray"; -export * from "./data/UnReadonly"; +export * from "./consts/vertexFormatMap"; -export * from "./utils/getBlendConstantColor"; -export * from "./utils/getTexImageSourceSize"; -export * from "./utils/getTextureBytesPerPixel"; +export * from "./data/BindingResources"; +export * from "./data/BlendComponent"; +export * from "./data/BlendState"; +export * from "./data/Buffer"; +export * from "./data/BufferBinding"; +export * from "./data/CanvasContext"; +export * from "./data/CanvasTexture"; +export * from "./data/ColorTargetState"; +export * from "./data/CommandEncoder"; +export * from "./data/CopyBufferToBuffer"; +export * from "./data/CopyTextureToTexture"; +export * from "./data/DepthStencilState"; +export * from "./data/DrawIndexed"; +export * from "./data/DrawVertex"; +export * from "./data/FragmentState"; +export * from "./data/ImageCopyTexture"; +export * from "./data/OcclusionQuery"; +export * from "./data/PrimitiveState"; +export * from "./data/ReadPixels"; +export * from "./data/RenderObject"; +export * from "./data/RenderPass"; +export * from "./data/RenderPassColorAttachment"; +export * from "./data/RenderPassDepthStencilAttachment"; +export * from "./data/RenderPassDescriptor"; +export * from "./data/RenderPipeline"; +export * from "./data/Sampler"; +export * from "./data/ScissorRect"; +export * from "./data/StencilFaceState"; +export * from "./data/Submit"; +export * from "./data/Texture"; +export * from "./data/TextureDataSource"; +export * from "./data/TextureImageSource"; +export * from "./data/TextureView"; +export * from "./data/VertexAttributes"; +export * from "./data/VertexState"; +export * from "./data/Viewport"; +export * from "./data/WriteBuffer"; +export * from "./reactivity"; + +export * from "./internal/BufferBindingInfo"; + +export * from "./types/TypedArray"; +export * from "./types/UnReadonly"; + +export * from "./utils/ChainMap"; + +import "./global" \ No newline at end of file diff --git a/src/internal/BufferBindingInfo.ts b/src/internal/BufferBindingInfo.ts new file mode 100644 index 0000000000000000000000000000000000000000..764a0d5793a0d10b8bb6972de5fb692331bdf432 --- /dev/null +++ b/src/internal/BufferBindingInfo.ts @@ -0,0 +1,13 @@ +/** + * 缓冲区绑定信息。 + */ +export interface BufferBindingInfo +{ + size: number; + items: { + paths: string[]; + offset: number; + size: number; + Cls: Float32ArrayConstructor | Int32ArrayConstructor | Uint32ArrayConstructor | Int16ArrayConstructor; + }[] +} diff --git a/src/reactivity.ts b/src/reactivity.ts new file mode 100644 index 0000000000000000000000000000000000000000..dbfb88700c32f03f90e639b0b36bbada2b3e1595 --- /dev/null +++ b/src/reactivity.ts @@ -0,0 +1,17 @@ +import { UnReadonly } from "@feng3d/render-api"; + +import { reactive as vueReactive, toRaw as vueToRaw } from "@feng3d/reactivity"; +export { computed, type Computed, effect } from "@feng3d/reactivity"; + +/** + * Vue响应式。 + * + * 额外把只读属性去掉(引擎希望原始数据只用于访问,不直接修改,通过修改响应式数据来触发引擎更新逻辑并间接修改原始数据)。 + */ +export const reactive: (target: T) => UnReadonly = vueReactive as any; + +/** + * Vue原始数据。 + * + */ +export const toRaw: (observed: T) => T = vueToRaw as any; diff --git a/src/data/TypedArray.ts b/src/types/TypedArray.ts similarity index 100% rename from src/data/TypedArray.ts rename to src/types/TypedArray.ts diff --git a/src/data/UnReadonly.ts b/src/types/UnReadonly.ts similarity index 100% rename from src/data/UnReadonly.ts rename to src/types/UnReadonly.ts diff --git a/src/utils/ChainMap.ts b/src/utils/ChainMap.ts new file mode 100644 index 0000000000000000000000000000000000000000..2fc63488e8140ac6f3c9d68ad89b7cf05745dd14 --- /dev/null +++ b/src/utils/ChainMap.ts @@ -0,0 +1,130 @@ +/** + * 链式字典。 + * + * 使用WeakMap构建的,支持多个key数组对应一个值。 + */ +export class ChainMap, V> +{ + /** + * 根字典。 + */ + private _根字典 = new WeakMap(); + private _数量 = 0; + + /** + * 获取键对应的值。 + * + * @param keys 键。 + * @returns 值。 + */ + get(keys: K): V + { + const keysLength = keys.length; + let map = this._根字典; + let key: any; + + for (let i = 0, n = keysLength - 1; i < n; i++) + { + key = wrapKey(keys[i]); + map = map.get(key); + + if (map === undefined) return undefined; + } + + key = wrapKey(keys[keysLength - 1]); + return map.get(key); + } + + /** + * 设置映射。 + * + * @param keys 键。 + * @param value 值。 + * + * @returns 返回设置的值。 + */ + set(keys: K, value: V) + { + const keysLength = keys.length; + let map = this._根字典; + let key: any; + + for (let i = 0; i < keysLength - 1; i++) + { + key = wrapKey(keys[i]); + + if (!map.has(key)) + { + map.set(key, new WeakMap()); + } + + map = map.get(key); + } + + key = wrapKey(keys[keysLength - 1]); + if (!map.has(key)) + { + map.set(key, value); + this._数量++; + } + + return value; + } + + /** + * 删除映射。 + * + * @param keys 键。 + * @returns 如果找到目标值且被删除返回 `true` ,否则返回 `false` 。 + */ + delete(keys: K): boolean + { + const keysLength = keys.length; + let map = this._根字典; + let key: any; + + for (let i = 0; i < keysLength - 1; i++) + { + key = wrapKey(keys[i]); + map = map.get(key); + + if (map === undefined) return false; + } + + key = wrapKey(keys[keysLength - 1]); + const result = map.delete(key); + if (result) this._数量--; + + return result; + } +} + +// 创建一个普通 Map 用于存储原始值和包装对象的映射 +const keyMap = new Map(); +// 用于生成唯一 ID 的计数器 +let idCounter = 0; + +// 包装函数,将非对象值包装成对象 +function wrapKey(key: any) +{ + if (typeof key === 'object' && key !== null) + { + // 如果 key 已经是对象,则直接返回 + return key; + } + if (keyMap.has(key)) + { + // 如果原始值已经有对应的包装对象,直接返回 + return keyMap.get(key); + } + // 为非对象 key 生成一个唯一 ID + const id = idCounter++; + // 创建一个包装对象 + const wrapper = { + __id: id, + __value: key + }; + // 存储原始值和包装对象的映射 + keyMap.set(key, wrapper); + return wrapper; +} \ No newline at end of file diff --git a/src/utils/getBlendConstantColor.ts b/src/utils/getBlendConstantColor.ts deleted file mode 100644 index 1622841b7936b6cd595e61b2b4427fdc18bbd92f..0000000000000000000000000000000000000000 --- a/src/utils/getBlendConstantColor.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { IBlendState } from "../data/IBlendState"; -import { IColor } from "../data/IRenderPassColorAttachment"; - -/** - * 当混合系数用到了混合常量值时设置混合常量值。 - * - * @param blend - * @returns - */ -export function getBlendConstantColor(blend: IBlendState): IColor -{ - if (!blend) return undefined; - - const { color, alpha, constantColor } = blend; - - // 当混合系数用到了混合常量值时设置混合常量值。 - if (0 - || color?.srcFactor === "constant" - || color?.srcFactor === "one-minus-constant" - || color?.dstFactor === "constant" - || color?.dstFactor === "one-minus-constant" - || alpha?.srcFactor === "constant" - || alpha?.srcFactor === "one-minus-constant" - || alpha?.dstFactor === "constant" - || alpha?.dstFactor === "one-minus-constant" - ) - { - return constantColor ?? [0, 0, 0, 0]; - } - - return undefined; -} diff --git a/src/utils/getTexImageSourceSize.ts b/src/utils/getTexImageSourceSize.ts deleted file mode 100644 index cd2114a580ec5f81d5c7374adc1b2de27b5d3e14..0000000000000000000000000000000000000000 --- a/src/utils/getTexImageSourceSize.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { IImageSize } from "../data/ITexture"; - -/** - * 获取纹理的图片资源尺寸。 - * - * @param texImageSource 纹理的图片资源。 - * @returns - */ -export function getTexImageSourceSize(texImageSource: TexImageSource): IImageSize -{ - let width: number; - let height: number; - if (texImageSource instanceof VideoFrame) - { - width = texImageSource.codedWidth; - height = texImageSource.codedHeight; - } - else if (texImageSource instanceof HTMLVideoElement) - { - width = texImageSource.videoWidth; - height = texImageSource.videoHeight; - } - else - { - width = texImageSource.width; - height = texImageSource.height; - } - - return [width, height]; -} \ No newline at end of file diff --git a/src/utils/getTextureBytesPerPixel.ts b/src/utils/getTextureBytesPerPixel.ts deleted file mode 100644 index ebf9051371d68a733d89e7d1e6a64cca2f4f20f7..0000000000000000000000000000000000000000 --- a/src/utils/getTextureBytesPerPixel.ts +++ /dev/null @@ -1,120 +0,0 @@ -import { ITextureFormat } from "../data/ITexture"; - -/** - * 获取纹理每个像素占用的字节数量。 - * - * @param format - */ -export function getTextureBytesPerPixel(format: ITextureFormat = "rgba8unorm") -{ - const bytesPerPixel = formatMap[format]?.bytesPerPixel; - - console.assert(!!bytesPerPixel, `未处理格式 ${format} ,无法查询到该格式中每个像素占用的字节数量!`); - - return bytesPerPixel; -} - -const formatMap: { - [key: string]: { - /** - * 每个像素占用的字节数量 - */ - bytesPerPixel: number - } -} = { - r8unorm: { bytesPerPixel: 1 }, - r8snorm: { bytesPerPixel: 1 }, - r8uint: { bytesPerPixel: 1 }, - r8sint: { bytesPerPixel: 1 }, - r16uint: { bytesPerPixel: 2 }, - r16sint: { bytesPerPixel: 2 }, - r16float: { bytesPerPixel: 2 }, - rg8unorm: { bytesPerPixel: 2 }, - rg8snorm: { bytesPerPixel: 2 }, - rg8uint: { bytesPerPixel: 2 }, - rg8sint: { bytesPerPixel: 2 }, - r32uint: { bytesPerPixel: 4 }, - r32sint: { bytesPerPixel: 4 }, - r32float: { bytesPerPixel: 4 }, - rg16uint: { bytesPerPixel: 4 }, - rg16sint: { bytesPerPixel: 4 }, - rg16float: { bytesPerPixel: 4 }, - rgba8unorm: { bytesPerPixel: 4 }, - "rgba8unorm-srgb": { bytesPerPixel: 4 }, - rgba8snorm: { bytesPerPixel: 4 }, - rgba8uint: { bytesPerPixel: 4 }, - rgba8sint: { bytesPerPixel: 4 }, - bgra8unorm: { bytesPerPixel: 4 }, - "bgra8unorm-srgb": { bytesPerPixel: 4 }, - rgb9e5ufloat: { bytesPerPixel: 4 }, - rgb10a2uint: { bytesPerPixel: 4 }, - rgb10a2unorm: { bytesPerPixel: 4 }, - rg11b10ufloat: { bytesPerPixel: 4 }, - rg32uint: { bytesPerPixel: 8 }, - rg32sint: { bytesPerPixel: 8 }, - rg32float: { bytesPerPixel: 8 }, - rgba16uint: { bytesPerPixel: 8 }, - rgba16sint: { bytesPerPixel: 8 }, - rgba16float: { bytesPerPixel: 8 }, - rgba32uint: { bytesPerPixel: 16 }, - rgba32sint: { bytesPerPixel: 16 }, - rgba32float: { bytesPerPixel: 16 }, - stencil8: { bytesPerPixel: 1 }, - depth16unorm: { bytesPerPixel: 2 }, - depth24plus: { bytesPerPixel: 3 }, - "depth24plus-stencil8": { bytesPerPixel: 4 }, - depth32float: { bytesPerPixel: 4 }, - "depth32float-stencil8": { bytesPerPixel: 5 }, - "bc1-rgba-unorm": undefined, - "bc1-rgba-unorm-srgb": undefined, - "bc2-rgba-unorm": undefined, - "bc2-rgba-unorm-srgb": undefined, - "bc3-rgba-unorm": undefined, - "bc3-rgba-unorm-srgb": undefined, - "bc4-r-unorm": undefined, - "bc4-r-snorm": undefined, - "bc5-rg-unorm": undefined, - "bc5-rg-snorm": undefined, - "bc6h-rgb-ufloat": undefined, - "bc6h-rgb-float": undefined, - "bc7-rgba-unorm": undefined, - "bc7-rgba-unorm-srgb": undefined, - "etc2-rgb8unorm": undefined, - "etc2-rgb8unorm-srgb": undefined, - "etc2-rgb8a1unorm": undefined, - "etc2-rgb8a1unorm-srgb": undefined, - "etc2-rgba8unorm": undefined, - "etc2-rgba8unorm-srgb": undefined, - "eac-r11unorm": undefined, - "eac-r11snorm": undefined, - "eac-rg11unorm": undefined, - "eac-rg11snorm": undefined, - "astc-4x4-unorm": undefined, - "astc-4x4-unorm-srgb": undefined, - "astc-5x4-unorm": undefined, - "astc-5x4-unorm-srgb": undefined, - "astc-5x5-unorm": undefined, - "astc-5x5-unorm-srgb": undefined, - "astc-6x5-unorm": undefined, - "astc-6x5-unorm-srgb": undefined, - "astc-6x6-unorm": undefined, - "astc-6x6-unorm-srgb": undefined, - "astc-8x5-unorm": undefined, - "astc-8x5-unorm-srgb": undefined, - "astc-8x6-unorm": undefined, - "astc-8x6-unorm-srgb": undefined, - "astc-8x8-unorm": undefined, - "astc-8x8-unorm-srgb": undefined, - "astc-10x5-unorm": undefined, - "astc-10x5-unorm-srgb": undefined, - "astc-10x6-unorm": undefined, - "astc-10x6-unorm-srgb": undefined, - "astc-10x8-unorm": undefined, - "astc-10x8-unorm-srgb": undefined, - "astc-10x10-unorm": undefined, - "astc-10x10-unorm-srgb": undefined, - "astc-12x10-unorm": undefined, - "astc-12x10-unorm-srgb": undefined, - "astc-12x12-unorm": undefined, - "astc-12x12-unorm-srgb": undefined, -}; \ No newline at end of file diff --git a/test/index.spec.ts b/test/index.spec.ts index f2fedc8d1ed712454366bc30cd3f5239f7958ae6..9dcf08b60dd55fa6b762959e049d7931abbd2375 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -1,3 +1,4 @@ +import { TextureImageSource } from "@feng3d/render-api"; import { assert, describe, it } from "vitest"; describe("test", () =>