Silicon Valley loves its dick jokes. One of the show’s most memorable: Pied Piper engineers trying to mathematically work out the “optimal tip-to-tip efficiency” of a guy jerking off an entire Tech Crunch audience in the season-one finale. In its own way, this week’s sight gag is just as impressive: After Richard tracks down new CEO Jack Barker at a stud farm because his “number-one breeding mare” is in heat, he’s treated to an eyeful of stallion-sized schlong followed by thoroughbreds screwing. Silicon co-creator and executive producer Mike Judge, who directed “Two in the Box,” says the episode’s epic boner joke wasn’t about upping the ante: The writers simply wanted something going on behind Richard and Jack while the newly demoted CTO complained about how his compression algorithm was being used. “One of our consultants told us about a guy who had made many, many millions, and blew it on horses, wine, and vintage cars,” Judge says. After trying and tossing a vintage-car scene, it was off to the races when executive producer Alec Berg suggested horses humping. Here, Judge reveals the ins and outs of shooting what he thinks is TV’s first equine-sex scene.

The writers educated themselves on the process with YouTube videos, and there was no casting call for a well-endowed stallion.

“I wasn’t too concerned about what the horses looked like,” Judge says about the plan to shoot real thoroughbreds in flagrante. “As long as we could get horses doing that; the whole thing is just so over the top.” After a horse consultant was hired, the writers learned the act is called “natural cover” (thoroughbreds must reproduce by natural, biological means), which Jack marvels at. Despite the show stud’s natural gifts, Judge says in real life, his semen wouldn’t be worth the $150,000 Jack paid.

The first time they tried to shoot the scene, the crew nearly got swept up in an FBI raid.

The original shoot had to be scrapped when the stud farm turned out to be a front for an illegal pot operation. “One of our camera guys would have been arrested if he’d gotten there, like, 15 minutes earlier,” Judge says. New stables in Temecula, California, were found, and those are real wranglers in the scene keeping the mare safe. “The horses go at it so hard, the handlers have to keep the stallion from slamming the mare into a wall or something,” he says.

Judge extended the length … of the scene.

Turns out horses get it on pretty quickly. To make the sex last as long as Richard and Jack’s conversation, Judge used a split screen, combining the tech execs going at it with the horses doing the same.

Thomas Middleditch improvised Richard’s penis-envy line.

Why is Richard so utterly uncomfortable with horses having sex? “He had something important to talk about, and just didn’t want to look at this giant horse penis,” Judge says. “So Thomas threw in that line: ‘You know this is just hard for me.’”

This isn’t necessarily Judge’s favorite dick joke.

Season two’s gag with Kiko the Nicaraguan monkey using his new bionic arm to masturbate (and throw his feces) is right up there.

Will there be more dick jokes this season?

Yes, but they’ll be of the human variety. “There are some romances,” Judge hints, so “some of the guys will be getting some action.” He says to expect “one or two small dick jokes — not size-wise — probably not as graphic as the horses.”