[][src]Trait web_glitz::pipeline::resources::Resources

pub unsafe trait Resources {
    type Encoding;

    const LAYOUT: &'static [TypedResourceSlotDescriptor];

    fn encode_bind_group(
        self,
        encoding_context: &mut BindGroupEncodingContext
    ) -> BindGroupEncoding<'_, Self::Encoding>; }

Provides a group of resources (uniform block buffers, sampled textures) that can initialize a BindGroup.

See [RendinderingContext::create_bind_group] for details on how a BindGroup is created.

The resulting bind group may be bound to a pipeline, such that all pipeline invokations may access these resources when the pipeline is executed. See [GraphicsPipelineTaskBuilder::bind_resources] for details on how bind groups are bound to a pipeline.

This type acts as an automatically derivable trait for a TypedBindableResourceGroup type that acts as its own [TypedBindGroupLayout]. This trait is only intended to be derived automatically; if your set of resources cannot be adequatly described by automatically deriving this trait, rather than manually implementing this trait, instead consider manually implementing the TypedBindableResourceGroup and [TypedBindGroupLayout] traits separately.

Note that a BindGroup may also be initialized from an untyped [BindableResourceGroup]. The resulting bind group will be untyped and binding it to a pipeline is unsafe: you are responsible for ensuring that the resources you bind match the resources expected by the shader stages of the pipeline, see [GraphicsPipelineTaskBuilder::bind_resources_untyped] for details.

Usage

The programmable stages of a pipeline may require access to various resources during pipeline execution. To this end, the code for a programmable stage may define slots for such resources (e.g. uniform blocks, texture samplers):

#version 300 es

uniform sampler2D some_texture;

uniform sampler2DArray some_other_texture;

layout(std140) uniform SomeUniformBlock {
    vec4 some_uniform;
    mat4 some_other_uniform;
};

main () {
    ...
}

Note that GLSL ES 3.0 does not allow you to define your own explicit bind groups. Instead, bind groups are implicitly defined for each kind of resource:

  • Uniform buffers: this bind group uses bind group index 0.
  • Sampled textures: this bind group uses bind group index 1.

This trait may be safely derived automatically on a type to define how specific resource instances should be bound to the pipeline:

use web_glitz::image::texture_2d::FloatSampledTexture2D;
use web_glitz::image::texture_2d_array::FloatSampledTexture2DArray;
use web_glitz::buffer::Buffer;

#[derive(web_glitz::derive::Resources)]
struct BufferResources<'a> {
    #[resource(binding=0, name="SomeUniformBlock")]
    some_uniform_block: &'a Buffer<SomeUniformBlock>,
}

#[derive(web_glitz::derive::Resources)]
struct TextureResources<'a> {
    #[resource(binding=0)]
    some_texture: FloatSampledTexture2D<'a>,

    #[resource(binding=1, name="some_other_texture")]
    second_texture: FloatSampledTexture2DArray<'a>,
}

#[std140::repr_std140]
#[derive(web_glitz::derive::InterfaceBlock)]
struct SomeUniformBlock {
    some_uniform: std140::vec4,
    some_other_uniform: std140::mat4x4
}

Any field marked with a #[resource(...)] attribute defines a resource binding; unmarked fields are ignored when initializing a bind group.

A #[resource(...)] attribute must always declare a binding index; for a texture this should be a positive integer that is smaller than the [RenderingContext::max_texture_resource_index] of the [RenderingContext] with which you intend to use the Resources (hardware dependent, at least 32); for a uniform buffer this should be a positive integer that is smaller than the [RenderingContext::max_buffer_resource_index] for the [RenderingContext] with which you intend to use the Resources (hardware dependent, at least 24).

If the field name does not match the resource name used in the shader code, then the attribute should also declare a name with a string that does match the resource name used in the shader code; if the field name does match the name used in the shader, then name may be omitted.

The field's type must implement Resource; marking a field that does not implement Resource with #[resource(...)] will result in a compilation error. If multiple #[resource(...)] fields are defined, then all fields must declare a unique binding index; 2 or more #[resource(...)] fields with the same binding index will also result in a compilation error.

Associated Types

Loading content...

Associated Constants

Loading content...

Required methods

fn encode_bind_group(
    self,
    encoding_context: &mut BindGroupEncodingContext
) -> BindGroupEncoding<'_, Self::Encoding>

Loading content...

Implementors

Loading content...