Skip to content

Commit

Permalink
Merge branch 'master' into kivutar/gl
Browse files Browse the repository at this point in the history
  • Loading branch information
kivutar committed Dec 1, 2018
2 parents 67ec74b + eda5d86 commit 6d9c3ac
Show file tree
Hide file tree
Showing 14 changed files with 267 additions and 126 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ before_install:
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo apt-get install libglfw3-dev libopenal-dev xorg-dev -y --allow-unauthenticated; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew update; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew install glfw openal-soft; fi
- if [[ "$TRAVIS_OS_NAME" == "windows" ]]; then choco install make; fi
- if [[ "$TRAVIS_OS_NAME" == "windows" ]]; then wget http://static.kivutar.me/openal-soft-1.19.0-bin.zip; fi
- if [[ "$TRAVIS_OS_NAME" == "windows" ]]; then 7z x openal-soft-1.19.0-bin.zip -o/c/Users/travis/openal-soft-1.19.0-bin; fi

Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ func runLoop(vid *video.Video) {
}

func main() {
var GLVersion uint
flag.StringVar(&state.Global.CorePath, "L", "", "Path to the libretro core")
flag.BoolVar(&state.Global.Verbose, "v", false, "Verbose logs")
flag.UintVar(&GLVersion, "glver", 32, "OpenGL version")
flag.Parse()
args := flag.Args()

Expand Down Expand Up @@ -91,7 +93,7 @@ func main() {

playlists.LoadPlaylists()

vid := video.Init(settings.Settings.VideoFullscreen)
vid := video.Init(settings.Settings.VideoFullscreen, GLVersion)

m := menu.Init(vid)
m.ContextReset()
Expand Down
2 changes: 1 addition & 1 deletion menu/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func genericDrawHintBar() {
vid.DrawImage(menu.icons["key-z"], stack, float32(h)-70*menu.ratio, 70*menu.ratio, 70*menu.ratio, 1.0, c)
stack += 70 * menu.ratio
stack += 10 * menu.ratio
vid.Font.Printf(370*menu.ratio, float32(h)-23*menu.ratio, 0.5*menu.ratio, "BACK")
vid.Font.Printf(stack, float32(h)-23*menu.ratio, 0.5*menu.ratio, "BACK")
stack += vid.Font.Width(0.5*menu.ratio, "BACK")

stack += 30 * menu.ratio
Expand Down
2 changes: 1 addition & 1 deletion menu/scene_playlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func (s *screenPlaylist) drawHintBar() {
vid.DrawImage(menu.icons["key-z"], stack, float32(h)-70*menu.ratio, 70*menu.ratio, 70*menu.ratio, 1.0, c)
stack += 70 * menu.ratio
stack += 10 * menu.ratio
vid.Font.Printf(370*menu.ratio, float32(h)-23*menu.ratio, 0.5*menu.ratio, "BACK")
vid.Font.Printf(stack, float32(h)-23*menu.ratio, 0.5*menu.ratio, "BACK")
stack += vid.Font.Width(0.5*menu.ratio, "BACK")

stack += 30 * menu.ratio
Expand Down
19 changes: 18 additions & 1 deletion menu/thumbnail.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"os"
"os/user"
"strings"

"github.com/libretro/ludo/video"
)
Expand Down Expand Up @@ -42,11 +43,27 @@ func downloadThumbnail(list *entry, i int, url, folderPath, path string) {
io.Copy(out, resp.Body)
}

// Scrub characters that are not cross-platform and/or violate the
// No-Intro filename standard.
func scrubIllegalChars(str string) string {
str = strings.Replace(str, "&", "_", -1)
str = strings.Replace(str, "*", "_", -1)
str = strings.Replace(str, "/", "_", -1)
str = strings.Replace(str, ":", "_", -1)
str = strings.Replace(str, "`", "_", -1)
str = strings.Replace(str, "<", "_", -1)
str = strings.Replace(str, ">", "_", -1)
str = strings.Replace(str, "?", "_", -1)
str = strings.Replace(str, "|", "_", -1)
return str
}

func drawThumbnail(list *entry, i int, system, gameName string, x, y, w, h, scale float32) {
usr, _ := user.Current()
folderPath := usr.HomeDir + "/.ludo/thumbnails/" + system + "/Named_Snaps/"
path := folderPath + gameName + ".png"
url := "http://thumbnails.libretro.com/" + system + "/Named_Snaps/" + gameName + ".png"
legalName := scrubIllegalChars(gameName)
url := "http://thumbnails.libretro.com/" + system + "/Named_Snaps/" + legalName + ".png"

if list.children[i].thumbnail == 0 || list.children[i].thumbnail == menu.icons["img-dl"] {
if _, err := os.Stat(path); !os.IsNotExist(err) {
Expand Down
21 changes: 15 additions & 6 deletions video/border_frag_shader.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
package video

// source of the shader to draw circles
var borderFragmentShader = `
#version 330
#if __VERSION__ >= 130
#define COMPAT_VARYING in
#define COMPAT_ATTRIBUTE in
#define COMPAT_TEXTURE texture
#define COMPAT_FRAGCOLOR FragColor
out vec4 COMPAT_FRAGCOLOR;
#else
#define COMPAT_VARYING varying
#define COMPAT_ATTRIBUTE attribute
#define COMPAT_TEXTURE texture2D
#define COMPAT_FRAGCOLOR gl_FragColor
#endif
uniform float border_width;
uniform vec4 color;
uniform vec2 size;
in vec2 fragTexCoord;
out vec4 outputColor;
COMPAT_VARYING vec2 fragTexCoord;
void main() {
float ratio = size.x / size.y;
Expand All @@ -20,9 +29,9 @@ void main() {
if (fragTexCoord.x < maxX && fragTexCoord.x > minX &&
fragTexCoord.y < maxY && fragTexCoord.y > minY) {
outputColor = vec4(0,0,0,0);
COMPAT_FRAGCOLOR = vec4(0,0,0,0);
} else {
outputColor = color;
COMPAT_FRAGCOLOR = color;
}
}
` + "\x00"
21 changes: 15 additions & 6 deletions video/circle_frag_shader.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
package video

// source of the shader to draw circles
var circleFragmentShader = `
#version 330
#if __VERSION__ >= 130
#define COMPAT_VARYING in
#define COMPAT_ATTRIBUTE in
#define COMPAT_TEXTURE texture
#define COMPAT_FRAGCOLOR FragColor
out vec4 COMPAT_FRAGCOLOR;
#else
#define COMPAT_VARYING varying
#define COMPAT_ATTRIBUTE attribute
#define COMPAT_TEXTURE texture2D
#define COMPAT_FRAGCOLOR gl_FragColor
#endif
uniform sampler2D tex;
uniform vec4 color;
in vec2 fragTexCoord;
out vec4 outputColor;
COMPAT_VARYING vec2 fragTexCoord;
float circle(in vec2 _st, in float _radius) {
float circle(vec2 _st, float _radius) {
vec2 dist = _st - vec2(0.5);
return 1.-smoothstep(_radius-(_radius*0.05), _radius+(_radius*0.05), dot(dist,dist)*4.0);
}
void main() {
outputColor = vec4(color.rgb, circle(fragTexCoord.xy, 0.125));
COMPAT_FRAGCOLOR = vec4(color.rgb, circle(fragTexCoord.xy, 0.125));
}
` + "\x00"
27 changes: 18 additions & 9 deletions video/darken_frag_shader.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
package video

var darkenFragmentShader = `
#version 330
#if __VERSION__ >= 130
#define COMPAT_VARYING in
#define COMPAT_ATTRIBUTE in
#define COMPAT_TEXTURE texture
#define COMPAT_FRAGCOLOR FragColor
out vec4 COMPAT_FRAGCOLOR;
#else
#define COMPAT_VARYING varying
#define COMPAT_ATTRIBUTE attribute
#define COMPAT_TEXTURE texture2D
#define COMPAT_FRAGCOLOR gl_FragColor
#endif
uniform sampler2D tex;
uniform float mask;
uniform vec4 texColor;
in vec2 fragTexCoord;
COMPAT_VARYING vec2 fragTexCoord;
out vec4 outputColor;
vec4 grayscale(in vec4 c) {
vec4 grayscale(vec4 c) {
float average = (c.r + c.g + c.b) / 3.0;
return vec4(average, average, average, 1.0);
}
vec4 darken(in vec4 c) {
return vec4(c.r/4, c.g/4, c.b/4, 1.0);
vec4 darken(vec4 c) {
return vec4(c.r/4.0, c.g/4.0, c.b/4.0, 1.0);
}
void main() {
vec4 color = texture(tex, fragTexCoord);
outputColor = texColor * mix(color, darken(grayscale(color)), mask);
vec4 color = COMPAT_TEXTURE(tex, fragTexCoord);
COMPAT_FRAGCOLOR = texColor * mix(color, darken(grayscale(color)), mask);
}
` + "\x00"
26 changes: 26 additions & 0 deletions video/default_vert_shader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package video

var vertexShader = `
#if __VERSION__ >= 130
#define COMPAT_VARYING out
#define COMPAT_ATTRIBUTE in
#define COMPAT_TEXTURE texture
#define COMPAT_FRAGCOLOR FragColor
out vec4 COMPAT_FRAGCOLOR;
#else
#define COMPAT_VARYING varying
#define COMPAT_ATTRIBUTE attribute
#define COMPAT_TEXTURE texture2D
#define COMPAT_FRAGCOLOR gl_FragColor
#endif
COMPAT_ATTRIBUTE vec2 vert;
COMPAT_ATTRIBUTE vec2 vertTexCoord;
COMPAT_VARYING vec2 fragTexCoord;
void main() {
fragTexCoord = vertTexCoord;
gl_Position = vec4(vert, 0, 1);
}
` + "\x00"
23 changes: 16 additions & 7 deletions video/demul_frag_shader.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
package video

var demulFragmentShader = `
#version 330
#if __VERSION__ >= 130
#define COMPAT_VARYING in
#define COMPAT_ATTRIBUTE in
#define COMPAT_TEXTURE texture
#define COMPAT_FRAGCOLOR FragColor
out vec4 COMPAT_FRAGCOLOR;
#else
#define COMPAT_VARYING varying
#define COMPAT_ATTRIBUTE attribute
#define COMPAT_TEXTURE texture2D
#define COMPAT_FRAGCOLOR gl_FragColor
#endif
uniform sampler2D tex;
uniform float mask;
uniform vec4 texColor;
in vec2 fragTexCoord;
COMPAT_VARYING vec2 fragTexCoord;
out vec4 outputColor;
vec4 demultiply(in vec4 c) {
vec4 demultiply(vec4 c) {
return vec4(c.rgb/c.a, c.a);
}
void main() {
vec4 color = demultiply(texture(tex, fragTexCoord));
outputColor = texColor * color;
vec4 color = demultiply(COMPAT_TEXTURE(tex, fragTexCoord));
COMPAT_FRAGCOLOR = texColor * color;
}
` + "\x00"
3 changes: 2 additions & 1 deletion video/gfx.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func textureLoad(rgba *image.RGBA) uint32 {
gl.GenTextures(1, &texture)
gl.ActiveTexture(gl.TEXTURE1)
gl.BindTexture(gl.TEXTURE_2D, texture)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
Expand All @@ -157,6 +157,7 @@ func textureLoad(rgba *image.RGBA) uint32 {
gl.RGBA,
gl.UNSIGNED_BYTE,
gl.Ptr(rgba.Pix))
gl.GenerateMipmap(gl.TEXTURE_2D)

return texture
}
Expand Down
18 changes: 14 additions & 4 deletions video/rounded_frag_shader.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
package video

var roundedFragmentShader = `
#version 330
#if __VERSION__ >= 130
#define COMPAT_VARYING in
#define COMPAT_ATTRIBUTE in
#define COMPAT_TEXTURE texture
#define COMPAT_FRAGCOLOR FragColor
out vec4 COMPAT_FRAGCOLOR;
#else
#define COMPAT_VARYING varying
#define COMPAT_ATTRIBUTE attribute
#define COMPAT_TEXTURE texture2D
#define COMPAT_FRAGCOLOR gl_FragColor
#endif
uniform vec4 color;
uniform float radius;
uniform vec2 size;
in vec2 fragTexCoord;
out vec4 outputColor;
COMPAT_VARYING vec2 fragTexCoord;
float udRoundBox(vec2 p, vec2 b, float r) {
return length(max(abs(p)-b+r,0.0))-r;
Expand All @@ -18,6 +28,6 @@ void main() {
float ratio = size.x / size.y;
vec2 halfRes = vec2(0.5*ratio, 0.5);
float b = udRoundBox(fragTexCoord*vec2(ratio,1.0) - halfRes, halfRes, min(halfRes.x,halfRes.y)*radius);
outputColor = vec4(color.r, color.g, color.b, min(color.a, 1-smoothstep(0.00001,0.001,b)));
COMPAT_FRAGCOLOR = vec4(color.r, color.g, color.b, min(color.a, 1.0-smoothstep(0.00001,0.001,b)));
}
` + "\x00"
69 changes: 69 additions & 0 deletions video/shader_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package video

import (
"fmt"
"strings"

"github.com/go-gl/gl/all-core/gl"
)

func newProgram(GLSLVersion uint, vertexShaderSource, fragmentShaderSource string) (uint32, error) {
vertexShaderSource = fmt.Sprintf("#version %d\n", GLSLVersion) + vertexShaderSource
fragmentShaderSource = fmt.Sprintf("#version %d\n", GLSLVersion) + fragmentShaderSource

vertexShader, err := compileShader(vertexShaderSource, gl.VERTEX_SHADER)
if err != nil {
return 0, err
}

fragmentShader, err := compileShader(fragmentShaderSource, gl.FRAGMENT_SHADER)
if err != nil {
return 0, err
}

program := gl.CreateProgram()

gl.AttachShader(program, vertexShader)
gl.AttachShader(program, fragmentShader)
gl.LinkProgram(program)

var status int32
gl.GetProgramiv(program, gl.LINK_STATUS, &status)
if status == gl.FALSE {
var logLength int32
gl.GetProgramiv(program, gl.INFO_LOG_LENGTH, &logLength)

log := strings.Repeat("\x00", int(logLength+1))
gl.GetProgramInfoLog(program, logLength, nil, gl.Str(log))

return 0, fmt.Errorf("failed to link program: %v", log)
}

gl.DeleteShader(vertexShader)
gl.DeleteShader(fragmentShader)

return program, nil
}

func compileShader(source string, shaderType uint32) (uint32, error) {
shader := gl.CreateShader(shaderType)

csources, free := gl.Strs(source)
gl.ShaderSource(shader, 1, csources, nil)
free()
gl.CompileShader(shader)

var status int32
gl.GetShaderiv(shader, gl.COMPILE_STATUS, &status)
if status == gl.FALSE {
var logLength int32
gl.GetShaderiv(shader, gl.INFO_LOG_LENGTH, &logLength)

log := strings.Repeat("\x00", int(logLength+1))
gl.GetShaderInfoLog(shader, logLength, nil, gl.Str(log))

return 0, fmt.Errorf("failed to compile %v: %v", source, log)
}

return shader, nil
}
Loading

0 comments on commit 6d9c3ac

Please sign in to comment.