{"id":278,"date":"2022-03-30T23:58:00","date_gmt":"2022-03-30T15:58:00","guid":{"rendered":"https:\/\/blog.indeex.club\/?p=278"},"modified":"2022-03-30T21:07:57","modified_gmt":"2022-03-30T13:07:57","slug":"webgpu%e5%9f%ba%e7%a1%80%ef%bc%88%e5%9b%9b%ef%bc%89-%e5%9c%b0%e5%bd%a2","status":"publish","type":"post","link":"https:\/\/blog.indeex.club\/index.php\/2022\/03\/30\/webgpu%e5%9f%ba%e7%a1%80%ef%bc%88%e5%9b%9b%ef%bc%89-%e5%9c%b0%e5%bd%a2\/","title":{"rendered":"WebGPU\u57fa\u7840\uff08\u56db\uff09- \u5730\u5f62"},"content":{"rendered":"<p>\u5730\u5f62\u5176\u5b9e\u5c31\u662f\u5e26\u9ad8\u5ea6\u7684\u7eb9\u7406\uff0c\u53c8\u79f0\u9ad8\u7a0b\u7eb9\u7406\u6216\u9ad8\u5ea6\u8d34\u56fe\u3002<\/p>\n<h2>engine<\/h2>\n<p>\u4fee\u6539\u4e4b\u524d\u7684engine\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-typescript\">\/\/...\n\nimport { setDevice, device, setCameraUniformBuffer, cameraUniformBuffer } from &#039;.\/common&#039;;\n\n\/\/...\n\nlet _cameraUniformBuffer = device.createBuffer({\n    size: this.matrixSize,\n    usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,\n});\n\nsetCameraUniformBuffer(_cameraUniformBuffer);\n\n\/\/...<\/code><\/pre>\n<p>\u5728common\u4e2d\u589e\u52a0<strong>cameraUniformBuffer<\/strong>\uff1a<\/p>\n<pre><code class=\"language-typescript\">\/\/...\n\nexport var cameraUniformBuffer: GPUBuffer;\nexport function setCameraUniformBuffer(_cameraUniformBuffer: GPUBuffer) {\n    cameraUniformBuffer = _cameraUniformBuffer;\n}\n\n\/\/...<\/code><\/pre>\n<h2>\u5e73\u9762<\/h2>\n<p>\u4fee\u6539\u4e4b\u524d\u521b\u5efa\u7269\u4f53\u7684\u7c7b\uff0c\u9996\u5148\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u7528\u6765\u751f\u6210\u5730\u5f62\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-typescript\">\n\/\/...\n\nexport function generatePlane(numSegX: number, numSegY: number, width: number, height: number): Mesh {\n    const result = new Mesh();\n\n    const normal = [0, 0, 1]\n\n    const xstep = width \/ numSegX;\n    const ystep = height \/ numSegY;\n\n    const widthHalf = width \/ 2;\n    const heightHalf = width \/ 2;\n\n    for (let x = - widthHalf; x &lt; widthHalf; x += xstep) {\n        for (let y = - heightHalf; y &lt; heightHalf; y += ystep) {\n\n            const x0 = x;\n            const y0 = y;\n            const x1 = x0 + xstep;\n            const y1 = y0 + ystep;\n\n            result.vertices.push({\n                pos: [x0, y0, 0],\n                norm: normal,\n                uv: [1 - (x0 + widthHalf) \/ width, (y0 + heightHalf) \/ height],\n            });\n\n            result.vertices.push({\n                pos: [x1, y0, 0],\n                norm: normal,\n                uv: [1 - (x1 + widthHalf) \/ width, (y0 + heightHalf) \/ height],\n            });\n\n            result.vertices.push({\n                pos: [x0, y1, 0],\n                norm: normal,\n                uv: [1 - (x0 + widthHalf) \/ width, (y1 + heightHalf) \/ height],\n            });\n\n            result.vertices.push({\n                pos: [x0, y1, 0],\n                norm: normal,\n                uv: [1 - (x0 + widthHalf) \/ width, (y1 + heightHalf) \/ height],\n            });\n\n            result.vertices.push({\n                pos: [x1, y0, 0],\n                norm: normal,\n                uv: [1 - (x1 + widthHalf) \/ width, (y0 + heightHalf) \/ height],\n            });\n\n            result.vertices.push({\n                pos: [x1, y1, 0],\n                norm: normal,\n                uv: [1 - (x1 + widthHalf) \/ width, (y1 + heightHalf) \/ height],\n            });\n        }\n    }\n\n    return result;\n}<\/code><\/pre>\n<h2>\u5730\u5f62<\/h2>\n<p>\u7136\u540e\u9700\u8981\u5c06\u5730\u5f62\u653e\u5728\u5e73\u9762\u4e0a\uff0c\u4f9d\u65e7\u9700\u8981\u7ba1\u7ebf\u548c\u961f\u5217\uff1a<\/p>\n<pre><code class=\"language-typescript\">constructor(parameter: Parameter3D, heightBitmap: ImageBitmap) {\n    this.setTransformation(parameter);\n    this.mesh = generatePlane(this.numSegX, this.numSegY, this.width, this.height);\n\n    this.renderPipeline = device.createRenderPipeline({\n        vertex: {\n            module: device.createShaderModule({ code: vertexShadowCode, }),\n            entryPoint: &#039;main&#039;,\n            buffers: [\n                {\n                    arrayStride: this.stride,\n                    attributes: [\n                        {\n                            shaderLocation: 0,\n                            offset: 0,\n                            format: &#039;float32x3&#039;,\n                        },\n                        {\n                            shaderLocation: 1,\n                            offset: 3 * 4,\n                            format: &#039;float32x3&#039;,\n                        },\n                        {\n                            shaderLocation: 2,\n                            offset: (3 + 3) * 4,\n                            format: &#039;float32x2&#039;,\n                        },\n                    ],\n                } as GPUVertexBufferLayout,\n            ],\n        },\n        fragment: {\n            module: device.createShaderModule({ code: fragShaderCode, }),\n            entryPoint: &#039;main&#039;,\n            targets: [\n                {\n                    format: &#039;bgra8unorm&#039; as GPUTextureFormat,\n                },\n            ],\n        },\n        primitive: {\n            topology: &#039;triangle-list&#039;,\n            cullMode: &#039;back&#039;,\n        },\n\n        depthStencil: {\n            depthWriteEnabled: true,\n            depthCompare: &#039;less&#039;,\n            format: &#039;depth24plus-stencil8&#039;,\n        },\n    });\n\n    this.verticesBuffer = device.createBuffer({\n        size: this.mesh.vertices.length * this.stride,\n        usage: GPUBufferUsage.VERTEX,\n        mappedAtCreation: true,\n    });\n\n    const mapping = new Float32Array(this.verticesBuffer.getMappedRange());\n    for (let i = 0; i &lt; this.mesh.vertices.length; i++) {\n        \/\/ (3 * 4) + (3 * 4) + (2 * 4)\n        mapping.set([(this.mesh as any).vertices[i].pos[0],\n        (this.mesh as any).vertices[i].pos[1],\n        (this.mesh as any).vertices[i].pos[2]], this.perVertex * i + 0);\n        mapping.set((this.mesh as any).vertices[i].norm, this.perVertex * i + 3);\n        mapping.set((this.mesh as any).vertices[i].uv, this.perVertex * i + 6);\n    }\n    this.verticesBuffer.unmap();\n\n    this.transformationBuffer = device.createBuffer({\n        size: this.uniformBufferSize,\n        usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,\n    });\n\n    const entries = [\n        {\n            binding: 0,\n            resource: {\n                buffer: this.transformationBuffer,\n                offset: 0,\n                size: this.matrixSize * 2,\n            },\n        },\n        {\n            binding: 1,\n            resource: {\n                buffer: cameraUniformBuffer,\n                offset: 0,\n                size: this.matrixSize,\n            },\n        },\n\n    ];\n\n    let height = device.createTexture({\n        size: [heightBitmap.width, heightBitmap.height, 1],\n        format: &#039;rgba8unorm&#039;,\n        usage: GPUTextureUsage.TEXTURE_BINDING |\n            GPUTextureUsage.COPY_DST |\n            GPUTextureUsage.RENDER_ATTACHMENT,\n    });\n    device.queue.copyExternalImageToTexture(\n        { source: heightBitmap },\n        { texture: height },\n        [heightBitmap.width, heightBitmap.height, 1]\n    );\n\n    entries.push({\n        binding: 2,\n        resource: height.createView(),\n    } as any);\n\n    this.transformationBindGroup = device.createBindGroup({\n        layout: this.renderPipeline.getBindGroupLayout(0),\n        entries: entries as Iterable&lt;GPUBindGroupEntry&gt;,\n    });\n}<\/code><\/pre>\n<h2>\u7ed8\u5236<\/h2>\n<p>\u7ed8\u5236\u7684\u65f6\u5019\u4f9d\u7136\u9700\u8981\u4ece<em>common<\/em>\u4e2d\u83b7\u53d6<em>device<\/em>\uff1a<\/p>\n<pre><code class=\"language-typescript\">public draw(passEncoder: GPURenderPassEncoder, device: GPUDevice) {\n    this.updateTransformationMatrix()\n\n    passEncoder.setPipeline(this.renderPipeline);\n    device.queue.writeBuffer(\n        this.transformationBuffer,\n        0,\n        this.transformMatrix.buffer,\n        this.transformMatrix.byteOffset,\n        this.transformMatrix.byteLength\n    );\n    device.queue.writeBuffer(\n        this.transformationBuffer,\n        64,\n        this.rotateMatrix.buffer,\n        this.rotateMatrix.byteOffset,\n        this.rotateMatrix.byteLength\n    );\n    passEncoder.setVertexBuffer(0, this.verticesBuffer);\n    passEncoder.setBindGroup(0, this.transformationBindGroup);\n    passEncoder.draw(this.mesh.vertices.length, 1, 0, 0);\n}<\/code><\/pre>\n<p>\u63a5\u7740\u662f\u66f4\u65b0\u548c\u8bbe\u7f6e\u77e9\u9635\uff1a<\/p>\n<pre><code class=\"language-typescript\">private updateTransformationMatrix() {\n    const transform = mat4.create();\n    const rotate = mat4.create();\n\n    mat4.translate(transform, transform, vec3.fromValues(this.x, this.y, this.z))\n    mat4.rotateX(transform, transform, this.rotX);\n    mat4.rotateY(transform, transform, this.rotY);\n    mat4.rotateZ(transform, transform, this.rotZ);\n\n    mat4.rotateX(rotate, rotate, this.rotX);\n    mat4.rotateY(rotate, rotate, this.rotY);\n    mat4.rotateZ(rotate, rotate, this.rotZ);\n\n    mat4.copy(this.transformMatrix, transform)\n    mat4.copy(this.rotateMatrix, rotate)\n}\n\nprivate setTransformation(parameter?: Parameter3D) {\n    if (parameter == null) {\n        return;\n    }\n\n    this.x = parameter.x ? parameter.x : 0;\n    this.y = parameter.y ? parameter.y : 0;\n    this.z = parameter.z ? parameter.z : 0;\n\n    this.rotX = parameter.rotX ? parameter.rotX : 0;\n    this.rotY = parameter.rotY ? parameter.rotY : 0;\n    this.rotZ = parameter.rotZ ? parameter.rotZ : 0;\n\n    this.width = parameter.width ? parameter.width : 1;\n    this.height = parameter.height ? parameter.height : 1;\n\n    this.numSegX = parameter.numSegX ? parameter.numSegX : 1;\n    this.numSegY = parameter.numSegY ? parameter.numSegY : 1;\n}<\/code><\/pre>\n<h2>\u573a\u666f<\/h2>\n<h2>\u76f8\u673a<\/h2>\n<p>\u573a\u666f\u548c\u76f8\u673a\u90fd\u4e0d\u9700\u8981\u4fee\u6539\u3002<\/p>\n<h2>\u5b9e\u4f8b\u5316<\/h2>\n<p>\u5b9e\u4f8b\u5316\u4e4b\u540e\uff0c\u5148\u521b\u5efa<em>bitmap<\/em>\uff1a<\/p>\n<pre><code class=\"language-typescript\">\/\/...\n\nconst heigtmap = document.createElement(&#039;img&#039;);\nheigtmap.src = hmpng3;\nawait heigtmap.decode();\nconst heightBitmap = await createImageBitmap(heigtmap);\n\n\/\/...<\/code><\/pre>\n<p>\u5c06<em>bitmap<\/em>\u653e\u5230\u5e73\u9762\u4e0a\uff0c\u5e76\u6dfb\u52a0\u5230\u573a\u666f\u4e2d\uff1a<\/p>\n<pre><code class=\"language-typescript\">const plane1 = new Plane({\n    y: -posy, width: width, height: height, rotX: -Math.PI \/ 2,\n    numSegX: 512, numSegY: 512\n}, heightBitmap);\nscene.add(plane1);<\/code><\/pre>\n<h2>\u4ea4\u4e92<\/h2>\n<p>\u4fee\u6539\u9f20\u6807\u4e2d\u952e\u4ea4\u4e92\uff1a<\/p>\n<pre><code class=\"language-typescript\">canvas.onwheel = (e: WheelEvent) =&gt; {\n    const delta = e.deltaY \/ 100;\n    if (camera.z &gt; -delta) {\n    camera.z += e.deltaY \/ 100\n    }\n}<\/code><\/pre>\n<p>\u5176\u4ed6\u7684\u9f20\u6807\u3001\u624b\u52bf\u3001\u952e\u76d8\u7b49\u4ea4\u4e92\u64cd\u4f5c\u4fdd\u6301\u4e0d\u53d8\u3002<\/p>\n<p><em>\u6ce8\u610f\uff1a\u4f8b\u5b50\u4e2d\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\u53ef\u80fd\u968f\u65f6\u8c03\u6574\uff0c\u5177\u4f53\u4f7f\u7528\u8bf7\u53c2\u8003W3C\u7684<a href=\"https:\/\/www.w3.org\/TR\/webgpu\/\">\u5b98\u65b9\u6587\u6863<\/a><\/em><\/p>\n<p>\u6548\u679c\u9884\u89c8\u5730\u5740\uff1a<\/p>\n<p><a href=\"https:\/\/hungking.org\/webgpu-wgsl-example\/#\/part10\">\u5730\u5f62<\/a><\/p>\n<p><img src=\"https:\/\/hungking.cc\/assets\/imgs\/indeex.cc\/WebGPUWGSLbase3.gif\" alt=\"\u5730\u5f62\" \/><\/p>\n<p>code enjoy! \ud83e\udd96\ud83e\udd96\ud83e\udd96<\/p>\n<p>\u4f5c\u8005\uff1aindeex  <\/p>\n<p>\u94fe\u63a5\uff1a<a href=\"https:\/\/indeex.club\/\">https:\/\/indeex.club<\/a>  <\/p>\n<p>\u8457\u4f5c\u6743\u5f52\u4f5c\u8005\u6240\u6709\u3002\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u8054\u7cfb\u4f5c\u8005\u83b7\u5f97\u6388\u6743\uff0c\u975e\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5730\u5f62\u5176\u5b9e\u5c31\u662f\u5e26\u9ad8\u5ea6\u7684\u7eb9\u7406\uff0c\u53c8\u79f0\u9ad8\u7a0b\u7eb9\u7406\u6216\u9ad8\u5ea6\u8d34\u56fe\u3002 engine \u4fee\u6539\u4e4b\u524d\u7684engine\u4ee3\u7801\uff1a \/\/<a href=\"https:\/\/blog.indeex.club\/index.php\/2022\/03\/30\/webgpu%e5%9f%ba%e7%a1%80%ef%bc%88%e5%9b%9b%ef%bc%89-%e5%9c%b0%e5%bd%a2\/\" class=\"read-more\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[10],"tags":[15],"_links":{"self":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/278"}],"collection":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/comments?post=278"}],"version-history":[{"count":1,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/278\/revisions"}],"predecessor-version":[{"id":279,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/278\/revisions\/279"}],"wp:attachment":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/media?parent=278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/categories?post=278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/tags?post=278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}