/
githubmirror
/
pixijs
Обзор
Документация
Войти
/
githubmirror
/
pixijs
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
dev
src/scene/graphics/gpu/GpuGraphicsAdaptor.ts
146 строк
5 KB
Zyie
feat: WGSL shader overrides, depth-stencil formats, partial buffer updates & view caching (#12089)
08 июл 2026, 13:21
Не верифицирован
08 июл 2026, 13:21
ad76fa5
Код
Авторство
О чём код?
import { ExtensionType } from '../../../extensions/Extensions'; import { Matrix } from '../../../maths/matrix/Matrix'; import { getTextureBatchBindGroup } from '../../../rendering/batcher/gpu/getTextureBatchBindGroup'; import { compileHighShaderGpuProgram } from '../../../rendering/high-shader/compileHighShaderToProgram'; import { colorBit } from '../../../rendering/high-shader/shader-bits/colorBit'; import { generateTextureBatchBit } from '../../../rendering/high-shader/shader-bits/generateTextureBatchBit'; import { localUniformBitGroup2 } from '../../../rendering/high-shader/shader-bits/localUniformBit'; import { roundPixelsBit } from '../../../rendering/high-shader/shader-bits/roundPixelsBit'; import { Shader } from '../../../rendering/renderers/shared/shader/Shader'; import { UniformGroup } from '../../../rendering/renderers/shared/shader/UniformGroup'; import { type Renderer } from '../../../rendering/renderers/types'; import type { Batch } from '../../../rendering/batcher/shared/Batcher'; import type { GpuEncoderSystem } from '../../../rendering/renderers/gpu/GpuEncoderSystem'; import type { WebGPURenderer } from '../../../rendering/renderers/gpu/WebGPURenderer'; import type { Topology } from '../../../rendering/renderers/shared/geometry/const'; import type { Graphics } from '../shared/Graphics'; import type { GraphicsContextSystem } from '../shared/GraphicsContextSystem'; import type { GraphicsAdaptor, GraphicsPipeLike } from '../shared/GraphicsPipe'; /** * A GraphicsAdaptor that uses the GPU to render graphics. * @category rendering * @ignore */ export class GpuGraphicsAdaptor implements GraphicsAdaptor { /** @ignore */ public static extension = { type: [ ExtensionType.WebGPUPipesAdaptor, ], name: 'graphics', } as const; public shader: Shader; private _maxTextures = 0; public contextChange(renderer: Renderer): void { const localUniforms = new UniformGroup({ uTransformMatrix: { value: new Matrix(), type: 'mat3x3<f32>' }, uColor: { value: new Float32Array([1, 1, 1, 1]), type: 'vec4<f32>' }, uRound: { value: 0, type: 'f32' }, }); this._maxTextures = renderer.limits.maxBatchableTextures; const gpuProgram = compileHighShaderGpuProgram({ name: 'graphics', bits: [ colorBit, generateTextureBatchBit(this._maxTextures), localUniformBitGroup2, roundPixelsBit ] }); this.shader = new Shader({ gpuProgram, resources: { // added on the fly! localUniforms, }, }); } public execute(graphicsPipe: GraphicsPipeLike, renderable: Graphics): void { const context = renderable.context; const shader = context.customShader || this.shader; const renderer = graphicsPipe.renderer as WebGPURenderer; const contextSystem = renderer.graphicsContext as GraphicsContextSystem; const { batcher, instructions } = contextSystem.getContextRenderData(context); // WebGPU specific... // TODO perf test this a bit... const encoder = renderer.encoder as GpuEncoderSystem; encoder.setGeometry(batcher.geometry, shader.gpuProgram); const globalUniformsBindGroup = renderer.globalUniforms.bindGroup; encoder.setBindGroup(0, globalUniformsBindGroup, shader.gpuProgram); const localBindGroup = (renderer as WebGPURenderer) .renderPipes.uniformBatch.getUniformBindGroup(shader.resources.localUniforms, true); encoder.setBindGroup(2, localBindGroup, shader.gpuProgram); const batches = instructions.instructions as Batch[]; let topology: Topology = null; for (let i = 0; i < instructions.instructionSize; i++) { const batch = batches[i]; if (batch.topology !== topology) { topology = batch.topology; encoder.setPipelineFromGeometryProgramAndState( batcher.geometry, shader.gpuProgram, graphicsPipe.state, batch.topology, shader._overrides ); } shader.groups[1] = batch.bindGroup; if (!batch.gpuBindGroup) { const textureBatch = batch.textures; batch.bindGroup = getTextureBatchBindGroup( textureBatch.textures, textureBatch.count, this._maxTextures ); batch.gpuBindGroup = renderer.bindGroup.getBindGroup( batch.bindGroup, shader.gpuProgram, 1 ); } encoder.setBindGroup(1, batch.bindGroup, shader.gpuProgram); encoder.renderPassEncoder.drawIndexed(batch.size, 1, batch.start); } } public destroy(): void { this.shader.destroy(true); this.shader = null; } }