-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_primer_resources.go
300 lines (275 loc) · 10.3 KB
/
image_primer_resources.go
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
// Copyright (C) 2019 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package vulkan
import "github.com/google/gapid/gapis/memory"
// descriptorSetPoolReservation is the result of descriptor set reservation
// request to homoDescriptorSetPool. It contains the descriptor sets reserved
// for the requester to use. It implements the flushablePiece interface.
type descriptorSetPoolReservation struct {
valid bool
descSets []VkDescriptorSet
owner flushableResource
}
// IsValid implements the flushablePiece interface, to show if the descriptor
// set reserved in this reservation are still valid to use.
func (res *descriptorSetPoolReservation) IsValid() bool {
return res.valid
}
// Owner implements the flushablePiece interface, returns the descriptor set
// pool from where this reservation was made.
func (res *descriptorSetPoolReservation) Owner() flushableResource {
return res.owner
}
// DescriptorSets returns a list of reserved descriptor sets.
func (res *descriptorSetPoolReservation) DescriptorSets() []VkDescriptorSet {
return res.descSets
}
// homoDescriptorSetPool is a pool of homogeneous descriptor sets allocated with
// exact same layout and from same descriptor pool. It implements the
// flushableResource interface so all the descriptor sets reserved from it have
// their life time managed by this descriptor set pool.
type homoDescriptorSetPool struct {
name debugMarkerName
dev VkDevice
layout VkDescriptorSetLayout
next uint32
capacity uint32
noFlushUntilFree bool
pools []VkDescriptorPool
descSets []VkDescriptorSet
users map[flushableResourceUser]struct{}
validReservations []*descriptorSetPoolReservation
}
// newHomoDescriptorSetPool creates a new homoDescriptorPool for the given
// descriptor set layout, and a pool with the given initial count of descriptor
// sets. If notFlushUntilFree is true, the pool will not invalidate any
// descriptor sets reserved from this pool, otherwise, only the descriptor sets
// reserved in the last reservation request is guanranteed to be valid to use,
// a new incoming reservation request may trigger a flush and invalidate
// previously reserved descriptor sets.
func newHomoDescriptorSetPool(sb *stateBuilder, nm debugMarkerName, dev VkDevice, layout VkDescriptorSetLayout, initialSetCount uint32, noFlushUntilFree bool) *homoDescriptorSetPool {
p := &homoDescriptorSetPool{
name: nm,
dev: dev,
layout: layout,
next: 0,
capacity: initialSetCount,
noFlushUntilFree: noFlushUntilFree,
pools: []VkDescriptorPool{},
descSets: []VkDescriptorSet{},
users: map[flushableResourceUser]struct{}{},
validReservations: []*descriptorSetPoolReservation{},
}
p.pools = append(p.pools, p.createDescriptorPool(sb, p.capacity))
p.descSets = append(p.descSets, p.allocateDescriptorSets(sb, p.pools[0], p.capacity)...)
return p
}
// ReserveDescriptorSets reserves descriptor sets from this
// homoDescriptorSetPool for a specific count, returns a reservation that
// contains the reserved descriptor sets, and error. If the
// homoDescriptorSetPool was created with noFlushUntilFree set to false, call
// to this function may trigger a flush and all the previously reserved
// descriptor sets might be flushed.
func (dss *homoDescriptorSetPool) ReserveDescriptorSets(sb *stateBuilder, count uint32) (*descriptorSetPoolReservation, error) {
if count+dss.next > dss.capacity {
if dss.noFlushUntilFree {
newDescSetCount := count + dss.next - dss.capacity
pool := dss.createDescriptorPool(sb, newDescSetCount)
newDescSets := dss.allocateDescriptorSets(sb, pool, newDescSetCount)
dss.pools = append(dss.pools, pool)
dss.descSets = append(dss.descSets, newDescSets...)
dss.capacity += newDescSetCount
} else {
if count > dss.capacity {
dss.capacity = count
}
dss.flush(sb)
}
return dss.ReserveDescriptorSets(sb, count)
}
current := dss.next
dss.next += count
reservation := &descriptorSetPoolReservation{
valid: true,
descSets: dss.descSets[current : current+count],
owner: dss,
}
return reservation, nil
}
func (dss *homoDescriptorSetPool) createDescriptorPool(sb *stateBuilder, setCount uint32) VkDescriptorPool {
layoutObj := GetState(sb.newState).DescriptorSetLayouts().Get(dss.layout)
countPerType := map[VkDescriptorType]uint32{}
for _, info := range layoutObj.Bindings().All() {
countPerType[info.Type()] += info.Count()
}
for t := range countPerType {
countPerType[t] *= setCount
}
poolSizes := []VkDescriptorPoolSize{}
for t, c := range countPerType {
poolSizes = append(poolSizes, NewVkDescriptorPoolSize(sb.ta, t, c))
}
handle := VkDescriptorPool(newUnusedID(true, func(x uint64) bool {
return GetState(sb.newState).DescriptorPools().Contains(VkDescriptorPool(x)) || GetState(sb.oldState).DescriptorPools().Contains(VkDescriptorPool(x))
}))
sb.write(sb.cb.VkCreateDescriptorPool(
dss.dev,
sb.MustAllocReadData(NewVkDescriptorPoolCreateInfo(sb.ta,
VkStructureType_VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, // sType
0, // pNext
VkDescriptorPoolCreateFlags(
VkDescriptorPoolCreateFlagBits_VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT), // flags
setCount, // maxSets
uint32(len(poolSizes)), // poolSizeCount
NewVkDescriptorPoolSizeᶜᵖ(sb.MustAllocReadData(poolSizes).Ptr()), // pPoolSizes
)).Ptr(),
memory.Nullptr,
sb.MustAllocWriteData(handle).Ptr(),
VkResult_VK_SUCCESS,
))
if len(dss.name) > 0 {
attachDebugMarkerName(sb, dss.name, dss.dev, handle)
}
return handle
}
func (dss *homoDescriptorSetPool) destroyAllDescriptorPool(sb *stateBuilder) {
for _, pool := range dss.pools {
sb.write(sb.cb.VkDestroyDescriptorPool(dss.dev, pool, memory.Nullptr))
}
dss.pools = []VkDescriptorPool{}
}
func (dss *homoDescriptorSetPool) allocateDescriptorSets(sb *stateBuilder, pool VkDescriptorPool, count uint32) []VkDescriptorSet {
newSets := make([]VkDescriptorSet, count)
for i := range newSets {
newSets[i] = VkDescriptorSet(
newUnusedID(true, func(x uint64) bool {
return GetState(sb.newState).DescriptorSets().Contains(VkDescriptorSet(x))
}))
}
layoutSlice := make([]VkDescriptorSetLayout, count)
for i := range layoutSlice {
layoutSlice[i] = dss.layout
}
sb.write(sb.cb.VkAllocateDescriptorSets(
dss.dev,
sb.MustAllocReadData(NewVkDescriptorSetAllocateInfo(sb.ta,
VkStructureType_VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, // sType
0, // pNext
pool, // descriptorPool
count, // descriptorSetCount
NewVkDescriptorSetLayoutᶜᵖ(sb.MustAllocReadData(layoutSlice).Ptr()), // pSetLayouts
)).Ptr(),
sb.MustAllocWriteData(newSets).Ptr(),
VkResult_VK_SUCCESS,
))
if len(dss.name) > 0 {
for _, set := range newSets {
attachDebugMarkerName(sb, dss.name, dss.dev, set)
}
}
return newSets
}
// flush implements the flushableResource interface.
func (dss *homoDescriptorSetPool) flush(sb *stateBuilder) {
for u := range dss.users {
u.OnResourceFlush(sb, dss)
delete(dss.users, u)
}
for _, res := range dss.validReservations {
res.valid = false
}
dss.validReservations = []*descriptorSetPoolReservation{}
dss.destroyAllDescriptorPool(sb)
if dss.layout != VkDescriptorSetLayout(0) {
pool := dss.createDescriptorPool(sb, dss.capacity)
dss.pools = append(dss.pools, pool)
dss.descSets = dss.allocateDescriptorSets(sb, pool, dss.capacity)
}
dss.next = 0
}
// AddUser implements the flushableResource interface. It registers a user of
// the descriptor sets reserved from this pool.
func (dss *homoDescriptorSetPool) AddUser(user flushableResourceUser) {
dss.users[user] = struct{}{}
}
// DropUser implements the flushableResource interface. It removes a user from
// the user list of this pool.
func (dss *homoDescriptorSetPool) DropUser(user flushableResourceUser) {
if _, ok := dss.users[user]; ok {
delete(dss.users, user)
}
}
// Free flushes all the descriptor sets reserved in this pool.
func (dss *homoDescriptorSetPool) Free(sb *stateBuilder) {
dss.layout = VkDescriptorSetLayout(0)
dss.flush(sb)
dss.descSets = nil
}
// naiveImageViewPool is a simple map based pool of VkImageView.
type naiveImageViewPool struct {
dev VkDevice
views map[ipImageViewInfo]VkImageView
}
func newNaiveImageViewPool(dev VkDevice) *naiveImageViewPool {
return &naiveImageViewPool{
dev: dev,
views: map[ipImageViewInfo]VkImageView{},
}
}
func (p *naiveImageViewPool) getOrCreateImageView(sb *stateBuilder, nm debugMarkerName, info ipImageViewInfo) VkImageView {
if v, ok := p.views[info]; ok {
return v
}
handle := ipCreateImageView(sb, nm, p.dev, info)
p.views[info] = handle
return handle
}
// Free destroyes all the image views in this pool.
func (p *naiveImageViewPool) Free(sb *stateBuilder) {
for _, v := range p.views {
sb.write(sb.cb.VkDestroyImageView(p.dev, v, memory.Nullptr))
}
p.views = map[ipImageViewInfo]VkImageView{}
return
}
// naiveShaderModulePool is a simple map based pool of VkShaderModule
type naiveShaderModulePool struct {
dev VkDevice
shaders map[ipShaderModuleInfo]VkShaderModule
}
func newNaiveShaderModulePool(dev VkDevice) *naiveShaderModulePool {
return &naiveShaderModulePool{
dev: dev,
shaders: map[ipShaderModuleInfo]VkShaderModule{},
}
}
func (p *naiveShaderModulePool) getOrCreateShaderModule(sb *stateBuilder, nm debugMarkerName, info ipShaderModuleInfo) VkShaderModule {
if s, ok := p.shaders[info]; ok {
return s
}
handle, err := ipCreateShaderModule(sb, nm, p.dev, info)
if err != nil {
panic(err)
}
p.shaders[info] = handle
return handle
}
// Free destroyes all the shader modules in this pool
func (p *naiveShaderModulePool) Free(sb *stateBuilder) {
for _, s := range p.shaders {
sb.write(sb.cb.VkDestroyShaderModule(p.dev, s, memory.Nullptr))
}
p.shaders = map[ipShaderModuleInfo]VkShaderModule{}
return
}