render_backend_gl.odin 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. buffer: u32,
  45. size: int,
  46. block_index: u32,
  47. }
  48. GL_Shader_Constant_Type :: enum {
  49. Uniform,
  50. Block_Variable,
  51. }
  52. // OpenGL can have constants both in blocks (like constant buffers in D3D11), or as stand-alone
  53. // uniforms. We support both.
  54. GL_Shader_Constant :: struct {
  55. type: GL_Shader_Constant_Type,
  56. // if type is Uniform, then this is the uniform loc
  57. // if type is Block_Variable, then this is the block loc
  58. loc: u32,
  59. // if this is a block variable, then this is the offset to it
  60. block_variable_offset: u32,
  61. }
  62. GL_Shader :: struct {
  63. handle: Shader_Handle,
  64. // This is like the "input layout"
  65. vao: u32,
  66. program: u32,
  67. constant_buffers: []GL_Shader_Constant_Buffer,
  68. constants: []GL_Shader_Constant,
  69. }
  70. s: ^GL_State
  71. gl_state_size :: proc() -> int {
  72. return size_of(GL_State)
  73. }
  74. gl_init :: proc(state: rawptr, window_handle: Window_Handle, swapchain_width, swapchain_height: int, allocator := context.allocator) {
  75. s = (^GL_State)(state)
  76. s.window_handle = window_handle
  77. s.width = swapchain_width
  78. s.height = swapchain_height
  79. s.allocator = allocator
  80. ctx, ctx_ok := _gl_get_context(window_handle)
  81. if !ctx_ok {
  82. log.panic("Could not find a valid pixel format for gl context")
  83. }
  84. s.ctx = ctx
  85. _gl_load_procs()
  86. gl.Enable(gl.DEPTH_TEST)
  87. gl.DepthFunc(gl.GREATER)
  88. gl.GenBuffers(1, &s.vertex_buffer_gpu)
  89. gl.BindBuffer(gl.ARRAY_BUFFER, s.vertex_buffer_gpu)
  90. gl.BufferData(gl.ARRAY_BUFFER, VERTEX_BUFFER_MAX, nil, gl.DYNAMIC_DRAW)
  91. }
  92. gl_shutdown :: proc() {
  93. gl.DeleteBuffers(1, &s.vertex_buffer_gpu)
  94. _gl_destroy_context(s.ctx)
  95. }
  96. gl_clear :: proc(color: Color) {
  97. c := f32_color_from_color(color)
  98. gl.ClearColor(c.r, c.g, c.b, c.a)
  99. gl.ClearDepth(-1)
  100. gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
  101. }
  102. gl_present :: proc() {
  103. _gl_present(s.window_handle)
  104. }
  105. gl_draw :: proc(shd: Shader, texture: Texture_Handle, scissor: Maybe(Rect), vertex_buffer: []u8) {
  106. gl_shd := hm.get(&s.shaders, shd.handle)
  107. if gl_shd == nil {
  108. return
  109. }
  110. gl.EnableVertexAttribArray(0)
  111. gl.EnableVertexAttribArray(1)
  112. gl.EnableVertexAttribArray(2)
  113. gl.UseProgram(gl_shd.program)
  114. assert(len(shd.constants) == len(gl_shd.constants))
  115. cpu_data := shd.constants_data
  116. for cidx in 0..<len(gl_shd.constants) {
  117. cpu_loc := shd.constants[cidx]
  118. gpu_loc := gl_shd.constants[cidx]
  119. switch gpu_loc.type {
  120. case .Block_Variable:
  121. gpu_buffer_info := gl_shd.constant_buffers[gpu_loc.loc]
  122. gpu_data := gpu_buffer_info.buffer
  123. gl.BindBuffer(gl.UNIFORM_BUFFER, gpu_data)
  124. src := cpu_data[cpu_loc.offset:cpu_loc.offset+cpu_loc.size]
  125. gl.BufferData(gl.UNIFORM_BUFFER, len(src), raw_data(src), gl.DYNAMIC_DRAW)
  126. gl.BindBufferBase(gl.UNIFORM_BUFFER, gpu_loc.loc, gpu_data)
  127. case .Uniform:
  128. //gl.uniform
  129. }
  130. }
  131. gl.BindBuffer(gl.ARRAY_BUFFER, s.vertex_buffer_gpu)
  132. vb_data := gl.MapBuffer(gl.ARRAY_BUFFER, gl.WRITE_ONLY)
  133. {
  134. gpu_map := slice.from_ptr((^u8)(vb_data), VERTEX_BUFFER_MAX)
  135. copy(
  136. gpu_map,
  137. vertex_buffer,
  138. )
  139. }
  140. gl.UnmapBuffer(gl.ARRAY_BUFFER)
  141. gl.DrawArrays(gl.TRIANGLES, 0, i32(len(vertex_buffer)/shd.vertex_size))
  142. }
  143. gl_resize_swapchain :: proc(w, h: int) {
  144. }
  145. gl_get_swapchain_width :: proc() -> int {
  146. return s.width
  147. }
  148. gl_get_swapchain_height :: proc() -> int {
  149. return s.height
  150. }
  151. gl_flip_z :: proc() -> bool {
  152. return false
  153. }
  154. gl_set_internal_state :: proc(state: rawptr) {
  155. }
  156. gl_create_texture :: proc(width: int, height: int, format: Pixel_Format) -> Texture_Handle {
  157. return {}
  158. }
  159. gl_load_texture :: proc(data: []u8, width: int, height: int, format: Pixel_Format) -> Texture_Handle {
  160. return {}
  161. }
  162. gl_update_texture :: proc(th: Texture_Handle, data: []u8, rect: Rect) -> bool {
  163. return false
  164. }
  165. gl_destroy_texture :: proc(th: Texture_Handle) {
  166. }
  167. Shader_Compile_Result_OK :: struct {}
  168. Shader_Compile_Result_Error :: string
  169. Shader_Compile_Result :: union #no_nil {
  170. Shader_Compile_Result_OK,
  171. Shader_Compile_Result_Error,
  172. }
  173. compile_shader_from_source :: proc(shader_data: string, shader_type: gl.Shader_Type, err_buf: []u8, err_msg: ^string) -> (shader_id: u32, ok: bool) {
  174. shader_id = gl.CreateShader(u32(shader_type))
  175. length := i32(len(shader_data))
  176. shader_cstr := cstring(raw_data(shader_data))
  177. gl.ShaderSource(shader_id, 1, &shader_cstr, &length)
  178. gl.CompileShader(shader_id)
  179. result: i32
  180. gl.GetShaderiv(shader_id, gl.COMPILE_STATUS, &result)
  181. if result != 1 {
  182. info_len: i32
  183. gl.GetShaderInfoLog(shader_id, i32(len(err_buf)), &info_len, raw_data(err_buf))
  184. err_msg^ = string(err_buf[:info_len])
  185. gl.DeleteShader(shader_id)
  186. return 0, false
  187. }
  188. return shader_id, true
  189. }
  190. link_shader :: proc(vs_shader: u32, fs_shader: u32, err_buf: []u8, err_msg: ^string) -> (program_id: u32, ok: bool) {
  191. program_id = gl.CreateProgram()
  192. gl.AttachShader(program_id, vs_shader)
  193. gl.AttachShader(program_id, fs_shader)
  194. gl.LinkProgram(program_id)
  195. result: i32
  196. gl.GetProgramiv(program_id, gl.LINK_STATUS, &result)
  197. if result != 1 {
  198. info_len: i32
  199. gl.GetProgramInfoLog(program_id, i32(len(err_buf)), &info_len, raw_data(err_buf))
  200. err_msg^ = string(err_buf[:info_len])
  201. gl.DeleteProgram(program_id)
  202. return 0, false
  203. }
  204. return program_id, true
  205. }
  206. gl_load_shader :: proc(vs_source: string, fs_source: string, desc_allocator := frame_allocator, layout_formats: []Pixel_Format = {}) -> (handle: Shader_Handle, desc: Shader_Desc) {
  207. @static err: [1024]u8
  208. err_msg: string
  209. vs_shader, vs_shader_ok := compile_shader_from_source(vs_source, gl.Shader_Type.VERTEX_SHADER, err[:], &err_msg)
  210. if !vs_shader_ok {
  211. log.error(err_msg)
  212. return {}, {}
  213. }
  214. fs_shader, fs_shader_ok := compile_shader_from_source(fs_source, gl.Shader_Type.FRAGMENT_SHADER, err[:], &err_msg)
  215. if !fs_shader_ok {
  216. log.error(err_msg)
  217. return {}, {}
  218. }
  219. program, program_ok := link_shader(vs_shader, fs_shader, err[:], &err_msg)
  220. if !program_ok {
  221. log.error(err_msg)
  222. return {}, {}
  223. }
  224. stride: int
  225. {
  226. num_attribs: i32
  227. gl.GetProgramiv(program, gl.ACTIVE_ATTRIBUTES, &num_attribs)
  228. desc.inputs = make([]Shader_Input, num_attribs, desc_allocator)
  229. attrib_name_buf: [256]u8
  230. for i in 0..<num_attribs {
  231. attrib_name_len: i32
  232. attrib_size: i32
  233. attrib_type: u32
  234. gl.GetActiveAttrib(program, u32(i), i32(len(attrib_name_buf)), &attrib_name_len, &attrib_size, &attrib_type, raw_data(attrib_name_buf[:]))
  235. name_cstr := cstring(raw_data(attrib_name_buf[:attrib_name_len]))
  236. loc := gl.GetAttribLocation(program, name_cstr)
  237. if loc >= num_attribs {
  238. continue
  239. }
  240. type: Shader_Input_Type
  241. switch attrib_type {
  242. case gl.FLOAT: type = .F32
  243. case gl.FLOAT_VEC2: type = .Vec2
  244. case gl.FLOAT_VEC3: type = .Vec3
  245. case gl.FLOAT_VEC4: type = .Vec4
  246. /* Possible (gl.) types:
  247. FLOAT, FLOAT_VEC2, FLOAT_VEC3, FLOAT_VEC4, FLOAT_MAT2,
  248. FLOAT_MAT3, FLOAT_MAT4, FLOAT_MAT2x3, FLOAT_MAT2x4,
  249. FLOAT_MAT3x2, FLOAT_MAT3x4, FLOAT_MAT4x2, FLOAT_MAT4x3,
  250. INT, INT_VEC2, INT_VEC3, INT_VEC4, UNSIGNED_INT,
  251. UNSIGNED_INT_VEC2, UNSIGNED_INT_VEC3, UNSIGNED_INT_VEC4,
  252. DOUBLE, DOUBLE_VEC2, DOUBLE_VEC3, DOUBLE_VEC4, DOUBLE_MAT2,
  253. DOUBLE_MAT3, DOUBLE_MAT4, DOUBLE_MAT2x3, DOUBLE_MAT2x4,
  254. DOUBLE_MAT3x2, DOUBLE_MAT3x4, DOUBLE_MAT4x2, or DOUBLE_MAT4x3 */
  255. case: log.errorf("Unknown type: %v", attrib_type)
  256. }
  257. name := strings.clone(string(attrib_name_buf[:attrib_name_len]), desc_allocator)
  258. format := len(layout_formats) > 0 ? layout_formats[loc] : get_shader_input_format(name, type)
  259. desc.inputs[loc] = {
  260. name = name,
  261. register = int(loc),
  262. format = format,
  263. type = type,
  264. }
  265. input_format := get_shader_input_format(name, type)
  266. format_size := pixel_format_size(input_format)
  267. stride += format_size
  268. }
  269. }
  270. gl_shd := GL_Shader {
  271. program = program,
  272. }
  273. gl.GenVertexArrays(1, &gl_shd.vao)
  274. gl.BindVertexArray(gl_shd.vao)
  275. gl.BindBuffer(gl.ARRAY_BUFFER, s.vertex_buffer_gpu)
  276. offset: int
  277. for idx in 0..<len(desc.inputs) {
  278. input := desc.inputs[idx]
  279. format_size := pixel_format_size(input.format)
  280. gl.EnableVertexAttribArray(u32(idx))
  281. format, num_components, norm := gl_describe_pixel_format(input.format)
  282. gl.VertexAttribPointer(u32(idx), num_components, format, norm ? gl.TRUE : gl.FALSE, i32(stride), uintptr(offset))
  283. offset += format_size
  284. }
  285. /*{
  286. num_uniforms: i32
  287. uniform_name_buf: [256]u8
  288. gl.GetProgramiv(program, gl.ACTIVE_UNIFORMS, &num_uniforms)
  289. for u_idx in 0..<num_uniforms {
  290. name_len: i32
  291. size: i32
  292. type: u32
  293. gl.GetActiveUniform(program, u32(u_idx), len(uniform_name_buf), &name_len, &size, &type, raw_data(&uniform_name_buf))
  294. if type == gl.SAMPLER_2D {
  295. }
  296. }
  297. }*/
  298. constant_descs: [dynamic]Shader_Constant_Desc
  299. gl_constants: [dynamic]GL_Shader_Constant
  300. {
  301. num_active_uniforms: i32
  302. gl.GetProgramiv(program, gl.ACTIVE_UNIFORMS, &num_active_uniforms)
  303. uniform_name_buf: [256]u8
  304. for cidx in 0..<num_active_uniforms {
  305. name_len: i32
  306. array_len: i32
  307. type: u32
  308. gl.GetActiveUniform(program, u32(cidx), len(uniform_name_buf), &name_len, &array_len, &type,
  309. raw_data(&uniform_name_buf))
  310. name := cstring(raw_data(uniform_name_buf[:name_len]))
  311. loc := gl.GetUniformLocation(program, name)
  312. sz: int
  313. switch type {
  314. case gl.FLOAT: sz = size_of(f32)
  315. case gl.FLOAT_VEC2: sz = size_of(Vec2)
  316. case gl.FLOAT_VEC3: sz = size_of(Vec3)
  317. case gl.FLOAT_VEC4: sz = size_of(Vec4)
  318. case gl.FLOAT_MAT4: sz = size_of(Mat4)
  319. case: log.errorf("Unknown type: %x", type)
  320. }
  321. append(&constant_descs, Shader_Constant_Desc {
  322. name = strings.clone_from_cstring(name, desc_allocator),
  323. size = sz,
  324. })
  325. append(&gl_constants, GL_Shader_Constant {
  326. type = .Uniform,
  327. loc = u32(loc),
  328. })
  329. }
  330. }
  331. // Blocks are like constant buffers in D3D, it's like a struct with multiple uniforms inside
  332. {
  333. num_active_uniform_blocks: i32
  334. gl.GetProgramiv(program, gl.ACTIVE_UNIFORM_BLOCKS, &num_active_uniform_blocks)
  335. gl_shd.constant_buffers = make([]GL_Shader_Constant_Buffer, num_active_uniform_blocks, s.allocator)
  336. uniform_block_name_buf: [256]u8
  337. uniform_name_buf: [256]u8
  338. for cb_idx in 0..<num_active_uniform_blocks {
  339. name_len: i32
  340. gl.GetActiveUniformBlockName(program, u32(cb_idx), len(uniform_block_name_buf), &name_len, raw_data(&uniform_block_name_buf))
  341. name_cstr := cstring(raw_data(uniform_block_name_buf[:name_len]))
  342. idx := gl.GetUniformBlockIndex(program, name_cstr)
  343. if i32(idx) >= num_active_uniform_blocks {
  344. continue
  345. }
  346. size: i32
  347. // TODO investigate if we need std140 layout in the shader or what is fine?
  348. gl.GetActiveUniformBlockiv(program, idx, gl.UNIFORM_BLOCK_DATA_SIZE, &size)
  349. if size == 0 {
  350. log.errorf("Uniform block %v has size 0", name_cstr)
  351. continue
  352. }
  353. buf: u32
  354. gl.GenBuffers(1, &buf)
  355. gl.BindBuffer(gl.UNIFORM_BUFFER, buf)
  356. gl.BufferData(gl.UNIFORM_BUFFER, int(size), nil, gl.DYNAMIC_DRAW)
  357. gl.BindBufferBase(gl.UNIFORM_BUFFER, idx, buf)
  358. gl_shd.constant_buffers[cb_idx] = {
  359. block_index = idx,
  360. buffer = buf,
  361. size = int(size),
  362. }
  363. num_uniforms: i32
  364. gl.GetActiveUniformBlockiv(program, idx, gl.UNIFORM_BLOCK_ACTIVE_UNIFORMS, &num_uniforms)
  365. uniform_indices := make([]i32, num_uniforms, frame_allocator)
  366. gl.GetActiveUniformBlockiv(program, idx, gl.UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, raw_data(uniform_indices))
  367. for var_idx in 0..<num_uniforms {
  368. uniform_idx := u32(uniform_indices[var_idx])
  369. offset: i32
  370. gl.GetActiveUniformsiv(program, 1, &uniform_idx, gl.UNIFORM_OFFSET, &offset)
  371. uniform_type: i32
  372. gl.GetActiveUniformsiv(program, 1, &uniform_idx, gl.UNIFORM_TYPE, &uniform_type)
  373. sz: int
  374. switch uniform_type {
  375. case gl.FLOAT: sz = size_of(f32)
  376. case gl.FLOAT_VEC2: sz = size_of(Vec2)
  377. case gl.FLOAT_VEC3: sz = size_of(Vec3)
  378. case gl.FLOAT_VEC4: sz = size_of(Vec4)
  379. case gl.FLOAT_MAT4: sz = size_of(Mat4)
  380. case: log.errorf("Unknwon type: %x", uniform_type)
  381. }
  382. variable_name_len: i32
  383. gl.GetActiveUniformName(program, uniform_idx, len(uniform_name_buf), &variable_name_len, raw_data(&uniform_name_buf))
  384. append(&constant_descs, Shader_Constant_Desc {
  385. name = strings.clone(string(uniform_name_buf[:variable_name_len]), desc_allocator),
  386. size = sz,
  387. })
  388. append(&gl_constants, GL_Shader_Constant {
  389. type = .Block_Variable,
  390. loc = idx,
  391. block_variable_offset = u32(offset),
  392. })
  393. }
  394. }
  395. }
  396. assert(len(constant_descs) == len(gl_constants))
  397. desc.constants = constant_descs[:]
  398. gl_shd.constants = gl_constants[:]
  399. h := hm.add(&s.shaders, gl_shd)
  400. return h, desc
  401. }
  402. gl_describe_pixel_format :: proc(f: Pixel_Format) -> (format: u32, num_components: i32, normalized: bool) {
  403. switch f {
  404. case .RGBA_32_Float: return gl.FLOAT, 4, false
  405. case .RGB_32_Float: return gl.FLOAT, 3, false
  406. case .RG_32_Float: return gl.FLOAT, 2, false
  407. case .R_32_Float: return gl.FLOAT, 1, false
  408. case .RGBA_8_Norm: return gl.UNSIGNED_BYTE, 4, true
  409. case .RG_8_Norm: return gl.UNSIGNED_BYTE, 2, true
  410. case .R_8_Norm: return gl.UNSIGNED_BYTE, 1, true
  411. case .R_8_UInt: return gl.BYTE, 1, false
  412. case .Unknown:
  413. }
  414. log.error("Unknown format")
  415. return 0, 0, false
  416. }
  417. gl_destroy_shader :: proc(h: Shader_Handle) {
  418. }
  419. gl_default_shader_vertex_source :: proc() -> string {
  420. return #load("default_shader_vertex.glsl")
  421. }
  422. gl_default_shader_fragment_source :: proc() -> string {
  423. return #load("default_shader_fragment.glsl")
  424. }