/
vertex.go
140 lines (108 loc) · 4.38 KB
/
vertex.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
package main
import (
"fmt"
"unsafe"
"github.com/bbredesen/go-vk"
"github.com/udhos/gwob"
)
// The Vertex struct goes away, since gwob outputs a single slice with position, texture, and normal coordinates. The
// underlying memory gets used directly and we just have to update the vertex binding and attribute descriptions.
// type Vertex struct {
// Pos vkm.Pt3
// TexCoord vkm.Pt2
// Normal vkm.Vec3
// }
var obj *gwob.Obj
var indices []uint32
func getVertexBindingDescription() vk.VertexInputBindingDescription {
return vk.VertexInputBindingDescription{
Binding: 0,
Stride: uint32(obj.StrideSize),
InputRate: vk.VERTEX_INPUT_RATE_VERTEX,
}
}
func getVertexAttributeDescriptions() []vk.VertexInputAttributeDescription {
rval := make([]vk.VertexInputAttributeDescription, 3)
rval[0] = vk.VertexInputAttributeDescription{
Location: 0,
Binding: 0,
Format: vk.FORMAT_R32G32B32_SFLOAT,
Offset: uint32(obj.StrideOffsetPosition),
}
rval[1] = vk.VertexInputAttributeDescription{
Location: 1,
Binding: 0,
Format: vk.FORMAT_R32G32B32_SFLOAT,
Offset: uint32(obj.StrideOffsetNormal),
}
rval[2] = vk.VertexInputAttributeDescription{
Location: 2,
Binding: 0,
Format: vk.FORMAT_R32G32_SFLOAT,
Offset: uint32(obj.StrideOffsetTexture),
}
return rval
}
func (app *App_06) createVertexBuffer() {
size := vk.DeviceSize(8*unsafe.Sizeof(float32(0))) * vk.DeviceSize(len(obj.Coord))
stagingBuffer, stagingBufferMemory := app.createBuffer(vk.BUFFER_USAGE_TRANSFER_SRC_BIT, size, vk.MEMORY_PROPERTY_HOST_VISIBLE_BIT|vk.MEMORY_PROPERTY_HOST_COHERENT_BIT)
ptr, err := vk.MapMemory(app.device, stagingBufferMemory, 0, size, 0)
if err != nil {
panic("Could not map memory for vertex buffer: " + err.Error())
}
vk.MemCopySlice(unsafe.Pointer(ptr), obj.Coord)
vk.UnmapMemory(app.device, stagingBufferMemory)
app.vertexBuffer, app.vertexBufferMemory = app.createBuffer(vk.BUFFER_USAGE_VERTEX_BUFFER_BIT|vk.BUFFER_USAGE_TRANSFER_DST_BIT, size, vk.MEMORY_PROPERTY_DEVICE_LOCAL_BIT)
app.copyBuffer(stagingBuffer, app.vertexBuffer, size)
vk.DestroyBuffer(app.device, stagingBuffer, nil)
vk.FreeMemory(app.device, stagingBufferMemory, nil)
}
func (app *App_06) createIndexBuffer() {
size := vk.DeviceSize(unsafe.Sizeof(indices[0])) * vk.DeviceSize(len(indices))
stagingBuffer, stagingBufferMemory := app.createBuffer(vk.BUFFER_USAGE_TRANSFER_SRC_BIT, size, vk.MEMORY_PROPERTY_HOST_VISIBLE_BIT|vk.MEMORY_PROPERTY_HOST_COHERENT_BIT)
ptr, err := vk.MapMemory(app.device, stagingBufferMemory, 0, size, 0)
if err != nil {
panic("Could not map memory for vertex buffer: " + err.Error())
}
vk.MemCopySlice(unsafe.Pointer(ptr), indices)
vk.UnmapMemory(app.device, stagingBufferMemory)
app.indexBuffer, app.indexBufferMemory = app.createBuffer(vk.BUFFER_USAGE_INDEX_BUFFER_BIT|vk.BUFFER_USAGE_TRANSFER_DST_BIT, size, vk.MEMORY_PROPERTY_DEVICE_LOCAL_BIT)
app.copyBuffer(stagingBuffer, app.indexBuffer, size)
vk.DestroyBuffer(app.device, stagingBuffer, nil)
vk.FreeMemory(app.device, stagingBufferMemory, nil)
}
func (app *App_06) findMemoryType(typeFilter uint32, fl vk.MemoryPropertyFlags) uint32 {
// fmt.Println(fl)
// _ = fl.String()
// fmt.Printf("typeFilter was %x, props were %x\n", typeFilter, fl)
// var memProps vk.PhysicalDeviceMemoryProperties = app.pdmp
var memProps vk.PhysicalDeviceMemoryProperties
memProps = vk.GetPhysicalDeviceMemoryProperties(app.physicalDevice)
memProps = vk.GetPhysicalDeviceMemoryProperties(app.physicalDevice)
var i uint32
for i = 0; i < memProps.MemoryTypeCount; i++ {
if typeFilter&(1<<i) != 0 && (memProps.MemoryTypes[i].PropertyFlags&fl) == fl {
return i
}
}
// for i = 0; i < app.pdmp.MemoryTypeCount; i++ {
// if typeFilter&(1<<i) != 0 && (app.pdmp.MemoryTypes[i].PropertyFlags&fl) == fl {
// return i
// }
// }
// fmt.Printf("memprops: %+v\n", app.pdmp)
panic(fmt.Sprintf("Could not find appropriate memory type; typeFilter was %x, props were %x, ", typeFilter, fl))
}
func (app *App_06) loadModel() {
var err error
if obj, err = gwob.NewObjFromFile("models/viking_room.obj", &gwob.ObjParserOptions{
LogStats: true,
}); err != nil {
panic("Could not load model from file: " + err.Error())
}
// Vulkan accepts index sizes only up to 32 bits, but gwob returns them as int, which will be 64 bits on most machines.
indices = make([]uint32, len(obj.Indices))
for i, v := range obj.Indices {
indices[i] = uint32(v)
}
}