gl_windows.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. // SPDX-License-Identifier: Unlicense OR MIT
  2. package gl
  3. import (
  4. "fmt"
  5. "math"
  6. "runtime"
  7. "sync"
  8. "syscall"
  9. "unsafe"
  10. "golang.org/x/sys/windows"
  11. )
  12. func loadGLESv2Procs() error {
  13. dllName := "libGLESv2.dll"
  14. handle, err := windows.LoadLibraryEx(dllName, 0, windows.LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)
  15. if err != nil {
  16. return fmt.Errorf("gl: failed to load %s: %v", dllName, err)
  17. }
  18. gles := windows.DLL{Handle: handle, Name: dllName}
  19. // d3dcompiler_47.dll is needed internally for shader compilation to function.
  20. dllName = "d3dcompiler_47.dll"
  21. _, err = windows.LoadLibraryEx(dllName, 0, windows.LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)
  22. if err != nil {
  23. return fmt.Errorf("gl: failed to load %s: %v", dllName, err)
  24. }
  25. procs := map[string]**windows.Proc{
  26. "glActiveTexture": &_glActiveTexture,
  27. "glAttachShader": &_glAttachShader,
  28. "glBeginQuery": &_glBeginQuery,
  29. "glBindAttribLocation": &_glBindAttribLocation,
  30. "glBindBuffer": &_glBindBuffer,
  31. "glBindBufferBase": &_glBindBufferBase,
  32. "glBindFramebuffer": &_glBindFramebuffer,
  33. "glBindRenderbuffer": &_glBindRenderbuffer,
  34. "glBindTexture": &_glBindTexture,
  35. "glBindVertexArray": &_glBindVertexArray,
  36. "glBlendEquation": &_glBlendEquation,
  37. "glBlendFuncSeparate": &_glBlendFuncSeparate,
  38. "glBufferData": &_glBufferData,
  39. "glBufferSubData": &_glBufferSubData,
  40. "glCheckFramebufferStatus": &_glCheckFramebufferStatus,
  41. "glClear": &_glClear,
  42. "glClearColor": &_glClearColor,
  43. "glClearDepthf": &_glClearDepthf,
  44. "glDeleteQueries": &_glDeleteQueries,
  45. "glDeleteVertexArrays": &_glDeleteVertexArrays,
  46. "glCompileShader": &_glCompileShader,
  47. "glCopyTexSubImage2D": &_glCopyTexSubImage2D,
  48. "glGenerateMipmap": &_glGenerateMipmap,
  49. "glGenBuffers": &_glGenBuffers,
  50. "glGenFramebuffers": &_glGenFramebuffers,
  51. "glGenVertexArrays": &_glGenVertexArrays,
  52. "glGetUniformBlockIndex": &_glGetUniformBlockIndex,
  53. "glCreateProgram": &_glCreateProgram,
  54. "glGenRenderbuffers": &_glGenRenderbuffers,
  55. "glCreateShader": &_glCreateShader,
  56. "glGenTextures": &_glGenTextures,
  57. "glDeleteBuffers": &_glDeleteBuffers,
  58. "glDeleteFramebuffers": &_glDeleteFramebuffers,
  59. "glDeleteProgram": &_glDeleteProgram,
  60. "glDeleteShader": &_glDeleteShader,
  61. "glDeleteRenderbuffers": &_glDeleteRenderbuffers,
  62. "glDeleteTextures": &_glDeleteTextures,
  63. "glDepthFunc": &_glDepthFunc,
  64. "glDepthMask": &_glDepthMask,
  65. "glDisableVertexAttribArray": &_glDisableVertexAttribArray,
  66. "glDisable": &_glDisable,
  67. "glDrawArrays": &_glDrawArrays,
  68. "glDrawElements": &_glDrawElements,
  69. "glEnable": &_glEnable,
  70. "glEnableVertexAttribArray": &_glEnableVertexAttribArray,
  71. "glEndQuery": &_glEndQuery,
  72. "glFinish": &_glFinish,
  73. "glFlush": &_glFlush,
  74. "glFramebufferRenderbuffer": &_glFramebufferRenderbuffer,
  75. "glFramebufferTexture2D": &_glFramebufferTexture2D,
  76. "glGenQueries": &_glGenQueries,
  77. "glGetError": &_glGetError,
  78. "glGetRenderbufferParameteriv": &_glGetRenderbufferParameteriv,
  79. "glGetFloatv": &_glGetFloatv,
  80. "glGetFramebufferAttachmentParameteriv": &_glGetFramebufferAttachmentParameteriv,
  81. "glGetIntegerv": &_glGetIntegerv,
  82. "glGetIntegeri_v": &_glGetIntegeri_v,
  83. "glGetProgramiv": &_glGetProgramiv,
  84. "glGetProgramInfoLog": &_glGetProgramInfoLog,
  85. "glGetQueryObjectuiv": &_glGetQueryObjectuiv,
  86. "glGetShaderiv": &_glGetShaderiv,
  87. "glGetShaderInfoLog": &_glGetShaderInfoLog,
  88. "glGetString": &_glGetString,
  89. "glGetUniformLocation": &_glGetUniformLocation,
  90. "glGetVertexAttribiv": &_glGetVertexAttribiv,
  91. "glGetVertexAttribPointerv": &_glGetVertexAttribPointerv,
  92. "glInvalidateFramebuffer": &_glInvalidateFramebuffer,
  93. "glIsEnabled": &_glIsEnabled,
  94. "glLinkProgram": &_glLinkProgram,
  95. "glPixelStorei": &_glPixelStorei,
  96. "glReadPixels": &_glReadPixels,
  97. "glRenderbufferStorage": &_glRenderbufferStorage,
  98. "glScissor": &_glScissor,
  99. "glShaderSource": &_glShaderSource,
  100. "glTexImage2D": &_glTexImage2D,
  101. "glTexStorage2D": &_glTexStorage2D,
  102. "glTexSubImage2D": &_glTexSubImage2D,
  103. "glTexParameteri": &_glTexParameteri,
  104. "glUniformBlockBinding": &_glUniformBlockBinding,
  105. "glUniform1f": &_glUniform1f,
  106. "glUniform1i": &_glUniform1i,
  107. "glUniform2f": &_glUniform2f,
  108. "glUniform3f": &_glUniform3f,
  109. "glUniform4f": &_glUniform4f,
  110. "glUseProgram": &_glUseProgram,
  111. "glVertexAttribPointer": &_glVertexAttribPointer,
  112. "glViewport": &_glViewport,
  113. }
  114. for name, proc := range procs {
  115. p, err := gles.FindProc(name)
  116. if err != nil {
  117. return fmt.Errorf("failed to locate %s in %s: %w", name, gles.Name, err)
  118. }
  119. *proc = p
  120. }
  121. return nil
  122. }
  123. var (
  124. glInitOnce sync.Once
  125. _glActiveTexture *windows.Proc
  126. _glAttachShader *windows.Proc
  127. _glBeginQuery *windows.Proc
  128. _glBindAttribLocation *windows.Proc
  129. _glBindBuffer *windows.Proc
  130. _glBindBufferBase *windows.Proc
  131. _glBindFramebuffer *windows.Proc
  132. _glBindRenderbuffer *windows.Proc
  133. _glBindTexture *windows.Proc
  134. _glBindVertexArray *windows.Proc
  135. _glBlendEquation *windows.Proc
  136. _glBlendFuncSeparate *windows.Proc
  137. _glBufferData *windows.Proc
  138. _glBufferSubData *windows.Proc
  139. _glCheckFramebufferStatus *windows.Proc
  140. _glClear *windows.Proc
  141. _glClearColor *windows.Proc
  142. _glClearDepthf *windows.Proc
  143. _glDeleteQueries *windows.Proc
  144. _glDeleteVertexArrays *windows.Proc
  145. _glCompileShader *windows.Proc
  146. _glCopyTexSubImage2D *windows.Proc
  147. _glGenerateMipmap *windows.Proc
  148. _glGenBuffers *windows.Proc
  149. _glGenFramebuffers *windows.Proc
  150. _glGenVertexArrays *windows.Proc
  151. _glGetUniformBlockIndex *windows.Proc
  152. _glCreateProgram *windows.Proc
  153. _glGenRenderbuffers *windows.Proc
  154. _glCreateShader *windows.Proc
  155. _glGenTextures *windows.Proc
  156. _glDeleteBuffers *windows.Proc
  157. _glDeleteFramebuffers *windows.Proc
  158. _glDeleteProgram *windows.Proc
  159. _glDeleteShader *windows.Proc
  160. _glDeleteRenderbuffers *windows.Proc
  161. _glDeleteTextures *windows.Proc
  162. _glDepthFunc *windows.Proc
  163. _glDepthMask *windows.Proc
  164. _glDisableVertexAttribArray *windows.Proc
  165. _glDisable *windows.Proc
  166. _glDrawArrays *windows.Proc
  167. _glDrawElements *windows.Proc
  168. _glEnable *windows.Proc
  169. _glEnableVertexAttribArray *windows.Proc
  170. _glEndQuery *windows.Proc
  171. _glFinish *windows.Proc
  172. _glFlush *windows.Proc
  173. _glFramebufferRenderbuffer *windows.Proc
  174. _glFramebufferTexture2D *windows.Proc
  175. _glGenQueries *windows.Proc
  176. _glGetError *windows.Proc
  177. _glGetRenderbufferParameteriv *windows.Proc
  178. _glGetFloatv *windows.Proc
  179. _glGetFramebufferAttachmentParameteriv *windows.Proc
  180. _glGetIntegerv *windows.Proc
  181. _glGetIntegeri_v *windows.Proc
  182. _glGetProgramiv *windows.Proc
  183. _glGetProgramInfoLog *windows.Proc
  184. _glGetQueryObjectuiv *windows.Proc
  185. _glGetShaderiv *windows.Proc
  186. _glGetShaderInfoLog *windows.Proc
  187. _glGetString *windows.Proc
  188. _glGetUniformLocation *windows.Proc
  189. _glGetVertexAttribiv *windows.Proc
  190. _glGetVertexAttribPointerv *windows.Proc
  191. _glInvalidateFramebuffer *windows.Proc
  192. _glIsEnabled *windows.Proc
  193. _glLinkProgram *windows.Proc
  194. _glPixelStorei *windows.Proc
  195. _glReadPixels *windows.Proc
  196. _glRenderbufferStorage *windows.Proc
  197. _glScissor *windows.Proc
  198. _glShaderSource *windows.Proc
  199. _glTexImage2D *windows.Proc
  200. _glTexStorage2D *windows.Proc
  201. _glTexSubImage2D *windows.Proc
  202. _glTexParameteri *windows.Proc
  203. _glUniformBlockBinding *windows.Proc
  204. _glUniform1f *windows.Proc
  205. _glUniform1i *windows.Proc
  206. _glUniform2f *windows.Proc
  207. _glUniform3f *windows.Proc
  208. _glUniform4f *windows.Proc
  209. _glUseProgram *windows.Proc
  210. _glVertexAttribPointer *windows.Proc
  211. _glViewport *windows.Proc
  212. )
  213. type Functions struct {
  214. // Query caches.
  215. int32s [100]int32
  216. float32s [100]float32
  217. uintptrs [100]uintptr
  218. }
  219. type Context interface{}
  220. func NewFunctions(ctx Context, forceES bool) (*Functions, error) {
  221. if ctx != nil {
  222. panic("non-nil context")
  223. }
  224. var err error
  225. glInitOnce.Do(func() {
  226. err = loadGLESv2Procs()
  227. })
  228. return new(Functions), err
  229. }
  230. func (c *Functions) ActiveTexture(t Enum) {
  231. syscall.Syscall(_glActiveTexture.Addr(), 1, uintptr(t), 0, 0)
  232. }
  233. func (c *Functions) AttachShader(p Program, s Shader) {
  234. syscall.Syscall(_glAttachShader.Addr(), 2, uintptr(p.V), uintptr(s.V), 0)
  235. }
  236. func (f *Functions) BeginQuery(target Enum, query Query) {
  237. syscall.Syscall(_glBeginQuery.Addr(), 2, uintptr(target), uintptr(query.V), 0)
  238. }
  239. func (c *Functions) BindAttribLocation(p Program, a Attrib, name string) {
  240. cname := cString(name)
  241. c0 := &cname[0]
  242. syscall.Syscall(_glBindAttribLocation.Addr(), 3, uintptr(p.V), uintptr(a), uintptr(unsafe.Pointer(c0)))
  243. issue34474KeepAlive(c)
  244. }
  245. func (c *Functions) BindBuffer(target Enum, b Buffer) {
  246. syscall.Syscall(_glBindBuffer.Addr(), 2, uintptr(target), uintptr(b.V), 0)
  247. }
  248. func (c *Functions) BindBufferBase(target Enum, index int, b Buffer) {
  249. syscall.Syscall(_glBindBufferBase.Addr(), 3, uintptr(target), uintptr(index), uintptr(b.V))
  250. }
  251. func (c *Functions) BindFramebuffer(target Enum, fb Framebuffer) {
  252. syscall.Syscall(_glBindFramebuffer.Addr(), 2, uintptr(target), uintptr(fb.V), 0)
  253. }
  254. func (c *Functions) BindRenderbuffer(target Enum, rb Renderbuffer) {
  255. syscall.Syscall(_glBindRenderbuffer.Addr(), 2, uintptr(target), uintptr(rb.V), 0)
  256. }
  257. func (f *Functions) BindImageTexture(unit int, t Texture, level int, layered bool, layer int, access, format Enum) {
  258. panic("not implemented")
  259. }
  260. func (c *Functions) BindTexture(target Enum, t Texture) {
  261. syscall.Syscall(_glBindTexture.Addr(), 2, uintptr(target), uintptr(t.V), 0)
  262. }
  263. func (c *Functions) BindVertexArray(a VertexArray) {
  264. syscall.Syscall(_glBindVertexArray.Addr(), 1, uintptr(a.V), 0, 0)
  265. }
  266. func (c *Functions) BlendEquation(mode Enum) {
  267. syscall.Syscall(_glBlendEquation.Addr(), 1, uintptr(mode), 0, 0)
  268. }
  269. func (c *Functions) BlendFuncSeparate(srcRGB, dstRGB, srcA, dstA Enum) {
  270. syscall.Syscall6(_glBlendFuncSeparate.Addr(), 4, uintptr(srcRGB), uintptr(dstRGB), uintptr(srcA), uintptr(dstA), 0, 0)
  271. }
  272. func (c *Functions) BufferData(target Enum, size int, usage Enum, data []byte) {
  273. var p unsafe.Pointer
  274. if len(data) > 0 {
  275. p = unsafe.Pointer(&data[0])
  276. }
  277. syscall.Syscall6(_glBufferData.Addr(), 4, uintptr(target), uintptr(size), uintptr(p), uintptr(usage), 0, 0)
  278. }
  279. func (f *Functions) BufferSubData(target Enum, offset int, src []byte) {
  280. if n := len(src); n > 0 {
  281. s0 := &src[0]
  282. syscall.Syscall6(_glBufferSubData.Addr(), 4, uintptr(target), uintptr(offset), uintptr(n), uintptr(unsafe.Pointer(s0)), 0, 0)
  283. issue34474KeepAlive(s0)
  284. }
  285. }
  286. func (c *Functions) CheckFramebufferStatus(target Enum) Enum {
  287. s, _, _ := syscall.Syscall(_glCheckFramebufferStatus.Addr(), 1, uintptr(target), 0, 0)
  288. return Enum(s)
  289. }
  290. func (c *Functions) Clear(mask Enum) {
  291. syscall.Syscall(_glClear.Addr(), 1, uintptr(mask), 0, 0)
  292. }
  293. func (c *Functions) ClearColor(red, green, blue, alpha float32) {
  294. syscall.Syscall6(_glClearColor.Addr(), 4, uintptr(math.Float32bits(red)), uintptr(math.Float32bits(green)), uintptr(math.Float32bits(blue)), uintptr(math.Float32bits(alpha)), 0, 0)
  295. }
  296. func (c *Functions) ClearDepthf(d float32) {
  297. syscall.Syscall(_glClearDepthf.Addr(), 1, uintptr(math.Float32bits(d)), 0, 0)
  298. }
  299. func (c *Functions) CompileShader(s Shader) {
  300. syscall.Syscall(_glCompileShader.Addr(), 1, uintptr(s.V), 0, 0)
  301. }
  302. func (f *Functions) CopyTexSubImage2D(target Enum, level, xoffset, yoffset, x, y, width, height int) {
  303. syscall.Syscall9(_glCopyTexSubImage2D.Addr(), 8, uintptr(target), uintptr(level), uintptr(xoffset), uintptr(yoffset), uintptr(x), uintptr(y), uintptr(width), uintptr(height), 0)
  304. }
  305. func (f *Functions) GenerateMipmap(target Enum) {
  306. syscall.Syscall(_glGenerateMipmap.Addr(), 1, uintptr(target), 0, 0)
  307. }
  308. func (c *Functions) CreateBuffer() Buffer {
  309. var buf uintptr
  310. syscall.Syscall(_glGenBuffers.Addr(), 2, 1, uintptr(unsafe.Pointer(&buf)), 0)
  311. return Buffer{uint(buf)}
  312. }
  313. func (c *Functions) CreateFramebuffer() Framebuffer {
  314. var fb uintptr
  315. syscall.Syscall(_glGenFramebuffers.Addr(), 2, 1, uintptr(unsafe.Pointer(&fb)), 0)
  316. return Framebuffer{uint(fb)}
  317. }
  318. func (c *Functions) CreateProgram() Program {
  319. p, _, _ := syscall.Syscall(_glCreateProgram.Addr(), 0, 0, 0, 0)
  320. return Program{uint(p)}
  321. }
  322. func (f *Functions) CreateQuery() Query {
  323. var q uintptr
  324. syscall.Syscall(_glGenQueries.Addr(), 2, 1, uintptr(unsafe.Pointer(&q)), 0)
  325. return Query{uint(q)}
  326. }
  327. func (c *Functions) CreateRenderbuffer() Renderbuffer {
  328. var rb uintptr
  329. syscall.Syscall(_glGenRenderbuffers.Addr(), 2, 1, uintptr(unsafe.Pointer(&rb)), 0)
  330. return Renderbuffer{uint(rb)}
  331. }
  332. func (c *Functions) CreateShader(ty Enum) Shader {
  333. s, _, _ := syscall.Syscall(_glCreateShader.Addr(), 1, uintptr(ty), 0, 0)
  334. return Shader{uint(s)}
  335. }
  336. func (c *Functions) CreateTexture() Texture {
  337. var t uintptr
  338. syscall.Syscall(_glGenTextures.Addr(), 2, 1, uintptr(unsafe.Pointer(&t)), 0)
  339. return Texture{uint(t)}
  340. }
  341. func (c *Functions) CreateVertexArray() VertexArray {
  342. var t uintptr
  343. syscall.Syscall(_glGenVertexArrays.Addr(), 2, 1, uintptr(unsafe.Pointer(&t)), 0)
  344. return VertexArray{uint(t)}
  345. }
  346. func (c *Functions) DeleteBuffer(v Buffer) {
  347. syscall.Syscall(_glDeleteBuffers.Addr(), 2, 1, uintptr(unsafe.Pointer(&v)), 0)
  348. }
  349. func (c *Functions) DeleteFramebuffer(v Framebuffer) {
  350. syscall.Syscall(_glDeleteFramebuffers.Addr(), 2, 1, uintptr(unsafe.Pointer(&v.V)), 0)
  351. }
  352. func (c *Functions) DeleteProgram(p Program) {
  353. syscall.Syscall(_glDeleteProgram.Addr(), 1, uintptr(p.V), 0, 0)
  354. }
  355. func (f *Functions) DeleteQuery(query Query) {
  356. syscall.Syscall(_glDeleteQueries.Addr(), 2, 1, uintptr(unsafe.Pointer(&query.V)), 0)
  357. }
  358. func (c *Functions) DeleteShader(s Shader) {
  359. syscall.Syscall(_glDeleteShader.Addr(), 1, uintptr(s.V), 0, 0)
  360. }
  361. func (c *Functions) DeleteRenderbuffer(v Renderbuffer) {
  362. syscall.Syscall(_glDeleteRenderbuffers.Addr(), 2, 1, uintptr(unsafe.Pointer(&v.V)), 0)
  363. }
  364. func (c *Functions) DeleteTexture(v Texture) {
  365. syscall.Syscall(_glDeleteTextures.Addr(), 2, 1, uintptr(unsafe.Pointer(&v.V)), 0)
  366. }
  367. func (f *Functions) DeleteVertexArray(array VertexArray) {
  368. syscall.Syscall(_glDeleteVertexArrays.Addr(), 2, 1, uintptr(unsafe.Pointer(&array.V)), 0)
  369. }
  370. func (c *Functions) DepthFunc(f Enum) {
  371. syscall.Syscall(_glDepthFunc.Addr(), 1, uintptr(f), 0, 0)
  372. }
  373. func (c *Functions) DepthMask(mask bool) {
  374. var m uintptr
  375. if mask {
  376. m = 1
  377. }
  378. syscall.Syscall(_glDepthMask.Addr(), 1, m, 0, 0)
  379. }
  380. func (c *Functions) DisableVertexAttribArray(a Attrib) {
  381. syscall.Syscall(_glDisableVertexAttribArray.Addr(), 1, uintptr(a), 0, 0)
  382. }
  383. func (c *Functions) Disable(cap Enum) {
  384. syscall.Syscall(_glDisable.Addr(), 1, uintptr(cap), 0, 0)
  385. }
  386. func (c *Functions) DrawArrays(mode Enum, first, count int) {
  387. syscall.Syscall(_glDrawArrays.Addr(), 3, uintptr(mode), uintptr(first), uintptr(count))
  388. }
  389. func (c *Functions) DrawElements(mode Enum, count int, ty Enum, offset int) {
  390. syscall.Syscall6(_glDrawElements.Addr(), 4, uintptr(mode), uintptr(count), uintptr(ty), uintptr(offset), 0, 0)
  391. }
  392. func (f *Functions) DispatchCompute(x, y, z int) {
  393. panic("not implemented")
  394. }
  395. func (c *Functions) Enable(cap Enum) {
  396. syscall.Syscall(_glEnable.Addr(), 1, uintptr(cap), 0, 0)
  397. }
  398. func (c *Functions) EnableVertexAttribArray(a Attrib) {
  399. syscall.Syscall(_glEnableVertexAttribArray.Addr(), 1, uintptr(a), 0, 0)
  400. }
  401. func (f *Functions) EndQuery(target Enum) {
  402. syscall.Syscall(_glEndQuery.Addr(), 1, uintptr(target), 0, 0)
  403. }
  404. func (c *Functions) Finish() {
  405. syscall.Syscall(_glFinish.Addr(), 0, 0, 0, 0)
  406. }
  407. func (c *Functions) Flush() {
  408. syscall.Syscall(_glFlush.Addr(), 0, 0, 0, 0)
  409. }
  410. func (c *Functions) FramebufferRenderbuffer(target, attachment, renderbuffertarget Enum, renderbuffer Renderbuffer) {
  411. syscall.Syscall6(_glFramebufferRenderbuffer.Addr(), 4, uintptr(target), uintptr(attachment), uintptr(renderbuffertarget), uintptr(renderbuffer.V), 0, 0)
  412. }
  413. func (c *Functions) FramebufferTexture2D(target, attachment, texTarget Enum, t Texture, level int) {
  414. syscall.Syscall6(_glFramebufferTexture2D.Addr(), 5, uintptr(target), uintptr(attachment), uintptr(texTarget), uintptr(t.V), uintptr(level), 0)
  415. }
  416. func (f *Functions) GetUniformBlockIndex(p Program, name string) uint {
  417. cname := cString(name)
  418. c0 := &cname[0]
  419. u, _, _ := syscall.Syscall(_glGetUniformBlockIndex.Addr(), 2, uintptr(p.V), uintptr(unsafe.Pointer(c0)), 0)
  420. issue34474KeepAlive(c0)
  421. return uint(u)
  422. }
  423. func (c *Functions) GetBinding(pname Enum) Object {
  424. return Object{uint(c.GetInteger(pname))}
  425. }
  426. func (c *Functions) GetBindingi(pname Enum, idx int) Object {
  427. return Object{uint(c.GetIntegeri(pname, idx))}
  428. }
  429. func (c *Functions) GetError() Enum {
  430. e, _, _ := syscall.Syscall(_glGetError.Addr(), 0, 0, 0, 0)
  431. return Enum(e)
  432. }
  433. func (c *Functions) GetRenderbufferParameteri(target, pname Enum) int {
  434. syscall.Syscall(_glGetRenderbufferParameteriv.Addr(), 3, uintptr(target), uintptr(pname), uintptr(unsafe.Pointer(&c.int32s[0])))
  435. return int(c.int32s[0])
  436. }
  437. func (c *Functions) GetFramebufferAttachmentParameteri(target, attachment, pname Enum) int {
  438. syscall.Syscall6(_glGetFramebufferAttachmentParameteriv.Addr(), 4, uintptr(target), uintptr(attachment), uintptr(pname), uintptr(unsafe.Pointer(&c.int32s[0])), 0, 0)
  439. return int(c.int32s[0])
  440. }
  441. func (c *Functions) GetInteger4(pname Enum) [4]int {
  442. syscall.Syscall(_glGetIntegerv.Addr(), 2, uintptr(pname), uintptr(unsafe.Pointer(&c.int32s[0])), 0)
  443. var r [4]int
  444. for i := range r {
  445. r[i] = int(c.int32s[i])
  446. }
  447. return r
  448. }
  449. func (c *Functions) GetInteger(pname Enum) int {
  450. syscall.Syscall(_glGetIntegerv.Addr(), 2, uintptr(pname), uintptr(unsafe.Pointer(&c.int32s[0])), 0)
  451. return int(c.int32s[0])
  452. }
  453. func (c *Functions) GetIntegeri(pname Enum, idx int) int {
  454. syscall.Syscall(_glGetIntegeri_v.Addr(), 3, uintptr(pname), uintptr(idx), uintptr(unsafe.Pointer(&c.int32s[0])))
  455. return int(c.int32s[0])
  456. }
  457. func (c *Functions) GetFloat(pname Enum) float32 {
  458. syscall.Syscall(_glGetFloatv.Addr(), 2, uintptr(pname), uintptr(unsafe.Pointer(&c.float32s[0])), 0)
  459. return c.float32s[0]
  460. }
  461. func (c *Functions) GetFloat4(pname Enum) [4]float32 {
  462. syscall.Syscall(_glGetFloatv.Addr(), 2, uintptr(pname), uintptr(unsafe.Pointer(&c.float32s[0])), 0)
  463. var r [4]float32
  464. copy(r[:], c.float32s[:])
  465. return r
  466. }
  467. func (c *Functions) GetProgrami(p Program, pname Enum) int {
  468. syscall.Syscall(_glGetProgramiv.Addr(), 3, uintptr(p.V), uintptr(pname), uintptr(unsafe.Pointer(&c.int32s[0])))
  469. return int(c.int32s[0])
  470. }
  471. func (c *Functions) GetProgramInfoLog(p Program) string {
  472. n := c.GetProgrami(p, INFO_LOG_LENGTH)
  473. if n == 0 {
  474. return ""
  475. }
  476. buf := make([]byte, n)
  477. syscall.Syscall6(_glGetProgramInfoLog.Addr(), 4, uintptr(p.V), uintptr(len(buf)), 0, uintptr(unsafe.Pointer(&buf[0])), 0, 0)
  478. return string(buf)
  479. }
  480. func (c *Functions) GetQueryObjectuiv(query Query, pname Enum) uint {
  481. syscall.Syscall(_glGetQueryObjectuiv.Addr(), 3, uintptr(query.V), uintptr(pname), uintptr(unsafe.Pointer(&c.int32s[0])))
  482. return uint(c.int32s[0])
  483. }
  484. func (c *Functions) GetShaderi(s Shader, pname Enum) int {
  485. syscall.Syscall(_glGetShaderiv.Addr(), 3, uintptr(s.V), uintptr(pname), uintptr(unsafe.Pointer(&c.int32s[0])))
  486. return int(c.int32s[0])
  487. }
  488. func (c *Functions) GetShaderInfoLog(s Shader) string {
  489. n := c.GetShaderi(s, INFO_LOG_LENGTH)
  490. buf := make([]byte, n)
  491. syscall.Syscall6(_glGetShaderInfoLog.Addr(), 4, uintptr(s.V), uintptr(len(buf)), 0, uintptr(unsafe.Pointer(&buf[0])), 0, 0)
  492. return string(buf)
  493. }
  494. func (c *Functions) GetString(pname Enum) string {
  495. s, _, _ := syscall.Syscall(_glGetString.Addr(), 1, uintptr(pname), 0, 0)
  496. return windows.BytePtrToString((*byte)(unsafe.Pointer(s)))
  497. }
  498. func (c *Functions) GetUniformLocation(p Program, name string) Uniform {
  499. cname := cString(name)
  500. c0 := &cname[0]
  501. u, _, _ := syscall.Syscall(_glGetUniformLocation.Addr(), 2, uintptr(p.V), uintptr(unsafe.Pointer(c0)), 0)
  502. issue34474KeepAlive(c0)
  503. return Uniform{int(u)}
  504. }
  505. func (c *Functions) GetVertexAttrib(index int, pname Enum) int {
  506. syscall.Syscall(_glGetVertexAttribiv.Addr(), 3, uintptr(index), uintptr(pname), uintptr(unsafe.Pointer(&c.int32s[0])))
  507. return int(c.int32s[0])
  508. }
  509. func (c *Functions) GetVertexAttribBinding(index int, pname Enum) Object {
  510. return Object{uint(c.GetVertexAttrib(index, pname))}
  511. }
  512. func (c *Functions) GetVertexAttribPointer(index int, pname Enum) uintptr {
  513. syscall.Syscall(_glGetVertexAttribPointerv.Addr(), 3, uintptr(index), uintptr(pname), uintptr(unsafe.Pointer(&c.uintptrs[0])))
  514. return c.uintptrs[0]
  515. }
  516. func (c *Functions) InvalidateFramebuffer(target, attachment Enum) {
  517. addr := _glInvalidateFramebuffer.Addr()
  518. if addr == 0 {
  519. // InvalidateFramebuffer is just a hint. Skip it if not supported.
  520. return
  521. }
  522. syscall.Syscall(addr, 3, uintptr(target), 1, uintptr(unsafe.Pointer(&attachment)))
  523. }
  524. func (f *Functions) IsEnabled(cap Enum) bool {
  525. u, _, _ := syscall.Syscall(_glIsEnabled.Addr(), 1, uintptr(cap), 0, 0)
  526. return u == TRUE
  527. }
  528. func (c *Functions) LinkProgram(p Program) {
  529. syscall.Syscall(_glLinkProgram.Addr(), 1, uintptr(p.V), 0, 0)
  530. }
  531. func (c *Functions) PixelStorei(pname Enum, param int) {
  532. syscall.Syscall(_glPixelStorei.Addr(), 2, uintptr(pname), uintptr(param), 0)
  533. }
  534. func (f *Functions) MemoryBarrier(barriers Enum) {
  535. panic("not implemented")
  536. }
  537. func (f *Functions) MapBufferRange(target Enum, offset, length int, access Enum) []byte {
  538. panic("not implemented")
  539. }
  540. func (f *Functions) ReadPixels(x, y, width, height int, format, ty Enum, data []byte) {
  541. d0 := &data[0]
  542. syscall.Syscall9(_glReadPixels.Addr(), 7, uintptr(x), uintptr(y), uintptr(width), uintptr(height), uintptr(format), uintptr(ty), uintptr(unsafe.Pointer(d0)), 0, 0)
  543. issue34474KeepAlive(d0)
  544. }
  545. func (c *Functions) RenderbufferStorage(target, internalformat Enum, width, height int) {
  546. syscall.Syscall6(_glRenderbufferStorage.Addr(), 4, uintptr(target), uintptr(internalformat), uintptr(width), uintptr(height), 0, 0)
  547. }
  548. func (c *Functions) Scissor(x, y, width, height int32) {
  549. syscall.Syscall6(_glScissor.Addr(), 4, uintptr(x), uintptr(y), uintptr(width), uintptr(height), 0, 0)
  550. }
  551. func (c *Functions) ShaderSource(s Shader, src string) {
  552. var n uintptr = uintptr(len(src))
  553. psrc := &src
  554. syscall.Syscall6(_glShaderSource.Addr(), 4, uintptr(s.V), 1, uintptr(unsafe.Pointer(psrc)), uintptr(unsafe.Pointer(&n)), 0, 0)
  555. issue34474KeepAlive(psrc)
  556. }
  557. func (f *Functions) TexImage2D(target Enum, level int, internalFormat Enum, width int, height int, format Enum, ty Enum) {
  558. syscall.Syscall9(_glTexImage2D.Addr(), 9, uintptr(target), uintptr(level), uintptr(internalFormat), uintptr(width), uintptr(height), 0, uintptr(format), uintptr(ty), 0)
  559. }
  560. func (f *Functions) TexStorage2D(target Enum, levels int, internalFormat Enum, width, height int) {
  561. syscall.Syscall6(_glTexStorage2D.Addr(), 5, uintptr(target), uintptr(levels), uintptr(internalFormat), uintptr(width), uintptr(height), 0)
  562. }
  563. func (c *Functions) TexSubImage2D(target Enum, level int, x, y, width, height int, format, ty Enum, data []byte) {
  564. d0 := &data[0]
  565. syscall.Syscall9(_glTexSubImage2D.Addr(), 9, uintptr(target), uintptr(level), uintptr(x), uintptr(y), uintptr(width), uintptr(height), uintptr(format), uintptr(ty), uintptr(unsafe.Pointer(d0)))
  566. issue34474KeepAlive(d0)
  567. }
  568. func (c *Functions) TexParameteri(target, pname Enum, param int) {
  569. syscall.Syscall(_glTexParameteri.Addr(), 3, uintptr(target), uintptr(pname), uintptr(param))
  570. }
  571. func (f *Functions) UniformBlockBinding(p Program, uniformBlockIndex uint, uniformBlockBinding uint) {
  572. syscall.Syscall(_glUniformBlockBinding.Addr(), 3, uintptr(p.V), uintptr(uniformBlockIndex), uintptr(uniformBlockBinding))
  573. }
  574. func (c *Functions) Uniform1f(dst Uniform, v float32) {
  575. syscall.Syscall(_glUniform1f.Addr(), 2, uintptr(dst.V), uintptr(math.Float32bits(v)), 0)
  576. }
  577. func (c *Functions) Uniform1i(dst Uniform, v int) {
  578. syscall.Syscall(_glUniform1i.Addr(), 2, uintptr(dst.V), uintptr(v), 0)
  579. }
  580. func (c *Functions) Uniform2f(dst Uniform, v0, v1 float32) {
  581. syscall.Syscall(_glUniform2f.Addr(), 3, uintptr(dst.V), uintptr(math.Float32bits(v0)), uintptr(math.Float32bits(v1)))
  582. }
  583. func (c *Functions) Uniform3f(dst Uniform, v0, v1, v2 float32) {
  584. syscall.Syscall6(_glUniform3f.Addr(), 4, uintptr(dst.V), uintptr(math.Float32bits(v0)), uintptr(math.Float32bits(v1)), uintptr(math.Float32bits(v2)), 0, 0)
  585. }
  586. func (c *Functions) Uniform4f(dst Uniform, v0, v1, v2, v3 float32) {
  587. syscall.Syscall6(_glUniform4f.Addr(), 5, uintptr(dst.V), uintptr(math.Float32bits(v0)), uintptr(math.Float32bits(v1)), uintptr(math.Float32bits(v2)), uintptr(math.Float32bits(v3)), 0)
  588. }
  589. func (c *Functions) UseProgram(p Program) {
  590. syscall.Syscall(_glUseProgram.Addr(), 1, uintptr(p.V), 0, 0)
  591. }
  592. func (f *Functions) UnmapBuffer(target Enum) bool {
  593. panic("not implemented")
  594. }
  595. func (c *Functions) VertexAttribPointer(dst Attrib, size int, ty Enum, normalized bool, stride, offset int) {
  596. var norm uintptr
  597. if normalized {
  598. norm = 1
  599. }
  600. syscall.Syscall6(_glVertexAttribPointer.Addr(), 6, uintptr(dst), uintptr(size), uintptr(ty), norm, uintptr(stride), uintptr(offset))
  601. }
  602. func (c *Functions) Viewport(x, y, width, height int) {
  603. syscall.Syscall6(_glViewport.Addr(), 4, uintptr(x), uintptr(y), uintptr(width), uintptr(height), 0, 0)
  604. }
  605. func cString(s string) []byte {
  606. b := make([]byte, len(s)+1)
  607. copy(b, s)
  608. return b
  609. }
  610. // issue34474KeepAlive calls runtime.KeepAlive as a
  611. // workaround for golang.org/issue/34474.
  612. func issue34474KeepAlive(v interface{}) {
  613. runtime.KeepAlive(v)
  614. }