render_backend_webgl.odin 20 KB

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