Skip to content

Commit

Permalink
Support adapter-dependent color formats
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Nov 28, 2021
1 parent 25c9a0f commit fa82c51
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 24 deletions.
12 changes: 9 additions & 3 deletions bin/boilerplate.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(clippy::single_match)]
use vangers::{
config,
render::{ScreenTargets, COLOR_FORMAT, DEPTH_FORMAT},
render::{ScreenTargets, DEPTH_FORMAT},
};

use futures::executor::{LocalPool, LocalSpawner};
Expand Down Expand Up @@ -41,6 +41,7 @@ pub struct Harness {
pub queue: wgpu::Queue,
pub downlevel_caps: wgpu::DownlevelCapabilities,
surface: wgpu::Surface,
pub color_format: wgpu::TextureFormat,
pub extent: wgpu::Extent3d,
reload_on_focus: bool,
depth_target: wgpu::TextureView,
Expand Down Expand Up @@ -118,12 +119,15 @@ impl Harness {

let config = wgpu::SurfaceConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
format: COLOR_FORMAT,
format: surface
.get_preferred_format(&adapter)
.unwrap_or(wgpu::TextureFormat::Bgra8UnormSrgb),
width: extent.width,
height: extent.height,
present_mode: wgpu::PresentMode::Mailbox,
};
surface.configure(&device, &config);

let depth_target = device
.create_texture(&wgpu::TextureDescriptor {
label: Some("Depth"),
Expand All @@ -144,6 +148,7 @@ impl Harness {
downlevel_caps,
queue,
surface,
color_format: config.format,
extent,
reload_on_focus: settings.window.reload_on_focus,
depth_target,
Expand All @@ -165,6 +170,7 @@ impl Harness {
queue,
downlevel_caps: _,
surface,
color_format,
mut extent,
reload_on_focus,
mut depth_target,
Expand All @@ -188,7 +194,7 @@ impl Harness {
};
let config = wgpu::SurfaceConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
format: COLOR_FORMAT,
format: color_format,
width: size.width,
height: size.height,
present_mode: wgpu::PresentMode::Mailbox,
Expand Down
2 changes: 2 additions & 0 deletions bin/car/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ impl CarView {
device: &wgpu::Device,
queue: &wgpu::Queue,
downlevel_caps: &wgpu::DownlevelCapabilities,
color_format: wgpu::TextureFormat,
) -> Self {
info!("Initializing the render");
let pal_data = level::read_palette(settings.open_palette(), None);
Expand All @@ -35,6 +36,7 @@ impl CarView {
let global = render::global::Context::new(
device,
queue,
color_format,
#[cfg(feature = "glsl")]
store_init.resource(),
None,
Expand Down
1 change: 1 addition & 0 deletions bin/car/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ fn main() {
&harness.device,
&harness.queue,
&harness.downlevel_caps,
harness.color_format,
);

harness.main_loop(app);
Expand Down
2 changes: 2 additions & 0 deletions bin/level/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub struct LevelView {
impl LevelView {
pub fn new(
settings: &config::settings::Settings,
color_format: wgpu::TextureFormat,
screen_extent: wgpu::Extent3d,
device: &wgpu::Device,
queue: &wgpu::Queue,
Expand Down Expand Up @@ -114,6 +115,7 @@ impl LevelView {
&level,
&objects_palette,
&settings.render,
color_format,
screen_extent,
#[cfg(feature = "glsl")]
store_init.resource(),
Expand Down
1 change: 1 addition & 0 deletions bin/level/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ fn main() {

let app = app::LevelView::new(
&settings,
harness.color_format,
harness.extent,
&harness.device,
&harness.queue,
Expand Down
2 changes: 2 additions & 0 deletions bin/model/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ impl ResourceView {
device: &wgpu::Device,
queue: &wgpu::Queue,
downlevel_caps: &wgpu::DownlevelCapabilities,
color_format: wgpu::TextureFormat,
) -> Self {
info!("Initializing the render");
let pal_data = level::read_palette(settings.open_palette(), None);
Expand All @@ -32,6 +33,7 @@ impl ResourceView {
let global = render::global::Context::new(
device,
queue,
color_format,
#[cfg(feature = "glsl")]
store_init.resource(),
None,
Expand Down
1 change: 1 addition & 0 deletions bin/model/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ fn main() {
&harness.device,
&harness.queue,
&harness.downlevel_caps,
harness.color_format,
);

harness.main_loop(app);
Expand Down
2 changes: 2 additions & 0 deletions bin/road/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ pub struct Game {
impl Game {
pub fn new(
settings: &config::Settings,
color_format: wgpu::TextureFormat,
screen_extent: wgpu::Extent3d,
device: &wgpu::Device,
queue: &wgpu::Queue,
Expand Down Expand Up @@ -373,6 +374,7 @@ impl Game {
&level,
&pal_data,
&settings.render,
color_format,
screen_extent,
#[cfg(feature = "glsl")]
store_init.resource(),
Expand Down
1 change: 1 addition & 0 deletions bin/road/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ fn main() {

let game = game::Game::new(
&settings,
harness.color_format,
harness.extent,
&harness.device,
&harness.queue,
Expand Down
8 changes: 5 additions & 3 deletions src/render/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
render::{
global::Context as GlobalContext,
object::{Context as ObjectContext, Instance as ObjectInstance},
VertexStorageNotSupported, COLOR_FORMAT, DEPTH_FORMAT,
VertexStorageNotSupported, DEPTH_FORMAT,
},
};

Expand Down Expand Up @@ -102,6 +102,7 @@ pub struct Context {
bind_group_line: wgpu::BindGroup,
bind_group_face: wgpu::BindGroup,
bind_group_edge: wgpu::BindGroup,
color_format: wgpu::TextureFormat,
// hold the buffers alive
vertex_buf: Option<wgpu::Buffer>,
color_buf: Option<wgpu::Buffer>,
Expand Down Expand Up @@ -204,6 +205,7 @@ impl Context {
bind_group_line,
bind_group_face,
bind_group_edge,
color_format: global.color_format,
vertex_buf: None,
color_buf: None,
};
Expand Down Expand Up @@ -237,7 +239,7 @@ impl Context {
module: &shaders.fs,
entry_point: "main",
targets: &[wgpu::ColorTargetState {
format: COLOR_FORMAT,
format: self.color_format,
blend: Some(wgpu::BlendState {
alpha: wgpu::BlendComponent {
src_factor: wgpu::BlendFactor::One,
Expand Down Expand Up @@ -312,7 +314,7 @@ impl Context {
module: &shader,
entry_point: "main_fs",
targets: &[wgpu::ColorTargetState {
format: COLOR_FORMAT,
format: self.color_format,
blend: Some(wgpu::BlendState {
color: blend,
alpha: blend,
Expand Down
3 changes: 3 additions & 0 deletions src/render/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ pub struct Context {
pub uniform_buf: wgpu::Buffer,
pub bind_group: wgpu::BindGroup,
pub shadow_bind_group: wgpu::BindGroup,
pub color_format: wgpu::TextureFormat,
}

impl Context {
pub fn new(
device: &wgpu::Device,
queue: &wgpu::Queue,
color_format: wgpu::TextureFormat,
#[cfg(feature = "glsl")] store_buffer: wgpu::BindingResource<'_>,
shadow_view: Option<&wgpu::TextureView>,
) -> Self {
Expand Down Expand Up @@ -222,6 +224,7 @@ impl Context {
uniform_buf,
bind_group,
shadow_bind_group,
color_format,
}
}
}
3 changes: 2 additions & 1 deletion src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ pub mod body {
}

pub use shadow::FORMAT as SHADOW_FORMAT;
pub const COLOR_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Bgra8Unorm;
pub const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float;

#[derive(Clone, Copy, Debug)]
Expand Down Expand Up @@ -536,6 +535,7 @@ impl Render {
level: &level::Level,
object_palette: &[[u8; 4]],
settings: &settings::Render,
color_format: wgpu::TextureFormat,
screen_size: wgpu::Extent3d,
#[cfg(feature = "glsl")] store_buffer: wgpu::BindingResource<'_>,
) -> Self {
Expand All @@ -550,6 +550,7 @@ impl Render {
let global = global::Context::new(
device,
queue,
color_format,
#[cfg(feature = "glsl")]
store_buffer,
shadow.as_ref().map(|shadow| &shadow.view),
Expand Down
16 changes: 11 additions & 5 deletions src/render/object.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
render::{
body::GpuBody, global::Context as GlobalContext, GpuTransform, Palette, PipelineSet,
VertexStorageNotSupported, COLOR_FORMAT, DEPTH_FORMAT, SHADOW_FORMAT,
VertexStorageNotSupported, DEPTH_FORMAT, SHADOW_FORMAT,
},
space::Transform,
};
Expand Down Expand Up @@ -108,10 +108,15 @@ pub struct Context {
pub shape_bind_group_layout: Result<wgpu::BindGroupLayout, VertexStorageNotSupported>,
pub pipeline_layout: wgpu::PipelineLayout,
pub pipelines: PipelineSet,
pub color_format: wgpu::TextureFormat,
}

impl Context {
fn create_pipelines(layout: &wgpu::PipelineLayout, device: &wgpu::Device) -> PipelineSet {
fn create_pipelines(
layout: &wgpu::PipelineLayout,
device: &wgpu::Device,
color_format: wgpu::TextureFormat,
) -> PipelineSet {
let vertex_descriptor = wgpu::VertexBufferLayout {
array_stride: mem::size_of::<Vertex>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
Expand All @@ -131,7 +136,7 @@ impl Context {
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: "color_fs",
targets: &[COLOR_FORMAT.into()],
targets: &[color_format.into()],
}),
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
Expand Down Expand Up @@ -319,17 +324,18 @@ impl Context {
bind_group_layouts: &[&global.bind_group_layout, &bind_group_layout],
push_constant_ranges: &[],
});
let pipelines = Self::create_pipelines(&pipeline_layout, device);
let pipelines = Self::create_pipelines(&pipeline_layout, device, global.color_format);

Context {
bind_group,
shape_bind_group_layout,
pipeline_layout,
pipelines,
color_format: global.color_format,
}
}

pub fn reload(&mut self, device: &wgpu::Device) {
self.pipelines = Self::create_pipelines(&self.pipeline_layout, device);
self.pipelines = Self::create_pipelines(&self.pipeline_layout, device, self.color_format);
}
}
Loading

0 comments on commit fa82c51

Please sign in to comment.