render_backend_webgl.odin 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. #+build js
  2. #+private file
  3. package karl2d
  4. @(private="package")
  5. RENDER_BACKEND_WEBGL :: Render_Backend_Interface {
  6. state_size = webgl_state_size,
  7. init = webgl_init,
  8. shutdown = webgl_shutdown,
  9. clear = webgl_clear,
  10. present = webgl_present,
  11. draw = webgl_draw,
  12. resize_swapchain = webgl_resize_swapchain,
  13. get_swapchain_width = webgl_get_swapchain_width,
  14. get_swapchain_height = webgl_get_swapchain_height,
  15. flip_z = webgl_flip_z,
  16. set_internal_state = webgl_set_internal_state,
  17. create_texture = webgl_create_texture,
  18. load_texture = webgl_load_texture,
  19. update_texture = webgl_update_texture,
  20. destroy_texture = webgl_destroy_texture,
  21. texture_needs_vertical_flip = webgl_texture_needs_vertical_flip,
  22. create_render_texture = webgl_create_render_texture,
  23. destroy_render_target = webgl_destroy_render_target,
  24. set_texture_filter = webgl_set_texture_filter,
  25. load_shader = webgl_load_shader,
  26. destroy_shader = webgl_destroy_shader,
  27. default_shader_vertex_source = webgl_default_shader_vertex_source,
  28. default_shader_fragment_source = webgl_default_shader_fragment_source,
  29. }
  30. import "base:runtime"
  31. import gl "vendor:wasm/WebGL"
  32. import hm "handle_map"
  33. import "core:log"
  34. import "core:strings"
  35. import la "core:math/linalg"
  36. _ :: la
  37. WebGL_State :: struct {
  38. canvas_id: string,
  39. width: int,
  40. height: int,
  41. allocator: runtime.Allocator,
  42. shaders: hm.Handle_Map(WebGL_Shader, Shader_Handle, 1024*10),
  43. vertex_buffer_gpu: gl.Buffer,
  44. textures: hm.Handle_Map(WebGL_Texture, Texture_Handle, 1024*10),
  45. render_targets: hm.Handle_Map(WebGL_Render_Target, Render_Target_Handle, 128),
  46. }
  47. WebGL_Shader_Constant_Buffer :: struct {
  48. buffer: gl.Buffer,
  49. size: int,
  50. block_index: i32,
  51. }
  52. WebGL_Shader_Constant_Type :: enum {
  53. Uniform,
  54. Block_Variable,
  55. }
  56. // OpenGL can have constants both in blocks (like constant buffers in D3D11), or as stand-alone
  57. // uniforms. We support both.
  58. WebGL_Shader_Constant :: struct {
  59. type: WebGL_Shader_Constant_Type,
  60. // if type is Uniform, then this is the uniform loc
  61. // if type is Block_Variable, then this is the block loc
  62. loc: i32,
  63. // if this is a block variable, then this is the offset to it
  64. block_variable_offset: u32,
  65. // if type is Uniform, then this contains the GL type of the uniform
  66. uniform_type: gl.Enum,
  67. }
  68. WebGL_Texture :: struct {
  69. handle: Texture_Handle,
  70. id: gl.Texture,
  71. format: Pixel_Format,
  72. needs_vertical_flip: bool,
  73. }
  74. WebGL_Texture_Binding :: struct {
  75. loc: i32,
  76. }
  77. WebGL_Render_Target :: struct {
  78. handle: Render_Target_Handle,
  79. depth: gl.Renderbuffer,
  80. framebuffer: gl.Framebuffer,
  81. width: int,
  82. height: int,
  83. }
  84. WebGL_Shader :: struct {
  85. handle: Shader_Handle,
  86. // This is like the "input layout"
  87. vao: gl.VertexArrayObject,
  88. program: gl.Program,
  89. constant_buffers: []WebGL_Shader_Constant_Buffer,
  90. constants: []WebGL_Shader_Constant,
  91. texture_bindings: []WebGL_Texture_Binding,
  92. }
  93. s: ^WebGL_State
  94. webgl_state_size :: proc() -> int {
  95. return size_of(WebGL_State)
  96. }
  97. webgl_init :: proc(state: rawptr, window_handle: Window_Handle, swapchain_width, swapchain_height: int, allocator := context.allocator) {
  98. s = (^WebGL_State)(state)
  99. canvas_id := (^HTML_Canvas_ID)(window_handle)^
  100. s.canvas_id = strings.clone(canvas_id, allocator)
  101. s.width = swapchain_width
  102. s.height = swapchain_height
  103. s.allocator = allocator
  104. context_ok := gl.CreateCurrentContextById(s.canvas_id, gl.DEFAULT_CONTEXT_ATTRIBUTES)
  105. log.ensuref(context_ok, "Could not create context for canvas ID %s", s.canvas_id)
  106. set_context_ok := gl.SetCurrentContextById(s.canvas_id)
  107. log.ensuref(set_context_ok, "Failed setting context with canvas ID %s", s.canvas_id)
  108. gl.Enable(gl.DEPTH_TEST)
  109. gl.DepthFunc(gl.GREATER)
  110. s.vertex_buffer_gpu = gl.CreateBuffer()
  111. gl.BindBuffer(gl.ARRAY_BUFFER, s.vertex_buffer_gpu)
  112. gl.BufferData(gl.ARRAY_BUFFER, VERTEX_BUFFER_MAX, nil, gl.STREAM_DRAW)
  113. gl.Enable(gl.BLEND)
  114. gl.Viewport(0, 0, i32(s.width), i32(s.height))
  115. }
  116. webgl_shutdown :: proc() {
  117. gl.DeleteBuffer(s.vertex_buffer_gpu)
  118. }
  119. webgl_clear :: proc(render_target: Render_Target_Handle, color: Color) {
  120. if rt := hm.get(&s.render_targets, render_target); rt != nil {
  121. gl.BindFramebuffer(gl.FRAMEBUFFER, rt.framebuffer)
  122. gl.Viewport(0, 0, i32(rt.width), i32(rt.height))
  123. } else {
  124. gl.BindFramebuffer(gl.FRAMEBUFFER, 0)
  125. gl.Viewport(0, 0, i32(s.width), i32(s.height))
  126. }
  127. c := f32_color_from_color(color)
  128. gl.ClearColor(c.r, c.g, c.b, c.a)
  129. gl.ClearDepth(-1)
  130. gl.Clear(u32(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT))
  131. }
  132. webgl_present :: proc() {
  133. // The browser flips the backbuffer for you when 'step' ends
  134. }
  135. webgl_draw :: proc(
  136. shd: Shader,
  137. render_target: Render_Target_Handle,
  138. bound_textures: []Texture_Handle,
  139. scissor: Maybe(Rect),
  140. blend_mode: Blend_Mode,
  141. vertex_buffer: []u8,
  142. ) {
  143. gl_shd := hm.get(&s.shaders, shd.handle)
  144. if gl_shd == nil {
  145. return
  146. }
  147. switch blend_mode {
  148. case .Alpha: gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
  149. case .Premultiplied_Alpha: gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA)
  150. }
  151. gl.BindVertexArray(gl_shd.vao)
  152. gl.UseProgram(gl_shd.program)
  153. assert(len(shd.constants) == len(gl_shd.constants))
  154. cpu_data := shd.constants_data
  155. for cidx in 0..<len(gl_shd.constants) {
  156. cpu_loc := shd.constants[cidx]
  157. if cpu_loc.size == 0 {
  158. continue
  159. }
  160. gpu_loc := gl_shd.constants[cidx]
  161. switch gpu_loc.type {
  162. case .Block_Variable:
  163. gpu_buffer_info := gl_shd.constant_buffers[gpu_loc.loc]
  164. gpu_data := gpu_buffer_info.buffer
  165. gl.BindBuffer(gl.UNIFORM_BUFFER, gpu_data)
  166. src := cpu_data[cpu_loc.offset:cpu_loc.offset+cpu_loc.size]
  167. gl.BufferData(gl.UNIFORM_BUFFER, len(src), raw_data(src), gl.DYNAMIC_DRAW)
  168. gl.BindBufferBase(gl.UNIFORM_BUFFER, gpu_loc.loc, gpu_data)
  169. case .Uniform:
  170. loc := i32(gpu_loc.loc)
  171. ptr := (rawptr)(&cpu_data[cpu_loc.offset])
  172. uptr: [^]u32 = (^u32)(ptr)
  173. iptr: [^]i32 = (^i32)(ptr)
  174. fptr: [^]f32 = (^f32)(ptr)
  175. switch gpu_loc.uniform_type {
  176. case gl.FLOAT:
  177. gl.Uniform1f(loc, fptr[0])
  178. case gl.FLOAT_VEC2:
  179. gl.Uniform2f(loc, fptr[0], fptr[1])
  180. case gl.FLOAT_MAT2:
  181. gl.UniformMatrix2fv(loc, (^matrix[2,2]f32)(ptr)^)
  182. case gl.FLOAT_MAT2x3:
  183. gl.UniformMatrix2x3fv(loc, (^matrix[3,2]f32)(ptr)^)
  184. case gl.FLOAT_MAT2x4:
  185. gl.UniformMatrix2x4fv(loc, (^matrix[4,2]f32)(ptr)^)
  186. case gl.FLOAT_VEC3:
  187. gl.Uniform3f(loc, fptr[0], fptr[1], fptr[2])
  188. case gl.FLOAT_MAT3x2:
  189. gl.UniformMatrix3x2fv(loc, (^matrix[2,3]f32)(ptr)^)
  190. case gl.FLOAT_MAT3:
  191. gl.UniformMatrix3fv(loc, (^matrix[3,3]f32)(ptr)^)
  192. case gl.FLOAT_MAT3x4:
  193. gl.UniformMatrix3x4fv(loc, (^matrix[4,3]f32)(ptr)^)
  194. case gl.FLOAT_VEC4:
  195. gl.Uniform4f(loc, fptr[0], fptr[1], fptr[2], fptr[3])
  196. case gl.FLOAT_MAT4x2:
  197. gl.UniformMatrix4x2fv(loc, (^matrix[2,4]f32)(ptr)^)
  198. case gl.FLOAT_MAT4x3:
  199. gl.UniformMatrix4x3fv(loc, (^matrix[3,4]f32)(ptr)^)
  200. case gl.FLOAT_MAT4:
  201. gl.UniformMatrix4fv(loc, (^matrix[4,4]f32)(ptr)^)
  202. case gl.INT:
  203. gl.Uniform1i(loc, iptr[0])
  204. case gl.INT_VEC2:
  205. gl.Uniform2i(loc, iptr[0], iptr[1])
  206. case gl.INT_VEC3:
  207. gl.Uniform3i(loc, iptr[0], iptr[1], iptr[2])
  208. case gl.INT_VEC4:
  209. gl.Uniform4i(loc, iptr[0], iptr[1], iptr[2], iptr[3])
  210. case gl.UNSIGNED_INT:
  211. gl.Uniform1ui(loc, uptr[0])
  212. case gl.UNSIGNED_INT_VEC2:
  213. gl.Uniform2ui(loc, uptr[0], uptr[1])
  214. case gl.UNSIGNED_INT_VEC3:
  215. gl.Uniform3ui(loc, uptr[0], uptr[1], uptr[2])
  216. case gl.UNSIGNED_INT_VEC4:
  217. gl.Uniform4ui(loc, uptr[0], uptr[1], uptr[2], uptr[3])
  218. case: log.errorf("Unknown type: %x", gpu_loc.uniform_type)
  219. }
  220. }
  221. }
  222. gl.BindBuffer(gl.ARRAY_BUFFER, s.vertex_buffer_gpu)
  223. gl.BufferDataSlice(gl.ARRAY_BUFFER, vertex_buffer, gl.STREAM_DRAW)
  224. if len(bound_textures) == len(gl_shd.texture_bindings) {
  225. for t, t_idx in bound_textures {
  226. gl_t := gl_shd.texture_bindings[t_idx]
  227. if t := hm.get(&s.textures, t); t != nil {
  228. gl.ActiveTexture(gl.TEXTURE0 + gl.Enum(t_idx))
  229. gl.BindTexture(gl.TEXTURE_2D, t.id)
  230. gl.Uniform1i(gl_t.loc, i32(t_idx))
  231. } else {
  232. gl.ActiveTexture(gl.TEXTURE0 + gl.Enum(t_idx))
  233. gl.BindTexture(gl.TEXTURE_2D, 0)
  234. gl.Uniform1i(gl_t.loc, i32(t_idx))
  235. }
  236. }
  237. }
  238. if rt := hm.get(&s.render_targets, render_target); rt != nil {
  239. gl.BindFramebuffer(gl.FRAMEBUFFER, rt.framebuffer)
  240. gl.Viewport(0, 0, i32(rt.width), i32(rt.height))
  241. } else {
  242. gl.BindFramebuffer(gl.FRAMEBUFFER, 0)
  243. gl.Viewport(0, 0, i32(s.width), i32(s.height))
  244. }
  245. gl.DrawArrays(gl.TRIANGLES, 0, int(len(vertex_buffer)/shd.vertex_size))
  246. }
  247. webgl_resize_swapchain :: proc(w, h: int) {
  248. s.width = w
  249. s.height = h
  250. gl.Viewport(0, 0, i32(w), i32(h))
  251. }
  252. webgl_get_swapchain_width :: proc() -> int {
  253. return s.width
  254. }
  255. webgl_get_swapchain_height :: proc() -> int {
  256. return s.height
  257. }
  258. webgl_flip_z :: proc() -> bool {
  259. return false
  260. }
  261. webgl_set_internal_state :: proc(state: rawptr) {
  262. s = (^WebGL_State)(state)
  263. }
  264. create_texture :: proc(width: int, height: int, format: Pixel_Format, data: rawptr) -> WebGL_Texture {
  265. id := gl.CreateTexture()
  266. gl.BindTexture(gl.TEXTURE_2D, id)
  267. gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, i32(gl.REPEAT))
  268. gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, i32(gl.REPEAT))
  269. gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, i32(gl.NEAREST))
  270. gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, i32(gl.NEAREST))
  271. pf := gl_translate_pixel_format(format)
  272. data_size := width*height*pixel_format_size(format)
  273. gl.TexImage2D(gl.TEXTURE_2D, 0, pf, i32(width), i32(height), 0, gl.RGBA, gl.UNSIGNED_BYTE, data_size, data)
  274. return {
  275. id = id,
  276. format = format,
  277. }
  278. }
  279. webgl_create_texture :: proc(width: int, height: int, format: Pixel_Format) -> Texture_Handle {
  280. return hm.add(&s.textures, create_texture(width, height, format, nil))
  281. }
  282. webgl_load_texture :: proc(data: []u8, width: int, height: int, format: Pixel_Format) -> Texture_Handle {
  283. return hm.add(&s.textures, create_texture(width, height, format, raw_data(data)))
  284. }
  285. webgl_update_texture :: proc(th: Texture_Handle, data: []u8, rect: Rect) -> bool {
  286. tex := hm.get(&s.textures, th)
  287. if tex == nil {
  288. return false
  289. }
  290. gl.BindTexture(gl.TEXTURE_2D, tex.id)
  291. gl.TexSubImage2D(gl.TEXTURE_2D, 0, i32(rect.x), i32(rect.y), i32(rect.w), i32(rect.h), gl.RGBA, gl.UNSIGNED_BYTE, len(data), raw_data(data))
  292. return true
  293. }
  294. webgl_destroy_texture :: proc(th: Texture_Handle) {
  295. tex := hm.get(&s.textures, th)
  296. if tex == nil {
  297. return
  298. }
  299. gl.DeleteTexture(tex.id)
  300. hm.remove(&s.textures, th)
  301. }
  302. webgl_texture_needs_vertical_flip :: proc(th: Texture_Handle) -> bool {
  303. tex := hm.get(&s.textures, th)
  304. if tex == nil {
  305. return false
  306. }
  307. return tex.needs_vertical_flip
  308. }
  309. webgl_create_render_texture :: proc(width: int, height: int) -> (Texture_Handle, Render_Target_Handle) {
  310. texture := create_texture(width, height, .RGBA_32_Float, nil)
  311. texture.needs_vertical_flip = true
  312. framebuffer := gl.CreateFramebuffer()
  313. gl.BindFramebuffer(gl.FRAMEBUFFER, framebuffer)
  314. depth := gl.CreateRenderbuffer()
  315. gl.BindRenderbuffer(gl.RENDERBUFFER, depth)
  316. gl.RenderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, i32(width), i32(height))
  317. gl.FramebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, depth)
  318. gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture.id, 0)
  319. gl.DrawBuffers({gl.COLOR_ATTACHMENT0})
  320. /*
  321. ADD BINDINGS FOR THIS
  322. if gl.CheckFramebufferStatus(gl.FRAMEBUFFER) != gl.FRAMEBUFFER_COMPLETE {
  323. log.errorf("Failed creating frame buffer of size %v x %v", width, height)
  324. return {}, {}
  325. }*/
  326. gl.BindFramebuffer(gl.FRAMEBUFFER, 0)
  327. gl.BindRenderbuffer(gl.RENDERBUFFER, 0)
  328. rt := WebGL_Render_Target {
  329. depth = depth,
  330. framebuffer = framebuffer,
  331. width = width,
  332. height = height,
  333. }
  334. return hm.add(&s.textures, texture), hm.add(&s.render_targets, rt)
  335. }
  336. webgl_destroy_render_target :: proc(render_target: Render_Target_Handle) {
  337. if rt := hm.get(&s.render_targets, render_target); rt != nil {
  338. gl.DeleteRenderbuffer(rt.depth)
  339. gl.DeleteFramebuffer(rt.framebuffer)
  340. }
  341. }
  342. webgl_set_texture_filter :: proc(
  343. th: Texture_Handle,
  344. scale_down_filter: Texture_Filter,
  345. scale_up_filter: Texture_Filter,
  346. mip_filter: Texture_Filter,
  347. ) {
  348. t := hm.get(&s.textures, th)
  349. if t == nil {
  350. log.error("Trying to set texture filter for invalid texture %v", th)
  351. return
  352. }
  353. gl.BindTexture(gl.TEXTURE_2D, t.id)
  354. min_filter := scale_down_filter == .Point ? gl.NEAREST : gl.LINEAR
  355. mag_filter := scale_up_filter == .Point ? gl.NEAREST : gl.LINEAR
  356. gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, i32(min_filter))
  357. gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, i32(mag_filter))
  358. }
  359. Shader_Compile_Result_OK :: struct {}
  360. Shader_Compile_Result_Error :: string
  361. Shader_Compile_Result :: union #no_nil {
  362. Shader_Compile_Result_OK,
  363. Shader_Compile_Result_Error,
  364. }
  365. compile_shader_from_source :: proc(shader_data: []byte, shader_type: gl.Enum, err_buf: []u8, err_msg: ^string) -> (shader_id: gl.Shader, ok: bool) {
  366. shader_id = gl.CreateShader(shader_type)
  367. gl.ShaderSource(shader_id, { string(shader_data) })
  368. gl.CompileShader(shader_id)
  369. result := gl.GetShaderiv(shader_id, gl.COMPILE_STATUS)
  370. if result != 1 {
  371. err_msg^ = gl.GetShaderInfoLog(shader_id, err_buf)
  372. gl.DeleteShader(shader_id)
  373. return 0, false
  374. }
  375. return shader_id, true
  376. }
  377. link_shader :: proc(vs_shader: gl.Shader, fs_shader: gl.Shader, err_buf: []u8, err_msg: ^string) -> (program_id: gl.Program, ok: bool) {
  378. program_id = gl.CreateProgram()
  379. gl.AttachShader(program_id, vs_shader)
  380. gl.AttachShader(program_id, fs_shader)
  381. gl.LinkProgram(program_id)
  382. status := gl.GetProgramParameter(program_id, gl.LINK_STATUS)
  383. if status != 1 {
  384. err_msg^ = gl.GetProgramInfoLog(program_id, err_buf)
  385. gl.DeleteProgram(program_id)
  386. return 0, false
  387. }
  388. return program_id, true
  389. }
  390. webgl_load_shader :: proc(vs_source: []byte, fs_source: []byte, desc_allocator := frame_allocator, layout_formats: []Pixel_Format = {}) -> (handle: Shader_Handle, desc: Shader_Desc) {
  391. @static err: [1024]u8
  392. err_msg: string
  393. vs_shader, vs_shader_ok := compile_shader_from_source(vs_source, gl.VERTEX_SHADER, err[:], &err_msg)
  394. if !vs_shader_ok {
  395. log.error(err_msg)
  396. return {}, {}
  397. }
  398. fs_shader, fs_shader_ok := compile_shader_from_source(fs_source, gl.FRAGMENT_SHADER, err[:], &err_msg)
  399. if !fs_shader_ok {
  400. log.error(err_msg)
  401. return {}, {}
  402. }
  403. program, program_ok := link_shader(vs_shader, fs_shader, err[:], &err_msg)
  404. if !program_ok {
  405. log.error(err_msg)
  406. return {}, {}
  407. }
  408. stride: int
  409. {
  410. num_attribs := gl.GetProgramParameter(program, gl.ACTIVE_ATTRIBUTES)
  411. desc.inputs = make([]Shader_Input, num_attribs, desc_allocator)
  412. for i in 0..<num_attribs {
  413. attrib_info := gl.GetActiveAttrib(program, u32(i), frame_allocator)
  414. loc := gl.GetAttribLocation(program, attrib_info.name)
  415. type: Shader_Input_Type
  416. switch attrib_info.type {
  417. case gl.FLOAT: type = .F32
  418. case gl.FLOAT_VEC2: type = .Vec2
  419. case gl.FLOAT_VEC3: type = .Vec3
  420. case gl.FLOAT_VEC4: type = .Vec4
  421. /* Possible (gl.) types:
  422. FLOAT, FLOAT_VEC2, FLOAT_VEC3, FLOAT_VEC4, FLOAT_MAT2,
  423. FLOAT_MAT3, FLOAT_MAT4, FLOAT_MAT2x3, FLOAT_MAT2x4,
  424. FLOAT_MAT3x2, FLOAT_MAT3x4, FLOAT_MAT4x2, FLOAT_MAT4x3,
  425. INT, INT_VEC2, INT_VEC3, INT_VEC4, UNSIGNED_INT,
  426. UNSIGNED_INT_VEC2, UNSIGNED_INT_VEC3, UNSIGNED_INT_VEC4,
  427. DOUBLE, DOUBLE_VEC2, DOUBLE_VEC3, DOUBLE_VEC4, DOUBLE_MAT2,
  428. DOUBLE_MAT3, DOUBLE_MAT4, DOUBLE_MAT2x3, DOUBLE_MAT2x4,
  429. DOUBLE_MAT3x2, DOUBLE_MAT3x4, DOUBLE_MAT4x2, or DOUBLE_MAT4x3 */
  430. case: log.errorf("Unknown type: %v", attrib_info.type)
  431. }
  432. name := strings.clone(attrib_info.name, desc_allocator)
  433. format := len(layout_formats) > 0 ? layout_formats[loc] : get_shader_input_format(name, type)
  434. desc.inputs[i] = {
  435. name = name,
  436. register = int(loc),
  437. format = format,
  438. type = type,
  439. }
  440. input_format := get_shader_input_format(name, type)
  441. format_size := pixel_format_size(input_format)
  442. stride += format_size
  443. }
  444. }
  445. gl_shd := WebGL_Shader {
  446. program = program,
  447. vao = gl.CreateVertexArray(),
  448. }
  449. gl.BindVertexArray(gl_shd.vao)
  450. offset: int
  451. for idx in 0..<len(desc.inputs) {
  452. input := desc.inputs[idx]
  453. format_size := pixel_format_size(input.format)
  454. gl.EnableVertexAttribArray(i32(input.register))
  455. format, num_components, norm := gl_describe_pixel_format(input.format)
  456. gl.VertexAttribPointer(i32(input.register), num_components, format, norm, stride, uintptr(offset))
  457. offset += format_size
  458. }
  459. constant_descs := make([dynamic]Shader_Constant_Desc, desc_allocator)
  460. gl_constants := make([dynamic]WebGL_Shader_Constant, s.allocator)
  461. texture_bindpoint_descs := make([dynamic]Shader_Texture_Bindpoint_Desc, desc_allocator)
  462. gl_texture_bindings := make([dynamic]WebGL_Texture_Binding, s.allocator)
  463. {
  464. num_active_uniforms := gl.GetProgramParameter(program, gl.ACTIVE_UNIFORMS)
  465. for cidx in 0..<num_active_uniforms {
  466. uniform_info := gl.GetActiveUniform(program, u32(cidx), frame_allocator)
  467. loc := gl.GetUniformLocation(program, uniform_info.name)
  468. if uniform_info.type == gl.SAMPLER_2D {
  469. append(&texture_bindpoint_descs, Shader_Texture_Bindpoint_Desc {
  470. name = strings.clone(uniform_info.name, desc_allocator),
  471. })
  472. append(&gl_texture_bindings, WebGL_Texture_Binding {
  473. loc = loc,
  474. })
  475. } else {
  476. append(&constant_descs, Shader_Constant_Desc {
  477. name = strings.clone(uniform_info.name, desc_allocator),
  478. size = uniform_size(uniform_info.type),
  479. })
  480. append(&gl_constants, WebGL_Shader_Constant {
  481. type = .Uniform,
  482. loc = loc,
  483. uniform_type = uniform_info.type,
  484. })
  485. }
  486. }
  487. }
  488. // Blocks are like constant buffers in D3D, it's like a struct with multiple uniforms inside
  489. {
  490. num_active_uniform_blocks := gl.GetProgramParameter(program, gl.ACTIVE_UNIFORM_BLOCKS)
  491. gl_shd.constant_buffers = make([]WebGL_Shader_Constant_Buffer, num_active_uniform_blocks, s.allocator)
  492. for cb_idx in 0..<num_active_uniform_blocks {
  493. name := gl.GetActiveUniformBlockName(program, i32(cb_idx), frame_allocator)
  494. idx := gl.GetUniformBlockIndex(program, name)
  495. if i32(idx) >= num_active_uniform_blocks {
  496. continue
  497. }
  498. size: i32
  499. // TODO investigate if we need std140 layout in the shader or what is fine?
  500. gl.GetActiveUniformBlockParameter(program, idx, gl.UNIFORM_BLOCK_DATA_SIZE, &size)
  501. if size == 0 {
  502. log.errorf("Uniform block %v has size 0", name)
  503. continue
  504. }
  505. buf := gl.CreateBuffer()
  506. gl.BindBuffer(gl.UNIFORM_BUFFER, buf)
  507. gl.BufferData(gl.UNIFORM_BUFFER, int(size), nil, gl.DYNAMIC_DRAW)
  508. gl.BindBufferBase(gl.UNIFORM_BUFFER, idx, buf)
  509. gl_shd.constant_buffers[cb_idx] = {
  510. block_index = idx,
  511. buffer = buf,
  512. size = int(size),
  513. }
  514. num_uniforms: i32
  515. gl.GetActiveUniformBlockParameter(program, idx, gl.UNIFORM_BLOCK_ACTIVE_UNIFORMS, &num_uniforms)
  516. uniform_indices := make([]i32, num_uniforms, frame_allocator)
  517. gl.GetActiveUniformBlockParameter(program, idx, gl.UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, raw_data(uniform_indices))
  518. for var_idx in 0..<num_uniforms {
  519. uniform_idx := u32(uniform_indices[var_idx])
  520. offset: i32
  521. gl.GetActiveUniforms(program, { uniform_idx }, gl.UNIFORM_OFFSET, &offset)
  522. uniform_info := gl.GetActiveUniform(program, uniform_idx, desc_allocator)
  523. append(&constant_descs, Shader_Constant_Desc {
  524. name = uniform_info.name,
  525. size = uniform_size(uniform_info.type),
  526. })
  527. append(&gl_constants, WebGL_Shader_Constant {
  528. type = .Block_Variable,
  529. loc = idx,
  530. block_variable_offset = u32(offset),
  531. })
  532. }
  533. }
  534. }
  535. assert(len(constant_descs) == len(gl_constants))
  536. desc.constants = constant_descs[:]
  537. desc.texture_bindpoints = texture_bindpoint_descs[:]
  538. gl_shd.constants = gl_constants[:]
  539. gl_shd.texture_bindings = gl_texture_bindings[:]
  540. h := hm.add(&s.shaders, gl_shd)
  541. return h, desc
  542. }
  543. // I might have missed something. But it doesn't seem like GL gives you this information.
  544. uniform_size :: proc(t: gl.Enum) -> int {
  545. sz: int
  546. switch t {
  547. case gl.FLOAT: sz = 4*1
  548. case gl.FLOAT_VEC2: sz = 4*2*1
  549. case gl.FLOAT_MAT2: sz = 4*2*2
  550. case gl.FLOAT_MAT2x3: sz = 4*2*3
  551. case gl.FLOAT_MAT2x4: sz = 4*2*4
  552. case gl.FLOAT_VEC3: sz = 4*3*1
  553. case gl.FLOAT_MAT3x2: sz = 4*3*2
  554. case gl.FLOAT_MAT3: sz = 4*3*3
  555. case gl.FLOAT_MAT3x4: sz = 4*3*4
  556. case gl.FLOAT_VEC4: sz = 4*4*1
  557. case gl.FLOAT_MAT4x2: sz = 4*4*2
  558. case gl.FLOAT_MAT4x3: sz = 4*4*3
  559. case gl.FLOAT_MAT4: sz = 4*4*4
  560. case gl.BOOL: sz = 4*1
  561. case gl.BOOL_VEC2: sz = 4*2
  562. case gl.BOOL_VEC3: sz = 4*3
  563. case gl.BOOL_VEC4: sz = 4*4
  564. case gl.INT: sz = 4*1
  565. case gl.INT_VEC2: sz = 4*2
  566. case gl.INT_VEC3: sz = 4*3
  567. case gl.INT_VEC4: sz = 4*4
  568. case gl.UNSIGNED_INT: sz = 4*1
  569. case gl.UNSIGNED_INT_VEC2: sz = 4*2
  570. case gl.UNSIGNED_INT_VEC3: sz = 4*3
  571. case gl.UNSIGNED_INT_VEC4: sz = 4*4
  572. case: log.errorf("Unhandled uniform type: %x", t)
  573. }
  574. return sz
  575. }
  576. gl_translate_pixel_format :: proc(f: Pixel_Format) -> gl.Enum {
  577. switch f {
  578. case .RGBA_32_Float: return gl.RGBA
  579. case .RGB_32_Float: return gl.RGB
  580. case .RG_32_Float: return gl.RG
  581. case .R_32_Float: return gl.RED
  582. // IS THIS STUFF CORRECT? Compare to GL backend
  583. // Do we need float textures? What is happening...
  584. case .RGBA_8_Norm: return gl.RGBA
  585. case .RG_8_Norm: return gl.RG
  586. case .R_8_Norm: return gl.RED
  587. case .R_8_UInt: return gl.RED
  588. case .Unknown: fallthrough
  589. case: log.error("Unhandled pixel format %v", f)
  590. }
  591. return 0
  592. }
  593. gl_describe_pixel_format :: proc(f: Pixel_Format) -> (format: gl.Enum, num_components: int, normalized: bool) {
  594. switch f {
  595. case .RGBA_32_Float: return gl.FLOAT, 4, false
  596. case .RGB_32_Float: return gl.FLOAT, 3, false
  597. case .RG_32_Float: return gl.FLOAT, 2, false
  598. case .R_32_Float: return gl.FLOAT, 1, false
  599. case .RGBA_8_Norm: return gl.UNSIGNED_BYTE, 4, true
  600. case .RG_8_Norm: return gl.UNSIGNED_BYTE, 2, true
  601. case .R_8_Norm: return gl.UNSIGNED_BYTE, 1, true
  602. case .R_8_UInt: return gl.BYTE, 1, false
  603. case .Unknown:
  604. }
  605. log.errorf("Unknown format %x", format)
  606. return 0, 0, false
  607. }
  608. webgl_destroy_shader :: proc(h: Shader_Handle) {
  609. shd := hm.get(&s.shaders, h)
  610. if shd == nil {
  611. log.errorf("Invalid shader: %v", h)
  612. return
  613. }
  614. delete(shd.constant_buffers, s.allocator)
  615. delete(shd.constants, s.allocator)
  616. delete(shd.texture_bindings, s.allocator)
  617. }
  618. webgl_default_shader_vertex_source :: proc() -> []byte {
  619. vertex_source := #load("render_backend_gl_default_vertex_shader.glsl")
  620. return vertex_source
  621. }
  622. webgl_default_shader_fragment_source :: proc() -> []byte {
  623. fragment_source := #load("render_backend_gl_default_fragment_shader.glsl")
  624. return fragment_source
  625. }