深入理解WebGPU

WebGPU 培训

WebGPU是什么? 它是一个在浏览器中实现低级图形渲染的API,可以让Web开发人员利用GPU提高渲染性能。WebGPU支持多种GPU,并允许Web应用程序通过JavaScript,WebAssembly或HLSL代码与GPU通信。

WebGPU可与WebGL和WebXR等其他Web图形API配合使用,但它是更先进和更灵活的API,提供了更多的控制权和更好的性能,从而更适合处理复杂和大型3D场景。

要使用WebGPU,需要使用最新的Web浏览器(如Chrome)和支持WebGPU的GPU。

  const adapter = await navigator.gpu.requestAdapter();
  const device = await adapter.requestDevice();

使用WebGPU,我们需要获取GPU的适配器(adapter)和设备(device),并将其存储在变量中,以便我们可以在GPU上执行操作。

实用WebGPU

1. 使用WebGPU渲染三角形

在使用WebGPU渲染三角形之前,需要进行一些准备工作。我们需要编写代码来创建窗口、初始化WebGPU并定义渲染器所需的常量和资源。在这里,我们使用了GLSL作为着色语言。

  let vertexShader =
  `
  #version 450
  layout(location = 0) in vec3 pos;
  void main() {
        gl_Position = vec4(pos, 1.0);
  }
  `;
  let fragmentShader =
  `
  #version 450
  layout(location = 0) out vec4 outColor;
  void main() {
        outColor = vec4(1.0, 1.0, 1.0, 1.0);
  }
  `;
  async function main() {
        // Step 1: Create a window to render into.
        const canvas = document.createElement('canvas');
        document.body.appendChild(canvas);
        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;
      
        // Step 2: Initialize WebGPU and create the "context".
        const adapter = await navigator.gpu.requestAdapter();
        const device = await adapter.requestDevice();
        const context = canvas.getContext('gpupresent');
      
        // Step 3: Define the shader modules (i.e. the shader code).
        const shaderModule =
              device.createShaderModule({
                    code: vertexShader
              });
      
        const fragModule = device.createShaderModule({
              code: fragmentShader
        });
      
        // Step 4: Define the pipeline layout. This specifies the vertex input
        // format, and any other inputs we will be using in our shaders.
        const layout = device.createPipelineLayout({
              bindGroupLayouts: []
        });
      
        // Step 5: Define the render pipeline. This specifies the shaders to use,
        // as well as the pipeline layout from step 4.
        const pipeline = device.createRenderPipeline({
              layout,
              vertex: {
                  module: shaderModule,
                  entryPoint: 'main',
                  buffers: [{
                        arrayStride: 12,
                        attributes: [{
                              shaderLocation: 0,
                              offset: 0,
                              format: 'float3'
                        }]
                  }]
              },
              fragment: {
                  module: fragModule,
                  entryPoint: 'main',
                  targets: [{
                        format: 'rgba8unorm'
                  }]
              },
              primitive: {
                  topology: 'triangle-list',
                  stripIndexFormat: undefined,
                  frontFace: 'ccw',
                  cullMode: 'none',
                  clampDepth: false
              },
              depthStencil: {
                  format: undefined,
                  depthWriteEnabled: false,
                  depthCompare: 'always',
                  stencilFront: {
                        compare: 'always',
                        failOp: 'keep',
                        passOp: 'keep',
                        depthFailOp: 'keep'
                  },
                  stencilBack: {
                        compare: 'always',
                        failOp: 'keep',
                        passOp: 'keep',
                        depthFailOp: 'keep'
                  },
                  stencilReadMask: 0xff,
                  stencilWriteMask: 0xff
              }
        });
      
        // Step 6: Define the uniform buffer.
        const uniformBuffer = device.createBuffer({
              size: 16,
              usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
        });
        const uniformBindGroup = device.createBindGroup({
              layout: pipeline.getBindGroupLayout(0),
              entries: [{
                    binding: 0,
                    resource: {
                          buffer: uniformBuffer
                    }
              }]
        });
      
        // Step 7: Define the render loop.
        function update() {
              const renderPassDescriptor = {
                    colorAttachments: [{
                          attachment: context.getCurrentTexture().createView(),
                          loadValue: [0, 0, 0, 1],
                          storeOp: 'store'
                    }]
              };
        
              const commandEncoder = device.createCommandEncoder();
              const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
              passEncoder.setPipeline(pipeline);
              passEncoder.setBindGroup(0, uniformBindGroup);
              passEncoder.draw(3, 1, 0, 0);
              passEncoder.endPass();
        
              device.queue.submit([commandEncoder.finish()]);
              requestAnimationFrame(update);
        }
      
        requestAnimationFrame(update);
  }
  main();

WebGPU React

使用WebGPU与React结合使用可以快速创建高性能的3D应用程序。下面的示例代码演示了如何在React中使用Jaspr和WebGPU来渲染一个简单的3D场景。

1. 在React中使用WebGPU渲染3D场景

  import * as THREE from 'three';
import {
  useResource,
  useFrame,
  Canvas,
} from 'react-three-fiber';
import Jaspr from 'jaspr';

function TriangleMesh(props) {
  const geometry = new THREE.Geometry();
  geometry.vertices.push(
    new THREE.Vector3(-1, 0, 0),
    new THREE.Vector3(0, 1, 0),
    new THREE.Vector3(1, 0, 0),
  );
  geometry.faces.push(new THREE.Face3(0, 1, 2));
  const [meshRef] = useResource();
  useFrame(({ clock }) => {
    meshRef.rotation.x = clock.getElapsedTime() * 0.5;
    meshRef.rotation.y = clock.getElapsedTime() * 0.8;
  });
  return (
    
      
         v.toArray())}
          itemSize={3}
        />
      
      
    
  );
}

function Scene() {
  return (
    
      
    
  );
}

function App() {
  return (
     {
        gl.getContext().makeCurrent();
      }}
    >
      
    
  );
}

export default App;

2. WebGPU 渲染动画

下面是一个简单的示例代码,演示如何在WebGPU上渲染动画。

  async function main() {
        // Step 1: Create a window to render into.
        const canvas = document.createElement('canvas');
        document.body.appendChild(canvas);
        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;
      
        // Step 2: Initialize WebGPU and create the "context".
        const adapter = await navigator.gpu.requestAdapter();
        const device = await adapter.requestDevice();
        const context = canvas.getContext('gpupresent');
      
        // Step 3: Define the shader modules (i.e. the shader code).
        const shaderModule =
              device.createShaderModule({
                    code: vertexShader
              });
      
        const fragModule = device.createShaderModule({
              code: fragmentShader
        });
      
        // Step 4: Define the pipeline layout.
        const layout = device.createPipelineLayout({
              bindGroupLayouts: []
        });
      
        // Step 5: Define the render pipeline.
        const pipeline = device.createRenderPipeline({
              layout,
              vertex: {
                  module: shaderModule,
                  entryPoint: 'main',
                  buffers: [{
                        arrayStride: 12,
                        attributes: [{
                              shaderLocation: 0,
                              offset: 0,
                              format: 'float3'
                        }]
                  }]
              },
              fragment: {
                  module: fragModule,
                  entryPoint: 'main',
                  targets: [{
                        format: 'rgba8unorm'
                  }]
              },
              primitive: {
                  topology: 'triangle-list',
                  stripIndexFormat: undefined,
                  frontFace: 'ccw',
                  cullMode: 'none',
                  clampDepth: false
              },
              depthStencil: {
                  format: undefined,
                  depthWriteEnabled: false,
                  depthCompare: 'always',
                  stencilFront: {
                        compare: 'always',
                        failOp: 'keep',
                        passOp: 'keep',
                        depthFailOp: 'keep'
                  },
                  stencilBack: {
                        compare: 'always',
                        failOp: 'keep',
                        passOp: 'keep',
                        depthFailOp: 'keep'
                  },
                  stencilReadMask: 0xff,
                  stencilWriteMask: 0xff
              }
        });
      
        // Step 6: Define the uniform buffer.
        const uniformBuffer = device.createBuffer({
              size: 16,
              usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
        });
        const uniformBindGroup = device.createBindGroup({
              layout: pipeline.getBindGroupLayout(0),
              entries: [{
                    binding: 0,
                    resource: {
                          buffer: uniformBuffer
                    }
              }]
        });
      
        // Step 7: Define the render loop.
        let time = 0;
        function update() {
              const t = performance.now() / 1000.0;
              const delta = t - time;
              time = t;
      
              const renderPassDescriptor = {
                    colorAttachments: [{
                          attachment: context.getCurrentTexture().createView(),
                          loadValue: [0, 0, 0, 1],
                          storeOp: 'store'
                    }]
              };
        
              const commandEncoder = device.createCommandEncoder();
              const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
              passEncoder.setPipeline(pipeline);
              passEncoder.setBindGroup(0, uniformBindGroup);
              passEncoder.draw(3, 1, 0, 0);
              passEncoder.endPass();
        
              device.queue.submit([commandEncoder.finish()]);
              requestAnimationFrame(update);
        }
        update();
  }
  main();

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/245345.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-12-12 13:08
下一篇 2024-12-12 13:08

相关推荐

  • 深入解析Vue3 defineExpose

    Vue 3在开发过程中引入了新的API `defineExpose`。在以前的版本中,我们经常使用 `$attrs` 和` $listeners` 实现父组件与子组件之间的通信,但…

    编程 2025-04-25
  • 深入理解byte转int

    一、字节与比特 在讨论byte转int之前,我们需要了解字节和比特的概念。字节是计算机存储单位的一种,通常表示8个比特(bit),即1字节=8比特。比特是计算机中最小的数据单位,是…

    编程 2025-04-25
  • 深入理解Flutter StreamBuilder

    一、什么是Flutter StreamBuilder? Flutter StreamBuilder是Flutter框架中的一个内置小部件,它可以监测数据流(Stream)中数据的变…

    编程 2025-04-25
  • 深入探讨OpenCV版本

    OpenCV是一个用于计算机视觉应用程序的开源库。它是由英特尔公司创建的,现已由Willow Garage管理。OpenCV旨在提供一个易于使用的计算机视觉和机器学习基础架构,以实…

    编程 2025-04-25
  • 深入了解scala-maven-plugin

    一、简介 Scala-maven-plugin 是一个创造和管理 Scala 项目的maven插件,它可以自动生成基本项目结构、依赖配置、Scala文件等。使用它可以使我们专注于代…

    编程 2025-04-25
  • 深入了解LaTeX的脚注(latexfootnote)

    一、基本介绍 LaTeX作为一种排版软件,具有各种各样的功能,其中脚注(footnote)是一个十分重要的功能之一。在LaTeX中,脚注是用命令latexfootnote来实现的。…

    编程 2025-04-25
  • 深入探讨冯诺依曼原理

    一、原理概述 冯诺依曼原理,又称“存储程序控制原理”,是指计算机的程序和数据都存储在同一个存储器中,并且通过一个统一的总线来传输数据。这个原理的提出,是计算机科学发展中的重大进展,…

    编程 2025-04-25
  • 深入理解Python字符串r

    一、r字符串的基本概念 r字符串(raw字符串)是指在Python中,以字母r为前缀的字符串。r字符串中的反斜杠(\)不会被转义,而是被当作普通字符处理,这使得r字符串可以非常方便…

    编程 2025-04-25
  • 深入剖析MapStruct未生成实现类问题

    一、MapStruct简介 MapStruct是一个Java bean映射器,它通过注解和代码生成来在Java bean之间转换成本类代码,实现类型安全,简单而不失灵活。 作为一个…

    编程 2025-04-25
  • 深入了解Python包

    一、包的概念 Python中一个程序就是一个模块,而一个模块可以引入另一个模块,这样就形成了包。包就是有多个模块组成的一个大模块,也可以看做是一个文件夹。包可以有效地组织代码和数据…

    编程 2025-04-25

发表回复

登录后才能评论