-
Notifications
You must be signed in to change notification settings - Fork 354
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
Add API for building attribute getter free functions. #1704
Changes from 1 commit
d318962
1176fd4
c312d24
07bf09a
6e352b6
d3ec4fe
637275d
e4efe31
4ad7c1d
9c929bd
587fe5e
46cc9e2
9f05e03
02c4237
028aa6c
e90f4e9
0a1498d
0db9c26
6c03cda
f914eeb
1577a8d
630530f
a0dd0ed
6f68b60
651c7f9
3edffb9
697d1d3
217a238
3006684
3a0d66c
74048bc
f8e449b
c9cebdf
8c5e57c
e21087a
7c62b2f
c260dac
e72ddfb
2798354
c7971ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ class RendererServices; | |
template<int WidthT> class BatchedRendererServices; | ||
class ShadingContext; | ||
struct ShaderGlobals; | ||
class ShaderGroup; | ||
|
||
// Tags for polymorphic dispatch | ||
template<int SimdWidthT> class WidthOf { | ||
|
@@ -29,6 +30,125 @@ typedef const void* TransformationPtr; | |
typedef void (*PrepareClosureFunc)(RendererServices*, int id, void* data); | ||
typedef void (*SetupClosureFunc)(RendererServices*, int id, void* data); | ||
|
||
enum class AttributeSpecBuiltinArg { | ||
ShaderGlobalsPointer, // void* (ideally ShaderGlobals*) | ||
ShadeIndex, // int | ||
Derivatives, // int (ideally bool) | ||
Type, // long long (ideally TypeDesc) | ||
ArrayIndex, // int, Always zero for non-indexed array lookups. | ||
ArrayLookup, // int (ideally bool) | ||
ObjectName, // const char* (ideally ustring) | ||
AlexMWells marked this conversation as resolved.
Show resolved
Hide resolved
|
||
AttributeName, // const char* (ideally ustring) | ||
}; | ||
|
||
// TODO: replace with std::variant | ||
AlexMWells marked this conversation as resolved.
Show resolved
Hide resolved
|
||
class AttributeSpecArg { | ||
union { | ||
AttributeSpecBuiltinArg m_builtin; | ||
int32_t m_int32; | ||
float m_float; | ||
uint64_t m_uint64; | ||
void* m_ptr; | ||
}; | ||
uint32_t m_type; | ||
|
||
public: | ||
AttributeSpecArg() : m_type(0) {} | ||
AttributeSpecArg(AttributeSpecBuiltinArg arg) : m_builtin(arg), m_type(1) {} | ||
AttributeSpecArg(int32_t arg) : m_int32(arg), m_type(2) {} | ||
AttributeSpecArg(float arg) : m_float(arg), m_type(3) {} | ||
AttributeSpecArg(uint64_t arg) : m_uint64(arg), m_type(4) {} | ||
AttributeSpecArg(void* arg) : m_ptr(arg), m_type(5) {} | ||
|
||
template<typename T> bool is_holding() const | ||
{ | ||
if (std::is_same<T, AttributeSpecBuiltinArg>::value) | ||
return m_type == 1; | ||
if (std::is_same<T, int32_t>::value) | ||
return m_type == 2; | ||
if (std::is_same<T, float>::value) | ||
return m_type == 3; | ||
if (std::is_same<T, uint64_t>::value) | ||
return m_type == 4; | ||
if (std::is_same<T, void*>::value) | ||
return m_type == 5; | ||
return false; | ||
} | ||
|
||
template<typename T> T get() const | ||
{ | ||
assert(false); | ||
return T(); | ||
} | ||
}; | ||
|
||
template<> | ||
inline AttributeSpecBuiltinArg | ||
AttributeSpecArg::get() const | ||
{ | ||
assert(is_holding<AttributeSpecBuiltinArg>()); | ||
return m_builtin; | ||
} | ||
|
||
template<> | ||
inline int32_t | ||
AttributeSpecArg::get() const | ||
{ | ||
assert(is_holding<int32_t>()); | ||
return m_int32; | ||
} | ||
|
||
template<> | ||
inline float | ||
AttributeSpecArg::get() const | ||
{ | ||
assert(is_holding<float>()); | ||
return m_float; | ||
} | ||
|
||
template<> | ||
inline uint64_t | ||
AttributeSpecArg::get() const | ||
{ | ||
assert(is_holding<uint64_t>()); | ||
AlexMWells marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return m_uint64; | ||
} | ||
|
||
template<> | ||
inline void* | ||
AttributeSpecArg::get() const | ||
{ | ||
assert(is_holding<void*>()); | ||
return m_ptr; | ||
} | ||
|
||
// The AttributeGetterSpec is never in device code, so it can be a bit more | ||
// complex and have dynamic allocations. Although we do want to | ||
// use an interface and not allow direct data member access | ||
class AttributeGetterSpec { | ||
ustring m_function_name; | ||
AlexMWells marked this conversation as resolved.
Show resolved
Hide resolved
|
||
std::vector<AttributeSpecArg> m_args; | ||
|
||
public: | ||
template<typename... ArgListT> | ||
void set(ustring function_name, ArgListT... args) | ||
AlexMWells marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
m_function_name = function_name; | ||
m_args.clear(); | ||
m_args.insert(m_args.end(), std::initializer_list<AttributeSpecArg> { | ||
AttributeSpecArg { args }... }); | ||
} | ||
|
||
const ustring& function_name() const { return m_function_name; } | ||
size_t arg_count() const { return m_args.size(); } | ||
const AttributeSpecArg& arg(size_t i) const | ||
{ | ||
assert(i < m_args.size()); | ||
return m_args[i]; | ||
} | ||
}; | ||
|
||
|
||
// Turn off warnings about unused params for this file, since we have lots | ||
// of declarations with stub function bodies. | ||
OSL_PRAGMA_WARNING_PUSH | ||
|
@@ -156,6 +276,26 @@ class OSLEXECPUBLIC RendererServices { | |
Vec3* Pout, int npoints, | ||
TypeDesc::VECSEMANTICS vectype); | ||
|
||
/// @brief Builds a free function to provide a value for a given attribute. | ||
AlexMWells marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// @param group The shader group currently requesting the attribute. | ||
/// @param object_name The object name. An empty string will be provided if | ||
/// the value is not specified or it is not known at compile time. | ||
/// @param attribute_name The attribute name. An empty string will be | ||
/// provided if the value is not known at compile time. | ||
/// @param type The type of the value being requested. | ||
/// @param derivatives True if derivatives are also being requested. | ||
/// @param object_lookup True if an object name was specified, even if the | ||
/// value is not known at compile time. | ||
/// @param array_lookup True if the attribute lookup provides an index. | ||
/// @param spec The built attribute getter. | ||
/// @return True if an attribute builder was provided. If false, the | ||
/// existing get_attribute or get_array_attribute functions will be | ||
/// used as the fallback. | ||
AlexMWells marked this conversation as resolved.
Show resolved
Hide resolved
|
||
virtual bool build_attribute_getter(ShaderGroup& group, ustring object_name, | ||
ustring attribute_name, TypeDesc type, | ||
bool derivatives, bool object_lookup, | ||
bool array_lookup, | ||
AttributeGetterSpec& spec); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We might need to add one more argument to this function to allow render developers to pass through their own information. e.g. consider a typical wrapper class:
Some ideas:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be good to have a pointer passed through to jit-compilation. We use the same shader group for multiple compiles depending on how we're going to execute it, so we'd want it to be different-per-compile call. Calls that compile as a convenient side-effect would need to have access to to it as well, so it should be piped through there. It would also be useful in the future for things like satisfying get-texture-info calls and the like at compile time. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, during shader execution,
Although looking at that, we do have the existing host side |
||
|
||
/// Get the named attribute from the renderer and if found then | ||
/// write it into 'val'. Otherwise, return false. If no object is | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3019,6 +3019,69 @@ LLVMGEN(llvm_gen_getattribute) | |
: rop.llvm_load_string(ustring()); | ||
llvm::Value* attr_name_arg = rop.llvm_load_value(Attribute); | ||
|
||
ustring object_name = (object_lookup && ObjectName.is_constant()) | ||
? ObjectName.get_string() | ||
: ustring(); | ||
ustring attribute_name = Attribute.is_constant() ? Attribute.get_string() | ||
: ustring(); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think if the attribute name isn't constant, since a constant getattribute("", result); can be pretty trivially turned into a "just fail" whereas if it was not constant, we want to do the run-time lookup. I bet it's pretty easy to get into a place where a "default" shader ends up doing a getattribute("",result) that can basically be used to skip a bunch of work, but if there's an attribute set in the shading node to cause that to turn into getattribute("mycoolval", result), there could be a lot of other stuff that would get run. If a non-constant attribute name causes us to call build_attribute_getter with "", we can no longer optimize the cases where it's a constant "". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The other thing we could do is just pass in the Symbol& and let the renderer inspect them to find that info out. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If the name isn't constant, what could the renderer figure out from the Symbol&? (not rhetorical) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, my first comment was a bit nonsensical. It could discriminate between a non-const name and a "". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After a bit of discussion with Larry and Alex, our proposal here would be to handle the compile time constant empty attribute name string internally, it would always produce a fail. In such case the I'd prefer to avoid passing LLVM symbols out into user facing code if possible. |
||
AttributeGetterSpec spec; | ||
if (rop.renderer()->build_attribute_getter( | ||
rop.group(), object_name, attribute_name, dest_type, | ||
Destination.has_derivs(), object_lookup, array_lookup, spec) | ||
&& !spec.function_name().empty()) { | ||
std::vector<llvm::Value*> args; | ||
for (int arg_index = 0; arg_index < spec.arg_count(); ++arg_index) { | ||
const auto& arg = spec.arg(arg_index); | ||
if (arg.is_holding<AttributeSpecBuiltinArg>()) { | ||
switch (arg.get<AttributeSpecBuiltinArg>()) { | ||
case AttributeSpecBuiltinArg::ShaderGlobalsPointer: | ||
args.push_back(rop.sg_void_ptr()); | ||
break; | ||
case AttributeSpecBuiltinArg::ShadeIndex: | ||
args.push_back(rop.shadeindex()); | ||
break; | ||
case AttributeSpecBuiltinArg::Derivatives: | ||
args.push_back( | ||
rop.ll.constant((int)Destination.has_derivs())); | ||
break; | ||
case AttributeSpecBuiltinArg::Type: | ||
args.push_back(rop.ll.constant(dest_type)); | ||
break; | ||
case AttributeSpecBuiltinArg::ArrayIndex: | ||
if (array_lookup) | ||
args.push_back(rop.llvm_load_value(Index)); | ||
else | ||
args.push_back(rop.ll.constant((int)0)); | ||
break; | ||
case AttributeSpecBuiltinArg::ArrayLookup: | ||
args.push_back(rop.ll.constant((int)array_lookup)); | ||
break; | ||
case AttributeSpecBuiltinArg::ObjectName: | ||
args.push_back(obj_name_arg); | ||
break; | ||
case AttributeSpecBuiltinArg::AttributeName: | ||
args.push_back(attr_name_arg); | ||
break; | ||
default: assert(false); break; | ||
} | ||
} else if (arg.is_holding<int32_t>()) { | ||
args.push_back(rop.ll.constant(arg.get<int32_t>())); | ||
} else if (arg.is_holding<float>()) { | ||
args.push_back(rop.ll.constant(arg.get<float>())); | ||
} else if (arg.is_holding<uint64_t>()) { | ||
args.push_back(rop.ll.constant64(arg.get<uint64_t>())); | ||
} else if (arg.is_holding<void*>()) { | ||
args.push_back(rop.ll.constant_ptr(arg.get<void*>())); | ||
} else { | ||
assert(false); | ||
} | ||
} | ||
args.push_back(rop.llvm_void_ptr(Destination)); | ||
llvm::Value* r = rop.ll.call_function(spec.function_name().c_str(), | ||
args); | ||
rop.llvm_store_value(r, Result); | ||
} else { | ||
llvm::Value* args[] = { | ||
rop.sg_void_ptr(), | ||
rop.ll.constant((int)Destination.has_derivs()), | ||
|
@@ -3031,6 +3094,7 @@ LLVMGEN(llvm_gen_getattribute) | |
}; | ||
llvm::Value* r = rop.ll.call_function("osl_get_attribute", args); | ||
rop.llvm_store_value(r, Result); | ||
} | ||
|
||
return true; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These additions are not strictly necessary for the rest of this PR to work, but it did seem to be useful to be able to query current symbol locations for a shader group inside the
build_attribute_getter
function.