描画をAngleに変更 (#523)
* Angleに対応 * oops * 起動しない問題を修正 * 例外を正確にトレースするように * テクスチャ読み込みのクラッシュをトレースするように * バグ修正 * 要望で変更 * リザルトの雲を縮小
This commit is contained in:
parent
88c46bde28
commit
a7225d582a
@ -10,13 +10,7 @@
|
||||
<PackageReference Include="Silk.NET.Windowing" Version="2.17.1"/>
|
||||
<PackageReference Include="Silk.NET.Maths" Version="2.17.1"/>
|
||||
<PackageReference Include="Silk.NET.Input" Version="2.17.1"/>
|
||||
<PackageReference Include="Silk.NET.OpenGL" Version="2.17.1"/>
|
||||
<PackageReference Include="Silk.NET.Vulkan" Version="2.17.1"/>
|
||||
<PackageReference Include="Silk.NET.Direct3D9" Version="2.17.1"/>
|
||||
<PackageReference Include="Silk.NET.Direct3D11" Version="2.17.1"/>
|
||||
<PackageReference Include="Silk.NET.Direct3D12" Version="2.17.1"/>
|
||||
<PackageReference Include="Silk.NET.DXGI" Version="2.17.1"/>
|
||||
<PackageReference Include="Silk.NET.Direct3D.Compilers" Version="2.17.1"/>
|
||||
<PackageReference Include="Silk.NET.OpenGLES" Version="2.17.1"/>
|
||||
<PackageReference Include="ImGui.NET" Version="1.89.7.1"/>
|
||||
<PackageReference Include="ManagedBass" Version="3.1.1"/>
|
||||
<PackageReference Include="ManagedBass.Fx" Version="3.1.1"/>
|
||||
@ -27,7 +21,6 @@
|
||||
<PackageReference Include="FFmpeg.AutoGen" Version="5.1.1"/>
|
||||
<PackageReference Include="SkiaSharp.NativeAssets.macOS" Version="2.88.5"/>
|
||||
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="2.88.5"/>
|
||||
<PackageReference Include="Silk.NET.DXVK.Native" Version="2.17.0"/>
|
||||
</ItemGroup>
|
||||
<ItemGroup Label="Resources">
|
||||
<EmbeddedResource Include="mplus-1p-medium.ttf"/>
|
||||
|
@ -25,23 +25,24 @@ using System.Threading;
|
||||
using System.Collections.ObjectModel;
|
||||
using Silk.NET.Windowing;
|
||||
using Silk.NET.Maths;
|
||||
using Silk.NET.OpenGLES;
|
||||
using SkiaSharp;
|
||||
using FDK;
|
||||
using Silk.NET.GLFW;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace SampleFramework
|
||||
{
|
||||
public enum GraphicsDeviceType
|
||||
{
|
||||
OpenGL,
|
||||
Vulkan,
|
||||
DirectX11,
|
||||
DirectX12
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Presents an easy to use wrapper for making games and samples.
|
||||
/// </summary>
|
||||
public abstract class Game : IDisposable
|
||||
{
|
||||
public static GL Gl { get; private set; }
|
||||
public static Silk.NET.Core.Contexts.IGLContext Context { get; private set; }
|
||||
|
||||
internal static List<Action> AsyncActions = new();
|
||||
|
||||
protected string _Text = "";
|
||||
protected string Text
|
||||
{
|
||||
@ -59,16 +60,10 @@ namespace SampleFramework
|
||||
}
|
||||
}
|
||||
|
||||
public static GraphicsDeviceType GraphicsDeviceType_ = GraphicsDeviceType.OpenGL;
|
||||
public static AnglePlatformType GraphicsDeviceType_ = AnglePlatformType.OpenGL;
|
||||
|
||||
public IWindow Window_;
|
||||
|
||||
public static IShader Shader_;
|
||||
|
||||
public static IPolygon Polygon_;
|
||||
|
||||
public static IGraphicsDevice GraphicsDevice;
|
||||
|
||||
private Vector2D<int> _WindowSize;
|
||||
public Vector2D<int> WindowSize
|
||||
{
|
||||
@ -156,37 +151,69 @@ namespace SampleFramework
|
||||
}
|
||||
}
|
||||
|
||||
internal static int VerticalFix
|
||||
{
|
||||
get
|
||||
{
|
||||
return GraphicsDeviceType_ == GraphicsDeviceType.OpenGL ? -1 : 1;
|
||||
}
|
||||
}
|
||||
|
||||
public static int MainThreadID { get; private set; }
|
||||
|
||||
private GraphicsAPI GetGraphicsAPI()
|
||||
{
|
||||
switch (GraphicsDeviceType_)
|
||||
{
|
||||
case GraphicsDeviceType.OpenGL:
|
||||
return GraphicsAPI.Default;
|
||||
case GraphicsDeviceType.Vulkan:
|
||||
return GraphicsAPI.DefaultVulkan;
|
||||
default:
|
||||
return GraphicsAPI.None;
|
||||
}
|
||||
}
|
||||
|
||||
public unsafe SKBitmap GetScreenShot()
|
||||
{
|
||||
return GraphicsDevice.GetScreenPixels();
|
||||
int ViewportWidth = Window_.FramebufferSize.X;
|
||||
int ViewportHeight = Window_.FramebufferSize.Y;
|
||||
fixed(uint* pixels = new uint[(uint)ViewportWidth * (uint)ViewportHeight])
|
||||
{
|
||||
Gl.ReadBuffer(GLEnum.Front);
|
||||
Gl.ReadPixels(0, 0, (uint)ViewportWidth, (uint)ViewportHeight, PixelFormat.Bgra, GLEnum.UnsignedByte, pixels);
|
||||
|
||||
fixed(uint* pixels2 = new uint[(uint)ViewportWidth * (uint)ViewportHeight])
|
||||
{
|
||||
for(int x = 0; x < ViewportWidth; x++)
|
||||
{
|
||||
for(int y = 1; y < ViewportHeight; y++)
|
||||
{
|
||||
int pos = x + ((y - 1) * ViewportWidth);
|
||||
int pos2 = x + ((ViewportHeight - y) * ViewportWidth);
|
||||
var p = pixels[pos2];
|
||||
pixels2[pos] = p;
|
||||
}
|
||||
}
|
||||
|
||||
using SKBitmap sKBitmap = new(ViewportWidth, ViewportHeight - 1);
|
||||
sKBitmap.SetPixels((IntPtr)pixels2);
|
||||
return sKBitmap.Copy();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void GetScreenShotAsync(Action<SKBitmap> action)
|
||||
public unsafe void GetScreenShotAsync(Action<SKBitmap> action)
|
||||
{
|
||||
GraphicsDevice.GetScreenPixelsASync(action);
|
||||
int ViewportWidth = Window_.FramebufferSize.X;
|
||||
int ViewportHeight = Window_.FramebufferSize.Y;
|
||||
byte[] pixels = new byte[(uint)ViewportWidth * (uint)ViewportHeight * 4];
|
||||
Gl.ReadBuffer(GLEnum.Front);
|
||||
fixed(byte* pix = pixels)
|
||||
{
|
||||
Gl.ReadPixels(0, 0, (uint)ViewportWidth, (uint)ViewportHeight, PixelFormat.Bgra, GLEnum.UnsignedByte, pix);
|
||||
}
|
||||
|
||||
Task.Run(() =>{
|
||||
fixed(byte* pixels2 = new byte[(uint)ViewportWidth * (uint)ViewportHeight * 4])
|
||||
{
|
||||
for(int x = 0; x < ViewportWidth; x++)
|
||||
{
|
||||
for(int y = 1; y < ViewportHeight; y++)
|
||||
{
|
||||
int pos = x + ((y - 1) * ViewportWidth);
|
||||
int pos2 = x + ((ViewportHeight - y) * ViewportWidth);
|
||||
pixels2[(pos * 4) + 0] = pixels[(pos2 * 4) + 0];
|
||||
pixels2[(pos * 4) + 1] = pixels[(pos2 * 4) + 1];
|
||||
pixels2[(pos * 4) + 2] = pixels[(pos2 * 4) + 2];
|
||||
pixels2[(pos * 4) + 3] = 255;
|
||||
}
|
||||
}
|
||||
|
||||
using SKBitmap sKBitmap = new(ViewportWidth, ViewportHeight - 1);
|
||||
sKBitmap.SetPixels((IntPtr)pixels2);
|
||||
action(sKBitmap);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static long TimeMs;
|
||||
@ -201,11 +228,17 @@ namespace SampleFramework
|
||||
}
|
||||
}
|
||||
|
||||
//[DllImportAttribute("libEGL", EntryPoint = "eglGetError")]
|
||||
//public static extern Silk.NET.OpenGLES.ErrorCode GetError();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the <see cref="Game"/> class.
|
||||
/// </summary>
|
||||
static Game()
|
||||
{
|
||||
//GlfwProvider.UninitializedGLFW.Value.InitHint(InitHint.AnglePlatformType, (int)AnglePlatformType.OpenGL);
|
||||
//GlfwProvider.UninitializedGLFW.Value.Init();
|
||||
//GetError();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -215,8 +248,10 @@ namespace SampleFramework
|
||||
{
|
||||
MainThreadID = Thread.CurrentThread.ManagedThreadId;
|
||||
Configuration();
|
||||
|
||||
//GlfwProvider.GLFW.Value.WindowHint(WindowHintContextApi.ContextCreationApi, ContextApi.EglContextApi);
|
||||
|
||||
WindowOptions options = GraphicsDeviceType_ == GraphicsDeviceType.Vulkan ? WindowOptions.DefaultVulkan : WindowOptions.Default;
|
||||
WindowOptions options = WindowOptions.Default;
|
||||
|
||||
options.Size = WindowSize;
|
||||
options.Position = WindowPosition;
|
||||
@ -224,7 +259,8 @@ namespace SampleFramework
|
||||
options.FramesPerSecond = VSync ? 0 : Framerate;
|
||||
options.WindowState = FullScreen ? WindowState.Fullscreen : WindowState.Normal;
|
||||
options.VSync = VSync;
|
||||
options.API = GetGraphicsAPI();
|
||||
//options.API = new GraphicsAPI( ContextAPI.OpenGLES, ContextProfile.Core, ContextFlags.Default, new APIVersion(2, 0));
|
||||
options.API = GraphicsAPI.None;
|
||||
options.WindowBorder = WindowBorder.Resizable;
|
||||
options.Title = Text;
|
||||
|
||||
@ -247,11 +283,13 @@ namespace SampleFramework
|
||||
{
|
||||
Window_.UpdatesPerSecond = 0;
|
||||
Window_.FramesPerSecond = 0;
|
||||
Context.SwapInterval(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
Window_.UpdatesPerSecond = value;
|
||||
Window_.FramesPerSecond = value;
|
||||
Context.SwapInterval(0);
|
||||
}
|
||||
}
|
||||
|
||||
@ -352,52 +390,18 @@ namespace SampleFramework
|
||||
|
||||
public void Window_Load()
|
||||
{
|
||||
switch (GraphicsDeviceType_)
|
||||
{
|
||||
case GraphicsDeviceType.OpenGL:
|
||||
GraphicsDevice = new OpenGLDevice(Window_);
|
||||
break;
|
||||
case GraphicsDeviceType.Vulkan:
|
||||
GraphicsDevice = new VulkanDevice(Window_);
|
||||
break;
|
||||
case GraphicsDeviceType.DirectX11:
|
||||
GraphicsDevice = new DirectX11Device(Window_);
|
||||
break;
|
||||
case GraphicsDeviceType.DirectX12:
|
||||
GraphicsDevice = new DirectX12Device(Window_);
|
||||
break;
|
||||
}
|
||||
Context = new AngleContext(GraphicsDeviceType_, Window_);
|
||||
Context.MakeCurrent();
|
||||
|
||||
Gl = GL.GetApi(Context);
|
||||
//Gl = Window_.CreateOpenGLES();
|
||||
Gl.Enable(GLEnum.Blend);
|
||||
BlendHelper.SetBlend(BlendType.Normal);
|
||||
CTexture.Init();
|
||||
|
||||
GraphicsDevice.SetClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
GraphicsDevice.SetViewPort(0, 0, (uint)Window_.Size.X, (uint)Window_.FramebufferSize.Y);
|
||||
GraphicsDevice.SetFrameBuffer((uint)Window_.FramebufferSize.X, (uint)Window_.FramebufferSize.Y);
|
||||
|
||||
Shader_ = GraphicsDevice.GenShader($@"Shaders{Path.AltDirectorySeparatorChar}Common");
|
||||
|
||||
Polygon_ = GraphicsDevice.GenPolygon(
|
||||
new float[]
|
||||
{
|
||||
1, 1 * VerticalFix, 0.0f,
|
||||
1, -1 * VerticalFix, 0.0f,
|
||||
-1, -1 * VerticalFix, 0.0f,
|
||||
-1, 1 * VerticalFix, 0.0f
|
||||
}
|
||||
,
|
||||
new uint[]
|
||||
{
|
||||
0u, 1u, 3u,
|
||||
1u, 2u, 3u
|
||||
}
|
||||
,
|
||||
new float[]
|
||||
{
|
||||
1.0f, 0.0f,
|
||||
1.0f, 1.0f,
|
||||
0.0f, 1.0f,
|
||||
0.0f, 0.0f,
|
||||
}
|
||||
);
|
||||
Gl.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
Gl.Viewport(0, 0, (uint)Window_.Size.X, (uint)Window_.Size.Y);
|
||||
Context.SwapInterval(VSync ? 1 : 0);
|
||||
|
||||
Initialize();
|
||||
LoadContent();
|
||||
@ -405,14 +409,12 @@ namespace SampleFramework
|
||||
|
||||
public void Window_Closing()
|
||||
{
|
||||
|
||||
CTexture.Terminate();
|
||||
|
||||
UnloadContent();
|
||||
OnExiting();
|
||||
|
||||
Polygon_.Dispose();
|
||||
Shader_.Dispose();
|
||||
|
||||
GraphicsDevice.Dispose();
|
||||
Context.Dispose();
|
||||
}
|
||||
|
||||
public void Window_Update(double deltaTime)
|
||||
@ -426,13 +428,19 @@ namespace SampleFramework
|
||||
public void Window_Render(double deltaTime)
|
||||
{
|
||||
Camera = Matrix4X4<float>.Identity;
|
||||
GraphicsDevice.ClearBuffer();
|
||||
|
||||
if (AsyncActions.Count > 0)
|
||||
{
|
||||
AsyncActions[0]?.Invoke();
|
||||
AsyncActions.Remove(AsyncActions[0]);
|
||||
}
|
||||
Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
|
||||
|
||||
Draw();
|
||||
|
||||
GraphicsDevice.SwapBuffer();
|
||||
|
||||
double fps = 1.0f / deltaTime;
|
||||
|
||||
Context.SwapBuffers();
|
||||
}
|
||||
|
||||
public void Window_Resize(Vector2D<int> size)
|
||||
@ -451,7 +459,7 @@ namespace SampleFramework
|
||||
}
|
||||
|
||||
WindowSize = size;
|
||||
GraphicsDevice.SetViewPort(0, 0, (uint)size.X, (uint)size.Y);
|
||||
Gl.Viewport(0, 0, (uint)size.X, (uint)size.Y);
|
||||
}
|
||||
|
||||
public void Window_Move(Vector2D<int> size)
|
||||
@ -461,7 +469,6 @@ namespace SampleFramework
|
||||
|
||||
public void Window_FramebufferResize(Vector2D<int> size)
|
||||
{
|
||||
GraphicsDevice.SetFrameBuffer((uint)size.X, (uint)size.Y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
149
FDK/src/01.Framework/Rendering/Angle/AngleContext.cs
Normal file
149
FDK/src/01.Framework/Rendering/Angle/AngleContext.cs
Normal file
@ -0,0 +1,149 @@
|
||||
using Silk.NET.Core.Contexts;
|
||||
using Silk.NET.GLFW;
|
||||
using OpenTK.Graphics.Egl; //OpenTKさん ありがとう!
|
||||
using Silk.NET.Windowing;
|
||||
using Silk.NET.OpenGLES;
|
||||
|
||||
namespace SampleFramework;
|
||||
|
||||
public class AngleContext : IGLContext
|
||||
{
|
||||
private nint Display;
|
||||
private nint Context;
|
||||
private nint Surface;
|
||||
|
||||
public AngleContext(AnglePlatformType anglePlatformType, IWindow window)
|
||||
{
|
||||
nint windowHandle;
|
||||
nint display;
|
||||
if (OperatingSystem.IsWindows())
|
||||
{
|
||||
windowHandle = window.Native.Win32.Value.Hwnd;
|
||||
display = window.Native.Win32.Value.HDC;
|
||||
}
|
||||
else if (OperatingSystem.IsLinux())
|
||||
{
|
||||
windowHandle = (nint)window.Native.X11.Value.Window;
|
||||
display = window.Native.X11.Value.Display;
|
||||
}
|
||||
else if (OperatingSystem.IsMacOS())
|
||||
{
|
||||
windowHandle = window.Native.Cocoa.Value;
|
||||
display = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Window not found");
|
||||
}
|
||||
|
||||
Source = window;
|
||||
|
||||
int platform = 0;
|
||||
switch(anglePlatformType)
|
||||
{
|
||||
case AnglePlatformType.OpenGL:
|
||||
platform = Egl.PLATFORM_ANGLE_TYPE_OPENGL_ANGLE;
|
||||
break;
|
||||
case AnglePlatformType.OpenGLES:
|
||||
platform = Egl.PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE;
|
||||
break;
|
||||
case AnglePlatformType.D3D9:
|
||||
platform = Egl.PLATFORM_ANGLE_TYPE_D3D9_ANGLE;
|
||||
break;
|
||||
case AnglePlatformType.D3D11:
|
||||
platform = Egl.PLATFORM_ANGLE_TYPE_D3D11_ANGLE;
|
||||
break;
|
||||
case AnglePlatformType.Vulkan:
|
||||
platform = Egl.PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
|
||||
break;
|
||||
case AnglePlatformType.Metal:
|
||||
platform = Egl.PLATFORM_ANGLE_TYPE_METAL_ANGLE;
|
||||
break;
|
||||
}
|
||||
int[] platformAttributes = new int[]
|
||||
{
|
||||
Egl.PLATFORM_ANGLE_TYPE_ANGLE, platform,
|
||||
Egl.NONE
|
||||
};
|
||||
|
||||
//getEGLNativeDisplay
|
||||
Display = Egl.GetPlatformDisplayEXT(Egl.PLATFORM_ANGLE_ANGLE, display, platformAttributes);
|
||||
|
||||
Egl.Initialize(Display, out int major, out int minor);
|
||||
Egl.BindAPI(RenderApi.ES);
|
||||
|
||||
IntPtr[] configs = new IntPtr[1];
|
||||
int[] configAttributes = new int[]
|
||||
{
|
||||
Egl.RENDERABLE_TYPE, Egl.OPENGL_ES2_BIT,
|
||||
Egl.BUFFER_SIZE, 0,
|
||||
Egl.NONE
|
||||
};
|
||||
unsafe
|
||||
{
|
||||
Egl.ChooseConfig(Display, configAttributes, configs, configs.Length, out int num_config);
|
||||
}
|
||||
|
||||
int[] contextAttributes = new int[]
|
||||
{
|
||||
//Egl.CONTEXT_CLIENT_VERSION, 2,
|
||||
Egl.CONTEXT_MAJOR_VERSION, 2,
|
||||
Egl.CONTEXT_MINOR_VERSION, 0,
|
||||
Egl.NONE
|
||||
};
|
||||
Context = Egl.CreateContext(Display, configs[0], 0, contextAttributes);
|
||||
|
||||
int[] surfaceAttributes = new int[]
|
||||
{
|
||||
Egl.NONE
|
||||
};
|
||||
|
||||
Surface = Egl.CreatePlatformWindowSurfaceEXT(Display, configs[0], windowHandle, null);
|
||||
|
||||
var error1 = Egl.GetError();
|
||||
}
|
||||
|
||||
public nint Handle { get; set; }
|
||||
|
||||
public IGLContextSource? Source { get; set; }
|
||||
|
||||
public bool IsCurrent { get; set; } = true;
|
||||
|
||||
public nint GetProcAddress(string proc, int? slot = null)
|
||||
{
|
||||
nint addr = Egl.GetProcAddress(proc);
|
||||
return addr;
|
||||
}
|
||||
|
||||
public bool TryGetProcAddress(string proc, out nint addr, int? slot = null)
|
||||
{
|
||||
addr = Egl.GetProcAddress(proc);
|
||||
return addr != 0;
|
||||
}
|
||||
|
||||
public void SwapInterval(int interval)
|
||||
{
|
||||
Egl.SwapInterval(Display, interval);
|
||||
}
|
||||
|
||||
public void SwapBuffers()
|
||||
{
|
||||
Egl.SwapBuffers(Display, Surface);
|
||||
}
|
||||
|
||||
public void MakeCurrent()
|
||||
{
|
||||
Egl.MakeCurrent(Display, Surface, Surface, Context);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Egl.DestroyContext(Display, Context);
|
||||
Egl.DestroySurface(Display, Surface);
|
||||
Egl.Terminate(Display);
|
||||
}
|
||||
}
|
420
FDK/src/01.Framework/Rendering/Angle/Egl.cs
Normal file
420
FDK/src/01.Framework/Rendering/Angle/Egl.cs
Normal file
@ -0,0 +1,420 @@
|
||||
//
|
||||
// The Open Toolkit Library License
|
||||
//
|
||||
// Copyright (c) 2006 - 2011 the Open Toolkit library.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights to
|
||||
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
// the Software, and to permit persons to whom the Software is furnished to do
|
||||
// so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// ReSharper disable InconsistentNaming
|
||||
// ReSharper disable UnusedMember.Global
|
||||
|
||||
#pragma warning disable 1591 // Missing XML comments
|
||||
|
||||
namespace OpenTK.Graphics.Egl
|
||||
{
|
||||
using EGLNativeDisplayType = IntPtr;
|
||||
using EGLNativeWindowType = IntPtr;
|
||||
using EGLNativePixmapType = IntPtr;
|
||||
using EGLConfig = IntPtr;
|
||||
using EGLContext = IntPtr;
|
||||
using EGLDisplay = IntPtr;
|
||||
using EGLSurface = IntPtr;
|
||||
using EGLClientBuffer = IntPtr;
|
||||
|
||||
public enum RenderApi
|
||||
{
|
||||
ES = Egl.OPENGL_ES_API,
|
||||
GL = Egl.OPENGL_API,
|
||||
VG = Egl.OPENVG_API
|
||||
}
|
||||
|
||||
[Flags]
|
||||
public enum RenderableFlags
|
||||
{
|
||||
ES = Egl.OPENGL_ES_BIT,
|
||||
ES2 = Egl.OPENGL_ES2_BIT,
|
||||
ES3 = Egl.OPENGL_ES3_BIT,
|
||||
GL = Egl.OPENGL_BIT,
|
||||
VG = Egl.OPENVG_BIT,
|
||||
}
|
||||
|
||||
public enum ErrorCode
|
||||
{
|
||||
SUCCESS = 12288,
|
||||
NOT_INITIALIZED = 12289,
|
||||
BAD_ACCESS = 12290,
|
||||
BAD_ALLOC = 12291,
|
||||
BAD_ATTRIBUTE = 12292,
|
||||
BAD_CONFIG = 12293,
|
||||
BAD_CONTEXT = 12294,
|
||||
BAD_CURRENT_SURFACE = 12295,
|
||||
BAD_DISPLAY = 12296,
|
||||
BAD_MATCH = 12297,
|
||||
BAD_NATIVE_PIXMAP = 12298,
|
||||
BAD_NATIVE_WINDOW = 12299,
|
||||
BAD_PARAMETER = 12300,
|
||||
BAD_SURFACE = 12301,
|
||||
CONTEXT_LOST = 12302,
|
||||
}
|
||||
|
||||
public enum SurfaceType
|
||||
{
|
||||
PBUFFER_BIT = 0x0001,
|
||||
PIXMAP_BIT = 0x0002,
|
||||
WINDOW_BIT = 0x0004,
|
||||
VG_COLORSPACE_LINEAR_BIT = 0x0020,
|
||||
VG_ALPHA_FORMAT_PRE_BIT = 0x0040,
|
||||
MULTISAMPLE_RESOLVE_BOX_BIT = 0x0200,
|
||||
SWAP_BEHAVIOR_PRESERVED_BIT = 0x0400,
|
||||
}
|
||||
|
||||
public class EglException : Exception
|
||||
{
|
||||
public EglException() : base()
|
||||
{ }
|
||||
|
||||
public EglException(string message) : base(message)
|
||||
{ }
|
||||
}
|
||||
|
||||
public static partial class Egl
|
||||
{
|
||||
public const int CONTEXT_MAJOR_VERSION = 0x3098;
|
||||
public const int CONTEXT_MINOR_VERSION = 0x30FB;
|
||||
|
||||
public const int VERSION_1_0 = 1;
|
||||
public const int VERSION_1_1 = 1;
|
||||
public const int VERSION_1_2 = 1;
|
||||
public const int VERSION_1_3 = 1;
|
||||
public const int VERSION_1_4 = 1;
|
||||
public const int FALSE = 0;
|
||||
public const int TRUE = 1;
|
||||
public const int DONT_CARE = -1;
|
||||
public const int CONTEXT_LOST = 12302;
|
||||
public const int BUFFER_SIZE = 12320;
|
||||
public const int ALPHA_SIZE = 12321;
|
||||
public const int BLUE_SIZE = 12322;
|
||||
public const int GREEN_SIZE = 12323;
|
||||
public const int RED_SIZE = 12324;
|
||||
public const int DEPTH_SIZE = 12325;
|
||||
public const int STENCIL_SIZE = 12326;
|
||||
public const int CONFIG_CAVEAT = 12327;
|
||||
public const int CONFIG_ID = 12328;
|
||||
public const int LEVEL = 12329;
|
||||
public const int MAX_PBUFFER_HEIGHT = 12330;
|
||||
public const int MAX_PBUFFER_PIXELS = 12331;
|
||||
public const int MAX_PBUFFER_WIDTH = 12332;
|
||||
public const int NATIVE_RENDERABLE = 12333;
|
||||
public const int NATIVE_VISUAL_ID = 12334;
|
||||
public const int NATIVE_VISUAL_TYPE = 12335;
|
||||
public const int PRESERVED_RESOURCES = 12336;
|
||||
public const int SAMPLES = 12337;
|
||||
public const int SAMPLE_BUFFERS = 12338;
|
||||
public const int SURFACE_TYPE = 12339;
|
||||
public const int TRANSPARENT_TYPE = 12340;
|
||||
public const int TRANSPARENT_BLUE_VALUE = 12341;
|
||||
public const int TRANSPARENT_GREEN_VALUE = 12342;
|
||||
public const int TRANSPARENT_RED_VALUE = 12343;
|
||||
public const int NONE = 12344;
|
||||
public const int BIND_TO_TEXTURE_RGB = 12345;
|
||||
public const int BIND_TO_TEXTURE_RGBA = 12346;
|
||||
public const int MIN_SWAP_INTERVAL = 12347;
|
||||
public const int MAX_SWAP_INTERVAL = 12348;
|
||||
public const int LUMINANCE_SIZE = 12349;
|
||||
public const int ALPHA_MASK_SIZE = 12350;
|
||||
public const int COLOR_BUFFER_TYPE = 12351;
|
||||
public const int RENDERABLE_TYPE = 12352;
|
||||
public const int MATCH_NATIVE_PIXMAP = 12353;
|
||||
public const int CONFORMANT = 12354;
|
||||
public const int SLOW_CONFIG = 12368;
|
||||
public const int NON_CONFORMANT_CONFIG = 12369;
|
||||
public const int TRANSPARENT_RGB = 12370;
|
||||
public const int RGB_BUFFER = 12430;
|
||||
public const int LUMINANCE_BUFFER = 12431;
|
||||
public const int NO_TEXTURE = 12380;
|
||||
public const int TEXTURE_RGB = 12381;
|
||||
public const int TEXTURE_RGBA = 12382;
|
||||
public const int TEXTURE_2D = 12383;
|
||||
public const int PBUFFER_BIT = 1;
|
||||
public const int PIXMAP_BIT = 2;
|
||||
public const int WINDOW_BIT = 4;
|
||||
public const int VG_COLORSPACE_LINEAR_BIT = 32;
|
||||
public const int VG_ALPHA_FORMAT_PRE_BIT = 64;
|
||||
public const int MULTISAMPLE_RESOLVE_BOX_BIT = 512;
|
||||
public const int SWAP_BEHAVIOR_PRESERVED_BIT = 1024;
|
||||
public const int OPENGL_ES_BIT = 1;
|
||||
public const int OPENVG_BIT = 2;
|
||||
public const int OPENGL_ES2_BIT = 4;
|
||||
public const int OPENGL_BIT = 8;
|
||||
public const int OPENGL_ES3_BIT = 64;
|
||||
public const int VENDOR = 12371;
|
||||
public const int VERSION = 12372;
|
||||
public const int EXTENSIONS = 12373;
|
||||
public const int CLIENT_APIS = 12429;
|
||||
public const int HEIGHT = 12374;
|
||||
public const int WIDTH = 12375;
|
||||
public const int LARGEST_PBUFFER = 12376;
|
||||
public const int TEXTURE_FORMAT = 12416;
|
||||
public const int TEXTURE_TARGET = 12417;
|
||||
public const int MIPMAP_TEXTURE = 12418;
|
||||
public const int MIPMAP_LEVEL = 12419;
|
||||
public const int RENDER_BUFFER = 12422;
|
||||
public const int VG_COLORSPACE = 12423;
|
||||
public const int VG_ALPHA_FORMAT = 12424;
|
||||
public const int HORIZONTAL_RESOLUTION = 12432;
|
||||
public const int VERTICAL_RESOLUTION = 12433;
|
||||
public const int PIXEL_ASPECT_RATIO = 12434;
|
||||
public const int SWAP_BEHAVIOR = 12435;
|
||||
public const int MULTISAMPLE_RESOLVE = 12441;
|
||||
public const int BACK_BUFFER = 12420;
|
||||
public const int SINGLE_BUFFER = 12421;
|
||||
public const int VG_COLORSPACE_sRGB = 12425;
|
||||
public const int VG_COLORSPACE_LINEAR = 12426;
|
||||
public const int VG_ALPHA_FORMAT_NONPRE = 12427;
|
||||
public const int VG_ALPHA_FORMAT_PRE = 12428;
|
||||
public const int DISPLAY_SCALING = 10000;
|
||||
public const int UNKNOWN = -1;
|
||||
public const int BUFFER_PRESERVED = 12436;
|
||||
public const int BUFFER_DESTROYED = 12437;
|
||||
public const int OPENVG_IMAGE = 12438;
|
||||
public const int CONTEXT_CLIENT_TYPE = 12439;
|
||||
public const int CONTEXT_CLIENT_VERSION = 12440;
|
||||
public const int MULTISAMPLE_RESOLVE_DEFAULT = 12442;
|
||||
public const int MULTISAMPLE_RESOLVE_BOX = 12443;
|
||||
public const int OPENGL_ES_API = 12448;
|
||||
public const int OPENVG_API = 12449;
|
||||
public const int OPENGL_API = 12450;
|
||||
public const int DRAW = 12377;
|
||||
public const int READ = 12378;
|
||||
public const int CORE_NATIVE_ENGINE = 12379;
|
||||
public const int COLORSPACE = VG_COLORSPACE;
|
||||
public const int ALPHA_FORMAT = VG_ALPHA_FORMAT;
|
||||
public const int COLORSPACE_sRGB = VG_COLORSPACE_sRGB;
|
||||
public const int COLORSPACE_LINEAR = VG_COLORSPACE_LINEAR;
|
||||
public const int ALPHA_FORMAT_NONPRE = VG_ALPHA_FORMAT_NONPRE;
|
||||
public const int ALPHA_FORMAT_PRE = VG_ALPHA_FORMAT_PRE;
|
||||
|
||||
// EGL_ANGLE_d3d_share_handle_client_buffer
|
||||
public const int D3D_TEXTURE_2D_SHARE_HANDLE_ANGLE = 0x3200;
|
||||
// EGL_ANGLE_window_fixed_size
|
||||
public const int FIXED_SIZE_ANGLE = 0x3201;
|
||||
// EGL_ANGLE_query_surface_pointer
|
||||
[DllImport("libEGL.dll", EntryPoint = "eglQuerySurfacePointerANGLE")]
|
||||
public static extern bool QuerySurfacePointerANGLE(EGLDisplay display, EGLSurface surface, int attribute, out IntPtr value);
|
||||
|
||||
[DllImport("libEGL.dll", EntryPoint = "eglGetPlatformDisplayEXT")]
|
||||
public static extern EGLDisplay GetPlatformDisplay(int platform, EGLNativeDisplayType displayId, int[] attribList);
|
||||
|
||||
// EGL_ANGLE_software_display
|
||||
public static readonly EGLNativeDisplayType SOFTWARE_DISPLAY_ANGLE = new EGLNativeDisplayType(-1);
|
||||
// EGL_ANGLE_direct3d_display
|
||||
public static readonly EGLNativeDisplayType D3D11_ELSE_D3D9_DISPLAY_ANGLE = new EGLNativeDisplayType(-2);
|
||||
public static readonly EGLNativeDisplayType D3D11_ONLY_DISPLAY_ANGLE = new EGLNativeDisplayType(-3);
|
||||
// EGL_ANGLE_device_d3d
|
||||
public const int D3D9_DEVICE_ANGLE = 0x33A0;
|
||||
public const int D3D11_DEVICE_ANGLE = 0x33A1;
|
||||
// EGL_ANGLE_platform_angle
|
||||
public const int PLATFORM_ANGLE_ANGLE = 0x3202;
|
||||
public const int PLATFORM_ANGLE_TYPE_ANGLE = 0x3203;
|
||||
public const int PLATFORM_ANGLE_MAX_VERSION_MAJOR_ANGLE = 0x3204;
|
||||
public const int PLATFORM_ANGLE_MAX_VERSION_MINOR_ANGLE = 0x3205;
|
||||
public const int PLATFORM_ANGLE_TYPE_DEFAULT_ANGLE = 0x3206;
|
||||
// EGL_ANGLE_platform_angle_d3d
|
||||
public const int PLATFORM_ANGLE_TYPE_D3D9_ANGLE = 0x3207;
|
||||
public const int PLATFORM_ANGLE_TYPE_D3D11_ANGLE = 0x3208;
|
||||
public const int PLATFORM_ANGLE_TYPE_VULKAN_ANGLE = 0x3450;
|
||||
public const int PLATFORM_ANGLE_TYPE_METAL_ANGLE = 0x3489;
|
||||
public const int PLATFORM_ANGLE_DEVICE_TYPE_ANGLE = 0x3209;
|
||||
public const int PLATFORM_ANGLE_DEVICE_TYPE_HARDWARE_ANGLE = 0x320A;
|
||||
public const int PLATFORM_ANGLE_DEVICE_TYPE_WARP_ANGLE = 0x320B;
|
||||
public const int PLATFORM_ANGLE_DEVICE_TYPE_REFERENCE_ANGLE = 0x320C;
|
||||
public const int PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE = 0x320F;
|
||||
// EGL_ANGLE_platform_angle_opengl
|
||||
public const int PLATFORM_ANGLE_TYPE_OPENGL_ANGLE = 0x320D;
|
||||
public const int PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE = 0x320E;
|
||||
// See EGL_ANGLE_surface_d3d_texture_2d_share_handle
|
||||
public const int EGL_D3D_TEXTURE_2D_SHARE_HANDLE_ANGLE = 0x3200;
|
||||
|
||||
[DllImportAttribute("libEGL.dll", EntryPoint = "eglGetError")]
|
||||
public static extern ErrorCode GetError();
|
||||
|
||||
[DllImportAttribute("libEGL.dll", EntryPoint = "eglGetDisplay")]
|
||||
public static extern EGLDisplay GetDisplay(EGLNativeDisplayType display_id);
|
||||
|
||||
[DllImportAttribute("libEGL.dll", EntryPoint = "eglInitialize")]
|
||||
//[return: MarshalAs(UnmanagedType.I1)]
|
||||
public static extern bool Initialize(EGLDisplay dpy, out int major, out int minor);
|
||||
|
||||
[DllImportAttribute("libEGL.dll", EntryPoint = "eglTerminate")]
|
||||
//[return: MarshalAs(UnmanagedType.I1)]
|
||||
public static extern bool Terminate(EGLDisplay dpy);
|
||||
|
||||
[DllImportAttribute("libEGL.dll", EntryPoint = "eglQueryString")]
|
||||
public static extern IntPtr QueryString(EGLDisplay dpy, int name);
|
||||
|
||||
[DllImportAttribute("libEGL.dll", EntryPoint = "eglGetConfigs")]
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
public static extern bool GetConfigs(EGLDisplay dpy, EGLConfig[] configs, int config_size, out int num_config);
|
||||
|
||||
[DllImportAttribute("libEGL.dll", EntryPoint = "eglChooseConfig")]
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
public static extern bool ChooseConfig(EGLDisplay dpy, int[] attrib_list, [In, Out] EGLConfig[] configs, int config_size, out int num_config);
|
||||
|
||||
[DllImportAttribute("libEGL.dll", EntryPoint = "eglChooseConfig")]
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
public unsafe static extern bool ChooseConfig(EGLDisplay dpy, int[] attrib_list, EGLConfig* configs, int config_size, out int num_config);
|
||||
|
||||
[DllImportAttribute("libEGL.dll", EntryPoint = "eglGetConfigAttrib")]
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
public static extern bool GetConfigAttrib(EGLDisplay dpy, EGLConfig config, int attribute, out int value);
|
||||
|
||||
[DllImportAttribute("libEGL.dll", EntryPoint = "eglCreateWindowSurface")]
|
||||
public static extern EGLSurface CreateWindowSurface(EGLDisplay dpy, EGLConfig config, IntPtr win, IntPtr attrib_list);
|
||||
|
||||
[DllImportAttribute("libEGL.dll", EntryPoint = "eglCreatePbufferSurface")]
|
||||
public static extern EGLSurface CreatePbufferSurface(EGLDisplay dpy, EGLConfig config, int[] attrib_list);
|
||||
|
||||
[DllImportAttribute("libEGL.dll", EntryPoint = "eglCreatePixmapSurface")]
|
||||
public static extern EGLSurface CreatePixmapSurface(EGLDisplay dpy, EGLConfig config, EGLNativePixmapType pixmap, int[] attrib_list);
|
||||
|
||||
[DllImportAttribute("libEGL.dll", EntryPoint = "eglDestroySurface")]
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
public static extern bool DestroySurface(EGLDisplay dpy, EGLSurface surface);
|
||||
|
||||
[DllImportAttribute("libEGL.dll", EntryPoint = "eglQuerySurface")]
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
public static extern bool QuerySurface(EGLDisplay dpy, EGLSurface surface, int attribute, out int value);
|
||||
|
||||
[DllImportAttribute("libEGL.dll", EntryPoint = "eglBindAPI")]
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
public static extern bool BindAPI(RenderApi api);
|
||||
|
||||
[DllImportAttribute("libEGL.dll", EntryPoint = "eglQueryAPI")]
|
||||
public static extern int QueryAPI();
|
||||
|
||||
[DllImportAttribute("libEGL.dll", EntryPoint = "eglWaitClient")]
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
public static extern bool WaitClient();
|
||||
|
||||
[DllImportAttribute("libEGL.dll", EntryPoint = "eglReleaseThread")]
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
public static extern bool ReleaseThread();
|
||||
|
||||
[DllImportAttribute("libEGL.dll", EntryPoint = "eglCreatePbufferFromClientBuffer")]
|
||||
public static extern EGLSurface CreatePbufferFromClientBuffer(EGLDisplay dpy, int buftype, EGLClientBuffer buffer, EGLConfig config, int[] attrib_list);
|
||||
|
||||
[DllImportAttribute("libEGL.dll", EntryPoint = "eglSurfaceAttrib")]
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
public static extern bool SurfaceAttrib(EGLDisplay dpy, EGLSurface surface, int attribute, int value);
|
||||
|
||||
[DllImportAttribute("libEGL.dll", EntryPoint = "eglBindTexImage")]
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
public static extern bool BindTexImage(EGLDisplay dpy, EGLSurface surface, int buffer);
|
||||
|
||||
[DllImportAttribute("libEGL.dll", EntryPoint = "eglReleaseTexImage")]
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
public static extern bool ReleaseTexImage(EGLDisplay dpy, EGLSurface surface, int buffer);
|
||||
|
||||
[DllImportAttribute("libEGL.dll", EntryPoint = "eglSwapInterval")]
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
public static extern bool SwapInterval(EGLDisplay dpy, int interval);
|
||||
|
||||
[DllImportAttribute("libEGL.dll", EntryPoint = "eglCreateContext")]
|
||||
private static extern IntPtr eglCreateContext(EGLDisplay dpy, EGLConfig config, EGLContext share_context, int[] attrib_list);
|
||||
|
||||
public static EGLContext CreateContext(EGLDisplay dpy, EGLConfig config, EGLContext share_context, int[] attrib_list)
|
||||
{
|
||||
IntPtr ptr = eglCreateContext(dpy, config, share_context, attrib_list);
|
||||
if (ptr == IntPtr.Zero)
|
||||
{
|
||||
throw new EglException(string.Format("Failed to create EGL context, error: {0}.", Egl.GetError()));
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
|
||||
[DllImportAttribute("libEGL.dll", EntryPoint = "eglDestroyContext")]
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
public static extern bool DestroyContext(EGLDisplay dpy, EGLContext ctx);
|
||||
|
||||
[DllImportAttribute("libEGL.dll", EntryPoint = "eglMakeCurrent")]
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
public static extern bool MakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx);
|
||||
|
||||
[DllImportAttribute("libEGL.dll", EntryPoint = "eglGetCurrentContext")]
|
||||
public static extern EGLContext GetCurrentContext();
|
||||
|
||||
[DllImportAttribute("libEGL.dll", EntryPoint = "eglGetCurrentSurface")]
|
||||
public static extern EGLSurface GetCurrentSurface(int readdraw);
|
||||
|
||||
[DllImportAttribute("libEGL.dll", EntryPoint = "eglGetCurrentDisplay")]
|
||||
public static extern EGLDisplay GetCurrentDisplay();
|
||||
|
||||
[DllImportAttribute("libEGL.dll", EntryPoint = "eglQueryContext")]
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
public static extern bool QueryContext(EGLDisplay dpy, EGLContext ctx, int attribute, out int value);
|
||||
|
||||
[DllImportAttribute("libEGL.dll", EntryPoint = "eglWaitGL")]
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
public static extern bool WaitGL();
|
||||
|
||||
[DllImportAttribute("libEGL.dll", EntryPoint = "eglWaitNative")]
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
public static extern bool WaitNative(int engine);
|
||||
|
||||
[DllImportAttribute("libEGL.dll", EntryPoint = "eglSwapBuffers")]
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
public static extern bool SwapBuffers(EGLDisplay dpy, EGLSurface surface);
|
||||
|
||||
[DllImportAttribute("libEGL.dll", EntryPoint = "eglCopyBuffers")]
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
public static extern bool CopyBuffers(EGLDisplay dpy, EGLSurface surface, EGLNativePixmapType target);
|
||||
|
||||
[DllImportAttribute("libEGL.dll", EntryPoint = "eglGetProcAddress")]
|
||||
public static extern IntPtr GetProcAddress(string funcname);
|
||||
|
||||
[DllImportAttribute("libEGL.dll", EntryPoint = "eglGetProcAddress")]
|
||||
public static extern IntPtr GetProcAddress(IntPtr funcname);
|
||||
|
||||
// EGL_EXT_platform_base
|
||||
[DllImport("libEGL.dll", EntryPoint = "eglGetPlatformDisplayEXT")]
|
||||
public static extern EGLDisplay GetPlatformDisplayEXT(int platform, EGLNativeDisplayType native_display, int[] attrib_list);
|
||||
|
||||
[DllImport("libEGL.dll", EntryPoint = "eglCreatePlatformWindowSurfaceEXT")]
|
||||
public static extern EGLSurface CreatePlatformWindowSurfaceEXT(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType native_window, int[] attrib_list);
|
||||
|
||||
[DllImport("libEGL.dll", EntryPoint = "eglCreatePlatformPixmapSurfaceEXT")]
|
||||
public static extern EGLSurface CreatePlatformPixmapSurfaceEXT(EGLDisplay dpy, EGLConfig config, EGLNativePixmapType native_pixmap, int[] attrib_list);
|
||||
|
||||
// Returns true if Egl drivers exist on the system.
|
||||
public static bool IsSupported
|
||||
{
|
||||
get
|
||||
{
|
||||
try { GetCurrentContext(); }
|
||||
catch (Exception) { return false; }
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
42
FDK/src/01.Framework/Rendering/BlendHelper.cs
Normal file
42
FDK/src/01.Framework/Rendering/BlendHelper.cs
Normal file
@ -0,0 +1,42 @@
|
||||
using Silk.NET.OpenGLES;
|
||||
|
||||
namespace SampleFramework;
|
||||
|
||||
public enum BlendType
|
||||
{
|
||||
Normal,
|
||||
Add,
|
||||
Screen,
|
||||
Multi,
|
||||
Sub
|
||||
}
|
||||
|
||||
public static class BlendHelper
|
||||
{
|
||||
public static void SetBlend(BlendType blendType)
|
||||
{
|
||||
switch(blendType)
|
||||
{
|
||||
case BlendType.Normal:
|
||||
Game.Gl.BlendEquation(BlendEquationModeEXT.FuncAdd);
|
||||
Game.Gl.BlendFunc(GLEnum.SrcAlpha, GLEnum.OneMinusSrcAlpha);
|
||||
break;
|
||||
case BlendType.Add:
|
||||
Game.Gl.BlendEquation(BlendEquationModeEXT.FuncAdd);
|
||||
Game.Gl.BlendFunc(GLEnum.SrcAlpha, GLEnum.One);
|
||||
break;
|
||||
case BlendType.Screen:
|
||||
Game.Gl.BlendEquation(BlendEquationModeEXT.FuncAdd);
|
||||
Game.Gl.BlendFunc(GLEnum.OneMinusDstColor, GLEnum.One);
|
||||
break;
|
||||
case BlendType.Multi:
|
||||
Game.Gl.BlendEquation(BlendEquationModeEXT.FuncAdd);
|
||||
Game.Gl.BlendFunc(GLEnum.SrcAlpha, GLEnum.SrcColor);
|
||||
break;
|
||||
case BlendType.Sub:
|
||||
Game.Gl.BlendEquation(BlendEquationModeEXT.FuncReverseSubtract);
|
||||
Game.Gl.BlendFunc(GLEnum.SrcAlpha, GLEnum.One);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,356 +0,0 @@
|
||||
using Silk.NET.Windowing;
|
||||
using Silk.NET.Maths;
|
||||
using Silk.NET.Core.Native;
|
||||
using Silk.NET.Direct3D.Compilers;
|
||||
using Silk.NET.Direct3D11;
|
||||
using Silk.NET.DXGI;
|
||||
using SkiaSharp;
|
||||
|
||||
namespace SampleFramework
|
||||
{
|
||||
unsafe class DirectX11Device : IGraphicsDevice
|
||||
{
|
||||
const bool FORCE_DXVK = false;
|
||||
|
||||
internal static D3D11 D3d11;
|
||||
|
||||
internal static DXGI DxGi;
|
||||
|
||||
internal static D3DCompiler D3dCompiler;
|
||||
|
||||
internal static ComPtr<IDXGIFactory> Factory;
|
||||
|
||||
internal static ComPtr<ID3D11Device> Device;
|
||||
|
||||
internal static ComPtr<ID3D11DeviceContext> ImmediateContext;
|
||||
|
||||
internal static ComPtr<IDXGISwapChain> SwapChain;
|
||||
|
||||
internal static ComPtr<ID3D11RenderTargetView> RenderTargetView;
|
||||
|
||||
internal static ComPtr<ID3D11Texture2D> DepthStencilTexture;
|
||||
|
||||
internal static ComPtr<ID3D11DepthStencilView> DepthStencilView;
|
||||
|
||||
internal static ComPtr<ID3D11BlendState>[] BlendStates = new ComPtr<ID3D11BlendState>[5];
|
||||
|
||||
internal ComPtr<ID3D11SamplerState> SamplerState;
|
||||
|
||||
internal static float[] CurrnetClearColor;
|
||||
|
||||
internal static IWindow Window_;
|
||||
|
||||
private Viewport Viewport_;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private void CreateRenderTargetView()
|
||||
{
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
SwapChain.GetBuffer(0, out ComPtr<ID3D11Texture2D> backBuffer)
|
||||
);
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
Device.CreateRenderTargetView(backBuffer, null, ref RenderTargetView)
|
||||
);
|
||||
backBuffer.Dispose();
|
||||
}
|
||||
|
||||
private void CreateDepthStencilView(uint width, uint height)
|
||||
{
|
||||
Texture2DDesc texture2DDesc = new Texture2DDesc();
|
||||
texture2DDesc.Width = width;
|
||||
texture2DDesc.Height = height;
|
||||
texture2DDesc.MipLevels = 1;
|
||||
texture2DDesc.ArraySize = 1;
|
||||
texture2DDesc.Format = Format.FormatD24UnormS8Uint;
|
||||
texture2DDesc.SampleDesc.Count = 1;
|
||||
texture2DDesc.SampleDesc.Quality = 0;
|
||||
texture2DDesc.Usage = Usage.Default;
|
||||
texture2DDesc.BindFlags = (uint)BindFlag.DepthStencil;
|
||||
texture2DDesc.CPUAccessFlags = 0;
|
||||
texture2DDesc.MiscFlags = 0;
|
||||
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
Device.CreateTexture2D(texture2DDesc, null, ref DepthStencilTexture)
|
||||
);
|
||||
|
||||
|
||||
|
||||
DepthStencilViewDesc depthStencilDesc = new DepthStencilViewDesc();
|
||||
depthStencilDesc.Format = texture2DDesc.Format;
|
||||
depthStencilDesc.ViewDimension = DsvDimension.Texture2D;
|
||||
depthStencilDesc.Texture2D = new Tex2DDsv(0);
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
Device.CreateDepthStencilView(DepthStencilTexture, depthStencilDesc, ref DepthStencilView)
|
||||
);
|
||||
}
|
||||
|
||||
public DirectX11Device(IWindow window)
|
||||
{
|
||||
Window_ = window;
|
||||
D3d11 = D3D11.GetApi(window, FORCE_DXVK);
|
||||
DxGi = DXGI.GetApi(window, FORCE_DXVK);
|
||||
D3dCompiler = D3DCompiler.GetApi();
|
||||
|
||||
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
DxGi.CreateDXGIFactory(out Factory)
|
||||
);
|
||||
|
||||
|
||||
D3DFeatureLevel[] featureLevels = new D3DFeatureLevel[]
|
||||
{
|
||||
D3DFeatureLevel.Level111,
|
||||
D3DFeatureLevel.Level110,
|
||||
};
|
||||
|
||||
uint debugFlag = 0;
|
||||
|
||||
#if DEBUG
|
||||
debugFlag = (uint)CreateDeviceFlag.Debug;
|
||||
#endif
|
||||
|
||||
fixed (D3DFeatureLevel* levels = featureLevels)
|
||||
{
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
D3d11.CreateDevice(
|
||||
default(ComPtr<IDXGIAdapter>),
|
||||
D3DDriverType.Hardware,
|
||||
0,
|
||||
debugFlag,
|
||||
levels,
|
||||
(uint)featureLevels.Length,
|
||||
D3D11.SdkVersion,
|
||||
ref Device,
|
||||
default,
|
||||
ref ImmediateContext)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
SwapChainDesc swapChainDesc = new SwapChainDesc();
|
||||
swapChainDesc.BufferDesc.Width = (uint)window.FramebufferSize.X;
|
||||
swapChainDesc.BufferDesc.Height = (uint)window.FramebufferSize.Y;
|
||||
swapChainDesc.BufferDesc.Format = Format.FormatR8G8B8A8Unorm;
|
||||
swapChainDesc.BufferDesc.ScanlineOrdering = ModeScanlineOrder.Unspecified;
|
||||
swapChainDesc.BufferDesc.Scaling = ModeScaling.Unspecified;
|
||||
swapChainDesc.BufferDesc.RefreshRate.Numerator = 0;
|
||||
swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
|
||||
swapChainDesc.SampleDesc.Count = 1;
|
||||
swapChainDesc.SampleDesc.Quality = 0;
|
||||
swapChainDesc.BufferUsage = DXGI.UsageRenderTargetOutput;
|
||||
swapChainDesc.BufferCount = 2;
|
||||
swapChainDesc.OutputWindow = window.Native.DXHandle.Value;
|
||||
swapChainDesc.Windowed = true;
|
||||
swapChainDesc.SwapEffect = SwapEffect.FlipDiscard;
|
||||
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
Factory.CreateSwapChain(
|
||||
Device,
|
||||
&swapChainDesc,
|
||||
ref SwapChain
|
||||
)
|
||||
);
|
||||
|
||||
CreateRenderTargetView();
|
||||
CreateDepthStencilView((uint)window.FramebufferSize.X, (uint)window.FramebufferSize.X);
|
||||
|
||||
for(BlendType i = 0; i < BlendType.Screen + 1; i++)
|
||||
{
|
||||
BlendDesc blendDesc = new(false, false);
|
||||
blendDesc.RenderTarget[0].BlendEnable = true;
|
||||
switch (i)
|
||||
{
|
||||
case BlendType.Normal:
|
||||
blendDesc.RenderTarget[0].SrcBlend = Blend.SrcAlpha;
|
||||
blendDesc.RenderTarget[0].DestBlend = Blend.InvSrcAlpha;
|
||||
break;
|
||||
case BlendType.Add:
|
||||
blendDesc.RenderTarget[0].SrcBlend = Blend.One;
|
||||
blendDesc.RenderTarget[0].DestBlend = Blend.One;
|
||||
break;
|
||||
case BlendType.Multi:
|
||||
blendDesc.RenderTarget[0].SrcBlend = Blend.SrcAlpha;
|
||||
blendDesc.RenderTarget[0].DestBlend = Blend.One;
|
||||
break;
|
||||
case BlendType.Sub:
|
||||
blendDesc.RenderTarget[0].SrcBlend = Blend.Zero;
|
||||
blendDesc.RenderTarget[0].DestBlend = Blend.InvSrcColor;
|
||||
break;
|
||||
case BlendType.Screen:
|
||||
blendDesc.RenderTarget[0].SrcBlend = Blend.InvDestColor;
|
||||
blendDesc.RenderTarget[0].DestBlend = Blend.One;
|
||||
break;
|
||||
}
|
||||
|
||||
blendDesc.RenderTarget[0].BlendOp = BlendOp.Add;
|
||||
blendDesc.RenderTarget[0].SrcBlendAlpha = Blend.One;
|
||||
blendDesc.RenderTarget[0].DestBlendAlpha = Blend.Zero;
|
||||
blendDesc.RenderTarget[0].BlendOpAlpha = BlendOp.Add;
|
||||
blendDesc.RenderTarget[0].RenderTargetWriteMask = (byte)ColorWriteEnable.All;
|
||||
|
||||
Device.CreateBlendState(blendDesc, ref BlendStates[(int)i]);
|
||||
}
|
||||
|
||||
SamplerDesc samplerDesc = new()
|
||||
{
|
||||
Filter = Filter.MinMagMipLinear,
|
||||
AddressU = TextureAddressMode.Wrap,
|
||||
AddressV = TextureAddressMode.Wrap,
|
||||
AddressW = TextureAddressMode.Wrap
|
||||
};
|
||||
|
||||
SilkMarshal.ThrowHResult(
|
||||
DirectX11Device.Device.CreateSamplerState(samplerDesc, ref SamplerState)
|
||||
);
|
||||
|
||||
|
||||
SetViewPort(0, 0, (uint)window.Size.X, (uint)window.Size.Y);
|
||||
}
|
||||
|
||||
public void SetClearColor(float r, float g, float b, float a)
|
||||
{
|
||||
CurrnetClearColor = new float[] { r, g, b, a };
|
||||
}
|
||||
|
||||
public void SetViewPort(int x, int y, uint width, uint height)
|
||||
{
|
||||
if (width <= 0 || height <= 0) return;
|
||||
|
||||
Viewport_ = new Viewport(0, 0, width, height, 0.0f, 1.0f);
|
||||
}
|
||||
|
||||
public void SetFrameBuffer(uint width, uint height)
|
||||
{
|
||||
if (width <= 0 || height <= 0) return;
|
||||
|
||||
RenderTargetView.Dispose();
|
||||
|
||||
DepthStencilTexture.Dispose();
|
||||
DepthStencilView.Dispose();
|
||||
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
SwapChain.ResizeBuffers(0, width, height, Format.FormatR8G8B8A8Unorm, 0)
|
||||
);
|
||||
|
||||
CreateRenderTargetView();
|
||||
CreateDepthStencilView(width, height);
|
||||
}
|
||||
|
||||
public void ClearBuffer()
|
||||
{
|
||||
ImmediateContext.OMSetRenderTargets(1, ref RenderTargetView, DepthStencilView);
|
||||
ImmediateContext.ClearRenderTargetView(RenderTargetView, CurrnetClearColor);
|
||||
|
||||
ImmediateContext.RSSetViewports(1, in Viewport_);
|
||||
}
|
||||
|
||||
public void SwapBuffer()
|
||||
{
|
||||
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
SwapChain.Present(Window_.VSync ? 1u : 0u, 0)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public IPolygon GenPolygon(float[] vertices, uint[] indices, float[] uvs)
|
||||
{
|
||||
return new DirectX11Polygon(vertices, indices, uvs);
|
||||
}
|
||||
|
||||
public IShader GenShader(string name)
|
||||
{
|
||||
using StreamReader stream = new StreamReader(@$"{name}.hlsl");
|
||||
return new DirectX11Shader(stream.ReadToEnd());
|
||||
}
|
||||
|
||||
|
||||
public unsafe ITexture GenTexture(void* data, int width, int height, RgbaType rgbaType)
|
||||
{
|
||||
return new DirectX11Texture(data, width, height, rgbaType);
|
||||
}
|
||||
|
||||
public unsafe void DrawPolygon(IPolygon polygon, IShader shader, ITexture texture, BlendType blendType)
|
||||
{
|
||||
DirectX11Polygon dx11polygon = (DirectX11Polygon)polygon;
|
||||
DirectX11Shader dx11shader = (DirectX11Shader)shader;
|
||||
DirectX11Texture dx11texture = (DirectX11Texture)texture;
|
||||
|
||||
if (dx11texture == null || dx11texture.IsWrongPixels) return;
|
||||
|
||||
ImmediateContext.IASetInputLayout(dx11shader.InputLayout);
|
||||
ImmediateContext.IASetVertexBuffers(0, 1, dx11polygon.VertexBuffer, dx11polygon.VertexStride, 0);
|
||||
ImmediateContext.IASetIndexBuffer(dx11polygon.IndexBuffer, Format.FormatR32Uint, 0);
|
||||
ImmediateContext.IASetPrimitiveTopology(D3DPrimitiveTopology.D3DPrimitiveTopologyTrianglelist);
|
||||
|
||||
dx11shader.Update();
|
||||
|
||||
// Bind our shaders.
|
||||
ImmediateContext.VSSetShader(dx11shader.VertexShader, default(ComPtr<ID3D11ClassInstance>), 0);
|
||||
ImmediateContext.VSSetConstantBuffers(0, 1, dx11shader.ConstantBuffer);
|
||||
|
||||
|
||||
float[] blendFactors = new float[] { 1, 1, 1, 1 };
|
||||
ImmediateContext.OMSetBlendState(BlendStates[(int)blendType], blendFactors, 0xffffffff);
|
||||
|
||||
|
||||
ImmediateContext.PSSetShader(dx11shader.PixelShader, default(ComPtr<ID3D11ClassInstance>), 0);
|
||||
ImmediateContext.PSSetConstantBuffers(0, 1, dx11shader.ConstantBuffer);
|
||||
|
||||
ImmediateContext.PSSetShaderResources(0, 1, dx11texture.TextureView);
|
||||
ImmediateContext.PSSetSamplers(0, 1, SamplerState);
|
||||
|
||||
ImmediateContext.ClearDepthStencilView(DepthStencilView, (uint)ClearFlag.Depth | (uint)ClearFlag.Stencil, 1.0f, 0);
|
||||
|
||||
// Draw the quad.
|
||||
ImmediateContext.DrawIndexed(polygon.IndiceCount, 0, 0);
|
||||
}
|
||||
|
||||
public unsafe SKBitmap GetScreenPixels()
|
||||
{
|
||||
ComPtr<ID3D11Texture2D> backBuffer = default;
|
||||
var iid = ID3D11Texture2D.Guid;
|
||||
void* ptr = backBuffer;
|
||||
SwapChain.GetBuffer(0, &iid, &ptr);
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public unsafe void GetScreenPixelsASync(Action<SKBitmap> action)
|
||||
{
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
SamplerState.Dispose();
|
||||
|
||||
for(int i = 0; i < 5; i++)
|
||||
{
|
||||
BlendStates[i].Dispose();
|
||||
}
|
||||
|
||||
DepthStencilTexture.Dispose();
|
||||
DepthStencilView.Dispose();
|
||||
RenderTargetView.Dispose();
|
||||
ImmediateContext.Dispose();
|
||||
Device.Dispose();
|
||||
Factory.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,82 +0,0 @@
|
||||
using Silk.NET.Windowing;
|
||||
using Silk.NET.Direct3D11;
|
||||
using Silk.NET.Core.Native;
|
||||
using SkiaSharp;
|
||||
|
||||
namespace SampleFramework
|
||||
{
|
||||
public class DirectX11Polygon : IPolygon
|
||||
{
|
||||
public uint IndiceCount { get; set; }
|
||||
|
||||
public uint VertexStride;
|
||||
|
||||
public ComPtr<ID3D11Buffer> VertexBuffer = default;
|
||||
|
||||
public ComPtr<ID3D11Buffer> IndexBuffer = default;
|
||||
|
||||
|
||||
public unsafe DirectX11Polygon(float[] vertices, uint[] indices, float[] uvs)
|
||||
{
|
||||
IndiceCount = (uint)indices.Length;
|
||||
VertexStride = 5U * sizeof(float);
|
||||
|
||||
List<float> mergedArray = new();
|
||||
for(int i = 0; i < vertices.Length / 3; i++)
|
||||
{
|
||||
int pos = 3 * i;
|
||||
int pos_uv = 2 * i;
|
||||
mergedArray.Add(vertices[pos]);
|
||||
mergedArray.Add(vertices[pos + 1]);
|
||||
mergedArray.Add(vertices[pos + 2]);
|
||||
mergedArray.Add(uvs[pos_uv]);
|
||||
mergedArray.Add(uvs[pos_uv + 1]);
|
||||
}
|
||||
|
||||
// Create our vertex buffer.
|
||||
var vertexBufferDesc = new BufferDesc
|
||||
{
|
||||
ByteWidth = (uint)(sizeof(float) * mergedArray.Count),
|
||||
Usage = Usage.Default,
|
||||
BindFlags = (uint)BindFlag.VertexBuffer
|
||||
};
|
||||
|
||||
//fixed (float* vertexData = vertices)
|
||||
fixed (float* vertexData = mergedArray.ToArray())
|
||||
{
|
||||
var subresourceData = new SubresourceData
|
||||
{
|
||||
PSysMem = vertexData,
|
||||
SysMemPitch = 0,
|
||||
SysMemSlicePitch = 0
|
||||
};
|
||||
|
||||
SilkMarshal.ThrowHResult(DirectX11Device.Device.CreateBuffer(in vertexBufferDesc, in subresourceData, ref VertexBuffer));
|
||||
}
|
||||
|
||||
// Create our index buffer.
|
||||
var indexBufferDesc = new BufferDesc
|
||||
{
|
||||
ByteWidth = (uint)(indices.Length * sizeof(uint)),
|
||||
Usage = Usage.Default,
|
||||
BindFlags = (uint)BindFlag.IndexBuffer
|
||||
};
|
||||
|
||||
fixed (uint* indexData = indices)
|
||||
{
|
||||
var subresourceData = new SubresourceData
|
||||
{
|
||||
PSysMem = indexData
|
||||
};
|
||||
|
||||
SilkMarshal.ThrowHResult(DirectX11Device.Device.CreateBuffer(in indexBufferDesc, in subresourceData, ref IndexBuffer));
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
VertexBuffer.Dispose();
|
||||
IndexBuffer.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,235 +0,0 @@
|
||||
using System;
|
||||
using Silk.NET.Windowing;
|
||||
using Silk.NET.Direct3D11;
|
||||
using Silk.NET.DXGI;
|
||||
using Silk.NET.Core.Native;
|
||||
using Silk.NET.Maths;
|
||||
using SkiaSharp;
|
||||
|
||||
namespace SampleFramework
|
||||
{
|
||||
public class DirectX11Shader : IShader
|
||||
{
|
||||
public struct ConstantBufferStruct
|
||||
{
|
||||
public Matrix4X4<float> Projection;
|
||||
|
||||
public Vector4D<float> Color;
|
||||
|
||||
public Vector4D<float> TextureRect;
|
||||
|
||||
public Matrix4X4<float> Camera;
|
||||
}
|
||||
|
||||
internal ComPtr<ID3D10Blob> VertexCode = default;
|
||||
|
||||
internal ComPtr<ID3D10Blob> PixelCode = default;
|
||||
|
||||
internal ComPtr<ID3D11VertexShader> VertexShader = default;
|
||||
|
||||
internal ComPtr<ID3D11PixelShader> PixelShader = default;
|
||||
|
||||
internal ComPtr<ID3D11InputLayout> InputLayout = default;
|
||||
|
||||
internal ComPtr<ID3D11Buffer> ConstantBuffer;
|
||||
|
||||
internal ConstantBufferStruct ConstantBufferStruct_;
|
||||
|
||||
public unsafe DirectX11Shader(string shaderSource)
|
||||
{
|
||||
var shaderBytes = System.Text.Encoding.ASCII.GetBytes(shaderSource);
|
||||
ComPtr<ID3D10Blob> vertexErrors = default;
|
||||
|
||||
// Compile vertex shader.
|
||||
HResult hr = DirectX11Device.D3dCompiler.Compile
|
||||
(
|
||||
in shaderBytes[0],
|
||||
(uint)shaderBytes.Length,
|
||||
nameof(shaderSource),
|
||||
null,
|
||||
default(ComPtr<ID3DInclude>),
|
||||
"vs_main",
|
||||
"vs_5_0",
|
||||
0,
|
||||
0,
|
||||
ref VertexCode,
|
||||
ref vertexErrors
|
||||
);
|
||||
|
||||
// Check for compilation errors.
|
||||
if (hr.IsFailure)
|
||||
{
|
||||
if (vertexErrors.Handle != null)
|
||||
{
|
||||
Console.WriteLine(SilkMarshal.PtrToString((int)vertexErrors.GetBufferPointer(), NativeStringEncoding.LPWStr));
|
||||
}
|
||||
|
||||
hr.Throw();
|
||||
}
|
||||
|
||||
// Compile pixel shader.
|
||||
ComPtr<ID3D10Blob> pixelErrors = default;
|
||||
hr = DirectX11Device.D3dCompiler.Compile
|
||||
(
|
||||
in shaderBytes[0],
|
||||
(uint)shaderBytes.Length,
|
||||
nameof(shaderSource),
|
||||
null,
|
||||
default(ComPtr<ID3DInclude>),
|
||||
"ps_main",
|
||||
"ps_5_0",
|
||||
0,
|
||||
0,
|
||||
ref PixelCode,
|
||||
ref pixelErrors
|
||||
);
|
||||
|
||||
// Check for compilation errors.
|
||||
if (hr.IsFailure)
|
||||
{
|
||||
if (pixelErrors.Handle != null)
|
||||
{
|
||||
Console.WriteLine(SilkMarshal.PtrToString((int)pixelErrors.GetBufferPointer(), NativeStringEncoding.LPWStr));
|
||||
}
|
||||
|
||||
hr.Throw();
|
||||
}
|
||||
|
||||
// Clean up any resources.
|
||||
vertexErrors.Dispose();
|
||||
pixelErrors.Dispose();
|
||||
|
||||
ConstantBufferStruct_ = new();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Create vertex shader.
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
DirectX11Device.Device.CreateVertexShader
|
||||
(
|
||||
VertexCode.GetBufferPointer(),
|
||||
VertexCode.GetBufferSize(),
|
||||
default(ComPtr<ID3D11ClassLinkage>),
|
||||
ref VertexShader
|
||||
)
|
||||
);
|
||||
|
||||
// Create pixel shader.
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
DirectX11Device.Device.CreatePixelShader
|
||||
(
|
||||
PixelCode.GetBufferPointer(),
|
||||
PixelCode.GetBufferSize(),
|
||||
default(ComPtr<ID3D11ClassLinkage>),
|
||||
ref PixelShader
|
||||
)
|
||||
);
|
||||
|
||||
// Describe the layout of the input data for the shader.
|
||||
fixed (byte* posName = SilkMarshal.StringToMemory("POS"))
|
||||
{
|
||||
fixed (byte* uvposName = SilkMarshal.StringToMemory("UVPOS"))
|
||||
{
|
||||
var inputElements = new InputElementDesc[]
|
||||
{
|
||||
new InputElementDesc()
|
||||
{
|
||||
SemanticName = posName,
|
||||
SemanticIndex = 0,
|
||||
Format = Format.FormatR32G32B32Float,
|
||||
InputSlot = 0,
|
||||
AlignedByteOffset = 0,
|
||||
InputSlotClass = InputClassification.PerVertexData,
|
||||
InstanceDataStepRate = 0
|
||||
}
|
||||
,
|
||||
new InputElementDesc()
|
||||
{
|
||||
SemanticName = uvposName,
|
||||
SemanticIndex = 0,
|
||||
Format = Format.FormatR32G32Float,
|
||||
InputSlot = 0,
|
||||
AlignedByteOffset = sizeof(float) * 3,
|
||||
InputSlotClass = InputClassification.PerVertexData,
|
||||
InstanceDataStepRate = 0
|
||||
}
|
||||
};
|
||||
|
||||
fixed(InputElementDesc* data = inputElements)
|
||||
{
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
DirectX11Device.Device.CreateInputLayout
|
||||
(
|
||||
data,
|
||||
(uint)inputElements.Length,
|
||||
VertexCode.GetBufferPointer(),
|
||||
VertexCode.GetBufferSize(),
|
||||
ref InputLayout
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
BufferDesc bufferDesc = new()
|
||||
{
|
||||
ByteWidth = (uint)sizeof(ConstantBufferStruct),
|
||||
Usage = Usage.Default,
|
||||
BindFlags = (uint)BindFlag.ConstantBuffer,
|
||||
CPUAccessFlags = 0,
|
||||
MiscFlags = 0,
|
||||
StructureByteStride = 0
|
||||
};
|
||||
|
||||
SilkMarshal.ThrowHResult(
|
||||
DirectX11Device.Device.CreateBuffer(in bufferDesc, null, ref ConstantBuffer)
|
||||
);
|
||||
}
|
||||
|
||||
public void SetMVP(Matrix4X4<float> mvp)
|
||||
{
|
||||
ConstantBufferStruct_.Projection = mvp;
|
||||
}
|
||||
|
||||
public void SetColor(Vector4D<float> color)
|
||||
{
|
||||
ConstantBufferStruct_.Color = color;
|
||||
}
|
||||
|
||||
public void SetTextureRect(Vector4D<float> rect)
|
||||
{
|
||||
ConstantBufferStruct_.TextureRect = rect;
|
||||
}
|
||||
|
||||
public void SetCamera(Matrix4X4<float> camera)
|
||||
{
|
||||
ConstantBufferStruct_.Camera = camera;
|
||||
}
|
||||
|
||||
public unsafe void Update()
|
||||
{
|
||||
DirectX11Device.ImmediateContext.UpdateSubresource(ConstantBuffer, 0, null, ConstantBufferStruct_, 0, 0);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
ConstantBuffer.Dispose();
|
||||
VertexCode.Dispose();
|
||||
PixelCode.Dispose();
|
||||
VertexShader.Dispose();
|
||||
PixelShader.Dispose();
|
||||
InputLayout.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,73 +0,0 @@
|
||||
using Silk.NET.Windowing;
|
||||
using Silk.NET.Direct3D11;
|
||||
using Silk.NET.DXGI;
|
||||
using SkiaSharp;
|
||||
using Silk.NET.Core.Native;
|
||||
|
||||
namespace SampleFramework
|
||||
{
|
||||
public class DirectX11Texture : ITexture
|
||||
{
|
||||
internal bool IsWrongPixels;
|
||||
internal ComPtr<ID3D11Texture2D> Texture;
|
||||
internal ComPtr<ID3D11ShaderResourceView> TextureView;
|
||||
|
||||
private Format RgbaTypeToFormat(RgbaType rgbaType)
|
||||
{
|
||||
switch(rgbaType)
|
||||
{
|
||||
case RgbaType.Rgba:
|
||||
return Format.FormatR8G8B8A8Unorm;
|
||||
case RgbaType.Bgra:
|
||||
return Format.FormatB8G8R8A8Unorm;
|
||||
default:
|
||||
return Format.FormatR8G8B8A8Unorm;
|
||||
}
|
||||
}
|
||||
|
||||
public unsafe DirectX11Texture(void* data, int width, int height, RgbaType rgbaType)
|
||||
{
|
||||
Texture2DDesc texture2DDesc = new()
|
||||
{
|
||||
Width = (uint)width,
|
||||
Height = (uint)height,
|
||||
CPUAccessFlags = 0,
|
||||
BindFlags = (uint)BindFlag.ShaderResource,
|
||||
MipLevels = 1,
|
||||
Format = RgbaTypeToFormat(rgbaType),
|
||||
MiscFlags = 0,
|
||||
ArraySize = 1,
|
||||
SampleDesc = new()
|
||||
{
|
||||
Count = 1,
|
||||
Quality = 0,
|
||||
}
|
||||
};
|
||||
|
||||
SubresourceData subresourceData = new()
|
||||
{
|
||||
PSysMem = data,
|
||||
SysMemPitch = (uint)(width * 4),
|
||||
SysMemSlicePitch = 0
|
||||
};
|
||||
|
||||
if (data == null || width <= 0 || height <= 0)
|
||||
{
|
||||
IsWrongPixels = true;
|
||||
}
|
||||
|
||||
SilkMarshal.ThrowHResult(
|
||||
DirectX11Device.Device.CreateTexture2D(texture2DDesc, subresourceData, ref Texture)
|
||||
);
|
||||
SilkMarshal.ThrowHResult(
|
||||
DirectX11Device.Device.CreateShaderResourceView(Texture, null, ref TextureView)
|
||||
);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Texture.Dispose();
|
||||
TextureView.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,771 +0,0 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Silk.NET.Windowing;
|
||||
using Silk.NET.Maths;
|
||||
using Silk.NET.Core.Native;
|
||||
using Silk.NET.Direct3D.Compilers;
|
||||
using Silk.NET.Direct3D12;
|
||||
using Silk.NET.DXGI;
|
||||
using SkiaSharp;
|
||||
|
||||
|
||||
namespace SampleFramework
|
||||
{
|
||||
unsafe class DirectX12Device : IGraphicsDevice
|
||||
{
|
||||
private const uint FrameCount = 2;
|
||||
|
||||
private D3D12 D3d12;
|
||||
|
||||
private DXGI DxGi;
|
||||
|
||||
internal D3DCompiler D3dCompiler;
|
||||
|
||||
private ComPtr<ID3D12Debug> DebugController;
|
||||
|
||||
private ComPtr<IDXGIFactory4> Factory;
|
||||
|
||||
private ComPtr<IDXGIAdapter1> HardwareAdapters;
|
||||
|
||||
internal ComPtr<ID3D12Device> Device;
|
||||
|
||||
private ComPtr<ID3D12CommandQueue> CommandQueue;
|
||||
|
||||
private ComPtr<IDXGISwapChain3> SwapChain;
|
||||
|
||||
internal ComPtr<ID3D12DescriptorHeap> RtvHeap;
|
||||
|
||||
private ComPtr<ID3D12DescriptorHeap> DSVHeap;
|
||||
|
||||
internal ComPtr<ID3D12DescriptorHeap> CBVHeap;
|
||||
|
||||
private ID3D12Resource*[] RenderTargets = new ID3D12Resource*[FrameCount];
|
||||
|
||||
private ComPtr<ID3D12Resource> DepthStencil;
|
||||
|
||||
private ComPtr<ID3D12CommandAllocator>[] CommandAllocator = new ComPtr<ID3D12CommandAllocator>[FrameCount];
|
||||
|
||||
internal ComPtr<ID3D12RootSignature> RootSignature;
|
||||
|
||||
internal ComPtr<ID3D12GraphicsCommandList>[] CommandList = new ComPtr<ID3D12GraphicsCommandList>[FrameCount];
|
||||
|
||||
private ComPtr<ID3D12Fence> Fence;
|
||||
|
||||
private uint[] FenceValue = new uint[FrameCount];
|
||||
|
||||
private IntPtr FenceEvent;
|
||||
|
||||
private uint RtvDescriptorSize;
|
||||
|
||||
private float[] CurrnetClearColor;
|
||||
|
||||
private IWindow Window_;
|
||||
|
||||
internal uint FrameBufferIndex;
|
||||
|
||||
private bool IsActivate;
|
||||
|
||||
|
||||
|
||||
private Viewport viewport;
|
||||
private Box2D<int> rect;
|
||||
|
||||
|
||||
|
||||
private bool SupportsRequiredDirect3DVersion(IDXGIAdapter1* adapter1)
|
||||
{
|
||||
var iid = ID3D12Device.Guid;
|
||||
return HResult.IndicatesSuccess(D3d12.CreateDevice((IUnknown*)adapter1, D3DFeatureLevel.Level110, &iid, null));
|
||||
}
|
||||
|
||||
private ID3D12Device* GetDevice()
|
||||
{
|
||||
ComPtr<IDXGIAdapter1> adapter = default;
|
||||
ID3D12Device* device = default;
|
||||
|
||||
for (uint i = 0; Factory.EnumAdapters(i, ref adapter) != 0x887A0002; i++)
|
||||
{
|
||||
AdapterDesc1 desc = default;
|
||||
adapter.GetDesc1(ref desc);
|
||||
|
||||
if ((desc.Flags & (uint)AdapterFlag.Software) != 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (SupportsRequiredDirect3DVersion(adapter)) break;
|
||||
}
|
||||
|
||||
var device_iid = ID3D12Device.Guid;
|
||||
IDXGIAdapter1* hardwareAdapters = adapter.Detach();
|
||||
HardwareAdapters = hardwareAdapters;
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
D3d12.CreateDevice((IUnknown*)hardwareAdapters, D3DFeatureLevel.Level110, &device_iid, (void**)&device)
|
||||
);
|
||||
|
||||
return device;
|
||||
}
|
||||
|
||||
private void CreateCommandQueue()
|
||||
{
|
||||
CommandQueueDesc commandQueueDesc = new CommandQueueDesc();
|
||||
commandQueueDesc.Flags = CommandQueueFlags.None;
|
||||
commandQueueDesc.Type = CommandListType.Direct;
|
||||
void* commandQueue = null;
|
||||
var iid = ID3D12CommandQueue.Guid;
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
Device.CreateCommandQueue(&commandQueueDesc, ref iid, &commandQueue)
|
||||
);
|
||||
CommandQueue = (ID3D12CommandQueue*)commandQueue;
|
||||
}
|
||||
|
||||
private void CreateSwapChain()
|
||||
{
|
||||
SwapChainDesc1 swapChainDesc = new(){
|
||||
Width = (uint)Window_.FramebufferSize.X,
|
||||
Height = (uint)Window_.FramebufferSize.Y,
|
||||
Format = Format.FormatR8G8B8A8Unorm,
|
||||
SampleDesc = new SampleDesc(1, 0),
|
||||
BufferUsage = DXGI.UsageRenderTargetOutput,
|
||||
BufferCount = FrameCount,
|
||||
Scaling = Scaling.Stretch,
|
||||
SwapEffect = SwapEffect.FlipDiscard,
|
||||
//Flags = (uint)SwapChainFlag.AllowModeSwitch,
|
||||
AlphaMode = AlphaMode.Ignore
|
||||
};
|
||||
|
||||
SwapChainFullscreenDesc swapChainFullscreenDesc = new()
|
||||
{
|
||||
RefreshRate = new Rational(0, 1),
|
||||
ScanlineOrdering = ModeScanlineOrder.Unspecified,
|
||||
Scaling = ModeScaling.Unspecified,
|
||||
Windowed = true
|
||||
};
|
||||
|
||||
void* device = CommandQueue;
|
||||
IDXGISwapChain1* swapChain;
|
||||
|
||||
/*
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
Factory.CreateSwapChainForHwnd((IUnknown*)device, Window_.Native.DXHandle.Value, swapChainDesc, swapChainFullscreenDesc, (IDXGIOutput*)0, &swapChain)
|
||||
);
|
||||
*/
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
Window_.CreateDxgiSwapchain((IDXGIFactory2*)Factory.AsVtblPtr(), (IUnknown*)device, &swapChainDesc, &swapChainFullscreenDesc, (IDXGIOutput*)0, &swapChain)
|
||||
);
|
||||
|
||||
SwapChain = (IDXGISwapChain3*)swapChain;
|
||||
|
||||
FrameBufferIndex = SwapChain.GetCurrentBackBufferIndex();
|
||||
}
|
||||
|
||||
private void CreateRTVHeap()
|
||||
{
|
||||
DescriptorHeapDesc rtvHeapDesc = new DescriptorHeapDesc()
|
||||
{
|
||||
NumDescriptors = FrameCount + 1,
|
||||
Type = DescriptorHeapType.Rtv,
|
||||
Flags = DescriptorHeapFlags.None
|
||||
};
|
||||
|
||||
void* rtvHeap = null;
|
||||
var iid = ID3D12DescriptorHeap.Guid;
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
Device.CreateDescriptorHeap(&rtvHeapDesc, ref iid, &rtvHeap)
|
||||
);
|
||||
RtvHeap = (ID3D12DescriptorHeap*)rtvHeap;
|
||||
|
||||
RtvDescriptorSize = Device.GetDescriptorHandleIncrementSize(DescriptorHeapType.Rtv);
|
||||
}
|
||||
|
||||
protected virtual void CreateDSVHeap()
|
||||
{
|
||||
var dsvHeapDesc = new DescriptorHeapDesc
|
||||
{
|
||||
NumDescriptors = 1,
|
||||
Type = DescriptorHeapType.Dsv,
|
||||
};
|
||||
|
||||
ID3D12DescriptorHeap* dsvHeap;
|
||||
|
||||
var iid = ID3D12DescriptorHeap.Guid;
|
||||
SilkMarshal.ThrowHResult(Device.CreateDescriptorHeap(&dsvHeapDesc, &iid, (void**) &dsvHeap));
|
||||
|
||||
DSVHeap = dsvHeap;
|
||||
}
|
||||
|
||||
private void CreateCBVHeap()
|
||||
{
|
||||
DescriptorHeapDesc cbvHeapDesc = new DescriptorHeapDesc()
|
||||
{
|
||||
NumDescriptors = 1,
|
||||
Type = DescriptorHeapType.CbvSrvUav,
|
||||
Flags = DescriptorHeapFlags.ShaderVisible
|
||||
};
|
||||
|
||||
void* cbvHeap = null;
|
||||
var iid = ID3D12DescriptorHeap.Guid;
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
Device.CreateDescriptorHeap(&cbvHeapDesc, ref iid, &cbvHeap)
|
||||
);
|
||||
CBVHeap = (ID3D12DescriptorHeap*)cbvHeap;
|
||||
}
|
||||
|
||||
private void CreateRenderTargetViews()
|
||||
{
|
||||
CpuDescriptorHandle rtvHandle = new CpuDescriptorHandle();
|
||||
rtvHandle.Ptr = RtvHeap.GetCPUDescriptorHandleForHeapStart().Ptr;
|
||||
var iid = ID3D12Resource.Guid;
|
||||
|
||||
for (uint i = 0; i < FrameCount; i++)
|
||||
{
|
||||
ID3D12Resource* renderTarget;
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
SwapChain.GetBuffer(i, ref iid, (void**)&renderTarget)
|
||||
);
|
||||
RenderTargets[i] = renderTarget;
|
||||
|
||||
|
||||
Device.CreateRenderTargetView(renderTarget, (RenderTargetViewDesc*)0, rtvHandle);
|
||||
rtvHandle.Ptr += RtvDescriptorSize;
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateDepthStencil()
|
||||
{
|
||||
|
||||
ID3D12Resource* depthStencil;
|
||||
|
||||
var heapProperties = new HeapProperties(HeapType.Default);
|
||||
|
||||
var resourceDesc = new ResourceDesc
|
||||
(
|
||||
ResourceDimension.Texture2D,
|
||||
0ul,
|
||||
(ulong) Window_.FramebufferSize.X,
|
||||
(uint) Window_.FramebufferSize.Y,
|
||||
1,
|
||||
1,
|
||||
Format.FormatD32Float,
|
||||
new SampleDesc() {Count = 1, Quality = 0},
|
||||
TextureLayout.LayoutUnknown,
|
||||
ResourceFlags.AllowDepthStencil
|
||||
);
|
||||
|
||||
var clearValue = new ClearValue(Format.FormatD32Float, depthStencil: new DepthStencilValue(1.0f, 0));
|
||||
|
||||
var iid = ID3D12Resource.Guid;
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
Device.CreateCommittedResource
|
||||
(
|
||||
&heapProperties, HeapFlags.None, &resourceDesc, ResourceStates.DepthWrite,
|
||||
&clearValue, &iid, (void**) &depthStencil
|
||||
)
|
||||
);
|
||||
|
||||
var dsvDesc = new DepthStencilViewDesc
|
||||
{
|
||||
Format = Format.FormatD32Float,
|
||||
ViewDimension = DsvDimension.Texture2D
|
||||
};
|
||||
Device.CreateDepthStencilView(depthStencil, &dsvDesc, DSVHeap.GetCPUDescriptorHandleForHeapStart());
|
||||
|
||||
DepthStencil = depthStencil;
|
||||
}
|
||||
|
||||
private void CreateCommandAllocator()
|
||||
{
|
||||
var iid = ID3D12CommandAllocator.Guid;
|
||||
|
||||
for(int i = 0; i < FrameCount; i++)
|
||||
{
|
||||
void* commandAllocator;
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
Device.CreateCommandAllocator(CommandListType.Direct, &iid, &commandAllocator)
|
||||
);
|
||||
|
||||
CommandAllocator[i] = (ID3D12CommandAllocator*)commandAllocator;
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateRootSignature()
|
||||
{
|
||||
DescriptorRange[] range = new DescriptorRange[2]
|
||||
{
|
||||
new DescriptorRange()
|
||||
{
|
||||
RangeType = DescriptorRangeType.Srv,
|
||||
NumDescriptors = 1,
|
||||
BaseShaderRegister = 0,
|
||||
RegisterSpace = 0,
|
||||
OffsetInDescriptorsFromTableStart = D3D12.DescriptorRangeOffsetAppend
|
||||
},
|
||||
new DescriptorRange()
|
||||
{
|
||||
RangeType = DescriptorRangeType.Cbv,
|
||||
NumDescriptors = 1,
|
||||
BaseShaderRegister = 0,
|
||||
RegisterSpace = 0,
|
||||
OffsetInDescriptorsFromTableStart = D3D12.DescriptorRangeOffsetAppend
|
||||
}
|
||||
};
|
||||
var range1 = range[0];
|
||||
var range2 = range[1];
|
||||
var rootParameters = stackalloc RootParameter[2]
|
||||
{
|
||||
new RootParameter()
|
||||
{
|
||||
ParameterType = RootParameterType.TypeDescriptorTable,
|
||||
DescriptorTable = new RootDescriptorTable(1, &range1),
|
||||
ShaderVisibility = ShaderVisibility.Pixel
|
||||
},
|
||||
new RootParameter()
|
||||
{
|
||||
ParameterType = RootParameterType.TypeDescriptorTable,
|
||||
DescriptorTable = new RootDescriptorTable(1, &range2),
|
||||
ShaderVisibility = ShaderVisibility.Vertex
|
||||
}
|
||||
};
|
||||
|
||||
StaticSamplerDesc sampleDesc = new()
|
||||
{
|
||||
Filter = Filter.MinMagMipPoint,
|
||||
AddressU = TextureAddressMode.Wrap,
|
||||
AddressV = TextureAddressMode.Wrap,
|
||||
AddressW = TextureAddressMode.Wrap,
|
||||
MipLODBias = 0,
|
||||
MaxAnisotropy = 16,
|
||||
ComparisonFunc = ComparisonFunc.Never,
|
||||
BorderColor = StaticBorderColor.TransparentBlack,
|
||||
MinLOD = 0.0f,
|
||||
MaxLOD = float.MaxValue,
|
||||
ShaderRegister = 0,
|
||||
RegisterSpace = 0,
|
||||
ShaderVisibility = ShaderVisibility.Pixel,
|
||||
};
|
||||
|
||||
RootSignatureDesc rootSignatureDesc = new RootSignatureDesc();
|
||||
rootSignatureDesc.NumParameters = 2;
|
||||
rootSignatureDesc.PParameters = rootParameters;
|
||||
rootSignatureDesc.NumStaticSamplers = 1;
|
||||
rootSignatureDesc.PStaticSamplers = &sampleDesc;
|
||||
rootSignatureDesc.Flags =
|
||||
RootSignatureFlags.AllowInputAssemblerInputLayout |
|
||||
RootSignatureFlags.DenyHullShaderRootAccess |
|
||||
RootSignatureFlags.DenyDomainShaderRootAccess |
|
||||
RootSignatureFlags.DenyGeometryShaderRootAccess |
|
||||
RootSignatureFlags.DenyPixelShaderRootAccess;
|
||||
|
||||
ID3D10Blob* signature = null;
|
||||
ID3D10Blob* error = null;
|
||||
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
D3d12.SerializeRootSignature(&rootSignatureDesc, D3DRootSignatureVersion.Version10, &signature, &error)
|
||||
);
|
||||
|
||||
var iid = ID3D12RootSignature.Guid;
|
||||
void* rootSignature;
|
||||
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
Device.CreateRootSignature(0, signature->GetBufferPointer(), signature->GetBufferSize(), &iid, &rootSignature)
|
||||
);
|
||||
|
||||
RootSignature = (ID3D12RootSignature*)rootSignature;
|
||||
|
||||
signature->Release();
|
||||
if (error != null) error->Release();
|
||||
}
|
||||
|
||||
private void CreateFence()
|
||||
{
|
||||
var iid = ID3D12Fence.Guid;
|
||||
void* fence;
|
||||
Device.CreateFence(0, FenceFlags.None, &iid, &fence);
|
||||
Fence = (ID3D12Fence*)fence;
|
||||
}
|
||||
|
||||
private void CreateFenceEvent()
|
||||
{
|
||||
FenceValue[0] = 1;
|
||||
var fenceEvent = SilkMarshal.CreateWindowsEvent(null, false, false, null);
|
||||
|
||||
if (fenceEvent == IntPtr.Zero)
|
||||
{
|
||||
var hr = Marshal.GetHRForLastWin32Error();
|
||||
Marshal.ThrowExceptionForHR(hr);
|
||||
}
|
||||
}
|
||||
|
||||
private void CreatePipelineState()
|
||||
{
|
||||
}
|
||||
|
||||
private void CreateCommandList()
|
||||
{
|
||||
void* commandList;
|
||||
var iid = ID3D12GraphicsCommandList.Guid;
|
||||
|
||||
for(int i = 0; i < FrameCount; i++)
|
||||
{
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
Device.CreateCommandList(0, CommandListType.Direct, CommandAllocator[i], (ID3D12PipelineState*)0, &iid, &commandList)
|
||||
);
|
||||
|
||||
CommandList[i] = (ID3D12GraphicsCommandList*)commandList;
|
||||
|
||||
CommandList[i].Close();
|
||||
}
|
||||
}
|
||||
|
||||
internal void WaitForGpu(bool moveToNextFrame, bool resetFrameBufferIndex = false)
|
||||
{
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
CommandQueue.Signal(Fence, FenceValue[FrameBufferIndex])
|
||||
);
|
||||
|
||||
if (moveToNextFrame)
|
||||
{
|
||||
FrameBufferIndex = SwapChain.GetCurrentBackBufferIndex();
|
||||
}
|
||||
|
||||
if (!moveToNextFrame || (Fence.GetCompletedValue() < FenceValue[FrameBufferIndex]))
|
||||
{
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
Fence.SetEventOnCompletion(FenceValue[FrameBufferIndex], FenceEvent.ToPointer())
|
||||
);
|
||||
_ = SilkMarshal.WaitWindowsObjects(FenceEvent);
|
||||
}
|
||||
|
||||
FenceValue[FrameBufferIndex]++;
|
||||
|
||||
if (resetFrameBufferIndex) FrameBufferIndex = 0;
|
||||
}
|
||||
|
||||
public DirectX12Device(IWindow window)
|
||||
{
|
||||
Window_ = window;
|
||||
D3d12 = D3D12.GetApi();
|
||||
DxGi = DXGI.GetApi(window, false);
|
||||
D3dCompiler = D3DCompiler.GetApi();
|
||||
|
||||
uint dxgiFactoryFlags = 0;
|
||||
|
||||
#if DEBUG
|
||||
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
D3d12.GetDebugInterface(out DebugController)
|
||||
);
|
||||
DebugController.EnableDebugLayer();
|
||||
dxgiFactoryFlags |= 0x01;
|
||||
|
||||
#endif
|
||||
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
DxGi.CreateDXGIFactory2(dxgiFactoryFlags, out Factory)
|
||||
);
|
||||
|
||||
Device = GetDevice();
|
||||
|
||||
CreateCommandQueue();
|
||||
|
||||
CreateSwapChain();
|
||||
|
||||
CreateRTVHeap();
|
||||
|
||||
CreateDSVHeap();
|
||||
|
||||
CreateCBVHeap();
|
||||
|
||||
CreateRenderTargetViews();
|
||||
|
||||
CreateDepthStencil();
|
||||
|
||||
CreateCommandAllocator();
|
||||
|
||||
CreateRootSignature();
|
||||
|
||||
CreatePipelineState();
|
||||
|
||||
CreateFence();
|
||||
|
||||
CreateFenceEvent();
|
||||
|
||||
CreateCommandList();
|
||||
|
||||
WaitForGpu(false);
|
||||
|
||||
SetViewPort(0, 0, (uint)window.FramebufferSize.X, (uint)window.FramebufferSize.Y);
|
||||
}
|
||||
|
||||
public void SetClearColor(float r, float g, float b, float a)
|
||||
{
|
||||
CurrnetClearColor = new float[] { r, g, b, a };
|
||||
}
|
||||
|
||||
public void SetViewPort(int x, int y, uint width, uint height)
|
||||
{
|
||||
if (CommandList[FrameBufferIndex].AsVtblPtr() == null) return;
|
||||
|
||||
viewport = new Viewport(0, 0, width, height, 0.0f, 1.0f);
|
||||
rect = new Box2D<int>(x, y, new Vector2D<int>((int)width, (int)height));
|
||||
}
|
||||
|
||||
public void SetFrameBuffer(uint width, uint height)
|
||||
{
|
||||
if (width <= 0 || height <= 0) return;
|
||||
|
||||
WaitForGpu(false, true);
|
||||
|
||||
for (uint i = 0; i < FrameCount; i++)
|
||||
{
|
||||
RenderTargets[i]->Release();
|
||||
FenceValue[i] = FenceValue[FrameBufferIndex];
|
||||
}
|
||||
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
SwapChain.ResizeBuffers(FrameCount, width, height, Format.FormatR8G8B8A8Unorm, 0)
|
||||
);
|
||||
|
||||
CreateRenderTargetViews();
|
||||
|
||||
DepthStencil.Release();
|
||||
CreateDepthStencil();
|
||||
}
|
||||
|
||||
private void SetResourceBarrier(ResourceStates stateBefore, ResourceStates stateAfter)
|
||||
{
|
||||
ResourceBarrier resourceBarrier = new ResourceBarrier();
|
||||
resourceBarrier.Type = ResourceBarrierType.Transition;
|
||||
resourceBarrier.Flags = ResourceBarrierFlags.None;
|
||||
resourceBarrier.Transition = new ResourceTransitionBarrier(RenderTargets[FrameBufferIndex], D3D12.ResourceBarrierAllSubresources, stateBefore, stateAfter);
|
||||
CommandList[FrameBufferIndex].ResourceBarrier(1, resourceBarrier);
|
||||
}
|
||||
|
||||
public void ClearBuffer()
|
||||
{
|
||||
WaitForGpu(false);
|
||||
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
CommandAllocator[FrameBufferIndex].Reset()
|
||||
);
|
||||
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
CommandList[FrameBufferIndex].Reset(CommandAllocator[FrameBufferIndex], (ID3D12PipelineState*)0)
|
||||
);
|
||||
|
||||
CommandList[FrameBufferIndex].SetGraphicsRootSignature(RootSignature);
|
||||
CommandList[FrameBufferIndex].RSSetViewports(1, viewport);
|
||||
CommandList[FrameBufferIndex].RSSetScissorRects(1, rect);
|
||||
|
||||
|
||||
SetResourceBarrier(ResourceStates.Present, ResourceStates.RenderTarget);
|
||||
|
||||
|
||||
CpuDescriptorHandle rtvHandle = new CpuDescriptorHandle();
|
||||
rtvHandle.Ptr = RtvHeap.GetCPUDescriptorHandleForHeapStart().Ptr + FrameBufferIndex * RtvDescriptorSize;
|
||||
CommandList[FrameBufferIndex].OMSetRenderTargets(1, rtvHandle, false, null);
|
||||
|
||||
fixed (float* color = CurrnetClearColor)
|
||||
{
|
||||
CommandList[FrameBufferIndex].ClearRenderTargetView(rtvHandle, color, 0, (Box2D<int>*)0);
|
||||
}
|
||||
|
||||
var dsvHandle = DSVHeap.GetCPUDescriptorHandleForHeapStart();
|
||||
CommandList[FrameBufferIndex].ClearDepthStencilView(dsvHandle, ClearFlags.Depth, 1, 0, 0, (Box2D<int>*)0);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//CommandList[FrameBufferIndex].DrawInstanced(3, 1, 0, 0);
|
||||
}
|
||||
|
||||
public void SwapBuffer()
|
||||
{
|
||||
SetResourceBarrier(ResourceStates.RenderTarget, ResourceStates.Present);
|
||||
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
CommandList[FrameBufferIndex].Close()
|
||||
);
|
||||
|
||||
const int CommandListsCount = 1;
|
||||
void* commandList = CommandList[FrameBufferIndex];
|
||||
var ppCommandLists = stackalloc ID3D12CommandList*[CommandListsCount]
|
||||
{
|
||||
(ID3D12CommandList*)commandList,
|
||||
};
|
||||
CommandQueue.ExecuteCommandLists(CommandListsCount, ppCommandLists);
|
||||
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
Device.GetDeviceRemovedReason()
|
||||
);
|
||||
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
SwapChain.Present(Window_.VSync ? 1u : 0u, 0)
|
||||
);
|
||||
|
||||
WaitForGpu(false);
|
||||
|
||||
FrameBufferIndex = SwapChain.GetCurrentBackBufferIndex();
|
||||
|
||||
IsActivate = true;
|
||||
}
|
||||
|
||||
public IPolygon GenPolygon(float[] vertices, uint[] indices, float[] uvs)
|
||||
{
|
||||
return new DirectX12Polygon(this, vertices, indices, uvs);
|
||||
}
|
||||
|
||||
public IShader GenShader(string name)
|
||||
{
|
||||
return new DirectX12Shader(this,
|
||||
@"
|
||||
|
||||
Texture2D g_texture : register(t0);
|
||||
SamplerState g_sampler : register(s0);
|
||||
|
||||
struct vs_in {
|
||||
float3 position_local : POS;
|
||||
float2 uvposition_local : UVPOS;
|
||||
};
|
||||
|
||||
struct vs_out {
|
||||
float4 position_clip : SV_POSITION;
|
||||
float2 uvposition_clip : TEXCOORD0;
|
||||
};
|
||||
|
||||
cbuffer ConstantBufferStruct
|
||||
{
|
||||
float4x4 Projection;
|
||||
float4 Color;
|
||||
float4 TextureRect;
|
||||
}
|
||||
|
||||
vs_out vs_main(vs_in input) {
|
||||
vs_out output = (vs_out)0;
|
||||
|
||||
float4 position = float4(input.position_local, 1.0);
|
||||
position = mul(Projection, position);
|
||||
|
||||
output.position_clip = position;
|
||||
|
||||
float2 texcoord = float2(TextureRect.x, TextureRect.y);
|
||||
texcoord.x += input.uvposition_local.x * TextureRect.z;
|
||||
texcoord.y += input.uvposition_local.y * TextureRect.w;
|
||||
|
||||
output.uvposition_clip = texcoord;
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
float4 ps_main(vs_out input) : SV_TARGET {
|
||||
float4 totalcolor = float4(1.0, 1.0, 1.0, 1.0);
|
||||
|
||||
totalcolor = g_texture.Sample(g_sampler, input.uvposition_clip);
|
||||
|
||||
totalcolor.rgba *= Color.rgba;
|
||||
|
||||
return totalcolor;
|
||||
}
|
||||
");
|
||||
}
|
||||
|
||||
public unsafe ITexture GenTexture(void* data, int width, int height, RgbaType rgbaType)
|
||||
{
|
||||
return new DirectX12Texture(data, width, height, rgbaType);
|
||||
}
|
||||
|
||||
public void DrawPolygon(IPolygon polygon, IShader shader, ITexture texture, BlendType blendType)
|
||||
{
|
||||
var dx12polygon = (DirectX12Polygon)polygon;
|
||||
var dx12shader = (DirectX12Shader)shader;
|
||||
var dx12texture = (DirectX12Texture)texture;
|
||||
|
||||
dx12shader.Update(this);
|
||||
CommandList[FrameBufferIndex].SetPipelineState(dx12shader.PipelineState);
|
||||
CommandList[FrameBufferIndex].IASetPrimitiveTopology(D3DPrimitiveTopology.D3DPrimitiveTopologyTrianglelist);
|
||||
CommandList[FrameBufferIndex].IASetVertexBuffers(0, 1, dx12polygon.VertexBufferView_);
|
||||
CommandList[FrameBufferIndex].IASetIndexBuffer(dx12polygon.IndexBufferView_);
|
||||
CommandList[FrameBufferIndex].DrawIndexedInstanced(dx12polygon.IndiceCount, 1, 0, 0, 0);
|
||||
}
|
||||
|
||||
public unsafe SKBitmap GetScreenPixels()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public unsafe void GetScreenPixelsASync(Action<SKBitmap> action)
|
||||
{
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
WaitForGpu(false);
|
||||
|
||||
|
||||
Fence.Dispose();
|
||||
for (int i = 0; i < CommandList.Length; i++)
|
||||
{
|
||||
CommandList[i].Dispose();
|
||||
}
|
||||
RootSignature.Dispose();
|
||||
for (int i = 0; i < CommandAllocator.Length; i++)
|
||||
{
|
||||
CommandAllocator[i].Dispose();
|
||||
}
|
||||
for (int i = 0; i < RenderTargets.Length; i++)
|
||||
{
|
||||
RenderTargets[i]->Release();
|
||||
}
|
||||
CBVHeap.Dispose();
|
||||
DSVHeap.Dispose();
|
||||
RtvHeap.Dispose();
|
||||
SwapChain.Dispose();
|
||||
CommandQueue.Dispose();
|
||||
HardwareAdapters.Dispose();
|
||||
Device.Dispose();
|
||||
Factory.Dispose();
|
||||
|
||||
#if DEBUG
|
||||
|
||||
DebugController.Dispose();
|
||||
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
@ -1,181 +0,0 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Silk.NET.Windowing;
|
||||
using Silk.NET.Maths;
|
||||
using Silk.NET.Core.Native;
|
||||
using Silk.NET.Direct3D.Compilers;
|
||||
using Silk.NET.Direct3D12;
|
||||
using Silk.NET.DXGI;
|
||||
|
||||
namespace SampleFramework
|
||||
{
|
||||
internal class DirectX12Polygon : IPolygon
|
||||
{
|
||||
public uint IndiceCount { get; set; }
|
||||
|
||||
public uint VertexStride;
|
||||
|
||||
|
||||
public ComPtr<ID3D12Resource> VertexBuffer;
|
||||
|
||||
public VertexBufferView VertexBufferView_;
|
||||
|
||||
public ComPtr<ID3D12Resource> IndexBuffer;
|
||||
|
||||
public IndexBufferView IndexBufferView_;
|
||||
|
||||
|
||||
private unsafe void CreateVertexBuffer(DirectX12Device device, float[] vertices, float[] uvs)
|
||||
{
|
||||
VertexStride = 5U * sizeof(float);
|
||||
|
||||
List<float> mergedArray = new();
|
||||
for(int i = 0; i < vertices.Length / 3; i++)
|
||||
{
|
||||
int pos = 3 * i;
|
||||
int pos_uv = 2 * i;
|
||||
mergedArray.Add(vertices[pos]);
|
||||
mergedArray.Add(vertices[pos + 1]);
|
||||
mergedArray.Add(vertices[pos + 2]);
|
||||
mergedArray.Add(uvs[pos_uv]);
|
||||
mergedArray.Add(uvs[pos_uv + 1]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
uint vertexBufferSize = (uint)(sizeof(float) * mergedArray.Count);
|
||||
|
||||
HeapProperties heapProperties = new HeapProperties()
|
||||
{
|
||||
Type = HeapType.Upload,
|
||||
CPUPageProperty = CpuPageProperty.Unknown,
|
||||
MemoryPoolPreference = MemoryPool.Unknown,
|
||||
CreationNodeMask = 1,
|
||||
VisibleNodeMask = 1
|
||||
};
|
||||
|
||||
ResourceDesc resourceDesc = new ResourceDesc()
|
||||
{
|
||||
Dimension = ResourceDimension.Buffer,
|
||||
Alignment = 0,
|
||||
Width = vertexBufferSize,
|
||||
Height = 1,
|
||||
DepthOrArraySize = 1,
|
||||
MipLevels = 1,
|
||||
Format = Format.FormatUnknown,
|
||||
SampleDesc = new SampleDesc()
|
||||
{
|
||||
Count = 1,
|
||||
Quality = 0,
|
||||
},
|
||||
Layout = TextureLayout.LayoutRowMajor,
|
||||
Flags = ResourceFlags.None
|
||||
};
|
||||
|
||||
void* vertexBuffer;
|
||||
var iid = ID3D12Resource.Guid;
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
device.Device.CreateCommittedResource(heapProperties, HeapFlags.None, resourceDesc, ResourceStates.GenericRead, null, &iid, &vertexBuffer)
|
||||
);
|
||||
VertexBuffer = (ID3D12Resource*)vertexBuffer;
|
||||
|
||||
Silk.NET.Direct3D12.Range range = new Silk.NET.Direct3D12.Range();
|
||||
|
||||
void* dataBegin;
|
||||
SilkMarshal.ThrowHResult(VertexBuffer.Map(0, &range, &dataBegin));
|
||||
|
||||
var data = (float*)SilkMarshal.Allocate(sizeof(float) * mergedArray.Count);
|
||||
for(int i = 0; i < mergedArray.Count; i++)
|
||||
{
|
||||
data[i] = mergedArray[i];
|
||||
}
|
||||
|
||||
Unsafe.CopyBlock(dataBegin, data, vertexBufferSize);
|
||||
VertexBuffer.Unmap(0, (Silk.NET.Direct3D12.Range*)0);
|
||||
|
||||
VertexBufferView_ = new VertexBufferView()
|
||||
{
|
||||
BufferLocation = VertexBuffer.GetGPUVirtualAddress(),
|
||||
StrideInBytes = VertexStride,
|
||||
SizeInBytes = vertexBufferSize,
|
||||
};
|
||||
}
|
||||
|
||||
private unsafe void CreateIndexBuffer(DirectX12Device device, uint[] indices)
|
||||
{
|
||||
uint bufferSize = (uint)(sizeof(uint) * indices.Length);
|
||||
|
||||
HeapProperties heapProperties = new HeapProperties()
|
||||
{
|
||||
Type = HeapType.Upload,
|
||||
CPUPageProperty = CpuPageProperty.Unknown,
|
||||
MemoryPoolPreference = MemoryPool.Unknown,
|
||||
CreationNodeMask = 1,
|
||||
VisibleNodeMask = 1
|
||||
};
|
||||
|
||||
ResourceDesc resourceDesc = new ResourceDesc()
|
||||
{
|
||||
Dimension = ResourceDimension.Buffer,
|
||||
Alignment = 0,
|
||||
Width = bufferSize,
|
||||
Height = 1,
|
||||
DepthOrArraySize = 1,
|
||||
MipLevels = 1,
|
||||
Format = Format.FormatUnknown,
|
||||
SampleDesc = new SampleDesc()
|
||||
{
|
||||
Count = 1,
|
||||
Quality = 0,
|
||||
},
|
||||
Layout = TextureLayout.LayoutRowMajor,
|
||||
Flags = ResourceFlags.None
|
||||
};
|
||||
|
||||
void* indexBuffer;
|
||||
var iid = ID3D12Resource.Guid;
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
device.Device.CreateCommittedResource(heapProperties, HeapFlags.None, resourceDesc, ResourceStates.GenericRead, null, &iid, &indexBuffer)
|
||||
);
|
||||
|
||||
IndexBuffer = (ID3D12Resource*)indexBuffer;
|
||||
|
||||
Silk.NET.Direct3D12.Range range = new Silk.NET.Direct3D12.Range();
|
||||
|
||||
void* dataBegin;
|
||||
SilkMarshal.ThrowHResult(IndexBuffer.Map(0, &range, &dataBegin));
|
||||
|
||||
var data = (uint*)SilkMarshal.Allocate(sizeof(uint) * indices.Length);
|
||||
for(int i = 0; i < indices.Length; i++)
|
||||
{
|
||||
data[i] = indices[i];
|
||||
}
|
||||
|
||||
Unsafe.CopyBlock(dataBegin, data, bufferSize);
|
||||
IndexBuffer.Unmap(0, (Silk.NET.Direct3D12.Range*)0);
|
||||
|
||||
IndexBufferView_ = new IndexBufferView()
|
||||
{
|
||||
BufferLocation = IndexBuffer.GetGPUVirtualAddress(),
|
||||
SizeInBytes = bufferSize,
|
||||
Format = Format.FormatR32Uint
|
||||
};
|
||||
}
|
||||
|
||||
public unsafe DirectX12Polygon(DirectX12Device device, float[] vertices, uint[] indices, float[] uvs)
|
||||
{
|
||||
IndiceCount = (uint)indices.Length;
|
||||
CreateVertexBuffer(device, vertices, uvs);
|
||||
CreateIndexBuffer(device, indices);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
VertexBuffer.Release();
|
||||
IndexBuffer.Release();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,274 +0,0 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Silk.NET.Windowing;
|
||||
using Silk.NET.Direct3D12;
|
||||
using Silk.NET.DXGI;
|
||||
using Silk.NET.Core.Native;
|
||||
using Silk.NET.Maths;
|
||||
using SkiaSharp;
|
||||
|
||||
namespace SampleFramework
|
||||
{
|
||||
public class DirectX12Shader : IShader
|
||||
{
|
||||
public struct ConstantBufferStruct
|
||||
{
|
||||
public Matrix4X4<float> Projection;
|
||||
|
||||
public Vector4D<float> Color;
|
||||
|
||||
public Vector4D<float> TextureRect;
|
||||
}
|
||||
|
||||
public ComPtr<ID3D12PipelineState> PipelineState;
|
||||
|
||||
public ComPtr<ID3D12Resource> ConstantBuffer;
|
||||
|
||||
public ComPtr<ID3D12ShaderReflectionConstantBuffer> ConstantBufferView_;
|
||||
|
||||
private unsafe void* DataBegin;
|
||||
|
||||
private ConstantBufferStruct ConstantBufferStruct_ = new ConstantBufferStruct();
|
||||
|
||||
|
||||
internal unsafe DirectX12Shader(DirectX12Device device, string shaderSource)
|
||||
{
|
||||
CreateConstantBuffer(device);
|
||||
|
||||
DirectXShaderSource directXShaderSource = new DirectXShaderSource(device.D3dCompiler);
|
||||
|
||||
const int ElementsLength = 2;
|
||||
|
||||
var inputElementDescs = stackalloc InputElementDesc[ElementsLength]
|
||||
{
|
||||
new InputElementDesc()
|
||||
{
|
||||
SemanticName = (byte*)SilkMarshal.StringToMemory("POS"),
|
||||
SemanticIndex = 0,
|
||||
Format = Format.FormatR32G32B32Float,
|
||||
InputSlot = 0,
|
||||
AlignedByteOffset = 0,
|
||||
InputSlotClass = InputClassification.PerVertexData,
|
||||
InstanceDataStepRate = 0
|
||||
}
|
||||
,
|
||||
new InputElementDesc()
|
||||
{
|
||||
SemanticName = (byte*)SilkMarshal.StringToMemory("UVPOS"),
|
||||
SemanticIndex = 0,
|
||||
Format = Format.FormatR32G32Float,
|
||||
InputSlot = 0,
|
||||
AlignedByteOffset = sizeof(float) * 3,
|
||||
InputSlotClass = InputClassification.PerVertexData,
|
||||
InstanceDataStepRate = 0
|
||||
}
|
||||
};
|
||||
|
||||
GraphicsPipelineStateDesc graphicsPipelineStateDesc = new GraphicsPipelineStateDesc();
|
||||
|
||||
graphicsPipelineStateDesc.InputLayout = new InputLayoutDesc()
|
||||
{
|
||||
PInputElementDescs = inputElementDescs,
|
||||
NumElements = ElementsLength,
|
||||
};
|
||||
|
||||
graphicsPipelineStateDesc.PRootSignature = device.RootSignature;
|
||||
graphicsPipelineStateDesc.VS = new ShaderBytecode(directXShaderSource.VertexCode.GetBufferPointer(), directXShaderSource.VertexCode.GetBufferSize());
|
||||
graphicsPipelineStateDesc.PS = new ShaderBytecode(directXShaderSource.PixelCode.GetBufferPointer(), directXShaderSource.PixelCode.GetBufferSize());
|
||||
|
||||
RasterizerDesc rasterizerDesc = new RasterizerDesc()
|
||||
{
|
||||
FillMode = FillMode.Solid,
|
||||
CullMode = CullMode.Back,
|
||||
FrontCounterClockwise = 0,
|
||||
DepthBias = D3D12.DefaultDepthBias,
|
||||
DepthBiasClamp = 0,
|
||||
SlopeScaledDepthBias = 0,
|
||||
DepthClipEnable = 1,
|
||||
MultisampleEnable = 0,
|
||||
AntialiasedLineEnable = 0,
|
||||
ForcedSampleCount = 0,
|
||||
ConservativeRaster = ConservativeRasterizationMode.Off
|
||||
};
|
||||
graphicsPipelineStateDesc.RasterizerState = rasterizerDesc;
|
||||
|
||||
|
||||
var defaultRenderTargetBlend = new RenderTargetBlendDesc()
|
||||
{
|
||||
BlendEnable = 0,
|
||||
LogicOpEnable = 0,
|
||||
SrcBlend = Blend.One,
|
||||
DestBlend = Blend.Zero,
|
||||
BlendOp = BlendOp.Add,
|
||||
SrcBlendAlpha = Blend.One,
|
||||
DestBlendAlpha = Blend.Zero,
|
||||
BlendOpAlpha = BlendOp.Add,
|
||||
LogicOp = LogicOp.Noop,
|
||||
RenderTargetWriteMask = (byte)ColorWriteEnable.All
|
||||
};
|
||||
BlendDesc blendDesc = new BlendDesc()
|
||||
{
|
||||
AlphaToCoverageEnable = 0,
|
||||
IndependentBlendEnable = 0,
|
||||
RenderTarget = new BlendDesc.RenderTargetBuffer()
|
||||
{
|
||||
[0] = defaultRenderTargetBlend,
|
||||
[1] = defaultRenderTargetBlend,
|
||||
[2] = defaultRenderTargetBlend,
|
||||
[3] = defaultRenderTargetBlend,
|
||||
[4] = defaultRenderTargetBlend,
|
||||
[5] = defaultRenderTargetBlend,
|
||||
[6] = defaultRenderTargetBlend,
|
||||
[7] = defaultRenderTargetBlend
|
||||
}
|
||||
};
|
||||
|
||||
var defaultStencilOp = new DepthStencilopDesc
|
||||
{
|
||||
StencilFailOp = StencilOp.Keep,
|
||||
StencilDepthFailOp = StencilOp.Keep,
|
||||
StencilPassOp = StencilOp.Keep,
|
||||
StencilFunc = ComparisonFunc.Always
|
||||
};
|
||||
|
||||
graphicsPipelineStateDesc.BlendState = blendDesc;
|
||||
|
||||
|
||||
graphicsPipelineStateDesc.DepthStencilState = new ()
|
||||
{
|
||||
DepthEnable = 1,
|
||||
DepthWriteMask = DepthWriteMask.All,
|
||||
DepthFunc = ComparisonFunc.Less,
|
||||
StencilEnable = 0,
|
||||
StencilReadMask = D3D12.DefaultStencilReadMask,
|
||||
StencilWriteMask = D3D12.DefaultStencilWriteMask,
|
||||
FrontFace = defaultStencilOp,
|
||||
BackFace = defaultStencilOp
|
||||
};
|
||||
graphicsPipelineStateDesc.SampleMask = uint.MaxValue;
|
||||
graphicsPipelineStateDesc.PrimitiveTopologyType = PrimitiveTopologyType.Triangle;
|
||||
graphicsPipelineStateDesc.NumRenderTargets = 1;
|
||||
graphicsPipelineStateDesc.RTVFormats[0] = Format.FormatR8G8B8A8Unorm;
|
||||
graphicsPipelineStateDesc.DSVFormat = Format.FormatUnknown;
|
||||
graphicsPipelineStateDesc.SampleDesc.Count = 1;
|
||||
graphicsPipelineStateDesc.SampleDesc.Quality = 0;
|
||||
graphicsPipelineStateDesc.DepthStencilState.DepthEnable = 0;
|
||||
|
||||
ID3D12PipelineState* pipelineState;
|
||||
var iid = ID3D12PipelineState.Guid;
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
device.Device.CreateGraphicsPipelineState(graphicsPipelineStateDesc, &iid, (void**)&pipelineState)
|
||||
);
|
||||
|
||||
directXShaderSource.Dispose();
|
||||
|
||||
PipelineState = pipelineState;
|
||||
|
||||
}
|
||||
|
||||
private unsafe void CreateConstantBuffer(DirectX12Device device)
|
||||
{
|
||||
long size = (sizeof(ConstantBufferStruct) + 0xff) & ~0xff;
|
||||
HeapProperties heapProperties = new HeapProperties()
|
||||
{
|
||||
Type = HeapType.Upload,
|
||||
CPUPageProperty = CpuPageProperty.Unknown,
|
||||
MemoryPoolPreference = MemoryPool.Unknown,
|
||||
CreationNodeMask = 1,
|
||||
VisibleNodeMask = 1
|
||||
};
|
||||
|
||||
ResourceDesc resourceDesc = new ResourceDesc()
|
||||
{
|
||||
Dimension = ResourceDimension.Buffer,
|
||||
Alignment = 0,
|
||||
Width = (ulong)size,
|
||||
Height = 1,
|
||||
DepthOrArraySize = 1,
|
||||
MipLevels = 1,
|
||||
Format = Format.FormatUnknown,
|
||||
SampleDesc = new SampleDesc()
|
||||
{
|
||||
Count = 1,
|
||||
Quality = 0,
|
||||
},
|
||||
Layout = TextureLayout.LayoutRowMajor,
|
||||
Flags = ResourceFlags.None
|
||||
};
|
||||
|
||||
void* constantBuffer;
|
||||
var riid = ID3D12Resource.Guid;
|
||||
SilkMarshal.ThrowHResult
|
||||
(
|
||||
device.Device.CreateCommittedResource(heapProperties, HeapFlags.None, resourceDesc, ResourceStates.GenericRead, (ClearValue*)0, &riid, &constantBuffer)
|
||||
);
|
||||
ConstantBuffer = (ID3D12Resource*)constantBuffer;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
CpuDescriptorHandle rtvHandle = new CpuDescriptorHandle();
|
||||
rtvHandle.Ptr = device.CBVHeap.GetCPUDescriptorHandleForHeapStart().Ptr;
|
||||
|
||||
ConstantBufferViewDesc constantBufferViewDesc = new()
|
||||
{
|
||||
SizeInBytes = (uint)ConstantBuffer.GetDesc().Width,
|
||||
BufferLocation = ConstantBuffer.GetGPUVirtualAddress(),
|
||||
};
|
||||
|
||||
device.Device.CreateConstantBufferView(constantBufferViewDesc, rtvHandle);
|
||||
|
||||
|
||||
SilkMarshal.ThrowHResult(device.Device.GetDeviceRemovedReason());
|
||||
|
||||
|
||||
Silk.NET.Direct3D12.Range range = new Silk.NET.Direct3D12.Range();
|
||||
void* dataBegin;
|
||||
SilkMarshal.ThrowHResult(ConstantBuffer.Map(0, &range, &dataBegin));
|
||||
DataBegin = dataBegin;
|
||||
|
||||
Update(device);
|
||||
}
|
||||
|
||||
public unsafe void SetMVP(Matrix4X4<float> mvp)
|
||||
{
|
||||
ConstantBufferStruct_.Projection = mvp;
|
||||
}
|
||||
|
||||
public void SetColor(Vector4D<float> color)
|
||||
{
|
||||
ConstantBufferStruct_.Color = color;
|
||||
}
|
||||
|
||||
public void SetTextureRect(Vector4D<float> rect)
|
||||
{
|
||||
ConstantBufferStruct_.TextureRect = rect;
|
||||
}
|
||||
|
||||
public void SetCamera(Matrix4X4<float> camera)
|
||||
{
|
||||
}
|
||||
|
||||
internal unsafe void Update(DirectX12Device device)
|
||||
{
|
||||
CpuDescriptorHandle rtvHandle = new CpuDescriptorHandle();
|
||||
rtvHandle.Ptr = device.CBVHeap.GetCPUDescriptorHandleForHeapStart().Ptr;
|
||||
|
||||
device.CommandList[device.FrameBufferIndex].SetDescriptorHeaps(1, device.CBVHeap);
|
||||
device.CommandList[device.FrameBufferIndex].SetGraphicsRootDescriptorTable(0, device.CBVHeap.GetGPUDescriptorHandleForHeapStart());
|
||||
|
||||
fixed(void* data = &ConstantBufferStruct_)
|
||||
{
|
||||
Unsafe.CopyBlock(DataBegin, data, (uint)sizeof(ConstantBufferStruct ));
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
PipelineState.Dispose();
|
||||
ConstantBuffer.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,42 +0,0 @@
|
||||
using Silk.NET.Windowing;
|
||||
using Silk.NET.Direct3D12;
|
||||
using Silk.NET.DXGI;
|
||||
using SkiaSharp;
|
||||
using Silk.NET.Core.Native;
|
||||
|
||||
namespace SampleFramework
|
||||
{
|
||||
public class DirectX12Texture : ITexture
|
||||
{
|
||||
internal bool IsWrongPixels;
|
||||
internal ComPtr<ID3D12Resource> Texture;
|
||||
internal ComPtr<ID3D12Resource> ConstantBuffer;
|
||||
|
||||
private Format RgbaTypeToFormat(RgbaType rgbaType)
|
||||
{
|
||||
switch(rgbaType)
|
||||
{
|
||||
case RgbaType.Rgba:
|
||||
return Format.FormatR8G8B8A8Unorm;
|
||||
case RgbaType.Bgra:
|
||||
return Format.FormatB8G8R8A8Unorm;
|
||||
default:
|
||||
return Format.FormatR8G8B8A8Unorm;
|
||||
}
|
||||
}
|
||||
|
||||
public unsafe DirectX12Texture(void* data, int width, int height, RgbaType rgbaType)
|
||||
{
|
||||
|
||||
if (data == null || width <= 0 || height <= 0)
|
||||
{
|
||||
IsWrongPixels = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Texture.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,112 +0,0 @@
|
||||
using Silk.NET.Windowing;
|
||||
using Silk.NET.Maths;
|
||||
using Silk.NET.Direct3D9;
|
||||
using Silk.NET.Core.Native;
|
||||
using SkiaSharp;
|
||||
|
||||
namespace SampleFramework
|
||||
{
|
||||
unsafe class DirectX9Device : IGraphicsDevice
|
||||
{
|
||||
private D3D9 D3d9;
|
||||
|
||||
private ComPtr<IDirect3D9> DX9;
|
||||
|
||||
private ComPtr<IDirect3DDevice9> Device;
|
||||
|
||||
public DirectX9Device(IWindow window)
|
||||
{
|
||||
D3d9 = D3D9.GetApi(window, false);
|
||||
|
||||
DX9 = D3d9.Direct3DCreate9(D3D9.SdkVersion);
|
||||
|
||||
PresentParameters presentParameters = new PresentParameters()
|
||||
{
|
||||
Windowed = true,
|
||||
SwapEffect = Swapeffect.Discard,
|
||||
HDeviceWindow = window.Native.DXHandle.Value
|
||||
};
|
||||
|
||||
DX9.CreateDevice(D3D9.AdapterDefault, Devtype.Hal, window.Native.DXHandle.Value, D3D9.CreateHardwareVertexprocessing, ref presentParameters, ref Device);
|
||||
}
|
||||
|
||||
public void SetClearColor(float r, float g, float b, float a)
|
||||
{
|
||||
}
|
||||
|
||||
public void SetViewPort(int x, int y, uint width, uint height)
|
||||
{
|
||||
Viewport9 viewport = new Viewport9()
|
||||
{
|
||||
X = (uint)x,
|
||||
Y = (uint)y,
|
||||
Width = width,
|
||||
Height = height,
|
||||
};
|
||||
Device.SetViewport(&viewport);
|
||||
}
|
||||
|
||||
public void SetFrameBuffer(uint width, uint height)
|
||||
{
|
||||
}
|
||||
|
||||
public void ClearBuffer()
|
||||
{
|
||||
Rect rect = new Rect()
|
||||
{
|
||||
X1 = 0,
|
||||
Y1 = 0,
|
||||
X2 = 1920,
|
||||
Y2 = 1080
|
||||
};
|
||||
Device.Clear(0,
|
||||
rect,
|
||||
D3D9.ClearTarget,
|
||||
0,
|
||||
0.0f,
|
||||
0);
|
||||
}
|
||||
|
||||
public void SwapBuffer()
|
||||
{
|
||||
RGNData rGNData = default;
|
||||
Device.Present(null, null, 0, rGNData);
|
||||
}
|
||||
|
||||
|
||||
public IPolygon GenPolygon(float[] vertices, uint[] indices, float[] uvs)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public IShader GenShader(string name)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public unsafe ITexture GenTexture(void* data, int width, int height, RgbaType rgbaType)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public void DrawPolygon(IPolygon polygon, IShader shader, ITexture texture, BlendType blendType)
|
||||
{
|
||||
}
|
||||
|
||||
public unsafe SKBitmap GetScreenPixels()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public unsafe void GetScreenPixelsASync(Action<SKBitmap> action)
|
||||
{
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Device.Dispose();
|
||||
DX9.Dispose();
|
||||
D3d9.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,127 +0,0 @@
|
||||
using System;
|
||||
using Silk.NET.Windowing;
|
||||
using Silk.NET.Maths;
|
||||
using Silk.NET.Core.Native;
|
||||
using Silk.NET.Direct3D.Compilers;
|
||||
using Silk.NET.DXGI;
|
||||
|
||||
namespace SampleFramework
|
||||
{
|
||||
unsafe class DirectXShaderSource : IDisposable
|
||||
{
|
||||
const string shaderSource = @"
|
||||
|
||||
Texture2D g_texture : register(t0);
|
||||
SamplerState g_sampler : register(s0);
|
||||
|
||||
cbuffer ConstantBuffer : register(b0)
|
||||
{
|
||||
float4x4 Projection;
|
||||
float4 Color;
|
||||
float4 TextureRect;
|
||||
}
|
||||
|
||||
struct vs_in {
|
||||
float3 position_local : POS;
|
||||
float2 uvposition_local : UVPOS;
|
||||
};
|
||||
|
||||
struct vs_out {
|
||||
float4 position_clip : SV_POSITION;
|
||||
float2 uvposition_clip : TEXCOORD0;
|
||||
};
|
||||
|
||||
vs_out vs_main(vs_in input) {
|
||||
vs_out output = (vs_out)0;
|
||||
|
||||
float4 position = float4(input.position_local, 1.0);
|
||||
|
||||
output.position_clip = position;
|
||||
|
||||
output.uvposition_clip = input.uvposition_local;
|
||||
return output;
|
||||
}
|
||||
|
||||
float4 ps_main(vs_out input) : SV_TARGET {
|
||||
float4 totalcolor = float4( input.uvposition_clip.x, input.uvposition_clip.y, 0.0, 1.0 );
|
||||
|
||||
return totalcolor;
|
||||
}
|
||||
";
|
||||
|
||||
public ComPtr<ID3D10Blob> VertexCode = default;
|
||||
|
||||
public ComPtr<ID3D10Blob> PixelCode = default;
|
||||
|
||||
public DirectXShaderSource(D3DCompiler d3dCompiler)
|
||||
{
|
||||
var shaderBytes = System.Text.Encoding.ASCII.GetBytes(shaderSource);
|
||||
ComPtr<ID3D10Blob> vertexErrors = default;
|
||||
|
||||
// Compile vertex shader.
|
||||
HResult hr = d3dCompiler.Compile
|
||||
(
|
||||
in shaderBytes[0],
|
||||
(uint)shaderBytes.Length,
|
||||
nameof(shaderSource),
|
||||
null,
|
||||
default(ComPtr<ID3DInclude>),
|
||||
"vs_main",
|
||||
"vs_5_0",
|
||||
0,
|
||||
0,
|
||||
ref VertexCode,
|
||||
ref vertexErrors
|
||||
);
|
||||
|
||||
// Check for compilation errors.
|
||||
if (hr.IsFailure)
|
||||
{
|
||||
if (vertexErrors.Handle != null)
|
||||
{
|
||||
Console.WriteLine(SilkMarshal.PtrToString((int)vertexErrors.GetBufferPointer(), NativeStringEncoding.LPWStr));
|
||||
}
|
||||
|
||||
hr.Throw();
|
||||
}
|
||||
|
||||
// Compile pixel shader.
|
||||
ComPtr<ID3D10Blob> pixelErrors = default;
|
||||
hr = d3dCompiler.Compile
|
||||
(
|
||||
in shaderBytes[0],
|
||||
(uint)shaderBytes.Length,
|
||||
nameof(shaderSource),
|
||||
null,
|
||||
default(ComPtr<ID3DInclude>),
|
||||
"ps_main",
|
||||
"ps_5_0",
|
||||
0,
|
||||
0,
|
||||
ref PixelCode,
|
||||
ref pixelErrors
|
||||
);
|
||||
|
||||
// Check for compilation errors.
|
||||
if (hr.IsFailure)
|
||||
{
|
||||
if (pixelErrors.Handle != null)
|
||||
{
|
||||
Console.WriteLine(SilkMarshal.PtrToString((int)pixelErrors.GetBufferPointer(), NativeStringEncoding.LPWStr));
|
||||
}
|
||||
|
||||
hr.Throw();
|
||||
}
|
||||
|
||||
// Clean up any resources.
|
||||
vertexErrors.Dispose();
|
||||
pixelErrors.Dispose();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
VertexCode.Dispose();
|
||||
PixelCode.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
using System;
|
||||
using Silk.NET.Windowing;
|
||||
using Silk.NET.Maths;
|
||||
using SkiaSharp;
|
||||
|
||||
namespace SampleFramework
|
||||
{
|
||||
public enum BlendType
|
||||
{
|
||||
Normal,
|
||||
Add,
|
||||
Multi,
|
||||
Sub,
|
||||
Screen
|
||||
}
|
||||
public interface IGraphicsDevice : IDisposable
|
||||
{
|
||||
void SetClearColor(float r, float g, float b, float a);
|
||||
|
||||
void SetViewPort(int x, int y, uint width, uint height);
|
||||
|
||||
void SetFrameBuffer(uint width, uint height);
|
||||
|
||||
void ClearBuffer();
|
||||
|
||||
void SwapBuffer();
|
||||
|
||||
IPolygon GenPolygon(float[] vertices, uint[] indices, float[] uvs);
|
||||
|
||||
IShader GenShader(string name);
|
||||
|
||||
unsafe ITexture GenTexture(void* data, int width, int height, RgbaType rgbaType);
|
||||
|
||||
void DrawPolygon(IPolygon polygon, IShader shader, ITexture texture, BlendType blendType);
|
||||
|
||||
unsafe SKBitmap GetScreenPixels();
|
||||
|
||||
unsafe void GetScreenPixelsASync(Action<SKBitmap> action);
|
||||
}
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
|
||||
namespace SampleFramework
|
||||
{
|
||||
public interface IPolygon : IDisposable
|
||||
{
|
||||
uint IndiceCount { get; }
|
||||
}
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
using Silk.NET.Maths;
|
||||
|
||||
namespace SampleFramework
|
||||
{
|
||||
public interface IShader : IDisposable
|
||||
{
|
||||
void SetMVP(Matrix4X4<float> mvp);
|
||||
void SetColor(Vector4D<float> color);
|
||||
void SetTextureRect(Vector4D<float> rect);
|
||||
void SetCamera(Matrix4X4<float> camera);
|
||||
}
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
|
||||
namespace SampleFramework
|
||||
{
|
||||
public enum RgbaType
|
||||
{
|
||||
Rgba,
|
||||
Bgra,
|
||||
}
|
||||
|
||||
public interface ITexture : IDisposable
|
||||
{
|
||||
|
||||
}
|
||||
}
|
@ -1,186 +0,0 @@
|
||||
using Silk.NET.Windowing;
|
||||
using Silk.NET.Maths;
|
||||
using Silk.NET.OpenGL;
|
||||
using SkiaSharp;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace SampleFramework
|
||||
{
|
||||
class OpenGLDevice : IGraphicsDevice
|
||||
{
|
||||
public static GL Gl;
|
||||
|
||||
private int ViewportWidth;
|
||||
|
||||
private int ViewportHeight;
|
||||
|
||||
internal static List<Action> AsyncActions = new();
|
||||
|
||||
public OpenGLDevice(IWindow window)
|
||||
{
|
||||
Gl = window.CreateOpenGL();
|
||||
Gl.Enable(GLEnum.Texture2D);
|
||||
Gl.Enable(GLEnum.Blend);
|
||||
}
|
||||
|
||||
public void SetClearColor(float r, float g, float b, float a)
|
||||
{
|
||||
Gl.ClearColor(r, g, b, a);
|
||||
}
|
||||
|
||||
public void SetViewPort(int x, int y, uint width, uint height)
|
||||
{
|
||||
ViewportWidth = (int)width;
|
||||
ViewportHeight = (int)height;
|
||||
Gl.Viewport(x, y, width, height);
|
||||
}
|
||||
|
||||
public void SetFrameBuffer(uint width, uint height)
|
||||
{
|
||||
}
|
||||
|
||||
public void ClearBuffer()
|
||||
{
|
||||
if (AsyncActions.Count > 0)
|
||||
{
|
||||
AsyncActions[0]?.Invoke();
|
||||
AsyncActions.Remove(AsyncActions[0]);
|
||||
}
|
||||
Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
|
||||
}
|
||||
|
||||
public void SwapBuffer()
|
||||
{
|
||||
}
|
||||
|
||||
public IPolygon GenPolygon(float[] vertices, uint[] indices, float[] uvs)
|
||||
{
|
||||
for(int i = 0; i < vertices.Length; i++)
|
||||
{
|
||||
if (i % 3 == 1)
|
||||
{
|
||||
vertices[i] = -vertices[i];
|
||||
}
|
||||
}
|
||||
return new OpenGLPolygon(vertices, indices, uvs);
|
||||
}
|
||||
|
||||
public IShader GenShader(string name)
|
||||
{
|
||||
using StreamReader vert = new StreamReader(@$"{name}.glsl.vert");
|
||||
using StreamReader frag = new StreamReader(@$"{name}.glsl.frag");
|
||||
return new OpenGLShader(vert.ReadToEnd(), frag.ReadToEnd());
|
||||
}
|
||||
|
||||
public unsafe ITexture GenTexture(void* data, int width, int height, RgbaType rgbaType)
|
||||
{
|
||||
return new OpenGLTexture(data, width, height, rgbaType);
|
||||
}
|
||||
|
||||
public void DrawPolygon(IPolygon polygon, IShader shader, ITexture texture, BlendType blendType)
|
||||
{
|
||||
OpenGLPolygon glPolygon = (OpenGLPolygon)polygon;
|
||||
OpenGLShader glShader = (OpenGLShader)shader;
|
||||
OpenGLTexture glTexture = (OpenGLTexture)texture;
|
||||
|
||||
if (glTexture == null) return;
|
||||
|
||||
Gl.BindTexture(TextureTarget.Texture2D, glTexture.TextureHandle);
|
||||
Gl.BindVertexArray(glPolygon.VAO);
|
||||
|
||||
switch(blendType)
|
||||
{
|
||||
case BlendType.Normal:
|
||||
Gl.BlendEquation(BlendEquationModeEXT.FuncAdd);
|
||||
Gl.BlendFunc(GLEnum.SrcAlpha, GLEnum.OneMinusSrcAlpha);
|
||||
break;
|
||||
case BlendType.Add:
|
||||
Gl.BlendEquation(BlendEquationModeEXT.FuncAdd);
|
||||
Gl.BlendFunc(GLEnum.SrcAlpha, GLEnum.One);
|
||||
break;
|
||||
case BlendType.Screen:
|
||||
Gl.BlendEquation(BlendEquationModeEXT.FuncAdd);
|
||||
Gl.BlendFunc(GLEnum.OneMinusDstColor, GLEnum.One);
|
||||
break;
|
||||
case BlendType.Multi:
|
||||
Gl.BlendEquation(BlendEquationModeEXT.FuncAdd);
|
||||
Gl.BlendFunc(GLEnum.Zero, GLEnum.SrcColor);
|
||||
break;
|
||||
case BlendType.Sub:
|
||||
Gl.BlendEquation(BlendEquationModeEXT.FuncReverseSubtract);
|
||||
Gl.BlendFunc(GLEnum.SrcAlpha, GLEnum.One);
|
||||
break;
|
||||
}
|
||||
|
||||
unsafe
|
||||
{
|
||||
Gl.UseProgram(glShader.ShaderProgram);
|
||||
Gl.DrawElements(PrimitiveType.Triangles, glPolygon.IndiceCount, DrawElementsType.UnsignedInt, (void*)0);
|
||||
}
|
||||
}
|
||||
|
||||
public unsafe SKBitmap GetScreenPixels()
|
||||
{
|
||||
fixed(uint* pixels = new uint[(uint)ViewportWidth * (uint)ViewportHeight])
|
||||
{
|
||||
Gl.ReadBuffer(GLEnum.Front);
|
||||
Gl.ReadPixels(0, 0, (uint)ViewportWidth, (uint)ViewportHeight, GLEnum.Bgra, GLEnum.UnsignedByte, pixels);
|
||||
|
||||
fixed(uint* pixels2 = new uint[(uint)ViewportWidth * (uint)ViewportHeight])
|
||||
{
|
||||
for(int x = 0; x < ViewportWidth; x++)
|
||||
{
|
||||
for(int y = 1; y < ViewportHeight; y++)
|
||||
{
|
||||
int pos = x + ((y - 1) * ViewportWidth);
|
||||
int pos2 = x + ((ViewportHeight - y) * ViewportWidth);
|
||||
var p = pixels[pos2];
|
||||
pixels2[pos] = p;
|
||||
}
|
||||
}
|
||||
|
||||
using SKBitmap sKBitmap = new(ViewportWidth, ViewportHeight - 1);
|
||||
sKBitmap.SetPixels((IntPtr)pixels2);
|
||||
return sKBitmap.Copy();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public unsafe void GetScreenPixelsASync(Action<SKBitmap> action)
|
||||
{
|
||||
byte[] pixels = new byte[(uint)ViewportWidth * (uint)ViewportHeight * 4];
|
||||
Gl.ReadBuffer(GLEnum.Front);
|
||||
fixed(byte* pix = pixels)
|
||||
{
|
||||
Gl.ReadPixels(0, 0, (uint)ViewportWidth, (uint)ViewportHeight, GLEnum.Bgra, GLEnum.UnsignedByte, pix);
|
||||
}
|
||||
|
||||
Task.Run(() =>{
|
||||
fixed(byte* pixels2 = new byte[(uint)ViewportWidth * (uint)ViewportHeight * 4])
|
||||
{
|
||||
for(int x = 0; x < ViewportWidth; x++)
|
||||
{
|
||||
for(int y = 1; y < ViewportHeight; y++)
|
||||
{
|
||||
int pos = x + ((y - 1) * ViewportWidth);
|
||||
int pos2 = x + ((ViewportHeight - y) * ViewportWidth);
|
||||
pixels2[(pos * 4) + 0] = pixels[(pos2 * 4) + 0];
|
||||
pixels2[(pos * 4) + 1] = pixels[(pos2 * 4) + 1];
|
||||
pixels2[(pos * 4) + 2] = pixels[(pos2 * 4) + 2];
|
||||
pixels2[(pos * 4) + 3] = 255;
|
||||
}
|
||||
}
|
||||
|
||||
using SKBitmap sKBitmap = new(ViewportWidth, ViewportHeight - 1);
|
||||
sKBitmap.SetPixels((IntPtr)pixels2);
|
||||
action(sKBitmap);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -1,70 +0,0 @@
|
||||
using Silk.NET.Windowing;
|
||||
using Silk.NET.OpenGL;
|
||||
using SkiaSharp;
|
||||
|
||||
namespace SampleFramework
|
||||
{
|
||||
public class OpenGLPolygon : IPolygon
|
||||
{
|
||||
public uint IndiceCount { get; set; }
|
||||
|
||||
internal uint VAO;
|
||||
internal uint VBO;
|
||||
internal uint EBO;
|
||||
internal uint UVBO;
|
||||
|
||||
public unsafe OpenGLPolygon(float[] vertices, uint[] indices, float[] uvs)
|
||||
{
|
||||
VAO = OpenGLDevice.Gl.GenVertexArray();
|
||||
OpenGLDevice.Gl.BindVertexArray(VAO);
|
||||
|
||||
|
||||
VBO = OpenGLDevice.Gl.GenBuffer();
|
||||
OpenGLDevice.Gl.BindBuffer(BufferTargetARB.ArrayBuffer, VBO);
|
||||
fixed(float* v = vertices)
|
||||
{
|
||||
OpenGLDevice.Gl.BufferData(BufferTargetARB.ArrayBuffer, (nuint)(sizeof(float) * vertices.Length), v, BufferUsageARB.StaticDraw);
|
||||
}
|
||||
|
||||
|
||||
EBO = OpenGLDevice.Gl.GenBuffer();
|
||||
OpenGLDevice.Gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, EBO);
|
||||
|
||||
fixed(uint* e = indices)
|
||||
{
|
||||
IndiceCount = (uint)indices.Length;
|
||||
OpenGLDevice.Gl.BufferData(BufferTargetARB.ElementArrayBuffer, (nuint)(sizeof(uint) * indices.Length), e, BufferUsageARB.StaticDraw);
|
||||
}
|
||||
|
||||
OpenGLDevice.Gl.EnableVertexAttribArray(0);
|
||||
OpenGLDevice.Gl.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 3 * sizeof(float), null);
|
||||
|
||||
|
||||
UVBO = OpenGLDevice.Gl.GenBuffer();
|
||||
OpenGLDevice.Gl.BindBuffer(BufferTargetARB.ArrayBuffer, UVBO);
|
||||
|
||||
fixed(float* tc = uvs)
|
||||
{
|
||||
OpenGLDevice.Gl.BufferData(BufferTargetARB.ArrayBuffer, (nuint)(sizeof(float) * uvs.Length), tc, BufferUsageARB.StaticDraw);
|
||||
}
|
||||
|
||||
OpenGLDevice.Gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, EBO);
|
||||
|
||||
OpenGLDevice.Gl.EnableVertexAttribArray(1);
|
||||
OpenGLDevice.Gl.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, false, 2 * sizeof(float), null);
|
||||
|
||||
|
||||
OpenGLDevice.Gl.BindVertexArray(0);
|
||||
OpenGLDevice.Gl.BindBuffer(BufferTargetARB.ArrayBuffer, 0);
|
||||
OpenGLDevice.Gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, 0);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
OpenGLDevice.Gl.DeleteVertexArray(VAO);
|
||||
OpenGLDevice.Gl.DeleteBuffer(VBO);
|
||||
OpenGLDevice.Gl.DeleteBuffer(EBO);
|
||||
OpenGLDevice.Gl.DeleteBuffer(UVBO);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,92 +0,0 @@
|
||||
using System;
|
||||
using Silk.NET.Windowing;
|
||||
using Silk.NET.OpenGL;
|
||||
using Silk.NET.Maths;
|
||||
using SkiaSharp;
|
||||
using System.Numerics;
|
||||
|
||||
namespace SampleFramework
|
||||
{
|
||||
public class OpenGLShader : IShader
|
||||
{
|
||||
internal uint ShaderProgram;
|
||||
|
||||
private int MVPID;
|
||||
|
||||
private int ColorID;
|
||||
|
||||
private int TextureRectID;
|
||||
|
||||
private int CameraID;
|
||||
|
||||
public OpenGLShader(string vertexCode, string fragmentCode)
|
||||
{
|
||||
uint vertexShader = OpenGLDevice.Gl.CreateShader(ShaderType.VertexShader);
|
||||
OpenGLDevice.Gl.ShaderSource(vertexShader, vertexCode);
|
||||
OpenGLDevice.Gl.CompileShader(vertexShader);
|
||||
OpenGLDevice.Gl.GetShader(vertexShader, ShaderParameterName.CompileStatus, out int vertexStatus);
|
||||
if (vertexStatus != (int)GLEnum.True)
|
||||
{
|
||||
throw new Exception("Vertex shader failed to compile: " + OpenGLDevice.Gl.GetShaderInfoLog(vertexShader));
|
||||
}
|
||||
|
||||
uint fragmentShader = OpenGLDevice.Gl.CreateShader(ShaderType.FragmentShader);
|
||||
OpenGLDevice.Gl.ShaderSource(fragmentShader, fragmentCode);
|
||||
OpenGLDevice.Gl.CompileShader(fragmentShader);
|
||||
OpenGLDevice.Gl.GetShader(fragmentShader, ShaderParameterName.CompileStatus, out int fragmentStatus);
|
||||
if (fragmentStatus != (int)GLEnum.True)
|
||||
{
|
||||
throw new Exception("Fragment shader failed to compile: " + OpenGLDevice.Gl.GetShaderInfoLog(fragmentShader));
|
||||
}
|
||||
|
||||
ShaderProgram = OpenGLDevice.Gl.CreateProgram();
|
||||
OpenGLDevice.Gl.AttachShader(ShaderProgram, vertexShader);
|
||||
OpenGLDevice.Gl.AttachShader(ShaderProgram, fragmentShader);
|
||||
|
||||
OpenGLDevice.Gl.LinkProgram(ShaderProgram);
|
||||
OpenGLDevice.Gl.GetProgram(ShaderProgram, ProgramPropertyARB.LinkStatus, out int lStatus);
|
||||
if (lStatus != (int)GLEnum.True)
|
||||
{
|
||||
throw new Exception("Fragment shader failed to compile: " + OpenGLDevice.Gl.GetProgramInfoLog(ShaderProgram));
|
||||
}
|
||||
|
||||
OpenGLDevice.Gl.DetachShader(ShaderProgram, vertexShader);
|
||||
OpenGLDevice.Gl.DetachShader(ShaderProgram, fragmentShader);
|
||||
OpenGLDevice.Gl.DeleteShader(vertexShader);
|
||||
OpenGLDevice.Gl.DeleteShader(fragmentShader);
|
||||
|
||||
|
||||
MVPID = OpenGLDevice.Gl.GetUniformLocation(ShaderProgram, "mvp");
|
||||
ColorID = OpenGLDevice.Gl.GetUniformLocation(ShaderProgram, "color");
|
||||
TextureRectID = OpenGLDevice.Gl.GetUniformLocation(ShaderProgram, "textureRect");
|
||||
CameraID = OpenGLDevice.Gl.GetUniformLocation(ShaderProgram, "camera");
|
||||
}
|
||||
|
||||
public unsafe void SetMVP(Matrix4X4<float> mvp)
|
||||
{
|
||||
OpenGLDevice.Gl.UniformMatrix4(MVPID, 1, false, (float*)&mvp);
|
||||
}
|
||||
|
||||
public unsafe void SetColor(Vector4D<float> color)
|
||||
{
|
||||
System.Numerics.Vector4 vector4 = new(color.X, color.Y, color.Z, color.W);
|
||||
OpenGLDevice.Gl.Uniform4(ColorID, ref vector4);
|
||||
}
|
||||
|
||||
public unsafe void SetTextureRect(Vector4D<float> rect)
|
||||
{
|
||||
System.Numerics.Vector4 vector4 = new(rect.X, rect.Y, rect.Z, rect.W);
|
||||
OpenGLDevice.Gl.Uniform4(TextureRectID, ref vector4);
|
||||
}
|
||||
|
||||
public unsafe void SetCamera(Matrix4X4<float> camera)
|
||||
{
|
||||
OpenGLDevice.Gl.UniformMatrix4(CameraID, 1, false, (float*)&camera);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
OpenGLDevice.Gl.DeleteProgram(ShaderProgram);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,51 +0,0 @@
|
||||
using Silk.NET.Windowing;
|
||||
using Silk.NET.OpenGL;
|
||||
using SkiaSharp;
|
||||
|
||||
namespace SampleFramework
|
||||
{
|
||||
public class OpenGLTexture : ITexture
|
||||
{
|
||||
internal uint TextureHandle;
|
||||
|
||||
public unsafe OpenGLTexture(void* data, int width, int height, RgbaType rgbaType)
|
||||
{
|
||||
void load()
|
||||
{
|
||||
TextureHandle = OpenGLDevice.Gl.GenTexture();
|
||||
OpenGLDevice.Gl.BindTexture(TextureTarget.Texture2D, TextureHandle);
|
||||
|
||||
switch(rgbaType)
|
||||
{
|
||||
case RgbaType.Rgba:
|
||||
OpenGLDevice.Gl.TexImage2D(GLEnum.Texture2D, 0, (int)InternalFormat.Rgba8, (uint)width, (uint)height, 0, GLEnum.Rgba, GLEnum.UnsignedByte, data);
|
||||
break;
|
||||
case RgbaType.Bgra:
|
||||
OpenGLDevice.Gl.TexImage2D(GLEnum.Texture2D, 0, (int)InternalFormat.Rgba8, (uint)width, (uint)height, 0, GLEnum.Bgra, GLEnum.UnsignedByte, data);
|
||||
break;
|
||||
}
|
||||
|
||||
OpenGLDevice.Gl.TexParameterI(GLEnum.Texture2D, GLEnum.TextureMinFilter, (int)TextureMinFilter.Nearest);
|
||||
OpenGLDevice.Gl.TexParameterI(GLEnum.Texture2D, GLEnum.TextureMagFilter, (int)TextureMagFilter.Nearest);
|
||||
}
|
||||
|
||||
if (Game.MainThreadID != Thread.CurrentThread.ManagedThreadId)
|
||||
{
|
||||
OpenGLDevice.AsyncActions.Add(load);
|
||||
while(OpenGLDevice.AsyncActions.Contains(load))
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
load();
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
OpenGLDevice.Gl.DeleteTexture(TextureHandle);
|
||||
}
|
||||
}
|
||||
}
|
55
FDK/src/01.Framework/Rendering/ShaderHelper.cs
Normal file
55
FDK/src/01.Framework/Rendering/ShaderHelper.cs
Normal file
@ -0,0 +1,55 @@
|
||||
using Silk.NET.OpenGLES;
|
||||
|
||||
namespace SampleFramework;
|
||||
|
||||
public static class ShaderHelper
|
||||
{
|
||||
public static uint CreateShader(string code, ShaderType shaderType)
|
||||
{
|
||||
uint vertexShader = Game.Gl.CreateShader(shaderType);
|
||||
|
||||
Game.Gl.ShaderSource(vertexShader, code);
|
||||
|
||||
Game.Gl.CompileShader(vertexShader);
|
||||
|
||||
Game.Gl.GetShader(vertexShader, ShaderParameterName.CompileStatus, out int status);
|
||||
|
||||
if (status != (int)GLEnum.True)
|
||||
throw new Exception($"{shaderType} failed to compile:{Game.Gl.GetShaderInfoLog(vertexShader)}");
|
||||
|
||||
return vertexShader;
|
||||
}
|
||||
|
||||
public static uint CreateShaderProgram(uint vertexShader, uint fragmentShader)
|
||||
{
|
||||
uint program = Game.Gl.CreateProgram();
|
||||
|
||||
Game.Gl.AttachShader(program, vertexShader);
|
||||
Game.Gl.AttachShader(program, fragmentShader);
|
||||
|
||||
Game.Gl.LinkProgram(program);
|
||||
|
||||
Game.Gl.GetProgram(program, ProgramPropertyARB.LinkStatus, out int linkStatus);
|
||||
|
||||
if (linkStatus != (int)GLEnum.True)
|
||||
throw new Exception($"Program failed to link:{Game.Gl.GetProgramInfoLog(program)}");
|
||||
|
||||
Game.Gl.DetachShader(program, vertexShader);
|
||||
Game.Gl.DetachShader(program, fragmentShader);
|
||||
|
||||
return program;
|
||||
}
|
||||
|
||||
public static uint CreateShaderProgramFromSource(string vertexCode, string fragmentCode)
|
||||
{
|
||||
uint vertexShader = CreateShader(vertexCode, ShaderType.VertexShader);
|
||||
uint fragmentShader = CreateShader(fragmentCode, ShaderType.FragmentShader);
|
||||
|
||||
uint program = CreateShaderProgram(vertexShader, fragmentShader);
|
||||
|
||||
Game.Gl.DeleteShader(vertexShader);
|
||||
Game.Gl.DeleteShader(fragmentShader);
|
||||
|
||||
return program;
|
||||
}
|
||||
}
|
@ -1,69 +0,0 @@
|
||||
using Silk.NET.Windowing;
|
||||
using Silk.NET.Maths;
|
||||
using Silk.NET.Vulkan;
|
||||
using SkiaSharp;
|
||||
|
||||
namespace SampleFramework
|
||||
{
|
||||
class VulkanDevice : IGraphicsDevice
|
||||
{
|
||||
private Vk VK;
|
||||
|
||||
public VulkanDevice(IWindow window)
|
||||
{
|
||||
VK = Vk.GetApi();
|
||||
}
|
||||
|
||||
public void SetClearColor(float r, float g, float b, float a)
|
||||
{
|
||||
}
|
||||
|
||||
public void SetViewPort(int x, int y, uint width, uint height)
|
||||
{
|
||||
}
|
||||
|
||||
public void SetFrameBuffer(uint width, uint height)
|
||||
{
|
||||
}
|
||||
|
||||
public void ClearBuffer()
|
||||
{
|
||||
}
|
||||
|
||||
public void SwapBuffer()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public IPolygon GenPolygon(float[] vertices, uint[] indices, float[] uvs)
|
||||
{
|
||||
return new VulkanPolygon(vertices, indices, uvs);
|
||||
}
|
||||
|
||||
public IShader GenShader(string name)
|
||||
{
|
||||
return new VulkanShader();
|
||||
}
|
||||
|
||||
public unsafe ITexture GenTexture(void* data, int width, int height, RgbaType rgbaType)
|
||||
{
|
||||
return new VulkanTexture(data, width, height, rgbaType);
|
||||
}
|
||||
public void DrawPolygon(IPolygon polygon, IShader shader, ITexture texture, BlendType blendType)
|
||||
{
|
||||
}
|
||||
|
||||
public unsafe SKBitmap GetScreenPixels()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public unsafe void GetScreenPixelsASync(Action<SKBitmap> action)
|
||||
{
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
using Silk.NET.Windowing;
|
||||
using Silk.NET.OpenGL;
|
||||
using SkiaSharp;
|
||||
|
||||
namespace SampleFramework
|
||||
{
|
||||
public class VulkanPolygon : IPolygon
|
||||
{
|
||||
public uint IndiceCount { get; set; }
|
||||
|
||||
|
||||
public unsafe VulkanPolygon(float[] vertices, uint[] indices, float[] uvs)
|
||||
{
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
using System;
|
||||
using Silk.NET.Windowing;
|
||||
using Silk.NET.OpenGL;
|
||||
using Silk.NET.Maths;
|
||||
using SkiaSharp;
|
||||
using System.Numerics;
|
||||
|
||||
namespace SampleFramework
|
||||
{
|
||||
public class VulkanShader : IShader
|
||||
{
|
||||
|
||||
public VulkanShader()
|
||||
{
|
||||
}
|
||||
|
||||
public unsafe void SetMVP(Matrix4X4<float> mvp)
|
||||
{
|
||||
}
|
||||
|
||||
public unsafe void SetColor(Vector4D<float> color)
|
||||
{
|
||||
}
|
||||
|
||||
public unsafe void SetTextureRect(Vector4D<float> rect)
|
||||
{
|
||||
}
|
||||
|
||||
public unsafe void SetCamera(Matrix4X4<float> camera)
|
||||
{
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
using SkiaSharp;
|
||||
|
||||
namespace SampleFramework
|
||||
{
|
||||
internal class VulkanTexture : ITexture
|
||||
{
|
||||
|
||||
public unsafe VulkanTexture(void* data, int width, int height, RgbaType rgbaType)
|
||||
{
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -13,11 +13,222 @@ using RectangleF = System.Drawing.RectangleF;
|
||||
using Point = System.Drawing.Point;
|
||||
using Color = System.Drawing.Color;
|
||||
using SampleFramework;
|
||||
using Silk.NET.OpenGLES;
|
||||
|
||||
namespace FDK
|
||||
{
|
||||
public class CTexture : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// バッファの集まり
|
||||
/// </summary>
|
||||
private static uint VAO;
|
||||
|
||||
/// <summary>
|
||||
/// 頂点バッファ
|
||||
/// </summary>
|
||||
private static uint VBO;
|
||||
|
||||
/// <summary>
|
||||
/// 頂点バッファの使用順バッファ
|
||||
/// </summary>
|
||||
private static uint EBO;
|
||||
|
||||
/// <summary>
|
||||
/// テクスチャで使用するUV座標バッファ
|
||||
/// </summary>
|
||||
private static uint UVBO;
|
||||
|
||||
/// <summary>
|
||||
/// 頂点バッファの使用順の数
|
||||
/// </summary>
|
||||
private static uint IndicesCount;
|
||||
|
||||
/// <summary>
|
||||
/// シェーダー
|
||||
/// </summary>
|
||||
private static uint ShaderProgram;
|
||||
|
||||
/// <summary>
|
||||
/// 移動、回転、拡大縮小に使うMatrixのハンドル
|
||||
/// </summary>
|
||||
private static int MVPID;
|
||||
|
||||
/// <summary>
|
||||
/// 色合いのハンドル
|
||||
/// </summary>
|
||||
private static int ColorID;
|
||||
|
||||
/// <summary>
|
||||
/// テクスチャの切り抜きのハンドル
|
||||
/// </summary>
|
||||
private static int TextureRectID;
|
||||
|
||||
private static int CameraID;
|
||||
|
||||
/// <summary>
|
||||
/// 描画に使用する共通のバッファを作成
|
||||
/// </summary>
|
||||
public static void Init()
|
||||
{
|
||||
//シェーダーを作成、実際のコードはCreateShaderProgramWithShaderを見てください
|
||||
ShaderProgram = ShaderHelper.CreateShaderProgramFromSource(
|
||||
@"#version 100
|
||||
precision mediump float;
|
||||
|
||||
attribute vec3 aPosition;
|
||||
attribute vec2 aUV;
|
||||
|
||||
uniform mat4 mvp;
|
||||
uniform vec4 color;
|
||||
uniform mat4 camera;
|
||||
|
||||
varying vec2 texcoord;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec4 position = vec4(aPosition, 1.0);
|
||||
position = camera * mvp * position;
|
||||
|
||||
texcoord = vec2(aUV.x, aUV.y);
|
||||
gl_Position = position;
|
||||
}"
|
||||
,
|
||||
@"#version 100
|
||||
precision mediump float;
|
||||
|
||||
uniform vec4 color;
|
||||
uniform sampler2D texture1;
|
||||
uniform vec4 textureRect;
|
||||
|
||||
varying vec2 texcoord;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec2 rect = vec2(textureRect.xy + (texcoord * textureRect.zw));
|
||||
gl_FragColor = texture2D(texture1, rect) * color;
|
||||
}"
|
||||
);
|
||||
//------
|
||||
|
||||
//シェーダーに値を送るためのハンドルを取得------
|
||||
MVPID = Game.Gl.GetUniformLocation(ShaderProgram, "mvp"); //拡大縮小、移動、回転のMatrix
|
||||
ColorID = Game.Gl.GetUniformLocation(ShaderProgram, "color"); //色合い
|
||||
TextureRectID = Game.Gl.GetUniformLocation(ShaderProgram, "textureRect"); //テクスチャの切り抜きの座標と大きさ
|
||||
CameraID = Game.Gl.GetUniformLocation(ShaderProgram, "camera"); //テクスチャの切り抜きの座標と大きさ
|
||||
|
||||
|
||||
//------
|
||||
|
||||
//2DSprite専用のバッファーを作成する... なんとVAOは一つでOK!
|
||||
|
||||
|
||||
|
||||
//VAOを作成----
|
||||
VAO = Game.Gl.GenVertexArray();
|
||||
Game.Gl.BindVertexArray(VAO);
|
||||
//----
|
||||
|
||||
//VBOを作成-----
|
||||
float[] vertices = new float[] //頂点データ
|
||||
{
|
||||
//x, y, z
|
||||
-1.0f, 1.0f, 0.0f,
|
||||
1.0f, 1.0f, 0.0f,
|
||||
-1.0f, -1.0f, 0.0f,
|
||||
1.0f, -1.0f, 0.0f,
|
||||
};
|
||||
VBO = Game.Gl.GenBuffer(); //頂点バッファを作る
|
||||
Game.Gl.BindBuffer(BufferTargetARB.ArrayBuffer, VBO); //頂点バッファをバインドをする
|
||||
unsafe
|
||||
{
|
||||
fixed(float* data = vertices)
|
||||
{
|
||||
Game.Gl.BufferData(BufferTargetARB.ArrayBuffer, (nuint)(vertices.Length * sizeof(float)), data, BufferUsageARB.StaticDraw); //VRAMに頂点データを送る
|
||||
}
|
||||
}
|
||||
|
||||
uint locationPosition = (uint)Game.Gl.GetAttribLocation(ShaderProgram, "aPosition");
|
||||
Game.Gl.EnableVertexAttribArray(locationPosition); //layout (location = 0)を使用可能に
|
||||
unsafe
|
||||
{
|
||||
Game.Gl.VertexAttribPointer(locationPosition, 3, VertexAttribPointerType.Float, false, 3 * sizeof(float), (void*)0); //float3個で一つのxyzの塊として頂点を作る
|
||||
}
|
||||
//-----
|
||||
|
||||
|
||||
|
||||
//EBOを作成------
|
||||
//普通に四角を描画すると頂点データのxyzの塊が6個も必要だけど四つだけ作成して読み込む順番をこうやって登録すればメモリが少なくなる!
|
||||
|
||||
EBO = Game.Gl.GenBuffer(); //頂点バッファの使用順バッファを作る
|
||||
Game.Gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, EBO); //頂点バッファの使用順バッファをバインドする
|
||||
|
||||
uint[] indices = new uint[] //
|
||||
{
|
||||
0, 1, 2,
|
||||
2, 1, 3
|
||||
};
|
||||
IndicesCount = (uint)indices.Length; //数を登録する
|
||||
unsafe
|
||||
{
|
||||
fixed(uint* data = indices)
|
||||
{
|
||||
Game.Gl.BufferData(BufferTargetARB.ElementArrayBuffer, (nuint)(indices.Length * sizeof(uint)), data, BufferUsageARB.StaticDraw); //VRAMに送る
|
||||
}
|
||||
}
|
||||
//-----
|
||||
|
||||
//テクスチャの読み込みに使用するUV座標のバッファを作成、処理はVBOと大体同じ
|
||||
UVBO = Game.Gl.GenBuffer();
|
||||
Game.Gl.BindBuffer(BufferTargetARB.ArrayBuffer, UVBO);
|
||||
|
||||
float[] uvs = new float[]
|
||||
{
|
||||
0.0f, 0.0f,
|
||||
1.0f, 0.0f,
|
||||
0.0f, 1.0f,
|
||||
1.0f, 1.0f,
|
||||
};
|
||||
unsafe
|
||||
{
|
||||
fixed(float* data = uvs)
|
||||
{
|
||||
Game.Gl.BufferData(BufferTargetARB.ArrayBuffer, (nuint)(uvs.Length * sizeof(float)), data, BufferUsageARB.StaticDraw);
|
||||
}
|
||||
}
|
||||
|
||||
uint locationUV = (uint)Game.Gl.GetAttribLocation(ShaderProgram, "aUV");
|
||||
Game.Gl.EnableVertexAttribArray(locationUV);
|
||||
unsafe
|
||||
{
|
||||
Game.Gl.VertexAttribPointer(locationUV, 2, VertexAttribPointerType.Float, false, 2 * sizeof(float), (void*)0);
|
||||
}
|
||||
//-----
|
||||
|
||||
|
||||
//バインドを解除 厳密には必須ではないが何かのはずみでバインドされたままBufferSubDataでデータが更新されたらとかされたらまあ大変-----
|
||||
Game.Gl.BindVertexArray(0);
|
||||
Game.Gl.BindBuffer(BufferTargetARB.ArrayBuffer, 0);
|
||||
Game.Gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, 0);
|
||||
//-----
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 描画に使用する共通のバッファを解放
|
||||
/// </summary>
|
||||
public static void Terminate()
|
||||
{
|
||||
//ちゃんとバッファは解放すること
|
||||
Game.Gl.DeleteVertexArray(VAO);
|
||||
Game.Gl.DeleteBuffer(VBO);
|
||||
Game.Gl.DeleteBuffer(EBO);
|
||||
Game.Gl.DeleteBuffer(UVBO);
|
||||
Game.Gl.DeleteProgram(ShaderProgram);
|
||||
}
|
||||
|
||||
// プロパティ
|
||||
public bool b加算合成
|
||||
{
|
||||
@ -89,7 +300,7 @@ namespace FDK
|
||||
/// </summary>
|
||||
public static float f画面比率 = 1.0f;
|
||||
|
||||
internal ITexture Texture_;
|
||||
internal uint Texture_;
|
||||
|
||||
// コンストラクタ
|
||||
|
||||
@ -124,13 +335,13 @@ namespace FDK
|
||||
this.rc全画像 = new Rectangle(0, 0, this.sz画像サイズ.Width, this.sz画像サイズ.Height);
|
||||
}
|
||||
|
||||
public void UpdateTexture(IntPtr texture, int width, int height, RgbaType rgbaType)
|
||||
public void UpdateTexture(IntPtr texture, int width, int height, PixelFormat rgbaType)
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
Texture_?.Dispose();
|
||||
Game.Gl.DeleteTexture(Texture_); //解放
|
||||
void* data = texture.ToPointer();
|
||||
Texture_ = Game.GraphicsDevice.GenTexture(data, width, height, rgbaType);
|
||||
Texture_ = GenTexture(data, (uint)width, (uint)height, rgbaType);
|
||||
}
|
||||
this.sz画像サイズ = new Size(width, height);
|
||||
this.szテクスチャサイズ = this.t指定されたサイズを超えない最適なテクスチャサイズを返す(this.sz画像サイズ);
|
||||
@ -211,6 +422,28 @@ namespace FDK
|
||||
{
|
||||
MakeTexture(bitmap, b黒を透過する);
|
||||
}
|
||||
|
||||
private unsafe uint GenTexture(void* data, uint width, uint height, PixelFormat pixelFormat)
|
||||
{
|
||||
//テクスチャハンドルの作成-----
|
||||
uint handle = Game.Gl.GenTexture();
|
||||
Game.Gl.BindTexture(TextureTarget.Texture2D, handle);
|
||||
//-----
|
||||
|
||||
//テクスチャのデータをVramに送る
|
||||
Game.Gl.TexImage2D(TextureTarget.Texture2D, 0, (int)pixelFormat, width, height, 0, pixelFormat, GLEnum.UnsignedByte, data);
|
||||
//-----
|
||||
|
||||
//拡大縮小の時の補完を指定------
|
||||
Game.Gl.TexParameterI(GLEnum.Texture2D, GLEnum.TextureMinFilter, (int)TextureMinFilter.Nearest); //この場合は補完しない
|
||||
Game.Gl.TexParameterI(GLEnum.Texture2D, GLEnum.TextureMagFilter, (int)TextureMinFilter.Nearest);
|
||||
//------
|
||||
|
||||
Game.Gl.BindTexture(TextureTarget.Texture2D, 0); //バインドを解除することを忘れないように
|
||||
|
||||
return handle;
|
||||
}
|
||||
|
||||
public void MakeTexture(SKBitmap bitmap, bool b黒を透過する)
|
||||
{
|
||||
try
|
||||
@ -224,7 +457,27 @@ namespace FDK
|
||||
{
|
||||
fixed(void* data = bitmap.Pixels)
|
||||
{
|
||||
Texture_ = Game.GraphicsDevice.GenTexture(data, bitmap.Width, bitmap.Height, RgbaType.Bgra);
|
||||
if (Thread.CurrentThread.ManagedThreadId == Game.MainThreadID)
|
||||
{
|
||||
Texture_ = GenTexture(data, (uint)bitmap.Width, (uint)bitmap.Height, PixelFormat.Bgra);
|
||||
}
|
||||
else
|
||||
{
|
||||
SKBitmap bm = bitmap.Copy();
|
||||
Action createInstance = () =>
|
||||
{
|
||||
fixed(void* data2 = bitmap.Pixels)
|
||||
{
|
||||
Texture_ = GenTexture(data2, (uint)bitmap.Width, (uint)bitmap.Height, PixelFormat.Bgra);
|
||||
}
|
||||
bm.Dispose();
|
||||
};
|
||||
Game.AsyncActions.Add(createInstance);
|
||||
while(Game.AsyncActions.Contains(createInstance))
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -446,85 +699,88 @@ namespace FDK
|
||||
{
|
||||
this.t2D描画((int)x, (int)y, 1f, rc画像内の描画領域);
|
||||
}
|
||||
public void t2D描画(float x, float y, float depth, RectangleF rc画像内の描画領域)
|
||||
public void t2D描画(float x, float y, float depth, RectangleF rc画像内の描画領域, bool flipX = false, bool flipY = false)
|
||||
{
|
||||
this.color4.Alpha = this._opacity / 255f;
|
||||
|
||||
float offsetX = rc画像内の描画領域.Width;
|
||||
float offsetY = rc画像内の描画領域.Height;
|
||||
|
||||
Matrix4X4<float> mvp = Matrix4X4<float>.Identity;
|
||||
|
||||
float trueWidth = Math.Abs((float)rc画像内の描画領域.Width);
|
||||
float trueHeight = Math.Abs((float)rc画像内の描画領域.Height);
|
||||
|
||||
Matrix4X4<float> scaling()
|
||||
{
|
||||
Matrix4X4<float> resizeMatrix = Matrix4X4.CreateScale(trueWidth / GameWindowSize.Width, trueHeight / GameWindowSize.Height, 0.0f);
|
||||
Matrix4X4<float> scaleMatrix = Matrix4X4.CreateScale(vc拡大縮小倍率.X, vc拡大縮小倍率.Y, vc拡大縮小倍率.Z);
|
||||
return resizeMatrix * scaleMatrix;
|
||||
}
|
||||
|
||||
Matrix4X4<float> rotation(float rotate)
|
||||
{
|
||||
Matrix4X4<float> rotationMatrix = Matrix4X4.CreateScale(1.0f * Game.ScreenAspect, 1.0f, 1.0f);
|
||||
rotationMatrix *=
|
||||
Matrix4X4.CreateRotationX(0.0f) *
|
||||
Matrix4X4.CreateRotationY(0.0f) *
|
||||
Matrix4X4.CreateRotationZ(rotate);
|
||||
rotationMatrix *= Matrix4X4.CreateScale(1.0f / Game.ScreenAspect, 1.0f, 1.0f);
|
||||
|
||||
return rotationMatrix;
|
||||
}
|
||||
|
||||
Matrix4X4<float> translation()
|
||||
{
|
||||
float api_x = (-1 + (x * 2.0f / GameWindowSize.Width));
|
||||
float api_y = (-1 + (y * 2.0f / GameWindowSize.Height)) * -1;
|
||||
|
||||
Matrix4X4<float> translation = Matrix4X4.CreateTranslation(api_x, api_y, 0.0f);
|
||||
Matrix4X4<float> translation2 = Matrix4X4.CreateTranslation(
|
||||
(trueWidth * vc拡大縮小倍率.X / GameWindowSize.Width),
|
||||
(trueHeight * vc拡大縮小倍率.Y / GameWindowSize.Height) * -1,
|
||||
0.0f);
|
||||
return translation * translation2;
|
||||
}
|
||||
|
||||
mvp *= scaling();
|
||||
mvp *= rotation(fZ軸中心回転);
|
||||
mvp *= translation();
|
||||
|
||||
Game.Shader_.SetColor(new Vector4D<float>(color4.Red, color4.Green, color4.Blue, color4.Alpha));
|
||||
Vector4D<float> rect = new(
|
||||
rc画像内の描画領域.X / rc全画像.Width,
|
||||
rc画像内の描画領域.Y / rc全画像.Height,
|
||||
rc画像内の描画領域.Width / rc全画像.Width,
|
||||
rc画像内の描画領域.Height / rc全画像.Height);
|
||||
Game.Shader_.SetTextureRect(rect);
|
||||
Game.Shader_.SetMVP(mvp);
|
||||
|
||||
Game.Shader_.SetCamera(Game.Camera);
|
||||
|
||||
BlendType blendType;
|
||||
if (b加算合成)
|
||||
{
|
||||
Game.GraphicsDevice.DrawPolygon(Game.Polygon_, Game.Shader_, Texture_, BlendType.Add);
|
||||
blendType = BlendType.Add;
|
||||
}
|
||||
else if (b乗算合成)
|
||||
{
|
||||
Game.GraphicsDevice.DrawPolygon(Game.Polygon_, Game.Shader_, Texture_, BlendType.Multi);
|
||||
blendType = BlendType.Multi;
|
||||
}
|
||||
else if (b減算合成)
|
||||
{
|
||||
Game.GraphicsDevice.DrawPolygon(Game.Polygon_, Game.Shader_, Texture_, BlendType.Sub);
|
||||
blendType = BlendType.Sub;
|
||||
}
|
||||
else if (bスクリーン合成)
|
||||
{
|
||||
Game.GraphicsDevice.DrawPolygon(Game.Polygon_, Game.Shader_, Texture_, BlendType.Screen);
|
||||
blendType = BlendType.Screen;
|
||||
}
|
||||
else
|
||||
{
|
||||
Game.GraphicsDevice.DrawPolygon(Game.Polygon_, Game.Shader_, Texture_, BlendType.Normal);
|
||||
blendType = BlendType.Normal;
|
||||
}
|
||||
|
||||
BlendHelper.SetBlend(blendType);
|
||||
|
||||
Game.Gl.UseProgram(ShaderProgram);//Uniform4よりこれが先
|
||||
|
||||
//Game.Gl.Uniform1(Texture1ID, Texture_);
|
||||
Game.Gl.BindTexture(TextureTarget.Texture2D, Texture_); //テクスチャをバインド
|
||||
|
||||
//MVPを設定----
|
||||
unsafe
|
||||
{
|
||||
Matrix4X4<float> mvp = Matrix4X4<float>.Identity;
|
||||
|
||||
float gameAspect = (float)GameWindowSize.Width / GameWindowSize.Height;
|
||||
|
||||
|
||||
//スケーリング-----
|
||||
mvp *= Matrix4X4.CreateScale(rc画像内の描画領域.Width / GameWindowSize.Width, rc画像内の描画領域.Height / GameWindowSize.Height, 1) *
|
||||
Matrix4X4.CreateScale(flipX ? -vc拡大縮小倍率.X : vc拡大縮小倍率.X, flipY ? -vc拡大縮小倍率.Y : vc拡大縮小倍率.Y, 1.0f);
|
||||
//-----
|
||||
|
||||
//回転-----
|
||||
mvp *= Matrix4X4.CreateScale(1.0f * gameAspect, 1.0f, 1.0f) * //ここでアスペクト比でスケーリングしないとおかしなことになる
|
||||
Matrix4X4.CreateRotationZ(fZ軸中心回転) *
|
||||
Matrix4X4.CreateScale(1.0f / gameAspect, 1.0f, 1.0f);//回転した後戻してあげる
|
||||
//-----
|
||||
|
||||
//移動----
|
||||
float offsetX = rc画像内の描画領域.Width * vc拡大縮小倍率.X / GameWindowSize.Width;
|
||||
float offsetY = rc画像内の描画領域.Height * vc拡大縮小倍率.Y / GameWindowSize.Height;
|
||||
mvp *= Matrix4X4.CreateTranslation(offsetX, -offsetY, 0.0f);
|
||||
mvp *= Matrix4X4.CreateTranslation(-1.0f, 1.0f, 0);
|
||||
mvp *= Matrix4X4.CreateTranslation(x / GameWindowSize.Width * 2, -y / GameWindowSize.Height * 2, 0.0f);
|
||||
//-----
|
||||
|
||||
Game.Gl.UniformMatrix4(MVPID, 1, false, (float*)&mvp); //MVPに値を設定
|
||||
Matrix4X4<float> camera = Game.Camera;
|
||||
Game.Gl.UniformMatrix4(CameraID, 1, false, (float*)&camera);
|
||||
}
|
||||
//------
|
||||
|
||||
Game.Gl.Uniform4(ColorID, new System.Numerics.Vector4(color4.Red, color4.Green, color4.Blue, color4.Alpha)); //変色用のカラーを設定
|
||||
|
||||
//テクスチャの切り抜きの座標と大きさを設定
|
||||
Game.Gl.Uniform4(TextureRectID, new System.Numerics.Vector4(
|
||||
rc画像内の描画領域.X / rc全画像.Width, rc画像内の描画領域.Y / rc全画像.Height, //始まり
|
||||
rc画像内の描画領域.Width / rc全画像.Width, rc画像内の描画領域.Height / rc全画像.Height)); //大きさ、終わりではない
|
||||
|
||||
|
||||
//描画-----
|
||||
Game.Gl.BindVertexArray(VAO);
|
||||
unsafe
|
||||
{
|
||||
Game.Gl.DrawElements(PrimitiveType.Triangles, IndicesCount, DrawElementsType.UnsignedInt, (void*)0);//描画!
|
||||
}
|
||||
|
||||
BlendHelper.SetBlend(BlendType.Normal);
|
||||
}
|
||||
public void t2D描画(int x, int y, float depth, Rectangle rc画像内の描画領域)
|
||||
{
|
||||
@ -552,11 +808,11 @@ namespace FDK
|
||||
}
|
||||
public void t2D左右反転描画(float x, float y, float depth, Rectangle rc画像内の描画領域)
|
||||
{
|
||||
t2D描画(x, y, depth, new RectangleF(rc画像内の描画領域.Width, 0, -rc画像内の描画領域.Width, rc画像内の描画領域.Height));
|
||||
t2D描画(x, y, depth, rc画像内の描画領域, flipX:true);
|
||||
}
|
||||
public void t2D上下反転描画(int x, int y, float depth, Rectangle rc画像内の描画領域)
|
||||
{
|
||||
t2D描画(x, y, depth, new RectangleF(0, rc画像内の描画領域.Height, rc画像内の描画領域.Width, -rc画像内の描画領域.Height));
|
||||
t2D描画(x, y, depth, rc画像内の描画領域, flipY:true);
|
||||
}
|
||||
public void t2D上下反転描画(Point pt)
|
||||
{
|
||||
@ -595,101 +851,6 @@ namespace FDK
|
||||
v3論理画面座標.Z);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// テクスチャを 3D 画像と見なして描画する。
|
||||
/// </summary>
|
||||
public void t3D描画(Matrix4X4<float> mat)
|
||||
{
|
||||
this.t3D描画(mat, this.rc全画像);
|
||||
}
|
||||
public void t3D描画(Matrix4X4<float> mat, Rectangle rc画像内の描画領域)
|
||||
{
|
||||
float x = ((float)rc画像内の描画領域.Width) / 2f;
|
||||
float y = ((float)rc画像内の描画領域.Height) / 2f;
|
||||
float z = 0.0f;
|
||||
float f左U値 = ((float)rc画像内の描画領域.Left) / ((float)this.szテクスチャサイズ.Width);
|
||||
float f右U値 = ((float)rc画像内の描画領域.Right) / ((float)this.szテクスチャサイズ.Width);
|
||||
float f上V値 = ((float)rc画像内の描画領域.Top) / ((float)this.szテクスチャサイズ.Height);
|
||||
float f下V値 = ((float)rc画像内の描画領域.Bottom) / ((float)this.szテクスチャサイズ.Height);
|
||||
this.color4.Alpha = ((float)this._opacity) / 255f;
|
||||
int color = ToArgb(this.color4);
|
||||
|
||||
Matrix4X4<float> mvp = mat;
|
||||
|
||||
Game.Shader_.SetColor(new Vector4D<float>(color4.Red, color4.Green, color4.Blue, color4.Alpha));
|
||||
Game.Shader_.SetMVP(mvp);
|
||||
|
||||
if (b加算合成)
|
||||
{
|
||||
Game.GraphicsDevice.DrawPolygon(Game.Polygon_, Game.Shader_, Texture_, BlendType.Add);
|
||||
}
|
||||
else if (b乗算合成)
|
||||
{
|
||||
Game.GraphicsDevice.DrawPolygon(Game.Polygon_, Game.Shader_, Texture_, BlendType.Multi);
|
||||
}
|
||||
else if (b減算合成)
|
||||
{
|
||||
Game.GraphicsDevice.DrawPolygon(Game.Polygon_, Game.Shader_, Texture_, BlendType.Sub);
|
||||
}
|
||||
else if (bスクリーン合成)
|
||||
{
|
||||
Game.GraphicsDevice.DrawPolygon(Game.Polygon_, Game.Shader_, Texture_, BlendType.Screen);
|
||||
}
|
||||
else
|
||||
{
|
||||
Game.GraphicsDevice.DrawPolygon(Game.Polygon_, Game.Shader_, Texture_, BlendType.Normal);
|
||||
}
|
||||
}
|
||||
|
||||
public void t3D左上基準描画(Matrix4X4<float> mat)
|
||||
{
|
||||
this.t3D左上基準描画(mat, this.rc全画像);
|
||||
}
|
||||
/// <summary>
|
||||
/// ○覚書
|
||||
/// SlimDX.Matrix mat = SlimDX.Matrix.Identity;
|
||||
/// mat *= SlimDX.Matrix.Translation( x, y, z );
|
||||
/// 「mat =」ではなく「mat *=」であることを忘れないこと。
|
||||
/// </summary>
|
||||
public void t3D左上基準描画(Matrix4X4<float> mat, Rectangle rc画像内の描画領域)
|
||||
{
|
||||
float x = 0.0f;
|
||||
float y = 0.0f;
|
||||
float z = 0.0f;
|
||||
float f左U値 = ((float)rc画像内の描画領域.Left) / ((float)this.szテクスチャサイズ.Width);
|
||||
float f右U値 = ((float)rc画像内の描画領域.Right) / ((float)this.szテクスチャサイズ.Width);
|
||||
float f上V値 = ((float)rc画像内の描画領域.Top) / ((float)this.szテクスチャサイズ.Height);
|
||||
float f下V値 = ((float)rc画像内の描画領域.Bottom) / ((float)this.szテクスチャサイズ.Height);
|
||||
this.color4.Alpha = ((float)this._opacity) / 255f;
|
||||
int color = ToArgb(this.color4);
|
||||
|
||||
Matrix4X4<float> mvp = mat;
|
||||
|
||||
Game.Shader_.SetColor(new Vector4D<float>(color4.Red, color4.Green, color4.Blue, color4.Alpha));
|
||||
Game.Shader_.SetMVP(mvp);
|
||||
|
||||
if (b加算合成)
|
||||
{
|
||||
Game.GraphicsDevice.DrawPolygon(Game.Polygon_, Game.Shader_, Texture_, BlendType.Add);
|
||||
}
|
||||
else if (b乗算合成)
|
||||
{
|
||||
Game.GraphicsDevice.DrawPolygon(Game.Polygon_, Game.Shader_, Texture_, BlendType.Multi);
|
||||
}
|
||||
else if (b減算合成)
|
||||
{
|
||||
Game.GraphicsDevice.DrawPolygon(Game.Polygon_, Game.Shader_, Texture_, BlendType.Sub);
|
||||
}
|
||||
else if (bスクリーン合成)
|
||||
{
|
||||
Game.GraphicsDevice.DrawPolygon(Game.Polygon_, Game.Shader_, Texture_, BlendType.Screen);
|
||||
}
|
||||
else
|
||||
{
|
||||
Game.GraphicsDevice.DrawPolygon(Game.Polygon_, Game.Shader_, Texture_, BlendType.Normal);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@ -698,79 +859,85 @@ namespace FDK
|
||||
this.color4.Alpha = this._opacity / 255f;
|
||||
|
||||
var rc画像内の描画領域 = rc全画像;
|
||||
this.color4.Alpha = this._opacity / 255f;
|
||||
|
||||
float offsetX = rc画像内の描画領域.Width;
|
||||
float offsetY = rc画像内の描画領域.Height;
|
||||
|
||||
Matrix4X4<float> mvp = Matrix4X4<float>.Identity;
|
||||
|
||||
Matrix4X4<float> scaling()
|
||||
{
|
||||
Matrix4X4<float> resizeMatrix = Matrix4X4.CreateScale((float)rc画像内の描画領域.Width / GameWindowSize.Width, (float)rc画像内の描画領域.Height / GameWindowSize.Height, 0.0f);
|
||||
Matrix4X4<float> scaleMatrix = Matrix4X4.CreateScale(xScale, yScale, 1.0f);
|
||||
return resizeMatrix * scaleMatrix;
|
||||
}
|
||||
|
||||
Matrix4X4<float> rotation(float rotate)
|
||||
{
|
||||
Matrix4X4<float> rotationMatrix = Matrix4X4.CreateScale(1.0f * Game.ScreenAspect, 1.0f, 1.0f);
|
||||
rotationMatrix *=
|
||||
Matrix4X4.CreateRotationX(0.0f) *
|
||||
Matrix4X4.CreateRotationY(0.0f) *
|
||||
Matrix4X4.CreateRotationZ(rotate);
|
||||
rotationMatrix *= Matrix4X4.CreateScale(1.0f / Game.ScreenAspect, 1.0f, 1.0f);
|
||||
|
||||
return rotationMatrix;
|
||||
}
|
||||
|
||||
Matrix4X4<float> translation()
|
||||
{
|
||||
float api_x = (-1 + (x * 2.0f / GameWindowSize.Width));
|
||||
float api_y = (-1 + (y * 2.0f / GameWindowSize.Height)) * -1;
|
||||
|
||||
Matrix4X4<float> translation = Matrix4X4.CreateTranslation(api_x, api_y, 0.0f);
|
||||
Matrix4X4<float> translation2 = Matrix4X4.CreateTranslation(
|
||||
(rc画像内の描画領域.Width * xScale / GameWindowSize.Width),
|
||||
(rc画像内の描画領域.Height * yScale / GameWindowSize.Height) * -1,
|
||||
0.0f);
|
||||
return translation * translation2;
|
||||
}
|
||||
|
||||
mvp *= scaling();
|
||||
mvp *= rotation(fZ軸中心回転);
|
||||
mvp *= translation();
|
||||
|
||||
Game.Shader_.SetColor(new Vector4D<float>(color4.Red, color4.Green, color4.Blue, color4.Alpha));
|
||||
Vector4D<float> rect = new(
|
||||
rc画像内の描画領域.X / rc全画像.Width,
|
||||
rc画像内の描画領域.Y / rc全画像.Height,
|
||||
rc画像内の描画領域.Width / rc全画像.Width,
|
||||
rc画像内の描画領域.Height / rc全画像.Height);
|
||||
Game.Shader_.SetTextureRect(rect);
|
||||
Game.Shader_.SetMVP(mvp);
|
||||
|
||||
Game.Shader_.SetCamera(Game.Camera);
|
||||
|
||||
BlendType blendType;
|
||||
if (b加算合成)
|
||||
{
|
||||
Game.GraphicsDevice.DrawPolygon(Game.Polygon_, Game.Shader_, Texture_, BlendType.Add);
|
||||
blendType = BlendType.Add;
|
||||
}
|
||||
else if (b乗算合成)
|
||||
{
|
||||
Game.GraphicsDevice.DrawPolygon(Game.Polygon_, Game.Shader_, Texture_, BlendType.Multi);
|
||||
blendType = BlendType.Multi;
|
||||
}
|
||||
else if (b減算合成)
|
||||
{
|
||||
Game.GraphicsDevice.DrawPolygon(Game.Polygon_, Game.Shader_, Texture_, BlendType.Sub);
|
||||
blendType = BlendType.Sub;
|
||||
}
|
||||
else if (bスクリーン合成)
|
||||
{
|
||||
Game.GraphicsDevice.DrawPolygon(Game.Polygon_, Game.Shader_, Texture_, BlendType.Screen);
|
||||
blendType = BlendType.Screen;
|
||||
}
|
||||
else
|
||||
{
|
||||
Game.GraphicsDevice.DrawPolygon(Game.Polygon_, Game.Shader_, Texture_, BlendType.Normal);
|
||||
blendType = BlendType.Normal;
|
||||
}
|
||||
|
||||
BlendHelper.SetBlend(blendType);
|
||||
|
||||
Game.Gl.UseProgram(ShaderProgram);//Uniform4よりこれが先
|
||||
|
||||
Game.Gl.BindTexture(TextureTarget.Texture2D, Texture_); //テクスチャをバインド
|
||||
|
||||
//MVPを設定----
|
||||
unsafe
|
||||
{
|
||||
Matrix4X4<float> mvp = Matrix4X4<float>.Identity;
|
||||
|
||||
float gameAspect = (float)GameWindowSize.Width / GameWindowSize.Height;
|
||||
|
||||
|
||||
//スケーリング-----
|
||||
mvp *= Matrix4X4.CreateScale((float)rc画像内の描画領域.Width / GameWindowSize.Width, (float)rc画像内の描画領域.Height / GameWindowSize.Height, 1) *
|
||||
Matrix4X4.CreateScale(xScale, yScale, 1.0f);
|
||||
//-----
|
||||
|
||||
//回転-----
|
||||
mvp *= Matrix4X4.CreateScale(1.0f * gameAspect, 1.0f, 1.0f) * //ここでアスペクト比でスケーリングしないとおかしなことになる
|
||||
Matrix4X4.CreateRotationZ(fZ軸中心回転) *
|
||||
Matrix4X4.CreateScale(1.0f / gameAspect, 1.0f, 1.0f);//回転した後戻してあげる
|
||||
//-----
|
||||
|
||||
//移動----
|
||||
float offsetX = rc画像内の描画領域.Width * xScale / GameWindowSize.Width;
|
||||
float offsetY = rc画像内の描画領域.Height * yScale / GameWindowSize.Height;
|
||||
mvp *= Matrix4X4.CreateTranslation(offsetX, -offsetY, 0.0f);
|
||||
mvp *= Matrix4X4.CreateTranslation(-1.0f, 1.0f, 0);
|
||||
mvp *= Matrix4X4.CreateTranslation(x / GameWindowSize.Width * 2, -y / GameWindowSize.Height * 2, 0.0f);
|
||||
//-----
|
||||
|
||||
Game.Gl.UniformMatrix4(MVPID, 1, false, (float*)&mvp); //MVPに値を設定
|
||||
Matrix4X4<float> camera = Game.Camera;
|
||||
Game.Gl.UniformMatrix4(CameraID, 1, false, (float*)&camera);
|
||||
}
|
||||
//------
|
||||
|
||||
Game.Gl.Uniform4(ColorID, new System.Numerics.Vector4(color4.Red, color4.Green, color4.Blue, color4.Alpha)); //変色用のカラーを設定
|
||||
|
||||
//テクスチャの切り抜きの座標と大きさを設定
|
||||
Game.Gl.Uniform4(TextureRectID, new System.Numerics.Vector4(
|
||||
rc画像内の描画領域.X / rc全画像.Width, rc画像内の描画領域.Y / rc全画像.Height, //始まり
|
||||
rc画像内の描画領域.Width / rc全画像.Width, rc画像内の描画領域.Height / rc全画像.Height)); //大きさ、終わりではない
|
||||
|
||||
|
||||
//描画-----
|
||||
Game.Gl.BindVertexArray(VAO);
|
||||
unsafe
|
||||
{
|
||||
Game.Gl.DrawElements(PrimitiveType.Triangles, IndicesCount, DrawElementsType.UnsignedInt, (void*)0);//描画!
|
||||
}
|
||||
|
||||
BlendHelper.SetBlend(BlendType.Normal);
|
||||
}
|
||||
|
||||
|
||||
@ -780,10 +947,7 @@ namespace FDK
|
||||
{
|
||||
if (!this.bDispose完了済み)
|
||||
{
|
||||
Texture_?.Dispose();
|
||||
|
||||
|
||||
|
||||
Game.Gl.DeleteTexture(Texture_); //解放
|
||||
|
||||
this.bDispose完了済み = true;
|
||||
}
|
||||
|
@ -189,83 +189,6 @@ namespace FDK
|
||||
t2D描画( (int) x, (int) y, rc );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// テクスチャを 2D 画像と見なして描画する。
|
||||
/// </summary>
|
||||
/// <param name="device">Direct3D9 デバイス。</param>
|
||||
/// <param name="x">描画位置(テクスチャの左上位置の X 座標[dot])。</param>
|
||||
/// <param name="y">描画位置(テクスチャの左上位置の Y 座標[dot])。</param>
|
||||
public void t3D描画(Matrix4X4<float> mat, float x, float y )
|
||||
{
|
||||
#if TEST_FOLDTEXTURE
|
||||
base.t2D描画( x, y, 1f, rc全画像 );
|
||||
#else
|
||||
for ( int n = 0; n <= _foldtimes; n++ )
|
||||
{
|
||||
Rectangle r;
|
||||
if ( b横長のテクスチャである )
|
||||
{
|
||||
int currentHeight = n * _orgHeight;
|
||||
r = new Rectangle( 0, currentHeight, this.rc全画像.Width, _orgHeight );
|
||||
base.t3D描画( mat );
|
||||
}
|
||||
else
|
||||
{
|
||||
int currentWidth = n * _orgWidth;
|
||||
r = new Rectangle( currentWidth, 0, _orgWidth, this.rc全画像.Height );
|
||||
base.t3D描画( mat );
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
public void t3D描画(Matrix4X4<float> mat, float x, float y, Rectangle rc )
|
||||
{
|
||||
Rectangle r;
|
||||
if ( b横長のテクスチャである )
|
||||
{
|
||||
int beginFold = rc.X / this.rc全画像.Width;
|
||||
int endFold = ( rc.X + rc.Width ) / rc全画像.Width;
|
||||
for ( int i = beginFold; i <= endFold; i++ )
|
||||
{
|
||||
if ( i > _foldtimes ) break;
|
||||
|
||||
int newRcY = i * _orgHeight + rc.Y;
|
||||
int newRcX = ( i == beginFold ) ? ( rc.X % this.rc全画像.Width ) : 0;
|
||||
int newRcWidth = ( newRcX + rc.Width > rc全画像.Width ) ? rc全画像.Width - newRcX : rc.Width;
|
||||
|
||||
r = new Rectangle( newRcX, newRcY, newRcWidth, rc.Height );
|
||||
base.t3D描画( mat, r );
|
||||
|
||||
int deltaX = ( i == beginFold ) ? ( i + 1 ) * rc全画像.Width - rc.X : rc全画像.Width;
|
||||
int newWidth = rc.Width - deltaX;
|
||||
x += deltaX;
|
||||
rc.Width = newWidth;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
int beginFold = rc.Y / this.rc全画像.Height;
|
||||
int endFold = ( rc.Y + rc.Height ) / rc全画像.Height;
|
||||
for ( int i = beginFold; i <= endFold; i++ )
|
||||
{
|
||||
if ( i > _foldtimes ) break;
|
||||
|
||||
int newRcX = i * _orgWidth + rc.X;
|
||||
int newRcY = ( i == beginFold ) ? ( rc.Y % this.rc全画像.Height ) : 0;
|
||||
int newRcHeight = ( newRcY + rc.Height > rc全画像.Height ) ? rc全画像.Height - newRcY : rc.Height;
|
||||
|
||||
r = new Rectangle( newRcX, newRcY, rc.Width, newRcHeight );
|
||||
base.t3D描画( mat, r );
|
||||
|
||||
int deltaY = ( i == beginFold ) ? ( i + 1 ) * rc全画像.Height - rc.Y : rc全画像.Height;
|
||||
int newHeight = rc.Height - deltaY;
|
||||
y += deltaY;
|
||||
rc.Height = newHeight;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#region [ private ]
|
||||
//-----------------
|
||||
private bool b横長のテクスチャである;
|
||||
|
@ -171,7 +171,7 @@ namespace FDK
|
||||
continue;
|
||||
}
|
||||
|
||||
lastTexture.UpdateTexture(cdecodedframe.TexPointer, cdecodedframe.TexSize.Width, cdecodedframe.TexSize.Height, SampleFramework.RgbaType.Rgba);
|
||||
lastTexture.UpdateTexture(cdecodedframe.TexPointer, cdecodedframe.TexSize.Width, cdecodedframe.TexSize.Height, Silk.NET.OpenGLES.PixelFormat.Rgba);
|
||||
|
||||
cdecodedframe.RemoveFrame();
|
||||
}
|
||||
|
BIN
OpenTaiko/Libs/linux-x64/libEGL.so
Normal file
BIN
OpenTaiko/Libs/linux-x64/libEGL.so
Normal file
Binary file not shown.
BIN
OpenTaiko/Libs/linux-x64/libGLESv2.so
Normal file
BIN
OpenTaiko/Libs/linux-x64/libGLESv2.so
Normal file
Binary file not shown.
BIN
OpenTaiko/Libs/linux-x64/libvulkan.so.1
Normal file
BIN
OpenTaiko/Libs/linux-x64/libvulkan.so.1
Normal file
Binary file not shown.
BIN
OpenTaiko/Libs/osx-x64/libEGL.dylib
Normal file
BIN
OpenTaiko/Libs/osx-x64/libEGL.dylib
Normal file
Binary file not shown.
BIN
OpenTaiko/Libs/osx-x64/libGLESv2.dylib
Normal file
BIN
OpenTaiko/Libs/osx-x64/libGLESv2.dylib
Normal file
Binary file not shown.
BIN
OpenTaiko/Libs/win-x64/libEGL.dll
Normal file
BIN
OpenTaiko/Libs/win-x64/libEGL.dll
Normal file
Binary file not shown.
BIN
OpenTaiko/Libs/win-x64/libGLESv2.dll
Normal file
BIN
OpenTaiko/Libs/win-x64/libGLESv2.dll
Normal file
Binary file not shown.
BIN
OpenTaiko/Libs/win-x64/vulkan-1.dll
Normal file
BIN
OpenTaiko/Libs/win-x64/vulkan-1.dll
Normal file
Binary file not shown.
BIN
OpenTaiko/Libs/win-x86/libEGL.dll
Normal file
BIN
OpenTaiko/Libs/win-x86/libEGL.dll
Normal file
Binary file not shown.
BIN
OpenTaiko/Libs/win-x86/libGLESv2.dll
Normal file
BIN
OpenTaiko/Libs/win-x86/libGLESv2.dll
Normal file
Binary file not shown.
BIN
OpenTaiko/Libs/win-x86/vulkan-1.dll
Normal file
BIN
OpenTaiko/Libs/win-x86/vulkan-1.dll
Normal file
Binary file not shown.
@ -1,21 +0,0 @@
|
||||
zlib License
|
||||
SPDX identifier
|
||||
Zlib
|
||||
License text
|
||||
zlib License
|
||||
|
||||
Copyright (c) <year> <copyright holders>
|
||||
|
||||
This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
SPDX web page
|
||||
https://spdx.org/licenses/Zlib.html
|
||||
Notice
|
||||
This license content is provided by the SPDX project. For more information about licenses.nuget.org, see our documentation.
|
||||
|
||||
Data pulled from spdx/license-list-data on February 9, 2023.
|
32
OpenTaiko/Licenses/angle.txt
Normal file
32
OpenTaiko/Licenses/angle.txt
Normal file
@ -0,0 +1,32 @@
|
||||
// Copyright 2018 The ANGLE Project Authors.
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions
|
||||
// are met:
|
||||
//
|
||||
// Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
//
|
||||
// Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following
|
||||
// disclaimer in the documentation and/or other materials provided
|
||||
// with the distribution.
|
||||
//
|
||||
// Neither the name of TransGaming Inc., Google Inc., 3DLabs Inc.
|
||||
// Ltd., nor the names of their contributors may be used to endorse
|
||||
// or promote products derived from this software without specific
|
||||
// prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
// POSSIBILITY OF SUCH DAMAGE.
|
8
OpenTaiko/Licenses/opentk.txt
Normal file
8
OpenTaiko/Licenses/opentk.txt
Normal file
@ -0,0 +1,8 @@
|
||||
MIT License
|
||||
Copyright (c) 2006-2019 Stefanos Apostolopoulos for the Open Toolkit project.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@ -13,7 +13,6 @@
|
||||
<Content Include="Databases/**" CopyToOutputDirectory="PreserveNewest"/>
|
||||
<Content Include="Documentation/**" CopyToOutputDirectory="PreserveNewest"/>
|
||||
<Content Include="Encyclopedia/**" CopyToOutputDirectory="PreserveNewest"/>
|
||||
<Content Include="Shaders/**" CopyToOutputDirectory="PreserveNewest"/>
|
||||
<Content Include="FFmpeg/**" CopyToOutputDirectory="PreserveNewest"/>
|
||||
<Content Include="Global/**" CopyToOutputDirectory="PreserveNewest"/>
|
||||
<Content Include="Libs/**" CopyToOutputDirectory="PreserveNewest"/>
|
||||
|
@ -1,13 +0,0 @@
|
||||
#version 330 core
|
||||
|
||||
in vec2 texcoord;
|
||||
out vec4 out_color;
|
||||
uniform sampler2D texture1;
|
||||
uniform vec4 color;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec4 totalcolor = texture(texture1, texcoord);
|
||||
totalcolor *= color;
|
||||
out_color = totalcolor;
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
|
||||
#version 330 core
|
||||
|
||||
layout (location = 0) in vec3 aPosition;
|
||||
layout (location = 1) in vec2 aTexCoord;
|
||||
|
||||
uniform mat4 mvp;
|
||||
uniform vec4 textureRect;
|
||||
uniform mat4 camera;
|
||||
|
||||
out vec2 texcoord;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec4 position = vec4(aPosition, 1.0);
|
||||
position = mvp * position * camera;
|
||||
|
||||
gl_Position = position;
|
||||
texcoord = textureRect.xy;
|
||||
texcoord.xy += aTexCoord.xy * textureRect.zw;
|
||||
}
|
@ -1,49 +0,0 @@
|
||||
|
||||
Texture2D g_texture : register(t0);
|
||||
SamplerState g_sampler : register(s0);
|
||||
|
||||
struct vs_in {
|
||||
float3 position_local : POS;
|
||||
float2 uvposition_local : UVPOS;
|
||||
};
|
||||
|
||||
struct vs_out {
|
||||
float4 position_clip : SV_POSITION;
|
||||
float2 uvposition_clip : TEXCOORD0;
|
||||
};
|
||||
|
||||
cbuffer ConstantBufferStruct
|
||||
{
|
||||
float4x4 Projection;
|
||||
float4 Color;
|
||||
float4 TextureRect;
|
||||
float4x4 Camera;
|
||||
}
|
||||
|
||||
vs_out vs_main(vs_in input) {
|
||||
vs_out output = (vs_out)0;
|
||||
|
||||
float4 position = float4(input.position_local, 1.0);
|
||||
position = mul(Projection, position);
|
||||
position = mul(position, Camera);
|
||||
|
||||
output.position_clip = position;
|
||||
|
||||
float2 texcoord = float2(TextureRect.x, TextureRect.y);
|
||||
texcoord.x += input.uvposition_local.x * TextureRect.z;
|
||||
texcoord.y += input.uvposition_local.y * TextureRect.w;
|
||||
|
||||
output.uvposition_clip = texcoord;
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
float4 ps_main(vs_out input) : SV_TARGET {
|
||||
float4 totalcolor = float4(1.0, 1.0, 1.0, 1.0);
|
||||
|
||||
totalcolor = g_texture.Sample(g_sampler, input.uvposition_clip);
|
||||
|
||||
totalcolor.rgba *= Color.rgba;
|
||||
|
||||
return totalcolor;
|
||||
}
|
Binary file not shown.
Before Width: | Height: | Size: 125 KiB After Width: | Height: | Size: 61 KiB |
@ -13,8 +13,8 @@
|
||||
local bg_width = 1920
|
||||
local bg_height = 1080
|
||||
|
||||
local cloud_width = 1800
|
||||
local cloud_height = 540
|
||||
local cloud_width = 1200
|
||||
local cloud_height = 360
|
||||
|
||||
local cloud_count = 11
|
||||
|
||||
@ -168,8 +168,6 @@ function draw()
|
||||
func:DrawGraph(0, 0 - ((mountainScale - 1.0) * bg_height), "Background_Mountain_"..tostring(index + 1)..".png")
|
||||
|
||||
|
||||
func:SetScale(0.65, 0.65, "Cloud.png")
|
||||
|
||||
cloudOpacity = 0
|
||||
if commonCounter >= mountainAppearValue and isClear[0] then
|
||||
cloudOpacity = math.min(255, math.max(0, commonCounter - mountainAppearValue))
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 61 KiB After Width: | Height: | Size: 25 KiB |
@ -13,8 +13,8 @@
|
||||
local bg_width = 1280
|
||||
local bg_height = 720
|
||||
|
||||
local cloud_width = 1200
|
||||
local cloud_height = 360
|
||||
local cloud_width = 800
|
||||
local cloud_height = 240
|
||||
|
||||
local cloud_count = 11
|
||||
|
||||
@ -168,8 +168,6 @@ function draw()
|
||||
func:DrawGraph(0, 0 - ((mountainScale - 1.0) * bg_height), "Background_Mountain_"..tostring(index + 1)..".png")
|
||||
|
||||
|
||||
func:SetScale(0.65, 0.65, "Cloud.png")
|
||||
|
||||
cloudOpacity = 0
|
||||
if commonCounter >= mountainAppearValue and isClear[0] then
|
||||
cloudOpacity = math.min(255, math.max(0, commonCounter - mountainAppearValue))
|
||||
|
@ -1748,7 +1748,7 @@ namespace TJAPlayer3
|
||||
//----------------------------------------
|
||||
#endif
|
||||
this.strDTXManiaのバージョン = "Unknown";
|
||||
this.str曲データ検索パス = @"." + Path.DirectorySeparatorChar + "Songs" + Path.DirectorySeparatorChar;
|
||||
this.str曲データ検索パス = "Songs" + Path.DirectorySeparatorChar;
|
||||
this.b全画面モード = false;
|
||||
this.b垂直帰線待ちを行う = true;
|
||||
this.n初期ウィンドウ開始位置X = 100; // #30675 2013.02.04 ikanick add
|
||||
@ -1897,7 +1897,7 @@ namespace TJAPlayer3
|
||||
this.bIsEnabledSystemMenu = true; // #28200 2012.5.1 yyagi System Menuの利用可否切替(使用可)
|
||||
this.strSystemSkinSubfolderFullName = ""; // #28195 2012.5.2 yyagi 使用中のSkinサブフォルダ名
|
||||
this.bTight = false; // #29500 2012.9.11 kairera0467 TIGHTモード
|
||||
nGraphicsDeviceType = (int)GraphicsDeviceType.OpenGL;
|
||||
nGraphicsDeviceType = 0;
|
||||
#region [ WASAPI/ASIO ]
|
||||
this.nSoundDeviceType = (int)ESoundDeviceTypeForConfig.Bass; // #24820 2012.12.23 yyagi 初期値はACM | #31927 2013.8.25 yyagi OSにより初期値変更
|
||||
nBassBufferSizeMs = 1;
|
||||
@ -2103,11 +2103,8 @@ namespace TJAPlayer3
|
||||
#endregion
|
||||
|
||||
#region [ Window関連 ]
|
||||
sw.WriteLine("; これは実験的な機能です");
|
||||
sw.WriteLine("; 使用する描画API(0=OpenGL, 1=Vulkan, 2=DirectX11, 3=DirectX12)");
|
||||
sw.WriteLine("; OpenGLは遅いが互換性が高く安定する、VulkanはLinuxで最速");
|
||||
sw.WriteLine("; DirectX11はWindows限定だが安定していて早い、DirectX12はWindows限定な上GPUが良くないと動かないが爆速");
|
||||
sw.WriteLine("; もう一回言いますこれは実験的な機能です");
|
||||
//sw.WriteLine("; 使用する描画API(0=OpenGL, 1=DirectX9, 2=DirectX11, 3=Vulkan, 4=Metal)");
|
||||
sw.WriteLine("; 使用する描画API(0=OpenGL, 1=DirectX11, 2=Vulkan, 3=Metal)");
|
||||
sw.WriteLine( "GraphicsDeviceType={0}", (int) this.nGraphicsDeviceType );
|
||||
sw.WriteLine();
|
||||
sw.WriteLine( "; 画面モード(0:ウィンドウ, 1:全画面)" );
|
||||
@ -2949,7 +2946,7 @@ namespace TJAPlayer3
|
||||
#region [ Window関係 ]
|
||||
else if ( str3.Equals( "GraphicsDeviceType" ) )
|
||||
{
|
||||
this.nGraphicsDeviceType = CConversion.n値を文字列から取得して範囲内に丸めて返す( str4, 0, 3, this.nGraphicsDeviceType );
|
||||
this.nGraphicsDeviceType = CConversion.n値を文字列から取得して範囲内に丸めて返す( str4, 0, 4, this.nGraphicsDeviceType );
|
||||
}
|
||||
else if (str3.Equals("FullScreen"))
|
||||
{
|
||||
|
@ -636,9 +636,37 @@ namespace TJAPlayer3
|
||||
Trace.TraceError( "例外が発生しましたが処理を継続します。 (b8d93255-bbe4-4ca3-8264-7ee5175b19f3)" );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
GraphicsDeviceType_ = (GraphicsDeviceType)ConfigIni.nGraphicsDeviceType;
|
||||
switch(ConfigIni.nGraphicsDeviceType)
|
||||
{
|
||||
case 0:
|
||||
GraphicsDeviceType_ = Silk.NET.GLFW.AnglePlatformType.OpenGL;
|
||||
break;
|
||||
/*
|
||||
case 1:
|
||||
GraphicsDeviceType_ = Silk.NET.GLFW.AnglePlatformType.D3D9;
|
||||
break;
|
||||
case 2:
|
||||
GraphicsDeviceType_ = Silk.NET.GLFW.AnglePlatformType.D3D11;
|
||||
break;
|
||||
case 3:
|
||||
GraphicsDeviceType_ = Silk.NET.GLFW.AnglePlatformType.Vulkan;
|
||||
break;
|
||||
case 4:
|
||||
GraphicsDeviceType_ = Silk.NET.GLFW.AnglePlatformType.Metal;
|
||||
break;
|
||||
*/
|
||||
case 1:
|
||||
GraphicsDeviceType_ = Silk.NET.GLFW.AnglePlatformType.D3D11;
|
||||
break;
|
||||
case 2:
|
||||
GraphicsDeviceType_ = Silk.NET.GLFW.AnglePlatformType.Vulkan;
|
||||
break;
|
||||
case 3:
|
||||
GraphicsDeviceType_ = Silk.NET.GLFW.AnglePlatformType.Metal;
|
||||
break;
|
||||
}
|
||||
|
||||
WindowPosition = new Silk.NET.Maths.Vector2D<int>(ConfigIni.n初期ウィンドウ開始位置X, ConfigIni.n初期ウィンドウ開始位置Y);
|
||||
WindowSize = new Silk.NET.Maths.Vector2D<int>(ConfigIni.nウインドウwidth, ConfigIni.nウインドウheight);
|
||||
FullScreen = ConfigIni.b全画面モード;
|
||||
@ -732,6 +760,10 @@ namespace TJAPlayer3
|
||||
}
|
||||
protected override void Draw()
|
||||
{
|
||||
#if !DEBUG
|
||||
try
|
||||
#endif
|
||||
{
|
||||
// Sound管理?.t再生中の処理をする();
|
||||
Timer?.Update();
|
||||
SoundManager.PlayTimer?.Update();
|
||||
@ -2358,6 +2390,18 @@ for (int i = 0; i < 3; i++) {
|
||||
this.b次のタイミングで垂直帰線同期切り替えを行う = false;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
#if !DEBUG
|
||||
catch( Exception e )
|
||||
{
|
||||
Trace.WriteLine( "" );
|
||||
Trace.Write( e.ToString() );
|
||||
Trace.WriteLine( "" );
|
||||
Trace.WriteLine( "エラーだゴメン!(涙" );
|
||||
AssemblyName asmApp = Assembly.GetExecutingAssembly().GetName();
|
||||
throw e;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// その他
|
||||
|
@ -219,13 +219,14 @@ namespace TJAPlayer3
|
||||
[9995] = "非同期テクスチャ読み込み",
|
||||
|
||||
[9996] = "描画の方式:\n" +
|
||||
"OpenGL, Vulkan, DirectX11, DirectX12\n" +
|
||||
"OpenGL, DirectX11, Vulkan, Metal\n" +
|
||||
"の中からグラフィックスAPIを選択します。\n" +
|
||||
"OpenGLは遅いですが互換性が高く安定してます\n" +
|
||||
"VulkanはLinux環境で最速です\n" +
|
||||
//"DirectX9は古いデバイスで高速です、\n" +
|
||||
"DirectX11はWindowsでしか動作しませんが、\n" +
|
||||
"早くて安定しています\n" +
|
||||
"DirectX12はWindowsでしか動作しませんが非常に高速です\n" +
|
||||
"VulkanはWindowsとLinux環境で最速です\n" +
|
||||
"MetalはMacのみですが最速です\n" +
|
||||
"\n" +
|
||||
"この変更は再起動後に反映されます\n",
|
||||
[9997] = "グラフィックスAPI",
|
||||
|
@ -138,14 +138,28 @@ namespace TJAPlayer3
|
||||
void loadTexture()
|
||||
{
|
||||
this.list進行文字列.Add("LOADING TEXTURES...");
|
||||
|
||||
try
|
||||
{
|
||||
TJAPlayer3.Tx.LoadTexture();
|
||||
|
||||
this.list進行文字列.Add("LOADING TEXTURES...OK");
|
||||
this.str現在進行中 = "Setup done.";
|
||||
this.eフェーズID = Eフェーズ.起動7_完了;
|
||||
TJAPlayer3.Skin.bgm起動画面.t停止する();
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
TJAPlayer3.Skin.bgm起動画面.t停止する();
|
||||
|
||||
Trace.TraceError( exception.ToString() );
|
||||
this.list進行文字列.Add("LOADING TEXTURES...NG");
|
||||
foreach(var text in exception.ToString().Split('\n'))
|
||||
{
|
||||
this.list進行文字列.Add(text);
|
||||
}
|
||||
}
|
||||
|
||||
this.list進行文字列.Add("LOADING TEXTURES...OK");
|
||||
this.str現在進行中 = "Setup done.";
|
||||
this.eフェーズID = Eフェーズ.起動7_完了;
|
||||
|
@ -109,12 +109,11 @@ namespace TJAPlayer3
|
||||
CLangManager.LangInstance.GetString(18));
|
||||
this.list項目リスト.Add( this.iSystemTimeStretch );
|
||||
|
||||
/*
|
||||
this.iSystemGraphicsType = new CItemList(CLangManager.LangInstance.GetString(9997), CItemList.Eパネル種別.通常, TJAPlayer3.ConfigIni.nGraphicsDeviceType,
|
||||
CLangManager.LangInstance.GetString(9996),
|
||||
new string[] { "OpenGL", "Vulkan", "DirectX11", "DirectX12" });
|
||||
//new string[] { "OpenGL", "DirectX9", "DirectX11", "Vulkan", "Metal" });
|
||||
new string[] { "OpenGL", "DirectX11", "Vulkan", "Metal" });
|
||||
this.list項目リスト.Add(this.iSystemGraphicsType);
|
||||
*/
|
||||
|
||||
this.iSystemFullscreen = new CItemToggle(CLangManager.LangInstance.GetString(10019), TJAPlayer3.ConfigIni.b全画面モード,
|
||||
CLangManager.LangInstance.GetString(19));
|
||||
@ -1619,7 +1618,7 @@ namespace TJAPlayer3
|
||||
//CDTXMania.ConfigIni.eDark = (Eダークモード) this.iCommonDark.n現在選択されている項目番号;
|
||||
TJAPlayer3.ConfigIni.n演奏速度 = this.iCommonPlaySpeed.n現在の値;
|
||||
|
||||
//TJAPlayer3.ConfigIni.nGraphicsDeviceType = this.iSystemGraphicsType.n現在選択されている項目番号;
|
||||
TJAPlayer3.ConfigIni.nGraphicsDeviceType = this.iSystemGraphicsType.n現在選択されている項目番号;
|
||||
TJAPlayer3.ConfigIni.b全画面モード = this.iSystemFullscreen.bON;
|
||||
// TJAPlayer3.ConfigIni.bSTAGEFAILED有効 = this.iSystemStageFailed.bON;
|
||||
TJAPlayer3.ConfigIni.bランダムセレクトで子BOXを検索対象とする = this.iSystemRandomFromSubBox.bON;
|
||||
|
Loading…
x
Reference in New Issue
Block a user