@animech-public/playcanvas
Version:
PlayCanvas WebGL game engine
2 lines (1 loc) • 2.31 kB
JavaScript
import{Shader as t}from"../shader.js";import{SHADERLANGUAGE_WGSL as e}from"../constants.js";class n{constructor(n){this.device=void 0,this.pipelineCache=new Map,this.device=n;const r="\n \n\t\t\t\t\t\tvar<private> pos : array<vec2f, 4> = array<vec2f, 4>(\n\t\t\t\t\t\t\t\tvec2(-1.0, 1.0), vec2(1.0, 1.0), vec2(-1.0, -1.0), vec2(1.0, -1.0)\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\tstruct VertexOutput {\n\t\t\t\t\t\t\t\t@builtin(position) position : vec4f,\n\t\t\t\t\t\t};\n\n\t\t\t\t\t\t@vertex\n\t\t\t\t\t\tfn vertexMain(@builtin(vertex_index) vertexIndex : u32) -> VertexOutput {\n\t\t\t\t\t\t\tvar output : VertexOutput;\n\t\t\t\t\t\t\toutput.position = vec4f(pos[vertexIndex], 0, 1);\n\t\t\t\t\t\t\treturn output;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\t@group(0) @binding(0) var img : texture_depth_multisampled_2d;\n\n\t\t\t\t\t\t@fragment\n\t\t\t\t\t\tfn fragmentMain(@builtin(position) fragColor: vec4f) -> @location(0) vec4f {\n\t\t\t\t\t\t\t\t// load th depth value from sample index 0\n\t\t\t\t\t\t\t\tvar depth = textureLoad(img, vec2i(fragColor.xy), 0u);\n\t\t\t\t\t\t\t\treturn vec4<f32>(depth, 0.0, 0.0, 0.0);\n\t\t\t\t\t\t}\n\t\t\t\t";this.shader=new t(n,{name:"WebGPUResolverDepthShader",shaderLanguage:e,vshader:r,fshader:r})}destroy(){this.shader.destroy(),this.shader=null,this.pipelineCache=null}getPipeline(t){let e=this.pipelineCache.get(t);return e||(e=this.createPipeline(t),this.pipelineCache.set(t,e)),e}createPipeline(t){const e=this.shader.impl;return this.device.wgpu.createRenderPipeline({layout:"auto",vertex:{module:e.getVertexShaderModule(),entryPoint:e.vertexEntryPoint},fragment:{module:e.getFragmentShaderModule(),entryPoint:e.fragmentEntryPoint,targets:[{format:t}]},primitive:{topology:"triangle-strip"}})}resolveDepth(t,e,n){const r=this.device,i=r.wgpu,a=this.getPipeline(n.format),o=e.depthOrArrayLayers;for(let r=0;r<o;r++){const o=e.createView({dimension:"2d",aspect:"depth-only",baseMipLevel:0,mipLevelCount:1,baseArrayLayer:r}),s=n.createView({dimension:"2d",baseMipLevel:0,mipLevelCount:1,baseArrayLayer:r}),p=t.beginRenderPass({colorAttachments:[{view:s,loadOp:"clear",storeOp:"store"}]}),d=i.createBindGroup({layout:a.getBindGroupLayout(0),entries:[{binding:0,resource:o}]});p.setPipeline(a),p.setBindGroup(0,d),p.draw(4),p.end()}r.pipeline=null}}export{n as WebgpuResolver};