render_backend_gl.odin 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 hm "handle_map"
  26. import "core:log"
  27. import win32 "core:sys/windows"
  28. GL_State :: struct {
  29. width: int,
  30. height: int,
  31. allocator: runtime.Allocator,
  32. shaders: hm.Handle_Map(GL_Shader, Shader_Handle, 1024*10),
  33. }
  34. GL_Shader :: struct {
  35. handle: Shader_Handle,
  36. program: u32,
  37. }
  38. s: ^GL_State
  39. gl_state_size :: proc() -> int {
  40. return size_of(GL_State)
  41. }
  42. gl_init :: proc(state: rawptr, window_handle: Window_Handle, swapchain_width, swapchain_height: int, allocator := context.allocator) {
  43. s = (^GL_State)(state)
  44. s.width = swapchain_width
  45. s.height = swapchain_height
  46. s.allocator = allocator
  47. // https://wikis.khronos.org/opengl/Creating_an_OpenGL_Context_(WGL)
  48. ctx := win32.wglCreateContext(win32.HDC(window_handle))
  49. win32.wglMakeCurrent(win32.HDC(window_handle), ctx)
  50. gl.load_up_to(3, 3, win32.gl_set_proc_address)
  51. }
  52. gl_shutdown :: proc() {
  53. }
  54. gl_clear :: proc(color: Color) {
  55. c := f32_color_from_color(color)
  56. gl.ClearColor(c.r, c.g, c.b, c.a)
  57. gl.Clear(gl.COLOR_BUFFER_BIT)
  58. }
  59. gl_present :: proc() {
  60. }
  61. gl_draw :: proc(shd: Shader, texture: Texture_Handle, view_proj: Mat4, scissor: Maybe(Rect), vertex_buffer: []u8) {
  62. }
  63. gl_resize_swapchain :: proc(w, h: int) {
  64. }
  65. gl_get_swapchain_width :: proc() -> int {
  66. return s.width
  67. }
  68. gl_get_swapchain_height :: proc() -> int {
  69. return s.height
  70. }
  71. gl_set_internal_state :: proc(state: rawptr) {
  72. }
  73. gl_create_texture :: proc(width: int, height: int, format: Pixel_Format) -> Texture_Handle {
  74. return {}
  75. }
  76. gl_load_texture :: proc(data: []u8, width: int, height: int, format: Pixel_Format) -> Texture_Handle {
  77. return {}
  78. }
  79. gl_update_texture :: proc(th: Texture_Handle, data: []u8, rect: Rect) -> bool {
  80. return false
  81. }
  82. gl_destroy_texture :: proc(th: Texture_Handle) {
  83. }
  84. gl_load_shader :: proc(vs_source: string, fs_source: string, desc_allocator := frame_allocator, layout_formats: []Pixel_Format = {}) -> (handle: Shader_Handle, desc: Shader_Desc) {
  85. vs_id := gl.CreateShader(u32(gl.Shader_Type.VERTEX_SHADER))
  86. vs_len := i32(len(vs_source))
  87. vs_cstr := cstring(raw_data(vs_source))
  88. gl.ShaderSource(vs_id, 1, &vs_cstr, &vs_len)
  89. gl.CompileShader(vs_id)
  90. fs_id := gl.CreateShader(u32(gl.Shader_Type.FRAGMENT_SHADER))
  91. fs_len := i32(len(fs_source))
  92. fs_cstr := cstring(raw_data(fs_source))
  93. gl.ShaderSource(fs_id, 1, &fs_cstr, &fs_len)
  94. gl.CompileShader(fs_id)
  95. program := gl.CreateProgram()
  96. gl.AttachShader(program, vs_id)
  97. gl.AttachShader(program, fs_id)
  98. gl.LinkProgram(program)
  99. shader := GL_Shader {
  100. program = program,
  101. }
  102. h := hm.add(&s.shaders, shader)
  103. return h, {}
  104. }
  105. gl_destroy_shader :: proc(h: Shader_Handle) {
  106. }