|
| 1 | +import * as renderer from '../renderer'; |
| 2 | +import * as shaders from '../shaders/shaders'; |
| 3 | +import { Stage } from '../stage/stage'; |
| 4 | +import { Node, Primitive, Scene, Texture } from "../stage/scene"; |
| 5 | + |
| 6 | +export class GlintRenderer extends renderer.Renderer { |
| 7 | + sceneUniformsBindGroupLayout: GPUBindGroupLayout; |
| 8 | + sceneUniformsBindGroup: GPUBindGroup; |
| 9 | + |
| 10 | + depthTexture: GPUTexture; |
| 11 | + depthTextureView: GPUTextureView; |
| 12 | + |
| 13 | + // We'll just re-use this for the glint render pass too. |
| 14 | + pipeline: GPURenderPipeline; |
| 15 | + |
| 16 | + // Assume no textures at all, very simple material |
| 17 | + // Figure out the rest of it later on. |
| 18 | + // glintPrimitives: Primitive[]; |
| 19 | + |
| 20 | + // Custom glint stage... idk |
| 21 | + glintScene: Scene = new Scene(); |
| 22 | + glintStage: Stage; |
| 23 | + |
| 24 | + glintRenderPipeline: GPURenderPipeline; |
| 25 | + |
| 26 | + constructor(stage: Stage) { |
| 27 | + super(stage); |
| 28 | + |
| 29 | + // Load scene stuff |
| 30 | + this.glintScene.loadGltf('./scenes/sphere/florbSphere.gltf'); |
| 31 | + this.glintStage = new Stage(this.glintScene, stage.lights, stage.camera, stage.stats); |
| 32 | + |
| 33 | + this.sceneUniformsBindGroupLayout = renderer.device.createBindGroupLayout({ |
| 34 | + label: "scene uniforms bind group layout", |
| 35 | + entries: [ |
| 36 | + { |
| 37 | + binding: 0, |
| 38 | + visibility: GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT, |
| 39 | + buffer: { type: "uniform"} |
| 40 | + }, |
| 41 | + { |
| 42 | + binding: 1, |
| 43 | + visibility: GPUShaderStage.FRAGMENT, |
| 44 | + buffer: { type: "read-only-storage" } |
| 45 | + } |
| 46 | + ] |
| 47 | + }); |
| 48 | + |
| 49 | + this.sceneUniformsBindGroup = renderer.device.createBindGroup({ |
| 50 | + label: "scene uniforms bind group", |
| 51 | + layout: this.sceneUniformsBindGroupLayout, |
| 52 | + entries: [ |
| 53 | + { |
| 54 | + binding: 0, |
| 55 | + resource: { buffer: this.camera.uniformsBuffer } |
| 56 | + }, |
| 57 | + { |
| 58 | + binding: 1, |
| 59 | + resource: { buffer: this.lights.lightSetStorageBuffer } |
| 60 | + } |
| 61 | + ] |
| 62 | + }); |
| 63 | + |
| 64 | + this.depthTexture = renderer.device.createTexture({ |
| 65 | + size: [renderer.canvas.width, renderer.canvas.height], |
| 66 | + format: "depth24plus", |
| 67 | + usage: GPUTextureUsage.RENDER_ATTACHMENT |
| 68 | + }); |
| 69 | + this.depthTextureView = this.depthTexture.createView(); |
| 70 | + |
| 71 | + this.pipeline = renderer.device.createRenderPipeline({ |
| 72 | + layout: renderer.device.createPipelineLayout({ |
| 73 | + label: "naive pipeline layout", |
| 74 | + bindGroupLayouts: [ |
| 75 | + this.sceneUniformsBindGroupLayout, |
| 76 | + renderer.modelBindGroupLayout, |
| 77 | + renderer.materialBindGroupLayout |
| 78 | + ] |
| 79 | + }), |
| 80 | + depthStencil: { |
| 81 | + depthWriteEnabled: true, |
| 82 | + depthCompare: "less", |
| 83 | + format: "depth24plus" |
| 84 | + }, |
| 85 | + vertex: { |
| 86 | + module: renderer.device.createShaderModule({ |
| 87 | + label: "naive vert shader", |
| 88 | + code: shaders.naiveVertSrc |
| 89 | + }), |
| 90 | + buffers: [ renderer.vertexBufferLayout ] |
| 91 | + }, |
| 92 | + fragment: { |
| 93 | + module: renderer.device.createShaderModule({ |
| 94 | + label: "naive frag shader", |
| 95 | + code: shaders.naiveFragSrc, |
| 96 | + }), |
| 97 | + targets: [ |
| 98 | + { |
| 99 | + format: renderer.canvasFormat, |
| 100 | + } |
| 101 | + ] |
| 102 | + } |
| 103 | + }); |
| 104 | + |
| 105 | + this.glintRenderPipeline = renderer.device.createRenderPipeline({ |
| 106 | + layout: renderer.device.createPipelineLayout({ |
| 107 | + label: "naive pipeline layout", |
| 108 | + bindGroupLayouts: [ |
| 109 | + this.sceneUniformsBindGroupLayout, |
| 110 | + renderer.modelBindGroupLayout, |
| 111 | + renderer.materialBindGroupLayout |
| 112 | + ] |
| 113 | + }), |
| 114 | + depthStencil: { |
| 115 | + depthWriteEnabled: true, |
| 116 | + depthCompare: "less", |
| 117 | + format: "depth24plus" |
| 118 | + }, |
| 119 | + vertex: { |
| 120 | + module: renderer.device.createShaderModule({ |
| 121 | + label: "naive vert shader", |
| 122 | + code: shaders.naiveVertSrc |
| 123 | + }), |
| 124 | + buffers: [ renderer.vertexBufferLayout ] |
| 125 | + }, |
| 126 | + fragment: { |
| 127 | + module: renderer.device.createShaderModule({ |
| 128 | + label: "glint frag shader", |
| 129 | + code: shaders.glintFragSrc, |
| 130 | + }), |
| 131 | + targets: [ |
| 132 | + { |
| 133 | + format: renderer.canvasFormat, |
| 134 | + } |
| 135 | + ] |
| 136 | + } |
| 137 | + }); |
| 138 | + } |
| 139 | + |
| 140 | + override draw() { |
| 141 | + const encoder = renderer.device.createCommandEncoder(); |
| 142 | + const canvasTextureView = renderer.context.getCurrentTexture().createView(); |
| 143 | + |
| 144 | + // Set up necessary bind groups for the debug glint model draw pass |
| 145 | + const glintRenderPass = encoder.beginRenderPass({ |
| 146 | + label: "naive render pass", |
| 147 | + colorAttachments: [ |
| 148 | + { |
| 149 | + view: canvasTextureView, |
| 150 | + clearValue: [0, 0, 0, 0], |
| 151 | + loadOp: "clear", |
| 152 | + storeOp: "store" |
| 153 | + } |
| 154 | + ], |
| 155 | + depthStencilAttachment: { |
| 156 | + view: this.depthTextureView, |
| 157 | + depthClearValue: 1.0, |
| 158 | + depthLoadOp: "clear", |
| 159 | + depthStoreOp: "store" |
| 160 | + } |
| 161 | + }); |
| 162 | + |
| 163 | + glintRenderPass.setPipeline(this.glintRenderPipeline); |
| 164 | + glintRenderPass.setBindGroup(shaders.constants.bindGroup_scene, this.sceneUniformsBindGroup); |
| 165 | + |
| 166 | + this.glintScene.iterate(node => { |
| 167 | + glintRenderPass.setBindGroup(shaders.constants.bindGroup_model, node.modelBindGroup); |
| 168 | + }, material => { |
| 169 | + glintRenderPass.setBindGroup(shaders.constants.bindGroup_material, material.materialBindGroup); |
| 170 | + }, primitive => { |
| 171 | + glintRenderPass.setVertexBuffer(0, primitive.vertexBuffer); |
| 172 | + glintRenderPass.setIndexBuffer(primitive.indexBuffer, 'uint32'); |
| 173 | + glintRenderPass.drawIndexed(primitive.numIndices); |
| 174 | + }); |
| 175 | + |
| 176 | + glintRenderPass.end(); |
| 177 | + |
| 178 | + renderer.device.queue.submit([encoder.finish()]); |
| 179 | + } |
| 180 | +} |
0 commit comments