2022-02-11 12:38:45 +01:00
|
|
|
// Copyright 2022 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 directx
|
|
|
|
|
|
|
|
import (
|
2023-03-26 10:39:49 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-03-27 07:14:22 +02:00
|
|
|
"math"
|
2023-03-25 16:49:30 +01:00
|
|
|
"os"
|
2023-03-26 10:39:49 +02:00
|
|
|
"runtime"
|
2023-03-29 09:22:20 +02:00
|
|
|
"strconv"
|
2023-03-25 16:49:30 +01:00
|
|
|
"strings"
|
2023-03-26 10:39:49 +02:00
|
|
|
"time"
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"golang.org/x/sys/windows"
|
2023-03-25 16:49:30 +01:00
|
|
|
|
2022-02-11 12:38:45 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
2023-03-27 07:14:22 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/microsoftgdk"
|
2023-03-30 19:07:35 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/winver"
|
2022-02-11 12:38:45 +01:00
|
|
|
)
|
|
|
|
|
2023-03-27 07:14:22 +02:00
|
|
|
type stencilMode int
|
|
|
|
|
|
|
|
const (
|
|
|
|
prepareStencil stencilMode = iota
|
|
|
|
drawWithStencil
|
|
|
|
noStencil
|
|
|
|
)
|
|
|
|
|
2023-03-26 10:39:49 +02:00
|
|
|
const frameCount = 2
|
|
|
|
|
2023-03-27 07:14:22 +02:00
|
|
|
func pow2(x uint32) uint32 {
|
|
|
|
if x > (math.MaxUint32+1)/2 {
|
|
|
|
return math.MaxUint32
|
|
|
|
}
|
|
|
|
|
|
|
|
var p2 uint32 = 1
|
|
|
|
for p2 < x {
|
|
|
|
p2 *= 2
|
|
|
|
}
|
|
|
|
return p2
|
|
|
|
}
|
|
|
|
|
2023-03-29 09:22:20 +02:00
|
|
|
func parseFeatureLevel(str string) (_D3D_FEATURE_LEVEL, bool) {
|
|
|
|
switch str {
|
|
|
|
case "11_0":
|
|
|
|
return _D3D_FEATURE_LEVEL_11_0, true
|
|
|
|
case "11_1":
|
|
|
|
return _D3D_FEATURE_LEVEL_11_1, true
|
|
|
|
case "12_0":
|
|
|
|
return _D3D_FEATURE_LEVEL_12_0, true
|
|
|
|
case "12_1":
|
|
|
|
return _D3D_FEATURE_LEVEL_12_1, true
|
|
|
|
case "12_2":
|
|
|
|
return _D3D_FEATURE_LEVEL_12_2, true
|
|
|
|
default:
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-03 15:40:39 +02:00
|
|
|
// NewGraphics creates an implementation of graphicsdriver.Graphics for DirectX.
|
2022-06-17 05:19:32 +02:00
|
|
|
// The returned graphics value is nil iff the error is not nil.
|
2022-06-17 04:39:43 +02:00
|
|
|
func NewGraphics() (graphicsdriver.Graphics, error) {
|
2023-03-29 18:45:22 +02:00
|
|
|
if !isD3DCompilerDLLAvailable() {
|
|
|
|
return nil, fmt.Errorf("directx: d3dcompiler_*.dll is missing in this environment")
|
|
|
|
}
|
|
|
|
|
2023-03-27 07:14:22 +02:00
|
|
|
var useWARP bool
|
|
|
|
var useDebugLayer bool
|
|
|
|
version := 11
|
|
|
|
|
2023-03-29 09:22:20 +02:00
|
|
|
// Specify the feature level 11 by default.
|
|
|
|
// Some old cards don't work well with the default feature level (#2447, #2486).
|
|
|
|
featureLevel := _D3D_FEATURE_LEVEL_11_0
|
|
|
|
|
|
|
|
// Parse a special environment variable for backward compatibility.
|
|
|
|
if env := os.Getenv("EBITENGINE_DIRECTX_FEATURE_LEVEL"); env != "" {
|
|
|
|
if fl, ok := parseFeatureLevel(env); ok {
|
|
|
|
featureLevel = fl
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-25 16:49:30 +01:00
|
|
|
env := os.Getenv("EBITENGINE_DIRECTX")
|
|
|
|
if env == "" {
|
|
|
|
// For backward compatibility, read the EBITEN_ version.
|
|
|
|
env = os.Getenv("EBITEN_DIRECTX")
|
|
|
|
}
|
2023-03-29 09:22:20 +02:00
|
|
|
|
2023-03-25 16:49:30 +01:00
|
|
|
for _, t := range strings.Split(env, ",") {
|
2023-03-29 09:22:20 +02:00
|
|
|
t := strings.TrimSpace(t)
|
|
|
|
switch {
|
|
|
|
case t == "warp":
|
2023-03-25 16:49:30 +01:00
|
|
|
// TODO: Is WARP available on Xbox?
|
|
|
|
useWARP = true
|
2023-03-29 09:22:20 +02:00
|
|
|
case t == "debug":
|
2023-03-25 16:49:30 +01:00
|
|
|
useDebugLayer = true
|
2023-03-29 09:22:20 +02:00
|
|
|
case strings.HasPrefix(t, "version="):
|
|
|
|
v, err := strconv.Atoi(t[len("version="):])
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
version = v
|
|
|
|
case strings.HasPrefix(t, "featurelevel="):
|
|
|
|
fl, ok := parseFeatureLevel(t[len("featurelevel="):])
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
featureLevel = fl
|
2023-03-25 16:49:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-27 07:14:22 +02:00
|
|
|
// On Xbox, only DirectX 12 is available.
|
|
|
|
if microsoftgdk.IsXbox() {
|
|
|
|
version = 12
|
2023-03-25 16:49:30 +01:00
|
|
|
}
|
|
|
|
|
2023-03-27 07:14:22 +02:00
|
|
|
switch version {
|
|
|
|
case 11:
|
|
|
|
g, err := newGraphics11(useWARP, useDebugLayer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return g, nil
|
|
|
|
case 12:
|
|
|
|
g, err := newGraphics12(useWARP, useDebugLayer, featureLevel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return g, nil
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("directx: unexpected DirectX version: %d", version))
|
2022-02-11 12:38:45 +01:00
|
|
|
}
|
|
|
|
}
|
2023-03-26 10:39:49 +02:00
|
|
|
|
|
|
|
type graphicsInfra struct {
|
2023-03-29 17:44:36 +02:00
|
|
|
factory *_IDXGIFactory
|
|
|
|
swapChain *_IDXGISwapChain
|
|
|
|
swapChain4 *_IDXGISwapChain4
|
2023-03-26 10:39:49 +02:00
|
|
|
|
2023-09-17 20:26:49 +02:00
|
|
|
allowTearing bool
|
|
|
|
|
2023-03-26 10:39:49 +02:00
|
|
|
// occluded reports whether the screen is invisible or not.
|
|
|
|
occluded bool
|
|
|
|
|
|
|
|
// lastTime is the last time for rendering.
|
|
|
|
lastTime time.Time
|
2023-03-30 14:08:20 +02:00
|
|
|
|
|
|
|
bufferCount int
|
2023-03-26 10:39:49 +02:00
|
|
|
}
|
|
|
|
|
2023-05-01 12:17:04 +02:00
|
|
|
// newGraphicsInfra takes the ownership of the given factory.
|
|
|
|
func newGraphicsInfra(factory *_IDXGIFactory) (*graphicsInfra, error) {
|
2023-03-26 10:39:49 +02:00
|
|
|
g := &graphicsInfra{
|
2023-05-01 12:17:04 +02:00
|
|
|
factory: factory,
|
2023-03-26 10:39:49 +02:00
|
|
|
}
|
|
|
|
runtime.SetFinalizer(g, (*graphicsInfra).release)
|
|
|
|
|
2023-09-17 20:26:49 +02:00
|
|
|
if f, err := g.factory.QueryInterface(&_IID_IDXGIFactory5); err == nil && f != nil {
|
|
|
|
factory := (*_IDXGIFactory5)(f)
|
|
|
|
defer factory.Release()
|
|
|
|
|
|
|
|
var allowTearing int32
|
|
|
|
if err := factory.CheckFeatureSupport(_DXGI_FEATURE_PRESENT_ALLOW_TEARING, unsafe.Pointer(&allowTearing), uint32(unsafe.Sizeof(allowTearing))); err == nil && allowTearing != 0 {
|
|
|
|
g.allowTearing = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-26 10:39:49 +02:00
|
|
|
return g, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *graphicsInfra) release() {
|
|
|
|
if g.factory != nil {
|
|
|
|
g.factory.Release()
|
|
|
|
g.factory = nil
|
|
|
|
}
|
|
|
|
if g.swapChain != nil {
|
|
|
|
g.swapChain.Release()
|
|
|
|
g.swapChain = nil
|
|
|
|
}
|
2023-03-29 17:44:36 +02:00
|
|
|
if g.swapChain4 != nil {
|
|
|
|
g.swapChain4.Release()
|
|
|
|
g.swapChain4 = nil
|
|
|
|
}
|
2023-03-26 10:39:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// appendAdapters appends found adapters to the given adapters.
|
|
|
|
// Releasing them is the caller's responsibility.
|
2023-03-29 08:32:49 +02:00
|
|
|
//
|
|
|
|
// warpForDX12 is valid only for DirectX 12.
|
|
|
|
func (g *graphicsInfra) appendAdapters(adapters []*_IDXGIAdapter1, warpForDX12 bool) ([]*_IDXGIAdapter1, error) {
|
2023-03-29 16:55:35 +02:00
|
|
|
f, err := g.factory.QueryInterface(&_IID_IDXGIFactory4)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if f == nil {
|
|
|
|
return nil, fmt.Errorf("directx: IID_IDXGIFactory4 was not available")
|
|
|
|
}
|
|
|
|
factory4 := (*_IDXGIFactory4)(f)
|
|
|
|
defer factory4.Release()
|
|
|
|
|
2023-03-29 08:32:49 +02:00
|
|
|
if warpForDX12 {
|
2023-03-29 16:55:35 +02:00
|
|
|
a, err := factory4.EnumWarpAdapter()
|
2023-03-26 10:39:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
adapters = append(adapters, a)
|
|
|
|
return adapters, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := uint32(0); ; i++ {
|
2023-03-29 16:55:35 +02:00
|
|
|
a, err := factory4.EnumAdapters1(i)
|
2023-03-26 10:39:49 +02:00
|
|
|
if errors.Is(err, _DXGI_ERROR_NOT_FOUND) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
adapters = append(adapters, a)
|
|
|
|
}
|
|
|
|
|
|
|
|
return adapters, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *graphicsInfra) isSwapChainInited() bool {
|
|
|
|
return g.swapChain != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *graphicsInfra) initSwapChain(width, height int, device unsafe.Pointer, window windows.HWND) (ferr error) {
|
|
|
|
if g.swapChain != nil {
|
|
|
|
return fmt.Errorf("directx: swap chain must not be initialized at initSwapChain, but is already done")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a swap chain.
|
|
|
|
//
|
|
|
|
// DXGI_ALPHA_MODE_PREMULTIPLIED doesn't work with a HWND well.
|
|
|
|
//
|
|
|
|
// IDXGIFactory::CreateSwapChain: Alpha blended swapchains must be created with CreateSwapChainForComposition,
|
|
|
|
// or CreateSwapChainForCoreWindow with the DXGI_SWAP_CHAIN_FLAG_FOREGROUND_LAYER flag
|
2023-03-30 05:31:05 +02:00
|
|
|
//
|
|
|
|
// Use *_SEQUENTIAL swap effects to follow the Mozilla way:
|
|
|
|
// https://github.com/mozilla/gecko-dev/blob/0907529ff72c456ddb47839f5f7ba16291f28dce/gfx/layers/d3d11/CompositorD3D11.cpp#L167-L254
|
2023-03-29 16:36:39 +02:00
|
|
|
desc := &_DXGI_SWAP_CHAIN_DESC{
|
|
|
|
BufferDesc: _DXGI_MODE_DESC{
|
|
|
|
Width: uint32(width),
|
|
|
|
Height: uint32(height),
|
|
|
|
Format: _DXGI_FORMAT_B8G8R8A8_UNORM,
|
|
|
|
},
|
2023-03-26 10:39:49 +02:00
|
|
|
SampleDesc: _DXGI_SAMPLE_DESC{
|
|
|
|
Count: 1,
|
|
|
|
Quality: 0,
|
|
|
|
},
|
2023-03-29 16:36:39 +02:00
|
|
|
BufferUsage: _DXGI_USAGE_RENDER_TARGET_OUTPUT,
|
|
|
|
BufferCount: frameCount,
|
|
|
|
OutputWindow: window,
|
|
|
|
Windowed: 1,
|
2023-03-30 05:31:05 +02:00
|
|
|
SwapEffect: _DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL,
|
|
|
|
}
|
|
|
|
|
|
|
|
// DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL/DISCARD are not supported for older Windows than 10 or DirectX 12.
|
|
|
|
// https://learn.microsoft.com/en-us/windows/win32/api/dxgi/ne-dxgi-dxgi_swap_effect
|
2023-03-30 19:07:35 +02:00
|
|
|
if !winver.IsWindows10OrGreater() {
|
2023-03-30 05:31:05 +02:00
|
|
|
desc.SwapEffect = _DXGI_SWAP_EFFECT_SEQUENTIAL
|
2023-03-30 14:08:20 +02:00
|
|
|
desc.BufferCount = 1
|
2023-03-26 10:39:49 +02:00
|
|
|
}
|
2023-03-30 05:31:05 +02:00
|
|
|
|
2023-03-30 14:08:20 +02:00
|
|
|
g.bufferCount = int(desc.BufferCount)
|
2023-09-17 20:26:49 +02:00
|
|
|
|
|
|
|
if g.allowTearing {
|
|
|
|
desc.Flags |= uint32(_DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING)
|
|
|
|
}
|
2023-03-29 16:36:39 +02:00
|
|
|
s, err := g.factory.CreateSwapChain(device, desc)
|
2023-03-26 10:39:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-29 17:13:58 +02:00
|
|
|
g.swapChain = s
|
2023-03-26 10:39:49 +02:00
|
|
|
defer func() {
|
|
|
|
if ferr != nil {
|
|
|
|
g.release()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-03-29 17:44:36 +02:00
|
|
|
if s4, err := g.swapChain.QueryInterface(&_IID_IDXGISwapChain4); err == nil && s4 != nil {
|
|
|
|
g.swapChain4 = (*_IDXGISwapChain4)(s4)
|
|
|
|
}
|
|
|
|
|
2023-03-26 10:39:49 +02:00
|
|
|
// MakeWindowAssociation should be called after swap chain creation.
|
|
|
|
// https://docs.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-idxgifactory-makewindowassociation
|
|
|
|
if err := g.factory.MakeWindowAssociation(window, _DXGI_MWA_NO_WINDOW_CHANGES|_DXGI_MWA_NO_ALT_ENTER); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *graphicsInfra) resizeSwapChain(width, height int) error {
|
|
|
|
if g.swapChain == nil {
|
|
|
|
return fmt.Errorf("directx: swap chain must be initialized at resizeSwapChain, but is not")
|
|
|
|
}
|
|
|
|
|
2023-09-17 20:26:49 +02:00
|
|
|
var flag uint32
|
|
|
|
if g.allowTearing {
|
|
|
|
flag |= uint32(_DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING)
|
|
|
|
}
|
|
|
|
if err := g.swapChain.ResizeBuffers(uint32(g.bufferCount), uint32(width), uint32(height), _DXGI_FORMAT_B8G8R8A8_UNORM, flag); err != nil {
|
2023-03-26 10:39:49 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-29 17:44:36 +02:00
|
|
|
func (g *graphicsInfra) currentBackBufferIndex() (int, error) {
|
|
|
|
if g.swapChain4 == nil {
|
|
|
|
return 0, fmt.Errorf("directx: IDXGISwapChain4 is not available")
|
|
|
|
}
|
|
|
|
return int(g.swapChain4.GetCurrentBackBufferIndex()), nil
|
2023-03-26 10:39:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *graphicsInfra) present(vsyncEnabled bool) error {
|
|
|
|
if g.swapChain == nil {
|
|
|
|
return fmt.Errorf("directx: swap chain must be initialized at present, but is not")
|
|
|
|
}
|
|
|
|
|
|
|
|
var syncInterval uint32
|
|
|
|
var flags _DXGI_PRESENT
|
|
|
|
if g.occluded {
|
|
|
|
// The screen is not visible. Test whether we can resume.
|
|
|
|
flags |= _DXGI_PRESENT_TEST
|
|
|
|
} else {
|
|
|
|
// Do actual rendering only when the screen is visible.
|
|
|
|
if vsyncEnabled {
|
|
|
|
syncInterval = 1
|
2023-09-17 20:26:49 +02:00
|
|
|
} else if g.allowTearing {
|
|
|
|
flags |= _DXGI_PRESENT_ALLOW_TEARING
|
2023-03-26 10:39:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
occluded, err := g.swapChain.Present(syncInterval, uint32(flags))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
g.occluded = occluded
|
|
|
|
|
|
|
|
// Reduce FPS when the screen is invisible.
|
|
|
|
now := time.Now()
|
|
|
|
if g.occluded {
|
|
|
|
if delta := 100*time.Millisecond - now.Sub(g.lastTime); delta > 0 {
|
|
|
|
time.Sleep(delta)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g.lastTime = now
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *graphicsInfra) getBuffer(buffer uint32, riid *windows.GUID) (unsafe.Pointer, error) {
|
|
|
|
return g.swapChain.GetBuffer(buffer, riid)
|
|
|
|
}
|