Advertisement
Guest User

BlitzBASIC 2 PlayBASIC Conversion Tool WIP - Episode 06 - 3D Gouraud Shaded Torus - 14th March 2022

a guest
Mar 14th, 2022
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 28.04 KB | None | 0 0
  1. BlitzBASIC 2 PlayBASIC Conversion Tool WIP - Episode 06 - 3D Gouraud Shaded Torus - 14th March 2022
  2. Blog: https://www.underwaredesign.com/forums/index.php?topic=4625.msg30589#msg30589
  3. Watch: https://www.youtube.com/watch?v=algDfggRTgc&ab_channel=PlayBasic
  4. ----------------------------------------------------------------------------------------------
  5.  
  6. hello and welcome back here we are
  7.  
  8. tackling this blitz basic to play basic
  9.  
  10. converter in today's episode we're
  11.  
  12. actually going to look at
  13.  
  14. the lovely taurus that's spinning around
  15.  
  16. on the screen
  17.  
  18. the current level of the build pretty
  19.  
  20. much puts us at a workable state now it
  21.  
  22. doesn't produce working code for this
  23.  
  24. example
  25.  
  26. but i'll put it on screen
  27.  
  28. for you
  29.  
  30. and we'll write and it's pretty cool
  31.  
  32. okay let's take a look at the original
  33.  
  34. blitz code
  35.  
  36. it was written in 2001 this example and
  37.  
  38. it's quite a nice example when running
  39.  
  40. as you've already seen
  41.  
  42. but the whole thing is
  43.  
  44. all in software there's no hardware
  45.  
  46. acceleration no internal polygon
  47.  
  48. rendering in it
  49.  
  50. all the poly rendering is done
  51.  
  52. uh
  53.  
  54. well brute force really
  55.  
  56. it's drawing a batch of polygons doing
  57.  
  58. this stuff here trying out vertices
  59.  
  60. sorting the faces etc
  61.  
  62. so a lot of work has been done
  63.  
  64. that's my key point here that's the the
  65.  
  66. takeaway point is that there's a lot of
  67.  
  68. work being done
  69.  
  70. until we come right down to the software
  71.  
  72. rendering this example is doing groud
  73.  
  74. shaded polygons and they're also light
  75.  
  76. source which just makes it look a bit
  77.  
  78. more interesting
  79.  
  80. what i've actually done is i've taken
  81.  
  82. the original code
  83.  
  84. and attached the data statements because
  85.  
  86. it was selectable in the original
  87.  
  88. our translator at the moment doesn't
  89.  
  90. understand include statement so we
  91.  
  92. couldn't have that
  93.  
  94. set up this way
  95.  
  96. this way we can just pass it through as
  97.  
  98. one big blob and get a blob out
  99.  
  100. back to the translation
  101.  
  102. so let's run the translator now we'll
  103.  
  104. write in normal mode so we can see the
  105.  
  106. the speed of the conversion
  107.  
  108. uh it's about a thousand lines i think
  109.  
  110. no sorry it's 2000 lines but most of
  111.  
  112. those data statements so don't get too
  113.  
  114. excited
  115.  
  116. so we're doing our conversion in about a
  117.  
  118. hundred and you know i'll be generous
  119.  
  120. say 150 milliseconds not bad
  121.  
  122. if we run into bug mode it'll take a lot
  123.  
  124. longer so it's spitting out a lot of
  125.  
  126. data to the console
  127.  
  128. we've got about half a second there all
  129.  
  130. up so the console is over here
  131.  
  132. that's our code
  133.  
  134. that's been translated
  135.  
  136. the data statements didn't need to be
  137.  
  138. translated at all so they've just passed
  139.  
  140. through
  141.  
  142. uh where are we our program code up here
  143.  
  144. has been converted
  145.  
  146. we go up the top we can see that's the
  147.  
  148. start of our code and
  149.  
  150. sorry above this is the information
  151.  
  152. about the scopes what local variables
  153.  
  154. there were
  155.  
  156. how many times they were used etc some
  157.  
  158. are used very frequently some are used
  159.  
  160. hardly ever
  161.  
  162. so we i think we'll be able to use that
  163.  
  164. to our advantage and give tips about the
  165.  
  166. conversion
  167.  
  168. uh for each scope
  169.  
  170. yeah you know
  171.  
  172. let's not get him ahead of ourselves
  173.  
  174. let's just get this thing converted
  175.  
  176. so let's grab all this
  177.  
  178. head down the bottom yeah okay grab that
  179.  
  180. thing
  181.  
  182. and uh quit out of this
  183.  
  184. okay i've already set up a
  185.  
  186. an empty project for us to paste into so
  187.  
  188. let's paste our code
  189.  
  190. from our conversion across
  191.  
  192. so we're getting 2
  193.  
  194. 700-ish lines of blitzcode
  195.  
  196. now we've got an include file up the
  197.  
  198. front here sorry let's grab that before
  199.  
  200. we've inserted an include file this is
  201.  
  202. coming from our my own wrapper at the
  203.  
  204. moment to emulate blitz functions
  205.  
  206. um let's kind of see how we go
  207.  
  208. uh see how far we get without
  209.  
  210. our dying
  211.  
  212. i know some functions here we're going
  213.  
  214. to need to remap like this read
  215.  
  216. statements here
  217.  
  218. pb has a different syntax for data
  219.  
  220. statements
  221.  
  222. i've got some collisions there too but
  223.  
  224. we'll go through them so we can you can
  225.  
  226. sort of follow what's happening with
  227.  
  228. this stuff we compile
  229.  
  230. right so our first
  231.  
  232. first problem is this read statement
  233.  
  234. uh
  235.  
  236. reading traditional basic is is a
  237.  
  238. command
  239.  
  240. and we don't support that we actually
  241.  
  242. have a read data function
  243.  
  244. to grab the next uh bit of data off of
  245.  
  246. the data
  247.  
  248. we can think of a data like a stack
  249.  
  250. can't you
  251.  
  252. now
  253.  
  254. it's really just like an index to it
  255.  
  256. to a table but
  257.  
  258. so branch ahead
  259.  
  260. uh vertex signal vertex we've got some
  261.  
  262. data statements there put those there
  263.  
  264. get rid of those thanks
  265.  
  266. i can see some problems with this
  267.  
  268. already
  269.  
  270. and they're not to do with the
  271.  
  272. translation they did to do with
  273.  
  274. differences in
  275.  
  276. well
  277.  
  278. how one language
  279.  
  280. likes to use keywords in one one uh
  281.  
  282. doesn't oh undo that thing so what i'll
  283.  
  284. just do then
  285.  
  286. delayed that terrible
  287.  
  288. some dummy reads there so that day has
  289.  
  290. got some empty stuff in it
  291.  
  292. don't need the reed stavens there of
  293.  
  294. course
  295.  
  296. pull them out
  297.  
  298. quick save
  299.  
  300. let's just see what the next thing is
  301.  
  302. doesn't like about this is
  303.  
  304. uh yeah i thought it would like this
  305.  
  306. actually see how we've got a type name
  307.  
  308. called vertex and an array called vertex
  309.  
  310. now
  311.  
  312. it can't really distinguish between the
  313.  
  314. two
  315.  
  316. obviously in blips you're allowed to do
  317.  
  318. that
  319.  
  320. oh in pv you can't so what we'll do is
  321.  
  322. we'll
  323.  
  324. put like a lowercase t in front of the
  325.  
  326. type names
  327.  
  328. and
  329.  
  330. so
  331.  
  332. it's in front of vertex
  333.  
  334. i thought a poly was another one as well
  335.  
  336. poly was one
  337.  
  338. and
  339.  
  340. all light source was one
  341.  
  342. okay
  343.  
  344. so like south park there yeah eric you
  345.  
  346. know there you go
  347.  
  348. that was my mistake terrell
  349.  
  350. ah here we go so this is a byproduct of
  351.  
  352. the wrapping pepe likes that it's um
  353.  
  354. functions to have
  355.  
  356. a pair of brackets on them
  357.  
  358. okay
  359.  
  360. this is something that popped up when i
  361.  
  362. was doing the a monster truck a most to
  363.  
  364. play basic conversion
  365.  
  366. in blitz um it's valid to have a
  367.  
  368. variable called fps
  369.  
  370. in pb that's the name of a function
  371.  
  372. so
  373.  
  374. the error is going
  375.  
  376. hang on what are you trying to do with
  377.  
  378. fps you can't do that with this so we
  379.  
  380. have to rename this um
  381.  
  382. when i did the amos converter it would
  383.  
  384. just put a prefix in front of these ones
  385.  
  386. that's what we'll do now we'll just go
  387.  
  388. yep
  389.  
  390. anything that's fps
  391.  
  392. just call it local fps yeah
  393.  
  394. i'm going to call it anything we want to
  395.  
  396. do there's ones over there too
  397.  
  398. hidden away anymore
  399.  
  400. can't see any more of those so let's
  401.  
  402. just compile and see how we go
  403.  
  404. oh text yeah text is a bit of a pain
  405.  
  406. because of the conversion
  407.  
  408. i've got a version of text that'll do
  409.  
  410. strings but here we've got a combination
  411.  
  412. of
  413.  
  414. string data and integer data so we have
  415.  
  416. to have like a string wrapper around
  417.  
  418. this
  419.  
  420. for the amos converter i did
  421.  
  422. actually
  423.  
  424. that's actually what i did is it went
  425.  
  426. through it it
  427.  
  428. did a rough reese
  429.  
  430. a rough sort of bracketing of these
  431.  
  432. these things here just assuming that
  433.  
  434. they're integers or whatever
  435.  
  436. i might just get rid of the wrapping for
  437.  
  438. this
  439.  
  440. command
  441.  
  442. because text is pretty much text there's
  443.  
  444. not much we're just drawing stuff in the
  445.  
  446. current font
  447.  
  448. i will have to actually put strings
  449.  
  450. around these
  451.  
  452. isn't that terrible oh good goodness
  453.  
  454. goodness gracious
  455.  
  456. uh
  457.  
  458. put the closing bracket on
  459.  
  460. yeah it's definitely something we should
  461.  
  462. definitely try and tackle
  463.  
  464. and then the variable collision stuff
  465.  
  466. too that could be a problem with big
  467.  
  468. code bases
  469.  
  470. um i forgot about that one
  471.  
  472. next one let's just hit compile again
  473.  
  474. see how we got
  475.  
  476. that flip need some brackets
  477.  
  478. flip is basically sink
  479.  
  480. you know
  481.  
  482. places plays in play basic lands just
  483.  
  484. flipping the back buff for the front
  485.  
  486. buffer
  487.  
  488. hey
  489.  
  490. that's a long way through
  491.  
  492. lock buffer
  493.  
  494. i think it blitz it requires a parameter
  495.  
  496. and we'll let's set up to
  497.  
  498. set up so that surface 0
  499.  
  500. is the screen so we'll just put lock
  501.  
  502. buffer 0
  503.  
  504. for that and there's another collision
  505.  
  506. there see how it's got rgb equals
  507.  
  508. this
  509.  
  510. expression wow i'm surprised it's done
  511.  
  512. like that
  513.  
  514. um
  515.  
  516. we'll leave that there
  517.  
  518. just to show you that that's going to
  519.  
  520. upset play basics see how this variable
  521.  
  522. name here it's going hey you've got a
  523.  
  524. function
  525.  
  526. using the function name in some wacky
  527.  
  528. way what are you doing
  529.  
  530. ah we'll just call it
  531.  
  532. put l in front of it for local or
  533.  
  534. something
  535.  
  536. you can so how far we get
  537.  
  538. right pixel
  539.  
  540. uh
  541.  
  542. oh it's got a surfacing on the end
  543.  
  544. point zero for surface
  545.  
  546. you know
  547.  
  548. pretend like we know what we're doing
  549.  
  550. unlock buffer no
  551.  
  552. zero for surface yep good on you
  553.  
  554. fantastic no no
  555.  
  556. oh man so on
  557.  
  558. so the functions ex
  559.  
  560. yeah okay
  561.  
  562. so the bottom there we're exporting a
  563.  
  564. integer which is a assumed return from
  565.  
  566. from the translation
  567.  
  568. um so for this exit function here we
  569.  
  570. have the same
  571.  
  572. same response
  573.  
  574. otherwise you might just get some stack
  575.  
  576. misalignment
  577.  
  578. um there is an error for that actually
  579.  
  580. if you get one
  581.  
  582. press on dudes let's go
  583.  
  584. yeah oh
  585.  
  586. yeah
  587.  
  588. yeah some basics will allow you to do
  589.  
  590. if expression and then then and then as
  591.  
  592. long as there's nothing following them
  593.  
  594. then you can you can use an end if
  595.  
  596. statement we don't do that
  597.  
  598. i must trap this one and cut it out or
  599.  
  600. just add it to the phrase at some point
  601.  
  602. lock buffer it's going to be screen
  603.  
  604. isn't it
  605.  
  606. unlock's going to be the
  607.  
  608. screen right pixel
  609.  
  610. one two three
  611.  
  612. pv
  613.  
  614. back buffer same collision
  615.  
  616. issue there
  617.  
  618. so that we're constructing a color and
  619.  
  620. then running it to the screen so i've
  621.  
  622. got a lot of work to draw a pixel
  623.  
  624. yeah so it's going to run pretty pretty
  625.  
  626. terribly initially but um i know we can
  627.  
  628. speed it up
  629.  
  630. when we get this thing going
  631.  
  632. same drama
  633.  
  634. don't need the then statements thanks
  635.  
  636. hit save
  637.  
  638. ah we've got through the code that's
  639.  
  640. cool
  641.  
  642. got a die in front of the label anymore
  643.  
  644. what else we got
  645.  
  646. chords
  647.  
  648. you can pile again
  649.  
  650. one more
  651.  
  652. surely that must be the end of the data
  653.  
  654. let's hit run
  655.  
  656. wow it's running
  657.  
  658. and it
  659.  
  660. actually is running but there's some
  661.  
  662. problems with it
  663.  
  664. as you can see
  665.  
  666. and one of the problems is not what you
  667.  
  668. what you're expecting
  669.  
  670. i did put this the other day and ran
  671.  
  672. into the same same drama so
  673.  
  674. i know roughly what the what this issue
  675.  
  676. is going to be
  677.  
  678. so before we tackle that i'll just
  679.  
  680. demonstrate we're working
  681.  
  682. why is working up front
  683.  
  684. so
  685.  
  686. we won't draw um the polygons as groud
  687.  
  688. we'll just draw them as a set of
  689.  
  690. vertices
  691.  
  692. the function's already in here
  693.  
  694. hopefully it should work there you go
  695.  
  696. in other words the code that handles the
  697.  
  698. rotation uh projection etc and
  699.  
  700. the polysorting that's all actually
  701.  
  702. working
  703.  
  704. what's not working is this drawing this
  705.  
  706. draw polygon function
  707.  
  708. uh
  709.  
  710. if we cut poly out
  711.  
  712. put one of these line savings in now
  713.  
  714. this has the old syntax see how it's got
  715.  
  716. the the backslash
  717.  
  718. that's the blitz syntax
  719.  
  720. because this code was inside a comment
  721.  
  722. and this wasn't converted
  723.  
  724. um quick save
  725.  
  726. let's hit
  727.  
  728. run
  729.  
  730. hmm so we are actually working
  731.  
  732. that's pretty cool i like that
  733.  
  734. and the rate's not too bad either we're
  735.  
  736. getting you know 10s and 15s etc
  737.  
  738. uh
  739.  
  740. we can do better now
  741.  
  742. sorry
  743.  
  744. i might just put a space bar
  745.  
  746. on this main loop where are we really
  747.  
  748. i've set an exit time there so if it
  749.  
  750. runs for a number of frames it'll exit
  751.  
  752. by itself i might drop that down to 50
  753.  
  754. frames
  755.  
  756. um
  757.  
  758. it sounds like a short time but
  759.  
  760. uh when we get this we start tackling
  761.  
  762. this um
  763.  
  764. this poly rendering routine it's very
  765.  
  766. slow initially because of all the
  767.  
  768. wrapping
  769.  
  770. it has a few bugs in it too that we've
  771.  
  772. got to actually track down
  773.  
  774. so it'll run for 50 foot and i'll run
  775.  
  776. for
  777.  
  778. 200 frames 250 frames okay
  779.  
  780. or until hit the space bar all this guy
  781.  
  782. all the escape go
  783.  
  784. spacebar good
  785.  
  786. right let's tackle this draw polly
  787.  
  788. go to
  789.  
  790. the groud poly function
  791.  
  792. um
  793.  
  794. i think what we'll do is
  795.  
  796. so we'll actually have a look at this uh
  797.  
  798. oh are we drawing the lines
  799.  
  800. or not what are we drawing
  801.  
  802. all right that's a little
  803.  
  804. that's the x
  805.  
  806. for this row
  807.  
  808. it's the x from the next row and we'll
  809.  
  810. draw it in like a
  811.  
  812. green maybe
  813.  
  814. so i should draw a strip for where the
  815.  
  816. hot each horizontal strip of the
  817.  
  818. the polynomial's drawing is
  819.  
  820. um
  821.  
  822. should
  823.  
  824. now clearly the lines are outside of the
  825.  
  826. polygons because the polygons are these
  827.  
  828. white lines here
  829.  
  830. and our lines are way outside that so
  831.  
  832. something's not right
  833.  
  834. i do know what the problem actually is
  835.  
  836. so i'll talk you through it this one
  837.  
  838. here
  839.  
  840. it's in this edge function
  841.  
  842. where we're lying on
  843.  
  844. well
  845.  
  846. let's go back a bit you might have
  847.  
  848. noticed that all of the values here
  849.  
  850. for coordinates
  851.  
  852. uh see how we've got x1
  853.  
  854. and then we've got rgb coordinates for
  855.  
  856. it we've got x2 rgb coordinates
  857.  
  858. uh these are to draw
  859.  
  860. down the edge of the polygons as it's
  861.  
  862. being scanned converted
  863.  
  864. now when we have a triangle we we work
  865.  
  866. out what's the top point
  867.  
  868. and we work out what edges we need to
  869.  
  870. draw and we draw
  871.  
  872. ideally from the top to the bottom you
  873.  
  874. can actually draw in any whatever your
  875.  
  876. order you like
  877.  
  878. as long as you end up with a bunch of
  879.  
  880. lists bunch of spans
  881.  
  882. that represent the shape
  883.  
  884. now this doesn't work
  885.  
  886. because here we've got a translation
  887.  
  888. where we're going
  889.  
  890. we're subtracting x1 from x2 and then
  891.  
  892. shifting it up 8 bits
  893.  
  894. if x1 x2 are positive
  895.  
  896. that's fine
  897.  
  898. and it's the same goes for y1 y2 if
  899.  
  900. that's positive that's fine
  901.  
  902. but at any point
  903.  
  904. we end up with something that's that's
  905.  
  906. negative
  907.  
  908. we're mixing signed arithmetic with
  909.  
  910. bitwise arithmetic
  911.  
  912. so something between how the shift
  913.  
  914. operator works in blitz and how the
  915.  
  916. shift operator works in pv is different
  917.  
  918. here i'm just moving the bits just going
  919.  
  920. hey
  921.  
  922. shift the bits up who cares about the
  923.  
  924. side of it
  925.  
  926. so we've actually got to do a division
  927.  
  928. here or sorry multiplication
  929.  
  930. so so
  931.  
  932. if we shift something
  933.  
  934. if we shift something up eight bits
  935.  
  936. i'll just call it scala i guess
  937.  
  938. so we're just going
  939.  
  940. um
  941.  
  942. we can do it like this
  943.  
  944. we can compute that scala
  945.  
  946. that's the level of precision
  947.  
  948. which is going to be an integer value of
  949.  
  950. 256.
  951.  
  952. so we could substitute in here
  953.  
  954. we're multiplying this sorry wrong
  955.  
  956. operator
  957.  
  958. now because pv is interpreted
  959.  
  960. the cost of having in an integer mold or
  961.  
  962. an integer shift is not that great
  963.  
  964. to be honest
  965.  
  966. it's really almost nothing
  967.  
  968. well actually i wonder there too see how
  969.  
  970. we've got
  971.  
  972. we've got a shift and then in addition i
  973.  
  974. wonder if the precedence
  975.  
  976. of this is actually a problem
  977.  
  978. let's just check that
  979.  
  980. hey we're here now let's just do it yeah
  981.  
  982. come on
  983.  
  984. i know you're bored i know you want to
  985.  
  986. see it running fantastic yeah
  987.  
  988. this is computing our edges if our
  989.  
  990. engines look normal then that's our
  991.  
  992. problem that it's not
  993.  
  994. there's not a difference between the
  995.  
  996. shift it's a difference between the
  997.  
  998. precedence of the operators
  999.  
  1000. i don't know
  1001.  
  1002. let's find out together
  1003.  
  1004. hey that's the problem
  1005.  
  1006. that's nice to know
  1007.  
  1008. yeah all right
  1009.  
  1010. just to check that
  1011.  
  1012. that means we can get away with we don't
  1013.  
  1014. need to do
  1015.  
  1016. those modifications we can just go yeah
  1017.  
  1018. all right do your thing dude
  1019.  
  1020. do this that's very good do this thing
  1021.  
  1022. very good
  1023.  
  1024. lovely long as it still works
  1025.  
  1026. the slide performance is actually
  1027.  
  1028. a common problem with direct draw
  1029.  
  1030. applications all right so that that's
  1031.  
  1032. actually solved
  1033.  
  1034. our problem so
  1035.  
  1036. if we go back
  1037.  
  1038. that's our pixel rendering loop
  1039.  
  1040. that's our line rendering rendering loop
  1041.  
  1042. now if we do
  1043.  
  1044. something
  1045.  
  1046. simple like we go a lock buffer here
  1047.  
  1048. so we're drawing all of these lines
  1049.  
  1050. batched up with and we're locking the
  1051.  
  1052. surface
  1053.  
  1054. that should
  1055.  
  1056. make people a lot happier when we're
  1057.  
  1058. doing this job
  1059.  
  1060. should
  1061.  
  1062. yeah it's much happier with that
  1063.  
  1064. cool
  1065.  
  1066. now we know the the
  1067.  
  1068. vertex locations are accurate we know
  1069.  
  1070. that our
  1071.  
  1072. they're all the same so now we need to
  1073.  
  1074. tackle this in a loop and i know this
  1075.  
  1076. three slides could take you know
  1077.  
  1078. quite a while to render so i'll put that
  1079.  
  1080. abort case
  1081.  
  1082. back into the code
  1083.  
  1084. of you know
  1085.  
  1086. i have it be uh
  1087.  
  1088. 20 frames
  1089.  
  1090. see how bad we get how bad it runs up
  1091.  
  1092. from up front
  1093.  
  1094. about a frame a second
  1095.  
  1096. but
  1097.  
  1098. it is running
  1099.  
  1100. can we improve on this you better
  1101.  
  1102. how we're going to do it well the same
  1103.  
  1104. old way we always do it
  1105.  
  1106. like in the buffers dude
  1107.  
  1108. so we know it works and all of our
  1109.  
  1110. efforts there have been have not been in
  1111.  
  1112. vain so
  1113.  
  1114. get rid of the line drawing stuff there
  1115.  
  1116. for a second
  1117.  
  1118. go up to
  1119.  
  1120. and draw a polygon and go have a look at
  1121.  
  1122. the draw
  1123.  
  1124. oh
  1125.  
  1126. that's right below it
  1127.  
  1128. right
  1129.  
  1130. now
  1131.  
  1132. right pixel is pretty much a wrapper
  1133.  
  1134. around
  1135.  
  1136. the
  1137.  
  1138. the dot function so we can substitute
  1139.  
  1140. right pixel
  1141.  
  1142. with dot we don't need to know the
  1143.  
  1144. buffer all the time we don't
  1145.  
  1146. people work that stuff out for us so we
  1147.  
  1148. could just do this
  1149.  
  1150. dot c which is the colored version
  1151.  
  1152. for the lrgb and that should help us out
  1153.  
  1154. a lot
  1155.  
  1156. doesn't seem like it has though does it
  1157.  
  1158. and
  1159.  
  1160. the main reason for that is that we are
  1161.  
  1162. drawing
  1163.  
  1164. 500 polygons in every scan line of those
  1165.  
  1166. polygons which is going to be let's say
  1167.  
  1168. each polygon's got 20 scan lines so
  1169.  
  1170. that's
  1171.  
  1172. 570 times 20.
  1173.  
  1174. that's how many times we are locking and
  1175.  
  1176. unlocking the surface
  1177.  
  1178. that's a lot
  1179.  
  1180. uh so what we need to do what we should
  1181.  
  1182. do really is that rather than lock the
  1183.  
  1184. buffer
  1185.  
  1186. every time we draw a strip draw one one
  1187.  
  1188. span
  1189.  
  1190. which is how this is has provisioned for
  1191.  
  1192. it before
  1193.  
  1194. go up to the
  1195.  
  1196. animal drawer these four those
  1197.  
  1198. so here we go
  1199.  
  1200. would you look at that
  1201.  
  1202. so we're running actual translated blitz
  1203.  
  1204. basic code
  1205.  
  1206. uh it's drawing every polygon dot by dot
  1207.  
  1208. we've got some buffer management control
  1209.  
  1210. in there
  1211.  
  1212. we're drawing lots of extra dots there
  1213.  
  1214. too so i bet they they don't have great
  1215.  
  1216. uh buff sorry
  1217.  
  1218. buffer management stuff in them as well
  1219.  
  1220. but we're still doing something that's
  1221.  
  1222. not great though isn't it we're locking
  1223.  
  1224. the buffers
  1225.  
  1226. every time we draw a polygon so if we
  1227.  
  1228. draw the more polygons we draw
  1229.  
  1230. in this case here if we if
  1231.  
  1232. we have 500 polygons and half of those
  1233.  
  1234. polygons are visible
  1235.  
  1236. so let's say 250 on average
  1237.  
  1238. then
  1239.  
  1240. we're going to be drawing attempting to
  1241.  
  1242. lock the buffer and unlock it
  1243.  
  1244. 250 times so we want to actually
  1245.  
  1246. probably a better solution would be to
  1247.  
  1248. lock the buffers
  1249.  
  1250. at the start of the polygon batching and
  1251.  
  1252. then release it at the end
  1253.  
  1254. let's try that then so let's draw a
  1255.  
  1256. polygon routine here
  1257.  
  1258. we're running through and doing all this
  1259.  
  1260. good stuff so we'll just do the locking
  1261.  
  1262. it at this level
  1263.  
  1264. and strip out the other stuff we added
  1265.  
  1266. before
  1267.  
  1268. where are we there we are there we are
  1269.  
  1270. so it locks the current surface by the
  1271.  
  1272. way if you're not familiar
  1273.  
  1274. uh draw vertices
  1275.  
  1276. there's no buff although there is some
  1277.  
  1278. there that's not too bad
  1279.  
  1280. sorry
  1281.  
  1282. let's go to grab polygon find that thing
  1283.  
  1284. we don't need to lock the buffer in here
  1285.  
  1286. so
  1287.  
  1288. we can do our strip
  1289.  
  1290. render our strips
  1291.  
  1292. and the buffer state is being managed on
  1293.  
  1294. a higher level so we don't need to have
  1295.  
  1296. all this pleasing thank you with those
  1297.  
  1298. that there's this low level here
  1299.  
  1300. that shouldn't make it perform much
  1301.  
  1302. better
  1303.  
  1304. should
  1305.  
  1306. and does
  1307.  
  1308. now we're capturing the screen we're
  1309.  
  1310. drawing a bunch of dots as well so you
  1311.  
  1312. know
  1313.  
  1314. it's not bad
  1315.  
  1316. it's just a complete translation of the
  1317.  
  1318. same application
  1319.  
  1320. uh clearly the inner loop here has some
  1321.  
  1322. things there we could probably get rid
  1323.  
  1324. of
  1325.  
  1326. like
  1327.  
  1328. if we know we're never drawing outside
  1329.  
  1330. of the display
  1331.  
  1332. constraints
  1333.  
  1334. we can swap dot with uh fast
  1335.  
  1336. fast dot
  1337.  
  1338. and we probably should do
  1339.  
  1340. here i think we're always rendering to
  1341.  
  1342. the same surface
  1343.  
  1344. so that would mean our our lock would be
  1345.  
  1346. appropriate for the for the same surface
  1347.  
  1348. all the time
  1349.  
  1350. but you've got to be careful with that
  1351.  
  1352. stuff
  1353.  
  1354. one way to ensure that's never going to
  1355.  
  1356. was going to work as we can read
  1357.  
  1358. if you ask
  1359.  
  1360. us pb to read a pixel from the surface
  1361.  
  1362. it will see the locking every time so we
  1363.  
  1364. read one pixel at the start of the batch
  1365.  
  1366. of all the polygons then we never needed
  1367.  
  1368. to do it again
  1369.  
  1370. if you're jumping around changing images
  1371.  
  1372. you're rendering to locking something
  1373.  
  1374. unlocking it
  1375.  
  1376. the
  1377.  
  1378. the optimizations that track the lock
  1379.  
  1380. state can
  1381.  
  1382. can you know they can work against you i
  1383.  
  1384. know
  1385.  
  1386. i'm a terrible person
  1387.  
  1388. should be the same
  1389.  
  1390. cool
  1391.  
  1392. that's pretty good some stuff in here
  1393.  
  1394. too yeah i could grab the
  1395.  
  1396. grab this thing here
  1397.  
  1398. um
  1399.  
  1400. just call it
  1401.  
  1402. this is your index
  1403.  
  1404. grab your index from there
  1405.  
  1406. save reading that array three times
  1407.  
  1408. so i'll read it once and then just throw
  1409.  
  1410. it away
  1411.  
  1412. those kind of you know there's micro
  1413.  
  1414. optimizations you can run through and do
  1415.  
  1416. those things yourself
  1417.  
  1418. um
  1419.  
  1420. you probably probably always treat your
  1421.  
  1422. colors separately and have the light
  1423.  
  1424. source computed
  1425.  
  1426. or you've got more actually
  1427.  
  1428. you can clip the range there's for
  1429.  
  1430. example c2 equals
  1431.  
  1432. clip range
  1433.  
  1434. c2
  1435.  
  1436. north of 255
  1437.  
  1438. you know
  1439.  
  1440. that sort of thing
  1441.  
  1442. um you're probably not really winning
  1443.  
  1444. back any performance by doing those
  1445.  
  1446. things but it can just reduce your code
  1447.  
  1448. it's pretty cool i like it
  1449.  
  1450. i like it a lot ah right
  1451.  
  1452. that's our translation for you um hope
  1453.  
  1454. you enjoyed that
  1455.  
  1456. let's say we wanted to push this a bit
  1457.  
  1458. further we could we could make this a
  1459.  
  1460. bit more play basic friendly
  1461.  
  1462. get rid of the dot rendering stuff
  1463.  
  1464. draw vertices get rid of that
  1465.  
  1466. don't really need it let's check this
  1467.  
  1468. thing out
  1469.  
  1470. that's nice
  1471.  
  1472. uh one thing we could do is obviously
  1473.  
  1474. since we're in pv land
  1475.  
  1476. now draw a polygon
  1477.  
  1478. now
  1479.  
  1480. let's head down to
  1481.  
  1482. our draw a line where are those
  1483.  
  1484. this particular section of code here
  1485.  
  1486. that's it's forming an rgb color
  1487.  
  1488. what we couldn't do is we could of
  1489.  
  1490. course
  1491.  
  1492. use the rgb function
  1493.  
  1494. rgb
  1495.  
  1496. was it rt
  1497.  
  1498. uh gt and then bt
  1499.  
  1500. we don't have to do this in software so
  1501.  
  1502. we you know we're two function calls
  1503.  
  1504. that approximate
  1505.  
  1506. this is
  1507.  
  1508. we get rid of a shift a shift and two
  1509.  
  1510. additions
  1511.  
  1512. but we substitute them for one function
  1513.  
  1514. call
  1515.  
  1516. let's try it
  1517.  
  1518. it's pretty hard to know
  1519.  
  1520. if if we've won much back in that i
  1521.  
  1522. would say we probably have
  1523.  
  1524. obviously the best way to do this in pv
  1525.  
  1526. is actually to do this
  1527.  
  1528. let's draw a groud strip from x1
  1529.  
  1530. from r1 g1
  1531.  
  1532. b1
  1533.  
  1534. to x2 rgb
  1535.  
  1536. it's going to be
  1537.  
  1538. r2
  1539.  
  1540. blue 2
  1541.  
  1542. and green two and
  1543.  
  1544. on the y row so we can just get rid of
  1545.  
  1546. all of that really
  1547.  
  1548. yeah
  1549.  
  1550. it's pretty comfortable now it's
  1551.  
  1552. obviously we're switching a lot of the
  1553.  
  1554. the runtime dependency to command set
  1555.  
  1556. dependencies
  1557.  
  1558. uh which is because the built-in
  1559.  
  1560. function for drawing a grad strip is
  1561.  
  1562. pretty much
  1563.  
  1564. well it's pretty optimal you know
  1565.  
  1566. compared to
  1567.  
  1568. i don't know how many cycles this takes
  1569.  
  1570. to
  1571.  
  1572. to execute through the through the
  1573.  
  1574. runtime but
  1575.  
  1576. every single pixel is doing that
  1577.  
  1578. i guess what you could do is you could
  1579.  
  1580. use some of the
  1581.  
  1582. [Music]
  1583.  
  1584. the paired pixel optimization stuff
  1585.  
  1586. where you
  1587.  
  1588. you know when you draw
  1589.  
  1590. you unroll the this inner loop a bit
  1591.  
  1592. further you draw pairs of pixels you
  1593.  
  1594. know in one pass compute them etc and
  1595.  
  1596. that actually works pretty well to be
  1597.  
  1598. honest
  1599.  
  1600. and you draw a pair of dots
  1601.  
  1602. at one time rather than a single lot
  1603.  
  1604. that makes that can be beneficial
  1605.  
  1606. because you're drawing
  1607.  
  1608. or you're saving
  1609.  
  1610. some overhead from calling a function
  1611.  
  1612. which in the runtime is quite expensive
  1613.  
  1614. so even though this is relatively fast
  1615.  
  1616. you know this is the fully
  1617.  
  1618. fully interpreted rendering loop
  1619.  
  1620. so we're probably somewhere in that you
  1621.  
  1622. know in the
  1623.  
  1624. between 30 to 60 frames per second
  1625.  
  1626. which is stunning to me
  1627.  
  1628. while it's been captured or
  1629.  
  1630. now this is going to be you know
  1631.  
  1632. comparing to the
  1633.  
  1634. the native loop that draws ground pixels
  1635.  
  1636. it's not that much detriment it's
  1637.  
  1638. probably twice as fast let's say
  1639.  
  1640. if i converted the whole thing to use
  1641.  
  1642. the groud polygon
  1643.  
  1644. it would be much faster again
  1645.  
  1646. but i want
  1647.  
  1648. i'll
  1649.  
  1650. just revert the code back to how it was
  1651.  
  1652. and you can
  1653.  
  1654. tackle this yourself
  1655.  
  1656. what if we avoided drawing the line at
  1657.  
  1658. all and just drew
  1659.  
  1660. a ground line in here
  1661.  
  1662. so we computed the start of the line
  1663.  
  1664. x1 yep go for it
  1665.  
  1666. x2 is going to equal
  1667.  
  1668. it's the here
  1669.  
  1670. so we just need these
  1671.  
  1672. rgb
  1673.  
  1674. values here
  1675.  
  1676. and that function should pretty much
  1677.  
  1678. work the way it was working before
  1679.  
  1680. we've got x y x one y is the same
  1681.  
  1682. let's try her out
  1683.  
  1684. i think for this approach we're pretty
  1685.  
  1686. much um
  1687.  
  1688. at maximize our throughput for it
  1689.  
  1690. to draw strips
  1691.  
  1692. to get quicker we're gonna have to go to
  1693.  
  1694. drawing polygons
  1695.  
  1696. just draw the whole thing
  1697.  
  1698. um
  1699.  
  1700. grab triangle x1 y1
  1701.  
  1702. rgb
  1703.  
  1704. one x2 y2
  1705.  
  1706. rgb2
  1707.  
  1708. x3
  1709.  
  1710. y3 rgb3
  1711.  
  1712. exit function to avoid all that other
  1713.  
  1714. stuff at the bottom there
  1715.  
  1716. you know
  1717.  
  1718. uh so rgb one equals rgb rgb
  1719.  
  1720. g1 b1
  1721.  
  1722. because the command expects uh the
  1723.  
  1724. parameters to be fed to it
  1725.  
  1726. as colors
  1727.  
  1728. um
  1729.  
  1730. that are compressed
  1731.  
  1732. so they're packed together as
  1733.  
  1734. rgb rather than separate rgb
  1735.  
  1736. fields
  1737.  
  1738. all right
  1739.  
  1740. let's give it a go
  1741.  
  1742. goodness gracious
  1743.  
  1744. oh
  1745.  
  1746. i've got a return to zero
  1747.  
  1748. yeah no surprises there
  1749.  
  1750. is you can see it kind of peaking at a
  1751.  
  1752. few hundred frames per second
  1753.  
  1754. what if we just keep those in the code
  1755.  
  1756. or have uh
  1757.  
  1758. i'll make three different versions of it
  1759.  
  1760. so version two
  1761.  
  1762. which will just be the
  1763.  
  1764. the line optimization and version three
  1765.  
  1766. will be the full polygon rendering
  1767.  
  1768. thing so we'll just
  1769.  
  1770. cut all of that out
  1771.  
  1772. so version three of the
  1773.  
  1774. function will be this
  1775.  
  1776. don't need anything below that
  1777.  
  1778. version two will be the
  1779.  
  1780. so doing all this work here great all
  1781.  
  1782. going well and then we're doing this
  1783.  
  1784. so we
  1785.  
  1786. using ground strip to do for this
  1787.  
  1788. version
  1789.  
  1790. uh
  1791.  
  1792. where are we and
  1793.  
  1794. for the original version we just you
  1795.  
  1796. were doing
  1797.  
  1798. as it was the stock version
  1799.  
  1800. uh
  1801.  
  1802. just
  1803.  
  1804. restore that to how it was cut all that
  1805.  
  1806. stuff there out again
  1807.  
  1808. excuse this video for being so long
  1809.  
  1810. so the inner loop no we want the actual
  1811.  
  1812. pixel rendering
  1813.  
  1814. run it give it a test
  1815.  
  1816. we're back to the normal software
  1817.  
  1818. rendering
  1819.  
  1820. which is still pretty good
  1821.  
  1822. cool
  1823.  
  1824. what if we put a switch in there
  1825.  
  1826. to choose which rendering method we
  1827.  
  1828. wanted
  1829.  
  1830. to
  1831.  
  1832. [Music]
  1833.  
  1834. global uh render
  1835.  
  1836. random method equals zero by default
  1837.  
  1838. uh at the bottom here we'll just
  1839.  
  1840. display this to the user
  1841.  
  1842. i don't draw up there actually
  1843.  
  1844. so i'll come down about
  1845.  
  1846. that lori 60 random method
  1847.  
  1848. random
  1849.  
  1850. random method
  1851.  
  1852. and
  1853.  
  1854. just call it render
  1855.  
  1856. yeah i'll do that
  1857.  
  1858. everything on the end here render
  1859.  
  1860. sorry method
  1861.  
  1862. come on
  1863.  
  1864. i know it's light it's not that light
  1865.  
  1866. add a bit of crappy logic up front you
  1867.  
  1868. know
  1869.  
  1870. all the good stuff
  1871.  
  1872. select
  1873.  
  1874. uh render
  1875.  
  1876. method
  1877.  
  1878. case zero
  1879.  
  1880. top
  1881.  
  1882. this is the fast dot
  1883.  
  1884. in a loop
  1885.  
  1886. then select
  1887.  
  1888. a couple of those
  1889.  
  1890. two
  1891.  
  1892. this is the
  1893.  
  1894. ground
  1895.  
  1896. strip
  1897.  
  1898. round triangle
  1899.  
  1900. um
  1901.  
  1902. so we'll just have a
  1903.  
  1904. bit of a check here um
  1905.  
  1906. i don't know if the answer is press or
  1907.  
  1908. something
  1909.  
  1910. whatever
  1911.  
  1912. uh this bump that
  1913.  
  1914. bigger than two
  1915.  
  1916. making it a zero it's kind of a default
  1917.  
  1918. case
  1919.  
  1920. i couldn't be bothered
  1921.  
  1922. isn't that terrible
  1923.  
  1924. now i could use like
  1925.  
  1926. call function in this and do some
  1927.  
  1928. as9
  1929.  
  1930. kind of crap
  1931.  
  1932. uh we we'll just have the polygon
  1933.  
  1934. rendering stuff wherever that's going to
  1935.  
  1936. uh draw a poly
  1937.  
  1938. select random method
  1939.  
  1940. have a select case
  1941.  
  1942. very old school
  1943.  
  1944. all right so we were method one method
  1945.  
  1946. two i'll make this correlate here
  1947.  
  1948. actually method one
  1949.  
  1950. method two um
  1951.  
  1952. uh sorry i didn't think this was
  1953.  
  1954. going what
  1955.  
  1956. what the hell
  1957.  
  1958. all right so method one is the
  1959.  
  1960. ground strip version and polygon
  1961.  
  1962. 2 is the full blown polygon version
  1963.  
  1964. let's try it out so we can check them
  1965.  
  1966. side by side
  1967.  
  1968. all right so looking at the the fast dot
  1969.  
  1970. which is the original emulation of the
  1971.  
  1972. blitz code
  1973.  
  1974. we're switching to ground strip mode
  1975.  
  1976. which is we're drawing the inner strips
  1977.  
  1978. with our groud strip command
  1979.  
  1980. we're peaking at 125 frames per second
  1981.  
  1982. we're averaging around over 60 frames
  1983.  
  1984. per second now
  1985.  
  1986. all in software
  1987.  
  1988. on the interpreter so with polygon
  1989.  
  1990. we're hitting 250 frames per second
  1991.  
  1992. the average is coming up and up and up
  1993.  
  1994. now of course that's going to be
  1995.  
  1996. quickest because we're doing the
  1997.  
  1998. we're passing control from the
  1999.  
  2000. interpreter
  2001.  
  2002. to machine code to fill the triangle and
  2003.  
  2004. come back
  2005.  
  2006. i hope this was insightful have you
  2007.  
  2008. learned something hope you've interested
  2009.  
  2010. in something
  2011.  
  2012. thanks for watching i'll see you next
  2013.  
  2014. time
  2015.  
  2016. bye
  2017.  
  2018.  
  2019.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement