WGPU: How i can change "fragment.targets" in RenderPipeline at runtime?

How i can change "fragment.targets" in RenderPipeline at runtime?
I have some windows. And i want that my program doesn't fail after add window.(I render same objects in a window as objects in another window).

Pipelines, like most wgpu entities other than buffers and textures, are immutable. You must create multiple pipelines instead of changing a single one.

That said, note that there is a limited number of color attachments possible. The feature of multiple color attachments is usually (as far as I know) used for visual effects that need more than four (RGBA) numeric outputs per pixel, not for rendering to multiple windows. A more usual approach would be to draw each window separately, or to draw the scene to a texture and copy that texture to each window surface (as a separate but cheap draw call).

That is, for each window I either need to make my own Pipeline or draw into a texture?

No, if you use only one render target when drawing, for each window, then you only need one pipeline for all of them, because the surface or other texture you are drawing to is specified as a color attachment in the render pass, not the pipeline. The pipeline says how to treat the color attachments, not what they are.

Why then?

Must match the format of the corresponding color attachment in [CommandEncoder::begin_render_pass]

(ColorTargetState in wgpu - Rust)

The format of the texture has to match the format specified in the pipeline, but it can be any texture with that format.

The reason for what goes in a pipeline and what does not is that pipelines are essentially a sort of compiled program/function[1] — they take your shader and other information and turn it into fully concrete instructions the GPU can execute on the data actually in its memory. So, the pipeline has to know about the texture format in order to give the GPU appropriate rendering/writing instructions for that format. But changing the texture is just changing the address to write to.


  1. How much compilation is actually involved depends greatly on the GPU architecture details. ↩︎