Реклама
Fast And Easy GPU Random Numbers In D3D11 - Nathan Reed’s Coding Weblog
16-03-2023, 13:10 | Автор: KristyOToole | Категория: Журналы
Fast And Easy GPU Random Numbers In D3D11 - Nathan Reed’s Coding WeblogWord: please see the 2021 replace to this post, right here. In games and graphics one usually must generate pseudorandom numbers. For sure, PRNGs are an extremely effectively-researched matter; nevertheless, the majority of the literature focuses on purposes with very exacting high quality necessities: cryptography, high-dimensional Monte Carlo simulations, and suchlike. These PRNGs tend to have a whole bunch of bytes of state and take hundreds of directions to update.

That’s means overkill for many extra modest purposes-if you happen to just need to perform a little random sampling in a sport context, you can probably get away with a lot much less. To drive home just how much lower my random quantity standards can be for this article, I’m not going to run a single statistical check on the numbers I generate-I’m simply going to look at them! The human visible system is fairly good at picking out patterns in what we see, so if we generate a bitmap with one random bit per pixel, black or white, it’s easy to see if we’re generating "pretty random" numbers-or if something’s going fallacious.

The one on the left is a linear congruential generator (LCG), and on the precise is Xorshift.

We’re at all times instructed that LCGs are dangerous news, and now you may see just how dangerous! Xorshift, then again, is much better. It’ll truly pass some medium-energy statistical assessments, and it definitely appears random sufficient to the attention. Furthermore, it’s fairly quick in comparison with other PRNGs of similar quality. Since D3D11 GPUs help integer operations natively, it’s straightforward to port these PRNGs to shader code.

GPUs do things in parallel, so we’ll create an unbiased occasion of the PRNG for Crypto-Markets every work merchandise-vertex, pixel, or compute-shader thread. Then we just have to seed them with different values, e.g. utilizing the vertex index, pixel screen coordinates, or thread index, and we’ll get completely different sequences. LCGs are actually quick-updating the state takes only one imad instruction (in HLSL assembly, which is simply an intermediate language, but still a reasonable proxy for machine code velocity).

Xorshift is a bit slower, requiring six directions, but that’s not bad considering the standard of random numbers it gives you.

Figure two or three more directions to get the quantity into the range you need, and convert it to a float if necessary. On a high-finish GPU, you possibly can generate tens of billions of random numbers per second with these PRNGs, easy. Once more, on the left is the LCG and on the proper is Xorshift. The LCG doesn’t look too totally different from before, but Xorshift seems to be completely terrible!

PRNGs are designed to be properly-distributed while you "go deep"-draw many values from the identical instance. Since this entails sequentially updating the state after every value, it doesn’t map effectively to the GPU. On the GPU we have to "go wide"-arrange a variety of impartial PRNG instances with totally different seeds so we are able to draw from every of them in parallel.
Скачать Skymonk по прямой ссылке
Просмотров: 14  |  Комментариев: (0)
Уважаемый посетитель, Вы зашли на сайт kopirki.net как незарегистрированный пользователь.
Мы рекомендуем Вам зарегистрироваться либо войти на сайт под своим именем.