"Use the whole buffer" is !0, not 0 (#663)

* "Use the whole buffer" is !0, not 0

Fixes #654
Applies to BufferBinding, set_vertex_buffer, set_index_buffer

* Add BufferSize type alias

* Make BufferSize a transparent type

Add a custom serialization "buddy" type
Use BufferSize::WHOLE instead of crate::WHOLE_SIZE

* Move SerBufferSize into device::trace mod

Co-authored-by: Paul Kernfeld <paulkernfeld@gmail.com>
This commit is contained in:
Paul Kernfeld 2020-05-26 13:25:06 -04:00 committed by GitHub
parent f81638b4dd
commit d529526e7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 75 additions and 19 deletions

View File

@ -5,7 +5,7 @@
use crate::{
id::{BindGroupLayoutId, BufferId, DeviceId, SamplerId, TextureViewId},
track::{TrackerSet, DUMMY_SELECTOR},
FastHashMap, LifeGuard, RefCount, Stored,
BufferSize, FastHashMap, LifeGuard, RefCount, Stored,
};
use arrayvec::ArrayVec;
@ -88,7 +88,7 @@ pub struct PipelineLayout<B: hal::Backend> {
pub struct BufferBinding {
pub buffer: BufferId,
pub offset: BufferAddress,
pub size: BufferAddress,
pub size: BufferSize,
}
#[repr(C)]

View File

@ -17,7 +17,7 @@ use crate::{
pipeline::PipelineFlags,
resource::{BufferUse, TextureUse, TextureViewInner},
track::TrackerSet,
Stored,
BufferSize, Stored,
};
use arrayvec::ArrayVec;
@ -70,13 +70,13 @@ pub enum RenderCommand {
SetIndexBuffer {
buffer_id: id::BufferId,
offset: BufferAddress,
size: BufferAddress,
size: BufferSize,
},
SetVertexBuffer {
slot: u32,
buffer_id: id::BufferId,
offset: BufferAddress,
size: BufferAddress,
size: BufferSize,
},
SetBlendColor(Color),
SetStencilReference(u32),
@ -1028,8 +1028,8 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
.unwrap();
assert!(buffer.usage.contains(BufferUsage::INDEX), "An invalid setIndexBuffer call has been made. The buffer usage is {:?} which does not contain required usage INDEX", buffer.usage);
let end = if size != 0 {
offset + size
let end = if size != BufferSize::WHOLE {
offset + size.0
} else {
buffer.size
};
@ -1065,15 +1065,19 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
.vertex
.inputs
.extend(iter::repeat(VertexBufferState::EMPTY).take(empty_slots));
state.vertex.inputs[slot as usize].total_size = if size != 0 {
size
state.vertex.inputs[slot as usize].total_size = if size != BufferSize::WHOLE {
size.0
} else {
buffer.size - offset
};
let range = hal::buffer::SubRange {
offset,
size: if size != 0 { Some(size) } else { None },
size: if size != BufferSize::WHOLE {
Some(size.0)
} else {
None
},
};
unsafe {
raw.bind_vertex_buffers(slot, iter::once((&buffer.raw, range)));
@ -1276,7 +1280,7 @@ pub mod render_ffi {
super::{PhantomSlice, RawPass, Rect},
RenderCommand,
};
use crate::{id, RawString};
use crate::{id, BufferSize, RawString};
use std::{convert::TryInto, slice};
use wgt::{BufferAddress, Color, DynamicOffset};
@ -1316,7 +1320,7 @@ pub mod render_ffi {
pass: &mut RawPass,
buffer_id: id::BufferId,
offset: BufferAddress,
size: BufferAddress,
size: BufferSize,
) {
pass.encode(&RenderCommand::SetIndexBuffer {
buffer_id,
@ -1331,7 +1335,7 @@ pub mod render_ffi {
slot: u32,
buffer_id: id::BufferId,
offset: BufferAddress,
size: BufferAddress,
size: BufferSize,
) {
pass.encode(&RenderCommand::SetVertexBuffer {
slot,

View File

@ -7,7 +7,7 @@ use crate::{
hub::{GfxBackend, Global, GlobalIdentityHandlerFactory, Input, Token},
id, pipeline, resource, swap_chain,
track::{BufferState, TextureState, TrackerSet},
FastHashMap, LifeGuard, PrivateFeatures, Stored,
BufferSize, FastHashMap, LifeGuard, PrivateFeatures, Stored,
};
use arrayvec::ArrayVec;
@ -1339,17 +1339,17 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
let sub_range = hal::buffer::SubRange {
offset: bb.offset,
size: if bb.size == 0 {
size: if bb.size == BufferSize::WHOLE {
None
} else {
let end = bb.offset + bb.size;
let end = bb.offset + bb.size.0;
assert!(
end <= buffer.size,
"Bound buffer range {:?} does not fit in buffer size {}",
bb.offset..end,
buffer.size
);
Some(bb.size)
Some(bb.size.0)
},
};
hal::pso::Descriptor::Buffer(&buffer.raw, sub_range)

View File

@ -4,7 +4,7 @@
use crate::{
command::{BufferCopyView, TextureCopyView},
id,
id, BufferSize,
};
#[cfg(feature = "trace")]
use std::io::Write as _;
@ -14,6 +14,36 @@ use std::ops::Range;
type FileName = String;
/// This type allows us to make the serialized representation of a BufferSize more human-readable
#[allow(dead_code)]
#[cfg_attr(feature = "trace", derive(serde::Serialize))]
#[cfg_attr(feature = "replay", derive(serde::Deserialize))]
pub enum SerBufferSize {
Size(u64),
Whole,
}
#[cfg(feature = "trace")]
impl From<BufferSize> for SerBufferSize {
fn from(buffer_size: BufferSize) -> Self {
if buffer_size == BufferSize::WHOLE {
Self::Whole
} else {
Self::Size(buffer_size.0)
}
}
}
#[cfg(feature = "replay")]
impl From<SerBufferSize> for BufferSize {
fn from(ser_buffer_size: SerBufferSize) -> Self {
match ser_buffer_size {
SerBufferSize::Size(size) => BufferSize(size),
SerBufferSize::Whole => BufferSize::WHOLE,
}
}
}
pub const FILE_NAME: &str = "trace.ron";
#[derive(Debug)]
@ -23,7 +53,7 @@ pub enum BindingResource {
Buffer {
id: id::BufferId,
offset: wgt::BufferAddress,
size: wgt::BufferAddress,
size: BufferSize,
},
Sampler(id::SamplerId),
TextureView(id::TextureViewId),

View File

@ -46,6 +46,8 @@ use std::sync::atomic;
use atomic::{AtomicUsize, Ordering};
use peek_poke::PeekPoke;
use std::{os::raw::c_char, ptr};
type SubmissionIndex = usize;
@ -54,6 +56,26 @@ type Epoch = u32;
pub type RawString = *const c_char;
pub const WHOLE_SIZE: wgt::BufferAddress = !0;
#[repr(transparent)]
#[derive(Clone, Copy, Debug, Default, PartialEq, PeekPoke)]
#[cfg_attr(
feature = "trace",
derive(serde::Serialize),
serde(into = "crate::device::trace::SerBufferSize")
)]
#[cfg_attr(
feature = "replay",
derive(serde::Deserialize),
serde(from = "crate::device::trace::SerBufferSize")
)]
pub struct BufferSize(pub u64);
impl BufferSize {
const WHOLE: BufferSize = BufferSize(!0u64);
}
//TODO: make it private. Currently used for swapchain creation impl.
#[derive(Debug)]
pub struct RefCount(ptr::NonNull<AtomicUsize>);