render_backend_gl.odin 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. #+build windows, darwin, linux
  2. #+private file
  3. package karl2d
  4. @(private="package")
  5. RENDER_BACKEND_INTERFACE_GL :: Render_Backend_Interface {
  6. state_size = gl_state_size,
  7. init = gl_init,
  8. shutdown = gl_shutdown,
  9. clear = gl_clear,
  10. present = gl_present,
  11. draw = gl_draw,
  12. resize_swapchain = gl_resize_swapchain,
  13. get_swapchain_width = gl_get_swapchain_width,
  14. get_swapchain_height = gl_get_swapchain_height,
  15. flip_z = gl_flip_z,
  16. set_internal_state = gl_set_internal_state,
  17. create_texture = gl_create_texture,
  18. load_texture = gl_load_texture,
  19. update_texture = gl_update_texture,
  20. destroy_texture = gl_destroy_texture,
  21. load_shader = gl_load_shader,
  22. destroy_shader = gl_destroy_shader,
  23. default_shader_vertex_source = gl_default_shader_vertex_source,
  24. default_shader_fragment_source = gl_default_shader_fragment_source,
  25. }
  26. import "base:runtime"
  27. import gl "vendor:OpenGL"
  28. import hm "handle_map"
  29. import "core:log"
  30. import "core:strings"
  31. import "core:slice"
  32. import la "core:math/linalg"
  33. _ :: la
  34. GL_State :: struct {
  35. window_handle: Window_Handle,
  36. width: int,
  37. height: int,
  38. allocator: runtime.Allocator,
  39. shaders: hm.Handle_Map(GL_Shader, Shader_Handle, 1024*10),
  40. ctx: GL_Context,
  41. vertex_buffer_gpu: u32,
  42. }
  43. GL_Shader_Constant_Buffer :: struct {
  44. gpu_data: rawptr,
  45. }
  46. GL_Shader :: struct {
  47. handle: Shader_Handle,
  48. // This is like the "input layout"
  49. vao: u32,
  50. program: u32,
  51. }
  52. s: ^GL_State
  53. gl_state_size :: proc() -> int {
  54. return size_of(GL_State)
  55. }
  56. gl_init :: proc(state: rawptr, window_handle: Window_Handle, swapchain_width, swapchain_height: int, allocator := context.allocator) {
  57. s = (^GL_State)(state)
  58. s.window_handle = window_handle
  59. s.width = swapchain_width
  60. s.height = swapchain_height
  61. s.allocator = allocator
  62. ctx, ctx_ok := _gl_get_context(window_handle)
  63. if !ctx_ok {
  64. log.panic("Could not find a valid pixel format for gl context")
  65. }
  66. s.ctx = ctx
  67. _gl_load_procs()
  68. gl.Enable(gl.DEPTH_TEST)
  69. gl.DepthFunc(gl.GREATER)
  70. gl.GenBuffers(1, &s.vertex_buffer_gpu)
  71. gl.BindBuffer(gl.ARRAY_BUFFER, s.vertex_buffer_gpu)
  72. gl.BufferData(gl.ARRAY_BUFFER, VERTEX_BUFFER_MAX, nil, gl.DYNAMIC_DRAW)
  73. }
  74. gl_shutdown :: proc() {
  75. gl.DeleteBuffers(1, &s.vertex_buffer_gpu)
  76. _gl_destroy_context(s.ctx)
  77. }
  78. gl_clear :: proc(color: Color) {
  79. c := f32_color_from_color(color)
  80. gl.ClearColor(c.r, c.g, c.b, c.a)
  81. gl.ClearDepth(-1)
  82. gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
  83. }
  84. gl_present :: proc() {
  85. _gl_present(s.window_handle)
  86. }
  87. gl_draw :: proc(shd: Shader, texture: Texture_Handle, view_proj: Mat4, scissor: Maybe(Rect), vertex_buffer: []u8) {
  88. shader := hm.get(&s.shaders, shd.handle)
  89. if shader == nil {
  90. return
  91. }
  92. gl.EnableVertexAttribArray(0)
  93. gl.EnableVertexAttribArray(1)
  94. gl.EnableVertexAttribArray(2)
  95. gl.UseProgram(shader.program)
  96. mvp_loc := gl.GetUniformLocation(shader.program, "mvp")
  97. //mvp := la.transpose(view_proj)
  98. mvp := view_proj
  99. gl.UniformMatrix4fv(mvp_loc, 1, gl.FALSE, (^f32)(&mvp))
  100. gl.BindBuffer(gl.ARRAY_BUFFER, s.vertex_buffer_gpu)
  101. vb_data := gl.MapBuffer(gl.ARRAY_BUFFER, gl.WRITE_ONLY)
  102. {
  103. gpu_map := slice.from_ptr((^u8)(vb_data), VERTEX_BUFFER_MAX)
  104. copy(
  105. gpu_map,
  106. vertex_buffer,
  107. )
  108. }
  109. gl.UnmapBuffer(gl.ARRAY_BUFFER)
  110. gl.DrawArrays(gl.TRIANGLES, 0, i32(len(vertex_buffer)/shd.vertex_size))
  111. }
  112. gl_resize_swapchain :: proc(w, h: int) {
  113. }
  114. gl_get_swapchain_width :: proc() -> int {
  115. return s.width
  116. }
  117. gl_get_swapchain_height :: proc() -> int {
  118. return s.height
  119. }
  120. gl_flip_z :: proc() -> bool {
  121. return false
  122. }
  123. gl_set_internal_state :: proc(state: rawptr) {
  124. }
  125. gl_create_texture :: proc(width: int, height: int, format: Pixel_Format) -> Texture_Handle {
  126. return {}
  127. }
  128. gl_load_texture :: proc(data: []u8, width: int, height: int, format: Pixel_Format) -> Texture_Handle {
  129. return {}
  130. }
  131. gl_update_texture :: proc(th: Texture_Handle, data: []u8, rect: Rect) -> bool {
  132. return false
  133. }
  134. gl_destroy_texture :: proc(th: Texture_Handle) {
  135. }
  136. Shader_Compile_Result_OK :: struct {}
  137. Shader_Compile_Result_Error :: string
  138. Shader_Compile_Result :: union #no_nil {
  139. Shader_Compile_Result_OK,
  140. Shader_Compile_Result_Error,
  141. }
  142. compile_shader_from_source :: proc(shader_data: string, shader_type: gl.Shader_Type, err_buf: []u8, err_msg: ^string) -> (shader_id: u32, ok: bool) {
  143. shader_id = gl.CreateShader(u32(shader_type))
  144. length := i32(len(shader_data))
  145. shader_cstr := cstring(raw_data(shader_data))
  146. gl.ShaderSource(shader_id, 1, &shader_cstr, &length)
  147. gl.CompileShader(shader_id)
  148. result: i32
  149. gl.GetShaderiv(shader_id, gl.COMPILE_STATUS, &result)
  150. if result != 1 {
  151. info_len: i32
  152. gl.GetShaderInfoLog(shader_id, i32(len(err_buf)), &info_len, raw_data(err_buf))
  153. err_msg^ = string(err_buf[:info_len])
  154. gl.DeleteShader(shader_id)
  155. return 0, false
  156. }
  157. return shader_id, true
  158. }
  159. link_shader :: proc(vs_shader: u32, fs_shader: u32, err_buf: []u8, err_msg: ^string) -> (program_id: u32, ok: bool) {
  160. program_id = gl.CreateProgram()
  161. gl.AttachShader(program_id, vs_shader)
  162. gl.AttachShader(program_id, fs_shader)
  163. gl.LinkProgram(program_id)
  164. result: i32
  165. gl.GetProgramiv(program_id, gl.LINK_STATUS, &result)
  166. if result != 1 {
  167. info_len: i32
  168. gl.GetProgramInfoLog(program_id, i32(len(err_buf)), &info_len, raw_data(err_buf))
  169. err_msg^ = string(err_buf[:info_len])
  170. gl.DeleteProgram(program_id)
  171. return 0, false
  172. }
  173. return program_id, true
  174. }
  175. gl_load_shader :: proc(vs_source: string, fs_source: string, desc_allocator := frame_allocator, layout_formats: []Pixel_Format = {}) -> (handle: Shader_Handle, desc: Shader_Desc) {
  176. @static err: [1024]u8
  177. err_msg: string
  178. vs_shader, vs_shader_ok := compile_shader_from_source(vs_source, gl.Shader_Type.VERTEX_SHADER, err[:], &err_msg)
  179. if !vs_shader_ok {
  180. log.error(err_msg)
  181. return {}, {}
  182. }
  183. fs_shader, fs_shader_ok := compile_shader_from_source(fs_source, gl.Shader_Type.FRAGMENT_SHADER, err[:], &err_msg)
  184. if !fs_shader_ok {
  185. log.error(err_msg)
  186. return {}, {}
  187. }
  188. program, program_ok := link_shader(vs_shader, fs_shader, err[:], &err_msg)
  189. if !program_ok {
  190. log.error(err_msg)
  191. return {}, {}
  192. }
  193. stride: int
  194. {
  195. num_attribs: i32
  196. gl.GetProgramiv(program, gl.ACTIVE_ATTRIBUTES, &num_attribs)
  197. desc.inputs = make([]Shader_Input, num_attribs, desc_allocator)
  198. attrib_name_buf: [256]u8
  199. for i in 0..<num_attribs {
  200. attrib_name_len: i32
  201. attrib_size: i32
  202. attrib_type: u32
  203. gl.GetActiveAttrib(program, u32(i), i32(len(attrib_name_buf)), &attrib_name_len, &attrib_size, &attrib_type, raw_data(attrib_name_buf[:]))
  204. name_cstr := strings.clone_to_cstring(string(attrib_name_buf[:attrib_name_len]), desc_allocator)
  205. loc := gl.GetAttribLocation(program, name_cstr)
  206. if loc >= num_attribs {
  207. continue
  208. }
  209. type: Shader_Input_Type
  210. switch attrib_type {
  211. case gl.FLOAT: type = .F32
  212. case gl.FLOAT_VEC2: type = .Vec2
  213. case gl.FLOAT_VEC3: type = .Vec3
  214. case gl.FLOAT_VEC4: type = .Vec4
  215. /* Possible (gl.) types:
  216. FLOAT, FLOAT_VEC2, FLOAT_VEC3, FLOAT_VEC4, FLOAT_MAT2,
  217. FLOAT_MAT3, FLOAT_MAT4, FLOAT_MAT2x3, FLOAT_MAT2x4,
  218. FLOAT_MAT3x2, FLOAT_MAT3x4, FLOAT_MAT4x2, FLOAT_MAT4x3,
  219. INT, INT_VEC2, INT_VEC3, INT_VEC4, UNSIGNED_INT,
  220. UNSIGNED_INT_VEC2, UNSIGNED_INT_VEC3, UNSIGNED_INT_VEC4,
  221. DOUBLE, DOUBLE_VEC2, DOUBLE_VEC3, DOUBLE_VEC4, DOUBLE_MAT2,
  222. DOUBLE_MAT3, DOUBLE_MAT4, DOUBLE_MAT2x3, DOUBLE_MAT2x4,
  223. DOUBLE_MAT3x2, DOUBLE_MAT3x4, DOUBLE_MAT4x2, or DOUBLE_MAT4x3 */
  224. case: log.errorf("Unknwon type: %v", attrib_type)
  225. }
  226. name := strings.clone(string(attrib_name_buf[:attrib_name_len]), desc_allocator)
  227. format := len(layout_formats) > 0 ? layout_formats[loc] : get_shader_input_format(name, type)
  228. desc.inputs[loc] = {
  229. name = name,
  230. register = int(loc),
  231. format = format,
  232. type = type,
  233. }
  234. input_format := get_shader_input_format(name, type)
  235. format_size := pixel_format_size(input_format)
  236. stride += format_size
  237. //
  238. //log.info(i, attrib_name_len, attrib_size, attrib_type, string(attrib_name_buf[:attrib_name_len]))
  239. }
  240. }
  241. shader := GL_Shader {
  242. program = program,
  243. }
  244. gl.GenVertexArrays(1, &shader.vao)
  245. gl.BindVertexArray(shader.vao)
  246. gl.BindBuffer(gl.ARRAY_BUFFER, s.vertex_buffer_gpu)
  247. offset: int
  248. for idx in 0..<len(desc.inputs) {
  249. input := desc.inputs[idx]
  250. format_size := pixel_format_size(input.format)
  251. gl.EnableVertexAttribArray(u32(idx))
  252. format, num_components, norm := gl_describe_pixel_format(input.format)
  253. gl.VertexAttribPointer(u32(idx), num_components, format, norm ? gl.TRUE : gl.FALSE, i32(stride), uintptr(offset))
  254. offset += format_size
  255. }
  256. {
  257. /*num_constant_buffers: i32
  258. gl.GetProgramiv(program, gl.ACTIVE_UNIFORM_BLOCKS, &num_attribs)
  259. desc.constant_buffers = make([]Shader_Constant_Buffer_Desc, num_constant_buffers, desc_allocator)
  260. shader.constant_buffers = make([]GL_Shader_Constant_Buffer, num_constant_buffers, s.allocator)
  261. for cb_idx in 0..<num_constant_buffers {
  262. buf: u32
  263. gl.GenBuffers(1, &buf)*/
  264. /*
  265. GetUniformIndices :: proc "c" (program: u32, uniformCount: i32, uniformNames: [^]cstring, uniformIndices: [^]u32) { impl_GetUniformIndices(program, uniformCount, uniformNames, uniformIndices) }
  266. GetActiveUniformsiv :: proc "c" (program: u32, uniformCount: i32, uniformIndices: [^]u32, pname: u32, params: [^]i32) { impl_GetActiveUniformsiv(program, uniformCount, uniformIndices, pname, params) }
  267. GetActiveUniformName :: proc "c" (program: u32, uniformIndex: u32, bufSize: i32, length: ^i32, uniformName: [^]u8) { impl_GetActiveUniformName(program, uniformIndex, bufSize, length, uniformName) }
  268. GetUniformBlockIndex :: proc "c" (program: u32, uniformBlockName: cstring) -> u32 { ret := impl_GetUniformBlockIndex(program, uniformBlockName); return ret }
  269. GetActiveUniformBlockiv :: proc "c" (program: u32, uniformBlockIndex: u32, pname: u32, params: [^]i32) { impl_GetActiveUniformBlockiv(program, uniformBlockIndex, pname, params) }
  270. */
  271. }
  272. h := hm.add(&s.shaders, shader)
  273. return h, desc
  274. }
  275. gl_describe_pixel_format :: proc(f: Pixel_Format) -> (format: u32, num_components: i32, normalized: bool) {
  276. switch f {
  277. case .RGBA_32_Float: return gl.FLOAT, 4, false
  278. case .RGB_32_Float: return gl.FLOAT, 3, false
  279. case .RG_32_Float: return gl.FLOAT, 2, false
  280. case .R_32_Float: return gl.FLOAT, 1, false
  281. case .RGBA_8_Norm: return gl.UNSIGNED_BYTE, 4, true
  282. case .RG_8_Norm: return gl.UNSIGNED_BYTE, 2, true
  283. case .R_8_Norm: return gl.UNSIGNED_BYTE, 1, true
  284. case .R_8_UInt: return gl.BYTE, 1, false
  285. case .Unknown:
  286. }
  287. log.error("Unknown format")
  288. return 0, 0, false
  289. }
  290. gl_destroy_shader :: proc(h: Shader_Handle) {
  291. }
  292. gl_default_shader_vertex_source :: proc() -> string {
  293. return #load("default_shader_vertex.glsl")
  294. }
  295. gl_default_shader_fragment_source :: proc() -> string {
  296. return #load("default_shader_fragment.glsl")
  297. }