3D Z Clipping For the PVR + OpenGL

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
User avatar
PH3NOM
DC Developer
DC Developer
Posts: 574
Joined: Fri Jun 18, 2010 9:29 pm
Has liked: 0
Been liked: 0

3D Z Clipping For the PVR + OpenGL

Post by PH3NOM » Wed Jan 01, 2014 1:38 pm

So, some of you might remember, I started working on a new OpenGL API for KOS early last year.
Time has been scarce for me to work on the project, but recently I have been on winter break and have made a little progress.
Some of the code is adapted from KGL, mostly the default matrix stack initialization, but carefully to ensure things are being done efficiently.

Some things that are different from KGL:
-Mixed-List Vertex Submission:
I have recently implemented a very nice vertex buffer solution that allows submitting mixed lists, meaning you can switch from rendering opaque polys and transparent polys at any given time by calling glEnable(GL_BLEND) for transparent polys, or calling glDisable(GL_BLEND) to switch back to opaque. There is no glKosFinishList() like before.
The vertex buffer is not using KOS DMA, instead my own solution. Why? I made some testing with KOS DMA, and the benchmark program I ran hits 1.25 mil verts/second at 60fps. With my solution, the benchmark program hits 2.5mil verts/sec at 60fps.

-Single Draw Call to the PVR
The scene is submitted by a call to glutSwapBuffers(), there is no glKosBeginFrame()/glKosFinishFrame().
Now, the vertex buffer stores generic pvr commands, so the headers or the vertices can be typecasted, this means the entire vertex array for each list for the scene is submitted in one transfer.

-Z Clipping ( Near and Far )
My experience with KGL demonstrated some wonky Near Z clipping that would often cause large polygons to be culled when they should clipped.
For this pipeline, I have written my own Near Z clipping algorithm, not based on KGL's code, that aims to remove those annoying bugs in KGL.
Also, it is possible to adjust the PVR's Far Z clipping that brings me to the point of this post.

So, with my OpenGL API up and running, I made a simple scene composed of triangle strips to simulate a large city street.

With no clipping enabled, the scene is quite ugly indeed. The large polygons disappear when they should be clipped, but are not. Also, the background color should be grey, this screen also shows a nasty bug in NullDC that does not display pvr_set_bg_color() correctly.
Image

Now lets enable my Near Z clipping algorithm:
Image

Looks better, but not what I had in mind. The large building disappears into the grey, and the viewing distance seems very poor. There is a skybox, but it can not be seen due to the draw distance.
This is because KOS sets the PVR's Far Z clipping to a relatively short distance.
We need to set the PVR's register to modify the draw distance:

Code: Select all

#include "pvr_internal.h"
    #define FARZ_CLIP 0.00000001f /* Far Z Clipping - On PVR Smaller Value = Further Clip Pane */
    pvr_state.zclip = FARZ_CLIP;
    PVR_SET(PVR_BGPLANE_Z, FARZ_CLIP);
Now, that's more like it :-)
Image

Image

For testing purposes, I have included a .cdi of the demo to look at, you can move around the "city" in a first-person perspective:
OpenGL-ClipR01-CDi.rar
OpegGL DC Demo 01 - CDI - (C) 2013 PH3NOM
(1.13 MiB) Downloaded 101 times
OpenGL-ClipR01-UnscrambledBin.rar
vOpegGL DC Demo 01 - unscrambled bin for loading from sd or cd - (C) 2013 PH3NOM
(744.43 KiB) Downloaded 105 times
User avatar
Indiket
DC Developer
DC Developer
Posts: 99
Joined: Sun Sep 05, 2010 5:44 am
Has liked: 0
Been liked: 0

Re: 3D Z Clipping For the PVR + OpenGL

Post by Indiket » Wed Jan 01, 2014 2:50 pm

Thats "PH3NOM-ENAL"!!!! :D :D . I'll take a quick look ;)
moribus
DCEmu Newbie
DCEmu Newbie
Posts: 6
Joined: Mon Jan 06, 2014 5:46 am
Location: France
Has liked: 0
Been liked: 0
Contact:

Re: 3D Z Clipping For the PVR + OpenGL

Post by moribus » Mon Jan 06, 2014 5:52 am

Wow.. It's pretty impressive ! Will you release the source code ?
User avatar
SiZiOUS
DC Developer
DC Developer
Posts: 390
Joined: Fri Mar 05, 2004 2:22 pm
Location: France
Has liked: 17 times
Been liked: 10 times
Contact:

Re: 3D Z Clipping For the PVR + OpenGL

Post by SiZiOUS » Mon Jan 06, 2014 2:48 pm

It's really nice to see things like this in 2014, PH3NOM.

Keep up the great work !!! :mrgreen:
User avatar
TheRedFox
Insane DCEmu
Insane DCEmu
Posts: 152
Joined: Tue Aug 05, 2008 1:57 pm
Has liked: 0
Been liked: 0

Re: 3D Z Clipping For the PVR + OpenGL

Post by TheRedFox » Mon Jan 06, 2014 10:33 pm

Cool!
Wyrd bi∂ ful aræd
User avatar
PH3NOM
DC Developer
DC Developer
Posts: 574
Joined: Fri Jun 18, 2010 9:29 pm
Has liked: 0
Been liked: 0

Re: 3D Z Clipping For the PVR + OpenGL

Post by PH3NOM » Fri Jan 10, 2014 6:30 pm

moribus wrote:Wow.. It's pretty impressive ! Will you release the source code ?
Yeah, I plan to release the OpenGL API soon, and the source to that demo will be included as an example app for the library.

Right now I am working on getting the lighting model finished for the library.

Inspired by Dave to get some massive terrain rendered, I pulled in some geometry exported by a terrain generator. Although it is running at 30fps, that is after being fully zclipped, which tends to slow things down when dealing with such a large number of polygons. Here is a look a todays work:
Image
Ayla
Insane DCEmu
Insane DCEmu
Posts: 140
Joined: Thu Apr 03, 2008 7:01 am
Has liked: 0
Been liked: 1 time
Contact:

Re: 3D Z Clipping For the PVR + OpenGL

Post by Ayla » Fri Jan 10, 2014 6:38 pm

That's nice! How does it perform on real hardware?
User avatar
MisterDave
DCEmu Freak
DCEmu Freak
Posts: 58
Joined: Mon Apr 08, 2013 1:16 pm
Has liked: 0
Been liked: 0

Re: 3D Z Clipping For the PVR + OpenGL

Post by MisterDave » Sat Jan 11, 2014 4:37 am

Very nice. It looks like you are sampling the heightmap at a pretty high resolution too and still getting a good frame rate.
User avatar
PH3NOM
DC Developer
DC Developer
Posts: 574
Joined: Fri Jun 18, 2010 9:29 pm
Has liked: 0
Been liked: 0

Re: 3D Z Clipping For the PVR + OpenGL

Post by PH3NOM » Thu Jan 16, 2014 11:04 pm

Ayla wrote:That's nice! How does it perform on real hardware?
20-30fps, depends on the scene. But there is room for optimization in the pipeline still :-) Demos will be released soon :0

Another test, this time with 3 spot lights:

Image

Image
MisterDave wrote:Very nice. It looks like you are sampling the heightmap at a pretty high resolution too and still getting a good frame rate.
Full resolution, and its being submitted as Triangles, not optimized as Strips :-)
moribus
DCEmu Newbie
DCEmu Newbie
Posts: 6
Joined: Mon Jan 06, 2014 5:46 am
Location: France
Has liked: 0
Been liked: 0
Contact:

Re: 3D Z Clipping For the PVR + OpenGL

Post by moribus » Fri Jan 17, 2014 4:14 am

Really cool ! I hope you will release the API very soon ! :)
It can revive the developpement of 3D homebrews I think.
User avatar
MisterDave
DCEmu Freak
DCEmu Freak
Posts: 58
Joined: Mon Apr 08, 2013 1:16 pm
Has liked: 0
Been liked: 0

Re: 3D Z Clipping For the PVR + OpenGL

Post by MisterDave » Fri Jan 17, 2014 11:15 am

Very nice. You can't go too far wrong with a lit terrain :)
User avatar
PH3NOM
DC Developer
DC Developer
Posts: 574
Joined: Fri Jun 18, 2010 9:29 pm
Has liked: 0
Been liked: 0

Re: 3D Z Clipping For the PVR + OpenGL

Post by PH3NOM » Tue Jan 21, 2014 11:05 pm

Even a smaller scale, its cool to see dynamic vertex lighting on the DC, with Zclipping enabled through OpenGL.

I keep thinking something like Portal would be fun...

Here is todays work, lit dynamically through only 2 light sources in OpenGL DC:

Image

Image

Image
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5404
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has liked: 1 time
Been liked: 16 times
Contact:

Re: 3D Z Clipping For the PVR + OpenGL

Post by BlueCrab » Wed Jan 22, 2014 12:42 am

By the way PH3NOM, when you're done with this all, how would you feel about it being put into kos-ports in place of the old kgl?
User avatar
PH3NOM
DC Developer
DC Developer
Posts: 574
Joined: Fri Jun 18, 2010 9:29 pm
Has liked: 0
Been liked: 0

Re: 3D Z Clipping For the PVR + OpenGL

Post by PH3NOM » Wed Jan 22, 2014 10:52 pm

BlueCrab wrote:By the way PH3NOM, when you're done with this all, how would you feel about it being put into kos-ports in place of the old kgl?
That would be an honor.

I have, in the process, ported over the old nehe demos to work with the new API, to make sure things are working.
When I initially enabled Dynamic Vertex Lighting to nehe08, my lighting code revealed some deficiencies.
I learned that Vertex Normals needed to be transformed differently than Vertex Positions.

I am getting close to being very happy with things. I will contact you via P.M. when I am ready to submit the code 8-)
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5404
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has liked: 1 time
Been liked: 16 times
Contact:

Re: 3D Z Clipping For the PVR + OpenGL

Post by BlueCrab » Thu Jan 23, 2014 9:02 am

PH3NOM wrote:I am getting close to being very happy with things. I will contact you via P.M. when I am ready to submit the code 8-)
There's no rush. Get it all working well, then we can discuss how to deal with getting it in the tree. :wink:
User avatar
SiZiOUS
DC Developer
DC Developer
Posts: 390
Joined: Fri Mar 05, 2004 2:22 pm
Location: France
Has liked: 17 times
Been liked: 10 times
Contact:

Re: 3D Z Clipping For the PVR + OpenGL

Post by SiZiOUS » Fri Jan 24, 2014 9:00 am

To be honest, PH3NOM, your screens are true beauty.

I can't say more since I don't have any 3D coding skills, but it's so cool to see a talented programmer doing this on our DC in 2014, more than 10 years after the official release date!

Btw have we a list of all free 3D libraries for the Dreamcast ?
Here's my attempt:
User avatar
RyoDC
Mental DCEmu
Mental DCEmu
Posts: 353
Joined: Wed Mar 30, 2011 12:13 pm
Has liked: 0
Been liked: 0

Re: 3D Z Clipping For the PVR + OpenGL

Post by RyoDC » Fri Jan 24, 2014 9:21 am

Sizious, You better list libraries that work and compile on Dreamcast.
Last time I tried to build a CubicVr it failed. Same for Iris3D.
How do I try to build a Dreamcast toolchain:
Image
User avatar
Neoblast
DC Developer
DC Developer
Posts: 312
Joined: Sat Dec 01, 2007 8:51 am
Has liked: 0
Been liked: 0

Re: 3D Z Clipping For the PVR + OpenGL

Post by Neoblast » Wed Jan 29, 2014 3:09 am

Wow... so the max is... 0.98M? With lightning?
Awesome, your opengl is going to change the 3D scene in the dreamcast as it is now...

Hmmm that is getting closer to the "less powerful" wince sdk.

How much do you think it'd be possible by improving the pipeline?
User avatar
PH3NOM
DC Developer
DC Developer
Posts: 574
Joined: Fri Jun 18, 2010 9:29 pm
Has liked: 0
Been liked: 0

Re: 3D Z Clipping For the PVR + OpenGL

Post by PH3NOM » Wed Jan 29, 2014 11:18 pm

Neoblast wrote:Wow... so the max is... 0.98M? With lightning?
Awesome, your opengl is going to change the 3D scene in the dreamcast as it is now...

Hmmm that is getting closer to the "less powerful" wince sdk.

How much do you think it'd be possible by improving the pipeline?
It depends how you use it; Triangle Strips Or Individual Triangles, etc..

And that is with ZClipping Enabled.

Models that do not need Zclipping will be much faster.

Also, I have included the ability to statically calculate vertex lighting so that it does not have to be done every frame, if you choose, as seen in some of those landscape screens.

In the pipeline, the clipping of Strips could be optimized when I get time. This will produce less vertices to be submitted to the PVR.

BTW, Sonic Adventure produces ~1mil verts/sec
Image
User avatar
Neoblast
DC Developer
DC Developer
Posts: 312
Joined: Sat Dec 01, 2007 8:51 am
Has liked: 0
Been liked: 0

Re: 3D Z Clipping For the PVR + OpenGL

Post by Neoblast » Thu Jan 30, 2014 3:39 pm

PH3NOM wrote:
Neoblast wrote:Wow... so the max is... 0.98M? With lightning?
Awesome, your opengl is going to change the 3D scene in the dreamcast as it is now...

Hmmm that is getting closer to the "less powerful" wince sdk.

How much do you think it'd be possible by improving the pipeline?
It depends how you use it; Triangle Strips Or Individual Triangles, etc..

And that is with ZClipping Enabled.

Models that do not need Zclipping will be much faster.

Also, I have included the ability to statically calculate vertex lighting so that it does not have to be done every frame, if you choose, as seen in some of those landscape screens.

In the pipeline, the clipping of Strips could be optimized when I get time. This will produce less vertices to be submitted to the PVR.

BTW, Sonic Adventure produces ~1mil verts/sec
Image
That exact screenshot, there's not much ahead. Taking one that shows a lot more... for example when you see down the lane, or meteor herd for example. I'm sure it must get to almost 2 million, if not more.

Hmm in any case it's looking VERY GOOD. I'd say there's even almost more performance than with DX6.
Keep us updated, and let me know if you need any sort of testing.
Post Reply