Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make batched use ustringhash_pod and make batched compile in Windows #1831

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,14 @@ 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)
option (OSL_USTRINGREP_IS_HASH "Always use ustringhash for strings" ON)


set (OSL_NO_DEFAULT_TEXTURESYSTEM OFF CACHE BOOL "Do not use create a raw OIIO::TextureSystem")

if (OSL_USTRINGREP_IS_HASH)
add_definitions ("-DOSL_USTRINGREP_IS_HASH=1")
endif ()
if (OSL_NO_DEFAULT_TEXTURESYSTEM)
add_definitions ("-DOSL_NO_DEFAULT_TEXTURESYSTEM=1")
endif ()
Expand Down
6 changes: 5 additions & 1 deletion src/include/OSL/export.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@
# define OSL_DLL_EXPORT
# define OSL_DLL_LOCAL
# else
# define OSL_DLL_IMPORT __declspec(dllimport)
# ifndef OSL_NO_DLL_IMPORTS
# define OSL_DLL_IMPORT __declspec(dllimport)
# else
# define OSL_DLL_IMPORT
# endif
# define OSL_DLL_EXPORT __declspec(dllexport)
# define OSL_DLL_LOCAL
# endif
Expand Down
12 changes: 9 additions & 3 deletions src/include/OSL/llvm_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,7 @@ class OSLEXECPUBLIC LLVM_Util {
/// Return an llvm::Value holding the given string constant (as
/// determined by the ustring_rep).
llvm::Value* constant(ustring s);
llvm::Value* constant_real_ustring(ustring s);
llvm::Value* constant(string_view s) { return constant(ustring(s)); }

llvm::Constant* constant_array(cspan<llvm::Constant*> constants);
Expand Down Expand Up @@ -750,6 +751,7 @@ class OSLEXECPUBLIC LLVM_Util {
llvm::Constant* wide_constant(size_t i);
llvm::Constant* wide_constant_bool(bool b);
llvm::Value* wide_constant(ustring s);
llvm::Value* wide_constant_real_ustring(ustring s);
llvm::Value* wide_constant(string_view s)
{
return wide_constant(ustring(s));
Expand Down Expand Up @@ -1058,10 +1060,14 @@ class OSLEXECPUBLIC LLVM_Util {
IRBuilder& builder();

int m_debug;
bool m_dumpasm = false;
bool m_jit_fma = false;
bool m_jit_aggressive = false;
bool m_dumpasm = false;
bool m_jit_fma = false;
bool m_jit_aggressive = false;
#ifndef OSL_USTRINGREP_IS_HASH
UstringRep m_ustring_rep = UstringRep::charptr;
#else
UstringRep m_ustring_rep = UstringRep::hash;
#endif
PerThreadInfo::Impl* m_thread;
llvm::LLVMContext* m_llvm_context;
llvm::Module* m_llvm_module;
Expand Down
4 changes: 1 addition & 3 deletions src/include/OSL/oslexec.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ namespace Strings {
#ifdef __CUDA_ARCH__
# define STRDECL(str, var_name)
#else
// Any strings referenced inside of a libsoslexec/wide/*.cpp
// or liboslnoise/wide/*.cpp will need OSLEXECPUBLIC
# define STRDECL(str, var_name) OSLEXECPUBLIC extern const ustring var_name;
# define STRDECL(str, var_name) extern const ustring var_name;
#endif
#include <OSL/strdecls.h>
#undef STRDECL
Expand Down
10 changes: 6 additions & 4 deletions src/include/OSL/oslnoise.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ template <typename S, typename T> OSL_HOSTDEVICE Vec3 vhashnoise (S x, T y);
} // namespace oslnoise



///////////////////////////////////////////////////////////////////////
// Implementation follows...
//
Expand All @@ -110,7 +109,7 @@ typedef void (*NoiseGenericFunc)(int outdim, float *out, bool derivs,
const float *period, NoiseParams *params);
typedef void (*NoiseImplFunc)(float *out, const float *in,
const float *period, NoiseParams *params);

#ifndef INCLUDED_FROM_WIDE
OSLNOISEPUBLIC OSL_HOSTDEVICE
float simplexnoise1 (float x, int seed=0, float *dnoise_dx=NULL);

Expand All @@ -127,7 +126,7 @@ OSLNOISEPUBLIC OSL_HOSTDEVICE
float simplexnoise4 (float x, float y, float z, float w, int seed=0,
float *dnoise_dx=NULL, float *dnoise_dy=NULL,
float *dnoise_dz=NULL, float *dnoise_dw=NULL);

#endif

namespace {

Expand Down Expand Up @@ -2797,6 +2796,7 @@ struct PeriodicSNoise : PeriodicSNoiseImpl<CGDefault> {};
struct PeriodicSNoiseScalar : PeriodicSNoiseImpl<CGScalar> {};


#ifndef INCLUDED_FROM_WIDE
struct SimplexNoise {
OSL_HOSTDEVICE SimplexNoise () { }

Expand Down Expand Up @@ -2908,7 +2908,7 @@ struct SimplexNoise {
result = make_Vec3 (r0, r1, r2);
}
};

#endif

// Scalar version of SimplexNoise that is SIMD friendly suitable to be
// inlined inside of a SIMD loops
Expand Down Expand Up @@ -3024,6 +3024,7 @@ struct SimplexNoiseScalar {
}
};

#ifndef INCLUDED_FROM_WIDE
// Unsigned simplex noise
struct USimplexNoise {
OSL_HOSTDEVICE USimplexNoise () { }
Expand Down Expand Up @@ -3151,6 +3152,7 @@ struct USimplexNoise {
}

};
#endif

// Scalar version of USimplexNoise that is SIMD friendly suitable to be
// inlined inside of a SIMD loops
Expand Down
6 changes: 6 additions & 0 deletions src/liboslexec/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,12 @@ foreach(batched_target ${BATCHED_TARGET_LIST})
target_link_libraries(${batched_target_lib} PRIVATE partio::partio ZLIB::ZLIB)
target_compile_definitions (${batched_target_lib} PRIVATE USE_PARTIO=1)
endif ()

target_compile_definitions (${batched_target_lib}
PRIVATE
OSL_NO_DLL_IMPORTS
INCLUDED_FROM_WIDE
)

endforeach(batched_target)

Expand Down
109 changes: 59 additions & 50 deletions src/liboslexec/batched_backendllvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,6 @@ using namespace OSL::pvt;

OSL_NAMESPACE_ENTER

namespace Strings {

// TODO: What qualifies these to move to strdecls.h?
// Being used in more than one .cpp?

// Shader global strings
static ustring backfacing("backfacing");
static ustring surfacearea("surfacearea");
static ustring object2common("object2common");
static ustring shader2common("shader2common");
static ustring flipHandedness("flipHandedness");
} // namespace Strings

namespace pvt {

namespace // Unnamed
Expand All @@ -40,39 +27,39 @@ namespace // Unnamed
// BatchedShaderGlobals struct in batched_shaderglobals.h,
// as well as the llvm 'sg' type
// defined in BatchedBackendLLVM::llvm_type_sg().
static ustring fields[] = {
static ustringhash fields[] = {
// Uniform
ustring("renderstate"), //
ustring("tracedata"), //
ustring("objdata"), //
ustring("shadingcontext"), //
ustring("renderer"), //
Strings::raytype, //
ustring("pad0"), //
ustring("pad1"), //
ustring("pad2"), //
ustring("pad3"), //
ustring("pad4"), //
ustringhash("renderstate"), //
ustringhash("tracedata"), //
ustringhash("objdata"), //
ustringhash("shadingcontext"), //
ustringhash("renderer"), //
Hashes::raytype, //
ustringhash("pad0"), //
ustringhash("pad1"), //
ustringhash("pad2"), //
ustringhash("pad3"), //
ustringhash("pad4"), //
// Varying
Strings::P, //
ustring("dPdz"), //
Strings::I, //
Strings::N, //
Strings::Ng, //
Strings::u, //
Strings::v, //
Strings::dPdu, //
Strings::dPdv, //
Strings::time, //
Strings::dtime, //
Strings::dPdtime, //
Strings::Ps, //
Strings::object2common, //
Strings::shader2common, //
Strings::Ci, //
Strings::surfacearea, //
Strings::flipHandedness, //
Strings::backfacing
Hashes::P, //
ustringhash("dPdz"), //
Hashes::I, //
Hashes::N, //
Hashes::Ng, //
Hashes::u, //
Hashes::v, //
Hashes::dPdu, //
Hashes::dPdv, //
Hashes::time, //
Hashes::dtime, //
Hashes::dPdtime, //
Hashes::Ps, //
ustringhash("object2common"), //
ustringhash("shader2common"), //
Hashes::Ci, //
ustringhash("surfacearea"), //
ustringhash("flipHandedness"), //
ustringhash("backfacing")
};

static bool field_is_uniform[] = {
Expand Down Expand Up @@ -143,6 +130,10 @@ BatchedBackendLLVM::BatchedBackendLLVM(ShadingSystemImpl& shadingsys,
case 8: m_true_mask_value = Mask<8>(true).value(); break;
default: OSL_ASSERT(0 && "unsupported vector width");
}

// Select the appropriate ustring representation
ll.ustring_rep(LLVM_Util::UstringRep::hash);

ll.dumpasm(shadingsys.m_llvm_dumpasm);
ll.jit_fma(shadingsys.m_llvm_jit_fma);
ll.jit_aggressive(shadingsys.m_llvm_jit_aggressive);
Expand Down Expand Up @@ -191,7 +182,7 @@ BatchedBackendLLVM::llvm_pass_type(const TypeSpec& typespec)
else if (t == TypeDesc::INT)
lt = ll.type_int();
else if (t == TypeDesc::STRING)
lt = (llvm::Type*)ll.type_ustring();
lt = (llvm::Type*)ll.type_real_ustring();
else if (t.aggregate == TypeDesc::VEC3)
lt = (llvm::Type*)ll.type_void_ptr(); //llvm_type_triple_ptr();
else if (t.aggregate == TypeDesc::MATRIX44)
Expand Down Expand Up @@ -271,9 +262,9 @@ BatchedBackendLLVM::llvm_assign_zero(const Symbol& sym)
zero = ll.wide_constant(0);
} else if (elemtype.is_string_based()) {
if (sym.is_uniform())
zero = ll.constant(ustring());
zero = ll.constant(uint64_t(0));
else
zero = ll.wide_constant(ustring());
zero = ll.wide_constant(uint64_t(0));
} else if (elemtype.is_closure_based()) {
if (sym.is_uniform())
zero = ll.void_ptr_null();
Expand Down Expand Up @@ -715,7 +706,8 @@ llvm::Value*
BatchedBackendLLVM::llvm_load_value(const Symbol& sym, int deriv,
llvm::Value* arrayindex, int component,
TypeDesc cast, bool op_is_uniform,
bool index_is_uniform)
bool index_is_uniform,
bool always_real_ustring)
{
// A uniform symbol can be broadcast into a varying value.
// But a varying symbol can NOT be loaded into a uniform value.
Expand Down Expand Up @@ -780,9 +772,15 @@ BatchedBackendLLVM::llvm_load_value(const Symbol& sym, int deriv,
if (sym.typespec().is_string()) {
ustring string_val = sym.get_string();
if (op_is_uniform) {
return ll.constant(string_val);
if (!always_real_ustring)
return ll.constant(string_val);
else
return ll.constant_real_ustring(string_val);
} else {
return ll.wide_constant(string_val);
if (!always_real_ustring)
return ll.wide_constant(string_val);
else
return ll.wide_constant_real_ustring(string_val);
}
}
OSL_ASSERT(0 && "unhandled constant type");
Expand All @@ -796,7 +794,17 @@ BatchedBackendLLVM::llvm_load_value(const Symbol& sym, int deriv,
sym.forced_llvm_bool());
}

llvm::Value*
BatchedBackendLLVM::llvm_const_hash(string_view str)
{
return llvm_const_hash(ustring(str));
}

llvm::Value*
BatchedBackendLLVM::llvm_const_hash(ustring str)
{
return ll.constant64((uint64_t)str.hash());
}

llvm::Value*
BatchedBackendLLVM::llvm_load_mask(const Symbol& cond)
Expand Down Expand Up @@ -1717,6 +1725,7 @@ BatchedBackendLLVM::llvm_call_function(const FuncSpec& name,
= llvm_load_value(s, /*deriv=*/d,
/*component*/ c, TypeUnknown,
function_is_uniform);

// Store our wide pointer on the stack
llvm_store_value(wide_value, tmpptr, t, d, NULL, c,
/*dst_is_uniform*/ false);
Expand Down
10 changes: 7 additions & 3 deletions src/liboslexec/batched_backendllvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,14 @@ class BatchedBackendLLVM : public OSOProcessorBase {
/// performed if cast is the default of UNKNOWN).
llvm::Value* llvm_load_value(const Symbol& sym, int deriv,
llvm::Value* arrayindex, int component,
TypeDesc cast = TypeDesc::UNKNOWN,
bool op_is_uniform = true,
bool index_is_uniform = true);
TypeDesc cast = TypeDesc::UNKNOWN,
bool op_is_uniform = true,
bool index_is_uniform = true,
bool always_real_ustring = false);

llvm::Value* llvm_const_hash(string_view str);

llvm::Value* llvm_const_hash(ustring str);

/// Given an llvm::Value* of a pointer (and the type of the data
/// that it points to), Return the llvm::Value* corresponding to the
Expand Down
Loading
Loading