Skip to content

Instantly share code, notes, and snippets.

@pontjho
Created July 1, 2019 20:41
Show Gist options
  • Save pontjho/5cf3dc02056d0691b0d963e0949edc58 to your computer and use it in GitHub Desktop.
Save pontjho/5cf3dc02056d0691b0d963e0949edc58 to your computer and use it in GitHub Desktop.
[package]
name = "stackoverflow"
version = "0.1.0"
authors = ["xxx"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
gfx = "0.18.1"
gfx_window_glutin = "0.31.0"
glutin = "0.21.0"
#[macro_use] extern crate gfx;
extern crate gfx_window_glutin;
extern crate glutin;
use gfx::traits::FactoryExt;
use gfx::Device;
use gfx_window_glutin as gfx_glutin;
pub type ColorFormat = gfx::format::Srgba8;
pub type DepthFormat = gfx::format::DepthStencil;
gfx_defines! {
vertex Vertex {
pos: [f32; 2] = "a_Pos",
color: [f32; 3] = "a_Color",
}
constant MyConst {
valoo: i32 = "my_val",
}
pipeline pipe {
my_const: gfx::ConstantBuffer<MyConst> = "my_const",
vbuf: gfx::VertexBuffer<Vertex> = (),
out: gfx::RenderTarget<ColorFormat> = "Target0",
}
}
const BLACK: [f32; 4] = [0.0, 0.0, 0.0, 1.0];
const WHITE: [f32; 3] = [1.0, 1.0, 1.0];
const SQUARE: [Vertex; 3] = [
Vertex { pos: [0.5, -0.5], color: WHITE },
Vertex { pos: [-0.5, -0.5], color: WHITE },
Vertex { pos: [-0.5, 0.5], color: WHITE }
];
pub fn main() {
let CONSTS: [MyConst; 3] = [
MyConst { valoo: 5 },
MyConst { valoo: 3 },
MyConst { valoo: 1 }
];
let mut events_loop = glutin::EventsLoop::new();
let builder = glutin::WindowBuilder::new()
.with_title("Square Toy".to_string())
.with_dimensions(glutin::dpi::LogicalSize::new(800., 600.));
let context = glutin::ContextBuilder::new()
.with_vsync(true);
let (window, mut device, mut factory, mut main_color, mut main_depth) =
gfx_window_glutin::init::<ColorFormat, DepthFormat>(builder, context, &events_loop)
.unwrap();
let mut encoder: gfx::Encoder<_, _> = factory.create_command_buffer().into();
let pso = factory.create_pipeline_simple(
include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/shaders/rect_150.glslv")),
include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/shaders/rect_150.glslf")),
pipe::new()
).unwrap();
let (vertex_buffer, slice) = factory.create_vertex_buffer_with_slice(&SQUARE, ());
let const_buffer = factory.create_constant_buffer(CONSTS.len());
let mut data = pipe::Data {
vbuf: vertex_buffer,
out: main_color,
my_const: const_buffer
};
encoder.update_buffer(&data.my_const, &CONSTS, 0).unwrap();
let mut running = true;
while running {
events_loop.poll_events(|event| {
match event {
glutin::Event::WindowEvent {window_id, event} => {
match event {
glutin::WindowEvent::KeyboardInput {device_id, input} => running = false,
glutin::WindowEvent::CloseRequested => running = false,
glutin::WindowEvent::Resized(_) => {
gfx_glutin::update_views(&window, &mut data.out, &mut main_depth);
},
_ => ()
}
},
_ => ()
}
});
encoder.clear(&data.out, BLACK);
encoder.draw(&slice, &pso, &data);
encoder.flush(&mut device);
window.swap_buffers().unwrap();
device.cleanup();
}
}
// shaders/rect_150.glslf
#version 150 core
in vec4 v_Color;
out vec4 Target0;
void main() {
Target0 = v_Color;
}
// shaders/rect_150.glslv
#version 150 core
struct MyConst
{
uint my_val;
};
in vec2 a_Pos;
in vec3 a_Color;
uniform MyConst my_const[3];
out vec4 v_Color;
void main() {
MyConst cc = my_const[0];
v_Color = vec4(a_Color, 1.0);
gl_Position = vec4(a_Pos, 0.0, 1.0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment