Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
Hello,
I am trying to develop a ray tracer for personal interest (or you may say to sharpen kitchen knives). So, as it may be obvious I am not happy with performance :)
The following lines consume about %52 of execution time. The time shown is a little more that actual data due to profiling but percentages are almost same every run.
C#
// Hit Count          : 6664569
// Time               : 2,57
// Time with Children : 4,18
{
/* %27.62*/vector dist = s.pos - viewRay.start;
/* %22.86*/float B = viewRay.dir * dist;
/* %24.34*/float D = B * B - dist * dist + s.size * s.size;
/* %6.61 */if (D < 0.0f)
/* %2.84 */   return false;
//Some more lines

I've overloaded multiplication substraction etc for vector math, I am not sure what can I do for any more speed but there must be something. I've tried converting vector class to struct but not much I've gained. This is just a 3 sphere scene and single thread I cant imagine what would happen if I had few thousands of triangles in scene. Is there anything else to run this faster? Any suggestion is welcome.
Thanks.
Posted

It's very difficult to suggest performance improvement in a tiny fraction of code: it may be that you need to re-think how the supporting structure of your code is organised.

For example, how often does viewRay change? if it doesn't change much, that it could be worth moving the viewRay.Start into it's own variable.
Similarly, is there a way you can move to integer arithmetic by using using values one hundred time larger throughout?

Frequently, you will find that changing the algorithm will get you bigger benefits than hand tuning lines of code - and don't forget that this will all change if you stop running debug mode, and run release: better optimizations are available so your hand tuning may make things worse!
 
Share this answer
 
Comments
yesotaso 21-May-11 10:23am    
Actually viewray passed here by ref I thought "if I dont change it why copy it around?". Is tarted to decompose composite operator overloads into primitive type operations. Readability, maintainability dropped drastically though I got ~%15 increase in speed min-maxing... 1 thing keeps waving hands from sidelines unsafe :) Thanks for answer btw.
OriginalGriff 21-May-11 10:29am    
Unsafe may help, may not: it depends on how your data is organised. It may be possible to re-organise into a form where direct pointers and so forth can help, but that will again impact readability. As will any remove-the-method-call changes, which can improve things because there are fewer memory operations going on. You also may find it possible to use more threads (and hopefully more cores) but again, that will depend on your overall algorithm and the additional overhead of starting threads may make that useless!
yesotaso 21-May-11 10:37am    
I did not pre-calculate ray as I use "orthographic projection" whole tracing returns 3 RGB value and I write it to bitmap via for each ray
unsafe
{
byte* pData = (byte*)dat.Scan0;
int x3 = x * 3;
int y3 = y * dat.Stride;
*(pData + y3 + x3) = (byte)((output.red >= 1 ? 1 : output.red) * 255);
*(pData + y3 + x3 + 1) = (byte)((output.green >= 1 ? 1 : output.green) * 255);
*(pData + y3 + x3 + 2) = (byte)((output.blue >= 1 ? 1 : output.blue) * 255);
}
and return bitmap in the end.
Edit: I plan on multithreading for each core available but thats a lil further now.
Sergey Alexandrovich Kryukov 22-May-11 0:40am    
Agree with you, Griff (my 5).
Will you look at my answer via reductio ad absurdum? :-)
--SA
Prasanta_Prince 24-May-11 9:13am    
Good One 5 from me.
This is wrong approach to addressing the performance issue: showing this part of code is not helping; probably this is not you can optimize. To understand my idea imaging you do geometric calculation and profile your code somehow; the profile tells you that 98% of your CPU time is spent by two functions: sin and cos.

It does not mean that you should get a sine and cosine functions with better performance (clearly, it would hardly be possible). It means you should look outside those function and pay attention to the caller: why these trigonometric functions are called so many times?

Same thing with your code: chances are, you problem lies outside of this fragment.

—SA
 
Share this answer
 
Comments
yesotaso 22-May-11 9:26am    
Indeed what you wrote is correct. On the other hand, there is no triginometric calculations in my code atm. If I am to imrove picture quality with different algorithm thats another story but for now all I do is brute-force vector calculus. I havent even used the accelerator struct I implemented (an OCTree operating on AABB) in case of many object (I found a "USS Fletcher" model with 380k+ triangles thats my target to render) thats why I am trying to figure basic performance tricks. Thanks.
Sergey Alexandrovich Kryukov 22-May-11 12:41pm    
As you understand, it was just to explain the idea. Hard to say anything more certain based on your information.
--SA
yesotaso 22-May-11 16:34pm    
Yeah sorry about vague stuff, anyway thanks for your time.
Sergey Alexandrovich Kryukov 22-May-11 17:36pm    
No, I'm sorry I cannot help more. I guess now it's you turn to research stuff. When and if you have some further question else to ask, I would be happy to be helpful.
--SA
Espen Harlinn 22-May-11 11:13am    
Good point, my 5
This may be of interest:
CUDA ZONE[^]

They are working on real-time raytracers using this kind of technology ...

Best regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-May-11 12:48pm    
Good idea, my 5.
--SA
Espen Harlinn 22-May-11 14:28pm    
Thanks SAKryukov - there is also c++ - Visual C++ enables /GL (Whole Program Optimization) that allows the compiler to perform optimizations with information on all modules in the program
Prasanta_Prince 24-May-11 9:12am    
Good One.
Espen Harlinn 24-May-11 13:53pm    
Thank you, Prasanta_Prince

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900