| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package mtldriver |
| 8 |
|
| 9 |
import ( |
| 10 |
"image" |
| 11 |
"image/color" |
| 12 |
"log" |
| 13 |
|
| 14 |
"dmitri.shuralyov.com/gpu/mtl" |
| 15 |
"github.com/go-gl/glfw/v3.3/glfw" |
| 16 |
"golang.org/x/exp/shiny/driver/internal/drawer" |
| 17 |
"golang.org/x/exp/shiny/driver/internal/event" |
| 18 |
"golang.org/x/exp/shiny/driver/internal/lifecycler" |
| 19 |
"golang.org/x/exp/shiny/driver/mtldriver/internal/coreanim" |
| 20 |
"golang.org/x/exp/shiny/screen" |
| 21 |
"golang.org/x/image/draw" |
| 22 |
"golang.org/x/image/math/f64" |
| 23 |
"golang.org/x/mobile/event/size" |
| 24 |
) |
| 25 |
|
| 26 |
|
| 27 |
type windowImpl struct { |
| 28 |
device mtl.Device |
| 29 |
window *glfw.Window |
| 30 |
releaseWindowCh chan releaseWindowReq |
| 31 |
ml coreanim.MetalLayer |
| 32 |
cq mtl.CommandQueue |
| 33 |
|
| 34 |
event.Deque |
| 35 |
lifecycler lifecycler.State |
| 36 |
|
| 37 |
rgba *image.RGBA |
| 38 |
texture mtl.Texture |
| 39 |
} |
| 40 |
|
| 41 |
func (w *windowImpl) Release() { |
| 42 |
respCh := make(chan struct{}) |
| 43 |
w.releaseWindowCh <- releaseWindowReq{ |
| 44 |
window: w.window, |
| 45 |
respCh: respCh, |
| 46 |
} |
| 47 |
glfw.PostEmptyEvent() |
| 48 |
<-respCh |
| 49 |
} |
| 50 |
|
| 51 |
func (w *windowImpl) NextEvent() interface{} { |
| 52 |
e := w.Deque.NextEvent() |
| 53 |
if sz, ok := e.(size.Event); ok { |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
w.ml.SetDrawableSize(sz.WidthPx, sz.HeightPx) |
| 59 |
w.rgba = image.NewRGBA(image.Rectangle{Max: image.Point{X: sz.WidthPx, Y: sz.HeightPx}}) |
| 60 |
w.texture = w.device.MakeTexture(mtl.TextureDescriptor{ |
| 61 |
PixelFormat: mtl.PixelFormatRGBA8UNorm, |
| 62 |
Width: sz.WidthPx, |
| 63 |
Height: sz.HeightPx, |
| 64 |
StorageMode: mtl.StorageModeManaged, |
| 65 |
}) |
| 66 |
} |
| 67 |
return e |
| 68 |
} |
| 69 |
|
| 70 |
func (w *windowImpl) Publish() screen.PublishResult { |
| 71 |
|
| 72 |
region := mtl.RegionMake2D(0, 0, w.texture.Width, w.texture.Height) |
| 73 |
bytesPerRow := 4 * w.texture.Width |
| 74 |
w.texture.ReplaceRegion(region, 0, &w.rgba.Pix[0], uintptr(bytesPerRow)) |
| 75 |
|
| 76 |
drawable, err := w.ml.NextDrawable() |
| 77 |
if err != nil { |
| 78 |
log.Println("Window.Publish: couldn't get the next drawable:", err) |
| 79 |
return screen.PublishResult{} |
| 80 |
} |
| 81 |
|
| 82 |
cb := w.cq.MakeCommandBuffer() |
| 83 |
|
| 84 |
|
| 85 |
bce := cb.MakeBlitCommandEncoder() |
| 86 |
bce.CopyFromTexture( |
| 87 |
w.texture, 0, 0, mtl.Origin{}, mtl.Size{w.texture.Width, w.texture.Height, 1}, |
| 88 |
drawable.Texture(), 0, 0, mtl.Origin{}) |
| 89 |
bce.EndEncoding() |
| 90 |
|
| 91 |
cb.PresentDrawable(drawable) |
| 92 |
cb.Commit() |
| 93 |
|
| 94 |
return screen.PublishResult{} |
| 95 |
} |
| 96 |
|
| 97 |
func (w *windowImpl) Upload(dp image.Point, src screen.Buffer, sr image.Rectangle) { |
| 98 |
draw.Draw(w.rgba, sr.Sub(sr.Min).Add(dp), src.RGBA(), sr.Min, draw.Src) |
| 99 |
} |
| 100 |
|
| 101 |
func (w *windowImpl) Fill(dr image.Rectangle, src color.Color, op draw.Op) { |
| 102 |
draw.Draw(w.rgba, dr, &image.Uniform{src}, image.Point{}, op) |
| 103 |
} |
| 104 |
|
| 105 |
func (w *windowImpl) Draw(src2dst f64.Aff3, src screen.Texture, sr image.Rectangle, op draw.Op, _ *screen.DrawOptions) { |
| 106 |
draw.NearestNeighbor.Transform(w.rgba, src2dst, src.(*textureImpl).rgba, sr, op, nil) |
| 107 |
} |
| 108 |
|
| 109 |
func (w *windowImpl) DrawUniform(src2dst f64.Aff3, src color.Color, sr image.Rectangle, op draw.Op, _ *screen.DrawOptions) { |
| 110 |
draw.NearestNeighbor.Transform(w.rgba, src2dst, &image.Uniform{src}, sr, op, nil) |
| 111 |
} |
| 112 |
|
| 113 |
func (w *windowImpl) Copy(dp image.Point, src screen.Texture, sr image.Rectangle, op draw.Op, opts *screen.DrawOptions) { |
| 114 |
drawer.Copy(w, dp, src, sr, op, opts) |
| 115 |
} |
| 116 |
|
| 117 |
func (w *windowImpl) Scale(dr image.Rectangle, src screen.Texture, sr image.Rectangle, op draw.Op, opts *screen.DrawOptions) { |
| 118 |
drawer.Scale(w, dr, src, sr, op, opts) |
| 119 |
} |