Skip to content

Commit 3a2d3a0

Browse files
committed
Partially moved Buffer to associated type. Getting ICE
1 parent a753f73 commit 3a2d3a0

File tree

4 files changed

+64
-44
lines changed

4 files changed

+64
-44
lines changed

src/device/draw.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,15 @@ impl DataBuffer {
7575
}
7676

7777
#[allow(missing_docs)] //TODO
78-
pub trait CommandBuffer {
78+
pub trait CommandBuffer<B> {
7979
/// An empty constructor
8080
fn new() -> Self;
8181
/// Clear the command buffer contents, retain the allocated storage
8282
fn clear(&mut self);
8383
fn bind_program(&mut self, back::Program);
8484
fn bind_array_buffer(&mut self, back::ArrayBuffer);
85-
fn bind_attribute(&mut self, ::AttributeSlot, back::Buffer, attrib::Format);
86-
fn bind_index(&mut self, back::Buffer);
85+
fn bind_attribute(&mut self, ::AttributeSlot, B, attrib::Format);
86+
fn bind_index(&mut self, B);
8787
fn bind_frame_buffer(&mut self, target::Access, back::FrameBuffer);
8888
/// Unbind any surface from the specified target slot
8989
fn unbind_target(&mut self, target::Access, target::Target);
@@ -93,7 +93,7 @@ pub trait CommandBuffer {
9393
fn bind_target_texture(&mut self, target::Access, target::Target, back::Texture,
9494
target::Level, Option<target::Layer>);
9595
fn bind_uniform_block(&mut self, back::Program, ::UniformBufferSlot,
96-
::UniformBlockIndex, back::Buffer);
96+
::UniformBlockIndex, B);
9797
fn bind_uniform(&mut self, shade::Location, shade::UniformValue);
9898
fn bind_texture(&mut self, ::TextureSlot, tex::TextureKind, back::Texture,
9999
Option<::SamplerHandle>);
@@ -106,7 +106,7 @@ pub trait CommandBuffer {
106106
Option<::state::Stencil>, ::state::CullMode);
107107
fn set_blend(&mut self, Option<::state::Blend>);
108108
fn set_color_mask(&mut self, ::state::ColorMask);
109-
fn update_buffer(&mut self, back::Buffer, DataPointer, usize);
109+
fn update_buffer(&mut self, B, DataPointer, usize);
110110
fn update_texture(&mut self, tex::TextureKind, back::Texture,
111111
tex::ImageInfo, DataPointer);
112112
fn call_clear(&mut self, target::ClearData, target::Mask);

src/device/gl_device/draw.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
use Command;
1818
use std::slice;
1919

20+
type Buffer = <super::GlDevice as ::Device>::Buffer;
21+
2022
pub struct GlCommandBuffer {
21-
buf: Vec<::Command>,
23+
buf: Vec<::Command<Buffer>>,
2224
}
2325

2426
impl GlCommandBuffer {
@@ -27,7 +29,7 @@ impl GlCommandBuffer {
2729
}
2830
}
2931

30-
impl ::draw::CommandBuffer for GlCommandBuffer {
32+
impl ::draw::CommandBuffer<Buffer> for GlCommandBuffer {
3133
fn new() -> GlCommandBuffer {
3234
GlCommandBuffer {
3335
buf: Vec::new(),
@@ -46,12 +48,12 @@ impl ::draw::CommandBuffer for GlCommandBuffer {
4648
self.buf.push(Command::BindArrayBuffer(vao));
4749
}
4850

49-
fn bind_attribute(&mut self, slot: ::AttributeSlot, buf: super::Buffer,
51+
fn bind_attribute(&mut self, slot: ::AttributeSlot, buf: Buffer,
5052
format: ::attrib::Format) {
5153
self.buf.push(Command::BindAttribute(slot, buf, format));
5254
}
5355

54-
fn bind_index(&mut self, buf: super::Buffer) {
56+
fn bind_index(&mut self, buf: Buffer) {
5557
self.buf.push(Command::BindIndex(buf));
5658
}
5759

@@ -75,7 +77,7 @@ impl ::draw::CommandBuffer for GlCommandBuffer {
7577
}
7678

7779
fn bind_uniform_block(&mut self, prog: super::Program, slot: ::UniformBufferSlot,
78-
index: ::UniformBlockIndex, buf: super::Buffer) {
80+
index: ::UniformBlockIndex, buf: Buffer) {
7981
self.buf.push(Command::BindUniformBlock(prog, slot, index, buf));
8082
}
8183

@@ -120,7 +122,7 @@ impl ::draw::CommandBuffer for GlCommandBuffer {
120122
self.buf.push(Command::SetColorMask(mask));
121123
}
122124

123-
fn update_buffer(&mut self, buf: super::Buffer, data: ::draw::DataPointer,
125+
fn update_buffer(&mut self, buf: Buffer, data: ::draw::DataPointer,
124126
offset_bytes: usize) {
125127
self.buf.push(Command::UpdateBuffer(buf, data, offset_bytes));
126128
}

src/device/gl_device/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ pub struct RawMapping {
4848
target: gl::types::GLenum,
4949
}
5050

51-
pub type Buffer = gl::types::GLuint;
5251
pub type ArrayBuffer = gl::types::GLuint;
5352
pub type Shader = gl::types::GLuint;
5453
pub type Program = gl::types::GLuint;
@@ -557,6 +556,7 @@ impl GlDevice {
557556
}
558557

559558
impl Device for GlDevice {
559+
type Buffer = gl::types::GLuint;
560560
type CommandBuffer = GlCommandBuffer;
561561

562562
fn get_capabilities<'a>(&'a self) -> &'a ::Capabilities {
@@ -577,7 +577,8 @@ impl Device for GlDevice {
577577
}
578578
}
579579

580-
fn create_buffer_raw(&mut self, size: usize, usage: BufferUsage) -> ::BufferHandle<()> {
580+
fn create_buffer_raw(&mut self, size: usize, usage: BufferUsage)
581+
-> ::BufferHandle<<Self as Device>::Buffer, ()> {
581582
let name = self.create_buffer_internal();
582583
let info = ::BufferInfo {
583584
usage: usage,

src/device/lib.rs

+48-31
Original file line numberDiff line numberDiff line change
@@ -159,25 +159,25 @@ impl<T: Copy, I> Handle<T, I> {
159159

160160
/// Type-safe buffer handle
161161
#[derive(Copy, Debug, PartialEq, Clone)]
162-
pub struct BufferHandle<T> {
163-
raw: RawBufferHandle,
162+
pub struct BufferHandle<B, T> {
163+
raw: RawBufferHandle<B>,
164164
}
165165

166-
impl<T> BufferHandle<T> {
166+
impl<B, T> BufferHandle<B, T> {
167167
/// Create a type-safe BufferHandle from a RawBufferHandle
168-
pub fn from_raw(handle: RawBufferHandle) -> BufferHandle<T> {
168+
pub fn from_raw(handle: RawBufferHandle<B>) -> BufferHandle<B, T> {
169169
BufferHandle {
170170
raw: handle,
171171
}
172172
}
173173

174174
/// Cast the type this BufferHandle references
175-
pub fn cast<U>(self) -> BufferHandle<U> {
175+
pub fn cast<U>(self) -> BufferHandle<B, U> {
176176
BufferHandle::from_raw(self.raw)
177177
}
178178

179179
/// Get the underlying GL name for this BufferHandle
180-
pub fn get_name(&self) -> back::Buffer {
180+
pub fn get_name(&self) -> B {
181181
self.raw.get_name()
182182
}
183183

@@ -187,7 +187,7 @@ impl<T> BufferHandle<T> {
187187
}
188188

189189
/// Get the underlying raw Handle
190-
pub fn raw(&self) -> RawBufferHandle {
190+
pub fn raw(&self) -> RawBufferHandle<B> {
191191
self.raw
192192
}
193193

@@ -200,8 +200,8 @@ impl<T> BufferHandle<T> {
200200
}
201201
}
202202

203-
/// Raw (untyped) Buffer Handle
204-
pub type RawBufferHandle = Handle<back::Buffer, BufferInfo>;
203+
/// Raw (untyped) buffer handle
204+
pub type RawBufferHandle<B> = Handle<B, BufferInfo>;
205205
/// Array Buffer Handle
206206
pub type ArrayBufferHandle = Handle<back::ArrayBuffer, ()>;
207207
/// Shader Handle
@@ -321,11 +321,11 @@ pub struct BufferInfo {
321321
/// such as OpenGL (prior to GLNG) and DirectX (prior to DX12)
322322
#[allow(missing_docs)]
323323
#[derive(Copy, Debug)]
324-
pub enum Command {
324+
pub enum Command<B> {
325325
BindProgram(back::Program),
326326
BindArrayBuffer(back::ArrayBuffer),
327-
BindAttribute(AttributeSlot, back::Buffer, attrib::Format),
328-
BindIndex(back::Buffer),
327+
BindAttribute(AttributeSlot, B, attrib::Format),
328+
BindIndex(B),
329329
BindFrameBuffer(target::Access, back::FrameBuffer),
330330
/// Unbind any surface from the specified target slot
331331
UnbindTarget(target::Access, target::Target),
@@ -334,7 +334,7 @@ pub enum Command {
334334
/// Bind a level of the texture to the specified target slot
335335
BindTargetTexture(target::Access, target::Target, back::Texture,
336336
target::Level, Option<target::Layer>),
337-
BindUniformBlock(back::Program, UniformBufferSlot, UniformBlockIndex, back::Buffer),
337+
BindUniformBlock(back::Program, UniformBufferSlot, UniformBlockIndex, B),
338338
BindUniform(shade::Location, shade::UniformValue),
339339
BindTexture(TextureSlot, tex::TextureKind, back::Texture, Option<SamplerHandle>),
340340
SetDrawColorBuffers(usize),
@@ -345,7 +345,7 @@ pub enum Command {
345345
SetDepthStencilState(Option<state::Depth>, Option<state::Stencil>, state::CullMode),
346346
SetBlendState(Option<state::Blend>),
347347
SetColorMask(state::ColorMask),
348-
UpdateBuffer(back::Buffer, draw::DataPointer, usize),
348+
UpdateBuffer(B, draw::DataPointer, usize),
349349
UpdateTexture(tex::TextureKind, back::Texture, tex::ImageInfo, draw::DataPointer),
350350
// drawing
351351
Clear(target::ClearData, target::Mask),
@@ -357,8 +357,10 @@ pub enum Command {
357357
/// An interface for performing draw calls using a specific graphics API
358358
#[allow(missing_docs)]
359359
pub trait Device {
360-
361-
type CommandBuffer: draw::CommandBuffer;
360+
/// Raw buffer
361+
type Buffer;
362+
/// Command buffer to collect calls and data
363+
type CommandBuffer: draw::CommandBuffer<Self::Buffer>;
362364

363365
/// Returns the capabilities available to the specific API implementation
364366
fn get_capabilities<'a>(&'a self) -> &'a Capabilities;
@@ -368,12 +370,16 @@ pub trait Device {
368370
fn submit(&mut self, buffer: (&Self::CommandBuffer, &draw::DataBuffer));
369371

370372
// resource creation
371-
fn create_buffer_raw(&mut self, size: usize, usage: BufferUsage) -> BufferHandle<()>;
372-
fn create_buffer<T>(&mut self, num: usize, usage: BufferUsage) -> BufferHandle<T> {
373+
fn create_buffer_raw(&mut self, size: usize, usage: BufferUsage)
374+
-> BufferHandle<Self::Buffer, ()>;
375+
fn create_buffer<T>(&mut self, num: usize, usage: BufferUsage)
376+
-> BufferHandle<Self::Buffer, T> {
373377
self.create_buffer_raw(num * mem::size_of::<T>(), usage).cast()
374378
}
375-
fn create_buffer_static_raw(&mut self, data: &[u8]) -> BufferHandle<()>;
376-
fn create_buffer_static<T: Copy>(&mut self, data: &[T]) -> BufferHandle<T> {
379+
fn create_buffer_static_raw(&mut self, data: &[u8])
380+
-> BufferHandle<Self::Buffer, ()>;
381+
fn create_buffer_static<T: Copy>(&mut self, data: &[T])
382+
-> BufferHandle<Self::Buffer, T> {
377383
self.create_buffer_static_raw(as_byte_slice(data)).cast()
378384
}
379385
fn create_array_buffer(&mut self) -> Result<ArrayBufferHandle, ()>;
@@ -387,8 +393,8 @@ pub trait Device {
387393
fn create_sampler(&mut self, info: tex::SamplerInfo) -> SamplerHandle;
388394

389395
// resource deletion
390-
fn delete_buffer_raw(&mut self, buf: BufferHandle<()>);
391-
fn delete_buffer<T>(&mut self, buf: BufferHandle<T>) {
396+
fn delete_buffer_raw(&mut self, buf: BufferHandle<Self::Buffer, ()>);
397+
fn delete_buffer<T>(&mut self, buf: BufferHandle<Self::Buffer, T>) {
392398
self.delete_buffer_raw(buf.cast());
393399
}
394400
fn delete_shader(&mut self, ShaderHandle);
@@ -398,17 +404,27 @@ pub trait Device {
398404
fn delete_sampler(&mut self, SamplerHandle);
399405

400406
/// Update the information stored in a specific buffer
401-
fn update_buffer_raw(&mut self, buf: BufferHandle<()>, data: &[u8],
407+
fn update_buffer_raw(&mut self, buf: BufferHandle<Self::Buffer, ()>, data: &[u8],
402408
offset_bytes: usize);
403-
fn update_buffer<T: Copy>(&mut self, buf: BufferHandle<T>, data: &[T],
404-
offset_elements: usize) {
405-
self.update_buffer_raw(buf.cast(), as_byte_slice(data), mem::size_of::<T>() * offset_elements)
409+
fn update_buffer<T: Copy>(&mut self, buf: BufferHandle<Self::Buffer, T>,
410+
data: &[T], offset_elements: usize) {
411+
self.update_buffer_raw(
412+
buf.cast(),
413+
as_byte_slice(data),
414+
mem::size_of::<T>() * offset_elements
415+
)
406416
}
407-
fn map_buffer_raw(&mut self, buf: BufferHandle<()>, access: MapAccess) -> back::RawMapping;
417+
fn map_buffer_raw(&mut self, buf: BufferHandle<Self::Buffer, ()>,
418+
access: MapAccess) -> back::RawMapping;
408419
fn unmap_buffer_raw(&mut self, map: back::RawMapping);
409-
fn map_buffer_readable<T: Copy>(&mut self, buf: BufferHandle<T>) -> ReadableMapping<T, Self>;
410-
fn map_buffer_writable<T: Copy>(&mut self, buf: BufferHandle<T>) -> WritableMapping<T, Self>;
411-
fn map_buffer_rw<T: Copy>(&mut self, buf: BufferHandle<T>) -> RWMapping<T, Self>;
420+
fn map_buffer_readable<T: Copy>(&mut self,
421+
buf: BufferHandle<Self::Buffer, T>)
422+
-> ReadableMapping<T, Self>;
423+
fn map_buffer_writable<T: Copy>(&mut self,
424+
buf: BufferHandle<Self::Buffer, T>)
425+
-> WritableMapping<T, Self>;
426+
fn map_buffer_rw<T: Copy>(&mut self, buf: BufferHandle<Self::Buffer, T>)
427+
-> RWMapping<T, Self>;
412428

413429
/// Update the information stored in a texture
414430
fn update_texture_raw(&mut self, tex: &TextureHandle, img: &tex::ImageInfo,
@@ -427,7 +443,8 @@ mod test {
427443
use super::{BufferHandle, Handle};
428444
use super::{BufferInfo, BufferUsage};
429445

430-
fn mock_buffer<T>(usage: BufferUsage, len: usize) -> BufferHandle<T> {
446+
fn mock_buffer<T>(usage: BufferUsage, len: usize)
447+
-> BufferHandle<Self::Buffer, T> {
431448
BufferHandle {
432449
raw: Handle(
433450
0,

0 commit comments

Comments
 (0)