Class WebGpuResourceRepository

WebGPU Resource Repository that manages WebGPU resources and provides rendering functionality. This class serves as a central hub for creating, managing, and utilizing WebGPU resources such as textures, buffers, pipelines, and render passes.

ICGAPIResourceRepository

Hierarchy (view full)

Implements

Properties

Methods

addWebGpuDeviceWrapper allocateTexture attachColorBufferCubeToFrameBufferObject attachColorBufferLayerToFrameBufferObject attachColorBufferToFrameBufferObject attachDepthBufferToFrameBufferObject attachDepthStencilBufferToFrameBufferObject attachStencilBufferToFrameBufferObject clearCache clearFrameBuffer copyTextureData createBindGroupLayoutForDrawParameters createCompressedTexture createCompressedTextureFromBasis createCubeTexture createCubeTextureFromFiles createCubeTextureViewAsRenderTarget createFrameBufferObject createIndexBuffer createRenderBuffer createRenderTargetTexture createRenderTargetTextureArray createRenderTargetTextureCube createShaderProgram createStorageBlendShapeBuffer createStorageBuffer createTextureArray createTextureFromDataUri createTextureFromHTMLImageElement createTextureFromImageBitmapData createTextureSampler createTextureView2d createTextureView2dArray createTextureView2dArrayAsRenderTarget createTextureViewAsRenderTarget createTextureViewCube createUniformMorphOffsetsBuffer createUniformMorphWeightsBuffer createVertexBuffer createVertexBufferAndIndexBuffer createVertexBufferFromTypedArray deleteFrameBufferObject deleteRenderBuffer deleteTexture deleteVertexBuffer deleteVertexDataResources draw duplicateTextureAsMipmapped executeRenderBundle finishRenderBundleEncoder flush generateMipmaps generateMipmaps2d generateMipmapsCube getCanvasSize getOrCreateRenderPipeline getTexturePixelData getWebGpuDeviceWrapper isMippmappedTexture isSupportMultiViewVRRendering loadImageToMipLevelOfTexture2D recreateSystemDepthTexture resizeCanvas setColorWriteMask setVertexDataToPipeline setViewport updateIndexBuffer updateStorageBlendShapeBuffer updateStorageBuffer updateStorageBufferPartially updateUniformBufferForDrawParameters updateUniformMorphOffsetsBuffer updateUniformMorphWeightsBuffer updateVertexBuffer updateVertexBufferAndIndexBuffer getCgApiResourceRepository getInstance getWebGLResourceRepository getWebGpuResourceRepository

Properties

InvalidCGAPIResourceUid: -1 = -1

Invalid resource handle constant used to indicate failed resource creation or invalid resources

Methods

  • Adds a WebGPU device wrapper to the repository and initializes the command encoder. This must be called before using any WebGPU functionality.

    Parameters

    • webGpuDeviceWrapper: WebGpuDeviceWrapper

      The WebGPU device wrapper containing the device and context

    Returns void

  • Clears all cached resources including render pipelines, bind groups, and render bundles. This method should be called when resources need to be recreated or when the rendering context changes.

    Returns void

  • Create Cube Texture from image files.

    Parameters

    • baseUri: string

      the base uri to load images;

    • mipLevelCount: number

      the number of mip levels (include root level). if no mipmap, the value should be 1;

    • isNamePosNeg: boolean
    • hdriFormat: EnumIO

    Returns Promise<[number, Sampler]>

    the WebGLResourceHandle for the generated Cube Texture

  • Parameters

    • textureHandle: number
    • faceIdx: number
    • mipLevel: number

    Returns number

  • Creates a WebGPU index buffer from an accessor containing index data. Automatically converts UnsignedByte indices to UnsignedShort since WebGPU doesn't support 8-bit index buffers.

    Parameters

    • accessor: Accessor

      Accessor containing the index data to upload

    Returns number

    Handle to the created index buffer resource

  • Creates a render buffer for multisampling (MSAA) or as a render attachment. Render buffers are textures that are only used for rendering and cannot be sampled in shaders.

    Parameters

    • width: number

      Width of the render buffer in pixels

    • height: number

      Height of the render buffer in pixels

    • internalFormat: TextureParameterEnum

      Internal format of the render buffer

    • isMSAA: boolean

      Whether to enable multisampling

    • sampleCountMSAA: number

      Number of samples for MSAA (ignored if isMSAA is false)

    Returns number

    Handle to the created render buffer resource

  • Creates a render target texture that can be used as a color attachment in framebuffers. This texture can be rendered to and also used as a texture input in shaders.

    Parameters

    • params: {
          format: TextureParameterEnum;
          height: number;
          mipLevelCount: number;
          width: number;
      }

      Configuration for the render target texture

      • format: TextureParameterEnum

        Texture format for the render target

      • height: number

        Height of the texture in pixels

      • mipLevelCount: number

        Number of mipmap levels to create

      • width: number

        Width of the texture in pixels

    Returns number

    Handle to the created render target texture resource

  • Creates a render target texture array that can hold multiple 2D textures. Useful for techniques like shadow mapping with multiple lights or layered rendering.

    Parameters

    • params: {
          arrayLength: number;
          format: EnumIO;
          height: number;
          internalFormat: TextureParameterEnum;
          level: number;
          type: ComponentTypeEnum;
          width: number;
      }

      Configuration for the render target texture array

      • arrayLength: number

        Number of texture layers in the array

      • format: EnumIO

        Pixel format of the data

      • height: number

        Height of each texture layer in pixels

      • internalFormat: TextureParameterEnum

        Internal format of the texture

      • level: number

        Mipmap level (typically 0)

      • type: ComponentTypeEnum

        Component type of the data

      • width: number

        Width of each texture layer in pixels

    Returns number

    Handle to the created render target texture array resource

  • Creates a render target cube texture for environment mapping or omnidirectional shadow mapping. This creates a cube texture with 6 faces that can be rendered to.

    Parameters

    • params: {
          format: TextureParameterEnum;
          height: number;
          mipLevelCount: number;
          width: number;
      }

      Configuration for the render target cube texture

      • format: TextureParameterEnum

        Texture format for the render target

      • height: number

        Height of each cube face in pixels

      • mipLevelCount: number

        Number of mipmap levels to create

      • width: number

        Width of each cube face in pixels

    Returns number

    Handle to the created render target cube texture resource

  • Creates shader modules (vertex and fragment) from shader source code. This method compiles both vertex and fragment shaders and returns their handles.

    Parameters

    • params: {
          attributeNames: AttributeNames;
          attributeSemantics: VertexAttributeEnum[];
          fragmentShaderStr: string;
          material: Material;
          onError?: ((message: string) => void);
          primitive: Primitive;
          vertexShaderStr: string;
      }

      Configuration object for shader creation

      • attributeNames: AttributeNames

        Names of vertex attributes

      • attributeSemantics: VertexAttributeEnum[]

        Semantic meanings of vertex attributes

      • fragmentShaderStr: string

        WGSL fragment shader source code

      • material: Material

        The material that will use these shaders

      • OptionalonError?: ((message: string) => void)

        Optional error callback function

          • (message): void
          • Parameters

            • message: string

            Returns void

      • primitive: Primitive

        The primitive that will be rendered with these shaders

      • vertexShaderStr: string

        WGSL vertex shader source code

    Returns number

    Handle to the shader program containing both modules

  • Creates a storage buffer from a Float32Array and registers it as a WebGPU resource. Storage buffers are used for storing large amounts of data accessible from shaders.

    Parameters

    • inputArray: Float32Array

      The Float32Array containing the data to store

    Returns number

    Handle to the created storage buffer resource

  • Creates a WebGPU texture from a data URI string. This method loads the image from the data URI and creates a texture from it.

    Parameters

    • dataUri: string

      The data URI string containing the image data

    • params: {
          border: number;
          format: EnumIO;
          generateMipmap: boolean;
          internalFormat: TextureParameterEnum;
          level: number;
          type: ComponentTypeEnum;
      }

      Configuration object containing texture parameters

      • border: number

        Border width (must be 0 for WebGPU)

      • format: EnumIO

        Pixel format of the source data

      • generateMipmap: boolean

        Whether to generate mipmaps automatically

      • internalFormat: TextureParameterEnum

        Internal format of the texture

      • level: number

        Mipmap level (typically 0 for base level)

      • type: ComponentTypeEnum

        Component type of the source data

    Returns Promise<number>

    Promise that resolves to the texture resource handle

  • Creates a WebGPU texture from ImageBitmap data with specified parameters. This method handles texture creation, data upload, and optional mipmap generation.

    Parameters

    • imageData: ImageBitmapData

      The ImageBitmap data to create the texture from

    • params: {
          border: number;
          format: EnumIO;
          generateMipmap: boolean;
          height: number;
          internalFormat: TextureParameterEnum;
          level: number;
          type: ComponentTypeEnum;
          width: number;
      }

      Configuration object containing texture parameters

      • border: number

        Border width (must be 0 for WebGPU)

      • format: EnumIO

        Pixel format of the source data

      • generateMipmap: boolean

        Whether to generate mipmaps automatically

      • height: number

        Height of the texture in pixels

      • internalFormat: TextureParameterEnum

        Internal format of the texture

      • level: number

        Mipmap level (typically 0 for base level)

      • type: ComponentTypeEnum

        Component type of the source data

      • width: number

        Width of the texture in pixels

    Returns Promise<number>

    Promise that resolves to the texture resource handle

  • Creates a 2D texture view from a texture resource. This view can be used for sampling the texture in shaders.

    Parameters

    • textureHandle: number

      Handle to the source texture

    Returns number

    Handle to the created texture view

  • Parameters

    • textureHandle: number
    • arrayIdx: number
    • mipLevel: number

    Returns number

  • Creates a texture view suitable for use as a render target. This view targets only the base mip level and first array layer.

    Parameters

    • textureHandle: number

      Handle to the source texture

    Returns number

    Handle to the created render target texture view

  • Creates a cube texture view from a cube texture resource. This view exposes all 6 faces of the cube texture for sampling.

    Parameters

    • textureHandle: number

      Handle to the source cube texture

    Returns number

    Handle to the created cube texture view

  • Executes a draw call for rendering a primitive with the specified material and render pass. This is the core rendering method that sets up the render pipeline, bind groups, and executes the actual GPU draw commands.

    Parameters

    • primitive: Primitive

      The geometric primitive to render (vertices, indices, attributes)

    • material: Material

      The material containing shaders and rendering properties

    • renderPass: RenderPass

      The render pass defining render targets and clear operations

    • cameraId: number

      Identifier for the camera used for rendering

    • zWrite: boolean

      Whether to enable depth writing during rendering

    Returns void

  • Submits all recorded commands to the GPU queue and resets the command encoder. This method must be called to execute any recorded rendering commands.

    Returns void

  • Generates mipmaps for a texture using render passes (including CubeMap support). This is an optimized method adapted from WebGPU best practices that uses a custom shader to generate each mipmap level from the previous one.

    Parameters

    • texture: GPUTexture

      The GPU texture to generate mipmaps for

    • textureDescriptor: GPUTextureDescriptor

      Descriptor containing texture format and dimensions

    Returns void

  • Generates mipmaps for a 2D texture using the specified dimensions. This method creates all mipmap levels from the base texture.

    Parameters

    • textureHandle: number

      Handle to the texture resource

    • width: number

      Width of the base texture level

    • height: number

      Height of the base texture level

    Returns void

  • Generates mipmaps for a cube texture using the specified dimensions. This method creates all mipmap levels for all 6 faces of the cube texture.

    Parameters

    • textureHandle: number

      Handle to the cube texture resource

    • width: number

      Width of the base texture level

    • height: number

      Height of the base texture level

    Returns void

  • Reads pixel data from a texture and returns it as a Uint8Array. This method is useful for debugging or post-processing texture data.

    Parameters

    • textureHandle: number

      Handle to the texture resource

    • width: number

      Width of the texture region to read

    • height: number

      Height of the texture region to read

    • frameBufferUid: number

      Handle to the framebuffer (if applicable)

    • colorAttachmentIndex: number

      Index of the color attachment to read from

    Returns Promise<Uint8Array>

    Promise that resolves to the pixel data as Uint8Array

  • Recreates the system depth texture with the current canvas dimensions. This is called when the canvas is resized or initialized.

    Returns void

  • Configures vertex data layout for the rendering pipeline. This method sets up vertex buffer layouts including both per-vertex and per-instance data.

    Parameters

    • bufferHandles: {
          iboHandle?: number;
          vaoHandle: number;
          vboHandles: number[];
      }

      Object containing vertex array object, index buffer, and vertex buffer handles

      • OptionaliboHandle?: number

        Handle to the index buffer (optional)

      • vaoHandle: number

        Handle to the vertex array object

      • vboHandles: number[]

        Array of vertex buffer handles

    • primitive: Primitive

      The primitive containing vertex attribute information

    • instanceIDBufferUid: number = CGAPIResourceRepository.InvalidCGAPIResourceUid

      Handle to the instance ID buffer for instanced rendering

    Returns void

  • Updates the data in an existing index buffer with new data from an accessor. Automatically handles conversion of UnsignedByte indices to UnsignedShort if needed.

    Parameters

    • accessor: Accessor

      Accessor containing the new index data

    • resourceHandle: number

      Handle to the existing index buffer to update

    Returns void

    Error if the index buffer is not found

  • Parameters

    • storageBufferHandle: number
    • inputArray: Float32Array
    • updateComponentSize: number

    Returns void

  • Updates an existing storage buffer with new data. Only updates the specified number of components to optimize data transfer.

    Parameters

    • storageBufferHandle: number

      Handle to the storage buffer to update

    • inputArray: Float32Array

      New data to write to the buffer

    • updateComponentSize: number

      Number of components to update

    Returns void

  • Updates a portion of a storage buffer with new data at a specific offset. This allows for efficient partial updates of large storage buffers.

    Parameters

    • storageBufferHandle: number

      Handle to the storage buffer to update

    • inputArray: Float32Array

      New data to write to the buffer

    • offsetOfStorageBufferInByte: number

      Byte offset in the storage buffer where to start writing

    • offsetOfInputArrayInElement: number

      Element offset in the input array where to start reading

    • updateComponentSize: number

      Number of components to update

    Returns void

  • Parameters

    • identifier: string
    • materialSid: number
    • cameraSID: number
    • currentPrimitiveIdx: number
    • morphTargetNumber: number

    Returns void

  • Updates the data in an existing vertex buffer with new data from an accessor. This method maps the buffer for writing and uploads the new data.

    Parameters

    • accessor: Accessor

      Accessor containing the new vertex data

    • resourceHandle: number

      Handle to the existing vertex buffer to update

    Returns void

    Error if the vertex buffer is not found