internal/graphicsdriver/metal/mtl: follow ObjC convention

Closes #2981
This commit is contained in:
Hajime Hoshi 2024-05-06 21:38:59 +09:00
parent 2261cf76de
commit f0ca3f1870
5 changed files with 140 additions and 144 deletions

View File

@ -184,7 +184,7 @@ func (g *Graphics) gcBuffers() {
func (g *Graphics) availableBuffer(length uintptr) mtl.Buffer { func (g *Graphics) availableBuffer(length uintptr) mtl.Buffer {
if g.cb == (mtl.CommandBuffer{}) { if g.cb == (mtl.CommandBuffer{}) {
g.cb = g.cq.MakeCommandBuffer() g.cb = g.cq.CommandBuffer()
} }
var newBuf mtl.Buffer var newBuf mtl.Buffer
@ -197,7 +197,7 @@ func (g *Graphics) availableBuffer(length uintptr) mtl.Buffer {
} }
if newBuf == (mtl.Buffer{}) { if newBuf == (mtl.Buffer{}) {
newBuf = g.view.getMTLDevice().MakeBufferWithLength(pow2(length), resourceStorageMode) newBuf = g.view.getMTLDevice().NewBufferWithLength(pow2(length), resourceStorageMode)
} }
if g.buffers == nil { if g.buffers == nil {
@ -286,7 +286,7 @@ func (g *Graphics) NewImage(width, height int) (graphicsdriver.Image, error) {
StorageMode: storageMode, StorageMode: storageMode,
Usage: mtl.TextureUsageShaderRead | mtl.TextureUsageRenderTarget, Usage: mtl.TextureUsageShaderRead | mtl.TextureUsageRenderTarget,
} }
t := g.view.getMTLDevice().MakeTexture(td) t := g.view.getMTLDevice().NewTextureWithDescriptor(td)
i := &Image{ i := &Image{
id: g.genNextImageID(), id: g.genNextImageID(),
graphics: g, graphics: g,
@ -397,7 +397,7 @@ func (g *Graphics) Initialize() error {
} }
// The stencil reference value is always 0 (default). // The stencil reference value is always 0 (default).
g.dsss[noStencil] = g.view.getMTLDevice().MakeDepthStencilState(mtl.DepthStencilDescriptor{ g.dsss[noStencil] = g.view.getMTLDevice().NewDepthStencilStateWithDescriptor(mtl.DepthStencilDescriptor{
BackFaceStencil: mtl.StencilDescriptor{ BackFaceStencil: mtl.StencilDescriptor{
StencilFailureOperation: mtl.StencilOperationKeep, StencilFailureOperation: mtl.StencilOperationKeep,
DepthFailureOperation: mtl.StencilOperationKeep, DepthFailureOperation: mtl.StencilOperationKeep,
@ -411,7 +411,7 @@ func (g *Graphics) Initialize() error {
StencilCompareFunction: mtl.CompareFunctionAlways, StencilCompareFunction: mtl.CompareFunctionAlways,
}, },
}) })
g.dsss[incrementStencil] = g.view.getMTLDevice().MakeDepthStencilState(mtl.DepthStencilDescriptor{ g.dsss[incrementStencil] = g.view.getMTLDevice().NewDepthStencilStateWithDescriptor(mtl.DepthStencilDescriptor{
BackFaceStencil: mtl.StencilDescriptor{ BackFaceStencil: mtl.StencilDescriptor{
StencilFailureOperation: mtl.StencilOperationKeep, StencilFailureOperation: mtl.StencilOperationKeep,
DepthFailureOperation: mtl.StencilOperationKeep, DepthFailureOperation: mtl.StencilOperationKeep,
@ -425,7 +425,7 @@ func (g *Graphics) Initialize() error {
StencilCompareFunction: mtl.CompareFunctionAlways, StencilCompareFunction: mtl.CompareFunctionAlways,
}, },
}) })
g.dsss[invertStencil] = g.view.getMTLDevice().MakeDepthStencilState(mtl.DepthStencilDescriptor{ g.dsss[invertStencil] = g.view.getMTLDevice().NewDepthStencilStateWithDescriptor(mtl.DepthStencilDescriptor{
BackFaceStencil: mtl.StencilDescriptor{ BackFaceStencil: mtl.StencilDescriptor{
StencilFailureOperation: mtl.StencilOperationKeep, StencilFailureOperation: mtl.StencilOperationKeep,
DepthFailureOperation: mtl.StencilOperationKeep, DepthFailureOperation: mtl.StencilOperationKeep,
@ -439,7 +439,7 @@ func (g *Graphics) Initialize() error {
StencilCompareFunction: mtl.CompareFunctionAlways, StencilCompareFunction: mtl.CompareFunctionAlways,
}, },
}) })
g.dsss[drawWithStencil] = g.view.getMTLDevice().MakeDepthStencilState(mtl.DepthStencilDescriptor{ g.dsss[drawWithStencil] = g.view.getMTLDevice().NewDepthStencilStateWithDescriptor(mtl.DepthStencilDescriptor{
BackFaceStencil: mtl.StencilDescriptor{ BackFaceStencil: mtl.StencilDescriptor{
StencilFailureOperation: mtl.StencilOperationKeep, StencilFailureOperation: mtl.StencilOperationKeep,
DepthFailureOperation: mtl.StencilOperationKeep, DepthFailureOperation: mtl.StencilOperationKeep,
@ -454,7 +454,7 @@ func (g *Graphics) Initialize() error {
}, },
}) })
g.cq = g.view.getMTLDevice().MakeCommandQueue() g.cq = g.view.getMTLDevice().NewCommandQueue()
return nil return nil
} }
@ -505,9 +505,9 @@ func (g *Graphics) draw(dst *Image, dstRegions []graphicsdriver.DstRegion, srcs
} }
if g.cb == (mtl.CommandBuffer{}) { if g.cb == (mtl.CommandBuffer{}) {
g.cb = g.cq.MakeCommandBuffer() g.cb = g.cq.CommandBuffer()
} }
g.rce = g.cb.MakeRenderCommandEncoder(rpd) g.rce = g.cb.RenderCommandEncoderWithDescriptor(rpd)
} }
w, h := dst.internalSize() w, h := dst.internalSize()
@ -810,8 +810,8 @@ func (i *Image) syncTexture() {
panic("metal: command buffer must be empty at syncTexture: flushIfNeeded is not called yet?") panic("metal: command buffer must be empty at syncTexture: flushIfNeeded is not called yet?")
} }
cb := i.graphics.cq.MakeCommandBuffer() cb := i.graphics.cq.CommandBuffer()
bce := cb.MakeBlitCommandEncoder() bce := cb.BlitCommandEncoder()
bce.SynchronizeTexture(i.texture, 0, 0) bce.SynchronizeTexture(i.texture, 0, 0)
bce.EndEncoding() bce.EndEncoding()
@ -859,7 +859,7 @@ func (i *Image) WritePixels(args []graphicsdriver.PixelsArgs) error {
StorageMode: storageMode, StorageMode: storageMode,
Usage: mtl.TextureUsageShaderRead | mtl.TextureUsageRenderTarget, Usage: mtl.TextureUsageShaderRead | mtl.TextureUsageRenderTarget,
} }
t := g.view.getMTLDevice().MakeTexture(td) t := g.view.getMTLDevice().NewTextureWithDescriptor(td)
g.tmpTextures = append(g.tmpTextures, t) g.tmpTextures = append(g.tmpTextures, t)
for _, a := range args { for _, a := range args {
@ -870,9 +870,9 @@ func (i *Image) WritePixels(args []graphicsdriver.PixelsArgs) error {
} }
if g.cb == (mtl.CommandBuffer{}) { if g.cb == (mtl.CommandBuffer{}) {
g.cb = i.graphics.cq.MakeCommandBuffer() g.cb = i.graphics.cq.CommandBuffer()
} }
bce := g.cb.MakeBlitCommandEncoder() bce := g.cb.BlitCommandEncoder()
for _, a := range args { for _, a := range args {
so := mtl.Origin{X: a.Region.Min.X - region.Min.X, Y: a.Region.Min.Y - region.Min.Y, Z: 0} so := mtl.Origin{X: a.Region.Min.X - region.Min.X, Y: a.Region.Min.Y - region.Min.Y, Z: 0}
ss := mtl.Size{Width: a.Region.Dx(), Height: a.Region.Dy(), Depth: 1} ss := mtl.Size{Width: a.Region.Dx(), Height: a.Region.Dy(), Depth: 1}
@ -914,5 +914,5 @@ func (i *Image) ensureStencil() {
StorageMode: mtl.StorageModePrivate, StorageMode: mtl.StorageModePrivate,
Usage: mtl.TextureUsageRenderTarget, Usage: mtl.TextureUsageRenderTarget,
} }
i.stencil = i.graphics.view.getMTLDevice().MakeTexture(td) i.stencil = i.graphics.view.getMTLDevice().NewTextureWithDescriptor(td)
} }

View File

@ -112,15 +112,15 @@ fragment float4 FragmentShader(Vertex in [[stage_in]]) {
return in.color; return in.color;
} }
` `
lib, err := device.MakeLibrary(source, mtl.CompileOptions{}) lib, err := device.NewLibraryWithSource(source, mtl.CompileOptions{})
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
vs, err := lib.MakeFunction("VertexShader") vs, err := lib.NewFunctionWithName("VertexShader")
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
fs, err := lib.MakeFunction("FragmentShader") fs, err := lib.NewFunctionWithName("FragmentShader")
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
@ -129,7 +129,7 @@ fragment float4 FragmentShader(Vertex in [[stage_in]]) {
rpld.FragmentFunction = fs rpld.FragmentFunction = fs
rpld.ColorAttachments[0].PixelFormat = mtl.PixelFormatRGBA8UNorm rpld.ColorAttachments[0].PixelFormat = mtl.PixelFormatRGBA8UNorm
rpld.ColorAttachments[0].WriteMask = mtl.ColorWriteMaskAll rpld.ColorAttachments[0].WriteMask = mtl.ColorWriteMaskAll
rps, err := device.MakeRenderPipelineState(rpld) rps, err := device.NewRenderPipelineStateWithDescriptor(rpld)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
@ -144,7 +144,7 @@ fragment float4 FragmentShader(Vertex in [[stage_in]]) {
{f32.Vec4{-0.75, -0.75, 0, 1}, f32.Vec4{1, 1, 1, 1}}, {f32.Vec4{-0.75, -0.75, 0, 1}, f32.Vec4{1, 1, 1, 1}},
{f32.Vec4{+0.75, -0.75, 0, 1}, f32.Vec4{0, 0, 0, 1}}, {f32.Vec4{+0.75, -0.75, 0, 1}, f32.Vec4{0, 0, 0, 1}},
} }
vertexBuffer := device.MakeBufferWithBytes(unsafe.Pointer(&vertexData[0]), unsafe.Sizeof(vertexData), mtl.ResourceStorageModeManaged) vertexBuffer := device.NewBufferWithBytes(unsafe.Pointer(&vertexData[0]), unsafe.Sizeof(vertexData), mtl.ResourceStorageModeManaged)
// Create an output texture to render into. // Create an output texture to render into.
td := mtl.TextureDescriptor{ td := mtl.TextureDescriptor{
@ -154,10 +154,10 @@ fragment float4 FragmentShader(Vertex in [[stage_in]]) {
Height: 20, Height: 20,
StorageMode: mtl.StorageModeManaged, StorageMode: mtl.StorageModeManaged,
} }
texture := device.MakeTexture(td) texture := device.NewTextureWithDescriptor(td)
cq := device.MakeCommandQueue() cq := device.NewCommandQueue()
cb := cq.MakeCommandBuffer() cb := cq.CommandBuffer()
// Encode all render commands. // Encode all render commands.
var rpd mtl.RenderPassDescriptor var rpd mtl.RenderPassDescriptor
@ -165,14 +165,14 @@ fragment float4 FragmentShader(Vertex in [[stage_in]]) {
rpd.ColorAttachments[0].StoreAction = mtl.StoreActionStore rpd.ColorAttachments[0].StoreAction = mtl.StoreActionStore
rpd.ColorAttachments[0].ClearColor = mtl.ClearColor{Red: 0, Green: 0, Blue: 0, Alpha: 1} rpd.ColorAttachments[0].ClearColor = mtl.ClearColor{Red: 0, Green: 0, Blue: 0, Alpha: 1}
rpd.ColorAttachments[0].Texture = texture rpd.ColorAttachments[0].Texture = texture
rce := cb.MakeRenderCommandEncoder(rpd) rce := cb.RenderCommandEncoderWithDescriptor(rpd)
rce.SetRenderPipelineState(rps) rce.SetRenderPipelineState(rps)
rce.SetVertexBuffer(vertexBuffer, 0, 0) rce.SetVertexBuffer(vertexBuffer, 0, 0)
rce.DrawPrimitives(mtl.PrimitiveTypeTriangle, 0, 3) rce.DrawPrimitives(mtl.PrimitiveTypeTriangle, 0, 3)
rce.EndEncoding() rce.EndEncoding()
// Encode all blit commands. // Encode all blit commands.
bce := cb.MakeBlitCommandEncoder() bce := cb.BlitCommandEncoder()
bce.Synchronize(texture) bce.Synchronize(texture)
bce.EndEncoding() bce.EndEncoding()

View File

@ -36,7 +36,7 @@ import (
// GPUFamily represents the functionality for families of GPUs. // GPUFamily represents the functionality for families of GPUs.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlgpufamily // Reference: https://developer.apple.com/documentation/metal/mtlgpufamily?language=objc.
type GPUFamily int type GPUFamily int
const ( const (
@ -54,7 +54,7 @@ const (
// FeatureSet defines a specific platform, hardware, and software configuration. // FeatureSet defines a specific platform, hardware, and software configuration.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlfeatureset. // Reference: https://developer.apple.com/documentation/metal/mtlfeatureset?language=objc.
type FeatureSet uint16 type FeatureSet uint16
const ( const (
@ -92,7 +92,7 @@ const (
// TextureType defines The dimension of each image, including whether multiple images are arranged into an array or // TextureType defines The dimension of each image, including whether multiple images are arranged into an array or
// a cube. // a cube.
// //
// Reference: https://developer.apple.com/documentation/metal/mtltexturetype // Reference: https://developer.apple.com/documentation/metal/mtltexturetype?language=objc.
type TextureType uint16 type TextureType uint16
const ( const (
@ -102,7 +102,7 @@ const (
// PixelFormat defines data formats that describe the organization // PixelFormat defines data formats that describe the organization
// and characteristics of individual pixels in a texture. // and characteristics of individual pixels in a texture.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlpixelformat. // Reference: https://developer.apple.com/documentation/metal/mtlpixelformat?language=objc.
type PixelFormat uint16 type PixelFormat uint16
// The data formats that describe the organization and characteristics // The data formats that describe the organization and characteristics
@ -117,7 +117,7 @@ const (
// PrimitiveType defines geometric primitive types for drawing commands. // PrimitiveType defines geometric primitive types for drawing commands.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlprimitivetype. // Reference: https://developer.apple.com/documentation/metal/mtlprimitivetype?language=objc.
type PrimitiveType uint8 type PrimitiveType uint8
// Geometric primitive types for drawing commands. // Geometric primitive types for drawing commands.
@ -132,7 +132,7 @@ const (
// LoadAction defines actions performed at the start of a rendering pass // LoadAction defines actions performed at the start of a rendering pass
// for a render command encoder. // for a render command encoder.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlloadaction. // Reference: https://developer.apple.com/documentation/metal/mtlloadaction?language=objc.
type LoadAction uint8 type LoadAction uint8
// Actions performed at the start of a rendering pass for a render command encoder. // Actions performed at the start of a rendering pass for a render command encoder.
@ -145,7 +145,7 @@ const (
// StoreAction defines actions performed at the end of a rendering pass // StoreAction defines actions performed at the end of a rendering pass
// for a render command encoder. // for a render command encoder.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlstoreaction. // Reference: https://developer.apple.com/documentation/metal/mtlstoreaction?language=objc.
type StoreAction uint8 type StoreAction uint8
// Actions performed at the end of a rendering pass for a render command encoder. // Actions performed at the end of a rendering pass for a render command encoder.
@ -160,7 +160,7 @@ const (
// StorageMode defines the memory location and access permissions of a resource. // StorageMode defines the memory location and access permissions of a resource.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlstoragemode. // Reference: https://developer.apple.com/documentation/metal/mtlstoragemode?language=objc.
type StorageMode uint8 type StorageMode uint8
const ( const (
@ -189,7 +189,7 @@ const (
// ResourceOptions defines optional arguments used to create // ResourceOptions defines optional arguments used to create
// and influence behavior of buffer and texture objects. // and influence behavior of buffer and texture objects.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlresourceoptions. // Reference: https://developer.apple.com/documentation/metal/mtlresourceoptions?language=objc.
type ResourceOptions uint16 type ResourceOptions uint16
const ( const (
@ -237,7 +237,7 @@ const (
// CPUCacheMode is the CPU cache mode that defines the CPU mapping of a resource. // CPUCacheMode is the CPU cache mode that defines the CPU mapping of a resource.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlcpucachemode. // Reference: https://developer.apple.com/documentation/metal/mtlcpucachemode?language=objc.
type CPUCacheMode uint8 type CPUCacheMode uint8
const ( const (
@ -252,7 +252,7 @@ const (
// IndexType is the index type for an index buffer that references vertices of geometric primitives. // IndexType is the index type for an index buffer that references vertices of geometric primitives.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlstoragemode // Reference: https://developer.apple.com/documentation/metal/mtlstoragemode?language=objc
type IndexType uint8 type IndexType uint8
const ( const (
@ -358,7 +358,7 @@ const (
// Resource represents a memory allocation for storing specialized data // Resource represents a memory allocation for storing specialized data
// that is accessible to the GPU. // that is accessible to the GPU.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlresource. // Reference: https://developer.apple.com/documentation/metal/mtlresource?language=objc.
type Resource interface { type Resource interface {
// resource returns the underlying id<MTLResource> pointer. // resource returns the underlying id<MTLResource> pointer.
resource() unsafe.Pointer resource() unsafe.Pointer
@ -366,7 +366,7 @@ type Resource interface {
// RenderPipelineDescriptor configures new RenderPipelineState objects. // RenderPipelineDescriptor configures new RenderPipelineState objects.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlrenderpipelinedescriptor. // Reference: https://developer.apple.com/documentation/metal/mtlrenderpipelinedescriptor?language=objc.
type RenderPipelineDescriptor struct { type RenderPipelineDescriptor struct {
// VertexFunction is a programmable function that processes individual vertices in a rendering pass. // VertexFunction is a programmable function that processes individual vertices in a rendering pass.
VertexFunction Function VertexFunction Function
@ -384,7 +384,7 @@ type RenderPipelineDescriptor struct {
// RenderPipelineColorAttachmentDescriptor describes a color render target that specifies // RenderPipelineColorAttachmentDescriptor describes a color render target that specifies
// the color configuration and color operations associated with a render pipeline. // the color configuration and color operations associated with a render pipeline.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlrenderpipelinecolorattachmentdescriptor. // Reference: https://developer.apple.com/documentation/metal/mtlrenderpipelinecolorattachmentdescriptor?language=objc.
type RenderPipelineColorAttachmentDescriptor struct { type RenderPipelineColorAttachmentDescriptor struct {
// PixelFormat is the pixel format of the color attachment's texture. // PixelFormat is the pixel format of the color attachment's texture.
PixelFormat PixelFormat PixelFormat PixelFormat
@ -404,7 +404,7 @@ type RenderPipelineColorAttachmentDescriptor struct {
// RenderPassDescriptor describes a group of render targets that serve as // RenderPassDescriptor describes a group of render targets that serve as
// the output destination for pixels generated by a render pass. // the output destination for pixels generated by a render pass.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlrenderpassdescriptor. // Reference: https://developer.apple.com/documentation/metal/mtlrenderpassdescriptor?language=objc.
type RenderPassDescriptor struct { type RenderPassDescriptor struct {
// ColorAttachments is array of state information for attachments that store color data. // ColorAttachments is array of state information for attachments that store color data.
ColorAttachments [1]RenderPassColorAttachmentDescriptor ColorAttachments [1]RenderPassColorAttachmentDescriptor
@ -416,7 +416,7 @@ type RenderPassDescriptor struct {
// RenderPassColorAttachmentDescriptor describes a color render target that serves // RenderPassColorAttachmentDescriptor describes a color render target that serves
// as the output destination for color pixels generated by a render pass. // as the output destination for color pixels generated by a render pass.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlrenderpasscolorattachmentdescriptor. // Reference: https://developer.apple.com/documentation/metal/mtlrenderpasscolorattachmentdescriptor?language=objc.
type RenderPassColorAttachmentDescriptor struct { type RenderPassColorAttachmentDescriptor struct {
RenderPassAttachmentDescriptor RenderPassAttachmentDescriptor
ClearColor ClearColor ClearColor ClearColor
@ -425,7 +425,7 @@ type RenderPassColorAttachmentDescriptor struct {
// RenderPassStencilAttachment describes a stencil render target that serves as the output // RenderPassStencilAttachment describes a stencil render target that serves as the output
// destination for stencil pixels generated by a render pass. // destination for stencil pixels generated by a render pass.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlrenderpassstencilattachmentdescriptor // Reference: https://developer.apple.com/documentation/metal/mtlrenderpassstencilattachmentdescriptor?language=objc.
type RenderPassStencilAttachment struct { type RenderPassStencilAttachment struct {
RenderPassAttachmentDescriptor RenderPassAttachmentDescriptor
} }
@ -433,7 +433,7 @@ type RenderPassStencilAttachment struct {
// RenderPassAttachmentDescriptor describes a render target that serves // RenderPassAttachmentDescriptor describes a render target that serves
// as the output destination for pixels generated by a render pass. // as the output destination for pixels generated by a render pass.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlrenderpassattachmentdescriptor. // Reference: https://developer.apple.com/documentation/metal/mtlrenderpassattachmentdescriptor?language=objc.
type RenderPassAttachmentDescriptor struct { type RenderPassAttachmentDescriptor struct {
LoadAction LoadAction LoadAction LoadAction
StoreAction StoreAction StoreAction StoreAction
@ -442,14 +442,14 @@ type RenderPassAttachmentDescriptor struct {
// ClearColor is an RGBA value used for a color pixel. // ClearColor is an RGBA value used for a color pixel.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlclearcolor. // Reference: https://developer.apple.com/documentation/metal/mtlclearcolor?language=objc.
type ClearColor struct { type ClearColor struct {
Red, Green, Blue, Alpha float64 Red, Green, Blue, Alpha float64
} }
// TextureDescriptor configures new Texture objects. // TextureDescriptor configures new Texture objects.
// //
// Reference: https://developer.apple.com/documentation/metal/mtltexturedescriptor. // Reference: https://developer.apple.com/documentation/metal/mtltexturedescriptor?language=objc.
type TextureDescriptor struct { type TextureDescriptor struct {
TextureType TextureType TextureType TextureType
PixelFormat PixelFormat PixelFormat PixelFormat
@ -462,7 +462,7 @@ type TextureDescriptor struct {
// Device is abstract representation of the GPU that // Device is abstract representation of the GPU that
// serves as the primary interface for a Metal app. // serves as the primary interface for a Metal app.
// //
// Reference: https://developer.apple.com/documentation/metal/mtldevice. // Reference: https://developer.apple.com/documentation/metal/mtldevice?language=objc.
type Device struct { type Device struct {
device objc.ID device objc.ID
@ -568,7 +568,7 @@ var (
// CreateSystemDefaultDevice returns the preferred system default Metal device. // CreateSystemDefaultDevice returns the preferred system default Metal device.
// //
// Reference: https://developer.apple.com/documentation/metal/1433401-mtlcreatesystemdefaultdevice. // Reference: https://developer.apple.com/documentation/metal/1433401-mtlcreatesystemdefaultdevice?language=objc.
func CreateSystemDefaultDevice() (Device, error) { func CreateSystemDefaultDevice() (Device, error) {
metal, err := purego.Dlopen("/System/Library/Frameworks/Metal.framework/Metal", purego.RTLD_LAZY|purego.RTLD_GLOBAL) metal, err := purego.Dlopen("/System/Library/Frameworks/Metal.framework/Metal", purego.RTLD_LAZY|purego.RTLD_GLOBAL)
if err != nil { if err != nil {
@ -608,37 +608,36 @@ func (d Device) Device() unsafe.Pointer { return *(*unsafe.Pointer)(unsafe.Point
// RespondsToSelector returns a Boolean value that indicates whether the receiver implements or inherits a method that can respond to a specified message. // RespondsToSelector returns a Boolean value that indicates whether the receiver implements or inherits a method that can respond to a specified message.
// //
// Reference: https://developer.apple.com/documentation/objectivec/1418956-nsobject/1418583-respondstoselector // Reference: https://developer.apple.com/documentation/objectivec/1418956-nsobject/1418583-respondstoselector?language=objc.
func (d Device) RespondsToSelector(sel objc.SEL) bool { func (d Device) RespondsToSelector(sel objc.SEL) bool {
return d.device.Send(sel_respondsToSelector, sel) != 0 return d.device.Send(sel_respondsToSelector, sel) != 0
} }
// SupportsFamily returns a Boolean value that indicates whether the GPU device supports the feature set of a specific GPU family. // SupportsFamily returns a Boolean value that indicates whether the GPU device supports the feature set of a specific GPU family.
// //
// Reference: https://developer.apple.com/documentation/metal/mtldevice/3143473-supportsfamily // Reference: https://developer.apple.com/documentation/metal/mtldevice/3143473-supportsfamily?language=objc.
func (d Device) SupportsFamily(gpuFamily GPUFamily) bool { func (d Device) SupportsFamily(gpuFamily GPUFamily) bool {
return d.device.Send(sel_supportsFamily, uintptr(gpuFamily)) != 0 return d.device.Send(sel_supportsFamily, uintptr(gpuFamily)) != 0
} }
// SupportsFeatureSet reports whether device d supports feature set fs. // SupportsFeatureSet reports whether device d supports feature set fs.
// //
// Reference: https://developer.apple.com/documentation/metal/mtldevice/1433418-supportsfeatureset. // Reference: https://developer.apple.com/documentation/metal/mtldevice/1433418-supportsfeatureset?language=objc.
func (d Device) SupportsFeatureSet(fs FeatureSet) bool { func (d Device) SupportsFeatureSet(fs FeatureSet) bool {
return d.device.Send(sel_supportsFeatureSet, uintptr(fs)) != 0 return d.device.Send(sel_supportsFeatureSet, uintptr(fs)) != 0
} }
// MakeCommandQueue creates a serial command submission queue. // NewCommandQueue creates a queue you use to submit rendering and computation commands to a GPU.
// //
// Reference: https://developer.apple.com/documentation/metal/mtldevice/1433388-makecommandqueue. // Reference: https://developer.apple.com/documentation/metal/mtldevice/1433388-newcommandqueue?language=objc.
func (d Device) MakeCommandQueue() CommandQueue { func (d Device) NewCommandQueue() CommandQueue {
return CommandQueue{d.device.Send(sel_newCommandQueue)} return CommandQueue{d.device.Send(sel_newCommandQueue)}
} }
// MakeLibrary creates a new library that contains // NewLibraryWithSource synchronously creates a Metal library instance by compiling the functions in a source string.
// the functions stored in the specified source string.
// //
// Reference: https://developer.apple.com/documentation/metal/mtldevice/1433431-makelibrary. // Reference: https://developer.apple.com/documentation/metal/mtldevice/1433431-newlibrarywithsource?language=objc.
func (d Device) MakeLibrary(source string, opt CompileOptions) (Library, error) { func (d Device) NewLibraryWithSource(source string, opt CompileOptions) (Library, error) {
var err cocoa.NSError var err cocoa.NSError
l := d.device.Send( l := d.device.Send(
sel_newLibraryWithSource_options_error, sel_newLibraryWithSource_options_error,
@ -653,10 +652,10 @@ func (d Device) MakeLibrary(source string, opt CompileOptions) (Library, error)
return Library{l}, nil return Library{l}, nil
} }
// MakeLibraryWithData creates a Metal library instance from a data instance that contains the functions in a precompiled Metal library. // NewLibraryWithData Creates a Metal library instance that contains the functions in a precompiled Metal library.
// //
// Reference: https://developer.apple.com/documentation/metal/mtldevice/1433391-makelibrary // Reference: https://developer.apple.com/documentation/metal/mtldevice/1433391-newlibrarywithdata?language=objc.
func (d Device) MakeLibraryWithData(buffer []byte) (Library, error) { func (d Device) NewLibraryWithData(buffer []byte) (Library, error) {
defer runtime.KeepAlive(buffer) defer runtime.KeepAlive(buffer)
data := dispatchDataCreate(unsafe.Pointer(&buffer[0]), uint(len(buffer)), 0, 0) data := dispatchDataCreate(unsafe.Pointer(&buffer[0]), uint(len(buffer)), 0, 0)
@ -674,10 +673,10 @@ func (d Device) MakeLibraryWithData(buffer []byte) (Library, error) {
return Library{l}, nil return Library{l}, nil
} }
// MakeRenderPipelineState creates a render pipeline state object. // NewRenderPipelineStateWithDescriptor synchronously creates a render pipeline state.
// //
// Reference: https://developer.apple.com/documentation/metal/mtldevice/1433369-makerenderpipelinestate. // Reference: https://developer.apple.com/documentation/metal/mtldevice/1433369-newrenderpipelinestatewithdescri?language=objc.
func (d Device) MakeRenderPipelineState(rpd RenderPipelineDescriptor) (RenderPipelineState, error) { func (d Device) NewRenderPipelineStateWithDescriptor(rpd RenderPipelineDescriptor) (RenderPipelineState, error) {
renderPipelineDescriptor := objc.ID(class_MTLRenderPipelineDescriptor).Send(sel_new) renderPipelineDescriptor := objc.ID(class_MTLRenderPipelineDescriptor).Send(sel_new)
renderPipelineDescriptor.Send(sel_setVertexFunction, rpd.VertexFunction.function) renderPipelineDescriptor.Send(sel_setVertexFunction, rpd.VertexFunction.function)
renderPipelineDescriptor.Send(sel_setFragmentFunction, rpd.FragmentFunction.function) renderPipelineDescriptor.Send(sel_setFragmentFunction, rpd.FragmentFunction.function)
@ -705,26 +704,24 @@ func (d Device) MakeRenderPipelineState(rpd RenderPipelineDescriptor) (RenderPip
return RenderPipelineState{renderPipelineState}, nil return RenderPipelineState{renderPipelineState}, nil
} }
// MakeBufferWithBytes allocates a new buffer of a given length // NewBufferWithBytes allocates a new buffer of a given length and initializes its contents by copying existing data into it.
// and initializes its contents by copying existing data into it.
// //
// Reference: https://developer.apple.com/documentation/metal/mtldevice/1433429-makebuffer. // Reference: https://developer.apple.com/documentation/metal/mtldevice/1433429-newbufferwithbytes?language=objc.
func (d Device) MakeBufferWithBytes(bytes unsafe.Pointer, length uintptr, opt ResourceOptions) Buffer { func (d Device) NewBufferWithBytes(bytes unsafe.Pointer, length uintptr, opt ResourceOptions) Buffer {
return Buffer{d.device.Send(sel_newBufferWithBytes_length_options, bytes, length, uintptr(opt))} return Buffer{d.device.Send(sel_newBufferWithBytes_length_options, bytes, length, uintptr(opt))}
} }
// MakeBufferWithLength allocates a new zero-filled buffer of a given length. // NewBufferWithLength allocates a new zero-filled buffer of a given length.
// //
// Reference: https://developer.apple.com/documentation/metal/mtldevice/1433375-newbufferwithlength // Reference: https://developer.apple.com/documentation/metal/mtldevice/1433375-newbufferwithlength?language=objc.
func (d Device) MakeBufferWithLength(length uintptr, opt ResourceOptions) Buffer { func (d Device) NewBufferWithLength(length uintptr, opt ResourceOptions) Buffer {
return Buffer{d.device.Send(sel_newBufferWithLength_options, length, uintptr(opt))} return Buffer{d.device.Send(sel_newBufferWithLength_options, length, uintptr(opt))}
} }
// MakeTexture creates a texture object with privately owned storage // NewTextureWithDescriptor creates a new texture instance.
// that contains texture state.
// //
// Reference: https://developer.apple.com/documentation/metal/mtldevice/1433425-maketexture. // Reference: https://developer.apple.com/documentation/metal/mtldevice/1433425-newtexturewithdescriptor?language=objc.
func (d Device) MakeTexture(td TextureDescriptor) Texture { func (d Device) NewTextureWithDescriptor(td TextureDescriptor) Texture {
textureDescriptor := objc.ID(class_MTLTextureDescriptor).Send(sel_new) textureDescriptor := objc.ID(class_MTLTextureDescriptor).Send(sel_new)
textureDescriptor.Send(sel_setTextureType, uintptr(td.TextureType)) textureDescriptor.Send(sel_setTextureType, uintptr(td.TextureType))
textureDescriptor.Send(sel_setPixelFormat, uintptr(td.PixelFormat)) textureDescriptor.Send(sel_setPixelFormat, uintptr(td.PixelFormat))
@ -739,10 +736,10 @@ func (d Device) MakeTexture(td TextureDescriptor) Texture {
} }
} }
// MakeDepthStencilState creates a new object that contains depth and stencil test state. // NewDepthStencilStateWithDescriptor creates a depth-stencil state instance.
// //
// Reference: https://developer.apple.com/documentation/metal/mtldevice/1433412-makedepthstencilstate // Reference: https://developer.apple.com/documentation/metal/mtldevice/1433412-newdepthstencilstatewithdescript?language=objc.
func (d Device) MakeDepthStencilState(dsd DepthStencilDescriptor) DepthStencilState { func (d Device) NewDepthStencilStateWithDescriptor(dsd DepthStencilDescriptor) DepthStencilState {
depthStencilDescriptor := objc.ID(class_MTLDepthStencilDescriptor).Send(sel_new) depthStencilDescriptor := objc.ID(class_MTLDepthStencilDescriptor).Send(sel_new)
backFaceStencil := depthStencilDescriptor.Send(sel_backFaceStencil) backFaceStencil := depthStencilDescriptor.Send(sel_backFaceStencil)
backFaceStencil.Send(sel_setStencilFailureOperation, uintptr(dsd.BackFaceStencil.StencilFailureOperation)) backFaceStencil.Send(sel_setStencilFailureOperation, uintptr(dsd.BackFaceStencil.StencilFailureOperation))
@ -764,14 +761,14 @@ func (d Device) MakeDepthStencilState(dsd DepthStencilDescriptor) DepthStencilSt
// CompileOptions specifies optional compilation settings for // CompileOptions specifies optional compilation settings for
// the graphics or compute functions within a library. // the graphics or compute functions within a library.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlcompileoptions. // Reference: https://developer.apple.com/documentation/metal/mtlcompileoptions?language=objc.
type CompileOptions struct { type CompileOptions struct {
// TODO. // TODO.
} }
// Drawable is a displayable resource that can be rendered or written to. // Drawable is a displayable resource that can be rendered or written to.
// //
// Reference: https://developer.apple.com/documentation/metal/mtldrawable. // Reference: https://developer.apple.com/documentation/metal/mtldrawable?language=objc.
type Drawable interface { type Drawable interface {
// Drawable returns the underlying id<MTLDrawable> pointer. // Drawable returns the underlying id<MTLDrawable> pointer.
Drawable() unsafe.Pointer Drawable() unsafe.Pointer
@ -780,7 +777,7 @@ type Drawable interface {
// CommandQueue is a queue that organizes the order // CommandQueue is a queue that organizes the order
// in which command buffers are executed by the GPU. // in which command buffers are executed by the GPU.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlcommandqueue. // Reference: https://developer.apple.com/documentation/metal/mtlcommandqueue?language=objc.
type CommandQueue struct { type CommandQueue struct {
commandQueue objc.ID commandQueue objc.ID
} }
@ -789,17 +786,17 @@ func (cq CommandQueue) Release() {
cq.commandQueue.Send(sel_release) cq.commandQueue.Send(sel_release)
} }
// MakeCommandBuffer creates a command buffer. // CommandBuffer returns a command buffer from the command queue that maintains strong references to resources.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlcommandqueue/1508686-makecommandbuffer. // Reference: https://developer.apple.com/documentation/metal/mtlcommandqueue/1508686-commandbuffer?language=objc.
func (cq CommandQueue) MakeCommandBuffer() CommandBuffer { func (cq CommandQueue) CommandBuffer() CommandBuffer {
return CommandBuffer{cq.commandQueue.Send(sel_commandBuffer)} return CommandBuffer{cq.commandQueue.Send(sel_commandBuffer)}
} }
// CommandBuffer is a container that stores encoded commands // CommandBuffer is a container that stores encoded commands
// that are committed to and executed by the GPU. // that are committed to and executed by the GPU.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlcommandbuffer. // Reference: https://developer.apple.com/documentation/metal/mtlcommandbuffer?language=objc.
type CommandBuffer struct { type CommandBuffer struct {
commandBuffer objc.ID commandBuffer objc.ID
} }
@ -814,44 +811,43 @@ func (cb CommandBuffer) Release() {
// Status returns the current stage in the lifetime of the command buffer. // Status returns the current stage in the lifetime of the command buffer.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlcommandbuffer/1443048-status // Reference: https://developer.apple.com/documentation/metal/mtlcommandbuffer/1443048-status?language=objc.
func (cb CommandBuffer) Status() CommandBufferStatus { func (cb CommandBuffer) Status() CommandBufferStatus {
return CommandBufferStatus(cb.commandBuffer.Send(sel_status)) return CommandBufferStatus(cb.commandBuffer.Send(sel_status))
} }
// PresentDrawable registers a drawable presentation to occur as soon as possible. // PresentDrawable registers a drawable presentation to occur as soon as possible.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlcommandbuffer/1443029-presentdrawable. // Reference: https://developer.apple.com/documentation/metal/mtlcommandbuffer/1443029-presentdrawable?language=objc.
func (cb CommandBuffer) PresentDrawable(d Drawable) { func (cb CommandBuffer) PresentDrawable(d Drawable) {
cb.commandBuffer.Send(sel_presentDrawable, d.Drawable()) cb.commandBuffer.Send(sel_presentDrawable, d.Drawable())
} }
// Commit commits this command buffer for execution as soon as possible. // Commit commits this command buffer for execution as soon as possible.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlcommandbuffer/1443003-commit. // Reference: https://developer.apple.com/documentation/metal/mtlcommandbuffer/1443003-commit?language=objc.
func (cb CommandBuffer) Commit() { func (cb CommandBuffer) Commit() {
cb.commandBuffer.Send(sel_commit) cb.commandBuffer.Send(sel_commit)
} }
// WaitUntilCompleted waits for the execution of this command buffer to complete. // WaitUntilCompleted waits for the execution of this command buffer to complete.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlcommandbuffer/1443039-waituntilcompleted. // Reference: https://developer.apple.com/documentation/metal/mtlcommandbuffer/1443039-waituntilcompleted?language=objc.
func (cb CommandBuffer) WaitUntilCompleted() { func (cb CommandBuffer) WaitUntilCompleted() {
cb.commandBuffer.Send(sel_waitUntilCompleted) cb.commandBuffer.Send(sel_waitUntilCompleted)
} }
// WaitUntilScheduled blocks execution of the current thread until the command buffer is scheduled. // WaitUntilScheduled blocks execution of the current thread until the command buffer is scheduled.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlcommandbuffer/1443036-waituntilscheduled. // Reference: https://developer.apple.com/documentation/metal/mtlcommandbuffer/1443036-waituntilscheduled?language=objc.
func (cb CommandBuffer) WaitUntilScheduled() { func (cb CommandBuffer) WaitUntilScheduled() {
cb.commandBuffer.Send(sel_waitUntilScheduled) cb.commandBuffer.Send(sel_waitUntilScheduled)
} }
// MakeRenderCommandEncoder creates an encoder object that can // RenderCommandEncoderWithDescriptor creates a render command encoder from a descriptor.
// encode graphics rendering commands into this command buffer.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlcommandbuffer/1442999-makerendercommandencoder. // Reference: https://developer.apple.com/documentation/metal/mtlcommandbuffer/1442999-rendercommandencoderwithdescript?language=objc.
func (cb CommandBuffer) MakeRenderCommandEncoder(rpd RenderPassDescriptor) RenderCommandEncoder { func (cb CommandBuffer) RenderCommandEncoderWithDescriptor(rpd RenderPassDescriptor) RenderCommandEncoder {
var renderPassDescriptor = objc.ID(class_MTLRenderPassDescriptor).Send(sel_new) var renderPassDescriptor = objc.ID(class_MTLRenderPassDescriptor).Send(sel_new)
var colorAttachments0 = renderPassDescriptor.Send(sel_colorAttachments).Send(sel_objectAtIndexedSubscript, 0) var colorAttachments0 = renderPassDescriptor.Send(sel_colorAttachments).Send(sel_objectAtIndexedSubscript, 0)
colorAttachments0.Send(sel_setLoadAction, int(rpd.ColorAttachments[0].LoadAction)) colorAttachments0.Send(sel_setLoadAction, int(rpd.ColorAttachments[0].LoadAction))
@ -872,11 +868,11 @@ func (cb CommandBuffer) MakeRenderCommandEncoder(rpd RenderPassDescriptor) Rende
return RenderCommandEncoder{CommandEncoder{rce}} return RenderCommandEncoder{CommandEncoder{rce}}
} }
// MakeBlitCommandEncoder creates an encoder object that can encode // BlitCommandEncoder creates an encoder object that can encode
// memory operation (blit) commands into this command buffer. // memory operation (blit) commands into this command buffer.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlcommandbuffer/1443001-makeblitcommandencoder. // Reference: https://developer.apple.com/documentation/metal/mtlcommandbuffer/1443001-makeblitcommandencoder?language=objc.
func (cb CommandBuffer) MakeBlitCommandEncoder() BlitCommandEncoder { func (cb CommandBuffer) BlitCommandEncoder() BlitCommandEncoder {
ce := cb.commandBuffer.Send(sel_blitCommandEncoder) ce := cb.commandBuffer.Send(sel_blitCommandEncoder)
return BlitCommandEncoder{CommandEncoder{ce}} return BlitCommandEncoder{CommandEncoder{ce}}
} }
@ -884,14 +880,14 @@ func (cb CommandBuffer) MakeBlitCommandEncoder() BlitCommandEncoder {
// CommandEncoder is an encoder that writes sequential GPU commands // CommandEncoder is an encoder that writes sequential GPU commands
// into a command buffer. // into a command buffer.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlcommandencoder. // Reference: https://developer.apple.com/documentation/metal/mtlcommandbuffer/1443001-blitcommandencoder?language=objc.
type CommandEncoder struct { type CommandEncoder struct {
commandEncoder objc.ID commandEncoder objc.ID
} }
// EndEncoding declares that all command generation from this encoder is completed. // EndEncoding declares that all command generation from this encoder is completed.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlcommandencoder/1458038-endencoding. // Reference: https://developer.apple.com/documentation/metal/mtlcommandencoder/1458038-endencoding?language=objc.
func (ce CommandEncoder) EndEncoding() { func (ce CommandEncoder) EndEncoding() {
ce.commandEncoder.Send(sel_endEncoding) ce.commandEncoder.Send(sel_endEncoding)
} }
@ -899,7 +895,7 @@ func (ce CommandEncoder) EndEncoding() {
// RenderCommandEncoder is an encoder that specifies graphics-rendering commands // RenderCommandEncoder is an encoder that specifies graphics-rendering commands
// and executes graphics functions. // and executes graphics functions.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlrendercommandencoder. // Reference: https://developer.apple.com/documentation/metal/mtlrendercommandencoder?language=objc.
type RenderCommandEncoder struct { type RenderCommandEncoder struct {
CommandEncoder CommandEncoder
} }
@ -910,7 +906,7 @@ func (rce RenderCommandEncoder) Release() {
// SetRenderPipelineState sets the current render pipeline state object. // SetRenderPipelineState sets the current render pipeline state object.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlrendercommandencoder/1515811-setrenderpipelinestate. // Reference: https://developer.apple.com/documentation/metal/mtlrendercommandencoder/1515811-setrenderpipelinestate?language=objc.
func (rce RenderCommandEncoder) SetRenderPipelineState(rps RenderPipelineState) { func (rce RenderCommandEncoder) SetRenderPipelineState(rps RenderPipelineState) {
rce.commandEncoder.Send(sel_setRenderPipelineState, rps.renderPipelineState) rce.commandEncoder.Send(sel_setRenderPipelineState, rps.renderPipelineState)
} }
@ -925,7 +921,7 @@ func (rce RenderCommandEncoder) SetViewport(viewport Viewport) {
// SetScissorRect sets the scissor rectangle for a fragment scissor test. // SetScissorRect sets the scissor rectangle for a fragment scissor test.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlrendercommandencoder/1515583-setscissorrect // Reference: https://developer.apple.com/documentation/metal/mtlrendercommandencoder/1515583-setscissorrect?language=objc.
func (rce RenderCommandEncoder) SetScissorRect(scissorRect ScissorRect) { func (rce RenderCommandEncoder) SetScissorRect(scissorRect ScissorRect) {
inv := cocoa.NSInvocation_invocationWithMethodSignature(cocoa.NSMethodSignature_signatureWithObjCTypes("v@:{MTLScissorRect=qqqq}")) inv := cocoa.NSInvocation_invocationWithMethodSignature(cocoa.NSMethodSignature_signatureWithObjCTypes("v@:{MTLScissorRect=qqqq}"))
inv.SetTarget(rce.commandEncoder) inv.SetTarget(rce.commandEncoder)
@ -937,14 +933,14 @@ func (rce RenderCommandEncoder) SetScissorRect(scissorRect ScissorRect) {
// SetVertexBuffer sets a buffer for the vertex shader function at an index // 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. // 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. // Reference: https://developer.apple.com/documentation/metal/mtlrendercommandencoder/1515829-setvertexbuffer?language=objc.
func (rce RenderCommandEncoder) SetVertexBuffer(buf Buffer, offset, index int) { func (rce RenderCommandEncoder) SetVertexBuffer(buf Buffer, offset, index int) {
rce.commandEncoder.Send(sel_setVertexBuffer_offset_atIndex, buf.buffer, offset, index) rce.commandEncoder.Send(sel_setVertexBuffer_offset_atIndex, buf.buffer, offset, index)
} }
// SetVertexBytes sets a block of data for the vertex function. // SetVertexBytes sets a block of data for the vertex function.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlrendercommandencoder/1515846-setvertexbytes. // Reference: https://developer.apple.com/documentation/metal/mtlrendercommandencoder/1515846-setvertexbytes?language=objc.
func (rce RenderCommandEncoder) SetVertexBytes(bytes unsafe.Pointer, length uintptr, index int) { func (rce RenderCommandEncoder) SetVertexBytes(bytes unsafe.Pointer, length uintptr, index int) {
rce.commandEncoder.Send(sel_setVertexBytes_length_atIndex, bytes, length, index) rce.commandEncoder.Send(sel_setVertexBytes_length_atIndex, bytes, length, index)
} }
@ -955,7 +951,7 @@ func (rce RenderCommandEncoder) SetFragmentBytes(bytes unsafe.Pointer, length ui
// SetFragmentTexture sets a texture for the fragment function at an index in the texture argument table. // SetFragmentTexture sets a texture for the fragment function at an index in the texture argument table.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlrendercommandencoder/1515390-setfragmenttexture // Reference: https://developer.apple.com/documentation/metal/mtlrendercommandencoder/1515390-setfragmenttexture?language=objc.
func (rce RenderCommandEncoder) SetFragmentTexture(texture Texture, index int) { func (rce RenderCommandEncoder) SetFragmentTexture(texture Texture, index int) {
rce.commandEncoder.Send(sel_setFragmentTexture_atIndex, texture.texture, index) rce.commandEncoder.Send(sel_setFragmentTexture_atIndex, texture.texture, index)
} }
@ -966,7 +962,7 @@ func (rce RenderCommandEncoder) SetBlendColor(red, green, blue, alpha float32) {
// SetDepthStencilState sets the depth and stencil test state. // SetDepthStencilState sets the depth and stencil test state.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlrendercommandencoder/1516119-setdepthstencilstate // Reference: https://developer.apple.com/documentation/metal/mtlrendercommandencoder/1516119-setdepthstencilstate?language=objc.
func (rce RenderCommandEncoder) SetDepthStencilState(depthStencilState DepthStencilState) { func (rce RenderCommandEncoder) SetDepthStencilState(depthStencilState DepthStencilState) {
rce.commandEncoder.Send(sel_setDepthStencilState, depthStencilState.depthStencilState) rce.commandEncoder.Send(sel_setDepthStencilState, depthStencilState.depthStencilState)
} }
@ -974,7 +970,7 @@ func (rce RenderCommandEncoder) SetDepthStencilState(depthStencilState DepthSten
// DrawPrimitives renders one instance of primitives using vertex data // DrawPrimitives renders one instance of primitives using vertex data
// in contiguous array elements. // in contiguous array elements.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlrendercommandencoder/1516326-drawprimitives. // Reference: https://developer.apple.com/documentation/metal/mtlrendercommandencoder/1516326-drawprimitives?language=objc.
func (rce RenderCommandEncoder) DrawPrimitives(typ PrimitiveType, vertexStart, vertexCount int) { func (rce RenderCommandEncoder) DrawPrimitives(typ PrimitiveType, vertexStart, vertexCount int) {
rce.commandEncoder.Send(sel_drawPrimitives_vertexStart_vertexCount, uintptr(typ), vertexStart, vertexCount) rce.commandEncoder.Send(sel_drawPrimitives_vertexStart_vertexCount, uintptr(typ), vertexStart, vertexCount)
} }
@ -991,7 +987,7 @@ func (rce RenderCommandEncoder) DrawIndexedPrimitives(typ PrimitiveType, indexCo
// BlitCommandEncoder is an encoder that specifies resource copy // BlitCommandEncoder is an encoder that specifies resource copy
// and resource synchronization commands. // and resource synchronization commands.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlblitcommandencoder. // Reference: https://developer.apple.com/documentation/metal/mtlblitcommandencoder?language=objc.
type BlitCommandEncoder struct { type BlitCommandEncoder struct {
CommandEncoder CommandEncoder
} }
@ -999,7 +995,7 @@ type BlitCommandEncoder struct {
// Synchronize flushes any copy of the specified resource from its corresponding // Synchronize flushes any copy of the specified resource from its corresponding
// Device caches and, if needed, invalidates any CPU caches. // Device caches and, if needed, invalidates any CPU caches.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlblitcommandencoder/1400775-synchronize. // Reference: https://developer.apple.com/documentation/metal/mtlblitcommandencoder/1400775-synchronize?language=objc.
func (bce BlitCommandEncoder) Synchronize(resource Resource) { func (bce BlitCommandEncoder) Synchronize(resource Resource) {
if runtime.GOOS == "ios" { if runtime.GOOS == "ios" {
return return
@ -1032,15 +1028,15 @@ func (bce BlitCommandEncoder) CopyFromTexture(sourceTexture Texture, sourceSlice
// Library is a collection of compiled graphics or compute functions. // Library is a collection of compiled graphics or compute functions.
// //
// Reference: https://developer.apple.com/documentation/metal/mtllibrary. // Reference: https://developer.apple.com/documentation/metal/mtllibrary?language=objc.
type Library struct { type Library struct {
library objc.ID library objc.ID
} }
// MakeFunction returns a pre-compiled, non-specialized function. // NewFunctionWithName returns a pre-compiled, non-specialized function.
// //
// Reference: https://developer.apple.com/documentation/metal/mtllibrary/1515524-makefunction. // Reference: https://developer.apple.com/documentation/metal/mtllibrary/1515524-newfunctionwithname?language=objc.
func (l Library) MakeFunction(name string) (Function, error) { func (l Library) NewFunctionWithName(name string) (Function, error) {
f := l.library.Send(sel_newFunctionWithName, f := l.library.Send(sel_newFunctionWithName,
cocoa.NSString_alloc().InitWithUTF8String(name).ID, cocoa.NSString_alloc().InitWithUTF8String(name).ID,
) )
@ -1057,7 +1053,7 @@ func (l Library) Release() {
// Texture is a memory allocation for storing formatted // Texture is a memory allocation for storing formatted
// image data that is accessible to the GPU. // image data that is accessible to the GPU.
// //
// Reference: https://developer.apple.com/documentation/metal/mtltexture. // Reference: https://developer.apple.com/documentation/metal/mtltexture?language=objc.
type Texture struct { type Texture struct {
texture objc.ID texture objc.ID
} }
@ -1077,7 +1073,7 @@ func (t Texture) Release() {
// GetBytes copies a block of pixels from the storage allocation of texture // GetBytes copies a block of pixels from the storage allocation of texture
// slice zero into system memory at a specified address. // slice zero into system memory at a specified address.
// //
// Reference: https://developer.apple.com/documentation/metal/mtltexture/1515751-getbytes. // Reference: https://developer.apple.com/documentation/metal/mtltexture/1515751-getbytes?language=objc.
func (t Texture) GetBytes(pixelBytes *byte, bytesPerRow uintptr, region Region, level int) { func (t Texture) GetBytes(pixelBytes *byte, bytesPerRow uintptr, region Region, level int) {
inv := cocoa.NSInvocation_invocationWithMethodSignature(cocoa.NSMethodSignature_signatureWithObjCTypes("v@:^vQ{MTLRegion={MTLOrigin=qqq}{MTLSize=qqq}}Q")) inv := cocoa.NSInvocation_invocationWithMethodSignature(cocoa.NSMethodSignature_signatureWithObjCTypes("v@:^vQ{MTLRegion={MTLOrigin=qqq}{MTLSize=qqq}}Q"))
inv.SetTarget(t.texture) inv.SetTarget(t.texture)
@ -1091,7 +1087,7 @@ func (t Texture) GetBytes(pixelBytes *byte, bytesPerRow uintptr, region Region,
// ReplaceRegion copies a block of pixels from the caller's pointer into the storage allocation for slice 0 of a texture. // ReplaceRegion copies a block of pixels from the caller's pointer into the storage allocation for slice 0 of a texture.
// //
// Reference: https://developer.apple.com/documentation/metal/mtltexture/1515464-replaceregion // Reference: https://developer.apple.com/documentation/metal/mtltexture/1515464-replaceregion?language=objc.
func (t Texture) ReplaceRegion(region Region, level int, pixelBytes unsafe.Pointer, bytesPerRow int) { func (t Texture) ReplaceRegion(region Region, level int, pixelBytes unsafe.Pointer, bytesPerRow int) {
inv := cocoa.NSInvocation_invocationWithMethodSignature(cocoa.NSMethodSignature_signatureWithObjCTypes("v@:{MTLRegion={MTLOrigin=qqq}{MTLSize=qqq}}Q^vQ")) inv := cocoa.NSInvocation_invocationWithMethodSignature(cocoa.NSMethodSignature_signatureWithObjCTypes("v@:{MTLRegion={MTLOrigin=qqq}{MTLSize=qqq}}Q^vQ"))
inv.SetTarget(t.texture) inv.SetTarget(t.texture)
@ -1105,14 +1101,14 @@ func (t Texture) ReplaceRegion(region Region, level int, pixelBytes unsafe.Point
// Width is the width of the texture image for the base level mipmap, in pixels. // Width is the width of the texture image for the base level mipmap, in pixels.
// //
// Reference: https://developer.apple.com/documentation/metal/mtltexture/1515339-width // Reference: https://developer.apple.com/documentation/metal/mtltexture/1515339-width?language=objc.
func (t Texture) Width() int { func (t Texture) Width() int {
return int(t.texture.Send(sel_width)) return int(t.texture.Send(sel_width))
} }
// Height is the height of the texture image for the base level mipmap, in pixels. // Height is the height of the texture image for the base level mipmap, in pixels.
// //
// Reference: https://developer.apple.com/documentation/metal/mtltexture/1515938-height // Reference: https://developer.apple.com/documentation/metal/mtltexture/1515938-height?language=objc.
func (t Texture) Height() int { func (t Texture) Height() int {
return int(t.texture.Send(sel_height)) return int(t.texture.Send(sel_height))
} }
@ -1120,7 +1116,7 @@ func (t Texture) Height() int {
// Buffer is a memory allocation for storing unformatted data // Buffer is a memory allocation for storing unformatted data
// that is accessible to the GPU. // that is accessible to the GPU.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlbuffer. // Reference: https://developer.apple.com/documentation/metal/mtlbuffer?language=objc.
type Buffer struct { type Buffer struct {
buffer objc.ID buffer objc.ID
} }
@ -1153,7 +1149,7 @@ func (b Buffer) Native() unsafe.Pointer {
// Function represents a programmable graphics or compute function executed by the GPU. // Function represents a programmable graphics or compute function executed by the GPU.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlfunction. // Reference: https://developer.apple.com/documentation/metal/mtlfunction?language=objc.
type Function struct { type Function struct {
function objc.ID function objc.ID
} }
@ -1165,7 +1161,7 @@ func (f Function) Release() {
// RenderPipelineState contains the graphics functions // RenderPipelineState contains the graphics functions
// and configuration state used in a render pass. // and configuration state used in a render pass.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlrenderpipelinestate. // Reference: https://developer.apple.com/documentation/metal/mtlrenderpipelinestate?language=objc.
type RenderPipelineState struct { type RenderPipelineState struct {
renderPipelineState objc.ID renderPipelineState objc.ID
} }
@ -1177,7 +1173,7 @@ func (r RenderPipelineState) Release() {
// Region is a rectangular block of pixels in an image or texture, // Region is a rectangular block of pixels in an image or texture,
// defined by its upper-left corner and its size. // defined by its upper-left corner and its size.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlregion. // Reference: https://developer.apple.com/documentation/metal/mtlregion?language=objc.
type Region struct { type Region struct {
Origin Origin // The location of the upper-left corner of the block. Origin Origin // The location of the upper-left corner of the block.
Size Size // The size of the block. Size Size // The size of the block.
@ -1186,18 +1182,18 @@ type Region struct {
// Origin represents the location of a pixel in an image or texture relative // Origin represents the location of a pixel in an image or texture relative
// to the upper-left corner, whose coordinates are (0, 0). // to the upper-left corner, whose coordinates are (0, 0).
// //
// Reference: https://developer.apple.com/documentation/metal/mtlorigin. // Reference: https://developer.apple.com/documentation/metal/mtlorigin?language=objc.
type Origin struct{ X, Y, Z int } type Origin struct{ X, Y, Z int }
// Size represents the set of dimensions that declare the size of an object, // Size represents the set of dimensions that declare the size of an object,
// such as an image, texture, threadgroup, or grid. // such as an image, texture, threadgroup, or grid.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlsize. // Reference: https://developer.apple.com/documentation/metal/mtlsize?language=objc.
type Size struct{ Width, Height, Depth int } type Size struct{ Width, Height, Depth int }
// RegionMake2D returns a 2D, rectangular region for image or texture data. // RegionMake2D returns a 2D, rectangular region for image or texture data.
// //
// Reference: https://developer.apple.com/documentation/metal/1515675-mtlregionmake2d. // Reference: https://developer.apple.com/documentation/metal/1515675-mtlregionmake2d?language=objc.
func RegionMake2D(x, y, width, height int) Region { func RegionMake2D(x, y, width, height int) Region {
return Region{ return Region{
Origin: Origin{x, y, 0}, Origin: Origin{x, y, 0},
@ -1216,7 +1212,7 @@ type Viewport struct {
// ScissorRect represents a rectangle for the scissor fragment test. // ScissorRect represents a rectangle for the scissor fragment test.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlscissorrect // Reference: https://developer.apple.com/documentation/metal/mtlscissorrect?language=objc.
type ScissorRect struct { type ScissorRect struct {
X int X int
Y int Y int
@ -1226,7 +1222,7 @@ type ScissorRect struct {
// DepthStencilState is a depth and stencil state object that specifies the depth and stencil configuration and operations used in a render pass. // DepthStencilState is a depth and stencil state object that specifies the depth and stencil configuration and operations used in a render pass.
// //
// Reference: https://developer.apple.com/documentation/metal/mtldepthstencilstate // Reference: https://developer.apple.com/documentation/metal/mtldepthstencilstate?language=objc.
type DepthStencilState struct { type DepthStencilState struct {
depthStencilState objc.ID depthStencilState objc.ID
} }
@ -1237,7 +1233,7 @@ func (d DepthStencilState) Release() {
// DepthStencilDescriptor is an object that configures new MTLDepthStencilState objects. // DepthStencilDescriptor is an object that configures new MTLDepthStencilState objects.
// //
// Reference: https://developer.apple.com/documentation/metal/mtldepthstencildescriptor // Reference: https://developer.apple.com/documentation/metal/mtldepthstencildescriptor?language=objc.
type DepthStencilDescriptor struct { type DepthStencilDescriptor struct {
// BackFaceStencil is the stencil descriptor for back-facing primitives. // BackFaceStencil is the stencil descriptor for back-facing primitives.
BackFaceStencil StencilDescriptor BackFaceStencil StencilDescriptor
@ -1248,7 +1244,7 @@ type DepthStencilDescriptor struct {
// StencilDescriptor is an object that defines the front-facing or back-facing stencil operations of a depth and stencil state object. // StencilDescriptor is an object that defines the front-facing or back-facing stencil operations of a depth and stencil state object.
// //
// Reference: https://developer.apple.com/documentation/metal/mtlstencildescriptor // Reference: https://developer.apple.com/documentation/metal/mtlstencildescriptor?language=objc.
type StencilDescriptor struct { type StencilDescriptor struct {
// StencilFailureOperation is the operation that is performed to update the values in the stencil attachment when the stencil test fails. // StencilFailureOperation is the operation that is performed to update the values in the stencil attachment when the stencil test fails.
StencilFailureOperation StencilOperation StencilFailureOperation StencilOperation

View File

@ -56,15 +56,15 @@ fragment float4 FragmentShader(Vertex in [[stage_in]]) {
return in.color; return in.color;
} }
` `
lib, err := device.MakeLibrary(source, mtl.CompileOptions{}) lib, err := device.NewLibraryWithSource(source, mtl.CompileOptions{})
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
vs, err := lib.MakeFunction("VertexShader") vs, err := lib.NewFunctionWithName("VertexShader")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
fs, err := lib.MakeFunction("FragmentShader") fs, err := lib.NewFunctionWithName("FragmentShader")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -72,7 +72,7 @@ fragment float4 FragmentShader(Vertex in [[stage_in]]) {
rpld.VertexFunction = vs rpld.VertexFunction = vs
rpld.FragmentFunction = fs rpld.FragmentFunction = fs
rpld.ColorAttachments[0].PixelFormat = mtl.PixelFormatRGBA8UNorm rpld.ColorAttachments[0].PixelFormat = mtl.PixelFormatRGBA8UNorm
rps, err := device.MakeRenderPipelineState(rpld) rps, err := device.NewRenderPipelineStateWithDescriptor(rpld)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -87,7 +87,7 @@ fragment float4 FragmentShader(Vertex in [[stage_in]]) {
{f32.Vec4{-0.75, -0.75, 0, 1}, f32.Vec4{0, 1, 0, 1}}, {f32.Vec4{-0.75, -0.75, 0, 1}, f32.Vec4{0, 1, 0, 1}},
{f32.Vec4{+0.75, -0.75, 0, 1}, f32.Vec4{0, 0, 1, 1}}, {f32.Vec4{+0.75, -0.75, 0, 1}, f32.Vec4{0, 0, 1, 1}},
} }
vertexBuffer := device.MakeBufferWithBytes(unsafe.Pointer(&vertexData[0]), unsafe.Sizeof(vertexData), mtl.ResourceStorageModeManaged) vertexBuffer := device.NewBufferWithBytes(unsafe.Pointer(&vertexData[0]), unsafe.Sizeof(vertexData), mtl.ResourceStorageModeManaged)
// Create an output texture to render into. // Create an output texture to render into.
td := mtl.TextureDescriptor{ td := mtl.TextureDescriptor{
@ -97,10 +97,10 @@ fragment float4 FragmentShader(Vertex in [[stage_in]]) {
Height: 512, Height: 512,
StorageMode: mtl.StorageModeManaged, StorageMode: mtl.StorageModeManaged,
} }
texture := device.MakeTexture(td) texture := device.NewTextureWithDescriptor(td)
cq := device.MakeCommandQueue() cq := device.NewCommandQueue()
cb := cq.MakeCommandBuffer() cb := cq.CommandBuffer()
// Encode all render commands. // Encode all render commands.
var rpd mtl.RenderPassDescriptor var rpd mtl.RenderPassDescriptor
@ -108,14 +108,14 @@ fragment float4 FragmentShader(Vertex in [[stage_in]]) {
rpd.ColorAttachments[0].StoreAction = mtl.StoreActionStore rpd.ColorAttachments[0].StoreAction = mtl.StoreActionStore
rpd.ColorAttachments[0].ClearColor = mtl.ClearColor{Red: 0.35, Green: 0.65, Blue: 0.85, Alpha: 1} rpd.ColorAttachments[0].ClearColor = mtl.ClearColor{Red: 0.35, Green: 0.65, Blue: 0.85, Alpha: 1}
rpd.ColorAttachments[0].Texture = texture rpd.ColorAttachments[0].Texture = texture
rce := cb.MakeRenderCommandEncoder(rpd) rce := cb.RenderCommandEncoderWithDescriptor(rpd)
rce.SetRenderPipelineState(rps) rce.SetRenderPipelineState(rps)
rce.SetVertexBuffer(vertexBuffer, 0, 0) rce.SetVertexBuffer(vertexBuffer, 0, 0)
rce.DrawPrimitives(mtl.PrimitiveTypeTriangle, 0, 3) rce.DrawPrimitives(mtl.PrimitiveTypeTriangle, 0, 3)
rce.EndEncoding() rce.EndEncoding()
// Encode all blit commands. // Encode all blit commands.
bce := cb.MakeBlitCommandEncoder() bce := cb.BlitCommandEncoder()
bce.Synchronize(texture) bce.Synchronize(texture)
bce.EndEncoding() bce.EndEncoding()

View File

@ -104,28 +104,28 @@ func (s *Shader) Dispose() {
func (s *Shader) init(device mtl.Device) error { func (s *Shader) init(device mtl.Device) error {
var src string var src string
if libBin := thePrecompiledLibraries.get(s.ir.SourceHash); len(libBin) > 0 { if libBin := thePrecompiledLibraries.get(s.ir.SourceHash); len(libBin) > 0 {
lib, err := device.MakeLibraryWithData(libBin) lib, err := device.NewLibraryWithData(libBin)
if err != nil { if err != nil {
return err return err
} }
s.lib = lib s.lib = lib
} else { } else {
src = msl.Compile(s.ir) src = msl.Compile(s.ir)
lib, err := device.MakeLibrary(src, mtl.CompileOptions{}) lib, err := device.NewLibraryWithSource(src, mtl.CompileOptions{})
if err != nil { if err != nil {
return fmt.Errorf("metal: device.MakeLibrary failed: %w, source: %s", err, src) return fmt.Errorf("metal: device.MakeLibrary failed: %w, source: %s", err, src)
} }
s.lib = lib s.lib = lib
} }
vs, err := s.lib.MakeFunction(msl.VertexName) vs, err := s.lib.NewFunctionWithName(msl.VertexName)
if err != nil { if err != nil {
if src != "" { if src != "" {
return fmt.Errorf("metal: lib.MakeFunction for vertex failed: %w, source: %s", err, src) return fmt.Errorf("metal: lib.MakeFunction for vertex failed: %w, source: %s", err, src)
} }
return fmt.Errorf("metal: lib.MakeFunction for vertex failed: %w", err) return fmt.Errorf("metal: lib.MakeFunction for vertex failed: %w", err)
} }
fs, err := s.lib.MakeFunction(msl.FragmentName) fs, err := s.lib.NewFunctionWithName(msl.FragmentName)
if err != nil { if err != nil {
if src != "" { if src != "" {
return fmt.Errorf("metal: lib.MakeFunction for fragment failed: %w, source: %s", err, src) return fmt.Errorf("metal: lib.MakeFunction for fragment failed: %w, source: %s", err, src)
@ -176,7 +176,7 @@ func (s *Shader) RenderPipelineState(view *view, blend graphicsdriver.Blend, ste
rpld.ColorAttachments[0].WriteMask = mtl.ColorWriteMaskNone rpld.ColorAttachments[0].WriteMask = mtl.ColorWriteMaskNone
} }
rps, err := view.getMTLDevice().MakeRenderPipelineState(rpld) rps, err := view.getMTLDevice().NewRenderPipelineStateWithDescriptor(rpld)
if err != nil { if err != nil {
return mtl.RenderPipelineState{}, err return mtl.RenderPipelineState{}, err
} }