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.
|
|
|
|
|
|
|
|
// +build darwin
|
|
|
|
|
|
|
|
package metal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/internal/affine"
|
|
|
|
"github.com/hajimehoshi/ebiten/internal/graphics"
|
|
|
|
"github.com/hajimehoshi/ebiten/internal/graphicsdriver"
|
|
|
|
"github.com/hajimehoshi/ebiten/internal/graphicsdriver/metal/ca"
|
|
|
|
"github.com/hajimehoshi/ebiten/internal/graphicsdriver/metal/mtl"
|
|
|
|
"github.com/hajimehoshi/ebiten/internal/graphicsdriver/metal/ns"
|
|
|
|
"github.com/hajimehoshi/ebiten/internal/mainthread"
|
|
|
|
)
|
|
|
|
|
|
|
|
const source = `#include <metal_stdlib>
|
|
|
|
|
2019-02-13 13:46:39 +01:00
|
|
|
#define FILTER_NEAREST {{.FilterNearest}}
|
|
|
|
#define FILTER_LINEAR {{.FilterLinear}}
|
|
|
|
#define FILTER_SCREEN {{.FilterScreen}}
|
2018-11-12 16:00:10 +01:00
|
|
|
|
2019-02-13 13:46:39 +01:00
|
|
|
#define ADDRESS_CLAMP_TO_ZERO {{.AddressClampToZero}}
|
|
|
|
#define ADDRESS_REPEAT {{.AddressRepeat}}
|
2018-12-23 19:00:00 +01:00
|
|
|
|
2018-11-12 16:00:10 +01:00
|
|
|
using namespace metal;
|
|
|
|
|
|
|
|
struct VertexIn {
|
|
|
|
packed_float2 position;
|
2018-12-22 17:40:43 +01:00
|
|
|
packed_float2 tex;
|
|
|
|
packed_float4 tex_region;
|
2018-11-12 16:00:10 +01:00
|
|
|
packed_float4 color;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct VertexOut {
|
|
|
|
float4 position [[position]];
|
|
|
|
float2 tex;
|
2018-12-22 17:40:43 +01:00
|
|
|
float4 tex_region;
|
2018-11-12 16:00:10 +01:00
|
|
|
float4 color;
|
|
|
|
};
|
|
|
|
|
|
|
|
vertex VertexOut VertexShader(
|
|
|
|
uint vid [[vertex_id]],
|
|
|
|
device VertexIn* vertices [[buffer(0)]],
|
|
|
|
constant float2& viewport_size [[buffer(1)]]
|
|
|
|
) {
|
|
|
|
float4x4 projectionMatrix = float4x4(
|
|
|
|
float4(2.0 / viewport_size.x, 0, 0, 0),
|
|
|
|
float4(0, -2.0 / viewport_size.y, 0, 0),
|
|
|
|
float4(0, 0, 1, 0),
|
|
|
|
float4(-1, 1, 0, 1)
|
|
|
|
);
|
|
|
|
|
|
|
|
VertexIn in = vertices[vid];
|
|
|
|
|
|
|
|
VertexOut out = {
|
|
|
|
.position = projectionMatrix * float4(in.position, 0, 1),
|
2018-12-22 17:40:43 +01:00
|
|
|
.tex = in.tex,
|
|
|
|
.tex_region = in.tex_region,
|
2018-11-12 16:00:10 +01:00
|
|
|
.color = in.color,
|
|
|
|
};
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2018-12-20 18:37:58 +01:00
|
|
|
// AdjustTexels adjust texels.
|
|
|
|
// See #669, #759
|
|
|
|
float2 AdjustTexel(float2 source_size, float2 p0, float2 p1) {
|
|
|
|
const float2 texel_size = 1.0 / source_size;
|
|
|
|
if (fract((p1.x-p0.x)*source_size.x) == 0.0) {
|
|
|
|
p1.x -= texel_size.x / 512.0;
|
|
|
|
}
|
|
|
|
if (fract((p1.y-p0.y)*source_size.y) == 0.0) {
|
|
|
|
p1.y -= texel_size.y / 512.0;
|
|
|
|
}
|
|
|
|
return p1;
|
|
|
|
}
|
|
|
|
|
2018-12-24 18:24:02 +01:00
|
|
|
float FloorMod(float x, float y) {
|
2018-12-23 19:00:00 +01:00
|
|
|
if (x < 0.0) {
|
|
|
|
return y - (-x - y * floor(-x/y));
|
|
|
|
}
|
|
|
|
return x - y * floor(x/y);
|
|
|
|
}
|
|
|
|
|
2019-02-13 13:46:39 +01:00
|
|
|
template<uint8_t address>
|
2019-02-14 01:57:39 +01:00
|
|
|
float2 AdjustTexelByAddress(float2 p, float4 tex_region);
|
|
|
|
|
|
|
|
template<>
|
2019-02-15 01:42:25 +01:00
|
|
|
inline float2 AdjustTexelByAddress<ADDRESS_CLAMP_TO_ZERO>(float2 p, float4 tex_region) {
|
2019-02-14 01:57:39 +01:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2019-02-15 01:42:25 +01:00
|
|
|
inline float2 AdjustTexelByAddress<ADDRESS_REPEAT>(float2 p, float4 tex_region) {
|
2019-02-14 01:57:39 +01:00
|
|
|
float2 o = float2(tex_region[0], tex_region[1]);
|
|
|
|
float2 size = float2(tex_region[2] - tex_region[0], tex_region[3] - tex_region[1]);
|
|
|
|
return float2(FloorMod((p.x - o.x), size.x) + o.x, FloorMod((p.y - o.y), size.y) + o.y);
|
2018-12-23 19:00:00 +01:00
|
|
|
}
|
|
|
|
|
2019-02-13 13:46:39 +01:00
|
|
|
template<uint8_t filter, uint8_t address>
|
2019-02-15 01:42:25 +01:00
|
|
|
struct GetColorFromTexel;
|
2018-11-12 16:00:10 +01:00
|
|
|
|
2019-02-15 01:42:25 +01:00
|
|
|
template<uint8_t address>
|
|
|
|
struct GetColorFromTexel<FILTER_NEAREST, address> {
|
|
|
|
inline float4 Do(VertexOut v, texture2d<float> texture, constant float2& source_size, float scale) {
|
|
|
|
constexpr sampler texture_sampler(filter::nearest);
|
|
|
|
const float2 texel_size = 1 / source_size;
|
2018-11-12 16:00:10 +01:00
|
|
|
|
2019-02-13 13:46:39 +01:00
|
|
|
float2 p = AdjustTexelByAddress<address>(v.tex, v.tex_region);
|
2018-12-23 19:00:00 +01:00
|
|
|
if (p.x < v.tex_region[0] ||
|
|
|
|
p.y < v.tex_region[1] ||
|
2019-02-16 05:38:23 +01:00
|
|
|
v.tex_region[2] <= p.x ||
|
|
|
|
v.tex_region[3] <= p.y) {
|
2019-02-15 01:42:25 +01:00
|
|
|
return 0.0;
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
2019-02-15 01:42:25 +01:00
|
|
|
return texture.sample(texture_sampler, p);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<uint8_t address>
|
|
|
|
struct GetColorFromTexel<FILTER_LINEAR, address> {
|
|
|
|
inline float4 Do(VertexOut v, texture2d<float> texture, constant float2& source_size, float scale) {
|
|
|
|
constexpr sampler texture_sampler(filter::nearest);
|
|
|
|
const float2 texel_size = 1 / source_size;
|
|
|
|
|
2018-11-12 16:00:10 +01:00
|
|
|
float2 p0 = v.tex - texel_size / 2.0;
|
|
|
|
float2 p1 = v.tex + texel_size / 2.0;
|
2018-12-20 18:37:58 +01:00
|
|
|
p1 = AdjustTexel(source_size, p0, p1);
|
2019-02-13 13:46:39 +01:00
|
|
|
p0 = AdjustTexelByAddress<address>(p0, v.tex_region);
|
|
|
|
p1 = AdjustTexelByAddress<address>(p1, v.tex_region);
|
2018-11-12 16:00:10 +01:00
|
|
|
|
|
|
|
float4 c0 = texture.sample(texture_sampler, p0);
|
|
|
|
float4 c1 = texture.sample(texture_sampler, float2(p1.x, p0.y));
|
|
|
|
float4 c2 = texture.sample(texture_sampler, float2(p0.x, p1.y));
|
|
|
|
float4 c3 = texture.sample(texture_sampler, p1);
|
|
|
|
|
2018-12-22 17:40:43 +01:00
|
|
|
if (p0.x < v.tex_region[0]) {
|
2018-11-12 16:00:10 +01:00
|
|
|
c0 = 0;
|
|
|
|
c2 = 0;
|
|
|
|
}
|
2018-12-22 17:40:43 +01:00
|
|
|
if (p0.y < v.tex_region[1]) {
|
2018-11-12 16:00:10 +01:00
|
|
|
c0 = 0;
|
|
|
|
c1 = 0;
|
|
|
|
}
|
2019-02-16 05:38:23 +01:00
|
|
|
if (v.tex_region[2] <= p1.x) {
|
2018-11-12 16:00:10 +01:00
|
|
|
c1 = 0;
|
|
|
|
c3 = 0;
|
|
|
|
}
|
2019-02-16 05:38:23 +01:00
|
|
|
if (v.tex_region[3] <= p1.y) {
|
2018-11-12 16:00:10 +01:00
|
|
|
c2 = 0;
|
|
|
|
c3 = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
float2 rate = fract(p0 * source_size);
|
2019-02-15 01:42:25 +01:00
|
|
|
return mix(mix(c0, c1, rate.x), mix(c2, c3, rate.x), rate.y);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<uint8_t address>
|
|
|
|
struct GetColorFromTexel<FILTER_SCREEN, address> {
|
|
|
|
inline float4 Do(VertexOut v, texture2d<float> texture, constant float2& source_size, float scale) {
|
|
|
|
constexpr sampler texture_sampler(filter::nearest);
|
|
|
|
const float2 texel_size = 1 / source_size;
|
|
|
|
|
2018-11-12 16:00:10 +01:00
|
|
|
float2 p0 = v.tex - texel_size / 2.0 / scale;
|
|
|
|
float2 p1 = v.tex + texel_size / 2.0 / scale;
|
2018-12-20 18:37:58 +01:00
|
|
|
p1 = AdjustTexel(source_size, p0, p1);
|
2018-11-12 16:00:10 +01:00
|
|
|
|
|
|
|
float4 c0 = texture.sample(texture_sampler, p0);
|
|
|
|
float4 c1 = texture.sample(texture_sampler, float2(p1.x, p0.y));
|
|
|
|
float4 c2 = texture.sample(texture_sampler, float2(p0.x, p1.y));
|
|
|
|
float4 c3 = texture.sample(texture_sampler, p1);
|
|
|
|
|
|
|
|
float2 rate_center = float2(1.0, 1.0) - texel_size / 2.0 / scale;
|
|
|
|
float2 rate = clamp(((fract(p0 * source_size) - rate_center) * scale) + rate_center, 0.0, 1.0);
|
2019-02-15 01:42:25 +01:00
|
|
|
return mix(mix(c0, c1, rate.x), mix(c2, c3, rate.x), rate.y);
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
2019-02-15 01:42:25 +01:00
|
|
|
};
|
2018-11-12 16:00:10 +01:00
|
|
|
|
2019-02-15 01:42:25 +01:00
|
|
|
template<uint8_t filter, uint8_t address>
|
|
|
|
float4 FragmentShaderImpl(
|
|
|
|
VertexOut v,
|
|
|
|
texture2d<float> texture,
|
|
|
|
constant float2& source_size,
|
|
|
|
constant float4x4& color_matrix_body,
|
|
|
|
constant float4& color_matrix_translation,
|
|
|
|
constant float& scale) {
|
|
|
|
float4 c = GetColorFromTexel<filter, address>().Do(v, texture, source_size, scale);
|
2018-11-12 16:00:10 +01:00
|
|
|
if (0 < c.a) {
|
|
|
|
c.rgb /= c.a;
|
|
|
|
}
|
|
|
|
c = (color_matrix_body * c) + color_matrix_translation;
|
|
|
|
c *= v.color;
|
2018-12-20 09:04:03 +01:00
|
|
|
c = clamp(c, 0.0, 1.0);
|
2018-11-12 16:00:10 +01:00
|
|
|
c.rgb *= c.a;
|
|
|
|
return c;
|
|
|
|
}
|
2019-02-13 13:46:39 +01:00
|
|
|
|
2019-02-13 15:36:45 +01:00
|
|
|
// Define Foo and FooCp macros to force macro replacement.
|
2019-02-13 13:46:39 +01:00
|
|
|
// See "6.10.3.1 Argument substitution" in ISO/IEC 9899.
|
|
|
|
|
|
|
|
#define FragmentShaderFunc(filter, address) \
|
2019-02-13 15:36:45 +01:00
|
|
|
FragmentShaderFuncCp(filter, address)
|
2019-02-13 13:46:39 +01:00
|
|
|
|
|
|
|
#define FragmentShaderFuncCp(filter, address) \
|
|
|
|
fragment float4 FragmentShader_##filter##_##address( \
|
|
|
|
VertexOut v [[stage_in]], \
|
|
|
|
texture2d<float> texture [[texture(0)]], \
|
2019-02-14 02:54:32 +01:00
|
|
|
constant float2& source_size [[buffer(2)]], \
|
|
|
|
constant float4x4& color_matrix_body [[buffer(3)]], \
|
|
|
|
constant float4& color_matrix_translation [[buffer(4)]], \
|
|
|
|
constant float& scale [[buffer(5)]]) { \
|
2019-02-14 01:57:39 +01:00
|
|
|
return FragmentShaderImpl<filter, address>( \
|
2019-02-14 02:54:32 +01:00
|
|
|
v, texture, source_size, color_matrix_body, color_matrix_translation, scale); \
|
2019-02-13 13:46:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
FragmentShaderFunc(FILTER_NEAREST, ADDRESS_CLAMP_TO_ZERO)
|
|
|
|
FragmentShaderFunc(FILTER_LINEAR, ADDRESS_CLAMP_TO_ZERO)
|
|
|
|
FragmentShaderFunc(FILTER_SCREEN, ADDRESS_CLAMP_TO_ZERO)
|
|
|
|
FragmentShaderFunc(FILTER_NEAREST, ADDRESS_REPEAT)
|
|
|
|
FragmentShaderFunc(FILTER_LINEAR, ADDRESS_REPEAT)
|
|
|
|
|
|
|
|
#undef FragmentShaderFuncName
|
2018-11-12 16:00:10 +01:00
|
|
|
`
|
|
|
|
|
2019-02-13 13:46:39 +01:00
|
|
|
type rpsKey struct {
|
|
|
|
filter graphics.Filter
|
|
|
|
address graphics.Address
|
|
|
|
compositeMode graphics.CompositeMode
|
|
|
|
}
|
|
|
|
|
2018-11-12 16:00:10 +01:00
|
|
|
type Driver struct {
|
|
|
|
window uintptr
|
|
|
|
|
|
|
|
device mtl.Device
|
|
|
|
ml ca.MetalLayer
|
|
|
|
screenRPS mtl.RenderPipelineState
|
2019-02-13 13:46:39 +01:00
|
|
|
rpss map[rpsKey]mtl.RenderPipelineState
|
2018-11-12 16:00:10 +01:00
|
|
|
cq mtl.CommandQueue
|
|
|
|
cb mtl.CommandBuffer
|
|
|
|
|
|
|
|
screenDrawable ca.MetalDrawable
|
|
|
|
|
2019-02-04 19:22:48 +01:00
|
|
|
vb mtl.Buffer
|
|
|
|
ib mtl.Buffer
|
2018-11-12 16:00:10 +01:00
|
|
|
|
|
|
|
src *Image
|
|
|
|
dst *Image
|
|
|
|
|
|
|
|
maxImageSize int
|
|
|
|
}
|
|
|
|
|
2018-12-27 18:18:38 +01:00
|
|
|
var theDriver Driver
|
2018-11-12 16:00:10 +01:00
|
|
|
|
|
|
|
func Get() *Driver {
|
|
|
|
return &theDriver
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) SetWindow(window uintptr) {
|
2018-12-27 18:43:00 +01:00
|
|
|
mainthread.Run(func() error {
|
|
|
|
// Note that [NSApp mainWindow] returns nil when the window is borderless.
|
|
|
|
// Then the window is needed to be given.
|
|
|
|
d.window = window
|
|
|
|
return nil
|
|
|
|
})
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) SetVertices(vertices []float32, indices []uint16) {
|
|
|
|
mainthread.Run(func() error {
|
2019-02-04 19:22:48 +01:00
|
|
|
if d.vb != (mtl.Buffer{}) {
|
|
|
|
d.vb.Release()
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
2019-02-04 19:22:48 +01:00
|
|
|
if d.ib != (mtl.Buffer{}) {
|
|
|
|
d.ib.Release()
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
2019-02-04 19:22:48 +01:00
|
|
|
d.vb = d.device.MakeBufferWithBytes(unsafe.Pointer(&vertices[0]), unsafe.Sizeof(vertices[0])*uintptr(len(vertices)), mtl.ResourceStorageModeManaged)
|
|
|
|
d.ib = d.device.MakeBufferWithBytes(unsafe.Pointer(&indices[0]), unsafe.Sizeof(indices[0])*uintptr(len(indices)), mtl.ResourceStorageModeManaged)
|
2018-11-12 16:00:10 +01:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) Flush() {
|
2019-02-01 20:35:38 +01:00
|
|
|
d.flush(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) flush(wait bool) {
|
2018-11-12 16:00:10 +01:00
|
|
|
mainthread.Run(func() error {
|
|
|
|
if d.cb == (mtl.CommandBuffer{}) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.screenDrawable != (ca.MetalDrawable{}) {
|
|
|
|
d.cb.PresentDrawable(d.screenDrawable)
|
|
|
|
}
|
|
|
|
d.cb.Commit()
|
2019-02-01 20:35:38 +01:00
|
|
|
if wait {
|
|
|
|
d.cb.WaitUntilCompleted()
|
|
|
|
}
|
2018-11-12 16:00:10 +01:00
|
|
|
|
|
|
|
d.cb = mtl.CommandBuffer{}
|
|
|
|
d.screenDrawable = ca.MetalDrawable{}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) checkSize(width, height int) {
|
|
|
|
m := 0
|
|
|
|
mainthread.Run(func() error {
|
|
|
|
if d.maxImageSize == 0 {
|
|
|
|
d.maxImageSize = 4096
|
|
|
|
// https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
|
|
|
|
switch {
|
|
|
|
case d.device.SupportsFeatureSet(mtl.FeatureSet_iOS_GPUFamily5_v1):
|
|
|
|
d.maxImageSize = 16384
|
|
|
|
case d.device.SupportsFeatureSet(mtl.FeatureSet_iOS_GPUFamily4_v1):
|
|
|
|
d.maxImageSize = 16384
|
|
|
|
case d.device.SupportsFeatureSet(mtl.FeatureSet_iOS_GPUFamily3_v1):
|
|
|
|
d.maxImageSize = 16384
|
|
|
|
case d.device.SupportsFeatureSet(mtl.FeatureSet_iOS_GPUFamily2_v2):
|
|
|
|
d.maxImageSize = 8192
|
|
|
|
case d.device.SupportsFeatureSet(mtl.FeatureSet_iOS_GPUFamily2_v1):
|
|
|
|
d.maxImageSize = 4096
|
|
|
|
case d.device.SupportsFeatureSet(mtl.FeatureSet_iOS_GPUFamily1_v2):
|
|
|
|
d.maxImageSize = 8192
|
|
|
|
case d.device.SupportsFeatureSet(mtl.FeatureSet_iOS_GPUFamily1_v1):
|
|
|
|
d.maxImageSize = 4096
|
|
|
|
case d.device.SupportsFeatureSet(mtl.FeatureSet_tvOS_GPUFamily2_v1):
|
|
|
|
d.maxImageSize = 16384
|
|
|
|
case d.device.SupportsFeatureSet(mtl.FeatureSet_tvOS_GPUFamily1_v1):
|
|
|
|
d.maxImageSize = 8192
|
|
|
|
case d.device.SupportsFeatureSet(mtl.FeatureSet_macOS_GPUFamily1_v1):
|
|
|
|
d.maxImageSize = 16384
|
|
|
|
default:
|
|
|
|
panic("metal: there is no supported feature set")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m = d.maxImageSize
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if width < 1 {
|
2019-02-14 13:04:34 +01:00
|
|
|
panic(fmt.Sprintf("metal: width (%d) must be equal or more than %d", width, 1))
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
|
|
|
if height < 1 {
|
2019-02-14 13:04:34 +01:00
|
|
|
panic(fmt.Sprintf("metal: height (%d) must be equal or more than %d", height, 1))
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
|
|
|
if width > m {
|
|
|
|
panic(fmt.Sprintf("metal: width (%d) must be less than or equal to %d", width, m))
|
|
|
|
}
|
|
|
|
if height > m {
|
|
|
|
panic(fmt.Sprintf("metal: height (%d) must be less than or equal to %d", height, m))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) NewImage(width, height int) (graphicsdriver.Image, error) {
|
|
|
|
d.checkSize(width, height)
|
|
|
|
td := mtl.TextureDescriptor{
|
|
|
|
PixelFormat: mtl.PixelFormatRGBA8UNorm,
|
2019-02-14 13:04:34 +01:00
|
|
|
Width: graphics.InternalImageSize(width),
|
|
|
|
Height: graphics.InternalImageSize(height),
|
2018-11-12 16:00:10 +01:00
|
|
|
StorageMode: mtl.StorageModeManaged,
|
|
|
|
|
|
|
|
// MTLTextureUsageRenderTarget might cause a problematic render result. Not sure the reason.
|
|
|
|
// Usage: mtl.TextureUsageShaderRead | mtl.TextureUsageRenderTarget
|
|
|
|
Usage: mtl.TextureUsageShaderRead,
|
|
|
|
}
|
|
|
|
var t mtl.Texture
|
|
|
|
mainthread.Run(func() error {
|
|
|
|
t = d.device.MakeTexture(td)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return &Image{
|
|
|
|
driver: d,
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
texture: t,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) NewScreenFramebufferImage(width, height int) (graphicsdriver.Image, error) {
|
|
|
|
mainthread.Run(func() error {
|
|
|
|
d.ml.SetDrawableSize(width, height)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return &Image{
|
|
|
|
driver: d,
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
screen: true,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) Reset() error {
|
|
|
|
if err := mainthread.Run(func() error {
|
|
|
|
if d.cq != (mtl.CommandQueue{}) {
|
|
|
|
d.cq.Release()
|
|
|
|
d.cq = mtl.CommandQueue{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Release existing rpss
|
|
|
|
if d.rpss == nil {
|
2019-02-13 13:46:39 +01:00
|
|
|
d.rpss = map[rpsKey]mtl.RenderPipelineState{}
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
d.device, err = mtl.CreateSystemDefaultDevice()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.ml = ca.MakeMetalLayer()
|
|
|
|
d.ml.SetDevice(d.device)
|
|
|
|
// https://developer.apple.com/documentation/quartzcore/cametallayer/1478155-pixelformat
|
|
|
|
//
|
|
|
|
// The pixel format for a Metal layer must be MTLPixelFormatBGRA8Unorm,
|
|
|
|
// MTLPixelFormatBGRA8Unorm_sRGB, MTLPixelFormatRGBA16Float, MTLPixelFormatBGRA10_XR, or
|
|
|
|
// MTLPixelFormatBGRA10_XR_sRGB.
|
|
|
|
d.ml.SetPixelFormat(mtl.PixelFormatBGRA8UNorm)
|
|
|
|
d.ml.SetMaximumDrawableCount(3)
|
|
|
|
|
|
|
|
replaces := map[string]string{
|
2018-12-23 19:00:00 +01:00
|
|
|
"{{.FilterNearest}}": fmt.Sprintf("%d", graphics.FilterNearest),
|
|
|
|
"{{.FilterLinear}}": fmt.Sprintf("%d", graphics.FilterLinear),
|
|
|
|
"{{.FilterScreen}}": fmt.Sprintf("%d", graphics.FilterScreen),
|
|
|
|
"{{.AddressClampToZero}}": fmt.Sprintf("%d", graphics.AddressClampToZero),
|
|
|
|
"{{.AddressRepeat}}": fmt.Sprintf("%d", graphics.AddressRepeat),
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
|
|
|
src := source
|
|
|
|
for k, v := range replaces {
|
|
|
|
src = strings.Replace(src, k, v, -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
lib, err := d.device.MakeLibrary(src, mtl.CompileOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
vs, err := lib.MakeFunction("VertexShader")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-13 13:46:39 +01:00
|
|
|
fs, err := lib.MakeFunction(
|
|
|
|
fmt.Sprintf("FragmentShader_%d_%d", graphics.FilterScreen, graphics.AddressClampToZero))
|
2018-11-12 16:00:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rpld := mtl.RenderPipelineDescriptor{
|
|
|
|
VertexFunction: vs,
|
|
|
|
FragmentFunction: fs,
|
|
|
|
}
|
|
|
|
rpld.ColorAttachments[0].PixelFormat = d.ml.PixelFormat()
|
|
|
|
rpld.ColorAttachments[0].BlendingEnabled = true
|
|
|
|
rpld.ColorAttachments[0].DestinationAlphaBlendFactor = mtl.BlendFactorZero
|
|
|
|
rpld.ColorAttachments[0].DestinationRGBBlendFactor = mtl.BlendFactorZero
|
|
|
|
rpld.ColorAttachments[0].SourceAlphaBlendFactor = mtl.BlendFactorOne
|
|
|
|
rpld.ColorAttachments[0].SourceRGBBlendFactor = mtl.BlendFactorOne
|
|
|
|
rps, err := d.device.MakeRenderPipelineState(rpld)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.screenRPS = rps
|
|
|
|
|
|
|
|
conv := func(c graphics.Operation) mtl.BlendFactor {
|
|
|
|
switch c {
|
|
|
|
case graphics.Zero:
|
|
|
|
return mtl.BlendFactorZero
|
|
|
|
case graphics.One:
|
|
|
|
return mtl.BlendFactorOne
|
|
|
|
case graphics.SrcAlpha:
|
|
|
|
return mtl.BlendFactorSourceAlpha
|
|
|
|
case graphics.DstAlpha:
|
|
|
|
return mtl.BlendFactorDestinationAlpha
|
|
|
|
case graphics.OneMinusSrcAlpha:
|
|
|
|
return mtl.BlendFactorOneMinusSourceAlpha
|
|
|
|
case graphics.OneMinusDstAlpha:
|
|
|
|
return mtl.BlendFactorOneMinusDestinationAlpha
|
|
|
|
default:
|
2019-02-07 09:19:24 +01:00
|
|
|
panic(fmt.Sprintf("metal: invalid operation: %d", c))
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-13 13:46:39 +01:00
|
|
|
for _, a := range []graphics.Address{
|
|
|
|
graphics.AddressClampToZero,
|
|
|
|
graphics.AddressRepeat,
|
|
|
|
} {
|
|
|
|
for _, f := range []graphics.Filter{
|
|
|
|
graphics.FilterNearest,
|
|
|
|
graphics.FilterLinear,
|
|
|
|
} {
|
|
|
|
for c := graphics.CompositeModeSourceOver; c <= graphics.CompositeModeMax; c++ {
|
|
|
|
fs, err := lib.MakeFunction(fmt.Sprintf("FragmentShader_%d_%d", f, a))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rpld := mtl.RenderPipelineDescriptor{
|
|
|
|
VertexFunction: vs,
|
|
|
|
FragmentFunction: fs,
|
|
|
|
}
|
|
|
|
rpld.ColorAttachments[0].PixelFormat = mtl.PixelFormatRGBA8UNorm
|
|
|
|
rpld.ColorAttachments[0].BlendingEnabled = true
|
|
|
|
|
|
|
|
src, dst := c.Operations()
|
|
|
|
rpld.ColorAttachments[0].DestinationAlphaBlendFactor = conv(dst)
|
|
|
|
rpld.ColorAttachments[0].DestinationRGBBlendFactor = conv(dst)
|
|
|
|
rpld.ColorAttachments[0].SourceAlphaBlendFactor = conv(src)
|
|
|
|
rpld.ColorAttachments[0].SourceRGBBlendFactor = conv(src)
|
|
|
|
rps, err := d.device.MakeRenderPipelineState(rpld)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.rpss[rpsKey{
|
|
|
|
filter: f,
|
|
|
|
address: a,
|
|
|
|
compositeMode: c,
|
|
|
|
}] = rps
|
|
|
|
}
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d.cq = d.device.MakeCommandQueue()
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-23 19:00:00 +01:00
|
|
|
func (d *Driver) Draw(indexLen int, indexOffset int, mode graphics.CompositeMode, colorM *affine.ColorM, filter graphics.Filter, address graphics.Address) error {
|
2018-11-12 16:00:10 +01:00
|
|
|
if err := mainthread.Run(func() error {
|
|
|
|
// NSView can be changed anytime (probably). Set this everyframe.
|
|
|
|
cocoaWindow := ns.NewWindow(unsafe.Pointer(d.window))
|
|
|
|
cocoaWindow.ContentView().SetLayer(d.ml)
|
|
|
|
cocoaWindow.ContentView().SetWantsLayer(true)
|
|
|
|
|
|
|
|
rpd := mtl.RenderPassDescriptor{}
|
|
|
|
if d.dst.screen {
|
|
|
|
rpd.ColorAttachments[0].LoadAction = mtl.LoadActionDontCare
|
|
|
|
rpd.ColorAttachments[0].StoreAction = mtl.StoreActionStore
|
|
|
|
} else {
|
|
|
|
rpd.ColorAttachments[0].LoadAction = mtl.LoadActionLoad
|
|
|
|
rpd.ColorAttachments[0].StoreAction = mtl.StoreActionStore
|
|
|
|
}
|
|
|
|
var t mtl.Texture
|
|
|
|
if d.dst.screen {
|
|
|
|
if d.screenDrawable == (ca.MetalDrawable{}) {
|
|
|
|
drawable, err := d.ml.NextDrawable()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.screenDrawable = drawable
|
|
|
|
}
|
|
|
|
t = d.screenDrawable.Texture()
|
|
|
|
} else {
|
|
|
|
d.screenDrawable = ca.MetalDrawable{}
|
|
|
|
t = d.dst.texture
|
|
|
|
}
|
|
|
|
rpd.ColorAttachments[0].Texture = t
|
|
|
|
rpd.ColorAttachments[0].ClearColor = mtl.ClearColor{}
|
|
|
|
|
|
|
|
w, h := d.dst.viewportSize()
|
|
|
|
|
|
|
|
if d.cb == (mtl.CommandBuffer{}) {
|
|
|
|
d.cb = d.cq.MakeCommandBuffer()
|
|
|
|
}
|
|
|
|
rce := d.cb.MakeRenderCommandEncoder(rpd)
|
|
|
|
|
|
|
|
if d.dst.screen {
|
|
|
|
rce.SetRenderPipelineState(d.screenRPS)
|
|
|
|
} else {
|
2019-02-13 13:46:39 +01:00
|
|
|
rce.SetRenderPipelineState(d.rpss[rpsKey{
|
|
|
|
filter: filter,
|
|
|
|
address: address,
|
|
|
|
compositeMode: mode,
|
|
|
|
}])
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
|
|
|
rce.SetViewport(mtl.Viewport{0, 0, float64(w), float64(h), -1, 1})
|
2019-02-04 19:22:48 +01:00
|
|
|
rce.SetVertexBuffer(d.vb, 0, 0)
|
2018-11-12 16:00:10 +01:00
|
|
|
|
|
|
|
viewportSize := [...]float32{float32(w), float32(h)}
|
|
|
|
rce.SetVertexBytes(unsafe.Pointer(&viewportSize[0]), unsafe.Sizeof(viewportSize), 1)
|
|
|
|
|
2019-02-14 03:09:16 +01:00
|
|
|
sourceSize := [...]float32{
|
2019-02-14 13:04:34 +01:00
|
|
|
float32(graphics.InternalImageSize(d.src.width)),
|
|
|
|
float32(graphics.InternalImageSize(d.src.height)),
|
2019-02-14 02:54:32 +01:00
|
|
|
}
|
|
|
|
rce.SetFragmentBytes(unsafe.Pointer(&sourceSize[0]), unsafe.Sizeof(sourceSize), 2)
|
|
|
|
|
|
|
|
esBody, esTranslate := colorM.UnsafeElements()
|
|
|
|
rce.SetFragmentBytes(unsafe.Pointer(&esBody[0]), unsafe.Sizeof(esBody[0])*uintptr(len(esBody)), 3)
|
|
|
|
rce.SetFragmentBytes(unsafe.Pointer(&esTranslate[0]), unsafe.Sizeof(esTranslate[0])*uintptr(len(esTranslate)), 4)
|
2018-11-12 16:00:10 +01:00
|
|
|
|
|
|
|
scale := float32(d.dst.width) / float32(d.src.width)
|
2019-02-14 02:54:32 +01:00
|
|
|
rce.SetFragmentBytes(unsafe.Pointer(&scale), unsafe.Sizeof(scale), 5)
|
2018-11-12 16:00:10 +01:00
|
|
|
|
|
|
|
if d.src != nil {
|
|
|
|
rce.SetFragmentTexture(d.src.texture, 0)
|
|
|
|
} else {
|
|
|
|
rce.SetFragmentTexture(mtl.Texture{}, 0)
|
|
|
|
}
|
2019-02-04 19:22:48 +01:00
|
|
|
rce.DrawIndexedPrimitives(mtl.PrimitiveTypeTriangle, indexLen, mtl.IndexTypeUInt16, d.ib, indexOffset*2)
|
2018-11-12 16:00:10 +01:00
|
|
|
rce.EndEncoding()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) ResetSource() {
|
2018-12-27 18:43:00 +01:00
|
|
|
mainthread.Run(func() error {
|
|
|
|
d.src = nil
|
|
|
|
return nil
|
|
|
|
})
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) SetVsyncEnabled(enabled bool) {
|
2018-12-27 18:23:39 +01:00
|
|
|
// TODO: Now SetVsyncEnabled is called only from the main thread, and mainthread.Run is not available since
|
|
|
|
// recursive function call via Run is forbidden.
|
|
|
|
// Fix this to use mainthread.Run to avoid confusion.
|
2018-12-27 18:18:38 +01:00
|
|
|
d.ml.SetDisplaySyncEnabled(enabled)
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) VDirection() graphicsdriver.VDirection {
|
|
|
|
return graphicsdriver.VUpward
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) IsGL() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
type Image struct {
|
|
|
|
driver *Driver
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
screen bool
|
|
|
|
texture mtl.Texture
|
|
|
|
}
|
|
|
|
|
2018-12-27 18:43:00 +01:00
|
|
|
// viewportSize must be called from the main thread.
|
2018-11-12 16:00:10 +01:00
|
|
|
func (i *Image) viewportSize() (int, int) {
|
|
|
|
if i.screen {
|
|
|
|
return i.width, i.height
|
|
|
|
}
|
2019-02-14 13:04:34 +01:00
|
|
|
return graphics.InternalImageSize(i.width), graphics.InternalImageSize(i.height)
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) Dispose() {
|
2018-12-20 15:08:40 +01:00
|
|
|
mainthread.Run(func() error {
|
|
|
|
i.texture.Release()
|
|
|
|
return nil
|
|
|
|
})
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) IsInvalidated() bool {
|
|
|
|
// TODO: Does Metal cause context lost?
|
|
|
|
// https://developer.apple.com/documentation/metal/mtlresource/1515898-setpurgeablestate
|
|
|
|
// https://developer.apple.com/documentation/metal/mtldevicenotificationhandler
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) syncTexture() {
|
|
|
|
mainthread.Run(func() error {
|
|
|
|
if i.driver.cb != (mtl.CommandBuffer{}) {
|
2019-02-07 09:19:24 +01:00
|
|
|
panic("metal: command buffer must be empty at syncTexture: flush is not called yet?")
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cb := i.driver.cq.MakeCommandBuffer()
|
|
|
|
bce := cb.MakeBlitCommandEncoder()
|
|
|
|
bce.SynchronizeTexture(i.texture, 0, 0)
|
|
|
|
bce.EndEncoding()
|
|
|
|
cb.Commit()
|
|
|
|
cb.WaitUntilCompleted()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) Pixels() ([]byte, error) {
|
2019-02-01 20:35:38 +01:00
|
|
|
i.driver.flush(true)
|
2018-11-12 16:00:10 +01:00
|
|
|
i.syncTexture()
|
|
|
|
|
|
|
|
b := make([]byte, 4*i.width*i.height)
|
|
|
|
mainthread.Run(func() error {
|
|
|
|
i.texture.GetBytes(&b[0], uintptr(4*i.width), mtl.Region{
|
|
|
|
Size: mtl.Size{i.width, i.height, 1},
|
|
|
|
}, 0)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) SetAsDestination() {
|
2018-12-27 18:43:00 +01:00
|
|
|
mainthread.Run(func() error {
|
|
|
|
i.driver.dst = i
|
|
|
|
return nil
|
|
|
|
})
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) SetAsSource() {
|
2018-12-27 18:43:00 +01:00
|
|
|
mainthread.Run(func() error {
|
|
|
|
i.driver.src = i
|
|
|
|
return nil
|
|
|
|
})
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) ReplacePixels(pixels []byte, x, y, width, height int) {
|
2019-02-01 20:35:38 +01:00
|
|
|
i.driver.flush(true)
|
2018-11-12 16:00:10 +01:00
|
|
|
|
|
|
|
mainthread.Run(func() error {
|
|
|
|
i.texture.ReplaceRegion(mtl.Region{
|
|
|
|
Origin: mtl.Origin{x, y, 0},
|
|
|
|
Size: mtl.Size{width, height, 1},
|
|
|
|
}, 0, unsafe.Pointer(&pixels[0]), 4*width)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|