-
Notifications
You must be signed in to change notification settings - Fork 54
/
util.go
122 lines (100 loc) · 2.81 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
package vulkan
import (
"bytes"
"fmt"
"unsafe"
)
// #include "vulkan/vulkan.h"
import "C"
// Max bounds of uint32 and uint64,
// declared as var so type would get checked.
var (
MaxUint32 uint32 = 1<<32 - 1 // also ^uint32(0)
MaxUint64 uint64 = 1<<64 - 1 // also ^uint64(0)
)
func (b Bool32) B() bool {
return b == True
}
type Version uint32
func (v Version) String() string {
return fmt.Sprintf("%d.%d.%d", v.Major(), v.Minor(), v.Patch())
}
func (v Version) Major() int {
return int(uint32(v) >> 22)
}
func (v Version) Minor() int {
return int(uint32(v) >> 12 & 0x3FF)
}
func (v Version) Patch() int {
return int(uint32(v) & 0xFFF)
}
func MakeVersion(major, minor, patch int) uint32 {
return uint32(major)<<22 | uint32(minor)<<12 | uint32(patch)
}
func ToString(buf []byte) string {
var str bytes.Buffer
for i := range buf {
if buf[i] == '\x00' {
return str.String()
}
str.WriteByte(buf[i])
}
return str.String()
}
// deprecated
func FindMemoryTypeIndex(dev PhysicalDevice,
typeBits uint32, reqMask MemoryPropertyFlagBits) (uint32, bool) {
var memProperties PhysicalDeviceMemoryProperties
GetPhysicalDeviceMemoryProperties(dev, &memProperties)
memProperties.Deref()
var memFlags = MemoryPropertyFlags(reqMask)
// search memtypes to find the first index with those requirements
for i := 0; i < 32; i++ {
if typeBits&1 == 1 { // type is available
memType := memProperties.MemoryTypes[i]
memType.Deref()
if memType.PropertyFlags&memFlags == memFlags {
return uint32(i), true
}
}
typeBits = typeBits >> 1
}
return 0, false
}
// Memcopy is like a Go's built-in copy function, it copies data from src slice,
// but into a destination pointer. Useful to copy data into device memory.
func Memcopy(dst unsafe.Pointer, src []byte) int {
const m = 0x7fffffff
dstView := (*[m]byte)(dst)
return copy(dstView[:len(src)], src)
}
// Memview returns a pointer to user data, so Vulkan runtime in userspace can peek.
func Memview(data []byte) unsafe.Pointer {
return unsafe.Pointer((*sliceHeader)(unsafe.Pointer(&data)).Data)
}
func NewClearValue(color []float32) ClearValue {
var v ClearValue
v.SetColor(color)
return v
}
func NewClearDepthStencil(depth float32, stencil uint32) ClearValue {
var v ClearValue
v.SetDepthStencil(depth, stencil)
return v
}
func (cv *ClearValue) SetColor(color []float32) {
vkClearValue := (*[4]float32)(unsafe.Pointer(cv))
for i := 0; i < len(color); i++ {
vkClearValue[i] = color[i]
}
}
func (cv *ClearValue) SetDepthStencil(depth float32, stencil uint32) {
depths := (*[2]float32)(unsafe.Pointer(cv))
stencils := (*[2]uint32)(unsafe.Pointer(cv))
depths[0] = depth
stencils[1] = stencil
}
// SurfaceFromPointer casts a pointer to a Vulkan surface into a Surface.
func SurfaceFromPointer(surface uintptr) Surface {
return *(*Surface)(unsafe.Pointer(surface))
}