render_backend_d3d11.odin 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. #+build windows
  2. #+vet explicit-allocators
  3. #+private file
  4. package karl2d
  5. @(private="package")
  6. RENDER_BACKEND_INTERFACE_D3D11 :: Render_Backend_Interface {
  7. state_size = d3d11_state_size,
  8. init = d3d11_init,
  9. shutdown = d3d11_shutdown,
  10. clear = d3d11_clear,
  11. present = d3d11_present,
  12. draw = d3d11_draw,
  13. resize_swapchain = d3d11_resize_swapchain,
  14. get_swapchain_width = d3d11_get_swapchain_width,
  15. get_swapchain_height = d3d11_get_swapchain_height,
  16. flip_z = d3d11_flip_z,
  17. set_internal_state = d3d11_set_internal_state,
  18. create_texture = d3d11_create_texture,
  19. load_texture = d3d11_load_texture,
  20. update_texture = d3d11_update_texture,
  21. destroy_texture = d3d11_destroy_texture,
  22. create_render_texture = d3d11_create_render_texture,
  23. set_texture_filter = d3d11_set_texture_filter,
  24. load_shader = d3d11_load_shader,
  25. destroy_shader = d3d11_destroy_shader,
  26. default_shader_vertex_source = d3d11_default_shader_vertex_source,
  27. default_shader_fragment_source = d3d11_default_shader_fragment_source,
  28. }
  29. import d3d11 "vendor:directx/d3d11"
  30. import dxgi "vendor:directx/dxgi"
  31. import "vendor:directx/d3d_compiler"
  32. import "core:strings"
  33. import "core:log"
  34. import "core:slice"
  35. import "core:mem"
  36. import hm "handle_map"
  37. import "base:runtime"
  38. d3d11_state_size :: proc() -> int {
  39. return size_of(D3D11_State)
  40. }
  41. d3d11_init :: proc(state: rawptr, window_handle: Window_Handle, swapchain_width, swapchain_height: int, allocator := context.allocator) {
  42. s = (^D3D11_State)(state)
  43. s.allocator = allocator
  44. s.window_handle = dxgi.HWND(window_handle)
  45. s.width = swapchain_width
  46. s.height = swapchain_height
  47. feature_levels := [?]d3d11.FEATURE_LEVEL{
  48. ._11_1,
  49. ._11_0,
  50. }
  51. base_device: ^d3d11.IDevice
  52. base_device_context: ^d3d11.IDeviceContext
  53. base_device_flags := d3d11.CREATE_DEVICE_FLAGS {
  54. .BGRA_SUPPORT,
  55. }
  56. when ODIN_DEBUG {
  57. device_flags := base_device_flags + { .DEBUG }
  58. device_err := ch(d3d11.CreateDevice(
  59. nil,
  60. .HARDWARE,
  61. nil,
  62. device_flags,
  63. &feature_levels[0], len(feature_levels),
  64. d3d11.SDK_VERSION, &base_device, nil, &base_device_context))
  65. if u32(device_err) == 0x887a002d {
  66. log.error("You're running in debug mode. So we are trying to create a debug D3D11 device. But you don't have DirectX SDK installed, so we can't enable debug layers. Creating a device without debug layers (you'll get no good D3D11 errors).")
  67. ch(d3d11.CreateDevice(
  68. nil,
  69. .HARDWARE,
  70. nil,
  71. base_device_flags,
  72. &feature_levels[0], len(feature_levels),
  73. d3d11.SDK_VERSION, &base_device, nil, &base_device_context))
  74. } else {
  75. ch(base_device->QueryInterface(d3d11.IInfoQueue_UUID, (^rawptr)(&s.info_queue)))
  76. }
  77. } else {
  78. ch(d3d11.CreateDevice(
  79. nil,
  80. .HARDWARE,
  81. nil,
  82. base_device_flags,
  83. &feature_levels[0], len(feature_levels),
  84. d3d11.SDK_VERSION, &base_device, nil, &base_device_context))
  85. }
  86. ch(base_device->QueryInterface(d3d11.IDevice_UUID, (^rawptr)(&s.device)))
  87. ch(base_device_context->QueryInterface(d3d11.IDeviceContext_UUID, (^rawptr)(&s.device_context)))
  88. dxgi_device: ^dxgi.IDevice
  89. ch(s.device->QueryInterface(dxgi.IDevice_UUID, (^rawptr)(&dxgi_device)))
  90. base_device->Release()
  91. base_device_context->Release()
  92. ch(dxgi_device->GetAdapter(&s.dxgi_adapter))
  93. create_swapchain(swapchain_width, swapchain_height)
  94. rasterizer_desc := d3d11.RASTERIZER_DESC{
  95. FillMode = .SOLID,
  96. CullMode = .BACK,
  97. ScissorEnable = true,
  98. }
  99. ch(s.device->CreateRasterizerState(&rasterizer_desc, &s.rasterizer_state))
  100. depth_stencil_desc := d3d11.DEPTH_STENCIL_DESC{
  101. DepthEnable = true,
  102. DepthWriteMask = .ALL,
  103. DepthFunc = .LESS,
  104. }
  105. ch(s.device->CreateDepthStencilState(&depth_stencil_desc, &s.depth_stencil_state))
  106. vertex_buffer_desc := d3d11.BUFFER_DESC{
  107. ByteWidth = VERTEX_BUFFER_MAX,
  108. Usage = .DYNAMIC,
  109. BindFlags = {.VERTEX_BUFFER},
  110. CPUAccessFlags = {.WRITE},
  111. }
  112. ch(s.device->CreateBuffer(&vertex_buffer_desc, nil, &s.vertex_buffer_gpu))
  113. blend_desc := d3d11.BLEND_DESC {
  114. RenderTarget = {
  115. 0 = {
  116. BlendEnable = true,
  117. SrcBlend = .SRC_ALPHA,
  118. DestBlend = .INV_SRC_ALPHA,
  119. BlendOp = .ADD,
  120. SrcBlendAlpha = .ONE,
  121. DestBlendAlpha = .ZERO,
  122. BlendOpAlpha = .ADD,
  123. RenderTargetWriteMask = u8(d3d11.COLOR_WRITE_ENABLE_ALL),
  124. },
  125. },
  126. }
  127. ch(s.device->CreateBlendState(&blend_desc, &s.blend_state))
  128. }
  129. d3d11_shutdown :: proc() {
  130. s.framebuffer_view->Release()
  131. s.depth_buffer_view->Release()
  132. s.depth_buffer->Release()
  133. s.framebuffer->Release()
  134. s.device_context->Release()
  135. s.vertex_buffer_gpu->Release()
  136. s.depth_stencil_state->Release()
  137. s.rasterizer_state->Release()
  138. s.swapchain->Release()
  139. s.blend_state->Release()
  140. s.dxgi_adapter->Release()
  141. when ODIN_DEBUG {
  142. debug: ^d3d11.IDebug
  143. if ch(s.device->QueryInterface(d3d11.IDebug_UUID, (^rawptr)(&debug))) >= 0 {
  144. ch(debug->ReportLiveDeviceObjects({.DETAIL, .IGNORE_INTERNAL}))
  145. log_messages()
  146. }
  147. debug->Release()
  148. }
  149. s.device->Release()
  150. s.info_queue->Release()
  151. }
  152. d3d11_clear :: proc(render_target: Render_Target_Handle, color: Color) {
  153. c := f32_color_from_color(color)
  154. if rt := hm.get(&s.render_targets, render_target); rt != nil {
  155. s.device_context->ClearRenderTargetView(rt.render_target_view, &c)
  156. s.device_context->ClearDepthStencilView(rt.depth_stencil_texture_view, {.DEPTH}, 1, 0)
  157. } else {
  158. s.device_context->ClearRenderTargetView(s.framebuffer_view, &c)
  159. s.device_context->ClearDepthStencilView(s.depth_buffer_view, {.DEPTH}, 1, 0)
  160. }
  161. }
  162. d3d11_present :: proc() {
  163. ch(s.swapchain->Present(1, {}))
  164. }
  165. d3d11_draw :: proc(
  166. shd: Shader,
  167. render_target: Render_Target_Handle,
  168. bound_textures: []Texture_Handle,
  169. scissor: Maybe(Rect),
  170. vertex_buffer: []u8,
  171. ) {
  172. if len(vertex_buffer) == 0 {
  173. return
  174. }
  175. d3d_shd := hm.get(&s.shaders, shd.handle)
  176. if d3d_shd == nil {
  177. log.error("Trying to draw with invalid shader %v", shd.handle)
  178. return
  179. }
  180. dc := s.device_context
  181. vb_data: d3d11.MAPPED_SUBRESOURCE
  182. ch(dc->Map(s.vertex_buffer_gpu, 0, .WRITE_DISCARD, {}, &vb_data))
  183. {
  184. gpu_map := slice.from_ptr((^u8)(vb_data.pData), VERTEX_BUFFER_MAX)
  185. copy(
  186. gpu_map,
  187. vertex_buffer,
  188. )
  189. }
  190. dc->Unmap(s.vertex_buffer_gpu, 0)
  191. dc->IASetPrimitiveTopology(.TRIANGLELIST)
  192. dc->IASetInputLayout(d3d_shd.input_layout)
  193. vertex_buffer_offset: u32
  194. vertex_buffer_stride := u32(shd.vertex_size)
  195. dc->IASetVertexBuffers(0, 1, &s.vertex_buffer_gpu, &vertex_buffer_stride, &vertex_buffer_offset)
  196. dc->VSSetShader(d3d_shd.vertex_shader, nil, 0)
  197. assert(len(shd.constants) == len(d3d_shd.constants))
  198. maps := make([]rawptr, len(d3d_shd.constant_buffers), frame_allocator)
  199. cpu_data := shd.constants_data
  200. for cidx in 0..<len(shd.constants) {
  201. cpu_loc := shd.constants[cidx]
  202. gpu_loc := d3d_shd.constants[cidx]//cpu_loc.gpu_constant_idx]
  203. gpu_buffer_info := d3d_shd.constant_buffers[gpu_loc.buffer_idx]
  204. gpu_data := gpu_buffer_info.gpu_data
  205. if gpu_data == nil {
  206. continue
  207. }
  208. if maps[gpu_loc.buffer_idx] == nil {
  209. // We do this little dance with the 'maps' array so we only have to map the memory once.
  210. // There can be multiple constants within a single constant buffer. So mapping and
  211. // unmapping for each one is slow.
  212. map_data: d3d11.MAPPED_SUBRESOURCE
  213. ch(dc->Map(gpu_data, 0, .WRITE_DISCARD, {}, &map_data))
  214. maps[gpu_loc.buffer_idx] = map_data.pData
  215. }
  216. data_slice := slice.bytes_from_ptr(maps[gpu_loc.buffer_idx], gpu_buffer_info.size)
  217. dst := data_slice[gpu_loc.offset:gpu_loc.offset+u32(cpu_loc.size)]
  218. src := cpu_data[cpu_loc.offset:cpu_loc.offset+cpu_loc.size]
  219. copy(dst, src)
  220. }
  221. for &cb, cb_idx in d3d_shd.constant_buffers {
  222. if .Vertex in cb.bound_shaders {
  223. dc->VSSetConstantBuffers(cb.bind_point, 1, &cb.gpu_data)
  224. }
  225. if .Pixel in cb.bound_shaders {
  226. dc->PSSetConstantBuffers(cb.bind_point, 1, &cb.gpu_data)
  227. }
  228. if maps[cb_idx] != nil {
  229. dc->Unmap(cb.gpu_data, 0)
  230. maps[cb_idx] = nil
  231. }
  232. }
  233. viewport := d3d11.VIEWPORT{
  234. 0, 0,
  235. f32(s.width), f32(s.height),
  236. 0, 1,
  237. }
  238. dc->RSSetViewports(1, &viewport)
  239. dc->RSSetState(s.rasterizer_state)
  240. scissor_rect := d3d11.RECT {
  241. right = i32(s.width),
  242. bottom = i32(s.height),
  243. }
  244. if sciss, sciss_ok := scissor.?; sciss_ok {
  245. scissor_rect = d3d11.RECT {
  246. left = i32(sciss.x),
  247. top = i32(sciss.y),
  248. right = i32(sciss.x + sciss.w),
  249. bottom = i32(sciss.y + sciss.h),
  250. }
  251. }
  252. dc->RSSetScissorRects(1, &scissor_rect)
  253. dc->PSSetShader(d3d_shd.pixel_shader, nil, 0)
  254. if len(bound_textures) == len(d3d_shd.texture_bindings) {
  255. for t, t_idx in bound_textures {
  256. d3d_t := d3d_shd.texture_bindings[t_idx]
  257. if t := hm.get(&s.textures, t); t != nil {
  258. dc->PSSetShaderResources(d3d_t.bind_point, 1, &t.view)
  259. dc->PSSetSamplers(d3d_t.sampler_bind_point, 1, &t.sampler)
  260. }
  261. }
  262. }
  263. if rt := hm.get(&s.render_targets, render_target); rt != nil {
  264. dc->OMSetRenderTargets(1, &rt.render_target_view, rt.depth_stencil_texture_view)
  265. } else {
  266. dc->OMSetRenderTargets(1, &s.framebuffer_view, s.depth_buffer_view)
  267. }
  268. dc->OMSetDepthStencilState(s.depth_stencil_state, 0)
  269. dc->OMSetBlendState(s.blend_state, nil, ~u32(0))
  270. dc->Draw(u32(len(vertex_buffer)/shd.vertex_size), 0)
  271. dc->OMSetRenderTargets(0, nil, nil)
  272. log_messages()
  273. }
  274. d3d11_resize_swapchain :: proc(w, h: int) {
  275. s.depth_buffer->Release()
  276. s.depth_buffer_view->Release()
  277. s.framebuffer->Release()
  278. s.framebuffer_view->Release()
  279. s.swapchain->Release()
  280. s.width = w
  281. s.height = h
  282. create_swapchain(w, h)
  283. }
  284. d3d11_get_swapchain_width :: proc() -> int {
  285. return s.width
  286. }
  287. d3d11_get_swapchain_height :: proc() -> int {
  288. return s.height
  289. }
  290. d3d11_flip_z :: proc() -> bool {
  291. return true
  292. }
  293. d3d11_set_internal_state :: proc(state: rawptr) {
  294. s = (^D3D11_State)(state)
  295. }
  296. create_texture :: proc(
  297. width: int,
  298. height: int,
  299. format: Pixel_Format,
  300. data: rawptr,
  301. ) -> (
  302. Texture_Handle,
  303. ) {
  304. texture_desc := d3d11.TEXTURE2D_DESC{
  305. Width = u32(width),
  306. Height = u32(height),
  307. MipLevels = 1,
  308. ArraySize = 1,
  309. Format = dxgi_format_from_pixel_format(format),
  310. SampleDesc = {Count = 1},
  311. Usage = .DEFAULT,
  312. BindFlags = {.SHADER_RESOURCE},
  313. }
  314. texture: ^d3d11.ITexture2D
  315. if data != nil {
  316. texture_data := d3d11.SUBRESOURCE_DATA{
  317. pSysMem = data,
  318. SysMemPitch = u32(width * pixel_format_size(format)),
  319. }
  320. s.device->CreateTexture2D(&texture_desc, &texture_data, &texture)
  321. } else {
  322. s.device->CreateTexture2D(&texture_desc, nil, &texture)
  323. }
  324. texture_view: ^d3d11.IShaderResourceView
  325. s.device->CreateShaderResourceView(texture, nil, &texture_view)
  326. tex := D3D11_Texture {
  327. tex = texture,
  328. format = format,
  329. view = texture_view,
  330. sampler = create_sampler(.MIN_MAG_MIP_POINT),
  331. }
  332. return hm.add(&s.textures, tex)
  333. }
  334. d3d11_create_texture :: proc(width: int, height: int, format: Pixel_Format) -> Texture_Handle {
  335. return create_texture(width, height, format, nil)
  336. }
  337. d3d11_create_render_texture :: proc(width: int, height: int) -> (Texture_Handle, Render_Target_Handle) {
  338. texture_desc := d3d11.TEXTURE2D_DESC{
  339. Width = u32(width),
  340. Height = u32(height),
  341. MipLevels = 1,
  342. ArraySize = 1,
  343. Format = dxgi_format_from_pixel_format(.RGBA_32_Float),
  344. SampleDesc = {Count = 1},
  345. Usage = .DEFAULT,
  346. BindFlags = {.SHADER_RESOURCE, .RENDER_TARGET},
  347. }
  348. texture: ^d3d11.ITexture2D
  349. ch(s.device->CreateTexture2D(&texture_desc, nil, &texture))
  350. texture_view: ^d3d11.IShaderResourceView
  351. ch(s.device->CreateShaderResourceView(texture, nil, &texture_view))
  352. depth_stencil_desc := d3d11.TEXTURE2D_DESC{
  353. Width = u32(width),
  354. Height = u32(height),
  355. MipLevels = 1,
  356. ArraySize = 1,
  357. Format = .D24_UNORM_S8_UINT,
  358. SampleDesc = {Count = 1},
  359. Usage = .DEFAULT,
  360. BindFlags = {.DEPTH_STENCIL},
  361. }
  362. depth_stencil_texture: ^d3d11.ITexture2D
  363. ch(ch(s.device->CreateTexture2D(&depth_stencil_desc, nil, &depth_stencil_texture)))
  364. depth_stencil_texture_view: ^d3d11.IDepthStencilView
  365. ch(s.device->CreateDepthStencilView(depth_stencil_texture, nil, &depth_stencil_texture_view))
  366. render_target_view_desc := d3d11.RENDER_TARGET_VIEW_DESC {
  367. Format = texture_desc.Format,
  368. ViewDimension = .TEXTURE2D,
  369. }
  370. render_target_view: ^d3d11.IRenderTargetView
  371. ch(s.device->CreateRenderTargetView(texture, &render_target_view_desc, &render_target_view))
  372. d3d11_texture := D3D11_Texture {
  373. tex = texture,
  374. view = texture_view,
  375. format = .RGBA_32_Float,
  376. sampler = create_sampler(.MIN_MAG_MIP_POINT),
  377. }
  378. d3d11_render_target := D3D11_Render_Target {
  379. depth_stencil_texture = depth_stencil_texture,
  380. depth_stencil_texture_view = depth_stencil_texture_view,
  381. render_target_view = render_target_view,
  382. }
  383. return hm.add(&s.textures, d3d11_texture), hm.add(&s.render_targets, d3d11_render_target)
  384. }
  385. d3d11_load_texture :: proc(data: []u8, width: int, height: int, format: Pixel_Format) -> Texture_Handle {
  386. return create_texture(width, height, format, raw_data(data))
  387. }
  388. d3d11_update_texture :: proc(th: Texture_Handle, data: []u8, rect: Rect) -> bool {
  389. tex := hm.get(&s.textures, th)
  390. if tex == nil || tex.tex == nil {
  391. log.errorf("Trying to update texture %v with new data, but it is invalid.", th)
  392. return false
  393. }
  394. box := d3d11.BOX {
  395. left = u32(rect.x),
  396. top = u32(rect.y),
  397. bottom = u32(rect.y + rect.h),
  398. right = u32(rect.x + rect.w),
  399. back = 1,
  400. front = 0,
  401. }
  402. row_pitch := pixel_format_size(tex.format) * int(rect.w)
  403. s.device_context->UpdateSubresource(tex.tex, 0, &box, raw_data(data), u32(row_pitch), 0)
  404. return true
  405. }
  406. d3d11_destroy_texture :: proc(th: Texture_Handle) {
  407. if t := hm.get(&s.textures, th); t != nil {
  408. t.tex->Release()
  409. t.view->Release()
  410. if t.sampler != nil {
  411. t.sampler->Release()
  412. }
  413. }
  414. hm.remove(&s.textures, th)
  415. }
  416. d3d11_set_texture_filter :: proc(
  417. th: Texture_Handle,
  418. scale_down_filter: Texture_Filter,
  419. scale_up_filter: Texture_Filter,
  420. mip_filter: Texture_Filter,
  421. ) {
  422. t := hm.get(&s.textures, th)
  423. if t == nil {
  424. log.error("Trying to set texture filter for invalid texture %v", th)
  425. return
  426. }
  427. d := scale_down_filter
  428. u := scale_up_filter
  429. m := mip_filter
  430. f: d3d11.FILTER
  431. if d == .Point && u == .Point && m == .Point {
  432. f = .MIN_MAG_MIP_POINT
  433. } else if d == .Linear && u == .Linear && m == .Linear {
  434. f = .MIN_MAG_MIP_LINEAR
  435. } else if d == .Point && u == .Point && m == .Linear {
  436. f = .MIN_MAG_POINT_MIP_LINEAR
  437. } else if d == .Point && u == .Linear && m == .Linear {
  438. f = .MIN_POINT_MAG_MIP_LINEAR
  439. } else if d == .Linear && u == .Linear && m == .Linear {
  440. f = .MIN_MAG_MIP_LINEAR
  441. } else if d == .Linear && u == .Linear && m == .Point {
  442. f = .MIN_MAG_LINEAR_MIP_POINT
  443. } else if d == .Linear && u == .Point && m == .Point {
  444. f = .MIN_LINEAR_MAG_MIP_POINT
  445. } else if d == .Linear && u == .Point && m == .Linear {
  446. f = .MIN_LINEAR_MAG_POINT_MIP_LINEAR
  447. } else if d == .Point && u == .Linear && m == .Point {
  448. f = .MIN_POINT_MAG_LINEAR_MIP_POINT
  449. }
  450. if t.sampler != nil {
  451. t.sampler->Release()
  452. }
  453. t.sampler = create_sampler(f)
  454. }
  455. create_sampler :: proc(filter: d3d11.FILTER) -> ^d3d11.ISamplerState {
  456. sampler_desc := d3d11.SAMPLER_DESC{
  457. Filter = filter,
  458. AddressU = .WRAP,
  459. AddressV = .WRAP,
  460. AddressW = .WRAP,
  461. ComparisonFunc = .NEVER,
  462. }
  463. smp: ^d3d11.ISamplerState
  464. ch(s.device->CreateSamplerState(&sampler_desc, &smp))
  465. return smp
  466. }
  467. d3d11_load_shader :: proc(
  468. vs_source: string,
  469. ps_source: string,
  470. desc_allocator := frame_allocator,
  471. layout_formats: []Pixel_Format = {},
  472. ) -> (
  473. handle: Shader_Handle,
  474. desc: Shader_Desc,
  475. ) {
  476. vs_blob: ^d3d11.IBlob
  477. vs_blob_errors: ^d3d11.IBlob
  478. ch(d3d_compiler.Compile(raw_data(vs_source), len(vs_source), nil, nil, nil, "vs_main", "vs_5_0", 0, 0, &vs_blob, &vs_blob_errors))
  479. if vs_blob_errors != nil {
  480. log.error("Failed compiling shader:")
  481. log.error(strings.string_from_ptr((^u8)(vs_blob_errors->GetBufferPointer()), int(vs_blob_errors->GetBufferSize())))
  482. return
  483. }
  484. // VERTEX SHADER
  485. vertex_shader: ^d3d11.IVertexShader
  486. ch(s.device->CreateVertexShader(vs_blob->GetBufferPointer(), vs_blob->GetBufferSize(), nil, &vertex_shader))
  487. vs_ref: ^d3d11.IShaderReflection
  488. ch(d3d_compiler.Reflect(vs_blob->GetBufferPointer(), vs_blob->GetBufferSize(), d3d11.ID3D11ShaderReflection_UUID, (^rawptr)(&vs_ref)))
  489. vs_desc: d3d11.SHADER_DESC
  490. ch(vs_ref->GetDesc(&vs_desc))
  491. {
  492. desc.inputs = make([]Shader_Input, vs_desc.InputParameters, desc_allocator)
  493. assert(len(layout_formats) == 0 || len(layout_formats) == len(desc.inputs))
  494. for in_idx in 0..<vs_desc.InputParameters {
  495. in_desc: d3d11.SIGNATURE_PARAMETER_DESC
  496. if ch(vs_ref->GetInputParameterDesc(in_idx, &in_desc)) < 0 {
  497. log.errorf("Invalid shader input: %v", in_idx)
  498. continue
  499. }
  500. type: Shader_Input_Type
  501. if in_desc.SemanticIndex > 0 {
  502. log.errorf("Matrix shader input types not yet implemented")
  503. continue
  504. }
  505. switch in_desc.ComponentType {
  506. case .UNKNOWN: log.errorf("Unknown component type")
  507. case .UINT32: log.errorf("Not implemented")
  508. case .SINT32: log.errorf("Not implemented")
  509. case .FLOAT32:
  510. switch in_desc.Mask {
  511. case 0: log.errorf("Invalid input mask"); continue
  512. case 1: type = .F32
  513. case 3: type = .Vec2
  514. case 7: type = .Vec3
  515. case 15: type = .Vec4
  516. }
  517. }
  518. name := strings.clone_from_cstring(in_desc.SemanticName, desc_allocator)
  519. format := len(layout_formats) > 0 ? layout_formats[in_idx] : get_shader_input_format(name, type)
  520. desc.inputs[in_idx] = {
  521. name = name,
  522. register = int(in_idx),
  523. format = format,
  524. type = type,
  525. }
  526. }
  527. }
  528. constant_descs := make([dynamic]Shader_Constant_Desc, desc_allocator)
  529. d3d_constants := make([dynamic]D3D11_Shader_Constant, s.allocator)
  530. d3d_constant_buffers := make([dynamic]D3D11_Shader_Constant_Buffer, s.allocator)
  531. d3d_texture_bindings := make([dynamic]D3D11_Texture_Binding, s.allocator)
  532. texture_bindpoint_descs := make([dynamic]Shader_Texture_Bindpoint_Desc, desc_allocator)
  533. reflect_shader_constants(
  534. vs_desc,
  535. vs_ref,
  536. &constant_descs,
  537. &d3d_constants,
  538. &d3d_constant_buffers,
  539. &d3d_texture_bindings,
  540. &texture_bindpoint_descs,
  541. desc_allocator,
  542. .Vertex,
  543. )
  544. input_layout_desc := make([]d3d11.INPUT_ELEMENT_DESC, len(desc.inputs), frame_allocator)
  545. for idx in 0..<len(desc.inputs) {
  546. input := desc.inputs[idx]
  547. input_layout_desc[idx] = {
  548. SemanticName = frame_cstring(input.name),
  549. Format = dxgi_format_from_pixel_format(input.format),
  550. AlignedByteOffset = idx == 0 ? 0 : d3d11.APPEND_ALIGNED_ELEMENT,
  551. InputSlotClass = .VERTEX_DATA,
  552. }
  553. }
  554. input_layout: ^d3d11.IInputLayout
  555. ch(s.device->CreateInputLayout(raw_data(input_layout_desc), u32(len(input_layout_desc)), vs_blob->GetBufferPointer(), vs_blob->GetBufferSize(), &input_layout))
  556. // PIXEL SHADER
  557. ps_blob: ^d3d11.IBlob
  558. ps_blob_errors: ^d3d11.IBlob
  559. ch(d3d_compiler.Compile(raw_data(ps_source), len(ps_source), nil, nil, nil, "ps_main", "ps_5_0", 0, 0, &ps_blob, &ps_blob_errors))
  560. if ps_blob_errors != nil {
  561. log.error("Failed compiling shader:")
  562. log.error(strings.string_from_ptr((^u8)(ps_blob_errors->GetBufferPointer()), int(ps_blob_errors->GetBufferSize())))
  563. return
  564. }
  565. pixel_shader: ^d3d11.IPixelShader
  566. ch(s.device->CreatePixelShader(ps_blob->GetBufferPointer(), ps_blob->GetBufferSize(), nil, &pixel_shader))
  567. ps_ref: ^d3d11.IShaderReflection
  568. ch(d3d_compiler.Reflect(ps_blob->GetBufferPointer(), ps_blob->GetBufferSize(), d3d11.ID3D11ShaderReflection_UUID, (^rawptr)(&ps_ref)))
  569. ps_desc: d3d11.SHADER_DESC
  570. ch(ps_ref->GetDesc(&ps_desc))
  571. reflect_shader_constants(
  572. ps_desc,
  573. ps_ref,
  574. &constant_descs,
  575. &d3d_constants,
  576. &d3d_constant_buffers,
  577. &d3d_texture_bindings,
  578. &texture_bindpoint_descs,
  579. desc_allocator,
  580. .Pixel,
  581. )
  582. // Done with vertex and pixel shader. Just combine all the state.
  583. desc.constants = constant_descs[:]
  584. desc.texture_bindpoints = texture_bindpoint_descs[:]
  585. d3d_shd := D3D11_Shader {
  586. constants = d3d_constants[:],
  587. constant_buffers = d3d_constant_buffers[:],
  588. vertex_shader = vertex_shader,
  589. pixel_shader = pixel_shader,
  590. input_layout = input_layout,
  591. texture_bindings = d3d_texture_bindings[:],
  592. }
  593. h := hm.add(&s.shaders, d3d_shd)
  594. return h, desc
  595. }
  596. D3D11_Shader_Type :: enum {
  597. Vertex,
  598. Pixel,
  599. }
  600. reflect_shader_constants :: proc(
  601. d3d_desc: d3d11.SHADER_DESC,
  602. ref: ^d3d11.IShaderReflection,
  603. constant_descs: ^[dynamic]Shader_Constant_Desc,
  604. d3d_constants: ^[dynamic]D3D11_Shader_Constant,
  605. d3d_constant_buffers: ^[dynamic]D3D11_Shader_Constant_Buffer,
  606. d3d_texture_bindings: ^[dynamic]D3D11_Texture_Binding,
  607. texture_bindpoint_descs: ^[dynamic]Shader_Texture_Bindpoint_Desc,
  608. desc_allocator: runtime.Allocator,
  609. shader_type: D3D11_Shader_Type,
  610. ) {
  611. found_sampler_bindpoints := make([dynamic]u32, frame_allocator)
  612. for br_idx in 0..<d3d_desc.BoundResources {
  613. bind_desc: d3d11.SHADER_INPUT_BIND_DESC
  614. ref->GetResourceBindingDesc(br_idx, &bind_desc)
  615. #partial switch bind_desc.Type {
  616. case .SAMPLER:
  617. append(&found_sampler_bindpoints, bind_desc.BindPoint)
  618. case .TEXTURE:
  619. append(d3d_texture_bindings, D3D11_Texture_Binding {
  620. bind_point = bind_desc.BindPoint,
  621. })
  622. append(texture_bindpoint_descs, Shader_Texture_Bindpoint_Desc {
  623. name = strings.clone_from_cstring(bind_desc.Name, desc_allocator),
  624. })
  625. case .CBUFFER:
  626. cb_info := ref->GetConstantBufferByName(bind_desc.Name)
  627. if cb_info == nil {
  628. continue
  629. }
  630. cb_desc: d3d11.SHADER_BUFFER_DESC
  631. cb_info->GetDesc(&cb_desc)
  632. if cb_desc.Size == 0 {
  633. continue
  634. }
  635. constant_buffer_desc := d3d11.BUFFER_DESC{
  636. ByteWidth = cb_desc.Size,
  637. Usage = .DYNAMIC,
  638. BindFlags = {.CONSTANT_BUFFER},
  639. CPUAccessFlags = {.WRITE},
  640. }
  641. buffer_idx := -1
  642. for &existing, existing_idx in d3d_constant_buffers {
  643. if existing.bind_point == bind_desc.BindPoint {
  644. existing.bound_shaders += {shader_type}
  645. buffer_idx = existing_idx
  646. break
  647. }
  648. }
  649. if buffer_idx == -1 {
  650. buffer_idx = len(d3d_constant_buffers)
  651. buf := D3D11_Shader_Constant_Buffer {
  652. bound_shaders = {shader_type},
  653. }
  654. ch(s.device->CreateBuffer(&constant_buffer_desc, nil, &buf.gpu_data))
  655. buf.size = int(cb_desc.Size)
  656. buf.bind_point = bind_desc.BindPoint
  657. append(d3d_constant_buffers, buf)
  658. }
  659. for var_idx in 0..<cb_desc.Variables {
  660. var_info := cb_info->GetVariableByIndex(var_idx)
  661. if var_info == nil {
  662. continue
  663. }
  664. var_desc: d3d11.SHADER_VARIABLE_DESC
  665. var_info->GetDesc(&var_desc)
  666. if var_desc.Name != "" {
  667. append(constant_descs, Shader_Constant_Desc {
  668. name = strings.clone_from_cstring(var_desc.Name, desc_allocator),
  669. size = int(var_desc.Size),
  670. })
  671. append(d3d_constants, D3D11_Shader_Constant {
  672. buffer_idx = u32(buffer_idx),
  673. offset = var_desc.StartOffset,
  674. })
  675. }
  676. }
  677. case:
  678. log.errorf("Type is %v", bind_desc.Type)
  679. }
  680. }
  681. // Make sure each texture has a sampler. In GL samplers are associated with textures. In D3D11
  682. // several textures can use a single sampler. We don't want this as we want to be able to
  683. // configure filters etc on a per-texture level. Since two textures can arrive at a draw call
  684. // with different filters set, if they use the same sampler, then it will be impossible to set
  685. // that filtering up.
  686. for t, t_idx in d3d_texture_bindings {
  687. found := false
  688. for sampler_bindpoint in found_sampler_bindpoints {
  689. if t.bind_point == sampler_bindpoint {
  690. found = true
  691. break
  692. }
  693. }
  694. if !found {
  695. log.errorf(
  696. "Texture %v at bindpoint %v does not have a dedicated sampler at " +
  697. "the sampler register with the same bindpoint number. This is required to " +
  698. "in order to make D3D11 behave the same way as OpenGL etc",
  699. texture_bindpoint_descs[t_idx].name,
  700. t.bind_point,
  701. )
  702. }
  703. }
  704. }
  705. d3d11_destroy_shader :: proc(h: Shader_Handle) {
  706. shd := hm.get(&s.shaders, h)
  707. if shd == nil {
  708. log.errorf("Invalid shader: %v", h)
  709. return
  710. }
  711. shd.input_layout->Release()
  712. shd.vertex_shader->Release()
  713. shd.pixel_shader->Release()
  714. for c in shd.constant_buffers {
  715. if c.gpu_data != nil {
  716. c.gpu_data->Release()
  717. }
  718. }
  719. delete(shd.texture_bindings, s.allocator)
  720. delete(shd.constants, s.allocator)
  721. delete(shd.constant_buffers, s.allocator)
  722. hm.remove(&s.shaders, h)
  723. }
  724. // API END
  725. s: ^D3D11_State
  726. D3D11_Shader_Constant_Buffer :: struct {
  727. gpu_data: ^d3d11.IBuffer,
  728. size: int,
  729. bound_shaders: bit_set[D3D11_Shader_Type],
  730. bind_point: u32,
  731. }
  732. D3D11_Texture_Binding :: struct {
  733. bind_point: u32,
  734. sampler_bind_point: u32,
  735. }
  736. D3D11_Shader_Constant :: struct {
  737. buffer_idx: u32,
  738. offset: u32,
  739. }
  740. D3D11_Shader :: struct {
  741. handle: Shader_Handle,
  742. vertex_shader: ^d3d11.IVertexShader,
  743. pixel_shader: ^d3d11.IPixelShader,
  744. input_layout: ^d3d11.IInputLayout,
  745. constant_buffers: []D3D11_Shader_Constant_Buffer,
  746. constants: []D3D11_Shader_Constant,
  747. texture_bindings: []D3D11_Texture_Binding,
  748. }
  749. D3D11_State :: struct {
  750. allocator: runtime.Allocator,
  751. window_handle: dxgi.HWND,
  752. width: int,
  753. height: int,
  754. dxgi_adapter: ^dxgi.IAdapter,
  755. swapchain: ^dxgi.ISwapChain1,
  756. framebuffer_view: ^d3d11.IRenderTargetView,
  757. depth_buffer_view: ^d3d11.IDepthStencilView,
  758. device_context: ^d3d11.IDeviceContext,
  759. depth_stencil_state: ^d3d11.IDepthStencilState,
  760. rasterizer_state: ^d3d11.IRasterizerState,
  761. device: ^d3d11.IDevice,
  762. depth_buffer: ^d3d11.ITexture2D,
  763. framebuffer: ^d3d11.ITexture2D,
  764. blend_state: ^d3d11.IBlendState,
  765. textures: hm.Handle_Map(D3D11_Texture, Texture_Handle, 1024*10),
  766. render_targets: hm.Handle_Map(D3D11_Render_Target, Render_Target_Handle, 1024*10),
  767. shaders: hm.Handle_Map(D3D11_Shader, Shader_Handle, 1024*10),
  768. info_queue: ^d3d11.IInfoQueue,
  769. vertex_buffer_gpu: ^d3d11.IBuffer,
  770. all_samplers: map[^d3d11.ISamplerState]struct{},
  771. }
  772. create_swapchain :: proc(w, h: int) {
  773. swapchain_desc := dxgi.SWAP_CHAIN_DESC1 {
  774. Width = u32(w),
  775. Height = u32(h),
  776. Format = .B8G8R8A8_UNORM,
  777. SampleDesc = {
  778. Count = 1,
  779. },
  780. BufferUsage = {.RENDER_TARGET_OUTPUT},
  781. BufferCount = 2,
  782. Scaling = .STRETCH,
  783. SwapEffect = .DISCARD,
  784. }
  785. dxgi_factory: ^dxgi.IFactory2
  786. ch(s.dxgi_adapter->GetParent(dxgi.IFactory2_UUID, (^rawptr)(&dxgi_factory)))
  787. ch(dxgi_factory->CreateSwapChainForHwnd(s.device, s.window_handle, &swapchain_desc, nil, nil, &s.swapchain))
  788. ch(s.swapchain->GetBuffer(0, d3d11.ITexture2D_UUID, (^rawptr)(&s.framebuffer)))
  789. ch(s.device->CreateRenderTargetView(s.framebuffer, nil, &s.framebuffer_view))
  790. depth_buffer_desc: d3d11.TEXTURE2D_DESC
  791. s.framebuffer->GetDesc(&depth_buffer_desc)
  792. depth_buffer_desc.Format = .D24_UNORM_S8_UINT
  793. depth_buffer_desc.BindFlags = {.DEPTH_STENCIL}
  794. ch(s.device->CreateTexture2D(&depth_buffer_desc, nil, &s.depth_buffer))
  795. ch(s.device->CreateDepthStencilView(s.depth_buffer, nil, &s.depth_buffer_view))
  796. s.device_context->ClearDepthStencilView(s.depth_buffer_view, {.DEPTH}, 1, 0)
  797. }
  798. D3D11_Texture :: struct {
  799. handle: Texture_Handle,
  800. tex: ^d3d11.ITexture2D,
  801. view: ^d3d11.IShaderResourceView,
  802. format: Pixel_Format,
  803. // It may seem strange that we have a sampler here. But samplers are reused if you recreate them
  804. // with the same options. D3D11 will return the same object. So each time we set the filter
  805. // mode or the UV wrapping settings, then we just ask D3D11 for the sampler state for those
  806. // settings.
  807. //
  808. // Moreover, in order to make D3D11 behave a bit like GL (or rather, to make them behave more
  809. // similarly), we require that each texture in the HLSL shaders have a dedicated sampler.
  810. sampler: ^d3d11.ISamplerState,
  811. }
  812. D3D11_Render_Target :: struct {
  813. handle: Render_Target_Handle,
  814. depth_stencil_texture: ^d3d11.ITexture2D,
  815. depth_stencil_texture_view: ^d3d11.IDepthStencilView,
  816. render_target_view: ^d3d11.IRenderTargetView,
  817. }
  818. dxgi_format_from_pixel_format :: proc(f: Pixel_Format) -> dxgi.FORMAT {
  819. switch f {
  820. case .Unknown: return .UNKNOWN
  821. case .RGBA_32_Float: return .R32G32B32A32_FLOAT
  822. case .RGB_32_Float: return .R32G32B32_FLOAT
  823. case .RG_32_Float: return .R32G32_FLOAT
  824. case .R_32_Float: return .R32_FLOAT
  825. case .RGBA_8_Norm: return .R8G8B8A8_UNORM
  826. case .RG_8_Norm: return .R8G8_UNORM
  827. case .R_8_Norm: return .R8_UNORM
  828. case .R_8_UInt: return .R8_UINT
  829. }
  830. log.error("Unknown format")
  831. return .UNKNOWN
  832. }
  833. // CHeck win errors and print message log if there is any error
  834. ch :: proc(hr: dxgi.HRESULT, loc := #caller_location) -> dxgi.HRESULT {
  835. if hr >= 0 {
  836. return hr
  837. }
  838. log.errorf("d3d11 error: %0x", u32(hr), location = loc)
  839. log_messages(loc)
  840. return hr
  841. }
  842. log_messages :: proc(loc := #caller_location) {
  843. iq := s.info_queue
  844. if iq == nil {
  845. return
  846. }
  847. n := iq->GetNumStoredMessages()
  848. longest_msg: d3d11.SIZE_T
  849. for i in 0..=n {
  850. msglen: d3d11.SIZE_T
  851. iq->GetMessage(i, nil, &msglen)
  852. if msglen > longest_msg {
  853. longest_msg = msglen
  854. }
  855. }
  856. if longest_msg > 0 {
  857. msg_raw_ptr, _ := (mem.alloc(int(longest_msg), allocator = frame_allocator))
  858. for i in 0..=n {
  859. msglen: d3d11.SIZE_T
  860. iq->GetMessage(i, nil, &msglen)
  861. if msglen > 0 {
  862. msg := (^d3d11.MESSAGE)(msg_raw_ptr)
  863. iq->GetMessage(i, msg, &msglen)
  864. log.error(msg.pDescription, location = loc)
  865. }
  866. }
  867. }
  868. iq->ClearStoredMessages()
  869. }
  870. DEFAULT_SHADER_SOURCE :: #load("render_backend_d3d11_default_shader.hlsl")
  871. d3d11_default_shader_vertex_source :: proc() -> string {
  872. return string(DEFAULT_SHADER_SOURCE)
  873. }
  874. d3d11_default_shader_fragment_source :: proc() -> string {
  875. return string(DEFAULT_SHADER_SOURCE)
  876. }