porting source 2 to vulkan - real-time...

50

Upload: others

Post on 22-Jan-2021

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer
Page 2: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Porting Source 2 to Vulkan

Dan Ginsburg

Valve

Page 3: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Summary

• Source 2 Overview

• Porting to Vulkan

– Shaders and Pipelines

– Command Buffers

– Memory Management

– Descriptor Sets

Page 4: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Source 2 Overview

Page 5: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Source 2

• OpenGL, DX9, DX11, Vulkan

• Windows, Linux, Mac

• Dota 2 Reborn

Page 6: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Source 2 Rendering

• DX11-like rendersystem abstraction

• Multithreaded

– DX9/GL: software command buffers

– DX11: deferred contexts

– Single submission thread

Page 7: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Source 2 Rendering (GL)

Page 8: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Source 2 Rendering (GL)

Page 9: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Source 2 Rendering (GL)

Page 10: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Source 2 Vulkan Port

• Started with GL and DX11 renderer

– DX11 deferred contexts mapped well to Vulkan

command buffers

– Leveraged GLSL shader conversion

Page 11: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Shaders and Pipelines

Page 12: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Porting Shaders to Vulkan

• HLSL -> GLSL

– See: Moving Your Games to OpenGL, Steam Dev Days 2014

• GLSL -> SPIR-V

– Descriptor set layout qualifiers to GLSL

– Open source glslang SPIR-V backend

– SPVremapper for compression

– https://github.com/KhronosGroup/glslang

Page 13: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Pipeline State Objects (PSOs)

• Each thread caches pipeline state

• Global pipeline manager

– PSO Map

– Pending and current

• Reduces mutexing

Page 14: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Pipeline State Objects (PSOs)

• Each thread caches pipeline state

• Global pipeline manager

– PSO Map

– Pending and current

• Reduces mutexing Pending PSO MapCurrent PSO Map

PSO0

PSO1

PSO2

Page 15: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Pipeline State Objects (PSOs)

• Each thread caches pipeline state

• Global pipeline manager

– PSO Map

– Pending and current

• Reduces mutexing Pending PSO MapCurrent PSO Map

PSO0

PSO1

PSO2

Pipeline State

Hash

Page 16: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Pipeline State Objects (PSOs)

• Each thread caches pipeline state

• Global pipeline manager

– PSO Map

– Pending and current

• Reduces mutexing Pending PSO MapCurrent PSO Map

PSO0

PSO1

PSO2

Pipeline State

Hash

Lookup

Page 17: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Pipeline State Objects (PSOs)

• Each thread caches pipeline state

• Global pipeline manager

– PSO Map

– Pending and current

• Reduces mutexing Pending PSO MapCurrent PSO Map

PSO0

PSO1

PSO2

Pipeline State

Hash

Page 18: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Pipeline State Objects (PSOs)

• Each thread caches pipeline state

• Global pipeline manager

– PSO Map

– Pending and current

• Reduces mutexing Pending PSO MapCurrent PSO Map

PSO0

PSO1

PSO2

Pipeline State

Hash

Lookup

Page 19: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Pipeline State Objects (PSOs)

• Each thread caches pipeline state

• Global pipeline manager

– PSO Map

– Pending and current

• Reduces mutexing Pending PSO MapCurrent PSO Map

PSO0

PSO1

PSO2

Pipeline State

Hash

Page 20: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Pipeline State Objects (PSOs)

• Each thread caches pipeline state

• Global pipeline manager

– PSO Map

– Pending and current

• Reduces mutexing Pending PSO MapCurrent PSO Map

PSO0

PSO1

PSO2

Pipeline State

HashCreate PSO

Page 21: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Pipeline State Objects (PSOs)

• Each thread caches pipeline state

• Global pipeline manager

– PSO Map

– Pending and current

• Reduces mutexing Pending PSO MapCurrent PSO Map

PSO0

PSO1

PSO2

Pipeline State

HashCreate PSO

PSO3

Page 22: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Pipeline State Objects (PSOs)

• Each thread caches pipeline state

• Global pipeline manager

– PSO Map

– Pending and current

• Reduces mutexing Pending PSO MapCurrent PSO Map

PSO0

PSO1

PSO2

Pipeline State

HashCreate PSO

PSO3

Page 23: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Pipeline State Objects (PSOs)

• Each thread caches pipeline state

• Global pipeline manager

– PSO Map

– Pending and current

• Reduces mutexing

End of

Frame

Pending PSO MapCurrent PSO Map

PSO0

PSO1

PSO2

Pipeline State

HashCreate PSO

PSO3

Page 24: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Command Buffers

Page 25: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Command Buffers

• Used where DX11 deferred contexts were used

• Each thread builds command buffer

• Single thread performs submission to queue

Page 26: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Command Buffers

• Used where DX11 deferred contexts were used

• Each thread builds command buffer

• Single thread performs submission to queue

Page 27: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Command Buffers

• Used where DX11 deferred contexts were used

• Each thread builds command buffer

• Single thread performs submission to queue

Page 28: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Command Buffers

• Used where DX11 deferred contexts were used

• Each thread builds command buffer

• Single thread performs submission to queue

Page 29: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Command Buffers

• Recycled within per-thread pools

Thread 0

CommandPool

Thread 1

CommandPool

CmdBuf CmdBuf

CmdBuf CmdBuf

CmdBuf CmdBuf CmdBuf

Submit1 Submit2

Thread 2

CommandPool

Thread0

Thread1

Thread2

Submit

Main Check Fences

Page 30: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Command Buffers

• Recycled within per-thread pools

Thread 0

CommandPool

Thread 1

CommandPool

CmdBuf CmdBuf

CmdBuf CmdBuf

CmdBuf CmdBuf CmdBuf

Submit1 Submit2

Thread 2

CommandPool

Thread0

Thread1

Thread2

Submit

Main Check Fences

Page 31: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Command Buffers

• Recycled within per-thread pools

Thread 0

CommandPool

Thread 1

CommandPool

CmdBuf CmdBuf

CmdBuf CmdBuf

CmdBuf CmdBuf CmdBuf

Submit1 Submit2

Thread 2

CommandPool

Thread0

Thread1

Thread2

Submit

Main Check Fences

Page 32: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Command Buffers

• Recycled within per-thread pools

Thread 0

CommandPool

Thread 1

CommandPool

CmdBuf CmdBuf

CmdBuf CmdBuf

CmdBuf CmdBuf CmdBuf

Submit1 Submit2

Thread 2

CommandPool

Thread0

Thread1

Thread2

Submit

Main Check Fences

Page 33: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Command Buffers

• Recycled within per-thread pools

Thread 0

CommandPool

Thread 1

CommandPool

CmdBuf CmdBuf

CmdBuf CmdBuf

CmdBuf CmdBuf CmdBuf

Submit1 Submit2

Thread 2

CommandPool

Thread0

Thread1

Thread2

Submit

Main Check Fences

Page 34: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Command Buffers

• Recycled within per-thread pools

Thread 0

CommandPool

Thread 1

CommandPool

CmdBuf CmdBuf

CmdBuf CmdBuf

CmdBuf CmdBuf CmdBuf

Submit1 Submit2

Thread 2

CommandPool

Thread0

Thread1

Thread2

Submit

Main Check Fences

Page 35: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Command Buffers

• Recycled within per-thread pools

Thread 0

CommandPool

Thread 1

CommandPool

CmdBuf CmdBuf

CmdBuf CmdBuf

CmdBuf CmdBuf CmdBuf

Submit1 Submit2

Thread 2

CommandPool

Thread0

Thread1

Thread2

Submit

Main Check Fences

Page 36: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Command Buffers

• Recycled within per-thread pools

Thread 0

CommandPool

Thread 1

CommandPool

CmdBuf CmdBuf

CmdBuf CmdBuf

CmdBuf CmdBuf CmdBuf

Submit1 Submit2

Thread 2

CommandPool

Thread0

Thread1

Thread2

Submit

Main Check Fences

Page 37: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Command Buffers

• Recycled within per-thread pools

Thread 0

CommandPool

Thread 1

CommandPool

CmdBuf CmdBuf

CmdBuf CmdBuf

CmdBuf CmdBuf CmdBuf

Submit1 Submit2

Thread 2

CommandPool

Thread0

Thread1

Thread2

Submit

Main Check Fences

Page 38: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Command Buffers

• Recycled within per-thread pools

Thread 0

CommandPool

Thread 1

CommandPool

CmdBuf CmdBuf

CmdBuf CmdBuf

CmdBuf CmdBuf CmdBuf

Submit1 Submit2

Thread 2

CommandPool

Thread0

Thread1

Thread2

Submit

Main Check Fences

Page 39: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Command Buffer Performance

• Submit in batches

– vkQueueSubmit has cost on Windows

– Faster to group submissions together

• Minimize number of command buffers

• Minimize memory referenced per command buffer

• Use VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT

– Optimize for one-time submission

Page 40: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Memory Management

Page 41: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

General Strategies

• Pool resources together

– Reduces memory reference count

• Use per-thread pools to reduce contention

• Recycle dynamic pools on frame boundaries

Page 42: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Resources

Static Resources Dynamic Resources

• Global Pools

• Device Only

• Textures/Render Targets

• 128MB Pools

• VB/IB/CBs

• 8MB Pools

• Per-Thread Pools

• Host Visible (Persistently Mapped)

• VB/IB/CBs

• 8MB Pools

Page 43: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Dynamic Vertex/Index Buffers

• To update:

– Grab new offset from per-thread pool

– memcpy into pool

– Bind VBs with: vkCmdBindVertexBuffers(..,buffer,offset)

– Bind IBs with: vkCmdBindIndexBuffer(..,buffer,offset,..)

• Recycle pools when last GPU fence of frame retires

Page 44: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Dynamic Uniform Buffers

• Differences from VB/IBs:

– UBOs are bound via descriptors

– Use dynamic UBOs to avoid vkUpdateDescriptors

– Pass UBO offset to vkCmdBindDescriptorSets

Page 45: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Dynamic Textures

• Staged in persistently mapped buffers

– Recycled per-frame

• Copy with vkCmdCopyBufferToImage

Page 46: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Descriptor Sets

Page 47: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Descriptor Set - Ideal

• Allocate and bake descriptor sets up front

• Group sets by update frequency

• Only update changed sets

Page 48: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Descriptor Set - Reality

• Difficult to bake descriptors with DX11-like abstraction

• Our approach

– Pre-allocate descriptor sets with fixed slots

– Only bind to used slots

– Update descriptors each draw

Page 49: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Summary

• Source 2 Overview

• Porting to Vulkan

– Shaders and Pipelines

– Command Buffers

– Memory Management

– Descriptor Sets

Page 50: Porting Source 2 to Vulkan - Real-Time Renderingnextgenapis.realtimerendering.com/presentations/6... · 2015. 8. 18. · Source 2 Vulkan Port • Started with GL and DX11 renderer

Questions?

[email protected]

• Khronos BOF:

– Wed. August 12th, 5:30-7:30

– JW Marriott LA Live in the Platinum Ballroom Salon F-I