|
| 1 | +import type { GraphicsCore } from "../core"; |
| 2 | +import type { ShaderManager } from "../shader/shader.manager"; |
| 3 | + |
| 4 | +export abstract class NfgComponent { |
| 5 | + private readonly _core: GraphicsCore; |
| 6 | + protected readonly _shaderManager: ShaderManager; |
| 7 | + private _vertices: Float32Array; |
| 8 | + protected _vertexBuffer: GPUBuffer; |
| 9 | + protected abstract _vertexLength: number; |
| 10 | + protected abstract readonly _vertexBufferLayout: GPUVertexBufferLayout; |
| 11 | + private _uniformBuffer: GPUBuffer; |
| 12 | + protected abstract _shader: GPUShaderModule; |
| 13 | + private _pipeline: GPURenderPipeline; |
| 14 | + private readonly _pipelineLayout: GPUPipelineLayout; |
| 15 | + private readonly _label: string; |
| 16 | + private _bindGroup: GPUBindGroup; |
| 17 | + |
| 18 | + constructor(core: GraphicsCore) { |
| 19 | + this._core = core; |
| 20 | + this._shaderManager = core.shaderManager; |
| 21 | + this._label = `${this.constructor.name} - ${Date.now()}`; |
| 22 | + |
| 23 | + const bindGroupLayout = this._core.device.createBindGroupLayout({ |
| 24 | + label: `${this._label} Bind Group Layout`, |
| 25 | + entries: [ |
| 26 | + { |
| 27 | + binding: 0, |
| 28 | + visibility: GPUShaderStage.VERTEX | GPUShaderStage.COMPUTE | GPUShaderStage.FRAGMENT, |
| 29 | + buffer: {}, |
| 30 | + }, |
| 31 | + ], |
| 32 | + }); |
| 33 | + |
| 34 | + this._pipelineLayout = this._core.device.createPipelineLayout({ |
| 35 | + label: `${this._label} Pipeline Layout`, |
| 36 | + bindGroupLayouts: [bindGroupLayout], |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + public async init(): Promise<NfgComponent> { |
| 41 | + await this._init(); |
| 42 | + this._updateUniforms(); |
| 43 | + this._updatePipeline(); |
| 44 | + return this; |
| 45 | + } |
| 46 | + |
| 47 | + public draw(pass: GPURenderPassEncoder): void { |
| 48 | + pass.setPipeline(this._pipeline); |
| 49 | + pass.setBindGroup(0, this._bindGroup); |
| 50 | + pass.setVertexBuffer(0, this._vertexBuffer); |
| 51 | + pass.draw(this._vertices.length / this._vertexLength); |
| 52 | + } |
| 53 | + |
| 54 | + protected abstract _init(): Promise<void>; |
| 55 | + |
| 56 | + protected _setVertices(raw: number[]): void { |
| 57 | + this._vertices = new Float32Array(raw); |
| 58 | + this._updateVertexBuffer(); |
| 59 | + } |
| 60 | + |
| 61 | + protected _updateVertexBuffer(): void { |
| 62 | + this._core.device.queue.writeBuffer(this._vertexBuffer, 0, this._vertices); |
| 63 | + } |
| 64 | + |
| 65 | + protected _updatePipeline(): void { |
| 66 | + this._pipeline = this._core.device.createRenderPipeline({ |
| 67 | + label: `${this._label} pipeline`, |
| 68 | + layout: this._pipelineLayout, |
| 69 | + vertex: { |
| 70 | + module: this._shader, |
| 71 | + entryPoint: "vertex_main", |
| 72 | + buffers: [this._vertexBufferLayout], |
| 73 | + }, |
| 74 | + fragment: { |
| 75 | + module: this._shader, |
| 76 | + entryPoint: "fragment_main", |
| 77 | + targets: [ |
| 78 | + { |
| 79 | + format: this._core.render.canvasFormat, |
| 80 | + }, |
| 81 | + ], |
| 82 | + }, |
| 83 | + }); |
| 84 | + this._updateBindGroup(); |
| 85 | + } |
| 86 | + |
| 87 | + protected _updateUniforms(): void { |
| 88 | + const uniformArray = new Float32Array([ |
| 89 | + 0, |
| 90 | + 0, |
| 91 | + 1, |
| 92 | + this._core.initContext.canvas.width, |
| 93 | + this._core.initContext.canvas.height, |
| 94 | + ]); |
| 95 | + this._uniformBuffer = this._core.device.createBuffer({ |
| 96 | + label: "View Uniforms", |
| 97 | + size: uniformArray.byteLength, |
| 98 | + usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST, // Une uniform est une valeur constante pour le gpu |
| 99 | + }); |
| 100 | + this._core.device.queue.writeBuffer(this._uniformBuffer, 0, uniformArray); |
| 101 | + } |
| 102 | + |
| 103 | + protected _updateBindGroup(): void { |
| 104 | + this._bindGroup = this._core.device.createBindGroup({ |
| 105 | + label: `${this._label} renderer bind group`, |
| 106 | + layout: this._pipeline.getBindGroupLayout(0), |
| 107 | + entries: [ |
| 108 | + { |
| 109 | + binding: 0, |
| 110 | + resource: { buffer: this._uniformBuffer }, |
| 111 | + }, |
| 112 | + ], |
| 113 | + }); |
| 114 | + } |
| 115 | +} |
0 commit comments