forked from hupe1980/go-mtl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_encoder.go
More file actions
151 lines (133 loc) Β· 5.67 KB
/
command_encoder.go
File metadata and controls
151 lines (133 loc) Β· 5.67 KB
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
//go:build darwin
// +build darwin
package mtl
/*
#include "command_encoder.h"
*/
import "C"
import (
"unsafe"
)
// CommandEncoder is an encoder that writes sequential GPU commands
// into a command buffer.
//
// Reference: https://developer.apple.com/documentation/metal/mtlcommandencoder
type CommandEncoder struct {
commandEncoder unsafe.Pointer
}
// EndEncoding declares that all command generation from this encoder is completed.
//
// Reference: https://developer.apple.com/documentation/metal/mtlcommandencoder/1458038-endencoding
func (ce CommandEncoder) EndEncoding() {
C.CommandEncoder_EndEncoding(ce.commandEncoder)
}
// ComputeCommandEncoder is an object for encoding commands in a compute pass.
//
// Reference: https://developer.apple.com/documentation/metal/mtlcomputecommandencoder
type ComputeCommandEncoder struct {
CommandEncoder
}
// SetComputePipelineState sets the current compute pipeline state object for the compute command encoder.
//
// Reference: https://developer.apple.com/documentation/metal/mtlcomputecommandencoder/1443140-setcomputepipelinestate
func (cce ComputeCommandEncoder) SetComputePipelineState(cps ComputePipelineState) {
C.ComputeCommandEncoder_SetComputePipelineState(cce.commandEncoder, cps.computePipelineState)
}
// SetBuffer sets a buffer for the compute function at a specified index.
//
// Reference: https://developer.apple.com/documentation/metal/mtlcomputecommandencoder/1443126-setbuffer
func (cce ComputeCommandEncoder) SetBuffer(buf Buffer, offset, index int) {
C.ComputeCommandEncoder_SetBuffer(cce.commandEncoder, buf.buffer, C.uint_t(offset), C.uint_t(index))
}
// DispatchThreads encodes a compute command using an arbitrarily sized grid.
//
// Reference: https://developer.apple.com/documentation/metal/mtlcomputecommandencoder/2866532-dispatchthreads
func (cce ComputeCommandEncoder) DispatchThreads(gridSize, threadgroupSize Size) {
gs := C.struct_Size{
Width: C.uint_t(gridSize.Width),
Height: C.uint_t(gridSize.Height),
Depth: C.uint_t(gridSize.Depth),
}
tgs := C.struct_Size{
Width: C.uint_t(threadgroupSize.Width),
Height: C.uint_t(threadgroupSize.Height),
Depth: C.uint_t(threadgroupSize.Depth),
}
C.ComputeCommandEncoder_DispatchThreads(cce.commandEncoder, gs, tgs)
}
// RenderCommandEncoder is an encoder that specifies graphics-rendering commands
// and executes graphics functions.
//
// Reference: https://developer.apple.com/documentation/metal/mtlrendercommandencoder.
type RenderCommandEncoder struct {
CommandEncoder
}
// SetRenderPipelineState sets the current render pipeline state object.
//
// Reference: https://developer.apple.com/documentation/metal/mtlrendercommandencoder/1515811-setrenderpipelinestate.
func (rce RenderCommandEncoder) SetRenderPipelineState(rps RenderPipelineState) {
C.RenderCommandEncoder_SetRenderPipelineState(rce.commandEncoder, rps.renderPipelineState)
}
// SetVertexBuffer sets a buffer for the vertex shader function at an index
// in the buffer argument table with an offset that specifies the start of the data.
//
// Reference: https://developer.apple.com/documentation/metal/mtlrendercommandencoder/1515829-setvertexbuffer.
func (rce RenderCommandEncoder) SetVertexBuffer(buf Buffer, offset, index int) {
C.RenderCommandEncoder_SetVertexBuffer(rce.commandEncoder, buf.buffer, C.uint_t(offset), C.uint_t(index))
}
// SetVertexBytes sets a block of data for the vertex function.
//
// Reference: https://developer.apple.com/documentation/metal/mtlrendercommandencoder/1515846-setvertexbytes.
func (rce RenderCommandEncoder) SetVertexBytes(bytes unsafe.Pointer, length uintptr, index int) {
C.RenderCommandEncoder_SetVertexBytes(rce.commandEncoder, bytes, C.size_t(length), C.uint_t(index))
}
// DrawPrimitives renders one instance of primitives using vertex data
// in contiguous array elements.
//
// Reference: https://developer.apple.com/documentation/metal/mtlrendercommandencoder/1516326-drawprimitives.
func (rce RenderCommandEncoder) DrawPrimitives(typ PrimitiveType, vertexStart, vertexCount int) {
C.RenderCommandEncoder_DrawPrimitives(rce.commandEncoder, C.uint8_t(typ), C.uint_t(vertexStart), C.uint_t(vertexCount))
}
// BlitCommandEncoder is an encoder that specifies resource copy
// and resource synchronization commands.
//
// Reference: https://developer.apple.com/documentation/metal/mtlblitcommandencoder.
type BlitCommandEncoder struct {
CommandEncoder
}
// CopyFromTexture encodes a command to copy image data from a slice of
// a source texture into a slice of a destination texture.
//
// Reference: https://developer.apple.com/documentation/metal/mtlblitcommandencoder/1400754-copyfromtexture.
func (bce BlitCommandEncoder) CopyFromTexture(
src Texture, srcSlice, srcLevel int, srcOrigin Origin, srcSize Size,
dst Texture, dstSlice, dstLevel int, dstOrigin Origin,
) {
C.BlitCommandEncoder_CopyFromTexture(
bce.commandEncoder,
src.texture, C.uint_t(srcSlice), C.uint_t(srcLevel),
C.struct_Origin{
X: C.uint_t(srcOrigin.X),
Y: C.uint_t(srcOrigin.Y),
Z: C.uint_t(srcOrigin.Z),
},
C.struct_Size{
Width: C.uint_t(srcSize.Width),
Height: C.uint_t(srcSize.Height),
Depth: C.uint_t(srcSize.Depth),
},
dst.texture, C.uint_t(dstSlice), C.uint_t(dstLevel),
C.struct_Origin{
X: C.uint_t(dstOrigin.X),
Y: C.uint_t(dstOrigin.Y),
Z: C.uint_t(dstOrigin.Z),
},
)
}
// SynchronizeResource flushes any copy of the specified resource from its corresponding
// Device caches and, if needed, invalidates any CPU caches.
//
// Reference: https://developer.apple.com/documentation/metal/mtlblitcommandencoder/1400775-synchronize.
func (bce BlitCommandEncoder) SynchronizeResource(resource Resource) {
C.BlitCommandEncoder_SynchronizeResource(bce.commandEncoder, resource.resource())
}