Author Topic: [z80 ASM] Unnamed set of 3D routines  (Read 8865 times)

0 Members and 4 Guests are viewing this topic.

Offline tr1p1ea

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 647
  • Rating: +110/-0
    • View Profile
Re: [z80 ASM] Unnamed set of 3D routines
« Reply #15 on: July 04, 2013, 05:43:11 pm »
Well you're matrix elements are between -64 and +64 (-1 to 1) arent they? I have usually taken advantage of this with regards to multiplication.
« Last Edit: July 04, 2013, 05:43:28 pm by tr1p1ea »
"My world is Black & White. But if I blink fast enough, I see it in Grayscale."


Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
Re: [z80 ASM] Unnamed set of 3D routines
« Reply #16 on: July 04, 2013, 06:22:10 pm »
Yeah true, I can use a simple HLtimesA there, but optimizations will come later.

Offline tr1p1ea

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 647
  • Rating: +110/-0
    • View Profile
Re: [z80 ASM] Unnamed set of 3D routines
« Reply #17 on: July 04, 2013, 06:28:51 pm »
Well i meant because you have a lower bit requirement there. But you're right, working first, optimisations later :).
"My world is Black & White. But if I blink fast enough, I see it in Grayscale."


Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: [z80 ASM] Unnamed set of 3D routines
« Reply #18 on: July 04, 2013, 07:23:30 pm »
If you only need 8-bit multiplication, I recently wrote my new personal best for speed and size:
Code: [Select]
H_Times_E:
;Inputs:
;     H,E
;Outputs:
;     HL is the product
;     D,B are 0
;     A,E,C are preserved
;Size:  12 bytes
;Speed: 311+6b, b is the number of bits set in the input H
;      average is 335 cycles
;      max required is 359 cycles
     ld d,0     ;1600    7      7
     ld l,d     ;6A      4      4
     ld b,8     ;0608    7      7
                ;           
     add hl,hl  ;29      11*8   88
     jr nc,$+3  ;3001 12*8-5b   96-5b
     add hl,de  ;19      11*b   11b
     djnz $-4   ;10FA  13*8-5   99
                ;           
     ret        ;C9      10     10
And the unrolled code isn't too large, either, so you can get away with a ridiculously fast routine:
Code: [Select]
H_Times_E:
;Inputs:
;     H,E
;Outputs:
;     HL is the product
;     D,B are 0
;     A,E,C are preserved
;Size:  36 bytes
;Speed: 191+6b+9p, b is the number of bits set in the input H, p is if it is odd
;   average is 229.5 cycles (105.5 cycles saved)
;   max required is 258 cycles (101 cycles saved)
     ld d,0      ;1600   7   7
     ld l,d      ;6A     4   4
           ;     
     sla h      ;CB24    8
     jr nc,$+3   ;3001  12-1b
     ld l,e       ;6B    --

     add hl,hl   ;29    11
     jr nc,$+3   ;3001  12+6b
     add hl,de   ;19    --

     add hl,hl   ;29    11
     jr nc,$+3   ;3001  12+6b
     add hl,de   ;19    --

     add hl,hl   ;29    11
     jr nc,$+3   ;3001  12+6b
     add hl,de   ;19    --

     add hl,hl   ;29    11
     jr nc,$+3   ;3001  12+6b
     add hl,de   ;19    --

     add hl,hl   ;29    11
     jr nc,$+3   ;3001  12+6b
     add hl,de   ;19    --

     add hl,hl   ;29    11
     jr nc,$+3   ;3001  12+6b
     add hl,de   ;19    --

     add hl,hl   ;29   11
     ret nc      ;D0   11+15p
     add hl,de   ;19   --
     ret         ;C9   --
Also, it returns a 16-bit result that you can work with to do whatever.

EDIT: Simple optimisation in the unrolled loop >.>

Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
Re: [z80 ASM] Unnamed set of 3D routines
« Reply #19 on: July 04, 2013, 07:38:07 pm »
@Xeda112358 actually I exactly need an HLtimesA routine, so it would perform HL=HL*signed(A) ;D

Anyway, randomly-proposing-things-that-came-to-my-mind time. Since I want the engine to be able to display actual objects (one can still dream), I need a way to correctly handle face declaration. I'll base a clipped triangle routine out of the one I included in AxeJh3D and the other one I just wrote in Axe for TheMachine02's GLib, so filling won't be a problem. However, I can't really think of a practical way to declare surfaces ...

Maybe I'll use some sort of a stack, which you push objects to be rendered on. By objects, I mean either a single vertex which will be rendered as a dot, a pair of vertices which will be rendered linked by ... a line (duh) or three vertices which will be rendered as a triangle (either black or solid white I guess). As I write it, it seems to be the best solution to me.

I'm thinking of something like this (heavily inspired by DCS7's GUI stack, but hey, it really works) :
Code: [Select]
ld hl,verticesTable    ; points to 6-bytes vertices table (X, Y, Z)
 call setCurrentVertices
 
 ld a,OBJECT_DOT
 ld hl,23     ; which vertex of the table do we use ?
 call pushObject

 ld a,OBJECT_LINK
 ld hl,vertices2     ; points to string of 2 2-bytes offsets for vertices in the table
 call pushObject

 ld a,OBJECT_TRI
 ld b,RENDER_BLACK    ; either black or solid white
 ld hl,vertices3      ; same as above with 3 offsets
 call pushObject

 call renderScene    ; objects are drawn, then popped
I don't know at all how will each function work, for now I'm only thinking of a syntax that would be practical to use (understand "not so painful"). What do you guys think of that ?

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: [z80 ASM] Unnamed set of 3D routines
« Reply #20 on: July 05, 2013, 08:10:51 am »
A lot of users would probably have pre-built images to use, so you should have a way so that users can pass a pointer to the image data (in the format of your stack) to have it rendered. Also, HL_Times_A:
Code: [Select]
HL_Times_A:
     ex de,hl
DE_Times_A:
;Inputs:
;     DE and A are factors
;Outputs:
;     AHL is the product
;     B is 0
;     C is not changed
;     DE is not changed
;Time:
;     342+13x
;
     ld b,8          ;7           7
     ld hl,0         ;10         10
aaa:
       add hl,hl     ;11*8       88
       adc a,a       ;4*8        32
       jr nc,rrr     ;(12|25)*8  96+13x
         add hl,de   ;--         --
         adc a,0
rrr:
       djnz aaa      ;13*7+8     99
     ret             ;10         10
I feel like there is a much better way to do this... Also, it returns a 24-bit result. If you only need the lower 16 bits, you can remove 'adc a,0' and change 'adc a,a' to 'rlca' to preserve a.

Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
Re: [z80 ASM] Unnamed set of 3D routines
« Reply #21 on: July 05, 2013, 11:19:25 am »
It doesn't work ... I mean doesn't give the same result as when I previously did :
Code: [Select]
call AextendSignDE
 call multHL_DE
And this time I recopied the exact same code. No modifications tried :P

EDIT : maybe it's because it's unsigned only ...
« Last Edit: July 05, 2013, 11:23:53 am by Matrefeytontias »

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: [z80 ASM] Unnamed set of 3D routines
« Reply #22 on: July 05, 2013, 11:31:39 am »
Well, multiplication is always signed, regardless. Division is the only one that you need to do a specific routine for the sign. What inputs/outputs are you expecting, though, when you use it? (just some numbers, so I can figure out what you are looking for)

Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
Re: [z80 ASM] Unnamed set of 3D routines
« Reply #23 on: July 05, 2013, 11:33:52 am »
But I can't say <_< there are too many variable factors involved. I calculate a 3*3 rotation matrix out of 2 angles, and then I apply it to a vertex. It's nearly random. And it really seems on-screen that the operation performed is unsigned.
« Last Edit: July 05, 2013, 11:36:43 am by Matrefeytontias »

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: [z80 ASM] Unnamed set of 3D routines
« Reply #24 on: July 05, 2013, 12:00:10 pm »
I see, A basically works as a 16-bit integer where the upper 9 bits are all the same. I have this:
Code: [Select]
     ld hl,0
     or a
     jp p,$+5
       sbc hl,de
     ld b,8
mulloop:
     add hl,hl
     rla
     jr nc,$+5
       add hl,de
       adc a,0
     djnz mulloop
     ret

That treats A as a signed integer, HL as an unsigned integer. I hope that works!

Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
Re: [z80 ASM] Unnamed set of 3D routines
« Reply #25 on: July 05, 2013, 12:11:33 pm »
It works :D and OH MY GOD I CAN'T SEE WHAT'S GOING ON ON-SCREEN IT DISPLAYS FASTER THAN THE LCD UPDATES O.O gonna test it

EDIT : 34 FPS !!!!  *.* :w00t: :banghead: O.O :o :crazy:

« Last Edit: July 05, 2013, 12:22:47 pm by Matrefeytontias »

Offline Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: [z80 ASM] Unnamed set of 3D routines
« Reply #26 on: July 05, 2013, 07:00:25 pm »
WOW, and i guess it is still 6MHZ?
HOW IS THAT POSSIBLE O.O :crazy:

THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: [z80 ASM] Unnamed set of 3D routines
« Reply #27 on: July 05, 2013, 10:17:16 pm »
That is awesome! Is that 6MHz? Also, what other kinds of math routines do you have in there? ^_^

Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
Re: [z80 ASM] Unnamed set of 3D routines
« Reply #28 on: July 06, 2013, 04:48:44 am »
Yes, I keep repeating that I always work on projects at 6 MHz !

And for now I only have LUT-based sincos, your HLtimesA et your HLsdivDE.

Offline Streetwalrus

  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3821
  • Rating: +80/-8
    • View Profile
Re: [z80 ASM] Unnamed set of 3D routines
« Reply #29 on: July 06, 2013, 05:05:47 am »
Yes, I keep repeating that I always work on projects at 6 MHz !
Add it to your signature. :P
Anyway that's looking freakin' awesome. O.O