/
vertex_buffer_windows.go
470 lines (433 loc) · 11.3 KB
/
vertex_buffer_windows.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
package d3d9
import (
"syscall"
"unsafe"
)
// VertexBuffer and its methods are used to manipulate vertex buffer resources.
type VertexBuffer struct {
vtbl *vertexBufferVtbl
}
type vertexBufferVtbl struct {
QueryInterface uintptr
AddRef uintptr
Release uintptr
GetDevice uintptr
SetPrivateData uintptr
GetPrivateData uintptr
FreePrivateData uintptr
SetPriority uintptr
GetPriority uintptr
PreLoad uintptr
GetType uintptr
Lock uintptr
Unlock uintptr
GetDesc uintptr
}
// AddRef increments the reference count for an interface on an object. This
// method should be called for every new copy of a pointer to an interface on an
// object.
func (obj *VertexBuffer) AddRef() uint32 {
ret, _, _ := syscall.Syscall(
obj.vtbl.AddRef,
1,
uintptr(unsafe.Pointer(obj)),
0,
0,
)
return uint32(ret)
}
// Release has to be called when finished using the object to free its
// associated resources.
func (obj *VertexBuffer) Release() uint32 {
ret, _, _ := syscall.Syscall(
obj.vtbl.Release,
1,
uintptr(unsafe.Pointer(obj)),
0,
0,
)
return uint32(ret)
}
// GetDevice retrieves the device associated with a resource.
// Call Release on the returned device when finished using it.
func (obj *VertexBuffer) GetDevice() (device *Device, err Error) {
ret, _, _ := syscall.Syscall(
obj.vtbl.GetDevice,
2,
uintptr(unsafe.Pointer(obj)),
uintptr(unsafe.Pointer(&device)),
0,
)
err = toErr(ret)
return
}
// SetPrivateData associates data with the resource that is intended for use by
// the application, not by Direct3D. Data is passed by value, and multiple sets
// of data can be associated with a single resource.
func (obj *VertexBuffer) SetPrivateData(
refguid GUID,
data uintptr,
sizeOfData uint32,
flags uint32,
) Error {
ret, _, _ := syscall.Syscall6(
obj.vtbl.SetPrivateData,
5,
uintptr(unsafe.Pointer(obj)),
uintptr(unsafe.Pointer(&refguid)),
data,
uintptr(sizeOfData),
uintptr(flags),
0,
)
return toErr(ret)
}
// SetPrivateDataBytes associates data with the resource that is intended for
// use by the application, not by Direct3D. Data is passed by value, and
// multiple sets of data can be associated with a single resource.
func (obj *VertexBuffer) SetPrivateDataBytes(
refguid GUID,
data []byte,
flags uint32,
) Error {
return obj.SetPrivateData(
refguid,
uintptr(unsafe.Pointer(&data[0])),
uint32(len(data)),
flags,
)
}
// GetPrivateData copies the private data associated with the resource to a
// provided buffer.
func (obj *VertexBuffer) GetPrivateData(refguid GUID) (data []byte, err Error) {
// first get the data size by passing nil as the data pointer
var sizeInBytes uint
ret, _, _ := syscall.Syscall6(
obj.vtbl.GetPrivateData,
4,
uintptr(unsafe.Pointer(obj)),
uintptr(unsafe.Pointer(&refguid)),
0,
uintptr(unsafe.Pointer(&sizeInBytes)),
0,
0,
)
if err := toErr(ret); err != nil {
return nil, err
}
data = make([]byte, sizeInBytes)
ret, _, _ = syscall.Syscall6(
obj.vtbl.GetPrivateData,
4,
uintptr(unsafe.Pointer(obj)),
uintptr(unsafe.Pointer(&refguid)),
uintptr(unsafe.Pointer(&data[0])),
uintptr(unsafe.Pointer(&sizeInBytes)),
0,
0,
)
return data, toErr(ret)
}
// FreePrivateData frees the specified private data associated with this
// resource.
func (obj *VertexBuffer) FreePrivateData(refguid GUID) Error {
ret, _, _ := syscall.Syscall(
obj.vtbl.FreePrivateData,
2,
uintptr(unsafe.Pointer(obj)),
uintptr(unsafe.Pointer(&refguid)),
0,
)
return toErr(ret)
}
// SetPriority assigns the priority of a resource for scheduling purposes.
func (obj *VertexBuffer) SetPriority(priorityNew uint32) uint32 {
ret, _, _ := syscall.Syscall(
obj.vtbl.SetPriority,
2,
uintptr(unsafe.Pointer(obj)),
uintptr(priorityNew),
0,
)
return uint32(ret)
}
// GetPriority retrieves the priority for this resource.
func (obj *VertexBuffer) GetPriority() uint32 {
ret, _, _ := syscall.Syscall(
obj.vtbl.GetPriority,
1,
uintptr(unsafe.Pointer(obj)),
0,
0,
)
return uint32(ret)
}
// PreLoad preloads a managed resource.
func (obj *VertexBuffer) PreLoad() {
syscall.Syscall(
obj.vtbl.PreLoad,
1,
uintptr(unsafe.Pointer(obj)),
0,
0,
)
}
// GetType returns the type of the resource.
func (obj *VertexBuffer) GetType() RESOURCETYPE {
ret, _, _ := syscall.Syscall(
obj.vtbl.GetType,
1,
uintptr(unsafe.Pointer(obj)),
0,
0,
)
return RESOURCETYPE(ret)
}
// Lock locks a range of vertex data and obtains a pointer to the vertex buffer
// memory.
func (obj *VertexBuffer) Lock(
offsetToLock uint,
sizeToLock uint,
flags uint32,
) (data VertexBufferMemory, err Error) {
var dataPtr uintptr
ret, _, _ := syscall.Syscall6(
obj.vtbl.Lock,
5,
uintptr(unsafe.Pointer(obj)),
uintptr(offsetToLock),
uintptr(sizeToLock),
uintptr(unsafe.Pointer(&dataPtr)),
uintptr(flags),
0,
)
err = toErr(ret)
data = VertexBufferMemory{Memory: dataPtr}
return
}
// Unlock unlocks vertex data.
func (obj *VertexBuffer) Unlock() Error {
ret, _, _ := syscall.Syscall(
obj.vtbl.Unlock,
1,
uintptr(unsafe.Pointer(obj)),
0,
0,
)
return toErr(ret)
}
// GetDesc retrieves a description of the vertex buffer resource.
func (obj *VertexBuffer) GetDesc() (desc VERTEXBUFFER_DESC, err Error) {
ret, _, _ := syscall.Syscall(
obj.vtbl.GetDesc,
2,
uintptr(unsafe.Pointer(obj)),
uintptr(unsafe.Pointer(&desc)),
0,
)
err = toErr(ret)
return
}
// VertexBufferMemory encapsulates a raw memory pointer and provides methods to
// get and set typical slice types.
type VertexBufferMemory struct {
Memory uintptr
}
// SetFloat32s copies the given slice to the memory, starting at the given
// offset in bytes.
func (m VertexBufferMemory) SetFloat32s(offsetInBytes int, data []float32) {
if len(data) > 0 {
dest := m.Memory + uintptr(offsetInBytes)
for i := range data {
*((*float32)(unsafe.Pointer(dest))) = data[i]
dest += 4
}
}
}
// SetFloat64s copies the given slice to the memory, starting at the given
// offset in bytes.
func (m VertexBufferMemory) SetFloat64s(offsetInBytes int, data []float64) {
if len(data) > 0 {
dest := m.Memory + uintptr(offsetInBytes)
for i := range data {
*((*float64)(unsafe.Pointer(dest))) = data[i]
dest += 8
}
}
}
// SetInt32s copies the given slice to the memory, starting at the given
// offset in bytes.
func (m VertexBufferMemory) SetInt32s(offsetInBytes int, data []int32) {
if len(data) > 0 {
dest := m.Memory + uintptr(offsetInBytes)
for i := range data {
*((*int32)(unsafe.Pointer(dest))) = data[i]
dest += 4
}
}
}
// SetUint32s copies the given slice to the memory, starting at the given
// offset in bytes.
func (m VertexBufferMemory) SetUint32s(offsetInBytes int, data []uint32) {
if len(data) > 0 {
dest := m.Memory + uintptr(offsetInBytes)
for i := range data {
*((*uint32)(unsafe.Pointer(dest))) = data[i]
dest += 4
}
}
}
// SetInt16s copies the given slice to the memory, starting at the given
// offset in bytes.
func (m VertexBufferMemory) SetInt16s(offsetInBytes int, data []int16) {
if len(data) > 0 {
dest := m.Memory + uintptr(offsetInBytes)
for i := range data {
*((*int16)(unsafe.Pointer(dest))) = data[i]
dest += 2
}
}
}
// SetUint16s copies the given slice to the memory, starting at the given
// offset in bytes.
func (m VertexBufferMemory) SetUint16s(offsetInBytes int, data []uint16) {
if len(data) > 0 {
dest := m.Memory + uintptr(offsetInBytes)
for i := range data {
*((*uint16)(unsafe.Pointer(dest))) = data[i]
dest += 2
}
}
}
// SetInt8s copies the given slice to the memory, starting at the given
// offset in bytes.
func (m VertexBufferMemory) SetInt8s(offsetInBytes int, data []int8) {
if len(data) > 0 {
dest := m.Memory + uintptr(offsetInBytes)
for i := range data {
*((*int8)(unsafe.Pointer(dest))) = data[i]
dest++
}
}
}
// SetUint8s copies the given slice to the memory, starting at the given offset
// in bytes.
func (m VertexBufferMemory) SetUint8s(offsetInBytes int, data []uint8) {
if len(data) > 0 {
dest := m.Memory + uintptr(offsetInBytes)
for i := range data {
*((*uint8)(unsafe.Pointer(dest))) = data[i]
dest++
}
}
}
// SetBytes copies the given slice to the memory, starting at the given offset
// in bytes.
func (m VertexBufferMemory) SetBytes(offsetInBytes int, data []byte) {
if len(data) > 0 {
dest := m.Memory + uintptr(offsetInBytes)
for i := range data {
*((*byte)(unsafe.Pointer(dest))) = data[i]
dest++
}
}
}
// GetFloat32s copies data from memory to the given slice, starting at the given
// offset in bytes.
func (m VertexBufferMemory) GetFloat32s(offsetInBytes int, data []float32) {
if len(data) > 0 {
src := m.Memory + uintptr(offsetInBytes)
for i := range data {
data[i] = *((*float32)(unsafe.Pointer(src)))
src += 4
}
}
}
// GetFloat64s copies data from memory to the given slice, starting at the given
// offset in bytes.
func (m VertexBufferMemory) GetFloat64s(offsetInBytes int, data []float64) {
if len(data) > 0 {
src := m.Memory + uintptr(offsetInBytes)
for i := range data {
data[i] = *((*float64)(unsafe.Pointer(src)))
src += 8
}
}
}
// GetInt32s copies data from memory to the given slice, starting at the given
// offset in bytes.
func (m VertexBufferMemory) GetInt32s(offsetInBytes int, data []int32) {
if len(data) > 0 {
src := m.Memory + uintptr(offsetInBytes)
for i := range data {
data[i] = *((*int32)(unsafe.Pointer(src)))
src += 4
}
}
}
// GetUint32s copies data from memory to the given slice, starting at the given
// offset in bytes.
func (m VertexBufferMemory) GetUint32s(offsetInBytes int, data []uint32) {
if len(data) > 0 {
src := m.Memory + uintptr(offsetInBytes)
for i := range data {
data[i] = *((*uint32)(unsafe.Pointer(src)))
src += 4
}
}
}
// GetInt16s copies data from memory to the given slice, starting at the given
// offset in bytes.
func (m VertexBufferMemory) GetInt16s(offsetInBytes int, data []int16) {
if len(data) > 0 {
src := m.Memory + uintptr(offsetInBytes)
for i := range data {
data[i] = *((*int16)(unsafe.Pointer(src)))
src += 2
}
}
}
// GetUint16s copies data from memory to the given slice, starting at the given
// offset in bytes.
func (m VertexBufferMemory) GetUint16s(offsetInBytes int, data []uint16) {
if len(data) > 0 {
src := m.Memory + uintptr(offsetInBytes)
for i := range data {
data[i] = *((*uint16)(unsafe.Pointer(src)))
src += 2
}
}
}
// GetInt8s copies data from memory to the given slice, starting at the given
// offset in bytes.
func (m VertexBufferMemory) GetInt8s(offsetInBytes int, data []int8) {
if len(data) > 0 {
src := m.Memory + uintptr(offsetInBytes)
for i := range data {
data[i] = *((*int8)(unsafe.Pointer(src)))
src++
}
}
}
// GetUint8s copies data from memory to the given slice, starting at the given
// offset in bytes.
func (m VertexBufferMemory) GetUint8s(offsetInBytes int, data []uint8) {
if len(data) > 0 {
src := m.Memory + uintptr(offsetInBytes)
for i := range data {
data[i] = *((*uint8)(unsafe.Pointer(src)))
src++
}
}
}
// GetBytes copies data from memory to the given slice, starting at the given
// offset in bytes.
func (m VertexBufferMemory) GetBytes(offsetInBytes int, data []byte) {
if len(data) > 0 {
src := m.Memory + uintptr(offsetInBytes)
for i := range data {
data[i] = *((*byte)(unsafe.Pointer(src)))
src++
}
}
}