Compare commits
No commits in common. "295037edc6a22774ae76cb3a9b26d97766e0db5d" and "5a6b10965de4b6fb530df846ec76a92b0722b8c4" have entirely different histories.
295037edc6
...
5a6b10965d
61
src/lib.rs
61
src/lib.rs
|
|
@ -8,10 +8,7 @@ use rand_distr::Distribution as _;
|
||||||
use crate::{
|
use crate::{
|
||||||
camera::OrbitalCamera,
|
camera::OrbitalCamera,
|
||||||
ray::Ray,
|
ray::Ray,
|
||||||
render::{
|
render::lines::{LookParams, Mesh, Pipeline, Vertex},
|
||||||
DEPTH_FORMAT, OUTPUT_FORMAT,
|
|
||||||
lines::{LookParams, Mesh, Pipeline, Vertex},
|
|
||||||
},
|
|
||||||
trace::{Hit, Lambertian, Reflector, Scene, Source, Sphere},
|
trace::{Hit, Lambertian, Reflector, Scene, Source, Sphere},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -21,6 +18,8 @@ mod render;
|
||||||
mod shape;
|
mod shape;
|
||||||
mod trace;
|
mod trace;
|
||||||
|
|
||||||
|
const OUTPUT_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Bgra8UnormSrgb;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub struct SphericalPosition {
|
pub struct SphericalPosition {
|
||||||
|
|
@ -67,8 +66,6 @@ pub struct Core {
|
||||||
queue: wgpu::Queue,
|
queue: wgpu::Queue,
|
||||||
surface: wgpu::Surface<'static>,
|
surface: wgpu::Surface<'static>,
|
||||||
|
|
||||||
depth: wgpu::Texture,
|
|
||||||
|
|
||||||
pipeline: Pipeline,
|
pipeline: Pipeline,
|
||||||
mesh_pipe: render::faces::Pipeline,
|
mesh_pipe: render::faces::Pipeline,
|
||||||
tripod: Mesh,
|
tripod: Mesh,
|
||||||
|
|
@ -126,26 +123,22 @@ impl OrbitalCamera {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Core {
|
impl Core {
|
||||||
pub fn new(gpu: Gpu, pixel_size: UVec2) -> Self {
|
pub fn new(gpu: Gpu) -> Self {
|
||||||
let Gpu {
|
let Gpu {
|
||||||
device,
|
device,
|
||||||
queue,
|
queue,
|
||||||
surface,
|
surface,
|
||||||
} = gpu;
|
} = gpu;
|
||||||
|
|
||||||
let pipeline = Pipeline::new(&device);
|
let pipeline = Pipeline::new(&device, OUTPUT_FORMAT);
|
||||||
let mesh_pipe = render::faces::Pipeline::new(&device);
|
let mesh_pipe = render::faces::Pipeline::new(&device, OUTPUT_FORMAT);
|
||||||
let tripod = new_tripod(&device);
|
let tripod = new_tripod(&device);
|
||||||
let depth = Self::create_depth_buffer(&device, pixel_size);
|
|
||||||
queue.submit([]); // flush buffer updates
|
queue.submit([]); // flush buffer updates
|
||||||
|
|
||||||
Self::configure_surface(&surface, &device, pixel_size);
|
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
device,
|
device,
|
||||||
queue,
|
queue,
|
||||||
surface,
|
surface,
|
||||||
depth,
|
|
||||||
pipeline,
|
pipeline,
|
||||||
tripod,
|
tripod,
|
||||||
mesh_pipe,
|
mesh_pipe,
|
||||||
|
|
@ -180,9 +173,6 @@ impl Core {
|
||||||
self.queue.submit([]); // flush buffer updates
|
self.queue.submit([]); // flush buffer updates
|
||||||
|
|
||||||
let view = output.create_view(&wgpu::TextureViewDescriptor::default());
|
let view = output.create_view(&wgpu::TextureViewDescriptor::default());
|
||||||
let depth_view = self
|
|
||||||
.depth
|
|
||||||
.create_view(&wgpu::TextureViewDescriptor::default());
|
|
||||||
let mut encoder = self
|
let mut encoder = self
|
||||||
.device
|
.device
|
||||||
.create_command_encoder(&wgpu::CommandEncoderDescriptor::default());
|
.create_command_encoder(&wgpu::CommandEncoderDescriptor::default());
|
||||||
|
|
@ -196,14 +186,7 @@ impl Core {
|
||||||
store: wgpu::StoreOp::Store,
|
store: wgpu::StoreOp::Store,
|
||||||
},
|
},
|
||||||
})],
|
})],
|
||||||
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
|
depth_stencil_attachment: None,
|
||||||
view: &depth_view,
|
|
||||||
depth_ops: Some(wgpu::Operations {
|
|
||||||
load: wgpu::LoadOp::Clear(1.),
|
|
||||||
store: wgpu::StoreOp::Discard,
|
|
||||||
}),
|
|
||||||
stencil_ops: None,
|
|
||||||
}),
|
|
||||||
..Default::default()
|
..Default::default()
|
||||||
});
|
});
|
||||||
if args.show_axes {
|
if args.show_axes {
|
||||||
|
|
@ -403,26 +386,10 @@ impl Core {
|
||||||
self.queue.submit(std::iter::once(encoder.finish()));
|
self.queue.submit(std::iter::once(encoder.finish()));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_depth_buffer(device: &wgpu::Device, pixel_size: UVec2) -> wgpu::Texture {
|
/// Configures the renderer for a given target size.
|
||||||
device.create_texture(&wgpu::TextureDescriptor {
|
pub fn configure(&mut self, pixel_size: UVec2) {
|
||||||
label: Some("depth buffer"),
|
self.surface.configure(
|
||||||
size: wgpu::Extent3d {
|
&self.device,
|
||||||
width: pixel_size.x,
|
|
||||||
height: pixel_size.y,
|
|
||||||
depth_or_array_layers: 1,
|
|
||||||
},
|
|
||||||
mip_level_count: 1,
|
|
||||||
sample_count: 1,
|
|
||||||
dimension: wgpu::TextureDimension::D2,
|
|
||||||
format: DEPTH_FORMAT,
|
|
||||||
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
|
|
||||||
view_formats: &[],
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn configure_surface(surface: &wgpu::Surface, device: &wgpu::Device, pixel_size: UVec2) {
|
|
||||||
surface.configure(
|
|
||||||
device,
|
|
||||||
&wgpu::SurfaceConfiguration {
|
&wgpu::SurfaceConfiguration {
|
||||||
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_DST,
|
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_DST,
|
||||||
format: OUTPUT_FORMAT,
|
format: OUTPUT_FORMAT,
|
||||||
|
|
@ -436,12 +403,6 @@ impl Core {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Configures the renderer for a given target size.
|
|
||||||
pub fn configure(&mut self, pixel_size: UVec2) {
|
|
||||||
Self::configure_surface(&self.surface, &self.device, pixel_size);
|
|
||||||
self.depth = Self::create_depth_buffer(&self.device, pixel_size);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Redraws the entire surface.
|
/// Redraws the entire surface.
|
||||||
///
|
///
|
||||||
/// [`Self::configure`] must be called at least once before this.
|
/// [`Self::configure`] must be called at least once before this.
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,8 @@ impl MainWindow {
|
||||||
instance.create_surface(Arc::clone(&window))
|
instance.create_surface(Arc::clone(&window))
|
||||||
}))
|
}))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let core = Core::new(gpu, uvec2(1, 1));
|
let mut core = Core::new(gpu);
|
||||||
|
core.configure(uvec2(1, 1));
|
||||||
Self { window, core }
|
Self { window, core }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,6 @@ use bytemuck::{Pod, Zeroable, bytes_of, cast_slice};
|
||||||
use glam::{Mat4, Vec3};
|
use glam::{Mat4, Vec3};
|
||||||
use wgpu::util::DeviceExt as _;
|
use wgpu::util::DeviceExt as _;
|
||||||
|
|
||||||
use crate::render::{DEPTH_FORMAT, OUTPUT_FORMAT};
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, Pod, Zeroable)]
|
#[derive(Debug, Clone, Copy, Pod, Zeroable)]
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub struct LookParams {
|
pub struct LookParams {
|
||||||
|
|
@ -69,7 +67,7 @@ pub struct Pipeline {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Pipeline {
|
impl Pipeline {
|
||||||
pub fn new(device: &wgpu::Device) -> Self {
|
pub fn new(device: &wgpu::Device, format: wgpu::TextureFormat) -> Self {
|
||||||
let look_buf = device.create_buffer(&wgpu::BufferDescriptor {
|
let look_buf = device.create_buffer(&wgpu::BufferDescriptor {
|
||||||
label: None,
|
label: None,
|
||||||
size: size_of::<LookParams>() as u64,
|
size: size_of::<LookParams>() as u64,
|
||||||
|
|
@ -109,13 +107,7 @@ impl Pipeline {
|
||||||
topology: wgpu::PrimitiveTopology::TriangleList,
|
topology: wgpu::PrimitiveTopology::TriangleList,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
depth_stencil: Some(wgpu::DepthStencilState {
|
depth_stencil: None,
|
||||||
format: DEPTH_FORMAT,
|
|
||||||
depth_write_enabled: true,
|
|
||||||
depth_compare: wgpu::CompareFunction::LessEqual,
|
|
||||||
stencil: wgpu::StencilState::default(),
|
|
||||||
bias: wgpu::DepthBiasState::default(),
|
|
||||||
}),
|
|
||||||
multisample: wgpu::MultisampleState {
|
multisample: wgpu::MultisampleState {
|
||||||
count: 1,
|
count: 1,
|
||||||
mask: !0,
|
mask: !0,
|
||||||
|
|
@ -126,7 +118,7 @@ impl Pipeline {
|
||||||
entry_point: None,
|
entry_point: None,
|
||||||
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||||
targets: &[Some(wgpu::ColorTargetState {
|
targets: &[Some(wgpu::ColorTargetState {
|
||||||
format: OUTPUT_FORMAT,
|
format,
|
||||||
blend: Some(wgpu::BlendState::REPLACE),
|
blend: Some(wgpu::BlendState::REPLACE),
|
||||||
write_mask: wgpu::ColorWrites::ALL,
|
write_mask: wgpu::ColorWrites::ALL,
|
||||||
})],
|
})],
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,6 @@ use bytemuck::{Pod, Zeroable, bytes_of, cast_slice};
|
||||||
use glam::{Mat4, Vec3};
|
use glam::{Mat4, Vec3};
|
||||||
use wgpu::util::DeviceExt as _;
|
use wgpu::util::DeviceExt as _;
|
||||||
|
|
||||||
use crate::render::{DEPTH_FORMAT, OUTPUT_FORMAT};
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, Pod, Zeroable)]
|
#[derive(Debug, Clone, Copy, Pod, Zeroable)]
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub struct LookParams {
|
pub struct LookParams {
|
||||||
|
|
@ -51,7 +49,7 @@ pub struct Pipeline {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Pipeline {
|
impl Pipeline {
|
||||||
pub fn new(device: &wgpu::Device) -> Self {
|
pub fn new(device: &wgpu::Device, format: wgpu::TextureFormat) -> Self {
|
||||||
let look_buf = device.create_buffer(&wgpu::BufferDescriptor {
|
let look_buf = device.create_buffer(&wgpu::BufferDescriptor {
|
||||||
label: None,
|
label: None,
|
||||||
size: size_of::<LookParams>() as u64,
|
size: size_of::<LookParams>() as u64,
|
||||||
|
|
@ -91,17 +89,7 @@ impl Pipeline {
|
||||||
topology: wgpu::PrimitiveTopology::LineList,
|
topology: wgpu::PrimitiveTopology::LineList,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
depth_stencil: Some(wgpu::DepthStencilState {
|
depth_stencil: None,
|
||||||
format: DEPTH_FORMAT,
|
|
||||||
depth_write_enabled: true,
|
|
||||||
depth_compare: wgpu::CompareFunction::LessEqual,
|
|
||||||
stencil: wgpu::StencilState::default(),
|
|
||||||
bias: wgpu::DepthBiasState {
|
|
||||||
constant: -2,
|
|
||||||
slope_scale: 0.,
|
|
||||||
clamp: 0.,
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
multisample: wgpu::MultisampleState {
|
multisample: wgpu::MultisampleState {
|
||||||
count: 1,
|
count: 1,
|
||||||
mask: !0,
|
mask: !0,
|
||||||
|
|
@ -112,7 +100,7 @@ impl Pipeline {
|
||||||
entry_point: None,
|
entry_point: None,
|
||||||
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||||
targets: &[Some(wgpu::ColorTargetState {
|
targets: &[Some(wgpu::ColorTargetState {
|
||||||
format: OUTPUT_FORMAT,
|
format,
|
||||||
blend: Some(wgpu::BlendState::REPLACE),
|
blend: Some(wgpu::BlendState::REPLACE),
|
||||||
write_mask: wgpu::ColorWrites::ALL,
|
write_mask: wgpu::ColorWrites::ALL,
|
||||||
})],
|
})],
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,3 @@ pub mod faces;
|
||||||
pub mod lines;
|
pub mod lines;
|
||||||
|
|
||||||
static SIMPLE_SHADER: &str = include_str!("simple.wgsl");
|
static SIMPLE_SHADER: &str = include_str!("simple.wgsl");
|
||||||
|
|
||||||
pub const OUTPUT_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Bgra8UnormSrgb;
|
|
||||||
pub const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth24Plus;
|
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
namespace ffi {
|
namespace ffi {
|
||||||
extern "C" Core* rt4_viewport_create(xcb_connection_t* connection, std::uint32_t window, std::uint32_t width, std::uint32_t height);
|
extern "C" Core* rt4_viewport_create(xcb_connection_t* connection, std::uint32_t window);
|
||||||
extern "C" void rt4_viewport_destroy(Core* viewport);
|
extern "C" void rt4_viewport_destroy(Core* viewport);
|
||||||
extern "C" void rt4_viewport_configure(Core* viewport, std::uint32_t width, std::uint32_t height);
|
extern "C" void rt4_viewport_configure(Core* viewport, std::uint32_t width, std::uint32_t height);
|
||||||
extern "C" void rt4_viewport_redraw(Core* viewport, const RedrawArgs* args);
|
extern "C" void rt4_viewport_redraw(Core* viewport, const RedrawArgs* args);
|
||||||
|
|
@ -26,13 +26,13 @@ BoxCore& BoxCore::operator=(BoxCore&& b) {
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
BoxCore BoxCore::from_xcb(xcb_connection_t* connection, std::uint32_t window, std::uint32_t width, std::uint32_t height) {
|
BoxCore BoxCore::from_xcb(xcb_connection_t* connection, std::uint32_t window) {
|
||||||
if (!connection)
|
if (!connection)
|
||||||
throw std::logic_error("attempt to use a null connection");
|
throw std::logic_error("attempt to use a null connection");
|
||||||
if (!window)
|
if (!window)
|
||||||
throw std::logic_error("attempt to use a null window");
|
throw std::logic_error("attempt to use a null window");
|
||||||
BoxCore out;
|
BoxCore out;
|
||||||
out.ptr.ptr = ffi::rt4_viewport_create(connection, window, width, height);
|
out.ptr.ptr = ffi::rt4_viewport_create(connection, window);
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -69,7 +69,7 @@ public:
|
||||||
|
|
||||||
void reset();
|
void reset();
|
||||||
|
|
||||||
static BoxCore from_xcb(xcb_connection_t* connection, std::uint32_t window, std::uint32_t width, std::uint32_t height);
|
static BoxCore from_xcb(xcb_connection_t* connection, std::uint32_t window);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
MutCore ptr;
|
MutCore ptr;
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,12 @@
|
||||||
use std::{ffi::c_void, num::NonZero, ptr::NonNull};
|
use std::{ffi::c_void, num::NonZero, ptr::NonNull};
|
||||||
|
|
||||||
use glam::{UVec2, uvec2};
|
use glam::uvec2;
|
||||||
use photon_light::{Core, RedrawArgs, init_gpu_inner};
|
use photon_light::{Core, RedrawArgs, init_gpu_inner};
|
||||||
use raw_window_handle::{RawDisplayHandle, RawWindowHandle, XcbDisplayHandle, XcbWindowHandle};
|
use raw_window_handle::{RawDisplayHandle, RawWindowHandle, XcbDisplayHandle, XcbWindowHandle};
|
||||||
|
|
||||||
unsafe fn create_viewport(
|
unsafe fn create_viewport(
|
||||||
display: impl Into<RawDisplayHandle>,
|
display: impl Into<RawDisplayHandle>,
|
||||||
window: impl Into<RawWindowHandle>,
|
window: impl Into<RawWindowHandle>,
|
||||||
size: UVec2,
|
|
||||||
) -> Box<Core> {
|
) -> Box<Core> {
|
||||||
let target = wgpu::SurfaceTargetUnsafe::RawHandle {
|
let target = wgpu::SurfaceTargetUnsafe::RawHandle {
|
||||||
raw_display_handle: display.into(),
|
raw_display_handle: display.into(),
|
||||||
|
|
@ -17,19 +16,17 @@ unsafe fn create_viewport(
|
||||||
instance.create_surface_unsafe(target)
|
instance.create_surface_unsafe(target)
|
||||||
}))
|
}))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
Box::new(Core::new(gpu, size))
|
Box::new(Core::new(gpu))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[unsafe(no_mangle)]
|
#[unsafe(no_mangle)]
|
||||||
unsafe extern "C" fn rt4_viewport_create(
|
unsafe extern "C" fn rt4_viewport_create(
|
||||||
connection: NonNull<c_void>,
|
connection: NonNull<c_void>,
|
||||||
window: NonZero<u32>,
|
window: NonZero<u32>,
|
||||||
width: u32,
|
|
||||||
height: u32,
|
|
||||||
) -> Box<Core> {
|
) -> Box<Core> {
|
||||||
let display = XcbDisplayHandle::new(Some(connection), 0);
|
let display = XcbDisplayHandle::new(Some(connection), 0);
|
||||||
let window = XcbWindowHandle::new(window);
|
let window = XcbWindowHandle::new(window);
|
||||||
unsafe { create_viewport(display, window, uvec2(width, height)) }
|
unsafe { create_viewport(display, window) }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[unsafe(no_mangle)]
|
#[unsafe(no_mangle)]
|
||||||
|
|
|
||||||
|
|
@ -61,9 +61,9 @@ void Viewport::recreate() try {
|
||||||
|
|
||||||
fprintf(stderr, "connection %p, window %#08x\n", xcb_connection, x11_window);
|
fprintf(stderr, "connection %p, window %#08x\n", xcb_connection, x11_window);
|
||||||
|
|
||||||
const QSize device_size = size() * devicePixelRatio();
|
|
||||||
core.reset();
|
core.reset();
|
||||||
core = BoxCore::from_xcb(xcb_connection, x11_window, device_size.width(), device_size.height());
|
core = BoxCore::from_xcb(xcb_connection, x11_window);
|
||||||
|
updateSize();
|
||||||
} catch (const std::exception& e) {
|
} catch (const std::exception& e) {
|
||||||
fprintf(stderr, "failed to recreate the viewport: %s", e.what());
|
fprintf(stderr, "failed to recreate the viewport: %s", e.what());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user