Sunday, July 8, 2018

Which to buy for gaming, a TV or a monitor?

Nowadays, to the average consumer there seems to be little to no difference between a TV and a computer display monitor. They look the same, they use the same technologies, they use the same resolutions and refresh rates, they have HDMI input... The only difference seems to be physical size. When computer display monitors, even 4k ones, have typical diagonal sizes of about 24-32 inches, TVs tend to have diagonal sizes of 40, 50 and even more inches.

If you are purchasing a new display primarily for gaming, even (or especially) if it's console gaming, which kind of display should you purchase? After all, a huge 50-inch 4k TV sounds enormously cooler for gaming, especially for console gaming, than some tiny 27-inch 4k display. And with the prices of both types of display coming down by the month, the TVs aren't necessarily even any more expensive than the computer displays. (Indeed, you can already find both decent 4k TVs and decent 4k monitors in the ballpark of 500€, sometimes even less.)

So what is the difference between these two types of display, besides physical size? Does it make any difference?

There indeed is one relatively crucial difference. And it's something that TVs very rarely advertise in their technical specifications: Response time.

Response time is the average time that it takes for a pixel to change its color.

With computer monitors, modern TN panels have extraordinarily fast response times, down to 1 millisecond, sometimes even less. On the other hand TN panels have horrible viewing angles, especially vertically. IPS and VA/AMVA panels (which are becoming more and more commonplace, also with TVs) have significantly better viewing angles, and their response time is typically 4 milliseconds, sometimes less (and sometimes more, depending on how cheap the display is).

TVs, on the other hand, have typical response times in the 10-12 millisecond range. That's almost four times the response time of a modern IPS computer display.

The side-effect of a slower pixel response time is ghosting. The pixels are simply slow to change colors, and thus moving details leave a faint "trace" behind. With 10ms it's really subtle, and especially with lower framerates (such as 30 fps, which is your typical movie framerate) it's almost unnoticeable. However, with higher framerates, like 60 and above, and very fast-paced movement it can become visible.

With a 4 ms response time ghosting is essentially unnoticeable (unless you go much higher than 60 fps, which most IPS panels do not yet support). With a TN panel with 1 ms response time there's quite literally no ghosting, even if you go to extremely high framerates, such as 144 Hz.

There may be another drawback in using a TV for gaming: Latency.

Most TVs post-process the image they receive in various ways, and this post-processing may introduce a delay in displaying the picture. This delay may range from 1 to half a dozen frames. The longer this delay is, the more laggy and slow to respond the game will feel.

Some/many TVs will offer a so-called "game mode", which will make it eschew using any post-processing and display the image as quickly as possible. This may fix the latency (or reduce it significantly, eg. to just 1 frame) and should always be used when playing a video game.

Needless to say, computer displays do not have this kind of latency (other than the minimum amount introduced by vsync, but this is not the fault of the display per se.)

Thursday, May 31, 2018

The role of the graphics card driver in real-time rendering

Common computer wisdom accentuates the importance of having the graphics card driver for your computer always up to date. Updates to this driver can fix bugs and misbehavior in games, and sometimes even make games run faster.

However, have you ever wondered why that is, especially in the case of the latter? Shouldn't it be the graphics card itself (in addition to your other hardware) that solely determines how fast a game runs? Why does the graphics card driver sometimes have such a great impact on the rendering speed of a video game? What role does it play in the whole process, and why is it so crucial?

On a more fundamental level, what exactly does a graphics card driver do?

(Note that the explanation below is a simplification, perhaps even an oversimplification. But it is so for the sake of brevity and clarity, even if it might not be technically 100% accurate in every single detail.)

Most typically, a program like a video game uses a graphical application program interface definition like DirectX, OpenGL, or Vulkan. These are not program libraries per se, but rather they are API specifications. In essence, what this means that the program will have code to make certain system function calls for different types of tasks related to rendering, such as loading a bunch of vertices into the graphics card memory, or compiling a fragment shader.

The implementations of these functions will be provided either by the running operating system (eg. Windows), or the graphics driver. Essentially, what's happening eg. in the case of DirectX, is this:


The program executable binary will have metadata that tells to the operating system "I need to call these system functions from these locations within myself". When the operating system loads the binary in order to be run, it modifies it in order to redirect those calls to the appropriate system implementation.

If the graphics card driver provides an implementation for a specific function, it will direct the program binary to call that. In some cases the implementation is not in the driver, and thus a kind of default implementation is provided by the DirectX runtime, which the operating system is running. (Sometimes if a graphics driver doesn't provide an implementation for something, the DirectX runtime essentially needs to emulate that feature. This is why there needs to be a DirectX library in the system at all.) With some implementations the DirectX implementation may call the implementation in the graphics driver.

So it ends up like this:


For most functions, the graphics driver needs to provide an implementation because only it knows how exactly to interact with the graphics card hardware, and what kind of data it needs.

For example, the graphics card driver has a compiler implementation that compiles vertex and fragment shaders into the machine code that the graphics card uses.

In the case of DirectX and Vulkan (and some other APIs, like Metal), the original shader source code is compiled into a hardware-independent intermediate bytecode form, when the program binary itself is being built. At runtime this intermediate form is sent to the graphics driver, which produces from it the actual machine code used by the graphics card.

In the case of OpenGL, and its shading language (glsl), OpenGL doesn't specify any such intermediate form, and the graphics card driver literally has a compiler implementation that takes the original glsl source code in textual form and compiles it into machine code.

This explains why the graphics card driver has such an important role in the entire process, and also why it has such a big impact in the performance of games. Oftentimes graphics card manufacturers will publish an updated driver that's specifically optimized for a particular new video game, and will make it run measurably faster (eg. several frames per second faster than with the old driver).

How? What they do is that they examine what kind of calls the game is doing, and with what kind of data. For example, they may notice that the game is doing a lot of calls with certain data in a certain way, and that if the graphics driver reorganizes that data in a certain manner, it makes it more efficient for the graphics card hardware to handle (without changing the behavior).

Likewise they may notice that the game uses certain types of shader code quite a lot and heavily, and may notice that the shader compiler can optimize that code to shave off a few clock cycles (again without changing its behavior), which may end up speeding up the overall rendering by several frames per second.

Tuesday, May 15, 2018

How many times can the same position repeat in chess?

Most official chess tournaments have a so-called "three-fold repetition" rule, which in short means that if the same board position is repeated three times, the game can be declared as draw. (In human tournaments declaring it as draw is optional and up to the players, even though many tournaments have an additional rule that says that if the same position repeats five times, the tournament referee can declare it a draw. In computer chess tournaments three-fold repetition is invariably declared a draw automatically.)

So the answer appears to be pretty obvious: Three times, duh.

Except it's actually not that simple. You see, there are additional rules with respect to what is considered the "same board position". Even though all the pieces might be on the exact same squares as in a previous position, for the purposes of this rule it might not be considered the "same position".

For starters, castling rights are taken into consideration when defining whether a position is the same as a previous one. Even if all the pieces are on the same squares as before, if however now one of the players has lost the castling right to either side, which he had in that previous position, it will be considered a different position.

So, how many times can the same arrangement of pieces repeat in this light? (We don't need to worry about how realistic in actual games this is. We are just pondering what the theoretical absolute maximum is.)

So, this is theoretically possible: Both players have castling rights to both sides. The same position repeats twice. Then one of the players loses one of these castling rights, after which the same position repeats an additional two times. And so on with the remaining three castling rights. Then, after the last one, the position can repeat a third time, and it will be draw.

So 2 initial times + 2 for each lost castling right (ie. times 4) + 1 final time = 11 times?

Nope! There are more conditions we haven't touched! You see, the rule also states that it has to be the same player to play, in order for it to be considered the "same" position, in the context of this rule.

Thus, we can actually repeat each of the above positions twice, except for the very last one, as long as it's the other player's turn each second time. (The last one can't be repeated with the other player having the turn, because the last one already makes it a draw immediately.)

Thus 21 times?

Still no! There's still one additional rule: Having en-passant right vs. not having it is considered yet another aspect that makes the position different.

How many times can we repeat the same position with en-passant? Only once. This is because this condition requires a pawn move, and can't be repeated with all pieces ending up on the same squares. Thus we have to start the whole sequence with having a player have en-passant right, after which it's lost, and we can proceed with the rest of the sequence above.

So the final answer is 22 times.

Wednesday, May 2, 2018

Indirect protection in chess

Consider the following position:


Black has just played the bishop from f5 to c2, attacking the a4 pawn.

Seemingly there is no way to protect that pawn. It's gone, and we simply have to accept this loss, which is rather significant in this part of the game, with this few pieces left. A pawn advantage at this stage is extremely important, but we just lost it.

Or did we?

There is a way to protect the pawn from being captured, but it's a rather sneaky indirect way of protection. Play the knight to b7!


How does this protect the pawn? The knight isn't directly protecting it. However, it is protecting it indirectly. If the bishop were to now capture the a4 pawn, we can fork the king and the bishop, and gain the bishop:


The a4 pawn cannot be captured. We could later protect the pawn directly by moving the knight to c5, but as long as the black king remains at d7, there is no hurry. If black moves something else, we can afford to likewise do something else. If the black king moves anywhere, we can then play the knight to c5, safely protecting the a4 pawn directly.

This is one of the tactical aspects of chess that can be difficult for many beginners to see and learn to read.

Thursday, January 18, 2018

The en passant rule causes problems to beginners

Some time around the 15th century, a new rule was added to chess: A pawn in its initial position (ie. 2nd rank if white, 7th rank if black) can advance one or two squares. Previously it could only advance one square. This change was made to allow a game to develop faster.

This new rule added one negative side-effect, though: It allows a white pawn to evade capture by an black pawn that's on the 4th rank, in an adjacent file (and vice-versa with a black pawn evading capture by a white pawn on the 5th rank), by advancing two squares. In order to combat this, an additional rule was added: The so-called en passant rule. In this situation, during the next move the pawn on the 4th rank (5th if reversed situation) is allowed to capture that pawn that just advanced as if it had advanced only one square. (This is the only situation in chess where capturing doesn't put the capturing piece on the same square as the captured one.)

The en passant rule feels a bit of a kludge, or a rule patch. It introduces a type of capture that's completely unique, and unlike any other capture in the game. It also is unusual in that the option to make this capture is only temporary, which is also quite unique, and unlike any of the other rules of the game (with castling rights being the only other rule that somewhat resembles this.) The fact that it was introduced to "fix" a problem in another recently-introduced rule makes it feel even more of a kludge.

It's also a relatively obscure rule for beginners. A surprising amount of beginner players have actually never heard of this rule, nor know of its existence. If somebody makes the move against them, they will often call foul, and even accuse the other player of cheating. If they are playing against a computer, and the computer makes the move, they often think that it's a bug in the program (or that the program is cheating somehow).

Sometimes even after they have been explained the rule, some such players keep feeling like it's somehow "cheating", or "unfair", or doesn't make much sense. I once read a comment by someone who seriously thought that this rule wasn't actually part of "normal" chess, that nobody actually uses it, and that computers using it is just semi-cheating, and that computer games should have an option to turn the rule off, like in "real" games of chess. (Apparently this guy had always played chess with people who agreed that the rule shouldn't be used, and got somehow the impression that it's so everywhere with everybody, or at least most people.)

The en passant rule, however, adds a surprising amount of tactical depth to the game. It doesn't happen very often, but when it does, it can be quite powerful. Just the threat of en passant capture can affect the tactics of the situation significantly (even if the capture itself never actualizes). The people who originally came up with the rule in the 15th century might not have been aware of how tactically deep the rule is, and thus it might be serendipitous, but it really adds a lot to the game.

Just having a pawn on the 5th rank (or 4th if playing black), if well protected, and depending on the situation, can be a great asset, precisely because it might be stopping both opponent pawns in the adjacent files from advancing.

One of my own games had this kind of situation (I'm not especially strong, just about 1450 elo on chess.com, but I still like this situation as illustrative):


That pawn on e5 had been a thorn in my opponent's side for the majority of the game, hindering the advancement of the d-pawn. This position is still relatively even, even though I'm now sure to capture the d-pawn. My opponent, perhaps because of time pressure or just weakness, tried desperately to save it, and advanced it two squares, no doubt hoping that I didn't know about en passant, or didn't notice the possibility.


Of course I did notice. Advancing the d-pawn is a bad mistake, and I proceeded to easily win the game with my two connected passed pawns.

Saturday, December 30, 2017

Approximating a function with Taylor series vs. continued fraction

A Taylor series is a polynomial that approximates any given (differentiable) function within a certain range (which, depending on the function in question, may extend indefinitely). The more elements in the polynomial, the more accurate the approximation is. (If the Taylor series is extended to infinity, it's theoretically completely equal to the original function, within its range of validity. In other words, the Taylor series converges towards the original function (with exceptions).)

For example, the first three terms of the Taylor series for the function sin(x) around x=0 are:


In this graph the sin(x) function is drawn in orange, and the above polynomial in blue. As you can see, the polynomial matches the original function decently well for x values close to 0:


What is the point of this? Is this just math for fun and curiosity?

Polynomial approximations of more complex functions are actually very useful in many situations. When such approximations are enough for the task at hand, polynomials are often much easier to handle than the original function (eg. when using these polynomials in physics problems requiring differentiation or integration, or other such applications).

Moreover, notice that these polynomials require solely simple arithmetic operations to calculate: Addition and multiplication (integer powers can be calculated as multiplications, and the divisions are merely multiplications with a constant term). In many applications, such as in microprocessors, it's much easier to approximate things like trigonometric functions using only addition and multiplication, which are much easier to implement in hardware than actual trigonometric functions. (And even if trigonometric functions were implemented, they would probably still internally use an approximation similar to this.)

Note that with functions like sin(x) we can keep adding terms to the Taylor series in order to get better and better approximations around x=0, for an increasingly larger range. (It's thus only a question of how much accuracy is required for the application at hand, and how much time we are willing to spend adding and multiplying more and more terms.)

The Taylor series is, in fact, often used, and mentioned, as a good approximation for such functions.

However, this doesn't mean that it's the best possible approximation that can be done with arithmetic operations alone! Sometimes there are other arithmetic expressions that actually approximate the desired function much better with much less operations required.

For example, the first three terms for the Taylor series of the function tan(x) around x=0 are:


The tan(x) function can also be approximated using a continued fraction. Its first three terms are:


It looks slightly more complicated, but it still requires only simple arithmetic operations (although in this case actual division needs to be added to the set of required operations).

How do these two expressions compare in terms of accuracy? In the following graph the tan(x) function is drawn in red, the polynomial expression above is drawn in blue, and the continued fraction expression is drawn in green (click the image for a larger version):


As you can see, while the polynomial (blue line) deviates from the function quite clearly, the continued fraction (green line) is so close to it that it actually covers it entirely in this image, in the relevant range from -pi/2 to pi/2. And this using only the first three terms!

(Also, curiously, and potentially usefully, while the polynomial is valid only within the range -pi/2 to pi/2, the continued fraction continues to approximate the function outside this range. In fact, if we add more and more terms to it, it will approximate the tan(x) function at a wider and wider range.)

How well do the two methods approximate the function in terms of actual values? Here are some numbers:

x = 0.00, tan(x) = 0.000000000000, taylor = 0.000000000000, contf = 0.000000000000
x = 0.10, tan(x) = 0.100334672085, taylor = 0.100334666667, contf = 0.100334672021
x = 0.25, tan(x) = 0.255341921221, taylor = 0.255338541667, contf = 0.255341880342
x = 0.50, tan(x) = 0.546302489844, taylor = 0.545833333333, contf = 0.546296296296
x = 0.75, tan(x) = 0.931596459944, taylor = 0.922265625000, contf = 0.931451612903
x = 1.00, tan(x) = 1.557407724655, taylor = 1.466666666667, contf = 1.555555555556
x = 1.25, tan(x) = 3.009569673863, taylor = 2.307942708333, contf = 2.986111111111
x = 1.50, tan(x) = 14.101419947172, taylor = 3.637500000000, contf = 12.750000000000

As seen from these values, even with just three terms the continued fraction approximates the function much better. For example for x=0.75 the Taylor series (with three terms) is only accurate to one decimal place, while the continued fraction is accurate to three.

As mentioned earlier, to improve the accuracy of either method, we can add more terms to them. Let's try adding just one more term to both, in other words:


and


How well do they compare now?

x = 0.00, tan(x) = 0.000000000000, taylor = 0.000000000000, contf = 0.000000000000
x = 0.10, tan(x) = 0.100334672085, taylor = 0.100334672063, contf = 0.100334672085
x = 0.25, tan(x) = 0.255341921221, taylor = 0.255341835627, contf = 0.255341921180
x = 0.50, tan(x) = 0.546302489844, taylor = 0.546254960317, contf = 0.546302465023
x = 0.75, tan(x) = 0.931596459944, taylor = 0.929469517299, contf = 0.931595136956
x = 1.00, tan(x) = 1.557407724655, taylor = 1.520634920635, contf = 1.557377049180
x = 1.25, tan(x) = 3.009569673863, taylor = 2.565283396887, contf = 3.008942661757
x = 1.50, tan(x) = 14.101419947172, taylor = 4.559598214286, contf = 14.042553191489

The Taylor series does not show a lot of improvement, but the continued fraction became quite significantly better. For example at that x=0.75 we looked at before, the Taylor series is still only accurate to one decimal place, while the continued fraction is now accurate to 5 decimal places.

Even at the quite extreme value x=1.5 the continued fraction became accurate to two significant digits (while the Taylor series is still far, far off).

While the Taylor series is usually presented as the archetypal method for approximating functions, it's not always the method that does so in the most efficient way. Although, to be fair, the continued fraction method requires actual divisions (which the Taylor series does not need), which may or may not be a problem depending on the application.

Thursday, September 28, 2017

What is "game mode" in modern TVs?

When watching speedrunners playing old console games, and even newer ones, it might be a bit surprising that many of them prefer a good old-fashioned chunky CRT TV over a modern LCD/LED TV (even though many of the latter support composite input, or in the worst case scenario, there exist converters).

Many people who don't know the reason might be a bit surprised by the reason: Input lag.

CRT TVs have essentially no input lag whatsoever. This means that the console is perfectly "in sync" with the cathode ray of the TV, ie. anything that the console is outputting for the current scanline is immediately shown on screen. The longest possible lag between eg. a controller button press and its effect happening on screen is 1/60th of a second (or 1/50th in the case of PAL), because that's how long the refreshing of the screen takes. This is more than sufficient for human reflexes (eg. in speedrunning).

This implies that LCD/LED TVs have some kind of input lag. That's often the case. Depending on the particular brand and model of TV, this input lag can vary all the way from one entire frame to half a dozen frames.

Modern TVs typically perform all kinds of post-processing to the image they are receiving for display. They might perform brightness/contrast adjustments, color correction, noise reduction, and all sorts of things. They buffer the input video signal they are taking, and perform these operations on the individual buffered images before displaying them. In some cases this might take several frames. (In other words, the image you are seeing might have been sent via the HDMI or whatever cable to it several frames prior, meaning something like 3/60ths or 4/60ths of a second ago.)

When watching TV or a DVD/BluRay this doesn't make any difference. It really doesn't matter if the BluRay player sent it the picture eg. 3/60ths of a second prior. It's impossible to even notice.

However, it can become noticeable when the TV is used to play a console game (or sometimes even a PC game). The more lag there is between you pressing a controller button, and its effect happening on screen, the more noticeable it becomes. For power-players (such as speedrunners and many online gamers) this input lag may be crucial. The better your reflexes are in a given game, the more you notice the input lag, and the more it hinders your gameplay.

Computer displays, even though in the modern day they use pretty much identical technology to TVs, generally eschew any such post-processing and display images as soon as they get it (meaning that the input lag is, optimally, at most that 1/60th of a second. Or even less if the display has a higher refresh rate.)

This is where the so-called "game mode" of many modern TVs comes in. Many people don't know what it means, and might think it's just a mode that adjusts brightness, contrast, color correction and other such things for games to look better (at least in the manufacturer's opinion). However, what it usually means is that the TV forgoes most of its post-processing steps in order to display the input image as soon as possible, reducing input lag.

With some TVs this might mean that it, for example, reduces the lag from something like 4/60ths of a second to just 2/60ths. A few TVs might even reach the computer monitor speed of 1/60ths of a second.

If you play with a console using a TV, it's recommended to use the "game mode" setting of the TV, if it has one, so that games will become more "responsive", without that input lag (or, at least, with a minimized input lag).