-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.go
188 lines (165 loc) · 5.3 KB
/
util.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
package asche
import (
"log"
"unsafe"
vk "github.com/vulkan-go/vulkan"
)
// InstanceExtensions gets a list of instance extensions available on the platform.
func InstanceExtensions() (names []string, err error) {
defer checkErr(&err)
var count uint32
ret := vk.EnumerateInstanceExtensionProperties("", &count, nil)
orPanic(NewError(ret))
list := make([]vk.ExtensionProperties, count)
ret = vk.EnumerateInstanceExtensionProperties("", &count, list)
orPanic(NewError(ret))
for _, ext := range list {
ext.Deref()
names = append(names, vk.ToString(ext.ExtensionName[:]))
}
return names, err
}
// DeviceExtensions gets a list of instance extensions available on the provided physical device.
func DeviceExtensions(gpu vk.PhysicalDevice) (names []string, err error) {
defer checkErr(&err)
var count uint32
ret := vk.EnumerateDeviceExtensionProperties(gpu, "", &count, nil)
orPanic(NewError(ret))
list := make([]vk.ExtensionProperties, count)
ret = vk.EnumerateDeviceExtensionProperties(gpu, "", &count, list)
orPanic(NewError(ret))
for _, ext := range list {
ext.Deref()
names = append(names, vk.ToString(ext.ExtensionName[:]))
}
return names, err
}
// ValidationLayers gets a list of validation layers available on the platform.
func ValidationLayers() (names []string, err error) {
defer checkErr(&err)
var count uint32
ret := vk.EnumerateInstanceLayerProperties(&count, nil)
orPanic(NewError(ret))
list := make([]vk.LayerProperties, count)
ret = vk.EnumerateInstanceLayerProperties(&count, list)
orPanic(NewError(ret))
for _, layer := range list {
layer.Deref()
names = append(names, vk.ToString(layer.LayerName[:]))
}
return names, err
}
func FindRequiredMemoryType(props vk.PhysicalDeviceMemoryProperties,
deviceRequirements, hostRequirements vk.MemoryPropertyFlagBits) (uint32, bool) {
for i := uint32(0); i < vk.MaxMemoryTypes; i++ {
if deviceRequirements&(vk.MemoryPropertyFlagBits(1)<<i) != 0 {
props.MemoryTypes[i].Deref()
flags := props.MemoryTypes[i].PropertyFlags
if flags&vk.MemoryPropertyFlags(hostRequirements) != 0 {
return i, true
}
}
}
return 0, false
}
func FindRequiredMemoryTypeFallback(props vk.PhysicalDeviceMemoryProperties,
deviceRequirements, hostRequirements vk.MemoryPropertyFlagBits) (uint32, bool) {
for i := uint32(0); i < vk.MaxMemoryTypes; i++ {
if deviceRequirements&(vk.MemoryPropertyFlagBits(1)<<i) != 0 {
props.MemoryTypes[i].Deref()
flags := props.MemoryTypes[i].PropertyFlags
if flags&vk.MemoryPropertyFlags(hostRequirements) != 0 {
return i, true
}
}
}
// Fallback to the first one available.
if hostRequirements != 0 {
return FindRequiredMemoryType(props, deviceRequirements, 0)
}
return 0, false
}
type Buffer struct {
// device for destroy purposes.
device vk.Device
// Buffer is the buffer object.
Buffer vk.Buffer
// Memory is the device memory backing buffer object.
Memory vk.DeviceMemory
}
func (b *Buffer) Destroy() {
vk.FreeMemory(b.device, b.Memory, nil)
vk.DestroyBuffer(b.device, b.Buffer, nil)
b.device = nil
}
func CreateBuffer(device vk.Device, memProps vk.PhysicalDeviceMemoryProperties,
data []byte, usage vk.BufferUsageFlagBits) *Buffer {
var buffer vk.Buffer
var memory vk.DeviceMemory
ret := vk.CreateBuffer(device, &vk.BufferCreateInfo{
SType: vk.StructureTypeBufferCreateInfo,
Usage: vk.BufferUsageFlags(usage),
Size: vk.DeviceSize(len(data)),
}, nil, &buffer)
orPanic(NewError(ret))
// Ask device about its memory requirements.
var memReqs vk.MemoryRequirements
vk.GetBufferMemoryRequirements(device, buffer, &memReqs)
memReqs.Deref()
memType, ok := FindRequiredMemoryType(memProps, vk.MemoryPropertyFlagBits(memReqs.MemoryTypeBits),
vk.MemoryPropertyHostVisibleBit|vk.MemoryPropertyHostCoherentBit)
if !ok {
log.Println("vulkan warning: failed to find required memory type")
}
// Allocate device memory and bind to the buffer.
ret = vk.AllocateMemory(device, &vk.MemoryAllocateInfo{
SType: vk.StructureTypeMemoryAllocateInfo,
AllocationSize: memReqs.Size,
MemoryTypeIndex: memType,
}, nil, &memory)
orPanic(NewError(ret), func() {
vk.DestroyBuffer(device, buffer, nil)
})
vk.BindBufferMemory(device, buffer, memory, 0)
b := &Buffer{
device: device,
Buffer: buffer,
Memory: memory,
}
// Map the memory and dump data in there.
if len(data) > 0 {
var pData unsafe.Pointer
ret := vk.MapMemory(device, memory, 0, vk.DeviceSize(len(data)), 0, &pData)
if isError(ret) {
log.Printf("vulkan warning: failed to map device memory for data (len=%d)", len(data))
return b
}
n := vk.Memcopy(pData, data)
if n != len(data) {
log.Printf("vulkan warning: failed to copy data, %d != %d", n, len(data))
}
vk.UnmapMemory(device, memory)
}
return b
}
func LoadShaderModule(device vk.Device, data []byte) (vk.ShaderModule, error) {
var module vk.ShaderModule
ret := vk.CreateShaderModule(device, &vk.ShaderModuleCreateInfo{
SType: vk.StructureTypeShaderModuleCreateInfo,
CodeSize: uint(len(data)),
PCode: sliceUint32(data),
}, nil, &module)
if isError(ret) {
return vk.NullShaderModule, NewError(ret)
}
return module, nil
}
func sliceUint32(data []byte) []uint32 {
const m = 0x7fffffff
return (*[m / 4]uint32)(unsafe.Pointer((*sliceHeader)(unsafe.Pointer(&data)).Data))[:len(data)/4]
}
type sliceHeader struct {
Data uintptr
Len int
Cap int
}