Latest / CyberSecurity Summary / Developing Graphics Frameworks with Java and OpenGL
Transcript
- 0:00Speaker 1: So if you take a look at the screen in
- 0:01Speaker 1: front of you right now, you might think you're seeing,
- 0:03Speaker 1: I don't know, a deep three dimensional landscape or high
- 0:06Speaker 1: speed motion.
- 0:07Speaker 2: Solid objects that cast actual real shadow.
- 0:10Speaker 1: Right exactly, but you are, well, you're actually hallucinating because
- 0:14Speaker 1: what you are really staring at is a completely flat
- 0:17Speaker 1: grid of tiny, physically static colored lights and they're updating,
- 0:21Speaker 1: you know, sixty to ninety times a second.
- 0:23Speaker 2: It's an incredibly flawless optical illusion, it really is.
- 0:26Speaker 1: And beneath the glass of that screen there is this relentless,
- 0:31Speaker 1: just hyper fast factory. It's an assembly line processing literally
- 0:35Speaker 1: millions of mathematical calculations every single millisecond just to keep
- 0:40Speaker 1: that illusion from shattering.
- 0:41Speaker 2: Yeah, it's arguably one of the most complex slights of
- 0:45Speaker 2: hand in modern human history. We were talking about taking
- 0:48Speaker 2: abstract and visible mathematics inside a processor and transforming it
- 0:52Speaker 2: instantly into a fully realized, immersive visual world.
- 0:56Speaker 1: And that is exactly our mission for this deep dive.
- 0:59Speaker 1: We are going to figure out out how that factory
- 1:01Speaker 1: actually works, like how do we go from raw math
- 1:03Speaker 1: to the real time three D environments of massive video
- 1:07Speaker 1: games or virtual reality or high end architectural simulations.
- 1:13Speaker 2: Right to guide us today, we're pulling from a really
- 1:15Speaker 2: excellent twenty twenty two textbook called Developing Graphics Frameworks with
- 1:19Speaker 2: Java and OpenGL. It's by Lease Demkowski and James Kona.
- 1:22Speaker 1: Yeah, and the source doesn't just talk about the theory,
- 1:24Speaker 1: which is great. It gets right down into the absolute
- 1:27Speaker 1: nuts and bolts of the code that makes a graphics card.
- 1:30Speaker 2: Tick.
- 1:31Speaker 1: Okay, let's unpack this. Before we can start constructing a
- 1:34Speaker 1: three D universe. We kind of have to understand the
- 1:36Speaker 1: canvas we are painting on, right, the engine itself.
- 1:39Speaker 2: Right, the physical canvas. Because the end goal of all
- 1:41Speaker 2: this incredibly dense graphics processing is a concept called rendering,
- 1:46Speaker 2: and the final output of rendering is well, it's surprisingly basic.
- 1:49Speaker 1: It's just a raster, a raster being simply a two
- 1:52Speaker 1: D grid of pixels, just a digital mosaic.
- 1:55Speaker 2: Essentially precisely, and on modern systems, every single one of
- 1:58Speaker 2: those pixels in the mosaic defines its color using just
- 2:01Speaker 2: three floating point numbers, right, And these numbers range from
- 2:05Speaker 2: zero to one, and they represent red, green, and blue light.
- 2:08Speaker 2: The classic RGB triples.
- 2:10Speaker 1: Which, if I remember correctly, is designed specifically to mimic
- 2:13Speaker 1: the photoreceptors in our human.
- 2:15Speaker 2: Eyes, right, yes, exactly that.
- 2:16Speaker 1: So if the computer feeds a pixel the numbers zero, zero, zero,
- 2:20Speaker 1: it means zero red, zero green, zero blue. That is
- 2:24Speaker 1: pure black, no light being emitted at.
- 2:26Speaker 2: All, spot on. And if it feeds it at one one, one,
- 2:30Speaker 2: you get maximum white across all three channels, which your
- 2:33Speaker 2: eye perceives as pure white.
- 2:34Speaker 1: Oh I see, yeah, And.
- 2:36Speaker 2: If you put in point five for all three you
- 2:38Speaker 2: get a medium gray. Basically, every color you have ever
- 2:40Speaker 2: seen on a screen is just a specific mathematical mix
- 2:43Speaker 2: of those three values. But you know how convincing that
- 2:47Speaker 2: final images depends heavily on the balance between resolution and precision.
- 2:50Speaker 1: Now, resolution is something most people are pretty familiar with.
- 2:53Speaker 1: It is just the raw number of pixels on your grid.
- 2:56Speaker 1: A four K screen has more pixels than a ten
- 2:58Speaker 1: eighty p screen. But precision is a bit different, right right.
- 3:02Speaker 2: Precision is about the memory allocated to each individual pixel.
- 3:07Speaker 2: It is how many bits of data you are allowed
- 3:09Speaker 2: to use to describe that RGB mix, and that dictates
- 3:13Speaker 2: your overall color count.
- 3:14Speaker 1: Got it. So, if you look back at retrogaming consoles
- 3:17Speaker 1: like the old eight bit era, the hardware only had
- 3:20Speaker 1: enough memory to use eight bits of precision per pixel.
- 3:23Speaker 2: Yeah, and two to the power of eight means the
- 3:25Speaker 2: entire system could only ever display two hundred and fifty
- 3:27Speaker 2: six different colors.
- 3:29Speaker 1: That is why those old games have that chunky, hyperstylized look.
- 3:32Speaker 1: The system literally didn't have the vocabulary to draw a
- 3:35Speaker 1: smooth gradient.
- 3:36Speaker 2: It just couldn't do it. But today the standard is
- 3:39Speaker 2: twenty four bit precision, which is often called true color.
- 3:42Speaker 2: That gives the computer over sixteen million possible color combinations
- 3:46Speaker 2: for every single pixel.
- 3:47Speaker 1: Wow, which introduces an enormous computational hurdle I'd imagine because
- 3:51Speaker 1: the screen isn't static, it has to update all of
- 3:54Speaker 1: those sixty million colors constantly to create the illusion of.
- 3:57Speaker 2: Motion exactly, which brings us to the frame rate. Movies
- 4:00Speaker 2: in a cinema traditionally run at twenty four frames per second,
- 4:03Speaker 2: which gives them that classic cinematic blur. The computer monitor
- 4:08Speaker 2: or phone you're looking at right now probably refreshes at
- 4:11Speaker 2: sixty frames per second. But the text points out something
- 4:14Speaker 2: fascinating about virtual reality.
- 4:16Speaker 1: Oh yeah, this was wild. If you are building a
- 4:19Speaker 1: VR simulation, sixty frames per second is completely unacceptable. You've
- 4:23Speaker 1: absolutely must hit a minimum of ninety frames per second.
- 4:27Speaker 2: Because in VR, the screen is strapped to your face.
- 4:30Speaker 2: It's literally replacing your entire visual field, right, So if
- 4:33Speaker 2: the simulation drops below ninety frames per second, your physical
- 4:36Speaker 2: head turns, but the digital world updates just a tiny
- 4:40Speaker 2: fraction of a second too.
- 4:41Speaker 1: Late, and your inner ear feels the motion, but your
- 4:43Speaker 1: eyes don't see it instantly exactly.
- 4:45Speaker 2: That sensory conflict causes genuine physical disorientation. It will literally
- 4:50Speaker 2: induce nausea in the user.
- 4:52Speaker 1: So if you are a programmer, you have to ensure
- 4:54Speaker 1: the computer can calculate and draw a new grid of
- 4:57Speaker 1: millions of pixels perfectly ninety times every single second, without fail.
- 5:02Speaker 2: It's a massive undertaking, and in the.
- 5:04Speaker 1: Early days of computing, the CPU, the central processing unit,
- 5:08Speaker 1: had to do all this work. But using a CPU
- 5:11Speaker 1: for graphics is a terrible mismatch. I like to look
- 5:14Speaker 1: at the CPU like it's a single, incredibly brilliant mathematician.
- 5:18Speaker 2: That's a good way to put it, right.
- 5:19Speaker 1: If you give that mathematician an impossibly complex, multi layered
- 5:23Speaker 1: word problem, they will solve it flawlessly. But if you
- 5:26Speaker 1: ask that same brilliant mathematician to take a crayon and
- 5:29Speaker 1: color an eight million individual boxes on a piece of
- 5:32Speaker 1: paper one by one, ninety times a second, they are
- 5:36Speaker 1: going to fail. It is just the wrong tool.
- 5:38Speaker 2: For the job, which is exactly what forced the tech
- 5:41Speaker 2: industry to invent a completely new piece of architecture, the GPU,
- 5:45Speaker 2: or the graphics processing unit.
- 5:47Speaker 1: I really love this piece of hardware history. It was
- 5:49Speaker 1: Sony that actually coined the term GPU back in nineteen
- 5:52Speaker 1: ninety four when they were developing the original PlayStation because
- 5:55Speaker 1: they just needed a name for the specialized circuitry handling
- 5:57Speaker 1: their three D polygons.
- 5:58Speaker 2: Oh wow, nineteen ninety four.
- 6:00Speaker 1: Yeah, and then in Nvidia popularized the term in nineteen
- 6:03Speaker 1: ninety nine with the release of the g Force two
- 6:05Speaker 1: fifty six.
- 6:06Speaker 2: What's fascinating here is the economic driving force behind this
- 6:09Speaker 2: hardware revolution. The relentless demand of the entertainment and video
- 6:13Speaker 2: game industries actually bankrolled this technology. Oh total, millions of
- 6:18Speaker 2: kids wanting cooler explosions and better textures in their games
- 6:21Speaker 2: paid for the research and development of these massive, highly
- 6:25Speaker 2: parallel processing chips, and today that exact same GPU architecture
- 6:31Speaker 2: built for rendering video game dragons is the foundational engine
- 6:35Speaker 2: running vital life saving technology like MRI visualizations right exactly,
- 6:39Speaker 2: MRI is DNA folding, CT scans, climate modeling, AI training.
- 6:44Speaker 2: It all runs on the back of gaming hardware.
- 6:46Speaker 1: Because if the CPU is that single brilliant mathematician, the
- 6:50Speaker 1: GPU is an army of thousands of basic calculators. They
- 6:53Speaker 1: aren't doing advanced calculus, but they don't need to. They
- 6:56Speaker 1: can all work simultaneously in parallel calculating the math for
- 6:59Speaker 1: millions of individual pixels at the exact same moment, and.
- 7:02Speaker 2: That parallel processing power is the entire secret to making
- 7:05Speaker 2: real time graphics possible. But you know, having an army
- 7:08Speaker 2: of calculators is only half the battle. If you don't
- 7:11Speaker 2: organize them perfectly, all you get is chaos.
- 7:13Speaker 1: So how does a programmer actually organize that army?
- 7:16Speaker 2: By utilizing an abstract architectural model known as the graphics pipeline.
- 7:20Speaker 1: Right, It is quite literally a digital assembly line. You
- 7:23Speaker 1: pour raw numbers into one end, and a fully rendered
- 7:27Speaker 1: three D world comes out the other.
- 7:28Speaker 2: Exactly. The pipeline in this OPENNGL framework operates in four
- 7:33Speaker 2: major stages, and the first stage doesn't even happen on
- 7:36Speaker 2: the GPU.
- 7:37Speaker 1: Wait really, yeah, it's the.
- 7:38Speaker 2: Application stage and it actually runs on your CPU. This
- 7:42Speaker 2: is the overarching program, the Java code. In the context
- 7:46Speaker 2: of our source text. It is reading the player's controller input,
- 7:49Speaker 2: calculating the physics of gravity, and then bundling up all
- 7:52Speaker 2: the raw data like the coordinates of a three D character,
- 7:55Speaker 2: and shipping it over the motherboard to the GPU.
- 7:58Speaker 1: Okay, so once the GPU receives that blue print, we
- 8:00Speaker 1: enter stage two, which is geometry processing. This is where
- 8:03Speaker 1: we deal with vertices, which are simply the points in
- 8:06Speaker 1: three D space that make up our shape. Right, the
- 8:08Speaker 1: raw points, But the computer has a massive perspective problem here.
- 8:12Speaker 1: When a three D model is created, its points are
- 8:15Speaker 1: plotted in what the text calls world space. The coordinates
- 8:19Speaker 1: are fixed within the absolute center of the virtual universe.
- 8:23Speaker 2: But the player isn't looking at the entire universe at once.
- 8:25Speaker 2: The players looking through a very specific window, a virtual camera.
- 8:29Speaker 2: So the very first thing the GPU have to do
- 8:32Speaker 2: is a view transformation.
- 8:33Speaker 1: Gotcha.
- 8:34Speaker 2: It takes every single coordinate in the entire virtual world
- 8:37Speaker 2: and recalculates it to figure out where that point is
- 8:40Speaker 2: relative to the camera lens. It moves everything into viewspace.
- 8:44Speaker 1: And that camera doesn't just see everything in front of
- 8:46Speaker 1: it infinitely. The camera's actual viewing region is a specific
- 8:50Speaker 1: three D shape called a frustum.
- 8:52Speaker 2: Yes, the frustum.
- 8:54Speaker 1: Imagine a traditional four sided pyramid, but you take a
- 8:57Speaker 1: sword and slice off the very top point. Your virtual
- 9:00Speaker 1: camera is sitting at that sliced off top, looking down
- 9:03Speaker 1: through the expanding base of the pyramid.
- 9:05Speaker 2: And only the objects that happen to fall inside the
- 9:08Speaker 2: mathematical boundaries of that specific frustum volume are going to
- 9:11Speaker 2: be processed further.
- 9:12Speaker 1: Here's where it gets really interesting. The concept of clipping
- 9:16Speaker 1: the computer literally discards and throws away the math for
- 9:19Speaker 1: any object outside that.
- 9:21Speaker 2: Frustum, just completely ignores it.
- 9:22Speaker 1: Yeah, if you are playing a game and your character
- 9:25Speaker 1: is standing in the middle of a sprawling, densely populated
- 9:29Speaker 1: virtual city, but you turn your camera to stare closely
- 9:32Speaker 1: at a blank brick wall, the GPU is not rendering
- 9:36Speaker 1: the rest of the city behind you. It completely stops
- 9:39Speaker 1: calculating the traffic, the skyscrapers, the pedestrians behind your back.
- 9:43Speaker 1: It is a total illusion. The world only exists exactly
- 9:46Speaker 1: where you are looking the exact millisecond you look at it.
- 9:49Speaker 2: It is a brilliant optimization. If the GPU tried to
- 9:52Speaker 2: process the geometry for an entire city when you can
- 9:54Speaker 2: only see a wall, system would instantly crash, so it
- 9:57Speaker 2: clips away.
- 9:58Speaker 1: The unseen world maankes total sense.
- 10:00Speaker 2: Then whatever is left inside the frustum goes through a
- 10:02Speaker 2: projection transformation. It mathematically squishes that three D volume into
- 10:07Speaker 2: a standardized two D cube shape so the next stage
- 10:10Speaker 2: can understand it. This is usually a perspective projection, which
- 10:13Speaker 2: artificially compresses things that are further away so they look smaller,
- 10:16Speaker 2: mimicking how human eyes perceive depth.
- 10:19Speaker 1: Okay, so we have our points. They are positioned relative
- 10:22Speaker 1: to the camera, and we have applied realistic perspective, but
- 10:26Speaker 1: they are still just invisible math points. How do we
- 10:29Speaker 1: actually paint them?
- 10:30Speaker 2: That takes us to stage three rasterization.
- 10:33Speaker 1: Rasterization, the GPU groups these floating points into primitives, which
- 10:38Speaker 1: are basic structural shapes, mostly lines or triangles. Any three
- 10:43Speaker 1: D model you have ever seen, whether it is a
- 10:45Speaker 1: hyperrealistic sports car or an alien monster, is really just
- 10:49Speaker 1: a hollow mesh made out of thousands or millions of
- 10:52Speaker 1: tiny flat triangles.
- 10:54Speaker 2: And during rasterization, the GPU analyzes every single triangle in
- 10:58Speaker 2: the scene and mathematically maps it all onto the final
- 11:00Speaker 2: two D grade of your screen. It figures out exactly
- 11:03Speaker 2: which pixels fall inside the borders of that specific triangle.
- 11:06Speaker 1: That's filling it in.
- 11:07Speaker 2: Essentially, yes, for every single pixel inside, it generates a
- 11:09Speaker 2: package of data called a fragment. A fragment is basically
- 11:12Speaker 2: a pixel in waiting. It holds all the geographic data
- 11:15Speaker 2: needed to eventually figure out what color that pixel should.
- 11:17Speaker 1: Be, which brings us to stage four pixel processing. This
- 11:21Speaker 1: is where the actual coloring happens. But reading through the text,
- 11:25Speaker 1: there is this incredibly elegant piece of math that happens
- 11:28Speaker 1: here called interpolation.
- 11:29Speaker 2: Oh. Interpolation is arguably where the GPU shows its true genius.
- 11:35Speaker 2: Imagine you are programming a single flat triangle on the screen.
- 11:39Speaker 2: You don't have to write code to color every single
- 11:41Speaker 2: pixel inside that shape. You only assign colors to the
- 11:45Speaker 2: three corner points the vertices.
- 11:47Speaker 1: So let's say I make the top corner pure red,
- 11:49Speaker 1: the bottom left corner pure green, and the bottom right
- 11:52Speaker 1: corner pure blue. I haven't told the computer what to
- 11:55Speaker 1: do with the empty space in the middle.
- 11:56Speaker 2: You haven't, but the GPU handles it automatically during rosterization.
- 12:00Speaker 2: It looks at a pixel right in the middle of
- 12:02Speaker 2: the triangle, measures how physically close that pixel is to
- 12:04Speaker 2: the red corner, the green corner, in the blue corner,
- 12:07Speaker 2: and then calculates a weighted average of those three colors
- 12:10Speaker 2: based on the distance.
- 12:11Speaker 1: It just silently does the gradient mass exactly a pixel
- 12:14Speaker 1: a little closer to the top corner it gets a
- 12:16Speaker 1: little more red mixedtem It creates a perfectly smooth, blended
- 12:20Speaker 1: rainbow across the entire face of the triangle, and the
- 12:23Speaker 1: programmer didn't have to write a single line of code
- 12:26Speaker 1: for the interior pixels.
- 12:27Speaker 2: The hardware just handles the transition naturally. But you know,
- 12:32Speaker 2: while a single, flat, colorful triangle is nice, it isn't
- 12:36Speaker 2: the three D world. The real challenge is depth.
- 12:38Speaker 1: Oh for sure.
- 12:39Speaker 2: When you have thousands of objects overlapping each other, how
- 12:43Speaker 2: does a flat two D screen know what is in
- 12:45Speaker 2: front and what is hiding in the back?
- 12:47Speaker 1: Right, Because if I write code to draw a massive
- 12:50Speaker 1: gray mountain, and then I write code to draw a
- 12:52Speaker 1: green tree standing in front of the mountain, the computer
- 12:54Speaker 1: needs to know not to paint the mountain over the tree.
- 12:57Speaker 2: Right.
- 12:57Speaker 1: This is managed by something called the frame buffer, which
- 12:59Speaker 1: is the chunk of memory holding the image data as
- 13:03Speaker 1: it is being built. The frame buffer has a color
- 13:05Speaker 1: buffer to hold the RGB values, but crucially, it also
- 13:09Speaker 1: has a depth buffer.
- 13:10Speaker 2: Yeah. The depth buffer is essentially a bouncer at the
- 13:13Speaker 2: door of the pixel grid. It keeps track of the
- 13:15Speaker 2: distance of every single fragment from the camera. Whenever the
- 13:18Speaker 2: GPU tries to color a pixel, the bouncer steps in.
- 13:21Speaker 2: The GP says, hey, I have a green pixel for
- 13:23Speaker 2: a tree, and its distance from the camera is ten.
- 13:27Speaker 2: The depth buffer checks its records and says, well, the
- 13:29Speaker 2: current color saved in this spot is a gray mountain,
- 13:32Speaker 2: but its distance is fifty. Because ten is closer than fifty,
- 13:36Speaker 2: the bouncer allows the GPU to overwrite the gray with green,
- 13:40Speaker 2: and it updates the depth record to ten.
- 13:42Speaker 1: That is a rock solid system for solid, opaque objects.
- 13:46Speaker 1: But as I was reading this, I realized transparency must
- 13:49Speaker 1: be an absolute nightmare for a programmer.
- 13:51Speaker 2: Oh. It is.
- 13:51Speaker 1: If I am looking through a glass window, I need
- 13:53Speaker 1: to see the window itself, but I also need to
- 13:55Speaker 1: see the mountain behind it. So the color buffer isn't
- 13:57Speaker 1: just RGB, it is actually RGB where the A stands
- 14:01Speaker 1: for alpha the transparency channel exactly.
- 14:04Speaker 2: An alpha value of zero is completely invisible, and one
- 14:07Speaker 2: is totally solid. If you set the glass alpha to
- 14:09Speaker 2: point six, the GPU knows to blend sixty percent of
- 14:12Speaker 2: the glass color with forty percent of whatever color is
- 14:15Speaker 2: already sitting behind it in the buffer.
- 14:17Speaker 1: But wait, if the depth buffer just ruthlessly overwrites whatever
- 14:21Speaker 1: is further away. What happens if I mess up the
- 14:23Speaker 1: drawing order in my code? So if I code a
- 14:26Speaker 1: glass window and an opaque brick wall, but tell the
- 14:30Speaker 1: computer to draw the glass first, does the wall just disappear?
- 14:33Speaker 2: This raises an important question, and it highlights what we
- 14:37Speaker 2: can call the transparency paradox. If you draw the glass first,
- 14:40Speaker 2: the illusion shatters completely. The brick wall will simply cease
- 14:43Speaker 2: to exist.
- 14:44Speaker 1: It completely vanishes. Why the computer knows the wall is there?
- 14:48Speaker 2: The application knows, but the depth buffer doesn't care. You
- 14:51Speaker 2: draw the glass window first. The depth buffer updates its record.
- 14:55Speaker 2: We now have an object to distance five A mill
- 14:58Speaker 2: a second later, the GPU tries to draw the opaque
- 15:01Speaker 2: brick wall at distance twenty. The bouncer checks the record.
- 15:04Speaker 2: Oh no, the bouncer sees that distance five is already occupied.
- 15:08Speaker 2: Distance twenty is further away, meaning it must be blocked.
- 15:11Speaker 2: The bouncer immediately discards all the map and colors for
- 15:13Speaker 2: the wall. The depth buffer has no way of knowing
- 15:16Speaker 2: that the object at distance five happens to be see
- 15:18Speaker 2: through glass.
- 15:18Speaker 1: I just threw away the data. Didn't even try to
- 15:20Speaker 1: blend it.
- 15:21Speaker 2: It can't, the data is gone. To prevent this, programmers
- 15:25Speaker 2: have to enforce an incredibly tedious strict rendering rule. You
- 15:29Speaker 2: must draw every single opaque object in your entire virtual
- 15:33Speaker 2: world first, get them all settled in the depth buffer.
- 15:36Speaker 2: Only then can you begin drawing the transparent objects, the water,
- 15:40Speaker 2: the glass, the smoke, and you must sort them and
- 15:42Speaker 2: draw them strictly in order from the furthest away to
- 15:44Speaker 2: the closest.
- 15:45Speaker 1: Which means if a player is walking through a dense
- 15:48Speaker 1: virtual forest with multiple layers of transparent fog and overlapping
- 15:51Speaker 1: glass windows, the CPU has to mathematically recalculate the distance
- 15:56Speaker 1: of every single pane of glass and puff of smoke
- 15:58Speaker 1: relative to the player every single frame, just to figure
- 16:01Speaker 1: out what order to hand them to the GPU. That
- 16:04Speaker 1: is staggering.
- 16:04Speaker 2: He is a massive computational bottleneck which is why poorly
- 16:08Speaker 2: optimized transparency often causes video games to lag. But we've
- 16:12Speaker 2: been talking about all these rules to frustum, the interpolation,
- 16:15Speaker 2: the depth buffering. How does a human programmer actually communicate
- 16:18Speaker 2: these specific rules to that army of calculators on the GPU.
- 16:22Speaker 1: We use shads, and the name is a bit of
- 16:25Speaker 1: a historical artifact. Originally they were just little programs written
- 16:28Speaker 1: to figure out how light shaded an object, but today
- 16:31Speaker 1: they are the absolute core of graphics programming. They are
- 16:34Speaker 1: small custom programs that you compile and send to run
- 16:37Speaker 1: directly on the GPU itself. In this textbooks framework, there
- 16:42Speaker 1: are two main ones, vertex shaders which handle where the
- 16:44Speaker 1: geometry points are in space, and fragment shaders, which calculate
- 16:48Speaker 1: the final RGBA color output.
- 16:50Speaker 2: And the language you use to write these custom programs
- 16:53Speaker 2: in open gl is called GLSL, the OPENNGL shading language.
- 16:57Speaker 2: It looks very similar to the C programming language, but
- 17:00Speaker 2: it is built entirely around processing geometry and color rapidly.
- 17:03Speaker 1: To do that, GLSL uses these very specific data structures
- 17:07Speaker 1: called vectors. In the code, you'll see things like vec two,
- 17:10Speaker 1: vec three, or vec four. These are basically just tight
- 17:12Speaker 1: little packages of two, three, or four floating point numbers.
- 17:15Speaker 1: But what I found amazing was the syntax they use
- 17:17Speaker 1: to access that data.
- 17:19Speaker 2: The dot notation syntax, or the life saver for cognitive load.
- 17:22Speaker 1: It really is. If a programmer creates a vec fourzo
- 17:25Speaker 1: a group of four numbers, they can pull those numbers
- 17:28Speaker 1: out using different letters depending on the context of what
- 17:30Speaker 1: they're doing. If they're working with a position in three
- 17:32Speaker 1: D space, they can type xyz or W to get
- 17:35Speaker 1: the coordinates. But if that exact same package of four
- 17:39Speaker 1: numbers is being used for color, they can just type
- 17:41Speaker 1: RGB or A. It is pulling from the exact same
- 17:45Speaker 1: memory slots, but the language allows the programmer to change
- 17:49Speaker 1: the labels on the fly so they don't lose their
- 17:51Speaker 1: mind trying to remember what the numbers represent.
- 17:53Speaker 2: It is a very human centric design in an otherwise
- 17:56Speaker 2: highly machine oriented process. But you touched on something brussal there,
- 18:01Speaker 2: the W coordinate for position.
- 18:02Speaker 1: Yes, I am so glad you brought that up, because
- 18:04Speaker 1: I was staring at the text trying to figure out
- 18:06Speaker 1: this mathematical quirk. Physical space only has three dimensions up
- 18:10Speaker 1: and down, left and right, forward and backward. The x,
- 18:13Speaker 1: y and z axis. Wait, physical space is three D.
- 18:15Speaker 1: Why on earth do we need a fourth coordinate, the
- 18:17Speaker 1: W and a vex four just to specify a position.
- 18:20Speaker 2: It is entirely a mathematical trick, and it is the
- 18:23Speaker 2: lynchpin of real time graphics. Yeah. In linear algebra, if
- 18:26Speaker 2: you want to scale a three D object to make
- 18:28Speaker 2: it bigger or rotate it, you can easily do that
- 18:32Speaker 2: by multiplying its x, y, and z coordinates by a
- 18:34Speaker 2: three x three grid of numbers called a matrix. Okay,
- 18:38Speaker 2: but here's the problem. You cannot translate an object, meaning
- 18:42Speaker 2: you can't slide it to a new location using a
- 18:45Speaker 2: three x three matrix multiplication. It physically doesn't work. Nor
- 18:49Speaker 2: can you do that complex perspective projection we talked about.
- 18:52Speaker 1: Wait, so if the math doesn't work, you'd have to
- 18:54Speaker 1: write different slower types of mathematical equations for every different
- 18:58Speaker 1: type of movement, like one equation to spin, a different one.
- 19:00Speaker 2: To sluck exactly, which would bottleneck the GPU instantly. But
- 19:04Speaker 2: mathematicians realize to workaround. If you artificially invent a fourth dimension,
- 19:08Speaker 2: this W coordinate, and you just set it to the
- 19:11Speaker 2: number one, you can upgrade your math from a three
- 19:13Speaker 2: x three matrix to a four by four matrix, and
- 19:15Speaker 2: by doing that, suddenly translation, rotation, scaling, and camera perspective
- 19:19Speaker 2: can all be mathematically combined into one single four x
- 19:22Speaker 2: four master matrix.
- 19:23Speaker 1: That is wild. The CPU just crunches all the player's
- 19:27Speaker 1: movements into one master four by four grid of numbers,
- 19:30Speaker 1: hands it to the GPU, and the GPU just multiplies
- 19:33Speaker 1: the object by that one matrix, and all the spinning, sliding,
- 19:36Speaker 1: and zooming happens in a single stroke. It is a
- 19:39Speaker 1: math hack that makes real time three D computationally affordable.
- 19:42Speaker 2: It is a profound optimization. So at this point, the
- 19:46Speaker 2: GPU has done the matrix, math clipped the frustum, interpolated
- 19:49Speaker 2: the colors, resolve the depth buffer, and run the GLSL shaders.
- 19:54Speaker 2: We have a finished frame, right, But how does the
- 19:56Speaker 2: overarching application safely deliver that finished image to your monitor
- 20:00Speaker 2: sixty times a second without it looking like a glitchy,
- 20:03Speaker 2: flashing mess.
- 20:04Speaker 1: This comes down to the application life cycle. The overarching
- 20:07Speaker 1: Java program has a very specific heartbeat, start up, the
- 20:10Speaker 1: main loop, and ship down. Startup gets everything loaded into memory,
- 20:13Speaker 1: but the main loop is where the game lives over
- 20:15Speaker 1: and over again. It processes the player's input updates the
- 20:18Speaker 1: scenes map and triggers the render.
- 20:20Speaker 2: And to prevent the rendering from becoming a glitchy mess,
- 20:23Speaker 2: the framework utilizes a technique called double buffering.
- 20:26Speaker 1: So what does this all mean? The best way I
- 20:30Speaker 1: could picture double buffering in my head was to imagine
- 20:33Speaker 1: a live theater production. You have the main stage where
- 20:36Speaker 1: the curtain is open and the audience is smoothly watching
- 20:39Speaker 1: the actors perform the current scene, but hidden right behind
- 20:43Speaker 1: a heavy velvet curtain, a crew of stage hands is
- 20:46Speaker 1: frantically dragging props around, pinking scenery, and setting up the
- 20:50Speaker 1: next scene in total chaos. Right right, the audience never
- 20:53Speaker 1: sees the mess. The moment the new scene is perfectly ready,
- 20:56Speaker 1: the director drops the curtain on the main stage, instantly
- 21:00Speaker 1: swaps the two sets and opens the curtain again.
- 21:02Speaker 2: That is a perfect analogy. In the computer, you have
- 21:04Speaker 2: two separate image buffers in the memory. The screen is
- 21:07Speaker 2: currently reading and displaying the front buffer the main stage. Meanwhile,
- 21:11Speaker 2: the TPU is acting as the stage hands, frankly writing
- 21:15Speaker 2: millions of pixels of new data to the back buffer.
- 21:17Speaker 1: Because if you only had one buffer, the screen would
- 21:19Speaker 1: be trying to display the image while the GPU was
- 21:22Speaker 1: still halfway through painting it. You'd see the top half
- 21:25Speaker 1: of the screen showing the new frame where you turned
- 21:27Speaker 1: your character's head, but the bottom half of the screen
- 21:30Speaker 1: would still be stuck on.
- 21:31Speaker 2: The old frame, and that is a visual artifact called
- 21:34Speaker 2: screen tearing. By using double buffering and carefully synchronizing the
- 21:38Speaker 2: swapping of those buffers with the physical refresh rate of
- 21:41Speaker 2: your monitor, you ensure the user never sees the stage
- 21:44Speaker 2: hands at work. They only ever see the flawless finished product.
- 21:49Speaker 1: And when those stage hands are actually drawing the shapes.
- 21:52Speaker 1: The code uses things called vertex array objects to manage
- 21:55Speaker 1: all the geometric data. The programmer triggers a drawing by
- 21:59Speaker 1: using commands like gl draw rays, but they have to
- 22:02Speaker 1: specify exactly how the points connect. Yes, they can use
- 22:05Speaker 1: something like a line loop which just plays connect the
- 22:07Speaker 1: dots from point A to B two C and loops
- 22:09Speaker 1: back to A to draw a hollow outline. Or they
- 22:12Speaker 1: can use a triangle fan, which picks a single point
- 22:14Speaker 1: as a central hub and draws solid triangles radiating outward
- 22:18Speaker 1: like spokes on a wheel to fill the shape in completely.
- 22:21Speaker 2: If we connect this to the bigger picture, this entire architecture,
- 22:25Speaker 2: from the jav application loop down to the low level
- 22:28Speaker 2: of triangle fans and GLSL vectors is about maintaining an
- 22:31Speaker 2: impossibly delicate synchronization. It is the synchronization between cold hardware
- 22:36Speaker 2: constraints and the fluid nature of human perception. If any
- 22:40Speaker 2: single part of that typeline stalls, the framework drops, the
- 22:44Speaker 2: illusion breaks, and you are jarringly reminded that you are
- 22:47Speaker 2: just looking at a machine.
- 22:48Speaker 1: It really is incredible when you step back and look
- 22:50Speaker 1: at it. To wrap this all up for you, just
- 22:52Speaker 1: think about the epic journey a single pixel takes before
- 22:55Speaker 1: it reaches your eye. It starts as a mundane variable
- 22:58Speaker 1: assignment in a JavaScript. It gets bundled into a buffer,
- 23:01Speaker 1: shot across the motherboard to the GPU, and multiplied by
- 23:04Speaker 1: a four x four matrix just to figure out where
- 23:06Speaker 1: it exists in the universe. It survives the ruthless clipping
- 23:09Speaker 1: of the frustum, gets grouped into a triangle, has its
- 23:12Speaker 1: colors automatically interpolated, gets tested against a depth bouncer, blended
- 23:17Speaker 1: with transparency values, and finally loaded into a hidden back buffer,
- 23:21Speaker 1: just waiting for the curtain to drop so it can
- 23:23Speaker 1: be swapped onto your screen, all of that millions of
- 23:26Speaker 1: times a second, just so a glowing yellow pixel can
- 23:29Speaker 1: appear in a video game.
- 23:30Speaker 2: It is a staggering amount of invisible computation, and it
- 23:33Speaker 2: leads you with a thought to moll over long after
- 23:36Speaker 2: you turn your screen off. Today, as these GLSL shaders
- 23:39Speaker 2: become exponentially more sophisticated, and as GPUs learn to trace
- 23:43Speaker 2: the actual physical path of light rays in real time,
- 23:45Speaker 2: at what point does the mathematical hallucination of a three
- 23:48Speaker 2: D reality on a flat two D screen become completely
- 23:51Speaker 2: indistinguishable from the real world