aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDeterminant <[email protected]>2017-12-11 16:44:53 +0800
committerDeterminant <[email protected]>2017-12-11 16:44:53 +0800
commitc8e09cc9961a9dd51587ad7da69995d248b3bb44 (patch)
tree21af031fca2243deaedcb4ab526bc00e7fb3dd79
parentb69a62a42b30baccd82c38361795a4abd67763f0 (diff)
improve rendering speed
-rw-r--r--Cargo.lock30
-rw-r--r--src/bin.rs74
2 files changed, 53 insertions, 51 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 663860b..c2e8d2b 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -26,22 +26,22 @@ dependencies = [
[[package]]
name = "lazy_static"
-version = "0.2.10"
+version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libc"
-version = "0.2.33"
+version = "0.2.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "num"
-version = "0.1.40"
+version = "0.1.41"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"num-integer 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)",
"num-iter 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)",
- "num-traits 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
+ "num-traits 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -49,7 +49,7 @@ name = "num-integer"
version = "0.1.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
- "num-traits 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
+ "num-traits 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -58,12 +58,12 @@ version = "0.1.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"num-integer 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)",
- "num-traits 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
+ "num-traits 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num-traits"
-version = "0.1.40"
+version = "0.1.41"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
@@ -72,7 +72,7 @@ version = "0.3.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"fuchsia-zircon 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
- "libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -88,9 +88,9 @@ version = "0.31.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "lazy_static 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
- "libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)",
- "num 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
+ "lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)",
+ "num 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)",
"sdl2-sys 0.31.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -108,12 +108,12 @@ dependencies = [
"checksum cfg-if 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d4c819a1287eb618df47cc647173c5c4c66ba19d888a6e50d605672aed3140de"
"checksum fuchsia-zircon 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f6c0581a4e363262e52b87f59ee2afe3415361c6ec35e665924eb08afe8ff159"
"checksum fuchsia-zircon-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "43f3795b4bae048dc6123a6b972cadde2e676f9ded08aef6bb77f5f157684a82"
-"checksum lazy_static 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "236eb37a62591d4a41a89b7763d7de3e06ca02d5ab2815446a8bae5d2f8c2d57"
-"checksum libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)" = "5ba3df4dcb460b9dfbd070d41c94c19209620c191b0340b929ce748a2bcd42d2"
-"checksum num 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "a311b77ebdc5dd4cf6449d81e4135d9f0e3b153839ac90e648a8ef538f923525"
+"checksum lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "76f033c7ad61445c5b347c7382dd1237847eb1bce590fe50365dcb33d546be73"
+"checksum libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)" = "36fbc8a8929c632868295d0178dd8f63fc423fd7537ad0738372bd010b3ac9b0"
+"checksum num 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "cc4083e14b542ea3eb9b5f33ff48bd373a92d78687e74f4cc0a30caeb754f0ca"
"checksum num-integer 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)" = "d1452e8b06e448a07f0e6ebb0bb1d92b8890eea63288c0b627331d53514d0fba"
"checksum num-iter 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)" = "7485fcc84f85b4ecd0ea527b14189281cf27d60e583ae65ebc9c088b13dffe01"
-"checksum num-traits 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "99843c856d68d8b4313b03a17e33c4bb42ae8f6610ea81b28abe076ac721b9b0"
+"checksum num-traits 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "cacfcab5eb48250ee7d0c7896b51a2c5eec99c1feea5f32025635f5ae4b00070"
"checksum rand 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)" = "6475140dfd8655aeb72e1fd4b7a1cc1c202be65d71669476e392fe62532b9edd"
"checksum sdl2 0.31.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a74c2a98a354b20713b90cce70aef9e927e46110d1bc4ef728fd74e0d53eba60"
"checksum sdl2-sys 0.31.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5c543ce8a6e33a30cb909612eeeb22e693848211a84558d5a00bb11e791b7ab7"
diff --git a/src/bin.rs b/src/bin.rs
index 6b19ed7..f30742e 100644
--- a/src/bin.rs
+++ b/src/bin.rs
@@ -5,6 +5,8 @@ use std::sync::{Mutex, Condvar};
use std::io::Read;
use std::cell::RefCell;
use std::intrinsics::transmute;
+//use std::time::{Instant, Duration};
+//use std::thread;
extern crate sdl2;
@@ -31,7 +33,7 @@ use memory::{CPUMemory, PPUMemory};
use cartridge::{BankType, MirrorType, Cartridge};
use controller::stdctl;
-const PIXEL_SIZE: u32 = 4;
+const PIXEL_SCALE: u32 = 4;
const RGB_COLORS: [u32; 64] = [
0x666666, 0x002a88, 0x1412a7, 0x3b00a4, 0x5c007e, 0x6e0040, 0x6c0600, 0x561d00,
0x333500, 0x0b4800, 0x005200, 0x004f08, 0x00404d, 0x000000, 0x000000, 0x000000,
@@ -43,13 +45,13 @@ const RGB_COLORS: [u32; 64] = [
0xe4e594, 0xcfef96, 0xbdf4ab, 0xb3f3cc, 0xb5ebf2, 0xb8b8b8, 0x000000, 0x000000,
];
-const PIX_WIDTH: usize = 256;
-const PIX_HEIGHT: usize = 240;
-const FB_PITCH: usize = PIX_WIDTH * 3 * (PIXEL_SIZE as usize);
-const FB_SIZE: usize = PIX_HEIGHT * FB_PITCH * (PIXEL_SIZE as usize);
-const WIN_WIDTH: u32 = PIX_WIDTH as u32 * PIXEL_SIZE;
-const WIN_HEIGHT: u32 = PIX_HEIGHT as u32 * PIXEL_SIZE;
-const AUDIO_SAMPLES: u16 = 4096;
+const PIX_WIDTH: u32 = 256;
+const PIX_HEIGHT: u32 = 240;
+const FB_PITCH: usize = PIX_WIDTH as usize * 3;
+const FB_SIZE: usize = PIX_HEIGHT as usize * FB_PITCH;
+const WIN_WIDTH: u32 = PIX_WIDTH * PIXEL_SCALE as u32;
+const WIN_HEIGHT: u32 = PIX_HEIGHT * PIXEL_SCALE as u32;
+const AUDIO_SAMPLES: u16 = 4410;
pub struct SimpleCart {
chr_rom: Vec<u8>,
@@ -113,9 +115,11 @@ impl<'a> SDLWindow<'a> {
.unwrap();
let mut canvas = window.into_canvas()
.accelerated()
+ .present_vsync()
.build().unwrap();
let texture_creator = canvas.texture_creator();
canvas.set_draw_color(Color::RGB(255, 255, 255));
+ canvas.set_scale(PIXEL_SCALE as f32, PIXEL_SCALE as f32).unwrap();
canvas.clear();
canvas.present();
let mut res = SDLWindow {
@@ -123,7 +127,7 @@ impl<'a> SDLWindow<'a> {
events: sdl_context.event_pump().unwrap(),
frame_buffer: [0; FB_SIZE],
texture: texture_creator.create_texture_streaming(
- PixelFormatEnum::RGB24, WIN_WIDTH, WIN_HEIGHT).unwrap(),
+ PixelFormatEnum::RGB24, PIX_WIDTH, PIX_HEIGHT).unwrap(),
p1_button_state: 0,
p1_ctl, p1_keymap: [stdctl::NULL; 256],
};
@@ -178,38 +182,25 @@ fn get_rgb(color: u8) -> (u8, u8, u8) {
impl<'a> ppu::Screen for SDLWindow<'a> {
+ #[inline(always)]
fn put(&mut self, x: u8, y: u8, color: u8) {
let (r, g, b) = get_rgb(color);
- let mut pattern = [0; 3 * PIXEL_SIZE as usize];
- let mut base = ((y as u32 * PIXEL_SIZE) as usize * FB_PITCH) +
- (x as u32 * 3 * PIXEL_SIZE) as usize;
- {
- let mut i = 0;
- for _ in 0..PIXEL_SIZE {
- pattern[i] = r;
- pattern[i + 1] = g;
- pattern[i + 2] = b;
- i += 3;
- }
- }
- for _ in 0..PIXEL_SIZE {
- self.frame_buffer[base..base + 3 * PIXEL_SIZE as usize]
- .copy_from_slice(&pattern[..]);
- base += FB_PITCH;
- }
+ let base = (y as usize * FB_PITCH) + x as usize * 3;
+ self.frame_buffer[base] = r;
+ self.frame_buffer[base + 1] = g;
+ self.frame_buffer[base + 2] = b;
}
fn render(&mut self) {
self.texture.update(None, &self.frame_buffer, FB_PITCH).unwrap();
- //canvas.set_draw_color(Color::RGB(128, 128, 128));
- }
-
- fn frame(&mut self) {
self.canvas.clear();
- self.canvas.copy(&self.texture, None,
- Some(Rect::new(0, 0, WIN_WIDTH, WIN_HEIGHT))).unwrap();
+ self.canvas.copy(&self.texture, None, None).unwrap();
self.canvas.present();
if self.poll() {std::process::exit(0);}
+
+ }
+
+ fn frame(&mut self) {
}
}
@@ -253,7 +244,7 @@ impl CircularBuffer {
struct AudioSync {
time_barrier: Condvar,
- buffer: Mutex<(CircularBuffer, u16)>,
+ buffer: Mutex<(CircularBuffer, u16, bool)>,
}
struct SDLAudio<'a>(&'a AudioSync);
@@ -265,6 +256,13 @@ impl<'a> AudioCallback for SDLAudioPlayback<'a> {
let mut m = self.0.buffer.lock().unwrap();
{
let b = &mut m.0;
+ /*
+ let l1 = (b.tail + b.buffer.len() - b.head) % b.buffer.len();
+ let l2 = out.len();
+ if l1 < l2 {
+ println!("{} {}", l1, l2);
+ }
+ */
for x in out.iter_mut() {
*x = b.deque()
}
@@ -272,6 +270,7 @@ impl<'a> AudioCallback for SDLAudioPlayback<'a> {
if m.1 >= AUDIO_SAMPLES {
m.1 -= AUDIO_SAMPLES
}
+ m.2 = false;
self.0.time_barrier.notify_one();
}
}
@@ -284,9 +283,12 @@ impl<'a> apu::Speaker for SDLAudio<'a> {
b.enque(sample.wrapping_sub(32768) as i16);
}
m.1 += 1;
- while m.1 >= AUDIO_SAMPLES {
- m = self.0.time_barrier.wait(m).unwrap()
+ if m.2 {
+ while m.2 && m.1 >= AUDIO_SAMPLES {
+ m = self.0.time_barrier.wait(m).unwrap();
+ }
}
+ m.2 = true;
}
}
@@ -363,7 +365,7 @@ fn main() {
let sdl_context = sdl2::init().unwrap();
let audio_subsystem = sdl_context.audio().unwrap();
let audio_sync = AudioSync { time_barrier: Condvar::new(),
- buffer: Mutex::new((CircularBuffer::new(), 0))};
+ buffer: Mutex::new((CircularBuffer::new(), 0, true))};
let mut spkr = SDLAudio(&audio_sync);
let desired_spec = AudioSpecDesired {
freq: Some(apu::AUDIO_SAMPLE_FREQ as i32),