-
Notifications
You must be signed in to change notification settings - Fork 0
/
op.go
274 lines (239 loc) · 7.66 KB
/
op.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
package vulkan
import (
vk "github.com/vulkan-go/vulkan"
"gorgonia.org/tensor"
"unsafe"
)
type spirvData []uint32
const spirvSliceToByteSize = 4 // num of bytes in a uint32
const pushConstantByteSize = 4 // num of bytes in a float32, used for push constants
type Op interface {
Init(params []tensor.Tensor) error
Destroy()
Record() error
}
type opAlgorithmBase struct {
algorithm
}
func newOpAlgorithmBase(e *Engine) opAlgorithmBase {
return opAlgorithmBase{
algorithm: newAlgorithm(e),
}
}
func (op *opAlgorithmBase) init(shaderFilePath string, params ...tensor.Tensor) error {
shaderFileData, err := readShaderFile(shaderFilePath)
if err != nil {
return err
}
return op.algorithm.init(shaderFileData, params)
}
func (op *opAlgorithmBase) destroy() {
op.algorithm.destroy()
}
type algorithm struct {
e *Engine
pushConstants []int32
shaderModule vk.ShaderModule
descriptorPool vk.DescriptorPool
descriptorSetLayout vk.DescriptorSetLayout
descriptorSet vk.DescriptorSet
pipelineLayout vk.PipelineLayout
pipeline vk.Pipeline
}
func newAlgorithm(e *Engine) algorithm {
return algorithm{
e: e,
}
}
func (a *algorithm) init(shaderFileData spirvData, params []tensor.Tensor) error {
if err := a.createParameters(params); err != nil {
return err
}
if err := a.createShaderModule(shaderFileData); err != nil {
return err
}
if err := a.createPipeline(); err != nil {
return err
}
return nil
}
func (a *algorithm) destroy() {
a.destroyPipeline()
a.destroyShaderModule()
a.destroyParameters()
}
func (a *algorithm) createParameters(params []tensor.Tensor) error {
//e, ok := params[0].Engine().(*Engine)
//if !ok {
// return fmt.Errorf("cannot use tensors that do not belong to the Vulkan engine")
//}
//e := params[0].e
descriptorPoolSize := vk.DescriptorPoolSize{
Type: vk.DescriptorTypeStorageBuffer,
DescriptorCount: uint32(len(params)),
}
descriptorPoolInfo := vk.DescriptorPoolCreateInfo{
SType: vk.StructureTypeDescriptorPoolCreateInfo,
Flags: 0,
MaxSets: 1,
PoolSizeCount: 1,
PPoolSizes: []vk.DescriptorPoolSize{descriptorPoolSize},
}
var descriptorPool vk.DescriptorPool
res := vk.CreateDescriptorPool(a.e.device, &descriptorPoolInfo, nil, &descriptorPool)
if res != vk.Success {
return VulkanError(res)
}
a.descriptorPool = descriptorPool
descriptorSetBindings := make([]vk.DescriptorSetLayoutBinding, len(params))
for i := 0; i < len(params); i++ {
descriptorSetBindings[i] = vk.DescriptorSetLayoutBinding{
Binding: uint32(i),
DescriptorType: vk.DescriptorTypeStorageBuffer,
DescriptorCount: 1,
StageFlags: vk.ShaderStageFlags(vk.ShaderStageComputeBit),
}
}
descriptorSetLayoutInfo := vk.DescriptorSetLayoutCreateInfo{
SType: vk.StructureTypeDescriptorSetLayoutCreateInfo,
BindingCount: uint32(len(descriptorSetBindings)),
PBindings: descriptorSetBindings,
}
var descriptorSetLayout vk.DescriptorSetLayout
res = vk.CreateDescriptorSetLayout(a.e.device, &descriptorSetLayoutInfo, nil, &descriptorSetLayout)
if res != vk.Success {
return VulkanError(res)
}
a.descriptorSetLayout = descriptorSetLayout
descriptorSetInfo := vk.DescriptorSetAllocateInfo{
SType: vk.StructureTypeDescriptorSetAllocateInfo,
DescriptorPool: descriptorPool,
DescriptorSetCount: 1,
PSetLayouts: []vk.DescriptorSetLayout{descriptorSetLayout},
}
var descriptorSet [1]vk.DescriptorSet
res = vk.AllocateDescriptorSets(a.e.device, &descriptorSetInfo, &descriptorSet[0])
if res != vk.Success {
return VulkanError(res)
}
a.descriptorSet = descriptorSet[0]
descriptorBufferInfos := make([]vk.DescriptorBufferInfo, len(params))
for i, param := range params {
hndls, err := a.e.handlesFromTensor(param)
if err != nil {
return err
}
// TODO: move this to Memory
descriptorBufferInfos[i] = vk.DescriptorBufferInfo{
Buffer: hndls.buffer,
Offset: 0,
Range: hndls.size,
}
}
writeDescriptorSet := []vk.WriteDescriptorSet{
{
SType: vk.StructureTypeWriteDescriptorSet,
DstSet: descriptorSet[0],
DstBinding: 0,
DstArrayElement: 0,
DescriptorCount: uint32(len(descriptorBufferInfos)),
DescriptorType: vk.DescriptorTypeStorageBuffer,
PBufferInfo: descriptorBufferInfos,
},
}
vk.UpdateDescriptorSets(a.e.device, 1, writeDescriptorSet, 0, nil)
return nil
}
func (a *algorithm) destroyParameters() {
vk.DestroyDescriptorSetLayout(a.e.device, a.descriptorSetLayout, nil)
vk.DestroyDescriptorPool(a.e.device, a.descriptorPool, nil)
}
func (a *algorithm) createShaderModule(shaderFileData spirvData) error {
shaderModuleCreateInfo := vk.ShaderModuleCreateInfo{
SType: vk.StructureTypeShaderModuleCreateInfo,
CodeSize: uint(len(shaderFileData) * spirvSliceToByteSize),
PCode: shaderFileData,
}
var shaderModule vk.ShaderModule
res := vk.CreateShaderModule(a.e.device, &shaderModuleCreateInfo, nil, &shaderModule)
if res != vk.Success {
return VulkanError(res)
}
a.shaderModule = shaderModule
return nil
}
func (a *algorithm) destroyShaderModule() {
vk.DestroyShaderModule(a.e.device, a.shaderModule, nil)
}
func (a *algorithm) createPipeline() error {
pipelineLayoutInfo := vk.PipelineLayoutCreateInfo{
SType: vk.StructureTypePipelineLayoutCreateInfo,
SetLayoutCount: 1,
PSetLayouts: []vk.DescriptorSetLayout{a.descriptorSetLayout},
PushConstantRangeCount: 0,
PPushConstantRanges: nil,
}
if len(a.pushConstants) > 0 {
pipelineLayoutInfo.PushConstantRangeCount = 1
pipelineLayoutInfo.PPushConstantRanges = []vk.PushConstantRange{
{
StageFlags: vk.ShaderStageFlags(vk.ShaderStageComputeBit),
Offset: 0,
Size: uint32(pushConstantByteSize * len(a.pushConstants)),
},
}
}
var pipelineLayout vk.PipelineLayout
res := vk.CreatePipelineLayout(a.e.device, &pipelineLayoutInfo, nil, &pipelineLayout)
if res != vk.Success {
return VulkanError(res)
}
// TODO: specialization info
pipelineShaderStageInfo := vk.PipelineShaderStageCreateInfo{
SType: vk.StructureTypePipelineShaderStageCreateInfo,
Flags: 0,
Stage: vk.ShaderStageComputeBit,
Module: a.shaderModule,
PName: "main\x00", // null terminated name of the entrypoint function
PSpecializationInfo: nil,
}
computePipelineInfo := []vk.ComputePipelineCreateInfo{
{
SType: vk.StructureTypeComputePipelineCreateInfo,
Flags: 0,
Stage: pipelineShaderStageInfo,
Layout: pipelineLayout,
},
}
// TODO: pipeline cache?
pipeline := make([]vk.Pipeline, len(computePipelineInfo))
res = vk.CreateComputePipelines(a.e.device, vk.NullPipelineCache, uint32(len(computePipelineInfo)), computePipelineInfo, nil, pipeline)
if res != vk.Success {
return VulkanError(res)
}
a.pipelineLayout = pipelineLayout
a.pipeline = pipeline[0]
return nil
}
func (a *algorithm) destroyPipeline() {
vk.DestroyPipeline(a.e.device, a.pipeline, nil)
vk.DestroyPipelineLayout(a.e.device, a.pipelineLayout, nil)
}
func (a *algorithm) recordDispatch(x uint32, y uint32, z uint32) {
// recordBindCore
vk.CmdBindPipeline(a.e.sequence.commandBuffer, vk.PipelineBindPointCompute, a.pipeline)
vk.CmdBindDescriptorSets(a.e.sequence.commandBuffer, vk.PipelineBindPointCompute, a.pipelineLayout, 0, 1, []vk.DescriptorSet{a.descriptorSet}, 0, nil)
// recordBindPush
if len(a.pushConstants) > 0 {
vk.CmdPushConstants(
a.e.sequence.commandBuffer,
a.pipelineLayout,
vk.ShaderStageFlags(vk.ShaderStageComputeBit),
0,
uint32(pushConstantByteSize*len(a.pushConstants)),
unsafe.Pointer(&a.pushConstants[0]),
)
}
// recordDispatch
vk.CmdDispatch(a.e.sequence.commandBuffer, x, y, z)
}