Hardware Multisampling (MSAA) with OpenB3D/OpenGL

BlitzMax Forums/MiniB3D Module/Hardware Multisampling (MSAA) with OpenB3D/OpenGL

RustyKristi(Posted 2016) [#1]
Continuing this topic here about AA with OpenB3D and going back to ARB, I did some snooping around and found out that this is somehow related to Hardware Multisampling with OpenGL?

So basically this is just enabling Hardware MSAA and just thought they did the same with MiniB3D's Antialias command if I'm not mistaken.

glEnable(GL_MULTISAMPLE)
or
glEnable(GL_MULTISAMPLE_ARB)

I'm sure they are more steps on that, but that is basically the setup.

further reading:

http://nehe.gamedev.net/tutorial/fullscreen_antialiasing/16008/
http://www.learnopengl.com/#!Advanced-OpenGL/Anti-aliasing

It's kind of irky that MiniB3d got this antialiasing available and the shaders with OpenB3D. Given that you can do PostFX AA using shaders but hardware multisampling seems to be efficient, at least for a default AA.


Kryzon(Posted 2016) [#2]
In order for the multisampling feature to be available at all, the OpenGL context (the "graphics mode") needs to be created with a specific multisample amount (2x, 4x, 16x etc.).
If you don't do that, using the glEnable or glDisable commands will make no difference.

http://www.blitzbasic.com/Community/posts.php?topic=64265


RustyKristi(Posted 2016) [#3]
I agree, that's why I mentioned that there are more steps on that. Thanks for the link. :D

Do you think this will work with OpenB3D? It looks like it should work

http://www.leadwerks.com/werkspace/topic/5143-blitzmax-arb-fsaa-ready-to-use-modules-real-antialias/


angros47(Posted 2016) [#4]
The way of implementing anti alias in original MiniB3D was to call the camera render function many times, shifting the picture by one pixel, and using the accumulation buffer to implement a convolution filter. This was not an efficient solution, because all entities were checked for visibility many times, with no need.

But implementing the same solution on OpenB3D (I remind OOpenB3D was originally based on iMiniB3D , that had no anti alias support, too) would have caused issues with the new functionalities I added, mainly terrains and shadows. In fact, terrain geometry is built from scratch at each frame, to provide the best LOD for current view. By calling the render routine many times, it would have been unnecessarily rebuilt many times, with a serious slowdown. The same applies to shadows (a volumetric shadow needs to be rebuilt each time the mesh or the light are moved). So, I needed to find another solution anyway, and the simplest one was to rely on the existing features of opengl. The right solution would be to render to a texture, then render the texture on the accumulation buffer many times to simulate a convolution filter; it would work like in MiniB3D, with the benefit of rendering the scene only once per frame, and use the same image. Actually, would be a sort of post processing without shaders. But I haven't implemented that in the core of the library, because there are many other ways a textured quad could be used, and I still am not sure on how implementing a generic all purposes solution.


RustyKristi(Posted 2016) [#5]
Thanks for the info angros47, so does this mean that it is more practical and efficient to implement or work on something like a antialias shader/postfx with OpenB3D?

I have not tested yet the Nvidia control panel to do this manually but figured if this can be done without external setup.


angros47(Posted 2016) [#6]
A well programmed shader would be more efficient, because it will use the GPU (that works with parallel processing) while the CPU can do other things. But you need a system with a programmable GPU: some drivers (like Mesa) provide a software fall back, but if the processor has to emulate a GPU (in a very inefficient way, since emulating a parallel execution cannot be fast) it would cause a serious slow down. So, it's better to just "hint" the driver to use multisampling: if the hardware supports it, you get multisampling, otherwise, the image will have some artifacts, but will still be rendered in acceptable time.

It's always possible to use the accumulation buffer, as in MiniB3D, but rendering in two steps (as it's done in post processing), to avoid repeating the most time-consuming operation. A convolution filter in theory should allow much more tricks, not only antialiasing. Maybe I could consider adding some feature like that... but not now. At the moment, my priority is a version that can replace the fixed function pipeline (as MiniB3D for Monkey did some years ago), otherwise there will be no future for OpenB3D.


Kryzon(Posted 2016) [#7]
From what I've read, multisampling is the fastest and safest choice for antialiasing since it's built-in.

Do you think this will work with OpenB3D?

Whether it's OpenB3D or another open source engine, you need to [test for compatibility and if available,] create a graphics context using a pixel format with one of the multisampling modes.


RustyKristi(Posted 2016) [#8]
Ok with the postfx camera features already in place, I'm glad there is still a great option here. Either way, setting it via the GPU's control panel is still my second fallback if that would work.

I agree, I was about to ask for using deferred rendering with openb3d so there's a possibility to create some shaders like PSSM shadows.

@Kryzon

Thanks, I was referring to JoshK's old code if that would work out of the box. I see a sample posted with a cube being anti-aliased (I dont know if thats minib3d or openb3d) but I'm curious to see the bottom result.


Kryzon(Posted 2016) [#9]
It should work with the BlitzMax wrapper module for OpenB3D, since that uses the Graphics command from the BRL modules.
https://github.com/markcwm/openb3d.mod/blob/master/openb3d.mod/inc/TGlobal.bmx#L147

JoshK's modification affects that Graphics function, adding the multisampling flags.
It would be necessary to change the Graphics3D function from the OpenB3D module to accept those flags as well.


RustyKristi(Posted 2016) [#10]
Thanks Kryzon! :) I assume by that thread where you have participated that you have tried this before?

Anyways, this would be awesome if it really work without any complex modification.


RustyKristi(Posted 2016) [#11]
Ok so adding the latest modules by Joshk, I get these errors when I try to build my app:

C:/BlitzMax/mod/brl.mod/glgraphics.mod/glgraphics.release.win32.x86.a(glgraphics.bmx.release.win32.x86.o):(code+0xb13): undefined reference to `bbGLGraphicsShareContexts'

No problems rebuilding all modules, just the app with this error.


RustyKristi(Posted 2016) [#12]
It looks like the .C file is already outdated.

Made some changes but it is not enough. Maybe somebody can pick it up. It looks like it is close I can't get this wglChoosePixelFormatARB part to work

static BOOL _wglChoosePixelFormatARB( int hDC, const int *intAttribs, const FLOAT *floatAttribs, unsigned int maxFormats, int *lPixelFormat, unsigned int *numFormats)
{
	//Define function pointer datatype
	typedef BOOL (APIENTRY * WGLCHOOSEPIXELFORMATARB) (int hDC, const int *intAttribs, const FLOAT *floatAttribs, unsigned int maxFormats, int *lPixelFormat, unsigned int *numFormats);

	//Get the "wglChoosePixelFormatARB" function
	WGLCHOOSEPIXELFORMATARB wglChoosePixelFormatARB = (WGLCHOOSEPIXELFORMATARB)wglGetProcAddress("wglChoosePixelFormatARB");
	if(wglChoosePixelFormatARB)
		return wglChoosePixelFormatARB(hDC, intAttribs, floatAttribs, maxFormats, lPixelFormat, numFormats);
	else
		MessageBox(0,"wglChoosePixelFormatARB() function not found!","Error",0);
		return 0;
}


http://s000.tinyupload.com/index.php?file_id=78441440943726650095