Professional Documents
Culture Documents
Amd 2018 Porting To Vulkan dx12 Adam Sawicki
Amd 2018 Porting To Vulkan dx12 Adam Sawicki
Amd 2018 Porting To Vulkan dx12 Adam Sawicki
ADAM SAWICKI
DEVELOPER TECHNOLOGY ENGINEER, AMD
AGENDA
Introduction
Porting
• Memory management
• API of your renderer
• Pipelines, descriptors, command buffers
• Objects lifetime
• Multithreading on CPU
• Using multiple GPU queues
• Barriers
• Frame graph
• Additional considerations
Conclusion
|2 MAY 2018
Introduction
Why port to Vulkan™ or DX12?
|3 MAY 2018
ADVANTAGES
New APIs
Game Driver
Engine Graphics API
|4 MAY 2018
ADVANTAGES
multithreading on CPU
using multiple GPU queues
explicit multi-GPU
better optimization for specific platforms
less CPU overhead
opportunity to improve engine architecture
|5 MAY 2018
Porting
How to port your engine?
|6 MAY 2018
RESPONSIBILITIES
|7 MAY 2018
MEMORY MANAGEMENT
THE CHALLENGE
Memory
|8 MAY 2018
MEMORY MANAGEMENT
ADVANTAGES
|9 MAY 2018
MEMORY MANAGEMENT
SUB-ALLOCATION
Possible solutions:
12
| MAY 2018
MEMORY MANAGEMENT
SUB-ALLOCATION
Good: Allocate large (e.g. 256 MiB) blocks when needed, sub-allocate parts of them for your resources
(CreatePlacedResource).
‒ requires writing custom allocator Vulkan™: you can use free library: Vulkan Memory Allocator
https://github.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator
‒ making new allocations in runtime can cause hitching do it on separate background thread
Excellent: Allocate all needed memory and create all resources while loading game/level.
13
| MAY 2018
MEMORY MANAGEMENT
OVER-COMMITMENT
14
| MAY 2018
MEMORY MANAGEMENT
OVER-COMMITMENT
Possible solutions:
Bad: Allocate as much memory as you need, handle allocation errors, rely on system migration policy.
15
| MAY 2018
RENDERER API
Pipeline / Pipeline State Object (PSO) encapsulates most of the configuration of graphics pipeline.
vertex format, shaders, depth-stencil state, blend state, …
17
| MAY 2018
PIPELINES
RECOMMENDATIONS
Possible solutions:
Bad: Leave old interface with separate states.
Flush on draw call: hash the state, lookup existing pipeline or create a new one.
‒ bad: wait for it hitching
‒ better: create it on background thread
• SetRenderState(D3DRS_CULLMODE, …)
• SetRenderState(D3DRS_ZENABLE, …)
• SetPixelShader(ps1)
• SetTexture(0, tex1)
• DrawIndexed()
18
| MAY 2018
DESCRIPTORS
19
| MAY 2018
COMMAND BUFFERS
Possible solutions:
Bad: Use single command buffer. Submit it and then immediately wait for it to finish.
CPU and GPU get serialized
GPU
CPU
Time
20
| MAY 2018
COMMAND BUFFERS
GPU
CPU
21
| MAY 2018
COMMAND BUFFERS
GPU
CPU
22
| MAY 2018
COMMAND BUFFERS
23
| MAY 2018
COMMAND BUFFERS
GPU
CPU Core 0
CPU Core 1
CPU Core 2
24
| MAY 2018
OBJECT LIFE-TIME
Most objects and data are not reference-counted or versioned by the API for usage on GPU.
You need to make sure they remain alive and unchanged as long as they are used by the GPU.
Includes: descriptors, contents of memory e.g. constant buffers.
GPU 0 1 0 1
CPU 0 1 0 1
25
| MAY 2018
OBJECT LIFE-TIME
26
| MAY 2018
MULTITHREADING (CPU)
Possible solutions:
Better: Main thread with gameplay logic, scripting etc. + separate render thread +
some background threads, e.g. AI, resource loading.
27
| MAY 2018
MULTITHREADING (CPU)
28
| MAY 2018
MULTITHREADING (GPU)
Graphics
Async compute
Transfer
29
| MAY 2018
MULTITHREADING (GPU)
Async compute
30
| MAY 2018
MULTITHREADING (GPU)
Transfer
31
| MAY 2018
MULTITHREADING (GPU)
EXAMPLE
other work
32
| MAY 2018
BARRIERS
Possible solutions:
use as render
Hard: Barriers hardcoded, placed manually. target
can reach good performance, but difficult and error-prone
barrier
Bad: Define “base state”. Always go back to this state after use.
not very efficient
use as sampled
Better: Remember last state. Transition to new state before use. texture
works, but still can do better
33
| MAY 2018
BARRIERS
Excellent: Have look-ahead of whole render frame. Find best place to issue barriers.
place barriers as early as possible before result is needed – may hide their latency
Most of your resources don’t need layout transitions in runtime. Only limited number does.
Bad result: waiting for idle between all draw calls. Good result: everything pipelined.
Batch barriers together into one call wherever possible.
34
| MAY 2018
FRAME GRAPH
Depth
AO
G-buffer
SM
Scene
35
| MAY 2018
FRAME GRAPH
ADVANTAGES
render passes
‒ determine order of render passes
‒ group them into command buffers, Vulkan™ render passes and subpasses
‒ parallelize on CPU – record command buffers on multiple threads
‒ parallelize on GPU – assign passes to hardware queues
AO
Z prepass Post-processing
Lighting
Fill G-buffer
Shadow map
36
| MAY 2018
FRAME GRAPH
ADVANTAGES
resources
‒ determine what barriers are needed
‒ find the most optimal place to issue barriers
‒ alias memory, if lifetime of resources don’t overlap
37
| MAY 2018
ADDITIONAL CONSIDERATIONS
1/3
38
| MAY 2018
ADDITIONAL CONSIDERATIONS
2/3
39
| MAY 2018
ADDITIONAL CONSIDERATIONS
3/3
40
| MAY 2018
Conclusion
41
| MAY 2018
CONCLUSION
New graphics APIs (Vulkan™, Direct3D 12) are lower level, more explicit.
There are recommended good practices, software libraries, and tools that can help you with that.
42
| MAY 2018
LIBEARIES
43
| MAY 2018
FURTHER READING
Rodrigues, Tiago (Ubisoft Montreal). Moving to DirectX ® 12: Lessons Learned. GDC 2017.
Sawicki, Adam (AMD). Memory management in Vulkan™ and DX12. GDC 2018.
44
| MAY 2018
Thank you
Questions?
45
| MAY 2018
DISCLAIMER & ATTRIBUTION
The information presented in this document is for informational purposes only and may contain technical inaccuracies, omissions and typographical errors.
The information contained herein is subject to change and may be rendered inaccurate for many reasons, including but not limited to product and roadmap changes, component and motherboard version changes, new
model and/or product releases, product differences between differing manufacturers, software changes, BIOS flashes, firmware upgrades, or the like. AMD assumes no obligation to update or otherwise correct or revise
this information. However, AMD reserves the right to revise this information and to make changes from time to time to the content hereof without obligation of AMD to notify any person of such revisions or changes.
AMD MAKES NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE CONTENTS HEREOF AND ASSUMES NO RESPONSIBILITY FOR ANY INACCURACIES, ERRORS OR OMISSIONS THAT MAY APPEAR IN THIS
INFORMATION.
AMD SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE. IN NO EVENT WILL AMD BE LIABLE TO ANY PERSON FOR ANY DIRECT, INDIRECT, SPECIAL OR
OTHER CONSEQUENTIAL DAMAGES ARISING FROM THE USE OF ANY INFORMATION CONTAINED HEREIN, EVEN IF AMD IS EXPRESSLY ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
ATTRIBUTION
© 2016 Advanced Micro Devices, Inc. All rights reserved. AMD, the AMD Arrow logo and combinations thereof are trademarks of Advanced Micro Devices, Inc. in the United States and/or other jurisdictions. Other names
are for informational purposes only and may be trademarks of their respective owners.
47
| MAY 2018