Skip to content

Commit

Permalink
提升瓦片图层性能 (#2258)
Browse files Browse the repository at this point in the history
* fix: add tile vector to ci

* fix: add program cache for device model

* chore: update tile vector snapshot
  • Loading branch information
xiaoiver authored Jan 17, 2024
1 parent f920530 commit 15d39a2
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 15 deletions.
Binary file modified __tests__/e2e/snapshots/Tile_vector.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 7 additions & 7 deletions examples/demos/tile/vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ export function MapRender(option: {
scene.on('loaded', () => {
scene.addLayer(layer);
// scene.startAnimate();
// scene.addLayer(boundaries);
// scene.addLayer(natural);
// scene.addLayer(buildings);
// scene.addLayer(transit);
// scene.addLayer(roads);
// scene.addLayer(water);
// scene.addLayer(point);
scene.addLayer(boundaries);
scene.addLayer(natural);
scene.addLayer(buildings);
scene.addLayer(transit);
scene.addLayer(roads);
scene.addLayer(water);
scene.addLayer(point);

if (window['screenshot']) {
window['screenshot']();
Expand Down
2 changes: 1 addition & 1 deletion examples/demos/webgpu/perf-animate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function MapRender(option: {
});

// 2400
flydata = new Array(500).fill(flydata).flat();
flydata = new Array(50).fill(flydata).flat();

const worldLine = new LineLayer()
.source(world)
Expand Down
2 changes: 1 addition & 1 deletion packages/renderer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"tsc": "tsc --project tsconfig.build.json"
},
"dependencies": {
"@antv/g-device-api": "^1.5.0",
"@antv/g-device-api": "^1.6.1",
"@antv/l7-core": "2.20.14",
"@antv/l7-utils": "2.20.14",
"@babel/runtime": "^7.7.7",
Expand Down
43 changes: 41 additions & 2 deletions packages/renderer/src/device/DeviceCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import type {
InputLayout,
InputLayoutDescriptor,
MegaStateDescriptor,
Program,
ProgramDescriptor,
RenderPipeline,
RenderPipelineDescriptor,
} from '@antv/g-device-api';
Expand Down Expand Up @@ -125,6 +127,28 @@ function bindingsDescriptorHash(a: BindingsDescriptor): number {
return hashCodeNumberFinish(hash);
}

function programDescriptorEquals(
a: ProgramDescriptor,
b: ProgramDescriptor,
): boolean {
return (
a.vertex?.glsl === b.vertex?.glsl && a.fragment?.glsl === b.fragment?.glsl
);
}

function programDescriptorCopy(
a: Readonly<ProgramDescriptor>,
): ProgramDescriptor {
return {
vertex: {
glsl: a.vertex?.glsl,
},
fragment: {
glsl: a.fragment?.glsl,
},
};
}

export class RenderCache {
constructor(private device: Device) {}

Expand All @@ -143,6 +167,11 @@ export class RenderCache {
nullHashFunc,
);

private programCache = new HashMap<ProgramDescriptor, Program>(
programDescriptorEquals,
nullHashFunc,
);

createBindings(descriptor: BindingsDescriptor): Bindings {
let bindings = this.bindingsCache.get(descriptor);
if (bindings === null) {
Expand Down Expand Up @@ -184,18 +213,28 @@ export class RenderCache {
return inputLayout;
}

createProgram(descriptor: ProgramDescriptor): Program {
let program = this.programCache.get(descriptor);
if (program === null) {
const descriptorCopy = programDescriptorCopy(descriptor);
program = this.device.createProgram(descriptor);
this.programCache.add(descriptorCopy, program);
}
return program;
}

destroy(): void {
for (const bindings of this.bindingsCache.values()) bindings.destroy();
for (const renderPipeline of this.renderPipelinesCache.values())
renderPipeline.destroy();
for (const inputLayout of this.inputLayoutsCache.values())
inputLayout.destroy();
// for (const program of this.programCache.values()) program.destroy();
for (const program of this.programCache.values()) program.destroy();
// for (const sampler of this.samplerCache.values()) sampler.destroy();
this.bindingsCache.clear();
this.renderPipelinesCache.clear();
this.inputLayoutsCache.clear();
// this.programCache.clear();
this.programCache.clear();
// this.samplerCache.clear();
}
}
6 changes: 2 additions & 4 deletions packages/renderer/src/device/DeviceModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default class DeviceModel implements IModel {
? 'diagnostic(off,derivative_uniformity);'
: '';

const program = device.createProgram({
this.program = service.renderCache.createProgram({
vertex: {
glsl: vs,
},
Expand All @@ -90,7 +90,6 @@ export default class DeviceModel implements IModel {
postprocess: (fs) => diagnosticDerivativeUniformityHeader + fs,
},
});
this.program = program;

if (uniforms) {
this.uniforms = this.extractUniforms(uniforms);
Expand Down Expand Up @@ -141,11 +140,10 @@ export default class DeviceModel implements IModel {
this.indexBuffer = (elements as DeviceElements).get();
}

// const inputLayout = device.createInputLayout({
const inputLayout = service.renderCache.createInputLayout({
vertexBufferDescriptors,
indexBufferFormat: elements ? Format.U32_R : null,
program,
program: this.program,
});
this.inputLayout = inputLayout;

Expand Down
3 changes: 3 additions & 0 deletions packages/renderer/src/device/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ export default class DeviceRendererService implements IRendererService {
}

beginFrame(): void {
this.device.beginFrame();

const { currentFramebuffer, swapChain, mainColorRT, mainDepthRT } = this;

const colorAttachment = currentFramebuffer
Expand Down Expand Up @@ -197,6 +199,7 @@ export default class DeviceRendererService implements IRendererService {

endFrame(): void {
this.device.submitPass(this.renderPass);
this.device.endFrame();
}

getPointSizeRange() {
Expand Down

0 comments on commit 15d39a2

Please sign in to comment.