Graphical Elements Flickering on W10 / NG / OpenGL

Started by sonic, June 04, 2020, 20:33:31

Previous topic - Next topic

sonic

hi there,

i've recently moved to Windows and i'm trying to get a solid 64bit NG build of my toolset that was previously made on Mac BMax 1.51. after a lot of messing about i've got everything up and running but i have a potential showstopper ...

i'm using OpenGL as i need an FBO module that i wouldn't have the skills to rewrite. what i'm seeing, using VSync or not, is elements of my screen disappearing for a single frame, randomly every couple of seconds (or maybe being rendered with a different colour or alpha, it's hard to know what the glitches actually look like as they're always just for 1 frame.) this applies to text, rectangles and even drawimage commands. elements of my UI (this is a toolset) keep flickering and i was hoping to release / possibly even sell the tool but without a stable image i can't.

the build works fine in 1.51, on PC or Mac, but of course is stuck in 32 bit. i'm on the latest drivers for my 2080 Super. not using MaxGUI, everything is constructed from Max2D commands. is there anything i should look into?

thanks again to Bruce and everyone maintaining the project. i really appreciate your work.

Derron

To simply test stuff:

Could you wrap your DrawImage() or prepend a "SetColor 255,255,255;SetAlpha 1.0" before drawing these "sometimes becoming invisible" elements?

If stuff becomes "randomly" invisible then this does not sound like a hardware / driver problem but simply ..an issue in how stuff is drawn. Maybe you have something which fades out but forgets to reset the alpha value back after drawing.

SetAlpha enemyImgAlphaFade
DrawImage(enemyImg, 0,0)
If enemyDead then exit
SetAlpha 1.0

...

So ensure you do SetColor 255,255,255 after you alter a color (eg when printing colored text!)
Ensure to reset alpha after you changed it for a certain element.

If you change blend modes (SetBlend Alphablend) ensure to set it back to the one you use normally (most probably ALPHABLEND).


@ FBO
Which one should it be? For render2texture I made a PullRequest allowing a consistent usage in DX,OGL and SDL:DX, SDL:OGL.



bye
Ron

sonic

hi Derron, thanks for your help, much appreciated.

if i hadn't already had it working in 1.51, my gut would tell me it was an issue of an out of order render state change slipping into the render loop, only it seems odd that the behaviour is different in NG from OG. it's most commonly visible on text elements, often drawn in a list, one after the other, with the same colour, and one will occasionally pop. perhaps i only set the colour once in those cases, i will check.

in any case, the scene is completely static when this happens, there should be no 'animating' elements (it's a level editor / lightweight scripting language IDE / pixel art creator). for example i have custom listboxes whose colour and alpha should be set for each item but i will look through and see if they could be the source of the problem. in theory though, my editor should draw all of its UI elements in the same order in each frame, which feels at odds with the likelihood of a stray render state change making its way into the render loop. unless there is some crazy threading going on in the background...

FBO - i mean i'm targetting Windows (after 18 years of Mac gamedev i've finally given in and defected) but i'd like it to be cross platform, as it's lightweight and has a chance of running on Mac. so OpenGL would be ideal, but i'm wary of how much longer it will be supported, given Apple's dropping of it. i'm guessing a Metal backend is not on the cards for now? I use the old beanage FBO mod which i've adapted many times over the years when it breaks. it still seems to work great, but i'm wondering how much, if at all, the OpenGL drivers we have in this day and age are the issue here...

i'll try a basic test with drawtext, drawrect and drawline (the ones that seem to have the most issues) and see if i can see it. thanks again.

sonic

so even by prepending each affected draw command within a listbox of text with a SetAlpha 1, SetColor and SetBlend ALPHABLEND i still see this issue... weirdly it does seem rarer now though. but disappointingly it is still there. next i will try a dummy program with just a list of DrawTexts...

sonic

i threw this together as a test...


Graphics DesktopWidth(),DesktopHeight(),32,60
While Not KeyDown (KEY_ESCAPE)
  Cls
  For Local x=0 To 10
    For Local y=0 To 10
      SetAlpha 1
      SetBlend ALPHABLEND
      SetScale 2,2
      SetColor x*20,y*20,(x+y)*10
      DrawText "THIS IS THE FUTURE",x*400,y*300
    Next
  Next
Flip -1
Wend


... and sure enough i still see the issue. i wonder if anyone else could test?

edit: setting it to the D3D9 driver i don't see the issue, as far as i can tell (sometimes the OpenGL version takes 30s plus to show the issue, other times it happens frequently. single drawtext calls randomly flash either black, white or a random colour. i didn't see any with the D3D driver in a couple of minutes of watching)

i don't think i'll be able to easily port all my shader code and FBO code to DirectX, so i'm feeling as if i've hit a bit of a wall here

Derron

So in this example random texts flicker or always the same?

What happens if you do a
Flip 0
Or
Flip 1

Instead of flip -1

And does it happen in windowed mode too?

Instead of brl.opengl you could try the sdl glmax version.


Edit: also interesting is: does the whole "THIS IS THE FUTURE" text flicker or just single chars?
Why am I asking? "DrawText" draws _each_ single character individually (they are own textures). So if it was something like "becoming invalid" textures, then the glyphs/characters would flicker (so all "A" or "E" chars). If the whole "text" (of a single DrawText command) flickers ... then it might be some reached "limit" or so?

Does it happen with sprites too? So if you drew 10*10 single sprites (a small image) ?



Bye
Ron


sonic

hi Derron,

i see this regardless of Flip setting. but it does seem that DrawImage doesn't display the issue, and neither does DrawRect. i saw issues with DrawRect in my app but it's possible it's because the calls followed some DrawText. so the source of the issue seems like it might be DrawText - i wonder if it is setting something incorrectly in the GL render state after a call?

edit: for completeness, here is the test prog i used


SetGraphicsDriver GLMax2DDriver() 'D3D9Max2DDriver()

Graphics DesktopWidth(),DesktopHeight(),32,60,GRAPHICS_BACKBUFFER

Cls
For Local count=0 To 100
SetColor 100+Rand(0,150),100+Rand(0,150),100+Rand(0,150)
DrawRect Rand(0,63),Rand(0,63),Rand(2,6),Rand(2,6)
Next

Local image:TImage = CreateImage(64,64,1,DYNAMICIMAGE|MASKEDIMAGE)
GrabImage image,0,0

While Not KeyDown (KEY_ESCAPE)
Cls
For Local x=0 To 10
For Local y=0 To 10
SetAlpha 1.0
SetBlend ALPHABLEND
SetScale 2,2
SetColor x*20,y*20,(x+y)*10
DrawText "THIS IS THE FUTURE",x*400,y*300
'DrawImage image,x*400,y*400
'DrawRect x*400,y*400,64,64
Next
Next
Flip
Wend

Derron

Code (Blitzmax) Select

SuperStrict
Framework Brl.StandardIO
Import Brl.GLMax2D
'SDL
'Import SDL.GLSDLMax2D
'Import SDL.GL2SDLMax2D
Import Brl.RandomDefault


SetGraphicsDriver GLMax2DDriver() 'D3D9Max2DDriver()
'SDL GL
'SetGraphicsDriver GLMax2DDriver()
'SDL GL2
'SetGraphicsDriver GL2Max2DDriver()


Graphics DesktopWidth(),DesktopHeight()
print "graphics: " + DesktopWidth() + "x" + DesktopHeight()

Cls
For Local count:int=0 To 100
SetColor 100+Rand(0,150),100+Rand(0,150),100+Rand(0,150)
DrawRect Rand(0,63),Rand(0,63),Rand(2,6),Rand(2,6)
Next

Local image:TImage = CreateImage(64,64,1,DYNAMICIMAGE|MASKEDIMAGE)
GrabImage image,0,0

While Not KeyDown (KEY_ESCAPE)
Cls
For Local x:Int=0 To 10
For Local y:Int=0 To 10
SetAlpha 1.0
SetBlend ALPHABLEND
SetScale 2,2
SetColor x*20,y*20,(x+y)*10
DrawText "THIS IS THE FUTURE",x*400,y*300
'DrawImage image,x*400,y*400
'DrawRect x*400,y*400,64,64
Next
Next
Flip
Wend


Modified it to be superstrict - and to only load the modules required.
You could comment out brl.glmax2d and use sdl.glmax2d instead (same "drivername") or sdl.gl2max2d
Dunno if this changes something (uses SDL as backend then).

Also : your DrawText is drawing "outside" of the graphics window ... eg my "DesktopWidth()" here reports both of my screens (dualscreen setup) so maybe you reach some limit or so? (here graphics context becomes 3200x1200)


You might also try this here:
https://apitrace.github.io/

it allows to go through your app "frame by frame" and you can check if something goes bonker.


bye
Ron

sonic

cheers again. i still see the issue with your modifications. inteterestingly 'BRL.RandomDefault' didn't build for me where 'BRL.Random' did.

i also see the same results with the text constrained to the desktop dimensions.

i should note that i'm running in 4k and the correct resolution is detected by DesktopWidth() & DesktopHeight(). when i try forcing the resolution to 1920x1080 (exactly half in each dimension) Blitz will not give me a correct fullscreen window: i see the Windows desktop, the monitor switches to a lower res (could be 1080p but actually looks lower), and the Blitz window is pinned to the top left of the screen, taking up about 60% of the screen. not at all usable for a professional app. i'm definitely concerned about the viability of NG for any kind of commercial release. it feels like it's so close, yet there are too many gotchas as it stands. i need robust resolution handling for this thing i'm trying to do (a combination of various editors i've made going back to about 2006.)

edit: forgot to mention - the issue doesn't show in this 'weird' 1080p mode though, so it does seem it could be related to the high resolution...

thanks for the tip about apitrace - i wonder to what degree i can single out a single frame out of many, that may have already passed, without hand-stepping through thousands to find the one that shows it... i also use Unity and that only allows you to 'Frame Debug' the current frame - but i'd need to know in advance that the issue was present on the current frame if using that.

Derron

OK ... so it happens with the SDL modules too?

If so then indeed the 4k resolution might create the trouble for you. As said: does it happen in a windowed mode too (so any resolution you want - but not full screen) ?



@ commercial thingies
My game TVTower has been in use by thousands of players and nobody reported any flicker yet - neither in OGL nor in DX (they reported other issues :D).


@ apitrace
You could do this: have a local variable "flickered:Int = False". As soon as you see it "flickering" .. you hit "KEY_SPACE" and it sets "flickered = True".

If "flickered = true" then draw 10 times "DrawImage(some image, 0,0)".

So what do you gain? in api trace you see the amount of "draw commands" - so as soon as you see an increased number of draw commands, you know "somewhere before" it must have happened.

This will help to reduce the amount of frames to a "reasonable " amount.


@ text flicker
I again ask if the whole text flickers or only some glyphs of it.
whole text : check out for alpha/color/transparency setups
glyphs: some texture corruption on the GPU might have happened


@ brl.randomdefault
If you used the most current code for brl/pub/... and bcc/bmk then you would need "brl.randomDefault" as Brucey got convinced by me to include other random number generators too - ones that generate the same numbers across platforms/systems (original brl.random is not guaranteed to do so). also the other random generators (random.mod/...) are possibly faster ... or just .... better :D


bye
Ron

sonic

sorry i skipped over your advice to try the SDL driver last time. when i did that it worked!

however, when setting manually to 1080p with the Graphics command, Blitz then reports 2560x1440 in DesktopWidth() & Height. the screen is scaled very strangely, seemingly larger than the desktop itself. 1600x900 gives me 2133x1200, similarly badly scaled. i wonder if this is related to Windows scaling settings, although i am set to 200% and none of these ratios match up with that.

i'm not able to get it working with the SDL driver in my own build as i get 'System driver already configured as Win32SystemDriver' from driver.bmx.

if i add the Framework command you used, it will not build because i guess i am relying on quite a few Pub and BRL modules. how do i find out which ones i need to add if i'm doing it manually?

edit - sorry your reply landed at the same time as mine... the whole text flickers, yeah. windowed mode seems to be ok, but this is a fullscreen app, so i definitely need that to work.

Derron

For the "windowed application test" you of course cannot use "DesktopWidth()" etc - just use some hardcoded values (like 1920, 1080) and remove the hertz/color-depth settings.


@ SDL
Yes, as without Framework command you automatically use "brl.SystemDefault" ... this is done to stay compatible with all the old projects not using SDL - and not using the framework command.

There was a "Framework Assistant" somewhere ... which would fiddle out what modules you need to import.
https://sites.google.com/site/jimbrown007
https://sites.google.com/site/jimbrown007/home/fa212.zip?attredirects=0

If you do not want to use that and are having NO clue what to use: you could manually add all the brl.* modules line by line (skipping the ones you know you are not using ... ) and the removing the Brl.SystemDefault etc from the list. Tedious but an option.


@ SDL #2
SDL also handles multi monitor setups better (eg centering on a single of the multiple screens etc).


@ SDL #3
if it does not flicker with "sdl.glmax2d" but flickers with "brl.glmax2d" then it must be something in how the graphics context is created - as the logic (how stuff is sent from blitzmax to opengl) should be the same.


bye
Ron

sonic

actually strike that - windowed mode also displays the issue. i guess i will try and move to SDL. could take a while, but it seems like the only viable option. even so, i'm very concerned about not having access to standard resolutions (that my monitor supports fine) like 1080p, 900p etc.

really appreciate your help with this

Derron

You should be able to use full screen with other supported resolutions ... does FullHD ("2k") work with Direct3D9 ?

Also you should check your driver settings - drivers could intercept resolution changes already (eg they want to do some clever upscaling). You can leave it up to the monitor to do so - or let the gpu do it.


bye
Ron

sonic

yes D3D9Max2D works as expected (DesktopWidth/Heightreturns 1920x1080 after the Graphics command), although GrabImage gives me an Exception Access Violation.

edit - ah the scaling issue is because of my NVidia settings that's good to know. i can now get the correct resolutions. it's a shame this would need to be something that users would need to get right before running the software as it's not exactly straightforward.

i guess my only option is to try the SDL framework import now... i'll let you know how it goes!

edit 2: i've just noticed that issue does display in 1080p, so we can rule out resolution as the cause