Raytracing in Rust

แชร์
ฝัง
  • เผยแพร่เมื่อ 4 ก.ย. 2023
  • I spent an entire week digging into and implementing my own Raytracing renderer in Rust. In this video we talk about what crates I used and the approaches I took... as well as whether or not it was worth the time.
    I followed the first two books from the Raytracing in One Weekend and Raytracing: The Next Week series: raytracing.github.io/
    The code is available on GitHub: github.com/rust-adventure/ray...
    and a fully written version of this video is available on Rust Adventure: www.rustadventure.dev/i-spent...
    00:23 What is Raytracing?
    01:17 PPM files
    03:55 Showing progress with Indicatif
    04:29 Introducing Glam: vec3
    05:12 Rays
    06:10 Sending Rays into the Scene Through a Viewport
    07:36 Rendering a Sphere
    08:51 Shading the Sphere with Normals
    10:11 Simplifying the ray-sphere intersection
    10:57 The Hittable Trait
    13:40 Refactoring the Camera
    15:03 What is a Pixel anyway?
    17:49 Diffuse Materials
    20:56 gamma correction
    21:42 Metal Materials
    23:43 clamp
    24:11 dielectric (glass) materials
    24:42 Camera positioning
    26:14 Defocus Blur
    27:05 Raytracing in one weekend final scene
    27:36 Rayon
    28:56 Refactoring
    31:39 Raytracing in one Week
    32:14 Motion Blur
    33:30 Efficiency Structures: AABB and BVH
    35:32 Textures
    38:02 Perlin Noise
    39:01 Quads
    40:12 Translate and Rotate
    41:23 Volumetric Materials
    42:39 Final Scene: Raytracing the next week
    43:40 Book 3?
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 46

  • @NetherFX
    @NetherFX 10 หลายเดือนก่อน +61

    It is indeed a tuesday

  • @igz
    @igz 10 หลายเดือนก่อน +8

    This was my first project in rust a year ago, I struggled with it like hell, fun times.

  • @indradb7377
    @indradb7377 10 หลายเดือนก่อน +4

    I wrote a cpu raytracer (similar to this one) as my first rust project but more realtime ish, running at 40 fps multi threaded
    was a good learning experience with Rust

  • @MattBolt35
    @MattBolt35 10 หลายเดือนก่อน +1

    This was an excellent walk through. Thanks for sharing!

  • @jaysistar2711
    @jaysistar2711 10 หลายเดือนก่อน +12

    PPM was common as an X Windows icon format in the 1990s. Using something like TGA or TIF is probably easier as a "trivial to output without a library" format.

    • @LukeFrisken
      @LukeFrisken 10 หลายเดือนก่อน +5

      PPM has a ascii text version which is very easy to implement and debug in a text editor

    • @earx23
      @earx23 7 หลายเดือนก่อน +1

      TGA is simple yes. TIFF a bit harder... to output. To input it's a nightmare: so many variants.

  • @red_metal
    @red_metal 10 หลายเดือนก่อน

    The timing of this video for me couldn't be better, I just finished doing book 1 in Rust a couple days ago, and it's really interesting to see your approach to translating the C++. Yours is definitely "rustier" than mine with more use of options and iterators, and at the same time some things turned out really similar, especially the use of traits. The whole point of doing this for me was to get more familiar with Rust, as I've done book 1 in the past in C++, and I think it was a really good exercise, but having yours to reference actually makes it even more valuable as I can see where I could have made more use of Rust's features. Thanks for sharing!

  • @TeamDman
    @TeamDman 10 หลายเดือนก่อน

    Great video, throwback to the things I learned in my graphics course 🌎
    cool to see how much the Rust ecosystem provides

  • @BrandonReinhart
    @BrandonReinhart 10 หลายเดือนก่อน

    I did this back in the day. Should probably revisit it some time. Fun project.

  • @BrettWitty
    @BrettWitty 10 หลายเดือนก่อน +2

    Really great and honest walk through. I appreciated the Rust short cuts and comparisons with C++.

  • @aure555
    @aure555 10 หลายเดือนก่อน

    Hell yeah long form content lets goooo

  • @Codeaholic1
    @Codeaholic1 10 หลายเดือนก่อน

    This is very cool. I've done some coding train exercises in rust with sdl2. Perhaps I should try this. Thanks for sharing.

  • @nepp9574
    @nepp9574 6 หลายเดือนก่อน

    This honestly felt more like bashing c++, rather than focussing on RT. Nevertheless, nice to see a different implementation. It didn’t hinder me throughout the video.
    Few points;
    - the math optimization early on results in less assembly instructions and thus a faster raytracer. This is explained in the text around it ;)
    - c++ and rust have a lot in common, but also a lot of differences. People use libraries a lot in c++, so there honestly is no reason to add certain functionality to the language if very decent libraries already exist. Rust didn’t have those so made it before there even was a good library. Apples and oranges. Neither one is better because of it.

  • @0.Andi.0
    @0.Andi.0 10 หลายเดือนก่อน

    We need a video series of this 🎉

    • @chrisbiscardi
      @chrisbiscardi  10 หลายเดือนก่อน +1

      I probably won't do a walkthrough on this exact series in particular. If I do a series it will likely be a workshop on Rust Adventure with a different approach to building a Raytracer. There's a lot in the Raytracing in a Weekend series that can be dropped or approached differently due to not using C++.

  • @nicklu9051
    @nicklu9051 10 หลายเดือนก่อน

    damn, this is definitely a full blown course on making ray tracing in rust

  • @Hauntear
    @Hauntear 10 หลายเดือนก่อน +1

    I did this project for a super computing course. We implemented it in C++ with MPI for the cross-communication and did some empirical studies comparing our own message-passing methods to that of MPI's built-in methods (which are optimized and topologically aware). No surprise, we got beat by quite a lot by MPI's own implementation. At most we ran a render of a 5k image on 4096 cores, which took something like ~1 second - crazy. We did a lot of samples per pixel and bounces too, but I don't remember quite how many.

    • @chrisbiscardi
      @chrisbiscardi  10 หลายเดือนก่อน

      Oh that sounds like a load of fun. 4096 cores is mind boggling.

  • @jaysistar2711
    @jaysistar2711 10 หลายเดือนก่อน +6

    Literally released on a Tuesday...

  • @Holobrine
    @Holobrine 10 หลายเดือนก่อน

    11:51 Yeah, this is why I want enums to be able to have trait bounds on the types they can store and obviate the need to have match expressions on methods every variant is guaranteed to have

  • @IgneousGorilla
    @IgneousGorilla 5 หลายเดือนก่อน

    27:30 rayon (leaving this for my future self)

  • @ferdynandkiepski5026
    @ferdynandkiepski5026 10 หลายเดือนก่อน +1

    This seems to be running awfully slowly compared to other implementations. There is a video by The Cherno where he reviews an implementation of the code from the raytracing in a weekend book from a year ago that I have watched previously. I don't know how much of it you can apply to Rust, as it's obviously a C++ code review. Options in rust are close to zero cost from what I know so you can probably ignore that part. The most applicable part of the video would probably be removing the branching and making sure that everything is in contiguous memory. The video is called "I made it FASTER // Code Review". Another thing you might want to try is compiling with the "-C target-cpu=native" flag for SIMD optimizations from the rust compiler but you might already be doing that.

    • @chrisbiscardi
      @chrisbiscardi  10 หลายเดือนก่อน +3

      to be clear, as I said in the video, I definitely think of this as a slow implementation and by no means a production level raytracer.
      As far as I can tell, the Raytracing in a Weekend series doesn't track performance *at all* or mention it in any of the material. So stack that on top of having not written a raytracing renderer before, and not knowing where the series was headed before going through it and you get slow output. You can't optimize code when that optimization may remove functionality you have to depend on in a future section.
      The Cherno spends that whole video painfully stating "Why is this so slow?" but the answer is that the series doesn't consider performance at all, seemingly intentionally. Its a learning tool that focuses on raytracing concepts, not performance. No hate to them, but for me the video didn't help at all. Its seemed to boil down to: "what if we didn't follow Raytracing in One Weekend and did something else instead", combined with "This is a bug in the C compiler"... which unfortunately isn't useful for me because I'm not writing C.
      Its worth noting in particular that the runtime vastly differs between my primary working computer (an m1 pro max) and my PC (7950x) which isn't featured in the video. A 24s workload on my mac runs in less than 10 seconds on my PC. I am *guessing* that this is due to simd, but I haven't gone further than running the program to see what happens and noting that there's an open PR for aarch64/neon in the underlying vec3 implementation I used -- github.com/bitshifter/glam-rs/pull/379
      I also tried your suggestion to use target-cpu=native, but it doesn't make a difference on either platform. Worth noting here that glam indicates that "SSE2 is enabled by default on x86_64 targets." -- github.com/bitshifter/glam-rs#enabling-simd
      So anyway, to your point: the runtime of my "final scene" (which is what's being optimized in that video), running on a comparable platform, is within 20 seconds of the optimized C++ version. Is my version slower? absolutely. Is it "awfully slowly compared to other implementations"? Only if you don't run it on comparable platforms.

  • @earx23
    @earx23 7 หลายเดือนก่อน

    This is impressive stuff. I once did a toy pathtracer in C++, then ported it to Digital Mars D. It was 10 times as slow. The culprit was dynamic allocation everywhere, and not knowing when that actually takes place. I'm sure your Rust tracer is just as fast as the C++ one?

  • @animanaut
    @animanaut 10 หลายเดือนก่อน

    i just bookmarked two books from Jamis Buck for a potential Rust driven run. One of them coincidently called 'The Ray Tracer Challenge' and the other one is called 'Mazes for Programmers'.

    • @chrisbiscardi
      @chrisbiscardi  10 หลายเดือนก่อน +1

      I just read through the ray tracer challenge this week. I liked that it was presented in a mostly language-independent way with pseudo code.

  • @immadmir
    @immadmir 10 หลายเดือนก่อน

    I was planning to do the same thing and this video popped...

  • @andreylizin9038
    @andreylizin9038 10 หลายเดือนก่อน

    Hi, what do you think about coding in web3?

  • @DBZM1k3
    @DBZM1k3 10 หลายเดือนก่อน

    Did you have any issue with the blue gradient part when implementing the rays? I decided to try this myself and I'm currently baffled as to why the ppm image is incorrect. I even downloaded the raw file of your implementation from your history to check to see if I wasn't crazy and it seems that your code produced the same issue.
    Edit: Nevermind! I worked it out. It was because there was no casting to u32 for the colours.

    • @chrisbiscardi
      @chrisbiscardi  10 หลายเดือนก่อน

      I'm not sure what issue you ran into (hard to tell from this description) but I'm happy you figured it out.
      I don't remember needing to cast the colors to a u32. Each component (r,g,b) should end up as a u8 (a number from 0-255) and there is some clamping later on in the series to guarantee this.
      The output I produced is here, which doesn't seem like it has any issues compared to the series' example output. github.com/rust-adventure/raytracing-in-one-weekend/blob/a565404b86143615106a618517b3dccfa109cfcc/outputs/blue-gradient.png

    • @DBZM1k3
      @DBZM1k3 10 หลายเดือนก่อน

      @chrisbiscardi yeah, it does seem weird. I pulled your code for the blue gradient to check, but for some reason, I got the same output. I'm not sure if it's a Rust version difference. I think I'm on Nightly.

  • @McRinato90
    @McRinato90 10 หลายเดือนก่อน

    Rust is also used by developers on NEAR, can you share your opinion on this?

  • @Endelin
    @Endelin 10 หลายเดือนก่อน +1

    Are you working furiously on a Bevy tutorial for ex-Unity devs?

    • @chrisbiscardi
      @chrisbiscardi  10 หลายเดือนก่อน +2

      haha, I don't think I have enough Unity experience to make that. I have some Bevy game workshops and videos but nothing from a "unity perspective".

  • @kirillgimranov4943
    @kirillgimranov4943 10 หลายเดือนก่อน

    I tried it 2 years ago and I failed after one third of the course cause the code was in c++ and I don't know it. C++ code is also hard to read if you aren't familiar with it, but rust is

    • @chrisbiscardi
      @chrisbiscardi  10 หลายเดือนก่อน +1

      The C++ was definitely tough to translate at times, and there were a lot of "C++ isms" like instantiating empty structs or mutating arbitrary arguments that felt really awkward.
      I definitely think it would be interesting to have a rust-forward version of the series.

    • @0.Andi.0
      @0.Andi.0 10 หลายเดือนก่อน

      There is a rust version of the book, i followed the book and made a rudimentary RT but implemented a display of hows going I've uploaded it to github and used rayon so it runs a lot faster

  • @cylian8422
    @cylian8422 10 หลายเดือนก่อน +3

    I've been fideling with raytracing in wgpu for some time and boy do I need a tutorial on raytracing in compute shaders. The fragment shaders are too slow and wgpu documentation is garbage

    • @chrisbiscardi
      @chrisbiscardi  10 หลายเดือนก่อน +6

      I'm going to make a wgpu version of this, and I'm planning to use compute shaders. I don't have any timeline for that video though.

    • @Xld3beats
      @Xld3beats 10 หลายเดือนก่อน +1

      My advice is prepare to debug! Get Vulkan's SDK which includes glslangvalidator, and get RenderDoc to see the shader's runtime. Also, you should make a "debug buffer" if you're building something complex. Oh, and don't even touch WGSL, its a mess right now. Stick to GLSL, I've tried HLSL > SPIRV and it just won't take it for some reason.

  • @earx23
    @earx23 7 หลายเดือนก่อน

    ppm and pgm are the poor man's format, but so nice if you don't want to include complicated image format codecs.