clray

changeset 14:29f9330cfa4b

trying to debug the bloody thing
author John Tsiombikas
date Sat, 07 Aug 2010 03:36:36 +0100
parents 407935b73af3
children 754faf15ba36
files clray.vcproj src/mesh.cc src/mesh.h src/ocl.cc src/ocl.h src/ogl.h src/rt.cc
diffstat 7 files changed, 114 insertions(+), 20 deletions(-) [+]
line diff
     1.1 --- a/clray.vcproj	Wed Aug 04 04:51:06 2010 +0100
     1.2 +++ b/clray.vcproj	Sat Aug 07 03:36:36 2010 +0100
     1.3 @@ -41,7 +41,7 @@
     1.4  			<Tool
     1.5  				Name="VCCLCompilerTool"
     1.6  				Optimization="0"
     1.7 -				PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE;_USE_MATH_DEFINES"
     1.8 +				PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE;_USE_MATH_DEFINES;_CRT_SECURE_NO_WARNINGS"
     1.9  				MinimalRebuild="true"
    1.10  				BasicRuntimeChecks="3"
    1.11  				RuntimeLibrary="3"
    1.12 @@ -115,7 +115,7 @@
    1.13  				Name="VCCLCompilerTool"
    1.14  				Optimization="2"
    1.15  				EnableIntrinsicFunctions="true"
    1.16 -				PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE;_USE_MATH_DEFINES"
    1.17 +				PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE;_USE_MATH_DEFINES;_CRT_SECURE_NO_WARNINGS"
    1.18  				RuntimeLibrary="2"
    1.19  				EnableFunctionLevelLinking="true"
    1.20  				UsePrecompiledHeader="0"
    1.21 @@ -175,6 +175,14 @@
    1.22  				>
    1.23  			</File>
    1.24  			<File
    1.25 +				RelativePath=".\src\matrix.cc"
    1.26 +				>
    1.27 +			</File>
    1.28 +			<File
    1.29 +				RelativePath=".\src\matrix.h"
    1.30 +				>
    1.31 +			</File>
    1.32 +			<File
    1.33  				RelativePath=".\src\mesh.cc"
    1.34  				>
    1.35  			</File>
    1.36 @@ -195,6 +203,10 @@
    1.37  				>
    1.38  			</File>
    1.39  			<File
    1.40 +				RelativePath=".\src\ogl.h"
    1.41 +				>
    1.42 +			</File>
    1.43 +			<File
    1.44  				RelativePath=".\src\rt.cc"
    1.45  				>
    1.46  			</File>
     2.1 --- a/src/mesh.cc	Wed Aug 04 04:51:06 2010 +0100
     2.2 +++ b/src/mesh.cc	Sat Aug 07 03:36:36 2010 +0100
     2.3 @@ -125,6 +125,11 @@
     2.4  	return true;
     2.5  }
     2.6  
     2.7 +int Scene::get_num_meshes() const
     2.8 +{
     2.9 +	return (int)meshes.size();
    2.10 +}
    2.11 +
    2.12  int Scene::get_num_faces() const
    2.13  {
    2.14  	int num_faces = 0;
    2.15 @@ -135,6 +140,27 @@
    2.16  	return num_faces;
    2.17  }
    2.18  
    2.19 +int Scene::get_num_materials() const
    2.20 +{
    2.21 +	return (int)matlib.size();
    2.22 +}
    2.23 +
    2.24 +Material *Scene::get_materials()
    2.25 +{
    2.26 +	if(matlib.empty()) {
    2.27 +		return 0;
    2.28 +	}
    2.29 +	return &matlib[0];
    2.30 +}
    2.31 +
    2.32 +const Material *Scene::get_materials() const
    2.33 +{
    2.34 +	if(matlib.empty()) {
    2.35 +		return 0;
    2.36 +	}
    2.37 +	return &matlib[0];
    2.38 +}
    2.39 +
    2.40  
    2.41  #define INVALID_IDX		INT_MIN
    2.42  
    2.43 @@ -638,7 +664,9 @@
    2.44  		char *pathname = (char*)alloca(res_len + fnamelen + 2);
    2.45  		memcpy(pathname, beg, res_len);
    2.46  		pathname[res_len] = 0;
    2.47 -		strcat(pathname, "/");
    2.48 +		if(res_len) {
    2.49 +			strcat(pathname, "/");
    2.50 +		}
    2.51  		strcat(pathname, fname);
    2.52  
    2.53  		if((fp = fopen(pathname, mode))) {
    2.54 @@ -648,6 +676,7 @@
    2.55  		}
    2.56  
    2.57  		beg += res_len;
    2.58 +		if(*beg == ':') beg++;
    2.59  	}
    2.60  	return false;
    2.61  }
    2.62 @@ -662,7 +691,11 @@
    2.63  		strncpy(buf, str, PATH_MAX);
    2.64  		char *ptr = strrchr(buf, '/');
    2.65  
    2.66 -		if(ptr && *ptr) *ptr = 0;
    2.67 +		if(ptr && *ptr) {
    2.68 +			*ptr = 0;
    2.69 +		} else {
    2.70 +			strcpy(buf, ".");
    2.71 +		}
    2.72  	}
    2.73  	return buf;
    2.74  }
     3.1 --- a/src/mesh.h	Wed Aug 04 04:51:06 2010 +0100
     3.2 +++ b/src/mesh.h	Sat Aug 07 03:36:36 2010 +0100
     3.3 @@ -35,8 +35,12 @@
     3.4  	std::vector<Material> matlib;
     3.5  
     3.6  	bool add_mesh(Mesh *m);
     3.7 +	int get_num_meshes() const;
     3.8 +	int get_num_materials() const;
     3.9 +	int get_num_faces() const;
    3.10  
    3.11 -	int get_num_faces() const;
    3.12 +	Material *get_materials();
    3.13 +	const Material *get_materials() const;
    3.14  
    3.15  	bool load(const char *fname);
    3.16  	bool load(FILE *fp);
     4.1 --- a/src/ocl.cc	Wed Aug 04 04:51:06 2010 +0100
     4.2 +++ b/src/ocl.cc	Sat Aug 07 03:36:36 2010 +0100
     4.3 @@ -156,6 +156,12 @@
     4.4  }
     4.5  
     4.6  
     4.7 +CLArg::CLArg()
     4.8 +{
     4.9 +	memset(this, 0, sizeof *this);
    4.10 +}
    4.11 +
    4.12 +
    4.13  CLProgram::CLProgram(const char *kname)
    4.14  {
    4.15  	prog = 0;
    4.16 @@ -242,7 +248,8 @@
    4.17  	printf("create argument %d buffer: %d bytes\n", idx, (int)sz);
    4.18  	CLMemBuffer *buf;
    4.19  
    4.20 -	if(!(buf = create_mem_buffer(rdwr, sz, ptr))) {
    4.21 +	if(sz <= 0 || !(buf = create_mem_buffer(rdwr, sz, ptr))) {
    4.22 +		fprintf(stderr, "invalid size while creating argument buffer %d: %d\n", idx, (int)sz);
    4.23  		return false;
    4.24  	}
    4.25  
    4.26 @@ -262,6 +269,17 @@
    4.27  	return args[arg].v.mbuf;
    4.28  }
    4.29  
    4.30 +int CLProgram::get_num_args() const
    4.31 +{
    4.32 +	int num_args = 0;
    4.33 +	for(size_t i=0; i<args.size(); i++) {
    4.34 +		if(args[i].type != ARGTYPE_NONE) {
    4.35 +			num_args++;
    4.36 +		}
    4.37 +	}
    4.38 +	return num_args;
    4.39 +}
    4.40 +
    4.41  bool CLProgram::build()
    4.42  {
    4.43  	int err;
     5.1 --- a/src/ocl.h	Wed Aug 04 04:51:06 2010 +0100
     5.2 +++ b/src/ocl.h	Sat Aug 07 03:36:36 2010 +0100
     5.3 @@ -53,6 +53,8 @@
     5.4  		cl_float4 vval;
     5.5  		CLMemBuffer *mbuf;
     5.6  	} v;
     5.7 +
     5.8 +	CLArg();
     5.9  };
    5.10  
    5.11  
    5.12 @@ -74,6 +76,7 @@
    5.13  	bool set_argf(int arg, float val);
    5.14  	bool set_arg_buffer(int arg, int rdwr, size_t sz, void *buf = 0);
    5.15  	CLMemBuffer *get_arg_buffer(int arg);
    5.16 +	int get_num_args() const;
    5.17  
    5.18  	bool build();
    5.19  
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/src/ogl.h	Sat Aug 07 03:36:36 2010 +0100
     6.3 @@ -0,0 +1,19 @@
     6.4 +#ifndef OGL_H_
     6.5 +#define OGL_H_
     6.6 +
     6.7 +#ifndef __APPLE__
     6.8 +
     6.9 +#if defined(WIN32) || defined(__WIN32__)
    6.10 +#include <windows.h>
    6.11 +#endif
    6.12 +
    6.13 +#include <GL/gl.h>
    6.14 +#include <GL/glu.h>
    6.15 +
    6.16 +#else	/* __APPLE__ */
    6.17 +#include <OpenGL/gl.h>
    6.18 +#include <OpenGL/glu.h>
    6.19 +#endif
    6.20 +
    6.21 +
    6.22 +#endif	/* OGL_H_ */
     7.1 --- a/src/rt.cc	Wed Aug 04 04:51:06 2010 +0100
     7.2 +++ b/src/rt.cc	Sat Aug 07 03:36:36 2010 +0100
     7.3 @@ -2,15 +2,7 @@
     7.4  #include <string.h>
     7.5  #include <math.h>
     7.6  #include <assert.h>
     7.7 -
     7.8 -#ifndef __APPLE__
     7.9 -#include <GL/gl.h>
    7.10 -#include <GL/glu.h>
    7.11 -#else
    7.12 -#include <OpenGL/gl.h>
    7.13 -#include <OpenGL/glu.h>
    7.14 -#endif
    7.15 -
    7.16 +#include "ogl.h"
    7.17  #include "ocl.h"
    7.18  #include "mesh.h"
    7.19  
    7.20 @@ -23,7 +15,9 @@
    7.21  	KARG_LIGHTS,
    7.22  	KARG_PRIM_RAYS,
    7.23  	KARG_XFORM,
    7.24 -	KARG_INVTRANS_XFORM
    7.25 +	KARG_INVTRANS_XFORM,
    7.26 +
    7.27 +	NUM_KERNEL_ARGS
    7.28  };
    7.29  
    7.30  struct RendInfo {
    7.31 @@ -89,13 +83,17 @@
    7.32  	/* setup argument buffers */
    7.33  	prog->set_arg_buffer(KARG_FRAMEBUFFER, ARG_WR, xsz * ysz * 4 * sizeof(float));
    7.34  	prog->set_arg_buffer(KARG_RENDER_INFO, ARG_RD, sizeof rinf, &rinf);
    7.35 -	prog->set_arg_buffer(KARG_FACES, ARG_RD, rinf.num_faces, faces);
    7.36 -	prog->set_arg_buffer(KARG_MATLIB, ARG_RD, scn->matlib.size() * sizeof(Material), &scn->matlib[0]);
    7.37 +	prog->set_arg_buffer(KARG_FACES, ARG_RD, rinf.num_faces * sizeof(Face), faces);
    7.38 +	prog->set_arg_buffer(KARG_MATLIB, ARG_RD, scn->get_num_materials() * sizeof(Material), scn->get_materials());
    7.39  	prog->set_arg_buffer(KARG_LIGHTS, ARG_RD, sizeof lightlist, lightlist);
    7.40  	prog->set_arg_buffer(KARG_PRIM_RAYS, ARG_RD, xsz * ysz * sizeof *prim_rays, prim_rays);
    7.41  	prog->set_arg_buffer(KARG_XFORM, ARG_RD, 16 * sizeof(float));
    7.42  	prog->set_arg_buffer(KARG_INVTRANS_XFORM, ARG_RD, 16 * sizeof(float));
    7.43  
    7.44 +	if(prog->get_num_args() < NUM_KERNEL_ARGS) {
    7.45 +		return false;
    7.46 +	}
    7.47 +
    7.48  	delete [] prim_rays;
    7.49  
    7.50  	global_size = xsz * ysz;
    7.51 @@ -140,11 +138,18 @@
    7.52  	glLoadIdentity();
    7.53  	gluPerspective(45.0, (float)rinf.xsz / (float)rinf.ysz, 0.5, 1000.0);
    7.54  
    7.55 +	Material *materials = scn->get_materials();
    7.56 +
    7.57  	glBegin(GL_TRIANGLES);
    7.58  	int num_faces = scn->get_num_faces();
    7.59  	for(int i=0; i<num_faces; i++) {
    7.60 -		Material *mat = &scn->matlib[faces[i].matid];
    7.61 -		glColor3f(mat->kd[0], mat->kd[1], mat->kd[2]);
    7.62 +		Material *mat = materials ? materials + faces[i].matid : 0;
    7.63 +
    7.64 +		if(mat) {
    7.65 +			glColor3f(mat->kd[0], mat->kd[1], mat->kd[2]);
    7.66 +		} else {
    7.67 +			glColor3f(1, 1, 1);
    7.68 +		}
    7.69  
    7.70  		for(int j=0; j<3; j++) {
    7.71  			float *pos = faces[i].v[j].pos;