Skip to content

Commit

Permalink
fix iGPUs on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
parzivail committed Jul 24, 2023
1 parent 1869cbe commit 6cdb643
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 50 deletions.
2 changes: 1 addition & 1 deletion ModelPainter/ModelPainter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<ItemGroup>
<PackageReference Include="JeremyAnsel.Media.WavefrontObj" Version="2.0.19"/>
<PackageReference Include="minecraft-dotnet.Substrate" Version="2.0.44-alpha"/>
<PackageReference Include="OpenTK.GLControl" Version="3.1.0"/>
<PackageReference Include="OpenTK.GLControl" Version="3.3.3"/>
<PackageReference Include="SkiaSharp" Version="2.88.0-preview.187"/>
<PackageReference Include="SkiaSharp.Views.WindowsForms" Version="2.88.0-preview.187"/>
<PackageReference Include="System.Drawing.Common" Version="6.0.2-mauipre.1.22054.8"/>
Expand Down
27 changes: 9 additions & 18 deletions ModelPainter/Render/Framebuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public int Samples
}
}

public Framebuffer(int samples, PixelInternalFormat internalFormat = PixelInternalFormat.Rgba, PixelFormat format = PixelFormat.Rgba, PixelType pixelType = PixelType.UnsignedByte,
public Framebuffer(int samples, PixelInternalFormat internalFormat = PixelInternalFormat.Rgba, PixelFormat format = PixelFormat.Rgba,
PixelType pixelType = PixelType.UnsignedByte,
int unboundFbo = 0)
{
_unboundFbo = unboundFbo;
Expand All @@ -49,31 +50,21 @@ public void Init(int width, int height)

Use();

if (_samples == 1)
{
GL.BindTexture(TextureTarget.Texture2D, Texture);

GL.TexImage2D(TextureTarget.Texture2D, 0, _internalFormat, width, height, 0, _format, _pixelType, IntPtr.Zero);
GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, Texture, 0);
}
else
{
GL.BindTexture(TextureTarget.Texture2DMultisample, Texture);

GL.TexImage2DMultisample(TextureTargetMultisample.Texture2DMultisample, _samples, _internalFormat, width, height, true);
GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2DMultisample, Texture, 0);
}
GL.BindTexture(TextureTarget.Texture2D, Texture);
GL.TexImage2D(TextureTarget.Texture2D, 0, _internalFormat, width, height, 0, _format, _pixelType, IntPtr.Zero);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, Texture, 0);

GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, DepthId);
GL.RenderbufferStorageMultisample(RenderbufferTarget.Renderbuffer, _samples, RenderbufferStorage.Depth24Stencil8, width, height);
GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, 0);
GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, RenderbufferStorage.Depth24Stencil8, width, height);
GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthStencilAttachment, RenderbufferTarget.Renderbuffer, DepthId);

var status = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer);
if (status != FramebufferErrorCode.FramebufferComplete)
throw new ApplicationException($"Framebuffer status expected to be FramebufferComplete, instead was {status}");

GL.BindTexture(_samples == 1 ? TextureTarget.Texture2D : TextureTarget.Texture2DMultisample, 0);
GL.BindTexture(TextureTarget.Texture2D, 0);
Release();
}

Expand Down
8 changes: 4 additions & 4 deletions ModelPainter/Render/ModelRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Drawing.Imaging;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using ModelPainter.Render.Shader;
using ModelPainter.Resources;
Expand Down Expand Up @@ -112,7 +113,7 @@ private void DrawFullscreenQuad()
GL.Enable(EnableCap.Texture2D);

GL.ActiveTexture(TextureUnit.Texture0);
GL.BindTexture(_viewFbo.Samples == 1 ? TextureTarget.Texture2D : TextureTarget.Texture2DMultisample, _viewFbo.Texture);
GL.BindTexture(TextureTarget.Texture2D, _viewFbo.Texture);

GL.BindVertexArray(_screenVao);
GL.DrawArrays(PrimitiveType.Triangles, 0, 6);
Expand Down Expand Up @@ -185,7 +186,6 @@ public void Render(bool useAlphaTestSelectMode)

_shaderScreen = new ShaderProgram(ResourceHelper.GetLocalStringResource("screen.frag"), ResourceHelper.GetLocalStringResource("screen.vert"));
_shaderScreen.Uniforms.SetValue("texScene", 0);
_shaderScreen.Uniforms.SetValue("samplesScene", _viewFbo.Samples);

_shaderModel = new ShaderProgram(ResourceHelper.GetLocalStringResource("model.frag"), ResourceHelper.GetLocalStringResource("model.vert"));
_shaderModel.Uniforms.SetValue("texModel", 1);
Expand Down Expand Up @@ -428,7 +428,7 @@ private static void OnGlMessage(DebugSource source, DebugType type, int id, Debu
return;

var msg = Marshal.PtrToStringAnsi(message, length);
Console.WriteLine(msg);
Debug.WriteLine(msg);
}

private static void RenderOriginAxes()
Expand Down
2 changes: 1 addition & 1 deletion ModelPainter/Render/PixelBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void Read()
var bufferByteSize = _data.Length * sizeof(T);

GL.BindBuffer(BufferTarget.PixelPackBuffer, _bufferId);
GL.BufferData(BufferTarget.PixelPackBuffer, bufferByteSize, IntPtr.Zero, BufferUsageHint.StaticRead);
GL.BufferData(BufferTarget.PixelPackBuffer, bufferByteSize, IntPtr.Zero, BufferUsageHint.StreamCopy);
GL.ReadPixels(0, 0, _width, _height, _pixelFormat, _pixelType, IntPtr.Zero);
GL.BindBuffer(BufferTarget.PixelPackBuffer, 0);
}
Expand Down
28 changes: 2 additions & 26 deletions ModelPainter/Resources/screen.frag
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,12 @@ out vec4 FragColor;

in vec2 TexCoords;

uniform sampler2DMS texScene;
uniform sampler2D texScene;
uniform int width;
uniform int height;
uniform int samplesScene;

vec4 mtexture(sampler2DMS s, vec2 coords, int samp)
{
ivec2 vpCoords = ivec2(width, height);
vpCoords.x = int(vpCoords.x * coords.x);
vpCoords.y = int(vpCoords.y * coords.y);

vec4 avg = vec4(0);
for (int i = 0; i < samp; i++)
{
avg += texelFetch(s, vpCoords, i);
}
return avg / float(samp);
}

float linearDepth(float depthSample)
{
const float zNear = 1;
const float zFar = 1024;
depthSample = 2.0 * depthSample - 1.0;
float zLinear = 2.0 * zNear * zFar / (zFar + zNear - depthSample * (zFar - zNear));
return zLinear;
}

void main()
{
vec4 color = mtexture(texScene, TexCoords, samplesScene);
vec4 color = texture(texScene, TexCoords);
FragColor = color;//vec4(TexCoords.x, TexCoords.y, 0., 1.);
}

0 comments on commit 6cdb643

Please sign in to comment.