Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support GL cores #91

Open
wants to merge 59 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
d861e3d
Improve the libretro binding to support GL cores
kivutar Nov 24, 2018
7e79469
Start implementing InitFramebuffer
kivutar Dec 30, 2018
d41ab5d
TODO
kivutar Dec 30, 2018
c6c0c9c
Try to implement ProcAddress
kivutar Dec 30, 2018
967b0da
Merge remote-tracking branch 'origin/master' into kivutar/gl
kivutar Feb 22, 2019
a7da8f7
Fix display of non gl cores
kivutar Feb 22, 2019
a7cf8d1
Implement more of InitFramebuffer
kivutar Feb 22, 2019
d3fd407
Move InitFramebuffer and call HWRenderCallback.ContextReset
kivutar Feb 22, 2019
dc56ebf
Merge branch 'master' into kivutar/gl
kivutar Mar 12, 2019
7eef38c
Comment
kivutar Mar 12, 2019
87175ca
Call vid.InitFramebuffer and Core.HWRenderCallback.ContextReset durin…
kivutar Mar 12, 2019
6e4b4b6
Remove useless code
kivutar Mar 12, 2019
f241ef4
List hardware contexts in the libretro binding
kivutar Mar 12, 2019
681dfbc
Merge branch 'master' into kivutar/gl
kivutar Oct 8, 2019
933c86b
Update dependency for GetProcAddress
kivutar Oct 8, 2019
341ad35
Fix framebuffer creation
kivutar Oct 26, 2019
4914b16
Fix gl.Clear
kivutar Oct 26, 2019
466c2ef
Cast C.retro_hw_context_reset_t
kivutar Oct 27, 2019
614c5c2
Merge remote-tracking branch 'origin/master' into kivutar/gl
kivutar Oct 27, 2019
c703075
Set c.get_current_framebuffer and c.get_proc_address properly
kivutar Oct 27, 2019
2d55e97
Lint fix
kivutar Oct 27, 2019
4618fb6
Merge remote-tracking branch 'origin/master' into kivutar/gl
kivutar Oct 31, 2019
8f51dd3
Merge remote-tracking branch 'origin/master' into kivutar/gl
kivutar Nov 22, 2019
ab13ef0
Fix compat with non GL cores
kivutar Nov 22, 2019
05adbe4
Debug
kivutar Nov 22, 2019
d127589
Fix an important memory bug
kivutar Nov 22, 2019
2dd9150
Various rendering improvements
kivutar Nov 24, 2019
9c5c3f5
Small fix
kivutar Nov 24, 2019
42d43d6
Active texture fixes
kivutar Nov 24, 2019
b814469
Unbind vertex array
kivutar Nov 24, 2019
ff9a3c8
OpenGL core support improvements (#308)
heuripedes Feb 7, 2020
40396a4
Merge remote-tracking branch 'origin/master' into kivutar/gl
kivutar Feb 7, 2020
827ae54
Fix sharp-bilinear and zfast-crt filters (#311)
heuripedes Feb 23, 2020
f32af42
Call gl.TexSubImage2D after binding the texture
kivutar Feb 24, 2020
e35cba2
Fix shader live update
kivutar Feb 24, 2020
dbfbe3a
Temporarily fix tests
kivutar Feb 24, 2020
1146cb6
Merge remote-tracking branch 'origin/master' into kivutar/gl
kivutar Feb 26, 2020
1d8f8e8
Merge remote-tracking branch 'origin/master' into kivutar/gl
kivutar Feb 29, 2020
008a70c
Fix FBO on OSX (#313)
kivutar Feb 29, 2020
8b8d49e
Merge remote-tracking branch 'origin/master' into kivutar/gl
kivutar Mar 1, 2020
73d70cb
Implement libretro.EnvironmentGetPrefferedHWRenderer
kivutar Mar 1, 2020
e01a76b
Merge remote-tracking branch 'origin/master' into kivutar/gl
kivutar Jul 26, 2020
1e0c4a0
Uncomment gl.Enable(gl.TEXTURE_2D)
kivutar Jul 26, 2020
d8afc60
Merge remote-tracking branch 'origin/master' into kivutar/gl
kivutar Jan 5, 2021
da0092e
Fixes
kivutar Jan 5, 2021
e4c5845
Merge branch 'master' into kivutar/gl
kivutar Jan 28, 2021
6097d39
Merge branch 'master' into kivutar/gl
kivutar Apr 17, 2021
a43843b
Fix
kivutar Apr 17, 2021
8647e6f
Merge branch 'master' into kivutar/gl
kivutar May 14, 2021
c95a3a7
Merge branch 'master' into kivutar/gl
kivutar May 14, 2021
346dbd8
Merge remote-tracking branch 'origin/master' into kivutar/gl
kivutar Sep 30, 2021
a957673
Merge remote-tracking branch 'origin/master' into kivutar/gl
kivutar Oct 22, 2021
d74969f
Fix
kivutar Oct 22, 2021
cb8649a
Forgot to use program before get uniform
kivutar Oct 22, 2021
7d48bb8
Merge remote-tracking branch 'origin/master' into kivutar/gl
kivutar Nov 14, 2021
2bcbb1d
Merge remote-tracking branch 'origin/master' into kivutar/gl
kivutar Nov 24, 2021
3e084bf
Merge remote-tracking branch 'origin/master' into kivutar/gl
kivutar Dec 14, 2021
e01cc6b
Merge remote-tracking branch 'origin/master' into kivutar/gl
kivutar Feb 10, 2022
3edf65f
Merge remote-tracking branch 'origin/master' into kivutar/gl
kivutar Aug 5, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ARCH ?= x86_64
VERSION ?= dev
BUNDLENAME = $(APP)-$(OS)-$(ARCH)-$(VERSION)

CORES = atari800 bluemsx swanstation fbneo fceumm gambatte gearsystem genesis_plus_gx handy lutro mednafen_ngp mednafen_pce mednafen_pce_fast mednafen_pcfx mednafen_psx mednafen_saturn mednafen_supergrafx mednafen_vb mednafen_wswan mgba melonds np2kai o2em pcsx_rearmed picodrive pokemini prosystem snes9x stella2014 vecx virtualjaguar
CORES = ppsspp dolphin atari800 bluemsx swanstation fbneo fceumm gambatte gearsystem genesis_plus_gx handy lutro mednafen_ngp mednafen_pce mednafen_pce_fast mednafen_pcfx mednafen_psx mednafen_saturn mednafen_supergrafx mednafen_vb mednafen_wswan mgba melonds np2kai o2em pcsx_rearmed picodrive pokemini prosystem snes9x stella2014 vecx virtualjaguar

ifeq ($(ARCH), arm)
CORES := $(filter-out swanstation,$(CORES))
Expand Down
5 changes: 5 additions & 0 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ func LoadGame(gamePath string) error {
log.Println("[Core]: Game loaded: " + gamePath)
savefiles.LoadSRAM()

if state.Core.HWRenderCallback != nil {
vid.InitFramebuffer()
state.Core.HWRenderCallback.ContextReset()
}

return nil
}

Expand Down
10 changes: 10 additions & 0 deletions core/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ func environment(cmd uint32, data unsafe.Pointer) bool {
state.Core.SetFrameTimeCallback(data)
case libretro.EnvironmentSetAudioCallback:
state.Core.SetAudioCallback(data)
case libretro.EnvironmentSetHWRender:
state.Core.HWRenderCallback = libretro.SetHWRenderCallback(
data,
vid.CurrentFramebuffer,
vid.ProcAddress)
log.Println("[Env]: HWContextType:", state.Core.HWRenderCallback.HWContextType)
return true
case libretro.EnvironmentGetCanDupe:
libretro.SetBool(data, true)
case libretro.EnvironmentSetPixelFormat:
Expand All @@ -151,6 +158,9 @@ func environment(cmd uint32, data unsafe.Pointer) bool {
return environmentGetSystemDirectory(data)
case libretro.EnvironmentGetSaveDirectory:
return environmentGetSaveDirectory(data)
case libretro.EnvironmentGetPrefferedHWRender:
log.Println("[Env]: EnvironmentGetPrefferedHWRenderer: 1")
libretro.SetUint(data, uint(libretro.HWContextOpenGL))
case libretro.EnvironmentShutdown:
vid.SetShouldClose(true)
case libretro.EnvironmentGetCoreOptionsVersion:
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/fatih/structs v1.1.0
github.com/go-gl/gl v0.0.0-20211210172815-726fda9656d6
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20211213063430-748e38ca8aec
github.com/go-gl/mathgl v1.0.0
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
github.com/golang/snappy v0.0.4 // indirect
github.com/klauspost/compress v1.13.6 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ github.com/go-gl/gl v0.0.0-20211210172815-726fda9656d6 h1:zDw5v7qm4yH7N8C8uWd+8I
github.com/go-gl/gl v0.0.0-20211210172815-726fda9656d6/go.mod h1:9YTyiznxEY1fVinfM7RvRcjRHbw2xLBJ3AAGIT0I4Nw=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20211213063430-748e38ca8aec h1:3FLiRYO6PlQFDpUU7OEFlWgjGD1jnBIVSJ5SYRWk+9c=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20211213063430-748e38ca8aec/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/mathgl v1.0.0 h1:t9DznWJlXxxjeeKLIdovCOVJQk/GzDEL7h/h+Ro2B68=
github.com/go-gl/mathgl v1.0.0/go.mod h1:yhpkQzEiH9yPyxDUGzkmgScbaBVlhC06qodikEM0ZwQ=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/golang/snappy v0.0.2/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
Expand Down Expand Up @@ -83,6 +85,7 @@ golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56/go.mod h1:JhuoJpWY28nO4Vef9tZUw9qufEGTyX1+7lmHxV5q5G4=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190321063152-3fc05d484e9f/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.0.0-20211028202545-6944b10bf410 h1:hTftEOvwiOq2+O8k2D5/Q7COC7k5Qcrgc2TFURJYnvQ=
Expand Down
18 changes: 18 additions & 0 deletions libretro/cfuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ void bridge_retro_frame_time_callback(retro_frame_time_callback_t f, retro_usec_
f(usec);
}

void bridge_retro_hw_context_reset(retro_hw_context_reset_t f) {
f();
}

void bridge_retro_hw_context_destroy(retro_hw_context_reset_t f) {
f();
}

void bridge_retro_audio_callback(retro_audio_callback_t f) {
f();
}
Expand Down Expand Up @@ -168,5 +176,15 @@ int64_t coreGetTimeUsec_cgo() {
return coreGetTimeUsec();
}

uintptr_t coreGetCurrentFramebuffer_cgo() {
uintptr_t coreGetCurrentFramebuffer();
return coreGetCurrentFramebuffer();
}

uintptr_t coreGetProcAddress_cgo(const char *sym) {
uintptr_t coreGetProcAddress(const char *sym);
return coreGetProcAddress(sym);
}

*/
import "C"
116 changes: 100 additions & 16 deletions libretro/libretro.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ void bridge_retro_unload_game(void *f);
void bridge_retro_run(void *f);
void bridge_retro_reset(void *f);
void bridge_retro_frame_time_callback(retro_frame_time_callback_t f, retro_usec_t usec);
void bridge_retro_hw_context_reset(retro_hw_context_reset_t f);
void bridge_retro_hw_context_destroy(retro_hw_context_reset_t f);
void bridge_retro_audio_callback(retro_audio_callback_t f);
void bridge_retro_audio_set_state(retro_audio_set_state_callback_t f, bool state);
size_t bridge_retro_get_memory_size(void *f, unsigned id);
Expand All @@ -51,6 +53,8 @@ size_t coreAudioSampleBatch_cgo(const int16_t *data, size_t frames);
int16_t coreInputState_cgo(unsigned port, unsigned device, unsigned index, unsigned id);
void coreLog_cgo(enum retro_log_level level, const char *msg);
int64_t coreGetTimeUsec_cgo();
uintptr_t coreGetCurrentFramebuffer_cgo();
uintptr_t coreGetProcAddress_cgo(const char *sym);
*/
import "C"
import (
Expand Down Expand Up @@ -216,6 +220,20 @@ type FrameTimeCallback struct {
Reference int64
}

// HWRenderCallback sets an interface to let a libretro core render with
// hardware acceleration.
type HWRenderCallback struct {
HWContextType uint32
ContextReset func()
Depth bool
Stencil bool
BottomLeftOrigin bool
VersionMajor, VersionMinor uint
CacheContext bool
ContextDestroy func()
DebugContext bool
}

// AudioCallback stores the audio callback itself and the SetState callback
type AudioCallback struct {
Callback func()
Expand Down Expand Up @@ -409,26 +427,48 @@ const (
MemoryVideoRAM = uint32(C.RETRO_MEMORY_VIDEO_RAM)
)

// Hardware contexts
const (
HWContextNone = uint32(C.RETRO_HW_CONTEXT_NONE)
HWContextOpenGL = uint32(C.RETRO_HW_CONTEXT_OPENGL)
HWContextOpenGLES2 = uint32(C.RETRO_HW_CONTEXT_OPENGLES2)
HWContextOpenGLCore = uint32(C.RETRO_HW_CONTEXT_OPENGL_CORE)
HWContextOpenGLES3 = uint32(C.RETRO_HW_CONTEXT_OPENGLES3)
HWContextOpenGLESVersion = uint32(C.RETRO_HW_CONTEXT_OPENGLES_VERSION)
HWContextVulkan = uint32(C.RETRO_HW_CONTEXT_VULKAN)
HWContextDummy = uint32(C.RETRO_HW_CONTEXT_DUMMY)
)

// Pass this to retro_video_refresh_t if rendering to hardware.
// Passing NULL to retro_video_refresh_t is still a frame dupe as normal.
var (
HWFrameBufferValid = unsafe.Pointer(C.RETRO_HW_FRAME_BUFFER_VALID)
)

type (
environmentFunc func(uint32, unsafe.Pointer) bool
videoRefreshFunc func(unsafe.Pointer, int32, int32, int32)
audioSampleFunc func(int16, int16)
audioSampleBatchFunc func([]byte, int32) int32
inputPollFunc func()
inputStateFunc func(uint, uint32, uint, uint) int16
logFunc func(uint32, string)
getTimeUsecFunc func() int64
environmentFunc func(uint32, unsafe.Pointer) bool
videoRefreshFunc func(unsafe.Pointer, int32, int32, int32)
audioSampleFunc func(int16, int16)
audioSampleBatchFunc func([]byte, int32) int32
inputPollFunc func()
inputStateFunc func(uint, uint32, uint, uint) int16
logFunc func(uint32, string)
getTimeUsecFunc func() int64
getCurrentFramebufferFunc func() uintptr
getProcAddressFunc func(string) uintptr
)

var (
environment environmentFunc
videoRefresh videoRefreshFunc
audioSample audioSampleFunc
audioSampleBatch audioSampleBatchFunc
inputPoll inputPollFunc
inputState inputStateFunc
log logFunc
getTimeUsec getTimeUsecFunc
environment environmentFunc
videoRefresh videoRefreshFunc
audioSample audioSampleFunc
audioSampleBatch audioSampleBatchFunc
inputPoll inputPollFunc
inputState inputStateFunc
log logFunc
getTimeUsec getTimeUsecFunc
getCurrentFramebuffer getCurrentFramebufferFunc
getProcAddress getProcAddressFunc
)

// Load dynamically loads a libretro core at the given path and returns a Core instance
Expand Down Expand Up @@ -490,6 +530,8 @@ func (core *Core) Deinit() {
inputState = nil
log = nil
getTimeUsec = nil
getCurrentFramebuffer = nil
getProcAddress = nil
}

// Run runs the game for one video frame.
Expand Down Expand Up @@ -721,6 +763,16 @@ func coreGetTimeUsec() C.uint64_t {
return C.uint64_t(getTimeUsec())
}

//export coreGetCurrentFramebuffer
func coreGetCurrentFramebuffer() C.uintptr_t {
return C.uintptr_t(getCurrentFramebuffer())
}

//export coreGetProcAddress
func coreGetProcAddress(sym *C.char) C.uintptr_t {
return C.uintptr_t(getProcAddress(C.GoString(sym)))
}

// SetData is a setter for the data of a GameInfo type
func (gi *GameInfo) SetData(bytes []byte) {
cstr := C.CString(string(bytes))
Expand Down Expand Up @@ -883,6 +935,38 @@ func (core *Core) SetFrameTimeCallback(data unsafe.Pointer) {
core.FrameTimeCallback = ftc
}

// SetHWRenderCallback is an environment callback helper to set the HWRenderCallback
func SetHWRenderCallback(
data unsafe.Pointer,
currentFramebuffer getCurrentFramebufferFunc,
procAddress getProcAddressFunc,
) *HWRenderCallback {
c := (*C.struct_retro_hw_render_callback)(data)
hwrc := HWRenderCallback{}
hwrc.HWContextType = uint32(c.context_type)

getCurrentFramebuffer = currentFramebuffer
getProcAddress = procAddress
c.get_current_framebuffer = (C.retro_hw_get_current_framebuffer_t)(C.coreGetCurrentFramebuffer_cgo)
c.get_proc_address = (C.retro_hw_get_proc_address_t)(C.coreGetProcAddress_cgo)

hwrc.ContextReset = func() {
C.bridge_retro_hw_context_reset((C.retro_hw_context_reset_t)(c.context_reset))
}

hwrc.Depth = bool(c.depth)
hwrc.Stencil = bool(c.stencil)
hwrc.BottomLeftOrigin = bool(c.bottom_left_origin)
hwrc.VersionMajor = uint(c.version_major)
hwrc.VersionMinor = uint(c.version_minor)
hwrc.CacheContext = bool(c.cache_context)
hwrc.ContextDestroy = func() {
C.bridge_retro_hw_context_destroy((C.retro_hw_context_reset_t)(c.context_destroy))
}
hwrc.DebugContext = bool(c.debug_context)
return &hwrc
}

// SetAudioCallback is an environment callback helper to set the AudioCallback
func (core *Core) SetAudioCallback(data unsafe.Pointer) {
c := *(*C.struct_retro_audio_callback)(data)
Expand Down
1 change: 1 addition & 0 deletions libretro/libretro_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Core struct {
AudioCallback *AudioCallback
FrameTimeCallback *FrameTimeCallback
DiskControlCallback *DiskControlCallback
HWRenderCallback *HWRenderCallback

MemoryMap []MemoryDescriptor
}
4 changes: 3 additions & 1 deletion video/default_vert_shader.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ COMPAT_ATTRIBUTE vec2 vertTexCoord;

COMPAT_VARYING vec2 fragTexCoord;

uniform mat4 MVP;

void main() {
fragTexCoord = vertTexCoord;
gl_Position = vec4(vert, 0.0, 1.0);
gl_Position = vec4(vert, 0.0, 1.0) * MVP;
}
` + "\x00"
42 changes: 19 additions & 23 deletions video/font.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,22 +174,14 @@ func LoadTrueTypeFont(program uint32, r io.Reader, scale int32, low, high rune,
f.fontChar = append(f.fontChar, char)
}

// Generate texture
gl.GenTextures(1, &f.textureID)
gl.BindTexture(gl.TEXTURE_2D, f.textureID)
gl.PixelStorei(gl.UNPACK_ALIGNMENT, 1)
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.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(rgba.Rect.Dx()), int32(rgba.Rect.Dy()), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(rgba.Pix))

gl.GenerateMipmap(gl.TEXTURE_2D)
gl.BindTexture(gl.TEXTURE_2D, 0)
textureUniform := gl.GetUniformLocation(f.program, gl.Str("Texture\x00"))
gl.Uniform1i(textureUniform, 0)

// Configure VAO/VBO for texture quads
genVertexArrays(1, &f.vao)
gl.GenBuffers(1, &f.vbo)
bindVertexArray(f.vao)

gl.GenBuffers(1, &f.vbo)
gl.BindBuffer(gl.ARRAY_BUFFER, f.vbo)

vertAttrib := uint32(gl.GetAttribLocation(f.program, gl.Str("vert\x00")))
Expand All @@ -200,6 +192,16 @@ func LoadTrueTypeFont(program uint32, r io.Reader, scale int32, low, high rune,
gl.EnableVertexAttribArray(texCoordAttrib)
gl.VertexAttribPointerWithOffset(texCoordAttrib, 2, gl.FLOAT, false, 4*4, 2*4)

// Generate texture
gl.ActiveTexture(gl.TEXTURE0)
gl.GenTextures(1, &f.textureID)
gl.BindTexture(gl.TEXTURE_2D, f.textureID)
gl.PixelStorei(gl.UNPACK_ALIGNMENT, 1)
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.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(rgba.Rect.Dx()), int32(rgba.Rect.Dy()), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(rgba.Pix))
gl.GenerateMipmap(gl.TEXTURE_2D)

gl.BindBuffer(gl.ARRAY_BUFFER, 0)
bindVertexArray(0)

Expand All @@ -224,8 +226,7 @@ func LoadFont(file string, scale int32, windowWidth int, windowHeight int) (*Fon
gl.UseProgram(program)

// Set screen resolution
resUniform := gl.GetUniformLocation(program, gl.Str("resolution\x00"))
gl.Uniform2f(resUniform, float32(windowWidth), float32(windowHeight))
gl.Uniform2f(gl.GetUniformLocation(program, gl.Str("resolution\x00")), float32(windowWidth), float32(windowHeight))

return LoadTrueTypeFont(program, fd, scale, 32, 256, LeftToRight)
}
Expand Down Expand Up @@ -253,15 +254,6 @@ func (f *Font) Printf(x, y float32, scale float32, fs string, argv ...interface{

lowChar := rune(32)

// Setup blending mode
gl.Enable(gl.BLEND)
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)

// Activate corresponding render state
gl.UseProgram(f.program)
// Set text color
gl.Uniform4f(gl.GetUniformLocation(f.program, gl.Str("textColor\x00")), f.color.R, f.color.G, f.color.B, f.color.A)

var coords []point

// Iterate through all characters in string
Expand Down Expand Up @@ -302,6 +294,10 @@ func (f *Font) Printf(x, y float32, scale float32, fs string, argv ...interface{
x += float32((ch.advance >> 6)) * scale // Bitshift by 6 to get value in pixels (2^6 = 64 (divide amount of 1/64th pixels by 64 to get amount of pixels))
}

gl.UseProgram(f.program)
gl.Uniform4f(gl.GetUniformLocation(f.program, gl.Str("color\x00")), f.color.R, f.color.G, f.color.B, f.color.A)
gl.Enable(gl.BLEND)
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
bindVertexArray(f.vao)
gl.ActiveTexture(gl.TEXTURE0)
gl.BindTexture(gl.TEXTURE_2D, f.textureID)
Expand Down
11 changes: 6 additions & 5 deletions video/font_frag_shader.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@ var fontFragmentShader = `
#define COMPAT_ATTRIBUTE in
#define COMPAT_TEXTURE texture
#define COMPAT_FRAGCOLOR FragColor
out vec4 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 textColor;
uniform sampler2D Texture;
uniform vec4 color;

COMPAT_VARYING vec2 fragTexCoord;

void main() {
vec4 sampled = vec4(1.0, 1.0, 1.0, COMPAT_TEXTURE(tex, fragTexCoord).r);
COMPAT_FRAGCOLOR = min(textColor, vec4(1.0, 1.0, 1.0, 1.0)) * sampled;
vec4 sampled = vec4(1.0, 1.0, 1.0, COMPAT_TEXTURE(Texture, fragTexCoord).r);
COMPAT_FRAGCOLOR = min(color, vec4(1.0, 1.0, 1.0, 1.0)) * sampled;
}
` + "\x00"
Loading