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.

AppGameKit Classic Chat / flipping an image via memblock or shader source

Author
Message
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 9th Feb 2023 09:45 Edited at: 10th Feb 2023 02:24
This program will flip an image in any direction using either memblocks or a shader
feel free to use

ive also included a test image to demonstrate
fubarpk
https://fubarpk.itch.io/

Attachments

Login to view attachments
MikeHart
AGK Bronze Backer
20
Years of Service
User Offline
Joined: 9th Jun 2003
Location:
Posted: 9th Feb 2023 10:23
Is this an example for image manipulation or is flipping the real target here?
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 9th Feb 2023 11:02
@mikehart Flipping is the target, I know the program is named imageManipulation, originally I had planned to add a rotation vector but that math with shaders is a bit beyond my abilities
fubarpk
https://fubarpk.itch.io/
Kevin Picone
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 9th Feb 2023 11:21 Edited at: 9th Feb 2023 11:28
For those interested in AppGameKit optimization and genreally tweaking their brute force operations in their AppGameKit programs, stuff like memblock operations are good place to look.

Generally speaking when using a runtime (like AppGameKit uses) it's best to work in the biggest data type that's on offer, so we should be able to swap the batches of byte read /write with a single int read /write.

(untested)




Focusing on the inner loop and thinking about how AppGameKit executes code, that being that every operation in your program ( loops / variables/arrays/function calls / operations / etc ) all get converted into an operation in a giant select / case that's being polled for every cycle of the program. That means excessive operations are like a tax on your programs target performance. Too many unnecessary operations will impact the performance of any loop dramatically.

All that probably sounds like a mouth full rubbish to most people, but there's many simple strategy we can all use to boost our 2D programs performance, be in AppGameKit or otherwise .. One handy one; is moving constant calculations outside of the inner worker bee for / next loop. What tends to happen when we work in nested For Y / For X loop blocks is we end up calculating things inside the X loop are in fact constant for the that row..

So we could more than like compute the SrcOffset once per row than step along it


(Untested - For educational purposes only ) )



So reading the source pixel is now one call to GetMemblockInt followed by an addition.

Now the question we need to ask ourselves is does that FLIP variable every change within the inner X loop ? In this case it' doesn't; so we could split the complex loop into three simple loops and remove the decisions per pixel

Perhaps like



Yep it's longer.. yep harder to maintain.. well not that much harder, but this type of approach can help you will back performance a program be lacking.

PlayBASIC To HTML5/WEB - Convert PlayBASIC To Machine Code
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 9th Feb 2023 11:53
Thats a great concept @Kevin and would improve the performance I feel aswell thankyou

For my image drawing software I work in smaller image sizes as there's lots in the background stored
The now optimed shader should be fastest ideally I should do a performance test of the now 3
methods.
fubarpk
https://fubarpk.itch.io/
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 9th Feb 2023 12:18
Doing speed tests using the following code

I get from the original memblock method
0.123284
with my improvised shader
0.000047
and kevins improvement
0.114965
fubarpk
https://fubarpk.itch.io/
janbo
15
Years of Service
User Offline
Joined: 10th Nov 2008
Location: Germany
Posted: 9th Feb 2023 16:57
Not sure if your shader test is fair as you actually change the source of the images with memblock commands but with shaders you'd need to render it back into an image.
This depends ofc. if you actually need it to change the source.
Wanna make another speed test ?
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 9th Feb 2023 22:20
using render and grabimage I made some modifications as suggested the shader still comes off faster, Im not sure if thats what you meant by ofc janbo

fubarpk
https://fubarpk.itch.io/
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 9th Feb 2023 22:43
Is there a problem with SetSpiteFlip()?
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 9th Feb 2023 22:57
Quote: "Is there a problem with SetSpriteFlip()?"

never knew of the command until now, I guess its a rather pointless to many thread useful only to those who want to make
drawing software, which was my original plan for the command. Atleast it gives an idea of the workings behind the command
and how a shader could've been used.

I was only going to add it to the snippets thread but that's now closed.
fubarpk
https://fubarpk.itch.io/
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 10th Feb 2023 00:54 Edited at: 10th Feb 2023 00:59
I see you're looping through all pixels in the image, there's no need to do that and it can be cut in half. Also, make some constants or at the very least document what the values 1,2,3 actually mean.
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda
Pixel-Perfect Collision

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds

blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 10th Feb 2023 02:28 Edited at: 10th Feb 2023 02:34
Quote: "I was only going to add it to the snippets thread but that's now closed."

Not anymore

One other thing. I think if you try GetMemblockInt() it will go a lot faster
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 10th Feb 2023 07:29 Edited at: 10th Feb 2023 07:45
Thanks the documented program got moved to https://forum.thegamecreators.com/thread/222027?page=4 and at the top of this thread.
@Phaelex other than placing the x loops inside a nested if Im not sure how else it could be improved. I wish I could rotate the UVs inside a memblock or
a shader but the math in that is quite extensive and beyond me. I did research on rotation but it needed the a Vs shader to do so and not just a pixel
shader

which can be achieved better with SetSpriteUV ( iSpriteIndex, u1, v1, u2, v2, u3, v3, u4, v4 ) see [href]file:///C:/Program%20Files%20(x86)/Steam/steamapps/common/App%20Game%20Kit%202/Tier%201/Help/Reference/Sprite/SetSpriteUV.htm[/href]
fubarpk
https://fubarpk.itch.io/
MikeHart
AGK Bronze Backer
20
Years of Service
User Offline
Joined: 9th Jun 2003
Location:
Posted: 10th Feb 2023 19:16
To flip it you could also scale it by a factor of -1.
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 11th Feb 2023 20:43
Quote: "To flip it you could also scale it by a factor of -1."

I thought that too but it didn't work for me
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 12th Feb 2023 01:16
The reverse scale technique may nolonger work, dont know, but ideally if we retrieve each of the uv positions of a sprite image, we should be able to
perform a rotation from the sprites offset and a bit of math. I'm sure both memblocks and a shader could achieve this, but I have no idea how to do this,
with my research there was alsorts of complicated math that frankly i didnt understand. If anyone would like to attempt rotation rather than just flipping
ide love to see it shared here. ("This would have the effect of the sprite standing still but the image rotating as if there was 2 sprites and a mask.")
fubarpk
https://fubarpk.itch.io/

Login to post a reply

Server time is: 2024-04-24 22:58:10
Your offset time is: 2024-04-24 22:58:10