render_backend_gl.odin 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. dc: win32.HDC,
  34. }
  35. GL_Shader :: struct {
  36. handle: Shader_Handle,
  37. program: u32,
  38. }
  39. s: ^GL_State
  40. gl_state_size :: proc() -> int {
  41. return size_of(GL_State)
  42. }
  43. gl_init :: proc(state: rawptr, window_handle: Window_Handle, swapchain_width, swapchain_height: int, allocator := context.allocator) {
  44. s = (^GL_State)(state)
  45. s.width = swapchain_width
  46. s.height = swapchain_height
  47. s.allocator = allocator
  48. pfd := win32.PIXELFORMATDESCRIPTOR {
  49. size_of(win32.PIXELFORMATDESCRIPTOR),
  50. 1,
  51. win32.PFD_DRAW_TO_WINDOW | win32.PFD_SUPPORT_OPENGL | win32.PFD_DOUBLEBUFFER, // Flags
  52. win32.PFD_TYPE_RGBA, // The kind of framebuffer. RGBA or palette.
  53. 32, // Colordepth of the framebuffer.
  54. 0, 0, 0, 0, 0, 0,
  55. 0,
  56. 0,
  57. 0,
  58. 0, 0, 0, 0,
  59. 24, // Number of bits for the depthbuffer
  60. 8, // Number of bits for the stencilbuffer
  61. 0, // Number of Aux buffers in the framebuffer.
  62. win32.PFD_MAIN_PLANE,
  63. 0,
  64. 0, 0, 0
  65. }
  66. hdc := win32.GetWindowDC(win32.HWND(window_handle))
  67. s.dc = hdc
  68. fmt := win32.ChoosePixelFormat(hdc, &pfd)
  69. win32.SetPixelFormat(hdc, fmt, &pfd)
  70. // https://wikis.khronos.org/opengl/Creating_an_OpenGL_Context_(WGL)
  71. ctx := win32.wglCreateContext(hdc)
  72. win32.wglMakeCurrent(hdc, ctx)
  73. gl.load_up_to(3, 3, win32.gl_set_proc_address)
  74. }
  75. gl_shutdown :: proc() {
  76. }
  77. gl_clear :: proc(color: Color) {
  78. c := f32_color_from_color(color)
  79. gl.ClearColor(c.r, c.g, c.b, c.a)
  80. gl.Clear(gl.COLOR_BUFFER_BIT)
  81. }
  82. gl_present :: proc() {
  83. win32.SwapBuffers(s.dc)
  84. }
  85. gl_draw :: proc(shd: Shader, texture: Texture_Handle, view_proj: Mat4, scissor: Maybe(Rect), vertex_buffer: []u8) {
  86. }
  87. gl_resize_swapchain :: proc(w, h: int) {
  88. }
  89. gl_get_swapchain_width :: proc() -> int {
  90. return s.width
  91. }
  92. gl_get_swapchain_height :: proc() -> int {
  93. return s.height
  94. }
  95. gl_set_internal_state :: proc(state: rawptr) {
  96. }
  97. gl_create_texture :: proc(width: int, height: int, format: Pixel_Format) -> Texture_Handle {
  98. return {}
  99. }
  100. gl_load_texture :: proc(data: []u8, width: int, height: int, format: Pixel_Format) -> Texture_Handle {
  101. return {}
  102. }
  103. gl_update_texture :: proc(th: Texture_Handle, data: []u8, rect: Rect) -> bool {
  104. return false
  105. }
  106. gl_destroy_texture :: proc(th: Texture_Handle) {
  107. }
  108. gl_load_shader :: proc(vs_source: string, fs_source: string, desc_allocator := frame_allocator, layout_formats: []Pixel_Format = {}) -> (handle: Shader_Handle, desc: Shader_Desc) {
  109. vs_id := gl.CreateShader(u32(gl.Shader_Type.VERTEX_SHADER))
  110. vs_len := i32(len(vs_source))
  111. vs_cstr := cstring(raw_data(vs_source))
  112. gl.ShaderSource(vs_id, 1, &vs_cstr, &vs_len)
  113. gl.CompileShader(vs_id)
  114. fs_id := gl.CreateShader(u32(gl.Shader_Type.FRAGMENT_SHADER))
  115. fs_len := i32(len(fs_source))
  116. fs_cstr := cstring(raw_data(fs_source))
  117. gl.ShaderSource(fs_id, 1, &fs_cstr, &fs_len)
  118. gl.CompileShader(fs_id)
  119. program := gl.CreateProgram()
  120. gl.AttachShader(program, vs_id)
  121. gl.AttachShader(program, fs_id)
  122. gl.LinkProgram(program)
  123. shader := GL_Shader {
  124. program = program,
  125. }
  126. h := hm.add(&s.shaders, shader)
  127. return h, {}
  128. }
  129. gl_destroy_shader :: proc(h: Shader_Handle) {
  130. }