Better shaders

This commit is contained in:
Joonas Javanainen 2017-08-05 15:34:29 +03:00
parent 51329d5938
commit 8621bbf116
No known key found for this signature in database
GPG Key ID: D39CCA5CB19B9179
24 changed files with 303 additions and 14 deletions

View File

@ -2,6 +2,19 @@
## [Unreleased]
### Added
- OpenGL ES 2.0+ support in gfx and glium renderers
- Separate OpenGL 2.0, 3.0, 4.0 shaders in both renderers. This should fix an
issue with some systems that refuse to use old GLSL shaders with modern
OpenGL contexts
### Changed
- imgui-gfx-renderer `Renderer::init` now requires a `shaders: Shaders`
parameter. Please see examples/support_gfx/mod.rs for a shader resolution
example
## [0.0.15] - 2017-07-23
### Added

View File

@ -1,4 +1,5 @@
use imgui::{ImGui, Ui};
use imgui_gfx_renderer::{Renderer, Shaders};
use std::time::Instant;
#[derive(Copy, Clone, PartialEq, Debug, Default)]
@ -12,7 +13,6 @@ pub fn run<F: FnMut(&Ui) -> bool>(title: String, clear_color: [f32; 4], mut run_
use gfx::{self, Device};
use gfx_window_glutin;
use glutin::{self, GlContext};
use imgui_gfx_renderer::Renderer;
type ColorFormat = gfx::format::Rgba8;
type DepthFormat = gfx::format::DepthStencil;
@ -26,9 +26,27 @@ pub fn run<F: FnMut(&Ui) -> bool>(title: String, clear_color: [f32; 4], mut run_
let (window, mut device, mut factory, mut main_color, mut main_depth) =
gfx_window_glutin::init::<ColorFormat, DepthFormat>(window, context, &events_loop);
let mut encoder: gfx::Encoder<_, _> = factory.create_command_buffer().into();
let shaders = {
let version = device.get_info().shading_language;
if version.is_embedded {
if version.major >= 3 {
Shaders::GlSlEs300
} else {
Shaders::GlSlEs100
}
} else {
if version.major >= 4 {
Shaders::GlSl400
} else if version.major >= 3 {
Shaders::GlSl130
} else {
Shaders::GlSl110
}
}
};
let mut imgui = ImGui::init();
let mut renderer = Renderer::init(&mut imgui, &mut factory, main_color.clone())
let mut renderer = Renderer::init(&mut imgui, &mut factory, shaders, main_color.clone())
.expect("Failed to initialize renderer");
configure_keys(&mut imgui);

View File

@ -43,6 +43,43 @@ gfx_defines!{
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Shaders {
GlSl400, // OpenGL 4.0+
GlSl130, // OpenGL 3.0+
GlSl110, // OpenGL 2.0+
GlSlEs300, // OpenGL ES 3.0+
GlSlEs100, // OpenGL ES 2.0+
}
impl Shaders {
fn get_program_code(self) -> (&'static [u8], &'static [u8]) {
use Shaders::*;
match self {
GlSl400 => (
include_bytes!("shader/glsl_400.vert"),
include_bytes!("shader/glsl_400.frag"),
),
GlSl130 => (
include_bytes!("shader/glsl_130.vert"),
include_bytes!("shader/glsl_130.frag"),
),
GlSl110 => (
include_bytes!("shader/glsl_110.vert"),
include_bytes!("shader/glsl_110.frag"),
),
GlSlEs300 => (
include_bytes!("shader/glsles_300.vert"),
include_bytes!("shader/glsles_300.frag"),
),
GlSlEs100 => (
include_bytes!("shader/glsles_100.vert"),
include_bytes!("shader/glsles_100.frag"),
),
}
}
}
pub struct Renderer<R: Resources> {
bundle: Bundle<R, pipe::Data<R>>,
index_buffer: Buffer<R, u16>,
@ -51,11 +88,11 @@ pub struct Renderer<R: Resources> {
impl<R: Resources> Renderer<R> {
pub fn init<F: Factory<R>>(imgui: &mut ImGui,
factory: &mut F,
shaders: Shaders,
out: RenderTargetView<R, gfx::format::Rgba8>)
-> RendererResult<Renderer<R>> {
let pso = factory.create_pipeline_simple(include_bytes!("shader/vert_110.glsl"),
include_bytes!("shader/frag_110.glsl"),
pipe::new())?;
let (vs_code, ps_code) = shaders.get_program_code();
let pso = factory.create_pipeline_simple(vs_code, ps_code, pipe::new())?;
let vertex_buffer = factory.create_buffer::<ImDrawVert>(256,
gfx::buffer::Role::Vertex,
gfx::memory::Usage::Dynamic,

View File

@ -5,6 +5,9 @@ uniform sampler2D tex;
varying vec2 f_uv;
varying vec4 f_color;
// Built-in:
// vec4 gl_FragColor
void main() {
gl_FragColor = f_color * texture2D(tex, f_uv.st);
}

View File

@ -9,6 +9,9 @@ attribute vec4 col;
varying vec2 f_uv;
varying vec4 f_color;
// Built-in:
// vec4 gl_Position
void main() {
f_uv = uv;
f_color = col;

View File

@ -1,4 +1,4 @@
#version 140
#version 130
uniform sampler2D tex;

View File

@ -1,4 +1,4 @@
#version 140
#version 130
uniform mat4 matrix;
@ -9,6 +9,9 @@ in vec4 col;
out vec2 f_uv;
out vec4 f_color;
// Built-in:
// vec4 gl_Position
void main() {
f_uv = uv;
f_color = col;

View File

@ -0,0 +1,12 @@
#version 400
uniform sampler2D tex;
in vec2 f_uv;
in vec4 f_color;
out vec4 Target0;
void main() {
Target0 = f_color * texture(tex, f_uv.st);
}

View File

@ -0,0 +1,19 @@
#version 400
uniform mat4 matrix;
in vec2 pos;
in vec2 uv;
in vec4 col;
out vec2 f_uv;
out vec4 f_color;
// Built-in:
// vec4 gl_Position
void main() {
f_uv = uv;
f_color = col;
gl_Position = matrix * vec4(pos.xy, 0, 1);
}

View File

@ -0,0 +1,13 @@
#version 100
uniform sampler2D tex;
varying mediump vec2 f_uv;
varying lowp vec4 f_color;
// Built-in:
// vec4 gl_FragColor
void main() {
gl_FragColor = f_color * texture2D(tex, f_uv.st);
}

View File

@ -0,0 +1,19 @@
#version 100
uniform mat4 matrix;
attribute mediump vec2 pos;
attribute mediump vec2 uv;
attribute lowp vec4 col;
varying mediump vec2 f_uv;
varying lowp vec4 f_color;
// Built-in:
// vec4 gl_Position
void main() {
f_uv = uv;
f_color = col;
gl_Position = matrix * vec4(pos.xy, 0, 1);
}

View File

@ -0,0 +1,12 @@
#version 300 es
uniform sampler2D tex;
in mediump vec2 f_uv;
in lowp vec4 f_color;
out lowp vec4 Target0;
void main() {
Target0 = f_color * texture(tex, f_uv.st);
}

View File

@ -0,0 +1,19 @@
#version 300 es
uniform mat4 matrix;
in mediump vec2 pos;
in mediump vec2 uv;
in lowp vec4 col;
out mediump vec2 f_uv;
out lowp vec4 f_color;
// Built-in:
// vec4 gl_Position
void main() {
f_uv = uv;
f_color = col;
gl_Position = matrix * vec4(pos.xy, 0, 1);
}

View File

@ -152,14 +152,29 @@ fn compile_default_program<F: Facade>(ctx: &F)
-> Result<Program, program::ProgramChooserCreationError> {
program!(
ctx,
140 => {
vertex: include_str!("shader/vert_140.glsl"),
fragment: include_str!("shader/frag_140.glsl"),
400 => {
vertex: include_str!("shader/glsl_400.vert"),
fragment: include_str!("shader/glsl_400.frag"),
outputs_srgb: true,
},
130 => {
vertex: include_str!("shader/glsl_130.vert"),
fragment: include_str!("shader/glsl_130.frag"),
outputs_srgb: true,
},
110 => {
vertex: include_str!("shader/vert_110.glsl"),
fragment: include_str!("shader/frag_110.glsl"),
vertex: include_str!("shader/glsl_110.vert"),
fragment: include_str!("shader/glsl_110.frag"),
outputs_srgb: true,
},
300 es => {
vertex: include_str!("shader/glsles_300.vert"),
fragment: include_str!("shader/glsles_300.frag"),
outputs_srgb: true,
},
100 es => {
vertex: include_str!("shader/glsles_100.vert"),
fragment: include_str!("shader/glsles_100.frag"),
outputs_srgb: true,
},
)

View File

@ -5,6 +5,9 @@ uniform sampler2D tex;
varying vec2 f_uv;
varying vec4 f_color;
// Built-in:
// vec4 gl_FragColor
void main() {
gl_FragColor = f_color * texture2D(tex, f_uv.st);
}

View File

@ -9,6 +9,9 @@ attribute vec4 col;
varying vec2 f_uv;
varying vec4 f_color;
// Built-in:
// vec4 gl_Position
void main() {
f_uv = uv;
f_color = col / 255.0;

View File

@ -1,4 +1,4 @@
#version 140
#version 130
uniform sampler2D tex;

View File

@ -1,4 +1,4 @@
#version 140
#version 130
uniform mat4 matrix;
@ -9,6 +9,9 @@ in vec4 col;
out vec2 f_uv;
out vec4 f_color;
// Built-in:
// vec4 gl_Position
void main() {
f_uv = uv;
f_color = col / 255.0;

View File

@ -0,0 +1,12 @@
#version 400
uniform sampler2D tex;
in vec2 f_uv;
in vec4 f_color;
out vec4 out_color;
void main() {
out_color = f_color * texture(tex, f_uv.st);
}

View File

@ -0,0 +1,19 @@
#version 400
uniform mat4 matrix;
in vec2 pos;
in vec2 uv;
in vec4 col;
out vec2 f_uv;
out vec4 f_color;
// Built-in:
// vec4 gl_Position
void main() {
f_uv = uv;
f_color = col / 255.0;
gl_Position = matrix * vec4(pos.xy, 0, 1);
}

View File

@ -0,0 +1,13 @@
#version 100
uniform sampler2D tex;
varying mediump vec2 f_uv;
varying lowp vec4 f_color;
// Built-in:
// vec4 gl_FragColor
void main() {
gl_FragColor = f_color * texture2D(tex, f_uv.st);
}

View File

@ -0,0 +1,19 @@
#version 100
uniform mat4 matrix;
attribute mediump vec2 pos;
attribute mediump vec2 uv;
attribute lowp vec4 col;
varying mediump vec2 f_uv;
varying lowp vec4 f_color;
// Built-in:
// vec4 gl_Position
void main() {
f_uv = uv;
f_color = col / 255.0;
gl_Position = matrix * vec4(pos.xy, 0, 1);
}

View File

@ -0,0 +1,12 @@
#version 300 es
uniform sampler2D tex;
in mediump vec2 f_uv;
in lowp vec4 f_color;
out lowp vec4 out_color;
void main() {
out_color = f_color * texture(tex, f_uv.st);
}

View File

@ -0,0 +1,19 @@
#version 300 es
uniform mat4 matrix;
in mediump vec2 pos;
in mediump vec2 uv;
in lowp vec4 col;
out mediump vec2 f_uv;
out lowp vec4 f_color;
// Built-in:
// vec4 gl_Position
void main() {
f_uv = uv;
f_color = col / 255.0;
gl_Position = matrix * vec4(pos.xy, 0, 1);
}