2018-11-12 16:00:10 +01:00
|
|
|
// Copyright 2018 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package mtl_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"image"
|
|
|
|
"image/color"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"unsafe"
|
|
|
|
|
2022-08-17 18:39:34 +02:00
|
|
|
"github.com/ebitengine/purego"
|
2018-11-12 16:00:10 +01:00
|
|
|
"golang.org/x/image/math/f32"
|
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal/mtl"
|
2018-11-12 16:00:10 +01:00
|
|
|
)
|
|
|
|
|
2022-08-17 18:39:34 +02:00
|
|
|
func init() {
|
|
|
|
// for these tests to pass Metal must be linked directly.
|
|
|
|
// it is not needed for any of the others nor for Ebitengine to work properly.
|
|
|
|
//go:cgo_import_dynamic _ _ "Metal.framework/Metal"
|
|
|
|
|
|
|
|
// It is also necessary for CoreGraphics to be linked
|
2023-03-10 15:24:03 +01:00
|
|
|
_, _ = purego.Dlopen("CoreGraphics.framework/CoreGraphics", purego.RTLD_LAZY|purego.RTLD_GLOBAL)
|
2022-08-17 18:39:34 +02:00
|
|
|
}
|
|
|
|
|
2018-11-12 16:00:10 +01:00
|
|
|
func Example_listDevices() {
|
2023-03-03 17:25:43 +01:00
|
|
|
device, err := mtl.CreateSystemDefaultDevice()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
|
|
|
printJSON("preferred system default Metal device = ", device)
|
|
|
|
|
2023-01-05 06:14:42 +01:00
|
|
|
// This test is not executed by `go test` command as this doesn't have an `Output:` comment.
|
2018-11-12 16:00:10 +01:00
|
|
|
|
|
|
|
// Sample output:
|
|
|
|
// all Metal devices in the system = [
|
|
|
|
// {
|
|
|
|
// "Headless": false,
|
|
|
|
// "LowPower": true,
|
|
|
|
// "Removable": false,
|
|
|
|
// "RegistryID": 4294968287,
|
|
|
|
// "Name": "Intel Iris Pro Graphics"
|
|
|
|
// },
|
|
|
|
// {
|
|
|
|
// "Headless": false,
|
|
|
|
// "LowPower": false,
|
|
|
|
// "Removable": false,
|
|
|
|
// "RegistryID": 4294968322,
|
|
|
|
// "Name": "AMD Radeon R9 M370X"
|
|
|
|
// }
|
|
|
|
// ]
|
|
|
|
// preferred system default Metal device = {
|
|
|
|
// "Headless": false,
|
|
|
|
// "LowPower": false,
|
|
|
|
// "Removable": false,
|
|
|
|
// "RegistryID": 4294968322,
|
|
|
|
// "Name": "AMD Radeon R9 M370X"
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
// printJSON prints label, then v as JSON encoded with indent to stdout. It panics on any error.
|
|
|
|
// It's meant to be used by examples to print the output.
|
2022-11-03 07:33:09 +01:00
|
|
|
func printJSON(label string, v any) {
|
2018-11-12 16:00:10 +01:00
|
|
|
fmt.Print(label)
|
|
|
|
w := json.NewEncoder(os.Stdout)
|
|
|
|
w.SetIndent("", "\t")
|
|
|
|
err := w.Encode(v)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Example_renderTriangle() {
|
2023-03-03 17:25:43 +01:00
|
|
|
device, err := mtl.CreateSystemDefaultDevice()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a render pipeline state.
|
|
|
|
const source = `#include <metal_stdlib>
|
|
|
|
|
|
|
|
using namespace metal;
|
|
|
|
|
|
|
|
struct Vertex {
|
|
|
|
float4 position [[position]];
|
|
|
|
float4 color;
|
|
|
|
};
|
|
|
|
|
|
|
|
vertex Vertex VertexShader(
|
|
|
|
uint vertexID [[vertex_id]],
|
|
|
|
device Vertex * vertices [[buffer(0)]]
|
|
|
|
) {
|
|
|
|
return vertices[vertexID];
|
|
|
|
}
|
|
|
|
|
|
|
|
fragment float4 FragmentShader(Vertex in [[stage_in]]) {
|
|
|
|
return in.color;
|
|
|
|
}
|
|
|
|
`
|
|
|
|
lib, err := device.MakeLibrary(source, mtl.CompileOptions{})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
vs, err := lib.MakeFunction("VertexShader")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
fs, err := lib.MakeFunction("FragmentShader")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
var rpld mtl.RenderPipelineDescriptor
|
|
|
|
rpld.VertexFunction = vs
|
|
|
|
rpld.FragmentFunction = fs
|
|
|
|
rpld.ColorAttachments[0].PixelFormat = mtl.PixelFormatRGBA8UNorm
|
2021-07-05 14:28:22 +02:00
|
|
|
rpld.ColorAttachments[0].WriteMask = mtl.ColorWriteMaskAll
|
2018-11-12 16:00:10 +01:00
|
|
|
rps, err := device.MakeRenderPipelineState(rpld)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a vertex buffer.
|
|
|
|
type Vertex struct {
|
|
|
|
Position f32.Vec4
|
|
|
|
Color f32.Vec4
|
|
|
|
}
|
|
|
|
vertexData := [...]Vertex{
|
|
|
|
{f32.Vec4{+0.00, +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}},
|
|
|
|
}
|
2019-02-07 10:42:54 +01:00
|
|
|
vertexBuffer := device.MakeBufferWithBytes(unsafe.Pointer(&vertexData[0]), unsafe.Sizeof(vertexData), mtl.ResourceStorageModeManaged)
|
2018-11-12 16:00:10 +01:00
|
|
|
|
|
|
|
// Create an output texture to render into.
|
|
|
|
td := mtl.TextureDescriptor{
|
2020-06-26 16:36:23 +02:00
|
|
|
TextureType: mtl.TextureType2D,
|
2018-11-12 16:00:10 +01:00
|
|
|
PixelFormat: mtl.PixelFormatRGBA8UNorm,
|
|
|
|
Width: 80,
|
|
|
|
Height: 20,
|
|
|
|
StorageMode: mtl.StorageModeManaged,
|
|
|
|
}
|
|
|
|
texture := device.MakeTexture(td)
|
|
|
|
|
|
|
|
cq := device.MakeCommandQueue()
|
|
|
|
cb := cq.MakeCommandBuffer()
|
|
|
|
|
|
|
|
// Encode all render commands.
|
|
|
|
var rpd mtl.RenderPassDescriptor
|
|
|
|
rpd.ColorAttachments[0].LoadAction = mtl.LoadActionClear
|
|
|
|
rpd.ColorAttachments[0].StoreAction = mtl.StoreActionStore
|
|
|
|
rpd.ColorAttachments[0].ClearColor = mtl.ClearColor{Red: 0, Green: 0, Blue: 0, Alpha: 1}
|
|
|
|
rpd.ColorAttachments[0].Texture = texture
|
|
|
|
rce := cb.MakeRenderCommandEncoder(rpd)
|
|
|
|
rce.SetRenderPipelineState(rps)
|
|
|
|
rce.SetVertexBuffer(vertexBuffer, 0, 0)
|
|
|
|
rce.DrawPrimitives(mtl.PrimitiveTypeTriangle, 0, 3)
|
|
|
|
rce.EndEncoding()
|
|
|
|
|
|
|
|
// Encode all blit commands.
|
|
|
|
bce := cb.MakeBlitCommandEncoder()
|
|
|
|
bce.Synchronize(texture)
|
|
|
|
bce.EndEncoding()
|
|
|
|
|
|
|
|
cb.Commit()
|
|
|
|
cb.WaitUntilCompleted()
|
|
|
|
|
|
|
|
// Read pixels from output texture into an image.
|
2020-06-28 11:15:25 +02:00
|
|
|
img := image.NewNRGBA(image.Rect(0, 0, texture.Width(), texture.Height()))
|
|
|
|
bytesPerRow := 4 * texture.Width()
|
|
|
|
region := mtl.RegionMake2D(0, 0, texture.Width(), texture.Height())
|
2018-11-12 16:00:10 +01:00
|
|
|
texture.GetBytes(&img.Pix[0], uintptr(bytesPerRow), region, 0)
|
|
|
|
|
|
|
|
// Output image to stdout as grayscale ASCII art.
|
|
|
|
levels := []struct {
|
|
|
|
MinY uint8
|
|
|
|
Shade string
|
|
|
|
}{{220, " "}, {170, "░"}, {85, "▒"}, {35, "▓"}, {0, "█"}}
|
|
|
|
for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ {
|
|
|
|
for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ {
|
|
|
|
c := color.GrayModel.Convert(img.At(x, y)).(color.Gray)
|
|
|
|
for _, l := range levels {
|
|
|
|
if c.Y >= l.MinY {
|
|
|
|
fmt.Print(l.Shade)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
// ████████████████████████████████████████████████████████████████████████████████
|
|
|
|
// ████████████████████████████████████████████████████████████████████████████████
|
|
|
|
// ████████████████████████████████████████████████████████████████████████████████
|
|
|
|
// ██████████████████████████████████████ ██████████████████████████████████████
|
|
|
|
// ████████████████████████████████████ ████████████████████████████████████
|
|
|
|
// ██████████████████████████████████ ░░░░██████████████████████████████████
|
|
|
|
// ████████████████████████████████ ░░░░░░░░████████████████████████████████
|
|
|
|
// ██████████████████████████████ ░░░░░░░░░░░░██████████████████████████████
|
|
|
|
// ████████████████████████████ ░░░░░░░░░░░░▒▒▒▒████████████████████████████
|
|
|
|
// ██████████████████████████ ░░░░░░░░░░░░▒▒▒▒▒▒▒▒██████████████████████████
|
|
|
|
// ████████████████████████ ░░░░░░░░░░░░▒▒▒▒▒▒▒▒▒▒▒▒████████████████████████
|
|
|
|
// ██████████████████████ ░░░░░░░░░░░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒██████████████████████
|
|
|
|
// ████████████████████ ░░░░░░░░░░░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒████████████████████
|
|
|
|
// ██████████████████ ░░░░░░░░░░░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓▓▓▓██████████████████
|
|
|
|
// ████████████████ ░░░░░░░░░░░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓▓▓▓▓▓▓▓████████████████
|
|
|
|
// ██████████████ ░░░░░░░░░░░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓▓▓▓▓▓▓▓▓▓▓▓██████████████
|
|
|
|
// ████████████ ░░░░░░░░░░░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓▓▓▓▓▓▓▓▓▓▓▓████████████████
|
|
|
|
// ████████████████████████████████████████████████████████████████████████████████
|
|
|
|
// ████████████████████████████████████████████████████████████████████████████████
|
|
|
|
// ████████████████████████████████████████████████████████████████████████████████
|
|
|
|
}
|