Revision: 44891
http://brlcad.svn.sourceforge.net/brlcad/?rev=44891&view=rev
Author: kunigami
Date: 2011-06-10 19:03:37 +0000 (Fri, 10 Jun 2011)
Log Message:
-----------
Discovered that OSLRender only leaks memory if called by sh_osl. I've copied
the osl raytracer into osl_rt in such a way that it calls OSLRender class.
Valgrind did not identified the memory leaks present in the rt run
Modified Paths:
--------------
brlcad/trunk/src/liboptical/CMakeLists.txt
brlcad/trunk/src/liboptical/osl-renderer.cpp
brlcad/trunk/src/liboptical/osl-renderer.h
brlcad/trunk/src/liboptical/render_svc.cpp
Added Paths:
-----------
brlcad/trunk/src/liboptical/osl_rt.cpp
Modified: brlcad/trunk/src/liboptical/CMakeLists.txt
===================================================================
--- brlcad/trunk/src/liboptical/CMakeLists.txt 2011-06-10 17:17:15 UTC (rev
44890)
+++ brlcad/trunk/src/liboptical/CMakeLists.txt 2011-06-10 19:03:37 UTC (rev
44891)
@@ -107,6 +107,13 @@
# Link liboptical with osl-renderer library
BRLCAD_ADDLIB(liboptical "${LIBOPTICAL_SOURCES}" "librt libbn libbu
osl-renderer")
+ ##########################################
+ # Renderer test
+ ##########################################
+
+ BRLCAD_ADDEXEC(osl_rt "osl_rt.cpp" "osl-renderer ${OSLRT_LIBS2}")
+
+
ELSE(BRLCAD-ENABLE_OSL)
BRLCAD_ADDLIB(liboptical "${LIBOPTICAL_SOURCES}" "librt libbn libbu")
Modified: brlcad/trunk/src/liboptical/osl-renderer.cpp
===================================================================
--- brlcad/trunk/src/liboptical/osl-renderer.cpp 2011-06-10 17:17:15 UTC
(rev 44890)
+++ brlcad/trunk/src/liboptical/osl-renderer.cpp 2011-06-10 19:03:37 UTC
(rev 44891)
@@ -6,7 +6,6 @@
InitShaders();
-
ssi = (ShadingSystemImpl *)shadingsys;
thread_info.handle = ssi->create_thread_info();
thread_info.ctx = ssi->get_context(thread_info.handle);
@@ -29,14 +28,18 @@
thread_info.Xi[1] = 0;
thread_info.Xi[2] = y*y*y;
- // execute shader
+ // execute shader
ShaderGlobals globals;
- ClosureColor *closure = ExecuteShaders(globals, info);
+ const ClosureColor *closure = ExecuteShaders(thread_info, globals, info,
+ shaderstate);
// sample primitive from closure tree
- Color3 weight;
- const ClosurePrimitive *prim = SamplePrimitive(weight, closure,
erand48(thread_info.Xi));
+ Color3 weight = Color3(0.0f);
+ //erand48(thread_info.Xi)
+ const ClosurePrimitive *prim = SamplePrimitive(weight, closure, 0.5);
+ //printf("Color: %.2lf %.2lf %.2lf\n", weight[0], weight[1], weight[2]);
+
if(prim) {
if(prim->category() == OSL::ClosurePrimitive::BSDF) {
// sample BSDF closure
@@ -48,9 +51,8 @@
bsdf->sample(globals.Ng, globals.I, zero, zero,
erand48(thread_info.Xi), erand48(thread_info.Xi),
omega_in, zero, zero, pdf, eval);
- printf("Weight: %.2lf %.2lf %.2lf -- pdf: %.2lf\n", weight[0],
weight[1], weight[2], pdf);
+ //printf("Weight: %.2lf %.2lf %.2lf -- pdf: %.2lf\n", weight[0],
weight[1], weight[2], pdf);
if (pdf <= 1e-4){
- printf("Error\n");
return Color3(0.0f);
}
return weight*eval/pdf;
@@ -92,7 +94,10 @@
shadingsys->clear_state();
}
-ClosureColor * OSLRenderer::ExecuteShaders(ShaderGlobals globals, RenderInfo
*info){
+const ClosureColor * OSLRenderer::
+ExecuteShaders(ThreadInfo &thread_info,
+ ShaderGlobals &globals, RenderInfo *info,
+ ShadingAttribStateRef shaderstate){
memset(&globals, 0, sizeof(globals));
@@ -120,7 +125,6 @@
thread_info.ctx->execute(ShadUseSurface, *shaderstate, globals);
-
return globals.Ci;
}
@@ -139,7 +143,9 @@
}
void OSLRenderer::SamplePrimitiveRecurse(const ClosurePrimitive*& r_prim,
Color3& r_weight, const ClosureColor *closure,
const Color3& weight, float& totw, float&
r){
+
if(closure->type == ClosureColor::COMPONENT) {
+
ClosureComponent *comp = (ClosureComponent*)closure;
ClosurePrimitive *prim = (ClosurePrimitive*)comp->data();
float p, w = fabsf(weight[0]) + fabsf(weight[1]) + fabsf(weight[2]);
@@ -155,13 +161,14 @@
r_weight = weight/w;
}
else {
+
p = w/totw;
if(r < p) {
// pick other primitive
r_prim = prim;
r_weight = weight/w;
-
+
r = r/p;
}
else {
Modified: brlcad/trunk/src/liboptical/osl-renderer.h
===================================================================
--- brlcad/trunk/src/liboptical/osl-renderer.h 2011-06-10 17:17:15 UTC (rev
44890)
+++ brlcad/trunk/src/liboptical/osl-renderer.h 2011-06-10 19:03:37 UTC (rev
44891)
@@ -56,7 +56,13 @@
FIXME: Add support for any osl shader */
void InitShaders();
/* Build the closure tree */
- ClosureColor *ExecuteShaders(ShaderGlobals globals, RenderInfo *info);
+ //static const ClosureColor *ExecuteShaders(ShaderGlobals &globals,
RenderInfo *info);
+
+ static const ClosureColor
+ *ExecuteShaders(ThreadInfo &thread_info,
+ ShaderGlobals &globals, RenderInfo *info,
+ ShadingAttribStateRef shaderstate);
+
/* Sample a primitive from the shaders group */
const ClosurePrimitive* SamplePrimitive(Color3& weight, const ClosureColor
*closure, float r);
/* Helper function for SamplePrimitive */
Added: brlcad/trunk/src/liboptical/osl_rt.cpp
===================================================================
--- brlcad/trunk/src/liboptical/osl_rt.cpp (rev 0)
+++ brlcad/trunk/src/liboptical/osl_rt.cpp 2011-06-10 19:03:37 UTC (rev
44891)
@@ -0,0 +1,216 @@
+#include "osl-renderer.h"
+
+using namespace OpenImageIO;
+
+static std::string outputfile = "image.png";
+static int w = 1024, h = 768;
+static int samples = 4;
+static unsigned short Xi[3];
+
+inline double clamp(double x) { return x<0 ? 0 : x>1 ? 1 : x; }
+
+struct Ray {
+ Vec3 o, d;
+
+ Ray(Vec3 o_, Vec3 d_)
+ : o(o_), d(d_) {}
+};
+
+typedef Imath::Vec3<double> Vec3d;
+
+struct Sphere {
+ double radius;
+ Vec3d p;
+ ShadingAttribStateRef shaderstate;
+ const char *shadername;
+ bool light;
+
+ Sphere(double radius_, Vec3d p_, const char *shadername_, bool light_)
+ : radius(radius_), p(p_), shadername(shadername_), light(light_) {}
+
+ // returns distance, 0 if no hit
+ double intersect(const Ray &r) const
+ {
+ // Solve t^2*d.d + 2*t*(o-p).d +(o-p).(o-p)-R^2 = 0
+ Vec3d op = p - Vec3d(r.o.x, r.o.y, r.o.z);
+ double t, eps = 1e-4;
+ double b = op.dot(Vec3(r.d.x, r.d.y, r.d.z));
+ double det = b*b - op.dot(op) + radius*radius;
+
+ if(det<0) return 0;
+ else {
+ det=sqrt(det);
+ return(t=b-det) > eps ? t :((t=b+det)>eps ? t : 0);
+ }
+ }
+
+ Vec3 normal(const Vec3& P)
+ {
+ return Vec3(P[0] - p[0], P[1] - p[1], P[2] - p[2]).normalized();
+ }
+};
+
+// Scene(modified cornell box): radius, position, shader name
+Sphere spheres[] = {
+ Sphere(1e5, Vec3d(1e5+1, 40.8, 81.6), "cornell_wall", false), // left
+ Sphere(1e5, Vec3d(-1e5+99, 40.8, 81.6), "cornell_wall", false), // right
+ Sphere(1e5, Vec3d(50, 40.8, 1e5), "cornell_wall", false), // back
+ Sphere(1e5, Vec3d(50, 1e5, 81.6), "cornell_wall", false), // front
+ Sphere(1e5, Vec3d(50, -1e5+81.6, 81.6), "cornell_wall", false), // bottom
+ Sphere(1e5, Vec3d(50, 40.8, -1e5+170), "cornell_wall", false), // top
+ Sphere(16.5, Vec3d(27, 16.5, 47), "mirror", false), // left
ball
+ Sphere(16.5, Vec3d(73, 16.5, 78), "glass", false), // right
ball
+ Sphere(600, Vec3d(50, 681.6-1.27, 81.6),"emitter", true) // light
+};
+
+int numSpheres = sizeof(spheres)/sizeof(Sphere);
+
+inline bool intersect(const Ray &r, double &t, int &id)
+{
+ double d, inf=t=1e20;
+
+ for(int i=sizeof(spheres)/sizeof(Sphere); i--;)
+ if((d=spheres[i].intersect(r))&&d<t) {
+ t=d;
+ id=i;
+ }
+
+ return t<inf;
+}
+
+void write_image(float *buffer, int w, int h)
+{
+ // write image using OIIO
+ ImageOutput *out = ImageOutput::create(outputfile);
+ ImageSpec spec(w, h, 3, TypeDesc::UINT8);
+
+ out->open(outputfile, spec);
+ out->write_image(TypeDesc::FLOAT, buffer);
+
+ out->close();
+ delete out;
+}
+
+
+int main (){
+
+ OSLRenderer oslr;
+
+ // camera parameters
+ Vec3 cam_o = Vec3(50, 52, 295.6); // positions
+ Vec3 cam_d = Vec3(0, -0.042612, -1).normalized(); // direction
+ Vec3 cam_x = Vec3(w*.5135/h, 0.0, 0.0); // ???
+ Vec3 cam_y = (cam_x.cross(cam_d)).normalized()*.5135; // ???
+
+ // pixels
+ Color3 *buffer = new Color3[w*h];
+ for(int i=0; i<w*h; i++)
+ buffer[i] = Color3(0.0f);
+
+ float scale = 10.0f;
+ int samps = 1; // number of samples
+
+ for(int y=0; y<h; y++) {
+
+ fprintf(stderr, "\rRendering (%d spp) %5.2f%%", samps*4, 100.*y/(h-1));
+
+ Xi[0] = 0;
+ Xi[1] = 0;
+ Xi[2] = y*y*y;
+
+ for(int x=0; x<w; x++) {
+
+ int i = (h - y - 1)*w + x;
+
+ // 2x2 subixel with tent filter
+ for(int sy = 0; sy < 2; sy++) {
+ for(int sx = 0; sx < 2; sx++) {
+ Vec3 r(0.0f);
+
+ for(int s=0; s<samps; s++) {
+ double r1 = 2*erand48(Xi);
+ double r2 = 2*erand48(Xi);
+
+ double dx = (r1 < 1)? sqrt(r1)-1: 1-sqrt(2-r1);
+ double dy = (r2 < 1)? sqrt(r2)-1: 1-sqrt(2-r2);
+
+ Vec3 d = cam_x*(((sx+.5 + dx)/2 + x)/w - .5) +
+ cam_y*(((sy+.5 + dy)/2 + y)/h - .5) + cam_d;
+
+ Ray ray(cam_o + d*130, d.normalized());
+
+ // -----------------------------------------
+ // - Fill in RenderInfo -
+ // -----------------------------------------
+ RenderInfo info;
+
+ info.screen_x = x;
+ info.screen_y = y;
+
+ // find the sphere that was hit
+ double t; // distance to intersection
+ int id = 0; // id of the sphere
+ if(!intersect(ray, t, id)) continue;
+ if(id != 6 && id != 7) continue;
+
+ Sphere &obj = spheres[id];
+
+ Vec3 P = ray.o + ray.d*t;
+ info.P[0] = P[0];
+ info.P[1] = P[1];
+ info.P[2] = P[2];
+
+ Vec3 N = obj.normal(P);
+ info.N[0] = N[0];
+ info.N[1] = N[1];
+ info.N[2] = N[2];
+
+ float phi = atan2(P.y, P.x);
+ if(phi < 0.) phi += 2*M_PI;
+ info.u = phi/(2*M_PI);
+ info.v = acos(P.z/obj.radius)/M_PI;
+
+ // tangents
+ float invzr = 1./sqrt(P.x*P.x + P.y*P.y);
+ float cosphi = P.x*invzr;
+ float sinphi = P.y*invzr;
+
+ Vec3 dPdu = Vec3(-2*M_PI*P.y, -2*M_PI*P.x, 0.);
+ Vec3 dPdv = Vec3(P.z*cosphi, P.z*sinphi,
+ -obj.radius*sin(info.v*M_PI))*M_PI;
+
+ info.dPdu[0] = dPdu[0];
+ info.dPdu[1] = dPdu[1];
+ info.dPdu[2] = dPdu[2];
+
+ info.dPdv[0] = dPdv[0];
+ info.dPdv[1] = dPdv[1];
+ info.dPdv[2] = dPdv[2];
+
+
+ info.depth = 0;
+ info.isbackfacing = 0;
+ info.surfacearea = 1.0;
+
+ r = r + oslr.QueryColor(&info)*(1.0f/samps);
+ }
+
+ // normalize
+ r = Vec3(clamp(r.x),
+ clamp(r.y),
+ clamp(r.z));
+ buffer[i] += r*scale*0.25f;
+ }
+ }
+ // gamma correction
+ buffer[i] = Vec3(pow(buffer[i].x, 1.0/2.2),
+ pow(buffer[i].y, 1.0/2.2),
+ pow(buffer[i].z, 1.0/2.2));
+ }
+ }
+
+ write_image((float*)buffer, w, h);
+ delete buffer;
+
+ return 0;
+}
Property changes on: brlcad/trunk/src/liboptical/osl_rt.cpp
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:eol-style
+ native
Modified: brlcad/trunk/src/liboptical/render_svc.cpp
===================================================================
--- brlcad/trunk/src/liboptical/render_svc.cpp 2011-06-10 17:17:15 UTC (rev
44890)
+++ brlcad/trunk/src/liboptical/render_svc.cpp 2011-06-10 19:03:37 UTC (rev
44891)
@@ -1,6 +1,5 @@
#include "render_svc.h"
-#include "render_svc.h"
using namespace OSL;
#ifdef OSL_NAMESPACE
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
------------------------------------------------------------------------------
EditLive Enterprise is the world's most technically advanced content
authoring tool. Experience the power of Track Changes, Inline Image
Editing and ensure content is compliant with Accessibility Checking.
http://p.sf.net/sfu/ephox-dev2dev
_______________________________________________
BRL-CAD Source Commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/brlcad-commits