-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrameBuffer.cs
More file actions
212 lines (200 loc) · 9.58 KB
/
FrameBuffer.cs
File metadata and controls
212 lines (200 loc) · 9.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
using JLUtility;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;
using OpenTK.Windowing.Desktop;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JLGraphics
{
public struct TFP
{
public PixelInternalFormat internalFormat;
public TextureMinFilter minFilter;
public TextureMagFilter magFilter;
public TextureWrapMode wrapMode;
public Vector4 borderColor;
public int maxMipmap;
public bool isShadowMap;
public static TFP Default => new TFP()
{
internalFormat = PixelInternalFormat.Rgb16f,
minFilter = TextureMinFilter.Nearest,
magFilter = TextureMagFilter.Nearest,
wrapMode = TextureWrapMode.Repeat,
borderColor = Vector4.Zero,
maxMipmap = 0,
};
public TFP()
{
internalFormat = PixelInternalFormat.Rgb16f;
minFilter = TextureMinFilter.Nearest;
magFilter = TextureMagFilter.Nearest;
wrapMode = TextureWrapMode.Repeat;
borderColor = Vector4.Zero;
maxMipmap = 0;
}
}
public class FrameBuffer : SafeDispose, IGlobalScope
{
internal Texture[] TextureAttachments { get; } = null;
DrawBuffersEnum[] ColorAttachmentsEnums = null;
internal int FrameBufferObject { get; } = 0;
internal int RenderBufferObject { get; } = 0;
public int Width { get; } = 0;
public int Height { get; } = 0;
public override string Name => "FrameBuffer:" + FrameBufferObject + " " + NameAddon;
public string NameAddon { get; private set; } = "";
public void SetName(string name)
{
NameAddon = name;
}
static bool IsDepthComponent(PixelInternalFormat internalPixelFormat)
{
return
internalPixelFormat == PixelInternalFormat.DepthComponent
|| internalPixelFormat == PixelInternalFormat.Depth24Stencil8
|| internalPixelFormat == PixelInternalFormat.Depth32fStencil8
|| internalPixelFormat == PixelInternalFormat.DepthComponent16
|| internalPixelFormat == PixelInternalFormat.DepthComponent16Sgix
|| internalPixelFormat == PixelInternalFormat.DepthComponent24
|| internalPixelFormat == PixelInternalFormat.DepthComponent24Sgix
|| internalPixelFormat == PixelInternalFormat.DepthComponent32
|| internalPixelFormat == PixelInternalFormat.DepthComponent32f
|| internalPixelFormat == PixelInternalFormat.DepthComponent32Sgix
|| internalPixelFormat == PixelInternalFormat.DepthStencil;
}
public FrameBuffer(int width, int height, bool enableDepthRenderBuffer = false, params TFP[] textureFormat) : this(width, height, 0, enableDepthRenderBuffer, textureFormat)
{
}
public FrameBuffer(int width, int height, int MSAA, bool enableDepthRenderBuffer = false, params TFP[] textureFormat)
{
Width = width;
Height = height;
int colorAttachmentCount = MathHelper.Clamp(textureFormat.Length, 0, 16);
FrameBufferObject = GL.GenFramebuffer();
GL.BindFramebuffer(FramebufferTarget.Framebuffer, FrameBufferObject);
TextureAttachments = new Texture[colorAttachmentCount];
int index = 0;
for (int i = 0; i < colorAttachmentCount; i++)
{
TextureAttachments[i] = new Texture();
TextureAttachments[i].textureTarget = MSAA > 0 ? TextureTarget.Texture2DMultisample : TextureTarget.Texture2D;
TextureAttachments[i].Width = width;
TextureAttachments[i].Height = height;
TextureAttachments[i].internalPixelFormat = textureFormat[i].internalFormat;
TextureAttachments[i].textureMagFilter = textureFormat[i].magFilter;
TextureAttachments[i].textureMinFilter = textureFormat[i].minFilter;
TextureAttachments[i].textureWrapMode = textureFormat[i].wrapMode;
TextureAttachments[i].mipmapLevels = textureFormat[i].maxMipmap;
TextureAttachments[i].generateMipMaps = textureFormat[i].maxMipmap != 0;
TextureAttachments[i].borderColor = textureFormat[i].borderColor;
TextureAttachments[i].ResolveTexture(textureFormat[i].isShadowMap, MSAA);
if (IsDepthComponent(TextureAttachments[i].internalPixelFormat))
{
GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, TextureAttachments[i].textureTarget, TextureAttachments[i].GlTextureID, 0);
}
else
{
FramebufferAttachment framebufferAttachment = FramebufferAttachment.ColorAttachment0 + index;
GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, framebufferAttachment, TextureAttachments[i].textureTarget, TextureAttachments[i].GlTextureID, 0);
index++;
}
}
if (enableDepthRenderBuffer)
{
RenderbufferStorage renderbufferStorage = RenderbufferStorage.DepthComponent24;
RenderBufferObject = GL.GenRenderbuffer();
GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, RenderBufferObject);
if(MSAA > 0)
GL.RenderbufferStorageMultisample(RenderbufferTarget.Renderbuffer, MSAA, renderbufferStorage, width, height);
else
GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, renderbufferStorage, width, height);
GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, RenderbufferTarget.Renderbuffer, RenderBufferObject);
}
GL.DrawBuffer(DrawBufferMode.None);
if(colorAttachmentCount != 0)
{
List<DrawBuffersEnum> attachments = new List<DrawBuffersEnum>(colorAttachmentCount);
for (int i = 0; i < colorAttachmentCount; i++)
{
if (IsDepthComponent(TextureAttachments[i].internalPixelFormat))
{
continue;
}
attachments.Add(DrawBuffersEnum.ColorAttachment0 + i);
}
if(attachments.Count > 0)
{
ColorAttachmentsEnums = attachments.ToArray();
}
}
if (GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer) != FramebufferErrorCode.FramebufferComplete)
Debug.Log("Framebuffer is not complete!", Debug.Flag.Error);
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
}
public static void BindFramebuffer(FrameBuffer frameBuffer)
{
GL.BindFramebuffer(FramebufferTarget.Framebuffer, frameBuffer.FrameBufferObject);
if(frameBuffer.ColorAttachmentsEnums != null)
{
GL.DrawBuffers(frameBuffer.ColorAttachmentsEnums.Length, frameBuffer.ColorAttachmentsEnums);
}
GL.ReadBuffer(ReadBufferMode.None);
GL.Viewport(0, 0, frameBuffer.Width, frameBuffer.Height);
}
protected override void OnDispose()
{
GL.DeleteFramebuffer(FrameBufferObject);
GL.DeleteRenderbuffer(RenderBufferObject);
for (int i = 0; i < TextureAttachments.Length; i++)
{
TextureAttachments[i].Dispose();
}
}
public static FrameBuffer CopyFirstColorAttachment(FrameBuffer src, float resolutionScale, bool enableDepthRenderBuffer = false)
{
FrameBuffer frameBuffer1;
var tfp = TFP.Default;
tfp.internalFormat = src.TextureAttachments[0].internalPixelFormat;
tfp.magFilter = src.TextureAttachments[0].textureMagFilter;
tfp.minFilter = src.TextureAttachments[0].textureMinFilter;
tfp.maxMipmap = src.TextureAttachments[0].mipmapLevels;
tfp.wrapMode = src.TextureAttachments[0].textureWrapMode;
tfp.isShadowMap = false;
tfp.borderColor = src.TextureAttachments[0].borderColor;
int width = (int)MathHelper.Ceiling(src.TextureAttachments[0].Width * resolutionScale);
int height = (int)MathHelper.Ceiling(src.TextureAttachments[0].Height * resolutionScale);
frameBuffer1 = new FrameBuffer(width, height, enableDepthRenderBuffer, tfp);
return frameBuffer1;
}
public static Vector2i GetScaledResolution(int width, int height, float scale)
{
return new Vector2i((int)MathF.Max(MathF.Floor(width * scale), 1), (int)MathF.Max(MathF.Floor(height * scale), 1));
}
public static bool AlikeResolution(FrameBuffer f1, FrameBuffer f2, float f1_resolutionScale = 1.0f)
{
if (f1 == null && f2 != null)
{
return false;
}
if (f2 == null && f1 != null)
{
return false;
}
if (f2 == null && f1 == null)
{
return true;
}
var res = GetScaledResolution(f2.Width, f2.Height, f1_resolutionScale);
bool resolutionCheck = f1.Width == res.X && f1.Height == res.Y;
if (!resolutionCheck)
{
return false;
}
return true;
}
}
}