Skip to content
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

Integration tests for graphics #80

Merged
merged 15 commits into from
Sep 3, 2019
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/target
tests/_graphics/models
**/*.rs.bk
Cargo.lock
*.DS_Store
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ wgpu_glyph = { version = "0.3", optional = true, git = "https://github.com/hecrj

[dev-dependencies]
rand = "0.6"
env_logger = "0.6"
2 changes: 1 addition & 1 deletion examples/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl UserInterface for Counter {
type Message = Message;
type Renderer = Renderer;

fn react(&mut self, message: Message) {
fn react(&mut self, message: Message, _window: &mut Window) {
match message {
Message::IncrementPressed => {
self.value += 1;
Expand Down
2 changes: 1 addition & 1 deletion examples/gamepad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl UserInterface for GamepadExample {
type Message = ();
type Renderer = Renderer;

fn react(&mut self, _msg: ()) {}
fn react(&mut self, _msg: (), _window: &mut Window) {}

fn layout(&mut self, window: &Window) -> Element<()> {
Column::new()
Expand Down
20 changes: 6 additions & 14 deletions examples/image.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use coffee::graphics::{
Color, Frame, HorizontalAlignment, VerticalAlignment, Window,
WindowSettings, self,
self, Color, Frame, HorizontalAlignment, VerticalAlignment, Window,
WindowSettings,
};
use coffee::load::Task;
use coffee::ui::{
Align, Column, Element, Justify, Renderer, Text, UserInterface, Image,
Align, Column, Element, Image, Justify, Renderer, Text, UserInterface,
};
use coffee::{Game, Result, Timer};

Expand All @@ -27,11 +27,7 @@ impl Game for ImageScreen {

fn load(_window: &Window) -> Task<ImageScreen> {
graphics::Image::load("resources/ui.png")
.map(|image| {
ImageScreen {
image,
}
})
.map(|image| ImageScreen { image })
}

fn draw(&mut self, frame: &mut Frame, _timer: &Timer) {
Expand All @@ -48,8 +44,7 @@ impl UserInterface for ImageScreen {
type Message = ();
type Renderer = Renderer;

fn react(&mut self, _message: ()) {
}
fn react(&mut self, _message: (), _window: &mut Window) {}

fn layout(&mut self, window: &Window) -> Element<()> {
Column::new()
Expand All @@ -65,10 +60,7 @@ impl UserInterface for ImageScreen {
.horizontal_alignment(HorizontalAlignment::Center)
.vertical_alignment(VerticalAlignment::Center),
)
.push(
Image::new(&self.image)
.height(250)
)
.push(Image::new(&self.image).height(250))
.into()
}
}
2 changes: 1 addition & 1 deletion examples/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl UserInterface for InputExample {
type Message = ();
type Renderer = Renderer;

fn react(&mut self, _msg: ()) {}
fn react(&mut self, _msg: (), _window: &mut Window) {}

fn layout(&mut self, window: &Window) -> Element<()> {
let keys = self
Expand Down
2 changes: 1 addition & 1 deletion examples/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl UserInterface for Example {
type Message = Message;
type Renderer = Renderer;

fn react(&mut self, msg: Message) {
fn react(&mut self, msg: Message, _window: &mut Window) {
match msg {
Message::ShapeSelected(shape) => {
self.shape = shape;
Expand Down
2 changes: 1 addition & 1 deletion examples/particles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl UserInterface for Particles {
type Message = Message;
type Renderer = Renderer;

fn react(&mut self, msg: Message) {
fn react(&mut self, msg: Message, _window: &mut Window) {
match msg {
Message::ToggleInterpolation(interpolate) => {
self.interpolate = interpolate;
Expand Down
15 changes: 4 additions & 11 deletions examples/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use coffee::graphics::{
};
use coffee::load::Task;
use coffee::ui::{
Align, Column, Element, Justify, Renderer, Text,
UserInterface, ProgressBar,
Align, Column, Element, Justify, ProgressBar, Renderer, Text, UserInterface,
};
use coffee::{Game, Result, Timer};

Expand All @@ -27,9 +26,7 @@ impl Game for Progress {
type LoadingScreen = ();

fn load(_window: &Window) -> Task<Progress> {
Task::succeed(|| Progress {
value: 0.0,
})
Task::succeed(|| Progress { value: 0.0 })
}

fn draw(&mut self, frame: &mut Frame, timer: &Timer) {
Expand All @@ -53,8 +50,7 @@ impl UserInterface for Progress {
type Message = ();
type Renderer = Renderer;

fn react(&mut self, _message: ()) {
}
fn react(&mut self, _message: (), _window: &mut Window) {}

fn layout(&mut self, window: &Window) -> Element<()> {
Column::new()
Expand All @@ -70,10 +66,7 @@ impl UserInterface for Progress {
.horizontal_alignment(HorizontalAlignment::Center)
.vertical_alignment(VerticalAlignment::Center),
)
.push(
ProgressBar::new(self.value)
.width(400),
)
.push(ProgressBar::new(self.value).width(400))
.into()
}
}
2 changes: 1 addition & 1 deletion examples/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl UserInterface for Tour {
type Message = Message;
type Renderer = Renderer;

fn react(&mut self, event: Message) {
fn react(&mut self, event: Message, _window: &mut Window) {
match event {
Message::BackPressed => {
self.steps.go_back();
Expand Down
7 changes: 7 additions & 0 deletions src/graphics/backend_gfx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ impl Gpu {
texture::Drawable::new(&mut self.factory, width, height)
}

pub(super) fn read_drawable_texture_pixels(
&mut self,
drawable: &texture::Drawable,
) -> image::DynamicImage {
drawable.read_pixels(&mut self.device, &mut self.factory)
}

pub(super) fn upload_font(&mut self, bytes: &'static [u8]) -> Font {
Font::from_bytes(&mut self.factory, bytes)
}
Expand Down
68 changes: 59 additions & 9 deletions src/graphics/backend_gfx/texture.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use image;

use gfx::format::{ChannelTyped, SurfaceTyped};
use gfx::memory::Typed;
use gfx::traits::FactoryExt;
use gfx_core::factory::Factory;
use gfx_device_gl as gl;

Expand All @@ -11,7 +13,7 @@ use crate::graphics::Transformation;

#[derive(Clone, Debug)]
pub struct Texture {
texture: RawTexture,
raw: RawTexture,
view: ShaderResource,
width: u16,
height: u16,
Expand All @@ -27,7 +29,7 @@ impl Texture {
let width = rgba.width() as u16;
let height = rgba.height() as u16;

let (texture, view) = create_texture_array(
let (raw, view) = create_texture_array(
factory,
width,
height,
Expand All @@ -37,7 +39,7 @@ impl Texture {
);

Texture {
texture,
raw,
view,
width,
height,
Expand All @@ -58,7 +60,7 @@ impl Texture {

let raw_layers: Vec<&[u8]> = rgba.iter().map(|i| &i[..]).collect();

let (texture, view) = create_texture_array(
let (raw, view) = create_texture_array(
factory,
width,
height,
Expand All @@ -68,7 +70,7 @@ impl Texture {
);

Texture {
texture,
raw,
view,
width,
height,
Expand All @@ -77,7 +79,7 @@ impl Texture {
}

pub(super) fn handle(&self) -> &RawTexture {
&self.texture
&self.raw
}

pub(super) fn view(&self) -> &ShaderResource {
Expand All @@ -101,17 +103,18 @@ pub struct Drawable {

impl Drawable {
pub fn new(factory: &mut gl::Factory, width: u16, height: u16) -> Drawable {
let (texture, view) = create_texture_array(
let (raw, view) = create_texture_array(
factory,
width,
height,
None,
gfx::memory::Bind::SHADER_RESOURCE
| gfx::memory::Bind::RENDER_TARGET,
| gfx::memory::Bind::RENDER_TARGET
| gfx::memory::Bind::TRANSFER_SRC,
);

let texture = Texture {
texture,
raw,
view,
width,
height,
Expand Down Expand Up @@ -139,6 +142,53 @@ impl Drawable {
&self.target
}

pub fn read_pixels(
&self,
device: &mut gl::Device,
factory: &mut gl::Factory,
) -> image::DynamicImage {
let width = self.texture.width();
let height = self.texture.height();

let download = factory
.create_download_buffer::<u8>(width as usize * height as usize * 4)
.expect("Create download buffer");

let mut encoder: gfx::Encoder<gl::Resources, gl::CommandBuffer> =
factory.create_command_buffer().into();

encoder
.copy_texture_to_buffer_raw(
&self.texture.raw,
None,
gfx::texture::RawImageInfo {
xoffset: 0,
yoffset: 0,
zoffset: 0,
width,
height,
depth: 0,
format: <gfx::format::Srgba8 as gfx::format::Formatted>::get_format(),
mipmap: 0,
},
download.raw(),
0,
)
.expect("Copy texture to raw buffer");

encoder.flush(device);

let reader = factory.read_mapping(&download).expect("Read mapping");

let mut rgba = Vec::with_capacity(width as usize * height as usize * 4);
rgba.extend(reader.into_iter());

image::DynamicImage::ImageRgba8(
image::ImageBuffer::from_raw(width as u32, height as u32, rgba)
.expect("Create RGBA8 image"),
)
}

pub fn render_transformation() -> Transformation {
Transformation::nonuniform_scale(Vector::new(1.0, -1.0))
}
Expand Down
7 changes: 7 additions & 0 deletions src/graphics/backend_wgpu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ impl Gpu {
)
}

pub(super) fn read_drawable_texture_pixels(
&mut self,
drawable: &texture::Drawable,
) -> image::DynamicImage {
drawable.read_pixels(&mut self.device)
}

pub(super) fn upload_font(&mut self, bytes: &'static [u8]) -> Font {
Font::from_bytes(&mut self.device, bytes)
}
Expand Down
Loading