Skip to content

Commit

Permalink
Add Resources trait for common resource types
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanzab committed Feb 17, 2015
1 parent 28f69e1 commit af64f3e
Show file tree
Hide file tree
Showing 15 changed files with 161 additions and 173 deletions.
14 changes: 7 additions & 7 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<gfx::GlDevice>,
light_pos_buf: gfx::RawBufferHandle<gfx::GlResources>,
#[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<gfx::GlDevice>,
light_pos_buf: gfx::RawBufferHandle<gfx::GlResources>,
#[name = "u_Radius"]
radius: f32,
}
Expand Down Expand Up @@ -308,8 +308,8 @@ fn calculate_color(height: f32) -> [f32; 3] {
}

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>) {
-> (gfx::Frame, TextureHandle<gfx::GlResources>, TextureHandle<gfx::GlResources>,
TextureHandle<gfx::GlResources>, TextureHandle<gfx::GlResources>) {
let mut frame = gfx::Frame::new(width, height);

let texture_info_float = gfx::tex::TextureInfo {
Expand Down Expand Up @@ -345,8 +345,8 @@ fn create_g_buffer(width: u16, height: u16, device: &mut gfx::GlDevice)
(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::GlDevice>)
-> (gfx::Frame, TextureHandle<gfx::GlDevice>, TextureHandle<gfx::GlDevice>) {
fn create_res_buffer(width: u16, height: u16, device: &mut gfx::GlDevice, texture_depth: TextureHandle<gfx::GlResources>)
-> (gfx::Frame, TextureHandle<gfx::GlResources>, TextureHandle<gfx::GlResources>) {
let mut frame = gfx::Frame::new(width, height);

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

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

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

//! Command Buffer device interface

use std::fmt;

use attrib;
use back;
use shade;
use target;
use tex;
use Resources;

type Offset = u32;
type Size = u32;
Expand Down Expand Up @@ -79,13 +78,7 @@ 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: 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;
type Resources: Resources;

/// An empty constructor
fn new() -> Self;
Expand Down Expand Up @@ -115,7 +108,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<back::GlDevice>>);
Option<::SamplerHandle<back::GlResources>>);
/// 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
14 changes: 4 additions & 10 deletions src/device/gl_device/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::slice;

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};
use super::{ArrayBuffer, Buffer, FrameBuffer, Program, Surface, Texture};

/// Serialized device command.
#[derive(Copy, Debug)]
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<back::GlDevice>>),
BindTexture(TextureSlot, tex::TextureKind, Texture, Option<::SamplerHandle<back::GlResources>>),
SetDrawColorBuffers(usize),
SetPrimitiveState(state::Primitive),
SetViewport(target::Rect),
Expand Down Expand Up @@ -62,13 +62,7 @@ impl CommandBuffer {
}

impl draw::CommandBuffer for CommandBuffer {
type Buffer = Buffer;
type ArrayBuffer = ArrayBuffer;
type Program = Program;
type FrameBuffer = FrameBuffer;
type Surface = Surface;
type Texture = Texture;
type Sampler = Sampler;
type Resources = super::GlResources;

fn new() -> CommandBuffer {
CommandBuffer {
Expand Down Expand Up @@ -125,7 +119,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<back::GlDevice>>) {
tex: Texture, sampler: Option<::SamplerHandle<back::GlResources>>) {
self.buf.push(Command::BindTexture(slot, kind, tex, sampler));
}

Expand Down
70 changes: 38 additions & 32 deletions src/device/gl_device/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use state::{CullMode, RasterMethod, WindingOrder};
use target::{Access, Target};

use BufferUsage;
use Device;
use {Device, Resources};
use {MapAccess, ReadableMapping, WritableMapping, RWMapping, BufferHandle, PrimitiveType};
use self::draw::{Command, CommandBuffer};
pub use self::info::{Info, PlatformName, Version};
Expand All @@ -55,6 +55,20 @@ pub type Surface = gl::types::GLuint;
pub type Sampler = gl::types::GLuint;
pub type Texture = gl::types::GLuint;

#[derive(Copy)]
pub enum GlResources {}

impl Resources for GlResources {
type Buffer = Buffer;
type ArrayBuffer = ArrayBuffer;
type Shader = Shader;
type Program = Program;
type FrameBuffer = FrameBuffer;
type Surface = Surface;
type Texture = Texture;
type Sampler = Sampler;
}

#[derive(Copy, Eq, PartialEq, Debug)]
pub enum GlError {
NoError,
Expand Down Expand Up @@ -542,17 +556,9 @@ impl GlDevice {
}

impl Device for GlDevice {
type Resources = GlResources;
type CommandBuffer = CommandBuffer;

type Buffer = Buffer;
type ArrayBuffer = ArrayBuffer;
type Shader = Shader;
type Program = Program;
type FrameBuffer = FrameBuffer;
type Surface = Surface;
type Texture = Texture;
type Sampler = Sampler;

fn get_capabilities<'a>(&'a self) -> &'a ::Capabilities {
&self.caps
}
Expand Down Expand Up @@ -588,7 +594,7 @@ impl Device for GlDevice {
}
}

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

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

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

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

fn create_shader(&mut self, stage: ::shade::Stage, code: &[u8])
-> Result<::ShaderHandle<GlDevice>, ::shade::CreateShaderError> {
-> Result<::ShaderHandle<GlResources>, ::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 @@ -634,7 +640,7 @@ impl Device for GlDevice {
name.map(|sh| ::Handle(sh, stage))
}

fn create_program(&mut self, shaders: &[::ShaderHandle<GlDevice>], targets: Option<&[&str]>) -> Result<::ProgramHandle<GlDevice>, ()> {
fn create_program(&mut self, shaders: &[::ShaderHandle<GlResources>], targets: Option<&[&str]>) -> Result<::ProgramHandle<GlResources>, ()> {
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 @@ -643,7 +649,7 @@ impl Device for GlDevice {
prog
}

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

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

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

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

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

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

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

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

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

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

fn update_buffer_raw(&mut self, buffer: ::BufferHandle<GlDevice, ()>, data: &[u8],
fn update_buffer_raw(&mut self, buffer: ::BufferHandle<GlResources, ()>, 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<GlDevice>,
fn update_texture_raw(&mut self, texture: &::TextureHandle<GlResources>,
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<GlDevice>) {
fn generate_mipmap(&mut self, texture: &::TextureHandle<GlResources>) {
tex::generate_mipmap(&self.gl, texture.get_info().kind, texture.get_name());
}

fn map_buffer_raw(&mut self, buf: BufferHandle<GlDevice, ()>, access: MapAccess) -> RawMapping {
fn map_buffer_raw(&mut self, buf: BufferHandle<GlResources, ()>, 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 @@ -756,7 +762,7 @@ impl Device for GlDevice {
unsafe { self.gl.UnmapBuffer(map.target) };
}

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

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

fn map_buffer_rw<T: Copy>(&mut self, buf: BufferHandle<GlDevice, T>) -> RWMapping<T, GlDevice> {
fn map_buffer_rw<T: Copy>(&mut self, buf: BufferHandle<GlResources, 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<super::GlDevice>], targets: Option<&[&str]>)
-> (Result<::ProgramHandle<super::GlDevice>, ()>, Option<String>) {
pub fn create_program(gl: &gl::Gl, caps: &::Capabilities, shaders: &[::ShaderHandle<super::GlResources>], targets: Option<&[&str]>)
-> (Result<::ProgramHandle<super::GlResources>, ()>, 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 af64f3e

Please sign in to comment.