-
Notifications
You must be signed in to change notification settings - Fork 68
/
video.go
353 lines (297 loc) · 10.2 KB
/
video.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// Package video takes care on the game display. It also creates the window
// using GLFW. It exports the Refresh callback used by the libretro
// implementation.
package video
import (
"log"
"unsafe"
"github.com/go-gl/gl/all-core/gl"
"github.com/go-gl/glfw/v3.2/glfw"
"github.com/kivutar/glfont"
"github.com/libretro/ludo/libretro"
"github.com/libretro/ludo/settings"
"github.com/libretro/ludo/state"
)
// WindowInterface lists all the methods from glfw.Window that we are using.
// It is there only to allow mocking during tests.
type WindowInterface interface {
GetFramebufferSize() (width, height int)
Destroy()
MakeContextCurrent()
SetSizeLimits(minw, minh, maxw, maxh int)
SetInputMode(mode glfw.InputMode, value int)
GetKey(key glfw.Key) glfw.Action
SetShouldClose(bool)
ShouldClose() bool
SetTitle(string)
SwapBuffers()
}
// Video holds the state of the video package
type Video struct {
GLVersion string
Window WindowInterface
Geom libretro.GameGeometry
Font *glfont.Font
program uint32 // default program used for the game window
roundedProgram uint32 // program to draw rectangles with rounded corners
borderProgram uint32 // program to draw rectangles borders
circleProgram uint32 // program to draw textured circles
demulProgram uint32 // program to draw premultiplied alpha images
vao uint32
vbo uint32
texID uint32
white uint32
pitch int32
pixFmt uint32
pixType uint32
bpp int32
}
// Init instanciates the video package
func Init(fullscreen bool, GLVersion string) *Video {
vid := &Video{}
vid.GLVersion = GLVersion
vid.Configure(fullscreen)
return vid
}
// Reconfigure destroys and recreates the window with new attributes
func (video *Video) Reconfigure(fullscreen bool) {
if video.Window != nil {
video.Window.Destroy()
}
video.Configure(fullscreen)
}
func (video *Video) configureContext() uint {
var GLSLVersion uint
switch video.GLVersion {
case "2.0":
glfw.WindowHint(glfw.ContextVersionMajor, 2)
glfw.WindowHint(glfw.ContextVersionMinor, 0)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLAnyProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.False)
GLSLVersion = 110
case "2.1":
glfw.WindowHint(glfw.ContextVersionMajor, 2)
glfw.WindowHint(glfw.ContextVersionMinor, 1)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLAnyProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.False)
GLSLVersion = 120
case "3.0":
glfw.WindowHint(glfw.ContextVersionMajor, 3)
glfw.WindowHint(glfw.ContextVersionMinor, 0)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLAnyProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.False)
GLSLVersion = 130
case "3.1":
glfw.WindowHint(glfw.ContextVersionMajor, 3)
glfw.WindowHint(glfw.ContextVersionMinor, 1)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLAnyProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.False)
GLSLVersion = 140
case "3.2":
glfw.WindowHint(glfw.ContextVersionMajor, 3)
glfw.WindowHint(glfw.ContextVersionMinor, 2)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
GLSLVersion = 150
case "4.1":
glfw.WindowHint(glfw.ContextVersionMajor, 4)
glfw.WindowHint(glfw.ContextVersionMinor, 1)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
GLSLVersion = 410
case "4.2":
glfw.WindowHint(glfw.ContextVersionMajor, 4)
glfw.WindowHint(glfw.ContextVersionMinor, 2)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
GLSLVersion = 420
default: // 3.2
glfw.WindowHint(glfw.ContextVersionMajor, 3)
glfw.WindowHint(glfw.ContextVersionMinor, 2)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
GLSLVersion = 150
}
return GLSLVersion
}
// Configure instanciates the video package
func (video *Video) Configure(fullscreen bool) {
var width, height int
var m *glfw.Monitor
GLSLVersion := video.configureContext()
if fullscreen {
m = glfw.GetMonitors()[settings.Current.VideoMonitorIndex]
vm := m.GetVideoMode()
width = vm.Width
height = vm.Height
} else {
width = 320 * 3
height = 180 * 3
}
var err error
video.Window, err = glfw.CreateWindow(width, height, "Ludo", m, nil)
if err != nil {
panic(err)
}
video.Window.MakeContextCurrent()
// Force a minimum size for the window.
video.Window.SetSizeLimits(160, 120, glfw.DontCare, glfw.DontCare)
video.Window.SetInputMode(glfw.CursorMode, glfw.CursorHidden)
// Initialize Glow
if err := gl.Init(); err != nil {
panic(err)
}
fbw, fbh := video.Window.GetFramebufferSize()
video.CoreRatioViewport(fbw, fbh)
// LoadFont (fontfile, font scale, window width, window height)
assets := settings.Current.AssetsDirectory
video.Font, err = glfont.LoadFont(assets+"/font.ttf", int32(64), fbw, fbh, GLSLVersion)
if err != nil {
panic(err)
}
if state.Global.Verbose {
version := gl.GoStr(gl.GetString(gl.VERSION))
log.Println("[Video]: OpenGL version:", version)
}
// Configure the vertex and fragment shaders
video.program, err = newProgram(GLSLVersion, vertexShader, darkenFragmentShader)
if err != nil {
panic(err)
}
video.roundedProgram, err = newProgram(GLSLVersion, vertexShader, roundedFragmentShader)
if err != nil {
panic(err)
}
video.borderProgram, err = newProgram(GLSLVersion, vertexShader, borderFragmentShader)
if err != nil {
panic(err)
}
video.circleProgram, err = newProgram(GLSLVersion, vertexShader, circleFragmentShader)
if err != nil {
panic(err)
}
video.demulProgram, err = newProgram(GLSLVersion, vertexShader, demulFragmentShader)
if err != nil {
panic(err)
}
gl.UseProgram(video.program)
textureUniform := gl.GetUniformLocation(video.program, gl.Str("tex\x00"))
gl.Uniform1i(textureUniform, 0)
gl.BindFragDataLocation(video.program, 0, gl.Str("outputColor\x00"))
// Configure the vertex data
gl.GenVertexArrays(1, &video.vao)
gl.BindVertexArray(video.vao)
gl.GenBuffers(1, &video.vbo)
gl.BindBuffer(gl.ARRAY_BUFFER, video.vbo)
gl.BufferData(gl.ARRAY_BUFFER, len(vertices)*4, gl.Ptr(vertices), gl.STATIC_DRAW)
vertAttrib := uint32(gl.GetAttribLocation(video.program, gl.Str("vert\x00")))
gl.EnableVertexAttribArray(vertAttrib)
gl.VertexAttribPointer(vertAttrib, 2, gl.FLOAT, false, 4*4, gl.PtrOffset(0))
texCoordAttrib := uint32(gl.GetAttribLocation(video.program, gl.Str("vertTexCoord\x00")))
gl.EnableVertexAttribArray(texCoordAttrib)
gl.VertexAttribPointer(texCoordAttrib, 2, gl.FLOAT, false, 4*4, gl.PtrOffset(2*4))
// Sets a default pixel format
if video.pixFmt == 0 {
video.pixFmt = gl.UNSIGNED_SHORT_5_5_5_1
}
gl.GenTextures(1, &video.texID)
gl.ActiveTexture(gl.TEXTURE0)
if video.texID == 0 && state.Global.Verbose {
log.Println("[Video]: Failed to create the vid texture")
}
gl.BindTexture(gl.TEXTURE_2D, video.texID)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
video.white = newWhite()
}
// SetPixelFormat is a callback passed to the libretro implementation.
// It allows the core or the game to tell us which pixel format should be used for the display.
func (video *Video) SetPixelFormat(format uint32) bool {
if state.Global.Verbose {
log.Printf("[Video]: Set Pixel Format: %v\n", format)
}
switch format {
case libretro.PixelFormat0RGB1555:
video.pixFmt = gl.UNSIGNED_SHORT_5_5_5_1
video.pixType = gl.BGRA
video.bpp = 2
case libretro.PixelFormatXRGB8888:
video.pixFmt = gl.UNSIGNED_INT_8_8_8_8_REV
video.pixType = gl.BGRA
video.bpp = 4
case libretro.PixelFormatRGB565:
video.pixFmt = gl.UNSIGNED_SHORT_5_6_5
video.pixType = gl.RGB
video.bpp = 2
default:
log.Fatalf("Unknown pixel type %v", format)
}
gl.PixelStorei(gl.UNPACK_ROW_LENGTH, video.pitch/video.bpp)
return true
}
func (video *Video) updateMaskUniform() {
maskUniform := gl.GetUniformLocation(video.program, gl.Str("mask\x00"))
if state.Global.MenuActive {
gl.Uniform1f(maskUniform, 1.0)
} else {
gl.Uniform1f(maskUniform, 0.0)
}
}
// CoreRatioViewport configures the vertex array to display the game at the center of the window
// while preserving the original ascpect ratio of the game or core
func (video *Video) CoreRatioViewport(fbWidth int, fbHeight int) {
// Scale the content to fit in the viewport.
fbw := float32(fbWidth)
fbh := float32(fbHeight)
h := fbh
w := fbh * float32(video.Geom.AspectRatio)
if w > fbw {
h = fbw / float32(video.Geom.AspectRatio)
w = fbw
}
// Place the content in the middle of the window.
x := (fbw - w) / 2
y := (fbh - h) / 2
va := video.vertexArray(x, y, w, h, 1.0)
gl.BindBuffer(gl.ARRAY_BUFFER, video.vbo)
gl.BufferData(gl.ARRAY_BUFFER, len(va)*4, gl.Ptr(va), gl.STATIC_DRAW)
}
// Render the current frame
func (video *Video) Render() {
if !state.Global.CoreRunning {
gl.ClearColor(1, 1, 1, 1)
gl.Clear(gl.COLOR_BUFFER_BIT)
return
}
gl.ClearColor(0, 0, 0, 1)
gl.Clear(gl.COLOR_BUFFER_BIT)
avi := state.Global.Core.GetSystemAVInfo()
video.Geom = avi.Geometry
fbw, fbh := video.Window.GetFramebufferSize()
gl.Viewport(0, 0, int32(fbw), int32(fbh))
video.CoreRatioViewport(fbw, fbh)
gl.UseProgram(video.program)
video.updateMaskUniform()
gl.Uniform4f(gl.GetUniformLocation(video.program, gl.Str("texColor\x00")), 1, 1, 1, 1)
gl.BindVertexArray(video.vao)
gl.BindTexture(gl.TEXTURE_2D, video.texID)
gl.BindBuffer(gl.ARRAY_BUFFER, video.vbo)
gl.DrawArrays(gl.TRIANGLE_STRIP, 0, 4)
}
// Refresh the texture framebuffer
func (video *Video) Refresh(data unsafe.Pointer, width int32, height int32, pitch int32) {
gl.BindTexture(gl.TEXTURE_2D, video.texID)
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA8, width, height, 0, video.pixType, video.pixFmt, nil)
video.pitch = pitch
gl.PixelStorei(gl.UNPACK_ROW_LENGTH, video.pitch/video.bpp)
if data != nil {
gl.TexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, width, height, video.pixType, video.pixFmt, data)
}
}
var vertices = []float32{
// X, Y, U, V
-1.0, -1.0, 0.0, 1.0, // left-bottom
-1.0, 1.0, 0.0, 0.0, // left-top
1.0, -1.0, 1.0, 1.0, // right-bottom
1.0, 1.0, 1.0, 0.0, // right-top
}