rhodonite
    Preparing search index...

    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 Summary)

    Implements

    Index

    Properties

    Methods

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

    Properties

    InvalidCGAPIResourceUid: -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

      • engine: Engine
      • 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, number, number]>

      the WebGLResourceHandle for the generated Cube Texture

    • Parameters

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

      Returns number

    • 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;
            height: number;
            internalFormat: TextureParameterEnum;
            width: number;
        }

        Configuration for the render target texture array

        • arrayLength: number

          Number of texture layers in the array

        • height: number

          Height of each texture layer in pixels

        • internalFormat: TextureParameterEnum

          Internal format of the texture

        • 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: {
            config: Config;
            engine: Engine;
            fragmentShaderStr: string;
            material: Material;
            vertexShaderStr: string;
        }

        Configuration object for shader creation

        • config: Config
        • engine: Engine

          The engine instance for logging

        • fragmentShaderStr: string

          WGSL fragment shader source code

        • material: Material

          The material that will use 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

      • gpuInstanceDataBuffers: Buffer[]

        The buffers 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: { generateMipmap: boolean; internalFormat: TextureParameterEnum }

        Configuration object containing texture parameters

        • generateMipmap: boolean

          Whether to generate mipmaps automatically

        • internalFormat: TextureParameterEnum

          Internal format of the texture

      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: {
            generateMipmap: boolean;
            height: number;
            internalFormat: TextureParameterEnum;
            width: number;
        }

        Configuration object containing texture parameters

        • generateMipmap: boolean

          Whether to generate mipmaps automatically

        • height: number

          Height of the texture in pixels

        • internalFormat: TextureParameterEnum

          Internal format of the texture

        • 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
      • arrayLayerCount: number

      Returns number

    • 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

    • Deletes a vertex buffer and removes it from the resource registry. This destroys the GPU buffer and frees its memory.

      Parameters

      • resourceHandle: number

        Handle to the vertex buffer to delete

      Returns void

      Error if the vertex buffer is not found

    • 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

      • engine: Engine
      • 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

      • displayIdx: number

      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 specific face of a cube texture. Uses a shader-based approach to sample the cubemap and render to a 2D texture, then copies the result to a buffer for CPU access.

      Parameters

      • textureHandle: number

        Handle to the cube texture

      • width: number

        Width of the face texture

      • height: number

        Height of the face texture

      • faceIndex: number

        Index of the cube face (0=+X, 1=-X, 2=+Y, 3=-Y, 4=+Z, 5=-Z)

      Returns Promise<Uint8Array<ArrayBufferLike>>

      Promise resolving to the pixel data as a Uint8Array (RGBA format)

    • Reads pixel data directly from a 2D texture (synchronous version). Note: WebGPU does not support synchronous texture readback. This method throws an error - use getPixelDataFromTextureAsync instead.

      Parameters

      • _textureHandle: number
      • _x: number
      • _y: number
      • _width: number
      • _height: number

      Returns Uint8Array

    • Reads pixel data directly from a 2D texture asynchronously. This method uses a render-based approach to avoid requiring COPY_SRC usage on the source texture. It renders the texture to a temporary render target with COPY_SRC, then reads from that.

      Parameters

      • textureHandle: number

        Handle to the texture to read from

      • _x: number
      • _y: number
      • width: number

        Width of the region to read

      • height: number

        Height of the region to read

      Returns Promise<Uint8Array<ArrayBufferLike>>

      Promise resolving to the pixel data as a Uint8Array in RGBA format

    • 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
      • _colorAttachmentIndex: number

      Returns Promise<Uint8Array<ArrayBufferLike>>

      Promise that resolves to the pixel data as Uint8Array

    • Load an image to a specific mip level of a texture

      Parameters

      • mipLevel: {
            data: TypedArray;
            format: TextureFormatEnum;
            height: number;
            mipLevel: number;
            rowSizeByPixel: number;
            textureUid: number;
            type: ComponentTypeEnum;
            width: number;
            xOffset: number;
            yOffset: number;
        }

        the mip level to load the image to

      Returns Promise<void>

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

      Returns void

    • Resizes the canvas and recreates the system depth texture. This method should be called when the window or viewport size changes.

      Parameters

      • width: number

        New canvas width in pixels

      • height: number

        New canvas height in pixels

      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: { vboHandles: number[] }

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

        • vboHandles: number[]

          Array of vertex buffer handles

      • primitive: Primitive

        The primitive containing vertex attribute information

      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

      • gpuInstanceDataBuffers: Buffer[]

        New data to write to the buffer

      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

    • Parameters

      • inputArray: Uint32Array
      • elementNum: number

      Returns void

    • Parameters

      • inputArray: Float32Array
      • elementNum: 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