karl2d_windows.odin 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. #+build windows
  2. package karl2d
  3. import "base:runtime"
  4. import win32 "core:sys/windows"
  5. import d3d11 "vendor:directx/d3d11"
  6. import dxgi "vendor:directx/dxgi"
  7. import "vendor:directx/d3d_compiler"
  8. import "core:strings"
  9. import "core:log"
  10. import "core:math/linalg"
  11. import "core:slice"
  12. import "core:mem"
  13. _init :: proc(width: int, height: int, title: string,
  14. allocator := context.allocator, loc := #caller_location) -> ^State {
  15. s = new(State, allocator, loc)
  16. s.custom_context = context
  17. CLASS_NAME :: "karl2d"
  18. instance := win32.HINSTANCE(win32.GetModuleHandleW(nil))
  19. s.run = true
  20. s.width = width
  21. s.height = height
  22. cls := win32.WNDCLASSW {
  23. lpfnWndProc = window_proc,
  24. lpszClassName = CLASS_NAME,
  25. hInstance = instance,
  26. hCursor = win32.LoadCursorA(nil, win32.IDC_ARROW),
  27. }
  28. win32.RegisterClassW(&cls)
  29. r: win32.RECT
  30. r.right = i32(width)
  31. r.bottom = i32(height)
  32. style := win32.WS_OVERLAPPEDWINDOW | win32.WS_VISIBLE
  33. win32.AdjustWindowRect(&r, style, false)
  34. hwnd := win32.CreateWindowW(CLASS_NAME,
  35. win32.utf8_to_wstring(title),
  36. style,
  37. 100, 10, r.right - r.left, r.bottom - r.top,
  38. nil, nil, instance, nil)
  39. assert(hwnd != nil, "Failed creating window")
  40. feature_levels := [?]d3d11.FEATURE_LEVEL{
  41. ._11_1,
  42. ._11_0,
  43. }
  44. base_device: ^d3d11.IDevice
  45. base_device_context: ^d3d11.IDeviceContext
  46. device_flags := d3d11.CREATE_DEVICE_FLAGS {
  47. .BGRA_SUPPORT,
  48. }
  49. when ODIN_DEBUG {
  50. device_flags += { .DEBUG }
  51. }
  52. ch(d3d11.CreateDevice(
  53. nil,
  54. .HARDWARE,
  55. nil,
  56. device_flags,
  57. &feature_levels[0], len(feature_levels),
  58. d3d11.SDK_VERSION, &base_device, nil, &base_device_context))
  59. ch(base_device->QueryInterface(d3d11.IInfoQueue_UUID, (^rawptr)(&s.info_queue)))
  60. ch(base_device->QueryInterface(d3d11.IDevice_UUID, (^rawptr)(&s.device)))
  61. ch(base_device_context->QueryInterface(d3d11.IDeviceContext_UUID, (^rawptr)(&s.device_context)))
  62. dxgi_device: ^dxgi.IDevice
  63. ch(s.device->QueryInterface(dxgi.IDevice_UUID, (^rawptr)(&dxgi_device)))
  64. base_device->Release()
  65. base_device_context->Release()
  66. dxgi_adapter: ^dxgi.IAdapter
  67. ch(dxgi_device->GetAdapter(&dxgi_adapter))
  68. dxgi_device->Release()
  69. dxgi_factory: ^dxgi.IFactory2
  70. ch(dxgi_adapter->GetParent(dxgi.IFactory2_UUID, (^rawptr)(&dxgi_factory)))
  71. swapchain_desc := dxgi.SWAP_CHAIN_DESC1 {
  72. Format = .B8G8R8A8_UNORM,
  73. SampleDesc = {
  74. Count = 1,
  75. },
  76. BufferUsage = {.RENDER_TARGET_OUTPUT},
  77. BufferCount = 2,
  78. Scaling = .STRETCH,
  79. SwapEffect = .DISCARD,
  80. }
  81. ch(dxgi_factory->CreateSwapChainForHwnd(s.device, hwnd, &swapchain_desc, nil, nil, &s.swapchain))
  82. ch(s.swapchain->GetBuffer(0, d3d11.ITexture2D_UUID, (^rawptr)(&s.framebuffer)))
  83. ch(s.device->CreateRenderTargetView(s.framebuffer, nil, &s.framebuffer_view))
  84. depth_buffer_desc: d3d11.TEXTURE2D_DESC
  85. s.framebuffer->GetDesc(&depth_buffer_desc)
  86. depth_buffer_desc.Format = .D24_UNORM_S8_UINT
  87. depth_buffer_desc.BindFlags = {.DEPTH_STENCIL}
  88. ch(s.device->CreateTexture2D(&depth_buffer_desc, nil, &s.depth_buffer))
  89. ch(s.device->CreateDepthStencilView(s.depth_buffer, nil, &s.depth_buffer_view))
  90. vs_blob: ^d3d11.IBlob
  91. vs_blob_errors: ^d3d11.IBlob
  92. ch(d3d_compiler.Compile(raw_data(shader_hlsl), len(shader_hlsl), "shader.hlsl", nil, nil, "vs_main", "vs_5_0", 0, 0, &vs_blob, &vs_blob_errors))
  93. if vs_blob_errors != nil {
  94. log.error("Failed compiling shader:")
  95. log.error(strings.string_from_ptr((^u8)(vs_blob_errors->GetBufferPointer()), int(vs_blob_errors->GetBufferSize())))
  96. }
  97. ch(s.device->CreateVertexShader(vs_blob->GetBufferPointer(), vs_blob->GetBufferSize(), nil, &s.vertex_shader))
  98. input_element_desc := [?]d3d11.INPUT_ELEMENT_DESC{
  99. { "POS", 0, .R32G32_FLOAT, 0, 0, .VERTEX_DATA, 0 },
  100. { "COL", 0, .R8G8B8A8_UNORM , 0, d3d11.APPEND_ALIGNED_ELEMENT, .VERTEX_DATA, 0 },
  101. }
  102. ch(s.device->CreateInputLayout(&input_element_desc[0], len(input_element_desc), vs_blob->GetBufferPointer(), vs_blob->GetBufferSize(), &s.input_layout))
  103. ps_blob: ^d3d11.IBlob
  104. ps_blob_errors: ^d3d11.IBlob
  105. ch(d3d_compiler.Compile(raw_data(shader_hlsl), len(shader_hlsl), "shader.hlsl", nil, nil, "ps_main", "ps_5_0", 0, 0, &ps_blob, &ps_blob_errors))
  106. if ps_blob_errors != nil {
  107. log.error("Failed compiling shader:")
  108. log.error(strings.string_from_ptr((^u8)(ps_blob_errors->GetBufferPointer()), int(ps_blob_errors->GetBufferSize())))
  109. }
  110. ch(s.device->CreatePixelShader(ps_blob->GetBufferPointer(), ps_blob->GetBufferSize(), nil, &s.pixel_shader))
  111. rasterizer_desc := d3d11.RASTERIZER_DESC{
  112. FillMode = .SOLID,
  113. CullMode = .BACK,
  114. }
  115. ch(s.device->CreateRasterizerState(&rasterizer_desc, &s.rasterizer_state))
  116. depth_stencil_desc := d3d11.DEPTH_STENCIL_DESC{
  117. DepthEnable = false,
  118. DepthWriteMask = .ALL,
  119. DepthFunc = .LESS,
  120. }
  121. ch(s.device->CreateDepthStencilState(&depth_stencil_desc, &s.depth_stencil_state))
  122. constant_buffer_desc := d3d11.BUFFER_DESC{
  123. ByteWidth = size_of(Constants),
  124. Usage = .DYNAMIC,
  125. BindFlags = {.CONSTANT_BUFFER},
  126. CPUAccessFlags = {.WRITE},
  127. }
  128. ch(s.device->CreateBuffer(&constant_buffer_desc, nil, &s.constant_buffer))
  129. vertex_buffer_desc := d3d11.BUFFER_DESC{
  130. ByteWidth = VERTEX_BUFFER_MAX * size_of(Vertex),
  131. Usage = .DYNAMIC,
  132. BindFlags = {.VERTEX_BUFFER},
  133. CPUAccessFlags = {.WRITE},
  134. }
  135. ch(s.device->CreateBuffer(&vertex_buffer_desc, nil, &s.vertex_buffer_gpu))
  136. s.vertex_buffer_cpu = make([]Vertex, VERTEX_BUFFER_MAX)
  137. blend_desc := d3d11.BLEND_DESC {
  138. RenderTarget = {
  139. 0 = {
  140. BlendEnable = true,
  141. SrcBlend = .SRC_ALPHA,
  142. DestBlend = .INV_SRC_ALPHA,
  143. BlendOp = .ADD,
  144. SrcBlendAlpha = .ONE,
  145. DestBlendAlpha = .ZERO,
  146. BlendOpAlpha = .ADD,
  147. RenderTargetWriteMask = u8(d3d11.COLOR_WRITE_ENABLE_ALL),
  148. },
  149. },
  150. }
  151. ch(s.device->CreateBlendState(&blend_desc, &s.blend_state))
  152. s.proj_matrix = make_default_projection(s.width, s.height)
  153. return s
  154. }
  155. shader_hlsl :: #load("shader.hlsl")
  156. Vertex :: struct {
  157. pos: Vec2,
  158. color: Color,
  159. }
  160. s: ^State
  161. VERTEX_BUFFER_MAX :: 10000
  162. State :: struct {
  163. swapchain: ^dxgi.ISwapChain1,
  164. framebuffer_view: ^d3d11.IRenderTargetView,
  165. depth_buffer_view: ^d3d11.IDepthStencilView,
  166. device_context: ^d3d11.IDeviceContext,
  167. constant_buffer: ^d3d11.IBuffer,
  168. vertex_shader: ^d3d11.IVertexShader,
  169. pixel_shader: ^d3d11.IPixelShader,
  170. depth_stencil_state: ^d3d11.IDepthStencilState,
  171. rasterizer_state: ^d3d11.IRasterizerState,
  172. input_layout: ^d3d11.IInputLayout,
  173. device: ^d3d11.IDevice,
  174. depth_buffer: ^d3d11.ITexture2D,
  175. framebuffer: ^d3d11.ITexture2D,
  176. blend_state: ^d3d11.IBlendState,
  177. info_queue: ^d3d11.IInfoQueue,
  178. vertex_buffer_gpu: ^d3d11.IBuffer,
  179. vertex_buffer_cpu: []Vertex,
  180. vertex_buffer_cpu_count: int,
  181. run: bool,
  182. custom_context: runtime.Context,
  183. width: int,
  184. height: int,
  185. keys_went_down: #sparse [Keyboard_Key]bool,
  186. keys_went_up: #sparse [Keyboard_Key]bool,
  187. keys_is_held: #sparse [Keyboard_Key]bool,
  188. proj_matrix: matrix[4,4]f32,
  189. }
  190. VK_MAP := [255]Keyboard_Key {
  191. win32.VK_A = .A,
  192. win32.VK_B = .B,
  193. win32.VK_C = .C,
  194. win32.VK_D = .D,
  195. win32.VK_E = .E,
  196. win32.VK_F = .F,
  197. win32.VK_G = .G,
  198. win32.VK_H = .H,
  199. win32.VK_I = .I,
  200. win32.VK_J = .J,
  201. win32.VK_K = .K,
  202. win32.VK_L = .L,
  203. win32.VK_M = .M,
  204. win32.VK_N = .N,
  205. win32.VK_O = .O,
  206. win32.VK_P = .P,
  207. win32.VK_Q = .Q,
  208. win32.VK_R = .R,
  209. win32.VK_S = .S,
  210. win32.VK_T = .T,
  211. win32.VK_U = .U,
  212. win32.VK_V = .V,
  213. win32.VK_W = .W,
  214. win32.VK_X = .X,
  215. win32.VK_Y = .Y,
  216. win32.VK_Z = .Z,
  217. win32.VK_LEFT = .Left,
  218. win32.VK_RIGHT = .Right,
  219. win32.VK_UP = .Up,
  220. win32.VK_DOWN = .Down,
  221. }
  222. window_proc :: proc "stdcall" (hwnd: win32.HWND, msg: win32.UINT, wparam: win32.WPARAM, lparam: win32.LPARAM) -> win32.LRESULT {
  223. context = s.custom_context
  224. switch msg {
  225. case win32.WM_DESTROY:
  226. win32.PostQuitMessage(0)
  227. s.run = false
  228. case win32.WM_CLOSE:
  229. s.run = false
  230. case win32.WM_KEYDOWN:
  231. key := VK_MAP[wparam]
  232. s.keys_went_down[key] = true
  233. s.keys_is_held[key] = true
  234. case win32.WM_KEYUP:
  235. key := VK_MAP[wparam]
  236. s.keys_is_held[key] = false
  237. s.keys_went_up[key] = true
  238. }
  239. return win32.DefWindowProcW(hwnd, msg, wparam, lparam)
  240. }
  241. _shutdown :: proc() {
  242. s.framebuffer_view->Release()
  243. s.depth_buffer_view->Release()
  244. s.depth_buffer->Release()
  245. s.framebuffer->Release()
  246. s.device_context->Release()
  247. s.vertex_buffer_gpu->Release()
  248. s.constant_buffer->Release()
  249. s.vertex_shader->Release()
  250. s.pixel_shader->Release()
  251. s.depth_stencil_state->Release()
  252. s.rasterizer_state->Release()
  253. s.input_layout->Release()
  254. s.swapchain->Release()
  255. when ODIN_DEBUG {
  256. debug: ^d3d11.IDebug
  257. if ch(s.device->QueryInterface(d3d11.IDebug_UUID, (^rawptr)(&debug))) >= 0 {
  258. ch(debug->ReportLiveDeviceObjects({.DETAIL, .IGNORE_INTERNAL}))
  259. log_messages()
  260. }
  261. debug->Release()
  262. }
  263. s.device->Release()
  264. s.info_queue->Release()
  265. }
  266. _set_internal_state :: proc(new_state: ^State) {
  267. s = new_state
  268. }
  269. Color_F32 :: [4]f32
  270. f32_color_from_color :: proc(color: Color) -> Color_F32 {
  271. return {
  272. f32(color.r) / 255,
  273. f32(color.g) / 255,
  274. f32(color.b) / 255,
  275. f32(color.a) / 255,
  276. }
  277. }
  278. _clear :: proc(color: Color) {
  279. c := f32_color_from_color(color)
  280. s.device_context->ClearRenderTargetView(s.framebuffer_view, &c)
  281. s.device_context->ClearDepthStencilView(s.depth_buffer_view, {.DEPTH}, 1, 0)
  282. }
  283. _load_texture :: proc(filename: string) -> Texture {
  284. return {}
  285. }
  286. _destroy_texture :: proc(tex: Texture) {
  287. }
  288. _draw_texture :: proc(tex: Texture, pos: Vec2, tint := WHITE) {
  289. _draw_texture_ex(
  290. tex,
  291. {0, 0, f32(tex.width), f32(tex.height)},
  292. {pos.x, pos.y, f32(tex.width), f32(tex.height)},
  293. {},
  294. 0,
  295. tint,
  296. )
  297. }
  298. _draw_texture_rect :: proc(tex: Texture, rect: Rect, pos: Vec2, tint := WHITE) {
  299. _draw_texture_ex(
  300. tex,
  301. rect,
  302. {pos.x, pos.y, rect.w, rect.h},
  303. {},
  304. 0,
  305. tint,
  306. )
  307. }
  308. add_vertex :: proc(v: Vec2, color: Color) {
  309. if s.vertex_buffer_cpu_count == len(s.vertex_buffer_cpu) {
  310. panic("Must dispatch here")
  311. }
  312. s.vertex_buffer_cpu[s.vertex_buffer_cpu_count] = {
  313. pos = v,
  314. color = color,
  315. }
  316. s.vertex_buffer_cpu_count += 1
  317. }
  318. _draw_texture_ex :: proc(tex: Texture, src: Rect, dst: Rect, origin: Vec2, rot: f32, tint := WHITE) {
  319. p := Vec2 {
  320. dst.x, dst.y,
  321. }
  322. p -= origin
  323. add_vertex({p.x, p.y}, tint)
  324. add_vertex({p.x + dst.w, p.y}, tint)
  325. add_vertex({p.x + dst.w, p.y + dst.h}, tint)
  326. add_vertex({p.x, p.y}, tint)
  327. add_vertex({p.x + dst.w, p.y + dst.h}, tint)
  328. add_vertex({p.x, p.y + dst.h}, tint)
  329. }
  330. _draw_rectangle :: proc(r: Rect, color: Color) {
  331. add_vertex({r.x, r.y}, color)
  332. add_vertex({r.x + r.w, r.y}, color)
  333. add_vertex({r.x + r.w, r.y + r.h}, color)
  334. add_vertex({r.x, r.y}, color)
  335. add_vertex({r.x + r.w, r.y + r.h}, color)
  336. add_vertex({r.x, r.y + r.h}, color)
  337. }
  338. _draw_rectangle_outline :: proc(r: Rect, thickness: f32, color: Color) {
  339. t := thickness
  340. // Based on DrawRectangleLinesEx from Raylib
  341. top := Rect {
  342. r.x,
  343. r.y,
  344. r.w,
  345. t,
  346. }
  347. bottom := Rect {
  348. r.x,
  349. r.y + r.h - t,
  350. r.w,
  351. t,
  352. }
  353. left := Rect {
  354. r.x,
  355. r.y + t,
  356. t,
  357. r.h - t * 2,
  358. }
  359. right := Rect {
  360. r.x + r.w - t,
  361. r.y + t,
  362. t,
  363. r.h - t * 2,
  364. }
  365. _draw_rectangle(top, color)
  366. _draw_rectangle(bottom, color)
  367. _draw_rectangle(left, color)
  368. _draw_rectangle(right, color)
  369. }
  370. _draw_circle :: proc(center: Vec2, radius: f32, color: Color) {
  371. }
  372. _draw_line :: proc(start: Vec2, end: Vec2, thickness: f32, color: Color) {
  373. }
  374. _get_screen_width :: proc() -> int {
  375. return 0
  376. }
  377. _get_screen_height :: proc() -> int {
  378. return 0
  379. }
  380. _key_went_down :: proc(key: Keyboard_Key) -> bool {
  381. return s.keys_went_down[key]
  382. }
  383. _key_went_up :: proc(key: Keyboard_Key) -> bool {
  384. return s.keys_went_up[key]
  385. }
  386. _key_is_held :: proc(key: Keyboard_Key) -> bool {
  387. return s.keys_is_held[key]
  388. }
  389. _window_should_close :: proc() -> bool {
  390. return !s.run
  391. }
  392. _draw_text :: proc(text: string, pos: Vec2, font_size: f32, color: Color) {
  393. }
  394. _mouse_button_pressed :: proc(button: Mouse_Button) -> bool {
  395. return false
  396. }
  397. _mouse_button_released :: proc(button: Mouse_Button) -> bool {
  398. return false
  399. }
  400. _mouse_button_held :: proc(button: Mouse_Button) -> bool {
  401. return false
  402. }
  403. _mouse_wheel_delta :: proc() -> f32 {
  404. return 0
  405. }
  406. _mouse_position :: proc() -> Vec2 {
  407. return {}
  408. }
  409. _enable_scissor :: proc(x, y, w, h: int) {
  410. }
  411. _disable_scissor :: proc() {
  412. }
  413. _set_window_size :: proc(width: int, height: int) {
  414. }
  415. _set_window_position :: proc(x: int, y: int) {
  416. }
  417. _screen_to_world :: proc(pos: Vec2, camera: Camera) -> Vec2 {
  418. return pos
  419. }
  420. _set_camera :: proc(camera: Maybe(Camera)) {
  421. if c, c_ok := camera.?; c_ok {
  422. s.proj_matrix = make_default_projection(s.width, s.height)
  423. s.proj_matrix[0, 0] *= c.zoom
  424. s.proj_matrix[1, 1] *= c.zoom
  425. } else {
  426. s.proj_matrix = make_default_projection(s.width, s.height)
  427. }
  428. }
  429. _set_scissor_rect :: proc(scissor_rect: Maybe(Rect)) {
  430. }
  431. _set_shader :: proc(shader: Maybe(Shader)) {
  432. }
  433. _process_events :: proc() {
  434. s.keys_went_up = {}
  435. s.keys_went_down = {}
  436. msg: win32.MSG
  437. for win32.PeekMessageW(&msg, nil, 0, 0, win32.PM_REMOVE) {
  438. win32.TranslateMessage(&msg)
  439. win32.DispatchMessageW(&msg)
  440. }
  441. }
  442. _flush :: proc() {
  443. }
  444. Constants :: struct #align (16) {
  445. projection: matrix[4, 4]f32,
  446. }
  447. make_default_projection :: proc(w, h: int) -> matrix[4,4]f32 {
  448. return linalg.matrix_ortho3d_f32(0, f32(w), f32(h), 0, 0.001, 2)
  449. }
  450. _present :: proc(do_flush := true) {
  451. viewport := d3d11.VIEWPORT{
  452. 0, 0,
  453. f32(s.width), f32(s.height),
  454. 0, 1,
  455. }
  456. dc := s.device_context
  457. vb_data: d3d11.MAPPED_SUBRESOURCE
  458. ch(dc->Map(s.vertex_buffer_gpu, 0, .WRITE_NO_OVERWRITE, {}, &vb_data))
  459. {
  460. gpu_map := slice.from_ptr((^Vertex)(vb_data.pData), VERTEX_BUFFER_MAX)
  461. copy(gpu_map, s.vertex_buffer_cpu[:s.vertex_buffer_cpu_count])
  462. }
  463. dc->Unmap(s.vertex_buffer_gpu, 0)
  464. cb_data: d3d11.MAPPED_SUBRESOURCE
  465. ch(dc->Map(s.constant_buffer, 0, .WRITE_DISCARD, {}, &cb_data))
  466. {
  467. constants := (^Constants)(cb_data.pData)
  468. constants.projection = s.proj_matrix
  469. }
  470. dc->Unmap(s.constant_buffer, 0)
  471. dc->IASetPrimitiveTopology(.TRIANGLELIST)
  472. dc->IASetInputLayout(s.input_layout)
  473. vertex_buffer_offset := u32(0)
  474. vertex_buffer_stride := u32(size_of(Vertex))
  475. dc->IASetVertexBuffers(0, 1, &s.vertex_buffer_gpu, &vertex_buffer_stride, &vertex_buffer_offset)
  476. dc->VSSetShader(s.vertex_shader, nil, 0)
  477. dc->VSSetConstantBuffers(0, 1, &s.constant_buffer)
  478. dc->RSSetViewports(1, &viewport)
  479. dc->RSSetState(s.rasterizer_state)
  480. dc->PSSetShader(s.pixel_shader, nil, 0)
  481. dc->OMSetRenderTargets(1, &s.framebuffer_view, s.depth_buffer_view)
  482. dc->OMSetDepthStencilState(s.depth_stencil_state, 0)
  483. dc->OMSetBlendState(s.blend_state, nil, ~u32(0))
  484. dc->Draw(u32(s.vertex_buffer_cpu_count), 0)
  485. log_messages()
  486. ch(s.swapchain->Present(1, {}))
  487. s.vertex_buffer_cpu_count = 0
  488. }
  489. _load_shader :: proc(vs: string, fs: string) -> Shader {
  490. return {}
  491. }
  492. _destroy_shader :: proc(shader: Shader) {
  493. }
  494. _get_shader_location :: proc(shader: Shader, uniform_name: string) -> int {
  495. return 0
  496. }
  497. _set_shader_value_f32 :: proc(shader: Shader, loc: int, val: f32) {
  498. }
  499. _set_shader_value_vec2 :: proc(shader: Shader, loc: int, val: Vec2) {
  500. }
  501. temp_cstring :: proc(str: string, loc := #caller_location) -> cstring {
  502. return strings.clone_to_cstring(str, context.temp_allocator)
  503. }
  504. // CHeck win errors and print message log if there is any error
  505. ch :: proc(hr: win32.HRESULT, loc := #caller_location) -> win32.HRESULT {
  506. if hr >= 0 {
  507. return hr
  508. }
  509. log.errorf("d3d11 error: %0x", u32(hr), location = loc)
  510. log_messages(loc)
  511. return hr
  512. }
  513. log_messages :: proc(loc := #caller_location) {
  514. iq := s.info_queue
  515. if iq == nil {
  516. return
  517. }
  518. n := iq->GetNumStoredMessages()
  519. longest_msg: d3d11.SIZE_T
  520. for i in 0..=n {
  521. msglen: d3d11.SIZE_T
  522. iq->GetMessage(i, nil, &msglen)
  523. if msglen > longest_msg {
  524. longest_msg = msglen
  525. }
  526. }
  527. if longest_msg > 0 {
  528. msg_raw_ptr, _ := (mem.alloc(int(longest_msg), allocator = context.temp_allocator))
  529. for i in 0..=n {
  530. msglen: d3d11.SIZE_T
  531. iq->GetMessage(i, nil, &msglen)
  532. if msglen > 0 {
  533. msg := (^d3d11.MESSAGE)(msg_raw_ptr)
  534. iq->GetMessage(i, msg, &msglen)
  535. log.error(msg.pDescription, location = loc)
  536. }
  537. }
  538. }
  539. iq->ClearStoredMessages()
  540. }