13 ms·
Ray Tracing in pure CMake
- chubot 6y agoThis is cool, someone should port this to bash, which also only supports integers. Then I can use it as a benchmark for https://www.oilshell.org https://www.oilshell.org :) Also bash can do the multicore part very easily with processes, like cmake.
- gumby 6y ago> This is cool, someone should port this to bash, which also only supports integers. With a little bitfield support (you could always call out to dc but you can do boolean arithmetic directly in bash) you can simply implement IEEE 754 in bash.
- chubot 6y agoYeah bash has 64 bit signed integers and bitwise ops on them. I'd be interested in seeing a floating point implementation. I think it would be pretty much identical to an implementation in C with signed long. echo $(( 2**63 | 1 )) -9223372036854775807
- nrdvana 6y agoFeel free to use my bash 3d geometry library https://github.com/nrdvana/CmdlineGL/blob/master/share/lib-bash/Geom.lib https://github.com/nrdvana/CmdlineGL/blob/master/share/lib-b...
- lasagnaphil 6y agoOh no... so far the most terrifying title I've seen in HN this year. By the way, it's impressive how they even managed to multithread this.
- usrusr 6y agoSomeone might be working on an article even worse: list of things that raytracers haven't been written in (yet, regularly updated)
- usrusr 6y ago(PS: would that eventually count as "raytracers, written in provocative blog post meme"?)
- amelius 6y agoOr how about: an LLVM backend for CMake.
- 0-_-0 6y agoThere are even Excel ray tracers out there
- Sharlin 6y agoIn fact that’s not even difficult, given how both a raytracer and a spreadsheet are basically functions that assign a value to every cell of a grid.
- dev_tty01 6y agoNext thing you know someone will suggest HN with hierarchy for repeated and unrelenting topics. Linux ports on home appliances, ray tracing, vi vs. emacs, launchd vs systemd, walled gardens, .... Oh no, say it isn't so!
- xyzzy_plugh 6y agoI refuse to even look at this. I've seen ray tracing in a PDF before but this is too far.
- tgv 6y agoLet me tell you one detail: since cmake doesn't have floats, they use fixed point arithmetic. LOOK AT IT. You know you want too! function(sqrt x res) div_by_2(${x} guess) foreach(counter RANGE 4) if(${guess} EQUAL 0) set("${res}" 0 PARENT_SCOPE) return() endif() div(${x} ${guess} tmp) add(${tmp} ${guess} tmp) div_by_2(${tmp} guess) endforeach() set("${res}" "${guess}" PARENT_SCOPE) endfunction()
- Someone 6y agoIMO, this isn’t that impressive. Turns out that CMake has a scripting language that can be used to implement a ray tracer, and do that multi-threaded. The model being rendered also is hard-coded in the ray tracer. I expected a monstrosity that had multiple targets for every pixel (“ray arriving at (x,y)”, “ray arriving at (x,y) after one bounce”, etc), with some magic to merge all the outputs of the last bounce into an image, say by generating a huge html table, with each cell having a different background color.
- delta64 6y ago> The model being rendered also is hard-coded in the ray tracer. This is true - but there's no reason why it couldn't be modified to read in a scene description. It's more a proof of concept than anything else.
- physicsguy 6y agoSimultaneously love and hate this
- tuankiet65 6y agoHere's a performance figure on my i5-3210m (2 core, 4 threads) when using 4 processes: * 64x64: 14.561s * 256x256: 226.28s, 15x compared to 64x64.
- pizza234 6y ago> * 256x256: 226.28s, 15x compared to 64x64. Basic ray tracers compute one ray for each pixel (without reflection/refraction), so that this increase in time is expected, as it's roughly 256²/64²=4². This is also due to the fact that each ray is independently computed. I don't know if sophisticated ones can save some (rays) computation by interpolating.
- Blikkentrekker 6y agoI must say that completing this article leaves me with a feeling not too dissimilar from that mixture of awe and disgust when I finished reading Uwa-Koi.
- dgellow 6y agoWow, the implementation doesn't even look that horrible! Fascinating, crazy project.
- bawana 6y agoPlease clarify for me the part about the set exec command. You create n workers but you pass the whole image size to each. Why isn’t the image size divided by the number of workers?
- Iwan-Zotow 6y agovery good porno
- knorker 6y agoWell well well, we finally found something CMake can actually do. Please, everyone, stop using CMake. It doesn't do its one job.
- skohan 6y agoWhat's wrong with CMake?
- bluGill 6y agoThe syntax is weird and inconsistent. Built ins are case insensitive, but user defined functions are sensitive. No way to return values from functions, except by jumping to the scope of the caller and changing its variables unexpectedly. (Better hope you know what the caller does) That isn't a complete list . Despite the above cmake is your best choice of build system for C. Though some competitors could overtake it.
- andi999 6y agoOn top of that I found the documentation and the book terrible. I thought it might be easier to understand what it does by reading the source code (didn't try yet)
- ahartmetz 6y agoThe documentation is "complete" in the way that some manpages are - the biggest problem with it is the lack of best practices guidance. About books, I believe that good ones exist now. The official CMake book is not among the good ones - last time I checked, it was ridiculously outdated (and IMO not even very good at the time it was written). I have extracted some knowledge from the source code. It's more underdesigned than overdesigned and I think that's the better side to err on. Kinda awkward but you can figure it out.
- andi999 6y agoWhich cmake book is good?
- unnouinceput 6y agoI remember writing a ray tracing in 1994 in Turbo Pascal 7.0 running on 286 under DOS 4.0 at the time. I did it with floating numbers, pure mathematical implementation. When it came to usability was something like 1 frame every 30 seconds for resolution of 640x480 with 256 colors only. The problem with ray tracing isn't writing one, the problem is its usability, to get the most realistic AND fast enough. Mine was very realistic for that resolution but the snail FPS was something unbearable. Despite my best efforts at the time, hardware was not ready. I remember the talk between us (prof. assistant, basically a master student and rest of wanna be programmers) and we're so sure it would take maximum 5 years for ray tracing and photo-realism to become every day commodity. We were wrong only by 20 years. Good times.
- pixelpoet 6y ago640x480 256 colours on a 286? With only 30 seconds for an image, even though it doesn't have a floating point unit? Sorry for being a doubting Thomas, but that seems very implausible (having spent my life writing ray tracers).
- dahart 6y ago> we're so sure it would take maximum 5 years for ray tracing and photo-realism to become every day commodity. We were wrong only by 20 years. Good times. Yeah that was a very long running joke about ray tracing: “real-time ray tracing is only five years away... and always will be”. ;) > The problem with ray tracing isn’t writing one, the problem is its usability, to get the most realistic AND fast enough. Times sure have changed eh? 640x480/30s is ~ 10k rays/sec. Now we have ray tracing GPUs that can do well over 10B rays/sec with full floating point. More than a million times faster, not even counting precision & scene size, crazy right?! I’m working in the field and still blown away by what people are doing in real time in the last few years, fully path traced scenes with global illumination that are hard to distinguish from photographs...
- turing_complete 6y agoThey were so preoccupied with whether or not they could, they didn't stop to think if they should.
- klyrs 6y agoFunny comment from somebody named turing_complete, innit? The fact that they "shouldn't" is what makes this abomination fun.
- fouronnes3 6y agoCMake is by far the worst language I have ever worked in.
- TheRealSteel 6y agoOh thank god, this thread is making me feel much better about myself. I've been trying to integrate OpenCV's C++ library with my own C++ code (not involving Python), with the additional contrib modules (meaning I have to compile OpenCV myself), for multiple platforms, and I got stuck on CMake for so long it made me question if I was just a total idiot and should give up on programming forever.
- jcelerier 6y agoto give another data point: I was given a project a couple weeks ago that was using visual studio solutions and opencv + contrib modules - I fighted 3 hours with making the .vcxprojs work on my machine before porting the whole shit to cmake in 15 minutes. Using OpenCV was just a matter of find_package(OpenCV 3.3.1 REQUIRED) target_include_directories(theApp PUBLIC ${OpenCV_INCLUDE_DIRS}) target_link_libraries(theApp PUBLIC ${OpenCV_LIBS}) and everything worked, so I'm curious about what failed for you
- jpm48 6y agoplus 1 for this, I have been using CMake with my students for a year (moving from qmake) and this in conjunction with vcpkg to install stuff has been a breeze. This book is great too https://crascit.com/professional-cmake/ https://crascit.com/professional-cmake/
- TheRealSteel 6y agoThanks for your reply. Will this include the contrib modules even for Android? And iOS? Once I sat down and read the Cmake basics, yes, it was easy enough to get OpenCV working with basicallly the code you posted, except I couldn't find a way to include the contrib modules in an Android version, it would always complain Aruco wasn't there. Until I went and compiled OpenCV myself, and that was the part that was a massive pain in my ass - CMake failing to include architecture information in the library files unless you specify it on the command line before running Cmake GUI, even tho it is an option in the GUI. Tho perhaps that's more of a failing of the OpenCV build script than Cmake itself? I did just recompile OpenCV myself, so I did get there in the end and it's all working now, but I'm sure I'll be tweaking all parts of this project for months to come so if there's an easier/proper way, I'm happy to learn. It also needs to be statically linked. Note: I'm not a real programmer, I don't know enough to have even been able to get an entry level job in the industry yet, I'm an idiot that doesn't belong on this website. I'm just trying to learn as I go.
- savant_penguin 6y agoThis is an ode to how much you can do with the four arithmetic operations and not much more
- iab 6y agoEvery day we stray further from god’s light
- hzhou321 6y agoI think this is a demonstration why CMake is bad. Unless it's a general purpose programming language, being Turing complete should always be listed in the "Con" side. It literally means the effort needed to understand its complexity can be unbounded.
- kortex 6y agoCmake isn't the problem, it's a solution to the complexity of building huge c and/or c++ projects such as VTK such that you can run on many different OSes, arches and platforms, with hundreds of different build options. That is just what you get with legacy build systems. Disclosure: I work at Kitware.
- vlovich123 6y agoI think Skylark (the language for Buck/Basel) represents an interesting compromise here that I like better, even though in the past I was a huge Cmake advocate. The majority of rules are declarative and easily understood. Python (or at least a dialect extremely close to Python) is how you extend and write new rules. The two live in very different files (declarative rules are in BUCK/BUILD files, .bzl files contain the imperative definitions of rules, and various other conventions control how you deal with platforms (ie select rules).
- gumby 6y agoI think of CMake as a coin with two faces: on one side is a cool engine that manages dependencies and can somehow wrangle Make/xcodebuild/ninja et al. On the other side is a dreadful interface (presumably due to legacy constraints) that you have to gingerly manipulate in the hope that it will cause the proper state changes on the other side so that it can generate the result you want...when I don't actually know what the internals of say Xcodebuild are. It's sort of like using Italian to correspond with someone when you know Latin but not Italian. If the code base really looks like that it might be interesting to implement a different front end. Even XML would be better. On the other hand I couldn't imagine working without it and would be happy if it supported more languages (I only use it with C and C++).
- 6y ago
- tdiff 6y agoSo far the funniest bug with cmake I've fixed was when we were accidentally setting a variable with name '0' to a value '1'. Later on, in other module, we were checking if some other variable was equal to '0' which was interpreted by cmake as a variable reference so the actual check was with '1'.
- nrdvana 6y agoHigh-five from the opengl-bash guy! http://www.nrdvana.net/cmdlinegl/ http://www.nrdvana.net/cmdlinegl/
- delta64 6y agoThanks! This is extremely impressive, and looks like far more effort haha
- nrdvana 6y agoThe OpenGL interpreter was a lot of effort, but the bash scripts that feed it were basically the same problem you worked out: how to implement 3d vector math in a language of integers, array variables, and functions without references or return values.
- nrdvana 6y agoBtw, one of the things I came up with in bash was an “object oriented” pattern (antipattern?) where a “constructor” function would declare variables for “attributes” and functions for “methods”, like for example declaring an object named Vector1 whose fields are Vector1_x Vector1_y Vector1_z and methods like Vector1_Normalize and Vector1_SetMagnitude. The functions would be eval’d for each instance of the object during the constructor to reference the attributes and methods of that named object instance. Is this possible in cmake? (I don’t know cmake at all)
- delta64 6y agoIt sounds like it should be possible (CMake also has eval), but the lack of OOP isn't so much a problem for what I needed. Packing and unpacking the vectors was more annoying. I can live without OOP, I've written enough C to get used to it haha.
- nrdvana 6y agoActually, thinking back, the primary reason I did that was that bash can’t dereference a variable by its name stored in another variable without an eval, and calling eval on each access of the variable was slow. By eval-ing the whole method ahead of time, I could access all the object attributes at runtime without any further evals. It looks like cmake has a getter/setter function that takes a name, so that wouldn’t be a problem here.
- s_gourichon 6y ago> Conclusion > If you made it this far, thanks for reading! Feel free to create issues, send pull requests or star the code on GitHub. Done, fixed 3 bugs there https://github.com/64/cmake-raytracer/pull/4 https://github.com/64/cmake-raytracer/pull/4 https://github.com/64/cmake-raytracer/pull/5 https://github.com/64/cmake-raytracer/pull/5 https://github.com/64/cmake-raytracer/pull/6 https://github.com/64/cmake-raytracer/pull/6 . Do I deserve a medal? A punishment for feeding an abomination?
- delta64 6y agoThanks so much! I'll take a look at these when I get a chance
- s_gourichon 6y agoThanks for merging all my PRs. :-)
- perfobotto 6y agoCan it run crysis?