This commit is contained in:
2025-01-15 18:59:01 +00:00
parent 414677428e
commit 53b319b535
2 changed files with 184 additions and 159 deletions
+115 -87
View File
@@ -1,7 +1,7 @@
#![feature(variant_count)] #![feature(variant_count)]
use std::{ use std::{
error::Error, error::Error,
fs::{File, remove_file, rename}, fs::{remove_file, rename, File},
io::{Cursor, Read, Write}, io::{Cursor, Read, Write},
path::{Path, PathBuf}, path::{Path, PathBuf},
sync::Arc, sync::Arc,
@@ -19,42 +19,38 @@ const MSAA_SAMPLES_ACTUAL: u32 = if MSAA_ENABLE { MSAA_SAMPLES } else { 1 };
use bytemuck::{Pod, Zeroable}; use bytemuck::{Pod, Zeroable};
use egui_winit_vulkano::{Gui, GuiConfig}; use egui_winit_vulkano::{Gui, GuiConfig};
use foldhash::{HashMap, HashMapExt, HashSet}; use foldhash::{HashMap, HashMapExt, HashSet};
use glam::{self, EulerRot, Mat3, Mat4, Vec3, vec3}; use glam::{self, vec3, EulerRot, Mat3, Mat4, Vec3};
use instruction_set::InputTypes; use instruction_set::InputTypes;
use log::{error, info, trace}; use log::{error, info, trace};
use rayon::prelude::*; use rayon::prelude::*;
use simplelog::{CombinedLogger, Config, TermLogger, WriteLogger}; use simplelog::{CombinedLogger, Config, TermLogger, WriteLogger};
use vulkano::{ use vulkano::{
Validated, Version, VulkanError, VulkanLibrary,
buffer::{ buffer::{
Buffer, BufferCreateInfo, BufferUsage, Subbuffer,
allocator::{SubbufferAllocator, SubbufferAllocatorCreateInfo}, allocator::{SubbufferAllocator, SubbufferAllocatorCreateInfo},
Buffer, BufferCreateInfo, BufferUsage, Subbuffer,
}, },
command_buffer::{ command_buffer::{
AutoCommandBufferBuilder, CommandBufferUsage, CopyBufferInfo, PrimaryCommandBufferAbstract, allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder, CommandBufferUsage,
RenderPassBeginInfo, SubpassBeginInfo, SubpassContents, CopyBufferInfo, PrimaryCommandBufferAbstract, RenderPassBeginInfo, SubpassBeginInfo,
allocator::StandardCommandBufferAllocator, SubpassContents,
}, },
descriptor_set::{ descriptor_set::{
DescriptorSet, WriteDescriptorSet, allocator::StandardDescriptorSetAllocator, allocator::StandardDescriptorSetAllocator, DescriptorSet, WriteDescriptorSet,
}, },
device::{ device::{
Device, DeviceCreateInfo, DeviceExtensions, DeviceFeatures, DeviceOwned, Queue, physical::PhysicalDeviceType, Device, DeviceCreateInfo, DeviceExtensions, DeviceFeatures,
QueueCreateInfo, QueueFlags, physical::PhysicalDeviceType, DeviceOwned, Queue, QueueCreateInfo, QueueFlags,
}, },
format::Format, format::Format,
image::{Image, ImageCreateInfo, ImageType, ImageUsage, view::ImageView}, image::{view::ImageView, Image, ImageCreateInfo, ImageType, ImageUsage},
instance::{Instance, InstanceCreateInfo, InstanceExtensions}, instance::{Instance, InstanceCreateInfo, InstanceExtensions},
memory::allocator::{ memory::allocator::{
AllocationCreateInfo, MemoryAllocatePreference, MemoryTypeFilter, StandardMemoryAllocator, AllocationCreateInfo, MemoryAllocatePreference, MemoryTypeFilter, StandardMemoryAllocator,
}, },
pipeline::{ pipeline::{
ComputePipeline, DynamicState, GraphicsPipeline, Pipeline, PipelineBindPoint,
PipelineLayout, PipelineShaderStageCreateInfo,
cache::{PipelineCache, PipelineCacheCreateInfo}, cache::{PipelineCache, PipelineCacheCreateInfo},
compute::ComputePipelineCreateInfo, compute::ComputePipelineCreateInfo,
graphics::{ graphics::{
GraphicsPipelineCreateInfo,
color_blend::{ColorBlendAttachmentState, ColorBlendState}, color_blend::{ColorBlendAttachmentState, ColorBlendState},
depth_stencil::{DepthState, DepthStencilState}, depth_stencil::{DepthState, DepthStencilState},
input_assembly::InputAssemblyState, input_assembly::InputAssemblyState,
@@ -62,16 +58,20 @@ use vulkano::{
rasterization::{CullMode, FrontFace, RasterizationState}, rasterization::{CullMode, FrontFace, RasterizationState},
vertex_input::{Vertex, VertexDefinition}, vertex_input::{Vertex, VertexDefinition},
viewport::{Viewport, ViewportState}, viewport::{Viewport, ViewportState},
GraphicsPipelineCreateInfo,
}, },
layout::PipelineDescriptorSetLayoutCreateInfo, layout::PipelineDescriptorSetLayoutCreateInfo,
ComputePipeline, DynamicState, GraphicsPipeline, Pipeline, PipelineBindPoint,
PipelineLayout, PipelineShaderStageCreateInfo,
}, },
render_pass::{Framebuffer, FramebufferCreateInfo, RenderPass, Subpass}, render_pass::{Framebuffer, FramebufferCreateInfo, RenderPass, Subpass},
shader::{ShaderModule, SpecializationConstant}, shader::{ShaderModule, SpecializationConstant},
swapchain::{ swapchain::{
PresentMode, Surface, SurfaceInfo, Swapchain, SwapchainCreateInfo, SwapchainPresentInfo, acquire_next_image, PresentMode, Surface, SurfaceInfo, Swapchain, SwapchainCreateInfo,
acquire_next_image, SwapchainPresentInfo,
}, },
sync::{self, GpuFuture}, sync::{self, GpuFuture},
Validated, Version, VulkanError, VulkanLibrary,
}; };
use winit::{ use winit::{
application::ApplicationHandler, application::ApplicationHandler,
@@ -204,7 +204,9 @@ impl App {
let library = VulkanLibrary::new().expect("Vulkan is not installed???"); let library = VulkanLibrary::new().expect("Vulkan is not installed???");
let required_extensions = Surface::required_extensions(event_loop).unwrap(); let required_extensions = Surface::required_extensions(event_loop).unwrap();
let instance = Instance::new(library, InstanceCreateInfo { let instance = Instance::new(
library,
InstanceCreateInfo {
enabled_extensions: InstanceExtensions { enabled_extensions: InstanceExtensions {
ext_surface_maintenance1: true, ext_surface_maintenance1: true,
..required_extensions ..required_extensions
@@ -214,7 +216,8 @@ impl App {
application_name: Some(env!("CARGO_PKG_NAME").to_owned()), application_name: Some(env!("CARGO_PKG_NAME").to_owned()),
application_version: app_version(), application_version: app_version(),
..Default::default() ..Default::default()
}) },
)
.unwrap(); .unwrap();
let mut device_extensions = DeviceExtensions { let mut device_extensions = DeviceExtensions {
@@ -288,7 +291,9 @@ impl App {
device_extensions.khr_dynamic_rendering = true; device_extensions.khr_dynamic_rendering = true;
} }
let (device, mut queues) = Device::new(physical_device, DeviceCreateInfo { let (device, mut queues) = Device::new(
physical_device,
DeviceCreateInfo {
enabled_extensions: device_extensions, enabled_extensions: device_extensions,
queue_create_infos: vec![ queue_create_infos: vec![
QueueCreateInfo { QueueCreateInfo {
@@ -315,7 +320,8 @@ impl App {
..DeviceFeatures::empty() ..DeviceFeatures::empty()
}, },
..Default::default() ..Default::default()
}) },
)
.expect("Unable to initialize device"); .expect("Unable to initialize device");
let graphics_queue = queues.next().expect("Unable to retrieve queues"); let graphics_queue = queues.next().expect("Unable to retrieve queues");
@@ -331,13 +337,15 @@ impl App {
Default::default(), Default::default(),
)); ));
let uniform_buffer_allocator = let uniform_buffer_allocator = SubbufferAllocator::new(
SubbufferAllocator::new(memory_allocator.clone(), SubbufferAllocatorCreateInfo { memory_allocator.clone(),
SubbufferAllocatorCreateInfo {
buffer_usage: BufferUsage::UNIFORM_BUFFER | BufferUsage::STORAGE_BUFFER, buffer_usage: BufferUsage::UNIFORM_BUFFER | BufferUsage::STORAGE_BUFFER,
memory_type_filter: MemoryTypeFilter::PREFER_DEVICE memory_type_filter: MemoryTypeFilter::PREFER_DEVICE
| MemoryTypeFilter::HOST_SEQUENTIAL_WRITE, | MemoryTypeFilter::HOST_SEQUENTIAL_WRITE,
..Default::default() ..Default::default()
}); },
);
let pipeline_cache = get_pipeline_cache(device.clone()); let pipeline_cache = get_pipeline_cache(device.clone());
@@ -487,10 +495,13 @@ impl ApplicationHandler for App {
let surface_capabilities = self let surface_capabilities = self
.device .device
.physical_device() .physical_device()
.surface_capabilities(&surface, SurfaceInfo { .surface_capabilities(
&surface,
SurfaceInfo {
present_mode: Some(present_mode), present_mode: Some(present_mode),
..Default::default() ..Default::default()
}) },
)
.unwrap(); .unwrap();
let (image_format, _) = self let (image_format, _) = self
@@ -499,7 +510,10 @@ impl ApplicationHandler for App {
.surface_formats(&surface, Default::default()) .surface_formats(&surface, Default::default())
.unwrap()[0]; .unwrap()[0];
Swapchain::new(self.device.clone(), surface.clone(), SwapchainCreateInfo { Swapchain::new(
self.device.clone(),
surface.clone(),
SwapchainCreateInfo {
min_image_count: 3 min_image_count: 3
.max(surface_capabilities.min_image_count) .max(surface_capabilities.min_image_count)
.min(surface_capabilities.max_image_count.unwrap_or(u32::MAX)), .min(surface_capabilities.max_image_count.unwrap_or(u32::MAX)),
@@ -518,7 +532,8 @@ impl ApplicationHandler for App {
present_mode, present_mode,
..Default::default() ..Default::default()
}) },
)
.unwrap() .unwrap()
}; };
@@ -1034,19 +1049,19 @@ impl App {
fake_csg.push(CSG { fake_csg.push(CSG {
name: format!("fuzz_{i}"), name: format!("fuzz_{i}"),
parts: vec![ parts: vec![
CSGPart::opcode(InstructionSet::OPMulVec3Float, vec![ CSGPart::opcode(
Inputs::Variable, InstructionSet::OPMulVec3Float,
Inputs::Float(0.9), vec![Inputs::Variable, Inputs::Float(0.9)],
]), ),
CSGPart::opcode(InstructionSet::OPDupVec3, vec![Inputs::Variable]), CSGPart::opcode(InstructionSet::OPDupVec3, vec![Inputs::Variable]),
CSGPart::opcode(InstructionSet::OPAddVec3Vec3, vec![ CSGPart::opcode(
Inputs::Variable, InstructionSet::OPAddVec3Vec3,
Inputs::Vec3(vec3(-0.7, i as f32, -0.7)), vec![Inputs::Variable, Inputs::Vec3(vec3(-0.7, i as f32, -0.7))],
]), ),
CSGPart::opcode(InstructionSet::OPSDFSphere, vec![ CSGPart::opcode(
Inputs::Float(0.5), InstructionSet::OPSDFSphere,
Inputs::Variable, vec![Inputs::Float(0.5), Inputs::Variable],
]), ),
//CSGPart::opcode(InstructionSet::OPNop,vec![]), //CSGPart::opcode(InstructionSet::OPNop,vec![]),
//CSGPart::opcode(InstructionSet::OPDupVec3, //CSGPart::opcode(InstructionSet::OPDupVec3,
// vec![Inputs::Variable]), // vec![Inputs::Variable]),
@@ -1056,21 +1071,21 @@ impl App {
// CSGPart::opcode(InstructionSet::OPAddVec3Vec3, // CSGPart::opcode(InstructionSet::OPAddVec3Vec3,
// vec![Inputs::Variable, Inputs::Vec3([-0.0, -0.0, // vec![Inputs::Variable, Inputs::Vec3([-0.0, -0.0,
// -0.0].into())]), // -0.0].into())]),
CSGPart::opcode(InstructionSet::OPSDFSphere, vec![ CSGPart::opcode(
Inputs::Float(1.2), InstructionSet::OPSDFSphere,
Inputs::Variable, vec![Inputs::Float(1.2), Inputs::Variable],
]), ),
//CSGPart::opcode(InstructionSet::OPSDFBox, //CSGPart::opcode(InstructionSet::OPSDFBox,
// vec![Inputs::Variable, // vec![Inputs::Variable,
// Inputs::Vec3([0.7, 0.7, 0.7].into())]), // Inputs::Vec3([0.7, 0.7, 0.7].into())]),
CSGPart::opcode(InstructionSet::OPMinFloat, vec![ CSGPart::opcode(
Inputs::Variable, InstructionSet::OPMinFloat,
Inputs::Variable, vec![Inputs::Variable, Inputs::Variable],
]), ),
CSGPart::opcode(InstructionSet::OPDivFloatFloat, vec![ CSGPart::opcode(
Inputs::Variable, InstructionSet::OPDivFloatFloat,
Inputs::Float(0.9), vec![Inputs::Variable, Inputs::Float(0.9)],
]), ),
CSGPart::opcode(InstructionSet::OPStop, vec![Inputs::Variable]), CSGPart::opcode(InstructionSet::OPStop, vec![Inputs::Variable]),
], ],
pos: Vec3::ZERO, pos: Vec3::ZERO,
@@ -1106,7 +1121,7 @@ impl App {
for (i, val) in scene.iter().skip(1).take(32).enumerate() { for (i, val) in scene.iter().skip(1).take(32).enumerate() {
for (y, code) in val.iter().enumerate() { for (y, code) in val.iter().enumerate() {
spec_constants.insert( spec_constants.insert(
(1000 + (i * 8) + (y * 2) + 0) as u32, (1000 + (i * 8) + (y * 2)) as u32,
SpecializationConstant::U32(code & 0xFFFF), SpecializationConstant::U32(code & 0xFFFF),
); );
spec_constants.insert( spec_constants.insert(
@@ -1324,10 +1339,13 @@ impl App {
} }
builder builder
.next_subpass(Default::default(), SubpassBeginInfo { .next_subpass(
Default::default(),
SubpassBeginInfo {
contents: SubpassContents::SecondaryCommandBuffers, contents: SubpassContents::SecondaryCommandBuffers,
..Default::default() ..Default::default()
}) },
)
.unwrap() .unwrap()
.execute_commands(guicb) .execute_commands(guicb)
.unwrap() .unwrap()
@@ -1420,7 +1438,9 @@ fn framebuffer_generation(
.map(|image| { .map(|image| {
let view = ImageView::new_default(image.clone()).unwrap(); let view = ImageView::new_default(image.clone()).unwrap();
Framebuffer::new(render_pass.clone(), FramebufferCreateInfo { Framebuffer::new(
render_pass.clone(),
FramebufferCreateInfo {
attachments: if MSAA_ENABLE { attachments: if MSAA_ENABLE {
vec![ vec![
intermediary.as_ref().unwrap().clone(), intermediary.as_ref().unwrap().clone(),
@@ -1431,7 +1451,8 @@ fn framebuffer_generation(
vec![view, depth_buffer.clone()] vec![view, depth_buffer.clone()]
}, },
..Default::default() ..Default::default()
}) },
)
.unwrap() .unwrap()
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
@@ -1444,7 +1465,7 @@ fn pipeline_recompile(
cache: &Arc<PipelineCache>, cache: &Arc<PipelineCache>,
shader_modules: &ShaderModules, shader_modules: &ShaderModules,
debug: &PreviousDebug, debug: &PreviousDebug,
scene: &Vec<[u32; 4]>, scene: &[[u32; 4]],
viewport: Option<&Viewport>, viewport: Option<&Viewport>,
) -> (Arc<GraphicsPipeline>, Arc<GraphicsPipeline>) { ) -> (Arc<GraphicsPipeline>, Arc<GraphicsPipeline>) {
let device = cache.device(); let device = cache.device();
@@ -1494,7 +1515,7 @@ fn pipeline_recompile(
for (i, val) in scene.iter().skip(1).take(32).enumerate() { for (i, val) in scene.iter().skip(1).take(32).enumerate() {
for (y, code) in val.iter().enumerate() { for (y, code) in val.iter().enumerate() {
spec_constants.insert( spec_constants.insert(
(1000 + (i * 8) + (y * 2) + 0) as u32, (1000 + (i * 8) + (y * 2)) as u32,
SpecializationConstant::U32(code & 0xFFFF), SpecializationConstant::U32(code & 0xFFFF),
); );
spec_constants.insert( spec_constants.insert(
@@ -1799,19 +1820,19 @@ fn object_size_dependent_setup(
]; ];
let example = vec![ let example = vec![
CSGPart::opcode(InstructionSet::OPMulVec3Float, vec![ CSGPart::opcode(
Inputs::Variable, InstructionSet::OPMulVec3Float,
Inputs::Float(0.9), vec![Inputs::Variable, Inputs::Float(0.9)],
]), ),
CSGPart::opcode(InstructionSet::OPDupVec3, vec![Inputs::Variable]), CSGPart::opcode(InstructionSet::OPDupVec3, vec![Inputs::Variable]),
CSGPart::opcode(InstructionSet::OPAddVec3Vec3, vec![ CSGPart::opcode(
Inputs::Variable, InstructionSet::OPAddVec3Vec3,
Inputs::Vec3([-0.7, -1.2, -0.7].into()), vec![Inputs::Variable, Inputs::Vec3([-0.7, -1.2, -0.7].into())],
]), ),
CSGPart::opcode(InstructionSet::OPSDFSphere, vec![ CSGPart::opcode(
Inputs::Float(0.5), InstructionSet::OPSDFSphere,
Inputs::Variable, vec![Inputs::Float(0.5), Inputs::Variable],
]), ),
//CSGPart::opcode(InstructionSet::OPNop,vec![]), //CSGPart::opcode(InstructionSet::OPNop,vec![]),
//CSGPart::opcode(InstructionSet::OPDupVec3, vec![Inputs::Variable]), //CSGPart::opcode(InstructionSet::OPDupVec3, vec![Inputs::Variable]),
//CSGPart::opcode(InstructionSet::OPAddVec3Vec3, vec![Inputs::Variable, //CSGPart::opcode(InstructionSet::OPAddVec3Vec3, vec![Inputs::Variable,
@@ -1820,20 +1841,20 @@ fn object_size_dependent_setup(
// Inputs::Vec3([-0.0, -0.0, -0.0].into())]), // Inputs::Vec3([-0.0, -0.0, -0.0].into())]),
// CSGPart::opcode(InstructionSet::OPSDFSphere,vec![Inputs::Float(1.2), // CSGPart::opcode(InstructionSet::OPSDFSphere,vec![Inputs::Float(1.2),
// Inputs::Variable]), // Inputs::Variable]),
CSGPart::opcode(InstructionSet::OPSDFTorus, vec![ CSGPart::opcode(
Inputs::Vec2([0.7, 0.4].into()), InstructionSet::OPSDFTorus,
Inputs::Variable, vec![Inputs::Vec2([0.7, 0.4].into()), Inputs::Variable],
]), ),
//CSGPart::opcode(InstructionSet::OPSDFBox, vec![Inputs::Vec3([0.7, 0.4, 0.7].into()), //CSGPart::opcode(InstructionSet::OPSDFBox, vec![Inputs::Vec3([0.7, 0.4, 0.7].into()),
// Inputs::Variable]), // Inputs::Variable]),
CSGPart::opcode(InstructionSet::OPMinFloat, vec![ CSGPart::opcode(
Inputs::Variable, InstructionSet::OPMinFloat,
Inputs::Variable, vec![Inputs::Variable, Inputs::Variable],
]), ),
CSGPart::opcode(InstructionSet::OPDivFloatFloat, vec![ CSGPart::opcode(
Inputs::Variable, InstructionSet::OPDivFloatFloat,
Inputs::Float(0.9), vec![Inputs::Variable, Inputs::Float(0.9)],
]), ),
CSGPart::opcode(InstructionSet::OPStop, vec![Inputs::Variable]), CSGPart::opcode(InstructionSet::OPStop, vec![Inputs::Variable]),
]; ];
@@ -2066,11 +2087,15 @@ fn object_size_dependent_setup(
) )
.unwrap(); .unwrap();
let staging = SubbufferAllocator::new(allocator.clone(), SubbufferAllocatorCreateInfo { let staging = SubbufferAllocator::new(
allocator.clone(),
SubbufferAllocatorCreateInfo {
buffer_usage: BufferUsage::TRANSFER_SRC, buffer_usage: BufferUsage::TRANSFER_SRC,
memory_type_filter: MemoryTypeFilter::PREFER_HOST | MemoryTypeFilter::HOST_SEQUENTIAL_WRITE, memory_type_filter: MemoryTypeFilter::PREFER_HOST
| MemoryTypeFilter::HOST_SEQUENTIAL_WRITE,
..Default::default() ..Default::default()
}); },
);
let csg_scene = gpu_buffer( let csg_scene = gpu_buffer(
scene.clone(), scene.clone(),
@@ -2197,10 +2222,13 @@ fn get_pipeline_cache(device: Arc<Device>) -> Arc<PipelineCache> {
}; };
unsafe { unsafe {
PipelineCache::new(device, PipelineCacheCreateInfo { PipelineCache::new(
device,
PipelineCacheCreateInfo {
initial_data, initial_data,
..Default::default() ..Default::default()
}) },
)
} }
.unwrap() .unwrap()
} }
+1 -4
View File
@@ -346,16 +346,13 @@ pub(crate) fn load_csg(
.map(|inpart| { .map(|inpart| {
let ty = inpart.get("type").ok_or("no type!")?.as_str(); let ty = inpart.get("type").ok_or("no type!")?.as_str();
let csgpart = CSGPart::opcode(InstructionSet::OPNop, vec![]); let csgpart = CSGPart::opcode(InstructionSet::OPNop, vec![]);
match ty { if ty == "sphere" {
"sphere" => {
let _blend = get_f32(inpart, "blend")?; let _blend = get_f32(inpart, "blend")?;
let _shell = get_percentage(inpart, "shell%")?; let _shell = get_percentage(inpart, "shell%")?;
let _power = get_f32_default(inpart, "power", 2.)?; let _power = get_f32_default(inpart, "power", 2.)?;
let _rgb = get_rgb(inpart)?; let _rgb = get_rgb(inpart)?;
let _trs = get_trs(inpart)?; let _trs = get_trs(inpart)?;
let _half = get_half(inpart)?; let _half = get_half(inpart)?;
},
_ => {}, //return Err("unknown type of csg".to_owned()),
} }
Ok(csgpart) Ok(csgpart)
}) })