Skip to content

Commit e2caeb0

Browse files
authored
api: Add ShadingSystem query to ask if attribute derivatives are requested. (#1932)
This PR adds an API to ask the shading system if derivatives are required for attributes needed by a shader group. Signed-off-by: Curtis Black <[email protected]>
1 parent b795e3e commit e2caeb0

File tree

4 files changed

+38
-11
lines changed

4 files changed

+38
-11
lines changed

src/include/OSL/oslexec.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,9 @@ class OSLEXECPUBLIC ShadingSystem {
483483
/// ptr userdata_offsets Retrieves a pointer to the array of
484484
/// int describing the userdata offsets
485485
/// within the heap.
486+
/// ptr userdata_derivs Retrieves a pointer to the array of
487+
/// bools (as bytes) describing if derivatives
488+
/// are requested for each userdata.
486489
/// int num_attributes_needed The number of attribute/scope pairs that
487490
/// are known to be queried by the group (the
488491
/// length of the attributes_needed and
@@ -499,6 +502,9 @@ class OSLEXECPUBLIC ShadingSystem {
499502
/// in the attributes_needed array.
500503
/// ptr attribute_types Retrieves a pointer to the array of
501504
/// TypeDesc describing the attributes.
505+
/// ptr attribute_derivs Retrieves a pointer to the array of
506+
/// bools (as bytes) describing if derivatives
507+
/// are requested for each attribute.
502508
/// int unknown_attributes_needed Nonzero if additional attributes may be
503509
/// needed, whose names will not be known
504510
/// until the shader actually runs.

src/liboslexec/oslexec_pvt.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,11 @@ struct AttributeNeeded {
218218
ustring name;
219219
ustring scope;
220220
TypeDesc type;
221+
bool derivs;
221222

222223
AttributeNeeded(ustring name, ustring scope = ustring(),
223-
TypeDesc type = TypeUnknown)
224-
: name(name), scope(scope), type(type)
224+
TypeDesc type = TypeUnknown, bool derivs = false)
225+
: name(name), scope(scope), type(type), derivs(derivs)
225226
{
226227
}
227228

@@ -240,6 +241,7 @@ struct AttributeNeeded {
240241
// Ignore vector semantics
241242
// if (a.type.vecsemantics != b.type.vecsemantics)
242243
// return a.type.vecsemantics < b.type.vecsemantics;
244+
// Do not sort based on derivs
243245
return false; // they are equal
244246
}
245247
};
@@ -2035,6 +2037,7 @@ class ShaderGroup {
20352037
std::vector<ustring> m_attributes_needed;
20362038
std::vector<ustring> m_attribute_scopes;
20372039
std::vector<TypeDesc> m_attribute_types;
2040+
std::vector<char> m_attribute_derivs;
20382041
std::vector<ustring> m_renderer_outputs; ///< Names of renderer outputs
20392042
std::vector<SymLocationDesc> m_symlocs; ///< SORTED!!
20402043
bool m_unknown_textures_needed;

src/liboslexec/runtimeoptimize.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3369,39 +3369,51 @@ RuntimeOptimizer::run()
33693369
Symbol* sym1 = opargsym(op, 1);
33703370
OSL_DASSERT(sym1 && sym1->typespec().is_string());
33713371
if (sym1->is_constant()) {
3372+
const auto insert = [&](const AttributeNeeded& attr) {
3373+
auto found = m_attributes_needed.find(attr);
3374+
if (found == m_attributes_needed.end())
3375+
m_attributes_needed.insert(attr);
3376+
else if (attr.derivs && !found->derivs) {
3377+
m_attributes_needed.erase(found);
3378+
m_attributes_needed.insert(attr);
3379+
}
3380+
};
33723381
if (op.nargs() == 3) {
33733382
Symbol* sym2 = opargsym(op, 2);
33743383
// getattribute( attributename, result )
3375-
m_attributes_needed.insert(
3376-
AttributeNeeded(sym1->get_string(), ustring(),
3377-
sym2->typespec().simpletype()));
3384+
insert(AttributeNeeded(sym1->get_string(), ustring(),
3385+
sym2->typespec().simpletype(),
3386+
sym2->has_derivs()));
33783387
} else if (op.nargs() == 4) {
33793388
Symbol* sym2 = opargsym(op, 2);
33803389
Symbol* sym3 = opargsym(op, 3);
33813390
if (sym2->typespec().is_string()) {
33823391
// getattribute( scopename, attributename, result )
33833392
if (sym2->is_constant()) {
3384-
m_attributes_needed.insert(AttributeNeeded(
3393+
insert(AttributeNeeded(
33853394
sym2->get_string(), sym1->get_string(),
3386-
sym3->typespec().simpletype()));
3395+
sym3->typespec().simpletype(),
3396+
sym3->has_derivs()));
33873397
} else {
33883398
m_unknown_attributes_needed = true;
33893399
}
33903400
} else {
33913401
// getattribute( attributename, arrayindex, result )
3392-
m_attributes_needed.insert(
3402+
insert(
33933403
AttributeNeeded(sym1->get_string(), ustring(),
3394-
sym3->typespec().simpletype()));
3404+
sym3->typespec().simpletype(),
3405+
sym3->has_derivs()));
33953406
}
33963407
} else if (op.nargs() == 5) {
33973408
Symbol* sym2 = opargsym(op, 2);
33983409
Symbol* sym4 = opargsym(op, 4);
33993410
if (sym2->typespec().is_string()) {
34003411
// getattribute( scopename, attributename, arrayindex, result )
34013412
if (sym2->is_constant()) {
3402-
m_attributes_needed.insert(AttributeNeeded(
3413+
insert(AttributeNeeded(
34033414
sym2->get_string(), sym1->get_string(),
3404-
sym4->typespec().simpletype()));
3415+
sym4->typespec().simpletype(),
3416+
sym4->has_derivs()));
34053417
} else {
34063418
m_unknown_attributes_needed = true;
34073419
}

src/liboslexec/shadingsys.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2282,6 +2282,11 @@ ShadingSystemImpl::getattribute(ShaderGroup* group, string_view name,
22822282
*(TypeDesc**)val = n ? &group->m_attribute_types[0] : NULL;
22832283
return true;
22842284
}
2285+
if (name == "attribute_derivs" && type.basetype == TypeDesc::PTR) {
2286+
size_t n = group->m_attribute_derivs.size();
2287+
*(char**)val = n ? &group->m_attribute_derivs[0] : NULL;
2288+
return true;
2289+
}
22852290
if (name == "unknown_attributes_needed" && type == TypeInt) {
22862291
*(int*)val = (int)group->m_unknown_attributes_needed;
22872292
return true;
@@ -3792,6 +3797,7 @@ ShadingSystemImpl::optimize_group(ShaderGroup& group, ShadingContext* ctx,
37923797
group.m_attributes_needed.push_back(f.name);
37933798
group.m_attribute_scopes.push_back(f.scope);
37943799
group.m_attribute_types.push_back(f.type);
3800+
group.m_attribute_derivs.push_back(f.derivs);
37953801
}
37963802
group.m_optimized = true;
37973803

0 commit comments

Comments
 (0)