A lot of this is from memory, and no cites so my memory could easily be off (or I might have read some things that we now know are complete nonsense).
In the ancient world, you really didn’t have dedicated defense researchers in any sort of organized fashion like DARPA. Weapon design came from weapon smiths, and that was usually a specialized guild. There was a lot of guild knowledge that was passed down from one generation of guild members to the next, and they were always doing their own research of sorts. If you were a really smart dude and could figure all kinds of things out, and you managed to impress someone with your creativity, someone with a lot of money might pay you to work in a room somewhere and create things. It was more like a patronage system than a DARPA type system though.
The flintlock was actually invented in this type of patronage. A talented artist, inventor, gunsmith, and luthier named Marin le Bourgeoys impressed the French royals and was given rooms in the Louvre to experiment in and create art. In the early 1600s he created the flintlock, which ruled over battlefields for the next 200 years, only finally being made obsolete by the percussion lock in the years leading up to the U.S. Civil War.
As for ship building, there weren’t many folks handing out patronages for someone to experiment with ships. However, the folks who knew how to make ships were always looking for ways to improve their ships, so when the local king/emperor/whatever decided he wanted new war ships, they would try to make an improved version based on what worked and what didn’t the last time around. But ships were too expensive to have some guy just constantly building ships to see what worked better and what didn’t.
Siege weapons were often made by carpenters. During peace time, they would build homes or whatever. In war, they would pack up their tools, follow the army to their destination, and start cutting down trees in order to make siege towers, ladders, etc.
The term “engineer” only goes back to maybe the 10th or 12th century or somewhere thereabouts. By this time, you now had dedicated dudes who had specialized knowledge in the creation and operation of siege engines (hence, “engineer”).
There were two very significant exceptions to all of this, the ancient Romans and the ancient Chinese. The ancient Roman engineers were called “architecti” (I think - something like that - it’s related to the modern word “architect”, since engineers and architects weren’t separate things back then). They would do their own research and development of weapons and engineering solutions, and they were much more advanced than anyone else at that time. If the Roman army needed to cross a river, rather than be forced to a choke point at a bridge or a suitably shallow river crossing, the architecti would build a bunch of simple boats, tie them all together, and build a bridge across all of them to make a huge floating bridge.
When the Roman Empire collapsed, much of this specialized engineering knowledge was lost for centuries.
Unfortunately, my knowledge on the Chinese side of things is severely lacking. I know that they also had large and complex siege machines, and they had engineers that figured out how to turn gunpowder into deadly weapons. But beyond that, I really don’t know much about how Chinese engineering knowledge was passed down from generation to generation or how they experimented to discover new weapons or ways to improve existing weapons.