Skip to content

Real Fast: Textures

Devin Brite edited this page Jul 13, 2020 · 1 revision

Putting the texture on the GPU

Create a Texture using a new TextureDescriptor.

Load your image bytes into your program however you wish (gltf-rs does this for you - Learn Wgpu uses the image crate to do this).

Create a Buffer with said byte data. Then copy that Buffer to the Texture you just made. (And don't forget to finish+submit your command).

Using the Texture you just created, create a default TextureView.

Create a Sampler using a new SamplerDescriptor.

Create a bind group

It's time to create a BindGroupLayout. This is done with a new BindGroupLayoutDescriptor which defines two bindings to be used in the fragment shader:

  1. a BindingType::SampledTexture and
  2. a BindingType::Sampler.

Next create a BindGroup using the previously created TextureView and Sampler in the bindings, and the BindGroupLayout.

Put the BindGroupLayout and BindGroup into a struct we'll call a WgpuTexture.

Rendering with your texture

Rust

When creating the PipelineLayout, include the WgpuTexture's BindGroupLayout in the bind_group_layouts.

Finally, before you draw your RenderPass, just add the WgpuTexture's BindGroup

GLSL

Declare the new TextureView and Sampler uniforms. N represents which BindGroup this is. In this example + the gltf loading example, N would be 0.

layout(set = N, binding = 0) uniform texture2D t_diffuse;
layout(set = N, binding = 1) uniform sampler s_diffuse;

You can access the color at a coordinate like:

vec4 tex_color = texture(sampler2D(t_diffuse, s_diffuse), v_tex_coords);