commit cf6eaa2f1088bef0f4c1756efaaec74cb6b1f269 Author: hodasemi Date: Mon Nov 23 18:41:14 2020 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..869df07 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..751177c --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,45 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Debug executable 'fractal'", + "cargo": { + "args": [ + "build", + "--bin=fractal", + "--package=fractal" + ], + "filter": { + "name": "fractal", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug unit tests in executable 'fractal'", + "cargo": { + "args": [ + "test", + "--no-run", + "--bin=fractal", + "--package=fractal" + ], + "filter": { + "name": "fractal", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + } + ] +} \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..0601e83 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "fractal" +version = "0.1.0" +authors = ["hodasemi "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +context = { git = "http://gavania.de:80/Gavania/Context.git", features = ["bundle_sdl2"] } diff --git a/src/fractal.frag b/src/fractal.frag new file mode 100644 index 0000000..2f88832 --- /dev/null +++ b/src/fractal.frag @@ -0,0 +1,7 @@ +#version 450 + +layout (location = 0) out vec4 color; + +void main() { + color = vec4(1.0, 0.0, 0.0, 1.0); +} \ No newline at end of file diff --git a/src/fractal.frag.spv b/src/fractal.frag.spv new file mode 100644 index 0000000..4caadfa Binary files /dev/null and b/src/fractal.frag.spv differ diff --git a/src/fractal.rs b/src/fractal.rs new file mode 100644 index 0000000..41cff22 --- /dev/null +++ b/src/fractal.rs @@ -0,0 +1,181 @@ +use std::{mem, sync::Arc}; + +use context::{prelude::*, ContextObject}; + +pub struct Fractal { + context: Arc, + + descriptor_set: Arc, + pipeline: Arc, + render_target: RenderTarget, + vertex_buffer: Arc>, +} + +impl Fractal { + pub fn new(context: &Arc) -> VerboseResult> { + let render_core = context.render_core(); + + let render_target = RenderTarget::builder() + .add_sub_pass( + SubPass::builder(render_core.width(), render_core.height()) + .set_prepared_targets( + render_core.images()?.single()?, + 0, + [0.0, 0.0, 0.0, 0.0], + true, + ) + .build(context.device(), context.queue())?, + ) + .build(context.device())?; + + let descriptor_layout = DescriptorSetLayout::builder() + .add_layout_binding( + 0, + VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, + VK_SHADER_STAGE_FRAGMENT_BIT, + 0, + ) + .build(context.device().clone())?; + + let pipeline_layout = PipelineLayout::builder() + .add_descriptor_set_layout(&descriptor_layout) + .build(context.device().clone())?; + + let pipeline = Pipeline::new_graphics() + .default_rasterization(VK_CULL_MODE_FRONT_BIT, VK_FRONT_FACE_COUNTER_CLOCKWISE) + .default_multisample(VK_SAMPLE_COUNT_1_BIT) + .input_assembly(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, false) + .default_color_blend(vec![VkPipelineColorBlendAttachmentState::default()]) + .set_vertex_shader( + ShaderModule::from_slice( + context.device().clone(), + include_bytes!("fractal.vert.spv"), + ShaderType::Vertex, + )?, + vec![VkVertexInputBindingDescription { + binding: 0, + inputRate: VK_VERTEX_INPUT_RATE_VERTEX, + stride: mem::size_of::<[f32; 2]>() as u32, + }], + vec![VkVertexInputAttributeDescription { + location: 0, + binding: 0, + format: VK_FORMAT_R32G32_SFLOAT, + offset: 0, + }], + ) + .set_fragment_shader(ShaderModule::from_slice( + context.device().clone(), + include_bytes!("fractal.frag.spv"), + ShaderType::Fragment, + )?) + .add_viewport(VkViewport { + x: 0.0, + y: 0.0, + width: render_core.width() as f32, + height: render_core.height() as f32, + minDepth: 0.0, + maxDepth: 1.0, + }) + .add_scissor(VkRect2D { + offset: VkOffset2D { x: 0, y: 0 }, + extent: VkExtent2D { + width: render_core.width(), + height: render_core.height(), + }, + }) + .build( + context.device().clone(), + &pipeline_layout, + render_target.render_pass(), + 0, + )?; + + let descriptor_set = DescriptorPool::builder() + .set_layout(descriptor_layout) + .build(context.device().clone())? + .prepare_set() + .allocate()?; + + let vertex_buffer = Buffer::builder() + .set_data(&[ + [-1.0, -1.0], + [1.0, -1.0], + [1.0, 1.0], + [1.0, 1.0], + [-1.0, 1.0], + [-1.0, -1.0], + ]) + .set_memory_usage(MemoryUsage::CpuOnly) + .set_usage(VK_BUFFER_USAGE_VERTEX_BUFFER_BIT) + .build(context.device().clone())?; + + Ok(Arc::new(Self { + context: context.clone(), + + descriptor_set, + pipeline, + render_target, + vertex_buffer, + })) + } +} + +impl ContextObject for Fractal { + fn name(&self) -> &str { + "Fractal" + } + + fn update(&self) -> VerboseResult<()> { + Ok(()) + } + + fn event(&self, event: Event) -> VerboseResult<()> { + match event { + Event::KeyDown(key) => match key { + Keycode::Escape => self.context.close()?, + + _ => (), + }, + + _ => (), + } + + Ok(()) + } +} + +impl TScene for Fractal { + fn update(&self) -> VerboseResult<()> { + Ok(()) + } + + fn process( + &self, + buffer_recorder: &mut CommandBufferRecorder<'_>, + indices: &TargetMode, + ) -> VerboseResult<()> { + match indices { + TargetMode::Single(index) => { + self.render_target + .begin(buffer_recorder, VK_SUBPASS_CONTENTS_INLINE, *index); + + buffer_recorder.bind_pipeline(&self.pipeline)?; + buffer_recorder.bind_descriptor_sets_minimal(&[&self.descriptor_set])?; + buffer_recorder.bind_vertex_buffer(&self.vertex_buffer); + buffer_recorder.draw_complete_single_instance(self.vertex_buffer.size() as u32); + + self.render_target.end(buffer_recorder); + } + TargetMode::Stereo(_, _) => unimplemented!(), + } + + Ok(()) + } + + fn resize(&self) -> VerboseResult<()> { + println!("resize of FB is still missing"); + + Ok(()) + } +} diff --git a/src/fractal.vert b/src/fractal.vert new file mode 100644 index 0000000..b79bedd --- /dev/null +++ b/src/fractal.vert @@ -0,0 +1,7 @@ +#version 450 + +layout (location = 0) in vec2 position; + +void main() { + gl_Position = vec4(position, 0.0, 1.0); +} \ No newline at end of file diff --git a/src/fractal.vert.spv b/src/fractal.vert.spv new file mode 100644 index 0000000..a875946 Binary files /dev/null and b/src/fractal.vert.spv differ diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..db9e708 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,31 @@ +use context::prelude::*; + +mod fractal; + +use fractal::Fractal; + +fn main() -> VerboseResult<()> { + let context = Context::new() + .set_vulkan_debug_info(VulkanDebugInfo { + debugging: true, + steam_layer: false, + verbose: false, + renderdoc: false, + use_util: false, + }) + .set_render_core_info( + VK_FORMAT_R8G8B8_UNORM, + VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, + true, + ) + .build()?; + + let fractal = Fractal::new(&context)?; + + context.set_context_object(Some(fractal.clone()))?; + context.render_core().add_scene(fractal)?; + + context.run()?; + + Ok(()) +}