Initial commit

This commit is contained in:
hodasemi 2020-11-23 18:41:14 +01:00
commit cf6eaa2f10
9 changed files with 283 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
Cargo.lock

45
.vscode/launch.json vendored Normal file
View file

@ -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}"
}
]
}

10
Cargo.toml Normal file
View file

@ -0,0 +1,10 @@
[package]
name = "fractal"
version = "0.1.0"
authors = ["hodasemi <michaelh.95@t-online.de>"]
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"] }

7
src/fractal.frag Normal file
View file

@ -0,0 +1,7 @@
#version 450
layout (location = 0) out vec4 color;
void main() {
color = vec4(1.0, 0.0, 0.0, 1.0);
}

BIN
src/fractal.frag.spv Normal file

Binary file not shown.

181
src/fractal.rs Normal file
View file

@ -0,0 +1,181 @@
use std::{mem, sync::Arc};
use context::{prelude::*, ContextObject};
pub struct Fractal {
context: Arc<Context>,
descriptor_set: Arc<DescriptorSet>,
pipeline: Arc<Pipeline>,
render_target: RenderTarget,
vertex_buffer: Arc<Buffer<[f32; 2]>>,
}
impl Fractal {
pub fn new(context: &Arc<Context>) -> VerboseResult<Arc<Self>> {
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<usize>,
) -> 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(())
}
}

7
src/fractal.vert Normal file
View file

@ -0,0 +1,7 @@
#version 450
layout (location = 0) in vec2 position;
void main() {
gl_Position = vec4(position, 0.0, 1.0);
}

BIN
src/fractal.vert.spv Normal file

Binary file not shown.

31
src/main.rs Normal file
View file

@ -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(())
}