Skip to content

Commit

Permalink
Parameterise handles over Device
Browse files Browse the repository at this point in the history
For now we just use `GlDevice` when we refer to them
  • Loading branch information
brendanzab committed Feb 17, 2015
1 parent 6337291 commit c25ef1d
Show file tree
Hide file tree
Showing 15 changed files with 199 additions and 169 deletions.
13 changes: 8 additions & 5 deletions examples/deferred/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ struct LightParams {
#[name = "u_Transform"]
transform: [[f32; 4]; 4],
#[name = "u_LightPosBlock"]
light_pos_buf: gfx::RawBufferHandle,
light_pos_buf: gfx::RawBufferHandle<gfx::GlDevice>,
#[name = "u_Radius"]
radius: f32,
#[name = "u_CameraPos"]
Expand All @@ -121,7 +121,7 @@ struct EmitterParams {
#[name = "u_Transform"]
transform: [[f32; 4]; 4],
#[name = "u_LightPosBlock"]
light_pos_buf: gfx::RawBufferHandle,
light_pos_buf: gfx::RawBufferHandle<gfx::GlDevice>,
#[name = "u_Radius"]
radius: f32,
}
Expand Down Expand Up @@ -307,7 +307,9 @@ fn calculate_color(height: f32) -> [f32; 3] {
}
}

fn create_g_buffer(width: u16, height: u16, device: &mut gfx::GlDevice) -> (gfx::Frame, TextureHandle, TextureHandle, TextureHandle, TextureHandle) {
fn create_g_buffer(width: u16, height: u16, device: &mut gfx::GlDevice)
-> (gfx::Frame, TextureHandle<gfx::GlDevice>, TextureHandle<gfx::GlDevice>,
TextureHandle<gfx::GlDevice>, TextureHandle<gfx::GlDevice>) {
let mut frame = gfx::Frame::new(width, height);

let texture_info_float = gfx::tex::TextureInfo {
Expand Down Expand Up @@ -343,7 +345,8 @@ fn create_g_buffer(width: u16, height: u16, device: &mut gfx::GlDevice) -> (gfx:
(frame, texture_pos, texture_normal, texture_diffuse, texture_depth)
}

fn create_res_buffer(width: u16, height: u16, device: &mut gfx::GlDevice, texture_depth: TextureHandle) -> (gfx::Frame, TextureHandle, TextureHandle) {
fn create_res_buffer(width: u16, height: u16, device: &mut gfx::GlDevice, texture_depth: TextureHandle<gfx::GlDevice>)
-> (gfx::Frame, TextureHandle<gfx::GlDevice>, TextureHandle<gfx::GlDevice>) {
let mut frame = gfx::Frame::new(width, height);

let texture_info_float = gfx::tex::TextureInfo {
Expand Down Expand Up @@ -567,7 +570,7 @@ fn main() {
tex: (texture_pos, Some(sampler)),
};

let mut debug_buf: Option<TextureHandle> = None;
let mut debug_buf: Option<TextureHandle<gfx::GlDevice>> = None;

let mut light_pos_vec: Vec<[f32; 4]> = (0 ..NUM_LIGHTS).map(|_| {
[0.0, 0.0, 0.0, 0.0]
Expand Down
18 changes: 10 additions & 8 deletions src/device/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

//! Command Buffer device interface
use std::fmt;

use attrib;
use back;
use shade;
Expand Down Expand Up @@ -77,13 +79,13 @@ impl DataBuffer {
/// An interface of the abstract command buffer. It collects commands in an
/// efficient API-specific manner, to be ready for execution on the device.
pub trait CommandBuffer {
type Buffer;
type ArrayBuffer;
type Program;
type FrameBuffer;
type Surface;
type Texture;
type Sampler;
type Buffer: Copy + fmt::Debug + PartialEq + Clone;
type ArrayBuffer: Copy + fmt::Debug + PartialEq + Clone;
type Program: Copy + fmt::Debug + PartialEq + Clone;
type FrameBuffer: Copy + fmt::Debug + PartialEq + Clone;
type Surface: Copy + fmt::Debug + PartialEq + Clone;
type Texture: Copy + fmt::Debug + PartialEq + Clone;
type Sampler: Copy + fmt::Debug + PartialEq + Clone;

/// An empty constructor
fn new() -> Self;
Expand Down Expand Up @@ -113,7 +115,7 @@ pub trait CommandBuffer {
fn bind_uniform(&mut self, shade::Location, shade::UniformValue);
/// Bind a texture
fn bind_texture(&mut self, ::TextureSlot, tex::TextureKind, back::Texture,
Option<::SamplerHandle>);
Option<::SamplerHandle<back::GlDevice>>);
/// Select, which color buffers are going to be targetted by the shader
fn set_draw_color_buffers(&mut self, usize);
/// Set primitive topology
Expand Down
6 changes: 3 additions & 3 deletions src/device/gl_device/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use std::slice;

use {attrib, draw, target, tex, shade, state};
use {attrib, back, draw, target, tex, shade, state};
use {AttributeSlot, IndexType, InstanceCount, PrimitiveType, TextureSlot, UniformBlockIndex, UniformBufferSlot, VertexCount};
use super::{ArrayBuffer, Buffer, FrameBuffer, Program, Sampler, Surface, Texture};

Expand All @@ -33,7 +33,7 @@ pub enum Command {
BindTargetTexture(target::Access, target::Target, Texture, target::Level, Option<target::Layer>),
BindUniformBlock(Program, UniformBufferSlot, UniformBlockIndex, Buffer),
BindUniform(shade::Location, shade::UniformValue),
BindTexture(TextureSlot, tex::TextureKind, Texture, Option<::SamplerHandle>),
BindTexture(TextureSlot, tex::TextureKind, Texture, Option<::SamplerHandle<back::GlDevice>>),
SetDrawColorBuffers(usize),
SetPrimitiveState(state::Primitive),
SetViewport(target::Rect),
Expand Down Expand Up @@ -125,7 +125,7 @@ impl draw::CommandBuffer for CommandBuffer {
self.buf.push(Command::BindUniform(loc, value));
}
fn bind_texture(&mut self, slot: ::TextureSlot, kind: ::tex::TextureKind,
tex: Texture, sampler: Option<::SamplerHandle>) {
tex: Texture, sampler: Option<::SamplerHandle<back::GlDevice>>) {
self.buf.push(Command::BindTexture(slot, kind, tex, sampler));
}

Expand Down
90 changes: 42 additions & 48 deletions src/device/gl_device/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,29 +80,6 @@ impl GlError {
}
}

static RESET_CB: &'static [Command] = &[
Command::BindProgram(0),
Command::BindArrayBuffer(0),
//BindAttribute
Command::BindIndex(0),
Command::BindFrameBuffer(Access::Draw, 0),
Command::BindFrameBuffer(Access::Read, 0),
//UnbindTarget
//BindUniformBlock
//BindUniform
//BindTexture
Command::SetPrimitiveState(::state::Primitive {
front_face: WindingOrder::CounterClockwise,
method: RasterMethod::Fill(CullMode::Back),
offset: None,
}),
Command::SetViewport(::target::Rect{x: 0, y: 0, w: 0, h: 0}),
Command::SetScissor(None),
Command::SetDepthStencilState(None, None, CullMode::Nothing),
Command::SetBlendState(None),
Command::SetColorMask(::state::MASK_ALL),
];

fn primitive_to_gl(prim_type: ::PrimitiveType) -> gl::types::GLenum {
match prim_type {
PrimitiveType::Point => gl::POINTS,
Expand Down Expand Up @@ -582,9 +559,26 @@ impl Device for GlDevice {

fn reset_state(&mut self) {
let data = ::draw::DataBuffer::new();
for com in RESET_CB.iter() {
self.process(com, &data);
}
self.process(&Command::BindProgram(0), &data);
self.process(&Command::BindArrayBuffer(0), &data);
// self.process(&command::BindAttribute, &data);
self.process(&Command::BindIndex(0), &data);
self.process(&Command::BindFrameBuffer(Access::Draw, 0), &data);
self.process(&Command::BindFrameBuffer(Access::Read, 0), &data);
// self.process(&command::UnbindTarget, &data);
// self.process(&command::BindUniformBlock, &data);
// self.process(&command::BindUniform, &data);
// self.process(&command::BindTexture, &data);
self.process(&Command::SetPrimitiveState(::state::Primitive {
front_face: WindingOrder::CounterClockwise,
method: RasterMethod::Fill(CullMode::Back),
offset: None,
}), &data);
self.process(&Command::SetViewport(::target::Rect{x: 0, y: 0, w: 0, h: 0}), &data);
self.process(&Command::SetScissor(None), &data);
self.process(&Command::SetDepthStencilState(None, None, CullMode::Nothing), &data);
self.process(&Command::SetBlendState(None), &data);
self.process(&Command::SetColorMask(::state::MASK_ALL), &data);
}

fn submit(&mut self, (cb, db): (&CommandBuffer, &::draw::DataBuffer)) {
Expand All @@ -594,7 +588,7 @@ impl Device for GlDevice {
}
}

fn create_buffer_raw(&mut self, size: usize, usage: BufferUsage) -> ::BufferHandle<()> {
fn create_buffer_raw(&mut self, size: usize, usage: BufferUsage) -> ::BufferHandle<GlDevice, ()> {
let name = self.create_buffer_internal();
let info = ::BufferInfo {
usage: usage,
Expand All @@ -604,7 +598,7 @@ impl Device for GlDevice {
::BufferHandle::from_raw(::Handle(name, info))
}

fn create_buffer_static_raw(&mut self, data: &[u8]) -> ::BufferHandle<()> {
fn create_buffer_static_raw(&mut self, data: &[u8]) -> ::BufferHandle<GlDevice, ()> {
let name = self.create_buffer_internal();

let info = ::BufferInfo {
Expand All @@ -616,7 +610,7 @@ impl Device for GlDevice {
::BufferHandle::from_raw(::Handle(name, info))
}

fn create_array_buffer(&mut self) -> Result<::ArrayBufferHandle, ()> {
fn create_array_buffer(&mut self) -> Result<::ArrayBufferHandle<GlDevice>, ()> {
if self.caps.array_buffer_supported {
let mut name = 0 as ArrayBuffer;
unsafe {
Expand All @@ -631,7 +625,7 @@ impl Device for GlDevice {
}

fn create_shader(&mut self, stage: ::shade::Stage, code: &[u8])
-> Result<::ShaderHandle, ::shade::CreateShaderError> {
-> Result<::ShaderHandle<GlDevice>, ::shade::CreateShaderError> {
let (name, info) = shade::create_shader(&self.gl, stage, code);
info.map(|info| {
let level = if name.is_err() { LogLevel::Error } else { LogLevel::Warn };
Expand All @@ -640,7 +634,7 @@ impl Device for GlDevice {
name.map(|sh| ::Handle(sh, stage))
}

fn create_program(&mut self, shaders: &[::ShaderHandle], targets: Option<&[&str]>) -> Result<::ProgramHandle, ()> {
fn create_program(&mut self, shaders: &[::ShaderHandle<GlDevice>], targets: Option<&[&str]>) -> Result<::ProgramHandle<GlDevice>, ()> {
let (prog, log) = shade::create_program(&self.gl, &self.caps, shaders, targets);
log.map(|log| {
let level = if prog.is_err() { LogLevel::Error } else { LogLevel::Warn };
Expand All @@ -649,7 +643,7 @@ impl Device for GlDevice {
prog
}

fn create_frame_buffer(&mut self) -> ::FrameBufferHandle {
fn create_frame_buffer(&mut self) -> ::FrameBufferHandle<GlDevice> {
if !self.caps.render_targets_supported {
panic!("No framebuffer objects, can't make a new one!");
}
Expand All @@ -663,12 +657,12 @@ impl Device for GlDevice {
}

fn create_surface(&mut self, info: ::tex::SurfaceInfo) ->
Result<::SurfaceHandle, ::tex::SurfaceError> {
Result<::SurfaceHandle<GlDevice>, ::tex::SurfaceError> {
tex::make_surface(&self.gl, &info).map(|suf| ::Handle(suf, info))
}

fn create_texture(&mut self, info: ::tex::TextureInfo) ->
Result<::TextureHandle, ::tex::TextureError> {
Result<::TextureHandle<GlDevice>, ::tex::TextureError> {
if info.width == 0 || info.height == 0 || info.levels == 0 {
return Err(::tex::TextureError::InvalidTextureInfo(info))
}
Expand All @@ -681,7 +675,7 @@ impl Device for GlDevice {
name.map(|tex| ::Handle(tex, info))
}

fn create_sampler(&mut self, info: ::tex::SamplerInfo) -> ::SamplerHandle {
fn create_sampler(&mut self, info: ::tex::SamplerInfo) -> ::SamplerHandle<GlDevice> {
let sam = if self.caps.sampler_objects_supported {
tex::make_sampler(&self.gl, &info)
} else {
Expand All @@ -690,61 +684,61 @@ impl Device for GlDevice {
::Handle(sam, info)
}

fn delete_buffer_raw(&mut self, handle: ::BufferHandle<()>) {
fn delete_buffer_raw(&mut self, handle: ::BufferHandle<GlDevice, ()>) {
let name = handle.get_name();
unsafe {
self.gl.DeleteBuffers(1, &name);
}
}

fn delete_shader(&mut self, handle: ::ShaderHandle) {
fn delete_shader(&mut self, handle: ::ShaderHandle<GlDevice>) {
unsafe { self.gl.DeleteShader(handle.get_name()) };
}

fn delete_program(&mut self, handle: ::ProgramHandle) {
fn delete_program(&mut self, handle: ::ProgramHandle<GlDevice>) {
unsafe { self.gl.DeleteProgram(handle.get_name()) };
}

fn delete_surface(&mut self, handle: ::SurfaceHandle) {
fn delete_surface(&mut self, handle: ::SurfaceHandle<GlDevice>) {
let name = handle.get_name();
unsafe {
self.gl.DeleteRenderbuffers(1, &name);
}
}

fn delete_texture(&mut self, handle: ::TextureHandle) {
fn delete_texture(&mut self, handle: ::TextureHandle<GlDevice>) {
let name = handle.get_name();
unsafe {
self.gl.DeleteTextures(1, &name);
}
}

fn delete_sampler(&mut self, handle: ::SamplerHandle) {
fn delete_sampler(&mut self, handle: ::SamplerHandle<GlDevice>) {
let name = handle.get_name();
unsafe {
self.gl.DeleteSamplers(1, &name);
}
}

fn update_buffer_raw(&mut self, buffer: ::BufferHandle<()>, data: &[u8],
fn update_buffer_raw(&mut self, buffer: ::BufferHandle<GlDevice, ()>, data: &[u8],
offset_bytes: usize) {
debug_assert!(offset_bytes + data.len() <= buffer.get_info().size);
self.update_sub_buffer(buffer.get_name(), data.as_ptr(), data.len(),
offset_bytes)
}

fn update_texture_raw(&mut self, texture: &::TextureHandle,
fn update_texture_raw(&mut self, texture: &::TextureHandle<GlDevice>,
img: &::tex::ImageInfo, data: &[u8])
-> Result<(), ::tex::TextureError> {
tex::update_texture(&self.gl, texture.get_info().kind,
texture.get_name(), img, data.as_ptr(), data.len())
}

fn generate_mipmap(&mut self, texture: &::TextureHandle) {
fn generate_mipmap(&mut self, texture: &::TextureHandle<GlDevice>) {
tex::generate_mipmap(&self.gl, texture.get_info().kind, texture.get_name());
}

fn map_buffer_raw(&mut self, buf: BufferHandle<()>, access: MapAccess) -> RawMapping {
fn map_buffer_raw(&mut self, buf: BufferHandle<GlDevice, ()>, access: MapAccess) -> RawMapping {
let ptr;
unsafe { self.gl.BindBuffer(gl::ARRAY_BUFFER, buf.get_name()) };
ptr = unsafe { self.gl.MapBuffer(gl::ARRAY_BUFFER, match access {
Expand All @@ -762,7 +756,7 @@ impl Device for GlDevice {
unsafe { self.gl.UnmapBuffer(map.target) };
}

fn map_buffer_readable<T: Copy>(&mut self, buf: BufferHandle<T>) -> ReadableMapping<T, GlDevice> {
fn map_buffer_readable<T: Copy>(&mut self, buf: BufferHandle<GlDevice, T>) -> ReadableMapping<T, GlDevice> {
let map = self.map_buffer_raw(buf.cast(), MapAccess::Readable);
ReadableMapping {
raw: map,
Expand All @@ -771,7 +765,7 @@ impl Device for GlDevice {
}
}

fn map_buffer_writable<T: Copy>(&mut self, buf: BufferHandle<T>) -> WritableMapping<T, GlDevice> {
fn map_buffer_writable<T: Copy>(&mut self, buf: BufferHandle<GlDevice, T>) -> WritableMapping<T, GlDevice> {
let map = self.map_buffer_raw(buf.cast(), MapAccess::Writable);
WritableMapping {
raw: map,
Expand All @@ -780,7 +774,7 @@ impl Device for GlDevice {
}
}

fn map_buffer_rw<T: Copy>(&mut self, buf: BufferHandle<T>) -> RWMapping<T, GlDevice> {
fn map_buffer_rw<T: Copy>(&mut self, buf: BufferHandle<GlDevice, T>) -> RWMapping<T, GlDevice> {
let map = self.map_buffer_raw(buf.cast(), MapAccess::RW);
RWMapping {
raw: map,
Expand Down
4 changes: 2 additions & 2 deletions src/device/gl_device/shade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,8 @@ fn query_parameters(gl: &gl::Gl, caps: &::Capabilities, prog: super::Program) ->
(uniforms, textures)
}

pub fn create_program(gl: &gl::Gl, caps: &::Capabilities, shaders: &[::ShaderHandle], targets: Option<&[&str]>)
-> (Result<::ProgramHandle, ()>, Option<String>) {
pub fn create_program(gl: &gl::Gl, caps: &::Capabilities, shaders: &[::ShaderHandle<super::GlDevice>], targets: Option<&[&str]>)
-> (Result<::ProgramHandle<super::GlDevice>, ()>, Option<String>) {
let name = unsafe { gl.CreateProgram() };
for sh in shaders.iter() {
unsafe { gl.AttachShader(name, sh.get_name()) };
Expand Down
Loading

0 comments on commit c25ef1d

Please sign in to comment.