Reputation: 372
I am trying to use SharpGL to display a video. The video is a list of image (OpenCV image that I convert to Bitmap).
2 calls are slow :
I think I am not using the hardware acceleration but my video card is a Nvidia 3080 with the latest drivers installed on Windows 10.
It's hard to find information about SharpGL :/ Maybe I should use DirectX ?
My code :
<Border HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Red" Name="BorderControl" SizeChanged="BorderControlSizeChanged">
<sharpGL:OpenGLControl OpenGLDraw="OpenGLControl_OpenGLDraw" Name="OpenGLControl" Resized="openGLControl1_Resized" Width="500" Height="500" DrawFPS="True">
</sharpGL:OpenGLControl>
</Border>
private void OpenGLControl_OpenGLDraw(object sender, OpenGLRoutedEventArgs args)
{
long time = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
long time0 = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
MutexWaitOne();
time0 = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond - time0;
long time1 = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
OpenGL gl = args.OpenGL;
gl.Enable(OpenGL.GL_TEXTURE_2D);
gl.Disable(OpenGL.GL_DEPTH_TEST);
time1 = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond - time1;
long time2 = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
int Width = (int)OpenGLControl.Width;
int Height = (int)OpenGLControl.Height;
gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT);
gl.LoadIdentity();
time2 = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond - time2;
long time3 = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
texture.Destroy(gl);
time3 = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond - time3;
long time4 = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
if (_cameraImagebuffer != null)
{
try
{
texture.Create(gl, _cameraImagebufferBitmap);
}catch(Exception ex)
{
MutexRelease();
return;
}
}
gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_LINEAR);
gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_LINEAR);
time4 = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond - time4;
long time5 = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
texture.Bind(gl);
time5 = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond - time5;
long time6 = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
gl.Begin(OpenGL.GL_QUADS);
gl.TexCoord(0.0f, 0.0f); gl.Vertex(0, 0, 0);
gl.TexCoord(1.0f, 0.0f); gl.Vertex(Width, 0, 0);
gl.TexCoord(1.0f, 1.0f); gl.Vertex(Width, Height, 0);
gl.TexCoord(0.0f, 1.0f); gl.Vertex(0, Height, 0);
gl.End();
time6 = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond - time6;
long time7 = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
gl.Flush();
time7 = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond - time7;
MutexRelease();
time = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond - time;
Console.WriteLine(String.Format("OPENGL: Total : {0} step0:{1} step1:{2} step2:{3} step3:{4} step4:{5} step5:{6} step6:{7} step7:{8}", time, time0, time1, time2, time3, time4, time5, time6, time7));
}
Upvotes: 0
Views: 272
Reputation: 67449
Oh wow that is an incredibly inefficient code, for a whole multitude of reasons:
Stop deleting and recreating your texture objects. You should be reusing them instead with glTextureSubImage2D
.
Never use glFlush
. Ever. It will stall your CPU waiting for what should be background processing on the GPU.
It's almost 2023 and you're still using glBegin
/glEnd
/glVertex
? Use a vertex array object instead. Especially with camera data that comes in as YUV420p, are you wasting even more time converting it to RGB on the CPU when you could be doing it in a fragment shader?
What's with the random mutex?
Upvotes: 2