internal/graphicsdriver/opengl/gl: refactoring

This commit is contained in:
Hajime Hoshi 2023-04-22 15:09:24 +09:00
parent 4f69ca4ea9
commit 0542f9daa3
8 changed files with 212 additions and 528 deletions

View File

@ -1,4 +1,5 @@
github.com/hajimehoshi/ebiten/v2/internal/cocoa github.com/hajimehoshi/ebiten/v2/internal/cocoa
github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/directx github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/directx
github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal/mtl github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal/mtl
github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl/gl
github.com/hajimehoshi/ebiten/v2/internal/goglfw github.com/hajimehoshi/ebiten/v2/internal/goglfw

View File

@ -330,7 +330,6 @@ package gl
import "C" import "C"
import ( import (
"errors"
"runtime" "runtime"
"unsafe" "unsafe"
) )
@ -773,257 +772,80 @@ func (c *defaultContext) Viewport(x int32, y int32, width int32, height int32) {
} }
func (c *defaultContext) LoadFunctions() error { func (c *defaultContext) LoadFunctions() error {
c.gpActiveTexture = (C.GPACTIVETEXTURE)(c.getProcAddress("glActiveTexture")) g := procAddressGetter{ctx: c}
if c.gpActiveTexture == nil {
return errors.New("gl: glActiveTexture is missing") c.gpActiveTexture = C.GPACTIVETEXTURE(unsafe.Pointer(g.get("glActiveTexture")))
} c.gpAttachShader = C.GPATTACHSHADER(unsafe.Pointer(g.get("glAttachShader")))
c.gpAttachShader = (C.GPATTACHSHADER)(c.getProcAddress("glAttachShader")) c.gpBindAttribLocation = C.GPBINDATTRIBLOCATION(unsafe.Pointer(g.get("glBindAttribLocation")))
if c.gpAttachShader == nil { c.gpBindBuffer = C.GPBINDBUFFER(unsafe.Pointer(g.get("glBindBuffer")))
return errors.New("gl: glAttachShader is missing") c.gpBindFramebufferEXT = C.GPBINDFRAMEBUFFEREXT(unsafe.Pointer(g.get("glBindFramebufferEXT")))
} c.gpBindRenderbufferEXT = C.GPBINDRENDERBUFFEREXT(unsafe.Pointer(g.get("glBindRenderbufferEXT")))
c.gpBindAttribLocation = (C.GPBINDATTRIBLOCATION)(c.getProcAddress("glBindAttribLocation")) c.gpBindTexture = C.GPBINDTEXTURE(unsafe.Pointer(g.get("glBindTexture")))
if c.gpBindAttribLocation == nil { c.gpBlendEquationSeparate = C.GPBLENDEQUATIONSEPARATE(unsafe.Pointer(g.get("glBlendEquationSeparate")))
return errors.New("gl: glBindAttribLocation is missing") c.gpBlendFuncSeparate = C.GPBLENDFUNCSEPARATE(unsafe.Pointer(g.get("glBlendFuncSeparate")))
} c.gpBufferData = C.GPBUFFERDATA(unsafe.Pointer(g.get("glBufferData")))
c.gpBindBuffer = (C.GPBINDBUFFER)(c.getProcAddress("glBindBuffer")) c.gpBufferSubData = C.GPBUFFERSUBDATA(unsafe.Pointer(g.get("glBufferSubData")))
if c.gpBindBuffer == nil { c.gpCheckFramebufferStatusEXT = C.GPCHECKFRAMEBUFFERSTATUSEXT(unsafe.Pointer(g.get("glCheckFramebufferStatusEXT")))
return errors.New("gl: glBindBuffer is missing") c.gpClear = C.GPCLEAR(unsafe.Pointer(g.get("glClear")))
} c.gpColorMask = C.GPCOLORMASK(unsafe.Pointer(g.get("glColorMask")))
c.gpBindFramebufferEXT = (C.GPBINDFRAMEBUFFEREXT)(c.getProcAddress("glBindFramebufferEXT")) c.gpCompileShader = C.GPCOMPILESHADER(unsafe.Pointer(g.get("glCompileShader")))
c.gpBindRenderbufferEXT = (C.GPBINDRENDERBUFFEREXT)(c.getProcAddress("glBindRenderbufferEXT")) c.gpCreateProgram = C.GPCREATEPROGRAM(unsafe.Pointer(g.get("glCreateProgram")))
c.gpBindTexture = (C.GPBINDTEXTURE)(c.getProcAddress("glBindTexture")) c.gpCreateShader = C.GPCREATESHADER(unsafe.Pointer(g.get("glCreateShader")))
if c.gpBindTexture == nil { c.gpDeleteBuffers = C.GPDELETEBUFFERS(unsafe.Pointer(g.get("glDeleteBuffers")))
return errors.New("gl: glBindTexture is missing") c.gpDeleteFramebuffersEXT = C.GPDELETEFRAMEBUFFERSEXT(unsafe.Pointer(g.get("glDeleteFramebuffersEXT")))
} c.gpDeleteProgram = C.GPDELETEPROGRAM(unsafe.Pointer(g.get("glDeleteProgram")))
c.gpBlendEquationSeparate = (C.GPBLENDEQUATIONSEPARATE)(c.getProcAddress("glBlendEquationSeparate")) c.gpDeleteRenderbuffersEXT = C.GPDELETERENDERBUFFERSEXT(unsafe.Pointer(g.get("glDeleteRenderbuffersEXT")))
if c.gpBlendEquationSeparate == nil { c.gpDeleteShader = C.GPDELETESHADER(unsafe.Pointer(g.get("glDeleteShader")))
return errors.New("gl: glBlendEquationSeparate is missing") c.gpDeleteTextures = C.GPDELETETEXTURES(unsafe.Pointer(g.get("glDeleteTextures")))
} c.gpDisable = C.GPDISABLE(unsafe.Pointer(g.get("glDisable")))
c.gpBlendFuncSeparate = (C.GPBLENDFUNCSEPARATE)(c.getProcAddress("glBlendFuncSeparate")) c.gpDisableVertexAttribArray = C.GPDISABLEVERTEXATTRIBARRAY(unsafe.Pointer(g.get("glDisableVertexAttribArray")))
if c.gpBlendFuncSeparate == nil { c.gpDrawElements = C.GPDRAWELEMENTS(unsafe.Pointer(g.get("glDrawElements")))
return errors.New("gl: glBlendFuncSeparate is missing") c.gpEnable = C.GPENABLE(unsafe.Pointer(g.get("glEnable")))
} c.gpEnableVertexAttribArray = C.GPENABLEVERTEXATTRIBARRAY(unsafe.Pointer(g.get("glEnableVertexAttribArray")))
c.gpBufferData = (C.GPBUFFERDATA)(c.getProcAddress("glBufferData")) c.gpFlush = C.GPFLUSH(unsafe.Pointer(g.get("glFlush")))
if c.gpBufferData == nil { c.gpFramebufferRenderbufferEXT = C.GPFRAMEBUFFERRENDERBUFFEREXT(unsafe.Pointer(g.get("glFramebufferRenderbufferEXT")))
return errors.New("gl: glBufferData is missing") c.gpFramebufferTexture2DEXT = C.GPFRAMEBUFFERTEXTURE2DEXT(unsafe.Pointer(g.get("glFramebufferTexture2DEXT")))
} c.gpGenBuffers = C.GPGENBUFFERS(unsafe.Pointer(g.get("glGenBuffers")))
c.gpBufferSubData = (C.GPBUFFERSUBDATA)(c.getProcAddress("glBufferSubData")) c.gpGenFramebuffersEXT = C.GPGENFRAMEBUFFERSEXT(unsafe.Pointer(g.get("glGenFramebuffersEXT")))
if c.gpBufferSubData == nil { c.gpGenRenderbuffersEXT = C.GPGENRENDERBUFFERSEXT(unsafe.Pointer(g.get("glGenRenderbuffersEXT")))
return errors.New("gl: glBufferSubData is missing") c.gpGenTextures = C.GPGENTEXTURES(unsafe.Pointer(g.get("glGenTextures")))
} c.gpGetError = C.GPGETERROR(unsafe.Pointer(g.get("glGetError")))
c.gpCheckFramebufferStatusEXT = (C.GPCHECKFRAMEBUFFERSTATUSEXT)(c.getProcAddress("glCheckFramebufferStatusEXT")) c.gpGetIntegerv = C.GPGETINTEGERV(unsafe.Pointer(g.get("glGetIntegerv")))
c.gpClear = (C.GPCLEAR)(c.getProcAddress("glClear")) c.gpGetProgramInfoLog = C.GPGETPROGRAMINFOLOG(unsafe.Pointer(g.get("glGetProgramInfoLog")))
if c.gpClear == nil { c.gpGetProgramiv = C.GPGETPROGRAMIV(unsafe.Pointer(g.get("glGetProgramiv")))
return errors.New("gl: glClear is missing") c.gpGetShaderInfoLog = C.GPGETSHADERINFOLOG(unsafe.Pointer(g.get("glGetShaderInfoLog")))
} c.gpGetShaderiv = C.GPGETSHADERIV(unsafe.Pointer(g.get("glGetShaderiv")))
c.gpColorMask = (C.GPCOLORMASK)(c.getProcAddress("glColorMask")) c.gpGetUniformLocation = C.GPGETUNIFORMLOCATION(unsafe.Pointer(g.get("glGetUniformLocation")))
if c.gpColorMask == nil { c.gpIsFramebufferEXT = C.GPISFRAMEBUFFEREXT(unsafe.Pointer(g.get("glIsFramebufferEXT")))
return errors.New("gl: glColorMask is missing") c.gpIsProgram = C.GPISPROGRAM(unsafe.Pointer(g.get("glIsProgram")))
} c.gpIsRenderbufferEXT = C.GPISRENDERBUFFEREXT(unsafe.Pointer(g.get("glIsRenderbufferEXT")))
c.gpCompileShader = (C.GPCOMPILESHADER)(c.getProcAddress("glCompileShader")) c.gpIsTexture = C.GPISTEXTURE(unsafe.Pointer(g.get("glIsTexture")))
if c.gpCompileShader == nil { c.gpLinkProgram = C.GPLINKPROGRAM(unsafe.Pointer(g.get("glLinkProgram")))
return errors.New("gl: glCompileShader is missing") c.gpPixelStorei = C.GPPIXELSTOREI(unsafe.Pointer(g.get("glPixelStorei")))
} c.gpReadPixels = C.GPREADPIXELS(unsafe.Pointer(g.get("glReadPixels")))
c.gpCreateProgram = (C.GPCREATEPROGRAM)(c.getProcAddress("glCreateProgram")) c.gpRenderbufferStorageEXT = C.GPRENDERBUFFERSTORAGEEXT(unsafe.Pointer(g.get("glRenderbufferStorageEXT")))
if c.gpCreateProgram == nil { c.gpScissor = C.GPSCISSOR(unsafe.Pointer(g.get("glScissor")))
return errors.New("gl: glCreateProgram is missing") c.gpShaderSource = C.GPSHADERSOURCE(unsafe.Pointer(g.get("glShaderSource")))
} c.gpStencilFunc = C.GPSTENCILFUNC(unsafe.Pointer(g.get("glStencilFunc")))
c.gpCreateShader = (C.GPCREATESHADER)(c.getProcAddress("glCreateShader")) c.gpStencilOp = C.GPSTENCILOP(unsafe.Pointer(g.get("glStencilOp")))
if c.gpCreateShader == nil { c.gpTexImage2D = C.GPTEXIMAGE2D(unsafe.Pointer(g.get("glTexImage2D")))
return errors.New("gl: glCreateShader is missing") c.gpTexParameteri = C.GPTEXPARAMETERI(unsafe.Pointer(g.get("glTexParameteri")))
} c.gpTexSubImage2D = C.GPTEXSUBIMAGE2D(unsafe.Pointer(g.get("glTexSubImage2D")))
c.gpDeleteBuffers = (C.GPDELETEBUFFERS)(c.getProcAddress("glDeleteBuffers")) c.gpUniform1fv = C.GPUNIFORM1FV(unsafe.Pointer(g.get("glUniform1fv")))
if c.gpDeleteBuffers == nil { c.gpUniform1i = C.GPUNIFORM1I(unsafe.Pointer(g.get("glUniform1i")))
return errors.New("gl: glDeleteBuffers is missing") c.gpUniform1iv = C.GPUNIFORM1IV(unsafe.Pointer(g.get("glUniform1iv")))
} c.gpUniform2fv = C.GPUNIFORM2FV(unsafe.Pointer(g.get("glUniform2fv")))
c.gpDeleteFramebuffersEXT = (C.GPDELETEFRAMEBUFFERSEXT)(c.getProcAddress("glDeleteFramebuffersEXT")) c.gpUniform2iv = C.GPUNIFORM2IV(unsafe.Pointer(g.get("glUniform2iv")))
c.gpDeleteProgram = (C.GPDELETEPROGRAM)(c.getProcAddress("glDeleteProgram")) c.gpUniform3fv = C.GPUNIFORM3FV(unsafe.Pointer(g.get("glUniform3fv")))
if c.gpDeleteProgram == nil { c.gpUniform3iv = C.GPUNIFORM3IV(unsafe.Pointer(g.get("glUniform3iv")))
return errors.New("gl: glDeleteProgram is missing") c.gpUniform4fv = C.GPUNIFORM4FV(unsafe.Pointer(g.get("glUniform4fv")))
} c.gpUniform4iv = C.GPUNIFORM4IV(unsafe.Pointer(g.get("glUniform4iv")))
c.gpDeleteRenderbuffersEXT = (C.GPDELETERENDERBUFFERSEXT)(c.getProcAddress("glDeleteRenderbuffersEXT")) c.gpUniformMatrix2fv = C.GPUNIFORMMATRIX2FV(unsafe.Pointer(g.get("glUniformMatrix2fv")))
c.gpDeleteShader = (C.GPDELETESHADER)(c.getProcAddress("glDeleteShader")) c.gpUniformMatrix3fv = C.GPUNIFORMMATRIX3FV(unsafe.Pointer(g.get("glUniformMatrix3fv")))
if c.gpDeleteShader == nil { c.gpUniformMatrix4fv = C.GPUNIFORMMATRIX4FV(unsafe.Pointer(g.get("glUniformMatrix4fv")))
return errors.New("gl: glDeleteShader is missing") c.gpUseProgram = C.GPUSEPROGRAM(unsafe.Pointer(g.get("glUseProgram")))
} c.gpVertexAttribPointer = C.GPVERTEXATTRIBPOINTER(unsafe.Pointer(g.get("glVertexAttribPointer")))
c.gpDeleteTextures = (C.GPDELETETEXTURES)(c.getProcAddress("glDeleteTextures")) c.gpViewport = C.GPVIEWPORT(unsafe.Pointer(g.get("glViewport")))
if c.gpDeleteTextures == nil {
return errors.New("gl: glDeleteTextures is missing") return g.error()
}
c.gpDisable = (C.GPDISABLE)(c.getProcAddress("glDisable"))
if c.gpDisable == nil {
return errors.New("gl: glDisable is missing")
}
c.gpDisableVertexAttribArray = (C.GPDISABLEVERTEXATTRIBARRAY)(c.getProcAddress("glDisableVertexAttribArray"))
if c.gpDisableVertexAttribArray == nil {
return errors.New("gl: glDisableVertexAttribArray is missing")
}
c.gpDrawElements = (C.GPDRAWELEMENTS)(c.getProcAddress("glDrawElements"))
if c.gpDrawElements == nil {
return errors.New("gl: glDrawElements is missing")
}
c.gpEnable = (C.GPENABLE)(c.getProcAddress("glEnable"))
if c.gpEnable == nil {
return errors.New("gl: glEnable is missing")
}
c.gpEnableVertexAttribArray = (C.GPENABLEVERTEXATTRIBARRAY)(c.getProcAddress("glEnableVertexAttribArray"))
if c.gpEnableVertexAttribArray == nil {
return errors.New("gl: glEnableVertexAttribArray is missing")
}
c.gpFlush = (C.GPFLUSH)(c.getProcAddress("glFlush"))
if c.gpFlush == nil {
return errors.New("gl: glFlush is missing")
}
c.gpFramebufferRenderbufferEXT = (C.GPFRAMEBUFFERRENDERBUFFEREXT)(c.getProcAddress("glFramebufferRenderbufferEXT"))
c.gpFramebufferTexture2DEXT = (C.GPFRAMEBUFFERTEXTURE2DEXT)(c.getProcAddress("glFramebufferTexture2DEXT"))
c.gpGenBuffers = (C.GPGENBUFFERS)(c.getProcAddress("glGenBuffers"))
if c.gpGenBuffers == nil {
return errors.New("gl: glGenBuffers is missing")
}
c.gpGenFramebuffersEXT = (C.GPGENFRAMEBUFFERSEXT)(c.getProcAddress("glGenFramebuffersEXT"))
c.gpGenRenderbuffersEXT = (C.GPGENRENDERBUFFERSEXT)(c.getProcAddress("glGenRenderbuffersEXT"))
c.gpGenTextures = (C.GPGENTEXTURES)(c.getProcAddress("glGenTextures"))
if c.gpGenTextures == nil {
return errors.New("gl: glGenTextures is missing")
}
c.gpGetError = (C.GPGETERROR)(c.getProcAddress("glGetError"))
if c.gpGetError == nil {
return errors.New("gl: glGetError is missing")
}
c.gpGetIntegerv = (C.GPGETINTEGERV)(c.getProcAddress("glGetIntegerv"))
if c.gpGetIntegerv == nil {
return errors.New("gl: glGetIntegerv is missing")
}
c.gpGetProgramInfoLog = (C.GPGETPROGRAMINFOLOG)(c.getProcAddress("glGetProgramInfoLog"))
if c.gpGetProgramInfoLog == nil {
return errors.New("gl: glGetProgramInfoLog is missing")
}
c.gpGetProgramiv = (C.GPGETPROGRAMIV)(c.getProcAddress("glGetProgramiv"))
if c.gpGetProgramiv == nil {
return errors.New("gl: glGetProgramiv is missing")
}
c.gpGetShaderInfoLog = (C.GPGETSHADERINFOLOG)(c.getProcAddress("glGetShaderInfoLog"))
if c.gpGetShaderInfoLog == nil {
return errors.New("gl: glGetShaderInfoLog is missing")
}
c.gpGetShaderiv = (C.GPGETSHADERIV)(c.getProcAddress("glGetShaderiv"))
if c.gpGetShaderiv == nil {
return errors.New("gl: glGetShaderiv is missing")
}
c.gpGetUniformLocation = (C.GPGETUNIFORMLOCATION)(c.getProcAddress("glGetUniformLocation"))
if c.gpGetUniformLocation == nil {
return errors.New("gl: glGetUniformLocation is missing")
}
c.gpIsFramebufferEXT = (C.GPISFRAMEBUFFEREXT)(c.getProcAddress("glIsFramebufferEXT"))
c.gpIsProgram = (C.GPISPROGRAM)(c.getProcAddress("glIsProgram"))
if c.gpIsProgram == nil {
return errors.New("gl: glIsProgram is missing")
}
c.gpIsRenderbufferEXT = (C.GPISRENDERBUFFEREXT)(c.getProcAddress("glIsRenderbufferEXT"))
c.gpIsTexture = (C.GPISTEXTURE)(c.getProcAddress("glIsTexture"))
if c.gpIsTexture == nil {
return errors.New("gl: glIsTexture is missing")
}
c.gpLinkProgram = (C.GPLINKPROGRAM)(c.getProcAddress("glLinkProgram"))
if c.gpLinkProgram == nil {
return errors.New("gl: glLinkProgram is missing")
}
c.gpPixelStorei = (C.GPPIXELSTOREI)(c.getProcAddress("glPixelStorei"))
if c.gpPixelStorei == nil {
return errors.New("gl: glPixelStorei is missing")
}
c.gpReadPixels = (C.GPREADPIXELS)(c.getProcAddress("glReadPixels"))
if c.gpReadPixels == nil {
return errors.New("gl: glReadPixels is missing")
}
c.gpRenderbufferStorageEXT = (C.GPRENDERBUFFERSTORAGEEXT)(c.getProcAddress("glRenderbufferStorageEXT"))
c.gpScissor = (C.GPSCISSOR)(c.getProcAddress("glScissor"))
if c.gpScissor == nil {
return errors.New("gl: glScissor is missing")
}
c.gpShaderSource = (C.GPSHADERSOURCE)(c.getProcAddress("glShaderSource"))
if c.gpShaderSource == nil {
return errors.New("gl: glShaderSource is missing")
}
c.gpStencilFunc = (C.GPSTENCILFUNC)(c.getProcAddress("glStencilFunc"))
if c.gpStencilFunc == nil {
return errors.New("gl: glStencilFunc is missing")
}
c.gpStencilOp = (C.GPSTENCILOP)(c.getProcAddress("glStencilOp"))
if c.gpStencilOp == nil {
return errors.New("gl: glStencilOp is missing")
}
c.gpTexImage2D = (C.GPTEXIMAGE2D)(c.getProcAddress("glTexImage2D"))
if c.gpTexImage2D == nil {
return errors.New("gl: glTexImage2D is missing")
}
c.gpTexParameteri = (C.GPTEXPARAMETERI)(c.getProcAddress("glTexParameteri"))
if c.gpTexParameteri == nil {
return errors.New("gl: glTexParameteri is missing")
}
c.gpTexSubImage2D = (C.GPTEXSUBIMAGE2D)(c.getProcAddress("glTexSubImage2D"))
if c.gpTexSubImage2D == nil {
return errors.New("gl: glTexSubImage2D is missing")
}
c.gpUniform1fv = (C.GPUNIFORM1FV)(c.getProcAddress("glUniform1fv"))
if c.gpUniform1fv == nil {
return errors.New("gl: glUniform1fv is missing")
}
c.gpUniform1i = (C.GPUNIFORM1I)(c.getProcAddress("glUniform1i"))
if c.gpUniform1i == nil {
return errors.New("gl: glUniform1i is missing")
}
c.gpUniform1iv = (C.GPUNIFORM1IV)(c.getProcAddress("glUniform1iv"))
if c.gpUniform1iv == nil {
return errors.New("gl: glUniform1iv is missing")
}
c.gpUniform2fv = (C.GPUNIFORM2FV)(c.getProcAddress("glUniform2fv"))
if c.gpUniform2fv == nil {
return errors.New("gl: glUniform2fv is missing")
}
c.gpUniform2iv = (C.GPUNIFORM2IV)(c.getProcAddress("glUniform2iv"))
if c.gpUniform2iv == nil {
return errors.New("gl: glUniform2iv is missing")
}
c.gpUniform3fv = (C.GPUNIFORM3FV)(c.getProcAddress("glUniform3fv"))
if c.gpUniform3fv == nil {
return errors.New("gl: glUniform3fv is missing")
}
c.gpUniform3iv = (C.GPUNIFORM3IV)(c.getProcAddress("glUniform3iv"))
if c.gpUniform3iv == nil {
return errors.New("gl: glUniform3iv is missing")
}
c.gpUniform4fv = (C.GPUNIFORM4FV)(c.getProcAddress("glUniform4fv"))
if c.gpUniform4fv == nil {
return errors.New("gl: glUniform4fv is missing")
}
c.gpUniform4iv = (C.GPUNIFORM4IV)(c.getProcAddress("glUniform4iv"))
if c.gpUniform4iv == nil {
return errors.New("gl: glUniform4iv is missing")
}
c.gpUniformMatrix2fv = (C.GPUNIFORMMATRIX2FV)(c.getProcAddress("glUniformMatrix2fv"))
if c.gpUniformMatrix2fv == nil {
return errors.New("gl: glUniformMatrix2fv is missing")
}
c.gpUniformMatrix3fv = (C.GPUNIFORMMATRIX3FV)(c.getProcAddress("glUniformMatrix3fv"))
if c.gpUniformMatrix3fv == nil {
return errors.New("gl: glUniformMatrix3fv is missing")
}
c.gpUniformMatrix4fv = (C.GPUNIFORMMATRIX4FV)(c.getProcAddress("glUniformMatrix4fv"))
if c.gpUniformMatrix4fv == nil {
return errors.New("gl: glUniformMatrix4fv is missing")
}
c.gpUseProgram = (C.GPUSEPROGRAM)(c.getProcAddress("glUseProgram"))
if c.gpUseProgram == nil {
return errors.New("gl: glUseProgram is missing")
}
c.gpVertexAttribPointer = (C.GPVERTEXATTRIBPOINTER)(c.getProcAddress("glVertexAttribPointer"))
if c.gpVertexAttribPointer == nil {
return errors.New("gl: glVertexAttribPointer is missing")
}
c.gpViewport = (C.GPVIEWPORT)(c.getProcAddress("glViewport"))
if c.gpViewport == nil {
return errors.New("gl: glViewport is missing")
}
return nil
} }

View File

@ -17,7 +17,6 @@
package gl package gl
import ( import (
"errors"
"runtime" "runtime"
"unsafe" "unsafe"
@ -462,259 +461,82 @@ func (c *defaultContext) Viewport(x int32, y int32, width int32, height int32) {
} }
func (c *defaultContext) LoadFunctions() error { func (c *defaultContext) LoadFunctions() error {
c.gpActiveTexture = c.getProcAddress("glActiveTexture") g := procAddressGetter{ctx: c}
if c.gpActiveTexture == 0 {
return errors.New("gl: glActiveTexture is missing") c.gpActiveTexture = g.get("glActiveTexture")
} c.gpAttachShader = g.get("glAttachShader")
c.gpAttachShader = c.getProcAddress("glAttachShader") c.gpBindAttribLocation = g.get("glBindAttribLocation")
if c.gpAttachShader == 0 { c.gpBindBuffer = g.get("glBindBuffer")
return errors.New("gl: glAttachShader is missing") c.gpBindFramebufferEXT = g.get("glBindFramebufferEXT")
} c.gpBindRenderbufferEXT = g.get("glBindRenderbufferEXT")
c.gpBindAttribLocation = c.getProcAddress("glBindAttribLocation") c.gpBindTexture = g.get("glBindTexture")
if c.gpBindAttribLocation == 0 { c.gpBlendEquationSeparate = g.get("glBlendEquationSeparate")
return errors.New("gl: glBindAttribLocation is missing") c.gpBlendFuncSeparate = g.get("glBlendFuncSeparate")
} c.gpBufferData = g.get("glBufferData")
c.gpBindBuffer = c.getProcAddress("glBindBuffer") c.gpBufferSubData = g.get("glBufferSubData")
if c.gpBindBuffer == 0 { c.gpCheckFramebufferStatusEXT = g.get("glCheckFramebufferStatusEXT")
return errors.New("gl: glBindBuffer is missing") c.gpClear = g.get("glClear")
} c.gpColorMask = g.get("glColorMask")
c.gpBindFramebufferEXT = c.getProcAddress("glBindFramebufferEXT") c.gpCompileShader = g.get("glCompileShader")
c.gpBindRenderbufferEXT = c.getProcAddress("glBindRenderbufferEXT") c.gpCreateProgram = g.get("glCreateProgram")
c.gpBindTexture = c.getProcAddress("glBindTexture") c.gpCreateShader = g.get("glCreateShader")
if c.gpBindTexture == 0 { c.gpDeleteBuffers = g.get("glDeleteBuffers")
return errors.New("gl: glBindTexture is missing") c.gpDeleteFramebuffersEXT = g.get("glDeleteFramebuffersEXT")
} c.gpDeleteProgram = g.get("glDeleteProgram")
c.gpBlendEquationSeparate = c.getProcAddress("glBlendEquationSeparate") c.gpDeleteRenderbuffersEXT = g.get("glDeleteRenderbuffersEXT")
if c.gpBlendEquationSeparate == 0 { c.gpDeleteShader = g.get("glDeleteShader")
return errors.New("gl: glBlendEquationSeparate is missing") c.gpDeleteTextures = g.get("glDeleteTextures")
} c.gpDisable = g.get("glDisable")
c.gpBlendFuncSeparate = c.getProcAddress("glBlendFuncSeparate") c.gpDisableVertexAttribArray = g.get("glDisableVertexAttribArray")
if c.gpBlendFuncSeparate == 0 { c.gpDrawElements = g.get("glDrawElements")
return errors.New("gl: glBlendFuncSeparate is missing") c.gpEnable = g.get("glEnable")
} c.gpEnableVertexAttribArray = g.get("glEnableVertexAttribArray")
c.gpBufferData = c.getProcAddress("glBufferData") c.gpFlush = g.get("glFlush")
if c.gpBufferData == 0 { c.gpFramebufferRenderbufferEXT = g.get("glFramebufferRenderbufferEXT")
return errors.New("gl: glBufferData is missing") c.gpFramebufferTexture2DEXT = g.get("glFramebufferTexture2DEXT")
} c.gpGenBuffers = g.get("glGenBuffers")
c.gpBufferSubData = c.getProcAddress("glBufferSubData") c.gpGenFramebuffersEXT = g.get("glGenFramebuffersEXT")
if c.gpBufferSubData == 0 { c.gpGenRenderbuffersEXT = g.get("glGenRenderbuffersEXT")
return errors.New("gl: glBufferSubData is missing") c.gpGenTextures = g.get("glGenTextures")
} c.gpGetError = g.get("glGetError")
c.gpCheckFramebufferStatusEXT = c.getProcAddress("glCheckFramebufferStatusEXT") c.gpGetIntegerv = g.get("glGetIntegerv")
c.gpClear = c.getProcAddress("glClear") c.gpGetProgramInfoLog = g.get("glGetProgramInfoLog")
if c.gpClear == 0 { c.gpGetProgramiv = g.get("glGetProgramiv")
return errors.New("gl: glClear is missing") c.gpGetShaderInfoLog = g.get("glGetShaderInfoLog")
} c.gpGetShaderiv = g.get("glGetShaderiv")
c.gpColorMask = c.getProcAddress("glColorMask") c.gpGetUniformLocation = g.get("glGetUniformLocation")
if c.gpColorMask == 0 { c.gpIsFramebufferEXT = g.get("glIsFramebufferEXT")
return errors.New("gl: glColorMask is missing") c.gpIsProgram = g.get("glIsProgram")
} c.gpIsRenderbufferEXT = g.get("glIsRenderbufferEXT")
c.gpCompileShader = c.getProcAddress("glCompileShader") c.gpIsTexture = g.get("glIsTexture")
if c.gpCompileShader == 0 { c.gpLinkProgram = g.get("glLinkProgram")
return errors.New("gl: glCompileShader is missing") c.gpPixelStorei = g.get("glPixelStorei")
} c.gpReadPixels = g.get("glReadPixels")
c.gpCreateProgram = c.getProcAddress("glCreateProgram") c.gpRenderbufferStorageEXT = g.get("glRenderbufferStorageEXT")
if c.gpCreateProgram == 0 { c.gpScissor = g.get("glScissor")
return errors.New("gl: glCreateProgram is missing") c.gpShaderSource = g.get("glShaderSource")
} c.gpStencilFunc = g.get("glStencilFunc")
c.gpCreateShader = c.getProcAddress("glCreateShader") c.gpStencilOp = g.get("glStencilOp")
if c.gpCreateShader == 0 { c.gpTexImage2D = g.get("glTexImage2D")
return errors.New("gl: glCreateShader is missing") c.gpTexParameteri = g.get("glTexParameteri")
} c.gpTexSubImage2D = g.get("glTexSubImage2D")
c.gpDeleteBuffers = c.getProcAddress("glDeleteBuffers") c.gpUniform1fv = g.get("glUniform1fv")
if c.gpDeleteBuffers == 0 { c.gpUniform1i = g.get("glUniform1i")
return errors.New("gl: glDeleteBuffers is missing") c.gpUniform1iv = g.get("glUniform1iv")
} c.gpUniform2fv = g.get("glUniform2fv")
c.gpDeleteFramebuffersEXT = c.getProcAddress("glDeleteFramebuffersEXT") c.gpUniform2iv = g.get("glUniform2iv")
c.gpDeleteProgram = c.getProcAddress("glDeleteProgram") c.gpUniform3fv = g.get("glUniform3fv")
if c.gpDeleteProgram == 0 { c.gpUniform3iv = g.get("glUniform3iv")
return errors.New("gl: glDeleteProgram is missing") c.gpUniform4fv = g.get("glUniform4fv")
} c.gpUniform4iv = g.get("glUniform4iv")
c.gpDeleteRenderbuffersEXT = c.getProcAddress("glDeleteRenderbuffersEXT") c.gpUniformMatrix2fv = g.get("glUniformMatrix2fv")
c.gpDeleteShader = c.getProcAddress("glDeleteShader") c.gpUniformMatrix3fv = g.get("glUniformMatrix3fv")
if c.gpDeleteShader == 0 { c.gpUniformMatrix4fv = g.get("glUniformMatrix4fv")
return errors.New("gl: glDeleteShader is missing") c.gpUseProgram = g.get("glUseProgram")
} c.gpVertexAttribPointer = g.get("glVertexAttribPointer")
c.gpDeleteTextures = c.getProcAddress("glDeleteTextures") c.gpViewport = g.get("glViewport")
if c.gpDeleteTextures == 0 {
return errors.New("gl: glDeleteTextures is missing") return g.error()
}
c.gpDisable = c.getProcAddress("glDisable")
if c.gpDisable == 0 {
return errors.New("gl: glDisable is missing")
}
c.gpDisableVertexAttribArray = c.getProcAddress("glDisableVertexAttribArray")
if c.gpDisableVertexAttribArray == 0 {
return errors.New("gl: glDisableVertexAttribArray is missing")
}
c.gpDrawElements = c.getProcAddress("glDrawElements")
if c.gpDrawElements == 0 {
return errors.New("gl: glDrawElements is missing")
}
c.gpEnable = c.getProcAddress("glEnable")
if c.gpEnable == 0 {
return errors.New("gl: glEnable is missing")
}
c.gpEnableVertexAttribArray = c.getProcAddress("glEnableVertexAttribArray")
if c.gpEnableVertexAttribArray == 0 {
return errors.New("gl: glEnableVertexAttribArray is missing")
}
c.gpFlush = c.getProcAddress("glFlush")
if c.gpFlush == 0 {
return errors.New("gl: glFlush is missing")
}
c.gpFramebufferRenderbufferEXT = c.getProcAddress("glFramebufferRenderbufferEXT")
c.gpFramebufferTexture2DEXT = c.getProcAddress("glFramebufferTexture2DEXT")
c.gpGenBuffers = c.getProcAddress("glGenBuffers")
if c.gpGenBuffers == 0 {
return errors.New("gl: glGenBuffers is missing")
}
c.gpGenFramebuffersEXT = c.getProcAddress("glGenFramebuffersEXT")
c.gpGenRenderbuffersEXT = c.getProcAddress("glGenRenderbuffersEXT")
c.gpGenTextures = c.getProcAddress("glGenTextures")
if c.gpGenTextures == 0 {
return errors.New("gl: glGenTextures is missing")
}
c.gpGetError = c.getProcAddress("glGetError")
if c.gpGetError == 0 {
return errors.New("gl: glGetError is missing")
}
c.gpGetIntegerv = c.getProcAddress("glGetIntegerv")
if c.gpGetIntegerv == 0 {
return errors.New("gl: glGetIntegerv is missing")
}
c.gpGetProgramInfoLog = c.getProcAddress("glGetProgramInfoLog")
if c.gpGetProgramInfoLog == 0 {
return errors.New("gl: glGetProgramInfoLog is missing")
}
c.gpGetProgramiv = c.getProcAddress("glGetProgramiv")
if c.gpGetProgramiv == 0 {
return errors.New("gl: glGetProgramiv is missing")
}
c.gpGetShaderInfoLog = c.getProcAddress("glGetShaderInfoLog")
if c.gpGetShaderInfoLog == 0 {
return errors.New("gl: glGetShaderInfoLog is missing")
}
c.gpGetShaderiv = c.getProcAddress("glGetShaderiv")
if c.gpGetShaderiv == 0 {
return errors.New("gl: glGetShaderiv is missing")
}
c.gpGetUniformLocation = c.getProcAddress("glGetUniformLocation")
if c.gpGetUniformLocation == 0 {
return errors.New("gl: glGetUniformLocation is missing")
}
c.gpIsFramebufferEXT = c.getProcAddress("glIsFramebufferEXT")
c.gpIsProgram = c.getProcAddress("glIsProgram")
if c.gpIsProgram == 0 {
return errors.New("gl: glIsProgram is missing")
}
c.gpIsRenderbufferEXT = c.getProcAddress("glIsRenderbufferEXT")
c.gpIsTexture = c.getProcAddress("glIsTexture")
if c.gpIsTexture == 0 {
return errors.New("gl: glIsTexture is missing")
}
c.gpLinkProgram = c.getProcAddress("glLinkProgram")
if c.gpLinkProgram == 0 {
return errors.New("gl: glLinkProgram is missing")
}
c.gpPixelStorei = c.getProcAddress("glPixelStorei")
if c.gpPixelStorei == 0 {
return errors.New("gl: glPixelStorei is missing")
}
c.gpReadPixels = c.getProcAddress("glReadPixels")
if c.gpReadPixels == 0 {
return errors.New("gl: glReadPixels is missing")
}
c.gpRenderbufferStorageEXT = c.getProcAddress("glRenderbufferStorageEXT")
c.gpScissor = c.getProcAddress("glScissor")
if c.gpScissor == 0 {
return errors.New("gl: glScissor is missing")
}
c.gpShaderSource = c.getProcAddress("glShaderSource")
if c.gpShaderSource == 0 {
return errors.New("gl: glShaderSource is missing")
}
c.gpStencilFunc = c.getProcAddress("glStencilFunc")
if c.gpStencilFunc == 0 {
return errors.New("gl: glStencilFunc is missing")
}
c.gpStencilOp = c.getProcAddress("glStencilOp")
if c.gpStencilOp == 0 {
return errors.New("gl: glStencilOp is missing")
}
c.gpTexImage2D = c.getProcAddress("glTexImage2D")
if c.gpTexImage2D == 0 {
return errors.New("gl: glTexImage2D is missing")
}
c.gpTexParameteri = c.getProcAddress("glTexParameteri")
if c.gpTexParameteri == 0 {
return errors.New("gl: glTexParameteri is missing")
}
c.gpTexSubImage2D = c.getProcAddress("glTexSubImage2D")
if c.gpTexSubImage2D == 0 {
return errors.New("gl: glTexSubImage2D is missing")
}
c.gpUniform1fv = c.getProcAddress("glUniform1fv")
if c.gpUniform1fv == 0 {
return errors.New("gl: glUniform1fv is missing")
}
c.gpUniform1i = c.getProcAddress("glUniform1i")
if c.gpUniform1i == 0 {
return errors.New("gl: glUniform1i is missing")
}
c.gpUniform1iv = c.getProcAddress("glUniform1iv")
if c.gpUniform1iv == 0 {
return errors.New("gl: glUniform1iv is missing")
}
c.gpUniform2fv = c.getProcAddress("glUniform2fv")
if c.gpUniform2fv == 0 {
return errors.New("gl: glUniform2fv is missing")
}
c.gpUniform2iv = c.getProcAddress("glUniform2iv")
if c.gpUniform2iv == 0 {
return errors.New("gl: glUniform2iv is missing")
}
c.gpUniform3fv = c.getProcAddress("glUniform3fv")
if c.gpUniform3fv == 0 {
return errors.New("gl: glUniform3fv is missing")
}
c.gpUniform3iv = c.getProcAddress("glUniform3iv")
if c.gpUniform3iv == 0 {
return errors.New("gl: glUniform3iv is missing")
}
c.gpUniform4fv = c.getProcAddress("glUniform4fv")
if c.gpUniform4fv == 0 {
return errors.New("gl: glUniform4fv is missing")
}
c.gpUniform4iv = c.getProcAddress("glUniform4iv")
if c.gpUniform4iv == 0 {
return errors.New("gl: glUniform4iv is missing")
}
c.gpUniformMatrix2fv = c.getProcAddress("glUniformMatrix2fv")
if c.gpUniformMatrix2fv == 0 {
return errors.New("gl: glUniformMatrix2fv is missing")
}
c.gpUniformMatrix3fv = c.getProcAddress("glUniformMatrix3fv")
if c.gpUniformMatrix3fv == 0 {
return errors.New("gl: glUniformMatrix3fv is missing")
}
c.gpUniformMatrix4fv = c.getProcAddress("glUniformMatrix4fv")
if c.gpUniformMatrix4fv == 0 {
return errors.New("gl: glUniformMatrix4fv is missing")
}
c.gpUseProgram = c.getProcAddress("glUseProgram")
if c.gpUseProgram == 0 {
return errors.New("gl: glUseProgram is missing")
}
c.gpVertexAttribPointer = c.getProcAddress("glVertexAttribPointer")
if c.gpVertexAttribPointer == 0 {
return errors.New("gl: glVertexAttribPointer is missing")
}
c.gpViewport = c.getProcAddress("glViewport")
if c.gpViewport == 0 {
return errors.New("gl: glViewport is missing")
}
return nil
} }
// cStr takes a Go string (with or without null-termination) // cStr takes a Go string (with or without null-termination)

View File

@ -0,0 +1,41 @@
// Copyright 2023 The Ebitengine Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gl
import (
"fmt"
)
type procAddressGetter struct {
ctx *defaultContext
err error
}
func (p *procAddressGetter) get(name string) uintptr {
proc, err := p.ctx.getProcAddress(name)
if err != nil {
p.err = fmt.Errorf("gl: %s is missing: %w", name, err)
return 0
}
if proc == 0 {
p.err = fmt.Errorf("gl: %s is missing", name)
return 0
}
return proc
}
func (p *procAddressGetter) error() error {
return p.err
}

View File

@ -43,14 +43,13 @@ func (c *defaultContext) init() error {
return fmt.Errorf("gl: failed to load: OpenGL.framework: %v, OpenGLES.framework: %v", errGL, errGLES) return fmt.Errorf("gl: failed to load: OpenGL.framework: %v, OpenGLES.framework: %v", errGL, errGLES)
} }
func (c *defaultContext) getProcAddress(name string) uintptr { func (c *defaultContext) getProcAddress(name string) (uintptr, error) {
if c.isES { if c.isES {
name = strings.TrimSuffix(name, "EXT") name = strings.TrimSuffix(name, "EXT")
} }
proc, err := purego.Dlsym(opengl, name) proc, err := purego.Dlsym(opengl, name)
if err != nil { if err != nil {
// The proc is not found. return 0, err
return 0
} }
return proc return proc, nil
} }

View File

@ -32,8 +32,8 @@ func (c *defaultContext) init() error {
return nil return nil
} }
func (c *defaultContext) getProcAddress(name string) unsafe.Pointer { func (c *defaultContext) getProcAddress(name string) (uintptr, error) {
cname := C.CString(name) cname := C.CString(name)
defer C.free(unsafe.Pointer(cname)) defer C.free(unsafe.Pointer(cname))
return C.getProcAddress(cname) return uintptr(C.getProcAddress(cname)), nil
} }

View File

@ -94,22 +94,22 @@ func (c *defaultContext) init() error {
return fmt.Errorf("gl: failed to load libGL.so and libGLESv2.so") return fmt.Errorf("gl: failed to load libGL.so and libGLESv2.so")
} }
func (c *defaultContext) getProcAddress(name string) unsafe.Pointer { func (c *defaultContext) getProcAddress(name string) (uintptr, error) {
if c.isES { if c.isES {
return getProcAddressGLES(name) return getProcAddressGLES(name), nil
} }
return getProcAddressGL(name) return getProcAddressGL(name), nil
} }
func getProcAddressGL(name string) unsafe.Pointer { func getProcAddressGL(name string) uintptr {
cname := C.CString(name) cname := C.CString(name)
defer C.free(unsafe.Pointer(cname)) defer C.free(unsafe.Pointer(cname))
return C.getProcAddressGL(libGL, cname) return uintptr(C.getProcAddressGL(libGL, cname))
} }
func getProcAddressGLES(name string) unsafe.Pointer { func getProcAddressGLES(name string) uintptr {
name = strings.TrimSuffix(name, "EXT") name = strings.TrimSuffix(name, "EXT")
cname := C.CString(name) cname := C.CString(name)
defer C.free(unsafe.Pointer(cname)) defer C.free(unsafe.Pointer(cname))
return C.getProcAddressGLES(libGLES, cname) return uintptr(C.getProcAddressGLES(libGLES, cname))
} }

View File

@ -30,24 +30,23 @@ func (c *defaultContext) init() error {
return nil return nil
} }
func (c *defaultContext) getProcAddress(namea string) uintptr { func (c *defaultContext) getProcAddress(namea string) (uintptr, error) {
cname, err := windows.BytePtrFromString(namea) cname, err := windows.BytePtrFromString(namea)
if err != nil { if err != nil {
panic(err) return 0, err
} }
r, _, err := procWglGetProcAddress.Call(uintptr(unsafe.Pointer(cname))) r, _, err := procWglGetProcAddress.Call(uintptr(unsafe.Pointer(cname)))
if r != 0 { if r != 0 {
return r return r, nil
} }
if err != nil && err != windows.ERROR_SUCCESS && err != windows.ERROR_PROC_NOT_FOUND { if err != nil && err != windows.ERROR_SUCCESS && err != windows.ERROR_PROC_NOT_FOUND {
panic(fmt.Sprintf("gl: wglGetProcAddress failed for %s: %s", namea, err.Error())) return 0, fmt.Errorf("gl: wglGetProcAddress failed for %s: %w", namea, err)
} }
p := opengl32.NewProc(namea) p := opengl32.NewProc(namea)
if err := p.Find(); err != nil { if err := p.Find(); err != nil {
// The proc is not found. return 0, err
return 0
} }
return p.Addr() return p.Addr(), nil
} }