-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd3d11_commands.odin
More file actions
720 lines (593 loc) · 24.9 KB
/
d3d11_commands.odin
File metadata and controls
720 lines (593 loc) · 24.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
#+build windows
package gpu
// Core
import "base:runtime"
import "core:log"
import "core:slice"
import sa "core:container/small_array"
// Vendor
import "vendor:directx/d3d11"
d3d11_execute_begin_render_pass :: proc(
encoder_impl: ^D3D11_Command_Encoder_Impl,
cmd: ^Command_Begin_Render_Pass,
loc := #caller_location,
) {
device_impl := get_impl(D3D11_Device_Impl, encoder_impl.device, loc)
d3d_context := device_impl.d3d_context
// Collect render target views
rtvs: [MAX_COLOR_ATTACHMENTS]^d3d11.IRenderTargetView
rtv_count: u32
// Process color attachments
for i in 0 ..< sa.len(cmd.color_attachments) {
color_att := sa.get(cmd.color_attachments, i)
view_impl := get_impl(D3D11_Texture_View_Impl, color_att.view, loc)
assert(view_impl.rtv != nil, "Color attachment view must have a render target view", loc)
rtvs[rtv_count] = view_impl.rtv
rtv_count += 1
// Handle clear operation
if color_att.ops.load == .Clear {
clear_color: [4]f32 = {
f32(color_att.ops.clear_value.r),
f32(color_att.ops.clear_value.g),
f32(color_att.ops.clear_value.b),
f32(color_att.ops.clear_value.a),
}
d3d_context->ClearRenderTargetView(view_impl.rtv, &clear_color)
}
}
// Handle depth stencil attachment
dsv: ^d3d11.IDepthStencilView = nil
if depth_stencil, ok := cmd.depth_stencil_attachment.?; ok {
view_impl := get_impl(D3D11_Texture_View_Impl, depth_stencil.view, loc)
texture_impl := get_impl(D3D11_Texture_Impl, view_impl.texture, loc)
assert(view_impl.dsv != nil,
"Depth stencil attachment view must have a depth stencil view", loc)
dsv = view_impl.dsv
// Determine clear flags based on format and load operations
clear_flags: d3d11.CLEAR_FLAGS
has_depth := texture_format_has_depth_aspect(texture_impl.format)
has_stencil := texture_format_has_stencil_aspect(texture_impl.format)
if has_depth && depth_stencil.depth_ops.load == .Clear {
clear_flags |= { .DEPTH }
}
if has_stencil && depth_stencil.stencil_ops.load == .Clear {
clear_flags |= { .STENCIL }
}
// Clear if any flags are set
if clear_flags != {} {
d3d_context->ClearDepthStencilView(
dsv,
clear_flags,
depth_stencil.depth_ops.clear_value,
u8(depth_stencil.stencil_ops.clear_value),
)
}
}
// Bind render targets
d3d_context->OMSetRenderTargets(rtv_count, &rtvs[0], dsv)
// Set default viewport
viewport := d3d11.VIEWPORT{
TopLeftX = 0,
TopLeftY = 0,
Width = f32(cmd.width),
Height = f32(cmd.height),
MinDepth = 0.0,
MaxDepth = 1.0,
}
d3d_context->RSSetViewports(1, &viewport)
// Set default scissor rect
scissor := d3d11.RECT{
left = 0,
top = 0,
right = i32(cmd.width),
bottom = i32(cmd.height),
}
d3d_context->RSSetScissorRects(1, &scissor)
// Store render pass state for later commands
encoder_impl.current_render_pass = cmd.render_pass
encoder_impl.current_render_pass_width = cmd.width
encoder_impl.current_render_pass_height = cmd.height
encoder_impl.current_blend_color = {0, 0, 0, 0}
encoder_impl.current_stencil_reference = 0
}
d3d11_execute_copy_texture_to_texture :: proc(
cmd: ^Command_Copy_Texture_To_Texture,
loc := #caller_location,
) {
src := get_impl(D3D11_Texture_Impl, cmd.source.texture, loc)
dst := get_impl(D3D11_Texture_Impl, cmd.destination.texture, loc)
device_impl := get_impl(D3D11_Device_Impl, src.device, loc)
d3d_context := device_impl.d3d_context
// Get the appropriate resource pointers based on texture dimension
src_resource: ^d3d11.IResource
#partial switch src.dimension {
case .D1: src_resource = cast(^d3d11.IResource)src.texture1d
case .D2: src_resource = cast(^d3d11.IResource)src.texture2d
case .D3: src_resource = cast(^d3d11.IResource)src.texture3d
case:
panic("Invalid texture dimension", loc)
}
assert(src_resource != nil, loc = loc)
dst_resource: ^d3d11.IResource
#partial switch dst.dimension {
case .D1: dst_resource = cast(^d3d11.IResource)dst.texture1d
case .D2: dst_resource = cast(^d3d11.IResource)dst.texture2d
case .D3: dst_resource = cast(^d3d11.IResource)dst.texture3d
case:
panic("Invalid texture dimension", loc)
}
assert(dst_resource != nil, loc = loc)
src_subresources := subresource_range_get_affected_by_copy(cmd.source, cmd.copy_size)
dst_subresources := subresource_range_get_affected_by_copy(cmd.destination, cmd.copy_size)
// Set up the source box
src_box := d3d11.BOX {
left = cmd.source.origin.x,
right = cmd.source.origin.x + cmd.copy_size.width,
top = cmd.source.origin.y,
bottom = cmd.source.origin.y + cmd.copy_size.height,
}
#partial switch src.dimension {
case .D1, .D2:
src_box.front = 0
src_box.back = 1
case .D3:
src_box.front = cmd.source.origin.z
src_box.back = cmd.source.origin.z + cmd.copy_size.depth_or_array_layers
case:
unreachable()
}
for layer in 0 ..< src_subresources.array_layer_count {
src_subresource_index := texture_get_subresource_index(
src,
cmd.source.mip_level,
src_subresources.base_array_layer + layer,
src_subresources.aspect,
)
dst_subresource_index := texture_get_subresource_index(
dst,
cmd.destination.mip_level,
dst_subresources.base_array_layer + layer,
dst_subresources.aspect,
)
// Perform the copy
d3d_context->CopySubresourceRegion(
pDstResource = dst_resource,
DstSubresource = dst_subresource_index,
DstX = cmd.destination.origin.x,
DstY = cmd.destination.origin.y,
DstZ = dst.dimension == .D3 ? cmd.destination.origin.z : 0,
pSrcResource = src_resource,
SrcSubresource = src_subresource_index,
pSrcBox = &src_box,
)
}
}
d3d11_execute_render_pass_draw :: proc(
encoder_impl: ^D3D11_Command_Encoder_Impl,
cmd: ^Command_Render_Pass_Draw,
loc := #caller_location,
) {
device_impl := get_impl(D3D11_Device_Impl, encoder_impl.device, loc)
d3d_context := device_impl.d3d_context
// Validate we have a pipeline set
// assert(encoder_impl.current_pipeline != nil, "No render pipeline set", loc)
d3d_context->Draw(cmd.vertex_count, cmd.first_vertex)
}
d3d11_execute_render_pass_draw_indexed :: proc(
encoder_impl: ^D3D11_Command_Encoder_Impl,
cmd: ^Command_Render_Pass_Draw_Indexed,
loc := #caller_location,
) {
device_impl := get_impl(D3D11_Device_Impl, encoder_impl.device, loc)
d3d_context := device_impl.d3d_context
d3d_context->DrawIndexedInstanced(
IndexCountPerInstance = cmd.index_count,
InstanceCount = cmd.instance_count,
StartIndexLocation = cmd.first_index,
BaseVertexLocation = cmd.vertex_offset,
StartInstanceLocation = cmd.first_instance,
)
}
d3d11_execute_render_pass_end :: proc(
encoder_impl: ^D3D11_Command_Encoder_Impl,
cmd: ^Command_Render_Pass_End,
loc := #caller_location,
) {
device_impl := get_impl(D3D11_Device_Impl, encoder_impl.device, loc)
d3d_context := device_impl.d3d_context
// Unbind render targets
d3d_context->OMSetRenderTargets(0, nil, nil)
@(require_results)
calculate_subresource_index :: proc(
mip_level: u32,
array_layer: u32,
mip_level_count: u32,
) -> u32 {
return mip_level + (array_layer * mip_level_count)
}
// Find the begin command to handle store operations and resolve
begin_cmd := encoder_impl.current_begin_render_pass_cmd
if begin_cmd != nil {
// Check if we need to resolve multisampled attachments
sample_count : u32 = 1
if sa.len(begin_cmd.color_attachments) > 0 {
color_att0 := sa.get(begin_cmd.color_attachments, 0)
view_impl := get_impl(D3D11_Texture_View_Impl, color_att0.view, loc)
texture_impl := get_impl(D3D11_Texture_Impl, view_impl.texture, loc)
sample_count = texture_impl.sample_count
}
// Resolve multisampled textures if needed
if sample_count > 1 {
for i in 0..<sa.len(begin_cmd.color_attachments) {
color_att := sa.get(begin_cmd.color_attachments, i)
// Skip if no resolve target
if color_att.resolve_target == nil {
continue
}
src_view_impl := get_impl(D3D11_Texture_View_Impl, color_att.view, loc)
dst_view_impl := get_impl(D3D11_Texture_View_Impl, color_att.resolve_target, loc)
src_texture_impl := get_impl(D3D11_Texture_Impl, src_view_impl.texture, loc)
dst_texture_impl := get_impl(D3D11_Texture_Impl, dst_view_impl.texture, loc)
// Calculate subresource indices
dst_subresource := calculate_subresource_index(
dst_view_impl.base_mip_level,
dst_view_impl.base_array_layer,
dst_texture_impl.mip_level_count,
)
src_subresource := calculate_subresource_index(
src_view_impl.base_mip_level,
src_view_impl.base_array_layer,
src_texture_impl.mip_level_count,
)
// Resolve
d3d_context->ResolveSubresource(
dst_texture_impl.texture2d,
dst_subresource,
src_texture_impl.texture2d,
src_subresource,
dst_texture_impl.dxgi_format,
)
}
}
// Handle store operations (discard optimization)
for i in 0..<sa.len(begin_cmd.color_attachments) {
color_att := sa.get(begin_cmd.color_attachments, i)
if color_att.ops.store == .Discard {
view_impl := get_impl(D3D11_Texture_View_Impl, color_att.view, loc)
if view_impl.rtv != nil {
d3d_context->DiscardView(view_impl.rtv)
}
}
}
// Handle depth/stencil discard
if depth_stencil, ok := begin_cmd.depth_stencil_attachment.?; ok {
should_discard := (
depth_stencil.depth_ops.store == .Discard ||
depth_stencil.stencil_ops.store == .Discard)
if should_discard {
view_impl := get_impl(D3D11_Texture_View_Impl, depth_stencil.view, loc)
if view_impl.dsv != nil {
d3d_context->DiscardView(view_impl.dsv)
}
}
}
}
// Clear render pass state
encoder_impl.current_render_pass = nil
encoder_impl.current_render_pass_width = 0
encoder_impl.current_render_pass_height = 0
encoder_impl.current_begin_render_pass_cmd = nil
}
d3d11_execute_render_pass_set_bind_group :: proc(
encoder_impl: ^D3D11_Command_Encoder_Impl,
cmd: ^Command_Render_Pass_Set_Bind_Group,
loc := #caller_location,
) {
bind_group_impl := get_impl(D3D11_Bind_Group_Impl, cmd.group, loc)
device_impl := get_impl(D3D11_Device_Impl, encoder_impl.device, loc)
d3d_context := device_impl.d3d_context
dynamic_offset_idx := 0
for &entry in bind_group_impl.entries {
layout_entry := &bind_group_impl.layout.entries[entry.binding]
bind_vs := .Vertex in layout_entry.visibility
bind_ps := .Fragment in layout_entry.visibility
bind_cs := .Compute in layout_entry.visibility
slot := entry.binding
switch &res in entry.resource {
case D3D11_Buffer_Binding:
buffer_layout := layout_entry.type.(D3D11_Buffer_Binding_Layout)
offset := res.offset
size := res.size
if buffer_layout.has_dynamic_offset {
assert(dynamic_offset_idx < len(cmd.dynamic_offsets),
"Not enough dynamic offsets provided", loc)
offset += u64(cmd.dynamic_offsets[dynamic_offset_idx])
dynamic_offset_idx += 1
}
#partial switch buffer_layout.type {
case .Uniform:
cb := res.buffer.buffer
// Constant buffer offsets are in units of 16 bytes (one "constant")
// Dynamic offsets are required to be 256-byte aligned
first_constant := u32(offset / 16)
// Ceiling division to cover partial constants at the end
num_constants := u32((size + 15) / 16)
if bind_vs {
d3d_context->VSSetConstantBuffers1(
slot, 1, &cb, &first_constant, &num_constants)
}
if bind_ps {
d3d_context->PSSetConstantBuffers1(
slot, 1, &cb, &first_constant, &num_constants)
}
if bind_cs {
d3d_context->CSSetConstantBuffers1(
slot, 1, &cb, &first_constant, &num_constants)
}
case .Storage, .Read_Only_Storage:
uav := res.buffer.uav
initial_count := max(u32) // keep existing counter for append/consume
if bind_vs {
log.warn(
"Storage buffers in vertex shaders have very limited support in D3D11",
location = loc)
}
if bind_ps {
d3d_context->OMSetRenderTargetsAndUnorderedAccessViews(
d3d11.KEEP_RENDER_TARGETS_AND_DEPTH_STENCIL,
nil,
nil,
slot,
1,
&uav,
&initial_count,
)
}
if bind_cs {
d3d_context->CSSetUnorderedAccessViews(slot, 1, &uav, &initial_count)
}
case:
unreachable()
}
case D3D11_Sampler_Binding:
sampler := res.sampler.sampler_state
if bind_vs { d3d_context->VSSetSamplers(slot, 1, &sampler) }
if bind_ps { d3d_context->PSSetSamplers(slot, 1, &sampler) }
if bind_cs { d3d_context->CSSetSamplers(slot, 1, &sampler) }
case D3D11_Texture_View_Binding:
srv := res.texture_view.srv
if bind_vs { d3d_context->VSSetShaderResources(slot, 1, &srv) }
if bind_ps { d3d_context->PSSetShaderResources(slot, 1, &srv) }
if bind_cs { d3d_context->CSSetShaderResources(slot, 1, &srv) }
case []D3D11_Buffer_Binding:
buffer_layout := layout_entry.type.(D3D11_Buffer_Binding_Layout)
count := u32(len(res))
#partial switch buffer_layout.type {
case .Uniform:
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
cbs := make([]^d3d11.IBuffer, count, context.temp_allocator)
first_constants := make([]u32, count, context.temp_allocator)
num_constants := make([]u32, count, context.temp_allocator)
for &buf, i in res {
offset := buf.offset
if buffer_layout.has_dynamic_offset {
assert(dynamic_offset_idx < len(cmd.dynamic_offsets),
"Not enough dynamic offsets provided", loc)
offset += u64(cmd.dynamic_offsets[dynamic_offset_idx])
dynamic_offset_idx += 1
}
cbs[i] = buf.buffer.buffer
first_constants[i] = u32(offset / 16)
num_constants[i] = u32((buf.size + 15) / 16)
}
if bind_vs {
d3d_context->VSSetConstantBuffers1(
slot, count, raw_data(cbs),
raw_data(first_constants), raw_data(num_constants))
}
if bind_ps {
d3d_context->PSSetConstantBuffers1(
slot, count, raw_data(cbs),
raw_data(first_constants), raw_data(num_constants))
}
if bind_cs {
d3d_context->CSSetConstantBuffers1(
slot, count, raw_data(cbs),
raw_data(first_constants), raw_data(num_constants))
}
case .Storage, .Read_Only_Storage:
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
uavs := make([]^d3d11.IUnorderedAccessView, count, context.temp_allocator)
initial_counts := make([]u32, count, context.temp_allocator)
slice.fill(initial_counts, max(u32))
for &buf, i in res {
uavs[i] = buf.buffer.uav
}
if bind_ps {
d3d_context->OMSetRenderTargetsAndUnorderedAccessViews(
d3d11.KEEP_RENDER_TARGETS_AND_DEPTH_STENCIL,
nil,
nil,
slot,
count,
raw_data(uavs),
raw_data(initial_counts),
)
}
if bind_cs {
d3d_context->CSSetUnorderedAccessViews(
slot, count, raw_data(uavs), raw_data(initial_counts))
}
case:
unreachable()
}
case []D3D11_Sampler_Binding:
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
count := u32(len(res))
samplers := make([]^d3d11.ISamplerState, count, context.temp_allocator)
for &samp, i in res {
samplers[i] = samp.sampler.sampler_state
}
if bind_vs { d3d_context->VSSetSamplers(slot, count, raw_data(samplers)) }
if bind_ps { d3d_context->PSSetSamplers(slot, count, raw_data(samplers)) }
if bind_cs { d3d_context->CSSetSamplers(slot, count, raw_data(samplers)) }
case []D3D11_Texture_View_Binding:
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
count := u32(len(res))
srvs := make([]^d3d11.IShaderResourceView, count, context.temp_allocator)
for &view, i in res {
srvs[i] = view.texture_view.srv
}
if bind_vs { d3d_context->VSSetShaderResources(slot, count, raw_data(srvs)) }
if bind_ps { d3d_context->PSSetShaderResources(slot, count, raw_data(srvs)) }
if bind_cs { d3d_context->CSSetShaderResources(slot, count, raw_data(srvs)) }
}
}
}
d3d11_execute_render_pass_set_index_buffer :: proc(
encoder_impl: ^D3D11_Command_Encoder_Impl,
cmd: ^Command_Render_Pass_Set_Index_Buffer,
loc := #caller_location,
) {
device_impl := get_impl(D3D11_Device_Impl, encoder_impl.device, loc)
d3d_context := device_impl.d3d_context
buffer_impl := get_impl(D3D11_Buffer_Impl, cmd.buffer, loc)
index_buffer := buffer_impl.buffer
index_buffer_format := d3d_dxgi_index_format(cmd.format)
index_buffer_offset := u32(cmd.offset)
d3d_context->IASetIndexBuffer(index_buffer, index_buffer_format, index_buffer_offset)
}
d3d11_command_render_pass_set_render_pipeline :: proc(
encoder_impl: ^D3D11_Command_Encoder_Impl,
cmd: ^Command_Render_Pass_Set_Render_Pipeline,
loc := #caller_location,
) {
device_impl := get_impl(D3D11_Device_Impl, encoder_impl.device, loc)
d3d_context := device_impl.d3d_context
pipeline_impl := get_impl(D3D11_Render_Pipeline_Impl, cmd.pipeline, loc)
// Set primitive topology
d3d_context->IASetPrimitiveTopology(pipeline_impl.primitive_topology)
// Set input layout
d3d_context->IASetInputLayout(pipeline_impl.input_layout)
// Set rasterizer state
d3d_context->RSSetState(pipeline_impl.rasterizer_state)
// Set shaders
d3d_context->VSSetShader(pipeline_impl.vertex_shader, nil, 0)
d3d_context->PSSetShader(pipeline_impl.pixel_shader, nil, 0)
// Set blend state
blend_factor := [4]f32{1.0, 1.0, 1.0, 1.0}
d3d_context->OMSetBlendState(
pipeline_impl.blend_state,
&blend_factor,
pipeline_impl.sample_mask,
)
// Set depth stencil state
if pipeline_impl.depth_stencil_state != nil {
d3d_context->OMSetDepthStencilState(pipeline_impl.depth_stencil_state, 0)
}
}
d3d11_execute_render_pass_set_scissor_rect :: proc(
encoder_impl: ^D3D11_Command_Encoder_Impl,
cmd: ^Command_Render_Pass_Set_Scissor_Rect,
loc := #caller_location,
) {
device_impl := get_impl(D3D11_Device_Impl, encoder_impl.device, loc)
d3d_context := device_impl.d3d_context
scissor_rect := d3d11.RECT{
left = i32(cmd.x),
top = i32(cmd.y),
right = i32(cmd.x + cmd.width),
bottom = i32(cmd.y + cmd.height),
}
d3d_context->RSSetScissorRects(1, &scissor_rect)
}
d3d11_execute_render_pass_set_stencil_reference :: proc(
encoder_impl: ^D3D11_Command_Encoder_Impl,
cmd: ^Command_Render_Pass_Set_Stencil_Reference,
loc := #caller_location,
) {
device_impl := get_impl(D3D11_Device_Impl, encoder_impl.device, loc)
d3d_context := device_impl.d3d_context
pipeline_impl := get_impl(D3D11_Render_Pipeline_Impl, cmd.pipeline, loc)
if pipeline_impl.depth_stencil_state != nil {
d3d_context->OMSetDepthStencilState(pipeline_impl.depth_stencil_state, cmd.reference)
}
}
d3d11_execute_render_pass_set_vertex_buffer :: proc(
encoder_impl: ^D3D11_Command_Encoder_Impl,
cmd: ^Command_Render_Pass_Set_Vertex_Buffer,
loc := #caller_location,
) {
device_impl := get_impl(D3D11_Device_Impl, encoder_impl.device, loc)
d3d_context := device_impl.d3d_context
buffer_impl := get_impl(D3D11_Buffer_Impl, cmd.buffer, loc)
pipeline_impl := get_impl(D3D11_Render_Pipeline_Impl, cmd.pipeline, loc)
assert(buffer_impl.buffer != nil, "Buffer is null", loc)
assert(.Vertex in buffer_impl.usage, "Buffer is not a vertex buffer", loc)
// Get stride from cached pipeline vertex buffer info
stride: u32 = 0
if cmd.slot < u32(len(pipeline_impl.vertex_buffers)) {
#no_bounds_check stride = pipeline_impl.vertex_buffers[cmd.slot].stride
} else {
log.errorf("Vertex buffer slot %d out of range (pipeline has %d vertex buffers)",
cmd.slot, len(pipeline_impl.vertex_buffers), location = loc)
return
}
offset := u32(cmd.offset)
d3d_context->IASetVertexBuffers(
cmd.slot,
1,
&buffer_impl.buffer,
&stride,
&offset,
)
}
d3d11_execute_render_pass_set_viewport :: proc(
encoder_impl: ^D3D11_Command_Encoder_Impl,
cmd: ^Command_Render_Pass_Set_Viewport,
loc := #caller_location,
) {
device_impl := get_impl(D3D11_Device_Impl, encoder_impl.device, loc)
d3d_context := device_impl.d3d_context
viewport := d3d11.VIEWPORT{
TopLeftX = cmd.x,
TopLeftY = cmd.y,
Width = cmd.width,
Height = cmd.height,
MinDepth = cmd.min_depth,
MaxDepth = cmd.max_depth,
}
d3d_context->RSSetViewports(1, &viewport)
}
d3d11_execute_command :: proc(
encoder_impl: ^D3D11_Command_Encoder_Impl,
cmd: ^Command,
loc := #caller_location,
) {
#partial switch &c in cmd {
case Command_Begin_Render_Pass:
d3d11_execute_begin_render_pass(encoder_impl, &c)
case Command_Copy_Texture_To_Texture:
d3d11_execute_copy_texture_to_texture(&c)
case Command_Render_Pass_Draw:
d3d11_execute_render_pass_draw(encoder_impl, &c)
case Command_Render_Pass_Draw_Indexed:
d3d11_execute_render_pass_draw_indexed(encoder_impl, &c)
case Command_Render_Pass_End:
d3d11_execute_render_pass_end(encoder_impl, &c)
case Command_Render_Pass_Set_Bind_Group:
d3d11_execute_render_pass_set_bind_group(encoder_impl, &c)
case Command_Render_Pass_Set_Index_Buffer:
d3d11_execute_render_pass_set_index_buffer(encoder_impl, &c)
case Command_Render_Pass_Set_Render_Pipeline:
d3d11_command_render_pass_set_render_pipeline(encoder_impl, &c)
case Command_Render_Pass_Set_Scissor_Rect:
d3d11_execute_render_pass_set_scissor_rect(encoder_impl, &c)
case Command_Render_Pass_Set_Stencil_Reference:
d3d11_execute_render_pass_set_stencil_reference(encoder_impl, &c)
case Command_Render_Pass_Set_Vertex_Buffer:
d3d11_execute_render_pass_set_vertex_buffer(encoder_impl, &c)
case Command_Render_Pass_Set_Viewport:
d3d11_execute_render_pass_set_viewport(encoder_impl, &c)
}
}