render_backend_gl.odin 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. set_internal_state = gl_set_internal_state,
  16. create_texture = gl_create_texture,
  17. load_texture = gl_load_texture,
  18. update_texture = gl_update_texture,
  19. destroy_texture = gl_destroy_texture,
  20. load_shader = gl_load_shader,
  21. destroy_shader = gl_destroy_shader,
  22. }
  23. import "base:runtime"
  24. import gl "vendor:OpenGL"
  25. import glfw "vendor:glfw"
  26. GL_State :: struct {
  27. width: int,
  28. height: int,
  29. allocator: runtime.Allocator,
  30. }
  31. s: ^GL_State
  32. gl_state_size :: proc() -> int {
  33. return size_of(GL_State)
  34. }
  35. gl_init :: proc(state: rawptr, window_handle: Window_Handle, swapchain_width, swapchain_height: int, allocator := context.allocator) {
  36. s = (^GL_State)(state)
  37. s.width = swapchain_width
  38. s.height = swapchain_height
  39. s.allocator = allocator
  40. gl.load_up_to(3, 3, glfw.gl_set_proc_address)
  41. }
  42. gl_shutdown :: proc() {
  43. }
  44. gl_clear :: proc(color: Color) {
  45. }
  46. gl_present :: proc() {
  47. }
  48. gl_draw :: proc(shd: Shader, texture: Texture_Handle, view_proj: Mat4, scissor: Maybe(Rect), vertex_buffer: []u8) {
  49. }
  50. gl_resize_swapchain :: proc(w, h: int) {
  51. }
  52. gl_get_swapchain_width :: proc() -> int {
  53. return s.width
  54. }
  55. gl_get_swapchain_height :: proc() -> int {
  56. return s.height
  57. }
  58. gl_set_internal_state :: proc(state: rawptr) {
  59. }
  60. gl_create_texture :: proc(width: int, height: int, format: Pixel_Format) -> Texture_Handle {
  61. return {}
  62. }
  63. gl_load_texture :: proc(data: []u8, width: int, height: int, format: Pixel_Format) -> Texture_Handle {
  64. return {}
  65. }
  66. gl_update_texture :: proc(th: Texture_Handle, data: []u8, rect: Rect) -> bool {
  67. return false
  68. }
  69. gl_destroy_texture :: proc(th: Texture_Handle) {
  70. }
  71. gl_load_shader :: proc(vs_source: string, fs_source: string, desc_allocator := frame_allocator, layout_formats: []Pixel_Format = {}) -> (handle: Shader_Handle, desc: Shader_Desc) {
  72. vs_id := gl.CreateShader(u32(gl.Shader_Type.VERTEX_SHADER))
  73. vs_len := i32(len(vs_source))
  74. vs_cstr := cstring(raw_data(vs_source))
  75. gl.ShaderSource(vs_id, 1, &vs_cstr, &vs_len)
  76. gl.CompileShader(vs_id)
  77. fs_id := gl.CreateShader(u32(gl.Shader_Type.FRAGMENT_SHADER))
  78. fs_len := i32(len(fs_source))
  79. fs_cstr := cstring(raw_data(fs_source))
  80. gl.ShaderSource(fs_id, 1, &fs_cstr, &fs_len)
  81. gl.CompileShader(fs_id)
  82. return {}, {}
  83. }
  84. gl_destroy_shader :: proc(h: Shader_Handle) {
  85. }