PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Considering moving from PocketFrog...

I'm considering using GD for my next game, and I'm just starting to look through the samples and docs.

Can anyone tell me if the following things exist or are easily implemented in GD:

* Drawing circles
* Cloning a surface
* Pixel shaders

I didn't find them in the docs, although I could have missed them. Thanks all!
To answer your questions:

> Drawing circles

Nope.


> Cloning a surface

No, but creating a new surface and blitting an image to it is trivial. A cloning function could be made by you in seconds.


> Pixel shaders

No, but GapiDraw provides direct access to the memory of each surface. This way you can implement special effects. GapiDraw in its current implementation does not contain 3D rendering btw.

Something very important you would get by moving to GapiDraw is the great support. Johan (the creator) has been extremely supportive of GapiDraw and it can now run on a wide range of devices. It's also optimized a lot!

--Eric
> Drawing circles

> Nope.

Johan, I would like to suggest adding this. I think I Recall you have primitive drawing tools for lines and rectangles, I think circles (or better still, ovals, which would of course include circles as a subset) would be a worthy inclusion. In addition, a poly drawing function would round out the primitive drawing tools nicely (also make all the closed form tools able to fill at the same time).


> Cloning a surface

> No, but creating a new surface and blitting an
> image to it is trivial. A cloning function could
> be made by you in seconds.

Granted it is easy to do, but this is another suggestion for Johan... I would have to believe that creating a surface and blitting an existing image to it would not be an optimized solution like a built-in clone method would be. This is something that would really help sell me on GD (note that I agree with most people that GD is the better library at this point, but I'm just pointing out the areas that I think PF has advantages that GD can learn from).


> Pixel shaders

> No, but GapiDraw provides direct access to the
> memory of each surface. This way you can implement > special effects. GapiDraw in its current
> implementation does not contain 3D rendering btw.

PF also gives you this ability, however, performance-wise, pixel shaders are far better. I would expect that to be true with GD as well, so I would like to suggest adding that too. This is actually the least important thing to me, the surface cloning however is something I have used fairly extensively and would like to see native in any library I decided to use. A full set of primitive drawing tools comes next.
Good primitive drawing is a non-trivial feature set. There are literally hundreds of algorithms for drawing primitives fast, or pretty, or both. I don't imagine this coming to GapiDraw right away, but Johan would be the one to know.

Cloning is the same thing as creating a surface and doing a memcpy IE Create Surface, Blit Surface. The only possible performance boosts could come from refcounted cloning. In which case you still suffer the costs of cloning, just that they wouldn't incur until you changed one of the clones. Until then they'd all point to the original surface. This latter effect can be achieved just as easily with pointers to CGapiSurfaces.

Needless to say the late copy method is not something you want in a realtime app like a game.

Pixel Shaders are an interesting architecture idea. Completely foreign to most 2D library models I've used, but very neat. However I fail to see how a pixel shader would be any faster then direct access pixel manipulation. Performance wise that is.
Hosed wrote:
Good primitive drawing is a non-trivial feature set. There are literally hundreds of algorithms for drawing primitives fast, or pretty, or both. I don't imagine this coming to GapiDraw right away, but Johan would be the one to know.


Absolutely true. From what I've seen, Johan knows his stuff like few others do, so I have the utmost confidence in him :)

Hosed wrote:
Cloning is the same thing as creating a surface and doing a memcpy IE Create Surface, Blit Surface. The only possible performance boosts could come from refcounted cloning. In which case you still suffer the costs of cloning, just that they wouldn't incur until you changed one of the clones. Until then they'd all point to the original surface. This latter effect can be achieved just as easily with pointers to CGapiSurfaces.


I think your assuming that Blit Surface is just doing a memcpy. If it is, than yes, there's no performance boost to be had and it's just a question of whether it would be nice to have the feature built-in or let people do it themselves (and I would agree it's probably not that important to be added then). But my suspicion is that as currently implemented, it's not simply a memcpy but a pixel-by-pixel drawing as any Blit is, in which case implementing cloning natively COULD be reduced to a creation & memcpy combination, which should yield a vast performance increase.

Hosed wrote:
Pixel Shaders are an interesting architecture idea. Completely foreign to most 2D library models I've used, but very neat. However I fail to see how a pixel shader would be any faster then direct access pixel manipulation. Performance wise that is.


I guess it really depends on how the shaders are implemented and how the direct access to surface data is implemented. I don't know how GD exposes the surface data, but in PF you have a GetPixel method. This, generally speaking, tends to be relatively slow. So, being able to give the address of a function that is called to modify the RGB values of each pixel plotted (which I guess is the definition of a pixel shader :) ) winds up being much more efficient. No more powerful I would think, but better performing and certainly more convenient. Maybe this isn't such a big deal with GD, depending on your access to surface data.

Come to think of it, I guess you can get at the surface data directly like that in PF too, but I've never done so. Ok, so now that I'm thinking about it more, pixel shaders might just be a somewhat more convenient construct than going after raw data in the end, especially in the case of GD. No big deal then.
fzammetti wrote:I guess it really depends on how the shaders are implemented and how the direct access to surface data is implemented. I don't know how GD exposes the surface data, but in PF you have a GetPixel method. This, generally speaking, tends to be relatively slow. So, being able to give the address of a function that is called to modify the RGB values of each pixel plotted (which I guess is the definition of a pixel shader :) ) winds up being much more efficient.

GapiDraw also provides a GetPixel() function, however you now have the powerful GetBuffer() function.

This function once called on a surface return the surface memory with the X and Y pitch to access the RGB data. This way you can access individual pixels directly.

GetBuffer() is very useful and is the function that allows you to do special effects.

For example, I use GetBuffer() to implement a real-time 90 degrees rotation and mirroring of surfaces in my game engine. The game still renders at 45+ frames per seconds even when a good percentage of the on-screen tiles are rotated/mirrored.

--Eric
That does sound fantastic, and I may even steal the concept for PF if it's not there already ;)

I wanted just that capability to do a pixelation effect... I couldn't pull it off with pixel shaders since you of course need the surrounding pixels to interpolate, this would fit the bill nicely.

Thanks for the info!
warmi · 09 May 2003, 19:53 UTC — GapiDraw
Since GapiDraw stores all surface in device-aligned format, GetBuffer incurs overhead of rotating the surface into device independent format and then rotating it back into device specific one.
This is the only major drawback of keeping surfaces aligned with the display rotation ( as opposed to rotating it during the final flip/blit).

As far as cloning surface - why create a special method for it ?
Why not use C++ copy constructor and assignment operators - they were designed precisely for that :-)
I have to make some assumptions here warmi, but...

I would expect a Surface object in GD to have a pointer to a chunk of memory that is the image data (as opposed to stored as an array or something like that).

If that is the case, then a copy constructor wouldn't help... I would get a clone of the class, which would clone the pointer value as well, but that's not a clone of the surface, it's just a clone of a reference to the memory that the image data is in.

I realize I might not have been specific originally... I'm talking about a method that gives me a clone of a Surface, not just a clone of the Surface object.
warmi · 09 May 2003, 20:15 UTC — Copy constructor etc ...
Yeah, but I wasn't talking about default copy constructor etc ... but rather about re-implemented one that actually does take care of allocating new memory and copying the content of the old surface into new one.
It would basically work the same way as clone() or copy() method would except it would follow C++ way of working. Why create a new method called clone() when C++ designed already created mechanism for that ( copy constructor and assignment operator.) ?

The only reason for creating special clone() method would be if you were doing some sort of ref counting or specifically limited your copy constructors/assignment operators to "shallow" copies only.
Ah, gotcha, I misinterpreted what you wrlte... I thought you were saying that the functionality should already be there by virtue of the default copy constructor (actually, I'm assuming one is generated in C++ as in Java, I don't even know if that's true).

Yes, I agree, implementing as you suggest makes sense.
I'm against seeing draw primitives in GapiDraw. I think it would be a great in a static lib (or helper library), but I don't always need to draw circles and as long as I have to distribute the dll its a space concern.

I think it would be a worthy addition to allow some better methods to hook to the surfaces that don't incur needless speed penalities. If what was said about GetBuffer is true, GetRawBuffer might be useful.
I guess it's a question of philosophy really... I personally feel that any graphics library that's really useful will include blit functions obviously, as well as primitive drawing tools. It would kind of be like if MS had only included BitBlt in GDI and left out all the primitive drawing tools. You could obviously get away with that, but I think there would be a general feeling that something was missing.

GD is most of the way there from what I've seen... I think you'd only be talking about a few extra K to include most of the functions I mentioned... Actually, we're just talking about code, I'd be surprised if it added more than 2K to the DLL size.

Ah well, no one should take this to be complaining about GD, I'm not complaining one bit. Just offering what I hope to be constructive suggestions.
fzammetti wrote:
I guess it's a question of philosophy really... It would kind of be like if MS had only included BitBlt in GDI and left out all the primitive drawing tools.


I agree, it is a question of philosophy. GDI is shipped with the OS so should contain as much as what could possibly be needed for GDI use so that every one that develops doesn't need to reproduce it and thereby waste a lot of space. GapiDraw isn't being shipped with any PDAs that I know of so follows a different set of rules IMO.


GD is most of the way there from what I've seen... I think you'd only be talking about a few extra K to include most of the functions I mentioned...


I see this more as a forest, a few extra K translates to thousands of megabytes when the sole method of transfer is downloading. Those megs are spread over many users obviously but its mostly a waste for many applications. I have some circle and filled circle code I've used with GD if you need some code let me know. One thing of note, I only used this until I got the real art for the objects in question.


Actually it might be a good idea to put together a helper library of sorts for a lot of these things that aren't going to be used by everyone but will certainly reduce the workload of whoever wants to use them. Some things I wish others had already written and released are a lot of the UI things that bore the hell out of me. Lists, scrollbars etc.
Away from the email for two days and whoooo.. Lots of new posts.. :)

fzammetti: The GapiDraw BltFast operation is actually quite cool. It will try the following operations depending on how surfaces are stored:

(1) memcpy (both surfaces have same pitch and size)
(2) memcpy each row (both surfaces have same yPitch and are wide)
(3) unrolled copy of each row
(4) aligned 32-bit copy source to destination
(5) source-aligned 32-bit read source to destination
(6) unaligned 16-bit pixel-by-pixel copy

Personally I don't like the DrawCircle function, mostly since it's impossible to get it accelerated by operating on multiple pixels (in contrast to DrawRect, DrawLine, etc). That's why it's not in GapiDraw, and most probably never will be. I'll put a feature in GapiDraw if it can be accelerated by analyzing the display orientation of the device. DrawCircle can't. But it's a trivial implementation.

Pixel shaders are also not there, but as many has said, using Get/Release buffer solves that as well...

Keep those questions coming!
Sm!rk/Warmi: GetBuffer does not copy/rotate/anything the internal buffer of each surface. It simply returns the internal memory area as used by GapiDraw. So it is more like "GetRawBuffer" as Sm!rk suggested.

I have received source code from a user here at PocketMatrix, which is a cache-optimized block rotator. I will publish this (with full regards to the creator of course) as a tutorial on how to create a cache-aligned buffer from each surface.

Lock()/Unlock() were both removed from GapiDraw 2.0. Reasons are that I did not want them mixed upp with LockVideoSurface()/UnlockVideoSurface(), and that they were commonly misused by DirectX programmers (due to their significant overhead).
Ah right .. it was lock() that was copying and "unrotating" surface from whatever native rotation format it was stored internally.

Yeah so at this point the original problem still persists, meaning that the memory buffer is in native format which is not as simple as say , storing it in standard format and doing rotation in the final flip.
On the other hand , I completely understand your decision to store rotated surfaces in native format to avoid shearing.
My current game ( about 95% done) uses variable rate terrain scrolling and shearing , not performance, is the biggest problem at this point so I am actually very glad that GapiDraw works the way it does - I don't want to even think how bad shearing would be if the final blit also included rotating...
:-)


PS.

BTW .. I just finished “porting” my game to GapiDraw 2.x. The performance went up by around 20%-30% which, considering that I almost exclusively use alphablit for all my sprites, is pretty much what I would expect.
Everything else works, fine – I had small issue with drawtext which seems to be a bit more picky as far as font bitmap ( compared to GapiDraw 1.x ) but I resolved all problems and now everything works fine.

PS2.
Here is a little movie of my new game in action.
http://www.warmi.net/images/zraid/game.avi
This is a version with "collisions turned off" for testing so I am plowing thru everything with ease :-)

Back in the day I played River Raid a lot and always wanted to make a clone with modern graphics etc ..
sbl wrote:I have received source code from a user here at PocketMatrix, which is a cache-optimized block rotator. I will publish this (with full regards to the creator of course) as a tutorial on how to create a cache-aligned buffer from each surface.

This will be invaluable for me! I use real-time rotations and I always wandered how to make it cache efficient. I spent quite some time on these rotations routines, but I'm sure new knowledge can help. Thanks!

And to comment on the implementation of GDI-type functions, I think creating a separate library for this would be the best way in my opinion.

Personally I have no use for such functions, and some of them would make GapiDraw gain weight...

--Eric
Warmi: Nice little movie. I really like the particle effects on the explosions. How did you create your avi?
warmi · 12 May 2003, 15:31 UTC — Recording
I used Camtasia (http://www.techsmith.com/products/studio/default2.asp) which is a $300 app ( "the premier screen recording program" according to official spin.)
It is really simple to use - you select your codec, select the source ( either entire desktop , parts of it or a selected window) and then you ... well, record :-)

Of course you have to have your application running on the desktop which is not a big deal for people using GapiDraw.

PS.
They have 30 day full-featured demo available on their site.