Skip to content

Decouple most of the types from GlDevice and GlResources #593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/cube/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct Params {
transform: [[f32; 4]; 4],

#[name = "t_Color"]
color: gfx::shade::TextureParam,
color: gfx::shade::TextureParam<gfx::GlResources>,
}

static VERTEX_SRC: &'static [u8] = b"
Expand Down Expand Up @@ -174,7 +174,7 @@ fn main() {

let state = gfx::DrawState::new().depth(gfx::state::Comparison::LessEqual, true);

let batch: RefBatch<Params, gfx::GlResources> = {
let batch: RefBatch<Params> = {
context.make_batch(&program, &mesh, slice, &state)
.ok().expect("Failed to make batch.")
};
Expand Down
16 changes: 8 additions & 8 deletions examples/deferred/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ struct LightParams {
#[name = "u_FrameRes"]
frame_res: [f32; 2],
#[name = "u_TexPos"]
tex_pos: gfx::shade::TextureParam,
tex_pos: gfx::shade::TextureParam<gfx::GlResources>,
#[name = "u_TexNormal"]
tex_normal: gfx::shade::TextureParam,
tex_normal: gfx::shade::TextureParam<gfx::GlResources>,
#[name = "u_TexDiffuse"]
tex_diffuse: gfx::shade::TextureParam,
tex_diffuse: gfx::shade::TextureParam<gfx::GlResources>,
}

#[shader_param]
Expand All @@ -129,7 +129,7 @@ struct EmitterParams {
#[shader_param]
struct BlitParams {
#[name = "u_Tex"]
tex: gfx::shade::TextureParam,
tex: gfx::shade::TextureParam<gfx::GlResources>,
}

static TERRAIN_VERTEX_SRC: &'static [u8] = b"
Expand Down Expand Up @@ -402,7 +402,7 @@ fn main() {
};

let terrain_scale = Vector3::new(25.0, 25.0, 25.0);
let terrain_batch: RefBatch<TerrainParams, gfx::GlResources> = {
let terrain_batch: RefBatch<TerrainParams> = {
let plane = genmesh::generators::Plane::subdivide(256, 256);
let vertex_data: Vec<TerrainVertex> = plane.shared_vertex_iter()
.map(|(x, y)| {
Expand Down Expand Up @@ -435,7 +435,7 @@ fn main() {
.ok().expect("Failed to match back")
};

let blit_batch: RefBatch<BlitParams, gfx::GlResources> = {
let blit_batch: RefBatch<BlitParams> = {
let vertex_data = [
BlitVertex { pos: [-1, -1, 0], tex_coord: [0, 0] },
BlitVertex { pos: [ 1, -1, 0], tex_coord: [1, 0] },
Expand Down Expand Up @@ -507,15 +507,15 @@ fn main() {
.depth(gfx::state::Comparison::LessEqual, false)
.blend(gfx::BlendPreset::Additive);

let light_batch: RefBatch<LightParams, gfx::GlResources> = {
let light_batch: RefBatch<LightParams> = {
let program = device.link_program(LIGHT_VERTEX_SRC, LIGHT_FRAGMENT_SRC)
.ok().expect("Failed to link program.");

context.make_batch(&program, &mesh, slice, &state)
.ok().expect("Failed to create batch")
};

let emitter_batch: RefBatch<EmitterParams, gfx::GlResources> = {
let emitter_batch: RefBatch<EmitterParams> = {
let program = device.link_program(EMITTER_VERTEX_SRC, EMITTER_FRAGMENT_SRC)
.ok().expect("Failed to link program.");

Expand Down
2 changes: 1 addition & 1 deletion examples/performance/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ fn gfx_main(mut glfw: glfw::Glfw,
};

let mut graphics = gfx::Graphics::new(device);
let batch: RefBatch<Params, gfx::GlResources> = {
let batch: RefBatch<Params> = {
graphics.make_batch(&program, &mesh, slice, &state)
.ok().expect("Failed to make batch")
};
Expand Down
2 changes: 1 addition & 1 deletion examples/terrain/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ fn main() {
let state = gfx::DrawState::new().depth(gfx::state::Comparison::LessEqual, true);

let mut graphics = gfx::Graphics::new(device);
let batch: RefBatch<Params, gfx::GlResources> = {
let batch: RefBatch<Params> = {
graphics.make_batch(&program, &mesh, slice, &state)
.ok().expect("Failed to make batch.")
};
Expand Down
2 changes: 1 addition & 1 deletion examples/triangle/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ fn main() {

renderer.reset();
renderer.clear(clear_data, gfx::COLOR, &frame);
renderer.draw(&(&mesh, slice.clone(), &program, &(), &state), &frame).unwrap();
renderer.draw(&(&mesh, slice.clone(), &program, &None, &state), &frame).unwrap();
device.submit(renderer.as_buffer());

window.swap_buffers();
Expand Down
27 changes: 13 additions & 14 deletions src/device/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
//! Command Buffer device interface

use attrib;
use back;
use shade;
use target;
use tex;
Expand Down Expand Up @@ -85,30 +84,30 @@ pub trait CommandBuffer {
/// Clear the command buffer contents, retain the allocated storage
fn clear(&mut self);
/// Bind a shader program
fn bind_program(&mut self, back::Program);
fn bind_program(&mut self, <Self::Resources as Resources>::Program);
/// Bind an array buffer object
fn bind_array_buffer(&mut self, back::ArrayBuffer);
fn bind_array_buffer(&mut self, <Self::Resources as Resources>::ArrayBuffer);
/// Bind a vertex attribute
fn bind_attribute(&mut self, ::AttributeSlot, back::Buffer, attrib::Format);
fn bind_attribute(&mut self, ::AttributeSlot, <Self::Resources as Resources>::Buffer, attrib::Format);
/// Bind an index buffer
fn bind_index(&mut self, back::Buffer);
fn bind_index(&mut self, <Self::Resources as Resources>::Buffer);
/// Bind a frame buffer object
fn bind_frame_buffer(&mut self, target::Access, back::FrameBuffer);
fn bind_frame_buffer(&mut self, target::Access, <Self::Resources as Resources>::FrameBuffer);
/// Unbind any surface from the specified target slot
fn unbind_target(&mut self, target::Access, target::Target);
/// Bind a surface to the specified target slot
fn bind_target_surface(&mut self, target::Access, target::Target, back::Surface);
fn bind_target_surface(&mut self, target::Access, target::Target, <Self::Resources as Resources>::Surface);
/// Bind a level of the texture to the specified target slot
fn bind_target_texture(&mut self, target::Access, target::Target, back::Texture,
fn bind_target_texture(&mut self, target::Access, target::Target, <Self::Resources as Resources>::Texture,
target::Level, Option<target::Layer>);
/// Bind a uniform block
fn bind_uniform_block(&mut self, back::Program, ::UniformBufferSlot,
::UniformBlockIndex, back::Buffer);
fn bind_uniform_block(&mut self, <Self::Resources as Resources>::Program, ::UniformBufferSlot,
::UniformBlockIndex, <Self::Resources as Resources>::Buffer);
/// Bind a single uniform in the default block
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::GlResources>>);
fn bind_texture(&mut self, ::TextureSlot, tex::TextureKind, <Self::Resources as Resources>::Texture,
Option<::SamplerHandle<Self::Resources>>);
/// Select, which color buffers are going to be targetted by the shader
fn set_draw_color_buffers(&mut self, usize);
/// Set primitive topology
Expand All @@ -127,9 +126,9 @@ pub trait CommandBuffer {
/// Set output color mask for all targets
fn set_color_mask(&mut self, ::state::ColorMask);
/// Update a vertex/index/uniform buffer
fn update_buffer(&mut self, back::Buffer, DataPointer, usize);
fn update_buffer(&mut self, <Self::Resources as Resources>::Buffer, DataPointer, usize);
/// Update a texture region
fn update_texture(&mut self, tex::TextureKind, back::Texture,
fn update_texture(&mut self, tex::TextureKind, <Self::Resources as Resources>::Texture,
tex::ImageInfo, DataPointer);
/// Clear target surfaces
fn call_clear(&mut self, target::ClearData, target::Mask);
Expand Down
22 changes: 21 additions & 1 deletion src/device/gl_device/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ extern crate libc;
extern crate "gfx_gl" as gl;

use std::marker::PhantomData;
use std::slice;
use log::LogLevel;

use attrib::{SignFlag, IntSubType, IntSize, FloatSubType, FloatSize, Type};
Expand All @@ -41,12 +42,26 @@ mod tex;
mod info;

#[allow(raw_pointer_derive)]
#[derive(Copy)]
#[derive(Copy, Clone)]
pub struct RawMapping {
pub pointer: *mut libc::c_void,
target: gl::types::GLenum,
}

impl ::RawMapping for RawMapping {
unsafe fn set<T>(&self, index: usize, val: T) {
*(self.pointer as *mut T).offset(index as isize) = val;
}

unsafe fn to_slice<T>(&self, len: usize) -> &[T] {
slice::from_raw_parts(self.pointer as *const T, len)
}

unsafe fn to_mut_slice<T>(&self, len: usize) -> &mut [T] {
slice::from_raw_parts_mut(self.pointer as *mut T, len)
}
}

pub type Buffer = gl::types::GLuint;
pub type ArrayBuffer = gl::types::GLuint;
pub type Shader = gl::types::GLuint;
Expand All @@ -60,6 +75,7 @@ pub type Texture = gl::types::GLuint;
pub enum GlResources {}

impl Resources for GlResources {
type RawMapping = RawMapping;
type Buffer = Buffer;
type ArrayBuffer = ArrayBuffer;
type Shader = Shader;
Expand All @@ -68,6 +84,10 @@ impl Resources for GlResources {
type Surface = Surface;
type Texture = Texture;
type Sampler = Sampler;

fn get_main_frame_buffer() -> ::FrameBufferHandle<GlResources> {
::Handle(0, ())
}
}

#[derive(Copy, Eq, PartialEq, Debug)]
Expand Down
Loading