Skip to content

Commit

Permalink
Merge pull request #323 from hannobraun/format
Browse files Browse the repository at this point in the history
Use preferred color format
  • Loading branch information
hannobraun authored Mar 9, 2022
2 parents 9233b41 + 320e903 commit af3c8fe
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
9 changes: 6 additions & 3 deletions src/graphics/config_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use wgpu_glyph::{

use crate::math::Aabb;

use super::{draw_config::DrawConfig, COLOR_FORMAT};
use super::draw_config::DrawConfig;

#[derive(Debug)]
pub struct ConfigUi {
Expand All @@ -17,11 +17,14 @@ pub struct ConfigUi {
}

impl ConfigUi {
pub fn new(device: &wgpu::Device) -> Result<Self, InvalidFont> {
pub fn new(
device: &wgpu::Device,
color_format: wgpu::TextureFormat,
) -> Result<Self, InvalidFont> {
let font =
FontArc::try_from_slice(include_bytes!("fonts/B612-Bold.ttf"))?;
let glyph_brush =
GlyphBrushBuilder::using_font(font).build(device, COLOR_FORMAT);
GlyphBrushBuilder::using_font(font).build(device, color_format);

let mut texts = HashMap::new();
for element in Element::elements() {
Expand Down
1 change: 0 additions & 1 deletion src/graphics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ pub use self::{
renderer::{DrawError, Renderer},
};

const COLOR_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Bgra8UnormSrgb;
const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float;
9 changes: 7 additions & 2 deletions src/graphics/pipelines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::mem::size_of;
use super::{
shaders::{Shader, Shaders},
vertices::Vertex,
COLOR_FORMAT, DEPTH_FORMAT,
DEPTH_FORMAT,
};

#[derive(Debug)]
Expand All @@ -17,6 +17,7 @@ impl Pipelines {
pub fn new(
device: &wgpu::Device,
bind_group_layout: &wgpu::BindGroupLayout,
color_format: wgpu::TextureFormat,
) -> Self {
let pipeline_layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
Expand All @@ -34,20 +35,23 @@ impl Pipelines {
shaders.model(),
wgpu::PrimitiveTopology::TriangleList,
wgpu::PolygonMode::Fill,
color_format,
),
mesh: Pipeline::new(
device,
&pipeline_layout,
shaders.mesh(),
wgpu::PrimitiveTopology::TriangleList,
wgpu::PolygonMode::Line,
color_format,
),
lines: Pipeline::new(
device,
&pipeline_layout,
shaders.lines(),
wgpu::PrimitiveTopology::LineList,
wgpu::PolygonMode::Line,
color_format,
),
}
}
Expand All @@ -63,6 +67,7 @@ impl Pipeline {
shader: Shader,
topology: wgpu::PrimitiveTopology,
polygon_mode: wgpu::PolygonMode,
color_format: wgpu::TextureFormat,
) -> Self {
let pipeline =
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
Expand Down Expand Up @@ -111,7 +116,7 @@ impl Pipeline {
module: shader.module,
entry_point: shader.frag_entry,
targets: &[wgpu::ColorTargetState {
format: COLOR_FORMAT,
format: color_format,
blend: Some(
wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING,
),
Expand Down
13 changes: 9 additions & 4 deletions src/graphics/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{camera::Camera, math::Aabb, math::Point, window::Window};
use super::{
config_ui::ConfigUi, draw_config::DrawConfig, drawables::Drawables,
geometries::Geometries, pipelines::Pipelines, transform::Transform,
uniforms::Uniforms, vertices::Vertices, COLOR_FORMAT, DEPTH_FORMAT,
uniforms::Uniforms, vertices::Vertices, DEPTH_FORMAT,
};

#[derive(Debug)]
Expand Down Expand Up @@ -65,9 +65,13 @@ impl Renderer {
)
.await?;

let color_format = surface
.get_preferred_format(&adapter)
.expect("Error determining preferred color format");

let surface_config = wgpu::SurfaceConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
format: COLOR_FORMAT,
format: color_format,
width: window.width(),
height: window.height(),
present_mode: wgpu::PresentMode::Fifo,
Expand Down Expand Up @@ -123,9 +127,10 @@ impl Renderer {
max: Point::from([0.0, 0.0, 0.0]),
},
);
let pipelines = Pipelines::new(&device, &bind_group_layout);
let pipelines =
Pipelines::new(&device, &bind_group_layout, color_format);

let config_ui = ConfigUi::new(&device)?;
let config_ui = ConfigUi::new(&device, color_format)?;

Ok(Self {
surface,
Expand Down

0 comments on commit af3c8fe

Please sign in to comment.