karl2d_windows.odin 16 KB

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