Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vulkan: Reduce the num of cmds for state rebuilding again #2056

Merged
merged 1 commit into from
Jul 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions gapis/api/vulkan/api/command_buffer_control.api
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,10 @@ cmd VkResult vkResetCommandPool(
////////////////////

enum RecordingState {
NOT_STARTED = 0,
RECORDING = 1,
COMPLETED = 2
NOT_STARTED = 0,
RECORDING = 1,
COMPLETED = 2,
TO_BE_RESET = 3
}

@internal class CommandBufferBegin {
Expand Down
3 changes: 3 additions & 0 deletions gapis/api/vulkan/api/queue.api
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ cmd VkResult vkQueueSubmit(
if cb.Recording != COMPLETED {
vkErrorCommandBufferIncomplete(command_buffers[j])
}
if (as!u32(cb.BeginInfo.Flags) & as!u32(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT)) != as!u32(0) {
cb.Recording = TO_BE_RESET
}
for k in (0 .. len(cb.CommandReferences)) {
ref := cb.CommandReferences[as!u32(k)]
LastBoundQueue.PendingCommands[len(LastBoundQueue.PendingCommands)]
Expand Down
56 changes: 28 additions & 28 deletions gapis/api/vulkan/scratch_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,18 +222,18 @@ func (qr *queueFamilyScratchResources) bindAndFillBuffers(totalAllocationSize ui
allocated = qr.allocated
usingTempMem = false
}
atData := sb.MustReserve(totalAllocationSize)
ptrAtData := sb.newState.AllocDataOrPanic(sb.ctx, NewVoidᵖ(atData.Ptr()))
sb.write(sb.cb.VkMapMemory(
dev, deviceMemory, VkDeviceSize(allocated), VkDeviceSize(totalAllocationSize),
VkMemoryMapFlags(0), ptrAtData.Ptr(), VkResult_VK_SUCCESS,
).AddRead(ptrAtData.Data()).AddWrite(ptrAtData.Data()))
ptrAtData.Free()

bufBindingOffset := allocated
for buf, info := range buffers {
sb.write(sb.cb.VkBindBufferMemory(
dev, buf, deviceMemory, VkDeviceSize(allocated), VkResult_VK_SUCCESS))

atData := sb.MustReserve(info.allocationSize)
ptrAtData := sb.newState.AllocDataOrPanic(sb.ctx, NewVoidᵖ(atData.Ptr()))
sb.write(sb.cb.VkMapMemory(
dev, deviceMemory, VkDeviceSize(allocated), VkDeviceSize(info.allocationSize),
VkMemoryMapFlags(0), ptrAtData.Ptr(), VkResult_VK_SUCCESS,
).AddRead(ptrAtData.Data()).AddWrite(ptrAtData.Data()))
ptrAtData.Free()

dev, buf, deviceMemory, VkDeviceSize(bufBindingOffset), VkResult_VK_SUCCESS))
for _, r := range info.data {
var hash id.ID
var err error
Expand All @@ -245,27 +245,27 @@ func (qr *queueFamilyScratchResources) bindAndFillBuffers(totalAllocationSize ui
} else {
hash = r.hash
}
sb.ReadDataAt(hash, atData.Address()+r.rng.First, r.rng.Count)
sb.ReadDataAt(hash, atData.Address()+bufBindingOffset-allocated+r.rng.First, r.rng.Count)
}
sb.write(sb.cb.VkFlushMappedMemoryRanges(
dev,
1,
sb.MustAllocReadData(NewVkMappedMemoryRange(sb.ta,
VkStructureType_VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE, // sType
0, // pNext
deviceMemory, // memory
VkDeviceSize(allocated), // offset
VkDeviceSize(info.allocationSize), // size
)).Ptr(),
VkResult_VK_SUCCESS,
))

sb.write(sb.cb.VkUnmapMemory(dev, deviceMemory))
atData.Free()
allocated += info.allocationSize
bufBindingOffset += info.allocationSize
}
sb.write(sb.cb.VkFlushMappedMemoryRanges(
dev,
1,
sb.MustAllocReadData(NewVkMappedMemoryRange(sb.ta,
VkStructureType_VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE, // sType
0, // pNext
deviceMemory, // memory
VkDeviceSize(allocated), // offset
VkDeviceSize(totalAllocationSize), // size
)).Ptr(),
VkResult_VK_SUCCESS,
))
sb.write(sb.cb.VkUnmapMemory(dev, deviceMemory))
atData.Free()

if !usingTempMem {
qr.allocated = allocated
qr.allocated += totalAllocationSize
}
return deviceMemory, usingTempMem
}
Expand Down
66 changes: 41 additions & 25 deletions gapis/api/vulkan/state_rebuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,15 @@ func (s *State) RebuildState(ctx context.Context, oldState *api.GlobalState) ([]
}

for _, dp := range s.DescriptorPools().Keys() {
sb.createDescriptorPool(s.DescriptorPools().Get(dp))
sb.createDescriptorPoolAndAllocateDescriptorSets(s.DescriptorPools().Get(dp))
}

for _, fb := range s.Framebuffers().Keys() {
sb.createFramebuffer(s.Framebuffers().Get(fb))
}

for _, fb := range s.DescriptorSets().Keys() {
sb.createDescriptorSet(s.DescriptorSets().Get(fb))
for _, ds := range s.DescriptorSets().Keys() {
sb.writeDescriptorSet(s.DescriptorSets().Get(ds))
}

for _, qp := range s.QueryPools().Keys() {
Expand Down Expand Up @@ -2386,7 +2386,7 @@ func (sb *stateBuilder) createBufferView(bv BufferViewObjectʳ) {
))
}

func (sb *stateBuilder) createDescriptorPool(dp DescriptorPoolObjectʳ) {
func (sb *stateBuilder) createDescriptorPoolAndAllocateDescriptorSets(dp DescriptorPoolObjectʳ) {
sb.write(sb.cb.VkCreateDescriptorPool(
dp.Device(),
sb.MustAllocReadData(NewVkDescriptorPoolCreateInfo(sb.ta,
Expand All @@ -2401,6 +2401,29 @@ func (sb *stateBuilder) createDescriptorPool(dp DescriptorPoolObjectʳ) {
sb.MustAllocWriteData(dp.VulkanHandle()).Ptr(),
VkResult_VK_SUCCESS,
))

descSetHandles := make([]VkDescriptorSet, 0, dp.DescriptorSets().Len())
descSetLayoutHandles := make([]VkDescriptorSetLayout, 0, dp.DescriptorSets().Len())
for vkDescSet, descSetObj := range dp.DescriptorSets().All() {
if sb.s.DescriptorSets().Contains(vkDescSet) {
descSetHandles = append(descSetHandles, vkDescSet)
descSetLayoutHandles = append(descSetLayoutHandles, descSetObj.Layout().VulkanHandle())
}
}
if len(descSetHandles) != 0 && len(descSetLayoutHandles) != 0 {
sb.write(sb.cb.VkAllocateDescriptorSets(
dp.Device(),
sb.MustAllocReadData(NewVkDescriptorSetAllocateInfo(sb.ta,
VkStructureType_VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, // sType
0, // pNext
dp.VulkanHandle(), // descriptorPool
uint32(len(descSetHandles)), // descriptorSetCount
NewVkDescriptorSetLayoutᶜᵖ(sb.MustAllocReadData(descSetLayoutHandles).Ptr()), // pSetLayouts
)).Ptr(),
sb.MustAllocWriteData(descSetHandles).Ptr(),
VkResult_VK_SUCCESS,
))
}
}

func (sb *stateBuilder) createFramebuffer(fb FramebufferObjectʳ) {
Expand Down Expand Up @@ -2449,23 +2472,14 @@ func (sb *stateBuilder) createFramebuffer(fb FramebufferObjectʳ) {
}
}

func (sb *stateBuilder) createDescriptorSet(ds DescriptorSetObjectʳ) {
func (sb *stateBuilder) writeDescriptorSet(ds DescriptorSetObjectʳ) {
ns := GetState(sb.newState)
if !ns.DescriptorPools().Contains(ds.DescriptorPool()) {
return
}
sb.write(sb.cb.VkAllocateDescriptorSets(
ds.Device(),
sb.MustAllocReadData(NewVkDescriptorSetAllocateInfo(sb.ta,
VkStructureType_VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, // sType
0, // pNext
ds.DescriptorPool(), // descriptorPool
1, // descriptorSetCount
NewVkDescriptorSetLayoutᶜᵖ(sb.MustAllocReadData(ds.Layout().VulkanHandle()).Ptr()), // pSetLayouts
)).Ptr(),
sb.MustAllocWriteData(ds.VulkanHandle()).Ptr(),
VkResult_VK_SUCCESS,
))
if !ns.DescriptorSets().Contains(ds.VulkanHandle()) {
return
}

writes := []VkWriteDescriptorSet{}
for _, k := range ds.Bindings().Keys() {
Expand Down Expand Up @@ -2565,13 +2579,15 @@ func (sb *stateBuilder) createDescriptorSet(ds DescriptorSetObjectʳ) {
}
}
}
sb.write(sb.cb.VkUpdateDescriptorSets(
ds.Device(),
uint32(len(writes)),
sb.MustAllocReadData(writes).Ptr(),
0,
memory.Nullptr,
))
if len(writes) > 0 {
sb.write(sb.cb.VkUpdateDescriptorSets(
ds.Device(),
uint32(len(writes)),
sb.MustAllocReadData(writes).Ptr(),
0,
memory.Nullptr,
))
}
}

func (sb *stateBuilder) createQueryPool(qp QueryPoolObjectʳ) {
Expand Down Expand Up @@ -2646,7 +2662,7 @@ func (sb *stateBuilder) createCommandBuffer(cb CommandBufferObjectʳ, level VkCo
VkResult_VK_SUCCESS,
))

if cb.Recording() == RecordingState_NOT_STARTED {
if cb.Recording() == RecordingState_NOT_STARTED || cb.Recording() == RecordingState_TO_BE_RESET {
return
}

Expand Down