Skip to content

Commit

Permalink
Set up ray types more or less correctly for testrender
Browse files Browse the repository at this point in the history
Try to set ray types correctly for rays -- a little naive now, only
using "camera", "shadow", and "diffuse" for all rays (leave
distinguishing between reflection, refraction, diffuse, glossy for
another day, and currently testrender doesn't support subsurface or
displacement, but maybe someday).

Also, for testshade, improve efficency -- don't decode raytype on
every point.  For each individual shade, testshade was converting the
name of the raytype from a string to a ustring, then decoding to a
bitfield. Pull it all out of the loops by computing the bitfield once.

Signed-off-by: Larry Gritz <lg@larrygritz.com>
  • Loading branch information
lgritz committed Feb 20, 2023
1 parent 23edc18 commit 30678fb
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
23 changes: 20 additions & 3 deletions src/testrender/raytracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,24 @@ ortho(const Vec3& n, Vec3& x, Vec3& y)

// Note: not used in OptiX mode
struct Ray {
Ray(const Vec3& o, const Vec3& d, float radius, float spread)
: origin(o), direction(d), radius(radius), spread(spread)
enum RayType {
CAMERA = 1,
SHADOW = 2,
REFLECTION = 4,
REFRACTION = 8,
DIFFUSE = 16,
GLOSSY = 32,
SUBSURFACE = 64,
DISPLACEMENT = 128
};

Ray(const Vec3& o, const Vec3& d, float radius, float spread,
RayType raytype)
: origin(o)
, direction(d)
, radius(radius)
, spread(spread)
, raytype(static_cast<int>(raytype))
{
}

Expand Down Expand Up @@ -72,6 +88,7 @@ struct Ray {

Vec3 origin, direction;
float radius, spread;
int raytype;
};


Expand Down Expand Up @@ -116,7 +133,7 @@ struct Camera {
const float cos_a = dir.dot(v);
const float spread
= sqrtf(invw * invh * cx.length() * cy.length() * cos_a) * cos_a;
return Ray(eye, v, 0, spread);
return Ray(eye, v, 0, spread, Ray::CAMERA);
}

// Specified by user:
Expand Down
8 changes: 5 additions & 3 deletions src/testrender/simpleraytracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,8 @@ SimpleRaytracer::subpixel_radiance(float x, float y, Sampler& sampler,
b.pdf);
if ((contrib.x + contrib.y + contrib.z) > 0) {
int shadow_id = id;
Ray shadow_ray = Ray(sg.P, bg_dir.val(), radius, 0);
Ray shadow_ray = Ray(sg.P, bg_dir.val(), radius, 0,
Ray::SHADOW);
Dual2<float> shadow_dist;
if (!scene.intersect(shadow_ray, shadow_dist,
shadow_id)) // ray reached the background?
Expand All @@ -956,7 +957,7 @@ SimpleRaytracer::subpixel_radiance(float x, float y, Sampler& sampler,
* MIS::power_heuristic<MIS::EVAL_WEIGHT>(light_pdf,
b.pdf);
if ((contrib.x + contrib.y + contrib.z) > 0) {
Ray shadow_ray = Ray(sg.P, ldir, radius, 0);
Ray shadow_ray = Ray(sg.P, ldir, radius, 0, Ray::SHADOW);
// trace a shadow ray and see if we actually hit the target
// in this tiny renderer, tracing a ray is probably cheaper than evaluating the light shader
int shadow_id = id; // ignore self hit
Expand All @@ -979,7 +980,8 @@ SimpleRaytracer::subpixel_radiance(float x, float y, Sampler& sampler,
// trace indirect ray and continue
BSDF::Sample p = result.bsdf.sample(-sg.I, xi, yi, zi);
path_weight *= p.weight;
bsdf_pdf = p.pdf;
bsdf_pdf = p.pdf;
r.raytype = Ray::DIFFUSE; // FIXME? Use DIFFUSE for all indiirect rays
r.direction = p.wi;
r.radius = radius;
// Just simply use roughness as spread slope
Expand Down
11 changes: 11 additions & 0 deletions src/testrender/testrender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ set_shadingsys_options()
shadingsys->attribute("llvm_debugging_symbols", 1);
shadingsys->attribute("llvm_profiling_events", 1);

// Note: MUST match Ray::RayTypes enum
static const char* raytype_names[] = {
"camera", "shadow", "reflection", "refraction",
"diffuse", "glossy", "subsurface", "displacement",
};
shadingsys->attribute("raytypes",
TypeDesc(TypeDesc::STRING,
sizeof(raytype_names)
/ sizeof(raytype_names[0])),
raytype_names);

shadingsys_options_set = true;
}

Expand Down
17 changes: 10 additions & 7 deletions src/testshade/testshade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,10 @@ static ParamValueList params;
static ParamValueList reparams;
static std::string reparam_layer;
static ErrorHandler errhandler;
static int iters = 1;
static std::string raytype = "camera";
static bool raytype_opt = false;
static int iters = 1;
static std::string raytype_name = "camera";
static int raytype_bit = 0;
static bool raytype_opt = false;
static std::string extraoptions;
static std::string texoptions;
static std::string colorspace;
Expand Down Expand Up @@ -703,7 +704,7 @@ getargs(int argc, const char* argv[])
.help("Specify a full group command");
ap.arg("--archivegroup %s:FILENAME", &archivegroup)
.help("Archive the group to a given filename");
ap.arg("--raytype %s", &raytype)
ap.arg("--raytype %s", &raytype_name)
.help("Set the raytype");
ap.arg("--raytype_opt", &raytype_opt)
.help("Specify ray type mask for optimization");
Expand Down Expand Up @@ -889,7 +890,7 @@ setup_shaderglobals(ShaderGlobals& sg, ShadingSystem* shadingsys, int x, int y)
sg.object2common = OSL::TransformationPtr(&Mobj);

// Just make it look like all shades are the result of 'raytype' rays.
sg.raytype = shadingsys->raytype_bit(ustring(raytype));
sg.raytype = raytype_bit;

// Set up u,v to vary across the "patch", and also their derivatives.
// Note that since u & x, and v & y are aligned, we only need to set
Expand Down Expand Up @@ -1099,10 +1100,10 @@ setup_output_images(SimpleRenderer* rend, ShadingSystem* shadingsys,
// not to actually run the shader.
OSL::PerThreadInfo* thread_info = shadingsys->create_thread_info();
ShadingContext* ctx = shadingsys->get_context(thread_info);
raytype_bit = shadingsys->raytype_bit(ustring(raytype_name));
ShaderGlobals sg;
setup_shaderglobals(sg, shadingsys, 0, 0);

int raytype_bit = shadingsys->raytype_bit(ustring(raytype));
#if OSL_USE_BATCHED
if (batched) {
// jit_group will optimize the group if necesssary
Expand Down Expand Up @@ -1500,6 +1501,8 @@ shade_region(SimpleRenderer* rend, ShaderGroup* shadergroup, OIIO::ROI roi,
// Set up shader globals and a little test grid of points to shade.
ShaderGlobals shaderglobals;

raytype_bit = shadingsys->raytype_bit(ustring(raytype_name));

// Loop over all pixels in the image (in x and y)...
for (int y = roi.ybegin; y < roi.yend; ++y) {
int shadeindex = y * xres + roi.xbegin;
Expand Down Expand Up @@ -1583,7 +1586,7 @@ setup_uniform_shaderglobals(BatchedShaderGlobals<WidthT>& bsg,
usg.renderstate = &bsg;

// Just make it look like all shades are the result of 'raytype' rays.
usg.raytype = shadingsys->raytype_bit(ustring(raytype));
usg.raytype = shadingsys->raytype_bit(ustring(raytype_name));
;


Expand Down

0 comments on commit 30678fb

Please sign in to comment.