Skip to content

Commit

Permalink
Docs: New conventions for retrieving renderer information
Browse files Browse the repository at this point in the history
Add the following conventions for attributes that retrieve information about
the renderer:

      "renderer:name"
      "renderer:version"
      "renderer:versionstring"

and the following attribute that retrieves an integer frame number:

      "camera:frame"
  • Loading branch information
lgritz committed May 8, 2020
1 parent 0d5bc28 commit ac6c478
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 11 deletions.
6 changes: 5 additions & 1 deletion src/doc/languagespec.tex
Original file line number Diff line number Diff line change
Expand Up @@ -4966,7 +4966,10 @@ \section{Renderer state and message passing}
\hline
{\bf Name} & {\bf Type} & {\bf Description} \\
\hline
{\cf "osl:version"} & {\cf int} & Major*10000 + Minor*100 + patch. \\
{\cf "osl:version"} & {\cf int} & Major*10000 + Minor*100 + patch. \\[1ex]
{\cf "renderer:name"} & {\cf string} & Name of the renderer. \\
{\cf "renderer:version"} & {\cf int[4]} & Version of renderer (major, minor, release, patch). \\
{\cf "renderer:versionstring"} & {\cf string} & Version of renderer as a string (e.g. \qkw{1.2.7.0}).\\[1ex]
{\cf "shader:shadername"} & {\cf string} & Name of the shader master. \\
{\cf "shader:layername"} & {\cf string} & Name of the layer instance. \\
{\cf "shader:groupname"} & {\cf string} & Name of the shader group. \\
Expand Down Expand Up @@ -4996,6 +4999,7 @@ \section{Renderer state and message passing}
{\cf "camera:shutter_open"} & {\cf float} & Shutter open time. \\
{\cf "camera:shutter_close"} & {\cf float} & Shutter close time. \\
{\cf "camera:shutter"} & {\cf float[2]} & Shutter open and close times. \\
{\cf "camera:frame"} & {\cf int} & Frame number within the shot. \\
{\cf "camera:screen_window"} & {\cf float[4]} & Screen window (xmin, ymin, xmax, ymax). \\
\hline
\end{tabular}
Expand Down
46 changes: 44 additions & 2 deletions src/osltoy/osltoyrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ static ustring u_mouse("mouse");
static constexpr TypeDesc TypeFloatArray2 (TypeDesc::FLOAT, 2);
static constexpr TypeDesc TypeFloatArray4 (TypeDesc::FLOAT, 4);
static constexpr TypeDesc TypeIntArray2 (TypeDesc::INT, 2);

static constexpr TypeDesc TypeIntArray4 (TypeDesc::INT, 4);



Expand All @@ -38,6 +38,7 @@ OSLToyRenderer::OSLToyRenderer ()
Matrix44 M; M.makeIdentity();
camera_params (M, u_perspective, 90.0f,
0.1f, 1000.0f, 256, 256);
shutter (0.0f, 1.0f/48, 0);

// Set up getters
m_attr_getters[ustring("osl:version")] = &OSLToyRenderer::get_osl_version;
Expand All @@ -52,6 +53,7 @@ OSLToyRenderer::OSLToyRenderer ()
m_attr_getters[ustring("camera:shutter")] = &OSLToyRenderer::get_camera_shutter;
m_attr_getters[ustring("camera:shutter_open")] = &OSLToyRenderer::get_camera_shutter_open;
m_attr_getters[ustring("camera:shutter_close")] = &OSLToyRenderer::get_camera_shutter_close;
m_attr_getters[ustring("camera:frame")] = &OSLToyRenderer::get_camera_frame;

// Set up default shaderglobals
ShaderGlobals &sg (m_shaderglobals_template);
Expand Down Expand Up @@ -133,7 +135,6 @@ OSLToyRenderer::camera_params (const Matrix44 &world_to_camera,
m_pixelaspect = 1.0f; // hard-coded
m_hither = hither;
m_yon = yon;
m_shutter[0] = 0.0f; m_shutter[1] = 1.0f; // hard-coded
float frame_aspect = float(xres)/float(yres) * m_pixelaspect;
m_screen_window[0] = -frame_aspect;
m_screen_window[1] = -1.0f;
Expand All @@ -145,6 +146,16 @@ OSLToyRenderer::camera_params (const Matrix44 &world_to_camera,



void
OSLToyRenderer::shutter (float open, float close, int framenumber)
{
m_shutter[0] = open;
m_shutter[1] = close;
m_frame = framenumber;
}



bool
OSLToyRenderer::get_matrix (ShaderGlobals* /*sg*/, Matrix44 &result,
TransformationPtr xform,
Expand Down Expand Up @@ -271,6 +282,25 @@ OSLToyRenderer::get_array_attribute (ShaderGlobals *sg, bool derivatives, ustrin
TypeDesc type, ustring name,
int index, void *val)
{
if (OIIO::Strutil::starts_with (name, "renderer:")) {
if (name == "renderer:name" && type == OIIO::TypeString) {
*(ustring *)val = ustring("OSL testrender");
return true;
}
if (name == "renderer:version" && type == TypeIntArray4) {
int *ival = (int *)val;
ival[0] = OSL_VERSION_MAJOR;
ival[1] = OSL_VERSION_MINOR;
ival[2] = OSL_VERSION_PATCH;
ival[3] = 0;
return true;
}
if (name == "renderer:versionstring" && type == OIIO::TypeString) {
*(ustring *)val = ustring(OSL_LIBRARY_VERSION_STRING);
return true;
}
}

AttrGetterMap::const_iterator g = m_attr_getters.find (name);
if (g != m_attr_getters.end()) {
AttrGetter getter = g->second;
Expand Down Expand Up @@ -517,4 +547,16 @@ OSLToyRenderer::get_camera_screen_window (ShaderGlobals* /*sg*/, bool derivs, us
}


bool
OSLToyRenderer::get_camera_frame (ShaderGlobals *sg, bool derivs, ustring object,
TypeDesc type, ustring name, void *val)
{
if (type == TypeDesc::TypeInt) {
((int *)val)[0] = m_frame;
return true;
}
return false;
}


OSL_NAMESPACE_EXIT
14 changes: 10 additions & 4 deletions src/osltoy/osltoyrenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class OSLToyRenderer : public RendererServices

void set_resolution (int x, int y) { m_xres = x; m_yres = y; }

void shutter (float open, float close, int framenumber);

void set_time (float t) { m_shaderglobals_template.time = t; }

void set_mouse (int x, int y) { m_mouse_x = x; m_mouse_y = y; }
Expand Down Expand Up @@ -88,12 +90,14 @@ class OSLToyRenderer : public RendererServices

// Camera parameters
Matrix44 m_world_to_camera;
ustring m_projection;
float m_fov, m_pixelaspect, m_hither, m_yon;
float m_shutter[2];
float m_screen_window[4];
ustring m_projection {"perspective"};
float m_fov {90}, m_pixelaspect {1.0};
float m_hither {1e-6}, m_yon {1e6};
float m_shutter[2] { 0.0f, 1.0f };
float m_screen_window[4] { -1, 1, -1, 1 };
int m_xres, m_yres;
int m_mouse_x = -1, m_mouse_y = -1;
int m_frame = 0;

// Named transforms
typedef std::map <ustring, std::shared_ptr<Transformation> > TransformMap;
Expand Down Expand Up @@ -135,6 +139,8 @@ class OSLToyRenderer : public RendererServices
TypeDesc type, ustring name, void *val);
bool get_camera_screen_window (ShaderGlobals *sg, bool derivs, ustring object,
TypeDesc type, ustring name, void *val);
bool get_camera_frame (ShaderGlobals *sg, bool derivs, ustring object,
TypeDesc type, ustring name, void *val);

};

Expand Down
44 changes: 44 additions & 0 deletions src/testshade/simplerend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ static ustring u_s("s"), u_t("t");
static TypeDesc TypeFloatArray2 (TypeDesc::FLOAT, 2);
static TypeDesc TypeFloatArray4 (TypeDesc::FLOAT, 4);
static TypeDesc TypeIntArray2 (TypeDesc::INT, 2);
static TypeDesc TypeIntArray4 (TypeDesc::INT, 4);


void register_closures(OSL::ShadingSystem* shadingsys) {
Expand Down Expand Up @@ -126,6 +127,7 @@ SimpleRenderer::SimpleRenderer ()
Matrix44 M; M.makeIdentity();
camera_params (M, u_perspective, 90.0f,
0.1f, 1000.0f, 256, 256);
shutter (0.0f, 1.0f/48, 0);

// Set up getters
m_attr_getters[ustring("osl:version")] = &SimpleRenderer::get_osl_version;
Expand All @@ -140,6 +142,7 @@ SimpleRenderer::SimpleRenderer ()
m_attr_getters[ustring("camera:shutter")] = &SimpleRenderer::get_camera_shutter;
m_attr_getters[ustring("camera:shutter_open")] = &SimpleRenderer::get_camera_shutter_open;
m_attr_getters[ustring("camera:shutter_close")] = &SimpleRenderer::get_camera_shutter_close;
m_attr_getters[ustring("camera:frame")] = &SimpleRenderer::get_camera_frame;
}


Expand Down Expand Up @@ -216,6 +219,16 @@ SimpleRenderer::camera_params (const Matrix44 &world_to_camera,



void
SimpleRenderer::shutter (float open, float close, int framenumber)
{
m_shutter[0] = open;
m_shutter[1] = close;
m_frame = framenumber;
}



bool
SimpleRenderer::get_matrix (ShaderGlobals* /*sg*/, Matrix44 &result,
TransformationPtr xform,
Expand Down Expand Up @@ -342,6 +355,25 @@ SimpleRenderer::get_array_attribute (ShaderGlobals *sg, bool derivatives, ustrin
TypeDesc type, ustring name,
int index, void *val)
{
if (OIIO::Strutil::starts_with (name, "renderer:")) {
if (name == "renderer:name" && type == OIIO::TypeString) {
*(ustring *)val = ustring("OSL testshade");
return true;
}
if (name == "renderer:version" && type == TypeIntArray4) {
int *ival = (int *)val;
ival[0] = OSL_VERSION_MAJOR;
ival[1] = OSL_VERSION_MINOR;
ival[2] = OSL_VERSION_PATCH;
ival[3] = 0;
return true;
}
if (name == "renderer:versionstring" && type == OIIO::TypeString) {
*(ustring *)val = ustring(OSL_LIBRARY_VERSION_STRING);
return true;
}
}

AttrGetterMap::const_iterator g = m_attr_getters.find (name);
if (g != m_attr_getters.end()) {
AttrGetter getter = g->second;
Expand Down Expand Up @@ -592,4 +624,16 @@ SimpleRenderer::add_output (string_view varname, string_view filename,



bool
SimpleRenderer::get_camera_frame (ShaderGlobals *sg, bool derivs, ustring object,
TypeDesc type, ustring name, void *val)
{
if (type == TypeDesc::TypeInt) {
((int *)val)[0] = m_frame;
return true;
}
return false;
}


OSL_NAMESPACE_EXIT
14 changes: 10 additions & 4 deletions src/testshade/simplerend.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,22 @@ class SimpleRenderer : public RendererServices
ShadingSystem *shadingsys = nullptr;
OIIO::ParamValueList options;

void shutter (float open, float close, int framenumber);

protected:
// Camera parameters
Matrix44 m_world_to_camera;
ustring m_projection;
float m_fov, m_pixelaspect, m_hither, m_yon;
float m_shutter[2];
float m_screen_window[4];
ustring m_projection {"perspective"};
float m_fov {90}, m_pixelaspect {1.0};
float m_hither {1e-6}, m_yon {1e6};
float m_shutter[2] { 0.0f, 1.0f };
float m_screen_window[4] { -1, 1, -1, 1 };
int m_xres, m_yres;
std::vector<ShaderGroupRef> m_shaders;
std::vector<ustring> m_outputvars;
std::vector<std::shared_ptr<OIIO::ImageBuf>> m_outputbufs;
std::unique_ptr<OIIO::ErrorHandler> m_errhandler { new OIIO::ErrorHandler };
int m_frame = 0;

// Named transforms
typedef std::map <ustring, std::shared_ptr<Transformation> > TransformMap;
Expand Down Expand Up @@ -162,6 +166,8 @@ class SimpleRenderer : public RendererServices
TypeDesc type, ustring name, void *val);
bool get_camera_screen_window (ShaderGlobals *sg, bool derivs, ustring object,
TypeDesc type, ustring name, void *val);
bool get_camera_frame (ShaderGlobals *sg, bool derivs, ustring object,
TypeDesc type, ustring name, void *val);

};

Expand Down

0 comments on commit ac6c478

Please sign in to comment.