Skip to content

Associated resource type preparations #564

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 11 commits into from
Feb 18, 2015
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::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,
light_pos_buf: gfx::RawBufferHandle<gfx::GlResources>,
#[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::GlResources>, TextureHandle<gfx::GlResources>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the user has to access gfx_device_gl crate anyway in order to create he device, perhaps we could provide a typedef for TextureHandle in there?

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 @@ -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::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 @@ -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::GlResources>> = None;

let mut light_pos_vec: Vec<[f32; 4]> = (0 ..NUM_LIGHTS).map(|_| {
[0.0, 0.0, 0.0, 0.0]
Expand Down
5 changes: 4 additions & 1 deletion src/device/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use back;
use shade;
use target;
use tex;
use Resources;

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

/// An empty constructor
fn new() -> Self;
/// Clear the command buffer contents, retain the allocated storage
Expand Down Expand Up @@ -105,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>);
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
108 changes: 72 additions & 36 deletions src/device/gl_device/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,58 @@

//! OpenGL implementation of the Command Buffer

use Command;
use std::slice;

pub struct GlCommandBuffer {
buf: Vec<::Command>,
use {attrib, back, draw, target, tex, shade, state};
use {AttributeSlot, IndexType, InstanceCount, PrimitiveType, TextureSlot, UniformBlockIndex, UniformBufferSlot, VertexCount};
use super::{ArrayBuffer, Buffer, FrameBuffer, Program, Surface, Texture};

/// Serialized device command.
#[derive(Copy, Debug)]
pub enum Command {
BindProgram(Program),
BindArrayBuffer(ArrayBuffer),
BindAttribute(AttributeSlot, Buffer, attrib::Format),
BindIndex(Buffer),
BindFrameBuffer(target::Access, FrameBuffer),
UnbindTarget(target::Access, target::Target),
BindTargetSurface(target::Access, target::Target, Surface),
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::GlResources>>),
SetDrawColorBuffers(usize),
SetPrimitiveState(state::Primitive),
SetViewport(target::Rect),
SetMultiSampleState(Option<state::MultiSample>),
SetScissor(Option<target::Rect>),
SetDepthStencilState(Option<state::Depth>, Option<state::Stencil>, state::CullMode),
SetBlendState(Option<state::Blend>),
SetColorMask(state::ColorMask),
UpdateBuffer(Buffer, draw::DataPointer, usize),
UpdateTexture(tex::TextureKind, Texture, tex::ImageInfo, draw::DataPointer),
// drawing
Clear(target::ClearData, target::Mask),
Draw(PrimitiveType, VertexCount, VertexCount, Option<(InstanceCount, VertexCount)>),
DrawIndexed(PrimitiveType, IndexType, VertexCount, VertexCount, VertexCount, Option<(InstanceCount, VertexCount)>),
Blit(target::Rect, target::Rect, target::Mirror, target::Mask),
}

impl GlCommandBuffer {
pub fn iter<'a>(&'a self) -> slice::Iter<'a, ::Command> {
pub struct CommandBuffer {
buf: Vec<Command>,
}

impl CommandBuffer {
pub fn iter<'a>(&'a self) -> slice::Iter<'a, Command> {
self.buf.iter()
}
}

impl ::draw::CommandBuffer for GlCommandBuffer {
fn new() -> GlCommandBuffer {
GlCommandBuffer {
impl draw::CommandBuffer for CommandBuffer {
type Resources = super::GlResources;

fn new() -> CommandBuffer {
CommandBuffer {
buf: Vec::new(),
}
}
Expand All @@ -38,99 +74,99 @@ impl ::draw::CommandBuffer for GlCommandBuffer {
self.buf.clear();
}

fn bind_program(&mut self, prog: super::Program) {
fn bind_program(&mut self, prog: Program) {
self.buf.push(Command::BindProgram(prog));
}

fn bind_array_buffer(&mut self, vao: super::ArrayBuffer) {
fn bind_array_buffer(&mut self, vao: ArrayBuffer) {
self.buf.push(Command::BindArrayBuffer(vao));
}

fn bind_attribute(&mut self, slot: ::AttributeSlot, buf: super::Buffer,
fn bind_attribute(&mut self, slot: ::AttributeSlot, buf: Buffer,
format: ::attrib::Format) {
self.buf.push(Command::BindAttribute(slot, buf, format));
}

fn bind_index(&mut self, buf: super::Buffer) {
fn bind_index(&mut self, buf: Buffer) {
self.buf.push(Command::BindIndex(buf));
}

fn bind_frame_buffer(&mut self, access: ::target::Access, fbo: super::FrameBuffer) {
fn bind_frame_buffer(&mut self, access: target::Access, fbo: FrameBuffer) {
self.buf.push(Command::BindFrameBuffer(access, fbo));
}

fn unbind_target(&mut self, access: ::target::Access, tar: ::target::Target) {
fn unbind_target(&mut self, access: target::Access, tar: target::Target) {
self.buf.push(Command::UnbindTarget(access, tar));
}

fn bind_target_surface(&mut self, access: ::target::Access,
tar: ::target::Target, suf: super::Surface) {
fn bind_target_surface(&mut self, access: target::Access,
tar: target::Target, suf: Surface) {
self.buf.push(Command::BindTargetSurface(access, tar, suf));
}

fn bind_target_texture(&mut self, access: ::target::Access,
tar: ::target::Target, tex: super::Texture,
level: ::target::Level, layer: Option<::target::Layer>) {
fn bind_target_texture(&mut self, access: target::Access,
tar: target::Target, tex: Texture,
level: target::Level, layer: Option<target::Layer>) {
self.buf.push(Command::BindTargetTexture(access, tar, tex, level, layer));
}

fn bind_uniform_block(&mut self, prog: super::Program, slot: ::UniformBufferSlot,
index: ::UniformBlockIndex, buf: super::Buffer) {
fn bind_uniform_block(&mut self, prog: Program, slot: ::UniformBufferSlot,
index: ::UniformBlockIndex, buf: Buffer) {
self.buf.push(Command::BindUniformBlock(prog, slot, index, buf));
}

fn bind_uniform(&mut self, loc: ::shade::Location, value: ::shade::UniformValue) {
self.buf.push(Command::BindUniform(loc, value));
}
fn bind_texture(&mut self, slot: ::TextureSlot, kind: ::tex::TextureKind,
tex: super::Texture, sampler: Option<::SamplerHandle>) {
tex: Texture, sampler: Option<::SamplerHandle<back::GlResources>>) {
self.buf.push(Command::BindTexture(slot, kind, tex, sampler));
}

fn set_draw_color_buffers(&mut self, num: usize) {
self.buf.push(Command::SetDrawColorBuffers(num));
}

fn set_primitive(&mut self, prim: ::state::Primitive) {
fn set_primitive(&mut self, prim: state::Primitive) {
self.buf.push(Command::SetPrimitiveState(prim));
}

fn set_viewport(&mut self, view: ::target::Rect) {
fn set_viewport(&mut self, view: target::Rect) {
self.buf.push(Command::SetViewport(view));
}

fn set_multi_sample(&mut self, ms: Option<::state::MultiSample>) {
fn set_multi_sample(&mut self, ms: Option<state::MultiSample>) {
self.buf.push(Command::SetMultiSampleState(ms));
}

fn set_scissor(&mut self, rect: Option<::target::Rect>) {
fn set_scissor(&mut self, rect: Option<target::Rect>) {
self.buf.push(Command::SetScissor(rect));
}

fn set_depth_stencil(&mut self, depth: Option<::state::Depth>,
stencil: Option<::state::Stencil>, cull: ::state::CullMode) {
fn set_depth_stencil(&mut self, depth: Option<state::Depth>,
stencil: Option<state::Stencil>, cull: state::CullMode) {
self.buf.push(Command::SetDepthStencilState(depth, stencil, cull));
}

fn set_blend(&mut self, blend: Option<::state::Blend>) {
fn set_blend(&mut self, blend: Option<state::Blend>) {
self.buf.push(Command::SetBlendState(blend));
}

fn set_color_mask(&mut self, mask: ::state::ColorMask) {
fn set_color_mask(&mut self, mask: state::ColorMask) {
self.buf.push(Command::SetColorMask(mask));
}

fn update_buffer(&mut self, buf: super::Buffer, data: ::draw::DataPointer,
fn update_buffer(&mut self, buf: Buffer, data: draw::DataPointer,
offset_bytes: usize) {
self.buf.push(Command::UpdateBuffer(buf, data, offset_bytes));
}

fn update_texture(&mut self, kind: ::tex::TextureKind, tex: super::Texture,
info: ::tex::ImageInfo, data: ::draw::DataPointer) {
fn update_texture(&mut self, kind: ::tex::TextureKind, tex: Texture,
info: ::tex::ImageInfo, data: draw::DataPointer) {
self.buf.push(Command::UpdateTexture(kind, tex, info, data));
}

fn call_clear(&mut self, data: ::target::ClearData, mask: ::target::Mask) {
fn call_clear(&mut self, data: target::ClearData, mask: target::Mask) {
self.buf.push(Command::Clear(data, mask));
}

Expand All @@ -145,8 +181,8 @@ impl ::draw::CommandBuffer for GlCommandBuffer {
self.buf.push(Command::DrawIndexed(ptype, itype, start, count, base, instances));
}

fn call_blit(&mut self, s_rect: ::target::Rect, d_rect: ::target::Rect,
mirror: ::target::Mirror, mask: ::target::Mask) {
fn call_blit(&mut self, s_rect: target::Rect, d_rect: target::Rect,
mirror: target::Mirror, mask: target::Mask) {
self.buf.push(Command::Blit(s_rect, d_rect, mirror, mask));
}
}
Loading