Skip to content

Commit

Permalink
Move string representation entirely to ustringhash -- phase I
Browse files Browse the repository at this point in the history
The first step of the great ustringhash conversion: This changes all
RendererServices methods (i.e., callbacks to the renderer from the
shader JITed code) that used to take ustring parameters, switched to
take ustringhash instead.

The second step -- which I will tackle after this part is merged --
will be to fully switch the in-memory representation of strings during
shading execution to ustringhash on the CPU (thus matching how we've
been doing it on GPU and making a number of data stuctures and
representations identical/shared for CPU and GPU).

But in the mean time, this lets renderers get started changing their
RendererServices specializations. Just like with OSL's internal
implementation, it may be easier for renderers to break the job into
phase I (rendererservices) and phase II (in-memory rep).

Some notes and guideposts:

* In oslconfig.h, define ustringrep to alias to either ustring or
  ustringhash, depending on a new (but temporary) CMake variable
  `OSL_USTRINGREP_IS_HASH`, which is OFF for now, meaning that
  ustringrep is still ustring.

* Also in oslconfig.h, several helper conversion functions among
  ustring, ustringhash, ustringrip.

* RendererServices and BatchedRendererServices (and their various
  subclasses in testshade and testrender): change the method
  signatures from ustring to ustringhash for all methods that are
  reachable from shader code.

Signed-off-by: Larry Gritz <[email protected]>
  • Loading branch information
lgritz committed Oct 11, 2022
1 parent d867d98 commit 39cbd3f
Show file tree
Hide file tree
Showing 25 changed files with 938 additions and 621 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

cmake_minimum_required (VERSION 3.12)

set (OSL_VERSION "1.13.1.0" CACHE STRING "Version")
set (OSL_VERSION "1.13.2.0" CACHE STRING "Version")
project (OSL VERSION ${OSL_VERSION}
LANGUAGES CXX C
HOMEPAGE_URL "https://github.com/AcademySoftwareFoundation/OpenShadingLanguage")
Expand Down Expand Up @@ -100,6 +100,7 @@ set (OSL_SHADER_INSTALL_DIR "${CMAKE_INSTALL_FULL_DATADIR}/${PROJECT_NAME}/shade
set (OSL_PTX_INSTALL_DIR "${CMAKE_INSTALL_FULL_DATADIR}/${PROJECT_NAME}/ptx"
CACHE STRING "Directory where OptiX PTX files will be installed")
set (CMAKE_DEBUG_POSTFIX "" CACHE STRING "Library naming postfix for Debug builds (e.g., '_debug')")
option (OSL_USTRINGREP_IS_HASH "Always use ustringhash for strings" OFF)


set (OSL_NO_DEFAULT_TEXTURESYSTEM OFF CACHE BOOL "Do not use create a raw OIIO::TextureSystem")
Expand Down
67 changes: 35 additions & 32 deletions src/include/OSL/batched_rendererservices.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,13 @@ template<int WidthT> class OSLEXECPUBLIC BatchedRendererServices {
/// Return a Mask with lanes set to true if ok, false if the named matrix
/// is not known.
virtual Mask get_matrix(BatchedShaderGlobals* bsg, Masked<Matrix44> wresult,
ustring from, Wide<const float> wtime)
ustringhash from, Wide<const float> wtime)
{
return Mask(false);
}
virtual Mask get_matrix(BatchedShaderGlobals* bsg, Masked<Matrix44> result,
Wide<const ustring> wfrom, Wide<const float> wtime);
Wide<const ustringhash> wfrom,
Wide<const float> wtime);
virtual bool is_overridden_get_matrix_WmWsWf() const = 0;


Expand All @@ -97,12 +98,12 @@ template<int WidthT> class OSLEXECPUBLIC BatchedRendererServices {
/// particular renderer may have a better technique and overload the
/// implementation.
virtual Mask get_inverse_matrix(BatchedShaderGlobals* bsg,
Masked<Matrix44> wresult, ustring to,
Masked<Matrix44> wresult, ustringhash to,
Wide<const float> wtime);
virtual bool is_overridden_get_inverse_matrix_WmsWf() const = 0;
virtual Mask get_inverse_matrix(BatchedShaderGlobals* bsg,
Masked<Matrix44> wresult,
Wide<const ustring> wto,
Wide<const ustringhash> wto,
Wide<const float> wtime);
virtual bool is_overridden_get_inverse_matrix_WmWsWf() const = 0;

Expand All @@ -124,22 +125,23 @@ template<int WidthT> class OSLEXECPUBLIC BatchedRendererServices {
/// specified (object == ustring()), then the renderer should search *first*
/// for the attribute on the currently shaded object, and next, if
/// unsuccessful, on the currently shaded "scene".
virtual Mask get_attribute(BatchedShaderGlobals* bsg, ustring object,
ustring name, MaskedData wval)
virtual Mask get_attribute(BatchedShaderGlobals* bsg, ustringhash object,
ustringhash name, MaskedData wval)
{
return Mask(false);
}

/// Similar to get_attribute(); this method will fetch the 'index'
/// element of an attribute array.
virtual Mask get_array_attribute(BatchedShaderGlobals* bsg, ustring object,
ustring name, int index, MaskedData wval)
virtual Mask get_array_attribute(BatchedShaderGlobals* bsg,
ustringhash object, ustringhash name,
int index, MaskedData wval)
{
return Mask(false);
}

virtual bool get_attribute_uniform(BatchedShaderGlobals* bsg,
ustring object, ustring name,
ustringhash object, ustringhash name,
RefData val)
{
return false;
Expand All @@ -148,8 +150,9 @@ template<int WidthT> class OSLEXECPUBLIC BatchedRendererServices {
/// Similar to get_attribute(); this method will fetch the 'index'
/// element of an attribute array.
virtual bool get_array_attribute_uniform(BatchedShaderGlobals* bsg,
ustring object, ustring name,
int index, RefData val)
ustringhash object,
ustringhash name, int index,
RefData val)
{
return false;
}
Expand All @@ -159,7 +162,7 @@ template<int WidthT> class OSLEXECPUBLIC BatchedRendererServices {
/// as well. It is assumed the results are varying and returns Mask
// with its bit set to off if no user-data with the given name and type was
/// found.
virtual Mask get_userdata(ustring name, BatchedShaderGlobals* bsg,
virtual Mask get_userdata(ustringhash name, BatchedShaderGlobals* bsg,
MaskedData wval)
{
return Mask(false);
Expand Down Expand Up @@ -206,7 +209,7 @@ template<int WidthT> class OSLEXECPUBLIC BatchedRendererServices {
/// error.
///
virtual Mask
texture(ustring filename, TextureSystem::TextureHandle* texture_handle,
texture(ustringhash filename, TextureSystem::TextureHandle* texture_handle,
TextureSystem::Perthread* texture_thread_info,
const BatchedTextureOptions& options, BatchedShaderGlobals* bsg,
Wide<const float> ws, Wide<const float> wt, Wide<const float> wdsdx,
Expand Down Expand Up @@ -240,7 +243,7 @@ template<int WidthT> class OSLEXECPUBLIC BatchedRendererServices {
/// messages (in case of failure, when the function returns false) will
/// be stored there, leaving it up to the caller/shader to handle the
/// error.
virtual Mask texture3d(ustring filename,
virtual Mask texture3d(ustringhash filename,
TextureSystem::TextureHandle* texture_handle,
TextureSystem::Perthread* texture_thread_info,
const BatchedTextureOptions& options,
Expand Down Expand Up @@ -273,7 +276,7 @@ template<int WidthT> class OSLEXECPUBLIC BatchedRendererServices {
/// messages (in case of failure, when the function returns false) will
/// be stored there, leaving it up to the caller/shader to handle the
/// error.
virtual Mask environment(ustring filename,
virtual Mask environment(ustringhash filename,
TextureSystem::TextureHandle* texture_handle,
TextureSystem::Perthread* texture_thread_info,
const BatchedTextureOptions& options,
Expand All @@ -300,21 +303,21 @@ template<int WidthT> class OSLEXECPUBLIC BatchedRendererServices {

virtual TextureSystem::TextureHandle* resolve_udim_uniform(
BatchedShaderGlobals* bsg, TexturePerthread* texture_thread_info,
ustring filename, TextureSystem::TextureHandle* texture_handle, float S,
float T);
ustringhash filename, TextureSystem::TextureHandle* texture_handle,
float S, float T);

virtual void resolve_udim(BatchedShaderGlobals* bsg,
TexturePerthread* texture_thread_info,
ustring filename,
ustringhash filename,
TextureSystem::TextureHandle* texture_handle,
Wide<const float> wS, Wide<const float> wT,
Masked<TextureSystem::TextureHandle*> wresult);

// Assumes any UDIM has been resolved already
virtual bool get_texture_info_uniform(
BatchedShaderGlobals* bsg, TexturePerthread* texture_thread_info,
ustring filename, TextureSystem::TextureHandle* texture_handle,
int subimage, ustring dataname, RefData val);
ustringhash filename, TextureSystem::TextureHandle* texture_handle,
int subimage, ustringhash dataname, RefData val);


/// Lookup nearest points in a point cloud. It will search for
Expand Down Expand Up @@ -407,26 +410,25 @@ template<int WidthT> class OSLEXECPUBLIC BatchedRendererServices {
};


virtual void pointcloud_search(BatchedShaderGlobals* bsg, ustring filename,
const void* wcenter,
virtual void pointcloud_search(BatchedShaderGlobals* bsg,
ustringhash filename, const void* wcenter,
Wide<const float> wradius, int max_points,
bool sort, PointCloudSearchResults& results);
virtual bool is_overridden_pointcloud_search() const = 0;


virtual Mask pointcloud_get(BatchedShaderGlobals* bsg, ustring filename,
virtual Mask pointcloud_get(BatchedShaderGlobals* bsg, ustringhash filename,
Wide<const int[]> windices,
Wide<const int> wnum_points, ustring attr_name,
MaskedData wout_data);
Wide<const int> wnum_points,
ustringhash attr_name, MaskedData wout_data);
virtual bool is_overridden_pointcloud_get() const = 0;


virtual Mask pointcloud_write(BatchedShaderGlobals* bsg, ustring filename,
Wide<const OSL::Vec3> wpos, int nattribs,
const ustring* attr_names,
const TypeDesc* attr_types,
const void** pointers_to_wide_attr_value,
Mask mask);
virtual Mask
pointcloud_write(BatchedShaderGlobals* bsg, ustringhash filename,
Wide<const OSL::Vec3> wpos, int nattribs,
const ustring* attr_names, const TypeDesc* attr_types,
const void** pointers_to_wide_attr_value, Mask mask);
virtual bool is_overridden_pointcloud_write() const = 0;

/// Options for the trace call.
Expand All @@ -441,7 +443,8 @@ template<int WidthT> class OSLEXECPUBLIC BatchedRendererServices {
Wide<const Vec3> wdRdy);

virtual void getmessage(BatchedShaderGlobals* bsg, Masked<int> wresult,
ustring source, ustring name, MaskedData wval);
ustringhash source, ustringhash name,
MaskedData wval);

// pointcloud_search is T.B.D.

Expand Down
50 changes: 50 additions & 0 deletions src/include/OSL/oslconfig.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,56 @@ fmtformat(const Str& fmt, Args&&... args)



/// OSL_USTRINGREP_IS_HASH will be 1 if the build-time option was enabled to
/// make the ustringrep be a ustringhash, rather than a ustring directly.
#cmakedefine01 OSL_USTRINGREP_IS_HASH

#if OSL_USTRINGREP_IS_HASH
using ustringrep = ustringhash;
#else
using ustringrep = ustring;
#endif


/// Convenience function to convert to a ustring.
inline ustring
ustring_from(ustringhash h)
{
return ustring::from_hash(h.hash());
}

/// Convenience function to convert to a ustring.
inline ustring
ustring_from(ustring u)
{
return u;
}


/// Convenience function to convert to a ustringrep.
inline ustringrep
ustringrep_from(ustringhash h)
{
#if OSL_USTRINGREP_IS_HASH
return h;
#else
return ustring::from_hash(h.hash());
#endif
}

/// Convenience function to convert to a ustringrep.
inline ustringrep
ustringrep_from(ustring u)
{
#if OSL_USTRINGREP_IS_HASH
return u.hash();
#else
return u;
#endif
}



// N.B. SymArena is not really "configuration", but we cram it here for
// lack of a better home.

Expand Down
Loading

0 comments on commit 39cbd3f

Please sign in to comment.