WebGPU04-Begin-The Surface,原来如此~
Begin | WebGPU
2.The Surface-3
译者注:本节内容紧随上节,是对上节内容的详细讲解。
LearnWgpu项目代码库
-
RenderPassDescriptor:渲染通道描述符做了什么?
也许在阅读这篇博客的你能够仅通过看一遍下面的代码就明白其中的含义,但我如果不将思路捋一遍就难以理解它。
下面让我们再来看一遍代码:
&wgpu::RenderPassDescriptor { label: Some("Render Pass"), color_attachments: &[ // ... ], depth_stencil_attachment: None, }
一个
RenderPassDescriptor
只有三个域:label
、color_attachments
、depth_stencil_attachment
。color_attachments
描述了我们要在哪里上色。我们使用之前创建的
TextureView
来确保我们能渲染到屏幕上。之后我们将要使用
depth_stencil_attachment
,但现在先将它设置为None
:wgpu::RenderPassColorAttachment { view: &view, resolve_target: None, ops: wgpu::Operations { load: wgpu::LoadOp::Clear(wgpu::Color { r: 0.1, g: 0.2, b: 0.3, a: 1.0, }), store: true, }, }
RenderPassColorAttachment
用view
域告知wgpu
用什么组织来存储颜色。在这种情况下,我们指定我们用
surface.get_current_texture()
创建的view
。这意味着我们向此附件中绘制的所有颜色都会在屏幕上显示。
resolve_target
纹理将会接收解析后的输出。除非启用了多重采样(multisampling),否则其将与view
相同。我们暂不需要指定它,所以此处设置为
None
。ops
字段采用了wpgu::Operations
对象。这告知wgpu要用在屏幕上显示的颜色做什么(由view
指定)。load
字段告知wgpu如何处理从前一帧存储的颜色。通常,我们用偏蓝的颜色清理屏幕。store
字段告知wgpu我们是否要将渲染结果存储到TextureView
后面的Texture
中(在本例中是SurfaceTexture
)。我们使用
true
作为我们想要存储渲染结果, -
Validation Error:发生了验证错误?
如果在你的机器上 wgpu 使用了 Vulkan,你可能会陷入验证错误如果你使用旧版本的 Vulkan SDK。你应当使用确保你的 Vlukan 高于
1.2.182
版本来避免出现错误。如果错误仍然存在,你可能遇到了存在于 wgpu 中的漏洞(bug),你可以在 gfx-rs/wgpu 中反馈。
-
Challenge:挑战
修改
input()
方法用于捕获鼠标活动,并通过使用它来更新颜色。提示:你可能需要使用
WindowEvent::CursorMoved。
该博客由本人个人翻译自Learn Wgpu,因此可能有部分文本不易理解或出现错误,如有发现还望告知,本人必定及时更改。