librashader/librashader-runtime-gl/tests/hello_triangle/gl3.rs

479 lines
13 KiB
Rust
Raw Normal View History

2022-11-29 09:42:29 +11:00
use std::sync::mpsc::Receiver;
2024-02-06 10:39:01 +11:00
use std::sync::Arc;
2022-11-29 09:42:29 +11:00
use glfw::{Context, Glfw, Window, WindowEvent};
2024-02-06 10:39:01 +11:00
use glow::HasContext;
2023-01-14 17:58:22 +11:00
use librashader_common::{Size, Viewport};
2022-11-29 09:42:29 +11:00
use librashader_runtime_gl::{FilterChainGL, GLFramebuffer, GLImage};
2022-11-29 09:42:29 +11:00
const WIDTH: u32 = 800;
const HEIGHT: u32 = 600;
2024-02-06 10:39:01 +11:00
const TITLE: &str = "librashader OpenGL 3.3";
pub fn setup() -> (
Glfw,
Window,
Receiver<(f64, WindowEvent)>,
glow::Program,
glow::VertexArray,
Arc<glow::Context>,
2022-11-29 09:42:29 +11:00
) {
let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap();
glfw.window_hint(glfw::WindowHint::ContextVersion(3, 3));
glfw.window_hint(glfw::WindowHint::OpenGlProfile(
glfw::OpenGlProfileHint::Core,
));
glfw.window_hint(glfw::WindowHint::OpenGlForwardCompat(true));
glfw.window_hint(glfw::WindowHint::Resizable(true));
glfw.window_hint(glfw::WindowHint::OpenGlDebugContext(true));
let (mut window, events) = glfw
.create_window(WIDTH, HEIGHT, TITLE, glfw::WindowMode::Windowed)
.unwrap();
let (screen_width, screen_height) = window.get_framebuffer_size();
window.make_current();
window.set_key_polling(true);
2024-02-06 10:39:01 +11:00
let mut gl = unsafe { glow::Context::from_loader_function(|ptr| window.get_proc_address(ptr)) };
2022-11-29 09:42:29 +11:00
unsafe {
2024-02-06 10:39:01 +11:00
gl.enable(glow::DEBUG_OUTPUT);
gl.enable(glow::DEBUG_OUTPUT_SYNCHRONOUS);
gl.debug_message_callback(super::debug_callback);
gl.debug_message_control(glow::DONT_CARE, glow::DONT_CARE, glow::DONT_CARE, &[], true);
2022-11-29 09:42:29 +11:00
}
unsafe {
2024-02-06 10:39:01 +11:00
gl.viewport(0, 0, screen_width, screen_height);
gl.clear_color(0.4, 0.4, 0.4, 1.0);
2022-11-29 09:42:29 +11:00
}
// -------------------------------------------
const VERT_SHADER: &str = "#version 330 core
layout (location = 0) in vec3 Position;
layout (location = 1) in vec3 Color;
out VS_OUTPUT {
vec3 Color;
} OUT;
void main()
{
gl_Position = vec4(Position, 1.0);
OUT.Color = Color;
}";
const FRAG_SHADER: &str = "#version 330 core
in VS_OUTPUT {
vec3 Color;
} IN;
layout(location = 0) out vec4 Color;
void main()
{
Color = vec4(IN.Color, 1.0f);
}";
2024-02-06 10:39:01 +11:00
let shader_program = super::compile_program(&gl, VERT_SHADER, FRAG_SHADER);
2022-11-29 09:42:29 +11:00
2024-02-13 17:11:06 +11:00
// unsafe {
2024-02-06 10:39:01 +11:00
// glow::ObjectLabel(
// glow::SHADER,
2024-02-13 17:11:06 +11:00
// shader_program,
// -1,
// b"color_shader\0".as_ptr().cast(),
// );
// }
2022-11-29 09:42:29 +11:00
let vertices = &[
// positions // colors
0.5f32, -0.5, 0.0, 1.0, 0.0, 0.0, // bottom right
-0.5, -0.5, 0.0, 0.0, 1.0, 0.0, // bottom left
0.0, 0.5, 0.0, 0.0, 0.0, 1.0, // top
];
2024-02-06 10:39:01 +11:00
let vbo;
2022-11-29 09:42:29 +11:00
unsafe {
2024-02-06 10:39:01 +11:00
vbo = gl.create_buffer().unwrap();
// glow::ObjectLabel(glow::BUFFER, vbo, -1, b"triangle_vbo\0".as_ptr().cast());
2022-11-29 09:42:29 +11:00
}
unsafe {
2024-02-06 10:39:01 +11:00
gl.bind_buffer(glow::ARRAY_BUFFER, Some(vbo));
gl.buffer_data_u8_slice(
glow::ARRAY_BUFFER, // target
bytemuck::cast_slice(vertices),
glow::STATIC_DRAW, // usage
2022-11-29 09:42:29 +11:00
);
2024-02-06 10:39:01 +11:00
gl.bind_buffer(glow::ARRAY_BUFFER, None);
2022-11-29 09:42:29 +11:00
}
// set up vertex array object
2024-02-06 10:39:01 +11:00
let vao;
2022-11-29 09:42:29 +11:00
unsafe {
2024-02-06 10:39:01 +11:00
vao = gl.create_vertex_array().unwrap();
// glow::ObjectLabel(glow::VERTEX_ARRAY, vao, -1, b"triangle_vao\0".as_ptr().cast());
2022-11-30 16:39:42 +11:00
}
2022-11-29 09:42:29 +11:00
2022-11-30 16:39:42 +11:00
unsafe {
2024-02-06 10:39:01 +11:00
gl.bind_vertex_array(Some(vao));
gl.bind_buffer(glow::ARRAY_BUFFER, Some(vbo));
gl.enable_vertex_attrib_array(0); // this is "layout (location = 0)" in vertex shader
gl.vertex_attrib_pointer_f32(
0, // index of the generic vertex attribute ("layout (location = 0)")
3, // the number of components per generic vertex attribute
glow::FLOAT, // data type
false, // normalized (int-to-float conversion)
(6 * std::mem::size_of::<f32>()) as i32, // stride (byte offset between consecutive attributes)
0, // offset of the first component
2022-11-30 16:39:42 +11:00
);
2024-02-06 10:39:01 +11:00
gl.enable_vertex_attrib_array(1);
gl.vertex_attrib_pointer_f32(
1, // index of the generic vertex attribute ("layout (location = 0)")
3, // the number of components per generic vertex attribute
glow::FLOAT, // data type
false, // normalized (int-to-float conversion)
(6 * std::mem::size_of::<f32>()) as i32, // stride (byte offset between consecutive attributes)
(3 * std::mem::size_of::<f32>()) as i32, // offset of the first component
2022-11-29 09:42:29 +11:00
);
2024-02-06 10:39:01 +11:00
gl.bind_buffer(glow::ARRAY_BUFFER, None);
gl.bind_vertex_array(None);
2022-11-29 09:42:29 +11:00
}
// set up shared state for window
unsafe {
2024-02-06 10:39:01 +11:00
gl.viewport(0, 0, 900, 700);
gl.clear_color(0.3, 0.3, 0.5, 1.0);
2022-11-29 09:42:29 +11:00
}
2024-02-06 10:39:01 +11:00
unsafe {
// -------------------------------------------
println!("OpenGL version: {}", gl.get_parameter_string(glow::VERSION));
println!(
"GLSL version: {}",
gl.get_parameter_string(glow::SHADING_LANGUAGE_VERSION)
);
}
2022-11-29 09:42:29 +11:00
2024-02-06 10:39:01 +11:00
(glfw, window, events, shader_program, vao, Arc::new(gl))
2022-11-29 09:42:29 +11:00
}
pub fn do_loop(
2024-02-06 10:39:01 +11:00
gl: &Arc<glow::Context>,
2022-11-29 09:42:29 +11:00
mut glfw: Glfw,
mut window: Window,
events: Receiver<(f64, WindowEvent)>,
2024-02-06 10:39:01 +11:00
triangle_program: glow::Program,
triangle_vao: glow::VertexArray,
filter: &mut FilterChainGL,
2022-11-29 09:42:29 +11:00
) {
let mut framecount = 0;
2024-02-06 10:39:01 +11:00
let rendered_framebuffer;
let rendered_texture;
let quad_vbuf;
2022-11-29 09:42:29 +11:00
2024-02-06 10:39:01 +11:00
let output_texture;
let output_framebuffer_handle;
let output_quad_vbuf;
2022-11-29 09:42:29 +11:00
unsafe {
// do frmaebuffer
2024-02-06 10:39:01 +11:00
rendered_framebuffer = gl.create_framebuffer().unwrap();
gl.bind_framebuffer(glow::FRAMEBUFFER, Some(rendered_framebuffer));
2022-11-29 09:42:29 +11:00
2024-02-06 10:39:01 +11:00
// glow::ObjectLabel(
// glow::FRAMEBUFFER,
2024-02-13 17:11:06 +11:00
// rendered_framebuffer,
// -1,
// b"rendered_framebuffer\0".as_ptr().cast(),
// );
2022-11-29 09:42:29 +11:00
// make tetxure
2024-02-06 10:39:01 +11:00
rendered_texture = gl.create_texture().unwrap();
gl.bind_texture(glow::TEXTURE_2D, Some(rendered_texture));
2022-11-29 09:42:29 +11:00
2024-02-06 10:39:01 +11:00
// glow::ObjectLabel(
// glow::TEXTURE,
2024-02-13 17:11:06 +11:00
// rendered_texture,
// -1,
// b"rendered_texture\0".as_ptr().cast(),
// );
2022-11-29 09:42:29 +11:00
// empty image
2024-02-06 10:39:01 +11:00
gl.tex_storage_2d(
glow::TEXTURE_2D,
2022-11-29 09:42:29 +11:00
1,
2024-02-06 10:39:01 +11:00
glow::RGBA8,
WIDTH as i32,
HEIGHT as i32,
2022-11-29 09:42:29 +11:00
);
2024-02-06 10:39:01 +11:00
gl.tex_parameter_i32(
glow::TEXTURE_2D,
glow::TEXTURE_MAG_FILTER,
glow::NEAREST as i32,
2022-11-29 09:42:29 +11:00
);
2024-02-06 10:39:01 +11:00
gl.tex_parameter_i32(
glow::TEXTURE_2D,
glow::TEXTURE_MIN_FILTER,
glow::NEAREST as i32,
);
gl.tex_parameter_i32(
glow::TEXTURE_2D,
glow::TEXTURE_WRAP_S,
glow::CLAMP_TO_EDGE as i32,
);
gl.tex_parameter_i32(
glow::TEXTURE_2D,
glow::TEXTURE_WRAP_T,
glow::CLAMP_TO_EDGE as i32,
2022-11-29 09:42:29 +11:00
);
// set color attachment
2024-02-06 10:39:01 +11:00
gl.framebuffer_texture_2d(
glow::FRAMEBUFFER,
glow::COLOR_ATTACHMENT0,
glow::TEXTURE_2D,
Some(rendered_texture),
2022-11-29 09:42:29 +11:00
0,
);
2024-02-06 10:39:01 +11:00
gl.draw_buffer(glow::COLOR_ATTACHMENT0);
2022-11-29 09:42:29 +11:00
2024-02-06 10:39:01 +11:00
if gl.check_framebuffer_status(glow::FRAMEBUFFER) != glow::FRAMEBUFFER_COMPLETE {
2022-11-29 09:42:29 +11:00
panic!("failed to create fbo")
}
let fullscreen_fbo = [
-1.0f32, -1.0, 0.0, 1.0, -1.0, 0.0, -1.0, 1.0, 0.0, -1.0, 1.0, 0.0, 1.0, -1.0, 0.0,
1.0, 1.0, 0.0,
];
2024-02-06 10:39:01 +11:00
quad_vbuf = gl.create_buffer().unwrap();
gl.bind_buffer(glow::ARRAY_BUFFER, Some(quad_vbuf));
gl.buffer_data_u8_slice(
glow::ARRAY_BUFFER,
bytemuck::cast_slice(&fullscreen_fbo),
glow::STATIC_DRAW,
2022-11-29 09:42:29 +11:00
);
}
unsafe {
// do frmaebuffer
2024-02-06 10:39:01 +11:00
output_framebuffer_handle = gl.create_framebuffer().unwrap();
gl.bind_framebuffer(glow::FRAMEBUFFER, Some(output_framebuffer_handle));
2022-11-29 09:42:29 +11:00
2024-02-06 10:39:01 +11:00
// glow::ObjectLabel(
// glow::FRAMEBUFFER,
2024-02-13 17:11:06 +11:00
// output_framebuffer_handle,
// -1,
// b"output_framebuffer\0".as_ptr().cast(),
// );
2022-11-29 09:42:29 +11:00
// make tetxure
2024-02-06 10:39:01 +11:00
output_texture = gl.create_texture().unwrap();
gl.bind_texture(glow::TEXTURE_2D, Some(output_texture));
2022-11-29 09:42:29 +11:00
2024-02-06 10:39:01 +11:00
// glow::ObjectLabel(
// glow::TEXTURE,
2024-02-13 17:11:06 +11:00
// output_texture,
// -1,
// b"output_texture\0".as_ptr().cast(),
// );
2022-11-29 09:42:29 +11:00
// empty image
2024-02-06 10:39:01 +11:00
gl.tex_storage_2d(
glow::TEXTURE_2D,
2022-11-29 09:42:29 +11:00
1,
2024-02-06 10:39:01 +11:00
glow::RGBA8,
WIDTH as i32,
HEIGHT as i32,
2022-11-29 09:42:29 +11:00
);
2024-02-06 10:39:01 +11:00
gl.tex_parameter_i32(
glow::TEXTURE_2D,
glow::TEXTURE_MAG_FILTER,
glow::NEAREST as i32,
2022-11-29 09:42:29 +11:00
);
2024-02-06 10:39:01 +11:00
gl.tex_parameter_i32(
glow::TEXTURE_2D,
glow::TEXTURE_MIN_FILTER,
glow::NEAREST as i32,
);
gl.tex_parameter_i32(
glow::TEXTURE_2D,
glow::TEXTURE_WRAP_S,
glow::CLAMP_TO_EDGE as i32,
);
gl.tex_parameter_i32(
glow::TEXTURE_2D,
glow::TEXTURE_WRAP_T,
glow::CLAMP_TO_EDGE as i32,
2022-11-29 09:42:29 +11:00
);
// set color attachment
2024-02-06 10:39:01 +11:00
gl.framebuffer_texture_2d(
glow::FRAMEBUFFER,
glow::COLOR_ATTACHMENT0,
glow::TEXTURE_2D,
Some(output_texture),
2022-11-29 09:42:29 +11:00
0,
);
2024-02-06 10:39:01 +11:00
gl.draw_buffer(glow::COLOR_ATTACHMENT0);
if gl.check_framebuffer_status(glow::FRAMEBUFFER) != glow::FRAMEBUFFER_COMPLETE {
2022-11-29 09:42:29 +11:00
panic!("failed to create fbo")
}
let fullscreen_fbo = [
-1.0f32, -1.0, 0.0, 1.0, -1.0, 0.0, -1.0, 1.0, 0.0, -1.0, 1.0, 0.0, 1.0, -1.0, 0.0,
1.0, 1.0, 0.0,
];
2024-02-06 10:39:01 +11:00
output_quad_vbuf = gl.create_buffer().unwrap();
gl.bind_buffer(glow::ARRAY_BUFFER, Some(output_quad_vbuf));
gl.buffer_data_u8_slice(
glow::ARRAY_BUFFER, // target
bytemuck::cast_slice(&fullscreen_fbo),
glow::STATIC_DRAW, // usage
2022-11-29 09:42:29 +11:00
);
}
const VERT_SHADER: &str = r"#version 150 core
out vec2 v_tex;
const vec2 pos[4]=vec2[4](vec2(-1.0, 1.0),
vec2(-1.0,-1.0),
vec2( 1.0, 1.0),
vec2( 1.0,-1.0));
void main()
{
v_tex=0.5*pos[gl_VertexID] + vec2(0.5);
gl_Position=vec4(pos[gl_VertexID], 0.0, 1.0);
}
";
const FRAG_SHADER: &str = r"
#version 150 core
in vec2 v_tex;
uniform sampler2D texSampler;
out vec4 color;
void main()
{
color=texture(texSampler, v_tex);
}";
2024-02-06 10:39:01 +11:00
let quad_programid = super::compile_program(gl, VERT_SHADER, FRAG_SHADER);
let quad_vao;
2022-11-29 09:42:29 +11:00
unsafe {
2024-02-06 10:39:01 +11:00
quad_vao = gl.create_vertex_array().unwrap();
2022-11-29 09:42:29 +11:00
}
let (fb_width, fb_height) = window.get_framebuffer_size();
let (vp_width, vp_height) = window.get_size();
let output = GLFramebuffer::new_from_raw(
2024-02-06 10:39:01 +11:00
Arc::clone(gl),
Some(output_texture),
2022-11-29 09:42:29 +11:00
output_framebuffer_handle,
2024-02-06 10:39:01 +11:00
glow::RGBA8,
2022-11-29 09:42:29 +11:00
Size::new(vp_width as u32, vp_height as u32),
1,
);
while !window.should_close() {
glfw.poll_events();
for (_, event) in glfw::flush_messages(&events) {
glfw_handle_event(&mut window, event);
}
unsafe {
// render to fb
2024-02-06 10:39:01 +11:00
gl.bind_framebuffer(glow::FRAMEBUFFER, Some(rendered_framebuffer));
gl.viewport(0, 0, vp_width, vp_height);
2022-11-29 09:42:29 +11:00
2022-11-30 16:39:42 +11:00
// clear color
2024-02-06 10:39:01 +11:00
gl.clear_color(0.3, 0.4, 0.6, 1.0);
gl.clear(glow::COLOR_BUFFER_BIT);
2022-11-30 16:39:42 +11:00
2022-11-29 09:42:29 +11:00
// do the drawing
2024-02-06 10:39:01 +11:00
gl.use_program(Some(triangle_program));
2022-11-29 09:42:29 +11:00
// select vertices
2024-02-06 10:39:01 +11:00
gl.bind_vertex_array(Some(triangle_vao));
2022-11-29 09:42:29 +11:00
// draw to bound target
2024-02-06 10:39:01 +11:00
gl.draw_arrays(glow::TRIANGLES, 0, 3);
2022-11-29 09:42:29 +11:00
// unselect vertices
2024-02-06 10:39:01 +11:00
gl.bind_vertex_array(None);
2022-11-29 09:42:29 +11:00
// unselect fbo
2024-02-06 10:39:01 +11:00
gl.bind_framebuffer(glow::FRAMEBUFFER, None);
2022-11-29 09:42:29 +11:00
}
let viewport = Viewport {
2022-12-01 11:05:24 +11:00
x: 0f32,
y: 0f32,
2022-11-29 09:42:29 +11:00
output: &output,
mvp: None,
};
2022-11-30 16:39:42 +11:00
let rendered = GLImage {
2024-02-06 10:39:01 +11:00
handle: Some(rendered_texture),
format: glow::RGBA8,
2022-11-29 09:42:29 +11:00
size: Size {
width: fb_width as u32,
height: fb_height as u32,
},
};
2023-02-17 10:19:38 +11:00
unsafe {
filter
.frame(&rendered, &viewport, framecount, None)
.unwrap();
}
2022-11-29 09:42:29 +11:00
unsafe {
// texture is done now.
// draw quad to screen
2024-02-06 10:39:01 +11:00
gl.use_program(Some(quad_programid));
2022-11-29 09:42:29 +11:00
2024-02-06 10:39:01 +11:00
gl.active_texture(glow::TEXTURE0);
gl.bind_texture(glow::TEXTURE_2D, Some(output_texture));
2022-11-29 09:42:29 +11:00
2024-02-06 10:39:01 +11:00
gl.bind_vertex_array(Some(quad_vao));
gl.draw_arrays(glow::TRIANGLE_STRIP, 0, 4);
2022-11-29 09:42:29 +11:00
}
framecount += 1;
window.swap_buffers();
}
}
fn glfw_handle_event(window: &mut glfw::Window, event: glfw::WindowEvent) {
use glfw::Action;
use glfw::Key;
use glfw::WindowEvent as Event;
match event {
Event::Key(Key::Escape, _, Action::Press, _) => {
window.set_should_close(true);
}
Event::Size(width, height) => window.set_size(width, height),
_ => {}
}
2022-11-30 17:38:05 +11:00
}