Wgpu: Validate resource usage as specified on creation

Created on 14 Jun 2019  路  4Comments  路  Source: gfx-rs/wgpu

On every operation, we should check if a resource supports it given the usage flags specified in create_texture and create_buffer.

validation help wanted enhancement

All 4 comments

Example in the wild:

VALIDATION [VUID-vkCmdCopyBuffer-srcBuffer-00118 (0)] : Invalid usage flag for Buffer 0x1936 used by vkCmdCopyBuffer(). In this case, Buffer should have VK_BUFFER_USAGE_TRANSFER_SRC_BIT set during creation. The Vulkan spec states: srcBuffer must have been created with VK_BUFFER_USAGE_TRANSFER_SRC_BIT usage flag (https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#VUID-vkCmdCopyBuffer-srcBuffer-00118)
object info: (type: BUFFER, hndl: 6454)

I'd like to take this. Will PR in the coming days or come back here to ask questions, if I get lost in the woods.

Here's my preliminary take on where to insert validations:

Buffers

MAP_READ and MAP_WRITE

wgpu_buffer_map_read_async: needs MAP_READ
wgpu_buffer_map_write_async: needs MAP_WRITE
wgpu_buffer_unmap: needs MAP_READ | MAP_WRITE

COPY_SRC and COPY_DST

  • wgpu_command_encoder_copy_buffer_to_buffer: src buffer needs COPY_SRC and dst buffer needs COPY_DST
  • wgpu_command_encoder_copy_buffer_to_texture: buffer needs COPY_SRC
  • wgpu_command_encoder_copy_texture_to_buffer: buffer needs COPY_DST

VERTEX and INDEX

wgpu_render_pass_set_vertex_buffers: needs VERTEX
wgpu_render_pass_set_index_buffer: needs INDEX

UNIFORM, STORAGE and STORAGE_READ

wgpu_device_create_bind_group: needs UNIFORM if defined UniformBuffer in bind group layout
wgpu_device_create_bind_group: needs STORAGE if defined StorageBuffer in bind group layout
wgpu_device_create_bind_group: needs STORAGE_READ if defined StorageBuffer { readonly: true, .. } in bind group layout

INDIRECT

not sure... is this exposed to wgpu-rs?

Textures

COPY_SRC and COPY_DST

wgpu_command_encoder_copy_buffer_to_texture: texture needs COPY_DST
wgpu_command_encoder_copy_texture_to_buffer: texture needs COPY_SRC
wgpu_command_encoder_copy_texture_to_texture: src texture needs COPY_SRC and dst texture needs COPY_DST

SAMPLED

wgpu_device_create_bind_group: needs SAMPLED if defined SampledTexture in bind group layout

STORAGE

wgpu_device_create_bind_group: needs STORAGE if defined StorageTexture in bind group layout

OUTPUT_ATTACHMENT

wgpu_command_encoder_begin_render_pass: needs OUTPUT_ATTACHMENT if used as color or depth-stencil attachment


Also... Is it okay to add a usage field to resource::Buffer and resource::Texture, or should we try to reconstruct wgpu's {Buffer,Texture}Usage from the low level gfx-rs and rendy usage flags? The second might be less overhead but seems a bit scary to me.

I've started on the implementation also, I expect to finish during the weekend.

Great list, thank you! Here is a few corrections:

wgpu_buffer_unmap: needs MAP_READ | MAP_WRITE

Shouldn't technically need to check for the usage here, since we require the buffer to be mapped at this point anyway (and mapping checks were done earlier).

wgpu_device_create_bind_group: needs STORAGE if defined StorageBuffer in bind group layout

Only if readonly=false

INDIRECT - not sure... is this exposed to wgpu-rs?

It's in wgpu-native, just haven't made it to wgpu-rs yet, see https://github.com/gfx-rs/wgpu-rs/pull/37
It's not much work in case of anybody wanting to help out...

wgpu_command_encoder_begin_render_pass: needs OUTPUT_ATTACHMENT if used as color or depth-stencil attachment

IIRC, it's also needed for resolve targets: https://github.com/gfx-rs/wgpu-rs/blob/472fa2934a951dabdbbe51be6bea8477915a411b/src/lib.rs#L419

Also... Is it okay to add a usage field to resource::Buffer and resource::Texture, or should we try to reconstruct wgpu's {Buffer,Texture}Usage from the low level gfx-rs and rendy usage flags?

Totally OK to add those usage fields to our resources 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rukai picture rukai  路  5Comments

kvark picture kvark  路  3Comments

paulkernfeld picture paulkernfeld  路  3Comments

kvark picture kvark  路  5Comments

msiglreith picture msiglreith  路  4Comments