Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Author
Message
IronManhood
9
Years of Service
User Offline
Joined: 6th Feb 2015
Location: US
Posted: 19th Jun 2018 22:03
I wrote a function using memblocks to blur an image and it takes a solid 30 seconds to blur a 1024x1024 sized image. I mean honestly my function could probably be optimized a bit more but I doubt that would make a massive improvement. Why are memblocks STILL so slow?
Bengismo
6
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 19th Jun 2018 22:11 Edited at: 19th Jun 2018 22:13
The memblocks isnt slow really...only as slow as doing a calculation on an array thats 1024,1024 in size. Truth is...just doing the processing on that amount of data on interpreted bytecode is slow. Thats a million calculations of whatever your doing for one pixel.

If you were doing it in teir 2....no problem. Teir 1 though...tight loops dont run that fast really.

In any case...can you post some code?...or at least explain what algorithm your bluring with?

its quite likely that you could blur the image using a shader or upscaling a downscaled render which will take a few milliseconds to produce a blur effect. Plenty of ways to do what you want.
Bengismo
6
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 19th Jun 2018 23:21 Edited at: 19th Jun 2018 23:53
A quick example... (not even using any shaders)

Just playing with downsampling and multiple overlapped blended renders can give some nice results in under 10mS! It might be possible to replicate the exact blur effect your going for by using another method that is much much quicker.



I'll post the blur functions if you want. (I quickly wrote 2 of them just to example this)

EDIT: My Fav ....Motion blur eagle with oversaturation/exposure (rendered in 0.95mS)
IronManhood
9
Years of Service
User Offline
Joined: 6th Feb 2015
Location: US
Posted: 20th Jun 2018 01:19 Edited at: 20th Jun 2018 01:29
I've never thought about resizing images like that. Looks interesting. I wrote the function to test how effective it would be and also to learn a bit about images. I never would have guessed it would be this slow. But it does indeed blur an image . I've been pondering whether or not to try to make a small image plugin with simple functions like this one.

Here's the blur function.

puzzler2018
User Banned
Posted: 20th Jun 2018 07:02
You got 3 for loops going on here, I can see what x and y loops are doing but what is the "i" loop acheiving

This just means that if you have say a 1024 x 1024 image - your processing the image over and over again 256 times for a 1024 x 1024 image

Thats why its slow,

Will have a think on a better technique

IronManhood
9
Years of Service
User Offline
Joined: 6th Feb 2015
Location: US
Posted: 20th Jun 2018 08:56
Loop "i" iterates through all of the image bytes by a step of 4(iterates over each pixel). The "__x" loop iterates through a number of pixels relative to "i" and the factor parameter while totaling the colors for averaging. The "__y" loop repeats "__x" and increments the "_xoffset" variable which adds vertical blurring.
Bengismo
6
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 20th Jun 2018 09:10
Id just use the graphics hardware that you have at your disposal....your function just seems to average the values over a range of x and y values (the range being dependent on 'factor') and so that larger you make that...the slower your function. Each pixel is 4 colours and each of those is being averaged over factorx2 on y and factorx4x2 on x. So with a factor of 5 you are processing 80 pixels to generate 1 value...and then doing that for 1million pixels(each having RGBA)....320 million calculations in interpreted basic. Your also calling the clamp function inside the loop too which isnt quick either. lol

This is a good example of nested loops getting way out of order....

I will post the blur functions I did last night when I get home.

1) Down samples then resizes to original size - basically uses the mipmapping built into the hardware
2) Does similar to yours and adds multiple (slightly offset versions) of the image to itself then divides by the total
3) Additive blends multiple images (SetTransparency(sprite,2)) then captures the result to a new image
4) Motions blur...multiple versions averaged over movement in 1 axis

All of these run in under 10mS on my pc.
IronManhood
9
Years of Service
User Offline
Joined: 6th Feb 2015
Location: US
Posted: 20th Jun 2018 10:50
When you put it that way...lol
IronManhood
9
Years of Service
User Offline
Joined: 6th Feb 2015
Location: US
Posted: 20th Jun 2018 12:53
I implemented this same function as a plugin and reduced the time down to like 2 seconds.
Bengismo
6
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 20th Jun 2018 13:17
Still a fair bit longer than 10mS. Plus using the built in functions is cross platform and faster than the plugin option.

You could write the whole thing as a shader and do it even quicker still but as long as you have a solution that your happy with.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 20th Jun 2018 13:18
Might want to look at some DB examples of image manipulation. Myself and a few others have written many image routines using memblocks. I don't recall any specific numbers, but I don't think anything ever took 30 seconds to run, including blur routines. You'll have to do some searching as it was a long time ago.

Another option would be to use a shader.

Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
IronManhood
9
Years of Service
User Offline
Joined: 6th Feb 2015
Location: US
Posted: 20th Jun 2018 14:08
Well one of the reasons I did this was to have these functions as a means to avoid applying a complex shader to hundreds or thousands of sprites. So you take a few seconds to generate the final image and then you're ready to go. Though I am a noob when it comes to shaders but my first inclination is to assume that it's better to apply the default shader than, say, a blur shader.
Bengismo
6
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 20th Jun 2018 14:23 Edited at: 20th Jun 2018 14:25
lol...you use the shader to render ONCE to make the image in 10mS and then just use the image from then on.... (you can delete the shader after that)

And the blur shader still runs at 300fps if you did want to constantly vary your blur level

Besides the examples I showed above dont even use a shader...

Anyway...as long as your sorted.
IronManhood
9
Years of Service
User Offline
Joined: 6th Feb 2015
Location: US
Posted: 20th Jun 2018 14:53 Edited at: 20th Jun 2018 14:59
Ohhh...I see. Render the sprite with whatever shader then grab the image.


This is me right now https://imgur.com/gallery/tC36upN.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 20th Jun 2018 19:58
IronManhood, lmao that clip was too funny
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
puzzler2018
User Banned
Posted: 20th Jun 2018 20:16
Yeah lol - dont give in....
Bengismo
6
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 20th Jun 2018 21:25 Edited at: 21st Jun 2018 00:58
Theres no problem!! There's plenty of different options and ways of doing things. It doesn't really matter how you get to what you want.... as long as you get there. We are all here to help.

Just to give one last example


This is the same image rendered as normal on a sprite on the left and using a blur shader on the right.
Framerate is >1000FPS so wholescreen is getting rendered in under 1mS and ive not really optimised anything- obviously...the speed depnds on your hardware but its quick to do one render and capture the image if you want to.

Blur sprite shader code is listed below (straight from blur.ps that i threw together)....as you can see...its really NOT complicated - in fact - its YOUR algorithm from above!! Just in shader form and if anything its much easier to read this way


shaders are not EVIL...they just arent intuitive sometimes
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 21st Jun 2018 00:36
That looks very good Bengismo

but I am doing something wrong when i try and use that shader it seems to have no effect


fubar
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 21st Jun 2018 00:44
I just tested Ben's shader code (had to remove any comments that included forward slashes) and used an image of 2048x1536 and got over 1400fps. And depending on the blur amount, I'd hit 7k fps if size was 1.



Shaders I think is easily your fastest method. As for the blur algorithm, it could use some improvements. You can make out noticeable lines in the blurring.
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
Bengismo
6
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 21st Jun 2018 00:51 Edited at: 21st Jun 2018 01:04
There was one extra space between two comment / / lines that crept in when I posted (damn space bar)...its corrected now and so works fine if copied straight in complete with the comments

Quote: "Shaders I think is easily your fastest method. As for the blur algorithm, it could use some improvements. You can make out noticeable lines in the blurring."


The lines in the shader
mediump vec2 uOff = vec2(1.0/256.0,0.0); // these can be changed to match the size of the texture/pixel size
mediump vec2 vOff = vec2(0.0,1.0/256.0);

the 256 should be changed to match your texture width and height ...so 2048x1536 in your case to remove any lines. (hence the comment) - they define a single pixel width in UV coords

These should really be shader constants and contralable outside the shader..... but this was posted as first steps for people to look at.... not to be a final render version.

Im sure there have been at least a few blur shaders posted around the web or even on these forums that are Waaaaay better then mine that was a 10 min exercise to get something to show the speed potential.
IronManhood
9
Years of Service
User Offline
Joined: 6th Feb 2015
Location: US
Posted: 21st Jun 2018 02:11
Wow thanks for the example shader! I'll be studying this a bit more..
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 21st Jun 2018 02:42 Edited at: 21st Jun 2018 02:44
The problem I was having as I was trying to use a rendered image incorrectly but when I tested by loading an image it worked perfectly well

My personal preference is to create shaders within the code this helps with debugging when modifications are being done
but I changed the code to include a uniform size variable which allows for setting the resolution and the blur amount but then needs the
SetShaderConstantByName command to be called to take effect before it didn't

Snippet

CreateShader




Definitely very useful code Bengismo
fubar
puzzler2018
User Banned
Posted: 21st Jun 2018 07:06
Wow guys - some very good Shader work here and very useful to work out how it all works cause some coding here is very understandable .. Thanks guys too.

Login to post a reply

Server time is: 2024-04-20 11:13:10
Your offset time is: 2024-04-20 11:13:10