start forward shader with vertex data
parent
75e6b20c3f
commit
154dd2b5dc
|
@ -23,7 +23,7 @@ rust_binary(
|
|||
"@io_bazel_rules_rust//tools/runfiles",
|
||||
],
|
||||
data = [
|
||||
"//engine/shaders:triangle_vert",
|
||||
"//engine/shaders:triangle_frag",
|
||||
"//engine/shaders:forward_vert",
|
||||
"//engine/shaders:forward_frag",
|
||||
],
|
||||
)
|
||||
|
|
|
@ -1,17 +1,17 @@
|
|||
load("//tools/bzl:glsl.bzl", "glsl_binary")
|
||||
|
||||
glsl_binary(
|
||||
name = "triangle_vert",
|
||||
name = "forward_vert",
|
||||
srcs = [
|
||||
"triangle.vert"
|
||||
"forward.vert"
|
||||
],
|
||||
visibility = ["//engine:__pkg__"],
|
||||
)
|
||||
|
||||
glsl_binary(
|
||||
name = "triangle_frag",
|
||||
name = "forward_frag",
|
||||
srcs = [
|
||||
"triangle.frag"
|
||||
"forward.frag"
|
||||
],
|
||||
visibility = ["//engine:__pkg__"],
|
||||
)
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
// vim: set ft=glsl:
|
||||
#version 450
|
||||
|
||||
layout(location = 0) in vec3 pos;
|
||||
layout(location = 1) in vec3 color;
|
||||
|
||||
layout(location = 0) out vec3 fragColor;
|
||||
|
||||
out gl_PerVertex {
|
||||
vec4 gl_Position;
|
||||
};
|
||||
|
||||
void main() {
|
||||
gl_Position = vec4(pos, 1.0);
|
||||
fragColor = color;
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
// vim: set ft=glsl:
|
||||
#version 450
|
||||
|
||||
layout(location = 0) out vec3 fragColor;
|
||||
|
||||
vec2 position[3] = vec2[](
|
||||
vec2(0.0, -0.5),
|
||||
vec2(0.5, 0.5),
|
||||
vec2(-0.5, 0.5)
|
||||
);
|
||||
|
||||
vec3 colors[3] = vec3[](
|
||||
vec3(1.0, 0.0, 0.0),
|
||||
vec3(0.0, 1.0, 0.0),
|
||||
vec3(0.0, 0.0, 1.0)
|
||||
);
|
||||
|
||||
void main() {
|
||||
gl_Position = vec4(position[gl_VertexIndex], 0.0, 1.0);
|
||||
fragColor = colors[gl_VertexIndex];
|
||||
}
|
|
@ -3,10 +3,11 @@ use std::time;
|
|||
use log;
|
||||
|
||||
use vulkano::command_buffer as vc;
|
||||
use vulkano::buffer as vb;
|
||||
use vulkano::instance as vi;
|
||||
use vulkano::swapchain as vs;
|
||||
use vulkano::framebuffer as vf;
|
||||
use vulkano::pipeline::vertex as vpv;
|
||||
use vulkano::pipeline as vp;
|
||||
use vulkano::sync::{FenceSignalFuture, GpuFuture};
|
||||
|
||||
mod binding;
|
||||
|
@ -40,6 +41,7 @@ pub struct Instance<WT> {
|
|||
|
||||
type FlipFuture<WT> = FenceSignalFuture<vs::PresentFuture<vc::CommandBufferExecFuture<vs::SwapchainAcquireFuture<WT>, Arc<vc::AutoCommandBuffer>>, WT>>;
|
||||
|
||||
|
||||
impl<WT: 'static + Send + Sync> Instance<WT> {
|
||||
pub fn new(name: String) -> Self {
|
||||
let ai = vi::ApplicationInfo {
|
||||
|
@ -99,8 +101,10 @@ impl<WT: 'static + Send + Sync> Instance<WT> {
|
|||
self.create_framebuffers();
|
||||
|
||||
let render_pass = self.render_pass.as_ref().unwrap().clone();
|
||||
let pipeline = shaders::pipeline_triangle(device.clone(), chain.dimensions(), render_pass);
|
||||
self.create_command_buffers(pipeline);
|
||||
let pipeline = shaders::pipeline_forward(device.clone(), chain.dimensions(), render_pass);
|
||||
let buffer = vb::cpu_access::CpuAccessibleBuffer::from_iter(device.clone(),
|
||||
vb::BufferUsage::vertex_buffer(), vertices().iter().cloned()).unwrap();
|
||||
self.create_command_buffers(pipeline, buffer);
|
||||
|
||||
self.previous_frame_end = None;
|
||||
self.armed = true;
|
||||
|
@ -188,18 +192,21 @@ impl<WT: 'static + Send + Sync> Instance<WT> {
|
|||
.collect::<Vec<_>>();
|
||||
}
|
||||
|
||||
fn create_command_buffers(&mut self, pipeline: Arc<shaders::ConcreteGraphicsPipeline>) {
|
||||
fn create_command_buffers(
|
||||
&mut self,
|
||||
pipeline: Arc<dyn vp::GraphicsPipelineAbstract + Send + Sync>,
|
||||
vertex_buffer: Arc<dyn vb::BufferAccess + Send + Sync>,
|
||||
) {
|
||||
let device = self.binding.as_ref().unwrap().device.clone();
|
||||
let qf = self.binding.as_ref().unwrap().graphics_queue.family();
|
||||
self.command_buffers = self.framebuffers.iter()
|
||||
.map(|framebuffer| {
|
||||
let vertices = vpv::BufferlessVertices { vertices: 3, instances: 1 };
|
||||
Arc::new(vc::AutoCommandBufferBuilder::primary_simultaneous_use(device.clone(), qf)
|
||||
.unwrap()
|
||||
.begin_render_pass(framebuffer.clone(), false, vec![[0.0, 0.0, 0.0, 1.0].into()])
|
||||
.unwrap()
|
||||
.draw(pipeline.clone(), &vc::DynamicState::none(),
|
||||
vertices, (), ())
|
||||
vec![vertex_buffer.clone()], (), ())
|
||||
.unwrap()
|
||||
.end_render_pass()
|
||||
.unwrap()
|
||||
|
@ -223,3 +230,24 @@ impl<WT: 'static + Send + Sync> Instance<WT> {
|
|||
}).expect("could not create debug callback")
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
struct Vertex {
|
||||
pos: [f32; 3],
|
||||
color: [f32; 3],
|
||||
}
|
||||
|
||||
impl Vertex {
|
||||
pub fn new(pos: [f32; 3], color: [f32; 3]) -> Self {
|
||||
Self { pos, color }
|
||||
}
|
||||
}
|
||||
vulkano::impl_vertex!(Vertex, pos, color);
|
||||
|
||||
fn vertices() -> [Vertex; 3] {
|
||||
[
|
||||
Vertex::new([0.0, -0.5, 0.0], [1.0, 1.0, 1.0]),
|
||||
Vertex::new([0.5, 0.5, 0.0], [0.0, 1.0, 0.0]),
|
||||
Vertex::new([-0.5, 0.5, 0.0], [0.0, 0.0, 1.])
|
||||
]
|
||||
}
|
||||
|
|
|
@ -6,7 +6,6 @@ use std::io::prelude::*;
|
|||
use std::sync::Arc;
|
||||
|
||||
use runfiles::Runfiles;
|
||||
use vulkano::descriptor as vD;
|
||||
use vulkano::descriptor::descriptor as vdd;
|
||||
use vulkano::descriptor::pipeline_layout as vdp;
|
||||
use vulkano::device as vd;
|
||||
|
@ -14,26 +13,26 @@ use vulkano::format::Format;
|
|||
use vulkano::framebuffer as vf;
|
||||
use vulkano::pipeline as vp;
|
||||
use vulkano::pipeline::shader as vps;
|
||||
use vulkano::pipeline::vertex as vpv;
|
||||
|
||||
pub type ConcreteGraphicsPipeline = vp::GraphicsPipeline<vpv::BufferlessDefinition, Box<dyn vD::PipelineLayoutAbstract + Send + Sync + 'static>, Arc<dyn vf::RenderPassAbstract + Send + Sync + 'static>>;
|
||||
|
||||
pub fn pipeline_triangle(
|
||||
pub fn pipeline_forward(
|
||||
device: Arc<vd::Device>,
|
||||
swap_chain_extent: [u32; 2],
|
||||
render_pass: Arc<dyn vf::RenderPassAbstract + Send + Sync>,
|
||||
) -> Arc<ConcreteGraphicsPipeline> {
|
||||
) -> Arc<dyn vp::GraphicsPipelineAbstract + Send + Sync> {
|
||||
let vertex = ShaderDefinition {
|
||||
name: "triangle_vert.spv".to_string(),
|
||||
name: "forward_vert.spv".to_string(),
|
||||
ty: vps::GraphicsShaderType::Vertex,
|
||||
inputs: vec![],
|
||||
inputs: vec![
|
||||
vps::ShaderInterfaceDefEntry { location: 0..1, format: Format::R32G32B32Sfloat, name: Some(Cow::Borrowed("pos")) },
|
||||
vps::ShaderInterfaceDefEntry { location: 1..2, format: Format::R32G32B32Sfloat, name: Some(Cow::Borrowed("color")) },
|
||||
],
|
||||
outputs: vec![
|
||||
vps::ShaderInterfaceDefEntry { location: 0..1, format: Format::R32G32B32Sfloat, name: Some(Cow::Borrowed("fragColor")) }
|
||||
],
|
||||
}.load_into(device.clone()).expect("could not load vertex shader");
|
||||
|
||||
let fragment = ShaderDefinition {
|
||||
name: "triangle_frag.spv".to_string(),
|
||||
name: "forward_frag.spv".to_string(),
|
||||
ty: vps::GraphicsShaderType::Fragment,
|
||||
inputs: vec![
|
||||
vps::ShaderInterfaceDefEntry { location: 0..1, format: Format::R32G32B32Sfloat, name: Some(Cow::Borrowed("fragColor")) }
|
||||
|
@ -51,7 +50,7 @@ pub fn pipeline_triangle(
|
|||
};
|
||||
|
||||
Arc::new(vp::GraphicsPipeline::start()
|
||||
.vertex_input(vpv::BufferlessDefinition {})
|
||||
.vertex_input_single_buffer::<super::Vertex>()
|
||||
.vertex_shader(vertex.entry_point(), ())
|
||||
.triangle_list()
|
||||
.primitive_restart(false)
|
||||
|
|
Loading…
Reference in New Issue