clray

diff src/dbgray.cc @ 55:df239a52a091

extensive render stats for the CPU raytracer
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 11 Sep 2010 03:00:21 +0100
parents 6a30f27fa1e6
children
line diff
     1.1 --- a/src/dbgray.cc	Fri Sep 10 16:47:00 2010 +0100
     1.2 +++ b/src/dbgray.cc	Sat Sep 11 03:00:21 2010 +0100
     1.3 @@ -1,5 +1,6 @@
     1.4  #include <string.h>
     1.5  #include <assert.h>
     1.6 +#include <limits.h>
     1.7  #include "rt.h"
     1.8  #include "ogl.h"
     1.9  #include "vector.h"
    1.10 @@ -25,7 +26,10 @@
    1.11  static unsigned int tex;
    1.12  static Scene *scn;
    1.13  static const Ray *prim_rays;
    1.14 +static int max_iter;
    1.15  
    1.16 +static RenderStats *rstat;
    1.17 +static int cur_ray_aabb_tests, cur_ray_triangle_tests;
    1.18  
    1.19  bool init_dbg_renderer(int width, int height, Scene *scene, unsigned int texid)
    1.20  {
    1.21 @@ -40,6 +44,9 @@
    1.22  	ysz = height;
    1.23  	tex = texid;
    1.24  	scn = scene;
    1.25 +
    1.26 +	rstat = (RenderStats*)get_render_stats();
    1.27 +
    1.28  	return true;
    1.29  }
    1.30  
    1.31 @@ -58,7 +65,12 @@
    1.32  {
    1.33  	unsigned long t0 = get_msec();
    1.34  
    1.35 -	int iter = get_render_option_int(ROPT_ITER);
    1.36 +	max_iter = get_render_option_int(ROPT_ITER);
    1.37 +
    1.38 +	// initialize render-stats
    1.39 +	memset(rstat, 0, sizeof *rstat);
    1.40 +	rstat->min_aabb_tests = rstat->min_triangle_tests = INT_MAX;
    1.41 +	rstat->max_aabb_tests = rstat->max_triangle_tests = 0;
    1.42  
    1.43  	int offs = 0;
    1.44  	for(int i=0; i<ysz; i++) {
    1.45 @@ -66,17 +78,45 @@
    1.46  			Ray ray = prim_rays[offs];
    1.47  			transform_ray(&ray, xform, invtrans_xform);
    1.48  
    1.49 -			trace_ray(fb + offs * 3, ray, iter, 1.0);
    1.50 +			cur_ray_aabb_tests = cur_ray_triangle_tests = 0;
    1.51 +
    1.52 +			trace_ray(fb + offs * 3, ray, max_iter, 1.0);
    1.53  			offs++;
    1.54 +
    1.55 +			// update stats as needed
    1.56 +			if(cur_ray_aabb_tests < rstat->min_aabb_tests) {
    1.57 +				rstat->min_aabb_tests = cur_ray_aabb_tests;
    1.58 +			}
    1.59 +			if(cur_ray_aabb_tests > rstat->max_aabb_tests) {
    1.60 +				rstat->max_aabb_tests = cur_ray_aabb_tests;
    1.61 +			}
    1.62 +			if(cur_ray_triangle_tests < rstat->min_triangle_tests) {
    1.63 +				rstat->min_triangle_tests = cur_ray_triangle_tests;
    1.64 +			}
    1.65 +			if(cur_ray_triangle_tests > rstat->max_triangle_tests) {
    1.66 +				rstat->max_triangle_tests = cur_ray_triangle_tests;
    1.67 +			}
    1.68 +			rstat->prim_rays++;
    1.69 +			rstat->aabb_tests += cur_ray_aabb_tests;
    1.70 +			rstat->triangle_tests += cur_ray_triangle_tests;
    1.71  		}
    1.72  	}
    1.73  
    1.74 +	unsigned long t1 = get_msec();
    1.75 +
    1.76  	glPushAttrib(GL_TEXTURE_BIT);
    1.77  	glBindTexture(GL_TEXTURE_2D, tex);
    1.78  	glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, xsz, ysz, GL_RGB, GL_FLOAT, fb);
    1.79  	glPopAttrib();
    1.80 +	glFinish();
    1.81  
    1.82 -	printf("rendered in %lu msec\n", get_msec() - t0);
    1.83 +	rstat->render_time = t1 - t0;
    1.84 +	rstat->tex_update_time = get_msec() - t1;
    1.85 +
    1.86 +	rstat->rays_cast = rstat->prim_rays + rstat->refl_rays + rstat->shadow_rays;
    1.87 +	rstat->rays_per_sec = 1000 * rstat->rays_cast / rstat->render_time;
    1.88 +	rstat->avg_aabb_tests = (float)rstat->aabb_tests / (float)rstat->rays_cast;
    1.89 +	rstat->avg_triangle_tests = (float)rstat->triangle_tests / (float)rstat->rays_cast;
    1.90  }
    1.91  
    1.92  static void trace_ray(float *pixel, const Ray &ray, int iter, float energy)
    1.93 @@ -120,6 +160,7 @@
    1.94  		shadowray.dir[2] = ldir.z;
    1.95  
    1.96  		if(!cast_shadows || !find_intersection(shadowray, scn, scn->kdtree, 0)) {
    1.97 +			rstat->brdf_evals++;
    1.98  
    1.99  			ldir.normalize();
   1.100  
   1.101 @@ -140,6 +181,10 @@
   1.102  			scol[1] += mat->ks[1] * spec;
   1.103  			scol[2] += mat->ks[2] * spec;
   1.104  		}
   1.105 +
   1.106 +		if(cast_shadows) {
   1.107 +			rstat->shadow_rays++;
   1.108 +		}
   1.109  	}
   1.110  
   1.111  	float refl_color[3];
   1.112 @@ -164,6 +209,8 @@
   1.113  		scol[0] += rcol[0] * mat->ks[0] * mat->kr;
   1.114  		scol[1] += rcol[1] * mat->ks[1] * mat->kr;
   1.115  		scol[2] += rcol[2] * mat->ks[2] * mat->kr;
   1.116 +
   1.117 +		rstat->refl_rays++;
   1.118  	}
   1.119  
   1.120  	pixel[0] = dcol[0] + scol[0];
   1.121 @@ -210,6 +257,8 @@
   1.122  
   1.123  static bool ray_aabb_test(const Ray &ray, const AABBox &aabb)
   1.124  {
   1.125 +	cur_ray_aabb_tests++;
   1.126 +
   1.127  	if(ray.origin[0] >= aabb.min[0] && ray.origin[1] >= aabb.min[1] && ray.origin[2] >= aabb.min[2] &&
   1.128  			ray.origin[0] < aabb.max[0] && ray.origin[1] < aabb.max[1] && ray.origin[2] < aabb.max[2]) {
   1.129  		return true;
   1.130 @@ -252,6 +301,8 @@
   1.131  
   1.132  static bool ray_triangle_test(const Ray &ray, const Face *face, SurfPoint *sp)
   1.133  {
   1.134 +	cur_ray_triangle_tests++;
   1.135 +
   1.136  	Vector3 origin = ray.origin;
   1.137  	Vector3 dir = ray.dir;
   1.138  	Vector3 norm = face->normal;