import { IClearOptions , IConfigService , IDENTIFIER , IRendererService , IShaderModuleService , ISystem , IView , } from '@antv/g-webgpu-core' ; import * as WebGPUConstants from '@webgpu/types/dist/constants' ; import { Container , inject , injectable , multiInject } from 'inversify' ; import mapFragDeclaration from './material/shaders/map.frag.declaration.glsl' ; import mapFragMain from './material/shaders/map.frag.main.glsl' ; import uvFragDeclaration from './material/shaders/uv.frag.declaration.glsl' ; import uvVertDeclaration from './material/shaders/uv.vert.declaration.glsl' ; import uvVertMain from './material/shaders/uv.vert.main.glsl' ; @ injectable ( ) export class Renderer { public container : Container ; @ inject ( IDENTIFIER . RenderEngine ) private readonly engine : IRendererService ; @ inject ( IDENTIFIER . ShaderModuleService ) private readonly shaderModule : IShaderModuleService ; @ inject ( IDENTIFIER . ConfigService ) private readonly configService : IConfigService ; private inited : boolean = false ; private rendering : boolean = false ; private pendings : Array < ( ) => void > = [ ] ; private views : IView [ ] = [ ] ; private size : { width : number ; height : number ; } ; public async init ( ) { this . shaderModule . registerBuiltinModules ( ) ; this . shaderModule . registerModule ( 'uv.vert.declaration' , { vs : uvVertDeclaration , } ) ; this . shaderModule . registerModule ( 'uv.vert.main' , { vs : uvVertMain , } ) ; this . shaderModule . registerModule ( 'uv.frag.declaration' , { fs : uvFragDeclaration , } ) ; this . shaderModule . registerModule ( 'map.frag.declaration' , { fs : mapFragDeclaration , } ) ; this . shaderModule . registerModule ( 'map.frag.main' , { fs : mapFragMain , } ) ; const systems = this . container . getAll < ISystem > ( IDENTIFIER . Systems ) ; const config = this . configService . get ( ) ; if ( config . canvas ) { await this . engine . init ( { canvas : config . canvas , swapChainFormat : WebGPUConstants . TextureFormat . BGRA8Unorm , antialiasing : false , } ) ; for ( const system of systems ) { if ( system . initialize ) { await system . initialize ( ) ; } } this . inited = true ; } } public async render ( ... views ) { if ( ! this . inited || this . rendering ) { return ; } if ( this . pendings . length ) { this . pendings . forEach ( ( pending ) => { pending ( ) ; } ) ; } this . rendering = true ; this . engine . beginFrame ( ) ; const systems = this . container . getAll < ISystem > ( IDENTIFIER . Systems ) ; for ( const system of systems ) { if ( system . execute ) { await system . execute ( views ) ; } } this . engine . endFrame ( ) ; this . rendering = false ; } public clear ( options ) { if ( this . inited ) { this . engine . clear ( options ) ; } else { this . pendings . unshift ( ( ) => { this . engine . clear ( options ) ; this . pendings . shift ( ) ; } ) ; } return this ; } public setSize ( { width , height } ) { const canvas = this . engine . getCanvas ( ) ; this . size = { width , height } ; canvas . width = width ; canvas . height = height ; return this ; } public getSize ( ) { return this . size ; } }